]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/config/darwin.c
Daily bump.
[thirdparty/gcc.git] / gcc / config / darwin.c
CommitLineData
ee890fe2 1/* Functions for generic Darwin as target machine for GNU C compiler.
5b86a469
KH
2 Copyright (C) 1989, 1990, 1991, 1992, 1993, 2000, 2001, 2002, 2003, 2004,
3 2005
ee890fe2
SS
4 Free Software Foundation, Inc.
5 Contributed by Apple Computer Inc.
6
7ec022b2 7This file is part of GCC.
ee890fe2 8
7ec022b2 9GCC is free software; you can redistribute it and/or modify
ee890fe2
SS
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation; either version 2, or (at your option)
12any later version.
13
7ec022b2 14GCC is distributed in the hope that it will be useful,
ee890fe2
SS
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
7ec022b2 20along with GCC; see the file COPYING. If not, write to
39d14dda
KC
21the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22Boston, MA 02110-1301, USA. */
ee890fe2
SS
23
24#include "config.h"
25#include "system.h"
4977bab6
ZW
26#include "coretypes.h"
27#include "tm.h"
ee890fe2
SS
28#include "rtl.h"
29#include "regs.h"
30#include "hard-reg-set.h"
31#include "real.h"
32#include "insn-config.h"
33#include "conditions.h"
34#include "insn-flags.h"
35#include "output.h"
36#include "insn-attr.h"
37#include "flags.h"
38#include "tree.h"
39#include "expr.h"
40#include "reload.h"
ee890fe2
SS
41#include "function.h"
42#include "ggc.h"
3ac88239 43#include "langhooks.h"
1adaa117 44#include "target.h"
245ff137 45#include "tm_p.h"
4c714dd4 46#include "toplev.h"
11abc112 47#include "hashtab.h"
ee890fe2 48
699c914a
MS
49/* Darwin supports a feature called fix-and-continue, which is used
50 for rapid turn around debugging. When code is compiled with the
51 -mfix-and-continue flag, two changes are made to the generated code
52 that allow the system to do things that it would normally not be
53 able to do easily. These changes allow gdb to load in
54 recompilation of a translation unit that has been changed into a
55 running program and replace existing functions and methods of that
56f42830 56 translation unit with versions of those functions and methods
699c914a 57 from the newly compiled translation unit. The new functions access
d6ff8575
MS
58 the existing static symbols from the old translation unit, if the
59 symbol existed in the unit to be replaced, and from the new
60 translation unit, otherwise.
699c914a 61
de2ab0ca 62 The changes are to insert 5 nops at the beginning of all functions
d6ff8575 63 and to use indirection to get at static symbols. The 5 nops
699c914a
MS
64 are required by consumers of the generated code. Currently, gdb
65 uses this to patch in a jump to the overriding function, this
66 allows all uses of the old name to forward to the replacement,
c112cf2b 67 including existing function pointers and virtual methods. See
699c914a 68 rs6000_emit_prologue for the code that handles the nop insertions.
083cad55 69
699c914a 70 The added indirection allows gdb to redirect accesses to static
d6ff8575
MS
71 symbols from the newly loaded translation unit to the existing
72 symbol, if any. @code{static} symbols are special and are handled by
73 setting the second word in the .non_lazy_symbol_pointer data
74 structure to symbol. See indirect_data for the code that handles
75 the extra indirection, and machopic_output_indirection and its use
76 of MACHO_SYMBOL_STATIC for the code that handles @code{static}
77 symbol indirection. */
699c914a 78
56c779bc
GK
79/* Section names. */
80section * darwin_sections[NUM_DARWIN_SECTIONS];
d6b5193b
RS
81
82/* A get_unnamed_section callback used to switch to an ObjC section.
83 DIRECTIVE is as for output_section_asm_op. */
84
85static void
86output_objc_section_asm_op (const void *directive)
87{
56c779bc 88 static bool been_here = false;
d6b5193b 89
56c779bc 90 if (! been_here)
d6b5193b 91 {
083cad55 92 static const enum darwin_section_enum tomark[] =
56c779bc
GK
93 {
94 /* written, cold -> hot */
95 objc_cat_cls_meth_section,
96 objc_cat_inst_meth_section,
97 objc_string_object_section,
98 objc_constant_string_object_section,
99 objc_selector_refs_section,
100 objc_selector_fixup_section,
101 objc_cls_refs_section,
102 objc_class_section,
103 objc_meta_class_section,
104 /* shared, hot -> cold */
105 objc_cls_meth_section,
106 objc_inst_meth_section,
107 objc_protocol_section,
108 objc_class_names_section,
109 objc_meth_var_types_section,
110 objc_meth_var_names_section,
111 objc_category_section,
112 objc_class_vars_section,
113 objc_instance_vars_section,
114 objc_module_info_section,
115 objc_symbols_section
116 };
117 size_t i;
083cad55 118
56c779bc
GK
119 been_here = true;
120 for (i = 0; i < ARRAY_SIZE (tomark); i++)
121 switch_to_section (darwin_sections[tomark[i]]);
d6b5193b
RS
122 }
123 output_section_asm_op (directive);
124}
125
126/* Implement TARGET_ASM_INIT_SECTIONS. */
127
128void
129darwin_init_sections (void)
130{
56c779bc
GK
131#define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC) \
132 darwin_sections[NAME] = \
133 get_unnamed_section (FLAGS, (OBJC \
134 ? output_objc_section_asm_op \
135 : output_section_asm_op), \
136 "\t" DIRECTIVE);
137#include "config/darwin-sections.def"
d6b5193b
RS
138#undef DEF_SECTION
139
56c779bc
GK
140 readonly_data_section = darwin_sections[const_section];
141 exception_section = darwin_sections[darwin_exception_section];
142 eh_frame_section = darwin_sections[darwin_eh_frame_section];
d6b5193b 143}
699c914a 144
ee890fe2 145int
9c808aad 146name_needs_quotes (const char *name)
ee890fe2
SS
147{
148 int c;
149 while ((c = *name++) != '\0')
6f94a68e 150 if (! ISIDNUM (c) && c != '.' && c != '$')
ee890fe2
SS
151 return 1;
152 return 0;
153}
154
16515e5c 155/* Return true if SYM_REF can be used without an indirection. */
11abc112
MM
156static int
157machopic_symbol_defined_p (rtx sym_ref)
ee890fe2 158{
16515e5c
AP
159 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
160 return true;
161
162 /* If a symbol references local and is not an extern to this
163 file, then the symbol might be able to declared as defined. */
164 if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
165 {
166 /* If the symbol references a variable and the variable is a
167 common symbol, then this symbol is not defined. */
168 if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
169 {
170 tree decl = SYMBOL_REF_DECL (sym_ref);
171 if (!decl)
172 return true;
173 if (DECL_COMMON (decl))
174 return false;
175 }
176 return true;
177 }
178 return false;
ee890fe2
SS
179}
180
11abc112
MM
181/* This module assumes that (const (symbol_ref "foo")) is a legal pic
182 reference, which will not be changed. */
9c808aad 183
ee890fe2 184enum machopic_addr_class
11abc112 185machopic_classify_symbol (rtx sym_ref)
ee890fe2 186{
11abc112
MM
187 int flags;
188 bool function_p;
189
190 flags = SYMBOL_REF_FLAGS (sym_ref);
191 function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
192 if (machopic_symbol_defined_p (sym_ref))
083cad55 193 return (function_p
11abc112
MM
194 ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
195 else
083cad55 196 return (function_p
11abc112 197 ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
ee890fe2
SS
198}
199
699c914a
MS
200#ifndef TARGET_FIX_AND_CONTINUE
201#define TARGET_FIX_AND_CONTINUE 0
202#endif
203
204/* Indicate when fix-and-continue style code generation is being used
205 and when a reference to data should be indirected so that it can be
35fd3193 206 rebound in a new translation unit to reference the original instance
699c914a
MS
207 of that data. Symbol names that are for code generation local to
208 the translation unit are bound to the new translation unit;
209 currently this means symbols that begin with L or _OBJC_;
210 otherwise, we indicate that an indirect reference should be made to
211 permit the runtime to rebind new instances of the translation unit
212 to the original instance of the data. */
213
214static int
215indirect_data (rtx sym_ref)
216{
217 int lprefix;
218 const char *name;
219
220 /* If we aren't generating fix-and-continue code, don't do anything special. */
221 if (TARGET_FIX_AND_CONTINUE == 0)
222 return 0;
223
224 /* Otherwise, all symbol except symbols that begin with L or _OBJC_
225 are indirected. Symbols that begin with L and _OBJC_ are always
226 bound to the current translation unit as they are used for
227 generated local data of the translation unit. */
228
229 name = XSTR (sym_ref, 0);
230
231 lprefix = (((name[0] == '*' || name[0] == '&')
232 && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
ad6aaeb6 233 || (strncmp (name, "_OBJC_", 6) == 0));
699c914a
MS
234
235 return ! lprefix;
236}
237
238
ee890fe2 239static int
11abc112 240machopic_data_defined_p (rtx sym_ref)
ee890fe2 241{
699c914a
MS
242 if (indirect_data (sym_ref))
243 return 0;
244
11abc112 245 switch (machopic_classify_symbol (sym_ref))
ee890fe2
SS
246 {
247 case MACHOPIC_DEFINED_DATA:
0e1ad529 248 case MACHOPIC_DEFINED_FUNCTION:
ee890fe2
SS
249 return 1;
250 default:
251 return 0;
252 }
253}
254
ee890fe2 255void
11abc112 256machopic_define_symbol (rtx mem)
ee890fe2 257{
11abc112 258 rtx sym_ref;
083cad55 259
992d08b1 260 gcc_assert (GET_CODE (mem) == MEM);
11abc112
MM
261 sym_ref = XEXP (mem, 0);
262 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
ee890fe2
SS
263}
264
6788f5ca 265static GTY(()) char * function_base;
ee890fe2 266
92c1a778 267const char *
9c808aad 268machopic_function_base_name (void)
ee890fe2 269{
ab82a49f 270 /* if dynamic-no-pic is on, we should not get here */
992d08b1 271 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
ee890fe2 272
1622229c 273 if (function_base == NULL)
9c808aad 274 function_base =
1622229c
AP
275 (char *) ggc_alloc_string ("<pic base>", sizeof ("<pic base>"));
276
277 current_function_uses_pic_offset_table = 1;
278
279 return function_base;
280}
281
11abc112
MM
282/* Return a SYMBOL_REF for the PIC function base. */
283
284rtx
285machopic_function_base_sym (void)
286{
287 rtx sym_ref;
288
289 sym_ref = gen_rtx_SYMBOL_REF (Pmode, machopic_function_base_name ());
083cad55 290 SYMBOL_REF_FLAGS (sym_ref)
11abc112
MM
291 |= (MACHO_SYMBOL_FLAG_VARIABLE | MACHO_SYMBOL_FLAG_DEFINED);
292 return sym_ref;
293}
294
14a07c92
PB
295/* Return either ORIG or (const:P (minus:P ORIG PIC_BASE)), depending
296 on whether pic_base is NULL or not. */
297static inline rtx
298gen_pic_offset (rtx orig, rtx pic_base)
299{
300 if (!pic_base)
301 return orig;
302 else
303 return gen_rtx_CONST (Pmode, gen_rtx_MINUS (Pmode, orig, pic_base));
304}
305
1622229c
AP
306static GTY(()) const char * function_base_func_name;
307static GTY(()) int current_pic_label_num;
308
309void
310machopic_output_function_base_name (FILE *file)
311{
312 const char *current_name;
313
ff482c8d 314 /* If dynamic-no-pic is on, we should not get here. */
992d08b1 315 gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
9c808aad 316 current_name =
1622229c 317 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
6788f5ca 318 if (function_base_func_name != current_name)
ee890fe2 319 {
ee890fe2 320 ++current_pic_label_num;
6788f5ca 321 function_base_func_name = current_name;
ee890fe2 322 }
1622229c 323 fprintf (file, "\"L%011d$pb\"", current_pic_label_num);
ee890fe2
SS
324}
325
11abc112
MM
326/* The suffix attached to non-lazy pointer symbols. */
327#define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
328/* The suffix attached to stub symbols. */
329#define STUB_SUFFIX "$stub"
ee890fe2 330
11abc112 331typedef struct machopic_indirection GTY (())
ee890fe2 332{
11abc112
MM
333 /* The SYMBOL_REF for the entity referenced. */
334 rtx symbol;
1adaa117
GK
335 /* The name of the stub or non-lazy pointer. */
336 const char * ptr_name;
11abc112
MM
337 /* True iff this entry is for a stub (as opposed to a non-lazy
338 pointer). */
339 bool stub_p;
340 /* True iff this stub or pointer pointer has been referenced. */
341 bool used;
342} machopic_indirection;
343
344/* A table mapping stub names and non-lazy pointer names to
345 SYMBOL_REFs for the stubbed-to and pointed-to entities. */
346
083cad55 347static GTY ((param_is (struct machopic_indirection))) htab_t
11abc112
MM
348 machopic_indirections;
349
350/* Return a hash value for a SLOT in the indirections hash table. */
351
352static hashval_t
353machopic_indirection_hash (const void *slot)
354{
355 const machopic_indirection *p = (const machopic_indirection *) slot;
1adaa117 356 return htab_hash_string (p->ptr_name);
11abc112 357}
9c808aad 358
11abc112
MM
359/* Returns true if the KEY is the same as that associated with
360 SLOT. */
361
362static int
363machopic_indirection_eq (const void *slot, const void *key)
364{
1adaa117 365 return strcmp (((const machopic_indirection *) slot)->ptr_name, key) == 0;
11abc112 366}
ee890fe2 367
11abc112
MM
368/* Return the name of the non-lazy pointer (if STUB_P is false) or
369 stub (if STUB_B is true) corresponding to the given name. */
df56a27f 370
11abc112
MM
371const char *
372machopic_indirection_name (rtx sym_ref, bool stub_p)
373{
374 char *buffer;
375 const char *name = XSTR (sym_ref, 0);
1adaa117 376 size_t namelen = strlen (name);
11abc112 377 machopic_indirection *p;
1adaa117 378 void ** slot;
3d20d4d8
MS
379 bool saw_star = false;
380 bool needs_quotes;
381 const char *suffix;
382 const char *prefix = user_label_prefix;
383 const char *quote = "";
0a6a4494
AO
384 tree id;
385
386 id = maybe_get_identifier (name);
387 if (id)
388 {
389 tree id_orig = id;
390
391 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
392 id = TREE_CHAIN (id);
393 if (id != id_orig)
394 {
395 name = IDENTIFIER_POINTER (id);
396 namelen = strlen (name);
397 }
398 }
083cad55 399
3d20d4d8 400 if (name[0] == '*')
df56a27f 401 {
3d20d4d8
MS
402 saw_star = true;
403 prefix = "";
404 ++name;
405 --namelen;
11abc112 406 }
3d20d4d8
MS
407
408 needs_quotes = name_needs_quotes (name);
409 if (needs_quotes)
11abc112 410 {
3d20d4d8 411 quote = "\"";
df56a27f
SS
412 }
413
3d20d4d8
MS
414 if (stub_p)
415 suffix = STUB_SUFFIX;
416 else
417 suffix = NON_LAZY_POINTER_SUFFIX;
418
419 buffer = alloca (strlen ("&L")
420 + strlen (prefix)
421 + namelen
422 + strlen (suffix)
423 + 2 * strlen (quote)
424 + 1 /* '\0' */);
425
426 /* Construct the name of the non-lazy pointer or stub. */
427 sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
428
1adaa117 429 if (!machopic_indirections)
083cad55 430 machopic_indirections = htab_create_ggc (37,
1adaa117
GK
431 machopic_indirection_hash,
432 machopic_indirection_eq,
433 /*htab_del=*/NULL);
083cad55 434
1adaa117
GK
435 slot = htab_find_slot_with_hash (machopic_indirections, buffer,
436 htab_hash_string (buffer), INSERT);
437 if (*slot)
438 {
439 p = (machopic_indirection *) *slot;
440 }
441 else
11abc112 442 {
11abc112
MM
443 p = (machopic_indirection *) ggc_alloc (sizeof (machopic_indirection));
444 p->symbol = sym_ref;
1adaa117 445 p->ptr_name = xstrdup (buffer);
11abc112 446 p->stub_p = stub_p;
1adaa117
GK
447 p->used = false;
448 *slot = p;
11abc112 449 }
083cad55 450
1adaa117 451 return p->ptr_name;
ee890fe2
SS
452}
453
11abc112 454/* Return the name of the stub for the mcount function. */
ee890fe2 455
11abc112
MM
456const char*
457machopic_mcount_stub_name (void)
ee890fe2 458{
76f60aa5
AP
459 rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
460 return machopic_indirection_name (symbol, /*stub_p=*/true);
ee890fe2
SS
461}
462
11abc112
MM
463/* If NAME is the name of a stub or a non-lazy pointer , mark the stub
464 or non-lazy pointer as used -- and mark the object to which the
465 pointer/stub refers as used as well, since the pointer/stub will
466 emit a reference to it. */
467
ee890fe2 468void
11abc112 469machopic_validate_stub_or_non_lazy_ptr (const char *name)
ee890fe2 470{
11abc112 471 machopic_indirection *p;
083cad55
EC
472
473 p = ((machopic_indirection *)
1adaa117
GK
474 (htab_find_with_hash (machopic_indirections, name,
475 htab_hash_string (name))));
476 if (p && ! p->used)
11abc112 477 {
1adaa117
GK
478 const char *real_name;
479 tree id;
083cad55 480
1adaa117
GK
481 p->used = true;
482
ca472546
GK
483 /* Do what output_addr_const will do when we actually call it. */
484 if (SYMBOL_REF_DECL (p->symbol))
485 mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
486
487 real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
083cad55 488
1adaa117
GK
489 id = maybe_get_identifier (real_name);
490 if (id)
491 mark_referenced (id);
11abc112 492 }
ee890fe2
SS
493}
494
495/* Transform ORIG, which may be any data source, to the corresponding
496 source using indirections. */
497
498rtx
9c808aad 499machopic_indirect_data_reference (rtx orig, rtx reg)
ee890fe2
SS
500{
501 rtx ptr_ref = orig;
9c808aad 502
ee890fe2
SS
503 if (! MACHOPIC_INDIRECT)
504 return orig;
505
506 if (GET_CODE (orig) == SYMBOL_REF)
507 {
11abc112 508 int defined = machopic_data_defined_p (orig);
ab82a49f
AP
509
510 if (defined && MACHO_DYNAMIC_NO_PIC_P)
511 {
512#if defined (TARGET_TOC)
4f8dbd34
AP
513 /* Create a new register for CSE opportunities. */
514 rtx hi_reg = (no_new_pseudos ? reg : gen_reg_rtx (Pmode));
515 emit_insn (gen_macho_high (hi_reg, orig));
516 emit_insn (gen_macho_low (reg, hi_reg, orig));
ab82a49f
AP
517#else
518 /* some other cpu -- writeme! */
992d08b1 519 gcc_unreachable ();
ab82a49f
AP
520#endif
521 return reg;
522 }
523 else if (defined)
ee890fe2 524 {
7ae8cf75 525#if defined (TARGET_TOC) || defined (HAVE_lo_sum)
11abc112 526 rtx pic_base = machopic_function_base_sym ();
14a07c92 527 rtx offset = gen_pic_offset (orig, pic_base);
7ae8cf75 528#endif
ee890fe2
SS
529
530#if defined (TARGET_TOC) /* i.e., PowerPC */
d9b46dfb 531 rtx hi_sum_reg = (no_new_pseudos ? reg : gen_reg_rtx (Pmode));
ee890fe2 532
992d08b1 533 gcc_assert (reg);
ee890fe2 534
6f94a68e
GK
535 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
536 gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
537 gen_rtx_HIGH (Pmode, offset))));
538 emit_insn (gen_rtx_SET (Pmode, reg,
539 gen_rtx_LO_SUM (Pmode, hi_sum_reg, offset)));
ee890fe2
SS
540
541 orig = reg;
542#else
543#if defined (HAVE_lo_sum)
992d08b1 544 gcc_assert (reg);
ee890fe2 545
6f94a68e
GK
546 emit_insn (gen_rtx_SET (VOIDmode, reg,
547 gen_rtx_HIGH (Pmode, offset)));
548 emit_insn (gen_rtx_SET (VOIDmode, reg,
549 gen_rtx_LO_SUM (Pmode, reg, offset)));
550 emit_insn (gen_rtx_USE (VOIDmode, pic_offset_table_rtx));
ee890fe2 551
6f94a68e 552 orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
ee890fe2
SS
553#endif
554#endif
555 return orig;
556 }
557
828a4fe4 558 ptr_ref = (gen_rtx_SYMBOL_REF
083cad55 559 (Pmode,
11abc112 560 machopic_indirection_name (orig, /*stub_p=*/false)));
ee890fe2 561
c185c797 562 SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
c0d594f1 563
542a8afa 564 ptr_ref = gen_const_mem (Pmode, ptr_ref);
828a4fe4 565 machopic_define_symbol (ptr_ref);
ee890fe2
SS
566
567 return ptr_ref;
568 }
569 else if (GET_CODE (orig) == CONST)
570 {
571 rtx base, result;
572
573 /* legitimize both operands of the PLUS */
574 if (GET_CODE (XEXP (orig, 0)) == PLUS)
575 {
576 base = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 0),
577 reg);
578 orig = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 1),
579 (base == reg ? 0 : reg));
580 }
9c808aad 581 else
ee890fe2
SS
582 return orig;
583
584 if (MACHOPIC_PURE && GET_CODE (orig) == CONST_INT)
ed8908e7 585 result = plus_constant (base, INTVAL (orig));
ee890fe2 586 else
6f94a68e 587 result = gen_rtx_PLUS (Pmode, base, orig);
ee890fe2 588
ee890fe2
SS
589 if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
590 {
591 if (reg)
592 {
593 emit_move_insn (reg, result);
594 result = reg;
595 }
596 else
597 {
598 result = force_reg (GET_MODE (result), result);
599 }
600 }
601
602 return result;
603
604 }
605 else if (GET_CODE (orig) == MEM)
606 XEXP (ptr_ref, 0) = machopic_indirect_data_reference (XEXP (orig, 0), reg);
607 /* When the target is i386, this code prevents crashes due to the
608 compiler's ignorance on how to move the PIC base register to
609 other registers. (The reload phase sometimes introduces such
610 insns.) */
611 else if (GET_CODE (orig) == PLUS
612 && GET_CODE (XEXP (orig, 0)) == REG
613 && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
614#ifdef I386
615 /* Prevent the same register from being erroneously used
616 as both the base and index registers. */
617 && GET_CODE (XEXP (orig, 1)) == CONST
618#endif
619 && reg)
620 {
621 emit_move_insn (reg, XEXP (orig, 0));
622 XEXP (ptr_ref, 0) = reg;
623 }
624 return ptr_ref;
625}
626
ee890fe2
SS
627/* Transform TARGET (a MEM), which is a function call target, to the
628 corresponding symbol_stub if necessary. Return a new MEM. */
629
630rtx
9c808aad 631machopic_indirect_call_target (rtx target)
ee890fe2
SS
632{
633 if (GET_CODE (target) != MEM)
634 return target;
635
083cad55 636 if (MACHOPIC_INDIRECT
11abc112
MM
637 && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
638 && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
639 & MACHO_SYMBOL_FLAG_DEFINED))
9c808aad 640 {
11abc112 641 rtx sym_ref = XEXP (target, 0);
083cad55 642 const char *stub_name = machopic_indirection_name (sym_ref,
11abc112
MM
643 /*stub_p=*/true);
644 enum machine_mode mode = GET_MODE (sym_ref);
083cad55 645
11abc112 646 XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
c185c797 647 SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
389fdba0 648 MEM_READONLY_P (target) = 1;
542a8afa 649 MEM_NOTRAP_P (target) = 1;
ee890fe2
SS
650 }
651
652 return target;
653}
654
655rtx
9c808aad 656machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
ee890fe2
SS
657{
658 rtx pic_ref = orig;
659
ab82a49f 660 if (! MACHOPIC_INDIRECT)
ee890fe2
SS
661 return orig;
662
663 /* First handle a simple SYMBOL_REF or LABEL_REF */
664 if (GET_CODE (orig) == LABEL_REF
665 || (GET_CODE (orig) == SYMBOL_REF
666 ))
667 {
668 /* addr(foo) = &func+(foo-func) */
669 rtx pic_base;
670
671 orig = machopic_indirect_data_reference (orig, reg);
672
9c808aad 673 if (GET_CODE (orig) == PLUS
ee890fe2
SS
674 && GET_CODE (XEXP (orig, 0)) == REG)
675 {
676 if (reg == 0)
677 return force_reg (mode, orig);
678
679 emit_move_insn (reg, orig);
680 return reg;
9c808aad 681 }
ee890fe2 682
14a07c92 683 /* if dynamic-no-pic we don't have a pic base */
ab82a49f 684 if (MACHO_DYNAMIC_NO_PIC_P)
14a07c92 685 pic_base = NULL;
ab82a49f 686 else
11abc112 687 pic_base = machopic_function_base_sym ();
ee890fe2
SS
688
689 if (GET_CODE (orig) == MEM)
690 {
691 if (reg == 0)
692 {
992d08b1
NS
693 gcc_assert (!reload_in_progress);
694 reg = gen_reg_rtx (Pmode);
ee890fe2 695 }
9c808aad 696
ee890fe2 697#ifdef HAVE_lo_sum
ab82a49f
AP
698 if (MACHO_DYNAMIC_NO_PIC_P
699 && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
700 || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
701 {
702#if defined (TARGET_TOC) /* ppc */
703 rtx temp_reg = (no_new_pseudos) ? reg : gen_reg_rtx (Pmode);
704 rtx asym = XEXP (orig, 0);
705 rtx mem;
706
b8a55285 707 emit_insn (gen_macho_high (temp_reg, asym));
542a8afa
RH
708 mem = gen_const_mem (GET_MODE (orig),
709 gen_rtx_LO_SUM (Pmode, temp_reg, asym));
6f94a68e 710 emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
ab82a49f
AP
711#else
712 /* Some other CPU -- WriteMe! but right now there are no other platform that can use dynamic-no-pic */
992d08b1 713 gcc_unreachable ();
ab82a49f
AP
714#endif
715 pic_ref = reg;
716 }
717 else
9c808aad 718 if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
ee890fe2
SS
719 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
720 {
14a07c92 721 rtx offset = gen_pic_offset (XEXP (orig, 0), pic_base);
ee890fe2
SS
722#if defined (TARGET_TOC) /* i.e., PowerPC */
723 /* Generating a new reg may expose opportunities for
724 common subexpression elimination. */
49bd1d27 725 rtx hi_sum_reg = no_new_pseudos ? reg : gen_reg_rtx (Pmode);
6f94a68e
GK
726 rtx mem;
727 rtx insn;
728 rtx sum;
083cad55 729
6f94a68e
GK
730 sum = gen_rtx_HIGH (Pmode, offset);
731 if (! MACHO_DYNAMIC_NO_PIC_P)
732 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
ee890fe2 733
6f94a68e
GK
734 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
735
542a8afa 736 mem = gen_const_mem (GET_MODE (orig),
083cad55 737 gen_rtx_LO_SUM (Pmode,
542a8afa 738 hi_sum_reg, offset));
6f94a68e 739 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
083cad55 740 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, pic_ref,
6f94a68e
GK
741 REG_NOTES (insn));
742
743 pic_ref = reg;
ee890fe2 744#else
6f94a68e 745 emit_insn (gen_rtx_USE (VOIDmode,
083cad55 746 gen_rtx_REG (Pmode,
6f94a68e
GK
747 PIC_OFFSET_TABLE_REGNUM)));
748
749 emit_insn (gen_rtx_SET (VOIDmode, reg,
750 gen_rtx_HIGH (Pmode,
083cad55 751 gen_rtx_CONST (Pmode,
6f94a68e
GK
752 offset))));
753 emit_insn (gen_rtx_SET (VOIDmode, reg,
754 gen_rtx_LO_SUM (Pmode, reg,
755 gen_rtx_CONST (Pmode, offset))));
756 pic_ref = gen_rtx_PLUS (Pmode,
757 pic_offset_table_rtx, reg);
ee890fe2
SS
758#endif
759 }
760 else
761#endif /* HAVE_lo_sum */
762 {
763 rtx pic = pic_offset_table_rtx;
764 if (GET_CODE (pic) != REG)
765 {
766 emit_move_insn (reg, pic);
767 pic = reg;
768 }
769#if 0
6f94a68e 770 emit_insn (gen_rtx_USE (VOIDmode,
083cad55 771 gen_rtx_REG (Pmode,
6f94a68e 772 PIC_OFFSET_TABLE_REGNUM)));
ee890fe2
SS
773#endif
774
7d072037
SH
775 if (reload_in_progress)
776 regs_ever_live[REGNO (pic)] = 1;
14a07c92
PB
777 pic_ref = gen_rtx_PLUS (Pmode, pic,
778 gen_pic_offset (XEXP (orig, 0),
779 pic_base));
ee890fe2 780 }
9c808aad 781
ee890fe2 782#if !defined (TARGET_TOC)
ee890fe2 783 emit_move_insn (reg, pic_ref);
542a8afa 784 pic_ref = gen_const_mem (GET_MODE (orig), reg);
f9b0ac3b 785#endif
ee890fe2
SS
786 }
787 else
788 {
789
790#ifdef HAVE_lo_sum
9c808aad 791 if (GET_CODE (orig) == SYMBOL_REF
ee890fe2
SS
792 || GET_CODE (orig) == LABEL_REF)
793 {
14a07c92 794 rtx offset = gen_pic_offset (orig, pic_base);
ee890fe2
SS
795#if defined (TARGET_TOC) /* i.e., PowerPC */
796 rtx hi_sum_reg;
797
798 if (reg == 0)
799 {
992d08b1
NS
800 gcc_assert (!reload_in_progress);
801 reg = gen_reg_rtx (Pmode);
ee890fe2 802 }
9c808aad 803
ee890fe2
SS
804 hi_sum_reg = reg;
805
6f94a68e
GK
806 emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
807 (MACHO_DYNAMIC_NO_PIC_P)
808 ? gen_rtx_HIGH (Pmode, offset)
809 : gen_rtx_PLUS (Pmode,
810 pic_offset_table_rtx,
083cad55 811 gen_rtx_HIGH (Pmode,
6f94a68e
GK
812 offset))));
813 emit_insn (gen_rtx_SET (VOIDmode, reg,
814 gen_rtx_LO_SUM (Pmode,
815 hi_sum_reg, offset)));
ee890fe2
SS
816 pic_ref = reg;
817#else
6f94a68e
GK
818 emit_insn (gen_rtx_SET (VOIDmode, reg,
819 gen_rtx_HIGH (Pmode, offset)));
820 emit_insn (gen_rtx_SET (VOIDmode, reg,
821 gen_rtx_LO_SUM (Pmode, reg, offset)));
822 pic_ref = gen_rtx_PLUS (Pmode,
823 pic_offset_table_rtx, reg);
ee890fe2
SS
824#endif
825 }
826 else
827#endif /* HAVE_lo_sum */
828 {
83cf88cb
AP
829 if (REG_P (orig)
830 || GET_CODE (orig) == SUBREG)
ee890fe2
SS
831 {
832 return orig;
833 }
834 else
835 {
836 rtx pic = pic_offset_table_rtx;
837 if (GET_CODE (pic) != REG)
838 {
839 emit_move_insn (reg, pic);
840 pic = reg;
841 }
842#if 0
6f94a68e
GK
843 emit_insn (gen_rtx_USE (VOIDmode,
844 pic_offset_table_rtx));
ee890fe2 845#endif
7d072037
SH
846 if (reload_in_progress)
847 regs_ever_live[REGNO (pic)] = 1;
6f94a68e
GK
848 pic_ref = gen_rtx_PLUS (Pmode,
849 pic,
14a07c92 850 gen_pic_offset (orig, pic_base));
ee890fe2
SS
851 }
852 }
853 }
854
ee890fe2
SS
855 if (GET_CODE (pic_ref) != REG)
856 {
857 if (reg != 0)
858 {
859 emit_move_insn (reg, pic_ref);
860 return reg;
861 }
862 else
863 {
864 return force_reg (mode, pic_ref);
865 }
866 }
867 else
868 {
869 return pic_ref;
870 }
871 }
872
873 else if (GET_CODE (orig) == SYMBOL_REF)
874 return orig;
875
876 else if (GET_CODE (orig) == PLUS
877 && (GET_CODE (XEXP (orig, 0)) == MEM
878 || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
879 || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
880 && XEXP (orig, 0) != pic_offset_table_rtx
881 && GET_CODE (XEXP (orig, 1)) != REG)
9c808aad 882
ee890fe2
SS
883 {
884 rtx base;
885 int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
886
887 base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
888 orig = machopic_legitimize_pic_address (XEXP (orig, 1),
889 Pmode, (base == reg ? 0 : reg));
890 if (GET_CODE (orig) == CONST_INT)
891 {
ed8908e7 892 pic_ref = plus_constant (base, INTVAL (orig));
ee890fe2
SS
893 is_complex = 1;
894 }
895 else
6f94a68e 896 pic_ref = gen_rtx_PLUS (Pmode, base, orig);
ee890fe2 897
ee890fe2
SS
898 if (reg && is_complex)
899 {
900 emit_move_insn (reg, pic_ref);
901 pic_ref = reg;
902 }
903 /* Likewise, should we set special REG_NOTEs here? */
904 }
905
906 else if (GET_CODE (orig) == CONST)
907 {
908 return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
909 }
910
911 else if (GET_CODE (orig) == MEM
912 && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
913 {
914 rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
389fdba0 915 addr = replace_equiv_address (orig, addr);
ee890fe2
SS
916 emit_move_insn (reg, addr);
917 pic_ref = reg;
918 }
919
920 return pic_ref;
921}
922
11abc112
MM
923/* Output the stub or non-lazy pointer in *SLOT, if it has been used.
924 DATA is the FILE* for assembly output. Called from
925 htab_traverse. */
ee890fe2 926
11abc112
MM
927static int
928machopic_output_indirection (void **slot, void *data)
ee890fe2 929{
11abc112
MM
930 machopic_indirection *p = *((machopic_indirection **) slot);
931 FILE *asm_out_file = (FILE *) data;
932 rtx symbol;
933 const char *sym_name;
934 const char *ptr_name;
083cad55 935
11abc112
MM
936 if (!p->used)
937 return 1;
ee890fe2 938
11abc112
MM
939 symbol = p->symbol;
940 sym_name = XSTR (symbol, 0);
1adaa117 941 ptr_name = p->ptr_name;
083cad55 942
11abc112 943 if (p->stub_p)
ee890fe2 944 {
ee890fe2
SS
945 char *sym;
946 char *stub;
0a6a4494
AO
947 tree id;
948
949 id = maybe_get_identifier (sym_name);
950 if (id)
951 {
952 tree id_orig = id;
953
954 while (IDENTIFIER_TRANSPARENT_ALIAS (id))
955 id = TREE_CHAIN (id);
956 if (id != id_orig)
957 sym_name = IDENTIFIER_POINTER (id);
958 }
ee890fe2 959
ee890fe2
SS
960 sym = alloca (strlen (sym_name) + 2);
961 if (sym_name[0] == '*' || sym_name[0] == '&')
962 strcpy (sym, sym_name + 1);
963 else if (sym_name[0] == '-' || sym_name[0] == '+')
9c808aad 964 strcpy (sym, sym_name);
ee890fe2 965 else
789a4ea3 966 sprintf (sym, "%s%s", user_label_prefix, sym_name);
ee890fe2 967
11abc112
MM
968 stub = alloca (strlen (ptr_name) + 2);
969 if (ptr_name[0] == '*' || ptr_name[0] == '&')
970 strcpy (stub, ptr_name + 1);
ee890fe2 971 else
11abc112 972 sprintf (stub, "%s%s", user_label_prefix, ptr_name);
ee890fe2 973
ca472546 974 machopic_output_stub (asm_out_file, sym, stub);
ee890fe2 975 }
699c914a 976 else if (! indirect_data (symbol)
156a126c
MS
977 && (machopic_symbol_defined_p (symbol)
978 || SYMBOL_REF_LOCAL_P (symbol)))
ee890fe2 979 {
d6b5193b 980 switch_to_section (data_section);
11abc112
MM
981 assemble_align (GET_MODE_ALIGNMENT (Pmode));
982 assemble_label (ptr_name);
983 assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
984 GET_MODE_SIZE (Pmode),
985 GET_MODE_ALIGNMENT (Pmode), 1);
ee890fe2 986 }
11abc112
MM
987 else
988 {
699c914a
MS
989 rtx init = const0_rtx;
990
56c779bc 991 switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
11abc112
MM
992 assemble_name (asm_out_file, ptr_name);
993 fprintf (asm_out_file, ":\n");
083cad55 994
11abc112
MM
995 fprintf (asm_out_file, "\t.indirect_symbol ");
996 assemble_name (asm_out_file, sym_name);
997 fprintf (asm_out_file, "\n");
083cad55 998
699c914a
MS
999 /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1000 have their symbol name instead of 0 in the second entry of
1001 the non-lazy symbol pointer data structure when they are
1002 defined. This allows the runtime to rebind newer instances
1003 of the translation unit with the original instance of the
d6ff8575 1004 symbol. */
699c914a
MS
1005
1006 if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1007 && machopic_symbol_defined_p (symbol))
1008 init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1009
1010 assemble_integer (init, GET_MODE_SIZE (Pmode),
11abc112
MM
1011 GET_MODE_ALIGNMENT (Pmode), 1);
1012 }
083cad55 1013
11abc112
MM
1014 return 1;
1015}
1016
1017void
1018machopic_finish (FILE *asm_out_file)
1019{
1020 if (machopic_indirections)
699c914a 1021 htab_traverse_noresize (machopic_indirections,
11abc112
MM
1022 machopic_output_indirection,
1023 asm_out_file);
ee890fe2
SS
1024}
1025
9c808aad
AJ
1026int
1027machopic_operand_p (rtx op)
ee890fe2
SS
1028{
1029 if (MACHOPIC_JUST_INDIRECT)
1030 {
1031 while (GET_CODE (op) == CONST)
1032 op = XEXP (op, 0);
1033
1034 if (GET_CODE (op) == SYMBOL_REF)
11abc112 1035 return machopic_symbol_defined_p (op);
ee890fe2
SS
1036 else
1037 return 0;
1038 }
1039
1040 while (GET_CODE (op) == CONST)
1041 op = XEXP (op, 0);
1042
1043 if (GET_CODE (op) == MINUS
1044 && GET_CODE (XEXP (op, 0)) == SYMBOL_REF
1045 && GET_CODE (XEXP (op, 1)) == SYMBOL_REF
11abc112
MM
1046 && machopic_symbol_defined_p (XEXP (op, 0))
1047 && machopic_symbol_defined_p (XEXP (op, 1)))
ee890fe2
SS
1048 return 1;
1049
ee890fe2
SS
1050 return 0;
1051}
df56a27f
SS
1052
1053/* This function records whether a given name corresponds to a defined
1054 or undefined function or variable, for machopic_classify_ident to
1055 use later. */
1056
1057void
9c808aad 1058darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
df56a27f 1059{
353e51f8 1060 rtx sym_ref;
df56a27f 1061
e1a4211d
SS
1062 /* Do the standard encoding things first. */
1063 default_encode_section_info (decl, rtl, first);
1064
11abc112
MM
1065 if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1066 return;
e1a4211d 1067
11abc112
MM
1068 sym_ref = XEXP (rtl, 0);
1069 if (TREE_CODE (decl) == VAR_DECL)
1070 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1071
1072 if (!DECL_EXTERNAL (decl)
f1a66265 1073 && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
a9b0b825 1074 && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
df56a27f
SS
1075 && ((TREE_STATIC (decl)
1076 && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
bfbdca0b 1077 || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
abe72dd8 1078 && DECL_INITIAL (decl) != error_mark_node)))
11abc112 1079 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
699c914a 1080
d6ff8575 1081 if (! TREE_PUBLIC (decl))
699c914a 1082 SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
4e08ba6c 1083}
2cc07db4 1084
8e3e233b
DP
1085void
1086darwin_mark_decl_preserved (const char *name)
1087{
1088 fprintf (asm_out_file, ".no_dead_strip ");
1089 assemble_name (asm_out_file, name);
1090 fputc ('\n', asm_out_file);
1091}
1092
d6b5193b 1093section *
9c808aad
AJ
1094machopic_select_section (tree exp, int reloc,
1095 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
ae46c4e0 1096{
d6b5193b 1097 section *base_section;
0f9bc2d6
GK
1098 bool weak_p = (DECL_P (exp) && DECL_WEAK (exp)
1099 && (lookup_attribute ("weak", DECL_ATTRIBUTES (exp))
1100 || ! lookup_attribute ("weak_import",
1101 DECL_ATTRIBUTES (exp))));
d6b5193b 1102
f1a66265 1103 if (TREE_CODE (exp) == FUNCTION_DECL)
d6b5193b 1104 {
c543ca49 1105 if (reloc == 1)
d6b5193b 1106 base_section = (weak_p
56c779bc 1107 ? darwin_sections[text_unlikely_coal_section]
d6b5193b
RS
1108 : unlikely_text_section ());
1109 else
56c779bc 1110 base_section = weak_p ? darwin_sections[text_coal_section] : text_section;
d6b5193b 1111 }
f1a66265 1112 else if (decl_readonly_section_1 (exp, reloc, MACHOPIC_INDIRECT))
56c779bc 1113 base_section = weak_p ? darwin_sections[const_coal_section] : darwin_sections[const_section];
3d7964d5 1114 else if (TREE_READONLY (exp) || TREE_CONSTANT (exp))
56c779bc 1115 base_section = weak_p ? darwin_sections[const_data_coal_section] : darwin_sections[const_data_section];
3d7964d5 1116 else
56c779bc 1117 base_section = weak_p ? darwin_sections[data_coal_section] : data_section;
9c808aad 1118
3d7964d5 1119 if (TREE_CODE (exp) == STRING_CST
9c808aad 1120 && ((size_t) TREE_STRING_LENGTH (exp)
3521b33c 1121 == strlen (TREE_STRING_POINTER (exp)) + 1))
56c779bc 1122 return darwin_sections[cstring_section];
3d7964d5
GK
1123 else if ((TREE_CODE (exp) == INTEGER_CST || TREE_CODE (exp) == REAL_CST)
1124 && flag_merge_constants)
ae46c4e0 1125 {
e1e04267 1126 tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
ae46c4e0
RH
1127
1128 if (TREE_CODE (size) == INTEGER_CST &&
1129 TREE_INT_CST_LOW (size) == 4 &&
1130 TREE_INT_CST_HIGH (size) == 0)
56c779bc 1131 return darwin_sections[literal4_section];
ae46c4e0
RH
1132 else if (TREE_CODE (size) == INTEGER_CST &&
1133 TREE_INT_CST_LOW (size) == 8 &&
1134 TREE_INT_CST_HIGH (size) == 0)
56c779bc 1135 return darwin_sections[literal8_section];
ae46c4e0 1136 else
d6b5193b 1137 return base_section;
ae46c4e0
RH
1138 }
1139 else if (TREE_CODE (exp) == CONSTRUCTOR
1140 && TREE_TYPE (exp)
1141 && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE
1142 && TYPE_NAME (TREE_TYPE (exp)))
1143 {
1144 tree name = TYPE_NAME (TREE_TYPE (exp));
1145 if (TREE_CODE (name) == TYPE_DECL)
1146 name = DECL_NAME (name);
c64de75f
ZL
1147
1148 if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1149 {
1150 if (flag_next_runtime)
56c779bc 1151 return darwin_sections[objc_constant_string_object_section];
c64de75f 1152 else
56c779bc 1153 return darwin_sections[objc_string_object_section];
c64de75f 1154 }
ae46c4e0 1155 else
d6b5193b 1156 return base_section;
ae46c4e0
RH
1157 }
1158 else if (TREE_CODE (exp) == VAR_DECL &&
1159 DECL_NAME (exp) &&
1160 TREE_CODE (DECL_NAME (exp)) == IDENTIFIER_NODE &&
1161 IDENTIFIER_POINTER (DECL_NAME (exp)) &&
1162 !strncmp (IDENTIFIER_POINTER (DECL_NAME (exp)), "_OBJC_", 6))
1163 {
1164 const char *name = IDENTIFIER_POINTER (DECL_NAME (exp));
1165
1166 if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
56c779bc 1167 return darwin_sections[objc_cls_meth_section];
ae46c4e0 1168 else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
56c779bc 1169 return darwin_sections[objc_inst_meth_section];
ae46c4e0 1170 else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 20))
56c779bc 1171 return darwin_sections[objc_cat_cls_meth_section];
ae46c4e0 1172 else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 23))
56c779bc 1173 return darwin_sections[objc_cat_inst_meth_section];
ae46c4e0 1174 else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
56c779bc 1175 return darwin_sections[objc_class_vars_section];
ae46c4e0 1176 else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
56c779bc 1177 return darwin_sections[objc_instance_vars_section];
ae46c4e0 1178 else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
56c779bc 1179 return darwin_sections[objc_cat_cls_meth_section];
ae46c4e0 1180 else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
56c779bc 1181 return darwin_sections[objc_class_names_section];
ae46c4e0 1182 else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
56c779bc 1183 return darwin_sections[objc_meth_var_names_section];
ae46c4e0 1184 else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
56c779bc 1185 return darwin_sections[objc_meth_var_types_section];
ae46c4e0 1186 else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
56c779bc 1187 return darwin_sections[objc_cls_refs_section];
ae46c4e0 1188 else if (!strncmp (name, "_OBJC_CLASS_", 12))
56c779bc 1189 return darwin_sections[objc_class_section];
ae46c4e0 1190 else if (!strncmp (name, "_OBJC_METACLASS_", 16))
56c779bc 1191 return darwin_sections[objc_meta_class_section];
ae46c4e0 1192 else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
56c779bc 1193 return darwin_sections[objc_category_section];
ae46c4e0 1194 else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
56c779bc 1195 return darwin_sections[objc_selector_refs_section];
ae46c4e0 1196 else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
56c779bc 1197 return darwin_sections[objc_selector_fixup_section];
ae46c4e0 1198 else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
56c779bc 1199 return darwin_sections[objc_symbols_section];
ae46c4e0 1200 else if (!strncmp (name, "_OBJC_MODULES", 13))
56c779bc 1201 return darwin_sections[objc_module_info_section];
264fa2db 1202 else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
56c779bc 1203 return darwin_sections[objc_image_info_section];
ae46c4e0 1204 else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
56c779bc 1205 return darwin_sections[objc_cat_inst_meth_section];
ae46c4e0 1206 else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
56c779bc 1207 return darwin_sections[objc_cat_cls_meth_section];
ae46c4e0 1208 else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
56c779bc 1209 return darwin_sections[objc_cat_cls_meth_section];
ae46c4e0 1210 else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
56c779bc 1211 return darwin_sections[objc_protocol_section];
ae46c4e0 1212 else
d6b5193b 1213 return base_section;
ae46c4e0
RH
1214 }
1215 else
d6b5193b 1216 return base_section;
ae46c4e0
RH
1217}
1218
b64a1b53 1219/* This can be called with address expressions as "rtx".
991b6592 1220 They must go in "const". */
b64a1b53 1221
d6b5193b 1222section *
9c808aad
AJ
1223machopic_select_rtx_section (enum machine_mode mode, rtx x,
1224 unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
b64a1b53 1225{
b79d4265
SS
1226 if (GET_MODE_SIZE (mode) == 8
1227 && (GET_CODE (x) == CONST_INT
1228 || GET_CODE (x) == CONST_DOUBLE))
56c779bc 1229 return darwin_sections[literal8_section];
b64a1b53
RH
1230 else if (GET_MODE_SIZE (mode) == 4
1231 && (GET_CODE (x) == CONST_INT
1232 || GET_CODE (x) == CONST_DOUBLE))
56c779bc 1233 return darwin_sections[literal4_section];
86b0a4f3 1234 else if (MACHOPIC_INDIRECT
2e53734e
GK
1235 && (GET_CODE (x) == SYMBOL_REF
1236 || GET_CODE (x) == CONST
1237 || GET_CODE (x) == LABEL_REF))
56c779bc 1238 return darwin_sections[const_data_section];
b64a1b53 1239 else
56c779bc 1240 return darwin_sections[const_section];
b64a1b53
RH
1241}
1242
2cc07db4 1243void
9c808aad 1244machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
2cc07db4 1245{
ab82a49f 1246 if (MACHOPIC_INDIRECT)
56c779bc 1247 switch_to_section (darwin_sections[mod_init_section]);
2cc07db4 1248 else
56c779bc 1249 switch_to_section (darwin_sections[constructor_section]);
c8af3574
RH
1250 assemble_align (POINTER_SIZE);
1251 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2cc07db4 1252
ab82a49f 1253 if (! MACHOPIC_INDIRECT)
2cc07db4
RH
1254 fprintf (asm_out_file, ".reference .constructors_used\n");
1255}
1256
1257void
9c808aad 1258machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
2cc07db4 1259{
ab82a49f 1260 if (MACHOPIC_INDIRECT)
56c779bc 1261 switch_to_section (darwin_sections[mod_term_section]);
2cc07db4 1262 else
56c779bc 1263 switch_to_section (darwin_sections[destructor_section]);
c8af3574
RH
1264 assemble_align (POINTER_SIZE);
1265 assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
2cc07db4 1266
ab82a49f 1267 if (! MACHOPIC_INDIRECT)
2cc07db4
RH
1268 fprintf (asm_out_file, ".reference .destructors_used\n");
1269}
e2500fed 1270
5eb99654 1271void
9c808aad 1272darwin_globalize_label (FILE *stream, const char *name)
5eb99654
KG
1273{
1274 if (!!strncmp (name, "_OBJC_", 6))
1275 default_globalize_label (stream, name);
1276}
1277
4746cf84 1278void
083cad55 1279darwin_asm_named_section (const char *name,
c18a5b6c
MM
1280 unsigned int flags ATTRIBUTE_UNUSED,
1281 tree decl ATTRIBUTE_UNUSED)
4746cf84 1282{
f1a66265 1283 fprintf (asm_out_file, "\t.section %s\n", name);
4746cf84
MA
1284}
1285
083cad55 1286void
f1a66265 1287darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
4746cf84 1288{
f1a66265 1289 /* Darwin does not use unique sections. */
4746cf84
MA
1290}
1291
005c1a13
GK
1292/* Handle a "weak_import" attribute; arguments as in
1293 struct attribute_spec.handler. */
1294
1295tree
1296darwin_handle_weak_import_attribute (tree *node, tree name,
1297 tree ARG_UNUSED (args),
1298 int ARG_UNUSED (flags),
1299 bool * no_add_attrs)
1300{
d7001ae5 1301 if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
005c1a13 1302 {
5c498b10
DD
1303 warning (OPT_Wattributes, "%qs attribute ignored",
1304 IDENTIFIER_POINTER (name));
005c1a13
GK
1305 *no_add_attrs = true;
1306 }
1307 else
1308 declare_weak (*node);
1309
1310 return NULL_TREE;
1311}
45cc4783
MS
1312
1313static void
1314no_dead_strip (FILE *file, const char *lab)
1315{
005c1a13 1316 fprintf (file, ".no_dead_strip %s\n", lab);
45cc4783
MS
1317}
1318
083cad55 1319/* Emit a label for an FDE, making it global and/or weak if appropriate.
eeab4d81
MS
1320 The third parameter is nonzero if this is for exception handling.
1321 The fourth parameter is nonzero if this is just a placeholder for an
4746cf84 1322 FDE that we are omitting. */
eeab4d81 1323
083cad55 1324void
eeab4d81 1325darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
4746cf84
MA
1326{
1327 tree id = DECL_ASSEMBLER_NAME (decl)
1328 ? DECL_ASSEMBLER_NAME (decl)
1329 : DECL_NAME (decl);
1330
3d20d4d8 1331 const char *prefix = user_label_prefix;
4746cf84
MA
1332
1333 const char *base = IDENTIFIER_POINTER (id);
1334 unsigned int base_len = IDENTIFIER_LENGTH (id);
1335
1336 const char *suffix = ".eh";
4746cf84
MA
1337
1338 int need_quotes = name_needs_quotes (base);
1339 int quotes_len = need_quotes ? 2 : 0;
eeab4d81
MS
1340 char *lab;
1341
1342 if (! for_eh)
1343 suffix = ".eh1";
4746cf84 1344
5ed6ace5 1345 lab = XNEWVEC (char, strlen (prefix)
3d20d4d8 1346 + base_len + strlen (suffix) + quotes_len + 1);
4746cf84
MA
1347 lab[0] = '\0';
1348
1349 if (need_quotes)
1350 strcat(lab, "\"");
1351 strcat(lab, prefix);
1352 strcat(lab, base);
1353 strcat(lab, suffix);
1354 if (need_quotes)
1355 strcat(lab, "\"");
1356
1357 if (TREE_PUBLIC (decl))
f1a66265 1358 fprintf (file, "\t%s %s\n",
4746cf84
MA
1359 (DECL_VISIBILITY (decl) != VISIBILITY_HIDDEN
1360 ? ".globl"
1361 : ".private_extern"),
1362 lab);
1363
f1a66265
GK
1364 if (DECL_WEAK (decl))
1365 fprintf (file, "\t.weak_definition %s\n", lab);
4746cf84
MA
1366
1367 if (empty)
45cc4783
MS
1368 {
1369 fprintf (file, "%s = 0\n", lab);
1370
1371 /* Mark the absolute .eh and .eh1 style labels as needed to
1372 ensure that we don't dead code strip them and keep such
1373 labels from another instantiation point until we can fix this
1374 properly with group comdat support. */
1375 no_dead_strip (file, lab);
1376 }
4746cf84
MA
1377 else
1378 fprintf (file, "%s:\n", lab);
1379
1380 free (lab);
1381}
1382
083cad55
EC
1383static GTY(()) unsigned long except_table_label_num;
1384
1385void
1386darwin_emit_except_table_label (FILE *file)
1387{
1388 char section_start_label[30];
1389
1390 ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
1391 except_table_label_num++);
1392 ASM_OUTPUT_LABEL (file, section_start_label);
1393}
1394/* Generate a PC-relative reference to a Mach-O non-lazy-symbol. */
eeab4d81 1395
4746cf84
MA
1396void
1397darwin_non_lazy_pcrel (FILE *file, rtx addr)
1398{
4746cf84
MA
1399 const char *nlp_name;
1400
992d08b1 1401 gcc_assert (GET_CODE (addr) == SYMBOL_REF);
4746cf84 1402
11abc112 1403 nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
4746cf84
MA
1404 fputs ("\t.long\t", file);
1405 ASM_OUTPUT_LABELREF (file, nlp_name);
1406 fputs ("-.", file);
1407}
1408
6ce4806b
MA
1409/* Emit an assembler directive to set visibility for a symbol. The
1410 only supported visibilities are VISIBILITY_DEFAULT and
1411 VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
1412 extern". There is no MACH-O equivalent of ELF's
1413 VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
1414
083cad55 1415void
6ce4806b
MA
1416darwin_assemble_visibility (tree decl, int vis)
1417{
1418 if (vis == VISIBILITY_DEFAULT)
1419 ;
1420 else if (vis == VISIBILITY_HIDDEN)
1421 {
1422 fputs ("\t.private_extern ", asm_out_file);
1423 assemble_name (asm_out_file,
1424 (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
1425 fputs ("\n", asm_out_file);
1426 }
1427 else
5c498b10
DD
1428 warning (OPT_Wattributes, "internal and protected visibility attributes "
1429 "not supported in this configuration; ignored");
6ce4806b
MA
1430}
1431
7606e68f
SS
1432/* Output a difference of two labels that will be an assembly time
1433 constant if the two labels are local. (.long lab1-lab2 will be
1434 very different if lab1 is at the boundary between two sections; it
1435 will be relocated according to the second section, not the first,
1436 so one ends up with a difference between labels in different
1437 sections, which is bad in the dwarf2 eh context for instance.) */
1438
1439static int darwin_dwarf_label_counter;
1440
1441void
59d8fe27 1442darwin_asm_output_dwarf_delta (FILE *file, int size,
9c808aad 1443 const char *lab1, const char *lab2)
7606e68f 1444{
4746cf84
MA
1445 int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
1446 && lab2[0] == '*' && lab2[1] == 'L');
d4b56511 1447 const char *directive = (size == 8 ? ".quad" : ".long");
7606e68f
SS
1448
1449 if (islocaldiff)
1450 fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
1451 else
59d8fe27 1452 fprintf (file, "\t%s\t", directive);
57829bc4 1453 assemble_name_raw (file, lab1);
7606e68f 1454 fprintf (file, "-");
57829bc4 1455 assemble_name_raw (file, lab2);
7606e68f 1456 if (islocaldiff)
59d8fe27 1457 fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
7606e68f
SS
1458}
1459
a5fe455b 1460void
9c808aad 1461darwin_file_end (void)
a5fe455b
ZW
1462{
1463 machopic_finish (asm_out_file);
1464 if (strcmp (lang_hooks.name, "GNU C++") == 0)
1465 {
56c779bc
GK
1466 switch_to_section (darwin_sections[constructor_section]);
1467 switch_to_section (darwin_sections[destructor_section]);
a5fe455b
ZW
1468 ASM_OUTPUT_ALIGN (asm_out_file, 1);
1469 }
92b9a671 1470 fprintf (asm_out_file, "\t.subsections_via_symbols\n");
a5fe455b
ZW
1471}
1472
31920d83
DJ
1473/* Cross-module name binding. Darwin does not support overriding
1474 functions at dynamic-link time. */
1475
1476bool
1477darwin_binds_local_p (tree decl)
1478{
1479 return default_binds_local_p_1 (decl, 0);
1480}
1481
ab0ff804
AP
1482/* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR. Define the
1483 anchor relative to ".", the current section position. We cannot use
1484 the default one because ASM_OUTPUT_DEF is wrong for Darwin. */
1485
1486void
1487darwin_asm_output_anchor (rtx symbol)
1488{
1489 fprintf (asm_out_file, "\t.set\t");
1490 assemble_name (asm_out_file, XSTR (symbol, 0));
1491 fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
1492 SYMBOL_REF_BLOCK_OFFSET (symbol));
1493}
1494
e2500fed 1495#include "gt-darwin.h"