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