Updated French translations for GOLD and LD
[binutils-gdb.git] / gas / config / obj-elf.c
blob00c6e38acf906e2cfd3449b42cb14c20d7c02748
1 /* ELF object file format
2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
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.
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.
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. */
21 #define OBJ_HEADER "obj-elf.h"
22 #include "as.h"
23 #include "safe-ctype.h"
24 #include "subsegs.h"
25 #include "obstack.h"
26 #include "dwarf2dbg.h"
27 #include "ginsn.h"
29 #ifndef ECOFF_DEBUGGING
30 #define ECOFF_DEBUGGING 0
31 #else
32 #define NEED_ECOFF_DEBUG
33 #endif
35 #ifdef NEED_ECOFF_DEBUG
36 #include "ecoff.h"
37 #include "bfd/ecoff-bfd.h"
38 #endif
40 #ifdef TC_ALPHA
41 #include "elf/alpha.h"
42 #endif
44 #ifdef TC_MIPS
45 #include "elf/mips.h"
46 #endif
48 #ifdef TC_PPC
49 #include "elf/ppc.h"
50 #endif
52 #ifdef TC_I386
53 #include "elf/x86-64.h"
54 #endif
56 #ifdef TC_MEP
57 #include "elf/mep.h"
58 #endif
60 #ifdef TC_NIOS2
61 #include "elf/nios2.h"
62 #endif
64 #ifdef TC_PRU
65 #include "elf/pru.h"
66 #endif
68 static void obj_elf_line (int);
69 static void obj_elf_size (int);
70 static void obj_elf_type (int);
71 static void obj_elf_ident (int);
72 static void obj_elf_weak (int);
73 static void obj_elf_local (int);
74 static void obj_elf_visibility (int);
75 static void obj_elf_symver (int);
76 static void obj_elf_subsection (int);
77 static void obj_elf_popsection (int);
78 static void obj_elf_gnu_attribute (int);
79 static void obj_elf_tls_common (int);
80 static void obj_elf_lcomm (int);
81 static void obj_elf_struct (int);
82 static void obj_elf_attach_to_group (int);
84 static const pseudo_typeS elf_pseudo_table[] =
86 {"attach_to_group", obj_elf_attach_to_group, 0},
87 {"comm", obj_elf_common, 0},
88 {"common", obj_elf_common, 1},
89 {"ident", obj_elf_ident, 0},
90 {"lcomm", obj_elf_lcomm, 0},
91 {"local", obj_elf_local, 0},
92 {"previous", obj_elf_previous, 0},
93 {"section", obj_elf_section, 0},
94 {"section.s", obj_elf_section, 0},
95 {"sect", obj_elf_section, 0},
96 {"sect.s", obj_elf_section, 0},
97 {"pushsection", obj_elf_section, 1},
98 {"popsection", obj_elf_popsection, 0},
99 {"size", obj_elf_size, 0},
100 {"type", obj_elf_type, 0},
101 {"version", obj_elf_version, 0},
102 {"weak", obj_elf_weak, 0},
104 /* These define symbol visibility. */
105 {"internal", obj_elf_visibility, STV_INTERNAL},
106 {"hidden", obj_elf_visibility, STV_HIDDEN},
107 {"protected", obj_elf_visibility, STV_PROTECTED},
109 /* These are used for stabs-in-elf configurations. */
110 {"line", obj_elf_line, 0},
112 /* This is a GNU extension to handle symbol versions. */
113 {"symver", obj_elf_symver, 0},
115 /* A GNU extension to change subsection only. */
116 {"subsection", obj_elf_subsection, 0},
118 /* These are GNU extensions to aid in garbage collecting C++ vtables. */
119 {"vtable_inherit", obj_elf_vtable_inherit, 0},
120 {"vtable_entry", obj_elf_vtable_entry, 0},
122 /* A GNU extension for object attributes. */
123 {"gnu_attribute", obj_elf_gnu_attribute, 0},
125 /* These are used for dwarf2. */
126 { "file", dwarf2_directive_file, 0 },
127 { "loc", dwarf2_directive_loc, 0 },
128 { "loc_mark_labels", dwarf2_directive_loc_mark_labels, 0 },
130 /* We need to trap the section changing calls to handle .previous. */
131 {"data", obj_elf_data, 0},
132 {"offset", obj_elf_struct, 0},
133 {"struct", obj_elf_struct, 0},
134 {"text", obj_elf_text, 0},
135 {"bss", obj_elf_bss, 0},
137 {"tls_common", obj_elf_tls_common, 0},
139 /* End sentinel. */
140 {NULL, NULL, 0},
143 static const pseudo_typeS ecoff_debug_pseudo_table[] =
145 #ifdef NEED_ECOFF_DEBUG
146 /* COFF style debugging information for ECOFF. .ln is not used; .loc
147 is used instead. */
148 { "def", ecoff_directive_def, 0 },
149 { "dim", ecoff_directive_dim, 0 },
150 { "endef", ecoff_directive_endef, 0 },
151 { "file", ecoff_directive_file, 0 },
152 { "scl", ecoff_directive_scl, 0 },
153 { "tag", ecoff_directive_tag, 0 },
154 { "val", ecoff_directive_val, 0 },
156 /* COFF debugging requires pseudo-ops .size and .type, but ELF
157 already has meanings for those. We use .esize and .etype
158 instead. These are only generated by gcc anyhow. */
159 { "esize", ecoff_directive_size, 0 },
160 { "etype", ecoff_directive_type, 0 },
162 /* ECOFF specific debugging information. */
163 { "aent", ecoff_directive_ent, 1 },
164 { "begin", ecoff_directive_begin, 0 },
165 { "bend", ecoff_directive_bend, 0 },
166 { "end", ecoff_directive_end, 0 },
167 { "ent", ecoff_directive_ent, 0 },
168 { "fmask", ecoff_directive_fmask, 0 },
169 { "frame", ecoff_directive_frame, 0 },
170 { "loc", ecoff_directive_loc, 0 },
171 { "mask", ecoff_directive_mask, 0 },
173 /* Other ECOFF directives. */
174 { "extern", ecoff_directive_extern, 0 },
176 /* These are used on Irix. I don't know how to implement them. */
177 { "alias", s_ignore, 0 },
178 { "bgnb", s_ignore, 0 },
179 { "endb", s_ignore, 0 },
180 { "lab", s_ignore, 0 },
181 { "noalias", s_ignore, 0 },
182 { "verstamp", s_ignore, 0 },
183 { "vreg", s_ignore, 0 },
184 #endif
186 {NULL, NULL, 0} /* end sentinel */
189 #undef NO_RELOC
190 #include "aout/aout64.h"
192 asection *elf_com_section_ptr;
194 void
195 elf_pop_insert (void)
197 pop_insert (elf_pseudo_table);
198 if (ECOFF_DEBUGGING)
199 pop_insert (ecoff_debug_pseudo_table);
202 static bfd_vma
203 elf_s_get_size (symbolS *sym)
205 return S_GET_SIZE (sym);
208 static void
209 elf_s_set_size (symbolS *sym, bfd_vma sz)
211 S_SET_SIZE (sym, sz);
214 static bfd_vma
215 elf_s_get_align (symbolS *sym)
217 return S_GET_ALIGN (sym);
220 static void
221 elf_s_set_align (symbolS *sym, bfd_vma align)
223 S_SET_ALIGN (sym, align);
227 elf_s_get_other (symbolS *sym)
229 return elf_symbol (symbol_get_bfdsym (sym))->internal_elf_sym.st_other;
232 static void
233 elf_s_set_other (symbolS *sym, int other)
235 S_SET_OTHER (sym, other);
238 static int
239 elf_sec_sym_ok_for_reloc (asection *sec)
241 return obj_sec_sym_ok_for_reloc (sec);
244 void
245 elf_file_symbol (const char *s)
247 asymbol *bsym;
248 symbolS *sym = symbol_new (s, absolute_section, &zero_address_frag, 0);
249 size_t name_length = strlen (s);
251 if (name_length > strlen (S_GET_NAME (sym)))
253 obstack_grow (&notes, s, name_length + 1);
254 S_SET_NAME (sym, (const char *) obstack_finish (&notes));
256 else
257 strcpy ((char *) S_GET_NAME (sym), s);
259 symbol_get_bfdsym (sym)->flags |= BSF_FILE;
261 if (symbol_rootP != sym
262 && ((bsym = symbol_get_bfdsym (symbol_rootP)) == NULL
263 || (bsym->flags & BSF_FILE) == 0))
265 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
266 symbol_insert (sym, symbol_rootP, &symbol_rootP, &symbol_lastP);
269 #ifdef DEBUG
270 verify_symbol_chain (symbol_rootP, symbol_lastP);
271 #endif
273 #ifdef NEED_ECOFF_DEBUG
274 ecoff_new_file (s);
275 #endif
278 /* Called from read.c:s_comm after we've parsed .comm symbol, size.
279 Parse a possible alignment value. */
281 symbolS *
282 elf_common_parse (int ignore ATTRIBUTE_UNUSED, symbolS *symbolP, addressT size)
284 addressT align = 0;
285 int is_local = symbol_get_obj (symbolP)->local;
287 if (*input_line_pointer == ',')
289 char *save = input_line_pointer;
291 input_line_pointer++;
292 SKIP_WHITESPACE ();
294 if (*input_line_pointer == '"')
296 /* For sparc. Accept .common symbol, length, "bss" */
297 input_line_pointer++;
298 /* Some use the dot, some don't. */
299 if (*input_line_pointer == '.')
300 input_line_pointer++;
301 /* Some say data, some say bss. */
302 if (startswith (input_line_pointer, "bss\""))
303 input_line_pointer += 4;
304 else if (startswith (input_line_pointer, "data\""))
305 input_line_pointer += 5;
306 else
308 char *p = input_line_pointer;
309 char c;
311 while (*--p != '"')
313 while (!is_end_of_line[(unsigned char) *input_line_pointer])
314 if (*input_line_pointer++ == '"')
315 break;
316 c = *input_line_pointer;
317 *input_line_pointer = '\0';
318 as_bad (_("bad .common segment %s"), p);
319 *input_line_pointer = c;
320 ignore_rest_of_line ();
321 return NULL;
323 /* ??? Don't ask me why these are always global. */
324 is_local = 0;
326 else
328 input_line_pointer = save;
329 align = parse_align (is_local);
330 if (align == (addressT) -1)
331 return NULL;
335 if (is_local)
337 bss_alloc (symbolP, size, align);
338 S_CLEAR_EXTERNAL (symbolP);
340 else
342 S_SET_VALUE (symbolP, size);
343 S_SET_ALIGN (symbolP, align);
344 S_SET_EXTERNAL (symbolP);
345 S_SET_SEGMENT (symbolP, elf_com_section_ptr);
348 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
350 return symbolP;
353 void
354 obj_elf_common (int is_common)
356 if (flag_mri && is_common)
357 s_mri_common (0);
358 else
359 s_comm_internal (0, elf_common_parse);
362 static void
363 obj_elf_tls_common (int ignore ATTRIBUTE_UNUSED)
365 symbolS *symbolP = s_comm_internal (0, elf_common_parse);
367 if (symbolP)
368 symbol_get_bfdsym (symbolP)->flags |= BSF_THREAD_LOCAL;
371 static void
372 obj_elf_lcomm (int ignore ATTRIBUTE_UNUSED)
374 symbolS *symbolP = s_comm_internal (0, s_lcomm_internal);
376 if (symbolP)
377 symbol_get_bfdsym (symbolP)->flags |= BSF_OBJECT;
380 static symbolS *
381 get_sym_from_input_line_and_check (void)
383 char *name;
384 char c;
385 symbolS *sym;
387 c = get_symbol_name (& name);
388 sym = symbol_find_or_make (name);
389 *input_line_pointer = c;
390 SKIP_WHITESPACE_AFTER_NAME ();
392 /* There is no symbol name if input_line_pointer has not moved. */
393 if (name == input_line_pointer)
394 as_bad (_("Missing symbol name in directive"));
395 return sym;
398 static void
399 obj_elf_local (int ignore ATTRIBUTE_UNUSED)
401 int c;
402 symbolS *symbolP;
406 symbolP = get_sym_from_input_line_and_check ();
407 c = *input_line_pointer;
408 S_CLEAR_EXTERNAL (symbolP);
409 symbol_get_obj (symbolP)->local = 1;
410 if (c == ',')
412 input_line_pointer++;
413 SKIP_WHITESPACE ();
414 if (*input_line_pointer == '\n')
415 c = '\n';
418 while (c == ',');
419 demand_empty_rest_of_line ();
422 static void
423 obj_elf_weak (int ignore ATTRIBUTE_UNUSED)
425 int c;
426 symbolS *symbolP;
430 symbolP = get_sym_from_input_line_and_check ();
431 c = *input_line_pointer;
432 S_SET_WEAK (symbolP);
433 if (c == ',')
435 input_line_pointer++;
436 SKIP_WHITESPACE ();
437 if (*input_line_pointer == '\n')
438 c = '\n';
441 while (c == ',');
442 demand_empty_rest_of_line ();
445 static void
446 obj_elf_visibility (int visibility)
448 int c;
449 symbolS *symbolP;
450 asymbol *bfdsym;
451 elf_symbol_type *elfsym;
455 symbolP = get_sym_from_input_line_and_check ();
457 bfdsym = symbol_get_bfdsym (symbolP);
458 elfsym = elf_symbol_from (bfdsym);
460 gas_assert (elfsym);
462 elfsym->internal_elf_sym.st_other &= ~3;
463 elfsym->internal_elf_sym.st_other |= visibility;
465 c = *input_line_pointer;
466 if (c == ',')
468 input_line_pointer ++;
470 SKIP_WHITESPACE ();
472 if (*input_line_pointer == '\n')
473 c = '\n';
476 while (c == ',');
478 demand_empty_rest_of_line ();
481 static segT previous_section;
482 static int previous_subsection;
484 struct section_stack
486 struct section_stack *next;
487 segT seg, prev_seg;
488 int subseg, prev_subseg;
491 static struct section_stack *section_stack;
493 /* ELF section flags for unique sections. */
494 #define SEC_ASSEMBLER_SHF_MASK SHF_GNU_RETAIN
496 /* Return TRUE iff SEC matches the section info INF. */
498 static bool
499 get_section_by_match (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
501 struct elf_section_match *match = (struct elf_section_match *) inf;
502 const char *gname = match->group_name;
503 const char *group_name = elf_group_name (sec);
504 const char *linked_to_symbol_name
505 = sec->map_head.linked_to_symbol_name;
506 unsigned int sh_info = elf_section_data (sec)->this_hdr.sh_info;
507 bfd_vma sh_flags = (elf_section_data (sec)->this_hdr.sh_flags
508 & SEC_ASSEMBLER_SHF_MASK);
510 return (sh_info == match->sh_info
511 && sh_flags == match->sh_flags
512 && ((bfd_section_flags (sec) & SEC_ASSEMBLER_SECTION_ID)
513 == (match->flags & SEC_ASSEMBLER_SECTION_ID))
514 && sec->section_id == match->section_id
515 && (group_name == gname
516 || (group_name != NULL
517 && gname != NULL
518 && strcmp (group_name, gname) == 0))
519 && (linked_to_symbol_name == match->linked_to_symbol_name
520 || (linked_to_symbol_name != NULL
521 && match->linked_to_symbol_name != NULL
522 && strcmp (linked_to_symbol_name,
523 match->linked_to_symbol_name) == 0)));
526 /* Handle the .section pseudo-op. This code supports two different
527 syntaxes.
529 The first is found on Solaris, and looks like
530 .section ".sec1",#alloc,#execinstr,#write
531 Here the names after '#' are the SHF_* flags to turn on for the
532 section. I'm not sure how it determines the SHT_* type (BFD
533 doesn't really give us control over the type, anyhow).
535 The second format is found on UnixWare, and probably most SVR4
536 machines, and looks like
537 .section .sec1,"a",@progbits
538 The quoted string may contain any combination of a, w, x, and
539 represents the SHF_* flags to turn on for the section. The string
540 beginning with '@' can be progbits or nobits. There should be
541 other possibilities, but I don't know what they are. In any case,
542 BFD doesn't really let us set the section type. */
544 static void
545 change_section (const char *name,
546 unsigned int type,
547 bfd_vma attr,
548 int entsize,
549 struct elf_section_match *match_p,
550 bool linkonce,
551 bool push,
552 subsegT new_subsection)
554 asection *old_sec;
555 segT sec;
556 flagword flags;
557 const struct elf_backend_data *bed;
558 const struct bfd_elf_special_section *ssect;
560 if (match_p == NULL)
562 static struct elf_section_match unused_match;
563 match_p = &unused_match;
566 #ifdef md_flush_pending_output
567 md_flush_pending_output ();
568 #endif
570 /* Switch to the section, creating it if necessary. */
571 if (push)
573 struct section_stack *elt;
574 elt = XNEW (struct section_stack);
575 elt->next = section_stack;
576 elt->seg = now_seg;
577 elt->prev_seg = previous_section;
578 elt->subseg = now_subseg;
579 elt->prev_subseg = previous_subsection;
580 section_stack = elt;
583 obj_elf_section_change_hook ();
585 old_sec = bfd_get_section_by_name_if (stdoutput, name, get_section_by_match,
586 (void *) match_p);
587 if (old_sec)
589 sec = old_sec;
590 subseg_set (sec, new_subsection);
592 else
593 sec = subseg_force_new (name, new_subsection);
595 bed = get_elf_backend_data (stdoutput);
596 ssect = (*bed->get_sec_type_attr) (stdoutput, sec);
598 if (ssect != NULL)
600 bool override = false;
602 if (type == SHT_NULL)
603 type = ssect->type;
604 else if (type != ssect->type)
606 if (old_sec == NULL
607 /* Some older versions of gcc will emit
609 .section .init_array,"aw",@progbits
611 for __attribute__ ((section (".init_array"))).
612 "@progbits" is incorrect. Also for x86-64 large bss
613 sections, some older versions of gcc will emit
615 .section .lbss,"aw",@progbits
617 "@progbits" is incorrect. */
618 #ifdef TC_I386
619 && (bed->s->arch_size != 64
620 || !(ssect->attr & SHF_X86_64_LARGE))
621 #endif
622 && ssect->type != SHT_INIT_ARRAY
623 && ssect->type != SHT_FINI_ARRAY
624 && ssect->type != SHT_PREINIT_ARRAY)
626 /* We allow to specify any type for a .note section. */
627 if (ssect->type != SHT_NOTE
628 /* Processor and application defined types are allowed too. */
629 && type < SHT_LOPROC)
630 as_warn (_("setting incorrect section type for %s"),
631 name);
633 else
635 as_warn (_("ignoring incorrect section type for %s"),
636 name);
637 type = ssect->type;
641 if (old_sec == NULL && ((attr & ~(SHF_LINK_ORDER
642 | SHF_MASKOS
643 | SHF_MASKPROC))
644 & ~ssect->attr) != 0)
646 /* Strip SHF_GNU_RETAIN. */
647 bfd_vma generic_attr = attr;
648 if (elf_tdata (stdoutput)->has_gnu_osabi)
649 generic_attr &= ~SHF_GNU_RETAIN;
651 /* As a GNU extension, we permit a .note section to be
652 allocatable. If the linker sees an allocatable .note
653 section, it will create a PT_NOTE segment in the output
654 file. We also allow "x" for .note.GNU-stack. */
655 if (ssect->type == SHT_NOTE
656 && (generic_attr == SHF_ALLOC
657 || generic_attr == SHF_EXECINSTR))
659 /* Allow different SHF_MERGE and SHF_STRINGS if we have
660 something like .rodata.str. */
661 else if (ssect->suffix_length == -2
662 && name[ssect->prefix_length] == '.'
663 && (generic_attr
664 & ~ssect->attr
665 & ~SHF_MERGE
666 & ~SHF_STRINGS) == 0)
668 /* .interp, .strtab and .symtab can have SHF_ALLOC. */
669 else if (generic_attr == SHF_ALLOC
670 && (strcmp (name, ".interp") == 0
671 || strcmp (name, ".strtab") == 0
672 || strcmp (name, ".symtab") == 0))
673 override = true;
674 /* .note.GNU-stack can have SHF_EXECINSTR. */
675 else if (generic_attr == SHF_EXECINSTR
676 && strcmp (name, ".note.GNU-stack") == 0)
677 override = true;
678 #ifdef TC_ALPHA
679 /* A section on Alpha may have SHF_ALPHA_GPREL. */
680 else if ((generic_attr & ~ssect->attr) == SHF_ALPHA_GPREL)
681 override = true;
682 #endif
683 #ifdef TC_RX
684 else if (generic_attr == (SHF_EXECINSTR | SHF_WRITE | SHF_ALLOC)
685 && (ssect->type == SHT_INIT_ARRAY
686 || ssect->type == SHT_FINI_ARRAY
687 || ssect->type == SHT_PREINIT_ARRAY))
688 /* RX init/fini arrays can and should have the "awx" attributes set. */
690 #endif
691 else
693 if (match_p->group_name == NULL)
694 as_warn (_("setting incorrect section attributes for %s"),
695 name);
696 override = true;
700 if (!override && old_sec == NULL)
701 attr |= ssect->attr;
704 /* Convert ELF type and flags to BFD flags. */
705 flags = (SEC_RELOC
706 | ((attr & SHF_WRITE) ? 0 : SEC_READONLY)
707 | ((attr & SHF_ALLOC) ? SEC_ALLOC : 0)
708 | (((attr & SHF_ALLOC) && type != SHT_NOBITS) ? SEC_LOAD : 0)
709 | ((attr & SHF_EXECINSTR) ? SEC_CODE : 0)
710 | ((attr & SHF_MERGE) ? SEC_MERGE : 0)
711 | ((attr & SHF_STRINGS) ? SEC_STRINGS : 0)
712 | ((attr & SHF_EXCLUDE) ? SEC_EXCLUDE: 0)
713 | ((attr & SHF_TLS) ? SEC_THREAD_LOCAL : 0));
714 #ifdef md_elf_section_flags
715 flags = md_elf_section_flags (flags, attr, type);
716 #endif
718 if (linkonce)
719 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
721 /* PR 28054: Set the SEC_ELF_OCTETS flag for debugging sections.
722 Based on the code in bfd/elf.c:_bfd_elf_make_section_from_shdr().
724 FIXME: We do not set the SEC_DEBUGGING flag because that causes
725 problems for the FT32 and MSP430 targets. Investigate and fix. */
726 if ((flags & SEC_ALLOC) == 0 && name [0] == '.')
728 if ( startswith (name, ".debug")
729 || startswith (name, ".zdebug")
730 || startswith (name, ".gnu.debuglto_.debug_")
731 || startswith (name, ".gnu.linkonce.wi.")
732 || startswith (name, GNU_BUILD_ATTRS_SECTION_NAME)
733 || startswith (name, ".note.gnu"))
734 flags |= SEC_ELF_OCTETS;
737 if (old_sec == NULL)
739 symbolS *secsym;
741 if (type == SHT_NULL)
742 type = bfd_elf_get_default_section_type (flags);
743 elf_section_type (sec) = type;
744 elf_section_flags (sec) = attr;
745 elf_section_data (sec)->this_hdr.sh_info = match_p->sh_info;
747 /* Prevent SEC_HAS_CONTENTS from being inadvertently set. */
748 if (type == SHT_NOBITS)
749 seg_info (sec)->bss = 1;
751 /* Set the section ID and flags. */
752 sec->section_id = match_p->section_id;
753 flags |= match_p->flags;
755 /* Set the linked-to symbol name. */
756 sec->map_head.linked_to_symbol_name
757 = match_p->linked_to_symbol_name;
759 bfd_set_section_flags (sec, flags);
760 if (flags & SEC_MERGE)
761 sec->entsize = entsize;
762 elf_group_name (sec) = match_p->group_name;
764 /* Add a symbol for this section to the symbol table. */
765 secsym = symbol_find (name);
766 if (secsym != NULL)
768 /* We could be repurposing an undefined symbol here: make sure we
769 reset sy_value to look like other section symbols in order to avoid
770 trying to incorrectly resolve this section symbol later on. */
771 static const expressionS exp = { .X_op = O_constant };
772 symbol_set_value_expression (secsym, &exp);
773 symbol_set_bfdsym (secsym, sec->symbol);
775 else
776 symbol_table_insert (section_symbol (sec));
778 else
780 if (type != SHT_NULL
781 && (unsigned) type != elf_section_type (old_sec))
783 if (ssect != NULL)
784 /* This is a special section with known type. User
785 assembly might get the section type wrong; Even high
786 profile projects like glibc have done so in the past.
787 So don't error in this case. */
788 as_warn (_("ignoring changed section type for %s"), name);
789 else
790 /* Do error when assembly isn't self-consistent. */
791 as_bad (_("changed section type for %s"), name);
794 if (attr != 0)
796 /* If section attributes are specified the second time we see a
797 particular section, then check that they are the same as we
798 saw the first time. */
799 if (((old_sec->flags ^ flags)
800 & (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_CODE
801 | SEC_EXCLUDE | SEC_SORT_ENTRIES | SEC_MERGE | SEC_STRINGS
802 | SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD
803 | SEC_THREAD_LOCAL)))
805 if (ssect != NULL)
806 as_warn (_("ignoring changed section attributes for %s"), name);
807 else
808 as_bad (_("changed section attributes for %s"), name);
810 else
811 /* FIXME: Maybe we should consider removing a previously set
812 processor or application specific attribute as suspicious? */
813 elf_section_flags (sec) = attr;
815 if ((flags & SEC_MERGE) && old_sec->entsize != (unsigned) entsize)
816 as_bad (_("changed section entity size for %s"), name);
820 #ifdef md_elf_section_change_hook
821 md_elf_section_change_hook ();
822 #endif
825 void
826 obj_elf_change_section (const char *name,
827 unsigned int type,
828 bfd_vma attr,
829 int entsize,
830 struct elf_section_match *match_p,
831 bool linkonce)
833 change_section (name, type, attr, entsize, match_p, linkonce, false, 0);
836 static bfd_vma
837 obj_elf_parse_section_letters (char *str, size_t len,
838 bool *is_clone, int *inherit, bfd_vma *gnu_attr)
840 bfd_vma attr = 0;
842 *is_clone = false;
843 *inherit = 0;
845 while (len > 0)
847 switch (*str)
849 case 'a':
850 attr |= SHF_ALLOC;
851 /* Compatibility. */
852 if (len > 1 && str[1] == 'm')
854 attr |= SHF_MERGE;
855 str++, len--;
856 if (len > 1 && str[1] == 's')
858 attr |= SHF_STRINGS;
859 str++, len--;
862 break;
863 case 'e':
864 attr |= SHF_EXCLUDE;
865 break;
866 case 'o':
867 attr |= SHF_LINK_ORDER;
868 break;
869 case 'w':
870 attr |= SHF_WRITE;
871 break;
872 case 'x':
873 attr |= SHF_EXECINSTR;
874 break;
875 case 'M':
876 attr |= SHF_MERGE;
877 break;
878 case 'S':
879 attr |= SHF_STRINGS;
880 break;
881 case 'G':
882 attr |= SHF_GROUP;
883 break;
884 case 'T':
885 attr |= SHF_TLS;
886 break;
887 case 'd':
888 *gnu_attr |= SHF_GNU_MBIND;
889 break;
890 case 'R':
891 *gnu_attr |= SHF_GNU_RETAIN;
892 break;
893 case '?':
894 *is_clone = true;
895 break;
896 default:
898 const char *bad_msg = _("unrecognized .section attribute:"
899 " want a,e,o,w,x,M,S,G,T or number");
900 #ifdef md_elf_section_letter
901 bfd_vma md_attr = md_elf_section_letter (*str, &bad_msg);
902 if (md_attr != (bfd_vma) -1)
903 attr |= md_attr;
904 else
905 #endif
906 if (ISDIGIT (*str))
908 char * end;
909 struct elf_backend_data *bed;
910 bfd_vma numeric_flags = strtoul (str, &end, 0);
912 attr |= numeric_flags;
914 bed = (struct elf_backend_data *)
915 get_elf_backend_data (stdoutput);
917 if (bed->elf_osabi == ELFOSABI_NONE
918 || bed->elf_osabi == ELFOSABI_STANDALONE
919 || bed->elf_osabi == ELFOSABI_GNU
920 || bed->elf_osabi == ELFOSABI_FREEBSD)
922 /* Add flags in the SHF_MASKOS range to gnu_attr for
923 OSABIs that support those flags.
924 Also adding the flags for ELFOSABI_{NONE,STANDALONE}
925 allows them to be validated later in obj_elf_section.
926 We can't just always set these bits in gnu_attr for
927 all OSABIs, since Binutils does not recognize all
928 SHF_MASKOS bits for non-GNU OSABIs. It's therefore
929 possible that numeric flags are being used to set bits
930 in the SHF_MASKOS range for those targets, and we
931 don't want assembly to fail in those situations. */
932 *gnu_attr |= (numeric_flags & SHF_MASKOS);
935 /* Update str and len, allowing for the fact that
936 we will execute str++ and len-- below. */
937 end --;
938 len -= (end - str);
939 str = end;
941 else if (!attr && !*gnu_attr && (*str == '+' || *str == '-'))
942 *inherit = *str == '+' ? 1 : -1;
943 else
944 as_fatal ("%s", bad_msg);
946 break;
948 str++, len--;
951 return attr;
954 static int
955 obj_elf_section_type (char *str, size_t len, bool warn)
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;
970 #ifdef md_elf_section_type
972 int md_type = md_elf_section_type (str, len);
973 if (md_type >= 0)
974 return md_type;
976 #endif
978 if (ISDIGIT (*str))
980 char * end;
981 int type = strtoul (str, & end, 0);
983 if (warn && (size_t) (end - str) != len)
984 as_warn (_("extraneous characters at end of numeric section type"));
986 return type;
989 if (warn)
990 as_warn (_("unrecognized section type"));
991 return 0;
994 #ifdef TC_SPARC
995 static bfd_vma
996 obj_elf_section_word (char *str, size_t len, int *type)
998 int ret;
1000 if (len == 5 && startswith (str, "write"))
1001 return SHF_WRITE;
1002 if (len == 5 && startswith (str, "alloc"))
1003 return SHF_ALLOC;
1004 if (len == 9 && startswith (str, "execinstr"))
1005 return SHF_EXECINSTR;
1006 if (len == 7 && startswith (str, "exclude"))
1007 return SHF_EXCLUDE;
1008 if (len == 3 && startswith (str, "tls"))
1009 return SHF_TLS;
1011 ret = obj_elf_section_type (str, len, false);
1012 if (ret != 0)
1013 *type = ret;
1014 else
1015 as_warn (_("unrecognized section attribute"));
1017 return 0;
1019 #endif
1021 /* Get name of section. */
1022 const char *
1023 obj_elf_section_name (void)
1025 char *name;
1027 SKIP_WHITESPACE ();
1028 if (*input_line_pointer == '"')
1030 int dummy;
1032 name = demand_copy_C_string (&dummy);
1033 if (name == NULL)
1035 ignore_rest_of_line ();
1036 return NULL;
1039 else
1041 char *end = input_line_pointer;
1043 while (0 == strchr ("\n\t,; ", *end))
1044 end++;
1045 if (end == input_line_pointer)
1047 as_bad (_("missing name"));
1048 ignore_rest_of_line ();
1049 return NULL;
1052 obstack_grow0 (&notes, input_line_pointer, end - input_line_pointer);
1053 name = obstack_base (&notes);
1055 while (flag_sectname_subst)
1057 char *subst = strchr (name, '%');
1058 if (subst && subst[1] == 'S')
1060 size_t head = subst - name;
1061 size_t tail = strlen (subst + 2) + 1;
1062 size_t slen = strlen (now_seg->name);
1064 if (slen > 2)
1066 obstack_blank (&notes, slen - 2);
1067 name = obstack_base (&notes);
1069 memmove (name + head + slen, name + head + 2, tail);
1070 memcpy (name + head, now_seg->name, slen);
1072 else
1073 break;
1076 obstack_finish (&notes);
1078 #ifdef tc_canonicalize_section_name
1079 name = tc_canonicalize_section_name (name);
1080 #endif
1081 input_line_pointer = end;
1083 SKIP_WHITESPACE ();
1084 return name;
1087 static void
1088 obj_elf_attach_to_group (int dummy ATTRIBUTE_UNUSED)
1090 const char * gname = obj_elf_section_name ();
1092 if (gname == NULL)
1094 as_warn (_("group name not parseable"));
1095 return;
1098 if (elf_group_name (now_seg))
1100 if (strcmp (elf_group_name (now_seg), gname) != 0)
1101 as_warn (_("section %s already has a group (%s)"),
1102 bfd_section_name (now_seg), elf_group_name (now_seg));
1103 return;
1106 elf_group_name (now_seg) = gname;
1107 elf_section_flags (now_seg) |= SHF_GROUP;
1110 void
1111 obj_elf_section (int push)
1113 const char *name;
1114 char *beg;
1115 int type, dummy;
1116 bfd_vma attr;
1117 bfd_vma gnu_attr;
1118 int entsize;
1119 bool linkonce;
1120 subsegT new_subsection = 0;
1121 struct elf_section_match match;
1122 unsigned long linked_to_section_index = -1UL;
1124 if (flag_mri)
1126 char mri_type;
1128 #ifdef md_flush_pending_output
1129 md_flush_pending_output ();
1130 #endif
1132 obj_elf_section_change_hook ();
1134 s_mri_sect (&mri_type);
1136 #ifdef md_elf_section_change_hook
1137 md_elf_section_change_hook ();
1138 #endif
1140 return;
1143 name = obj_elf_section_name ();
1144 if (name == NULL)
1145 return;
1147 memset (&match, 0, sizeof (match));
1149 symbolS * sym;
1150 if ((sym = symbol_find (name)) != NULL
1151 && ! symbol_section_p (sym)
1152 && S_IS_DEFINED (sym)
1153 && ! S_IS_VOLATILE (sym)
1154 && ! S_CAN_BE_REDEFINED (sym))
1156 as_bad (_("section name '%s' already defined as another symbol"), name);
1157 ignore_rest_of_line ();
1158 return;
1160 type = SHT_NULL;
1161 attr = 0;
1162 gnu_attr = 0;
1163 entsize = 0;
1164 linkonce = 0;
1166 if (*input_line_pointer == ',')
1168 /* Skip the comma. */
1169 ++input_line_pointer;
1170 SKIP_WHITESPACE ();
1172 if (push && ISDIGIT (*input_line_pointer))
1174 /* .pushsection has an optional subsection. */
1175 new_subsection = (subsegT) get_absolute_expression ();
1177 SKIP_WHITESPACE ();
1179 /* Stop if we don't see a comma. */
1180 if (*input_line_pointer != ',')
1181 goto done;
1183 /* Skip the comma. */
1184 ++input_line_pointer;
1185 SKIP_WHITESPACE ();
1188 if (*input_line_pointer == '"')
1190 bool is_clone;
1191 int inherit;
1193 beg = demand_copy_C_string (&dummy);
1194 if (beg == NULL)
1196 ignore_rest_of_line ();
1197 return;
1199 attr = obj_elf_parse_section_letters (beg, strlen (beg), &is_clone,
1200 &inherit, &gnu_attr);
1202 if (inherit > 0)
1203 attr |= elf_section_flags (now_seg);
1204 else if (inherit < 0)
1205 attr = elf_section_flags (now_seg) & ~attr;
1206 if (inherit)
1207 type = elf_section_type (now_seg);
1209 SKIP_WHITESPACE ();
1210 if (*input_line_pointer == ',')
1212 char c;
1213 char *save = input_line_pointer;
1215 ++input_line_pointer;
1216 SKIP_WHITESPACE ();
1217 c = *input_line_pointer;
1218 if (c == '"')
1220 beg = demand_copy_C_string (&dummy);
1221 if (beg == NULL)
1223 ignore_rest_of_line ();
1224 return;
1226 type = obj_elf_section_type (beg, strlen (beg), true);
1228 else if (c == '@' || c == '%')
1230 ++input_line_pointer;
1232 if (ISDIGIT (* input_line_pointer))
1233 type = strtoul (input_line_pointer, &input_line_pointer, 0);
1234 else
1236 c = get_symbol_name (& beg);
1237 (void) restore_line_pointer (c);
1238 type = obj_elf_section_type (beg,
1239 input_line_pointer - beg,
1240 true);
1243 else
1244 input_line_pointer = save;
1247 SKIP_WHITESPACE ();
1248 if ((attr & SHF_MERGE) != 0 && *input_line_pointer == ',')
1250 ++input_line_pointer;
1251 SKIP_WHITESPACE ();
1252 if (inherit && *input_line_pointer == ','
1253 && (bfd_section_flags (now_seg) & SEC_MERGE) != 0)
1254 goto fetch_entsize;
1255 entsize = get_absolute_expression ();
1256 SKIP_WHITESPACE ();
1257 if (entsize < 0)
1259 as_warn (_("invalid merge entity size"));
1260 attr &= ~SHF_MERGE;
1261 entsize = 0;
1264 else if ((attr & SHF_MERGE) != 0 && inherit
1265 && (bfd_section_flags (now_seg) & SEC_MERGE) != 0)
1267 fetch_entsize:
1268 entsize = now_seg->entsize;
1270 else if ((attr & SHF_MERGE) != 0)
1272 as_warn (_("entity size for SHF_MERGE not specified"));
1273 attr &= ~SHF_MERGE;
1276 if ((attr & SHF_LINK_ORDER) != 0 && *input_line_pointer == ',')
1278 ++input_line_pointer;
1279 SKIP_WHITESPACE ();
1280 /* Check for a numeric section index, rather than a symbol name. */
1281 if (ISDIGIT (* input_line_pointer))
1283 linked_to_section_index = strtoul (input_line_pointer, & input_line_pointer, 0);
1285 else if (inherit && *input_line_pointer == ','
1286 && (elf_section_flags (now_seg) & SHF_LINK_ORDER) != 0)
1287 goto fetch_linked_to;
1288 else
1290 char c;
1291 unsigned int length;
1293 c = get_symbol_name (& beg);
1294 (void) restore_line_pointer (c);
1295 length = input_line_pointer - beg;
1296 if (length)
1297 match.linked_to_symbol_name = xmemdup0 (beg, length);
1300 else if ((attr & SHF_LINK_ORDER) != 0 && inherit
1301 && (elf_section_flags (now_seg) & SHF_LINK_ORDER) != 0)
1303 fetch_linked_to:
1304 if (now_seg->map_head.linked_to_symbol_name)
1305 match.linked_to_symbol_name =
1306 now_seg->map_head.linked_to_symbol_name;
1307 else
1308 linked_to_section_index =
1309 elf_section_data (now_seg)->this_hdr.sh_link;
1312 if ((attr & SHF_GROUP) != 0 && is_clone)
1314 as_warn (_("? section flag ignored with G present"));
1315 is_clone = false;
1318 if ((attr & SHF_GROUP) != 0 && *input_line_pointer == ',')
1320 ++input_line_pointer;
1321 SKIP_WHITESPACE ();
1322 if (inherit && *input_line_pointer == ','
1323 && (elf_section_flags (now_seg) & SHF_GROUP) != 0)
1324 goto fetch_group;
1325 match.group_name = obj_elf_section_name ();
1326 if (match.group_name == NULL)
1327 attr &= ~SHF_GROUP;
1328 else if (*input_line_pointer == ',')
1330 ++input_line_pointer;
1331 SKIP_WHITESPACE ();
1332 if (startswith (input_line_pointer, "comdat"))
1334 input_line_pointer += 6;
1335 linkonce = 1;
1338 else if (startswith (name, ".gnu.linkonce"))
1339 linkonce = 1;
1341 else if ((attr & SHF_GROUP) != 0 && inherit
1342 && (elf_section_flags (now_seg) & SHF_GROUP) != 0)
1344 fetch_group:
1345 match.group_name = elf_group_name (now_seg);
1346 linkonce =
1347 (bfd_section_flags (now_seg) & SEC_LINK_ONCE) != 0;
1349 else if ((attr & SHF_GROUP) != 0)
1351 as_warn (_("group name for SHF_GROUP not specified"));
1352 attr &= ~SHF_GROUP;
1355 if (is_clone)
1357 const char *now_group = elf_group_name (now_seg);
1358 if (now_group != NULL)
1360 match.group_name = now_group;
1361 linkonce = (now_seg->flags & SEC_LINK_ONCE) != 0;
1365 if ((gnu_attr & SHF_GNU_MBIND) != 0 && *input_line_pointer == ',')
1367 char *save = input_line_pointer;
1368 ++input_line_pointer;
1369 SKIP_WHITESPACE ();
1370 if (ISDIGIT (* input_line_pointer))
1372 char *t = input_line_pointer;
1373 match.sh_info = strtoul (input_line_pointer,
1374 &input_line_pointer, 0);
1375 if (match.sh_info == (unsigned int) -1)
1377 as_warn (_("unsupported mbind section info: %s"), t);
1378 match.sh_info = 0;
1381 else
1382 input_line_pointer = save;
1385 if ((gnu_attr & SHF_GNU_RETAIN) != 0)
1386 match.sh_flags |= SHF_GNU_RETAIN;
1388 if (*input_line_pointer == ',')
1390 char *save = input_line_pointer;
1392 ++input_line_pointer;
1393 SKIP_WHITESPACE ();
1394 if (startswith (input_line_pointer, "unique"))
1396 input_line_pointer += 6;
1397 SKIP_WHITESPACE ();
1398 if (*input_line_pointer == ',')
1400 ++input_line_pointer;
1401 SKIP_WHITESPACE ();
1402 if (ISDIGIT (* input_line_pointer))
1404 bfd_vma id;
1405 bool overflow;
1406 char *t = input_line_pointer;
1407 if (sizeof (bfd_vma) <= sizeof (unsigned long))
1409 errno = 0;
1410 id = strtoul (input_line_pointer,
1411 &input_line_pointer, 0);
1412 overflow = (id == (unsigned long) -1
1413 && errno == ERANGE);
1415 else
1417 id = bfd_scan_vma
1418 (input_line_pointer,
1419 (const char **) &input_line_pointer, 0);
1420 overflow = id == ~(bfd_vma) 0;
1422 if (overflow || id > (unsigned int) -1)
1424 char *linefeed, saved_char = 0;
1425 if ((linefeed = strchr (t, '\n')) != NULL)
1427 saved_char = *linefeed;
1428 *linefeed = '\0';
1430 as_bad (_("unsupported section id: %s"), t);
1431 if (saved_char)
1432 *linefeed = saved_char;
1434 else
1436 match.section_id = id;
1437 match.flags |= SEC_ASSEMBLER_SECTION_ID;
1442 else
1443 input_line_pointer = save;
1446 #ifdef TC_SPARC
1447 else
1451 char c;
1453 SKIP_WHITESPACE ();
1454 if (*input_line_pointer != '#')
1456 as_bad (_("character following name is not '#'"));
1457 ignore_rest_of_line ();
1458 return;
1460 ++input_line_pointer;
1461 c = get_symbol_name (& beg);
1462 (void) restore_line_pointer (c);
1464 attr |= obj_elf_section_word (beg, input_line_pointer - beg,
1465 &type);
1467 SKIP_WHITESPACE ();
1469 while (*input_line_pointer++ == ',');
1470 --input_line_pointer;
1472 #endif
1475 done:
1476 demand_empty_rest_of_line ();
1478 if ((gnu_attr & (SHF_GNU_MBIND | SHF_GNU_RETAIN)) != 0)
1480 const struct elf_backend_data *bed;
1481 bool mbind_p = (gnu_attr & SHF_GNU_MBIND) != 0;
1483 if (mbind_p && (attr & SHF_ALLOC) == 0)
1484 as_bad (_("SHF_ALLOC isn't set for GNU_MBIND section: %s"), name);
1486 bed = get_elf_backend_data (stdoutput);
1488 if (bed->elf_osabi != ELFOSABI_GNU
1489 && bed->elf_osabi != ELFOSABI_FREEBSD
1490 && bed->elf_osabi != ELFOSABI_NONE)
1491 as_bad (_("%s section is supported only by GNU and FreeBSD targets"),
1492 mbind_p ? "GNU_MBIND" : "GNU_RETAIN");
1493 else
1495 if (mbind_p)
1496 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_mbind;
1497 if ((gnu_attr & SHF_GNU_RETAIN) != 0)
1498 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_retain;
1500 attr |= gnu_attr;
1504 change_section (name, type, attr, entsize, &match, linkonce, push,
1505 new_subsection);
1507 if (linked_to_section_index != -1UL)
1509 elf_section_flags (now_seg) |= SHF_LINK_ORDER;
1510 elf_section_data (now_seg)->this_hdr.sh_link = linked_to_section_index;
1511 /* FIXME: Should we perform some sanity checking on the section index ? */
1515 /* Change to the .bss section. */
1517 void
1518 obj_elf_bss (int i ATTRIBUTE_UNUSED)
1520 int temp;
1522 #ifdef md_flush_pending_output
1523 md_flush_pending_output ();
1524 #endif
1526 obj_elf_section_change_hook ();
1528 temp = get_absolute_expression ();
1529 subseg_set (bss_section, (subsegT) temp);
1530 demand_empty_rest_of_line ();
1532 #ifdef md_elf_section_change_hook
1533 md_elf_section_change_hook ();
1534 #endif
1537 /* Change to the .data section. */
1539 void
1540 obj_elf_data (int i)
1542 #ifdef md_flush_pending_output
1543 md_flush_pending_output ();
1544 #endif
1546 obj_elf_section_change_hook ();
1548 s_data (i);
1550 #ifdef md_elf_section_change_hook
1551 md_elf_section_change_hook ();
1552 #endif
1555 /* Change to the .text section. */
1557 void
1558 obj_elf_text (int i)
1560 #ifdef md_flush_pending_output
1561 md_flush_pending_output ();
1562 #endif
1564 obj_elf_section_change_hook ();
1566 s_text (i);
1568 #ifdef md_elf_section_change_hook
1569 md_elf_section_change_hook ();
1570 #endif
1573 /* Change to the *ABS* section. */
1575 void
1576 obj_elf_struct (int i)
1578 #ifdef md_flush_pending_output
1579 md_flush_pending_output ();
1580 #endif
1582 obj_elf_section_change_hook ();
1584 s_struct (i);
1586 #ifdef md_elf_section_change_hook
1587 md_elf_section_change_hook ();
1588 #endif
1591 static void
1592 obj_elf_subsection (int ignore ATTRIBUTE_UNUSED)
1594 int temp;
1596 #ifdef md_flush_pending_output
1597 md_flush_pending_output ();
1598 #endif
1600 obj_elf_section_change_hook ();
1602 temp = get_absolute_expression ();
1603 subseg_set (now_seg, (subsegT) temp);
1604 demand_empty_rest_of_line ();
1606 #ifdef md_elf_section_change_hook
1607 md_elf_section_change_hook ();
1608 #endif
1611 /* This can be called from the processor backends if they change
1612 sections. */
1614 void
1615 obj_elf_section_change_hook (void)
1617 previous_section = now_seg;
1618 previous_subsection = now_subseg;
1621 void
1622 obj_elf_previous (int ignore ATTRIBUTE_UNUSED)
1624 segT new_section;
1625 int new_subsection;
1627 if (previous_section == 0)
1629 as_warn (_(".previous without corresponding .section; ignored"));
1630 return;
1633 #ifdef md_flush_pending_output
1634 md_flush_pending_output ();
1635 #endif
1637 new_section = previous_section;
1638 new_subsection = previous_subsection;
1639 obj_elf_section_change_hook ();
1641 subseg_set (new_section, new_subsection);
1643 #ifdef md_elf_section_change_hook
1644 md_elf_section_change_hook ();
1645 #endif
1648 static void
1649 obj_elf_popsection (int xxx ATTRIBUTE_UNUSED)
1651 struct section_stack *top = section_stack;
1653 if (top == NULL)
1655 as_warn (_(".popsection without corresponding .pushsection; ignored"));
1656 return;
1659 #ifdef md_flush_pending_output
1660 md_flush_pending_output ();
1661 #endif
1663 section_stack = top->next;
1664 previous_section = top->prev_seg;
1665 previous_subsection = top->prev_subseg;
1666 subseg_set (top->seg, top->subseg);
1667 free (top);
1669 #ifdef md_elf_section_change_hook
1670 md_elf_section_change_hook ();
1671 #endif
1674 static void
1675 obj_elf_line (int ignore ATTRIBUTE_UNUSED)
1677 /* Assume delimiter is part of expression. BSD4.2 as fails with
1678 delightful bug, so we are not being incompatible here. */
1679 new_logical_line (NULL, get_absolute_expression ());
1680 demand_empty_rest_of_line ();
1683 static struct elf_versioned_name_list *
1684 obj_elf_find_and_add_versioned_name (const char *version_name,
1685 const char *sym_name,
1686 const char *ver,
1687 struct elf_obj_sy *sy_obj)
1689 struct elf_versioned_name_list *versioned_name;
1690 const char *p;
1692 for (p = ver + 1; *p == ELF_VER_CHR; p++)
1695 /* NB: Since some tests in ld/testsuite/ld-elfvers have no version
1696 names, we have to disable this. */
1697 if (0 && *p == '\0')
1699 as_bad (_("missing version name in `%s' for symbol `%s'"),
1700 version_name, sym_name);
1701 return NULL;
1704 versioned_name = sy_obj->versioned_name;
1706 switch (p - ver)
1708 case 1:
1709 case 2:
1710 break;
1711 case 3:
1712 if (sy_obj->rename)
1714 if (strcmp (versioned_name->name, version_name) == 0)
1715 return versioned_name;
1716 else
1718 as_bad (_("only one version name with `@@@' is allowed "
1719 "for symbol `%s'"), sym_name);
1720 return NULL;
1723 sy_obj->rename = true;
1724 break;
1725 default:
1726 as_bad (_("invalid version name '%s' for symbol `%s'"),
1727 version_name, sym_name);
1728 return NULL;
1731 for (;
1732 versioned_name != NULL;
1733 versioned_name = versioned_name->next)
1734 if (strcmp (versioned_name->name, version_name) == 0)
1735 return versioned_name;
1737 /* Add this versioned name to the head of the list, */
1738 versioned_name = (struct elf_versioned_name_list *)
1739 xmalloc (sizeof (*versioned_name));
1740 versioned_name->name = xstrdup (version_name);
1741 versioned_name->next = sy_obj->versioned_name;
1742 sy_obj->versioned_name = versioned_name;
1744 return versioned_name;
1747 /* This handles the .symver pseudo-op, which is used to specify a
1748 symbol version. The syntax is ``.symver NAME,SYMVERNAME''.
1749 SYMVERNAME may contain ELF_VER_CHR ('@') characters. This
1750 pseudo-op causes the assembler to emit a symbol named SYMVERNAME
1751 with the same value as the symbol NAME. */
1753 static void
1754 obj_elf_symver (int ignore ATTRIBUTE_UNUSED)
1756 char *name;
1757 const char *sym_name;
1758 char c;
1759 char old_lexat;
1760 symbolS *sym;
1761 struct elf_obj_sy *sy_obj;
1762 char *p;
1764 sym = get_sym_from_input_line_and_check ();
1766 if (*input_line_pointer != ',')
1768 as_bad (_("expected comma after name in .symver"));
1769 ignore_rest_of_line ();
1770 return;
1773 ++input_line_pointer;
1774 SKIP_WHITESPACE ();
1776 /* Temporarily include '@' in symbol names. */
1777 old_lexat = lex_type[(unsigned char) '@'];
1778 lex_type[(unsigned char) '@'] |= LEX_NAME;
1779 c = get_symbol_name (& name);
1780 lex_type[(unsigned char) '@'] = old_lexat;
1781 sym_name = S_GET_NAME (sym);
1783 if (S_IS_COMMON (sym))
1785 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
1786 name, sym_name);
1787 ignore_rest_of_line ();
1788 return;
1791 p = strchr (name, ELF_VER_CHR);
1792 if (p == NULL)
1794 as_bad (_("missing version name in `%s' for symbol `%s'"),
1795 name, sym_name);
1796 ignore_rest_of_line ();
1797 return;
1800 sy_obj = symbol_get_obj (sym);
1801 if (obj_elf_find_and_add_versioned_name (name, sym_name,
1802 p, sy_obj) == NULL)
1804 sy_obj->bad_version = true;
1805 ignore_rest_of_line ();
1806 return;
1809 (void) restore_line_pointer (c);
1811 if (*input_line_pointer == ',')
1813 char *save = input_line_pointer;
1815 ++input_line_pointer;
1816 SKIP_WHITESPACE ();
1817 if (startswith (input_line_pointer, "local"))
1819 input_line_pointer += 5;
1820 sy_obj->visibility = visibility_local;
1822 else if (startswith (input_line_pointer, "hidden"))
1824 input_line_pointer += 6;
1825 sy_obj->visibility = visibility_hidden;
1827 else if (startswith (input_line_pointer, "remove"))
1829 input_line_pointer += 6;
1830 sy_obj->visibility = visibility_remove;
1832 else
1833 input_line_pointer = save;
1836 demand_empty_rest_of_line ();
1839 /* This handles the .vtable_inherit pseudo-op, which is used to indicate
1840 to the linker the hierarchy in which a particular table resides. The
1841 syntax is ".vtable_inherit CHILDNAME, PARENTNAME". */
1843 struct fix *
1844 obj_elf_get_vtable_inherit (void)
1846 char *cname, *pname;
1847 symbolS *csym, *psym;
1848 char c, bad = 0;
1850 if (*input_line_pointer == '#')
1851 ++input_line_pointer;
1853 c = get_symbol_name (& cname);
1854 csym = symbol_find (cname);
1856 /* GCFIXME: should check that we don't have two .vtable_inherits for
1857 the same child symbol. Also, we can currently only do this if the
1858 child symbol is already exists and is placed in a fragment. */
1860 if (csym == NULL || symbol_get_frag (csym) == NULL)
1862 as_bad (_("expected `%s' to have already been set for .vtable_inherit"),
1863 cname);
1864 bad = 1;
1867 *input_line_pointer = c;
1869 SKIP_WHITESPACE_AFTER_NAME ();
1870 if (*input_line_pointer != ',')
1872 as_bad (_("expected comma after name in .vtable_inherit"));
1873 ignore_rest_of_line ();
1874 return NULL;
1877 ++input_line_pointer;
1878 SKIP_WHITESPACE ();
1880 if (*input_line_pointer == '#')
1881 ++input_line_pointer;
1883 if (input_line_pointer[0] == '0'
1884 && (input_line_pointer[1] == '\0'
1885 || ISSPACE (input_line_pointer[1])))
1887 psym = section_symbol (absolute_section);
1888 ++input_line_pointer;
1890 else
1892 c = get_symbol_name (& pname);
1893 psym = symbol_find_or_make (pname);
1894 restore_line_pointer (c);
1897 demand_empty_rest_of_line ();
1899 if (bad)
1900 return NULL;
1902 gas_assert (symbol_get_value_expression (csym)->X_op == O_constant);
1903 return fix_new (symbol_get_frag (csym),
1904 symbol_get_value_expression (csym)->X_add_number,
1905 0, psym, 0, 0, BFD_RELOC_VTABLE_INHERIT);
1908 /* This is a version of obj_elf_get_vtable_inherit() that is
1909 suitable for use in struct _pseudo_type tables. */
1911 void
1912 obj_elf_vtable_inherit (int ignore ATTRIBUTE_UNUSED)
1914 (void) obj_elf_get_vtable_inherit ();
1917 /* This handles the .vtable_entry pseudo-op, which is used to indicate
1918 to the linker that a vtable slot was used. The syntax is
1919 ".vtable_entry tablename, offset". */
1921 struct fix *
1922 obj_elf_get_vtable_entry (void)
1924 symbolS *sym;
1925 offsetT offset;
1927 if (*input_line_pointer == '#')
1928 ++input_line_pointer;
1930 sym = get_sym_from_input_line_and_check ();
1931 if (*input_line_pointer != ',')
1933 as_bad (_("expected comma after name in .vtable_entry"));
1934 ignore_rest_of_line ();
1935 return NULL;
1938 ++input_line_pointer;
1939 if (*input_line_pointer == '#')
1940 ++input_line_pointer;
1942 offset = get_absolute_expression ();
1944 demand_empty_rest_of_line ();
1946 return fix_new (frag_now, frag_now_fix (), 0, sym, offset, 0,
1947 BFD_RELOC_VTABLE_ENTRY);
1950 /* This is a version of obj_elf_get_vtable_entry() that is
1951 suitable for use in struct _pseudo_type tables. */
1953 void
1954 obj_elf_vtable_entry (int ignore ATTRIBUTE_UNUSED)
1956 (void) obj_elf_get_vtable_entry ();
1959 #define skip_whitespace(str) do { if (*(str) == ' ') ++(str); } while (0)
1961 static inline int
1962 skip_past_char (char ** str, char c)
1964 if (**str == c)
1966 (*str)++;
1967 return 0;
1969 else
1970 return -1;
1972 #define skip_past_comma(str) skip_past_char (str, ',')
1974 /* A list of attributes that have been explicitly set by the assembly code.
1975 VENDOR is the vendor id, BASE is the tag shifted right by the number
1976 of bits in MASK, and bit N of MASK is set if tag BASE+N has been set. */
1977 struct recorded_attribute_info {
1978 struct recorded_attribute_info *next;
1979 int vendor;
1980 unsigned int base;
1981 unsigned long mask;
1983 static struct recorded_attribute_info *recorded_attributes;
1985 /* Record that we have seen an explicit specification of attribute TAG
1986 for vendor VENDOR. */
1988 static void
1989 record_attribute (int vendor, unsigned int tag)
1991 unsigned int base;
1992 unsigned long mask;
1993 struct recorded_attribute_info *rai;
1995 base = tag / (8 * sizeof (rai->mask));
1996 mask = 1UL << (tag % (8 * sizeof (rai->mask)));
1997 for (rai = recorded_attributes; rai; rai = rai->next)
1998 if (rai->vendor == vendor && rai->base == base)
2000 rai->mask |= mask;
2001 return;
2004 rai = XNEW (struct recorded_attribute_info);
2005 rai->next = recorded_attributes;
2006 rai->vendor = vendor;
2007 rai->base = base;
2008 rai->mask = mask;
2009 recorded_attributes = rai;
2012 /* Return true if we have seen an explicit specification of attribute TAG
2013 for vendor VENDOR. */
2015 bool
2016 obj_elf_seen_attribute (int vendor, unsigned int tag)
2018 unsigned int base;
2019 unsigned long mask;
2020 struct recorded_attribute_info *rai;
2022 base = tag / (8 * sizeof (rai->mask));
2023 mask = 1UL << (tag % (8 * sizeof (rai->mask)));
2024 for (rai = recorded_attributes; rai; rai = rai->next)
2025 if (rai->vendor == vendor && rai->base == base)
2026 return (rai->mask & mask) != 0;
2027 return false;
2030 /* Parse an attribute directive for VENDOR.
2031 Returns the attribute number read, or zero on error. */
2034 obj_elf_vendor_attribute (int vendor)
2036 expressionS exp;
2037 int type;
2038 int tag;
2039 unsigned int i = 0;
2040 char *s = NULL;
2042 /* Read the first number or name. */
2043 skip_whitespace (input_line_pointer);
2044 s = input_line_pointer;
2045 if (ISDIGIT (*input_line_pointer))
2047 expression (& exp);
2048 if (exp.X_op != O_constant)
2049 goto bad;
2050 tag = exp.X_add_number;
2052 else
2054 char *name;
2056 /* A name may contain '_', but no other punctuation. */
2057 for (; ISALNUM (*input_line_pointer) || *input_line_pointer == '_';
2058 ++input_line_pointer)
2059 i++;
2060 if (i == 0)
2061 goto bad;
2063 name = xmemdup0 (s, i);
2065 #ifndef CONVERT_SYMBOLIC_ATTRIBUTE
2066 #define CONVERT_SYMBOLIC_ATTRIBUTE(a) -1
2067 #endif
2069 tag = CONVERT_SYMBOLIC_ATTRIBUTE (name);
2070 if (tag == -1)
2072 as_bad (_("Attribute name not recognised: %s"), name);
2073 ignore_rest_of_line ();
2074 free (name);
2075 return 0;
2077 free (name);
2080 type = _bfd_elf_obj_attrs_arg_type (stdoutput, vendor, tag);
2082 if (skip_past_comma (&input_line_pointer) == -1)
2083 goto bad;
2084 if (type & 1)
2086 expression (& exp);
2087 if (exp.X_op != O_constant)
2089 as_bad (_("expected numeric constant"));
2090 ignore_rest_of_line ();
2091 return 0;
2093 i = exp.X_add_number;
2095 if ((type & 3) == 3
2096 && skip_past_comma (&input_line_pointer) == -1)
2098 as_bad (_("expected comma"));
2099 ignore_rest_of_line ();
2100 return 0;
2102 if (type & 2)
2104 int len;
2106 skip_whitespace (input_line_pointer);
2107 if (*input_line_pointer != '"')
2108 goto bad_string;
2109 s = demand_copy_C_string (&len);
2112 record_attribute (vendor, tag);
2113 bool ok = false;
2114 switch (type & 3)
2116 case 3:
2117 ok = bfd_elf_add_obj_attr_int_string (stdoutput, vendor, tag, i, s);
2118 break;
2119 case 2:
2120 ok = bfd_elf_add_obj_attr_string (stdoutput, vendor, tag, s);
2121 break;
2122 case 1:
2123 ok = bfd_elf_add_obj_attr_int (stdoutput, vendor, tag, i);
2124 break;
2125 default:
2126 abort ();
2128 if (!ok)
2129 as_fatal (_("error adding attribute: %s"),
2130 bfd_errmsg (bfd_get_error ()));
2132 demand_empty_rest_of_line ();
2133 return tag;
2134 bad_string:
2135 as_bad (_("bad string constant"));
2136 ignore_rest_of_line ();
2137 return 0;
2138 bad:
2139 as_bad (_("expected <tag> , <value>"));
2140 ignore_rest_of_line ();
2141 return 0;
2144 /* Parse a .gnu_attribute directive. */
2146 static void
2147 obj_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED)
2149 obj_elf_vendor_attribute (OBJ_ATTR_GNU);
2152 void
2153 elf_obj_read_begin_hook (void)
2155 #ifdef NEED_ECOFF_DEBUG
2156 if (ECOFF_DEBUGGING)
2157 ecoff_read_begin_hook ();
2158 #endif
2161 void
2162 elf_obj_symbol_new_hook (symbolS *symbolP)
2164 struct elf_obj_sy *sy_obj;
2166 sy_obj = symbol_get_obj (symbolP);
2167 sy_obj->size = NULL;
2168 sy_obj->versioned_name = NULL;
2170 #ifdef NEED_ECOFF_DEBUG
2171 if (ECOFF_DEBUGGING)
2172 ecoff_symbol_new_hook (symbolP);
2173 #endif
2176 /* Deduplicate size expressions. We might get into trouble with
2177 multiple freeing or use after free if we leave them pointing to the
2178 same expressionS. */
2180 void
2181 elf_obj_symbol_clone_hook (symbolS *newsym, symbolS *orgsym ATTRIBUTE_UNUSED)
2183 struct elf_obj_sy *newelf = symbol_get_obj (newsym);
2184 if (newelf->size)
2186 expressionS *exp = XNEW (expressionS);
2187 *exp = *newelf->size;
2188 newelf->size = exp;
2192 void
2193 elf_copy_symbol_attributes (symbolS *dest, symbolS *src)
2195 struct elf_obj_sy *srcelf = symbol_get_obj (src);
2196 struct elf_obj_sy *destelf = symbol_get_obj (dest);
2197 /* If size is unset, copy size from src. Because we don't track whether
2198 .size has been used, we can't differentiate .size dest, 0 from the case
2199 where dest's size is unset. */
2200 if (!destelf->size && S_GET_SIZE (dest) == 0)
2202 if (srcelf->size)
2204 destelf->size = XNEW (expressionS);
2205 *destelf->size = *srcelf->size;
2207 S_SET_SIZE (dest, S_GET_SIZE (src));
2209 /* Don't copy visibility. */
2210 S_SET_OTHER (dest, (ELF_ST_VISIBILITY (S_GET_OTHER (dest))
2211 | (S_GET_OTHER (src) & ~ELF_ST_VISIBILITY (-1))));
2214 void
2215 obj_elf_version (int ignore ATTRIBUTE_UNUSED)
2217 char *name;
2218 unsigned int c;
2219 char *p;
2220 asection *seg = now_seg;
2221 subsegT subseg = now_subseg;
2222 Elf_Internal_Note i_note;
2223 Elf_External_Note e_note;
2224 asection *note_secp = NULL;
2226 SKIP_WHITESPACE ();
2227 if (*input_line_pointer == '\"')
2229 unsigned int len;
2231 ++input_line_pointer; /* -> 1st char of string. */
2232 name = input_line_pointer;
2234 while (is_a_char (c = next_char_of_string ()))
2236 c = *input_line_pointer;
2237 *input_line_pointer = '\0';
2238 *(input_line_pointer - 1) = '\0';
2239 *input_line_pointer = c;
2241 /* Create the .note section. */
2242 note_secp = subseg_new (".note", 0);
2243 bfd_set_section_flags (note_secp, SEC_HAS_CONTENTS | SEC_READONLY);
2244 record_alignment (note_secp, 2);
2246 /* Process the version string. */
2247 len = strlen (name) + 1;
2249 /* PR 3456: Although the name field is padded out to an 4-byte
2250 boundary, the namesz field should not be adjusted. */
2251 i_note.namesz = len;
2252 i_note.descsz = 0; /* No description. */
2253 i_note.type = NT_VERSION;
2254 p = frag_more (sizeof (e_note.namesz));
2255 md_number_to_chars (p, i_note.namesz, sizeof (e_note.namesz));
2256 p = frag_more (sizeof (e_note.descsz));
2257 md_number_to_chars (p, i_note.descsz, sizeof (e_note.descsz));
2258 p = frag_more (sizeof (e_note.type));
2259 md_number_to_chars (p, i_note.type, sizeof (e_note.type));
2260 p = frag_more (len);
2261 memcpy (p, name, len);
2263 frag_align (2, 0, 0);
2265 subseg_set (seg, subseg);
2267 else
2268 as_bad (_("expected quoted string"));
2270 demand_empty_rest_of_line ();
2273 static void
2274 obj_elf_size (int ignore ATTRIBUTE_UNUSED)
2276 char *name;
2277 char c = get_symbol_name (&name);
2278 char *p;
2279 expressionS exp;
2280 symbolS *sym;
2282 p = input_line_pointer;
2283 *p = c;
2284 SKIP_WHITESPACE_AFTER_NAME ();
2285 if (*input_line_pointer != ',')
2287 *p = 0;
2288 as_bad (_("expected comma after name `%s' in .size directive"), name);
2289 *p = c;
2290 ignore_rest_of_line ();
2291 return;
2293 input_line_pointer++;
2294 expression (&exp);
2295 if (exp.X_op == O_absent)
2297 as_bad (_("missing expression in .size directive"));
2298 exp.X_op = O_constant;
2299 exp.X_add_number = 0;
2301 *p = 0;
2302 sym = symbol_find_or_make (name);
2303 *p = c;
2304 if (exp.X_op == O_constant)
2306 S_SET_SIZE (sym, exp.X_add_number);
2307 xfree (symbol_get_obj (sym)->size);
2308 symbol_get_obj (sym)->size = NULL;
2310 else
2312 symbol_get_obj (sym)->size = XNEW (expressionS);
2313 *symbol_get_obj (sym)->size = exp;
2316 /* If the symbol in the directive matches the current function being
2317 processed, indicate end of the current stream of ginsns. */
2318 if (flag_synth_cfi
2319 && S_IS_FUNCTION (sym) && sym == ginsn_data_func_symbol ())
2320 ginsn_data_end (symbol_temp_new_now ());
2322 demand_empty_rest_of_line ();
2325 /* Handle the ELF .type pseudo-op. This sets the type of a symbol.
2326 There are six syntaxes:
2328 The first (used on Solaris) is
2329 .type SYM,#function
2330 The second (used on UnixWare) is
2331 .type SYM,@function
2332 The third (reportedly to be used on Irix 6.0) is
2333 .type SYM STT_FUNC
2334 The fourth (used on NetBSD/Arm and Linux/ARM) is
2335 .type SYM,%function
2336 The fifth (used on SVR4/860) is
2337 .type SYM,"function"
2338 The sixth (emitted by recent SunPRO under Solaris) is
2339 .type SYM,[0-9]
2340 where the integer is the STT_* value.
2343 static char *
2344 obj_elf_type_name (char *cp)
2346 char *p;
2348 p = input_line_pointer;
2349 if (*input_line_pointer >= '0'
2350 && *input_line_pointer <= '9')
2352 while (*input_line_pointer >= '0'
2353 && *input_line_pointer <= '9')
2354 ++input_line_pointer;
2355 *cp = *input_line_pointer;
2356 *input_line_pointer = '\0';
2358 else
2359 *cp = get_symbol_name (&p);
2361 return p;
2364 static void
2365 obj_elf_type (int ignore ATTRIBUTE_UNUSED)
2367 char c;
2368 int type;
2369 const char *type_name;
2370 symbolS *sym;
2371 elf_symbol_type *elfsym;
2373 sym = get_sym_from_input_line_and_check ();
2374 c = *input_line_pointer;
2375 elfsym = (elf_symbol_type *) symbol_get_bfdsym (sym);
2377 if (*input_line_pointer == ',')
2378 ++input_line_pointer;
2380 SKIP_WHITESPACE ();
2381 if ( *input_line_pointer == '#'
2382 || *input_line_pointer == '@'
2383 || *input_line_pointer == '"'
2384 || *input_line_pointer == '%')
2385 ++input_line_pointer;
2387 type_name = obj_elf_type_name (& c);
2389 type = 0;
2390 if (strcmp (type_name, "function") == 0
2391 || strcmp (type_name, "2") == 0
2392 || strcmp (type_name, "STT_FUNC") == 0)
2393 type = BSF_FUNCTION;
2394 else if (strcmp (type_name, "object") == 0
2395 || strcmp (type_name, "1") == 0
2396 || strcmp (type_name, "STT_OBJECT") == 0)
2397 type = BSF_OBJECT;
2398 else if (strcmp (type_name, "tls_object") == 0
2399 || strcmp (type_name, "6") == 0
2400 || strcmp (type_name, "STT_TLS") == 0)
2401 type = BSF_OBJECT | BSF_THREAD_LOCAL;
2402 else if (strcmp (type_name, "notype") == 0
2403 || strcmp (type_name, "0") == 0
2404 || strcmp (type_name, "STT_NOTYPE") == 0)
2406 else if (strcmp (type_name, "common") == 0
2407 || strcmp (type_name, "5") == 0
2408 || strcmp (type_name, "STT_COMMON") == 0)
2410 type = BSF_OBJECT;
2412 if (! S_IS_COMMON (sym))
2414 if (S_IS_VOLATILE (sym))
2416 sym = symbol_clone (sym, 1);
2417 S_SET_SEGMENT (sym, bfd_com_section_ptr);
2418 S_SET_VALUE (sym, 0);
2419 S_SET_EXTERNAL (sym);
2420 symbol_set_frag (sym, &zero_address_frag);
2421 S_CLEAR_VOLATILE (sym);
2423 else if (S_IS_DEFINED (sym) || symbol_equated_p (sym))
2424 as_bad (_("symbol '%s' is already defined"), S_GET_NAME (sym));
2425 else
2427 /* FIXME: Is it safe to just change the section ? */
2428 S_SET_SEGMENT (sym, bfd_com_section_ptr);
2429 S_SET_VALUE (sym, 0);
2430 S_SET_EXTERNAL (sym);
2434 else if (strcmp (type_name, "gnu_indirect_function") == 0
2435 || strcmp (type_name, "10") == 0
2436 || strcmp (type_name, "STT_GNU_IFUNC") == 0)
2438 const struct elf_backend_data *bed;
2440 bed = get_elf_backend_data (stdoutput);
2441 if (bed->elf_osabi != ELFOSABI_NONE
2442 && bed->elf_osabi != ELFOSABI_GNU
2443 && bed->elf_osabi != ELFOSABI_FREEBSD)
2444 as_bad (_("symbol type \"%s\" is supported only by GNU "
2445 "and FreeBSD targets"), type_name);
2446 /* MIPS targets do not support IFUNCS. */
2447 else if (bed->target_id == MIPS_ELF_DATA)
2448 as_bad (_("symbol type \"%s\" is not supported by "
2449 "MIPS targets"), type_name);
2450 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_ifunc;
2451 type = BSF_FUNCTION | BSF_GNU_INDIRECT_FUNCTION;
2453 else if (strcmp (type_name, "gnu_unique_object") == 0)
2455 const struct elf_backend_data *bed;
2457 bed = get_elf_backend_data (stdoutput);
2458 if (bed->elf_osabi != ELFOSABI_NONE
2459 && bed->elf_osabi != ELFOSABI_GNU)
2460 as_bad (_("symbol type \"%s\" is supported only by GNU targets"),
2461 type_name);
2462 elf_tdata (stdoutput)->has_gnu_osabi |= elf_gnu_osabi_unique;
2463 type = BSF_OBJECT | BSF_GNU_UNIQUE;
2465 #ifdef md_elf_symbol_type
2466 else if ((type = md_elf_symbol_type (type_name, sym, elfsym)) != -1)
2468 #endif
2469 else
2470 as_bad (_("unrecognized symbol type \"%s\""), type_name);
2472 *input_line_pointer = c;
2474 if (*input_line_pointer == '"')
2475 ++input_line_pointer;
2477 #ifdef md_elf_symbol_type_change
2478 if (!md_elf_symbol_type_change (sym, elfsym, type))
2479 #endif
2481 flagword mask = BSF_FUNCTION | BSF_OBJECT;
2483 if (type != BSF_FUNCTION)
2484 mask |= BSF_GNU_INDIRECT_FUNCTION;
2485 if (type != BSF_OBJECT)
2487 mask |= BSF_GNU_UNIQUE | BSF_THREAD_LOCAL;
2489 if (S_IS_COMMON (sym))
2491 as_bad (_("cannot change type of common symbol '%s'"),
2492 S_GET_NAME (sym));
2493 mask = type = 0;
2497 /* Don't warn when changing to STT_NOTYPE. */
2498 if (type)
2500 flagword new = (elfsym->symbol.flags & ~mask) | type;
2502 if (new != (elfsym->symbol.flags | type))
2503 as_warn (_("symbol '%s' already has its type set"), S_GET_NAME (sym));
2504 elfsym->symbol.flags = new;
2506 else
2507 elfsym->symbol.flags &= ~mask;
2510 if (S_IS_FUNCTION (sym) && flag_synth_cfi)
2512 /* When using SCFI, .type directive indicates start of a new FDE for SCFI
2513 processing. So, we must first demarcate the previous block of ginsns,
2514 if any, to mark the end of a previous FDE. */
2515 if (frchain_now->frch_ginsn_data)
2516 ginsn_data_end (symbol_temp_new_now ());
2517 ginsn_data_begin (sym);
2520 demand_empty_rest_of_line ();
2523 static segT comment_section;
2525 static void
2526 obj_elf_ident (int ignore ATTRIBUTE_UNUSED)
2528 segT old_section = now_seg;
2529 int old_subsection = now_subseg;
2531 #ifdef md_flush_pending_output
2532 md_flush_pending_output ();
2533 #endif
2535 if (!comment_section)
2537 char *p;
2538 comment_section = subseg_new (".comment", 0);
2539 bfd_set_section_flags (comment_section, (SEC_READONLY | SEC_HAS_CONTENTS
2540 | SEC_MERGE | SEC_STRINGS));
2541 comment_section->entsize = 1;
2542 #ifdef md_elf_section_change_hook
2543 md_elf_section_change_hook ();
2544 #endif
2545 p = frag_more (1);
2546 *p = 0;
2548 else
2550 subseg_set (comment_section, 0);
2551 #ifdef md_elf_section_change_hook
2552 md_elf_section_change_hook ();
2553 #endif
2555 stringer (8 + 1);
2556 subseg_set (old_section, old_subsection);
2557 #ifdef md_elf_section_change_hook
2558 md_elf_section_change_hook ();
2559 #endif
2562 #ifdef INIT_STAB_SECTION
2564 /* The first entry in a .stabs section is special. */
2566 void
2567 obj_elf_init_stab_section (segT stab, segT stabstr)
2569 char *file;
2570 char *p;
2571 unsigned int stroff;
2573 /* Force the section to align to a longword boundary. Without this,
2574 UnixWare ar crashes. */
2575 bfd_set_section_alignment (stab, 2);
2577 /* Make space for this first symbol. */
2578 p = frag_more (12);
2579 /* Zero it out. */
2580 memset (p, 0, 12);
2581 file = remap_debug_filename (as_where (NULL));
2582 stroff = get_stab_string_offset (file, stabstr);
2583 know (stroff == 1 || (stroff == 0 && file[0] == '\0'));
2584 md_number_to_chars (p, stroff, 4);
2585 seg_info (stab)->stabu.p = p;
2586 free (file);
2589 #endif
2591 /* Fill in the counts in the first entry in a .stabs section. */
2593 static void
2594 adjust_stab_sections (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
2596 char *name;
2597 asection *strsec;
2598 char *p;
2599 int strsz, nsyms;
2601 if (!startswith (sec->name, ".stab"))
2602 return;
2603 if (!strcmp ("str", sec->name + strlen (sec->name) - 3))
2604 return;
2606 name = concat (sec->name, "str", NULL);
2607 strsec = bfd_get_section_by_name (abfd, name);
2608 if (strsec)
2609 strsz = bfd_section_size (strsec);
2610 else
2611 strsz = 0;
2612 nsyms = bfd_section_size (sec) / 12 - 1;
2614 p = seg_info (sec)->stabu.p;
2615 gas_assert (p != 0);
2617 bfd_h_put_16 (abfd, nsyms, p + 6);
2618 bfd_h_put_32 (abfd, strsz, p + 8);
2619 free (name);
2622 #ifdef NEED_ECOFF_DEBUG
2624 /* This function is called by the ECOFF code. It is supposed to
2625 record the external symbol information so that the backend can
2626 write it out correctly. The ELF backend doesn't actually handle
2627 this at the moment, so we do it ourselves. We save the information
2628 in the symbol. */
2630 #ifdef OBJ_MAYBE_ELF
2631 static
2632 #endif
2633 void
2634 elf_ecoff_set_ext (symbolS *sym, struct ecoff_extr *ext)
2636 symbol_get_bfdsym (sym)->udata.p = ext;
2639 /* This function is called by bfd_ecoff_debug_externals. It is
2640 supposed to *EXT to the external symbol information, and return
2641 whether the symbol should be used at all. */
2643 static bool
2644 elf_get_extr (asymbol *sym, EXTR *ext)
2646 if (sym->udata.p == NULL)
2647 return false;
2648 *ext = *(EXTR *) sym->udata.p;
2649 return true;
2652 /* This function is called by bfd_ecoff_debug_externals. It has
2653 nothing to do for ELF. */
2655 static void
2656 elf_set_index (asymbol *sym ATTRIBUTE_UNUSED,
2657 bfd_size_type indx ATTRIBUTE_UNUSED)
2661 #endif /* NEED_ECOFF_DEBUG */
2663 void
2664 elf_frob_symbol (symbolS *symp, int *puntp)
2666 struct elf_obj_sy *sy_obj;
2667 expressionS *size;
2668 struct elf_versioned_name_list *versioned_name;
2670 #ifdef NEED_ECOFF_DEBUG
2671 if (ECOFF_DEBUGGING)
2672 ecoff_frob_symbol (symp);
2673 #endif
2675 sy_obj = symbol_get_obj (symp);
2677 size = sy_obj->size;
2678 if (size != NULL)
2680 if (resolve_expression (size)
2681 && size->X_op == O_constant)
2682 S_SET_SIZE (symp, size->X_add_number);
2683 else
2685 if (!flag_allow_nonconst_size)
2686 as_bad (_(".size expression for %s "
2687 "does not evaluate to a constant"), S_GET_NAME (symp));
2688 else
2689 as_warn (_(".size expression for %s "
2690 "does not evaluate to a constant"), S_GET_NAME (symp));
2692 free (sy_obj->size);
2693 sy_obj->size = NULL;
2696 versioned_name = sy_obj->versioned_name;
2697 if (versioned_name)
2699 /* This symbol was given a new name with the .symver directive.
2700 If this is an external reference, just rename the symbol to
2701 include the version string. This will make the relocs be
2702 against the correct versioned symbol. */
2704 /* We will have already reported an version error. */
2705 if (sy_obj->bad_version)
2706 *puntp = true;
2707 /* elf_frob_file_before_adjust only allows one version symbol for
2708 renamed symbol. */
2709 else if (sy_obj->rename)
2710 S_SET_NAME (symp, versioned_name->name);
2711 else if (S_IS_COMMON (symp))
2713 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
2714 versioned_name->name, S_GET_NAME (symp));
2715 *puntp = true;
2717 else
2719 asymbol *bfdsym;
2720 elf_symbol_type *elfsym;
2722 /* This is a definition. Add an alias for each version.
2723 FIXME: Using an alias will permit the debugging information
2724 to refer to the right symbol. However, it's not clear
2725 whether it is the best approach. */
2727 /* FIXME: Creating a new symbol here is risky. We're
2728 in the final loop over the symbol table. We can
2729 get away with it only because the symbol goes to
2730 the end of the list, where the loop will still see
2731 it. It would probably be better to do this in
2732 obj_frob_file_before_adjust. */
2733 for (; versioned_name != NULL;
2734 versioned_name = versioned_name->next)
2736 symbolS *symp2 = symbol_find_or_make (versioned_name->name);
2738 S_SET_SEGMENT (symp2, S_GET_SEGMENT (symp));
2740 /* Subtracting out the frag address here is a hack
2741 because we are in the middle of the final loop. */
2742 S_SET_VALUE (symp2,
2743 (S_GET_VALUE (symp)
2744 - (symbol_get_frag (symp)->fr_address
2745 / OCTETS_PER_BYTE)));
2747 symbol_set_frag (symp2, symbol_get_frag (symp));
2749 /* This will copy over the size information. */
2750 copy_symbol_attributes (symp2, symp);
2752 S_SET_OTHER (symp2, S_GET_OTHER (symp));
2754 if (S_IS_WEAK (symp))
2755 S_SET_WEAK (symp2);
2757 if (S_IS_EXTERNAL (symp))
2758 S_SET_EXTERNAL (symp2);
2761 switch (sy_obj->visibility)
2763 case visibility_unchanged:
2764 break;
2765 case visibility_hidden:
2766 bfdsym = symbol_get_bfdsym (symp);
2767 elfsym = elf_symbol_from (bfdsym);
2768 elfsym->internal_elf_sym.st_other &= ~3;
2769 elfsym->internal_elf_sym.st_other |= STV_HIDDEN;
2770 break;
2771 case visibility_remove:
2772 /* Don't remove the symbol if it is used in relocation.
2773 Instead, mark it as to be removed and issue an error
2774 if the symbol has more than one versioned name. */
2775 if (symbol_used_in_reloc_p (symp))
2777 if (sy_obj->versioned_name->next != NULL)
2778 as_bad (_("symbol '%s' with multiple versions cannot be used in relocation"),
2779 S_GET_NAME (symp));
2780 symbol_mark_removed (symp);
2782 else
2783 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
2784 break;
2785 case visibility_local:
2786 S_CLEAR_EXTERNAL (symp);
2787 break;
2792 /* Double check weak symbols. */
2793 if (S_IS_WEAK (symp))
2795 if (S_IS_COMMON (symp))
2796 as_bad (_("symbol `%s' can not be both weak and common"),
2797 S_GET_NAME (symp));
2801 /* Fix up SYMPP which has been marked to be removed by .symver. */
2803 void
2804 elf_fixup_removed_symbol (symbolS **sympp)
2806 symbolS *symp = *sympp;
2807 struct elf_obj_sy *sy_obj = symbol_get_obj (symp);
2809 /* Replace the removed symbol with the versioned symbol. */
2810 symp = symbol_find (sy_obj->versioned_name->name);
2811 *sympp = symp;
2814 struct group_list
2816 asection **head; /* Section lists. */
2817 unsigned int num_group; /* Number of lists. */
2818 htab_t indexes; /* Maps group name to index in head array. */
2821 static struct group_list groups;
2823 /* Called via bfd_map_over_sections. If SEC is a member of a group,
2824 add it to a list of sections belonging to the group. INF is a
2825 pointer to a struct group_list, which is where we store the head of
2826 each list. If its link_to_symbol_name isn't NULL, set up its
2827 linked-to section. */
2829 static void
2830 build_additional_section_info (bfd *abfd ATTRIBUTE_UNUSED,
2831 asection *sec, void *inf)
2833 struct group_list *list = (struct group_list *) inf;
2834 const char *group_name = elf_group_name (sec);
2835 unsigned int i;
2836 unsigned int *elem_idx;
2837 unsigned int *idx_ptr;
2839 if (sec->map_head.linked_to_symbol_name)
2841 symbolS *linked_to_sym;
2842 linked_to_sym = symbol_find (sec->map_head.linked_to_symbol_name);
2843 if (!linked_to_sym || !S_IS_DEFINED (linked_to_sym))
2844 as_bad (_("undefined linked-to symbol `%s' on section `%s'"),
2845 sec->map_head.linked_to_symbol_name,
2846 bfd_section_name (sec));
2847 else
2848 elf_linked_to_section (sec) = S_GET_SEGMENT (linked_to_sym);
2851 if (group_name == NULL)
2852 return;
2854 /* If this group already has a list, add the section to the head of
2855 the list. */
2856 elem_idx = (unsigned int *) str_hash_find (list->indexes, group_name);
2857 if (elem_idx != NULL)
2859 elf_next_in_group (sec) = list->head[*elem_idx];
2860 list->head[*elem_idx] = sec;
2861 return;
2864 /* New group. Make the arrays bigger in chunks to minimize calls to
2865 realloc. */
2866 i = list->num_group;
2867 if ((i & 127) == 0)
2869 unsigned int newsize = i + 128;
2870 list->head = XRESIZEVEC (asection *, list->head, newsize);
2872 list->head[i] = sec;
2873 list->num_group += 1;
2875 /* Add index to hash. */
2876 idx_ptr = XNEW (unsigned int);
2877 *idx_ptr = i;
2878 str_hash_insert (list->indexes, group_name, idx_ptr, 0);
2881 static void
2882 free_section_idx (void *ent)
2884 string_tuple_t *tuple = ent;
2885 free ((char *) tuple->value);
2888 /* Create symbols for group signature. */
2890 void
2891 elf_adjust_symtab (void)
2893 unsigned int i;
2895 /* Go find section groups. */
2896 groups.num_group = 0;
2897 groups.head = NULL;
2898 groups.indexes = htab_create_alloc (16, hash_string_tuple, eq_string_tuple,
2899 free_section_idx, notes_calloc, NULL);
2900 bfd_map_over_sections (stdoutput, build_additional_section_info,
2901 &groups);
2903 /* Make the SHT_GROUP sections that describe each section group. We
2904 can't set up the section contents here yet, because elf section
2905 indices have yet to be calculated. elf.c:set_group_contents does
2906 the rest of the work. */
2907 for (i = 0; i < groups.num_group; i++)
2909 const char *group_name = elf_group_name (groups.head[i]);
2910 const char *sec_name;
2911 asection *s;
2912 flagword flags;
2913 struct symbol *sy;
2915 flags = SEC_READONLY | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_GROUP;
2916 for (s = groups.head[i]; s != NULL; s = elf_next_in_group (s))
2917 if ((s->flags ^ flags) & SEC_LINK_ONCE)
2919 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
2920 if (s != groups.head[i])
2922 as_warn (_("assuming all members of group `%s' are COMDAT"),
2923 group_name);
2924 break;
2928 sec_name = ".group";
2929 s = subseg_force_new (sec_name, 0);
2930 if (s == NULL
2931 || !bfd_set_section_flags (s, flags)
2932 || !bfd_set_section_alignment (s, 2))
2934 as_fatal (_("can't create group: %s"),
2935 bfd_errmsg (bfd_get_error ()));
2937 elf_section_type (s) = SHT_GROUP;
2939 /* Pass a pointer to the first section in this group. */
2940 elf_next_in_group (s) = groups.head[i];
2941 elf_sec_group (groups.head[i]) = s;
2942 /* Make sure that the signature symbol for the group has the
2943 name of the group. */
2944 sy = symbol_find_exact (group_name);
2945 if (!sy || !symbol_on_chain (sy, symbol_rootP, symbol_lastP))
2947 /* Create the symbol now. */
2948 sy = symbol_new (group_name, now_seg, frag_now, 0);
2949 #ifdef TE_SOLARIS
2950 /* Before Solaris 11 build 154, Sun ld rejects local group
2951 signature symbols, so make them weak hidden instead. */
2952 symbol_get_bfdsym (sy)->flags |= BSF_WEAK;
2953 S_SET_OTHER (sy, STV_HIDDEN);
2954 #else
2955 symbol_get_obj (sy)->local = 1;
2956 #endif
2957 symbol_table_insert (sy);
2959 elf_group_id (s) = symbol_get_bfdsym (sy);
2960 /* Mark the group signature symbol as used so that it will be
2961 included in the symbol table. */
2962 symbol_mark_used_in_reloc (sy);
2966 void
2967 elf_frob_file (void)
2969 bfd_map_over_sections (stdoutput, adjust_stab_sections, NULL);
2971 #ifdef elf_tc_final_processing
2972 elf_tc_final_processing ();
2973 #endif
2976 /* It removes any unneeded versioned symbols from the symbol table. */
2978 void
2979 elf_frob_file_before_adjust (void)
2981 if (symbol_rootP)
2983 symbolS *symp;
2985 for (symp = symbol_rootP; symp; symp = symbol_next (symp))
2987 struct elf_obj_sy *sy_obj = symbol_get_obj (symp);
2988 int is_defined = !!S_IS_DEFINED (symp);
2990 if (sy_obj->versioned_name)
2992 char *p = strchr (sy_obj->versioned_name->name,
2993 ELF_VER_CHR);
2995 if (sy_obj->rename)
2997 /* The @@@ syntax is a special case. If the symbol is
2998 not defined, 2 `@'s will be removed from the
2999 versioned_name. Otherwise, 1 `@' will be removed. */
3000 size_t l = strlen (&p[3]) + 1;
3001 memmove (&p[1 + is_defined], &p[3], l);
3004 if (!is_defined)
3006 /* Verify that the name isn't using the @@ syntax--this
3007 is reserved for definitions of the default version
3008 to link against. */
3009 if (!sy_obj->rename && p[1] == ELF_VER_CHR)
3011 as_bad (_("invalid attempt to declare external "
3012 "version name as default in symbol `%s'"),
3013 sy_obj->versioned_name->name);
3014 return;
3017 /* Only one version symbol is allowed for undefined
3018 symbol. */
3019 if (sy_obj->versioned_name->next)
3021 as_bad (_("multiple versions [`%s'|`%s'] for "
3022 "symbol `%s'"),
3023 sy_obj->versioned_name->name,
3024 sy_obj->versioned_name->next->name,
3025 S_GET_NAME (symp));
3026 return;
3029 sy_obj->rename = true;
3033 /* If there was .symver or .weak, but symbol was neither
3034 defined nor used anywhere, remove it. */
3035 if (!is_defined
3036 && (sy_obj->versioned_name || S_IS_WEAK (symp))
3037 && symbol_used_p (symp) == 0
3038 && symbol_used_in_reloc_p (symp) == 0)
3039 symbol_remove (symp, &symbol_rootP, &symbol_lastP);
3044 /* It is required that we let write_relocs have the opportunity to
3045 optimize away fixups before output has begun, since it is possible
3046 to eliminate all fixups for a section and thus we never should
3047 have generated the relocation section. */
3049 void
3050 elf_frob_file_after_relocs (void)
3052 unsigned int i;
3054 /* Set SHT_GROUP section size. */
3055 for (i = 0; i < groups.num_group; i++)
3057 asection *s, *head, *group;
3058 bfd_size_type size;
3060 head = groups.head[i];
3061 size = 4;
3062 for (s = head; s != NULL; s = elf_next_in_group (s))
3063 size += (s->flags & SEC_RELOC) != 0 ? 8 : 4;
3065 group = elf_sec_group (head);
3066 subseg_set (group, 0);
3067 bfd_set_section_size (group, size);
3068 group->contents = (unsigned char *) frag_more (size);
3069 frag_now->fr_fix = frag_now_fix_octets ();
3070 frag_wane (frag_now);
3073 #ifdef NEED_ECOFF_DEBUG
3074 if (ECOFF_DEBUGGING)
3075 /* Generate the ECOFF debugging information. */
3077 const struct ecoff_debug_swap *debug_swap;
3078 struct ecoff_debug_info debug;
3079 char *buf;
3080 asection *sec;
3082 debug_swap
3083 = get_elf_backend_data (stdoutput)->elf_backend_ecoff_debug_swap;
3084 know (debug_swap != NULL);
3085 ecoff_build_debug (&debug.symbolic_header, &buf, debug_swap);
3087 /* Set up the pointers in debug. */
3088 debug.alloc_syments = true;
3089 #define SET(ptr, offset, type) \
3090 debug.ptr = (type) (buf + debug.symbolic_header.offset)
3092 SET (line, cbLineOffset, unsigned char *);
3093 SET (external_dnr, cbDnOffset, void *);
3094 SET (external_pdr, cbPdOffset, void *);
3095 SET (external_sym, cbSymOffset, void *);
3096 SET (external_opt, cbOptOffset, void *);
3097 SET (external_aux, cbAuxOffset, union aux_ext *);
3098 SET (ss, cbSsOffset, char *);
3099 SET (external_fdr, cbFdOffset, void *);
3100 SET (external_rfd, cbRfdOffset, void *);
3101 /* ssext and external_ext are set up just below. */
3103 #undef SET
3105 /* Set up the external symbols. */
3106 debug.ssext = debug.ssext_end = NULL;
3107 debug.external_ext = debug.external_ext_end = NULL;
3108 if (! bfd_ecoff_debug_externals (stdoutput, &debug, debug_swap, true,
3109 elf_get_extr, elf_set_index))
3110 as_fatal (_("failed to set up debugging information: %s"),
3111 bfd_errmsg (bfd_get_error ()));
3113 sec = bfd_get_section_by_name (stdoutput, ".mdebug");
3114 gas_assert (sec != NULL);
3116 know (!stdoutput->output_has_begun);
3118 /* We set the size of the section, call bfd_set_section_contents
3119 to force the ELF backend to allocate a file position, and then
3120 write out the data. FIXME: Is this really the best way to do
3121 this? */
3122 bfd_set_section_size (sec, bfd_ecoff_debug_size (stdoutput, &debug,
3123 debug_swap));
3125 /* Pass BUF to bfd_set_section_contents because this will
3126 eventually become a call to fwrite, and ISO C prohibits
3127 passing a NULL pointer to a stdio function even if the
3128 pointer will not be used. */
3129 if (! bfd_set_section_contents (stdoutput, sec, buf, 0, 0))
3130 as_fatal (_("can't start writing .mdebug section: %s"),
3131 bfd_errmsg (bfd_get_error ()));
3133 know (stdoutput->output_has_begun);
3134 know (sec->filepos != 0);
3136 if (! bfd_ecoff_write_debug (stdoutput, &debug, debug_swap,
3137 sec->filepos))
3138 as_fatal (_("could not write .mdebug section: %s"),
3139 bfd_errmsg (bfd_get_error ()));
3141 #endif /* NEED_ECOFF_DEBUG */
3144 static void
3145 elf_generate_asm_lineno (void)
3147 #ifdef NEED_ECOFF_DEBUG
3148 if (ECOFF_DEBUGGING)
3149 ecoff_generate_asm_lineno ();
3150 #endif
3153 static void
3154 elf_process_stab (int what ATTRIBUTE_UNUSED,
3155 const char *string ATTRIBUTE_UNUSED,
3156 int type ATTRIBUTE_UNUSED,
3157 int other ATTRIBUTE_UNUSED,
3158 int desc ATTRIBUTE_UNUSED)
3160 #ifdef NEED_ECOFF_DEBUG
3161 if (ECOFF_DEBUGGING)
3162 ecoff_stab (what, string, type, other, desc);
3163 #endif
3166 static int
3167 elf_separate_stab_sections (void)
3169 #ifdef NEED_ECOFF_DEBUG
3170 return (!ECOFF_DEBUGGING);
3171 #else
3172 return 1;
3173 #endif
3176 static void
3177 elf_init_stab_section (segT stab, segT stabstr)
3179 #ifdef NEED_ECOFF_DEBUG
3180 if (!ECOFF_DEBUGGING)
3181 #endif
3182 obj_elf_init_stab_section (stab, stabstr);
3185 /* This is called when the assembler starts. */
3187 void
3188 elf_begin (void)
3190 asection *s;
3192 /* Add symbols for the known sections to the symbol table. */
3193 s = bfd_get_section_by_name (stdoutput, TEXT_SECTION_NAME);
3194 symbol_table_insert (section_symbol (s));
3195 s = bfd_get_section_by_name (stdoutput, DATA_SECTION_NAME);
3196 symbol_table_insert (section_symbol (s));
3197 s = bfd_get_section_by_name (stdoutput, BSS_SECTION_NAME);
3198 symbol_table_insert (section_symbol (s));
3199 elf_com_section_ptr = bfd_com_section_ptr;
3200 previous_section = NULL;
3201 previous_subsection = 0;
3202 comment_section = NULL;
3203 memset (&groups, 0, sizeof (groups));
3206 void
3207 elf_end (void)
3209 while (section_stack)
3211 struct section_stack *top = section_stack;
3212 section_stack = top->next;
3213 free (top);
3215 while (recorded_attributes)
3217 struct recorded_attribute_info *rai = recorded_attributes;
3218 recorded_attributes = rai->next;
3219 free (rai);
3221 if (groups.indexes)
3223 htab_delete (groups.indexes);
3224 free (groups.head);
3228 const struct format_ops elf_format_ops =
3230 bfd_target_elf_flavour,
3231 0, /* dfl_leading_underscore */
3232 1, /* emit_section_symbols */
3233 elf_begin,
3234 elf_end,
3235 elf_file_symbol,
3236 elf_frob_symbol,
3237 elf_frob_file,
3238 elf_frob_file_before_adjust,
3239 0, /* obj_frob_file_before_fix */
3240 elf_frob_file_after_relocs,
3241 elf_s_get_size, elf_s_set_size,
3242 elf_s_get_align, elf_s_set_align,
3243 elf_s_get_other,
3244 elf_s_set_other,
3245 0, /* s_get_desc */
3246 0, /* s_set_desc */
3247 0, /* s_get_type */
3248 0, /* s_set_type */
3249 elf_copy_symbol_attributes,
3250 elf_generate_asm_lineno,
3251 elf_process_stab,
3252 elf_separate_stab_sections,
3253 elf_init_stab_section,
3254 elf_sec_sym_ok_for_reloc,
3255 elf_pop_insert,
3256 #ifdef NEED_ECOFF_DEBUG
3257 elf_ecoff_set_ext,
3258 #else
3259 0, /* ecoff_set_ext */
3260 #endif
3261 elf_obj_read_begin_hook,
3262 elf_obj_symbol_new_hook,
3263 elf_obj_symbol_clone_hook,
3264 elf_adjust_symtab