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