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