OSDN Git Service

6c63d7a32b4b89fefdce59ed676c8e1487a01bcb
[pf3gnuchains/gcc-fork.git] / gcc / config / darwin.c
1 /* Functions for generic Darwin as target machine for GNU C compiler.
2    Copyright (C) 1989, 1990, 1991, 1992, 1993, 2000, 2001, 2002, 2003, 2004,
3    2005, 2006, 2007, 2008, 2009, 2010
4    Free Software Foundation, Inc.
5    Contributed by Apple Computer Inc.
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
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"
40 #include "function.h"
41 #include "ggc.h"
42 #include "langhooks.h"
43 #include "target.h"
44 #include "tm_p.h"
45 #include "toplev.h"
46 #include "hashtab.h"
47 #include "df.h"
48 #include "debug.h"
49 #include "obstack.h"
50 #include "lto-streamer.h"
51
52 /* Darwin supports a feature called fix-and-continue, which is used
53    for rapid turn around debugging.  When code is compiled with the
54    -mfix-and-continue flag, two changes are made to the generated code
55    that allow the system to do things that it would normally not be
56    able to do easily.  These changes allow gdb to load in
57    recompilation of a translation unit that has been changed into a
58    running program and replace existing functions and methods of that
59    translation unit with versions of those functions and methods
60    from the newly compiled translation unit.  The new functions access
61    the existing static symbols from the old translation unit, if the
62    symbol existed in the unit to be replaced, and from the new
63    translation unit, otherwise.
64
65    The changes are to insert 5 nops at the beginning of all functions
66    and to use indirection to get at static symbols.  The 5 nops
67    are required by consumers of the generated code.  Currently, gdb
68    uses this to patch in a jump to the overriding function, this
69    allows all uses of the old name to forward to the replacement,
70    including existing function pointers and virtual methods.  See
71    rs6000_emit_prologue for the code that handles the nop insertions.
72
73    The added indirection allows gdb to redirect accesses to static
74    symbols from the newly loaded translation unit to the existing
75    symbol, if any.  @code{static} symbols are special and are handled by
76    setting the second word in the .non_lazy_symbol_pointer data
77    structure to symbol.  See indirect_data for the code that handles
78    the extra indirection, and machopic_output_indirection and its use
79    of MACHO_SYMBOL_STATIC for the code that handles @code{static}
80    symbol indirection.  */
81
82 /* Section names.  */
83 section * darwin_sections[NUM_DARWIN_SECTIONS];
84
85 /* True if we're setting __attribute__ ((ms_struct)).  */
86 int darwin_ms_struct = false;
87
88 /* A get_unnamed_section callback used to switch to an ObjC section.
89    DIRECTIVE is as for output_section_asm_op.  */
90
91 static void
92 output_objc_section_asm_op (const void *directive)
93 {
94   static bool been_here = false;
95
96   if (! been_here)
97     {
98       static const enum darwin_section_enum tomark[] =
99         {
100           /* written, cold -> hot */
101           objc_cat_cls_meth_section,
102           objc_cat_inst_meth_section,
103           objc_string_object_section,
104           objc_constant_string_object_section,
105           objc_selector_refs_section,
106           objc_selector_fixup_section,
107           objc_cls_refs_section,
108           objc_class_section,
109           objc_meta_class_section,
110           /* shared, hot -> cold */
111           objc_cls_meth_section,
112           objc_inst_meth_section,
113           objc_protocol_section,
114           objc_class_names_section,
115           objc_meth_var_types_section,
116           objc_meth_var_names_section,
117           objc_category_section,
118           objc_class_vars_section,
119           objc_instance_vars_section,
120           objc_module_info_section,
121           objc_symbols_section
122         };
123       size_t i;
124
125       been_here = true;
126       for (i = 0; i < ARRAY_SIZE (tomark); i++)
127         switch_to_section (darwin_sections[tomark[i]]);
128     }
129   output_section_asm_op (directive);
130 }
131
132 /* Implement TARGET_ASM_INIT_SECTIONS.  */
133
134 void
135 darwin_init_sections (void)
136 {
137 #define DEF_SECTION(NAME, FLAGS, DIRECTIVE, OBJC)               \
138   darwin_sections[NAME] =                                       \
139     get_unnamed_section (FLAGS, (OBJC                           \
140                                  ? output_objc_section_asm_op   \
141                                  : output_section_asm_op),      \
142                          "\t" DIRECTIVE);
143 #include "config/darwin-sections.def"
144 #undef DEF_SECTION
145
146   readonly_data_section = darwin_sections[const_section];
147   exception_section = darwin_sections[darwin_exception_section];
148   eh_frame_section = darwin_sections[darwin_eh_frame_section];
149 }
150
151 int
152 name_needs_quotes (const char *name)
153 {
154   int c;
155   while ((c = *name++) != '\0')
156     if (! ISIDNUM (c) && c != '.' && c != '$')
157       return 1;
158   return 0;
159 }
160
161 /* Return true if SYM_REF can be used without an indirection.  */
162 static int
163 machopic_symbol_defined_p (rtx sym_ref)
164 {
165   if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_DEFINED)
166     return true;
167
168   /* If a symbol references local and is not an extern to this
169      file, then the symbol might be able to declared as defined.  */
170   if (SYMBOL_REF_LOCAL_P (sym_ref) && ! SYMBOL_REF_EXTERNAL_P (sym_ref))
171     {
172       /* If the symbol references a variable and the variable is a
173          common symbol, then this symbol is not defined.  */
174       if (SYMBOL_REF_FLAGS (sym_ref) & MACHO_SYMBOL_FLAG_VARIABLE)
175         {
176           tree decl = SYMBOL_REF_DECL (sym_ref);
177           if (!decl)
178             return true;
179           if (DECL_COMMON (decl))
180             return false;
181         }
182       return true;
183     }
184   return false;
185 }
186
187 /* This module assumes that (const (symbol_ref "foo")) is a legal pic
188    reference, which will not be changed.  */
189
190 enum machopic_addr_class
191 machopic_classify_symbol (rtx sym_ref)
192 {
193   bool function_p;
194
195   function_p = SYMBOL_REF_FUNCTION_P (sym_ref);
196   if (machopic_symbol_defined_p (sym_ref))
197     return (function_p
198             ? MACHOPIC_DEFINED_FUNCTION : MACHOPIC_DEFINED_DATA);
199   else
200     return (function_p
201             ? MACHOPIC_UNDEFINED_FUNCTION : MACHOPIC_UNDEFINED_DATA);
202 }
203
204 #ifndef TARGET_FIX_AND_CONTINUE
205 #define TARGET_FIX_AND_CONTINUE 0
206 #endif
207
208 /* Indicate when fix-and-continue style code generation is being used
209    and when a reference to data should be indirected so that it can be
210    rebound in a new translation unit to reference the original instance
211    of that data.  Symbol names that are for code generation local to
212    the translation unit are bound to the new translation unit;
213    currently this means symbols that begin with L or _OBJC_;
214    otherwise, we indicate that an indirect reference should be made to
215    permit the runtime to rebind new instances of the translation unit
216    to the original instance of the data.  */
217
218 static int
219 indirect_data (rtx sym_ref)
220 {
221   int lprefix;
222   const char *name;
223
224   /* If we aren't generating fix-and-continue code, don't do anything
225      special.  */
226   if (TARGET_FIX_AND_CONTINUE == 0)
227     return 0;
228
229   /* Otherwise, all symbol except symbols that begin with L or _OBJC_
230      are indirected.  Symbols that begin with L and _OBJC_ are always
231      bound to the current translation unit as they are used for
232      generated local data of the translation unit.  */
233
234   name = XSTR (sym_ref, 0);
235
236   lprefix = (((name[0] == '*' || name[0] == '&')
237               && (name[1] == 'L' || (name[1] == '"' && name[2] == 'L')))
238              || (strncmp (name, "_OBJC_", 6) == 0));
239
240   return ! lprefix;
241 }
242
243
244 static int
245 machopic_data_defined_p (rtx sym_ref)
246 {
247   if (indirect_data (sym_ref))
248     return 0;
249
250   switch (machopic_classify_symbol (sym_ref))
251     {
252     case MACHOPIC_DEFINED_DATA:
253     case MACHOPIC_DEFINED_FUNCTION:
254       return 1;
255     default:
256       return 0;
257     }
258 }
259
260 void
261 machopic_define_symbol (rtx mem)
262 {
263   rtx sym_ref;
264
265   gcc_assert (GET_CODE (mem) == MEM);
266   sym_ref = XEXP (mem, 0);
267   SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
268 }
269
270 /* Return either ORIG or:
271
272      (const:P (unspec:P [ORIG] UNSPEC_MACHOPIC_OFFSET))
273
274    depending on MACHO_DYNAMIC_NO_PIC_P.  */
275 rtx
276 machopic_gen_offset (rtx orig)
277 {
278   if (MACHO_DYNAMIC_NO_PIC_P)
279     return orig;
280   else
281     {
282       /* Play games to avoid marking the function as needing pic if we
283          are being called as part of the cost-estimation process.  */
284       if (current_ir_type () != IR_GIMPLE || currently_expanding_to_rtl)
285         crtl->uses_pic_offset_table = 1;
286       orig = gen_rtx_UNSPEC (Pmode, gen_rtvec (1, orig),
287                              UNSPEC_MACHOPIC_OFFSET);
288       return gen_rtx_CONST (Pmode, orig);
289     }
290 }
291
292 static GTY(()) const char * function_base_func_name;
293 static GTY(()) int current_pic_label_num;
294
295 void
296 machopic_output_function_base_name (FILE *file)
297 {
298   const char *current_name;
299
300   /* If dynamic-no-pic is on, we should not get here.  */
301   gcc_assert (!MACHO_DYNAMIC_NO_PIC_P);
302   current_name =
303     IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (current_function_decl));
304   if (function_base_func_name != current_name)
305     {
306       ++current_pic_label_num;
307       function_base_func_name = current_name;
308     }
309   fprintf (file, "\"L%011d$pb\"", current_pic_label_num);
310 }
311
312 /* The suffix attached to non-lazy pointer symbols.  */
313 #define NON_LAZY_POINTER_SUFFIX "$non_lazy_ptr"
314 /* The suffix attached to stub symbols.  */
315 #define STUB_SUFFIX "$stub"
316
317 typedef struct GTY (()) machopic_indirection
318 {
319   /* The SYMBOL_REF for the entity referenced.  */
320   rtx symbol;
321   /* The name of the stub or non-lazy pointer.  */
322   const char * ptr_name;
323   /* True iff this entry is for a stub (as opposed to a non-lazy
324      pointer).  */
325   bool stub_p;
326   /* True iff this stub or pointer pointer has been referenced.  */
327   bool used;
328 } machopic_indirection;
329
330 /* A table mapping stub names and non-lazy pointer names to
331    SYMBOL_REFs for the stubbed-to and pointed-to entities.  */
332
333 static GTY ((param_is (struct machopic_indirection))) htab_t
334   machopic_indirections;
335
336 /* Return a hash value for a SLOT in the indirections hash table.  */
337
338 static hashval_t
339 machopic_indirection_hash (const void *slot)
340 {
341   const machopic_indirection *p = (const machopic_indirection *) slot;
342   return htab_hash_string (p->ptr_name);
343 }
344
345 /* Returns true if the KEY is the same as that associated with
346    SLOT.  */
347
348 static int
349 machopic_indirection_eq (const void *slot, const void *key)
350 {
351   return strcmp (((const machopic_indirection *) slot)->ptr_name,
352                  (const char *) key) == 0;
353 }
354
355 /* Return the name of the non-lazy pointer (if STUB_P is false) or
356    stub (if STUB_B is true) corresponding to the given name.  */
357
358 const char *
359 machopic_indirection_name (rtx sym_ref, bool stub_p)
360 {
361   char *buffer;
362   const char *name = XSTR (sym_ref, 0);
363   size_t namelen = strlen (name);
364   machopic_indirection *p;
365   void ** slot;
366   bool needs_quotes;
367   const char *suffix;
368   const char *prefix = user_label_prefix;
369   const char *quote = "";
370   tree id;
371
372   id = maybe_get_identifier (name);
373   if (id)
374     {
375       tree id_orig = id;
376
377       while (IDENTIFIER_TRANSPARENT_ALIAS (id))
378         id = TREE_CHAIN (id);
379       if (id != id_orig)
380         {
381           name = IDENTIFIER_POINTER (id);
382           namelen = strlen (name);
383         }
384     }
385
386   if (name[0] == '*')
387     {
388       prefix = "";
389       ++name;
390       --namelen;
391     }
392
393   needs_quotes = name_needs_quotes (name);
394   if (needs_quotes)
395     {
396       quote = "\"";
397     }
398
399   if (stub_p)
400     suffix = STUB_SUFFIX;
401   else
402     suffix = NON_LAZY_POINTER_SUFFIX;
403
404   buffer = XALLOCAVEC (char, strlen ("&L")
405                    + strlen (prefix)
406                    + namelen
407                    + strlen (suffix)
408                    + 2 * strlen (quote)
409                    + 1 /* '\0' */);
410
411   /* Construct the name of the non-lazy pointer or stub.  */
412   sprintf (buffer, "&%sL%s%s%s%s", quote, prefix, name, suffix, quote);
413
414   if (!machopic_indirections)
415     machopic_indirections = htab_create_ggc (37,
416                                              machopic_indirection_hash,
417                                              machopic_indirection_eq,
418                                              /*htab_del=*/NULL);
419
420   slot = htab_find_slot_with_hash (machopic_indirections, buffer,
421                                    htab_hash_string (buffer), INSERT);
422   if (*slot)
423     {
424       p = (machopic_indirection *) *slot;
425     }
426   else
427     {
428       p = (machopic_indirection *) ggc_alloc (sizeof (machopic_indirection));
429       p->symbol = sym_ref;
430       p->ptr_name = xstrdup (buffer);
431       p->stub_p = stub_p;
432       p->used = false;
433       *slot = p;
434     }
435
436   return p->ptr_name;
437 }
438
439 /* Return the name of the stub for the mcount function.  */
440
441 const char*
442 machopic_mcount_stub_name (void)
443 {
444   rtx symbol = gen_rtx_SYMBOL_REF (Pmode, "*mcount");
445   return machopic_indirection_name (symbol, /*stub_p=*/true);
446 }
447
448 /* If NAME is the name of a stub or a non-lazy pointer , mark the stub
449    or non-lazy pointer as used -- and mark the object to which the
450    pointer/stub refers as used as well, since the pointer/stub will
451    emit a reference to it.  */
452
453 void
454 machopic_validate_stub_or_non_lazy_ptr (const char *name)
455 {
456   machopic_indirection *p;
457
458   p = ((machopic_indirection *)
459        (htab_find_with_hash (machopic_indirections, name,
460                              htab_hash_string (name))));
461   if (p && ! p->used)
462     {
463       const char *real_name;
464       tree id;
465
466       p->used = true;
467
468       /* Do what output_addr_const will do when we actually call it.  */
469       if (SYMBOL_REF_DECL (p->symbol))
470         mark_decl_referenced (SYMBOL_REF_DECL (p->symbol));
471
472       real_name = targetm.strip_name_encoding (XSTR (p->symbol, 0));
473
474       id = maybe_get_identifier (real_name);
475       if (id)
476         mark_referenced (id);
477     }
478 }
479
480 /* Transform ORIG, which may be any data source, to the corresponding
481    source using indirections.  */
482
483 rtx
484 machopic_indirect_data_reference (rtx orig, rtx reg)
485 {
486   rtx ptr_ref = orig;
487
488   if (! MACHOPIC_INDIRECT)
489     return orig;
490
491   if (GET_CODE (orig) == SYMBOL_REF)
492     {
493       int defined = machopic_data_defined_p (orig);
494
495       if (defined && MACHO_DYNAMIC_NO_PIC_P)
496         {
497 #if defined (TARGET_TOC)
498           /* Create a new register for CSE opportunities.  */
499           rtx hi_reg = (!can_create_pseudo_p () ? reg : gen_reg_rtx (Pmode));
500           emit_insn (gen_macho_high (hi_reg, orig));
501           emit_insn (gen_macho_low (reg, hi_reg, orig));
502 #else
503            /* some other cpu -- writeme!  */
504            gcc_unreachable ();
505 #endif
506            return reg;
507         }
508       else if (defined)
509         {
510 #if defined (TARGET_TOC) || defined (HAVE_lo_sum)
511           rtx offset = machopic_gen_offset (orig);
512 #endif
513
514 #if defined (TARGET_TOC) /* i.e., PowerPC */
515           rtx hi_sum_reg = (!can_create_pseudo_p ()
516                             ? reg
517                             : gen_reg_rtx (Pmode));
518
519           gcc_assert (reg);
520
521           emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
522                               gen_rtx_PLUS (Pmode, pic_offset_table_rtx,
523                                        gen_rtx_HIGH (Pmode, offset))));
524           emit_insn (gen_rtx_SET (Pmode, reg,
525                                   gen_rtx_LO_SUM (Pmode, hi_sum_reg,
526                                                   copy_rtx (offset))));
527
528           orig = reg;
529 #else
530 #if defined (HAVE_lo_sum)
531           gcc_assert (reg);
532
533           emit_insn (gen_rtx_SET (VOIDmode, reg,
534                                   gen_rtx_HIGH (Pmode, offset)));
535           emit_insn (gen_rtx_SET (VOIDmode, reg,
536                                   gen_rtx_LO_SUM (Pmode, reg,
537                                                   copy_rtx (offset))));
538           emit_use (pic_offset_table_rtx);
539
540           orig = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, reg);
541 #endif
542 #endif
543           return orig;
544         }
545
546       ptr_ref = (gen_rtx_SYMBOL_REF
547                  (Pmode,
548                   machopic_indirection_name (orig, /*stub_p=*/false)));
549
550       SYMBOL_REF_DATA (ptr_ref) = SYMBOL_REF_DATA (orig);
551
552       ptr_ref = gen_const_mem (Pmode, ptr_ref);
553       machopic_define_symbol (ptr_ref);
554
555       return ptr_ref;
556     }
557   else if (GET_CODE (orig) == CONST)
558     {
559       rtx base, result;
560
561       /* legitimize both operands of the PLUS */
562       if (GET_CODE (XEXP (orig, 0)) == PLUS)
563         {
564           base = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 0),
565                                                    reg);
566           orig = machopic_indirect_data_reference (XEXP (XEXP (orig, 0), 1),
567                                                    (base == reg ? 0 : reg));
568         }
569       else
570         return orig;
571
572       if (MACHOPIC_PURE && GET_CODE (orig) == CONST_INT)
573         result = plus_constant (base, INTVAL (orig));
574       else
575         result = gen_rtx_PLUS (Pmode, base, orig);
576
577       if (MACHOPIC_JUST_INDIRECT && GET_CODE (base) == MEM)
578         {
579           if (reg)
580             {
581               emit_move_insn (reg, result);
582               result = reg;
583             }
584           else
585             {
586               result = force_reg (GET_MODE (result), result);
587             }
588         }
589
590       return result;
591
592     }
593   else if (GET_CODE (orig) == MEM)
594     XEXP (ptr_ref, 0) = machopic_indirect_data_reference (XEXP (orig, 0), reg);
595   /* When the target is i386, this code prevents crashes due to the
596      compiler's ignorance on how to move the PIC base register to
597      other registers.  (The reload phase sometimes introduces such
598      insns.)  */
599   else if (GET_CODE (orig) == PLUS
600            && GET_CODE (XEXP (orig, 0)) == REG
601            && REGNO (XEXP (orig, 0)) == PIC_OFFSET_TABLE_REGNUM
602 #ifdef I386
603            /* Prevent the same register from being erroneously used
604               as both the base and index registers.  */
605            && GET_CODE (XEXP (orig, 1)) == CONST
606 #endif
607            && reg)
608     {
609       emit_move_insn (reg, XEXP (orig, 0));
610       XEXP (ptr_ref, 0) = reg;
611     }
612   return ptr_ref;
613 }
614
615 /* Transform TARGET (a MEM), which is a function call target, to the
616    corresponding symbol_stub if necessary.  Return a new MEM.  */
617
618 rtx
619 machopic_indirect_call_target (rtx target)
620 {
621   if (GET_CODE (target) != MEM)
622     return target;
623
624   if (MACHOPIC_INDIRECT
625       && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
626       && !(SYMBOL_REF_FLAGS (XEXP (target, 0))
627            & MACHO_SYMBOL_FLAG_DEFINED))
628     {
629       rtx sym_ref = XEXP (target, 0);
630       const char *stub_name = machopic_indirection_name (sym_ref,
631                                                          /*stub_p=*/true);
632       enum machine_mode mode = GET_MODE (sym_ref);
633
634       XEXP (target, 0) = gen_rtx_SYMBOL_REF (mode, stub_name);
635       SYMBOL_REF_DATA (XEXP (target, 0)) = SYMBOL_REF_DATA (sym_ref);
636       MEM_READONLY_P (target) = 1;
637       MEM_NOTRAP_P (target) = 1;
638     }
639
640   return target;
641 }
642
643 rtx
644 machopic_legitimize_pic_address (rtx orig, enum machine_mode mode, rtx reg)
645 {
646   rtx pic_ref = orig;
647
648   if (! MACHOPIC_INDIRECT)
649     return orig;
650
651   /* First handle a simple SYMBOL_REF or LABEL_REF */
652   if (GET_CODE (orig) == LABEL_REF
653       || (GET_CODE (orig) == SYMBOL_REF
654           ))
655     {
656       /* addr(foo) = &func+(foo-func) */
657       orig = machopic_indirect_data_reference (orig, reg);
658
659       if (GET_CODE (orig) == PLUS
660           && GET_CODE (XEXP (orig, 0)) == REG)
661         {
662           if (reg == 0)
663             return force_reg (mode, orig);
664
665           emit_move_insn (reg, orig);
666           return reg;
667         }
668
669       if (GET_CODE (orig) == MEM)
670         {
671           if (reg == 0)
672             {
673               gcc_assert (!reload_in_progress);
674               reg = gen_reg_rtx (Pmode);
675             }
676
677 #ifdef HAVE_lo_sum
678           if (MACHO_DYNAMIC_NO_PIC_P
679               && (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
680                   || GET_CODE (XEXP (orig, 0)) == LABEL_REF))
681             {
682 #if defined (TARGET_TOC)        /* ppc  */
683               rtx temp_reg = (!can_create_pseudo_p ()
684                               ? reg :
685                               gen_reg_rtx (Pmode));
686               rtx asym = XEXP (orig, 0);
687               rtx mem;
688
689               emit_insn (gen_macho_high (temp_reg, asym));
690               mem = gen_const_mem (GET_MODE (orig),
691                                    gen_rtx_LO_SUM (Pmode, temp_reg,
692                                                    copy_rtx (asym)));
693               emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
694 #else
695               /* Some other CPU -- WriteMe! but right now there are no other
696                  platforms that can use dynamic-no-pic  */
697               gcc_unreachable ();
698 #endif
699               pic_ref = reg;
700             }
701           else
702           if (GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
703               || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
704             {
705               rtx offset = machopic_gen_offset (XEXP (orig, 0));
706 #if defined (TARGET_TOC) /* i.e., PowerPC */
707               /* Generating a new reg may expose opportunities for
708                  common subexpression elimination.  */
709               rtx hi_sum_reg = (!can_create_pseudo_p ()
710                                 ? reg
711                                 : gen_reg_rtx (Pmode));
712               rtx mem;
713               rtx insn;
714               rtx sum;
715
716               sum = gen_rtx_HIGH (Pmode, offset);
717               if (! MACHO_DYNAMIC_NO_PIC_P)
718                 sum = gen_rtx_PLUS (Pmode, pic_offset_table_rtx, sum);
719
720               emit_insn (gen_rtx_SET (Pmode, hi_sum_reg, sum));
721
722               mem = gen_const_mem (GET_MODE (orig),
723                                   gen_rtx_LO_SUM (Pmode,
724                                                   hi_sum_reg,
725                                                   copy_rtx (offset)));
726               insn = emit_insn (gen_rtx_SET (VOIDmode, reg, mem));
727               set_unique_reg_note (insn, REG_EQUAL, pic_ref);
728
729               pic_ref = reg;
730 #else
731               emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
732
733               emit_insn (gen_rtx_SET (VOIDmode, reg,
734                                       gen_rtx_HIGH (Pmode,
735                                                     gen_rtx_CONST (Pmode,
736                                                                    offset))));
737               emit_insn (gen_rtx_SET (VOIDmode, reg,
738                                   gen_rtx_LO_SUM (Pmode, reg,
739                                            gen_rtx_CONST (Pmode,
740                                                           copy_rtx (offset)))));
741               pic_ref = gen_rtx_PLUS (Pmode,
742                                       pic_offset_table_rtx, reg);
743 #endif
744             }
745           else
746 #endif  /* HAVE_lo_sum */
747             {
748               rtx pic = pic_offset_table_rtx;
749               if (GET_CODE (pic) != REG)
750                 {
751                   emit_move_insn (reg, pic);
752                   pic = reg;
753                 }
754 #if 0
755               emit_use (gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM));
756 #endif
757
758               if (reload_in_progress)
759                 df_set_regs_ever_live (REGNO (pic), true);
760               pic_ref = gen_rtx_PLUS (Pmode, pic,
761                                       machopic_gen_offset (XEXP (orig, 0)));
762             }
763
764 #if !defined (TARGET_TOC)
765           emit_move_insn (reg, pic_ref);
766           pic_ref = gen_const_mem (GET_MODE (orig), reg);
767 #endif
768         }
769       else
770         {
771
772 #ifdef HAVE_lo_sum
773           if (GET_CODE (orig) == SYMBOL_REF
774               || GET_CODE (orig) == LABEL_REF)
775             {
776               rtx offset = machopic_gen_offset (orig);
777 #if defined (TARGET_TOC) /* i.e., PowerPC */
778               rtx hi_sum_reg;
779
780               if (reg == 0)
781                 {
782                   gcc_assert (!reload_in_progress);
783                   reg = gen_reg_rtx (Pmode);
784                 }
785
786               hi_sum_reg = reg;
787
788               emit_insn (gen_rtx_SET (Pmode, hi_sum_reg,
789                                       (MACHO_DYNAMIC_NO_PIC_P)
790                                       ? gen_rtx_HIGH (Pmode, offset)
791                                       : gen_rtx_PLUS (Pmode,
792                                                       pic_offset_table_rtx,
793                                                       gen_rtx_HIGH (Pmode,
794                                                                     offset))));
795               emit_insn (gen_rtx_SET (VOIDmode, reg,
796                                       gen_rtx_LO_SUM (Pmode,
797                                                       hi_sum_reg,
798                                                       copy_rtx (offset))));
799               pic_ref = reg;
800 #else
801               emit_insn (gen_rtx_SET (VOIDmode, reg,
802                                       gen_rtx_HIGH (Pmode, offset)));
803               emit_insn (gen_rtx_SET (VOIDmode, reg,
804                                       gen_rtx_LO_SUM (Pmode, reg,
805                                                       copy_rtx (offset))));
806               pic_ref = gen_rtx_PLUS (Pmode,
807                                       pic_offset_table_rtx, reg);
808 #endif
809             }
810           else
811 #endif  /*  HAVE_lo_sum  */
812             {
813               if (REG_P (orig)
814                   || GET_CODE (orig) == SUBREG)
815                 {
816                   return orig;
817                 }
818               else
819                 {
820                   rtx pic = pic_offset_table_rtx;
821                   if (GET_CODE (pic) != REG)
822                     {
823                       emit_move_insn (reg, pic);
824                       pic = reg;
825                     }
826 #if 0
827                   emit_use (pic_offset_table_rtx);
828 #endif
829                   if (reload_in_progress)
830                     df_set_regs_ever_live (REGNO (pic), true);
831                   pic_ref = gen_rtx_PLUS (Pmode,
832                                           pic,
833                                           machopic_gen_offset (orig));
834                 }
835             }
836         }
837
838       if (GET_CODE (pic_ref) != REG)
839         {
840           if (reg != 0)
841             {
842               emit_move_insn (reg, pic_ref);
843               return reg;
844             }
845           else
846             {
847               return force_reg (mode, pic_ref);
848             }
849         }
850       else
851         {
852           return pic_ref;
853         }
854     }
855
856   else if (GET_CODE (orig) == SYMBOL_REF)
857     return orig;
858
859   else if (GET_CODE (orig) == PLUS
860            && (GET_CODE (XEXP (orig, 0)) == MEM
861                || GET_CODE (XEXP (orig, 0)) == SYMBOL_REF
862                || GET_CODE (XEXP (orig, 0)) == LABEL_REF)
863            && XEXP (orig, 0) != pic_offset_table_rtx
864            && GET_CODE (XEXP (orig, 1)) != REG)
865
866     {
867       rtx base;
868       int is_complex = (GET_CODE (XEXP (orig, 0)) == MEM);
869
870       base = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
871       orig = machopic_legitimize_pic_address (XEXP (orig, 1),
872                                               Pmode, (base == reg ? 0 : reg));
873       if (GET_CODE (orig) == CONST_INT)
874         {
875           pic_ref = plus_constant (base, INTVAL (orig));
876           is_complex = 1;
877         }
878       else
879         pic_ref = gen_rtx_PLUS (Pmode, base, orig);
880
881       if (reg && is_complex)
882         {
883           emit_move_insn (reg, pic_ref);
884           pic_ref = reg;
885         }
886       /* Likewise, should we set special REG_NOTEs here?  */
887     }
888
889   else if (GET_CODE (orig) == CONST)
890     {
891       return machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
892     }
893
894   else if (GET_CODE (orig) == MEM
895            && GET_CODE (XEXP (orig, 0)) == SYMBOL_REF)
896     {
897       rtx addr = machopic_legitimize_pic_address (XEXP (orig, 0), Pmode, reg);
898       addr = replace_equiv_address (orig, addr);
899       emit_move_insn (reg, addr);
900       pic_ref = reg;
901     }
902
903   return pic_ref;
904 }
905
906 /* Output the stub or non-lazy pointer in *SLOT, if it has been used.
907    DATA is the FILE* for assembly output.  Called from
908    htab_traverse.  */
909
910 static int
911 machopic_output_indirection (void **slot, void *data)
912 {
913   machopic_indirection *p = *((machopic_indirection **) slot);
914   FILE *asm_out_file = (FILE *) data;
915   rtx symbol;
916   const char *sym_name;
917   const char *ptr_name;
918
919   if (!p->used)
920     return 1;
921
922   symbol = p->symbol;
923   sym_name = XSTR (symbol, 0);
924   ptr_name = p->ptr_name;
925
926   if (p->stub_p)
927     {
928       char *sym;
929       char *stub;
930       tree id;
931
932       id = maybe_get_identifier (sym_name);
933       if (id)
934         {
935           tree id_orig = id;
936
937           while (IDENTIFIER_TRANSPARENT_ALIAS (id))
938             id = TREE_CHAIN (id);
939           if (id != id_orig)
940             sym_name = IDENTIFIER_POINTER (id);
941         }
942
943       sym = XALLOCAVEC (char, strlen (sym_name) + 2);
944       if (sym_name[0] == '*' || sym_name[0] == '&')
945         strcpy (sym, sym_name + 1);
946       else if (sym_name[0] == '-' || sym_name[0] == '+')
947         strcpy (sym, sym_name);
948       else
949         sprintf (sym, "%s%s", user_label_prefix, sym_name);
950
951       stub = XALLOCAVEC (char, strlen (ptr_name) + 2);
952       if (ptr_name[0] == '*' || ptr_name[0] == '&')
953         strcpy (stub, ptr_name + 1);
954       else
955         sprintf (stub, "%s%s", user_label_prefix, ptr_name);
956
957       machopic_output_stub (asm_out_file, sym, stub);
958     }
959   else if (! indirect_data (symbol)
960            && (machopic_symbol_defined_p (symbol)
961                || SYMBOL_REF_LOCAL_P (symbol)))
962     {
963       switch_to_section (data_section);
964       assemble_align (GET_MODE_ALIGNMENT (Pmode));
965       assemble_label (ptr_name);
966       assemble_integer (gen_rtx_SYMBOL_REF (Pmode, sym_name),
967                         GET_MODE_SIZE (Pmode),
968                         GET_MODE_ALIGNMENT (Pmode), 1);
969     }
970   else
971     {
972       rtx init = const0_rtx;
973
974       switch_to_section (darwin_sections[machopic_nl_symbol_ptr_section]);
975
976       /* Mach-O symbols are passed around in code through indirect
977          references and the original symbol_ref hasn't passed through
978          the generic handling and reference-catching in
979          output_operand, so we need to manually mark weak references
980          as such.  */
981       if (SYMBOL_REF_WEAK (symbol))
982         {
983           tree decl = SYMBOL_REF_DECL (symbol);
984           gcc_assert (DECL_P (decl));
985
986           if (decl != NULL_TREE
987               && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl)
988               /* Handle only actual external-only definitions, not
989                  e.g. extern inline code or variables for which
990                  storage has been allocated.  */
991               && !TREE_STATIC (decl))
992             {
993               fputs ("\t.weak_reference ", asm_out_file);
994               assemble_name (asm_out_file, sym_name);
995               fputc ('\n', asm_out_file);
996             }
997         }
998
999       assemble_name (asm_out_file, ptr_name);
1000       fprintf (asm_out_file, ":\n");
1001
1002       fprintf (asm_out_file, "\t.indirect_symbol ");
1003       assemble_name (asm_out_file, sym_name);
1004       fprintf (asm_out_file, "\n");
1005
1006       /* Variables that are marked with MACHO_SYMBOL_STATIC need to
1007          have their symbol name instead of 0 in the second entry of
1008          the non-lazy symbol pointer data structure when they are
1009          defined.  This allows the runtime to rebind newer instances
1010          of the translation unit with the original instance of the
1011          symbol.  */
1012
1013       if ((SYMBOL_REF_FLAGS (symbol) & MACHO_SYMBOL_STATIC)
1014           && machopic_symbol_defined_p (symbol))
1015         init = gen_rtx_SYMBOL_REF (Pmode, sym_name);
1016
1017       assemble_integer (init, GET_MODE_SIZE (Pmode),
1018                         GET_MODE_ALIGNMENT (Pmode), 1);
1019     }
1020
1021   return 1;
1022 }
1023
1024 void
1025 machopic_finish (FILE *asm_out_file)
1026 {
1027   if (machopic_indirections)
1028     htab_traverse_noresize (machopic_indirections,
1029                             machopic_output_indirection,
1030                             asm_out_file);
1031 }
1032
1033 int
1034 machopic_operand_p (rtx op)
1035 {
1036   if (MACHOPIC_JUST_INDIRECT)
1037     return (GET_CODE (op) == SYMBOL_REF
1038             && machopic_symbol_defined_p (op));
1039   else
1040     return (GET_CODE (op) == CONST
1041             && GET_CODE (XEXP (op, 0)) == UNSPEC
1042             && XINT (XEXP (op, 0), 1) == UNSPEC_MACHOPIC_OFFSET);
1043 }
1044
1045 /* This function records whether a given name corresponds to a defined
1046    or undefined function or variable, for machopic_classify_ident to
1047    use later.  */
1048
1049 void
1050 darwin_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
1051 {
1052   rtx sym_ref;
1053
1054   /* Do the standard encoding things first.  */
1055   default_encode_section_info (decl, rtl, first);
1056
1057   if (TREE_CODE (decl) != FUNCTION_DECL && TREE_CODE (decl) != VAR_DECL)
1058     return;
1059
1060   sym_ref = XEXP (rtl, 0);
1061   if (TREE_CODE (decl) == VAR_DECL)
1062     SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_VARIABLE;
1063
1064   if (!DECL_EXTERNAL (decl)
1065       && (!TREE_PUBLIC (decl) || !DECL_WEAK (decl))
1066       && ! lookup_attribute ("weakref", DECL_ATTRIBUTES (decl))
1067       && ((TREE_STATIC (decl)
1068            && (!DECL_COMMON (decl) || !TREE_PUBLIC (decl)))
1069           || (!DECL_COMMON (decl) && DECL_INITIAL (decl)
1070               && DECL_INITIAL (decl) != error_mark_node)))
1071     SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_FLAG_DEFINED;
1072
1073   if (! TREE_PUBLIC (decl))
1074     SYMBOL_REF_FLAGS (sym_ref) |= MACHO_SYMBOL_STATIC;
1075 }
1076
1077 void
1078 darwin_mark_decl_preserved (const char *name)
1079 {
1080   fprintf (asm_out_file, ".no_dead_strip ");
1081   assemble_name (asm_out_file, name);
1082   fputc ('\n', asm_out_file);
1083 }
1084
1085 static section *
1086 darwin_text_section (int reloc, int weak)
1087 {
1088   if (reloc)
1089     return (weak
1090             ? darwin_sections[text_unlikely_coal_section]
1091             : unlikely_text_section ());
1092   else
1093     return (weak
1094             ? darwin_sections[text_coal_section]
1095             : text_section);
1096 }
1097
1098 static section *
1099 darwin_rodata_section (int weak)
1100 {
1101   return (weak
1102           ? darwin_sections[const_coal_section]
1103           : darwin_sections[const_section]);
1104 }
1105
1106 static section *
1107 darwin_mergeable_string_section (tree exp,
1108                                  unsigned HOST_WIDE_INT align)
1109 {
1110   if (flag_merge_constants
1111       && TREE_CODE (exp) == STRING_CST
1112       && TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE
1113       && align <= 256
1114       && (int_size_in_bytes (TREE_TYPE (exp))
1115           == TREE_STRING_LENGTH (exp))
1116       && ((size_t) TREE_STRING_LENGTH (exp)
1117           == strlen (TREE_STRING_POINTER (exp)) + 1))
1118     return darwin_sections[cstring_section];
1119
1120   return readonly_data_section;
1121 }
1122
1123 #ifndef HAVE_GAS_LITERAL16
1124 #define HAVE_GAS_LITERAL16 0
1125 #endif
1126
1127 static section *
1128 darwin_mergeable_constant_section (tree exp,
1129                                    unsigned HOST_WIDE_INT align)
1130 {
1131   enum machine_mode mode = DECL_MODE (exp);
1132   unsigned int modesize = GET_MODE_BITSIZE (mode);
1133
1134   if (flag_merge_constants
1135       && mode != VOIDmode
1136       && mode != BLKmode
1137       && modesize <= align
1138       && align >= 8
1139       && align <= 256
1140       && (align & (align -1)) == 0)
1141     {
1142       tree size = TYPE_SIZE_UNIT (TREE_TYPE (exp));
1143
1144       if (TREE_CODE (size) == INTEGER_CST
1145           && TREE_INT_CST_LOW (size) == 4
1146           && TREE_INT_CST_HIGH (size) == 0)
1147         return darwin_sections[literal4_section];
1148       else if (TREE_CODE (size) == INTEGER_CST
1149                && TREE_INT_CST_LOW (size) == 8
1150                && TREE_INT_CST_HIGH (size) == 0)
1151         return darwin_sections[literal8_section];
1152       else if (HAVE_GAS_LITERAL16
1153                && TARGET_64BIT
1154                && TREE_CODE (size) == INTEGER_CST
1155                && TREE_INT_CST_LOW (size) == 16
1156                && TREE_INT_CST_HIGH (size) == 0)
1157         return darwin_sections[literal16_section];
1158       else
1159         return readonly_data_section;
1160     }
1161
1162   return readonly_data_section;
1163 }
1164
1165 int
1166 machopic_reloc_rw_mask (void)
1167 {
1168   return MACHOPIC_INDIRECT ? 3 : 0;
1169 }
1170
1171 section *
1172 machopic_select_section (tree decl,
1173                          int reloc,
1174                          unsigned HOST_WIDE_INT align)
1175 {
1176   bool weak = (DECL_P (decl)
1177                && DECL_WEAK (decl)
1178                && !lookup_attribute ("weak_import",
1179                                      DECL_ATTRIBUTES (decl)));
1180   section *base_section;
1181
1182   switch (categorize_decl_for_section (decl, reloc))
1183     {
1184     case SECCAT_TEXT:
1185       base_section = darwin_text_section (reloc, weak);
1186       break;
1187
1188     case SECCAT_RODATA:
1189     case SECCAT_SRODATA:
1190       base_section = darwin_rodata_section (weak);
1191       break;
1192
1193     case SECCAT_RODATA_MERGE_STR:
1194       base_section = darwin_mergeable_string_section (decl, align);
1195       break;
1196
1197     case SECCAT_RODATA_MERGE_STR_INIT:
1198       base_section = darwin_mergeable_string_section (DECL_INITIAL (decl), align);
1199       break;
1200
1201     case SECCAT_RODATA_MERGE_CONST:
1202       base_section =  darwin_mergeable_constant_section (decl, align);
1203       break;
1204
1205     case SECCAT_DATA:
1206     case SECCAT_DATA_REL:
1207     case SECCAT_DATA_REL_LOCAL:
1208     case SECCAT_DATA_REL_RO:
1209     case SECCAT_DATA_REL_RO_LOCAL:
1210     case SECCAT_SDATA:
1211     case SECCAT_TDATA:
1212     case SECCAT_BSS:
1213     case SECCAT_SBSS:
1214     case SECCAT_TBSS:
1215       if (TREE_READONLY (decl) || TREE_CONSTANT (decl))
1216         base_section = weak ? darwin_sections[const_data_coal_section]
1217                             : darwin_sections[const_data_section];
1218       else
1219         base_section = weak ? darwin_sections[data_coal_section] : data_section;
1220       break;
1221
1222     default:
1223       gcc_unreachable ();
1224     }
1225
1226   /* Darwin weird special cases.  */
1227   if (TREE_CODE (decl) == CONSTRUCTOR
1228       && TREE_TYPE (decl)
1229       && TREE_CODE (TREE_TYPE (decl)) == RECORD_TYPE
1230       && TYPE_NAME (TREE_TYPE (decl)))
1231     {
1232       tree name = TYPE_NAME (TREE_TYPE (decl));
1233       if (TREE_CODE (name) == TYPE_DECL)
1234         name = DECL_NAME (name);
1235
1236       if (!strcmp (IDENTIFIER_POINTER (name), "__builtin_ObjCString"))
1237         {
1238           if (flag_next_runtime)
1239             return darwin_sections[objc_constant_string_object_section];
1240           else
1241             return darwin_sections[objc_string_object_section];
1242         }
1243       else
1244         return base_section;
1245     }
1246   else if (TREE_CODE (decl) == VAR_DECL
1247            && DECL_NAME (decl)
1248            && TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE
1249            && IDENTIFIER_POINTER (DECL_NAME (decl))
1250            && !strncmp (IDENTIFIER_POINTER (DECL_NAME (decl)), "_OBJC_", 6))
1251     {
1252       const char *name = IDENTIFIER_POINTER (DECL_NAME (decl));
1253
1254       if (!strncmp (name, "_OBJC_CLASS_METHODS_", 20))
1255         return darwin_sections[objc_cls_meth_section];
1256       else if (!strncmp (name, "_OBJC_INSTANCE_METHODS_", 23))
1257         return darwin_sections[objc_inst_meth_section];
1258       else if (!strncmp (name, "_OBJC_CATEGORY_CLASS_METHODS_", 20))
1259         return darwin_sections[objc_cat_cls_meth_section];
1260       else if (!strncmp (name, "_OBJC_CATEGORY_INSTANCE_METHODS_", 23))
1261         return darwin_sections[objc_cat_inst_meth_section];
1262       else if (!strncmp (name, "_OBJC_CLASS_VARIABLES_", 22))
1263         return darwin_sections[objc_class_vars_section];
1264       else if (!strncmp (name, "_OBJC_INSTANCE_VARIABLES_", 25))
1265         return darwin_sections[objc_instance_vars_section];
1266       else if (!strncmp (name, "_OBJC_CLASS_PROTOCOLS_", 22))
1267         return darwin_sections[objc_cat_cls_meth_section];
1268       else if (!strncmp (name, "_OBJC_CLASS_NAME_", 17))
1269         return darwin_sections[objc_class_names_section];
1270       else if (!strncmp (name, "_OBJC_METH_VAR_NAME_", 20))
1271         return darwin_sections[objc_meth_var_names_section];
1272       else if (!strncmp (name, "_OBJC_METH_VAR_TYPE_", 20))
1273         return darwin_sections[objc_meth_var_types_section];
1274       else if (!strncmp (name, "_OBJC_CLASS_REFERENCES", 22))
1275         return darwin_sections[objc_cls_refs_section];
1276       else if (!strncmp (name, "_OBJC_CLASS_", 12))
1277         return darwin_sections[objc_class_section];
1278       else if (!strncmp (name, "_OBJC_METACLASS_", 16))
1279         return darwin_sections[objc_meta_class_section];
1280       else if (!strncmp (name, "_OBJC_CATEGORY_", 15))
1281         return darwin_sections[objc_category_section];
1282       else if (!strncmp (name, "_OBJC_SELECTOR_REFERENCES", 25))
1283         return darwin_sections[objc_selector_refs_section];
1284       else if (!strncmp (name, "_OBJC_SELECTOR_FIXUP", 20))
1285         return darwin_sections[objc_selector_fixup_section];
1286       else if (!strncmp (name, "_OBJC_SYMBOLS", 13))
1287         return darwin_sections[objc_symbols_section];
1288       else if (!strncmp (name, "_OBJC_MODULES", 13))
1289         return darwin_sections[objc_module_info_section];
1290       else if (!strncmp (name, "_OBJC_IMAGE_INFO", 16))
1291         return darwin_sections[objc_image_info_section];
1292       else if (!strncmp (name, "_OBJC_PROTOCOL_INSTANCE_METHODS_", 32))
1293         return darwin_sections[objc_cat_inst_meth_section];
1294       else if (!strncmp (name, "_OBJC_PROTOCOL_CLASS_METHODS_", 29))
1295         return darwin_sections[objc_cat_cls_meth_section];
1296       else if (!strncmp (name, "_OBJC_PROTOCOL_REFS_", 20))
1297         return darwin_sections[objc_cat_cls_meth_section];
1298       else if (!strncmp (name, "_OBJC_PROTOCOL_", 15))
1299         return darwin_sections[objc_protocol_section];
1300       else
1301         return base_section;
1302     }
1303
1304   return base_section;
1305 }
1306
1307 /* This can be called with address expressions as "rtx".
1308    They must go in "const".  */
1309
1310 section *
1311 machopic_select_rtx_section (enum machine_mode mode, rtx x,
1312                              unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
1313 {
1314   if (GET_MODE_SIZE (mode) == 8
1315       && (GET_CODE (x) == CONST_INT
1316           || GET_CODE (x) == CONST_DOUBLE))
1317     return darwin_sections[literal8_section];
1318   else if (GET_MODE_SIZE (mode) == 4
1319            && (GET_CODE (x) == CONST_INT
1320                || GET_CODE (x) == CONST_DOUBLE))
1321     return darwin_sections[literal4_section];
1322   else if (HAVE_GAS_LITERAL16
1323            && TARGET_64BIT
1324            && GET_MODE_SIZE (mode) == 16
1325            && (GET_CODE (x) == CONST_INT
1326                || GET_CODE (x) == CONST_DOUBLE
1327                || GET_CODE (x) == CONST_VECTOR))
1328     return darwin_sections[literal16_section];
1329   else if (MACHOPIC_INDIRECT
1330            && (GET_CODE (x) == SYMBOL_REF
1331                || GET_CODE (x) == CONST
1332                || GET_CODE (x) == LABEL_REF))
1333     return darwin_sections[const_data_section];
1334   else
1335     return darwin_sections[const_section];
1336 }
1337
1338 void
1339 machopic_asm_out_constructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1340 {
1341   if (MACHOPIC_INDIRECT)
1342     switch_to_section (darwin_sections[mod_init_section]);
1343   else
1344     switch_to_section (darwin_sections[constructor_section]);
1345   assemble_align (POINTER_SIZE);
1346   assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1347
1348   if (! MACHOPIC_INDIRECT)
1349     fprintf (asm_out_file, ".reference .constructors_used\n");
1350 }
1351
1352 void
1353 machopic_asm_out_destructor (rtx symbol, int priority ATTRIBUTE_UNUSED)
1354 {
1355   if (MACHOPIC_INDIRECT)
1356     switch_to_section (darwin_sections[mod_term_section]);
1357   else
1358     switch_to_section (darwin_sections[destructor_section]);
1359   assemble_align (POINTER_SIZE);
1360   assemble_integer (symbol, POINTER_SIZE / BITS_PER_UNIT, POINTER_SIZE, 1);
1361
1362   if (! MACHOPIC_INDIRECT)
1363     fprintf (asm_out_file, ".reference .destructors_used\n");
1364 }
1365
1366 void
1367 darwin_globalize_label (FILE *stream, const char *name)
1368 {
1369   if (!!strncmp (name, "_OBJC_", 6))
1370     default_globalize_label (stream, name);
1371 }
1372
1373 /* This routine returns non-zero if 'name' starts with the special objective-c 
1374    anonymous file-scope static name.  It accommodates c++'s mangling of such 
1375    symbols (in this case the symbols will have form _ZL{d}*_OBJC_* d=digit).  */
1376    
1377 int 
1378 darwin_label_is_anonymous_local_objc_name (const char *name)
1379 {
1380   const unsigned char *p = (const unsigned char *) name;
1381   if (*p != '_')
1382     return 0;
1383   if (p[1] == 'Z' && p[2] == 'L')
1384   {
1385     p += 3;
1386     while (*p >= '0' && *p <= '9')
1387       p++;
1388   }
1389   return (!strncmp ((const char *)p, "_OBJC_", 6));
1390 }
1391
1392 /* LTO support for Mach-O.  */
1393
1394 /* Section names for LTO sections.  */
1395 static unsigned int lto_section_names_offset = 0;
1396
1397 /* This is the obstack which we use to allocate the many strings.  */
1398 static struct obstack lto_section_names_obstack;
1399
1400 /* Segment name for LTO sections.  */
1401 #define LTO_SEGMENT_NAME "__GNU_LTO"
1402
1403 /* Section name for LTO section names section.  */
1404 #define LTO_NAMES_SECTION "__section_names"
1405
1406 /* File to temporarily store LTO data.  This is appended to asm_out_file
1407    in darwin_end_file.  */
1408 static FILE *lto_asm_out_file, *saved_asm_out_file;
1409 static char *lto_asm_out_name;
1410
1411 /* Prepare asm_out_file for LTO output.  For darwin, this means hiding
1412    asm_out_file and switching to an alternative output file.  */
1413 void
1414 darwin_asm_lto_start (void)
1415 {
1416   gcc_assert (! saved_asm_out_file);
1417   saved_asm_out_file = asm_out_file;
1418   if (! lto_asm_out_name)
1419     lto_asm_out_name = make_temp_file (".lto.s");
1420   lto_asm_out_file = fopen (lto_asm_out_name, "a");
1421   if (lto_asm_out_file == NULL)
1422     fatal_error ("failed to open temporary file %s for LTO output",
1423                  lto_asm_out_name);
1424   asm_out_file = lto_asm_out_file;
1425 }
1426
1427 /* Restore asm_out_file.  */
1428 void
1429 darwin_asm_lto_end (void)
1430 {
1431   gcc_assert (saved_asm_out_file);
1432   fclose (lto_asm_out_file);
1433   asm_out_file = saved_asm_out_file;
1434   saved_asm_out_file = NULL;
1435 }
1436
1437 void
1438 darwin_asm_named_section (const char *name,
1439                           unsigned int flags,
1440                           tree decl ATTRIBUTE_UNUSED)
1441 {
1442   /* LTO sections go in a special segment __GNU_LTO.  We want to replace the
1443      section name with something we can use to represent arbitrary-length
1444      names (section names in Mach-O are at most 16 characters long).  */
1445   if (strncmp (name, LTO_SECTION_NAME_PREFIX,
1446                strlen (LTO_SECTION_NAME_PREFIX)) == 0)
1447     {
1448       /* We expect certain flags to be set...  */
1449       gcc_assert ((flags & (SECTION_DEBUG | SECTION_NAMED))
1450                   == (SECTION_DEBUG | SECTION_NAMED));
1451
1452       /* Add the section name to the things to output when we end the
1453          current assembler output file.
1454          This is all not very efficient, but that doesn't matter -- this
1455          shouldn't be a hot path in the compiler...  */
1456       obstack_1grow (&lto_section_names_obstack, '\t');
1457       obstack_grow (&lto_section_names_obstack, ".ascii ", 7);
1458       obstack_1grow (&lto_section_names_obstack, '"');
1459       obstack_grow (&lto_section_names_obstack, name, strlen (name));
1460       obstack_grow (&lto_section_names_obstack, "\\0\"\n", 4);
1461
1462       /* Output the dummy section name.  */
1463       fprintf (asm_out_file, "\t# %s\n", name);
1464       fprintf (asm_out_file, "\t.section %s,__%08X,regular,debug\n",
1465                LTO_SEGMENT_NAME, lto_section_names_offset);
1466
1467       /* Update the offset for the next section name.  Make sure we stay
1468          within reasonable length.  */  
1469       lto_section_names_offset += strlen (name) + 1;
1470       gcc_assert (lto_section_names_offset > 0
1471                   && lto_section_names_offset < ((unsigned) 1 << 31));
1472     }
1473   else
1474     fprintf (asm_out_file, "\t.section %s\n", name);
1475 }
1476
1477 void
1478 darwin_unique_section (tree decl ATTRIBUTE_UNUSED, int reloc ATTRIBUTE_UNUSED)
1479 {
1480   /* Darwin does not use unique sections.  */
1481 }
1482
1483 /* Handle __attribute__ ((apple_kext_compatibility)).
1484    This only applies to darwin kexts for 2.95 compatibility -- it shrinks the
1485    vtable for classes with this attribute (and their descendants) by not
1486    outputting the new 3.0 nondeleting destructor.  This means that such
1487    objects CANNOT be allocated on the stack or as globals UNLESS they have
1488    a completely empty `operator delete'.
1489    Luckily, this fits in with the Darwin kext model.
1490
1491    This attribute also disables gcc3's potential overlaying of derived
1492    class data members on the padding at the end of the base class.  */
1493
1494 tree
1495 darwin_handle_kext_attribute (tree *node, tree name,
1496                               tree args ATTRIBUTE_UNUSED,
1497                               int flags ATTRIBUTE_UNUSED,
1498                               bool *no_add_attrs)
1499 {
1500   /* APPLE KEXT stuff -- only applies with pure static C++ code.  */
1501   if (! TARGET_KEXTABI)
1502     {
1503       warning (0, "%qE 2.95 vtable-compatibility attribute applies "
1504                "only when compiling a kext", name);
1505
1506       *no_add_attrs = true;
1507     }
1508   else if (TREE_CODE (*node) != RECORD_TYPE)
1509     {
1510       warning (0, "%qE 2.95 vtable-compatibility attribute applies "
1511                "only to C++ classes", name);
1512
1513       *no_add_attrs = true;
1514     }
1515
1516   return NULL_TREE;
1517 }
1518
1519 /* Handle a "weak_import" attribute; arguments as in
1520    struct attribute_spec.handler.  */
1521
1522 tree
1523 darwin_handle_weak_import_attribute (tree *node, tree name,
1524                                      tree ARG_UNUSED (args),
1525                                      int ARG_UNUSED (flags),
1526                                      bool * no_add_attrs)
1527 {
1528   if (TREE_CODE (*node) != FUNCTION_DECL && TREE_CODE (*node) != VAR_DECL)
1529     {
1530       warning (OPT_Wattributes, "%qE attribute ignored",
1531                name);
1532       *no_add_attrs = true;
1533     }
1534   else
1535     declare_weak (*node);
1536
1537   return NULL_TREE;
1538 }
1539
1540 /* Emit a label for an FDE, making it global and/or weak if appropriate.
1541    The third parameter is nonzero if this is for exception handling.
1542    The fourth parameter is nonzero if this is just a placeholder for an
1543    FDE that we are omitting. */
1544
1545 void
1546 darwin_emit_unwind_label (FILE *file, tree decl, int for_eh, int empty)
1547 {
1548   char *lab;
1549
1550   if (! for_eh)
1551     return;
1552
1553   lab = concat (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), ".eh", NULL);
1554
1555   if (TREE_PUBLIC (decl))
1556     {
1557       targetm.asm_out.globalize_label (file, lab);
1558       if (DECL_VISIBILITY (decl) == VISIBILITY_HIDDEN)
1559         {
1560           fputs ("\t.private_extern ", file);
1561           assemble_name (file, lab);
1562           fputc ('\n', file);
1563         }
1564     }
1565
1566   if (DECL_WEAK (decl))
1567     {
1568       fputs ("\t.weak_definition ", file);
1569       assemble_name (file, lab);
1570       fputc ('\n', file);
1571     }
1572
1573   assemble_name (file, lab);
1574   if (empty)
1575     {
1576       fputs (" = 0\n", file);
1577
1578       /* Mark the absolute .eh and .eh1 style labels as needed to
1579          ensure that we don't dead code strip them and keep such
1580          labels from another instantiation point until we can fix this
1581          properly with group comdat support.  */
1582       darwin_mark_decl_preserved (lab);
1583     }
1584   else
1585     fputs (":\n", file);
1586
1587   free (lab);
1588 }
1589
1590 static GTY(()) unsigned long except_table_label_num;
1591
1592 void
1593 darwin_emit_except_table_label (FILE *file)
1594 {
1595   char section_start_label[30];
1596
1597   ASM_GENERATE_INTERNAL_LABEL (section_start_label, "GCC_except_table",
1598                                except_table_label_num++);
1599   ASM_OUTPUT_LABEL (file, section_start_label);
1600 }
1601 /* Generate a PC-relative reference to a Mach-O non-lazy-symbol.  */
1602
1603 void
1604 darwin_non_lazy_pcrel (FILE *file, rtx addr)
1605 {
1606   const char *nlp_name;
1607
1608   gcc_assert (GET_CODE (addr) == SYMBOL_REF);
1609
1610   nlp_name = machopic_indirection_name (addr, /*stub_p=*/false);
1611   fputs ("\t.long\t", file);
1612   ASM_OUTPUT_LABELREF (file, nlp_name);
1613   fputs ("-.", file);
1614 }
1615
1616 /* Emit an assembler directive to set visibility for a symbol.  The
1617    only supported visibilities are VISIBILITY_DEFAULT and
1618    VISIBILITY_HIDDEN; the latter corresponds to Darwin's "private
1619    extern".  There is no MACH-O equivalent of ELF's
1620    VISIBILITY_INTERNAL or VISIBILITY_PROTECTED. */
1621
1622 void
1623 darwin_assemble_visibility (tree decl, int vis)
1624 {
1625   if (vis == VISIBILITY_DEFAULT)
1626     ;
1627   else if (vis == VISIBILITY_HIDDEN)
1628     {
1629       fputs ("\t.private_extern ", asm_out_file);
1630       assemble_name (asm_out_file,
1631                      (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))));
1632       fputs ("\n", asm_out_file);
1633     }
1634   else
1635     warning (OPT_Wattributes, "internal and protected visibility attributes "
1636              "not supported in this configuration; ignored");
1637 }
1638
1639 /* Output a difference of two labels that will be an assembly time
1640    constant if the two labels are local.  (.long lab1-lab2 will be
1641    very different if lab1 is at the boundary between two sections; it
1642    will be relocated according to the second section, not the first,
1643    so one ends up with a difference between labels in different
1644    sections, which is bad in the dwarf2 eh context for instance.)  */
1645
1646 static int darwin_dwarf_label_counter;
1647
1648 void
1649 darwin_asm_output_dwarf_delta (FILE *file, int size,
1650                                const char *lab1, const char *lab2)
1651 {
1652   int islocaldiff = (lab1[0] == '*' && lab1[1] == 'L'
1653                      && lab2[0] == '*' && lab2[1] == 'L');
1654   const char *directive = (size == 8 ? ".quad" : ".long");
1655
1656   if (islocaldiff)
1657     fprintf (file, "\t.set L$set$%d,", darwin_dwarf_label_counter);
1658   else
1659     fprintf (file, "\t%s\t", directive);
1660   assemble_name_raw (file, lab1);
1661   fprintf (file, "-");
1662   assemble_name_raw (file, lab2);
1663   if (islocaldiff)
1664     fprintf (file, "\n\t%s L$set$%d", directive, darwin_dwarf_label_counter++);
1665 }
1666
1667 /* Output labels for the start of the DWARF sections if necessary.
1668    Initialize the stuff we need for LTO long section names support.  */
1669 void
1670 darwin_file_start (void)
1671 {
1672   if (write_symbols == DWARF2_DEBUG)
1673     {
1674       static const char * const debugnames[] =
1675         {
1676           DEBUG_FRAME_SECTION,
1677           DEBUG_INFO_SECTION,
1678           DEBUG_ABBREV_SECTION,
1679           DEBUG_ARANGES_SECTION,
1680           DEBUG_MACINFO_SECTION,
1681           DEBUG_LINE_SECTION,
1682           DEBUG_LOC_SECTION,
1683           DEBUG_PUBNAMES_SECTION,
1684           DEBUG_PUBTYPES_SECTION,
1685           DEBUG_STR_SECTION,
1686           DEBUG_RANGES_SECTION
1687         };
1688       size_t i;
1689
1690       for (i = 0; i < ARRAY_SIZE (debugnames); i++)
1691         {
1692           int namelen;
1693
1694           switch_to_section (get_section (debugnames[i], SECTION_DEBUG, NULL));
1695
1696           gcc_assert (strncmp (debugnames[i], "__DWARF,", 8) == 0);
1697           gcc_assert (strchr (debugnames[i] + 8, ','));
1698
1699           namelen = strchr (debugnames[i] + 8, ',') - (debugnames[i] + 8);
1700           fprintf (asm_out_file, "Lsection%.*s:\n", namelen, debugnames[i] + 8);
1701         }
1702     }
1703
1704   /* We fill this obstack with the complete section text for the lto section
1705      names to write in darwin_file_end.  */
1706   obstack_init (&lto_section_names_obstack);
1707   lto_section_names_offset = 0;
1708 }
1709
1710 /* Output an offset in a DWARF section on Darwin.  On Darwin, DWARF section
1711    offsets are not represented using relocs in .o files; either the
1712    section never leaves the .o file, or the linker or other tool is
1713    responsible for parsing the DWARF and updating the offsets.  */
1714
1715 void
1716 darwin_asm_output_dwarf_offset (FILE *file, int size, const char * lab,
1717                                 section *base)
1718 {
1719   char sname[64];
1720   int namelen;
1721
1722   gcc_assert (base->common.flags & SECTION_NAMED);
1723   gcc_assert (strncmp (base->named.name, "__DWARF,", 8) == 0);
1724   gcc_assert (strchr (base->named.name + 8, ','));
1725
1726   namelen = strchr (base->named.name + 8, ',') - (base->named.name + 8);
1727   sprintf (sname, "*Lsection%.*s", namelen, base->named.name + 8);
1728   darwin_asm_output_dwarf_delta (file, size, lab, sname);
1729 }
1730
1731 void
1732 darwin_file_end (void)
1733 {
1734   const char *lto_section_names;
1735
1736   machopic_finish (asm_out_file);
1737   if (strcmp (lang_hooks.name, "GNU C++") == 0)
1738     {
1739       switch_to_section (darwin_sections[constructor_section]);
1740       switch_to_section (darwin_sections[destructor_section]);
1741       ASM_OUTPUT_ALIGN (asm_out_file, 1);
1742     }
1743
1744   /* If there was LTO assembler output, append it to asm_out_file.  */
1745   if (lto_asm_out_name)
1746     {
1747       int n;
1748       char *buf, *lto_asm_txt;
1749
1750       /* Shouldn't be here if we failed to switch back.  */
1751       gcc_assert (! saved_asm_out_file);
1752
1753       lto_asm_out_file = fopen (lto_asm_out_name, "r");
1754       if (lto_asm_out_file == NULL)
1755         fatal_error ("failed to open temporary file %s with LTO output",
1756                      lto_asm_out_name);
1757       fseek (lto_asm_out_file, 0, SEEK_END);
1758       n = ftell (lto_asm_out_file);
1759       if (n > 0)
1760         {
1761           fseek (lto_asm_out_file, 0, SEEK_SET);
1762           lto_asm_txt = buf = (char *) xmalloc (n + 1);
1763           while (fgets (lto_asm_txt, n, lto_asm_out_file))
1764             fputs (lto_asm_txt, asm_out_file);
1765         }
1766
1767       /* Remove the temporary file.  */
1768       fclose (lto_asm_out_file);
1769       unlink_if_ordinary (lto_asm_out_name);
1770       free (lto_asm_out_name);
1771     }
1772
1773   /* Finish the LTO section names obstack.  Don't output anything if
1774      there are no recorded section names.  */
1775   obstack_1grow (&lto_section_names_obstack, '\0');
1776   lto_section_names = XOBFINISH (&lto_section_names_obstack, const char *);
1777   if (strlen (lto_section_names) > 0)
1778     {
1779       fprintf (asm_out_file,
1780                "\t.section %s,%s,regular,debug\n",
1781                LTO_SEGMENT_NAME, LTO_NAMES_SECTION);
1782       fprintf (asm_out_file,
1783                "\t# Section names in %s are offsets into this table\n",
1784                LTO_SEGMENT_NAME);
1785       fprintf (asm_out_file, "%s\n", lto_section_names);
1786     }
1787   obstack_free (&lto_section_names_obstack, NULL);
1788
1789   fprintf (asm_out_file, "\t.subsections_via_symbols\n");
1790 }
1791
1792 /* TODO: Add a language hook for identifying if a decl is a vtable.  */
1793 #define DARWIN_VTABLE_P(DECL) 0
1794
1795 /* Cross-module name binding.  Darwin does not support overriding
1796    functions at dynamic-link time, except for vtables in kexts.  */
1797
1798 bool
1799 darwin_binds_local_p (const_tree decl)
1800 {
1801   return default_binds_local_p_1 (decl,
1802                                   TARGET_KEXTABI && DARWIN_VTABLE_P (decl));
1803 }
1804
1805 #if 0
1806 /* See TARGET_ASM_OUTPUT_ANCHOR for why we can't do this yet.  */
1807 /* The Darwin's implementation of TARGET_ASM_OUTPUT_ANCHOR.  Define the
1808    anchor relative to ".", the current section position.  We cannot use
1809    the default one because ASM_OUTPUT_DEF is wrong for Darwin.  */
1810
1811 void
1812 darwin_asm_output_anchor (rtx symbol)
1813 {
1814   fprintf (asm_out_file, "\t.set\t");
1815   assemble_name (asm_out_file, XSTR (symbol, 0));
1816   fprintf (asm_out_file, ", . + " HOST_WIDE_INT_PRINT_DEC "\n",
1817            SYMBOL_REF_BLOCK_OFFSET (symbol));
1818 }
1819 #endif
1820
1821 /* Set the darwin specific attributes on TYPE.  */
1822 void
1823 darwin_set_default_type_attributes (tree type)
1824 {
1825   if (darwin_ms_struct
1826       && TREE_CODE (type) == RECORD_TYPE)
1827     TYPE_ATTRIBUTES (type) = tree_cons (get_identifier ("ms_struct"),
1828                                         NULL_TREE,
1829                                         TYPE_ATTRIBUTES (type));
1830 }
1831
1832 /* True, iff we're generating code for loadable kernel extensions.  */
1833
1834 bool
1835 darwin_kextabi_p (void) {
1836   return flag_apple_kext;
1837 }
1838
1839 void
1840 darwin_override_options (void)
1841 {
1842   /* Don't emit DWARF3/4 unless specifically selected.  This is a 
1843      workaround for tool bugs.  */
1844   if (dwarf_strict < 0) 
1845     dwarf_strict = 1;
1846
1847   /* Disable -freorder-blocks-and-partition for darwin_emit_unwind_label.  */
1848   if (flag_reorder_blocks_and_partition 
1849       && (targetm.asm_out.unwind_label == darwin_emit_unwind_label))
1850     {
1851       inform (input_location,
1852               "-freorder-blocks-and-partition does not work with exceptions "
1853               "on this architecture");
1854       flag_reorder_blocks_and_partition = 0;
1855       flag_reorder_blocks = 1;
1856     }
1857
1858   if (flag_mkernel || flag_apple_kext)
1859     {
1860       /* -mkernel implies -fapple-kext for C++ */
1861       if (strcmp (lang_hooks.name, "GNU C++") == 0)
1862         flag_apple_kext = 1;
1863
1864       flag_no_common = 1;
1865
1866       /* No EH in kexts.  */
1867       flag_exceptions = 0;
1868       /* No -fnon-call-exceptions data in kexts.  */
1869       flag_non_call_exceptions = 0;
1870     }
1871   if (flag_var_tracking
1872       && strverscmp (darwin_macosx_version_min, "10.5") >= 0
1873       && debug_info_level >= DINFO_LEVEL_NORMAL
1874       && debug_hooks->var_location != do_nothing_debug_hooks.var_location)
1875     flag_var_tracking_uninit = 1;
1876 }
1877
1878 /* Add $LDBL128 suffix to long double builtins.  */
1879
1880 static void
1881 darwin_patch_builtin (int fncode)
1882 {
1883   tree fn = built_in_decls[fncode];
1884   tree sym;
1885   char *newname;
1886
1887   if (!fn)
1888     return;
1889
1890   sym = DECL_ASSEMBLER_NAME (fn);
1891   newname = ACONCAT (("_", IDENTIFIER_POINTER (sym), "$LDBL128", NULL));
1892
1893   set_user_assembler_name (fn, newname);
1894
1895   fn = implicit_built_in_decls[fncode];
1896   if (fn)
1897     set_user_assembler_name (fn, newname);
1898 }
1899
1900 void
1901 darwin_patch_builtins (void)
1902 {
1903   if (LONG_DOUBLE_TYPE_SIZE != 128)
1904     return;
1905
1906 #define PATCH_BUILTIN(fncode) darwin_patch_builtin (fncode);
1907 #define PATCH_BUILTIN_NO64(fncode)              \
1908   if (!TARGET_64BIT)                            \
1909     darwin_patch_builtin (fncode);
1910 #define PATCH_BUILTIN_VARIADIC(fncode)                            \
1911   if (!TARGET_64BIT                                               \
1912       && (strverscmp (darwin_macosx_version_min, "10.3.9") >= 0)) \
1913     darwin_patch_builtin (fncode);
1914 #include "darwin-ppc-ldouble-patch.def"
1915 #undef PATCH_BUILTIN
1916 #undef PATCH_BUILTIN_NO64
1917 #undef PATCH_BUILTIN_VARIADIC
1918 }
1919
1920
1921 #include "gt-darwin.h"