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