PR target/56858
[official-gcc.git] / gcc / config / darwin.c
blobd13983ce811d5cf38f69bcdc9ab7d0297a044711
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"
64 #include "lto-section-names.h"
66 /* Darwin supports a feature called fix-and-continue, which is used
67 for rapid turn around debugging. When code is compiled with the
68 -mfix-and-continue flag, two changes are made to the generated code
69 that allow the system to do things that it would normally not be
70 able to do easily. These changes allow gdb to load in
71 recompilation of a translation unit that has been changed into a
72 running program and replace existing functions and methods of that
73 translation unit with versions of those functions and methods
74 from the newly compiled translation unit. The new functions access
75 the existing static symbols from the old translation unit, if the
76 symbol existed in the unit to be replaced, and from the new
77 translation unit, otherwise.
79 The changes are to insert 5 nops at the beginning of all functions
80 and to use indirection to get at static symbols. The 5 nops
81 are required by consumers of the generated code. Currently, gdb
82 uses this to patch in a jump to the overriding function, this
83 allows all uses of the old name to forward to the replacement,
84 including existing function pointers and virtual methods. See
85 rs6000_emit_prologue for the code that handles the nop insertions.
87 The added indirection allows gdb to redirect accesses to static
88 symbols from the newly loaded translation unit to the existing
89 symbol, if any. @code{static} symbols are special and are handled by
90 setting the second word in the .non_lazy_symbol_pointer data
91 structure to symbol. See indirect_data for the code that handles
92 the extra indirection, and machopic_output_indirection and its use
93 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
94 symbol indirection. */
96 /* For darwin >= 9 (OSX 10.5) the linker is capable of making the necessary
97 branch islands and we no longer need to emit darwin stubs.
98 However, if we are generating code for earlier systems (or for use in the
99 kernel) the stubs might still be required, and this will be set true. */
100 int darwin_emit_branch_islands = false;
102 typedef struct GTY(()) cdtor_record {
103 rtx symbol;
104 int priority; /* [con/de]structor priority */
105 int position; /* original position */
106 } cdtor_record;
108 static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL;
109 static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL;
111 /* A flag to determine whether we are running c++ or obj-c++. This has to be
112 settable from non-c-family contexts too (i.e. we can't use the c_dialect_
113 functions). */
114 int darwin_running_cxx;
116 /* Some code-gen now depends on OS major version numbers (at least). */
117 int generating_for_darwin_version ;
119 /* Section names. */
120 section * darwin_sections[NUM_DARWIN_SECTIONS];
122 /* While we transition to using in-tests instead of ifdef'd code. */
123 #ifndef HAVE_lo_sum
124 #define HAVE_lo_sum 0
125 #define gen_macho_high(a,b) (a)
126 #define gen_macho_low(a,b,c) (a)
127 #endif
129 /* True if we're setting __attribute__ ((ms_struct)). */
130 int darwin_ms_struct = false;
132 /* Earlier versions of Darwin as do not recognize an alignment field in
133 .comm directives, this should be set for versions that allow it. */
134 int emit_aligned_common = false;
136 /* A get_unnamed_section callback used to switch to an ObjC section.
137 DIRECTIVE is as for output_section_asm_op. */
139 static void
140 output_objc_section_asm_op (const void *directive)
142 static bool been_here = false;
144 /* The NeXT ObjC Runtime requires these sections to be present and in
145 order in the object. The code below implements this by emitting
146 a section header for each ObjC section the first time that an ObjC
147 section is requested. */
148 if (! been_here)
150 section *saved_in_section = in_section;
151 static const enum darwin_section_enum tomark[] =
153 /* written, cold -> hot */
154 objc_cat_cls_meth_section,
155 objc_cat_inst_meth_section,
156 objc_string_object_section,
157 objc_constant_string_object_section,
158 objc_selector_refs_section,
159 objc_selector_fixup_section,
160 objc_cls_refs_section,
161 objc_class_section,
162 objc_meta_class_section,
163 /* shared, hot -> cold */
164 objc_cls_meth_section,
165 objc_inst_meth_section,
166 objc_protocol_section,
167 objc_class_names_section,
168 objc_meth_var_types_section,
169 objc_meth_var_names_section,
170 objc_category_section,
171 objc_class_vars_section,
172 objc_instance_vars_section,
173 objc_module_info_section,
174 objc_symbols_section,
176 /* ABI=1 */
177 static const enum darwin_section_enum tomarkv1[] =
179 objc1_protocol_ext_section,
180 objc1_class_ext_section,
181 objc1_prop_list_section
183 /* ABI=2 */
184 static const enum darwin_section_enum tomarkv2[] =
186 objc2_message_refs_section,
187 objc2_classdefs_section,
188 objc2_metadata_section,
189 objc2_classrefs_section,
190 objc2_classlist_section,
191 objc2_categorylist_section,
192 objc2_selector_refs_section,
193 objc2_nonlazy_class_section,
194 objc2_nonlazy_category_section,
195 objc2_protocollist_section,
196 objc2_protocolrefs_section,
197 objc2_super_classrefs_section,
198 objc2_image_info_section,
199 objc2_constant_string_object_section
201 size_t i;
203 been_here = true;
204 if (flag_objc_abi < 2)
206 for (i = 0; i < ARRAY_SIZE (tomark); i++)
207 switch_to_section (darwin_sections[tomark[i]]);
208 if (flag_objc_abi == 1)
209 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
210 switch_to_section (darwin_sections[tomarkv1[i]]);
212 else
213 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
214 switch_to_section (darwin_sections[tomarkv2[i]]);
215 /* Make sure we don't get varasm.c out of sync with us. */
216 switch_to_section (saved_in_section);
218 output_section_asm_op (directive);
222 /* Private flag applied to disable section-anchors in a particular section. */
223 #define SECTION_NO_ANCHOR SECTION_MACH_DEP
226 /* Implement TARGET_ASM_INIT_SECTIONS. */
228 void
229 darwin_init_sections (void)
231 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
232 darwin_sections[NAME] = \
233 get_unnamed_section (FLAGS, (OBJC \
234 ? output_objc_section_asm_op \
235 : output_section_asm_op), \
236 "\t" DIRECTIVE);
237 #include "config/darwin-sections.def"
238 #undef DEF_SECTION
240 readonly_data_section = darwin_sections[const_section];
241 exception_section = darwin_sections[darwin_exception_section];
242 eh_frame_section = darwin_sections[darwin_eh_frame_section];
246 name_needs_quotes (const char *name)
248 int c;
249 while ((c = *name++) != '\0')
250 if (! ISIDNUM (c)
251 && c != '.' && c != '$' && c != '_' )
252 return 1;
253 return 0;
256 /* Return true if SYM_REF can be used without an indirection. */
258 machopic_symbol_defined_p (rtx sym_ref)
260 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
261 return true;
263 /* If a symbol references local and is not an extern to this
264 file, then the symbol might be able to declared as defined. */
265 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
267 /* If the symbol references a variable and the variable is a
268 common symbol, then this symbol is not defined. */
269 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
271 tree decl = SYMBOL_REF_DECL (sym_ref);
272 if (!decl)
273 return true;
274 if (DECL_COMMON (decl))
275 return false;
277 return true;
279 return false;
282 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
283 reference, which will not be changed. */
285 enum machopic_addr_class
286 machopic_classify_symbol (rtx sym_ref)
288 bool function_p;
290 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
291 if (machopic_symbol_defined_p (sym_ref))
292 return (function_p
293 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
294 else
295 return (function_p
296 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
299 #ifndef TARGET_FIX_AND_CONTINUE
300 #define TARGET_FIX_AND_CONTINUE 0
301 #endif
303 /* Indicate when fix-and-continue style code generation is being used
304 and when a reference to data should be indirected so that it can be
305 rebound in a new translation unit to reference the original instance
306 of that data. Symbol names that are for code generation local to
307 the translation unit are bound to the new translation unit;
308 currently this means symbols that begin with L or _OBJC_;
309 otherwise, we indicate that an indirect reference should be made to
310 permit the runtime to rebind new instances of the translation unit
311 to the original instance of the data. */
313 static int
314 indirect_data (rtx sym_ref)
316 int lprefix;
317 const char *name;
319 /* If we aren't generating fix-and-continue code, don't do anything
320 special. */
321 if (TARGET_FIX_AND_CONTINUE == 0)
322 return 0;
324 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
325 are indirected. Symbols that begin with L and _OBJC_ are always
326 bound to the current translation unit as they are used for
327 generated local data of the translation unit. */
329 name = XSTR (sym_ref, 0);
331 lprefix = (((name[0] == '*' || name[0] == '&')
332 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
333 || (strncmp (name, "_OBJC_", 6) == 0));
335 return ! lprefix;
338 static int
339 machopic_data_defined_p (rtx sym_ref)
341 if (indirect_data (sym_ref))
342 return 0;
344 switch (machopic_classify_symbol (sym_ref))
346 case MACHOPIC_DEFINED_DATA:
347 case MACHOPIC_DEFINED_FUNCTION:
348 return 1;
349 default:
350 return 0;
354 void
355 machopic_define_symbol (rtx mem)
357 rtx sym_ref;
359 gcc_assert (GET_CODE (mem) == MEM);
360 sym_ref = XEXP (mem, 0);
361 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
364 /* Return either ORIG or:
366 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
368 depending on MACHO_DYNAMIC_NO_PIC_P. */
370 machopic_gen_offset (rtx orig)
372 if (MACHO_DYNAMIC_NO_PIC_P)
373 return orig;
374 else
376 /* Play games to avoid marking the function as needing pic if we
377 are being called as part of the cost-estimation process. */
378 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
379 crtl->uses_pic_offset_table = 1;
380 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
381 UNSPEC_MACHOPIC_OFFSET);
382 return gen_rtx_CONST (Pmode, orig);
386 static GTY(()) const char * function_base_func_name;
387 static GTY(()) int current_pic_label_num;
388 static GTY(()) int emitted_pic_label_num;
390 static void
391 update_pic_label_number_if_needed (void)
393 const char *current_name;
395 /* When we are generating _get_pc thunks within stubs, there is no current
396 function. */
397 if (current_function_decl)
399 current_name =
400 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
401 if (function_base_func_name != current_name)
403 ++current_pic_label_num;
404 function_base_func_name = current_name;
407 else
409 ++current_pic_label_num;
410 function_base_func_name = "L_machopic_stub_dummy";
414 void
415 machopic_output_function_base_name (FILE *file)
417 /* If dynamic-no-pic is on, we should not get here. */
418 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
420 update_pic_label_number_if_needed ();
421 fprintf (file, "L%d$pb", current_pic_label_num);
424 char curr_picbasename[32];
426 const char *
427 machopic_get_function_picbase (void)
429 /* If dynamic-no-pic is on, we should not get here. */
430 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
432 update_pic_label_number_if_needed ();
433 snprintf (curr_picbasename, 32, "L%d$pb", current_pic_label_num);
434 return (const char *) curr_picbasename;
437 bool
438 machopic_should_output_picbase_label (void)
440 update_pic_label_number_if_needed ();
442 if (current_pic_label_num == emitted_pic_label_num)
443 return false;
445 emitted_pic_label_num = current_pic_label_num;
446 return true;
449 /* The suffix attached to non-lazy pointer symbols. */
450 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
451 /* The suffix attached to stub symbols. */
452 #define STUB_SUFFIX "$stub"
454 typedef struct GTY (()) machopic_indirection
456 /* The SYMBOL_REF for the entity referenced. */
457 rtx symbol;
458 /* The name of the stub or non-lazy pointer. */
459 const char * ptr_name;
460 /* True iff this entry is for a stub (as opposed to a non-lazy
461 pointer). */
462 bool stub_p;
463 /* True iff this stub or pointer pointer has been referenced. */
464 bool used;
465 } machopic_indirection;
467 /* A table mapping stub names and non-lazy pointer names to
468 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
470 static GTY ((param_is (struct machopic_indirection))) htab_t
471 machopic_indirections;
473 /* Return a hash value for a SLOT in the indirections hash table. */
475 static hashval_t
476 machopic_indirection_hash (const void *slot)
478 const machopic_indirection *p = (const machopic_indirection *) slot;
479 return htab_hash_string (p->ptr_name);
482 /* Returns true if the KEY is the same as that associated with
483 SLOT. */
485 static int
486 machopic_indirection_eq (const void *slot, const void *key)
488 return strcmp (((const machopic_indirection *) slot)->ptr_name,
489 (const char *) key) == 0;
492 /* Return the name of the non-lazy pointer (if STUB_P is false) or
493 stub (if STUB_B is true) corresponding to the given name. */
495 const char *
496 machopic_indirection_name (rtx sym_ref, bool stub_p)
498 char *buffer;
499 const char *name = XSTR (sym_ref, 0);
500 size_t namelen = strlen (name);
501 machopic_indirection *p;
502 void ** slot;
503 bool needs_quotes;
504 const char *suffix;
505 const char *prefix = user_label_prefix;
506 const char *quote = "";
507 tree id;
509 id = maybe_get_identifier (name);
510 if (id)
512 tree id_orig = id;
514 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
515 id = TREE_CHAIN (id);
516 if (id != id_orig)
518 name = IDENTIFIER_POINTER (id);
519 namelen = strlen (name);
523 if (name[0] == '*')
525 prefix = "";
526 ++name;
527 --namelen;
530 needs_quotes = name_needs_quotes (name);
531 if (needs_quotes)
533 quote = "\"";
536 if (stub_p)
537 suffix = STUB_SUFFIX;
538 else
539 suffix = NON_LAZY_POINTER_SUFFIX;
541 buffer = XALLOCAVEC (char, strlen ("&L")
542 + strlen (prefix)
543 + namelen
544 + strlen (suffix)
545 + 2 * strlen (quote)
546 + 1 /* '\0' */);
548 /* Construct the name of the non-lazy pointer or stub. */
549 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
551 if (!machopic_indirections)
552 machopic_indirections = htab_create_ggc (37,
553 machopic_indirection_hash,
554 machopic_indirection_eq,
555 /*htab_del=*/NULL);
557 slot = htab_find_slot_with_hash (machopic_indirections, buffer,
558 htab_hash_string (buffer), INSERT);
559 if (*slot)
561 p = (machopic_indirection *) *slot;
563 else
565 p = ggc_alloc<machopic_indirection> ();
566 p->symbol = sym_ref;
567 p->ptr_name = xstrdup (buffer);
568 p->stub_p = stub_p;
569 p->used = false;
570 *slot = p;
573 return p->ptr_name;
576 /* Return the name of the stub for the mcount function. */
578 const char*
579 machopic_mcount_stub_name (void)
581 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
582 return machopic_indirection_name (symbol, /*stub_p=*/true);
585 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
586 or non-lazy pointer as used -- and mark the object to which the
587 pointer/stub refers as used as well, since the pointer/stub will
588 emit a reference to it. */
590 void
591 machopic_validate_stub_or_non_lazy_ptr (const char *name)
593 machopic_indirection *p;
595 p = ((machopic_indirection *)
596 (htab_find_with_hash (machopic_indirections, name,
597 htab_hash_string (name))));
598 if (p && ! p->used)
600 const char *real_name;
601 tree id;
603 p->used = true;
605 /* Do what output_addr_const will do when we actually call it. */
606 if (SYMBOL_REF_DECL (p->symbol))
607 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
609 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
611 id = maybe_get_identifier (real_name);
612 if (id)
613 mark_referenced (id);
617 /* Transform ORIG, which may be any data source, to the corresponding
618 source using indirections. */
621 machopic_indirect_data_reference (rtx orig, rtx reg)
623 rtx ptr_ref = orig;
625 if (! MACHOPIC_INDIRECT)
626 return orig;
628 if (GET_CODE (orig) == SYMBOL_REF)
630 int defined = machopic_data_defined_p (orig);
632 if (defined && MACHO_DYNAMIC_NO_PIC_P)
634 if (DARWIN_PPC)
636 /* Create a new register for CSE opportunities. */
637 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
638 emit_insn (gen_macho_high (hi_reg, orig));
639 emit_insn (gen_macho_low (reg, hi_reg, orig));
640 return reg;
642 else if (DARWIN_X86)
643 return orig;
644 else
645 /* some other cpu -- writeme! */
646 gcc_unreachable ();
648 else if (defined)
650 rtx offset = NULL;
651 if (DARWIN_PPC || HAVE_lo_sum)
652 offset = machopic_gen_offset (orig);
654 if (DARWIN_PPC)
656 rtx hi_sum_reg = (!can_create_pseudo_p ()
657 ? reg
658 : gen_reg_rtx (Pmode));
660 gcc_assert (reg);
662 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
663 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
664 gen_rtx_HIGH (Pmode, offset))));
665 emit_insn (gen_rtx_SET (Pmode, reg,
666 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
667 copy_rtx (offset))));
669 orig = reg;
671 else if (HAVE_lo_sum)
673 gcc_assert (reg);
675 emit_insn (gen_rtx_SET (VOIDmode, reg,
676 gen_rtx_HIGH (Pmode, offset)));
677 emit_insn (gen_rtx_SET (VOIDmode, reg,
678 gen_rtx_LO_SUM (Pmode, reg,
679 copy_rtx (offset))));
680 emit_use (pic_offset_table_rtx);
682 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
684 return orig;
687 ptr_ref = (gen_rtx_SYMBOL_REF
688 (Pmode,
689 machopic_indirection_name (orig, /*stub_p=*/false)));
691 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
693 ptr_ref = gen_const_mem (Pmode, ptr_ref);
694 machopic_define_symbol (ptr_ref);
696 if (DARWIN_X86
697 && reg
698 && MACHO_DYNAMIC_NO_PIC_P)
700 emit_insn (gen_rtx_SET (Pmode, reg, ptr_ref));
701 ptr_ref = reg;
704 return ptr_ref;
706 else if (GET_CODE (orig) == CONST)
708 /* If "(const (plus ...", walk the PLUS and return that result.
709 PLUS processing (below) will restore the "(const ..." if
710 appropriate. */
711 if (GET_CODE (XEXP (orig, 0)) == PLUS)
712 return machopic_indirect_data_reference (XEXP (orig, 0), reg);
713 else
714 return orig;
716 else if (GET_CODE (orig) == MEM)
718 XEXP (ptr_ref, 0) =
719 machopic_indirect_data_reference (XEXP (orig, 0), reg);
720 return ptr_ref;
722 else if (GET_CODE (orig) == PLUS)
724 rtx base, result;
725 /* When the target is i386, this code prevents crashes due to the
726 compiler's ignorance on how to move the PIC base register to
727 other registers. (The reload phase sometimes introduces such
728 insns.) */
729 if (GET_CODE (XEXP (orig, 0)) == REG
730 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
731 /* Prevent the same register from being erroneously used
732 as both the base and index registers. */
733 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
734 && reg)
736 emit_move_insn (reg, XEXP (orig, 0));
737 XEXP (ptr_ref, 0) = reg;
738 return ptr_ref;
741 /* Legitimize both operands of the PLUS. */
742 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
743 orig = machopic_indirect_data_reference (XEXP (orig, 1),
744 (base == reg ? 0 : reg));
745 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
746 result = plus_constant (Pmode, base, INTVAL (orig));
747 else
748 result = gen_rtx_PLUS (Pmode, base, orig);
750 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
752 if (reg)
754 emit_move_insn (reg, result);
755 result = reg;
757 else
759 result = force_reg (GET_MODE (result), result);
763 return result;
765 return ptr_ref;
768 /* Transform TARGET (a MEM), which is a function call target, to the
769 corresponding symbol_stub if necessary. Return a new MEM. */
772 machopic_indirect_call_target (rtx target)
774 if (! darwin_emit_branch_islands)
775 return target;
777 if (GET_CODE (target) != MEM)
778 return target;
780 if (MACHOPIC_INDIRECT
781 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
782 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
783 & MACHO_SYMBOL_FLAG_DEFINED))
785 rtx sym_ref = XEXP (target, 0);
786 const char *stub_name = machopic_indirection_name (sym_ref,
787 /*stub_p=*/true);
788 enum machine_mode mode = GET_MODE (sym_ref);
790 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
791 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
792 MEM_READONLY_P (target) = 1;
793 MEM_NOTRAP_P (target) = 1;
796 return target;
800 machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
802 rtx pic_ref = orig;
804 if (! MACHOPIC_INDIRECT)
805 return orig;
807 /* First handle a simple SYMBOL_REF or LABEL_REF */
808 if (GET_CODE (orig) == LABEL_REF
809 || (GET_CODE (orig) == SYMBOL_REF
812 /* addr(foo) = &func+(foo-func) */
813 orig = machopic_indirect_data_reference (orig, reg);
815 if (GET_CODE (orig) == PLUS
816 && GET_CODE (XEXP (orig, 0)) == REG)
818 if (reg == 0)
819 return force_reg (mode, orig);
821 emit_move_insn (reg, orig);
822 return reg;
825 if (GET_CODE (orig) == MEM)
827 if (reg == 0)
829 gcc_assert (!reload_in_progress);
830 reg = gen_reg_rtx (Pmode);
833 #if HAVE_lo_sum
834 if (MACHO_DYNAMIC_NO_PIC_P
835 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
836 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
838 #if defined (TARGET_TOC) /* ppc */
839 rtx temp_reg = (!can_create_pseudo_p ()
840 ? reg :
841 gen_reg_rtx (Pmode));
842 rtx asym = XEXP (orig, 0);
843 rtx mem;
845 emit_insn (gen_macho_high (temp_reg, asym));
846 mem = gen_const_mem (GET_MODE (orig),
847 gen_rtx_LO_SUM (Pmode, temp_reg,
848 copy_rtx (asym)));
849 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
850 #else
851 /* Some other CPU -- WriteMe! but right now there are no other
852 platforms that can use dynamic-no-pic */
853 gcc_unreachable ();
854 #endif
855 pic_ref = reg;
857 else
858 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
859 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
861 rtx offset = machopic_gen_offset (XEXP (orig, 0));
862 #if defined (TARGET_TOC) /* i.e., PowerPC */
863 /* Generating a new reg may expose opportunities for
864 common subexpression elimination. */
865 rtx hi_sum_reg = (!can_create_pseudo_p ()
866 ? reg
867 : gen_reg_rtx (Pmode));
868 rtx mem;
869 rtx insn;
870 rtx sum;
872 sum = gen_rtx_HIGH (Pmode, offset);
873 if (! MACHO_DYNAMIC_NO_PIC_P)
874 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
876 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
878 mem = gen_const_mem (GET_MODE (orig),
879 gen_rtx_LO_SUM (Pmode,
880 hi_sum_reg,
881 copy_rtx (offset)));
882 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
883 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
885 pic_ref = reg;
886 #else
887 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
889 emit_insn (gen_rtx_SET (VOIDmode, reg,
890 gen_rtx_HIGH (Pmode,
891 gen_rtx_CONST (Pmode,
892 offset))));
893 emit_insn (gen_rtx_SET (VOIDmode, reg,
894 gen_rtx_LO_SUM (Pmode, reg,
895 gen_rtx_CONST (Pmode,
896 copy_rtx (offset)))));
897 pic_ref = gen_rtx_PLUS (Pmode,
898 pic_offset_table_rtx, reg);
899 #endif
901 else
902 #endif /* HAVE_lo_sum */
904 rtx pic = pic_offset_table_rtx;
905 if (GET_CODE (pic) != REG)
907 emit_move_insn (reg, pic);
908 pic = reg;
910 #if 0
911 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
912 #endif
914 if (reload_in_progress)
915 df_set_regs_ever_live (REGNO (pic), true);
916 pic_ref = gen_rtx_PLUS (Pmode, pic,
917 machopic_gen_offset (XEXP (orig, 0)));
920 #if !defined (TARGET_TOC)
921 emit_move_insn (reg, pic_ref);
922 pic_ref = gen_const_mem (GET_MODE (orig), reg);
923 #endif
925 else
928 #if HAVE_lo_sum
929 if (GET_CODE (orig) == SYMBOL_REF
930 || GET_CODE (orig) == LABEL_REF)
932 rtx offset = machopic_gen_offset (orig);
933 #if defined (TARGET_TOC) /* i.e., PowerPC */
934 rtx hi_sum_reg;
936 if (reg == 0)
938 gcc_assert (!reload_in_progress);
939 reg = gen_reg_rtx (Pmode);
942 hi_sum_reg = reg;
944 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
945 (MACHO_DYNAMIC_NO_PIC_P)
946 ? gen_rtx_HIGH (Pmode, offset)
947 : gen_rtx_PLUS (Pmode,
948 pic_offset_table_rtx,
949 gen_rtx_HIGH (Pmode,
950 offset))));
951 emit_insn (gen_rtx_SET (VOIDmode, reg,
952 gen_rtx_LO_SUM (Pmode,
953 hi_sum_reg,
954 copy_rtx (offset))));
955 pic_ref = reg;
956 #else
957 emit_insn (gen_rtx_SET (VOIDmode, reg,
958 gen_rtx_HIGH (Pmode, offset)));
959 emit_insn (gen_rtx_SET (VOIDmode, reg,
960 gen_rtx_LO_SUM (Pmode, reg,
961 copy_rtx (offset))));
962 pic_ref = gen_rtx_PLUS (Pmode,
963 pic_offset_table_rtx, reg);
964 #endif
966 else
967 #endif /* HAVE_lo_sum */
969 if (REG_P (orig)
970 || GET_CODE (orig) == SUBREG)
972 return orig;
974 else
976 rtx pic = pic_offset_table_rtx;
977 if (GET_CODE (pic) != REG)
979 emit_move_insn (reg, pic);
980 pic = reg;
982 #if 0
983 emit_use (pic_offset_table_rtx);
984 #endif
985 if (reload_in_progress)
986 df_set_regs_ever_live (REGNO (pic), true);
987 pic_ref = gen_rtx_PLUS (Pmode,
988 pic,
989 machopic_gen_offset (orig));
994 if (GET_CODE (pic_ref) != REG)
996 if (reg != 0)
998 emit_move_insn (reg, pic_ref);
999 return reg;
1001 else
1003 return force_reg (mode, pic_ref);
1006 else
1008 return pic_ref;
1012 else if (GET_CODE (orig) == SYMBOL_REF)
1013 return orig;
1015 else if (GET_CODE (orig) == PLUS
1016 && (GET_CODE (XEXP (orig, 0)) == MEM
1017 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
1018 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
1019 && XEXP (orig, 0) != pic_offset_table_rtx
1020 && GET_CODE (XEXP (orig, 1)) != REG)
1023 rtx base;
1024 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
1026 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1027 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
1028 Pmode, (base == reg ? 0 : reg));
1029 if (GET_CODE (orig) == CONST_INT)
1031 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
1032 is_complex = 1;
1034 else
1035 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
1037 if (reg && is_complex)
1039 emit_move_insn (reg, pic_ref);
1040 pic_ref = reg;
1042 /* Likewise, should we set special REG_NOTEs here? */
1045 else if (GET_CODE (orig) == CONST)
1047 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1050 else if (GET_CODE (orig) == MEM
1051 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
1053 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1054 addr = replace_equiv_address (orig, addr);
1055 emit_move_insn (reg, addr);
1056 pic_ref = reg;
1059 return pic_ref;
1062 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
1063 DATA is the FILE* for assembly output. Called from
1064 htab_traverse. */
1066 static int
1067 machopic_output_indirection (void **slot, void *data)
1069 machopic_indirection *p = *((machopic_indirection **) slot);
1070 FILE *asm_out_file = (FILE *) data;
1071 rtx symbol;
1072 const char *sym_name;
1073 const char *ptr_name;
1075 if (!p->used)
1076 return 1;
1078 symbol = p->symbol;
1079 sym_name = XSTR (symbol, 0);
1080 ptr_name = p->ptr_name;
1082 if (p->stub_p)
1084 char *sym;
1085 char *stub;
1086 tree id;
1088 id = maybe_get_identifier (sym_name);
1089 if (id)
1091 tree id_orig = id;
1093 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1094 id = TREE_CHAIN (id);
1095 if (id != id_orig)
1096 sym_name = IDENTIFIER_POINTER (id);
1099 sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1100 if (sym_name[0] == '*' || sym_name[0] == '&')
1101 strcpy (sym, sym_name + 1);
1102 else if (sym_name[0] == '-' || sym_name[0] == '+')
1103 strcpy (sym, sym_name);
1104 else
1105 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1107 stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1108 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1109 strcpy (stub, ptr_name + 1);
1110 else
1111 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1113 machopic_output_stub (asm_out_file, sym, stub);
1115 else if (! indirect_data (symbol)
1116 && (machopic_symbol_defined_p (symbol)
1117 || SYMBOL_REF_LOCAL_P (symbol)))
1119 switch_to_section (data_section);
1120 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1121 assemble_label (asm_out_file, ptr_name);
1122 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1123 GET_MODE_SIZE (Pmode),
1124 GET_MODE_ALIGNMENT (Pmode), 1);
1126 else
1128 rtx init = const0_rtx;
1130 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1132 /* Mach-O symbols are passed around in code through indirect
1133 references and the original symbol_ref hasn't passed through
1134 the generic handling and reference-catching in
1135 output_operand, so we need to manually mark weak references
1136 as such. */
1137 if (SYMBOL_REF_WEAK (symbol))
1139 tree decl = SYMBOL_REF_DECL (symbol);
1140 gcc_assert (DECL_P (decl));
1142 if (decl != NULL_TREE
1143 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
1144 /* Handle only actual external-only definitions, not
1145 e.g. extern inline code or variables for which
1146 storage has been allocated. */
1147 && !TREE_STATIC (decl))
1149 fputs ("\t.weak_reference ", asm_out_file);
1150 assemble_name (asm_out_file, sym_name);
1151 fputc ('\n', asm_out_file);
1155 assemble_name (asm_out_file, ptr_name);
1156 fprintf (asm_out_file, ":\n");
1158 fprintf (asm_out_file, "\t.indirect_symbol ");
1159 assemble_name (asm_out_file, sym_name);
1160 fprintf (asm_out_file, "\n");
1162 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1163 have their symbol name instead of 0 in the second entry of
1164 the non-lazy symbol pointer data structure when they are
1165 defined. This allows the runtime to rebind newer instances
1166 of the translation unit with the original instance of the
1167 symbol. */
1169 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1170 && machopic_symbol_defined_p (symbol))
1171 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1173 assemble_integer (init, GET_MODE_SIZE (Pmode),
1174 GET_MODE_ALIGNMENT (Pmode), 1);
1177 return 1;
1180 void
1181 machopic_finish (FILE *asm_out_file)
1183 if (machopic_indirections)
1184 htab_traverse_noresize (machopic_indirections,
1185 machopic_output_indirection,
1186 asm_out_file);
1190 machopic_operand_p (rtx op)
1192 if (MACHOPIC_JUST_INDIRECT)
1193 return (GET_CODE (op) == SYMBOL_REF
1194 && machopic_symbol_defined_p (op));
1195 else
1196 return (GET_CODE (op) == CONST
1197 && GET_CODE (XEXP (op, 0)) == UNSPEC
1198 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1201 /* This function records whether a given name corresponds to a defined
1202 or undefined function or variable, for machopic_classify_ident to
1203 use later. */
1205 void
1206 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1208 rtx sym_ref;
1210 /* Do the standard encoding things first. */
1211 default_encode_section_info (decl, rtl, first);
1213 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1214 return;
1216 sym_ref = XEXP (rtl, 0);
1217 if (TREE_CODE (decl) == VAR_DECL)
1218 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1220 if (!DECL_EXTERNAL (decl)
1221 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1222 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1223 && ((TREE_STATIC (decl)
1224 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1225 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1226 && DECL_INITIAL (decl) != error_mark_node)))
1227 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1229 if (! TREE_PUBLIC (decl))
1230 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1233 void
1234 darwin_mark_decl_preserved (const char *name)
1236 fprintf (asm_out_file, "\t.no_dead_strip ");
1237 assemble_name (asm_out_file, name);
1238 fputc ('\n', asm_out_file);
1241 static section *
1242 darwin_rodata_section (int weak, bool zsize)
1244 return (weak
1245 ? darwin_sections[const_coal_section]
1246 : (zsize ? darwin_sections[zobj_const_section]
1247 : darwin_sections[const_section]));
1250 static section *
1251 darwin_mergeable_string_section (tree exp,
1252 unsigned HOST_WIDE_INT align)
1254 /* Darwin's ld expects to see non-writable string literals in the .cstring
1255 section. Later versions of ld check and complain when CFStrings are
1256 enabled. Therefore we shall force the strings into .cstring since we
1257 don't support writable ones anyway. */
1258 if ((darwin_constant_cfstrings || flag_merge_constants)
1259 && TREE_CODE (exp) == STRING_CST
1260 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1261 && align <= 256
1262 && (int_size_in_bytes (TREE_TYPE (exp))
1263 == TREE_STRING_LENGTH (exp))
1264 && ((size_t) TREE_STRING_LENGTH (exp)
1265 == strlen (TREE_STRING_POINTER (exp)) + 1))
1266 return darwin_sections[cstring_section];
1268 if (DARWIN_SECTION_ANCHORS && flag_section_anchors
1269 && TREE_CODE (exp) == STRING_CST
1270 && TREE_STRING_LENGTH (exp) == 0)
1271 return darwin_sections[zobj_const_section];
1273 return readonly_data_section;
1276 #ifndef HAVE_GAS_LITERAL16
1277 #define HAVE_GAS_LITERAL16 0
1278 #endif
1280 static section *
1281 darwin_mergeable_constant_section (tree exp,
1282 unsigned HOST_WIDE_INT align,
1283 bool zsize)
1285 enum machine_mode mode = DECL_MODE (exp);
1286 unsigned int modesize = GET_MODE_BITSIZE (mode);
1288 if (DARWIN_SECTION_ANCHORS
1289 && flag_section_anchors
1290 && zsize)
1291 return darwin_sections[zobj_const_section];
1293 if (flag_merge_constants
1294 && mode != VOIDmode
1295 && mode != BLKmode
1296 && modesize <= align
1297 && align >= 8
1298 && align <= 256
1299 && (align & (align -1)) == 0)
1301 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1303 if (TREE_CODE (size) == INTEGER_CST)
1305 if (wi::eq_p (size, 4))
1306 return darwin_sections[literal4_section];
1307 else if (wi::eq_p (size, 8))
1308 return darwin_sections[literal8_section];
1309 else if (HAVE_GAS_LITERAL16
1310 && TARGET_64BIT
1311 && wi::eq_p (size, 16))
1312 return darwin_sections[literal16_section];
1316 return readonly_data_section;
1319 section *
1320 darwin_tm_clone_table_section (void)
1322 return get_named_section (NULL,
1323 "__DATA,__tm_clone_table,regular,no_dead_strip",
1328 machopic_reloc_rw_mask (void)
1330 return MACHOPIC_INDIRECT ? 3 : 0;
1333 /* We have to deal with ObjC/C++ metadata section placement in the common
1334 code, since it will also be called from LTO.
1336 Return metadata attributes, if present (searching for ABI=2 first)
1337 Return NULL_TREE if no such attributes are found. */
1339 static tree
1340 is_objc_metadata (tree decl)
1342 if (DECL_P (decl)
1343 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1344 && DECL_ATTRIBUTES (decl))
1346 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1347 if (meta)
1348 return meta;
1349 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1350 if (meta)
1351 return meta;
1353 return NULL_TREE;
1356 static int classes_seen;
1357 static int objc_metadata_seen;
1359 /* Return the section required for Objective C ABI 2 metadata. */
1360 static section *
1361 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1363 const char *p;
1364 tree ident = TREE_VALUE (meta);
1365 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1366 p = IDENTIFIER_POINTER (ident);
1368 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi == 2);
1370 objc_metadata_seen = 1;
1372 if (base == data_section)
1373 base = darwin_sections[objc2_metadata_section];
1375 /* Most of the OBJC2 META-data end up in the base section, so check it
1376 first. */
1377 if (!strncmp (p, "V2_BASE", 7))
1378 return base;
1379 else if (!strncmp (p, "V2_STRG", 7))
1380 return darwin_sections[cstring_section];
1382 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1383 return darwin_sections[objc2_classdefs_section];
1384 else if (!strncmp (p, "V2_MREF", 7))
1385 return darwin_sections[objc2_message_refs_section];
1386 else if (!strncmp (p, "V2_CLRF", 7))
1387 return darwin_sections[objc2_classrefs_section];
1388 else if (!strncmp (p, "V2_SURF", 7))
1389 return darwin_sections[objc2_super_classrefs_section];
1390 else if (!strncmp (p, "V2_NLCL", 7))
1391 return darwin_sections[objc2_nonlazy_class_section];
1392 else if (!strncmp (p, "V2_CLAB", 7))
1394 classes_seen = 1;
1395 return darwin_sections[objc2_classlist_section];
1397 else if (!strncmp (p, "V2_SRFS", 7))
1398 return darwin_sections[objc2_selector_refs_section];
1399 else if (!strncmp (p, "V2_NLCA", 7))
1400 return darwin_sections[objc2_nonlazy_category_section];
1401 else if (!strncmp (p, "V2_CALA", 7))
1402 return darwin_sections[objc2_categorylist_section];
1404 else if (!strncmp (p, "V2_PLST", 7))
1405 return darwin_sections[objc2_protocollist_section];
1406 else if (!strncmp (p, "V2_PRFS", 7))
1407 return darwin_sections[objc2_protocolrefs_section];
1409 else if (!strncmp (p, "V2_INFO", 7))
1410 return darwin_sections[objc2_image_info_section];
1412 else if (!strncmp (p, "V2_EHTY", 7))
1413 return darwin_sections[data_coal_section];
1415 else if (!strncmp (p, "V2_CSTR", 7))
1416 return darwin_sections[objc2_constant_string_object_section];
1418 /* Not recognized, default. */
1419 return base;
1422 /* Return the section required for Objective C ABI 0/1 metadata. */
1423 static section *
1424 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1426 const char *p;
1427 tree ident = TREE_VALUE (meta);
1428 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1429 p = IDENTIFIER_POINTER (ident);
1431 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi < 2);
1433 objc_metadata_seen = 1;
1435 /* String sections first, cos there are lots of strings. */
1436 if (!strncmp (p, "V1_STRG", 7))
1437 return darwin_sections[cstring_section];
1438 else if (!strncmp (p, "V1_CLSN", 7))
1439 return darwin_sections[objc_class_names_section];
1440 else if (!strncmp (p, "V1_METN", 7))
1441 return darwin_sections[objc_meth_var_names_section];
1442 else if (!strncmp (p, "V1_METT", 7))
1443 return darwin_sections[objc_meth_var_types_section];
1445 else if (!strncmp (p, "V1_CLAS", 7))
1447 classes_seen = 1;
1448 return darwin_sections[objc_class_section];
1450 else if (!strncmp (p, "V1_META", 7))
1451 return darwin_sections[objc_meta_class_section];
1452 else if (!strncmp (p, "V1_CATG", 7))
1453 return darwin_sections[objc_category_section];
1454 else if (!strncmp (p, "V1_PROT", 7))
1455 return darwin_sections[objc_protocol_section];
1457 else if (!strncmp (p, "V1_CLCV", 7))
1458 return darwin_sections[objc_class_vars_section];
1459 else if (!strncmp (p, "V1_CLIV", 7))
1460 return darwin_sections[objc_instance_vars_section];
1462 else if (!strncmp (p, "V1_CLCM", 7))
1463 return darwin_sections[objc_cls_meth_section];
1464 else if (!strncmp (p, "V1_CLIM", 7))
1465 return darwin_sections[objc_inst_meth_section];
1466 else if (!strncmp (p, "V1_CACM", 7))
1467 return darwin_sections[objc_cat_cls_meth_section];
1468 else if (!strncmp (p, "V1_CAIM", 7))
1469 return darwin_sections[objc_cat_inst_meth_section];
1470 else if (!strncmp (p, "V1_PNSM", 7))
1471 return darwin_sections[objc_cat_inst_meth_section];
1472 else if (!strncmp (p, "V1_PCLM", 7))
1473 return darwin_sections[objc_cat_cls_meth_section];
1475 else if (!strncmp (p, "V1_CLPR", 7))
1476 return darwin_sections[objc_cat_cls_meth_section];
1477 else if (!strncmp (p, "V1_CAPR", 7))
1478 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1480 else if (!strncmp (p, "V1_PRFS", 7))
1481 return darwin_sections[objc_cat_cls_meth_section];
1482 else if (!strncmp (p, "V1_CLRF", 7))
1483 return darwin_sections[objc_cls_refs_section];
1484 else if (!strncmp (p, "V1_SRFS", 7))
1485 return darwin_sections[objc_selector_refs_section];
1487 else if (!strncmp (p, "V1_MODU", 7))
1488 return darwin_sections[objc_module_info_section];
1489 else if (!strncmp (p, "V1_SYMT", 7))
1490 return darwin_sections[objc_symbols_section];
1491 else if (!strncmp (p, "V1_INFO", 7))
1492 return darwin_sections[objc_image_info_section];
1494 else if (!strncmp (p, "V1_PLST", 7))
1495 return darwin_sections[objc1_prop_list_section];
1496 else if (!strncmp (p, "V1_PEXT", 7))
1497 return darwin_sections[objc1_protocol_ext_section];
1498 else if (!strncmp (p, "V1_CEXT", 7))
1499 return darwin_sections[objc1_class_ext_section];
1501 else if (!strncmp (p, "V2_CSTR", 7))
1502 return darwin_sections[objc_constant_string_object_section];
1504 return base;
1507 section *
1508 machopic_select_section (tree decl,
1509 int reloc,
1510 unsigned HOST_WIDE_INT align)
1512 bool zsize, one, weak, ro;
1513 section *base_section = NULL;
1515 weak = (DECL_P (decl)
1516 && DECL_WEAK (decl)
1517 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1519 zsize = (DECL_P (decl)
1520 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1521 && tree_to_uhwi (DECL_SIZE_UNIT (decl)) == 0);
1523 one = DECL_P (decl)
1524 && TREE_CODE (decl) == VAR_DECL
1525 && DECL_COMDAT_GROUP (decl);
1527 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1529 switch (categorize_decl_for_section (decl, reloc))
1531 case SECCAT_TEXT:
1532 gcc_unreachable ();
1533 break;
1535 case SECCAT_RODATA:
1536 case SECCAT_SRODATA:
1537 base_section = darwin_rodata_section (weak, zsize);
1538 break;
1540 case SECCAT_RODATA_MERGE_STR:
1541 base_section = darwin_mergeable_string_section (decl, align);
1542 break;
1544 case SECCAT_RODATA_MERGE_STR_INIT:
1545 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1546 break;
1548 case SECCAT_RODATA_MERGE_CONST:
1549 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1550 break;
1552 case SECCAT_DATA:
1553 case SECCAT_DATA_REL:
1554 case SECCAT_DATA_REL_LOCAL:
1555 case SECCAT_DATA_REL_RO:
1556 case SECCAT_DATA_REL_RO_LOCAL:
1557 case SECCAT_SDATA:
1558 case SECCAT_TDATA:
1559 if (weak || one)
1561 if (ro)
1562 base_section = darwin_sections[const_data_coal_section];
1563 else
1564 base_section = darwin_sections[data_coal_section];
1566 else if (DARWIN_SECTION_ANCHORS
1567 && flag_section_anchors
1568 && zsize)
1570 /* If we're doing section anchors, then punt zero-sized objects into
1571 their own sections so that they don't interfere with offset
1572 computation for the remaining vars. This does not need to be done
1573 for stuff in mergeable sections, since these are ineligible for
1574 anchors. */
1575 if (ro)
1576 base_section = darwin_sections[zobj_const_data_section];
1577 else
1578 base_section = darwin_sections[zobj_data_section];
1580 else if (ro)
1581 base_section = darwin_sections[const_data_section];
1582 else
1583 base_section = data_section;
1584 break;
1585 case SECCAT_BSS:
1586 case SECCAT_SBSS:
1587 case SECCAT_TBSS:
1588 if (weak || one)
1589 base_section = darwin_sections[data_coal_section];
1590 else
1592 if (!TREE_PUBLIC (decl))
1593 base_section = lcomm_section;
1594 else if (bss_noswitch_section)
1595 base_section = bss_noswitch_section;
1596 else
1597 base_section = data_section;
1599 break;
1601 default:
1602 gcc_unreachable ();
1605 /* Darwin weird special cases.
1606 a) OBJC Meta-data. */
1607 if (DECL_P (decl)
1608 && (TREE_CODE (decl) == VAR_DECL
1609 || TREE_CODE (decl) == CONST_DECL)
1610 && DECL_ATTRIBUTES (decl))
1612 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1613 if (meta)
1614 return darwin_objc2_section (decl, meta, base_section);
1615 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1616 if (meta)
1617 return darwin_objc1_section (decl, meta, base_section);
1618 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1619 if (meta)
1620 return base_section; /* GNU runtime is happy with it all in one pot. */
1623 /* b) Constant string objects. */
1624 if (TREE_CODE (decl) == CONSTRUCTOR
1625 && TREE_TYPE (decl)
1626 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1627 && TYPE_NAME (TREE_TYPE (decl)))
1629 tree name = TYPE_NAME (TREE_TYPE (decl));
1630 if (TREE_CODE (name) == TYPE_DECL)
1631 name = DECL_NAME (name);
1633 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1635 if (flag_next_runtime)
1637 if (flag_objc_abi == 2)
1638 return darwin_sections[objc2_constant_string_object_section];
1639 else
1640 return darwin_sections[objc_constant_string_object_section];
1642 else
1643 return darwin_sections[objc_string_object_section];
1645 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1646 return darwin_sections[cfstring_constant_object_section];
1647 else
1648 return base_section;
1650 /* c) legacy meta-data selection. */
1651 else if (TREE_CODE (decl) == VAR_DECL
1652 && DECL_NAME (decl)
1653 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1654 && IDENTIFIER_POINTER (DECL_NAME (decl))
1655 && flag_next_runtime
1656 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1658 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1659 static bool warned_objc_46 = false;
1660 /* We shall assert that zero-sized objects are an error in ObjC
1661 meta-data. */
1662 gcc_assert (tree_to_uhwi (DECL_SIZE_UNIT (decl)) != 0);
1664 /* ??? This mechanism for determining the metadata section is
1665 broken when LTO is in use, since the frontend that generated
1666 the data is not identified. We will keep the capability for
1667 the short term - in case any non-Objective-C programs are using
1668 it to place data in specified sections. */
1669 if (!warned_objc_46)
1671 location_t loc = DECL_SOURCE_LOCATION (decl);
1672 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
1673 " to select meta-data sections is deprecated at 4.6"
1674 " and will be removed in 4.7");
1675 warned_objc_46 = true;
1678 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1679 return darwin_sections[objc_cls_meth_section];
1680 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1681 return darwin_sections[objc_inst_meth_section];
1682 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
1683 return darwin_sections[objc_cat_cls_meth_section];
1684 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
1685 return darwin_sections[objc_cat_inst_meth_section];
1686 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1687 return darwin_sections[objc_class_vars_section];
1688 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1689 return darwin_sections[objc_instance_vars_section];
1690 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1691 return darwin_sections[objc_cat_cls_meth_section];
1692 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1693 return darwin_sections[objc_class_names_section];
1694 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1695 return darwin_sections[objc_meth_var_names_section];
1696 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1697 return darwin_sections[objc_meth_var_types_section];
1698 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1699 return darwin_sections[objc_cls_refs_section];
1700 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1701 return darwin_sections[objc_class_section];
1702 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1703 return darwin_sections[objc_meta_class_section];
1704 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1705 return darwin_sections[objc_category_section];
1706 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1707 return darwin_sections[objc_selector_refs_section];
1708 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1709 return darwin_sections[objc_selector_fixup_section];
1710 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1711 return darwin_sections[objc_symbols_section];
1712 else if (!strncmp (name, "_OBJC_MODULES", 13))
1713 return darwin_sections[objc_module_info_section];
1714 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1715 return darwin_sections[objc_image_info_section];
1716 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1717 return darwin_sections[objc_cat_inst_meth_section];
1718 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1719 return darwin_sections[objc_cat_cls_meth_section];
1720 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1721 return darwin_sections[objc_cat_cls_meth_section];
1722 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1723 return darwin_sections[objc_protocol_section];
1724 else
1725 return base_section;
1728 return base_section;
1731 /* This can be called with address expressions as "rtx".
1732 They must go in "const". */
1734 section *
1735 machopic_select_rtx_section (enum machine_mode mode, rtx x,
1736 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1738 if (GET_MODE_SIZE (mode) == 8
1739 && (GET_CODE (x) == CONST_INT
1740 || GET_CODE (x) == CONST_WIDE_INT
1741 || GET_CODE (x) == CONST_DOUBLE))
1742 return darwin_sections[literal8_section];
1743 else if (GET_MODE_SIZE (mode) == 4
1744 && (GET_CODE (x) == CONST_INT
1745 || GET_CODE (x) == CONST_WIDE_INT
1746 || GET_CODE (x) == CONST_DOUBLE))
1747 return darwin_sections[literal4_section];
1748 else if (HAVE_GAS_LITERAL16
1749 && TARGET_64BIT
1750 && GET_MODE_SIZE (mode) == 16
1751 && (GET_CODE (x) == CONST_INT
1752 || GET_CODE (x) == CONST_WIDE_INT
1753 || GET_CODE (x) == CONST_DOUBLE
1754 || GET_CODE (x) == CONST_VECTOR))
1755 return darwin_sections[literal16_section];
1756 else if (MACHOPIC_INDIRECT
1757 && (GET_CODE (x) == SYMBOL_REF
1758 || GET_CODE (x) == CONST
1759 || GET_CODE (x) == LABEL_REF))
1760 return darwin_sections[const_data_section];
1761 else
1762 return darwin_sections[const_section];
1765 void
1766 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1768 cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
1770 vec_safe_push (ctors, new_elt);
1772 if (! MACHOPIC_INDIRECT)
1773 fprintf (asm_out_file, ".reference .constructors_used\n");
1776 void
1777 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1779 cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)};
1781 vec_safe_push (dtors, new_elt);
1783 if (! MACHOPIC_INDIRECT)
1784 fprintf (asm_out_file, ".reference .destructors_used\n");
1787 static int
1788 sort_cdtor_records (const void * a, const void * b)
1790 const cdtor_record *cda = (const cdtor_record *)a;
1791 const cdtor_record *cdb = (const cdtor_record *)b;
1792 if (cda->priority > cdb->priority)
1793 return 1;
1794 if (cda->priority < cdb->priority)
1795 return -1;
1796 if (cda->position > cdb->position)
1797 return 1;
1798 if (cda->position < cdb->position)
1799 return -1;
1800 return 0;
1803 static void
1804 finalize_ctors ()
1806 unsigned int i;
1807 cdtor_record *elt;
1809 if (MACHOPIC_INDIRECT)
1810 switch_to_section (darwin_sections[mod_init_section]);
1811 else
1812 switch_to_section (darwin_sections[constructor_section]);
1814 if (vec_safe_length (ctors) > 1)
1815 ctors->qsort (sort_cdtor_records);
1816 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
1818 assemble_align (POINTER_SIZE);
1819 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1823 static void
1824 finalize_dtors ()
1826 unsigned int i;
1827 cdtor_record *elt;
1829 if (MACHOPIC_INDIRECT)
1830 switch_to_section (darwin_sections[mod_term_section]);
1831 else
1832 switch_to_section (darwin_sections[destructor_section]);
1834 if (vec_safe_length (dtors) > 1)
1835 dtors->qsort (sort_cdtor_records);
1836 FOR_EACH_VEC_SAFE_ELT (dtors, i, elt)
1838 assemble_align (POINTER_SIZE);
1839 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1843 void
1844 darwin_globalize_label (FILE *stream, const char *name)
1846 if (!!strncmp (name, "_OBJC_", 6))
1847 default_globalize_label (stream, name);
1850 /* This routine returns non-zero if 'name' starts with the special objective-c
1851 anonymous file-scope static name. It accommodates c++'s mangling of such
1852 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1854 int
1855 darwin_label_is_anonymous_local_objc_name (const char *name)
1857 const unsigned char *p = (const unsigned char *) name;
1858 if (*p != '_')
1859 return 0;
1860 if (p[1] == 'Z' && p[2] == 'L')
1862 p += 3;
1863 while (*p >= '0' && *p <= '9')
1864 p++;
1866 return (!strncmp ((const char *)p, "_OBJC_", 6));
1869 /* LTO support for Mach-O.
1871 This version uses three mach-o sections to encapsulate the (unlimited
1872 number of) lto sections.
1874 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1875 __GNU_LTO, __section_names contains the GNU LTO section names.
1876 __GNU_LTO, __section_index contains an array of values that index these.
1878 Indexed thus:
1879 <section offset from the start of __GNU_LTO, __lto_sections>,
1880 <section length>
1881 <name offset from the start of __GNU_LTO, __section_names,
1882 <name length>.
1884 At present, for both m32 and m64 mach-o files each of these fields is
1885 represented by a uint32_t. This is because, AFAICT, a mach-o object
1886 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1888 uint32_t offset;
1889 "offset An integer specifying the offset to this section in the file." */
1891 /* Count lto section numbers. */
1892 static unsigned int lto_section_num = 0;
1894 /* A vector of information about LTO sections, at present, we only have
1895 the name. TODO: see if we can get the data length somehow. */
1896 typedef struct GTY (()) darwin_lto_section_e {
1897 const char *sectname;
1898 } darwin_lto_section_e ;
1900 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1902 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1903 sections into three Mach-O ones).
1904 NOTE: These names MUST be kept in sync with those in
1905 libiberty/simple-object-mach-o. */
1906 #define LTO_SECTS_SECTION "__wrapper_sects"
1907 #define LTO_NAMES_SECTION "__wrapper_names"
1908 #define LTO_INDEX_SECTION "__wrapper_index"
1910 /* File to temporarily store LTO data. This is appended to asm_out_file
1911 in darwin_end_file. */
1912 static FILE *lto_asm_out_file, *saved_asm_out_file;
1913 static char *lto_asm_out_name;
1915 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1916 asm_out_file and switching to an alternative output file. */
1917 void
1918 darwin_asm_lto_start (void)
1920 gcc_assert (! saved_asm_out_file);
1921 saved_asm_out_file = asm_out_file;
1922 if (! lto_asm_out_name)
1923 lto_asm_out_name = make_temp_file (".lto.s");
1924 lto_asm_out_file = fopen (lto_asm_out_name, "a");
1925 if (lto_asm_out_file == NULL)
1926 fatal_error ("failed to open temporary file %s for LTO output",
1927 lto_asm_out_name);
1928 asm_out_file = lto_asm_out_file;
1931 /* Restore asm_out_file. */
1932 void
1933 darwin_asm_lto_end (void)
1935 gcc_assert (saved_asm_out_file);
1936 fclose (lto_asm_out_file);
1937 asm_out_file = saved_asm_out_file;
1938 saved_asm_out_file = NULL;
1941 static void
1942 darwin_asm_dwarf_section (const char *name, unsigned int flags, tree decl);
1944 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
1946 void
1947 darwin_asm_named_section (const char *name,
1948 unsigned int flags,
1949 tree decl ATTRIBUTE_UNUSED)
1951 /* LTO sections go in a special section that encapsulates the (unlimited)
1952 number of GNU LTO sections within a single mach-o one. */
1953 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1954 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1956 darwin_lto_section_e e;
1957 /* We expect certain flags to be set... */
1958 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1959 == (SECTION_DEBUG | SECTION_NAMED));
1961 /* Switch to our combined section. */
1962 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1963 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1964 /* Output a label for the start of this sub-section. */
1965 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1966 lto_section_num, name);
1967 /* We have to jump through hoops to get the values of the intra-section
1968 offsets... */
1969 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1970 lto_section_num, lto_section_num);
1971 fprintf (asm_out_file,
1972 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1973 lto_section_num, lto_section_num+1, lto_section_num);
1974 lto_section_num++;
1975 e.sectname = xstrdup (name);
1976 /* Keep the names, we'll need to make a table later.
1977 TODO: check that we do not revisit sections, that would break
1978 the assumption of how this is done. */
1979 if (lto_section_names == NULL)
1980 vec_alloc (lto_section_names, 16);
1981 vec_safe_push (lto_section_names, e);
1983 else if (strncmp (name, "__DWARF,", 8) == 0)
1984 darwin_asm_dwarf_section (name, flags, decl);
1985 else
1986 fprintf (asm_out_file, "\t.section %s\n", name);
1989 void
1990 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
1992 /* Darwin does not use unique sections. */
1995 /* Handle __attribute__ ((apple_kext_compatibility)).
1996 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
1997 vtable for classes with this attribute (and their descendants) by not
1998 outputting the new 3.0 nondeleting destructor. This means that such
1999 objects CANNOT be allocated on the stack or as globals UNLESS they have
2000 a completely empty `operator delete'.
2001 Luckily, this fits in with the Darwin kext model.
2003 This attribute also disables gcc3's potential overlaying of derived
2004 class data members on the padding at the end of the base class. */
2006 tree
2007 darwin_handle_kext_attribute (tree *node, tree name,
2008 tree args ATTRIBUTE_UNUSED,
2009 int flags ATTRIBUTE_UNUSED,
2010 bool *no_add_attrs)
2012 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
2013 if (! TARGET_KEXTABI)
2015 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2016 "only when compiling a kext", name);
2018 *no_add_attrs = true;
2020 else if (TREE_CODE (*node) != RECORD_TYPE)
2022 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2023 "only to C++ classes", name);
2025 *no_add_attrs = true;
2028 return NULL_TREE;
2031 /* Handle a "weak_import" attribute; arguments as in
2032 struct attribute_spec.handler. */
2034 tree
2035 darwin_handle_weak_import_attribute (tree *node, tree name,
2036 tree ARG_UNUSED (args),
2037 int ARG_UNUSED (flags),
2038 bool * no_add_attrs)
2040 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
2042 warning (OPT_Wattributes, "%qE attribute ignored",
2043 name);
2044 *no_add_attrs = true;
2046 else
2047 declare_weak (*node);
2049 return NULL_TREE;
2052 /* Emit a label for an FDE, making it global and/or weak if appropriate.
2053 The third parameter is nonzero if this is for exception handling.
2054 The fourth parameter is nonzero if this is just a placeholder for an
2055 FDE that we are omitting. */
2057 void
2058 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
2060 char *lab ;
2061 char buf[32];
2062 static int invok_count = 0;
2063 static tree last_fun_decl = NULL_TREE;
2065 /* We use the linker to emit the .eh labels for Darwin 9 and above. */
2066 if (! for_eh || generating_for_darwin_version >= 9)
2067 return;
2069 /* FIXME: This only works when the eh for all sections of a function is
2070 emitted at the same time. If that changes, we would need to use a lookup
2071 table of some form to determine what to do. Also, we should emit the
2072 unadorned label for the partition containing the public label for a
2073 function. This is of limited use, probably, since we do not currently
2074 enable partitioning. */
2075 strcpy (buf, ".eh");
2076 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
2078 if (decl == last_fun_decl)
2080 invok_count++;
2081 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
2083 else
2085 last_fun_decl = decl;
2086 invok_count = 0;
2090 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
2092 if (TREE_PUBLIC (decl))
2094 targetm.asm_out.globalize_label (file, lab);
2095 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
2097 fputs ("\t.private_extern ", file);
2098 assemble_name (file, lab);
2099 fputc ('\n', file);
2103 if (DECL_WEAK (decl))
2105 fputs ("\t.weak_definition ", file);
2106 assemble_name (file, lab);
2107 fputc ('\n', file);
2110 assemble_name (file, lab);
2111 if (empty)
2113 fputs (" = 0\n", file);
2115 /* Mark the absolute .eh and .eh1 style labels as needed to
2116 ensure that we don't dead code strip them and keep such
2117 labels from another instantiation point until we can fix this
2118 properly with group comdat support. */
2119 darwin_mark_decl_preserved (lab);
2121 else
2122 fputs (":\n", file);
2124 free (lab);
2127 static GTY(()) unsigned long except_table_label_num;
2129 void
2130 darwin_emit_except_table_label (FILE *file)
2132 char section_start_label[30];
2134 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2135 except_table_label_num++);
2136 ASM_OUTPUT_LABEL (file, section_start_label);
2138 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2140 void
2141 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2143 const char *nlp_name;
2145 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2147 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2148 fputs ("\t.long\t", file);
2149 ASM_OUTPUT_LABELREF (file, nlp_name);
2150 fputs ("-.", file);
2153 /* If this is uncommented, details of each allocation will be printed
2154 in the asm right before the actual code. WARNING - this will cause some
2155 test-suite fails (since the printout will contain items that some tests
2156 are not expecting) -- so don't leave it on by default (it bloats the
2157 asm too). */
2158 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2160 /* The first two of these routines are ostensibly just intended to put
2161 names into the asm. However, they are both hijacked in order to ensure
2162 that zero-sized items do not make their way into the output. Consequently,
2163 we also need to make these participate in provisions for dealing with
2164 such items in section anchors. */
2166 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2167 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2168 but it does need to be referenced via indirect PIC data pointers.
2169 The machopic_define_symbol calls are telling the machopic subsystem
2170 that the name *is* defined in this module, so it doesn't need to
2171 make them indirect. */
2172 void
2173 darwin_asm_declare_object_name (FILE *file,
2174 const char *nam, tree decl)
2176 const char *xname = nam;
2177 unsigned HOST_WIDE_INT size;
2178 bool local_def, weak;
2180 weak = (DECL_P (decl)
2181 && DECL_WEAK (decl)
2182 && !lookup_attribute ("weak_import",
2183 DECL_ATTRIBUTES (decl)));
2185 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2186 && (!DECL_COMMON (decl)
2187 || !TREE_PUBLIC (decl)));
2189 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2190 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2192 if (local_def)
2194 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2195 if (!weak)
2196 machopic_define_symbol (DECL_RTL (decl));
2199 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2201 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2202 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2203 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2204 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2205 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2206 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2207 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2208 (unsigned long)DECL_INITIAL (decl));
2209 #endif
2211 /* Darwin needs help to support local zero-sized objects.
2212 They must be made at least one byte, and the section containing must be
2213 marked as unsuitable for section-anchors (see storage allocators below).
2215 For non-zero objects this output is handled by varasm.c.
2217 if (!size)
2219 unsigned int l2align = 0;
2221 /* The align must be honored, even for zero-sized. */
2222 if (DECL_ALIGN (decl))
2224 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2225 fprintf (file, "\t.align\t%u\n", l2align);
2228 ASM_OUTPUT_LABEL (file, xname);
2229 size = 1;
2230 fprintf (file, "\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2232 /* Check that we've correctly picked up the zero-sized item and placed it
2233 properly. */
2234 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2235 || (in_section
2236 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2238 else
2239 ASM_OUTPUT_LABEL (file, xname);
2242 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2243 void
2244 darwin_asm_declare_constant_name (FILE *file, const char *name,
2245 const_tree exp ATTRIBUTE_UNUSED,
2246 HOST_WIDE_INT size)
2248 assemble_label (file, name);
2249 /* As for other items, we need at least one byte. */
2250 if (!size)
2252 fputs ("\t.space\t1\n", file);
2253 /* Check that we've correctly picked up the zero-sized item and placed it
2254 properly. */
2255 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2256 || (in_section
2257 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2261 /* Darwin storage allocators.
2263 Zerofill sections are desirable for large blank data since, otherwise, these
2264 data bloat objects (PR33210).
2266 However, section anchors don't work in .zerofill sections (one cannot switch
2267 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2268 to put (at least some) data into 'normal' switchable sections.
2270 Here we set a relatively arbitrary value for the size of an object to trigger
2271 zerofill when section anchors are enabled (anything bigger than a page for
2272 current Darwin implementations). FIXME: there ought to be some objective way
2273 to make this choice.
2275 When section anchor are off this is ignored anyway. */
2277 #define BYTES_ZFILL 4096
2279 /* Emit a chunk of data for items coalesced by the linker. */
2280 static void
2281 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2282 unsigned HOST_WIDE_INT size,
2283 unsigned int align)
2285 /* Since the sections used here are coalesed, they will not be eligible
2286 for section anchors, and therefore we don't need to break that out. */
2287 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2288 switch_to_section (darwin_sections[const_data_coal_section]);
2289 else
2290 switch_to_section (darwin_sections[data_coal_section]);
2292 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2293 the align info for zero-sized items... but do it here otherwise. */
2294 if (size && align)
2295 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2297 if (TREE_PUBLIC (decl))
2298 darwin_globalize_label (fp, name);
2300 /* ... and we let it deal with outputting one byte of zero for them too. */
2301 darwin_asm_declare_object_name (fp, name, decl);
2302 if (size)
2303 assemble_zeros (size);
2306 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2307 static void
2308 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2309 unsigned HOST_WIDE_INT size,
2310 unsigned int align, tree meta)
2312 section *ocs = data_section;
2314 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2315 ocs = darwin_objc2_section (decl, meta, ocs);
2316 else
2317 ocs = darwin_objc1_section (decl, meta, ocs);
2319 switch_to_section (ocs);
2321 /* We shall declare that zero-sized meta-data are not valid (yet). */
2322 gcc_assert (size);
2323 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2325 /* ... and we let it deal with outputting one byte of zero for them too. */
2326 darwin_asm_declare_object_name (fp, name, decl);
2327 assemble_zeros (size);
2330 /* This routine emits 'local' storage:
2332 When Section Anchors are off this routine emits .zerofill commands in
2333 sections named for their alignment.
2335 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2336 the .static_data section so that the section anchoring system can see them.
2337 Larger items are still placed in .zerofill sections, addressing PR33210.
2338 The routine has no checking - it is all assumed to be done by the caller.
2340 static void
2341 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2342 unsigned HOST_WIDE_INT size,
2343 unsigned int l2align)
2345 /* FIXME: We have a fudge to make this work with Java even when the target does
2346 not use sections anchors -- Java seems to need at least one small item in a
2347 non-zerofill segment. */
2348 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2349 || (size && size <= 2))
2351 /* Put smaller objects in _static_data, where the section anchors system
2352 can get them.
2353 However, if they are zero-sized punt them to yet a different section
2354 (that is not allowed to participate in anchoring). */
2355 if (!size)
2357 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2358 in_section = darwin_sections[zobj_bss_section];
2359 size = 1;
2361 else
2363 fputs ("\t.static_data\n", fp);
2364 in_section = darwin_sections[static_data_section];
2367 if (l2align)
2368 fprintf (fp, "\t.align\t%u\n", l2align);
2370 assemble_name (fp, name);
2371 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2373 else
2375 /* When we are on a non-section anchor target, we can get zero-sized
2376 items here. However, all we need to do is to bump them to one byte
2377 and the section alignment will take care of the rest. */
2378 char secnam[64];
2379 unsigned int flags ;
2380 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
2381 (unsigned) l2align);
2382 /* We can't anchor (yet, if ever) in zerofill sections, because we can't
2383 switch to them and emit a label. */
2384 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2385 in_section = get_section (secnam, flags, NULL);
2386 fprintf (fp, "\t.zerofill %s,", secnam);
2387 assemble_name (fp, name);
2388 if (!size)
2389 size = 1;
2391 if (l2align)
2392 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2393 size, (unsigned) l2align);
2394 else
2395 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2398 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2399 /* This is defined as a file-scope var, so we know to notify machopic. */
2400 machopic_define_symbol (DECL_RTL (decl));
2403 /* Emit a chunk of common. */
2404 static void
2405 darwin_emit_common (FILE *fp, const char *name,
2406 unsigned HOST_WIDE_INT size, unsigned int align)
2408 unsigned HOST_WIDE_INT rounded;
2409 unsigned int l2align;
2411 /* Earlier systems complain if the alignment exceeds the page size.
2412 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2413 if (!emit_aligned_common && (align > 32768UL))
2414 align = 4096UL; /* In units. */
2415 else
2416 align /= BITS_PER_UNIT;
2418 /* Make sure we have a meaningful align. */
2419 if (!align)
2420 align = 1;
2422 /* For earlier toolchains, we need to emit the var as a rounded size to
2423 tell ld the alignment. */
2424 if (size < align)
2425 rounded = align;
2426 else
2427 rounded = (size + (align-1)) & ~(align-1);
2429 l2align = floor_log2 (align);
2430 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2432 in_section = comm_section;
2433 /* We mustn't allow multiple public symbols to share an address when using
2434 the normal OSX toolchain. */
2435 if (!size)
2437 /* Put at least one byte. */
2438 size = 1;
2439 /* This section can no longer participate in section anchoring. */
2440 comm_section->common.flags |= SECTION_NO_ANCHOR;
2443 fputs ("\t.comm\t", fp);
2444 assemble_name (fp, name);
2445 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2446 emit_aligned_common?size:rounded);
2447 if (l2align && emit_aligned_common)
2448 fprintf (fp, ",%u", l2align);
2449 fputs ("\n", fp);
2452 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2453 or coalescable data sections (for weak or comdat) as appropriate. */
2455 void
2456 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2457 unsigned HOST_WIDE_INT size, unsigned int align)
2459 unsigned int l2align;
2460 bool one, pub, weak;
2461 tree meta;
2463 pub = TREE_PUBLIC (decl);
2464 one = DECL_ONE_ONLY (decl);
2465 weak = (DECL_P (decl)
2466 && DECL_WEAK (decl)
2467 && !lookup_attribute ("weak_import",
2468 DECL_ATTRIBUTES (decl)));
2470 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2471 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2472 " pub %d weak %d one %d init %lx\n",
2473 name, (long long)size, (int)align, TREE_READONLY (decl),
2474 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2475 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2476 #endif
2478 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2479 before the target has a chance to comment. */
2480 if ((meta = is_objc_metadata (decl)))
2482 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2483 return;
2486 /* Check that any initializer is valid. */
2487 gcc_assert ((DECL_INITIAL (decl) == NULL)
2488 || (DECL_INITIAL (decl) == error_mark_node)
2489 || initializer_zerop (DECL_INITIAL (decl)));
2491 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2492 gcc_assert (!DECL_COMMON (decl));
2494 /* Pick up the correct alignment. */
2495 if (!size || !align)
2496 align = DECL_ALIGN (decl);
2498 l2align = floor_log2 (align / BITS_PER_UNIT);
2499 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2501 last_assemble_variable_decl = decl;
2503 /* We would rather not have to check this here - but it seems that we might
2504 be passed a decl that should be in coalesced space. */
2505 if (one || weak)
2507 /* Weak or COMDAT objects are put in mergeable sections. */
2508 darwin_emit_weak_or_comdat (fp, decl, name, size,
2509 DECL_ALIGN (decl));
2510 return;
2513 /* If this is not public, then emit according to local rules. */
2514 if (!pub)
2516 darwin_emit_local_bss (fp, decl, name, size, l2align);
2517 return;
2520 /* So we have a public symbol (small item fudge for Java, see above). */
2521 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2522 || (size && size <= 2))
2524 /* Put smaller objects in data, where the section anchors system can get
2525 them. However, if they are zero-sized punt them to yet a different
2526 section (that is not allowed to participate in anchoring). */
2527 if (!size)
2529 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2530 in_section = darwin_sections[zobj_data_section];
2531 size = 1;
2533 else
2535 fputs ("\t.data\n", fp);
2536 in_section = data_section;
2539 if (l2align)
2540 fprintf (fp, "\t.align\t%u\n", l2align);
2542 assemble_name (fp, name);
2543 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2545 else
2547 char secnam[64];
2548 unsigned int flags ;
2549 /* When we are on a non-section anchor target, we can get zero-sized
2550 items here. However, all we need to do is to bump them to one byte
2551 and the section alignment will take care of the rest. */
2552 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
2554 /* We can't anchor in zerofill sections, because we can't switch
2555 to them and emit a label. */
2556 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2557 in_section = get_section (secnam, flags, NULL);
2558 fprintf (fp, "\t.zerofill %s,", secnam);
2559 assemble_name (fp, name);
2560 if (!size)
2561 size = 1;
2563 if (l2align)
2564 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2565 else
2566 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2568 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2571 /* Output a chunk of common, with alignment specified (where the target
2572 supports this). */
2573 void
2574 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2575 unsigned HOST_WIDE_INT size,
2576 unsigned int align)
2578 unsigned int l2align;
2579 bool one, weak;
2580 tree meta;
2582 /* No corresponding var. */
2583 if (decl==NULL)
2585 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2586 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2587 #endif
2588 darwin_emit_common (fp, name, size, align);
2589 return;
2592 one = DECL_ONE_ONLY (decl);
2593 weak = (DECL_P (decl)
2594 && DECL_WEAK (decl)
2595 && !lookup_attribute ("weak_import",
2596 DECL_ATTRIBUTES (decl)));
2598 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2599 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2600 " weak %d one %d init %lx\n",
2601 name, (long long)size, (int)align, TREE_READONLY (decl),
2602 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2603 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2604 #endif
2606 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2607 before the target has a chance to comment. */
2608 if ((meta = is_objc_metadata (decl)))
2610 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2611 return;
2614 /* We shouldn't be messing with this if the decl has a section name. */
2615 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2617 /* We would rather not have to check this here - but it seems that we might
2618 be passed a decl that should be in coalesced space. */
2619 if (one || weak)
2621 /* Weak or COMDAT objects are put in mergable sections. */
2622 darwin_emit_weak_or_comdat (fp, decl, name, size,
2623 DECL_ALIGN (decl));
2624 return;
2627 /* We should only get here for DECL_COMMON, with a zero init (and, in
2628 principle, only for public symbols too - although we deal with local
2629 ones below). */
2631 /* Check the initializer is OK. */
2632 gcc_assert (DECL_COMMON (decl)
2633 && ((DECL_INITIAL (decl) == NULL)
2634 || (DECL_INITIAL (decl) == error_mark_node)
2635 || initializer_zerop (DECL_INITIAL (decl))));
2637 last_assemble_variable_decl = decl;
2639 if (!size || !align)
2640 align = DECL_ALIGN (decl);
2642 l2align = floor_log2 (align / BITS_PER_UNIT);
2643 /* Check we aren't asking for more aligment than the platform allows. */
2644 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2646 if (TREE_PUBLIC (decl) != 0)
2647 darwin_emit_common (fp, name, size, align);
2648 else
2649 darwin_emit_local_bss (fp, decl, name, size, l2align);
2652 /* Output a chunk of BSS with alignment specfied. */
2653 void
2654 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2655 unsigned HOST_WIDE_INT size,
2656 unsigned int align)
2658 unsigned long l2align;
2659 bool one, weak;
2660 tree meta;
2662 one = DECL_ONE_ONLY (decl);
2663 weak = (DECL_P (decl)
2664 && DECL_WEAK (decl)
2665 && !lookup_attribute ("weak_import",
2666 DECL_ATTRIBUTES (decl)));
2668 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2669 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2670 " weak %d init %lx\n",
2671 name, (long long)size, (int)align, TREE_READONLY (decl),
2672 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2673 weak , (unsigned long)DECL_INITIAL (decl));
2674 #endif
2676 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2677 before the target has a chance to comment. */
2678 if ((meta = is_objc_metadata (decl)))
2680 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2681 return;
2684 /* We shouldn't be messing with this if the decl has a section name. */
2685 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2687 /* We would rather not have to check this here - but it seems that we might
2688 be passed a decl that should be in coalesced space. */
2689 if (one || weak)
2691 /* Weak or COMDAT objects are put in mergable sections. */
2692 darwin_emit_weak_or_comdat (fp, decl, name, size,
2693 DECL_ALIGN (decl));
2694 return;
2697 /* .. and it should be suitable for placement in local mem. */
2698 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2699 /* .. and any initializer must be all-zero. */
2700 gcc_assert ((DECL_INITIAL (decl) == NULL)
2701 || (DECL_INITIAL (decl) == error_mark_node)
2702 || initializer_zerop (DECL_INITIAL (decl)));
2704 last_assemble_variable_decl = decl;
2706 if (!size || !align)
2707 align = DECL_ALIGN (decl);
2709 l2align = floor_log2 (align / BITS_PER_UNIT);
2710 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2712 darwin_emit_local_bss (fp, decl, name, size, l2align);
2715 /* Emit an assembler directive to set visibility for a symbol. The
2716 only supported visibilities are VISIBILITY_DEFAULT and
2717 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2718 extern". There is no MACH-O equivalent of ELF's
2719 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2721 void
2722 darwin_assemble_visibility (tree decl, int vis)
2724 if (vis == VISIBILITY_DEFAULT)
2726 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2728 fputs ("\t.private_extern ", asm_out_file);
2729 assemble_name (asm_out_file,
2730 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2731 fputs ("\n", asm_out_file);
2733 else
2734 warning (OPT_Wattributes, "protected visibility attribute "
2735 "not supported in this configuration; ignored");
2738 /* vec used by darwin_asm_dwarf_section.
2739 Maybe a hash tab would be better here - but the intention is that this is
2740 a very short list (fewer than 16 items) and each entry should (ideally,
2741 eventually) only be presented once.
2743 A structure to hold a dwarf debug section used entry. */
2745 typedef struct GTY(()) dwarf_sect_used_entry {
2746 const char *name;
2747 unsigned count;
2749 dwarf_sect_used_entry;
2752 /* A list of used __DWARF sections. */
2753 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2755 /* This is called when we are asked to assemble a named section and the
2756 name begins with __DWARF,. We keep a list of the section names (without
2757 the __DWARF, prefix) and use this to emit our required start label on the
2758 first switch to each section. */
2760 static void
2761 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2762 tree ARG_UNUSED (decl))
2764 unsigned i;
2765 int namelen;
2766 const char * sname;
2767 dwarf_sect_used_entry *ref;
2768 bool found = false;
2769 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2770 == (SECTION_DEBUG | SECTION_NAMED));
2771 /* We know that the name starts with __DWARF, */
2772 sname = name + 8;
2773 namelen = strchr (sname, ',') - sname;
2774 gcc_assert (namelen);
2775 if (dwarf_sect_names_table == NULL)
2776 vec_alloc (dwarf_sect_names_table, 16);
2777 else
2778 for (i = 0;
2779 dwarf_sect_names_table->iterate (i, &ref);
2780 i++)
2782 if (!ref)
2783 break;
2784 if (!strcmp (ref->name, sname))
2786 found = true;
2787 ref->count++;
2788 break;
2792 fprintf (asm_out_file, "\t.section %s\n", name);
2793 if (!found)
2795 dwarf_sect_used_entry e;
2796 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2797 e.count = 1;
2798 e.name = xstrdup (sname);
2799 vec_safe_push (dwarf_sect_names_table, e);
2803 /* Output a difference of two labels that will be an assembly time
2804 constant if the two labels are local. (.long lab1-lab2 will be
2805 very different if lab1 is at the boundary between two sections; it
2806 will be relocated according to the second section, not the first,
2807 so one ends up with a difference between labels in different
2808 sections, which is bad in the dwarf2 eh context for instance.) */
2810 static int darwin_dwarf_label_counter;
2812 void
2813 darwin_asm_output_dwarf_delta (FILE *file, int size,
2814 const char *lab1, const char *lab2)
2816 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2817 && lab2[0] == '*' && lab2[1] == 'L');
2818 const char *directive = (size == 8 ? ".quad" : ".long");
2820 if (islocaldiff)
2821 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2822 else
2823 fprintf (file, "\t%s\t", directive);
2825 assemble_name_raw (file, lab1);
2826 fprintf (file, "-");
2827 assemble_name_raw (file, lab2);
2828 if (islocaldiff)
2829 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2832 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2833 offsets are not represented using relocs in .o files; either the
2834 section never leaves the .o file, or the linker or other tool is
2835 responsible for parsing the DWARF and updating the offsets. */
2837 void
2838 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2839 section *base)
2841 char sname[64];
2842 int namelen;
2844 gcc_assert (base->common.flags & SECTION_NAMED);
2845 gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
2846 gcc_assert (strchr (base->named.name + 8, ','));
2848 namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
2849 sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
2850 darwin_asm_output_dwarf_delta (file, size, lab, sname);
2853 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2855 void
2856 darwin_file_start (void)
2858 /* Nothing to do. */
2861 /* Called for the TARGET_ASM_FILE_END hook.
2862 Emit the mach-o pic indirection data, the lto data and, finally a flag
2863 to tell the linker that it can break the file object into sections and
2864 move those around for efficiency. */
2866 void
2867 darwin_file_end (void)
2869 if (!vec_safe_is_empty (ctors))
2870 finalize_ctors ();
2871 if (!vec_safe_is_empty (dtors))
2872 finalize_dtors ();
2874 /* If we are expecting to output NeXT ObjC meta-data, (and we actually see
2875 some) then we output the fix-and-continue marker (Image Info).
2876 This applies to Objective C, Objective C++ and LTO with either language
2877 as part of the input. */
2878 if (flag_next_runtime && objc_metadata_seen)
2880 unsigned int flags = 0;
2881 if (flag_objc_abi >= 2)
2883 flags = 16;
2884 output_section_asm_op
2885 (darwin_sections[objc2_image_info_section]->unnamed.data);
2887 else
2888 output_section_asm_op
2889 (darwin_sections[objc_image_info_section]->unnamed.data);
2891 ASM_OUTPUT_ALIGN (asm_out_file, 2);
2892 fputs ("L_OBJC_ImageInfo:\n", asm_out_file);
2894 flags |= (flag_replace_objc_classes && classes_seen) ? 1 : 0;
2895 flags |= flag_objc_gc ? 2 : 0;
2897 fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags);
2900 machopic_finish (asm_out_file);
2901 if (strcmp (lang_hooks.name, "GNU C++") == 0)
2903 switch_to_section (darwin_sections[constructor_section]);
2904 switch_to_section (darwin_sections[destructor_section]);
2905 ASM_OUTPUT_ALIGN (asm_out_file, 1);
2908 /* If there was LTO assembler output, append it to asm_out_file. */
2909 if (lto_asm_out_name)
2911 int n;
2912 char *buf, *lto_asm_txt;
2914 /* Shouldn't be here if we failed to switch back. */
2915 gcc_assert (! saved_asm_out_file);
2917 lto_asm_out_file = fopen (lto_asm_out_name, "r");
2918 if (lto_asm_out_file == NULL)
2919 fatal_error ("failed to open temporary file %s with LTO output",
2920 lto_asm_out_name);
2921 fseek (lto_asm_out_file, 0, SEEK_END);
2922 n = ftell (lto_asm_out_file);
2923 if (n > 0)
2925 fseek (lto_asm_out_file, 0, SEEK_SET);
2926 lto_asm_txt = buf = (char *) xmalloc (n + 1);
2927 while (fgets (lto_asm_txt, n, lto_asm_out_file))
2928 fputs (lto_asm_txt, asm_out_file);
2929 /* Put a termination label. */
2930 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2931 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2932 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2933 lto_section_num);
2934 /* Make sure our termination label stays in this section. */
2935 fputs ("\t.space\t1\n", asm_out_file);
2938 /* Remove the temporary file. */
2939 fclose (lto_asm_out_file);
2940 unlink_if_ordinary (lto_asm_out_name);
2941 free (lto_asm_out_name);
2944 /* Output the names and indices. */
2945 if (lto_section_names && lto_section_names->length ())
2947 int count;
2948 darwin_lto_section_e *ref;
2949 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
2950 the latter up ourselves. */
2951 const char *op = integer_asm_op (4,0);
2953 /* Emit the names. */
2954 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2955 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2956 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2958 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
2959 /* We have to jump through hoops to get the values of the intra-section
2960 offsets... */
2961 fprintf (asm_out_file,
2962 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
2963 count, count);
2964 fprintf (asm_out_file,
2965 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
2966 count, count+1, count);
2967 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
2969 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
2970 /* make sure our termination label stays in this section. */
2971 fputs ("\t.space\t1\n", asm_out_file);
2973 /* Emit the Index. */
2974 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2975 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
2976 fputs ("\t.align\t2\n", asm_out_file);
2977 fputs ("# Section offset, Section length, Name offset, Name length\n",
2978 asm_out_file);
2979 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2981 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
2982 op, count, ref->sectname);
2983 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
2984 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
2985 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
2989 /* If we have section anchors, then we must prevent the linker from
2990 re-arranging data. */
2991 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2992 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
2995 /* TODO: Add a language hook for identifying if a decl is a vtable. */
2996 #define DARWIN_VTABLE_P(DECL) 0
2998 /* Cross-module name binding. Darwin does not support overriding
2999 functions at dynamic-link time, except for vtables in kexts. */
3001 bool
3002 darwin_binds_local_p (const_tree decl)
3004 return default_binds_local_p_1 (decl,
3005 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
3008 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
3009 anchor relative to ".", the current section position. We cannot use
3010 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
3011 void
3012 darwin_asm_output_anchor (rtx symbol)
3014 fprintf (asm_out_file, "\t.set\t");
3015 assemble_name (asm_out_file, XSTR (symbol, 0));
3016 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
3017 SYMBOL_REF_BLOCK_OFFSET (symbol));
3020 /* Disable section anchoring on any section containing a zero-sized
3021 object. */
3022 bool
3023 darwin_use_anchors_for_symbol_p (const_rtx symbol)
3025 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
3027 section *sect;
3028 /* If the section contains a zero-sized object it's ineligible. */
3029 sect = SYMBOL_REF_BLOCK (symbol)->sect;
3030 /* This should have the effect of disabling anchors for vars that follow
3031 any zero-sized one, in a given section. */
3032 if (sect->common.flags & SECTION_NO_ANCHOR)
3033 return false;
3035 /* Also check the normal reasons for suppressing. */
3036 return default_use_anchors_for_symbol_p (symbol);
3038 else
3039 return false;
3042 /* Set the darwin specific attributes on TYPE. */
3043 void
3044 darwin_set_default_type_attributes (tree type)
3046 if (darwin_ms_struct
3047 && TREE_CODE (type) == RECORD_TYPE)
3048 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
3049 NULL_TREE,
3050 TYPE_ATTRIBUTES (type));
3053 /* True, iff we're generating code for loadable kernel extensions. */
3055 bool
3056 darwin_kextabi_p (void) {
3057 return flag_apple_kext;
3060 void
3061 darwin_override_options (void)
3063 /* Keep track of which (major) version we're generating code for. */
3064 if (darwin_macosx_version_min)
3066 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
3067 generating_for_darwin_version = 10;
3068 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
3069 generating_for_darwin_version = 9;
3071 /* Earlier versions are not specifically accounted, until required. */
3074 /* In principle, this should be c-family only. However, we really need to
3075 set sensible defaults for LTO as well, since the section selection stuff
3076 should check for correctness re. the ABI. TODO: check and provide the
3077 flags (runtime & ABI) from the lto wrapper). */
3079 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
3080 if (!global_options_set.x_flag_objc_abi)
3081 global_options.x_flag_objc_abi
3082 = (!flag_next_runtime)
3084 : (TARGET_64BIT ? 2
3085 : (generating_for_darwin_version >= 9) ? 1
3086 : 0);
3088 /* Objective-C family ABI 2 is only valid for next/m64 at present. */
3089 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
3091 if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
3092 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
3093 " used for %<-m64%> targets with"
3094 " %<-fnext-runtime%>");
3095 if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
3096 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
3097 " supported on %<-m32%> targets with"
3098 " %<-fnext-runtime%>");
3101 /* Don't emit DWARF3/4 unless specifically selected. This is a
3102 workaround for tool bugs. */
3103 if (!global_options_set.x_dwarf_strict)
3104 dwarf_strict = 1;
3105 if (!global_options_set.x_dwarf_version)
3106 dwarf_version = 2;
3108 /* Do not allow unwind tables to be generated by default for m32.
3109 fnon-call-exceptions will override this, regardless of what we do. */
3110 if (generating_for_darwin_version < 10
3111 && !global_options_set.x_flag_asynchronous_unwind_tables
3112 && !TARGET_64BIT)
3113 global_options.x_flag_asynchronous_unwind_tables = 0;
3115 /* Disable -freorder-blocks-and-partition when unwind tables are being
3116 emitted for Darwin < 9 (OSX 10.5).
3117 The strategy is, "Unless the User has specifically set/unset an unwind
3118 flag we will switch off -freorder-blocks-and-partition when unwind tables
3119 will be generated". If the User specifically sets flags... we assume
3120 (s)he knows why... */
3121 if (generating_for_darwin_version < 9
3122 && global_options_set.x_flag_reorder_blocks_and_partition
3123 && ((global_options.x_flag_exceptions /* User, c++, java */
3124 && !global_options_set.x_flag_exceptions) /* User specified... */
3125 || (global_options.x_flag_unwind_tables
3126 && !global_options_set.x_flag_unwind_tables)
3127 || (global_options.x_flag_non_call_exceptions
3128 && !global_options_set.x_flag_non_call_exceptions)
3129 || (global_options.x_flag_asynchronous_unwind_tables
3130 && !global_options_set.x_flag_asynchronous_unwind_tables)))
3132 inform (input_location,
3133 "-freorder-blocks-and-partition does not work with exceptions "
3134 "on this architecture");
3135 flag_reorder_blocks_and_partition = 0;
3136 flag_reorder_blocks = 1;
3139 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3140 one valid choice of exception scheme for each runtime. */
3141 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3142 global_options.x_flag_objc_sjlj_exceptions =
3143 flag_next_runtime && !TARGET_64BIT;
3145 /* FIXME: and this could be eliminated then too. */
3146 if (!global_options_set.x_flag_exceptions
3147 && flag_objc_exceptions
3148 && TARGET_64BIT)
3149 flag_exceptions = 1;
3151 if (flag_mkernel || flag_apple_kext)
3153 /* -mkernel implies -fapple-kext for C++ */
3154 if (strcmp (lang_hooks.name, "GNU C++") == 0)
3155 flag_apple_kext = 1;
3157 flag_no_common = 1;
3159 /* No EH in kexts. */
3160 flag_exceptions = 0;
3161 /* No -fnon-call-exceptions data in kexts. */
3162 flag_non_call_exceptions = 0;
3163 /* so no tables either.. */
3164 flag_unwind_tables = 0;
3165 flag_asynchronous_unwind_tables = 0;
3166 /* We still need to emit branch islands for kernel context. */
3167 darwin_emit_branch_islands = true;
3170 if (flag_var_tracking_uninit == 0
3171 && generating_for_darwin_version >= 9
3172 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3173 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3174 && write_symbols == DWARF2_DEBUG)
3175 flag_var_tracking_uninit = flag_var_tracking;
3177 if (MACHO_DYNAMIC_NO_PIC_P)
3179 if (flag_pic)
3180 warning_at (UNKNOWN_LOCATION, 0,
3181 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3182 " %<-fpie%> or %<-fPIE%>");
3183 flag_pic = 0;
3185 else if (flag_pic == 1)
3187 /* Darwin's -fpic is -fPIC. */
3188 flag_pic = 2;
3191 /* It is assumed that branch island stubs are needed for earlier systems. */
3192 if (generating_for_darwin_version < 9)
3193 darwin_emit_branch_islands = true;
3194 else
3195 emit_aligned_common = true; /* Later systems can support aligned common. */
3197 /* The c_dialect...() macros are not available to us here. */
3198 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3201 #if DARWIN_PPC
3202 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3204 static void
3205 darwin_patch_builtin (enum built_in_function fncode)
3207 tree fn = builtin_decl_explicit (fncode);
3208 tree sym;
3209 char *newname;
3211 if (!fn)
3212 return;
3214 sym = DECL_ASSEMBLER_NAME (fn);
3215 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3217 set_user_assembler_name (fn, newname);
3219 fn = builtin_decl_implicit (fncode);
3220 if (fn)
3221 set_user_assembler_name (fn, newname);
3224 void
3225 darwin_patch_builtins (void)
3227 if (LONG_DOUBLE_TYPE_SIZE != 128)
3228 return;
3230 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3231 #define PATCH_BUILTIN_NO64(fncode) \
3232 if (!TARGET_64BIT) \
3233 darwin_patch_builtin (fncode);
3234 #define PATCH_BUILTIN_VARIADIC(fncode) \
3235 if (!TARGET_64BIT \
3236 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3237 darwin_patch_builtin (fncode);
3238 #include "darwin-ppc-ldouble-patch.def"
3239 #undef PATCH_BUILTIN
3240 #undef PATCH_BUILTIN_NO64
3241 #undef PATCH_BUILTIN_VARIADIC
3243 #endif
3245 /* CFStrings implementation. */
3246 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3247 static GTY(()) tree cfstring_type_node = NULL_TREE;
3248 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3249 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3250 static GTY(()) tree pcint_type_node = NULL_TREE;
3251 static GTY(()) tree pcchar_type_node = NULL_TREE;
3253 static enum built_in_function darwin_builtin_cfstring;
3255 /* Store all constructed constant CFStrings in a hash table so that
3256 they get uniqued properly. */
3258 typedef struct GTY (()) cfstring_descriptor {
3259 /* The string literal. */
3260 tree literal;
3261 /* The resulting constant CFString. */
3262 tree constructor;
3263 } cfstring_descriptor;
3265 static GTY ((param_is (struct cfstring_descriptor))) htab_t cfstring_htab;
3267 static hashval_t cfstring_hash (const void *);
3268 static int cfstring_eq (const void *, const void *);
3270 static tree
3271 add_builtin_field_decl (tree type, const char *name, tree **chain)
3273 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3274 get_identifier (name), type);
3276 if (*chain != NULL)
3277 **chain = field;
3278 *chain = &DECL_CHAIN (field);
3280 return field;
3283 tree
3284 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3286 tree cfsfun, fields, pccfstring_ftype_pcchar;
3287 tree *chain = NULL;
3289 darwin_builtin_cfstring =
3290 (enum built_in_function) builtin_cfstring;
3292 /* struct __builtin_CFString {
3293 const int *isa; (will point at
3294 int flags; __CFConstantStringClassReference)
3295 const char *str;
3296 long length;
3297 }; */
3299 pcint_type_node = build_pointer_type
3300 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3302 pcchar_type_node = build_pointer_type
3303 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3305 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3307 /* Have to build backwards for finish struct. */
3308 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3309 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3310 add_builtin_field_decl (integer_type_node, "flags", &chain);
3311 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3312 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3313 fields, NULL_TREE);
3315 /* const struct __builtin_CFstring *
3316 __builtin___CFStringMakeConstantString (const char *); */
3318 ccfstring_type_node = build_qualified_type
3319 (cfstring_type_node, TYPE_QUAL_CONST);
3320 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3321 pccfstring_ftype_pcchar = build_function_type_list
3322 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3324 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3325 get_identifier ("__builtin___CFStringMakeConstantString"),
3326 pccfstring_ftype_pcchar);
3328 TREE_PUBLIC (cfsfun) = 1;
3329 DECL_EXTERNAL (cfsfun) = 1;
3330 DECL_ARTIFICIAL (cfsfun) = 1;
3331 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3332 in place of the existing, which may be NULL. */
3333 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3334 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3335 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
3336 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
3337 lang_hooks.builtin_function (cfsfun);
3339 /* extern int __CFConstantStringClassReference[]; */
3340 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3341 get_identifier ("__CFConstantStringClassReference"),
3342 build_array_type (integer_type_node, NULL_TREE));
3344 TREE_PUBLIC (cfstring_class_reference) = 1;
3345 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3346 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3347 DECL_EXTERNAL (cfstring_class_reference) = 1;
3348 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3350 /* Initialize the hash table used to hold the constant CFString objects. */
3351 cfstring_htab = htab_create_ggc (31, cfstring_hash, cfstring_eq, NULL);
3353 return cfstring_type_node;
3356 tree
3357 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3358 bool ARG_UNUSED (ignore))
3360 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3362 if (fcode == darwin_builtin_cfstring)
3364 if (!darwin_constant_cfstrings)
3366 error ("built-in function %qD requires the"
3367 " %<-mconstant-cfstrings%> flag", fndecl);
3368 return error_mark_node;
3371 if (n_args != 1)
3373 error ("built-in function %qD takes one argument only", fndecl);
3374 return error_mark_node;
3377 return darwin_build_constant_cfstring (*argp);
3380 return NULL_TREE;
3383 void
3384 darwin_rename_builtins (void)
3386 /* The system ___divdc3 routine in libSystem on darwin10 is not
3387 accurate to 1ulp, ours is, so we avoid ever using the system name
3388 for this routine and instead install a non-conflicting name that
3389 is accurate.
3391 When -ffast-math or -funsafe-math-optimizations is given, we can
3392 use the faster version. */
3393 if (!flag_unsafe_math_optimizations)
3395 enum built_in_function dcode
3396 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3397 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3398 tree fn = builtin_decl_explicit (dcode);
3399 /* Fortran and c call TARGET_INIT_BUILTINS and
3400 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3401 call into each to ensure that at least one of them is called
3402 after build_common_builtin_nodes. A better fix is to add a
3403 new hook to run after build_common_builtin_nodes runs. */
3404 if (fn)
3405 set_user_assembler_name (fn, "___ieee_divdc3");
3406 fn = builtin_decl_implicit (dcode);
3407 if (fn)
3408 set_user_assembler_name (fn, "___ieee_divdc3");
3412 bool
3413 darwin_libc_has_function (enum function_class fn_class)
3415 if (fn_class == function_sincos)
3416 return false;
3417 if (fn_class == function_c99_math_complex
3418 || fn_class == function_c99_misc)
3419 return (TARGET_64BIT
3420 || strverscmp (darwin_macosx_version_min, "10.3") >= 0);
3422 return true;
3425 static hashval_t
3426 cfstring_hash (const void *ptr)
3428 tree str = ((const struct cfstring_descriptor *)ptr)->literal;
3429 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3430 int i, len = TREE_STRING_LENGTH (str);
3431 hashval_t h = len;
3433 for (i = 0; i < len; i++)
3434 h = ((h * 613) + p[i]);
3436 return h;
3439 static int
3440 cfstring_eq (const void *ptr1, const void *ptr2)
3442 tree str1 = ((const struct cfstring_descriptor *)ptr1)->literal;
3443 tree str2 = ((const struct cfstring_descriptor *)ptr2)->literal;
3444 int len1 = TREE_STRING_LENGTH (str1);
3446 return (len1 == TREE_STRING_LENGTH (str2)
3447 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3448 len1));
3451 tree
3452 darwin_build_constant_cfstring (tree str)
3454 struct cfstring_descriptor *desc, key;
3455 void **loc;
3456 tree addr;
3458 if (!str)
3460 error ("CFString literal is missing");
3461 return error_mark_node;
3464 STRIP_NOPS (str);
3466 if (TREE_CODE (str) == ADDR_EXPR)
3467 str = TREE_OPERAND (str, 0);
3469 if (TREE_CODE (str) != STRING_CST)
3471 error ("CFString literal expression is not a string constant");
3472 return error_mark_node;
3475 /* Perhaps we already constructed a constant CFString just like this one? */
3476 key.literal = str;
3477 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3478 desc = (struct cfstring_descriptor *) *loc;
3480 if (!desc)
3482 tree var, constructor, field;
3483 vec<constructor_elt, va_gc> *v = NULL;
3484 int length = TREE_STRING_LENGTH (str) - 1;
3486 if (darwin_warn_nonportable_cfstrings)
3488 const char *s = TREE_STRING_POINTER (str);
3489 int l = 0;
3491 for (l = 0; l < length; l++)
3492 if (!s[l] || !isascii (s[l]))
3494 warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
3495 s[l] ? "non-ASCII character" : "embedded NUL");
3496 break;
3500 *loc = desc = ggc_cleared_alloc<cfstring_descriptor> ();
3501 desc->literal = str;
3503 /* isa *. */
3504 field = TYPE_FIELDS (ccfstring_type_node);
3505 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3506 build1 (ADDR_EXPR, TREE_TYPE (field),
3507 cfstring_class_reference));
3508 /* flags */
3509 field = DECL_CHAIN (field);
3510 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3511 build_int_cst (TREE_TYPE (field), 0x000007c8));
3512 /* string *. */
3513 field = DECL_CHAIN (field);
3514 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3515 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3516 /* length */
3517 field = DECL_CHAIN (field);
3518 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3519 build_int_cst (TREE_TYPE (field), length));
3521 constructor = build_constructor (ccfstring_type_node, v);
3522 TREE_READONLY (constructor) = 1;
3523 TREE_CONSTANT (constructor) = 1;
3524 TREE_STATIC (constructor) = 1;
3526 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3527 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3528 being built without any knowledge of C++ tree accessors; hence, we shall
3529 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3530 if (darwin_running_cxx)
3531 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3533 /* Create an anonymous global variable for this CFString. */
3534 var = build_decl (input_location, CONST_DECL,
3535 NULL, TREE_TYPE (constructor));
3536 DECL_ARTIFICIAL (var) = 1;
3537 TREE_STATIC (var) = 1;
3538 DECL_INITIAL (var) = constructor;
3539 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3540 DECL_CONTEXT (var) = NULL_TREE;
3541 desc->constructor = var;
3544 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3545 TREE_CONSTANT (addr) = 1;
3547 return addr;
3550 bool
3551 darwin_cfstring_p (tree str)
3553 struct cfstring_descriptor key;
3554 void **loc;
3556 if (!str)
3557 return false;
3559 STRIP_NOPS (str);
3561 if (TREE_CODE (str) == ADDR_EXPR)
3562 str = TREE_OPERAND (str, 0);
3564 if (TREE_CODE (str) != STRING_CST)
3565 return false;
3567 key.literal = str;
3568 loc = htab_find_slot (cfstring_htab, &key, NO_INSERT);
3570 if (loc)
3571 return true;
3573 return false;
3576 void
3577 darwin_enter_string_into_cfstring_table (tree str)
3579 struct cfstring_descriptor key;
3580 void **loc;
3582 key.literal = str;
3583 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3585 if (!*loc)
3587 *loc = ggc_cleared_alloc<cfstring_descriptor> ();
3588 ((struct cfstring_descriptor *)*loc)->literal = str;
3592 /* Choose named function section based on its frequency. */
3594 section *
3595 darwin_function_section (tree decl, enum node_frequency freq,
3596 bool startup, bool exit)
3598 /* Decide if we need to put this in a coalescable section. */
3599 bool weak = (decl
3600 && DECL_WEAK (decl)
3601 && (!DECL_ATTRIBUTES (decl)
3602 || !lookup_attribute ("weak_import",
3603 DECL_ATTRIBUTES (decl))));
3605 /* If there is a specified section name, we should not be trying to
3606 override. */
3607 if (decl && DECL_SECTION_NAME (decl) != NULL)
3608 return get_named_section (decl, NULL, 0);
3610 /* We always put unlikely executed stuff in the cold section. */
3611 if (freq == NODE_FREQUENCY_UNLIKELY_EXECUTED)
3612 return (weak) ? darwin_sections[text_cold_coal_section]
3613 : darwin_sections[text_cold_section];
3615 /* If we have LTO *and* feedback information, then let LTO handle
3616 the function ordering, it makes a better job (for normal, hot,
3617 startup and exit - hence the bailout for cold above). */
3618 if (in_lto_p && flag_profile_values)
3619 goto default_function_sections;
3621 /* Non-cold startup code should go to startup subsection. */
3622 if (startup)
3623 return (weak) ? darwin_sections[text_startup_coal_section]
3624 : darwin_sections[text_startup_section];
3626 /* Similarly for exit. */
3627 if (exit)
3628 return (weak) ? darwin_sections[text_exit_coal_section]
3629 : darwin_sections[text_exit_section];
3631 /* Place hot code. */
3632 if (freq == NODE_FREQUENCY_HOT)
3633 return (weak) ? darwin_sections[text_hot_coal_section]
3634 : darwin_sections[text_hot_section];
3636 /* Otherwise, default to the 'normal' non-reordered sections. */
3637 default_function_sections:
3638 return (weak) ? darwin_sections[text_coal_section]
3639 : text_section;
3642 /* When a function is partitioned between sections, we need to insert a label
3643 at the start of each new chunk - so that it may become a valid 'atom' for
3644 eh and debug purposes. Without this the linker will emit warnings if one
3645 tries to add line location information (since the switched fragment will
3646 be anonymous). */
3648 void
3649 darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
3651 char buf[128];
3652 snprintf (buf, 128, "%s%s",new_is_cold?"__cold_sect_of_":"__hot_sect_of_",
3653 IDENTIFIER_POINTER (DECL_NAME (decl)));
3654 /* Make sure we pick up all the relevant quotes etc. */
3655 assemble_name_raw (fp, (const char *) buf);
3656 fputs (":\n", fp);
3659 #include "gt-darwin.h"