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