gcc/
[official-gcc.git] / gcc / config / darwin.c
blobadf370d4d072febcf085da6138cc67889159f807
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 "function.h"
41 #include "ggc.h"
42 #include "langhooks.h"
43 #include "target.h"
44 #include "tm_p.h"
45 #include "diagnostic-core.h"
46 #include "toplev.h"
47 #include "hashtab.h"
48 #include "df.h"
49 #include "debug.h"
50 #include "obstack.h"
51 #include "pointer-set.h"
52 #include "hash-table.h"
53 #include "vec.h"
54 #include "basic-block.h"
55 #include "tree-ssa-alias.h"
56 #include "internal-fn.h"
57 #include "gimple-fold.h"
58 #include "tree-eh.h"
59 #include "gimple-expr.h"
60 #include "is-a.h"
61 #include "gimple.h"
62 #include "gimplify.h"
63 #include "lto-streamer.h"
65 /* Darwin supports a feature called fix-and-continue, which is used
66 for rapid turn around debugging. When code is compiled with the
67 -mfix-and-continue flag, two changes are made to the generated code
68 that allow the system to do things that it would normally not be
69 able to do easily. These changes allow gdb to load in
70 recompilation of a translation unit that has been changed into a
71 running program and replace existing functions and methods of that
72 translation unit with versions of those functions and methods
73 from the newly compiled translation unit. The new functions access
74 the existing static symbols from the old translation unit, if the
75 symbol existed in the unit to be replaced, and from the new
76 translation unit, otherwise.
78 The changes are to insert 5 nops at the beginning of all functions
79 and to use indirection to get at static symbols. The 5 nops
80 are required by consumers of the generated code. Currently, gdb
81 uses this to patch in a jump to the overriding function, this
82 allows all uses of the old name to forward to the replacement,
83 including existing function pointers and virtual methods. See
84 rs6000_emit_prologue for the code that handles the nop insertions.
86 The added indirection allows gdb to redirect accesses to static
87 symbols from the newly loaded translation unit to the existing
88 symbol, if any. @code{static} symbols are special and are handled by
89 setting the second word in the .non_lazy_symbol_pointer data
90 structure to symbol. See indirect_data for the code that handles
91 the extra indirection, and machopic_output_indirection and its use
92 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
93 symbol indirection. */
95 /* For darwin >= 9 (OSX 10.5) the linker is capable of making the necessary
96 branch islands and we no longer need to emit darwin stubs.
97 However, if we are generating code for earlier systems (or for use in the
98 kernel) the stubs might still be required, and this will be set true. */
99 int darwin_emit_branch_islands = false;
101 typedef struct GTY(()) cdtor_record {
102 rtx symbol;
103 int priority; /* [con/de]structor priority */
104 int position; /* original position */
105 } cdtor_record;
107 static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL;
108 static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL;
110 /* A flag to determine whether we are running c++ or obj-c++. This has to be
111 settable from non-c-family contexts too (i.e. we can't use the c_dialect_
112 functions). */
113 int darwin_running_cxx;
115 /* Some code-gen now depends on OS major version numbers (at least). */
116 int generating_for_darwin_version ;
118 /* Section names. */
119 section * darwin_sections[NUM_DARWIN_SECTIONS];
121 /* While we transition to using in-tests instead of ifdef'd code. */
122 #ifndef HAVE_lo_sum
123 #define HAVE_lo_sum 0
124 #define gen_macho_high(a,b) (a)
125 #define gen_macho_low(a,b,c) (a)
126 #endif
128 /* True if we're setting __attribute__ ((ms_struct)). */
129 int darwin_ms_struct = false;
131 /* Earlier versions of Darwin as do not recognize an alignment field in
132 .comm directives, this should be set for versions that allow it. */
133 int emit_aligned_common = false;
135 /* A get_unnamed_section callback used to switch to an ObjC section.
136 DIRECTIVE is as for output_section_asm_op. */
138 static void
139 output_objc_section_asm_op (const void *directive)
141 static bool been_here = false;
143 /* The NeXT ObjC Runtime requires these sections to be present and in
144 order in the object. The code below implements this by emitting
145 a section header for each ObjC section the first time that an ObjC
146 section is requested. */
147 if (! been_here)
149 section *saved_in_section = in_section;
150 static const enum darwin_section_enum tomark[] =
152 /* written, cold -> hot */
153 objc_cat_cls_meth_section,
154 objc_cat_inst_meth_section,
155 objc_string_object_section,
156 objc_constant_string_object_section,
157 objc_selector_refs_section,
158 objc_selector_fixup_section,
159 objc_cls_refs_section,
160 objc_class_section,
161 objc_meta_class_section,
162 /* shared, hot -> cold */
163 objc_cls_meth_section,
164 objc_inst_meth_section,
165 objc_protocol_section,
166 objc_class_names_section,
167 objc_meth_var_types_section,
168 objc_meth_var_names_section,
169 objc_category_section,
170 objc_class_vars_section,
171 objc_instance_vars_section,
172 objc_module_info_section,
173 objc_symbols_section,
175 /* ABI=1 */
176 static const enum darwin_section_enum tomarkv1[] =
178 objc1_protocol_ext_section,
179 objc1_class_ext_section,
180 objc1_prop_list_section
182 /* ABI=2 */
183 static const enum darwin_section_enum tomarkv2[] =
185 objc2_message_refs_section,
186 objc2_classdefs_section,
187 objc2_metadata_section,
188 objc2_classrefs_section,
189 objc2_classlist_section,
190 objc2_categorylist_section,
191 objc2_selector_refs_section,
192 objc2_nonlazy_class_section,
193 objc2_nonlazy_category_section,
194 objc2_protocollist_section,
195 objc2_protocolrefs_section,
196 objc2_super_classrefs_section,
197 objc2_image_info_section,
198 objc2_constant_string_object_section
200 size_t i;
202 been_here = true;
203 if (flag_objc_abi < 2)
205 for (i = 0; i < ARRAY_SIZE (tomark); i++)
206 switch_to_section (darwin_sections[tomark[i]]);
207 if (flag_objc_abi == 1)
208 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
209 switch_to_section (darwin_sections[tomarkv1[i]]);
211 else
212 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
213 switch_to_section (darwin_sections[tomarkv2[i]]);
214 /* Make sure we don't get varasm.c out of sync with us. */
215 switch_to_section (saved_in_section);
217 output_section_asm_op (directive);
221 /* Private flag applied to disable section-anchors in a particular section. */
222 #define SECTION_NO_ANCHOR SECTION_MACH_DEP
225 /* Implement TARGET_ASM_INIT_SECTIONS. */
227 void
228 darwin_init_sections (void)
230 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
231 darwin_sections[NAME] = \
232 get_unnamed_section (FLAGS, (OBJC \
233 ? output_objc_section_asm_op \
234 : output_section_asm_op), \
235 "\t" DIRECTIVE);
236 #include "config/darwin-sections.def"
237 #undef DEF_SECTION
239 readonly_data_section = darwin_sections[const_section];
240 exception_section = darwin_sections[darwin_exception_section];
241 eh_frame_section = darwin_sections[darwin_eh_frame_section];
245 name_needs_quotes (const char *name)
247 int c;
248 while ((c = *name++) != '\0')
249 if (! ISIDNUM (c)
250 && c != '.' && c != '$' && c != '_' )
251 return 1;
252 return 0;
255 /* Return true if SYM_REF can be used without an indirection. */
257 machopic_symbol_defined_p (rtx sym_ref)
259 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
260 return true;
262 /* If a symbol references local and is not an extern to this
263 file, then the symbol might be able to declared as defined. */
264 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
266 /* If the symbol references a variable and the variable is a
267 common symbol, then this symbol is not defined. */
268 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
270 tree decl = SYMBOL_REF_DECL (sym_ref);
271 if (!decl)
272 return true;
273 if (DECL_COMMON (decl))
274 return false;
276 return true;
278 return false;
281 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
282 reference, which will not be changed. */
284 enum machopic_addr_class
285 machopic_classify_symbol (rtx sym_ref)
287 bool function_p;
289 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
290 if (machopic_symbol_defined_p (sym_ref))
291 return (function_p
292 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
293 else
294 return (function_p
295 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
298 #ifndef TARGET_FIX_AND_CONTINUE
299 #define TARGET_FIX_AND_CONTINUE 0
300 #endif
302 /* Indicate when fix-and-continue style code generation is being used
303 and when a reference to data should be indirected so that it can be
304 rebound in a new translation unit to reference the original instance
305 of that data. Symbol names that are for code generation local to
306 the translation unit are bound to the new translation unit;
307 currently this means symbols that begin with L or _OBJC_;
308 otherwise, we indicate that an indirect reference should be made to
309 permit the runtime to rebind new instances of the translation unit
310 to the original instance of the data. */
312 static int
313 indirect_data (rtx sym_ref)
315 int lprefix;
316 const char *name;
318 /* If we aren't generating fix-and-continue code, don't do anything
319 special. */
320 if (TARGET_FIX_AND_CONTINUE == 0)
321 return 0;
323 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
324 are indirected. Symbols that begin with L and _OBJC_ are always
325 bound to the current translation unit as they are used for
326 generated local data of the translation unit. */
328 name = XSTR (sym_ref, 0);
330 lprefix = (((name[0] == '*' || name[0] == '&')
331 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
332 || (strncmp (name, "_OBJC_", 6) == 0));
334 return ! lprefix;
337 static int
338 machopic_data_defined_p (rtx sym_ref)
340 if (indirect_data (sym_ref))
341 return 0;
343 switch (machopic_classify_symbol (sym_ref))
345 case MACHOPIC_DEFINED_DATA:
346 case MACHOPIC_DEFINED_FUNCTION:
347 return 1;
348 default:
349 return 0;
353 void
354 machopic_define_symbol (rtx mem)
356 rtx sym_ref;
358 gcc_assert (GET_CODE (mem) == MEM);
359 sym_ref = XEXP (mem, 0);
360 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
363 /* Return either ORIG or:
365 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
367 depending on MACHO_DYNAMIC_NO_PIC_P. */
369 machopic_gen_offset (rtx orig)
371 if (MACHO_DYNAMIC_NO_PIC_P)
372 return orig;
373 else
375 /* Play games to avoid marking the function as needing pic if we
376 are being called as part of the cost-estimation process. */
377 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
378 crtl->uses_pic_offset_table = 1;
379 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
380 UNSPEC_MACHOPIC_OFFSET);
381 return gen_rtx_CONST (Pmode, orig);
385 static GTY(()) const char * function_base_func_name;
386 static GTY(()) int current_pic_label_num;
387 static GTY(()) int emitted_pic_label_num;
389 static void
390 update_pic_label_number_if_needed (void)
392 const char *current_name;
394 /* When we are generating _get_pc thunks within stubs, there is no current
395 function. */
396 if (current_function_decl)
398 current_name =
399 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
400 if (function_base_func_name != current_name)
402 ++current_pic_label_num;
403 function_base_func_name = current_name;
406 else
408 ++current_pic_label_num;
409 function_base_func_name = "L_machopic_stub_dummy";
413 void
414 machopic_output_function_base_name (FILE *file)
416 /* If dynamic-no-pic is on, we should not get here. */
417 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
419 update_pic_label_number_if_needed ();
420 fprintf (file, "L%d$pb", current_pic_label_num);
423 char curr_picbasename[32];
425 const char *
426 machopic_get_function_picbase (void)
428 /* If dynamic-no-pic is on, we should not get here. */
429 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
431 update_pic_label_number_if_needed ();
432 snprintf (curr_picbasename, 32, "L%d$pb", current_pic_label_num);
433 return (const char *) curr_picbasename;
436 bool
437 machopic_should_output_picbase_label (void)
439 update_pic_label_number_if_needed ();
441 if (current_pic_label_num == emitted_pic_label_num)
442 return false;
444 emitted_pic_label_num = current_pic_label_num;
445 return true;
448 /* The suffix attached to non-lazy pointer symbols. */
449 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
450 /* The suffix attached to stub symbols. */
451 #define STUB_SUFFIX "$stub"
453 typedef struct GTY (()) machopic_indirection
455 /* The SYMBOL_REF for the entity referenced. */
456 rtx symbol;
457 /* The name of the stub or non-lazy pointer. */
458 const char * ptr_name;
459 /* True iff this entry is for a stub (as opposed to a non-lazy
460 pointer). */
461 bool stub_p;
462 /* True iff this stub or pointer pointer has been referenced. */
463 bool used;
464 } machopic_indirection;
466 /* A table mapping stub names and non-lazy pointer names to
467 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
469 static GTY ((param_is (struct machopic_indirection))) htab_t
470 machopic_indirections;
472 /* Return a hash value for a SLOT in the indirections hash table. */
474 static hashval_t
475 machopic_indirection_hash (const void *slot)
477 const machopic_indirection *p = (const machopic_indirection *) slot;
478 return htab_hash_string (p->ptr_name);
481 /* Returns true if the KEY is the same as that associated with
482 SLOT. */
484 static int
485 machopic_indirection_eq (const void *slot, const void *key)
487 return strcmp (((const machopic_indirection *) slot)->ptr_name,
488 (const char *) key) == 0;
491 /* Return the name of the non-lazy pointer (if STUB_P is false) or
492 stub (if STUB_B is true) corresponding to the given name. */
494 const char *
495 machopic_indirection_name (rtx sym_ref, bool stub_p)
497 char *buffer;
498 const char *name = XSTR (sym_ref, 0);
499 size_t namelen = strlen (name);
500 machopic_indirection *p;
501 void ** slot;
502 bool needs_quotes;
503 const char *suffix;
504 const char *prefix = user_label_prefix;
505 const char *quote = "";
506 tree id;
508 id = maybe_get_identifier (name);
509 if (id)
511 tree id_orig = id;
513 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
514 id = TREE_CHAIN (id);
515 if (id != id_orig)
517 name = IDENTIFIER_POINTER (id);
518 namelen = strlen (name);
522 if (name[0] == '*')
524 prefix = "";
525 ++name;
526 --namelen;
529 needs_quotes = name_needs_quotes (name);
530 if (needs_quotes)
532 quote = "\"";
535 if (stub_p)
536 suffix = STUB_SUFFIX;
537 else
538 suffix = NON_LAZY_POINTER_SUFFIX;
540 buffer = XALLOCAVEC (char, strlen ("&L")
541 + strlen (prefix)
542 + namelen
543 + strlen (suffix)
544 + 2 * strlen (quote)
545 + 1 /* '\0' */);
547 /* Construct the name of the non-lazy pointer or stub. */
548 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
550 if (!machopic_indirections)
551 machopic_indirections = htab_create_ggc (37,
552 machopic_indirection_hash,
553 machopic_indirection_eq,
554 /*htab_del=*/NULL);
556 slot = htab_find_slot_with_hash (machopic_indirections, buffer,
557 htab_hash_string (buffer), INSERT);
558 if (*slot)
560 p = (machopic_indirection *) *slot;
562 else
564 p = ggc_alloc_machopic_indirection ();
565 p->symbol = sym_ref;
566 p->ptr_name = xstrdup (buffer);
567 p->stub_p = stub_p;
568 p->used = false;
569 *slot = p;
572 return p->ptr_name;
575 /* Return the name of the stub for the mcount function. */
577 const char*
578 machopic_mcount_stub_name (void)
580 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
581 return machopic_indirection_name (symbol, /*stub_p=*/true);
584 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
585 or non-lazy pointer as used -- and mark the object to which the
586 pointer/stub refers as used as well, since the pointer/stub will
587 emit a reference to it. */
589 void
590 machopic_validate_stub_or_non_lazy_ptr (const char *name)
592 machopic_indirection *p;
594 p = ((machopic_indirection *)
595 (htab_find_with_hash (machopic_indirections, name,
596 htab_hash_string (name))));
597 if (p && ! p->used)
599 const char *real_name;
600 tree id;
602 p->used = true;
604 /* Do what output_addr_const will do when we actually call it. */
605 if (SYMBOL_REF_DECL (p->symbol))
606 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
608 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
610 id = maybe_get_identifier (real_name);
611 if (id)
612 mark_referenced (id);
616 /* Transform ORIG, which may be any data source, to the corresponding
617 source using indirections. */
620 machopic_indirect_data_reference (rtx orig, rtx reg)
622 rtx ptr_ref = orig;
624 if (! MACHOPIC_INDIRECT)
625 return orig;
627 if (GET_CODE (orig) == SYMBOL_REF)
629 int defined = machopic_data_defined_p (orig);
631 if (defined && MACHO_DYNAMIC_NO_PIC_P)
633 if (DARWIN_PPC)
635 /* Create a new register for CSE opportunities. */
636 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
637 emit_insn (gen_macho_high (hi_reg, orig));
638 emit_insn (gen_macho_low (reg, hi_reg, orig));
639 return reg;
641 else if (DARWIN_X86)
642 return orig;
643 else
644 /* some other cpu -- writeme! */
645 gcc_unreachable ();
647 else if (defined)
649 rtx offset = NULL;
650 if (DARWIN_PPC || HAVE_lo_sum)
651 offset = machopic_gen_offset (orig);
653 if (DARWIN_PPC)
655 rtx hi_sum_reg = (!can_create_pseudo_p ()
656 ? reg
657 : gen_reg_rtx (Pmode));
659 gcc_assert (reg);
661 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
662 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
663 gen_rtx_HIGH (Pmode, offset))));
664 emit_insn (gen_rtx_SET (Pmode, reg,
665 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
666 copy_rtx (offset))));
668 orig = reg;
670 else if (HAVE_lo_sum)
672 gcc_assert (reg);
674 emit_insn (gen_rtx_SET (VOIDmode, reg,
675 gen_rtx_HIGH (Pmode, offset)));
676 emit_insn (gen_rtx_SET (VOIDmode, reg,
677 gen_rtx_LO_SUM (Pmode, reg,
678 copy_rtx (offset))));
679 emit_use (pic_offset_table_rtx);
681 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
683 return orig;
686 ptr_ref = (gen_rtx_SYMBOL_REF
687 (Pmode,
688 machopic_indirection_name (orig, /*stub_p=*/false)));
690 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
692 ptr_ref = gen_const_mem (Pmode, ptr_ref);
693 machopic_define_symbol (ptr_ref);
695 if (DARWIN_X86
696 && reg
697 && MACHO_DYNAMIC_NO_PIC_P)
699 emit_insn (gen_rtx_SET (Pmode, reg, ptr_ref));
700 ptr_ref = reg;
703 return ptr_ref;
705 else if (GET_CODE (orig) == CONST)
707 /* If "(const (plus ...", walk the PLUS and return that result.
708 PLUS processing (below) will restore the "(const ..." if
709 appropriate. */
710 if (GET_CODE (XEXP (orig, 0)) == PLUS)
711 return machopic_indirect_data_reference (XEXP (orig, 0), reg);
712 else
713 return orig;
715 else if (GET_CODE (orig) == MEM)
717 XEXP (ptr_ref, 0) =
718 machopic_indirect_data_reference (XEXP (orig, 0), reg);
719 return ptr_ref;
721 else if (GET_CODE (orig) == PLUS)
723 rtx base, result;
724 /* When the target is i386, this code prevents crashes due to the
725 compiler's ignorance on how to move the PIC base register to
726 other registers. (The reload phase sometimes introduces such
727 insns.) */
728 if (GET_CODE (XEXP (orig, 0)) == REG
729 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
730 /* Prevent the same register from being erroneously used
731 as both the base and index registers. */
732 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
733 && reg)
735 emit_move_insn (reg, XEXP (orig, 0));
736 XEXP (ptr_ref, 0) = reg;
737 return ptr_ref;
740 /* Legitimize both operands of the PLUS. */
741 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
742 orig = machopic_indirect_data_reference (XEXP (orig, 1),
743 (base == reg ? 0 : reg));
744 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
745 result = plus_constant (Pmode, base, INTVAL (orig));
746 else
747 result = gen_rtx_PLUS (Pmode, base, orig);
749 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
751 if (reg)
753 emit_move_insn (reg, result);
754 result = reg;
756 else
758 result = force_reg (GET_MODE (result), result);
762 return result;
764 return ptr_ref;
767 /* Transform TARGET (a MEM), which is a function call target, to the
768 corresponding symbol_stub if necessary. Return a new MEM. */
771 machopic_indirect_call_target (rtx target)
773 if (! darwin_emit_branch_islands)
774 return target;
776 if (GET_CODE (target) != MEM)
777 return target;
779 if (MACHOPIC_INDIRECT
780 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
781 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
782 & MACHO_SYMBOL_FLAG_DEFINED))
784 rtx sym_ref = XEXP (target, 0);
785 const char *stub_name = machopic_indirection_name (sym_ref,
786 /*stub_p=*/true);
787 enum machine_mode mode = GET_MODE (sym_ref);
789 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
790 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
791 MEM_READONLY_P (target) = 1;
792 MEM_NOTRAP_P (target) = 1;
795 return target;
799 machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
801 rtx pic_ref = orig;
803 if (! MACHOPIC_INDIRECT)
804 return orig;
806 /* First handle a simple SYMBOL_REF or LABEL_REF */
807 if (GET_CODE (orig) == LABEL_REF
808 || (GET_CODE (orig) == SYMBOL_REF
811 /* addr(foo) = &func+(foo-func) */
812 orig = machopic_indirect_data_reference (orig, reg);
814 if (GET_CODE (orig) == PLUS
815 && GET_CODE (XEXP (orig, 0)) == REG)
817 if (reg == 0)
818 return force_reg (mode, orig);
820 emit_move_insn (reg, orig);
821 return reg;
824 if (GET_CODE (orig) == MEM)
826 if (reg == 0)
828 gcc_assert (!reload_in_progress);
829 reg = gen_reg_rtx (Pmode);
832 #if HAVE_lo_sum
833 if (MACHO_DYNAMIC_NO_PIC_P
834 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
835 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
837 #if defined (TARGET_TOC) /* ppc */
838 rtx temp_reg = (!can_create_pseudo_p ()
839 ? reg :
840 gen_reg_rtx (Pmode));
841 rtx asym = XEXP (orig, 0);
842 rtx mem;
844 emit_insn (gen_macho_high (temp_reg, asym));
845 mem = gen_const_mem (GET_MODE (orig),
846 gen_rtx_LO_SUM (Pmode, temp_reg,
847 copy_rtx (asym)));
848 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
849 #else
850 /* Some other CPU -- WriteMe! but right now there are no other
851 platforms that can use dynamic-no-pic */
852 gcc_unreachable ();
853 #endif
854 pic_ref = reg;
856 else
857 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
858 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
860 rtx offset = machopic_gen_offset (XEXP (orig, 0));
861 #if defined (TARGET_TOC) /* i.e., PowerPC */
862 /* Generating a new reg may expose opportunities for
863 common subexpression elimination. */
864 rtx hi_sum_reg = (!can_create_pseudo_p ()
865 ? reg
866 : gen_reg_rtx (Pmode));
867 rtx mem;
868 rtx insn;
869 rtx sum;
871 sum = gen_rtx_HIGH (Pmode, offset);
872 if (! MACHO_DYNAMIC_NO_PIC_P)
873 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
875 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
877 mem = gen_const_mem (GET_MODE (orig),
878 gen_rtx_LO_SUM (Pmode,
879 hi_sum_reg,
880 copy_rtx (offset)));
881 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
882 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
884 pic_ref = reg;
885 #else
886 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
888 emit_insn (gen_rtx_SET (VOIDmode, reg,
889 gen_rtx_HIGH (Pmode,
890 gen_rtx_CONST (Pmode,
891 offset))));
892 emit_insn (gen_rtx_SET (VOIDmode, reg,
893 gen_rtx_LO_SUM (Pmode, reg,
894 gen_rtx_CONST (Pmode,
895 copy_rtx (offset)))));
896 pic_ref = gen_rtx_PLUS (Pmode,
897 pic_offset_table_rtx, reg);
898 #endif
900 else
901 #endif /* HAVE_lo_sum */
903 rtx pic = pic_offset_table_rtx;
904 if (GET_CODE (pic) != REG)
906 emit_move_insn (reg, pic);
907 pic = reg;
909 #if 0
910 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
911 #endif
913 if (reload_in_progress)
914 df_set_regs_ever_live (REGNO (pic), true);
915 pic_ref = gen_rtx_PLUS (Pmode, pic,
916 machopic_gen_offset (XEXP (orig, 0)));
919 #if !defined (TARGET_TOC)
920 emit_move_insn (reg, pic_ref);
921 pic_ref = gen_const_mem (GET_MODE (orig), reg);
922 #endif
924 else
927 #if HAVE_lo_sum
928 if (GET_CODE (orig) == SYMBOL_REF
929 || GET_CODE (orig) == LABEL_REF)
931 rtx offset = machopic_gen_offset (orig);
932 #if defined (TARGET_TOC) /* i.e., PowerPC */
933 rtx hi_sum_reg;
935 if (reg == 0)
937 gcc_assert (!reload_in_progress);
938 reg = gen_reg_rtx (Pmode);
941 hi_sum_reg = reg;
943 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
944 (MACHO_DYNAMIC_NO_PIC_P)
945 ? gen_rtx_HIGH (Pmode, offset)
946 : gen_rtx_PLUS (Pmode,
947 pic_offset_table_rtx,
948 gen_rtx_HIGH (Pmode,
949 offset))));
950 emit_insn (gen_rtx_SET (VOIDmode, reg,
951 gen_rtx_LO_SUM (Pmode,
952 hi_sum_reg,
953 copy_rtx (offset))));
954 pic_ref = reg;
955 #else
956 emit_insn (gen_rtx_SET (VOIDmode, reg,
957 gen_rtx_HIGH (Pmode, offset)));
958 emit_insn (gen_rtx_SET (VOIDmode, reg,
959 gen_rtx_LO_SUM (Pmode, reg,
960 copy_rtx (offset))));
961 pic_ref = gen_rtx_PLUS (Pmode,
962 pic_offset_table_rtx, reg);
963 #endif
965 else
966 #endif /* HAVE_lo_sum */
968 if (REG_P (orig)
969 || GET_CODE (orig) == SUBREG)
971 return orig;
973 else
975 rtx pic = pic_offset_table_rtx;
976 if (GET_CODE (pic) != REG)
978 emit_move_insn (reg, pic);
979 pic = reg;
981 #if 0
982 emit_use (pic_offset_table_rtx);
983 #endif
984 if (reload_in_progress)
985 df_set_regs_ever_live (REGNO (pic), true);
986 pic_ref = gen_rtx_PLUS (Pmode,
987 pic,
988 machopic_gen_offset (orig));
993 if (GET_CODE (pic_ref) != REG)
995 if (reg != 0)
997 emit_move_insn (reg, pic_ref);
998 return reg;
1000 else
1002 return force_reg (mode, pic_ref);
1005 else
1007 return pic_ref;
1011 else if (GET_CODE (orig) == SYMBOL_REF)
1012 return orig;
1014 else if (GET_CODE (orig) == PLUS
1015 && (GET_CODE (XEXP (orig, 0)) == MEM
1016 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
1017 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
1018 && XEXP (orig, 0) != pic_offset_table_rtx
1019 && GET_CODE (XEXP (orig, 1)) != REG)
1022 rtx base;
1023 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
1025 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1026 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
1027 Pmode, (base == reg ? 0 : reg));
1028 if (GET_CODE (orig) == CONST_INT)
1030 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
1031 is_complex = 1;
1033 else
1034 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
1036 if (reg && is_complex)
1038 emit_move_insn (reg, pic_ref);
1039 pic_ref = reg;
1041 /* Likewise, should we set special REG_NOTEs here? */
1044 else if (GET_CODE (orig) == CONST)
1046 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1049 else if (GET_CODE (orig) == MEM
1050 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
1052 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1053 addr = replace_equiv_address (orig, addr);
1054 emit_move_insn (reg, addr);
1055 pic_ref = reg;
1058 return pic_ref;
1061 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
1062 DATA is the FILE* for assembly output. Called from
1063 htab_traverse. */
1065 static int
1066 machopic_output_indirection (void **slot, void *data)
1068 machopic_indirection *p = *((machopic_indirection **) slot);
1069 FILE *asm_out_file = (FILE *) data;
1070 rtx symbol;
1071 const char *sym_name;
1072 const char *ptr_name;
1074 if (!p->used)
1075 return 1;
1077 symbol = p->symbol;
1078 sym_name = XSTR (symbol, 0);
1079 ptr_name = p->ptr_name;
1081 if (p->stub_p)
1083 char *sym;
1084 char *stub;
1085 tree id;
1087 id = maybe_get_identifier (sym_name);
1088 if (id)
1090 tree id_orig = id;
1092 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1093 id = TREE_CHAIN (id);
1094 if (id != id_orig)
1095 sym_name = IDENTIFIER_POINTER (id);
1098 sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1099 if (sym_name[0] == '*' || sym_name[0] == '&')
1100 strcpy (sym, sym_name + 1);
1101 else if (sym_name[0] == '-' || sym_name[0] == '+')
1102 strcpy (sym, sym_name);
1103 else
1104 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1106 stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1107 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1108 strcpy (stub, ptr_name + 1);
1109 else
1110 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1112 machopic_output_stub (asm_out_file, sym, stub);
1114 else if (! indirect_data (symbol)
1115 && (machopic_symbol_defined_p (symbol)
1116 || SYMBOL_REF_LOCAL_P (symbol)))
1118 switch_to_section (data_section);
1119 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1120 assemble_label (asm_out_file, ptr_name);
1121 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1122 GET_MODE_SIZE (Pmode),
1123 GET_MODE_ALIGNMENT (Pmode), 1);
1125 else
1127 rtx init = const0_rtx;
1129 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1131 /* Mach-O symbols are passed around in code through indirect
1132 references and the original symbol_ref hasn't passed through
1133 the generic handling and reference-catching in
1134 output_operand, so we need to manually mark weak references
1135 as such. */
1136 if (SYMBOL_REF_WEAK (symbol))
1138 tree decl = SYMBOL_REF_DECL (symbol);
1139 gcc_assert (DECL_P (decl));
1141 if (decl != NULL_TREE
1142 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
1143 /* Handle only actual external-only definitions, not
1144 e.g. extern inline code or variables for which
1145 storage has been allocated. */
1146 && !TREE_STATIC (decl))
1148 fputs ("\t.weak_reference ", asm_out_file);
1149 assemble_name (asm_out_file, sym_name);
1150 fputc ('\n', asm_out_file);
1154 assemble_name (asm_out_file, ptr_name);
1155 fprintf (asm_out_file, ":\n");
1157 fprintf (asm_out_file, "\t.indirect_symbol ");
1158 assemble_name (asm_out_file, sym_name);
1159 fprintf (asm_out_file, "\n");
1161 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1162 have their symbol name instead of 0 in the second entry of
1163 the non-lazy symbol pointer data structure when they are
1164 defined. This allows the runtime to rebind newer instances
1165 of the translation unit with the original instance of the
1166 symbol. */
1168 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1169 && machopic_symbol_defined_p (symbol))
1170 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1172 assemble_integer (init, GET_MODE_SIZE (Pmode),
1173 GET_MODE_ALIGNMENT (Pmode), 1);
1176 return 1;
1179 void
1180 machopic_finish (FILE *asm_out_file)
1182 if (machopic_indirections)
1183 htab_traverse_noresize (machopic_indirections,
1184 machopic_output_indirection,
1185 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
1303 && TREE_INT_CST_LOW (size) == 4
1304 && TREE_INT_CST_HIGH (size) == 0)
1305 return darwin_sections[literal4_section];
1306 else if (TREE_CODE (size) == INTEGER_CST
1307 && TREE_INT_CST_LOW (size) == 8
1308 && TREE_INT_CST_HIGH (size) == 0)
1309 return darwin_sections[literal8_section];
1310 else if (HAVE_GAS_LITERAL16
1311 && TARGET_64BIT
1312 && TREE_CODE (size) == INTEGER_CST
1313 && TREE_INT_CST_LOW (size) == 16
1314 && TREE_INT_CST_HIGH (size) == 0)
1315 return darwin_sections[literal16_section];
1316 else
1317 return readonly_data_section;
1320 return readonly_data_section;
1323 section *
1324 darwin_tm_clone_table_section (void)
1326 return get_named_section (NULL,
1327 "__DATA,__tm_clone_table,regular,no_dead_strip",
1332 machopic_reloc_rw_mask (void)
1334 return MACHOPIC_INDIRECT ? 3 : 0;
1337 /* We have to deal with ObjC/C++ metadata section placement in the common
1338 code, since it will also be called from LTO.
1340 Return metadata attributes, if present (searching for ABI=2 first)
1341 Return NULL_TREE if no such attributes are found. */
1343 static tree
1344 is_objc_metadata (tree decl)
1346 if (DECL_P (decl)
1347 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1348 && DECL_ATTRIBUTES (decl))
1350 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1351 if (meta)
1352 return meta;
1353 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1354 if (meta)
1355 return meta;
1357 return NULL_TREE;
1360 static int classes_seen;
1361 static int objc_metadata_seen;
1363 /* Return the section required for Objective C ABI 2 metadata. */
1364 static section *
1365 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1367 const char *p;
1368 tree ident = TREE_VALUE (meta);
1369 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1370 p = IDENTIFIER_POINTER (ident);
1372 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi == 2);
1374 objc_metadata_seen = 1;
1376 if (base == data_section)
1377 base = darwin_sections[objc2_metadata_section];
1379 /* Most of the OBJC2 META-data end up in the base section, so check it
1380 first. */
1381 if (!strncmp (p, "V2_BASE", 7))
1382 return base;
1383 else if (!strncmp (p, "V2_STRG", 7))
1384 return darwin_sections[cstring_section];
1386 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1387 return darwin_sections[objc2_classdefs_section];
1388 else if (!strncmp (p, "V2_MREF", 7))
1389 return darwin_sections[objc2_message_refs_section];
1390 else if (!strncmp (p, "V2_CLRF", 7))
1391 return darwin_sections[objc2_classrefs_section];
1392 else if (!strncmp (p, "V2_SURF", 7))
1393 return darwin_sections[objc2_super_classrefs_section];
1394 else if (!strncmp (p, "V2_NLCL", 7))
1395 return darwin_sections[objc2_nonlazy_class_section];
1396 else if (!strncmp (p, "V2_CLAB", 7))
1398 classes_seen = 1;
1399 return darwin_sections[objc2_classlist_section];
1401 else if (!strncmp (p, "V2_SRFS", 7))
1402 return darwin_sections[objc2_selector_refs_section];
1403 else if (!strncmp (p, "V2_NLCA", 7))
1404 return darwin_sections[objc2_nonlazy_category_section];
1405 else if (!strncmp (p, "V2_CALA", 7))
1406 return darwin_sections[objc2_categorylist_section];
1408 else if (!strncmp (p, "V2_PLST", 7))
1409 return darwin_sections[objc2_protocollist_section];
1410 else if (!strncmp (p, "V2_PRFS", 7))
1411 return darwin_sections[objc2_protocolrefs_section];
1413 else if (!strncmp (p, "V2_INFO", 7))
1414 return darwin_sections[objc2_image_info_section];
1416 else if (!strncmp (p, "V2_EHTY", 7))
1417 return darwin_sections[data_coal_section];
1419 else if (!strncmp (p, "V2_CSTR", 7))
1420 return darwin_sections[objc2_constant_string_object_section];
1422 /* Not recognized, default. */
1423 return base;
1426 /* Return the section required for Objective C ABI 0/1 metadata. */
1427 static section *
1428 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1430 const char *p;
1431 tree ident = TREE_VALUE (meta);
1432 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1433 p = IDENTIFIER_POINTER (ident);
1435 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi < 2);
1437 objc_metadata_seen = 1;
1439 /* String sections first, cos there are lots of strings. */
1440 if (!strncmp (p, "V1_STRG", 7))
1441 return darwin_sections[cstring_section];
1442 else if (!strncmp (p, "V1_CLSN", 7))
1443 return darwin_sections[objc_class_names_section];
1444 else if (!strncmp (p, "V1_METN", 7))
1445 return darwin_sections[objc_meth_var_names_section];
1446 else if (!strncmp (p, "V1_METT", 7))
1447 return darwin_sections[objc_meth_var_types_section];
1449 else if (!strncmp (p, "V1_CLAS", 7))
1451 classes_seen = 1;
1452 return darwin_sections[objc_class_section];
1454 else if (!strncmp (p, "V1_META", 7))
1455 return darwin_sections[objc_meta_class_section];
1456 else if (!strncmp (p, "V1_CATG", 7))
1457 return darwin_sections[objc_category_section];
1458 else if (!strncmp (p, "V1_PROT", 7))
1459 return darwin_sections[objc_protocol_section];
1461 else if (!strncmp (p, "V1_CLCV", 7))
1462 return darwin_sections[objc_class_vars_section];
1463 else if (!strncmp (p, "V1_CLIV", 7))
1464 return darwin_sections[objc_instance_vars_section];
1466 else if (!strncmp (p, "V1_CLCM", 7))
1467 return darwin_sections[objc_cls_meth_section];
1468 else if (!strncmp (p, "V1_CLIM", 7))
1469 return darwin_sections[objc_inst_meth_section];
1470 else if (!strncmp (p, "V1_CACM", 7))
1471 return darwin_sections[objc_cat_cls_meth_section];
1472 else if (!strncmp (p, "V1_CAIM", 7))
1473 return darwin_sections[objc_cat_inst_meth_section];
1474 else if (!strncmp (p, "V1_PNSM", 7))
1475 return darwin_sections[objc_cat_inst_meth_section];
1476 else if (!strncmp (p, "V1_PCLM", 7))
1477 return darwin_sections[objc_cat_cls_meth_section];
1479 else if (!strncmp (p, "V1_CLPR", 7))
1480 return darwin_sections[objc_cat_cls_meth_section];
1481 else if (!strncmp (p, "V1_CAPR", 7))
1482 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1484 else if (!strncmp (p, "V1_PRFS", 7))
1485 return darwin_sections[objc_cat_cls_meth_section];
1486 else if (!strncmp (p, "V1_CLRF", 7))
1487 return darwin_sections[objc_cls_refs_section];
1488 else if (!strncmp (p, "V1_SRFS", 7))
1489 return darwin_sections[objc_selector_refs_section];
1491 else if (!strncmp (p, "V1_MODU", 7))
1492 return darwin_sections[objc_module_info_section];
1493 else if (!strncmp (p, "V1_SYMT", 7))
1494 return darwin_sections[objc_symbols_section];
1495 else if (!strncmp (p, "V1_INFO", 7))
1496 return darwin_sections[objc_image_info_section];
1498 else if (!strncmp (p, "V1_PLST", 7))
1499 return darwin_sections[objc1_prop_list_section];
1500 else if (!strncmp (p, "V1_PEXT", 7))
1501 return darwin_sections[objc1_protocol_ext_section];
1502 else if (!strncmp (p, "V1_CEXT", 7))
1503 return darwin_sections[objc1_class_ext_section];
1505 else if (!strncmp (p, "V2_CSTR", 7))
1506 return darwin_sections[objc_constant_string_object_section];
1508 return base;
1511 section *
1512 machopic_select_section (tree decl,
1513 int reloc,
1514 unsigned HOST_WIDE_INT align)
1516 bool zsize, one, weak, ro;
1517 section *base_section = NULL;
1519 weak = (DECL_P (decl)
1520 && DECL_WEAK (decl)
1521 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1523 zsize = (DECL_P (decl)
1524 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1525 && tree_to_uhwi (DECL_SIZE_UNIT (decl)) == 0);
1527 one = DECL_P (decl)
1528 && TREE_CODE (decl) == VAR_DECL
1529 && DECL_ONE_ONLY (decl);
1531 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1533 switch (categorize_decl_for_section (decl, reloc))
1535 case SECCAT_TEXT:
1536 gcc_unreachable ();
1537 break;
1539 case SECCAT_RODATA:
1540 case SECCAT_SRODATA:
1541 base_section = darwin_rodata_section (weak, zsize);
1542 break;
1544 case SECCAT_RODATA_MERGE_STR:
1545 base_section = darwin_mergeable_string_section (decl, align);
1546 break;
1548 case SECCAT_RODATA_MERGE_STR_INIT:
1549 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1550 break;
1552 case SECCAT_RODATA_MERGE_CONST:
1553 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1554 break;
1556 case SECCAT_DATA:
1557 case SECCAT_DATA_REL:
1558 case SECCAT_DATA_REL_LOCAL:
1559 case SECCAT_DATA_REL_RO:
1560 case SECCAT_DATA_REL_RO_LOCAL:
1561 case SECCAT_SDATA:
1562 case SECCAT_TDATA:
1563 if (weak || one)
1565 if (ro)
1566 base_section = darwin_sections[const_data_coal_section];
1567 else
1568 base_section = darwin_sections[data_coal_section];
1570 else if (DARWIN_SECTION_ANCHORS
1571 && flag_section_anchors
1572 && zsize)
1574 /* If we're doing section anchors, then punt zero-sized objects into
1575 their own sections so that they don't interfere with offset
1576 computation for the remaining vars. This does not need to be done
1577 for stuff in mergeable sections, since these are ineligible for
1578 anchors. */
1579 if (ro)
1580 base_section = darwin_sections[zobj_const_data_section];
1581 else
1582 base_section = darwin_sections[zobj_data_section];
1584 else if (ro)
1585 base_section = darwin_sections[const_data_section];
1586 else
1587 base_section = data_section;
1588 break;
1589 case SECCAT_BSS:
1590 case SECCAT_SBSS:
1591 case SECCAT_TBSS:
1592 if (weak || one)
1593 base_section = darwin_sections[data_coal_section];
1594 else
1596 if (!TREE_PUBLIC (decl))
1597 base_section = lcomm_section;
1598 else if (bss_noswitch_section)
1599 base_section = bss_noswitch_section;
1600 else
1601 base_section = data_section;
1603 break;
1605 default:
1606 gcc_unreachable ();
1609 /* Darwin weird special cases.
1610 a) OBJC Meta-data. */
1611 if (DECL_P (decl)
1612 && (TREE_CODE (decl) == VAR_DECL
1613 || TREE_CODE (decl) == CONST_DECL)
1614 && DECL_ATTRIBUTES (decl))
1616 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1617 if (meta)
1618 return darwin_objc2_section (decl, meta, base_section);
1619 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1620 if (meta)
1621 return darwin_objc1_section (decl, meta, base_section);
1622 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1623 if (meta)
1624 return base_section; /* GNU runtime is happy with it all in one pot. */
1627 /* b) Constant string objects. */
1628 if (TREE_CODE (decl) == CONSTRUCTOR
1629 && TREE_TYPE (decl)
1630 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1631 && TYPE_NAME (TREE_TYPE (decl)))
1633 tree name = TYPE_NAME (TREE_TYPE (decl));
1634 if (TREE_CODE (name) == TYPE_DECL)
1635 name = DECL_NAME (name);
1637 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1639 if (flag_next_runtime)
1641 if (flag_objc_abi == 2)
1642 return darwin_sections[objc2_constant_string_object_section];
1643 else
1644 return darwin_sections[objc_constant_string_object_section];
1646 else
1647 return darwin_sections[objc_string_object_section];
1649 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1650 return darwin_sections[cfstring_constant_object_section];
1651 else
1652 return base_section;
1654 /* c) legacy meta-data selection. */
1655 else if (TREE_CODE (decl) == VAR_DECL
1656 && DECL_NAME (decl)
1657 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1658 && IDENTIFIER_POINTER (DECL_NAME (decl))
1659 && flag_next_runtime
1660 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1662 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1663 static bool warned_objc_46 = false;
1664 /* We shall assert that zero-sized objects are an error in ObjC
1665 meta-data. */
1666 gcc_assert (tree_to_uhwi (DECL_SIZE_UNIT (decl)) != 0);
1668 /* ??? This mechanism for determining the metadata section is
1669 broken when LTO is in use, since the frontend that generated
1670 the data is not identified. We will keep the capability for
1671 the short term - in case any non-Objective-C programs are using
1672 it to place data in specified sections. */
1673 if (!warned_objc_46)
1675 location_t loc = DECL_SOURCE_LOCATION (decl);
1676 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
1677 " to select meta-data sections is deprecated at 4.6"
1678 " and will be removed in 4.7");
1679 warned_objc_46 = true;
1682 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1683 return darwin_sections[objc_cls_meth_section];
1684 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1685 return darwin_sections[objc_inst_meth_section];
1686 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
1687 return darwin_sections[objc_cat_cls_meth_section];
1688 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
1689 return darwin_sections[objc_cat_inst_meth_section];
1690 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1691 return darwin_sections[objc_class_vars_section];
1692 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1693 return darwin_sections[objc_instance_vars_section];
1694 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1695 return darwin_sections[objc_cat_cls_meth_section];
1696 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1697 return darwin_sections[objc_class_names_section];
1698 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1699 return darwin_sections[objc_meth_var_names_section];
1700 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1701 return darwin_sections[objc_meth_var_types_section];
1702 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1703 return darwin_sections[objc_cls_refs_section];
1704 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1705 return darwin_sections[objc_class_section];
1706 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1707 return darwin_sections[objc_meta_class_section];
1708 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1709 return darwin_sections[objc_category_section];
1710 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1711 return darwin_sections[objc_selector_refs_section];
1712 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1713 return darwin_sections[objc_selector_fixup_section];
1714 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1715 return darwin_sections[objc_symbols_section];
1716 else if (!strncmp (name, "_OBJC_MODULES", 13))
1717 return darwin_sections[objc_module_info_section];
1718 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1719 return darwin_sections[objc_image_info_section];
1720 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1721 return darwin_sections[objc_cat_inst_meth_section];
1722 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1723 return darwin_sections[objc_cat_cls_meth_section];
1724 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1725 return darwin_sections[objc_cat_cls_meth_section];
1726 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1727 return darwin_sections[objc_protocol_section];
1728 else
1729 return base_section;
1732 return base_section;
1735 /* This can be called with address expressions as "rtx".
1736 They must go in "const". */
1738 section *
1739 machopic_select_rtx_section (enum machine_mode mode, rtx x,
1740 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1742 if (GET_MODE_SIZE (mode) == 8
1743 && (GET_CODE (x) == CONST_INT
1744 || GET_CODE (x) == CONST_DOUBLE))
1745 return darwin_sections[literal8_section];
1746 else if (GET_MODE_SIZE (mode) == 4
1747 && (GET_CODE (x) == CONST_INT
1748 || GET_CODE (x) == CONST_DOUBLE))
1749 return darwin_sections[literal4_section];
1750 else if (HAVE_GAS_LITERAL16
1751 && TARGET_64BIT
1752 && GET_MODE_SIZE (mode) == 16
1753 && (GET_CODE (x) == CONST_INT
1754 || GET_CODE (x) == CONST_DOUBLE
1755 || GET_CODE (x) == CONST_VECTOR))
1756 return darwin_sections[literal16_section];
1757 else if (MACHOPIC_INDIRECT
1758 && (GET_CODE (x) == SYMBOL_REF
1759 || GET_CODE (x) == CONST
1760 || GET_CODE (x) == LABEL_REF))
1761 return darwin_sections[const_data_section];
1762 else
1763 return darwin_sections[const_section];
1766 void
1767 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1769 cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
1771 vec_safe_push (ctors, new_elt);
1773 if (! MACHOPIC_INDIRECT)
1774 fprintf (asm_out_file, ".reference .constructors_used\n");
1777 void
1778 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1780 cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)};
1782 vec_safe_push (dtors, new_elt);
1784 if (! MACHOPIC_INDIRECT)
1785 fprintf (asm_out_file, ".reference .destructors_used\n");
1788 static int
1789 sort_cdtor_records (const void * a, const void * b)
1791 const cdtor_record *cda = (const cdtor_record *)a;
1792 const cdtor_record *cdb = (const cdtor_record *)b;
1793 if (cda->priority > cdb->priority)
1794 return 1;
1795 if (cda->priority < cdb->priority)
1796 return -1;
1797 if (cda->position > cdb->position)
1798 return 1;
1799 if (cda->position < cdb->position)
1800 return -1;
1801 return 0;
1804 static void
1805 finalize_ctors ()
1807 unsigned int i;
1808 cdtor_record *elt;
1810 if (MACHOPIC_INDIRECT)
1811 switch_to_section (darwin_sections[mod_init_section]);
1812 else
1813 switch_to_section (darwin_sections[constructor_section]);
1815 if (vec_safe_length (ctors) > 1)
1816 ctors->qsort (sort_cdtor_records);
1817 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
1819 assemble_align (POINTER_SIZE);
1820 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1824 static void
1825 finalize_dtors ()
1827 unsigned int i;
1828 cdtor_record *elt;
1830 if (MACHOPIC_INDIRECT)
1831 switch_to_section (darwin_sections[mod_term_section]);
1832 else
1833 switch_to_section (darwin_sections[destructor_section]);
1835 if (vec_safe_length (dtors) > 1)
1836 dtors->qsort (sort_cdtor_records);
1837 FOR_EACH_VEC_SAFE_ELT (dtors, i, elt)
1839 assemble_align (POINTER_SIZE);
1840 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1844 void
1845 darwin_globalize_label (FILE *stream, const char *name)
1847 if (!!strncmp (name, "_OBJC_", 6))
1848 default_globalize_label (stream, name);
1851 /* This routine returns non-zero if 'name' starts with the special objective-c
1852 anonymous file-scope static name. It accommodates c++'s mangling of such
1853 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1855 int
1856 darwin_label_is_anonymous_local_objc_name (const char *name)
1858 const unsigned char *p = (const unsigned char *) name;
1859 if (*p != '_')
1860 return 0;
1861 if (p[1] == 'Z' && p[2] == 'L')
1863 p += 3;
1864 while (*p >= '0' && *p <= '9')
1865 p++;
1867 return (!strncmp ((const char *)p, "_OBJC_", 6));
1870 /* LTO support for Mach-O.
1872 This version uses three mach-o sections to encapsulate the (unlimited
1873 number of) lto sections.
1875 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1876 __GNU_LTO, __section_names contains the GNU LTO section names.
1877 __GNU_LTO, __section_index contains an array of values that index these.
1879 Indexed thus:
1880 <section offset from the start of __GNU_LTO, __lto_sections>,
1881 <section length>
1882 <name offset from the start of __GNU_LTO, __section_names,
1883 <name length>.
1885 At present, for both m32 and m64 mach-o files each of these fields is
1886 represented by a uint32_t. This is because, AFAICT, a mach-o object
1887 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1889 uint32_t offset;
1890 "offset An integer specifying the offset to this section in the file." */
1892 /* Count lto section numbers. */
1893 static unsigned int lto_section_num = 0;
1895 /* A vector of information about LTO sections, at present, we only have
1896 the name. TODO: see if we can get the data length somehow. */
1897 typedef struct GTY (()) darwin_lto_section_e {
1898 const char *sectname;
1899 } darwin_lto_section_e ;
1901 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1903 /* Segment for LTO data. */
1904 #define LTO_SEGMENT_NAME "__GNU_LTO"
1906 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1907 sections into three Mach-O ones).
1908 NOTE: These names MUST be kept in sync with those in
1909 libiberty/simple-object-mach-o. */
1910 #define LTO_SECTS_SECTION "__wrapper_sects"
1911 #define LTO_NAMES_SECTION "__wrapper_names"
1912 #define LTO_INDEX_SECTION "__wrapper_index"
1914 /* File to temporarily store LTO data. This is appended to asm_out_file
1915 in darwin_end_file. */
1916 static FILE *lto_asm_out_file, *saved_asm_out_file;
1917 static char *lto_asm_out_name;
1919 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1920 asm_out_file and switching to an alternative output file. */
1921 void
1922 darwin_asm_lto_start (void)
1924 gcc_assert (! saved_asm_out_file);
1925 saved_asm_out_file = asm_out_file;
1926 if (! lto_asm_out_name)
1927 lto_asm_out_name = make_temp_file (".lto.s");
1928 lto_asm_out_file = fopen (lto_asm_out_name, "a");
1929 if (lto_asm_out_file == NULL)
1930 fatal_error ("failed to open temporary file %s for LTO output",
1931 lto_asm_out_name);
1932 asm_out_file = lto_asm_out_file;
1935 /* Restore asm_out_file. */
1936 void
1937 darwin_asm_lto_end (void)
1939 gcc_assert (saved_asm_out_file);
1940 fclose (lto_asm_out_file);
1941 asm_out_file = saved_asm_out_file;
1942 saved_asm_out_file = NULL;
1945 static void
1946 darwin_asm_dwarf_section (const char *name, unsigned int flags, tree decl);
1948 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
1950 void
1951 darwin_asm_named_section (const char *name,
1952 unsigned int flags,
1953 tree decl ATTRIBUTE_UNUSED)
1955 /* LTO sections go in a special section that encapsulates the (unlimited)
1956 number of GNU LTO sections within a single mach-o one. */
1957 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1958 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1960 darwin_lto_section_e e;
1961 /* We expect certain flags to be set... */
1962 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1963 == (SECTION_DEBUG | SECTION_NAMED));
1965 /* Switch to our combined section. */
1966 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1967 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1968 /* Output a label for the start of this sub-section. */
1969 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1970 lto_section_num, name);
1971 /* We have to jump through hoops to get the values of the intra-section
1972 offsets... */
1973 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1974 lto_section_num, lto_section_num);
1975 fprintf (asm_out_file,
1976 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1977 lto_section_num, lto_section_num+1, lto_section_num);
1978 lto_section_num++;
1979 e.sectname = xstrdup (name);
1980 /* Keep the names, we'll need to make a table later.
1981 TODO: check that we do not revisit sections, that would break
1982 the assumption of how this is done. */
1983 if (lto_section_names == NULL)
1984 vec_alloc (lto_section_names, 16);
1985 vec_safe_push (lto_section_names, e);
1987 else if (strncmp (name, "__DWARF,", 8) == 0)
1988 darwin_asm_dwarf_section (name, flags, decl);
1989 else
1990 fprintf (asm_out_file, "\t.section %s\n", name);
1993 void
1994 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
1996 /* Darwin does not use unique sections. */
1999 /* Handle __attribute__ ((apple_kext_compatibility)).
2000 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
2001 vtable for classes with this attribute (and their descendants) by not
2002 outputting the new 3.0 nondeleting destructor. This means that such
2003 objects CANNOT be allocated on the stack or as globals UNLESS they have
2004 a completely empty `operator delete'.
2005 Luckily, this fits in with the Darwin kext model.
2007 This attribute also disables gcc3's potential overlaying of derived
2008 class data members on the padding at the end of the base class. */
2010 tree
2011 darwin_handle_kext_attribute (tree *node, tree name,
2012 tree args ATTRIBUTE_UNUSED,
2013 int flags ATTRIBUTE_UNUSED,
2014 bool *no_add_attrs)
2016 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
2017 if (! TARGET_KEXTABI)
2019 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2020 "only when compiling a kext", name);
2022 *no_add_attrs = true;
2024 else if (TREE_CODE (*node) != RECORD_TYPE)
2026 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2027 "only to C++ classes", name);
2029 *no_add_attrs = true;
2032 return NULL_TREE;
2035 /* Handle a "weak_import" attribute; arguments as in
2036 struct attribute_spec.handler. */
2038 tree
2039 darwin_handle_weak_import_attribute (tree *node, tree name,
2040 tree ARG_UNUSED (args),
2041 int ARG_UNUSED (flags),
2042 bool * no_add_attrs)
2044 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
2046 warning (OPT_Wattributes, "%qE attribute ignored",
2047 name);
2048 *no_add_attrs = true;
2050 else
2051 declare_weak (*node);
2053 return NULL_TREE;
2056 /* Emit a label for an FDE, making it global and/or weak if appropriate.
2057 The third parameter is nonzero if this is for exception handling.
2058 The fourth parameter is nonzero if this is just a placeholder for an
2059 FDE that we are omitting. */
2061 void
2062 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
2064 char *lab ;
2065 char buf[32];
2066 static int invok_count = 0;
2067 static tree last_fun_decl = NULL_TREE;
2069 /* We use the linker to emit the .eh labels for Darwin 9 and above. */
2070 if (! for_eh || generating_for_darwin_version >= 9)
2071 return;
2073 /* FIXME: This only works when the eh for all sections of a function is
2074 emitted at the same time. If that changes, we would need to use a lookup
2075 table of some form to determine what to do. Also, we should emit the
2076 unadorned label for the partition containing the public label for a
2077 function. This is of limited use, probably, since we do not currently
2078 enable partitioning. */
2079 strcpy (buf, ".eh");
2080 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
2082 if (decl == last_fun_decl)
2084 invok_count++;
2085 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
2087 else
2089 last_fun_decl = decl;
2090 invok_count = 0;
2094 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
2096 if (TREE_PUBLIC (decl))
2098 targetm.asm_out.globalize_label (file, lab);
2099 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
2101 fputs ("\t.private_extern ", file);
2102 assemble_name (file, lab);
2103 fputc ('\n', file);
2107 if (DECL_WEAK (decl))
2109 fputs ("\t.weak_definition ", file);
2110 assemble_name (file, lab);
2111 fputc ('\n', file);
2114 assemble_name (file, lab);
2115 if (empty)
2117 fputs (" = 0\n", file);
2119 /* Mark the absolute .eh and .eh1 style labels as needed to
2120 ensure that we don't dead code strip them and keep such
2121 labels from another instantiation point until we can fix this
2122 properly with group comdat support. */
2123 darwin_mark_decl_preserved (lab);
2125 else
2126 fputs (":\n", file);
2128 free (lab);
2131 static GTY(()) unsigned long except_table_label_num;
2133 void
2134 darwin_emit_except_table_label (FILE *file)
2136 char section_start_label[30];
2138 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2139 except_table_label_num++);
2140 ASM_OUTPUT_LABEL (file, section_start_label);
2142 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2144 void
2145 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2147 const char *nlp_name;
2149 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2151 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2152 fputs ("\t.long\t", file);
2153 ASM_OUTPUT_LABELREF (file, nlp_name);
2154 fputs ("-.", file);
2157 /* If this is uncommented, details of each allocation will be printed
2158 in the asm right before the actual code. WARNING - this will cause some
2159 test-suite fails (since the printout will contain items that some tests
2160 are not expecting) -- so don't leave it on by default (it bloats the
2161 asm too). */
2162 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2164 /* The first two of these routines are ostensibly just intended to put
2165 names into the asm. However, they are both hijacked in order to ensure
2166 that zero-sized items do not make their way into the output. Consequently,
2167 we also need to make these participate in provisions for dealing with
2168 such items in section anchors. */
2170 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2171 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2172 but it does need to be referenced via indirect PIC data pointers.
2173 The machopic_define_symbol calls are telling the machopic subsystem
2174 that the name *is* defined in this module, so it doesn't need to
2175 make them indirect. */
2176 void
2177 darwin_asm_declare_object_name (FILE *file,
2178 const char *nam, tree decl)
2180 const char *xname = nam;
2181 unsigned HOST_WIDE_INT size;
2182 bool local_def, weak;
2184 weak = (DECL_P (decl)
2185 && DECL_WEAK (decl)
2186 && !lookup_attribute ("weak_import",
2187 DECL_ATTRIBUTES (decl)));
2189 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2190 && (!DECL_COMMON (decl)
2191 || !TREE_PUBLIC (decl)));
2193 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2194 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2196 if (local_def)
2198 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2199 if (!weak)
2200 machopic_define_symbol (DECL_RTL (decl));
2203 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2205 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2206 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2207 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2208 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2209 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2210 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2211 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2212 (unsigned long)DECL_INITIAL (decl));
2213 #endif
2215 /* Darwin needs help to support local zero-sized objects.
2216 They must be made at least one byte, and the section containing must be
2217 marked as unsuitable for section-anchors (see storage allocators below).
2219 For non-zero objects this output is handled by varasm.c.
2221 if (!size)
2223 unsigned int l2align = 0;
2225 /* The align must be honored, even for zero-sized. */
2226 if (DECL_ALIGN (decl))
2228 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2229 fprintf (file, "\t.align\t%u\n", l2align);
2232 ASM_OUTPUT_LABEL (file, xname);
2233 size = 1;
2234 fprintf (file, "\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2236 /* Check that we've correctly picked up the zero-sized item and placed it
2237 properly. */
2238 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2239 || (in_section
2240 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2242 else
2243 ASM_OUTPUT_LABEL (file, xname);
2246 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2247 void
2248 darwin_asm_declare_constant_name (FILE *file, const char *name,
2249 const_tree exp ATTRIBUTE_UNUSED,
2250 HOST_WIDE_INT size)
2252 assemble_label (file, name);
2253 /* As for other items, we need at least one byte. */
2254 if (!size)
2256 fputs ("\t.space\t1\n", file);
2257 /* Check that we've correctly picked up the zero-sized item and placed it
2258 properly. */
2259 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2260 || (in_section
2261 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2265 /* Darwin storage allocators.
2267 Zerofill sections are desirable for large blank data since, otherwise, these
2268 data bloat objects (PR33210).
2270 However, section anchors don't work in .zerofill sections (one cannot switch
2271 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2272 to put (at least some) data into 'normal' switchable sections.
2274 Here we set a relatively arbitrary value for the size of an object to trigger
2275 zerofill when section anchors are enabled (anything bigger than a page for
2276 current Darwin implementations). FIXME: there ought to be some objective way
2277 to make this choice.
2279 When section anchor are off this is ignored anyway. */
2281 #define BYTES_ZFILL 4096
2283 /* Emit a chunk of data for items coalesced by the linker. */
2284 static void
2285 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2286 unsigned HOST_WIDE_INT size,
2287 unsigned int align)
2289 /* Since the sections used here are coalesed, they will not be eligible
2290 for section anchors, and therefore we don't need to break that out. */
2291 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2292 switch_to_section (darwin_sections[const_data_coal_section]);
2293 else
2294 switch_to_section (darwin_sections[data_coal_section]);
2296 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2297 the align info for zero-sized items... but do it here otherwise. */
2298 if (size && align)
2299 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2301 if (TREE_PUBLIC (decl))
2302 darwin_globalize_label (fp, name);
2304 /* ... and we let it deal with outputting one byte of zero for them too. */
2305 darwin_asm_declare_object_name (fp, name, decl);
2306 if (size)
2307 assemble_zeros (size);
2310 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2311 static void
2312 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2313 unsigned HOST_WIDE_INT size,
2314 unsigned int align, tree meta)
2316 section *ocs = data_section;
2318 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2319 ocs = darwin_objc2_section (decl, meta, ocs);
2320 else
2321 ocs = darwin_objc1_section (decl, meta, ocs);
2323 switch_to_section (ocs);
2325 /* We shall declare that zero-sized meta-data are not valid (yet). */
2326 gcc_assert (size);
2327 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2329 /* ... and we let it deal with outputting one byte of zero for them too. */
2330 darwin_asm_declare_object_name (fp, name, decl);
2331 assemble_zeros (size);
2334 /* This routine emits 'local' storage:
2336 When Section Anchors are off this routine emits .zerofill commands in
2337 sections named for their alignment.
2339 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2340 the .static_data section so that the section anchoring system can see them.
2341 Larger items are still placed in .zerofill sections, addressing PR33210.
2342 The routine has no checking - it is all assumed to be done by the caller.
2344 static void
2345 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2346 unsigned HOST_WIDE_INT size,
2347 unsigned int l2align)
2349 /* FIXME: We have a fudge to make this work with Java even when the target does
2350 not use sections anchors -- Java seems to need at least one small item in a
2351 non-zerofill segment. */
2352 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2353 || (size && size <= 2))
2355 /* Put smaller objects in _static_data, where the section anchors system
2356 can get them.
2357 However, if they are zero-sized punt them to yet a different section
2358 (that is not allowed to participate in anchoring). */
2359 if (!size)
2361 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2362 in_section = darwin_sections[zobj_bss_section];
2363 size = 1;
2365 else
2367 fputs ("\t.static_data\n", fp);
2368 in_section = darwin_sections[static_data_section];
2371 if (l2align)
2372 fprintf (fp, "\t.align\t%u\n", l2align);
2374 assemble_name (fp, name);
2375 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2377 else
2379 /* When we are on a non-section anchor target, we can get zero-sized
2380 items here. However, all we need to do is to bump them to one byte
2381 and the section alignment will take care of the rest. */
2382 char secnam[64];
2383 unsigned int flags ;
2384 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
2385 (unsigned) l2align);
2386 /* We can't anchor (yet, if ever) in zerofill sections, because we can't
2387 switch to them and emit a label. */
2388 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2389 in_section = get_section (secnam, flags, NULL);
2390 fprintf (fp, "\t.zerofill %s,", secnam);
2391 assemble_name (fp, name);
2392 if (!size)
2393 size = 1;
2395 if (l2align)
2396 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2397 size, (unsigned) l2align);
2398 else
2399 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2402 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2403 /* This is defined as a file-scope var, so we know to notify machopic. */
2404 machopic_define_symbol (DECL_RTL (decl));
2407 /* Emit a chunk of common. */
2408 static void
2409 darwin_emit_common (FILE *fp, const char *name,
2410 unsigned HOST_WIDE_INT size, unsigned int align)
2412 unsigned HOST_WIDE_INT rounded;
2413 unsigned int l2align;
2415 /* Earlier systems complain if the alignment exceeds the page size.
2416 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2417 if (!emit_aligned_common && (align > 32768UL))
2418 align = 4096UL; /* In units. */
2419 else
2420 align /= BITS_PER_UNIT;
2422 /* Make sure we have a meaningful align. */
2423 if (!align)
2424 align = 1;
2426 /* For earlier toolchains, we need to emit the var as a rounded size to
2427 tell ld the alignment. */
2428 if (size < align)
2429 rounded = align;
2430 else
2431 rounded = (size + (align-1)) & ~(align-1);
2433 l2align = floor_log2 (align);
2434 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2436 in_section = comm_section;
2437 /* We mustn't allow multiple public symbols to share an address when using
2438 the normal OSX toolchain. */
2439 if (!size)
2441 /* Put at least one byte. */
2442 size = 1;
2443 /* This section can no longer participate in section anchoring. */
2444 comm_section->common.flags |= SECTION_NO_ANCHOR;
2447 fputs ("\t.comm\t", fp);
2448 assemble_name (fp, name);
2449 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2450 emit_aligned_common?size:rounded);
2451 if (l2align && emit_aligned_common)
2452 fprintf (fp, ",%u", l2align);
2453 fputs ("\n", fp);
2456 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2457 or coalescable data sections (for weak or comdat) as appropriate. */
2459 void
2460 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2461 unsigned HOST_WIDE_INT size, unsigned int align)
2463 unsigned int l2align;
2464 bool one, pub, weak;
2465 tree meta;
2467 pub = TREE_PUBLIC (decl);
2468 one = DECL_ONE_ONLY (decl);
2469 weak = (DECL_P (decl)
2470 && DECL_WEAK (decl)
2471 && !lookup_attribute ("weak_import",
2472 DECL_ATTRIBUTES (decl)));
2474 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2475 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2476 " pub %d weak %d one %d init %lx\n",
2477 name, (long long)size, (int)align, TREE_READONLY (decl),
2478 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2479 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2480 #endif
2482 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2483 before the target has a chance to comment. */
2484 if ((meta = is_objc_metadata (decl)))
2486 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2487 return;
2490 /* Check that any initializer is valid. */
2491 gcc_assert ((DECL_INITIAL (decl) == NULL)
2492 || (DECL_INITIAL (decl) == error_mark_node)
2493 || initializer_zerop (DECL_INITIAL (decl)));
2495 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2496 gcc_assert (!DECL_COMMON (decl));
2498 /* Pick up the correct alignment. */
2499 if (!size || !align)
2500 align = DECL_ALIGN (decl);
2502 l2align = floor_log2 (align / BITS_PER_UNIT);
2503 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2505 last_assemble_variable_decl = decl;
2507 /* We would rather not have to check this here - but it seems that we might
2508 be passed a decl that should be in coalesced space. */
2509 if (one || weak)
2511 /* Weak or COMDAT objects are put in mergeable sections. */
2512 darwin_emit_weak_or_comdat (fp, decl, name, size,
2513 DECL_ALIGN (decl));
2514 return;
2517 /* If this is not public, then emit according to local rules. */
2518 if (!pub)
2520 darwin_emit_local_bss (fp, decl, name, size, l2align);
2521 return;
2524 /* So we have a public symbol (small item fudge for Java, see above). */
2525 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2526 || (size && size <= 2))
2528 /* Put smaller objects in data, where the section anchors system can get
2529 them. However, if they are zero-sized punt them to yet a different
2530 section (that is not allowed to participate in anchoring). */
2531 if (!size)
2533 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2534 in_section = darwin_sections[zobj_data_section];
2535 size = 1;
2537 else
2539 fputs ("\t.data\n", fp);
2540 in_section = data_section;
2543 if (l2align)
2544 fprintf (fp, "\t.align\t%u\n", l2align);
2546 assemble_name (fp, name);
2547 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2549 else
2551 char secnam[64];
2552 unsigned int flags ;
2553 /* When we are on a non-section anchor target, we can get zero-sized
2554 items here. However, all we need to do is to bump them to one byte
2555 and the section alignment will take care of the rest. */
2556 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
2558 /* We can't anchor in zerofill sections, because we can't switch
2559 to them and emit a label. */
2560 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2561 in_section = get_section (secnam, flags, NULL);
2562 fprintf (fp, "\t.zerofill %s,", secnam);
2563 assemble_name (fp, name);
2564 if (!size)
2565 size = 1;
2567 if (l2align)
2568 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2569 else
2570 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2572 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2575 /* Output a chunk of common, with alignment specified (where the target
2576 supports this). */
2577 void
2578 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2579 unsigned HOST_WIDE_INT size,
2580 unsigned int align)
2582 unsigned int l2align;
2583 bool one, weak;
2584 tree meta;
2586 /* No corresponding var. */
2587 if (decl==NULL)
2589 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2590 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2591 #endif
2592 darwin_emit_common (fp, name, size, align);
2593 return;
2596 one = DECL_ONE_ONLY (decl);
2597 weak = (DECL_P (decl)
2598 && DECL_WEAK (decl)
2599 && !lookup_attribute ("weak_import",
2600 DECL_ATTRIBUTES (decl)));
2602 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2603 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2604 " weak %d one %d init %lx\n",
2605 name, (long long)size, (int)align, TREE_READONLY (decl),
2606 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2607 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2608 #endif
2610 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2611 before the target has a chance to comment. */
2612 if ((meta = is_objc_metadata (decl)))
2614 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2615 return;
2618 /* We shouldn't be messing with this if the decl has a section name. */
2619 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2621 /* We would rather not have to check this here - but it seems that we might
2622 be passed a decl that should be in coalesced space. */
2623 if (one || weak)
2625 /* Weak or COMDAT objects are put in mergable sections. */
2626 darwin_emit_weak_or_comdat (fp, decl, name, size,
2627 DECL_ALIGN (decl));
2628 return;
2631 /* We should only get here for DECL_COMMON, with a zero init (and, in
2632 principle, only for public symbols too - although we deal with local
2633 ones below). */
2635 /* Check the initializer is OK. */
2636 gcc_assert (DECL_COMMON (decl)
2637 && ((DECL_INITIAL (decl) == NULL)
2638 || (DECL_INITIAL (decl) == error_mark_node)
2639 || initializer_zerop (DECL_INITIAL (decl))));
2641 last_assemble_variable_decl = decl;
2643 if (!size || !align)
2644 align = DECL_ALIGN (decl);
2646 l2align = floor_log2 (align / BITS_PER_UNIT);
2647 /* Check we aren't asking for more aligment than the platform allows. */
2648 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2650 if (TREE_PUBLIC (decl) != 0)
2651 darwin_emit_common (fp, name, size, align);
2652 else
2653 darwin_emit_local_bss (fp, decl, name, size, l2align);
2656 /* Output a chunk of BSS with alignment specfied. */
2657 void
2658 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2659 unsigned HOST_WIDE_INT size,
2660 unsigned int align)
2662 unsigned long l2align;
2663 bool one, weak;
2664 tree meta;
2666 one = DECL_ONE_ONLY (decl);
2667 weak = (DECL_P (decl)
2668 && DECL_WEAK (decl)
2669 && !lookup_attribute ("weak_import",
2670 DECL_ATTRIBUTES (decl)));
2672 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2673 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2674 " weak %d init %lx\n",
2675 name, (long long)size, (int)align, TREE_READONLY (decl),
2676 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2677 weak , (unsigned long)DECL_INITIAL (decl));
2678 #endif
2680 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2681 before the target has a chance to comment. */
2682 if ((meta = is_objc_metadata (decl)))
2684 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2685 return;
2688 /* We shouldn't be messing with this if the decl has a section name. */
2689 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2691 /* We would rather not have to check this here - but it seems that we might
2692 be passed a decl that should be in coalesced space. */
2693 if (one || weak)
2695 /* Weak or COMDAT objects are put in mergable sections. */
2696 darwin_emit_weak_or_comdat (fp, decl, name, size,
2697 DECL_ALIGN (decl));
2698 return;
2701 /* .. and it should be suitable for placement in local mem. */
2702 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2703 /* .. and any initializer must be all-zero. */
2704 gcc_assert ((DECL_INITIAL (decl) == NULL)
2705 || (DECL_INITIAL (decl) == error_mark_node)
2706 || initializer_zerop (DECL_INITIAL (decl)));
2708 last_assemble_variable_decl = decl;
2710 if (!size || !align)
2711 align = DECL_ALIGN (decl);
2713 l2align = floor_log2 (align / BITS_PER_UNIT);
2714 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2716 darwin_emit_local_bss (fp, decl, name, size, l2align);
2719 /* Emit an assembler directive to set visibility for a symbol. The
2720 only supported visibilities are VISIBILITY_DEFAULT and
2721 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2722 extern". There is no MACH-O equivalent of ELF's
2723 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2725 void
2726 darwin_assemble_visibility (tree decl, int vis)
2728 if (vis == VISIBILITY_DEFAULT)
2730 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2732 fputs ("\t.private_extern ", asm_out_file);
2733 assemble_name (asm_out_file,
2734 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2735 fputs ("\n", asm_out_file);
2737 else
2738 warning (OPT_Wattributes, "protected visibility attribute "
2739 "not supported in this configuration; ignored");
2742 /* vec used by darwin_asm_dwarf_section.
2743 Maybe a hash tab would be better here - but the intention is that this is
2744 a very short list (fewer than 16 items) and each entry should (ideally,
2745 eventually) only be presented once.
2747 A structure to hold a dwarf debug section used entry. */
2749 typedef struct GTY(()) dwarf_sect_used_entry {
2750 const char *name;
2751 unsigned count;
2753 dwarf_sect_used_entry;
2756 /* A list of used __DWARF sections. */
2757 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2759 /* This is called when we are asked to assemble a named section and the
2760 name begins with __DWARF,. We keep a list of the section names (without
2761 the __DWARF, prefix) and use this to emit our required start label on the
2762 first switch to each section. */
2764 static void
2765 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2766 tree ARG_UNUSED (decl))
2768 unsigned i;
2769 int namelen;
2770 const char * sname;
2771 dwarf_sect_used_entry *ref;
2772 bool found = false;
2773 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2774 == (SECTION_DEBUG | SECTION_NAMED));
2775 /* We know that the name starts with __DWARF, */
2776 sname = name + 8;
2777 namelen = strchr (sname, ',') - sname;
2778 gcc_assert (namelen);
2779 if (dwarf_sect_names_table == NULL)
2780 vec_alloc (dwarf_sect_names_table, 16);
2781 else
2782 for (i = 0;
2783 dwarf_sect_names_table->iterate (i, &ref);
2784 i++)
2786 if (!ref)
2787 break;
2788 if (!strcmp (ref->name, sname))
2790 found = true;
2791 ref->count++;
2792 break;
2796 fprintf (asm_out_file, "\t.section %s\n", name);
2797 if (!found)
2799 dwarf_sect_used_entry e;
2800 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2801 e.count = 1;
2802 e.name = xstrdup (sname);
2803 vec_safe_push (dwarf_sect_names_table, e);
2807 /* Output a difference of two labels that will be an assembly time
2808 constant if the two labels are local. (.long lab1-lab2 will be
2809 very different if lab1 is at the boundary between two sections; it
2810 will be relocated according to the second section, not the first,
2811 so one ends up with a difference between labels in different
2812 sections, which is bad in the dwarf2 eh context for instance.) */
2814 static int darwin_dwarf_label_counter;
2816 void
2817 darwin_asm_output_dwarf_delta (FILE *file, int size,
2818 const char *lab1, const char *lab2)
2820 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2821 && lab2[0] == '*' && lab2[1] == 'L');
2822 const char *directive = (size == 8 ? ".quad" : ".long");
2824 if (islocaldiff)
2825 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2826 else
2827 fprintf (file, "\t%s\t", directive);
2829 assemble_name_raw (file, lab1);
2830 fprintf (file, "-");
2831 assemble_name_raw (file, lab2);
2832 if (islocaldiff)
2833 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2836 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2837 offsets are not represented using relocs in .o files; either the
2838 section never leaves the .o file, or the linker or other tool is
2839 responsible for parsing the DWARF and updating the offsets. */
2841 void
2842 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2843 section *base)
2845 char sname[64];
2846 int namelen;
2848 gcc_assert (base->common.flags & SECTION_NAMED);
2849 gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
2850 gcc_assert (strchr (base->named.name + 8, ','));
2852 namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
2853 sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
2854 darwin_asm_output_dwarf_delta (file, size, lab, sname);
2857 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2859 void
2860 darwin_file_start (void)
2862 /* Nothing to do. */
2865 /* Called for the TARGET_ASM_FILE_END hook.
2866 Emit the mach-o pic indirection data, the lto data and, finally a flag
2867 to tell the linker that it can break the file object into sections and
2868 move those around for efficiency. */
2870 void
2871 darwin_file_end (void)
2873 if (!vec_safe_is_empty (ctors))
2874 finalize_ctors ();
2875 if (!vec_safe_is_empty (dtors))
2876 finalize_dtors ();
2878 /* If we are expecting to output NeXT ObjC meta-data, (and we actually see
2879 some) then we output the fix-and-continue marker (Image Info).
2880 This applies to Objective C, Objective C++ and LTO with either language
2881 as part of the input. */
2882 if (flag_next_runtime && objc_metadata_seen)
2884 unsigned int flags = 0;
2885 if (flag_objc_abi >= 2)
2887 flags = 16;
2888 output_section_asm_op
2889 (darwin_sections[objc2_image_info_section]->unnamed.data);
2891 else
2892 output_section_asm_op
2893 (darwin_sections[objc_image_info_section]->unnamed.data);
2895 ASM_OUTPUT_ALIGN (asm_out_file, 2);
2896 fputs ("L_OBJC_ImageInfo:\n", asm_out_file);
2898 flags |= (flag_replace_objc_classes && classes_seen) ? 1 : 0;
2899 flags |= flag_objc_gc ? 2 : 0;
2901 fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags);
2904 machopic_finish (asm_out_file);
2905 if (strcmp (lang_hooks.name, "GNU C++") == 0)
2907 switch_to_section (darwin_sections[constructor_section]);
2908 switch_to_section (darwin_sections[destructor_section]);
2909 ASM_OUTPUT_ALIGN (asm_out_file, 1);
2912 /* If there was LTO assembler output, append it to asm_out_file. */
2913 if (lto_asm_out_name)
2915 int n;
2916 char *buf, *lto_asm_txt;
2918 /* Shouldn't be here if we failed to switch back. */
2919 gcc_assert (! saved_asm_out_file);
2921 lto_asm_out_file = fopen (lto_asm_out_name, "r");
2922 if (lto_asm_out_file == NULL)
2923 fatal_error ("failed to open temporary file %s with LTO output",
2924 lto_asm_out_name);
2925 fseek (lto_asm_out_file, 0, SEEK_END);
2926 n = ftell (lto_asm_out_file);
2927 if (n > 0)
2929 fseek (lto_asm_out_file, 0, SEEK_SET);
2930 lto_asm_txt = buf = (char *) xmalloc (n + 1);
2931 while (fgets (lto_asm_txt, n, lto_asm_out_file))
2932 fputs (lto_asm_txt, asm_out_file);
2933 /* Put a termination label. */
2934 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2935 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2936 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2937 lto_section_num);
2938 /* Make sure our termination label stays in this section. */
2939 fputs ("\t.space\t1\n", asm_out_file);
2942 /* Remove the temporary file. */
2943 fclose (lto_asm_out_file);
2944 unlink_if_ordinary (lto_asm_out_name);
2945 free (lto_asm_out_name);
2948 /* Output the names and indices. */
2949 if (lto_section_names && lto_section_names->length ())
2951 int count;
2952 darwin_lto_section_e *ref;
2953 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
2954 the latter up ourselves. */
2955 const char *op = integer_asm_op (4,0);
2957 /* Emit the names. */
2958 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2959 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2960 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2962 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
2963 /* We have to jump through hoops to get the values of the intra-section
2964 offsets... */
2965 fprintf (asm_out_file,
2966 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
2967 count, count);
2968 fprintf (asm_out_file,
2969 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
2970 count, count+1, count);
2971 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
2973 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
2974 /* make sure our termination label stays in this section. */
2975 fputs ("\t.space\t1\n", asm_out_file);
2977 /* Emit the Index. */
2978 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2979 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
2980 fputs ("\t.align\t2\n", asm_out_file);
2981 fputs ("# Section offset, Section length, Name offset, Name length\n",
2982 asm_out_file);
2983 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2985 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
2986 op, count, ref->sectname);
2987 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
2988 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
2989 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
2993 /* If we have section anchors, then we must prevent the linker from
2994 re-arranging data. */
2995 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2996 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
2999 /* TODO: Add a language hook for identifying if a decl is a vtable. */
3000 #define DARWIN_VTABLE_P(DECL) 0
3002 /* Cross-module name binding. Darwin does not support overriding
3003 functions at dynamic-link time, except for vtables in kexts. */
3005 bool
3006 darwin_binds_local_p (const_tree decl)
3008 return default_binds_local_p_1 (decl,
3009 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
3012 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
3013 anchor relative to ".", the current section position. We cannot use
3014 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
3015 void
3016 darwin_asm_output_anchor (rtx symbol)
3018 fprintf (asm_out_file, "\t.set\t");
3019 assemble_name (asm_out_file, XSTR (symbol, 0));
3020 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
3021 SYMBOL_REF_BLOCK_OFFSET (symbol));
3024 /* Disable section anchoring on any section containing a zero-sized
3025 object. */
3026 bool
3027 darwin_use_anchors_for_symbol_p (const_rtx symbol)
3029 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
3031 section *sect;
3032 /* If the section contains a zero-sized object it's ineligible. */
3033 sect = SYMBOL_REF_BLOCK (symbol)->sect;
3034 /* This should have the effect of disabling anchors for vars that follow
3035 any zero-sized one, in a given section. */
3036 if (sect->common.flags & SECTION_NO_ANCHOR)
3037 return false;
3039 /* Also check the normal reasons for suppressing. */
3040 return default_use_anchors_for_symbol_p (symbol);
3042 else
3043 return false;
3046 /* Set the darwin specific attributes on TYPE. */
3047 void
3048 darwin_set_default_type_attributes (tree type)
3050 if (darwin_ms_struct
3051 && TREE_CODE (type) == RECORD_TYPE)
3052 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
3053 NULL_TREE,
3054 TYPE_ATTRIBUTES (type));
3057 /* True, iff we're generating code for loadable kernel extensions. */
3059 bool
3060 darwin_kextabi_p (void) {
3061 return flag_apple_kext;
3064 void
3065 darwin_override_options (void)
3067 /* Keep track of which (major) version we're generating code for. */
3068 if (darwin_macosx_version_min)
3070 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
3071 generating_for_darwin_version = 10;
3072 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
3073 generating_for_darwin_version = 9;
3075 /* Earlier versions are not specifically accounted, until required. */
3078 /* In principle, this should be c-family only. However, we really need to
3079 set sensible defaults for LTO as well, since the section selection stuff
3080 should check for correctness re. the ABI. TODO: check and provide the
3081 flags (runtime & ABI) from the lto wrapper). */
3083 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
3084 if (!global_options_set.x_flag_objc_abi)
3085 global_options.x_flag_objc_abi
3086 = (!flag_next_runtime)
3088 : (TARGET_64BIT ? 2
3089 : (generating_for_darwin_version >= 9) ? 1
3090 : 0);
3092 /* Objective-C family ABI 2 is only valid for next/m64 at present. */
3093 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
3095 if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
3096 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
3097 " used for %<-m64%> targets with"
3098 " %<-fnext-runtime%>");
3099 if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
3100 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
3101 " supported on %<-m32%> targets with"
3102 " %<-fnext-runtime%>");
3105 /* Don't emit DWARF3/4 unless specifically selected. This is a
3106 workaround for tool bugs. */
3107 if (!global_options_set.x_dwarf_strict)
3108 dwarf_strict = 1;
3109 if (!global_options_set.x_dwarf_version)
3110 dwarf_version = 2;
3112 /* Do not allow unwind tables to be generated by default for m32.
3113 fnon-call-exceptions will override this, regardless of what we do. */
3114 if (generating_for_darwin_version < 10
3115 && !global_options_set.x_flag_asynchronous_unwind_tables
3116 && !TARGET_64BIT)
3117 global_options.x_flag_asynchronous_unwind_tables = 0;
3119 /* Disable -freorder-blocks-and-partition when unwind tables are being
3120 emitted for Darwin < 9 (OSX 10.5).
3121 The strategy is, "Unless the User has specifically set/unset an unwind
3122 flag we will switch off -freorder-blocks-and-partition when unwind tables
3123 will be generated". If the User specifically sets flags... we assume
3124 (s)he knows why... */
3125 if (generating_for_darwin_version < 9
3126 && global_options_set.x_flag_reorder_blocks_and_partition
3127 && ((global_options.x_flag_exceptions /* User, c++, java */
3128 && !global_options_set.x_flag_exceptions) /* User specified... */
3129 || (global_options.x_flag_unwind_tables
3130 && !global_options_set.x_flag_unwind_tables)
3131 || (global_options.x_flag_non_call_exceptions
3132 && !global_options_set.x_flag_non_call_exceptions)
3133 || (global_options.x_flag_asynchronous_unwind_tables
3134 && !global_options_set.x_flag_asynchronous_unwind_tables)))
3136 inform (input_location,
3137 "-freorder-blocks-and-partition does not work with exceptions "
3138 "on this architecture");
3139 flag_reorder_blocks_and_partition = 0;
3140 flag_reorder_blocks = 1;
3143 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3144 one valid choice of exception scheme for each runtime. */
3145 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3146 global_options.x_flag_objc_sjlj_exceptions =
3147 flag_next_runtime && !TARGET_64BIT;
3149 /* FIXME: and this could be eliminated then too. */
3150 if (!global_options_set.x_flag_exceptions
3151 && flag_objc_exceptions
3152 && TARGET_64BIT)
3153 flag_exceptions = 1;
3155 if (flag_mkernel || flag_apple_kext)
3157 /* -mkernel implies -fapple-kext for C++ */
3158 if (strcmp (lang_hooks.name, "GNU C++") == 0)
3159 flag_apple_kext = 1;
3161 flag_no_common = 1;
3163 /* No EH in kexts. */
3164 flag_exceptions = 0;
3165 /* No -fnon-call-exceptions data in kexts. */
3166 flag_non_call_exceptions = 0;
3167 /* so no tables either.. */
3168 flag_unwind_tables = 0;
3169 flag_asynchronous_unwind_tables = 0;
3170 /* We still need to emit branch islands for kernel context. */
3171 darwin_emit_branch_islands = true;
3174 if (flag_var_tracking_uninit == 0
3175 && generating_for_darwin_version >= 9
3176 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3177 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3178 && write_symbols == DWARF2_DEBUG)
3179 flag_var_tracking_uninit = flag_var_tracking;
3181 if (MACHO_DYNAMIC_NO_PIC_P)
3183 if (flag_pic)
3184 warning_at (UNKNOWN_LOCATION, 0,
3185 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3186 " %<-fpie%> or %<-fPIE%>");
3187 flag_pic = 0;
3189 else if (flag_pic == 1)
3191 /* Darwin's -fpic is -fPIC. */
3192 flag_pic = 2;
3195 /* It is assumed that branch island stubs are needed for earlier systems. */
3196 if (generating_for_darwin_version < 9)
3197 darwin_emit_branch_islands = true;
3198 else
3199 emit_aligned_common = true; /* Later systems can support aligned common. */
3201 /* The c_dialect...() macros are not available to us here. */
3202 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3205 #if DARWIN_PPC
3206 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3208 static void
3209 darwin_patch_builtin (enum built_in_function fncode)
3211 tree fn = builtin_decl_explicit (fncode);
3212 tree sym;
3213 char *newname;
3215 if (!fn)
3216 return;
3218 sym = DECL_ASSEMBLER_NAME (fn);
3219 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3221 set_user_assembler_name (fn, newname);
3223 fn = builtin_decl_implicit (fncode);
3224 if (fn)
3225 set_user_assembler_name (fn, newname);
3228 void
3229 darwin_patch_builtins (void)
3231 if (LONG_DOUBLE_TYPE_SIZE != 128)
3232 return;
3234 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3235 #define PATCH_BUILTIN_NO64(fncode) \
3236 if (!TARGET_64BIT) \
3237 darwin_patch_builtin (fncode);
3238 #define PATCH_BUILTIN_VARIADIC(fncode) \
3239 if (!TARGET_64BIT \
3240 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3241 darwin_patch_builtin (fncode);
3242 #include "darwin-ppc-ldouble-patch.def"
3243 #undef PATCH_BUILTIN
3244 #undef PATCH_BUILTIN_NO64
3245 #undef PATCH_BUILTIN_VARIADIC
3247 #endif
3249 /* CFStrings implementation. */
3250 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3251 static GTY(()) tree cfstring_type_node = NULL_TREE;
3252 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3253 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3254 static GTY(()) tree pcint_type_node = NULL_TREE;
3255 static GTY(()) tree pcchar_type_node = NULL_TREE;
3257 static enum built_in_function darwin_builtin_cfstring;
3259 /* Store all constructed constant CFStrings in a hash table so that
3260 they get uniqued properly. */
3262 typedef struct GTY (()) cfstring_descriptor {
3263 /* The string literal. */
3264 tree literal;
3265 /* The resulting constant CFString. */
3266 tree constructor;
3267 } cfstring_descriptor;
3269 static GTY ((param_is (struct cfstring_descriptor))) htab_t cfstring_htab;
3271 static hashval_t cfstring_hash (const void *);
3272 static int cfstring_eq (const void *, const void *);
3274 static tree
3275 add_builtin_field_decl (tree type, const char *name, tree **chain)
3277 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3278 get_identifier (name), type);
3280 if (*chain != NULL)
3281 **chain = field;
3282 *chain = &DECL_CHAIN (field);
3284 return field;
3287 tree
3288 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3290 tree cfsfun, fields, pccfstring_ftype_pcchar;
3291 tree *chain = NULL;
3293 darwin_builtin_cfstring =
3294 (enum built_in_function) builtin_cfstring;
3296 /* struct __builtin_CFString {
3297 const int *isa; (will point at
3298 int flags; __CFConstantStringClassReference)
3299 const char *str;
3300 long length;
3301 }; */
3303 pcint_type_node = build_pointer_type
3304 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3306 pcchar_type_node = build_pointer_type
3307 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3309 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3311 /* Have to build backwards for finish struct. */
3312 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3313 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3314 add_builtin_field_decl (integer_type_node, "flags", &chain);
3315 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3316 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3317 fields, NULL_TREE);
3319 /* const struct __builtin_CFstring *
3320 __builtin___CFStringMakeConstantString (const char *); */
3322 ccfstring_type_node = build_qualified_type
3323 (cfstring_type_node, TYPE_QUAL_CONST);
3324 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3325 pccfstring_ftype_pcchar = build_function_type_list
3326 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3328 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3329 get_identifier ("__builtin___CFStringMakeConstantString"),
3330 pccfstring_ftype_pcchar);
3332 TREE_PUBLIC (cfsfun) = 1;
3333 DECL_EXTERNAL (cfsfun) = 1;
3334 DECL_ARTIFICIAL (cfsfun) = 1;
3335 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3336 in place of the existing, which may be NULL. */
3337 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3338 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3339 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
3340 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
3341 lang_hooks.builtin_function (cfsfun);
3343 /* extern int __CFConstantStringClassReference[]; */
3344 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3345 get_identifier ("__CFConstantStringClassReference"),
3346 build_array_type (integer_type_node, NULL_TREE));
3348 TREE_PUBLIC (cfstring_class_reference) = 1;
3349 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3350 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3351 DECL_EXTERNAL (cfstring_class_reference) = 1;
3352 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3354 /* Initialize the hash table used to hold the constant CFString objects. */
3355 cfstring_htab = htab_create_ggc (31, cfstring_hash, cfstring_eq, NULL);
3357 return cfstring_type_node;
3360 tree
3361 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3362 bool ARG_UNUSED (ignore))
3364 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3366 if (fcode == darwin_builtin_cfstring)
3368 if (!darwin_constant_cfstrings)
3370 error ("built-in function %qD requires the"
3371 " %<-mconstant-cfstrings%> flag", fndecl);
3372 return error_mark_node;
3375 if (n_args != 1)
3377 error ("built-in function %qD takes one argument only", fndecl);
3378 return error_mark_node;
3381 return darwin_build_constant_cfstring (*argp);
3384 return NULL_TREE;
3387 void
3388 darwin_rename_builtins (void)
3390 /* The system ___divdc3 routine in libSystem on darwin10 is not
3391 accurate to 1ulp, ours is, so we avoid ever using the system name
3392 for this routine and instead install a non-conflicting name that
3393 is accurate.
3395 When -ffast-math or -funsafe-math-optimizations is given, we can
3396 use the faster version. */
3397 if (!flag_unsafe_math_optimizations)
3399 enum built_in_function dcode
3400 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3401 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3402 tree fn = builtin_decl_explicit (dcode);
3403 /* Fortran and c call TARGET_INIT_BUILTINS and
3404 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3405 call into each to ensure that at least one of them is called
3406 after build_common_builtin_nodes. A better fix is to add a
3407 new hook to run after build_common_builtin_nodes runs. */
3408 if (fn)
3409 set_user_assembler_name (fn, "___ieee_divdc3");
3410 fn = builtin_decl_implicit (dcode);
3411 if (fn)
3412 set_user_assembler_name (fn, "___ieee_divdc3");
3416 bool
3417 darwin_libc_has_function (enum function_class fn_class)
3419 if (fn_class == function_sincos)
3420 return false;
3421 if (fn_class == function_c99_math_complex
3422 || fn_class == function_c99_misc)
3423 return (TARGET_64BIT
3424 || strverscmp (darwin_macosx_version_min, "10.3") >= 0);
3426 return true;
3429 static hashval_t
3430 cfstring_hash (const void *ptr)
3432 tree str = ((const struct cfstring_descriptor *)ptr)->literal;
3433 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3434 int i, len = TREE_STRING_LENGTH (str);
3435 hashval_t h = len;
3437 for (i = 0; i < len; i++)
3438 h = ((h * 613) + p[i]);
3440 return h;
3443 static int
3444 cfstring_eq (const void *ptr1, const void *ptr2)
3446 tree str1 = ((const struct cfstring_descriptor *)ptr1)->literal;
3447 tree str2 = ((const struct cfstring_descriptor *)ptr2)->literal;
3448 int len1 = TREE_STRING_LENGTH (str1);
3450 return (len1 == TREE_STRING_LENGTH (str2)
3451 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3452 len1));
3455 tree
3456 darwin_build_constant_cfstring (tree str)
3458 struct cfstring_descriptor *desc, key;
3459 void **loc;
3460 tree addr;
3462 if (!str)
3464 error ("CFString literal is missing");
3465 return error_mark_node;
3468 STRIP_NOPS (str);
3470 if (TREE_CODE (str) == ADDR_EXPR)
3471 str = TREE_OPERAND (str, 0);
3473 if (TREE_CODE (str) != STRING_CST)
3475 error ("CFString literal expression is not a string constant");
3476 return error_mark_node;
3479 /* Perhaps we already constructed a constant CFString just like this one? */
3480 key.literal = str;
3481 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3482 desc = (struct cfstring_descriptor *) *loc;
3484 if (!desc)
3486 tree var, constructor, field;
3487 vec<constructor_elt, va_gc> *v = NULL;
3488 int length = TREE_STRING_LENGTH (str) - 1;
3490 if (darwin_warn_nonportable_cfstrings)
3492 const char *s = TREE_STRING_POINTER (str);
3493 int l = 0;
3495 for (l = 0; l < length; l++)
3496 if (!s[l] || !isascii (s[l]))
3498 warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
3499 s[l] ? "non-ASCII character" : "embedded NUL");
3500 break;
3504 *loc = desc = ggc_alloc_cleared_cfstring_descriptor ();
3505 desc->literal = str;
3507 /* isa *. */
3508 field = TYPE_FIELDS (ccfstring_type_node);
3509 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3510 build1 (ADDR_EXPR, TREE_TYPE (field),
3511 cfstring_class_reference));
3512 /* flags */
3513 field = DECL_CHAIN (field);
3514 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3515 build_int_cst (TREE_TYPE (field), 0x000007c8));
3516 /* string *. */
3517 field = DECL_CHAIN (field);
3518 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3519 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3520 /* length */
3521 field = DECL_CHAIN (field);
3522 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3523 build_int_cst (TREE_TYPE (field), length));
3525 constructor = build_constructor (ccfstring_type_node, v);
3526 TREE_READONLY (constructor) = 1;
3527 TREE_CONSTANT (constructor) = 1;
3528 TREE_STATIC (constructor) = 1;
3530 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3531 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3532 being built without any knowledge of C++ tree accessors; hence, we shall
3533 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3534 if (darwin_running_cxx)
3535 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3537 /* Create an anonymous global variable for this CFString. */
3538 var = build_decl (input_location, CONST_DECL,
3539 NULL, TREE_TYPE (constructor));
3540 DECL_ARTIFICIAL (var) = 1;
3541 TREE_STATIC (var) = 1;
3542 DECL_INITIAL (var) = constructor;
3543 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3544 DECL_CONTEXT (var) = NULL_TREE;
3545 desc->constructor = var;
3548 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3549 TREE_CONSTANT (addr) = 1;
3551 return addr;
3554 bool
3555 darwin_cfstring_p (tree str)
3557 struct cfstring_descriptor key;
3558 void **loc;
3560 if (!str)
3561 return false;
3563 STRIP_NOPS (str);
3565 if (TREE_CODE (str) == ADDR_EXPR)
3566 str = TREE_OPERAND (str, 0);
3568 if (TREE_CODE (str) != STRING_CST)
3569 return false;
3571 key.literal = str;
3572 loc = htab_find_slot (cfstring_htab, &key, NO_INSERT);
3574 if (loc)
3575 return true;
3577 return false;
3580 void
3581 darwin_enter_string_into_cfstring_table (tree str)
3583 struct cfstring_descriptor key;
3584 void **loc;
3586 key.literal = str;
3587 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3589 if (!*loc)
3591 *loc = ggc_alloc_cleared_cfstring_descriptor ();
3592 ((struct cfstring_descriptor *)*loc)->literal = str;
3596 /* Choose named function section based on its frequency. */
3598 section *
3599 darwin_function_section (tree decl, enum node_frequency freq,
3600 bool startup, bool exit)
3602 /* Decide if we need to put this in a coalescable section. */
3603 bool weak = (decl
3604 && DECL_WEAK (decl)
3605 && (!DECL_ATTRIBUTES (decl)
3606 || !lookup_attribute ("weak_import",
3607 DECL_ATTRIBUTES (decl))));
3609 /* If there is a specified section name, we should not be trying to
3610 override. */
3611 if (decl && DECL_SECTION_NAME (decl) != NULL_TREE)
3612 return get_named_section (decl, NULL, 0);
3614 /* We always put unlikely executed stuff in the cold section. */
3615 if (freq == NODE_FREQUENCY_UNLIKELY_EXECUTED)
3616 return (weak) ? darwin_sections[text_cold_coal_section]
3617 : darwin_sections[text_cold_section];
3619 /* If we have LTO *and* feedback information, then let LTO handle
3620 the function ordering, it makes a better job (for normal, hot,
3621 startup and exit - hence the bailout for cold above). */
3622 if (in_lto_p && flag_profile_values)
3623 goto default_function_sections;
3625 /* Non-cold startup code should go to startup subsection. */
3626 if (startup)
3627 return (weak) ? darwin_sections[text_startup_coal_section]
3628 : darwin_sections[text_startup_section];
3630 /* Similarly for exit. */
3631 if (exit)
3632 return (weak) ? darwin_sections[text_exit_coal_section]
3633 : darwin_sections[text_exit_section];
3635 /* Place hot code. */
3636 if (freq == NODE_FREQUENCY_HOT)
3637 return (weak) ? darwin_sections[text_hot_coal_section]
3638 : darwin_sections[text_hot_section];
3640 /* Otherwise, default to the 'normal' non-reordered sections. */
3641 default_function_sections:
3642 return (weak) ? darwin_sections[text_coal_section]
3643 : text_section;
3646 /* When a function is partitioned between sections, we need to insert a label
3647 at the start of each new chunk - so that it may become a valid 'atom' for
3648 eh and debug purposes. Without this the linker will emit warnings if one
3649 tries to add line location information (since the switched fragment will
3650 be anonymous). */
3652 void
3653 darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
3655 char buf[128];
3656 snprintf (buf, 128, "%s%s",new_is_cold?"__cold_sect_of_":"__hot_sect_of_",
3657 IDENTIFIER_POINTER (DECL_NAME (decl)));
3658 /* Make sure we pick up all the relevant quotes etc. */
3659 assemble_name_raw (fp, (const char *) buf);
3660 fputs (":\n", fp);
3663 #include "gt-darwin.h"