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