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