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