gcc/ChangeLog:
[official-gcc.git] / gcc / config / darwin.c
blob949db25c6500345bc66856a1ce9280143200fd71
1 /* Functions for generic Darwin as target machine for GNU C compiler.
2 Copyright (C) 1989-2017 Free Software Foundation, Inc.
3 Contributed by Apple Computer Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "df.h"
31 #include "memmodel.h"
32 #include "tm_p.h"
33 #include "stringpool.h"
34 #include "attribs.h"
35 #include "insn-config.h"
36 #include "emit-rtl.h"
37 #include "cgraph.h"
38 #include "lto-streamer.h"
39 #include "output.h"
40 #include "varasm.h"
41 #include "stor-layout.h"
42 #include "explow.h"
43 #include "expr.h"
44 #include "langhooks.h"
45 #include "toplev.h"
46 #include "lto-section-names.h"
48 /* Darwin supports a feature called fix-and-continue, which is used
49 for rapid turn around debugging. When code is compiled with the
50 -mfix-and-continue flag, two changes are made to the generated code
51 that allow the system to do things that it would normally not be
52 able to do easily. These changes allow gdb to load in
53 recompilation of a translation unit that has been changed into a
54 running program and replace existing functions and methods of that
55 translation unit with versions of those functions and methods
56 from the newly compiled translation unit. The new functions access
57 the existing static symbols from the old translation unit, if the
58 symbol existed in the unit to be replaced, and from the new
59 translation unit, otherwise.
61 The changes are to insert 5 nops at the beginning of all functions
62 and to use indirection to get at static symbols. The 5 nops
63 are required by consumers of the generated code. Currently, gdb
64 uses this to patch in a jump to the overriding function, this
65 allows all uses of the old name to forward to the replacement,
66 including existing function pointers and virtual methods. See
67 rs6000_emit_prologue for the code that handles the nop insertions.
69 The added indirection allows gdb to redirect accesses to static
70 symbols from the newly loaded translation unit to the existing
71 symbol, if any. @code{static} symbols are special and are handled by
72 setting the second word in the .non_lazy_symbol_pointer data
73 structure to symbol. See indirect_data for the code that handles
74 the extra indirection, and machopic_output_indirection and its use
75 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
76 symbol indirection. */
78 /* For darwin >= 9 (OSX 10.5) the linker is capable of making the necessary
79 branch islands and we no longer need to emit darwin stubs.
80 However, if we are generating code for earlier systems (or for use in the
81 kernel) the stubs might still be required, and this will be set true. */
82 int darwin_emit_branch_islands = false;
84 typedef struct GTY(()) cdtor_record {
85 rtx symbol;
86 int priority; /* [con/de]structor priority */
87 int position; /* original position */
88 } cdtor_record;
90 static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL;
91 static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL;
93 /* A flag to determine whether we are running c++ or obj-c++. This has to be
94 settable from non-c-family contexts too (i.e. we can't use the c_dialect_
95 functions). */
96 int darwin_running_cxx;
98 /* Some code-gen now depends on OS major version numbers (at least). */
99 int generating_for_darwin_version ;
101 /* For older linkers we need to emit special sections (marked 'coalesced') for
102 for weak or single-definition items. */
103 static bool ld_uses_coal_sects = false;
105 /* Section names. */
106 section * darwin_sections[NUM_DARWIN_SECTIONS];
108 /* While we transition to using in-tests instead of ifdef'd code. */
109 #if !HAVE_lo_sum
110 #define gen_macho_high(a,b) (a)
111 #define gen_macho_low(a,b,c) (a)
112 #endif
114 /* True if we're setting __attribute__ ((ms_struct)). */
115 int darwin_ms_struct = false;
117 /* Earlier versions of Darwin as do not recognize an alignment field in
118 .comm directives, this should be set for versions that allow it. */
119 int emit_aligned_common = false;
121 /* A get_unnamed_section callback used to switch to an ObjC section.
122 DIRECTIVE is as for output_section_asm_op. */
124 static void
125 output_objc_section_asm_op (const void *directive)
127 static bool been_here = false;
129 /* The NeXT ObjC Runtime requires these sections to be present and in
130 order in the object. The code below implements this by emitting
131 a section header for each ObjC section the first time that an ObjC
132 section is requested. */
133 if (! been_here)
135 section *saved_in_section = in_section;
136 static const enum darwin_section_enum tomark[] =
138 /* written, cold -> hot */
139 objc_cat_cls_meth_section,
140 objc_cat_inst_meth_section,
141 objc_string_object_section,
142 objc_constant_string_object_section,
143 objc_selector_refs_section,
144 objc_selector_fixup_section,
145 objc_cls_refs_section,
146 objc_class_section,
147 objc_meta_class_section,
148 /* shared, hot -> cold */
149 objc_cls_meth_section,
150 objc_inst_meth_section,
151 objc_protocol_section,
152 objc_class_names_section,
153 objc_meth_var_types_section,
154 objc_meth_var_names_section,
155 objc_category_section,
156 objc_class_vars_section,
157 objc_instance_vars_section,
158 objc_module_info_section,
159 objc_symbols_section,
161 /* ABI=1 */
162 static const enum darwin_section_enum tomarkv1[] =
164 objc1_protocol_ext_section,
165 objc1_class_ext_section,
166 objc1_prop_list_section
168 /* ABI=2 */
169 static const enum darwin_section_enum tomarkv2[] =
171 objc2_message_refs_section,
172 objc2_classdefs_section,
173 objc2_metadata_section,
174 objc2_classrefs_section,
175 objc2_classlist_section,
176 objc2_categorylist_section,
177 objc2_selector_refs_section,
178 objc2_nonlazy_class_section,
179 objc2_nonlazy_category_section,
180 objc2_protocollist_section,
181 objc2_protocolrefs_section,
182 objc2_super_classrefs_section,
183 objc2_image_info_section,
184 objc2_constant_string_object_section
186 size_t i;
188 been_here = true;
189 if (flag_objc_abi < 2)
191 for (i = 0; i < ARRAY_SIZE (tomark); i++)
192 switch_to_section (darwin_sections[tomark[i]]);
193 if (flag_objc_abi == 1)
194 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
195 switch_to_section (darwin_sections[tomarkv1[i]]);
197 else
198 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
199 switch_to_section (darwin_sections[tomarkv2[i]]);
200 /* Make sure we don't get varasm.c out of sync with us. */
201 switch_to_section (saved_in_section);
203 output_section_asm_op (directive);
207 /* Private flag applied to disable section-anchors in a particular section. */
208 #define SECTION_NO_ANCHOR SECTION_MACH_DEP
211 /* Implement TARGET_ASM_INIT_SECTIONS. */
213 void
214 darwin_init_sections (void)
216 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
217 darwin_sections[NAME] = \
218 get_unnamed_section (FLAGS, (OBJC \
219 ? output_objc_section_asm_op \
220 : output_section_asm_op), \
221 "\t" DIRECTIVE);
222 #include "config/darwin-sections.def"
223 #undef DEF_SECTION
225 readonly_data_section = darwin_sections[const_section];
226 exception_section = darwin_sections[darwin_exception_section];
227 eh_frame_section = darwin_sections[darwin_eh_frame_section];
229 /* If our linker is new enough to coalesce weak symbols, then we
230 can just put picbase_thunks into the text section. */
231 if (! ld_uses_coal_sects )
232 darwin_sections[picbase_thunk_section] = text_section;
236 name_needs_quotes (const char *name)
238 int c;
239 while ((c = *name++) != '\0')
240 if (! ISIDNUM (c)
241 && c != '.' && c != '$' && c != '_' )
242 return 1;
243 return 0;
246 /* Return true if SYM_REF can be used without an indirection. */
248 machopic_symbol_defined_p (rtx sym_ref)
250 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
251 return true;
253 /* If a symbol references local and is not an extern to this
254 file, then the symbol might be able to declared as defined. */
255 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
257 /* If the symbol references a variable and the variable is a
258 common symbol, then this symbol is not defined. */
259 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
261 tree decl = SYMBOL_REF_DECL (sym_ref);
262 if (!decl)
263 return true;
264 if (DECL_COMMON (decl))
265 return false;
267 return true;
269 return false;
272 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
273 reference, which will not be changed. */
275 enum machopic_addr_class
276 machopic_classify_symbol (rtx sym_ref)
278 bool function_p;
280 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
281 if (machopic_symbol_defined_p (sym_ref))
282 return (function_p
283 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
284 else
285 return (function_p
286 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
289 #ifndef TARGET_FIX_AND_CONTINUE
290 #define TARGET_FIX_AND_CONTINUE 0
291 #endif
293 /* Indicate when fix-and-continue style code generation is being used
294 and when a reference to data should be indirected so that it can be
295 rebound in a new translation unit to reference the original instance
296 of that data. Symbol names that are for code generation local to
297 the translation unit are bound to the new translation unit;
298 currently this means symbols that begin with L or _OBJC_;
299 otherwise, we indicate that an indirect reference should be made to
300 permit the runtime to rebind new instances of the translation unit
301 to the original instance of the data. */
303 static int
304 indirect_data (rtx sym_ref)
306 int lprefix;
307 const char *name;
309 /* If we aren't generating fix-and-continue code, don't do anything
310 special. */
311 if (TARGET_FIX_AND_CONTINUE == 0)
312 return 0;
314 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
315 are indirected. Symbols that begin with L and _OBJC_ are always
316 bound to the current translation unit as they are used for
317 generated local data of the translation unit. */
319 name = XSTR (sym_ref, 0);
321 lprefix = (((name[0] == '*' || name[0] == '&')
322 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
323 || (strncmp (name, "_OBJC_", 6) == 0));
325 return ! lprefix;
328 static int
329 machopic_data_defined_p (rtx sym_ref)
331 if (indirect_data (sym_ref))
332 return 0;
334 switch (machopic_classify_symbol (sym_ref))
336 case MACHOPIC_DEFINED_DATA:
337 case MACHOPIC_DEFINED_FUNCTION:
338 return 1;
339 default:
340 return 0;
344 void
345 machopic_define_symbol (rtx mem)
347 rtx sym_ref;
349 gcc_assert (GET_CODE (mem) == MEM);
350 sym_ref = XEXP (mem, 0);
351 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
354 /* Return either ORIG or:
356 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
358 depending on MACHO_DYNAMIC_NO_PIC_P. */
360 machopic_gen_offset (rtx orig)
362 if (MACHO_DYNAMIC_NO_PIC_P)
363 return orig;
364 else
366 /* Play games to avoid marking the function as needing pic if we
367 are being called as part of the cost-estimation process. */
368 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
369 crtl->uses_pic_offset_table = 1;
370 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
371 UNSPEC_MACHOPIC_OFFSET);
372 return gen_rtx_CONST (Pmode, orig);
376 static GTY(()) const char * function_base_func_name;
377 static GTY(()) int current_pic_label_num;
378 static GTY(()) int emitted_pic_label_num;
380 static void
381 update_pic_label_number_if_needed (void)
383 const char *current_name;
385 /* When we are generating _get_pc thunks within stubs, there is no current
386 function. */
387 if (current_function_decl)
389 current_name =
390 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
391 if (function_base_func_name != current_name)
393 ++current_pic_label_num;
394 function_base_func_name = current_name;
397 else
399 ++current_pic_label_num;
400 function_base_func_name = "L_machopic_stub_dummy";
404 void
405 machopic_output_function_base_name (FILE *file)
407 /* If dynamic-no-pic is on, we should not get here. */
408 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
410 update_pic_label_number_if_needed ();
411 fprintf (file, "L%d$pb", current_pic_label_num);
414 char curr_picbasename[32];
416 const char *
417 machopic_get_function_picbase (void)
419 /* If dynamic-no-pic is on, we should not get here. */
420 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
422 update_pic_label_number_if_needed ();
423 snprintf (curr_picbasename, 32, "L%d$pb", current_pic_label_num);
424 return (const char *) curr_picbasename;
427 bool
428 machopic_should_output_picbase_label (void)
430 update_pic_label_number_if_needed ();
432 if (current_pic_label_num == emitted_pic_label_num)
433 return false;
435 emitted_pic_label_num = current_pic_label_num;
436 return true;
439 /* The suffix attached to non-lazy pointer symbols. */
440 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
441 /* The suffix attached to stub symbols. */
442 #define STUB_SUFFIX "$stub"
444 typedef struct GTY ((for_user)) machopic_indirection
446 /* The SYMBOL_REF for the entity referenced. */
447 rtx symbol;
448 /* The name of the stub or non-lazy pointer. */
449 const char * ptr_name;
450 /* True iff this entry is for a stub (as opposed to a non-lazy
451 pointer). */
452 bool stub_p;
453 /* True iff this stub or pointer has been referenced. */
454 bool used;
455 } machopic_indirection;
457 struct indirection_hasher : ggc_ptr_hash<machopic_indirection>
459 typedef const char *compare_type;
460 static hashval_t hash (machopic_indirection *);
461 static bool equal (machopic_indirection *, const char *);
464 /* A table mapping stub names and non-lazy pointer names to
465 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
467 static GTY (()) hash_table<indirection_hasher> *machopic_indirections;
469 /* Return a hash value for a SLOT in the indirections hash table. */
471 hashval_t
472 indirection_hasher::hash (machopic_indirection *p)
474 return htab_hash_string (p->ptr_name);
477 /* Returns true if the KEY is the same as that associated with
478 SLOT. */
480 bool
481 indirection_hasher::equal (machopic_indirection *s, const char *k)
483 return strcmp (s->ptr_name, k) == 0;
486 /* Return the name of the non-lazy pointer (if STUB_P is false) or
487 stub (if STUB_B is true) corresponding to the given name.
489 If we have a situation like:
491 global_weak_symbol:
492 ....
493 Lnon_weak_local:
494 ....
496 ld64 will be unable to split this into two atoms (because the "L" makes
497 the second symbol 'invisible'). This means that legitimate direct accesses
498 to the second symbol will appear to be non-allowed direct accesses to an
499 atom of type weak, global which are not allowed.
501 To avoid this, we make the indirections have a leading 'l' (lower-case L)
502 which has a special meaning: linker can see this and use it to determine
503 atoms, but it is not placed into the final symbol table.
505 The implementation here is somewhat heavy-handed in that it will also mark
506 indirections to the __IMPORT,__pointers section the same way which is
507 really unnecessary, since ld64 _can_ split those into atoms as they are
508 fixed size. FIXME: determine if this is a penalty worth extra code to
509 fix.
513 const char *
514 machopic_indirection_name (rtx sym_ref, bool stub_p)
516 char *buffer;
517 const char *name = XSTR (sym_ref, 0);
518 size_t namelen = strlen (name);
519 machopic_indirection *p;
520 bool needs_quotes;
521 const char *suffix;
522 char L_or_l = 'L';
523 const char *prefix = user_label_prefix;
524 const char *quote = "";
525 tree id;
527 id = maybe_get_identifier (name);
528 if (id)
530 tree id_orig = id;
532 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
533 id = TREE_CHAIN (id);
534 if (id != id_orig)
536 name = IDENTIFIER_POINTER (id);
537 namelen = strlen (name);
541 if (name[0] == '*')
543 prefix = "";
544 ++name;
545 --namelen;
548 needs_quotes = name_needs_quotes (name);
549 if (needs_quotes)
551 quote = "\"";
554 if (stub_p)
555 suffix = STUB_SUFFIX;
556 else
558 suffix = NON_LAZY_POINTER_SUFFIX;
559 /* Let the linker see this. */
560 L_or_l = 'l';
563 buffer = XALLOCAVEC (char, 2 /* strlen ("&L") or ("&l") */
564 + strlen (prefix)
565 + namelen
566 + strlen (suffix)
567 + 2 * strlen (quote)
568 + 1 /* '\0' */);
570 /* Construct the name of the non-lazy pointer or stub. */
571 sprintf (buffer, "&%s%c%s%s%s%s", quote, L_or_l, prefix, name, suffix, quote);
573 if (!machopic_indirections)
574 machopic_indirections = hash_table<indirection_hasher>::create_ggc (37);
576 machopic_indirection **slot
577 = machopic_indirections->find_slot_with_hash (buffer,
578 htab_hash_string (buffer),
579 INSERT);
580 if (*slot)
582 p = *slot;
584 else
586 p = ggc_alloc<machopic_indirection> ();
587 p->symbol = sym_ref;
588 p->ptr_name = xstrdup (buffer);
589 p->stub_p = stub_p;
590 p->used = false;
591 *slot = p;
594 return p->ptr_name;
597 /* Return the name of the stub for the mcount function. */
599 const char*
600 machopic_mcount_stub_name (void)
602 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
603 return machopic_indirection_name (symbol, /*stub_p=*/true);
606 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
607 or non-lazy pointer as used -- and mark the object to which the
608 pointer/stub refers as used as well, since the pointer/stub will
609 emit a reference to it. */
611 void
612 machopic_validate_stub_or_non_lazy_ptr (const char *name)
614 machopic_indirection *p
615 = machopic_indirections->find_with_hash (name, htab_hash_string (name));
616 if (p && ! p->used)
618 const char *real_name;
619 tree id;
621 p->used = true;
623 /* Do what output_addr_const will do when we actually call it. */
624 if (SYMBOL_REF_DECL (p->symbol))
625 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
627 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
629 id = maybe_get_identifier (real_name);
630 if (id)
631 mark_referenced (id);
635 /* Transform ORIG, which may be any data source, to the corresponding
636 source using indirections. */
639 machopic_indirect_data_reference (rtx orig, rtx reg)
641 rtx ptr_ref = orig;
643 if (! MACHOPIC_INDIRECT)
644 return orig;
646 if (GET_CODE (orig) == SYMBOL_REF)
648 int defined = machopic_data_defined_p (orig);
650 if (defined && MACHO_DYNAMIC_NO_PIC_P)
652 if (DARWIN_PPC)
654 /* Create a new register for CSE opportunities. */
655 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
656 emit_insn (gen_macho_high (hi_reg, orig));
657 emit_insn (gen_macho_low (reg, hi_reg, orig));
658 return reg;
660 else if (DARWIN_X86)
661 return orig;
662 else
663 /* some other cpu -- writeme! */
664 gcc_unreachable ();
666 else if (defined)
668 rtx offset = NULL;
669 if (DARWIN_PPC || HAVE_lo_sum)
670 offset = machopic_gen_offset (orig);
672 if (DARWIN_PPC)
674 rtx hi_sum_reg = (!can_create_pseudo_p ()
675 ? reg
676 : gen_reg_rtx (Pmode));
678 gcc_assert (reg);
680 emit_insn (gen_rtx_SET (hi_sum_reg,
681 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
682 gen_rtx_HIGH (Pmode, offset))));
683 emit_insn (gen_rtx_SET (reg,
684 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
685 copy_rtx (offset))));
687 orig = reg;
689 else if (HAVE_lo_sum)
691 gcc_assert (reg);
693 emit_insn (gen_rtx_SET (reg, gen_rtx_HIGH (Pmode, offset)));
694 emit_insn (gen_rtx_SET (reg, gen_rtx_LO_SUM (Pmode, reg,
695 copy_rtx (offset))));
696 emit_use (pic_offset_table_rtx);
698 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
700 return orig;
703 ptr_ref = (gen_rtx_SYMBOL_REF
704 (Pmode,
705 machopic_indirection_name (orig, /*stub_p=*/false)));
707 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
709 ptr_ref = gen_const_mem (Pmode, ptr_ref);
710 machopic_define_symbol (ptr_ref);
712 if (DARWIN_X86
713 && reg
714 && MACHO_DYNAMIC_NO_PIC_P)
716 emit_insn (gen_rtx_SET (reg, ptr_ref));
717 ptr_ref = reg;
720 return ptr_ref;
722 else if (GET_CODE (orig) == CONST)
724 /* If "(const (plus ...", walk the PLUS and return that result.
725 PLUS processing (below) will restore the "(const ..." if
726 appropriate. */
727 if (GET_CODE (XEXP (orig, 0)) == PLUS)
728 return machopic_indirect_data_reference (XEXP (orig, 0), reg);
729 else
730 return orig;
732 else if (GET_CODE (orig) == MEM)
734 XEXP (ptr_ref, 0) =
735 machopic_indirect_data_reference (XEXP (orig, 0), reg);
736 return ptr_ref;
738 else if (GET_CODE (orig) == PLUS)
740 rtx base, result;
741 /* When the target is i386, this code prevents crashes due to the
742 compiler's ignorance on how to move the PIC base register to
743 other registers. (The reload phase sometimes introduces such
744 insns.) */
745 if (GET_CODE (XEXP (orig, 0)) == REG
746 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
747 /* Prevent the same register from being erroneously used
748 as both the base and index registers. */
749 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
750 && reg)
752 emit_move_insn (reg, XEXP (orig, 0));
753 XEXP (ptr_ref, 0) = reg;
754 return ptr_ref;
757 /* Legitimize both operands of the PLUS. */
758 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
759 orig = machopic_indirect_data_reference (XEXP (orig, 1),
760 (base == reg ? 0 : reg));
761 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
762 result = plus_constant (Pmode, base, INTVAL (orig));
763 else
764 result = gen_rtx_PLUS (Pmode, base, orig);
766 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
768 if (reg)
770 emit_move_insn (reg, result);
771 result = reg;
773 else
775 result = force_reg (GET_MODE (result), result);
779 return result;
781 return ptr_ref;
784 /* Transform TARGET (a MEM), which is a function call target, to the
785 corresponding symbol_stub if necessary. Return a new MEM. */
788 machopic_indirect_call_target (rtx target)
790 if (! darwin_emit_branch_islands)
791 return target;
793 if (GET_CODE (target) != MEM)
794 return target;
796 if (MACHOPIC_INDIRECT
797 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
798 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
799 & MACHO_SYMBOL_FLAG_DEFINED))
801 rtx sym_ref = XEXP (target, 0);
802 const char *stub_name = machopic_indirection_name (sym_ref,
803 /*stub_p=*/true);
804 machine_mode mode = GET_MODE (sym_ref);
806 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
807 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
808 MEM_READONLY_P (target) = 1;
809 MEM_NOTRAP_P (target) = 1;
812 return target;
816 machopic_legitimize_pic_address (rtx orig, machine_mode mode, rtx reg)
818 rtx pic_ref = orig;
820 if (! MACHOPIC_INDIRECT)
821 return orig;
823 /* First handle a simple SYMBOL_REF or LABEL_REF */
824 if (GET_CODE (orig) == LABEL_REF
825 || (GET_CODE (orig) == SYMBOL_REF
828 /* addr(foo) = &func+(foo-func) */
829 orig = machopic_indirect_data_reference (orig, reg);
831 if (GET_CODE (orig) == PLUS
832 && GET_CODE (XEXP (orig, 0)) == REG)
834 if (reg == 0)
835 return force_reg (mode, orig);
837 emit_move_insn (reg, orig);
838 return reg;
841 if (GET_CODE (orig) == MEM)
843 if (reg == 0)
845 gcc_assert (!reload_in_progress);
846 reg = gen_reg_rtx (Pmode);
849 #if HAVE_lo_sum
850 if (MACHO_DYNAMIC_NO_PIC_P
851 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
852 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
854 #if defined (TARGET_TOC) /* ppc */
855 rtx temp_reg = (!can_create_pseudo_p ()
856 ? reg :
857 gen_reg_rtx (Pmode));
858 rtx asym = XEXP (orig, 0);
859 rtx mem;
861 emit_insn (gen_macho_high (temp_reg, asym));
862 mem = gen_const_mem (GET_MODE (orig),
863 gen_rtx_LO_SUM (Pmode, temp_reg,
864 copy_rtx (asym)));
865 emit_insn (gen_rtx_SET (reg, mem));
866 #else
867 /* Some other CPU -- WriteMe! but right now there are no other
868 platforms that can use dynamic-no-pic */
869 gcc_unreachable ();
870 #endif
871 pic_ref = reg;
873 else
874 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
875 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
877 rtx offset = machopic_gen_offset (XEXP (orig, 0));
878 #if defined (TARGET_TOC) /* i.e., PowerPC */
879 /* Generating a new reg may expose opportunities for
880 common subexpression elimination. */
881 rtx hi_sum_reg = (!can_create_pseudo_p ()
882 ? reg
883 : gen_reg_rtx (Pmode));
884 rtx mem;
885 rtx sum;
887 sum = gen_rtx_HIGH (Pmode, offset);
888 if (! MACHO_DYNAMIC_NO_PIC_P)
889 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
891 emit_insn (gen_rtx_SET (hi_sum_reg, sum));
893 mem = gen_const_mem (GET_MODE (orig),
894 gen_rtx_LO_SUM (Pmode,
895 hi_sum_reg,
896 copy_rtx (offset)));
897 rtx_insn *insn = emit_insn (gen_rtx_SET (reg, mem));
898 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
900 pic_ref = reg;
901 #else
902 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
904 emit_insn (gen_rtx_SET (reg,
905 gen_rtx_HIGH (Pmode,
906 gen_rtx_CONST (Pmode,
907 offset))));
908 emit_insn (gen_rtx_SET (reg,
909 gen_rtx_LO_SUM (Pmode, reg,
910 gen_rtx_CONST (Pmode,
911 copy_rtx (offset)))));
912 pic_ref = gen_rtx_PLUS (Pmode,
913 pic_offset_table_rtx, reg);
914 #endif
916 else
917 #endif /* HAVE_lo_sum */
919 rtx pic = pic_offset_table_rtx;
920 if (GET_CODE (pic) != REG)
922 emit_move_insn (reg, pic);
923 pic = reg;
925 #if 0
926 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
927 #endif
929 if (reload_in_progress)
930 df_set_regs_ever_live (REGNO (pic), true);
931 pic_ref = gen_rtx_PLUS (Pmode, pic,
932 machopic_gen_offset (XEXP (orig, 0)));
935 #if !defined (TARGET_TOC)
936 emit_move_insn (reg, pic_ref);
937 pic_ref = gen_const_mem (GET_MODE (orig), reg);
938 #endif
940 else
943 #if HAVE_lo_sum
944 if (GET_CODE (orig) == SYMBOL_REF
945 || GET_CODE (orig) == LABEL_REF)
947 rtx offset = machopic_gen_offset (orig);
948 #if defined (TARGET_TOC) /* i.e., PowerPC */
949 rtx hi_sum_reg;
951 if (reg == 0)
953 gcc_assert (!reload_in_progress);
954 reg = gen_reg_rtx (Pmode);
957 hi_sum_reg = reg;
959 emit_insn (gen_rtx_SET (hi_sum_reg,
960 (MACHO_DYNAMIC_NO_PIC_P)
961 ? gen_rtx_HIGH (Pmode, offset)
962 : gen_rtx_PLUS (Pmode,
963 pic_offset_table_rtx,
964 gen_rtx_HIGH (Pmode,
965 offset))));
966 emit_insn (gen_rtx_SET (reg,
967 gen_rtx_LO_SUM (Pmode,
968 hi_sum_reg,
969 copy_rtx (offset))));
970 pic_ref = reg;
971 #else
972 emit_insn (gen_rtx_SET (reg, gen_rtx_HIGH (Pmode, offset)));
973 emit_insn (gen_rtx_SET (reg,
974 gen_rtx_LO_SUM (Pmode, reg,
975 copy_rtx (offset))));
976 pic_ref = gen_rtx_PLUS (Pmode,
977 pic_offset_table_rtx, reg);
978 #endif
980 else
981 #endif /* HAVE_lo_sum */
983 if (REG_P (orig)
984 || GET_CODE (orig) == SUBREG)
986 return orig;
988 else
990 rtx pic = pic_offset_table_rtx;
991 if (GET_CODE (pic) != REG)
993 emit_move_insn (reg, pic);
994 pic = reg;
996 #if 0
997 emit_use (pic_offset_table_rtx);
998 #endif
999 if (reload_in_progress)
1000 df_set_regs_ever_live (REGNO (pic), true);
1001 pic_ref = gen_rtx_PLUS (Pmode,
1002 pic,
1003 machopic_gen_offset (orig));
1008 if (GET_CODE (pic_ref) != REG)
1010 if (reg != 0)
1012 emit_move_insn (reg, pic_ref);
1013 return reg;
1015 else
1017 return force_reg (mode, pic_ref);
1020 else
1022 return pic_ref;
1026 else if (GET_CODE (orig) == SYMBOL_REF)
1027 return orig;
1029 else if (GET_CODE (orig) == PLUS
1030 && (GET_CODE (XEXP (orig, 0)) == MEM
1031 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
1032 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
1033 && XEXP (orig, 0) != pic_offset_table_rtx
1034 && GET_CODE (XEXP (orig, 1)) != REG)
1037 rtx base;
1038 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
1040 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1041 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
1042 Pmode, (base == reg ? 0 : reg));
1043 if (GET_CODE (orig) == CONST_INT)
1045 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
1046 is_complex = 1;
1048 else
1049 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
1051 if (reg && is_complex)
1053 emit_move_insn (reg, pic_ref);
1054 pic_ref = reg;
1056 /* Likewise, should we set special REG_NOTEs here? */
1059 else if (GET_CODE (orig) == CONST)
1061 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1064 else if (GET_CODE (orig) == MEM
1065 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
1067 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1068 addr = replace_equiv_address (orig, addr);
1069 emit_move_insn (reg, addr);
1070 pic_ref = reg;
1073 return pic_ref;
1076 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
1077 DATA is the FILE* for assembly output. Called from
1078 htab_traverse. */
1081 machopic_output_indirection (machopic_indirection **slot, FILE *asm_out_file)
1083 machopic_indirection *p = *slot;
1084 rtx symbol;
1085 const char *sym_name;
1086 const char *ptr_name;
1088 if (!p->used)
1089 return 1;
1091 symbol = p->symbol;
1092 sym_name = XSTR (symbol, 0);
1093 ptr_name = p->ptr_name;
1095 if (p->stub_p)
1097 char *sym;
1098 char *stub;
1099 tree id;
1101 id = maybe_get_identifier (sym_name);
1102 if (id)
1104 tree id_orig = id;
1106 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1107 id = TREE_CHAIN (id);
1108 if (id != id_orig)
1109 sym_name = IDENTIFIER_POINTER (id);
1112 sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1113 if (sym_name[0] == '*' || sym_name[0] == '&')
1114 strcpy (sym, sym_name + 1);
1115 else if (sym_name[0] == '-' || sym_name[0] == '+')
1116 strcpy (sym, sym_name);
1117 else
1118 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1120 stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1121 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1122 strcpy (stub, ptr_name + 1);
1123 else
1124 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1126 machopic_output_stub (asm_out_file, sym, stub);
1128 else if (! indirect_data (symbol)
1129 && (machopic_symbol_defined_p (symbol)
1130 || SYMBOL_REF_LOCAL_P (symbol)))
1132 switch_to_section (data_section);
1133 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1134 assemble_label (asm_out_file, ptr_name);
1135 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1136 GET_MODE_SIZE (Pmode),
1137 GET_MODE_ALIGNMENT (Pmode), 1);
1139 else
1141 rtx init = const0_rtx;
1143 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1145 /* Mach-O symbols are passed around in code through indirect
1146 references and the original symbol_ref hasn't passed through
1147 the generic handling and reference-catching in
1148 output_operand, so we need to manually mark weak references
1149 as such. */
1150 if (SYMBOL_REF_WEAK (symbol))
1152 tree decl = SYMBOL_REF_DECL (symbol);
1153 gcc_assert (DECL_P (decl));
1155 if (decl != NULL_TREE
1156 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
1157 /* Handle only actual external-only definitions, not
1158 e.g. extern inline code or variables for which
1159 storage has been allocated. */
1160 && !TREE_STATIC (decl))
1162 fputs ("\t.weak_reference ", asm_out_file);
1163 assemble_name (asm_out_file, sym_name);
1164 fputc ('\n', asm_out_file);
1168 assemble_name (asm_out_file, ptr_name);
1169 fprintf (asm_out_file, ":\n");
1171 fprintf (asm_out_file, "\t.indirect_symbol ");
1172 assemble_name (asm_out_file, sym_name);
1173 fprintf (asm_out_file, "\n");
1175 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1176 have their symbol name instead of 0 in the second entry of
1177 the non-lazy symbol pointer data structure when they are
1178 defined. This allows the runtime to rebind newer instances
1179 of the translation unit with the original instance of the
1180 symbol. */
1182 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1183 && machopic_symbol_defined_p (symbol))
1184 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1186 assemble_integer (init, GET_MODE_SIZE (Pmode),
1187 GET_MODE_ALIGNMENT (Pmode), 1);
1190 return 1;
1193 void
1194 machopic_finish (FILE *asm_out_file)
1196 if (machopic_indirections)
1197 machopic_indirections
1198 ->traverse_noresize<FILE *, machopic_output_indirection> (asm_out_file);
1202 machopic_operand_p (rtx op)
1204 if (MACHOPIC_JUST_INDIRECT)
1205 return (GET_CODE (op) == SYMBOL_REF
1206 && machopic_symbol_defined_p (op));
1207 else
1208 return (GET_CODE (op) == CONST
1209 && GET_CODE (XEXP (op, 0)) == UNSPEC
1210 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1213 /* This function records whether a given name corresponds to a defined
1214 or undefined function or variable, for machopic_classify_ident to
1215 use later. */
1217 void
1218 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1220 rtx sym_ref;
1222 /* Do the standard encoding things first. */
1223 default_encode_section_info (decl, rtl, first);
1225 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1226 return;
1228 sym_ref = XEXP (rtl, 0);
1229 if (TREE_CODE (decl) == VAR_DECL)
1230 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1232 if (!DECL_EXTERNAL (decl)
1233 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1234 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1235 && ((TREE_STATIC (decl)
1236 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1237 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1238 && DECL_INITIAL (decl) != error_mark_node)))
1239 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1241 if (! TREE_PUBLIC (decl))
1242 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1245 void
1246 darwin_mark_decl_preserved (const char *name)
1248 /* Actually we shouldn't mark any local symbol this way, but for now
1249 this only happens with ObjC meta-data. */
1250 if (darwin_label_is_anonymous_local_objc_name (name))
1251 return;
1253 fprintf (asm_out_file, "\t.no_dead_strip ");
1254 assemble_name (asm_out_file, name);
1255 fputc ('\n', asm_out_file);
1258 static section *
1259 darwin_rodata_section (int use_coal, bool zsize)
1261 return (use_coal
1262 ? darwin_sections[const_coal_section]
1263 : (zsize ? darwin_sections[zobj_const_section]
1264 : darwin_sections[const_section]));
1267 static section *
1268 darwin_mergeable_string_section (tree exp,
1269 unsigned HOST_WIDE_INT align)
1271 /* Darwin's ld expects to see non-writable string literals in the .cstring
1272 section. Later versions of ld check and complain when CFStrings are
1273 enabled. Therefore we shall force the strings into .cstring since we
1274 don't support writable ones anyway. */
1275 if ((darwin_constant_cfstrings || flag_merge_constants)
1276 && TREE_CODE (exp) == STRING_CST
1277 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1278 && align <= 256
1279 && (int_size_in_bytes (TREE_TYPE (exp))
1280 == TREE_STRING_LENGTH (exp))
1281 && ((size_t) TREE_STRING_LENGTH (exp)
1282 == strlen (TREE_STRING_POINTER (exp)) + 1))
1283 return darwin_sections[cstring_section];
1285 if (DARWIN_SECTION_ANCHORS && flag_section_anchors
1286 && TREE_CODE (exp) == STRING_CST
1287 && TREE_STRING_LENGTH (exp) == 0)
1288 return darwin_sections[zobj_const_section];
1290 return readonly_data_section;
1293 #ifndef HAVE_GAS_LITERAL16
1294 #define HAVE_GAS_LITERAL16 0
1295 #endif
1297 static section *
1298 darwin_mergeable_constant_section (tree exp,
1299 unsigned HOST_WIDE_INT align,
1300 bool zsize)
1302 machine_mode mode = DECL_MODE (exp);
1303 unsigned int modesize = GET_MODE_BITSIZE (mode);
1305 if (DARWIN_SECTION_ANCHORS
1306 && flag_section_anchors
1307 && zsize)
1308 return darwin_sections[zobj_const_section];
1310 if (flag_merge_constants
1311 && mode != VOIDmode
1312 && mode != BLKmode
1313 && modesize <= align
1314 && align >= 8
1315 && align <= 256
1316 && (align & (align -1)) == 0)
1318 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1320 if (TREE_CODE (size) == INTEGER_CST)
1322 if (wi::eq_p (size, 4))
1323 return darwin_sections[literal4_section];
1324 else if (wi::eq_p (size, 8))
1325 return darwin_sections[literal8_section];
1326 else if (HAVE_GAS_LITERAL16
1327 && TARGET_64BIT
1328 && wi::eq_p (size, 16))
1329 return darwin_sections[literal16_section];
1333 return readonly_data_section;
1336 section *
1337 darwin_tm_clone_table_section (void)
1339 return get_named_section (NULL,
1340 "__DATA,__tm_clone_table,regular,no_dead_strip",
1345 machopic_reloc_rw_mask (void)
1347 return MACHOPIC_INDIRECT ? 3 : 0;
1350 /* We have to deal with ObjC/C++ metadata section placement in the common
1351 code, since it will also be called from LTO.
1353 Return metadata attributes, if present (searching for ABI=2 first)
1354 Return NULL_TREE if no such attributes are found. */
1356 static tree
1357 is_objc_metadata (tree decl)
1359 if (DECL_P (decl)
1360 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1361 && DECL_ATTRIBUTES (decl))
1363 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1364 if (meta)
1365 return meta;
1366 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1367 if (meta)
1368 return meta;
1370 return NULL_TREE;
1373 static int classes_seen;
1374 static int objc_metadata_seen;
1376 /* Return the section required for Objective C ABI 2 metadata. */
1377 static section *
1378 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1380 const char *p;
1381 tree ident = TREE_VALUE (meta);
1382 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1383 p = IDENTIFIER_POINTER (ident);
1385 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi == 2);
1387 objc_metadata_seen = 1;
1389 if (base == data_section)
1390 base = darwin_sections[objc2_metadata_section];
1392 /* Most of the OBJC2 META-data end up in the base section, so check it
1393 first. */
1394 if (!strncmp (p, "V2_BASE", 7))
1395 return base;
1396 else if (!strncmp (p, "V2_STRG", 7))
1397 return darwin_sections[cstring_section];
1399 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1400 return darwin_sections[objc2_classdefs_section];
1401 else if (!strncmp (p, "V2_MREF", 7))
1402 return darwin_sections[objc2_message_refs_section];
1403 else if (!strncmp (p, "V2_CLRF", 7))
1404 return darwin_sections[objc2_classrefs_section];
1405 else if (!strncmp (p, "V2_SURF", 7))
1406 return darwin_sections[objc2_super_classrefs_section];
1407 else if (!strncmp (p, "V2_NLCL", 7))
1408 return darwin_sections[objc2_nonlazy_class_section];
1409 else if (!strncmp (p, "V2_CLAB", 7))
1411 classes_seen = 1;
1412 return darwin_sections[objc2_classlist_section];
1414 else if (!strncmp (p, "V2_SRFS", 7))
1415 return darwin_sections[objc2_selector_refs_section];
1416 else if (!strncmp (p, "V2_NLCA", 7))
1417 return darwin_sections[objc2_nonlazy_category_section];
1418 else if (!strncmp (p, "V2_CALA", 7))
1419 return darwin_sections[objc2_categorylist_section];
1421 else if (!strncmp (p, "V2_PLST", 7))
1422 return darwin_sections[objc2_protocollist_section];
1423 else if (!strncmp (p, "V2_PRFS", 7))
1424 return darwin_sections[objc2_protocolrefs_section];
1426 else if (!strncmp (p, "V2_INFO", 7))
1427 return darwin_sections[objc2_image_info_section];
1429 else if (!strncmp (p, "V2_EHTY", 7))
1430 return ld_uses_coal_sects ? darwin_sections[data_coal_section]
1431 : data_section;
1433 else if (!strncmp (p, "V2_CSTR", 7))
1434 return darwin_sections[objc2_constant_string_object_section];
1436 /* Not recognized, default. */
1437 return base;
1440 /* Return the section required for Objective C ABI 0/1 metadata. */
1441 static section *
1442 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1444 const char *p;
1445 tree ident = TREE_VALUE (meta);
1446 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1447 p = IDENTIFIER_POINTER (ident);
1449 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi < 2);
1451 objc_metadata_seen = 1;
1453 /* String sections first, cos there are lots of strings. */
1454 if (!strncmp (p, "V1_STRG", 7))
1455 return darwin_sections[cstring_section];
1456 else if (!strncmp (p, "V1_CLSN", 7))
1457 return darwin_sections[objc_class_names_section];
1458 else if (!strncmp (p, "V1_METN", 7))
1459 return darwin_sections[objc_meth_var_names_section];
1460 else if (!strncmp (p, "V1_METT", 7))
1461 return darwin_sections[objc_meth_var_types_section];
1463 else if (!strncmp (p, "V1_CLAS", 7))
1465 classes_seen = 1;
1466 return darwin_sections[objc_class_section];
1468 else if (!strncmp (p, "V1_META", 7))
1469 return darwin_sections[objc_meta_class_section];
1470 else if (!strncmp (p, "V1_CATG", 7))
1471 return darwin_sections[objc_category_section];
1472 else if (!strncmp (p, "V1_PROT", 7))
1473 return darwin_sections[objc_protocol_section];
1475 else if (!strncmp (p, "V1_CLCV", 7))
1476 return darwin_sections[objc_class_vars_section];
1477 else if (!strncmp (p, "V1_CLIV", 7))
1478 return darwin_sections[objc_instance_vars_section];
1480 else if (!strncmp (p, "V1_CLCM", 7))
1481 return darwin_sections[objc_cls_meth_section];
1482 else if (!strncmp (p, "V1_CLIM", 7))
1483 return darwin_sections[objc_inst_meth_section];
1484 else if (!strncmp (p, "V1_CACM", 7))
1485 return darwin_sections[objc_cat_cls_meth_section];
1486 else if (!strncmp (p, "V1_CAIM", 7))
1487 return darwin_sections[objc_cat_inst_meth_section];
1488 else if (!strncmp (p, "V1_PNSM", 7))
1489 return darwin_sections[objc_cat_inst_meth_section];
1490 else if (!strncmp (p, "V1_PCLM", 7))
1491 return darwin_sections[objc_cat_cls_meth_section];
1493 else if (!strncmp (p, "V1_CLPR", 7))
1494 return darwin_sections[objc_cat_cls_meth_section];
1495 else if (!strncmp (p, "V1_CAPR", 7))
1496 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1498 else if (!strncmp (p, "V1_PRFS", 7))
1499 return darwin_sections[objc_cat_cls_meth_section];
1500 else if (!strncmp (p, "V1_CLRF", 7))
1501 return darwin_sections[objc_cls_refs_section];
1502 else if (!strncmp (p, "V1_SRFS", 7))
1503 return darwin_sections[objc_selector_refs_section];
1505 else if (!strncmp (p, "V1_MODU", 7))
1506 return darwin_sections[objc_module_info_section];
1507 else if (!strncmp (p, "V1_SYMT", 7))
1508 return darwin_sections[objc_symbols_section];
1509 else if (!strncmp (p, "V1_INFO", 7))
1510 return darwin_sections[objc_image_info_section];
1512 else if (!strncmp (p, "V1_PLST", 7))
1513 return darwin_sections[objc1_prop_list_section];
1514 else if (!strncmp (p, "V1_PEXT", 7))
1515 return darwin_sections[objc1_protocol_ext_section];
1516 else if (!strncmp (p, "V1_CEXT", 7))
1517 return darwin_sections[objc1_class_ext_section];
1519 else if (!strncmp (p, "V2_CSTR", 7))
1520 return darwin_sections[objc_constant_string_object_section];
1522 return base;
1525 section *
1526 machopic_select_section (tree decl,
1527 int reloc,
1528 unsigned HOST_WIDE_INT align)
1530 bool zsize, one, weak, use_coal, ro;
1531 section *base_section = NULL;
1533 weak = (DECL_P (decl)
1534 && DECL_WEAK (decl)
1535 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1537 zsize = (DECL_P (decl)
1538 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1539 && tree_to_uhwi (DECL_SIZE_UNIT (decl)) == 0);
1541 one = DECL_P (decl)
1542 && TREE_CODE (decl) == VAR_DECL
1543 && DECL_COMDAT_GROUP (decl);
1545 use_coal = (weak || one) && ld_uses_coal_sects;
1547 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1549 switch (categorize_decl_for_section (decl, reloc))
1551 case SECCAT_TEXT:
1552 gcc_unreachable ();
1553 break;
1555 case SECCAT_RODATA:
1556 case SECCAT_SRODATA:
1557 base_section = darwin_rodata_section (use_coal, zsize);
1558 break;
1560 case SECCAT_RODATA_MERGE_STR:
1561 base_section = darwin_mergeable_string_section (decl, align);
1562 break;
1564 case SECCAT_RODATA_MERGE_STR_INIT:
1565 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1566 break;
1568 case SECCAT_RODATA_MERGE_CONST:
1569 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1570 break;
1572 case SECCAT_DATA:
1573 case SECCAT_DATA_REL:
1574 case SECCAT_DATA_REL_LOCAL:
1575 case SECCAT_DATA_REL_RO:
1576 case SECCAT_DATA_REL_RO_LOCAL:
1577 case SECCAT_SDATA:
1578 case SECCAT_TDATA:
1579 if (use_coal)
1581 if (ro)
1582 base_section = darwin_sections[const_data_coal_section];
1583 else
1584 base_section = darwin_sections[data_coal_section];
1586 else if (DARWIN_SECTION_ANCHORS
1587 && flag_section_anchors
1588 && zsize)
1590 /* If we're doing section anchors, then punt zero-sized objects into
1591 their own sections so that they don't interfere with offset
1592 computation for the remaining vars. This does not need to be done
1593 for stuff in mergeable sections, since these are ineligible for
1594 anchors. */
1595 if (ro)
1596 base_section = darwin_sections[zobj_const_data_section];
1597 else
1598 base_section = darwin_sections[zobj_data_section];
1600 else if (ro)
1601 base_section = darwin_sections[const_data_section];
1602 else
1603 base_section = data_section;
1604 break;
1605 case SECCAT_BSS:
1606 case SECCAT_SBSS:
1607 case SECCAT_TBSS:
1608 if (use_coal)
1609 base_section = darwin_sections[data_coal_section];
1610 else
1612 if (!TREE_PUBLIC (decl))
1613 base_section = lcomm_section;
1614 else if (bss_noswitch_section)
1615 base_section = bss_noswitch_section;
1616 else
1617 base_section = data_section;
1619 break;
1621 default:
1622 gcc_unreachable ();
1625 /* Darwin weird special cases.
1626 a) OBJC Meta-data. */
1627 if (DECL_P (decl)
1628 && (TREE_CODE (decl) == VAR_DECL
1629 || TREE_CODE (decl) == CONST_DECL)
1630 && DECL_ATTRIBUTES (decl))
1632 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1633 if (meta)
1634 return darwin_objc2_section (decl, meta, base_section);
1635 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1636 if (meta)
1637 return darwin_objc1_section (decl, meta, base_section);
1638 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1639 if (meta)
1640 return base_section; /* GNU runtime is happy with it all in one pot. */
1643 /* b) Constant string objects. */
1644 if (TREE_CODE (decl) == CONSTRUCTOR
1645 && TREE_TYPE (decl)
1646 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1647 && TYPE_NAME (TREE_TYPE (decl)))
1649 tree name = TYPE_NAME (TREE_TYPE (decl));
1650 if (TREE_CODE (name) == TYPE_DECL)
1651 name = DECL_NAME (name);
1653 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1655 if (flag_next_runtime)
1657 if (flag_objc_abi == 2)
1658 return darwin_sections[objc2_constant_string_object_section];
1659 else
1660 return darwin_sections[objc_constant_string_object_section];
1662 else
1663 return darwin_sections[objc_string_object_section];
1665 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1666 return darwin_sections[cfstring_constant_object_section];
1667 else
1668 return base_section;
1670 /* c) legacy meta-data selection. */
1671 else if (TREE_CODE (decl) == VAR_DECL
1672 && DECL_NAME (decl)
1673 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1674 && IDENTIFIER_POINTER (DECL_NAME (decl))
1675 && flag_next_runtime
1676 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1678 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1679 static bool warned_objc_46 = false;
1680 /* We shall assert that zero-sized objects are an error in ObjC
1681 meta-data. */
1682 gcc_assert (tree_to_uhwi (DECL_SIZE_UNIT (decl)) != 0);
1684 /* ??? This mechanism for determining the metadata section is
1685 broken when LTO is in use, since the frontend that generated
1686 the data is not identified. We will keep the capability for
1687 the short term - in case any non-Objective-C programs are using
1688 it to place data in specified sections. */
1689 if (!warned_objc_46)
1691 location_t loc = DECL_SOURCE_LOCATION (decl);
1692 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
1693 " to select meta-data sections is deprecated at 4.6"
1694 " and will be removed in 4.7");
1695 warned_objc_46 = true;
1698 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1699 return darwin_sections[objc_cls_meth_section];
1700 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1701 return darwin_sections[objc_inst_meth_section];
1702 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
1703 return darwin_sections[objc_cat_cls_meth_section];
1704 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
1705 return darwin_sections[objc_cat_inst_meth_section];
1706 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1707 return darwin_sections[objc_class_vars_section];
1708 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1709 return darwin_sections[objc_instance_vars_section];
1710 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1711 return darwin_sections[objc_cat_cls_meth_section];
1712 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1713 return darwin_sections[objc_class_names_section];
1714 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1715 return darwin_sections[objc_meth_var_names_section];
1716 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1717 return darwin_sections[objc_meth_var_types_section];
1718 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1719 return darwin_sections[objc_cls_refs_section];
1720 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1721 return darwin_sections[objc_class_section];
1722 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1723 return darwin_sections[objc_meta_class_section];
1724 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1725 return darwin_sections[objc_category_section];
1726 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1727 return darwin_sections[objc_selector_refs_section];
1728 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1729 return darwin_sections[objc_selector_fixup_section];
1730 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1731 return darwin_sections[objc_symbols_section];
1732 else if (!strncmp (name, "_OBJC_MODULES", 13))
1733 return darwin_sections[objc_module_info_section];
1734 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1735 return darwin_sections[objc_image_info_section];
1736 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1737 return darwin_sections[objc_cat_inst_meth_section];
1738 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1739 return darwin_sections[objc_cat_cls_meth_section];
1740 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1741 return darwin_sections[objc_cat_cls_meth_section];
1742 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1743 return darwin_sections[objc_protocol_section];
1744 else
1745 return base_section;
1748 return base_section;
1751 /* This can be called with address expressions as "rtx".
1752 They must go in "const". */
1754 section *
1755 machopic_select_rtx_section (machine_mode mode, rtx x,
1756 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1758 if (GET_MODE_SIZE (mode) == 8
1759 && (GET_CODE (x) == CONST_INT
1760 || GET_CODE (x) == CONST_WIDE_INT
1761 || GET_CODE (x) == CONST_DOUBLE))
1762 return darwin_sections[literal8_section];
1763 else if (GET_MODE_SIZE (mode) == 4
1764 && (GET_CODE (x) == CONST_INT
1765 || GET_CODE (x) == CONST_WIDE_INT
1766 || GET_CODE (x) == CONST_DOUBLE))
1767 return darwin_sections[literal4_section];
1768 else if (HAVE_GAS_LITERAL16
1769 && TARGET_64BIT
1770 && GET_MODE_SIZE (mode) == 16
1771 && (GET_CODE (x) == CONST_INT
1772 || GET_CODE (x) == CONST_WIDE_INT
1773 || GET_CODE (x) == CONST_DOUBLE
1774 || GET_CODE (x) == CONST_VECTOR))
1775 return darwin_sections[literal16_section];
1776 else if (MACHOPIC_INDIRECT
1777 && (GET_CODE (x) == SYMBOL_REF
1778 || GET_CODE (x) == CONST
1779 || GET_CODE (x) == LABEL_REF))
1780 return darwin_sections[const_data_section];
1781 else
1782 return darwin_sections[const_section];
1785 void
1786 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1788 cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
1790 vec_safe_push (ctors, new_elt);
1792 if (! MACHOPIC_INDIRECT)
1793 fprintf (asm_out_file, ".reference .constructors_used\n");
1796 void
1797 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1799 cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)};
1801 vec_safe_push (dtors, new_elt);
1803 if (! MACHOPIC_INDIRECT)
1804 fprintf (asm_out_file, ".reference .destructors_used\n");
1807 static int
1808 sort_cdtor_records (const void * a, const void * b)
1810 const cdtor_record *cda = (const cdtor_record *)a;
1811 const cdtor_record *cdb = (const cdtor_record *)b;
1812 if (cda->priority > cdb->priority)
1813 return 1;
1814 if (cda->priority < cdb->priority)
1815 return -1;
1816 if (cda->position > cdb->position)
1817 return 1;
1818 if (cda->position < cdb->position)
1819 return -1;
1820 return 0;
1823 static void
1824 finalize_ctors ()
1826 unsigned int i;
1827 cdtor_record *elt;
1829 if (MACHOPIC_INDIRECT)
1830 switch_to_section (darwin_sections[mod_init_section]);
1831 else
1832 switch_to_section (darwin_sections[constructor_section]);
1834 if (vec_safe_length (ctors) > 1)
1835 ctors->qsort (sort_cdtor_records);
1836 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
1838 assemble_align (POINTER_SIZE);
1839 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1843 static void
1844 finalize_dtors ()
1846 unsigned int i;
1847 cdtor_record *elt;
1849 if (MACHOPIC_INDIRECT)
1850 switch_to_section (darwin_sections[mod_term_section]);
1851 else
1852 switch_to_section (darwin_sections[destructor_section]);
1854 if (vec_safe_length (dtors) > 1)
1855 dtors->qsort (sort_cdtor_records);
1856 FOR_EACH_VEC_SAFE_ELT (dtors, i, elt)
1858 assemble_align (POINTER_SIZE);
1859 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1863 void
1864 darwin_globalize_label (FILE *stream, const char *name)
1866 if (!!strncmp (name, "_OBJC_", 6))
1867 default_globalize_label (stream, name);
1870 /* This routine returns non-zero if 'name' starts with the special objective-c
1871 anonymous file-scope static name. It accommodates c++'s mangling of such
1872 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1874 int
1875 darwin_label_is_anonymous_local_objc_name (const char *name)
1877 const unsigned char *p = (const unsigned char *) name;
1878 if (*p != '_')
1879 return 0;
1880 if (p[1] == 'Z' && p[2] == 'L')
1882 p += 3;
1883 while (*p >= '0' && *p <= '9')
1884 p++;
1886 return (!strncmp ((const char *)p, "_OBJC_", 6));
1889 /* LTO support for Mach-O.
1891 This version uses three mach-o sections to encapsulate the (unlimited
1892 number of) lto sections.
1894 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1895 __GNU_LTO, __section_names contains the GNU LTO section names.
1896 __GNU_LTO, __section_index contains an array of values that index these.
1898 Indexed thus:
1899 <section offset from the start of __GNU_LTO, __lto_sections>,
1900 <section length>
1901 <name offset from the start of __GNU_LTO, __section_names,
1902 <name length>.
1904 At present, for both m32 and m64 mach-o files each of these fields is
1905 represented by a uint32_t. This is because, AFAICT, a mach-o object
1906 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1908 uint32_t offset;
1909 "offset An integer specifying the offset to this section in the file." */
1911 /* Count lto section numbers. */
1912 static unsigned int lto_section_num = 0;
1914 /* A vector of information about LTO sections, at present, we only have
1915 the name. TODO: see if we can get the data length somehow. */
1916 typedef struct GTY (()) darwin_lto_section_e {
1917 const char *sectname;
1918 } darwin_lto_section_e ;
1920 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1922 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1923 sections into three Mach-O ones).
1924 NOTE: These names MUST be kept in sync with those in
1925 libiberty/simple-object-mach-o. */
1926 #define LTO_SECTS_SECTION "__wrapper_sects"
1927 #define LTO_NAMES_SECTION "__wrapper_names"
1928 #define LTO_INDEX_SECTION "__wrapper_index"
1930 /* File to temporarily store LTO data. This is appended to asm_out_file
1931 in darwin_end_file. */
1932 static FILE *lto_asm_out_file, *saved_asm_out_file;
1933 static char *lto_asm_out_name;
1935 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1936 asm_out_file and switching to an alternative output file. */
1937 void
1938 darwin_asm_lto_start (void)
1940 gcc_assert (! saved_asm_out_file);
1941 saved_asm_out_file = asm_out_file;
1942 if (! lto_asm_out_name)
1943 lto_asm_out_name = make_temp_file (".lto.s");
1944 lto_asm_out_file = fopen (lto_asm_out_name, "a");
1945 if (lto_asm_out_file == NULL)
1946 fatal_error (input_location,
1947 "failed to open temporary file %s for LTO output",
1948 lto_asm_out_name);
1949 asm_out_file = lto_asm_out_file;
1952 /* Restore asm_out_file. */
1953 void
1954 darwin_asm_lto_end (void)
1956 gcc_assert (saved_asm_out_file);
1957 fclose (lto_asm_out_file);
1958 asm_out_file = saved_asm_out_file;
1959 saved_asm_out_file = NULL;
1962 static void
1963 darwin_asm_dwarf_section (const char *name, unsigned int flags, tree decl);
1965 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
1967 void
1968 darwin_asm_named_section (const char *name,
1969 unsigned int flags,
1970 tree decl ATTRIBUTE_UNUSED)
1972 /* LTO sections go in a special section that encapsulates the (unlimited)
1973 number of GNU LTO sections within a single mach-o one. */
1974 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1975 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1977 darwin_lto_section_e e;
1978 /* We expect certain flags to be set... */
1979 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1980 == (SECTION_DEBUG | SECTION_NAMED));
1982 /* Switch to our combined section. */
1983 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1984 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1985 /* Output a label for the start of this sub-section. */
1986 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1987 lto_section_num, name);
1988 /* We have to jump through hoops to get the values of the intra-section
1989 offsets... */
1990 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1991 lto_section_num, lto_section_num);
1992 fprintf (asm_out_file,
1993 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1994 lto_section_num, lto_section_num+1, lto_section_num);
1995 lto_section_num++;
1996 e.sectname = xstrdup (name);
1997 /* Keep the names, we'll need to make a table later.
1998 TODO: check that we do not revisit sections, that would break
1999 the assumption of how this is done. */
2000 if (lto_section_names == NULL)
2001 vec_alloc (lto_section_names, 16);
2002 vec_safe_push (lto_section_names, e);
2004 else if (strncmp (name, "__DWARF,", 8) == 0)
2005 darwin_asm_dwarf_section (name, flags, decl);
2006 else
2007 fprintf (asm_out_file, "\t.section %s\n", name);
2010 void
2011 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
2013 /* Darwin does not use unique sections. */
2016 /* Handle __attribute__ ((apple_kext_compatibility)).
2017 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
2018 vtable for classes with this attribute (and their descendants) by not
2019 outputting the new 3.0 nondeleting destructor. This means that such
2020 objects CANNOT be allocated on the stack or as globals UNLESS they have
2021 a completely empty `operator delete'.
2022 Luckily, this fits in with the Darwin kext model.
2024 This attribute also disables gcc3's potential overlaying of derived
2025 class data members on the padding at the end of the base class. */
2027 tree
2028 darwin_handle_kext_attribute (tree *node, tree name,
2029 tree args ATTRIBUTE_UNUSED,
2030 int flags ATTRIBUTE_UNUSED,
2031 bool *no_add_attrs)
2033 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
2034 if (! TARGET_KEXTABI)
2036 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2037 "only when compiling a kext", name);
2039 *no_add_attrs = true;
2041 else if (TREE_CODE (*node) != RECORD_TYPE)
2043 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2044 "only to C++ classes", name);
2046 *no_add_attrs = true;
2049 return NULL_TREE;
2052 /* Handle a "weak_import" attribute; arguments as in
2053 struct attribute_spec.handler. */
2055 tree
2056 darwin_handle_weak_import_attribute (tree *node, tree name,
2057 tree ARG_UNUSED (args),
2058 int ARG_UNUSED (flags),
2059 bool * no_add_attrs)
2061 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
2063 warning (OPT_Wattributes, "%qE attribute ignored",
2064 name);
2065 *no_add_attrs = true;
2067 else
2068 declare_weak (*node);
2070 return NULL_TREE;
2073 /* Emit a label for an FDE, making it global and/or weak if appropriate.
2074 The third parameter is nonzero if this is for exception handling.
2075 The fourth parameter is nonzero if this is just a placeholder for an
2076 FDE that we are omitting. */
2078 void
2079 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
2081 char *lab ;
2082 char buf[32];
2083 static int invok_count = 0;
2084 static tree last_fun_decl = NULL_TREE;
2086 /* We use the linker to emit the .eh labels for Darwin 9 and above. */
2087 if (! for_eh || generating_for_darwin_version >= 9)
2088 return;
2090 /* FIXME: This only works when the eh for all sections of a function is
2091 emitted at the same time. If that changes, we would need to use a lookup
2092 table of some form to determine what to do. Also, we should emit the
2093 unadorned label for the partition containing the public label for a
2094 function. This is of limited use, probably, since we do not currently
2095 enable partitioning. */
2096 strcpy (buf, ".eh");
2097 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
2099 if (decl == last_fun_decl)
2101 invok_count++;
2102 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
2104 else
2106 last_fun_decl = decl;
2107 invok_count = 0;
2111 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
2113 if (TREE_PUBLIC (decl))
2115 targetm.asm_out.globalize_label (file, lab);
2116 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
2118 fputs ("\t.private_extern ", file);
2119 assemble_name (file, lab);
2120 fputc ('\n', file);
2124 if (DECL_WEAK (decl))
2126 fputs ("\t.weak_definition ", file);
2127 assemble_name (file, lab);
2128 fputc ('\n', file);
2131 assemble_name (file, lab);
2132 if (empty)
2134 fputs (" = 0\n", file);
2136 /* Mark the absolute .eh and .eh1 style labels as needed to
2137 ensure that we don't dead code strip them and keep such
2138 labels from another instantiation point until we can fix this
2139 properly with group comdat support. */
2140 darwin_mark_decl_preserved (lab);
2142 else
2143 fputs (":\n", file);
2145 free (lab);
2148 static GTY(()) unsigned long except_table_label_num;
2150 void
2151 darwin_emit_except_table_label (FILE *file)
2153 char section_start_label[30];
2155 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2156 except_table_label_num++);
2157 ASM_OUTPUT_LABEL (file, section_start_label);
2159 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2161 void
2162 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2164 const char *nlp_name;
2166 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2168 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2169 fputs ("\t.long\t", file);
2170 ASM_OUTPUT_LABELREF (file, nlp_name);
2171 fputs ("-.", file);
2174 /* If this is uncommented, details of each allocation will be printed
2175 in the asm right before the actual code. WARNING - this will cause some
2176 test-suite fails (since the printout will contain items that some tests
2177 are not expecting) -- so don't leave it on by default (it bloats the
2178 asm too). */
2179 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2181 /* The first two of these routines are ostensibly just intended to put
2182 names into the asm. However, they are both hijacked in order to ensure
2183 that zero-sized items do not make their way into the output. Consequently,
2184 we also need to make these participate in provisions for dealing with
2185 such items in section anchors. */
2187 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2188 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2189 but it does need to be referenced via indirect PIC data pointers.
2190 The machopic_define_symbol calls are telling the machopic subsystem
2191 that the name *is* defined in this module, so it doesn't need to
2192 make them indirect. */
2193 void
2194 darwin_asm_declare_object_name (FILE *file,
2195 const char *nam, tree decl)
2197 const char *xname = nam;
2198 unsigned HOST_WIDE_INT size;
2199 bool local_def, weak;
2201 weak = (DECL_P (decl)
2202 && DECL_WEAK (decl)
2203 && !lookup_attribute ("weak_import",
2204 DECL_ATTRIBUTES (decl)));
2206 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2207 && (!DECL_COMMON (decl)
2208 || !TREE_PUBLIC (decl)));
2210 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2211 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2213 if (local_def)
2215 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2216 if (!weak)
2217 machopic_define_symbol (DECL_RTL (decl));
2220 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2222 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2223 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2224 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2225 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2226 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2227 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2228 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2229 (unsigned long)DECL_INITIAL (decl));
2230 #endif
2232 /* Darwin needs help to support local zero-sized objects.
2233 They must be made at least one byte, and the section containing must be
2234 marked as unsuitable for section-anchors (see storage allocators below).
2236 For non-zero objects this output is handled by varasm.c.
2238 if (!size)
2240 unsigned int l2align = 0;
2242 /* The align must be honored, even for zero-sized. */
2243 if (DECL_ALIGN (decl))
2245 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2246 fprintf (file, "\t.align\t%u\n", l2align);
2249 ASM_OUTPUT_LABEL (file, xname);
2250 size = 1;
2251 fprintf (file, "\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2253 /* Check that we've correctly picked up the zero-sized item and placed it
2254 properly. */
2255 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2256 || (in_section
2257 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2259 else
2260 ASM_OUTPUT_LABEL (file, xname);
2263 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2264 void
2265 darwin_asm_declare_constant_name (FILE *file, const char *name,
2266 const_tree exp ATTRIBUTE_UNUSED,
2267 HOST_WIDE_INT size)
2269 assemble_label (file, name);
2270 /* As for other items, we need at least one byte. */
2271 if (!size)
2273 fputs ("\t.space\t1\n", file);
2274 /* Check that we've correctly picked up the zero-sized item and placed it
2275 properly. */
2276 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2277 || (in_section
2278 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2282 /* Darwin storage allocators.
2284 Zerofill sections are desirable for large blank data since, otherwise, these
2285 data bloat objects (PR33210).
2287 However, section anchors don't work in .zerofill sections (one cannot switch
2288 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2289 to put (at least some) data into 'normal' switchable sections.
2291 Here we set a relatively arbitrary value for the size of an object to trigger
2292 zerofill when section anchors are enabled (anything bigger than a page for
2293 current Darwin implementations). FIXME: there ought to be some objective way
2294 to make this choice.
2296 When section anchor are off this is ignored anyway. */
2298 #define BYTES_ZFILL 4096
2300 /* Emit a chunk of data for items coalesced by the linker. */
2301 static void
2302 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2303 unsigned HOST_WIDE_INT size,
2304 bool use_coal,
2305 unsigned int align)
2307 /* Since the sections used here are coalesced, they will not be eligible
2308 for section anchors, and therefore we don't need to break that out.
2309 CHECKME: for modern linker on PowerPC. */
2310 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2311 switch_to_section (use_coal ? darwin_sections[const_data_coal_section]
2312 : darwin_sections[const_data_section]);
2313 else
2314 switch_to_section (use_coal ? darwin_sections[data_coal_section]
2315 : data_section);
2317 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2318 the align info for zero-sized items... but do it here otherwise. */
2319 if (size && align)
2320 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2322 if (TREE_PUBLIC (decl))
2323 darwin_globalize_label (fp, name);
2325 /* ... and we let it deal with outputting one byte of zero for them too. */
2326 darwin_asm_declare_object_name (fp, name, decl);
2327 if (size)
2328 assemble_zeros (size);
2331 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2332 static void
2333 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2334 unsigned HOST_WIDE_INT size,
2335 unsigned int align, tree meta)
2337 section *ocs = data_section;
2339 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2340 ocs = darwin_objc2_section (decl, meta, ocs);
2341 else
2342 ocs = darwin_objc1_section (decl, meta, ocs);
2344 switch_to_section (ocs);
2346 /* We shall declare that zero-sized meta-data are not valid (yet). */
2347 gcc_assert (size);
2348 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2350 /* ... and we let it deal with outputting one byte of zero for them too. */
2351 darwin_asm_declare_object_name (fp, name, decl);
2352 assemble_zeros (size);
2355 /* This routine emits 'local' storage:
2357 When Section Anchors are off this routine emits .zerofill commands in
2358 sections named for their alignment.
2360 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2361 the .static_data section so that the section anchoring system can see them.
2362 Larger items are still placed in .zerofill sections, addressing PR33210.
2363 The routine has no checking - it is all assumed to be done by the caller.
2365 static void
2366 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2367 unsigned HOST_WIDE_INT size,
2368 unsigned int l2align)
2370 /* FIXME: We have a fudge to make this work with Java even when the target does
2371 not use sections anchors -- Java seems to need at least one small item in a
2372 non-zerofill segment. */
2373 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2374 || (size && size <= 2))
2376 /* Put smaller objects in _static_data, where the section anchors system
2377 can get them.
2378 However, if they are zero-sized punt them to yet a different section
2379 (that is not allowed to participate in anchoring). */
2380 if (!size)
2382 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2383 in_section = darwin_sections[zobj_bss_section];
2384 size = 1;
2386 else
2388 fputs ("\t.static_data\n", fp);
2389 in_section = darwin_sections[static_data_section];
2392 if (l2align)
2393 fprintf (fp, "\t.align\t%u\n", l2align);
2395 assemble_name (fp, name);
2396 fprintf (fp, ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2398 else
2400 /* When we are on a non-section anchor target, we can get zero-sized
2401 items here. However, all we need to do is to bump them to one byte
2402 and the section alignment will take care of the rest. */
2403 char secnam[64];
2404 unsigned int flags ;
2405 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
2406 (unsigned) l2align);
2407 /* We can't anchor (yet, if ever) in zerofill sections, because we can't
2408 switch to them and emit a label. */
2409 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2410 in_section = get_section (secnam, flags, NULL);
2411 fprintf (fp, "\t.zerofill %s,", secnam);
2412 assemble_name (fp, name);
2413 if (!size)
2414 size = 1;
2416 if (l2align)
2417 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2418 size, (unsigned) l2align);
2419 else
2420 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2423 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2424 /* This is defined as a file-scope var, so we know to notify machopic. */
2425 machopic_define_symbol (DECL_RTL (decl));
2428 /* Emit a chunk of common. */
2429 static void
2430 darwin_emit_common (FILE *fp, const char *name,
2431 unsigned HOST_WIDE_INT size, unsigned int align)
2433 unsigned HOST_WIDE_INT rounded;
2434 unsigned int l2align;
2436 /* Earlier systems complain if the alignment exceeds the page size.
2437 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2438 if (!emit_aligned_common && (align > 32768UL))
2439 align = 4096UL; /* In units. */
2440 else
2441 align /= BITS_PER_UNIT;
2443 /* Make sure we have a meaningful align. */
2444 if (!align)
2445 align = 1;
2447 /* For earlier toolchains, we need to emit the var as a rounded size to
2448 tell ld the alignment. */
2449 if (size < align)
2450 rounded = align;
2451 else
2452 rounded = (size + (align-1)) & ~(align-1);
2454 l2align = floor_log2 (align);
2455 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2457 in_section = comm_section;
2458 /* We mustn't allow multiple public symbols to share an address when using
2459 the normal OSX toolchain. */
2460 if (!size)
2462 /* Put at least one byte. */
2463 size = 1;
2464 /* This section can no longer participate in section anchoring. */
2465 comm_section->common.flags |= SECTION_NO_ANCHOR;
2468 fputs ("\t.comm\t", fp);
2469 assemble_name (fp, name);
2470 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2471 emit_aligned_common?size:rounded);
2472 if (l2align && emit_aligned_common)
2473 fprintf (fp, ",%u", l2align);
2474 fputs ("\n", fp);
2477 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2478 or coalescable data sections (for weak or comdat) as appropriate. */
2480 void
2481 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2482 unsigned HOST_WIDE_INT size, unsigned int align)
2484 unsigned int l2align;
2485 bool one, pub, weak;
2486 tree meta;
2488 pub = TREE_PUBLIC (decl);
2489 one = DECL_ONE_ONLY (decl);
2490 weak = (DECL_P (decl)
2491 && DECL_WEAK (decl)
2492 && !lookup_attribute ("weak_import",
2493 DECL_ATTRIBUTES (decl)));
2495 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2496 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2497 " pub %d weak %d one %d init %lx\n",
2498 name, (long long)size, (int)align, TREE_READONLY (decl),
2499 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2500 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2501 #endif
2503 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2504 before the target has a chance to comment. */
2505 if ((meta = is_objc_metadata (decl)))
2507 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2508 return;
2511 /* Check that any initializer is valid. */
2512 gcc_assert ((DECL_INITIAL (decl) == NULL)
2513 || (DECL_INITIAL (decl) == error_mark_node)
2514 || initializer_zerop (DECL_INITIAL (decl)));
2516 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2517 gcc_assert (!DECL_COMMON (decl));
2519 /* Pick up the correct alignment. */
2520 if (!size || !align)
2521 align = DECL_ALIGN (decl);
2523 l2align = floor_log2 (align / BITS_PER_UNIT);
2524 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2526 last_assemble_variable_decl = decl;
2528 /* We would rather not have to check this here - but it seems that we might
2529 be passed a decl that should be in coalesced space. */
2530 if (one || weak)
2532 /* Weak or COMDAT objects are put in mergeable sections. */
2533 darwin_emit_weak_or_comdat (fp, decl, name, size,
2534 ld_uses_coal_sects, DECL_ALIGN (decl));
2535 return;
2538 /* If this is not public, then emit according to local rules. */
2539 if (!pub)
2541 darwin_emit_local_bss (fp, decl, name, size, l2align);
2542 return;
2545 /* So we have a public symbol (small item fudge for Java, see above). */
2546 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2547 || (size && size <= 2))
2549 /* Put smaller objects in data, where the section anchors system can get
2550 them. However, if they are zero-sized punt them to yet a different
2551 section (that is not allowed to participate in anchoring). */
2552 if (!size)
2554 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2555 in_section = darwin_sections[zobj_data_section];
2556 size = 1;
2558 else
2560 fputs ("\t.data\n", fp);
2561 in_section = data_section;
2564 if (l2align)
2565 fprintf (fp, "\t.align\t%u\n", l2align);
2567 assemble_name (fp, name);
2568 fprintf (fp, ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2570 else
2572 char secnam[64];
2573 unsigned int flags ;
2574 /* When we are on a non-section anchor target, we can get zero-sized
2575 items here. However, all we need to do is to bump them to one byte
2576 and the section alignment will take care of the rest. */
2577 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
2579 /* We can't anchor in zerofill sections, because we can't switch
2580 to them and emit a label. */
2581 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2582 in_section = get_section (secnam, flags, NULL);
2583 fprintf (fp, "\t.zerofill %s,", secnam);
2584 assemble_name (fp, name);
2585 if (!size)
2586 size = 1;
2588 if (l2align)
2589 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2590 else
2591 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2593 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2596 /* Output a chunk of common, with alignment specified (where the target
2597 supports this). */
2598 void
2599 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2600 unsigned HOST_WIDE_INT size,
2601 unsigned int align)
2603 unsigned int l2align;
2604 bool one, weak;
2605 tree meta;
2607 /* No corresponding var. */
2608 if (decl==NULL)
2610 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2611 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2612 #endif
2613 darwin_emit_common (fp, name, size, align);
2614 return;
2617 one = DECL_ONE_ONLY (decl);
2618 weak = (DECL_P (decl)
2619 && DECL_WEAK (decl)
2620 && !lookup_attribute ("weak_import",
2621 DECL_ATTRIBUTES (decl)));
2623 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2624 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2625 " weak %d one %d init %lx\n",
2626 name, (long long)size, (int)align, TREE_READONLY (decl),
2627 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2628 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2629 #endif
2631 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2632 before the target has a chance to comment. */
2633 if ((meta = is_objc_metadata (decl)))
2635 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2636 return;
2639 /* We shouldn't be messing with this if the decl has a section name. */
2640 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2642 /* We would rather not have to check this here - but it seems that we might
2643 be passed a decl that should be in coalesced space. */
2644 if (one || weak)
2646 /* Weak or COMDAT objects are put in mergable sections. */
2647 darwin_emit_weak_or_comdat (fp, decl, name, size,
2648 ld_uses_coal_sects, DECL_ALIGN (decl));
2649 return;
2652 /* We should only get here for DECL_COMMON, with a zero init (and, in
2653 principle, only for public symbols too - although we deal with local
2654 ones below). */
2656 /* Check the initializer is OK. */
2657 gcc_assert (DECL_COMMON (decl)
2658 && ((DECL_INITIAL (decl) == NULL)
2659 || (DECL_INITIAL (decl) == error_mark_node)
2660 || initializer_zerop (DECL_INITIAL (decl))));
2662 last_assemble_variable_decl = decl;
2664 if (!size || !align)
2665 align = DECL_ALIGN (decl);
2667 l2align = floor_log2 (align / BITS_PER_UNIT);
2668 /* Check we aren't asking for more aligment than the platform allows. */
2669 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2671 if (TREE_PUBLIC (decl) != 0)
2672 darwin_emit_common (fp, name, size, align);
2673 else
2674 darwin_emit_local_bss (fp, decl, name, size, l2align);
2677 /* Output a chunk of BSS with alignment specfied. */
2678 void
2679 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2680 unsigned HOST_WIDE_INT size,
2681 unsigned int align)
2683 unsigned long l2align;
2684 bool one, weak;
2685 tree meta;
2687 one = DECL_ONE_ONLY (decl);
2688 weak = (DECL_P (decl)
2689 && DECL_WEAK (decl)
2690 && !lookup_attribute ("weak_import",
2691 DECL_ATTRIBUTES (decl)));
2693 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2694 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2695 " weak %d init %lx\n",
2696 name, (long long)size, (int)align, TREE_READONLY (decl),
2697 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2698 weak , (unsigned long)DECL_INITIAL (decl));
2699 #endif
2701 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2702 before the target has a chance to comment. */
2703 if ((meta = is_objc_metadata (decl)))
2705 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2706 return;
2709 /* We shouldn't be messing with this if the decl has a section name. */
2710 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2712 /* We would rather not have to check this here - but it seems that we might
2713 be passed a decl that should be in coalesced space. */
2714 if (one || weak)
2716 /* Weak or COMDAT objects are put in mergable sections. */
2717 darwin_emit_weak_or_comdat (fp, decl, name, size,
2718 ld_uses_coal_sects, DECL_ALIGN (decl));
2719 return;
2722 /* .. and it should be suitable for placement in local mem. */
2723 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2724 /* .. and any initializer must be all-zero. */
2725 gcc_assert ((DECL_INITIAL (decl) == NULL)
2726 || (DECL_INITIAL (decl) == error_mark_node)
2727 || initializer_zerop (DECL_INITIAL (decl)));
2729 last_assemble_variable_decl = decl;
2731 if (!size || !align)
2732 align = DECL_ALIGN (decl);
2734 l2align = floor_log2 (align / BITS_PER_UNIT);
2735 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2737 darwin_emit_local_bss (fp, decl, name, size, l2align);
2740 /* Emit an assembler directive to set visibility for a symbol. The
2741 only supported visibilities are VISIBILITY_DEFAULT and
2742 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2743 extern". There is no MACH-O equivalent of ELF's
2744 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2746 void
2747 darwin_assemble_visibility (tree decl, int vis)
2749 if (vis == VISIBILITY_DEFAULT)
2751 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2753 fputs ("\t.private_extern ", asm_out_file);
2754 assemble_name (asm_out_file,
2755 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2756 fputs ("\n", asm_out_file);
2758 else
2759 warning (OPT_Wattributes, "protected visibility attribute "
2760 "not supported in this configuration; ignored");
2763 /* vec used by darwin_asm_dwarf_section.
2764 Maybe a hash tab would be better here - but the intention is that this is
2765 a very short list (fewer than 16 items) and each entry should (ideally,
2766 eventually) only be presented once.
2768 A structure to hold a dwarf debug section used entry. */
2770 typedef struct GTY(()) dwarf_sect_used_entry {
2771 const char *name;
2772 unsigned count;
2774 dwarf_sect_used_entry;
2777 /* A list of used __DWARF sections. */
2778 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2780 /* This is called when we are asked to assemble a named section and the
2781 name begins with __DWARF,. We keep a list of the section names (without
2782 the __DWARF, prefix) and use this to emit our required start label on the
2783 first switch to each section. */
2785 static void
2786 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2787 tree ARG_UNUSED (decl))
2789 unsigned i;
2790 int namelen;
2791 const char * sname;
2792 dwarf_sect_used_entry *ref;
2793 bool found = false;
2794 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2795 == (SECTION_DEBUG | SECTION_NAMED));
2796 /* We know that the name starts with __DWARF, */
2797 sname = name + 8;
2798 namelen = strchr (sname, ',') - sname;
2799 gcc_assert (namelen);
2800 if (dwarf_sect_names_table == NULL)
2801 vec_alloc (dwarf_sect_names_table, 16);
2802 else
2803 for (i = 0;
2804 dwarf_sect_names_table->iterate (i, &ref);
2805 i++)
2807 if (!ref)
2808 break;
2809 if (!strcmp (ref->name, sname))
2811 found = true;
2812 ref->count++;
2813 break;
2817 fprintf (asm_out_file, "\t.section %s\n", name);
2818 if (!found)
2820 dwarf_sect_used_entry e;
2821 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2822 e.count = 1;
2823 e.name = xstrdup (sname);
2824 vec_safe_push (dwarf_sect_names_table, e);
2828 /* Output a difference of two labels that will be an assembly time
2829 constant if the two labels are local. (.long lab1-lab2 will be
2830 very different if lab1 is at the boundary between two sections; it
2831 will be relocated according to the second section, not the first,
2832 so one ends up with a difference between labels in different
2833 sections, which is bad in the dwarf2 eh context for instance.) */
2835 static int darwin_dwarf_label_counter;
2837 void
2838 darwin_asm_output_dwarf_delta (FILE *file, int size,
2839 const char *lab1, const char *lab2,
2840 HOST_WIDE_INT offset)
2842 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2843 && lab2[0] == '*' && lab2[1] == 'L');
2844 const char *directive = (size == 8 ? ".quad" : ".long");
2846 if (islocaldiff)
2847 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2848 else
2849 fprintf (file, "\t%s\t", directive);
2851 assemble_name_raw (file, lab1);
2852 fprintf (file, "-");
2853 assemble_name_raw (file, lab2);
2854 if (offset != 0)
2855 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
2856 if (islocaldiff)
2857 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2860 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2861 offsets are not represented using relocs in .o files; either the
2862 section never leaves the .o file, or the linker or other tool is
2863 responsible for parsing the DWARF and updating the offsets. */
2865 void
2866 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2867 HOST_WIDE_INT offset, section *base)
2869 char sname[64];
2870 int namelen;
2872 gcc_assert (base->common.flags & SECTION_NAMED);
2873 gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
2874 gcc_assert (strchr (base->named.name + 8, ','));
2876 namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
2877 sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
2878 darwin_asm_output_dwarf_delta (file, size, lab, sname, offset);
2881 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2883 void
2884 darwin_file_start (void)
2886 /* Nothing to do. */
2889 /* Called for the TARGET_ASM_FILE_END hook.
2890 Emit the mach-o pic indirection data, the lto data and, finally a flag
2891 to tell the linker that it can break the file object into sections and
2892 move those around for efficiency. */
2894 void
2895 darwin_file_end (void)
2897 if (!vec_safe_is_empty (ctors))
2898 finalize_ctors ();
2899 if (!vec_safe_is_empty (dtors))
2900 finalize_dtors ();
2902 /* If we are expecting to output NeXT ObjC meta-data, (and we actually see
2903 some) then we output the fix-and-continue marker (Image Info).
2904 This applies to Objective C, Objective C++ and LTO with either language
2905 as part of the input. */
2906 if (flag_next_runtime && objc_metadata_seen)
2908 unsigned int flags = 0;
2909 if (flag_objc_abi >= 2)
2911 flags = 16;
2912 output_section_asm_op
2913 (darwin_sections[objc2_image_info_section]->unnamed.data);
2915 else
2916 output_section_asm_op
2917 (darwin_sections[objc_image_info_section]->unnamed.data);
2919 ASM_OUTPUT_ALIGN (asm_out_file, 2);
2920 fputs ("L_OBJC_ImageInfo:\n", asm_out_file);
2922 flags |= (flag_replace_objc_classes && classes_seen) ? 1 : 0;
2923 flags |= flag_objc_gc ? 2 : 0;
2925 fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags);
2928 machopic_finish (asm_out_file);
2929 if (lang_GNU_CXX ())
2931 switch_to_section (darwin_sections[constructor_section]);
2932 switch_to_section (darwin_sections[destructor_section]);
2933 ASM_OUTPUT_ALIGN (asm_out_file, 1);
2936 /* If there was LTO assembler output, append it to asm_out_file. */
2937 if (lto_asm_out_name)
2939 int n;
2940 char *buf, *lto_asm_txt;
2942 /* Shouldn't be here if we failed to switch back. */
2943 gcc_assert (! saved_asm_out_file);
2945 lto_asm_out_file = fopen (lto_asm_out_name, "r");
2946 if (lto_asm_out_file == NULL)
2947 fatal_error (input_location,
2948 "failed to open temporary file %s with LTO output",
2949 lto_asm_out_name);
2950 fseek (lto_asm_out_file, 0, SEEK_END);
2951 n = ftell (lto_asm_out_file);
2952 if (n > 0)
2954 fseek (lto_asm_out_file, 0, SEEK_SET);
2955 lto_asm_txt = buf = (char *) xmalloc (n + 1);
2956 while (fgets (lto_asm_txt, n, lto_asm_out_file))
2957 fputs (lto_asm_txt, asm_out_file);
2958 /* Put a termination label. */
2959 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2960 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2961 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2962 lto_section_num);
2963 /* Make sure our termination label stays in this section. */
2964 fputs ("\t.space\t1\n", asm_out_file);
2967 /* Remove the temporary file. */
2968 fclose (lto_asm_out_file);
2969 unlink_if_ordinary (lto_asm_out_name);
2970 free (lto_asm_out_name);
2973 /* Output the names and indices. */
2974 if (lto_section_names && lto_section_names->length ())
2976 int count;
2977 darwin_lto_section_e *ref;
2978 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
2979 the latter up ourselves. */
2980 const char *op = integer_asm_op (4,0);
2982 /* Emit the names. */
2983 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2984 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2985 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2987 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
2988 /* We have to jump through hoops to get the values of the intra-section
2989 offsets... */
2990 fprintf (asm_out_file,
2991 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
2992 count, count);
2993 fprintf (asm_out_file,
2994 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
2995 count, count+1, count);
2996 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
2998 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
2999 /* make sure our termination label stays in this section. */
3000 fputs ("\t.space\t1\n", asm_out_file);
3002 /* Emit the Index. */
3003 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
3004 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
3005 fputs ("\t.align\t2\n", asm_out_file);
3006 fputs ("# Section offset, Section length, Name offset, Name length\n",
3007 asm_out_file);
3008 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
3010 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
3011 op, count, ref->sectname);
3012 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
3013 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
3014 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
3018 /* If we have section anchors, then we must prevent the linker from
3019 re-arranging data. */
3020 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
3021 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
3024 /* TODO: Add a language hook for identifying if a decl is a vtable. */
3025 #define DARWIN_VTABLE_P(DECL) 0
3027 /* Cross-module name binding. Darwin does not support overriding
3028 functions at dynamic-link time, except for vtables in kexts. */
3030 bool
3031 darwin_binds_local_p (const_tree decl)
3033 return default_binds_local_p_1 (decl,
3034 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
3037 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
3038 anchor relative to ".", the current section position. We cannot use
3039 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
3040 void
3041 darwin_asm_output_anchor (rtx symbol)
3043 fprintf (asm_out_file, "\t.set\t");
3044 assemble_name (asm_out_file, XSTR (symbol, 0));
3045 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
3046 SYMBOL_REF_BLOCK_OFFSET (symbol));
3049 /* Disable section anchoring on any section containing a zero-sized
3050 object. */
3051 bool
3052 darwin_use_anchors_for_symbol_p (const_rtx symbol)
3054 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
3056 section *sect;
3057 /* If the section contains a zero-sized object it's ineligible. */
3058 sect = SYMBOL_REF_BLOCK (symbol)->sect;
3059 /* This should have the effect of disabling anchors for vars that follow
3060 any zero-sized one, in a given section. */
3061 if (sect->common.flags & SECTION_NO_ANCHOR)
3062 return false;
3064 /* Also check the normal reasons for suppressing. */
3065 return default_use_anchors_for_symbol_p (symbol);
3067 else
3068 return false;
3071 /* Set the darwin specific attributes on TYPE. */
3072 void
3073 darwin_set_default_type_attributes (tree type)
3075 if (darwin_ms_struct
3076 && TREE_CODE (type) == RECORD_TYPE)
3077 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
3078 NULL_TREE,
3079 TYPE_ATTRIBUTES (type));
3082 /* True, iff we're generating code for loadable kernel extensions. */
3084 bool
3085 darwin_kextabi_p (void) {
3086 return flag_apple_kext;
3089 void
3090 darwin_override_options (void)
3092 /* Keep track of which (major) version we're generating code for. */
3093 if (darwin_macosx_version_min)
3095 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
3096 generating_for_darwin_version = 10;
3097 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
3098 generating_for_darwin_version = 9;
3100 /* Earlier versions are not specifically accounted, until required. */
3103 /* Older Darwin ld could not coalesce weak entities without them being
3104 placed in special sections. */
3105 if (darwin_target_linker
3106 && (strverscmp (darwin_target_linker, MIN_LD64_NO_COAL_SECTS) < 0))
3107 ld_uses_coal_sects = true;
3109 /* In principle, this should be c-family only. However, we really need to
3110 set sensible defaults for LTO as well, since the section selection stuff
3111 should check for correctness re. the ABI. TODO: check and provide the
3112 flags (runtime & ABI) from the lto wrapper). */
3114 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
3115 if (!global_options_set.x_flag_objc_abi)
3116 global_options.x_flag_objc_abi
3117 = (!flag_next_runtime)
3119 : (TARGET_64BIT ? 2
3120 : (generating_for_darwin_version >= 9) ? 1
3121 : 0);
3123 /* Objective-C family ABI 2 is only valid for next/m64 at present. */
3124 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
3126 if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
3127 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
3128 " used for %<-m64%> targets with"
3129 " %<-fnext-runtime%>");
3130 if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
3131 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
3132 " supported on %<-m32%> targets with"
3133 " %<-fnext-runtime%>");
3136 /* Don't emit DWARF3/4 unless specifically selected. This is a
3137 workaround for tool bugs. */
3138 if (!global_options_set.x_dwarf_strict)
3139 dwarf_strict = 1;
3140 if (!global_options_set.x_dwarf_version)
3141 dwarf_version = 2;
3143 /* Do not allow unwind tables to be generated by default for m32.
3144 fnon-call-exceptions will override this, regardless of what we do. */
3145 if (generating_for_darwin_version < 10
3146 && !global_options_set.x_flag_asynchronous_unwind_tables
3147 && !TARGET_64BIT)
3148 global_options.x_flag_asynchronous_unwind_tables = 0;
3150 /* Disable -freorder-blocks-and-partition when unwind tables are being
3151 emitted for Darwin < 9 (OSX 10.5).
3152 The strategy is, "Unless the User has specifically set/unset an unwind
3153 flag we will switch off -freorder-blocks-and-partition when unwind tables
3154 will be generated". If the User specifically sets flags... we assume
3155 (s)he knows why... */
3156 if (generating_for_darwin_version < 9
3157 && global_options_set.x_flag_reorder_blocks_and_partition
3158 && ((global_options.x_flag_exceptions /* User, c++, java */
3159 && !global_options_set.x_flag_exceptions) /* User specified... */
3160 || (global_options.x_flag_unwind_tables
3161 && !global_options_set.x_flag_unwind_tables)
3162 || (global_options.x_flag_non_call_exceptions
3163 && !global_options_set.x_flag_non_call_exceptions)
3164 || (global_options.x_flag_asynchronous_unwind_tables
3165 && !global_options_set.x_flag_asynchronous_unwind_tables)))
3167 inform (input_location,
3168 "-freorder-blocks-and-partition does not work with exceptions "
3169 "on this architecture");
3170 flag_reorder_blocks_and_partition = 0;
3171 flag_reorder_blocks = 1;
3174 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3175 one valid choice of exception scheme for each runtime. */
3176 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3177 global_options.x_flag_objc_sjlj_exceptions =
3178 flag_next_runtime && !TARGET_64BIT;
3180 /* FIXME: and this could be eliminated then too. */
3181 if (!global_options_set.x_flag_exceptions
3182 && flag_objc_exceptions
3183 && TARGET_64BIT)
3184 flag_exceptions = 1;
3186 if (flag_mkernel || flag_apple_kext)
3188 /* -mkernel implies -fapple-kext for C++ */
3189 if (lang_GNU_CXX ())
3190 flag_apple_kext = 1;
3192 flag_no_common = 1;
3194 /* No EH in kexts. */
3195 flag_exceptions = 0;
3196 /* No -fnon-call-exceptions data in kexts. */
3197 flag_non_call_exceptions = 0;
3198 /* so no tables either.. */
3199 flag_unwind_tables = 0;
3200 flag_asynchronous_unwind_tables = 0;
3201 /* We still need to emit branch islands for kernel context. */
3202 darwin_emit_branch_islands = true;
3205 if (flag_var_tracking_uninit == 0
3206 && generating_for_darwin_version >= 9
3207 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3208 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3209 && write_symbols == DWARF2_DEBUG)
3210 flag_var_tracking_uninit = flag_var_tracking;
3212 if (MACHO_DYNAMIC_NO_PIC_P)
3214 if (flag_pic)
3215 warning_at (UNKNOWN_LOCATION, 0,
3216 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3217 " %<-fpie%> or %<-fPIE%>");
3218 flag_pic = 0;
3220 else if (flag_pic == 1)
3222 /* Darwin's -fpic is -fPIC. */
3223 flag_pic = 2;
3226 /* It is assumed that branch island stubs are needed for earlier systems. */
3227 if (generating_for_darwin_version < 9)
3228 darwin_emit_branch_islands = true;
3229 else
3230 emit_aligned_common = true; /* Later systems can support aligned common. */
3232 /* The c_dialect...() macros are not available to us here. */
3233 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3236 #if DARWIN_PPC
3237 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3239 static void
3240 darwin_patch_builtin (enum built_in_function fncode)
3242 tree fn = builtin_decl_explicit (fncode);
3243 tree sym;
3244 char *newname;
3246 if (!fn)
3247 return;
3249 sym = DECL_ASSEMBLER_NAME (fn);
3250 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3252 set_user_assembler_name (fn, newname);
3254 fn = builtin_decl_implicit (fncode);
3255 if (fn)
3256 set_user_assembler_name (fn, newname);
3259 void
3260 darwin_patch_builtins (void)
3262 if (LONG_DOUBLE_TYPE_SIZE != 128)
3263 return;
3265 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3266 #define PATCH_BUILTIN_NO64(fncode) \
3267 if (!TARGET_64BIT) \
3268 darwin_patch_builtin (fncode);
3269 #define PATCH_BUILTIN_VARIADIC(fncode) \
3270 if (!TARGET_64BIT \
3271 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3272 darwin_patch_builtin (fncode);
3273 #include "darwin-ppc-ldouble-patch.def"
3274 #undef PATCH_BUILTIN
3275 #undef PATCH_BUILTIN_NO64
3276 #undef PATCH_BUILTIN_VARIADIC
3278 #endif
3280 /* CFStrings implementation. */
3281 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3282 static GTY(()) tree cfstring_type_node = NULL_TREE;
3283 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3284 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3285 static GTY(()) tree pcint_type_node = NULL_TREE;
3286 static GTY(()) tree pcchar_type_node = NULL_TREE;
3288 static enum built_in_function darwin_builtin_cfstring;
3290 /* Store all constructed constant CFStrings in a hash table so that
3291 they get uniqued properly. */
3293 typedef struct GTY ((for_user)) cfstring_descriptor {
3294 /* The string literal. */
3295 tree literal;
3296 /* The resulting constant CFString. */
3297 tree constructor;
3298 } cfstring_descriptor;
3300 struct cfstring_hasher : ggc_ptr_hash<cfstring_descriptor>
3302 static hashval_t hash (cfstring_descriptor *);
3303 static bool equal (cfstring_descriptor *, cfstring_descriptor *);
3306 static GTY (()) hash_table<cfstring_hasher> *cfstring_htab;
3308 static tree
3309 add_builtin_field_decl (tree type, const char *name, tree **chain)
3311 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3312 get_identifier (name), type);
3314 if (*chain != NULL)
3315 **chain = field;
3316 *chain = &DECL_CHAIN (field);
3318 return field;
3321 tree
3322 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3324 tree cfsfun, fields, pccfstring_ftype_pcchar;
3325 tree *chain = NULL;
3327 darwin_builtin_cfstring =
3328 (enum built_in_function) builtin_cfstring;
3330 /* struct __builtin_CFString {
3331 const int *isa; (will point at
3332 int flags; __CFConstantStringClassReference)
3333 const char *str;
3334 long length;
3335 }; */
3337 pcint_type_node = build_pointer_type
3338 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3340 pcchar_type_node = build_pointer_type
3341 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3343 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3345 /* Have to build backwards for finish struct. */
3346 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3347 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3348 add_builtin_field_decl (integer_type_node, "flags", &chain);
3349 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3350 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3351 fields, NULL_TREE);
3353 /* const struct __builtin_CFstring *
3354 __builtin___CFStringMakeConstantString (const char *); */
3356 ccfstring_type_node = build_qualified_type
3357 (cfstring_type_node, TYPE_QUAL_CONST);
3358 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3359 pccfstring_ftype_pcchar = build_function_type_list
3360 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3362 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3363 get_identifier ("__builtin___CFStringMakeConstantString"),
3364 pccfstring_ftype_pcchar);
3366 TREE_PUBLIC (cfsfun) = 1;
3367 DECL_EXTERNAL (cfsfun) = 1;
3368 DECL_ARTIFICIAL (cfsfun) = 1;
3369 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3370 in place of the existing, which may be NULL. */
3371 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3372 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3373 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
3374 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
3375 lang_hooks.builtin_function (cfsfun);
3377 /* extern int __CFConstantStringClassReference[]; */
3378 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3379 get_identifier ("__CFConstantStringClassReference"),
3380 build_array_type (integer_type_node, NULL_TREE));
3382 TREE_PUBLIC (cfstring_class_reference) = 1;
3383 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3384 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3385 DECL_EXTERNAL (cfstring_class_reference) = 1;
3386 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3388 /* Initialize the hash table used to hold the constant CFString objects. */
3389 cfstring_htab = hash_table<cfstring_hasher>::create_ggc (31);
3391 return cfstring_type_node;
3394 tree
3395 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3396 bool ARG_UNUSED (ignore))
3398 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3400 if (fcode == darwin_builtin_cfstring)
3402 if (!darwin_constant_cfstrings)
3404 error ("built-in function %qD requires the"
3405 " %<-mconstant-cfstrings%> flag", fndecl);
3406 return error_mark_node;
3409 if (n_args != 1)
3411 error ("built-in function %qD takes one argument only", fndecl);
3412 return error_mark_node;
3415 return darwin_build_constant_cfstring (*argp);
3418 return NULL_TREE;
3421 void
3422 darwin_rename_builtins (void)
3424 /* The system ___divdc3 routine in libSystem on darwin10 is not
3425 accurate to 1ulp, ours is, so we avoid ever using the system name
3426 for this routine and instead install a non-conflicting name that
3427 is accurate.
3429 When -ffast-math or -funsafe-math-optimizations is given, we can
3430 use the faster version. */
3431 if (!flag_unsafe_math_optimizations)
3433 enum built_in_function dcode
3434 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3435 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3436 tree fn = builtin_decl_explicit (dcode);
3437 /* Fortran and c call TARGET_INIT_BUILTINS and
3438 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3439 call into each to ensure that at least one of them is called
3440 after build_common_builtin_nodes. A better fix is to add a
3441 new hook to run after build_common_builtin_nodes runs. */
3442 if (fn)
3443 set_user_assembler_name (fn, "___ieee_divdc3");
3444 fn = builtin_decl_implicit (dcode);
3445 if (fn)
3446 set_user_assembler_name (fn, "___ieee_divdc3");
3450 bool
3451 darwin_libc_has_function (enum function_class fn_class)
3453 if (fn_class == function_sincos)
3454 return false;
3455 if (fn_class == function_c99_math_complex
3456 || fn_class == function_c99_misc)
3457 return (TARGET_64BIT
3458 || strverscmp (darwin_macosx_version_min, "10.3") >= 0);
3460 return true;
3463 hashval_t
3464 cfstring_hasher::hash (cfstring_descriptor *ptr)
3466 tree str = ptr->literal;
3467 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3468 int i, len = TREE_STRING_LENGTH (str);
3469 hashval_t h = len;
3471 for (i = 0; i < len; i++)
3472 h = ((h * 613) + p[i]);
3474 return h;
3477 bool
3478 cfstring_hasher::equal (cfstring_descriptor *ptr1, cfstring_descriptor *ptr2)
3480 tree str1 = ptr1->literal;
3481 tree str2 = ptr2->literal;
3482 int len1 = TREE_STRING_LENGTH (str1);
3484 return (len1 == TREE_STRING_LENGTH (str2)
3485 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3486 len1));
3489 tree
3490 darwin_build_constant_cfstring (tree str)
3492 struct cfstring_descriptor *desc, key;
3493 tree addr;
3495 if (!str)
3497 error ("CFString literal is missing");
3498 return error_mark_node;
3501 STRIP_NOPS (str);
3503 if (TREE_CODE (str) == ADDR_EXPR)
3504 str = TREE_OPERAND (str, 0);
3506 if (TREE_CODE (str) != STRING_CST)
3508 error ("CFString literal expression is not a string constant");
3509 return error_mark_node;
3512 /* Perhaps we already constructed a constant CFString just like this one? */
3513 key.literal = str;
3514 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
3515 desc = *loc;
3517 if (!desc)
3519 tree var, constructor, field;
3520 vec<constructor_elt, va_gc> *v = NULL;
3521 int length = TREE_STRING_LENGTH (str) - 1;
3523 if (darwin_warn_nonportable_cfstrings)
3525 const char *s = TREE_STRING_POINTER (str);
3526 int l = 0;
3528 for (l = 0; l < length; l++)
3529 if (!s[l] || !isascii (s[l]))
3531 warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
3532 s[l] ? "non-ASCII character" : "embedded NUL");
3533 break;
3537 *loc = desc = ggc_cleared_alloc<cfstring_descriptor> ();
3538 desc->literal = str;
3540 /* isa *. */
3541 field = TYPE_FIELDS (ccfstring_type_node);
3542 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3543 build1 (ADDR_EXPR, TREE_TYPE (field),
3544 cfstring_class_reference));
3545 /* flags */
3546 field = DECL_CHAIN (field);
3547 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3548 build_int_cst (TREE_TYPE (field), 0x000007c8));
3549 /* string *. */
3550 field = DECL_CHAIN (field);
3551 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3552 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3553 /* length */
3554 field = DECL_CHAIN (field);
3555 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3556 build_int_cst (TREE_TYPE (field), length));
3558 constructor = build_constructor (ccfstring_type_node, v);
3559 TREE_READONLY (constructor) = 1;
3560 TREE_CONSTANT (constructor) = 1;
3561 TREE_STATIC (constructor) = 1;
3563 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3564 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3565 being built without any knowledge of C++ tree accessors; hence, we shall
3566 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3567 if (darwin_running_cxx)
3568 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3570 /* Create an anonymous global variable for this CFString. */
3571 var = build_decl (input_location, CONST_DECL,
3572 NULL, TREE_TYPE (constructor));
3573 DECL_ARTIFICIAL (var) = 1;
3574 TREE_STATIC (var) = 1;
3575 DECL_INITIAL (var) = constructor;
3576 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3577 DECL_CONTEXT (var) = NULL_TREE;
3578 desc->constructor = var;
3581 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3582 TREE_CONSTANT (addr) = 1;
3584 return addr;
3587 bool
3588 darwin_cfstring_p (tree str)
3590 struct cfstring_descriptor key;
3592 if (!str)
3593 return false;
3595 STRIP_NOPS (str);
3597 if (TREE_CODE (str) == ADDR_EXPR)
3598 str = TREE_OPERAND (str, 0);
3600 if (TREE_CODE (str) != STRING_CST)
3601 return false;
3603 key.literal = str;
3604 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, NO_INSERT);
3606 if (loc)
3607 return true;
3609 return false;
3612 void
3613 darwin_enter_string_into_cfstring_table (tree str)
3615 struct cfstring_descriptor key;
3617 key.literal = str;
3618 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
3620 if (!*loc)
3622 *loc = ggc_cleared_alloc<cfstring_descriptor> ();
3623 ((struct cfstring_descriptor *)*loc)->literal = str;
3627 /* Choose named function section based on its frequency. */
3629 section *
3630 darwin_function_section (tree decl, enum node_frequency freq,
3631 bool startup, bool exit)
3633 /* Decide if we need to put this in a coalescable section. */
3634 bool weak = (decl
3635 && DECL_WEAK (decl)
3636 && (!DECL_ATTRIBUTES (decl)
3637 || !lookup_attribute ("weak_import",
3638 DECL_ATTRIBUTES (decl))));
3640 bool use_coal = weak && ld_uses_coal_sects;
3641 /* If there is a specified section name, we should not be trying to
3642 override. */
3643 if (decl && DECL_SECTION_NAME (decl) != NULL)
3644 return get_named_section (decl, NULL, 0);
3646 /* We always put unlikely executed stuff in the cold section. */
3647 if (freq == NODE_FREQUENCY_UNLIKELY_EXECUTED)
3648 return (use_coal) ? darwin_sections[text_cold_coal_section]
3649 : darwin_sections[text_cold_section];
3651 /* If we have LTO *and* feedback information, then let LTO handle
3652 the function ordering, it makes a better job (for normal, hot,
3653 startup and exit - hence the bailout for cold above). */
3654 if (in_lto_p && flag_profile_values)
3655 goto default_function_sections;
3657 /* Non-cold startup code should go to startup subsection. */
3658 if (startup)
3659 return (use_coal) ? darwin_sections[text_startup_coal_section]
3660 : darwin_sections[text_startup_section];
3662 /* Similarly for exit. */
3663 if (exit)
3664 return (use_coal) ? darwin_sections[text_exit_coal_section]
3665 : darwin_sections[text_exit_section];
3667 /* Place hot code. */
3668 if (freq == NODE_FREQUENCY_HOT)
3669 return (use_coal) ? darwin_sections[text_hot_coal_section]
3670 : darwin_sections[text_hot_section];
3672 /* Otherwise, default to the 'normal' non-reordered sections. */
3673 default_function_sections:
3674 return (use_coal) ? darwin_sections[text_coal_section]
3675 : text_section;
3678 /* When a function is partitioned between sections, we need to insert a label
3679 at the start of each new chunk - so that it may become a valid 'atom' for
3680 eh and debug purposes. Without this the linker will emit warnings if one
3681 tries to add line location information (since the switched fragment will
3682 be anonymous). */
3684 void
3685 darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
3687 /* Make sure we pick up all the relevant quotes etc. */
3688 assemble_name_raw (fp, new_is_cold ? "__cold_sect_of_" : "__hot_sect_of_");
3689 assemble_name_raw (fp, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
3690 fputs (":\n", fp);
3693 #include "gt-darwin.h"