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