1 /* ELF object file format
2 Copyright (C) 1992-2022 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 name
= xmemdup0 (input_line_pointer
, end
- input_line_pointer
);
1060 while (flag_sectname_subst
)
1062 char *subst
= strchr (name
, '%');
1063 if (subst
&& subst
[1] == 'S')
1065 int oldlen
= strlen (name
);
1066 int substlen
= strlen (now_seg
->name
);
1067 int newlen
= oldlen
- 2 + substlen
;
1068 char *newname
= XNEWVEC (char, newlen
+ 1);
1069 int headlen
= subst
- name
;
1070 memcpy (newname
, name
, headlen
);
1071 strcpy (newname
+ headlen
, now_seg
->name
);
1072 strcat (newname
+ headlen
, subst
+ 2);
1080 #ifdef tc_canonicalize_section_name
1081 name
= tc_canonicalize_section_name (name
);
1083 input_line_pointer
= end
;
1090 obj_elf_attach_to_group (int dummy ATTRIBUTE_UNUSED
)
1092 const char * gname
= obj_elf_section_name ();
1096 as_warn (_("group name not parseable"));
1100 if (elf_group_name (now_seg
))
1102 as_warn (_("section %s already has a group (%s)"),
1103 bfd_section_name (now_seg
), elf_group_name (now_seg
));
1107 elf_group_name (now_seg
) = xstrdup (gname
);
1108 elf_section_flags (now_seg
) |= SHF_GROUP
;
1112 obj_elf_section (int push
)
1121 subsegT new_subsection
= -1;
1122 struct elf_section_match match
;
1123 unsigned long linked_to_section_index
= -1UL;
1129 #ifdef md_flush_pending_output
1130 md_flush_pending_output ();
1133 obj_elf_section_change_hook ();
1135 s_mri_sect (&mri_type
);
1137 #ifdef md_elf_section_change_hook
1138 md_elf_section_change_hook ();
1144 name
= obj_elf_section_name ();
1148 memset (&match
, 0, sizeof (match
));
1151 if ((sym
= symbol_find (name
)) != NULL
1152 && ! symbol_section_p (sym
)
1153 && S_IS_DEFINED (sym
)
1154 && ! S_IS_VOLATILE (sym
)
1155 && ! S_CAN_BE_REDEFINED (sym
))
1157 as_bad (_("section name '%s' already defined as another symbol"), name
);
1158 ignore_rest_of_line ();
1167 if (*input_line_pointer
== ',')
1169 /* Skip the comma. */
1170 ++input_line_pointer
;
1173 if (push
&& ISDIGIT (*input_line_pointer
))
1175 /* .pushsection has an optional subsection. */
1176 new_subsection
= (subsegT
) get_absolute_expression ();
1180 /* Stop if we don't see a comma. */
1181 if (*input_line_pointer
!= ',')
1184 /* Skip the comma. */
1185 ++input_line_pointer
;
1189 if (*input_line_pointer
== '"')
1193 beg
= demand_copy_C_string (&dummy
);
1196 ignore_rest_of_line ();
1199 attr
|= obj_elf_parse_section_letters (beg
, strlen (beg
),
1200 &is_clone
, &gnu_attr
);
1203 if (*input_line_pointer
== ',')
1206 char *save
= input_line_pointer
;
1208 ++input_line_pointer
;
1210 c
= *input_line_pointer
;
1213 beg
= demand_copy_C_string (&dummy
);
1216 ignore_rest_of_line ();
1219 type
= obj_elf_section_type (beg
, strlen (beg
), true);
1221 else if (c
== '@' || c
== '%')
1223 ++input_line_pointer
;
1225 if (ISDIGIT (* input_line_pointer
))
1226 type
= strtoul (input_line_pointer
, &input_line_pointer
, 0);
1229 c
= get_symbol_name (& beg
);
1230 (void) restore_line_pointer (c
);
1231 type
= obj_elf_section_type (beg
,
1232 input_line_pointer
- beg
,
1237 input_line_pointer
= save
;
1241 if ((attr
& SHF_MERGE
) != 0 && *input_line_pointer
== ',')
1243 ++input_line_pointer
;
1245 entsize
= get_absolute_expression ();
1249 as_warn (_("invalid merge entity size"));
1254 else if ((attr
& SHF_MERGE
) != 0)
1256 as_warn (_("entity size for SHF_MERGE not specified"));
1260 if ((attr
& SHF_LINK_ORDER
) != 0 && *input_line_pointer
== ',')
1262 ++input_line_pointer
;
1264 /* Check for a numeric section index, rather than a symbol name. */
1265 if (ISDIGIT (* input_line_pointer
))
1267 linked_to_section_index
= strtoul (input_line_pointer
, & input_line_pointer
, 0);
1272 unsigned int length
;
1274 c
= get_symbol_name (& beg
);
1275 (void) restore_line_pointer (c
);
1276 length
= input_line_pointer
- beg
;
1278 match
.linked_to_symbol_name
= xmemdup0 (beg
, length
);
1282 if ((attr
& SHF_GROUP
) != 0 && is_clone
)
1284 as_warn (_("? section flag ignored with G present"));
1288 if ((attr
& SHF_GROUP
) != 0 && *input_line_pointer
== ',')
1290 ++input_line_pointer
;
1291 match
.group_name
= obj_elf_section_name ();
1292 if (match
.group_name
== NULL
)
1294 else if (*input_line_pointer
== ',')
1296 ++input_line_pointer
;
1298 if (startswith (input_line_pointer
, "comdat"))
1300 input_line_pointer
+= 6;
1304 else if (startswith (name
, ".gnu.linkonce"))
1307 else if ((attr
& SHF_GROUP
) != 0)
1309 as_warn (_("group name for SHF_GROUP not specified"));
1315 const char *now_group
= elf_group_name (now_seg
);
1316 if (now_group
!= NULL
)
1318 match
.group_name
= xstrdup (now_group
);
1319 linkonce
= (now_seg
->flags
& SEC_LINK_ONCE
) != 0;
1323 if ((gnu_attr
& SHF_GNU_MBIND
) != 0 && *input_line_pointer
== ',')
1325 char *save
= input_line_pointer
;
1326 ++input_line_pointer
;
1328 if (ISDIGIT (* input_line_pointer
))
1330 char *t
= input_line_pointer
;
1331 match
.sh_info
= strtoul (input_line_pointer
,
1332 &input_line_pointer
, 0);
1333 if (match
.sh_info
== (unsigned int) -1)
1335 as_warn (_("unsupported mbind section info: %s"), t
);
1340 input_line_pointer
= save
;
1343 if ((gnu_attr
& SHF_GNU_RETAIN
) != 0)
1344 match
.sh_flags
|= SHF_GNU_RETAIN
;
1346 if (*input_line_pointer
== ',')
1348 char *save
= input_line_pointer
;
1350 ++input_line_pointer
;
1352 if (startswith (input_line_pointer
, "unique"))
1354 input_line_pointer
+= 6;
1356 if (*input_line_pointer
== ',')
1358 ++input_line_pointer
;
1360 if (ISDIGIT (* input_line_pointer
))
1364 char *t
= input_line_pointer
;
1365 if (sizeof (bfd_vma
) <= sizeof (unsigned long))
1368 id
= strtoul (input_line_pointer
,
1369 &input_line_pointer
, 0);
1370 overflow
= (id
== (unsigned long) -1
1371 && errno
== ERANGE
);
1376 (input_line_pointer
,
1377 (const char **) &input_line_pointer
, 0);
1378 overflow
= id
== ~(bfd_vma
) 0;
1380 if (overflow
|| id
> (unsigned int) -1)
1382 char *linefeed
, saved_char
= 0;
1383 if ((linefeed
= strchr (t
, '\n')) != NULL
)
1385 saved_char
= *linefeed
;
1388 as_bad (_("unsupported section id: %s"), t
);
1390 *linefeed
= saved_char
;
1394 match
.section_id
= id
;
1395 match
.flags
|= SEC_ASSEMBLER_SECTION_ID
;
1401 input_line_pointer
= save
;
1411 if (*input_line_pointer
!= '#')
1413 as_bad (_("character following name is not '#'"));
1414 ignore_rest_of_line ();
1417 ++input_line_pointer
;
1418 c
= get_symbol_name (& beg
);
1419 (void) restore_line_pointer (c
);
1421 attr
|= obj_elf_section_word (beg
, input_line_pointer
- beg
,
1426 while (*input_line_pointer
++ == ',');
1427 --input_line_pointer
;
1432 demand_empty_rest_of_line ();
1434 if ((gnu_attr
& (SHF_GNU_MBIND
| SHF_GNU_RETAIN
)) != 0)
1436 const struct elf_backend_data
*bed
;
1437 bool mbind_p
= (gnu_attr
& SHF_GNU_MBIND
) != 0;
1439 if (mbind_p
&& (attr
& SHF_ALLOC
) == 0)
1440 as_bad (_("SHF_ALLOC isn't set for GNU_MBIND section: %s"), name
);
1442 bed
= get_elf_backend_data (stdoutput
);
1444 if (bed
->elf_osabi
!= ELFOSABI_GNU
1445 && bed
->elf_osabi
!= ELFOSABI_FREEBSD
1446 && bed
->elf_osabi
!= ELFOSABI_NONE
)
1447 as_bad (_("%s section is supported only by GNU and FreeBSD targets"),
1448 mbind_p
? "GNU_MBIND" : "GNU_RETAIN");
1452 elf_tdata (stdoutput
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1453 if ((gnu_attr
& SHF_GNU_RETAIN
) != 0)
1454 elf_tdata (stdoutput
)->has_gnu_osabi
|= elf_gnu_osabi_retain
;
1460 obj_elf_change_section (name
, type
, attr
, entsize
, &match
, linkonce
,
1463 if (linked_to_section_index
!= -1UL)
1465 elf_section_flags (now_seg
) |= SHF_LINK_ORDER
;
1466 elf_section_data (now_seg
)->this_hdr
.sh_link
= linked_to_section_index
;
1467 /* FIXME: Should we perform some sanity checking on the section index ? */
1470 if (push
&& new_subsection
!= -1)
1471 subseg_set (now_seg
, new_subsection
);
1474 /* Change to the .bss section. */
1477 obj_elf_bss (int i ATTRIBUTE_UNUSED
)
1481 #ifdef md_flush_pending_output
1482 md_flush_pending_output ();
1485 obj_elf_section_change_hook ();
1487 temp
= get_absolute_expression ();
1488 subseg_set (bss_section
, (subsegT
) temp
);
1489 demand_empty_rest_of_line ();
1491 #ifdef md_elf_section_change_hook
1492 md_elf_section_change_hook ();
1496 /* Change to the .data section. */
1499 obj_elf_data (int i
)
1501 #ifdef md_flush_pending_output
1502 md_flush_pending_output ();
1505 obj_elf_section_change_hook ();
1509 #ifdef md_elf_section_change_hook
1510 md_elf_section_change_hook ();
1514 /* Change to the .text section. */
1517 obj_elf_text (int i
)
1519 #ifdef md_flush_pending_output
1520 md_flush_pending_output ();
1523 obj_elf_section_change_hook ();
1527 #ifdef md_elf_section_change_hook
1528 md_elf_section_change_hook ();
1532 /* Change to the *ABS* section. */
1535 obj_elf_struct (int i
)
1537 #ifdef md_flush_pending_output
1538 md_flush_pending_output ();
1541 obj_elf_section_change_hook ();
1545 #ifdef md_elf_section_change_hook
1546 md_elf_section_change_hook ();
1551 obj_elf_subsection (int ignore ATTRIBUTE_UNUSED
)
1555 #ifdef md_flush_pending_output
1556 md_flush_pending_output ();
1559 obj_elf_section_change_hook ();
1561 temp
= get_absolute_expression ();
1562 subseg_set (now_seg
, (subsegT
) temp
);
1563 demand_empty_rest_of_line ();
1565 #ifdef md_elf_section_change_hook
1566 md_elf_section_change_hook ();
1570 /* This can be called from the processor backends if they change
1574 obj_elf_section_change_hook (void)
1576 previous_section
= now_seg
;
1577 previous_subsection
= now_subseg
;
1581 obj_elf_previous (int ignore ATTRIBUTE_UNUSED
)
1586 if (previous_section
== 0)
1588 as_warn (_(".previous without corresponding .section; ignored"));
1592 #ifdef md_flush_pending_output
1593 md_flush_pending_output ();
1596 new_section
= previous_section
;
1597 new_subsection
= previous_subsection
;
1598 obj_elf_section_change_hook ();
1600 subseg_set (new_section
, new_subsection
);
1602 #ifdef md_elf_section_change_hook
1603 md_elf_section_change_hook ();
1608 obj_elf_popsection (int xxx ATTRIBUTE_UNUSED
)
1610 struct section_stack
*top
= section_stack
;
1614 as_warn (_(".popsection without corresponding .pushsection; ignored"));
1618 #ifdef md_flush_pending_output
1619 md_flush_pending_output ();
1622 section_stack
= top
->next
;
1623 previous_section
= top
->prev_seg
;
1624 previous_subsection
= top
->prev_subseg
;
1625 subseg_set (top
->seg
, top
->subseg
);
1628 #ifdef md_elf_section_change_hook
1629 md_elf_section_change_hook ();
1634 obj_elf_line (int ignore ATTRIBUTE_UNUSED
)
1636 /* Assume delimiter is part of expression. BSD4.2 as fails with
1637 delightful bug, so we are not being incompatible here. */
1638 new_logical_line (NULL
, get_absolute_expression ());
1639 demand_empty_rest_of_line ();
1642 static struct elf_versioned_name_list
*
1643 obj_elf_find_and_add_versioned_name (const char *version_name
,
1644 const char *sym_name
,
1646 struct elf_obj_sy
*sy_obj
)
1648 struct elf_versioned_name_list
*versioned_name
;
1651 for (p
= ver
+ 1; *p
== ELF_VER_CHR
; p
++)
1654 /* NB: Since some tests in ld/testsuite/ld-elfvers have no version
1655 names, we have to disable this. */
1656 if (0 && *p
== '\0')
1658 as_bad (_("missing version name in `%s' for symbol `%s'"),
1659 version_name
, sym_name
);
1663 versioned_name
= sy_obj
->versioned_name
;
1673 if (strcmp (versioned_name
->name
, version_name
) == 0)
1674 return versioned_name
;
1677 as_bad (_("only one version name with `@@@' is allowed "
1678 "for symbol `%s'"), sym_name
);
1682 sy_obj
->rename
= true;
1685 as_bad (_("invalid version name '%s' for symbol `%s'"),
1686 version_name
, sym_name
);
1691 versioned_name
!= NULL
;
1692 versioned_name
= versioned_name
->next
)
1693 if (strcmp (versioned_name
->name
, version_name
) == 0)
1694 return versioned_name
;
1696 /* Add this versioned name to the head of the list, */
1697 versioned_name
= (struct elf_versioned_name_list
*)
1698 xmalloc (sizeof (*versioned_name
));
1699 versioned_name
->name
= xstrdup (version_name
);
1700 versioned_name
->next
= sy_obj
->versioned_name
;
1701 sy_obj
->versioned_name
= versioned_name
;
1703 return versioned_name
;
1706 /* This handles the .symver pseudo-op, which is used to specify a
1707 symbol version. The syntax is ``.symver NAME,SYMVERNAME''.
1708 SYMVERNAME may contain ELF_VER_CHR ('@') characters. This
1709 pseudo-op causes the assembler to emit a symbol named SYMVERNAME
1710 with the same value as the symbol NAME. */
1713 obj_elf_symver (int ignore ATTRIBUTE_UNUSED
)
1716 const char *sym_name
;
1720 struct elf_obj_sy
*sy_obj
;
1723 sym
= get_sym_from_input_line_and_check ();
1725 if (*input_line_pointer
!= ',')
1727 as_bad (_("expected comma after name in .symver"));
1728 ignore_rest_of_line ();
1732 ++input_line_pointer
;
1735 /* Temporarily include '@' in symbol names. */
1736 old_lexat
= lex_type
[(unsigned char) '@'];
1737 lex_type
[(unsigned char) '@'] |= LEX_NAME
;
1738 c
= get_symbol_name (& name
);
1739 lex_type
[(unsigned char) '@'] = old_lexat
;
1740 sym_name
= S_GET_NAME (sym
);
1742 if (S_IS_COMMON (sym
))
1744 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
1746 ignore_rest_of_line ();
1750 p
= strchr (name
, ELF_VER_CHR
);
1753 as_bad (_("missing version name in `%s' for symbol `%s'"),
1755 ignore_rest_of_line ();
1759 sy_obj
= symbol_get_obj (sym
);
1760 if (obj_elf_find_and_add_versioned_name (name
, sym_name
,
1763 sy_obj
->bad_version
= true;
1764 ignore_rest_of_line ();
1768 (void) restore_line_pointer (c
);
1770 if (*input_line_pointer
== ',')
1772 char *save
= input_line_pointer
;
1774 ++input_line_pointer
;
1776 if (startswith (input_line_pointer
, "local"))
1778 input_line_pointer
+= 5;
1779 sy_obj
->visibility
= visibility_local
;
1781 else if (startswith (input_line_pointer
, "hidden"))
1783 input_line_pointer
+= 6;
1784 sy_obj
->visibility
= visibility_hidden
;
1786 else if (startswith (input_line_pointer
, "remove"))
1788 input_line_pointer
+= 6;
1789 sy_obj
->visibility
= visibility_remove
;
1792 input_line_pointer
= save
;
1795 demand_empty_rest_of_line ();
1798 /* This handles the .vtable_inherit pseudo-op, which is used to indicate
1799 to the linker the hierarchy in which a particular table resides. The
1800 syntax is ".vtable_inherit CHILDNAME, PARENTNAME". */
1803 obj_elf_get_vtable_inherit (void)
1805 char *cname
, *pname
;
1806 symbolS
*csym
, *psym
;
1809 if (*input_line_pointer
== '#')
1810 ++input_line_pointer
;
1812 c
= get_symbol_name (& cname
);
1813 csym
= symbol_find (cname
);
1815 /* GCFIXME: should check that we don't have two .vtable_inherits for
1816 the same child symbol. Also, we can currently only do this if the
1817 child symbol is already exists and is placed in a fragment. */
1819 if (csym
== NULL
|| symbol_get_frag (csym
) == NULL
)
1821 as_bad (_("expected `%s' to have already been set for .vtable_inherit"),
1826 *input_line_pointer
= c
;
1828 SKIP_WHITESPACE_AFTER_NAME ();
1829 if (*input_line_pointer
!= ',')
1831 as_bad (_("expected comma after name in .vtable_inherit"));
1832 ignore_rest_of_line ();
1836 ++input_line_pointer
;
1839 if (*input_line_pointer
== '#')
1840 ++input_line_pointer
;
1842 if (input_line_pointer
[0] == '0'
1843 && (input_line_pointer
[1] == '\0'
1844 || ISSPACE (input_line_pointer
[1])))
1846 psym
= section_symbol (absolute_section
);
1847 ++input_line_pointer
;
1851 c
= get_symbol_name (& pname
);
1852 psym
= symbol_find_or_make (pname
);
1853 restore_line_pointer (c
);
1856 demand_empty_rest_of_line ();
1861 gas_assert (symbol_get_value_expression (csym
)->X_op
== O_constant
);
1862 return fix_new (symbol_get_frag (csym
),
1863 symbol_get_value_expression (csym
)->X_add_number
,
1864 0, psym
, 0, 0, BFD_RELOC_VTABLE_INHERIT
);
1867 /* This is a version of obj_elf_get_vtable_inherit() that is
1868 suitable for use in struct _pseudo_type tables. */
1871 obj_elf_vtable_inherit (int ignore ATTRIBUTE_UNUSED
)
1873 (void) obj_elf_get_vtable_inherit ();
1876 /* This handles the .vtable_entry pseudo-op, which is used to indicate
1877 to the linker that a vtable slot was used. The syntax is
1878 ".vtable_entry tablename, offset". */
1881 obj_elf_get_vtable_entry (void)
1886 if (*input_line_pointer
== '#')
1887 ++input_line_pointer
;
1889 sym
= get_sym_from_input_line_and_check ();
1890 if (*input_line_pointer
!= ',')
1892 as_bad (_("expected comma after name in .vtable_entry"));
1893 ignore_rest_of_line ();
1897 ++input_line_pointer
;
1898 if (*input_line_pointer
== '#')
1899 ++input_line_pointer
;
1901 offset
= get_absolute_expression ();
1903 demand_empty_rest_of_line ();
1905 return fix_new (frag_now
, frag_now_fix (), 0, sym
, offset
, 0,
1906 BFD_RELOC_VTABLE_ENTRY
);
1909 /* This is a version of obj_elf_get_vtable_entry() that is
1910 suitable for use in struct _pseudo_type tables. */
1913 obj_elf_vtable_entry (int ignore ATTRIBUTE_UNUSED
)
1915 (void) obj_elf_get_vtable_entry ();
1918 #define skip_whitespace(str) do { if (*(str) == ' ') ++(str); } while (0)
1921 skip_past_char (char ** str
, char c
)
1931 #define skip_past_comma(str) skip_past_char (str, ',')
1933 /* A list of attributes that have been explicitly set by the assembly code.
1934 VENDOR is the vendor id, BASE is the tag shifted right by the number
1935 of bits in MASK, and bit N of MASK is set if tag BASE+N has been set. */
1936 struct recorded_attribute_info
{
1937 struct recorded_attribute_info
*next
;
1942 static struct recorded_attribute_info
*recorded_attributes
;
1944 /* Record that we have seen an explicit specification of attribute TAG
1945 for vendor VENDOR. */
1948 record_attribute (int vendor
, unsigned int tag
)
1952 struct recorded_attribute_info
*rai
;
1954 base
= tag
/ (8 * sizeof (rai
->mask
));
1955 mask
= 1UL << (tag
% (8 * sizeof (rai
->mask
)));
1956 for (rai
= recorded_attributes
; rai
; rai
= rai
->next
)
1957 if (rai
->vendor
== vendor
&& rai
->base
== base
)
1963 rai
= XNEW (struct recorded_attribute_info
);
1964 rai
->next
= recorded_attributes
;
1965 rai
->vendor
= vendor
;
1968 recorded_attributes
= rai
;
1971 /* Return true if we have seen an explicit specification of attribute TAG
1972 for vendor VENDOR. */
1975 obj_elf_seen_attribute (int vendor
, unsigned int tag
)
1979 struct recorded_attribute_info
*rai
;
1981 base
= tag
/ (8 * sizeof (rai
->mask
));
1982 mask
= 1UL << (tag
% (8 * sizeof (rai
->mask
)));
1983 for (rai
= recorded_attributes
; rai
; rai
= rai
->next
)
1984 if (rai
->vendor
== vendor
&& rai
->base
== base
)
1985 return (rai
->mask
& mask
) != 0;
1989 /* Parse an attribute directive for VENDOR.
1990 Returns the attribute number read, or zero on error. */
1993 obj_elf_vendor_attribute (int vendor
)
2001 /* Read the first number or name. */
2002 skip_whitespace (input_line_pointer
);
2003 s
= input_line_pointer
;
2004 if (ISDIGIT (*input_line_pointer
))
2007 if (exp
.X_op
!= O_constant
)
2009 tag
= exp
.X_add_number
;
2015 /* A name may contain '_', but no other punctuation. */
2016 for (; ISALNUM (*input_line_pointer
) || *input_line_pointer
== '_';
2017 ++input_line_pointer
)
2022 name
= xstrndup (s
, i
);
2024 #ifndef CONVERT_SYMBOLIC_ATTRIBUTE
2025 #define CONVERT_SYMBOLIC_ATTRIBUTE(a) -1
2028 tag
= CONVERT_SYMBOLIC_ATTRIBUTE (name
);
2031 as_bad (_("Attribute name not recognised: %s"), name
);
2032 ignore_rest_of_line ();
2039 type
= _bfd_elf_obj_attrs_arg_type (stdoutput
, vendor
, tag
);
2041 if (skip_past_comma (&input_line_pointer
) == -1)
2046 if (exp
.X_op
!= O_constant
)
2048 as_bad (_("expected numeric constant"));
2049 ignore_rest_of_line ();
2052 i
= exp
.X_add_number
;
2055 && skip_past_comma (&input_line_pointer
) == -1)
2057 as_bad (_("expected comma"));
2058 ignore_rest_of_line ();
2065 skip_whitespace (input_line_pointer
);
2066 if (*input_line_pointer
!= '"')
2068 s
= demand_copy_C_string (&len
);
2071 record_attribute (vendor
, tag
);
2075 bfd_elf_add_obj_attr_int_string (stdoutput
, vendor
, tag
, i
, s
);
2078 bfd_elf_add_obj_attr_string (stdoutput
, vendor
, tag
, s
);
2081 bfd_elf_add_obj_attr_int (stdoutput
, vendor
, tag
, i
);
2087 demand_empty_rest_of_line ();
2090 as_bad (_("bad string constant"));
2091 ignore_rest_of_line ();
2094 as_bad (_("expected <tag> , <value>"));
2095 ignore_rest_of_line ();
2099 /* Parse a .gnu_attribute directive. */
2102 obj_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED
)
2104 obj_elf_vendor_attribute (OBJ_ATTR_GNU
);
2108 elf_obj_read_begin_hook (void)
2110 #ifdef NEED_ECOFF_DEBUG
2111 if (ECOFF_DEBUGGING
)
2112 ecoff_read_begin_hook ();
2117 elf_obj_symbol_new_hook (symbolS
*symbolP
)
2119 struct elf_obj_sy
*sy_obj
;
2121 sy_obj
= symbol_get_obj (symbolP
);
2122 sy_obj
->size
= NULL
;
2123 sy_obj
->versioned_name
= NULL
;
2125 #ifdef NEED_ECOFF_DEBUG
2126 if (ECOFF_DEBUGGING
)
2127 ecoff_symbol_new_hook (symbolP
);
2131 /* Deduplicate size expressions. We might get into trouble with
2132 multiple freeing or use after free if we leave them pointing to the
2133 same expressionS. */
2136 elf_obj_symbol_clone_hook (symbolS
*newsym
, symbolS
*orgsym ATTRIBUTE_UNUSED
)
2138 struct elf_obj_sy
*newelf
= symbol_get_obj (newsym
);
2141 expressionS
*exp
= XNEW (expressionS
);
2142 *exp
= *newelf
->size
;
2148 elf_copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
2150 struct elf_obj_sy
*srcelf
= symbol_get_obj (src
);
2151 struct elf_obj_sy
*destelf
= symbol_get_obj (dest
);
2152 /* If size is unset, copy size from src. Because we don't track whether
2153 .size has been used, we can't differentiate .size dest, 0 from the case
2154 where dest's size is unset. */
2155 if (!destelf
->size
&& S_GET_SIZE (dest
) == 0)
2159 destelf
->size
= XNEW (expressionS
);
2160 *destelf
->size
= *srcelf
->size
;
2162 S_SET_SIZE (dest
, S_GET_SIZE (src
));
2164 /* Don't copy visibility. */
2165 S_SET_OTHER (dest
, (ELF_ST_VISIBILITY (S_GET_OTHER (dest
))
2166 | (S_GET_OTHER (src
) & ~ELF_ST_VISIBILITY (-1))));
2170 obj_elf_version (int ignore ATTRIBUTE_UNUSED
)
2175 asection
*seg
= now_seg
;
2176 subsegT subseg
= now_subseg
;
2177 Elf_Internal_Note i_note
;
2178 Elf_External_Note e_note
;
2179 asection
*note_secp
= NULL
;
2182 if (*input_line_pointer
== '\"')
2186 ++input_line_pointer
; /* -> 1st char of string. */
2187 name
= input_line_pointer
;
2189 while (is_a_char (c
= next_char_of_string ()))
2191 c
= *input_line_pointer
;
2192 *input_line_pointer
= '\0';
2193 *(input_line_pointer
- 1) = '\0';
2194 *input_line_pointer
= c
;
2196 /* Create the .note section. */
2197 note_secp
= subseg_new (".note", 0);
2198 bfd_set_section_flags (note_secp
, SEC_HAS_CONTENTS
| SEC_READONLY
);
2199 record_alignment (note_secp
, 2);
2201 /* Process the version string. */
2202 len
= strlen (name
) + 1;
2204 /* PR 3456: Although the name field is padded out to an 4-byte
2205 boundary, the namesz field should not be adjusted. */
2206 i_note
.namesz
= len
;
2207 i_note
.descsz
= 0; /* No description. */
2208 i_note
.type
= NT_VERSION
;
2209 p
= frag_more (sizeof (e_note
.namesz
));
2210 md_number_to_chars (p
, i_note
.namesz
, sizeof (e_note
.namesz
));
2211 p
= frag_more (sizeof (e_note
.descsz
));
2212 md_number_to_chars (p
, i_note
.descsz
, sizeof (e_note
.descsz
));
2213 p
= frag_more (sizeof (e_note
.type
));
2214 md_number_to_chars (p
, i_note
.type
, sizeof (e_note
.type
));
2215 p
= frag_more (len
);
2216 memcpy (p
, name
, len
);
2218 frag_align (2, 0, 0);
2220 subseg_set (seg
, subseg
);
2223 as_bad (_("expected quoted string"));
2225 demand_empty_rest_of_line ();
2229 obj_elf_size (int ignore ATTRIBUTE_UNUSED
)
2232 char c
= get_symbol_name (&name
);
2237 p
= input_line_pointer
;
2239 SKIP_WHITESPACE_AFTER_NAME ();
2240 if (*input_line_pointer
!= ',')
2243 as_bad (_("expected comma after name `%s' in .size directive"), name
);
2245 ignore_rest_of_line ();
2248 input_line_pointer
++;
2250 if (exp
.X_op
== O_absent
)
2252 as_bad (_("missing expression in .size directive"));
2253 exp
.X_op
= O_constant
;
2254 exp
.X_add_number
= 0;
2257 sym
= symbol_find_or_make (name
);
2259 if (exp
.X_op
== O_constant
)
2261 S_SET_SIZE (sym
, exp
.X_add_number
);
2262 xfree (symbol_get_obj (sym
)->size
);
2263 symbol_get_obj (sym
)->size
= NULL
;
2267 symbol_get_obj (sym
)->size
= XNEW (expressionS
);
2268 *symbol_get_obj (sym
)->size
= exp
;
2270 demand_empty_rest_of_line ();
2273 /* Handle the ELF .type pseudo-op. This sets the type of a symbol.
2274 There are six syntaxes:
2276 The first (used on Solaris) is
2278 The second (used on UnixWare) is
2280 The third (reportedly to be used on Irix 6.0) is
2282 The fourth (used on NetBSD/Arm and Linux/ARM) is
2284 The fifth (used on SVR4/860) is
2285 .type SYM,"function"
2286 The sixth (emitted by recent SunPRO under Solaris) is
2288 where the integer is the STT_* value.
2292 obj_elf_type_name (char *cp
)
2296 p
= input_line_pointer
;
2297 if (*input_line_pointer
>= '0'
2298 && *input_line_pointer
<= '9')
2300 while (*input_line_pointer
>= '0'
2301 && *input_line_pointer
<= '9')
2302 ++input_line_pointer
;
2303 *cp
= *input_line_pointer
;
2304 *input_line_pointer
= '\0';
2307 *cp
= get_symbol_name (&p
);
2313 obj_elf_type (int ignore ATTRIBUTE_UNUSED
)
2317 const char *type_name
;
2319 elf_symbol_type
*elfsym
;
2321 sym
= get_sym_from_input_line_and_check ();
2322 c
= *input_line_pointer
;
2323 elfsym
= (elf_symbol_type
*) symbol_get_bfdsym (sym
);
2325 if (*input_line_pointer
== ',')
2326 ++input_line_pointer
;
2329 if ( *input_line_pointer
== '#'
2330 || *input_line_pointer
== '@'
2331 || *input_line_pointer
== '"'
2332 || *input_line_pointer
== '%')
2333 ++input_line_pointer
;
2335 type_name
= obj_elf_type_name (& c
);
2338 if (strcmp (type_name
, "function") == 0
2339 || strcmp (type_name
, "2") == 0
2340 || strcmp (type_name
, "STT_FUNC") == 0)
2341 type
= BSF_FUNCTION
;
2342 else if (strcmp (type_name
, "object") == 0
2343 || strcmp (type_name
, "1") == 0
2344 || strcmp (type_name
, "STT_OBJECT") == 0)
2346 else if (strcmp (type_name
, "tls_object") == 0
2347 || strcmp (type_name
, "6") == 0
2348 || strcmp (type_name
, "STT_TLS") == 0)
2349 type
= BSF_OBJECT
| BSF_THREAD_LOCAL
;
2350 else if (strcmp (type_name
, "notype") == 0
2351 || strcmp (type_name
, "0") == 0
2352 || strcmp (type_name
, "STT_NOTYPE") == 0)
2354 else if (strcmp (type_name
, "common") == 0
2355 || strcmp (type_name
, "5") == 0
2356 || strcmp (type_name
, "STT_COMMON") == 0)
2360 if (! S_IS_COMMON (sym
))
2362 if (S_IS_VOLATILE (sym
))
2364 sym
= symbol_clone (sym
, 1);
2365 S_SET_SEGMENT (sym
, bfd_com_section_ptr
);
2366 S_SET_VALUE (sym
, 0);
2367 S_SET_EXTERNAL (sym
);
2368 symbol_set_frag (sym
, &zero_address_frag
);
2369 S_CLEAR_VOLATILE (sym
);
2371 else if (S_IS_DEFINED (sym
) || symbol_equated_p (sym
))
2372 as_bad (_("symbol '%s' is already defined"), S_GET_NAME (sym
));
2375 /* FIXME: Is it safe to just change the section ? */
2376 S_SET_SEGMENT (sym
, bfd_com_section_ptr
);
2377 S_SET_VALUE (sym
, 0);
2378 S_SET_EXTERNAL (sym
);
2382 else if (strcmp (type_name
, "gnu_indirect_function") == 0
2383 || strcmp (type_name
, "10") == 0
2384 || strcmp (type_name
, "STT_GNU_IFUNC") == 0)
2386 const struct elf_backend_data
*bed
;
2388 bed
= get_elf_backend_data (stdoutput
);
2389 if (bed
->elf_osabi
!= ELFOSABI_NONE
2390 && bed
->elf_osabi
!= ELFOSABI_GNU
2391 && bed
->elf_osabi
!= ELFOSABI_FREEBSD
)
2392 as_bad (_("symbol type \"%s\" is supported only by GNU "
2393 "and FreeBSD targets"), type_name
);
2394 /* MIPS targets do not support IFUNCS. */
2395 else if (bed
->target_id
== MIPS_ELF_DATA
)
2396 as_bad (_("symbol type \"%s\" is not supported by "
2397 "MIPS targets"), type_name
);
2398 elf_tdata (stdoutput
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
2399 type
= BSF_FUNCTION
| BSF_GNU_INDIRECT_FUNCTION
;
2401 else if (strcmp (type_name
, "gnu_unique_object") == 0)
2403 const struct elf_backend_data
*bed
;
2405 bed
= get_elf_backend_data (stdoutput
);
2406 if (bed
->elf_osabi
!= ELFOSABI_NONE
2407 && bed
->elf_osabi
!= ELFOSABI_GNU
)
2408 as_bad (_("symbol type \"%s\" is supported only by GNU targets"),
2410 elf_tdata (stdoutput
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
2411 type
= BSF_OBJECT
| BSF_GNU_UNIQUE
;
2413 #ifdef md_elf_symbol_type
2414 else if ((type
= md_elf_symbol_type (type_name
, sym
, elfsym
)) != -1)
2418 as_bad (_("unrecognized symbol type \"%s\""), type_name
);
2420 *input_line_pointer
= c
;
2422 if (*input_line_pointer
== '"')
2423 ++input_line_pointer
;
2425 #ifdef md_elf_symbol_type_change
2426 if (!md_elf_symbol_type_change (sym
, elfsym
, type
))
2429 flagword mask
= BSF_FUNCTION
| BSF_OBJECT
;
2431 if (type
!= BSF_FUNCTION
)
2432 mask
|= BSF_GNU_INDIRECT_FUNCTION
;
2433 if (type
!= BSF_OBJECT
)
2435 mask
|= BSF_GNU_UNIQUE
| BSF_THREAD_LOCAL
;
2437 if (S_IS_COMMON (sym
))
2439 as_bad (_("cannot change type of common symbol '%s'"),
2445 /* Don't warn when changing to STT_NOTYPE. */
2448 flagword
new = (elfsym
->symbol
.flags
& ~mask
) | type
;
2450 if (new != (elfsym
->symbol
.flags
| type
))
2451 as_warn (_("symbol '%s' already has its type set"), S_GET_NAME (sym
));
2452 elfsym
->symbol
.flags
= new;
2455 elfsym
->symbol
.flags
&= ~mask
;
2458 demand_empty_rest_of_line ();
2462 obj_elf_ident (int ignore ATTRIBUTE_UNUSED
)
2464 static segT comment_section
;
2465 segT old_section
= now_seg
;
2466 int old_subsection
= now_subseg
;
2468 #ifdef md_flush_pending_output
2469 md_flush_pending_output ();
2472 if (!comment_section
)
2475 comment_section
= subseg_new (".comment", 0);
2476 bfd_set_section_flags (comment_section
, (SEC_READONLY
| SEC_HAS_CONTENTS
2477 | SEC_MERGE
| SEC_STRINGS
));
2478 comment_section
->entsize
= 1;
2479 #ifdef md_elf_section_change_hook
2480 md_elf_section_change_hook ();
2486 subseg_set (comment_section
, 0);
2488 subseg_set (old_section
, old_subsection
);
2491 #ifdef INIT_STAB_SECTION
2493 /* The first entry in a .stabs section is special. */
2496 obj_elf_init_stab_section (segT seg
)
2501 unsigned int stroff
;
2503 /* Force the section to align to a longword boundary. Without this,
2504 UnixWare ar crashes. */
2505 bfd_set_section_alignment (seg
, 2);
2507 /* Make space for this first symbol. */
2511 file
= remap_debug_filename (as_where (NULL
));
2512 stabstr_name
= concat (segment_name (seg
), "str", (char *) NULL
);
2513 stroff
= get_stab_string_offset (file
, stabstr_name
, true);
2514 know (stroff
== 1 || (stroff
== 0 && file
[0] == '\0'));
2515 md_number_to_chars (p
, stroff
, 4);
2516 seg_info (seg
)->stabu
.p
= p
;
2517 xfree ((char *) file
);
2522 /* Fill in the counts in the first entry in a .stabs section. */
2525 adjust_stab_sections (bfd
*abfd
, asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
2532 if (!startswith (sec
->name
, ".stab"))
2534 if (!strcmp ("str", sec
->name
+ strlen (sec
->name
) - 3))
2537 name
= concat (sec
->name
, "str", NULL
);
2538 strsec
= bfd_get_section_by_name (abfd
, name
);
2540 strsz
= bfd_section_size (strsec
);
2543 nsyms
= bfd_section_size (sec
) / 12 - 1;
2545 p
= seg_info (sec
)->stabu
.p
;
2546 gas_assert (p
!= 0);
2548 bfd_h_put_16 (abfd
, nsyms
, p
+ 6);
2549 bfd_h_put_32 (abfd
, strsz
, p
+ 8);
2553 #ifdef NEED_ECOFF_DEBUG
2555 /* This function is called by the ECOFF code. It is supposed to
2556 record the external symbol information so that the backend can
2557 write it out correctly. The ELF backend doesn't actually handle
2558 this at the moment, so we do it ourselves. We save the information
2561 #ifdef OBJ_MAYBE_ELF
2565 elf_ecoff_set_ext (symbolS
*sym
, struct ecoff_extr
*ext
)
2567 symbol_get_bfdsym (sym
)->udata
.p
= ext
;
2570 /* This function is called by bfd_ecoff_debug_externals. It is
2571 supposed to *EXT to the external symbol information, and return
2572 whether the symbol should be used at all. */
2575 elf_get_extr (asymbol
*sym
, EXTR
*ext
)
2577 if (sym
->udata
.p
== NULL
)
2579 *ext
= *(EXTR
*) sym
->udata
.p
;
2583 /* This function is called by bfd_ecoff_debug_externals. It has
2584 nothing to do for ELF. */
2587 elf_set_index (asymbol
*sym ATTRIBUTE_UNUSED
,
2588 bfd_size_type indx ATTRIBUTE_UNUSED
)
2592 #endif /* NEED_ECOFF_DEBUG */
2595 elf_frob_symbol (symbolS
*symp
, int *puntp
)
2597 struct elf_obj_sy
*sy_obj
;
2599 struct elf_versioned_name_list
*versioned_name
;
2601 #ifdef NEED_ECOFF_DEBUG
2602 if (ECOFF_DEBUGGING
)
2603 ecoff_frob_symbol (symp
);
2606 sy_obj
= symbol_get_obj (symp
);
2608 size
= sy_obj
->size
;
2611 if (resolve_expression (size
)
2612 && size
->X_op
== O_constant
)
2613 S_SET_SIZE (symp
, size
->X_add_number
);
2616 if (!flag_allow_nonconst_size
)
2617 as_bad (_(".size expression for %s "
2618 "does not evaluate to a constant"), S_GET_NAME (symp
));
2620 as_warn (_(".size expression for %s "
2621 "does not evaluate to a constant"), S_GET_NAME (symp
));
2623 free (sy_obj
->size
);
2624 sy_obj
->size
= NULL
;
2627 versioned_name
= sy_obj
->versioned_name
;
2630 /* This symbol was given a new name with the .symver directive.
2631 If this is an external reference, just rename the symbol to
2632 include the version string. This will make the relocs be
2633 against the correct versioned symbol. */
2635 /* We will have already reported an version error. */
2636 if (sy_obj
->bad_version
)
2638 /* elf_frob_file_before_adjust only allows one version symbol for
2640 else if (sy_obj
->rename
)
2641 S_SET_NAME (symp
, versioned_name
->name
);
2642 else if (S_IS_COMMON (symp
))
2644 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
2645 versioned_name
->name
, S_GET_NAME (symp
));
2651 elf_symbol_type
*elfsym
;
2653 /* This is a definition. Add an alias for each version.
2654 FIXME: Using an alias will permit the debugging information
2655 to refer to the right symbol. However, it's not clear
2656 whether it is the best approach. */
2658 /* FIXME: Creating a new symbol here is risky. We're
2659 in the final loop over the symbol table. We can
2660 get away with it only because the symbol goes to
2661 the end of the list, where the loop will still see
2662 it. It would probably be better to do this in
2663 obj_frob_file_before_adjust. */
2664 for (; versioned_name
!= NULL
;
2665 versioned_name
= versioned_name
->next
)
2667 symbolS
*symp2
= symbol_find_or_make (versioned_name
->name
);
2669 S_SET_SEGMENT (symp2
, S_GET_SEGMENT (symp
));
2671 /* Subtracting out the frag address here is a hack
2672 because we are in the middle of the final loop. */
2675 - (symbol_get_frag (symp
)->fr_address
2676 / OCTETS_PER_BYTE
)));
2678 symbol_set_frag (symp2
, symbol_get_frag (symp
));
2680 /* This will copy over the size information. */
2681 copy_symbol_attributes (symp2
, symp
);
2683 S_SET_OTHER (symp2
, S_GET_OTHER (symp
));
2685 if (S_IS_WEAK (symp
))
2688 if (S_IS_EXTERNAL (symp
))
2689 S_SET_EXTERNAL (symp2
);
2692 switch (sy_obj
->visibility
)
2694 case visibility_unchanged
:
2696 case visibility_hidden
:
2697 bfdsym
= symbol_get_bfdsym (symp
);
2698 elfsym
= elf_symbol_from (bfdsym
);
2699 elfsym
->internal_elf_sym
.st_other
&= ~3;
2700 elfsym
->internal_elf_sym
.st_other
|= STV_HIDDEN
;
2702 case visibility_remove
:
2703 /* Don't remove the symbol if it is used in relocation.
2704 Instead, mark it as to be removed and issue an error
2705 if the symbol has more than one versioned name. */
2706 if (symbol_used_in_reloc_p (symp
))
2708 if (sy_obj
->versioned_name
->next
!= NULL
)
2709 as_bad (_("symbol '%s' with multiple versions cannot be used in relocation"),
2711 symbol_mark_removed (symp
);
2714 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2716 case visibility_local
:
2717 S_CLEAR_EXTERNAL (symp
);
2723 /* Double check weak symbols. */
2724 if (S_IS_WEAK (symp
))
2726 if (S_IS_COMMON (symp
))
2727 as_bad (_("symbol `%s' can not be both weak and common"),
2732 /* Fix up SYMPP which has been marked to be removed by .symver. */
2735 elf_fixup_removed_symbol (symbolS
**sympp
)
2737 symbolS
*symp
= *sympp
;
2738 struct elf_obj_sy
*sy_obj
= symbol_get_obj (symp
);
2740 /* Replace the removed symbol with the versioned symbol. */
2741 symp
= symbol_find (sy_obj
->versioned_name
->name
);
2747 asection
**head
; /* Section lists. */
2748 unsigned int num_group
; /* Number of lists. */
2749 htab_t indexes
; /* Maps group name to index in head array. */
2752 static struct group_list groups
;
2754 /* Called via bfd_map_over_sections. If SEC is a member of a group,
2755 add it to a list of sections belonging to the group. INF is a
2756 pointer to a struct group_list, which is where we store the head of
2757 each list. If its link_to_symbol_name isn't NULL, set up its
2758 linked-to section. */
2761 build_additional_section_info (bfd
*abfd ATTRIBUTE_UNUSED
,
2762 asection
*sec
, void *inf
)
2764 struct group_list
*list
= (struct group_list
*) inf
;
2765 const char *group_name
= elf_group_name (sec
);
2767 unsigned int *elem_idx
;
2768 unsigned int *idx_ptr
;
2770 if (sec
->map_head
.linked_to_symbol_name
)
2772 symbolS
*linked_to_sym
;
2773 linked_to_sym
= symbol_find (sec
->map_head
.linked_to_symbol_name
);
2774 if (!linked_to_sym
|| !S_IS_DEFINED (linked_to_sym
))
2775 as_bad (_("undefined linked-to symbol `%s' on section `%s'"),
2776 sec
->map_head
.linked_to_symbol_name
,
2777 bfd_section_name (sec
));
2779 elf_linked_to_section (sec
) = S_GET_SEGMENT (linked_to_sym
);
2782 if (group_name
== NULL
)
2785 /* If this group already has a list, add the section to the head of
2787 elem_idx
= (unsigned int *) str_hash_find (list
->indexes
, group_name
);
2788 if (elem_idx
!= NULL
)
2790 elf_next_in_group (sec
) = list
->head
[*elem_idx
];
2791 list
->head
[*elem_idx
] = sec
;
2795 /* New group. Make the arrays bigger in chunks to minimize calls to
2797 i
= list
->num_group
;
2800 unsigned int newsize
= i
+ 128;
2801 list
->head
= XRESIZEVEC (asection
*, list
->head
, newsize
);
2803 list
->head
[i
] = sec
;
2804 list
->num_group
+= 1;
2806 /* Add index to hash. */
2807 idx_ptr
= XNEW (unsigned int);
2809 str_hash_insert (list
->indexes
, group_name
, idx_ptr
, 0);
2813 free_section_idx (void **slot
, void *arg ATTRIBUTE_UNUSED
)
2815 string_tuple_t
*tuple
= *((string_tuple_t
**) slot
);
2816 free ((char *)tuple
->value
);
2820 /* Create symbols for group signature. */
2823 elf_adjust_symtab (void)
2827 /* Go find section groups. */
2828 groups
.num_group
= 0;
2830 groups
.indexes
= str_htab_create ();
2831 bfd_map_over_sections (stdoutput
, build_additional_section_info
,
2834 /* Make the SHT_GROUP sections that describe each section group. We
2835 can't set up the section contents here yet, because elf section
2836 indices have yet to be calculated. elf.c:set_group_contents does
2837 the rest of the work. */
2838 for (i
= 0; i
< groups
.num_group
; i
++)
2840 const char *group_name
= elf_group_name (groups
.head
[i
]);
2841 const char *sec_name
;
2846 flags
= SEC_READONLY
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
| SEC_GROUP
;
2847 for (s
= groups
.head
[i
]; s
!= NULL
; s
= elf_next_in_group (s
))
2848 if ((s
->flags
^ flags
) & SEC_LINK_ONCE
)
2850 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2851 if (s
!= groups
.head
[i
])
2853 as_warn (_("assuming all members of group `%s' are COMDAT"),
2859 sec_name
= ".group";
2860 s
= subseg_force_new (sec_name
, 0);
2862 || !bfd_set_section_flags (s
, flags
)
2863 || !bfd_set_section_alignment (s
, 2))
2865 as_fatal (_("can't create group: %s"),
2866 bfd_errmsg (bfd_get_error ()));
2868 elf_section_type (s
) = SHT_GROUP
;
2870 /* Pass a pointer to the first section in this group. */
2871 elf_next_in_group (s
) = groups
.head
[i
];
2872 elf_sec_group (groups
.head
[i
]) = s
;
2873 /* Make sure that the signature symbol for the group has the
2874 name of the group. */
2875 sy
= symbol_find_exact (group_name
);
2876 if (!sy
|| !symbol_on_chain (sy
, symbol_rootP
, symbol_lastP
))
2878 /* Create the symbol now. */
2879 sy
= symbol_new (group_name
, now_seg
, frag_now
, 0);
2881 /* Before Solaris 11 build 154, Sun ld rejects local group
2882 signature symbols, so make them weak hidden instead. */
2883 symbol_get_bfdsym (sy
)->flags
|= BSF_WEAK
;
2884 S_SET_OTHER (sy
, STV_HIDDEN
);
2886 symbol_get_obj (sy
)->local
= 1;
2888 symbol_table_insert (sy
);
2890 elf_group_id (s
) = symbol_get_bfdsym (sy
);
2891 /* Mark the group signature symbol as used so that it will be
2892 included in the symbol table. */
2893 symbol_mark_used_in_reloc (sy
);
2898 elf_frob_file (void)
2900 bfd_map_over_sections (stdoutput
, adjust_stab_sections
, NULL
);
2902 #ifdef elf_tc_final_processing
2903 elf_tc_final_processing ();
2907 /* It removes any unneeded versioned symbols from the symbol table. */
2910 elf_frob_file_before_adjust (void)
2916 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
2918 struct elf_obj_sy
*sy_obj
= symbol_get_obj (symp
);
2919 int is_defined
= !!S_IS_DEFINED (symp
);
2921 if (sy_obj
->versioned_name
)
2923 char *p
= strchr (sy_obj
->versioned_name
->name
,
2928 /* The @@@ syntax is a special case. If the symbol is
2929 not defined, 2 `@'s will be removed from the
2930 versioned_name. Otherwise, 1 `@' will be removed. */
2931 size_t l
= strlen (&p
[3]) + 1;
2932 memmove (&p
[1 + is_defined
], &p
[3], l
);
2937 /* Verify that the name isn't using the @@ syntax--this
2938 is reserved for definitions of the default version
2940 if (!sy_obj
->rename
&& p
[1] == ELF_VER_CHR
)
2942 as_bad (_("invalid attempt to declare external "
2943 "version name as default in symbol `%s'"),
2944 sy_obj
->versioned_name
->name
);
2948 /* Only one version symbol is allowed for undefined
2950 if (sy_obj
->versioned_name
->next
)
2952 as_bad (_("multiple versions [`%s'|`%s'] for "
2954 sy_obj
->versioned_name
->name
,
2955 sy_obj
->versioned_name
->next
->name
,
2960 sy_obj
->rename
= true;
2964 /* If there was .symver or .weak, but symbol was neither
2965 defined nor used anywhere, remove it. */
2967 && (sy_obj
->versioned_name
|| S_IS_WEAK (symp
))
2968 && symbol_used_p (symp
) == 0
2969 && symbol_used_in_reloc_p (symp
) == 0)
2970 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2975 /* It is required that we let write_relocs have the opportunity to
2976 optimize away fixups before output has begun, since it is possible
2977 to eliminate all fixups for a section and thus we never should
2978 have generated the relocation section. */
2981 elf_frob_file_after_relocs (void)
2985 /* Set SHT_GROUP section size. */
2986 for (i
= 0; i
< groups
.num_group
; i
++)
2988 asection
*s
, *head
, *group
;
2991 head
= groups
.head
[i
];
2993 for (s
= head
; s
!= NULL
; s
= elf_next_in_group (s
))
2994 size
+= (s
->flags
& SEC_RELOC
) != 0 ? 8 : 4;
2996 group
= elf_sec_group (head
);
2997 subseg_set (group
, 0);
2998 bfd_set_section_size (group
, size
);
2999 group
->contents
= (unsigned char *) frag_more (size
);
3000 frag_now
->fr_fix
= frag_now_fix_octets ();
3001 frag_wane (frag_now
);
3005 htab_traverse (groups
.indexes
, free_section_idx
, NULL
);
3006 htab_delete (groups
.indexes
);
3008 #ifdef NEED_ECOFF_DEBUG
3009 if (ECOFF_DEBUGGING
)
3010 /* Generate the ECOFF debugging information. */
3012 const struct ecoff_debug_swap
*debug_swap
;
3013 struct ecoff_debug_info debug
;
3018 = get_elf_backend_data (stdoutput
)->elf_backend_ecoff_debug_swap
;
3019 know (debug_swap
!= NULL
);
3020 ecoff_build_debug (&debug
.symbolic_header
, &buf
, debug_swap
);
3022 /* Set up the pointers in debug. */
3023 #define SET(ptr, offset, type) \
3024 debug.ptr = (type) (buf + debug.symbolic_header.offset)
3026 SET (line
, cbLineOffset
, unsigned char *);
3027 SET (external_dnr
, cbDnOffset
, void *);
3028 SET (external_pdr
, cbPdOffset
, void *);
3029 SET (external_sym
, cbSymOffset
, void *);
3030 SET (external_opt
, cbOptOffset
, void *);
3031 SET (external_aux
, cbAuxOffset
, union aux_ext
*);
3032 SET (ss
, cbSsOffset
, char *);
3033 SET (external_fdr
, cbFdOffset
, void *);
3034 SET (external_rfd
, cbRfdOffset
, void *);
3035 /* ssext and external_ext are set up just below. */
3039 /* Set up the external symbols. */
3040 debug
.ssext
= debug
.ssext_end
= NULL
;
3041 debug
.external_ext
= debug
.external_ext_end
= NULL
;
3042 if (! bfd_ecoff_debug_externals (stdoutput
, &debug
, debug_swap
, true,
3043 elf_get_extr
, elf_set_index
))
3044 as_fatal (_("failed to set up debugging information: %s"),
3045 bfd_errmsg (bfd_get_error ()));
3047 sec
= bfd_get_section_by_name (stdoutput
, ".mdebug");
3048 gas_assert (sec
!= NULL
);
3050 know (!stdoutput
->output_has_begun
);
3052 /* We set the size of the section, call bfd_set_section_contents
3053 to force the ELF backend to allocate a file position, and then
3054 write out the data. FIXME: Is this really the best way to do
3056 bfd_set_section_size (sec
, bfd_ecoff_debug_size (stdoutput
, &debug
,
3059 /* Pass BUF to bfd_set_section_contents because this will
3060 eventually become a call to fwrite, and ISO C prohibits
3061 passing a NULL pointer to a stdio function even if the
3062 pointer will not be used. */
3063 if (! bfd_set_section_contents (stdoutput
, sec
, buf
, 0, 0))
3064 as_fatal (_("can't start writing .mdebug section: %s"),
3065 bfd_errmsg (bfd_get_error ()));
3067 know (stdoutput
->output_has_begun
);
3068 know (sec
->filepos
!= 0);
3070 if (! bfd_ecoff_write_debug (stdoutput
, &debug
, debug_swap
,
3072 as_fatal (_("could not write .mdebug section: %s"),
3073 bfd_errmsg (bfd_get_error ()));
3075 #endif /* NEED_ECOFF_DEBUG */
3079 elf_generate_asm_lineno (void)
3081 #ifdef NEED_ECOFF_DEBUG
3082 if (ECOFF_DEBUGGING
)
3083 ecoff_generate_asm_lineno ();
3088 elf_process_stab (segT sec ATTRIBUTE_UNUSED
,
3089 int what ATTRIBUTE_UNUSED
,
3090 const char *string ATTRIBUTE_UNUSED
,
3091 int type ATTRIBUTE_UNUSED
,
3092 int other ATTRIBUTE_UNUSED
,
3093 int desc ATTRIBUTE_UNUSED
)
3095 #ifdef NEED_ECOFF_DEBUG
3096 if (ECOFF_DEBUGGING
)
3097 ecoff_stab (sec
, what
, string
, type
, other
, desc
);
3102 elf_separate_stab_sections (void)
3104 #ifdef NEED_ECOFF_DEBUG
3105 return (!ECOFF_DEBUGGING
);
3112 elf_init_stab_section (segT seg
)
3114 #ifdef NEED_ECOFF_DEBUG
3115 if (!ECOFF_DEBUGGING
)
3117 obj_elf_init_stab_section (seg
);
3120 const struct format_ops elf_format_ops
=
3122 bfd_target_elf_flavour
,
3123 0, /* dfl_leading_underscore */
3124 1, /* emit_section_symbols */
3129 elf_frob_file_before_adjust
,
3130 0, /* obj_frob_file_before_fix */
3131 elf_frob_file_after_relocs
,
3132 elf_s_get_size
, elf_s_set_size
,
3133 elf_s_get_align
, elf_s_set_align
,
3140 elf_copy_symbol_attributes
,
3141 elf_generate_asm_lineno
,
3143 elf_separate_stab_sections
,
3144 elf_init_stab_section
,
3145 elf_sec_sym_ok_for_reloc
,
3147 #ifdef NEED_ECOFF_DEBUG
3150 0, /* ecoff_set_ext */
3152 elf_obj_read_begin_hook
,
3153 elf_obj_symbol_new_hook
,
3154 elf_obj_symbol_clone_hook
,