1 /* ELF object file format
2 Copyright (C) 1992-2023 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
21 #define OBJ_HEADER "obj-elf.h"
23 #include "safe-ctype.h"
26 #include "dwarf2dbg.h"
28 #ifndef ECOFF_DEBUGGING
29 #define ECOFF_DEBUGGING 0
31 #define NEED_ECOFF_DEBUG
34 #ifdef NEED_ECOFF_DEBUG
36 #include "bfd/ecoff-bfd.h"
40 #include "elf/alpha.h"
52 #include "elf/x86-64.h"
60 #include "elf/nios2.h"
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);
83 static const pseudo_typeS elf_pseudo_table
[] =
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},
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
},
108 /* These are used for stabs-in-elf configurations. */
109 {"line", obj_elf_line
, 0},
111 /* This is a GNU extension to handle symbol versions. */
112 {"symver", obj_elf_symver
, 0},
114 /* A GNU extension to change subsection only. */
115 {"subsection", obj_elf_subsection
, 0},
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},
121 /* A GNU extension for object attributes. */
122 {"gnu_attribute", obj_elf_gnu_attribute
, 0},
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 },
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},
136 {"tls_common", obj_elf_tls_common
, 0},
142 static const pseudo_typeS ecoff_debug_pseudo_table
[] =
144 #ifdef NEED_ECOFF_DEBUG
145 /* COFF style debugging information for ECOFF. .ln is not used; .loc
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 },
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 },
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 },
172 /* Other ECOFF directives. */
173 { "extern", ecoff_directive_extern
, 0 },
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 },
185 {NULL
, NULL
, 0} /* end sentinel */
189 #include "aout/aout64.h"
191 /* This is called when the assembler starts. */
193 asection
*elf_com_section_ptr
;
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
;
211 elf_pop_insert (void)
213 pop_insert (elf_pseudo_table
);
215 pop_insert (ecoff_debug_pseudo_table
);
219 elf_s_get_size (symbolS
*sym
)
221 return S_GET_SIZE (sym
);
225 elf_s_set_size (symbolS
*sym
, bfd_vma sz
)
227 S_SET_SIZE (sym
, sz
);
231 elf_s_get_align (symbolS
*sym
)
233 return S_GET_ALIGN (sym
);
237 elf_s_set_align (symbolS
*sym
, bfd_vma align
)
239 S_SET_ALIGN (sym
, align
);
243 elf_s_get_other (symbolS
*sym
)
245 return elf_symbol (symbol_get_bfdsym (sym
))->internal_elf_sym
.st_other
;
249 elf_s_set_other (symbolS
*sym
, int other
)
251 S_SET_OTHER (sym
, other
);
255 elf_sec_sym_ok_for_reloc (asection
*sec
)
257 return obj_sec_sym_ok_for_reloc (sec
);
261 elf_file_symbol (const char *s
)
264 symbolS
*sym
= symbol_new (s
, absolute_section
, &zero_address_frag
, 0);
265 size_t name_length
= strlen (s
);
267 if (name_length
> strlen (S_GET_NAME (sym
)))
269 obstack_grow (¬es
, s
, name_length
+ 1);
270 S_SET_NAME (sym
, (const char *) obstack_finish (¬es
));
273 strcpy ((char *) S_GET_NAME (sym
), s
);
275 symbol_get_bfdsym (sym
)->flags
|= BSF_FILE
;
277 if (symbol_rootP
!= sym
278 && ((bsym
= symbol_get_bfdsym (symbol_rootP
)) == NULL
279 || (bsym
->flags
& BSF_FILE
) == 0))
281 symbol_remove (sym
, &symbol_rootP
, &symbol_lastP
);
282 symbol_insert (sym
, symbol_rootP
, &symbol_rootP
, &symbol_lastP
);
286 verify_symbol_chain (symbol_rootP
, symbol_lastP
);
289 #ifdef NEED_ECOFF_DEBUG
294 /* Called from read.c:s_comm after we've parsed .comm symbol, size.
295 Parse a possible alignment value. */
298 elf_common_parse (int ignore ATTRIBUTE_UNUSED
, symbolS
*symbolP
, addressT size
)
301 int is_local
= symbol_get_obj (symbolP
)->local
;
303 if (*input_line_pointer
== ',')
305 char *save
= input_line_pointer
;
307 input_line_pointer
++;
310 if (*input_line_pointer
== '"')
312 /* For sparc. Accept .common symbol, length, "bss" */
313 input_line_pointer
++;
314 /* Some use the dot, some don't. */
315 if (*input_line_pointer
== '.')
316 input_line_pointer
++;
317 /* Some say data, some say bss. */
318 if (startswith (input_line_pointer
, "bss\""))
319 input_line_pointer
+= 4;
320 else if (startswith (input_line_pointer
, "data\""))
321 input_line_pointer
+= 5;
324 char *p
= input_line_pointer
;
329 while (!is_end_of_line
[(unsigned char) *input_line_pointer
])
330 if (*input_line_pointer
++ == '"')
332 c
= *input_line_pointer
;
333 *input_line_pointer
= '\0';
334 as_bad (_("bad .common segment %s"), p
);
335 *input_line_pointer
= c
;
336 ignore_rest_of_line ();
339 /* ??? Don't ask me why these are always global. */
344 input_line_pointer
= save
;
345 align
= parse_align (is_local
);
346 if (align
== (addressT
) -1)
353 bss_alloc (symbolP
, size
, align
);
354 S_CLEAR_EXTERNAL (symbolP
);
358 S_SET_VALUE (symbolP
, size
);
359 S_SET_ALIGN (symbolP
, align
);
360 S_SET_EXTERNAL (symbolP
);
361 S_SET_SEGMENT (symbolP
, elf_com_section_ptr
);
364 symbol_get_bfdsym (symbolP
)->flags
|= BSF_OBJECT
;
370 obj_elf_common (int is_common
)
372 if (flag_mri
&& is_common
)
375 s_comm_internal (0, elf_common_parse
);
379 obj_elf_tls_common (int ignore ATTRIBUTE_UNUSED
)
381 symbolS
*symbolP
= s_comm_internal (0, elf_common_parse
);
384 symbol_get_bfdsym (symbolP
)->flags
|= BSF_THREAD_LOCAL
;
388 obj_elf_lcomm (int ignore ATTRIBUTE_UNUSED
)
390 symbolS
*symbolP
= s_comm_internal (0, s_lcomm_internal
);
393 symbol_get_bfdsym (symbolP
)->flags
|= BSF_OBJECT
;
397 get_sym_from_input_line_and_check (void)
403 c
= get_symbol_name (& name
);
404 sym
= symbol_find_or_make (name
);
405 *input_line_pointer
= c
;
406 SKIP_WHITESPACE_AFTER_NAME ();
408 /* There is no symbol name if input_line_pointer has not moved. */
409 if (name
== input_line_pointer
)
410 as_bad (_("Missing symbol name in directive"));
415 obj_elf_local (int ignore ATTRIBUTE_UNUSED
)
422 symbolP
= get_sym_from_input_line_and_check ();
423 c
= *input_line_pointer
;
424 S_CLEAR_EXTERNAL (symbolP
);
425 symbol_get_obj (symbolP
)->local
= 1;
428 input_line_pointer
++;
430 if (*input_line_pointer
== '\n')
435 demand_empty_rest_of_line ();
439 obj_elf_weak (int ignore ATTRIBUTE_UNUSED
)
446 symbolP
= get_sym_from_input_line_and_check ();
447 c
= *input_line_pointer
;
448 S_SET_WEAK (symbolP
);
451 input_line_pointer
++;
453 if (*input_line_pointer
== '\n')
458 demand_empty_rest_of_line ();
462 obj_elf_visibility (int visibility
)
467 elf_symbol_type
*elfsym
;
471 symbolP
= get_sym_from_input_line_and_check ();
473 bfdsym
= symbol_get_bfdsym (symbolP
);
474 elfsym
= elf_symbol_from (bfdsym
);
478 elfsym
->internal_elf_sym
.st_other
&= ~3;
479 elfsym
->internal_elf_sym
.st_other
|= visibility
;
481 c
= *input_line_pointer
;
484 input_line_pointer
++;
488 if (*input_line_pointer
== '\n')
494 demand_empty_rest_of_line ();
497 static segT previous_section
;
498 static int previous_subsection
;
502 struct section_stack
*next
;
504 int subseg
, prev_subseg
;
507 static struct section_stack
*section_stack
;
509 /* ELF section flags for unique sections. */
510 #define SEC_ASSEMBLER_SHF_MASK SHF_GNU_RETAIN
512 /* Return TRUE iff SEC matches the section info INF. */
515 get_section_by_match (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *inf
)
517 struct elf_section_match
*match
= (struct elf_section_match
*) inf
;
518 const char *gname
= match
->group_name
;
519 const char *group_name
= elf_group_name (sec
);
520 const char *linked_to_symbol_name
521 = sec
->map_head
.linked_to_symbol_name
;
522 unsigned int sh_info
= elf_section_data (sec
)->this_hdr
.sh_info
;
523 bfd_vma sh_flags
= (elf_section_data (sec
)->this_hdr
.sh_flags
524 & SEC_ASSEMBLER_SHF_MASK
);
526 return (sh_info
== match
->sh_info
527 && sh_flags
== match
->sh_flags
528 && ((bfd_section_flags (sec
) & SEC_ASSEMBLER_SECTION_ID
)
529 == (match
->flags
& SEC_ASSEMBLER_SECTION_ID
))
530 && sec
->section_id
== match
->section_id
531 && (group_name
== gname
532 || (group_name
!= NULL
534 && strcmp (group_name
, gname
) == 0))
535 && (linked_to_symbol_name
== match
->linked_to_symbol_name
536 || (linked_to_symbol_name
!= NULL
537 && match
->linked_to_symbol_name
!= NULL
538 && strcmp (linked_to_symbol_name
,
539 match
->linked_to_symbol_name
) == 0)));
542 /* Handle the .section pseudo-op. This code supports two different
545 The first is found on Solaris, and looks like
546 .section ".sec1",#alloc,#execinstr,#write
547 Here the names after '#' are the SHF_* flags to turn on for the
548 section. I'm not sure how it determines the SHT_* type (BFD
549 doesn't really give us control over the type, anyhow).
551 The second format is found on UnixWare, and probably most SVR4
552 machines, and looks like
553 .section .sec1,"a",@progbits
554 The quoted string may contain any combination of a, w, x, and
555 represents the SHF_* flags to turn on for the section. The string
556 beginning with '@' can be progbits or nobits. There should be
557 other possibilities, but I don't know what they are. In any case,
558 BFD doesn't really let us set the section type. */
561 obj_elf_change_section (const char *name
,
565 struct elf_section_match
*match_p
,
572 const struct elf_backend_data
*bed
;
573 const struct bfd_elf_special_section
*ssect
;
577 static struct elf_section_match unused_match
;
578 match_p
= &unused_match
;
581 #ifdef md_flush_pending_output
582 md_flush_pending_output ();
585 /* Switch to the section, creating it if necessary. */
588 struct section_stack
*elt
;
589 elt
= XNEW (struct section_stack
);
590 elt
->next
= section_stack
;
592 elt
->prev_seg
= previous_section
;
593 elt
->subseg
= now_subseg
;
594 elt
->prev_subseg
= previous_subsection
;
598 obj_elf_section_change_hook ();
600 old_sec
= bfd_get_section_by_name_if (stdoutput
, name
, get_section_by_match
,
608 sec
= subseg_force_new (name
, 0);
610 bed
= get_elf_backend_data (stdoutput
);
611 ssect
= (*bed
->get_sec_type_attr
) (stdoutput
, sec
);
615 bool override
= false;
617 if (type
== SHT_NULL
)
619 else if (type
!= ssect
->type
)
622 /* Some older versions of gcc will emit
624 .section .init_array,"aw",@progbits
626 for __attribute__ ((section (".init_array"))).
627 "@progbits" is incorrect. Also for x86-64 large bss
628 sections, some older versions of gcc will emit
630 .section .lbss,"aw",@progbits
632 "@progbits" is incorrect. */
634 && (bed
->s
->arch_size
!= 64
635 || !(ssect
->attr
& SHF_X86_64_LARGE
))
637 && ssect
->type
!= SHT_INIT_ARRAY
638 && ssect
->type
!= SHT_FINI_ARRAY
639 && ssect
->type
!= SHT_PREINIT_ARRAY
)
641 /* We allow to specify any type for a .note section. */
642 if (ssect
->type
!= SHT_NOTE
643 /* Processor and application defined types are allowed too. */
644 && type
< SHT_LOPROC
)
645 as_warn (_("setting incorrect section type for %s"),
650 as_warn (_("ignoring incorrect section type for %s"),
656 if (old_sec
== NULL
&& ((attr
& ~(SHF_LINK_ORDER
659 & ~ssect
->attr
) != 0)
661 /* Strip SHF_GNU_RETAIN. */
662 bfd_vma generic_attr
= attr
;
663 if (elf_tdata (stdoutput
)->has_gnu_osabi
)
664 generic_attr
&= ~SHF_GNU_RETAIN
;
666 /* As a GNU extension, we permit a .note section to be
667 allocatable. If the linker sees an allocatable .note
668 section, it will create a PT_NOTE segment in the output
669 file. We also allow "x" for .note.GNU-stack. */
670 if (ssect
->type
== SHT_NOTE
671 && (generic_attr
== SHF_ALLOC
672 || generic_attr
== SHF_EXECINSTR
))
674 /* Allow different SHF_MERGE and SHF_STRINGS if we have
675 something like .rodata.str. */
676 else if (ssect
->suffix_length
== -2
677 && name
[ssect
->prefix_length
] == '.'
681 & ~SHF_STRINGS
) == 0)
683 /* .interp, .strtab and .symtab can have SHF_ALLOC. */
684 else if (generic_attr
== SHF_ALLOC
685 && (strcmp (name
, ".interp") == 0
686 || strcmp (name
, ".strtab") == 0
687 || strcmp (name
, ".symtab") == 0))
689 /* .note.GNU-stack can have SHF_EXECINSTR. */
690 else if (generic_attr
== SHF_EXECINSTR
691 && strcmp (name
, ".note.GNU-stack") == 0)
694 /* A section on Alpha may have SHF_ALPHA_GPREL. */
695 else if ((generic_attr
& ~ssect
->attr
) == SHF_ALPHA_GPREL
)
699 else if (generic_attr
== (SHF_EXECINSTR
| SHF_WRITE
| SHF_ALLOC
)
700 && (ssect
->type
== SHT_INIT_ARRAY
701 || ssect
->type
== SHT_FINI_ARRAY
702 || ssect
->type
== SHT_PREINIT_ARRAY
))
703 /* RX init/fini arrays can and should have the "awx" attributes set. */
708 if (match_p
->group_name
== NULL
)
709 as_warn (_("setting incorrect section attributes for %s"),
715 if (!override
&& old_sec
== NULL
)
719 /* Convert ELF type and flags to BFD flags. */
721 | ((attr
& SHF_WRITE
) ? 0 : SEC_READONLY
)
722 | ((attr
& SHF_ALLOC
) ? SEC_ALLOC
: 0)
723 | (((attr
& SHF_ALLOC
) && type
!= SHT_NOBITS
) ? SEC_LOAD
: 0)
724 | ((attr
& SHF_EXECINSTR
) ? SEC_CODE
: 0)
725 | ((attr
& SHF_MERGE
) ? SEC_MERGE
: 0)
726 | ((attr
& SHF_STRINGS
) ? SEC_STRINGS
: 0)
727 | ((attr
& SHF_EXCLUDE
) ? SEC_EXCLUDE
: 0)
728 | ((attr
& SHF_TLS
) ? SEC_THREAD_LOCAL
: 0));
729 #ifdef md_elf_section_flags
730 flags
= md_elf_section_flags (flags
, attr
, type
);
734 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
736 /* PR 28054: Set the SEC_ELF_OCTETS flag for debugging sections.
737 Based on the code in bfd/elf.c:_bfd_elf_make_section_from_shdr().
739 FIXME: We do not set the SEC_DEBUGGING flag because that causes
740 problems for the FT32 and MSP430 targets. Investigate and fix. */
741 if ((flags
& SEC_ALLOC
) == 0 && name
[0] == '.')
743 if ( startswith (name
, ".debug")
744 || startswith (name
, ".zdebug")
745 || startswith (name
, ".gnu.debuglto_.debug_")
746 || startswith (name
, ".gnu.linkonce.wi.")
747 || startswith (name
, GNU_BUILD_ATTRS_SECTION_NAME
)
748 || startswith (name
, ".note.gnu"))
749 flags
|= SEC_ELF_OCTETS
;
756 if (type
== SHT_NULL
)
757 type
= bfd_elf_get_default_section_type (flags
);
758 elf_section_type (sec
) = type
;
759 elf_section_flags (sec
) = attr
;
760 elf_section_data (sec
)->this_hdr
.sh_info
= match_p
->sh_info
;
762 /* Prevent SEC_HAS_CONTENTS from being inadvertently set. */
763 if (type
== SHT_NOBITS
)
764 seg_info (sec
)->bss
= 1;
766 /* Set the section ID and flags. */
767 sec
->section_id
= match_p
->section_id
;
768 flags
|= match_p
->flags
;
770 /* Set the linked-to symbol name. */
771 sec
->map_head
.linked_to_symbol_name
772 = match_p
->linked_to_symbol_name
;
774 bfd_set_section_flags (sec
, flags
);
775 if (flags
& SEC_MERGE
)
776 sec
->entsize
= entsize
;
777 elf_group_name (sec
) = match_p
->group_name
;
779 /* Add a symbol for this section to the symbol table. */
780 secsym
= symbol_find (name
);
783 /* We could be repurposing an undefined symbol here: make sure we
784 reset sy_value to look like other section symbols in order to avoid
785 trying to incorrectly resolve this section symbol later on. */
786 static const expressionS exp
= { .X_op
= O_constant
};
787 symbol_set_value_expression (secsym
, &exp
);
788 symbol_set_bfdsym (secsym
, sec
->symbol
);
791 symbol_table_insert (section_symbol (sec
));
796 && (unsigned) type
!= elf_section_type (old_sec
))
799 /* This is a special section with known type. User
800 assembly might get the section type wrong; Even high
801 profile projects like glibc have done so in the past.
802 So don't error in this case. */
803 as_warn (_("ignoring changed section type for %s"), name
);
805 /* Do error when assembly isn't self-consistent. */
806 as_bad (_("changed section type for %s"), name
);
811 /* If section attributes are specified the second time we see a
812 particular section, then check that they are the same as we
813 saw the first time. */
814 if (((old_sec
->flags
^ flags
)
815 & (SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_CODE
816 | SEC_EXCLUDE
| SEC_SORT_ENTRIES
| SEC_MERGE
| SEC_STRINGS
817 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
818 | SEC_THREAD_LOCAL
)))
821 as_warn (_("ignoring changed section attributes for %s"), name
);
823 as_bad (_("changed section attributes for %s"), name
);
826 /* FIXME: Maybe we should consider removing a previously set
827 processor or application specific attribute as suspicious? */
828 elf_section_flags (sec
) = attr
;
830 if ((flags
& SEC_MERGE
) && old_sec
->entsize
!= (unsigned) entsize
)
831 as_bad (_("changed section entity size for %s"), name
);
835 #ifdef md_elf_section_change_hook
836 md_elf_section_change_hook ();
841 obj_elf_parse_section_letters (char *str
, size_t len
,
842 bool *is_clone
, bfd_vma
*gnu_attr
)
854 if (len
> 1 && str
[1] == 'm')
858 if (len
> 1 && str
[1] == 's')
869 attr
|= SHF_LINK_ORDER
;
875 attr
|= SHF_EXECINSTR
;
890 *gnu_attr
|= SHF_GNU_MBIND
;
893 *gnu_attr
|= SHF_GNU_RETAIN
;
900 const char *bad_msg
= _("unrecognized .section attribute:"
901 " want a,e,o,w,x,M,S,G,T or number");
902 #ifdef md_elf_section_letter
903 bfd_vma md_attr
= md_elf_section_letter (*str
, &bad_msg
);
904 if (md_attr
!= (bfd_vma
) -1)
911 struct elf_backend_data
*bed
;
912 bfd_vma numeric_flags
= strtoul (str
, &end
, 0);
914 attr
|= numeric_flags
;
916 bed
= (struct elf_backend_data
*)
917 get_elf_backend_data (stdoutput
);
919 if (bed
->elf_osabi
== ELFOSABI_NONE
920 || bed
->elf_osabi
== ELFOSABI_STANDALONE
921 || bed
->elf_osabi
== ELFOSABI_GNU
922 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
924 /* Add flags in the SHF_MASKOS range to gnu_attr for
925 OSABIs that support those flags.
926 Also adding the flags for ELFOSABI_{NONE,STANDALONE}
927 allows them to be validated later in obj_elf_section.
928 We can't just always set these bits in gnu_attr for
929 all OSABIs, since Binutils does not recognize all
930 SHF_MASKOS bits for non-GNU OSABIs. It's therefore
931 possible that numeric flags are being used to set bits
932 in the SHF_MASKOS range for those targets, and we
933 don't want assembly to fail in those situations. */
934 *gnu_attr
|= (numeric_flags
& SHF_MASKOS
);
937 /* Update str and len, allowing for the fact that
938 we will execute str++ and len-- below. */
944 as_fatal ("%s", bad_msg
);
955 obj_elf_section_type (char *str
, size_t len
, bool warn
)
957 if (len
== 8 && startswith (str
, "progbits"))
959 if (len
== 6 && startswith (str
, "nobits"))
961 if (len
== 4 && startswith (str
, "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
);
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"));
990 as_warn (_("unrecognized section type"));
995 obj_elf_section_word (char *str
, size_t len
, int *type
)
999 if (len
== 5 && startswith (str
, "write"))
1001 if (len
== 5 && startswith (str
, "alloc"))
1003 if (len
== 9 && startswith (str
, "execinstr"))
1004 return SHF_EXECINSTR
;
1005 if (len
== 7 && startswith (str
, "exclude"))
1007 if (len
== 3 && startswith (str
, "tls"))
1010 #ifdef md_elf_section_word
1012 bfd_vma md_attr
= md_elf_section_word (str
, len
);
1018 ret
= obj_elf_section_type (str
, len
, false);
1022 as_warn (_("unrecognized section attribute"));
1027 /* Get name of section. */
1029 obj_elf_section_name (void)
1034 if (*input_line_pointer
== '"')
1038 name
= demand_copy_C_string (&dummy
);
1041 ignore_rest_of_line ();
1047 char *end
= input_line_pointer
;
1049 while (0 == strchr ("\n\t,; ", *end
))
1051 if (end
== input_line_pointer
)
1053 as_bad (_("missing name"));
1054 ignore_rest_of_line ();
1058 obstack_grow0 (¬es
, input_line_pointer
, end
- input_line_pointer
);
1059 name
= obstack_base (¬es
);
1061 while (flag_sectname_subst
)
1063 char *subst
= strchr (name
, '%');
1064 if (subst
&& subst
[1] == 'S')
1066 size_t head
= subst
- name
;
1067 size_t tail
= strlen (subst
+ 2) + 1;
1068 size_t slen
= strlen (now_seg
->name
);
1072 obstack_blank (¬es
, slen
- 2);
1073 name
= obstack_base (¬es
);
1075 memmove (name
+ head
+ slen
, name
+ head
+ 2, tail
);
1076 memcpy (name
+ head
, now_seg
->name
, slen
);
1082 obstack_finish (¬es
);
1084 #ifdef tc_canonicalize_section_name
1085 name
= tc_canonicalize_section_name (name
);
1087 input_line_pointer
= end
;
1094 obj_elf_attach_to_group (int dummy ATTRIBUTE_UNUSED
)
1096 const char * gname
= obj_elf_section_name ();
1100 as_warn (_("group name not parseable"));
1104 if (elf_group_name (now_seg
))
1106 as_warn (_("section %s already has a group (%s)"),
1107 bfd_section_name (now_seg
), elf_group_name (now_seg
));
1111 elf_group_name (now_seg
) = gname
;
1112 elf_section_flags (now_seg
) |= SHF_GROUP
;
1116 obj_elf_section (int push
)
1125 subsegT new_subsection
= -1;
1126 struct elf_section_match match
;
1127 unsigned long linked_to_section_index
= -1UL;
1133 #ifdef md_flush_pending_output
1134 md_flush_pending_output ();
1137 obj_elf_section_change_hook ();
1139 s_mri_sect (&mri_type
);
1141 #ifdef md_elf_section_change_hook
1142 md_elf_section_change_hook ();
1148 name
= obj_elf_section_name ();
1152 memset (&match
, 0, sizeof (match
));
1155 if ((sym
= symbol_find (name
)) != NULL
1156 && ! symbol_section_p (sym
)
1157 && S_IS_DEFINED (sym
)
1158 && ! S_IS_VOLATILE (sym
)
1159 && ! S_CAN_BE_REDEFINED (sym
))
1161 as_bad (_("section name '%s' already defined as another symbol"), name
);
1162 ignore_rest_of_line ();
1171 if (*input_line_pointer
== ',')
1173 /* Skip the comma. */
1174 ++input_line_pointer
;
1177 if (push
&& ISDIGIT (*input_line_pointer
))
1179 /* .pushsection has an optional subsection. */
1180 new_subsection
= (subsegT
) get_absolute_expression ();
1184 /* Stop if we don't see a comma. */
1185 if (*input_line_pointer
!= ',')
1188 /* Skip the comma. */
1189 ++input_line_pointer
;
1193 if (*input_line_pointer
== '"')
1197 beg
= demand_copy_C_string (&dummy
);
1200 ignore_rest_of_line ();
1203 attr
|= obj_elf_parse_section_letters (beg
, strlen (beg
),
1204 &is_clone
, &gnu_attr
);
1207 if (*input_line_pointer
== ',')
1210 char *save
= input_line_pointer
;
1212 ++input_line_pointer
;
1214 c
= *input_line_pointer
;
1217 beg
= demand_copy_C_string (&dummy
);
1220 ignore_rest_of_line ();
1223 type
= obj_elf_section_type (beg
, strlen (beg
), true);
1225 else if (c
== '@' || c
== '%')
1227 ++input_line_pointer
;
1229 if (ISDIGIT (* input_line_pointer
))
1230 type
= strtoul (input_line_pointer
, &input_line_pointer
, 0);
1233 c
= get_symbol_name (& beg
);
1234 (void) restore_line_pointer (c
);
1235 type
= obj_elf_section_type (beg
,
1236 input_line_pointer
- beg
,
1241 input_line_pointer
= save
;
1245 if ((attr
& SHF_MERGE
) != 0 && *input_line_pointer
== ',')
1247 ++input_line_pointer
;
1249 entsize
= get_absolute_expression ();
1253 as_warn (_("invalid merge entity size"));
1258 else if ((attr
& SHF_MERGE
) != 0)
1260 as_warn (_("entity size for SHF_MERGE not specified"));
1264 if ((attr
& SHF_LINK_ORDER
) != 0 && *input_line_pointer
== ',')
1266 ++input_line_pointer
;
1268 /* Check for a numeric section index, rather than a symbol name. */
1269 if (ISDIGIT (* input_line_pointer
))
1271 linked_to_section_index
= strtoul (input_line_pointer
, & input_line_pointer
, 0);
1276 unsigned int length
;
1278 c
= get_symbol_name (& beg
);
1279 (void) restore_line_pointer (c
);
1280 length
= input_line_pointer
- beg
;
1282 match
.linked_to_symbol_name
= xmemdup0 (beg
, length
);
1286 if ((attr
& SHF_GROUP
) != 0 && is_clone
)
1288 as_warn (_("? section flag ignored with G present"));
1292 if ((attr
& SHF_GROUP
) != 0 && *input_line_pointer
== ',')
1294 ++input_line_pointer
;
1295 match
.group_name
= obj_elf_section_name ();
1296 if (match
.group_name
== NULL
)
1298 else if (*input_line_pointer
== ',')
1300 ++input_line_pointer
;
1302 if (startswith (input_line_pointer
, "comdat"))
1304 input_line_pointer
+= 6;
1308 else if (startswith (name
, ".gnu.linkonce"))
1311 else if ((attr
& SHF_GROUP
) != 0)
1313 as_warn (_("group name for SHF_GROUP not specified"));
1319 const char *now_group
= elf_group_name (now_seg
);
1320 if (now_group
!= NULL
)
1322 match
.group_name
= now_group
;
1323 linkonce
= (now_seg
->flags
& SEC_LINK_ONCE
) != 0;
1327 if ((gnu_attr
& SHF_GNU_MBIND
) != 0 && *input_line_pointer
== ',')
1329 char *save
= input_line_pointer
;
1330 ++input_line_pointer
;
1332 if (ISDIGIT (* input_line_pointer
))
1334 char *t
= input_line_pointer
;
1335 match
.sh_info
= strtoul (input_line_pointer
,
1336 &input_line_pointer
, 0);
1337 if (match
.sh_info
== (unsigned int) -1)
1339 as_warn (_("unsupported mbind section info: %s"), t
);
1344 input_line_pointer
= save
;
1347 if ((gnu_attr
& SHF_GNU_RETAIN
) != 0)
1348 match
.sh_flags
|= SHF_GNU_RETAIN
;
1350 if (*input_line_pointer
== ',')
1352 char *save
= input_line_pointer
;
1354 ++input_line_pointer
;
1356 if (startswith (input_line_pointer
, "unique"))
1358 input_line_pointer
+= 6;
1360 if (*input_line_pointer
== ',')
1362 ++input_line_pointer
;
1364 if (ISDIGIT (* input_line_pointer
))
1368 char *t
= input_line_pointer
;
1369 if (sizeof (bfd_vma
) <= sizeof (unsigned long))
1372 id
= strtoul (input_line_pointer
,
1373 &input_line_pointer
, 0);
1374 overflow
= (id
== (unsigned long) -1
1375 && errno
== ERANGE
);
1380 (input_line_pointer
,
1381 (const char **) &input_line_pointer
, 0);
1382 overflow
= id
== ~(bfd_vma
) 0;
1384 if (overflow
|| id
> (unsigned int) -1)
1386 char *linefeed
, saved_char
= 0;
1387 if ((linefeed
= strchr (t
, '\n')) != NULL
)
1389 saved_char
= *linefeed
;
1392 as_bad (_("unsupported section id: %s"), t
);
1394 *linefeed
= saved_char
;
1398 match
.section_id
= id
;
1399 match
.flags
|= SEC_ASSEMBLER_SECTION_ID
;
1405 input_line_pointer
= save
;
1415 if (*input_line_pointer
!= '#')
1417 as_bad (_("character following name is not '#'"));
1418 ignore_rest_of_line ();
1421 ++input_line_pointer
;
1422 c
= get_symbol_name (& beg
);
1423 (void) restore_line_pointer (c
);
1425 attr
|= obj_elf_section_word (beg
, input_line_pointer
- beg
,
1430 while (*input_line_pointer
++ == ',');
1431 --input_line_pointer
;
1436 demand_empty_rest_of_line ();
1438 if ((gnu_attr
& (SHF_GNU_MBIND
| SHF_GNU_RETAIN
)) != 0)
1440 const struct elf_backend_data
*bed
;
1441 bool mbind_p
= (gnu_attr
& SHF_GNU_MBIND
) != 0;
1443 if (mbind_p
&& (attr
& SHF_ALLOC
) == 0)
1444 as_bad (_("SHF_ALLOC isn't set for GNU_MBIND section: %s"), name
);
1446 bed
= get_elf_backend_data (stdoutput
);
1448 if (bed
->elf_osabi
!= ELFOSABI_GNU
1449 && bed
->elf_osabi
!= ELFOSABI_FREEBSD
1450 && bed
->elf_osabi
!= ELFOSABI_NONE
)
1451 as_bad (_("%s section is supported only by GNU and FreeBSD targets"),
1452 mbind_p
? "GNU_MBIND" : "GNU_RETAIN");
1456 elf_tdata (stdoutput
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1457 if ((gnu_attr
& SHF_GNU_RETAIN
) != 0)
1458 elf_tdata (stdoutput
)->has_gnu_osabi
|= elf_gnu_osabi_retain
;
1464 obj_elf_change_section (name
, type
, attr
, entsize
, &match
, linkonce
,
1467 if (linked_to_section_index
!= -1UL)
1469 elf_section_flags (now_seg
) |= SHF_LINK_ORDER
;
1470 elf_section_data (now_seg
)->this_hdr
.sh_link
= linked_to_section_index
;
1471 /* FIXME: Should we perform some sanity checking on the section index ? */
1474 if (push
&& new_subsection
!= -1)
1475 subseg_set (now_seg
, new_subsection
);
1478 /* Change to the .bss section. */
1481 obj_elf_bss (int i ATTRIBUTE_UNUSED
)
1485 #ifdef md_flush_pending_output
1486 md_flush_pending_output ();
1489 obj_elf_section_change_hook ();
1491 temp
= get_absolute_expression ();
1492 subseg_set (bss_section
, (subsegT
) temp
);
1493 demand_empty_rest_of_line ();
1495 #ifdef md_elf_section_change_hook
1496 md_elf_section_change_hook ();
1500 /* Change to the .data section. */
1503 obj_elf_data (int i
)
1505 #ifdef md_flush_pending_output
1506 md_flush_pending_output ();
1509 obj_elf_section_change_hook ();
1513 #ifdef md_elf_section_change_hook
1514 md_elf_section_change_hook ();
1518 /* Change to the .text section. */
1521 obj_elf_text (int i
)
1523 #ifdef md_flush_pending_output
1524 md_flush_pending_output ();
1527 obj_elf_section_change_hook ();
1531 #ifdef md_elf_section_change_hook
1532 md_elf_section_change_hook ();
1536 /* Change to the *ABS* section. */
1539 obj_elf_struct (int i
)
1541 #ifdef md_flush_pending_output
1542 md_flush_pending_output ();
1545 obj_elf_section_change_hook ();
1549 #ifdef md_elf_section_change_hook
1550 md_elf_section_change_hook ();
1555 obj_elf_subsection (int ignore ATTRIBUTE_UNUSED
)
1559 #ifdef md_flush_pending_output
1560 md_flush_pending_output ();
1563 obj_elf_section_change_hook ();
1565 temp
= get_absolute_expression ();
1566 subseg_set (now_seg
, (subsegT
) temp
);
1567 demand_empty_rest_of_line ();
1569 #ifdef md_elf_section_change_hook
1570 md_elf_section_change_hook ();
1574 /* This can be called from the processor backends if they change
1578 obj_elf_section_change_hook (void)
1580 previous_section
= now_seg
;
1581 previous_subsection
= now_subseg
;
1585 obj_elf_previous (int ignore ATTRIBUTE_UNUSED
)
1590 if (previous_section
== 0)
1592 as_warn (_(".previous without corresponding .section; ignored"));
1596 #ifdef md_flush_pending_output
1597 md_flush_pending_output ();
1600 new_section
= previous_section
;
1601 new_subsection
= previous_subsection
;
1602 obj_elf_section_change_hook ();
1604 subseg_set (new_section
, new_subsection
);
1606 #ifdef md_elf_section_change_hook
1607 md_elf_section_change_hook ();
1612 obj_elf_popsection (int xxx ATTRIBUTE_UNUSED
)
1614 struct section_stack
*top
= section_stack
;
1618 as_warn (_(".popsection without corresponding .pushsection; ignored"));
1622 #ifdef md_flush_pending_output
1623 md_flush_pending_output ();
1626 section_stack
= top
->next
;
1627 previous_section
= top
->prev_seg
;
1628 previous_subsection
= top
->prev_subseg
;
1629 subseg_set (top
->seg
, top
->subseg
);
1632 #ifdef md_elf_section_change_hook
1633 md_elf_section_change_hook ();
1638 obj_elf_line (int ignore ATTRIBUTE_UNUSED
)
1640 /* Assume delimiter is part of expression. BSD4.2 as fails with
1641 delightful bug, so we are not being incompatible here. */
1642 new_logical_line (NULL
, get_absolute_expression ());
1643 demand_empty_rest_of_line ();
1646 static struct elf_versioned_name_list
*
1647 obj_elf_find_and_add_versioned_name (const char *version_name
,
1648 const char *sym_name
,
1650 struct elf_obj_sy
*sy_obj
)
1652 struct elf_versioned_name_list
*versioned_name
;
1655 for (p
= ver
+ 1; *p
== ELF_VER_CHR
; p
++)
1658 /* NB: Since some tests in ld/testsuite/ld-elfvers have no version
1659 names, we have to disable this. */
1660 if (0 && *p
== '\0')
1662 as_bad (_("missing version name in `%s' for symbol `%s'"),
1663 version_name
, sym_name
);
1667 versioned_name
= sy_obj
->versioned_name
;
1677 if (strcmp (versioned_name
->name
, version_name
) == 0)
1678 return versioned_name
;
1681 as_bad (_("only one version name with `@@@' is allowed "
1682 "for symbol `%s'"), sym_name
);
1686 sy_obj
->rename
= true;
1689 as_bad (_("invalid version name '%s' for symbol `%s'"),
1690 version_name
, sym_name
);
1695 versioned_name
!= NULL
;
1696 versioned_name
= versioned_name
->next
)
1697 if (strcmp (versioned_name
->name
, version_name
) == 0)
1698 return versioned_name
;
1700 /* Add this versioned name to the head of the list, */
1701 versioned_name
= (struct elf_versioned_name_list
*)
1702 xmalloc (sizeof (*versioned_name
));
1703 versioned_name
->name
= xstrdup (version_name
);
1704 versioned_name
->next
= sy_obj
->versioned_name
;
1705 sy_obj
->versioned_name
= versioned_name
;
1707 return versioned_name
;
1710 /* This handles the .symver pseudo-op, which is used to specify a
1711 symbol version. The syntax is ``.symver NAME,SYMVERNAME''.
1712 SYMVERNAME may contain ELF_VER_CHR ('@') characters. This
1713 pseudo-op causes the assembler to emit a symbol named SYMVERNAME
1714 with the same value as the symbol NAME. */
1717 obj_elf_symver (int ignore ATTRIBUTE_UNUSED
)
1720 const char *sym_name
;
1724 struct elf_obj_sy
*sy_obj
;
1727 sym
= get_sym_from_input_line_and_check ();
1729 if (*input_line_pointer
!= ',')
1731 as_bad (_("expected comma after name in .symver"));
1732 ignore_rest_of_line ();
1736 ++input_line_pointer
;
1739 /* Temporarily include '@' in symbol names. */
1740 old_lexat
= lex_type
[(unsigned char) '@'];
1741 lex_type
[(unsigned char) '@'] |= LEX_NAME
;
1742 c
= get_symbol_name (& name
);
1743 lex_type
[(unsigned char) '@'] = old_lexat
;
1744 sym_name
= S_GET_NAME (sym
);
1746 if (S_IS_COMMON (sym
))
1748 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
1750 ignore_rest_of_line ();
1754 p
= strchr (name
, ELF_VER_CHR
);
1757 as_bad (_("missing version name in `%s' for symbol `%s'"),
1759 ignore_rest_of_line ();
1763 sy_obj
= symbol_get_obj (sym
);
1764 if (obj_elf_find_and_add_versioned_name (name
, sym_name
,
1767 sy_obj
->bad_version
= true;
1768 ignore_rest_of_line ();
1772 (void) restore_line_pointer (c
);
1774 if (*input_line_pointer
== ',')
1776 char *save
= input_line_pointer
;
1778 ++input_line_pointer
;
1780 if (startswith (input_line_pointer
, "local"))
1782 input_line_pointer
+= 5;
1783 sy_obj
->visibility
= visibility_local
;
1785 else if (startswith (input_line_pointer
, "hidden"))
1787 input_line_pointer
+= 6;
1788 sy_obj
->visibility
= visibility_hidden
;
1790 else if (startswith (input_line_pointer
, "remove"))
1792 input_line_pointer
+= 6;
1793 sy_obj
->visibility
= visibility_remove
;
1796 input_line_pointer
= save
;
1799 demand_empty_rest_of_line ();
1802 /* This handles the .vtable_inherit pseudo-op, which is used to indicate
1803 to the linker the hierarchy in which a particular table resides. The
1804 syntax is ".vtable_inherit CHILDNAME, PARENTNAME". */
1807 obj_elf_get_vtable_inherit (void)
1809 char *cname
, *pname
;
1810 symbolS
*csym
, *psym
;
1813 if (*input_line_pointer
== '#')
1814 ++input_line_pointer
;
1816 c
= get_symbol_name (& cname
);
1817 csym
= symbol_find (cname
);
1819 /* GCFIXME: should check that we don't have two .vtable_inherits for
1820 the same child symbol. Also, we can currently only do this if the
1821 child symbol is already exists and is placed in a fragment. */
1823 if (csym
== NULL
|| symbol_get_frag (csym
) == NULL
)
1825 as_bad (_("expected `%s' to have already been set for .vtable_inherit"),
1830 *input_line_pointer
= c
;
1832 SKIP_WHITESPACE_AFTER_NAME ();
1833 if (*input_line_pointer
!= ',')
1835 as_bad (_("expected comma after name in .vtable_inherit"));
1836 ignore_rest_of_line ();
1840 ++input_line_pointer
;
1843 if (*input_line_pointer
== '#')
1844 ++input_line_pointer
;
1846 if (input_line_pointer
[0] == '0'
1847 && (input_line_pointer
[1] == '\0'
1848 || ISSPACE (input_line_pointer
[1])))
1850 psym
= section_symbol (absolute_section
);
1851 ++input_line_pointer
;
1855 c
= get_symbol_name (& pname
);
1856 psym
= symbol_find_or_make (pname
);
1857 restore_line_pointer (c
);
1860 demand_empty_rest_of_line ();
1865 gas_assert (symbol_get_value_expression (csym
)->X_op
== O_constant
);
1866 return fix_new (symbol_get_frag (csym
),
1867 symbol_get_value_expression (csym
)->X_add_number
,
1868 0, psym
, 0, 0, BFD_RELOC_VTABLE_INHERIT
);
1871 /* This is a version of obj_elf_get_vtable_inherit() that is
1872 suitable for use in struct _pseudo_type tables. */
1875 obj_elf_vtable_inherit (int ignore ATTRIBUTE_UNUSED
)
1877 (void) obj_elf_get_vtable_inherit ();
1880 /* This handles the .vtable_entry pseudo-op, which is used to indicate
1881 to the linker that a vtable slot was used. The syntax is
1882 ".vtable_entry tablename, offset". */
1885 obj_elf_get_vtable_entry (void)
1890 if (*input_line_pointer
== '#')
1891 ++input_line_pointer
;
1893 sym
= get_sym_from_input_line_and_check ();
1894 if (*input_line_pointer
!= ',')
1896 as_bad (_("expected comma after name in .vtable_entry"));
1897 ignore_rest_of_line ();
1901 ++input_line_pointer
;
1902 if (*input_line_pointer
== '#')
1903 ++input_line_pointer
;
1905 offset
= get_absolute_expression ();
1907 demand_empty_rest_of_line ();
1909 return fix_new (frag_now
, frag_now_fix (), 0, sym
, offset
, 0,
1910 BFD_RELOC_VTABLE_ENTRY
);
1913 /* This is a version of obj_elf_get_vtable_entry() that is
1914 suitable for use in struct _pseudo_type tables. */
1917 obj_elf_vtable_entry (int ignore ATTRIBUTE_UNUSED
)
1919 (void) obj_elf_get_vtable_entry ();
1922 #define skip_whitespace(str) do { if (*(str) == ' ') ++(str); } while (0)
1925 skip_past_char (char ** str
, char c
)
1935 #define skip_past_comma(str) skip_past_char (str, ',')
1937 /* A list of attributes that have been explicitly set by the assembly code.
1938 VENDOR is the vendor id, BASE is the tag shifted right by the number
1939 of bits in MASK, and bit N of MASK is set if tag BASE+N has been set. */
1940 struct recorded_attribute_info
{
1941 struct recorded_attribute_info
*next
;
1946 static struct recorded_attribute_info
*recorded_attributes
;
1948 /* Record that we have seen an explicit specification of attribute TAG
1949 for vendor VENDOR. */
1952 record_attribute (int vendor
, unsigned int tag
)
1956 struct recorded_attribute_info
*rai
;
1958 base
= tag
/ (8 * sizeof (rai
->mask
));
1959 mask
= 1UL << (tag
% (8 * sizeof (rai
->mask
)));
1960 for (rai
= recorded_attributes
; rai
; rai
= rai
->next
)
1961 if (rai
->vendor
== vendor
&& rai
->base
== base
)
1967 rai
= XNEW (struct recorded_attribute_info
);
1968 rai
->next
= recorded_attributes
;
1969 rai
->vendor
= vendor
;
1972 recorded_attributes
= rai
;
1975 /* Return true if we have seen an explicit specification of attribute TAG
1976 for vendor VENDOR. */
1979 obj_elf_seen_attribute (int vendor
, unsigned int tag
)
1983 struct recorded_attribute_info
*rai
;
1985 base
= tag
/ (8 * sizeof (rai
->mask
));
1986 mask
= 1UL << (tag
% (8 * sizeof (rai
->mask
)));
1987 for (rai
= recorded_attributes
; rai
; rai
= rai
->next
)
1988 if (rai
->vendor
== vendor
&& rai
->base
== base
)
1989 return (rai
->mask
& mask
) != 0;
1993 /* Parse an attribute directive for VENDOR.
1994 Returns the attribute number read, or zero on error. */
1997 obj_elf_vendor_attribute (int vendor
)
2005 /* Read the first number or name. */
2006 skip_whitespace (input_line_pointer
);
2007 s
= input_line_pointer
;
2008 if (ISDIGIT (*input_line_pointer
))
2011 if (exp
.X_op
!= O_constant
)
2013 tag
= exp
.X_add_number
;
2019 /* A name may contain '_', but no other punctuation. */
2020 for (; ISALNUM (*input_line_pointer
) || *input_line_pointer
== '_';
2021 ++input_line_pointer
)
2026 name
= xmemdup0 (s
, i
);
2028 #ifndef CONVERT_SYMBOLIC_ATTRIBUTE
2029 #define CONVERT_SYMBOLIC_ATTRIBUTE(a) -1
2032 tag
= CONVERT_SYMBOLIC_ATTRIBUTE (name
);
2035 as_bad (_("Attribute name not recognised: %s"), name
);
2036 ignore_rest_of_line ();
2043 type
= _bfd_elf_obj_attrs_arg_type (stdoutput
, vendor
, tag
);
2045 if (skip_past_comma (&input_line_pointer
) == -1)
2050 if (exp
.X_op
!= O_constant
)
2052 as_bad (_("expected numeric constant"));
2053 ignore_rest_of_line ();
2056 i
= exp
.X_add_number
;
2059 && skip_past_comma (&input_line_pointer
) == -1)
2061 as_bad (_("expected comma"));
2062 ignore_rest_of_line ();
2069 skip_whitespace (input_line_pointer
);
2070 if (*input_line_pointer
!= '"')
2072 s
= demand_copy_C_string (&len
);
2075 record_attribute (vendor
, tag
);
2079 bfd_elf_add_obj_attr_int_string (stdoutput
, vendor
, tag
, i
, s
);
2082 bfd_elf_add_obj_attr_string (stdoutput
, vendor
, tag
, s
);
2085 bfd_elf_add_obj_attr_int (stdoutput
, vendor
, tag
, i
);
2091 demand_empty_rest_of_line ();
2094 as_bad (_("bad string constant"));
2095 ignore_rest_of_line ();
2098 as_bad (_("expected <tag> , <value>"));
2099 ignore_rest_of_line ();
2103 /* Parse a .gnu_attribute directive. */
2106 obj_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED
)
2108 obj_elf_vendor_attribute (OBJ_ATTR_GNU
);
2112 elf_obj_read_begin_hook (void)
2114 #ifdef NEED_ECOFF_DEBUG
2115 if (ECOFF_DEBUGGING
)
2116 ecoff_read_begin_hook ();
2121 elf_obj_symbol_new_hook (symbolS
*symbolP
)
2123 struct elf_obj_sy
*sy_obj
;
2125 sy_obj
= symbol_get_obj (symbolP
);
2126 sy_obj
->size
= NULL
;
2127 sy_obj
->versioned_name
= NULL
;
2129 #ifdef NEED_ECOFF_DEBUG
2130 if (ECOFF_DEBUGGING
)
2131 ecoff_symbol_new_hook (symbolP
);
2135 /* Deduplicate size expressions. We might get into trouble with
2136 multiple freeing or use after free if we leave them pointing to the
2137 same expressionS. */
2140 elf_obj_symbol_clone_hook (symbolS
*newsym
, symbolS
*orgsym ATTRIBUTE_UNUSED
)
2142 struct elf_obj_sy
*newelf
= symbol_get_obj (newsym
);
2145 expressionS
*exp
= XNEW (expressionS
);
2146 *exp
= *newelf
->size
;
2152 elf_copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
2154 struct elf_obj_sy
*srcelf
= symbol_get_obj (src
);
2155 struct elf_obj_sy
*destelf
= symbol_get_obj (dest
);
2156 /* If size is unset, copy size from src. Because we don't track whether
2157 .size has been used, we can't differentiate .size dest, 0 from the case
2158 where dest's size is unset. */
2159 if (!destelf
->size
&& S_GET_SIZE (dest
) == 0)
2163 destelf
->size
= XNEW (expressionS
);
2164 *destelf
->size
= *srcelf
->size
;
2166 S_SET_SIZE (dest
, S_GET_SIZE (src
));
2168 /* Don't copy visibility. */
2169 S_SET_OTHER (dest
, (ELF_ST_VISIBILITY (S_GET_OTHER (dest
))
2170 | (S_GET_OTHER (src
) & ~ELF_ST_VISIBILITY (-1))));
2174 obj_elf_version (int ignore ATTRIBUTE_UNUSED
)
2179 asection
*seg
= now_seg
;
2180 subsegT subseg
= now_subseg
;
2181 Elf_Internal_Note i_note
;
2182 Elf_External_Note e_note
;
2183 asection
*note_secp
= NULL
;
2186 if (*input_line_pointer
== '\"')
2190 ++input_line_pointer
; /* -> 1st char of string. */
2191 name
= input_line_pointer
;
2193 while (is_a_char (c
= next_char_of_string ()))
2195 c
= *input_line_pointer
;
2196 *input_line_pointer
= '\0';
2197 *(input_line_pointer
- 1) = '\0';
2198 *input_line_pointer
= c
;
2200 /* Create the .note section. */
2201 note_secp
= subseg_new (".note", 0);
2202 bfd_set_section_flags (note_secp
, SEC_HAS_CONTENTS
| SEC_READONLY
);
2203 record_alignment (note_secp
, 2);
2205 /* Process the version string. */
2206 len
= strlen (name
) + 1;
2208 /* PR 3456: Although the name field is padded out to an 4-byte
2209 boundary, the namesz field should not be adjusted. */
2210 i_note
.namesz
= len
;
2211 i_note
.descsz
= 0; /* No description. */
2212 i_note
.type
= NT_VERSION
;
2213 p
= frag_more (sizeof (e_note
.namesz
));
2214 md_number_to_chars (p
, i_note
.namesz
, sizeof (e_note
.namesz
));
2215 p
= frag_more (sizeof (e_note
.descsz
));
2216 md_number_to_chars (p
, i_note
.descsz
, sizeof (e_note
.descsz
));
2217 p
= frag_more (sizeof (e_note
.type
));
2218 md_number_to_chars (p
, i_note
.type
, sizeof (e_note
.type
));
2219 p
= frag_more (len
);
2220 memcpy (p
, name
, len
);
2222 frag_align (2, 0, 0);
2224 subseg_set (seg
, subseg
);
2227 as_bad (_("expected quoted string"));
2229 demand_empty_rest_of_line ();
2233 obj_elf_size (int ignore ATTRIBUTE_UNUSED
)
2236 char c
= get_symbol_name (&name
);
2241 p
= input_line_pointer
;
2243 SKIP_WHITESPACE_AFTER_NAME ();
2244 if (*input_line_pointer
!= ',')
2247 as_bad (_("expected comma after name `%s' in .size directive"), name
);
2249 ignore_rest_of_line ();
2252 input_line_pointer
++;
2254 if (exp
.X_op
== O_absent
)
2256 as_bad (_("missing expression in .size directive"));
2257 exp
.X_op
= O_constant
;
2258 exp
.X_add_number
= 0;
2261 sym
= symbol_find_or_make (name
);
2263 if (exp
.X_op
== O_constant
)
2265 S_SET_SIZE (sym
, exp
.X_add_number
);
2266 xfree (symbol_get_obj (sym
)->size
);
2267 symbol_get_obj (sym
)->size
= NULL
;
2271 symbol_get_obj (sym
)->size
= XNEW (expressionS
);
2272 *symbol_get_obj (sym
)->size
= exp
;
2274 demand_empty_rest_of_line ();
2277 /* Handle the ELF .type pseudo-op. This sets the type of a symbol.
2278 There are six syntaxes:
2280 The first (used on Solaris) is
2282 The second (used on UnixWare) is
2284 The third (reportedly to be used on Irix 6.0) is
2286 The fourth (used on NetBSD/Arm and Linux/ARM) is
2288 The fifth (used on SVR4/860) is
2289 .type SYM,"function"
2290 The sixth (emitted by recent SunPRO under Solaris) is
2292 where the integer is the STT_* value.
2296 obj_elf_type_name (char *cp
)
2300 p
= input_line_pointer
;
2301 if (*input_line_pointer
>= '0'
2302 && *input_line_pointer
<= '9')
2304 while (*input_line_pointer
>= '0'
2305 && *input_line_pointer
<= '9')
2306 ++input_line_pointer
;
2307 *cp
= *input_line_pointer
;
2308 *input_line_pointer
= '\0';
2311 *cp
= get_symbol_name (&p
);
2317 obj_elf_type (int ignore ATTRIBUTE_UNUSED
)
2321 const char *type_name
;
2323 elf_symbol_type
*elfsym
;
2325 sym
= get_sym_from_input_line_and_check ();
2326 c
= *input_line_pointer
;
2327 elfsym
= (elf_symbol_type
*) symbol_get_bfdsym (sym
);
2329 if (*input_line_pointer
== ',')
2330 ++input_line_pointer
;
2333 if ( *input_line_pointer
== '#'
2334 || *input_line_pointer
== '@'
2335 || *input_line_pointer
== '"'
2336 || *input_line_pointer
== '%')
2337 ++input_line_pointer
;
2339 type_name
= obj_elf_type_name (& c
);
2342 if (strcmp (type_name
, "function") == 0
2343 || strcmp (type_name
, "2") == 0
2344 || strcmp (type_name
, "STT_FUNC") == 0)
2345 type
= BSF_FUNCTION
;
2346 else if (strcmp (type_name
, "object") == 0
2347 || strcmp (type_name
, "1") == 0
2348 || strcmp (type_name
, "STT_OBJECT") == 0)
2350 else if (strcmp (type_name
, "tls_object") == 0
2351 || strcmp (type_name
, "6") == 0
2352 || strcmp (type_name
, "STT_TLS") == 0)
2353 type
= BSF_OBJECT
| BSF_THREAD_LOCAL
;
2354 else if (strcmp (type_name
, "notype") == 0
2355 || strcmp (type_name
, "0") == 0
2356 || strcmp (type_name
, "STT_NOTYPE") == 0)
2358 else if (strcmp (type_name
, "common") == 0
2359 || strcmp (type_name
, "5") == 0
2360 || strcmp (type_name
, "STT_COMMON") == 0)
2364 if (! S_IS_COMMON (sym
))
2366 if (S_IS_VOLATILE (sym
))
2368 sym
= symbol_clone (sym
, 1);
2369 S_SET_SEGMENT (sym
, bfd_com_section_ptr
);
2370 S_SET_VALUE (sym
, 0);
2371 S_SET_EXTERNAL (sym
);
2372 symbol_set_frag (sym
, &zero_address_frag
);
2373 S_CLEAR_VOLATILE (sym
);
2375 else if (S_IS_DEFINED (sym
) || symbol_equated_p (sym
))
2376 as_bad (_("symbol '%s' is already defined"), S_GET_NAME (sym
));
2379 /* FIXME: Is it safe to just change the section ? */
2380 S_SET_SEGMENT (sym
, bfd_com_section_ptr
);
2381 S_SET_VALUE (sym
, 0);
2382 S_SET_EXTERNAL (sym
);
2386 else if (strcmp (type_name
, "gnu_indirect_function") == 0
2387 || strcmp (type_name
, "10") == 0
2388 || strcmp (type_name
, "STT_GNU_IFUNC") == 0)
2390 const struct elf_backend_data
*bed
;
2392 bed
= get_elf_backend_data (stdoutput
);
2393 if (bed
->elf_osabi
!= ELFOSABI_NONE
2394 && bed
->elf_osabi
!= ELFOSABI_GNU
2395 && bed
->elf_osabi
!= ELFOSABI_FREEBSD
)
2396 as_bad (_("symbol type \"%s\" is supported only by GNU "
2397 "and FreeBSD targets"), type_name
);
2398 /* MIPS targets do not support IFUNCS. */
2399 else if (bed
->target_id
== MIPS_ELF_DATA
)
2400 as_bad (_("symbol type \"%s\" is not supported by "
2401 "MIPS targets"), type_name
);
2402 elf_tdata (stdoutput
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
2403 type
= BSF_FUNCTION
| BSF_GNU_INDIRECT_FUNCTION
;
2405 else if (strcmp (type_name
, "gnu_unique_object") == 0)
2407 const struct elf_backend_data
*bed
;
2409 bed
= get_elf_backend_data (stdoutput
);
2410 if (bed
->elf_osabi
!= ELFOSABI_NONE
2411 && bed
->elf_osabi
!= ELFOSABI_GNU
)
2412 as_bad (_("symbol type \"%s\" is supported only by GNU targets"),
2414 elf_tdata (stdoutput
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
2415 type
= BSF_OBJECT
| BSF_GNU_UNIQUE
;
2417 #ifdef md_elf_symbol_type
2418 else if ((type
= md_elf_symbol_type (type_name
, sym
, elfsym
)) != -1)
2422 as_bad (_("unrecognized symbol type \"%s\""), type_name
);
2424 *input_line_pointer
= c
;
2426 if (*input_line_pointer
== '"')
2427 ++input_line_pointer
;
2429 #ifdef md_elf_symbol_type_change
2430 if (!md_elf_symbol_type_change (sym
, elfsym
, type
))
2433 flagword mask
= BSF_FUNCTION
| BSF_OBJECT
;
2435 if (type
!= BSF_FUNCTION
)
2436 mask
|= BSF_GNU_INDIRECT_FUNCTION
;
2437 if (type
!= BSF_OBJECT
)
2439 mask
|= BSF_GNU_UNIQUE
| BSF_THREAD_LOCAL
;
2441 if (S_IS_COMMON (sym
))
2443 as_bad (_("cannot change type of common symbol '%s'"),
2449 /* Don't warn when changing to STT_NOTYPE. */
2452 flagword
new = (elfsym
->symbol
.flags
& ~mask
) | type
;
2454 if (new != (elfsym
->symbol
.flags
| type
))
2455 as_warn (_("symbol '%s' already has its type set"), S_GET_NAME (sym
));
2456 elfsym
->symbol
.flags
= new;
2459 elfsym
->symbol
.flags
&= ~mask
;
2462 demand_empty_rest_of_line ();
2466 obj_elf_ident (int ignore ATTRIBUTE_UNUSED
)
2468 static segT comment_section
;
2469 segT old_section
= now_seg
;
2470 int old_subsection
= now_subseg
;
2472 #ifdef md_flush_pending_output
2473 md_flush_pending_output ();
2476 if (!comment_section
)
2479 comment_section
= subseg_new (".comment", 0);
2480 bfd_set_section_flags (comment_section
, (SEC_READONLY
| SEC_HAS_CONTENTS
2481 | SEC_MERGE
| SEC_STRINGS
));
2482 comment_section
->entsize
= 1;
2483 #ifdef md_elf_section_change_hook
2484 md_elf_section_change_hook ();
2490 subseg_set (comment_section
, 0);
2492 subseg_set (old_section
, old_subsection
);
2495 #ifdef INIT_STAB_SECTION
2497 /* The first entry in a .stabs section is special. */
2500 obj_elf_init_stab_section (segT seg
)
2505 unsigned int stroff
;
2507 /* Force the section to align to a longword boundary. Without this,
2508 UnixWare ar crashes. */
2509 bfd_set_section_alignment (seg
, 2);
2511 /* Make space for this first symbol. */
2515 file
= remap_debug_filename (as_where (NULL
));
2516 stabstr_name
= concat (segment_name (seg
), "str", (char *) NULL
);
2517 stroff
= get_stab_string_offset (file
, stabstr_name
, true);
2518 know (stroff
== 1 || (stroff
== 0 && file
[0] == '\0'));
2519 md_number_to_chars (p
, stroff
, 4);
2520 seg_info (seg
)->stabu
.p
= p
;
2526 /* Fill in the counts in the first entry in a .stabs section. */
2529 adjust_stab_sections (bfd
*abfd
, asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
2536 if (!startswith (sec
->name
, ".stab"))
2538 if (!strcmp ("str", sec
->name
+ strlen (sec
->name
) - 3))
2541 name
= concat (sec
->name
, "str", NULL
);
2542 strsec
= bfd_get_section_by_name (abfd
, name
);
2544 strsz
= bfd_section_size (strsec
);
2547 nsyms
= bfd_section_size (sec
) / 12 - 1;
2549 p
= seg_info (sec
)->stabu
.p
;
2550 gas_assert (p
!= 0);
2552 bfd_h_put_16 (abfd
, nsyms
, p
+ 6);
2553 bfd_h_put_32 (abfd
, strsz
, p
+ 8);
2557 #ifdef NEED_ECOFF_DEBUG
2559 /* This function is called by the ECOFF code. It is supposed to
2560 record the external symbol information so that the backend can
2561 write it out correctly. The ELF backend doesn't actually handle
2562 this at the moment, so we do it ourselves. We save the information
2565 #ifdef OBJ_MAYBE_ELF
2569 elf_ecoff_set_ext (symbolS
*sym
, struct ecoff_extr
*ext
)
2571 symbol_get_bfdsym (sym
)->udata
.p
= ext
;
2574 /* This function is called by bfd_ecoff_debug_externals. It is
2575 supposed to *EXT to the external symbol information, and return
2576 whether the symbol should be used at all. */
2579 elf_get_extr (asymbol
*sym
, EXTR
*ext
)
2581 if (sym
->udata
.p
== NULL
)
2583 *ext
= *(EXTR
*) sym
->udata
.p
;
2587 /* This function is called by bfd_ecoff_debug_externals. It has
2588 nothing to do for ELF. */
2591 elf_set_index (asymbol
*sym ATTRIBUTE_UNUSED
,
2592 bfd_size_type indx ATTRIBUTE_UNUSED
)
2596 #endif /* NEED_ECOFF_DEBUG */
2599 elf_frob_symbol (symbolS
*symp
, int *puntp
)
2601 struct elf_obj_sy
*sy_obj
;
2603 struct elf_versioned_name_list
*versioned_name
;
2605 #ifdef NEED_ECOFF_DEBUG
2606 if (ECOFF_DEBUGGING
)
2607 ecoff_frob_symbol (symp
);
2610 sy_obj
= symbol_get_obj (symp
);
2612 size
= sy_obj
->size
;
2615 if (resolve_expression (size
)
2616 && size
->X_op
== O_constant
)
2617 S_SET_SIZE (symp
, size
->X_add_number
);
2620 if (!flag_allow_nonconst_size
)
2621 as_bad (_(".size expression for %s "
2622 "does not evaluate to a constant"), S_GET_NAME (symp
));
2624 as_warn (_(".size expression for %s "
2625 "does not evaluate to a constant"), S_GET_NAME (symp
));
2627 free (sy_obj
->size
);
2628 sy_obj
->size
= NULL
;
2631 versioned_name
= sy_obj
->versioned_name
;
2634 /* This symbol was given a new name with the .symver directive.
2635 If this is an external reference, just rename the symbol to
2636 include the version string. This will make the relocs be
2637 against the correct versioned symbol. */
2639 /* We will have already reported an version error. */
2640 if (sy_obj
->bad_version
)
2642 /* elf_frob_file_before_adjust only allows one version symbol for
2644 else if (sy_obj
->rename
)
2645 S_SET_NAME (symp
, versioned_name
->name
);
2646 else if (S_IS_COMMON (symp
))
2648 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
2649 versioned_name
->name
, S_GET_NAME (symp
));
2655 elf_symbol_type
*elfsym
;
2657 /* This is a definition. Add an alias for each version.
2658 FIXME: Using an alias will permit the debugging information
2659 to refer to the right symbol. However, it's not clear
2660 whether it is the best approach. */
2662 /* FIXME: Creating a new symbol here is risky. We're
2663 in the final loop over the symbol table. We can
2664 get away with it only because the symbol goes to
2665 the end of the list, where the loop will still see
2666 it. It would probably be better to do this in
2667 obj_frob_file_before_adjust. */
2668 for (; versioned_name
!= NULL
;
2669 versioned_name
= versioned_name
->next
)
2671 symbolS
*symp2
= symbol_find_or_make (versioned_name
->name
);
2673 S_SET_SEGMENT (symp2
, S_GET_SEGMENT (symp
));
2675 /* Subtracting out the frag address here is a hack
2676 because we are in the middle of the final loop. */
2679 - (symbol_get_frag (symp
)->fr_address
2680 / OCTETS_PER_BYTE
)));
2682 symbol_set_frag (symp2
, symbol_get_frag (symp
));
2684 /* This will copy over the size information. */
2685 copy_symbol_attributes (symp2
, symp
);
2687 S_SET_OTHER (symp2
, S_GET_OTHER (symp
));
2689 if (S_IS_WEAK (symp
))
2692 if (S_IS_EXTERNAL (symp
))
2693 S_SET_EXTERNAL (symp2
);
2696 switch (sy_obj
->visibility
)
2698 case visibility_unchanged
:
2700 case visibility_hidden
:
2701 bfdsym
= symbol_get_bfdsym (symp
);
2702 elfsym
= elf_symbol_from (bfdsym
);
2703 elfsym
->internal_elf_sym
.st_other
&= ~3;
2704 elfsym
->internal_elf_sym
.st_other
|= STV_HIDDEN
;
2706 case visibility_remove
:
2707 /* Don't remove the symbol if it is used in relocation.
2708 Instead, mark it as to be removed and issue an error
2709 if the symbol has more than one versioned name. */
2710 if (symbol_used_in_reloc_p (symp
))
2712 if (sy_obj
->versioned_name
->next
!= NULL
)
2713 as_bad (_("symbol '%s' with multiple versions cannot be used in relocation"),
2715 symbol_mark_removed (symp
);
2718 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2720 case visibility_local
:
2721 S_CLEAR_EXTERNAL (symp
);
2727 /* Double check weak symbols. */
2728 if (S_IS_WEAK (symp
))
2730 if (S_IS_COMMON (symp
))
2731 as_bad (_("symbol `%s' can not be both weak and common"),
2736 /* Fix up SYMPP which has been marked to be removed by .symver. */
2739 elf_fixup_removed_symbol (symbolS
**sympp
)
2741 symbolS
*symp
= *sympp
;
2742 struct elf_obj_sy
*sy_obj
= symbol_get_obj (symp
);
2744 /* Replace the removed symbol with the versioned symbol. */
2745 symp
= symbol_find (sy_obj
->versioned_name
->name
);
2751 asection
**head
; /* Section lists. */
2752 unsigned int num_group
; /* Number of lists. */
2753 htab_t indexes
; /* Maps group name to index in head array. */
2756 static struct group_list groups
;
2758 /* Called via bfd_map_over_sections. If SEC is a member of a group,
2759 add it to a list of sections belonging to the group. INF is a
2760 pointer to a struct group_list, which is where we store the head of
2761 each list. If its link_to_symbol_name isn't NULL, set up its
2762 linked-to section. */
2765 build_additional_section_info (bfd
*abfd ATTRIBUTE_UNUSED
,
2766 asection
*sec
, void *inf
)
2768 struct group_list
*list
= (struct group_list
*) inf
;
2769 const char *group_name
= elf_group_name (sec
);
2771 unsigned int *elem_idx
;
2772 unsigned int *idx_ptr
;
2774 if (sec
->map_head
.linked_to_symbol_name
)
2776 symbolS
*linked_to_sym
;
2777 linked_to_sym
= symbol_find (sec
->map_head
.linked_to_symbol_name
);
2778 if (!linked_to_sym
|| !S_IS_DEFINED (linked_to_sym
))
2779 as_bad (_("undefined linked-to symbol `%s' on section `%s'"),
2780 sec
->map_head
.linked_to_symbol_name
,
2781 bfd_section_name (sec
));
2783 elf_linked_to_section (sec
) = S_GET_SEGMENT (linked_to_sym
);
2786 if (group_name
== NULL
)
2789 /* If this group already has a list, add the section to the head of
2791 elem_idx
= (unsigned int *) str_hash_find (list
->indexes
, group_name
);
2792 if (elem_idx
!= NULL
)
2794 elf_next_in_group (sec
) = list
->head
[*elem_idx
];
2795 list
->head
[*elem_idx
] = sec
;
2799 /* New group. Make the arrays bigger in chunks to minimize calls to
2801 i
= list
->num_group
;
2804 unsigned int newsize
= i
+ 128;
2805 list
->head
= XRESIZEVEC (asection
*, list
->head
, newsize
);
2807 list
->head
[i
] = sec
;
2808 list
->num_group
+= 1;
2810 /* Add index to hash. */
2811 idx_ptr
= XNEW (unsigned int);
2813 str_hash_insert (list
->indexes
, group_name
, idx_ptr
, 0);
2817 free_section_idx (void **slot
, void *arg ATTRIBUTE_UNUSED
)
2819 string_tuple_t
*tuple
= *((string_tuple_t
**) slot
);
2820 free ((char *)tuple
->value
);
2824 /* Create symbols for group signature. */
2827 elf_adjust_symtab (void)
2831 /* Go find section groups. */
2832 groups
.num_group
= 0;
2834 groups
.indexes
= str_htab_create ();
2835 bfd_map_over_sections (stdoutput
, build_additional_section_info
,
2838 /* Make the SHT_GROUP sections that describe each section group. We
2839 can't set up the section contents here yet, because elf section
2840 indices have yet to be calculated. elf.c:set_group_contents does
2841 the rest of the work. */
2842 for (i
= 0; i
< groups
.num_group
; i
++)
2844 const char *group_name
= elf_group_name (groups
.head
[i
]);
2845 const char *sec_name
;
2850 flags
= SEC_READONLY
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
| SEC_GROUP
;
2851 for (s
= groups
.head
[i
]; s
!= NULL
; s
= elf_next_in_group (s
))
2852 if ((s
->flags
^ flags
) & SEC_LINK_ONCE
)
2854 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2855 if (s
!= groups
.head
[i
])
2857 as_warn (_("assuming all members of group `%s' are COMDAT"),
2863 sec_name
= ".group";
2864 s
= subseg_force_new (sec_name
, 0);
2866 || !bfd_set_section_flags (s
, flags
)
2867 || !bfd_set_section_alignment (s
, 2))
2869 as_fatal (_("can't create group: %s"),
2870 bfd_errmsg (bfd_get_error ()));
2872 elf_section_type (s
) = SHT_GROUP
;
2874 /* Pass a pointer to the first section in this group. */
2875 elf_next_in_group (s
) = groups
.head
[i
];
2876 elf_sec_group (groups
.head
[i
]) = s
;
2877 /* Make sure that the signature symbol for the group has the
2878 name of the group. */
2879 sy
= symbol_find_exact (group_name
);
2880 if (!sy
|| !symbol_on_chain (sy
, symbol_rootP
, symbol_lastP
))
2882 /* Create the symbol now. */
2883 sy
= symbol_new (group_name
, now_seg
, frag_now
, 0);
2885 /* Before Solaris 11 build 154, Sun ld rejects local group
2886 signature symbols, so make them weak hidden instead. */
2887 symbol_get_bfdsym (sy
)->flags
|= BSF_WEAK
;
2888 S_SET_OTHER (sy
, STV_HIDDEN
);
2890 symbol_get_obj (sy
)->local
= 1;
2892 symbol_table_insert (sy
);
2894 elf_group_id (s
) = symbol_get_bfdsym (sy
);
2895 /* Mark the group signature symbol as used so that it will be
2896 included in the symbol table. */
2897 symbol_mark_used_in_reloc (sy
);
2902 elf_frob_file (void)
2904 bfd_map_over_sections (stdoutput
, adjust_stab_sections
, NULL
);
2906 #ifdef elf_tc_final_processing
2907 elf_tc_final_processing ();
2911 /* It removes any unneeded versioned symbols from the symbol table. */
2914 elf_frob_file_before_adjust (void)
2920 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
2922 struct elf_obj_sy
*sy_obj
= symbol_get_obj (symp
);
2923 int is_defined
= !!S_IS_DEFINED (symp
);
2925 if (sy_obj
->versioned_name
)
2927 char *p
= strchr (sy_obj
->versioned_name
->name
,
2932 /* The @@@ syntax is a special case. If the symbol is
2933 not defined, 2 `@'s will be removed from the
2934 versioned_name. Otherwise, 1 `@' will be removed. */
2935 size_t l
= strlen (&p
[3]) + 1;
2936 memmove (&p
[1 + is_defined
], &p
[3], l
);
2941 /* Verify that the name isn't using the @@ syntax--this
2942 is reserved for definitions of the default version
2944 if (!sy_obj
->rename
&& p
[1] == ELF_VER_CHR
)
2946 as_bad (_("invalid attempt to declare external "
2947 "version name as default in symbol `%s'"),
2948 sy_obj
->versioned_name
->name
);
2952 /* Only one version symbol is allowed for undefined
2954 if (sy_obj
->versioned_name
->next
)
2956 as_bad (_("multiple versions [`%s'|`%s'] for "
2958 sy_obj
->versioned_name
->name
,
2959 sy_obj
->versioned_name
->next
->name
,
2964 sy_obj
->rename
= true;
2968 /* If there was .symver or .weak, but symbol was neither
2969 defined nor used anywhere, remove it. */
2971 && (sy_obj
->versioned_name
|| S_IS_WEAK (symp
))
2972 && symbol_used_p (symp
) == 0
2973 && symbol_used_in_reloc_p (symp
) == 0)
2974 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2979 /* It is required that we let write_relocs have the opportunity to
2980 optimize away fixups before output has begun, since it is possible
2981 to eliminate all fixups for a section and thus we never should
2982 have generated the relocation section. */
2985 elf_frob_file_after_relocs (void)
2989 /* Set SHT_GROUP section size. */
2990 for (i
= 0; i
< groups
.num_group
; i
++)
2992 asection
*s
, *head
, *group
;
2995 head
= groups
.head
[i
];
2997 for (s
= head
; s
!= NULL
; s
= elf_next_in_group (s
))
2998 size
+= (s
->flags
& SEC_RELOC
) != 0 ? 8 : 4;
3000 group
= elf_sec_group (head
);
3001 subseg_set (group
, 0);
3002 bfd_set_section_size (group
, size
);
3003 group
->contents
= (unsigned char *) frag_more (size
);
3004 frag_now
->fr_fix
= frag_now_fix_octets ();
3005 frag_wane (frag_now
);
3009 htab_traverse_noresize (groups
.indexes
, free_section_idx
, NULL
);
3010 htab_delete (groups
.indexes
);
3012 #ifdef NEED_ECOFF_DEBUG
3013 if (ECOFF_DEBUGGING
)
3014 /* Generate the ECOFF debugging information. */
3016 const struct ecoff_debug_swap
*debug_swap
;
3017 struct ecoff_debug_info debug
;
3022 = get_elf_backend_data (stdoutput
)->elf_backend_ecoff_debug_swap
;
3023 know (debug_swap
!= NULL
);
3024 ecoff_build_debug (&debug
.symbolic_header
, &buf
, debug_swap
);
3026 /* Set up the pointers in debug. */
3027 #define SET(ptr, offset, type) \
3028 debug.ptr = (type) (buf + debug.symbolic_header.offset)
3030 SET (line
, cbLineOffset
, unsigned char *);
3031 SET (external_dnr
, cbDnOffset
, void *);
3032 SET (external_pdr
, cbPdOffset
, void *);
3033 SET (external_sym
, cbSymOffset
, void *);
3034 SET (external_opt
, cbOptOffset
, void *);
3035 SET (external_aux
, cbAuxOffset
, union aux_ext
*);
3036 SET (ss
, cbSsOffset
, char *);
3037 SET (external_fdr
, cbFdOffset
, void *);
3038 SET (external_rfd
, cbRfdOffset
, void *);
3039 /* ssext and external_ext are set up just below. */
3043 /* Set up the external symbols. */
3044 debug
.ssext
= debug
.ssext_end
= NULL
;
3045 debug
.external_ext
= debug
.external_ext_end
= NULL
;
3046 if (! bfd_ecoff_debug_externals (stdoutput
, &debug
, debug_swap
, true,
3047 elf_get_extr
, elf_set_index
))
3048 as_fatal (_("failed to set up debugging information: %s"),
3049 bfd_errmsg (bfd_get_error ()));
3051 sec
= bfd_get_section_by_name (stdoutput
, ".mdebug");
3052 gas_assert (sec
!= NULL
);
3054 know (!stdoutput
->output_has_begun
);
3056 /* We set the size of the section, call bfd_set_section_contents
3057 to force the ELF backend to allocate a file position, and then
3058 write out the data. FIXME: Is this really the best way to do
3060 bfd_set_section_size (sec
, bfd_ecoff_debug_size (stdoutput
, &debug
,
3063 /* Pass BUF to bfd_set_section_contents because this will
3064 eventually become a call to fwrite, and ISO C prohibits
3065 passing a NULL pointer to a stdio function even if the
3066 pointer will not be used. */
3067 if (! bfd_set_section_contents (stdoutput
, sec
, buf
, 0, 0))
3068 as_fatal (_("can't start writing .mdebug section: %s"),
3069 bfd_errmsg (bfd_get_error ()));
3071 know (stdoutput
->output_has_begun
);
3072 know (sec
->filepos
!= 0);
3074 if (! bfd_ecoff_write_debug (stdoutput
, &debug
, debug_swap
,
3076 as_fatal (_("could not write .mdebug section: %s"),
3077 bfd_errmsg (bfd_get_error ()));
3079 #endif /* NEED_ECOFF_DEBUG */
3083 elf_generate_asm_lineno (void)
3085 #ifdef NEED_ECOFF_DEBUG
3086 if (ECOFF_DEBUGGING
)
3087 ecoff_generate_asm_lineno ();
3092 elf_process_stab (segT sec ATTRIBUTE_UNUSED
,
3093 int what ATTRIBUTE_UNUSED
,
3094 const char *string ATTRIBUTE_UNUSED
,
3095 int type ATTRIBUTE_UNUSED
,
3096 int other ATTRIBUTE_UNUSED
,
3097 int desc ATTRIBUTE_UNUSED
)
3099 #ifdef NEED_ECOFF_DEBUG
3100 if (ECOFF_DEBUGGING
)
3101 ecoff_stab (sec
, what
, string
, type
, other
, desc
);
3106 elf_separate_stab_sections (void)
3108 #ifdef NEED_ECOFF_DEBUG
3109 return (!ECOFF_DEBUGGING
);
3116 elf_init_stab_section (segT seg
)
3118 #ifdef NEED_ECOFF_DEBUG
3119 if (!ECOFF_DEBUGGING
)
3121 obj_elf_init_stab_section (seg
);
3124 const struct format_ops elf_format_ops
=
3126 bfd_target_elf_flavour
,
3127 0, /* dfl_leading_underscore */
3128 1, /* emit_section_symbols */
3133 elf_frob_file_before_adjust
,
3134 0, /* obj_frob_file_before_fix */
3135 elf_frob_file_after_relocs
,
3136 elf_s_get_size
, elf_s_set_size
,
3137 elf_s_get_align
, elf_s_set_align
,
3144 elf_copy_symbol_attributes
,
3145 elf_generate_asm_lineno
,
3147 elf_separate_stab_sections
,
3148 elf_init_stab_section
,
3149 elf_sec_sym_ok_for_reloc
,
3151 #ifdef NEED_ECOFF_DEBUG
3154 0, /* ecoff_set_ext */
3156 elf_obj_read_begin_hook
,
3157 elf_obj_symbol_new_hook
,
3158 elf_obj_symbol_clone_hook
,