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