testsuite: Correct vec-rlmi-rlnm.c testsuite expected result
[official-gcc.git] / gcc / config / darwin.c
blobdd4857f9e34d83f98aceef82478748b7d25deabd
1 /* Functions for generic Darwin as target machine for GNU C compiler.
2 Copyright (C) 1989-2020 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"
47 #include "intl.h"
48 #include "optabs.h"
50 /* Fix and Continue.
52 NOTES:
53 1) this facility requires suitable support from a modified version
54 of GDB, which is not provided on any system after MacOS 10.7/Darwin11.
55 2) There is no support for this in any X86 version of the FSF compiler.
57 Fix and continue was used on some earlier MacOS systems for rapid turn
58 around debugging. When code is compiled with the -mfix-and-continue
59 flag, two changes are made to the generated code that allow the system
60 to do things that it would normally not be able to do easily. These
61 changes allow gdb to load in recompilation of a translation unit that
62 has been changed into a running program and replace existing functions
63 and methods of that translation unit with versions of those functions
64 and methods from the newly compiled translation unit. The new functions
65 access the existing static symbols from the old translation unit, if the
66 symbol existed in the unit to be replaced, and from the new translation
67 unit, otherwise.
69 The changes are to insert 5 nops at the beginning of all functions
70 and to use indirection to get at static symbols. The 5 nops
71 are required by consumers of the generated code. Currently, gdb
72 uses this to patch in a jump to the overriding function, this
73 allows all uses of the old name to forward to the replacement,
74 including existing function pointers and virtual methods. See
75 rs6000_emit_prologue for the code that handles the nop insertions.
77 The added indirection allows gdb to redirect accesses to static
78 symbols from the newly loaded translation unit to the existing
79 symbol, if any. @code{static} symbols are special and are handled by
80 setting the second word in the .non_lazy_symbol_pointer data
81 structure to symbol. See indirect_data for the code that handles
82 the extra indirection, and machopic_output_indirection and its use
83 of MACHO_SYMBOL_FLAG_STATIC for the code that handles @code{static}
84 symbol indirection. */
86 typedef struct GTY(()) cdtor_record {
87 rtx symbol;
88 int priority; /* [con/de]structor priority */
89 int position; /* original position */
90 } cdtor_record;
92 static GTY(()) vec<cdtor_record, va_gc> *ctors = NULL;
93 static GTY(()) vec<cdtor_record, va_gc> *dtors = NULL;
95 /* A flag to determine whether we are running c++ or obj-c++. This has to be
96 settable from non-c-family contexts too (i.e. we can't use the c_dialect_
97 functions). */
98 int darwin_running_cxx;
100 /* Some code-gen now depends on OS major version numbers (at least). */
101 int generating_for_darwin_version ;
103 /* For older linkers we need to emit special sections (marked 'coalesced') for
104 for weak or single-definition items. */
105 static bool ld_uses_coal_sects = false;
107 /* Very old (ld_classic) linkers need a symbol to mark the start of
108 each FDE. */
109 static bool ld_needs_eh_markers = false;
111 /* Section names. */
112 section * darwin_sections[NUM_DARWIN_SECTIONS];
114 /* While we transition to using in-tests instead of ifdef'd code. */
115 #if !HAVE_lo_sum
116 #define gen_macho_high(m,a,b) (a)
117 #define gen_macho_low(m,a,b,c) (a)
118 #endif
120 /* True if we're setting __attribute__ ((ms_struct)). */
121 int darwin_ms_struct = false;
123 /* Earlier versions of Darwin as do not recognize an alignment field in
124 .comm directives, this should be set for versions that allow it. */
125 int emit_aligned_common = false;
127 /* A get_unnamed_section callback used to switch to an ObjC section.
128 DIRECTIVE is as for output_section_asm_op. */
130 static void
131 output_objc_section_asm_op (const void *directive)
133 static bool been_here = false;
135 /* The NeXT ObjC Runtime requires these sections to be present and in
136 order in the object. The code below implements this by emitting
137 a section header for each ObjC section the first time that an ObjC
138 section is requested. */
139 if (darwin_symbol_stubs && ! been_here)
141 section *saved_in_section = in_section;
142 static const enum darwin_section_enum tomark[] =
144 /* written, cold -> hot */
145 objc_cat_cls_meth_section,
146 objc_cat_inst_meth_section,
147 objc_string_object_section,
148 objc_constant_string_object_section,
149 objc_selector_refs_section,
150 objc_selector_fixup_section,
151 objc_cls_refs_section,
152 objc_class_section,
153 objc_meta_class_section,
154 /* shared, hot -> cold */
155 objc_cls_meth_section,
156 objc_inst_meth_section,
157 objc_protocol_section,
158 objc_class_names_section,
159 objc_meth_var_types_section,
160 objc_meth_var_names_section,
161 objc_category_section,
162 objc_class_vars_section,
163 objc_instance_vars_section,
164 objc_module_info_section,
165 objc_symbols_section,
167 /* ABI=1 */
168 static const enum darwin_section_enum tomarkv1[] =
170 objc1_protocol_ext_section,
171 objc1_class_ext_section,
172 objc1_prop_list_section
174 /* ABI=2 */
175 static const enum darwin_section_enum tomarkv2[] =
177 objc2_method_names_section,
178 objc2_message_refs_section,
179 objc2_selector_refs_section,
180 objc2_ivar_section,
181 objc2_classdefs_section,
182 objc2_metadata_section,
183 objc2_classrefs_section,
184 objc2_class_names_section,
185 objc2_classlist_section,
186 objc2_categorylist_section,
187 objc2_nonlazy_class_section,
188 objc2_nonlazy_category_section,
189 objc2_protocollist_section,
190 objc2_protocolrefs_section,
191 objc2_super_classrefs_section,
192 objc2_constant_string_object_section,
193 objc2_image_info_section,
195 size_t i;
197 been_here = true;
198 if (flag_objc_abi < 2)
200 for (i = 0; i < ARRAY_SIZE (tomark); i++)
201 switch_to_section (darwin_sections[tomark[i]]);
202 if (flag_objc_abi == 1)
203 for (i = 0; i < ARRAY_SIZE (tomarkv1); i++)
204 switch_to_section (darwin_sections[tomarkv1[i]]);
206 else
207 for (i = 0; i < ARRAY_SIZE (tomarkv2); i++)
208 switch_to_section (darwin_sections[tomarkv2[i]]);
209 /* Make sure we don't get varasm.c out of sync with us. */
210 switch_to_section (saved_in_section);
212 output_section_asm_op (directive);
216 /* Private flag applied to disable section-anchors in a particular section. */
217 #define SECTION_NO_ANCHOR SECTION_MACH_DEP
220 /* Implement TARGET_ASM_INIT_SECTIONS. */
222 void
223 darwin_init_sections (void)
225 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
226 darwin_sections[NAME] = \
227 get_unnamed_section (FLAGS, (OBJC \
228 ? output_objc_section_asm_op \
229 : output_section_asm_op), \
230 "\t" DIRECTIVE);
231 #include "config/darwin-sections.def"
232 #undef DEF_SECTION
234 readonly_data_section = darwin_sections[const_section];
235 exception_section = darwin_sections[darwin_exception_section];
236 eh_frame_section = darwin_sections[darwin_eh_frame_section];
238 /* If our linker is new enough to coalesce weak symbols, then we
239 can just put picbase_thunks into the text section. */
240 if (! ld_uses_coal_sects )
241 darwin_sections[picbase_thunk_section] = text_section;
245 name_needs_quotes (const char *name)
247 int c;
248 while ((c = *name++) != '\0')
249 if (! ISIDNUM (c)
250 && c != '.' && c != '$' && c != '_' )
251 return 1;
252 return 0;
255 /* Return true if SYM_REF can be used without an indirection. */
257 machopic_symbol_defined_p (rtx sym_ref)
259 if (MACHO_SYMBOL_DEFINED_P (sym_ref))
260 return true;
262 /* If a symbol references local and is not an extern to this
263 file, then the symbol might be able to declared as defined. */
264 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
266 /* If the symbol references a variable and the variable is a
267 common symbol, then this symbol is not defined. */
268 if (MACHO_SYMBOL_VARIABLE_P (sym_ref))
270 tree decl = SYMBOL_REF_DECL (sym_ref);
271 if (!decl)
272 return true;
273 if (DECL_COMMON (decl))
274 return false;
276 return true;
278 return false;
281 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
282 reference, which will not be changed. */
284 enum machopic_addr_class
285 machopic_classify_symbol (rtx sym_ref)
287 bool function_p;
289 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
290 if (machopic_symbol_defined_p (sym_ref))
291 return (function_p
292 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
293 else
294 return (function_p
295 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
298 #ifndef TARGET_FIX_AND_CONTINUE
299 #define TARGET_FIX_AND_CONTINUE 0
300 #endif
302 /* Indicate when fix-and-continue style code generation is being used
303 and when a reference to data should be indirected so that it can be
304 rebound in a new translation unit to reference the original instance
305 of that data. Symbol names that are for code generation local to
306 the translation unit are bound to the new translation unit;
307 currently this means symbols that begin with L or _OBJC_;
308 otherwise, we indicate that an indirect reference should be made to
309 permit the runtime to rebind new instances of the translation unit
310 to the original instance of the data. */
312 static int
313 indirect_data (rtx sym_ref)
315 int lprefix;
316 const char *name;
318 /* If we aren't generating fix-and-continue code, don't do anything
319 special. */
320 if (TARGET_FIX_AND_CONTINUE == 0)
321 return 0;
323 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
324 are indirected. Symbols that begin with L and _OBJC_ are always
325 bound to the current translation unit as they are used for
326 generated local data of the translation unit. */
328 name = XSTR (sym_ref, 0);
330 lprefix = (((name[0] == '*' || name[0] == '&')
331 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
332 || (strncmp (name, "_OBJC_", 6) == 0));
334 return ! lprefix;
337 static int
338 machopic_data_defined_p (rtx sym_ref)
340 if (indirect_data (sym_ref))
341 return 0;
343 switch (machopic_classify_symbol (sym_ref))
345 case MACHOPIC_DEFINED_DATA:
346 case MACHOPIC_DEFINED_FUNCTION:
347 return 1;
348 default:
349 return 0;
353 void
354 machopic_define_symbol (rtx mem)
356 rtx sym_ref;
358 gcc_assert (GET_CODE (mem) == MEM);
359 sym_ref = XEXP (mem, 0);
360 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
363 /* Return either ORIG or:
365 (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
367 depending on MACHO_DYNAMIC_NO_PIC_P. */
369 machopic_gen_offset (rtx orig)
371 if (MACHO_DYNAMIC_NO_PIC_P)
372 return orig;
373 else
375 /* Play games to avoid marking the function as needing pic if we
376 are being called as part of the cost-estimation process. */
377 if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
378 crtl->uses_pic_offset_table = 1;
379 orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
380 UNSPEC_MACHOPIC_OFFSET);
381 return gen_rtx_CONST (Pmode, orig);
385 static GTY(()) const char * function_base_func_name = NULL;
386 static GTY(()) unsigned current_pic_label_num = 0;
387 static GTY(()) unsigned emitted_pic_label_num = 0;
389 /* We need to keep one picbase label per function, but (when we emit code
390 to reload the picbase for setjump receiver) we might need to check for
391 a second use. So, only update the picbase label counter when we see a
392 new function. When there's no function decl, we assume that the call is
393 from the x86 stub generation code. */
394 static void
395 update_pic_label_number_if_needed (void)
397 if (current_function_decl)
400 const char *current_name =
401 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
402 if (function_base_func_name != current_name)
404 ++current_pic_label_num;
405 function_base_func_name = current_name;
408 else
410 ++current_pic_label_num;
411 function_base_func_name = "L_machopic_stub_dummy";
415 void
416 machopic_output_function_base_name (FILE *file)
418 /* We should only get here for -fPIC. */
419 gcc_checking_assert (MACHOPIC_PURE);
421 update_pic_label_number_if_needed ();
422 fprintf (file, "L%u$pb", current_pic_label_num);
425 char curr_picbasename[32];
427 const char *
428 machopic_get_function_picbase (void)
430 /* We should only get here for -fPIC. */
431 gcc_checking_assert (MACHOPIC_PURE);
433 update_pic_label_number_if_needed ();
434 snprintf (curr_picbasename, 32, "L%u$pb", current_pic_label_num);
435 return (const char *) curr_picbasename;
438 bool
439 machopic_should_output_picbase_label (void)
441 update_pic_label_number_if_needed ();
443 if (current_pic_label_num == emitted_pic_label_num)
444 return false;
446 emitted_pic_label_num = current_pic_label_num;
447 return true;
450 /* The suffix attached to non-lazy pointer symbols. */
451 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
452 /* The suffix attached to stub symbols. */
453 #define STUB_SUFFIX "$stub"
455 typedef struct GTY ((for_user)) machopic_indirection
457 /* The SYMBOL_REF for the entity referenced. */
458 rtx symbol;
459 /* The name of the stub or non-lazy pointer. */
460 const char * ptr_name;
461 /* True iff this entry is for a stub (as opposed to a non-lazy
462 pointer). */
463 bool stub_p;
464 /* True iff this stub or pointer has been referenced. */
465 bool used;
466 /* True iff a non-lazy symbol pointer should be emitted into the .data
467 section, rather than the non-lazy symbol pointers section. The cases
468 for which this occurred seem to have been unintentional, and later
469 toolchains emit all of the indirections to the 'usual' section. We
470 are keeping this in case it is necessary to preserve compatibility with
471 older toolchains. */
472 bool nlsp_in_data_section;
473 } machopic_indirection;
475 struct indirection_hasher : ggc_ptr_hash<machopic_indirection>
477 typedef const char *compare_type;
478 static hashval_t hash (machopic_indirection *);
479 static bool equal (machopic_indirection *, const char *);
482 /* A table mapping stub names and non-lazy pointer names to
483 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
485 static GTY (()) hash_table<indirection_hasher> *machopic_indirections;
487 /* Return a hash value for a SLOT in the indirections hash table. */
489 hashval_t
490 indirection_hasher::hash (machopic_indirection *p)
492 return htab_hash_string (p->ptr_name);
495 /* Returns true if the KEY is the same as that associated with
496 SLOT. */
498 bool
499 indirection_hasher::equal (machopic_indirection *s, const char *k)
501 return strcmp (s->ptr_name, k) == 0;
504 /* Return the name of the non-lazy pointer (if STUB_P is false) or
505 stub (if STUB_B is true) corresponding to the given name.
507 PR71767 - If we have a situation like:
509 global_weak_symbol:
510 ....
511 Lnon_weak_local:
512 ....
514 ld64 will be unable to split this into two atoms (because the "L" makes
515 the second symbol 'invisible'). This means that legitimate direct accesses
516 to the second symbol will appear to be direct accesses to an atom of type
517 weak, global which are not allowed.
519 To avoid this, we make any data-section indirections have a leading 'l'
520 (lower-case L) which has a special meaning: linker can see this and use
521 it to determine atoms, but it is not placed into the final symbol table.
523 Symbols in the non-lazy symbol pointers section (or stubs) do not have this
524 problem because ld64 already knows the size of each entry.
527 const char *
528 machopic_indirection_name (rtx sym_ref, bool stub_p)
530 const char *name = XSTR (sym_ref, 0);
531 tree id = maybe_get_identifier (name);
532 if (id)
534 tree id_orig = id;
536 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
537 id = TREE_CHAIN (id);
538 if (id != id_orig)
539 name = IDENTIFIER_POINTER (id);
542 const char *prefix = user_label_prefix;
543 /* If we are emitting the label 'verbatim' then omit the U_L_P and count
544 the name without the leading '*'. */
545 if (name[0] == '*')
547 prefix = "";
548 ++name;
551 /* Here we are undoing a number of causes that placed some indirections
552 (apparently erroneously) into the .data section. Specifically, some
553 symbols that are ABI mandated indirections and some hidden symbols
554 were being placed there - which cause difficulties with later
555 versions of ld64. Iff (after these checks) some symbol still gets an
556 indirection in the data section, we want to adjust the indirection
557 name to be linker visible to deal with PR71767 (notes above). */
558 bool nlsp_in_data_section =
559 ! MACHO_SYMBOL_MUST_INDIRECT_P (sym_ref)
560 && ! MACHO_SYMBOL_HIDDEN_VIS_P (sym_ref)
561 && (machopic_symbol_defined_p (sym_ref) || SYMBOL_REF_LOCAL_P (sym_ref))
562 && ! indirect_data (sym_ref);
564 const char *suffix = stub_p ? STUB_SUFFIX : NON_LAZY_POINTER_SUFFIX;
565 /* If the indirection is in the data section, let the linker see it. */
566 char L_or_l = (!stub_p && nlsp_in_data_section) ? 'l' : 'L';
567 /* We have mangled symbols with spaces and punctuation which typically
568 need surrounding in quotes for the assembler to consume them. */
569 const char *quote = name_needs_quotes (name) ? "\"" : "";
570 char *buffer = XALLOCAVEC (char, 2 /* strlen ("&L") or ("&l") */
571 + strlen (prefix)
572 + strlen (name)
573 + strlen (suffix)
574 + 2 * strlen (quote)
575 + 1 /* '\0' */);
577 /* Construct the name of the non-lazy pointer or stub. */
578 sprintf (buffer, "&%s%c%s%s%s%s", quote, L_or_l, prefix, name,
579 suffix, quote);
581 if (!machopic_indirections)
582 machopic_indirections = hash_table<indirection_hasher>::create_ggc (37);
584 machopic_indirection **slot
585 = machopic_indirections->find_slot_with_hash (buffer,
586 htab_hash_string (buffer),
587 INSERT);
588 machopic_indirection *p;
589 if (*slot)
590 p = *slot;
591 else
593 p = ggc_alloc<machopic_indirection> ();
594 p->symbol = sym_ref;
595 p->ptr_name = xstrdup (buffer);
596 p->stub_p = stub_p;
597 p->used = false;
598 p->nlsp_in_data_section = nlsp_in_data_section;
599 *slot = p;
602 return p->ptr_name;
605 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
606 or non-lazy pointer as used -- and mark the object to which the
607 pointer/stub refers as used as well, since the pointer/stub will
608 emit a reference to it. */
610 void
611 machopic_validate_stub_or_non_lazy_ptr (const char *name)
613 machopic_indirection *p
614 = machopic_indirections->find_with_hash (name, htab_hash_string (name));
615 if (p && ! p->used)
617 const char *real_name;
618 tree id;
620 p->used = true;
622 /* Do what output_addr_const will do when we actually call it. */
623 if (SYMBOL_REF_DECL (p->symbol))
624 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
626 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
628 id = maybe_get_identifier (real_name);
629 if (id)
630 mark_referenced (id);
634 /* Transform ORIG, which may be any data source, to the corresponding
635 source using indirections. */
638 machopic_indirect_data_reference (rtx orig, rtx reg)
640 rtx ptr_ref = orig;
642 if (! MACHOPIC_INDIRECT)
643 return orig;
645 if (GET_CODE (orig) == SYMBOL_REF)
647 int defined = machopic_data_defined_p (orig);
649 if (defined && MACHO_DYNAMIC_NO_PIC_P)
651 if (DARWIN_PPC)
653 /* Create a new register for CSE opportunities. */
654 rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
655 emit_insn (gen_macho_high (Pmode, hi_reg, orig));
656 emit_insn (gen_macho_low (Pmode, reg, hi_reg, orig));
657 return reg;
659 else if (DARWIN_X86)
660 return orig;
661 else
662 /* some other cpu -- writeme! */
663 gcc_unreachable ();
665 else if (defined && ! MACHO_SYMBOL_MUST_INDIRECT_P (orig))
667 rtx offset = NULL;
668 if (DARWIN_PPC || HAVE_lo_sum)
669 offset = machopic_gen_offset (orig);
671 if (DARWIN_PPC)
673 rtx hi_sum_reg = (!can_create_pseudo_p ()
674 ? reg
675 : gen_reg_rtx (Pmode));
677 gcc_assert (reg);
679 emit_insn (gen_rtx_SET (hi_sum_reg,
680 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
681 gen_rtx_HIGH (Pmode, offset))));
682 emit_insn (gen_rtx_SET (reg,
683 gen_rtx_LO_SUM (Pmode, hi_sum_reg,
684 copy_rtx (offset))));
686 orig = reg;
688 else if (HAVE_lo_sum)
690 gcc_assert (reg);
692 emit_insn (gen_rtx_SET (reg, gen_rtx_HIGH (Pmode, offset)));
693 emit_insn (gen_rtx_SET (reg, gen_rtx_LO_SUM (Pmode, reg,
694 copy_rtx (offset))));
695 emit_use (pic_offset_table_rtx);
697 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
699 return orig;
702 ptr_ref = (gen_rtx_SYMBOL_REF
703 (Pmode,
704 machopic_indirection_name (orig, /*stub_p=*/false)));
706 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
707 SYMBOL_REF_FLAGS (ptr_ref) |= MACHO_SYMBOL_FLAG_INDIRECTION;
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;
742 /* Legitimize both operands of the PLUS. */
743 base = machopic_indirect_data_reference (XEXP (orig, 0), reg);
744 orig = machopic_indirect_data_reference (XEXP (orig, 1),
745 (base == reg ? 0 : reg));
746 if (MACHOPIC_INDIRECT && (GET_CODE (orig) == CONST_INT))
747 result = plus_constant (Pmode, base, INTVAL (orig));
748 else
749 result = gen_rtx_PLUS (Pmode, base, orig);
751 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
753 if (reg)
755 emit_move_insn (reg, result);
756 result = reg;
758 else
760 result = force_reg (GET_MODE (result), result);
764 return result;
766 return ptr_ref;
769 /* Transform TARGET (a MEM), which is a function call target, to the
770 corresponding symbol_stub if necessary. Return a new MEM. */
773 machopic_indirect_call_target (rtx target)
775 if (! darwin_symbol_stubs)
776 return target;
778 if (GET_CODE (target) != MEM)
779 return target;
781 if (MACHOPIC_INDIRECT
782 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
783 && ! MACHO_SYMBOL_DEFINED_P (XEXP (target, 0)))
785 rtx sym_ref = XEXP (target, 0);
786 const char *stub_name = machopic_indirection_name (sym_ref,
787 /*stub_p=*/true);
788 machine_mode mode = GET_MODE (sym_ref);
790 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
791 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
792 SYMBOL_REF_FLAGS (XEXP (target, 0)) |= MACHO_SYMBOL_FLAG_INDIRECTION;
793 MEM_READONLY_P (target) = 1;
794 MEM_NOTRAP_P (target) = 1;
797 return target;
801 machopic_legitimize_pic_address (rtx orig, machine_mode mode, rtx reg)
803 rtx pic_ref = orig;
805 if (! MACHOPIC_INDIRECT)
806 return orig;
808 /* First handle a simple SYMBOL_REF or LABEL_REF */
809 if (GET_CODE (orig) == LABEL_REF
810 || GET_CODE (orig) == SYMBOL_REF)
812 /* addr(foo) = &func+(foo-func) */
813 orig = machopic_indirect_data_reference (orig, reg);
815 if (GET_CODE (orig) == PLUS
816 && GET_CODE (XEXP (orig, 0)) == REG)
818 if (reg == 0)
819 return force_reg (mode, orig);
821 emit_move_insn (reg, orig);
822 return reg;
825 if (GET_CODE (orig) == MEM)
827 if (reg == 0)
829 gcc_assert (!lra_in_progress);
830 reg = gen_reg_rtx (Pmode);
833 #if HAVE_lo_sum
834 if (MACHO_DYNAMIC_NO_PIC_P
835 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
836 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
838 #if defined (TARGET_TOC) /* ppc */
839 rtx temp_reg = (!can_create_pseudo_p ()
840 ? reg :
841 gen_reg_rtx (Pmode));
842 rtx asym = XEXP (orig, 0);
843 rtx mem;
845 emit_insn (gen_macho_high (Pmode, temp_reg, asym));
846 mem = gen_const_mem (GET_MODE (orig),
847 gen_rtx_LO_SUM (Pmode, temp_reg,
848 copy_rtx (asym)));
849 emit_insn (gen_rtx_SET (reg, mem));
850 #else
851 /* Some other CPU -- WriteMe! but right now there are no other
852 platforms that can use dynamic-no-pic */
853 gcc_unreachable ();
854 #endif
855 pic_ref = reg;
857 else
858 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
859 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
861 rtx offset = machopic_gen_offset (XEXP (orig, 0));
862 #if defined (TARGET_TOC) /* i.e., PowerPC */
863 /* Generating a new reg may expose opportunities for
864 common subexpression elimination. */
865 rtx hi_sum_reg = (!can_create_pseudo_p ()
866 ? reg
867 : gen_reg_rtx (Pmode));
868 rtx mem;
869 rtx sum;
871 sum = gen_rtx_HIGH (Pmode, offset);
872 if (! MACHO_DYNAMIC_NO_PIC_P)
873 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
875 emit_insn (gen_rtx_SET (hi_sum_reg, sum));
877 mem = gen_const_mem (GET_MODE (orig),
878 gen_rtx_LO_SUM (Pmode,
879 hi_sum_reg,
880 copy_rtx (offset)));
881 rtx_insn *insn = emit_insn (gen_rtx_SET (reg, mem));
882 set_unique_reg_note (insn, REG_EQUAL, pic_ref);
884 pic_ref = reg;
885 #else
886 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
888 emit_insn (gen_rtx_SET (reg,
889 gen_rtx_HIGH (Pmode,
890 gen_rtx_CONST (Pmode,
891 offset))));
892 emit_insn (gen_rtx_SET (reg,
893 gen_rtx_LO_SUM (Pmode, reg,
894 gen_rtx_CONST (Pmode,
895 copy_rtx (offset)))));
896 pic_ref = gen_rtx_PLUS (Pmode,
897 pic_offset_table_rtx, reg);
898 #endif
900 else
901 #endif /* HAVE_lo_sum */
903 rtx pic = pic_offset_table_rtx;
904 if (GET_CODE (pic) != REG)
906 emit_move_insn (reg, pic);
907 pic = reg;
909 #if 0
910 emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
911 #endif
913 if (lra_in_progress)
914 df_set_regs_ever_live (REGNO (pic), true);
915 pic_ref = gen_rtx_PLUS (Pmode, pic,
916 machopic_gen_offset (XEXP (orig, 0)));
919 #if !defined (TARGET_TOC)
920 emit_move_insn (reg, pic_ref);
921 pic_ref = gen_const_mem (GET_MODE (orig), reg);
922 #endif
924 else
927 #if HAVE_lo_sum
928 if (GET_CODE (orig) == SYMBOL_REF
929 || GET_CODE (orig) == LABEL_REF)
931 rtx offset = machopic_gen_offset (orig);
932 #if defined (TARGET_TOC) /* i.e., PowerPC */
933 rtx hi_sum_reg;
935 if (reg == 0)
937 gcc_assert (!lra_in_progress);
938 reg = gen_reg_rtx (Pmode);
941 hi_sum_reg = reg;
943 emit_insn (gen_rtx_SET (hi_sum_reg,
944 (MACHO_DYNAMIC_NO_PIC_P)
945 ? gen_rtx_HIGH (Pmode, offset)
946 : gen_rtx_PLUS (Pmode,
947 pic_offset_table_rtx,
948 gen_rtx_HIGH (Pmode,
949 offset))));
950 emit_insn (gen_rtx_SET (reg,
951 gen_rtx_LO_SUM (Pmode,
952 hi_sum_reg,
953 copy_rtx (offset))));
954 pic_ref = reg;
955 #else
956 emit_insn (gen_rtx_SET (reg, gen_rtx_HIGH (Pmode, offset)));
957 emit_insn (gen_rtx_SET (reg,
958 gen_rtx_LO_SUM (Pmode, reg,
959 copy_rtx (offset))));
960 pic_ref = gen_rtx_PLUS (Pmode,
961 pic_offset_table_rtx, reg);
962 #endif
964 else
965 #endif /* HAVE_lo_sum */
967 if (REG_P (orig)
968 || GET_CODE (orig) == SUBREG)
970 return orig;
972 else
974 rtx pic = pic_offset_table_rtx;
975 if (GET_CODE (pic) != REG)
977 emit_move_insn (reg, pic);
978 pic = reg;
980 #if 0
981 emit_use (pic_offset_table_rtx);
982 #endif
983 if (lra_in_progress)
984 df_set_regs_ever_live (REGNO (pic), true);
985 pic_ref = gen_rtx_PLUS (Pmode,
986 pic,
987 machopic_gen_offset (orig));
992 if (GET_CODE (pic_ref) != REG)
994 if (reg != 0)
996 emit_move_insn (reg, pic_ref);
997 return reg;
999 else
1001 return force_reg (mode, pic_ref);
1004 else
1006 return pic_ref;
1009 else if (GET_CODE (orig) == PLUS
1010 && (GET_CODE (XEXP (orig, 0)) == MEM
1011 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
1012 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
1013 && XEXP (orig, 0) != pic_offset_table_rtx
1014 && GET_CODE (XEXP (orig, 1)) != REG)
1017 rtx base;
1018 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
1020 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1021 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
1022 Pmode, (base == reg ? 0 : reg));
1023 if (GET_CODE (orig) == CONST_INT)
1025 pic_ref = plus_constant (Pmode, base, INTVAL (orig));
1026 is_complex = 1;
1028 else
1029 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
1031 if (reg && is_complex)
1033 emit_move_insn (reg, pic_ref);
1034 pic_ref = reg;
1036 /* Likewise, should we set special REG_NOTEs here? */
1038 else if (GET_CODE (orig) == CONST)
1040 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1042 else if (GET_CODE (orig) == MEM
1043 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
1045 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
1046 addr = replace_equiv_address (orig, addr);
1047 emit_move_insn (reg, addr);
1048 pic_ref = reg;
1051 return pic_ref;
1054 /* Callbacks to output the stub or non-lazy pointers.
1055 Each works on the item in *SLOT,if it has been used.
1056 DATA is the FILE* for assembly output.
1057 Called from htab_traverses, invoked from machopic_finish(). */
1060 machopic_output_data_section_indirection (machopic_indirection **slot,
1061 FILE *asm_out_file)
1063 machopic_indirection *p = *slot;
1065 if (!p->used || !p->nlsp_in_data_section)
1066 return 1;
1068 rtx symbol = p->symbol;
1069 /* The original symbol name. */
1070 const char *sym_name = XSTR (symbol, 0);
1071 /* The name of the indirection symbol. */
1072 const char *ptr_name = p->ptr_name;
1074 switch_to_section (data_section);
1075 assemble_align (GET_MODE_ALIGNMENT (Pmode));
1076 assemble_label (asm_out_file, ptr_name);
1077 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
1078 GET_MODE_SIZE (Pmode),
1079 GET_MODE_ALIGNMENT (Pmode), 1);
1081 return 1;
1085 machopic_output_stub_indirection (machopic_indirection **slot,
1086 FILE *asm_out_file)
1088 machopic_indirection *p = *slot;
1090 if (!p->used || !p->stub_p)
1091 return 1;
1093 rtx symbol = p->symbol;
1094 /* The original symbol name. */
1095 const char *sym_name = XSTR (symbol, 0);
1096 /* The name of the stub symbol. */
1097 const char *ptr_name = p->ptr_name;
1099 tree id = maybe_get_identifier (sym_name);
1100 if (id)
1102 tree id_orig = id;
1104 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
1105 id = TREE_CHAIN (id);
1106 if (id != id_orig)
1107 sym_name = IDENTIFIER_POINTER (id);
1110 char *sym = XALLOCAVEC (char, strlen (sym_name) + 2);
1111 if (sym_name[0] == '*' || sym_name[0] == '&')
1112 strcpy (sym, sym_name + 1);
1113 else if (sym_name[0] == '-' || sym_name[0] == '+')
1114 strcpy (sym, sym_name);
1115 else
1116 sprintf (sym, "%s%s", user_label_prefix, sym_name);
1118 char *stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
1119 if (ptr_name[0] == '*' || ptr_name[0] == '&')
1120 strcpy (stub, ptr_name + 1);
1121 else
1122 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
1124 machopic_output_stub (asm_out_file, sym, stub);
1126 return 1;
1130 machopic_output_indirection (machopic_indirection **slot, FILE *asm_out_file)
1132 machopic_indirection *p = *slot;
1134 if (!p->used || p->stub_p || p->nlsp_in_data_section)
1135 return 1;
1137 rtx symbol = p->symbol;
1138 /* The original symbol name. */
1139 const char *sym_name = XSTR (symbol, 0);
1140 /* The nonlazy-stub symbol name. */
1141 const char *ptr_name = p->ptr_name;
1143 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
1145 /* Mach-O symbols are passed around in code through indirect references and
1146 the original symbol_ref hasn't passed through the generic handling and
1147 reference-catching in output_operand, so we need to manually mark weak
1148 references as such. */
1150 if (SYMBOL_REF_WEAK (symbol))
1152 tree decl = SYMBOL_REF_DECL (symbol);
1153 gcc_checking_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_FLAG_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 rtx init = const0_rtx;
1183 if (MACHO_SYMBOL_STATIC_P (symbol) && 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);
1189 return 1;
1192 static void
1193 machopic_finish (FILE *asm_out_file)
1195 if (!machopic_indirections)
1196 return;
1198 /* First output an symbol indirections that have been placed into .data
1199 (we don't expect these now). */
1200 machopic_indirections->traverse_noresize
1201 <FILE *, machopic_output_data_section_indirection> (asm_out_file);
1203 machopic_indirections->traverse_noresize
1204 <FILE *, machopic_output_stub_indirection> (asm_out_file);
1206 machopic_indirections->traverse_noresize
1207 <FILE *, machopic_output_indirection> (asm_out_file);
1211 machopic_operand_p (rtx op)
1213 if (MACHOPIC_JUST_INDIRECT)
1214 return (GET_CODE (op) == SYMBOL_REF
1215 && machopic_symbol_defined_p (op));
1216 else
1217 return (GET_CODE (op) == CONST
1218 && GET_CODE (XEXP (op, 0)) == UNSPEC
1219 && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1222 /* This function:
1223 computes and caches a series of flags that characterise the symbol's
1224 properties that affect Mach-O code gen (including accidental cases
1225 from older toolchains).
1227 TODO:
1228 Here we also need to do enough analysis to determine if a symbol's
1229 name needs to be made linker-visible. This is more tricky - since
1230 it depends on whether we've previously seen a global weak definition
1231 in the same section.
1234 void
1235 darwin_encode_section_info (tree decl, rtx rtl, int first)
1237 /* Careful not to prod global register variables. */
1238 if (!MEM_P (rtl))
1239 return;
1241 /* Do the standard encoding things first; this sets:
1242 SYMBOL_FLAG_FUNCTION,
1243 SYMBOL_FLAG_LOCAL, (binds_local_p)
1244 TLS_MODEL, SYMBOL_FLAG_SMALL
1245 SYMBOL_FLAG_EXTERNAL. */
1246 default_encode_section_info (decl, rtl, first);
1248 if (! VAR_OR_FUNCTION_DECL_P (decl))
1249 return;
1251 rtx sym_ref = XEXP (rtl, 0);
1252 if (VAR_P (decl))
1253 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1255 /* Only really common if there's no initialiser. */
1256 bool really_common_p = (DECL_COMMON (decl)
1257 && (DECL_INITIAL (decl) == NULL
1258 || (!in_lto_p
1259 && DECL_INITIAL (decl) == error_mark_node)));
1261 /* For Darwin, if we have specified visibility and it's not the default
1262 that's counted 'hidden'. */
1263 if (DECL_VISIBILITY_SPECIFIED (decl)
1264 && DECL_VISIBILITY (decl) != VISIBILITY_DEFAULT)
1265 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_HIDDEN_VIS;
1267 if (!DECL_EXTERNAL (decl)
1268 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1269 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1270 && ((TREE_STATIC (decl)
1271 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1272 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1273 && DECL_INITIAL (decl) != error_mark_node)))
1274 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1276 if (! TREE_PUBLIC (decl))
1277 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_STATIC;
1279 /* Short cut check for Darwin 'must indirect' rules. */
1280 if (really_common_p
1281 || (DECL_WEAK (decl) && ! MACHO_SYMBOL_HIDDEN_VIS_P (sym_ref))
1282 || lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
1283 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_MUST_INDIRECT;
1285 #if DARWIN_PPC
1286 /* Objective C V2 (m64) IVAR offset refs from Apple GCC-4.x have an
1287 indirection for m64 code on PPC. Historically, these indirections
1288 also appear in the .data section. */
1289 tree o2meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1290 o2meta = o2meta ? TREE_VALUE (o2meta) : NULL_TREE;
1292 if (o2meta && strncmp (IDENTIFIER_POINTER (o2meta), "V2_IVRF",7) == 0)
1293 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_MUST_INDIRECT;
1294 #endif
1297 void
1298 darwin_mark_decl_preserved (const char *name)
1300 /* Actually we shouldn't mark any local symbol this way, but for now
1301 this only happens with ObjC meta-data. */
1302 if (darwin_label_is_anonymous_local_objc_name (name))
1303 return;
1305 fprintf (asm_out_file, "\t.no_dead_strip ");
1306 assemble_name (asm_out_file, name);
1307 fputc ('\n', asm_out_file);
1310 static section *
1311 darwin_rodata_section (int use_coal, bool zsize, int reloc)
1313 return (use_coal
1314 ? darwin_sections[const_coal_section]
1315 : (zsize ? darwin_sections[zobj_const_section]
1316 : reloc ? darwin_sections[const_data_section]
1317 : darwin_sections[const_section]));
1320 static section *
1321 darwin_mergeable_string_section (tree exp,
1322 unsigned HOST_WIDE_INT align)
1324 /* Darwin's ld expects to see non-writable string literals in the .cstring
1325 section. Later versions of ld check and complain when CFStrings are
1326 enabled. Therefore we shall force the strings into .cstring since we
1327 don't support writable ones anyway. */
1328 if ((darwin_constant_cfstrings || flag_merge_constants)
1329 && TREE_CODE (exp) == STRING_CST
1330 && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1331 && align <= 256
1332 && (int_size_in_bytes (TREE_TYPE (exp))
1333 == TREE_STRING_LENGTH (exp))
1334 && ((size_t) TREE_STRING_LENGTH (exp)
1335 == strlen (TREE_STRING_POINTER (exp)) + 1))
1336 return darwin_sections[cstring_section];
1338 if (DARWIN_SECTION_ANCHORS && flag_section_anchors
1339 && TREE_CODE (exp) == STRING_CST
1340 && TREE_STRING_LENGTH (exp) == 0)
1341 return darwin_sections[zobj_const_section];
1343 return readonly_data_section;
1346 #ifndef HAVE_GAS_LITERAL16
1347 #define HAVE_GAS_LITERAL16 0
1348 #endif
1350 static section *
1351 darwin_mergeable_constant_section (tree exp,
1352 unsigned HOST_WIDE_INT align,
1353 bool zsize)
1355 if (zsize)
1356 return darwin_sections[zobj_const_section];
1358 machine_mode mode = DECL_MODE (exp);
1359 if (!flag_merge_constants
1360 || mode == VOIDmode
1361 || mode == BLKmode
1362 || align < 8
1363 || align > 256
1364 || (align & (align -1)) != 0)
1365 return readonly_data_section;
1367 /* This will ICE if the mode is not a constant size, but that is reasonable,
1368 since one cannot put a variable-sized thing into a constant section, we
1369 shouldn't be trying. */
1370 const unsigned int modesize = GET_MODE_BITSIZE (mode).to_constant ();
1372 if (modesize > align)
1373 return readonly_data_section;
1375 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1377 if (TREE_CODE (size) != INTEGER_CST)
1378 return readonly_data_section;
1380 unsigned isize = TREE_INT_CST_LOW (size);
1381 if (isize == 4)
1382 return darwin_sections[literal4_section];
1383 else if (isize == 8)
1384 return darwin_sections[literal8_section];
1385 else if (HAVE_GAS_LITERAL16
1386 && TARGET_64BIT
1387 && isize == 16)
1388 return darwin_sections[literal16_section];
1390 return readonly_data_section;
1393 section *
1394 darwin_tm_clone_table_section (void)
1396 return get_named_section (NULL,
1397 "__DATA,__tm_clone_table,regular,no_dead_strip",
1402 machopic_reloc_rw_mask (void)
1404 return MACHOPIC_INDIRECT ? 3 : 0;
1407 /* We have to deal with ObjC/C++ metadata section placement in the common
1408 code, since it will also be called from LTO.
1410 Return metadata attributes, if present (searching for ABI=2 first)
1411 Return NULL_TREE if no such attributes are found. */
1413 static tree
1414 is_objc_metadata (tree decl)
1416 if (DECL_P (decl)
1417 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1418 && DECL_ATTRIBUTES (decl))
1420 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1421 if (meta)
1422 return meta;
1423 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1424 if (meta)
1425 return meta;
1427 return NULL_TREE;
1430 static int classes_seen;
1431 static int objc_metadata_seen;
1433 /* Return the section required for Objective C ABI 2 metadata. */
1434 static section *
1435 darwin_objc2_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1437 const char *p;
1438 tree ident = TREE_VALUE (meta);
1439 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1440 p = IDENTIFIER_POINTER (ident);
1442 gcc_checking_assert (flag_next_runtime >= 1 && flag_objc_abi == 2);
1444 objc_metadata_seen = 1;
1446 if (base == data_section)
1447 base = darwin_sections[objc2_metadata_section];
1449 /* Most of the OBJC2 META-data end up in the base section, so check it
1450 first. */
1451 if (!strncmp (p, "V2_BASE", 7))
1452 return base;
1453 else if (!strncmp (p, "V2_CNAM", 7))
1454 return darwin_sections[objc2_class_names_section];
1455 else if (!strncmp (p, "V2_MNAM", 7))
1456 return darwin_sections[objc2_method_names_section];
1457 else if (!strncmp (p, "V2_MTYP", 7))
1458 return darwin_sections[objc2_method_types_section];
1459 else if (!strncmp (p, "V2_STRG", 7))
1460 return darwin_sections[cstring_section];
1462 else if (!strncmp (p, "G2_META", 7) || !strncmp (p, "G2_CLAS", 7))
1463 return darwin_sections[objc2_classdefs_section];
1464 else if (!strncmp (p, "V2_PCOL", 7))
1465 return ld_uses_coal_sects ? darwin_sections[data_coal_section]
1466 : darwin_sections[objc2_data_section];
1467 else if (!strncmp (p, "V2_MREF", 7))
1468 return darwin_sections[objc2_message_refs_section];
1469 else if (!strncmp (p, "V2_CLRF", 7))
1470 return darwin_sections[objc2_classrefs_section];
1471 else if (!strncmp (p, "V2_SURF", 7))
1472 return darwin_sections[objc2_super_classrefs_section];
1473 else if (!strncmp (p, "V2_NLCL", 7))
1474 return darwin_sections[objc2_nonlazy_class_section];
1475 else if (!strncmp (p, "V2_CLAB", 7))
1477 classes_seen = 1;
1478 return darwin_sections[objc2_classlist_section];
1480 else if (!strncmp (p, "V2_SRFS", 7))
1481 return darwin_sections[objc2_selector_refs_section];
1482 else if (!strncmp (p, "V2_NLCA", 7))
1483 return darwin_sections[objc2_nonlazy_category_section];
1484 else if (!strncmp (p, "V2_CALA", 7))
1485 return darwin_sections[objc2_categorylist_section];
1487 else if (!strncmp (p, "V2_PLST", 7))
1488 return darwin_sections[objc2_protocollist_section];
1489 else if (!strncmp (p, "V2_PRFS", 7))
1490 return darwin_sections[objc2_protocolrefs_section];
1492 else if (!strncmp (p, "V2_INFO", 7))
1493 return darwin_sections[objc2_image_info_section];
1495 else if (!strncmp (p, "V2_EHTY", 7))
1496 return ld_uses_coal_sects ? darwin_sections[data_coal_section]
1497 : data_section;
1499 else if (!strncmp (p, "V2_CSTR", 7))
1500 return darwin_sections[objc2_constant_string_object_section];
1502 else if (!strncmp (p, "V2_IVRF", 7))
1503 return darwin_sections[objc2_ivar_section];
1505 /* Not recognized, default. */
1506 return base;
1509 /* Return the section required for Objective C ABI 0/1 metadata. */
1510 static section *
1511 darwin_objc1_section (tree decl ATTRIBUTE_UNUSED, tree meta, section * base)
1513 const char *p;
1514 tree ident = TREE_VALUE (meta);
1515 gcc_assert (TREE_CODE (ident) == IDENTIFIER_NODE);
1516 p = IDENTIFIER_POINTER (ident);
1518 gcc_checking_assert (flag_next_runtime >= 1 && flag_objc_abi < 2);
1520 objc_metadata_seen = 1;
1522 /* String sections first, cos there are lots of strings. */
1523 if (!strncmp (p, "V1_STRG", 7))
1524 return darwin_sections[cstring_section];
1525 else if (!strncmp (p, "V1_CLSN", 7))
1526 return darwin_sections[objc_class_names_section];
1527 else if (!strncmp (p, "V1_METN", 7))
1528 return darwin_sections[objc_meth_var_names_section];
1529 else if (!strncmp (p, "V1_METT", 7))
1530 return darwin_sections[objc_meth_var_types_section];
1532 else if (!strncmp (p, "V1_CLAS", 7))
1534 classes_seen = 1;
1535 return darwin_sections[objc_class_section];
1537 else if (!strncmp (p, "V1_META", 7))
1538 return darwin_sections[objc_meta_class_section];
1539 else if (!strncmp (p, "V1_CATG", 7))
1540 return darwin_sections[objc_category_section];
1541 else if (!strncmp (p, "V1_PROT", 7))
1542 return darwin_sections[objc_protocol_section];
1544 else if (!strncmp (p, "V1_CLCV", 7))
1545 return darwin_sections[objc_class_vars_section];
1546 else if (!strncmp (p, "V1_CLIV", 7))
1547 return darwin_sections[objc_instance_vars_section];
1549 else if (!strncmp (p, "V1_CLCM", 7))
1550 return darwin_sections[objc_cls_meth_section];
1551 else if (!strncmp (p, "V1_CLIM", 7))
1552 return darwin_sections[objc_inst_meth_section];
1553 else if (!strncmp (p, "V1_CACM", 7))
1554 return darwin_sections[objc_cat_cls_meth_section];
1555 else if (!strncmp (p, "V1_CAIM", 7))
1556 return darwin_sections[objc_cat_inst_meth_section];
1557 else if (!strncmp (p, "V1_PNSM", 7))
1558 return darwin_sections[objc_cat_inst_meth_section];
1559 else if (!strncmp (p, "V1_PCLM", 7))
1560 return darwin_sections[objc_cat_cls_meth_section];
1562 else if (!strncmp (p, "V1_CLPR", 7))
1563 return darwin_sections[objc_cat_cls_meth_section];
1564 else if (!strncmp (p, "V1_CAPR", 7))
1565 return darwin_sections[objc_category_section]; /* ??? CHECK me. */
1567 else if (!strncmp (p, "V1_PRFS", 7))
1568 return darwin_sections[objc_cat_cls_meth_section];
1569 else if (!strncmp (p, "V1_CLRF", 7))
1570 return darwin_sections[objc_cls_refs_section];
1571 else if (!strncmp (p, "V1_SRFS", 7))
1572 return darwin_sections[objc_selector_refs_section];
1574 else if (!strncmp (p, "V1_MODU", 7))
1575 return darwin_sections[objc_module_info_section];
1576 else if (!strncmp (p, "V1_SYMT", 7))
1577 return darwin_sections[objc_symbols_section];
1578 else if (!strncmp (p, "V1_INFO", 7))
1579 return darwin_sections[objc_image_info_section];
1581 else if (!strncmp (p, "V1_PLST", 7))
1582 return darwin_sections[objc1_prop_list_section];
1583 else if (!strncmp (p, "V1_PEXT", 7))
1584 return darwin_sections[objc1_protocol_ext_section];
1585 else if (!strncmp (p, "V1_CEXT", 7))
1586 return darwin_sections[objc1_class_ext_section];
1588 else if (!strncmp (p, "V2_CSTR", 7))
1589 return darwin_sections[objc_constant_string_object_section];
1591 return base;
1594 section *
1595 machopic_select_section (tree decl,
1596 int reloc,
1597 unsigned HOST_WIDE_INT align)
1599 bool zsize, one, weak, use_coal, ro;
1600 section *base_section = NULL;
1602 weak = (DECL_P (decl)
1603 && DECL_WEAK (decl)
1604 && !lookup_attribute ("weak_import", DECL_ATTRIBUTES (decl)));
1606 /* Darwin pads zero-sized objects with at least one byte, so that the ld64
1607 atom model is preserved (objects must have distinct regions starting with
1608 a unique linker-visible symbol).
1609 In order to support section anchors, we need to move objects with zero
1610 size into sections which are marked as "no section anchors"; the padded
1611 objects, obviously, have real sizes that differ from their DECL sizes. */
1612 zsize = DARWIN_SECTION_ANCHORS && flag_section_anchors;
1614 /* In the streaming of LTO symbol data, we might have a situation where the
1615 var is incomplete or layout not finished (DECL_SIZE_UNIT is NULL_TREE).
1616 We cannot tell if it is zero-sized then, but we can get the section
1617 category correct so that nm reports the right kind of section
1618 (e.g. BSS c.f. data). */
1619 zsize = (zsize
1620 && DECL_P (decl)
1621 && (TREE_CODE (decl) == VAR_DECL || TREE_CODE (decl) == CONST_DECL)
1622 && DECL_SIZE_UNIT (decl)
1623 && tree_to_uhwi (DECL_SIZE_UNIT (decl)) == 0);
1625 one = DECL_P (decl)
1626 && TREE_CODE (decl) == VAR_DECL
1627 && DECL_COMDAT_GROUP (decl);
1629 use_coal = (weak || one) && ld_uses_coal_sects;
1631 ro = TREE_READONLY (decl) || TREE_CONSTANT (decl) ;
1633 switch (categorize_decl_for_section (decl, reloc))
1635 case SECCAT_TEXT:
1636 gcc_unreachable ();
1637 break;
1639 case SECCAT_RODATA:
1640 case SECCAT_SRODATA:
1641 base_section = darwin_rodata_section (use_coal, zsize, reloc);
1642 break;
1644 case SECCAT_RODATA_MERGE_STR:
1645 base_section = darwin_mergeable_string_section (decl, align);
1646 break;
1648 case SECCAT_RODATA_MERGE_STR_INIT:
1649 base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1650 break;
1652 case SECCAT_RODATA_MERGE_CONST:
1653 base_section = darwin_mergeable_constant_section (decl, align, zsize);
1654 break;
1656 case SECCAT_DATA:
1657 case SECCAT_DATA_REL:
1658 case SECCAT_DATA_REL_LOCAL:
1659 case SECCAT_DATA_REL_RO:
1660 case SECCAT_DATA_REL_RO_LOCAL:
1661 case SECCAT_SDATA:
1662 case SECCAT_TDATA:
1663 if (use_coal)
1665 if (ro)
1666 base_section = darwin_sections[const_data_coal_section];
1667 else
1668 base_section = darwin_sections[data_coal_section];
1670 else if (zsize)
1672 /* If we're doing section anchors, then punt zero-sized objects into
1673 their own sections so that they don't interfere with offset
1674 computation for the remaining vars. */
1675 if (ro)
1676 base_section = darwin_sections[zobj_const_data_section];
1677 else
1678 base_section = darwin_sections[zobj_data_section];
1680 else if (ro)
1681 base_section = darwin_sections[const_data_section];
1682 else
1683 base_section = data_section;
1684 break;
1685 case SECCAT_BSS:
1686 case SECCAT_SBSS:
1687 case SECCAT_TBSS:
1688 if (use_coal)
1689 base_section = darwin_sections[data_coal_section];
1690 else
1692 if (!TREE_PUBLIC (decl))
1693 base_section = lcomm_section;
1694 else if (bss_noswitch_section)
1695 base_section = bss_noswitch_section;
1696 else
1697 base_section = data_section;
1699 break;
1701 default:
1702 gcc_unreachable ();
1705 /* Darwin weird special cases.
1706 a) OBJC Meta-data. */
1707 if (DECL_P (decl)
1708 && (TREE_CODE (decl) == VAR_DECL
1709 || TREE_CODE (decl) == CONST_DECL)
1710 && DECL_ATTRIBUTES (decl))
1712 tree meta = lookup_attribute ("OBJC2META", DECL_ATTRIBUTES (decl));
1713 if (meta)
1714 return darwin_objc2_section (decl, meta, base_section);
1715 meta = lookup_attribute ("OBJC1META", DECL_ATTRIBUTES (decl));
1716 if (meta)
1717 return darwin_objc1_section (decl, meta, base_section);
1718 meta = lookup_attribute ("OBJC1METG", DECL_ATTRIBUTES (decl));
1719 if (meta)
1720 return base_section; /* GNU runtime is happy with it all in one pot. */
1723 /* b) Constant string objects. */
1724 if (TREE_CODE (decl) == CONSTRUCTOR
1725 && TREE_TYPE (decl)
1726 && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1727 && TYPE_NAME (TREE_TYPE (decl)))
1729 tree name = TYPE_NAME (TREE_TYPE (decl));
1730 if (TREE_CODE (name) == TYPE_DECL)
1731 name = DECL_NAME (name);
1733 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1735 if (flag_next_runtime)
1737 if (flag_objc_abi == 2)
1738 return darwin_sections[objc2_constant_string_object_section];
1739 else
1740 return darwin_sections[objc_constant_string_object_section];
1742 else
1743 return darwin_sections[objc_string_object_section];
1745 else if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_CFString"))
1746 return darwin_sections[cfstring_constant_object_section];
1747 else
1748 return base_section;
1750 else if (flag_next_runtime
1751 && VAR_P (decl)
1752 && DECL_NAME (decl)
1753 && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1754 && IDENTIFIER_POINTER (DECL_NAME (decl))
1755 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1756 /* c) legacy meta-data selection was deprecated at 4.6, removed now. */
1757 gcc_unreachable ();
1759 return base_section;
1762 /* This can be called with address expressions as "rtx".
1763 They must go in "const". */
1765 section *
1766 machopic_select_rtx_section (machine_mode mode, rtx x,
1767 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1769 if (known_eq (GET_MODE_SIZE (mode), 8)
1770 && (GET_CODE (x) == CONST_INT
1771 || GET_CODE (x) == CONST_WIDE_INT
1772 || GET_CODE (x) == CONST_DOUBLE))
1773 return darwin_sections[literal8_section];
1774 else if (known_eq (GET_MODE_SIZE (mode), 4)
1775 && (GET_CODE (x) == CONST_INT
1776 || GET_CODE (x) == CONST_WIDE_INT
1777 || GET_CODE (x) == CONST_DOUBLE))
1778 return darwin_sections[literal4_section];
1779 else if (HAVE_GAS_LITERAL16
1780 && TARGET_64BIT
1781 && known_eq (GET_MODE_SIZE (mode), 16)
1782 && (GET_CODE (x) == CONST_INT
1783 || GET_CODE (x) == CONST_WIDE_INT
1784 || GET_CODE (x) == CONST_DOUBLE
1785 || GET_CODE (x) == CONST_VECTOR))
1786 return darwin_sections[literal16_section];
1787 else if (MACHOPIC_INDIRECT
1788 && (GET_CODE (x) == SYMBOL_REF
1789 || GET_CODE (x) == CONST
1790 || GET_CODE (x) == LABEL_REF))
1791 return darwin_sections[const_data_section];
1792 else
1793 return darwin_sections[const_section];
1796 void
1797 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1799 cdtor_record new_elt = {symbol, priority, vec_safe_length (ctors)};
1801 vec_safe_push (ctors, new_elt);
1803 if (! MACHOPIC_INDIRECT)
1804 fprintf (asm_out_file, ".reference .constructors_used\n");
1807 void
1808 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1810 cdtor_record new_elt = {symbol, priority, vec_safe_length (dtors)};
1812 vec_safe_push (dtors, new_elt);
1814 if (! MACHOPIC_INDIRECT)
1815 fprintf (asm_out_file, ".reference .destructors_used\n");
1818 static int
1819 sort_cdtor_records (const void * a, const void * b)
1821 const cdtor_record *cda = (const cdtor_record *)a;
1822 const cdtor_record *cdb = (const cdtor_record *)b;
1823 if (cda->priority > cdb->priority)
1824 return 1;
1825 if (cda->priority < cdb->priority)
1826 return -1;
1827 if (cda->position > cdb->position)
1828 return 1;
1829 if (cda->position < cdb->position)
1830 return -1;
1831 return 0;
1834 static void
1835 finalize_ctors ()
1837 unsigned int i;
1838 cdtor_record *elt;
1840 if (MACHOPIC_INDIRECT)
1841 switch_to_section (darwin_sections[mod_init_section]);
1842 else
1843 switch_to_section (darwin_sections[constructor_section]);
1845 if (vec_safe_length (ctors) > 1)
1846 ctors->qsort (sort_cdtor_records);
1847 FOR_EACH_VEC_SAFE_ELT (ctors, i, elt)
1849 assemble_align (POINTER_SIZE);
1850 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1854 static void
1855 finalize_dtors ()
1857 unsigned int i;
1858 cdtor_record *elt;
1860 if (MACHOPIC_INDIRECT)
1861 switch_to_section (darwin_sections[mod_term_section]);
1862 else
1863 switch_to_section (darwin_sections[destructor_section]);
1865 if (vec_safe_length (dtors) > 1)
1866 dtors->qsort (sort_cdtor_records);
1867 FOR_EACH_VEC_SAFE_ELT (dtors, i, elt)
1869 assemble_align (POINTER_SIZE);
1870 assemble_integer (elt->symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1874 void
1875 darwin_globalize_label (FILE *stream, const char *name)
1877 if (!!strncmp (name, "_OBJC_", 6))
1878 default_globalize_label (stream, name);
1879 /* We have some Objective C cases that need to be global, but only on newer
1880 OS versions. */
1881 if (flag_objc_abi < 2 || flag_next_runtime < 100700)
1882 return;
1883 if (!strncmp (name+6, "LabelPro", 8))
1884 default_globalize_label (stream, name);
1885 if (!strncmp (name+6, "Protocol_", 9))
1886 default_globalize_label (stream, name);
1889 /* This routine returns non-zero if 'name' starts with the special objective-c
1890 anonymous file-scope static name. It accommodates c++'s mangling of such
1891 symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit). */
1894 darwin_label_is_anonymous_local_objc_name (const char *name)
1896 const unsigned char *p = (const unsigned char *) name;
1897 if (*p != '_')
1898 return 0;
1899 if (p[1] == 'Z' && p[2] == 'L')
1901 p += 3;
1902 while (*p >= '0' && *p <= '9')
1903 p++;
1905 if (strncmp ((const char *)p, "_OBJC_", 6) != 0)
1906 return false;
1908 /* We need some of the objective c meta-data symbols to be visible to the
1909 linker (when the target OS version is newer). FIXME: this is horrible,
1910 we need a better mechanism. */
1912 if (flag_objc_abi < 2 || flag_next_runtime < 100700)
1913 return true;
1915 p += 6;
1916 if (!strncmp ((const char *)p, "ClassRef", 8))
1917 return false;
1918 else if (!strncmp ((const char *)p, "SelRef", 6))
1919 return false;
1920 else if (!strncmp ((const char *)p, "Category", 8))
1922 if (p[8] == '_' || p[8] == 'I' || p[8] == 'P' || p[8] == 'C' )
1923 return false;
1924 return true;
1926 else if (!strncmp ((const char *)p, "ClassMethods", 12))
1927 return false;
1928 else if (!strncmp ((const char *)p, "Instance", 8))
1930 if (p[8] == 'I' || p[8] == 'M')
1931 return false;
1932 return true;
1934 else if (!strncmp ((const char *)p, "CLASS_RO", 8))
1935 return false;
1936 else if (!strncmp ((const char *)p, "METACLASS_RO", 12))
1937 return false;
1938 else if (!strncmp ((const char *)p, "Protocol", 8))
1940 if (p[8] == '_' || p[8] == 'I' || p[8] == 'P'
1941 || p[8] == 'M' || p[8] == 'C' || p[8] == 'O')
1942 return false;
1943 return true;
1945 else if (!strncmp ((const char *)p, "LabelPro", 8))
1946 return false;
1947 return true;
1950 /* LTO support for Mach-O.
1952 This version uses three mach-o sections to encapsulate the (unlimited
1953 number of) lto sections.
1955 __GNU_LTO, __lto_sections contains the concatented GNU LTO section data.
1956 __GNU_LTO, __section_names contains the GNU LTO section names.
1957 __GNU_LTO, __section_index contains an array of values that index these.
1959 Indexed thus:
1960 <section offset from the start of __GNU_LTO, __lto_sections>,
1961 <section length>
1962 <name offset from the start of __GNU_LTO, __section_names,
1963 <name length>.
1965 At present, for both m32 and m64 mach-o files each of these fields is
1966 represented by a uint32_t. This is because, AFAICT, a mach-o object
1967 cannot exceed 4Gb because the section_64 offset field (see below) is 32bits.
1969 uint32_t offset;
1970 "offset An integer specifying the offset to this section in the file." */
1972 /* Count lto section numbers. */
1973 static unsigned int lto_section_num = 0;
1975 /* A vector of information about LTO sections, at present, we only have
1976 the name. TODO: see if we can get the data length somehow. */
1977 typedef struct GTY (()) darwin_lto_section_e {
1978 const char *sectname;
1979 } darwin_lto_section_e ;
1981 static GTY (()) vec<darwin_lto_section_e, va_gc> *lto_section_names;
1983 /* Section wrapper scheme (used here to wrap the unlimited number of LTO
1984 sections into three Mach-O ones).
1985 NOTE: These names MUST be kept in sync with those in
1986 libiberty/simple-object-mach-o. */
1987 #define LTO_SECTS_SECTION "__wrapper_sects"
1988 #define LTO_NAMES_SECTION "__wrapper_names"
1989 #define LTO_INDEX_SECTION "__wrapper_index"
1991 /* File to temporarily store LTO data. This is appended to asm_out_file
1992 in darwin_end_file. */
1993 static FILE *lto_asm_out_file, *saved_asm_out_file;
1994 static char *lto_asm_out_name;
1995 static enum debug_info_levels saved_debug_info_level;
1997 /* Prepare asm_out_file for LTO output. For darwin, this means hiding
1998 asm_out_file and switching to an alternative output file. */
1999 void
2000 darwin_asm_lto_start (void)
2002 gcc_assert (! saved_asm_out_file);
2003 saved_asm_out_file = asm_out_file;
2004 saved_debug_info_level = debug_info_level;
2005 debug_info_level = DINFO_LEVEL_NONE;
2006 if (! lto_asm_out_name)
2007 lto_asm_out_name = make_temp_file (".lto.s");
2008 lto_asm_out_file = fopen (lto_asm_out_name, "a");
2009 if (lto_asm_out_file == NULL)
2010 fatal_error (input_location,
2011 "failed to open temporary file %s for LTO output",
2012 lto_asm_out_name);
2013 asm_out_file = lto_asm_out_file;
2016 /* Restore asm_out_file. */
2017 void
2018 darwin_asm_lto_end (void)
2020 gcc_assert (saved_asm_out_file);
2021 fclose (lto_asm_out_file);
2022 asm_out_file = saved_asm_out_file;
2023 saved_asm_out_file = NULL;
2024 debug_info_level = saved_debug_info_level;
2027 static void
2028 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2029 tree decl, bool is_for_lto);
2031 /* Called for the TARGET_ASM_NAMED_SECTION hook. */
2033 void
2034 darwin_asm_named_section (const char *name,
2035 unsigned int flags,
2036 tree decl ATTRIBUTE_UNUSED)
2038 /* LTO sections go in a special section that encapsulates the (unlimited)
2039 number of GNU LTO sections within a single mach-o one. */
2040 if (strncmp (name, LTO_SECTION_NAME_PREFIX,
2041 strlen (LTO_SECTION_NAME_PREFIX)) == 0)
2043 darwin_lto_section_e e;
2044 /* We expect certain flags to be set... */
2045 gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2046 == (SECTION_DEBUG | SECTION_NAMED));
2048 /* Switch to our combined section. */
2049 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
2050 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
2051 /* Output a label for the start of this sub-section. */
2052 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# %s\n",
2053 lto_section_num, name);
2054 /* We have to jump through hoops to get the values of the intra-section
2055 offsets... */
2056 fprintf (asm_out_file, "\t.set L$gnu$lto$offs%d,L_GNU_LTO%d-L_GNU_LTO0\n",
2057 lto_section_num, lto_section_num);
2058 fprintf (asm_out_file,
2059 "\t.set L$gnu$lto$size%d,L_GNU_LTO%d-L_GNU_LTO%d\n",
2060 lto_section_num, lto_section_num+1, lto_section_num);
2061 lto_section_num++;
2062 e.sectname = xstrdup (name);
2063 /* Keep the names, we'll need to make a table later.
2064 TODO: check that we do not revisit sections, that would break
2065 the assumption of how this is done. */
2066 if (lto_section_names == NULL)
2067 vec_alloc (lto_section_names, 16);
2068 vec_safe_push (lto_section_names, e);
2070 else if (strncmp (name, "__DWARF,", 8) == 0)
2071 darwin_asm_dwarf_section (name, flags, decl, false);
2072 else if (strncmp (name, "__GNU_DWARF_LTO,", 16) == 0)
2073 darwin_asm_dwarf_section (name, flags, decl, true);
2074 else
2075 fprintf (asm_out_file, "\t.section %s\n", name);
2078 void
2079 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
2081 /* Darwin does not use unique sections. */
2084 /* Handle __attribute__ ((apple_kext_compatibility)).
2085 This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
2086 vtable for classes with this attribute (and their descendants) by not
2087 outputting the new 3.0 nondeleting destructor. This means that such
2088 objects CANNOT be allocated on the stack or as globals UNLESS they have
2089 a completely empty `operator delete'.
2090 Luckily, this fits in with the Darwin kext model.
2092 This attribute also disables gcc3's potential overlaying of derived
2093 class data members on the padding at the end of the base class. */
2095 tree
2096 darwin_handle_kext_attribute (tree *node, tree name,
2097 tree args ATTRIBUTE_UNUSED,
2098 int flags ATTRIBUTE_UNUSED,
2099 bool *no_add_attrs)
2101 /* APPLE KEXT stuff -- only applies with pure static C++ code. */
2102 if (! TARGET_KEXTABI)
2104 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2105 "only when compiling a kext", name);
2107 *no_add_attrs = true;
2109 else if (TREE_CODE (*node) != RECORD_TYPE)
2111 warning (0, "%qE 2.95 vtable-compatibility attribute applies "
2112 "only to C++ classes", name);
2114 *no_add_attrs = true;
2117 return NULL_TREE;
2120 /* Handle a "weak_import" attribute; arguments as in
2121 struct attribute_spec.handler. */
2123 tree
2124 darwin_handle_weak_import_attribute (tree *node, tree name,
2125 tree ARG_UNUSED (args),
2126 int ARG_UNUSED (flags),
2127 bool * no_add_attrs)
2129 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
2131 warning (OPT_Wattributes, "%qE attribute ignored",
2132 name);
2133 *no_add_attrs = true;
2135 else
2136 declare_weak (*node);
2138 return NULL_TREE;
2141 /* Emit a label for an FDE, making it global and/or weak if appropriate.
2142 The third parameter is nonzero if this is for exception handling.
2143 The fourth parameter is nonzero if this is just a placeholder for an
2144 FDE that we are omitting. */
2146 void
2147 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
2149 char *lab ;
2150 char buf[32];
2151 static int invok_count = 0;
2152 static tree last_fun_decl = NULL_TREE;
2154 /* Modern linkers can produce distinct FDEs without compiler support. */
2155 if (! for_eh || ! ld_needs_eh_markers)
2156 return;
2158 /* FIXME: This only works when the eh for all sections of a function are
2159 emitted at the same time. If that changes, we would need to use a lookup
2160 table of some form to determine what to do. Also, we should emit the
2161 unadorned label for the partition containing the public label for a
2162 function. This is of limited use, probably, since we do not currently
2163 enable partitioning. */
2164 strcpy (buf, ".eh");
2165 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
2167 if (decl == last_fun_decl)
2169 invok_count++;
2170 snprintf (buf, 31, "$$part$$%d.eh", invok_count);
2172 else
2174 last_fun_decl = decl;
2175 invok_count = 0;
2179 lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), buf, NULL);
2181 if (TREE_PUBLIC (decl))
2183 targetm.asm_out.globalize_label (file, lab);
2184 if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
2186 fputs ("\t.private_extern ", file);
2187 assemble_name (file, lab);
2188 fputc ('\n', file);
2192 if (DECL_WEAK (decl))
2194 fputs ("\t.weak_definition ", file);
2195 assemble_name (file, lab);
2196 fputc ('\n', file);
2199 assemble_name (file, lab);
2200 if (empty)
2202 fputs (" = 0\n", file);
2204 /* Mark the absolute .eh and .eh1 style labels as needed to
2205 ensure that we don't dead code strip them and keep such
2206 labels from another instantiation point until we can fix this
2207 properly with group comdat support. */
2208 darwin_mark_decl_preserved (lab);
2210 else
2211 fputs (":\n", file);
2213 free (lab);
2216 static GTY(()) unsigned long except_table_label_num;
2218 void
2219 darwin_emit_except_table_label (FILE *file)
2221 char section_start_label[30];
2223 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
2224 except_table_label_num++);
2225 ASM_OUTPUT_LABEL (file, section_start_label);
2228 /* Return, and mark as used, the name of the stub for the mcount function.
2229 Currently, this is only called by X86 code in the expansion of the
2230 FUNCTION_PROFILER macro, when stubs are enabled. */
2232 const char*
2233 machopic_mcount_stub_name (void)
2235 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
2236 const char *name = machopic_indirection_name (symbol, /*stub_p=*/true);
2237 machopic_validate_stub_or_non_lazy_ptr (name);
2238 return name;
2241 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
2243 void
2244 darwin_non_lazy_pcrel (FILE *file, rtx addr)
2246 const char *nlp_name;
2248 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
2250 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
2251 fputs ("\t.long\t", file);
2252 ASM_OUTPUT_LABELREF (file, nlp_name);
2253 fputs ("-.", file);
2256 /* If this is uncommented, details of each allocation will be printed
2257 in the asm right before the actual code. WARNING - this will cause some
2258 test-suite fails (since the printout will contain items that some tests
2259 are not expecting) -- so don't leave it on by default (it bloats the
2260 asm too). */
2261 /*#define DEBUG_DARWIN_MEM_ALLOCATORS*/
2263 /* The first two of these routines are ostensibly just intended to put
2264 names into the asm. However, they are both hijacked in order to ensure
2265 that zero-sized items do not make their way into the output. Consequently,
2266 we also need to make these participate in provisions for dealing with
2267 such items in section anchors. */
2269 /* The implementation of ASM_DECLARE_OBJECT_NAME. */
2270 /* The RTTI data (e.g., __ti4name) is common and public (and static),
2271 but it does need to be referenced via indirect PIC data pointers.
2272 The machopic_define_symbol calls are telling the machopic subsystem
2273 that the name *is* defined in this module, so it doesn't need to
2274 make them indirect. */
2275 void
2276 darwin_asm_declare_object_name (FILE *file,
2277 const char *nam, tree decl)
2279 const char *xname = nam;
2280 unsigned HOST_WIDE_INT size;
2281 bool local_def, weak;
2283 weak = (DECL_P (decl)
2284 && DECL_WEAK (decl)
2285 && !lookup_attribute ("weak_import",
2286 DECL_ATTRIBUTES (decl)));
2288 local_def = DECL_INITIAL (decl) || (TREE_STATIC (decl)
2289 && (!DECL_COMMON (decl)
2290 || !TREE_PUBLIC (decl)));
2292 if (GET_CODE (XEXP (DECL_RTL (decl), 0)) != SYMBOL_REF)
2293 xname = IDENTIFIER_POINTER (DECL_NAME (decl));
2295 if (local_def)
2297 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2298 if (!weak)
2299 machopic_define_symbol (DECL_RTL (decl));
2302 size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2304 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2305 fprintf (file, "# dadon: %s %s (%llu, %u) local %d weak %d"
2306 " stat %d com %d pub %d t-const %d t-ro %d init %lx\n",
2307 xname, (TREE_CODE (decl) == VAR_DECL?"var":"const"),
2308 (unsigned long long)size, DECL_ALIGN (decl), local_def,
2309 DECL_WEAK (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2310 TREE_PUBLIC (decl), TREE_CONSTANT (decl), TREE_READONLY (decl),
2311 (unsigned long)DECL_INITIAL (decl));
2312 #endif
2314 /* Darwin needs help to support local zero-sized objects.
2315 They must be made at least one byte, and the section containing must be
2316 marked as unsuitable for section-anchors (see storage allocators below).
2318 For non-zero objects this output is handled by varasm.c.
2320 if (!size)
2322 unsigned int l2align = 0;
2324 /* The align must be honored, even for zero-sized. */
2325 if (DECL_ALIGN (decl))
2327 l2align = floor_log2 (DECL_ALIGN (decl) / BITS_PER_UNIT);
2328 fprintf (file, "\t.align\t%u\n", l2align);
2331 ASM_OUTPUT_LABEL (file, xname);
2332 size = 1;
2333 fprintf (file, "\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2335 /* Check that we've correctly picked up the zero-sized item and placed it
2336 properly. */
2337 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2338 || (in_section
2339 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2341 else
2342 ASM_OUTPUT_LABEL (file, xname);
2345 /* The implementation of ASM_DECLARE_CONSTANT_NAME. */
2346 void
2347 darwin_asm_declare_constant_name (FILE *file, const char *name,
2348 const_tree exp ATTRIBUTE_UNUSED,
2349 HOST_WIDE_INT size)
2351 assemble_label (file, name);
2352 /* As for other items, we need at least one byte. */
2353 if (!size)
2355 fputs ("\t.space\t1\n", file);
2356 /* Check that we've correctly picked up the zero-sized item and placed it
2357 properly. */
2358 gcc_assert ((!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
2359 || (in_section
2360 && (in_section->common.flags & SECTION_NO_ANCHOR)));
2364 /* Darwin storage allocators.
2366 Zerofill sections are desirable for large blank data since, otherwise, these
2367 data bloat objects (PR33210).
2369 However, section anchors don't work in .zerofill sections (one cannot switch
2370 to a zerofill section). Ergo, for Darwin targets using section anchors we need
2371 to put (at least some) data into 'normal' switchable sections.
2373 Here we set a relatively arbitrary value for the size of an object to trigger
2374 zerofill when section anchors are enabled (anything bigger than a page for
2375 current Darwin implementations). FIXME: there ought to be some objective way
2376 to make this choice.
2378 When section anchor are off this is ignored anyway. */
2380 #define BYTES_ZFILL 4096
2382 /* Emit a chunk of data for items coalesced by the linker. */
2383 static void
2384 darwin_emit_weak_or_comdat (FILE *fp, tree decl, const char *name,
2385 unsigned HOST_WIDE_INT size,
2386 bool use_coal,
2387 unsigned int align)
2389 /* Since the sections used here are coalesced, they will not be eligible
2390 for section anchors, and therefore we don't need to break that out.
2391 CHECKME: for modern linker on PowerPC. */
2392 if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
2393 switch_to_section (use_coal ? darwin_sections[const_data_coal_section]
2394 : darwin_sections[const_data_section]);
2395 else
2396 switch_to_section (use_coal ? darwin_sections[data_coal_section]
2397 : data_section);
2399 /* To be consistent, we'll allow darwin_asm_declare_object_name to assemble
2400 the align info for zero-sized items... but do it here otherwise. */
2401 if (size && align)
2402 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2404 if (TREE_PUBLIC (decl))
2405 darwin_globalize_label (fp, name);
2407 /* ... and we let it deal with outputting one byte of zero for them too. */
2408 darwin_asm_declare_object_name (fp, name, decl);
2409 if (size)
2410 assemble_zeros (size);
2413 /* Emit a chunk of data for ObjC meta-data that got placed in BSS erroneously. */
2414 static void
2415 darwin_emit_objc_zeroed (FILE *fp, tree decl, const char *name,
2416 unsigned HOST_WIDE_INT size,
2417 unsigned int align, tree meta)
2419 section *ocs = data_section;
2421 if (TREE_PURPOSE (meta) == get_identifier("OBJC2META"))
2422 ocs = darwin_objc2_section (decl, meta, ocs);
2423 else
2424 ocs = darwin_objc1_section (decl, meta, ocs);
2426 switch_to_section (ocs);
2428 /* We shall declare that zero-sized meta-data are not valid (yet). */
2429 gcc_assert (size);
2430 fprintf (fp, "\t.align\t%d\n", floor_log2 (align / BITS_PER_UNIT));
2432 /* ... and we let it deal with outputting one byte of zero for them too. */
2433 darwin_asm_declare_object_name (fp, name, decl);
2434 assemble_zeros (size);
2437 /* This routine emits 'local' storage:
2439 When Section Anchors are off this routine emits .zerofill commands in
2440 sections named for their alignment.
2442 When Section Anchors are on, smaller (non-zero-sized) items are placed in
2443 the .static_data section so that the section anchoring system can see them.
2444 Larger items are still placed in .zerofill sections, addressing PR33210.
2445 The routine has no checking - it is all assumed to be done by the caller.
2447 static void
2448 darwin_emit_local_bss (FILE *fp, tree decl, const char *name,
2449 unsigned HOST_WIDE_INT size,
2450 unsigned int l2align)
2452 if (DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2454 /* Put smaller objects in _static_data, where the section anchors system
2455 can get them.
2456 However, if they are zero-sized punt them to yet a different section
2457 (that is not allowed to participate in anchoring). */
2458 if (!size)
2460 fputs ("\t.section\t__DATA,__zobj_bss\n", fp);
2461 in_section = darwin_sections[zobj_bss_section];
2462 size = 1;
2464 else
2466 fputs ("\t.static_data\n", fp);
2467 in_section = darwin_sections[static_data_section];
2470 if (l2align)
2471 fprintf (fp, "\t.align\t%u\n", l2align);
2473 assemble_name (fp, name);
2474 fprintf (fp, ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2476 else
2478 /* When we are on a non-section anchor target (or not using section
2479 anchors, we can get zero-sized items here. However, all we need to
2480 do is to bump them to one byte and the section alignment will take
2481 care of the rest. */
2482 char secnam[64];
2483 snprintf (secnam, 64, "__DATA,__bss");
2484 unsigned int flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2485 in_section = get_section (secnam, flags, NULL);
2486 fprintf (fp, "\t.zerofill %s,", secnam);
2487 assemble_name (fp, name);
2488 if (!size)
2489 size = 1;
2491 if (l2align)
2492 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",%u\n",
2493 size, (unsigned) l2align);
2494 else
2495 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",0\n", size);
2498 (*targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2499 /* This is defined as a file-scope var, so we know to notify machopic. */
2500 machopic_define_symbol (DECL_RTL (decl));
2503 /* Emit a chunk of common. */
2504 static void
2505 darwin_emit_common (FILE *fp, const char *name,
2506 unsigned HOST_WIDE_INT size, unsigned int align)
2508 unsigned HOST_WIDE_INT rounded;
2509 unsigned int l2align;
2511 /* Earlier systems complain if the alignment exceeds the page size.
2512 The magic number is 4096 * 8 - hard-coded for legacy systems. */
2513 if (!emit_aligned_common && (align > 32768UL))
2514 align = 4096UL; /* In units. */
2515 else
2516 align /= BITS_PER_UNIT;
2518 /* Make sure we have a meaningful align. */
2519 if (!align)
2520 align = 1;
2522 /* For earlier toolchains, we need to emit the var as a rounded size to
2523 tell ld the alignment. */
2524 if (size < align)
2525 rounded = align;
2526 else
2527 rounded = (size + (align-1)) & ~(align-1);
2529 l2align = floor_log2 (align);
2530 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2532 in_section = comm_section;
2533 /* We mustn't allow multiple public symbols to share an address when using
2534 the normal OSX toolchain. */
2535 if (!size)
2537 /* Put at least one byte. */
2538 size = 1;
2539 /* This section can no longer participate in section anchoring. */
2540 comm_section->common.flags |= SECTION_NO_ANCHOR;
2543 fputs ("\t.comm\t", fp);
2544 assemble_name (fp, name);
2545 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED,
2546 emit_aligned_common?size:rounded);
2547 if (l2align && emit_aligned_common)
2548 fprintf (fp, ",%u", l2align);
2549 fputs ("\n", fp);
2552 /* Output a var which is all zero - into aligned BSS sections, common, lcomm
2553 or coalescable data sections (for weak or comdat) as appropriate. */
2555 void
2556 darwin_output_aligned_bss (FILE *fp, tree decl, const char *name,
2557 unsigned HOST_WIDE_INT size, unsigned int align)
2559 unsigned int l2align;
2560 bool one, pub, weak;
2561 tree meta;
2563 pub = TREE_PUBLIC (decl);
2564 one = DECL_ONE_ONLY (decl);
2565 weak = (DECL_P (decl)
2566 && DECL_WEAK (decl)
2567 && !lookup_attribute ("weak_import",
2568 DECL_ATTRIBUTES (decl)));
2570 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2571 fprintf (fp, "# albss: %s (%lld,%d) ro %d cst %d stat %d com %d"
2572 " pub %d weak %d one %d init %lx\n",
2573 name, (long long)size, (int)align, TREE_READONLY (decl),
2574 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2575 pub, weak, one, (unsigned long)DECL_INITIAL (decl));
2576 #endif
2578 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2579 before the target has a chance to comment. */
2580 if ((meta = is_objc_metadata (decl)))
2582 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2583 return;
2586 /* Check that any initializer is valid. */
2587 gcc_assert ((DECL_INITIAL (decl) == NULL)
2588 || (DECL_INITIAL (decl) == error_mark_node)
2589 || initializer_zerop (DECL_INITIAL (decl)));
2591 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2592 gcc_assert (!DECL_COMMON (decl));
2594 /* Pick up the correct alignment. */
2595 if (!size || !align)
2596 align = DECL_ALIGN (decl);
2598 l2align = floor_log2 (align / BITS_PER_UNIT);
2599 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2601 last_assemble_variable_decl = decl;
2603 /* We would rather not have to check this here - but it seems that we might
2604 be passed a decl that should be in coalesced space. */
2605 if (one || weak)
2607 /* Weak or COMDAT objects are put in mergeable sections. */
2608 darwin_emit_weak_or_comdat (fp, decl, name, size,
2609 ld_uses_coal_sects, DECL_ALIGN (decl));
2610 return;
2613 /* If this is not public, then emit according to local rules. */
2614 if (!pub)
2616 darwin_emit_local_bss (fp, decl, name, size, l2align);
2617 return;
2620 /* So we have a public symbol. */
2621 if (DARWIN_SECTION_ANCHORS && flag_section_anchors && size < BYTES_ZFILL)
2623 /* Put smaller objects in data, where the section anchors system can get
2624 them. However, if they are zero-sized punt them to yet a different
2625 section (that is not allowed to participate in anchoring). */
2626 if (!size)
2628 fputs ("\t.section\t__DATA,__zobj_data\n", fp);
2629 in_section = darwin_sections[zobj_data_section];
2630 size = 1;
2632 else
2634 fputs ("\t.data\n", fp);
2635 in_section = data_section;
2638 if (l2align)
2639 fprintf (fp, "\t.align\t%u\n", l2align);
2641 assemble_name (fp, name);
2642 fprintf (fp, ":\n\t.space\t" HOST_WIDE_INT_PRINT_UNSIGNED"\n", size);
2644 else
2646 /* Section anchors not in use. */
2647 unsigned int flags = SECTION_BSS|SECTION_WRITE|SECTION_NO_ANCHOR;
2648 char secnam[64];
2649 snprintf (secnam, 64, "__DATA,__common");
2650 in_section = get_section (secnam, flags, NULL);
2651 fprintf (fp, "\t.zerofill %s,", secnam);
2652 assemble_name (fp, name);
2653 if (!size)
2654 size = 1;
2656 if (l2align)
2657 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",%u\n", size, l2align);
2658 else
2659 fprintf (fp, "," HOST_WIDE_INT_PRINT_UNSIGNED",0\n", size);
2661 (* targetm.encode_section_info) (decl, DECL_RTL (decl), false);
2664 /* Output a chunk of common, with alignment specified (where the target
2665 supports this). */
2666 void
2667 darwin_asm_output_aligned_decl_common (FILE *fp, tree decl, const char *name,
2668 unsigned HOST_WIDE_INT size,
2669 unsigned int align)
2671 unsigned int l2align;
2672 bool one, weak;
2673 tree meta;
2675 /* No corresponding var. */
2676 if (decl==NULL)
2678 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2679 fprintf (fp, "# adcom: %s (%d,%d) decl=0x0\n", name, (int)size, (int)align);
2680 #endif
2681 darwin_emit_common (fp, name, size, align);
2682 return;
2685 one = DECL_ONE_ONLY (decl);
2686 weak = (DECL_P (decl)
2687 && DECL_WEAK (decl)
2688 && !lookup_attribute ("weak_import",
2689 DECL_ATTRIBUTES (decl)));
2691 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2692 fprintf (fp, "# adcom: %s (%lld,%d) ro %d cst %d stat %d com %d pub %d"
2693 " weak %d one %d init %lx\n",
2694 name, (long long)size, (int)align, TREE_READONLY (decl),
2695 TREE_CONSTANT (decl), TREE_STATIC (decl), DECL_COMMON (decl),
2696 TREE_PUBLIC (decl), weak, one, (unsigned long)DECL_INITIAL (decl));
2697 #endif
2699 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2700 before the target has a chance to comment. */
2701 if ((meta = is_objc_metadata (decl)))
2703 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2704 return;
2707 /* We shouldn't be messing with this if the decl has a section name. */
2708 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2710 /* We would rather not have to check this here - but it seems that we might
2711 be passed a decl that should be in coalesced space. */
2712 if (one || weak)
2714 /* Weak or COMDAT objects are put in mergable sections. */
2715 darwin_emit_weak_or_comdat (fp, decl, name, size,
2716 ld_uses_coal_sects, DECL_ALIGN (decl));
2717 return;
2720 /* We should only get here for DECL_COMMON, with a zero init (and, in
2721 principle, only for public symbols too - although we deal with local
2722 ones below). */
2724 /* Check the initializer is OK. */
2725 gcc_assert (DECL_COMMON (decl)
2726 && ((DECL_INITIAL (decl) == NULL)
2727 || (DECL_INITIAL (decl) == error_mark_node)
2728 || initializer_zerop (DECL_INITIAL (decl))));
2730 last_assemble_variable_decl = decl;
2732 if (!size || !align)
2733 align = DECL_ALIGN (decl);
2735 l2align = floor_log2 (align / BITS_PER_UNIT);
2736 /* Check we aren't asking for more aligment than the platform allows. */
2737 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2739 if (TREE_PUBLIC (decl) != 0)
2740 darwin_emit_common (fp, name, size, align);
2741 else
2742 darwin_emit_local_bss (fp, decl, name, size, l2align);
2745 /* Output a chunk of BSS with alignment specfied. */
2746 void
2747 darwin_asm_output_aligned_decl_local (FILE *fp, tree decl, const char *name,
2748 unsigned HOST_WIDE_INT size,
2749 unsigned int align)
2751 unsigned long l2align;
2752 bool one, weak;
2753 tree meta;
2755 one = DECL_ONE_ONLY (decl);
2756 weak = (DECL_P (decl)
2757 && DECL_WEAK (decl)
2758 && !lookup_attribute ("weak_import",
2759 DECL_ATTRIBUTES (decl)));
2761 #ifdef DEBUG_DARWIN_MEM_ALLOCATORS
2762 fprintf (fp, "# adloc: %s (%lld,%d) ro %d cst %d stat %d one %d pub %d"
2763 " weak %d init %lx\n",
2764 name, (long long)size, (int)align, TREE_READONLY (decl),
2765 TREE_CONSTANT (decl), TREE_STATIC (decl), one, TREE_PUBLIC (decl),
2766 weak , (unsigned long)DECL_INITIAL (decl));
2767 #endif
2769 /* ObjC metadata can get put in BSS because varasm.c decides it's BSS
2770 before the target has a chance to comment. */
2771 if ((meta = is_objc_metadata (decl)))
2773 darwin_emit_objc_zeroed (fp, decl, name, size, DECL_ALIGN (decl), meta);
2774 return;
2777 /* We shouldn't be messing with this if the decl has a section name. */
2778 gcc_assert (DECL_SECTION_NAME (decl) == NULL);
2780 /* We would rather not have to check this here - but it seems that we might
2781 be passed a decl that should be in coalesced space. */
2782 if (one || weak)
2784 /* Weak or COMDAT objects are put in mergable sections. */
2785 darwin_emit_weak_or_comdat (fp, decl, name, size,
2786 ld_uses_coal_sects, DECL_ALIGN (decl));
2787 return;
2790 /* .. and it should be suitable for placement in local mem. */
2791 gcc_assert(!TREE_PUBLIC (decl) && !DECL_COMMON (decl));
2792 /* .. and any initializer must be all-zero. */
2793 gcc_assert ((DECL_INITIAL (decl) == NULL)
2794 || (DECL_INITIAL (decl) == error_mark_node)
2795 || initializer_zerop (DECL_INITIAL (decl)));
2797 last_assemble_variable_decl = decl;
2799 if (!size || !align)
2800 align = DECL_ALIGN (decl);
2802 l2align = floor_log2 (align / BITS_PER_UNIT);
2803 gcc_assert (l2align <= L2_MAX_OFILE_ALIGNMENT);
2805 darwin_emit_local_bss (fp, decl, name, size, l2align);
2808 /* Emit an assembler directive to set visibility for a symbol. The
2809 only supported visibilities are VISIBILITY_DEFAULT and
2810 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
2811 extern". There is no MACH-O equivalent of ELF's
2812 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
2814 void
2815 darwin_assemble_visibility (tree decl, int vis)
2817 if (vis == VISIBILITY_DEFAULT)
2819 else if (vis == VISIBILITY_HIDDEN || vis == VISIBILITY_INTERNAL)
2821 fputs ("\t.private_extern ", asm_out_file);
2822 assemble_name (asm_out_file,
2823 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
2824 fputs ("\n", asm_out_file);
2826 else
2827 warning (OPT_Wattributes, "protected visibility attribute "
2828 "not supported in this configuration; ignored");
2831 /* vec used by darwin_asm_dwarf_section.
2832 Maybe a hash tab would be better here - but the intention is that this is
2833 a very short list (fewer than 16 items) and each entry should (ideally,
2834 eventually) only be presented once.
2836 A structure to hold a dwarf debug section used entry. */
2838 typedef struct GTY(()) dwarf_sect_used_entry {
2839 const char *name;
2840 unsigned count;
2842 dwarf_sect_used_entry;
2845 /* A list of used __DWARF sections. */
2846 static GTY (()) vec<dwarf_sect_used_entry, va_gc> *dwarf_sect_names_table;
2848 /* This is called when we are asked to assemble a named section and the
2849 name begins with __DWARF,. We keep a list of the section names (without
2850 the __DWARF, prefix) and use this to emit our required start label on the
2851 first switch to each section. */
2853 static void
2854 darwin_asm_dwarf_section (const char *name, unsigned int flags,
2855 tree ARG_UNUSED (decl), bool is_for_lto)
2857 unsigned i;
2858 int namelen, extra = 0;
2859 const char *sect, *lto_add = "";
2860 char sname[64];
2861 dwarf_sect_used_entry *ref;
2862 bool found = false;
2864 gcc_checking_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
2865 == (SECTION_DEBUG | SECTION_NAMED));
2867 /* We know that the name starts with __DWARF, or __GNU_DAWRF_LTO */
2868 sect = strchr (name, ',') + 1;
2869 namelen = strchr (sect, ',') - sect;
2870 gcc_checking_assert (namelen);
2872 /* The section switch is output as written... */
2873 fprintf (asm_out_file, "\t.section %s\n", name);
2875 /* ... but the string we keep to make section start labels needs
2876 adjustment for lto cases. */
2877 if (is_for_lto)
2879 lto_add = "_lto";
2880 extra = 4;
2883 snprintf (sname, 64, "%.*s%.*s", namelen, sect, extra, lto_add);
2884 namelen += extra;
2886 if (dwarf_sect_names_table == NULL)
2887 vec_alloc (dwarf_sect_names_table, 16);
2888 else
2889 for (i = 0;
2890 dwarf_sect_names_table->iterate (i, &ref);
2891 i++)
2893 if (!ref)
2894 break;
2895 if (!strcmp (ref->name, sname))
2897 found = true;
2898 ref->count++;
2899 break;
2903 if (!found)
2905 dwarf_sect_used_entry e;
2906 fprintf (asm_out_file, "Lsection%.*s:\n", namelen, sname);
2907 e.count = 1;
2908 e.name = xstrdup (sname);
2909 vec_safe_push (dwarf_sect_names_table, e);
2913 /* Output a difference of two labels that will be an assembly time
2914 constant if the two labels are local. (.long lab1-lab2 will be
2915 very different if lab1 is at the boundary between two sections; it
2916 will be relocated according to the second section, not the first,
2917 so one ends up with a difference between labels in different
2918 sections, which is bad in the dwarf2 eh context for instance.) */
2920 static int darwin_dwarf_label_counter;
2922 void
2923 darwin_asm_output_dwarf_delta (FILE *file, int size,
2924 const char *lab1, const char *lab2,
2925 HOST_WIDE_INT offset)
2927 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
2928 && lab2[0] == '*' && lab2[1] == 'L');
2929 const char *directive = (size == 8 ? ".quad" : ".long");
2931 if (islocaldiff)
2932 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
2933 else
2934 fprintf (file, "\t%s\t", directive);
2936 assemble_name_raw (file, lab1);
2937 fprintf (file, "-");
2938 assemble_name_raw (file, lab2);
2939 if (offset != 0)
2940 fprintf (file, "+" HOST_WIDE_INT_PRINT_DEC, offset);
2941 if (islocaldiff)
2942 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
2945 /* Output an offset in a DWARF section on Darwin. On Darwin, DWARF section
2946 offsets are not represented using relocs in .o files; either the
2947 section never leaves the .o file, or the linker or other tool is
2948 responsible for parsing the DWARF and updating the offsets. */
2950 void
2951 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
2952 HOST_WIDE_INT offset, section *base)
2954 char sname[64];
2955 int namelen, extra = 0;
2956 bool is_for_lto;
2957 const char *lto_add = "";
2959 gcc_checking_assert (base->common.flags & SECTION_NAMED);
2960 is_for_lto = strncmp (base->named.name, "__GNU_DWARF_LTO,", 16) == 0;
2961 gcc_checking_assert (is_for_lto
2962 || strncmp (base->named.name, "__DWARF,", 8) == 0);
2963 const char *name = strchr (base->named.name, ',') + 1;
2964 gcc_checking_assert (name);
2966 namelen = strchr (name, ',') - (name);
2967 if (is_for_lto)
2969 lto_add = "_lto";
2970 extra = 4;
2972 snprintf (sname, 64, "*Lsection%.*s%.*s", namelen, name, extra, lto_add);
2973 darwin_asm_output_dwarf_delta (file, size, lab, sname, offset);
2976 /* Called from the within the TARGET_ASM_FILE_START for each target. */
2978 void
2979 darwin_file_start (void)
2981 /* Nothing to do. */
2984 /* Called for the TARGET_ASM_FILE_END hook.
2985 Emit the mach-o pic indirection data, the lto data and, finally a flag
2986 to tell the linker that it can break the file object into sections and
2987 move those around for efficiency. */
2989 void
2990 darwin_file_end (void)
2992 if (!vec_safe_is_empty (ctors))
2993 finalize_ctors ();
2994 if (!vec_safe_is_empty (dtors))
2995 finalize_dtors ();
2997 /* If we are expecting to output NeXT ObjC meta-data, (and we actually see
2998 some) then we output the fix-and-continue marker (Image Info).
2999 This applies to Objective C, Objective C++ and LTO with either language
3000 as part of the input. */
3001 if (flag_next_runtime && objc_metadata_seen)
3003 unsigned int flags = 0;
3004 if (flag_objc_abi >= 2)
3006 flags = 16;
3007 switch_to_section (darwin_sections[objc2_image_info_section]);
3009 else
3010 switch_to_section (darwin_sections[objc_image_info_section]);
3012 ASM_OUTPUT_ALIGN (asm_out_file, 2);
3013 fputs ("L_OBJC_ImageInfo:\n", asm_out_file);
3015 flags |= (flag_replace_objc_classes && classes_seen) ? 1 : 0;
3016 flags |= flag_objc_gc ? 2 : 0;
3018 fprintf (asm_out_file, "\t.long\t0\n\t.long\t%u\n", flags);
3021 machopic_finish (asm_out_file);
3022 if (flag_apple_kext)
3024 /* These sections are only used for kernel code. */
3025 switch_to_section (darwin_sections[constructor_section]);
3026 switch_to_section (darwin_sections[destructor_section]);
3027 ASM_OUTPUT_ALIGN (asm_out_file, 1);
3030 /* If there was LTO assembler output, append it to asm_out_file. */
3031 if (lto_asm_out_name)
3033 int n;
3034 char *buf, *lto_asm_txt;
3036 /* Shouldn't be here if we failed to switch back. */
3037 gcc_assert (! saved_asm_out_file);
3039 lto_asm_out_file = fopen (lto_asm_out_name, "r");
3040 if (lto_asm_out_file == NULL)
3041 fatal_error (input_location,
3042 "failed to open temporary file %s with LTO output",
3043 lto_asm_out_name);
3044 fseek (lto_asm_out_file, 0, SEEK_END);
3045 n = ftell (lto_asm_out_file);
3046 if (n > 0)
3048 fseek (lto_asm_out_file, 0, SEEK_SET);
3049 lto_asm_txt = buf = (char *) xmalloc (n + 1);
3050 while (fgets (lto_asm_txt, n, lto_asm_out_file))
3051 fputs (lto_asm_txt, asm_out_file);
3052 /* Put a termination label. */
3053 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
3054 LTO_SEGMENT_NAME, LTO_SECTS_SECTION);
3055 fprintf (asm_out_file, "L_GNU_LTO%d:\t;# end of lto\n",
3056 lto_section_num);
3057 /* Make sure our termination label stays in this section. */
3058 fputs ("\t.space\t1\n", asm_out_file);
3061 /* Remove the temporary file. */
3062 fclose (lto_asm_out_file);
3063 unlink_if_ordinary (lto_asm_out_name);
3064 free (lto_asm_out_name);
3067 /* Output the names and indices. */
3068 if (lto_section_names && lto_section_names->length ())
3070 int count;
3071 darwin_lto_section_e *ref;
3072 /* For now, we'll make the offsets 4 bytes and unaligned - we'll fix
3073 the latter up ourselves. */
3074 const char *op = integer_asm_op (4,0);
3076 /* Emit the names. */
3077 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
3078 LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
3079 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
3081 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\n", count);
3082 /* We have to jump through hoops to get the values of the intra-section
3083 offsets... */
3084 fprintf (asm_out_file,
3085 "\t.set L$gnu$lto$noff%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME0\n",
3086 count, count);
3087 fprintf (asm_out_file,
3088 "\t.set L$gnu$lto$nsiz%d,L_GNU_LTO_NAME%d-L_GNU_LTO_NAME%d\n",
3089 count, count+1, count);
3090 fprintf (asm_out_file, "\t.asciz\t\"%s\"\n", ref->sectname);
3092 fprintf (asm_out_file, "L_GNU_LTO_NAME%d:\t;# end\n", lto_section_num);
3093 /* make sure our termination label stays in this section. */
3094 fputs ("\t.space\t1\n", asm_out_file);
3096 /* Emit the Index. */
3097 fprintf (asm_out_file, "\t.section %s,%s,regular,debug\n",
3098 LTO_SEGMENT_NAME, LTO_INDEX_SECTION);
3099 fputs ("\t.align\t2\n", asm_out_file);
3100 fputs ("# Section offset, Section length, Name offset, Name length\n",
3101 asm_out_file);
3102 FOR_EACH_VEC_ELT (*lto_section_names, count, ref)
3104 fprintf (asm_out_file, "%s L$gnu$lto$offs%d\t;# %s\n",
3105 op, count, ref->sectname);
3106 fprintf (asm_out_file, "%s L$gnu$lto$size%d\n", op, count);
3107 fprintf (asm_out_file, "%s L$gnu$lto$noff%d\n", op, count);
3108 fprintf (asm_out_file, "%s L$gnu$lto$nsiz%d\n", op, count);
3112 /* If we have section anchors, then we must prevent the linker from
3113 re-arranging data. */
3114 if (!DARWIN_SECTION_ANCHORS || !flag_section_anchors)
3115 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
3118 /* TODO: Add a language hook for identifying if a decl is a vtable. */
3119 #define DARWIN_VTABLE_P(DECL) 0
3121 /* Cross-module name binding. Darwin does not support overriding
3122 functions at dynamic-link time, except for vtables in kexts. */
3124 bool
3125 darwin_binds_local_p (const_tree decl)
3127 /* We use the "shlib" input to indicate that a symbol should be
3128 considered overridable; only relevant for vtables in kernel modules
3129 on earlier system versions, and with a TODO to complete. */
3130 bool force_overridable = TARGET_KEXTABI && DARWIN_VTABLE_P (decl);
3131 return default_binds_local_p_3 (decl, force_overridable /* shlib */,
3132 false /* weak dominate */,
3133 false /* extern_protected_data */,
3134 false /* common_local_p */);
3137 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
3138 anchor relative to ".", the current section position. We cannot use
3139 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
3140 void
3141 darwin_asm_output_anchor (rtx symbol)
3143 fprintf (asm_out_file, "\t.set\t");
3144 assemble_name (asm_out_file, XSTR (symbol, 0));
3145 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
3146 SYMBOL_REF_BLOCK_OFFSET (symbol));
3149 /* Disable section anchoring on any section containing a zero-sized
3150 object. */
3151 bool
3152 darwin_use_anchors_for_symbol_p (const_rtx symbol)
3154 if (DARWIN_SECTION_ANCHORS && flag_section_anchors)
3156 section *sect;
3157 /* If the section contains a zero-sized object it's ineligible. */
3158 sect = SYMBOL_REF_BLOCK (symbol)->sect;
3159 /* This should have the effect of disabling anchors for vars that follow
3160 any zero-sized one, in a given section. */
3161 if (sect->common.flags & SECTION_NO_ANCHOR)
3162 return false;
3164 /* Also check the normal reasons for suppressing. */
3165 return default_use_anchors_for_symbol_p (symbol);
3167 else
3168 return false;
3171 /* Set the darwin specific attributes on TYPE. */
3172 void
3173 darwin_set_default_type_attributes (tree type)
3175 if (darwin_ms_struct
3176 && TREE_CODE (type) == RECORD_TYPE)
3177 TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
3178 NULL_TREE,
3179 TYPE_ATTRIBUTES (type));
3182 /* True, iff we're generating code for loadable kernel extensions. */
3184 bool
3185 darwin_kextabi_p (void) {
3186 return flag_apple_kext;
3189 void
3190 darwin_override_options (void)
3192 /* Keep track of which (major) version we're generating code for. */
3193 if (darwin_macosx_version_min)
3195 if (strverscmp (darwin_macosx_version_min, "10.7") >= 0)
3196 generating_for_darwin_version = 11;
3197 else if (strverscmp (darwin_macosx_version_min, "10.6") >= 0)
3198 generating_for_darwin_version = 10;
3199 else if (strverscmp (darwin_macosx_version_min, "10.5") >= 0)
3200 generating_for_darwin_version = 9;
3201 else if (strverscmp (darwin_macosx_version_min, "10.4") >= 0)
3202 generating_for_darwin_version = 8;
3204 /* Earlier versions are not specifically accounted, until required. */
3207 /* Older Darwin ld could not coalesce weak entities without them being
3208 placed in special sections. */
3209 if (darwin_target_linker
3210 && (strverscmp (darwin_target_linker, MIN_LD64_NO_COAL_SECTS) < 0))
3211 ld_uses_coal_sects = true;
3213 /* In principle, this should be c-family only. However, we really need to
3214 set sensible defaults for LTO as well, since the section selection stuff
3215 should check for correctness re. the ABI. TODO: check and provide the
3216 flags (runtime & ABI) from the lto wrapper). */
3218 /* At present, make a hard update to the runtime version based on the target
3219 OS version. */
3220 if (flag_next_runtime)
3222 if (generating_for_darwin_version > 10)
3223 flag_next_runtime = 100705;
3224 else if (generating_for_darwin_version > 9)
3225 flag_next_runtime = 100608;
3226 else if (generating_for_darwin_version > 8)
3227 flag_next_runtime = 100508;
3228 else
3229 flag_next_runtime = 100000;
3232 /* Unless set, force ABI=2 for NeXT and m64, 0 otherwise. */
3233 if (!global_options_set.x_flag_objc_abi)
3234 global_options.x_flag_objc_abi
3235 = (!flag_next_runtime)
3237 : (TARGET_64BIT ? 2
3238 : (generating_for_darwin_version >= 9) ? 1
3239 : 0);
3241 if (global_options_set.x_flag_objc_abi && flag_next_runtime)
3243 if (TARGET_64BIT && global_options.x_flag_objc_abi != 2)
3244 /* The Objective-C family ABI 2 is the only valid version NeXT/m64. */
3245 error_at (UNKNOWN_LOCATION,
3246 "%<-fobjc-abi-version%> 2 must be used for 64 bit targets"
3247 " with %<-fnext-runtime%>");
3248 else if (!TARGET_64BIT && global_options.x_flag_objc_abi >= 2)
3249 /* ABI versions 0 and 1 are the only valid versions NeXT/m32. */
3250 error_at (UNKNOWN_LOCATION,
3251 "%<-fobjc-abi-version%> %d is not supported for 32 bit"
3252 " targets with %<-fnext-runtime%>",
3253 global_options.x_flag_objc_abi);
3256 /* Don't emit DWARF3/4 unless specifically selected. This is a
3257 workaround for tool bugs. */
3258 if (!global_options_set.x_dwarf_strict)
3259 dwarf_strict = 1;
3260 if (!global_options_set.x_dwarf_version)
3261 dwarf_version = 2;
3263 if (global_options_set.x_dwarf_split_debug_info)
3265 inform (input_location,
3266 "%<-gsplit-dwarf%> is not supported on this platform, ignored");
3267 dwarf_split_debug_info = 0;
3268 global_options_set.x_dwarf_split_debug_info = 0;
3271 /* Do not allow unwind tables to be generated by default for m32.
3272 fnon-call-exceptions will override this, regardless of what we do. */
3273 if (generating_for_darwin_version < 10
3274 && !global_options_set.x_flag_asynchronous_unwind_tables
3275 && !TARGET_64BIT)
3276 global_options.x_flag_asynchronous_unwind_tables = 0;
3278 /* Disable -freorder-blocks-and-partition when unwind tables are being
3279 emitted for Darwin < 9 (OSX 10.5).
3280 The strategy is, "Unless the User has specifically set/unset an unwind
3281 flag we will switch off -freorder-blocks-and-partition when unwind tables
3282 will be generated". If the User specifically sets flags... we assume
3283 (s)he knows why... */
3284 if (generating_for_darwin_version < 9
3285 && global_options_set.x_flag_reorder_blocks_and_partition
3286 && ((global_options.x_flag_exceptions /* User, c++, java */
3287 && !global_options_set.x_flag_exceptions) /* User specified... */
3288 || (global_options.x_flag_unwind_tables
3289 && !global_options_set.x_flag_unwind_tables)
3290 || (global_options.x_flag_non_call_exceptions
3291 && !global_options_set.x_flag_non_call_exceptions)
3292 || (global_options.x_flag_asynchronous_unwind_tables
3293 && !global_options_set.x_flag_asynchronous_unwind_tables)))
3295 inform (input_location,
3296 "%<-freorder-blocks-and-partition%> does not work with "
3297 "exceptions on this architecture");
3298 flag_reorder_blocks_and_partition = 0;
3299 flag_reorder_blocks = 1;
3302 /* FIXME: flag_objc_sjlj_exceptions is no longer needed since there is only
3303 one valid choice of exception scheme for each runtime. */
3304 if (!global_options_set.x_flag_objc_sjlj_exceptions)
3305 global_options.x_flag_objc_sjlj_exceptions =
3306 flag_next_runtime && !TARGET_64BIT;
3308 /* FIXME: and this could be eliminated then too. */
3309 if (!global_options_set.x_flag_exceptions
3310 && flag_objc_exceptions
3311 && TARGET_64BIT)
3312 flag_exceptions = 1;
3314 if (flag_mkernel || flag_apple_kext)
3316 /* -mkernel implies -fapple-kext for C++ */
3317 if (lang_GNU_CXX ())
3318 flag_apple_kext = 1;
3320 flag_no_common = 1;
3322 /* No EH in kexts. */
3323 flag_exceptions = 0;
3324 /* No -fnon-call-exceptions data in kexts. */
3325 flag_non_call_exceptions = 0;
3326 /* so no tables either.. */
3327 flag_unwind_tables = 0;
3328 flag_asynchronous_unwind_tables = 0;
3331 if (flag_var_tracking_uninit == 0
3332 && generating_for_darwin_version >= 9
3333 && (flag_gtoggle ? (debug_info_level == DINFO_LEVEL_NONE)
3334 : (debug_info_level >= DINFO_LEVEL_NORMAL))
3335 && write_symbols == DWARF2_DEBUG)
3336 flag_var_tracking_uninit = flag_var_tracking;
3338 /* Final check on PCI options; for Darwin these are not dependent on the PIE
3339 ones, although PIE does require PIC to support it. */
3340 if (MACHO_DYNAMIC_NO_PIC_P)
3342 if (flag_pic)
3343 warning_at (UNKNOWN_LOCATION, 0,
3344 "%<-mdynamic-no-pic%> overrides %<-fpic%>, %<-fPIC%>,"
3345 " %<-fpie%> or %<-fPIE%>");
3346 flag_pic = 0;
3348 else if (flag_pic == 1
3349 || (flag_pic == 0 && !(flag_mkernel || flag_apple_kext)))
3351 /* Darwin's -fpic is -fPIC.
3352 We only support "static" code in the kernel and kernel exts. */
3353 flag_pic = 2;
3356 /* Linkers >= ld64-62.1 (at least) are capable of making the necessary PIC
3357 indirections and we no longer need to emit pic symbol stubs.
3358 However, if we are generating code for earlier ones (or for use in the
3359 kernel) the stubs might still be required, and this will be set true.
3360 If the user sets it on or off - then that takes precedence.
3362 Linkers that don't need stubs, don't need the EH symbol markers either.
3365 if (!global_options_set.x_darwin_symbol_stubs)
3367 if (darwin_target_linker)
3369 if (strverscmp (darwin_target_linker, MIN_LD64_OMIT_STUBS) < 0)
3371 darwin_symbol_stubs = true;
3372 ld_needs_eh_markers = true;
3375 else if (generating_for_darwin_version < 9)
3377 /* If we don't know the linker version and we're targeting an old
3378 system, we know no better than to assume the use of an earlier
3379 linker. */
3380 darwin_symbol_stubs = true;
3381 ld_needs_eh_markers = true;
3384 else if (DARWIN_X86 && darwin_symbol_stubs && TARGET_64BIT)
3386 inform (input_location,
3387 "%<-mpic-symbol-stubs%> is not required for 64-bit code "
3388 "(ignored)");
3389 darwin_symbol_stubs = false;
3392 if (generating_for_darwin_version >= 9)
3393 /* Later systems can support aligned common. */
3394 emit_aligned_common = true;
3396 /* The c_dialect...() macros are not available to us here. */
3397 darwin_running_cxx = (strstr (lang_hooks.name, "C++") != 0);
3400 #if DARWIN_PPC
3401 /* Add $LDBL128 suffix to long double builtins for ppc darwin. */
3403 static void
3404 darwin_patch_builtin (enum built_in_function fncode)
3406 tree fn = builtin_decl_explicit (fncode);
3407 tree sym;
3408 char *newname;
3410 if (!fn)
3411 return;
3413 sym = DECL_ASSEMBLER_NAME (fn);
3414 newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
3416 set_user_assembler_name (fn, newname);
3418 fn = builtin_decl_implicit (fncode);
3419 if (fn)
3420 set_user_assembler_name (fn, newname);
3423 void
3424 darwin_patch_builtins (void)
3426 if (LONG_DOUBLE_TYPE_SIZE != 128)
3427 return;
3429 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
3430 #define PATCH_BUILTIN_NO64(fncode) \
3431 if (!TARGET_64BIT) \
3432 darwin_patch_builtin (fncode);
3433 #define PATCH_BUILTIN_VARIADIC(fncode) \
3434 if (!TARGET_64BIT \
3435 && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
3436 darwin_patch_builtin (fncode);
3437 #include "darwin-ppc-ldouble-patch.def"
3438 #undef PATCH_BUILTIN
3439 #undef PATCH_BUILTIN_NO64
3440 #undef PATCH_BUILTIN_VARIADIC
3442 #endif
3444 /* CFStrings implementation. */
3445 static GTY(()) tree cfstring_class_reference = NULL_TREE;
3446 static GTY(()) tree cfstring_type_node = NULL_TREE;
3447 static GTY(()) tree ccfstring_type_node = NULL_TREE;
3448 static GTY(()) tree pccfstring_type_node = NULL_TREE;
3449 static GTY(()) tree pcint_type_node = NULL_TREE;
3450 static GTY(()) tree pcchar_type_node = NULL_TREE;
3452 static enum built_in_function darwin_builtin_cfstring;
3454 /* Store all constructed constant CFStrings in a hash table so that
3455 they get uniqued properly. */
3457 typedef struct GTY ((for_user)) cfstring_descriptor {
3458 /* The string literal. */
3459 tree literal;
3460 /* The resulting constant CFString. */
3461 tree constructor;
3462 } cfstring_descriptor;
3464 struct cfstring_hasher : ggc_ptr_hash<cfstring_descriptor>
3466 static hashval_t hash (cfstring_descriptor *);
3467 static bool equal (cfstring_descriptor *, cfstring_descriptor *);
3470 static GTY (()) hash_table<cfstring_hasher> *cfstring_htab;
3472 static tree
3473 add_builtin_field_decl (tree type, const char *name, tree **chain)
3475 tree field = build_decl (BUILTINS_LOCATION, FIELD_DECL,
3476 get_identifier (name), type);
3478 if (*chain != NULL)
3479 **chain = field;
3480 *chain = &DECL_CHAIN (field);
3482 return field;
3485 tree
3486 darwin_init_cfstring_builtins (unsigned builtin_cfstring)
3488 tree cfsfun, fields, pccfstring_ftype_pcchar;
3489 tree *chain = NULL;
3491 darwin_builtin_cfstring =
3492 (enum built_in_function) builtin_cfstring;
3494 /* struct __builtin_CFString {
3495 const int *isa; (will point at
3496 int flags; __CFConstantStringClassReference)
3497 const char *str;
3498 long length;
3499 }; */
3501 pcint_type_node = build_pointer_type
3502 (build_qualified_type (integer_type_node, TYPE_QUAL_CONST));
3504 pcchar_type_node = build_pointer_type
3505 (build_qualified_type (char_type_node, TYPE_QUAL_CONST));
3507 cfstring_type_node = (*lang_hooks.types.make_type) (RECORD_TYPE);
3509 /* Have to build backwards for finish struct. */
3510 fields = add_builtin_field_decl (long_integer_type_node, "length", &chain);
3511 add_builtin_field_decl (pcchar_type_node, "str", &chain);
3512 add_builtin_field_decl (integer_type_node, "flags", &chain);
3513 add_builtin_field_decl (pcint_type_node, "isa", &chain);
3514 finish_builtin_struct (cfstring_type_node, "__builtin_CFString",
3515 fields, NULL_TREE);
3517 /* const struct __builtin_CFstring *
3518 __builtin___CFStringMakeConstantString (const char *); */
3520 ccfstring_type_node = build_qualified_type
3521 (cfstring_type_node, TYPE_QUAL_CONST);
3522 pccfstring_type_node = build_pointer_type (ccfstring_type_node);
3523 pccfstring_ftype_pcchar = build_function_type_list
3524 (pccfstring_type_node, pcchar_type_node, NULL_TREE);
3526 cfsfun = build_decl (BUILTINS_LOCATION, FUNCTION_DECL,
3527 get_identifier ("__builtin___CFStringMakeConstantString"),
3528 pccfstring_ftype_pcchar);
3530 TREE_PUBLIC (cfsfun) = 1;
3531 DECL_EXTERNAL (cfsfun) = 1;
3532 DECL_ARTIFICIAL (cfsfun) = 1;
3533 /* Make a lang-specific section - dup_lang_specific_decl makes a new node
3534 in place of the existing, which may be NULL. */
3535 DECL_LANG_SPECIFIC (cfsfun) = NULL;
3536 (*lang_hooks.dup_lang_specific_decl) (cfsfun);
3537 set_decl_built_in_function (cfsfun, BUILT_IN_MD, darwin_builtin_cfstring);
3538 lang_hooks.builtin_function (cfsfun);
3540 /* extern int __CFConstantStringClassReference[]; */
3541 cfstring_class_reference = build_decl (BUILTINS_LOCATION, VAR_DECL,
3542 get_identifier ("__CFConstantStringClassReference"),
3543 build_array_type (integer_type_node, NULL_TREE));
3545 TREE_PUBLIC (cfstring_class_reference) = 1;
3546 DECL_ARTIFICIAL (cfstring_class_reference) = 1;
3547 (*lang_hooks.decls.pushdecl) (cfstring_class_reference);
3548 DECL_EXTERNAL (cfstring_class_reference) = 1;
3549 rest_of_decl_compilation (cfstring_class_reference, 0, 0);
3551 /* Initialize the hash table used to hold the constant CFString objects. */
3552 cfstring_htab = hash_table<cfstring_hasher>::create_ggc (31);
3554 return cfstring_type_node;
3557 tree
3558 darwin_fold_builtin (tree fndecl, int n_args, tree *argp,
3559 bool ARG_UNUSED (ignore))
3561 unsigned int fcode = DECL_MD_FUNCTION_CODE (fndecl);
3563 if (fcode == darwin_builtin_cfstring)
3565 if (!darwin_constant_cfstrings)
3567 error ("built-in function %qD requires the"
3568 " %<-mconstant-cfstrings%> flag", fndecl);
3569 return error_mark_node;
3572 if (n_args != 1)
3574 error ("built-in function %qD takes one argument only", fndecl);
3575 return error_mark_node;
3578 return darwin_build_constant_cfstring (*argp);
3581 return NULL_TREE;
3584 void
3585 darwin_rename_builtins (void)
3587 /* The system ___divdc3 routine in libSystem on darwin10 is not
3588 accurate to 1ulp, ours is, so we avoid ever using the system name
3589 for this routine and instead install a non-conflicting name that
3590 is accurate.
3592 When -ffast-math or -funsafe-math-optimizations is given, we can
3593 use the faster version. */
3594 if (!flag_unsafe_math_optimizations)
3596 enum built_in_function dcode
3597 = (enum built_in_function)(BUILT_IN_COMPLEX_DIV_MIN
3598 + DCmode - MIN_MODE_COMPLEX_FLOAT);
3599 tree fn = builtin_decl_explicit (dcode);
3600 /* Fortran and c call TARGET_INIT_BUILTINS and
3601 TARGET_INIT_LIBFUNCS at different times, so we have to put a
3602 call into each to ensure that at least one of them is called
3603 after build_common_builtin_nodes. A better fix is to add a
3604 new hook to run after build_common_builtin_nodes runs. */
3605 if (fn)
3606 set_user_assembler_name (fn, "___ieee_divdc3");
3607 fn = builtin_decl_implicit (dcode);
3608 if (fn)
3609 set_user_assembler_name (fn, "___ieee_divdc3");
3613 bool
3614 darwin_libc_has_function (enum function_class fn_class,
3615 tree type ATTRIBUTE_UNUSED)
3617 if (fn_class == function_sincos)
3618 return (strverscmp (darwin_macosx_version_min, "10.9") >= 0);
3620 if (fn_class == function_c99_math_complex
3621 || fn_class == function_c99_misc)
3622 return (TARGET_64BIT
3623 || strverscmp (darwin_macosx_version_min, "10.3") >= 0);
3625 return true;
3628 hashval_t
3629 cfstring_hasher::hash (cfstring_descriptor *ptr)
3631 tree str = ptr->literal;
3632 const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (str);
3633 int i, len = TREE_STRING_LENGTH (str);
3634 hashval_t h = len;
3636 for (i = 0; i < len; i++)
3637 h = ((h * 613) + p[i]);
3639 return h;
3642 bool
3643 cfstring_hasher::equal (cfstring_descriptor *ptr1, cfstring_descriptor *ptr2)
3645 tree str1 = ptr1->literal;
3646 tree str2 = ptr2->literal;
3647 int len1 = TREE_STRING_LENGTH (str1);
3649 return (len1 == TREE_STRING_LENGTH (str2)
3650 && !memcmp (TREE_STRING_POINTER (str1), TREE_STRING_POINTER (str2),
3651 len1));
3654 tree
3655 darwin_build_constant_cfstring (tree str)
3657 struct cfstring_descriptor *desc, key;
3658 tree addr;
3660 if (!str)
3662 error ("CFString literal is missing");
3663 return error_mark_node;
3666 STRIP_NOPS (str);
3668 if (TREE_CODE (str) == ADDR_EXPR)
3669 str = TREE_OPERAND (str, 0);
3671 if (TREE_CODE (str) != STRING_CST)
3673 error ("CFString literal expression is not a string constant");
3674 return error_mark_node;
3677 /* Perhaps we already constructed a constant CFString just like this one? */
3678 key.literal = str;
3679 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
3680 desc = *loc;
3682 if (!desc)
3684 tree var, constructor, field;
3685 vec<constructor_elt, va_gc> *v = NULL;
3686 int length = TREE_STRING_LENGTH (str) - 1;
3688 if (darwin_warn_nonportable_cfstrings)
3690 const char *s = TREE_STRING_POINTER (str);
3691 int l = 0;
3693 for (l = 0; l < length; l++)
3694 if (!s[l] || !isascii (s[l]))
3696 warning (darwin_warn_nonportable_cfstrings,
3697 s[l] ? G_("non-ASCII character in CFString literal")
3698 : G_("embedded NUL in CFString literal"));
3699 break;
3703 *loc = desc = ggc_cleared_alloc<cfstring_descriptor> ();
3704 desc->literal = str;
3706 /* isa *. */
3707 field = TYPE_FIELDS (ccfstring_type_node);
3708 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3709 build1 (ADDR_EXPR, TREE_TYPE (field),
3710 cfstring_class_reference));
3711 /* flags */
3712 field = DECL_CHAIN (field);
3713 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3714 build_int_cst (TREE_TYPE (field), 0x000007c8));
3715 /* string *. */
3716 field = DECL_CHAIN (field);
3717 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3718 build1 (ADDR_EXPR, TREE_TYPE (field), str));
3719 /* length */
3720 field = DECL_CHAIN (field);
3721 CONSTRUCTOR_APPEND_ELT(v, NULL_TREE,
3722 build_int_cst (TREE_TYPE (field), length));
3724 constructor = build_constructor (ccfstring_type_node, v);
3725 TREE_READONLY (constructor) = 1;
3726 TREE_CONSTANT (constructor) = 1;
3727 TREE_STATIC (constructor) = 1;
3729 /* Fromage: The C++ flavor of 'build_unary_op' expects constructor nodes
3730 to have the TREE_HAS_CONSTRUCTOR (...) bit set. However, this file is
3731 being built without any knowledge of C++ tree accessors; hence, we shall
3732 use the generic accessor that TREE_HAS_CONSTRUCTOR actually maps to! */
3733 if (darwin_running_cxx)
3734 TREE_LANG_FLAG_4 (constructor) = 1; /* TREE_HAS_CONSTRUCTOR */
3736 /* Create an anonymous global variable for this CFString. */
3737 var = build_decl (input_location, CONST_DECL,
3738 NULL, TREE_TYPE (constructor));
3739 DECL_ARTIFICIAL (var) = 1;
3740 TREE_STATIC (var) = 1;
3741 DECL_INITIAL (var) = constructor;
3742 /* FIXME: This should use a translation_unit_decl to indicate file scope. */
3743 DECL_CONTEXT (var) = NULL_TREE;
3744 desc->constructor = var;
3747 addr = build1 (ADDR_EXPR, pccfstring_type_node, desc->constructor);
3748 TREE_CONSTANT (addr) = 1;
3750 return addr;
3753 bool
3754 darwin_cfstring_p (tree str)
3756 struct cfstring_descriptor key;
3758 if (!str)
3759 return false;
3761 STRIP_NOPS (str);
3763 if (TREE_CODE (str) == ADDR_EXPR)
3764 str = TREE_OPERAND (str, 0);
3766 if (TREE_CODE (str) != STRING_CST)
3767 return false;
3769 key.literal = str;
3770 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, NO_INSERT);
3772 if (loc)
3773 return true;
3775 return false;
3778 void
3779 darwin_enter_string_into_cfstring_table (tree str)
3781 struct cfstring_descriptor key;
3783 key.literal = str;
3784 cfstring_descriptor **loc = cfstring_htab->find_slot (&key, INSERT);
3786 if (!*loc)
3788 *loc = ggc_cleared_alloc<cfstring_descriptor> ();
3789 ((struct cfstring_descriptor *)*loc)->literal = str;
3793 /* Choose named function section based on its frequency. */
3795 section *
3796 darwin_function_section (tree decl, enum node_frequency freq,
3797 bool startup, bool exit)
3799 /* Decide if we need to put this in a coalescable section. */
3800 bool weak = (decl
3801 && DECL_WEAK (decl)
3802 && (!DECL_ATTRIBUTES (decl)
3803 || !lookup_attribute ("weak_import",
3804 DECL_ATTRIBUTES (decl))));
3806 bool use_coal = weak && ld_uses_coal_sects;
3807 /* If there is a specified section name, we should not be trying to
3808 override. */
3809 if (decl && DECL_SECTION_NAME (decl) != NULL)
3810 return get_named_section (decl, NULL, 0);
3812 /* We always put unlikely executed stuff in the cold section. */
3813 if (freq == NODE_FREQUENCY_UNLIKELY_EXECUTED)
3814 return (use_coal) ? darwin_sections[text_cold_coal_section]
3815 : darwin_sections[text_cold_section];
3817 /* If we have LTO *and* feedback information, then let LTO handle
3818 the function ordering, it makes a better job (for normal, hot,
3819 startup and exit - hence the bailout for cold above). */
3820 if (in_lto_p && flag_profile_values)
3821 goto default_function_sections;
3823 /* Non-cold startup code should go to startup subsection. */
3824 if (startup)
3825 return (use_coal) ? darwin_sections[text_startup_coal_section]
3826 : darwin_sections[text_startup_section];
3828 /* Similarly for exit. */
3829 if (exit)
3830 return (use_coal) ? darwin_sections[text_exit_coal_section]
3831 : darwin_sections[text_exit_section];
3833 /* Place hot code. */
3834 if (freq == NODE_FREQUENCY_HOT)
3835 return (use_coal) ? darwin_sections[text_hot_coal_section]
3836 : darwin_sections[text_hot_section];
3838 /* Otherwise, default to the 'normal' non-reordered sections. */
3839 default_function_sections:
3840 return (use_coal) ? darwin_sections[text_coal_section]
3841 : text_section;
3844 #include "gt-darwin.h"