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