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