2014-10-24 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / config / darwin.c
blobe8e9412df16006ed9526506630947a4aff3d6728
1 /* Functions for generic Darwin as target machine for GNU C compiler.
2 Copyright (C) 1989-2014 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 "tree.h"
35 #include "stringpool.h"
36 #include "varasm.h"
37 #include "stor-layout.h"
38 #include "expr.h"
39 #include "reload.h"
40 #include "hashtab.h"
41 #include "hash-set.h"
42 #include "vec.h"
43 #include "machmode.h"
44 #include "input.h"
45 #include "function.h"
46 #include "ggc.h"
47 #include "langhooks.h"
48 #include "target.h"
49 #include "tm_p.h"
50 #include "diagnostic-core.h"
51 #include "toplev.h"
52 #include "df.h"
53 #include "debug.h"
54 #include "obstack.h"
55 #include "hash-table.h"
56 #include "basic-block.h"
57 #include "tree-ssa-alias.h"
58 #include "internal-fn.h"
59 #include "gimple-fold.h"
60 #include "tree-eh.h"
61 #include "gimple-expr.h"
62 #include "is-a.h"
63 #include "gimple.h"
64 #include "gimplify.h"
65 #include "lto-streamer.h"
66 #include "lto-section-names.h"
68 /* Darwin supports a feature called fix-and-continue, which is used
69 for rapid turn around debugging. When code is compiled with the
70 -mfix-and-continue flag, two changes are made to the generated code
71 that allow the system to do things that it would normally not be
72 able to do easily. These changes allow gdb to load in
73 recompilation of a translation unit that has been changed into a
74 running program and replace existing functions and methods of that
75 translation unit with versions of those functions and methods
76 from the newly compiled translation unit. The new functions access
77 the existing static symbols from the old translation unit, if the
78 symbol existed in the unit to be replaced, and from the new
79 translation unit, otherwise.
81 The changes are to insert 5 nops at the beginning of all functions
82 and to use indirection to get at static symbols. The 5 nops
83 are required by consumers of the generated code. Currently, gdb
84 uses this to patch in a jump to the overriding function, this
85 allows all uses of the old name to forward to the replacement,
86 including existing function pointers and virtual methods. See
87 rs6000_emit_prologue for the code that handles the nop insertions.
89 The added indirection allows gdb to redirect accesses to static
90 symbols from the newly loaded translation unit to the existing
91 symbol, if any. @code{static} symbols are special and are handled by
92 setting the second word in the .non_lazy_symbol_pointer data
93 structure to symbol. See indirect_data for the code that handles
94 the extra indirection, and machopic_output_indirection and its use
95 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
96 symbol indirection. */
98 /* For darwin >= 9 (OSX 10.5) the linker is capable of making the necessary
99 branch islands and we no longer need to emit darwin stubs.
100 However, if we are generating code for earlier systems (or for use in the
101 kernel) the stubs might still be required, and this will be set true. */
102 int darwin_emit_branch_islands = false;
104 typedef struct GTY(()) cdtor_record {
105 rtx symbol;
106 int priority; /* [con/de]structor priority */
107 int position; /* original position */
108 } cdtor_record;
110 static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL;
111 static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL;
113 /* A flag to determine whether we are running c++ or obj-c++. This has to be
114 settable from non-c-family contexts too (i.e. we can't use the c_dialect_
115 functions). */
116 int darwin_running_cxx;
118 /* Some code-gen now depends on OS major version numbers (at least). */
119 int generating_for_darwin_version ;
121 /* Section names. */
122 section * darwin_sections[NUM_DARWIN_SECTIONS];
124 /* While we transition to using in-tests instead of ifdef'd code. */
125 #ifndef HAVE_lo_sum
126 #define HAVE_lo_sum 0
127 #define gen_macho_high(a,b) (a)
128 #define gen_macho_low(a,b,c) (a)
129 #endif
131 /* True if we're setting __attribute__ ((ms_struct)). */
132 int darwin_ms_struct = false;
134 /* Earlier versions of Darwin as do not recognize an alignment field in
135 .comm directives, this should be set for versions that allow it. */
136 int emit_aligned_common = false;
138 /* A get_unnamed_section callback used to switch to an ObjC section.
139 DIRECTIVE is as for output_section_asm_op. */
141 static void
142 output_objc_section_asm_op (const void *directive)
144 static bool been_here = false;
146 /* The NeXT ObjC Runtime requires these sections to be present and in
147 order in the object. The code below implements this by emitting
148 a section header for each ObjC section the first time that an ObjC
149 section is requested. */
150 if (! been_here)
152 section *saved_in_section = in_section;
153 static const enum darwin_section_enum tomark[] =
155 /* written, cold -> hot */
156 objc_cat_cls_meth_section,
157 objc_cat_inst_meth_section,
158 objc_string_object_section,
159 objc_constant_string_object_section,
160 objc_selector_refs_section,
161 objc_selector_fixup_section,
162 objc_cls_refs_section,
163 objc_class_section,
164 objc_meta_class_section,
165 /* shared, hot -> cold */
166 objc_cls_meth_section,
167 objc_inst_meth_section,
168 objc_protocol_section,
169 objc_class_names_section,
170 objc_meth_var_types_section,
171 objc_meth_var_names_section,
172 objc_category_section,
173 objc_class_vars_section,
174 objc_instance_vars_section,
175 objc_module_info_section,
176 objc_symbols_section,
178 /* ABI=1 */
179 static const enum darwin_section_enum tomarkv1[] =
181 objc1_protocol_ext_section,
182 objc1_class_ext_section,
183 objc1_prop_list_section
185 /* ABI=2 */
186 static const enum darwin_section_enum tomarkv2[] =
188 objc2_message_refs_section,
189 objc2_classdefs_section,
190 objc2_metadata_section,
191 objc2_classrefs_section,
192 objc2_classlist_section,
193 objc2_categorylist_section,
194 objc2_selector_refs_section,
195 objc2_nonlazy_class_section,
196 objc2_nonlazy_category_section,
197 objc2_protocollist_section,
198 objc2_protocolrefs_section,
199 objc2_super_classrefs_section,
200 objc2_image_info_section,
201 objc2_constant_string_object_section
203 size_t i;
205 been_here = true;
206 if (flag_objc_abi < 2)
208 for (i = 0; i < ARRAY_SIZE (tomark); i++)
209 switch_to_section (darwin_sections[tomark[i]]);
210 if (flag_objc_abi == 1)
211 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
212 switch_to_section (darwin_sections[tomarkv1[i]]);
214 else
215 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
216 switch_to_section (darwin_sections[tomarkv2[i]]);
217 /* Make sure we don't get varasm.c out of sync with us. */
218 switch_to_section (saved_in_section);
220 output_section_asm_op (directive);
224 /* Private flag applied to disable section-anchors in a particular section. */
225 #define SECTION_NO_ANCHOR SECTION_MACH_DEP
228 /* Implement TARGET_ASM_INIT_SECTIONS. */
230 void
231 darwin_init_sections (void)
233 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
234 darwin_sections[NAME] = \
235 get_unnamed_section (FLAGS, (OBJC \
236 ? output_objc_section_asm_op \
237 : output_section_asm_op), \
238 "\t" DIRECTIVE);
239 #include "config/darwin-sections.def"
240 #undef DEF_SECTION
242 readonly_data_section = darwin_sections[const_section];
243 exception_section = darwin_sections[darwin_exception_section];
244 eh_frame_section = darwin_sections[darwin_eh_frame_section];
248 name_needs_quotes (const char *name)
250 int c;
251 while ((c = *name++) != '\0')
252 if (! ISIDNUM (c)
253 && c != '.' && c != '$' && c != '_' )
254 return 1;
255 return 0;
258 /* Return true if SYM_REF can be used without an indirection. */
260 machopic_symbol_defined_p (rtx sym_ref)
262 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
263 return true;
265 /* If a symbol references local and is not an extern to this
266 file, then the symbol might be able to declared as defined. */
267 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
269 /* If the symbol references a variable and the variable is a
270 common symbol, then this symbol is not defined. */
271 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
273 tree decl = SYMBOL_REF_DECL (sym_ref);
274 if (!decl)
275 return true;
276 if (DECL_COMMON (decl))
277 return false;
279 return true;
281 return false;
284 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
285 reference, which will not be changed. */
287 enum machopic_addr_class
288 machopic_classify_symbol (rtx sym_ref)
290 bool function_p;
292 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
293 if (machopic_symbol_defined_p (sym_ref))
294 return (function_p
295 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
296 else
297 return (function_p
298 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
301 #ifndef TARGET_FIX_AND_CONTINUE
302 #define TARGET_FIX_AND_CONTINUE 0
303 #endif
305 /* Indicate when fix-and-continue style code generation is being used
306 and when a reference to data should be indirected so that it can be
307 rebound in a new translation unit to reference the original instance
308 of that data. Symbol names that are for code generation local to
309 the translation unit are bound to the new translation unit;
310 currently this means symbols that begin with L or _OBJC_;
311 otherwise, we indicate that an indirect reference should be made to
312 permit the runtime to rebind new instances of the translation unit
313 to the original instance of the data. */
315 static int
316 indirect_data (rtx sym_ref)
318 int lprefix;
319 const char *name;
321 /* If we aren't generating fix-and-continue code, don't do anything
322 special. */
323 if (TARGET_FIX_AND_CONTINUE == 0)
324 return 0;
326 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
327 are indirected. Symbols that begin with L and _OBJC_ are always
328 bound to the current translation unit as they are used for
329 generated local data of the translation unit. */
331 name = XSTR (sym_ref, 0);
333 lprefix = (((name[0] == '*' || name[0] == '&')
334 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
335 || (strncmp (name, "_OBJC_", 6) == 0));
337 return ! lprefix;
340 static int
341 machopic_data_defined_p (rtx sym_ref)
343 if (indirect_data (sym_ref))
344 return 0;
346 switch (machopic_classify_symbol (sym_ref))
348 case MACHOPIC_DEFINED_DATA:
349 case MACHOPIC_DEFINED_FUNCTION:
350 return 1;
351 default:
352 return 0;
356 void
357 machopic_define_symbol (rtx mem)
359 rtx sym_ref;
361 gcc_assert (GET_CODE (mem) == MEM);
362 sym_ref = XEXP (mem, 0);
363 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
366 /* Return either ORIG or:
368 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
370 depending on MACHO_DYNAMIC_NO_PIC_P. */
372 machopic_gen_offset (rtx orig)
374 if (MACHO_DYNAMIC_NO_PIC_P)
375 return orig;
376 else
378 /* Play games to avoid marking the function as needing pic if we
379 are being called as part of the cost-estimation process. */
380 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
381 crtl->uses_pic_offset_table = 1;
382 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
383 UNSPEC_MACHOPIC_OFFSET);
384 return gen_rtx_CONST (Pmode, orig);
388 static GTY(()) const char * function_base_func_name;
389 static GTY(()) int current_pic_label_num;
390 static GTY(()) int emitted_pic_label_num;
392 static void
393 update_pic_label_number_if_needed (void)
395 const char *current_name;
397 /* When we are generating _get_pc thunks within stubs, there is no current
398 function. */
399 if (current_function_decl)
401 current_name =
402 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
403 if (function_base_func_name != current_name)
405 ++current_pic_label_num;
406 function_base_func_name = current_name;
409 else
411 ++current_pic_label_num;
412 function_base_func_name = "L_machopic_stub_dummy";
416 void
417 machopic_output_function_base_name (FILE *file)
419 /* If dynamic-no-pic is on, we should not get here. */
420 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
422 update_pic_label_number_if_needed ();
423 fprintf (file, "L%d$pb", current_pic_label_num);
426 char curr_picbasename[32];
428 const char *
429 machopic_get_function_picbase (void)
431 /* If dynamic-no-pic is on, we should not get here. */
432 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
434 update_pic_label_number_if_needed ();
435 snprintf (curr_picbasename, 32, "L%d$pb", current_pic_label_num);
436 return (const char *) curr_picbasename;
439 bool
440 machopic_should_output_picbase_label (void)
442 update_pic_label_number_if_needed ();
444 if (current_pic_label_num == emitted_pic_label_num)
445 return false;
447 emitted_pic_label_num = current_pic_label_num;
448 return true;
451 /* The suffix attached to non-lazy pointer symbols. */
452 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
453 /* The suffix attached to stub symbols. */
454 #define STUB_SUFFIX "$stub"
456 typedef struct GTY ((for_user)) machopic_indirection
458 /* The SYMBOL_REF for the entity referenced. */
459 rtx symbol;
460 /* The name of the stub or non-lazy pointer. */
461 const char * ptr_name;
462 /* True iff this entry is for a stub (as opposed to a non-lazy
463 pointer). */
464 bool stub_p;
465 /* True iff this stub or pointer pointer has been referenced. */
466 bool used;
467 } machopic_indirection;
469 struct indirection_hasher : ggc_hasher<machopic_indirection *>
471 typedef const char *compare_type;
472 static hashval_t hash (machopic_indirection *);
473 static bool equal (machopic_indirection *, const char *);
476 /* A table mapping stub names and non-lazy pointer names to
477 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
479 static GTY (()) hash_table<indirection_hasher> *machopic_indirections;
481 /* Return a hash value for a SLOT in the indirections hash table. */
483 hashval_t
484 indirection_hasher::hash (machopic_indirection *p)
486 return htab_hash_string (p->ptr_name);
489 /* Returns true if the KEY is the same as that associated with
490 SLOT. */
492 bool
493 indirection_hasher::equal (machopic_indirection *s, const char *k)
495 return strcmp (s->ptr_name, k) == 0;
498 /* Return the name of the non-lazy pointer (if STUB_P is false) or
499 stub (if STUB_B is true) corresponding to the given name. */
501 const char *
502 machopic_indirection_name (rtx sym_ref, bool stub_p)
504 char *buffer;
505 const char *name = XSTR (sym_ref, 0);
506 size_t namelen = strlen (name);
507 machopic_indirection *p;
508 bool needs_quotes;
509 const char *suffix;
510 const char *prefix = user_label_prefix;
511 const char *quote = "";
512 tree id;
514 id = maybe_get_identifier (name);
515 if (id)
517 tree id_orig = id;
519 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
520 id = TREE_CHAIN (id);
521 if (id != id_orig)
523 name = IDENTIFIER_POINTER (id);
524 namelen = strlen (name);
528 if (name[0] == '*')
530 prefix = "";
531 ++name;
532 --namelen;
535 needs_quotes = name_needs_quotes (name);
536 if (needs_quotes)
538 quote = "\"";
541 if (stub_p)
542 suffix = STUB_SUFFIX;
543 else
544 suffix = NON_LAZY_POINTER_SUFFIX;
546 buffer = XALLOCAVEC (char, strlen ("&L")
547 + strlen (prefix)
548 + namelen
549 + strlen (suffix)
550 + 2 * strlen (quote)
551 + 1 /* '\0' */);
553 /* Construct the name of the non-lazy pointer or stub. */
554 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
556 if (!machopic_indirections)
557 machopic_indirections = hash_table<indirection_hasher>::create_ggc (37);
559 machopic_indirection **slot
560 = machopic_indirections->find_slot_with_hash (buffer,
561 htab_hash_string (buffer),
562 INSERT);
563 if (*slot)
565 p = *slot;
567 else
569 p = ggc_alloc<machopic_indirection> ();
570 p->symbol = sym_ref;
571 p->ptr_name = xstrdup (buffer);
572 p->stub_p = stub_p;
573 p->used = false;
574 *slot = p;
577 return p->ptr_name;
580 /* Return the name of the stub for the mcount function. */
582 const char*
583 machopic_mcount_stub_name (void)
585 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
586 return machopic_indirection_name (symbol, /*stub_p=*/true);
589 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
590 or non-lazy pointer as used -- and mark the object to which the
591 pointer/stub refers as used as well, since the pointer/stub will
592 emit a reference to it. */
594 void
595 machopic_validate_stub_or_non_lazy_ptr (const char *name)
597 machopic_indirection *p
598 = machopic_indirections->find_with_hash (name, htab_hash_string (name));
599 if (p && ! p->used)
601 const char *real_name;
602 tree id;
604 p->used = true;
606 /* Do what output_addr_const will do when we actually call it. */
607 if (SYMBOL_REF_DECL (p->symbol))
608 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
610 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
612 id = maybe_get_identifier (real_name);
613 if (id)
614 mark_referenced (id);
618 /* Transform ORIG, which may be any data source, to the corresponding
619 source using indirections. */
622 machopic_indirect_data_reference (rtx orig, rtx reg)
624 rtx ptr_ref = orig;
626 if (! MACHOPIC_INDIRECT)
627 return orig;
629 if (GET_CODE (orig) == SYMBOL_REF)
631 int defined = machopic_data_defined_p (orig);
633 if (defined && MACHO_DYNAMIC_NO_PIC_P)
635 if (DARWIN_PPC)
637 /* Create a new register for CSE opportunities. */
638 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
639 emit_insn (gen_macho_high (hi_reg, orig));
640 emit_insn (gen_macho_low (reg, hi_reg, orig));
641 return reg;
643 else if (DARWIN_X86)
644 return orig;
645 else
646 /* some other cpu -- writeme! */
647 gcc_unreachable ();
649 else if (defined)
651 rtx offset = NULL;
652 if (DARWIN_PPC || HAVE_lo_sum)
653 offset = machopic_gen_offset (orig);
655 if (DARWIN_PPC)
657 rtx hi_sum_reg = (!can_create_pseudo_p ()
658 ? reg
659 : gen_reg_rtx (Pmode));
661 gcc_assert (reg);
663 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
664 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
665 gen_rtx_HIGH (Pmode, offset))));
666 emit_insn (gen_rtx_SET (Pmode, reg,
667 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
668 copy_rtx (offset))));
670 orig = reg;
672 else if (HAVE_lo_sum)
674 gcc_assert (reg);
676 emit_insn (gen_rtx_SET (VOIDmode, reg,
677 gen_rtx_HIGH (Pmode, offset)));
678 emit_insn (gen_rtx_SET (VOIDmode, reg,
679 gen_rtx_LO_SUM (Pmode, reg,
680 copy_rtx (offset))));
681 emit_use (pic_offset_table_rtx);
683 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
685 return orig;
688 ptr_ref = (gen_rtx_SYMBOL_REF
689 (Pmode,
690 machopic_indirection_name (orig, /*stub_p=*/false)));
692 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
694 ptr_ref = gen_const_mem (Pmode, ptr_ref);
695 machopic_define_symbol (ptr_ref);
697 if (DARWIN_X86
698 && reg
699 && MACHO_DYNAMIC_NO_PIC_P)
701 emit_insn (gen_rtx_SET (Pmode, reg, ptr_ref));
702 ptr_ref = reg;
705 return ptr_ref;
707 else if (GET_CODE (orig) == CONST)
709 /* If "(const (plus ...", walk the PLUS and return that result.
710 PLUS processing (below) will restore the "(const ..." if
711 appropriate. */
712 if (GET_CODE (XEXP (orig, 0)) == PLUS)
713 return machopic_indirect_data_reference (XEXP (orig, 0), reg);
714 else
715 return orig;
717 else if (GET_CODE (orig) == MEM)
719 XEXP (ptr_ref, 0) =
720 machopic_indirect_data_reference (XEXP (orig, 0), reg);
721 return ptr_ref;
723 else if (GET_CODE (orig) == PLUS)
725 rtx base, result;
726 /* When the target is i386, this code prevents crashes due to the
727 compiler's ignorance on how to move the PIC base register to
728 other registers. (The reload phase sometimes introduces such
729 insns.) */
730 if (GET_CODE (XEXP (orig, 0)) == REG
731 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
732 /* Prevent the same register from being erroneously used
733 as both the base and index registers. */
734 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
735 && reg)
737 emit_move_insn (reg, XEXP (orig, 0));
738 XEXP (ptr_ref, 0) = reg;
739 return ptr_ref;
742 /* Legitimize both operands of the PLUS. */
743 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
744 orig = machopic_indirect_data_reference (XEXP (orig, 1),
745 (base == reg ? 0 : reg));
746 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
747 result = plus_constant (Pmode, base, INTVAL (orig));
748 else
749 result = gen_rtx_PLUS (Pmode, base, orig);
751 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
753 if (reg)
755 emit_move_insn (reg, result);
756 result = reg;
758 else
760 result = force_reg (GET_MODE (result), result);
764 return result;
766 return ptr_ref;
769 /* Transform TARGET (a MEM), which is a function call target, to the
770 corresponding symbol_stub if necessary. Return a new MEM. */
773 machopic_indirect_call_target (rtx target)
775 if (! darwin_emit_branch_islands)
776 return target;
778 if (GET_CODE (target) != MEM)
779 return target;
781 if (MACHOPIC_INDIRECT
782 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
783 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
784 & MACHO_SYMBOL_FLAG_DEFINED))
786 rtx sym_ref = XEXP (target, 0);
787 const char *stub_name = machopic_indirection_name (sym_ref,
788 /*stub_p=*/true);
789 enum machine_mode mode = GET_MODE (sym_ref);
791 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
792 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
793 MEM_READONLY_P (target) = 1;
794 MEM_NOTRAP_P (target) = 1;
797 return target;
801 machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
803 rtx pic_ref = orig;
805 if (! MACHOPIC_INDIRECT)
806 return orig;
808 /* First handle a simple SYMBOL_REF or LABEL_REF */
809 if (GET_CODE (orig) == LABEL_REF
810 || (GET_CODE (orig) == SYMBOL_REF
813 /* addr(foo) = &func+(foo-func) */
814 orig = machopic_indirect_data_reference (orig, reg);
816 if (GET_CODE (orig) == PLUS
817 && GET_CODE (XEXP (orig, 0)) == REG)
819 if (reg == 0)
820 return force_reg (mode, orig);
822 emit_move_insn (reg, orig);
823 return reg;
826 if (GET_CODE (orig) == MEM)
828 if (reg == 0)
830 gcc_assert (!reload_in_progress);
831 reg = gen_reg_rtx (Pmode);
834 #if HAVE_lo_sum
835 if (MACHO_DYNAMIC_NO_PIC_P
836 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
837 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
839 #if defined (TARGET_TOC) /* ppc */
840 rtx temp_reg = (!can_create_pseudo_p ()
841 ? reg :
842 gen_reg_rtx (Pmode));
843 rtx asym = XEXP (orig, 0);
844 rtx mem;
846 emit_insn (gen_macho_high (temp_reg, asym));
847 mem = gen_const_mem (GET_MODE (orig),
848 gen_rtx_LO_SUM (Pmode, temp_reg,
849 copy_rtx (asym)));
850 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
851 #else
852 /* Some other CPU -- WriteMe! but right now there are no other
853 platforms that can use dynamic-no-pic */
854 gcc_unreachable ();
855 #endif
856 pic_ref = reg;
858 else
859 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
860 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
862 rtx offset = machopic_gen_offset (XEXP (orig, 0));
863 #if defined (TARGET_TOC) /* i.e., PowerPC */
864 /* Generating a new reg may expose opportunities for
865 common subexpression elimination. */
866 rtx hi_sum_reg = (!can_create_pseudo_p ()
867 ? reg
868 : gen_reg_rtx (Pmode));
869 rtx mem;
870 rtx insn;
871 rtx sum;
873 sum = gen_rtx_HIGH (Pmode, offset);
874 if (! MACHO_DYNAMIC_NO_PIC_P)
875 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
877 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
879 mem = gen_const_mem (GET_MODE (orig),
880 gen_rtx_LO_SUM (Pmode,
881 hi_sum_reg,
882 copy_rtx (offset)));
883 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
884 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
886 pic_ref = reg;
887 #else
888 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
890 emit_insn (gen_rtx_SET (VOIDmode, reg,
891 gen_rtx_HIGH (Pmode,
892 gen_rtx_CONST (Pmode,
893 offset))));
894 emit_insn (gen_rtx_SET (VOIDmode, reg,
895 gen_rtx_LO_SUM (Pmode, reg,
896 gen_rtx_CONST (Pmode,
897 copy_rtx (offset)))));
898 pic_ref = gen_rtx_PLUS (Pmode,
899 pic_offset_table_rtx, reg);
900 #endif
902 else
903 #endif /* HAVE_lo_sum */
905 rtx pic = pic_offset_table_rtx;
906 if (GET_CODE (pic) != REG)
908 emit_move_insn (reg, pic);
909 pic = reg;
911 #if 0
912 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
913 #endif
915 if (reload_in_progress)
916 df_set_regs_ever_live (REGNO (pic), true);
917 pic_ref = gen_rtx_PLUS (Pmode, pic,
918 machopic_gen_offset (XEXP (orig, 0)));
921 #if !defined (TARGET_TOC)
922 emit_move_insn (reg, pic_ref);
923 pic_ref = gen_const_mem (GET_MODE (orig), reg);
924 #endif
926 else
929 #if HAVE_lo_sum
930 if (GET_CODE (orig) == SYMBOL_REF
931 || GET_CODE (orig) == LABEL_REF)
933 rtx offset = machopic_gen_offset (orig);
934 #if defined (TARGET_TOC) /* i.e., PowerPC */
935 rtx hi_sum_reg;
937 if (reg == 0)
939 gcc_assert (!reload_in_progress);
940 reg = gen_reg_rtx (Pmode);
943 hi_sum_reg = reg;
945 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
946 (MACHO_DYNAMIC_NO_PIC_P)
947 ? gen_rtx_HIGH (Pmode, offset)
948 : gen_rtx_PLUS (Pmode,
949 pic_offset_table_rtx,
950 gen_rtx_HIGH (Pmode,
951 offset))));
952 emit_insn (gen_rtx_SET (VOIDmode, reg,
953 gen_rtx_LO_SUM (Pmode,
954 hi_sum_reg,
955 copy_rtx (offset))));
956 pic_ref = reg;
957 #else
958 emit_insn (gen_rtx_SET (VOIDmode, reg,
959 gen_rtx_HIGH (Pmode, offset)));
960 emit_insn (gen_rtx_SET (VOIDmode, reg,
961 gen_rtx_LO_SUM (Pmode, reg,
962 copy_rtx (offset))));
963 pic_ref = gen_rtx_PLUS (Pmode,
964 pic_offset_table_rtx, reg);
965 #endif
967 else
968 #endif /* HAVE_lo_sum */
970 if (REG_P (orig)
971 || GET_CODE (orig) == SUBREG)
973 return orig;
975 else
977 rtx pic = pic_offset_table_rtx;
978 if (GET_CODE (pic) != REG)
980 emit_move_insn (reg, pic);
981 pic = reg;
983 #if 0
984 emit_use (pic_offset_table_rtx);
985 #endif
986 if (reload_in_progress)
987 df_set_regs_ever_live (REGNO (pic), true);
988 pic_ref = gen_rtx_PLUS (Pmode,
989 pic,
990 machopic_gen_offset (orig));
995 if (GET_CODE (pic_ref) != REG)
997 if (reg != 0)
999 emit_move_insn (reg, pic_ref);
1000 return reg;
1002 else
1004 return force_reg (mode, pic_ref);
1007 else
1009 return pic_ref;
1013 else if (GET_CODE (orig) == SYMBOL_REF)
1014 return orig;
1016 else if (GET_CODE (orig) == PLUS
1017 && (GET_CODE (XEXP (orig, 0)) == MEM
1018 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
1019 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
1020 && XEXP (orig, 0) != pic_offset_table_rtx
1021 && GET_CODE (XEXP (orig, 1)) != REG)
1024 rtx base;
1025 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
1027 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1028 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
1029 Pmode, (base == reg ? 0 : reg));
1030 if (GET_CODE (orig) == CONST_INT)
1032 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
1033 is_complex = 1;
1035 else
1036 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
1038 if (reg && is_complex)
1040 emit_move_insn (reg, pic_ref);
1041 pic_ref = reg;
1043 /* Likewise, should we set special REG_NOTEs here? */
1046 else if (GET_CODE (orig) == CONST)
1048 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1051 else if (GET_CODE (orig) == MEM
1052 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
1054 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1055 addr = replace_equiv_address (orig, addr);
1056 emit_move_insn (reg, addr);
1057 pic_ref = reg;
1060 return pic_ref;
1063 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
1064 DATA is the FILE* for assembly output. Called from
1065 htab_traverse. */
1068 machopic_output_indirection (machopic_indirection **slot, FILE *asm_out_file)
1070 machopic_indirection *p = *slot;
1071 rtx symbol;
1072 const char *sym_name;
1073 const char *ptr_name;
1075 if (!p->used)
1076 return 1;
1078 symbol = p->symbol;
1079 sym_name = XSTR (symbol, 0);
1080 ptr_name = p->ptr_name;
1082 if (p->stub_p)
1084 char *sym;
1085 char *stub;
1086 tree id;
1088 id = maybe_get_identifier (sym_name);
1089 if (id)
1091 tree id_orig = id;
1093 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1094 id = TREE_CHAIN (id);
1095 if (id != id_orig)
1096 sym_name = IDENTIFIER_POINTER (id);
1099 sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1100 if (sym_name[0] == '*' || sym_name[0] == '&')
1101 strcpy (sym, sym_name + 1);
1102 else if (sym_name[0] == '-' || sym_name[0] == '+')
1103 strcpy (sym, sym_name);
1104 else
1105 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1107 stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1108 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1109 strcpy (stub, ptr_name + 1);
1110 else
1111 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1113 machopic_output_stub (asm_out_file, sym, stub);
1115 else if (! indirect_data (symbol)
1116 && (machopic_symbol_defined_p (symbol)
1117 || SYMBOL_REF_LOCAL_P (symbol)))
1119 switch_to_section (data_section);
1120 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1121 assemble_label (asm_out_file, ptr_name);
1122 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1123 GET_MODE_SIZE (Pmode),
1124 GET_MODE_ALIGNMENT (Pmode), 1);
1126 else
1128 rtx init = const0_rtx;
1130 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1132 /* Mach-O symbols are passed around in code through indirect
1133 references and the original symbol_ref hasn't passed through
1134 the generic handling and reference-catching in
1135 output_operand, so we need to manually mark weak references
1136 as such. */
1137 if (SYMBOL_REF_WEAK (symbol))
1139 tree decl = SYMBOL_REF_DECL (symbol);
1140 gcc_assert (DECL_P (decl));
1142 if (decl != NULL_TREE
1143 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
1144 /* Handle only actual external-only definitions, not
1145 e.g. extern inline code or variables for which
1146 storage has been allocated. */
1147 && !TREE_STATIC (decl))
1149 fputs ("\t.weak_reference ", asm_out_file);
1150 assemble_name (asm_out_file, sym_name);
1151 fputc ('\n', asm_out_file);
1155 assemble_name (asm_out_file, ptr_name);
1156 fprintf (asm_out_file, ":\n");
1158 fprintf (asm_out_file, "\t.indirect_symbol ");
1159 assemble_name (asm_out_file, sym_name);
1160 fprintf (asm_out_file, "\n");
1162 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1163 have their symbol name instead of 0 in the second entry of
1164 the non-lazy symbol pointer data structure when they are
1165 defined. This allows the runtime to rebind newer instances
1166 of the translation unit with the original instance of the
1167 symbol. */
1169 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1170 && machopic_symbol_defined_p (symbol))
1171 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1173 assemble_integer (init, GET_MODE_SIZE (Pmode),
1174 GET_MODE_ALIGNMENT (Pmode), 1);
1177 return 1;
1180 void
1181 machopic_finish (FILE *asm_out_file)
1183 if (machopic_indirections)
1184 machopic_indirections
1185 ->traverse_noresize<FILE *, machopic_output_indirection> (asm_out_file);
1189 machopic_operand_p (rtx op)
1191 if (MACHOPIC_JUST_INDIRECT)
1192 return (GET_CODE (op) == SYMBOL_REF
1193 && machopic_symbol_defined_p (op));
1194 else
1195 return (GET_CODE (op) == CONST
1196 && GET_CODE (XEXP (op, 0)) == UNSPEC
1197 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1200 /* This function records whether a given name corresponds to a defined
1201 or undefined function or variable, for machopic_classify_ident to
1202 use later. */
1204 void
1205 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1207 rtx sym_ref;
1209 /* Do the standard encoding things first. */
1210 default_encode_section_info (decl, rtl, first);
1212 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1213 return;
1215 sym_ref = XEXP (rtl, 0);
1216 if (TREE_CODE (decl) == VAR_DECL)
1217 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1219 if (!DECL_EXTERNAL (decl)
1220 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1221 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1222 && ((TREE_STATIC (decl)
1223 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1224 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1225 && DECL_INITIAL (decl) != error_mark_node)))
1226 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1228 if (! TREE_PUBLIC (decl))
1229 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1232 void
1233 darwin_mark_decl_preserved (const char *name)
1235 fprintf (asm_out_file, "\t.no_dead_strip ");
1236 assemble_name (asm_out_file, name);
1237 fputc ('\n', asm_out_file);
1240 static section *
1241 darwin_rodata_section (int weak, bool zsize)
1243 return (weak
1244 ? darwin_sections[const_coal_section]
1245 : (zsize ? darwin_sections[zobj_const_section]
1246 : darwin_sections[const_section]));
1249 static section *
1250 darwin_mergeable_string_section (tree exp,
1251 unsigned HOST_WIDE_INT align)
1253 /* Darwin's ld expects to see non-writable string literals in the .cstring
1254 section. Later versions of ld check and complain when CFStrings are
1255 enabled. Therefore we shall force the strings into .cstring since we
1256 don't support writable ones anyway. */
1257 if ((darwin_constant_cfstrings || flag_merge_constants)
1258 && TREE_CODE (exp) == STRING_CST
1259 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1260 && align <= 256
1261 && (int_size_in_bytes (TREE_TYPE (exp))
1262 == TREE_STRING_LENGTH (exp))
1263 && ((size_t) TREE_STRING_LENGTH (exp)
1264 == strlen (TREE_STRING_POINTER (exp)) + 1))
1265 return darwin_sections[cstring_section];
1267 if (DARWIN_SECTION_ANCHORS && flag_section_anchors
1268 && TREE_CODE (exp) == STRING_CST
1269 && TREE_STRING_LENGTH (exp) == 0)
1270 return darwin_sections[zobj_const_section];
1272 return readonly_data_section;
1275 #ifndef HAVE_GAS_LITERAL16
1276 #define HAVE_GAS_LITERAL16 0
1277 #endif
1279 static section *
1280 darwin_mergeable_constant_section (tree exp,
1281 unsigned HOST_WIDE_INT align,
1282 bool zsize)
1284 enum machine_mode mode = DECL_MODE (exp);
1285 unsigned int modesize = GET_MODE_BITSIZE (mode);
1287 if (DARWIN_SECTION_ANCHORS
1288 && flag_section_anchors
1289 && zsize)
1290 return darwin_sections[zobj_const_section];
1292 if (flag_merge_constants
1293 && mode != VOIDmode
1294 && mode != BLKmode
1295 && modesize <= align
1296 && align >= 8
1297 && align <= 256
1298 && (align & (align -1)) == 0)
1300 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1302 if (TREE_CODE (size) == INTEGER_CST)
1304 if (wi::eq_p (size, 4))
1305 return darwin_sections[literal4_section];
1306 else if (wi::eq_p (size, 8))
1307 return darwin_sections[literal8_section];
1308 else if (HAVE_GAS_LITERAL16
1309 && TARGET_64BIT
1310 && wi::eq_p (size, 16))
1311 return darwin_sections[literal16_section];
1315 return readonly_data_section;
1318 section *
1319 darwin_tm_clone_table_section (void)
1321 return get_named_section (NULL,
1322 "__DATA,__tm_clone_table,regular,no_dead_strip",
1327 machopic_reloc_rw_mask (void)
1329 return MACHOPIC_INDIRECT ? 3 : 0;
1332 /* We have to deal with ObjC/C++ metadata section placement in the common
1333 code, since it will also be called from LTO.
1335 Return metadata attributes, if present (searching for ABI=2 first)
1336 Return NULL_TREE if no such attributes are found. */
1338 static tree
1339 is_objc_metadata (tree decl)
1341 if (DECL_P (decl)
1342 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1343 && DECL_ATTRIBUTES (decl))
1345 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1346 if (meta)
1347 return meta;
1348 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1349 if (meta)
1350 return meta;
1352 return NULL_TREE;
1355 static int classes_seen;
1356 static int objc_metadata_seen;
1358 /* Return the section required for Objective C ABI 2 metadata. */
1359 static section *
1360 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1362 const char *p;
1363 tree ident = TREE_VALUE (meta);
1364 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1365 p = IDENTIFIER_POINTER (ident);
1367 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi == 2);
1369 objc_metadata_seen = 1;
1371 if (base == data_section)
1372 base = darwin_sections[objc2_metadata_section];
1374 /* Most of the OBJC2 META-data end up in the base section, so check it
1375 first. */
1376 if (!strncmp (p, "V2_BASE", 7))
1377 return base;
1378 else if (!strncmp (p, "V2_STRG", 7))
1379 return darwin_sections[cstring_section];
1381 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1382 return darwin_sections[objc2_classdefs_section];
1383 else if (!strncmp (p, "V2_MREF", 7))
1384 return darwin_sections[objc2_message_refs_section];
1385 else if (!strncmp (p, "V2_CLRF", 7))
1386 return darwin_sections[objc2_classrefs_section];
1387 else if (!strncmp (p, "V2_SURF", 7))
1388 return darwin_sections[objc2_super_classrefs_section];
1389 else if (!strncmp (p, "V2_NLCL", 7))
1390 return darwin_sections[objc2_nonlazy_class_section];
1391 else if (!strncmp (p, "V2_CLAB", 7))
1393 classes_seen = 1;
1394 return darwin_sections[objc2_classlist_section];
1396 else if (!strncmp (p, "V2_SRFS", 7))
1397 return darwin_sections[objc2_selector_refs_section];
1398 else if (!strncmp (p, "V2_NLCA", 7))
1399 return darwin_sections[objc2_nonlazy_category_section];
1400 else if (!strncmp (p, "V2_CALA", 7))
1401 return darwin_sections[objc2_categorylist_section];
1403 else if (!strncmp (p, "V2_PLST", 7))
1404 return darwin_sections[objc2_protocollist_section];
1405 else if (!strncmp (p, "V2_PRFS", 7))
1406 return darwin_sections[objc2_protocolrefs_section];
1408 else if (!strncmp (p, "V2_INFO", 7))
1409 return darwin_sections[objc2_image_info_section];
1411 else if (!strncmp (p, "V2_EHTY", 7))
1412 return darwin_sections[data_coal_section];
1414 else if (!strncmp (p, "V2_CSTR", 7))
1415 return darwin_sections[objc2_constant_string_object_section];
1417 /* Not recognized, default. */
1418 return base;
1421 /* Return the section required for Objective C ABI 0/1 metadata. */
1422 static section *
1423 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1425 const char *p;
1426 tree ident = TREE_VALUE (meta);
1427 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1428 p = IDENTIFIER_POINTER (ident);
1430 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi < 2);
1432 objc_metadata_seen = 1;
1434 /* String sections first, cos there are lots of strings. */
1435 if (!strncmp (p, "V1_STRG", 7))
1436 return darwin_sections[cstring_section];
1437 else if (!strncmp (p, "V1_CLSN", 7))
1438 return darwin_sections[objc_class_names_section];
1439 else if (!strncmp (p, "V1_METN", 7))
1440 return darwin_sections[objc_meth_var_names_section];
1441 else if (!strncmp (p, "V1_METT", 7))
1442 return darwin_sections[objc_meth_var_types_section];
1444 else if (!strncmp (p, "V1_CLAS", 7))
1446 classes_seen = 1;
1447 return darwin_sections[objc_class_section];
1449 else if (!strncmp (p, "V1_META", 7))
1450 return darwin_sections[objc_meta_class_section];
1451 else if (!strncmp (p, "V1_CATG", 7))
1452 return darwin_sections[objc_category_section];
1453 else if (!strncmp (p, "V1_PROT", 7))
1454 return darwin_sections[objc_protocol_section];
1456 else if (!strncmp (p, "V1_CLCV", 7))
1457 return darwin_sections[objc_class_vars_section];
1458 else if (!strncmp (p, "V1_CLIV", 7))
1459 return darwin_sections[objc_instance_vars_section];
1461 else if (!strncmp (p, "V1_CLCM", 7))
1462 return darwin_sections[objc_cls_meth_section];
1463 else if (!strncmp (p, "V1_CLIM", 7))
1464 return darwin_sections[objc_inst_meth_section];
1465 else if (!strncmp (p, "V1_CACM", 7))
1466 return darwin_sections[objc_cat_cls_meth_section];
1467 else if (!strncmp (p, "V1_CAIM", 7))
1468 return darwin_sections[objc_cat_inst_meth_section];
1469 else if (!strncmp (p, "V1_PNSM", 7))
1470 return darwin_sections[objc_cat_inst_meth_section];
1471 else if (!strncmp (p, "V1_PCLM", 7))
1472 return darwin_sections[objc_cat_cls_meth_section];
1474 else if (!strncmp (p, "V1_CLPR", 7))
1475 return darwin_sections[objc_cat_cls_meth_section];
1476 else if (!strncmp (p, "V1_CAPR", 7))
1477 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1479 else if (!strncmp (p, "V1_PRFS", 7))
1480 return darwin_sections[objc_cat_cls_meth_section];
1481 else if (!strncmp (p, "V1_CLRF", 7))
1482 return darwin_sections[objc_cls_refs_section];
1483 else if (!strncmp (p, "V1_SRFS", 7))
1484 return darwin_sections[objc_selector_refs_section];
1486 else if (!strncmp (p, "V1_MODU", 7))
1487 return darwin_sections[objc_module_info_section];
1488 else if (!strncmp (p, "V1_SYMT", 7))
1489 return darwin_sections[objc_symbols_section];
1490 else if (!strncmp (p, "V1_INFO", 7))
1491 return darwin_sections[objc_image_info_section];
1493 else if (!strncmp (p, "V1_PLST", 7))
1494 return darwin_sections[objc1_prop_list_section];
1495 else if (!strncmp (p, "V1_PEXT", 7))
1496 return darwin_sections[objc1_protocol_ext_section];
1497 else if (!strncmp (p, "V1_CEXT", 7))
1498 return darwin_sections[objc1_class_ext_section];
1500 else if (!strncmp (p, "V2_CSTR", 7))
1501 return darwin_sections[objc_constant_string_object_section];
1503 return base;
1506 section *
1507 machopic_select_section (tree decl,
1508 int reloc,
1509 unsigned HOST_WIDE_INT align)
1511 bool zsize, one, weak, ro;
1512 section *base_section = NULL;
1514 weak = (DECL_P (decl)
1515 && DECL_WEAK (decl)
1516 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1518 zsize = (DECL_P (decl)
1519 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1520 && tree_to_uhwi (DECL_SIZE_UNIT (decl)) == 0);
1522 one = DECL_P (decl)
1523 && TREE_CODE (decl) == VAR_DECL
1524 && DECL_COMDAT_GROUP (decl);
1526 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1528 switch (categorize_decl_for_section (decl, reloc))
1530 case SECCAT_TEXT:
1531 gcc_unreachable ();
1532 break;
1534 case SECCAT_RODATA:
1535 case SECCAT_SRODATA:
1536 base_section = darwin_rodata_section (weak, zsize);
1537 break;
1539 case SECCAT_RODATA_MERGE_STR:
1540 base_section = darwin_mergeable_string_section (decl, align);
1541 break;
1543 case SECCAT_RODATA_MERGE_STR_INIT:
1544 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1545 break;
1547 case SECCAT_RODATA_MERGE_CONST:
1548 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1549 break;
1551 case SECCAT_DATA:
1552 case SECCAT_DATA_REL:
1553 case SECCAT_DATA_REL_LOCAL:
1554 case SECCAT_DATA_REL_RO:
1555 case SECCAT_DATA_REL_RO_LOCAL:
1556 case SECCAT_SDATA:
1557 case SECCAT_TDATA:
1558 if (weak || one)
1560 if (ro)
1561 base_section = darwin_sections[const_data_coal_section];
1562 else
1563 base_section = darwin_sections[data_coal_section];
1565 else if (DARWIN_SECTION_ANCHORS
1566 && flag_section_anchors
1567 && zsize)
1569 /* If we're doing section anchors, then punt zero-sized objects into
1570 their own sections so that they don't interfere with offset
1571 computation for the remaining vars. This does not need to be done
1572 for stuff in mergeable sections, since these are ineligible for
1573 anchors. */
1574 if (ro)
1575 base_section = darwin_sections[zobj_const_data_section];
1576 else
1577 base_section = darwin_sections[zobj_data_section];
1579 else if (ro)
1580 base_section = darwin_sections[const_data_section];
1581 else
1582 base_section = data_section;
1583 break;
1584 case SECCAT_BSS:
1585 case SECCAT_SBSS:
1586 case SECCAT_TBSS:
1587 if (weak || one)
1588 base_section = darwin_sections[data_coal_section];
1589 else
1591 if (!TREE_PUBLIC (decl))
1592 base_section = lcomm_section;
1593 else if (bss_noswitch_section)
1594 base_section = bss_noswitch_section;
1595 else
1596 base_section = data_section;
1598 break;
1600 default:
1601 gcc_unreachable ();
1604 /* Darwin weird special cases.
1605 a) OBJC Meta-data. */
1606 if (DECL_P (decl)
1607 && (TREE_CODE (decl) == VAR_DECL
1608 || TREE_CODE (decl) == CONST_DECL)
1609 && DECL_ATTRIBUTES (decl))
1611 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1612 if (meta)
1613 return darwin_objc2_section (decl, meta, base_section);
1614 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1615 if (meta)
1616 return darwin_objc1_section (decl, meta, base_section);
1617 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1618 if (meta)
1619 return base_section; /* GNU runtime is happy with it all in one pot. */
1622 /* b) Constant string objects. */
1623 if (TREE_CODE (decl) == CONSTRUCTOR
1624 && TREE_TYPE (decl)
1625 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1626 && TYPE_NAME (TREE_TYPE (decl)))
1628 tree name = TYPE_NAME (TREE_TYPE (decl));
1629 if (TREE_CODE (name) == TYPE_DECL)
1630 name = DECL_NAME (name);
1632 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1634 if (flag_next_runtime)
1636 if (flag_objc_abi == 2)
1637 return darwin_sections[objc2_constant_string_object_section];
1638 else
1639 return darwin_sections[objc_constant_string_object_section];
1641 else
1642 return darwin_sections[objc_string_object_section];
1644 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1645 return darwin_sections[cfstring_constant_object_section];
1646 else
1647 return base_section;
1649 /* c) legacy meta-data selection. */
1650 else if (TREE_CODE (decl) == VAR_DECL
1651 && DECL_NAME (decl)
1652 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1653 && IDENTIFIER_POINTER (DECL_NAME (decl))
1654 && flag_next_runtime
1655 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1657 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1658 static bool warned_objc_46 = false;
1659 /* We shall assert that zero-sized objects are an error in ObjC
1660 meta-data. */
1661 gcc_assert (tree_to_uhwi (DECL_SIZE_UNIT (decl)) != 0);
1663 /* ??? This mechanism for determining the metadata section is
1664 broken when LTO is in use, since the frontend that generated
1665 the data is not identified. We will keep the capability for
1666 the short term - in case any non-Objective-C programs are using
1667 it to place data in specified sections. */
1668 if (!warned_objc_46)
1670 location_t loc = DECL_SOURCE_LOCATION (decl);
1671 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
1672 " to select meta-data sections is deprecated at 4.6"
1673 " and will be removed in 4.7");
1674 warned_objc_46 = true;
1677 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1678 return darwin_sections[objc_cls_meth_section];
1679 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1680 return darwin_sections[objc_inst_meth_section];
1681 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
1682 return darwin_sections[objc_cat_cls_meth_section];
1683 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
1684 return darwin_sections[objc_cat_inst_meth_section];
1685 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1686 return darwin_sections[objc_class_vars_section];
1687 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1688 return darwin_sections[objc_instance_vars_section];
1689 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1690 return darwin_sections[objc_cat_cls_meth_section];
1691 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1692 return darwin_sections[objc_class_names_section];
1693 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1694 return darwin_sections[objc_meth_var_names_section];
1695 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1696 return darwin_sections[objc_meth_var_types_section];
1697 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1698 return darwin_sections[objc_cls_refs_section];
1699 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1700 return darwin_sections[objc_class_section];
1701 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1702 return darwin_sections[objc_meta_class_section];
1703 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1704 return darwin_sections[objc_category_section];
1705 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1706 return darwin_sections[objc_selector_refs_section];
1707 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1708 return darwin_sections[objc_selector_fixup_section];
1709 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1710 return darwin_sections[objc_symbols_section];
1711 else if (!strncmp (name, "_OBJC_MODULES", 13))
1712 return darwin_sections[objc_module_info_section];
1713 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1714 return darwin_sections[objc_image_info_section];
1715 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1716 return darwin_sections[objc_cat_inst_meth_section];
1717 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1718 return darwin_sections[objc_cat_cls_meth_section];
1719 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1720 return darwin_sections[objc_cat_cls_meth_section];
1721 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1722 return darwin_sections[objc_protocol_section];
1723 else
1724 return base_section;
1727 return base_section;
1730 /* This can be called with address expressions as "rtx".
1731 They must go in "const". */
1733 section *
1734 machopic_select_rtx_section (enum machine_mode mode, rtx x,
1735 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1737 if (GET_MODE_SIZE (mode) == 8
1738 && (GET_CODE (x) == CONST_INT
1739 || GET_CODE (x) == CONST_WIDE_INT
1740 || GET_CODE (x) == CONST_DOUBLE))
1741 return darwin_sections[literal8_section];
1742 else if (GET_MODE_SIZE (mode) == 4
1743 && (GET_CODE (x) == CONST_INT
1744 || GET_CODE (x) == CONST_WIDE_INT
1745 || GET_CODE (x) == CONST_DOUBLE))
1746 return darwin_sections[literal4_section];
1747 else if (HAVE_GAS_LITERAL16
1748 && TARGET_64BIT
1749 && GET_MODE_SIZE (mode) == 16
1750 && (GET_CODE (x) == CONST_INT
1751 || GET_CODE (x) == CONST_WIDE_INT
1752 || GET_CODE (x) == CONST_DOUBLE
1753 || GET_CODE (x) == CONST_VECTOR))
1754 return darwin_sections[literal16_section];
1755 else if (MACHOPIC_INDIRECT
1756 && (GET_CODE (x) == SYMBOL_REF
1757 || GET_CODE (x) == CONST
1758 || GET_CODE (x) == LABEL_REF))
1759 return darwin_sections[const_data_section];
1760 else
1761 return darwin_sections[const_section];
1764 void
1765 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1767 cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
1769 vec_safe_push (ctors, new_elt);
1771 if (! MACHOPIC_INDIRECT)
1772 fprintf (asm_out_file, ".reference .constructors_used\n");
1775 void
1776 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1778 cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)};
1780 vec_safe_push (dtors, new_elt);
1782 if (! MACHOPIC_INDIRECT)
1783 fprintf (asm_out_file, ".reference .destructors_used\n");
1786 static int
1787 sort_cdtor_records (const void * a, const void * b)
1789 const cdtor_record *cda = (const cdtor_record *)a;
1790 const cdtor_record *cdb = (const cdtor_record *)b;
1791 if (cda->priority > cdb->priority)
1792 return 1;
1793 if (cda->priority < cdb->priority)
1794 return -1;
1795 if (cda->position > cdb->position)
1796 return 1;
1797 if (cda->position < cdb->position)
1798 return -1;
1799 return 0;
1802 static void
1803 finalize_ctors ()
1805 unsigned int i;
1806 cdtor_record *elt;
1808 if (MACHOPIC_INDIRECT)
1809 switch_to_section (darwin_sections[mod_init_section]);
1810 else
1811 switch_to_section (darwin_sections[constructor_section]);
1813 if (vec_safe_length (ctors) > 1)
1814 ctors->qsort (sort_cdtor_records);
1815 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
1817 assemble_align (POINTER_SIZE);
1818 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1822 static void
1823 finalize_dtors ()
1825 unsigned int i;
1826 cdtor_record *elt;
1828 if (MACHOPIC_INDIRECT)
1829 switch_to_section (darwin_sections[mod_term_section]);
1830 else
1831 switch_to_section (darwin_sections[destructor_section]);
1833 if (vec_safe_length (dtors) > 1)
1834 dtors->qsort (sort_cdtor_records);
1835 FOR_EACH_VEC_SAFE_ELT (dtors, i, elt)
1837 assemble_align (POINTER_SIZE);
1838 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1842 void
1843 darwin_globalize_label (FILE *stream, const char *name)
1845 if (!!strncmp (name, "_OBJC_", 6))
1846 default_globalize_label (stream, name);
1849 /* This routine returns non-zero if 'name' starts with the special objective-c
1850 anonymous file-scope static name. It accommodates c++'s mangling of such
1851 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1853 int
1854 darwin_label_is_anonymous_local_objc_name (const char *name)
1856 const unsigned char *p = (const unsigned char *) name;
1857 if (*p != '_')
1858 return 0;
1859 if (p[1] == 'Z' && p[2] == 'L')
1861 p += 3;
1862 while (*p >= '0' && *p <= '9')
1863 p++;
1865 return (!strncmp ((const char *)p, "_OBJC_", 6));
1868 /* LTO support for Mach-O.
1870 This version uses three mach-o sections to encapsulate the (unlimited
1871 number of) lto sections.
1873 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1874 __GNU_LTO, __section_names contains the GNU LTO section names.
1875 __GNU_LTO, __section_index contains an array of values that index these.
1877 Indexed thus:
1878 <section offset from the start of __GNU_LTO, __lto_sections>,
1879 <section length>
1880 <name offset from the start of __GNU_LTO, __section_names,
1881 <name length>.
1883 At present, for both m32 and m64 mach-o files each of these fields is
1884 represented by a uint32_t. This is because, AFAICT, a mach-o object
1885 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1887 uint32_t offset;
1888 "offset An integer specifying the offset to this section in the file." */
1890 /* Count lto section numbers. */
1891 static unsigned int lto_section_num = 0;
1893 /* A vector of information about LTO sections, at present, we only have
1894 the name. TODO: see if we can get the data length somehow. */
1895 typedef struct GTY (()) darwin_lto_section_e {
1896 const char *sectname;
1897 } darwin_lto_section_e ;
1899 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1901 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1902 sections into three Mach-O ones).
1903 NOTE: These names MUST be kept in sync with those in
1904 libiberty/simple-object-mach-o. */
1905 #define LTO_SECTS_SECTION "__wrapper_sects"
1906 #define LTO_NAMES_SECTION "__wrapper_names"
1907 #define LTO_INDEX_SECTION "__wrapper_index"
1909 /* File to temporarily store LTO data. This is appended to asm_out_file
1910 in darwin_end_file. */
1911 static FILE *lto_asm_out_file, *saved_asm_out_file;
1912 static char *lto_asm_out_name;
1914 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1915 asm_out_file and switching to an alternative output file. */
1916 void
1917 darwin_asm_lto_start (void)
1919 gcc_assert (! saved_asm_out_file);
1920 saved_asm_out_file = asm_out_file;
1921 if (! lto_asm_out_name)
1922 lto_asm_out_name = make_temp_file (".lto.s");
1923 lto_asm_out_file = fopen (lto_asm_out_name, "a");
1924 if (lto_asm_out_file == NULL)
1925 fatal_error ("failed to open temporary file %s for LTO output",
1926 lto_asm_out_name);
1927 asm_out_file = lto_asm_out_file;
1930 /* Restore asm_out_file. */
1931 void
1932 darwin_asm_lto_end (void)
1934 gcc_assert (saved_asm_out_file);
1935 fclose (lto_asm_out_file);
1936 asm_out_file = saved_asm_out_file;
1937 saved_asm_out_file = NULL;
1940 static void
1941 darwin_asm_dwarf_section (const char *name, unsigned int flags, tree decl);
1943 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
1945 void
1946 darwin_asm_named_section (const char *name,
1947 unsigned int flags,
1948 tree decl ATTRIBUTE_UNUSED)
1950 /* LTO sections go in a special section that encapsulates the (unlimited)
1951 number of GNU LTO sections within a single mach-o one. */
1952 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1953 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1955 darwin_lto_section_e e;
1956 /* We expect certain flags to be set... */
1957 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1958 == (SECTION_DEBUG | SECTION_NAMED));
1960 /* Switch to our combined section. */
1961 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1962 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1963 /* Output a label for the start of this sub-section. */
1964 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1965 lto_section_num, name);
1966 /* We have to jump through hoops to get the values of the intra-section
1967 offsets... */
1968 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1969 lto_section_num, lto_section_num);
1970 fprintf (asm_out_file,
1971 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1972 lto_section_num, lto_section_num+1, lto_section_num);
1973 lto_section_num++;
1974 e.sectname = xstrdup (name);
1975 /* Keep the names, we'll need to make a table later.
1976 TODO: check that we do not revisit sections, that would break
1977 the assumption of how this is done. */
1978 if (lto_section_names == NULL)
1979 vec_alloc (lto_section_names, 16);
1980 vec_safe_push (lto_section_names, e);
1982 else if (strncmp (name, "__DWARF,", 8) == 0)
1983 darwin_asm_dwarf_section (name, flags, decl);
1984 else
1985 fprintf (asm_out_file, "\t.section %s\n", name);
1988 void
1989 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
1991 /* Darwin does not use unique sections. */
1994 /* Handle __attribute__ ((apple_kext_compatibility)).
1995 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
1996 vtable for classes with this attribute (and their descendants) by not
1997 outputting the new 3.0 nondeleting destructor. This means that such
1998 objects CANNOT be allocated on the stack or as globals UNLESS they have
1999 a completely empty `operator delete'.
2000 Luckily, this fits in with the Darwin kext model.
2002 This attribute also disables gcc3's potential overlaying of derived
2003 class data members on the padding at the end of the base class. */
2005 tree
2006 darwin_handle_kext_attribute (tree *node, tree name,
2007 tree args ATTRIBUTE_UNUSED,
2008 int flags ATTRIBUTE_UNUSED,
2009 bool *no_add_attrs)
2011 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
2012 if (! TARGET_KEXTABI)
2014 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2015 "only when compiling a kext", name);
2017 *no_add_attrs = true;
2019 else if (TREE_CODE (*node) != RECORD_TYPE)
2021 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2022 "only to C++ classes", name);
2024 *no_add_attrs = true;
2027 return NULL_TREE;
2030 /* Handle a "weak_import" attribute; arguments as in
2031 struct attribute_spec.handler. */
2033 tree
2034 darwin_handle_weak_import_attribute (tree *node, tree name,
2035 tree ARG_UNUSED (args),
2036 int ARG_UNUSED (flags),
2037 bool * no_add_attrs)
2039 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
2041 warning (OPT_Wattributes, "%qE attribute ignored",
2042 name);
2043 *no_add_attrs = true;
2045 else
2046 declare_weak (*node);
2048 return NULL_TREE;
2051 /* Emit a label for an FDE, making it global and/or weak if appropriate.
2052 The third parameter is nonzero if this is for exception handling.
2053 The fourth parameter is nonzero if this is just a placeholder for an
2054 FDE that we are omitting. */
2056 void
2057 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
2059 char *lab ;
2060 char buf[32];
2061 static int invok_count = 0;
2062 static tree last_fun_decl = NULL_TREE;
2064 /* We use the linker to emit the .eh labels for Darwin 9 and above. */
2065 if (! for_eh || generating_for_darwin_version >= 9)
2066 return;
2068 /* FIXME: This only works when the eh for all sections of a function is
2069 emitted at the same time. If that changes, we would need to use a lookup
2070 table of some form to determine what to do. Also, we should emit the
2071 unadorned label for the partition containing the public label for a
2072 function. This is of limited use, probably, since we do not currently
2073 enable partitioning. */
2074 strcpy (buf, ".eh");
2075 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
2077 if (decl == last_fun_decl)
2079 invok_count++;
2080 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
2082 else
2084 last_fun_decl = decl;
2085 invok_count = 0;
2089 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
2091 if (TREE_PUBLIC (decl))
2093 targetm.asm_out.globalize_label (file, lab);
2094 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
2096 fputs ("\t.private_extern ", file);
2097 assemble_name (file, lab);
2098 fputc ('\n', file);
2102 if (DECL_WEAK (decl))
2104 fputs ("\t.weak_definition ", file);
2105 assemble_name (file, lab);
2106 fputc ('\n', file);
2109 assemble_name (file, lab);
2110 if (empty)
2112 fputs (" = 0\n", file);
2114 /* Mark the absolute .eh and .eh1 style labels as needed to
2115 ensure that we don't dead code strip them and keep such
2116 labels from another instantiation point until we can fix this
2117 properly with group comdat support. */
2118 darwin_mark_decl_preserved (lab);
2120 else
2121 fputs (":\n", file);
2123 free (lab);
2126 static GTY(()) unsigned long except_table_label_num;
2128 void
2129 darwin_emit_except_table_label (FILE *file)
2131 char section_start_label[30];
2133 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2134 except_table_label_num++);
2135 ASM_OUTPUT_LABEL (file, section_start_label);
2137 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2139 void
2140 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2142 const char *nlp_name;
2144 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2146 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2147 fputs ("\t.long\t", file);
2148 ASM_OUTPUT_LABELREF (file, nlp_name);
2149 fputs ("-.", file);
2152 /* If this is uncommented, details of each allocation will be printed
2153 in the asm right before the actual code. WARNING - this will cause some
2154 test-suite fails (since the printout will contain items that some tests
2155 are not expecting) -- so don't leave it on by default (it bloats the
2156 asm too). */
2157 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2159 /* The first two of these routines are ostensibly just intended to put
2160 names into the asm. However, they are both hijacked in order to ensure
2161 that zero-sized items do not make their way into the output. Consequently,
2162 we also need to make these participate in provisions for dealing with
2163 such items in section anchors. */
2165 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2166 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2167 but it does need to be referenced via indirect PIC data pointers.
2168 The machopic_define_symbol calls are telling the machopic subsystem
2169 that the name *is* defined in this module, so it doesn't need to
2170 make them indirect. */
2171 void
2172 darwin_asm_declare_object_name (FILE *file,
2173 const char *nam, tree decl)
2175 const char *xname = nam;
2176 unsigned HOST_WIDE_INT size;
2177 bool local_def, weak;
2179 weak = (DECL_P (decl)
2180 && DECL_WEAK (decl)
2181 && !lookup_attribute ("weak_import",
2182 DECL_ATTRIBUTES (decl)));
2184 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2185 && (!DECL_COMMON (decl)
2186 || !TREE_PUBLIC (decl)));
2188 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2189 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2191 if (local_def)
2193 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2194 if (!weak)
2195 machopic_define_symbol (DECL_RTL (decl));
2198 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2200 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2201 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2202 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2203 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2204 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2205 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2206 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2207 (unsigned long)DECL_INITIAL (decl));
2208 #endif
2210 /* Darwin needs help to support local zero-sized objects.
2211 They must be made at least one byte, and the section containing must be
2212 marked as unsuitable for section-anchors (see storage allocators below).
2214 For non-zero objects this output is handled by varasm.c.
2216 if (!size)
2218 unsigned int l2align = 0;
2220 /* The align must be honored, even for zero-sized. */
2221 if (DECL_ALIGN (decl))
2223 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2224 fprintf (file, "\t.align\t%u\n", l2align);
2227 ASM_OUTPUT_LABEL (file, xname);
2228 size = 1;
2229 fprintf (file, "\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2231 /* Check that we've correctly picked up the zero-sized item and placed it
2232 properly. */
2233 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2234 || (in_section
2235 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2237 else
2238 ASM_OUTPUT_LABEL (file, xname);
2241 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2242 void
2243 darwin_asm_declare_constant_name (FILE *file, const char *name,
2244 const_tree exp ATTRIBUTE_UNUSED,
2245 HOST_WIDE_INT size)
2247 assemble_label (file, name);
2248 /* As for other items, we need at least one byte. */
2249 if (!size)
2251 fputs ("\t.space\t1\n", file);
2252 /* Check that we've correctly picked up the zero-sized item and placed it
2253 properly. */
2254 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2255 || (in_section
2256 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2260 /* Darwin storage allocators.
2262 Zerofill sections are desirable for large blank data since, otherwise, these
2263 data bloat objects (PR33210).
2265 However, section anchors don't work in .zerofill sections (one cannot switch
2266 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2267 to put (at least some) data into 'normal' switchable sections.
2269 Here we set a relatively arbitrary value for the size of an object to trigger
2270 zerofill when section anchors are enabled (anything bigger than a page for
2271 current Darwin implementations). FIXME: there ought to be some objective way
2272 to make this choice.
2274 When section anchor are off this is ignored anyway. */
2276 #define BYTES_ZFILL 4096
2278 /* Emit a chunk of data for items coalesced by the linker. */
2279 static void
2280 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2281 unsigned HOST_WIDE_INT size,
2282 unsigned int align)
2284 /* Since the sections used here are coalesed, they will not be eligible
2285 for section anchors, and therefore we don't need to break that out. */
2286 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2287 switch_to_section (darwin_sections[const_data_coal_section]);
2288 else
2289 switch_to_section (darwin_sections[data_coal_section]);
2291 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2292 the align info for zero-sized items... but do it here otherwise. */
2293 if (size && align)
2294 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2296 if (TREE_PUBLIC (decl))
2297 darwin_globalize_label (fp, name);
2299 /* ... and we let it deal with outputting one byte of zero for them too. */
2300 darwin_asm_declare_object_name (fp, name, decl);
2301 if (size)
2302 assemble_zeros (size);
2305 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2306 static void
2307 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2308 unsigned HOST_WIDE_INT size,
2309 unsigned int align, tree meta)
2311 section *ocs = data_section;
2313 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2314 ocs = darwin_objc2_section (decl, meta, ocs);
2315 else
2316 ocs = darwin_objc1_section (decl, meta, ocs);
2318 switch_to_section (ocs);
2320 /* We shall declare that zero-sized meta-data are not valid (yet). */
2321 gcc_assert (size);
2322 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
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 assemble_zeros (size);
2329 /* This routine emits 'local' storage:
2331 When Section Anchors are off this routine emits .zerofill commands in
2332 sections named for their alignment.
2334 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2335 the .static_data section so that the section anchoring system can see them.
2336 Larger items are still placed in .zerofill sections, addressing PR33210.
2337 The routine has no checking - it is all assumed to be done by the caller.
2339 static void
2340 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2341 unsigned HOST_WIDE_INT size,
2342 unsigned int l2align)
2344 /* FIXME: We have a fudge to make this work with Java even when the target does
2345 not use sections anchors -- Java seems to need at least one small item in a
2346 non-zerofill segment. */
2347 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2348 || (size && size <= 2))
2350 /* Put smaller objects in _static_data, where the section anchors system
2351 can get them.
2352 However, if they are zero-sized punt them to yet a different section
2353 (that is not allowed to participate in anchoring). */
2354 if (!size)
2356 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2357 in_section = darwin_sections[zobj_bss_section];
2358 size = 1;
2360 else
2362 fputs ("\t.static_data\n", fp);
2363 in_section = darwin_sections[static_data_section];
2366 if (l2align)
2367 fprintf (fp, "\t.align\t%u\n", l2align);
2369 assemble_name (fp, name);
2370 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2372 else
2374 /* When we are on a non-section anchor target, we can get zero-sized
2375 items here. However, all we need to do is to bump them to one byte
2376 and the section alignment will take care of the rest. */
2377 char secnam[64];
2378 unsigned int flags ;
2379 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
2380 (unsigned) l2align);
2381 /* We can't anchor (yet, if ever) in zerofill sections, because we can't
2382 switch to them and emit a label. */
2383 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2384 in_section = get_section (secnam, flags, NULL);
2385 fprintf (fp, "\t.zerofill %s,", secnam);
2386 assemble_name (fp, name);
2387 if (!size)
2388 size = 1;
2390 if (l2align)
2391 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2392 size, (unsigned) l2align);
2393 else
2394 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2397 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2398 /* This is defined as a file-scope var, so we know to notify machopic. */
2399 machopic_define_symbol (DECL_RTL (decl));
2402 /* Emit a chunk of common. */
2403 static void
2404 darwin_emit_common (FILE *fp, const char *name,
2405 unsigned HOST_WIDE_INT size, unsigned int align)
2407 unsigned HOST_WIDE_INT rounded;
2408 unsigned int l2align;
2410 /* Earlier systems complain if the alignment exceeds the page size.
2411 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2412 if (!emit_aligned_common && (align > 32768UL))
2413 align = 4096UL; /* In units. */
2414 else
2415 align /= BITS_PER_UNIT;
2417 /* Make sure we have a meaningful align. */
2418 if (!align)
2419 align = 1;
2421 /* For earlier toolchains, we need to emit the var as a rounded size to
2422 tell ld the alignment. */
2423 if (size < align)
2424 rounded = align;
2425 else
2426 rounded = (size + (align-1)) & ~(align-1);
2428 l2align = floor_log2 (align);
2429 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2431 in_section = comm_section;
2432 /* We mustn't allow multiple public symbols to share an address when using
2433 the normal OSX toolchain. */
2434 if (!size)
2436 /* Put at least one byte. */
2437 size = 1;
2438 /* This section can no longer participate in section anchoring. */
2439 comm_section->common.flags |= SECTION_NO_ANCHOR;
2442 fputs ("\t.comm\t", fp);
2443 assemble_name (fp, name);
2444 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2445 emit_aligned_common?size:rounded);
2446 if (l2align && emit_aligned_common)
2447 fprintf (fp, ",%u", l2align);
2448 fputs ("\n", fp);
2451 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2452 or coalescable data sections (for weak or comdat) as appropriate. */
2454 void
2455 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2456 unsigned HOST_WIDE_INT size, unsigned int align)
2458 unsigned int l2align;
2459 bool one, pub, weak;
2460 tree meta;
2462 pub = TREE_PUBLIC (decl);
2463 one = DECL_ONE_ONLY (decl);
2464 weak = (DECL_P (decl)
2465 && DECL_WEAK (decl)
2466 && !lookup_attribute ("weak_import",
2467 DECL_ATTRIBUTES (decl)));
2469 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2470 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2471 " pub %d weak %d one %d init %lx\n",
2472 name, (long long)size, (int)align, TREE_READONLY (decl),
2473 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2474 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2475 #endif
2477 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2478 before the target has a chance to comment. */
2479 if ((meta = is_objc_metadata (decl)))
2481 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2482 return;
2485 /* Check that any initializer is valid. */
2486 gcc_assert ((DECL_INITIAL (decl) == NULL)
2487 || (DECL_INITIAL (decl) == error_mark_node)
2488 || initializer_zerop (DECL_INITIAL (decl)));
2490 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2491 gcc_assert (!DECL_COMMON (decl));
2493 /* Pick up the correct alignment. */
2494 if (!size || !align)
2495 align = DECL_ALIGN (decl);
2497 l2align = floor_log2 (align / BITS_PER_UNIT);
2498 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2500 last_assemble_variable_decl = decl;
2502 /* We would rather not have to check this here - but it seems that we might
2503 be passed a decl that should be in coalesced space. */
2504 if (one || weak)
2506 /* Weak or COMDAT objects are put in mergeable sections. */
2507 darwin_emit_weak_or_comdat (fp, decl, name, size,
2508 DECL_ALIGN (decl));
2509 return;
2512 /* If this is not public, then emit according to local rules. */
2513 if (!pub)
2515 darwin_emit_local_bss (fp, decl, name, size, l2align);
2516 return;
2519 /* So we have a public symbol (small item fudge for Java, see above). */
2520 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2521 || (size && size <= 2))
2523 /* Put smaller objects in data, where the section anchors system can get
2524 them. However, if they are zero-sized punt them to yet a different
2525 section (that is not allowed to participate in anchoring). */
2526 if (!size)
2528 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2529 in_section = darwin_sections[zobj_data_section];
2530 size = 1;
2532 else
2534 fputs ("\t.data\n", fp);
2535 in_section = data_section;
2538 if (l2align)
2539 fprintf (fp, "\t.align\t%u\n", l2align);
2541 assemble_name (fp, name);
2542 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2544 else
2546 char secnam[64];
2547 unsigned int flags ;
2548 /* When we are on a non-section anchor target, we can get zero-sized
2549 items here. However, all we need to do is to bump them to one byte
2550 and the section alignment will take care of the rest. */
2551 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
2553 /* We can't anchor in zerofill sections, because we can't switch
2554 to them and emit a label. */
2555 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2556 in_section = get_section (secnam, flags, NULL);
2557 fprintf (fp, "\t.zerofill %s,", secnam);
2558 assemble_name (fp, name);
2559 if (!size)
2560 size = 1;
2562 if (l2align)
2563 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2564 else
2565 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2567 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2570 /* Output a chunk of common, with alignment specified (where the target
2571 supports this). */
2572 void
2573 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2574 unsigned HOST_WIDE_INT size,
2575 unsigned int align)
2577 unsigned int l2align;
2578 bool one, weak;
2579 tree meta;
2581 /* No corresponding var. */
2582 if (decl==NULL)
2584 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2585 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2586 #endif
2587 darwin_emit_common (fp, name, size, align);
2588 return;
2591 one = DECL_ONE_ONLY (decl);
2592 weak = (DECL_P (decl)
2593 && DECL_WEAK (decl)
2594 && !lookup_attribute ("weak_import",
2595 DECL_ATTRIBUTES (decl)));
2597 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2598 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2599 " weak %d one %d init %lx\n",
2600 name, (long long)size, (int)align, TREE_READONLY (decl),
2601 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2602 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2603 #endif
2605 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2606 before the target has a chance to comment. */
2607 if ((meta = is_objc_metadata (decl)))
2609 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2610 return;
2613 /* We shouldn't be messing with this if the decl has a section name. */
2614 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2616 /* We would rather not have to check this here - but it seems that we might
2617 be passed a decl that should be in coalesced space. */
2618 if (one || weak)
2620 /* Weak or COMDAT objects are put in mergable sections. */
2621 darwin_emit_weak_or_comdat (fp, decl, name, size,
2622 DECL_ALIGN (decl));
2623 return;
2626 /* We should only get here for DECL_COMMON, with a zero init (and, in
2627 principle, only for public symbols too - although we deal with local
2628 ones below). */
2630 /* Check the initializer is OK. */
2631 gcc_assert (DECL_COMMON (decl)
2632 && ((DECL_INITIAL (decl) == NULL)
2633 || (DECL_INITIAL (decl) == error_mark_node)
2634 || initializer_zerop (DECL_INITIAL (decl))));
2636 last_assemble_variable_decl = decl;
2638 if (!size || !align)
2639 align = DECL_ALIGN (decl);
2641 l2align = floor_log2 (align / BITS_PER_UNIT);
2642 /* Check we aren't asking for more aligment than the platform allows. */
2643 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2645 if (TREE_PUBLIC (decl) != 0)
2646 darwin_emit_common (fp, name, size, align);
2647 else
2648 darwin_emit_local_bss (fp, decl, name, size, l2align);
2651 /* Output a chunk of BSS with alignment specfied. */
2652 void
2653 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2654 unsigned HOST_WIDE_INT size,
2655 unsigned int align)
2657 unsigned long l2align;
2658 bool one, weak;
2659 tree meta;
2661 one = DECL_ONE_ONLY (decl);
2662 weak = (DECL_P (decl)
2663 && DECL_WEAK (decl)
2664 && !lookup_attribute ("weak_import",
2665 DECL_ATTRIBUTES (decl)));
2667 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2668 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2669 " weak %d init %lx\n",
2670 name, (long long)size, (int)align, TREE_READONLY (decl),
2671 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2672 weak , (unsigned long)DECL_INITIAL (decl));
2673 #endif
2675 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2676 before the target has a chance to comment. */
2677 if ((meta = is_objc_metadata (decl)))
2679 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2680 return;
2683 /* We shouldn't be messing with this if the decl has a section name. */
2684 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2686 /* We would rather not have to check this here - but it seems that we might
2687 be passed a decl that should be in coalesced space. */
2688 if (one || weak)
2690 /* Weak or COMDAT objects are put in mergable sections. */
2691 darwin_emit_weak_or_comdat (fp, decl, name, size,
2692 DECL_ALIGN (decl));
2693 return;
2696 /* .. and it should be suitable for placement in local mem. */
2697 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2698 /* .. and any initializer must be all-zero. */
2699 gcc_assert ((DECL_INITIAL (decl) == NULL)
2700 || (DECL_INITIAL (decl) == error_mark_node)
2701 || initializer_zerop (DECL_INITIAL (decl)));
2703 last_assemble_variable_decl = decl;
2705 if (!size || !align)
2706 align = DECL_ALIGN (decl);
2708 l2align = floor_log2 (align / BITS_PER_UNIT);
2709 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2711 darwin_emit_local_bss (fp, decl, name, size, l2align);
2714 /* Emit an assembler directive to set visibility for a symbol. The
2715 only supported visibilities are VISIBILITY_DEFAULT and
2716 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2717 extern". There is no MACH-O equivalent of ELF's
2718 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2720 void
2721 darwin_assemble_visibility (tree decl, int vis)
2723 if (vis == VISIBILITY_DEFAULT)
2725 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2727 fputs ("\t.private_extern ", asm_out_file);
2728 assemble_name (asm_out_file,
2729 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2730 fputs ("\n", asm_out_file);
2732 else
2733 warning (OPT_Wattributes, "protected visibility attribute "
2734 "not supported in this configuration; ignored");
2737 /* vec used by darwin_asm_dwarf_section.
2738 Maybe a hash tab would be better here - but the intention is that this is
2739 a very short list (fewer than 16 items) and each entry should (ideally,
2740 eventually) only be presented once.
2742 A structure to hold a dwarf debug section used entry. */
2744 typedef struct GTY(()) dwarf_sect_used_entry {
2745 const char *name;
2746 unsigned count;
2748 dwarf_sect_used_entry;
2751 /* A list of used __DWARF sections. */
2752 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2754 /* This is called when we are asked to assemble a named section and the
2755 name begins with __DWARF,. We keep a list of the section names (without
2756 the __DWARF, prefix) and use this to emit our required start label on the
2757 first switch to each section. */
2759 static void
2760 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2761 tree ARG_UNUSED (decl))
2763 unsigned i;
2764 int namelen;
2765 const char * sname;
2766 dwarf_sect_used_entry *ref;
2767 bool found = false;
2768 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2769 == (SECTION_DEBUG | SECTION_NAMED));
2770 /* We know that the name starts with __DWARF, */
2771 sname = name + 8;
2772 namelen = strchr (sname, ',') - sname;
2773 gcc_assert (namelen);
2774 if (dwarf_sect_names_table == NULL)
2775 vec_alloc (dwarf_sect_names_table, 16);
2776 else
2777 for (i = 0;
2778 dwarf_sect_names_table->iterate (i, &ref);
2779 i++)
2781 if (!ref)
2782 break;
2783 if (!strcmp (ref->name, sname))
2785 found = true;
2786 ref->count++;
2787 break;
2791 fprintf (asm_out_file, "\t.section %s\n", name);
2792 if (!found)
2794 dwarf_sect_used_entry e;
2795 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2796 e.count = 1;
2797 e.name = xstrdup (sname);
2798 vec_safe_push (dwarf_sect_names_table, e);
2802 /* Output a difference of two labels that will be an assembly time
2803 constant if the two labels are local. (.long lab1-lab2 will be
2804 very different if lab1 is at the boundary between two sections; it
2805 will be relocated according to the second section, not the first,
2806 so one ends up with a difference between labels in different
2807 sections, which is bad in the dwarf2 eh context for instance.) */
2809 static int darwin_dwarf_label_counter;
2811 void
2812 darwin_asm_output_dwarf_delta (FILE *file, int size,
2813 const char *lab1, const char *lab2)
2815 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2816 && lab2[0] == '*' && lab2[1] == 'L');
2817 const char *directive = (size == 8 ? ".quad" : ".long");
2819 if (islocaldiff)
2820 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2821 else
2822 fprintf (file, "\t%s\t", directive);
2824 assemble_name_raw (file, lab1);
2825 fprintf (file, "-");
2826 assemble_name_raw (file, lab2);
2827 if (islocaldiff)
2828 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2831 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2832 offsets are not represented using relocs in .o files; either the
2833 section never leaves the .o file, or the linker or other tool is
2834 responsible for parsing the DWARF and updating the offsets. */
2836 void
2837 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2838 section *base)
2840 char sname[64];
2841 int namelen;
2843 gcc_assert (base->common.flags & SECTION_NAMED);
2844 gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
2845 gcc_assert (strchr (base->named.name + 8, ','));
2847 namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
2848 sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
2849 darwin_asm_output_dwarf_delta (file, size, lab, sname);
2852 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2854 void
2855 darwin_file_start (void)
2857 /* Nothing to do. */
2860 /* Called for the TARGET_ASM_FILE_END hook.
2861 Emit the mach-o pic indirection data, the lto data and, finally a flag
2862 to tell the linker that it can break the file object into sections and
2863 move those around for efficiency. */
2865 void
2866 darwin_file_end (void)
2868 if (!vec_safe_is_empty (ctors))
2869 finalize_ctors ();
2870 if (!vec_safe_is_empty (dtors))
2871 finalize_dtors ();
2873 /* If we are expecting to output NeXT ObjC meta-data, (and we actually see
2874 some) then we output the fix-and-continue marker (Image Info).
2875 This applies to Objective C, Objective C++ and LTO with either language
2876 as part of the input. */
2877 if (flag_next_runtime && objc_metadata_seen)
2879 unsigned int flags = 0;
2880 if (flag_objc_abi >= 2)
2882 flags = 16;
2883 output_section_asm_op
2884 (darwin_sections[objc2_image_info_section]->unnamed.data);
2886 else
2887 output_section_asm_op
2888 (darwin_sections[objc_image_info_section]->unnamed.data);
2890 ASM_OUTPUT_ALIGN (asm_out_file, 2);
2891 fputs ("L_OBJC_ImageInfo:\n", asm_out_file);
2893 flags |= (flag_replace_objc_classes && classes_seen) ? 1 : 0;
2894 flags |= flag_objc_gc ? 2 : 0;
2896 fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags);
2899 machopic_finish (asm_out_file);
2900 if (strcmp (lang_hooks.name, "GNU C++") == 0)
2902 switch_to_section (darwin_sections[constructor_section]);
2903 switch_to_section (darwin_sections[destructor_section]);
2904 ASM_OUTPUT_ALIGN (asm_out_file, 1);
2907 /* If there was LTO assembler output, append it to asm_out_file. */
2908 if (lto_asm_out_name)
2910 int n;
2911 char *buf, *lto_asm_txt;
2913 /* Shouldn't be here if we failed to switch back. */
2914 gcc_assert (! saved_asm_out_file);
2916 lto_asm_out_file = fopen (lto_asm_out_name, "r");
2917 if (lto_asm_out_file == NULL)
2918 fatal_error ("failed to open temporary file %s with LTO output",
2919 lto_asm_out_name);
2920 fseek (lto_asm_out_file, 0, SEEK_END);
2921 n = ftell (lto_asm_out_file);
2922 if (n > 0)
2924 fseek (lto_asm_out_file, 0, SEEK_SET);
2925 lto_asm_txt = buf = (char *) xmalloc (n + 1);
2926 while (fgets (lto_asm_txt, n, lto_asm_out_file))
2927 fputs (lto_asm_txt, asm_out_file);
2928 /* Put a termination label. */
2929 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2930 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2931 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2932 lto_section_num);
2933 /* Make sure our termination label stays in this section. */
2934 fputs ("\t.space\t1\n", asm_out_file);
2937 /* Remove the temporary file. */
2938 fclose (lto_asm_out_file);
2939 unlink_if_ordinary (lto_asm_out_name);
2940 free (lto_asm_out_name);
2943 /* Output the names and indices. */
2944 if (lto_section_names && lto_section_names->length ())
2946 int count;
2947 darwin_lto_section_e *ref;
2948 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
2949 the latter up ourselves. */
2950 const char *op = integer_asm_op (4,0);
2952 /* Emit the names. */
2953 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2954 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2955 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2957 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
2958 /* We have to jump through hoops to get the values of the intra-section
2959 offsets... */
2960 fprintf (asm_out_file,
2961 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
2962 count, count);
2963 fprintf (asm_out_file,
2964 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
2965 count, count+1, count);
2966 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
2968 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
2969 /* make sure our termination label stays in this section. */
2970 fputs ("\t.space\t1\n", asm_out_file);
2972 /* Emit the Index. */
2973 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2974 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
2975 fputs ("\t.align\t2\n", asm_out_file);
2976 fputs ("# Section offset, Section length, Name offset, Name length\n",
2977 asm_out_file);
2978 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2980 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
2981 op, count, ref->sectname);
2982 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
2983 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
2984 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
2988 /* If we have section anchors, then we must prevent the linker from
2989 re-arranging data. */
2990 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2991 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
2994 /* TODO: Add a language hook for identifying if a decl is a vtable. */
2995 #define DARWIN_VTABLE_P(DECL) 0
2997 /* Cross-module name binding. Darwin does not support overriding
2998 functions at dynamic-link time, except for vtables in kexts. */
3000 bool
3001 darwin_binds_local_p (const_tree decl)
3003 return default_binds_local_p_1 (decl,
3004 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
3007 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
3008 anchor relative to ".", the current section position. We cannot use
3009 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
3010 void
3011 darwin_asm_output_anchor (rtx symbol)
3013 fprintf (asm_out_file, "\t.set\t");
3014 assemble_name (asm_out_file, XSTR (symbol, 0));
3015 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
3016 SYMBOL_REF_BLOCK_OFFSET (symbol));
3019 /* Disable section anchoring on any section containing a zero-sized
3020 object. */
3021 bool
3022 darwin_use_anchors_for_symbol_p (const_rtx symbol)
3024 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
3026 section *sect;
3027 /* If the section contains a zero-sized object it's ineligible. */
3028 sect = SYMBOL_REF_BLOCK (symbol)->sect;
3029 /* This should have the effect of disabling anchors for vars that follow
3030 any zero-sized one, in a given section. */
3031 if (sect->common.flags & SECTION_NO_ANCHOR)
3032 return false;
3034 /* Also check the normal reasons for suppressing. */
3035 return default_use_anchors_for_symbol_p (symbol);
3037 else
3038 return false;
3041 /* Set the darwin specific attributes on TYPE. */
3042 void
3043 darwin_set_default_type_attributes (tree type)
3045 if (darwin_ms_struct
3046 && TREE_CODE (type) == RECORD_TYPE)
3047 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
3048 NULL_TREE,
3049 TYPE_ATTRIBUTES (type));
3052 /* True, iff we're generating code for loadable kernel extensions. */
3054 bool
3055 darwin_kextabi_p (void) {
3056 return flag_apple_kext;
3059 void
3060 darwin_override_options (void)
3062 /* Keep track of which (major) version we're generating code for. */
3063 if (darwin_macosx_version_min)
3065 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
3066 generating_for_darwin_version = 10;
3067 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
3068 generating_for_darwin_version = 9;
3070 /* Earlier versions are not specifically accounted, until required. */
3073 /* In principle, this should be c-family only. However, we really need to
3074 set sensible defaults for LTO as well, since the section selection stuff
3075 should check for correctness re. the ABI. TODO: check and provide the
3076 flags (runtime & ABI) from the lto wrapper). */
3078 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
3079 if (!global_options_set.x_flag_objc_abi)
3080 global_options.x_flag_objc_abi
3081 = (!flag_next_runtime)
3083 : (TARGET_64BIT ? 2
3084 : (generating_for_darwin_version >= 9) ? 1
3085 : 0);
3087 /* Objective-C family ABI 2 is only valid for next/m64 at present. */
3088 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
3090 if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
3091 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
3092 " used for %<-m64%> targets with"
3093 " %<-fnext-runtime%>");
3094 if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
3095 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
3096 " supported on %<-m32%> targets with"
3097 " %<-fnext-runtime%>");
3100 /* Don't emit DWARF3/4 unless specifically selected. This is a
3101 workaround for tool bugs. */
3102 if (!global_options_set.x_dwarf_strict)
3103 dwarf_strict = 1;
3104 if (!global_options_set.x_dwarf_version)
3105 dwarf_version = 2;
3107 /* Do not allow unwind tables to be generated by default for m32.
3108 fnon-call-exceptions will override this, regardless of what we do. */
3109 if (generating_for_darwin_version < 10
3110 && !global_options_set.x_flag_asynchronous_unwind_tables
3111 && !TARGET_64BIT)
3112 global_options.x_flag_asynchronous_unwind_tables = 0;
3114 /* Disable -freorder-blocks-and-partition when unwind tables are being
3115 emitted for Darwin < 9 (OSX 10.5).
3116 The strategy is, "Unless the User has specifically set/unset an unwind
3117 flag we will switch off -freorder-blocks-and-partition when unwind tables
3118 will be generated". If the User specifically sets flags... we assume
3119 (s)he knows why... */
3120 if (generating_for_darwin_version < 9
3121 && global_options_set.x_flag_reorder_blocks_and_partition
3122 && ((global_options.x_flag_exceptions /* User, c++, java */
3123 && !global_options_set.x_flag_exceptions) /* User specified... */
3124 || (global_options.x_flag_unwind_tables
3125 && !global_options_set.x_flag_unwind_tables)
3126 || (global_options.x_flag_non_call_exceptions
3127 && !global_options_set.x_flag_non_call_exceptions)
3128 || (global_options.x_flag_asynchronous_unwind_tables
3129 && !global_options_set.x_flag_asynchronous_unwind_tables)))
3131 inform (input_location,
3132 "-freorder-blocks-and-partition does not work with exceptions "
3133 "on this architecture");
3134 flag_reorder_blocks_and_partition = 0;
3135 flag_reorder_blocks = 1;
3138 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3139 one valid choice of exception scheme for each runtime. */
3140 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3141 global_options.x_flag_objc_sjlj_exceptions =
3142 flag_next_runtime && !TARGET_64BIT;
3144 /* FIXME: and this could be eliminated then too. */
3145 if (!global_options_set.x_flag_exceptions
3146 && flag_objc_exceptions
3147 && TARGET_64BIT)
3148 flag_exceptions = 1;
3150 if (flag_mkernel || flag_apple_kext)
3152 /* -mkernel implies -fapple-kext for C++ */
3153 if (strcmp (lang_hooks.name, "GNU C++") == 0)
3154 flag_apple_kext = 1;
3156 flag_no_common = 1;
3158 /* No EH in kexts. */
3159 flag_exceptions = 0;
3160 /* No -fnon-call-exceptions data in kexts. */
3161 flag_non_call_exceptions = 0;
3162 /* so no tables either.. */
3163 flag_unwind_tables = 0;
3164 flag_asynchronous_unwind_tables = 0;
3165 /* We still need to emit branch islands for kernel context. */
3166 darwin_emit_branch_islands = true;
3169 if (flag_var_tracking_uninit == 0
3170 && generating_for_darwin_version >= 9
3171 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3172 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3173 && write_symbols == DWARF2_DEBUG)
3174 flag_var_tracking_uninit = flag_var_tracking;
3176 if (MACHO_DYNAMIC_NO_PIC_P)
3178 if (flag_pic)
3179 warning_at (UNKNOWN_LOCATION, 0,
3180 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3181 " %<-fpie%> or %<-fPIE%>");
3182 flag_pic = 0;
3184 else if (flag_pic == 1)
3186 /* Darwin's -fpic is -fPIC. */
3187 flag_pic = 2;
3190 /* It is assumed that branch island stubs are needed for earlier systems. */
3191 if (generating_for_darwin_version < 9)
3192 darwin_emit_branch_islands = true;
3193 else
3194 emit_aligned_common = true; /* Later systems can support aligned common. */
3196 /* The c_dialect...() macros are not available to us here. */
3197 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3200 #if DARWIN_PPC
3201 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3203 static void
3204 darwin_patch_builtin (enum built_in_function fncode)
3206 tree fn = builtin_decl_explicit (fncode);
3207 tree sym;
3208 char *newname;
3210 if (!fn)
3211 return;
3213 sym = DECL_ASSEMBLER_NAME (fn);
3214 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3216 set_user_assembler_name (fn, newname);
3218 fn = builtin_decl_implicit (fncode);
3219 if (fn)
3220 set_user_assembler_name (fn, newname);
3223 void
3224 darwin_patch_builtins (void)
3226 if (LONG_DOUBLE_TYPE_SIZE != 128)
3227 return;
3229 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3230 #define PATCH_BUILTIN_NO64(fncode) \
3231 if (!TARGET_64BIT) \
3232 darwin_patch_builtin (fncode);
3233 #define PATCH_BUILTIN_VARIADIC(fncode) \
3234 if (!TARGET_64BIT \
3235 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3236 darwin_patch_builtin (fncode);
3237 #include "darwin-ppc-ldouble-patch.def"
3238 #undef PATCH_BUILTIN
3239 #undef PATCH_BUILTIN_NO64
3240 #undef PATCH_BUILTIN_VARIADIC
3242 #endif
3244 /* CFStrings implementation. */
3245 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3246 static GTY(()) tree cfstring_type_node = NULL_TREE;
3247 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3248 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3249 static GTY(()) tree pcint_type_node = NULL_TREE;
3250 static GTY(()) tree pcchar_type_node = NULL_TREE;
3252 static enum built_in_function darwin_builtin_cfstring;
3254 /* Store all constructed constant CFStrings in a hash table so that
3255 they get uniqued properly. */
3257 typedef struct GTY ((for_user)) cfstring_descriptor {
3258 /* The string literal. */
3259 tree literal;
3260 /* The resulting constant CFString. */
3261 tree constructor;
3262 } cfstring_descriptor;
3264 struct cfstring_hasher : ggc_hasher<cfstring_descriptor *>
3266 static hashval_t hash (cfstring_descriptor *);
3267 static bool equal (cfstring_descriptor *, cfstring_descriptor *);
3270 static GTY (()) hash_table<cfstring_hasher> *cfstring_htab;
3272 static tree
3273 add_builtin_field_decl (tree type, const char *name, tree **chain)
3275 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3276 get_identifier (name), type);
3278 if (*chain != NULL)
3279 **chain = field;
3280 *chain = &DECL_CHAIN (field);
3282 return field;
3285 tree
3286 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3288 tree cfsfun, fields, pccfstring_ftype_pcchar;
3289 tree *chain = NULL;
3291 darwin_builtin_cfstring =
3292 (enum built_in_function) builtin_cfstring;
3294 /* struct __builtin_CFString {
3295 const int *isa; (will point at
3296 int flags; __CFConstantStringClassReference)
3297 const char *str;
3298 long length;
3299 }; */
3301 pcint_type_node = build_pointer_type
3302 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3304 pcchar_type_node = build_pointer_type
3305 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3307 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3309 /* Have to build backwards for finish struct. */
3310 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3311 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3312 add_builtin_field_decl (integer_type_node, "flags", &chain);
3313 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3314 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3315 fields, NULL_TREE);
3317 /* const struct __builtin_CFstring *
3318 __builtin___CFStringMakeConstantString (const char *); */
3320 ccfstring_type_node = build_qualified_type
3321 (cfstring_type_node, TYPE_QUAL_CONST);
3322 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3323 pccfstring_ftype_pcchar = build_function_type_list
3324 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3326 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3327 get_identifier ("__builtin___CFStringMakeConstantString"),
3328 pccfstring_ftype_pcchar);
3330 TREE_PUBLIC (cfsfun) = 1;
3331 DECL_EXTERNAL (cfsfun) = 1;
3332 DECL_ARTIFICIAL (cfsfun) = 1;
3333 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3334 in place of the existing, which may be NULL. */
3335 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3336 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3337 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
3338 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
3339 lang_hooks.builtin_function (cfsfun);
3341 /* extern int __CFConstantStringClassReference[]; */
3342 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3343 get_identifier ("__CFConstantStringClassReference"),
3344 build_array_type (integer_type_node, NULL_TREE));
3346 TREE_PUBLIC (cfstring_class_reference) = 1;
3347 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3348 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3349 DECL_EXTERNAL (cfstring_class_reference) = 1;
3350 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3352 /* Initialize the hash table used to hold the constant CFString objects. */
3353 cfstring_htab = hash_table<cfstring_hasher>::create_ggc (31);
3355 return cfstring_type_node;
3358 tree
3359 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3360 bool ARG_UNUSED (ignore))
3362 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3364 if (fcode == darwin_builtin_cfstring)
3366 if (!darwin_constant_cfstrings)
3368 error ("built-in function %qD requires the"
3369 " %<-mconstant-cfstrings%> flag", fndecl);
3370 return error_mark_node;
3373 if (n_args != 1)
3375 error ("built-in function %qD takes one argument only", fndecl);
3376 return error_mark_node;
3379 return darwin_build_constant_cfstring (*argp);
3382 return NULL_TREE;
3385 void
3386 darwin_rename_builtins (void)
3388 /* The system ___divdc3 routine in libSystem on darwin10 is not
3389 accurate to 1ulp, ours is, so we avoid ever using the system name
3390 for this routine and instead install a non-conflicting name that
3391 is accurate.
3393 When -ffast-math or -funsafe-math-optimizations is given, we can
3394 use the faster version. */
3395 if (!flag_unsafe_math_optimizations)
3397 enum built_in_function dcode
3398 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3399 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3400 tree fn = builtin_decl_explicit (dcode);
3401 /* Fortran and c call TARGET_INIT_BUILTINS and
3402 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3403 call into each to ensure that at least one of them is called
3404 after build_common_builtin_nodes. A better fix is to add a
3405 new hook to run after build_common_builtin_nodes runs. */
3406 if (fn)
3407 set_user_assembler_name (fn, "___ieee_divdc3");
3408 fn = builtin_decl_implicit (dcode);
3409 if (fn)
3410 set_user_assembler_name (fn, "___ieee_divdc3");
3414 bool
3415 darwin_libc_has_function (enum function_class fn_class)
3417 if (fn_class == function_sincos)
3418 return false;
3419 if (fn_class == function_c99_math_complex
3420 || fn_class == function_c99_misc)
3421 return (TARGET_64BIT
3422 || strverscmp (darwin_macosx_version_min, "10.3") >= 0);
3424 return true;
3427 hashval_t
3428 cfstring_hasher::hash (cfstring_descriptor *ptr)
3430 tree str = ptr->literal;
3431 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3432 int i, len = TREE_STRING_LENGTH (str);
3433 hashval_t h = len;
3435 for (i = 0; i < len; i++)
3436 h = ((h * 613) + p[i]);
3438 return h;
3441 bool
3442 cfstring_hasher::equal (cfstring_descriptor *ptr1, cfstring_descriptor *ptr2)
3444 tree str1 = ptr1->literal;
3445 tree str2 = ptr2->literal;
3446 int len1 = TREE_STRING_LENGTH (str1);
3448 return (len1 == TREE_STRING_LENGTH (str2)
3449 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3450 len1));
3453 tree
3454 darwin_build_constant_cfstring (tree str)
3456 struct cfstring_descriptor *desc, key;
3457 tree addr;
3459 if (!str)
3461 error ("CFString literal is missing");
3462 return error_mark_node;
3465 STRIP_NOPS (str);
3467 if (TREE_CODE (str) == ADDR_EXPR)
3468 str = TREE_OPERAND (str, 0);
3470 if (TREE_CODE (str) != STRING_CST)
3472 error ("CFString literal expression is not a string constant");
3473 return error_mark_node;
3476 /* Perhaps we already constructed a constant CFString just like this one? */
3477 key.literal = str;
3478 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
3479 desc = *loc;
3481 if (!desc)
3483 tree var, constructor, field;
3484 vec<constructor_elt, va_gc> *v = NULL;
3485 int length = TREE_STRING_LENGTH (str) - 1;
3487 if (darwin_warn_nonportable_cfstrings)
3489 const char *s = TREE_STRING_POINTER (str);
3490 int l = 0;
3492 for (l = 0; l < length; l++)
3493 if (!s[l] || !isascii (s[l]))
3495 warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
3496 s[l] ? "non-ASCII character" : "embedded NUL");
3497 break;
3501 *loc = desc = ggc_cleared_alloc<cfstring_descriptor> ();
3502 desc->literal = str;
3504 /* isa *. */
3505 field = TYPE_FIELDS (ccfstring_type_node);
3506 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3507 build1 (ADDR_EXPR, TREE_TYPE (field),
3508 cfstring_class_reference));
3509 /* flags */
3510 field = DECL_CHAIN (field);
3511 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3512 build_int_cst (TREE_TYPE (field), 0x000007c8));
3513 /* string *. */
3514 field = DECL_CHAIN (field);
3515 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3516 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3517 /* length */
3518 field = DECL_CHAIN (field);
3519 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3520 build_int_cst (TREE_TYPE (field), length));
3522 constructor = build_constructor (ccfstring_type_node, v);
3523 TREE_READONLY (constructor) = 1;
3524 TREE_CONSTANT (constructor) = 1;
3525 TREE_STATIC (constructor) = 1;
3527 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3528 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3529 being built without any knowledge of C++ tree accessors; hence, we shall
3530 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3531 if (darwin_running_cxx)
3532 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3534 /* Create an anonymous global variable for this CFString. */
3535 var = build_decl (input_location, CONST_DECL,
3536 NULL, TREE_TYPE (constructor));
3537 DECL_ARTIFICIAL (var) = 1;
3538 TREE_STATIC (var) = 1;
3539 DECL_INITIAL (var) = constructor;
3540 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3541 DECL_CONTEXT (var) = NULL_TREE;
3542 desc->constructor = var;
3545 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3546 TREE_CONSTANT (addr) = 1;
3548 return addr;
3551 bool
3552 darwin_cfstring_p (tree str)
3554 struct cfstring_descriptor key;
3556 if (!str)
3557 return false;
3559 STRIP_NOPS (str);
3561 if (TREE_CODE (str) == ADDR_EXPR)
3562 str = TREE_OPERAND (str, 0);
3564 if (TREE_CODE (str) != STRING_CST)
3565 return false;
3567 key.literal = str;
3568 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, NO_INSERT);
3570 if (loc)
3571 return true;
3573 return false;
3576 void
3577 darwin_enter_string_into_cfstring_table (tree str)
3579 struct cfstring_descriptor key;
3581 key.literal = str;
3582 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
3584 if (!*loc)
3586 *loc = ggc_cleared_alloc<cfstring_descriptor> ();
3587 ((struct cfstring_descriptor *)*loc)->literal = str;
3591 /* Choose named function section based on its frequency. */
3593 section *
3594 darwin_function_section (tree decl, enum node_frequency freq,
3595 bool startup, bool exit)
3597 /* Decide if we need to put this in a coalescable section. */
3598 bool weak = (decl
3599 && DECL_WEAK (decl)
3600 && (!DECL_ATTRIBUTES (decl)
3601 || !lookup_attribute ("weak_import",
3602 DECL_ATTRIBUTES (decl))));
3604 /* If there is a specified section name, we should not be trying to
3605 override. */
3606 if (decl && DECL_SECTION_NAME (decl) != NULL)
3607 return get_named_section (decl, NULL, 0);
3609 /* We always put unlikely executed stuff in the cold section. */
3610 if (freq == NODE_FREQUENCY_UNLIKELY_EXECUTED)
3611 return (weak) ? darwin_sections[text_cold_coal_section]
3612 : darwin_sections[text_cold_section];
3614 /* If we have LTO *and* feedback information, then let LTO handle
3615 the function ordering, it makes a better job (for normal, hot,
3616 startup and exit - hence the bailout for cold above). */
3617 if (in_lto_p && flag_profile_values)
3618 goto default_function_sections;
3620 /* Non-cold startup code should go to startup subsection. */
3621 if (startup)
3622 return (weak) ? darwin_sections[text_startup_coal_section]
3623 : darwin_sections[text_startup_section];
3625 /* Similarly for exit. */
3626 if (exit)
3627 return (weak) ? darwin_sections[text_exit_coal_section]
3628 : darwin_sections[text_exit_section];
3630 /* Place hot code. */
3631 if (freq == NODE_FREQUENCY_HOT)
3632 return (weak) ? darwin_sections[text_hot_coal_section]
3633 : darwin_sections[text_hot_section];
3635 /* Otherwise, default to the 'normal' non-reordered sections. */
3636 default_function_sections:
3637 return (weak) ? darwin_sections[text_coal_section]
3638 : text_section;
3641 /* When a function is partitioned between sections, we need to insert a label
3642 at the start of each new chunk - so that it may become a valid 'atom' for
3643 eh and debug purposes. Without this the linker will emit warnings if one
3644 tries to add line location information (since the switched fragment will
3645 be anonymous). */
3647 void
3648 darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
3650 char buf[128];
3651 snprintf (buf, 128, "%s%s",new_is_cold?"__cold_sect_of_":"__hot_sect_of_",
3652 IDENTIFIER_POINTER (DECL_NAME (decl)));
3653 /* Make sure we pick up all the relevant quotes etc. */
3654 assemble_name_raw (fp, (const char *) buf);
3655 fputs (":\n", fp);
3658 #include "gt-darwin.h"