cgraph.h (symtab_node): Add symver flag.
[platform/upstream/gcc.git] / gcc / varasm.c
1 /* Output variables, constants and external declarations, for GNU compiler.
2    Copyright (C) 1987-2019 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
10
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19
20
21 /* This file handles generation of all the assembler code
22    *except* the instructions of a function.
23    This includes declarations of variables and their initial values.
24
25    We also output the assembler code for constants stored in memory
26    and are responsible for combining constants with the same value.  */
27
28 #include "config.h"
29 #include "system.h"
30 #include "coretypes.h"
31 #include "backend.h"
32 #include "target.h"
33 #include "rtl.h"
34 #include "tree.h"
35 #include "predict.h"
36 #include "memmodel.h"
37 #include "tm_p.h"
38 #include "stringpool.h"
39 #include "regs.h"
40 #include "emit-rtl.h"
41 #include "cgraph.h"
42 #include "diagnostic-core.h"
43 #include "fold-const.h"
44 #include "stor-layout.h"
45 #include "varasm.h"
46 #include "flags.h"
47 #include "stmt.h"
48 #include "expr.h"
49 #include "expmed.h"
50 #include "optabs.h"
51 #include "output.h"
52 #include "langhooks.h"
53 #include "debug.h"
54 #include "common/common-target.h"
55 #include "stringpool.h"
56 #include "attribs.h"
57 #include "asan.h"
58 #include "rtl-iter.h"
59 #include "file-prefix-map.h" /* remap_debug_filename()  */
60
61 #ifdef XCOFF_DEBUGGING_INFO
62 #include "xcoffout.h"           /* Needed for external data declarations.  */
63 #endif
64
65 /* The (assembler) name of the first globally-visible object output.  */
66 extern GTY(()) const char *first_global_object_name;
67 extern GTY(()) const char *weak_global_object_name;
68
69 const char *first_global_object_name;
70 const char *weak_global_object_name;
71
72 class addr_const;
73 class constant_descriptor_rtx;
74 struct rtx_constant_pool;
75
76 #define n_deferred_constants (crtl->varasm.deferred_constants)
77
78 /* Number for making the label on the next
79    constant that is stored in memory.  */
80
81 static GTY(()) int const_labelno;
82
83 /* Carry information from ASM_DECLARE_OBJECT_NAME
84    to ASM_FINISH_DECLARE_OBJECT.  */
85
86 int size_directive_output;
87
88 /* The last decl for which assemble_variable was called,
89    if it did ASM_DECLARE_OBJECT_NAME.
90    If the last call to assemble_variable didn't do that,
91    this holds 0.  */
92
93 tree last_assemble_variable_decl;
94
95 /* The following global variable indicates if the first basic block
96    in a function belongs to the cold partition or not.  */
97
98 bool first_function_block_is_cold;
99
100 /* Whether we saw any functions with no_split_stack.  */
101
102 static bool saw_no_split_stack;
103
104 static const char *strip_reg_name (const char *);
105 static int contains_pointers_p (tree);
106 #ifdef ASM_OUTPUT_EXTERNAL
107 static bool incorporeal_function_p (tree);
108 #endif
109 static void decode_addr_const (tree, class addr_const *);
110 static hashval_t const_hash_1 (const tree);
111 static int compare_constant (const tree, const tree);
112 static void output_constant_def_contents (rtx);
113 static void output_addressed_constants (tree);
114 static unsigned HOST_WIDE_INT output_constant (tree, unsigned HOST_WIDE_INT,
115                                                unsigned int, bool, bool);
116 static void globalize_decl (tree);
117 static bool decl_readonly_section_1 (enum section_category);
118 #ifdef BSS_SECTION_ASM_OP
119 #ifdef ASM_OUTPUT_ALIGNED_BSS
120 static void asm_output_aligned_bss (FILE *, tree, const char *,
121                                     unsigned HOST_WIDE_INT, int)
122      ATTRIBUTE_UNUSED;
123 #endif
124 #endif /* BSS_SECTION_ASM_OP */
125 static void mark_weak (tree);
126 static void output_constant_pool (const char *, tree);
127 static void handle_vtv_comdat_section (section *, const_tree);
128 \f
129 /* Well-known sections, each one associated with some sort of *_ASM_OP.  */
130 section *text_section;
131 section *data_section;
132 section *readonly_data_section;
133 section *sdata_section;
134 section *ctors_section;
135 section *dtors_section;
136 section *bss_section;
137 section *sbss_section;
138
139 /* Various forms of common section.  All are guaranteed to be nonnull.  */
140 section *tls_comm_section;
141 section *comm_section;
142 section *lcomm_section;
143
144 /* A SECTION_NOSWITCH section used for declaring global BSS variables.
145    May be null.  */
146 section *bss_noswitch_section;
147
148 /* The section that holds the main exception table, when known.  The section
149    is set either by the target's init_sections hook or by the first call to
150    switch_to_exception_section.  */
151 section *exception_section;
152
153 /* The section that holds the DWARF2 frame unwind information, when known.
154    The section is set either by the target's init_sections hook or by the
155    first call to switch_to_eh_frame_section.  */
156 section *eh_frame_section;
157
158 /* asm_out_file's current section.  This is NULL if no section has yet
159    been selected or if we lose track of what the current section is.  */
160 section *in_section;
161
162 /* True if code for the current function is currently being directed
163    at the cold section.  */
164 bool in_cold_section_p;
165
166 /* The following global holds the "function name" for the code in the
167    cold section of a function, if hot/cold function splitting is enabled
168    and there was actually code that went into the cold section.  A
169    pseudo function name is needed for the cold section of code for some
170    debugging tools that perform symbolization. */
171 tree cold_function_name = NULL_TREE;
172
173 /* A linked list of all the unnamed sections.  */
174 static GTY(()) section *unnamed_sections;
175
176 /* Return a nonzero value if DECL has a section attribute.  */
177 #define IN_NAMED_SECTION(DECL) \
178   (VAR_OR_FUNCTION_DECL_P (DECL) && DECL_SECTION_NAME (DECL) != NULL)
179
180 struct section_hasher : ggc_ptr_hash<section>
181 {
182   typedef const char *compare_type;
183
184   static hashval_t hash (section *);
185   static bool equal (section *, const char *);
186 };
187
188 /* Hash table of named sections.  */
189 static GTY(()) hash_table<section_hasher> *section_htab;
190
191 struct object_block_hasher : ggc_ptr_hash<object_block>
192 {
193   typedef const section *compare_type;
194
195   static hashval_t hash (object_block *);
196   static bool equal (object_block *, const section *);
197 };
198
199 /* A table of object_blocks, indexed by section.  */
200 static GTY(()) hash_table<object_block_hasher> *object_block_htab;
201
202 /* The next number to use for internal anchor labels.  */
203 static GTY(()) int anchor_labelno;
204
205 /* A pool of constants that can be shared between functions.  */
206 static GTY(()) struct rtx_constant_pool *shared_constant_pool;
207
208 /* Helper routines for maintaining section_htab.  */
209
210 bool
211 section_hasher::equal (section *old, const char *new_name)
212 {
213   return strcmp (old->named.name, new_name) == 0;
214 }
215
216 hashval_t
217 section_hasher::hash (section *old)
218 {
219   return htab_hash_string (old->named.name);
220 }
221
222 /* Return a hash value for section SECT.  */
223
224 static hashval_t
225 hash_section (section *sect)
226 {
227   if (sect->common.flags & SECTION_NAMED)
228     return htab_hash_string (sect->named.name);
229   return sect->common.flags & ~SECTION_DECLARED;
230 }
231
232 /* Helper routines for maintaining object_block_htab.  */
233
234 inline bool
235 object_block_hasher::equal (object_block *old, const section *new_section)
236 {
237   return old->sect == new_section;
238 }
239
240 hashval_t
241 object_block_hasher::hash (object_block *old)
242 {
243   return hash_section (old->sect);
244 }
245
246 /* Return a new unnamed section with the given fields.  */
247
248 section *
249 get_unnamed_section (unsigned int flags, void (*callback) (const void *),
250                      const void *data)
251 {
252   section *sect;
253
254   sect = ggc_alloc<section> ();
255   sect->unnamed.common.flags = flags | SECTION_UNNAMED;
256   sect->unnamed.callback = callback;
257   sect->unnamed.data = data;
258   sect->unnamed.next = unnamed_sections;
259
260   unnamed_sections = sect;
261   return sect;
262 }
263
264 /* Return a SECTION_NOSWITCH section with the given fields.  */
265
266 static section *
267 get_noswitch_section (unsigned int flags, noswitch_section_callback callback)
268 {
269   section *sect;
270
271   sect = ggc_alloc<section> ();
272   sect->noswitch.common.flags = flags | SECTION_NOSWITCH;
273   sect->noswitch.callback = callback;
274
275   return sect;
276 }
277
278 /* Return the named section structure associated with NAME.  Create
279    a new section with the given fields if no such structure exists.  */
280
281 section *
282 get_section (const char *name, unsigned int flags, tree decl)
283 {
284   section *sect, **slot;
285
286   slot = section_htab->find_slot_with_hash (name, htab_hash_string (name),
287                                             INSERT);
288   flags |= SECTION_NAMED;
289   if (*slot == NULL)
290     {
291       sect = ggc_alloc<section> ();
292       sect->named.common.flags = flags;
293       sect->named.name = ggc_strdup (name);
294       sect->named.decl = decl;
295       *slot = sect;
296     }
297   else
298     {
299       sect = *slot;
300       /* It is fine if one of the sections has SECTION_NOTYPE as long as
301          the other has none of the contrary flags (see the logic at the end
302          of default_section_type_flags, below).  */
303       if (((sect->common.flags ^ flags) & SECTION_NOTYPE)
304           && !((sect->common.flags | flags)
305                & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE
306                   | (HAVE_COMDAT_GROUP ? SECTION_LINKONCE : 0))))
307         {
308           sect->common.flags |= SECTION_NOTYPE;
309           flags |= SECTION_NOTYPE;
310         }
311       if ((sect->common.flags & ~SECTION_DECLARED) != flags
312           && ((sect->common.flags | flags) & SECTION_OVERRIDE) == 0)
313         {
314           /* It is fine if one of the section flags is
315              SECTION_WRITE | SECTION_RELRO and the other has none of these
316              flags (i.e. read-only) in named sections and either the
317              section hasn't been declared yet or has been declared as writable.
318              In that case just make sure the resulting flags are
319              SECTION_WRITE | SECTION_RELRO, ie. writable only because of
320              relocations.  */
321           if (((sect->common.flags ^ flags) & (SECTION_WRITE | SECTION_RELRO))
322               == (SECTION_WRITE | SECTION_RELRO)
323               && (sect->common.flags
324                   & ~(SECTION_DECLARED | SECTION_WRITE | SECTION_RELRO))
325                  == (flags & ~(SECTION_WRITE | SECTION_RELRO))
326               && ((sect->common.flags & SECTION_DECLARED) == 0
327                   || (sect->common.flags & SECTION_WRITE)))
328             {
329               sect->common.flags |= (SECTION_WRITE | SECTION_RELRO);
330               return sect;
331             }
332           /* Sanity check user variables for flag changes.  */
333           if (sect->named.decl != NULL
334               && DECL_P (sect->named.decl)
335               && decl != sect->named.decl)
336             {
337               if (decl != NULL && DECL_P (decl))
338                 error ("%+qD causes a section type conflict with %qD",
339                        decl, sect->named.decl);
340               else
341                 error ("section type conflict with %qD", sect->named.decl);
342               inform (DECL_SOURCE_LOCATION (sect->named.decl),
343                       "%qD was declared here", sect->named.decl);
344             }
345           else if (decl != NULL && DECL_P (decl))
346             error ("%+qD causes a section type conflict", decl);
347           else
348             error ("section type conflict");
349           /* Make sure we don't error about one section multiple times.  */
350           sect->common.flags |= SECTION_OVERRIDE;
351         }
352     }
353   return sect;
354 }
355
356 /* Return true if the current compilation mode benefits from having
357    objects grouped into blocks.  */
358
359 static bool
360 use_object_blocks_p (void)
361 {
362   return flag_section_anchors;
363 }
364
365 /* Return the object_block structure for section SECT.  Create a new
366    structure if we haven't created one already.  Return null if SECT
367    itself is null.  Return also null for mergeable sections since
368    section anchors can't be used in mergeable sections anyway,
369    because the linker might move objects around, and using the
370    object blocks infrastructure in that case is both a waste and a
371    maintenance burden.  */
372
373 static struct object_block *
374 get_block_for_section (section *sect)
375 {
376   struct object_block *block;
377
378   if (sect == NULL)
379     return NULL;
380
381   if (sect->common.flags & SECTION_MERGE)
382     return NULL;
383
384   object_block **slot
385     = object_block_htab->find_slot_with_hash (sect, hash_section (sect),
386                                               INSERT);
387   block = *slot;
388   if (block == NULL)
389     {
390       block = ggc_cleared_alloc<object_block> ();
391       block->sect = sect;
392       *slot = block;
393     }
394   return block;
395 }
396
397 /* Create a symbol with label LABEL and place it at byte offset
398    OFFSET in BLOCK.  OFFSET can be negative if the symbol's offset
399    is not yet known.  LABEL must be a garbage-collected string.  */
400
401 static rtx
402 create_block_symbol (const char *label, struct object_block *block,
403                      HOST_WIDE_INT offset)
404 {
405   rtx symbol;
406   unsigned int size;
407
408   /* Create the extended SYMBOL_REF.  */
409   size = RTX_HDR_SIZE + sizeof (struct block_symbol);
410   symbol = (rtx) ggc_internal_alloc (size);
411
412   /* Initialize the normal SYMBOL_REF fields.  */
413   memset (symbol, 0, size);
414   PUT_CODE (symbol, SYMBOL_REF);
415   PUT_MODE (symbol, Pmode);
416   XSTR (symbol, 0) = label;
417   SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_HAS_BLOCK_INFO;
418
419   /* Initialize the block_symbol stuff.  */
420   SYMBOL_REF_BLOCK (symbol) = block;
421   SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
422
423   return symbol;
424 }
425
426 /* Return a section with a particular name and with whatever SECTION_*
427    flags section_type_flags deems appropriate.  The name of the section
428    is taken from NAME if nonnull, otherwise it is taken from DECL's
429    DECL_SECTION_NAME.  DECL is the decl associated with the section
430    (see the section comment for details) and RELOC is as for
431    section_type_flags.  */
432
433 section *
434 get_named_section (tree decl, const char *name, int reloc)
435 {
436   unsigned int flags;
437
438   if (name == NULL)
439     {
440       gcc_assert (decl && DECL_P (decl) && DECL_SECTION_NAME (decl));
441       name = DECL_SECTION_NAME (decl);
442     }
443
444   flags = targetm.section_type_flags (decl, name, reloc);
445   return get_section (name, flags, decl);
446 }
447
448 /* Worker for resolve_unique_section.  */
449
450 static bool
451 set_implicit_section (struct symtab_node *n, void *data ATTRIBUTE_UNUSED)
452 {
453   n->implicit_section = true;
454   return false;
455 }
456
457 /* If required, set DECL_SECTION_NAME to a unique name.  */
458
459 void
460 resolve_unique_section (tree decl, int reloc ATTRIBUTE_UNUSED,
461                         int flag_function_or_data_sections)
462 {
463   if (DECL_SECTION_NAME (decl) == NULL
464       && targetm_common.have_named_sections
465       && (flag_function_or_data_sections
466           || DECL_COMDAT_GROUP (decl)))
467     {
468       targetm.asm_out.unique_section (decl, reloc);
469       if (DECL_SECTION_NAME (decl))
470         symtab_node::get (decl)->call_for_symbol_and_aliases
471           (set_implicit_section, NULL, true);
472     }
473 }
474
475 #ifdef BSS_SECTION_ASM_OP
476
477 #ifdef ASM_OUTPUT_ALIGNED_BSS
478
479 /* Utility function for targets to use in implementing
480    ASM_OUTPUT_ALIGNED_BSS.
481    ??? It is believed that this function will work in most cases so such
482    support is localized here.  */
483
484 static void
485 asm_output_aligned_bss (FILE *file, tree decl ATTRIBUTE_UNUSED,
486                         const char *name, unsigned HOST_WIDE_INT size,
487                         int align)
488 {
489   switch_to_section (bss_section);
490   ASM_OUTPUT_ALIGN (file, floor_log2 (align / BITS_PER_UNIT));
491 #ifdef ASM_DECLARE_OBJECT_NAME
492   last_assemble_variable_decl = decl;
493   ASM_DECLARE_OBJECT_NAME (file, name, decl);
494 #else
495   /* Standard thing is just output label for the object.  */
496   ASM_OUTPUT_LABEL (file, name);
497 #endif /* ASM_DECLARE_OBJECT_NAME */
498   ASM_OUTPUT_SKIP (file, size ? size : 1);
499 }
500
501 #endif
502
503 #endif /* BSS_SECTION_ASM_OP */
504
505 #ifndef USE_SELECT_SECTION_FOR_FUNCTIONS
506 /* Return the hot section for function DECL.  Return text_section for
507    null DECLs.  */
508
509 static section *
510 hot_function_section (tree decl)
511 {
512   if (decl != NULL_TREE
513       && DECL_SECTION_NAME (decl) != NULL
514       && targetm_common.have_named_sections)
515     return get_named_section (decl, NULL, 0);
516   else
517     return text_section;
518 }
519 #endif
520
521 /* Return section for TEXT_SECTION_NAME if DECL or DECL_SECTION_NAME (DECL)
522    is NULL.
523
524    When DECL_SECTION_NAME is non-NULL and it is implicit section and
525    NAMED_SECTION_SUFFIX is non-NULL, then produce section called
526    concatenate the name with NAMED_SECTION_SUFFIX.
527    Otherwise produce "TEXT_SECTION_NAME.IMPLICIT_NAME".  */
528
529 section *
530 get_named_text_section (tree decl,
531                         const char *text_section_name,
532                         const char *named_section_suffix)
533 {
534   if (decl && DECL_SECTION_NAME (decl))
535     {
536       if (named_section_suffix)
537         {
538           const char *dsn = DECL_SECTION_NAME (decl);
539           const char *stripped_name;
540           char *name, *buffer;
541
542           name = (char *) alloca (strlen (dsn) + 1);
543           memcpy (name, dsn,
544                   strlen (dsn) + 1);
545
546           stripped_name = targetm.strip_name_encoding (name);
547
548           buffer = ACONCAT ((stripped_name, named_section_suffix, NULL));
549           return get_named_section (decl, buffer, 0);
550         }
551       else if (symtab_node::get (decl)->implicit_section)
552         {
553           const char *name;
554
555           /* Do not try to split gnu_linkonce functions.  This gets somewhat
556              slipperly.  */
557           if (DECL_COMDAT_GROUP (decl) && !HAVE_COMDAT_GROUP)
558             return NULL;
559           name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
560           name = targetm.strip_name_encoding (name);
561           return get_named_section (decl, ACONCAT ((text_section_name, ".",
562                                                    name, NULL)), 0);
563         }
564       else
565         return NULL;
566     }
567   return get_named_section (decl, text_section_name, 0);
568 }
569
570 /* Choose named function section based on its frequency.  */
571
572 section *
573 default_function_section (tree decl, enum node_frequency freq,
574                           bool startup, bool exit)
575 {
576 #if defined HAVE_LD_EH_GC_SECTIONS && defined HAVE_LD_EH_GC_SECTIONS_BUG
577   /* Old GNU linkers have buggy --gc-section support, which sometimes
578      results in .gcc_except_table* sections being garbage collected.  */
579   if (decl
580       && symtab_node::get (decl)->implicit_section)
581     return NULL;
582 #endif
583
584   if (!flag_reorder_functions
585       || !targetm_common.have_named_sections)
586     return NULL;
587   /* Startup code should go to startup subsection unless it is
588      unlikely executed (this happens especially with function splitting
589      where we can split away unnecessary parts of static constructors.  */
590   if (startup && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
591   {
592     /* If we do have a profile or(and) LTO phase is executed, we do not need
593        these ELF section.  */
594     if (!in_lto_p || !flag_profile_values)
595       return get_named_text_section (decl, ".text.startup", NULL);
596     else
597       return NULL;
598   }
599
600   /* Similarly for exit.  */
601   if (exit && freq != NODE_FREQUENCY_UNLIKELY_EXECUTED)
602     return get_named_text_section (decl, ".text.exit", NULL);
603
604   /* Group cold functions together, similarly for hot code.  */
605   switch (freq)
606     {
607       case NODE_FREQUENCY_UNLIKELY_EXECUTED:
608         return get_named_text_section (decl, ".text.unlikely", NULL);
609       case NODE_FREQUENCY_HOT:
610         /* If we do have a profile or(and) LTO phase is executed, we do not need
611            these ELF section.  */
612         if (!in_lto_p || !flag_profile_values)
613           return get_named_text_section (decl, ".text.hot", NULL);
614         /* FALLTHRU */
615       default:
616         return NULL;
617     }
618 }
619
620 /* Return the section for function DECL.
621
622    If DECL is NULL_TREE, return the text section.  We can be passed
623    NULL_TREE under some circumstances by dbxout.c at least.
624
625    If FORCE_COLD is true, return cold function section ignoring
626    the frequency info of cgraph_node.  */
627
628 static section *
629 function_section_1 (tree decl, bool force_cold)
630 {
631   section *section = NULL;
632   enum node_frequency freq = NODE_FREQUENCY_NORMAL;
633   bool startup = false, exit = false;
634
635   if (decl)
636     {
637       struct cgraph_node *node = cgraph_node::get (decl);
638
639       if (node)
640         {
641           freq = node->frequency;
642           startup = node->only_called_at_startup;
643           exit = node->only_called_at_exit;
644         }
645     }
646   if (force_cold)
647     freq = NODE_FREQUENCY_UNLIKELY_EXECUTED;
648
649 #ifdef USE_SELECT_SECTION_FOR_FUNCTIONS
650   if (decl != NULL_TREE
651       && DECL_SECTION_NAME (decl) != NULL)
652     {
653       if (targetm.asm_out.function_section)
654         section = targetm.asm_out.function_section (decl, freq,
655                                                     startup, exit);
656       if (section)
657         return section;
658       return get_named_section (decl, NULL, 0);
659     }
660   else
661     return targetm.asm_out.select_section
662             (decl, freq == NODE_FREQUENCY_UNLIKELY_EXECUTED,
663              symtab_node::get (decl)->definition_alignment ());
664 #else
665   if (targetm.asm_out.function_section)
666     section = targetm.asm_out.function_section (decl, freq, startup, exit);
667   if (section)
668     return section;
669   return hot_function_section (decl);
670 #endif
671 }
672
673 /* Return the section for function DECL.
674
675    If DECL is NULL_TREE, return the text section.  We can be passed
676    NULL_TREE under some circumstances by dbxout.c at least.  */
677
678 section *
679 function_section (tree decl)
680 {
681   /* Handle cases where function splitting code decides
682      to put function entry point into unlikely executed section
683      despite the fact that the function itself is not cold
684      (i.e. it is called rarely but contains a hot loop that is
685      better to live in hot subsection for the code locality).  */
686   return function_section_1 (decl,
687                              first_function_block_is_cold);
688 }
689
690 /* Return the section for the current function, take IN_COLD_SECTION_P
691    into account.  */
692
693 section *
694 current_function_section (void)
695 {
696   return function_section_1 (current_function_decl, in_cold_section_p);
697 }
698
699 /* Tell assembler to switch to unlikely-to-be-executed text section.  */
700
701 section *
702 unlikely_text_section (void)
703 {
704   return function_section_1 (current_function_decl, true);
705 }
706
707 /* When called within a function context, return true if the function
708    has been assigned a cold text section and if SECT is that section.
709    When called outside a function context, return true if SECT is the
710    default cold section.  */
711
712 bool
713 unlikely_text_section_p (section *sect)
714 {
715   return sect == function_section_1 (current_function_decl, true);
716 }
717
718 /* Switch to the other function partition (if inside of hot section
719    into cold section, otherwise into the hot section).  */
720
721 void
722 switch_to_other_text_partition (void)
723 {
724   in_cold_section_p = !in_cold_section_p;
725   switch_to_section (current_function_section ());
726 }
727
728 /* Return the read-only data section associated with function DECL.  */
729
730 section *
731 default_function_rodata_section (tree decl)
732 {
733   if (decl != NULL_TREE && DECL_SECTION_NAME (decl))
734     {
735       const char *name = DECL_SECTION_NAME (decl);
736
737       if (DECL_COMDAT_GROUP (decl) && HAVE_COMDAT_GROUP)
738         {
739           const char *dot;
740           size_t len;
741           char* rname;
742
743           dot = strchr (name + 1, '.');
744           if (!dot)
745             dot = name;
746           len = strlen (dot) + 8;
747           rname = (char *) alloca (len);
748
749           strcpy (rname, ".rodata");
750           strcat (rname, dot);
751           return get_section (rname, SECTION_LINKONCE, decl);
752         }
753       /* For .gnu.linkonce.t.foo we want to use .gnu.linkonce.r.foo.  */
754       else if (DECL_COMDAT_GROUP (decl)
755                && strncmp (name, ".gnu.linkonce.t.", 16) == 0)
756         {
757           size_t len = strlen (name) + 1;
758           char *rname = (char *) alloca (len);
759
760           memcpy (rname, name, len);
761           rname[14] = 'r';
762           return get_section (rname, SECTION_LINKONCE, decl);
763         }
764       /* For .text.foo we want to use .rodata.foo.  */
765       else if (flag_function_sections && flag_data_sections
766                && strncmp (name, ".text.", 6) == 0)
767         {
768           size_t len = strlen (name) + 1;
769           char *rname = (char *) alloca (len + 2);
770
771           memcpy (rname, ".rodata", 7);
772           memcpy (rname + 7, name + 5, len - 5);
773           return get_section (rname, 0, decl);
774         }
775     }
776
777   return readonly_data_section;
778 }
779
780 /* Return the read-only data section associated with function DECL
781    for targets where that section should be always the single
782    readonly data section.  */
783
784 section *
785 default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
786 {
787   return readonly_data_section;
788 }
789
790 /* A subroutine of mergeable_string_section and mergeable_constant_section.  */
791
792 static const char *
793 function_mergeable_rodata_prefix (void)
794 {
795   section *s = targetm.asm_out.function_rodata_section (current_function_decl);
796   if (SECTION_STYLE (s) == SECTION_NAMED)
797     return s->named.name;
798   else
799     return targetm.asm_out.mergeable_rodata_prefix;
800 }
801
802 /* Return the section to use for string merging.  */
803
804 static section *
805 mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
806                           unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
807                           unsigned int flags ATTRIBUTE_UNUSED)
808 {
809   HOST_WIDE_INT len;
810
811   if (HAVE_GAS_SHF_MERGE && flag_merge_constants
812       && TREE_CODE (decl) == STRING_CST
813       && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
814       && align <= 256
815       && (len = int_size_in_bytes (TREE_TYPE (decl))) > 0
816       && TREE_STRING_LENGTH (decl) == len)
817     {
818       scalar_int_mode mode;
819       unsigned int modesize;
820       const char *str;
821       HOST_WIDE_INT i;
822       int j, unit;
823       const char *prefix = function_mergeable_rodata_prefix ();
824       char *name = (char *) alloca (strlen (prefix) + 30);
825
826       mode = SCALAR_INT_TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
827       modesize = GET_MODE_BITSIZE (mode);
828       if (modesize >= 8 && modesize <= 256
829           && (modesize & (modesize - 1)) == 0)
830         {
831           if (align < modesize)
832             align = modesize;
833
834           if (!HAVE_LD_ALIGNED_SHF_MERGE && align > 8)
835             return readonly_data_section;
836
837           str = TREE_STRING_POINTER (decl);
838           unit = GET_MODE_SIZE (mode);
839
840           /* Check for embedded NUL characters.  */
841           for (i = 0; i < len; i += unit)
842             {
843               for (j = 0; j < unit; j++)
844                 if (str[i + j] != '\0')
845                   break;
846               if (j == unit)
847                 break;
848             }
849           if (i == len - unit || (unit == 1 && i == len))
850             {
851               sprintf (name, "%s.str%d.%d", prefix,
852                        modesize / 8, (int) (align / 8));
853               flags |= (modesize / 8) | SECTION_MERGE | SECTION_STRINGS;
854               return get_section (name, flags, NULL);
855             }
856         }
857     }
858
859   return readonly_data_section;
860 }
861
862 /* Return the section to use for constant merging.  */
863
864 section *
865 mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
866                             unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED,
867                             unsigned int flags ATTRIBUTE_UNUSED)
868 {
869   if (HAVE_GAS_SHF_MERGE && flag_merge_constants
870       && mode != VOIDmode
871       && mode != BLKmode
872       && known_le (GET_MODE_BITSIZE (mode), align)
873       && align >= 8
874       && align <= 256
875       && (align & (align - 1)) == 0
876       && (HAVE_LD_ALIGNED_SHF_MERGE ? 1 : align == 8))
877     {
878       const char *prefix = function_mergeable_rodata_prefix ();
879       char *name = (char *) alloca (strlen (prefix) + 30);
880
881       sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
882       flags |= (align / 8) | SECTION_MERGE;
883       return get_section (name, flags, NULL);
884     }
885   return readonly_data_section;
886 }
887 \f
888 /* Given NAME, a putative register name, discard any customary prefixes.  */
889
890 static const char *
891 strip_reg_name (const char *name)
892 {
893 #ifdef REGISTER_PREFIX
894   if (!strncmp (name, REGISTER_PREFIX, strlen (REGISTER_PREFIX)))
895     name += strlen (REGISTER_PREFIX);
896 #endif
897   if (name[0] == '%' || name[0] == '#')
898     name++;
899   return name;
900 }
901 \f
902 /* The user has asked for a DECL to have a particular name.  Set (or
903    change) it in such a way that we don't prefix an underscore to
904    it.  */
905 void
906 set_user_assembler_name (tree decl, const char *name)
907 {
908   char *starred = (char *) alloca (strlen (name) + 2);
909   starred[0] = '*';
910   strcpy (starred + 1, name);
911   symtab->change_decl_assembler_name (decl, get_identifier (starred));
912   SET_DECL_RTL (decl, NULL_RTX);
913 }
914 \f
915 /* Decode an `asm' spec for a declaration as a register name.
916    Return the register number, or -1 if nothing specified,
917    or -2 if the ASMSPEC is not `cc' or `memory' and is not recognized,
918    or -3 if ASMSPEC is `cc' and is not recognized,
919    or -4 if ASMSPEC is `memory' and is not recognized.
920    Accept an exact spelling or a decimal number.
921    Prefixes such as % are optional.  */
922
923 int
924 decode_reg_name_and_count (const char *asmspec, int *pnregs)
925 {
926   /* Presume just one register is clobbered.  */
927   *pnregs = 1;
928
929   if (asmspec != 0)
930     {
931       int i;
932
933       /* Get rid of confusing prefixes.  */
934       asmspec = strip_reg_name (asmspec);
935
936       /* Allow a decimal number as a "register name".  */
937       for (i = strlen (asmspec) - 1; i >= 0; i--)
938         if (! ISDIGIT (asmspec[i]))
939           break;
940       if (asmspec[0] != 0 && i < 0)
941         {
942           i = atoi (asmspec);
943           if (i < FIRST_PSEUDO_REGISTER && i >= 0 && reg_names[i][0])
944             return i;
945           else
946             return -2;
947         }
948
949       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
950         if (reg_names[i][0]
951             && ! strcmp (asmspec, strip_reg_name (reg_names[i])))
952           return i;
953
954 #ifdef OVERLAPPING_REGISTER_NAMES
955       {
956         static const struct
957         {
958           const char *const name;
959           const int number;
960           const int nregs;
961         } table[] = OVERLAPPING_REGISTER_NAMES;
962
963         for (i = 0; i < (int) ARRAY_SIZE (table); i++)
964           if (table[i].name[0]
965               && ! strcmp (asmspec, table[i].name))
966             {
967               *pnregs = table[i].nregs;
968               return table[i].number;
969             }
970       }
971 #endif /* OVERLAPPING_REGISTER_NAMES */
972
973 #ifdef ADDITIONAL_REGISTER_NAMES
974       {
975         static const struct { const char *const name; const int number; } table[]
976           = ADDITIONAL_REGISTER_NAMES;
977
978         for (i = 0; i < (int) ARRAY_SIZE (table); i++)
979           if (table[i].name[0]
980               && ! strcmp (asmspec, table[i].name)
981               && reg_names[table[i].number][0])
982             return table[i].number;
983       }
984 #endif /* ADDITIONAL_REGISTER_NAMES */
985
986       if (!strcmp (asmspec, "memory"))
987         return -4;
988
989       if (!strcmp (asmspec, "cc"))
990         return -3;
991
992       return -2;
993     }
994
995   return -1;
996 }
997
998 int
999 decode_reg_name (const char *name)
1000 {
1001   int count;
1002   return decode_reg_name_and_count (name, &count);
1003 }
1004
1005 \f
1006 /* Return true if DECL's initializer is suitable for a BSS section.  */
1007
1008 bool
1009 bss_initializer_p (const_tree decl, bool named)
1010 {
1011   /* Do not put non-common constants into the .bss section, they belong in
1012      a readonly section, except when NAMED is true.  */
1013   return ((!TREE_READONLY (decl) || DECL_COMMON (decl) || named)
1014           && (DECL_INITIAL (decl) == NULL
1015               /* In LTO we have no errors in program; error_mark_node is used
1016                  to mark offlined constructors.  */
1017               || (DECL_INITIAL (decl) == error_mark_node
1018                   && !in_lto_p)
1019               || (flag_zero_initialized_in_bss
1020                   && initializer_zerop (DECL_INITIAL (decl)))));
1021 }
1022
1023 /* Compute the alignment of variable specified by DECL.
1024    DONT_OUTPUT_DATA is from assemble_variable.  */
1025
1026 void
1027 align_variable (tree decl, bool dont_output_data)
1028 {
1029   unsigned int align = DECL_ALIGN (decl);
1030
1031   /* In the case for initialing an array whose length isn't specified,
1032      where we have not yet been able to do the layout,
1033      figure out the proper alignment now.  */
1034   if (dont_output_data && DECL_SIZE (decl) == 0
1035       && TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
1036     align = MAX (align, TYPE_ALIGN (TREE_TYPE (TREE_TYPE (decl))));
1037
1038   /* Some object file formats have a maximum alignment which they support.
1039      In particular, a.out format supports a maximum alignment of 4.  */
1040   if (align > MAX_OFILE_ALIGNMENT)
1041     {
1042       error ("alignment of %q+D is greater than maximum object "
1043              "file alignment %d", decl,
1044              MAX_OFILE_ALIGNMENT/BITS_PER_UNIT);
1045       align = MAX_OFILE_ALIGNMENT;
1046     }
1047
1048   if (! DECL_USER_ALIGN (decl))
1049     {
1050 #ifdef DATA_ABI_ALIGNMENT
1051       unsigned int data_abi_align
1052         = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1053       /* For backwards compatibility, don't assume the ABI alignment for
1054          TLS variables.  */
1055       if (! DECL_THREAD_LOCAL_P (decl) || data_abi_align <= BITS_PER_WORD)
1056         align = data_abi_align;
1057 #endif
1058
1059       /* On some machines, it is good to increase alignment sometimes.
1060          But as DECL_ALIGN is used both for actually emitting the variable
1061          and for code accessing the variable as guaranteed alignment, we
1062          can only increase the alignment if it is a performance optimization
1063          if the references to it must bind to the current definition.  */
1064       if (decl_binds_to_current_def_p (decl)
1065           && !DECL_VIRTUAL_P (decl))
1066         {
1067 #ifdef DATA_ALIGNMENT
1068           unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1069           /* Don't increase alignment too much for TLS variables - TLS space
1070              is too precious.  */
1071           if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1072             align = data_align;
1073 #endif
1074           if (DECL_INITIAL (decl) != 0
1075               /* In LTO we have no errors in program; error_mark_node is used
1076                  to mark offlined constructors.  */
1077               && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1078             {
1079               unsigned int const_align
1080                 = targetm.constant_alignment (DECL_INITIAL (decl), align);
1081               /* Don't increase alignment too much for TLS variables - TLS
1082                  space is too precious.  */
1083               if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1084                 align = const_align;
1085             }
1086         }
1087     }
1088
1089   /* Reset the alignment in case we have made it tighter, so we can benefit
1090      from it in get_pointer_alignment.  */
1091   SET_DECL_ALIGN (decl, align);
1092 }
1093
1094 /* Return DECL_ALIGN (decl), possibly increased for optimization purposes
1095    beyond what align_variable returned.  */
1096
1097 static unsigned int
1098 get_variable_align (tree decl)
1099 {
1100   unsigned int align = DECL_ALIGN (decl);
1101
1102   /* For user aligned vars or static vars align_variable already did
1103      everything.  */
1104   if (DECL_USER_ALIGN (decl) || !TREE_PUBLIC (decl))
1105     return align;
1106
1107 #ifdef DATA_ABI_ALIGNMENT
1108   if (DECL_THREAD_LOCAL_P (decl))
1109     align = DATA_ABI_ALIGNMENT (TREE_TYPE (decl), align);
1110 #endif
1111
1112   /* For decls that bind to the current definition, align_variable
1113      did also everything, except for not assuming ABI required alignment
1114      of TLS variables.  For other vars, increase the alignment here
1115      as an optimization.  */
1116   if (!decl_binds_to_current_def_p (decl))
1117     {
1118       /* On some machines, it is good to increase alignment sometimes.  */
1119 #ifdef DATA_ALIGNMENT
1120       unsigned int data_align = DATA_ALIGNMENT (TREE_TYPE (decl), align);
1121       /* Don't increase alignment too much for TLS variables - TLS space
1122          is too precious.  */
1123       if (! DECL_THREAD_LOCAL_P (decl) || data_align <= BITS_PER_WORD)
1124         align = data_align;
1125 #endif
1126       if (DECL_INITIAL (decl) != 0
1127           /* In LTO we have no errors in program; error_mark_node is used
1128              to mark offlined constructors.  */
1129           && (in_lto_p || DECL_INITIAL (decl) != error_mark_node))
1130         {
1131           unsigned int const_align
1132             = targetm.constant_alignment (DECL_INITIAL (decl), align);
1133           /* Don't increase alignment too much for TLS variables - TLS space
1134              is too precious.  */
1135           if (! DECL_THREAD_LOCAL_P (decl) || const_align <= BITS_PER_WORD)
1136             align = const_align;
1137         }
1138     }
1139
1140   return align;
1141 }
1142
1143 /* Return the section into which the given VAR_DECL or CONST_DECL
1144    should be placed.  PREFER_NOSWITCH_P is true if a noswitch
1145    section should be used wherever possible.  */
1146
1147 section *
1148 get_variable_section (tree decl, bool prefer_noswitch_p)
1149 {
1150   addr_space_t as = ADDR_SPACE_GENERIC;
1151   int reloc;
1152   varpool_node *vnode = varpool_node::get (decl);
1153   if (vnode)
1154     {
1155       vnode = vnode->ultimate_alias_target ();
1156       decl = vnode->decl;
1157     }
1158
1159   if (TREE_TYPE (decl) != error_mark_node)
1160     as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1161
1162   /* We need the constructor to figure out reloc flag.  */
1163   if (vnode)
1164     vnode->get_constructor ();
1165
1166   if (DECL_COMMON (decl))
1167     {
1168       /* If the decl has been given an explicit section name, or it resides
1169          in a non-generic address space, then it isn't common, and shouldn't
1170          be handled as such.  */
1171       gcc_assert (DECL_SECTION_NAME (decl) == NULL
1172                   && ADDR_SPACE_GENERIC_P (as));
1173       if (DECL_THREAD_LOCAL_P (decl))
1174         return tls_comm_section;
1175       else if (TREE_PUBLIC (decl) && bss_initializer_p (decl))
1176         return comm_section;
1177     }
1178
1179   if (DECL_INITIAL (decl) == error_mark_node)
1180     reloc = contains_pointers_p (TREE_TYPE (decl)) ? 3 : 0;
1181   else if (DECL_INITIAL (decl))
1182     reloc = compute_reloc_for_constant (DECL_INITIAL (decl));
1183   else
1184     reloc = 0;
1185
1186   resolve_unique_section (decl, reloc, flag_data_sections);
1187   if (IN_NAMED_SECTION (decl))
1188     {
1189       section *sect = get_named_section (decl, NULL, reloc);
1190
1191       if ((sect->common.flags & SECTION_BSS)
1192           && !bss_initializer_p (decl, true))
1193         {
1194           error_at (DECL_SOURCE_LOCATION (decl),
1195                     "only zero initializers are allowed in section %qs",
1196                     sect->named.name);
1197           DECL_INITIAL (decl) = error_mark_node;
1198         }
1199       return sect;
1200     }
1201
1202   if (ADDR_SPACE_GENERIC_P (as)
1203       && !DECL_THREAD_LOCAL_P (decl)
1204       && !(prefer_noswitch_p && targetm.have_switchable_bss_sections)
1205       && bss_initializer_p (decl))
1206     {
1207       if (!TREE_PUBLIC (decl)
1208           && !((flag_sanitize & SANITIZE_ADDRESS)
1209                && asan_protect_global (decl)))
1210         return lcomm_section;
1211       if (bss_noswitch_section)
1212         return bss_noswitch_section;
1213     }
1214
1215   return targetm.asm_out.select_section (decl, reloc,
1216                                          get_variable_align (decl));
1217 }
1218
1219 /* Return the block into which object_block DECL should be placed.  */
1220
1221 static struct object_block *
1222 get_block_for_decl (tree decl)
1223 {
1224   section *sect;
1225
1226   if (VAR_P (decl))
1227     {
1228       /* The object must be defined in this translation unit.  */
1229       if (DECL_EXTERNAL (decl))
1230         return NULL;
1231
1232       /* There's no point using object blocks for something that is
1233          isolated by definition.  */
1234       if (DECL_COMDAT_GROUP (decl))
1235         return NULL;
1236     }
1237
1238   /* We can only calculate block offsets if the decl has a known
1239      constant size.  */
1240   if (DECL_SIZE_UNIT (decl) == NULL)
1241     return NULL;
1242   if (!tree_fits_uhwi_p (DECL_SIZE_UNIT (decl)))
1243     return NULL;
1244
1245   /* Find out which section should contain DECL.  We cannot put it into
1246      an object block if it requires a standalone definition.  */
1247   if (VAR_P (decl))
1248       align_variable (decl, 0);
1249   sect = get_variable_section (decl, true);
1250   if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
1251     return NULL;
1252
1253   return get_block_for_section (sect);
1254 }
1255
1256 /* Make sure block symbol SYMBOL is in block BLOCK.  */
1257
1258 static void
1259 change_symbol_block (rtx symbol, struct object_block *block)
1260 {
1261   if (block != SYMBOL_REF_BLOCK (symbol))
1262     {
1263       gcc_assert (SYMBOL_REF_BLOCK_OFFSET (symbol) < 0);
1264       SYMBOL_REF_BLOCK (symbol) = block;
1265     }
1266 }
1267
1268 /* Return true if it is possible to put DECL in an object_block.  */
1269
1270 static bool
1271 use_blocks_for_decl_p (tree decl)
1272 {
1273   struct symtab_node *snode;
1274
1275   /* Only data DECLs can be placed into object blocks.  */
1276   if (!VAR_P (decl) && TREE_CODE (decl) != CONST_DECL)
1277     return false;
1278
1279   /* DECL_INITIAL (decl) set to decl is a hack used for some decls that
1280      are never used from code directly and we never want object block handling
1281      for those.  */
1282   if (DECL_INITIAL (decl) == decl)
1283     return false;
1284
1285   /* If this decl is an alias, then we don't want to emit a
1286      definition.  */
1287   if (VAR_P (decl)
1288       && (snode = symtab_node::get (decl)) != NULL
1289       && snode->alias)
1290     return false;
1291
1292   return targetm.use_blocks_for_decl_p (decl);
1293 }
1294
1295 /* Follow the IDENTIFIER_TRANSPARENT_ALIAS chain starting at *ALIAS
1296    until we find an identifier that is not itself a transparent alias.
1297    Modify the alias passed to it by reference (and all aliases on the
1298    way to the ultimate target), such that they do not have to be
1299    followed again, and return the ultimate target of the alias
1300    chain.  */
1301
1302 static inline tree
1303 ultimate_transparent_alias_target (tree *alias)
1304 {
1305   tree target = *alias;
1306
1307   if (IDENTIFIER_TRANSPARENT_ALIAS (target))
1308     {
1309       gcc_assert (TREE_CHAIN (target));
1310       target = ultimate_transparent_alias_target (&TREE_CHAIN (target));
1311       gcc_assert (! IDENTIFIER_TRANSPARENT_ALIAS (target)
1312                   && ! TREE_CHAIN (target));
1313       *alias = target;
1314     }
1315
1316   return target;
1317 }
1318
1319 /* Create the DECL_RTL for a VAR_DECL or FUNCTION_DECL.  DECL should
1320    have static storage duration.  In other words, it should not be an
1321    automatic variable, including PARM_DECLs.
1322
1323    There is, however, one exception: this function handles variables
1324    explicitly placed in a particular register by the user.
1325
1326    This is never called for PARM_DECL nodes.  */
1327
1328 void
1329 make_decl_rtl (tree decl)
1330 {
1331   const char *name = 0;
1332   int reg_number;
1333   tree id;
1334   rtx x;
1335
1336   /* Check that we are not being given an automatic variable.  */
1337   gcc_assert (TREE_CODE (decl) != PARM_DECL
1338               && TREE_CODE (decl) != RESULT_DECL);
1339
1340   /* A weak alias has TREE_PUBLIC set but not the other bits.  */
1341   gcc_assert (!VAR_P (decl)
1342               || TREE_STATIC (decl)
1343               || TREE_PUBLIC (decl)
1344               || DECL_EXTERNAL (decl)
1345               || DECL_REGISTER (decl));
1346
1347   /* And that we were not given a type or a label.  */
1348   gcc_assert (TREE_CODE (decl) != TYPE_DECL
1349               && TREE_CODE (decl) != LABEL_DECL);
1350
1351   /* For a duplicate declaration, we can be called twice on the
1352      same DECL node.  Don't discard the RTL already made.  */
1353   if (DECL_RTL_SET_P (decl))
1354     {
1355       /* If the old RTL had the wrong mode, fix the mode.  */
1356       x = DECL_RTL (decl);
1357       if (GET_MODE (x) != DECL_MODE (decl))
1358         SET_DECL_RTL (decl, adjust_address_nv (x, DECL_MODE (decl), 0));
1359
1360       if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1361         return;
1362
1363       /* ??? Another way to do this would be to maintain a hashed
1364          table of such critters.  Instead of adding stuff to a DECL
1365          to give certain attributes to it, we could use an external
1366          hash map from DECL to set of attributes.  */
1367
1368       /* Let the target reassign the RTL if it wants.
1369          This is necessary, for example, when one machine specific
1370          decl attribute overrides another.  */
1371       targetm.encode_section_info (decl, DECL_RTL (decl), false);
1372
1373       /* If the symbol has a SYMBOL_REF_BLOCK field, update it based
1374          on the new decl information.  */
1375       if (MEM_P (x)
1376           && GET_CODE (XEXP (x, 0)) == SYMBOL_REF
1377           && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (x, 0)))
1378         change_symbol_block (XEXP (x, 0), get_block_for_decl (decl));
1379
1380       return;
1381     }
1382
1383   /* If this variable belongs to the global constant pool, retrieve the
1384      pre-computed RTL or recompute it in LTO mode.  */
1385   if (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
1386     {
1387       SET_DECL_RTL (decl, output_constant_def (DECL_INITIAL (decl), 1));
1388       return;
1389     }
1390
1391   id = DECL_ASSEMBLER_NAME (decl);
1392   name = IDENTIFIER_POINTER (id);
1393
1394   if (name[0] != '*' && TREE_CODE (decl) != FUNCTION_DECL
1395       && DECL_REGISTER (decl))
1396     {
1397       error ("register name not specified for %q+D", decl);
1398     }
1399   else if (TREE_CODE (decl) != FUNCTION_DECL && DECL_REGISTER (decl))
1400     {
1401       const char *asmspec = name+1;
1402       machine_mode mode = DECL_MODE (decl);
1403       reg_number = decode_reg_name (asmspec);
1404       /* First detect errors in declaring global registers.  */
1405       if (reg_number == -1)
1406         error ("register name not specified for %q+D", decl);
1407       else if (reg_number < 0)
1408         error ("invalid register name for %q+D", decl);
1409       else if (mode == BLKmode)
1410         error ("data type of %q+D isn%'t suitable for a register",
1411                decl);
1412       else if (!in_hard_reg_set_p (accessible_reg_set, mode, reg_number))
1413         error ("the register specified for %q+D cannot be accessed"
1414                " by the current target", decl);
1415       else if (!in_hard_reg_set_p (operand_reg_set, mode, reg_number))
1416         error ("the register specified for %q+D is not general enough"
1417                " to be used as a register variable", decl);
1418       else if (!targetm.hard_regno_mode_ok (reg_number, mode))
1419         error ("register specified for %q+D isn%'t suitable for data type",
1420                decl);
1421       /* Now handle properly declared static register variables.  */
1422       else
1423         {
1424           int nregs;
1425
1426           if (DECL_INITIAL (decl) != 0 && TREE_STATIC (decl))
1427             {
1428               DECL_INITIAL (decl) = 0;
1429               error ("global register variable has initial value");
1430             }
1431           if (TREE_THIS_VOLATILE (decl))
1432             warning (OPT_Wvolatile_register_var,
1433                      "optimization may eliminate reads and/or "
1434                      "writes to register variables");
1435
1436           /* If the user specified one of the eliminables registers here,
1437              e.g., FRAME_POINTER_REGNUM, we don't want to get this variable
1438              confused with that register and be eliminated.  This usage is
1439              somewhat suspect...  */
1440
1441           SET_DECL_RTL (decl, gen_raw_REG (mode, reg_number));
1442           ORIGINAL_REGNO (DECL_RTL (decl)) = reg_number;
1443           REG_USERVAR_P (DECL_RTL (decl)) = 1;
1444
1445           if (TREE_STATIC (decl))
1446             {
1447               /* Make this register global, so not usable for anything
1448                  else.  */
1449 #ifdef ASM_DECLARE_REGISTER_GLOBAL
1450               name = IDENTIFIER_POINTER (DECL_NAME (decl));
1451               ASM_DECLARE_REGISTER_GLOBAL (asm_out_file, decl, reg_number, name);
1452 #endif
1453               nregs = hard_regno_nregs (reg_number, mode);
1454               while (nregs > 0)
1455                 globalize_reg (decl, reg_number + --nregs);
1456             }
1457
1458           /* As a register variable, it has no section.  */
1459           return;
1460         }
1461       /* Avoid internal errors from invalid register
1462          specifications.  */
1463       SET_DECL_ASSEMBLER_NAME (decl, NULL_TREE);
1464       DECL_HARD_REGISTER (decl) = 0;
1465       /* Also avoid SSA inconsistencies by pretending this is an external
1466          decl now.  */
1467       DECL_EXTERNAL (decl) = 1;
1468       return;
1469     }
1470   /* Now handle ordinary static variables and functions (in memory).
1471      Also handle vars declared register invalidly.  */
1472   else if (name[0] == '*')
1473   {
1474 #ifdef REGISTER_PREFIX
1475     if (strlen (REGISTER_PREFIX) != 0)
1476       {
1477         reg_number = decode_reg_name (name);
1478         if (reg_number >= 0 || reg_number == -3)
1479           error ("register name given for non-register variable %q+D", decl);
1480       }
1481 #endif
1482   }
1483
1484   /* Specifying a section attribute on a variable forces it into a
1485      non-.bss section, and thus it cannot be common.  */
1486   /* FIXME: In general this code should not be necessary because
1487      visibility pass is doing the same work.  But notice_global_symbol
1488      is called early and it needs to make DECL_RTL to get the name.
1489      we take care of recomputing the DECL_RTL after visibility is changed.  */
1490   if (VAR_P (decl)
1491       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))
1492       && DECL_SECTION_NAME (decl) != NULL
1493       && DECL_INITIAL (decl) == NULL_TREE
1494       && DECL_COMMON (decl))
1495     DECL_COMMON (decl) = 0;
1496
1497   /* Variables can't be both common and weak.  */
1498   if (VAR_P (decl) && DECL_WEAK (decl))
1499     DECL_COMMON (decl) = 0;
1500
1501   if (use_object_blocks_p () && use_blocks_for_decl_p (decl))
1502     x = create_block_symbol (name, get_block_for_decl (decl), -1);
1503   else
1504     {
1505       machine_mode address_mode = Pmode;
1506       if (TREE_TYPE (decl) != error_mark_node)
1507         {
1508           addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (decl));
1509           address_mode = targetm.addr_space.address_mode (as);
1510         }
1511       x = gen_rtx_SYMBOL_REF (address_mode, name);
1512     }
1513   SYMBOL_REF_WEAK (x) = DECL_WEAK (decl);
1514   SET_SYMBOL_REF_DECL (x, decl);
1515
1516   x = gen_rtx_MEM (DECL_MODE (decl), x);
1517   if (TREE_CODE (decl) != FUNCTION_DECL)
1518     set_mem_attributes (x, decl, 1);
1519   SET_DECL_RTL (decl, x);
1520
1521   /* Optionally set flags or add text to the name to record information
1522      such as that it is a function name.
1523      If the name is changed, the macro ASM_OUTPUT_LABELREF
1524      will have to know how to strip this information.  */
1525   targetm.encode_section_info (decl, DECL_RTL (decl), true);
1526 }
1527
1528 /* Like make_decl_rtl, but inhibit creation of new alias sets when
1529    calling make_decl_rtl.  Also, reset DECL_RTL before returning the
1530    rtl.  */
1531
1532 rtx
1533 make_decl_rtl_for_debug (tree decl)
1534 {
1535   unsigned int save_aliasing_flag;
1536   rtx rtl;
1537
1538   if (DECL_RTL_SET_P (decl))
1539     return DECL_RTL (decl);
1540
1541   /* Kludge alert!  Somewhere down the call chain, make_decl_rtl will
1542      call new_alias_set.  If running with -fcompare-debug, sometimes
1543      we do not want to create alias sets that will throw the alias
1544      numbers off in the comparison dumps.  So... clearing
1545      flag_strict_aliasing will keep new_alias_set() from creating a
1546      new set.  */
1547   save_aliasing_flag = flag_strict_aliasing;
1548   flag_strict_aliasing = 0;
1549
1550   rtl = DECL_RTL (decl);
1551   /* Reset DECL_RTL back, as various parts of the compiler expects
1552      DECL_RTL set meaning it is actually going to be output.  */
1553   SET_DECL_RTL (decl, NULL);
1554
1555   flag_strict_aliasing = save_aliasing_flag;
1556   return rtl;
1557 }
1558 \f
1559 /* Output a string of literal assembler code
1560    for an `asm' keyword used between functions.  */
1561
1562 void
1563 assemble_asm (tree string)
1564 {
1565   const char *p;
1566   app_enable ();
1567
1568   if (TREE_CODE (string) == ADDR_EXPR)
1569     string = TREE_OPERAND (string, 0);
1570
1571   p = TREE_STRING_POINTER (string);
1572   fprintf (asm_out_file, "%s%s\n", p[0] == '\t' ? "" : "\t", p);
1573 }
1574
1575 /* Write the address of the entity given by SYMBOL to SEC.  */
1576 void
1577 assemble_addr_to_section (rtx symbol, section *sec)
1578 {
1579   switch_to_section (sec);
1580   assemble_align (POINTER_SIZE);
1581   assemble_integer (symbol, POINTER_SIZE_UNITS, POINTER_SIZE, 1);
1582 }
1583
1584 /* Return the numbered .ctors.N (if CONSTRUCTOR_P) or .dtors.N (if
1585    not) section for PRIORITY.  */
1586 section *
1587 get_cdtor_priority_section (int priority, bool constructor_p)
1588 {
1589   /* Buffer conservatively large enough for the full range of a 32-bit
1590      int plus the text below.  */
1591   char buf[18];
1592
1593   /* ??? This only works reliably with the GNU linker.  */
1594   sprintf (buf, "%s.%.5u",
1595            constructor_p ? ".ctors" : ".dtors",
1596            /* Invert the numbering so the linker puts us in the proper
1597               order; constructors are run from right to left, and the
1598               linker sorts in increasing order.  */
1599            MAX_INIT_PRIORITY - priority);
1600   return get_section (buf, SECTION_WRITE, NULL);
1601 }
1602
1603 void
1604 default_named_section_asm_out_destructor (rtx symbol, int priority)
1605 {
1606   section *sec;
1607
1608   if (priority != DEFAULT_INIT_PRIORITY)
1609     sec = get_cdtor_priority_section (priority,
1610                                       /*constructor_p=*/false);
1611   else
1612     sec = get_section (".dtors", SECTION_WRITE, NULL);
1613
1614   assemble_addr_to_section (symbol, sec);
1615 }
1616
1617 #ifdef DTORS_SECTION_ASM_OP
1618 void
1619 default_dtor_section_asm_out_destructor (rtx symbol,
1620                                          int priority ATTRIBUTE_UNUSED)
1621 {
1622   assemble_addr_to_section (symbol, dtors_section);
1623 }
1624 #endif
1625
1626 void
1627 default_named_section_asm_out_constructor (rtx symbol, int priority)
1628 {
1629   section *sec;
1630
1631   if (priority != DEFAULT_INIT_PRIORITY)
1632     sec = get_cdtor_priority_section (priority,
1633                                       /*constructor_p=*/true);
1634   else
1635     sec = get_section (".ctors", SECTION_WRITE, NULL);
1636
1637   assemble_addr_to_section (symbol, sec);
1638 }
1639
1640 #ifdef CTORS_SECTION_ASM_OP
1641 void
1642 default_ctor_section_asm_out_constructor (rtx symbol,
1643                                           int priority ATTRIBUTE_UNUSED)
1644 {
1645   assemble_addr_to_section (symbol, ctors_section);
1646 }
1647 #endif
1648 \f
1649 /* CONSTANT_POOL_BEFORE_FUNCTION may be defined as an expression with
1650    a nonzero value if the constant pool should be output before the
1651    start of the function, or a zero value if the pool should output
1652    after the end of the function.  The default is to put it before the
1653    start.  */
1654
1655 #ifndef CONSTANT_POOL_BEFORE_FUNCTION
1656 #define CONSTANT_POOL_BEFORE_FUNCTION 1
1657 #endif
1658
1659 /* DECL is an object (either VAR_DECL or FUNCTION_DECL) which is going
1660    to be output to assembler.
1661    Set first_global_object_name and weak_global_object_name as appropriate.  */
1662
1663 void
1664 notice_global_symbol (tree decl)
1665 {
1666   const char **t = &first_global_object_name;
1667
1668   if (first_global_object_name
1669       || !TREE_PUBLIC (decl)
1670       || DECL_EXTERNAL (decl)
1671       || !DECL_NAME (decl)
1672       || (VAR_P (decl) && DECL_HARD_REGISTER (decl))
1673       || (TREE_CODE (decl) != FUNCTION_DECL
1674           && (!VAR_P (decl)
1675               || (DECL_COMMON (decl)
1676                   && (DECL_INITIAL (decl) == 0
1677                       || DECL_INITIAL (decl) == error_mark_node)))))
1678     return;
1679
1680   /* We win when global object is found, but it is useful to know about weak
1681      symbol as well so we can produce nicer unique names.  */
1682   if (DECL_WEAK (decl) || DECL_ONE_ONLY (decl) || flag_shlib)
1683     t = &weak_global_object_name;
1684
1685   if (!*t)
1686     {
1687       tree id = DECL_ASSEMBLER_NAME (decl);
1688       ultimate_transparent_alias_target (&id);
1689       *t = ggc_strdup (targetm.strip_name_encoding (IDENTIFIER_POINTER (id)));
1690     }
1691 }
1692
1693 /* If not using flag_reorder_blocks_and_partition, decide early whether the
1694    current function goes into the cold section, so that targets can use
1695    current_function_section during RTL expansion.  DECL describes the
1696    function.  */
1697
1698 void
1699 decide_function_section (tree decl)
1700 {
1701   first_function_block_is_cold = false;
1702
1703  if (DECL_SECTION_NAME (decl))
1704     {
1705       struct cgraph_node *node = cgraph_node::get (current_function_decl);
1706       /* Calls to function_section rely on first_function_block_is_cold
1707          being accurate.  */
1708       first_function_block_is_cold = (node
1709                                       && node->frequency
1710                                       == NODE_FREQUENCY_UNLIKELY_EXECUTED);
1711     }
1712
1713   in_cold_section_p = first_function_block_is_cold;
1714 }
1715
1716 /* Get the function's name, as described by its RTL.  This may be
1717    different from the DECL_NAME name used in the source file.  */
1718 const char *
1719 get_fnname_from_decl (tree decl)
1720 {
1721   rtx x = DECL_RTL (decl);
1722   gcc_assert (MEM_P (x));
1723   x = XEXP (x, 0);
1724   gcc_assert (GET_CODE (x) == SYMBOL_REF);
1725   return XSTR (x, 0);
1726 }
1727
1728 /* Output assembler code for the constant pool of a function and associated
1729    with defining the name of the function.  DECL describes the function.
1730    NAME is the function's name.  For the constant pool, we use the current
1731    constant pool data.  */
1732
1733 void
1734 assemble_start_function (tree decl, const char *fnname)
1735 {
1736   int align;
1737   char tmp_label[100];
1738   bool hot_label_written = false;
1739
1740   if (crtl->has_bb_partition)
1741     {
1742       ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTB", const_labelno);
1743       crtl->subsections.hot_section_label = ggc_strdup (tmp_label);
1744       ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDB", const_labelno);
1745       crtl->subsections.cold_section_label = ggc_strdup (tmp_label);
1746       ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LHOTE", const_labelno);
1747       crtl->subsections.hot_section_end_label = ggc_strdup (tmp_label);
1748       ASM_GENERATE_INTERNAL_LABEL (tmp_label, "LCOLDE", const_labelno);
1749       crtl->subsections.cold_section_end_label = ggc_strdup (tmp_label);
1750       const_labelno++;
1751       cold_function_name = NULL_TREE;
1752     }
1753   else
1754     {
1755       crtl->subsections.hot_section_label = NULL;
1756       crtl->subsections.cold_section_label = NULL;
1757       crtl->subsections.hot_section_end_label = NULL;
1758       crtl->subsections.cold_section_end_label = NULL;
1759     }
1760
1761   /* The following code does not need preprocessing in the assembler.  */
1762
1763   app_disable ();
1764
1765   if (CONSTANT_POOL_BEFORE_FUNCTION)
1766     output_constant_pool (fnname, decl);
1767
1768   align = symtab_node::get (decl)->definition_alignment ();
1769
1770   /* Make sure the not and cold text (code) sections are properly
1771      aligned.  This is necessary here in the case where the function
1772      has both hot and cold sections, because we don't want to re-set
1773      the alignment when the section switch happens mid-function.  */
1774
1775   if (crtl->has_bb_partition)
1776     {
1777       first_function_block_is_cold = false;
1778
1779       switch_to_section (unlikely_text_section ());
1780       assemble_align (align);
1781       ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_label);
1782
1783       /* When the function starts with a cold section, we need to explicitly
1784          align the hot section and write out the hot section label.
1785          But if the current function is a thunk, we do not have a CFG.  */
1786       if (!cfun->is_thunk
1787           && BB_PARTITION (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb) == BB_COLD_PARTITION)
1788         {
1789           switch_to_section (text_section);
1790           assemble_align (align);
1791           ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1792           hot_label_written = true;
1793           first_function_block_is_cold = true;
1794         }
1795       in_cold_section_p = first_function_block_is_cold;
1796     }
1797
1798
1799   /* Switch to the correct text section for the start of the function.  */
1800
1801   switch_to_section (function_section (decl));
1802   if (crtl->has_bb_partition && !hot_label_written)
1803     ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_label);
1804
1805   /* Tell assembler to move to target machine's alignment for functions.  */
1806   align = floor_log2 (align / BITS_PER_UNIT);
1807   if (align > 0)
1808     {
1809       ASM_OUTPUT_ALIGN (asm_out_file, align);
1810     }
1811
1812   /* Handle a user-specified function alignment.
1813      Note that we still need to align to DECL_ALIGN, as above,
1814      because ASM_OUTPUT_MAX_SKIP_ALIGN might not do any alignment at all.  */
1815   if (! DECL_USER_ALIGN (decl)
1816       && align_functions.levels[0].log > align
1817       && optimize_function_for_speed_p (cfun))
1818     {
1819 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1820       int align_log = align_functions.levels[0].log;
1821 #endif
1822       int max_skip = align_functions.levels[0].maxskip;
1823       if (flag_limit_function_alignment && crtl->max_insn_address > 0
1824           && max_skip >= crtl->max_insn_address)
1825         max_skip = crtl->max_insn_address - 1;
1826
1827 #ifdef ASM_OUTPUT_MAX_SKIP_ALIGN
1828       ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file, align_log, max_skip);
1829       if (max_skip == align_functions.levels[0].maxskip)
1830         ASM_OUTPUT_MAX_SKIP_ALIGN (asm_out_file,
1831                                    align_functions.levels[1].log,
1832                                    align_functions.levels[1].maxskip);
1833 #else
1834       ASM_OUTPUT_ALIGN (asm_out_file, align_functions.levels[0].log);
1835 #endif
1836     }
1837
1838 #ifdef ASM_OUTPUT_FUNCTION_PREFIX
1839   ASM_OUTPUT_FUNCTION_PREFIX (asm_out_file, fnname);
1840 #endif
1841
1842   if (!DECL_IGNORED_P (decl))
1843     (*debug_hooks->begin_function) (decl);
1844
1845   /* Make function name accessible from other files, if appropriate.  */
1846
1847   if (TREE_PUBLIC (decl))
1848     {
1849       notice_global_symbol (decl);
1850
1851       globalize_decl (decl);
1852
1853       maybe_assemble_visibility (decl);
1854     }
1855
1856   if (DECL_PRESERVE_P (decl))
1857     targetm.asm_out.mark_decl_preserved (fnname);
1858
1859   unsigned HOST_WIDE_INT patch_area_size = function_entry_patch_area_size;
1860   unsigned HOST_WIDE_INT patch_area_entry = function_entry_patch_area_start;
1861
1862   tree patchable_function_entry_attr
1863     = lookup_attribute ("patchable_function_entry", DECL_ATTRIBUTES (decl));
1864   if (patchable_function_entry_attr)
1865     {
1866       tree pp_val = TREE_VALUE (patchable_function_entry_attr);
1867       tree patchable_function_entry_value1 = TREE_VALUE (pp_val);
1868
1869       patch_area_size = tree_to_uhwi (patchable_function_entry_value1);
1870       patch_area_entry = 0;
1871       if (TREE_CHAIN (pp_val) != NULL_TREE)
1872         {
1873           tree patchable_function_entry_value2
1874             = TREE_VALUE (TREE_CHAIN (pp_val));
1875           patch_area_entry = tree_to_uhwi (patchable_function_entry_value2);
1876         }
1877     }
1878
1879   if (patch_area_entry > patch_area_size)
1880     {
1881       if (patch_area_size > 0)
1882         warning (OPT_Wattributes,
1883                  "patchable function entry %wu exceeds size %wu",
1884                  patch_area_entry, patch_area_size);
1885       patch_area_entry = 0;
1886     }
1887
1888   /* Emit the patching area before the entry label, if any.  */
1889   if (patch_area_entry > 0)
1890     targetm.asm_out.print_patchable_function_entry (asm_out_file,
1891                                                     patch_area_entry, true);
1892
1893   /* Do any machine/system dependent processing of the function name.  */
1894 #ifdef ASM_DECLARE_FUNCTION_NAME
1895   ASM_DECLARE_FUNCTION_NAME (asm_out_file, fnname, current_function_decl);
1896 #else
1897   /* Standard thing is just output label for the function.  */
1898   ASM_OUTPUT_FUNCTION_LABEL (asm_out_file, fnname, current_function_decl);
1899 #endif /* ASM_DECLARE_FUNCTION_NAME */
1900
1901   /* And the area after the label.  Record it if we haven't done so yet.  */
1902   if (patch_area_size > patch_area_entry)
1903     targetm.asm_out.print_patchable_function_entry (asm_out_file,
1904                                                     patch_area_size
1905                                                     - patch_area_entry,
1906                                                     patch_area_entry == 0);
1907
1908   if (lookup_attribute ("no_split_stack", DECL_ATTRIBUTES (decl)))
1909     saw_no_split_stack = true;
1910 }
1911
1912 /* Output assembler code associated with defining the size of the
1913    function.  DECL describes the function.  NAME is the function's name.  */
1914
1915 void
1916 assemble_end_function (tree decl, const char *fnname ATTRIBUTE_UNUSED)
1917 {
1918 #ifdef ASM_DECLARE_FUNCTION_SIZE
1919   /* We could have switched section in the middle of the function.  */
1920   if (crtl->has_bb_partition)
1921     switch_to_section (function_section (decl));
1922   ASM_DECLARE_FUNCTION_SIZE (asm_out_file, fnname, decl);
1923 #endif
1924   if (! CONSTANT_POOL_BEFORE_FUNCTION)
1925     {
1926       output_constant_pool (fnname, decl);
1927       switch_to_section (function_section (decl)); /* need to switch back */
1928     }
1929   /* Output labels for end of hot/cold text sections (to be used by
1930      debug info.)  */
1931   if (crtl->has_bb_partition)
1932     {
1933       section *save_text_section;
1934
1935       save_text_section = in_section;
1936       switch_to_section (unlikely_text_section ());
1937 #ifdef ASM_DECLARE_COLD_FUNCTION_SIZE
1938       if (cold_function_name != NULL_TREE)
1939         ASM_DECLARE_COLD_FUNCTION_SIZE (asm_out_file,
1940                                         IDENTIFIER_POINTER (cold_function_name),
1941                                         decl);
1942 #endif
1943       ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.cold_section_end_label);
1944       if (first_function_block_is_cold)
1945         switch_to_section (text_section);
1946       else
1947         switch_to_section (function_section (decl));
1948       ASM_OUTPUT_LABEL (asm_out_file, crtl->subsections.hot_section_end_label);
1949       switch_to_section (save_text_section);
1950     }
1951 }
1952 \f
1953 /* Assemble code to leave SIZE bytes of zeros.  */
1954
1955 void
1956 assemble_zeros (unsigned HOST_WIDE_INT size)
1957 {
1958   /* Do no output if -fsyntax-only.  */
1959   if (flag_syntax_only)
1960     return;
1961
1962 #ifdef ASM_NO_SKIP_IN_TEXT
1963   /* The `space' pseudo in the text section outputs nop insns rather than 0s,
1964      so we must output 0s explicitly in the text section.  */
1965   if (ASM_NO_SKIP_IN_TEXT && (in_section->common.flags & SECTION_CODE) != 0)
1966     {
1967       unsigned HOST_WIDE_INT i;
1968       for (i = 0; i < size; i++)
1969         assemble_integer (const0_rtx, 1, BITS_PER_UNIT, 1);
1970     }
1971   else
1972 #endif
1973     if (size > 0)
1974       ASM_OUTPUT_SKIP (asm_out_file, size);
1975 }
1976
1977 /* Assemble an alignment pseudo op for an ALIGN-bit boundary.  */
1978
1979 void
1980 assemble_align (unsigned int align)
1981 {
1982   if (align > BITS_PER_UNIT)
1983     {
1984       ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
1985     }
1986 }
1987
1988 /* Assemble a string constant with the specified C string as contents.  */
1989
1990 void
1991 assemble_string (const char *p, int size)
1992 {
1993   int pos = 0;
1994   int maximum = 2000;
1995
1996   /* If the string is very long, split it up.  */
1997
1998   while (pos < size)
1999     {
2000       int thissize = size - pos;
2001       if (thissize > maximum)
2002         thissize = maximum;
2003
2004       ASM_OUTPUT_ASCII (asm_out_file, p, thissize);
2005
2006       pos += thissize;
2007       p += thissize;
2008     }
2009 }
2010
2011 \f
2012 /* A noswitch_section_callback for lcomm_section.  */
2013
2014 static bool
2015 emit_local (tree decl ATTRIBUTE_UNUSED,
2016             const char *name ATTRIBUTE_UNUSED,
2017             unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2018             unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2019 {
2020 #if defined ASM_OUTPUT_ALIGNED_DECL_LOCAL
2021   unsigned int align = symtab_node::get (decl)->definition_alignment ();
2022   ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, decl, name,
2023                                  size, align);
2024   return true;
2025 #elif defined ASM_OUTPUT_ALIGNED_LOCAL
2026   unsigned int align = symtab_node::get (decl)->definition_alignment ();
2027   ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, align);
2028   return true;
2029 #else
2030   ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2031   return false;
2032 #endif
2033 }
2034
2035 /* A noswitch_section_callback for bss_noswitch_section.  */
2036
2037 #if defined ASM_OUTPUT_ALIGNED_BSS
2038 static bool
2039 emit_bss (tree decl ATTRIBUTE_UNUSED,
2040           const char *name ATTRIBUTE_UNUSED,
2041           unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2042           unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2043 {
2044   ASM_OUTPUT_ALIGNED_BSS (asm_out_file, decl, name, size,
2045                           get_variable_align (decl));
2046   return true;
2047 }
2048 #endif
2049
2050 /* A noswitch_section_callback for comm_section.  */
2051
2052 static bool
2053 emit_common (tree decl ATTRIBUTE_UNUSED,
2054              const char *name ATTRIBUTE_UNUSED,
2055              unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2056              unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2057 {
2058 #if defined ASM_OUTPUT_ALIGNED_DECL_COMMON
2059   ASM_OUTPUT_ALIGNED_DECL_COMMON (asm_out_file, decl, name,
2060                                   size, get_variable_align (decl));
2061   return true;
2062 #elif defined ASM_OUTPUT_ALIGNED_COMMON
2063   ASM_OUTPUT_ALIGNED_COMMON (asm_out_file, name, size,
2064                              get_variable_align (decl));
2065   return true;
2066 #else
2067   ASM_OUTPUT_COMMON (asm_out_file, name, size, rounded);
2068   return false;
2069 #endif
2070 }
2071
2072 /* A noswitch_section_callback for tls_comm_section.  */
2073
2074 static bool
2075 emit_tls_common (tree decl ATTRIBUTE_UNUSED,
2076                  const char *name ATTRIBUTE_UNUSED,
2077                  unsigned HOST_WIDE_INT size ATTRIBUTE_UNUSED,
2078                  unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED)
2079 {
2080 #ifdef ASM_OUTPUT_TLS_COMMON
2081   ASM_OUTPUT_TLS_COMMON (asm_out_file, decl, name, size);
2082   return true;
2083 #else
2084   sorry ("thread-local COMMON data not implemented");
2085   return true;
2086 #endif
2087 }
2088
2089 /* Assemble DECL given that it belongs in SECTION_NOSWITCH section SECT.
2090    NAME is the name of DECL's SYMBOL_REF.  */
2091
2092 static void
2093 assemble_noswitch_variable (tree decl, const char *name, section *sect,
2094                             unsigned int align)
2095 {
2096   unsigned HOST_WIDE_INT size, rounded;
2097
2098   size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2099   rounded = size;
2100
2101   if ((flag_sanitize & SANITIZE_ADDRESS) && asan_protect_global (decl))
2102     size += asan_red_zone_size (size);
2103
2104   /* Don't allocate zero bytes of common,
2105      since that means "undefined external" in the linker.  */
2106   if (size == 0)
2107     rounded = 1;
2108
2109   /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2110      so that each uninitialized object starts on such a boundary.  */
2111   rounded += (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1;
2112   rounded = (rounded / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2113              * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2114
2115   if (!sect->noswitch.callback (decl, name, size, rounded)
2116       && (unsigned HOST_WIDE_INT) (align / BITS_PER_UNIT) > rounded)
2117     error ("requested alignment for %q+D is greater than "
2118            "implemented alignment of %wu", decl, rounded);
2119 }
2120
2121 /* A subroutine of assemble_variable.  Output the label and contents of
2122    DECL, whose address is a SYMBOL_REF with name NAME.  DONT_OUTPUT_DATA
2123    is as for assemble_variable.  */
2124
2125 static void
2126 assemble_variable_contents (tree decl, const char *name,
2127                             bool dont_output_data, bool merge_strings)
2128 {
2129   /* Do any machine/system dependent processing of the object.  */
2130 #ifdef ASM_DECLARE_OBJECT_NAME
2131   last_assemble_variable_decl = decl;
2132   ASM_DECLARE_OBJECT_NAME (asm_out_file, name, decl);
2133 #else
2134   /* Standard thing is just output label for the object.  */
2135   ASM_OUTPUT_LABEL (asm_out_file, name);
2136 #endif /* ASM_DECLARE_OBJECT_NAME */
2137
2138   if (!dont_output_data)
2139     {
2140       /* Caller is supposed to use varpool_get_constructor when it wants
2141          to output the body.  */
2142       gcc_assert (!in_lto_p || DECL_INITIAL (decl) != error_mark_node);
2143       if (DECL_INITIAL (decl)
2144           && DECL_INITIAL (decl) != error_mark_node
2145           && !initializer_zerop (DECL_INITIAL (decl)))
2146         /* Output the actual data.  */
2147         output_constant (DECL_INITIAL (decl),
2148                          tree_to_uhwi (DECL_SIZE_UNIT (decl)),
2149                          get_variable_align (decl),
2150                          false, merge_strings);
2151       else
2152         /* Leave space for it.  */
2153         assemble_zeros (tree_to_uhwi (DECL_SIZE_UNIT (decl)));
2154       targetm.asm_out.decl_end ();
2155     }
2156 }
2157
2158 /* Write out assembly for the variable DECL, which is not defined in
2159    the current translation unit.  */
2160 void
2161 assemble_undefined_decl (tree decl)
2162 {
2163   const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
2164   targetm.asm_out.assemble_undefined_decl (asm_out_file, name, decl);
2165 }
2166
2167 /* Assemble everything that is needed for a variable or function declaration.
2168    Not used for automatic variables, and not used for function definitions.
2169    Should not be called for variables of incomplete structure type.
2170
2171    TOP_LEVEL is nonzero if this variable has file scope.
2172    AT_END is nonzero if this is the special handling, at end of compilation,
2173    to define things that have had only tentative definitions.
2174    DONT_OUTPUT_DATA if nonzero means don't actually output the
2175    initial value (that will be done by the caller).  */
2176
2177 void
2178 assemble_variable (tree decl, int top_level ATTRIBUTE_UNUSED,
2179                    int at_end ATTRIBUTE_UNUSED, int dont_output_data)
2180 {
2181   const char *name;
2182   rtx decl_rtl, symbol;
2183   section *sect;
2184   unsigned int align;
2185   bool asan_protected = false;
2186
2187   /* This function is supposed to handle VARIABLES.  Ensure we have one.  */
2188   gcc_assert (VAR_P (decl));
2189
2190   /* Emulated TLS had better not get this far.  */
2191   gcc_checking_assert (targetm.have_tls || !DECL_THREAD_LOCAL_P (decl));
2192
2193   last_assemble_variable_decl = 0;
2194
2195   /* Normally no need to say anything here for external references,
2196      since assemble_external is called by the language-specific code
2197      when a declaration is first seen.  */
2198
2199   if (DECL_EXTERNAL (decl))
2200     return;
2201
2202   /* Do nothing for global register variables.  */
2203   if (DECL_RTL_SET_P (decl) && REG_P (DECL_RTL (decl)))
2204     {
2205       TREE_ASM_WRITTEN (decl) = 1;
2206       return;
2207     }
2208
2209   /* If type was incomplete when the variable was declared,
2210      see if it is complete now.  */
2211
2212   if (DECL_SIZE (decl) == 0)
2213     layout_decl (decl, 0);
2214
2215   /* Still incomplete => don't allocate it; treat the tentative defn
2216      (which is what it must have been) as an `extern' reference.  */
2217
2218   if (!dont_output_data && DECL_SIZE (decl) == 0)
2219     {
2220       error ("storage size of %q+D isn%'t known", decl);
2221       TREE_ASM_WRITTEN (decl) = 1;
2222       return;
2223     }
2224
2225   /* The first declaration of a variable that comes through this function
2226      decides whether it is global (in C, has external linkage)
2227      or local (in C, has internal linkage).  So do nothing more
2228      if this function has already run.  */
2229
2230   if (TREE_ASM_WRITTEN (decl))
2231     return;
2232
2233   /* Make sure targetm.encode_section_info is invoked before we set
2234      ASM_WRITTEN.  */
2235   decl_rtl = DECL_RTL (decl);
2236
2237   TREE_ASM_WRITTEN (decl) = 1;
2238
2239   /* Do no output if -fsyntax-only.  */
2240   if (flag_syntax_only)
2241     return;
2242
2243   if (! dont_output_data
2244       && ! valid_constant_size_p (DECL_SIZE_UNIT (decl)))
2245     {
2246       error ("size of variable %q+D is too large", decl);
2247       return;
2248     }
2249
2250   gcc_assert (MEM_P (decl_rtl));
2251   gcc_assert (GET_CODE (XEXP (decl_rtl, 0)) == SYMBOL_REF);
2252   symbol = XEXP (decl_rtl, 0);
2253
2254   /* If this symbol belongs to the tree constant pool, output the constant
2255      if it hasn't already been written.  */
2256   if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
2257     {
2258       tree decl = SYMBOL_REF_DECL (symbol);
2259       if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
2260         output_constant_def_contents (symbol);
2261       return;
2262     }
2263
2264   app_disable ();
2265
2266   name = XSTR (symbol, 0);
2267   if (TREE_PUBLIC (decl) && DECL_NAME (decl))
2268     notice_global_symbol (decl);
2269
2270   /* Compute the alignment of this data.  */
2271
2272   align_variable (decl, dont_output_data);
2273
2274   if ((flag_sanitize & SANITIZE_ADDRESS)
2275       && asan_protect_global (decl))
2276     {
2277       asan_protected = true;
2278       SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
2279                                  ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
2280     }
2281
2282   set_mem_align (decl_rtl, DECL_ALIGN (decl));
2283
2284   align = get_variable_align (decl);
2285
2286   if (TREE_PUBLIC (decl))
2287     maybe_assemble_visibility (decl);
2288
2289   if (DECL_PRESERVE_P (decl))
2290     targetm.asm_out.mark_decl_preserved (name);
2291
2292   /* First make the assembler name(s) global if appropriate.  */
2293   sect = get_variable_section (decl, false);
2294   if (TREE_PUBLIC (decl)
2295       && (sect->common.flags & SECTION_COMMON) == 0)
2296     globalize_decl (decl);
2297
2298   /* Output any data that we will need to use the address of.  */
2299   if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node)
2300     output_addressed_constants (DECL_INITIAL (decl));
2301
2302   /* dbxout.c needs to know this.  */
2303   if (sect && (sect->common.flags & SECTION_CODE) != 0)
2304     DECL_IN_TEXT_SECTION (decl) = 1;
2305
2306   /* If the decl is part of an object_block, make sure that the decl
2307      has been positioned within its block, but do not write out its
2308      definition yet.  output_object_blocks will do that later.  */
2309   if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
2310     {
2311       gcc_assert (!dont_output_data);
2312       place_block_symbol (symbol);
2313     }
2314   else if (SECTION_STYLE (sect) == SECTION_NOSWITCH)
2315     assemble_noswitch_variable (decl, name, sect, align);
2316   else
2317     {
2318       /* Special-case handling of vtv comdat sections.  */
2319       if (sect->named.name
2320           && (strcmp (sect->named.name, ".vtable_map_vars") == 0))
2321         handle_vtv_comdat_section (sect, decl);
2322       else
2323         switch_to_section (sect);
2324       if (align > BITS_PER_UNIT)
2325         ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
2326       assemble_variable_contents (decl, name, dont_output_data,
2327                                   (sect->common.flags & SECTION_MERGE)
2328                                   && (sect->common.flags & SECTION_STRINGS));
2329       if (asan_protected)
2330         {
2331           unsigned HOST_WIDE_INT int size
2332             = tree_to_uhwi (DECL_SIZE_UNIT (decl));
2333           assemble_zeros (asan_red_zone_size (size));
2334         }
2335     }
2336 }
2337
2338
2339 /* Given a function declaration (FN_DECL), this function assembles the
2340    function into the .preinit_array section.  */
2341
2342 void
2343 assemble_vtv_preinit_initializer (tree fn_decl)
2344 {
2345   section *sect;
2346   unsigned flags = SECTION_WRITE;
2347   rtx symbol = XEXP (DECL_RTL (fn_decl), 0);
2348
2349   flags |= SECTION_NOTYPE;
2350   sect = get_section (".preinit_array", flags, fn_decl);
2351   switch_to_section (sect);
2352   assemble_addr_to_section (symbol, sect);
2353 }
2354
2355 /* Return 1 if type TYPE contains any pointers.  */
2356
2357 static int
2358 contains_pointers_p (tree type)
2359 {
2360   switch (TREE_CODE (type))
2361     {
2362     case POINTER_TYPE:
2363     case REFERENCE_TYPE:
2364       /* I'm not sure whether OFFSET_TYPE needs this treatment,
2365          so I'll play safe and return 1.  */
2366     case OFFSET_TYPE:
2367       return 1;
2368
2369     case RECORD_TYPE:
2370     case UNION_TYPE:
2371     case QUAL_UNION_TYPE:
2372       {
2373         tree fields;
2374         /* For a type that has fields, see if the fields have pointers.  */
2375         for (fields = TYPE_FIELDS (type); fields; fields = DECL_CHAIN (fields))
2376           if (TREE_CODE (fields) == FIELD_DECL
2377               && contains_pointers_p (TREE_TYPE (fields)))
2378             return 1;
2379         return 0;
2380       }
2381
2382     case ARRAY_TYPE:
2383       /* An array type contains pointers if its element type does.  */
2384       return contains_pointers_p (TREE_TYPE (type));
2385
2386     default:
2387       return 0;
2388     }
2389 }
2390
2391 /* We delay assemble_external processing until
2392    the compilation unit is finalized.  This is the best we can do for
2393    right now (i.e. stage 3 of GCC 4.0) - the right thing is to delay
2394    it all the way to final.  See PR 17982 for further discussion.  */
2395 static GTY(()) tree pending_assemble_externals;
2396
2397 #ifdef ASM_OUTPUT_EXTERNAL
2398 /* Some targets delay some output to final using TARGET_ASM_FILE_END.
2399    As a result, assemble_external can be called after the list of externals
2400    is processed and the pointer set destroyed.  */
2401 static bool pending_assemble_externals_processed;
2402
2403 /* Avoid O(external_decls**2) lookups in the pending_assemble_externals
2404    TREE_LIST in assemble_external.  */
2405 static hash_set<tree> *pending_assemble_externals_set;
2406
2407 /* True if DECL is a function decl for which no out-of-line copy exists.
2408    It is assumed that DECL's assembler name has been set.  */
2409
2410 static bool
2411 incorporeal_function_p (tree decl)
2412 {
2413   if (TREE_CODE (decl) == FUNCTION_DECL && fndecl_built_in_p (decl))
2414     {
2415       const char *name;
2416
2417       if (DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL
2418           && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (decl)))
2419         return true;
2420
2421       name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2422       /* Atomic or sync builtins which have survived this far will be
2423          resolved externally and therefore are not incorporeal.  */
2424       if (strncmp (name, "__builtin_", 10) == 0)
2425         return true;
2426     }
2427   return false;
2428 }
2429
2430 /* Actually do the tests to determine if this is necessary, and invoke
2431    ASM_OUTPUT_EXTERNAL.  */
2432 static void
2433 assemble_external_real (tree decl)
2434 {
2435   rtx rtl = DECL_RTL (decl);
2436
2437   if (MEM_P (rtl) && GET_CODE (XEXP (rtl, 0)) == SYMBOL_REF
2438       && !SYMBOL_REF_USED (XEXP (rtl, 0))
2439       && !incorporeal_function_p (decl))
2440     {
2441       /* Some systems do require some output.  */
2442       SYMBOL_REF_USED (XEXP (rtl, 0)) = 1;
2443       ASM_OUTPUT_EXTERNAL (asm_out_file, decl, XSTR (XEXP (rtl, 0), 0));
2444     }
2445 }
2446 #endif
2447
2448 void
2449 process_pending_assemble_externals (void)
2450 {
2451 #ifdef ASM_OUTPUT_EXTERNAL
2452   tree list;
2453   for (list = pending_assemble_externals; list; list = TREE_CHAIN (list))
2454     assemble_external_real (TREE_VALUE (list));
2455
2456   pending_assemble_externals = 0;
2457   pending_assemble_externals_processed = true;
2458   delete pending_assemble_externals_set;
2459 #endif
2460 }
2461
2462 /* This TREE_LIST contains any weak symbol declarations waiting
2463    to be emitted.  */
2464 static GTY(()) tree weak_decls;
2465
2466 /* Output something to declare an external symbol to the assembler,
2467    and qualifiers such as weakness.  (Most assemblers don't need
2468    extern declaration, so we normally output nothing.)  Do nothing if
2469    DECL is not external.  */
2470
2471 void
2472 assemble_external (tree decl ATTRIBUTE_UNUSED)
2473 {
2474   /*  Make sure that the ASM_OUT_FILE is open.
2475       If it's not, we should not be calling this function.  */
2476   gcc_assert (asm_out_file);
2477
2478   /* In a perfect world, the following condition would be true.
2479      Sadly, the Go front end emit assembly *from the front end*,
2480      bypassing the call graph.  See PR52739.  Fix before GCC 4.8.  */
2481 #if 0
2482   /* This function should only be called if we are expanding, or have
2483      expanded, to RTL.
2484      Ideally, only final.c would be calling this function, but it is
2485      not clear whether that would break things somehow.  See PR 17982
2486      for further discussion.  */
2487   gcc_assert (state == EXPANSION
2488               || state == FINISHED);
2489 #endif
2490
2491   if (!DECL_P (decl) || !DECL_EXTERNAL (decl) || !TREE_PUBLIC (decl))
2492     return;
2493
2494   /* We want to output annotation for weak and external symbols at
2495      very last to check if they are references or not.  */
2496
2497   if (TARGET_SUPPORTS_WEAK
2498       && DECL_WEAK (decl)
2499       /* TREE_STATIC is a weird and abused creature which is not
2500          generally the right test for whether an entity has been
2501          locally emitted, inlined or otherwise not-really-extern, but
2502          for declarations that can be weak, it happens to be
2503          match.  */
2504       && !TREE_STATIC (decl)
2505       && lookup_attribute ("weak", DECL_ATTRIBUTES (decl))
2506       && value_member (decl, weak_decls) == NULL_TREE)
2507     weak_decls = tree_cons (NULL, decl, weak_decls);
2508
2509 #ifdef ASM_OUTPUT_EXTERNAL
2510   if (pending_assemble_externals_processed)
2511     {
2512       assemble_external_real (decl);
2513       return;
2514     }
2515
2516   if (! pending_assemble_externals_set->add (decl))
2517     pending_assemble_externals = tree_cons (NULL, decl,
2518                                             pending_assemble_externals);
2519 #endif
2520 }
2521
2522 /* Similar, for calling a library function FUN.  */
2523
2524 void
2525 assemble_external_libcall (rtx fun)
2526 {
2527   /* Declare library function name external when first used, if nec.  */
2528   if (! SYMBOL_REF_USED (fun))
2529     {
2530       SYMBOL_REF_USED (fun) = 1;
2531       targetm.asm_out.external_libcall (fun);
2532     }
2533 }
2534
2535 /* Assemble a label named NAME.  */
2536
2537 void
2538 assemble_label (FILE *file, const char *name)
2539 {
2540   ASM_OUTPUT_LABEL (file, name);
2541 }
2542
2543 /* Set the symbol_referenced flag for ID.  */
2544 void
2545 mark_referenced (tree id)
2546 {
2547   TREE_SYMBOL_REFERENCED (id) = 1;
2548 }
2549
2550 /* Set the symbol_referenced flag for DECL and notify callgraph.  */
2551 void
2552 mark_decl_referenced (tree decl)
2553 {
2554   if (TREE_CODE (decl) == FUNCTION_DECL)
2555     {
2556       /* Extern inline functions don't become needed when referenced.
2557          If we know a method will be emitted in other TU and no new
2558          functions can be marked reachable, just use the external
2559          definition.  */
2560       struct cgraph_node *node = cgraph_node::get_create (decl);
2561       if (!DECL_EXTERNAL (decl)
2562           && !node->definition)
2563         node->mark_force_output ();
2564     }
2565   else if (VAR_P (decl))
2566     {
2567       varpool_node *node = varpool_node::get_create (decl);
2568       /* C++ frontend use mark_decl_references to force COMDAT variables
2569          to be output that might appear dead otherwise.  */
2570       node->force_output = true;
2571     }
2572   /* else do nothing - we can get various sorts of CST nodes here,
2573      which do not need to be marked.  */
2574 }
2575
2576
2577 /* Output to FILE (an assembly file) a reference to NAME.  If NAME
2578    starts with a *, the rest of NAME is output verbatim.  Otherwise
2579    NAME is transformed in a target-specific way (usually by the
2580    addition of an underscore).  */
2581
2582 void
2583 assemble_name_raw (FILE *file, const char *name)
2584 {
2585   if (name[0] == '*')
2586     fputs (&name[1], file);
2587   else
2588     ASM_OUTPUT_LABELREF (file, name);
2589 }
2590
2591 /* Like assemble_name_raw, but should be used when NAME might refer to
2592    an entity that is also represented as a tree (like a function or
2593    variable).  If NAME does refer to such an entity, that entity will
2594    be marked as referenced.  */
2595
2596 void
2597 assemble_name (FILE *file, const char *name)
2598 {
2599   const char *real_name;
2600   tree id;
2601
2602   real_name = targetm.strip_name_encoding (name);
2603
2604   id = maybe_get_identifier (real_name);
2605   if (id)
2606     {
2607       tree id_orig = id;
2608
2609       mark_referenced (id);
2610       ultimate_transparent_alias_target (&id);
2611       if (id != id_orig)
2612         name = IDENTIFIER_POINTER (id);
2613       gcc_assert (! TREE_CHAIN (id));
2614     }
2615
2616   assemble_name_raw (file, name);
2617 }
2618
2619 /* Allocate SIZE bytes writable static space with a gensym name
2620    and return an RTX to refer to its address.  */
2621
2622 rtx
2623 assemble_static_space (unsigned HOST_WIDE_INT size)
2624 {
2625   char name[17];
2626   const char *namestring;
2627   rtx x;
2628
2629   ASM_GENERATE_INTERNAL_LABEL (name, "LF", const_labelno);
2630   ++const_labelno;
2631   namestring = ggc_strdup (name);
2632
2633   x = gen_rtx_SYMBOL_REF (Pmode, namestring);
2634   SYMBOL_REF_FLAGS (x) = SYMBOL_FLAG_LOCAL;
2635
2636 #ifdef ASM_OUTPUT_ALIGNED_DECL_LOCAL
2637   ASM_OUTPUT_ALIGNED_DECL_LOCAL (asm_out_file, NULL_TREE, name, size,
2638                                  BIGGEST_ALIGNMENT);
2639 #else
2640 #ifdef ASM_OUTPUT_ALIGNED_LOCAL
2641   ASM_OUTPUT_ALIGNED_LOCAL (asm_out_file, name, size, BIGGEST_ALIGNMENT);
2642 #else
2643   {
2644     /* Round size up to multiple of BIGGEST_ALIGNMENT bits
2645        so that each uninitialized object starts on such a boundary.  */
2646     /* Variable `rounded' might or might not be used in ASM_OUTPUT_LOCAL.  */
2647     unsigned HOST_WIDE_INT rounded ATTRIBUTE_UNUSED
2648       = ((size + (BIGGEST_ALIGNMENT / BITS_PER_UNIT) - 1)
2649          / (BIGGEST_ALIGNMENT / BITS_PER_UNIT)
2650          * (BIGGEST_ALIGNMENT / BITS_PER_UNIT));
2651     ASM_OUTPUT_LOCAL (asm_out_file, name, size, rounded);
2652   }
2653 #endif
2654 #endif
2655   return x;
2656 }
2657
2658 /* Assemble the static constant template for function entry trampolines.
2659    This is done at most once per compilation.
2660    Returns an RTX for the address of the template.  */
2661
2662 static GTY(()) rtx initial_trampoline;
2663
2664 rtx
2665 assemble_trampoline_template (void)
2666 {
2667   char label[256];
2668   const char *name;
2669   int align;
2670   rtx symbol;
2671
2672   gcc_assert (targetm.asm_out.trampoline_template != NULL);
2673
2674   if (initial_trampoline)
2675     return initial_trampoline;
2676
2677   /* By default, put trampoline templates in read-only data section.  */
2678
2679 #ifdef TRAMPOLINE_SECTION
2680   switch_to_section (TRAMPOLINE_SECTION);
2681 #else
2682   switch_to_section (readonly_data_section);
2683 #endif
2684
2685   /* Write the assembler code to define one.  */
2686   align = floor_log2 (TRAMPOLINE_ALIGNMENT / BITS_PER_UNIT);
2687   if (align > 0)
2688     ASM_OUTPUT_ALIGN (asm_out_file, align);
2689
2690   targetm.asm_out.internal_label (asm_out_file, "LTRAMP", 0);
2691   targetm.asm_out.trampoline_template (asm_out_file);
2692
2693   /* Record the rtl to refer to it.  */
2694   ASM_GENERATE_INTERNAL_LABEL (label, "LTRAMP", 0);
2695   name = ggc_strdup (label);
2696   symbol = gen_rtx_SYMBOL_REF (Pmode, name);
2697   SYMBOL_REF_FLAGS (symbol) = SYMBOL_FLAG_LOCAL;
2698
2699   initial_trampoline = gen_const_mem (BLKmode, symbol);
2700   set_mem_align (initial_trampoline, TRAMPOLINE_ALIGNMENT);
2701   set_mem_size (initial_trampoline, TRAMPOLINE_SIZE);
2702
2703   return initial_trampoline;
2704 }
2705 \f
2706 /* A and B are either alignments or offsets.  Return the minimum alignment
2707    that may be assumed after adding the two together.  */
2708
2709 static inline unsigned
2710 min_align (unsigned int a, unsigned int b)
2711 {
2712   return least_bit_hwi (a | b);
2713 }
2714
2715 /* Return the assembler directive for creating a given kind of integer
2716    object.  SIZE is the number of bytes in the object and ALIGNED_P
2717    indicates whether it is known to be aligned.  Return NULL if the
2718    assembly dialect has no such directive.
2719
2720    The returned string should be printed at the start of a new line and
2721    be followed immediately by the object's initial value.  */
2722
2723 const char *
2724 integer_asm_op (int size, int aligned_p)
2725 {
2726   struct asm_int_op *ops;
2727
2728   if (aligned_p)
2729     ops = &targetm.asm_out.aligned_op;
2730   else
2731     ops = &targetm.asm_out.unaligned_op;
2732
2733   switch (size)
2734     {
2735     case 1:
2736       return targetm.asm_out.byte_op;
2737     case 2:
2738       return ops->hi;
2739     case 3:
2740       return ops->psi;
2741     case 4:
2742       return ops->si;
2743     case 5:
2744     case 6:
2745     case 7:
2746       return ops->pdi;
2747     case 8:
2748       return ops->di;
2749     case 9:
2750     case 10:
2751     case 11:
2752     case 12:
2753     case 13:
2754     case 14:
2755     case 15:
2756       return ops->pti;
2757     case 16:
2758       return ops->ti;
2759     default:
2760       return NULL;
2761     }
2762 }
2763
2764 /* Use directive OP to assemble an integer object X.  Print OP at the
2765    start of the line, followed immediately by the value of X.  */
2766
2767 void
2768 assemble_integer_with_op (const char *op, rtx x)
2769 {
2770   fputs (op, asm_out_file);
2771   output_addr_const (asm_out_file, x);
2772   fputc ('\n', asm_out_file);
2773 }
2774
2775 /* The default implementation of the asm_out.integer target hook.  */
2776
2777 bool
2778 default_assemble_integer (rtx x ATTRIBUTE_UNUSED,
2779                           unsigned int size ATTRIBUTE_UNUSED,
2780                           int aligned_p ATTRIBUTE_UNUSED)
2781 {
2782   const char *op = integer_asm_op (size, aligned_p);
2783   /* Avoid GAS bugs for large values.  Specifically negative values whose
2784      absolute value fits in a bfd_vma, but not in a bfd_signed_vma.  */
2785   if (size > UNITS_PER_WORD && size > POINTER_SIZE_UNITS)
2786     return false;
2787   return op && (assemble_integer_with_op (op, x), true);
2788 }
2789
2790 /* Assemble the integer constant X into an object of SIZE bytes.  ALIGN is
2791    the alignment of the integer in bits.  Return 1 if we were able to output
2792    the constant, otherwise 0.  We must be able to output the constant,
2793    if FORCE is nonzero.  */
2794
2795 bool
2796 assemble_integer (rtx x, unsigned int size, unsigned int align, int force)
2797 {
2798   int aligned_p;
2799
2800   aligned_p = (align >= MIN (size * BITS_PER_UNIT, BIGGEST_ALIGNMENT));
2801
2802   /* See if the target hook can handle this kind of object.  */
2803   if (targetm.asm_out.integer (x, size, aligned_p))
2804     return true;
2805
2806   /* If the object is a multi-byte one, try splitting it up.  Split
2807      it into words it if is multi-word, otherwise split it into bytes.  */
2808   if (size > 1)
2809     {
2810       machine_mode omode, imode;
2811       unsigned int subalign;
2812       unsigned int subsize, i;
2813       enum mode_class mclass;
2814
2815       subsize = size > UNITS_PER_WORD? UNITS_PER_WORD : 1;
2816       subalign = MIN (align, subsize * BITS_PER_UNIT);
2817       if (GET_CODE (x) == CONST_FIXED)
2818         mclass = GET_MODE_CLASS (GET_MODE (x));
2819       else
2820         mclass = MODE_INT;
2821
2822       omode = mode_for_size (subsize * BITS_PER_UNIT, mclass, 0).require ();
2823       imode = mode_for_size (size * BITS_PER_UNIT, mclass, 0).require ();
2824
2825       for (i = 0; i < size; i += subsize)
2826         {
2827           rtx partial = simplify_subreg (omode, x, imode, i);
2828           if (!partial || !assemble_integer (partial, subsize, subalign, 0))
2829             break;
2830         }
2831       if (i == size)
2832         return true;
2833
2834       /* If we've printed some of it, but not all of it, there's no going
2835          back now.  */
2836       gcc_assert (!i);
2837     }
2838
2839   gcc_assert (!force);
2840
2841   return false;
2842 }
2843 \f
2844 /* Assemble the floating-point constant D into an object of size MODE.  ALIGN
2845    is the alignment of the constant in bits.  If REVERSE is true, D is output
2846    in reverse storage order.  */
2847
2848 void
2849 assemble_real (REAL_VALUE_TYPE d, scalar_float_mode mode, unsigned int align,
2850                bool reverse)
2851 {
2852   long data[4] = {0, 0, 0, 0};
2853   int bitsize, nelts, nunits, units_per;
2854   rtx elt;
2855
2856   /* This is hairy.  We have a quantity of known size.  real_to_target
2857      will put it into an array of *host* longs, 32 bits per element
2858      (even if long is more than 32 bits).  We need to determine the
2859      number of array elements that are occupied (nelts) and the number
2860      of *target* min-addressable units that will be occupied in the
2861      object file (nunits).  We cannot assume that 32 divides the
2862      mode's bitsize (size * BITS_PER_UNIT) evenly.
2863
2864      size * BITS_PER_UNIT is used here to make sure that padding bits
2865      (which might appear at either end of the value; real_to_target
2866      will include the padding bits in its output array) are included.  */
2867
2868   nunits = GET_MODE_SIZE (mode);
2869   bitsize = nunits * BITS_PER_UNIT;
2870   nelts = CEIL (bitsize, 32);
2871   units_per = 32 / BITS_PER_UNIT;
2872
2873   real_to_target (data, &d, mode);
2874
2875   /* Put out the first word with the specified alignment.  */
2876   unsigned int chunk_nunits = MIN (nunits, units_per);
2877   if (reverse)
2878     elt = flip_storage_order (SImode, gen_int_mode (data[nelts - 1], SImode));
2879   else
2880     elt = GEN_INT (sext_hwi (data[0], chunk_nunits * BITS_PER_UNIT));
2881   assemble_integer (elt, chunk_nunits, align, 1);
2882   nunits -= chunk_nunits;
2883
2884   /* Subsequent words need only 32-bit alignment.  */
2885   align = min_align (align, 32);
2886
2887   for (int i = 1; i < nelts; i++)
2888     {
2889       chunk_nunits = MIN (nunits, units_per);
2890       if (reverse)
2891         elt = flip_storage_order (SImode,
2892                                   gen_int_mode (data[nelts - 1 - i], SImode));
2893       else
2894         elt = GEN_INT (sext_hwi (data[i], chunk_nunits * BITS_PER_UNIT));
2895       assemble_integer (elt, chunk_nunits, align, 1);
2896       nunits -= chunk_nunits;
2897     }
2898 }
2899 \f
2900 /* Given an expression EXP with a constant value,
2901    reduce it to the sum of an assembler symbol and an integer.
2902    Store them both in the structure *VALUE.
2903    EXP must be reducible.  */
2904
2905 class addr_const {
2906 public:
2907   rtx base;
2908   poly_int64 offset;
2909 };
2910
2911 static void
2912 decode_addr_const (tree exp, class addr_const *value)
2913 {
2914   tree target = TREE_OPERAND (exp, 0);
2915   poly_int64 offset = 0;
2916   rtx x;
2917
2918   while (1)
2919     {
2920       poly_int64 bytepos;
2921       if (TREE_CODE (target) == COMPONENT_REF
2922           && poly_int_tree_p (byte_position (TREE_OPERAND (target, 1)),
2923                               &bytepos))
2924         {
2925           offset += bytepos;
2926           target = TREE_OPERAND (target, 0);
2927         }
2928       else if (TREE_CODE (target) == ARRAY_REF
2929                || TREE_CODE (target) == ARRAY_RANGE_REF)
2930         {
2931           /* Truncate big offset.  */
2932           offset
2933             += (TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (target)))
2934                 * wi::to_poly_widest (TREE_OPERAND (target, 1)).force_shwi ());
2935           target = TREE_OPERAND (target, 0);
2936         }
2937       else if (TREE_CODE (target) == MEM_REF
2938                && TREE_CODE (TREE_OPERAND (target, 0)) == ADDR_EXPR)
2939         {
2940           offset += mem_ref_offset (target).force_shwi ();
2941           target = TREE_OPERAND (TREE_OPERAND (target, 0), 0);
2942         }
2943       else if (TREE_CODE (target) == INDIRECT_REF
2944                && TREE_CODE (TREE_OPERAND (target, 0)) == NOP_EXPR
2945                && TREE_CODE (TREE_OPERAND (TREE_OPERAND (target, 0), 0))
2946                   == ADDR_EXPR)
2947         target = TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (target, 0), 0), 0);
2948       else
2949         break;
2950     }
2951
2952   switch (TREE_CODE (target))
2953     {
2954     case VAR_DECL:
2955     case FUNCTION_DECL:
2956       x = DECL_RTL (target);
2957       break;
2958
2959     case LABEL_DECL:
2960       x = gen_rtx_MEM (FUNCTION_MODE,
2961                        gen_rtx_LABEL_REF (Pmode, force_label_rtx (target)));
2962       break;
2963
2964     case REAL_CST:
2965     case FIXED_CST:
2966     case STRING_CST:
2967     case COMPLEX_CST:
2968     case CONSTRUCTOR:
2969     case INTEGER_CST:
2970       x = lookup_constant_def (target);
2971       /* Should have been added by output_addressed_constants.  */
2972       gcc_assert (x);
2973       break;
2974
2975     case INDIRECT_REF:
2976       /* This deals with absolute addresses.  */
2977       offset += tree_to_shwi (TREE_OPERAND (target, 0));
2978       x = gen_rtx_MEM (QImode,
2979                        gen_rtx_SYMBOL_REF (Pmode, "origin of addresses"));
2980       break;
2981
2982     case COMPOUND_LITERAL_EXPR:
2983       gcc_assert (COMPOUND_LITERAL_EXPR_DECL (target));
2984       x = DECL_RTL (COMPOUND_LITERAL_EXPR_DECL (target));
2985       break;
2986
2987     default:
2988       gcc_unreachable ();
2989     }
2990
2991   gcc_assert (MEM_P (x));
2992   x = XEXP (x, 0);
2993
2994   value->base = x;
2995   value->offset = offset;
2996 }
2997 \f
2998 static GTY(()) hash_table<tree_descriptor_hasher> *const_desc_htab;
2999
3000 static void maybe_output_constant_def_contents (struct constant_descriptor_tree *, int);
3001
3002 /* Constant pool accessor function.  */
3003
3004 hash_table<tree_descriptor_hasher> *
3005 constant_pool_htab (void)
3006 {
3007   return const_desc_htab;
3008 }
3009
3010 /* Compute a hash code for a constant expression.  */
3011
3012 hashval_t
3013 tree_descriptor_hasher::hash (constant_descriptor_tree *ptr)
3014 {
3015   return ptr->hash;
3016 }
3017
3018 static hashval_t
3019 const_hash_1 (const tree exp)
3020 {
3021   const char *p;
3022   hashval_t hi;
3023   int len, i;
3024   enum tree_code code = TREE_CODE (exp);
3025
3026   /* Either set P and LEN to the address and len of something to hash and
3027      exit the switch or return a value.  */
3028
3029   switch (code)
3030     {
3031     case INTEGER_CST:
3032       p = (char *) &TREE_INT_CST_ELT (exp, 0);
3033       len = TREE_INT_CST_NUNITS (exp) * sizeof (HOST_WIDE_INT);
3034       break;
3035
3036     case REAL_CST:
3037       return real_hash (TREE_REAL_CST_PTR (exp));
3038
3039     case FIXED_CST:
3040       return fixed_hash (TREE_FIXED_CST_PTR (exp));
3041
3042     case STRING_CST:
3043       p = TREE_STRING_POINTER (exp);
3044       len = TREE_STRING_LENGTH (exp);
3045       break;
3046
3047     case COMPLEX_CST:
3048       return (const_hash_1 (TREE_REALPART (exp)) * 5
3049               + const_hash_1 (TREE_IMAGPART (exp)));
3050
3051     case VECTOR_CST:
3052       {
3053         hi = 7 + VECTOR_CST_NPATTERNS (exp);
3054         hi = hi * 563 + VECTOR_CST_NELTS_PER_PATTERN (exp);
3055         unsigned int count = vector_cst_encoded_nelts (exp);
3056         for (unsigned int i = 0; i < count; ++i)
3057           hi = hi * 563 + const_hash_1 (VECTOR_CST_ENCODED_ELT (exp, i));
3058         return hi;
3059       }
3060
3061     case CONSTRUCTOR:
3062       {
3063         unsigned HOST_WIDE_INT idx;
3064         tree value;
3065
3066         hi = 5 + int_size_in_bytes (TREE_TYPE (exp));
3067
3068         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
3069           if (value)
3070             hi = hi * 603 + const_hash_1 (value);
3071
3072         return hi;
3073       }
3074
3075     case ADDR_EXPR:
3076       if (CONSTANT_CLASS_P (TREE_OPERAND (exp, 0)))
3077        return const_hash_1 (TREE_OPERAND (exp, 0));
3078
3079       /* Fallthru.  */
3080     case FDESC_EXPR:
3081       {
3082         class addr_const value;
3083
3084         decode_addr_const (exp, &value);
3085         switch (GET_CODE (value.base))
3086           {
3087           case SYMBOL_REF:
3088             /* Don't hash the address of the SYMBOL_REF;
3089                only use the offset and the symbol name.  */
3090             hi = value.offset.coeffs[0];
3091             p = XSTR (value.base, 0);
3092             for (i = 0; p[i] != 0; i++)
3093               hi = ((hi * 613) + (unsigned) (p[i]));
3094             break;
3095
3096           case LABEL_REF:
3097             hi = (value.offset.coeffs[0]
3098                   + CODE_LABEL_NUMBER (label_ref_label (value.base)) * 13);
3099             break;
3100
3101           default:
3102             gcc_unreachable ();
3103           }
3104       }
3105       return hi;
3106
3107     case PLUS_EXPR:
3108     case POINTER_PLUS_EXPR:
3109     case MINUS_EXPR:
3110       return (const_hash_1 (TREE_OPERAND (exp, 0)) * 9
3111               + const_hash_1 (TREE_OPERAND (exp, 1)));
3112
3113     CASE_CONVERT:
3114       return const_hash_1 (TREE_OPERAND (exp, 0)) * 7 + 2;
3115
3116     default:
3117       /* A language specific constant. Just hash the code.  */
3118       return code;
3119     }
3120
3121   /* Compute hashing function.  */
3122   hi = len;
3123   for (i = 0; i < len; i++)
3124     hi = ((hi * 613) + (unsigned) (p[i]));
3125
3126   return hi;
3127 }
3128
3129 /* Wrapper of compare_constant, for the htab interface.  */
3130 bool
3131 tree_descriptor_hasher::equal (constant_descriptor_tree *c1,
3132                                constant_descriptor_tree *c2)
3133 {
3134   if (c1->hash != c2->hash)
3135     return 0;
3136   return compare_constant (c1->value, c2->value);
3137 }
3138
3139 /* Compare t1 and t2, and return 1 only if they are known to result in
3140    the same bit pattern on output.  */
3141
3142 static int
3143 compare_constant (const tree t1, const tree t2)
3144 {
3145   enum tree_code typecode;
3146
3147   if (t1 == NULL_TREE)
3148     return t2 == NULL_TREE;
3149   if (t2 == NULL_TREE)
3150     return 0;
3151
3152   if (TREE_CODE (t1) != TREE_CODE (t2))
3153     return 0;
3154
3155   switch (TREE_CODE (t1))
3156     {
3157     case INTEGER_CST:
3158       /* Integer constants are the same only if the same width of type.  */
3159       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3160         return 0;
3161       if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3162         return 0;
3163       return tree_int_cst_equal (t1, t2);
3164
3165     case REAL_CST:
3166       /* Real constants are the same only if the same width of type.  In
3167          addition to the same width, we need to check whether the modes are the
3168          same.  There might be two floating point modes that are the same size
3169          but have different representations, such as the PowerPC that has 2
3170          different 128-bit floating point types (IBM extended double and IEEE
3171          128-bit floating point).  */
3172       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3173         return 0;
3174       if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2)))
3175         return 0;
3176       return real_identical (&TREE_REAL_CST (t1), &TREE_REAL_CST (t2));
3177
3178     case FIXED_CST:
3179       /* Fixed constants are the same only if the same width of type.  */
3180       if (TYPE_PRECISION (TREE_TYPE (t1)) != TYPE_PRECISION (TREE_TYPE (t2)))
3181         return 0;
3182
3183       return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (t1), TREE_FIXED_CST (t2));
3184
3185     case STRING_CST:
3186       if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3187           || int_size_in_bytes (TREE_TYPE (t1))
3188              != int_size_in_bytes (TREE_TYPE (t2)))
3189         return 0;
3190
3191       return (TREE_STRING_LENGTH (t1) == TREE_STRING_LENGTH (t2)
3192               && ! memcmp (TREE_STRING_POINTER (t1), TREE_STRING_POINTER (t2),
3193                          TREE_STRING_LENGTH (t1)));
3194
3195     case COMPLEX_CST:
3196       return (compare_constant (TREE_REALPART (t1), TREE_REALPART (t2))
3197               && compare_constant (TREE_IMAGPART (t1), TREE_IMAGPART (t2)));
3198
3199     case VECTOR_CST:
3200       {
3201         if (VECTOR_CST_NPATTERNS (t1)
3202             != VECTOR_CST_NPATTERNS (t2))
3203           return 0;
3204
3205         if (VECTOR_CST_NELTS_PER_PATTERN (t1)
3206             != VECTOR_CST_NELTS_PER_PATTERN (t2))
3207           return 0;
3208
3209         unsigned int count = vector_cst_encoded_nelts (t1);
3210         for (unsigned int i = 0; i < count; ++i)
3211           if (!compare_constant (VECTOR_CST_ENCODED_ELT (t1, i),
3212                                  VECTOR_CST_ENCODED_ELT (t2, i)))
3213             return 0;
3214
3215         return 1;
3216       }
3217
3218     case CONSTRUCTOR:
3219       {
3220         vec<constructor_elt, va_gc> *v1, *v2;
3221         unsigned HOST_WIDE_INT idx;
3222
3223         typecode = TREE_CODE (TREE_TYPE (t1));
3224         if (typecode != TREE_CODE (TREE_TYPE (t2)))
3225           return 0;
3226
3227         if (typecode == ARRAY_TYPE)
3228           {
3229             HOST_WIDE_INT size_1 = int_size_in_bytes (TREE_TYPE (t1));
3230             /* For arrays, check that mode, size and storage order match.  */
3231             if (TYPE_MODE (TREE_TYPE (t1)) != TYPE_MODE (TREE_TYPE (t2))
3232                 || size_1 == -1
3233                 || size_1 != int_size_in_bytes (TREE_TYPE (t2))
3234                 || TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t1))
3235                    != TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (t2)))
3236               return 0;
3237           }
3238         else
3239           {
3240             /* For record and union constructors, require exact type
3241                equality.  */
3242             if (TREE_TYPE (t1) != TREE_TYPE (t2))
3243               return 0;
3244           }
3245
3246         v1 = CONSTRUCTOR_ELTS (t1);
3247         v2 = CONSTRUCTOR_ELTS (t2);
3248         if (vec_safe_length (v1) != vec_safe_length (v2))
3249           return 0;
3250
3251         for (idx = 0; idx < vec_safe_length (v1); ++idx)
3252           {
3253             constructor_elt *c1 = &(*v1)[idx];
3254             constructor_elt *c2 = &(*v2)[idx];
3255
3256             /* Check that each value is the same...  */
3257             if (!compare_constant (c1->value, c2->value))
3258               return 0;
3259             /* ... and that they apply to the same fields!  */
3260             if (typecode == ARRAY_TYPE)
3261               {
3262                 if (!compare_constant (c1->index, c2->index))
3263                   return 0;
3264               }
3265             else
3266               {
3267                 if (c1->index != c2->index)
3268                   return 0;
3269               }
3270           }
3271
3272         return 1;
3273       }
3274
3275     case ADDR_EXPR:
3276     case FDESC_EXPR:
3277       {
3278         class addr_const value1, value2;
3279         enum rtx_code code;
3280         int ret;
3281
3282         decode_addr_const (t1, &value1);
3283         decode_addr_const (t2, &value2);
3284
3285         if (maybe_ne (value1.offset, value2.offset))
3286           return 0;
3287
3288         code = GET_CODE (value1.base);
3289         if (code != GET_CODE (value2.base))
3290           return 0;
3291
3292         switch (code)
3293           {
3294           case SYMBOL_REF:
3295             ret = (strcmp (XSTR (value1.base, 0), XSTR (value2.base, 0)) == 0);
3296             break;
3297
3298           case LABEL_REF:
3299             ret = (CODE_LABEL_NUMBER (label_ref_label (value1.base))
3300                    == CODE_LABEL_NUMBER (label_ref_label (value2.base)));
3301             break;
3302
3303           default:
3304             gcc_unreachable ();
3305           }
3306         return ret;
3307       }
3308
3309     case PLUS_EXPR:
3310     case POINTER_PLUS_EXPR:
3311     case MINUS_EXPR:
3312     case RANGE_EXPR:
3313       return (compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0))
3314               && compare_constant (TREE_OPERAND (t1, 1), TREE_OPERAND (t2, 1)));
3315
3316     CASE_CONVERT:
3317     case VIEW_CONVERT_EXPR:
3318       return compare_constant (TREE_OPERAND (t1, 0), TREE_OPERAND (t2, 0));
3319
3320     default:
3321       return 0;
3322     }
3323
3324   gcc_unreachable ();
3325 }
3326 \f
3327 /* Return the section into which constant EXP should be placed.  */
3328
3329 static section *
3330 get_constant_section (tree exp, unsigned int align)
3331 {
3332   return targetm.asm_out.select_section (exp,
3333                                          compute_reloc_for_constant (exp),
3334                                          align);
3335 }
3336
3337 /* Return the size of constant EXP in bytes.  */
3338
3339 static HOST_WIDE_INT
3340 get_constant_size (tree exp)
3341 {
3342   HOST_WIDE_INT size;
3343
3344   size = int_size_in_bytes (TREE_TYPE (exp));
3345   gcc_checking_assert (size >= 0);
3346   gcc_checking_assert (TREE_CODE (exp) != STRING_CST
3347                        || size >= TREE_STRING_LENGTH (exp));
3348   return size;
3349 }
3350
3351 /* Subroutine of output_constant_def:
3352    No constant equal to EXP is known to have been output.
3353    Make a constant descriptor to enter EXP in the hash table.
3354    Assign the label number and construct RTL to refer to the
3355    constant's location in memory.
3356    Caller is responsible for updating the hash table.  */
3357
3358 static struct constant_descriptor_tree *
3359 build_constant_desc (tree exp)
3360 {
3361   struct constant_descriptor_tree *desc;
3362   rtx symbol, rtl;
3363   char label[256];
3364   int labelno;
3365   tree decl;
3366
3367   desc = ggc_alloc<constant_descriptor_tree> ();
3368   desc->value = exp;
3369
3370   /* Create a string containing the label name, in LABEL.  */
3371   labelno = const_labelno++;
3372   ASM_GENERATE_INTERNAL_LABEL (label, "LC", labelno);
3373
3374   /* Construct the VAR_DECL associated with the constant.  */
3375   decl = build_decl (UNKNOWN_LOCATION, VAR_DECL, get_identifier (label),
3376                      TREE_TYPE (exp));
3377   DECL_ARTIFICIAL (decl) = 1;
3378   DECL_IGNORED_P (decl) = 1;
3379   TREE_READONLY (decl) = 1;
3380   TREE_STATIC (decl) = 1;
3381   TREE_ADDRESSABLE (decl) = 1;
3382   /* We don't set the RTL yet as this would cause varpool to assume that the
3383      variable is referenced.  Moreover, it would just be dropped in LTO mode.
3384      Instead we set the flag that will be recognized in make_decl_rtl.  */
3385   DECL_IN_CONSTANT_POOL (decl) = 1;
3386   DECL_INITIAL (decl) = desc->value;
3387   /* ??? targetm.constant_alignment hasn't been updated for vector types on
3388      most architectures so use DATA_ALIGNMENT as well, except for strings.  */
3389   if (TREE_CODE (exp) == STRING_CST)
3390     SET_DECL_ALIGN (decl, targetm.constant_alignment (exp, DECL_ALIGN (decl)));
3391   else
3392     {
3393       align_variable (decl, 0);
3394       if (DECL_ALIGN (decl) < GET_MODE_ALIGNMENT (DECL_MODE (decl))
3395           && ((optab_handler (movmisalign_optab, DECL_MODE (decl))
3396                != CODE_FOR_nothing)
3397               || targetm.slow_unaligned_access (DECL_MODE (decl),
3398                                                 DECL_ALIGN (decl))))
3399         SET_DECL_ALIGN (decl, GET_MODE_ALIGNMENT (DECL_MODE (decl)));
3400     }
3401
3402   /* Now construct the SYMBOL_REF and the MEM.  */
3403   if (use_object_blocks_p ())
3404     {
3405       int align = (TREE_CODE (decl) == CONST_DECL
3406                    || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3407                    ? DECL_ALIGN (decl)
3408                    : symtab_node::get (decl)->definition_alignment ());
3409       section *sect = get_constant_section (exp, align);
3410       symbol = create_block_symbol (ggc_strdup (label),
3411                                     get_block_for_section (sect), -1);
3412     }
3413   else
3414     symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3415   SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3416   SET_SYMBOL_REF_DECL (symbol, decl);
3417   TREE_CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3418
3419   rtl = gen_const_mem (TYPE_MODE (TREE_TYPE (exp)), symbol);
3420   set_mem_attributes (rtl, exp, 1);
3421   set_mem_alias_set (rtl, 0);
3422
3423   /* Putting EXP into the literal pool might have imposed a different
3424      alignment which should be visible in the RTX as well.  */
3425   set_mem_align (rtl, DECL_ALIGN (decl));
3426
3427   /* We cannot share RTX'es in pool entries.
3428      Mark this piece of RTL as required for unsharing.  */
3429   RTX_FLAG (rtl, used) = 1;
3430
3431   /* Set flags or add text to the name to record information, such as
3432      that it is a local symbol.  If the name is changed, the macro
3433      ASM_OUTPUT_LABELREF will have to know how to strip this
3434      information.  This call might invalidate our local variable
3435      SYMBOL; we can't use it afterward.  */
3436   targetm.encode_section_info (exp, rtl, true);
3437
3438   desc->rtl = rtl;
3439
3440   return desc;
3441 }
3442
3443 /* Subroutine of output_constant_def and tree_output_constant_def:
3444    Add a constant to the hash table that tracks which constants
3445    already have labels.  */
3446
3447 static constant_descriptor_tree *
3448 add_constant_to_table (tree exp)
3449 {
3450   /* The hash table methods may call output_constant_def for addressed
3451      constants, so handle them first.  */
3452   output_addressed_constants (exp);
3453
3454   /* Sanity check to catch recursive insertion.  */
3455   static bool inserting;
3456   gcc_assert (!inserting);
3457   inserting = true;
3458
3459   /* Look up EXP in the table of constant descriptors.  If we didn't
3460      find it, create a new one.  */
3461   struct constant_descriptor_tree key;
3462   key.value = exp;
3463   key.hash = const_hash_1 (exp);
3464   constant_descriptor_tree **loc
3465     = const_desc_htab->find_slot_with_hash (&key, key.hash, INSERT);
3466
3467   inserting = false;
3468
3469   struct constant_descriptor_tree *desc = *loc;
3470   if (!desc)
3471     {
3472       desc = build_constant_desc (exp);
3473       desc->hash = key.hash;
3474       *loc = desc;
3475     }
3476
3477   return desc;
3478 }
3479
3480 /* Return an rtx representing a reference to constant data in memory
3481    for the constant expression EXP.
3482
3483    If assembler code for such a constant has already been output,
3484    return an rtx to refer to it.
3485    Otherwise, output such a constant in memory
3486    and generate an rtx for it.
3487
3488    If DEFER is nonzero, this constant can be deferred and output only
3489    if referenced in the function after all optimizations.
3490
3491    `const_desc_table' records which constants already have label strings.  */
3492
3493 rtx
3494 output_constant_def (tree exp, int defer)
3495 {
3496   struct constant_descriptor_tree *desc = add_constant_to_table (exp);
3497   maybe_output_constant_def_contents (desc, defer);
3498   return desc->rtl;
3499 }
3500
3501 /* Subroutine of output_constant_def: Decide whether or not we need to
3502    output the constant DESC now, and if so, do it.  */
3503 static void
3504 maybe_output_constant_def_contents (struct constant_descriptor_tree *desc,
3505                                     int defer)
3506 {
3507   rtx symbol = XEXP (desc->rtl, 0);
3508   tree exp = desc->value;
3509
3510   if (flag_syntax_only)
3511     return;
3512
3513   if (TREE_ASM_WRITTEN (exp))
3514     /* Already output; don't do it again.  */
3515     return;
3516
3517   /* We can always defer constants as long as the context allows
3518      doing so.  */
3519   if (defer)
3520     {
3521       /* Increment n_deferred_constants if it exists.  It needs to be at
3522          least as large as the number of constants actually referred to
3523          by the function.  If it's too small we'll stop looking too early
3524          and fail to emit constants; if it's too large we'll only look
3525          through the entire function when we could have stopped earlier.  */
3526       if (cfun)
3527         n_deferred_constants++;
3528       return;
3529     }
3530
3531   output_constant_def_contents (symbol);
3532 }
3533
3534 /* Subroutine of output_constant_def_contents.  Output the definition
3535    of constant EXP, which is pointed to by label LABEL.  ALIGN is the
3536    constant's alignment in bits.  */
3537
3538 static void
3539 assemble_constant_contents (tree exp, const char *label, unsigned int align,
3540                             bool merge_strings)
3541 {
3542   HOST_WIDE_INT size;
3543
3544   size = get_constant_size (exp);
3545
3546   /* Do any machine/system dependent processing of the constant.  */
3547   targetm.asm_out.declare_constant_name (asm_out_file, label, exp, size);
3548
3549   /* Output the value of EXP.  */
3550   output_constant (exp, size, align, false, merge_strings);
3551
3552   targetm.asm_out.decl_end ();
3553 }
3554
3555 /* We must output the constant data referred to by SYMBOL; do so.  */
3556
3557 static void
3558 output_constant_def_contents (rtx symbol)
3559 {
3560   tree decl = SYMBOL_REF_DECL (symbol);
3561   tree exp = DECL_INITIAL (decl);
3562   bool asan_protected = false;
3563
3564   /* Make sure any other constants whose addresses appear in EXP
3565      are assigned label numbers.  */
3566   output_addressed_constants (exp);
3567
3568   /* We are no longer deferring this constant.  */
3569   TREE_ASM_WRITTEN (decl) = TREE_ASM_WRITTEN (exp) = 1;
3570
3571   if ((flag_sanitize & SANITIZE_ADDRESS)
3572       && TREE_CODE (exp) == STRING_CST
3573       && asan_protect_global (exp))
3574     {
3575       asan_protected = true;
3576       SET_DECL_ALIGN (decl, MAX (DECL_ALIGN (decl),
3577                                  ASAN_RED_ZONE_SIZE * BITS_PER_UNIT));
3578     }
3579
3580   /* If the constant is part of an object block, make sure that the
3581      decl has been positioned within its block, but do not write out
3582      its definition yet.  output_object_blocks will do that later.  */
3583   if (SYMBOL_REF_HAS_BLOCK_INFO_P (symbol) && SYMBOL_REF_BLOCK (symbol))
3584     place_block_symbol (symbol);
3585   else
3586     {
3587       int align = (TREE_CODE (decl) == CONST_DECL
3588                    || (VAR_P (decl) && DECL_IN_CONSTANT_POOL (decl))
3589                    ? DECL_ALIGN (decl)
3590                    : symtab_node::get (decl)->definition_alignment ());
3591       section *sect = get_constant_section (exp, align);
3592       switch_to_section (sect);
3593       if (align > BITS_PER_UNIT)
3594         ASM_OUTPUT_ALIGN (asm_out_file, floor_log2 (align / BITS_PER_UNIT));
3595       assemble_constant_contents (exp, XSTR (symbol, 0), align,
3596                                   (sect->common.flags & SECTION_MERGE)
3597                                   && (sect->common.flags & SECTION_STRINGS));
3598       if (asan_protected)
3599         {
3600           HOST_WIDE_INT size = get_constant_size (exp);
3601           assemble_zeros (asan_red_zone_size (size));
3602         }
3603     }
3604 }
3605
3606 /* Look up EXP in the table of constant descriptors.  Return the rtl
3607    if it has been emitted, else null.  */
3608
3609 rtx
3610 lookup_constant_def (tree exp)
3611 {
3612   struct constant_descriptor_tree key;
3613
3614   key.value = exp;
3615   key.hash = const_hash_1 (exp);
3616   constant_descriptor_tree *desc
3617     = const_desc_htab->find_with_hash (&key, key.hash);
3618
3619   return (desc ? desc->rtl : NULL_RTX);
3620 }
3621
3622 /* Return a tree representing a reference to constant data in memory
3623    for the constant expression EXP.
3624
3625    This is the counterpart of output_constant_def at the Tree level.  */
3626
3627 tree
3628 tree_output_constant_def (tree exp)
3629 {
3630   struct constant_descriptor_tree *desc = add_constant_to_table (exp);
3631   tree decl = SYMBOL_REF_DECL (XEXP (desc->rtl, 0));
3632   varpool_node::finalize_decl (decl);
3633   return decl;
3634 }
3635 \f
3636 class GTY((chain_next ("%h.next"), for_user)) constant_descriptor_rtx {
3637 public:
3638   class constant_descriptor_rtx *next;
3639   rtx mem;
3640   rtx sym;
3641   rtx constant;
3642   HOST_WIDE_INT offset;
3643   hashval_t hash;
3644   fixed_size_mode mode;
3645   unsigned int align;
3646   int labelno;
3647   int mark;
3648 };
3649
3650 struct const_rtx_desc_hasher : ggc_ptr_hash<constant_descriptor_rtx>
3651 {
3652   static hashval_t hash (constant_descriptor_rtx *);
3653   static bool equal (constant_descriptor_rtx *, constant_descriptor_rtx *);
3654 };
3655
3656 /* Used in the hash tables to avoid outputting the same constant
3657    twice.  Unlike 'struct constant_descriptor_tree', RTX constants
3658    are output once per function, not once per file.  */
3659 /* ??? Only a few targets need per-function constant pools.  Most
3660    can use one per-file pool.  Should add a targetm bit to tell the
3661    difference.  */
3662
3663 struct GTY(()) rtx_constant_pool {
3664   /* Pointers to first and last constant in pool, as ordered by offset.  */
3665   class constant_descriptor_rtx *first;
3666   class constant_descriptor_rtx *last;
3667
3668   /* Hash facility for making memory-constants from constant rtl-expressions.
3669      It is used on RISC machines where immediate integer arguments and
3670      constant addresses are restricted so that such constants must be stored
3671      in memory.  */
3672   hash_table<const_rtx_desc_hasher> *const_rtx_htab;
3673
3674   /* Current offset in constant pool (does not include any
3675      machine-specific header).  */
3676   HOST_WIDE_INT offset;
3677 };
3678
3679 /* Hash and compare functions for const_rtx_htab.  */
3680
3681 hashval_t
3682 const_rtx_desc_hasher::hash (constant_descriptor_rtx *desc)
3683 {
3684   return desc->hash;
3685 }
3686
3687 bool
3688 const_rtx_desc_hasher::equal (constant_descriptor_rtx *x,
3689                               constant_descriptor_rtx *y)
3690 {
3691   if (x->mode != y->mode)
3692     return 0;
3693   return rtx_equal_p (x->constant, y->constant);
3694 }
3695
3696 /* Hash one component of a constant.  */
3697
3698 static hashval_t
3699 const_rtx_hash_1 (const_rtx x)
3700 {
3701   unsigned HOST_WIDE_INT hwi;
3702   machine_mode mode;
3703   enum rtx_code code;
3704   hashval_t h;
3705   int i;
3706
3707   code = GET_CODE (x);
3708   mode = GET_MODE (x);
3709   h = (hashval_t) code * 1048573 + mode;
3710
3711   switch (code)
3712     {
3713     case CONST_INT:
3714       hwi = INTVAL (x);
3715
3716     fold_hwi:
3717       {
3718         int shift = sizeof (hashval_t) * CHAR_BIT;
3719         const int n = sizeof (HOST_WIDE_INT) / sizeof (hashval_t);
3720
3721         h ^= (hashval_t) hwi;
3722         for (i = 1; i < n; ++i)
3723           {
3724             hwi >>= shift;
3725             h ^= (hashval_t) hwi;
3726           }
3727       }
3728       break;
3729
3730     case CONST_WIDE_INT:
3731       hwi = 0;
3732       {
3733         for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
3734           hwi ^= CONST_WIDE_INT_ELT (x, i);
3735         goto fold_hwi;
3736       }
3737
3738     case CONST_DOUBLE:
3739       if (TARGET_SUPPORTS_WIDE_INT == 0 && mode == VOIDmode)
3740         {
3741           hwi = CONST_DOUBLE_LOW (x) ^ CONST_DOUBLE_HIGH (x);
3742           goto fold_hwi;
3743         }
3744       else
3745         h ^= real_hash (CONST_DOUBLE_REAL_VALUE (x));
3746       break;
3747
3748     case CONST_FIXED:
3749       h ^= fixed_hash (CONST_FIXED_VALUE (x));
3750       break;
3751
3752     case SYMBOL_REF:
3753       h ^= htab_hash_string (XSTR (x, 0));
3754       break;
3755
3756     case LABEL_REF:
3757       h = h * 251 + CODE_LABEL_NUMBER (label_ref_label (x));
3758       break;
3759
3760     case UNSPEC:
3761     case UNSPEC_VOLATILE:
3762       h = h * 251 + XINT (x, 1);
3763       break;
3764
3765     default:
3766       break;
3767     }
3768
3769   return h;
3770 }
3771
3772 /* Compute a hash value for X, which should be a constant.  */
3773
3774 static hashval_t
3775 const_rtx_hash (rtx x)
3776 {
3777   hashval_t h = 0;
3778   subrtx_iterator::array_type array;
3779   FOR_EACH_SUBRTX (iter, array, x, ALL)
3780     h = h * 509 + const_rtx_hash_1 (*iter);
3781   return h;
3782 }
3783
3784 \f
3785 /* Create and return a new rtx constant pool.  */
3786
3787 static struct rtx_constant_pool *
3788 create_constant_pool (void)
3789 {
3790   struct rtx_constant_pool *pool;
3791
3792   pool = ggc_alloc<rtx_constant_pool> ();
3793   pool->const_rtx_htab = hash_table<const_rtx_desc_hasher>::create_ggc (31);
3794   pool->first = NULL;
3795   pool->last = NULL;
3796   pool->offset = 0;
3797   return pool;
3798 }
3799
3800 /* Initialize constant pool hashing for a new function.  */
3801
3802 void
3803 init_varasm_status (void)
3804 {
3805   crtl->varasm.pool = create_constant_pool ();
3806   crtl->varasm.deferred_constants = 0;
3807 }
3808 \f
3809 /* Given a MINUS expression, simplify it if both sides
3810    include the same symbol.  */
3811
3812 rtx
3813 simplify_subtraction (rtx x)
3814 {
3815   rtx r = simplify_rtx (x);
3816   return r ? r : x;
3817 }
3818 \f
3819 /* Given a constant rtx X, make (or find) a memory constant for its value
3820    and return a MEM rtx to refer to it in memory.  IN_MODE is the mode
3821    of X.  */
3822
3823 rtx
3824 force_const_mem (machine_mode in_mode, rtx x)
3825 {
3826   class constant_descriptor_rtx *desc, tmp;
3827   struct rtx_constant_pool *pool;
3828   char label[256];
3829   rtx def, symbol;
3830   hashval_t hash;
3831   unsigned int align;
3832   constant_descriptor_rtx **slot;
3833   fixed_size_mode mode;
3834
3835   /* We can't force variable-sized objects to memory.  */
3836   if (!is_a <fixed_size_mode> (in_mode, &mode))
3837     return NULL_RTX;
3838
3839   /* If we're not allowed to drop X into the constant pool, don't.  */
3840   if (targetm.cannot_force_const_mem (mode, x))
3841     return NULL_RTX;
3842
3843   /* Record that this function has used a constant pool entry.  */
3844   crtl->uses_const_pool = 1;
3845
3846   /* Decide which pool to use.  */
3847   pool = (targetm.use_blocks_for_constant_p (mode, x)
3848           ? shared_constant_pool
3849           : crtl->varasm.pool);
3850
3851   /* Lookup the value in the hashtable.  */
3852   tmp.constant = x;
3853   tmp.mode = mode;
3854   hash = const_rtx_hash (x);
3855   slot = pool->const_rtx_htab->find_slot_with_hash (&tmp, hash, INSERT);
3856   desc = *slot;
3857
3858   /* If the constant was already present, return its memory.  */
3859   if (desc)
3860     return copy_rtx (desc->mem);
3861
3862   /* Otherwise, create a new descriptor.  */
3863   desc = ggc_alloc<constant_descriptor_rtx> ();
3864   *slot = desc;
3865
3866   /* Align the location counter as required by EXP's data type.  */
3867   machine_mode align_mode = (mode == VOIDmode ? word_mode : mode);
3868   align = targetm.static_rtx_alignment (align_mode);
3869
3870   pool->offset += (align / BITS_PER_UNIT) - 1;
3871   pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
3872
3873   desc->next = NULL;
3874   desc->constant = copy_rtx (tmp.constant);
3875   desc->offset = pool->offset;
3876   desc->hash = hash;
3877   desc->mode = mode;
3878   desc->align = align;
3879   desc->labelno = const_labelno;
3880   desc->mark = 0;
3881
3882   pool->offset += GET_MODE_SIZE (mode);
3883   if (pool->last)
3884     pool->last->next = desc;
3885   else
3886     pool->first = pool->last = desc;
3887   pool->last = desc;
3888
3889   /* Create a string containing the label name, in LABEL.  */
3890   ASM_GENERATE_INTERNAL_LABEL (label, "LC", const_labelno);
3891   ++const_labelno;
3892
3893   /* Construct the SYMBOL_REF.  Make sure to mark it as belonging to
3894      the constants pool.  */
3895   if (use_object_blocks_p () && targetm.use_blocks_for_constant_p (mode, x))
3896     {
3897       section *sect = targetm.asm_out.select_rtx_section (mode, x, align);
3898       symbol = create_block_symbol (ggc_strdup (label),
3899                                     get_block_for_section (sect), -1);
3900     }
3901   else
3902     symbol = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup (label));
3903   desc->sym = symbol;
3904   SYMBOL_REF_FLAGS (symbol) |= SYMBOL_FLAG_LOCAL;
3905   CONSTANT_POOL_ADDRESS_P (symbol) = 1;
3906   SET_SYMBOL_REF_CONSTANT (symbol, desc);
3907
3908   /* Construct the MEM.  */
3909   desc->mem = def = gen_const_mem (mode, symbol);
3910   set_mem_align (def, align);
3911
3912   /* If we're dropping a label to the constant pool, make sure we
3913      don't delete it.  */
3914   if (GET_CODE (x) == LABEL_REF)
3915     LABEL_PRESERVE_P (XEXP (x, 0)) = 1;
3916
3917   return copy_rtx (def);
3918 }
3919 \f
3920 /* Given a constant pool SYMBOL_REF, return the corresponding constant.  */
3921
3922 rtx
3923 get_pool_constant (const_rtx addr)
3924 {
3925   return SYMBOL_REF_CONSTANT (addr)->constant;
3926 }
3927
3928 /* Given a constant pool SYMBOL_REF, return the corresponding constant
3929    and whether it has been output or not.  */
3930
3931 rtx
3932 get_pool_constant_mark (rtx addr, bool *pmarked)
3933 {
3934   class constant_descriptor_rtx *desc;
3935
3936   desc = SYMBOL_REF_CONSTANT (addr);
3937   *pmarked = (desc->mark != 0);
3938   return desc->constant;
3939 }
3940
3941 /* Similar, return the mode.  */
3942
3943 fixed_size_mode
3944 get_pool_mode (const_rtx addr)
3945 {
3946   return SYMBOL_REF_CONSTANT (addr)->mode;
3947 }
3948
3949 /* Return TRUE if and only if the constant pool has no entries.  Note
3950    that even entries we might end up choosing not to emit are counted
3951    here, so there is the potential for missed optimizations.  */
3952
3953 bool
3954 constant_pool_empty_p (void)
3955 {
3956   return crtl->varasm.pool->first == NULL;
3957 }
3958 \f
3959 /* Worker function for output_constant_pool_1.  Emit assembly for X
3960    in MODE with known alignment ALIGN.  */
3961
3962 static void
3963 output_constant_pool_2 (fixed_size_mode mode, rtx x, unsigned int align)
3964 {
3965   switch (GET_MODE_CLASS (mode))
3966     {
3967     case MODE_FLOAT:
3968     case MODE_DECIMAL_FLOAT:
3969       {
3970         gcc_assert (CONST_DOUBLE_AS_FLOAT_P (x));
3971         assemble_real (*CONST_DOUBLE_REAL_VALUE (x),
3972                        as_a <scalar_float_mode> (mode), align, false);
3973         break;
3974       }
3975
3976     case MODE_INT:
3977     case MODE_PARTIAL_INT:
3978     case MODE_FRACT:
3979     case MODE_UFRACT:
3980     case MODE_ACCUM:
3981     case MODE_UACCUM:
3982       assemble_integer (x, GET_MODE_SIZE (mode), align, 1);
3983       break;
3984
3985     case MODE_VECTOR_BOOL:
3986       {
3987         gcc_assert (GET_CODE (x) == CONST_VECTOR);
3988
3989         /* Pick the smallest integer mode that contains at least one
3990            whole element.  Often this is byte_mode and contains more
3991            than one element.  */
3992         unsigned int nelts = GET_MODE_NUNITS (mode);
3993         unsigned int elt_bits = GET_MODE_BITSIZE (mode) / nelts;
3994         unsigned int int_bits = MAX (elt_bits, BITS_PER_UNIT);
3995         scalar_int_mode int_mode = int_mode_for_size (int_bits, 0).require ();
3996
3997         /* Build the constant up one integer at a time.  */
3998         unsigned int elts_per_int = int_bits / elt_bits;
3999         for (unsigned int i = 0; i < nelts; i += elts_per_int)
4000           {
4001             unsigned HOST_WIDE_INT value = 0;
4002             unsigned int limit = MIN (nelts - i, elts_per_int);
4003             for (unsigned int j = 0; j < limit; ++j)
4004               if (INTVAL (CONST_VECTOR_ELT (x, i + j)) != 0)
4005                 value |= 1 << (j * elt_bits);
4006             output_constant_pool_2 (int_mode, gen_int_mode (value, int_mode),
4007                                     i != 0 ? MIN (align, int_bits) : align);
4008           }
4009         break;
4010       }
4011     case MODE_VECTOR_FLOAT:
4012     case MODE_VECTOR_INT:
4013     case MODE_VECTOR_FRACT:
4014     case MODE_VECTOR_UFRACT:
4015     case MODE_VECTOR_ACCUM:
4016     case MODE_VECTOR_UACCUM:
4017       {
4018         int i, units;
4019         scalar_mode submode = GET_MODE_INNER (mode);
4020         unsigned int subalign = MIN (align, GET_MODE_BITSIZE (submode));
4021
4022         gcc_assert (GET_CODE (x) == CONST_VECTOR);
4023         units = GET_MODE_NUNITS (mode);
4024
4025         for (i = 0; i < units; i++)
4026           {
4027             rtx elt = CONST_VECTOR_ELT (x, i);
4028             output_constant_pool_2 (submode, elt, i ? subalign : align);
4029           }
4030       }
4031       break;
4032
4033     default:
4034       gcc_unreachable ();
4035     }
4036 }
4037
4038 /* Worker function for output_constant_pool.  Emit constant DESC,
4039    giving it ALIGN bits of alignment.  */
4040
4041 static void
4042 output_constant_pool_1 (class constant_descriptor_rtx *desc,
4043                         unsigned int align)
4044 {
4045   rtx x, tmp;
4046
4047   x = desc->constant;
4048
4049   /* See if X is a LABEL_REF (or a CONST referring to a LABEL_REF)
4050      whose CODE_LABEL has been deleted.  This can occur if a jump table
4051      is eliminated by optimization.  If so, write a constant of zero
4052      instead.  Note that this can also happen by turning the
4053      CODE_LABEL into a NOTE.  */
4054   /* ??? This seems completely and utterly wrong.  Certainly it's
4055      not true for NOTE_INSN_DELETED_LABEL, but I disbelieve proper
4056      functioning even with rtx_insn::deleted and friends.  */
4057
4058   tmp = x;
4059   switch (GET_CODE (tmp))
4060     {
4061     case CONST:
4062       if (GET_CODE (XEXP (tmp, 0)) != PLUS
4063           || GET_CODE (XEXP (XEXP (tmp, 0), 0)) != LABEL_REF)
4064         break;
4065       tmp = XEXP (XEXP (tmp, 0), 0);
4066       /* FALLTHRU  */
4067
4068     case LABEL_REF:
4069       {
4070         rtx_insn *insn = label_ref_label (tmp);
4071         gcc_assert (!insn->deleted ());
4072         gcc_assert (!NOTE_P (insn)
4073                     || NOTE_KIND (insn) != NOTE_INSN_DELETED);
4074         break;
4075       }
4076
4077     default:
4078       break;
4079     }
4080
4081 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4082   ASM_OUTPUT_SPECIAL_POOL_ENTRY (asm_out_file, x, desc->mode,
4083                                  align, desc->labelno, done);
4084 #endif
4085
4086   assemble_align (align);
4087
4088   /* Output the label.  */
4089   targetm.asm_out.internal_label (asm_out_file, "LC", desc->labelno);
4090
4091   /* Output the data.
4092      Pass actual alignment value while emitting string constant to asm code
4093      as function 'output_constant_pool_1' explicitly passes the alignment as 1
4094      assuming that the data is already aligned which prevents the generation 
4095      of fix-up table entries.  */
4096   output_constant_pool_2 (desc->mode, x, desc->align);
4097
4098   /* Make sure all constants in SECTION_MERGE and not SECTION_STRINGS
4099      sections have proper size.  */
4100   if (align > GET_MODE_BITSIZE (desc->mode)
4101       && in_section
4102       && (in_section->common.flags & SECTION_MERGE))
4103     assemble_align (align);
4104
4105 #ifdef ASM_OUTPUT_SPECIAL_POOL_ENTRY
4106  done:
4107 #endif
4108   return;
4109 }
4110
4111 /* Recompute the offsets of entries in POOL, and the overall size of
4112    POOL.  Do this after calling mark_constant_pool to ensure that we
4113    are computing the offset values for the pool which we will actually
4114    emit.  */
4115
4116 static void
4117 recompute_pool_offsets (struct rtx_constant_pool *pool)
4118 {
4119   class constant_descriptor_rtx *desc;
4120   pool->offset = 0;
4121
4122   for (desc = pool->first; desc ; desc = desc->next)
4123     if (desc->mark)
4124       {
4125           /* Recalculate offset.  */
4126         unsigned int align = desc->align;
4127         pool->offset += (align / BITS_PER_UNIT) - 1;
4128         pool->offset &= ~ ((align / BITS_PER_UNIT) - 1);
4129         desc->offset = pool->offset;
4130         pool->offset += GET_MODE_SIZE (desc->mode);
4131       }
4132 }
4133
4134 /* Mark all constants that are referenced by SYMBOL_REFs in X.
4135    Emit referenced deferred strings.  */
4136
4137 static void
4138 mark_constants_in_pattern (rtx insn)
4139 {
4140   subrtx_iterator::array_type array;
4141   FOR_EACH_SUBRTX (iter, array, PATTERN (insn), ALL)
4142     {
4143       const_rtx x = *iter;
4144       if (GET_CODE (x) == SYMBOL_REF)
4145         {
4146           if (CONSTANT_POOL_ADDRESS_P (x))
4147             {
4148               class constant_descriptor_rtx *desc = SYMBOL_REF_CONSTANT (x);
4149               if (desc->mark == 0)
4150                 {
4151                   desc->mark = 1;
4152                   iter.substitute (desc->constant);
4153                 }
4154             }
4155           else if (TREE_CONSTANT_POOL_ADDRESS_P (x))
4156             {
4157               tree decl = SYMBOL_REF_DECL (x);
4158               if (!TREE_ASM_WRITTEN (DECL_INITIAL (decl)))
4159                 {
4160                   n_deferred_constants--;
4161                   output_constant_def_contents (CONST_CAST_RTX (x));
4162                 }
4163             }
4164         }
4165     }
4166 }
4167
4168 /* Look through appropriate parts of INSN, marking all entries in the
4169    constant pool which are actually being used.  Entries that are only
4170    referenced by other constants are also marked as used.  Emit
4171    deferred strings that are used.  */
4172
4173 static void
4174 mark_constants (rtx_insn *insn)
4175 {
4176   if (!INSN_P (insn))
4177     return;
4178
4179   /* Insns may appear inside a SEQUENCE.  Only check the patterns of
4180      insns, not any notes that may be attached.  We don't want to mark
4181      a constant just because it happens to appear in a REG_EQUIV note.  */
4182   if (rtx_sequence *seq = dyn_cast <rtx_sequence *> (PATTERN (insn)))
4183     {
4184       int i, n = seq->len ();
4185       for (i = 0; i < n; ++i)
4186         {
4187           rtx subinsn = seq->element (i);
4188           if (INSN_P (subinsn))
4189             mark_constants_in_pattern (subinsn);
4190         }
4191     }
4192   else
4193     mark_constants_in_pattern (insn);
4194 }
4195
4196 /* Look through the instructions for this function, and mark all the
4197    entries in POOL which are actually being used.  Emit deferred constants
4198    which have indeed been used.  */
4199
4200 static void
4201 mark_constant_pool (void)
4202 {
4203   rtx_insn *insn;
4204
4205   if (!crtl->uses_const_pool && n_deferred_constants == 0)
4206     return;
4207
4208   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4209     mark_constants (insn);
4210 }
4211
4212 /* Write all the constants in POOL.  */
4213
4214 static void
4215 output_constant_pool_contents (struct rtx_constant_pool *pool)
4216 {
4217   class constant_descriptor_rtx *desc;
4218
4219   for (desc = pool->first; desc ; desc = desc->next)
4220     if (desc->mark)
4221       {
4222         /* If the constant is part of an object_block, make sure that
4223            the constant has been positioned within its block, but do not
4224            write out its definition yet.  output_object_blocks will do
4225            that later.  */
4226         if (SYMBOL_REF_HAS_BLOCK_INFO_P (desc->sym)
4227             && SYMBOL_REF_BLOCK (desc->sym))
4228           place_block_symbol (desc->sym);
4229         else
4230           {
4231             switch_to_section (targetm.asm_out.select_rtx_section
4232                                (desc->mode, desc->constant, desc->align));
4233             output_constant_pool_1 (desc, desc->align);
4234           }
4235       }
4236 }
4237
4238 /* Mark all constants that are used in the current function, then write
4239    out the function's private constant pool.  */
4240
4241 static void
4242 output_constant_pool (const char *fnname ATTRIBUTE_UNUSED,
4243                       tree fndecl ATTRIBUTE_UNUSED)
4244 {
4245   struct rtx_constant_pool *pool = crtl->varasm.pool;
4246
4247   /* It is possible for gcc to call force_const_mem and then to later
4248      discard the instructions which refer to the constant.  In such a
4249      case we do not need to output the constant.  */
4250   mark_constant_pool ();
4251
4252   /* Having marked the constant pool entries we'll actually emit, we
4253      now need to rebuild the offset information, which may have become
4254      stale.  */
4255   recompute_pool_offsets (pool);
4256
4257 #ifdef ASM_OUTPUT_POOL_PROLOGUE
4258   ASM_OUTPUT_POOL_PROLOGUE (asm_out_file, fnname, fndecl, pool->offset);
4259 #endif
4260
4261   output_constant_pool_contents (pool);
4262
4263 #ifdef ASM_OUTPUT_POOL_EPILOGUE
4264   ASM_OUTPUT_POOL_EPILOGUE (asm_out_file, fnname, fndecl, pool->offset);
4265 #endif
4266 }
4267 \f
4268 /* Write the contents of the shared constant pool.  */
4269
4270 void
4271 output_shared_constant_pool (void)
4272 {
4273   output_constant_pool_contents (shared_constant_pool);
4274 }
4275 \f
4276 /* Determine what kind of relocations EXP may need.  */
4277
4278 int
4279 compute_reloc_for_constant (tree exp)
4280 {
4281   int reloc = 0, reloc2;
4282   tree tem;
4283
4284   switch (TREE_CODE (exp))
4285     {
4286     case ADDR_EXPR:
4287     case FDESC_EXPR:
4288       /* Go inside any operations that get_inner_reference can handle and see
4289          if what's inside is a constant: no need to do anything here for
4290          addresses of variables or functions.  */
4291       for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4292            tem = TREE_OPERAND (tem, 0))
4293         ;
4294
4295       if (TREE_CODE (tem) == MEM_REF
4296           && TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR)
4297         {
4298           reloc = compute_reloc_for_constant (TREE_OPERAND (tem, 0));
4299           break;
4300         }
4301
4302       if (!targetm.binds_local_p (tem))
4303         reloc |= 2;
4304       else
4305         reloc |= 1;
4306       break;
4307
4308     case PLUS_EXPR:
4309     case POINTER_PLUS_EXPR:
4310       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4311       reloc |= compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4312       break;
4313
4314     case MINUS_EXPR:
4315       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4316       reloc2 = compute_reloc_for_constant (TREE_OPERAND (exp, 1));
4317       /* The difference of two local labels is computable at link time.  */
4318       if (reloc == 1 && reloc2 == 1)
4319         reloc = 0;
4320       else
4321         reloc |= reloc2;
4322       break;
4323
4324     CASE_CONVERT:
4325     case VIEW_CONVERT_EXPR:
4326       reloc = compute_reloc_for_constant (TREE_OPERAND (exp, 0));
4327       break;
4328
4329     case CONSTRUCTOR:
4330       {
4331         unsigned HOST_WIDE_INT idx;
4332         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4333           if (tem != 0)
4334             reloc |= compute_reloc_for_constant (tem);
4335       }
4336       break;
4337
4338     default:
4339       break;
4340     }
4341   return reloc;
4342 }
4343
4344 /* Find all the constants whose addresses are referenced inside of EXP,
4345    and make sure assembler code with a label has been output for each one.
4346    Indicate whether an ADDR_EXPR has been encountered.  */
4347
4348 static void
4349 output_addressed_constants (tree exp)
4350 {
4351   tree tem;
4352
4353   switch (TREE_CODE (exp))
4354     {
4355     case ADDR_EXPR:
4356     case FDESC_EXPR:
4357       /* Go inside any operations that get_inner_reference can handle and see
4358          if what's inside is a constant: no need to do anything here for
4359          addresses of variables or functions.  */
4360       for (tem = TREE_OPERAND (exp, 0); handled_component_p (tem);
4361            tem = TREE_OPERAND (tem, 0))
4362         ;
4363
4364       /* If we have an initialized CONST_DECL, retrieve the initializer.  */
4365       if (TREE_CODE (tem) == CONST_DECL && DECL_INITIAL (tem))
4366         tem = DECL_INITIAL (tem);
4367
4368       if (CONSTANT_CLASS_P (tem) || TREE_CODE (tem) == CONSTRUCTOR)
4369         output_constant_def (tem, 0);
4370
4371       if (TREE_CODE (tem) == MEM_REF)
4372         output_addressed_constants (TREE_OPERAND (tem, 0));
4373       break;
4374
4375     case PLUS_EXPR:
4376     case POINTER_PLUS_EXPR:
4377     case MINUS_EXPR:
4378       output_addressed_constants (TREE_OPERAND (exp, 1));
4379       gcc_fallthrough ();
4380
4381     CASE_CONVERT:
4382     case VIEW_CONVERT_EXPR:
4383       output_addressed_constants (TREE_OPERAND (exp, 0));
4384       break;
4385
4386     case CONSTRUCTOR:
4387       {
4388         unsigned HOST_WIDE_INT idx;
4389         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, tem)
4390           if (tem != 0)
4391             output_addressed_constants (tem);
4392       }
4393       break;
4394
4395     default:
4396       break;
4397     }
4398 }
4399 \f
4400 /* Whether a constructor CTOR is a valid static constant initializer if all
4401    its elements are.  This used to be internal to initializer_constant_valid_p
4402    and has been exposed to let other functions like categorize_ctor_elements
4403    evaluate the property while walking a constructor for other purposes.  */
4404
4405 bool
4406 constructor_static_from_elts_p (const_tree ctor)
4407 {
4408   return (TREE_CONSTANT (ctor)
4409           && (TREE_CODE (TREE_TYPE (ctor)) == UNION_TYPE
4410               || TREE_CODE (TREE_TYPE (ctor)) == RECORD_TYPE
4411               || TREE_CODE (TREE_TYPE (ctor)) == ARRAY_TYPE));
4412 }
4413
4414 static tree initializer_constant_valid_p_1 (tree value, tree endtype,
4415                                             tree *cache);
4416
4417 /* A subroutine of initializer_constant_valid_p.  VALUE is a MINUS_EXPR,
4418    PLUS_EXPR or POINTER_PLUS_EXPR.  This looks for cases of VALUE
4419    which are valid when ENDTYPE is an integer of any size; in
4420    particular, this does not accept a pointer minus a constant.  This
4421    returns null_pointer_node if the VALUE is an absolute constant
4422    which can be used to initialize a static variable.  Otherwise it
4423    returns NULL.  */
4424
4425 static tree
4426 narrowing_initializer_constant_valid_p (tree value, tree endtype, tree *cache)
4427 {
4428   tree op0, op1;
4429
4430   if (!INTEGRAL_TYPE_P (endtype))
4431     return NULL_TREE;
4432
4433   op0 = TREE_OPERAND (value, 0);
4434   op1 = TREE_OPERAND (value, 1);
4435
4436   /* Like STRIP_NOPS except allow the operand mode to widen.  This
4437      works around a feature of fold that simplifies (int)(p1 - p2) to
4438      ((int)p1 - (int)p2) under the theory that the narrower operation
4439      is cheaper.  */
4440
4441   while (CONVERT_EXPR_P (op0)
4442          || TREE_CODE (op0) == NON_LVALUE_EXPR)
4443     {
4444       tree inner = TREE_OPERAND (op0, 0);
4445       if (inner == error_mark_node
4446           || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4447           || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op0)))
4448               > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4449         break;
4450       op0 = inner;
4451     }
4452
4453   while (CONVERT_EXPR_P (op1)
4454          || TREE_CODE (op1) == NON_LVALUE_EXPR)
4455     {
4456       tree inner = TREE_OPERAND (op1, 0);
4457       if (inner == error_mark_node
4458           || ! INTEGRAL_MODE_P (TYPE_MODE (TREE_TYPE (inner)))
4459           || (GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (op1)))
4460               > GET_MODE_SIZE (SCALAR_INT_TYPE_MODE (TREE_TYPE (inner)))))
4461         break;
4462       op1 = inner;
4463     }
4464
4465   op0 = initializer_constant_valid_p_1 (op0, endtype, cache);
4466   if (!op0)
4467     return NULL_TREE;
4468
4469   op1 = initializer_constant_valid_p_1 (op1, endtype,
4470                                         cache ? cache + 2 : NULL);
4471   /* Both initializers must be known.  */
4472   if (op1)
4473     {
4474       if (op0 == op1
4475           && (op0 == null_pointer_node
4476               || TREE_CODE (value) == MINUS_EXPR))
4477         return null_pointer_node;
4478
4479       /* Support differences between labels.  */
4480       if (TREE_CODE (op0) == LABEL_DECL
4481           && TREE_CODE (op1) == LABEL_DECL)
4482         return null_pointer_node;
4483
4484       if (TREE_CODE (op0) == STRING_CST
4485           && TREE_CODE (op1) == STRING_CST
4486           && operand_equal_p (op0, op1, 1))
4487         return null_pointer_node;
4488     }
4489
4490   return NULL_TREE;
4491 }
4492
4493 /* Helper function of initializer_constant_valid_p.
4494    Return nonzero if VALUE is a valid constant-valued expression
4495    for use in initializing a static variable; one that can be an
4496    element of a "constant" initializer.
4497
4498    Return null_pointer_node if the value is absolute;
4499    if it is relocatable, return the variable that determines the relocation.
4500    We assume that VALUE has been folded as much as possible;
4501    therefore, we do not need to check for such things as
4502    arithmetic-combinations of integers.
4503
4504    Use CACHE (pointer to 2 tree values) for caching if non-NULL.  */
4505
4506 static tree
4507 initializer_constant_valid_p_1 (tree value, tree endtype, tree *cache)
4508 {
4509   tree ret;
4510
4511   switch (TREE_CODE (value))
4512     {
4513     case CONSTRUCTOR:
4514       if (constructor_static_from_elts_p (value))
4515         {
4516           unsigned HOST_WIDE_INT idx;
4517           tree elt;
4518           bool absolute = true;
4519
4520           if (cache && cache[0] == value)
4521             return cache[1];
4522           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4523             {
4524               tree reloc;
4525               reloc = initializer_constant_valid_p_1 (elt, TREE_TYPE (elt),
4526                                                       NULL);
4527               if (!reloc
4528                   /* An absolute value is required with reverse SSO.  */
4529                   || (reloc != null_pointer_node
4530                       && TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (value))
4531                       && !AGGREGATE_TYPE_P (TREE_TYPE (elt))))
4532                 {
4533                   if (cache)
4534                     {
4535                       cache[0] = value;
4536                       cache[1] = NULL_TREE;
4537                     }
4538                   return NULL_TREE;
4539                 }
4540               if (reloc != null_pointer_node)
4541                 absolute = false;
4542             }
4543           /* For a non-absolute relocation, there is no single
4544              variable that can be "the variable that determines the
4545              relocation."  */
4546           if (cache)
4547             {
4548               cache[0] = value;
4549               cache[1] = absolute ? null_pointer_node : error_mark_node;
4550             }
4551           return absolute ? null_pointer_node : error_mark_node;
4552         }
4553
4554       return TREE_STATIC (value) ? null_pointer_node : NULL_TREE;
4555
4556     case INTEGER_CST:
4557     case VECTOR_CST:
4558     case REAL_CST:
4559     case FIXED_CST:
4560     case STRING_CST:
4561     case COMPLEX_CST:
4562       return null_pointer_node;
4563
4564     case ADDR_EXPR:
4565     case FDESC_EXPR:
4566       {
4567         tree op0 = staticp (TREE_OPERAND (value, 0));
4568         if (op0)
4569           {
4570             /* "&(*a).f" is like unto pointer arithmetic.  If "a" turns out
4571                to be a constant, this is old-skool offsetof-like nonsense.  */
4572             if (TREE_CODE (op0) == INDIRECT_REF
4573                 && TREE_CONSTANT (TREE_OPERAND (op0, 0)))
4574               return null_pointer_node;
4575             /* Taking the address of a nested function involves a trampoline,
4576                unless we don't need or want one.  */
4577             if (TREE_CODE (op0) == FUNCTION_DECL
4578                 && DECL_STATIC_CHAIN (op0)
4579                 && !TREE_NO_TRAMPOLINE (value))
4580               return NULL_TREE;
4581             /* "&{...}" requires a temporary to hold the constructed
4582                object.  */
4583             if (TREE_CODE (op0) == CONSTRUCTOR)
4584               return NULL_TREE;
4585           }
4586         return op0;
4587       }
4588
4589     case NON_LVALUE_EXPR:
4590       return initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4591                                              endtype, cache);
4592
4593     case VIEW_CONVERT_EXPR:
4594       {
4595         tree src = TREE_OPERAND (value, 0);
4596         tree src_type = TREE_TYPE (src);
4597         tree dest_type = TREE_TYPE (value);
4598
4599         /* Allow view-conversions from aggregate to non-aggregate type only
4600            if the bit pattern is fully preserved afterwards; otherwise, the
4601            RTL expander won't be able to apply a subsequent transformation
4602            to the underlying constructor.  */
4603         if (AGGREGATE_TYPE_P (src_type) && !AGGREGATE_TYPE_P (dest_type))
4604           {
4605             if (TYPE_MODE (endtype) == TYPE_MODE (dest_type))
4606               return initializer_constant_valid_p_1 (src, endtype, cache);
4607             else
4608               return NULL_TREE;
4609           }
4610
4611         /* Allow all other kinds of view-conversion.  */
4612         return initializer_constant_valid_p_1 (src, endtype, cache);
4613       }
4614
4615     CASE_CONVERT:
4616       {
4617         tree src = TREE_OPERAND (value, 0);
4618         tree src_type = TREE_TYPE (src);
4619         tree dest_type = TREE_TYPE (value);
4620
4621         /* Allow conversions between pointer types, floating-point
4622            types, and offset types.  */
4623         if ((POINTER_TYPE_P (dest_type) && POINTER_TYPE_P (src_type))
4624             || (FLOAT_TYPE_P (dest_type) && FLOAT_TYPE_P (src_type))
4625             || (TREE_CODE (dest_type) == OFFSET_TYPE
4626                 && TREE_CODE (src_type) == OFFSET_TYPE))
4627           return initializer_constant_valid_p_1 (src, endtype, cache);
4628
4629         /* Allow length-preserving conversions between integer types.  */
4630         if (INTEGRAL_TYPE_P (dest_type) && INTEGRAL_TYPE_P (src_type)
4631             && (TYPE_PRECISION (dest_type) == TYPE_PRECISION (src_type)))
4632           return initializer_constant_valid_p_1 (src, endtype, cache);
4633
4634         /* Allow conversions between other integer types only if
4635            explicit value.  Don't allow sign-extension to a type larger
4636            than word and pointer, there aren't relocations that would
4637            allow to sign extend it to a wider type.  */
4638         if (INTEGRAL_TYPE_P (dest_type)
4639             && INTEGRAL_TYPE_P (src_type)
4640             && (TYPE_UNSIGNED (src_type)
4641                 || TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type)
4642                 || TYPE_PRECISION (dest_type) <= BITS_PER_WORD
4643                 || TYPE_PRECISION (dest_type) <= POINTER_SIZE))
4644           {
4645             tree inner = initializer_constant_valid_p_1 (src, endtype, cache);
4646             if (inner == null_pointer_node)
4647               return null_pointer_node;
4648             break;
4649           }
4650
4651         /* Allow (int) &foo provided int is as wide as a pointer.  */
4652         if (INTEGRAL_TYPE_P (dest_type) && POINTER_TYPE_P (src_type)
4653             && (TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type)))
4654           return initializer_constant_valid_p_1 (src, endtype, cache);
4655
4656         /* Likewise conversions from int to pointers, but also allow
4657            conversions from 0.  */
4658         if ((POINTER_TYPE_P (dest_type)
4659              || TREE_CODE (dest_type) == OFFSET_TYPE)
4660             && INTEGRAL_TYPE_P (src_type))
4661           {
4662             if (TREE_CODE (src) == INTEGER_CST
4663                 && TYPE_PRECISION (dest_type) >= TYPE_PRECISION (src_type))
4664               return null_pointer_node;
4665             if (integer_zerop (src))
4666               return null_pointer_node;
4667             else if (TYPE_PRECISION (dest_type) <= TYPE_PRECISION (src_type))
4668               return initializer_constant_valid_p_1 (src, endtype, cache);
4669           }
4670
4671         /* Allow conversions to struct or union types if the value
4672            inside is okay.  */
4673         if (TREE_CODE (dest_type) == RECORD_TYPE
4674             || TREE_CODE (dest_type) == UNION_TYPE)
4675           return initializer_constant_valid_p_1 (src, endtype, cache);
4676       }
4677       break;
4678
4679     case POINTER_PLUS_EXPR:
4680     case PLUS_EXPR:
4681       /* Any valid floating-point constants will have been folded by now;
4682          with -frounding-math we hit this with addition of two constants.  */
4683       if (TREE_CODE (endtype) == REAL_TYPE)
4684         return NULL_TREE;
4685       if (cache && cache[0] == value)
4686         return cache[1];
4687       if (! INTEGRAL_TYPE_P (endtype)
4688           || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4689         {
4690           tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4691           tree valid0
4692             = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4693                                               endtype, ncache);
4694           tree valid1
4695             = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4696                                               endtype, ncache + 2);
4697           /* If either term is absolute, use the other term's relocation.  */
4698           if (valid0 == null_pointer_node)
4699             ret = valid1;
4700           else if (valid1 == null_pointer_node)
4701             ret = valid0;
4702           /* Support narrowing pointer differences.  */
4703           else
4704             ret = narrowing_initializer_constant_valid_p (value, endtype,
4705                                                           ncache);
4706         }
4707       else
4708       /* Support narrowing pointer differences.  */
4709         ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4710       if (cache)
4711         {
4712           cache[0] = value;
4713           cache[1] = ret;
4714         }
4715       return ret;
4716
4717     case POINTER_DIFF_EXPR:
4718     case MINUS_EXPR:
4719       if (TREE_CODE (endtype) == REAL_TYPE)
4720         return NULL_TREE;
4721       if (cache && cache[0] == value)
4722         return cache[1];
4723       if (! INTEGRAL_TYPE_P (endtype)
4724           || TYPE_PRECISION (endtype) >= TYPE_PRECISION (TREE_TYPE (value)))
4725         {
4726           tree ncache[4] = { NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE };
4727           tree valid0
4728             = initializer_constant_valid_p_1 (TREE_OPERAND (value, 0),
4729                                               endtype, ncache);
4730           tree valid1
4731             = initializer_constant_valid_p_1 (TREE_OPERAND (value, 1),
4732                                               endtype, ncache + 2);
4733           /* Win if second argument is absolute.  */
4734           if (valid1 == null_pointer_node)
4735             ret = valid0;
4736           /* Win if both arguments have the same relocation.
4737              Then the value is absolute.  */
4738           else if (valid0 == valid1 && valid0 != 0)
4739             ret = null_pointer_node;
4740           /* Since GCC guarantees that string constants are unique in the
4741              generated code, a subtraction between two copies of the same
4742              constant string is absolute.  */
4743           else if (valid0 && TREE_CODE (valid0) == STRING_CST
4744                    && valid1 && TREE_CODE (valid1) == STRING_CST
4745                    && operand_equal_p (valid0, valid1, 1))
4746             ret = null_pointer_node;
4747           /* Support narrowing differences.  */
4748           else
4749             ret = narrowing_initializer_constant_valid_p (value, endtype,
4750                                                           ncache);
4751         }
4752       else
4753         /* Support narrowing differences.  */
4754         ret = narrowing_initializer_constant_valid_p (value, endtype, NULL);
4755       if (cache)
4756         {
4757           cache[0] = value;
4758           cache[1] = ret;
4759         }
4760       return ret;
4761
4762     default:
4763       break;
4764     }
4765
4766   return NULL_TREE;
4767 }
4768
4769 /* Return nonzero if VALUE is a valid constant-valued expression
4770    for use in initializing a static variable; one that can be an
4771    element of a "constant" initializer.
4772
4773    Return null_pointer_node if the value is absolute;
4774    if it is relocatable, return the variable that determines the relocation.
4775    We assume that VALUE has been folded as much as possible;
4776    therefore, we do not need to check for such things as
4777    arithmetic-combinations of integers.  */
4778 tree
4779 initializer_constant_valid_p (tree value, tree endtype, bool reverse)
4780 {
4781   tree reloc = initializer_constant_valid_p_1 (value, endtype, NULL);
4782
4783   /* An absolute value is required with reverse storage order.  */
4784   if (reloc
4785       && reloc != null_pointer_node
4786       && reverse
4787       && !AGGREGATE_TYPE_P (endtype)
4788       && !VECTOR_TYPE_P (endtype))
4789     reloc = NULL_TREE;
4790
4791   return reloc;
4792 }
4793 \f
4794 /* Return true if VALUE is a valid constant-valued expression
4795    for use in initializing a static bit-field; one that can be
4796    an element of a "constant" initializer.  */
4797
4798 bool
4799 initializer_constant_valid_for_bitfield_p (tree value)
4800 {
4801   /* For bitfields we support integer constants or possibly nested aggregates
4802      of such.  */
4803   switch (TREE_CODE (value))
4804     {
4805     case CONSTRUCTOR:
4806       {
4807         unsigned HOST_WIDE_INT idx;
4808         tree elt;
4809
4810         FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (value), idx, elt)
4811           if (!initializer_constant_valid_for_bitfield_p (elt))
4812             return false;
4813         return true;
4814       }
4815
4816     case INTEGER_CST:
4817     case REAL_CST:
4818       return true;
4819
4820     case VIEW_CONVERT_EXPR:
4821     case NON_LVALUE_EXPR:
4822       return
4823         initializer_constant_valid_for_bitfield_p (TREE_OPERAND (value, 0));
4824
4825     default:
4826       break;
4827     }
4828
4829   return false;
4830 }
4831
4832 /* Check if a STRING_CST fits into the field.
4833    Tolerate only the case when the NUL termination
4834    does not fit into the field.   */
4835
4836 static bool
4837 check_string_literal (tree string, unsigned HOST_WIDE_INT size)
4838 {
4839   tree type = TREE_TYPE (string);
4840   tree eltype = TREE_TYPE (type);
4841   unsigned HOST_WIDE_INT elts = tree_to_uhwi (TYPE_SIZE_UNIT (eltype));
4842   unsigned HOST_WIDE_INT mem_size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
4843   int len = TREE_STRING_LENGTH (string);
4844
4845   if (elts != 1 && elts != 2 && elts != 4)
4846     return false;
4847   if (len < 0 || len % elts != 0)
4848     return false;
4849   if (size < (unsigned)len)
4850     return false;
4851   if (mem_size != size)
4852     return false;
4853   return true;
4854 }
4855
4856 /* output_constructor outer state of relevance in recursive calls, typically
4857    for nested aggregate bitfields.  */
4858
4859 struct oc_outer_state {
4860   unsigned int bit_offset;  /* current position in ...  */
4861   int byte;                 /* ... the outer byte buffer.  */
4862 };
4863
4864 static unsigned HOST_WIDE_INT
4865 output_constructor (tree, unsigned HOST_WIDE_INT, unsigned int, bool,
4866                     oc_outer_state *);
4867
4868 /* Output assembler code for constant EXP, with no label.
4869    This includes the pseudo-op such as ".int" or ".byte", and a newline.
4870    Assumes output_addressed_constants has been done on EXP already.
4871
4872    Generate at least SIZE bytes of assembler data, padding at the end
4873    with zeros if necessary.  SIZE must always be specified.  The returned
4874    value is the actual number of bytes of assembler data generated, which
4875    may be bigger than SIZE if the object contains a variable length field.
4876
4877    SIZE is important for structure constructors,
4878    since trailing members may have been omitted from the constructor.
4879    It is also important for initialization of arrays from string constants
4880    since the full length of the string constant might not be wanted.
4881    It is also needed for initialization of unions, where the initializer's
4882    type is just one member, and that may not be as long as the union.
4883
4884    There a case in which we would fail to output exactly SIZE bytes:
4885    for a structure constructor that wants to produce more than SIZE bytes.
4886    But such constructors will never be generated for any possible input.
4887
4888    ALIGN is the alignment of the data in bits.
4889
4890    If REVERSE is true, EXP is output in reverse storage order.  */
4891
4892 static unsigned HOST_WIDE_INT
4893 output_constant (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
4894                  bool reverse, bool merge_strings)
4895 {
4896   enum tree_code code;
4897   unsigned HOST_WIDE_INT thissize;
4898   rtx cst;
4899
4900   if (size == 0 || flag_syntax_only)
4901     return size;
4902
4903   /* See if we're trying to initialize a pointer in a non-default mode
4904      to the address of some declaration somewhere.  If the target says
4905      the mode is valid for pointers, assume the target has a way of
4906      resolving it.  */
4907   if (TREE_CODE (exp) == NOP_EXPR
4908       && POINTER_TYPE_P (TREE_TYPE (exp))
4909       && targetm.addr_space.valid_pointer_mode
4910            (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
4911             TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4912     {
4913       tree saved_type = TREE_TYPE (exp);
4914
4915       /* Peel off any intermediate conversions-to-pointer for valid
4916          pointer modes.  */
4917       while (TREE_CODE (exp) == NOP_EXPR
4918              && POINTER_TYPE_P (TREE_TYPE (exp))
4919              && targetm.addr_space.valid_pointer_mode
4920                   (SCALAR_INT_TYPE_MODE (TREE_TYPE (exp)),
4921                    TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)))))
4922         exp = TREE_OPERAND (exp, 0);
4923
4924       /* If what we're left with is the address of something, we can
4925          convert the address to the final type and output it that
4926          way.  */
4927       if (TREE_CODE (exp) == ADDR_EXPR)
4928         exp = build1 (ADDR_EXPR, saved_type, TREE_OPERAND (exp, 0));
4929       /* Likewise for constant ints.  */
4930       else if (TREE_CODE (exp) == INTEGER_CST)
4931         exp = fold_convert (saved_type, exp);
4932
4933     }
4934
4935   /* Eliminate any conversions since we'll be outputting the underlying
4936      constant.  */
4937   while (CONVERT_EXPR_P (exp)
4938          || TREE_CODE (exp) == NON_LVALUE_EXPR
4939          || TREE_CODE (exp) == VIEW_CONVERT_EXPR)
4940     {
4941       HOST_WIDE_INT type_size = int_size_in_bytes (TREE_TYPE (exp));
4942       HOST_WIDE_INT op_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (exp, 0)));
4943
4944       /* Make sure eliminating the conversion is really a no-op, except with
4945          VIEW_CONVERT_EXPRs to allow for wild Ada unchecked conversions and
4946          union types to allow for Ada unchecked unions.  */
4947       if (type_size > op_size
4948           && TREE_CODE (exp) != VIEW_CONVERT_EXPR
4949           && TREE_CODE (TREE_TYPE (exp)) != UNION_TYPE)
4950         /* Keep the conversion. */
4951         break;
4952       else
4953         exp = TREE_OPERAND (exp, 0);
4954     }
4955
4956   code = TREE_CODE (TREE_TYPE (exp));
4957   thissize = int_size_in_bytes (TREE_TYPE (exp));
4958
4959   /* Allow a constructor with no elements for any data type.
4960      This means to fill the space with zeros.  */
4961   if (TREE_CODE (exp) == CONSTRUCTOR
4962       && vec_safe_is_empty (CONSTRUCTOR_ELTS (exp)))
4963     {
4964       assemble_zeros (size);
4965       return size;
4966     }
4967
4968   if (TREE_CODE (exp) == FDESC_EXPR)
4969     {
4970 #ifdef ASM_OUTPUT_FDESC
4971       HOST_WIDE_INT part = tree_to_shwi (TREE_OPERAND (exp, 1));
4972       tree decl = TREE_OPERAND (exp, 0);
4973       ASM_OUTPUT_FDESC (asm_out_file, decl, part);
4974 #else
4975       gcc_unreachable ();
4976 #endif
4977       return size;
4978     }
4979
4980   /* Now output the underlying data.  If we've handling the padding, return.
4981      Otherwise, break and ensure SIZE is the size written.  */
4982   switch (code)
4983     {
4984     case BOOLEAN_TYPE:
4985     case INTEGER_TYPE:
4986     case ENUMERAL_TYPE:
4987     case POINTER_TYPE:
4988     case REFERENCE_TYPE:
4989     case OFFSET_TYPE:
4990     case FIXED_POINT_TYPE:
4991     case NULLPTR_TYPE:
4992       cst = expand_expr (exp, NULL_RTX, VOIDmode, EXPAND_INITIALIZER);
4993       if (reverse)
4994         cst = flip_storage_order (TYPE_MODE (TREE_TYPE (exp)), cst);
4995       if (!assemble_integer (cst, MIN (size, thissize), align, 0))
4996         error ("initializer for integer/fixed-point value is too complicated");
4997       break;
4998
4999     case REAL_TYPE:
5000       if (TREE_CODE (exp) != REAL_CST)
5001         error ("initializer for floating value is not a floating constant");
5002       else
5003         assemble_real (TREE_REAL_CST (exp),
5004                        SCALAR_FLOAT_TYPE_MODE (TREE_TYPE (exp)),
5005                        align, reverse);
5006       break;
5007
5008     case COMPLEX_TYPE:
5009       output_constant (TREE_REALPART (exp), thissize / 2, align,
5010                        reverse, false);
5011       output_constant (TREE_IMAGPART (exp), thissize / 2,
5012                        min_align (align, BITS_PER_UNIT * (thissize / 2)),
5013                        reverse, false);
5014       break;
5015
5016     case ARRAY_TYPE:
5017     case VECTOR_TYPE:
5018       switch (TREE_CODE (exp))
5019         {
5020         case CONSTRUCTOR:
5021           return output_constructor (exp, size, align, reverse, NULL);
5022         case STRING_CST:
5023           thissize = (unsigned HOST_WIDE_INT)TREE_STRING_LENGTH (exp);
5024           if (merge_strings
5025               && (thissize == 0
5026                   || TREE_STRING_POINTER (exp) [thissize - 1] != '\0'))
5027             thissize++;
5028           gcc_checking_assert (check_string_literal (exp, size));
5029           assemble_string (TREE_STRING_POINTER (exp), thissize);
5030           break;
5031         case VECTOR_CST:
5032           {
5033             scalar_mode inner = SCALAR_TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
5034             unsigned int nalign = MIN (align, GET_MODE_ALIGNMENT (inner));
5035             int elt_size = GET_MODE_SIZE (inner);
5036             output_constant (VECTOR_CST_ELT (exp, 0), elt_size, align,
5037                              reverse, false);
5038             thissize = elt_size;
5039             /* Static constants must have a fixed size.  */
5040             unsigned int nunits = VECTOR_CST_NELTS (exp).to_constant ();
5041             for (unsigned int i = 1; i < nunits; i++)
5042               {
5043                 output_constant (VECTOR_CST_ELT (exp, i), elt_size, nalign,
5044                                  reverse, false);
5045                 thissize += elt_size;
5046               }
5047             break;
5048           }
5049         default:
5050           gcc_unreachable ();
5051         }
5052       break;
5053
5054     case RECORD_TYPE:
5055     case UNION_TYPE:
5056       gcc_assert (TREE_CODE (exp) == CONSTRUCTOR);
5057       return output_constructor (exp, size, align, reverse, NULL);
5058
5059     case ERROR_MARK:
5060       return 0;
5061
5062     default:
5063       gcc_unreachable ();
5064     }
5065
5066   if (size > thissize)
5067     assemble_zeros (size - thissize);
5068
5069   return size;
5070 }
5071 \f
5072 /* Subroutine of output_constructor, used for computing the size of
5073    arrays of unspecified length.  VAL must be a CONSTRUCTOR of an array
5074    type with an unspecified upper bound.  */
5075
5076 static unsigned HOST_WIDE_INT
5077 array_size_for_constructor (tree val)
5078 {
5079   tree max_index;
5080   unsigned HOST_WIDE_INT cnt;
5081   tree index, value, tmp;
5082   offset_int i;
5083
5084   /* This code used to attempt to handle string constants that are not
5085      arrays of single-bytes, but nothing else does, so there's no point in
5086      doing it here.  */
5087   if (TREE_CODE (val) == STRING_CST)
5088     return TREE_STRING_LENGTH (val);
5089
5090   max_index = NULL_TREE;
5091   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (val), cnt, index, value)
5092     {
5093       if (TREE_CODE (index) == RANGE_EXPR)
5094         index = TREE_OPERAND (index, 1);
5095       if (max_index == NULL_TREE || tree_int_cst_lt (max_index, index))
5096         max_index = index;
5097     }
5098
5099   if (max_index == NULL_TREE)
5100     return 0;
5101
5102   /* Compute the total number of array elements.  */
5103   tmp = TYPE_MIN_VALUE (TYPE_DOMAIN (TREE_TYPE (val)));
5104   i = wi::to_offset (max_index) - wi::to_offset (tmp) + 1;
5105
5106   /* Multiply by the array element unit size to find number of bytes.  */
5107   i *= wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (val))));
5108
5109   gcc_assert (wi::fits_uhwi_p (i));
5110   return i.to_uhwi ();
5111 }
5112
5113 /* Other datastructures + helpers for output_constructor.  */
5114
5115 /* output_constructor local state to support interaction with helpers.  */
5116
5117 struct oc_local_state {
5118
5119   /* Received arguments.  */
5120   tree exp;                     /* Constructor expression.  */
5121   tree type;                    /* Type of constructor expression.  */
5122   unsigned HOST_WIDE_INT size;  /* # bytes to output - pad if necessary.  */
5123   unsigned int align;           /* Known initial alignment.  */
5124   tree min_index;               /* Lower bound if specified for an array.  */
5125
5126   /* Output processing state.  */
5127   HOST_WIDE_INT total_bytes;  /* # bytes output so far / current position.  */
5128   int byte;                   /* Part of a bitfield byte yet to be output.  */
5129   int last_relative_index;    /* Implicit or explicit index of the last
5130                                  array element output within a bitfield.  */
5131   bool byte_buffer_in_use;    /* Whether BYTE is in use.  */
5132   bool reverse;               /* Whether reverse storage order is in use.  */
5133
5134   /* Current element.  */
5135   tree field;      /* Current field decl in a record.  */
5136   tree val;        /* Current element value.  */
5137   tree index;      /* Current element index.  */
5138
5139 };
5140
5141 /* Helper for output_constructor.  From the current LOCAL state, output a
5142    RANGE_EXPR element.  */
5143
5144 static void
5145 output_constructor_array_range (oc_local_state *local)
5146 {
5147   unsigned HOST_WIDE_INT fieldsize
5148     = int_size_in_bytes (TREE_TYPE (local->type));
5149
5150   HOST_WIDE_INT lo_index
5151     = tree_to_shwi (TREE_OPERAND (local->index, 0));
5152   HOST_WIDE_INT hi_index
5153     = tree_to_shwi (TREE_OPERAND (local->index, 1));
5154   HOST_WIDE_INT index;
5155
5156   unsigned int align2
5157     = min_align (local->align, fieldsize * BITS_PER_UNIT);
5158
5159   for (index = lo_index; index <= hi_index; index++)
5160     {
5161       /* Output the element's initial value.  */
5162       if (local->val == NULL_TREE)
5163         assemble_zeros (fieldsize);
5164       else
5165         fieldsize = output_constant (local->val, fieldsize, align2,
5166                                      local->reverse, false);
5167
5168       /* Count its size.  */
5169       local->total_bytes += fieldsize;
5170     }
5171 }
5172
5173 /* Helper for output_constructor.  From the current LOCAL state, output a
5174    field element that is not true bitfield or part of an outer one.  */
5175
5176 static void
5177 output_constructor_regular_field (oc_local_state *local)
5178 {
5179   /* Field size and position.  Since this structure is static, we know the
5180      positions are constant.  */
5181   unsigned HOST_WIDE_INT fieldsize;
5182   HOST_WIDE_INT fieldpos;
5183
5184   unsigned int align2;
5185
5186   /* Output any buffered-up bit-fields preceding this element.  */
5187   if (local->byte_buffer_in_use)
5188     {
5189       assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5190       local->total_bytes++;
5191       local->byte_buffer_in_use = false;
5192     }
5193
5194   if (local->index != NULL_TREE)
5195     {
5196       /* Perform the index calculation in modulo arithmetic but
5197          sign-extend the result because Ada has negative DECL_FIELD_OFFSETs
5198          but we are using an unsigned sizetype.  */
5199       unsigned prec = TYPE_PRECISION (sizetype);
5200       offset_int idx = wi::sext (wi::to_offset (local->index)
5201                                  - wi::to_offset (local->min_index), prec);
5202       fieldpos = (idx * wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (local->val))))
5203         .to_short_addr ();
5204     }
5205   else if (local->field != NULL_TREE)
5206     fieldpos = int_byte_position (local->field);
5207   else
5208     fieldpos = 0;
5209
5210   /* Advance to offset of this element.
5211      Note no alignment needed in an array, since that is guaranteed
5212      if each element has the proper size.  */
5213   if (local->field != NULL_TREE || local->index != NULL_TREE)
5214     {
5215       if (fieldpos > local->total_bytes)
5216         {
5217           assemble_zeros (fieldpos - local->total_bytes);
5218           local->total_bytes = fieldpos;
5219         }
5220       else
5221         /* Must not go backwards.  */
5222         gcc_assert (fieldpos == local->total_bytes);
5223     }
5224
5225   /* Find the alignment of this element.  */
5226   align2 = min_align (local->align, BITS_PER_UNIT * fieldpos);
5227
5228   /* Determine size this element should occupy.  */
5229   if (local->field)
5230     {
5231       fieldsize = 0;
5232
5233       /* If this is an array with an unspecified upper bound,
5234          the initializer determines the size.  */
5235       /* ??? This ought to only checked if DECL_SIZE_UNIT is NULL,
5236          but we cannot do this until the deprecated support for
5237          initializing zero-length array members is removed.  */
5238       if (TREE_CODE (TREE_TYPE (local->field)) == ARRAY_TYPE
5239           && (!TYPE_DOMAIN (TREE_TYPE (local->field))
5240               || !TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (local->field)))))
5241         {
5242           fieldsize = array_size_for_constructor (local->val);
5243           /* Given a non-empty initialization, this field had better
5244              be last.  Given a flexible array member, the next field
5245              on the chain is a TYPE_DECL of the enclosing struct.  */
5246           const_tree next = DECL_CHAIN (local->field);
5247           gcc_assert (!fieldsize || !next || TREE_CODE (next) != FIELD_DECL);
5248           tree size = TYPE_SIZE_UNIT (TREE_TYPE (local->val));
5249           gcc_checking_assert (compare_tree_int (size, fieldsize) == 0);
5250         }
5251       else
5252         fieldsize = tree_to_uhwi (DECL_SIZE_UNIT (local->field));
5253     }
5254   else
5255     fieldsize = int_size_in_bytes (TREE_TYPE (local->type));
5256
5257   /* Output the element's initial value.  */
5258   if (local->val == NULL_TREE)
5259     assemble_zeros (fieldsize);
5260   else
5261     fieldsize = output_constant (local->val, fieldsize, align2,
5262                                  local->reverse, false);
5263
5264   /* Count its size.  */
5265   local->total_bytes += fieldsize;
5266 }
5267
5268 /* Helper for output_constructor.  From the LOCAL state, output an element
5269    that is a true bitfield or part of an outer one.  BIT_OFFSET is the offset
5270    from the start of a possibly ongoing outer byte buffer.  */
5271
5272 static void
5273 output_constructor_bitfield (oc_local_state *local, unsigned int bit_offset)
5274 {
5275   /* Bit size of this element.  */
5276   HOST_WIDE_INT ebitsize
5277     = (local->field
5278        ? tree_to_uhwi (DECL_SIZE (local->field))
5279        : tree_to_uhwi (TYPE_SIZE (TREE_TYPE (local->type))));
5280
5281   /* Relative index of this element if this is an array component.  */
5282   HOST_WIDE_INT relative_index
5283     = (!local->field
5284        ? (local->index
5285           ? (tree_to_shwi (local->index)
5286              - tree_to_shwi (local->min_index))
5287           : local->last_relative_index + 1)
5288        : 0);
5289
5290   /* Bit position of this element from the start of the containing
5291      constructor.  */
5292   HOST_WIDE_INT constructor_relative_ebitpos
5293       = (local->field
5294          ? int_bit_position (local->field)
5295          : ebitsize * relative_index);
5296
5297   /* Bit position of this element from the start of a possibly ongoing
5298      outer byte buffer.  */
5299   HOST_WIDE_INT byte_relative_ebitpos
5300     = bit_offset + constructor_relative_ebitpos;
5301
5302   /* From the start of a possibly ongoing outer byte buffer, offsets to
5303      the first bit of this element and to the first bit past the end of
5304      this element.  */
5305   HOST_WIDE_INT next_offset = byte_relative_ebitpos;
5306   HOST_WIDE_INT end_offset = byte_relative_ebitpos + ebitsize;
5307
5308   local->last_relative_index = relative_index;
5309
5310   if (local->val == NULL_TREE)
5311     local->val = integer_zero_node;
5312
5313   while (TREE_CODE (local->val) == VIEW_CONVERT_EXPR
5314          || TREE_CODE (local->val) == NON_LVALUE_EXPR)
5315     local->val = TREE_OPERAND (local->val, 0);
5316
5317   if (TREE_CODE (local->val) != INTEGER_CST
5318       && TREE_CODE (local->val) != CONSTRUCTOR)
5319     {
5320       error ("invalid initial value for member %qE", DECL_NAME (local->field));
5321       return;
5322     }
5323
5324   /* If this field does not start in this (or next) byte, skip some bytes.  */
5325   if (next_offset / BITS_PER_UNIT != local->total_bytes)
5326     {
5327       /* Output remnant of any bit field in previous bytes.  */
5328       if (local->byte_buffer_in_use)
5329         {
5330           assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5331           local->total_bytes++;
5332           local->byte_buffer_in_use = false;
5333         }
5334
5335       /* If still not at proper byte, advance to there.  */
5336       if (next_offset / BITS_PER_UNIT != local->total_bytes)
5337         {
5338           gcc_assert (next_offset / BITS_PER_UNIT >= local->total_bytes);
5339           assemble_zeros (next_offset / BITS_PER_UNIT - local->total_bytes);
5340           local->total_bytes = next_offset / BITS_PER_UNIT;
5341         }
5342     }
5343
5344   /* Set up the buffer if necessary.  */
5345   if (!local->byte_buffer_in_use)
5346     {
5347       local->byte = 0;
5348       if (ebitsize > 0)
5349         local->byte_buffer_in_use = true;
5350     }
5351
5352   /* If this is nested constructor, recurse passing the bit offset and the
5353      pending data, then retrieve the new pending data afterwards.  */
5354   if (TREE_CODE (local->val) == CONSTRUCTOR)
5355     {
5356       oc_outer_state temp_state;
5357       temp_state.bit_offset = next_offset % BITS_PER_UNIT;
5358       temp_state.byte = local->byte;
5359       local->total_bytes
5360         += output_constructor (local->val, 0, 0, local->reverse, &temp_state);
5361       local->byte = temp_state.byte;
5362       return;
5363     }
5364
5365   /* Otherwise, we must split the element into pieces that fall within
5366      separate bytes, and combine each byte with previous or following
5367      bit-fields.  */
5368   while (next_offset < end_offset)
5369     {
5370       int this_time;
5371       int shift;
5372       unsigned HOST_WIDE_INT value;
5373       HOST_WIDE_INT next_byte = next_offset / BITS_PER_UNIT;
5374       HOST_WIDE_INT next_bit = next_offset % BITS_PER_UNIT;
5375
5376       /* Advance from byte to byte within this element when necessary.  */
5377       while (next_byte != local->total_bytes)
5378         {
5379           assemble_integer (GEN_INT (local->byte), 1, BITS_PER_UNIT, 1);
5380           local->total_bytes++;
5381           local->byte = 0;
5382         }
5383
5384       /* Number of bits we can process at once (all part of the same byte).  */
5385       this_time = MIN (end_offset - next_offset, BITS_PER_UNIT - next_bit);
5386       if (local->reverse ? !BYTES_BIG_ENDIAN : BYTES_BIG_ENDIAN)
5387         {
5388           /* For big-endian data, take the most significant bits (of the
5389              bits that are significant) first and put them into bytes from
5390              the most significant end.  */
5391           shift = end_offset - next_offset - this_time;
5392
5393           /* Don't try to take a bunch of bits that cross
5394              the word boundary in the INTEGER_CST.  We can
5395              only select bits from one element.  */
5396           if ((shift / HOST_BITS_PER_WIDE_INT)
5397               != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5398             {
5399               const int end = shift + this_time - 1;
5400               shift = end & -HOST_BITS_PER_WIDE_INT;
5401               this_time = end - shift + 1;
5402             }
5403
5404           /* Now get the bits we want to insert.  */
5405           value = wi::extract_uhwi (wi::to_widest (local->val),
5406                                     shift, this_time);
5407
5408           /* Get the result.  This works only when:
5409              1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
5410           local->byte |= value << (BITS_PER_UNIT - this_time - next_bit);
5411         }
5412       else
5413         {
5414           /* On little-endian machines, take the least significant bits of
5415              the value first and pack them starting at the least significant
5416              bits of the bytes.  */
5417           shift = next_offset - byte_relative_ebitpos;
5418
5419           /* Don't try to take a bunch of bits that cross
5420              the word boundary in the INTEGER_CST.  We can
5421              only select bits from one element.  */
5422           if ((shift / HOST_BITS_PER_WIDE_INT)
5423               != ((shift + this_time - 1) / HOST_BITS_PER_WIDE_INT))
5424             this_time
5425               = HOST_BITS_PER_WIDE_INT - (shift & (HOST_BITS_PER_WIDE_INT - 1));
5426
5427           /* Now get the bits we want to insert.  */
5428           value = wi::extract_uhwi (wi::to_widest (local->val),
5429                                     shift, this_time);
5430
5431           /* Get the result.  This works only when:
5432              1 <= this_time <= HOST_BITS_PER_WIDE_INT.  */
5433           local->byte |= value << next_bit;
5434         }
5435
5436       next_offset += this_time;
5437       local->byte_buffer_in_use = true;
5438     }
5439 }
5440
5441 /* Subroutine of output_constant, used for CONSTRUCTORs (aggregate constants).
5442    Generate at least SIZE bytes, padding if necessary.  OUTER designates the
5443    caller output state of relevance in recursive invocations.  */
5444
5445 static unsigned HOST_WIDE_INT
5446 output_constructor (tree exp, unsigned HOST_WIDE_INT size, unsigned int align,
5447                     bool reverse, oc_outer_state *outer)
5448 {
5449   unsigned HOST_WIDE_INT cnt;
5450   constructor_elt *ce;
5451   oc_local_state local;
5452
5453   /* Setup our local state to communicate with helpers.  */
5454   local.exp = exp;
5455   local.type = TREE_TYPE (exp);
5456   local.size = size;
5457   local.align = align;
5458   if (TREE_CODE (local.type) == ARRAY_TYPE && TYPE_DOMAIN (local.type))
5459     local.min_index = TYPE_MIN_VALUE (TYPE_DOMAIN (local.type));
5460   else
5461     local.min_index = integer_zero_node;
5462
5463   local.total_bytes = 0;
5464   local.byte_buffer_in_use = outer != NULL;
5465   local.byte = outer ? outer->byte : 0;
5466   local.last_relative_index = -1;
5467   /* The storage order is specified for every aggregate type.  */
5468   if (AGGREGATE_TYPE_P (local.type))
5469     local.reverse = TYPE_REVERSE_STORAGE_ORDER (local.type);
5470   else
5471     local.reverse = reverse;
5472
5473   gcc_assert (HOST_BITS_PER_WIDE_INT >= BITS_PER_UNIT);
5474
5475   /* As CE goes through the elements of the constant, FIELD goes through the
5476      structure fields if the constant is a structure.  If the constant is a
5477      union, we override this by getting the field from the TREE_LIST element.
5478      But the constant could also be an array.  Then FIELD is zero.
5479
5480      There is always a maximum of one element in the chain LINK for unions
5481      (even if the initializer in a source program incorrectly contains
5482      more one).  */
5483
5484   if (TREE_CODE (local.type) == RECORD_TYPE)
5485     local.field = TYPE_FIELDS (local.type);
5486   else
5487     local.field = NULL_TREE;
5488
5489   for (cnt = 0;
5490        vec_safe_iterate (CONSTRUCTOR_ELTS (exp), cnt, &ce);
5491        cnt++, local.field = local.field ? DECL_CHAIN (local.field) : 0)
5492     {
5493       local.val = ce->value;
5494       local.index = NULL_TREE;
5495
5496       /* The element in a union constructor specifies the proper field
5497          or index.  */
5498       if (RECORD_OR_UNION_TYPE_P (local.type) && ce->index != NULL_TREE)
5499         local.field = ce->index;
5500
5501       else if (TREE_CODE (local.type) == ARRAY_TYPE)
5502         local.index = ce->index;
5503
5504       if (local.field && flag_verbose_asm)
5505         fprintf (asm_out_file, "%s %s:\n",
5506                  ASM_COMMENT_START,
5507                  DECL_NAME (local.field)
5508                  ? IDENTIFIER_POINTER (DECL_NAME (local.field))
5509                  : "<anonymous>");
5510
5511       /* Eliminate the marker that makes a cast not be an lvalue.  */
5512       if (local.val != NULL_TREE)
5513         STRIP_NOPS (local.val);
5514
5515       /* Output the current element, using the appropriate helper ...  */
5516
5517       /* For an array slice not part of an outer bitfield.  */
5518       if (!outer
5519           && local.index != NULL_TREE
5520           && TREE_CODE (local.index) == RANGE_EXPR)
5521         output_constructor_array_range (&local);
5522
5523       /* For a field that is neither a true bitfield nor part of an outer one,
5524          known to be at least byte aligned and multiple-of-bytes long.  */
5525       else if (!outer
5526                && (local.field == NULL_TREE
5527                    || !CONSTRUCTOR_BITFIELD_P (local.field)))
5528         output_constructor_regular_field (&local);
5529
5530       /* For a true bitfield or part of an outer one.  Only INTEGER_CSTs are
5531          supported for scalar fields, so we may need to convert first.  */
5532       else
5533         {
5534           if (TREE_CODE (local.val) == REAL_CST)
5535             local.val
5536               = fold_unary (VIEW_CONVERT_EXPR,
5537                             build_nonstandard_integer_type
5538                             (TYPE_PRECISION (TREE_TYPE (local.val)), 0),
5539                             local.val);
5540           output_constructor_bitfield (&local, outer ? outer->bit_offset : 0);
5541         }
5542     }
5543
5544   /* If we are not at toplevel, save the pending data for our caller.
5545      Otherwise output the pending data and padding zeros as needed. */
5546   if (outer)
5547     outer->byte = local.byte;
5548   else
5549     {
5550       if (local.byte_buffer_in_use)
5551         {
5552           assemble_integer (GEN_INT (local.byte), 1, BITS_PER_UNIT, 1);
5553           local.total_bytes++;
5554         }
5555
5556       if ((unsigned HOST_WIDE_INT)local.total_bytes < local.size)
5557         {
5558           assemble_zeros (local.size - local.total_bytes);
5559           local.total_bytes = local.size;
5560         }
5561     }
5562
5563   return local.total_bytes;
5564 }
5565
5566 /* Mark DECL as weak.  */
5567
5568 static void
5569 mark_weak (tree decl)
5570 {
5571   if (DECL_WEAK (decl))
5572     return;
5573
5574   struct symtab_node *n = symtab_node::get (decl);
5575   if (n && n->refuse_visibility_changes)
5576     error ("%+qD declared weak after being used", decl);
5577   DECL_WEAK (decl) = 1;
5578
5579   if (DECL_RTL_SET_P (decl)
5580       && MEM_P (DECL_RTL (decl))
5581       && XEXP (DECL_RTL (decl), 0)
5582       && GET_CODE (XEXP (DECL_RTL (decl), 0)) == SYMBOL_REF)
5583     SYMBOL_REF_WEAK (XEXP (DECL_RTL (decl), 0)) = 1;
5584 }
5585
5586 /* Merge weak status between NEWDECL and OLDDECL.  */
5587
5588 void
5589 merge_weak (tree newdecl, tree olddecl)
5590 {
5591   if (DECL_WEAK (newdecl) == DECL_WEAK (olddecl))
5592     {
5593       if (DECL_WEAK (newdecl) && TARGET_SUPPORTS_WEAK)
5594         {
5595           tree *pwd;
5596           /* We put the NEWDECL on the weak_decls list at some point
5597              and OLDDECL as well.  Keep just OLDDECL on the list.  */
5598           for (pwd = &weak_decls; *pwd; pwd = &TREE_CHAIN (*pwd))
5599             if (TREE_VALUE (*pwd) == newdecl)
5600               {
5601                 *pwd = TREE_CHAIN (*pwd);
5602                 break;
5603               }
5604         }
5605       return;
5606     }
5607
5608   if (DECL_WEAK (newdecl))
5609     {
5610       tree wd;
5611
5612       /* NEWDECL is weak, but OLDDECL is not.  */
5613
5614       /* If we already output the OLDDECL, we're in trouble; we can't
5615          go back and make it weak.  This should never happen in
5616          unit-at-a-time compilation.  */
5617       gcc_assert (!TREE_ASM_WRITTEN (olddecl));
5618
5619       /* If we've already generated rtl referencing OLDDECL, we may
5620          have done so in a way that will not function properly with
5621          a weak symbol.  Again in unit-at-a-time this should be
5622          impossible.  */
5623       gcc_assert (!TREE_USED (olddecl)
5624                   || !TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (olddecl)));
5625
5626       /* PR 49899: You cannot convert a static function into a weak, public function.  */
5627       if (! TREE_PUBLIC (olddecl) && TREE_PUBLIC (newdecl))
5628         error ("weak declaration of %q+D being applied to a already "
5629                "existing, static definition", newdecl);
5630
5631       if (TARGET_SUPPORTS_WEAK)
5632         {
5633           /* We put the NEWDECL on the weak_decls list at some point.
5634              Replace it with the OLDDECL.  */
5635           for (wd = weak_decls; wd; wd = TREE_CHAIN (wd))
5636             if (TREE_VALUE (wd) == newdecl)
5637               {
5638                 TREE_VALUE (wd) = olddecl;
5639                 break;
5640               }
5641           /* We may not find the entry on the list.  If NEWDECL is a
5642              weak alias, then we will have already called
5643              globalize_decl to remove the entry; in that case, we do
5644              not need to do anything.  */
5645         }
5646
5647       /* Make the OLDDECL weak; it's OLDDECL that we'll be keeping.  */
5648       mark_weak (olddecl);
5649     }
5650   else
5651     /* OLDDECL was weak, but NEWDECL was not explicitly marked as
5652        weak.  Just update NEWDECL to indicate that it's weak too.  */
5653     mark_weak (newdecl);
5654 }
5655
5656 /* Declare DECL to be a weak symbol.  */
5657
5658 void
5659 declare_weak (tree decl)
5660 {
5661   gcc_assert (TREE_CODE (decl) != FUNCTION_DECL || !TREE_ASM_WRITTEN (decl));
5662   if (! TREE_PUBLIC (decl))
5663     {
5664       error ("weak declaration of %q+D must be public", decl);
5665       return;
5666     }
5667   else if (!TARGET_SUPPORTS_WEAK)
5668     warning (0, "weak declaration of %q+D not supported", decl);
5669
5670   mark_weak (decl);
5671   if (!lookup_attribute ("weak", DECL_ATTRIBUTES (decl)))
5672     DECL_ATTRIBUTES (decl)
5673       = tree_cons (get_identifier ("weak"), NULL, DECL_ATTRIBUTES (decl));
5674 }
5675
5676 static void
5677 weak_finish_1 (tree decl)
5678 {
5679 #if defined (ASM_WEAKEN_DECL) || defined (ASM_WEAKEN_LABEL)
5680   const char *const name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
5681 #endif
5682
5683   if (! TREE_USED (decl))
5684     return;
5685
5686 #ifdef ASM_WEAKEN_DECL
5687   ASM_WEAKEN_DECL (asm_out_file, decl, name, NULL);
5688 #else
5689 #ifdef ASM_WEAKEN_LABEL
5690   ASM_WEAKEN_LABEL (asm_out_file, name);
5691 #else
5692 #ifdef ASM_OUTPUT_WEAK_ALIAS
5693   {
5694     static bool warn_once = 0;
5695     if (! warn_once)
5696       {
5697         warning (0, "only weak aliases are supported in this configuration");
5698         warn_once = 1;
5699       }
5700     return;
5701   }
5702 #endif
5703 #endif
5704 #endif
5705 }
5706
5707 /* Fiven an assembly name, find the decl it is associated with.  */
5708 static tree
5709 find_decl (tree target)
5710 {
5711   symtab_node *node = symtab_node::get_for_asmname (target);
5712   if (node)
5713     return node->decl;
5714   return NULL_TREE;
5715 }
5716
5717 /* This TREE_LIST contains weakref targets.  */
5718
5719 static GTY(()) tree weakref_targets;
5720
5721 /* Emit any pending weak declarations.  */
5722
5723 void
5724 weak_finish (void)
5725 {
5726   tree t;
5727
5728   for (t = weakref_targets; t; t = TREE_CHAIN (t))
5729     {
5730       tree alias_decl = TREE_PURPOSE (t);
5731       tree target = ultimate_transparent_alias_target (&TREE_VALUE (t));
5732
5733       if (! TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (alias_decl))
5734          || TREE_SYMBOL_REFERENCED (target))
5735         /* Remove alias_decl from the weak list, but leave entries for
5736            the target alone.  */
5737         target = NULL_TREE;
5738 #ifndef ASM_OUTPUT_WEAKREF
5739       else if (! TREE_SYMBOL_REFERENCED (target))
5740         {
5741           /* Use ASM_WEAKEN_LABEL only if ASM_WEAKEN_DECL is not
5742              defined, otherwise we and weak_finish_1 would use
5743              different macros.  */
5744 # if defined ASM_WEAKEN_LABEL && ! defined ASM_WEAKEN_DECL
5745           ASM_WEAKEN_LABEL (asm_out_file, IDENTIFIER_POINTER (target));
5746 # else
5747           tree decl = find_decl (target);
5748
5749           if (! decl)
5750             {
5751               decl = build_decl (DECL_SOURCE_LOCATION (alias_decl),
5752                                  TREE_CODE (alias_decl), target,
5753                                  TREE_TYPE (alias_decl));
5754
5755               DECL_EXTERNAL (decl) = 1;
5756               TREE_PUBLIC (decl) = 1;
5757               DECL_ARTIFICIAL (decl) = 1;
5758               TREE_NOTHROW (decl) = TREE_NOTHROW (alias_decl);
5759               TREE_USED (decl) = 1;
5760             }
5761
5762           weak_finish_1 (decl);
5763 # endif
5764         }
5765 #endif
5766
5767       {
5768         tree *p;
5769         tree t2;
5770
5771         /* Remove the alias and the target from the pending weak list
5772            so that we do not emit any .weak directives for the former,
5773            nor multiple .weak directives for the latter.  */
5774         for (p = &weak_decls; (t2 = *p) ; )
5775           {
5776             if (TREE_VALUE (t2) == alias_decl
5777                 || target == DECL_ASSEMBLER_NAME (TREE_VALUE (t2)))
5778               *p = TREE_CHAIN (t2);
5779             else
5780               p = &TREE_CHAIN (t2);
5781           }
5782
5783         /* Remove other weakrefs to the same target, to speed things up.  */
5784         for (p = &TREE_CHAIN (t); (t2 = *p) ; )
5785           {
5786             if (target == ultimate_transparent_alias_target (&TREE_VALUE (t2)))
5787               *p = TREE_CHAIN (t2);
5788             else
5789               p = &TREE_CHAIN (t2);
5790           }
5791       }
5792     }
5793
5794   for (t = weak_decls; t; t = TREE_CHAIN (t))
5795     {
5796       tree decl = TREE_VALUE (t);
5797
5798       weak_finish_1 (decl);
5799     }
5800 }
5801
5802 /* Emit the assembly bits to indicate that DECL is globally visible.  */
5803
5804 static void
5805 globalize_decl (tree decl)
5806 {
5807
5808 #if defined (ASM_WEAKEN_LABEL) || defined (ASM_WEAKEN_DECL)
5809   if (DECL_WEAK (decl))
5810     {
5811       const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
5812       tree *p, t;
5813
5814 #ifdef ASM_WEAKEN_DECL
5815       ASM_WEAKEN_DECL (asm_out_file, decl, name, 0);
5816 #else
5817       ASM_WEAKEN_LABEL (asm_out_file, name);
5818 #endif
5819
5820       /* Remove this function from the pending weak list so that
5821          we do not emit multiple .weak directives for it.  */
5822       for (p = &weak_decls; (t = *p) ; )
5823         {
5824           if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5825             *p = TREE_CHAIN (t);
5826           else
5827             p = &TREE_CHAIN (t);
5828         }
5829
5830       /* Remove weakrefs to the same target from the pending weakref
5831          list, for the same reason.  */
5832       for (p = &weakref_targets; (t = *p) ; )
5833         {
5834           if (DECL_ASSEMBLER_NAME (decl)
5835               == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5836             *p = TREE_CHAIN (t);
5837           else
5838             p = &TREE_CHAIN (t);
5839         }
5840
5841       return;
5842     }
5843 #endif
5844
5845   targetm.asm_out.globalize_decl_name (asm_out_file, decl);
5846 }
5847
5848 vec<alias_pair, va_gc> *alias_pairs;
5849
5850 /* Output the assembler code for a define (equate) using ASM_OUTPUT_DEF
5851    or ASM_OUTPUT_DEF_FROM_DECLS.  The function defines the symbol whose
5852    tree node is DECL to have the value of the tree node TARGET.  */
5853
5854 void
5855 do_assemble_alias (tree decl, tree target)
5856 {
5857   tree id;
5858
5859   /* Emulated TLS had better not get this var.  */
5860   gcc_assert (!(!targetm.have_tls
5861                 && VAR_P (decl)
5862                 && DECL_THREAD_LOCAL_P (decl)));
5863
5864   if (TREE_ASM_WRITTEN (decl))
5865     return;
5866
5867   id = DECL_ASSEMBLER_NAME (decl);
5868   ultimate_transparent_alias_target (&id);
5869   ultimate_transparent_alias_target (&target);
5870
5871   /* We must force creation of DECL_RTL for debug info generation, even though
5872      we don't use it here.  */
5873   make_decl_rtl (decl);
5874
5875   TREE_ASM_WRITTEN (decl) = 1;
5876   TREE_ASM_WRITTEN (DECL_ASSEMBLER_NAME (decl)) = 1;
5877   TREE_ASM_WRITTEN (id) = 1;
5878
5879   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5880     {
5881       if (!TREE_SYMBOL_REFERENCED (target))
5882         weakref_targets = tree_cons (decl, target, weakref_targets);
5883
5884 #ifdef ASM_OUTPUT_WEAKREF
5885       ASM_OUTPUT_WEAKREF (asm_out_file, decl,
5886                           IDENTIFIER_POINTER (id),
5887                           IDENTIFIER_POINTER (target));
5888 #else
5889       if (!TARGET_SUPPORTS_WEAK)
5890         {
5891           error_at (DECL_SOURCE_LOCATION (decl),
5892                     "weakref is not supported in this configuration");
5893           return;
5894         }
5895 #endif
5896       return;
5897     }
5898
5899 #ifdef ASM_OUTPUT_DEF
5900   tree orig_decl = decl;
5901
5902   /* Make name accessible from other files, if appropriate.  */
5903
5904   if (TREE_PUBLIC (decl) || TREE_PUBLIC (orig_decl))
5905     {
5906       globalize_decl (decl);
5907       maybe_assemble_visibility (decl);
5908     }
5909   if (TREE_CODE (decl) == FUNCTION_DECL
5910       && cgraph_node::get (decl)->ifunc_resolver)
5911     {
5912 #if defined (ASM_OUTPUT_TYPE_DIRECTIVE)
5913       if (targetm.has_ifunc_p ())
5914         ASM_OUTPUT_TYPE_DIRECTIVE
5915           (asm_out_file, IDENTIFIER_POINTER (id),
5916            IFUNC_ASM_TYPE);
5917       else
5918 #endif
5919         error_at (DECL_SOURCE_LOCATION (decl),
5920                   "%qs is not supported on this target", "ifunc");
5921     }
5922
5923 # ifdef ASM_OUTPUT_DEF_FROM_DECLS
5924   ASM_OUTPUT_DEF_FROM_DECLS (asm_out_file, decl, target);
5925 # else
5926   ASM_OUTPUT_DEF (asm_out_file,
5927                   IDENTIFIER_POINTER (id),
5928                   IDENTIFIER_POINTER (target));
5929 # endif
5930 #elif defined (ASM_OUTPUT_WEAK_ALIAS) || defined (ASM_WEAKEN_DECL)
5931   {
5932     const char *name;
5933     tree *p, t;
5934
5935     name = IDENTIFIER_POINTER (id);
5936 # ifdef ASM_WEAKEN_DECL
5937     ASM_WEAKEN_DECL (asm_out_file, decl, name, IDENTIFIER_POINTER (target));
5938 # else
5939     ASM_OUTPUT_WEAK_ALIAS (asm_out_file, name, IDENTIFIER_POINTER (target));
5940 # endif
5941     /* Remove this function from the pending weak list so that
5942        we do not emit multiple .weak directives for it.  */
5943     for (p = &weak_decls; (t = *p) ; )
5944       if (DECL_ASSEMBLER_NAME (decl) == DECL_ASSEMBLER_NAME (TREE_VALUE (t))
5945           || id == DECL_ASSEMBLER_NAME (TREE_VALUE (t)))
5946         *p = TREE_CHAIN (t);
5947       else
5948         p = &TREE_CHAIN (t);
5949
5950     /* Remove weakrefs to the same target from the pending weakref
5951        list, for the same reason.  */
5952     for (p = &weakref_targets; (t = *p) ; )
5953       {
5954         if (id == ultimate_transparent_alias_target (&TREE_VALUE (t)))
5955           *p = TREE_CHAIN (t);
5956         else
5957           p = &TREE_CHAIN (t);
5958       }
5959   }
5960 #endif
5961 }
5962
5963 /* Output .symver directive.  */
5964
5965 void
5966 do_assemble_symver (tree decl, tree target)
5967 {
5968   tree id = DECL_ASSEMBLER_NAME (decl);
5969   ultimate_transparent_alias_target (&id);
5970   ultimate_transparent_alias_target (&target);
5971 #ifdef ASM_OUTPUT_SYMVER_DIRECTIVE
5972   ASM_OUTPUT_SYMVER_DIRECTIVE (asm_out_file,
5973                                IDENTIFIER_POINTER (target),
5974                                IDENTIFIER_POINTER (id));
5975 #else
5976   error ("symver is only supported on ELF platforms");
5977 #endif
5978 }
5979
5980 /* Emit an assembler directive to make the symbol for DECL an alias to
5981    the symbol for TARGET.  */
5982
5983 void
5984 assemble_alias (tree decl, tree target)
5985 {
5986   tree target_decl;
5987
5988   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
5989     {
5990       tree alias = DECL_ASSEMBLER_NAME (decl);
5991
5992       ultimate_transparent_alias_target (&target);
5993
5994       if (alias == target)
5995         error ("%qs symbol %q+D ultimately targets itself", "weakref", decl);
5996       if (TREE_PUBLIC (decl))
5997         error ("%qs symbol %q+D must have static linkage", "weakref", decl);
5998     }
5999   else
6000     {
6001 #if !defined (ASM_OUTPUT_DEF)
6002 # if !defined(ASM_OUTPUT_WEAK_ALIAS) && !defined (ASM_WEAKEN_DECL)
6003       error_at (DECL_SOURCE_LOCATION (decl),
6004                 "alias definitions not supported in this configuration");
6005       TREE_ASM_WRITTEN (decl) = 1;
6006       return;
6007 # else
6008       if (!DECL_WEAK (decl))
6009         {
6010           /* NB: ifunc_resolver isn't set when an error is detected.  */
6011           if (TREE_CODE (decl) == FUNCTION_DECL
6012               && lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl)))
6013             error_at (DECL_SOURCE_LOCATION (decl),
6014                       "%qs is not supported in this configuration", "ifunc");
6015           else
6016             error_at (DECL_SOURCE_LOCATION (decl),
6017                       "only weak aliases are supported in this configuration");
6018           TREE_ASM_WRITTEN (decl) = 1;
6019           return;
6020         }
6021 # endif
6022 #endif
6023     }
6024   TREE_USED (decl) = 1;
6025
6026   /* Allow aliases to aliases.  */
6027   if (TREE_CODE (decl) == FUNCTION_DECL)
6028     cgraph_node::get_create (decl)->alias = true;
6029   else
6030     varpool_node::get_create (decl)->alias = true;
6031
6032   /* If the target has already been emitted, we don't have to queue the
6033      alias.  This saves a tad of memory.  */
6034   if (symtab->global_info_ready)
6035     target_decl = find_decl (target);
6036   else
6037     target_decl= NULL;
6038   if ((target_decl && TREE_ASM_WRITTEN (target_decl))
6039       || symtab->state >= EXPANSION)
6040     do_assemble_alias (decl, target);
6041   else
6042     {
6043       alias_pair p = {decl, target};
6044       vec_safe_push (alias_pairs, p);
6045     }
6046 }
6047
6048 /* Record and output a table of translations from original function
6049    to its transaction aware clone.  Note that tm_pure functions are
6050    considered to be their own clone.  */
6051
6052 struct tm_clone_hasher : ggc_cache_ptr_hash<tree_map>
6053 {
6054   static hashval_t hash (tree_map *m) { return tree_map_hash (m); }
6055   static bool equal (tree_map *a, tree_map *b) { return tree_map_eq (a, b); }
6056
6057   static int
6058   keep_cache_entry (tree_map *&e)
6059   {
6060     return ggc_marked_p (e->base.from);
6061   }
6062 };
6063
6064 static GTY((cache)) hash_table<tm_clone_hasher> *tm_clone_hash;
6065
6066 void
6067 record_tm_clone_pair (tree o, tree n)
6068 {
6069   struct tree_map **slot, *h;
6070
6071   if (tm_clone_hash == NULL)
6072     tm_clone_hash = hash_table<tm_clone_hasher>::create_ggc (32);
6073
6074   h = ggc_alloc<tree_map> ();
6075   h->hash = htab_hash_pointer (o);
6076   h->base.from = o;
6077   h->to = n;
6078
6079   slot = tm_clone_hash->find_slot_with_hash (h, h->hash, INSERT);
6080   *slot = h;
6081 }
6082
6083 tree
6084 get_tm_clone_pair (tree o)
6085 {
6086   if (tm_clone_hash)
6087     {
6088       struct tree_map *h, in;
6089
6090       in.base.from = o;
6091       in.hash = htab_hash_pointer (o);
6092       h = tm_clone_hash->find_with_hash (&in, in.hash);
6093       if (h)
6094         return h->to;
6095     }
6096   return NULL_TREE;
6097 }
6098
6099 struct tm_alias_pair
6100 {
6101   unsigned int uid;
6102   tree from;
6103   tree to;
6104 };
6105
6106
6107 /* Dump the actual pairs to the .tm_clone_table section.  */
6108
6109 static void
6110 dump_tm_clone_pairs (vec<tm_alias_pair> tm_alias_pairs)
6111 {
6112   unsigned i;
6113   tm_alias_pair *p;
6114   bool switched = false;
6115
6116   FOR_EACH_VEC_ELT (tm_alias_pairs, i, p)
6117     {
6118       tree src = p->from;
6119       tree dst = p->to;
6120       struct cgraph_node *src_n = cgraph_node::get (src);
6121       struct cgraph_node *dst_n = cgraph_node::get (dst);
6122
6123       /* The function ipa_tm_create_version() marks the clone as needed if
6124          the original function was needed.  But we also mark the clone as
6125          needed if we ever called the clone indirectly through
6126          TM_GETTMCLONE.  If neither of these are true, we didn't generate
6127          a clone, and we didn't call it indirectly... no sense keeping it
6128          in the clone table.  */
6129       if (!dst_n || !dst_n->definition)
6130         continue;
6131
6132       /* This covers the case where we have optimized the original
6133          function away, and only access the transactional clone.  */
6134       if (!src_n || !src_n->definition)
6135         continue;
6136
6137       if (!switched)
6138         {
6139           switch_to_section (targetm.asm_out.tm_clone_table_section ());
6140           assemble_align (POINTER_SIZE);
6141           switched = true;
6142         }
6143
6144       assemble_integer (XEXP (DECL_RTL (src), 0),
6145                         POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6146       assemble_integer (XEXP (DECL_RTL (dst), 0),
6147                         POINTER_SIZE_UNITS, POINTER_SIZE, 1);
6148     }
6149 }
6150
6151 /* Provide a default for the tm_clone_table section.  */
6152
6153 section *
6154 default_clone_table_section (void)
6155 {
6156   return get_named_section (NULL, ".tm_clone_table", 3);
6157 }
6158
6159 /* Helper comparison function for qsorting by the DECL_UID stored in
6160    alias_pair->emitted_diags.  */
6161
6162 static int
6163 tm_alias_pair_cmp (const void *x, const void *y)
6164 {
6165   const tm_alias_pair *p1 = (const tm_alias_pair *) x;
6166   const tm_alias_pair *p2 = (const tm_alias_pair *) y;
6167   if (p1->uid < p2->uid)
6168     return -1;
6169   if (p1->uid > p2->uid)
6170     return 1;
6171   return 0;
6172 }
6173
6174 void
6175 finish_tm_clone_pairs (void)
6176 {
6177   vec<tm_alias_pair> tm_alias_pairs = vNULL;
6178
6179   if (tm_clone_hash == NULL)
6180     return;
6181
6182   /* We need a determenistic order for the .tm_clone_table, otherwise
6183      we will get bootstrap comparison failures, so dump the hash table
6184      to a vector, sort it, and dump the vector.  */
6185
6186   /* Dump the hashtable to a vector.  */
6187   tree_map *map;
6188   hash_table<tm_clone_hasher>::iterator iter;
6189   FOR_EACH_HASH_TABLE_ELEMENT (*tm_clone_hash, map, tree_map *, iter)
6190     {
6191       tm_alias_pair p = {DECL_UID (map->base.from), map->base.from, map->to};
6192       tm_alias_pairs.safe_push (p);
6193     }
6194   /* Sort it.  */
6195   tm_alias_pairs.qsort (tm_alias_pair_cmp);
6196
6197   /* Dump it.  */
6198   dump_tm_clone_pairs (tm_alias_pairs);
6199
6200   tm_clone_hash->empty ();
6201   tm_clone_hash = NULL;
6202   tm_alias_pairs.release ();
6203 }
6204
6205
6206 /* Emit an assembler directive to set symbol for DECL visibility to
6207    the visibility type VIS, which must not be VISIBILITY_DEFAULT.  */
6208
6209 void
6210 default_assemble_visibility (tree decl ATTRIBUTE_UNUSED,
6211                              int vis ATTRIBUTE_UNUSED)
6212 {
6213 #ifdef HAVE_GAS_HIDDEN
6214   static const char * const visibility_types[] = {
6215     NULL, "protected", "hidden", "internal"
6216   };
6217
6218   const char *name, *type;
6219   tree id;
6220
6221   id = DECL_ASSEMBLER_NAME (decl);
6222   ultimate_transparent_alias_target (&id);
6223   name = IDENTIFIER_POINTER (id);
6224
6225   type = visibility_types[vis];
6226
6227   fprintf (asm_out_file, "\t.%s\t", type);
6228   assemble_name (asm_out_file, name);
6229   fprintf (asm_out_file, "\n");
6230 #else
6231   if (!DECL_ARTIFICIAL (decl))
6232     warning (OPT_Wattributes, "visibility attribute not supported "
6233              "in this configuration; ignored");
6234 #endif
6235 }
6236
6237 /* A helper function to call assemble_visibility when needed for a decl.  */
6238
6239 int
6240 maybe_assemble_visibility (tree decl)
6241 {
6242   enum symbol_visibility vis = DECL_VISIBILITY (decl);
6243   if (vis != VISIBILITY_DEFAULT)
6244     {
6245       targetm.asm_out.assemble_visibility (decl, vis);
6246       return 1;
6247     }
6248   else
6249     return 0;
6250 }
6251
6252 /* Returns 1 if the target configuration supports defining public symbols
6253    so that one of them will be chosen at link time instead of generating a
6254    multiply-defined symbol error, whether through the use of weak symbols or
6255    a target-specific mechanism for having duplicates discarded.  */
6256
6257 int
6258 supports_one_only (void)
6259 {
6260   if (SUPPORTS_ONE_ONLY)
6261     return 1;
6262   return TARGET_SUPPORTS_WEAK;
6263 }
6264
6265 /* Set up DECL as a public symbol that can be defined in multiple
6266    translation units without generating a linker error.  */
6267
6268 void
6269 make_decl_one_only (tree decl, tree comdat_group)
6270 {
6271   struct symtab_node *symbol;
6272   gcc_assert (VAR_OR_FUNCTION_DECL_P (decl));
6273
6274   TREE_PUBLIC (decl) = 1;
6275
6276   if (VAR_P (decl))
6277     symbol = varpool_node::get_create (decl);
6278   else
6279     symbol = cgraph_node::get_create (decl);
6280
6281   if (SUPPORTS_ONE_ONLY)
6282     {
6283 #ifdef MAKE_DECL_ONE_ONLY
6284       MAKE_DECL_ONE_ONLY (decl);
6285 #endif
6286       symbol->set_comdat_group (comdat_group);
6287     }
6288   else if (VAR_P (decl)
6289            && (DECL_INITIAL (decl) == 0
6290                || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
6291     DECL_COMMON (decl) = 1;
6292   else
6293     {
6294       gcc_assert (TARGET_SUPPORTS_WEAK);
6295       DECL_WEAK (decl) = 1;
6296     }
6297 }
6298
6299 void
6300 init_varasm_once (void)
6301 {
6302   section_htab = hash_table<section_hasher>::create_ggc (31);
6303   object_block_htab = hash_table<object_block_hasher>::create_ggc (31);
6304   const_desc_htab = hash_table<tree_descriptor_hasher>::create_ggc (1009);
6305
6306   shared_constant_pool = create_constant_pool ();
6307
6308 #ifdef TEXT_SECTION_ASM_OP
6309   text_section = get_unnamed_section (SECTION_CODE, output_section_asm_op,
6310                                       TEXT_SECTION_ASM_OP);
6311 #endif
6312
6313 #ifdef DATA_SECTION_ASM_OP
6314   data_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6315                                       DATA_SECTION_ASM_OP);
6316 #endif
6317
6318 #ifdef SDATA_SECTION_ASM_OP
6319   sdata_section = get_unnamed_section (SECTION_WRITE, output_section_asm_op,
6320                                        SDATA_SECTION_ASM_OP);
6321 #endif
6322
6323 #ifdef READONLY_DATA_SECTION_ASM_OP
6324   readonly_data_section = get_unnamed_section (0, output_section_asm_op,
6325                                                READONLY_DATA_SECTION_ASM_OP);
6326 #endif
6327
6328 #ifdef CTORS_SECTION_ASM_OP
6329   ctors_section = get_unnamed_section (0, output_section_asm_op,
6330                                        CTORS_SECTION_ASM_OP);
6331 #endif
6332
6333 #ifdef DTORS_SECTION_ASM_OP
6334   dtors_section = get_unnamed_section (0, output_section_asm_op,
6335                                        DTORS_SECTION_ASM_OP);
6336 #endif
6337
6338 #ifdef BSS_SECTION_ASM_OP
6339   bss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6340                                      output_section_asm_op,
6341                                      BSS_SECTION_ASM_OP);
6342 #endif
6343
6344 #ifdef SBSS_SECTION_ASM_OP
6345   sbss_section = get_unnamed_section (SECTION_WRITE | SECTION_BSS,
6346                                       output_section_asm_op,
6347                                       SBSS_SECTION_ASM_OP);
6348 #endif
6349
6350   tls_comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6351                                            | SECTION_COMMON, emit_tls_common);
6352   lcomm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6353                                         | SECTION_COMMON, emit_local);
6354   comm_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS
6355                                        | SECTION_COMMON, emit_common);
6356
6357 #if defined ASM_OUTPUT_ALIGNED_BSS
6358   bss_noswitch_section = get_noswitch_section (SECTION_WRITE | SECTION_BSS,
6359                                                emit_bss);
6360 #endif
6361
6362   targetm.asm_out.init_sections ();
6363
6364   if (readonly_data_section == NULL)
6365     readonly_data_section = text_section;
6366
6367 #ifdef ASM_OUTPUT_EXTERNAL
6368   pending_assemble_externals_set = new hash_set<tree>;
6369 #endif
6370 }
6371
6372 enum tls_model
6373 decl_default_tls_model (const_tree decl)
6374 {
6375   enum tls_model kind;
6376   bool is_local;
6377
6378   is_local = targetm.binds_local_p (decl);
6379   if (!flag_shlib)
6380     {
6381       if (is_local)
6382         kind = TLS_MODEL_LOCAL_EXEC;
6383       else
6384         kind = TLS_MODEL_INITIAL_EXEC;
6385     }
6386
6387   /* Local dynamic is inefficient when we're not combining the
6388      parts of the address.  */
6389   else if (optimize && is_local)
6390     kind = TLS_MODEL_LOCAL_DYNAMIC;
6391   else
6392     kind = TLS_MODEL_GLOBAL_DYNAMIC;
6393   if (kind < flag_tls_default)
6394     kind = flag_tls_default;
6395
6396   return kind;
6397 }
6398
6399 /* Select a set of attributes for section NAME based on the properties
6400    of DECL and whether or not RELOC indicates that DECL's initializer
6401    might contain runtime relocations.
6402
6403    We make the section read-only and executable for a function decl,
6404    read-only for a const data decl, and writable for a non-const data decl.  */
6405
6406 unsigned int
6407 default_section_type_flags (tree decl, const char *name, int reloc)
6408 {
6409   unsigned int flags;
6410
6411   if (decl && TREE_CODE (decl) == FUNCTION_DECL)
6412     flags = SECTION_CODE;
6413   else if (decl)
6414     {
6415       enum section_category category
6416         = categorize_decl_for_section (decl, reloc);
6417       if (decl_readonly_section_1 (category))
6418         flags = 0;
6419       else if (category == SECCAT_DATA_REL_RO
6420                || category == SECCAT_DATA_REL_RO_LOCAL)
6421         flags = SECTION_WRITE | SECTION_RELRO;
6422       else
6423         flags = SECTION_WRITE;
6424     }
6425   else
6426     {
6427       flags = SECTION_WRITE;
6428       if (strcmp (name, ".data.rel.ro") == 0
6429           || strcmp (name, ".data.rel.ro.local") == 0)
6430         flags |= SECTION_RELRO;
6431     }
6432
6433   if (decl && DECL_P (decl) && DECL_COMDAT_GROUP (decl))
6434     flags |= SECTION_LINKONCE;
6435
6436   if (strcmp (name, ".vtable_map_vars") == 0)
6437     flags |= SECTION_LINKONCE;
6438
6439   if (decl && VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6440     flags |= SECTION_TLS | SECTION_WRITE;
6441
6442   if (strcmp (name, ".bss") == 0
6443       || strncmp (name, ".bss.", 5) == 0
6444       || strncmp (name, ".gnu.linkonce.b.", 16) == 0
6445       || strcmp (name, ".persistent.bss") == 0
6446       || strcmp (name, ".sbss") == 0
6447       || strncmp (name, ".sbss.", 6) == 0
6448       || strncmp (name, ".gnu.linkonce.sb.", 17) == 0)
6449     flags |= SECTION_BSS;
6450
6451   if (strcmp (name, ".tdata") == 0
6452       || strncmp (name, ".tdata.", 7) == 0
6453       || strncmp (name, ".gnu.linkonce.td.", 17) == 0)
6454     flags |= SECTION_TLS;
6455
6456   if (strcmp (name, ".tbss") == 0
6457       || strncmp (name, ".tbss.", 6) == 0
6458       || strncmp (name, ".gnu.linkonce.tb.", 17) == 0)
6459     flags |= SECTION_TLS | SECTION_BSS;
6460
6461   if (strcmp (name, ".noinit") == 0)
6462     flags |= SECTION_WRITE | SECTION_BSS | SECTION_NOTYPE;
6463
6464   /* Various sections have special ELF types that the assembler will
6465      assign by default based on the name.  They are neither SHT_PROGBITS
6466      nor SHT_NOBITS, so when changing sections we don't want to print a
6467      section type (@progbits or @nobits).  Rather than duplicating the
6468      assembler's knowledge of what those special name patterns are, just
6469      let the assembler choose the type if we don't know a specific
6470      reason to set it to something other than the default.  SHT_PROGBITS
6471      is the default for sections whose name is not specially known to
6472      the assembler, so it does no harm to leave the choice to the
6473      assembler when @progbits is the best thing we know to use.  If
6474      someone is silly enough to emit code or TLS variables to one of
6475      these sections, then don't handle them specially.
6476
6477      default_elf_asm_named_section (below) handles the BSS, TLS, ENTSIZE, and
6478      LINKONCE cases when NOTYPE is not set, so leave those to its logic.  */
6479   if (!(flags & (SECTION_CODE | SECTION_BSS | SECTION_TLS | SECTION_ENTSIZE))
6480       && !(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE)))
6481     flags |= SECTION_NOTYPE;
6482
6483   return flags;
6484 }
6485
6486 /* Return true if the target supports some form of global BSS,
6487    either through bss_noswitch_section, or by selecting a BSS
6488    section in TARGET_ASM_SELECT_SECTION.  */
6489
6490 bool
6491 have_global_bss_p (void)
6492 {
6493   return bss_noswitch_section || targetm.have_switchable_bss_sections;
6494 }
6495
6496 /* Output assembly to switch to section NAME with attribute FLAGS.
6497    Four variants for common object file formats.  */
6498
6499 void
6500 default_no_named_section (const char *name ATTRIBUTE_UNUSED,
6501                           unsigned int flags ATTRIBUTE_UNUSED,
6502                           tree decl ATTRIBUTE_UNUSED)
6503 {
6504   /* Some object formats don't support named sections at all.  The
6505      front-end should already have flagged this as an error.  */
6506   gcc_unreachable ();
6507 }
6508
6509 #ifndef TLS_SECTION_ASM_FLAG
6510 #define TLS_SECTION_ASM_FLAG 'T'
6511 #endif
6512
6513 void
6514 default_elf_asm_named_section (const char *name, unsigned int flags,
6515                                tree decl)
6516 {
6517   char flagchars[11], *f = flagchars;
6518   unsigned int numeric_value = 0;
6519
6520   /* If we have already declared this section, we can use an
6521      abbreviated form to switch back to it -- unless this section is
6522      part of a COMDAT groups, in which case GAS requires the full
6523      declaration every time.  */
6524   if (!(HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6525       && (flags & SECTION_DECLARED))
6526     {
6527       fprintf (asm_out_file, "\t.section\t%s\n", name);
6528       return;
6529     }
6530
6531   /* If we have a machine specific flag, then use the numeric value to pass
6532      this on to GAS.  */
6533   if (targetm.asm_out.elf_flags_numeric (flags, &numeric_value))
6534       snprintf (f, sizeof (flagchars), "0x%08x", numeric_value);
6535   else
6536     {
6537       if (!(flags & SECTION_DEBUG))
6538         *f++ = 'a';
6539 #if HAVE_GAS_SECTION_EXCLUDE
6540       if (flags & SECTION_EXCLUDE)
6541         *f++ = 'e';
6542 #endif
6543       if (flags & SECTION_WRITE)
6544         *f++ = 'w';
6545       if (flags & SECTION_CODE)
6546         *f++ = 'x';
6547       if (flags & SECTION_SMALL)
6548         *f++ = 's';
6549       if (flags & SECTION_MERGE)
6550         *f++ = 'M';
6551       if (flags & SECTION_STRINGS)
6552         *f++ = 'S';
6553       if (flags & SECTION_TLS)
6554         *f++ = TLS_SECTION_ASM_FLAG;
6555       if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6556         *f++ = 'G';
6557 #ifdef MACH_DEP_SECTION_ASM_FLAG
6558       if (flags & SECTION_MACH_DEP)
6559         *f++ = MACH_DEP_SECTION_ASM_FLAG;
6560 #endif
6561       *f = '\0';
6562     }
6563
6564   fprintf (asm_out_file, "\t.section\t%s,\"%s\"", name, flagchars);
6565
6566   /* default_section_type_flags (above) knows which flags need special
6567      handling here, and sets NOTYPE when none of these apply so that the
6568      assembler's logic for default types can apply to user-chosen
6569      section names.  */
6570   if (!(flags & SECTION_NOTYPE))
6571     {
6572       const char *type;
6573       const char *format;
6574
6575       if (flags & SECTION_BSS)
6576         type = "nobits";
6577       else
6578         type = "progbits";
6579
6580       format = ",@%s";
6581       /* On platforms that use "@" as the assembly comment character,
6582          use "%" instead.  */
6583       if (strcmp (ASM_COMMENT_START, "@") == 0)
6584         format = ",%%%s";
6585       fprintf (asm_out_file, format, type);
6586
6587       if (flags & SECTION_ENTSIZE)
6588         fprintf (asm_out_file, ",%d", flags & SECTION_ENTSIZE);
6589       if (HAVE_COMDAT_GROUP && (flags & SECTION_LINKONCE))
6590         {
6591           if (TREE_CODE (decl) == IDENTIFIER_NODE)
6592             fprintf (asm_out_file, ",%s,comdat", IDENTIFIER_POINTER (decl));
6593           else
6594             fprintf (asm_out_file, ",%s,comdat",
6595                      IDENTIFIER_POINTER (DECL_COMDAT_GROUP (decl)));
6596         }
6597     }
6598
6599   putc ('\n', asm_out_file);
6600 }
6601
6602 void
6603 default_coff_asm_named_section (const char *name, unsigned int flags,
6604                                 tree decl ATTRIBUTE_UNUSED)
6605 {
6606   char flagchars[8], *f = flagchars;
6607
6608   if (flags & SECTION_WRITE)
6609     *f++ = 'w';
6610   if (flags & SECTION_CODE)
6611     *f++ = 'x';
6612   *f = '\0';
6613
6614   fprintf (asm_out_file, "\t.section\t%s,\"%s\"\n", name, flagchars);
6615 }
6616
6617 void
6618 default_pe_asm_named_section (const char *name, unsigned int flags,
6619                               tree decl)
6620 {
6621   default_coff_asm_named_section (name, flags, decl);
6622
6623   if (flags & SECTION_LINKONCE)
6624     {
6625       /* Functions may have been compiled at various levels of
6626          optimization so we can't use `same_size' here.
6627          Instead, have the linker pick one.  */
6628       fprintf (asm_out_file, "\t.linkonce %s\n",
6629                (flags & SECTION_CODE ? "discard" : "same_size"));
6630     }
6631 }
6632 \f
6633 /* The lame default section selector.  */
6634
6635 section *
6636 default_select_section (tree decl, int reloc,
6637                         unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6638 {
6639   if (DECL_P (decl))
6640     {
6641       if (decl_readonly_section (decl, reloc))
6642         return readonly_data_section;
6643     }
6644   else if (TREE_CODE (decl) == CONSTRUCTOR)
6645     {
6646       if (! ((flag_pic && reloc)
6647              || !TREE_READONLY (decl)
6648              || TREE_SIDE_EFFECTS (decl)
6649              || !TREE_CONSTANT (decl)))
6650         return readonly_data_section;
6651     }
6652   else if (TREE_CODE (decl) == STRING_CST)
6653     return readonly_data_section;
6654   else if (! (flag_pic && reloc))
6655     return readonly_data_section;
6656
6657   return data_section;
6658 }
6659
6660 enum section_category
6661 categorize_decl_for_section (const_tree decl, int reloc)
6662 {
6663   enum section_category ret;
6664
6665   if (TREE_CODE (decl) == FUNCTION_DECL)
6666     return SECCAT_TEXT;
6667   else if (TREE_CODE (decl) == STRING_CST)
6668     {
6669       if ((flag_sanitize & SANITIZE_ADDRESS)
6670           && asan_protect_global (CONST_CAST_TREE (decl)))
6671       /* or !flag_merge_constants */
6672         return SECCAT_RODATA;
6673       else
6674         return SECCAT_RODATA_MERGE_STR;
6675     }
6676   else if (VAR_P (decl))
6677     {
6678       tree d = CONST_CAST_TREE (decl);
6679       if (bss_initializer_p (decl))
6680         ret = SECCAT_BSS;
6681       else if (! TREE_READONLY (decl)
6682                || TREE_SIDE_EFFECTS (decl)
6683                || (DECL_INITIAL (decl)
6684                    && ! TREE_CONSTANT (DECL_INITIAL (decl))))
6685         {
6686           /* Here the reloc_rw_mask is not testing whether the section should
6687              be read-only or not, but whether the dynamic link will have to
6688              do something.  If so, we wish to segregate the data in order to
6689              minimize cache misses inside the dynamic linker.  */
6690           if (reloc & targetm.asm_out.reloc_rw_mask ())
6691             ret = reloc == 1 ? SECCAT_DATA_REL_LOCAL : SECCAT_DATA_REL;
6692           else
6693             ret = SECCAT_DATA;
6694         }
6695       else if (reloc & targetm.asm_out.reloc_rw_mask ())
6696         ret = reloc == 1 ? SECCAT_DATA_REL_RO_LOCAL : SECCAT_DATA_REL_RO;
6697       else if (reloc || flag_merge_constants < 2
6698                || ((flag_sanitize & SANITIZE_ADDRESS)
6699                    /* PR 81697: for architectures that use section anchors we
6700                       need to ignore DECL_RTL_SET_P (decl) for string constants
6701                       inside this asan_protect_global call because otherwise
6702                       we'll wrongly put them into SECCAT_RODATA_MERGE_CONST
6703                       section, set DECL_RTL (decl) later on and add DECL to
6704                       protected globals via successive asan_protect_global
6705                       calls.  In this scenario we'll end up with wrong
6706                       alignment of these strings at runtime and possible ASan
6707                       false positives.  */
6708                    && asan_protect_global (d, use_object_blocks_p ()
6709                                               && use_blocks_for_decl_p (d))))
6710         /* C and C++ don't allow different variables to share the same
6711            location.  -fmerge-all-constants allows even that (at the
6712            expense of not conforming).  */
6713         ret = SECCAT_RODATA;
6714       else if (DECL_INITIAL (decl)
6715                && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST)
6716         ret = SECCAT_RODATA_MERGE_STR_INIT;
6717       else
6718         ret = SECCAT_RODATA_MERGE_CONST;
6719     }
6720   else if (TREE_CODE (decl) == CONSTRUCTOR)
6721     {
6722       if ((reloc & targetm.asm_out.reloc_rw_mask ())
6723           || TREE_SIDE_EFFECTS (decl)
6724           || ! TREE_CONSTANT (decl))
6725         ret = SECCAT_DATA;
6726       else
6727         ret = SECCAT_RODATA;
6728     }
6729   else
6730     ret = SECCAT_RODATA;
6731
6732   /* There are no read-only thread-local sections.  */
6733   if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
6734     {
6735       /* Note that this would be *just* SECCAT_BSS, except that there's
6736          no concept of a read-only thread-local-data section.  */
6737       if (ret == SECCAT_BSS
6738           || DECL_INITIAL (decl) == NULL
6739           || (flag_zero_initialized_in_bss
6740               && initializer_zerop (DECL_INITIAL (decl))))
6741         ret = SECCAT_TBSS;
6742       else
6743         ret = SECCAT_TDATA;
6744     }
6745
6746   /* If the target uses small data sections, select it.  */
6747   else if (targetm.in_small_data_p (decl))
6748     {
6749       if (ret == SECCAT_BSS)
6750         ret = SECCAT_SBSS;
6751       else if (targetm.have_srodata_section && ret == SECCAT_RODATA)
6752         ret = SECCAT_SRODATA;
6753       else
6754         ret = SECCAT_SDATA;
6755     }
6756
6757   return ret;
6758 }
6759
6760 static bool
6761 decl_readonly_section_1 (enum section_category category)
6762 {
6763   switch (category)
6764     {
6765     case SECCAT_RODATA:
6766     case SECCAT_RODATA_MERGE_STR:
6767     case SECCAT_RODATA_MERGE_STR_INIT:
6768     case SECCAT_RODATA_MERGE_CONST:
6769     case SECCAT_SRODATA:
6770       return true;
6771     default:
6772       return false;
6773     }
6774 }
6775
6776 bool
6777 decl_readonly_section (const_tree decl, int reloc)
6778 {
6779   return decl_readonly_section_1 (categorize_decl_for_section (decl, reloc));
6780 }
6781
6782 /* Select a section based on the above categorization.  */
6783
6784 section *
6785 default_elf_select_section (tree decl, int reloc,
6786                             unsigned HOST_WIDE_INT align)
6787 {
6788   const char *sname;
6789
6790   switch (categorize_decl_for_section (decl, reloc))
6791     {
6792     case SECCAT_TEXT:
6793       /* We're not supposed to be called on FUNCTION_DECLs.  */
6794       gcc_unreachable ();
6795     case SECCAT_RODATA:
6796       return readonly_data_section;
6797     case SECCAT_RODATA_MERGE_STR:
6798       return mergeable_string_section (decl, align, 0);
6799     case SECCAT_RODATA_MERGE_STR_INIT:
6800       return mergeable_string_section (DECL_INITIAL (decl), align, 0);
6801     case SECCAT_RODATA_MERGE_CONST:
6802       return mergeable_constant_section (DECL_MODE (decl), align, 0);
6803     case SECCAT_SRODATA:
6804       sname = ".sdata2";
6805       break;
6806     case SECCAT_DATA:
6807       return data_section;
6808     case SECCAT_DATA_REL:
6809       sname = ".data.rel";
6810       break;
6811     case SECCAT_DATA_REL_LOCAL:
6812       sname = ".data.rel.local";
6813       break;
6814     case SECCAT_DATA_REL_RO:
6815       sname = ".data.rel.ro";
6816       break;
6817     case SECCAT_DATA_REL_RO_LOCAL:
6818       sname = ".data.rel.ro.local";
6819       break;
6820     case SECCAT_SDATA:
6821       sname = ".sdata";
6822       break;
6823     case SECCAT_TDATA:
6824       sname = ".tdata";
6825       break;
6826     case SECCAT_BSS:
6827       if (DECL_P (decl)
6828           && lookup_attribute ("noinit", DECL_ATTRIBUTES (decl)) != NULL_TREE)
6829         {
6830           sname = ".noinit";
6831           break;
6832         }
6833
6834       if (bss_section)
6835         return bss_section;
6836       sname = ".bss";
6837       break;
6838     case SECCAT_SBSS:
6839       sname = ".sbss";
6840       break;
6841     case SECCAT_TBSS:
6842       sname = ".tbss";
6843       break;
6844     default:
6845       gcc_unreachable ();
6846     }
6847
6848   return get_named_section (decl, sname, reloc);
6849 }
6850
6851 /* Construct a unique section name based on the decl name and the
6852    categorization performed above.  */
6853
6854 void
6855 default_unique_section (tree decl, int reloc)
6856 {
6857   /* We only need to use .gnu.linkonce if we don't have COMDAT groups.  */
6858   bool one_only = DECL_ONE_ONLY (decl) && !HAVE_COMDAT_GROUP;
6859   const char *prefix, *name, *linkonce;
6860   char *string;
6861   tree id;
6862
6863   switch (categorize_decl_for_section (decl, reloc))
6864     {
6865     case SECCAT_TEXT:
6866       prefix = one_only ? ".t" : ".text";
6867       break;
6868     case SECCAT_RODATA:
6869     case SECCAT_RODATA_MERGE_STR:
6870     case SECCAT_RODATA_MERGE_STR_INIT:
6871     case SECCAT_RODATA_MERGE_CONST:
6872       prefix = one_only ? ".r" : ".rodata";
6873       break;
6874     case SECCAT_SRODATA:
6875       prefix = one_only ? ".s2" : ".sdata2";
6876       break;
6877     case SECCAT_DATA:
6878       prefix = one_only ? ".d" : ".data";
6879       break;
6880     case SECCAT_DATA_REL:
6881       prefix = one_only ? ".d.rel" : ".data.rel";
6882       break;
6883     case SECCAT_DATA_REL_LOCAL:
6884       prefix = one_only ? ".d.rel.local" : ".data.rel.local";
6885       break;
6886     case SECCAT_DATA_REL_RO:
6887       prefix = one_only ? ".d.rel.ro" : ".data.rel.ro";
6888       break;
6889     case SECCAT_DATA_REL_RO_LOCAL:
6890       prefix = one_only ? ".d.rel.ro.local" : ".data.rel.ro.local";
6891       break;
6892     case SECCAT_SDATA:
6893       prefix = one_only ? ".s" : ".sdata";
6894       break;
6895     case SECCAT_BSS:
6896       prefix = one_only ? ".b" : ".bss";
6897       break;
6898     case SECCAT_SBSS:
6899       prefix = one_only ? ".sb" : ".sbss";
6900       break;
6901     case SECCAT_TDATA:
6902       prefix = one_only ? ".td" : ".tdata";
6903       break;
6904     case SECCAT_TBSS:
6905       prefix = one_only ? ".tb" : ".tbss";
6906       break;
6907     default:
6908       gcc_unreachable ();
6909     }
6910
6911   id = DECL_ASSEMBLER_NAME (decl);
6912   ultimate_transparent_alias_target (&id);
6913   name = IDENTIFIER_POINTER (id);
6914   name = targetm.strip_name_encoding (name);
6915
6916   /* If we're using one_only, then there needs to be a .gnu.linkonce
6917      prefix to the section name.  */
6918   linkonce = one_only ? ".gnu.linkonce" : "";
6919
6920   string = ACONCAT ((linkonce, prefix, ".", name, NULL));
6921
6922   set_decl_section_name (decl, string);
6923 }
6924
6925 /* Subroutine of compute_reloc_for_rtx for leaf rtxes.  */
6926
6927 static int
6928 compute_reloc_for_rtx_1 (const_rtx x)
6929 {
6930   switch (GET_CODE (x))
6931     {
6932     case SYMBOL_REF:
6933       return SYMBOL_REF_LOCAL_P (x) ? 1 : 2;
6934     case LABEL_REF:
6935       return 1;
6936     default:
6937       return 0;
6938     }
6939 }
6940
6941 /* Like compute_reloc_for_constant, except for an RTX.  The return value
6942    is a mask for which bit 1 indicates a global relocation, and bit 0
6943    indicates a local relocation.  */
6944
6945 static int
6946 compute_reloc_for_rtx (const_rtx x)
6947 {
6948   switch (GET_CODE (x))
6949     {
6950     case SYMBOL_REF:
6951     case LABEL_REF:
6952       return compute_reloc_for_rtx_1 (x);
6953
6954     case CONST:
6955       {
6956         int reloc = 0;
6957         subrtx_iterator::array_type array;
6958         FOR_EACH_SUBRTX (iter, array, x, ALL)
6959           reloc |= compute_reloc_for_rtx_1 (*iter);
6960         return reloc;
6961       }
6962
6963     default:
6964       return 0;
6965     }
6966 }
6967
6968 section *
6969 default_select_rtx_section (machine_mode mode ATTRIBUTE_UNUSED,
6970                             rtx x,
6971                             unsigned HOST_WIDE_INT align ATTRIBUTE_UNUSED)
6972 {
6973   if (compute_reloc_for_rtx (x) & targetm.asm_out.reloc_rw_mask ())
6974     return data_section;
6975   else
6976     return readonly_data_section;
6977 }
6978
6979 section *
6980 default_elf_select_rtx_section (machine_mode mode, rtx x,
6981                                 unsigned HOST_WIDE_INT align)
6982 {
6983   int reloc = compute_reloc_for_rtx (x);
6984
6985   /* ??? Handle small data here somehow.  */
6986
6987   if (reloc & targetm.asm_out.reloc_rw_mask ())
6988     {
6989       if (reloc == 1)
6990         return get_named_section (NULL, ".data.rel.ro.local", 1);
6991       else
6992         return get_named_section (NULL, ".data.rel.ro", 3);
6993     }
6994
6995   return mergeable_constant_section (mode, align, 0);
6996 }
6997
6998 /* Set the generally applicable flags on the SYMBOL_REF for EXP.  */
6999
7000 void
7001 default_encode_section_info (tree decl, rtx rtl, int first ATTRIBUTE_UNUSED)
7002 {
7003   rtx symbol;
7004   int flags;
7005
7006   /* Careful not to prod global register variables.  */
7007   if (!MEM_P (rtl))
7008     return;
7009   symbol = XEXP (rtl, 0);
7010   if (GET_CODE (symbol) != SYMBOL_REF)
7011     return;
7012
7013   flags = SYMBOL_REF_FLAGS (symbol) & SYMBOL_FLAG_HAS_BLOCK_INFO;
7014   if (TREE_CODE (decl) == FUNCTION_DECL)
7015     flags |= SYMBOL_FLAG_FUNCTION;
7016   if (targetm.binds_local_p (decl))
7017     flags |= SYMBOL_FLAG_LOCAL;
7018   if (VAR_P (decl) && DECL_THREAD_LOCAL_P (decl))
7019     flags |= DECL_TLS_MODEL (decl) << SYMBOL_FLAG_TLS_SHIFT;
7020   else if (targetm.in_small_data_p (decl))
7021     flags |= SYMBOL_FLAG_SMALL;
7022   /* ??? Why is DECL_EXTERNAL ever set for non-PUBLIC names?  Without
7023      being PUBLIC, the thing *must* be defined in this translation unit.
7024      Prevent this buglet from being propagated into rtl code as well.  */
7025   if (DECL_P (decl) && DECL_EXTERNAL (decl) && TREE_PUBLIC (decl))
7026     flags |= SYMBOL_FLAG_EXTERNAL;
7027
7028   SYMBOL_REF_FLAGS (symbol) = flags;
7029 }
7030
7031 /* By default, we do nothing for encode_section_info, so we need not
7032    do anything but discard the '*' marker.  */
7033
7034 const char *
7035 default_strip_name_encoding (const char *str)
7036 {
7037   return str + (*str == '*');
7038 }
7039
7040 #ifdef ASM_OUTPUT_DEF
7041 /* The default implementation of TARGET_ASM_OUTPUT_ANCHOR.  Define the
7042    anchor relative to ".", the current section position.  */
7043
7044 void
7045 default_asm_output_anchor (rtx symbol)
7046 {
7047   char buffer[100];
7048
7049   sprintf (buffer, "*. + " HOST_WIDE_INT_PRINT_DEC,
7050            SYMBOL_REF_BLOCK_OFFSET (symbol));
7051   ASM_OUTPUT_DEF (asm_out_file, XSTR (symbol, 0), buffer);
7052 }
7053 #endif
7054
7055 /* The default implementation of TARGET_USE_ANCHORS_FOR_SYMBOL_P.  */
7056
7057 bool
7058 default_use_anchors_for_symbol_p (const_rtx symbol)
7059 {
7060   tree decl;
7061   section *sect = SYMBOL_REF_BLOCK (symbol)->sect;
7062
7063   /* This function should only be called with non-zero SYMBOL_REF_BLOCK,
7064      furthermore get_block_for_section should not create object blocks
7065      for mergeable sections.  */
7066   gcc_checking_assert (sect && !(sect->common.flags & SECTION_MERGE));
7067
7068   /* Don't use anchors for small data sections.  The small data register
7069      acts as an anchor for such sections.  */
7070   if (sect->common.flags & SECTION_SMALL)
7071     return false;
7072
7073   decl = SYMBOL_REF_DECL (symbol);
7074   if (decl && DECL_P (decl))
7075     {
7076       /* Don't use section anchors for decls that might be defined or
7077          usurped by other modules.  */
7078       if (TREE_PUBLIC (decl) && !decl_binds_to_current_def_p (decl))
7079         return false;
7080
7081       /* Don't use section anchors for decls that will be placed in a
7082          small data section.  */
7083       /* ??? Ideally, this check would be redundant with the SECTION_SMALL
7084          one above.  The problem is that we only use SECTION_SMALL for
7085          sections that should be marked as small in the section directive.  */
7086       if (targetm.in_small_data_p (decl))
7087         return false;
7088
7089       /* Don't use section anchors for decls that won't fit inside a single
7090          anchor range to reduce the amount of instructions required to refer
7091          to the entire declaration.  */
7092       if (DECL_SIZE_UNIT (decl) == NULL_TREE
7093           || !tree_fits_uhwi_p (DECL_SIZE_UNIT (decl))
7094           || (tree_to_uhwi (DECL_SIZE_UNIT (decl))
7095               >= (unsigned HOST_WIDE_INT) targetm.max_anchor_offset))
7096         return false;
7097
7098     }
7099   return true;
7100 }
7101
7102 /* Return true when RESOLUTION indicate that symbol will be bound to the
7103    definition provided by current .o file.  */
7104
7105 static bool
7106 resolution_to_local_definition_p (enum ld_plugin_symbol_resolution resolution)
7107 {
7108   return (resolution == LDPR_PREVAILING_DEF
7109           || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7110           || resolution == LDPR_PREVAILING_DEF_IRONLY);
7111 }
7112
7113 /* Return true when RESOLUTION indicate that symbol will be bound locally
7114    within current executable or DSO.  */
7115
7116 static bool
7117 resolution_local_p (enum ld_plugin_symbol_resolution resolution)
7118 {
7119   return (resolution == LDPR_PREVAILING_DEF
7120           || resolution == LDPR_PREVAILING_DEF_IRONLY
7121           || resolution == LDPR_PREVAILING_DEF_IRONLY_EXP
7122           || resolution == LDPR_PREEMPTED_REG
7123           || resolution == LDPR_PREEMPTED_IR
7124           || resolution == LDPR_RESOLVED_IR
7125           || resolution == LDPR_RESOLVED_EXEC);
7126 }
7127
7128 /* COMMON_LOCAL_P is true means that the linker can guarantee that an
7129    uninitialized common symbol in the executable will still be defined
7130    (through COPY relocation) in the executable.  */
7131
7132 bool
7133 default_binds_local_p_3 (const_tree exp, bool shlib, bool weak_dominate,
7134                          bool extern_protected_data, bool common_local_p)
7135 {
7136   /* A non-decl is an entry in the constant pool.  */
7137   if (!DECL_P (exp))
7138     return true;
7139
7140   /* Weakrefs may not bind locally, even though the weakref itself is always
7141      static and therefore local.  Similarly, the resolver for ifunc functions
7142      might resolve to a non-local function.
7143      FIXME: We can resolve the weakref case more curefuly by looking at the
7144      weakref alias.  */
7145   if (lookup_attribute ("weakref", DECL_ATTRIBUTES (exp))
7146       || (TREE_CODE (exp) == FUNCTION_DECL
7147           && cgraph_node::get (exp)
7148           && cgraph_node::get (exp)->ifunc_resolver))
7149     return false;
7150
7151   /* Static variables are always local.  */
7152   if (! TREE_PUBLIC (exp))
7153     return true;
7154
7155   /* With resolution file in hand, take look into resolutions.
7156      We can't just return true for resolved_locally symbols,
7157      because dynamic linking might overwrite symbols
7158      in shared libraries.  */
7159   bool resolved_locally = false;
7160
7161   bool uninited_common = (DECL_COMMON (exp)
7162                           && (DECL_INITIAL (exp) == NULL
7163                               || (!in_lto_p
7164                                   && DECL_INITIAL (exp) == error_mark_node)));
7165
7166   /* A non-external variable is defined locally only if it isn't
7167      uninitialized COMMON variable or common_local_p is true.  */
7168   bool defined_locally = (!DECL_EXTERNAL (exp)
7169                           && (!uninited_common || common_local_p));
7170   if (symtab_node *node = symtab_node::get (exp))
7171     {
7172       if (node->in_other_partition)
7173         defined_locally = true;
7174       if (node->can_be_discarded_p ())
7175         ;
7176       else if (resolution_to_local_definition_p (node->resolution))
7177         defined_locally = resolved_locally = true;
7178       else if (resolution_local_p (node->resolution))
7179         resolved_locally = true;
7180     }
7181   if (defined_locally && weak_dominate && !shlib)
7182     resolved_locally = true;
7183
7184   /* Undefined weak symbols are never defined locally.  */
7185   if (DECL_WEAK (exp) && !defined_locally)
7186     return false;
7187
7188   /* A symbol is local if the user has said explicitly that it will be,
7189      or if we have a definition for the symbol.  We cannot infer visibility
7190      for undefined symbols.  */
7191   if (DECL_VISIBILITY (exp) != VISIBILITY_DEFAULT
7192       && (TREE_CODE (exp) == FUNCTION_DECL
7193           || !extern_protected_data
7194           || DECL_VISIBILITY (exp) != VISIBILITY_PROTECTED)
7195       && (DECL_VISIBILITY_SPECIFIED (exp) || defined_locally))
7196     return true;
7197
7198   /* If PIC, then assume that any global name can be overridden by
7199      symbols resolved from other modules.  */
7200   if (shlib)
7201     return false;
7202
7203   /* Variables defined outside this object might not be local.  */
7204   if (DECL_EXTERNAL (exp) && !resolved_locally)
7205     return false;
7206
7207   /* Non-dominant weak symbols are not defined locally.  */
7208   if (DECL_WEAK (exp) && !resolved_locally)
7209     return false;
7210
7211   /* Uninitialized COMMON variable may be unified with symbols
7212      resolved from other modules.  */
7213   if (uninited_common && !resolved_locally)
7214     return false;
7215
7216   /* Otherwise we're left with initialized (or non-common) global data
7217      which is of necessity defined locally.  */
7218   return true;
7219 }
7220
7221 /* Assume ELF-ish defaults, since that's pretty much the most liberal
7222    wrt cross-module name binding.  */
7223
7224 bool
7225 default_binds_local_p (const_tree exp)
7226 {
7227   return default_binds_local_p_3 (exp, flag_shlib != 0, true, false, false);
7228 }
7229
7230 /* Similar to default_binds_local_p, but common symbol may be local and
7231    extern protected data is non-local.  */
7232
7233 bool
7234 default_binds_local_p_2 (const_tree exp)
7235 {
7236   return default_binds_local_p_3 (exp, flag_shlib != 0, true, true,
7237                                   !flag_pic);
7238 }
7239
7240 bool
7241 default_binds_local_p_1 (const_tree exp, int shlib)
7242 {
7243   return default_binds_local_p_3 (exp, shlib != 0, false, false, false);
7244 }
7245
7246 /* Return true when references to DECL must bind to current definition in
7247    final executable.
7248
7249    The condition is usually equivalent to whether the function binds to the
7250    current module (shared library or executable), that is to binds_local_p.
7251    We use this fact to avoid need for another target hook and implement
7252    the logic using binds_local_p and just special cases where
7253    decl_binds_to_current_def_p is stronger than binds_local_p.  In particular
7254    the weak definitions (that can be overwritten at linktime by other
7255    definition from different object file) and when resolution info is available
7256    we simply use the knowledge passed to us by linker plugin.  */
7257 bool
7258 decl_binds_to_current_def_p (const_tree decl)
7259 {
7260   gcc_assert (DECL_P (decl));
7261   if (!targetm.binds_local_p (decl))
7262     return false;
7263   if (!TREE_PUBLIC (decl))
7264     return true;
7265
7266   /* When resolution is available, just use it.  */
7267   if (symtab_node *node = symtab_node::get (decl))
7268     {
7269       if (node->resolution != LDPR_UNKNOWN
7270           && !node->can_be_discarded_p ())
7271         return resolution_to_local_definition_p (node->resolution);
7272     }
7273
7274   /* Otherwise we have to assume the worst for DECL_WEAK (hidden weaks
7275      binds locally but still can be overwritten), DECL_COMMON (can be merged
7276      with a non-common definition somewhere in the same module) or
7277      DECL_EXTERNAL.
7278      This rely on fact that binds_local_p behave as decl_replaceable_p
7279      for all other declaration types.  */
7280   if (DECL_WEAK (decl))
7281     return false;
7282   if (DECL_COMMON (decl)
7283       && (DECL_INITIAL (decl) == NULL
7284           || (!in_lto_p && DECL_INITIAL (decl) == error_mark_node)))
7285     return false;
7286   if (DECL_EXTERNAL (decl))
7287     return false;
7288   return true;
7289 }
7290
7291 /* A replaceable function or variable is one which may be replaced
7292    at link-time with an entirely different definition, provided that the
7293    replacement has the same type.  For example, functions declared
7294    with __attribute__((weak)) on most systems are replaceable.
7295
7296    COMDAT functions are not replaceable, since all definitions of the
7297    function must be equivalent.  It is important that COMDAT functions
7298    not be treated as replaceable so that use of C++ template
7299    instantiations is not penalized.  */
7300
7301 bool
7302 decl_replaceable_p (tree decl)
7303 {
7304   gcc_assert (DECL_P (decl));
7305   if (!TREE_PUBLIC (decl) || DECL_COMDAT (decl))
7306     return false;
7307   if (!flag_semantic_interposition
7308       && !DECL_WEAK (decl))
7309     return false;
7310   return !decl_binds_to_current_def_p (decl);
7311 }
7312
7313 /* Default function to output code that will globalize a label.  A
7314    target must define GLOBAL_ASM_OP or provide its own function to
7315    globalize a label.  */
7316 #ifdef GLOBAL_ASM_OP
7317 void
7318 default_globalize_label (FILE * stream, const char *name)
7319 {
7320   fputs (GLOBAL_ASM_OP, stream);
7321   assemble_name (stream, name);
7322   putc ('\n', stream);
7323 }
7324 #endif /* GLOBAL_ASM_OP */
7325
7326 /* Default function to output code that will globalize a declaration.  */
7327 void
7328 default_globalize_decl_name (FILE * stream, tree decl)
7329 {
7330   const char *name = XSTR (XEXP (DECL_RTL (decl), 0), 0);
7331   targetm.asm_out.globalize_label (stream, name);
7332 }
7333
7334 /* Default function to output a label for unwind information.  The
7335    default is to do nothing.  A target that needs nonlocal labels for
7336    unwind information must provide its own function to do this.  */
7337 void
7338 default_emit_unwind_label (FILE * stream ATTRIBUTE_UNUSED,
7339                            tree decl ATTRIBUTE_UNUSED,
7340                            int for_eh ATTRIBUTE_UNUSED,
7341                            int empty ATTRIBUTE_UNUSED)
7342 {
7343 }
7344
7345 /* Default function to output a label to divide up the exception table.
7346    The default is to do nothing.  A target that needs/wants to divide
7347    up the table must provide it's own function to do this.  */
7348 void
7349 default_emit_except_table_label (FILE * stream ATTRIBUTE_UNUSED)
7350 {
7351 }
7352
7353 /* This is how to output an internal numbered label where PREFIX is
7354    the class of label and LABELNO is the number within the class.  */
7355
7356 void
7357 default_generate_internal_label (char *buf, const char *prefix,
7358                                  unsigned long labelno)
7359 {
7360   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7361 }
7362
7363 /* This is how to output an internal numbered label where PREFIX is
7364    the class of label and LABELNO is the number within the class.  */
7365
7366 void
7367 default_internal_label (FILE *stream, const char *prefix,
7368                         unsigned long labelno)
7369 {
7370   char *const buf = (char *) alloca (40 + strlen (prefix));
7371   ASM_GENERATE_INTERNAL_LABEL (buf, prefix, labelno);
7372   ASM_OUTPUT_INTERNAL_LABEL (stream, buf);
7373 }
7374
7375
7376 /* The default implementation of ASM_DECLARE_CONSTANT_NAME.  */
7377
7378 void
7379 default_asm_declare_constant_name (FILE *file, const char *name,
7380                                    const_tree exp ATTRIBUTE_UNUSED,
7381                                    HOST_WIDE_INT size ATTRIBUTE_UNUSED)
7382 {
7383   assemble_label (file, name);
7384 }
7385
7386 /* This is the default behavior at the beginning of a file.  It's
7387    controlled by two other target-hook toggles.  */
7388 void
7389 default_file_start (void)
7390 {
7391   if (targetm.asm_file_start_app_off
7392       && !(flag_verbose_asm || flag_debug_asm || flag_dump_rtl_in_asm))
7393     fputs (ASM_APP_OFF, asm_out_file);
7394
7395   if (targetm.asm_file_start_file_directive)
7396     {
7397       /* LTO produced units have no meaningful main_input_filename.  */
7398       if (in_lto_p)
7399         output_file_directive (asm_out_file, "<artificial>");
7400       else
7401         output_file_directive (asm_out_file, main_input_filename);
7402     }
7403 }
7404
7405 /* This is a generic routine suitable for use as TARGET_ASM_FILE_END
7406    which emits a special section directive used to indicate whether or
7407    not this object file needs an executable stack.  This is primarily
7408    a GNU extension to ELF but could be used on other targets.  */
7409
7410 int trampolines_created;
7411
7412 void
7413 file_end_indicate_exec_stack (void)
7414 {
7415   unsigned int flags = SECTION_DEBUG;
7416   if (trampolines_created)
7417     flags |= SECTION_CODE;
7418
7419   switch_to_section (get_section (".note.GNU-stack", flags, NULL));
7420 }
7421
7422 /* Emit a special section directive to indicate that this object file
7423    was compiled with -fsplit-stack.  This is used to let the linker
7424    detect calls between split-stack code and non-split-stack code, so
7425    that it can modify the split-stack code to allocate a sufficiently
7426    large stack.  We emit another special section if there are any
7427    functions in this file which have the no_split_stack attribute, to
7428    prevent the linker from warning about being unable to convert the
7429    functions if they call non-split-stack code.  */
7430
7431 void
7432 file_end_indicate_split_stack (void)
7433 {
7434   if (flag_split_stack)
7435     {
7436       switch_to_section (get_section (".note.GNU-split-stack", SECTION_DEBUG,
7437                                       NULL));
7438       if (saw_no_split_stack)
7439         switch_to_section (get_section (".note.GNU-no-split-stack",
7440                                         SECTION_DEBUG, NULL));
7441     }
7442 }
7443
7444 /* Output DIRECTIVE (a C string) followed by a newline.  This is used as
7445    a get_unnamed_section callback.  */
7446
7447 void
7448 output_section_asm_op (const void *directive)
7449 {
7450   fprintf (asm_out_file, "%s\n", (const char *) directive);
7451 }
7452
7453 /* Emit assembly code to switch to section NEW_SECTION.  Do nothing if
7454    the current section is NEW_SECTION.  */
7455
7456 void
7457 switch_to_section (section *new_section)
7458 {
7459   if (in_section == new_section)
7460     return;
7461
7462   if (new_section->common.flags & SECTION_FORGET)
7463     in_section = NULL;
7464   else
7465     in_section = new_section;
7466
7467   switch (SECTION_STYLE (new_section))
7468     {
7469     case SECTION_NAMED:
7470       targetm.asm_out.named_section (new_section->named.name,
7471                                      new_section->named.common.flags,
7472                                      new_section->named.decl);
7473       break;
7474
7475     case SECTION_UNNAMED:
7476       new_section->unnamed.callback (new_section->unnamed.data);
7477       break;
7478
7479     case SECTION_NOSWITCH:
7480       gcc_unreachable ();
7481       break;
7482     }
7483
7484   new_section->common.flags |= SECTION_DECLARED;
7485 }
7486
7487 /* If block symbol SYMBOL has not yet been assigned an offset, place
7488    it at the end of its block.  */
7489
7490 void
7491 place_block_symbol (rtx symbol)
7492 {
7493   unsigned HOST_WIDE_INT size, mask, offset;
7494   class constant_descriptor_rtx *desc;
7495   unsigned int alignment;
7496   struct object_block *block;
7497   tree decl;
7498
7499   gcc_assert (SYMBOL_REF_BLOCK (symbol));
7500   if (SYMBOL_REF_BLOCK_OFFSET (symbol) >= 0)
7501     return;
7502
7503   /* Work out the symbol's size and alignment.  */
7504   if (CONSTANT_POOL_ADDRESS_P (symbol))
7505     {
7506       desc = SYMBOL_REF_CONSTANT (symbol);
7507       alignment = desc->align;
7508       size = GET_MODE_SIZE (desc->mode);
7509     }
7510   else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7511     {
7512       decl = SYMBOL_REF_DECL (symbol);
7513       gcc_checking_assert (DECL_IN_CONSTANT_POOL (decl));
7514       alignment = DECL_ALIGN (decl);
7515       size = get_constant_size (DECL_INITIAL (decl));
7516       if ((flag_sanitize & SANITIZE_ADDRESS)
7517           && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7518           && asan_protect_global (DECL_INITIAL (decl)))
7519         {
7520           size += asan_red_zone_size (size);
7521           alignment = MAX (alignment,
7522                            ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7523         }
7524     }
7525   else
7526     {
7527       struct symtab_node *snode;
7528       decl = SYMBOL_REF_DECL (symbol);
7529
7530       snode = symtab_node::get (decl);
7531       if (snode->alias)
7532         {
7533           rtx target = DECL_RTL (snode->ultimate_alias_target ()->decl);
7534
7535           gcc_assert (MEM_P (target)
7536                       && GET_CODE (XEXP (target, 0)) == SYMBOL_REF
7537                       && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (target, 0)));
7538           target = XEXP (target, 0);
7539           place_block_symbol (target);
7540           SYMBOL_REF_BLOCK_OFFSET (symbol) = SYMBOL_REF_BLOCK_OFFSET (target);
7541           return;
7542         }
7543       alignment = get_variable_align (decl);
7544       size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7545       if ((flag_sanitize & SANITIZE_ADDRESS)
7546           && asan_protect_global (decl))
7547         {
7548           size += asan_red_zone_size (size);
7549           alignment = MAX (alignment,
7550                            ASAN_RED_ZONE_SIZE * BITS_PER_UNIT);
7551         }
7552     }
7553
7554   /* Calculate the object's offset from the start of the block.  */
7555   block = SYMBOL_REF_BLOCK (symbol);
7556   mask = alignment / BITS_PER_UNIT - 1;
7557   offset = (block->size + mask) & ~mask;
7558   SYMBOL_REF_BLOCK_OFFSET (symbol) = offset;
7559
7560   /* Record the block's new alignment and size.  */
7561   block->alignment = MAX (block->alignment, alignment);
7562   block->size = offset + size;
7563
7564   vec_safe_push (block->objects, symbol);
7565 }
7566
7567 /* Return the anchor that should be used to address byte offset OFFSET
7568    from the first object in BLOCK.  MODEL is the TLS model used
7569    to access it.  */
7570
7571 rtx
7572 get_section_anchor (struct object_block *block, HOST_WIDE_INT offset,
7573                     enum tls_model model)
7574 {
7575   char label[100];
7576   unsigned int begin, middle, end;
7577   unsigned HOST_WIDE_INT min_offset, max_offset, range, bias, delta;
7578   rtx anchor;
7579
7580   /* Work out the anchor's offset.  Use an offset of 0 for the first
7581      anchor so that we don't pessimize the case where we take the address
7582      of a variable at the beginning of the block.  This is particularly
7583      useful when a block has only one variable assigned to it.
7584
7585      We try to place anchors RANGE bytes apart, so there can then be
7586      anchors at +/-RANGE, +/-2 * RANGE, and so on, up to the limits of
7587      a ptr_mode offset.  With some target settings, the lowest such
7588      anchor might be out of range for the lowest ptr_mode offset;
7589      likewise the highest anchor for the highest offset.  Use anchors
7590      at the extreme ends of the ptr_mode range in such cases.
7591
7592      All arithmetic uses unsigned integers in order to avoid
7593      signed overflow.  */
7594   max_offset = (unsigned HOST_WIDE_INT) targetm.max_anchor_offset;
7595   min_offset = (unsigned HOST_WIDE_INT) targetm.min_anchor_offset;
7596   range = max_offset - min_offset + 1;
7597   if (range == 0)
7598     offset = 0;
7599   else
7600     {
7601       bias = HOST_WIDE_INT_1U << (GET_MODE_BITSIZE (ptr_mode) - 1);
7602       if (offset < 0)
7603         {
7604           delta = -(unsigned HOST_WIDE_INT) offset + max_offset;
7605           delta -= delta % range;
7606           if (delta > bias)
7607             delta = bias;
7608           offset = (HOST_WIDE_INT) (-delta);
7609         }
7610       else
7611         {
7612           delta = (unsigned HOST_WIDE_INT) offset - min_offset;
7613           delta -= delta % range;
7614           if (delta > bias - 1)
7615             delta = bias - 1;
7616           offset = (HOST_WIDE_INT) delta;
7617         }
7618     }
7619
7620   /* Do a binary search to see if there's already an anchor we can use.
7621      Set BEGIN to the new anchor's index if not.  */
7622   begin = 0;
7623   end = vec_safe_length (block->anchors);
7624   while (begin != end)
7625     {
7626       middle = (end + begin) / 2;
7627       anchor = (*block->anchors)[middle];
7628       if (SYMBOL_REF_BLOCK_OFFSET (anchor) > offset)
7629         end = middle;
7630       else if (SYMBOL_REF_BLOCK_OFFSET (anchor) < offset)
7631         begin = middle + 1;
7632       else if (SYMBOL_REF_TLS_MODEL (anchor) > model)
7633         end = middle;
7634       else if (SYMBOL_REF_TLS_MODEL (anchor) < model)
7635         begin = middle + 1;
7636       else
7637         return anchor;
7638     }
7639
7640   /* Create a new anchor with a unique label.  */
7641   ASM_GENERATE_INTERNAL_LABEL (label, "LANCHOR", anchor_labelno++);
7642   anchor = create_block_symbol (ggc_strdup (label), block, offset);
7643   SYMBOL_REF_FLAGS (anchor) |= SYMBOL_FLAG_LOCAL | SYMBOL_FLAG_ANCHOR;
7644   SYMBOL_REF_FLAGS (anchor) |= model << SYMBOL_FLAG_TLS_SHIFT;
7645
7646   /* Insert it at index BEGIN.  */
7647   vec_safe_insert (block->anchors, begin, anchor);
7648   return anchor;
7649 }
7650
7651 /* Output the objects in BLOCK.  */
7652
7653 static void
7654 output_object_block (struct object_block *block)
7655 {
7656   class constant_descriptor_rtx *desc;
7657   unsigned int i;
7658   HOST_WIDE_INT offset;
7659   tree decl;
7660   rtx symbol;
7661
7662   if (!block->objects)
7663     return;
7664
7665   /* Switch to the section and make sure that the first byte is
7666      suitably aligned.  */
7667   /* Special case VTV comdat sections similar to assemble_variable.  */
7668   if (SECTION_STYLE (block->sect) == SECTION_NAMED
7669       && block->sect->named.name
7670       && (strcmp (block->sect->named.name, ".vtable_map_vars") == 0))
7671     handle_vtv_comdat_section (block->sect, block->sect->named.decl);
7672   else
7673     switch_to_section (block->sect);
7674
7675   gcc_checking_assert (!(block->sect->common.flags & SECTION_MERGE));
7676   assemble_align (block->alignment);
7677
7678   /* Define the values of all anchors relative to the current section
7679      position.  */
7680   FOR_EACH_VEC_SAFE_ELT (block->anchors, i, symbol)
7681     targetm.asm_out.output_anchor (symbol);
7682
7683   /* Output the objects themselves.  */
7684   offset = 0;
7685   FOR_EACH_VEC_ELT (*block->objects, i, symbol)
7686     {
7687       /* Move to the object's offset, padding with zeros if necessary.  */
7688       assemble_zeros (SYMBOL_REF_BLOCK_OFFSET (symbol) - offset);
7689       offset = SYMBOL_REF_BLOCK_OFFSET (symbol);
7690       if (CONSTANT_POOL_ADDRESS_P (symbol))
7691         {
7692           desc = SYMBOL_REF_CONSTANT (symbol);
7693           /* Pass 1 for align as we have already laid out everything in the block.
7694              So aligning shouldn't be necessary.  */
7695           output_constant_pool_1 (desc, 1);
7696           offset += GET_MODE_SIZE (desc->mode);
7697         }
7698       else if (TREE_CONSTANT_POOL_ADDRESS_P (symbol))
7699         {
7700           HOST_WIDE_INT size;
7701           decl = SYMBOL_REF_DECL (symbol);
7702           assemble_constant_contents (DECL_INITIAL (decl), XSTR (symbol, 0),
7703                                       DECL_ALIGN (decl), false);
7704
7705           size = get_constant_size (DECL_INITIAL (decl));
7706           offset += size;
7707           if ((flag_sanitize & SANITIZE_ADDRESS)
7708               && TREE_CODE (DECL_INITIAL (decl)) == STRING_CST
7709               && asan_protect_global (DECL_INITIAL (decl)))
7710             {
7711               size = asan_red_zone_size (size);
7712               assemble_zeros (size);
7713               offset += size;
7714             }
7715         }
7716       else
7717         {
7718           HOST_WIDE_INT size;
7719           decl = SYMBOL_REF_DECL (symbol);
7720           assemble_variable_contents (decl, XSTR (symbol, 0), false, false);
7721           size = tree_to_uhwi (DECL_SIZE_UNIT (decl));
7722           offset += size;
7723           if ((flag_sanitize & SANITIZE_ADDRESS)
7724               && asan_protect_global (decl))
7725             {
7726               size = asan_red_zone_size (size);
7727               assemble_zeros (size);
7728               offset += size;
7729             }
7730         }
7731     }
7732 }
7733
7734 /* A callback for qsort to compare object_blocks.  */
7735
7736 static int
7737 output_object_block_compare (const void *x, const void *y)
7738 {
7739   object_block *p1 = *(object_block * const*)x;
7740   object_block *p2 = *(object_block * const*)y;
7741
7742   if (p1->sect->common.flags & SECTION_NAMED
7743       && !(p2->sect->common.flags & SECTION_NAMED))
7744     return 1;
7745
7746   if (!(p1->sect->common.flags & SECTION_NAMED)
7747       && p2->sect->common.flags & SECTION_NAMED)
7748     return -1;
7749
7750   if (p1->sect->common.flags & SECTION_NAMED
7751       && p2->sect->common.flags & SECTION_NAMED)
7752     return strcmp (p1->sect->named.name, p2->sect->named.name);
7753
7754   unsigned f1 = p1->sect->common.flags;
7755   unsigned f2 = p2->sect->common.flags;
7756   if (f1 == f2)
7757     return 0;
7758   return f1 < f2 ? -1 : 1;
7759 }
7760
7761 /* Output the definitions of all object_blocks.  */
7762
7763 void
7764 output_object_blocks (void)
7765 {
7766   vec<object_block *, va_heap> v;
7767   v.create (object_block_htab->elements ());
7768   object_block *obj;
7769   hash_table<object_block_hasher>::iterator hi;
7770
7771   FOR_EACH_HASH_TABLE_ELEMENT (*object_block_htab, obj, object_block *, hi)
7772     v.quick_push (obj);
7773
7774   /* Sort them in order to output them in a deterministic manner,
7775      otherwise we may get .rodata sections in different orders with
7776      and without -g.  */
7777   v.qsort (output_object_block_compare);
7778   unsigned i;
7779   FOR_EACH_VEC_ELT (v, i, obj)
7780     output_object_block (obj);
7781
7782   v.release ();
7783 }
7784
7785 /* This function provides a possible implementation of the
7786    TARGET_ASM_RECORD_GCC_SWITCHES target hook for ELF targets.  When triggered
7787    by -frecord-gcc-switches it creates a new mergeable, string section in the
7788    assembler output file called TARGET_ASM_RECORD_GCC_SWITCHES_SECTION which
7789    contains the switches in ASCII format.
7790
7791    FIXME: This code does not correctly handle double quote characters
7792    that appear inside strings, (it strips them rather than preserving them).
7793    FIXME: ASM_OUTPUT_ASCII, as defined in config/elfos.h will not emit NUL
7794    characters - instead it treats them as sub-string separators.  Since
7795    we want to emit NUL strings terminators into the object file we have to use
7796    ASM_OUTPUT_SKIP.  */
7797
7798 int
7799 elf_record_gcc_switches (print_switch_type type, const char * name)
7800 {
7801   switch (type)
7802     {
7803     case SWITCH_TYPE_PASSED:
7804       ASM_OUTPUT_ASCII (asm_out_file, name, strlen (name));
7805       ASM_OUTPUT_SKIP (asm_out_file, HOST_WIDE_INT_1U);
7806       break;
7807
7808     case SWITCH_TYPE_DESCRIPTIVE:
7809       if (name == NULL)
7810         {
7811           /* Distinguish between invocations where name is NULL.  */
7812           static bool started = false;
7813
7814           if (!started)
7815             {
7816               section * sec;
7817
7818               sec = get_section (targetm.asm_out.record_gcc_switches_section,
7819                                  SECTION_DEBUG
7820                                  | SECTION_MERGE
7821                                  | SECTION_STRINGS
7822                                  | (SECTION_ENTSIZE & 1),
7823                                  NULL);
7824               switch_to_section (sec);
7825               started = true;
7826             }
7827         }
7828
7829     default:
7830       break;
7831     }
7832
7833   /* The return value is currently ignored by the caller, but must be 0.
7834      For -fverbose-asm the return value would be the number of characters
7835      emitted into the assembler file.  */
7836   return 0;
7837 }
7838
7839 /* Emit text to declare externally defined symbols. It is needed to
7840    properly support non-default visibility.  */
7841 void
7842 default_elf_asm_output_external (FILE *file ATTRIBUTE_UNUSED,
7843                                  tree decl,
7844                                  const char *name ATTRIBUTE_UNUSED)
7845 {
7846   /* We output the name if and only if TREE_SYMBOL_REFERENCED is
7847      set in order to avoid putting out names that are never really
7848      used.  Always output visibility specified in the source.  */
7849   if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
7850       && (DECL_VISIBILITY_SPECIFIED (decl)
7851           || targetm.binds_local_p (decl)))
7852     maybe_assemble_visibility (decl);
7853 }
7854
7855 /* The default hook for TARGET_ASM_OUTPUT_SOURCE_FILENAME.  */
7856
7857 void
7858 default_asm_output_source_filename (FILE *file, const char *name)
7859 {
7860 #ifdef ASM_OUTPUT_SOURCE_FILENAME
7861   ASM_OUTPUT_SOURCE_FILENAME (file, name);
7862 #else
7863   fprintf (file, "\t.file\t");
7864   output_quoted_string (file, name);
7865   putc ('\n', file);
7866 #endif
7867 }
7868
7869 /* Output a file name in the form wanted by System V.  */
7870
7871 void
7872 output_file_directive (FILE *asm_file, const char *input_name)
7873 {
7874   int len;
7875   const char *na;
7876
7877   if (input_name == NULL)
7878     input_name = "<stdin>";
7879   else
7880     input_name = remap_debug_filename (input_name);
7881
7882   len = strlen (input_name);
7883   na = input_name + len;
7884
7885   /* NA gets INPUT_NAME sans directory names.  */
7886   while (na > input_name)
7887     {
7888       if (IS_DIR_SEPARATOR (na[-1]))
7889         break;
7890       na--;
7891     }
7892
7893   targetm.asm_out.output_source_filename (asm_file, na);
7894 }
7895
7896 /* Create a DEBUG_EXPR_DECL / DEBUG_EXPR pair from RTL expression
7897    EXP.  */
7898 rtx
7899 make_debug_expr_from_rtl (const_rtx exp)
7900 {
7901   tree ddecl = make_node (DEBUG_EXPR_DECL), type;
7902   machine_mode mode = GET_MODE (exp);
7903   rtx dval;
7904
7905   DECL_ARTIFICIAL (ddecl) = 1;
7906   if (REG_P (exp) && REG_EXPR (exp))
7907     type = TREE_TYPE (REG_EXPR (exp));
7908   else if (MEM_P (exp) && MEM_EXPR (exp))
7909     type = TREE_TYPE (MEM_EXPR (exp));
7910   else
7911     type = NULL_TREE;
7912   if (type && TYPE_MODE (type) == mode)
7913     TREE_TYPE (ddecl) = type;
7914   else
7915     TREE_TYPE (ddecl) = lang_hooks.types.type_for_mode (mode, 1);
7916   SET_DECL_MODE (ddecl, mode);
7917   dval = gen_rtx_DEBUG_EXPR (mode);
7918   DEBUG_EXPR_TREE_DECL (dval) = ddecl;
7919   SET_DECL_RTL (ddecl, dval);
7920   return dval;
7921 }
7922
7923 #ifdef ELF_ASCII_ESCAPES
7924 /* Default ASM_OUTPUT_LIMITED_STRING for ELF targets.  */
7925
7926 void
7927 default_elf_asm_output_limited_string (FILE *f, const char *s)
7928 {
7929   int escape;
7930   unsigned char c;
7931
7932   fputs (STRING_ASM_OP, f);
7933   putc ('"', f);
7934   while (*s != '\0')
7935     {
7936       c = *s;
7937       escape = ELF_ASCII_ESCAPES[c];
7938       switch (escape)
7939         {
7940         case 0:
7941           putc (c, f);
7942           break;
7943         case 1:
7944           putc ('\\', f);
7945           putc ('0'+((c>>6)&7), f);
7946           putc ('0'+((c>>3)&7), f);
7947           putc ('0'+(c&7), f);
7948           break;
7949         default:
7950           putc ('\\', f);
7951           putc (escape, f);
7952           break;
7953         }
7954       s++;
7955     }
7956   putc ('\"', f);
7957   putc ('\n', f);
7958 }
7959
7960 /* Default ASM_OUTPUT_ASCII for ELF targets.  */
7961
7962 void
7963 default_elf_asm_output_ascii (FILE *f, const char *s, unsigned int len)
7964 {
7965   const char *limit = s + len;
7966   const char *last_null = NULL;
7967   unsigned bytes_in_chunk = 0;
7968   unsigned char c;
7969   int escape;
7970
7971   for (; s < limit; s++)
7972     {
7973       const char *p;
7974
7975       if (bytes_in_chunk >= 60)
7976         {
7977           putc ('\"', f);
7978           putc ('\n', f);
7979           bytes_in_chunk = 0;
7980         }
7981
7982       if (s > last_null)
7983         {
7984           for (p = s; p < limit && *p != '\0'; p++)
7985             continue;
7986           last_null = p;
7987         }
7988       else
7989         p = last_null;
7990
7991       if (p < limit && (p - s) <= (long) ELF_STRING_LIMIT)
7992         {
7993           if (bytes_in_chunk > 0)
7994             {
7995               putc ('\"', f);
7996               putc ('\n', f);
7997               bytes_in_chunk = 0;
7998             }
7999
8000           default_elf_asm_output_limited_string (f, s);
8001           s = p;
8002         }
8003       else
8004         {
8005           if (bytes_in_chunk == 0)
8006             fputs (ASCII_DATA_ASM_OP "\"", f);
8007
8008           c = *s;
8009           escape = ELF_ASCII_ESCAPES[c];
8010           switch (escape)
8011             {
8012             case 0:
8013               putc (c, f);
8014               bytes_in_chunk++;
8015               break;
8016             case 1:
8017               putc ('\\', f);
8018               putc ('0'+((c>>6)&7), f);
8019               putc ('0'+((c>>3)&7), f);
8020               putc ('0'+(c&7), f);
8021               bytes_in_chunk += 4;
8022               break;
8023             default:
8024               putc ('\\', f);
8025               putc (escape, f);
8026               bytes_in_chunk += 2;
8027               break;
8028             }
8029
8030         }
8031     }
8032
8033   if (bytes_in_chunk > 0)
8034     {
8035       putc ('\"', f);
8036       putc ('\n', f);
8037     }
8038 }
8039 #endif
8040
8041 static GTY(()) section *elf_init_array_section;
8042 static GTY(()) section *elf_fini_array_section;
8043
8044 static section *
8045 get_elf_initfini_array_priority_section (int priority,
8046                                          bool constructor_p)
8047 {
8048   section *sec;
8049   if (priority != DEFAULT_INIT_PRIORITY)
8050     {
8051       char buf[18];
8052       sprintf (buf, "%s.%.5u", 
8053                constructor_p ? ".init_array" : ".fini_array",
8054                priority);
8055       sec = get_section (buf, SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8056     }
8057   else
8058     {
8059       if (constructor_p)
8060         {
8061           if (elf_init_array_section == NULL)
8062             elf_init_array_section
8063               = get_section (".init_array",
8064                              SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8065           sec = elf_init_array_section;
8066         }
8067       else
8068         {
8069           if (elf_fini_array_section == NULL)
8070             elf_fini_array_section
8071               = get_section (".fini_array",
8072                              SECTION_WRITE | SECTION_NOTYPE, NULL_TREE);
8073           sec = elf_fini_array_section;
8074         }
8075     }
8076   return sec;
8077 }
8078
8079 /* Use .init_array section for constructors. */
8080
8081 void
8082 default_elf_init_array_asm_out_constructor (rtx symbol, int priority)
8083 {
8084   section *sec = get_elf_initfini_array_priority_section (priority,
8085                                                           true);
8086   assemble_addr_to_section (symbol, sec);
8087 }
8088
8089 /* Use .fini_array section for destructors. */
8090
8091 void
8092 default_elf_fini_array_asm_out_destructor (rtx symbol, int priority)
8093 {
8094   section *sec = get_elf_initfini_array_priority_section (priority,
8095                                                           false);
8096   assemble_addr_to_section (symbol, sec);
8097 }
8098
8099 /* Default TARGET_ASM_OUTPUT_IDENT hook.
8100
8101    This is a bit of a cheat.  The real default is a no-op, but this
8102    hook is the default for all targets with a .ident directive.  */
8103
8104 void
8105 default_asm_output_ident_directive (const char *ident_str)
8106 {
8107   const char *ident_asm_op = "\t.ident\t";
8108
8109   /* If we are still in the front end, do not write out the string
8110      to asm_out_file.  Instead, add a fake top-level asm statement.
8111      This allows the front ends to use this hook without actually
8112      writing to asm_out_file, to handle #ident or Pragma Ident.  */
8113   if (symtab->state == PARSING)
8114     {
8115       char *buf = ACONCAT ((ident_asm_op, "\"", ident_str, "\"\n", NULL));
8116       symtab->finalize_toplevel_asm (build_string (strlen (buf), buf));
8117     }
8118   else
8119     fprintf (asm_out_file, "%s\"%s\"\n", ident_asm_op, ident_str);
8120 }
8121
8122
8123 /* This function ensures that vtable_map variables are not only
8124    in the comdat section, but that each variable has its own unique
8125    comdat name.  Without this the variables end up in the same section
8126    with a single comdat name.
8127
8128    FIXME:  resolve_unique_section needs to deal better with
8129    decls with both DECL_SECTION_NAME and DECL_ONE_ONLY.  Once
8130    that is fixed, this if-else statement can be replaced with
8131    a single call to "switch_to_section (sect)".  */
8132
8133 static void
8134 handle_vtv_comdat_section (section *sect, const_tree decl ATTRIBUTE_UNUSED)
8135 {
8136 #if defined (OBJECT_FORMAT_ELF)
8137   targetm.asm_out.named_section (sect->named.name,
8138                                  sect->named.common.flags
8139                                  | SECTION_LINKONCE,
8140                                  DECL_NAME (decl));
8141   in_section = sect;
8142 #else
8143   /* Neither OBJECT_FORMAT_PE, nor OBJECT_FORMAT_COFF is set here.
8144      Therefore the following check is used.
8145      In case a the target is PE or COFF a comdat group section
8146      is created, e.g. .vtable_map_vars$foo. The linker places
8147      everything in .vtable_map_vars at the end.
8148
8149      A fix could be made in
8150      gcc/config/i386/winnt.c: i386_pe_unique_section.  */
8151   if (TARGET_PECOFF)
8152     {
8153       char *name;
8154
8155       if (TREE_CODE (DECL_NAME (decl)) == IDENTIFIER_NODE)
8156         name = ACONCAT ((sect->named.name, "$",
8157                          IDENTIFIER_POINTER (DECL_NAME (decl)), NULL));
8158       else
8159         name = ACONCAT ((sect->named.name, "$",
8160                          IDENTIFIER_POINTER (DECL_COMDAT_GROUP (DECL_NAME (decl))),
8161                          NULL));
8162
8163       targetm.asm_out.named_section (name,
8164                                      sect->named.common.flags
8165                                      | SECTION_LINKONCE,
8166                                      DECL_NAME (decl));
8167       in_section = sect;
8168     }
8169   else
8170     switch_to_section (sect);
8171 #endif
8172 }
8173
8174 #include "gt-varasm.h"