* target.def (TARGET_LIBC_HAS_FUNCTION): New target hook.
[official-gcc.git] / gcc / config / darwin.c
blob6c5d9c006236bbfdc1124fdcce7019113fda3376
1 /* Functions for generic Darwin as target machine for GNU C compiler.
2 Copyright (C) 1989-2013 Free Software Foundation, Inc.
3 Contributed by Apple Computer Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "regs.h"
27 #include "hard-reg-set.h"
28 #include "insn-config.h"
29 #include "conditions.h"
30 #include "insn-flags.h"
31 #include "output.h"
32 #include "insn-attr.h"
33 #include "flags.h"
34 #include "tree.h"
35 #include "expr.h"
36 #include "reload.h"
37 #include "function.h"
38 #include "ggc.h"
39 #include "langhooks.h"
40 #include "target.h"
41 #include "tm_p.h"
42 #include "diagnostic-core.h"
43 #include "toplev.h"
44 #include "hashtab.h"
45 #include "df.h"
46 #include "debug.h"
47 #include "obstack.h"
48 #include "lto-streamer.h"
50 /* Darwin supports a feature called fix-and-continue, which is used
51 for rapid turn around debugging. When code is compiled with the
52 -mfix-and-continue flag, two changes are made to the generated code
53 that allow the system to do things that it would normally not be
54 able to do easily. These changes allow gdb to load in
55 recompilation of a translation unit that has been changed into a
56 running program and replace existing functions and methods of that
57 translation unit with versions of those functions and methods
58 from the newly compiled translation unit. The new functions access
59 the existing static symbols from the old translation unit, if the
60 symbol existed in the unit to be replaced, and from the new
61 translation unit, otherwise.
63 The changes are to insert 5 nops at the beginning of all functions
64 and to use indirection to get at static symbols. The 5 nops
65 are required by consumers of the generated code. Currently, gdb
66 uses this to patch in a jump to the overriding function, this
67 allows all uses of the old name to forward to the replacement,
68 including existing function pointers and virtual methods. See
69 rs6000_emit_prologue for the code that handles the nop insertions.
71 The added indirection allows gdb to redirect accesses to static
72 symbols from the newly loaded translation unit to the existing
73 symbol, if any. @code{static} symbols are special and are handled by
74 setting the second word in the .non_lazy_symbol_pointer data
75 structure to symbol. See indirect_data for the code that handles
76 the extra indirection, and machopic_output_indirection and its use
77 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
78 symbol indirection. */
80 /* For darwin >= 9 (OSX 10.5) the linker is capable of making the necessary
81 branch islands and we no longer need to emit darwin stubs.
82 However, if we are generating code for earlier systems (or for use in the
83 kernel) the stubs might still be required, and this will be set true. */
84 int darwin_emit_branch_islands = false;
86 typedef struct GTY(()) cdtor_record {
87 rtx symbol;
88 int priority; /* [con/de]structor priority */
89 int position; /* original position */
90 } cdtor_record;
92 static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL;
93 static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL;
95 /* A flag to determine whether we are running c++ or obj-c++. This has to be
96 settable from non-c-family contexts too (i.e. we can't use the c_dialect_
97 functions). */
98 int darwin_running_cxx;
100 /* Some code-gen now depends on OS major version numbers (at least). */
101 int generating_for_darwin_version ;
103 /* Section names. */
104 section * darwin_sections[NUM_DARWIN_SECTIONS];
106 /* While we transition to using in-tests instead of ifdef'd code. */
107 #ifndef HAVE_lo_sum
108 #define HAVE_lo_sum 0
109 #define gen_macho_high(a,b) (a)
110 #define gen_macho_low(a,b,c) (a)
111 #endif
113 /* True if we're setting __attribute__ ((ms_struct)). */
114 int darwin_ms_struct = false;
116 /* Earlier versions of Darwin as do not recognize an alignment field in
117 .comm directives, this should be set for versions that allow it. */
118 int emit_aligned_common = false;
120 /* A get_unnamed_section callback used to switch to an ObjC section.
121 DIRECTIVE is as for output_section_asm_op. */
123 static void
124 output_objc_section_asm_op (const void *directive)
126 static bool been_here = false;
128 /* The NeXT ObjC Runtime requires these sections to be present and in
129 order in the object. The code below implements this by emitting
130 a section header for each ObjC section the first time that an ObjC
131 section is requested. */
132 if (! been_here)
134 section *saved_in_section = in_section;
135 static const enum darwin_section_enum tomark[] =
137 /* written, cold -> hot */
138 objc_cat_cls_meth_section,
139 objc_cat_inst_meth_section,
140 objc_string_object_section,
141 objc_constant_string_object_section,
142 objc_selector_refs_section,
143 objc_selector_fixup_section,
144 objc_cls_refs_section,
145 objc_class_section,
146 objc_meta_class_section,
147 /* shared, hot -> cold */
148 objc_cls_meth_section,
149 objc_inst_meth_section,
150 objc_protocol_section,
151 objc_class_names_section,
152 objc_meth_var_types_section,
153 objc_meth_var_names_section,
154 objc_category_section,
155 objc_class_vars_section,
156 objc_instance_vars_section,
157 objc_module_info_section,
158 objc_symbols_section,
160 /* ABI=1 */
161 static const enum darwin_section_enum tomarkv1[] =
163 objc1_protocol_ext_section,
164 objc1_class_ext_section,
165 objc1_prop_list_section
167 /* ABI=2 */
168 static const enum darwin_section_enum tomarkv2[] =
170 objc2_message_refs_section,
171 objc2_classdefs_section,
172 objc2_metadata_section,
173 objc2_classrefs_section,
174 objc2_classlist_section,
175 objc2_categorylist_section,
176 objc2_selector_refs_section,
177 objc2_nonlazy_class_section,
178 objc2_nonlazy_category_section,
179 objc2_protocollist_section,
180 objc2_protocolrefs_section,
181 objc2_super_classrefs_section,
182 objc2_image_info_section,
183 objc2_constant_string_object_section
185 size_t i;
187 been_here = true;
188 if (flag_objc_abi < 2)
190 for (i = 0; i < ARRAY_SIZE (tomark); i++)
191 switch_to_section (darwin_sections[tomark[i]]);
192 if (flag_objc_abi == 1)
193 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
194 switch_to_section (darwin_sections[tomarkv1[i]]);
196 else
197 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
198 switch_to_section (darwin_sections[tomarkv2[i]]);
199 /* Make sure we don't get varasm.c out of sync with us. */
200 switch_to_section (saved_in_section);
202 output_section_asm_op (directive);
206 /* Private flag applied to disable section-anchors in a particular section. */
207 #define SECTION_NO_ANCHOR SECTION_MACH_DEP
210 /* Implement TARGET_ASM_INIT_SECTIONS. */
212 void
213 darwin_init_sections (void)
215 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
216 darwin_sections[NAME] = \
217 get_unnamed_section (FLAGS, (OBJC \
218 ? output_objc_section_asm_op \
219 : output_section_asm_op), \
220 "\t" DIRECTIVE);
221 #include "config/darwin-sections.def"
222 #undef DEF_SECTION
224 readonly_data_section = darwin_sections[const_section];
225 exception_section = darwin_sections[darwin_exception_section];
226 eh_frame_section = darwin_sections[darwin_eh_frame_section];
230 name_needs_quotes (const char *name)
232 int c;
233 while ((c = *name++) != '\0')
234 if (! ISIDNUM (c)
235 && c != '.' && c != '$' && c != '_' )
236 return 1;
237 return 0;
240 /* Return true if SYM_REF can be used without an indirection. */
242 machopic_symbol_defined_p (rtx sym_ref)
244 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
245 return true;
247 /* If a symbol references local and is not an extern to this
248 file, then the symbol might be able to declared as defined. */
249 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
251 /* If the symbol references a variable and the variable is a
252 common symbol, then this symbol is not defined. */
253 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
255 tree decl = SYMBOL_REF_DECL (sym_ref);
256 if (!decl)
257 return true;
258 if (DECL_COMMON (decl))
259 return false;
261 return true;
263 return false;
266 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
267 reference, which will not be changed. */
269 enum machopic_addr_class
270 machopic_classify_symbol (rtx sym_ref)
272 bool function_p;
274 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
275 if (machopic_symbol_defined_p (sym_ref))
276 return (function_p
277 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
278 else
279 return (function_p
280 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
283 #ifndef TARGET_FIX_AND_CONTINUE
284 #define TARGET_FIX_AND_CONTINUE 0
285 #endif
287 /* Indicate when fix-and-continue style code generation is being used
288 and when a reference to data should be indirected so that it can be
289 rebound in a new translation unit to reference the original instance
290 of that data. Symbol names that are for code generation local to
291 the translation unit are bound to the new translation unit;
292 currently this means symbols that begin with L or _OBJC_;
293 otherwise, we indicate that an indirect reference should be made to
294 permit the runtime to rebind new instances of the translation unit
295 to the original instance of the data. */
297 static int
298 indirect_data (rtx sym_ref)
300 int lprefix;
301 const char *name;
303 /* If we aren't generating fix-and-continue code, don't do anything
304 special. */
305 if (TARGET_FIX_AND_CONTINUE == 0)
306 return 0;
308 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
309 are indirected. Symbols that begin with L and _OBJC_ are always
310 bound to the current translation unit as they are used for
311 generated local data of the translation unit. */
313 name = XSTR (sym_ref, 0);
315 lprefix = (((name[0] == '*' || name[0] == '&')
316 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
317 || (strncmp (name, "_OBJC_", 6) == 0));
319 return ! lprefix;
322 static int
323 machopic_data_defined_p (rtx sym_ref)
325 if (indirect_data (sym_ref))
326 return 0;
328 switch (machopic_classify_symbol (sym_ref))
330 case MACHOPIC_DEFINED_DATA:
331 case MACHOPIC_DEFINED_FUNCTION:
332 return 1;
333 default:
334 return 0;
338 void
339 machopic_define_symbol (rtx mem)
341 rtx sym_ref;
343 gcc_assert (GET_CODE (mem) == MEM);
344 sym_ref = XEXP (mem, 0);
345 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
348 /* Return either ORIG or:
350 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
352 depending on MACHO_DYNAMIC_NO_PIC_P. */
354 machopic_gen_offset (rtx orig)
356 if (MACHO_DYNAMIC_NO_PIC_P)
357 return orig;
358 else
360 /* Play games to avoid marking the function as needing pic if we
361 are being called as part of the cost-estimation process. */
362 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
363 crtl->uses_pic_offset_table = 1;
364 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
365 UNSPEC_MACHOPIC_OFFSET);
366 return gen_rtx_CONST (Pmode, orig);
370 static GTY(()) const char * function_base_func_name;
371 static GTY(()) int current_pic_label_num;
372 static GTY(()) int emitted_pic_label_num;
374 static void
375 update_pic_label_number_if_needed (void)
377 const char *current_name;
379 /* When we are generating _get_pc thunks within stubs, there is no current
380 function. */
381 if (current_function_decl)
383 current_name =
384 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
385 if (function_base_func_name != current_name)
387 ++current_pic_label_num;
388 function_base_func_name = current_name;
391 else
393 ++current_pic_label_num;
394 function_base_func_name = "L_machopic_stub_dummy";
398 void
399 machopic_output_function_base_name (FILE *file)
401 /* If dynamic-no-pic is on, we should not get here. */
402 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
404 update_pic_label_number_if_needed ();
405 fprintf (file, "L%d$pb", current_pic_label_num);
408 bool
409 machopic_should_output_picbase_label (void)
411 update_pic_label_number_if_needed ();
413 if (current_pic_label_num == emitted_pic_label_num)
414 return false;
416 emitted_pic_label_num = current_pic_label_num;
417 return true;
420 /* The suffix attached to non-lazy pointer symbols. */
421 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
422 /* The suffix attached to stub symbols. */
423 #define STUB_SUFFIX "$stub"
425 typedef struct GTY (()) machopic_indirection
427 /* The SYMBOL_REF for the entity referenced. */
428 rtx symbol;
429 /* The name of the stub or non-lazy pointer. */
430 const char * ptr_name;
431 /* True iff this entry is for a stub (as opposed to a non-lazy
432 pointer). */
433 bool stub_p;
434 /* True iff this stub or pointer pointer has been referenced. */
435 bool used;
436 } machopic_indirection;
438 /* A table mapping stub names and non-lazy pointer names to
439 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
441 static GTY ((param_is (struct machopic_indirection))) htab_t
442 machopic_indirections;
444 /* Return a hash value for a SLOT in the indirections hash table. */
446 static hashval_t
447 machopic_indirection_hash (const void *slot)
449 const machopic_indirection *p = (const machopic_indirection *) slot;
450 return htab_hash_string (p->ptr_name);
453 /* Returns true if the KEY is the same as that associated with
454 SLOT. */
456 static int
457 machopic_indirection_eq (const void *slot, const void *key)
459 return strcmp (((const machopic_indirection *) slot)->ptr_name,
460 (const char *) key) == 0;
463 /* Return the name of the non-lazy pointer (if STUB_P is false) or
464 stub (if STUB_B is true) corresponding to the given name. */
466 const char *
467 machopic_indirection_name (rtx sym_ref, bool stub_p)
469 char *buffer;
470 const char *name = XSTR (sym_ref, 0);
471 size_t namelen = strlen (name);
472 machopic_indirection *p;
473 void ** slot;
474 bool needs_quotes;
475 const char *suffix;
476 const char *prefix = user_label_prefix;
477 const char *quote = "";
478 tree id;
480 id = maybe_get_identifier (name);
481 if (id)
483 tree id_orig = id;
485 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
486 id = TREE_CHAIN (id);
487 if (id != id_orig)
489 name = IDENTIFIER_POINTER (id);
490 namelen = strlen (name);
494 if (name[0] == '*')
496 prefix = "";
497 ++name;
498 --namelen;
501 needs_quotes = name_needs_quotes (name);
502 if (needs_quotes)
504 quote = "\"";
507 if (stub_p)
508 suffix = STUB_SUFFIX;
509 else
510 suffix = NON_LAZY_POINTER_SUFFIX;
512 buffer = XALLOCAVEC (char, strlen ("&L")
513 + strlen (prefix)
514 + namelen
515 + strlen (suffix)
516 + 2 * strlen (quote)
517 + 1 /* '\0' */);
519 /* Construct the name of the non-lazy pointer or stub. */
520 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
522 if (!machopic_indirections)
523 machopic_indirections = htab_create_ggc (37,
524 machopic_indirection_hash,
525 machopic_indirection_eq,
526 /*htab_del=*/NULL);
528 slot = htab_find_slot_with_hash (machopic_indirections, buffer,
529 htab_hash_string (buffer), INSERT);
530 if (*slot)
532 p = (machopic_indirection *) *slot;
534 else
536 p = ggc_alloc_machopic_indirection ();
537 p->symbol = sym_ref;
538 p->ptr_name = xstrdup (buffer);
539 p->stub_p = stub_p;
540 p->used = false;
541 *slot = p;
544 return p->ptr_name;
547 /* Return the name of the stub for the mcount function. */
549 const char*
550 machopic_mcount_stub_name (void)
552 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
553 return machopic_indirection_name (symbol, /*stub_p=*/true);
556 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
557 or non-lazy pointer as used -- and mark the object to which the
558 pointer/stub refers as used as well, since the pointer/stub will
559 emit a reference to it. */
561 void
562 machopic_validate_stub_or_non_lazy_ptr (const char *name)
564 machopic_indirection *p;
566 p = ((machopic_indirection *)
567 (htab_find_with_hash (machopic_indirections, name,
568 htab_hash_string (name))));
569 if (p && ! p->used)
571 const char *real_name;
572 tree id;
574 p->used = true;
576 /* Do what output_addr_const will do when we actually call it. */
577 if (SYMBOL_REF_DECL (p->symbol))
578 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
580 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
582 id = maybe_get_identifier (real_name);
583 if (id)
584 mark_referenced (id);
588 /* Transform ORIG, which may be any data source, to the corresponding
589 source using indirections. */
592 machopic_indirect_data_reference (rtx orig, rtx reg)
594 rtx ptr_ref = orig;
596 if (! MACHOPIC_INDIRECT)
597 return orig;
599 if (GET_CODE (orig) == SYMBOL_REF)
601 int defined = machopic_data_defined_p (orig);
603 if (defined && MACHO_DYNAMIC_NO_PIC_P)
605 if (DARWIN_PPC)
607 /* Create a new register for CSE opportunities. */
608 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
609 emit_insn (gen_macho_high (hi_reg, orig));
610 emit_insn (gen_macho_low (reg, hi_reg, orig));
611 return reg;
613 else if (DARWIN_X86)
614 return orig;
615 else
616 /* some other cpu -- writeme! */
617 gcc_unreachable ();
619 else if (defined)
621 rtx offset = NULL;
622 if (DARWIN_PPC || HAVE_lo_sum)
623 offset = machopic_gen_offset (orig);
625 if (DARWIN_PPC)
627 rtx hi_sum_reg = (!can_create_pseudo_p ()
628 ? reg
629 : gen_reg_rtx (Pmode));
631 gcc_assert (reg);
633 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
634 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
635 gen_rtx_HIGH (Pmode, offset))));
636 emit_insn (gen_rtx_SET (Pmode, reg,
637 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
638 copy_rtx (offset))));
640 orig = reg;
642 else if (HAVE_lo_sum)
644 gcc_assert (reg);
646 emit_insn (gen_rtx_SET (VOIDmode, reg,
647 gen_rtx_HIGH (Pmode, offset)));
648 emit_insn (gen_rtx_SET (VOIDmode, reg,
649 gen_rtx_LO_SUM (Pmode, reg,
650 copy_rtx (offset))));
651 emit_use (pic_offset_table_rtx);
653 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
655 return orig;
658 ptr_ref = (gen_rtx_SYMBOL_REF
659 (Pmode,
660 machopic_indirection_name (orig, /*stub_p=*/false)));
662 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
664 ptr_ref = gen_const_mem (Pmode, ptr_ref);
665 machopic_define_symbol (ptr_ref);
667 if (DARWIN_X86
668 && reg
669 && MACHO_DYNAMIC_NO_PIC_P)
671 emit_insn (gen_rtx_SET (Pmode, reg, ptr_ref));
672 ptr_ref = reg;
675 return ptr_ref;
677 else if (GET_CODE (orig) == CONST)
679 /* If "(const (plus ...", walk the PLUS and return that result.
680 PLUS processing (below) will restore the "(const ..." if
681 appropriate. */
682 if (GET_CODE (XEXP (orig, 0)) == PLUS)
683 return machopic_indirect_data_reference (XEXP (orig, 0), reg);
684 else
685 return orig;
687 else if (GET_CODE (orig) == MEM)
689 XEXP (ptr_ref, 0) =
690 machopic_indirect_data_reference (XEXP (orig, 0), reg);
691 return ptr_ref;
693 else if (GET_CODE (orig) == PLUS)
695 rtx base, result;
696 /* When the target is i386, this code prevents crashes due to the
697 compiler's ignorance on how to move the PIC base register to
698 other registers. (The reload phase sometimes introduces such
699 insns.) */
700 if (GET_CODE (XEXP (orig, 0)) == REG
701 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
702 /* Prevent the same register from being erroneously used
703 as both the base and index registers. */
704 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
705 && reg)
707 emit_move_insn (reg, XEXP (orig, 0));
708 XEXP (ptr_ref, 0) = reg;
709 return ptr_ref;
712 /* Legitimize both operands of the PLUS. */
713 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
714 orig = machopic_indirect_data_reference (XEXP (orig, 1),
715 (base == reg ? 0 : reg));
716 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
717 result = plus_constant (Pmode, base, INTVAL (orig));
718 else
719 result = gen_rtx_PLUS (Pmode, base, orig);
721 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
723 if (reg)
725 emit_move_insn (reg, result);
726 result = reg;
728 else
730 result = force_reg (GET_MODE (result), result);
734 return result;
736 return ptr_ref;
739 /* Transform TARGET (a MEM), which is a function call target, to the
740 corresponding symbol_stub if necessary. Return a new MEM. */
743 machopic_indirect_call_target (rtx target)
745 if (! darwin_emit_branch_islands)
746 return target;
748 if (GET_CODE (target) != MEM)
749 return target;
751 if (MACHOPIC_INDIRECT
752 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
753 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
754 & MACHO_SYMBOL_FLAG_DEFINED))
756 rtx sym_ref = XEXP (target, 0);
757 const char *stub_name = machopic_indirection_name (sym_ref,
758 /*stub_p=*/true);
759 enum machine_mode mode = GET_MODE (sym_ref);
761 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
762 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
763 MEM_READONLY_P (target) = 1;
764 MEM_NOTRAP_P (target) = 1;
767 return target;
771 machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
773 rtx pic_ref = orig;
775 if (! MACHOPIC_INDIRECT)
776 return orig;
778 /* First handle a simple SYMBOL_REF or LABEL_REF */
779 if (GET_CODE (orig) == LABEL_REF
780 || (GET_CODE (orig) == SYMBOL_REF
783 /* addr(foo) = &func+(foo-func) */
784 orig = machopic_indirect_data_reference (orig, reg);
786 if (GET_CODE (orig) == PLUS
787 && GET_CODE (XEXP (orig, 0)) == REG)
789 if (reg == 0)
790 return force_reg (mode, orig);
792 emit_move_insn (reg, orig);
793 return reg;
796 if (GET_CODE (orig) == MEM)
798 if (reg == 0)
800 gcc_assert (!reload_in_progress);
801 reg = gen_reg_rtx (Pmode);
804 #if HAVE_lo_sum
805 if (MACHO_DYNAMIC_NO_PIC_P
806 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
807 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
809 #if defined (TARGET_TOC) /* ppc */
810 rtx temp_reg = (!can_create_pseudo_p ()
811 ? reg :
812 gen_reg_rtx (Pmode));
813 rtx asym = XEXP (orig, 0);
814 rtx mem;
816 emit_insn (gen_macho_high (temp_reg, asym));
817 mem = gen_const_mem (GET_MODE (orig),
818 gen_rtx_LO_SUM (Pmode, temp_reg,
819 copy_rtx (asym)));
820 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
821 #else
822 /* Some other CPU -- WriteMe! but right now there are no other
823 platforms that can use dynamic-no-pic */
824 gcc_unreachable ();
825 #endif
826 pic_ref = reg;
828 else
829 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
830 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
832 rtx offset = machopic_gen_offset (XEXP (orig, 0));
833 #if defined (TARGET_TOC) /* i.e., PowerPC */
834 /* Generating a new reg may expose opportunities for
835 common subexpression elimination. */
836 rtx hi_sum_reg = (!can_create_pseudo_p ()
837 ? reg
838 : gen_reg_rtx (Pmode));
839 rtx mem;
840 rtx insn;
841 rtx sum;
843 sum = gen_rtx_HIGH (Pmode, offset);
844 if (! MACHO_DYNAMIC_NO_PIC_P)
845 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
847 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
849 mem = gen_const_mem (GET_MODE (orig),
850 gen_rtx_LO_SUM (Pmode,
851 hi_sum_reg,
852 copy_rtx (offset)));
853 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
854 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
856 pic_ref = reg;
857 #else
858 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
860 emit_insn (gen_rtx_SET (VOIDmode, reg,
861 gen_rtx_HIGH (Pmode,
862 gen_rtx_CONST (Pmode,
863 offset))));
864 emit_insn (gen_rtx_SET (VOIDmode, reg,
865 gen_rtx_LO_SUM (Pmode, reg,
866 gen_rtx_CONST (Pmode,
867 copy_rtx (offset)))));
868 pic_ref = gen_rtx_PLUS (Pmode,
869 pic_offset_table_rtx, reg);
870 #endif
872 else
873 #endif /* HAVE_lo_sum */
875 rtx pic = pic_offset_table_rtx;
876 if (GET_CODE (pic) != REG)
878 emit_move_insn (reg, pic);
879 pic = reg;
881 #if 0
882 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
883 #endif
885 if (reload_in_progress)
886 df_set_regs_ever_live (REGNO (pic), true);
887 pic_ref = gen_rtx_PLUS (Pmode, pic,
888 machopic_gen_offset (XEXP (orig, 0)));
891 #if !defined (TARGET_TOC)
892 emit_move_insn (reg, pic_ref);
893 pic_ref = gen_const_mem (GET_MODE (orig), reg);
894 #endif
896 else
899 #if HAVE_lo_sum
900 if (GET_CODE (orig) == SYMBOL_REF
901 || GET_CODE (orig) == LABEL_REF)
903 rtx offset = machopic_gen_offset (orig);
904 #if defined (TARGET_TOC) /* i.e., PowerPC */
905 rtx hi_sum_reg;
907 if (reg == 0)
909 gcc_assert (!reload_in_progress);
910 reg = gen_reg_rtx (Pmode);
913 hi_sum_reg = reg;
915 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
916 (MACHO_DYNAMIC_NO_PIC_P)
917 ? gen_rtx_HIGH (Pmode, offset)
918 : gen_rtx_PLUS (Pmode,
919 pic_offset_table_rtx,
920 gen_rtx_HIGH (Pmode,
921 offset))));
922 emit_insn (gen_rtx_SET (VOIDmode, reg,
923 gen_rtx_LO_SUM (Pmode,
924 hi_sum_reg,
925 copy_rtx (offset))));
926 pic_ref = reg;
927 #else
928 emit_insn (gen_rtx_SET (VOIDmode, reg,
929 gen_rtx_HIGH (Pmode, offset)));
930 emit_insn (gen_rtx_SET (VOIDmode, reg,
931 gen_rtx_LO_SUM (Pmode, reg,
932 copy_rtx (offset))));
933 pic_ref = gen_rtx_PLUS (Pmode,
934 pic_offset_table_rtx, reg);
935 #endif
937 else
938 #endif /* HAVE_lo_sum */
940 if (REG_P (orig)
941 || GET_CODE (orig) == SUBREG)
943 return orig;
945 else
947 rtx pic = pic_offset_table_rtx;
948 if (GET_CODE (pic) != REG)
950 emit_move_insn (reg, pic);
951 pic = reg;
953 #if 0
954 emit_use (pic_offset_table_rtx);
955 #endif
956 if (reload_in_progress)
957 df_set_regs_ever_live (REGNO (pic), true);
958 pic_ref = gen_rtx_PLUS (Pmode,
959 pic,
960 machopic_gen_offset (orig));
965 if (GET_CODE (pic_ref) != REG)
967 if (reg != 0)
969 emit_move_insn (reg, pic_ref);
970 return reg;
972 else
974 return force_reg (mode, pic_ref);
977 else
979 return pic_ref;
983 else if (GET_CODE (orig) == SYMBOL_REF)
984 return orig;
986 else if (GET_CODE (orig) == PLUS
987 && (GET_CODE (XEXP (orig, 0)) == MEM
988 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
989 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
990 && XEXP (orig, 0) != pic_offset_table_rtx
991 && GET_CODE (XEXP (orig, 1)) != REG)
994 rtx base;
995 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
997 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
998 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
999 Pmode, (base == reg ? 0 : reg));
1000 if (GET_CODE (orig) == CONST_INT)
1002 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
1003 is_complex = 1;
1005 else
1006 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
1008 if (reg && is_complex)
1010 emit_move_insn (reg, pic_ref);
1011 pic_ref = reg;
1013 /* Likewise, should we set special REG_NOTEs here? */
1016 else if (GET_CODE (orig) == CONST)
1018 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1021 else if (GET_CODE (orig) == MEM
1022 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
1024 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1025 addr = replace_equiv_address (orig, addr);
1026 emit_move_insn (reg, addr);
1027 pic_ref = reg;
1030 return pic_ref;
1033 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
1034 DATA is the FILE* for assembly output. Called from
1035 htab_traverse. */
1037 static int
1038 machopic_output_indirection (void **slot, void *data)
1040 machopic_indirection *p = *((machopic_indirection **) slot);
1041 FILE *asm_out_file = (FILE *) data;
1042 rtx symbol;
1043 const char *sym_name;
1044 const char *ptr_name;
1046 if (!p->used)
1047 return 1;
1049 symbol = p->symbol;
1050 sym_name = XSTR (symbol, 0);
1051 ptr_name = p->ptr_name;
1053 if (p->stub_p)
1055 char *sym;
1056 char *stub;
1057 tree id;
1059 id = maybe_get_identifier (sym_name);
1060 if (id)
1062 tree id_orig = id;
1064 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1065 id = TREE_CHAIN (id);
1066 if (id != id_orig)
1067 sym_name = IDENTIFIER_POINTER (id);
1070 sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1071 if (sym_name[0] == '*' || sym_name[0] == '&')
1072 strcpy (sym, sym_name + 1);
1073 else if (sym_name[0] == '-' || sym_name[0] == '+')
1074 strcpy (sym, sym_name);
1075 else
1076 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1078 stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1079 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1080 strcpy (stub, ptr_name + 1);
1081 else
1082 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1084 machopic_output_stub (asm_out_file, sym, stub);
1086 else if (! indirect_data (symbol)
1087 && (machopic_symbol_defined_p (symbol)
1088 || SYMBOL_REF_LOCAL_P (symbol)))
1090 switch_to_section (data_section);
1091 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1092 assemble_label (asm_out_file, ptr_name);
1093 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1094 GET_MODE_SIZE (Pmode),
1095 GET_MODE_ALIGNMENT (Pmode), 1);
1097 else
1099 rtx init = const0_rtx;
1101 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1103 /* Mach-O symbols are passed around in code through indirect
1104 references and the original symbol_ref hasn't passed through
1105 the generic handling and reference-catching in
1106 output_operand, so we need to manually mark weak references
1107 as such. */
1108 if (SYMBOL_REF_WEAK (symbol))
1110 tree decl = SYMBOL_REF_DECL (symbol);
1111 gcc_assert (DECL_P (decl));
1113 if (decl != NULL_TREE
1114 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
1115 /* Handle only actual external-only definitions, not
1116 e.g. extern inline code or variables for which
1117 storage has been allocated. */
1118 && !TREE_STATIC (decl))
1120 fputs ("\t.weak_reference ", asm_out_file);
1121 assemble_name (asm_out_file, sym_name);
1122 fputc ('\n', asm_out_file);
1126 assemble_name (asm_out_file, ptr_name);
1127 fprintf (asm_out_file, ":\n");
1129 fprintf (asm_out_file, "\t.indirect_symbol ");
1130 assemble_name (asm_out_file, sym_name);
1131 fprintf (asm_out_file, "\n");
1133 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1134 have their symbol name instead of 0 in the second entry of
1135 the non-lazy symbol pointer data structure when they are
1136 defined. This allows the runtime to rebind newer instances
1137 of the translation unit with the original instance of the
1138 symbol. */
1140 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1141 && machopic_symbol_defined_p (symbol))
1142 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1144 assemble_integer (init, GET_MODE_SIZE (Pmode),
1145 GET_MODE_ALIGNMENT (Pmode), 1);
1148 return 1;
1151 void
1152 machopic_finish (FILE *asm_out_file)
1154 if (machopic_indirections)
1155 htab_traverse_noresize (machopic_indirections,
1156 machopic_output_indirection,
1157 asm_out_file);
1161 machopic_operand_p (rtx op)
1163 if (MACHOPIC_JUST_INDIRECT)
1164 return (GET_CODE (op) == SYMBOL_REF
1165 && machopic_symbol_defined_p (op));
1166 else
1167 return (GET_CODE (op) == CONST
1168 && GET_CODE (XEXP (op, 0)) == UNSPEC
1169 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1172 /* This function records whether a given name corresponds to a defined
1173 or undefined function or variable, for machopic_classify_ident to
1174 use later. */
1176 void
1177 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1179 rtx sym_ref;
1181 /* Do the standard encoding things first. */
1182 default_encode_section_info (decl, rtl, first);
1184 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1185 return;
1187 sym_ref = XEXP (rtl, 0);
1188 if (TREE_CODE (decl) == VAR_DECL)
1189 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1191 if (!DECL_EXTERNAL (decl)
1192 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1193 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1194 && ((TREE_STATIC (decl)
1195 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1196 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1197 && DECL_INITIAL (decl) != error_mark_node)))
1198 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1200 if (! TREE_PUBLIC (decl))
1201 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1204 void
1205 darwin_mark_decl_preserved (const char *name)
1207 fprintf (asm_out_file, "\t.no_dead_strip ");
1208 assemble_name (asm_out_file, name);
1209 fputc ('\n', asm_out_file);
1212 static section *
1213 darwin_rodata_section (int weak, bool zsize)
1215 return (weak
1216 ? darwin_sections[const_coal_section]
1217 : (zsize ? darwin_sections[zobj_const_section]
1218 : darwin_sections[const_section]));
1221 static section *
1222 darwin_mergeable_string_section (tree exp,
1223 unsigned HOST_WIDE_INT align)
1225 /* Darwin's ld expects to see non-writable string literals in the .cstring
1226 section. Later versions of ld check and complain when CFStrings are
1227 enabled. Therefore we shall force the strings into .cstring since we
1228 don't support writable ones anyway. */
1229 if ((darwin_constant_cfstrings || flag_merge_constants)
1230 && TREE_CODE (exp) == STRING_CST
1231 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1232 && align <= 256
1233 && (int_size_in_bytes (TREE_TYPE (exp))
1234 == TREE_STRING_LENGTH (exp))
1235 && ((size_t) TREE_STRING_LENGTH (exp)
1236 == strlen (TREE_STRING_POINTER (exp)) + 1))
1237 return darwin_sections[cstring_section];
1239 if (DARWIN_SECTION_ANCHORS && flag_section_anchors
1240 && TREE_CODE (exp) == STRING_CST
1241 && TREE_STRING_LENGTH (exp) == 0)
1242 return darwin_sections[zobj_const_section];
1244 return readonly_data_section;
1247 #ifndef HAVE_GAS_LITERAL16
1248 #define HAVE_GAS_LITERAL16 0
1249 #endif
1251 static section *
1252 darwin_mergeable_constant_section (tree exp,
1253 unsigned HOST_WIDE_INT align,
1254 bool zsize)
1256 enum machine_mode mode = DECL_MODE (exp);
1257 unsigned int modesize = GET_MODE_BITSIZE (mode);
1259 if (DARWIN_SECTION_ANCHORS
1260 && flag_section_anchors
1261 && zsize)
1262 return darwin_sections[zobj_const_section];
1264 if (flag_merge_constants
1265 && mode != VOIDmode
1266 && mode != BLKmode
1267 && modesize <= align
1268 && align >= 8
1269 && align <= 256
1270 && (align & (align -1)) == 0)
1272 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1274 if (TREE_CODE (size) == INTEGER_CST
1275 && TREE_INT_CST_LOW (size) == 4
1276 && TREE_INT_CST_HIGH (size) == 0)
1277 return darwin_sections[literal4_section];
1278 else if (TREE_CODE (size) == INTEGER_CST
1279 && TREE_INT_CST_LOW (size) == 8
1280 && TREE_INT_CST_HIGH (size) == 0)
1281 return darwin_sections[literal8_section];
1282 else if (HAVE_GAS_LITERAL16
1283 && TARGET_64BIT
1284 && TREE_CODE (size) == INTEGER_CST
1285 && TREE_INT_CST_LOW (size) == 16
1286 && TREE_INT_CST_HIGH (size) == 0)
1287 return darwin_sections[literal16_section];
1288 else
1289 return readonly_data_section;
1292 return readonly_data_section;
1295 section *
1296 darwin_tm_clone_table_section (void)
1298 return get_named_section (NULL,
1299 "__DATA,__tm_clone_table,regular,no_dead_strip",
1304 machopic_reloc_rw_mask (void)
1306 return MACHOPIC_INDIRECT ? 3 : 0;
1309 /* We have to deal with ObjC/C++ metadata section placement in the common
1310 code, since it will also be called from LTO.
1312 Return metadata attributes, if present (searching for ABI=2 first)
1313 Return NULL_TREE if no such attributes are found. */
1315 static tree
1316 is_objc_metadata (tree decl)
1318 if (DECL_P (decl)
1319 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1320 && DECL_ATTRIBUTES (decl))
1322 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1323 if (meta)
1324 return meta;
1325 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1326 if (meta)
1327 return meta;
1329 return NULL_TREE;
1332 /* Return the section required for Objective C ABI 2 metadata. */
1333 static section *
1334 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1336 const char *p;
1337 tree ident = TREE_VALUE (meta);
1338 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1339 p = IDENTIFIER_POINTER (ident);
1341 /* If we are in LTO, then we don't know the state of flag_next_runtime
1342 or flag_objc_abi when the code was generated. We set these from the
1343 meta-data - which is needed to deal with const string constructors. */
1345 flag_next_runtime = 1;
1346 flag_objc_abi = 2;
1348 if (base == data_section)
1349 base = darwin_sections[objc2_metadata_section];
1351 /* Most of the OBJC2 META-data end up in the base section, so check it
1352 first. */
1353 if (!strncmp (p, "V2_BASE", 7))
1354 return base;
1355 else if (!strncmp (p, "V2_STRG", 7))
1356 return darwin_sections[cstring_section];
1358 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1359 return darwin_sections[objc2_classdefs_section];
1360 else if (!strncmp (p, "V2_MREF", 7))
1361 return darwin_sections[objc2_message_refs_section];
1362 else if (!strncmp (p, "V2_CLRF", 7))
1363 return darwin_sections[objc2_classrefs_section];
1364 else if (!strncmp (p, "V2_SURF", 7))
1365 return darwin_sections[objc2_super_classrefs_section];
1366 else if (!strncmp (p, "V2_NLCL", 7))
1367 return darwin_sections[objc2_nonlazy_class_section];
1368 else if (!strncmp (p, "V2_CLAB", 7))
1369 return darwin_sections[objc2_classlist_section];
1370 else if (!strncmp (p, "V2_SRFS", 7))
1371 return darwin_sections[objc2_selector_refs_section];
1372 else if (!strncmp (p, "V2_NLCA", 7))
1373 return darwin_sections[objc2_nonlazy_category_section];
1374 else if (!strncmp (p, "V2_CALA", 7))
1375 return darwin_sections[objc2_categorylist_section];
1377 else if (!strncmp (p, "V2_PLST", 7))
1378 return darwin_sections[objc2_protocollist_section];
1379 else if (!strncmp (p, "V2_PRFS", 7))
1380 return darwin_sections[objc2_protocolrefs_section];
1382 else if (!strncmp (p, "V2_INFO", 7))
1383 return darwin_sections[objc2_image_info_section];
1385 else if (!strncmp (p, "V2_EHTY", 7))
1386 return darwin_sections[data_coal_section];
1388 else if (!strncmp (p, "V2_CSTR", 7))
1389 return darwin_sections[objc2_constant_string_object_section];
1391 /* Not recognized, default. */
1392 return base;
1395 /* Return the section required for Objective C ABI 0/1 metadata. */
1396 static section *
1397 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1399 const char *p;
1400 tree ident = TREE_VALUE (meta);
1401 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1402 p = IDENTIFIER_POINTER (ident);
1404 /* If we are in LTO, then we don't know the state of flag_next_runtime
1405 or flag_objc_abi when the code was generated. We set these from the
1406 meta-data - which is needed to deal with const string constructors. */
1407 flag_next_runtime = 1;
1408 if (!global_options_set.x_flag_objc_abi)
1409 flag_objc_abi = 1;
1411 /* String sections first, cos there are lots of strings. */
1412 if (!strncmp (p, "V1_STRG", 7))
1413 return darwin_sections[cstring_section];
1414 else if (!strncmp (p, "V1_CLSN", 7))
1415 return darwin_sections[objc_class_names_section];
1416 else if (!strncmp (p, "V1_METN", 7))
1417 return darwin_sections[objc_meth_var_names_section];
1418 else if (!strncmp (p, "V1_METT", 7))
1419 return darwin_sections[objc_meth_var_types_section];
1421 else if (!strncmp (p, "V1_CLAS", 7))
1422 return darwin_sections[objc_class_section];
1423 else if (!strncmp (p, "V1_META", 7))
1424 return darwin_sections[objc_meta_class_section];
1425 else if (!strncmp (p, "V1_CATG", 7))
1426 return darwin_sections[objc_category_section];
1427 else if (!strncmp (p, "V1_PROT", 7))
1428 return darwin_sections[objc_protocol_section];
1430 else if (!strncmp (p, "V1_CLCV", 7))
1431 return darwin_sections[objc_class_vars_section];
1432 else if (!strncmp (p, "V1_CLIV", 7))
1433 return darwin_sections[objc_instance_vars_section];
1435 else if (!strncmp (p, "V1_CLCM", 7))
1436 return darwin_sections[objc_cls_meth_section];
1437 else if (!strncmp (p, "V1_CLIM", 7))
1438 return darwin_sections[objc_inst_meth_section];
1439 else if (!strncmp (p, "V1_CACM", 7))
1440 return darwin_sections[objc_cat_cls_meth_section];
1441 else if (!strncmp (p, "V1_CAIM", 7))
1442 return darwin_sections[objc_cat_inst_meth_section];
1443 else if (!strncmp (p, "V1_PNSM", 7))
1444 return darwin_sections[objc_cat_inst_meth_section];
1445 else if (!strncmp (p, "V1_PCLM", 7))
1446 return darwin_sections[objc_cat_cls_meth_section];
1448 else if (!strncmp (p, "V1_CLPR", 7))
1449 return darwin_sections[objc_cat_cls_meth_section];
1450 else if (!strncmp (p, "V1_CAPR", 7))
1451 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1453 else if (!strncmp (p, "V1_PRFS", 7))
1454 return darwin_sections[objc_cat_cls_meth_section];
1455 else if (!strncmp (p, "V1_CLRF", 7))
1456 return darwin_sections[objc_cls_refs_section];
1457 else if (!strncmp (p, "V1_SRFS", 7))
1458 return darwin_sections[objc_selector_refs_section];
1460 else if (!strncmp (p, "V1_MODU", 7))
1461 return darwin_sections[objc_module_info_section];
1462 else if (!strncmp (p, "V1_SYMT", 7))
1463 return darwin_sections[objc_symbols_section];
1464 else if (!strncmp (p, "V1_INFO", 7))
1465 return darwin_sections[objc_image_info_section];
1467 else if (!strncmp (p, "V1_PLST", 7))
1468 return darwin_sections[objc1_prop_list_section];
1469 else if (!strncmp (p, "V1_PEXT", 7))
1470 return darwin_sections[objc1_protocol_ext_section];
1471 else if (!strncmp (p, "V1_CEXT", 7))
1472 return darwin_sections[objc1_class_ext_section];
1474 else if (!strncmp (p, "V2_CSTR", 7))
1475 return darwin_sections[objc_constant_string_object_section];
1477 return base;
1480 section *
1481 machopic_select_section (tree decl,
1482 int reloc,
1483 unsigned HOST_WIDE_INT align)
1485 bool zsize, one, weak, ro;
1486 section *base_section = NULL;
1488 weak = (DECL_P (decl)
1489 && DECL_WEAK (decl)
1490 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1492 zsize = (DECL_P (decl)
1493 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1494 && tree_low_cst (DECL_SIZE_UNIT (decl), 1) == 0);
1496 one = DECL_P (decl)
1497 && TREE_CODE (decl) == VAR_DECL
1498 && DECL_ONE_ONLY (decl);
1500 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1502 switch (categorize_decl_for_section (decl, reloc))
1504 case SECCAT_TEXT:
1505 gcc_unreachable ();
1506 break;
1508 case SECCAT_RODATA:
1509 case SECCAT_SRODATA:
1510 base_section = darwin_rodata_section (weak, zsize);
1511 break;
1513 case SECCAT_RODATA_MERGE_STR:
1514 base_section = darwin_mergeable_string_section (decl, align);
1515 break;
1517 case SECCAT_RODATA_MERGE_STR_INIT:
1518 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1519 break;
1521 case SECCAT_RODATA_MERGE_CONST:
1522 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1523 break;
1525 case SECCAT_DATA:
1526 case SECCAT_DATA_REL:
1527 case SECCAT_DATA_REL_LOCAL:
1528 case SECCAT_DATA_REL_RO:
1529 case SECCAT_DATA_REL_RO_LOCAL:
1530 case SECCAT_SDATA:
1531 case SECCAT_TDATA:
1532 if (weak || one)
1534 if (ro)
1535 base_section = darwin_sections[const_data_coal_section];
1536 else
1537 base_section = darwin_sections[data_coal_section];
1539 else if (DARWIN_SECTION_ANCHORS
1540 && flag_section_anchors
1541 && zsize)
1543 /* If we're doing section anchors, then punt zero-sized objects into
1544 their own sections so that they don't interfere with offset
1545 computation for the remaining vars. This does not need to be done
1546 for stuff in mergeable sections, since these are ineligible for
1547 anchors. */
1548 if (ro)
1549 base_section = darwin_sections[zobj_const_data_section];
1550 else
1551 base_section = darwin_sections[zobj_data_section];
1553 else if (ro)
1554 base_section = darwin_sections[const_data_section];
1555 else
1556 base_section = data_section;
1557 break;
1558 case SECCAT_BSS:
1559 case SECCAT_SBSS:
1560 case SECCAT_TBSS:
1561 if (weak || one)
1562 base_section = darwin_sections[data_coal_section];
1563 else
1565 if (!TREE_PUBLIC (decl))
1566 base_section = lcomm_section;
1567 else if (bss_noswitch_section)
1568 base_section = bss_noswitch_section;
1569 else
1570 base_section = data_section;
1572 break;
1574 default:
1575 gcc_unreachable ();
1578 /* Darwin weird special cases.
1579 a) OBJC Meta-data. */
1580 if (DECL_P (decl)
1581 && (TREE_CODE (decl) == VAR_DECL
1582 || TREE_CODE (decl) == CONST_DECL)
1583 && DECL_ATTRIBUTES (decl))
1585 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1586 if (meta)
1587 return darwin_objc2_section (decl, meta, base_section);
1588 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1589 if (meta)
1590 return darwin_objc1_section (decl, meta, base_section);
1591 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1592 if (meta)
1593 return base_section; /* GNU runtime is happy with it all in one pot. */
1596 /* b) Constant string objects. */
1597 if (TREE_CODE (decl) == CONSTRUCTOR
1598 && TREE_TYPE (decl)
1599 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1600 && TYPE_NAME (TREE_TYPE (decl)))
1602 tree name = TYPE_NAME (TREE_TYPE (decl));
1603 if (TREE_CODE (name) == TYPE_DECL)
1604 name = DECL_NAME (name);
1606 /* FIXME: This is unsatisfactory for LTO, since it relies on other
1607 metadata determining the source FE. */
1608 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1610 if (flag_next_runtime)
1612 if (flag_objc_abi == 2)
1613 return darwin_sections[objc2_constant_string_object_section];
1614 else
1615 return darwin_sections[objc_constant_string_object_section];
1617 else
1618 return darwin_sections[objc_string_object_section];
1620 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1621 return darwin_sections[cfstring_constant_object_section];
1622 else
1623 return base_section;
1625 /* c) legacy meta-data selection. */
1626 else if (TREE_CODE (decl) == VAR_DECL
1627 && DECL_NAME (decl)
1628 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1629 && IDENTIFIER_POINTER (DECL_NAME (decl))
1630 && flag_next_runtime
1631 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1633 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1634 static bool warned_objc_46 = false;
1635 /* We shall assert that zero-sized objects are an error in ObjC
1636 meta-data. */
1637 gcc_assert (tree_low_cst (DECL_SIZE_UNIT (decl), 1) != 0);
1639 /* ??? This mechanism for determining the metadata section is
1640 broken when LTO is in use, since the frontend that generated
1641 the data is not identified. We will keep the capability for
1642 the short term - in case any non-Objective-C programs are using
1643 it to place data in specified sections. */
1644 if (!warned_objc_46)
1646 location_t loc = DECL_SOURCE_LOCATION (decl);
1647 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
1648 " to select meta-data sections is deprecated at 4.6"
1649 " and will be removed in 4.7");
1650 warned_objc_46 = true;
1653 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1654 return darwin_sections[objc_cls_meth_section];
1655 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1656 return darwin_sections[objc_inst_meth_section];
1657 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
1658 return darwin_sections[objc_cat_cls_meth_section];
1659 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
1660 return darwin_sections[objc_cat_inst_meth_section];
1661 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1662 return darwin_sections[objc_class_vars_section];
1663 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1664 return darwin_sections[objc_instance_vars_section];
1665 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1666 return darwin_sections[objc_cat_cls_meth_section];
1667 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1668 return darwin_sections[objc_class_names_section];
1669 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1670 return darwin_sections[objc_meth_var_names_section];
1671 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1672 return darwin_sections[objc_meth_var_types_section];
1673 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1674 return darwin_sections[objc_cls_refs_section];
1675 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1676 return darwin_sections[objc_class_section];
1677 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1678 return darwin_sections[objc_meta_class_section];
1679 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1680 return darwin_sections[objc_category_section];
1681 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1682 return darwin_sections[objc_selector_refs_section];
1683 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1684 return darwin_sections[objc_selector_fixup_section];
1685 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1686 return darwin_sections[objc_symbols_section];
1687 else if (!strncmp (name, "_OBJC_MODULES", 13))
1688 return darwin_sections[objc_module_info_section];
1689 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1690 return darwin_sections[objc_image_info_section];
1691 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1692 return darwin_sections[objc_cat_inst_meth_section];
1693 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1694 return darwin_sections[objc_cat_cls_meth_section];
1695 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1696 return darwin_sections[objc_cat_cls_meth_section];
1697 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1698 return darwin_sections[objc_protocol_section];
1699 else
1700 return base_section;
1703 return base_section;
1706 /* This can be called with address expressions as "rtx".
1707 They must go in "const". */
1709 section *
1710 machopic_select_rtx_section (enum machine_mode mode, rtx x,
1711 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1713 if (GET_MODE_SIZE (mode) == 8
1714 && (GET_CODE (x) == CONST_INT
1715 || GET_CODE (x) == CONST_DOUBLE))
1716 return darwin_sections[literal8_section];
1717 else if (GET_MODE_SIZE (mode) == 4
1718 && (GET_CODE (x) == CONST_INT
1719 || GET_CODE (x) == CONST_DOUBLE))
1720 return darwin_sections[literal4_section];
1721 else if (HAVE_GAS_LITERAL16
1722 && TARGET_64BIT
1723 && GET_MODE_SIZE (mode) == 16
1724 && (GET_CODE (x) == CONST_INT
1725 || GET_CODE (x) == CONST_DOUBLE
1726 || GET_CODE (x) == CONST_VECTOR))
1727 return darwin_sections[literal16_section];
1728 else if (MACHOPIC_INDIRECT
1729 && (GET_CODE (x) == SYMBOL_REF
1730 || GET_CODE (x) == CONST
1731 || GET_CODE (x) == LABEL_REF))
1732 return darwin_sections[const_data_section];
1733 else
1734 return darwin_sections[const_section];
1737 void
1738 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1740 cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
1742 vec_safe_push (ctors, new_elt);
1744 if (! MACHOPIC_INDIRECT)
1745 fprintf (asm_out_file, ".reference .constructors_used\n");
1748 void
1749 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1751 cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)};
1753 vec_safe_push (dtors, new_elt);
1755 if (! MACHOPIC_INDIRECT)
1756 fprintf (asm_out_file, ".reference .destructors_used\n");
1759 static int
1760 sort_cdtor_records (const void * a, const void * b)
1762 const cdtor_record *cda = (const cdtor_record *)a;
1763 const cdtor_record *cdb = (const cdtor_record *)b;
1764 if (cda->priority > cdb->priority)
1765 return 1;
1766 if (cda->priority < cdb->priority)
1767 return -1;
1768 if (cda->position > cdb->position)
1769 return 1;
1770 if (cda->position < cdb->position)
1771 return -1;
1772 return 0;
1775 static void
1776 finalize_ctors ()
1778 unsigned int i;
1779 cdtor_record *elt;
1781 if (MACHOPIC_INDIRECT)
1782 switch_to_section (darwin_sections[mod_init_section]);
1783 else
1784 switch_to_section (darwin_sections[constructor_section]);
1786 if (vec_safe_length (ctors) > 1)
1787 ctors->qsort (sort_cdtor_records);
1788 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
1790 assemble_align (POINTER_SIZE);
1791 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1795 static void
1796 finalize_dtors ()
1798 unsigned int i;
1799 cdtor_record *elt;
1801 if (MACHOPIC_INDIRECT)
1802 switch_to_section (darwin_sections[mod_term_section]);
1803 else
1804 switch_to_section (darwin_sections[destructor_section]);
1806 if (vec_safe_length (dtors) > 1)
1807 dtors->qsort (sort_cdtor_records);
1808 FOR_EACH_VEC_SAFE_ELT (dtors, i, elt)
1810 assemble_align (POINTER_SIZE);
1811 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1815 void
1816 darwin_globalize_label (FILE *stream, const char *name)
1818 if (!!strncmp (name, "_OBJC_", 6))
1819 default_globalize_label (stream, name);
1822 /* This routine returns non-zero if 'name' starts with the special objective-c
1823 anonymous file-scope static name. It accommodates c++'s mangling of such
1824 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1826 int
1827 darwin_label_is_anonymous_local_objc_name (const char *name)
1829 const unsigned char *p = (const unsigned char *) name;
1830 if (*p != '_')
1831 return 0;
1832 if (p[1] == 'Z' && p[2] == 'L')
1834 p += 3;
1835 while (*p >= '0' && *p <= '9')
1836 p++;
1838 return (!strncmp ((const char *)p, "_OBJC_", 6));
1841 /* LTO support for Mach-O.
1843 This version uses three mach-o sections to encapsulate the (unlimited
1844 number of) lto sections.
1846 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1847 __GNU_LTO, __section_names contains the GNU LTO section names.
1848 __GNU_LTO, __section_index contains an array of values that index these.
1850 Indexed thus:
1851 <section offset from the start of __GNU_LTO, __lto_sections>,
1852 <section length>
1853 <name offset from the start of __GNU_LTO, __section_names,
1854 <name length>.
1856 At present, for both m32 and m64 mach-o files each of these fields is
1857 represented by a uint32_t. This is because, AFAICT, a mach-o object
1858 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1860 uint32_t offset;
1861 "offset An integer specifying the offset to this section in the file." */
1863 /* Count lto section numbers. */
1864 static unsigned int lto_section_num = 0;
1866 /* A vector of information about LTO sections, at present, we only have
1867 the name. TODO: see if we can get the data length somehow. */
1868 typedef struct GTY (()) darwin_lto_section_e {
1869 const char *sectname;
1870 } darwin_lto_section_e ;
1872 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1874 /* Segment for LTO data. */
1875 #define LTO_SEGMENT_NAME "__GNU_LTO"
1877 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1878 sections into three Mach-O ones).
1879 NOTE: These names MUST be kept in sync with those in
1880 libiberty/simple-object-mach-o. */
1881 #define LTO_SECTS_SECTION "__wrapper_sects"
1882 #define LTO_NAMES_SECTION "__wrapper_names"
1883 #define LTO_INDEX_SECTION "__wrapper_index"
1885 /* File to temporarily store LTO data. This is appended to asm_out_file
1886 in darwin_end_file. */
1887 static FILE *lto_asm_out_file, *saved_asm_out_file;
1888 static char *lto_asm_out_name;
1890 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1891 asm_out_file and switching to an alternative output file. */
1892 void
1893 darwin_asm_lto_start (void)
1895 gcc_assert (! saved_asm_out_file);
1896 saved_asm_out_file = asm_out_file;
1897 if (! lto_asm_out_name)
1898 lto_asm_out_name = make_temp_file (".lto.s");
1899 lto_asm_out_file = fopen (lto_asm_out_name, "a");
1900 if (lto_asm_out_file == NULL)
1901 fatal_error ("failed to open temporary file %s for LTO output",
1902 lto_asm_out_name);
1903 asm_out_file = lto_asm_out_file;
1906 /* Restore asm_out_file. */
1907 void
1908 darwin_asm_lto_end (void)
1910 gcc_assert (saved_asm_out_file);
1911 fclose (lto_asm_out_file);
1912 asm_out_file = saved_asm_out_file;
1913 saved_asm_out_file = NULL;
1916 static void
1917 darwin_asm_dwarf_section (const char *name, unsigned int flags, tree decl);
1919 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
1921 void
1922 darwin_asm_named_section (const char *name,
1923 unsigned int flags,
1924 tree decl ATTRIBUTE_UNUSED)
1926 /* LTO sections go in a special section that encapsulates the (unlimited)
1927 number of GNU LTO sections within a single mach-o one. */
1928 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1929 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1931 darwin_lto_section_e e;
1932 /* We expect certain flags to be set... */
1933 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1934 == (SECTION_DEBUG | SECTION_NAMED));
1936 /* Switch to our combined section. */
1937 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1938 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1939 /* Output a label for the start of this sub-section. */
1940 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1941 lto_section_num, name);
1942 /* We have to jump through hoops to get the values of the intra-section
1943 offsets... */
1944 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1945 lto_section_num, lto_section_num);
1946 fprintf (asm_out_file,
1947 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1948 lto_section_num, lto_section_num+1, lto_section_num);
1949 lto_section_num++;
1950 e.sectname = xstrdup (name);
1951 /* Keep the names, we'll need to make a table later.
1952 TODO: check that we do not revisit sections, that would break
1953 the assumption of how this is done. */
1954 if (lto_section_names == NULL)
1955 vec_alloc (lto_section_names, 16);
1956 vec_safe_push (lto_section_names, e);
1958 else if (strncmp (name, "__DWARF,", 8) == 0)
1959 darwin_asm_dwarf_section (name, flags, decl);
1960 else
1961 fprintf (asm_out_file, "\t.section %s\n", name);
1964 void
1965 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
1967 /* Darwin does not use unique sections. */
1970 /* Handle __attribute__ ((apple_kext_compatibility)).
1971 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
1972 vtable for classes with this attribute (and their descendants) by not
1973 outputting the new 3.0 nondeleting destructor. This means that such
1974 objects CANNOT be allocated on the stack or as globals UNLESS they have
1975 a completely empty `operator delete'.
1976 Luckily, this fits in with the Darwin kext model.
1978 This attribute also disables gcc3's potential overlaying of derived
1979 class data members on the padding at the end of the base class. */
1981 tree
1982 darwin_handle_kext_attribute (tree *node, tree name,
1983 tree args ATTRIBUTE_UNUSED,
1984 int flags ATTRIBUTE_UNUSED,
1985 bool *no_add_attrs)
1987 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
1988 if (! TARGET_KEXTABI)
1990 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
1991 "only when compiling a kext", name);
1993 *no_add_attrs = true;
1995 else if (TREE_CODE (*node) != RECORD_TYPE)
1997 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
1998 "only to C++ classes", name);
2000 *no_add_attrs = true;
2003 return NULL_TREE;
2006 /* Handle a "weak_import" attribute; arguments as in
2007 struct attribute_spec.handler. */
2009 tree
2010 darwin_handle_weak_import_attribute (tree *node, tree name,
2011 tree ARG_UNUSED (args),
2012 int ARG_UNUSED (flags),
2013 bool * no_add_attrs)
2015 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
2017 warning (OPT_Wattributes, "%qE attribute ignored",
2018 name);
2019 *no_add_attrs = true;
2021 else
2022 declare_weak (*node);
2024 return NULL_TREE;
2027 /* Emit a label for an FDE, making it global and/or weak if appropriate.
2028 The third parameter is nonzero if this is for exception handling.
2029 The fourth parameter is nonzero if this is just a placeholder for an
2030 FDE that we are omitting. */
2032 void
2033 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
2035 char *lab ;
2036 char buf[32];
2037 static int invok_count = 0;
2038 static tree last_fun_decl = NULL_TREE;
2040 /* We use the linker to emit the .eh labels for Darwin 9 and above. */
2041 if (! for_eh || generating_for_darwin_version >= 9)
2042 return;
2044 /* FIXME: This only works when the eh for all sections of a function is
2045 emitted at the same time. If that changes, we would need to use a lookup
2046 table of some form to determine what to do. Also, we should emit the
2047 unadorned label for the partition containing the public label for a
2048 function. This is of limited use, probably, since we do not currently
2049 enable partitioning. */
2050 strcpy (buf, ".eh");
2051 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
2053 if (decl == last_fun_decl)
2055 invok_count++;
2056 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
2058 else
2060 last_fun_decl = decl;
2061 invok_count = 0;
2065 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
2067 if (TREE_PUBLIC (decl))
2069 targetm.asm_out.globalize_label (file, lab);
2070 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
2072 fputs ("\t.private_extern ", file);
2073 assemble_name (file, lab);
2074 fputc ('\n', file);
2078 if (DECL_WEAK (decl))
2080 fputs ("\t.weak_definition ", file);
2081 assemble_name (file, lab);
2082 fputc ('\n', file);
2085 assemble_name (file, lab);
2086 if (empty)
2088 fputs (" = 0\n", file);
2090 /* Mark the absolute .eh and .eh1 style labels as needed to
2091 ensure that we don't dead code strip them and keep such
2092 labels from another instantiation point until we can fix this
2093 properly with group comdat support. */
2094 darwin_mark_decl_preserved (lab);
2096 else
2097 fputs (":\n", file);
2099 free (lab);
2102 static GTY(()) unsigned long except_table_label_num;
2104 void
2105 darwin_emit_except_table_label (FILE *file)
2107 char section_start_label[30];
2109 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2110 except_table_label_num++);
2111 ASM_OUTPUT_LABEL (file, section_start_label);
2113 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2115 void
2116 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2118 const char *nlp_name;
2120 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2122 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2123 fputs ("\t.long\t", file);
2124 ASM_OUTPUT_LABELREF (file, nlp_name);
2125 fputs ("-.", file);
2128 /* If this is uncommented, details of each allocation will be printed
2129 in the asm right before the actual code. WARNING - this will cause some
2130 test-suite fails (since the printout will contain items that some tests
2131 are not expecting) -- so don't leave it on by default (it bloats the
2132 asm too). */
2133 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2135 /* The first two of these routines are ostensibly just intended to put
2136 names into the asm. However, they are both hijacked in order to ensure
2137 that zero-sized items do not make their way into the output. Consequently,
2138 we also need to make these participate in provisions for dealing with
2139 such items in section anchors. */
2141 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2142 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2143 but it does need to be referenced via indirect PIC data pointers.
2144 The machopic_define_symbol calls are telling the machopic subsystem
2145 that the name *is* defined in this module, so it doesn't need to
2146 make them indirect. */
2147 void
2148 darwin_asm_declare_object_name (FILE *file,
2149 const char *nam, tree decl)
2151 const char *xname = nam;
2152 unsigned HOST_WIDE_INT size;
2153 bool local_def, weak;
2155 weak = (DECL_P (decl)
2156 && DECL_WEAK (decl)
2157 && !lookup_attribute ("weak_import",
2158 DECL_ATTRIBUTES (decl)));
2160 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2161 && (!DECL_COMMON (decl)
2162 || !TREE_PUBLIC (decl)));
2164 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2165 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2167 if (local_def)
2169 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2170 if (!weak)
2171 machopic_define_symbol (DECL_RTL (decl));
2174 size = tree_low_cst (DECL_SIZE_UNIT (decl), 1);
2176 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2177 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2178 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2179 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2180 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2181 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2182 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2183 (unsigned long)DECL_INITIAL (decl));
2184 #endif
2186 /* Darwin needs help to support local zero-sized objects.
2187 They must be made at least one byte, and the section containing must be
2188 marked as unsuitable for section-anchors (see storage allocators below).
2190 For non-zero objects this output is handled by varasm.c.
2192 if (!size)
2194 unsigned int l2align = 0;
2196 /* The align must be honored, even for zero-sized. */
2197 if (DECL_ALIGN (decl))
2199 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2200 fprintf (file, "\t.align\t%u\n", l2align);
2203 ASM_OUTPUT_LABEL (file, xname);
2204 size = 1;
2205 fprintf (file, "\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2207 /* Check that we've correctly picked up the zero-sized item and placed it
2208 properly. */
2209 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2210 || (in_section
2211 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2213 else
2214 ASM_OUTPUT_LABEL (file, xname);
2217 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2218 void
2219 darwin_asm_declare_constant_name (FILE *file, const char *name,
2220 const_tree exp ATTRIBUTE_UNUSED,
2221 HOST_WIDE_INT size)
2223 assemble_label (file, name);
2224 /* As for other items, we need at least one byte. */
2225 if (!size)
2227 fputs ("\t.space\t1\n", file);
2228 /* Check that we've correctly picked up the zero-sized item and placed it
2229 properly. */
2230 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2231 || (in_section
2232 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2236 /* Darwin storage allocators.
2238 Zerofill sections are desirable for large blank data since, otherwise, these
2239 data bloat objects (PR33210).
2241 However, section anchors don't work in .zerofill sections (one cannot switch
2242 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2243 to put (at least some) data into 'normal' switchable sections.
2245 Here we set a relatively arbitrary value for the size of an object to trigger
2246 zerofill when section anchors are enabled (anything bigger than a page for
2247 current Darwin implementations). FIXME: there ought to be some objective way
2248 to make this choice.
2250 When section anchor are off this is ignored anyway. */
2252 #define BYTES_ZFILL 4096
2254 /* Emit a chunk of data for items coalesced by the linker. */
2255 static void
2256 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2257 unsigned HOST_WIDE_INT size,
2258 unsigned int align)
2260 /* Since the sections used here are coalesed, they will not be eligible
2261 for section anchors, and therefore we don't need to break that out. */
2262 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2263 switch_to_section (darwin_sections[const_data_coal_section]);
2264 else
2265 switch_to_section (darwin_sections[data_coal_section]);
2267 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2268 the align info for zero-sized items... but do it here otherwise. */
2269 if (size && align)
2270 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2272 if (TREE_PUBLIC (decl))
2273 darwin_globalize_label (fp, name);
2275 /* ... and we let it deal with outputting one byte of zero for them too. */
2276 darwin_asm_declare_object_name (fp, name, decl);
2277 if (size)
2278 assemble_zeros (size);
2281 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2282 static void
2283 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2284 unsigned HOST_WIDE_INT size,
2285 unsigned int align, tree meta)
2287 section *ocs = data_section;
2289 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2290 ocs = darwin_objc2_section (decl, meta, ocs);
2291 else
2292 ocs = darwin_objc1_section (decl, meta, ocs);
2294 switch_to_section (ocs);
2296 /* We shall declare that zero-sized meta-data are not valid (yet). */
2297 gcc_assert (size);
2298 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2300 /* ... and we let it deal with outputting one byte of zero for them too. */
2301 darwin_asm_declare_object_name (fp, name, decl);
2302 assemble_zeros (size);
2305 /* This routine emits 'local' storage:
2307 When Section Anchors are off this routine emits .zerofill commands in
2308 sections named for their alignment.
2310 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2311 the .static_data section so that the section anchoring system can see them.
2312 Larger items are still placed in .zerofill sections, addressing PR33210.
2313 The routine has no checking - it is all assumed to be done by the caller.
2315 static void
2316 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2317 unsigned HOST_WIDE_INT size,
2318 unsigned int l2align)
2320 /* FIXME: We have a fudge to make this work with Java even when the target does
2321 not use sections anchors -- Java seems to need at least one small item in a
2322 non-zerofill segment. */
2323 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2324 || (size && size <= 2))
2326 /* Put smaller objects in _static_data, where the section anchors system
2327 can get them.
2328 However, if they are zero-sized punt them to yet a different section
2329 (that is not allowed to participate in anchoring). */
2330 if (!size)
2332 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2333 in_section = darwin_sections[zobj_bss_section];
2334 size = 1;
2336 else
2338 fputs ("\t.static_data\n", fp);
2339 in_section = darwin_sections[static_data_section];
2342 if (l2align)
2343 fprintf (fp, "\t.align\t%u\n", l2align);
2345 assemble_name (fp, name);
2346 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2348 else
2350 /* When we are on a non-section anchor target, we can get zero-sized
2351 items here. However, all we need to do is to bump them to one byte
2352 and the section alignment will take care of the rest. */
2353 char secnam[64];
2354 unsigned int flags ;
2355 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
2356 (unsigned) l2align);
2357 /* We can't anchor (yet, if ever) in zerofill sections, because we can't
2358 switch to them and emit a label. */
2359 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2360 in_section = get_section (secnam, flags, NULL);
2361 fprintf (fp, "\t.zerofill %s,", secnam);
2362 assemble_name (fp, name);
2363 if (!size)
2364 size = 1;
2366 if (l2align)
2367 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2368 size, (unsigned) l2align);
2369 else
2370 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2373 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2374 /* This is defined as a file-scope var, so we know to notify machopic. */
2375 machopic_define_symbol (DECL_RTL (decl));
2378 /* Emit a chunk of common. */
2379 static void
2380 darwin_emit_common (FILE *fp, const char *name,
2381 unsigned HOST_WIDE_INT size, unsigned int align)
2383 unsigned HOST_WIDE_INT rounded;
2384 unsigned int l2align;
2386 /* Earlier systems complain if the alignment exceeds the page size.
2387 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2388 if (!emit_aligned_common && (align > 32768UL))
2389 align = 4096UL; /* In units. */
2390 else
2391 align /= BITS_PER_UNIT;
2393 /* Make sure we have a meaningful align. */
2394 if (!align)
2395 align = 1;
2397 /* For earlier toolchains, we need to emit the var as a rounded size to
2398 tell ld the alignment. */
2399 if (size < align)
2400 rounded = align;
2401 else
2402 rounded = (size + (align-1)) & ~(align-1);
2404 l2align = floor_log2 (align);
2405 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2407 in_section = comm_section;
2408 /* We mustn't allow multiple public symbols to share an address when using
2409 the normal OSX toolchain. */
2410 if (!size)
2412 /* Put at least one byte. */
2413 size = 1;
2414 /* This section can no longer participate in section anchoring. */
2415 comm_section->common.flags |= SECTION_NO_ANCHOR;
2418 fputs ("\t.comm\t", fp);
2419 assemble_name (fp, name);
2420 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2421 emit_aligned_common?size:rounded);
2422 if (l2align && emit_aligned_common)
2423 fprintf (fp, ",%u", l2align);
2424 fputs ("\n", fp);
2427 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2428 or coalescable data sections (for weak or comdat) as appropriate. */
2430 void
2431 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2432 unsigned HOST_WIDE_INT size, unsigned int align)
2434 unsigned int l2align;
2435 bool one, pub, weak;
2436 tree meta;
2438 pub = TREE_PUBLIC (decl);
2439 one = DECL_ONE_ONLY (decl);
2440 weak = (DECL_P (decl)
2441 && DECL_WEAK (decl)
2442 && !lookup_attribute ("weak_import",
2443 DECL_ATTRIBUTES (decl)));
2445 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2446 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2447 " pub %d weak %d one %d init %lx\n",
2448 name, (long long)size, (int)align, TREE_READONLY (decl),
2449 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2450 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2451 #endif
2453 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2454 before the target has a chance to comment. */
2455 if ((meta = is_objc_metadata (decl)))
2457 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2458 return;
2461 /* Check that any initializer is valid. */
2462 gcc_assert ((DECL_INITIAL (decl) == NULL)
2463 || (DECL_INITIAL (decl) == error_mark_node)
2464 || initializer_zerop (DECL_INITIAL (decl)));
2466 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2467 gcc_assert (!DECL_COMMON (decl));
2469 /* Pick up the correct alignment. */
2470 if (!size || !align)
2471 align = DECL_ALIGN (decl);
2473 l2align = floor_log2 (align / BITS_PER_UNIT);
2474 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2476 last_assemble_variable_decl = decl;
2478 /* We would rather not have to check this here - but it seems that we might
2479 be passed a decl that should be in coalesced space. */
2480 if (one || weak)
2482 /* Weak or COMDAT objects are put in mergeable sections. */
2483 darwin_emit_weak_or_comdat (fp, decl, name, size,
2484 DECL_ALIGN (decl));
2485 return;
2488 /* If this is not public, then emit according to local rules. */
2489 if (!pub)
2491 darwin_emit_local_bss (fp, decl, name, size, l2align);
2492 return;
2495 /* So we have a public symbol (small item fudge for Java, see above). */
2496 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2497 || (size && size <= 2))
2499 /* Put smaller objects in data, where the section anchors system can get
2500 them. However, if they are zero-sized punt them to yet a different
2501 section (that is not allowed to participate in anchoring). */
2502 if (!size)
2504 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2505 in_section = darwin_sections[zobj_data_section];
2506 size = 1;
2508 else
2510 fputs ("\t.data\n", fp);
2511 in_section = data_section;
2514 if (l2align)
2515 fprintf (fp, "\t.align\t%u\n", l2align);
2517 assemble_name (fp, name);
2518 fprintf (fp, ":\n\t.space\t"HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2520 else
2522 char secnam[64];
2523 unsigned int flags ;
2524 /* When we are on a non-section anchor target, we can get zero-sized
2525 items here. However, all we need to do is to bump them to one byte
2526 and the section alignment will take care of the rest. */
2527 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
2529 /* We can't anchor in zerofill sections, because we can't switch
2530 to them and emit a label. */
2531 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2532 in_section = get_section (secnam, flags, NULL);
2533 fprintf (fp, "\t.zerofill %s,", secnam);
2534 assemble_name (fp, name);
2535 if (!size)
2536 size = 1;
2538 if (l2align)
2539 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2540 else
2541 fprintf (fp, ","HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2543 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2546 /* Output a chunk of common, with alignment specified (where the target
2547 supports this). */
2548 void
2549 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2550 unsigned HOST_WIDE_INT size,
2551 unsigned int align)
2553 unsigned int l2align;
2554 bool one, weak;
2555 tree meta;
2557 /* No corresponding var. */
2558 if (decl==NULL)
2560 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2561 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2562 #endif
2563 darwin_emit_common (fp, name, size, align);
2564 return;
2567 one = DECL_ONE_ONLY (decl);
2568 weak = (DECL_P (decl)
2569 && DECL_WEAK (decl)
2570 && !lookup_attribute ("weak_import",
2571 DECL_ATTRIBUTES (decl)));
2573 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2574 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2575 " weak %d one %d init %lx\n",
2576 name, (long long)size, (int)align, TREE_READONLY (decl),
2577 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2578 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2579 #endif
2581 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2582 before the target has a chance to comment. */
2583 if ((meta = is_objc_metadata (decl)))
2585 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2586 return;
2589 /* We shouldn't be messing with this if the decl has a section name. */
2590 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2592 /* We would rather not have to check this here - but it seems that we might
2593 be passed a decl that should be in coalesced space. */
2594 if (one || weak)
2596 /* Weak or COMDAT objects are put in mergable sections. */
2597 darwin_emit_weak_or_comdat (fp, decl, name, size,
2598 DECL_ALIGN (decl));
2599 return;
2602 /* We should only get here for DECL_COMMON, with a zero init (and, in
2603 principle, only for public symbols too - although we deal with local
2604 ones below). */
2606 /* Check the initializer is OK. */
2607 gcc_assert (DECL_COMMON (decl)
2608 && ((DECL_INITIAL (decl) == NULL)
2609 || (DECL_INITIAL (decl) == error_mark_node)
2610 || initializer_zerop (DECL_INITIAL (decl))));
2612 last_assemble_variable_decl = decl;
2614 if (!size || !align)
2615 align = DECL_ALIGN (decl);
2617 l2align = floor_log2 (align / BITS_PER_UNIT);
2618 /* Check we aren't asking for more aligment than the platform allows. */
2619 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2621 if (TREE_PUBLIC (decl) != 0)
2622 darwin_emit_common (fp, name, size, align);
2623 else
2624 darwin_emit_local_bss (fp, decl, name, size, l2align);
2627 /* Output a chunk of BSS with alignment specfied. */
2628 void
2629 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2630 unsigned HOST_WIDE_INT size,
2631 unsigned int align)
2633 unsigned long l2align;
2634 bool one, weak;
2635 tree meta;
2637 one = DECL_ONE_ONLY (decl);
2638 weak = (DECL_P (decl)
2639 && DECL_WEAK (decl)
2640 && !lookup_attribute ("weak_import",
2641 DECL_ATTRIBUTES (decl)));
2643 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2644 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2645 " weak %d init %lx\n",
2646 name, (long long)size, (int)align, TREE_READONLY (decl),
2647 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2648 weak , (unsigned long)DECL_INITIAL (decl));
2649 #endif
2651 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2652 before the target has a chance to comment. */
2653 if ((meta = is_objc_metadata (decl)))
2655 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2656 return;
2659 /* We shouldn't be messing with this if the decl has a section name. */
2660 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2662 /* We would rather not have to check this here - but it seems that we might
2663 be passed a decl that should be in coalesced space. */
2664 if (one || weak)
2666 /* Weak or COMDAT objects are put in mergable sections. */
2667 darwin_emit_weak_or_comdat (fp, decl, name, size,
2668 DECL_ALIGN (decl));
2669 return;
2672 /* .. and it should be suitable for placement in local mem. */
2673 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2674 /* .. and any initializer must be all-zero. */
2675 gcc_assert ((DECL_INITIAL (decl) == NULL)
2676 || (DECL_INITIAL (decl) == error_mark_node)
2677 || initializer_zerop (DECL_INITIAL (decl)));
2679 last_assemble_variable_decl = decl;
2681 if (!size || !align)
2682 align = DECL_ALIGN (decl);
2684 l2align = floor_log2 (align / BITS_PER_UNIT);
2685 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2687 darwin_emit_local_bss (fp, decl, name, size, l2align);
2690 /* Emit an assembler directive to set visibility for a symbol. The
2691 only supported visibilities are VISIBILITY_DEFAULT and
2692 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2693 extern". There is no MACH-O equivalent of ELF's
2694 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2696 void
2697 darwin_assemble_visibility (tree decl, int vis)
2699 if (vis == VISIBILITY_DEFAULT)
2701 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2703 fputs ("\t.private_extern ", asm_out_file);
2704 assemble_name (asm_out_file,
2705 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2706 fputs ("\n", asm_out_file);
2708 else
2709 warning (OPT_Wattributes, "protected visibility attribute "
2710 "not supported in this configuration; ignored");
2713 /* vec used by darwin_asm_dwarf_section.
2714 Maybe a hash tab would be better here - but the intention is that this is
2715 a very short list (fewer than 16 items) and each entry should (ideally,
2716 eventually) only be presented once.
2718 A structure to hold a dwarf debug section used entry. */
2720 typedef struct GTY(()) dwarf_sect_used_entry {
2721 const char *name;
2722 unsigned count;
2724 dwarf_sect_used_entry;
2727 /* A list of used __DWARF sections. */
2728 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2730 /* This is called when we are asked to assemble a named section and the
2731 name begins with __DWARF,. We keep a list of the section names (without
2732 the __DWARF, prefix) and use this to emit our required start label on the
2733 first switch to each section. */
2735 static void
2736 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2737 tree ARG_UNUSED (decl))
2739 unsigned i;
2740 int namelen;
2741 const char * sname;
2742 dwarf_sect_used_entry *ref;
2743 bool found = false;
2744 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2745 == (SECTION_DEBUG | SECTION_NAMED));
2746 /* We know that the name starts with __DWARF, */
2747 sname = name + 8;
2748 namelen = strchr (sname, ',') - sname;
2749 gcc_assert (namelen);
2750 if (dwarf_sect_names_table == NULL)
2751 vec_alloc (dwarf_sect_names_table, 16);
2752 else
2753 for (i = 0;
2754 dwarf_sect_names_table->iterate (i, &ref);
2755 i++)
2757 if (!ref)
2758 break;
2759 if (!strcmp (ref->name, sname))
2761 found = true;
2762 ref->count++;
2763 break;
2767 fprintf (asm_out_file, "\t.section %s\n", name);
2768 if (!found)
2770 dwarf_sect_used_entry e;
2771 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2772 e.count = 1;
2773 e.name = xstrdup (sname);
2774 vec_safe_push (dwarf_sect_names_table, e);
2778 /* Output a difference of two labels that will be an assembly time
2779 constant if the two labels are local. (.long lab1-lab2 will be
2780 very different if lab1 is at the boundary between two sections; it
2781 will be relocated according to the second section, not the first,
2782 so one ends up with a difference between labels in different
2783 sections, which is bad in the dwarf2 eh context for instance.) */
2785 static int darwin_dwarf_label_counter;
2787 void
2788 darwin_asm_output_dwarf_delta (FILE *file, int size,
2789 const char *lab1, const char *lab2)
2791 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2792 && lab2[0] == '*' && lab2[1] == 'L');
2793 const char *directive = (size == 8 ? ".quad" : ".long");
2795 if (islocaldiff)
2796 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2797 else
2798 fprintf (file, "\t%s\t", directive);
2800 assemble_name_raw (file, lab1);
2801 fprintf (file, "-");
2802 assemble_name_raw (file, lab2);
2803 if (islocaldiff)
2804 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2807 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2808 offsets are not represented using relocs in .o files; either the
2809 section never leaves the .o file, or the linker or other tool is
2810 responsible for parsing the DWARF and updating the offsets. */
2812 void
2813 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2814 section *base)
2816 char sname[64];
2817 int namelen;
2819 gcc_assert (base->common.flags & SECTION_NAMED);
2820 gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
2821 gcc_assert (strchr (base->named.name + 8, ','));
2823 namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
2824 sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
2825 darwin_asm_output_dwarf_delta (file, size, lab, sname);
2828 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2830 void
2831 darwin_file_start (void)
2833 /* Nothing to do. */
2836 /* Called for the TARGET_ASM_FILE_END hook.
2837 Emit the mach-o pic indirection data, the lto data and, finally a flag
2838 to tell the linker that it can break the file object into sections and
2839 move those around for efficiency. */
2841 void
2842 darwin_file_end (void)
2844 if (!vec_safe_is_empty (ctors))
2845 finalize_ctors ();
2846 if (!vec_safe_is_empty (dtors))
2847 finalize_dtors ();
2848 machopic_finish (asm_out_file);
2849 if (strcmp (lang_hooks.name, "GNU C++") == 0)
2851 switch_to_section (darwin_sections[constructor_section]);
2852 switch_to_section (darwin_sections[destructor_section]);
2853 ASM_OUTPUT_ALIGN (asm_out_file, 1);
2856 /* If there was LTO assembler output, append it to asm_out_file. */
2857 if (lto_asm_out_name)
2859 int n;
2860 char *buf, *lto_asm_txt;
2862 /* Shouldn't be here if we failed to switch back. */
2863 gcc_assert (! saved_asm_out_file);
2865 lto_asm_out_file = fopen (lto_asm_out_name, "r");
2866 if (lto_asm_out_file == NULL)
2867 fatal_error ("failed to open temporary file %s with LTO output",
2868 lto_asm_out_name);
2869 fseek (lto_asm_out_file, 0, SEEK_END);
2870 n = ftell (lto_asm_out_file);
2871 if (n > 0)
2873 fseek (lto_asm_out_file, 0, SEEK_SET);
2874 lto_asm_txt = buf = (char *) xmalloc (n + 1);
2875 while (fgets (lto_asm_txt, n, lto_asm_out_file))
2876 fputs (lto_asm_txt, asm_out_file);
2877 /* Put a termination label. */
2878 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2879 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2880 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2881 lto_section_num);
2882 /* Make sure our termination label stays in this section. */
2883 fputs ("\t.space\t1\n", asm_out_file);
2886 /* Remove the temporary file. */
2887 fclose (lto_asm_out_file);
2888 unlink_if_ordinary (lto_asm_out_name);
2889 free (lto_asm_out_name);
2892 /* Output the names and indices. */
2893 if (lto_section_names && lto_section_names->length ())
2895 int count;
2896 darwin_lto_section_e *ref;
2897 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
2898 the latter up ourselves. */
2899 const char *op = integer_asm_op (4,0);
2901 /* Emit the names. */
2902 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2903 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
2904 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2906 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
2907 /* We have to jump through hoops to get the values of the intra-section
2908 offsets... */
2909 fprintf (asm_out_file,
2910 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
2911 count, count);
2912 fprintf (asm_out_file,
2913 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
2914 count, count+1, count);
2915 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
2917 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
2918 /* make sure our termination label stays in this section. */
2919 fputs ("\t.space\t1\n", asm_out_file);
2921 /* Emit the Index. */
2922 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2923 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
2924 fputs ("\t.align\t2\n", asm_out_file);
2925 fputs ("# Section offset, Section length, Name offset, Name length\n",
2926 asm_out_file);
2927 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
2929 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
2930 op, count, ref->sectname);
2931 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
2932 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
2933 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
2937 /* If we have section anchors, then we must prevent the linker from
2938 re-arranging data. */
2939 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2940 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
2943 /* TODO: Add a language hook for identifying if a decl is a vtable. */
2944 #define DARWIN_VTABLE_P(DECL) 0
2946 /* Cross-module name binding. Darwin does not support overriding
2947 functions at dynamic-link time, except for vtables in kexts. */
2949 bool
2950 darwin_binds_local_p (const_tree decl)
2952 return default_binds_local_p_1 (decl,
2953 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
2956 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
2957 anchor relative to ".", the current section position. We cannot use
2958 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
2959 void
2960 darwin_asm_output_anchor (rtx symbol)
2962 fprintf (asm_out_file, "\t.set\t");
2963 assemble_name (asm_out_file, XSTR (symbol, 0));
2964 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
2965 SYMBOL_REF_BLOCK_OFFSET (symbol));
2968 /* Disable section anchoring on any section containing a zero-sized
2969 object. */
2970 bool
2971 darwin_use_anchors_for_symbol_p (const_rtx symbol)
2973 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
2975 section *sect;
2976 /* If the section contains a zero-sized object it's ineligible. */
2977 sect = SYMBOL_REF_BLOCK (symbol)->sect;
2978 /* This should have the effect of disabling anchors for vars that follow
2979 any zero-sized one, in a given section. */
2980 if (sect->common.flags & SECTION_NO_ANCHOR)
2981 return false;
2983 /* Also check the normal reasons for suppressing. */
2984 return default_use_anchors_for_symbol_p (symbol);
2986 else
2987 return false;
2990 /* Set the darwin specific attributes on TYPE. */
2991 void
2992 darwin_set_default_type_attributes (tree type)
2994 if (darwin_ms_struct
2995 && TREE_CODE (type) == RECORD_TYPE)
2996 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
2997 NULL_TREE,
2998 TYPE_ATTRIBUTES (type));
3001 /* True, iff we're generating code for loadable kernel extensions. */
3003 bool
3004 darwin_kextabi_p (void) {
3005 return flag_apple_kext;
3008 void
3009 darwin_override_options (void)
3011 /* Keep track of which (major) version we're generating code for. */
3012 if (darwin_macosx_version_min)
3014 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
3015 generating_for_darwin_version = 10;
3016 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
3017 generating_for_darwin_version = 9;
3019 /* Earlier versions are not specifically accounted, until required. */
3022 /* In principle, this should be c-family only. However, we really need to
3023 set sensible defaults for LTO as well, since the section selection stuff
3024 should check for correctness re. the ABI. TODO: check and provide the
3025 flags (runtime & ABI) from the lto wrapper). */
3027 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
3028 if (!global_options_set.x_flag_objc_abi)
3029 global_options.x_flag_objc_abi
3030 = (!flag_next_runtime)
3032 : (TARGET_64BIT ? 2
3033 : (generating_for_darwin_version >= 9) ? 1
3034 : 0);
3036 /* Objective-C family ABI 2 is only valid for next/m64 at present. */
3037 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
3039 if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
3040 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
3041 " used for %<-m64%> targets with"
3042 " %<-fnext-runtime%>");
3043 if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
3044 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
3045 " supported on %<-m32%> targets with"
3046 " %<-fnext-runtime%>");
3049 /* Don't emit DWARF3/4 unless specifically selected. This is a
3050 workaround for tool bugs. */
3051 if (!global_options_set.x_dwarf_strict)
3052 dwarf_strict = 1;
3053 if (!global_options_set.x_dwarf_version)
3054 dwarf_version = 2;
3056 /* Do not allow unwind tables to be generated by default for m32.
3057 fnon-call-exceptions will override this, regardless of what we do. */
3058 if (generating_for_darwin_version < 10
3059 && !global_options_set.x_flag_asynchronous_unwind_tables
3060 && !TARGET_64BIT)
3061 global_options.x_flag_asynchronous_unwind_tables = 0;
3063 /* Disable -freorder-blocks-and-partition when unwind tables are being
3064 emitted for Darwin < 9 (OSX 10.5).
3065 The strategy is, "Unless the User has specifically set/unset an unwind
3066 flag we will switch off -freorder-blocks-and-partition when unwind tables
3067 will be generated". If the User specifically sets flags... we assume
3068 (s)he knows why... */
3069 if (generating_for_darwin_version < 9
3070 && global_options_set.x_flag_reorder_blocks_and_partition
3071 && ((global_options.x_flag_exceptions /* User, c++, java */
3072 && !global_options_set.x_flag_exceptions) /* User specified... */
3073 || (global_options.x_flag_unwind_tables
3074 && !global_options_set.x_flag_unwind_tables)
3075 || (global_options.x_flag_non_call_exceptions
3076 && !global_options_set.x_flag_non_call_exceptions)
3077 || (global_options.x_flag_asynchronous_unwind_tables
3078 && !global_options_set.x_flag_asynchronous_unwind_tables)))
3080 inform (input_location,
3081 "-freorder-blocks-and-partition does not work with exceptions "
3082 "on this architecture");
3083 flag_reorder_blocks_and_partition = 0;
3084 flag_reorder_blocks = 1;
3087 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3088 one valid choice of exception scheme for each runtime. */
3089 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3090 global_options.x_flag_objc_sjlj_exceptions =
3091 flag_next_runtime && !TARGET_64BIT;
3093 /* FIXME: and this could be eliminated then too. */
3094 if (!global_options_set.x_flag_exceptions
3095 && flag_objc_exceptions
3096 && TARGET_64BIT)
3097 flag_exceptions = 1;
3099 if (flag_mkernel || flag_apple_kext)
3101 /* -mkernel implies -fapple-kext for C++ */
3102 if (strcmp (lang_hooks.name, "GNU C++") == 0)
3103 flag_apple_kext = 1;
3105 flag_no_common = 1;
3107 /* No EH in kexts. */
3108 flag_exceptions = 0;
3109 /* No -fnon-call-exceptions data in kexts. */
3110 flag_non_call_exceptions = 0;
3111 /* so no tables either.. */
3112 flag_unwind_tables = 0;
3113 flag_asynchronous_unwind_tables = 0;
3114 /* We still need to emit branch islands for kernel context. */
3115 darwin_emit_branch_islands = true;
3118 if (flag_var_tracking_uninit == 0
3119 && generating_for_darwin_version >= 9
3120 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3121 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3122 && write_symbols == DWARF2_DEBUG)
3123 flag_var_tracking_uninit = flag_var_tracking;
3125 if (MACHO_DYNAMIC_NO_PIC_P)
3127 if (flag_pic)
3128 warning_at (UNKNOWN_LOCATION, 0,
3129 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3130 " %<-fpie%> or %<-fPIE%>");
3131 flag_pic = 0;
3133 else if (flag_pic == 1)
3135 /* Darwin's -fpic is -fPIC. */
3136 flag_pic = 2;
3139 /* It is assumed that branch island stubs are needed for earlier systems. */
3140 if (generating_for_darwin_version < 9)
3141 darwin_emit_branch_islands = true;
3142 else
3143 emit_aligned_common = true; /* Later systems can support aligned common. */
3145 /* The c_dialect...() macros are not available to us here. */
3146 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3149 #if DARWIN_PPC
3150 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3152 static void
3153 darwin_patch_builtin (enum built_in_function fncode)
3155 tree fn = builtin_decl_explicit (fncode);
3156 tree sym;
3157 char *newname;
3159 if (!fn)
3160 return;
3162 sym = DECL_ASSEMBLER_NAME (fn);
3163 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3165 set_user_assembler_name (fn, newname);
3167 fn = builtin_decl_implicit (fncode);
3168 if (fn)
3169 set_user_assembler_name (fn, newname);
3172 void
3173 darwin_patch_builtins (void)
3175 if (LONG_DOUBLE_TYPE_SIZE != 128)
3176 return;
3178 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3179 #define PATCH_BUILTIN_NO64(fncode) \
3180 if (!TARGET_64BIT) \
3181 darwin_patch_builtin (fncode);
3182 #define PATCH_BUILTIN_VARIADIC(fncode) \
3183 if (!TARGET_64BIT \
3184 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3185 darwin_patch_builtin (fncode);
3186 #include "darwin-ppc-ldouble-patch.def"
3187 #undef PATCH_BUILTIN
3188 #undef PATCH_BUILTIN_NO64
3189 #undef PATCH_BUILTIN_VARIADIC
3191 #endif
3193 /* CFStrings implementation. */
3194 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3195 static GTY(()) tree cfstring_type_node = NULL_TREE;
3196 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3197 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3198 static GTY(()) tree pcint_type_node = NULL_TREE;
3199 static GTY(()) tree pcchar_type_node = NULL_TREE;
3201 static enum built_in_function darwin_builtin_cfstring;
3203 /* Store all constructed constant CFStrings in a hash table so that
3204 they get uniqued properly. */
3206 typedef struct GTY (()) cfstring_descriptor {
3207 /* The string literal. */
3208 tree literal;
3209 /* The resulting constant CFString. */
3210 tree constructor;
3211 } cfstring_descriptor;
3213 static GTY ((param_is (struct cfstring_descriptor))) htab_t cfstring_htab;
3215 static hashval_t cfstring_hash (const void *);
3216 static int cfstring_eq (const void *, const void *);
3218 static tree
3219 add_builtin_field_decl (tree type, const char *name, tree **chain)
3221 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3222 get_identifier (name), type);
3224 if (*chain != NULL)
3225 **chain = field;
3226 *chain = &DECL_CHAIN (field);
3228 return field;
3231 tree
3232 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3234 tree cfsfun, fields, pccfstring_ftype_pcchar;
3235 tree *chain = NULL;
3237 darwin_builtin_cfstring =
3238 (enum built_in_function) builtin_cfstring;
3240 /* struct __builtin_CFString {
3241 const int *isa; (will point at
3242 int flags; __CFConstantStringClassReference)
3243 const char *str;
3244 long length;
3245 }; */
3247 pcint_type_node = build_pointer_type
3248 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3250 pcchar_type_node = build_pointer_type
3251 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3253 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3255 /* Have to build backwards for finish struct. */
3256 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3257 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3258 add_builtin_field_decl (integer_type_node, "flags", &chain);
3259 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3260 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3261 fields, NULL_TREE);
3263 /* const struct __builtin_CFstring *
3264 __builtin___CFStringMakeConstantString (const char *); */
3266 ccfstring_type_node = build_qualified_type
3267 (cfstring_type_node, TYPE_QUAL_CONST);
3268 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3269 pccfstring_ftype_pcchar = build_function_type_list
3270 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3272 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3273 get_identifier ("__builtin___CFStringMakeConstantString"),
3274 pccfstring_ftype_pcchar);
3276 TREE_PUBLIC (cfsfun) = 1;
3277 DECL_EXTERNAL (cfsfun) = 1;
3278 DECL_ARTIFICIAL (cfsfun) = 1;
3279 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3280 in place of the existing, which may be NULL. */
3281 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3282 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3283 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
3284 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
3285 lang_hooks.builtin_function (cfsfun);
3287 /* extern int __CFConstantStringClassReference[]; */
3288 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3289 get_identifier ("__CFConstantStringClassReference"),
3290 build_array_type (integer_type_node, NULL_TREE));
3292 TREE_PUBLIC (cfstring_class_reference) = 1;
3293 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3294 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3295 DECL_EXTERNAL (cfstring_class_reference) = 1;
3296 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3298 /* Initialize the hash table used to hold the constant CFString objects. */
3299 cfstring_htab = htab_create_ggc (31, cfstring_hash, cfstring_eq, NULL);
3301 return cfstring_type_node;
3304 tree
3305 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3306 bool ARG_UNUSED (ignore))
3308 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3310 if (fcode == darwin_builtin_cfstring)
3312 if (!darwin_constant_cfstrings)
3314 error ("built-in function %qD requires the"
3315 " %<-mconstant-cfstrings%> flag", fndecl);
3316 return error_mark_node;
3319 if (n_args != 1)
3321 error ("built-in function %qD takes one argument only", fndecl);
3322 return error_mark_node;
3325 return darwin_build_constant_cfstring (*argp);
3328 return NULL_TREE;
3331 void
3332 darwin_rename_builtins (void)
3334 /* The system ___divdc3 routine in libSystem on darwin10 is not
3335 accurate to 1ulp, ours is, so we avoid ever using the system name
3336 for this routine and instead install a non-conflicting name that
3337 is accurate.
3339 When -ffast-math or -funsafe-math-optimizations is given, we can
3340 use the faster version. */
3341 if (!flag_unsafe_math_optimizations)
3343 enum built_in_function dcode
3344 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3345 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3346 tree fn = builtin_decl_explicit (dcode);
3347 /* Fortran and c call TARGET_INIT_BUILTINS and
3348 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3349 call into each to ensure that at least one of them is called
3350 after build_common_builtin_nodes. A better fix is to add a
3351 new hook to run after build_common_builtin_nodes runs. */
3352 if (fn)
3353 set_user_assembler_name (fn, "___ieee_divdc3");
3354 fn = builtin_decl_implicit (dcode);
3355 if (fn)
3356 set_user_assembler_name (fn, "___ieee_divdc3");
3360 bool
3361 darwin_libc_has_function (enum function_class fn_class)
3363 if (fn_class == function_sincos)
3364 return false;
3365 if (fn_class == function_c99_math_complex
3366 || fn_class == function_c99_misc)
3367 return (TARGET_64BIT
3368 || strverscmp (darwin_macosx_version_min, "10.3") >= 0);
3370 return true;
3373 static hashval_t
3374 cfstring_hash (const void *ptr)
3376 tree str = ((const struct cfstring_descriptor *)ptr)->literal;
3377 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3378 int i, len = TREE_STRING_LENGTH (str);
3379 hashval_t h = len;
3381 for (i = 0; i < len; i++)
3382 h = ((h * 613) + p[i]);
3384 return h;
3387 static int
3388 cfstring_eq (const void *ptr1, const void *ptr2)
3390 tree str1 = ((const struct cfstring_descriptor *)ptr1)->literal;
3391 tree str2 = ((const struct cfstring_descriptor *)ptr2)->literal;
3392 int len1 = TREE_STRING_LENGTH (str1);
3394 return (len1 == TREE_STRING_LENGTH (str2)
3395 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3396 len1));
3399 tree
3400 darwin_build_constant_cfstring (tree str)
3402 struct cfstring_descriptor *desc, key;
3403 void **loc;
3404 tree addr;
3406 if (!str)
3408 error ("CFString literal is missing");
3409 return error_mark_node;
3412 STRIP_NOPS (str);
3414 if (TREE_CODE (str) == ADDR_EXPR)
3415 str = TREE_OPERAND (str, 0);
3417 if (TREE_CODE (str) != STRING_CST)
3419 error ("CFString literal expression is not a string constant");
3420 return error_mark_node;
3423 /* Perhaps we already constructed a constant CFString just like this one? */
3424 key.literal = str;
3425 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3426 desc = (struct cfstring_descriptor *) *loc;
3428 if (!desc)
3430 tree var, constructor, field;
3431 vec<constructor_elt, va_gc> *v = NULL;
3432 int length = TREE_STRING_LENGTH (str) - 1;
3434 if (darwin_warn_nonportable_cfstrings)
3436 const char *s = TREE_STRING_POINTER (str);
3437 int l = 0;
3439 for (l = 0; l < length; l++)
3440 if (!s[l] || !isascii (s[l]))
3442 warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
3443 s[l] ? "non-ASCII character" : "embedded NUL");
3444 break;
3448 *loc = desc = ggc_alloc_cleared_cfstring_descriptor ();
3449 desc->literal = str;
3451 /* isa *. */
3452 field = TYPE_FIELDS (ccfstring_type_node);
3453 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3454 build1 (ADDR_EXPR, TREE_TYPE (field),
3455 cfstring_class_reference));
3456 /* flags */
3457 field = DECL_CHAIN (field);
3458 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3459 build_int_cst (TREE_TYPE (field), 0x000007c8));
3460 /* string *. */
3461 field = DECL_CHAIN (field);
3462 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3463 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3464 /* length */
3465 field = DECL_CHAIN (field);
3466 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3467 build_int_cst (TREE_TYPE (field), length));
3469 constructor = build_constructor (ccfstring_type_node, v);
3470 TREE_READONLY (constructor) = 1;
3471 TREE_CONSTANT (constructor) = 1;
3472 TREE_STATIC (constructor) = 1;
3474 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3475 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3476 being built without any knowledge of C++ tree accessors; hence, we shall
3477 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3478 if (darwin_running_cxx)
3479 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3481 /* Create an anonymous global variable for this CFString. */
3482 var = build_decl (input_location, CONST_DECL,
3483 NULL, TREE_TYPE (constructor));
3484 DECL_ARTIFICIAL (var) = 1;
3485 TREE_STATIC (var) = 1;
3486 DECL_INITIAL (var) = constructor;
3487 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3488 DECL_CONTEXT (var) = NULL_TREE;
3489 desc->constructor = var;
3492 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3493 TREE_CONSTANT (addr) = 1;
3495 return addr;
3498 bool
3499 darwin_cfstring_p (tree str)
3501 struct cfstring_descriptor key;
3502 void **loc;
3504 if (!str)
3505 return false;
3507 STRIP_NOPS (str);
3509 if (TREE_CODE (str) == ADDR_EXPR)
3510 str = TREE_OPERAND (str, 0);
3512 if (TREE_CODE (str) != STRING_CST)
3513 return false;
3515 key.literal = str;
3516 loc = htab_find_slot (cfstring_htab, &key, NO_INSERT);
3518 if (loc)
3519 return true;
3521 return false;
3524 void
3525 darwin_enter_string_into_cfstring_table (tree str)
3527 struct cfstring_descriptor key;
3528 void **loc;
3530 key.literal = str;
3531 loc = htab_find_slot (cfstring_htab, &key, INSERT);
3533 if (!*loc)
3535 *loc = ggc_alloc_cleared_cfstring_descriptor ();
3536 ((struct cfstring_descriptor *)*loc)->literal = str;
3540 /* Choose named function section based on its frequency. */
3542 section *
3543 darwin_function_section (tree decl, enum node_frequency freq,
3544 bool startup, bool exit)
3546 /* Decide if we need to put this in a coalescable section. */
3547 bool weak = (decl
3548 && DECL_WEAK (decl)
3549 && (!DECL_ATTRIBUTES (decl)
3550 || !lookup_attribute ("weak_import",
3551 DECL_ATTRIBUTES (decl))));
3553 /* If there is a specified section name, we should not be trying to
3554 override. */
3555 if (decl && DECL_SECTION_NAME (decl) != NULL_TREE)
3556 return get_named_section (decl, NULL, 0);
3558 /* Default when there is no function re-ordering. */
3559 if (!flag_reorder_functions)
3560 return (weak)
3561 ? darwin_sections[text_coal_section]
3562 : text_section;
3564 /* Startup code should go to startup subsection unless it is
3565 unlikely executed (this happens especially with function splitting
3566 where we can split away unnecessary parts of static constructors). */
3567 if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
3568 return (weak)
3569 ? darwin_sections[text_startup_coal_section]
3570 : darwin_sections[text_startup_section];
3572 /* Similarly for exit. */
3573 if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
3574 return (weak)
3575 ? darwin_sections[text_exit_coal_section]
3576 : darwin_sections[text_exit_section];
3578 /* Group cold functions together, similarly for hot code. */
3579 switch (freq)
3581 case NODE_FREQUENCY_UNLIKELY_EXECUTED:
3582 return (weak)
3583 ? darwin_sections[text_cold_coal_section]
3584 : darwin_sections[text_cold_section];
3585 break;
3586 case NODE_FREQUENCY_HOT:
3587 return (weak)
3588 ? darwin_sections[text_hot_coal_section]
3589 : darwin_sections[text_hot_section];
3590 break;
3591 default:
3592 return (weak)
3593 ? darwin_sections[text_coal_section]
3594 : text_section;
3595 break;
3599 /* When a function is partitioned between sections, we need to insert a label
3600 at the start of each new chunk - so that it may become a valid 'atom' for
3601 eh and debug purposes. Without this the linker will emit warnings if one
3602 tries to add line location information (since the switched fragment will
3603 be anonymous). */
3605 void
3606 darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
3608 char buf[128];
3609 snprintf (buf, 128, "%s%s",new_is_cold?"__cold_sect_of_":"__hot_sect_of_",
3610 IDENTIFIER_POINTER (DECL_NAME (decl)));
3611 /* Make sure we pick up all the relevant quotes etc. */
3612 assemble_name_raw (fp, (const char *) buf);
3613 fputs (":\n", fp);
3616 #include "gt-darwin.h"