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