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