]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gas/config/obj-elf.c
gdb: Avoid compilation warning in gcore.c.
[thirdparty/binutils-gdb.git] / gas / config / obj-elf.c
1 /* ELF object file format
2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
3
4 This file is part of GAS, the GNU Assembler.
5
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 3,
9 or (at your option) any later version.
10
11 GAS is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
19 02110-1301, USA. */
20
21 #define OBJ_HEADER "obj-elf.h"
22 #include "as.h"
23 #include "safe-ctype.h"
24 #include "subsegs.h"
25 #include "obstack.h"
26 #include "dwarf2dbg.h"
27 #include "ginsn.h"
28
29 #ifndef ECOFF_DEBUGGING
30 #define ECOFF_DEBUGGING 0
31 #else
32 #define NEED_ECOFF_DEBUG
33 #endif
34
35 #ifdef NEED_ECOFF_DEBUG
36 #include "ecoff.h"
37 #include "bfd/ecoff-bfd.h"
38 #endif
39
40 #ifdef TC_ALPHA
41 #include "elf/alpha.h"
42 #endif
43
44 #ifdef TC_MIPS
45 #include "elf/mips.h"
46 #endif
47
48 #ifdef TC_PPC
49 #include "elf/ppc.h"
50 #endif
51
52 #ifdef TC_I386
53 #include "elf/x86-64.h"
54 #endif
55
56 #ifdef TC_MEP
57 #include "elf/mep.h"
58 #endif
59
60 #ifdef TC_NIOS2
61 #include "elf/nios2.h"
62 #endif
63
64 #ifdef TC_PRU
65 #include "elf/pru.h"
66 #endif
67
68 static void obj_elf_line (int);
69 static void obj_elf_size (int);
70 static void obj_elf_type (int);
71 static void obj_elf_ident (int);
72 static void obj_elf_weak (int);
73 static void obj_elf_local (int);
74 static void obj_elf_visibility (int);
75 static void obj_elf_symver (int);
76 static void obj_elf_subsection (int);
77 static void obj_elf_popsection (int);
78 static void obj_elf_gnu_attribute (int);
79 static void obj_elf_tls_common (int);
80 static void obj_elf_lcomm (int);
81 static void obj_elf_struct (int);
82 static void obj_elf_attach_to_group (int);
83
84 static const pseudo_typeS elf_pseudo_table[] =
85 {
86 {"attach_to_group", obj_elf_attach_to_group, 0},
87 {"comm", obj_elf_common, 0},
88 {"common", obj_elf_common, 1},
89 {"ident", obj_elf_ident, 0},
90 {"lcomm", obj_elf_lcomm, 0},
91 {"local", obj_elf_local, 0},
92 {"previous", obj_elf_previous, 0},
93 {"section", obj_elf_section, 0},
94 {"section.s", obj_elf_section, 0},
95 {"sect", obj_elf_section, 0},
96 {"sect.s", obj_elf_section, 0},
97 {"pushsection", obj_elf_section, 1},
98 {"popsection", obj_elf_popsection, 0},
99 {"size", obj_elf_size, 0},
100 {"type", obj_elf_type, 0},
101 {"version", obj_elf_version, 0},
102 {"weak", obj_elf_weak, 0},
103
104 /* These define symbol visibility. */
105 {"internal", obj_elf_visibility, STV_INTERNAL},
106 {"hidden", obj_elf_visibility, STV_HIDDEN},
107 {"protected", obj_elf_visibility, STV_PROTECTED},
108
109 /* These are used for stabs-in-elf configurations. */
110 {"line", obj_elf_line, 0},
111
112 /* This is a GNU extension to handle symbol versions. */
113 {"symver", obj_elf_symver, 0},
114
115 /* A GNU extension to change subsection only. */
116 {"subsection", obj_elf_subsection, 0},
117
118 /* These are GNU extensions to aid in garbage collecting C++ vtables. */
119 {"vtable_inherit", obj_elf_vtable_inherit, 0},
120 {"vtable_entry", obj_elf_vtable_entry, 0},
121
122 /* A GNU extension for object attributes. */
123 {"gnu_attribute", obj_elf_gnu_attribute, 0},
124
125 /* These are used for dwarf2. */
126 { "file", dwarf2_directive_file, 0 },
127 { "loc", dwarf2_directive_loc, 0 },
128 { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
129
130 /* We need to trap the section changing calls to handle .previous. */
131 {"data", obj_elf_data, 0},
132 {"offset", obj_elf_struct, 0},
133 {"struct", obj_elf_struct, 0},
134 {"text", obj_elf_text, 0},
135 {"bss", obj_elf_bss, 0},
136
137 {"tls_common", obj_elf_tls_common, 0},
138
139 /* End sentinel. */
140 {NULL, NULL, 0},
141 };
142
143 static const pseudo_typeS ecoff_debug_pseudo_table[] =
144 {
145 #ifdef NEED_ECOFF_DEBUG
146 /* COFF style debugging information for ECOFF. .ln is not used; .loc
147 is used instead. */
148 { "def", ecoff_directive_def, 0 },
149 { "dim", ecoff_directive_dim, 0 },
150 { "endef", ecoff_directive_endef, 0 },
151 { "file", ecoff_directive_file, 0 },
152 { "scl", ecoff_directive_scl, 0 },
153 { "tag", ecoff_directive_tag, 0 },
154 { "val", ecoff_directive_val, 0 },
155
156 /* COFF debugging requires pseudo-ops .size and .type, but ELF
157 already has meanings for those. We use .esize and .etype
158 instead. These are only generated by gcc anyhow. */
159 { "esize", ecoff_directive_size, 0 },
160 { "etype", ecoff_directive_type, 0 },
161
162 /* ECOFF specific debugging information. */
163 { "aent", ecoff_directive_ent, 1 },
164 { "begin", ecoff_directive_begin, 0 },
165 { "bend", ecoff_directive_bend, 0 },
166 { "end", ecoff_directive_end, 0 },
167 { "ent", ecoff_directive_ent, 0 },
168 { "fmask", ecoff_directive_fmask, 0 },
169 { "frame", ecoff_directive_frame, 0 },
170 { "loc", ecoff_directive_loc, 0 },
171 { "mask", ecoff_directive_mask, 0 },
172
173 /* Other ECOFF directives. */
174 { "extern", ecoff_directive_extern, 0 },
175
176 /* These are used on Irix. I don't know how to implement them. */
177 { "alias", s_ignore, 0 },
178 { "bgnb", s_ignore, 0 },
179 { "endb", s_ignore, 0 },
180 { "lab", s_ignore, 0 },
181 { "noalias", s_ignore, 0 },
182 { "verstamp", s_ignore, 0 },
183 { "vreg", s_ignore, 0 },
184 #endif
185
186 {NULL, NULL, 0} /* end sentinel */
187 };
188
189 #undef NO_RELOC
190 #include "aout/aout64.h"
191
192 asection *elf_com_section_ptr;
193
194 void
195 elf_pop_insert (void)
196 {
197 pop_insert (elf_pseudo_table);
198 if (ECOFF_DEBUGGING)
199 pop_insert (ecoff_debug_pseudo_table);
200 }
201
202 static bfd_vma
203 elf_s_get_size (symbolS *sym)
204 {
205 return S_GET_SIZE (sym);
206 }
207
208 static void
209 elf_s_set_size (symbolS *sym, bfd_vma sz)
210 {
211 S_SET_SIZE (sym, sz);
212 }
213
214 static bfd_vma
215 elf_s_get_align (symbolS *sym)
216 {
217 return S_GET_ALIGN (sym);
218 }
219
220 static void
221 elf_s_set_align (symbolS *sym, bfd_vma align)
222 {
223 S_SET_ALIGN (sym, align);
224 }
225
226 int
227 elf_s_get_other (symbolS *sym)
228 {
229 return elf_symbol (symbol_get_bfdsym (sym))->internal_elf_sym.st_other;
230 }
231
232 static void
233 elf_s_set_other (symbolS *sym, int other)
234 {
235 S_SET_OTHER (sym, other);
236 }
237
238 static int
239 elf_sec_sym_ok_for_reloc (asection *sec)
240 {
241 return obj_sec_sym_ok_for_reloc (sec);
242 }
243
244 void
245 elf_file_symbol (const char *s)
246 {
247 asymbol *bsym;
248 symbolS *sym = symbol_new (s, absolute_section, &zero_address_frag, 0);
249 size_t name_length = strlen (s);
250
251 if (name_length > strlen (S_GET_NAME (sym)))
252 {
253 obstack_grow (&notes, s, name_length + 1);
254 S_SET_NAME (sym, (const char *) obstack_finish (&notes));
255 }
256 else
257 strcpy ((char *) S_GET_NAME (sym), s);
258
259 symbol_get_bfdsym (sym)->flags |= BSF_FILE;
260
261 if (symbol_rootP != sym
262 && ((bsym = symbol_get_bfdsym (symbol_rootP)) == NULL
263 || (bsym->flags & BSF_FILE) == 0))
264 {
265 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
266 symbol_insert (sym, symbol_rootP, &symbol_rootP, &symbol_lastP);
267 }
268
269 #ifdef DEBUG
270 verify_symbol_chain (symbol_rootP, symbol_lastP);
271 #endif
272
273 #ifdef NEED_ECOFF_DEBUG
274 ecoff_new_file (s);
275 #endif
276 }
277
278 /* Called from read.c:s_comm after we've parsed .comm symbol, size.
279 Parse a possible alignment value. */
280
281 symbolS *
282 elf_common_parse (int ignore ATTRIBUTE_UNUSED, symbolS *symbolP, addressT size)
283 {
284 addressT align = 0;
285 int is_local = symbol_get_obj (symbolP)->local;
286
287 if (*input_line_pointer == ',')
288 {
289 char *save = input_line_pointer;
290
291 input_line_pointer++;
292 SKIP_WHITESPACE ();
293
294 if (*input_line_pointer == '"')
295 {
296 /* For sparc. Accept .common symbol, length, "bss" */
297 input_line_pointer++;
298 /* Some use the dot, some don't. */
299 if (*input_line_pointer == '.')
300 input_line_pointer++;
301 /* Some say data, some say bss. */
302 if (startswith (input_line_pointer, "bss\""))
303 input_line_pointer += 4;
304 else if (startswith (input_line_pointer, "data\""))
305 input_line_pointer += 5;
306 else
307 {
308 char *p = input_line_pointer;
309 char c;
310
311 while (*--p != '"')
312 ;
313 while (!is_end_of_line[(unsigned char) *input_line_pointer])
314 if (*input_line_pointer++ == '"')
315 break;
316 c = *input_line_pointer;
317 *input_line_pointer = '\0';
318 as_bad (_("bad .common segment %s"), p);
319 *input_line_pointer = c;
320 ignore_rest_of_line ();
321 return NULL;
322 }
323 /* ??? Don't ask me why these are always global. */
324 is_local = 0;
325 }
326 else
327 {
328 input_line_pointer = save;
329 align = parse_align (is_local);
330 if (align == (addressT) -1)
331 return NULL;
332 }
333 }
334
335 if (is_local)
336 {
337 bss_alloc (symbolP, size, align);
338 S_CLEAR_EXTERNAL (symbolP);
339 }
340 else
341 {
342 S_SET_VALUE (symbolP, size);
343 S_SET_ALIGN (symbolP, align);
344 S_SET_EXTERNAL (symbolP);
345 S_SET_SEGMENT (symbolP, elf_com_section_ptr);
346 }
347
348 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
349
350 return symbolP;
351 }
352
353 void
354 obj_elf_common (int is_common)
355 {
356 if (flag_mri && is_common)
357 s_mri_common (0);
358 else
359 s_comm_internal (0, elf_common_parse);
360 }
361
362 static void
363 obj_elf_tls_common (int ignore ATTRIBUTE_UNUSED)
364 {
365 symbolS *symbolP = s_comm_internal (0, elf_common_parse);
366
367 if (symbolP)
368 symbol_get_bfdsym (symbolP)->flags |= BSF_THREAD_LOCAL;
369 }
370
371 static void
372 obj_elf_lcomm (int ignore ATTRIBUTE_UNUSED)
373 {
374 symbolS *symbolP = s_comm_internal (0, s_lcomm_internal);
375
376 if (symbolP)
377 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
378 }
379
380 static symbolS *
381 get_sym_from_input_line_and_check (void)
382 {
383 char *name;
384 char c;
385 symbolS *sym;
386
387 c = get_symbol_name (& name);
388 sym = symbol_find_or_make (name);
389 *input_line_pointer = c;
390 SKIP_WHITESPACE_AFTER_NAME ();
391
392 /* There is no symbol name if input_line_pointer has not moved. */
393 if (name == input_line_pointer)
394 as_bad (_("Missing symbol name in directive"));
395 return sym;
396 }
397
398 static void
399 obj_elf_local (int ignore ATTRIBUTE_UNUSED)
400 {
401 int c;
402 symbolS *symbolP;
403
404 do
405 {
406 symbolP = get_sym_from_input_line_and_check ();
407 c = *input_line_pointer;
408 S_CLEAR_EXTERNAL (symbolP);
409 symbol_get_obj (symbolP)->local = 1;
410 if (c == ',')
411 {
412 input_line_pointer++;
413 SKIP_WHITESPACE ();
414 if (*input_line_pointer == '\n')
415 c = '\n';
416 }
417 }
418 while (c == ',');
419 demand_empty_rest_of_line ();
420 }
421
422 static void
423 obj_elf_weak (int ignore ATTRIBUTE_UNUSED)
424 {
425 int c;
426 symbolS *symbolP;
427
428 do
429 {
430 symbolP = get_sym_from_input_line_and_check ();
431 c = *input_line_pointer;
432 S_SET_WEAK (symbolP);
433 if (c == ',')
434 {
435 input_line_pointer++;
436 SKIP_WHITESPACE ();
437 if (*input_line_pointer == '\n')
438 c = '\n';
439 }
440 }
441 while (c == ',');
442 demand_empty_rest_of_line ();
443 }
444
445 static void
446 obj_elf_visibility (int visibility)
447 {
448 int c;
449 symbolS *symbolP;
450 asymbol *bfdsym;
451 elf_symbol_type *elfsym;
452
453 do
454 {
455 symbolP = get_sym_from_input_line_and_check ();
456
457 bfdsym = symbol_get_bfdsym (symbolP);
458 elfsym = elf_symbol_from (bfdsym);
459
460 gas_assert (elfsym);
461
462 elfsym->internal_elf_sym.st_other &= ~3;
463 elfsym->internal_elf_sym.st_other |= visibility;
464
465 c = *input_line_pointer;
466 if (c == ',')
467 {
468 input_line_pointer ++;
469
470 SKIP_WHITESPACE ();
471
472 if (*input_line_pointer == '\n')
473 c = '\n';
474 }
475 }
476 while (c == ',');
477
478 demand_empty_rest_of_line ();
479 }
480
481 static segT previous_section;
482 static int previous_subsection;
483
484 struct section_stack
485 {
486 struct section_stack *next;
487 segT seg, prev_seg;
488 int subseg, prev_subseg;
489 };
490
491 static struct section_stack *section_stack;
492
493 /* ELF section flags for unique sections. */
494 #define SEC_ASSEMBLER_SHF_MASK SHF_GNU_RETAIN
495
496 struct group_list
497 {
498 asection **head; /* Section lists. */
499 unsigned int num_group; /* Number of lists. */
500 htab_t indexes; /* Maps group name to index in head array. */
501 };
502
503 static struct group_list groups;
504
505 static bool
506 match_section (const asection *sec, const struct elf_section_match *match)
507 {
508 const char *linked_to_symbol_name = sec->map_head.linked_to_symbol_name;
509 unsigned int sh_info = elf_section_data (sec)->this_hdr.sh_info;
510 bfd_vma sh_flags = (elf_section_data (sec)->this_hdr.sh_flags
511 & SEC_ASSEMBLER_SHF_MASK);
512
513 return (sh_info == match->sh_info
514 && sh_flags == match->sh_flags
515 && ((bfd_section_flags (sec) & SEC_ASSEMBLER_SECTION_ID)
516 == (match->flags & SEC_ASSEMBLER_SECTION_ID))
517 && sec->section_id == match->section_id
518 && (linked_to_symbol_name == match->linked_to_symbol_name
519 || (linked_to_symbol_name != NULL
520 && match->linked_to_symbol_name != NULL
521 && strcmp (linked_to_symbol_name,
522 match->linked_to_symbol_name) == 0)));
523 }
524
525 /* Return TRUE iff SEC matches the section info INF. */
526
527 static bool
528 get_section_by_match (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
529 {
530 struct elf_section_match *match = (struct elf_section_match *) inf;
531 const char *gname = match->group_name;
532 const char *group_name = elf_group_name (sec);
533
534 return ((group_name == gname
535 || (group_name != NULL
536 && gname != NULL
537 && strcmp (group_name, gname) == 0))
538 && match_section (sec, match));
539 }
540
541 static void
542 free_section_idx (void *ent)
543 {
544 string_tuple_t *tuple = ent;
545 free ((char *) tuple->value);
546 }
547
548 /* Go look in section lists kept per group for SEC_NAME with
549 properties given by MATCH. If info for the group named by
550 MATCH->GROUP_NAME has been initialised, set GROUP_IDX. */
551
552 static asection *
553 group_section_find (const struct elf_section_match *match,
554 const char *sec_name,
555 unsigned int **group_idx)
556 {
557 if (!groups.indexes)
558 {
559 groups.num_group = 0;
560 groups.head = NULL;
561 groups.indexes = htab_create_alloc (16, hash_string_tuple, eq_string_tuple,
562 free_section_idx, notes_calloc, NULL);
563 *group_idx = NULL;
564 return NULL;
565 }
566
567 *group_idx = str_hash_find (groups.indexes, match->group_name);
568 if (*group_idx == NULL)
569 return NULL;
570
571 asection *s;
572 for (s = groups.head[**group_idx]; s != NULL; s = elf_next_in_group (s))
573 if ((s->name == sec_name
574 || strcmp (s->name, sec_name) == 0)
575 && match_section (s, match))
576 break;
577 return s;
578 }
579
580 /* Insert SEC into section lists kept per group. MATCH and GROUP_IDX
581 must be from a prior call to group_section_find. */
582
583 static void
584 group_section_insert (const struct elf_section_match *match,
585 asection *sec,
586 unsigned int **group_idx)
587 {
588 if (*group_idx != NULL)
589 {
590 elf_next_in_group (sec) = groups.head[**group_idx];
591 groups.head[**group_idx] = sec;
592 return;
593 }
594
595 unsigned int i = groups.num_group;
596 if ((i & 127) == 0)
597 groups.head = XRESIZEVEC (asection *, groups.head, i + 128);
598 groups.head[i] = sec;
599 groups.num_group += 1;
600
601 /* We keep the index into groups.head rather than the entry address
602 because groups.head may be realloc'd, and because str_hash values
603 are a void* we make a copy of the index. Strictly speaking there
604 is no guarantee that void* can represent any int value, so doing
605 without the indirection by casting an int or even uintptr_t may
606 for example lose lsbs of the value. */
607 unsigned int *idx_ptr = XNEW (unsigned int);
608 *idx_ptr = i;
609 str_hash_insert (groups.indexes, match->group_name, idx_ptr, 0);
610 }
611
612 /* Handle the .section pseudo-op. This code supports two different
613 syntaxes.
614
615 The first is found on Solaris, and looks like
616 .section ".sec1",#alloc,#execinstr,#write
617 Here the names after '#' are the SHF_* flags to turn on for the
618 section. I'm not sure how it determines the SHT_* type (BFD
619 doesn't really give us control over the type, anyhow).
620
621 The second format is found on UnixWare, and probably most SVR4
622 machines, and looks like
623 .section .sec1,"a",@progbits
624 The quoted string may contain any combination of a, w, x, and
625 represents the SHF_* flags to turn on for the section. The string
626 beginning with '@' can be progbits or nobits. There should be
627 other possibilities, but I don't know what they are. In any case,
628 BFD doesn't really let us set the section type. */
629
630 static void
631 change_section (const char *name,
632 unsigned int type,
633 bfd_vma attr,
634 int entsize,
635 struct elf_section_match *match_p,
636 bool linkonce,
637 bool push,
638 subsegT new_subsection)
639 {
640 asection *old_sec;
641 segT sec;
642 flagword flags;
643 const struct elf_backend_data *bed;
644 const struct bfd_elf_special_section *ssect;
645
646 if (match_p == NULL)
647 {
648 static struct elf_section_match unused_match;
649 match_p = &unused_match;
650 }
651
652 #ifdef md_flush_pending_output
653 md_flush_pending_output ();
654 #endif
655
656 /* Switch to the section, creating it if necessary. */
657 if (push)
658 {
659 struct section_stack *elt;
660 elt = XNEW (struct section_stack);
661 elt->next = section_stack;
662 elt->seg = now_seg;
663 elt->prev_seg = previous_section;
664 elt->subseg = now_subseg;
665 elt->prev_subseg = previous_subsection;
666 section_stack = elt;
667 }
668
669 obj_elf_section_change_hook ();
670
671 unsigned int *group_idx = NULL;
672 if (match_p->group_name)
673 old_sec = group_section_find (match_p, name, &group_idx);
674 else
675 old_sec = bfd_get_section_by_name_if (stdoutput, name, get_section_by_match,
676 (void *) match_p);
677 if (old_sec)
678 {
679 sec = old_sec;
680 subseg_set (sec, new_subsection);
681 }
682 else
683 {
684 sec = subseg_force_new (name, new_subsection);
685 if (match_p->group_name)
686 group_section_insert (match_p, sec, &group_idx);
687 }
688
689 bed = get_elf_backend_data (stdoutput);
690 ssect = (*bed->get_sec_type_attr) (stdoutput, sec);
691
692 if (ssect != NULL)
693 {
694 bool override = false;
695
696 if (type == SHT_NULL)
697 type = ssect->type;
698 else if (type != ssect->type)
699 {
700 if (old_sec == NULL
701 /* Some older versions of gcc will emit
702
703 .section .init_array,"aw",@progbits
704
705 for __attribute__ ((section (".init_array"))).
706 "@progbits" is incorrect. Also for x86-64 large bss
707 sections, some older versions of gcc will emit
708
709 .section .lbss,"aw",@progbits
710
711 "@progbits" is incorrect. */
712 #ifdef TC_I386
713 && (bed->s->arch_size != 64
714 || !(ssect->attr & SHF_X86_64_LARGE))
715 #endif
716 && ssect->type != SHT_INIT_ARRAY
717 && ssect->type != SHT_FINI_ARRAY
718 && ssect->type != SHT_PREINIT_ARRAY)
719 {
720 /* We allow to specify any type for a .note section. */
721 if (ssect->type != SHT_NOTE
722 /* Processor and application defined types are allowed too. */
723 && type < SHT_LOPROC)
724 as_warn (_("setting incorrect section type for %s"),
725 name);
726 }
727 else
728 {
729 as_warn (_("ignoring incorrect section type for %s"),
730 name);
731 type = ssect->type;
732 }
733 }
734
735 if (old_sec == NULL && ((attr & ~(SHF_LINK_ORDER
736 | SHF_MASKOS
737 | SHF_MASKPROC))
738 & ~ssect->attr) != 0)
739 {
740 /* Strip SHF_GNU_RETAIN. */
741 bfd_vma generic_attr = attr;
742 if (elf_tdata (stdoutput)->has_gnu_osabi)
743 generic_attr &= ~SHF_GNU_RETAIN;
744
745 /* As a GNU extension, we permit a .note section to be
746 allocatable. If the linker sees an allocatable .note
747 section, it will create a PT_NOTE segment in the output
748 file. We also allow "x" for .note.GNU-stack. */
749 if (ssect->type == SHT_NOTE
750 && (generic_attr == SHF_ALLOC
751 || generic_attr == SHF_EXECINSTR))
752 ;
753 /* Allow different SHF_MERGE and SHF_STRINGS if we have
754 something like .rodata.str. */
755 else if (ssect->suffix_length == -2
756 && name[ssect->prefix_length] == '.'
757 && (generic_attr
758 & ~ssect->attr
759 & ~SHF_MERGE
760 & ~SHF_STRINGS) == 0)
761 ;
762 /* .interp, .strtab and .symtab can have SHF_ALLOC. */
763 else if (generic_attr == SHF_ALLOC
764 && (strcmp (name, ".interp") == 0
765 || strcmp (name, ".strtab") == 0
766 || strcmp (name, ".symtab") == 0))
767 override = true;
768 /* .note.GNU-stack can have SHF_EXECINSTR. */
769 else if (generic_attr == SHF_EXECINSTR
770 && strcmp (name, ".note.GNU-stack") == 0)
771 override = true;
772 #ifdef TC_ALPHA
773 /* A section on Alpha may have SHF_ALPHA_GPREL. */
774 else if ((generic_attr & ~ssect->attr) == SHF_ALPHA_GPREL)
775 override = true;
776 #endif
777 #ifdef TC_RX
778 else if (generic_attr == (SHF_EXECINSTR | SHF_WRITE | SHF_ALLOC)
779 && (ssect->type == SHT_INIT_ARRAY
780 || ssect->type == SHT_FINI_ARRAY
781 || ssect->type == SHT_PREINIT_ARRAY))
782 /* RX init/fini arrays can and should have the "awx" attributes set. */
783 ;
784 #endif
785 else
786 {
787 if (match_p->group_name == NULL)
788 as_warn (_("setting incorrect section attributes for %s"),
789 name);
790 override = true;
791 }
792 }
793
794 if (!override && old_sec == NULL)
795 attr |= ssect->attr;
796 }
797
798 /* Convert ELF type and flags to BFD flags. */
799 flags = (SEC_RELOC
800 | ((attr & SHF_WRITE) ? 0 : SEC_READONLY)
801 | ((attr & SHF_ALLOC) ? SEC_ALLOC : 0)
802 | (((attr & SHF_ALLOC) && type != SHT_NOBITS) ? SEC_LOAD : 0)
803 | ((attr & SHF_EXECINSTR) ? SEC_CODE : 0)
804 | ((attr & SHF_MERGE) ? SEC_MERGE : 0)
805 | ((attr & SHF_STRINGS) ? SEC_STRINGS : 0)
806 | ((attr & SHF_EXCLUDE) ? SEC_EXCLUDE: 0)
807 | ((attr & SHF_TLS) ? SEC_THREAD_LOCAL : 0));
808 #ifdef md_elf_section_flags
809 flags = md_elf_section_flags (flags, attr, type);
810 #endif
811
812 if (linkonce)
813 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
814
815 /* PR 28054: Set the SEC_ELF_OCTETS flag for debugging sections.
816 Based on the code in bfd/elf.c:_bfd_elf_make_section_from_shdr().
817
818 FIXME: We do not set the SEC_DEBUGGING flag because that causes
819 problems for the FT32 and MSP430 targets. Investigate and fix. */
820 if ((flags & SEC_ALLOC) == 0 && name [0] == '.')
821 {
822 if ( startswith (name, ".debug")
823 || startswith (name, ".zdebug")
824 || startswith (name, ".gnu.debuglto_.debug_")
825 || startswith (name, ".gnu.linkonce.wi.")
826 || startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
827 || startswith (name, ".note.gnu"))
828 flags |= SEC_ELF_OCTETS;
829 }
830
831 if (old_sec == NULL)
832 {
833 symbolS *secsym;
834
835 if (type == SHT_NULL)
836 type = bfd_elf_get_default_section_type (flags);
837 elf_section_type (sec) = type;
838 elf_section_flags (sec) = attr;
839 elf_section_data (sec)->this_hdr.sh_info = match_p->sh_info;
840
841 /* Prevent SEC_HAS_CONTENTS from being inadvertently set. */
842 if (type == SHT_NOBITS)
843 seg_info (sec)->bss = 1;
844
845 /* Set the section ID and flags. */
846 sec->section_id = match_p->section_id;
847 flags |= match_p->flags;
848
849 /* Set the linked-to symbol name. */
850 sec->map_head.linked_to_symbol_name
851 = match_p->linked_to_symbol_name;
852
853 bfd_set_section_flags (sec, flags);
854 if (flags & SEC_MERGE)
855 sec->entsize = entsize;
856 elf_group_name (sec) = match_p->group_name;
857
858 /* Add a symbol for this section to the symbol table. */
859 secsym = symbol_find (name);
860 if (secsym != NULL)
861 {
862 /* We could be repurposing an undefined symbol here: make sure we
863 reset sy_value to look like other section symbols in order to avoid
864 trying to incorrectly resolve this section symbol later on. */
865 static const expressionS exp = { .X_op = O_constant };
866 symbol_set_value_expression (secsym, &exp);
867 symbol_set_bfdsym (secsym, sec->symbol);
868 }
869 else
870 symbol_table_insert (section_symbol (sec));
871 }
872 else
873 {
874 if (type != SHT_NULL
875 && (unsigned) type != elf_section_type (old_sec))
876 {
877 if (ssect != NULL)
878 /* This is a special section with known type. User
879 assembly might get the section type wrong; Even high
880 profile projects like glibc have done so in the past.
881 So don't error in this case. */
882 as_warn (_("ignoring changed section type for %s"), name);
883 else
884 /* Do error when assembly isn't self-consistent. */
885 as_bad (_("changed section type for %s"), name);
886 }
887
888 if (attr != 0)
889 {
890 /* If section attributes are specified the second time we see a
891 particular section, then check that they are the same as we
892 saw the first time. */
893 if (((old_sec->flags ^ flags)
894 & (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
895 | SEC_EXCLUDE | SEC_SORT_ENTRIES | SEC_MERGE | SEC_STRINGS
896 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
897 | SEC_THREAD_LOCAL)))
898 {
899 if (ssect != NULL)
900 as_warn (_("ignoring changed section attributes for %s"), name);
901 else
902 as_bad (_("changed section attributes for %s"), name);
903 }
904 else
905 /* FIXME: Maybe we should consider removing a previously set
906 processor or application specific attribute as suspicious? */
907 elf_section_flags (sec) = attr;
908
909 if ((flags & SEC_MERGE) && old_sec->entsize != (unsigned) entsize)
910 as_bad (_("changed section entity size for %s"), name);
911 }
912 }
913
914 #ifdef md_elf_section_change_hook
915 md_elf_section_change_hook ();
916 #endif
917 }
918
919 void
920 obj_elf_change_section (const char *name,
921 unsigned int type,
922 bfd_vma attr,
923 int entsize,
924 struct elf_section_match *match_p,
925 bool linkonce)
926 {
927 change_section (name, type, attr, entsize, match_p, linkonce, false, 0);
928 }
929
930 static bfd_vma
931 obj_elf_parse_section_letters (char *str, size_t len,
932 bool *is_clone, int *inherit, bfd_vma *gnu_attr)
933 {
934 bfd_vma attr = 0;
935
936 *is_clone = false;
937 *inherit = 0;
938
939 while (len > 0)
940 {
941 switch (*str)
942 {
943 case 'a':
944 attr |= SHF_ALLOC;
945 /* Compatibility. */
946 if (len > 1 && str[1] == 'm')
947 {
948 attr |= SHF_MERGE;
949 str++, len--;
950 if (len > 1 && str[1] == 's')
951 {
952 attr |= SHF_STRINGS;
953 str++, len--;
954 }
955 }
956 break;
957 case 'e':
958 attr |= SHF_EXCLUDE;
959 break;
960 case 'o':
961 attr |= SHF_LINK_ORDER;
962 break;
963 case 'w':
964 attr |= SHF_WRITE;
965 break;
966 case 'x':
967 attr |= SHF_EXECINSTR;
968 break;
969 case 'M':
970 attr |= SHF_MERGE;
971 break;
972 case 'S':
973 attr |= SHF_STRINGS;
974 break;
975 case 'G':
976 attr |= SHF_GROUP;
977 break;
978 case 'T':
979 attr |= SHF_TLS;
980 break;
981 case 'd':
982 *gnu_attr |= SHF_GNU_MBIND;
983 break;
984 case 'R':
985 *gnu_attr |= SHF_GNU_RETAIN;
986 break;
987 case '?':
988 *is_clone = true;
989 break;
990 default:
991 {
992 const char *bad_msg = _("unrecognized .section attribute:"
993 " want a,e,o,w,x,M,S,G,T or number");
994 #ifdef md_elf_section_letter
995 bfd_vma md_attr = md_elf_section_letter (*str, &bad_msg);
996 if (md_attr != (bfd_vma) -1)
997 attr |= md_attr;
998 else
999 #endif
1000 if (ISDIGIT (*str))
1001 {
1002 char * end;
1003 struct elf_backend_data *bed;
1004 bfd_vma numeric_flags = strtoul (str, &end, 0);
1005
1006 attr |= numeric_flags;
1007
1008 bed = (struct elf_backend_data *)
1009 get_elf_backend_data (stdoutput);
1010
1011 if (bed->elf_osabi == ELFOSABI_NONE
1012 || bed->elf_osabi == ELFOSABI_STANDALONE
1013 || bed->elf_osabi == ELFOSABI_GNU
1014 || bed->elf_osabi == ELFOSABI_FREEBSD)
1015 {
1016 /* Add flags in the SHF_MASKOS range to gnu_attr for
1017 OSABIs that support those flags.
1018 Also adding the flags for ELFOSABI_{NONE,STANDALONE}
1019 allows them to be validated later in obj_elf_section.
1020 We can't just always set these bits in gnu_attr for
1021 all OSABIs, since Binutils does not recognize all
1022 SHF_MASKOS bits for non-GNU OSABIs. It's therefore
1023 possible that numeric flags are being used to set bits
1024 in the SHF_MASKOS range for those targets, and we
1025 don't want assembly to fail in those situations. */
1026 *gnu_attr |= (numeric_flags & SHF_MASKOS);
1027 }
1028
1029 /* Update str and len, allowing for the fact that
1030 we will execute str++ and len-- below. */
1031 end --;
1032 len -= (end - str);
1033 str = end;
1034 }
1035 else if (!attr && !*gnu_attr && (*str == '+' || *str == '-'))
1036 *inherit = *str == '+' ? 1 : -1;
1037 else
1038 as_fatal ("%s", bad_msg);
1039 }
1040 break;
1041 }
1042 str++, len--;
1043 }
1044
1045 return attr;
1046 }
1047
1048 static int
1049 obj_elf_section_type (char *str, size_t len, bool warn)
1050 {
1051 if (len == 8 && startswith (str, "progbits"))
1052 return SHT_PROGBITS;
1053 if (len == 6 && startswith (str, "nobits"))
1054 return SHT_NOBITS;
1055 if (len == 4 && startswith (str, "note"))
1056 return SHT_NOTE;
1057 if (len == 10 && startswith (str, "init_array"))
1058 return SHT_INIT_ARRAY;
1059 if (len == 10 && startswith (str, "fini_array"))
1060 return SHT_FINI_ARRAY;
1061 if (len == 13 && startswith (str, "preinit_array"))
1062 return SHT_PREINIT_ARRAY;
1063
1064 #ifdef md_elf_section_type
1065 {
1066 int md_type = md_elf_section_type (str, len);
1067 if (md_type >= 0)
1068 return md_type;
1069 }
1070 #endif
1071
1072 if (ISDIGIT (*str))
1073 {
1074 char * end;
1075 int type = strtoul (str, & end, 0);
1076
1077 if (warn && (size_t) (end - str) != len)
1078 as_warn (_("extraneous characters at end of numeric section type"));
1079
1080 return type;
1081 }
1082
1083 if (warn)
1084 as_warn (_("unrecognized section type"));
1085 return 0;
1086 }
1087
1088 #ifdef TC_SPARC
1089 static bfd_vma
1090 obj_elf_section_word (char *str, size_t len, int *type)
1091 {
1092 int ret;
1093
1094 if (len == 5 && startswith (str, "write"))
1095 return SHF_WRITE;
1096 if (len == 5 && startswith (str, "alloc"))
1097 return SHF_ALLOC;
1098 if (len == 9 && startswith (str, "execinstr"))
1099 return SHF_EXECINSTR;
1100 if (len == 7 && startswith (str, "exclude"))
1101 return SHF_EXCLUDE;
1102 if (len == 3 && startswith (str, "tls"))
1103 return SHF_TLS;
1104
1105 ret = obj_elf_section_type (str, len, false);
1106 if (ret != 0)
1107 *type = ret;
1108 else
1109 as_warn (_("unrecognized section attribute"));
1110
1111 return 0;
1112 }
1113 #endif
1114
1115 /* Get name of section. */
1116 const char *
1117 obj_elf_section_name (void)
1118 {
1119 char *name;
1120
1121 SKIP_WHITESPACE ();
1122 if (*input_line_pointer == '"')
1123 {
1124 int dummy;
1125
1126 name = demand_copy_C_string (&dummy);
1127 if (name == NULL)
1128 {
1129 ignore_rest_of_line ();
1130 return NULL;
1131 }
1132 }
1133 else
1134 {
1135 char *end = input_line_pointer;
1136
1137 while (0 == strchr ("\n\t,; ", *end))
1138 end++;
1139 if (end == input_line_pointer)
1140 {
1141 as_bad (_("missing name"));
1142 ignore_rest_of_line ();
1143 return NULL;
1144 }
1145
1146 obstack_grow0 (&notes, input_line_pointer, end - input_line_pointer);
1147 name = obstack_base (&notes);
1148
1149 while (flag_sectname_subst)
1150 {
1151 char *subst = strchr (name, '%');
1152 if (subst && subst[1] == 'S')
1153 {
1154 size_t head = subst - name;
1155 size_t tail = strlen (subst + 2) + 1;
1156 size_t slen = strlen (now_seg->name);
1157
1158 if (slen > 2)
1159 {
1160 obstack_blank (&notes, slen - 2);
1161 name = obstack_base (&notes);
1162 }
1163 memmove (name + head + slen, name + head + 2, tail);
1164 memcpy (name + head, now_seg->name, slen);
1165 }
1166 else
1167 break;
1168 }
1169
1170 obstack_finish (&notes);
1171
1172 #ifdef tc_canonicalize_section_name
1173 name = tc_canonicalize_section_name (name);
1174 #endif
1175 input_line_pointer = end;
1176 }
1177 SKIP_WHITESPACE ();
1178 return name;
1179 }
1180
1181 /* Arrange to put SEC, known to be in group GNAME into the per-group
1182 section lists kept by gas. */
1183
1184 void
1185 elf_set_group_name (asection *sec, const char *gname)
1186 {
1187 elf_group_name (sec) = gname;
1188 elf_section_flags (sec) |= SHF_GROUP;
1189
1190 struct elf_section_match match;
1191 match.group_name = gname;
1192 match.linked_to_symbol_name = sec->map_head.linked_to_symbol_name;
1193 match.section_id = sec->section_id;
1194 match.sh_info = elf_section_data (sec)->this_hdr.sh_info;
1195 match.sh_flags = (elf_section_data (sec)->this_hdr.sh_flags
1196 & SEC_ASSEMBLER_SHF_MASK);
1197 match.flags = bfd_section_flags (sec) & SEC_ASSEMBLER_SECTION_ID;
1198
1199 unsigned int *group_idx;
1200 if (!group_section_find (&match, sec->name, &group_idx))
1201 group_section_insert (&match, sec, &group_idx);
1202 }
1203
1204 static void
1205 obj_elf_attach_to_group (int dummy ATTRIBUTE_UNUSED)
1206 {
1207 const char * gname = obj_elf_section_name ();
1208
1209 if (gname == NULL)
1210 {
1211 as_warn (_("group name not parseable"));
1212 return;
1213 }
1214
1215 if (elf_group_name (now_seg))
1216 {
1217 if (strcmp (elf_group_name (now_seg), gname) != 0)
1218 as_warn (_("section %s already has a group (%s)"),
1219 bfd_section_name (now_seg), elf_group_name (now_seg));
1220 return;
1221 }
1222 elf_set_group_name (now_seg, gname);
1223 }
1224
1225 void
1226 obj_elf_section (int push)
1227 {
1228 const char *name;
1229 char *beg;
1230 int type, dummy;
1231 bfd_vma attr;
1232 bfd_vma gnu_attr;
1233 int entsize;
1234 bool linkonce;
1235 subsegT new_subsection = 0;
1236 struct elf_section_match match;
1237 unsigned long linked_to_section_index = -1UL;
1238
1239 if (flag_mri)
1240 {
1241 char mri_type;
1242
1243 #ifdef md_flush_pending_output
1244 md_flush_pending_output ();
1245 #endif
1246
1247 obj_elf_section_change_hook ();
1248
1249 s_mri_sect (&mri_type);
1250
1251 #ifdef md_elf_section_change_hook
1252 md_elf_section_change_hook ();
1253 #endif
1254
1255 return;
1256 }
1257
1258 name = obj_elf_section_name ();
1259 if (name == NULL)
1260 return;
1261
1262 memset (&match, 0, sizeof (match));
1263
1264 symbolS * sym;
1265 if ((sym = symbol_find (name)) != NULL
1266 && ! symbol_section_p (sym)
1267 && S_IS_DEFINED (sym)
1268 && ! S_IS_VOLATILE (sym)
1269 && ! S_CAN_BE_REDEFINED (sym))
1270 {
1271 as_bad (_("section name '%s' already defined as another symbol"), name);
1272 ignore_rest_of_line ();
1273 return;
1274 }
1275 type = SHT_NULL;
1276 attr = 0;
1277 gnu_attr = 0;
1278 entsize = 0;
1279 linkonce = 0;
1280
1281 if (*input_line_pointer == ',')
1282 {
1283 /* Skip the comma. */
1284 ++input_line_pointer;
1285 SKIP_WHITESPACE ();
1286
1287 if (push && ISDIGIT (*input_line_pointer))
1288 {
1289 /* .pushsection has an optional subsection. */
1290 new_subsection = (subsegT) get_absolute_expression ();
1291
1292 SKIP_WHITESPACE ();
1293
1294 /* Stop if we don't see a comma. */
1295 if (*input_line_pointer != ',')
1296 goto done;
1297
1298 /* Skip the comma. */
1299 ++input_line_pointer;
1300 SKIP_WHITESPACE ();
1301 }
1302
1303 if (*input_line_pointer == '"')
1304 {
1305 bool is_clone;
1306 int inherit;
1307
1308 beg = demand_copy_C_string (&dummy);
1309 if (beg == NULL)
1310 {
1311 ignore_rest_of_line ();
1312 return;
1313 }
1314 attr = obj_elf_parse_section_letters (beg, strlen (beg), &is_clone,
1315 &inherit, &gnu_attr);
1316
1317 if (inherit > 0)
1318 attr |= elf_section_flags (now_seg);
1319 else if (inherit < 0)
1320 attr = elf_section_flags (now_seg) & ~attr;
1321 if (inherit)
1322 type = elf_section_type (now_seg);
1323
1324 SKIP_WHITESPACE ();
1325 if (*input_line_pointer == ',')
1326 {
1327 char c;
1328 char *save = input_line_pointer;
1329
1330 ++input_line_pointer;
1331 SKIP_WHITESPACE ();
1332 c = *input_line_pointer;
1333 if (c == '"')
1334 {
1335 beg = demand_copy_C_string (&dummy);
1336 if (beg == NULL)
1337 {
1338 ignore_rest_of_line ();
1339 return;
1340 }
1341 type = obj_elf_section_type (beg, strlen (beg), true);
1342 }
1343 else if (c == '@' || c == '%')
1344 {
1345 ++input_line_pointer;
1346
1347 if (ISDIGIT (* input_line_pointer))
1348 type = strtoul (input_line_pointer, &input_line_pointer, 0);
1349 else
1350 {
1351 c = get_symbol_name (& beg);
1352 (void) restore_line_pointer (c);
1353 type = obj_elf_section_type (beg,
1354 input_line_pointer - beg,
1355 true);
1356 }
1357 }
1358 else
1359 input_line_pointer = save;
1360 }
1361
1362 SKIP_WHITESPACE ();
1363 if ((attr & SHF_MERGE) != 0 && *input_line_pointer == ',')
1364 {
1365 ++input_line_pointer;
1366 SKIP_WHITESPACE ();
1367 if (inherit && *input_line_pointer == ','
1368 && (bfd_section_flags (now_seg) & SEC_MERGE) != 0)
1369 goto fetch_entsize;
1370 entsize = get_absolute_expression ();
1371 SKIP_WHITESPACE ();
1372 if (entsize < 0)
1373 {
1374 as_warn (_("invalid merge entity size"));
1375 attr &= ~SHF_MERGE;
1376 entsize = 0;
1377 }
1378 }
1379 else if ((attr & SHF_MERGE) != 0 && inherit
1380 && (bfd_section_flags (now_seg) & SEC_MERGE) != 0)
1381 {
1382 fetch_entsize:
1383 entsize = now_seg->entsize;
1384 }
1385 else if ((attr & SHF_MERGE) != 0)
1386 {
1387 as_warn (_("entity size for SHF_MERGE not specified"));
1388 attr &= ~SHF_MERGE;
1389 }
1390
1391 if ((attr & SHF_LINK_ORDER) != 0 && *input_line_pointer == ',')
1392 {
1393 ++input_line_pointer;
1394 SKIP_WHITESPACE ();
1395 /* Check for a numeric section index, rather than a symbol name. */
1396 if (ISDIGIT (* input_line_pointer))
1397 {
1398 linked_to_section_index = strtoul (input_line_pointer, & input_line_pointer, 0);
1399 }
1400 else if (inherit && *input_line_pointer == ','
1401 && (elf_section_flags (now_seg) & SHF_LINK_ORDER) != 0)
1402 goto fetch_linked_to;
1403 else
1404 {
1405 char c;
1406 unsigned int length;
1407
1408 c = get_symbol_name (& beg);
1409 (void) restore_line_pointer (c);
1410 length = input_line_pointer - beg;
1411 if (length)
1412 match.linked_to_symbol_name = xmemdup0 (beg, length);
1413 }
1414 }
1415 else if ((attr & SHF_LINK_ORDER) != 0 && inherit
1416 && (elf_section_flags (now_seg) & SHF_LINK_ORDER) != 0)
1417 {
1418 fetch_linked_to:
1419 if (now_seg->map_head.linked_to_symbol_name)
1420 match.linked_to_symbol_name =
1421 now_seg->map_head.linked_to_symbol_name;
1422 else
1423 linked_to_section_index =
1424 elf_section_data (now_seg)->this_hdr.sh_link;
1425 }
1426
1427 if ((attr & SHF_GROUP) != 0 && is_clone)
1428 {
1429 as_warn (_("? section flag ignored with G present"));
1430 is_clone = false;
1431 }
1432
1433 if ((attr & SHF_GROUP) != 0 && *input_line_pointer == ',')
1434 {
1435 ++input_line_pointer;
1436 SKIP_WHITESPACE ();
1437 if (inherit && *input_line_pointer == ','
1438 && (elf_section_flags (now_seg) & SHF_GROUP) != 0)
1439 goto fetch_group;
1440 match.group_name = obj_elf_section_name ();
1441 if (match.group_name == NULL)
1442 attr &= ~SHF_GROUP;
1443 else if (*input_line_pointer == ',')
1444 {
1445 ++input_line_pointer;
1446 SKIP_WHITESPACE ();
1447 if (startswith (input_line_pointer, "comdat"))
1448 {
1449 input_line_pointer += 6;
1450 linkonce = 1;
1451 }
1452 }
1453 else if (startswith (name, ".gnu.linkonce"))
1454 linkonce = 1;
1455 }
1456 else if ((attr & SHF_GROUP) != 0 && inherit
1457 && (elf_section_flags (now_seg) & SHF_GROUP) != 0)
1458 {
1459 fetch_group:
1460 match.group_name = elf_group_name (now_seg);
1461 linkonce =
1462 (bfd_section_flags (now_seg) & SEC_LINK_ONCE) != 0;
1463 }
1464 else if ((attr & SHF_GROUP) != 0)
1465 {
1466 as_warn (_("group name for SHF_GROUP not specified"));
1467 attr &= ~SHF_GROUP;
1468 }
1469
1470 if (is_clone)
1471 {
1472 const char *now_group = elf_group_name (now_seg);
1473 if (now_group != NULL)
1474 {
1475 match.group_name = now_group;
1476 linkonce = (now_seg->flags & SEC_LINK_ONCE) != 0;
1477 }
1478 }
1479
1480 if ((gnu_attr & SHF_GNU_MBIND) != 0 && *input_line_pointer == ',')
1481 {
1482 char *save = input_line_pointer;
1483 ++input_line_pointer;
1484 SKIP_WHITESPACE ();
1485 if (ISDIGIT (* input_line_pointer))
1486 {
1487 char *t = input_line_pointer;
1488 match.sh_info = strtoul (input_line_pointer,
1489 &input_line_pointer, 0);
1490 if (match.sh_info == (unsigned int) -1)
1491 {
1492 as_warn (_("unsupported mbind section info: %s"), t);
1493 match.sh_info = 0;
1494 }
1495 }
1496 else
1497 input_line_pointer = save;
1498 }
1499
1500 if ((gnu_attr & SHF_GNU_RETAIN) != 0)
1501 match.sh_flags |= SHF_GNU_RETAIN;
1502
1503 if (*input_line_pointer == ',')
1504 {
1505 char *save = input_line_pointer;
1506
1507 ++input_line_pointer;
1508 SKIP_WHITESPACE ();
1509 if (startswith (input_line_pointer, "unique"))
1510 {
1511 input_line_pointer += 6;
1512 SKIP_WHITESPACE ();
1513 if (*input_line_pointer == ',')
1514 {
1515 ++input_line_pointer;
1516 SKIP_WHITESPACE ();
1517 if (ISDIGIT (* input_line_pointer))
1518 {
1519 bfd_vma id;
1520 bool overflow;
1521 char *t = input_line_pointer;
1522 if (sizeof (bfd_vma) <= sizeof (unsigned long))
1523 {
1524 errno = 0;
1525 id = strtoul (input_line_pointer,
1526 &input_line_pointer, 0);
1527 overflow = (id == (unsigned long) -1
1528 && errno == ERANGE);
1529 }
1530 else
1531 {
1532 id = bfd_scan_vma
1533 (input_line_pointer,
1534 (const char **) &input_line_pointer, 0);
1535 overflow = id == ~(bfd_vma) 0;
1536 }
1537 if (overflow || id > (unsigned int) -1)
1538 {
1539 char *linefeed, saved_char = 0;
1540 if ((linefeed = strchr (t, '\n')) != NULL)
1541 {
1542 saved_char = *linefeed;
1543 *linefeed = '\0';
1544 }
1545 as_bad (_("unsupported section id: %s"), t);
1546 if (saved_char)
1547 *linefeed = saved_char;
1548 }
1549 else
1550 {
1551 match.section_id = id;
1552 match.flags |= SEC_ASSEMBLER_SECTION_ID;
1553 }
1554 }
1555 }
1556 }
1557 else
1558 input_line_pointer = save;
1559 }
1560 }
1561 #ifdef TC_SPARC
1562 else
1563 {
1564 do
1565 {
1566 char c;
1567
1568 SKIP_WHITESPACE ();
1569 if (*input_line_pointer != '#')
1570 {
1571 as_bad (_("character following name is not '#'"));
1572 ignore_rest_of_line ();
1573 return;
1574 }
1575 ++input_line_pointer;
1576 c = get_symbol_name (& beg);
1577 (void) restore_line_pointer (c);
1578
1579 attr |= obj_elf_section_word (beg, input_line_pointer - beg,
1580 &type);
1581
1582 SKIP_WHITESPACE ();
1583 }
1584 while (*input_line_pointer++ == ',');
1585 --input_line_pointer;
1586 }
1587 #endif
1588 }
1589
1590 done:
1591 demand_empty_rest_of_line ();
1592
1593 if ((gnu_attr & (SHF_GNU_MBIND | SHF_GNU_RETAIN)) != 0)
1594 {
1595 const struct elf_backend_data *bed;
1596 bool mbind_p = (gnu_attr & SHF_GNU_MBIND) != 0;
1597
1598 if (mbind_p && (attr & SHF_ALLOC) == 0)
1599 as_bad (_("SHF_ALLOC isn't set for GNU_MBIND section: %s"), name);
1600
1601 bed = get_elf_backend_data (stdoutput);
1602
1603 if (bed->elf_osabi != ELFOSABI_GNU
1604 && bed->elf_osabi != ELFOSABI_FREEBSD
1605 && bed->elf_osabi != ELFOSABI_NONE)
1606 as_bad (_("%s section is supported only by GNU and FreeBSD targets"),
1607 mbind_p ? "GNU_MBIND" : "GNU_RETAIN");
1608 else
1609 {
1610 if (mbind_p)
1611 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1612 if ((gnu_attr & SHF_GNU_RETAIN) != 0)
1613 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_retain;
1614
1615 attr |= gnu_attr;
1616 }
1617 }
1618
1619 change_section (name, type, attr, entsize, &match, linkonce, push,
1620 new_subsection);
1621
1622 if (linked_to_section_index != -1UL)
1623 {
1624 elf_section_flags (now_seg) |= SHF_LINK_ORDER;
1625 elf_section_data (now_seg)->this_hdr.sh_link = linked_to_section_index;
1626 /* FIXME: Should we perform some sanity checking on the section index ? */
1627 }
1628 }
1629
1630 /* Change to the .bss section. */
1631
1632 void
1633 obj_elf_bss (int i ATTRIBUTE_UNUSED)
1634 {
1635 int temp;
1636
1637 #ifdef md_flush_pending_output
1638 md_flush_pending_output ();
1639 #endif
1640
1641 obj_elf_section_change_hook ();
1642
1643 temp = get_absolute_expression ();
1644 subseg_set (bss_section, (subsegT) temp);
1645 demand_empty_rest_of_line ();
1646
1647 #ifdef md_elf_section_change_hook
1648 md_elf_section_change_hook ();
1649 #endif
1650 }
1651
1652 /* Change to the .data section. */
1653
1654 void
1655 obj_elf_data (int i)
1656 {
1657 #ifdef md_flush_pending_output
1658 md_flush_pending_output ();
1659 #endif
1660
1661 obj_elf_section_change_hook ();
1662
1663 s_data (i);
1664
1665 #ifdef md_elf_section_change_hook
1666 md_elf_section_change_hook ();
1667 #endif
1668 }
1669
1670 /* Change to the .text section. */
1671
1672 void
1673 obj_elf_text (int i)
1674 {
1675 #ifdef md_flush_pending_output
1676 md_flush_pending_output ();
1677 #endif
1678
1679 obj_elf_section_change_hook ();
1680
1681 s_text (i);
1682
1683 #ifdef md_elf_section_change_hook
1684 md_elf_section_change_hook ();
1685 #endif
1686 }
1687
1688 /* Change to the *ABS* section. */
1689
1690 void
1691 obj_elf_struct (int i)
1692 {
1693 #ifdef md_flush_pending_output
1694 md_flush_pending_output ();
1695 #endif
1696
1697 obj_elf_section_change_hook ();
1698
1699 s_struct (i);
1700
1701 #ifdef md_elf_section_change_hook
1702 md_elf_section_change_hook ();
1703 #endif
1704 }
1705
1706 static void
1707 obj_elf_subsection (int ignore ATTRIBUTE_UNUSED)
1708 {
1709 int temp;
1710
1711 #ifdef md_flush_pending_output
1712 md_flush_pending_output ();
1713 #endif
1714
1715 obj_elf_section_change_hook ();
1716
1717 temp = get_absolute_expression ();
1718 subseg_set (now_seg, (subsegT) temp);
1719 demand_empty_rest_of_line ();
1720
1721 #ifdef md_elf_section_change_hook
1722 md_elf_section_change_hook ();
1723 #endif
1724 }
1725
1726 /* This can be called from the processor backends if they change
1727 sections. */
1728
1729 void
1730 obj_elf_section_change_hook (void)
1731 {
1732 previous_section = now_seg;
1733 previous_subsection = now_subseg;
1734 }
1735
1736 void
1737 obj_elf_previous (int ignore ATTRIBUTE_UNUSED)
1738 {
1739 segT new_section;
1740 int new_subsection;
1741
1742 if (previous_section == 0)
1743 {
1744 as_warn (_(".previous without corresponding .section; ignored"));
1745 return;
1746 }
1747
1748 #ifdef md_flush_pending_output
1749 md_flush_pending_output ();
1750 #endif
1751
1752 new_section = previous_section;
1753 new_subsection = previous_subsection;
1754 obj_elf_section_change_hook ();
1755
1756 subseg_set (new_section, new_subsection);
1757
1758 #ifdef md_elf_section_change_hook
1759 md_elf_section_change_hook ();
1760 #endif
1761 }
1762
1763 static void
1764 obj_elf_popsection (int xxx ATTRIBUTE_UNUSED)
1765 {
1766 struct section_stack *top = section_stack;
1767
1768 if (top == NULL)
1769 {
1770 as_warn (_(".popsection without corresponding .pushsection; ignored"));
1771 return;
1772 }
1773
1774 #ifdef md_flush_pending_output
1775 md_flush_pending_output ();
1776 #endif
1777
1778 section_stack = top->next;
1779 previous_section = top->prev_seg;
1780 previous_subsection = top->prev_subseg;
1781 subseg_set (top->seg, top->subseg);
1782 free (top);
1783
1784 #ifdef md_elf_section_change_hook
1785 md_elf_section_change_hook ();
1786 #endif
1787 }
1788
1789 static void
1790 obj_elf_line (int ignore ATTRIBUTE_UNUSED)
1791 {
1792 /* Assume delimiter is part of expression. BSD4.2 as fails with
1793 delightful bug, so we are not being incompatible here. */
1794 new_logical_line (NULL, get_absolute_expression ());
1795 demand_empty_rest_of_line ();
1796 }
1797
1798 static struct elf_versioned_name_list *
1799 obj_elf_find_and_add_versioned_name (const char *version_name,
1800 const char *sym_name,
1801 const char *ver,
1802 struct elf_obj_sy *sy_obj)
1803 {
1804 struct elf_versioned_name_list *versioned_name;
1805 const char *p;
1806
1807 for (p = ver + 1; *p == ELF_VER_CHR; p++)
1808 ;
1809
1810 /* NB: Since some tests in ld/testsuite/ld-elfvers have no version
1811 names, we have to disable this. */
1812 if (0 && *p == '\0')
1813 {
1814 as_bad (_("missing version name in `%s' for symbol `%s'"),
1815 version_name, sym_name);
1816 return NULL;
1817 }
1818
1819 versioned_name = sy_obj->versioned_name;
1820
1821 switch (p - ver)
1822 {
1823 case 1:
1824 case 2:
1825 break;
1826 case 3:
1827 if (sy_obj->rename)
1828 {
1829 if (strcmp (versioned_name->name, version_name) == 0)
1830 return versioned_name;
1831 else
1832 {
1833 as_bad (_("only one version name with `@@@' is allowed "
1834 "for symbol `%s'"), sym_name);
1835 return NULL;
1836 }
1837 }
1838 sy_obj->rename = true;
1839 break;
1840 default:
1841 as_bad (_("invalid version name '%s' for symbol `%s'"),
1842 version_name, sym_name);
1843 return NULL;
1844 }
1845
1846 for (;
1847 versioned_name != NULL;
1848 versioned_name = versioned_name->next)
1849 if (strcmp (versioned_name->name, version_name) == 0)
1850 return versioned_name;
1851
1852 /* Add this versioned name to the head of the list, */
1853 versioned_name = (struct elf_versioned_name_list *)
1854 xmalloc (sizeof (*versioned_name));
1855 versioned_name->name = xstrdup (version_name);
1856 versioned_name->next = sy_obj->versioned_name;
1857 sy_obj->versioned_name = versioned_name;
1858
1859 return versioned_name;
1860 }
1861
1862 /* This handles the .symver pseudo-op, which is used to specify a
1863 symbol version. The syntax is ``.symver NAME,SYMVERNAME''.
1864 SYMVERNAME may contain ELF_VER_CHR ('@') characters. This
1865 pseudo-op causes the assembler to emit a symbol named SYMVERNAME
1866 with the same value as the symbol NAME. */
1867
1868 static void
1869 obj_elf_symver (int ignore ATTRIBUTE_UNUSED)
1870 {
1871 char *name;
1872 const char *sym_name;
1873 char c;
1874 char old_lexat;
1875 symbolS *sym;
1876 struct elf_obj_sy *sy_obj;
1877 char *p;
1878
1879 sym = get_sym_from_input_line_and_check ();
1880
1881 if (*input_line_pointer != ',')
1882 {
1883 as_bad (_("expected comma after name in .symver"));
1884 ignore_rest_of_line ();
1885 return;
1886 }
1887
1888 ++input_line_pointer;
1889 SKIP_WHITESPACE ();
1890
1891 /* Temporarily include '@' in symbol names. */
1892 old_lexat = lex_type[(unsigned char) '@'];
1893 lex_type[(unsigned char) '@'] |= LEX_NAME;
1894 c = get_symbol_name (& name);
1895 lex_type[(unsigned char) '@'] = old_lexat;
1896 sym_name = S_GET_NAME (sym);
1897
1898 if (S_IS_COMMON (sym))
1899 {
1900 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
1901 name, sym_name);
1902 ignore_rest_of_line ();
1903 return;
1904 }
1905
1906 p = strchr (name, ELF_VER_CHR);
1907 if (p == NULL)
1908 {
1909 as_bad (_("missing version name in `%s' for symbol `%s'"),
1910 name, sym_name);
1911 ignore_rest_of_line ();
1912 return;
1913 }
1914
1915 sy_obj = symbol_get_obj (sym);
1916 if (obj_elf_find_and_add_versioned_name (name, sym_name,
1917 p, sy_obj) == NULL)
1918 {
1919 sy_obj->bad_version = true;
1920 ignore_rest_of_line ();
1921 return;
1922 }
1923
1924 (void) restore_line_pointer (c);
1925
1926 if (*input_line_pointer == ',')
1927 {
1928 char *save = input_line_pointer;
1929
1930 ++input_line_pointer;
1931 SKIP_WHITESPACE ();
1932 if (startswith (input_line_pointer, "local"))
1933 {
1934 input_line_pointer += 5;
1935 sy_obj->visibility = visibility_local;
1936 }
1937 else if (startswith (input_line_pointer, "hidden"))
1938 {
1939 input_line_pointer += 6;
1940 sy_obj->visibility = visibility_hidden;
1941 }
1942 else if (startswith (input_line_pointer, "remove"))
1943 {
1944 input_line_pointer += 6;
1945 sy_obj->visibility = visibility_remove;
1946 }
1947 else
1948 input_line_pointer = save;
1949 }
1950
1951 demand_empty_rest_of_line ();
1952 }
1953
1954 /* This handles the .vtable_inherit pseudo-op, which is used to indicate
1955 to the linker the hierarchy in which a particular table resides. The
1956 syntax is ".vtable_inherit CHILDNAME, PARENTNAME". */
1957
1958 struct fix *
1959 obj_elf_get_vtable_inherit (void)
1960 {
1961 char *cname, *pname;
1962 symbolS *csym, *psym;
1963 char c, bad = 0;
1964
1965 if (*input_line_pointer == '#')
1966 ++input_line_pointer;
1967
1968 c = get_symbol_name (& cname);
1969 csym = symbol_find (cname);
1970
1971 /* GCFIXME: should check that we don't have two .vtable_inherits for
1972 the same child symbol. Also, we can currently only do this if the
1973 child symbol is already exists and is placed in a fragment. */
1974
1975 if (csym == NULL || symbol_get_frag (csym) == NULL)
1976 {
1977 as_bad (_("expected `%s' to have already been set for .vtable_inherit"),
1978 cname);
1979 bad = 1;
1980 }
1981
1982 *input_line_pointer = c;
1983
1984 SKIP_WHITESPACE_AFTER_NAME ();
1985 if (*input_line_pointer != ',')
1986 {
1987 as_bad (_("expected comma after name in .vtable_inherit"));
1988 ignore_rest_of_line ();
1989 return NULL;
1990 }
1991
1992 ++input_line_pointer;
1993 SKIP_WHITESPACE ();
1994
1995 if (*input_line_pointer == '#')
1996 ++input_line_pointer;
1997
1998 if (input_line_pointer[0] == '0'
1999 && (input_line_pointer[1] == '\0'
2000 || ISSPACE (input_line_pointer[1])))
2001 {
2002 psym = section_symbol (absolute_section);
2003 ++input_line_pointer;
2004 }
2005 else
2006 {
2007 c = get_symbol_name (& pname);
2008 psym = symbol_find_or_make (pname);
2009 restore_line_pointer (c);
2010 }
2011
2012 demand_empty_rest_of_line ();
2013
2014 if (bad)
2015 return NULL;
2016
2017 gas_assert (symbol_get_value_expression (csym)->X_op == O_constant);
2018 return fix_new (symbol_get_frag (csym),
2019 symbol_get_value_expression (csym)->X_add_number,
2020 0, psym, 0, 0, BFD_RELOC_VTABLE_INHERIT);
2021 }
2022
2023 /* This is a version of obj_elf_get_vtable_inherit() that is
2024 suitable for use in struct _pseudo_type tables. */
2025
2026 void
2027 obj_elf_vtable_inherit (int ignore ATTRIBUTE_UNUSED)
2028 {
2029 (void) obj_elf_get_vtable_inherit ();
2030 }
2031
2032 /* This handles the .vtable_entry pseudo-op, which is used to indicate
2033 to the linker that a vtable slot was used. The syntax is
2034 ".vtable_entry tablename, offset". */
2035
2036 struct fix *
2037 obj_elf_get_vtable_entry (void)
2038 {
2039 symbolS *sym;
2040 offsetT offset;
2041
2042 if (*input_line_pointer == '#')
2043 ++input_line_pointer;
2044
2045 sym = get_sym_from_input_line_and_check ();
2046 if (*input_line_pointer != ',')
2047 {
2048 as_bad (_("expected comma after name in .vtable_entry"));
2049 ignore_rest_of_line ();
2050 return NULL;
2051 }
2052
2053 ++input_line_pointer;
2054 if (*input_line_pointer == '#')
2055 ++input_line_pointer;
2056
2057 offset = get_absolute_expression ();
2058
2059 demand_empty_rest_of_line ();
2060
2061 return fix_new (frag_now, frag_now_fix (), 0, sym, offset, 0,
2062 BFD_RELOC_VTABLE_ENTRY);
2063 }
2064
2065 /* This is a version of obj_elf_get_vtable_entry() that is
2066 suitable for use in struct _pseudo_type tables. */
2067
2068 void
2069 obj_elf_vtable_entry (int ignore ATTRIBUTE_UNUSED)
2070 {
2071 (void) obj_elf_get_vtable_entry ();
2072 }
2073
2074 #define skip_whitespace(str) do { if (*(str) == ' ') ++(str); } while (0)
2075
2076 static inline int
2077 skip_past_char (char ** str, char c)
2078 {
2079 if (**str == c)
2080 {
2081 (*str)++;
2082 return 0;
2083 }
2084 else
2085 return -1;
2086 }
2087 #define skip_past_comma(str) skip_past_char (str, ',')
2088
2089 /* A list of attributes that have been explicitly set by the assembly code.
2090 VENDOR is the vendor id, BASE is the tag shifted right by the number
2091 of bits in MASK, and bit N of MASK is set if tag BASE+N has been set. */
2092 struct recorded_attribute_info {
2093 struct recorded_attribute_info *next;
2094 int vendor;
2095 unsigned int base;
2096 unsigned long mask;
2097 };
2098 static struct recorded_attribute_info *recorded_attributes;
2099
2100 /* Record that we have seen an explicit specification of attribute TAG
2101 for vendor VENDOR. */
2102
2103 static void
2104 record_attribute (int vendor, unsigned int tag)
2105 {
2106 unsigned int base;
2107 unsigned long mask;
2108 struct recorded_attribute_info *rai;
2109
2110 base = tag / (8 * sizeof (rai->mask));
2111 mask = 1UL << (tag % (8 * sizeof (rai->mask)));
2112 for (rai = recorded_attributes; rai; rai = rai->next)
2113 if (rai->vendor == vendor && rai->base == base)
2114 {
2115 rai->mask |= mask;
2116 return;
2117 }
2118
2119 rai = XNEW (struct recorded_attribute_info);
2120 rai->next = recorded_attributes;
2121 rai->vendor = vendor;
2122 rai->base = base;
2123 rai->mask = mask;
2124 recorded_attributes = rai;
2125 }
2126
2127 /* Return true if we have seen an explicit specification of attribute TAG
2128 for vendor VENDOR. */
2129
2130 bool
2131 obj_elf_seen_attribute (int vendor, unsigned int tag)
2132 {
2133 unsigned int base;
2134 unsigned long mask;
2135 struct recorded_attribute_info *rai;
2136
2137 base = tag / (8 * sizeof (rai->mask));
2138 mask = 1UL << (tag % (8 * sizeof (rai->mask)));
2139 for (rai = recorded_attributes; rai; rai = rai->next)
2140 if (rai->vendor == vendor && rai->base == base)
2141 return (rai->mask & mask) != 0;
2142 return false;
2143 }
2144
2145 /* Parse an attribute directive for VENDOR.
2146 Returns the attribute number read, or zero on error. */
2147
2148 int
2149 obj_elf_vendor_attribute (int vendor)
2150 {
2151 expressionS exp;
2152 int type;
2153 int tag;
2154 unsigned int i = 0;
2155 char *s = NULL;
2156
2157 /* Read the first number or name. */
2158 skip_whitespace (input_line_pointer);
2159 s = input_line_pointer;
2160 if (ISDIGIT (*input_line_pointer))
2161 {
2162 expression (& exp);
2163 if (exp.X_op != O_constant)
2164 goto bad;
2165 tag = exp.X_add_number;
2166 }
2167 else
2168 {
2169 char *name;
2170
2171 /* A name may contain '_', but no other punctuation. */
2172 for (; ISALNUM (*input_line_pointer) || *input_line_pointer == '_';
2173 ++input_line_pointer)
2174 i++;
2175 if (i == 0)
2176 goto bad;
2177
2178 name = xmemdup0 (s, i);
2179
2180 #ifndef CONVERT_SYMBOLIC_ATTRIBUTE
2181 #define CONVERT_SYMBOLIC_ATTRIBUTE(a) -1
2182 #endif
2183
2184 tag = CONVERT_SYMBOLIC_ATTRIBUTE (name);
2185 if (tag == -1)
2186 {
2187 as_bad (_("Attribute name not recognised: %s"), name);
2188 ignore_rest_of_line ();
2189 free (name);
2190 return 0;
2191 }
2192 free (name);
2193 }
2194
2195 type = _bfd_elf_obj_attrs_arg_type (stdoutput, vendor, tag);
2196
2197 if (skip_past_comma (&input_line_pointer) == -1)
2198 goto bad;
2199 if (type & 1)
2200 {
2201 expression (& exp);
2202 if (exp.X_op != O_constant)
2203 {
2204 as_bad (_("expected numeric constant"));
2205 ignore_rest_of_line ();
2206 return 0;
2207 }
2208 i = exp.X_add_number;
2209 }
2210 if ((type & 3) == 3
2211 && skip_past_comma (&input_line_pointer) == -1)
2212 {
2213 as_bad (_("expected comma"));
2214 ignore_rest_of_line ();
2215 return 0;
2216 }
2217 if (type & 2)
2218 {
2219 int len;
2220
2221 skip_whitespace (input_line_pointer);
2222 if (*input_line_pointer != '"')
2223 goto bad_string;
2224 s = demand_copy_C_string (&len);
2225 }
2226
2227 record_attribute (vendor, tag);
2228 bool ok = false;
2229 switch (type & 3)
2230 {
2231 case 3:
2232 ok = bfd_elf_add_obj_attr_int_string (stdoutput, vendor, tag, i, s);
2233 break;
2234 case 2:
2235 ok = bfd_elf_add_obj_attr_string (stdoutput, vendor, tag, s);
2236 break;
2237 case 1:
2238 ok = bfd_elf_add_obj_attr_int (stdoutput, vendor, tag, i);
2239 break;
2240 default:
2241 abort ();
2242 }
2243 if (!ok)
2244 as_fatal (_("error adding attribute: %s"),
2245 bfd_errmsg (bfd_get_error ()));
2246
2247 demand_empty_rest_of_line ();
2248 return tag;
2249 bad_string:
2250 as_bad (_("bad string constant"));
2251 ignore_rest_of_line ();
2252 return 0;
2253 bad:
2254 as_bad (_("expected <tag> , <value>"));
2255 ignore_rest_of_line ();
2256 return 0;
2257 }
2258
2259 /* Parse a .gnu_attribute directive. */
2260
2261 static void
2262 obj_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED)
2263 {
2264 obj_elf_vendor_attribute (OBJ_ATTR_GNU);
2265 }
2266
2267 void
2268 elf_obj_read_begin_hook (void)
2269 {
2270 #ifdef NEED_ECOFF_DEBUG
2271 if (ECOFF_DEBUGGING)
2272 ecoff_read_begin_hook ();
2273 #endif
2274 }
2275
2276 void
2277 elf_obj_symbol_new_hook (symbolS *symbolP)
2278 {
2279 struct elf_obj_sy *sy_obj;
2280
2281 sy_obj = symbol_get_obj (symbolP);
2282 sy_obj->size = NULL;
2283 sy_obj->versioned_name = NULL;
2284
2285 #ifdef NEED_ECOFF_DEBUG
2286 if (ECOFF_DEBUGGING)
2287 ecoff_symbol_new_hook (symbolP);
2288 #endif
2289 }
2290
2291 /* Deduplicate size expressions. We might get into trouble with
2292 multiple freeing or use after free if we leave them pointing to the
2293 same expressionS. */
2294
2295 void
2296 elf_obj_symbol_clone_hook (symbolS *newsym, symbolS *orgsym ATTRIBUTE_UNUSED)
2297 {
2298 struct elf_obj_sy *newelf = symbol_get_obj (newsym);
2299 if (newelf->size)
2300 {
2301 expressionS *exp = XNEW (expressionS);
2302 *exp = *newelf->size;
2303 newelf->size = exp;
2304 }
2305 }
2306
2307 void
2308 elf_copy_symbol_attributes (symbolS *dest, symbolS *src)
2309 {
2310 struct elf_obj_sy *srcelf = symbol_get_obj (src);
2311 struct elf_obj_sy *destelf = symbol_get_obj (dest);
2312 /* If size is unset, copy size from src. Because we don't track whether
2313 .size has been used, we can't differentiate .size dest, 0 from the case
2314 where dest's size is unset. */
2315 if (!destelf->size && S_GET_SIZE (dest) == 0)
2316 {
2317 if (srcelf->size)
2318 {
2319 destelf->size = XNEW (expressionS);
2320 *destelf->size = *srcelf->size;
2321 }
2322 S_SET_SIZE (dest, S_GET_SIZE (src));
2323 }
2324 /* Don't copy visibility. */
2325 S_SET_OTHER (dest, (ELF_ST_VISIBILITY (S_GET_OTHER (dest))
2326 | (S_GET_OTHER (src) & ~ELF_ST_VISIBILITY (-1))));
2327 }
2328
2329 void
2330 obj_elf_version (int ignore ATTRIBUTE_UNUSED)
2331 {
2332 char *name;
2333 unsigned int c;
2334 char *p;
2335 asection *seg = now_seg;
2336 subsegT subseg = now_subseg;
2337 Elf_Internal_Note i_note;
2338 Elf_External_Note e_note;
2339 asection *note_secp = NULL;
2340
2341 SKIP_WHITESPACE ();
2342 if (*input_line_pointer == '\"')
2343 {
2344 unsigned int len;
2345
2346 ++input_line_pointer; /* -> 1st char of string. */
2347 name = input_line_pointer;
2348
2349 while (is_a_char (c = next_char_of_string ()))
2350 ;
2351 c = *input_line_pointer;
2352 *input_line_pointer = '\0';
2353 *(input_line_pointer - 1) = '\0';
2354 *input_line_pointer = c;
2355
2356 /* Create the .note section. */
2357 note_secp = subseg_new (".note", 0);
2358 bfd_set_section_flags (note_secp, SEC_HAS_CONTENTS | SEC_READONLY);
2359 record_alignment (note_secp, 2);
2360
2361 /* Process the version string. */
2362 len = strlen (name) + 1;
2363
2364 /* PR 3456: Although the name field is padded out to an 4-byte
2365 boundary, the namesz field should not be adjusted. */
2366 i_note.namesz = len;
2367 i_note.descsz = 0; /* No description. */
2368 i_note.type = NT_VERSION;
2369 p = frag_more (sizeof (e_note.namesz));
2370 md_number_to_chars (p, i_note.namesz, sizeof (e_note.namesz));
2371 p = frag_more (sizeof (e_note.descsz));
2372 md_number_to_chars (p, i_note.descsz, sizeof (e_note.descsz));
2373 p = frag_more (sizeof (e_note.type));
2374 md_number_to_chars (p, i_note.type, sizeof (e_note.type));
2375 p = frag_more (len);
2376 memcpy (p, name, len);
2377
2378 frag_align (2, 0, 0);
2379
2380 subseg_set (seg, subseg);
2381 }
2382 else
2383 as_bad (_("expected quoted string"));
2384
2385 demand_empty_rest_of_line ();
2386 }
2387
2388 static void
2389 obj_elf_size (int ignore ATTRIBUTE_UNUSED)
2390 {
2391 char *name;
2392 char c = get_symbol_name (&name);
2393 char *p;
2394 expressionS exp;
2395 symbolS *sym;
2396
2397 p = input_line_pointer;
2398 *p = c;
2399 SKIP_WHITESPACE_AFTER_NAME ();
2400 if (*input_line_pointer != ',')
2401 {
2402 *p = 0;
2403 as_bad (_("expected comma after name `%s' in .size directive"), name);
2404 *p = c;
2405 ignore_rest_of_line ();
2406 return;
2407 }
2408 input_line_pointer++;
2409 expression (&exp);
2410 if (exp.X_op == O_absent)
2411 {
2412 as_bad (_("missing expression in .size directive"));
2413 exp.X_op = O_constant;
2414 exp.X_add_number = 0;
2415 }
2416 *p = 0;
2417 sym = symbol_find_or_make (name);
2418 *p = c;
2419 if (exp.X_op == O_constant)
2420 {
2421 S_SET_SIZE (sym, exp.X_add_number);
2422 xfree (symbol_get_obj (sym)->size);
2423 symbol_get_obj (sym)->size = NULL;
2424 }
2425 else
2426 {
2427 symbol_get_obj (sym)->size = XNEW (expressionS);
2428 *symbol_get_obj (sym)->size = exp;
2429 }
2430
2431 /* If the symbol in the directive matches the current function being
2432 processed, indicate end of the current stream of ginsns. */
2433 if (flag_synth_cfi
2434 && S_IS_FUNCTION (sym) && sym == ginsn_data_func_symbol ())
2435 ginsn_data_end (symbol_temp_new_now ());
2436
2437 demand_empty_rest_of_line ();
2438 }
2439
2440 /* Handle the ELF .type pseudo-op. This sets the type of a symbol.
2441 There are six syntaxes:
2442
2443 The first (used on Solaris) is
2444 .type SYM,#function
2445 The second (used on UnixWare) is
2446 .type SYM,@function
2447 The third (reportedly to be used on Irix 6.0) is
2448 .type SYM STT_FUNC
2449 The fourth (used on NetBSD/Arm and Linux/ARM) is
2450 .type SYM,%function
2451 The fifth (used on SVR4/860) is
2452 .type SYM,"function"
2453 The sixth (emitted by recent SunPRO under Solaris) is
2454 .type SYM,[0-9]
2455 where the integer is the STT_* value.
2456 */
2457
2458 static char *
2459 obj_elf_type_name (char *cp)
2460 {
2461 char *p;
2462
2463 p = input_line_pointer;
2464 if (*input_line_pointer >= '0'
2465 && *input_line_pointer <= '9')
2466 {
2467 while (*input_line_pointer >= '0'
2468 && *input_line_pointer <= '9')
2469 ++input_line_pointer;
2470 *cp = *input_line_pointer;
2471 *input_line_pointer = '\0';
2472 }
2473 else
2474 *cp = get_symbol_name (&p);
2475
2476 return p;
2477 }
2478
2479 static void
2480 obj_elf_type (int ignore ATTRIBUTE_UNUSED)
2481 {
2482 char c;
2483 int type;
2484 const char *type_name;
2485 symbolS *sym;
2486 elf_symbol_type *elfsym;
2487
2488 sym = get_sym_from_input_line_and_check ();
2489 c = *input_line_pointer;
2490 elfsym = (elf_symbol_type *) symbol_get_bfdsym (sym);
2491
2492 if (*input_line_pointer == ',')
2493 ++input_line_pointer;
2494
2495 SKIP_WHITESPACE ();
2496 if ( *input_line_pointer == '#'
2497 || *input_line_pointer == '@'
2498 || *input_line_pointer == '"'
2499 || *input_line_pointer == '%')
2500 ++input_line_pointer;
2501
2502 type_name = obj_elf_type_name (& c);
2503
2504 type = 0;
2505 if (strcmp (type_name, "function") == 0
2506 || strcmp (type_name, "2") == 0
2507 || strcmp (type_name, "STT_FUNC") == 0)
2508 type = BSF_FUNCTION;
2509 else if (strcmp (type_name, "object") == 0
2510 || strcmp (type_name, "1") == 0
2511 || strcmp (type_name, "STT_OBJECT") == 0)
2512 type = BSF_OBJECT;
2513 else if (strcmp (type_name, "tls_object") == 0
2514 || strcmp (type_name, "6") == 0
2515 || strcmp (type_name, "STT_TLS") == 0)
2516 type = BSF_OBJECT | BSF_THREAD_LOCAL;
2517 else if (strcmp (type_name, "notype") == 0
2518 || strcmp (type_name, "0") == 0
2519 || strcmp (type_name, "STT_NOTYPE") == 0)
2520 ;
2521 else if (strcmp (type_name, "common") == 0
2522 || strcmp (type_name, "5") == 0
2523 || strcmp (type_name, "STT_COMMON") == 0)
2524 {
2525 type = BSF_OBJECT;
2526
2527 if (! S_IS_COMMON (sym))
2528 {
2529 if (S_IS_VOLATILE (sym))
2530 {
2531 sym = symbol_clone (sym, 1);
2532 S_SET_SEGMENT (sym, bfd_com_section_ptr);
2533 S_SET_VALUE (sym, 0);
2534 S_SET_EXTERNAL (sym);
2535 symbol_set_frag (sym, &zero_address_frag);
2536 S_CLEAR_VOLATILE (sym);
2537 }
2538 else if (S_IS_DEFINED (sym) || symbol_equated_p (sym))
2539 as_bad (_("symbol '%s' is already defined"), S_GET_NAME (sym));
2540 else
2541 {
2542 /* FIXME: Is it safe to just change the section ? */
2543 S_SET_SEGMENT (sym, bfd_com_section_ptr);
2544 S_SET_VALUE (sym, 0);
2545 S_SET_EXTERNAL (sym);
2546 }
2547 }
2548 }
2549 else if (strcmp (type_name, "gnu_indirect_function") == 0
2550 || strcmp (type_name, "10") == 0
2551 || strcmp (type_name, "STT_GNU_IFUNC") == 0)
2552 {
2553 const struct elf_backend_data *bed;
2554
2555 bed = get_elf_backend_data (stdoutput);
2556 if (bed->elf_osabi != ELFOSABI_NONE
2557 && bed->elf_osabi != ELFOSABI_GNU
2558 && bed->elf_osabi != ELFOSABI_FREEBSD)
2559 as_bad (_("symbol type \"%s\" is supported only by GNU "
2560 "and FreeBSD targets"), type_name);
2561 /* MIPS targets do not support IFUNCS. */
2562 else if (bed->target_id == MIPS_ELF_DATA)
2563 as_bad (_("symbol type \"%s\" is not supported by "
2564 "MIPS targets"), type_name);
2565 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
2566 type = BSF_FUNCTION | BSF_GNU_INDIRECT_FUNCTION;
2567 }
2568 else if (strcmp (type_name, "gnu_unique_object") == 0)
2569 {
2570 const struct elf_backend_data *bed;
2571
2572 bed = get_elf_backend_data (stdoutput);
2573 if (bed->elf_osabi != ELFOSABI_NONE
2574 && bed->elf_osabi != ELFOSABI_GNU)
2575 as_bad (_("symbol type \"%s\" is supported only by GNU targets"),
2576 type_name);
2577 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_unique;
2578 type = BSF_OBJECT | BSF_GNU_UNIQUE;
2579 }
2580 #ifdef md_elf_symbol_type
2581 else if ((type = md_elf_symbol_type (type_name, sym, elfsym)) != -1)
2582 ;
2583 #endif
2584 else
2585 as_bad (_("unrecognized symbol type \"%s\""), type_name);
2586
2587 *input_line_pointer = c;
2588
2589 if (*input_line_pointer == '"')
2590 ++input_line_pointer;
2591
2592 #ifdef md_elf_symbol_type_change
2593 if (!md_elf_symbol_type_change (sym, elfsym, type))
2594 #endif
2595 {
2596 flagword mask = BSF_FUNCTION | BSF_OBJECT;
2597
2598 if (type != BSF_FUNCTION)
2599 mask |= BSF_GNU_INDIRECT_FUNCTION;
2600 if (type != BSF_OBJECT)
2601 {
2602 mask |= BSF_GNU_UNIQUE | BSF_THREAD_LOCAL;
2603
2604 if (S_IS_COMMON (sym))
2605 {
2606 as_bad (_("cannot change type of common symbol '%s'"),
2607 S_GET_NAME (sym));
2608 mask = type = 0;
2609 }
2610 }
2611
2612 /* Don't warn when changing to STT_NOTYPE. */
2613 if (type)
2614 {
2615 flagword new = (elfsym->symbol.flags & ~mask) | type;
2616
2617 if (new != (elfsym->symbol.flags | type))
2618 as_warn (_("symbol '%s' already has its type set"), S_GET_NAME (sym));
2619 elfsym->symbol.flags = new;
2620 }
2621 else
2622 elfsym->symbol.flags &= ~mask;
2623 }
2624
2625 if (S_IS_FUNCTION (sym) && flag_synth_cfi)
2626 {
2627 /* When using SCFI, .type directive indicates start of a new FDE for SCFI
2628 processing. So, we must first demarcate the previous block of ginsns,
2629 if any, to mark the end of a previous FDE. */
2630 if (frchain_now->frch_ginsn_data)
2631 ginsn_data_end (symbol_temp_new_now ());
2632 ginsn_data_begin (sym);
2633 }
2634
2635 demand_empty_rest_of_line ();
2636 }
2637
2638 static segT comment_section;
2639
2640 static void
2641 obj_elf_ident (int ignore ATTRIBUTE_UNUSED)
2642 {
2643 segT old_section = now_seg;
2644 int old_subsection = now_subseg;
2645
2646 #ifdef md_flush_pending_output
2647 md_flush_pending_output ();
2648 #endif
2649
2650 if (!comment_section)
2651 {
2652 char *p;
2653 comment_section = subseg_new (".comment", 0);
2654 bfd_set_section_flags (comment_section, (SEC_READONLY | SEC_HAS_CONTENTS
2655 | SEC_MERGE | SEC_STRINGS));
2656 comment_section->entsize = 1;
2657 #ifdef md_elf_section_change_hook
2658 md_elf_section_change_hook ();
2659 #endif
2660 p = frag_more (1);
2661 *p = 0;
2662 }
2663 else
2664 {
2665 subseg_set (comment_section, 0);
2666 #ifdef md_elf_section_change_hook
2667 md_elf_section_change_hook ();
2668 #endif
2669 }
2670 stringer (8 + 1);
2671 subseg_set (old_section, old_subsection);
2672 #ifdef md_elf_section_change_hook
2673 md_elf_section_change_hook ();
2674 #endif
2675 }
2676
2677 #ifdef INIT_STAB_SECTION
2678
2679 /* The first entry in a .stabs section is special. */
2680
2681 void
2682 obj_elf_init_stab_section (segT stab, segT stabstr)
2683 {
2684 char *file;
2685 char *p;
2686 unsigned int stroff;
2687
2688 /* Force the section to align to a longword boundary. Without this,
2689 UnixWare ar crashes. */
2690 bfd_set_section_alignment (stab, 2);
2691
2692 /* Make space for this first symbol. */
2693 p = frag_more (12);
2694 /* Zero it out. */
2695 memset (p, 0, 12);
2696 file = remap_debug_filename (as_where (NULL));
2697 stroff = get_stab_string_offset (file, stabstr);
2698 know (stroff == 1 || (stroff == 0 && file[0] == '\0'));
2699 md_number_to_chars (p, stroff, 4);
2700 seg_info (stab)->stabu.p = p;
2701 free (file);
2702 }
2703
2704 #endif
2705
2706 /* Called via bfd_map_over_sections. If SEC's linked_to_symbol_name
2707 isn't NULL, set up its linked-to section.
2708 For .stabs section, fill in the counts in the first entry. */
2709
2710 static void
2711 set_additional_section_info (bfd *abfd,
2712 asection *sec,
2713 void *inf ATTRIBUTE_UNUSED)
2714 {
2715 if (sec->map_head.linked_to_symbol_name)
2716 {
2717 symbolS *linked_to_sym;
2718 linked_to_sym = symbol_find (sec->map_head.linked_to_symbol_name);
2719 if (!linked_to_sym || !S_IS_DEFINED (linked_to_sym))
2720 as_bad (_("undefined linked-to symbol `%s' on section `%s'"),
2721 sec->map_head.linked_to_symbol_name,
2722 bfd_section_name (sec));
2723 else
2724 elf_linked_to_section (sec) = S_GET_SEGMENT (linked_to_sym);
2725 }
2726
2727 char *name;
2728 asection *strsec;
2729 char *p;
2730 int strsz, nsyms;
2731
2732 if (!startswith (sec->name, ".stab"))
2733 return;
2734 if (!strcmp ("str", sec->name + strlen (sec->name) - 3))
2735 return;
2736
2737 name = concat (sec->name, "str", NULL);
2738 strsec = bfd_get_section_by_name (abfd, name);
2739 if (strsec)
2740 strsz = bfd_section_size (strsec);
2741 else
2742 strsz = 0;
2743 nsyms = bfd_section_size (sec) / 12 - 1;
2744
2745 p = seg_info (sec)->stabu.p;
2746 gas_assert (p != 0);
2747
2748 bfd_h_put_16 (abfd, nsyms, p + 6);
2749 bfd_h_put_32 (abfd, strsz, p + 8);
2750 free (name);
2751 }
2752
2753 #ifdef NEED_ECOFF_DEBUG
2754
2755 /* This function is called by the ECOFF code. It is supposed to
2756 record the external symbol information so that the backend can
2757 write it out correctly. The ELF backend doesn't actually handle
2758 this at the moment, so we do it ourselves. We save the information
2759 in the symbol. */
2760
2761 #ifdef OBJ_MAYBE_ELF
2762 static
2763 #endif
2764 void
2765 elf_ecoff_set_ext (symbolS *sym, struct ecoff_extr *ext)
2766 {
2767 symbol_get_bfdsym (sym)->udata.p = ext;
2768 }
2769
2770 /* This function is called by bfd_ecoff_debug_externals. It is
2771 supposed to *EXT to the external symbol information, and return
2772 whether the symbol should be used at all. */
2773
2774 static bool
2775 elf_get_extr (asymbol *sym, EXTR *ext)
2776 {
2777 if (sym->udata.p == NULL)
2778 return false;
2779 *ext = *(EXTR *) sym->udata.p;
2780 return true;
2781 }
2782
2783 /* This function is called by bfd_ecoff_debug_externals. It has
2784 nothing to do for ELF. */
2785
2786 static void
2787 elf_set_index (asymbol *sym ATTRIBUTE_UNUSED,
2788 bfd_size_type indx ATTRIBUTE_UNUSED)
2789 {
2790 }
2791
2792 #endif /* NEED_ECOFF_DEBUG */
2793
2794 void
2795 elf_frob_symbol (symbolS *symp, int *puntp)
2796 {
2797 struct elf_obj_sy *sy_obj;
2798 expressionS *size;
2799 struct elf_versioned_name_list *versioned_name;
2800
2801 #ifdef NEED_ECOFF_DEBUG
2802 if (ECOFF_DEBUGGING)
2803 ecoff_frob_symbol (symp);
2804 #endif
2805
2806 sy_obj = symbol_get_obj (symp);
2807
2808 size = sy_obj->size;
2809 if (size != NULL)
2810 {
2811 if (resolve_expression (size)
2812 && size->X_op == O_constant)
2813 S_SET_SIZE (symp, size->X_add_number);
2814 else
2815 {
2816 if (!flag_allow_nonconst_size)
2817 as_bad (_(".size expression for %s "
2818 "does not evaluate to a constant"), S_GET_NAME (symp));
2819 else
2820 as_warn (_(".size expression for %s "
2821 "does not evaluate to a constant"), S_GET_NAME (symp));
2822 }
2823 free (sy_obj->size);
2824 sy_obj->size = NULL;
2825 }
2826
2827 versioned_name = sy_obj->versioned_name;
2828 if (versioned_name)
2829 {
2830 /* This symbol was given a new name with the .symver directive.
2831 If this is an external reference, just rename the symbol to
2832 include the version string. This will make the relocs be
2833 against the correct versioned symbol. */
2834
2835 /* We will have already reported an version error. */
2836 if (sy_obj->bad_version)
2837 *puntp = true;
2838 /* elf_frob_file_before_adjust only allows one version symbol for
2839 renamed symbol. */
2840 else if (sy_obj->rename)
2841 S_SET_NAME (symp, versioned_name->name);
2842 else if (S_IS_COMMON (symp))
2843 {
2844 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
2845 versioned_name->name, S_GET_NAME (symp));
2846 *puntp = true;
2847 }
2848 else
2849 {
2850 asymbol *bfdsym;
2851 elf_symbol_type *elfsym;
2852
2853 /* This is a definition. Add an alias for each version.
2854 FIXME: Using an alias will permit the debugging information
2855 to refer to the right symbol. However, it's not clear
2856 whether it is the best approach. */
2857
2858 /* FIXME: Creating a new symbol here is risky. We're
2859 in the final loop over the symbol table. We can
2860 get away with it only because the symbol goes to
2861 the end of the list, where the loop will still see
2862 it. It would probably be better to do this in
2863 obj_frob_file_before_adjust. */
2864 for (; versioned_name != NULL;
2865 versioned_name = versioned_name->next)
2866 {
2867 symbolS *symp2 = symbol_find_or_make (versioned_name->name);
2868
2869 S_SET_SEGMENT (symp2, S_GET_SEGMENT (symp));
2870
2871 /* Subtracting out the frag address here is a hack
2872 because we are in the middle of the final loop. */
2873 S_SET_VALUE (symp2,
2874 (S_GET_VALUE (symp)
2875 - (symbol_get_frag (symp)->fr_address
2876 / OCTETS_PER_BYTE)));
2877
2878 symbol_set_frag (symp2, symbol_get_frag (symp));
2879
2880 /* This will copy over the size information. */
2881 copy_symbol_attributes (symp2, symp);
2882
2883 S_SET_OTHER (symp2, S_GET_OTHER (symp));
2884
2885 if (S_IS_WEAK (symp))
2886 S_SET_WEAK (symp2);
2887
2888 if (S_IS_EXTERNAL (symp))
2889 S_SET_EXTERNAL (symp2);
2890 }
2891
2892 switch (sy_obj->visibility)
2893 {
2894 case visibility_unchanged:
2895 break;
2896 case visibility_hidden:
2897 bfdsym = symbol_get_bfdsym (symp);
2898 elfsym = elf_symbol_from (bfdsym);
2899 elfsym->internal_elf_sym.st_other &= ~3;
2900 elfsym->internal_elf_sym.st_other |= STV_HIDDEN;
2901 break;
2902 case visibility_remove:
2903 /* Don't remove the symbol if it is used in relocation.
2904 Instead, mark it as to be removed and issue an error
2905 if the symbol has more than one versioned name. */
2906 if (symbol_used_in_reloc_p (symp))
2907 {
2908 if (sy_obj->versioned_name->next != NULL)
2909 as_bad (_("symbol '%s' with multiple versions cannot be used in relocation"),
2910 S_GET_NAME (symp));
2911 symbol_mark_removed (symp);
2912 }
2913 else
2914 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2915 break;
2916 case visibility_local:
2917 S_CLEAR_EXTERNAL (symp);
2918 break;
2919 }
2920 }
2921 }
2922
2923 /* Double check weak symbols. */
2924 if (S_IS_WEAK (symp))
2925 {
2926 if (S_IS_COMMON (symp))
2927 as_bad (_("symbol `%s' can not be both weak and common"),
2928 S_GET_NAME (symp));
2929 }
2930 }
2931
2932 /* Fix up SYMPP which has been marked to be removed by .symver. */
2933
2934 void
2935 elf_fixup_removed_symbol (symbolS **sympp)
2936 {
2937 symbolS *symp = *sympp;
2938 struct elf_obj_sy *sy_obj = symbol_get_obj (symp);
2939
2940 /* Replace the removed symbol with the versioned symbol. */
2941 symp = symbol_find (sy_obj->versioned_name->name);
2942 *sympp = symp;
2943 }
2944
2945 /* Create symbols for group signature. */
2946
2947 void
2948 elf_adjust_symtab (void)
2949 {
2950 unsigned int i;
2951
2952 /* Make the SHT_GROUP sections that describe each section group. We
2953 can't set up the section contents here yet, because elf section
2954 indices have yet to be calculated. elf.c:set_group_contents does
2955 the rest of the work. */
2956 for (i = 0; i < groups.num_group; i++)
2957 {
2958 const char *group_name = elf_group_name (groups.head[i]);
2959 const char *sec_name;
2960 asection *s;
2961 flagword flags;
2962 struct symbol *sy;
2963
2964 flags = SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_GROUP;
2965 for (s = groups.head[i]; s != NULL; s = elf_next_in_group (s))
2966 if ((s->flags ^ flags) & SEC_LINK_ONCE)
2967 {
2968 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2969 if (s != groups.head[i])
2970 {
2971 as_warn (_("assuming all members of group `%s' are COMDAT"),
2972 group_name);
2973 break;
2974 }
2975 }
2976
2977 sec_name = ".group";
2978 s = subseg_force_new (sec_name, 0);
2979 if (s == NULL
2980 || !bfd_set_section_flags (s, flags)
2981 || !bfd_set_section_alignment (s, 2))
2982 {
2983 as_fatal (_("can't create group: %s"),
2984 bfd_errmsg (bfd_get_error ()));
2985 }
2986 elf_section_type (s) = SHT_GROUP;
2987
2988 /* Pass a pointer to the first section in this group. */
2989 elf_next_in_group (s) = groups.head[i];
2990 elf_sec_group (groups.head[i]) = s;
2991 /* Make sure that the signature symbol for the group has the
2992 name of the group. */
2993 sy = symbol_find_exact (group_name);
2994 if (!sy || !symbol_on_chain (sy, symbol_rootP, symbol_lastP))
2995 {
2996 /* Create the symbol now. */
2997 sy = symbol_new (group_name, now_seg, frag_now, 0);
2998 #ifdef TE_SOLARIS
2999 /* Before Solaris 11 build 154, Sun ld rejects local group
3000 signature symbols, so make them weak hidden instead. */
3001 symbol_get_bfdsym (sy)->flags |= BSF_WEAK;
3002 S_SET_OTHER (sy, STV_HIDDEN);
3003 #else
3004 symbol_get_obj (sy)->local = 1;
3005 #endif
3006 symbol_table_insert (sy);
3007 }
3008 elf_group_id (s) = symbol_get_bfdsym (sy);
3009 /* Mark the group signature symbol as used so that it will be
3010 included in the symbol table. */
3011 symbol_mark_used_in_reloc (sy);
3012 }
3013 }
3014
3015 void
3016 elf_frob_file (void)
3017 {
3018 bfd_map_over_sections (stdoutput, set_additional_section_info, NULL);
3019
3020 #ifdef elf_tc_final_processing
3021 elf_tc_final_processing ();
3022 #endif
3023 }
3024
3025 /* It removes any unneeded versioned symbols from the symbol table. */
3026
3027 void
3028 elf_frob_file_before_adjust (void)
3029 {
3030 if (symbol_rootP)
3031 {
3032 symbolS *symp;
3033
3034 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
3035 {
3036 struct elf_obj_sy *sy_obj = symbol_get_obj (symp);
3037 int is_defined = !!S_IS_DEFINED (symp);
3038
3039 if (sy_obj->versioned_name)
3040 {
3041 char *p = strchr (sy_obj->versioned_name->name,
3042 ELF_VER_CHR);
3043
3044 if (sy_obj->rename)
3045 {
3046 /* The @@@ syntax is a special case. If the symbol is
3047 not defined, 2 `@'s will be removed from the
3048 versioned_name. Otherwise, 1 `@' will be removed. */
3049 size_t l = strlen (&p[3]) + 1;
3050 memmove (&p[1 + is_defined], &p[3], l);
3051 }
3052
3053 if (!is_defined)
3054 {
3055 /* Verify that the name isn't using the @@ syntax--this
3056 is reserved for definitions of the default version
3057 to link against. */
3058 if (!sy_obj->rename && p[1] == ELF_VER_CHR)
3059 {
3060 as_bad (_("invalid attempt to declare external "
3061 "version name as default in symbol `%s'"),
3062 sy_obj->versioned_name->name);
3063 return;
3064 }
3065
3066 /* Only one version symbol is allowed for undefined
3067 symbol. */
3068 if (sy_obj->versioned_name->next)
3069 {
3070 as_bad (_("multiple versions [`%s'|`%s'] for "
3071 "symbol `%s'"),
3072 sy_obj->versioned_name->name,
3073 sy_obj->versioned_name->next->name,
3074 S_GET_NAME (symp));
3075 return;
3076 }
3077
3078 sy_obj->rename = true;
3079 }
3080 }
3081
3082 /* If there was .symver or .weak, but symbol was neither
3083 defined nor used anywhere, remove it. */
3084 if (!is_defined
3085 && (sy_obj->versioned_name || S_IS_WEAK (symp))
3086 && symbol_used_p (symp) == 0
3087 && symbol_used_in_reloc_p (symp) == 0)
3088 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
3089 }
3090 }
3091 }
3092
3093 /* It is required that we let write_relocs have the opportunity to
3094 optimize away fixups before output has begun, since it is possible
3095 to eliminate all fixups for a section and thus we never should
3096 have generated the relocation section. */
3097
3098 void
3099 elf_frob_file_after_relocs (void)
3100 {
3101 unsigned int i;
3102
3103 /* Set SHT_GROUP section size. */
3104 for (i = 0; i < groups.num_group; i++)
3105 {
3106 asection *s, *head, *group;
3107 bfd_size_type size;
3108
3109 head = groups.head[i];
3110 size = 4;
3111 for (s = head; s != NULL; s = elf_next_in_group (s))
3112 size += (s->flags & SEC_RELOC) != 0 ? 8 : 4;
3113
3114 group = elf_sec_group (head);
3115 subseg_set (group, 0);
3116 bfd_set_section_size (group, size);
3117 group->contents = (unsigned char *) frag_more (size);
3118 frag_now->fr_fix = frag_now_fix_octets ();
3119 frag_wane (frag_now);
3120 }
3121
3122 #ifdef NEED_ECOFF_DEBUG
3123 if (ECOFF_DEBUGGING)
3124 /* Generate the ECOFF debugging information. */
3125 {
3126 const struct ecoff_debug_swap *debug_swap;
3127 struct ecoff_debug_info debug;
3128 char *buf;
3129 asection *sec;
3130
3131 debug_swap
3132 = get_elf_backend_data (stdoutput)->elf_backend_ecoff_debug_swap;
3133 know (debug_swap != NULL);
3134 ecoff_build_debug (&debug.symbolic_header, &buf, debug_swap);
3135
3136 /* Set up the pointers in debug. */
3137 debug.alloc_syments = true;
3138 #define SET(ptr, offset, type) \
3139 debug.ptr = (type) (buf + debug.symbolic_header.offset)
3140
3141 SET (line, cbLineOffset, unsigned char *);
3142 SET (external_dnr, cbDnOffset, void *);
3143 SET (external_pdr, cbPdOffset, void *);
3144 SET (external_sym, cbSymOffset, void *);
3145 SET (external_opt, cbOptOffset, void *);
3146 SET (external_aux, cbAuxOffset, union aux_ext *);
3147 SET (ss, cbSsOffset, char *);
3148 SET (external_fdr, cbFdOffset, void *);
3149 SET (external_rfd, cbRfdOffset, void *);
3150 /* ssext and external_ext are set up just below. */
3151
3152 #undef SET
3153
3154 /* Set up the external symbols. */
3155 debug.ssext = debug.ssext_end = NULL;
3156 debug.external_ext = debug.external_ext_end = NULL;
3157 if (! bfd_ecoff_debug_externals (stdoutput, &debug, debug_swap, true,
3158 elf_get_extr, elf_set_index))
3159 as_fatal (_("failed to set up debugging information: %s"),
3160 bfd_errmsg (bfd_get_error ()));
3161
3162 sec = bfd_get_section_by_name (stdoutput, ".mdebug");
3163 gas_assert (sec != NULL);
3164
3165 know (!stdoutput->output_has_begun);
3166
3167 /* We set the size of the section, call bfd_set_section_contents
3168 to force the ELF backend to allocate a file position, and then
3169 write out the data. FIXME: Is this really the best way to do
3170 this? */
3171 bfd_set_section_size (sec, bfd_ecoff_debug_size (stdoutput, &debug,
3172 debug_swap));
3173
3174 /* Pass BUF to bfd_set_section_contents because this will
3175 eventually become a call to fwrite, and ISO C prohibits
3176 passing a NULL pointer to a stdio function even if the
3177 pointer will not be used. */
3178 if (! bfd_set_section_contents (stdoutput, sec, buf, 0, 0))
3179 as_fatal (_("can't start writing .mdebug section: %s"),
3180 bfd_errmsg (bfd_get_error ()));
3181
3182 know (stdoutput->output_has_begun);
3183 know (sec->filepos != 0);
3184
3185 if (! bfd_ecoff_write_debug (stdoutput, &debug, debug_swap,
3186 sec->filepos))
3187 as_fatal (_("could not write .mdebug section: %s"),
3188 bfd_errmsg (bfd_get_error ()));
3189 }
3190 #endif /* NEED_ECOFF_DEBUG */
3191 }
3192
3193 static void
3194 elf_generate_asm_lineno (void)
3195 {
3196 #ifdef NEED_ECOFF_DEBUG
3197 if (ECOFF_DEBUGGING)
3198 ecoff_generate_asm_lineno ();
3199 #endif
3200 }
3201
3202 static void
3203 elf_process_stab (int what ATTRIBUTE_UNUSED,
3204 const char *string ATTRIBUTE_UNUSED,
3205 int type ATTRIBUTE_UNUSED,
3206 int other ATTRIBUTE_UNUSED,
3207 int desc ATTRIBUTE_UNUSED)
3208 {
3209 #ifdef NEED_ECOFF_DEBUG
3210 if (ECOFF_DEBUGGING)
3211 ecoff_stab (what, string, type, other, desc);
3212 #endif
3213 }
3214
3215 static int
3216 elf_separate_stab_sections (void)
3217 {
3218 #ifdef NEED_ECOFF_DEBUG
3219 return (!ECOFF_DEBUGGING);
3220 #else
3221 return 1;
3222 #endif
3223 }
3224
3225 static void
3226 elf_init_stab_section (segT stab, segT stabstr)
3227 {
3228 #ifdef NEED_ECOFF_DEBUG
3229 if (!ECOFF_DEBUGGING)
3230 #endif
3231 obj_elf_init_stab_section (stab, stabstr);
3232 }
3233
3234 /* This is called when the assembler starts. */
3235
3236 void
3237 elf_begin (void)
3238 {
3239 asection *s;
3240
3241 /* Add symbols for the known sections to the symbol table. */
3242 s = bfd_get_section_by_name (stdoutput, TEXT_SECTION_NAME);
3243 symbol_table_insert (section_symbol (s));
3244 s = bfd_get_section_by_name (stdoutput, DATA_SECTION_NAME);
3245 symbol_table_insert (section_symbol (s));
3246 s = bfd_get_section_by_name (stdoutput, BSS_SECTION_NAME);
3247 symbol_table_insert (section_symbol (s));
3248 elf_com_section_ptr = bfd_com_section_ptr;
3249 previous_section = NULL;
3250 previous_subsection = 0;
3251 comment_section = NULL;
3252 memset (&groups, 0, sizeof (groups));
3253 }
3254
3255 void
3256 elf_end (void)
3257 {
3258 while (section_stack)
3259 {
3260 struct section_stack *top = section_stack;
3261 section_stack = top->next;
3262 free (top);
3263 }
3264 while (recorded_attributes)
3265 {
3266 struct recorded_attribute_info *rai = recorded_attributes;
3267 recorded_attributes = rai->next;
3268 free (rai);
3269 }
3270 if (groups.indexes)
3271 {
3272 htab_delete (groups.indexes);
3273 free (groups.head);
3274 }
3275 }
3276
3277 const struct format_ops elf_format_ops =
3278 {
3279 bfd_target_elf_flavour,
3280 0, /* dfl_leading_underscore */
3281 1, /* emit_section_symbols */
3282 elf_begin,
3283 elf_end,
3284 elf_file_symbol,
3285 elf_frob_symbol,
3286 elf_frob_file,
3287 elf_frob_file_before_adjust,
3288 0, /* obj_frob_file_before_fix */
3289 elf_frob_file_after_relocs,
3290 elf_s_get_size, elf_s_set_size,
3291 elf_s_get_align, elf_s_set_align,
3292 elf_s_get_other,
3293 elf_s_set_other,
3294 0, /* s_get_desc */
3295 0, /* s_set_desc */
3296 0, /* s_get_type */
3297 0, /* s_set_type */
3298 elf_copy_symbol_attributes,
3299 elf_generate_asm_lineno,
3300 elf_process_stab,
3301 elf_separate_stab_sections,
3302 elf_init_stab_section,
3303 elf_sec_sym_ok_for_reloc,
3304 elf_pop_insert,
3305 #ifdef NEED_ECOFF_DEBUG
3306 elf_ecoff_set_ext,
3307 #else
3308 0, /* ecoff_set_ext */
3309 #endif
3310 elf_obj_read_begin_hook,
3311 elf_obj_symbol_new_hook,
3312 elf_obj_symbol_clone_hook,
3313 elf_adjust_symtab
3314 };