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