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