Debian Codegraph @

gcc/varasm.cc

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