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