gcc/
[official-gcc.git] / gcc / config / darwin.c
blobef4d163269b5764be5727377cebdecfbad32a7ba
1 /* Functions for generic Darwin as target machine for GNU C compiler.
2 Copyright (C) 1989-2015 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 "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-flags.h"
31 #include "output.h"
32 #include "insn-attr.h"
33 #include "flags.h"
34 #include "hash-set.h"
35 #include "machmode.h"
36 #include "vec.h"
37 #include "double-int.h"
38 #include "input.h"
39 #include "alias.h"
40 #include "symtab.h"
41 #include "wide-int.h"
42 #include "inchash.h"
43 #include "tree.h"
44 #include "fold-const.h"
45 #include "stringpool.h"
46 #include "varasm.h"
47 #include "stor-layout.h"
48 #include "hashtab.h"
49 #include "function.h"
50 #include "statistics.h"
51 #include "real.h"
52 #include "fixed-value.h"
53 #include "expmed.h"
54 #include "dojump.h"
55 #include "explow.h"
56 #include "calls.h"
57 #include "emit-rtl.h"
58 #include "stmt.h"
59 #include "expr.h"
60 #include "reload.h"
61 #include "ggc.h"
62 #include "langhooks.h"
63 #include "target.h"
64 #include "tm_p.h"
65 #include "diagnostic-core.h"
66 #include "toplev.h"
67 #include "dominance.h"
68 #include "cfg.h"
69 #include "cfgrtl.h"
70 #include "cfganal.h"
71 #include "lcm.h"
72 #include "cfgbuild.h"
73 #include "cfgcleanup.h"
74 #include "predict.h"
75 #include "basic-block.h"
76 #include "df.h"
77 #include "debug.h"
78 #include "obstack.h"
79 #include "hash-table.h"
80 #include "tree-ssa-alias.h"
81 #include "internal-fn.h"
82 #include "gimple-fold.h"
83 #include "tree-eh.h"
84 #include "gimple-expr.h"
85 #include "is-a.h"
86 #include "gimple.h"
87 #include "gimplify.h"
88 #include "hash-map.h"
89 #include "plugin-api.h"
90 #include "ipa-ref.h"
91 #include "cgraph.h"
92 #include "lto-streamer.h"
93 #include "lto-section-names.h"
95 /* Darwin supports a feature called fix-and-continue, which is used
96 for rapid turn around debugging. When code is compiled with the
97 -mfix-and-continue flag, two changes are made to the generated code
98 that allow the system to do things that it would normally not be
99 able to do easily. These changes allow gdb to load in
100 recompilation of a translation unit that has been changed into a
101 running program and replace existing functions and methods of that
102 translation unit with versions of those functions and methods
103 from the newly compiled translation unit. The new functions access
104 the existing static symbols from the old translation unit, if the
105 symbol existed in the unit to be replaced, and from the new
106 translation unit, otherwise.
108 The changes are to insert 5 nops at the beginning of all functions
109 and to use indirection to get at static symbols. The 5 nops
110 are required by consumers of the generated code. Currently, gdb
111 uses this to patch in a jump to the overriding function, this
112 allows all uses of the old name to forward to the replacement,
113 including existing function pointers and virtual methods. See
114 rs6000_emit_prologue for the code that handles the nop insertions.
116 The added indirection allows gdb to redirect accesses to static
117 symbols from the newly loaded translation unit to the existing
118 symbol, if any. @code{static} symbols are special and are handled by
119 setting the second word in the .non_lazy_symbol_pointer data
120 structure to symbol. See indirect_data for the code that handles
121 the extra indirection, and machopic_output_indirection and its use
122 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
123 symbol indirection. */
125 /* For darwin >= 9 (OSX 10.5) the linker is capable of making the necessary
126 branch islands and we no longer need to emit darwin stubs.
127 However, if we are generating code for earlier systems (or for use in the
128 kernel) the stubs might still be required, and this will be set true. */
129 int darwin_emit_branch_islands = false;
131 typedef struct GTY(()) cdtor_record {
132 rtx symbol;
133 int priority; /* [con/de]structor priority */
134 int position; /* original position */
135 } cdtor_record;
137 static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL;
138 static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL;
140 /* A flag to determine whether we are running c++ or obj-c++. This has to be
141 settable from non-c-family contexts too (i.e. we can't use the c_dialect_
142 functions). */
143 int darwin_running_cxx;
145 /* Some code-gen now depends on OS major version numbers (at least). */
146 int generating_for_darwin_version ;
148 /* Section names. */
149 section * darwin_sections[NUM_DARWIN_SECTIONS];
151 /* While we transition to using in-tests instead of ifdef'd code. */
152 #ifndef HAVE_lo_sum
153 #define HAVE_lo_sum 0
154 #define gen_macho_high(a,b) (a)
155 #define gen_macho_low(a,b,c) (a)
156 #endif
158 /* True if we're setting __attribute__ ((ms_struct)). */
159 int darwin_ms_struct = false;
161 /* Earlier versions of Darwin as do not recognize an alignment field in
162 .comm directives, this should be set for versions that allow it. */
163 int emit_aligned_common = false;
165 /* A get_unnamed_section callback used to switch to an ObjC section.
166 DIRECTIVE is as for output_section_asm_op. */
168 static void
169 output_objc_section_asm_op (const void *directive)
171 static bool been_here = false;
173 /* The NeXT ObjC Runtime requires these sections to be present and in
174 order in the object. The code below implements this by emitting
175 a section header for each ObjC section the first time that an ObjC
176 section is requested. */
177 if (! been_here)
179 section *saved_in_section = in_section;
180 static const enum darwin_section_enum tomark[] =
182 /* written, cold -> hot */
183 objc_cat_cls_meth_section,
184 objc_cat_inst_meth_section,
185 objc_string_object_section,
186 objc_constant_string_object_section,
187 objc_selector_refs_section,
188 objc_selector_fixup_section,
189 objc_cls_refs_section,
190 objc_class_section,
191 objc_meta_class_section,
192 /* shared, hot -> cold */
193 objc_cls_meth_section,
194 objc_inst_meth_section,
195 objc_protocol_section,
196 objc_class_names_section,
197 objc_meth_var_types_section,
198 objc_meth_var_names_section,
199 objc_category_section,
200 objc_class_vars_section,
201 objc_instance_vars_section,
202 objc_module_info_section,
203 objc_symbols_section,
205 /* ABI=1 */
206 static const enum darwin_section_enum tomarkv1[] =
208 objc1_protocol_ext_section,
209 objc1_class_ext_section,
210 objc1_prop_list_section
212 /* ABI=2 */
213 static const enum darwin_section_enum tomarkv2[] =
215 objc2_message_refs_section,
216 objc2_classdefs_section,
217 objc2_metadata_section,
218 objc2_classrefs_section,
219 objc2_classlist_section,
220 objc2_categorylist_section,
221 objc2_selector_refs_section,
222 objc2_nonlazy_class_section,
223 objc2_nonlazy_category_section,
224 objc2_protocollist_section,
225 objc2_protocolrefs_section,
226 objc2_super_classrefs_section,
227 objc2_image_info_section,
228 objc2_constant_string_object_section
230 size_t i;
232 been_here = true;
233 if (flag_objc_abi < 2)
235 for (i = 0; i < ARRAY_SIZE (tomark); i++)
236 switch_to_section (darwin_sections[tomark[i]]);
237 if (flag_objc_abi == 1)
238 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
239 switch_to_section (darwin_sections[tomarkv1[i]]);
241 else
242 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
243 switch_to_section (darwin_sections[tomarkv2[i]]);
244 /* Make sure we don't get varasm.c out of sync with us. */
245 switch_to_section (saved_in_section);
247 output_section_asm_op (directive);
251 /* Private flag applied to disable section-anchors in a particular section. */
252 #define SECTION_NO_ANCHOR SECTION_MACH_DEP
255 /* Implement TARGET_ASM_INIT_SECTIONS. */
257 void
258 darwin_init_sections (void)
260 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
261 darwin_sections[NAME] = \
262 get_unnamed_section (FLAGS, (OBJC \
263 ? output_objc_section_asm_op \
264 : output_section_asm_op), \
265 "\t" DIRECTIVE);
266 #include "config/darwin-sections.def"
267 #undef DEF_SECTION
269 readonly_data_section = darwin_sections[const_section];
270 exception_section = darwin_sections[darwin_exception_section];
271 eh_frame_section = darwin_sections[darwin_eh_frame_section];
275 name_needs_quotes (const char *name)
277 int c;
278 while ((c = *name++) != '\0')
279 if (! ISIDNUM (c)
280 && c != '.' && c != '$' && c != '_' )
281 return 1;
282 return 0;
285 /* Return true if SYM_REF can be used without an indirection. */
287 machopic_symbol_defined_p (rtx sym_ref)
289 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
290 return true;
292 /* If a symbol references local and is not an extern to this
293 file, then the symbol might be able to declared as defined. */
294 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
296 /* If the symbol references a variable and the variable is a
297 common symbol, then this symbol is not defined. */
298 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
300 tree decl = SYMBOL_REF_DECL (sym_ref);
301 if (!decl)
302 return true;
303 if (DECL_COMMON (decl))
304 return false;
306 return true;
308 return false;
311 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
312 reference, which will not be changed. */
314 enum machopic_addr_class
315 machopic_classify_symbol (rtx sym_ref)
317 bool function_p;
319 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
320 if (machopic_symbol_defined_p (sym_ref))
321 return (function_p
322 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
323 else
324 return (function_p
325 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
328 #ifndef TARGET_FIX_AND_CONTINUE
329 #define TARGET_FIX_AND_CONTINUE 0
330 #endif
332 /* Indicate when fix-and-continue style code generation is being used
333 and when a reference to data should be indirected so that it can be
334 rebound in a new translation unit to reference the original instance
335 of that data. Symbol names that are for code generation local to
336 the translation unit are bound to the new translation unit;
337 currently this means symbols that begin with L or _OBJC_;
338 otherwise, we indicate that an indirect reference should be made to
339 permit the runtime to rebind new instances of the translation unit
340 to the original instance of the data. */
342 static int
343 indirect_data (rtx sym_ref)
345 int lprefix;
346 const char *name;
348 /* If we aren't generating fix-and-continue code, don't do anything
349 special. */
350 if (TARGET_FIX_AND_CONTINUE == 0)
351 return 0;
353 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
354 are indirected. Symbols that begin with L and _OBJC_ are always
355 bound to the current translation unit as they are used for
356 generated local data of the translation unit. */
358 name = XSTR (sym_ref, 0);
360 lprefix = (((name[0] == '*' || name[0] == '&')
361 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
362 || (strncmp (name, "_OBJC_", 6) == 0));
364 return ! lprefix;
367 static int
368 machopic_data_defined_p (rtx sym_ref)
370 if (indirect_data (sym_ref))
371 return 0;
373 switch (machopic_classify_symbol (sym_ref))
375 case MACHOPIC_DEFINED_DATA:
376 case MACHOPIC_DEFINED_FUNCTION:
377 return 1;
378 default:
379 return 0;
383 void
384 machopic_define_symbol (rtx mem)
386 rtx sym_ref;
388 gcc_assert (GET_CODE (mem) == MEM);
389 sym_ref = XEXP (mem, 0);
390 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
393 /* Return either ORIG or:
395 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
397 depending on MACHO_DYNAMIC_NO_PIC_P. */
399 machopic_gen_offset (rtx orig)
401 if (MACHO_DYNAMIC_NO_PIC_P)
402 return orig;
403 else
405 /* Play games to avoid marking the function as needing pic if we
406 are being called as part of the cost-estimation process. */
407 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
408 crtl->uses_pic_offset_table = 1;
409 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
410 UNSPEC_MACHOPIC_OFFSET);
411 return gen_rtx_CONST (Pmode, orig);
415 static GTY(()) const char * function_base_func_name;
416 static GTY(()) int current_pic_label_num;
417 static GTY(()) int emitted_pic_label_num;
419 static void
420 update_pic_label_number_if_needed (void)
422 const char *current_name;
424 /* When we are generating _get_pc thunks within stubs, there is no current
425 function. */
426 if (current_function_decl)
428 current_name =
429 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
430 if (function_base_func_name != current_name)
432 ++current_pic_label_num;
433 function_base_func_name = current_name;
436 else
438 ++current_pic_label_num;
439 function_base_func_name = "L_machopic_stub_dummy";
443 void
444 machopic_output_function_base_name (FILE *file)
446 /* If dynamic-no-pic is on, we should not get here. */
447 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
449 update_pic_label_number_if_needed ();
450 fprintf (file, "L%d$pb", current_pic_label_num);
453 char curr_picbasename[32];
455 const char *
456 machopic_get_function_picbase (void)
458 /* If dynamic-no-pic is on, we should not get here. */
459 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
461 update_pic_label_number_if_needed ();
462 snprintf (curr_picbasename, 32, "L%d$pb", current_pic_label_num);
463 return (const char *) curr_picbasename;
466 bool
467 machopic_should_output_picbase_label (void)
469 update_pic_label_number_if_needed ();
471 if (current_pic_label_num == emitted_pic_label_num)
472 return false;
474 emitted_pic_label_num = current_pic_label_num;
475 return true;
478 /* The suffix attached to non-lazy pointer symbols. */
479 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
480 /* The suffix attached to stub symbols. */
481 #define STUB_SUFFIX "$stub"
483 typedef struct GTY ((for_user)) machopic_indirection
485 /* The SYMBOL_REF for the entity referenced. */
486 rtx symbol;
487 /* The name of the stub or non-lazy pointer. */
488 const char * ptr_name;
489 /* True iff this entry is for a stub (as opposed to a non-lazy
490 pointer). */
491 bool stub_p;
492 /* True iff this stub or pointer pointer has been referenced. */
493 bool used;
494 } machopic_indirection;
496 struct indirection_hasher : ggc_hasher<machopic_indirection *>
498 typedef const char *compare_type;
499 static hashval_t hash (machopic_indirection *);
500 static bool equal (machopic_indirection *, const char *);
503 /* A table mapping stub names and non-lazy pointer names to
504 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
506 static GTY (()) hash_table<indirection_hasher> *machopic_indirections;
508 /* Return a hash value for a SLOT in the indirections hash table. */
510 hashval_t
511 indirection_hasher::hash (machopic_indirection *p)
513 return htab_hash_string (p->ptr_name);
516 /* Returns true if the KEY is the same as that associated with
517 SLOT. */
519 bool
520 indirection_hasher::equal (machopic_indirection *s, const char *k)
522 return strcmp (s->ptr_name, k) == 0;
525 /* Return the name of the non-lazy pointer (if STUB_P is false) or
526 stub (if STUB_B is true) corresponding to the given name. */
528 const char *
529 machopic_indirection_name (rtx sym_ref, bool stub_p)
531 char *buffer;
532 const char *name = XSTR (sym_ref, 0);
533 size_t namelen = strlen (name);
534 machopic_indirection *p;
535 bool needs_quotes;
536 const char *suffix;
537 const char *prefix = user_label_prefix;
538 const char *quote = "";
539 tree id;
541 id = maybe_get_identifier (name);
542 if (id)
544 tree id_orig = id;
546 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
547 id = TREE_CHAIN (id);
548 if (id != id_orig)
550 name = IDENTIFIER_POINTER (id);
551 namelen = strlen (name);
555 if (name[0] == '*')
557 prefix = "";
558 ++name;
559 --namelen;
562 needs_quotes = name_needs_quotes (name);
563 if (needs_quotes)
565 quote = "\"";
568 if (stub_p)
569 suffix = STUB_SUFFIX;
570 else
571 suffix = NON_LAZY_POINTER_SUFFIX;
573 buffer = XALLOCAVEC (char, strlen ("&L")
574 + strlen (prefix)
575 + namelen
576 + strlen (suffix)
577 + 2 * strlen (quote)
578 + 1 /* '\0' */);
580 /* Construct the name of the non-lazy pointer or stub. */
581 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
583 if (!machopic_indirections)
584 machopic_indirections = hash_table<indirection_hasher>::create_ggc (37);
586 machopic_indirection **slot
587 = machopic_indirections->find_slot_with_hash (buffer,
588 htab_hash_string (buffer),
589 INSERT);
590 if (*slot)
592 p = *slot;
594 else
596 p = ggc_alloc<machopic_indirection> ();
597 p->symbol = sym_ref;
598 p->ptr_name = xstrdup (buffer);
599 p->stub_p = stub_p;
600 p->used = false;
601 *slot = p;
604 return p->ptr_name;
607 /* Return the name of the stub for the mcount function. */
609 const char*
610 machopic_mcount_stub_name (void)
612 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
613 return machopic_indirection_name (symbol, /*stub_p=*/true);
616 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
617 or non-lazy pointer as used -- and mark the object to which the
618 pointer/stub refers as used as well, since the pointer/stub will
619 emit a reference to it. */
621 void
622 machopic_validate_stub_or_non_lazy_ptr (const char *name)
624 machopic_indirection *p
625 = machopic_indirections->find_with_hash (name, htab_hash_string (name));
626 if (p && ! p->used)
628 const char *real_name;
629 tree id;
631 p->used = true;
633 /* Do what output_addr_const will do when we actually call it. */
634 if (SYMBOL_REF_DECL (p->symbol))
635 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
637 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
639 id = maybe_get_identifier (real_name);
640 if (id)
641 mark_referenced (id);
645 /* Transform ORIG, which may be any data source, to the corresponding
646 source using indirections. */
649 machopic_indirect_data_reference (rtx orig, rtx reg)
651 rtx ptr_ref = orig;
653 if (! MACHOPIC_INDIRECT)
654 return orig;
656 if (GET_CODE (orig) == SYMBOL_REF)
658 int defined = machopic_data_defined_p (orig);
660 if (defined && MACHO_DYNAMIC_NO_PIC_P)
662 if (DARWIN_PPC)
664 /* Create a new register for CSE opportunities. */
665 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
666 emit_insn (gen_macho_high (hi_reg, orig));
667 emit_insn (gen_macho_low (reg, hi_reg, orig));
668 return reg;
670 else if (DARWIN_X86)
671 return orig;
672 else
673 /* some other cpu -- writeme! */
674 gcc_unreachable ();
676 else if (defined)
678 rtx offset = NULL;
679 if (DARWIN_PPC || HAVE_lo_sum)
680 offset = machopic_gen_offset (orig);
682 if (DARWIN_PPC)
684 rtx hi_sum_reg = (!can_create_pseudo_p ()
685 ? reg
686 : gen_reg_rtx (Pmode));
688 gcc_assert (reg);
690 emit_insn (gen_rtx_SET (hi_sum_reg,
691 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
692 gen_rtx_HIGH (Pmode, offset))));
693 emit_insn (gen_rtx_SET (reg,
694 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
695 copy_rtx (offset))));
697 orig = reg;
699 else if (HAVE_lo_sum)
701 gcc_assert (reg);
703 emit_insn (gen_rtx_SET (reg, gen_rtx_HIGH (Pmode, offset)));
704 emit_insn (gen_rtx_SET (reg, gen_rtx_LO_SUM (Pmode, reg,
705 copy_rtx (offset))));
706 emit_use (pic_offset_table_rtx);
708 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
710 return orig;
713 ptr_ref = (gen_rtx_SYMBOL_REF
714 (Pmode,
715 machopic_indirection_name (orig, /*stub_p=*/false)));
717 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
719 ptr_ref = gen_const_mem (Pmode, ptr_ref);
720 machopic_define_symbol (ptr_ref);
722 if (DARWIN_X86
723 && reg
724 && MACHO_DYNAMIC_NO_PIC_P)
726 emit_insn (gen_rtx_SET (reg, ptr_ref));
727 ptr_ref = reg;
730 return ptr_ref;
732 else if (GET_CODE (orig) == CONST)
734 /* If "(const (plus ...", walk the PLUS and return that result.
735 PLUS processing (below) will restore the "(const ..." if
736 appropriate. */
737 if (GET_CODE (XEXP (orig, 0)) == PLUS)
738 return machopic_indirect_data_reference (XEXP (orig, 0), reg);
739 else
740 return orig;
742 else if (GET_CODE (orig) == MEM)
744 XEXP (ptr_ref, 0) =
745 machopic_indirect_data_reference (XEXP (orig, 0), reg);
746 return ptr_ref;
748 else if (GET_CODE (orig) == PLUS)
750 rtx base, result;
751 /* When the target is i386, this code prevents crashes due to the
752 compiler's ignorance on how to move the PIC base register to
753 other registers. (The reload phase sometimes introduces such
754 insns.) */
755 if (GET_CODE (XEXP (orig, 0)) == REG
756 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
757 /* Prevent the same register from being erroneously used
758 as both the base and index registers. */
759 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
760 && reg)
762 emit_move_insn (reg, XEXP (orig, 0));
763 XEXP (ptr_ref, 0) = reg;
764 return ptr_ref;
767 /* Legitimize both operands of the PLUS. */
768 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
769 orig = machopic_indirect_data_reference (XEXP (orig, 1),
770 (base == reg ? 0 : reg));
771 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
772 result = plus_constant (Pmode, base, INTVAL (orig));
773 else
774 result = gen_rtx_PLUS (Pmode, base, orig);
776 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
778 if (reg)
780 emit_move_insn (reg, result);
781 result = reg;
783 else
785 result = force_reg (GET_MODE (result), result);
789 return result;
791 return ptr_ref;
794 /* Transform TARGET (a MEM), which is a function call target, to the
795 corresponding symbol_stub if necessary. Return a new MEM. */
798 machopic_indirect_call_target (rtx target)
800 if (! darwin_emit_branch_islands)
801 return target;
803 if (GET_CODE (target) != MEM)
804 return target;
806 if (MACHOPIC_INDIRECT
807 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
808 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
809 & MACHO_SYMBOL_FLAG_DEFINED))
811 rtx sym_ref = XEXP (target, 0);
812 const char *stub_name = machopic_indirection_name (sym_ref,
813 /*stub_p=*/true);
814 machine_mode mode = GET_MODE (sym_ref);
816 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
817 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
818 MEM_READONLY_P (target) = 1;
819 MEM_NOTRAP_P (target) = 1;
822 return target;
826 machopic_legitimize_pic_address (rtx orig, machine_mode mode, rtx reg)
828 rtx pic_ref = orig;
830 if (! MACHOPIC_INDIRECT)
831 return orig;
833 /* First handle a simple SYMBOL_REF or LABEL_REF */
834 if (GET_CODE (orig) == LABEL_REF
835 || (GET_CODE (orig) == SYMBOL_REF
838 /* addr(foo) = &func+(foo-func) */
839 orig = machopic_indirect_data_reference (orig, reg);
841 if (GET_CODE (orig) == PLUS
842 && GET_CODE (XEXP (orig, 0)) == REG)
844 if (reg == 0)
845 return force_reg (mode, orig);
847 emit_move_insn (reg, orig);
848 return reg;
851 if (GET_CODE (orig) == MEM)
853 if (reg == 0)
855 gcc_assert (!reload_in_progress);
856 reg = gen_reg_rtx (Pmode);
859 #if HAVE_lo_sum
860 if (MACHO_DYNAMIC_NO_PIC_P
861 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
862 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
864 #if defined (TARGET_TOC) /* ppc */
865 rtx temp_reg = (!can_create_pseudo_p ()
866 ? reg :
867 gen_reg_rtx (Pmode));
868 rtx asym = XEXP (orig, 0);
869 rtx mem;
871 emit_insn (gen_macho_high (temp_reg, asym));
872 mem = gen_const_mem (GET_MODE (orig),
873 gen_rtx_LO_SUM (Pmode, temp_reg,
874 copy_rtx (asym)));
875 emit_insn (gen_rtx_SET (reg, mem));
876 #else
877 /* Some other CPU -- WriteMe! but right now there are no other
878 platforms that can use dynamic-no-pic */
879 gcc_unreachable ();
880 #endif
881 pic_ref = reg;
883 else
884 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
885 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
887 rtx offset = machopic_gen_offset (XEXP (orig, 0));
888 #if defined (TARGET_TOC) /* i.e., PowerPC */
889 /* Generating a new reg may expose opportunities for
890 common subexpression elimination. */
891 rtx hi_sum_reg = (!can_create_pseudo_p ()
892 ? reg
893 : gen_reg_rtx (Pmode));
894 rtx mem;
895 rtx insn;
896 rtx sum;
898 sum = gen_rtx_HIGH (Pmode, offset);
899 if (! MACHO_DYNAMIC_NO_PIC_P)
900 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
902 emit_insn (gen_rtx_SET (hi_sum_reg, sum));
904 mem = gen_const_mem (GET_MODE (orig),
905 gen_rtx_LO_SUM (Pmode,
906 hi_sum_reg,
907 copy_rtx (offset)));
908 insn = emit_insn (gen_rtx_SET (reg, mem));
909 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
911 pic_ref = reg;
912 #else
913 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
915 emit_insn (gen_rtx_SET (reg,
916 gen_rtx_HIGH (Pmode,
917 gen_rtx_CONST (Pmode,
918 offset))));
919 emit_insn (gen_rtx_SET (reg,
920 gen_rtx_LO_SUM (Pmode, reg,
921 gen_rtx_CONST (Pmode,
922 copy_rtx (offset)))));
923 pic_ref = gen_rtx_PLUS (Pmode,
924 pic_offset_table_rtx, reg);
925 #endif
927 else
928 #endif /* HAVE_lo_sum */
930 rtx pic = pic_offset_table_rtx;
931 if (GET_CODE (pic) != REG)
933 emit_move_insn (reg, pic);
934 pic = reg;
936 #if 0
937 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
938 #endif
940 if (reload_in_progress)
941 df_set_regs_ever_live (REGNO (pic), true);
942 pic_ref = gen_rtx_PLUS (Pmode, pic,
943 machopic_gen_offset (XEXP (orig, 0)));
946 #if !defined (TARGET_TOC)
947 emit_move_insn (reg, pic_ref);
948 pic_ref = gen_const_mem (GET_MODE (orig), reg);
949 #endif
951 else
954 #if HAVE_lo_sum
955 if (GET_CODE (orig) == SYMBOL_REF
956 || GET_CODE (orig) == LABEL_REF)
958 rtx offset = machopic_gen_offset (orig);
959 #if defined (TARGET_TOC) /* i.e., PowerPC */
960 rtx hi_sum_reg;
962 if (reg == 0)
964 gcc_assert (!reload_in_progress);
965 reg = gen_reg_rtx (Pmode);
968 hi_sum_reg = reg;
970 emit_insn (gen_rtx_SET (hi_sum_reg,
971 (MACHO_DYNAMIC_NO_PIC_P)
972 ? gen_rtx_HIGH (Pmode, offset)
973 : gen_rtx_PLUS (Pmode,
974 pic_offset_table_rtx,
975 gen_rtx_HIGH (Pmode,
976 offset))));
977 emit_insn (gen_rtx_SET (reg,
978 gen_rtx_LO_SUM (Pmode,
979 hi_sum_reg,
980 copy_rtx (offset))));
981 pic_ref = reg;
982 #else
983 emit_insn (gen_rtx_SET (reg, gen_rtx_HIGH (Pmode, offset)));
984 emit_insn (gen_rtx_SET (reg,
985 gen_rtx_LO_SUM (Pmode, reg,
986 copy_rtx (offset))));
987 pic_ref = gen_rtx_PLUS (Pmode,
988 pic_offset_table_rtx, reg);
989 #endif
991 else
992 #endif /* HAVE_lo_sum */
994 if (REG_P (orig)
995 || GET_CODE (orig) == SUBREG)
997 return orig;
999 else
1001 rtx pic = pic_offset_table_rtx;
1002 if (GET_CODE (pic) != REG)
1004 emit_move_insn (reg, pic);
1005 pic = reg;
1007 #if 0
1008 emit_use (pic_offset_table_rtx);
1009 #endif
1010 if (reload_in_progress)
1011 df_set_regs_ever_live (REGNO (pic), true);
1012 pic_ref = gen_rtx_PLUS (Pmode,
1013 pic,
1014 machopic_gen_offset (orig));
1019 if (GET_CODE (pic_ref) != REG)
1021 if (reg != 0)
1023 emit_move_insn (reg, pic_ref);
1024 return reg;
1026 else
1028 return force_reg (mode, pic_ref);
1031 else
1033 return pic_ref;
1037 else if (GET_CODE (orig) == SYMBOL_REF)
1038 return orig;
1040 else if (GET_CODE (orig) == PLUS
1041 && (GET_CODE (XEXP (orig, 0)) == MEM
1042 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
1043 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
1044 && XEXP (orig, 0) != pic_offset_table_rtx
1045 && GET_CODE (XEXP (orig, 1)) != REG)
1048 rtx base;
1049 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
1051 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1052 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
1053 Pmode, (base == reg ? 0 : reg));
1054 if (GET_CODE (orig) == CONST_INT)
1056 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
1057 is_complex = 1;
1059 else
1060 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
1062 if (reg && is_complex)
1064 emit_move_insn (reg, pic_ref);
1065 pic_ref = reg;
1067 /* Likewise, should we set special REG_NOTEs here? */
1070 else if (GET_CODE (orig) == CONST)
1072 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1075 else if (GET_CODE (orig) == MEM
1076 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
1078 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1079 addr = replace_equiv_address (orig, addr);
1080 emit_move_insn (reg, addr);
1081 pic_ref = reg;
1084 return pic_ref;
1087 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
1088 DATA is the FILE* for assembly output. Called from
1089 htab_traverse. */
1092 machopic_output_indirection (machopic_indirection **slot, FILE *asm_out_file)
1094 machopic_indirection *p = *slot;
1095 rtx symbol;
1096 const char *sym_name;
1097 const char *ptr_name;
1099 if (!p->used)
1100 return 1;
1102 symbol = p->symbol;
1103 sym_name = XSTR (symbol, 0);
1104 ptr_name = p->ptr_name;
1106 if (p->stub_p)
1108 char *sym;
1109 char *stub;
1110 tree id;
1112 id = maybe_get_identifier (sym_name);
1113 if (id)
1115 tree id_orig = id;
1117 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1118 id = TREE_CHAIN (id);
1119 if (id != id_orig)
1120 sym_name = IDENTIFIER_POINTER (id);
1123 sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1124 if (sym_name[0] == '*' || sym_name[0] == '&')
1125 strcpy (sym, sym_name + 1);
1126 else if (sym_name[0] == '-' || sym_name[0] == '+')
1127 strcpy (sym, sym_name);
1128 else
1129 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1131 stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1132 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1133 strcpy (stub, ptr_name + 1);
1134 else
1135 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1137 machopic_output_stub (asm_out_file, sym, stub);
1139 else if (! indirect_data (symbol)
1140 && (machopic_symbol_defined_p (symbol)
1141 || SYMBOL_REF_LOCAL_P (symbol)))
1143 switch_to_section (data_section);
1144 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1145 assemble_label (asm_out_file, ptr_name);
1146 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1147 GET_MODE_SIZE (Pmode),
1148 GET_MODE_ALIGNMENT (Pmode), 1);
1150 else
1152 rtx init = const0_rtx;
1154 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1156 /* Mach-O symbols are passed around in code through indirect
1157 references and the original symbol_ref hasn't passed through
1158 the generic handling and reference-catching in
1159 output_operand, so we need to manually mark weak references
1160 as such. */
1161 if (SYMBOL_REF_WEAK (symbol))
1163 tree decl = SYMBOL_REF_DECL (symbol);
1164 gcc_assert (DECL_P (decl));
1166 if (decl != NULL_TREE
1167 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
1168 /* Handle only actual external-only definitions, not
1169 e.g. extern inline code or variables for which
1170 storage has been allocated. */
1171 && !TREE_STATIC (decl))
1173 fputs ("\t.weak_reference ", asm_out_file);
1174 assemble_name (asm_out_file, sym_name);
1175 fputc ('\n', asm_out_file);
1179 assemble_name (asm_out_file, ptr_name);
1180 fprintf (asm_out_file, ":\n");
1182 fprintf (asm_out_file, "\t.indirect_symbol ");
1183 assemble_name (asm_out_file, sym_name);
1184 fprintf (asm_out_file, "\n");
1186 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1187 have their symbol name instead of 0 in the second entry of
1188 the non-lazy symbol pointer data structure when they are
1189 defined. This allows the runtime to rebind newer instances
1190 of the translation unit with the original instance of the
1191 symbol. */
1193 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1194 && machopic_symbol_defined_p (symbol))
1195 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1197 assemble_integer (init, GET_MODE_SIZE (Pmode),
1198 GET_MODE_ALIGNMENT (Pmode), 1);
1201 return 1;
1204 void
1205 machopic_finish (FILE *asm_out_file)
1207 if (machopic_indirections)
1208 machopic_indirections
1209 ->traverse_noresize<FILE *, machopic_output_indirection> (asm_out_file);
1213 machopic_operand_p (rtx op)
1215 if (MACHOPIC_JUST_INDIRECT)
1216 return (GET_CODE (op) == SYMBOL_REF
1217 && machopic_symbol_defined_p (op));
1218 else
1219 return (GET_CODE (op) == CONST
1220 && GET_CODE (XEXP (op, 0)) == UNSPEC
1221 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1224 /* This function records whether a given name corresponds to a defined
1225 or undefined function or variable, for machopic_classify_ident to
1226 use later. */
1228 void
1229 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1231 rtx sym_ref;
1233 /* Do the standard encoding things first. */
1234 default_encode_section_info (decl, rtl, first);
1236 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1237 return;
1239 sym_ref = XEXP (rtl, 0);
1240 if (TREE_CODE (decl) == VAR_DECL)
1241 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1243 if (!DECL_EXTERNAL (decl)
1244 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1245 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1246 && ((TREE_STATIC (decl)
1247 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1248 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1249 && DECL_INITIAL (decl) != error_mark_node)))
1250 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1252 if (! TREE_PUBLIC (decl))
1253 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1256 void
1257 darwin_mark_decl_preserved (const char *name)
1259 fprintf (asm_out_file, "\t.no_dead_strip ");
1260 assemble_name (asm_out_file, name);
1261 fputc ('\n', asm_out_file);
1264 static section *
1265 darwin_rodata_section (int weak, bool zsize)
1267 return (weak
1268 ? darwin_sections[const_coal_section]
1269 : (zsize ? darwin_sections[zobj_const_section]
1270 : darwin_sections[const_section]));
1273 static section *
1274 darwin_mergeable_string_section (tree exp,
1275 unsigned HOST_WIDE_INT align)
1277 /* Darwin's ld expects to see non-writable string literals in the .cstring
1278 section. Later versions of ld check and complain when CFStrings are
1279 enabled. Therefore we shall force the strings into .cstring since we
1280 don't support writable ones anyway. */
1281 if ((darwin_constant_cfstrings || flag_merge_constants)
1282 && TREE_CODE (exp) == STRING_CST
1283 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1284 && align <= 256
1285 && (int_size_in_bytes (TREE_TYPE (exp))
1286 == TREE_STRING_LENGTH (exp))
1287 && ((size_t) TREE_STRING_LENGTH (exp)
1288 == strlen (TREE_STRING_POINTER (exp)) + 1))
1289 return darwin_sections[cstring_section];
1291 if (DARWIN_SECTION_ANCHORS && flag_section_anchors
1292 && TREE_CODE (exp) == STRING_CST
1293 && TREE_STRING_LENGTH (exp) == 0)
1294 return darwin_sections[zobj_const_section];
1296 return readonly_data_section;
1299 #ifndef HAVE_GAS_LITERAL16
1300 #define HAVE_GAS_LITERAL16 0
1301 #endif
1303 static section *
1304 darwin_mergeable_constant_section (tree exp,
1305 unsigned HOST_WIDE_INT align,
1306 bool zsize)
1308 machine_mode mode = DECL_MODE (exp);
1309 unsigned int modesize = GET_MODE_BITSIZE (mode);
1311 if (DARWIN_SECTION_ANCHORS
1312 && flag_section_anchors
1313 && zsize)
1314 return darwin_sections[zobj_const_section];
1316 if (flag_merge_constants
1317 && mode != VOIDmode
1318 && mode != BLKmode
1319 && modesize <= align
1320 && align >= 8
1321 && align <= 256
1322 && (align & (align -1)) == 0)
1324 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1326 if (TREE_CODE (size) == INTEGER_CST)
1328 if (wi::eq_p (size, 4))
1329 return darwin_sections[literal4_section];
1330 else if (wi::eq_p (size, 8))
1331 return darwin_sections[literal8_section];
1332 else if (HAVE_GAS_LITERAL16
1333 && TARGET_64BIT
1334 && wi::eq_p (size, 16))
1335 return darwin_sections[literal16_section];
1339 return readonly_data_section;
1342 section *
1343 darwin_tm_clone_table_section (void)
1345 return get_named_section (NULL,
1346 "__DATA,__tm_clone_table,regular,no_dead_strip",
1351 machopic_reloc_rw_mask (void)
1353 return MACHOPIC_INDIRECT ? 3 : 0;
1356 /* We have to deal with ObjC/C++ metadata section placement in the common
1357 code, since it will also be called from LTO.
1359 Return metadata attributes, if present (searching for ABI=2 first)
1360 Return NULL_TREE if no such attributes are found. */
1362 static tree
1363 is_objc_metadata (tree decl)
1365 if (DECL_P (decl)
1366 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1367 && DECL_ATTRIBUTES (decl))
1369 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1370 if (meta)
1371 return meta;
1372 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1373 if (meta)
1374 return meta;
1376 return NULL_TREE;
1379 static int classes_seen;
1380 static int objc_metadata_seen;
1382 /* Return the section required for Objective C ABI 2 metadata. */
1383 static section *
1384 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1386 const char *p;
1387 tree ident = TREE_VALUE (meta);
1388 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1389 p = IDENTIFIER_POINTER (ident);
1391 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi == 2);
1393 objc_metadata_seen = 1;
1395 if (base == data_section)
1396 base = darwin_sections[objc2_metadata_section];
1398 /* Most of the OBJC2 META-data end up in the base section, so check it
1399 first. */
1400 if (!strncmp (p, "V2_BASE", 7))
1401 return base;
1402 else if (!strncmp (p, "V2_STRG", 7))
1403 return darwin_sections[cstring_section];
1405 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1406 return darwin_sections[objc2_classdefs_section];
1407 else if (!strncmp (p, "V2_MREF", 7))
1408 return darwin_sections[objc2_message_refs_section];
1409 else if (!strncmp (p, "V2_CLRF", 7))
1410 return darwin_sections[objc2_classrefs_section];
1411 else if (!strncmp (p, "V2_SURF", 7))
1412 return darwin_sections[objc2_super_classrefs_section];
1413 else if (!strncmp (p, "V2_NLCL", 7))
1414 return darwin_sections[objc2_nonlazy_class_section];
1415 else if (!strncmp (p, "V2_CLAB", 7))
1417 classes_seen = 1;
1418 return darwin_sections[objc2_classlist_section];
1420 else if (!strncmp (p, "V2_SRFS", 7))
1421 return darwin_sections[objc2_selector_refs_section];
1422 else if (!strncmp (p, "V2_NLCA", 7))
1423 return darwin_sections[objc2_nonlazy_category_section];
1424 else if (!strncmp (p, "V2_CALA", 7))
1425 return darwin_sections[objc2_categorylist_section];
1427 else if (!strncmp (p, "V2_PLST", 7))
1428 return darwin_sections[objc2_protocollist_section];
1429 else if (!strncmp (p, "V2_PRFS", 7))
1430 return darwin_sections[objc2_protocolrefs_section];
1432 else if (!strncmp (p, "V2_INFO", 7))
1433 return darwin_sections[objc2_image_info_section];
1435 else if (!strncmp (p, "V2_EHTY", 7))
1436 return darwin_sections[data_coal_section];
1438 else if (!strncmp (p, "V2_CSTR", 7))
1439 return darwin_sections[objc2_constant_string_object_section];
1441 /* Not recognized, default. */
1442 return base;
1445 /* Return the section required for Objective C ABI 0/1 metadata. */
1446 static section *
1447 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1449 const char *p;
1450 tree ident = TREE_VALUE (meta);
1451 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1452 p = IDENTIFIER_POINTER (ident);
1454 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi < 2);
1456 objc_metadata_seen = 1;
1458 /* String sections first, cos there are lots of strings. */
1459 if (!strncmp (p, "V1_STRG", 7))
1460 return darwin_sections[cstring_section];
1461 else if (!strncmp (p, "V1_CLSN", 7))
1462 return darwin_sections[objc_class_names_section];
1463 else if (!strncmp (p, "V1_METN", 7))
1464 return darwin_sections[objc_meth_var_names_section];
1465 else if (!strncmp (p, "V1_METT", 7))
1466 return darwin_sections[objc_meth_var_types_section];
1468 else if (!strncmp (p, "V1_CLAS", 7))
1470 classes_seen = 1;
1471 return darwin_sections[objc_class_section];
1473 else if (!strncmp (p, "V1_META", 7))
1474 return darwin_sections[objc_meta_class_section];
1475 else if (!strncmp (p, "V1_CATG", 7))
1476 return darwin_sections[objc_category_section];
1477 else if (!strncmp (p, "V1_PROT", 7))
1478 return darwin_sections[objc_protocol_section];
1480 else if (!strncmp (p, "V1_CLCV", 7))
1481 return darwin_sections[objc_class_vars_section];
1482 else if (!strncmp (p, "V1_CLIV", 7))
1483 return darwin_sections[objc_instance_vars_section];
1485 else if (!strncmp (p, "V1_CLCM", 7))
1486 return darwin_sections[objc_cls_meth_section];
1487 else if (!strncmp (p, "V1_CLIM", 7))
1488 return darwin_sections[objc_inst_meth_section];
1489 else if (!strncmp (p, "V1_CACM", 7))
1490 return darwin_sections[objc_cat_cls_meth_section];
1491 else if (!strncmp (p, "V1_CAIM", 7))
1492 return darwin_sections[objc_cat_inst_meth_section];
1493 else if (!strncmp (p, "V1_PNSM", 7))
1494 return darwin_sections[objc_cat_inst_meth_section];
1495 else if (!strncmp (p, "V1_PCLM", 7))
1496 return darwin_sections[objc_cat_cls_meth_section];
1498 else if (!strncmp (p, "V1_CLPR", 7))
1499 return darwin_sections[objc_cat_cls_meth_section];
1500 else if (!strncmp (p, "V1_CAPR", 7))
1501 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1503 else if (!strncmp (p, "V1_PRFS", 7))
1504 return darwin_sections[objc_cat_cls_meth_section];
1505 else if (!strncmp (p, "V1_CLRF", 7))
1506 return darwin_sections[objc_cls_refs_section];
1507 else if (!strncmp (p, "V1_SRFS", 7))
1508 return darwin_sections[objc_selector_refs_section];
1510 else if (!strncmp (p, "V1_MODU", 7))
1511 return darwin_sections[objc_module_info_section];
1512 else if (!strncmp (p, "V1_SYMT", 7))
1513 return darwin_sections[objc_symbols_section];
1514 else if (!strncmp (p, "V1_INFO", 7))
1515 return darwin_sections[objc_image_info_section];
1517 else if (!strncmp (p, "V1_PLST", 7))
1518 return darwin_sections[objc1_prop_list_section];
1519 else if (!strncmp (p, "V1_PEXT", 7))
1520 return darwin_sections[objc1_protocol_ext_section];
1521 else if (!strncmp (p, "V1_CEXT", 7))
1522 return darwin_sections[objc1_class_ext_section];
1524 else if (!strncmp (p, "V2_CSTR", 7))
1525 return darwin_sections[objc_constant_string_object_section];
1527 return base;
1530 section *
1531 machopic_select_section (tree decl,
1532 int reloc,
1533 unsigned HOST_WIDE_INT align)
1535 bool zsize, one, weak, ro;
1536 section *base_section = NULL;
1538 weak = (DECL_P (decl)
1539 && DECL_WEAK (decl)
1540 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1542 zsize = (DECL_P (decl)
1543 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1544 && tree_to_uhwi (DECL_SIZE_UNIT (decl)) == 0);
1546 one = DECL_P (decl)
1547 && TREE_CODE (decl) == VAR_DECL
1548 && DECL_COMDAT_GROUP (decl);
1550 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1552 switch (categorize_decl_for_section (decl, reloc))
1554 case SECCAT_TEXT:
1555 gcc_unreachable ();
1556 break;
1558 case SECCAT_RODATA:
1559 case SECCAT_SRODATA:
1560 base_section = darwin_rodata_section (weak, zsize);
1561 break;
1563 case SECCAT_RODATA_MERGE_STR:
1564 base_section = darwin_mergeable_string_section (decl, align);
1565 break;
1567 case SECCAT_RODATA_MERGE_STR_INIT:
1568 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1569 break;
1571 case SECCAT_RODATA_MERGE_CONST:
1572 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1573 break;
1575 case SECCAT_DATA:
1576 case SECCAT_DATA_REL:
1577 case SECCAT_DATA_REL_LOCAL:
1578 case SECCAT_DATA_REL_RO:
1579 case SECCAT_DATA_REL_RO_LOCAL:
1580 case SECCAT_SDATA:
1581 case SECCAT_TDATA:
1582 if (weak || one)
1584 if (ro)
1585 base_section = darwin_sections[const_data_coal_section];
1586 else
1587 base_section = darwin_sections[data_coal_section];
1589 else if (DARWIN_SECTION_ANCHORS
1590 && flag_section_anchors
1591 && zsize)
1593 /* If we're doing section anchors, then punt zero-sized objects into
1594 their own sections so that they don't interfere with offset
1595 computation for the remaining vars. This does not need to be done
1596 for stuff in mergeable sections, since these are ineligible for
1597 anchors. */
1598 if (ro)
1599 base_section = darwin_sections[zobj_const_data_section];
1600 else
1601 base_section = darwin_sections[zobj_data_section];
1603 else if (ro)
1604 base_section = darwin_sections[const_data_section];
1605 else
1606 base_section = data_section;
1607 break;
1608 case SECCAT_BSS:
1609 case SECCAT_SBSS:
1610 case SECCAT_TBSS:
1611 if (weak || one)
1612 base_section = darwin_sections[data_coal_section];
1613 else
1615 if (!TREE_PUBLIC (decl))
1616 base_section = lcomm_section;
1617 else if (bss_noswitch_section)
1618 base_section = bss_noswitch_section;
1619 else
1620 base_section = data_section;
1622 break;
1624 default:
1625 gcc_unreachable ();
1628 /* Darwin weird special cases.
1629 a) OBJC Meta-data. */
1630 if (DECL_P (decl)
1631 && (TREE_CODE (decl) == VAR_DECL
1632 || TREE_CODE (decl) == CONST_DECL)
1633 && DECL_ATTRIBUTES (decl))
1635 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1636 if (meta)
1637 return darwin_objc2_section (decl, meta, base_section);
1638 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1639 if (meta)
1640 return darwin_objc1_section (decl, meta, base_section);
1641 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1642 if (meta)
1643 return base_section; /* GNU runtime is happy with it all in one pot. */
1646 /* b) Constant string objects. */
1647 if (TREE_CODE (decl) == CONSTRUCTOR
1648 && TREE_TYPE (decl)
1649 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1650 && TYPE_NAME (TREE_TYPE (decl)))
1652 tree name = TYPE_NAME (TREE_TYPE (decl));
1653 if (TREE_CODE (name) == TYPE_DECL)
1654 name = DECL_NAME (name);
1656 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1658 if (flag_next_runtime)
1660 if (flag_objc_abi == 2)
1661 return darwin_sections[objc2_constant_string_object_section];
1662 else
1663 return darwin_sections[objc_constant_string_object_section];
1665 else
1666 return darwin_sections[objc_string_object_section];
1668 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1669 return darwin_sections[cfstring_constant_object_section];
1670 else
1671 return base_section;
1673 /* c) legacy meta-data selection. */
1674 else if (TREE_CODE (decl) == VAR_DECL
1675 && DECL_NAME (decl)
1676 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1677 && IDENTIFIER_POINTER (DECL_NAME (decl))
1678 && flag_next_runtime
1679 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1681 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1682 static bool warned_objc_46 = false;
1683 /* We shall assert that zero-sized objects are an error in ObjC
1684 meta-data. */
1685 gcc_assert (tree_to_uhwi (DECL_SIZE_UNIT (decl)) != 0);
1687 /* ??? This mechanism for determining the metadata section is
1688 broken when LTO is in use, since the frontend that generated
1689 the data is not identified. We will keep the capability for
1690 the short term - in case any non-Objective-C programs are using
1691 it to place data in specified sections. */
1692 if (!warned_objc_46)
1694 location_t loc = DECL_SOURCE_LOCATION (decl);
1695 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
1696 " to select meta-data sections is deprecated at 4.6"
1697 " and will be removed in 4.7");
1698 warned_objc_46 = true;
1701 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1702 return darwin_sections[objc_cls_meth_section];
1703 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1704 return darwin_sections[objc_inst_meth_section];
1705 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
1706 return darwin_sections[objc_cat_cls_meth_section];
1707 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
1708 return darwin_sections[objc_cat_inst_meth_section];
1709 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1710 return darwin_sections[objc_class_vars_section];
1711 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1712 return darwin_sections[objc_instance_vars_section];
1713 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1714 return darwin_sections[objc_cat_cls_meth_section];
1715 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1716 return darwin_sections[objc_class_names_section];
1717 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1718 return darwin_sections[objc_meth_var_names_section];
1719 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1720 return darwin_sections[objc_meth_var_types_section];
1721 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1722 return darwin_sections[objc_cls_refs_section];
1723 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1724 return darwin_sections[objc_class_section];
1725 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1726 return darwin_sections[objc_meta_class_section];
1727 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1728 return darwin_sections[objc_category_section];
1729 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1730 return darwin_sections[objc_selector_refs_section];
1731 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1732 return darwin_sections[objc_selector_fixup_section];
1733 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1734 return darwin_sections[objc_symbols_section];
1735 else if (!strncmp (name, "_OBJC_MODULES", 13))
1736 return darwin_sections[objc_module_info_section];
1737 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1738 return darwin_sections[objc_image_info_section];
1739 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1740 return darwin_sections[objc_cat_inst_meth_section];
1741 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1742 return darwin_sections[objc_cat_cls_meth_section];
1743 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1744 return darwin_sections[objc_cat_cls_meth_section];
1745 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1746 return darwin_sections[objc_protocol_section];
1747 else
1748 return base_section;
1751 return base_section;
1754 /* This can be called with address expressions as "rtx".
1755 They must go in "const". */
1757 section *
1758 machopic_select_rtx_section (machine_mode mode, rtx x,
1759 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1761 if (GET_MODE_SIZE (mode) == 8
1762 && (GET_CODE (x) == CONST_INT
1763 || GET_CODE (x) == CONST_WIDE_INT
1764 || GET_CODE (x) == CONST_DOUBLE))
1765 return darwin_sections[literal8_section];
1766 else if (GET_MODE_SIZE (mode) == 4
1767 && (GET_CODE (x) == CONST_INT
1768 || GET_CODE (x) == CONST_WIDE_INT
1769 || GET_CODE (x) == CONST_DOUBLE))
1770 return darwin_sections[literal4_section];
1771 else if (HAVE_GAS_LITERAL16
1772 && TARGET_64BIT
1773 && GET_MODE_SIZE (mode) == 16
1774 && (GET_CODE (x) == CONST_INT
1775 || GET_CODE (x) == CONST_WIDE_INT
1776 || GET_CODE (x) == CONST_DOUBLE
1777 || GET_CODE (x) == CONST_VECTOR))
1778 return darwin_sections[literal16_section];
1779 else if (MACHOPIC_INDIRECT
1780 && (GET_CODE (x) == SYMBOL_REF
1781 || GET_CODE (x) == CONST
1782 || GET_CODE (x) == LABEL_REF))
1783 return darwin_sections[const_data_section];
1784 else
1785 return darwin_sections[const_section];
1788 void
1789 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1791 cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
1793 vec_safe_push (ctors, new_elt);
1795 if (! MACHOPIC_INDIRECT)
1796 fprintf (asm_out_file, ".reference .constructors_used\n");
1799 void
1800 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1802 cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)};
1804 vec_safe_push (dtors, new_elt);
1806 if (! MACHOPIC_INDIRECT)
1807 fprintf (asm_out_file, ".reference .destructors_used\n");
1810 static int
1811 sort_cdtor_records (const void * a, const void * b)
1813 const cdtor_record *cda = (const cdtor_record *)a;
1814 const cdtor_record *cdb = (const cdtor_record *)b;
1815 if (cda->priority > cdb->priority)
1816 return 1;
1817 if (cda->priority < cdb->priority)
1818 return -1;
1819 if (cda->position > cdb->position)
1820 return 1;
1821 if (cda->position < cdb->position)
1822 return -1;
1823 return 0;
1826 static void
1827 finalize_ctors ()
1829 unsigned int i;
1830 cdtor_record *elt;
1832 if (MACHOPIC_INDIRECT)
1833 switch_to_section (darwin_sections[mod_init_section]);
1834 else
1835 switch_to_section (darwin_sections[constructor_section]);
1837 if (vec_safe_length (ctors) > 1)
1838 ctors->qsort (sort_cdtor_records);
1839 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
1841 assemble_align (POINTER_SIZE);
1842 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1846 static void
1847 finalize_dtors ()
1849 unsigned int i;
1850 cdtor_record *elt;
1852 if (MACHOPIC_INDIRECT)
1853 switch_to_section (darwin_sections[mod_term_section]);
1854 else
1855 switch_to_section (darwin_sections[destructor_section]);
1857 if (vec_safe_length (dtors) > 1)
1858 dtors->qsort (sort_cdtor_records);
1859 FOR_EACH_VEC_SAFE_ELT (dtors, i, elt)
1861 assemble_align (POINTER_SIZE);
1862 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1866 void
1867 darwin_globalize_label (FILE *stream, const char *name)
1869 if (!!strncmp (name, "_OBJC_", 6))
1870 default_globalize_label (stream, name);
1873 /* This routine returns non-zero if 'name' starts with the special objective-c
1874 anonymous file-scope static name. It accommodates c++'s mangling of such
1875 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1877 int
1878 darwin_label_is_anonymous_local_objc_name (const char *name)
1880 const unsigned char *p = (const unsigned char *) name;
1881 if (*p != '_')
1882 return 0;
1883 if (p[1] == 'Z' && p[2] == 'L')
1885 p += 3;
1886 while (*p >= '0' && *p <= '9')
1887 p++;
1889 return (!strncmp ((const char *)p, "_OBJC_", 6));
1892 /* LTO support for Mach-O.
1894 This version uses three mach-o sections to encapsulate the (unlimited
1895 number of) lto sections.
1897 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1898 __GNU_LTO, __section_names contains the GNU LTO section names.
1899 __GNU_LTO, __section_index contains an array of values that index these.
1901 Indexed thus:
1902 <section offset from the start of __GNU_LTO, __lto_sections>,
1903 <section length>
1904 <name offset from the start of __GNU_LTO, __section_names,
1905 <name length>.
1907 At present, for both m32 and m64 mach-o files each of these fields is
1908 represented by a uint32_t. This is because, AFAICT, a mach-o object
1909 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1911 uint32_t offset;
1912 "offset An integer specifying the offset to this section in the file." */
1914 /* Count lto section numbers. */
1915 static unsigned int lto_section_num = 0;
1917 /* A vector of information about LTO sections, at present, we only have
1918 the name. TODO: see if we can get the data length somehow. */
1919 typedef struct GTY (()) darwin_lto_section_e {
1920 const char *sectname;
1921 } darwin_lto_section_e ;
1923 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1925 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1926 sections into three Mach-O ones).
1927 NOTE: These names MUST be kept in sync with those in
1928 libiberty/simple-object-mach-o. */
1929 #define LTO_SECTS_SECTION "__wrapper_sects"
1930 #define LTO_NAMES_SECTION "__wrapper_names"
1931 #define LTO_INDEX_SECTION "__wrapper_index"
1933 /* File to temporarily store LTO data. This is appended to asm_out_file
1934 in darwin_end_file. */
1935 static FILE *lto_asm_out_file, *saved_asm_out_file;
1936 static char *lto_asm_out_name;
1938 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1939 asm_out_file and switching to an alternative output file. */
1940 void
1941 darwin_asm_lto_start (void)
1943 gcc_assert (! saved_asm_out_file);
1944 saved_asm_out_file = asm_out_file;
1945 if (! lto_asm_out_name)
1946 lto_asm_out_name = make_temp_file (".lto.s");
1947 lto_asm_out_file = fopen (lto_asm_out_name, "a");
1948 if (lto_asm_out_file == NULL)
1949 fatal_error (input_location,
1950 "failed to open temporary file %s for LTO output",
1951 lto_asm_out_name);
1952 asm_out_file = lto_asm_out_file;
1955 /* Restore asm_out_file. */
1956 void
1957 darwin_asm_lto_end (void)
1959 gcc_assert (saved_asm_out_file);
1960 fclose (lto_asm_out_file);
1961 asm_out_file = saved_asm_out_file;
1962 saved_asm_out_file = NULL;
1965 static void
1966 darwin_asm_dwarf_section (const char *name, unsigned int flags, tree decl);
1968 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
1970 void
1971 darwin_asm_named_section (const char *name,
1972 unsigned int flags,
1973 tree decl ATTRIBUTE_UNUSED)
1975 /* LTO sections go in a special section that encapsulates the (unlimited)
1976 number of GNU LTO sections within a single mach-o one. */
1977 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1978 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1980 darwin_lto_section_e e;
1981 /* We expect certain flags to be set... */
1982 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1983 == (SECTION_DEBUG | SECTION_NAMED));
1985 /* Switch to our combined section. */
1986 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1987 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1988 /* Output a label for the start of this sub-section. */
1989 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1990 lto_section_num, name);
1991 /* We have to jump through hoops to get the values of the intra-section
1992 offsets... */
1993 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1994 lto_section_num, lto_section_num);
1995 fprintf (asm_out_file,
1996 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1997 lto_section_num, lto_section_num+1, lto_section_num);
1998 lto_section_num++;
1999 e.sectname = xstrdup (name);
2000 /* Keep the names, we'll need to make a table later.
2001 TODO: check that we do not revisit sections, that would break
2002 the assumption of how this is done. */
2003 if (lto_section_names == NULL)
2004 vec_alloc (lto_section_names, 16);
2005 vec_safe_push (lto_section_names, e);
2007 else if (strncmp (name, "__DWARF,", 8) == 0)
2008 darwin_asm_dwarf_section (name, flags, decl);
2009 else
2010 fprintf (asm_out_file, "\t.section %s\n", name);
2013 void
2014 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
2016 /* Darwin does not use unique sections. */
2019 /* Handle __attribute__ ((apple_kext_compatibility)).
2020 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
2021 vtable for classes with this attribute (and their descendants) by not
2022 outputting the new 3.0 nondeleting destructor. This means that such
2023 objects CANNOT be allocated on the stack or as globals UNLESS they have
2024 a completely empty `operator delete'.
2025 Luckily, this fits in with the Darwin kext model.
2027 This attribute also disables gcc3's potential overlaying of derived
2028 class data members on the padding at the end of the base class. */
2030 tree
2031 darwin_handle_kext_attribute (tree *node, tree name,
2032 tree args ATTRIBUTE_UNUSED,
2033 int flags ATTRIBUTE_UNUSED,
2034 bool *no_add_attrs)
2036 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
2037 if (! TARGET_KEXTABI)
2039 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2040 "only when compiling a kext", name);
2042 *no_add_attrs = true;
2044 else if (TREE_CODE (*node) != RECORD_TYPE)
2046 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2047 "only to C++ classes", name);
2049 *no_add_attrs = true;
2052 return NULL_TREE;
2055 /* Handle a "weak_import" attribute; arguments as in
2056 struct attribute_spec.handler. */
2058 tree
2059 darwin_handle_weak_import_attribute (tree *node, tree name,
2060 tree ARG_UNUSED (args),
2061 int ARG_UNUSED (flags),
2062 bool * no_add_attrs)
2064 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
2066 warning (OPT_Wattributes, "%qE attribute ignored",
2067 name);
2068 *no_add_attrs = true;
2070 else
2071 declare_weak (*node);
2073 return NULL_TREE;
2076 /* Emit a label for an FDE, making it global and/or weak if appropriate.
2077 The third parameter is nonzero if this is for exception handling.
2078 The fourth parameter is nonzero if this is just a placeholder for an
2079 FDE that we are omitting. */
2081 void
2082 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
2084 char *lab ;
2085 char buf[32];
2086 static int invok_count = 0;
2087 static tree last_fun_decl = NULL_TREE;
2089 /* We use the linker to emit the .eh labels for Darwin 9 and above. */
2090 if (! for_eh || generating_for_darwin_version >= 9)
2091 return;
2093 /* FIXME: This only works when the eh for all sections of a function is
2094 emitted at the same time. If that changes, we would need to use a lookup
2095 table of some form to determine what to do. Also, we should emit the
2096 unadorned label for the partition containing the public label for a
2097 function. This is of limited use, probably, since we do not currently
2098 enable partitioning. */
2099 strcpy (buf, ".eh");
2100 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
2102 if (decl == last_fun_decl)
2104 invok_count++;
2105 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
2107 else
2109 last_fun_decl = decl;
2110 invok_count = 0;
2114 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
2116 if (TREE_PUBLIC (decl))
2118 targetm.asm_out.globalize_label (file, lab);
2119 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
2121 fputs ("\t.private_extern ", file);
2122 assemble_name (file, lab);
2123 fputc ('\n', file);
2127 if (DECL_WEAK (decl))
2129 fputs ("\t.weak_definition ", file);
2130 assemble_name (file, lab);
2131 fputc ('\n', file);
2134 assemble_name (file, lab);
2135 if (empty)
2137 fputs (" = 0\n", file);
2139 /* Mark the absolute .eh and .eh1 style labels as needed to
2140 ensure that we don't dead code strip them and keep such
2141 labels from another instantiation point until we can fix this
2142 properly with group comdat support. */
2143 darwin_mark_decl_preserved (lab);
2145 else
2146 fputs (":\n", file);
2148 free (lab);
2151 static GTY(()) unsigned long except_table_label_num;
2153 void
2154 darwin_emit_except_table_label (FILE *file)
2156 char section_start_label[30];
2158 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2159 except_table_label_num++);
2160 ASM_OUTPUT_LABEL (file, section_start_label);
2162 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2164 void
2165 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2167 const char *nlp_name;
2169 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2171 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2172 fputs ("\t.long\t", file);
2173 ASM_OUTPUT_LABELREF (file, nlp_name);
2174 fputs ("-.", file);
2177 /* If this is uncommented, details of each allocation will be printed
2178 in the asm right before the actual code. WARNING - this will cause some
2179 test-suite fails (since the printout will contain items that some tests
2180 are not expecting) -- so don't leave it on by default (it bloats the
2181 asm too). */
2182 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2184 /* The first two of these routines are ostensibly just intended to put
2185 names into the asm. However, they are both hijacked in order to ensure
2186 that zero-sized items do not make their way into the output. Consequently,
2187 we also need to make these participate in provisions for dealing with
2188 such items in section anchors. */
2190 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2191 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2192 but it does need to be referenced via indirect PIC data pointers.
2193 The machopic_define_symbol calls are telling the machopic subsystem
2194 that the name *is* defined in this module, so it doesn't need to
2195 make them indirect. */
2196 void
2197 darwin_asm_declare_object_name (FILE *file,
2198 const char *nam, tree decl)
2200 const char *xname = nam;
2201 unsigned HOST_WIDE_INT size;
2202 bool local_def, weak;
2204 weak = (DECL_P (decl)
2205 && DECL_WEAK (decl)
2206 && !lookup_attribute ("weak_import",
2207 DECL_ATTRIBUTES (decl)));
2209 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2210 && (!DECL_COMMON (decl)
2211 || !TREE_PUBLIC (decl)));
2213 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2214 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2216 if (local_def)
2218 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2219 if (!weak)
2220 machopic_define_symbol (DECL_RTL (decl));
2223 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2225 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2226 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2227 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2228 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2229 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2230 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2231 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2232 (unsigned long)DECL_INITIAL (decl));
2233 #endif
2235 /* Darwin needs help to support local zero-sized objects.
2236 They must be made at least one byte, and the section containing must be
2237 marked as unsuitable for section-anchors (see storage allocators below).
2239 For non-zero objects this output is handled by varasm.c.
2241 if (!size)
2243 unsigned int l2align = 0;
2245 /* The align must be honored, even for zero-sized. */
2246 if (DECL_ALIGN (decl))
2248 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2249 fprintf (file, "\t.align\t%u\n", l2align);
2252 ASM_OUTPUT_LABEL (file, xname);
2253 size = 1;
2254 fprintf (file, "\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2256 /* Check that we've correctly picked up the zero-sized item and placed it
2257 properly. */
2258 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2259 || (in_section
2260 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2262 else
2263 ASM_OUTPUT_LABEL (file, xname);
2266 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2267 void
2268 darwin_asm_declare_constant_name (FILE *file, const char *name,
2269 const_tree exp ATTRIBUTE_UNUSED,
2270 HOST_WIDE_INT size)
2272 assemble_label (file, name);
2273 /* As for other items, we need at least one byte. */
2274 if (!size)
2276 fputs ("\t.space\t1\n", file);
2277 /* Check that we've correctly picked up the zero-sized item and placed it
2278 properly. */
2279 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2280 || (in_section
2281 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2285 /* Darwin storage allocators.
2287 Zerofill sections are desirable for large blank data since, otherwise, these
2288 data bloat objects (PR33210).
2290 However, section anchors don't work in .zerofill sections (one cannot switch
2291 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2292 to put (at least some) data into 'normal' switchable sections.
2294 Here we set a relatively arbitrary value for the size of an object to trigger
2295 zerofill when section anchors are enabled (anything bigger than a page for
2296 current Darwin implementations). FIXME: there ought to be some objective way
2297 to make this choice.
2299 When section anchor are off this is ignored anyway. */
2301 #define BYTES_ZFILL 4096
2303 /* Emit a chunk of data for items coalesced by the linker. */
2304 static void
2305 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2306 unsigned HOST_WIDE_INT size,
2307 unsigned int align)
2309 /* Since the sections used here are coalesed, they will not be eligible
2310 for section anchors, and therefore we don't need to break that out. */
2311 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2312 switch_to_section (darwin_sections[const_data_coal_section]);
2313 else
2314 switch_to_section (darwin_sections[data_coal_section]);
2316 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2317 the align info for zero-sized items... but do it here otherwise. */
2318 if (size && align)
2319 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2321 if (TREE_PUBLIC (decl))
2322 darwin_globalize_label (fp, name);
2324 /* ... and we let it deal with outputting one byte of zero for them too. */
2325 darwin_asm_declare_object_name (fp, name, decl);
2326 if (size)
2327 assemble_zeros (size);
2330 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2331 static void
2332 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2333 unsigned HOST_WIDE_INT size,
2334 unsigned int align, tree meta)
2336 section *ocs = data_section;
2338 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2339 ocs = darwin_objc2_section (decl, meta, ocs);
2340 else
2341 ocs = darwin_objc1_section (decl, meta, ocs);
2343 switch_to_section (ocs);
2345 /* We shall declare that zero-sized meta-data are not valid (yet). */
2346 gcc_assert (size);
2347 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2349 /* ... and we let it deal with outputting one byte of zero for them too. */
2350 darwin_asm_declare_object_name (fp, name, decl);
2351 assemble_zeros (size);
2354 /* This routine emits 'local' storage:
2356 When Section Anchors are off this routine emits .zerofill commands in
2357 sections named for their alignment.
2359 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2360 the .static_data section so that the section anchoring system can see them.
2361 Larger items are still placed in .zerofill sections, addressing PR33210.
2362 The routine has no checking - it is all assumed to be done by the caller.
2364 static void
2365 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2366 unsigned HOST_WIDE_INT size,
2367 unsigned int l2align)
2369 /* FIXME: We have a fudge to make this work with Java even when the target does
2370 not use sections anchors -- Java seems to need at least one small item in a
2371 non-zerofill segment. */
2372 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2373 || (size && size <= 2))
2375 /* Put smaller objects in _static_data, where the section anchors system
2376 can get them.
2377 However, if they are zero-sized punt them to yet a different section
2378 (that is not allowed to participate in anchoring). */
2379 if (!size)
2381 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2382 in_section = darwin_sections[zobj_bss_section];
2383 size = 1;
2385 else
2387 fputs ("\t.static_data\n", fp);
2388 in_section = darwin_sections[static_data_section];
2391 if (l2align)
2392 fprintf (fp, "\t.align\t%u\n", l2align);
2394 assemble_name (fp, name);
2395 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2397 else
2399 /* When we are on a non-section anchor target, we can get zero-sized
2400 items here. However, all we need to do is to bump them to one byte
2401 and the section alignment will take care of the rest. */
2402 char secnam[64];
2403 unsigned int flags ;
2404 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
2405 (unsigned) l2align);
2406 /* We can't anchor (yet, if ever) in zerofill sections, because we can't
2407 switch to them and emit a label. */
2408 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2409 in_section = get_section (secnam, flags, NULL);
2410 fprintf (fp, "\t.zerofill %s,", secnam);
2411 assemble_name (fp, name);
2412 if (!size)
2413 size = 1;
2415 if (l2align)
2416 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2417 size, (unsigned) l2align);
2418 else
2419 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2422 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2423 /* This is defined as a file-scope var, so we know to notify machopic. */
2424 machopic_define_symbol (DECL_RTL (decl));
2427 /* Emit a chunk of common. */
2428 static void
2429 darwin_emit_common (FILE *fp, const char *name,
2430 unsigned HOST_WIDE_INT size, unsigned int align)
2432 unsigned HOST_WIDE_INT rounded;
2433 unsigned int l2align;
2435 /* Earlier systems complain if the alignment exceeds the page size.
2436 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2437 if (!emit_aligned_common && (align > 32768UL))
2438 align = 4096UL; /* In units. */
2439 else
2440 align /= BITS_PER_UNIT;
2442 /* Make sure we have a meaningful align. */
2443 if (!align)
2444 align = 1;
2446 /* For earlier toolchains, we need to emit the var as a rounded size to
2447 tell ld the alignment. */
2448 if (size < align)
2449 rounded = align;
2450 else
2451 rounded = (size + (align-1)) & ~(align-1);
2453 l2align = floor_log2 (align);
2454 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2456 in_section = comm_section;
2457 /* We mustn't allow multiple public symbols to share an address when using
2458 the normal OSX toolchain. */
2459 if (!size)
2461 /* Put at least one byte. */
2462 size = 1;
2463 /* This section can no longer participate in section anchoring. */
2464 comm_section->common.flags |= SECTION_NO_ANCHOR;
2467 fputs ("\t.comm\t", fp);
2468 assemble_name (fp, name);
2469 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2470 emit_aligned_common?size:rounded);
2471 if (l2align && emit_aligned_common)
2472 fprintf (fp, ",%u", l2align);
2473 fputs ("\n", fp);
2476 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2477 or coalescable data sections (for weak or comdat) as appropriate. */
2479 void
2480 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2481 unsigned HOST_WIDE_INT size, unsigned int align)
2483 unsigned int l2align;
2484 bool one, pub, weak;
2485 tree meta;
2487 pub = TREE_PUBLIC (decl);
2488 one = DECL_ONE_ONLY (decl);
2489 weak = (DECL_P (decl)
2490 && DECL_WEAK (decl)
2491 && !lookup_attribute ("weak_import",
2492 DECL_ATTRIBUTES (decl)));
2494 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2495 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2496 " pub %d weak %d one %d init %lx\n",
2497 name, (long long)size, (int)align, TREE_READONLY (decl),
2498 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2499 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2500 #endif
2502 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2503 before the target has a chance to comment. */
2504 if ((meta = is_objc_metadata (decl)))
2506 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2507 return;
2510 /* Check that any initializer is valid. */
2511 gcc_assert ((DECL_INITIAL (decl) == NULL)
2512 || (DECL_INITIAL (decl) == error_mark_node)
2513 || initializer_zerop (DECL_INITIAL (decl)));
2515 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2516 gcc_assert (!DECL_COMMON (decl));
2518 /* Pick up the correct alignment. */
2519 if (!size || !align)
2520 align = DECL_ALIGN (decl);
2522 l2align = floor_log2 (align / BITS_PER_UNIT);
2523 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2525 last_assemble_variable_decl = decl;
2527 /* We would rather not have to check this here - but it seems that we might
2528 be passed a decl that should be in coalesced space. */
2529 if (one || weak)
2531 /* Weak or COMDAT objects are put in mergeable sections. */
2532 darwin_emit_weak_or_comdat (fp, decl, name, size,
2533 DECL_ALIGN (decl));
2534 return;
2537 /* If this is not public, then emit according to local rules. */
2538 if (!pub)
2540 darwin_emit_local_bss (fp, decl, name, size, l2align);
2541 return;
2544 /* So we have a public symbol (small item fudge for Java, see above). */
2545 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2546 || (size && size <= 2))
2548 /* Put smaller objects in data, where the section anchors system can get
2549 them. However, if they are zero-sized punt them to yet a different
2550 section (that is not allowed to participate in anchoring). */
2551 if (!size)
2553 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2554 in_section = darwin_sections[zobj_data_section];
2555 size = 1;
2557 else
2559 fputs ("\t.data\n", fp);
2560 in_section = data_section;
2563 if (l2align)
2564 fprintf (fp, "\t.align\t%u\n", l2align);
2566 assemble_name (fp, name);
2567 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2569 else
2571 char secnam[64];
2572 unsigned int flags ;
2573 /* When we are on a non-section anchor target, we can get zero-sized
2574 items here. However, all we need to do is to bump them to one byte
2575 and the section alignment will take care of the rest. */
2576 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
2578 /* We can't anchor in zerofill sections, because we can't switch
2579 to them and emit a label. */
2580 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2581 in_section = get_section (secnam, flags, NULL);
2582 fprintf (fp, "\t.zerofill %s,", secnam);
2583 assemble_name (fp, name);
2584 if (!size)
2585 size = 1;
2587 if (l2align)
2588 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2589 else
2590 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2592 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2595 /* Output a chunk of common, with alignment specified (where the target
2596 supports this). */
2597 void
2598 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2599 unsigned HOST_WIDE_INT size,
2600 unsigned int align)
2602 unsigned int l2align;
2603 bool one, weak;
2604 tree meta;
2606 /* No corresponding var. */
2607 if (decl==NULL)
2609 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2610 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2611 #endif
2612 darwin_emit_common (fp, name, size, align);
2613 return;
2616 one = DECL_ONE_ONLY (decl);
2617 weak = (DECL_P (decl)
2618 && DECL_WEAK (decl)
2619 && !lookup_attribute ("weak_import",
2620 DECL_ATTRIBUTES (decl)));
2622 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2623 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2624 " weak %d one %d init %lx\n",
2625 name, (long long)size, (int)align, TREE_READONLY (decl),
2626 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2627 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2628 #endif
2630 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2631 before the target has a chance to comment. */
2632 if ((meta = is_objc_metadata (decl)))
2634 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2635 return;
2638 /* We shouldn't be messing with this if the decl has a section name. */
2639 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2641 /* We would rather not have to check this here - but it seems that we might
2642 be passed a decl that should be in coalesced space. */
2643 if (one || weak)
2645 /* Weak or COMDAT objects are put in mergable sections. */
2646 darwin_emit_weak_or_comdat (fp, decl, name, size,
2647 DECL_ALIGN (decl));
2648 return;
2651 /* We should only get here for DECL_COMMON, with a zero init (and, in
2652 principle, only for public symbols too - although we deal with local
2653 ones below). */
2655 /* Check the initializer is OK. */
2656 gcc_assert (DECL_COMMON (decl)
2657 && ((DECL_INITIAL (decl) == NULL)
2658 || (DECL_INITIAL (decl) == error_mark_node)
2659 || initializer_zerop (DECL_INITIAL (decl))));
2661 last_assemble_variable_decl = decl;
2663 if (!size || !align)
2664 align = DECL_ALIGN (decl);
2666 l2align = floor_log2 (align / BITS_PER_UNIT);
2667 /* Check we aren't asking for more aligment than the platform allows. */
2668 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2670 if (TREE_PUBLIC (decl) != 0)
2671 darwin_emit_common (fp, name, size, align);
2672 else
2673 darwin_emit_local_bss (fp, decl, name, size, l2align);
2676 /* Output a chunk of BSS with alignment specfied. */
2677 void
2678 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2679 unsigned HOST_WIDE_INT size,
2680 unsigned int align)
2682 unsigned long l2align;
2683 bool one, weak;
2684 tree meta;
2686 one = DECL_ONE_ONLY (decl);
2687 weak = (DECL_P (decl)
2688 && DECL_WEAK (decl)
2689 && !lookup_attribute ("weak_import",
2690 DECL_ATTRIBUTES (decl)));
2692 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2693 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2694 " weak %d init %lx\n",
2695 name, (long long)size, (int)align, TREE_READONLY (decl),
2696 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2697 weak , (unsigned long)DECL_INITIAL (decl));
2698 #endif
2700 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2701 before the target has a chance to comment. */
2702 if ((meta = is_objc_metadata (decl)))
2704 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2705 return;
2708 /* We shouldn't be messing with this if the decl has a section name. */
2709 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2711 /* We would rather not have to check this here - but it seems that we might
2712 be passed a decl that should be in coalesced space. */
2713 if (one || weak)
2715 /* Weak or COMDAT objects are put in mergable sections. */
2716 darwin_emit_weak_or_comdat (fp, decl, name, size,
2717 DECL_ALIGN (decl));
2718 return;
2721 /* .. and it should be suitable for placement in local mem. */
2722 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2723 /* .. and any initializer must be all-zero. */
2724 gcc_assert ((DECL_INITIAL (decl) == NULL)
2725 || (DECL_INITIAL (decl) == error_mark_node)
2726 || initializer_zerop (DECL_INITIAL (decl)));
2728 last_assemble_variable_decl = decl;
2730 if (!size || !align)
2731 align = DECL_ALIGN (decl);
2733 l2align = floor_log2 (align / BITS_PER_UNIT);
2734 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2736 darwin_emit_local_bss (fp, decl, name, size, l2align);
2739 /* Emit an assembler directive to set visibility for a symbol. The
2740 only supported visibilities are VISIBILITY_DEFAULT and
2741 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2742 extern". There is no MACH-O equivalent of ELF's
2743 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2745 void
2746 darwin_assemble_visibility (tree decl, int vis)
2748 if (vis == VISIBILITY_DEFAULT)
2750 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2752 fputs ("\t.private_extern ", asm_out_file);
2753 assemble_name (asm_out_file,
2754 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2755 fputs ("\n", asm_out_file);
2757 else
2758 warning (OPT_Wattributes, "protected visibility attribute "
2759 "not supported in this configuration; ignored");
2762 /* vec used by darwin_asm_dwarf_section.
2763 Maybe a hash tab would be better here - but the intention is that this is
2764 a very short list (fewer than 16 items) and each entry should (ideally,
2765 eventually) only be presented once.
2767 A structure to hold a dwarf debug section used entry. */
2769 typedef struct GTY(()) dwarf_sect_used_entry {
2770 const char *name;
2771 unsigned count;
2773 dwarf_sect_used_entry;
2776 /* A list of used __DWARF sections. */
2777 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2779 /* This is called when we are asked to assemble a named section and the
2780 name begins with __DWARF,. We keep a list of the section names (without
2781 the __DWARF, prefix) and use this to emit our required start label on the
2782 first switch to each section. */
2784 static void
2785 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2786 tree ARG_UNUSED (decl))
2788 unsigned i;
2789 int namelen;
2790 const char * sname;
2791 dwarf_sect_used_entry *ref;
2792 bool found = false;
2793 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2794 == (SECTION_DEBUG | SECTION_NAMED));
2795 /* We know that the name starts with __DWARF, */
2796 sname = name + 8;
2797 namelen = strchr (sname, ',') - sname;
2798 gcc_assert (namelen);
2799 if (dwarf_sect_names_table == NULL)
2800 vec_alloc (dwarf_sect_names_table, 16);
2801 else
2802 for (i = 0;
2803 dwarf_sect_names_table->iterate (i, &ref);
2804 i++)
2806 if (!ref)
2807 break;
2808 if (!strcmp (ref->name, sname))
2810 found = true;
2811 ref->count++;
2812 break;
2816 fprintf (asm_out_file, "\t.section %s\n", name);
2817 if (!found)
2819 dwarf_sect_used_entry e;
2820 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2821 e.count = 1;
2822 e.name = xstrdup (sname);
2823 vec_safe_push (dwarf_sect_names_table, e);
2827 /* Output a difference of two labels that will be an assembly time
2828 constant if the two labels are local. (.long lab1-lab2 will be
2829 very different if lab1 is at the boundary between two sections; it
2830 will be relocated according to the second section, not the first,
2831 so one ends up with a difference between labels in different
2832 sections, which is bad in the dwarf2 eh context for instance.) */
2834 static int darwin_dwarf_label_counter;
2836 void
2837 darwin_asm_output_dwarf_delta (FILE *file, int size,
2838 const char *lab1, const char *lab2)
2840 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2841 && lab2[0] == '*' && lab2[1] == 'L');
2842 const char *directive = (size == 8 ? ".quad" : ".long");
2844 if (islocaldiff)
2845 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2846 else
2847 fprintf (file, "\t%s\t", directive);
2849 assemble_name_raw (file, lab1);
2850 fprintf (file, "-");
2851 assemble_name_raw (file, lab2);
2852 if (islocaldiff)
2853 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2856 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2857 offsets are not represented using relocs in .o files; either the
2858 section never leaves the .o file, or the linker or other tool is
2859 responsible for parsing the DWARF and updating the offsets. */
2861 void
2862 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2863 section *base)
2865 char sname[64];
2866 int namelen;
2868 gcc_assert (base->common.flags & SECTION_NAMED);
2869 gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
2870 gcc_assert (strchr (base->named.name + 8, ','));
2872 namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
2873 sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
2874 darwin_asm_output_dwarf_delta (file, size, lab, sname);
2877 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2879 void
2880 darwin_file_start (void)
2882 /* Nothing to do. */
2885 /* Called for the TARGET_ASM_FILE_END hook.
2886 Emit the mach-o pic indirection data, the lto data and, finally a flag
2887 to tell the linker that it can break the file object into sections and
2888 move those around for efficiency. */
2890 void
2891 darwin_file_end (void)
2893 if (!vec_safe_is_empty (ctors))
2894 finalize_ctors ();
2895 if (!vec_safe_is_empty (dtors))
2896 finalize_dtors ();
2898 /* If we are expecting to output NeXT ObjC meta-data, (and we actually see
2899 some) then we output the fix-and-continue marker (Image Info).
2900 This applies to Objective C, Objective C++ and LTO with either language
2901 as part of the input. */
2902 if (flag_next_runtime && objc_metadata_seen)
2904 unsigned int flags = 0;
2905 if (flag_objc_abi >= 2)
2907 flags = 16;
2908 output_section_asm_op
2909 (darwin_sections[objc2_image_info_section]->unnamed.data);
2911 else
2912 output_section_asm_op
2913 (darwin_sections[objc_image_info_section]->unnamed.data);
2915 ASM_OUTPUT_ALIGN (asm_out_file, 2);
2916 fputs ("L_OBJC_ImageInfo:\n", asm_out_file);
2918 flags |= (flag_replace_objc_classes && classes_seen) ? 1 : 0;
2919 flags |= flag_objc_gc ? 2 : 0;
2921 fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags);
2924 machopic_finish (asm_out_file);
2925 if (lang_GNU_CXX ())
2927 switch_to_section (darwin_sections[constructor_section]);
2928 switch_to_section (darwin_sections[destructor_section]);
2929 ASM_OUTPUT_ALIGN (asm_out_file, 1);
2932 /* If there was LTO assembler output, append it to asm_out_file. */
2933 if (lto_asm_out_name)
2935 int n;
2936 char *buf, *lto_asm_txt;
2938 /* Shouldn't be here if we failed to switch back. */
2939 gcc_assert (! saved_asm_out_file);
2941 lto_asm_out_file = fopen (lto_asm_out_name, "r");
2942 if (lto_asm_out_file == NULL)
2943 fatal_error (input_location,
2944 "failed to open temporary file %s with LTO output",
2945 lto_asm_out_name);
2946 fseek (lto_asm_out_file, 0, SEEK_END);
2947 n = ftell (lto_asm_out_file);
2948 if (n > 0)
2950 fseek (lto_asm_out_file, 0, SEEK_SET);
2951 lto_asm_txt = buf = (char *) xmalloc (n + 1);
2952 while (fgets (lto_asm_txt, n, lto_asm_out_file))
2953 fputs (lto_asm_txt, asm_out_file);
2954 /* Put a termination label. */
2955 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2956 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2957 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2958 lto_section_num);
2959 /* Make sure our termination label stays in this section. */
2960 fputs ("\t.space\t1\n", asm_out_file);
2963 /* Remove the temporary file. */
2964 fclose (lto_asm_out_file);
2965 unlink_if_ordinary (lto_asm_out_name);
2966 free (lto_asm_out_name);
2969 /* Output the names and indices. */
2970 if (lto_section_names && lto_section_names->length ())
2972 int count;
2973 darwin_lto_section_e *ref;
2974 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
2975 the latter up ourselves. */
2976 const char *op = integer_asm_op (4,0);
2978 /* Emit the names. */
2979 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2980 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2981 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2983 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
2984 /* We have to jump through hoops to get the values of the intra-section
2985 offsets... */
2986 fprintf (asm_out_file,
2987 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
2988 count, count);
2989 fprintf (asm_out_file,
2990 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
2991 count, count+1, count);
2992 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
2994 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
2995 /* make sure our termination label stays in this section. */
2996 fputs ("\t.space\t1\n", asm_out_file);
2998 /* Emit the Index. */
2999 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
3000 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
3001 fputs ("\t.align\t2\n", asm_out_file);
3002 fputs ("# Section offset, Section length, Name offset, Name length\n",
3003 asm_out_file);
3004 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
3006 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
3007 op, count, ref->sectname);
3008 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
3009 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
3010 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
3014 /* If we have section anchors, then we must prevent the linker from
3015 re-arranging data. */
3016 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
3017 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
3020 /* TODO: Add a language hook for identifying if a decl is a vtable. */
3021 #define DARWIN_VTABLE_P(DECL) 0
3023 /* Cross-module name binding. Darwin does not support overriding
3024 functions at dynamic-link time, except for vtables in kexts. */
3026 bool
3027 darwin_binds_local_p (const_tree decl)
3029 return default_binds_local_p_1 (decl,
3030 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
3033 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
3034 anchor relative to ".", the current section position. We cannot use
3035 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
3036 void
3037 darwin_asm_output_anchor (rtx symbol)
3039 fprintf (asm_out_file, "\t.set\t");
3040 assemble_name (asm_out_file, XSTR (symbol, 0));
3041 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
3042 SYMBOL_REF_BLOCK_OFFSET (symbol));
3045 /* Disable section anchoring on any section containing a zero-sized
3046 object. */
3047 bool
3048 darwin_use_anchors_for_symbol_p (const_rtx symbol)
3050 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
3052 section *sect;
3053 /* If the section contains a zero-sized object it's ineligible. */
3054 sect = SYMBOL_REF_BLOCK (symbol)->sect;
3055 /* This should have the effect of disabling anchors for vars that follow
3056 any zero-sized one, in a given section. */
3057 if (sect->common.flags & SECTION_NO_ANCHOR)
3058 return false;
3060 /* Also check the normal reasons for suppressing. */
3061 return default_use_anchors_for_symbol_p (symbol);
3063 else
3064 return false;
3067 /* Set the darwin specific attributes on TYPE. */
3068 void
3069 darwin_set_default_type_attributes (tree type)
3071 if (darwin_ms_struct
3072 && TREE_CODE (type) == RECORD_TYPE)
3073 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
3074 NULL_TREE,
3075 TYPE_ATTRIBUTES (type));
3078 /* True, iff we're generating code for loadable kernel extensions. */
3080 bool
3081 darwin_kextabi_p (void) {
3082 return flag_apple_kext;
3085 void
3086 darwin_override_options (void)
3088 /* Keep track of which (major) version we're generating code for. */
3089 if (darwin_macosx_version_min)
3091 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
3092 generating_for_darwin_version = 10;
3093 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
3094 generating_for_darwin_version = 9;
3096 /* Earlier versions are not specifically accounted, until required. */
3099 /* In principle, this should be c-family only. However, we really need to
3100 set sensible defaults for LTO as well, since the section selection stuff
3101 should check for correctness re. the ABI. TODO: check and provide the
3102 flags (runtime & ABI) from the lto wrapper). */
3104 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
3105 if (!global_options_set.x_flag_objc_abi)
3106 global_options.x_flag_objc_abi
3107 = (!flag_next_runtime)
3109 : (TARGET_64BIT ? 2
3110 : (generating_for_darwin_version >= 9) ? 1
3111 : 0);
3113 /* Objective-C family ABI 2 is only valid for next/m64 at present. */
3114 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
3116 if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
3117 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
3118 " used for %<-m64%> targets with"
3119 " %<-fnext-runtime%>");
3120 if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
3121 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
3122 " supported on %<-m32%> targets with"
3123 " %<-fnext-runtime%>");
3126 /* Don't emit DWARF3/4 unless specifically selected. This is a
3127 workaround for tool bugs. */
3128 if (!global_options_set.x_dwarf_strict)
3129 dwarf_strict = 1;
3130 if (!global_options_set.x_dwarf_version)
3131 dwarf_version = 2;
3133 /* Do not allow unwind tables to be generated by default for m32.
3134 fnon-call-exceptions will override this, regardless of what we do. */
3135 if (generating_for_darwin_version < 10
3136 && !global_options_set.x_flag_asynchronous_unwind_tables
3137 && !TARGET_64BIT)
3138 global_options.x_flag_asynchronous_unwind_tables = 0;
3140 /* Disable -freorder-blocks-and-partition when unwind tables are being
3141 emitted for Darwin < 9 (OSX 10.5).
3142 The strategy is, "Unless the User has specifically set/unset an unwind
3143 flag we will switch off -freorder-blocks-and-partition when unwind tables
3144 will be generated". If the User specifically sets flags... we assume
3145 (s)he knows why... */
3146 if (generating_for_darwin_version < 9
3147 && global_options_set.x_flag_reorder_blocks_and_partition
3148 && ((global_options.x_flag_exceptions /* User, c++, java */
3149 && !global_options_set.x_flag_exceptions) /* User specified... */
3150 || (global_options.x_flag_unwind_tables
3151 && !global_options_set.x_flag_unwind_tables)
3152 || (global_options.x_flag_non_call_exceptions
3153 && !global_options_set.x_flag_non_call_exceptions)
3154 || (global_options.x_flag_asynchronous_unwind_tables
3155 && !global_options_set.x_flag_asynchronous_unwind_tables)))
3157 inform (input_location,
3158 "-freorder-blocks-and-partition does not work with exceptions "
3159 "on this architecture");
3160 flag_reorder_blocks_and_partition = 0;
3161 flag_reorder_blocks = 1;
3164 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3165 one valid choice of exception scheme for each runtime. */
3166 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3167 global_options.x_flag_objc_sjlj_exceptions =
3168 flag_next_runtime && !TARGET_64BIT;
3170 /* FIXME: and this could be eliminated then too. */
3171 if (!global_options_set.x_flag_exceptions
3172 && flag_objc_exceptions
3173 && TARGET_64BIT)
3174 flag_exceptions = 1;
3176 if (flag_mkernel || flag_apple_kext)
3178 /* -mkernel implies -fapple-kext for C++ */
3179 if (lang_GNU_CXX ())
3180 flag_apple_kext = 1;
3182 flag_no_common = 1;
3184 /* No EH in kexts. */
3185 flag_exceptions = 0;
3186 /* No -fnon-call-exceptions data in kexts. */
3187 flag_non_call_exceptions = 0;
3188 /* so no tables either.. */
3189 flag_unwind_tables = 0;
3190 flag_asynchronous_unwind_tables = 0;
3191 /* We still need to emit branch islands for kernel context. */
3192 darwin_emit_branch_islands = true;
3195 if (flag_var_tracking_uninit == 0
3196 && generating_for_darwin_version >= 9
3197 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3198 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3199 && write_symbols == DWARF2_DEBUG)
3200 flag_var_tracking_uninit = flag_var_tracking;
3202 if (MACHO_DYNAMIC_NO_PIC_P)
3204 if (flag_pic)
3205 warning_at (UNKNOWN_LOCATION, 0,
3206 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3207 " %<-fpie%> or %<-fPIE%>");
3208 flag_pic = 0;
3210 else if (flag_pic == 1)
3212 /* Darwin's -fpic is -fPIC. */
3213 flag_pic = 2;
3216 /* It is assumed that branch island stubs are needed for earlier systems. */
3217 if (generating_for_darwin_version < 9)
3218 darwin_emit_branch_islands = true;
3219 else
3220 emit_aligned_common = true; /* Later systems can support aligned common. */
3222 /* The c_dialect...() macros are not available to us here. */
3223 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3226 #if DARWIN_PPC
3227 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3229 static void
3230 darwin_patch_builtin (enum built_in_function fncode)
3232 tree fn = builtin_decl_explicit (fncode);
3233 tree sym;
3234 char *newname;
3236 if (!fn)
3237 return;
3239 sym = DECL_ASSEMBLER_NAME (fn);
3240 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3242 set_user_assembler_name (fn, newname);
3244 fn = builtin_decl_implicit (fncode);
3245 if (fn)
3246 set_user_assembler_name (fn, newname);
3249 void
3250 darwin_patch_builtins (void)
3252 if (LONG_DOUBLE_TYPE_SIZE != 128)
3253 return;
3255 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3256 #define PATCH_BUILTIN_NO64(fncode) \
3257 if (!TARGET_64BIT) \
3258 darwin_patch_builtin (fncode);
3259 #define PATCH_BUILTIN_VARIADIC(fncode) \
3260 if (!TARGET_64BIT \
3261 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3262 darwin_patch_builtin (fncode);
3263 #include "darwin-ppc-ldouble-patch.def"
3264 #undef PATCH_BUILTIN
3265 #undef PATCH_BUILTIN_NO64
3266 #undef PATCH_BUILTIN_VARIADIC
3268 #endif
3270 /* CFStrings implementation. */
3271 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3272 static GTY(()) tree cfstring_type_node = NULL_TREE;
3273 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3274 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3275 static GTY(()) tree pcint_type_node = NULL_TREE;
3276 static GTY(()) tree pcchar_type_node = NULL_TREE;
3278 static enum built_in_function darwin_builtin_cfstring;
3280 /* Store all constructed constant CFStrings in a hash table so that
3281 they get uniqued properly. */
3283 typedef struct GTY ((for_user)) cfstring_descriptor {
3284 /* The string literal. */
3285 tree literal;
3286 /* The resulting constant CFString. */
3287 tree constructor;
3288 } cfstring_descriptor;
3290 struct cfstring_hasher : ggc_hasher<cfstring_descriptor *>
3292 static hashval_t hash (cfstring_descriptor *);
3293 static bool equal (cfstring_descriptor *, cfstring_descriptor *);
3296 static GTY (()) hash_table<cfstring_hasher> *cfstring_htab;
3298 static tree
3299 add_builtin_field_decl (tree type, const char *name, tree **chain)
3301 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3302 get_identifier (name), type);
3304 if (*chain != NULL)
3305 **chain = field;
3306 *chain = &DECL_CHAIN (field);
3308 return field;
3311 tree
3312 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3314 tree cfsfun, fields, pccfstring_ftype_pcchar;
3315 tree *chain = NULL;
3317 darwin_builtin_cfstring =
3318 (enum built_in_function) builtin_cfstring;
3320 /* struct __builtin_CFString {
3321 const int *isa; (will point at
3322 int flags; __CFConstantStringClassReference)
3323 const char *str;
3324 long length;
3325 }; */
3327 pcint_type_node = build_pointer_type
3328 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3330 pcchar_type_node = build_pointer_type
3331 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3333 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3335 /* Have to build backwards for finish struct. */
3336 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3337 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3338 add_builtin_field_decl (integer_type_node, "flags", &chain);
3339 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3340 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3341 fields, NULL_TREE);
3343 /* const struct __builtin_CFstring *
3344 __builtin___CFStringMakeConstantString (const char *); */
3346 ccfstring_type_node = build_qualified_type
3347 (cfstring_type_node, TYPE_QUAL_CONST);
3348 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3349 pccfstring_ftype_pcchar = build_function_type_list
3350 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3352 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3353 get_identifier ("__builtin___CFStringMakeConstantString"),
3354 pccfstring_ftype_pcchar);
3356 TREE_PUBLIC (cfsfun) = 1;
3357 DECL_EXTERNAL (cfsfun) = 1;
3358 DECL_ARTIFICIAL (cfsfun) = 1;
3359 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3360 in place of the existing, which may be NULL. */
3361 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3362 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3363 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
3364 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
3365 lang_hooks.builtin_function (cfsfun);
3367 /* extern int __CFConstantStringClassReference[]; */
3368 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3369 get_identifier ("__CFConstantStringClassReference"),
3370 build_array_type (integer_type_node, NULL_TREE));
3372 TREE_PUBLIC (cfstring_class_reference) = 1;
3373 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3374 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3375 DECL_EXTERNAL (cfstring_class_reference) = 1;
3376 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3378 /* Initialize the hash table used to hold the constant CFString objects. */
3379 cfstring_htab = hash_table<cfstring_hasher>::create_ggc (31);
3381 return cfstring_type_node;
3384 tree
3385 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3386 bool ARG_UNUSED (ignore))
3388 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3390 if (fcode == darwin_builtin_cfstring)
3392 if (!darwin_constant_cfstrings)
3394 error ("built-in function %qD requires the"
3395 " %<-mconstant-cfstrings%> flag", fndecl);
3396 return error_mark_node;
3399 if (n_args != 1)
3401 error ("built-in function %qD takes one argument only", fndecl);
3402 return error_mark_node;
3405 return darwin_build_constant_cfstring (*argp);
3408 return NULL_TREE;
3411 void
3412 darwin_rename_builtins (void)
3414 /* The system ___divdc3 routine in libSystem on darwin10 is not
3415 accurate to 1ulp, ours is, so we avoid ever using the system name
3416 for this routine and instead install a non-conflicting name that
3417 is accurate.
3419 When -ffast-math or -funsafe-math-optimizations is given, we can
3420 use the faster version. */
3421 if (!flag_unsafe_math_optimizations)
3423 enum built_in_function dcode
3424 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3425 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3426 tree fn = builtin_decl_explicit (dcode);
3427 /* Fortran and c call TARGET_INIT_BUILTINS and
3428 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3429 call into each to ensure that at least one of them is called
3430 after build_common_builtin_nodes. A better fix is to add a
3431 new hook to run after build_common_builtin_nodes runs. */
3432 if (fn)
3433 set_user_assembler_name (fn, "___ieee_divdc3");
3434 fn = builtin_decl_implicit (dcode);
3435 if (fn)
3436 set_user_assembler_name (fn, "___ieee_divdc3");
3440 bool
3441 darwin_libc_has_function (enum function_class fn_class)
3443 if (fn_class == function_sincos)
3444 return false;
3445 if (fn_class == function_c99_math_complex
3446 || fn_class == function_c99_misc)
3447 return (TARGET_64BIT
3448 || strverscmp (darwin_macosx_version_min, "10.3") >= 0);
3450 return true;
3453 hashval_t
3454 cfstring_hasher::hash (cfstring_descriptor *ptr)
3456 tree str = ptr->literal;
3457 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3458 int i, len = TREE_STRING_LENGTH (str);
3459 hashval_t h = len;
3461 for (i = 0; i < len; i++)
3462 h = ((h * 613) + p[i]);
3464 return h;
3467 bool
3468 cfstring_hasher::equal (cfstring_descriptor *ptr1, cfstring_descriptor *ptr2)
3470 tree str1 = ptr1->literal;
3471 tree str2 = ptr2->literal;
3472 int len1 = TREE_STRING_LENGTH (str1);
3474 return (len1 == TREE_STRING_LENGTH (str2)
3475 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3476 len1));
3479 tree
3480 darwin_build_constant_cfstring (tree str)
3482 struct cfstring_descriptor *desc, key;
3483 tree addr;
3485 if (!str)
3487 error ("CFString literal is missing");
3488 return error_mark_node;
3491 STRIP_NOPS (str);
3493 if (TREE_CODE (str) == ADDR_EXPR)
3494 str = TREE_OPERAND (str, 0);
3496 if (TREE_CODE (str) != STRING_CST)
3498 error ("CFString literal expression is not a string constant");
3499 return error_mark_node;
3502 /* Perhaps we already constructed a constant CFString just like this one? */
3503 key.literal = str;
3504 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
3505 desc = *loc;
3507 if (!desc)
3509 tree var, constructor, field;
3510 vec<constructor_elt, va_gc> *v = NULL;
3511 int length = TREE_STRING_LENGTH (str) - 1;
3513 if (darwin_warn_nonportable_cfstrings)
3515 const char *s = TREE_STRING_POINTER (str);
3516 int l = 0;
3518 for (l = 0; l < length; l++)
3519 if (!s[l] || !isascii (s[l]))
3521 warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
3522 s[l] ? "non-ASCII character" : "embedded NUL");
3523 break;
3527 *loc = desc = ggc_cleared_alloc<cfstring_descriptor> ();
3528 desc->literal = str;
3530 /* isa *. */
3531 field = TYPE_FIELDS (ccfstring_type_node);
3532 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3533 build1 (ADDR_EXPR, TREE_TYPE (field),
3534 cfstring_class_reference));
3535 /* flags */
3536 field = DECL_CHAIN (field);
3537 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3538 build_int_cst (TREE_TYPE (field), 0x000007c8));
3539 /* string *. */
3540 field = DECL_CHAIN (field);
3541 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3542 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3543 /* length */
3544 field = DECL_CHAIN (field);
3545 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3546 build_int_cst (TREE_TYPE (field), length));
3548 constructor = build_constructor (ccfstring_type_node, v);
3549 TREE_READONLY (constructor) = 1;
3550 TREE_CONSTANT (constructor) = 1;
3551 TREE_STATIC (constructor) = 1;
3553 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3554 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3555 being built without any knowledge of C++ tree accessors; hence, we shall
3556 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3557 if (darwin_running_cxx)
3558 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3560 /* Create an anonymous global variable for this CFString. */
3561 var = build_decl (input_location, CONST_DECL,
3562 NULL, TREE_TYPE (constructor));
3563 DECL_ARTIFICIAL (var) = 1;
3564 TREE_STATIC (var) = 1;
3565 DECL_INITIAL (var) = constructor;
3566 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3567 DECL_CONTEXT (var) = NULL_TREE;
3568 desc->constructor = var;
3571 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3572 TREE_CONSTANT (addr) = 1;
3574 return addr;
3577 bool
3578 darwin_cfstring_p (tree str)
3580 struct cfstring_descriptor key;
3582 if (!str)
3583 return false;
3585 STRIP_NOPS (str);
3587 if (TREE_CODE (str) == ADDR_EXPR)
3588 str = TREE_OPERAND (str, 0);
3590 if (TREE_CODE (str) != STRING_CST)
3591 return false;
3593 key.literal = str;
3594 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, NO_INSERT);
3596 if (loc)
3597 return true;
3599 return false;
3602 void
3603 darwin_enter_string_into_cfstring_table (tree str)
3605 struct cfstring_descriptor key;
3607 key.literal = str;
3608 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
3610 if (!*loc)
3612 *loc = ggc_cleared_alloc<cfstring_descriptor> ();
3613 ((struct cfstring_descriptor *)*loc)->literal = str;
3617 /* Choose named function section based on its frequency. */
3619 section *
3620 darwin_function_section (tree decl, enum node_frequency freq,
3621 bool startup, bool exit)
3623 /* Decide if we need to put this in a coalescable section. */
3624 bool weak = (decl
3625 && DECL_WEAK (decl)
3626 && (!DECL_ATTRIBUTES (decl)
3627 || !lookup_attribute ("weak_import",
3628 DECL_ATTRIBUTES (decl))));
3630 /* If there is a specified section name, we should not be trying to
3631 override. */
3632 if (decl && DECL_SECTION_NAME (decl) != NULL)
3633 return get_named_section (decl, NULL, 0);
3635 /* We always put unlikely executed stuff in the cold section. */
3636 if (freq == NODE_FREQUENCY_UNLIKELY_EXECUTED)
3637 return (weak) ? darwin_sections[text_cold_coal_section]
3638 : darwin_sections[text_cold_section];
3640 /* If we have LTO *and* feedback information, then let LTO handle
3641 the function ordering, it makes a better job (for normal, hot,
3642 startup and exit - hence the bailout for cold above). */
3643 if (in_lto_p && flag_profile_values)
3644 goto default_function_sections;
3646 /* Non-cold startup code should go to startup subsection. */
3647 if (startup)
3648 return (weak) ? darwin_sections[text_startup_coal_section]
3649 : darwin_sections[text_startup_section];
3651 /* Similarly for exit. */
3652 if (exit)
3653 return (weak) ? darwin_sections[text_exit_coal_section]
3654 : darwin_sections[text_exit_section];
3656 /* Place hot code. */
3657 if (freq == NODE_FREQUENCY_HOT)
3658 return (weak) ? darwin_sections[text_hot_coal_section]
3659 : darwin_sections[text_hot_section];
3661 /* Otherwise, default to the 'normal' non-reordered sections. */
3662 default_function_sections:
3663 return (weak) ? darwin_sections[text_coal_section]
3664 : text_section;
3667 /* When a function is partitioned between sections, we need to insert a label
3668 at the start of each new chunk - so that it may become a valid 'atom' for
3669 eh and debug purposes. Without this the linker will emit warnings if one
3670 tries to add line location information (since the switched fragment will
3671 be anonymous). */
3673 void
3674 darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
3676 char buf[128];
3677 snprintf (buf, 128, "%s%s",new_is_cold?"__cold_sect_of_":"__hot_sect_of_",
3678 IDENTIFIER_POINTER (DECL_NAME (decl)));
3679 /* Make sure we pick up all the relevant quotes etc. */
3680 assemble_name_raw (fp, (const char *) buf);
3681 fputs (":\n", fp);
3684 #include "gt-darwin.h"