Merged revisions 195901,195904,196012 via svnmerge from
[official-gcc.git] / main / gcc / config / darwin.c
blobf1bf24aa91dde3ff83f76e5d139450eb5747cfa9
1 /* Functions for generic Darwin as target machine for GNU C compiler.
2 Copyright (C) 1989-2013 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 "expr.h"
36 #include "reload.h"
37 #include "function.h"
38 #include "ggc.h"
39 #include "langhooks.h"
40 #include "target.h"
41 #include "tm_p.h"
42 #include "diagnostic-core.h"
43 #include "toplev.h"
44 #include "hashtab.h"
45 #include "df.h"
46 #include "debug.h"
47 #include "obstack.h"
48 #include "lto-streamer.h"
50 /* Darwin supports a feature called fix-and-continue, which is used
51 for rapid turn around debugging. When code is compiled with the
52 -mfix-and-continue flag, two changes are made to the generated code
53 that allow the system to do things that it would normally not be
54 able to do easily. These changes allow gdb to load in
55 recompilation of a translation unit that has been changed into a
56 running program and replace existing functions and methods of that
57 translation unit with versions of those functions and methods
58 from the newly compiled translation unit. The new functions access
59 the existing static symbols from the old translation unit, if the
60 symbol existed in the unit to be replaced, and from the new
61 translation unit, otherwise.
63 The changes are to insert 5 nops at the beginning of all functions
64 and to use indirection to get at static symbols. The 5 nops
65 are required by consumers of the generated code. Currently, gdb
66 uses this to patch in a jump to the overriding function, this
67 allows all uses of the old name to forward to the replacement,
68 including existing function pointers and virtual methods. See
69 rs6000_emit_prologue for the code that handles the nop insertions.
71 The added indirection allows gdb to redirect accesses to static
72 symbols from the newly loaded translation unit to the existing
73 symbol, if any. @code{static} symbols are special and are handled by
74 setting the second word in the .non_lazy_symbol_pointer data
75 structure to symbol. See indirect_data for the code that handles
76 the extra indirection, and machopic_output_indirection and its use
77 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
78 symbol indirection. */
80 /* For darwin >= 9 (OSX 10.5) the linker is capable of making the necessary
81 branch islands and we no longer need to emit darwin stubs.
82 However, if we are generating code for earlier systems (or for use in the
83 kernel) the stubs might still be required, and this will be set true. */
84 int darwin_emit_branch_islands = false;
86 typedef struct GTY(()) ctor_record {
87 rtx symbol;
88 int priority; /* constructor priority */
89 int position; /* original position */
90 } ctor_record;
92 static GTY(()) vec<ctor_record, va_gc> *ctors = NULL;
94 /* A flag to determine whether we are running c++ or obj-c++. This has to be
95 settable from non-c-family contexts too (i.e. we can't use the c_dialect_
96 functions). */
97 int darwin_running_cxx;
99 /* Some code-gen now depends on OS major version numbers (at least). */
100 int generating_for_darwin_version ;
102 /* Section names. */
103 section * darwin_sections[NUM_DARWIN_SECTIONS];
105 /* While we transition to using in-tests instead of ifdef'd code. */
106 #ifndef HAVE_lo_sum
107 #define HAVE_lo_sum 0
108 #define gen_macho_high(a,b) (a)
109 #define gen_macho_low(a,b,c) (a)
110 #endif
112 /* True if we're setting __attribute__ ((ms_struct)). */
113 int darwin_ms_struct = false;
115 /* Earlier versions of Darwin as do not recognize an alignment field in
116 .comm directives, this should be set for versions that allow it. */
117 int emit_aligned_common = false;
119 /* A get_unnamed_section callback used to switch to an ObjC section.
120 DIRECTIVE is as for output_section_asm_op. */
122 static void
123 output_objc_section_asm_op (const void *directive)
125 static bool been_here = false;
127 /* The NeXT ObjC Runtime requires these sections to be present and in
128 order in the object. The code below implements this by emitting
129 a section header for each ObjC section the first time that an ObjC
130 section is requested. */
131 if (! been_here)
133 section *saved_in_section = in_section;
134 static const enum darwin_section_enum tomark[] =
136 /* written, cold -> hot */
137 objc_cat_cls_meth_section,
138 objc_cat_inst_meth_section,
139 objc_string_object_section,
140 objc_constant_string_object_section,
141 objc_selector_refs_section,
142 objc_selector_fixup_section,
143 objc_cls_refs_section,
144 objc_class_section,
145 objc_meta_class_section,
146 /* shared, hot -> cold */
147 objc_cls_meth_section,
148 objc_inst_meth_section,
149 objc_protocol_section,
150 objc_class_names_section,
151 objc_meth_var_types_section,
152 objc_meth_var_names_section,
153 objc_category_section,
154 objc_class_vars_section,
155 objc_instance_vars_section,
156 objc_module_info_section,
157 objc_symbols_section,
159 /* ABI=1 */
160 static const enum darwin_section_enum tomarkv1[] =
162 objc1_protocol_ext_section,
163 objc1_class_ext_section,
164 objc1_prop_list_section
166 /* ABI=2 */
167 static const enum darwin_section_enum tomarkv2[] =
169 objc2_message_refs_section,
170 objc2_classdefs_section,
171 objc2_metadata_section,
172 objc2_classrefs_section,
173 objc2_classlist_section,
174 objc2_categorylist_section,
175 objc2_selector_refs_section,
176 objc2_nonlazy_class_section,
177 objc2_nonlazy_category_section,
178 objc2_protocollist_section,
179 objc2_protocolrefs_section,
180 objc2_super_classrefs_section,
181 objc2_image_info_section,
182 objc2_constant_string_object_section
184 size_t i;
186 been_here = true;
187 if (flag_objc_abi < 2)
189 for (i = 0; i < ARRAY_SIZE (tomark); i++)
190 switch_to_section (darwin_sections[tomark[i]]);
191 if (flag_objc_abi == 1)
192 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
193 switch_to_section (darwin_sections[tomarkv1[i]]);
195 else
196 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
197 switch_to_section (darwin_sections[tomarkv2[i]]);
198 /* Make sure we don't get varasm.c out of sync with us. */
199 switch_to_section (saved_in_section);
201 output_section_asm_op (directive);
205 /* Private flag applied to disable section-anchors in a particular section. */
206 #define SECTION_NO_ANCHOR SECTION_MACH_DEP
209 /* Implement TARGET_ASM_INIT_SECTIONS. */
211 void
212 darwin_init_sections (void)
214 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
215 darwin_sections[NAME] = \
216 get_unnamed_section (FLAGS, (OBJC \
217 ? output_objc_section_asm_op \
218 : output_section_asm_op), \
219 "\t" DIRECTIVE);
220 #include "config/darwin-sections.def"
221 #undef DEF_SECTION
223 readonly_data_section = darwin_sections[const_section];
224 exception_section = darwin_sections[darwin_exception_section];
225 eh_frame_section = darwin_sections[darwin_eh_frame_section];
229 name_needs_quotes (const char *name)
231 int c;
232 while ((c = *name++) != '\0')
233 if (! ISIDNUM (c)
234 && c != '.' && c != '$' && c != '_' )
235 return 1;
236 return 0;
239 /* Return true if SYM_REF can be used without an indirection. */
241 machopic_symbol_defined_p (rtx sym_ref)
243 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
244 return true;
246 /* If a symbol references local and is not an extern to this
247 file, then the symbol might be able to declared as defined. */
248 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
250 /* If the symbol references a variable and the variable is a
251 common symbol, then this symbol is not defined. */
252 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
254 tree decl = SYMBOL_REF_DECL (sym_ref);
255 if (!decl)
256 return true;
257 if (DECL_COMMON (decl))
258 return false;
260 return true;
262 return false;
265 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
266 reference, which will not be changed. */
268 enum machopic_addr_class
269 machopic_classify_symbol (rtx sym_ref)
271 bool function_p;
273 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
274 if (machopic_symbol_defined_p (sym_ref))
275 return (function_p
276 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
277 else
278 return (function_p
279 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
282 #ifndef TARGET_FIX_AND_CONTINUE
283 #define TARGET_FIX_AND_CONTINUE 0
284 #endif
286 /* Indicate when fix-and-continue style code generation is being used
287 and when a reference to data should be indirected so that it can be
288 rebound in a new translation unit to reference the original instance
289 of that data. Symbol names that are for code generation local to
290 the translation unit are bound to the new translation unit;
291 currently this means symbols that begin with L or _OBJC_;
292 otherwise, we indicate that an indirect reference should be made to
293 permit the runtime to rebind new instances of the translation unit
294 to the original instance of the data. */
296 static int
297 indirect_data (rtx sym_ref)
299 int lprefix;
300 const char *name;
302 /* If we aren't generating fix-and-continue code, don't do anything
303 special. */
304 if (TARGET_FIX_AND_CONTINUE == 0)
305 return 0;
307 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
308 are indirected. Symbols that begin with L and _OBJC_ are always
309 bound to the current translation unit as they are used for
310 generated local data of the translation unit. */
312 name = XSTR (sym_ref, 0);
314 lprefix = (((name[0] == '*' || name[0] == '&')
315 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
316 || (strncmp (name, "_OBJC_", 6) == 0));
318 return ! lprefix;
321 static int
322 machopic_data_defined_p (rtx sym_ref)
324 if (indirect_data (sym_ref))
325 return 0;
327 switch (machopic_classify_symbol (sym_ref))
329 case MACHOPIC_DEFINED_DATA:
330 case MACHOPIC_DEFINED_FUNCTION:
331 return 1;
332 default:
333 return 0;
337 void
338 machopic_define_symbol (rtx mem)
340 rtx sym_ref;
342 gcc_assert (GET_CODE (mem) == MEM);
343 sym_ref = XEXP (mem, 0);
344 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
347 /* Return either ORIG or:
349 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
351 depending on MACHO_DYNAMIC_NO_PIC_P. */
353 machopic_gen_offset (rtx orig)
355 if (MACHO_DYNAMIC_NO_PIC_P)
356 return orig;
357 else
359 /* Play games to avoid marking the function as needing pic if we
360 are being called as part of the cost-estimation process. */
361 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
362 crtl->uses_pic_offset_table = 1;
363 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
364 UNSPEC_MACHOPIC_OFFSET);
365 return gen_rtx_CONST (Pmode, orig);
369 static GTY(()) const char * function_base_func_name;
370 static GTY(()) int current_pic_label_num;
372 void
373 machopic_output_function_base_name (FILE *file)
375 const char *current_name;
377 /* If dynamic-no-pic is on, we should not get here. */
378 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
379 /* When we are generating _get_pc thunks within stubs, there is no current
380 function. */
381 if (current_function_decl)
383 current_name =
384 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
385 if (function_base_func_name != current_name)
387 ++current_pic_label_num;
388 function_base_func_name = current_name;
391 else
393 ++current_pic_label_num;
394 function_base_func_name = "L_machopic_stub_dummy";
396 fprintf (file, "L%011d$pb", current_pic_label_num);
399 /* The suffix attached to non-lazy pointer symbols. */
400 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
401 /* The suffix attached to stub symbols. */
402 #define STUB_SUFFIX "$stub"
404 typedef struct GTY (()) machopic_indirection
406 /* The SYMBOL_REF for the entity referenced. */
407 rtx symbol;
408 /* The name of the stub or non-lazy pointer. */
409 const char * ptr_name;
410 /* True iff this entry is for a stub (as opposed to a non-lazy
411 pointer). */
412 bool stub_p;
413 /* True iff this stub or pointer pointer has been referenced. */
414 bool used;
415 } machopic_indirection;
417 /* A table mapping stub names and non-lazy pointer names to
418 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
420 static GTY ((param_is (struct machopic_indirection))) htab_t
421 machopic_indirections;
423 /* Return a hash value for a SLOT in the indirections hash table. */
425 static hashval_t
426 machopic_indirection_hash (const void *slot)
428 const machopic_indirection *p = (const machopic_indirection *) slot;
429 return htab_hash_string (p->ptr_name);
432 /* Returns true if the KEY is the same as that associated with
433 SLOT. */
435 static int
436 machopic_indirection_eq (const void *slot, const void *key)
438 return strcmp (((const machopic_indirection *) slot)->ptr_name,
439 (const char *) key) == 0;
442 /* Return the name of the non-lazy pointer (if STUB_P is false) or
443 stub (if STUB_B is true) corresponding to the given name. */
445 const char *
446 machopic_indirection_name (rtx sym_ref, bool stub_p)
448 char *buffer;
449 const char *name = XSTR (sym_ref, 0);
450 size_t namelen = strlen (name);
451 machopic_indirection *p;
452 void ** slot;
453 bool needs_quotes;
454 const char *suffix;
455 const char *prefix = user_label_prefix;
456 const char *quote = "";
457 tree id;
459 id = maybe_get_identifier (name);
460 if (id)
462 tree id_orig = id;
464 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
465 id = TREE_CHAIN (id);
466 if (id != id_orig)
468 name = IDENTIFIER_POINTER (id);
469 namelen = strlen (name);
473 if (name[0] == '*')
475 prefix = "";
476 ++name;
477 --namelen;
480 needs_quotes = name_needs_quotes (name);
481 if (needs_quotes)
483 quote = "\"";
486 if (stub_p)
487 suffix = STUB_SUFFIX;
488 else
489 suffix = NON_LAZY_POINTER_SUFFIX;
491 buffer = XALLOCAVEC (char, strlen ("&L")
492 + strlen (prefix)
493 + namelen
494 + strlen (suffix)
495 + 2 * strlen (quote)
496 + 1 /* '\0' */);
498 /* Construct the name of the non-lazy pointer or stub. */
499 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
501 if (!machopic_indirections)
502 machopic_indirections = htab_create_ggc (37,
503 machopic_indirection_hash,
504 machopic_indirection_eq,
505 /*htab_del=*/NULL);
507 slot = htab_find_slot_with_hash (machopic_indirections, buffer,
508 htab_hash_string (buffer), INSERT);
509 if (*slot)
511 p = (machopic_indirection *) *slot;
513 else
515 p = ggc_alloc_machopic_indirection ();
516 p->symbol = sym_ref;
517 p->ptr_name = xstrdup (buffer);
518 p->stub_p = stub_p;
519 p->used = false;
520 *slot = p;
523 return p->ptr_name;
526 /* Return the name of the stub for the mcount function. */
528 const char*
529 machopic_mcount_stub_name (void)
531 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
532 return machopic_indirection_name (symbol, /*stub_p=*/true);
535 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
536 or non-lazy pointer as used -- and mark the object to which the
537 pointer/stub refers as used as well, since the pointer/stub will
538 emit a reference to it. */
540 void
541 machopic_validate_stub_or_non_lazy_ptr (const char *name)
543 machopic_indirection *p;
545 p = ((machopic_indirection *)
546 (htab_find_with_hash (machopic_indirections, name,
547 htab_hash_string (name))));
548 if (p && ! p->used)
550 const char *real_name;
551 tree id;
553 p->used = true;
555 /* Do what output_addr_const will do when we actually call it. */
556 if (SYMBOL_REF_DECL (p->symbol))
557 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
559 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
561 id = maybe_get_identifier (real_name);
562 if (id)
563 mark_referenced (id);
567 /* Transform ORIG, which may be any data source, to the corresponding
568 source using indirections. */
571 machopic_indirect_data_reference (rtx orig, rtx reg)
573 rtx ptr_ref = orig;
575 if (! MACHOPIC_INDIRECT)
576 return orig;
578 if (GET_CODE (orig) == SYMBOL_REF)
580 int defined = machopic_data_defined_p (orig);
582 if (defined && MACHO_DYNAMIC_NO_PIC_P)
584 if (DARWIN_PPC)
586 /* Create a new register for CSE opportunities. */
587 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
588 emit_insn (gen_macho_high (hi_reg, orig));
589 emit_insn (gen_macho_low (reg, hi_reg, orig));
590 return reg;
592 else if (DARWIN_X86)
593 return orig;
594 else
595 /* some other cpu -- writeme! */
596 gcc_unreachable ();
598 else if (defined)
600 rtx offset = NULL;
601 if (DARWIN_PPC || HAVE_lo_sum)
602 offset = machopic_gen_offset (orig);
604 if (DARWIN_PPC)
606 rtx hi_sum_reg = (!can_create_pseudo_p ()
607 ? reg
608 : gen_reg_rtx (Pmode));
610 gcc_assert (reg);
612 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
613 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
614 gen_rtx_HIGH (Pmode, offset))));
615 emit_insn (gen_rtx_SET (Pmode, reg,
616 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
617 copy_rtx (offset))));
619 orig = reg;
621 else if (HAVE_lo_sum)
623 gcc_assert (reg);
625 emit_insn (gen_rtx_SET (VOIDmode, reg,
626 gen_rtx_HIGH (Pmode, offset)));
627 emit_insn (gen_rtx_SET (VOIDmode, reg,
628 gen_rtx_LO_SUM (Pmode, reg,
629 copy_rtx (offset))));
630 emit_use (pic_offset_table_rtx);
632 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
634 return orig;
637 ptr_ref = (gen_rtx_SYMBOL_REF
638 (Pmode,
639 machopic_indirection_name (orig, /*stub_p=*/false)));
641 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
643 ptr_ref = gen_const_mem (Pmode, ptr_ref);
644 machopic_define_symbol (ptr_ref);
646 if (DARWIN_X86
647 && reg
648 && MACHO_DYNAMIC_NO_PIC_P)
650 emit_insn (gen_rtx_SET (Pmode, reg, ptr_ref));
651 ptr_ref = reg;
654 return ptr_ref;
656 else if (GET_CODE (orig) == CONST)
658 /* If "(const (plus ...", walk the PLUS and return that result.
659 PLUS processing (below) will restore the "(const ..." if
660 appropriate. */
661 if (GET_CODE (XEXP (orig, 0)) == PLUS)
662 return machopic_indirect_data_reference (XEXP (orig, 0), reg);
663 else
664 return orig;
666 else if (GET_CODE (orig) == MEM)
668 XEXP (ptr_ref, 0) =
669 machopic_indirect_data_reference (XEXP (orig, 0), reg);
670 return ptr_ref;
672 else if (GET_CODE (orig) == PLUS)
674 rtx base, result;
675 /* When the target is i386, this code prevents crashes due to the
676 compiler's ignorance on how to move the PIC base register to
677 other registers. (The reload phase sometimes introduces such
678 insns.) */
679 if (GET_CODE (XEXP (orig, 0)) == REG
680 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
681 /* Prevent the same register from being erroneously used
682 as both the base and index registers. */
683 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
684 && reg)
686 emit_move_insn (reg, XEXP (orig, 0));
687 XEXP (ptr_ref, 0) = reg;
688 return ptr_ref;
691 /* Legitimize both operands of the PLUS. */
692 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
693 orig = machopic_indirect_data_reference (XEXP (orig, 1),
694 (base == reg ? 0 : reg));
695 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
696 result = plus_constant (Pmode, base, INTVAL (orig));
697 else
698 result = gen_rtx_PLUS (Pmode, base, orig);
700 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
702 if (reg)
704 emit_move_insn (reg, result);
705 result = reg;
707 else
709 result = force_reg (GET_MODE (result), result);
713 return result;
715 return ptr_ref;
718 /* Transform TARGET (a MEM), which is a function call target, to the
719 corresponding symbol_stub if necessary. Return a new MEM. */
722 machopic_indirect_call_target (rtx target)
724 if (! darwin_emit_branch_islands)
725 return target;
727 if (GET_CODE (target) != MEM)
728 return target;
730 if (MACHOPIC_INDIRECT
731 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
732 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
733 & MACHO_SYMBOL_FLAG_DEFINED))
735 rtx sym_ref = XEXP (target, 0);
736 const char *stub_name = machopic_indirection_name (sym_ref,
737 /*stub_p=*/true);
738 enum machine_mode mode = GET_MODE (sym_ref);
740 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
741 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
742 MEM_READONLY_P (target) = 1;
743 MEM_NOTRAP_P (target) = 1;
746 return target;
750 machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
752 rtx pic_ref = orig;
754 if (! MACHOPIC_INDIRECT)
755 return orig;
757 /* First handle a simple SYMBOL_REF or LABEL_REF */
758 if (GET_CODE (orig) == LABEL_REF
759 || (GET_CODE (orig) == SYMBOL_REF
762 /* addr(foo) = &func+(foo-func) */
763 orig = machopic_indirect_data_reference (orig, reg);
765 if (GET_CODE (orig) == PLUS
766 && GET_CODE (XEXP (orig, 0)) == REG)
768 if (reg == 0)
769 return force_reg (mode, orig);
771 emit_move_insn (reg, orig);
772 return reg;
775 if (GET_CODE (orig) == MEM)
777 if (reg == 0)
779 gcc_assert (!reload_in_progress);
780 reg = gen_reg_rtx (Pmode);
783 #if HAVE_lo_sum
784 if (MACHO_DYNAMIC_NO_PIC_P
785 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
786 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
788 #if defined (TARGET_TOC) /* ppc */
789 rtx temp_reg = (!can_create_pseudo_p ()
790 ? reg :
791 gen_reg_rtx (Pmode));
792 rtx asym = XEXP (orig, 0);
793 rtx mem;
795 emit_insn (gen_macho_high (temp_reg, asym));
796 mem = gen_const_mem (GET_MODE (orig),
797 gen_rtx_LO_SUM (Pmode, temp_reg,
798 copy_rtx (asym)));
799 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
800 #else
801 /* Some other CPU -- WriteMe! but right now there are no other
802 platforms that can use dynamic-no-pic */
803 gcc_unreachable ();
804 #endif
805 pic_ref = reg;
807 else
808 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
809 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
811 rtx offset = machopic_gen_offset (XEXP (orig, 0));
812 #if defined (TARGET_TOC) /* i.e., PowerPC */
813 /* Generating a new reg may expose opportunities for
814 common subexpression elimination. */
815 rtx hi_sum_reg = (!can_create_pseudo_p ()
816 ? reg
817 : gen_reg_rtx (Pmode));
818 rtx mem;
819 rtx insn;
820 rtx sum;
822 sum = gen_rtx_HIGH (Pmode, offset);
823 if (! MACHO_DYNAMIC_NO_PIC_P)
824 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
826 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
828 mem = gen_const_mem (GET_MODE (orig),
829 gen_rtx_LO_SUM (Pmode,
830 hi_sum_reg,
831 copy_rtx (offset)));
832 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
833 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
835 pic_ref = reg;
836 #else
837 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
839 emit_insn (gen_rtx_SET (VOIDmode, reg,
840 gen_rtx_HIGH (Pmode,
841 gen_rtx_CONST (Pmode,
842 offset))));
843 emit_insn (gen_rtx_SET (VOIDmode, reg,
844 gen_rtx_LO_SUM (Pmode, reg,
845 gen_rtx_CONST (Pmode,
846 copy_rtx (offset)))));
847 pic_ref = gen_rtx_PLUS (Pmode,
848 pic_offset_table_rtx, reg);
849 #endif
851 else
852 #endif /* HAVE_lo_sum */
854 rtx pic = pic_offset_table_rtx;
855 if (GET_CODE (pic) != REG)
857 emit_move_insn (reg, pic);
858 pic = reg;
860 #if 0
861 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
862 #endif
864 if (reload_in_progress)
865 df_set_regs_ever_live (REGNO (pic), true);
866 pic_ref = gen_rtx_PLUS (Pmode, pic,
867 machopic_gen_offset (XEXP (orig, 0)));
870 #if !defined (TARGET_TOC)
871 emit_move_insn (reg, pic_ref);
872 pic_ref = gen_const_mem (GET_MODE (orig), reg);
873 #endif
875 else
878 #if HAVE_lo_sum
879 if (GET_CODE (orig) == SYMBOL_REF
880 || GET_CODE (orig) == LABEL_REF)
882 rtx offset = machopic_gen_offset (orig);
883 #if defined (TARGET_TOC) /* i.e., PowerPC */
884 rtx hi_sum_reg;
886 if (reg == 0)
888 gcc_assert (!reload_in_progress);
889 reg = gen_reg_rtx (Pmode);
892 hi_sum_reg = reg;
894 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
895 (MACHO_DYNAMIC_NO_PIC_P)
896 ? gen_rtx_HIGH (Pmode, offset)
897 : gen_rtx_PLUS (Pmode,
898 pic_offset_table_rtx,
899 gen_rtx_HIGH (Pmode,
900 offset))));
901 emit_insn (gen_rtx_SET (VOIDmode, reg,
902 gen_rtx_LO_SUM (Pmode,
903 hi_sum_reg,
904 copy_rtx (offset))));
905 pic_ref = reg;
906 #else
907 emit_insn (gen_rtx_SET (VOIDmode, reg,
908 gen_rtx_HIGH (Pmode, offset)));
909 emit_insn (gen_rtx_SET (VOIDmode, reg,
910 gen_rtx_LO_SUM (Pmode, reg,
911 copy_rtx (offset))));
912 pic_ref = gen_rtx_PLUS (Pmode,
913 pic_offset_table_rtx, reg);
914 #endif
916 else
917 #endif /* HAVE_lo_sum */
919 if (REG_P (orig)
920 || GET_CODE (orig) == SUBREG)
922 return orig;
924 else
926 rtx pic = pic_offset_table_rtx;
927 if (GET_CODE (pic) != REG)
929 emit_move_insn (reg, pic);
930 pic = reg;
932 #if 0
933 emit_use (pic_offset_table_rtx);
934 #endif
935 if (reload_in_progress)
936 df_set_regs_ever_live (REGNO (pic), true);
937 pic_ref = gen_rtx_PLUS (Pmode,
938 pic,
939 machopic_gen_offset (orig));
944 if (GET_CODE (pic_ref) != REG)
946 if (reg != 0)
948 emit_move_insn (reg, pic_ref);
949 return reg;
951 else
953 return force_reg (mode, pic_ref);
956 else
958 return pic_ref;
962 else if (GET_CODE (orig) == SYMBOL_REF)
963 return orig;
965 else if (GET_CODE (orig) == PLUS
966 && (GET_CODE (XEXP (orig, 0)) == MEM
967 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
968 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
969 && XEXP (orig, 0) != pic_offset_table_rtx
970 && GET_CODE (XEXP (orig, 1)) != REG)
973 rtx base;
974 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
976 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
977 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
978 Pmode, (base == reg ? 0 : reg));
979 if (GET_CODE (orig) == CONST_INT)
981 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
982 is_complex = 1;
984 else
985 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
987 if (reg && is_complex)
989 emit_move_insn (reg, pic_ref);
990 pic_ref = reg;
992 /* Likewise, should we set special REG_NOTEs here? */
995 else if (GET_CODE (orig) == CONST)
997 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1000 else if (GET_CODE (orig) == MEM
1001 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
1003 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1004 addr = replace_equiv_address (orig, addr);
1005 emit_move_insn (reg, addr);
1006 pic_ref = reg;
1009 return pic_ref;
1012 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
1013 DATA is the FILE* for assembly output. Called from
1014 htab_traverse. */
1016 static int
1017 machopic_output_indirection (void **slot, void *data)
1019 machopic_indirection *p = *((machopic_indirection **) slot);
1020 FILE *asm_out_file = (FILE *) data;
1021 rtx symbol;
1022 const char *sym_name;
1023 const char *ptr_name;
1025 if (!p->used)
1026 return 1;
1028 symbol = p->symbol;
1029 sym_name = XSTR (symbol, 0);
1030 ptr_name = p->ptr_name;
1032 if (p->stub_p)
1034 char *sym;
1035 char *stub;
1036 tree id;
1038 id = maybe_get_identifier (sym_name);
1039 if (id)
1041 tree id_orig = id;
1043 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1044 id = TREE_CHAIN (id);
1045 if (id != id_orig)
1046 sym_name = IDENTIFIER_POINTER (id);
1049 sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1050 if (sym_name[0] == '*' || sym_name[0] == '&')
1051 strcpy (sym, sym_name + 1);
1052 else if (sym_name[0] == '-' || sym_name[0] == '+')
1053 strcpy (sym, sym_name);
1054 else
1055 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1057 stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1058 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1059 strcpy (stub, ptr_name + 1);
1060 else
1061 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1063 machopic_output_stub (asm_out_file, sym, stub);
1065 else if (! indirect_data (symbol)
1066 && (machopic_symbol_defined_p (symbol)
1067 || SYMBOL_REF_LOCAL_P (symbol)))
1069 switch_to_section (data_section);
1070 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1071 assemble_label (asm_out_file, ptr_name);
1072 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1073 GET_MODE_SIZE (Pmode),
1074 GET_MODE_ALIGNMENT (Pmode), 1);
1076 else
1078 rtx init = const0_rtx;
1080 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1082 /* Mach-O symbols are passed around in code through indirect
1083 references and the original symbol_ref hasn't passed through
1084 the generic handling and reference-catching in
1085 output_operand, so we need to manually mark weak references
1086 as such. */
1087 if (SYMBOL_REF_WEAK (symbol))
1089 tree decl = SYMBOL_REF_DECL (symbol);
1090 gcc_assert (DECL_P (decl));
1092 if (decl != NULL_TREE
1093 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
1094 /* Handle only actual external-only definitions, not
1095 e.g. extern inline code or variables for which
1096 storage has been allocated. */
1097 && !TREE_STATIC (decl))
1099 fputs ("\t.weak_reference ", asm_out_file);
1100 assemble_name (asm_out_file, sym_name);
1101 fputc ('\n', asm_out_file);
1105 assemble_name (asm_out_file, ptr_name);
1106 fprintf (asm_out_file, ":\n");
1108 fprintf (asm_out_file, "\t.indirect_symbol ");
1109 assemble_name (asm_out_file, sym_name);
1110 fprintf (asm_out_file, "\n");
1112 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1113 have their symbol name instead of 0 in the second entry of
1114 the non-lazy symbol pointer data structure when they are
1115 defined. This allows the runtime to rebind newer instances
1116 of the translation unit with the original instance of the
1117 symbol. */
1119 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1120 && machopic_symbol_defined_p (symbol))
1121 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1123 assemble_integer (init, GET_MODE_SIZE (Pmode),
1124 GET_MODE_ALIGNMENT (Pmode), 1);
1127 return 1;
1130 void
1131 machopic_finish (FILE *asm_out_file)
1133 if (machopic_indirections)
1134 htab_traverse_noresize (machopic_indirections,
1135 machopic_output_indirection,
1136 asm_out_file);
1140 machopic_operand_p (rtx op)
1142 if (MACHOPIC_JUST_INDIRECT)
1143 return (GET_CODE (op) == SYMBOL_REF
1144 && machopic_symbol_defined_p (op));
1145 else
1146 return (GET_CODE (op) == CONST
1147 && GET_CODE (XEXP (op, 0)) == UNSPEC
1148 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1151 /* This function records whether a given name corresponds to a defined
1152 or undefined function or variable, for machopic_classify_ident to
1153 use later. */
1155 void
1156 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1158 rtx sym_ref;
1160 /* Do the standard encoding things first. */
1161 default_encode_section_info (decl, rtl, first);
1163 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1164 return;
1166 sym_ref = XEXP (rtl, 0);
1167 if (TREE_CODE (decl) == VAR_DECL)
1168 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1170 if (!DECL_EXTERNAL (decl)
1171 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1172 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1173 && ((TREE_STATIC (decl)
1174 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1175 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1176 && DECL_INITIAL (decl) != error_mark_node)))
1177 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1179 if (! TREE_PUBLIC (decl))
1180 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1183 void
1184 darwin_mark_decl_preserved (const char *name)
1186 fprintf (asm_out_file, "\t.no_dead_strip ");
1187 assemble_name (asm_out_file, name);
1188 fputc ('\n', asm_out_file);
1191 static section *
1192 darwin_rodata_section (int weak, bool zsize)
1194 return (weak
1195 ? darwin_sections[const_coal_section]
1196 : (zsize ? darwin_sections[zobj_const_section]
1197 : darwin_sections[const_section]));
1200 static section *
1201 darwin_mergeable_string_section (tree exp,
1202 unsigned HOST_WIDE_INT align)
1204 /* Darwin's ld expects to see non-writable string literals in the .cstring
1205 section. Later versions of ld check and complain when CFStrings are
1206 enabled. Therefore we shall force the strings into .cstring since we
1207 don't support writable ones anyway. */
1208 if ((darwin_constant_cfstrings || flag_merge_constants)
1209 && TREE_CODE (exp) == STRING_CST
1210 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1211 && align <= 256
1212 && (int_size_in_bytes (TREE_TYPE (exp))
1213 == TREE_STRING_LENGTH (exp))
1214 && ((size_t) TREE_STRING_LENGTH (exp)
1215 == strlen (TREE_STRING_POINTER (exp)) + 1))
1216 return darwin_sections[cstring_section];
1218 if (DARWIN_SECTION_ANCHORS && flag_section_anchors
1219 && TREE_CODE (exp) == STRING_CST
1220 && TREE_STRING_LENGTH (exp) == 0)
1221 return darwin_sections[zobj_const_section];
1223 return readonly_data_section;
1226 #ifndef HAVE_GAS_LITERAL16
1227 #define HAVE_GAS_LITERAL16 0
1228 #endif
1230 static section *
1231 darwin_mergeable_constant_section (tree exp,
1232 unsigned HOST_WIDE_INT align,
1233 bool zsize)
1235 enum machine_mode mode = DECL_MODE (exp);
1236 unsigned int modesize = GET_MODE_BITSIZE (mode);
1238 if (DARWIN_SECTION_ANCHORS
1239 && flag_section_anchors
1240 && zsize)
1241 return darwin_sections[zobj_const_section];
1243 if (flag_merge_constants
1244 && mode != VOIDmode
1245 && mode != BLKmode
1246 && modesize <= align
1247 && align >= 8
1248 && align <= 256
1249 && (align & (align -1)) == 0)
1251 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1253 if (TREE_CODE (size) == INTEGER_CST
1254 && TREE_INT_CST_LOW (size) == 4
1255 && TREE_INT_CST_HIGH (size) == 0)
1256 return darwin_sections[literal4_section];
1257 else if (TREE_CODE (size) == INTEGER_CST
1258 && TREE_INT_CST_LOW (size) == 8
1259 && TREE_INT_CST_HIGH (size) == 0)
1260 return darwin_sections[literal8_section];
1261 else if (HAVE_GAS_LITERAL16
1262 && TARGET_64BIT
1263 && TREE_CODE (size) == INTEGER_CST
1264 && TREE_INT_CST_LOW (size) == 16
1265 && TREE_INT_CST_HIGH (size) == 0)
1266 return darwin_sections[literal16_section];
1267 else
1268 return readonly_data_section;
1271 return readonly_data_section;
1274 section *
1275 darwin_tm_clone_table_section (void)
1277 return get_named_section (NULL,
1278 "__DATA,__tm_clone_table,regular,no_dead_strip",
1283 machopic_reloc_rw_mask (void)
1285 return MACHOPIC_INDIRECT ? 3 : 0;
1288 /* We have to deal with ObjC/C++ metadata section placement in the common
1289 code, since it will also be called from LTO.
1291 Return metadata attributes, if present (searching for ABI=2 first)
1292 Return NULL_TREE if no such attributes are found. */
1294 static tree
1295 is_objc_metadata (tree decl)
1297 if (DECL_P (decl)
1298 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1299 && DECL_ATTRIBUTES (decl))
1301 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1302 if (meta)
1303 return meta;
1304 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1305 if (meta)
1306 return meta;
1308 return NULL_TREE;
1311 /* Return the section required for Objective C ABI 2 metadata. */
1312 static section *
1313 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1315 const char *p;
1316 tree ident = TREE_VALUE (meta);
1317 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1318 p = IDENTIFIER_POINTER (ident);
1320 /* If we are in LTO, then we don't know the state of flag_next_runtime
1321 or flag_objc_abi when the code was generated. We set these from the
1322 meta-data - which is needed to deal with const string constructors. */
1324 flag_next_runtime = 1;
1325 flag_objc_abi = 2;
1327 if (base == data_section)
1328 base = darwin_sections[objc2_metadata_section];
1330 /* Most of the OBJC2 META-data end up in the base section, so check it
1331 first. */
1332 if (!strncmp (p, "V2_BASE", 7))
1333 return base;
1334 else if (!strncmp (p, "V2_STRG", 7))
1335 return darwin_sections[cstring_section];
1337 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1338 return darwin_sections[objc2_classdefs_section];
1339 else if (!strncmp (p, "V2_MREF", 7))
1340 return darwin_sections[objc2_message_refs_section];
1341 else if (!strncmp (p, "V2_CLRF", 7))
1342 return darwin_sections[objc2_classrefs_section];
1343 else if (!strncmp (p, "V2_SURF", 7))
1344 return darwin_sections[objc2_super_classrefs_section];
1345 else if (!strncmp (p, "V2_NLCL", 7))
1346 return darwin_sections[objc2_nonlazy_class_section];
1347 else if (!strncmp (p, "V2_CLAB", 7))
1348 return darwin_sections[objc2_classlist_section];
1349 else if (!strncmp (p, "V2_SRFS", 7))
1350 return darwin_sections[objc2_selector_refs_section];
1351 else if (!strncmp (p, "V2_NLCA", 7))
1352 return darwin_sections[objc2_nonlazy_category_section];
1353 else if (!strncmp (p, "V2_CALA", 7))
1354 return darwin_sections[objc2_categorylist_section];
1356 else if (!strncmp (p, "V2_PLST", 7))
1357 return darwin_sections[objc2_protocollist_section];
1358 else if (!strncmp (p, "V2_PRFS", 7))
1359 return darwin_sections[objc2_protocolrefs_section];
1361 else if (!strncmp (p, "V2_INFO", 7))
1362 return darwin_sections[objc2_image_info_section];
1364 else if (!strncmp (p, "V2_EHTY", 7))
1365 return darwin_sections[data_coal_section];
1367 else if (!strncmp (p, "V2_CSTR", 7))
1368 return darwin_sections[objc2_constant_string_object_section];
1370 /* Not recognized, default. */
1371 return base;
1374 /* Return the section required for Objective C ABI 0/1 metadata. */
1375 static section *
1376 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1378 const char *p;
1379 tree ident = TREE_VALUE (meta);
1380 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1381 p = IDENTIFIER_POINTER (ident);
1383 /* If we are in LTO, then we don't know the state of flag_next_runtime
1384 or flag_objc_abi when the code was generated. We set these from the
1385 meta-data - which is needed to deal with const string constructors. */
1386 flag_next_runtime = 1;
1387 if (!global_options_set.x_flag_objc_abi)
1388 flag_objc_abi = 1;
1390 /* String sections first, cos there are lots of strings. */
1391 if (!strncmp (p, "V1_STRG", 7))
1392 return darwin_sections[cstring_section];
1393 else if (!strncmp (p, "V1_CLSN", 7))
1394 return darwin_sections[objc_class_names_section];
1395 else if (!strncmp (p, "V1_METN", 7))
1396 return darwin_sections[objc_meth_var_names_section];
1397 else if (!strncmp (p, "V1_METT", 7))
1398 return darwin_sections[objc_meth_var_types_section];
1400 else if (!strncmp (p, "V1_CLAS", 7))
1401 return darwin_sections[objc_class_section];
1402 else if (!strncmp (p, "V1_META", 7))
1403 return darwin_sections[objc_meta_class_section];
1404 else if (!strncmp (p, "V1_CATG", 7))
1405 return darwin_sections[objc_category_section];
1406 else if (!strncmp (p, "V1_PROT", 7))
1407 return darwin_sections[objc_protocol_section];
1409 else if (!strncmp (p, "V1_CLCV", 7))
1410 return darwin_sections[objc_class_vars_section];
1411 else if (!strncmp (p, "V1_CLIV", 7))
1412 return darwin_sections[objc_instance_vars_section];
1414 else if (!strncmp (p, "V1_CLCM", 7))
1415 return darwin_sections[objc_cls_meth_section];
1416 else if (!strncmp (p, "V1_CLIM", 7))
1417 return darwin_sections[objc_inst_meth_section];
1418 else if (!strncmp (p, "V1_CACM", 7))
1419 return darwin_sections[objc_cat_cls_meth_section];
1420 else if (!strncmp (p, "V1_CAIM", 7))
1421 return darwin_sections[objc_cat_inst_meth_section];
1422 else if (!strncmp (p, "V1_PNSM", 7))
1423 return darwin_sections[objc_cat_inst_meth_section];
1424 else if (!strncmp (p, "V1_PCLM", 7))
1425 return darwin_sections[objc_cat_cls_meth_section];
1427 else if (!strncmp (p, "V1_CLPR", 7))
1428 return darwin_sections[objc_cat_cls_meth_section];
1429 else if (!strncmp (p, "V1_CAPR", 7))
1430 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1432 else if (!strncmp (p, "V1_PRFS", 7))
1433 return darwin_sections[objc_cat_cls_meth_section];
1434 else if (!strncmp (p, "V1_CLRF", 7))
1435 return darwin_sections[objc_cls_refs_section];
1436 else if (!strncmp (p, "V1_SRFS", 7))
1437 return darwin_sections[objc_selector_refs_section];
1439 else if (!strncmp (p, "V1_MODU", 7))
1440 return darwin_sections[objc_module_info_section];
1441 else if (!strncmp (p, "V1_SYMT", 7))
1442 return darwin_sections[objc_symbols_section];
1443 else if (!strncmp (p, "V1_INFO", 7))
1444 return darwin_sections[objc_image_info_section];
1446 else if (!strncmp (p, "V1_PLST", 7))
1447 return darwin_sections[objc1_prop_list_section];
1448 else if (!strncmp (p, "V1_PEXT", 7))
1449 return darwin_sections[objc1_protocol_ext_section];
1450 else if (!strncmp (p, "V1_CEXT", 7))
1451 return darwin_sections[objc1_class_ext_section];
1453 else if (!strncmp (p, "V2_CSTR", 7))
1454 return darwin_sections[objc_constant_string_object_section];
1456 return base;
1459 section *
1460 machopic_select_section (tree decl,
1461 int reloc,
1462 unsigned HOST_WIDE_INT align)
1464 bool zsize, one, weak, ro;
1465 section *base_section = NULL;
1467 weak = (DECL_P (decl)
1468 && DECL_WEAK (decl)
1469 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1471 zsize = (DECL_P (decl)
1472 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1473 && tree_low_cst (DECL_SIZE_UNIT (decl), 1) == 0);
1475 one = DECL_P (decl)
1476 && TREE_CODE (decl) == VAR_DECL
1477 && DECL_ONE_ONLY (decl);
1479 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1481 switch (categorize_decl_for_section (decl, reloc))
1483 case SECCAT_TEXT:
1484 gcc_unreachable ();
1485 break;
1487 case SECCAT_RODATA:
1488 case SECCAT_SRODATA:
1489 base_section = darwin_rodata_section (weak, zsize);
1490 break;
1492 case SECCAT_RODATA_MERGE_STR:
1493 base_section = darwin_mergeable_string_section (decl, align);
1494 break;
1496 case SECCAT_RODATA_MERGE_STR_INIT:
1497 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1498 break;
1500 case SECCAT_RODATA_MERGE_CONST:
1501 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1502 break;
1504 case SECCAT_DATA:
1505 case SECCAT_DATA_REL:
1506 case SECCAT_DATA_REL_LOCAL:
1507 case SECCAT_DATA_REL_RO:
1508 case SECCAT_DATA_REL_RO_LOCAL:
1509 case SECCAT_SDATA:
1510 case SECCAT_TDATA:
1511 if (weak || one)
1513 if (ro)
1514 base_section = darwin_sections[const_data_coal_section];
1515 else
1516 base_section = darwin_sections[data_coal_section];
1518 else if (DARWIN_SECTION_ANCHORS
1519 && flag_section_anchors
1520 && zsize)
1522 /* If we're doing section anchors, then punt zero-sized objects into
1523 their own sections so that they don't interfere with offset
1524 computation for the remaining vars. This does not need to be done
1525 for stuff in mergeable sections, since these are ineligible for
1526 anchors. */
1527 if (ro)
1528 base_section = darwin_sections[zobj_const_data_section];
1529 else
1530 base_section = darwin_sections[zobj_data_section];
1532 else if (ro)
1533 base_section = darwin_sections[const_data_section];
1534 else
1535 base_section = data_section;
1536 break;
1537 case SECCAT_BSS:
1538 case SECCAT_SBSS:
1539 case SECCAT_TBSS:
1540 if (weak || one)
1541 base_section = darwin_sections[data_coal_section];
1542 else
1544 if (!TREE_PUBLIC (decl))
1545 base_section = lcomm_section;
1546 else if (bss_noswitch_section)
1547 base_section = bss_noswitch_section;
1548 else
1549 base_section = data_section;
1551 break;
1553 default:
1554 gcc_unreachable ();
1557 /* Darwin weird special cases.
1558 a) OBJC Meta-data. */
1559 if (DECL_P (decl)
1560 && (TREE_CODE (decl) == VAR_DECL
1561 || TREE_CODE (decl) == CONST_DECL)
1562 && DECL_ATTRIBUTES (decl))
1564 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1565 if (meta)
1566 return darwin_objc2_section (decl, meta, base_section);
1567 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1568 if (meta)
1569 return darwin_objc1_section (decl, meta, base_section);
1570 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1571 if (meta)
1572 return base_section; /* GNU runtime is happy with it all in one pot. */
1575 /* b) Constant string objects. */
1576 if (TREE_CODE (decl) == CONSTRUCTOR
1577 && TREE_TYPE (decl)
1578 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1579 && TYPE_NAME (TREE_TYPE (decl)))
1581 tree name = TYPE_NAME (TREE_TYPE (decl));
1582 if (TREE_CODE (name) == TYPE_DECL)
1583 name = DECL_NAME (name);
1585 /* FIXME: This is unsatisfactory for LTO, since it relies on other
1586 metadata determining the source FE. */
1587 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1589 if (flag_next_runtime)
1591 if (flag_objc_abi == 2)
1592 return darwin_sections[objc2_constant_string_object_section];
1593 else
1594 return darwin_sections[objc_constant_string_object_section];
1596 else
1597 return darwin_sections[objc_string_object_section];
1599 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1600 return darwin_sections[cfstring_constant_object_section];
1601 else
1602 return base_section;
1604 /* c) legacy meta-data selection. */
1605 else if (TREE_CODE (decl) == VAR_DECL
1606 && DECL_NAME (decl)
1607 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1608 && IDENTIFIER_POINTER (DECL_NAME (decl))
1609 && flag_next_runtime
1610 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1612 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1613 static bool warned_objc_46 = false;
1614 /* We shall assert that zero-sized objects are an error in ObjC
1615 meta-data. */
1616 gcc_assert (tree_low_cst (DECL_SIZE_UNIT (decl), 1) != 0);
1618 /* ??? This mechanism for determining the metadata section is
1619 broken when LTO is in use, since the frontend that generated
1620 the data is not identified. We will keep the capability for
1621 the short term - in case any non-Objective-C programs are using
1622 it to place data in specified sections. */
1623 if (!warned_objc_46)
1625 location_t loc = DECL_SOURCE_LOCATION (decl);
1626 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
1627 " to select meta-data sections is deprecated at 4.6"
1628 " and will be removed in 4.7");
1629 warned_objc_46 = true;
1632 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1633 return darwin_sections[objc_cls_meth_section];
1634 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1635 return darwin_sections[objc_inst_meth_section];
1636 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
1637 return darwin_sections[objc_cat_cls_meth_section];
1638 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
1639 return darwin_sections[objc_cat_inst_meth_section];
1640 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1641 return darwin_sections[objc_class_vars_section];
1642 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1643 return darwin_sections[objc_instance_vars_section];
1644 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1645 return darwin_sections[objc_cat_cls_meth_section];
1646 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1647 return darwin_sections[objc_class_names_section];
1648 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1649 return darwin_sections[objc_meth_var_names_section];
1650 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1651 return darwin_sections[objc_meth_var_types_section];
1652 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1653 return darwin_sections[objc_cls_refs_section];
1654 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1655 return darwin_sections[objc_class_section];
1656 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1657 return darwin_sections[objc_meta_class_section];
1658 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1659 return darwin_sections[objc_category_section];
1660 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1661 return darwin_sections[objc_selector_refs_section];
1662 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1663 return darwin_sections[objc_selector_fixup_section];
1664 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1665 return darwin_sections[objc_symbols_section];
1666 else if (!strncmp (name, "_OBJC_MODULES", 13))
1667 return darwin_sections[objc_module_info_section];
1668 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1669 return darwin_sections[objc_image_info_section];
1670 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1671 return darwin_sections[objc_cat_inst_meth_section];
1672 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1673 return darwin_sections[objc_cat_cls_meth_section];
1674 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1675 return darwin_sections[objc_cat_cls_meth_section];
1676 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1677 return darwin_sections[objc_protocol_section];
1678 else
1679 return base_section;
1682 return base_section;
1685 /* This can be called with address expressions as "rtx".
1686 They must go in "const". */
1688 section *
1689 machopic_select_rtx_section (enum machine_mode mode, rtx x,
1690 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1692 if (GET_MODE_SIZE (mode) == 8
1693 && (GET_CODE (x) == CONST_INT
1694 || GET_CODE (x) == CONST_DOUBLE))
1695 return darwin_sections[literal8_section];
1696 else if (GET_MODE_SIZE (mode) == 4
1697 && (GET_CODE (x) == CONST_INT
1698 || GET_CODE (x) == CONST_DOUBLE))
1699 return darwin_sections[literal4_section];
1700 else if (HAVE_GAS_LITERAL16
1701 && TARGET_64BIT
1702 && GET_MODE_SIZE (mode) == 16
1703 && (GET_CODE (x) == CONST_INT
1704 || GET_CODE (x) == CONST_DOUBLE
1705 || GET_CODE (x) == CONST_VECTOR))
1706 return darwin_sections[literal16_section];
1707 else if (MACHOPIC_INDIRECT
1708 && (GET_CODE (x) == SYMBOL_REF
1709 || GET_CODE (x) == CONST
1710 || GET_CODE (x) == LABEL_REF))
1711 return darwin_sections[const_data_section];
1712 else
1713 return darwin_sections[const_section];
1716 void
1717 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1719 ctor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
1721 vec_safe_push (ctors, new_elt);
1723 if (! MACHOPIC_INDIRECT)
1724 fprintf (asm_out_file, ".reference .constructors_used\n");
1727 static int
1728 sort_ctor_records (const void * a, const void * b)
1730 const ctor_record *ca = (const ctor_record *)a;
1731 const ctor_record *cb = (const ctor_record *)b;
1732 if (ca->priority > cb->priority)
1733 return 1;
1734 if (ca->priority < cb->priority)
1735 return -1;
1736 if (ca->position > cb->position)
1737 return 1;
1738 if (ca->position < cb->position)
1739 return -1;
1740 return 0;
1743 static void
1744 finalize_ctors()
1746 unsigned int i;
1747 ctor_record *elt;
1749 if (MACHOPIC_INDIRECT)
1750 switch_to_section (darwin_sections[mod_init_section]);
1751 else
1752 switch_to_section (darwin_sections[constructor_section]);
1754 if (vec_safe_length (ctors) > 1)
1755 ctors->qsort (sort_ctor_records);
1756 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
1758 assemble_align (POINTER_SIZE);
1759 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1763 void
1764 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1766 if (MACHOPIC_INDIRECT)
1767 switch_to_section (darwin_sections[mod_term_section]);
1768 else
1769 switch_to_section (darwin_sections[destructor_section]);
1770 assemble_align (POINTER_SIZE);
1771 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1773 if (! MACHOPIC_INDIRECT)
1774 fprintf (asm_out_file, ".reference .destructors_used\n");
1777 void
1778 darwin_globalize_label (FILE *stream, const char *name)
1780 if (!!strncmp (name, "_OBJC_", 6))
1781 default_globalize_label (stream, name);
1784 /* This routine returns non-zero if 'name' starts with the special objective-c
1785 anonymous file-scope static name. It accommodates c++'s mangling of such
1786 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1788 int
1789 darwin_label_is_anonymous_local_objc_name (const char *name)
1791 const unsigned char *p = (const unsigned char *) name;
1792 if (*p != '_')
1793 return 0;
1794 if (p[1] == 'Z' && p[2] == 'L')
1796 p += 3;
1797 while (*p >= '0' && *p <= '9')
1798 p++;
1800 return (!strncmp ((const char *)p, "_OBJC_", 6));
1803 /* LTO support for Mach-O.
1805 This version uses three mach-o sections to encapsulate the (unlimited
1806 number of) lto sections.
1808 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1809 __GNU_LTO, __section_names contains the GNU LTO section names.
1810 __GNU_LTO, __section_index contains an array of values that index these.
1812 Indexed thus:
1813 <section offset from the start of __GNU_LTO, __lto_sections>,
1814 <section length>
1815 <name offset from the start of __GNU_LTO, __section_names,
1816 <name length>.
1818 At present, for both m32 and m64 mach-o files each of these fields is
1819 represented by a uint32_t. This is because, AFAICT, a mach-o object
1820 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1822 uint32_t offset;
1823 "offset An integer specifying the offset to this section in the file." */
1825 /* Count lto section numbers. */
1826 static unsigned int lto_section_num = 0;
1828 /* A vector of information about LTO sections, at present, we only have
1829 the name. TODO: see if we can get the data length somehow. */
1830 typedef struct GTY (()) darwin_lto_section_e {
1831 const char *sectname;
1832 } darwin_lto_section_e ;
1834 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1836 /* Segment for LTO data. */
1837 #define LTO_SEGMENT_NAME "__GNU_LTO"
1839 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1840 sections into three Mach-O ones).
1841 NOTE: These names MUST be kept in sync with those in
1842 libiberty/simple-object-mach-o. */
1843 #define LTO_SECTS_SECTION "__wrapper_sects"
1844 #define LTO_NAMES_SECTION "__wrapper_names"
1845 #define LTO_INDEX_SECTION "__wrapper_index"
1847 /* File to temporarily store LTO data. This is appended to asm_out_file
1848 in darwin_end_file. */
1849 static FILE *lto_asm_out_file, *saved_asm_out_file;
1850 static char *lto_asm_out_name;
1852 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1853 asm_out_file and switching to an alternative output file. */
1854 void
1855 darwin_asm_lto_start (void)
1857 gcc_assert (! saved_asm_out_file);
1858 saved_asm_out_file = asm_out_file;
1859 if (! lto_asm_out_name)
1860 lto_asm_out_name = make_temp_file (".lto.s");
1861 lto_asm_out_file = fopen (lto_asm_out_name, "a");
1862 if (lto_asm_out_file == NULL)
1863 fatal_error ("failed to open temporary file %s for LTO output",
1864 lto_asm_out_name);
1865 asm_out_file = lto_asm_out_file;
1868 /* Restore asm_out_file. */
1869 void
1870 darwin_asm_lto_end (void)
1872 gcc_assert (saved_asm_out_file);
1873 fclose (lto_asm_out_file);
1874 asm_out_file = saved_asm_out_file;
1875 saved_asm_out_file = NULL;
1878 static void
1879 darwin_asm_dwarf_section (const char *name, unsigned int flags, tree decl);
1881 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
1883 void
1884 darwin_asm_named_section (const char *name,
1885 unsigned int flags,
1886 tree decl ATTRIBUTE_UNUSED)
1888 /* LTO sections go in a special section that encapsulates the (unlimited)
1889 number of GNU LTO sections within a single mach-o one. */
1890 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1891 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1893 darwin_lto_section_e e;
1894 /* We expect certain flags to be set... */
1895 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1896 == (SECTION_DEBUG | SECTION_NAMED));
1898 /* Switch to our combined section. */
1899 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1900 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1901 /* Output a label for the start of this sub-section. */
1902 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1903 lto_section_num, name);
1904 /* We have to jump through hoops to get the values of the intra-section
1905 offsets... */
1906 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1907 lto_section_num, lto_section_num);
1908 fprintf (asm_out_file,
1909 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1910 lto_section_num, lto_section_num+1, lto_section_num);
1911 lto_section_num++;
1912 e.sectname = xstrdup (name);
1913 /* Keep the names, we'll need to make a table later.
1914 TODO: check that we do not revisit sections, that would break
1915 the assumption of how this is done. */
1916 if (lto_section_names == NULL)
1917 vec_alloc (lto_section_names, 16);
1918 vec_safe_push (lto_section_names, e);
1920 else if (strncmp (name, "__DWARF,", 8) == 0)
1921 darwin_asm_dwarf_section (name, flags, decl);
1922 else
1923 fprintf (asm_out_file, "\t.section %s\n", name);
1926 void
1927 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
1929 /* Darwin does not use unique sections. */
1932 /* Handle __attribute__ ((apple_kext_compatibility)).
1933 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
1934 vtable for classes with this attribute (and their descendants) by not
1935 outputting the new 3.0 nondeleting destructor. This means that such
1936 objects CANNOT be allocated on the stack or as globals UNLESS they have
1937 a completely empty `operator delete'.
1938 Luckily, this fits in with the Darwin kext model.
1940 This attribute also disables gcc3's potential overlaying of derived
1941 class data members on the padding at the end of the base class. */
1943 tree
1944 darwin_handle_kext_attribute (tree *node, tree name,
1945 tree args ATTRIBUTE_UNUSED,
1946 int flags ATTRIBUTE_UNUSED,
1947 bool *no_add_attrs)
1949 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
1950 if (! TARGET_KEXTABI)
1952 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
1953 "only when compiling a kext", name);
1955 *no_add_attrs = true;
1957 else if (TREE_CODE (*node) != RECORD_TYPE)
1959 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
1960 "only to C++ classes", name);
1962 *no_add_attrs = true;
1965 return NULL_TREE;
1968 /* Handle a "weak_import" attribute; arguments as in
1969 struct attribute_spec.handler. */
1971 tree
1972 darwin_handle_weak_import_attribute (tree *node, tree name,
1973 tree ARG_UNUSED (args),
1974 int ARG_UNUSED (flags),
1975 bool * no_add_attrs)
1977 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
1979 warning (OPT_Wattributes, "%qE attribute ignored",
1980 name);
1981 *no_add_attrs = true;
1983 else
1984 declare_weak (*node);
1986 return NULL_TREE;
1989 /* Emit a label for an FDE, making it global and/or weak if appropriate.
1990 The third parameter is nonzero if this is for exception handling.
1991 The fourth parameter is nonzero if this is just a placeholder for an
1992 FDE that we are omitting. */
1994 void
1995 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
1997 char *lab ;
1998 char buf[32];
1999 static int invok_count = 0;
2000 static tree last_fun_decl = NULL_TREE;
2002 /* We use the linker to emit the .eh labels for Darwin 9 and above. */
2003 if (! for_eh || generating_for_darwin_version >= 9)
2004 return;
2006 /* FIXME: This only works when the eh for all sections of a function is
2007 emitted at the same time. If that changes, we would need to use a lookup
2008 table of some form to determine what to do. Also, we should emit the
2009 unadorned label for the partition containing the public label for a
2010 function. This is of limited use, probably, since we do not currently
2011 enable partitioning. */
2012 strcpy (buf, ".eh");
2013 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
2015 if (decl == last_fun_decl)
2017 invok_count++;
2018 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
2020 else
2022 last_fun_decl = decl;
2023 invok_count = 0;
2027 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
2029 if (TREE_PUBLIC (decl))
2031 targetm.asm_out.globalize_label (file, lab);
2032 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
2034 fputs ("\t.private_extern ", file);
2035 assemble_name (file, lab);
2036 fputc ('\n', file);
2040 if (DECL_WEAK (decl))
2042 fputs ("\t.weak_definition ", file);
2043 assemble_name (file, lab);
2044 fputc ('\n', file);
2047 assemble_name (file, lab);
2048 if (empty)
2050 fputs (" = 0\n", file);
2052 /* Mark the absolute .eh and .eh1 style labels as needed to
2053 ensure that we don't dead code strip them and keep such
2054 labels from another instantiation point until we can fix this
2055 properly with group comdat support. */
2056 darwin_mark_decl_preserved (lab);
2058 else
2059 fputs (":\n", file);
2061 free (lab);
2064 static GTY(()) unsigned long except_table_label_num;
2066 void
2067 darwin_emit_except_table_label (FILE *file)
2069 char section_start_label[30];
2071 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2072 except_table_label_num++);
2073 ASM_OUTPUT_LABEL (file, section_start_label);
2075 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2077 void
2078 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2080 const char *nlp_name;
2082 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2084 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2085 fputs ("\t.long\t", file);
2086 ASM_OUTPUT_LABELREF (file, nlp_name);
2087 fputs ("-.", file);
2090 /* If this is uncommented, details of each allocation will be printed
2091 in the asm right before the actual code. WARNING - this will cause some
2092 test-suite fails (since the printout will contain items that some tests
2093 are not expecting) -- so don't leave it on by default (it bloats the
2094 asm too). */
2095 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2097 /* The first two of these routines are ostensibly just intended to put
2098 names into the asm. However, they are both hijacked in order to ensure
2099 that zero-sized items do not make their way into the output. Consequently,
2100 we also need to make these participate in provisions for dealing with
2101 such items in section anchors. */
2103 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2104 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2105 but it does need to be referenced via indirect PIC data pointers.
2106 The machopic_define_symbol calls are telling the machopic subsystem
2107 that the name *is* defined in this module, so it doesn't need to
2108 make them indirect. */
2109 void
2110 darwin_asm_declare_object_name (FILE *file,
2111 const char *nam, tree decl)
2113 const char *xname = nam;
2114 unsigned HOST_WIDE_INT size;
2115 bool local_def, weak;
2117 weak = (DECL_P (decl)
2118 && DECL_WEAK (decl)
2119 && !lookup_attribute ("weak_import",
2120 DECL_ATTRIBUTES (decl)));
2122 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2123 && (!DECL_COMMON (decl)
2124 || !TREE_PUBLIC (decl)));
2126 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2127 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2129 if (local_def)
2131 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2132 if (!weak)
2133 machopic_define_symbol (DECL_RTL (decl));
2136 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
2138 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2139 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2140 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2141 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2142 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2143 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2144 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2145 (unsigned long)DECL_INITIAL (decl));
2146 #endif
2148 /* Darwin needs help to support local zero-sized objects.
2149 They must be made at least one byte, and the section containing must be
2150 marked as unsuitable for section-anchors (see storage allocators below).
2152 For non-zero objects this output is handled by varasm.c.
2154 if (!size)
2156 unsigned int l2align = 0;
2158 /* The align must be honored, even for zero-sized. */
2159 if (DECL_ALIGN (decl))
2161 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2162 fprintf (file, "\t.align\t%u\n", l2align);
2165 ASM_OUTPUT_LABEL (file, xname);
2166 size = 1;
2167 fprintf (file, "\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2169 /* Check that we've correctly picked up the zero-sized item and placed it
2170 properly. */
2171 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2172 || (in_section
2173 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2175 else
2176 ASM_OUTPUT_LABEL (file, xname);
2179 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2180 void
2181 darwin_asm_declare_constant_name (FILE *file, const char *name,
2182 const_tree exp ATTRIBUTE_UNUSED,
2183 HOST_WIDE_INT size)
2185 assemble_label (file, name);
2186 /* As for other items, we need at least one byte. */
2187 if (!size)
2189 fputs ("\t.space\t1\n", file);
2190 /* Check that we've correctly picked up the zero-sized item and placed it
2191 properly. */
2192 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2193 || (in_section
2194 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2198 /* Darwin storage allocators.
2200 Zerofill sections are desirable for large blank data since, otherwise, these
2201 data bloat objects (PR33210).
2203 However, section anchors don't work in .zerofill sections (one cannot switch
2204 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2205 to put (at least some) data into 'normal' switchable sections.
2207 Here we set a relatively arbitrary value for the size of an object to trigger
2208 zerofill when section anchors are enabled (anything bigger than a page for
2209 current Darwin implementations). FIXME: there ought to be some objective way
2210 to make this choice.
2212 When section anchor are off this is ignored anyway. */
2214 #define BYTES_ZFILL 4096
2216 /* Emit a chunk of data for items coalesced by the linker. */
2217 static void
2218 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2219 unsigned HOST_WIDE_INT size,
2220 unsigned int align)
2222 /* Since the sections used here are coalesed, they will not be eligible
2223 for section anchors, and therefore we don't need to break that out. */
2224 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2225 switch_to_section (darwin_sections[const_data_coal_section]);
2226 else
2227 switch_to_section (darwin_sections[data_coal_section]);
2229 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2230 the align info for zero-sized items... but do it here otherwise. */
2231 if (size && align)
2232 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2234 if (TREE_PUBLIC (decl))
2235 darwin_globalize_label (fp, name);
2237 /* ... and we let it deal with outputting one byte of zero for them too. */
2238 darwin_asm_declare_object_name (fp, name, decl);
2239 if (size)
2240 assemble_zeros (size);
2243 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2244 static void
2245 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2246 unsigned HOST_WIDE_INT size,
2247 unsigned int align, tree meta)
2249 section *ocs = data_section;
2251 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2252 ocs = darwin_objc2_section (decl, meta, ocs);
2253 else
2254 ocs = darwin_objc1_section (decl, meta, ocs);
2256 switch_to_section (ocs);
2258 /* We shall declare that zero-sized meta-data are not valid (yet). */
2259 gcc_assert (size);
2260 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2262 /* ... and we let it deal with outputting one byte of zero for them too. */
2263 darwin_asm_declare_object_name (fp, name, decl);
2264 assemble_zeros (size);
2267 /* This routine emits 'local' storage:
2269 When Section Anchors are off this routine emits .zerofill commands in
2270 sections named for their alignment.
2272 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2273 the .static_data section so that the section anchoring system can see them.
2274 Larger items are still placed in .zerofill sections, addressing PR33210.
2275 The routine has no checking - it is all assumed to be done by the caller.
2277 static void
2278 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2279 unsigned HOST_WIDE_INT size,
2280 unsigned int l2align)
2282 /* FIXME: We have a fudge to make this work with Java even when the target does
2283 not use sections anchors -- Java seems to need at least one small item in a
2284 non-zerofill segment. */
2285 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2286 || (size && size <= 2))
2288 /* Put smaller objects in _static_data, where the section anchors system
2289 can get them.
2290 However, if they are zero-sized punt them to yet a different section
2291 (that is not allowed to participate in anchoring). */
2292 if (!size)
2294 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2295 in_section = darwin_sections[zobj_bss_section];
2296 size = 1;
2298 else
2300 fputs ("\t.static_data\n", fp);
2301 in_section = darwin_sections[static_data_section];
2304 if (l2align)
2305 fprintf (fp, "\t.align\t%u\n", l2align);
2307 assemble_name (fp, name);
2308 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2310 else
2312 /* When we are on a non-section anchor target, we can get zero-sized
2313 items here. However, all we need to do is to bump them to one byte
2314 and the section alignment will take care of the rest. */
2315 char secnam[64];
2316 unsigned int flags ;
2317 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
2318 (unsigned) l2align);
2319 /* We can't anchor (yet, if ever) in zerofill sections, because we can't
2320 switch to them and emit a label. */
2321 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2322 in_section = get_section (secnam, flags, NULL);
2323 fprintf (fp, "\t.zerofill %s,", secnam);
2324 assemble_name (fp, name);
2325 if (!size)
2326 size = 1;
2328 if (l2align)
2329 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2330 size, (unsigned) l2align);
2331 else
2332 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2335 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2336 /* This is defined as a file-scope var, so we know to notify machopic. */
2337 machopic_define_symbol (DECL_RTL (decl));
2340 /* Emit a chunk of common. */
2341 static void
2342 darwin_emit_common (FILE *fp, const char *name,
2343 unsigned HOST_WIDE_INT size, unsigned int align)
2345 unsigned HOST_WIDE_INT rounded;
2346 unsigned int l2align;
2348 /* Earlier systems complain if the alignment exceeds the page size.
2349 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2350 if (!emit_aligned_common && (align > 32768UL))
2351 align = 4096UL; /* In units. */
2352 else
2353 align /= BITS_PER_UNIT;
2355 /* Make sure we have a meaningful align. */
2356 if (!align)
2357 align = 1;
2359 /* For earlier toolchains, we need to emit the var as a rounded size to
2360 tell ld the alignment. */
2361 if (size < align)
2362 rounded = align;
2363 else
2364 rounded = (size + (align-1)) & ~(align-1);
2366 l2align = floor_log2 (align);
2367 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2369 in_section = comm_section;
2370 /* We mustn't allow multiple public symbols to share an address when using
2371 the normal OSX toolchain. */
2372 if (!size)
2374 /* Put at least one byte. */
2375 size = 1;
2376 /* This section can no longer participate in section anchoring. */
2377 comm_section->common.flags |= SECTION_NO_ANCHOR;
2380 fputs ("\t.comm\t", fp);
2381 assemble_name (fp, name);
2382 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2383 emit_aligned_common?size:rounded);
2384 if (l2align && emit_aligned_common)
2385 fprintf (fp, ",%u", l2align);
2386 fputs ("\n", fp);
2389 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2390 or coalescable data sections (for weak or comdat) as appropriate. */
2392 void
2393 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2394 unsigned HOST_WIDE_INT size, unsigned int align)
2396 unsigned int l2align;
2397 bool one, pub, weak;
2398 tree meta;
2400 pub = TREE_PUBLIC (decl);
2401 one = DECL_ONE_ONLY (decl);
2402 weak = (DECL_P (decl)
2403 && DECL_WEAK (decl)
2404 && !lookup_attribute ("weak_import",
2405 DECL_ATTRIBUTES (decl)));
2407 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2408 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2409 " pub %d weak %d one %d init %lx\n",
2410 name, (long long)size, (int)align, TREE_READONLY (decl),
2411 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2412 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2413 #endif
2415 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2416 before the target has a chance to comment. */
2417 if ((meta = is_objc_metadata (decl)))
2419 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2420 return;
2423 /* Check that any initializer is valid. */
2424 gcc_assert ((DECL_INITIAL (decl) == NULL)
2425 || (DECL_INITIAL (decl) == error_mark_node)
2426 || initializer_zerop (DECL_INITIAL (decl)));
2428 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2429 gcc_assert (!DECL_COMMON (decl));
2431 /* Pick up the correct alignment. */
2432 if (!size || !align)
2433 align = DECL_ALIGN (decl);
2435 l2align = floor_log2 (align / BITS_PER_UNIT);
2436 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2438 last_assemble_variable_decl = decl;
2440 /* We would rather not have to check this here - but it seems that we might
2441 be passed a decl that should be in coalesced space. */
2442 if (one || weak)
2444 /* Weak or COMDAT objects are put in mergeable sections. */
2445 darwin_emit_weak_or_comdat (fp, decl, name, size,
2446 DECL_ALIGN (decl));
2447 return;
2450 /* If this is not public, then emit according to local rules. */
2451 if (!pub)
2453 darwin_emit_local_bss (fp, decl, name, size, l2align);
2454 return;
2457 /* So we have a public symbol (small item fudge for Java, see above). */
2458 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2459 || (size && size <= 2))
2461 /* Put smaller objects in data, where the section anchors system can get
2462 them. However, if they are zero-sized punt them to yet a different
2463 section (that is not allowed to participate in anchoring). */
2464 if (!size)
2466 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2467 in_section = darwin_sections[zobj_data_section];
2468 size = 1;
2470 else
2472 fputs ("\t.data\n", fp);
2473 in_section = data_section;
2476 if (l2align)
2477 fprintf (fp, "\t.align\t%u\n", l2align);
2479 assemble_name (fp, name);
2480 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2482 else
2484 char secnam[64];
2485 unsigned int flags ;
2486 /* When we are on a non-section anchor target, we can get zero-sized
2487 items here. However, all we need to do is to bump them to one byte
2488 and the section alignment will take care of the rest. */
2489 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
2491 /* We can't anchor in zerofill sections, because we can't switch
2492 to them and emit a label. */
2493 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2494 in_section = get_section (secnam, flags, NULL);
2495 fprintf (fp, "\t.zerofill %s,", secnam);
2496 assemble_name (fp, name);
2497 if (!size)
2498 size = 1;
2500 if (l2align)
2501 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2502 else
2503 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2505 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2508 /* Output a chunk of common, with alignment specified (where the target
2509 supports this). */
2510 void
2511 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2512 unsigned HOST_WIDE_INT size,
2513 unsigned int align)
2515 unsigned int l2align;
2516 bool one, weak;
2517 tree meta;
2519 /* No corresponding var. */
2520 if (decl==NULL)
2522 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2523 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2524 #endif
2525 darwin_emit_common (fp, name, size, align);
2526 return;
2529 one = DECL_ONE_ONLY (decl);
2530 weak = (DECL_P (decl)
2531 && DECL_WEAK (decl)
2532 && !lookup_attribute ("weak_import",
2533 DECL_ATTRIBUTES (decl)));
2535 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2536 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2537 " weak %d one %d init %lx\n",
2538 name, (long long)size, (int)align, TREE_READONLY (decl),
2539 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2540 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2541 #endif
2543 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2544 before the target has a chance to comment. */
2545 if ((meta = is_objc_metadata (decl)))
2547 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2548 return;
2551 /* We shouldn't be messing with this if the decl has a section name. */
2552 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2554 /* We would rather not have to check this here - but it seems that we might
2555 be passed a decl that should be in coalesced space. */
2556 if (one || weak)
2558 /* Weak or COMDAT objects are put in mergable sections. */
2559 darwin_emit_weak_or_comdat (fp, decl, name, size,
2560 DECL_ALIGN (decl));
2561 return;
2564 /* We should only get here for DECL_COMMON, with a zero init (and, in
2565 principle, only for public symbols too - although we deal with local
2566 ones below). */
2568 /* Check the initializer is OK. */
2569 gcc_assert (DECL_COMMON (decl)
2570 && ((DECL_INITIAL (decl) == NULL)
2571 || (DECL_INITIAL (decl) == error_mark_node)
2572 || initializer_zerop (DECL_INITIAL (decl))));
2574 last_assemble_variable_decl = decl;
2576 if (!size || !align)
2577 align = DECL_ALIGN (decl);
2579 l2align = floor_log2 (align / BITS_PER_UNIT);
2580 /* Check we aren't asking for more aligment than the platform allows. */
2581 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2583 if (TREE_PUBLIC (decl) != 0)
2584 darwin_emit_common (fp, name, size, align);
2585 else
2586 darwin_emit_local_bss (fp, decl, name, size, l2align);
2589 /* Output a chunk of BSS with alignment specfied. */
2590 void
2591 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2592 unsigned HOST_WIDE_INT size,
2593 unsigned int align)
2595 unsigned long l2align;
2596 bool one, weak;
2597 tree meta;
2599 one = DECL_ONE_ONLY (decl);
2600 weak = (DECL_P (decl)
2601 && DECL_WEAK (decl)
2602 && !lookup_attribute ("weak_import",
2603 DECL_ATTRIBUTES (decl)));
2605 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2606 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2607 " weak %d init %lx\n",
2608 name, (long long)size, (int)align, TREE_READONLY (decl),
2609 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2610 weak , (unsigned long)DECL_INITIAL (decl));
2611 #endif
2613 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2614 before the target has a chance to comment. */
2615 if ((meta = is_objc_metadata (decl)))
2617 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2618 return;
2621 /* We shouldn't be messing with this if the decl has a section name. */
2622 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2624 /* We would rather not have to check this here - but it seems that we might
2625 be passed a decl that should be in coalesced space. */
2626 if (one || weak)
2628 /* Weak or COMDAT objects are put in mergable sections. */
2629 darwin_emit_weak_or_comdat (fp, decl, name, size,
2630 DECL_ALIGN (decl));
2631 return;
2634 /* .. and it should be suitable for placement in local mem. */
2635 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2636 /* .. and any initializer must be all-zero. */
2637 gcc_assert ((DECL_INITIAL (decl) == NULL)
2638 || (DECL_INITIAL (decl) == error_mark_node)
2639 || initializer_zerop (DECL_INITIAL (decl)));
2641 last_assemble_variable_decl = decl;
2643 if (!size || !align)
2644 align = DECL_ALIGN (decl);
2646 l2align = floor_log2 (align / BITS_PER_UNIT);
2647 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2649 darwin_emit_local_bss (fp, decl, name, size, l2align);
2652 /* Emit an assembler directive to set visibility for a symbol. The
2653 only supported visibilities are VISIBILITY_DEFAULT and
2654 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2655 extern". There is no MACH-O equivalent of ELF's
2656 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2658 void
2659 darwin_assemble_visibility (tree decl, int vis)
2661 if (vis == VISIBILITY_DEFAULT)
2663 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2665 fputs ("\t.private_extern ", asm_out_file);
2666 assemble_name (asm_out_file,
2667 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2668 fputs ("\n", asm_out_file);
2670 else
2671 warning (OPT_Wattributes, "protected visibility attribute "
2672 "not supported in this configuration; ignored");
2675 /* vec used by darwin_asm_dwarf_section.
2676 Maybe a hash tab would be better here - but the intention is that this is
2677 a very short list (fewer than 16 items) and each entry should (ideally,
2678 eventually) only be presented once.
2680 A structure to hold a dwarf debug section used entry. */
2682 typedef struct GTY(()) dwarf_sect_used_entry {
2683 const char *name;
2684 unsigned count;
2686 dwarf_sect_used_entry;
2689 /* A list of used __DWARF sections. */
2690 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2692 /* This is called when we are asked to assemble a named section and the
2693 name begins with __DWARF,. We keep a list of the section names (without
2694 the __DWARF, prefix) and use this to emit our required start label on the
2695 first switch to each section. */
2697 static void
2698 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2699 tree ARG_UNUSED (decl))
2701 unsigned i;
2702 int namelen;
2703 const char * sname;
2704 dwarf_sect_used_entry *ref;
2705 bool found = false;
2706 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2707 == (SECTION_DEBUG | SECTION_NAMED));
2708 /* We know that the name starts with __DWARF, */
2709 sname = name + 8;
2710 namelen = strchr (sname, ',') - sname;
2711 gcc_assert (namelen);
2712 if (dwarf_sect_names_table == NULL)
2713 vec_alloc (dwarf_sect_names_table, 16);
2714 else
2715 for (i = 0;
2716 dwarf_sect_names_table->iterate (i, &ref);
2717 i++)
2719 if (!ref)
2720 break;
2721 if (!strcmp (ref->name, sname))
2723 found = true;
2724 ref->count++;
2725 break;
2729 fprintf (asm_out_file, "\t.section %s\n", name);
2730 if (!found)
2732 dwarf_sect_used_entry e;
2733 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2734 e.count = 1;
2735 e.name = xstrdup (sname);
2736 vec_safe_push (dwarf_sect_names_table, e);
2740 /* Output a difference of two labels that will be an assembly time
2741 constant if the two labels are local. (.long lab1-lab2 will be
2742 very different if lab1 is at the boundary between two sections; it
2743 will be relocated according to the second section, not the first,
2744 so one ends up with a difference between labels in different
2745 sections, which is bad in the dwarf2 eh context for instance.) */
2747 static int darwin_dwarf_label_counter;
2749 void
2750 darwin_asm_output_dwarf_delta (FILE *file, int size,
2751 const char *lab1, const char *lab2)
2753 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2754 && lab2[0] == '*' && lab2[1] == 'L');
2755 const char *directive = (size == 8 ? ".quad" : ".long");
2757 if (islocaldiff)
2758 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2759 else
2760 fprintf (file, "\t%s\t", directive);
2762 assemble_name_raw (file, lab1);
2763 fprintf (file, "-");
2764 assemble_name_raw (file, lab2);
2765 if (islocaldiff)
2766 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2769 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2770 offsets are not represented using relocs in .o files; either the
2771 section never leaves the .o file, or the linker or other tool is
2772 responsible for parsing the DWARF and updating the offsets. */
2774 void
2775 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2776 section *base)
2778 char sname[64];
2779 int namelen;
2781 gcc_assert (base->common.flags & SECTION_NAMED);
2782 gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
2783 gcc_assert (strchr (base->named.name + 8, ','));
2785 namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
2786 sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
2787 darwin_asm_output_dwarf_delta (file, size, lab, sname);
2790 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2792 void
2793 darwin_file_start (void)
2795 /* Nothing to do. */
2798 /* Called for the TARGET_ASM_FILE_END hook.
2799 Emit the mach-o pic indirection data, the lto data and, finally a flag
2800 to tell the linker that it can break the file object into sections and
2801 move those around for efficiency. */
2803 void
2804 darwin_file_end (void)
2806 if (!vec_safe_is_empty (ctors))
2807 finalize_ctors();
2808 machopic_finish (asm_out_file);
2809 if (strcmp (lang_hooks.name, "GNU C++") == 0)
2811 switch_to_section (darwin_sections[constructor_section]);
2812 switch_to_section (darwin_sections[destructor_section]);
2813 ASM_OUTPUT_ALIGN (asm_out_file, 1);
2816 /* If there was LTO assembler output, append it to asm_out_file. */
2817 if (lto_asm_out_name)
2819 int n;
2820 char *buf, *lto_asm_txt;
2822 /* Shouldn't be here if we failed to switch back. */
2823 gcc_assert (! saved_asm_out_file);
2825 lto_asm_out_file = fopen (lto_asm_out_name, "r");
2826 if (lto_asm_out_file == NULL)
2827 fatal_error ("failed to open temporary file %s with LTO output",
2828 lto_asm_out_name);
2829 fseek (lto_asm_out_file, 0, SEEK_END);
2830 n = ftell (lto_asm_out_file);
2831 if (n > 0)
2833 fseek (lto_asm_out_file, 0, SEEK_SET);
2834 lto_asm_txt = buf = (char *) xmalloc (n + 1);
2835 while (fgets (lto_asm_txt, n, lto_asm_out_file))
2836 fputs (lto_asm_txt, asm_out_file);
2837 /* Put a termination label. */
2838 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2839 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2840 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2841 lto_section_num);
2842 /* Make sure our termination label stays in this section. */
2843 fputs ("\t.space\t1\n", asm_out_file);
2846 /* Remove the temporary file. */
2847 fclose (lto_asm_out_file);
2848 unlink_if_ordinary (lto_asm_out_name);
2849 free (lto_asm_out_name);
2852 /* Output the names and indices. */
2853 if (lto_section_names && lto_section_names->length ())
2855 int count;
2856 darwin_lto_section_e *ref;
2857 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
2858 the latter up ourselves. */
2859 const char *op = integer_asm_op (4,0);
2861 /* Emit the names. */
2862 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2863 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2864 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2866 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
2867 /* We have to jump through hoops to get the values of the intra-section
2868 offsets... */
2869 fprintf (asm_out_file,
2870 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
2871 count, count);
2872 fprintf (asm_out_file,
2873 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
2874 count, count+1, count);
2875 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
2877 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
2878 /* make sure our termination label stays in this section. */
2879 fputs ("\t.space\t1\n", asm_out_file);
2881 /* Emit the Index. */
2882 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2883 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
2884 fputs ("\t.align\t2\n", asm_out_file);
2885 fputs ("# Section offset, Section length, Name offset, Name length\n",
2886 asm_out_file);
2887 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2889 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
2890 op, count, ref->sectname);
2891 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
2892 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
2893 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
2897 /* If we have section anchors, then we must prevent the linker from
2898 re-arranging data. */
2899 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2900 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
2903 /* TODO: Add a language hook for identifying if a decl is a vtable. */
2904 #define DARWIN_VTABLE_P(DECL) 0
2906 /* Cross-module name binding. Darwin does not support overriding
2907 functions at dynamic-link time, except for vtables in kexts. */
2909 bool
2910 darwin_binds_local_p (const_tree decl)
2912 return default_binds_local_p_1 (decl,
2913 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
2916 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
2917 anchor relative to ".", the current section position. We cannot use
2918 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
2919 void
2920 darwin_asm_output_anchor (rtx symbol)
2922 fprintf (asm_out_file, "\t.set\t");
2923 assemble_name (asm_out_file, XSTR (symbol, 0));
2924 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
2925 SYMBOL_REF_BLOCK_OFFSET (symbol));
2928 /* Disable section anchoring on any section containing a zero-sized
2929 object. */
2930 bool
2931 darwin_use_anchors_for_symbol_p (const_rtx symbol)
2933 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
2935 section *sect;
2936 /* If the section contains a zero-sized object it's ineligible. */
2937 sect = SYMBOL_REF_BLOCK (symbol)->sect;
2938 /* This should have the effect of disabling anchors for vars that follow
2939 any zero-sized one, in a given section. */
2940 if (sect->common.flags & SECTION_NO_ANCHOR)
2941 return false;
2943 /* Also check the normal reasons for suppressing. */
2944 return default_use_anchors_for_symbol_p (symbol);
2946 else
2947 return false;
2950 /* Set the darwin specific attributes on TYPE. */
2951 void
2952 darwin_set_default_type_attributes (tree type)
2954 if (darwin_ms_struct
2955 && TREE_CODE (type) == RECORD_TYPE)
2956 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
2957 NULL_TREE,
2958 TYPE_ATTRIBUTES (type));
2961 /* True, iff we're generating code for loadable kernel extensions. */
2963 bool
2964 darwin_kextabi_p (void) {
2965 return flag_apple_kext;
2968 void
2969 darwin_override_options (void)
2971 /* Keep track of which (major) version we're generating code for. */
2972 if (darwin_macosx_version_min)
2974 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
2975 generating_for_darwin_version = 10;
2976 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
2977 generating_for_darwin_version = 9;
2979 /* Earlier versions are not specifically accounted, until required. */
2982 /* In principle, this should be c-family only. However, we really need to
2983 set sensible defaults for LTO as well, since the section selection stuff
2984 should check for correctness re. the ABI. TODO: check and provide the
2985 flags (runtime & ABI) from the lto wrapper). */
2987 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
2988 if (!global_options_set.x_flag_objc_abi)
2989 global_options.x_flag_objc_abi
2990 = (!flag_next_runtime)
2992 : (TARGET_64BIT ? 2
2993 : (generating_for_darwin_version >= 9) ? 1
2994 : 0);
2996 /* Objective-C family ABI 2 is only valid for next/m64 at present. */
2997 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
2999 if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
3000 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
3001 " used for %<-m64%> targets with"
3002 " %<-fnext-runtime%>");
3003 if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
3004 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
3005 " supported on %<-m32%> targets with"
3006 " %<-fnext-runtime%>");
3009 /* Don't emit DWARF3/4 unless specifically selected. This is a
3010 workaround for tool bugs. */
3011 if (!global_options_set.x_dwarf_strict)
3012 dwarf_strict = 1;
3013 if (!global_options_set.x_dwarf_version)
3014 dwarf_version = 2;
3016 /* Do not allow unwind tables to be generated by default for m32.
3017 fnon-call-exceptions will override this, regardless of what we do. */
3018 if (generating_for_darwin_version < 10
3019 && !global_options_set.x_flag_asynchronous_unwind_tables
3020 && !TARGET_64BIT)
3021 global_options.x_flag_asynchronous_unwind_tables = 0;
3023 /* Disable -freorder-blocks-and-partition when unwind tables are being
3024 emitted for Darwin < 9 (OSX 10.5).
3025 The strategy is, "Unless the User has specifically set/unset an unwind
3026 flag we will switch off -freorder-blocks-and-partition when unwind tables
3027 will be generated". If the User specifically sets flags... we assume
3028 (s)he knows why... */
3029 if (generating_for_darwin_version < 9
3030 && global_options_set.x_flag_reorder_blocks_and_partition
3031 && ((global_options.x_flag_exceptions /* User, c++, java */
3032 && !global_options_set.x_flag_exceptions) /* User specified... */
3033 || (global_options.x_flag_unwind_tables
3034 && !global_options_set.x_flag_unwind_tables)
3035 || (global_options.x_flag_non_call_exceptions
3036 && !global_options_set.x_flag_non_call_exceptions)
3037 || (global_options.x_flag_asynchronous_unwind_tables
3038 && !global_options_set.x_flag_asynchronous_unwind_tables)))
3040 inform (input_location,
3041 "-freorder-blocks-and-partition does not work with exceptions "
3042 "on this architecture");
3043 flag_reorder_blocks_and_partition = 0;
3044 flag_reorder_blocks = 1;
3047 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3048 one valid choice of exception scheme for each runtime. */
3049 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3050 global_options.x_flag_objc_sjlj_exceptions =
3051 flag_next_runtime && !TARGET_64BIT;
3053 /* FIXME: and this could be eliminated then too. */
3054 if (!global_options_set.x_flag_exceptions
3055 && flag_objc_exceptions
3056 && TARGET_64BIT)
3057 flag_exceptions = 1;
3059 if (flag_mkernel || flag_apple_kext)
3061 /* -mkernel implies -fapple-kext for C++ */
3062 if (strcmp (lang_hooks.name, "GNU C++") == 0)
3063 flag_apple_kext = 1;
3065 flag_no_common = 1;
3067 /* No EH in kexts. */
3068 flag_exceptions = 0;
3069 /* No -fnon-call-exceptions data in kexts. */
3070 flag_non_call_exceptions = 0;
3071 /* so no tables either.. */
3072 flag_unwind_tables = 0;
3073 flag_asynchronous_unwind_tables = 0;
3074 /* We still need to emit branch islands for kernel context. */
3075 darwin_emit_branch_islands = true;
3078 if (flag_var_tracking_uninit == 0
3079 && generating_for_darwin_version >= 9
3080 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3081 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3082 && write_symbols == DWARF2_DEBUG)
3083 flag_var_tracking_uninit = flag_var_tracking;
3085 if (MACHO_DYNAMIC_NO_PIC_P)
3087 if (flag_pic)
3088 warning_at (UNKNOWN_LOCATION, 0,
3089 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3090 " %<-fpie%> or %<-fPIE%>");
3091 flag_pic = 0;
3093 else if (flag_pic == 1)
3095 /* Darwin's -fpic is -fPIC. */
3096 flag_pic = 2;
3099 /* It is assumed that branch island stubs are needed for earlier systems. */
3100 if (generating_for_darwin_version < 9)
3101 darwin_emit_branch_islands = true;
3102 else
3103 emit_aligned_common = true; /* Later systems can support aligned common. */
3105 /* The c_dialect...() macros are not available to us here. */
3106 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3109 #if DARWIN_PPC
3110 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3112 static void
3113 darwin_patch_builtin (enum built_in_function fncode)
3115 tree fn = builtin_decl_explicit (fncode);
3116 tree sym;
3117 char *newname;
3119 if (!fn)
3120 return;
3122 sym = DECL_ASSEMBLER_NAME (fn);
3123 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3125 set_user_assembler_name (fn, newname);
3127 fn = builtin_decl_implicit (fncode);
3128 if (fn)
3129 set_user_assembler_name (fn, newname);
3132 void
3133 darwin_patch_builtins (void)
3135 if (LONG_DOUBLE_TYPE_SIZE != 128)
3136 return;
3138 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3139 #define PATCH_BUILTIN_NO64(fncode) \
3140 if (!TARGET_64BIT) \
3141 darwin_patch_builtin (fncode);
3142 #define PATCH_BUILTIN_VARIADIC(fncode) \
3143 if (!TARGET_64BIT \
3144 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3145 darwin_patch_builtin (fncode);
3146 #include "darwin-ppc-ldouble-patch.def"
3147 #undef PATCH_BUILTIN
3148 #undef PATCH_BUILTIN_NO64
3149 #undef PATCH_BUILTIN_VARIADIC
3151 #endif
3153 /* CFStrings implementation. */
3154 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3155 static GTY(()) tree cfstring_type_node = NULL_TREE;
3156 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3157 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3158 static GTY(()) tree pcint_type_node = NULL_TREE;
3159 static GTY(()) tree pcchar_type_node = NULL_TREE;
3161 static enum built_in_function darwin_builtin_cfstring;
3163 /* Store all constructed constant CFStrings in a hash table so that
3164 they get uniqued properly. */
3166 typedef struct GTY (()) cfstring_descriptor {
3167 /* The string literal. */
3168 tree literal;
3169 /* The resulting constant CFString. */
3170 tree constructor;
3171 } cfstring_descriptor;
3173 static GTY ((param_is (struct cfstring_descriptor))) htab_t cfstring_htab;
3175 static hashval_t cfstring_hash (const void *);
3176 static int cfstring_eq (const void *, const void *);
3178 static tree
3179 add_builtin_field_decl (tree type, const char *name, tree **chain)
3181 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3182 get_identifier (name), type);
3184 if (*chain != NULL)
3185 **chain = field;
3186 *chain = &DECL_CHAIN (field);
3188 return field;
3191 tree
3192 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3194 tree cfsfun, fields, pccfstring_ftype_pcchar;
3195 tree *chain = NULL;
3197 darwin_builtin_cfstring =
3198 (enum built_in_function) builtin_cfstring;
3200 /* struct __builtin_CFString {
3201 const int *isa; (will point at
3202 int flags; __CFConstantStringClassReference)
3203 const char *str;
3204 long length;
3205 }; */
3207 pcint_type_node = build_pointer_type
3208 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3210 pcchar_type_node = build_pointer_type
3211 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3213 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3215 /* Have to build backwards for finish struct. */
3216 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3217 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3218 add_builtin_field_decl (integer_type_node, "flags", &chain);
3219 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3220 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3221 fields, NULL_TREE);
3223 /* const struct __builtin_CFstring *
3224 __builtin___CFStringMakeConstantString (const char *); */
3226 ccfstring_type_node = build_qualified_type
3227 (cfstring_type_node, TYPE_QUAL_CONST);
3228 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3229 pccfstring_ftype_pcchar = build_function_type_list
3230 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3232 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3233 get_identifier ("__builtin___CFStringMakeConstantString"),
3234 pccfstring_ftype_pcchar);
3236 TREE_PUBLIC (cfsfun) = 1;
3237 DECL_EXTERNAL (cfsfun) = 1;
3238 DECL_ARTIFICIAL (cfsfun) = 1;
3239 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3240 in place of the existing, which may be NULL. */
3241 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3242 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3243 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
3244 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
3245 lang_hooks.builtin_function (cfsfun);
3247 /* extern int __CFConstantStringClassReference[]; */
3248 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3249 get_identifier ("__CFConstantStringClassReference"),
3250 build_array_type (integer_type_node, NULL_TREE));
3252 TREE_PUBLIC (cfstring_class_reference) = 1;
3253 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3254 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3255 DECL_EXTERNAL (cfstring_class_reference) = 1;
3256 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3258 /* Initialize the hash table used to hold the constant CFString objects. */
3259 cfstring_htab = htab_create_ggc (31, cfstring_hash, cfstring_eq, NULL);
3261 return cfstring_type_node;
3264 tree
3265 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3266 bool ARG_UNUSED (ignore))
3268 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3270 if (fcode == darwin_builtin_cfstring)
3272 if (!darwin_constant_cfstrings)
3274 error ("built-in function %qD requires the"
3275 " %<-mconstant-cfstrings%> flag", fndecl);
3276 return error_mark_node;
3279 if (n_args != 1)
3281 error ("built-in function %qD takes one argument only", fndecl);
3282 return error_mark_node;
3285 return darwin_build_constant_cfstring (*argp);
3288 return NULL_TREE;
3291 void
3292 darwin_rename_builtins (void)
3294 /* The system ___divdc3 routine in libSystem on darwin10 is not
3295 accurate to 1ulp, ours is, so we avoid ever using the system name
3296 for this routine and instead install a non-conflicting name that
3297 is accurate.
3299 When -ffast-math or -funsafe-math-optimizations is given, we can
3300 use the faster version. */
3301 if (!flag_unsafe_math_optimizations)
3303 enum built_in_function dcode
3304 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3305 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3306 tree fn = builtin_decl_explicit (dcode);
3307 /* Fortran and c call TARGET_INIT_BUILTINS and
3308 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3309 call into each to ensure that at least one of them is called
3310 after build_common_builtin_nodes. A better fix is to add a
3311 new hook to run after build_common_builtin_nodes runs. */
3312 if (fn)
3313 set_user_assembler_name (fn, "___ieee_divdc3");
3314 fn = builtin_decl_implicit (dcode);
3315 if (fn)
3316 set_user_assembler_name (fn, "___ieee_divdc3");
3320 static hashval_t
3321 cfstring_hash (const void *ptr)
3323 tree str = ((const struct cfstring_descriptor *)ptr)->literal;
3324 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3325 int i, len = TREE_STRING_LENGTH (str);
3326 hashval_t h = len;
3328 for (i = 0; i < len; i++)
3329 h = ((h * 613) + p[i]);
3331 return h;
3334 static int
3335 cfstring_eq (const void *ptr1, const void *ptr2)
3337 tree str1 = ((const struct cfstring_descriptor *)ptr1)->literal;
3338 tree str2 = ((const struct cfstring_descriptor *)ptr2)->literal;
3339 int len1 = TREE_STRING_LENGTH (str1);
3341 return (len1 == TREE_STRING_LENGTH (str2)
3342 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3343 len1));
3346 tree
3347 darwin_build_constant_cfstring (tree str)
3349 struct cfstring_descriptor *desc, key;
3350 void **loc;
3351 tree addr;
3353 if (!str)
3355 error ("CFString literal is missing");
3356 return error_mark_node;
3359 STRIP_NOPS (str);
3361 if (TREE_CODE (str) == ADDR_EXPR)
3362 str = TREE_OPERAND (str, 0);
3364 if (TREE_CODE (str) != STRING_CST)
3366 error ("CFString literal expression is not a string constant");
3367 return error_mark_node;
3370 /* Perhaps we already constructed a constant CFString just like this one? */
3371 key.literal = str;
3372 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3373 desc = (struct cfstring_descriptor *) *loc;
3375 if (!desc)
3377 tree var, constructor, field;
3378 vec<constructor_elt, va_gc> *v = NULL;
3379 int length = TREE_STRING_LENGTH (str) - 1;
3381 if (darwin_warn_nonportable_cfstrings)
3383 const char *s = TREE_STRING_POINTER (str);
3384 int l = 0;
3386 for (l = 0; l < length; l++)
3387 if (!s[l] || !isascii (s[l]))
3389 warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
3390 s[l] ? "non-ASCII character" : "embedded NUL");
3391 break;
3395 *loc = desc = ggc_alloc_cleared_cfstring_descriptor ();
3396 desc->literal = str;
3398 /* isa *. */
3399 field = TYPE_FIELDS (ccfstring_type_node);
3400 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3401 build1 (ADDR_EXPR, TREE_TYPE (field),
3402 cfstring_class_reference));
3403 /* flags */
3404 field = DECL_CHAIN (field);
3405 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3406 build_int_cst (TREE_TYPE (field), 0x000007c8));
3407 /* string *. */
3408 field = DECL_CHAIN (field);
3409 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3410 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3411 /* length */
3412 field = DECL_CHAIN (field);
3413 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3414 build_int_cst (TREE_TYPE (field), length));
3416 constructor = build_constructor (ccfstring_type_node, v);
3417 TREE_READONLY (constructor) = 1;
3418 TREE_CONSTANT (constructor) = 1;
3419 TREE_STATIC (constructor) = 1;
3421 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3422 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3423 being built without any knowledge of C++ tree accessors; hence, we shall
3424 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3425 if (darwin_running_cxx)
3426 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3428 /* Create an anonymous global variable for this CFString. */
3429 var = build_decl (input_location, CONST_DECL,
3430 NULL, TREE_TYPE (constructor));
3431 DECL_ARTIFICIAL (var) = 1;
3432 TREE_STATIC (var) = 1;
3433 DECL_INITIAL (var) = constructor;
3434 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3435 DECL_CONTEXT (var) = NULL_TREE;
3436 desc->constructor = var;
3439 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3440 TREE_CONSTANT (addr) = 1;
3442 return addr;
3445 bool
3446 darwin_cfstring_p (tree str)
3448 struct cfstring_descriptor key;
3449 void **loc;
3451 if (!str)
3452 return false;
3454 STRIP_NOPS (str);
3456 if (TREE_CODE (str) == ADDR_EXPR)
3457 str = TREE_OPERAND (str, 0);
3459 if (TREE_CODE (str) != STRING_CST)
3460 return false;
3462 key.literal = str;
3463 loc = htab_find_slot (cfstring_htab, &key, NO_INSERT);
3465 if (loc)
3466 return true;
3468 return false;
3471 void
3472 darwin_enter_string_into_cfstring_table (tree str)
3474 struct cfstring_descriptor key;
3475 void **loc;
3477 key.literal = str;
3478 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3480 if (!*loc)
3482 *loc = ggc_alloc_cleared_cfstring_descriptor ();
3483 ((struct cfstring_descriptor *)*loc)->literal = str;
3487 /* Choose named function section based on its frequency. */
3489 section *
3490 darwin_function_section (tree decl, enum node_frequency freq,
3491 bool startup, bool exit)
3493 /* Decide if we need to put this in a coalescable section. */
3494 bool weak = (decl
3495 && DECL_WEAK (decl)
3496 && (!DECL_ATTRIBUTES (decl)
3497 || !lookup_attribute ("weak_import",
3498 DECL_ATTRIBUTES (decl))));
3500 /* If there is a specified section name, we should not be trying to
3501 override. */
3502 if (decl && DECL_SECTION_NAME (decl) != NULL_TREE)
3503 return get_named_section (decl, NULL, 0);
3505 /* Default when there is no function re-ordering. */
3506 if (!flag_reorder_functions)
3507 return (weak)
3508 ? darwin_sections[text_coal_section]
3509 : text_section;
3511 /* Startup code should go to startup subsection unless it is
3512 unlikely executed (this happens especially with function splitting
3513 where we can split away unnecessary parts of static constructors). */
3514 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
3515 return (weak)
3516 ? darwin_sections[text_startup_coal_section]
3517 : darwin_sections[text_startup_section];
3519 /* Similarly for exit. */
3520 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
3521 return (weak)
3522 ? darwin_sections[text_exit_coal_section]
3523 : darwin_sections[text_exit_section];
3525 /* Group cold functions together, similarly for hot code. */
3526 switch (freq)
3528 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
3529 return (weak)
3530 ? darwin_sections[text_cold_coal_section]
3531 : darwin_sections[text_cold_section];
3532 break;
3533 case NODE_FREQUENCY_HOT:
3534 return (weak)
3535 ? darwin_sections[text_hot_coal_section]
3536 : darwin_sections[text_hot_section];
3537 break;
3538 default:
3539 return (weak)
3540 ? darwin_sections[text_coal_section]
3541 : text_section;
3542 break;
3546 /* When a function is partitioned between sections, we need to insert a label
3547 at the start of each new chunk - so that it may become a valid 'atom' for
3548 eh and debug purposes. Without this the linker will emit warnings if one
3549 tries to add line location information (since the switched fragment will
3550 be anonymous). */
3552 void
3553 darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
3555 char buf[128];
3556 snprintf (buf, 128, "%s%s",new_is_cold?"__cold_sect_of_":"__hot_sect_of_",
3557 IDENTIFIER_POINTER (DECL_NAME (decl)));
3558 /* Make sure we pick up all the relevant quotes etc. */
3559 assemble_name_raw (fp, (const char *) buf);
3560 fputs (":\n", fp);
3563 #include "gt-darwin.h"