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