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