Daily bump.
[official-gcc.git] / gcc / config / darwin.c
blobb6dad70df0ab5a2a54e32cb69907b3d9ebcf4d4e
1 /* Functions for generic Darwin as target machine for GNU C compiler.
2 Copyright (C) 1989-2017 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 "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "cfghooks.h"
30 #include "df.h"
31 #include "memmodel.h"
32 #include "tm_p.h"
33 #include "stringpool.h"
34 #include "attribs.h"
35 #include "insn-config.h"
36 #include "emit-rtl.h"
37 #include "cgraph.h"
38 #include "lto-streamer.h"
39 #include "output.h"
40 #include "varasm.h"
41 #include "stor-layout.h"
42 #include "explow.h"
43 #include "expr.h"
44 #include "langhooks.h"
45 #include "toplev.h"
46 #include "lto-section-names.h"
48 /* Darwin supports a feature called fix-and-continue, which is used
49 for rapid turn around debugging. When code is compiled with the
50 -mfix-and-continue flag, two changes are made to the generated code
51 that allow the system to do things that it would normally not be
52 able to do easily. These changes allow gdb to load in
53 recompilation of a translation unit that has been changed into a
54 running program and replace existing functions and methods of that
55 translation unit with versions of those functions and methods
56 from the newly compiled translation unit. The new functions access
57 the existing static symbols from the old translation unit, if the
58 symbol existed in the unit to be replaced, and from the new
59 translation unit, otherwise.
61 The changes are to insert 5 nops at the beginning of all functions
62 and to use indirection to get at static symbols. The 5 nops
63 are required by consumers of the generated code. Currently, gdb
64 uses this to patch in a jump to the overriding function, this
65 allows all uses of the old name to forward to the replacement,
66 including existing function pointers and virtual methods. See
67 rs6000_emit_prologue for the code that handles the nop insertions.
69 The added indirection allows gdb to redirect accesses to static
70 symbols from the newly loaded translation unit to the existing
71 symbol, if any. @code{static} symbols are special and are handled by
72 setting the second word in the .non_lazy_symbol_pointer data
73 structure to symbol. See indirect_data for the code that handles
74 the extra indirection, and machopic_output_indirection and its use
75 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
76 symbol indirection. */
78 /* For darwin >= 9 (OSX 10.5) the linker is capable of making the necessary
79 branch islands and we no longer need to emit darwin stubs.
80 However, if we are generating code for earlier systems (or for use in the
81 kernel) the stubs might still be required, and this will be set true. */
82 int darwin_emit_branch_islands = false;
84 typedef struct GTY(()) cdtor_record {
85 rtx symbol;
86 int priority; /* [con/de]structor priority */
87 int position; /* original position */
88 } cdtor_record;
90 static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL;
91 static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL;
93 /* A flag to determine whether we are running c++ or obj-c++. This has to be
94 settable from non-c-family contexts too (i.e. we can't use the c_dialect_
95 functions). */
96 int darwin_running_cxx;
98 /* Some code-gen now depends on OS major version numbers (at least). */
99 int generating_for_darwin_version ;
101 /* For older linkers we need to emit special sections (marked 'coalesced') for
102 for weak or single-definition items. */
103 static bool ld_uses_coal_sects = false;
105 /* Section names. */
106 section * darwin_sections[NUM_DARWIN_SECTIONS];
108 /* While we transition to using in-tests instead of ifdef'd code. */
109 #if !HAVE_lo_sum
110 #define gen_macho_high(a,b) (a)
111 #define gen_macho_low(a,b,c) (a)
112 #endif
114 /* True if we're setting __attribute__ ((ms_struct)). */
115 int darwin_ms_struct = false;
117 /* Earlier versions of Darwin as do not recognize an alignment field in
118 .comm directives, this should be set for versions that allow it. */
119 int emit_aligned_common = false;
121 /* A get_unnamed_section callback used to switch to an ObjC section.
122 DIRECTIVE is as for output_section_asm_op. */
124 static void
125 output_objc_section_asm_op (const void *directive)
127 static bool been_here = false;
129 /* The NeXT ObjC Runtime requires these sections to be present and in
130 order in the object. The code below implements this by emitting
131 a section header for each ObjC section the first time that an ObjC
132 section is requested. */
133 if (! been_here)
135 section *saved_in_section = in_section;
136 static const enum darwin_section_enum tomark[] =
138 /* written, cold -> hot */
139 objc_cat_cls_meth_section,
140 objc_cat_inst_meth_section,
141 objc_string_object_section,
142 objc_constant_string_object_section,
143 objc_selector_refs_section,
144 objc_selector_fixup_section,
145 objc_cls_refs_section,
146 objc_class_section,
147 objc_meta_class_section,
148 /* shared, hot -> cold */
149 objc_cls_meth_section,
150 objc_inst_meth_section,
151 objc_protocol_section,
152 objc_class_names_section,
153 objc_meth_var_types_section,
154 objc_meth_var_names_section,
155 objc_category_section,
156 objc_class_vars_section,
157 objc_instance_vars_section,
158 objc_module_info_section,
159 objc_symbols_section,
161 /* ABI=1 */
162 static const enum darwin_section_enum tomarkv1[] =
164 objc1_protocol_ext_section,
165 objc1_class_ext_section,
166 objc1_prop_list_section
168 /* ABI=2 */
169 static const enum darwin_section_enum tomarkv2[] =
171 objc2_message_refs_section,
172 objc2_classdefs_section,
173 objc2_metadata_section,
174 objc2_classrefs_section,
175 objc2_classlist_section,
176 objc2_categorylist_section,
177 objc2_selector_refs_section,
178 objc2_nonlazy_class_section,
179 objc2_nonlazy_category_section,
180 objc2_protocollist_section,
181 objc2_protocolrefs_section,
182 objc2_super_classrefs_section,
183 objc2_image_info_section,
184 objc2_constant_string_object_section
186 size_t i;
188 been_here = true;
189 if (flag_objc_abi < 2)
191 for (i = 0; i < ARRAY_SIZE (tomark); i++)
192 switch_to_section (darwin_sections[tomark[i]]);
193 if (flag_objc_abi == 1)
194 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
195 switch_to_section (darwin_sections[tomarkv1[i]]);
197 else
198 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
199 switch_to_section (darwin_sections[tomarkv2[i]]);
200 /* Make sure we don't get varasm.c out of sync with us. */
201 switch_to_section (saved_in_section);
203 output_section_asm_op (directive);
207 /* Private flag applied to disable section-anchors in a particular section. */
208 #define SECTION_NO_ANCHOR SECTION_MACH_DEP
211 /* Implement TARGET_ASM_INIT_SECTIONS. */
213 void
214 darwin_init_sections (void)
216 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
217 darwin_sections[NAME] = \
218 get_unnamed_section (FLAGS, (OBJC \
219 ? output_objc_section_asm_op \
220 : output_section_asm_op), \
221 "\t" DIRECTIVE);
222 #include "config/darwin-sections.def"
223 #undef DEF_SECTION
225 readonly_data_section = darwin_sections[const_section];
226 exception_section = darwin_sections[darwin_exception_section];
227 eh_frame_section = darwin_sections[darwin_eh_frame_section];
229 /* If our linker is new enough to coalesce weak symbols, then we
230 can just put picbase_thunks into the text section. */
231 if (! ld_uses_coal_sects )
232 darwin_sections[picbase_thunk_section] = text_section;
236 name_needs_quotes (const char *name)
238 int c;
239 while ((c = *name++) != '\0')
240 if (! ISIDNUM (c)
241 && c != '.' && c != '$' && c != '_' )
242 return 1;
243 return 0;
246 /* Return true if SYM_REF can be used without an indirection. */
248 machopic_symbol_defined_p (rtx sym_ref)
250 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
251 return true;
253 /* If a symbol references local and is not an extern to this
254 file, then the symbol might be able to declared as defined. */
255 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
257 /* If the symbol references a variable and the variable is a
258 common symbol, then this symbol is not defined. */
259 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
261 tree decl = SYMBOL_REF_DECL (sym_ref);
262 if (!decl)
263 return true;
264 if (DECL_COMMON (decl))
265 return false;
267 return true;
269 return false;
272 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
273 reference, which will not be changed. */
275 enum machopic_addr_class
276 machopic_classify_symbol (rtx sym_ref)
278 bool function_p;
280 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
281 if (machopic_symbol_defined_p (sym_ref))
282 return (function_p
283 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
284 else
285 return (function_p
286 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
289 #ifndef TARGET_FIX_AND_CONTINUE
290 #define TARGET_FIX_AND_CONTINUE 0
291 #endif
293 /* Indicate when fix-and-continue style code generation is being used
294 and when a reference to data should be indirected so that it can be
295 rebound in a new translation unit to reference the original instance
296 of that data. Symbol names that are for code generation local to
297 the translation unit are bound to the new translation unit;
298 currently this means symbols that begin with L or _OBJC_;
299 otherwise, we indicate that an indirect reference should be made to
300 permit the runtime to rebind new instances of the translation unit
301 to the original instance of the data. */
303 static int
304 indirect_data (rtx sym_ref)
306 int lprefix;
307 const char *name;
309 /* If we aren't generating fix-and-continue code, don't do anything
310 special. */
311 if (TARGET_FIX_AND_CONTINUE == 0)
312 return 0;
314 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
315 are indirected. Symbols that begin with L and _OBJC_ are always
316 bound to the current translation unit as they are used for
317 generated local data of the translation unit. */
319 name = XSTR (sym_ref, 0);
321 lprefix = (((name[0] == '*' || name[0] == '&')
322 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
323 || (strncmp (name, "_OBJC_", 6) == 0));
325 return ! lprefix;
328 static int
329 machopic_data_defined_p (rtx sym_ref)
331 if (indirect_data (sym_ref))
332 return 0;
334 switch (machopic_classify_symbol (sym_ref))
336 case MACHOPIC_DEFINED_DATA:
337 case MACHOPIC_DEFINED_FUNCTION:
338 return 1;
339 default:
340 return 0;
344 void
345 machopic_define_symbol (rtx mem)
347 rtx sym_ref;
349 gcc_assert (GET_CODE (mem) == MEM);
350 sym_ref = XEXP (mem, 0);
351 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
354 /* Return either ORIG or:
356 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
358 depending on MACHO_DYNAMIC_NO_PIC_P. */
360 machopic_gen_offset (rtx orig)
362 if (MACHO_DYNAMIC_NO_PIC_P)
363 return orig;
364 else
366 /* Play games to avoid marking the function as needing pic if we
367 are being called as part of the cost-estimation process. */
368 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
369 crtl->uses_pic_offset_table = 1;
370 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
371 UNSPEC_MACHOPIC_OFFSET);
372 return gen_rtx_CONST (Pmode, orig);
376 static GTY(()) const char * function_base_func_name;
377 static GTY(()) int current_pic_label_num;
378 static GTY(()) int emitted_pic_label_num;
380 static void
381 update_pic_label_number_if_needed (void)
383 const char *current_name;
385 /* When we are generating _get_pc thunks within stubs, there is no current
386 function. */
387 if (current_function_decl)
389 current_name =
390 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
391 if (function_base_func_name != current_name)
393 ++current_pic_label_num;
394 function_base_func_name = current_name;
397 else
399 ++current_pic_label_num;
400 function_base_func_name = "L_machopic_stub_dummy";
404 void
405 machopic_output_function_base_name (FILE *file)
407 /* If dynamic-no-pic is on, we should not get here. */
408 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
410 update_pic_label_number_if_needed ();
411 fprintf (file, "L%d$pb", current_pic_label_num);
414 char curr_picbasename[32];
416 const char *
417 machopic_get_function_picbase (void)
419 /* If dynamic-no-pic is on, we should not get here. */
420 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
422 update_pic_label_number_if_needed ();
423 snprintf (curr_picbasename, 32, "L%d$pb", current_pic_label_num);
424 return (const char *) curr_picbasename;
427 bool
428 machopic_should_output_picbase_label (void)
430 update_pic_label_number_if_needed ();
432 if (current_pic_label_num == emitted_pic_label_num)
433 return false;
435 emitted_pic_label_num = current_pic_label_num;
436 return true;
439 /* The suffix attached to non-lazy pointer symbols. */
440 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
441 /* The suffix attached to stub symbols. */
442 #define STUB_SUFFIX "$stub"
444 typedef struct GTY ((for_user)) machopic_indirection
446 /* The SYMBOL_REF for the entity referenced. */
447 rtx symbol;
448 /* The name of the stub or non-lazy pointer. */
449 const char * ptr_name;
450 /* True iff this entry is for a stub (as opposed to a non-lazy
451 pointer). */
452 bool stub_p;
453 /* True iff this stub or pointer has been referenced. */
454 bool used;
455 } machopic_indirection;
457 struct indirection_hasher : ggc_ptr_hash<machopic_indirection>
459 typedef const char *compare_type;
460 static hashval_t hash (machopic_indirection *);
461 static bool equal (machopic_indirection *, const char *);
464 /* A table mapping stub names and non-lazy pointer names to
465 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
467 static GTY (()) hash_table<indirection_hasher> *machopic_indirections;
469 /* Return a hash value for a SLOT in the indirections hash table. */
471 hashval_t
472 indirection_hasher::hash (machopic_indirection *p)
474 return htab_hash_string (p->ptr_name);
477 /* Returns true if the KEY is the same as that associated with
478 SLOT. */
480 bool
481 indirection_hasher::equal (machopic_indirection *s, const char *k)
483 return strcmp (s->ptr_name, k) == 0;
486 /* Return the name of the non-lazy pointer (if STUB_P is false) or
487 stub (if STUB_B is true) corresponding to the given name.
489 If we have a situation like:
491 global_weak_symbol:
492 ....
493 Lnon_weak_local:
494 ....
496 ld64 will be unable to split this into two atoms (because the "L" makes
497 the second symbol 'invisible'). This means that legitimate direct accesses
498 to the second symbol will appear to be non-allowed direct accesses to an
499 atom of type weak, global which are not allowed.
501 To avoid this, we make the indirections have a leading 'l' (lower-case L)
502 which has a special meaning: linker can see this and use it to determine
503 atoms, but it is not placed into the final symbol table.
505 The implementation here is somewhat heavy-handed in that it will also mark
506 indirections to the __IMPORT,__pointers section the same way which is
507 really unnecessary, since ld64 _can_ split those into atoms as they are
508 fixed size. FIXME: determine if this is a penalty worth extra code to
509 fix.
513 const char *
514 machopic_indirection_name (rtx sym_ref, bool stub_p)
516 char *buffer;
517 const char *name = XSTR (sym_ref, 0);
518 size_t namelen = strlen (name);
519 machopic_indirection *p;
520 bool needs_quotes;
521 const char *suffix;
522 char L_or_l = 'L';
523 const char *prefix = user_label_prefix;
524 const char *quote = "";
525 tree id;
527 id = maybe_get_identifier (name);
528 if (id)
530 tree id_orig = id;
532 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
533 id = TREE_CHAIN (id);
534 if (id != id_orig)
536 name = IDENTIFIER_POINTER (id);
537 namelen = strlen (name);
541 if (name[0] == '*')
543 prefix = "";
544 ++name;
545 --namelen;
548 needs_quotes = name_needs_quotes (name);
549 if (needs_quotes)
551 quote = "\"";
554 if (stub_p)
555 suffix = STUB_SUFFIX;
556 else
558 suffix = NON_LAZY_POINTER_SUFFIX;
559 /* Let the linker see this. */
560 L_or_l = 'l';
563 buffer = XALLOCAVEC (char, 2 /* strlen ("&L") or ("&l") */
564 + strlen (prefix)
565 + namelen
566 + strlen (suffix)
567 + 2 * strlen (quote)
568 + 1 /* '\0' */);
570 /* Construct the name of the non-lazy pointer or stub. */
571 sprintf (buffer, "&%s%c%s%s%s%s", quote, L_or_l, prefix, name, suffix, quote);
573 if (!machopic_indirections)
574 machopic_indirections = hash_table<indirection_hasher>::create_ggc (37);
576 machopic_indirection **slot
577 = machopic_indirections->find_slot_with_hash (buffer,
578 htab_hash_string (buffer),
579 INSERT);
580 if (*slot)
582 p = *slot;
584 else
586 p = ggc_alloc<machopic_indirection> ();
587 p->symbol = sym_ref;
588 p->ptr_name = xstrdup (buffer);
589 p->stub_p = stub_p;
590 p->used = false;
591 *slot = p;
594 return p->ptr_name;
597 /* Return the name of the stub for the mcount function. */
599 const char*
600 machopic_mcount_stub_name (void)
602 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
603 return machopic_indirection_name (symbol, /*stub_p=*/true);
606 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
607 or non-lazy pointer as used -- and mark the object to which the
608 pointer/stub refers as used as well, since the pointer/stub will
609 emit a reference to it. */
611 void
612 machopic_validate_stub_or_non_lazy_ptr (const char *name)
614 machopic_indirection *p
615 = machopic_indirections->find_with_hash (name, htab_hash_string (name));
616 if (p && ! p->used)
618 const char *real_name;
619 tree id;
621 p->used = true;
623 /* Do what output_addr_const will do when we actually call it. */
624 if (SYMBOL_REF_DECL (p->symbol))
625 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
627 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
629 id = maybe_get_identifier (real_name);
630 if (id)
631 mark_referenced (id);
635 /* Transform ORIG, which may be any data source, to the corresponding
636 source using indirections. */
639 machopic_indirect_data_reference (rtx orig, rtx reg)
641 rtx ptr_ref = orig;
643 if (! MACHOPIC_INDIRECT)
644 return orig;
646 if (GET_CODE (orig) == SYMBOL_REF)
648 int defined = machopic_data_defined_p (orig);
650 if (defined && MACHO_DYNAMIC_NO_PIC_P)
652 if (DARWIN_PPC)
654 /* Create a new register for CSE opportunities. */
655 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
656 emit_insn (gen_macho_high (hi_reg, orig));
657 emit_insn (gen_macho_low (reg, hi_reg, orig));
658 return reg;
660 else if (DARWIN_X86)
661 return orig;
662 else
663 /* some other cpu -- writeme! */
664 gcc_unreachable ();
666 else if (defined)
668 rtx offset = NULL;
669 if (DARWIN_PPC || HAVE_lo_sum)
670 offset = machopic_gen_offset (orig);
672 if (DARWIN_PPC)
674 rtx hi_sum_reg = (!can_create_pseudo_p ()
675 ? reg
676 : gen_reg_rtx (Pmode));
678 gcc_assert (reg);
680 emit_insn (gen_rtx_SET (hi_sum_reg,
681 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
682 gen_rtx_HIGH (Pmode, offset))));
683 emit_insn (gen_rtx_SET (reg,
684 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
685 copy_rtx (offset))));
687 orig = reg;
689 else if (HAVE_lo_sum)
691 gcc_assert (reg);
693 emit_insn (gen_rtx_SET (reg, gen_rtx_HIGH (Pmode, offset)));
694 emit_insn (gen_rtx_SET (reg, gen_rtx_LO_SUM (Pmode, reg,
695 copy_rtx (offset))));
696 emit_use (pic_offset_table_rtx);
698 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
700 return orig;
703 ptr_ref = (gen_rtx_SYMBOL_REF
704 (Pmode,
705 machopic_indirection_name (orig, /*stub_p=*/false)));
707 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
709 ptr_ref = gen_const_mem (Pmode, ptr_ref);
710 machopic_define_symbol (ptr_ref);
712 if (DARWIN_X86
713 && reg
714 && MACHO_DYNAMIC_NO_PIC_P)
716 emit_insn (gen_rtx_SET (reg, ptr_ref));
717 ptr_ref = reg;
720 return ptr_ref;
722 else if (GET_CODE (orig) == CONST)
724 /* If "(const (plus ...", walk the PLUS and return that result.
725 PLUS processing (below) will restore the "(const ..." if
726 appropriate. */
727 if (GET_CODE (XEXP (orig, 0)) == PLUS)
728 return machopic_indirect_data_reference (XEXP (orig, 0), reg);
729 else
730 return orig;
732 else if (GET_CODE (orig) == MEM)
734 XEXP (ptr_ref, 0) =
735 machopic_indirect_data_reference (XEXP (orig, 0), reg);
736 return ptr_ref;
738 else if (GET_CODE (orig) == PLUS)
740 rtx base, result;
741 /* When the target is i386, this code prevents crashes due to the
742 compiler's ignorance on how to move the PIC base register to
743 other registers. (The reload phase sometimes introduces such
744 insns.) */
745 if (GET_CODE (XEXP (orig, 0)) == REG
746 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
747 /* Prevent the same register from being erroneously used
748 as both the base and index registers. */
749 && (DARWIN_X86 && (GET_CODE (XEXP (orig, 1)) == CONST))
750 && reg)
752 emit_move_insn (reg, XEXP (orig, 0));
753 XEXP (ptr_ref, 0) = reg;
754 return ptr_ref;
757 /* Legitimize both operands of the PLUS. */
758 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
759 orig = machopic_indirect_data_reference (XEXP (orig, 1),
760 (base == reg ? 0 : reg));
761 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
762 result = plus_constant (Pmode, base, INTVAL (orig));
763 else
764 result = gen_rtx_PLUS (Pmode, base, orig);
766 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
768 if (reg)
770 emit_move_insn (reg, result);
771 result = reg;
773 else
775 result = force_reg (GET_MODE (result), result);
779 return result;
781 return ptr_ref;
784 /* Transform TARGET (a MEM), which is a function call target, to the
785 corresponding symbol_stub if necessary. Return a new MEM. */
788 machopic_indirect_call_target (rtx target)
790 if (! darwin_emit_branch_islands)
791 return target;
793 if (GET_CODE (target) != MEM)
794 return target;
796 if (MACHOPIC_INDIRECT
797 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
798 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
799 & MACHO_SYMBOL_FLAG_DEFINED))
801 rtx sym_ref = XEXP (target, 0);
802 const char *stub_name = machopic_indirection_name (sym_ref,
803 /*stub_p=*/true);
804 machine_mode mode = GET_MODE (sym_ref);
806 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
807 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
808 MEM_READONLY_P (target) = 1;
809 MEM_NOTRAP_P (target) = 1;
812 return target;
816 machopic_legitimize_pic_address (rtx orig, machine_mode mode, rtx reg)
818 rtx pic_ref = orig;
820 if (! MACHOPIC_INDIRECT)
821 return orig;
823 /* First handle a simple SYMBOL_REF or LABEL_REF */
824 if (GET_CODE (orig) == LABEL_REF
825 || (GET_CODE (orig) == SYMBOL_REF
828 /* addr(foo) = &func+(foo-func) */
829 orig = machopic_indirect_data_reference (orig, reg);
831 if (GET_CODE (orig) == PLUS
832 && GET_CODE (XEXP (orig, 0)) == REG)
834 if (reg == 0)
835 return force_reg (mode, orig);
837 emit_move_insn (reg, orig);
838 return reg;
841 if (GET_CODE (orig) == MEM)
843 if (reg == 0)
845 gcc_assert (!reload_in_progress);
846 reg = gen_reg_rtx (Pmode);
849 #if HAVE_lo_sum
850 if (MACHO_DYNAMIC_NO_PIC_P
851 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
852 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
854 #if defined (TARGET_TOC) /* ppc */
855 rtx temp_reg = (!can_create_pseudo_p ()
856 ? reg :
857 gen_reg_rtx (Pmode));
858 rtx asym = XEXP (orig, 0);
859 rtx mem;
861 emit_insn (gen_macho_high (temp_reg, asym));
862 mem = gen_const_mem (GET_MODE (orig),
863 gen_rtx_LO_SUM (Pmode, temp_reg,
864 copy_rtx (asym)));
865 emit_insn (gen_rtx_SET (reg, mem));
866 #else
867 /* Some other CPU -- WriteMe! but right now there are no other
868 platforms that can use dynamic-no-pic */
869 gcc_unreachable ();
870 #endif
871 pic_ref = reg;
873 else
874 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
875 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
877 rtx offset = machopic_gen_offset (XEXP (orig, 0));
878 #if defined (TARGET_TOC) /* i.e., PowerPC */
879 /* Generating a new reg may expose opportunities for
880 common subexpression elimination. */
881 rtx hi_sum_reg = (!can_create_pseudo_p ()
882 ? reg
883 : gen_reg_rtx (Pmode));
884 rtx mem;
885 rtx sum;
887 sum = gen_rtx_HIGH (Pmode, offset);
888 if (! MACHO_DYNAMIC_NO_PIC_P)
889 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
891 emit_insn (gen_rtx_SET (hi_sum_reg, sum));
893 mem = gen_const_mem (GET_MODE (orig),
894 gen_rtx_LO_SUM (Pmode,
895 hi_sum_reg,
896 copy_rtx (offset)));
897 rtx_insn *insn = emit_insn (gen_rtx_SET (reg, mem));
898 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
900 pic_ref = reg;
901 #else
902 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
904 emit_insn (gen_rtx_SET (reg,
905 gen_rtx_HIGH (Pmode,
906 gen_rtx_CONST (Pmode,
907 offset))));
908 emit_insn (gen_rtx_SET (reg,
909 gen_rtx_LO_SUM (Pmode, reg,
910 gen_rtx_CONST (Pmode,
911 copy_rtx (offset)))));
912 pic_ref = gen_rtx_PLUS (Pmode,
913 pic_offset_table_rtx, reg);
914 #endif
916 else
917 #endif /* HAVE_lo_sum */
919 rtx pic = pic_offset_table_rtx;
920 if (GET_CODE (pic) != REG)
922 emit_move_insn (reg, pic);
923 pic = reg;
925 #if 0
926 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
927 #endif
929 if (reload_in_progress)
930 df_set_regs_ever_live (REGNO (pic), true);
931 pic_ref = gen_rtx_PLUS (Pmode, pic,
932 machopic_gen_offset (XEXP (orig, 0)));
935 #if !defined (TARGET_TOC)
936 emit_move_insn (reg, pic_ref);
937 pic_ref = gen_const_mem (GET_MODE (orig), reg);
938 #endif
940 else
943 #if HAVE_lo_sum
944 if (GET_CODE (orig) == SYMBOL_REF
945 || GET_CODE (orig) == LABEL_REF)
947 rtx offset = machopic_gen_offset (orig);
948 #if defined (TARGET_TOC) /* i.e., PowerPC */
949 rtx hi_sum_reg;
951 if (reg == 0)
953 gcc_assert (!reload_in_progress);
954 reg = gen_reg_rtx (Pmode);
957 hi_sum_reg = reg;
959 emit_insn (gen_rtx_SET (hi_sum_reg,
960 (MACHO_DYNAMIC_NO_PIC_P)
961 ? gen_rtx_HIGH (Pmode, offset)
962 : gen_rtx_PLUS (Pmode,
963 pic_offset_table_rtx,
964 gen_rtx_HIGH (Pmode,
965 offset))));
966 emit_insn (gen_rtx_SET (reg,
967 gen_rtx_LO_SUM (Pmode,
968 hi_sum_reg,
969 copy_rtx (offset))));
970 pic_ref = reg;
971 #else
972 emit_insn (gen_rtx_SET (reg, gen_rtx_HIGH (Pmode, offset)));
973 emit_insn (gen_rtx_SET (reg,
974 gen_rtx_LO_SUM (Pmode, reg,
975 copy_rtx (offset))));
976 pic_ref = gen_rtx_PLUS (Pmode,
977 pic_offset_table_rtx, reg);
978 #endif
980 else
981 #endif /* HAVE_lo_sum */
983 if (REG_P (orig)
984 || GET_CODE (orig) == SUBREG)
986 return orig;
988 else
990 rtx pic = pic_offset_table_rtx;
991 if (GET_CODE (pic) != REG)
993 emit_move_insn (reg, pic);
994 pic = reg;
996 #if 0
997 emit_use (pic_offset_table_rtx);
998 #endif
999 if (reload_in_progress)
1000 df_set_regs_ever_live (REGNO (pic), true);
1001 pic_ref = gen_rtx_PLUS (Pmode,
1002 pic,
1003 machopic_gen_offset (orig));
1008 if (GET_CODE (pic_ref) != REG)
1010 if (reg != 0)
1012 emit_move_insn (reg, pic_ref);
1013 return reg;
1015 else
1017 return force_reg (mode, pic_ref);
1020 else
1022 return pic_ref;
1026 else if (GET_CODE (orig) == SYMBOL_REF)
1027 return orig;
1029 else if (GET_CODE (orig) == PLUS
1030 && (GET_CODE (XEXP (orig, 0)) == MEM
1031 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
1032 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
1033 && XEXP (orig, 0) != pic_offset_table_rtx
1034 && GET_CODE (XEXP (orig, 1)) != REG)
1037 rtx base;
1038 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
1040 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1041 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
1042 Pmode, (base == reg ? 0 : reg));
1043 if (GET_CODE (orig) == CONST_INT)
1045 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
1046 is_complex = 1;
1048 else
1049 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
1051 if (reg && is_complex)
1053 emit_move_insn (reg, pic_ref);
1054 pic_ref = reg;
1056 /* Likewise, should we set special REG_NOTEs here? */
1059 else if (GET_CODE (orig) == CONST)
1061 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1064 else if (GET_CODE (orig) == MEM
1065 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
1067 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1068 addr = replace_equiv_address (orig, addr);
1069 emit_move_insn (reg, addr);
1070 pic_ref = reg;
1073 return pic_ref;
1076 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
1077 DATA is the FILE* for assembly output. Called from
1078 htab_traverse. */
1081 machopic_output_indirection (machopic_indirection **slot, FILE *asm_out_file)
1083 machopic_indirection *p = *slot;
1084 rtx symbol;
1085 const char *sym_name;
1086 const char *ptr_name;
1088 if (!p->used)
1089 return 1;
1091 symbol = p->symbol;
1092 sym_name = XSTR (symbol, 0);
1093 ptr_name = p->ptr_name;
1095 if (p->stub_p)
1097 char *sym;
1098 char *stub;
1099 tree id;
1101 id = maybe_get_identifier (sym_name);
1102 if (id)
1104 tree id_orig = id;
1106 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1107 id = TREE_CHAIN (id);
1108 if (id != id_orig)
1109 sym_name = IDENTIFIER_POINTER (id);
1112 sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1113 if (sym_name[0] == '*' || sym_name[0] == '&')
1114 strcpy (sym, sym_name + 1);
1115 else if (sym_name[0] == '-' || sym_name[0] == '+')
1116 strcpy (sym, sym_name);
1117 else
1118 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1120 stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1121 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1122 strcpy (stub, ptr_name + 1);
1123 else
1124 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1126 machopic_output_stub (asm_out_file, sym, stub);
1128 else if (! indirect_data (symbol)
1129 && (machopic_symbol_defined_p (symbol)
1130 || SYMBOL_REF_LOCAL_P (symbol)))
1132 switch_to_section (data_section);
1133 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1134 assemble_label (asm_out_file, ptr_name);
1135 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1136 GET_MODE_SIZE (Pmode),
1137 GET_MODE_ALIGNMENT (Pmode), 1);
1139 else
1141 rtx init = const0_rtx;
1143 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1145 /* Mach-O symbols are passed around in code through indirect
1146 references and the original symbol_ref hasn't passed through
1147 the generic handling and reference-catching in
1148 output_operand, so we need to manually mark weak references
1149 as such. */
1150 if (SYMBOL_REF_WEAK (symbol))
1152 tree decl = SYMBOL_REF_DECL (symbol);
1153 gcc_assert (DECL_P (decl));
1155 if (decl != NULL_TREE
1156 && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
1157 /* Handle only actual external-only definitions, not
1158 e.g. extern inline code or variables for which
1159 storage has been allocated. */
1160 && !TREE_STATIC (decl))
1162 fputs ("\t.weak_reference ", asm_out_file);
1163 assemble_name (asm_out_file, sym_name);
1164 fputc ('\n', asm_out_file);
1168 assemble_name (asm_out_file, ptr_name);
1169 fprintf (asm_out_file, ":\n");
1171 fprintf (asm_out_file, "\t.indirect_symbol ");
1172 assemble_name (asm_out_file, sym_name);
1173 fprintf (asm_out_file, "\n");
1175 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1176 have their symbol name instead of 0 in the second entry of
1177 the non-lazy symbol pointer data structure when they are
1178 defined. This allows the runtime to rebind newer instances
1179 of the translation unit with the original instance of the
1180 symbol. */
1182 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1183 && machopic_symbol_defined_p (symbol))
1184 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1186 assemble_integer (init, GET_MODE_SIZE (Pmode),
1187 GET_MODE_ALIGNMENT (Pmode), 1);
1190 return 1;
1193 void
1194 machopic_finish (FILE *asm_out_file)
1196 if (machopic_indirections)
1197 machopic_indirections
1198 ->traverse_noresize<FILE *, machopic_output_indirection> (asm_out_file);
1202 machopic_operand_p (rtx op)
1204 if (MACHOPIC_JUST_INDIRECT)
1205 return (GET_CODE (op) == SYMBOL_REF
1206 && machopic_symbol_defined_p (op));
1207 else
1208 return (GET_CODE (op) == CONST
1209 && GET_CODE (XEXP (op, 0)) == UNSPEC
1210 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1213 /* This function records whether a given name corresponds to a defined
1214 or undefined function or variable, for machopic_classify_ident to
1215 use later. */
1217 void
1218 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1220 rtx sym_ref;
1222 /* Do the standard encoding things first. */
1223 default_encode_section_info (decl, rtl, first);
1225 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1226 return;
1228 sym_ref = XEXP (rtl, 0);
1229 if (TREE_CODE (decl) == VAR_DECL)
1230 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1232 if (!DECL_EXTERNAL (decl)
1233 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1234 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1235 && ((TREE_STATIC (decl)
1236 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1237 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1238 && DECL_INITIAL (decl) != error_mark_node)))
1239 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1241 if (! TREE_PUBLIC (decl))
1242 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1245 void
1246 darwin_mark_decl_preserved (const char *name)
1248 /* Actually we shouldn't mark any local symbol this way, but for now
1249 this only happens with ObjC meta-data. */
1250 if (darwin_label_is_anonymous_local_objc_name (name))
1251 return;
1253 fprintf (asm_out_file, "\t.no_dead_strip ");
1254 assemble_name (asm_out_file, name);
1255 fputc ('\n', asm_out_file);
1258 static section *
1259 darwin_rodata_section (int use_coal, bool zsize)
1261 return (use_coal
1262 ? darwin_sections[const_coal_section]
1263 : (zsize ? darwin_sections[zobj_const_section]
1264 : darwin_sections[const_section]));
1267 static section *
1268 darwin_mergeable_string_section (tree exp,
1269 unsigned HOST_WIDE_INT align)
1271 /* Darwin's ld expects to see non-writable string literals in the .cstring
1272 section. Later versions of ld check and complain when CFStrings are
1273 enabled. Therefore we shall force the strings into .cstring since we
1274 don't support writable ones anyway. */
1275 if ((darwin_constant_cfstrings || flag_merge_constants)
1276 && TREE_CODE (exp) == STRING_CST
1277 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1278 && align <= 256
1279 && (int_size_in_bytes (TREE_TYPE (exp))
1280 == TREE_STRING_LENGTH (exp))
1281 && ((size_t) TREE_STRING_LENGTH (exp)
1282 == strlen (TREE_STRING_POINTER (exp)) + 1))
1283 return darwin_sections[cstring_section];
1285 if (DARWIN_SECTION_ANCHORS && flag_section_anchors
1286 && TREE_CODE (exp) == STRING_CST
1287 && TREE_STRING_LENGTH (exp) == 0)
1288 return darwin_sections[zobj_const_section];
1290 return readonly_data_section;
1293 #ifndef HAVE_GAS_LITERAL16
1294 #define HAVE_GAS_LITERAL16 0
1295 #endif
1297 static section *
1298 darwin_mergeable_constant_section (tree exp,
1299 unsigned HOST_WIDE_INT align,
1300 bool zsize)
1302 machine_mode mode = DECL_MODE (exp);
1303 unsigned int modesize = GET_MODE_BITSIZE (mode);
1305 if (DARWIN_SECTION_ANCHORS
1306 && flag_section_anchors
1307 && zsize)
1308 return darwin_sections[zobj_const_section];
1310 if (flag_merge_constants
1311 && mode != VOIDmode
1312 && mode != BLKmode
1313 && modesize <= align
1314 && align >= 8
1315 && align <= 256
1316 && (align & (align -1)) == 0)
1318 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1320 if (TREE_CODE (size) == INTEGER_CST)
1322 if (wi::eq_p (size, 4))
1323 return darwin_sections[literal4_section];
1324 else if (wi::eq_p (size, 8))
1325 return darwin_sections[literal8_section];
1326 else if (HAVE_GAS_LITERAL16
1327 && TARGET_64BIT
1328 && wi::eq_p (size, 16))
1329 return darwin_sections[literal16_section];
1333 return readonly_data_section;
1336 section *
1337 darwin_tm_clone_table_section (void)
1339 return get_named_section (NULL,
1340 "__DATA,__tm_clone_table,regular,no_dead_strip",
1345 machopic_reloc_rw_mask (void)
1347 return MACHOPIC_INDIRECT ? 3 : 0;
1350 /* We have to deal with ObjC/C++ metadata section placement in the common
1351 code, since it will also be called from LTO.
1353 Return metadata attributes, if present (searching for ABI=2 first)
1354 Return NULL_TREE if no such attributes are found. */
1356 static tree
1357 is_objc_metadata (tree decl)
1359 if (DECL_P (decl)
1360 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1361 && DECL_ATTRIBUTES (decl))
1363 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1364 if (meta)
1365 return meta;
1366 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1367 if (meta)
1368 return meta;
1370 return NULL_TREE;
1373 static int classes_seen;
1374 static int objc_metadata_seen;
1376 /* Return the section required for Objective C ABI 2 metadata. */
1377 static section *
1378 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1380 const char *p;
1381 tree ident = TREE_VALUE (meta);
1382 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1383 p = IDENTIFIER_POINTER (ident);
1385 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi == 2);
1387 objc_metadata_seen = 1;
1389 if (base == data_section)
1390 base = darwin_sections[objc2_metadata_section];
1392 /* Most of the OBJC2 META-data end up in the base section, so check it
1393 first. */
1394 if (!strncmp (p, "V2_BASE", 7))
1395 return base;
1396 else if (!strncmp (p, "V2_STRG", 7))
1397 return darwin_sections[cstring_section];
1399 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1400 return darwin_sections[objc2_classdefs_section];
1401 else if (!strncmp (p, "V2_MREF", 7))
1402 return darwin_sections[objc2_message_refs_section];
1403 else if (!strncmp (p, "V2_CLRF", 7))
1404 return darwin_sections[objc2_classrefs_section];
1405 else if (!strncmp (p, "V2_SURF", 7))
1406 return darwin_sections[objc2_super_classrefs_section];
1407 else if (!strncmp (p, "V2_NLCL", 7))
1408 return darwin_sections[objc2_nonlazy_class_section];
1409 else if (!strncmp (p, "V2_CLAB", 7))
1411 classes_seen = 1;
1412 return darwin_sections[objc2_classlist_section];
1414 else if (!strncmp (p, "V2_SRFS", 7))
1415 return darwin_sections[objc2_selector_refs_section];
1416 else if (!strncmp (p, "V2_NLCA", 7))
1417 return darwin_sections[objc2_nonlazy_category_section];
1418 else if (!strncmp (p, "V2_CALA", 7))
1419 return darwin_sections[objc2_categorylist_section];
1421 else if (!strncmp (p, "V2_PLST", 7))
1422 return darwin_sections[objc2_protocollist_section];
1423 else if (!strncmp (p, "V2_PRFS", 7))
1424 return darwin_sections[objc2_protocolrefs_section];
1426 else if (!strncmp (p, "V2_INFO", 7))
1427 return darwin_sections[objc2_image_info_section];
1429 else if (!strncmp (p, "V2_EHTY", 7))
1430 return ld_uses_coal_sects ? darwin_sections[data_coal_section]
1431 : data_section;
1433 else if (!strncmp (p, "V2_CSTR", 7))
1434 return darwin_sections[objc2_constant_string_object_section];
1436 /* Not recognized, default. */
1437 return base;
1440 /* Return the section required for Objective C ABI 0/1 metadata. */
1441 static section *
1442 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1444 const char *p;
1445 tree ident = TREE_VALUE (meta);
1446 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1447 p = IDENTIFIER_POINTER (ident);
1449 gcc_checking_assert (flag_next_runtime == 1 && flag_objc_abi < 2);
1451 objc_metadata_seen = 1;
1453 /* String sections first, cos there are lots of strings. */
1454 if (!strncmp (p, "V1_STRG", 7))
1455 return darwin_sections[cstring_section];
1456 else if (!strncmp (p, "V1_CLSN", 7))
1457 return darwin_sections[objc_class_names_section];
1458 else if (!strncmp (p, "V1_METN", 7))
1459 return darwin_sections[objc_meth_var_names_section];
1460 else if (!strncmp (p, "V1_METT", 7))
1461 return darwin_sections[objc_meth_var_types_section];
1463 else if (!strncmp (p, "V1_CLAS", 7))
1465 classes_seen = 1;
1466 return darwin_sections[objc_class_section];
1468 else if (!strncmp (p, "V1_META", 7))
1469 return darwin_sections[objc_meta_class_section];
1470 else if (!strncmp (p, "V1_CATG", 7))
1471 return darwin_sections[objc_category_section];
1472 else if (!strncmp (p, "V1_PROT", 7))
1473 return darwin_sections[objc_protocol_section];
1475 else if (!strncmp (p, "V1_CLCV", 7))
1476 return darwin_sections[objc_class_vars_section];
1477 else if (!strncmp (p, "V1_CLIV", 7))
1478 return darwin_sections[objc_instance_vars_section];
1480 else if (!strncmp (p, "V1_CLCM", 7))
1481 return darwin_sections[objc_cls_meth_section];
1482 else if (!strncmp (p, "V1_CLIM", 7))
1483 return darwin_sections[objc_inst_meth_section];
1484 else if (!strncmp (p, "V1_CACM", 7))
1485 return darwin_sections[objc_cat_cls_meth_section];
1486 else if (!strncmp (p, "V1_CAIM", 7))
1487 return darwin_sections[objc_cat_inst_meth_section];
1488 else if (!strncmp (p, "V1_PNSM", 7))
1489 return darwin_sections[objc_cat_inst_meth_section];
1490 else if (!strncmp (p, "V1_PCLM", 7))
1491 return darwin_sections[objc_cat_cls_meth_section];
1493 else if (!strncmp (p, "V1_CLPR", 7))
1494 return darwin_sections[objc_cat_cls_meth_section];
1495 else if (!strncmp (p, "V1_CAPR", 7))
1496 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1498 else if (!strncmp (p, "V1_PRFS", 7))
1499 return darwin_sections[objc_cat_cls_meth_section];
1500 else if (!strncmp (p, "V1_CLRF", 7))
1501 return darwin_sections[objc_cls_refs_section];
1502 else if (!strncmp (p, "V1_SRFS", 7))
1503 return darwin_sections[objc_selector_refs_section];
1505 else if (!strncmp (p, "V1_MODU", 7))
1506 return darwin_sections[objc_module_info_section];
1507 else if (!strncmp (p, "V1_SYMT", 7))
1508 return darwin_sections[objc_symbols_section];
1509 else if (!strncmp (p, "V1_INFO", 7))
1510 return darwin_sections[objc_image_info_section];
1512 else if (!strncmp (p, "V1_PLST", 7))
1513 return darwin_sections[objc1_prop_list_section];
1514 else if (!strncmp (p, "V1_PEXT", 7))
1515 return darwin_sections[objc1_protocol_ext_section];
1516 else if (!strncmp (p, "V1_CEXT", 7))
1517 return darwin_sections[objc1_class_ext_section];
1519 else if (!strncmp (p, "V2_CSTR", 7))
1520 return darwin_sections[objc_constant_string_object_section];
1522 return base;
1525 section *
1526 machopic_select_section (tree decl,
1527 int reloc,
1528 unsigned HOST_WIDE_INT align)
1530 bool zsize, one, weak, use_coal, ro;
1531 section *base_section = NULL;
1533 weak = (DECL_P (decl)
1534 && DECL_WEAK (decl)
1535 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1537 zsize = (DECL_P (decl)
1538 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1539 && tree_to_uhwi (DECL_SIZE_UNIT (decl)) == 0);
1541 one = DECL_P (decl)
1542 && TREE_CODE (decl) == VAR_DECL
1543 && DECL_COMDAT_GROUP (decl);
1545 use_coal = (weak || one) && ld_uses_coal_sects;
1547 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1549 switch (categorize_decl_for_section (decl, reloc))
1551 case SECCAT_TEXT:
1552 gcc_unreachable ();
1553 break;
1555 case SECCAT_RODATA:
1556 case SECCAT_SRODATA:
1557 base_section = darwin_rodata_section (use_coal, zsize);
1558 break;
1560 case SECCAT_RODATA_MERGE_STR:
1561 base_section = darwin_mergeable_string_section (decl, align);
1562 break;
1564 case SECCAT_RODATA_MERGE_STR_INIT:
1565 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1566 break;
1568 case SECCAT_RODATA_MERGE_CONST:
1569 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1570 break;
1572 case SECCAT_DATA:
1573 case SECCAT_DATA_REL:
1574 case SECCAT_DATA_REL_LOCAL:
1575 case SECCAT_DATA_REL_RO:
1576 case SECCAT_DATA_REL_RO_LOCAL:
1577 case SECCAT_SDATA:
1578 case SECCAT_TDATA:
1579 if (use_coal)
1581 if (ro)
1582 base_section = darwin_sections[const_data_coal_section];
1583 else
1584 base_section = darwin_sections[data_coal_section];
1586 else if (DARWIN_SECTION_ANCHORS
1587 && flag_section_anchors
1588 && zsize)
1590 /* If we're doing section anchors, then punt zero-sized objects into
1591 their own sections so that they don't interfere with offset
1592 computation for the remaining vars. This does not need to be done
1593 for stuff in mergeable sections, since these are ineligible for
1594 anchors. */
1595 if (ro)
1596 base_section = darwin_sections[zobj_const_data_section];
1597 else
1598 base_section = darwin_sections[zobj_data_section];
1600 else if (ro)
1601 base_section = darwin_sections[const_data_section];
1602 else
1603 base_section = data_section;
1604 break;
1605 case SECCAT_BSS:
1606 case SECCAT_SBSS:
1607 case SECCAT_TBSS:
1608 if (use_coal)
1609 base_section = darwin_sections[data_coal_section];
1610 else
1612 if (!TREE_PUBLIC (decl))
1613 base_section = lcomm_section;
1614 else if (bss_noswitch_section)
1615 base_section = bss_noswitch_section;
1616 else
1617 base_section = data_section;
1619 break;
1621 default:
1622 gcc_unreachable ();
1625 /* Darwin weird special cases.
1626 a) OBJC Meta-data. */
1627 if (DECL_P (decl)
1628 && (TREE_CODE (decl) == VAR_DECL
1629 || TREE_CODE (decl) == CONST_DECL)
1630 && DECL_ATTRIBUTES (decl))
1632 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1633 if (meta)
1634 return darwin_objc2_section (decl, meta, base_section);
1635 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1636 if (meta)
1637 return darwin_objc1_section (decl, meta, base_section);
1638 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1639 if (meta)
1640 return base_section; /* GNU runtime is happy with it all in one pot. */
1643 /* b) Constant string objects. */
1644 if (TREE_CODE (decl) == CONSTRUCTOR
1645 && TREE_TYPE (decl)
1646 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1647 && TYPE_NAME (TREE_TYPE (decl)))
1649 tree name = TYPE_NAME (TREE_TYPE (decl));
1650 if (TREE_CODE (name) == TYPE_DECL)
1651 name = DECL_NAME (name);
1653 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1655 if (flag_next_runtime)
1657 if (flag_objc_abi == 2)
1658 return darwin_sections[objc2_constant_string_object_section];
1659 else
1660 return darwin_sections[objc_constant_string_object_section];
1662 else
1663 return darwin_sections[objc_string_object_section];
1665 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1666 return darwin_sections[cfstring_constant_object_section];
1667 else
1668 return base_section;
1670 /* c) legacy meta-data selection. */
1671 else if (TREE_CODE (decl) == VAR_DECL
1672 && DECL_NAME (decl)
1673 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1674 && IDENTIFIER_POINTER (DECL_NAME (decl))
1675 && flag_next_runtime
1676 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1678 const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1679 static bool warned_objc_46 = false;
1680 /* We shall assert that zero-sized objects are an error in ObjC
1681 meta-data. */
1682 gcc_assert (tree_to_uhwi (DECL_SIZE_UNIT (decl)) != 0);
1684 /* ??? This mechanism for determining the metadata section is
1685 broken when LTO is in use, since the frontend that generated
1686 the data is not identified. We will keep the capability for
1687 the short term - in case any non-Objective-C programs are using
1688 it to place data in specified sections. */
1689 if (!warned_objc_46)
1691 location_t loc = DECL_SOURCE_LOCATION (decl);
1692 warning_at (loc, 0, "the use of _OBJC_-prefixed variable names"
1693 " to select meta-data sections is deprecated at 4.6"
1694 " and will be removed in 4.7");
1695 warned_objc_46 = true;
1698 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1699 return darwin_sections[objc_cls_meth_section];
1700 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1701 return darwin_sections[objc_inst_meth_section];
1702 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 29))
1703 return darwin_sections[objc_cat_cls_meth_section];
1704 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 32))
1705 return darwin_sections[objc_cat_inst_meth_section];
1706 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1707 return darwin_sections[objc_class_vars_section];
1708 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1709 return darwin_sections[objc_instance_vars_section];
1710 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1711 return darwin_sections[objc_cat_cls_meth_section];
1712 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1713 return darwin_sections[objc_class_names_section];
1714 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1715 return darwin_sections[objc_meth_var_names_section];
1716 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1717 return darwin_sections[objc_meth_var_types_section];
1718 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1719 return darwin_sections[objc_cls_refs_section];
1720 else if (!strncmp (name, "_OBJC_CLASS_", 12))
1721 return darwin_sections[objc_class_section];
1722 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1723 return darwin_sections[objc_meta_class_section];
1724 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1725 return darwin_sections[objc_category_section];
1726 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1727 return darwin_sections[objc_selector_refs_section];
1728 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1729 return darwin_sections[objc_selector_fixup_section];
1730 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1731 return darwin_sections[objc_symbols_section];
1732 else if (!strncmp (name, "_OBJC_MODULES", 13))
1733 return darwin_sections[objc_module_info_section];
1734 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1735 return darwin_sections[objc_image_info_section];
1736 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1737 return darwin_sections[objc_cat_inst_meth_section];
1738 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1739 return darwin_sections[objc_cat_cls_meth_section];
1740 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1741 return darwin_sections[objc_cat_cls_meth_section];
1742 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1743 return darwin_sections[objc_protocol_section];
1744 else
1745 return base_section;
1748 return base_section;
1751 /* This can be called with address expressions as "rtx".
1752 They must go in "const". */
1754 section *
1755 machopic_select_rtx_section (machine_mode mode, rtx x,
1756 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1758 if (GET_MODE_SIZE (mode) == 8
1759 && (GET_CODE (x) == CONST_INT
1760 || GET_CODE (x) == CONST_WIDE_INT
1761 || GET_CODE (x) == CONST_DOUBLE))
1762 return darwin_sections[literal8_section];
1763 else if (GET_MODE_SIZE (mode) == 4
1764 && (GET_CODE (x) == CONST_INT
1765 || GET_CODE (x) == CONST_WIDE_INT
1766 || GET_CODE (x) == CONST_DOUBLE))
1767 return darwin_sections[literal4_section];
1768 else if (HAVE_GAS_LITERAL16
1769 && TARGET_64BIT
1770 && GET_MODE_SIZE (mode) == 16
1771 && (GET_CODE (x) == CONST_INT
1772 || GET_CODE (x) == CONST_WIDE_INT
1773 || GET_CODE (x) == CONST_DOUBLE
1774 || GET_CODE (x) == CONST_VECTOR))
1775 return darwin_sections[literal16_section];
1776 else if (MACHOPIC_INDIRECT
1777 && (GET_CODE (x) == SYMBOL_REF
1778 || GET_CODE (x) == CONST
1779 || GET_CODE (x) == LABEL_REF))
1780 return darwin_sections[const_data_section];
1781 else
1782 return darwin_sections[const_section];
1785 void
1786 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1788 cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
1790 vec_safe_push (ctors, new_elt);
1792 if (! MACHOPIC_INDIRECT)
1793 fprintf (asm_out_file, ".reference .constructors_used\n");
1796 void
1797 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1799 cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)};
1801 vec_safe_push (dtors, new_elt);
1803 if (! MACHOPIC_INDIRECT)
1804 fprintf (asm_out_file, ".reference .destructors_used\n");
1807 static int
1808 sort_cdtor_records (const void * a, const void * b)
1810 const cdtor_record *cda = (const cdtor_record *)a;
1811 const cdtor_record *cdb = (const cdtor_record *)b;
1812 if (cda->priority > cdb->priority)
1813 return 1;
1814 if (cda->priority < cdb->priority)
1815 return -1;
1816 if (cda->position > cdb->position)
1817 return 1;
1818 if (cda->position < cdb->position)
1819 return -1;
1820 return 0;
1823 static void
1824 finalize_ctors ()
1826 unsigned int i;
1827 cdtor_record *elt;
1829 if (MACHOPIC_INDIRECT)
1830 switch_to_section (darwin_sections[mod_init_section]);
1831 else
1832 switch_to_section (darwin_sections[constructor_section]);
1834 if (vec_safe_length (ctors) > 1)
1835 ctors->qsort (sort_cdtor_records);
1836 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
1838 assemble_align (POINTER_SIZE);
1839 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1843 static void
1844 finalize_dtors ()
1846 unsigned int i;
1847 cdtor_record *elt;
1849 if (MACHOPIC_INDIRECT)
1850 switch_to_section (darwin_sections[mod_term_section]);
1851 else
1852 switch_to_section (darwin_sections[destructor_section]);
1854 if (vec_safe_length (dtors) > 1)
1855 dtors->qsort (sort_cdtor_records);
1856 FOR_EACH_VEC_SAFE_ELT (dtors, i, elt)
1858 assemble_align (POINTER_SIZE);
1859 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1863 void
1864 darwin_globalize_label (FILE *stream, const char *name)
1866 if (!!strncmp (name, "_OBJC_", 6))
1867 default_globalize_label (stream, name);
1870 /* This routine returns non-zero if 'name' starts with the special objective-c
1871 anonymous file-scope static name. It accommodates c++'s mangling of such
1872 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1874 int
1875 darwin_label_is_anonymous_local_objc_name (const char *name)
1877 const unsigned char *p = (const unsigned char *) name;
1878 if (*p != '_')
1879 return 0;
1880 if (p[1] == 'Z' && p[2] == 'L')
1882 p += 3;
1883 while (*p >= '0' && *p <= '9')
1884 p++;
1886 return (!strncmp ((const char *)p, "_OBJC_", 6));
1889 /* LTO support for Mach-O.
1891 This version uses three mach-o sections to encapsulate the (unlimited
1892 number of) lto sections.
1894 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1895 __GNU_LTO, __section_names contains the GNU LTO section names.
1896 __GNU_LTO, __section_index contains an array of values that index these.
1898 Indexed thus:
1899 <section offset from the start of __GNU_LTO, __lto_sections>,
1900 <section length>
1901 <name offset from the start of __GNU_LTO, __section_names,
1902 <name length>.
1904 At present, for both m32 and m64 mach-o files each of these fields is
1905 represented by a uint32_t. This is because, AFAICT, a mach-o object
1906 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1908 uint32_t offset;
1909 "offset An integer specifying the offset to this section in the file." */
1911 /* Count lto section numbers. */
1912 static unsigned int lto_section_num = 0;
1914 /* A vector of information about LTO sections, at present, we only have
1915 the name. TODO: see if we can get the data length somehow. */
1916 typedef struct GTY (()) darwin_lto_section_e {
1917 const char *sectname;
1918 } darwin_lto_section_e ;
1920 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1922 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1923 sections into three Mach-O ones).
1924 NOTE: These names MUST be kept in sync with those in
1925 libiberty/simple-object-mach-o. */
1926 #define LTO_SECTS_SECTION "__wrapper_sects"
1927 #define LTO_NAMES_SECTION "__wrapper_names"
1928 #define LTO_INDEX_SECTION "__wrapper_index"
1930 /* File to temporarily store LTO data. This is appended to asm_out_file
1931 in darwin_end_file. */
1932 static FILE *lto_asm_out_file, *saved_asm_out_file;
1933 static char *lto_asm_out_name;
1935 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1936 asm_out_file and switching to an alternative output file. */
1937 void
1938 darwin_asm_lto_start (void)
1940 gcc_assert (! saved_asm_out_file);
1941 saved_asm_out_file = asm_out_file;
1942 if (! lto_asm_out_name)
1943 lto_asm_out_name = make_temp_file (".lto.s");
1944 lto_asm_out_file = fopen (lto_asm_out_name, "a");
1945 if (lto_asm_out_file == NULL)
1946 fatal_error (input_location,
1947 "failed to open temporary file %s for LTO output",
1948 lto_asm_out_name);
1949 asm_out_file = lto_asm_out_file;
1952 /* Restore asm_out_file. */
1953 void
1954 darwin_asm_lto_end (void)
1956 gcc_assert (saved_asm_out_file);
1957 fclose (lto_asm_out_file);
1958 asm_out_file = saved_asm_out_file;
1959 saved_asm_out_file = NULL;
1962 static void
1963 darwin_asm_dwarf_section (const char *name, unsigned int flags,
1964 tree decl, bool is_for_lto);
1966 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
1968 void
1969 darwin_asm_named_section (const char *name,
1970 unsigned int flags,
1971 tree decl ATTRIBUTE_UNUSED)
1973 /* LTO sections go in a special section that encapsulates the (unlimited)
1974 number of GNU LTO sections within a single mach-o one. */
1975 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1976 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1978 darwin_lto_section_e e;
1979 /* We expect certain flags to be set... */
1980 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1981 == (SECTION_DEBUG | SECTION_NAMED));
1983 /* Switch to our combined section. */
1984 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
1985 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
1986 /* Output a label for the start of this sub-section. */
1987 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
1988 lto_section_num, name);
1989 /* We have to jump through hoops to get the values of the intra-section
1990 offsets... */
1991 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
1992 lto_section_num, lto_section_num);
1993 fprintf (asm_out_file,
1994 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
1995 lto_section_num, lto_section_num+1, lto_section_num);
1996 lto_section_num++;
1997 e.sectname = xstrdup (name);
1998 /* Keep the names, we'll need to make a table later.
1999 TODO: check that we do not revisit sections, that would break
2000 the assumption of how this is done. */
2001 if (lto_section_names == NULL)
2002 vec_alloc (lto_section_names, 16);
2003 vec_safe_push (lto_section_names, e);
2005 else if (strncmp (name, "__DWARF,", 8) == 0)
2006 darwin_asm_dwarf_section (name, flags, decl, false);
2007 else if (strncmp (name, "__GNU_DWARF_LTO,", 16) == 0)
2008 darwin_asm_dwarf_section (name, flags, decl, true);
2009 else
2010 fprintf (asm_out_file, "\t.section %s\n", name);
2013 void
2014 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
2016 /* Darwin does not use unique sections. */
2019 /* Handle __attribute__ ((apple_kext_compatibility)).
2020 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
2021 vtable for classes with this attribute (and their descendants) by not
2022 outputting the new 3.0 nondeleting destructor. This means that such
2023 objects CANNOT be allocated on the stack or as globals UNLESS they have
2024 a completely empty `operator delete'.
2025 Luckily, this fits in with the Darwin kext model.
2027 This attribute also disables gcc3's potential overlaying of derived
2028 class data members on the padding at the end of the base class. */
2030 tree
2031 darwin_handle_kext_attribute (tree *node, tree name,
2032 tree args ATTRIBUTE_UNUSED,
2033 int flags ATTRIBUTE_UNUSED,
2034 bool *no_add_attrs)
2036 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
2037 if (! TARGET_KEXTABI)
2039 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2040 "only when compiling a kext", name);
2042 *no_add_attrs = true;
2044 else if (TREE_CODE (*node) != RECORD_TYPE)
2046 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2047 "only to C++ classes", name);
2049 *no_add_attrs = true;
2052 return NULL_TREE;
2055 /* Handle a "weak_import" attribute; arguments as in
2056 struct attribute_spec.handler. */
2058 tree
2059 darwin_handle_weak_import_attribute (tree *node, tree name,
2060 tree ARG_UNUSED (args),
2061 int ARG_UNUSED (flags),
2062 bool * no_add_attrs)
2064 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
2066 warning (OPT_Wattributes, "%qE attribute ignored",
2067 name);
2068 *no_add_attrs = true;
2070 else
2071 declare_weak (*node);
2073 return NULL_TREE;
2076 /* Emit a label for an FDE, making it global and/or weak if appropriate.
2077 The third parameter is nonzero if this is for exception handling.
2078 The fourth parameter is nonzero if this is just a placeholder for an
2079 FDE that we are omitting. */
2081 void
2082 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
2084 char *lab ;
2085 char buf[32];
2086 static int invok_count = 0;
2087 static tree last_fun_decl = NULL_TREE;
2089 /* We use the linker to emit the .eh labels for Darwin 9 and above. */
2090 if (! for_eh || generating_for_darwin_version >= 9)
2091 return;
2093 /* FIXME: This only works when the eh for all sections of a function is
2094 emitted at the same time. If that changes, we would need to use a lookup
2095 table of some form to determine what to do. Also, we should emit the
2096 unadorned label for the partition containing the public label for a
2097 function. This is of limited use, probably, since we do not currently
2098 enable partitioning. */
2099 strcpy (buf, ".eh");
2100 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
2102 if (decl == last_fun_decl)
2104 invok_count++;
2105 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
2107 else
2109 last_fun_decl = decl;
2110 invok_count = 0;
2114 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
2116 if (TREE_PUBLIC (decl))
2118 targetm.asm_out.globalize_label (file, lab);
2119 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
2121 fputs ("\t.private_extern ", file);
2122 assemble_name (file, lab);
2123 fputc ('\n', file);
2127 if (DECL_WEAK (decl))
2129 fputs ("\t.weak_definition ", file);
2130 assemble_name (file, lab);
2131 fputc ('\n', file);
2134 assemble_name (file, lab);
2135 if (empty)
2137 fputs (" = 0\n", file);
2139 /* Mark the absolute .eh and .eh1 style labels as needed to
2140 ensure that we don't dead code strip them and keep such
2141 labels from another instantiation point until we can fix this
2142 properly with group comdat support. */
2143 darwin_mark_decl_preserved (lab);
2145 else
2146 fputs (":\n", file);
2148 free (lab);
2151 static GTY(()) unsigned long except_table_label_num;
2153 void
2154 darwin_emit_except_table_label (FILE *file)
2156 char section_start_label[30];
2158 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2159 except_table_label_num++);
2160 ASM_OUTPUT_LABEL (file, section_start_label);
2162 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2164 void
2165 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2167 const char *nlp_name;
2169 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2171 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2172 fputs ("\t.long\t", file);
2173 ASM_OUTPUT_LABELREF (file, nlp_name);
2174 fputs ("-.", file);
2177 /* If this is uncommented, details of each allocation will be printed
2178 in the asm right before the actual code. WARNING - this will cause some
2179 test-suite fails (since the printout will contain items that some tests
2180 are not expecting) -- so don't leave it on by default (it bloats the
2181 asm too). */
2182 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2184 /* The first two of these routines are ostensibly just intended to put
2185 names into the asm. However, they are both hijacked in order to ensure
2186 that zero-sized items do not make their way into the output. Consequently,
2187 we also need to make these participate in provisions for dealing with
2188 such items in section anchors. */
2190 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2191 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2192 but it does need to be referenced via indirect PIC data pointers.
2193 The machopic_define_symbol calls are telling the machopic subsystem
2194 that the name *is* defined in this module, so it doesn't need to
2195 make them indirect. */
2196 void
2197 darwin_asm_declare_object_name (FILE *file,
2198 const char *nam, tree decl)
2200 const char *xname = nam;
2201 unsigned HOST_WIDE_INT size;
2202 bool local_def, weak;
2204 weak = (DECL_P (decl)
2205 && DECL_WEAK (decl)
2206 && !lookup_attribute ("weak_import",
2207 DECL_ATTRIBUTES (decl)));
2209 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2210 && (!DECL_COMMON (decl)
2211 || !TREE_PUBLIC (decl)));
2213 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2214 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2216 if (local_def)
2218 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2219 if (!weak)
2220 machopic_define_symbol (DECL_RTL (decl));
2223 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2225 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2226 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2227 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2228 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2229 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2230 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2231 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2232 (unsigned long)DECL_INITIAL (decl));
2233 #endif
2235 /* Darwin needs help to support local zero-sized objects.
2236 They must be made at least one byte, and the section containing must be
2237 marked as unsuitable for section-anchors (see storage allocators below).
2239 For non-zero objects this output is handled by varasm.c.
2241 if (!size)
2243 unsigned int l2align = 0;
2245 /* The align must be honored, even for zero-sized. */
2246 if (DECL_ALIGN (decl))
2248 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2249 fprintf (file, "\t.align\t%u\n", l2align);
2252 ASM_OUTPUT_LABEL (file, xname);
2253 size = 1;
2254 fprintf (file, "\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2256 /* Check that we've correctly picked up the zero-sized item and placed it
2257 properly. */
2258 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2259 || (in_section
2260 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2262 else
2263 ASM_OUTPUT_LABEL (file, xname);
2266 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2267 void
2268 darwin_asm_declare_constant_name (FILE *file, const char *name,
2269 const_tree exp ATTRIBUTE_UNUSED,
2270 HOST_WIDE_INT size)
2272 assemble_label (file, name);
2273 /* As for other items, we need at least one byte. */
2274 if (!size)
2276 fputs ("\t.space\t1\n", file);
2277 /* Check that we've correctly picked up the zero-sized item and placed it
2278 properly. */
2279 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2280 || (in_section
2281 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2285 /* Darwin storage allocators.
2287 Zerofill sections are desirable for large blank data since, otherwise, these
2288 data bloat objects (PR33210).
2290 However, section anchors don't work in .zerofill sections (one cannot switch
2291 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2292 to put (at least some) data into 'normal' switchable sections.
2294 Here we set a relatively arbitrary value for the size of an object to trigger
2295 zerofill when section anchors are enabled (anything bigger than a page for
2296 current Darwin implementations). FIXME: there ought to be some objective way
2297 to make this choice.
2299 When section anchor are off this is ignored anyway. */
2301 #define BYTES_ZFILL 4096
2303 /* Emit a chunk of data for items coalesced by the linker. */
2304 static void
2305 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2306 unsigned HOST_WIDE_INT size,
2307 bool use_coal,
2308 unsigned int align)
2310 /* Since the sections used here are coalesced, they will not be eligible
2311 for section anchors, and therefore we don't need to break that out.
2312 CHECKME: for modern linker on PowerPC. */
2313 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2314 switch_to_section (use_coal ? darwin_sections[const_data_coal_section]
2315 : darwin_sections[const_data_section]);
2316 else
2317 switch_to_section (use_coal ? darwin_sections[data_coal_section]
2318 : data_section);
2320 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2321 the align info for zero-sized items... but do it here otherwise. */
2322 if (size && align)
2323 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2325 if (TREE_PUBLIC (decl))
2326 darwin_globalize_label (fp, name);
2328 /* ... and we let it deal with outputting one byte of zero for them too. */
2329 darwin_asm_declare_object_name (fp, name, decl);
2330 if (size)
2331 assemble_zeros (size);
2334 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2335 static void
2336 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2337 unsigned HOST_WIDE_INT size,
2338 unsigned int align, tree meta)
2340 section *ocs = data_section;
2342 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2343 ocs = darwin_objc2_section (decl, meta, ocs);
2344 else
2345 ocs = darwin_objc1_section (decl, meta, ocs);
2347 switch_to_section (ocs);
2349 /* We shall declare that zero-sized meta-data are not valid (yet). */
2350 gcc_assert (size);
2351 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2353 /* ... and we let it deal with outputting one byte of zero for them too. */
2354 darwin_asm_declare_object_name (fp, name, decl);
2355 assemble_zeros (size);
2358 /* This routine emits 'local' storage:
2360 When Section Anchors are off this routine emits .zerofill commands in
2361 sections named for their alignment.
2363 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2364 the .static_data section so that the section anchoring system can see them.
2365 Larger items are still placed in .zerofill sections, addressing PR33210.
2366 The routine has no checking - it is all assumed to be done by the caller.
2368 static void
2369 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2370 unsigned HOST_WIDE_INT size,
2371 unsigned int l2align)
2373 /* FIXME: We have a fudge to make this work with Java even when the target does
2374 not use sections anchors -- Java seems to need at least one small item in a
2375 non-zerofill segment. */
2376 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2377 || (size && size <= 2))
2379 /* Put smaller objects in _static_data, where the section anchors system
2380 can get them.
2381 However, if they are zero-sized punt them to yet a different section
2382 (that is not allowed to participate in anchoring). */
2383 if (!size)
2385 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2386 in_section = darwin_sections[zobj_bss_section];
2387 size = 1;
2389 else
2391 fputs ("\t.static_data\n", fp);
2392 in_section = darwin_sections[static_data_section];
2395 if (l2align)
2396 fprintf (fp, "\t.align\t%u\n", l2align);
2398 assemble_name (fp, name);
2399 fprintf (fp, ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2401 else
2403 /* When we are on a non-section anchor target, we can get zero-sized
2404 items here. However, all we need to do is to bump them to one byte
2405 and the section alignment will take care of the rest. */
2406 char secnam[64];
2407 unsigned int flags ;
2408 snprintf (secnam, 64, "__DATA,__%sbss%u", ((size)?"":"zo_"),
2409 (unsigned) l2align);
2410 /* We can't anchor (yet, if ever) in zerofill sections, because we can't
2411 switch to them and emit a label. */
2412 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2413 in_section = get_section (secnam, flags, NULL);
2414 fprintf (fp, "\t.zerofill %s,", secnam);
2415 assemble_name (fp, name);
2416 if (!size)
2417 size = 1;
2419 if (l2align)
2420 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2421 size, (unsigned) l2align);
2422 else
2423 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2426 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2427 /* This is defined as a file-scope var, so we know to notify machopic. */
2428 machopic_define_symbol (DECL_RTL (decl));
2431 /* Emit a chunk of common. */
2432 static void
2433 darwin_emit_common (FILE *fp, const char *name,
2434 unsigned HOST_WIDE_INT size, unsigned int align)
2436 unsigned HOST_WIDE_INT rounded;
2437 unsigned int l2align;
2439 /* Earlier systems complain if the alignment exceeds the page size.
2440 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2441 if (!emit_aligned_common && (align > 32768UL))
2442 align = 4096UL; /* In units. */
2443 else
2444 align /= BITS_PER_UNIT;
2446 /* Make sure we have a meaningful align. */
2447 if (!align)
2448 align = 1;
2450 /* For earlier toolchains, we need to emit the var as a rounded size to
2451 tell ld the alignment. */
2452 if (size < align)
2453 rounded = align;
2454 else
2455 rounded = (size + (align-1)) & ~(align-1);
2457 l2align = floor_log2 (align);
2458 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2460 in_section = comm_section;
2461 /* We mustn't allow multiple public symbols to share an address when using
2462 the normal OSX toolchain. */
2463 if (!size)
2465 /* Put at least one byte. */
2466 size = 1;
2467 /* This section can no longer participate in section anchoring. */
2468 comm_section->common.flags |= SECTION_NO_ANCHOR;
2471 fputs ("\t.comm\t", fp);
2472 assemble_name (fp, name);
2473 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2474 emit_aligned_common?size:rounded);
2475 if (l2align && emit_aligned_common)
2476 fprintf (fp, ",%u", l2align);
2477 fputs ("\n", fp);
2480 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2481 or coalescable data sections (for weak or comdat) as appropriate. */
2483 void
2484 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2485 unsigned HOST_WIDE_INT size, unsigned int align)
2487 unsigned int l2align;
2488 bool one, pub, weak;
2489 tree meta;
2491 pub = TREE_PUBLIC (decl);
2492 one = DECL_ONE_ONLY (decl);
2493 weak = (DECL_P (decl)
2494 && DECL_WEAK (decl)
2495 && !lookup_attribute ("weak_import",
2496 DECL_ATTRIBUTES (decl)));
2498 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2499 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2500 " pub %d weak %d one %d init %lx\n",
2501 name, (long long)size, (int)align, TREE_READONLY (decl),
2502 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2503 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2504 #endif
2506 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2507 before the target has a chance to comment. */
2508 if ((meta = is_objc_metadata (decl)))
2510 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2511 return;
2514 /* Check that any initializer is valid. */
2515 gcc_assert ((DECL_INITIAL (decl) == NULL)
2516 || (DECL_INITIAL (decl) == error_mark_node)
2517 || initializer_zerop (DECL_INITIAL (decl)));
2519 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2520 gcc_assert (!DECL_COMMON (decl));
2522 /* Pick up the correct alignment. */
2523 if (!size || !align)
2524 align = DECL_ALIGN (decl);
2526 l2align = floor_log2 (align / BITS_PER_UNIT);
2527 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2529 last_assemble_variable_decl = decl;
2531 /* We would rather not have to check this here - but it seems that we might
2532 be passed a decl that should be in coalesced space. */
2533 if (one || weak)
2535 /* Weak or COMDAT objects are put in mergeable sections. */
2536 darwin_emit_weak_or_comdat (fp, decl, name, size,
2537 ld_uses_coal_sects, DECL_ALIGN (decl));
2538 return;
2541 /* If this is not public, then emit according to local rules. */
2542 if (!pub)
2544 darwin_emit_local_bss (fp, decl, name, size, l2align);
2545 return;
2548 /* So we have a public symbol (small item fudge for Java, see above). */
2549 if ((DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2550 || (size && size <= 2))
2552 /* Put smaller objects in data, where the section anchors system can get
2553 them. However, if they are zero-sized punt them to yet a different
2554 section (that is not allowed to participate in anchoring). */
2555 if (!size)
2557 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2558 in_section = darwin_sections[zobj_data_section];
2559 size = 1;
2561 else
2563 fputs ("\t.data\n", fp);
2564 in_section = data_section;
2567 if (l2align)
2568 fprintf (fp, "\t.align\t%u\n", l2align);
2570 assemble_name (fp, name);
2571 fprintf (fp, ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2573 else
2575 char secnam[64];
2576 unsigned int flags ;
2577 /* When we are on a non-section anchor target, we can get zero-sized
2578 items here. However, all we need to do is to bump them to one byte
2579 and the section alignment will take care of the rest. */
2580 snprintf (secnam, 64, "__DATA,__%spu_bss%u", ((size)?"":"zo_"), l2align);
2582 /* We can't anchor in zerofill sections, because we can't switch
2583 to them and emit a label. */
2584 flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2585 in_section = get_section (secnam, flags, NULL);
2586 fprintf (fp, "\t.zerofill %s,", secnam);
2587 assemble_name (fp, name);
2588 if (!size)
2589 size = 1;
2591 if (l2align)
2592 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2593 else
2594 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2596 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2599 /* Output a chunk of common, with alignment specified (where the target
2600 supports this). */
2601 void
2602 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2603 unsigned HOST_WIDE_INT size,
2604 unsigned int align)
2606 unsigned int l2align;
2607 bool one, weak;
2608 tree meta;
2610 /* No corresponding var. */
2611 if (decl==NULL)
2613 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2614 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2615 #endif
2616 darwin_emit_common (fp, name, size, align);
2617 return;
2620 one = DECL_ONE_ONLY (decl);
2621 weak = (DECL_P (decl)
2622 && DECL_WEAK (decl)
2623 && !lookup_attribute ("weak_import",
2624 DECL_ATTRIBUTES (decl)));
2626 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2627 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2628 " weak %d one %d init %lx\n",
2629 name, (long long)size, (int)align, TREE_READONLY (decl),
2630 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2631 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2632 #endif
2634 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2635 before the target has a chance to comment. */
2636 if ((meta = is_objc_metadata (decl)))
2638 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2639 return;
2642 /* We shouldn't be messing with this if the decl has a section name. */
2643 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2645 /* We would rather not have to check this here - but it seems that we might
2646 be passed a decl that should be in coalesced space. */
2647 if (one || weak)
2649 /* Weak or COMDAT objects are put in mergable sections. */
2650 darwin_emit_weak_or_comdat (fp, decl, name, size,
2651 ld_uses_coal_sects, DECL_ALIGN (decl));
2652 return;
2655 /* We should only get here for DECL_COMMON, with a zero init (and, in
2656 principle, only for public symbols too - although we deal with local
2657 ones below). */
2659 /* Check the initializer is OK. */
2660 gcc_assert (DECL_COMMON (decl)
2661 && ((DECL_INITIAL (decl) == NULL)
2662 || (DECL_INITIAL (decl) == error_mark_node)
2663 || initializer_zerop (DECL_INITIAL (decl))));
2665 last_assemble_variable_decl = decl;
2667 if (!size || !align)
2668 align = DECL_ALIGN (decl);
2670 l2align = floor_log2 (align / BITS_PER_UNIT);
2671 /* Check we aren't asking for more aligment than the platform allows. */
2672 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2674 if (TREE_PUBLIC (decl) != 0)
2675 darwin_emit_common (fp, name, size, align);
2676 else
2677 darwin_emit_local_bss (fp, decl, name, size, l2align);
2680 /* Output a chunk of BSS with alignment specfied. */
2681 void
2682 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2683 unsigned HOST_WIDE_INT size,
2684 unsigned int align)
2686 unsigned long l2align;
2687 bool one, weak;
2688 tree meta;
2690 one = DECL_ONE_ONLY (decl);
2691 weak = (DECL_P (decl)
2692 && DECL_WEAK (decl)
2693 && !lookup_attribute ("weak_import",
2694 DECL_ATTRIBUTES (decl)));
2696 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2697 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2698 " weak %d init %lx\n",
2699 name, (long long)size, (int)align, TREE_READONLY (decl),
2700 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2701 weak , (unsigned long)DECL_INITIAL (decl));
2702 #endif
2704 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2705 before the target has a chance to comment. */
2706 if ((meta = is_objc_metadata (decl)))
2708 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2709 return;
2712 /* We shouldn't be messing with this if the decl has a section name. */
2713 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2715 /* We would rather not have to check this here - but it seems that we might
2716 be passed a decl that should be in coalesced space. */
2717 if (one || weak)
2719 /* Weak or COMDAT objects are put in mergable sections. */
2720 darwin_emit_weak_or_comdat (fp, decl, name, size,
2721 ld_uses_coal_sects, DECL_ALIGN (decl));
2722 return;
2725 /* .. and it should be suitable for placement in local mem. */
2726 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2727 /* .. and any initializer must be all-zero. */
2728 gcc_assert ((DECL_INITIAL (decl) == NULL)
2729 || (DECL_INITIAL (decl) == error_mark_node)
2730 || initializer_zerop (DECL_INITIAL (decl)));
2732 last_assemble_variable_decl = decl;
2734 if (!size || !align)
2735 align = DECL_ALIGN (decl);
2737 l2align = floor_log2 (align / BITS_PER_UNIT);
2738 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2740 darwin_emit_local_bss (fp, decl, name, size, l2align);
2743 /* Emit an assembler directive to set visibility for a symbol. The
2744 only supported visibilities are VISIBILITY_DEFAULT and
2745 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2746 extern". There is no MACH-O equivalent of ELF's
2747 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2749 void
2750 darwin_assemble_visibility (tree decl, int vis)
2752 if (vis == VISIBILITY_DEFAULT)
2754 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2756 fputs ("\t.private_extern ", asm_out_file);
2757 assemble_name (asm_out_file,
2758 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2759 fputs ("\n", asm_out_file);
2761 else
2762 warning (OPT_Wattributes, "protected visibility attribute "
2763 "not supported in this configuration; ignored");
2766 /* vec used by darwin_asm_dwarf_section.
2767 Maybe a hash tab would be better here - but the intention is that this is
2768 a very short list (fewer than 16 items) and each entry should (ideally,
2769 eventually) only be presented once.
2771 A structure to hold a dwarf debug section used entry. */
2773 typedef struct GTY(()) dwarf_sect_used_entry {
2774 const char *name;
2775 unsigned count;
2777 dwarf_sect_used_entry;
2780 /* A list of used __DWARF sections. */
2781 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2783 /* This is called when we are asked to assemble a named section and the
2784 name begins with __DWARF,. We keep a list of the section names (without
2785 the __DWARF, prefix) and use this to emit our required start label on the
2786 first switch to each section. */
2788 static void
2789 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2790 tree ARG_UNUSED (decl), bool is_for_lto)
2792 unsigned i;
2793 int namelen, extra = 0;
2794 const char *sect, *lto_add = "";
2795 char sname[64];
2796 dwarf_sect_used_entry *ref;
2797 bool found = false;
2799 gcc_checking_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2800 == (SECTION_DEBUG | SECTION_NAMED));
2802 /* We know that the name starts with __DWARF, or __GNU_DAWRF_LTO */
2803 sect = strchr (name, ',') + 1;
2804 namelen = strchr (sect, ',') - sect;
2805 gcc_checking_assert (namelen);
2807 /* The section switch is output as written... */
2808 fprintf (asm_out_file, "\t.section %s\n", name);
2810 /* ... but the string we keep to make section start labels needs
2811 adjustment for lto cases. */
2812 if (is_for_lto)
2814 lto_add = "_lto";
2815 extra = 4;
2818 snprintf (sname, 64, "%.*s%.*s", namelen, sect, extra, lto_add);
2819 namelen += extra;
2821 if (dwarf_sect_names_table == NULL)
2822 vec_alloc (dwarf_sect_names_table, 16);
2823 else
2824 for (i = 0;
2825 dwarf_sect_names_table->iterate (i, &ref);
2826 i++)
2828 if (!ref)
2829 break;
2830 if (!strcmp (ref->name, sname))
2832 found = true;
2833 ref->count++;
2834 break;
2838 if (!found)
2840 dwarf_sect_used_entry e;
2841 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2842 e.count = 1;
2843 e.name = xstrdup (sname);
2844 vec_safe_push (dwarf_sect_names_table, e);
2848 /* Output a difference of two labels that will be an assembly time
2849 constant if the two labels are local. (.long lab1-lab2 will be
2850 very different if lab1 is at the boundary between two sections; it
2851 will be relocated according to the second section, not the first,
2852 so one ends up with a difference between labels in different
2853 sections, which is bad in the dwarf2 eh context for instance.) */
2855 static int darwin_dwarf_label_counter;
2857 void
2858 darwin_asm_output_dwarf_delta (FILE *file, int size,
2859 const char *lab1, const char *lab2,
2860 HOST_WIDE_INT offset)
2862 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2863 && lab2[0] == '*' && lab2[1] == 'L');
2864 const char *directive = (size == 8 ? ".quad" : ".long");
2866 if (islocaldiff)
2867 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2868 else
2869 fprintf (file, "\t%s\t", directive);
2871 assemble_name_raw (file, lab1);
2872 fprintf (file, "-");
2873 assemble_name_raw (file, lab2);
2874 if (offset != 0)
2875 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
2876 if (islocaldiff)
2877 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2880 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2881 offsets are not represented using relocs in .o files; either the
2882 section never leaves the .o file, or the linker or other tool is
2883 responsible for parsing the DWARF and updating the offsets. */
2885 void
2886 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2887 HOST_WIDE_INT offset, section *base)
2889 char sname[64];
2890 int namelen, extra = 0;
2891 bool is_for_lto;
2892 const char *lto_add = "";
2894 gcc_checking_assert (base->common.flags & SECTION_NAMED);
2895 is_for_lto = strncmp (base->named.name, "__GNU_DWARF_LTO,", 16) == 0;
2896 gcc_checking_assert (is_for_lto
2897 || strncmp (base->named.name, "__DWARF,", 8) == 0);
2898 const char *name = strchr (base->named.name, ',') + 1;
2899 gcc_checking_assert (name);
2901 namelen = strchr (name, ',') - (name);
2902 if (is_for_lto)
2904 lto_add = "_lto";
2905 extra = 4;
2907 snprintf (sname, 64, "*Lsection%.*s%.*s", namelen, name, extra, lto_add);
2908 darwin_asm_output_dwarf_delta (file, size, lab, sname, offset);
2911 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2913 void
2914 darwin_file_start (void)
2916 /* Nothing to do. */
2919 /* Called for the TARGET_ASM_FILE_END hook.
2920 Emit the mach-o pic indirection data, the lto data and, finally a flag
2921 to tell the linker that it can break the file object into sections and
2922 move those around for efficiency. */
2924 void
2925 darwin_file_end (void)
2927 if (!vec_safe_is_empty (ctors))
2928 finalize_ctors ();
2929 if (!vec_safe_is_empty (dtors))
2930 finalize_dtors ();
2932 /* If we are expecting to output NeXT ObjC meta-data, (and we actually see
2933 some) then we output the fix-and-continue marker (Image Info).
2934 This applies to Objective C, Objective C++ and LTO with either language
2935 as part of the input. */
2936 if (flag_next_runtime && objc_metadata_seen)
2938 unsigned int flags = 0;
2939 if (flag_objc_abi >= 2)
2941 flags = 16;
2942 output_section_asm_op
2943 (darwin_sections[objc2_image_info_section]->unnamed.data);
2945 else
2946 output_section_asm_op
2947 (darwin_sections[objc_image_info_section]->unnamed.data);
2949 ASM_OUTPUT_ALIGN (asm_out_file, 2);
2950 fputs ("L_OBJC_ImageInfo:\n", asm_out_file);
2952 flags |= (flag_replace_objc_classes && classes_seen) ? 1 : 0;
2953 flags |= flag_objc_gc ? 2 : 0;
2955 fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags);
2958 machopic_finish (asm_out_file);
2959 if (lang_GNU_CXX ())
2961 switch_to_section (darwin_sections[constructor_section]);
2962 switch_to_section (darwin_sections[destructor_section]);
2963 ASM_OUTPUT_ALIGN (asm_out_file, 1);
2966 /* If there was LTO assembler output, append it to asm_out_file. */
2967 if (lto_asm_out_name)
2969 int n;
2970 char *buf, *lto_asm_txt;
2972 /* Shouldn't be here if we failed to switch back. */
2973 gcc_assert (! saved_asm_out_file);
2975 lto_asm_out_file = fopen (lto_asm_out_name, "r");
2976 if (lto_asm_out_file == NULL)
2977 fatal_error (input_location,
2978 "failed to open temporary file %s with LTO output",
2979 lto_asm_out_name);
2980 fseek (lto_asm_out_file, 0, SEEK_END);
2981 n = ftell (lto_asm_out_file);
2982 if (n > 0)
2984 fseek (lto_asm_out_file, 0, SEEK_SET);
2985 lto_asm_txt = buf = (char *) xmalloc (n + 1);
2986 while (fgets (lto_asm_txt, n, lto_asm_out_file))
2987 fputs (lto_asm_txt, asm_out_file);
2988 /* Put a termination label. */
2989 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2990 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2991 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
2992 lto_section_num);
2993 /* Make sure our termination label stays in this section. */
2994 fputs ("\t.space\t1\n", asm_out_file);
2997 /* Remove the temporary file. */
2998 fclose (lto_asm_out_file);
2999 unlink_if_ordinary (lto_asm_out_name);
3000 free (lto_asm_out_name);
3003 /* Output the names and indices. */
3004 if (lto_section_names && lto_section_names->length ())
3006 int count;
3007 darwin_lto_section_e *ref;
3008 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
3009 the latter up ourselves. */
3010 const char *op = integer_asm_op (4,0);
3012 /* Emit the names. */
3013 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
3014 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
3015 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
3017 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
3018 /* We have to jump through hoops to get the values of the intra-section
3019 offsets... */
3020 fprintf (asm_out_file,
3021 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
3022 count, count);
3023 fprintf (asm_out_file,
3024 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
3025 count, count+1, count);
3026 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
3028 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
3029 /* make sure our termination label stays in this section. */
3030 fputs ("\t.space\t1\n", asm_out_file);
3032 /* Emit the Index. */
3033 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
3034 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
3035 fputs ("\t.align\t2\n", asm_out_file);
3036 fputs ("# Section offset, Section length, Name offset, Name length\n",
3037 asm_out_file);
3038 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
3040 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
3041 op, count, ref->sectname);
3042 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
3043 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
3044 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
3048 /* If we have section anchors, then we must prevent the linker from
3049 re-arranging data. */
3050 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
3051 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
3054 /* TODO: Add a language hook for identifying if a decl is a vtable. */
3055 #define DARWIN_VTABLE_P(DECL) 0
3057 /* Cross-module name binding. Darwin does not support overriding
3058 functions at dynamic-link time, except for vtables in kexts. */
3060 bool
3061 darwin_binds_local_p (const_tree decl)
3063 return default_binds_local_p_1 (decl,
3064 TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
3067 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
3068 anchor relative to ".", the current section position. We cannot use
3069 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
3070 void
3071 darwin_asm_output_anchor (rtx symbol)
3073 fprintf (asm_out_file, "\t.set\t");
3074 assemble_name (asm_out_file, XSTR (symbol, 0));
3075 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
3076 SYMBOL_REF_BLOCK_OFFSET (symbol));
3079 /* Disable section anchoring on any section containing a zero-sized
3080 object. */
3081 bool
3082 darwin_use_anchors_for_symbol_p (const_rtx symbol)
3084 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
3086 section *sect;
3087 /* If the section contains a zero-sized object it's ineligible. */
3088 sect = SYMBOL_REF_BLOCK (symbol)->sect;
3089 /* This should have the effect of disabling anchors for vars that follow
3090 any zero-sized one, in a given section. */
3091 if (sect->common.flags & SECTION_NO_ANCHOR)
3092 return false;
3094 /* Also check the normal reasons for suppressing. */
3095 return default_use_anchors_for_symbol_p (symbol);
3097 else
3098 return false;
3101 /* Set the darwin specific attributes on TYPE. */
3102 void
3103 darwin_set_default_type_attributes (tree type)
3105 if (darwin_ms_struct
3106 && TREE_CODE (type) == RECORD_TYPE)
3107 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
3108 NULL_TREE,
3109 TYPE_ATTRIBUTES (type));
3112 /* True, iff we're generating code for loadable kernel extensions. */
3114 bool
3115 darwin_kextabi_p (void) {
3116 return flag_apple_kext;
3119 void
3120 darwin_override_options (void)
3122 /* Keep track of which (major) version we're generating code for. */
3123 if (darwin_macosx_version_min)
3125 if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
3126 generating_for_darwin_version = 10;
3127 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
3128 generating_for_darwin_version = 9;
3130 /* Earlier versions are not specifically accounted, until required. */
3133 /* Older Darwin ld could not coalesce weak entities without them being
3134 placed in special sections. */
3135 if (darwin_target_linker
3136 && (strverscmp (darwin_target_linker, MIN_LD64_NO_COAL_SECTS) < 0))
3137 ld_uses_coal_sects = true;
3139 /* In principle, this should be c-family only. However, we really need to
3140 set sensible defaults for LTO as well, since the section selection stuff
3141 should check for correctness re. the ABI. TODO: check and provide the
3142 flags (runtime & ABI) from the lto wrapper). */
3144 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
3145 if (!global_options_set.x_flag_objc_abi)
3146 global_options.x_flag_objc_abi
3147 = (!flag_next_runtime)
3149 : (TARGET_64BIT ? 2
3150 : (generating_for_darwin_version >= 9) ? 1
3151 : 0);
3153 /* Objective-C family ABI 2 is only valid for next/m64 at present. */
3154 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
3156 if (TARGET_64BIT && global_options.x_flag_objc_abi < 2)
3157 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 must be"
3158 " used for %<-m64%> targets with"
3159 " %<-fnext-runtime%>");
3160 if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
3161 error_at (UNKNOWN_LOCATION, "%<-fobjc-abi-version%> >= 2 is not"
3162 " supported on %<-m32%> targets with"
3163 " %<-fnext-runtime%>");
3166 /* Don't emit DWARF3/4 unless specifically selected. This is a
3167 workaround for tool bugs. */
3168 if (!global_options_set.x_dwarf_strict)
3169 dwarf_strict = 1;
3170 if (!global_options_set.x_dwarf_version)
3171 dwarf_version = 2;
3173 /* Do not allow unwind tables to be generated by default for m32.
3174 fnon-call-exceptions will override this, regardless of what we do. */
3175 if (generating_for_darwin_version < 10
3176 && !global_options_set.x_flag_asynchronous_unwind_tables
3177 && !TARGET_64BIT)
3178 global_options.x_flag_asynchronous_unwind_tables = 0;
3180 /* Disable -freorder-blocks-and-partition when unwind tables are being
3181 emitted for Darwin < 9 (OSX 10.5).
3182 The strategy is, "Unless the User has specifically set/unset an unwind
3183 flag we will switch off -freorder-blocks-and-partition when unwind tables
3184 will be generated". If the User specifically sets flags... we assume
3185 (s)he knows why... */
3186 if (generating_for_darwin_version < 9
3187 && global_options_set.x_flag_reorder_blocks_and_partition
3188 && ((global_options.x_flag_exceptions /* User, c++, java */
3189 && !global_options_set.x_flag_exceptions) /* User specified... */
3190 || (global_options.x_flag_unwind_tables
3191 && !global_options_set.x_flag_unwind_tables)
3192 || (global_options.x_flag_non_call_exceptions
3193 && !global_options_set.x_flag_non_call_exceptions)
3194 || (global_options.x_flag_asynchronous_unwind_tables
3195 && !global_options_set.x_flag_asynchronous_unwind_tables)))
3197 inform (input_location,
3198 "-freorder-blocks-and-partition does not work with exceptions "
3199 "on this architecture");
3200 flag_reorder_blocks_and_partition = 0;
3201 flag_reorder_blocks = 1;
3204 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3205 one valid choice of exception scheme for each runtime. */
3206 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3207 global_options.x_flag_objc_sjlj_exceptions =
3208 flag_next_runtime && !TARGET_64BIT;
3210 /* FIXME: and this could be eliminated then too. */
3211 if (!global_options_set.x_flag_exceptions
3212 && flag_objc_exceptions
3213 && TARGET_64BIT)
3214 flag_exceptions = 1;
3216 if (flag_mkernel || flag_apple_kext)
3218 /* -mkernel implies -fapple-kext for C++ */
3219 if (lang_GNU_CXX ())
3220 flag_apple_kext = 1;
3222 flag_no_common = 1;
3224 /* No EH in kexts. */
3225 flag_exceptions = 0;
3226 /* No -fnon-call-exceptions data in kexts. */
3227 flag_non_call_exceptions = 0;
3228 /* so no tables either.. */
3229 flag_unwind_tables = 0;
3230 flag_asynchronous_unwind_tables = 0;
3231 /* We still need to emit branch islands for kernel context. */
3232 darwin_emit_branch_islands = true;
3235 if (flag_var_tracking_uninit == 0
3236 && generating_for_darwin_version >= 9
3237 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3238 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3239 && write_symbols == DWARF2_DEBUG)
3240 flag_var_tracking_uninit = flag_var_tracking;
3242 if (MACHO_DYNAMIC_NO_PIC_P)
3244 if (flag_pic)
3245 warning_at (UNKNOWN_LOCATION, 0,
3246 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3247 " %<-fpie%> or %<-fPIE%>");
3248 flag_pic = 0;
3250 else if (flag_pic == 1)
3252 /* Darwin's -fpic is -fPIC. */
3253 flag_pic = 2;
3256 /* It is assumed that branch island stubs are needed for earlier systems. */
3257 if (generating_for_darwin_version < 9)
3258 darwin_emit_branch_islands = true;
3259 else
3260 emit_aligned_common = true; /* Later systems can support aligned common. */
3262 /* The c_dialect...() macros are not available to us here. */
3263 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3266 #if DARWIN_PPC
3267 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3269 static void
3270 darwin_patch_builtin (enum built_in_function fncode)
3272 tree fn = builtin_decl_explicit (fncode);
3273 tree sym;
3274 char *newname;
3276 if (!fn)
3277 return;
3279 sym = DECL_ASSEMBLER_NAME (fn);
3280 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3282 set_user_assembler_name (fn, newname);
3284 fn = builtin_decl_implicit (fncode);
3285 if (fn)
3286 set_user_assembler_name (fn, newname);
3289 void
3290 darwin_patch_builtins (void)
3292 if (LONG_DOUBLE_TYPE_SIZE != 128)
3293 return;
3295 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3296 #define PATCH_BUILTIN_NO64(fncode) \
3297 if (!TARGET_64BIT) \
3298 darwin_patch_builtin (fncode);
3299 #define PATCH_BUILTIN_VARIADIC(fncode) \
3300 if (!TARGET_64BIT \
3301 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3302 darwin_patch_builtin (fncode);
3303 #include "darwin-ppc-ldouble-patch.def"
3304 #undef PATCH_BUILTIN
3305 #undef PATCH_BUILTIN_NO64
3306 #undef PATCH_BUILTIN_VARIADIC
3308 #endif
3310 /* CFStrings implementation. */
3311 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3312 static GTY(()) tree cfstring_type_node = NULL_TREE;
3313 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3314 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3315 static GTY(()) tree pcint_type_node = NULL_TREE;
3316 static GTY(()) tree pcchar_type_node = NULL_TREE;
3318 static enum built_in_function darwin_builtin_cfstring;
3320 /* Store all constructed constant CFStrings in a hash table so that
3321 they get uniqued properly. */
3323 typedef struct GTY ((for_user)) cfstring_descriptor {
3324 /* The string literal. */
3325 tree literal;
3326 /* The resulting constant CFString. */
3327 tree constructor;
3328 } cfstring_descriptor;
3330 struct cfstring_hasher : ggc_ptr_hash<cfstring_descriptor>
3332 static hashval_t hash (cfstring_descriptor *);
3333 static bool equal (cfstring_descriptor *, cfstring_descriptor *);
3336 static GTY (()) hash_table<cfstring_hasher> *cfstring_htab;
3338 static tree
3339 add_builtin_field_decl (tree type, const char *name, tree **chain)
3341 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3342 get_identifier (name), type);
3344 if (*chain != NULL)
3345 **chain = field;
3346 *chain = &DECL_CHAIN (field);
3348 return field;
3351 tree
3352 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3354 tree cfsfun, fields, pccfstring_ftype_pcchar;
3355 tree *chain = NULL;
3357 darwin_builtin_cfstring =
3358 (enum built_in_function) builtin_cfstring;
3360 /* struct __builtin_CFString {
3361 const int *isa; (will point at
3362 int flags; __CFConstantStringClassReference)
3363 const char *str;
3364 long length;
3365 }; */
3367 pcint_type_node = build_pointer_type
3368 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3370 pcchar_type_node = build_pointer_type
3371 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3373 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3375 /* Have to build backwards for finish struct. */
3376 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3377 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3378 add_builtin_field_decl (integer_type_node, "flags", &chain);
3379 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3380 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3381 fields, NULL_TREE);
3383 /* const struct __builtin_CFstring *
3384 __builtin___CFStringMakeConstantString (const char *); */
3386 ccfstring_type_node = build_qualified_type
3387 (cfstring_type_node, TYPE_QUAL_CONST);
3388 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3389 pccfstring_ftype_pcchar = build_function_type_list
3390 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3392 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3393 get_identifier ("__builtin___CFStringMakeConstantString"),
3394 pccfstring_ftype_pcchar);
3396 TREE_PUBLIC (cfsfun) = 1;
3397 DECL_EXTERNAL (cfsfun) = 1;
3398 DECL_ARTIFICIAL (cfsfun) = 1;
3399 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3400 in place of the existing, which may be NULL. */
3401 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3402 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3403 DECL_BUILT_IN_CLASS (cfsfun) = BUILT_IN_MD;
3404 DECL_FUNCTION_CODE (cfsfun) = darwin_builtin_cfstring;
3405 lang_hooks.builtin_function (cfsfun);
3407 /* extern int __CFConstantStringClassReference[]; */
3408 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3409 get_identifier ("__CFConstantStringClassReference"),
3410 build_array_type (integer_type_node, NULL_TREE));
3412 TREE_PUBLIC (cfstring_class_reference) = 1;
3413 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3414 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3415 DECL_EXTERNAL (cfstring_class_reference) = 1;
3416 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3418 /* Initialize the hash table used to hold the constant CFString objects. */
3419 cfstring_htab = hash_table<cfstring_hasher>::create_ggc (31);
3421 return cfstring_type_node;
3424 tree
3425 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3426 bool ARG_UNUSED (ignore))
3428 unsigned int fcode = DECL_FUNCTION_CODE (fndecl);
3430 if (fcode == darwin_builtin_cfstring)
3432 if (!darwin_constant_cfstrings)
3434 error ("built-in function %qD requires the"
3435 " %<-mconstant-cfstrings%> flag", fndecl);
3436 return error_mark_node;
3439 if (n_args != 1)
3441 error ("built-in function %qD takes one argument only", fndecl);
3442 return error_mark_node;
3445 return darwin_build_constant_cfstring (*argp);
3448 return NULL_TREE;
3451 void
3452 darwin_rename_builtins (void)
3454 /* The system ___divdc3 routine in libSystem on darwin10 is not
3455 accurate to 1ulp, ours is, so we avoid ever using the system name
3456 for this routine and instead install a non-conflicting name that
3457 is accurate.
3459 When -ffast-math or -funsafe-math-optimizations is given, we can
3460 use the faster version. */
3461 if (!flag_unsafe_math_optimizations)
3463 enum built_in_function dcode
3464 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3465 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3466 tree fn = builtin_decl_explicit (dcode);
3467 /* Fortran and c call TARGET_INIT_BUILTINS and
3468 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3469 call into each to ensure that at least one of them is called
3470 after build_common_builtin_nodes. A better fix is to add a
3471 new hook to run after build_common_builtin_nodes runs. */
3472 if (fn)
3473 set_user_assembler_name (fn, "___ieee_divdc3");
3474 fn = builtin_decl_implicit (dcode);
3475 if (fn)
3476 set_user_assembler_name (fn, "___ieee_divdc3");
3480 bool
3481 darwin_libc_has_function (enum function_class fn_class)
3483 if (fn_class == function_sincos)
3484 return false;
3485 if (fn_class == function_c99_math_complex
3486 || fn_class == function_c99_misc)
3487 return (TARGET_64BIT
3488 || strverscmp (darwin_macosx_version_min, "10.3") >= 0);
3490 return true;
3493 hashval_t
3494 cfstring_hasher::hash (cfstring_descriptor *ptr)
3496 tree str = ptr->literal;
3497 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3498 int i, len = TREE_STRING_LENGTH (str);
3499 hashval_t h = len;
3501 for (i = 0; i < len; i++)
3502 h = ((h * 613) + p[i]);
3504 return h;
3507 bool
3508 cfstring_hasher::equal (cfstring_descriptor *ptr1, cfstring_descriptor *ptr2)
3510 tree str1 = ptr1->literal;
3511 tree str2 = ptr2->literal;
3512 int len1 = TREE_STRING_LENGTH (str1);
3514 return (len1 == TREE_STRING_LENGTH (str2)
3515 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3516 len1));
3519 tree
3520 darwin_build_constant_cfstring (tree str)
3522 struct cfstring_descriptor *desc, key;
3523 tree addr;
3525 if (!str)
3527 error ("CFString literal is missing");
3528 return error_mark_node;
3531 STRIP_NOPS (str);
3533 if (TREE_CODE (str) == ADDR_EXPR)
3534 str = TREE_OPERAND (str, 0);
3536 if (TREE_CODE (str) != STRING_CST)
3538 error ("CFString literal expression is not a string constant");
3539 return error_mark_node;
3542 /* Perhaps we already constructed a constant CFString just like this one? */
3543 key.literal = str;
3544 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
3545 desc = *loc;
3547 if (!desc)
3549 tree var, constructor, field;
3550 vec<constructor_elt, va_gc> *v = NULL;
3551 int length = TREE_STRING_LENGTH (str) - 1;
3553 if (darwin_warn_nonportable_cfstrings)
3555 const char *s = TREE_STRING_POINTER (str);
3556 int l = 0;
3558 for (l = 0; l < length; l++)
3559 if (!s[l] || !isascii (s[l]))
3561 warning (darwin_warn_nonportable_cfstrings, "%s in CFString literal",
3562 s[l] ? "non-ASCII character" : "embedded NUL");
3563 break;
3567 *loc = desc = ggc_cleared_alloc<cfstring_descriptor> ();
3568 desc->literal = str;
3570 /* isa *. */
3571 field = TYPE_FIELDS (ccfstring_type_node);
3572 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3573 build1 (ADDR_EXPR, TREE_TYPE (field),
3574 cfstring_class_reference));
3575 /* flags */
3576 field = DECL_CHAIN (field);
3577 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3578 build_int_cst (TREE_TYPE (field), 0x000007c8));
3579 /* string *. */
3580 field = DECL_CHAIN (field);
3581 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3582 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3583 /* length */
3584 field = DECL_CHAIN (field);
3585 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3586 build_int_cst (TREE_TYPE (field), length));
3588 constructor = build_constructor (ccfstring_type_node, v);
3589 TREE_READONLY (constructor) = 1;
3590 TREE_CONSTANT (constructor) = 1;
3591 TREE_STATIC (constructor) = 1;
3593 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3594 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3595 being built without any knowledge of C++ tree accessors; hence, we shall
3596 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3597 if (darwin_running_cxx)
3598 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3600 /* Create an anonymous global variable for this CFString. */
3601 var = build_decl (input_location, CONST_DECL,
3602 NULL, TREE_TYPE (constructor));
3603 DECL_ARTIFICIAL (var) = 1;
3604 TREE_STATIC (var) = 1;
3605 DECL_INITIAL (var) = constructor;
3606 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3607 DECL_CONTEXT (var) = NULL_TREE;
3608 desc->constructor = var;
3611 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3612 TREE_CONSTANT (addr) = 1;
3614 return addr;
3617 bool
3618 darwin_cfstring_p (tree str)
3620 struct cfstring_descriptor key;
3622 if (!str)
3623 return false;
3625 STRIP_NOPS (str);
3627 if (TREE_CODE (str) == ADDR_EXPR)
3628 str = TREE_OPERAND (str, 0);
3630 if (TREE_CODE (str) != STRING_CST)
3631 return false;
3633 key.literal = str;
3634 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, NO_INSERT);
3636 if (loc)
3637 return true;
3639 return false;
3642 void
3643 darwin_enter_string_into_cfstring_table (tree str)
3645 struct cfstring_descriptor key;
3647 key.literal = str;
3648 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
3650 if (!*loc)
3652 *loc = ggc_cleared_alloc<cfstring_descriptor> ();
3653 ((struct cfstring_descriptor *)*loc)->literal = str;
3657 /* Choose named function section based on its frequency. */
3659 section *
3660 darwin_function_section (tree decl, enum node_frequency freq,
3661 bool startup, bool exit)
3663 /* Decide if we need to put this in a coalescable section. */
3664 bool weak = (decl
3665 && DECL_WEAK (decl)
3666 && (!DECL_ATTRIBUTES (decl)
3667 || !lookup_attribute ("weak_import",
3668 DECL_ATTRIBUTES (decl))));
3670 bool use_coal = weak && ld_uses_coal_sects;
3671 /* If there is a specified section name, we should not be trying to
3672 override. */
3673 if (decl && DECL_SECTION_NAME (decl) != NULL)
3674 return get_named_section (decl, NULL, 0);
3676 /* We always put unlikely executed stuff in the cold section. */
3677 if (freq == NODE_FREQUENCY_UNLIKELY_EXECUTED)
3678 return (use_coal) ? darwin_sections[text_cold_coal_section]
3679 : darwin_sections[text_cold_section];
3681 /* If we have LTO *and* feedback information, then let LTO handle
3682 the function ordering, it makes a better job (for normal, hot,
3683 startup and exit - hence the bailout for cold above). */
3684 if (in_lto_p && flag_profile_values)
3685 goto default_function_sections;
3687 /* Non-cold startup code should go to startup subsection. */
3688 if (startup)
3689 return (use_coal) ? darwin_sections[text_startup_coal_section]
3690 : darwin_sections[text_startup_section];
3692 /* Similarly for exit. */
3693 if (exit)
3694 return (use_coal) ? darwin_sections[text_exit_coal_section]
3695 : darwin_sections[text_exit_section];
3697 /* Place hot code. */
3698 if (freq == NODE_FREQUENCY_HOT)
3699 return (use_coal) ? darwin_sections[text_hot_coal_section]
3700 : darwin_sections[text_hot_section];
3702 /* Otherwise, default to the 'normal' non-reordered sections. */
3703 default_function_sections:
3704 return (use_coal) ? darwin_sections[text_coal_section]
3705 : text_section;
3708 /* When a function is partitioned between sections, we need to insert a label
3709 at the start of each new chunk - so that it may become a valid 'atom' for
3710 eh and debug purposes. Without this the linker will emit warnings if one
3711 tries to add line location information (since the switched fragment will
3712 be anonymous). */
3714 void
3715 darwin_function_switched_text_sections (FILE *fp, tree decl, bool new_is_cold)
3717 /* Make sure we pick up all the relevant quotes etc. */
3718 assemble_name_raw (fp, new_is_cold ? "__cold_sect_of_" : "__hot_sect_of_");
3719 assemble_name_raw (fp, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
3720 fputs (":\n", fp);
3723 #include "gt-darwin.h"