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