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