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