Reverting merge from trunk
[official-gcc.git] / gcc / config / darwin.c
blob3a5287fe744a5dcaaf911984295ea6ae30fb242a
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 "gimple.h"
49 #include "gimplify.h"
50 #include "lto-streamer.h"
52 /* Darwin supports a feature called fix-and-continue, which is used
53 for rapid turn around debugging. When code is compiled with the
54 -mfix-and-continue flag, two changes are made to the generated code
55 that allow the system to do things that it would normally not be
56 able to do easily. These changes allow gdb to load in
57 recompilation of a translation unit that has been changed into a
58 running program and replace existing functions and methods of that
59 translation unit with versions of those functions and methods
60 from the newly compiled translation unit. The new functions access
61 the existing static symbols from the old translation unit, if the
62 symbol existed in the unit to be replaced, and from the new
63 translation unit, otherwise.
65 The changes are to insert 5 nops at the beginning of all functions
66 and to use indirection to get at static symbols. The 5 nops
67 are required by consumers of the generated code. Currently, gdb
68 uses this to patch in a jump to the overriding function, this
69 allows all uses of the old name to forward to the replacement,
70 including existing function pointers and virtual methods. See
71 rs6000_emit_prologue for the code that handles the nop insertions.
73 The added indirection allows gdb to redirect accesses to static
74 symbols from the newly loaded translation unit to the existing
75 symbol, if any. @code{static} symbols are special and are handled by
76 setting the second word in the .non_lazy_symbol_pointer data
77 structure to symbol. See indirect_data for the code that handles
78 the extra indirection, and machopic_output_indirection and its use
79 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
80 symbol indirection. */
82 /* For darwin >= 9 (OSX 10.5) the linker is capable of making the necessary
83 branch islands and we no longer need to emit darwin stubs.
84 However, if we are generating code for earlier systems (or for use in the
85 kernel) the stubs might still be required, and this will be set true. */
86 int darwin_emit_branch_islands = false;
88 typedef struct GTY(()) cdtor_record {
89 rtx symbol;
90 int priority; /* [con/de]structor priority */
91 int position; /* original position */
92 } cdtor_record;
94 static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL;
95 static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL;
97 /* A flag to determine whether we are running c++ or obj-c++. This has to be
98 settable from non-c-family contexts too (i.e. we can't use the c_dialect_
99 functions). */
100 int darwin_running_cxx;
102 /* Some code-gen now depends on OS major version numbers (at least). */
103 int generating_for_darwin_version ;
105 /* Section names. */
106 section * darwin_sections[NUM_DARWIN_SECTIONS];
108 /* While we transition to using in-tests instead of ifdef'd code. */
109 #ifndef HAVE_lo_sum
110 #define HAVE_lo_sum 0
111 #define gen_macho_high(a,b) (a)
112 #define gen_macho_low(a,b,c) (a)
113 #endif
115 /* True if we're setting __attribute__ ((ms_struct)). */
116 int darwin_ms_struct = false;
118 /* Earlier versions of Darwin as do not recognize an alignment field in
119 .comm directives, this should be set for versions that allow it. */
120 int emit_aligned_common = false;
122 /* A get_unnamed_section callback used to switch to an ObjC section.
123 DIRECTIVE is as for output_section_asm_op. */
125 static void
126 output_objc_section_asm_op (const void *directive)
128 static bool been_here = false;
130 /* The NeXT ObjC Runtime requires these sections to be present and in
131 order in the object. The code below implements this by emitting
132 a section header for each ObjC section the first time that an ObjC
133 section is requested. */
134 if (! been_here)
136 section *saved_in_section = in_section;
137 static const enum darwin_section_enum tomark[] =
139 /* written, cold -> hot */
140 objc_cat_cls_meth_section,
141 objc_cat_inst_meth_section,
142 objc_string_object_section,
143 objc_constant_string_object_section,
144 objc_selector_refs_section,
145 objc_selector_fixup_section,
146 objc_cls_refs_section,
147 objc_class_section,
148 objc_meta_class_section,
149 /* shared, hot -> cold */
150 objc_cls_meth_section,
151 objc_inst_meth_section,
152 objc_protocol_section,
153 objc_class_names_section,
154 objc_meth_var_types_section,
155 objc_meth_var_names_section,
156 objc_category_section,
157 objc_class_vars_section,
158 objc_instance_vars_section,
159 objc_module_info_section,
160 objc_symbols_section,
162 /* ABI=1 */
163 static const enum darwin_section_enum tomarkv1[] =
165 objc1_protocol_ext_section,
166 objc1_class_ext_section,
167 objc1_prop_list_section
169 /* ABI=2 */
170 static const enum darwin_section_enum tomarkv2[] =
172 objc2_message_refs_section,
173 objc2_classdefs_section,
174 objc2_metadata_section,
175 objc2_classrefs_section,
176 objc2_classlist_section,
177 objc2_categorylist_section,
178 objc2_selector_refs_section,
179 objc2_nonlazy_class_section,
180 objc2_nonlazy_category_section,
181 objc2_protocollist_section,
182 objc2_protocolrefs_section,
183 objc2_super_classrefs_section,
184 objc2_image_info_section,
185 objc2_constant_string_object_section
187 size_t i;
189 been_here = true;
190 if (flag_objc_abi < 2)
192 for (i = 0; i < ARRAY_SIZE (tomark); i++)
193 switch_to_section (darwin_sections[tomark[i]]);
194 if (flag_objc_abi == 1)
195 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
196 switch_to_section (darwin_sections[tomarkv1[i]]);
198 else
199 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
200 switch_to_section (darwin_sections[tomarkv2[i]]);
201 /* Make sure we don't get varasm.c out of sync with us. */
202 switch_to_section (saved_in_section);
204 output_section_asm_op (directive);
208 /* Private flag applied to disable section-anchors in a particular section. */
209 #define SECTION_NO_ANCHOR SECTION_MACH_DEP
212 /* Implement TARGET_ASM_INIT_SECTIONS. */
214 void
215 darwin_init_sections (void)
217 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
218 darwin_sections[NAME] = \
219 get_unnamed_section (FLAGS, (OBJC \
220 ? output_objc_section_asm_op \
221 : output_section_asm_op), \
222 "\t" DIRECTIVE);
223 #include "config/darwin-sections.def"
224 #undef DEF_SECTION
226 readonly_data_section = darwin_sections[const_section];
227 exception_section = darwin_sections[darwin_exception_section];
228 eh_frame_section = darwin_sections[darwin_eh_frame_section];
232 name_needs_quotes (const char *name)
234 int c;
235 while ((c = *name++) != '\0')
236 if (! ISIDNUM (c)
237 && c != '.' && c != '$' && c != '_' )
238 return 1;
239 return 0;
242 /* Return true if SYM_REF can be used without an indirection. */
244 machopic_symbol_defined_p (rtx sym_ref)
246 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
247 return true;
249 /* If a symbol references local and is not an extern to this
250 file, then the symbol might be able to declared as defined. */
251 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
253 /* If the symbol references a variable and the variable is a
254 common symbol, then this symbol is not defined. */
255 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
257 tree decl = SYMBOL_REF_DECL (sym_ref);
258 if (!decl)
259 return true;
260 if (DECL_COMMON (decl))
261 return false;
263 return true;
265 return false;
268 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
269 reference, which will not be changed. */
271 enum machopic_addr_class
272 machopic_classify_symbol (rtx sym_ref)
274 bool function_p;
276 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
277 if (machopic_symbol_defined_p (sym_ref))
278 return (function_p
279 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
280 else
281 return (function_p
282 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
285 #ifndef TARGET_FIX_AND_CONTINUE
286 #define TARGET_FIX_AND_CONTINUE 0
287 #endif
289 /* Indicate when fix-and-continue style code generation is being used
290 and when a reference to data should be indirected so that it can be
291 rebound in a new translation unit to reference the original instance
292 of that data. Symbol names that are for code generation local to
293 the translation unit are bound to the new translation unit;
294 currently this means symbols that begin with L or _OBJC_;
295 otherwise, we indicate that an indirect reference should be made to
296 permit the runtime to rebind new instances of the translation unit
297 to the original instance of the data. */
299 static int
300 indirect_data (rtx sym_ref)
302 int lprefix;
303 const char *name;
305 /* If we aren't generating fix-and-continue code, don't do anything
306 special. */
307 if (TARGET_FIX_AND_CONTINUE == 0)
308 return 0;
310 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
311 are indirected. Symbols that begin with L and _OBJC_ are always
312 bound to the current translation unit as they are used for
313 generated local data of the translation unit. */
315 name = XSTR (sym_ref, 0);
317 lprefix = (((name[0] == '*' || name[0] == '&')
318 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
319 || (strncmp (name, "_OBJC_", 6) == 0));
321 return ! lprefix;
324 static int
325 machopic_data_defined_p (rtx sym_ref)
327 if (indirect_data (sym_ref))
328 return 0;
330 switch (machopic_classify_symbol (sym_ref))
332 case MACHOPIC_DEFINED_DATA:
333 case MACHOPIC_DEFINED_FUNCTION:
334 return 1;
335 default:
336 return 0;
340 void
341 machopic_define_symbol (rtx mem)
343 rtx sym_ref;
345 gcc_assert (GET_CODE (mem) == MEM);
346 sym_ref = XEXP (mem, 0);
347 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
350 /* Return either ORIG or:
352 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
354 depending on MACHO_DYNAMIC_NO_PIC_P. */
356 machopic_gen_offset (rtx orig)
358 if (MACHO_DYNAMIC_NO_PIC_P)
359 return orig;
360 else
362 /* Play games to avoid marking the function as needing pic if we
363 are being called as part of the cost-estimation process. */
364 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
365 crtl->uses_pic_offset_table = 1;
366 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
367 UNSPEC_MACHOPIC_OFFSET);
368 return gen_rtx_CONST (Pmode, orig);
372 static GTY(()) const char * function_base_func_name;
373 static GTY(()) int current_pic_label_num;
374 static GTY(()) int emitted_pic_label_num;
376 static void
377 update_pic_label_number_if_needed (void)
379 const char *current_name;
381 /* When we are generating _get_pc thunks within stubs, there is no current
382 function. */
383 if (current_function_decl)
385 current_name =
386 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
387 if (function_base_func_name != current_name)
389 ++current_pic_label_num;
390 function_base_func_name = current_name;
393 else
395 ++current_pic_label_num;
396 function_base_func_name = "L_machopic_stub_dummy";
400 void
401 machopic_output_function_base_name (FILE *file)
403 /* If dynamic-no-pic is on, we should not get here. */
404 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
406 update_pic_label_number_if_needed ();
407 fprintf (file, "L%d$pb", current_pic_label_num);
410 char curr_picbasename[32];
412 const char *
413 machopic_get_function_picbase (void)
415 /* If dynamic-no-pic is on, we should not get here. */
416 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
418 update_pic_label_number_if_needed ();
419 snprintf (curr_picbasename, 32, "L%d$pb", current_pic_label_num);
420 return (const char *) curr_picbasename;
423 bool
424 machopic_should_output_picbase_label (void)
426 update_pic_label_number_if_needed ();
428 if (current_pic_label_num == emitted_pic_label_num)
429 return false;
431 emitted_pic_label_num = current_pic_label_num;
432 return true;
435 /* The suffix attached to non-lazy pointer symbols. */
436 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
437 /* The suffix attached to stub symbols. */
438 #define STUB_SUFFIX "$stub"
440 typedef struct GTY (()) machopic_indirection
442 /* The SYMBOL_REF for the entity referenced. */
443 rtx symbol;
444 /* The name of the stub or non-lazy pointer. */
445 const char * ptr_name;
446 /* True iff this entry is for a stub (as opposed to a non-lazy
447 pointer). */
448 bool stub_p;
449 /* True iff this stub or pointer pointer has been referenced. */
450 bool used;
451 } machopic_indirection;
453 /* A table mapping stub names and non-lazy pointer names to
454 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
456 static GTY ((param_is (struct machopic_indirection))) htab_t
457 machopic_indirections;
459 /* Return a hash value for a SLOT in the indirections hash table. */
461 static hashval_t
462 machopic_indirection_hash (const void *slot)
464 const machopic_indirection *p = (const machopic_indirection *) slot;
465 return htab_hash_string (p->ptr_name);
468 /* Returns true if the KEY is the same as that associated with
469 SLOT. */
471 static int
472 machopic_indirection_eq (const void *slot, const void *key)
474 return strcmp (((const machopic_indirection *) slot)->ptr_name,
475 (const char *) key) == 0;
478 /* Return the name of the non-lazy pointer (if STUB_P is false) or
479 stub (if STUB_B is true) corresponding to the given name. */
481 const char *
482 machopic_indirection_name (rtx sym_ref, bool stub_p)
484 char *buffer;
485 const char *name = XSTR (sym_ref, 0);
486 size_t namelen = strlen (name);
487 machopic_indirection *p;
488 void ** slot;
489 bool needs_quotes;
490 const char *suffix;
491 const char *prefix = user_label_prefix;
492 const char *quote = "";
493 tree id;
495 id = maybe_get_identifier (name);
496 if (id)
498 tree id_orig = id;
500 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
501 id = TREE_CHAIN (id);
502 if (id != id_orig)
504 name = IDENTIFIER_POINTER (id);
505 namelen = strlen (name);
509 if (name[0] == '*')
511 prefix = "";
512 ++name;
513 --namelen;
516 needs_quotes = name_needs_quotes (name);
517 if (needs_quotes)
519 quote = "\"";
522 if (stub_p)
523 suffix = STUB_SUFFIX;
524 else
525 suffix = NON_LAZY_POINTER_SUFFIX;
527 buffer = XALLOCAVEC (char, strlen ("&L")
528 + strlen (prefix)
529 + namelen
530 + strlen (suffix)
531 + 2 * strlen (quote)
532 + 1 /* '\0' */);
534 /* Construct the name of the non-lazy pointer or stub. */
535 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
537 if (!machopic_indirections)
538 machopic_indirections = htab_create_ggc (37,
539 machopic_indirection_hash,
540 machopic_indirection_eq,
541 /*htab_del=*/NULL);
543 slot = htab_find_slot_with_hash (machopic_indirections, buffer,
544 htab_hash_string (buffer), INSERT);
545 if (*slot)
547 p = (machopic_indirection *) *slot;
549 else
551 p = ggc_alloc_machopic_indirection ();
552 p->symbol = sym_ref;
553 p->ptr_name = xstrdup (buffer);
554 p->stub_p = stub_p;
555 p->used = false;
556 *slot = p;
559 return p->ptr_name;
562 /* Return the name of the stub for the mcount function. */
564 const char*
565 machopic_mcount_stub_name (void)
567 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
568 return machopic_indirection_name (symbol, /*stub_p=*/true);
571 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
572 or non-lazy pointer as used -- and mark the object to which the
573 pointer/stub refers as used as well, since the pointer/stub will
574 emit a reference to it. */
576 void
577 machopic_validate_stub_or_non_lazy_ptr (const char *name)
579 machopic_indirection *p;
581 p = ((machopic_indirection *)
582 (htab_find_with_hash (machopic_indirections, name,
583 htab_hash_string (name))));
584 if (p && ! p->used)
586 const char *real_name;
587 tree id;
589 p->used = true;
591 /* Do what output_addr_const will do when we actually call it. */
592 if (SYMBOL_REF_DECL (p->symbol))
593 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
595 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
597 id = maybe_get_identifier (real_name);
598 if (id)
599 mark_referenced (id);
603 /* Transform ORIG, which may be any data source, to the corresponding
604 source using indirections. */
607 machopic_indirect_data_reference (rtx orig, rtx reg)
609 rtx ptr_ref = orig;
611 if (! MACHOPIC_INDIRECT)
612 return orig;
614 if (GET_CODE (orig) == SYMBOL_REF)
616 int defined = machopic_data_defined_p (orig);
618 if (defined && MACHO_DYNAMIC_NO_PIC_P)
620 if (DARWIN_PPC)
622 /* Create a new register for CSE opportunities. */
623 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
624 emit_insn (gen_macho_high (hi_reg, orig));
625 emit_insn (gen_macho_low (reg, hi_reg, orig));
626 return reg;
628 else if (DARWIN_X86)
629 return orig;
630 else
631 /* some other cpu -- writeme! */
632 gcc_unreachable ();
634 else if (defined)
636 rtx offset = NULL;
637 if (DARWIN_PPC || HAVE_lo_sum)
638 offset = machopic_gen_offset (orig);
640 if (DARWIN_PPC)
642 rtx hi_sum_reg = (!can_create_pseudo_p ()
643 ? reg
644 : gen_reg_rtx (Pmode));
646 gcc_assert (reg);
648 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
649 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
650 gen_rtx_HIGH (Pmode, offset))));
651 emit_insn (gen_rtx_SET (Pmode, reg,
652 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
653 copy_rtx (offset))));
655 orig = reg;
657 else if (HAVE_lo_sum)
659 gcc_assert (reg);
661 emit_insn (gen_rtx_SET (VOIDmode, reg,
662 gen_rtx_HIGH (Pmode, offset)));
663 emit_insn (gen_rtx_SET (VOIDmode, reg,
664 gen_rtx_LO_SUM (Pmode, reg,
665 copy_rtx (offset))));
666 emit_use (pic_offset_table_rtx);
668 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
670 return orig;
673 ptr_ref = (gen_rtx_SYMBOL_REF
674 (Pmode,
675 machopic_indirection_name (orig, /*stub_p=*/false)));
677 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
679 ptr_ref = gen_const_mem (Pmode, ptr_ref);
680 machopic_define_symbol (ptr_ref);
682 if (DARWIN_X86
683 && reg
684 && MACHO_DYNAMIC_NO_PIC_P)
686 emit_insn (gen_rtx_SET (Pmode, reg, ptr_ref));
687 ptr_ref = reg;
690 return ptr_ref;
692 else if (GET_CODE (orig) == CONST)
694 /* If "(const (plus ...", walk the PLUS and return that result.
695 PLUS processing (below) will restore the "(const ..." if
696 appropriate. */
697 if (GET_CODE (XEXP (orig, 0)) == PLUS)
698 return machopic_indirect_data_reference (XEXP (orig, 0), reg);
699 else
700 return orig;
702 else if (GET_CODE (orig) == MEM)
704 XEXP (ptr_ref, 0) =
705 machopic_indirect_data_reference (XEXP (orig, 0), reg);
706 return ptr_ref;
708 else if (GET_CODE (orig) == PLUS)
710 rtx base, result;
711 /* When the target is i386, this code prevents crashes due to the
712 compiler's ignorance on how to move the PIC base register to
713 other registers. (The reload phase sometimes introduces such
714 insns.) */
715 if (GET_CODE (XEXP (orig, 0)) == REG
716 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
717 /* Prevent the same register from being erroneously used
718 as both the base and index registers. */
719 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
720 && reg)
722 emit_move_insn (reg, XEXP (orig, 0));
723 XEXP (ptr_ref, 0) = reg;
724 return ptr_ref;
727 /* Legitimize both operands of the PLUS. */
728 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
729 orig = machopic_indirect_data_reference (XEXP (orig, 1),
730 (base == reg ? 0 : reg));
731 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
732 result = plus_constant (Pmode, base, INTVAL (orig));
733 else
734 result = gen_rtx_PLUS (Pmode, base, orig);
736 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
738 if (reg)
740 emit_move_insn (reg, result);
741 result = reg;
743 else
745 result = force_reg (GET_MODE (result), result);
749 return result;
751 return ptr_ref;
754 /* Transform TARGET (a MEM), which is a function call target, to the
755 corresponding symbol_stub if necessary. Return a new MEM. */
758 machopic_indirect_call_target (rtx target)
760 if (! darwin_emit_branch_islands)
761 return target;
763 if (GET_CODE (target) != MEM)
764 return target;
766 if (MACHOPIC_INDIRECT
767 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
768 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
769 & MACHO_SYMBOL_FLAG_DEFINED))
771 rtx sym_ref = XEXP (target, 0);
772 const char *stub_name = machopic_indirection_name (sym_ref,
773 /*stub_p=*/true);
774 enum machine_mode mode = GET_MODE (sym_ref);
776 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
777 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
778 MEM_READONLY_P (target) = 1;
779 MEM_NOTRAP_P (target) = 1;
782 return target;
786 machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
788 rtx pic_ref = orig;
790 if (! MACHOPIC_INDIRECT)
791 return orig;
793 /* First handle a simple SYMBOL_REF or LABEL_REF */
794 if (GET_CODE (orig) == LABEL_REF
795 || (GET_CODE (orig) == SYMBOL_REF
798 /* addr(foo) = &func+(foo-func) */
799 orig = machopic_indirect_data_reference (orig, reg);
801 if (GET_CODE (orig) == PLUS
802 && GET_CODE (XEXP (orig, 0)) == REG)
804 if (reg == 0)
805 return force_reg (mode, orig);
807 emit_move_insn (reg, orig);
808 return reg;
811 if (GET_CODE (orig) == MEM)
813 if (reg == 0)
815 gcc_assert (!reload_in_progress);
816 reg = gen_reg_rtx (Pmode);
819 #if HAVE_lo_sum
820 if (MACHO_DYNAMIC_NO_PIC_P
821 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
822 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
824 #if defined (TARGET_TOC) /* ppc */
825 rtx temp_reg = (!can_create_pseudo_p ()
826 ? reg :
827 gen_reg_rtx (Pmode));
828 rtx asym = XEXP (orig, 0);
829 rtx mem;
831 emit_insn (gen_macho_high (temp_reg, asym));
832 mem = gen_const_mem (GET_MODE (orig),
833 gen_rtx_LO_SUM (Pmode, temp_reg,
834 copy_rtx (asym)));
835 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
836 #else
837 /* Some other CPU -- WriteMe! but right now there are no other
838 platforms that can use dynamic-no-pic */
839 gcc_unreachable ();
840 #endif
841 pic_ref = reg;
843 else
844 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
845 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
847 rtx offset = machopic_gen_offset (XEXP (orig, 0));
848 #if defined (TARGET_TOC) /* i.e., PowerPC */
849 /* Generating a new reg may expose opportunities for
850 common subexpression elimination. */
851 rtx hi_sum_reg = (!can_create_pseudo_p ()
852 ? reg
853 : gen_reg_rtx (Pmode));
854 rtx mem;
855 rtx insn;
856 rtx sum;
858 sum = gen_rtx_HIGH (Pmode, offset);
859 if (! MACHO_DYNAMIC_NO_PIC_P)
860 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
862 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
864 mem = gen_const_mem (GET_MODE (orig),
865 gen_rtx_LO_SUM (Pmode,
866 hi_sum_reg,
867 copy_rtx (offset)));
868 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
869 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
871 pic_ref = reg;
872 #else
873 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
875 emit_insn (gen_rtx_SET (VOIDmode, reg,
876 gen_rtx_HIGH (Pmode,
877 gen_rtx_CONST (Pmode,
878 offset))));
879 emit_insn (gen_rtx_SET (VOIDmode, reg,
880 gen_rtx_LO_SUM (Pmode, reg,
881 gen_rtx_CONST (Pmode,
882 copy_rtx (offset)))));
883 pic_ref = gen_rtx_PLUS (Pmode,
884 pic_offset_table_rtx, reg);
885 #endif
887 else
888 #endif /* HAVE_lo_sum */
890 rtx pic = pic_offset_table_rtx;
891 if (GET_CODE (pic) != REG)
893 emit_move_insn (reg, pic);
894 pic = reg;
896 #if 0
897 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
898 #endif
900 if (reload_in_progress)
901 df_set_regs_ever_live (REGNO (pic), true);
902 pic_ref = gen_rtx_PLUS (Pmode, pic,
903 machopic_gen_offset (XEXP (orig, 0)));
906 #if !defined (TARGET_TOC)
907 emit_move_insn (reg, pic_ref);
908 pic_ref = gen_const_mem (GET_MODE (orig), reg);
909 #endif
911 else
914 #if HAVE_lo_sum
915 if (GET_CODE (orig) == SYMBOL_REF
916 || GET_CODE (orig) == LABEL_REF)
918 rtx offset = machopic_gen_offset (orig);
919 #if defined (TARGET_TOC) /* i.e., PowerPC */
920 rtx hi_sum_reg;
922 if (reg == 0)
924 gcc_assert (!reload_in_progress);
925 reg = gen_reg_rtx (Pmode);
928 hi_sum_reg = reg;
930 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
931 (MACHO_DYNAMIC_NO_PIC_P)
932 ? gen_rtx_HIGH (Pmode, offset)
933 : gen_rtx_PLUS (Pmode,
934 pic_offset_table_rtx,
935 gen_rtx_HIGH (Pmode,
936 offset))));
937 emit_insn (gen_rtx_SET (VOIDmode, reg,
938 gen_rtx_LO_SUM (Pmode,
939 hi_sum_reg,
940 copy_rtx (offset))));
941 pic_ref = reg;
942 #else
943 emit_insn (gen_rtx_SET (VOIDmode, reg,
944 gen_rtx_HIGH (Pmode, offset)));
945 emit_insn (gen_rtx_SET (VOIDmode, reg,
946 gen_rtx_LO_SUM (Pmode, reg,
947 copy_rtx (offset))));
948 pic_ref = gen_rtx_PLUS (Pmode,
949 pic_offset_table_rtx, reg);
950 #endif
952 else
953 #endif /* HAVE_lo_sum */
955 if (REG_P (orig)
956 || GET_CODE (orig) == SUBREG)
958 return orig;
960 else
962 rtx pic = pic_offset_table_rtx;
963 if (GET_CODE (pic) != REG)
965 emit_move_insn (reg, pic);
966 pic = reg;
968 #if 0
969 emit_use (pic_offset_table_rtx);
970 #endif
971 if (reload_in_progress)
972 df_set_regs_ever_live (REGNO (pic), true);
973 pic_ref = gen_rtx_PLUS (Pmode,
974 pic,
975 machopic_gen_offset (orig));
980 if (GET_CODE (pic_ref) != REG)
982 if (reg != 0)
984 emit_move_insn (reg, pic_ref);
985 return reg;
987 else
989 return force_reg (mode, pic_ref);
992 else
994 return pic_ref;
998 else if (GET_CODE (orig) == SYMBOL_REF)
999 return orig;
1001 else if (GET_CODE (orig) == PLUS
1002 && (GET_CODE (XEXP (orig, 0)) == MEM
1003 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
1004 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
1005 && XEXP (orig, 0) != pic_offset_table_rtx
1006 && GET_CODE (XEXP (orig, 1)) != REG)
1009 rtx base;
1010 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
1012 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1013 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
1014 Pmode, (base == reg ? 0 : reg));
1015 if (GET_CODE (orig) == CONST_INT)
1017 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
1018 is_complex = 1;
1020 else
1021 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
1023 if (reg && is_complex)
1025 emit_move_insn (reg, pic_ref);
1026 pic_ref = reg;
1028 /* Likewise, should we set special REG_NOTEs here? */
1031 else if (GET_CODE (orig) == CONST)
1033 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1036 else if (GET_CODE (orig) == MEM
1037 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
1039 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1040 addr = replace_equiv_address (orig, addr);
1041 emit_move_insn (reg, addr);
1042 pic_ref = reg;
1045 return pic_ref;
1048 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
1049 DATA is the FILE* for assembly output. Called from
1050 htab_traverse. */
1052 static int
1053 machopic_output_indirection (void **slot, void *data)
1055 machopic_indirection *p = *((machopic_indirection **) slot);
1056 FILE *asm_out_file = (FILE *) data;
1057 rtx symbol;
1058 const char *sym_name;
1059 const char *ptr_name;
1061 if (!p->used)
1062 return 1;
1064 symbol = p->symbol;
1065 sym_name = XSTR (symbol, 0);
1066 ptr_name = p->ptr_name;
1068 if (p->stub_p)
1070 char *sym;
1071 char *stub;
1072 tree id;
1074 id = maybe_get_identifier (sym_name);
1075 if (id)
1077 tree id_orig = id;
1079 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1080 id = TREE_CHAIN (id);
1081 if (id != id_orig)
1082 sym_name = IDENTIFIER_POINTER (id);
1085 sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1086 if (sym_name[0] == '*' || sym_name[0] == '&')
1087 strcpy (sym, sym_name + 1);
1088 else if (sym_name[0] == '-' || sym_name[0] == '+')
1089 strcpy (sym, sym_name);
1090 else
1091 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1093 stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1094 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1095 strcpy (stub, ptr_name + 1);
1096 else
1097 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1099 machopic_output_stub (asm_out_file, sym, stub);
1101 else if (! indirect_data (symbol)
1102 && (machopic_symbol_defined_p (symbol)
1103 || SYMBOL_REF_LOCAL_P (symbol)))
1105 switch_to_section (data_section);
1106 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1107 assemble_label (asm_out_file, ptr_name);
1108 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1109 GET_MODE_SIZE (Pmode),
1110 GET_MODE_ALIGNMENT (Pmode), 1);
1112 else
1114 rtx init = const0_rtx;
1116 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1118 /* Mach-O symbols are passed around in code through indirect
1119 references and the original symbol_ref hasn't passed through
1120 the generic handling and reference-catching in
1121 output_operand, so we need to manually mark weak references
1122 as such. */
1123 if (SYMBOL_REF_WEAK (symbol))
1125 tree decl = SYMBOL_REF_DECL (symbol);
1126 gcc_assert (DECL_P (decl));
1128 if (decl != NULL_TREE
1129 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
1130 /* Handle only actual external-only definitions, not
1131 e.g. extern inline code or variables for which
1132 storage has been allocated. */
1133 && !TREE_STATIC (decl))
1135 fputs ("\t.weak_reference ", asm_out_file);
1136 assemble_name (asm_out_file, sym_name);
1137 fputc ('\n', asm_out_file);
1141 assemble_name (asm_out_file, ptr_name);
1142 fprintf (asm_out_file, ":\n");
1144 fprintf (asm_out_file, "\t.indirect_symbol ");
1145 assemble_name (asm_out_file, sym_name);
1146 fprintf (asm_out_file, "\n");
1148 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1149 have their symbol name instead of 0 in the second entry of
1150 the non-lazy symbol pointer data structure when they are
1151 defined. This allows the runtime to rebind newer instances
1152 of the translation unit with the original instance of the
1153 symbol. */
1155 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1156 && machopic_symbol_defined_p (symbol))
1157 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1159 assemble_integer (init, GET_MODE_SIZE (Pmode),
1160 GET_MODE_ALIGNMENT (Pmode), 1);
1163 return 1;
1166 void
1167 machopic_finish (FILE *asm_out_file)
1169 if (machopic_indirections)
1170 htab_traverse_noresize (machopic_indirections,
1171 machopic_output_indirection,
1172 asm_out_file);
1176 machopic_operand_p (rtx op)
1178 if (MACHOPIC_JUST_INDIRECT)
1179 return (GET_CODE (op) == SYMBOL_REF
1180 && machopic_symbol_defined_p (op));
1181 else
1182 return (GET_CODE (op) == CONST
1183 && GET_CODE (XEXP (op, 0)) == UNSPEC
1184 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1187 /* This function records whether a given name corresponds to a defined
1188 or undefined function or variable, for machopic_classify_ident to
1189 use later. */
1191 void
1192 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1194 rtx sym_ref;
1196 /* Do the standard encoding things first. */
1197 default_encode_section_info (decl, rtl, first);
1199 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1200 return;
1202 sym_ref = XEXP (rtl, 0);
1203 if (TREE_CODE (decl) == VAR_DECL)
1204 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1206 if (!DECL_EXTERNAL (decl)
1207 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1208 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1209 && ((TREE_STATIC (decl)
1210 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1211 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1212 && DECL_INITIAL (decl) != error_mark_node)))
1213 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1215 if (! TREE_PUBLIC (decl))
1216 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1219 void
1220 darwin_mark_decl_preserved (const char *name)
1222 fprintf (asm_out_file, "\t.no_dead_strip ");
1223 assemble_name (asm_out_file, name);
1224 fputc ('\n', asm_out_file);
1227 static section *
1228 darwin_rodata_section (int weak, bool zsize)
1230 return (weak
1231 ? darwin_sections[const_coal_section]
1232 : (zsize ? darwin_sections[zobj_const_section]
1233 : darwin_sections[const_section]));
1236 static section *
1237 darwin_mergeable_string_section (tree exp,
1238 unsigned HOST_WIDE_INT align)
1240 /* Darwin's ld expects to see non-writable string literals in the .cstring
1241 section. Later versions of ld check and complain when CFStrings are
1242 enabled. Therefore we shall force the strings into .cstring since we
1243 don't support writable ones anyway. */
1244 if ((darwin_constant_cfstrings || flag_merge_constants)
1245 && TREE_CODE (exp) == STRING_CST
1246 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1247 && align <= 256
1248 && (int_size_in_bytes (TREE_TYPE (exp))
1249 == TREE_STRING_LENGTH (exp))
1250 && ((size_t) TREE_STRING_LENGTH (exp)
1251 == strlen (TREE_STRING_POINTER (exp)) + 1))
1252 return darwin_sections[cstring_section];
1254 if (DARWIN_SECTION_ANCHORS && flag_section_anchors
1255 && TREE_CODE (exp) == STRING_CST
1256 && TREE_STRING_LENGTH (exp) == 0)
1257 return darwin_sections[zobj_const_section];
1259 return readonly_data_section;
1262 #ifndef HAVE_GAS_LITERAL16
1263 #define HAVE_GAS_LITERAL16 0
1264 #endif
1266 static section *
1267 darwin_mergeable_constant_section (tree exp,
1268 unsigned HOST_WIDE_INT align,
1269 bool zsize)
1271 enum machine_mode mode = DECL_MODE (exp);
1272 unsigned int modesize = GET_MODE_BITSIZE (mode);
1274 if (DARWIN_SECTION_ANCHORS
1275 && flag_section_anchors
1276 && zsize)
1277 return darwin_sections[zobj_const_section];
1279 if (flag_merge_constants
1280 && mode != VOIDmode
1281 && mode != BLKmode
1282 && modesize <= align
1283 && align >= 8
1284 && align <= 256
1285 && (align & (align -1)) == 0)
1287 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1289 if (TREE_CODE (size) == INTEGER_CST
1290 && TREE_INT_CST_LOW (size) == 4
1291 && TREE_INT_CST_HIGH (size) == 0)
1292 return darwin_sections[literal4_section];
1293 else if (TREE_CODE (size) == INTEGER_CST
1294 && TREE_INT_CST_LOW (size) == 8
1295 && TREE_INT_CST_HIGH (size) == 0)
1296 return darwin_sections[literal8_section];
1297 else if (HAVE_GAS_LITERAL16
1298 && TARGET_64BIT
1299 && TREE_CODE (size) == INTEGER_CST
1300 && TREE_INT_CST_LOW (size) == 16
1301 && TREE_INT_CST_HIGH (size) == 0)
1302 return darwin_sections[literal16_section];
1303 else
1304 return readonly_data_section;
1307 return readonly_data_section;
1310 section *
1311 darwin_tm_clone_table_section (void)
1313 return get_named_section (NULL,
1314 "__DATA,__tm_clone_table,regular,no_dead_strip",
1319 machopic_reloc_rw_mask (void)
1321 return MACHOPIC_INDIRECT ? 3 : 0;
1324 /* We have to deal with ObjC/C++ metadata section placement in the common
1325 code, since it will also be called from LTO.
1327 Return metadata attributes, if present (searching for ABI=2 first)
1328 Return NULL_TREE if no such attributes are found. */
1330 static tree
1331 is_objc_metadata (tree decl)
1333 if (DECL_P (decl)
1334 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1335 && DECL_ATTRIBUTES (decl))
1337 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1338 if (meta)
1339 return meta;
1340 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1341 if (meta)
1342 return meta;
1344 return NULL_TREE;
1347 static int classes_seen;
1348 static int objc_metadata_seen;
1350 /* Return the section required for Objective C ABI 2 metadata. */
1351 static section *
1352 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1354 const char *p;
1355 tree ident = TREE_VALUE (meta);
1356 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1357 p = IDENTIFIER_POINTER (ident);
1359 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi == 2);
1361 objc_metadata_seen = 1;
1363 if (base == data_section)
1364 base = darwin_sections[objc2_metadata_section];
1366 /* Most of the OBJC2 META-data end up in the base section, so check it
1367 first. */
1368 if (!strncmp (p, "V2_BASE", 7))
1369 return base;
1370 else if (!strncmp (p, "V2_STRG", 7))
1371 return darwin_sections[cstring_section];
1373 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1374 return darwin_sections[objc2_classdefs_section];
1375 else if (!strncmp (p, "V2_MREF", 7))
1376 return darwin_sections[objc2_message_refs_section];
1377 else if (!strncmp (p, "V2_CLRF", 7))
1378 return darwin_sections[objc2_classrefs_section];
1379 else if (!strncmp (p, "V2_SURF", 7))
1380 return darwin_sections[objc2_super_classrefs_section];
1381 else if (!strncmp (p, "V2_NLCL", 7))
1382 return darwin_sections[objc2_nonlazy_class_section];
1383 else if (!strncmp (p, "V2_CLAB", 7))
1385 classes_seen = 1;
1386 return darwin_sections[objc2_classlist_section];
1388 else if (!strncmp (p, "V2_SRFS", 7))
1389 return darwin_sections[objc2_selector_refs_section];
1390 else if (!strncmp (p, "V2_NLCA", 7))
1391 return darwin_sections[objc2_nonlazy_category_section];
1392 else if (!strncmp (p, "V2_CALA", 7))
1393 return darwin_sections[objc2_categorylist_section];
1395 else if (!strncmp (p, "V2_PLST", 7))
1396 return darwin_sections[objc2_protocollist_section];
1397 else if (!strncmp (p, "V2_PRFS", 7))
1398 return darwin_sections[objc2_protocolrefs_section];
1400 else if (!strncmp (p, "V2_INFO", 7))
1401 return darwin_sections[objc2_image_info_section];
1403 else if (!strncmp (p, "V2_EHTY", 7))
1404 return darwin_sections[data_coal_section];
1406 else if (!strncmp (p, "V2_CSTR", 7))
1407 return darwin_sections[objc2_constant_string_object_section];
1409 /* Not recognized, default. */
1410 return base;
1413 /* Return the section required for Objective C ABI 0/1 metadata. */
1414 static section *
1415 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1417 const char *p;
1418 tree ident = TREE_VALUE (meta);
1419 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1420 p = IDENTIFIER_POINTER (ident);
1422 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi < 2);
1424 objc_metadata_seen = 1;
1426 /* String sections first, cos there are lots of strings. */
1427 if (!strncmp (p, "V1_STRG", 7))
1428 return darwin_sections[cstring_section];
1429 else if (!strncmp (p, "V1_CLSN", 7))
1430 return darwin_sections[objc_class_names_section];
1431 else if (!strncmp (p, "V1_METN", 7))
1432 return darwin_sections[objc_meth_var_names_section];
1433 else if (!strncmp (p, "V1_METT", 7))
1434 return darwin_sections[objc_meth_var_types_section];
1436 else if (!strncmp (p, "V1_CLAS", 7))
1438 classes_seen = 1;
1439 return darwin_sections[objc_class_section];
1441 else if (!strncmp (p, "V1_META", 7))
1442 return darwin_sections[objc_meta_class_section];
1443 else if (!strncmp (p, "V1_CATG", 7))
1444 return darwin_sections[objc_category_section];
1445 else if (!strncmp (p, "V1_PROT", 7))
1446 return darwin_sections[objc_protocol_section];
1448 else if (!strncmp (p, "V1_CLCV", 7))
1449 return darwin_sections[objc_class_vars_section];
1450 else if (!strncmp (p, "V1_CLIV", 7))
1451 return darwin_sections[objc_instance_vars_section];
1453 else if (!strncmp (p, "V1_CLCM", 7))
1454 return darwin_sections[objc_cls_meth_section];
1455 else if (!strncmp (p, "V1_CLIM", 7))
1456 return darwin_sections[objc_inst_meth_section];
1457 else if (!strncmp (p, "V1_CACM", 7))
1458 return darwin_sections[objc_cat_cls_meth_section];
1459 else if (!strncmp (p, "V1_CAIM", 7))
1460 return darwin_sections[objc_cat_inst_meth_section];
1461 else if (!strncmp (p, "V1_PNSM", 7))
1462 return darwin_sections[objc_cat_inst_meth_section];
1463 else if (!strncmp (p, "V1_PCLM", 7))
1464 return darwin_sections[objc_cat_cls_meth_section];
1466 else if (!strncmp (p, "V1_CLPR", 7))
1467 return darwin_sections[objc_cat_cls_meth_section];
1468 else if (!strncmp (p, "V1_CAPR", 7))
1469 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1471 else if (!strncmp (p, "V1_PRFS", 7))
1472 return darwin_sections[objc_cat_cls_meth_section];
1473 else if (!strncmp (p, "V1_CLRF", 7))
1474 return darwin_sections[objc_cls_refs_section];
1475 else if (!strncmp (p, "V1_SRFS", 7))
1476 return darwin_sections[objc_selector_refs_section];
1478 else if (!strncmp (p, "V1_MODU", 7))
1479 return darwin_sections[objc_module_info_section];
1480 else if (!strncmp (p, "V1_SYMT", 7))
1481 return darwin_sections[objc_symbols_section];
1482 else if (!strncmp (p, "V1_INFO", 7))
1483 return darwin_sections[objc_image_info_section];
1485 else if (!strncmp (p, "V1_PLST", 7))
1486 return darwin_sections[objc1_prop_list_section];
1487 else if (!strncmp (p, "V1_PEXT", 7))
1488 return darwin_sections[objc1_protocol_ext_section];
1489 else if (!strncmp (p, "V1_CEXT", 7))
1490 return darwin_sections[objc1_class_ext_section];
1492 else if (!strncmp (p, "V2_CSTR", 7))
1493 return darwin_sections[objc_constant_string_object_section];
1495 return base;
1498 section *
1499 machopic_select_section (tree decl,
1500 int reloc,
1501 unsigned HOST_WIDE_INT align)
1503 bool zsize, one, weak, ro;
1504 section *base_section = NULL;
1506 weak = (DECL_P (decl)
1507 && DECL_WEAK (decl)
1508 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1510 zsize = (DECL_P (decl)
1511 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1512 && tree_low_cst (DECL_SIZE_UNIT (decl), 1) == 0);
1514 one = DECL_P (decl)
1515 && TREE_CODE (decl) == VAR_DECL
1516 && DECL_ONE_ONLY (decl);
1518 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1520 switch (categorize_decl_for_section (decl, reloc))
1522 case SECCAT_TEXT:
1523 gcc_unreachable ();
1524 break;
1526 case SECCAT_RODATA:
1527 case SECCAT_SRODATA:
1528 base_section = darwin_rodata_section (weak, zsize);
1529 break;
1531 case SECCAT_RODATA_MERGE_STR:
1532 base_section = darwin_mergeable_string_section (decl, align);
1533 break;
1535 case SECCAT_RODATA_MERGE_STR_INIT:
1536 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1537 break;
1539 case SECCAT_RODATA_MERGE_CONST:
1540 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1541 break;
1543 case SECCAT_DATA:
1544 case SECCAT_DATA_REL:
1545 case SECCAT_DATA_REL_LOCAL:
1546 case SECCAT_DATA_REL_RO:
1547 case SECCAT_DATA_REL_RO_LOCAL:
1548 case SECCAT_SDATA:
1549 case SECCAT_TDATA:
1550 if (weak || one)
1552 if (ro)
1553 base_section = darwin_sections[const_data_coal_section];
1554 else
1555 base_section = darwin_sections[data_coal_section];
1557 else if (DARWIN_SECTION_ANCHORS
1558 && flag_section_anchors
1559 && zsize)
1561 /* If we're doing section anchors, then punt zero-sized objects into
1562 their own sections so that they don't interfere with offset
1563 computation for the remaining vars. This does not need to be done
1564 for stuff in mergeable sections, since these are ineligible for
1565 anchors. */
1566 if (ro)
1567 base_section = darwin_sections[zobj_const_data_section];
1568 else
1569 base_section = darwin_sections[zobj_data_section];
1571 else if (ro)
1572 base_section = darwin_sections[const_data_section];
1573 else
1574 base_section = data_section;
1575 break;
1576 case SECCAT_BSS:
1577 case SECCAT_SBSS:
1578 case SECCAT_TBSS:
1579 if (weak || one)
1580 base_section = darwin_sections[data_coal_section];
1581 else
1583 if (!TREE_PUBLIC (decl))
1584 base_section = lcomm_section;
1585 else if (bss_noswitch_section)
1586 base_section = bss_noswitch_section;
1587 else
1588 base_section = data_section;
1590 break;
1592 default:
1593 gcc_unreachable ();
1596 /* Darwin weird special cases.
1597 a) OBJC Meta-data. */
1598 if (DECL_P (decl)
1599 && (TREE_CODE (decl) == VAR_DECL
1600 || TREE_CODE (decl) == CONST_DECL)
1601 && DECL_ATTRIBUTES (decl))
1603 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1604 if (meta)
1605 return darwin_objc2_section (decl, meta, base_section);
1606 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1607 if (meta)
1608 return darwin_objc1_section (decl, meta, base_section);
1609 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1610 if (meta)
1611 return base_section; /* GNU runtime is happy with it all in one pot. */
1614 /* b) Constant string objects. */
1615 if (TREE_CODE (decl) == CONSTRUCTOR
1616 && TREE_TYPE (decl)
1617 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1618 && TYPE_NAME (TREE_TYPE (decl)))
1620 tree name = TYPE_NAME (TREE_TYPE (decl));
1621 if (TREE_CODE (name) == TYPE_DECL)
1622 name = DECL_NAME (name);
1624 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1626 if (flag_next_runtime)
1628 if (flag_objc_abi == 2)
1629 return darwin_sections[objc2_constant_string_object_section];
1630 else
1631 return darwin_sections[objc_constant_string_object_section];
1633 else
1634 return darwin_sections[objc_string_object_section];
1636 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1637 return darwin_sections[cfstring_constant_object_section];
1638 else
1639 return base_section;
1641 /* c) legacy meta-data selection. */
1642 else if (TREE_CODE (decl) == VAR_DECL
1643 && DECL_NAME (decl)
1644 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1645 && IDENTIFIER_POINTER (DECL_NAME (decl))
1646 && flag_next_runtime
1647 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1649 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1650 static bool warned_objc_46 = false;
1651 /* We shall assert that zero-sized objects are an error in ObjC
1652 meta-data. */
1653 gcc_assert (tree_low_cst (DECL_SIZE_UNIT (decl), 1) != 0);
1655 /* ??? This mechanism for determining the metadata section is
1656 broken when LTO is in use, since the frontend that generated
1657 the data is not identified. We will keep the capability for
1658 the short term - in case any non-Objective-C programs are using
1659 it to place data in specified sections. */
1660 if (!warned_objc_46)
1662 location_t loc = DECL_SOURCE_LOCATION (decl);
1663 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
1664 " to select meta-data sections is deprecated at 4.6"
1665 " and will be removed in 4.7");
1666 warned_objc_46 = true;
1669 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1670 return darwin_sections[objc_cls_meth_section];
1671 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1672 return darwin_sections[objc_inst_meth_section];
1673 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
1674 return darwin_sections[objc_cat_cls_meth_section];
1675 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
1676 return darwin_sections[objc_cat_inst_meth_section];
1677 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1678 return darwin_sections[objc_class_vars_section];
1679 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1680 return darwin_sections[objc_instance_vars_section];
1681 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1682 return darwin_sections[objc_cat_cls_meth_section];
1683 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1684 return darwin_sections[objc_class_names_section];
1685 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1686 return darwin_sections[objc_meth_var_names_section];
1687 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1688 return darwin_sections[objc_meth_var_types_section];
1689 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1690 return darwin_sections[objc_cls_refs_section];
1691 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1692 return darwin_sections[objc_class_section];
1693 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1694 return darwin_sections[objc_meta_class_section];
1695 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1696 return darwin_sections[objc_category_section];
1697 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1698 return darwin_sections[objc_selector_refs_section];
1699 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1700 return darwin_sections[objc_selector_fixup_section];
1701 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1702 return darwin_sections[objc_symbols_section];
1703 else if (!strncmp (name, "_OBJC_MODULES", 13))
1704 return darwin_sections[objc_module_info_section];
1705 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1706 return darwin_sections[objc_image_info_section];
1707 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1708 return darwin_sections[objc_cat_inst_meth_section];
1709 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1710 return darwin_sections[objc_cat_cls_meth_section];
1711 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1712 return darwin_sections[objc_cat_cls_meth_section];
1713 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1714 return darwin_sections[objc_protocol_section];
1715 else
1716 return base_section;
1719 return base_section;
1722 /* This can be called with address expressions as "rtx".
1723 They must go in "const". */
1725 section *
1726 machopic_select_rtx_section (enum machine_mode mode, rtx x,
1727 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1729 if (GET_MODE_SIZE (mode) == 8
1730 && (GET_CODE (x) == CONST_INT
1731 || GET_CODE (x) == CONST_DOUBLE))
1732 return darwin_sections[literal8_section];
1733 else if (GET_MODE_SIZE (mode) == 4
1734 && (GET_CODE (x) == CONST_INT
1735 || GET_CODE (x) == CONST_DOUBLE))
1736 return darwin_sections[literal4_section];
1737 else if (HAVE_GAS_LITERAL16
1738 && TARGET_64BIT
1739 && GET_MODE_SIZE (mode) == 16
1740 && (GET_CODE (x) == CONST_INT
1741 || GET_CODE (x) == CONST_DOUBLE
1742 || GET_CODE (x) == CONST_VECTOR))
1743 return darwin_sections[literal16_section];
1744 else if (MACHOPIC_INDIRECT
1745 && (GET_CODE (x) == SYMBOL_REF
1746 || GET_CODE (x) == CONST
1747 || GET_CODE (x) == LABEL_REF))
1748 return darwin_sections[const_data_section];
1749 else
1750 return darwin_sections[const_section];
1753 void
1754 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1756 cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
1758 vec_safe_push (ctors, new_elt);
1760 if (! MACHOPIC_INDIRECT)
1761 fprintf (asm_out_file, ".reference .constructors_used\n");
1764 void
1765 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1767 cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)};
1769 vec_safe_push (dtors, new_elt);
1771 if (! MACHOPIC_INDIRECT)
1772 fprintf (asm_out_file, ".reference .destructors_used\n");
1775 static int
1776 sort_cdtor_records (const void * a, const void * b)
1778 const cdtor_record *cda = (const cdtor_record *)a;
1779 const cdtor_record *cdb = (const cdtor_record *)b;
1780 if (cda->priority > cdb->priority)
1781 return 1;
1782 if (cda->priority < cdb->priority)
1783 return -1;
1784 if (cda->position > cdb->position)
1785 return 1;
1786 if (cda->position < cdb->position)
1787 return -1;
1788 return 0;
1791 static void
1792 finalize_ctors ()
1794 unsigned int i;
1795 cdtor_record *elt;
1797 if (MACHOPIC_INDIRECT)
1798 switch_to_section (darwin_sections[mod_init_section]);
1799 else
1800 switch_to_section (darwin_sections[constructor_section]);
1802 if (vec_safe_length (ctors) > 1)
1803 ctors->qsort (sort_cdtor_records);
1804 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
1806 assemble_align (POINTER_SIZE);
1807 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1811 static void
1812 finalize_dtors ()
1814 unsigned int i;
1815 cdtor_record *elt;
1817 if (MACHOPIC_INDIRECT)
1818 switch_to_section (darwin_sections[mod_term_section]);
1819 else
1820 switch_to_section (darwin_sections[destructor_section]);
1822 if (vec_safe_length (dtors) > 1)
1823 dtors->qsort (sort_cdtor_records);
1824 FOR_EACH_VEC_SAFE_ELT (dtors, i, elt)
1826 assemble_align (POINTER_SIZE);
1827 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1831 void
1832 darwin_globalize_label (FILE *stream, const char *name)
1834 if (!!strncmp (name, "_OBJC_", 6))
1835 default_globalize_label (stream, name);
1838 /* This routine returns non-zero if 'name' starts with the special objective-c
1839 anonymous file-scope static name. It accommodates c++'s mangling of such
1840 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1842 int
1843 darwin_label_is_anonymous_local_objc_name (const char *name)
1845 const unsigned char *p = (const unsigned char *) name;
1846 if (*p != '_')
1847 return 0;
1848 if (p[1] == 'Z' && p[2] == 'L')
1850 p += 3;
1851 while (*p >= '0' && *p <= '9')
1852 p++;
1854 return (!strncmp ((const char *)p, "_OBJC_", 6));
1857 /* LTO support for Mach-O.
1859 This version uses three mach-o sections to encapsulate the (unlimited
1860 number of) lto sections.
1862 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1863 __GNU_LTO, __section_names contains the GNU LTO section names.
1864 __GNU_LTO, __section_index contains an array of values that index these.
1866 Indexed thus:
1867 <section offset from the start of __GNU_LTO, __lto_sections>,
1868 <section length>
1869 <name offset from the start of __GNU_LTO, __section_names,
1870 <name length>.
1872 At present, for both m32 and m64 mach-o files each of these fields is
1873 represented by a uint32_t. This is because, AFAICT, a mach-o object
1874 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1876 uint32_t offset;
1877 "offset An integer specifying the offset to this section in the file." */
1879 /* Count lto section numbers. */
1880 static unsigned int lto_section_num = 0;
1882 /* A vector of information about LTO sections, at present, we only have
1883 the name. TODO: see if we can get the data length somehow. */
1884 typedef struct GTY (()) darwin_lto_section_e {
1885 const char *sectname;
1886 } darwin_lto_section_e ;
1888 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1890 /* Segment for LTO data. */
1891 #define LTO_SEGMENT_NAME "__GNU_LTO"
1893 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1894 sections into three Mach-O ones).
1895 NOTE: These names MUST be kept in sync with those in
1896 libiberty/simple-object-mach-o. */
1897 #define LTO_SECTS_SECTION "__wrapper_sects"
1898 #define LTO_NAMES_SECTION "__wrapper_names"
1899 #define LTO_INDEX_SECTION "__wrapper_index"
1901 /* File to temporarily store LTO data. This is appended to asm_out_file
1902 in darwin_end_file. */
1903 static FILE *lto_asm_out_file, *saved_asm_out_file;
1904 static char *lto_asm_out_name;
1906 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1907 asm_out_file and switching to an alternative output file. */
1908 void
1909 darwin_asm_lto_start (void)
1911 gcc_assert (! saved_asm_out_file);
1912 saved_asm_out_file = asm_out_file;
1913 if (! lto_asm_out_name)
1914 lto_asm_out_name = make_temp_file (".lto.s");
1915 lto_asm_out_file = fopen (lto_asm_out_name, "a");
1916 if (lto_asm_out_file == NULL)
1917 fatal_error ("failed to open temporary file %s for LTO output",
1918 lto_asm_out_name);
1919 asm_out_file = lto_asm_out_file;
1922 /* Restore asm_out_file. */
1923 void
1924 darwin_asm_lto_end (void)
1926 gcc_assert (saved_asm_out_file);
1927 fclose (lto_asm_out_file);
1928 asm_out_file = saved_asm_out_file;
1929 saved_asm_out_file = NULL;
1932 static void
1933 darwin_asm_dwarf_section (const char *name, unsigned int flags, tree decl);
1935 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
1937 void
1938 darwin_asm_named_section (const char *name,
1939 unsigned int flags,
1940 tree decl ATTRIBUTE_UNUSED)
1942 /* LTO sections go in a special section that encapsulates the (unlimited)
1943 number of GNU LTO sections within a single mach-o one. */
1944 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1945 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1947 darwin_lto_section_e e;
1948 /* We expect certain flags to be set... */
1949 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1950 == (SECTION_DEBUG | SECTION_NAMED));
1952 /* Switch to our combined section. */
1953 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1954 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1955 /* Output a label for the start of this sub-section. */
1956 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1957 lto_section_num, name);
1958 /* We have to jump through hoops to get the values of the intra-section
1959 offsets... */
1960 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1961 lto_section_num, lto_section_num);
1962 fprintf (asm_out_file,
1963 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1964 lto_section_num, lto_section_num+1, lto_section_num);
1965 lto_section_num++;
1966 e.sectname = xstrdup (name);
1967 /* Keep the names, we'll need to make a table later.
1968 TODO: check that we do not revisit sections, that would break
1969 the assumption of how this is done. */
1970 if (lto_section_names == NULL)
1971 vec_alloc (lto_section_names, 16);
1972 vec_safe_push (lto_section_names, e);
1974 else if (strncmp (name, "__DWARF,", 8) == 0)
1975 darwin_asm_dwarf_section (name, flags, decl);
1976 else
1977 fprintf (asm_out_file, "\t.section %s\n", name);
1980 void
1981 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
1983 /* Darwin does not use unique sections. */
1986 /* Handle __attribute__ ((apple_kext_compatibility)).
1987 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
1988 vtable for classes with this attribute (and their descendants) by not
1989 outputting the new 3.0 nondeleting destructor. This means that such
1990 objects CANNOT be allocated on the stack or as globals UNLESS they have
1991 a completely empty `operator delete'.
1992 Luckily, this fits in with the Darwin kext model.
1994 This attribute also disables gcc3's potential overlaying of derived
1995 class data members on the padding at the end of the base class. */
1997 tree
1998 darwin_handle_kext_attribute (tree *node, tree name,
1999 tree args ATTRIBUTE_UNUSED,
2000 int flags ATTRIBUTE_UNUSED,
2001 bool *no_add_attrs)
2003 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
2004 if (! TARGET_KEXTABI)
2006 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2007 "only when compiling a kext", name);
2009 *no_add_attrs = true;
2011 else if (TREE_CODE (*node) != RECORD_TYPE)
2013 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2014 "only to C++ classes", name);
2016 *no_add_attrs = true;
2019 return NULL_TREE;
2022 /* Handle a "weak_import" attribute; arguments as in
2023 struct attribute_spec.handler. */
2025 tree
2026 darwin_handle_weak_import_attribute (tree *node, tree name,
2027 tree ARG_UNUSED (args),
2028 int ARG_UNUSED (flags),
2029 bool * no_add_attrs)
2031 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
2033 warning (OPT_Wattributes, "%qE attribute ignored",
2034 name);
2035 *no_add_attrs = true;
2037 else
2038 declare_weak (*node);
2040 return NULL_TREE;
2043 /* Emit a label for an FDE, making it global and/or weak if appropriate.
2044 The third parameter is nonzero if this is for exception handling.
2045 The fourth parameter is nonzero if this is just a placeholder for an
2046 FDE that we are omitting. */
2048 void
2049 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
2051 char *lab ;
2052 char buf[32];
2053 static int invok_count = 0;
2054 static tree last_fun_decl = NULL_TREE;
2056 /* We use the linker to emit the .eh labels for Darwin 9 and above. */
2057 if (! for_eh || generating_for_darwin_version >= 9)
2058 return;
2060 /* FIXME: This only works when the eh for all sections of a function is
2061 emitted at the same time. If that changes, we would need to use a lookup
2062 table of some form to determine what to do. Also, we should emit the
2063 unadorned label for the partition containing the public label for a
2064 function. This is of limited use, probably, since we do not currently
2065 enable partitioning. */
2066 strcpy (buf, ".eh");
2067 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
2069 if (decl == last_fun_decl)
2071 invok_count++;
2072 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
2074 else
2076 last_fun_decl = decl;
2077 invok_count = 0;
2081 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
2083 if (TREE_PUBLIC (decl))
2085 targetm.asm_out.globalize_label (file, lab);
2086 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
2088 fputs ("\t.private_extern ", file);
2089 assemble_name (file, lab);
2090 fputc ('\n', file);
2094 if (DECL_WEAK (decl))
2096 fputs ("\t.weak_definition ", file);
2097 assemble_name (file, lab);
2098 fputc ('\n', file);
2101 assemble_name (file, lab);
2102 if (empty)
2104 fputs (" = 0\n", file);
2106 /* Mark the absolute .eh and .eh1 style labels as needed to
2107 ensure that we don't dead code strip them and keep such
2108 labels from another instantiation point until we can fix this
2109 properly with group comdat support. */
2110 darwin_mark_decl_preserved (lab);
2112 else
2113 fputs (":\n", file);
2115 free (lab);
2118 static GTY(()) unsigned long except_table_label_num;
2120 void
2121 darwin_emit_except_table_label (FILE *file)
2123 char section_start_label[30];
2125 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2126 except_table_label_num++);
2127 ASM_OUTPUT_LABEL (file, section_start_label);
2129 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2131 void
2132 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2134 const char *nlp_name;
2136 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2138 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2139 fputs ("\t.long\t", file);
2140 ASM_OUTPUT_LABELREF (file, nlp_name);
2141 fputs ("-.", file);
2144 /* If this is uncommented, details of each allocation will be printed
2145 in the asm right before the actual code. WARNING - this will cause some
2146 test-suite fails (since the printout will contain items that some tests
2147 are not expecting) -- so don't leave it on by default (it bloats the
2148 asm too). */
2149 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2151 /* The first two of these routines are ostensibly just intended to put
2152 names into the asm. However, they are both hijacked in order to ensure
2153 that zero-sized items do not make their way into the output. Consequently,
2154 we also need to make these participate in provisions for dealing with
2155 such items in section anchors. */
2157 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2158 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2159 but it does need to be referenced via indirect PIC data pointers.
2160 The machopic_define_symbol calls are telling the machopic subsystem
2161 that the name *is* defined in this module, so it doesn't need to
2162 make them indirect. */
2163 void
2164 darwin_asm_declare_object_name (FILE *file,
2165 const char *nam, tree decl)
2167 const char *xname = nam;
2168 unsigned HOST_WIDE_INT size;
2169 bool local_def, weak;
2171 weak = (DECL_P (decl)
2172 && DECL_WEAK (decl)
2173 && !lookup_attribute ("weak_import",
2174 DECL_ATTRIBUTES (decl)));
2176 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2177 && (!DECL_COMMON (decl)
2178 || !TREE_PUBLIC (decl)));
2180 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2181 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2183 if (local_def)
2185 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2186 if (!weak)
2187 machopic_define_symbol (DECL_RTL (decl));
2190 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
2192 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2193 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2194 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2195 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2196 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2197 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2198 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2199 (unsigned long)DECL_INITIAL (decl));
2200 #endif
2202 /* Darwin needs help to support local zero-sized objects.
2203 They must be made at least one byte, and the section containing must be
2204 marked as unsuitable for section-anchors (see storage allocators below).
2206 For non-zero objects this output is handled by varasm.c.
2208 if (!size)
2210 unsigned int l2align = 0;
2212 /* The align must be honored, even for zero-sized. */
2213 if (DECL_ALIGN (decl))
2215 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2216 fprintf (file, "\t.align\t%u\n", l2align);
2219 ASM_OUTPUT_LABEL (file, xname);
2220 size = 1;
2221 fprintf (file, "\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2223 /* Check that we've correctly picked up the zero-sized item and placed it
2224 properly. */
2225 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2226 || (in_section
2227 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2229 else
2230 ASM_OUTPUT_LABEL (file, xname);
2233 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2234 void
2235 darwin_asm_declare_constant_name (FILE *file, const char *name,
2236 const_tree exp ATTRIBUTE_UNUSED,
2237 HOST_WIDE_INT size)
2239 assemble_label (file, name);
2240 /* As for other items, we need at least one byte. */
2241 if (!size)
2243 fputs ("\t.space\t1\n", file);
2244 /* Check that we've correctly picked up the zero-sized item and placed it
2245 properly. */
2246 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2247 || (in_section
2248 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2252 /* Darwin storage allocators.
2254 Zerofill sections are desirable for large blank data since, otherwise, these
2255 data bloat objects (PR33210).
2257 However, section anchors don't work in .zerofill sections (one cannot switch
2258 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2259 to put (at least some) data into 'normal' switchable sections.
2261 Here we set a relatively arbitrary value for the size of an object to trigger
2262 zerofill when section anchors are enabled (anything bigger than a page for
2263 current Darwin implementations). FIXME: there ought to be some objective way
2264 to make this choice.
2266 When section anchor are off this is ignored anyway. */
2268 #define BYTES_ZFILL 4096
2270 /* Emit a chunk of data for items coalesced by the linker. */
2271 static void
2272 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2273 unsigned HOST_WIDE_INT size,
2274 unsigned int align)
2276 /* Since the sections used here are coalesed, they will not be eligible
2277 for section anchors, and therefore we don't need to break that out. */
2278 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2279 switch_to_section (darwin_sections[const_data_coal_section]);
2280 else
2281 switch_to_section (darwin_sections[data_coal_section]);
2283 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2284 the align info for zero-sized items... but do it here otherwise. */
2285 if (size && align)
2286 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2288 if (TREE_PUBLIC (decl))
2289 darwin_globalize_label (fp, name);
2291 /* ... and we let it deal with outputting one byte of zero for them too. */
2292 darwin_asm_declare_object_name (fp, name, decl);
2293 if (size)
2294 assemble_zeros (size);
2297 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2298 static void
2299 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2300 unsigned HOST_WIDE_INT size,
2301 unsigned int align, tree meta)
2303 section *ocs = data_section;
2305 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2306 ocs = darwin_objc2_section (decl, meta, ocs);
2307 else
2308 ocs = darwin_objc1_section (decl, meta, ocs);
2310 switch_to_section (ocs);
2312 /* We shall declare that zero-sized meta-data are not valid (yet). */
2313 gcc_assert (size);
2314 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2316 /* ... and we let it deal with outputting one byte of zero for them too. */
2317 darwin_asm_declare_object_name (fp, name, decl);
2318 assemble_zeros (size);
2321 /* This routine emits 'local' storage:
2323 When Section Anchors are off this routine emits .zerofill commands in
2324 sections named for their alignment.
2326 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2327 the .static_data section so that the section anchoring system can see them.
2328 Larger items are still placed in .zerofill sections, addressing PR33210.
2329 The routine has no checking - it is all assumed to be done by the caller.
2331 static void
2332 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2333 unsigned HOST_WIDE_INT size,
2334 unsigned int l2align)
2336 /* FIXME: We have a fudge to make this work with Java even when the target does
2337 not use sections anchors -- Java seems to need at least one small item in a
2338 non-zerofill segment. */
2339 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2340 || (size && size <= 2))
2342 /* Put smaller objects in _static_data, where the section anchors system
2343 can get them.
2344 However, if they are zero-sized punt them to yet a different section
2345 (that is not allowed to participate in anchoring). */
2346 if (!size)
2348 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2349 in_section = darwin_sections[zobj_bss_section];
2350 size = 1;
2352 else
2354 fputs ("\t.static_data\n", fp);
2355 in_section = darwin_sections[static_data_section];
2358 if (l2align)
2359 fprintf (fp, "\t.align\t%u\n", l2align);
2361 assemble_name (fp, name);
2362 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2364 else
2366 /* When we are on a non-section anchor target, we can get zero-sized
2367 items here. However, all we need to do is to bump them to one byte
2368 and the section alignment will take care of the rest. */
2369 char secnam[64];
2370 unsigned int flags ;
2371 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
2372 (unsigned) l2align);
2373 /* We can't anchor (yet, if ever) in zerofill sections, because we can't
2374 switch to them and emit a label. */
2375 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2376 in_section = get_section (secnam, flags, NULL);
2377 fprintf (fp, "\t.zerofill %s,", secnam);
2378 assemble_name (fp, name);
2379 if (!size)
2380 size = 1;
2382 if (l2align)
2383 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2384 size, (unsigned) l2align);
2385 else
2386 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2389 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2390 /* This is defined as a file-scope var, so we know to notify machopic. */
2391 machopic_define_symbol (DECL_RTL (decl));
2394 /* Emit a chunk of common. */
2395 static void
2396 darwin_emit_common (FILE *fp, const char *name,
2397 unsigned HOST_WIDE_INT size, unsigned int align)
2399 unsigned HOST_WIDE_INT rounded;
2400 unsigned int l2align;
2402 /* Earlier systems complain if the alignment exceeds the page size.
2403 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2404 if (!emit_aligned_common && (align > 32768UL))
2405 align = 4096UL; /* In units. */
2406 else
2407 align /= BITS_PER_UNIT;
2409 /* Make sure we have a meaningful align. */
2410 if (!align)
2411 align = 1;
2413 /* For earlier toolchains, we need to emit the var as a rounded size to
2414 tell ld the alignment. */
2415 if (size < align)
2416 rounded = align;
2417 else
2418 rounded = (size + (align-1)) & ~(align-1);
2420 l2align = floor_log2 (align);
2421 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2423 in_section = comm_section;
2424 /* We mustn't allow multiple public symbols to share an address when using
2425 the normal OSX toolchain. */
2426 if (!size)
2428 /* Put at least one byte. */
2429 size = 1;
2430 /* This section can no longer participate in section anchoring. */
2431 comm_section->common.flags |= SECTION_NO_ANCHOR;
2434 fputs ("\t.comm\t", fp);
2435 assemble_name (fp, name);
2436 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2437 emit_aligned_common?size:rounded);
2438 if (l2align && emit_aligned_common)
2439 fprintf (fp, ",%u", l2align);
2440 fputs ("\n", fp);
2443 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2444 or coalescable data sections (for weak or comdat) as appropriate. */
2446 void
2447 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2448 unsigned HOST_WIDE_INT size, unsigned int align)
2450 unsigned int l2align;
2451 bool one, pub, weak;
2452 tree meta;
2454 pub = TREE_PUBLIC (decl);
2455 one = DECL_ONE_ONLY (decl);
2456 weak = (DECL_P (decl)
2457 && DECL_WEAK (decl)
2458 && !lookup_attribute ("weak_import",
2459 DECL_ATTRIBUTES (decl)));
2461 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2462 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2463 " pub %d weak %d one %d init %lx\n",
2464 name, (long long)size, (int)align, TREE_READONLY (decl),
2465 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2466 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2467 #endif
2469 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2470 before the target has a chance to comment. */
2471 if ((meta = is_objc_metadata (decl)))
2473 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2474 return;
2477 /* Check that any initializer is valid. */
2478 gcc_assert ((DECL_INITIAL (decl) == NULL)
2479 || (DECL_INITIAL (decl) == error_mark_node)
2480 || initializer_zerop (DECL_INITIAL (decl)));
2482 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2483 gcc_assert (!DECL_COMMON (decl));
2485 /* Pick up the correct alignment. */
2486 if (!size || !align)
2487 align = DECL_ALIGN (decl);
2489 l2align = floor_log2 (align / BITS_PER_UNIT);
2490 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2492 last_assemble_variable_decl = decl;
2494 /* We would rather not have to check this here - but it seems that we might
2495 be passed a decl that should be in coalesced space. */
2496 if (one || weak)
2498 /* Weak or COMDAT objects are put in mergeable sections. */
2499 darwin_emit_weak_or_comdat (fp, decl, name, size,
2500 DECL_ALIGN (decl));
2501 return;
2504 /* If this is not public, then emit according to local rules. */
2505 if (!pub)
2507 darwin_emit_local_bss (fp, decl, name, size, l2align);
2508 return;
2511 /* So we have a public symbol (small item fudge for Java, see above). */
2512 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2513 || (size && size <= 2))
2515 /* Put smaller objects in data, where the section anchors system can get
2516 them. However, if they are zero-sized punt them to yet a different
2517 section (that is not allowed to participate in anchoring). */
2518 if (!size)
2520 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2521 in_section = darwin_sections[zobj_data_section];
2522 size = 1;
2524 else
2526 fputs ("\t.data\n", fp);
2527 in_section = data_section;
2530 if (l2align)
2531 fprintf (fp, "\t.align\t%u\n", l2align);
2533 assemble_name (fp, name);
2534 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2536 else
2538 char secnam[64];
2539 unsigned int flags ;
2540 /* When we are on a non-section anchor target, we can get zero-sized
2541 items here. However, all we need to do is to bump them to one byte
2542 and the section alignment will take care of the rest. */
2543 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
2545 /* We can't anchor in zerofill sections, because we can't switch
2546 to them and emit a label. */
2547 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2548 in_section = get_section (secnam, flags, NULL);
2549 fprintf (fp, "\t.zerofill %s,", secnam);
2550 assemble_name (fp, name);
2551 if (!size)
2552 size = 1;
2554 if (l2align)
2555 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2556 else
2557 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2559 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2562 /* Output a chunk of common, with alignment specified (where the target
2563 supports this). */
2564 void
2565 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2566 unsigned HOST_WIDE_INT size,
2567 unsigned int align)
2569 unsigned int l2align;
2570 bool one, weak;
2571 tree meta;
2573 /* No corresponding var. */
2574 if (decl==NULL)
2576 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2577 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2578 #endif
2579 darwin_emit_common (fp, name, size, align);
2580 return;
2583 one = DECL_ONE_ONLY (decl);
2584 weak = (DECL_P (decl)
2585 && DECL_WEAK (decl)
2586 && !lookup_attribute ("weak_import",
2587 DECL_ATTRIBUTES (decl)));
2589 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2590 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2591 " weak %d one %d init %lx\n",
2592 name, (long long)size, (int)align, TREE_READONLY (decl),
2593 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2594 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2595 #endif
2597 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2598 before the target has a chance to comment. */
2599 if ((meta = is_objc_metadata (decl)))
2601 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2602 return;
2605 /* We shouldn't be messing with this if the decl has a section name. */
2606 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2608 /* We would rather not have to check this here - but it seems that we might
2609 be passed a decl that should be in coalesced space. */
2610 if (one || weak)
2612 /* Weak or COMDAT objects are put in mergable sections. */
2613 darwin_emit_weak_or_comdat (fp, decl, name, size,
2614 DECL_ALIGN (decl));
2615 return;
2618 /* We should only get here for DECL_COMMON, with a zero init (and, in
2619 principle, only for public symbols too - although we deal with local
2620 ones below). */
2622 /* Check the initializer is OK. */
2623 gcc_assert (DECL_COMMON (decl)
2624 && ((DECL_INITIAL (decl) == NULL)
2625 || (DECL_INITIAL (decl) == error_mark_node)
2626 || initializer_zerop (DECL_INITIAL (decl))));
2628 last_assemble_variable_decl = decl;
2630 if (!size || !align)
2631 align = DECL_ALIGN (decl);
2633 l2align = floor_log2 (align / BITS_PER_UNIT);
2634 /* Check we aren't asking for more aligment than the platform allows. */
2635 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2637 if (TREE_PUBLIC (decl) != 0)
2638 darwin_emit_common (fp, name, size, align);
2639 else
2640 darwin_emit_local_bss (fp, decl, name, size, l2align);
2643 /* Output a chunk of BSS with alignment specfied. */
2644 void
2645 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2646 unsigned HOST_WIDE_INT size,
2647 unsigned int align)
2649 unsigned long l2align;
2650 bool one, weak;
2651 tree meta;
2653 one = DECL_ONE_ONLY (decl);
2654 weak = (DECL_P (decl)
2655 && DECL_WEAK (decl)
2656 && !lookup_attribute ("weak_import",
2657 DECL_ATTRIBUTES (decl)));
2659 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2660 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2661 " weak %d init %lx\n",
2662 name, (long long)size, (int)align, TREE_READONLY (decl),
2663 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2664 weak , (unsigned long)DECL_INITIAL (decl));
2665 #endif
2667 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2668 before the target has a chance to comment. */
2669 if ((meta = is_objc_metadata (decl)))
2671 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2672 return;
2675 /* We shouldn't be messing with this if the decl has a section name. */
2676 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2678 /* We would rather not have to check this here - but it seems that we might
2679 be passed a decl that should be in coalesced space. */
2680 if (one || weak)
2682 /* Weak or COMDAT objects are put in mergable sections. */
2683 darwin_emit_weak_or_comdat (fp, decl, name, size,
2684 DECL_ALIGN (decl));
2685 return;
2688 /* .. and it should be suitable for placement in local mem. */
2689 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2690 /* .. and any initializer must be all-zero. */
2691 gcc_assert ((DECL_INITIAL (decl) == NULL)
2692 || (DECL_INITIAL (decl) == error_mark_node)
2693 || initializer_zerop (DECL_INITIAL (decl)));
2695 last_assemble_variable_decl = decl;
2697 if (!size || !align)
2698 align = DECL_ALIGN (decl);
2700 l2align = floor_log2 (align / BITS_PER_UNIT);
2701 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2703 darwin_emit_local_bss (fp, decl, name, size, l2align);
2706 /* Emit an assembler directive to set visibility for a symbol. The
2707 only supported visibilities are VISIBILITY_DEFAULT and
2708 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2709 extern". There is no MACH-O equivalent of ELF's
2710 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2712 void
2713 darwin_assemble_visibility (tree decl, int vis)
2715 if (vis == VISIBILITY_DEFAULT)
2717 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2719 fputs ("\t.private_extern ", asm_out_file);
2720 assemble_name (asm_out_file,
2721 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2722 fputs ("\n", asm_out_file);
2724 else
2725 warning (OPT_Wattributes, "protected visibility attribute "
2726 "not supported in this configuration; ignored");
2729 /* vec used by darwin_asm_dwarf_section.
2730 Maybe a hash tab would be better here - but the intention is that this is
2731 a very short list (fewer than 16 items) and each entry should (ideally,
2732 eventually) only be presented once.
2734 A structure to hold a dwarf debug section used entry. */
2736 typedef struct GTY(()) dwarf_sect_used_entry {
2737 const char *name;
2738 unsigned count;
2740 dwarf_sect_used_entry;
2743 /* A list of used __DWARF sections. */
2744 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2746 /* This is called when we are asked to assemble a named section and the
2747 name begins with __DWARF,. We keep a list of the section names (without
2748 the __DWARF, prefix) and use this to emit our required start label on the
2749 first switch to each section. */
2751 static void
2752 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2753 tree ARG_UNUSED (decl))
2755 unsigned i;
2756 int namelen;
2757 const char * sname;
2758 dwarf_sect_used_entry *ref;
2759 bool found = false;
2760 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2761 == (SECTION_DEBUG | SECTION_NAMED));
2762 /* We know that the name starts with __DWARF, */
2763 sname = name + 8;
2764 namelen = strchr (sname, ',') - sname;
2765 gcc_assert (namelen);
2766 if (dwarf_sect_names_table == NULL)
2767 vec_alloc (dwarf_sect_names_table, 16);
2768 else
2769 for (i = 0;
2770 dwarf_sect_names_table->iterate (i, &ref);
2771 i++)
2773 if (!ref)
2774 break;
2775 if (!strcmp (ref->name, sname))
2777 found = true;
2778 ref->count++;
2779 break;
2783 fprintf (asm_out_file, "\t.section %s\n", name);
2784 if (!found)
2786 dwarf_sect_used_entry e;
2787 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2788 e.count = 1;
2789 e.name = xstrdup (sname);
2790 vec_safe_push (dwarf_sect_names_table, e);
2794 /* Output a difference of two labels that will be an assembly time
2795 constant if the two labels are local. (.long lab1-lab2 will be
2796 very different if lab1 is at the boundary between two sections; it
2797 will be relocated according to the second section, not the first,
2798 so one ends up with a difference between labels in different
2799 sections, which is bad in the dwarf2 eh context for instance.) */
2801 static int darwin_dwarf_label_counter;
2803 void
2804 darwin_asm_output_dwarf_delta (FILE *file, int size,
2805 const char *lab1, const char *lab2)
2807 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2808 && lab2[0] == '*' && lab2[1] == 'L');
2809 const char *directive = (size == 8 ? ".quad" : ".long");
2811 if (islocaldiff)
2812 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2813 else
2814 fprintf (file, "\t%s\t", directive);
2816 assemble_name_raw (file, lab1);
2817 fprintf (file, "-");
2818 assemble_name_raw (file, lab2);
2819 if (islocaldiff)
2820 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2823 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2824 offsets are not represented using relocs in .o files; either the
2825 section never leaves the .o file, or the linker or other tool is
2826 responsible for parsing the DWARF and updating the offsets. */
2828 void
2829 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2830 section *base)
2832 char sname[64];
2833 int namelen;
2835 gcc_assert (base->common.flags & SECTION_NAMED);
2836 gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
2837 gcc_assert (strchr (base->named.name + 8, ','));
2839 namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
2840 sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
2841 darwin_asm_output_dwarf_delta (file, size, lab, sname);
2844 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2846 void
2847 darwin_file_start (void)
2849 /* Nothing to do. */
2852 /* Called for the TARGET_ASM_FILE_END hook.
2853 Emit the mach-o pic indirection data, the lto data and, finally a flag
2854 to tell the linker that it can break the file object into sections and
2855 move those around for efficiency. */
2857 void
2858 darwin_file_end (void)
2860 if (!vec_safe_is_empty (ctors))
2861 finalize_ctors ();
2862 if (!vec_safe_is_empty (dtors))
2863 finalize_dtors ();
2865 /* If we are expecting to output NeXT ObjC meta-data, (and we actually see
2866 some) then we output the fix-and-continue marker (Image Info).
2867 This applies to Objective C, Objective C++ and LTO with either language
2868 as part of the input. */
2869 if (flag_next_runtime && objc_metadata_seen)
2871 unsigned int flags = 0;
2872 if (flag_objc_abi >= 2)
2874 flags = 16;
2875 output_section_asm_op
2876 (darwin_sections[objc2_image_info_section]->unnamed.data);
2878 else
2879 output_section_asm_op
2880 (darwin_sections[objc_image_info_section]->unnamed.data);
2882 ASM_OUTPUT_ALIGN (asm_out_file, 2);
2883 fputs ("L_OBJC_ImageInfo:\n", asm_out_file);
2885 flags |= (flag_replace_objc_classes && classes_seen) ? 1 : 0;
2886 flags |= flag_objc_gc ? 2 : 0;
2888 fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags);
2891 machopic_finish (asm_out_file);
2892 if (strcmp (lang_hooks.name, "GNU C++") == 0)
2894 switch_to_section (darwin_sections[constructor_section]);
2895 switch_to_section (darwin_sections[destructor_section]);
2896 ASM_OUTPUT_ALIGN (asm_out_file, 1);
2899 /* If there was LTO assembler output, append it to asm_out_file. */
2900 if (lto_asm_out_name)
2902 int n;
2903 char *buf, *lto_asm_txt;
2905 /* Shouldn't be here if we failed to switch back. */
2906 gcc_assert (! saved_asm_out_file);
2908 lto_asm_out_file = fopen (lto_asm_out_name, "r");
2909 if (lto_asm_out_file == NULL)
2910 fatal_error ("failed to open temporary file %s with LTO output",
2911 lto_asm_out_name);
2912 fseek (lto_asm_out_file, 0, SEEK_END);
2913 n = ftell (lto_asm_out_file);
2914 if (n > 0)
2916 fseek (lto_asm_out_file, 0, SEEK_SET);
2917 lto_asm_txt = buf = (char *) xmalloc (n + 1);
2918 while (fgets (lto_asm_txt, n, lto_asm_out_file))
2919 fputs (lto_asm_txt, asm_out_file);
2920 /* Put a termination label. */
2921 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2922 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2923 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2924 lto_section_num);
2925 /* Make sure our termination label stays in this section. */
2926 fputs ("\t.space\t1\n", asm_out_file);
2929 /* Remove the temporary file. */
2930 fclose (lto_asm_out_file);
2931 unlink_if_ordinary (lto_asm_out_name);
2932 free (lto_asm_out_name);
2935 /* Output the names and indices. */
2936 if (lto_section_names && lto_section_names->length ())
2938 int count;
2939 darwin_lto_section_e *ref;
2940 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
2941 the latter up ourselves. */
2942 const char *op = integer_asm_op (4,0);
2944 /* Emit the names. */
2945 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2946 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2947 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2949 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
2950 /* We have to jump through hoops to get the values of the intra-section
2951 offsets... */
2952 fprintf (asm_out_file,
2953 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
2954 count, count);
2955 fprintf (asm_out_file,
2956 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
2957 count, count+1, count);
2958 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
2960 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
2961 /* make sure our termination label stays in this section. */
2962 fputs ("\t.space\t1\n", asm_out_file);
2964 /* Emit the Index. */
2965 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2966 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
2967 fputs ("\t.align\t2\n", asm_out_file);
2968 fputs ("# Section offset, Section length, Name offset, Name length\n",
2969 asm_out_file);
2970 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2972 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
2973 op, count, ref->sectname);
2974 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
2975 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
2976 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
2980 /* If we have section anchors, then we must prevent the linker from
2981 re-arranging data. */
2982 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2983 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
2986 /* TODO: Add a language hook for identifying if a decl is a vtable. */
2987 #define DARWIN_VTABLE_P(DECL) 0
2989 /* Cross-module name binding. Darwin does not support overriding
2990 functions at dynamic-link time, except for vtables in kexts. */
2992 bool
2993 darwin_binds_local_p (const_tree decl)
2995 return default_binds_local_p_1 (decl,
2996 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
2999 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
3000 anchor relative to ".", the current section position. We cannot use
3001 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
3002 void
3003 darwin_asm_output_anchor (rtx symbol)
3005 fprintf (asm_out_file, "\t.set\t");
3006 assemble_name (asm_out_file, XSTR (symbol, 0));
3007 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
3008 SYMBOL_REF_BLOCK_OFFSET (symbol));
3011 /* Disable section anchoring on any section containing a zero-sized
3012 object. */
3013 bool
3014 darwin_use_anchors_for_symbol_p (const_rtx symbol)
3016 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
3018 section *sect;
3019 /* If the section contains a zero-sized object it's ineligible. */
3020 sect = SYMBOL_REF_BLOCK (symbol)->sect;
3021 /* This should have the effect of disabling anchors for vars that follow
3022 any zero-sized one, in a given section. */
3023 if (sect->common.flags & SECTION_NO_ANCHOR)
3024 return false;
3026 /* Also check the normal reasons for suppressing. */
3027 return default_use_anchors_for_symbol_p (symbol);
3029 else
3030 return false;
3033 /* Set the darwin specific attributes on TYPE. */
3034 void
3035 darwin_set_default_type_attributes (tree type)
3037 if (darwin_ms_struct
3038 && TREE_CODE (type) == RECORD_TYPE)
3039 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
3040 NULL_TREE,
3041 TYPE_ATTRIBUTES (type));
3044 /* True, iff we're generating code for loadable kernel extensions. */
3046 bool
3047 darwin_kextabi_p (void) {
3048 return flag_apple_kext;
3051 void
3052 darwin_override_options (void)
3054 /* Keep track of which (major) version we're generating code for. */
3055 if (darwin_macosx_version_min)
3057 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
3058 generating_for_darwin_version = 10;
3059 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
3060 generating_for_darwin_version = 9;
3062 /* Earlier versions are not specifically accounted, until required. */
3065 /* In principle, this should be c-family only. However, we really need to
3066 set sensible defaults for LTO as well, since the section selection stuff
3067 should check for correctness re. the ABI. TODO: check and provide the
3068 flags (runtime & ABI) from the lto wrapper). */
3070 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
3071 if (!global_options_set.x_flag_objc_abi)
3072 global_options.x_flag_objc_abi
3073 = (!flag_next_runtime)
3075 : (TARGET_64BIT ? 2
3076 : (generating_for_darwin_version >= 9) ? 1
3077 : 0);
3079 /* Objective-C family ABI 2 is only valid for next/m64 at present. */
3080 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
3082 if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
3083 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
3084 " used for %<-m64%> targets with"
3085 " %<-fnext-runtime%>");
3086 if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
3087 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
3088 " supported on %<-m32%> targets with"
3089 " %<-fnext-runtime%>");
3092 /* Don't emit DWARF3/4 unless specifically selected. This is a
3093 workaround for tool bugs. */
3094 if (!global_options_set.x_dwarf_strict)
3095 dwarf_strict = 1;
3096 if (!global_options_set.x_dwarf_version)
3097 dwarf_version = 2;
3099 /* Do not allow unwind tables to be generated by default for m32.
3100 fnon-call-exceptions will override this, regardless of what we do. */
3101 if (generating_for_darwin_version < 10
3102 && !global_options_set.x_flag_asynchronous_unwind_tables
3103 && !TARGET_64BIT)
3104 global_options.x_flag_asynchronous_unwind_tables = 0;
3106 /* Disable -freorder-blocks-and-partition when unwind tables are being
3107 emitted for Darwin < 9 (OSX 10.5).
3108 The strategy is, "Unless the User has specifically set/unset an unwind
3109 flag we will switch off -freorder-blocks-and-partition when unwind tables
3110 will be generated". If the User specifically sets flags... we assume
3111 (s)he knows why... */
3112 if (generating_for_darwin_version < 9
3113 && global_options_set.x_flag_reorder_blocks_and_partition
3114 && ((global_options.x_flag_exceptions /* User, c++, java */
3115 && !global_options_set.x_flag_exceptions) /* User specified... */
3116 || (global_options.x_flag_unwind_tables
3117 && !global_options_set.x_flag_unwind_tables)
3118 || (global_options.x_flag_non_call_exceptions
3119 && !global_options_set.x_flag_non_call_exceptions)
3120 || (global_options.x_flag_asynchronous_unwind_tables
3121 && !global_options_set.x_flag_asynchronous_unwind_tables)))
3123 inform (input_location,
3124 "-freorder-blocks-and-partition does not work with exceptions "
3125 "on this architecture");
3126 flag_reorder_blocks_and_partition = 0;
3127 flag_reorder_blocks = 1;
3130 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3131 one valid choice of exception scheme for each runtime. */
3132 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3133 global_options.x_flag_objc_sjlj_exceptions =
3134 flag_next_runtime && !TARGET_64BIT;
3136 /* FIXME: and this could be eliminated then too. */
3137 if (!global_options_set.x_flag_exceptions
3138 && flag_objc_exceptions
3139 && TARGET_64BIT)
3140 flag_exceptions = 1;
3142 if (flag_mkernel || flag_apple_kext)
3144 /* -mkernel implies -fapple-kext for C++ */
3145 if (strcmp (lang_hooks.name, "GNU C++") == 0)
3146 flag_apple_kext = 1;
3148 flag_no_common = 1;
3150 /* No EH in kexts. */
3151 flag_exceptions = 0;
3152 /* No -fnon-call-exceptions data in kexts. */
3153 flag_non_call_exceptions = 0;
3154 /* so no tables either.. */
3155 flag_unwind_tables = 0;
3156 flag_asynchronous_unwind_tables = 0;
3157 /* We still need to emit branch islands for kernel context. */
3158 darwin_emit_branch_islands = true;
3161 if (flag_var_tracking_uninit == 0
3162 && generating_for_darwin_version >= 9
3163 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3164 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3165 && write_symbols == DWARF2_DEBUG)
3166 flag_var_tracking_uninit = flag_var_tracking;
3168 if (MACHO_DYNAMIC_NO_PIC_P)
3170 if (flag_pic)
3171 warning_at (UNKNOWN_LOCATION, 0,
3172 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3173 " %<-fpie%> or %<-fPIE%>");
3174 flag_pic = 0;
3176 else if (flag_pic == 1)
3178 /* Darwin's -fpic is -fPIC. */
3179 flag_pic = 2;
3182 /* It is assumed that branch island stubs are needed for earlier systems. */
3183 if (generating_for_darwin_version < 9)
3184 darwin_emit_branch_islands = true;
3185 else
3186 emit_aligned_common = true; /* Later systems can support aligned common. */
3188 /* The c_dialect...() macros are not available to us here. */
3189 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3192 #if DARWIN_PPC
3193 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3195 static void
3196 darwin_patch_builtin (enum built_in_function fncode)
3198 tree fn = builtin_decl_explicit (fncode);
3199 tree sym;
3200 char *newname;
3202 if (!fn)
3203 return;
3205 sym = DECL_ASSEMBLER_NAME (fn);
3206 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3208 set_user_assembler_name (fn, newname);
3210 fn = builtin_decl_implicit (fncode);
3211 if (fn)
3212 set_user_assembler_name (fn, newname);
3215 void
3216 darwin_patch_builtins (void)
3218 if (LONG_DOUBLE_TYPE_SIZE != 128)
3219 return;
3221 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3222 #define PATCH_BUILTIN_NO64(fncode) \
3223 if (!TARGET_64BIT) \
3224 darwin_patch_builtin (fncode);
3225 #define PATCH_BUILTIN_VARIADIC(fncode) \
3226 if (!TARGET_64BIT \
3227 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3228 darwin_patch_builtin (fncode);
3229 #include "darwin-ppc-ldouble-patch.def"
3230 #undef PATCH_BUILTIN
3231 #undef PATCH_BUILTIN_NO64
3232 #undef PATCH_BUILTIN_VARIADIC
3234 #endif
3236 /* CFStrings implementation. */
3237 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3238 static GTY(()) tree cfstring_type_node = NULL_TREE;
3239 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3240 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3241 static GTY(()) tree pcint_type_node = NULL_TREE;
3242 static GTY(()) tree pcchar_type_node = NULL_TREE;
3244 static enum built_in_function darwin_builtin_cfstring;
3246 /* Store all constructed constant CFStrings in a hash table so that
3247 they get uniqued properly. */
3249 typedef struct GTY (()) cfstring_descriptor {
3250 /* The string literal. */
3251 tree literal;
3252 /* The resulting constant CFString. */
3253 tree constructor;
3254 } cfstring_descriptor;
3256 static GTY ((param_is (struct cfstring_descriptor))) htab_t cfstring_htab;
3258 static hashval_t cfstring_hash (const void *);
3259 static int cfstring_eq (const void *, const void *);
3261 static tree
3262 add_builtin_field_decl (tree type, const char *name, tree **chain)
3264 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3265 get_identifier (name), type);
3267 if (*chain != NULL)
3268 **chain = field;
3269 *chain = &DECL_CHAIN (field);
3271 return field;
3274 tree
3275 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3277 tree cfsfun, fields, pccfstring_ftype_pcchar;
3278 tree *chain = NULL;
3280 darwin_builtin_cfstring =
3281 (enum built_in_function) builtin_cfstring;
3283 /* struct __builtin_CFString {
3284 const int *isa; (will point at
3285 int flags; __CFConstantStringClassReference)
3286 const char *str;
3287 long length;
3288 }; */
3290 pcint_type_node = build_pointer_type
3291 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3293 pcchar_type_node = build_pointer_type
3294 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3296 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3298 /* Have to build backwards for finish struct. */
3299 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3300 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3301 add_builtin_field_decl (integer_type_node, "flags", &chain);
3302 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3303 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3304 fields, NULL_TREE);
3306 /* const struct __builtin_CFstring *
3307 __builtin___CFStringMakeConstantString (const char *); */
3309 ccfstring_type_node = build_qualified_type
3310 (cfstring_type_node, TYPE_QUAL_CONST);
3311 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3312 pccfstring_ftype_pcchar = build_function_type_list
3313 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3315 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3316 get_identifier ("__builtin___CFStringMakeConstantString"),
3317 pccfstring_ftype_pcchar);
3319 TREE_PUBLIC (cfsfun) = 1;
3320 DECL_EXTERNAL (cfsfun) = 1;
3321 DECL_ARTIFICIAL (cfsfun) = 1;
3322 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3323 in place of the existing, which may be NULL. */
3324 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3325 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3326 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
3327 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
3328 lang_hooks.builtin_function (cfsfun);
3330 /* extern int __CFConstantStringClassReference[]; */
3331 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3332 get_identifier ("__CFConstantStringClassReference"),
3333 build_array_type (integer_type_node, NULL_TREE));
3335 TREE_PUBLIC (cfstring_class_reference) = 1;
3336 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3337 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3338 DECL_EXTERNAL (cfstring_class_reference) = 1;
3339 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3341 /* Initialize the hash table used to hold the constant CFString objects. */
3342 cfstring_htab = htab_create_ggc (31, cfstring_hash, cfstring_eq, NULL);
3344 return cfstring_type_node;
3347 tree
3348 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3349 bool ARG_UNUSED (ignore))
3351 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3353 if (fcode == darwin_builtin_cfstring)
3355 if (!darwin_constant_cfstrings)
3357 error ("built-in function %qD requires the"
3358 " %<-mconstant-cfstrings%> flag", fndecl);
3359 return error_mark_node;
3362 if (n_args != 1)
3364 error ("built-in function %qD takes one argument only", fndecl);
3365 return error_mark_node;
3368 return darwin_build_constant_cfstring (*argp);
3371 return NULL_TREE;
3374 void
3375 darwin_rename_builtins (void)
3377 /* The system ___divdc3 routine in libSystem on darwin10 is not
3378 accurate to 1ulp, ours is, so we avoid ever using the system name
3379 for this routine and instead install a non-conflicting name that
3380 is accurate.
3382 When -ffast-math or -funsafe-math-optimizations is given, we can
3383 use the faster version. */
3384 if (!flag_unsafe_math_optimizations)
3386 enum built_in_function dcode
3387 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3388 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3389 tree fn = builtin_decl_explicit (dcode);
3390 /* Fortran and c call TARGET_INIT_BUILTINS and
3391 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3392 call into each to ensure that at least one of them is called
3393 after build_common_builtin_nodes. A better fix is to add a
3394 new hook to run after build_common_builtin_nodes runs. */
3395 if (fn)
3396 set_user_assembler_name (fn, "___ieee_divdc3");
3397 fn = builtin_decl_implicit (dcode);
3398 if (fn)
3399 set_user_assembler_name (fn, "___ieee_divdc3");
3403 bool
3404 darwin_libc_has_function (enum function_class fn_class)
3406 if (fn_class == function_sincos)
3407 return false;
3408 if (fn_class == function_c99_math_complex
3409 || fn_class == function_c99_misc)
3410 return (TARGET_64BIT
3411 || strverscmp (darwin_macosx_version_min, "10.3") >= 0);
3413 return true;
3416 static hashval_t
3417 cfstring_hash (const void *ptr)
3419 tree str = ((const struct cfstring_descriptor *)ptr)->literal;
3420 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3421 int i, len = TREE_STRING_LENGTH (str);
3422 hashval_t h = len;
3424 for (i = 0; i < len; i++)
3425 h = ((h * 613) + p[i]);
3427 return h;
3430 static int
3431 cfstring_eq (const void *ptr1, const void *ptr2)
3433 tree str1 = ((const struct cfstring_descriptor *)ptr1)->literal;
3434 tree str2 = ((const struct cfstring_descriptor *)ptr2)->literal;
3435 int len1 = TREE_STRING_LENGTH (str1);
3437 return (len1 == TREE_STRING_LENGTH (str2)
3438 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3439 len1));
3442 tree
3443 darwin_build_constant_cfstring (tree str)
3445 struct cfstring_descriptor *desc, key;
3446 void **loc;
3447 tree addr;
3449 if (!str)
3451 error ("CFString literal is missing");
3452 return error_mark_node;
3455 STRIP_NOPS (str);
3457 if (TREE_CODE (str) == ADDR_EXPR)
3458 str = TREE_OPERAND (str, 0);
3460 if (TREE_CODE (str) != STRING_CST)
3462 error ("CFString literal expression is not a string constant");
3463 return error_mark_node;
3466 /* Perhaps we already constructed a constant CFString just like this one? */
3467 key.literal = str;
3468 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3469 desc = (struct cfstring_descriptor *) *loc;
3471 if (!desc)
3473 tree var, constructor, field;
3474 vec<constructor_elt, va_gc> *v = NULL;
3475 int length = TREE_STRING_LENGTH (str) - 1;
3477 if (darwin_warn_nonportable_cfstrings)
3479 const char *s = TREE_STRING_POINTER (str);
3480 int l = 0;
3482 for (l = 0; l < length; l++)
3483 if (!s[l] || !isascii (s[l]))
3485 warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
3486 s[l] ? "non-ASCII character" : "embedded NUL");
3487 break;
3491 *loc = desc = ggc_alloc_cleared_cfstring_descriptor ();
3492 desc->literal = str;
3494 /* isa *. */
3495 field = TYPE_FIELDS (ccfstring_type_node);
3496 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3497 build1 (ADDR_EXPR, TREE_TYPE (field),
3498 cfstring_class_reference));
3499 /* flags */
3500 field = DECL_CHAIN (field);
3501 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3502 build_int_cst (TREE_TYPE (field), 0x000007c8));
3503 /* string *. */
3504 field = DECL_CHAIN (field);
3505 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3506 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3507 /* length */
3508 field = DECL_CHAIN (field);
3509 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3510 build_int_cst (TREE_TYPE (field), length));
3512 constructor = build_constructor (ccfstring_type_node, v);
3513 TREE_READONLY (constructor) = 1;
3514 TREE_CONSTANT (constructor) = 1;
3515 TREE_STATIC (constructor) = 1;
3517 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3518 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3519 being built without any knowledge of C++ tree accessors; hence, we shall
3520 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3521 if (darwin_running_cxx)
3522 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3524 /* Create an anonymous global variable for this CFString. */
3525 var = build_decl (input_location, CONST_DECL,
3526 NULL, TREE_TYPE (constructor));
3527 DECL_ARTIFICIAL (var) = 1;
3528 TREE_STATIC (var) = 1;
3529 DECL_INITIAL (var) = constructor;
3530 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3531 DECL_CONTEXT (var) = NULL_TREE;
3532 desc->constructor = var;
3535 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3536 TREE_CONSTANT (addr) = 1;
3538 return addr;
3541 bool
3542 darwin_cfstring_p (tree str)
3544 struct cfstring_descriptor key;
3545 void **loc;
3547 if (!str)
3548 return false;
3550 STRIP_NOPS (str);
3552 if (TREE_CODE (str) == ADDR_EXPR)
3553 str = TREE_OPERAND (str, 0);
3555 if (TREE_CODE (str) != STRING_CST)
3556 return false;
3558 key.literal = str;
3559 loc = htab_find_slot (cfstring_htab, &key, NO_INSERT);
3561 if (loc)
3562 return true;
3564 return false;
3567 void
3568 darwin_enter_string_into_cfstring_table (tree str)
3570 struct cfstring_descriptor key;
3571 void **loc;
3573 key.literal = str;
3574 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3576 if (!*loc)
3578 *loc = ggc_alloc_cleared_cfstring_descriptor ();
3579 ((struct cfstring_descriptor *)*loc)->literal = str;
3583 /* Choose named function section based on its frequency. */
3585 section *
3586 darwin_function_section (tree decl, enum node_frequency freq,
3587 bool startup, bool exit)
3589 /* Decide if we need to put this in a coalescable section. */
3590 bool weak = (decl
3591 && DECL_WEAK (decl)
3592 && (!DECL_ATTRIBUTES (decl)
3593 || !lookup_attribute ("weak_import",
3594 DECL_ATTRIBUTES (decl))));
3596 /* If there is a specified section name, we should not be trying to
3597 override. */
3598 if (decl && DECL_SECTION_NAME (decl) != NULL_TREE)
3599 return get_named_section (decl, NULL, 0);
3601 /* Default when there is no function re-ordering. */
3602 if (!flag_reorder_functions)
3603 return (weak)
3604 ? darwin_sections[text_coal_section]
3605 : text_section;
3607 /* Startup code should go to startup subsection unless it is
3608 unlikely executed (this happens especially with function splitting
3609 where we can split away unnecessary parts of static constructors). */
3610 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
3611 return (weak)
3612 ? darwin_sections[text_startup_coal_section]
3613 : darwin_sections[text_startup_section];
3615 /* Similarly for exit. */
3616 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
3617 return (weak)
3618 ? darwin_sections[text_exit_coal_section]
3619 : darwin_sections[text_exit_section];
3621 /* Group cold functions together, similarly for hot code. */
3622 switch (freq)
3624 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
3625 return (weak)
3626 ? darwin_sections[text_cold_coal_section]
3627 : darwin_sections[text_cold_section];
3628 break;
3629 case NODE_FREQUENCY_HOT:
3630 return (weak)
3631 ? darwin_sections[text_hot_coal_section]
3632 : darwin_sections[text_hot_section];
3633 break;
3634 default:
3635 return (weak)
3636 ? darwin_sections[text_coal_section]
3637 : text_section;
3638 break;
3642 /* When a function is partitioned between sections, we need to insert a label
3643 at the start of each new chunk - so that it may become a valid 'atom' for
3644 eh and debug purposes. Without this the linker will emit warnings if one
3645 tries to add line location information (since the switched fragment will
3646 be anonymous). */
3648 void
3649 darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
3651 char buf[128];
3652 snprintf (buf, 128, "%s%s",new_is_cold?"__cold_sect_of_":"__hot_sect_of_",
3653 IDENTIFIER_POINTER (DECL_NAME (decl)));
3654 /* Make sure we pick up all the relevant quotes etc. */
3655 assemble_name_raw (fp, (const char *) buf);
3656 fputs (":\n", fp);
3659 #include "gt-darwin.h"