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