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