1 /* ELF object file format
2 Copyright (C) 1992-2024 Free Software Foundation, Inc.
4 This file is part of GAS, the GNU Assembler.
6 GAS is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 3,
9 or (at your option) any later version.
11 GAS is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GAS; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
21 #define OBJ_HEADER "obj-elf.h"
23 #include "safe-ctype.h"
26 #include "dwarf2dbg.h"
29 #ifndef ECOFF_DEBUGGING
30 #define ECOFF_DEBUGGING 0
32 #define NEED_ECOFF_DEBUG
35 #ifdef NEED_ECOFF_DEBUG
37 #include "bfd/ecoff-bfd.h"
41 #include "elf/alpha.h"
53 #include "elf/x86-64.h"
61 #include "elf/nios2.h"
68 static void obj_elf_line (int);
69 static void obj_elf_size (int);
70 static void obj_elf_type (int);
71 static void obj_elf_ident (int);
72 static void obj_elf_weak (int);
73 static void obj_elf_local (int);
74 static void obj_elf_visibility (int);
75 static void obj_elf_symver (int);
76 static void obj_elf_subsection (int);
77 static void obj_elf_popsection (int);
78 static void obj_elf_gnu_attribute (int);
79 static void obj_elf_tls_common (int);
80 static void obj_elf_lcomm (int);
81 static void obj_elf_struct (int);
82 static void obj_elf_attach_to_group (int);
84 static const pseudo_typeS elf_pseudo_table
[] =
86 {"attach_to_group", obj_elf_attach_to_group
, 0},
87 {"comm", obj_elf_common
, 0},
88 {"common", obj_elf_common
, 1},
89 {"ident", obj_elf_ident
, 0},
90 {"lcomm", obj_elf_lcomm
, 0},
91 {"local", obj_elf_local
, 0},
92 {"previous", obj_elf_previous
, 0},
93 {"section", obj_elf_section
, 0},
94 {"section.s", obj_elf_section
, 0},
95 {"sect", obj_elf_section
, 0},
96 {"sect.s", obj_elf_section
, 0},
97 {"pushsection", obj_elf_section
, 1},
98 {"popsection", obj_elf_popsection
, 0},
99 {"size", obj_elf_size
, 0},
100 {"type", obj_elf_type
, 0},
101 {"version", obj_elf_version
, 0},
102 {"weak", obj_elf_weak
, 0},
104 /* These define symbol visibility. */
105 {"internal", obj_elf_visibility
, STV_INTERNAL
},
106 {"hidden", obj_elf_visibility
, STV_HIDDEN
},
107 {"protected", obj_elf_visibility
, STV_PROTECTED
},
109 /* These are used for stabs-in-elf configurations. */
110 {"line", obj_elf_line
, 0},
112 /* This is a GNU extension to handle symbol versions. */
113 {"symver", obj_elf_symver
, 0},
115 /* A GNU extension to change subsection only. */
116 {"subsection", obj_elf_subsection
, 0},
118 /* These are GNU extensions to aid in garbage collecting C++ vtables. */
119 {"vtable_inherit", obj_elf_vtable_inherit
, 0},
120 {"vtable_entry", obj_elf_vtable_entry
, 0},
122 /* A GNU extension for object attributes. */
123 {"gnu_attribute", obj_elf_gnu_attribute
, 0},
125 /* These are used for dwarf2. */
126 { "file", dwarf2_directive_file
, 0 },
127 { "loc", dwarf2_directive_loc
, 0 },
128 { "loc_mark_labels", dwarf2_directive_loc_mark_labels
, 0 },
130 /* We need to trap the section changing calls to handle .previous. */
131 {"data", obj_elf_data
, 0},
132 {"offset", obj_elf_struct
, 0},
133 {"struct", obj_elf_struct
, 0},
134 {"text", obj_elf_text
, 0},
135 {"bss", obj_elf_bss
, 0},
137 {"tls_common", obj_elf_tls_common
, 0},
143 static const pseudo_typeS ecoff_debug_pseudo_table
[] =
145 #ifdef NEED_ECOFF_DEBUG
146 /* COFF style debugging information for ECOFF. .ln is not used; .loc
148 { "def", ecoff_directive_def
, 0 },
149 { "dim", ecoff_directive_dim
, 0 },
150 { "endef", ecoff_directive_endef
, 0 },
151 { "file", ecoff_directive_file
, 0 },
152 { "scl", ecoff_directive_scl
, 0 },
153 { "tag", ecoff_directive_tag
, 0 },
154 { "val", ecoff_directive_val
, 0 },
156 /* COFF debugging requires pseudo-ops .size and .type, but ELF
157 already has meanings for those. We use .esize and .etype
158 instead. These are only generated by gcc anyhow. */
159 { "esize", ecoff_directive_size
, 0 },
160 { "etype", ecoff_directive_type
, 0 },
162 /* ECOFF specific debugging information. */
163 { "aent", ecoff_directive_ent
, 1 },
164 { "begin", ecoff_directive_begin
, 0 },
165 { "bend", ecoff_directive_bend
, 0 },
166 { "end", ecoff_directive_end
, 0 },
167 { "ent", ecoff_directive_ent
, 0 },
168 { "fmask", ecoff_directive_fmask
, 0 },
169 { "frame", ecoff_directive_frame
, 0 },
170 { "loc", ecoff_directive_loc
, 0 },
171 { "mask", ecoff_directive_mask
, 0 },
173 /* Other ECOFF directives. */
174 { "extern", ecoff_directive_extern
, 0 },
176 /* These are used on Irix. I don't know how to implement them. */
177 { "alias", s_ignore
, 0 },
178 { "bgnb", s_ignore
, 0 },
179 { "endb", s_ignore
, 0 },
180 { "lab", s_ignore
, 0 },
181 { "noalias", s_ignore
, 0 },
182 { "verstamp", s_ignore
, 0 },
183 { "vreg", s_ignore
, 0 },
186 {NULL
, NULL
, 0} /* end sentinel */
190 #include "aout/aout64.h"
192 asection
*elf_com_section_ptr
;
195 elf_pop_insert (void)
197 pop_insert (elf_pseudo_table
);
199 pop_insert (ecoff_debug_pseudo_table
);
203 elf_s_get_size (symbolS
*sym
)
205 return S_GET_SIZE (sym
);
209 elf_s_set_size (symbolS
*sym
, bfd_vma sz
)
211 S_SET_SIZE (sym
, sz
);
215 elf_s_get_align (symbolS
*sym
)
217 return S_GET_ALIGN (sym
);
221 elf_s_set_align (symbolS
*sym
, bfd_vma align
)
223 S_SET_ALIGN (sym
, align
);
227 elf_s_get_other (symbolS
*sym
)
229 return elf_symbol (symbol_get_bfdsym (sym
))->internal_elf_sym
.st_other
;
233 elf_s_set_other (symbolS
*sym
, int other
)
235 S_SET_OTHER (sym
, other
);
239 elf_sec_sym_ok_for_reloc (asection
*sec
)
241 return obj_sec_sym_ok_for_reloc (sec
);
245 elf_file_symbol (const char *s
)
248 symbolS
*sym
= symbol_new (s
, absolute_section
, &zero_address_frag
, 0);
249 size_t name_length
= strlen (s
);
251 if (name_length
> strlen (S_GET_NAME (sym
)))
253 obstack_grow (¬es
, s
, name_length
+ 1);
254 S_SET_NAME (sym
, (const char *) obstack_finish (¬es
));
257 strcpy ((char *) S_GET_NAME (sym
), s
);
259 symbol_get_bfdsym (sym
)->flags
|= BSF_FILE
;
261 if (symbol_rootP
!= sym
262 && ((bsym
= symbol_get_bfdsym (symbol_rootP
)) == NULL
263 || (bsym
->flags
& BSF_FILE
) == 0))
265 symbol_remove (sym
, &symbol_rootP
, &symbol_lastP
);
266 symbol_insert (sym
, symbol_rootP
, &symbol_rootP
, &symbol_lastP
);
270 verify_symbol_chain (symbol_rootP
, symbol_lastP
);
273 #ifdef NEED_ECOFF_DEBUG
278 /* Called from read.c:s_comm after we've parsed .comm symbol, size.
279 Parse a possible alignment value. */
282 elf_common_parse (int ignore ATTRIBUTE_UNUSED
, symbolS
*symbolP
, addressT size
)
285 int is_local
= symbol_get_obj (symbolP
)->local
;
287 if (*input_line_pointer
== ',')
289 char *save
= input_line_pointer
;
291 input_line_pointer
++;
294 if (*input_line_pointer
== '"')
296 /* For sparc. Accept .common symbol, length, "bss" */
297 input_line_pointer
++;
298 /* Some use the dot, some don't. */
299 if (*input_line_pointer
== '.')
300 input_line_pointer
++;
301 /* Some say data, some say bss. */
302 if (startswith (input_line_pointer
, "bss\""))
303 input_line_pointer
+= 4;
304 else if (startswith (input_line_pointer
, "data\""))
305 input_line_pointer
+= 5;
308 char *p
= input_line_pointer
;
313 while (!is_end_of_line
[(unsigned char) *input_line_pointer
])
314 if (*input_line_pointer
++ == '"')
316 c
= *input_line_pointer
;
317 *input_line_pointer
= '\0';
318 as_bad (_("bad .common segment %s"), p
);
319 *input_line_pointer
= c
;
320 ignore_rest_of_line ();
323 /* ??? Don't ask me why these are always global. */
328 input_line_pointer
= save
;
329 align
= parse_align (is_local
);
330 if (align
== (addressT
) -1)
337 bss_alloc (symbolP
, size
, align
);
338 S_CLEAR_EXTERNAL (symbolP
);
342 S_SET_VALUE (symbolP
, size
);
343 S_SET_ALIGN (symbolP
, align
);
344 S_SET_EXTERNAL (symbolP
);
345 S_SET_SEGMENT (symbolP
, elf_com_section_ptr
);
348 symbol_get_bfdsym (symbolP
)->flags
|= BSF_OBJECT
;
354 obj_elf_common (int is_common
)
356 if (flag_mri
&& is_common
)
359 s_comm_internal (0, elf_common_parse
);
363 obj_elf_tls_common (int ignore ATTRIBUTE_UNUSED
)
365 symbolS
*symbolP
= s_comm_internal (0, elf_common_parse
);
368 symbol_get_bfdsym (symbolP
)->flags
|= BSF_THREAD_LOCAL
;
372 obj_elf_lcomm (int ignore ATTRIBUTE_UNUSED
)
374 symbolS
*symbolP
= s_comm_internal (0, s_lcomm_internal
);
377 symbol_get_bfdsym (symbolP
)->flags
|= BSF_OBJECT
;
381 get_sym_from_input_line_and_check (void)
387 c
= get_symbol_name (& name
);
388 sym
= symbol_find_or_make (name
);
389 *input_line_pointer
= c
;
390 SKIP_WHITESPACE_AFTER_NAME ();
392 /* There is no symbol name if input_line_pointer has not moved. */
393 if (name
== input_line_pointer
)
394 as_bad (_("Missing symbol name in directive"));
399 obj_elf_local (int ignore ATTRIBUTE_UNUSED
)
406 symbolP
= get_sym_from_input_line_and_check ();
407 c
= *input_line_pointer
;
408 S_CLEAR_EXTERNAL (symbolP
);
409 symbol_get_obj (symbolP
)->local
= 1;
412 input_line_pointer
++;
414 if (*input_line_pointer
== '\n')
419 demand_empty_rest_of_line ();
423 obj_elf_weak (int ignore ATTRIBUTE_UNUSED
)
430 symbolP
= get_sym_from_input_line_and_check ();
431 c
= *input_line_pointer
;
432 S_SET_WEAK (symbolP
);
435 input_line_pointer
++;
437 if (*input_line_pointer
== '\n')
442 demand_empty_rest_of_line ();
446 obj_elf_visibility (int visibility
)
451 elf_symbol_type
*elfsym
;
455 symbolP
= get_sym_from_input_line_and_check ();
457 bfdsym
= symbol_get_bfdsym (symbolP
);
458 elfsym
= elf_symbol_from (bfdsym
);
462 elfsym
->internal_elf_sym
.st_other
&= ~3;
463 elfsym
->internal_elf_sym
.st_other
|= visibility
;
465 c
= *input_line_pointer
;
468 input_line_pointer
++;
472 if (*input_line_pointer
== '\n')
478 demand_empty_rest_of_line ();
481 static segT previous_section
;
482 static int previous_subsection
;
486 struct section_stack
*next
;
488 int subseg
, prev_subseg
;
491 static struct section_stack
*section_stack
;
493 /* ELF section flags for unique sections. */
494 #define SEC_ASSEMBLER_SHF_MASK SHF_GNU_RETAIN
498 asection
**head
; /* Section lists. */
499 unsigned int num_group
; /* Number of lists. */
500 htab_t indexes
; /* Maps group name to index in head array. */
503 static struct group_list groups
;
506 match_section (const asection
*sec
, const struct elf_section_match
*match
)
508 const char *linked_to_symbol_name
= sec
->map_head
.linked_to_symbol_name
;
509 unsigned int sh_info
= elf_section_data (sec
)->this_hdr
.sh_info
;
510 bfd_vma sh_flags
= (elf_section_data (sec
)->this_hdr
.sh_flags
511 & SEC_ASSEMBLER_SHF_MASK
);
513 return (sh_info
== match
->sh_info
514 && sh_flags
== match
->sh_flags
515 && ((bfd_section_flags (sec
) & SEC_ASSEMBLER_SECTION_ID
)
516 == (match
->flags
& SEC_ASSEMBLER_SECTION_ID
))
517 && sec
->section_id
== match
->section_id
518 && (linked_to_symbol_name
== match
->linked_to_symbol_name
519 || (linked_to_symbol_name
!= NULL
520 && match
->linked_to_symbol_name
!= NULL
521 && strcmp (linked_to_symbol_name
,
522 match
->linked_to_symbol_name
) == 0)));
525 /* Return TRUE iff SEC matches the section info INF. */
528 get_section_by_match (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *inf
)
530 struct elf_section_match
*match
= (struct elf_section_match
*) inf
;
531 const char *gname
= match
->group_name
;
532 const char *group_name
= elf_group_name (sec
);
534 return ((group_name
== gname
535 || (group_name
!= NULL
537 && strcmp (group_name
, gname
) == 0))
538 && match_section (sec
, match
));
542 free_section_idx (void *ent
)
544 string_tuple_t
*tuple
= ent
;
545 free ((char *) tuple
->value
);
548 /* Go look in section lists kept per group for SEC_NAME with
549 properties given by MATCH. If info for the group named by
550 MATCH->GROUP_NAME has been initialised, set GROUP_IDX. */
553 group_section_find (const struct elf_section_match
*match
,
554 const char *sec_name
,
555 unsigned int **group_idx
)
559 groups
.num_group
= 0;
561 groups
.indexes
= htab_create_alloc (16, hash_string_tuple
, eq_string_tuple
,
562 free_section_idx
, notes_calloc
, NULL
);
567 *group_idx
= str_hash_find (groups
.indexes
, match
->group_name
);
568 if (*group_idx
== NULL
)
572 for (s
= groups
.head
[**group_idx
]; s
!= NULL
; s
= elf_next_in_group (s
))
573 if ((s
->name
== sec_name
574 || strcmp (s
->name
, sec_name
) == 0)
575 && match_section (s
, match
))
580 /* Insert SEC into section lists kept per group. MATCH and GROUP_IDX
581 must be from a prior call to group_section_find. */
584 group_section_insert (const struct elf_section_match
*match
,
586 unsigned int **group_idx
)
588 if (*group_idx
!= NULL
)
590 elf_next_in_group (sec
) = groups
.head
[**group_idx
];
591 groups
.head
[**group_idx
] = sec
;
595 unsigned int i
= groups
.num_group
;
597 groups
.head
= XRESIZEVEC (asection
*, groups
.head
, i
+ 128);
598 groups
.head
[i
] = sec
;
599 groups
.num_group
+= 1;
601 /* We keep the index into groups.head rather than the entry address
602 because groups.head may be realloc'd, and because str_hash values
603 are a void* we make a copy of the index. Strictly speaking there
604 is no guarantee that void* can represent any int value, so doing
605 without the indirection by casting an int or even uintptr_t may
606 for example lose lsbs of the value. */
607 unsigned int *idx_ptr
= XNEW (unsigned int);
609 str_hash_insert (groups
.indexes
, match
->group_name
, idx_ptr
, 0);
612 /* Handle the .section pseudo-op. This code supports two different
615 The first is found on Solaris, and looks like
616 .section ".sec1",#alloc,#execinstr,#write
617 Here the names after '#' are the SHF_* flags to turn on for the
618 section. I'm not sure how it determines the SHT_* type (BFD
619 doesn't really give us control over the type, anyhow).
621 The second format is found on UnixWare, and probably most SVR4
622 machines, and looks like
623 .section .sec1,"a",@progbits
624 The quoted string may contain any combination of a, w, x, and
625 represents the SHF_* flags to turn on for the section. The string
626 beginning with '@' can be progbits or nobits. There should be
627 other possibilities, but I don't know what they are. In any case,
628 BFD doesn't really let us set the section type. */
631 change_section (const char *name
,
635 struct elf_section_match
*match_p
,
638 subsegT new_subsection
)
643 const struct elf_backend_data
*bed
;
644 const struct bfd_elf_special_section
*ssect
;
648 static struct elf_section_match unused_match
;
649 match_p
= &unused_match
;
652 #ifdef md_flush_pending_output
653 md_flush_pending_output ();
656 /* Switch to the section, creating it if necessary. */
659 struct section_stack
*elt
;
660 elt
= XNEW (struct section_stack
);
661 elt
->next
= section_stack
;
663 elt
->prev_seg
= previous_section
;
664 elt
->subseg
= now_subseg
;
665 elt
->prev_subseg
= previous_subsection
;
669 obj_elf_section_change_hook ();
671 unsigned int *group_idx
= NULL
;
672 if (match_p
->group_name
)
673 old_sec
= group_section_find (match_p
, name
, &group_idx
);
675 old_sec
= bfd_get_section_by_name_if (stdoutput
, name
, get_section_by_match
,
680 subseg_set (sec
, new_subsection
);
684 sec
= subseg_force_new (name
, new_subsection
);
685 if (match_p
->group_name
)
686 group_section_insert (match_p
, sec
, &group_idx
);
689 bed
= get_elf_backend_data (stdoutput
);
690 ssect
= (*bed
->get_sec_type_attr
) (stdoutput
, sec
);
694 bool override
= false;
696 if (type
== SHT_NULL
)
698 else if (type
!= ssect
->type
)
701 /* Some older versions of gcc will emit
703 .section .init_array,"aw",@progbits
705 for __attribute__ ((section (".init_array"))).
706 "@progbits" is incorrect. Also for x86-64 large bss
707 sections, some older versions of gcc will emit
709 .section .lbss,"aw",@progbits
711 "@progbits" is incorrect. */
713 && (bed
->s
->arch_size
!= 64
714 || !(ssect
->attr
& SHF_X86_64_LARGE
))
716 && ssect
->type
!= SHT_INIT_ARRAY
717 && ssect
->type
!= SHT_FINI_ARRAY
718 && ssect
->type
!= SHT_PREINIT_ARRAY
)
720 /* We allow to specify any type for a .note section. */
721 if (ssect
->type
!= SHT_NOTE
722 /* Processor and application defined types are allowed too. */
723 && type
< SHT_LOPROC
)
724 as_warn (_("setting incorrect section type for %s"),
729 as_warn (_("ignoring incorrect section type for %s"),
735 if (old_sec
== NULL
&& ((attr
& ~(SHF_LINK_ORDER
738 & ~ssect
->attr
) != 0)
740 /* Strip SHF_GNU_RETAIN. */
741 bfd_vma generic_attr
= attr
;
742 if (elf_tdata (stdoutput
)->has_gnu_osabi
)
743 generic_attr
&= ~SHF_GNU_RETAIN
;
745 /* As a GNU extension, we permit a .note section to be
746 allocatable. If the linker sees an allocatable .note
747 section, it will create a PT_NOTE segment in the output
748 file. We also allow "x" for .note.GNU-stack. */
749 if (ssect
->type
== SHT_NOTE
750 && (generic_attr
== SHF_ALLOC
751 || generic_attr
== SHF_EXECINSTR
))
753 /* Allow different SHF_MERGE and SHF_STRINGS if we have
754 something like .rodata.str. */
755 else if (ssect
->suffix_length
== -2
756 && name
[ssect
->prefix_length
] == '.'
760 & ~SHF_STRINGS
) == 0)
762 /* .interp, .strtab and .symtab can have SHF_ALLOC. */
763 else if (generic_attr
== SHF_ALLOC
764 && (strcmp (name
, ".interp") == 0
765 || strcmp (name
, ".strtab") == 0
766 || strcmp (name
, ".symtab") == 0))
768 /* .note.GNU-stack can have SHF_EXECINSTR. */
769 else if (generic_attr
== SHF_EXECINSTR
770 && strcmp (name
, ".note.GNU-stack") == 0)
773 /* A section on Alpha may have SHF_ALPHA_GPREL. */
774 else if ((generic_attr
& ~ssect
->attr
) == SHF_ALPHA_GPREL
)
778 else if (generic_attr
== (SHF_EXECINSTR
| SHF_WRITE
| SHF_ALLOC
)
779 && (ssect
->type
== SHT_INIT_ARRAY
780 || ssect
->type
== SHT_FINI_ARRAY
781 || ssect
->type
== SHT_PREINIT_ARRAY
))
782 /* RX init/fini arrays can and should have the "awx" attributes set. */
787 if (match_p
->group_name
== NULL
)
788 as_warn (_("setting incorrect section attributes for %s"),
794 if (!override
&& old_sec
== NULL
)
798 /* Convert ELF type and flags to BFD flags. */
800 | ((attr
& SHF_WRITE
) ? 0 : SEC_READONLY
)
801 | ((attr
& SHF_ALLOC
) ? SEC_ALLOC
: 0)
802 | (((attr
& SHF_ALLOC
) && type
!= SHT_NOBITS
) ? SEC_LOAD
: 0)
803 | ((attr
& SHF_EXECINSTR
) ? SEC_CODE
: 0)
804 | ((attr
& SHF_MERGE
) ? SEC_MERGE
: 0)
805 | ((attr
& SHF_STRINGS
) ? SEC_STRINGS
: 0)
806 | ((attr
& SHF_EXCLUDE
) ? SEC_EXCLUDE
: 0)
807 | ((attr
& SHF_TLS
) ? SEC_THREAD_LOCAL
: 0));
808 #ifdef md_elf_section_flags
809 flags
= md_elf_section_flags (flags
, attr
, type
);
813 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
815 /* PR 28054: Set the SEC_ELF_OCTETS flag for debugging sections.
816 Based on the code in bfd/elf.c:_bfd_elf_make_section_from_shdr().
818 FIXME: We do not set the SEC_DEBUGGING flag because that causes
819 problems for the FT32 and MSP430 targets. Investigate and fix. */
820 if ((flags
& SEC_ALLOC
) == 0 && name
[0] == '.')
822 if ( startswith (name
, ".debug")
823 || startswith (name
, ".zdebug")
824 || startswith (name
, ".gnu.debuglto_.debug_")
825 || startswith (name
, ".gnu.linkonce.wi.")
826 || startswith (name
, GNU_BUILD_ATTRS_SECTION_NAME
)
827 || startswith (name
, ".note.gnu"))
828 flags
|= SEC_ELF_OCTETS
;
835 if (type
== SHT_NULL
)
836 type
= bfd_elf_get_default_section_type (flags
);
837 elf_section_type (sec
) = type
;
838 elf_section_flags (sec
) = attr
;
839 elf_section_data (sec
)->this_hdr
.sh_info
= match_p
->sh_info
;
841 /* Prevent SEC_HAS_CONTENTS from being inadvertently set. */
842 if (type
== SHT_NOBITS
)
843 seg_info (sec
)->bss
= 1;
845 /* Set the section ID and flags. */
846 sec
->section_id
= match_p
->section_id
;
847 flags
|= match_p
->flags
;
849 /* Set the linked-to symbol name. */
850 sec
->map_head
.linked_to_symbol_name
851 = match_p
->linked_to_symbol_name
;
853 bfd_set_section_flags (sec
, flags
);
854 if (flags
& SEC_MERGE
)
855 sec
->entsize
= entsize
;
856 elf_group_name (sec
) = match_p
->group_name
;
858 /* Add a symbol for this section to the symbol table. */
859 secsym
= symbol_find (name
);
862 /* We could be repurposing an undefined symbol here: make sure we
863 reset sy_value to look like other section symbols in order to avoid
864 trying to incorrectly resolve this section symbol later on. */
865 static const expressionS exp
= { .X_op
= O_constant
};
866 symbol_set_value_expression (secsym
, &exp
);
867 symbol_set_bfdsym (secsym
, sec
->symbol
);
870 symbol_table_insert (section_symbol (sec
));
875 && (unsigned) type
!= elf_section_type (old_sec
))
878 /* This is a special section with known type. User
879 assembly might get the section type wrong; Even high
880 profile projects like glibc have done so in the past.
881 So don't error in this case. */
882 as_warn (_("ignoring changed section type for %s"), name
);
884 /* Do error when assembly isn't self-consistent. */
885 as_bad (_("changed section type for %s"), name
);
890 /* If section attributes are specified the second time we see a
891 particular section, then check that they are the same as we
892 saw the first time. */
893 if (((old_sec
->flags
^ flags
)
894 & (SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_CODE
895 | SEC_EXCLUDE
| SEC_SORT_ENTRIES
| SEC_MERGE
| SEC_STRINGS
896 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
897 | SEC_THREAD_LOCAL
)))
900 as_warn (_("ignoring changed section attributes for %s"), name
);
902 as_bad (_("changed section attributes for %s"), name
);
905 /* FIXME: Maybe we should consider removing a previously set
906 processor or application specific attribute as suspicious? */
907 elf_section_flags (sec
) = attr
;
909 if ((flags
& SEC_MERGE
) && old_sec
->entsize
!= (unsigned) entsize
)
910 as_bad (_("changed section entity size for %s"), name
);
914 #ifdef md_elf_section_change_hook
915 md_elf_section_change_hook ();
920 obj_elf_change_section (const char *name
,
924 struct elf_section_match
*match_p
,
927 change_section (name
, type
, attr
, entsize
, match_p
, linkonce
, false, 0);
931 obj_elf_parse_section_letters (char *str
, size_t len
,
932 bool *is_clone
, int *inherit
, bfd_vma
*gnu_attr
)
946 if (len
> 1 && str
[1] == 'm')
950 if (len
> 1 && str
[1] == 's')
961 attr
|= SHF_LINK_ORDER
;
967 attr
|= SHF_EXECINSTR
;
982 *gnu_attr
|= SHF_GNU_MBIND
;
985 *gnu_attr
|= SHF_GNU_RETAIN
;
992 const char *bad_msg
= _("unrecognized .section attribute:"
993 " want a,e,o,w,x,M,S,G,T or number");
994 #ifdef md_elf_section_letter
995 bfd_vma md_attr
= md_elf_section_letter (*str
, &bad_msg
);
996 if (md_attr
!= (bfd_vma
) -1)
1003 struct elf_backend_data
*bed
;
1004 bfd_vma numeric_flags
= strtoul (str
, &end
, 0);
1006 attr
|= numeric_flags
;
1008 bed
= (struct elf_backend_data
*)
1009 get_elf_backend_data (stdoutput
);
1011 if (bed
->elf_osabi
== ELFOSABI_NONE
1012 || bed
->elf_osabi
== ELFOSABI_STANDALONE
1013 || bed
->elf_osabi
== ELFOSABI_GNU
1014 || bed
->elf_osabi
== ELFOSABI_FREEBSD
)
1016 /* Add flags in the SHF_MASKOS range to gnu_attr for
1017 OSABIs that support those flags.
1018 Also adding the flags for ELFOSABI_{NONE,STANDALONE}
1019 allows them to be validated later in obj_elf_section.
1020 We can't just always set these bits in gnu_attr for
1021 all OSABIs, since Binutils does not recognize all
1022 SHF_MASKOS bits for non-GNU OSABIs. It's therefore
1023 possible that numeric flags are being used to set bits
1024 in the SHF_MASKOS range for those targets, and we
1025 don't want assembly to fail in those situations. */
1026 *gnu_attr
|= (numeric_flags
& SHF_MASKOS
);
1029 /* Update str and len, allowing for the fact that
1030 we will execute str++ and len-- below. */
1035 else if (!attr
&& !*gnu_attr
&& (*str
== '+' || *str
== '-'))
1036 *inherit
= *str
== '+' ? 1 : -1;
1038 as_fatal ("%s", bad_msg
);
1049 obj_elf_section_type (char *str
, size_t len
, bool warn
)
1051 if (len
== 8 && startswith (str
, "progbits"))
1052 return SHT_PROGBITS
;
1053 if (len
== 6 && startswith (str
, "nobits"))
1055 if (len
== 4 && startswith (str
, "note"))
1057 if (len
== 10 && startswith (str
, "init_array"))
1058 return SHT_INIT_ARRAY
;
1059 if (len
== 10 && startswith (str
, "fini_array"))
1060 return SHT_FINI_ARRAY
;
1061 if (len
== 13 && startswith (str
, "preinit_array"))
1062 return SHT_PREINIT_ARRAY
;
1064 #ifdef md_elf_section_type
1066 int md_type
= md_elf_section_type (str
, len
);
1075 int type
= strtoul (str
, & end
, 0);
1077 if (warn
&& (size_t) (end
- str
) != len
)
1078 as_warn (_("extraneous characters at end of numeric section type"));
1084 as_warn (_("unrecognized section type"));
1090 obj_elf_section_word (char *str
, size_t len
, int *type
)
1094 if (len
== 5 && startswith (str
, "write"))
1096 if (len
== 5 && startswith (str
, "alloc"))
1098 if (len
== 9 && startswith (str
, "execinstr"))
1099 return SHF_EXECINSTR
;
1100 if (len
== 7 && startswith (str
, "exclude"))
1102 if (len
== 3 && startswith (str
, "tls"))
1105 ret
= obj_elf_section_type (str
, len
, false);
1109 as_warn (_("unrecognized section attribute"));
1115 /* Get name of section. */
1117 obj_elf_section_name (void)
1122 if (*input_line_pointer
== '"')
1126 name
= demand_copy_C_string (&dummy
);
1129 ignore_rest_of_line ();
1135 char *end
= input_line_pointer
;
1137 while (0 == strchr ("\n\t,; ", *end
))
1139 if (end
== input_line_pointer
)
1141 as_bad (_("missing name"));
1142 ignore_rest_of_line ();
1146 obstack_grow0 (¬es
, input_line_pointer
, end
- input_line_pointer
);
1147 name
= obstack_base (¬es
);
1149 while (flag_sectname_subst
)
1151 char *subst
= strchr (name
, '%');
1152 if (subst
&& subst
[1] == 'S')
1154 size_t head
= subst
- name
;
1155 size_t tail
= strlen (subst
+ 2) + 1;
1156 size_t slen
= strlen (now_seg
->name
);
1160 obstack_blank (¬es
, slen
- 2);
1161 name
= obstack_base (¬es
);
1163 memmove (name
+ head
+ slen
, name
+ head
+ 2, tail
);
1164 memcpy (name
+ head
, now_seg
->name
, slen
);
1170 obstack_finish (¬es
);
1172 #ifdef tc_canonicalize_section_name
1173 name
= tc_canonicalize_section_name (name
);
1175 input_line_pointer
= end
;
1181 /* Arrange to put SEC, known to be in group GNAME into the per-group
1182 section lists kept by gas. */
1185 elf_set_group_name (asection
*sec
, const char *gname
)
1187 elf_group_name (sec
) = gname
;
1188 elf_section_flags (sec
) |= SHF_GROUP
;
1190 struct elf_section_match match
;
1191 match
.group_name
= gname
;
1192 match
.linked_to_symbol_name
= sec
->map_head
.linked_to_symbol_name
;
1193 match
.section_id
= sec
->section_id
;
1194 match
.sh_info
= elf_section_data (sec
)->this_hdr
.sh_info
;
1195 match
.sh_flags
= (elf_section_data (sec
)->this_hdr
.sh_flags
1196 & SEC_ASSEMBLER_SHF_MASK
);
1197 match
.flags
= bfd_section_flags (sec
) & SEC_ASSEMBLER_SECTION_ID
;
1199 unsigned int *group_idx
;
1200 if (!group_section_find (&match
, sec
->name
, &group_idx
))
1201 group_section_insert (&match
, sec
, &group_idx
);
1205 obj_elf_attach_to_group (int dummy ATTRIBUTE_UNUSED
)
1207 const char * gname
= obj_elf_section_name ();
1211 as_warn (_("group name not parseable"));
1215 if (elf_group_name (now_seg
))
1217 if (strcmp (elf_group_name (now_seg
), gname
) != 0)
1218 as_warn (_("section %s already has a group (%s)"),
1219 bfd_section_name (now_seg
), elf_group_name (now_seg
));
1222 elf_set_group_name (now_seg
, gname
);
1226 obj_elf_section (int push
)
1235 subsegT new_subsection
= 0;
1236 struct elf_section_match match
;
1237 unsigned long linked_to_section_index
= -1UL;
1243 #ifdef md_flush_pending_output
1244 md_flush_pending_output ();
1247 obj_elf_section_change_hook ();
1249 s_mri_sect (&mri_type
);
1251 #ifdef md_elf_section_change_hook
1252 md_elf_section_change_hook ();
1258 name
= obj_elf_section_name ();
1262 memset (&match
, 0, sizeof (match
));
1265 if ((sym
= symbol_find (name
)) != NULL
1266 && ! symbol_section_p (sym
)
1267 && S_IS_DEFINED (sym
)
1268 && ! S_IS_VOLATILE (sym
)
1269 && ! S_CAN_BE_REDEFINED (sym
))
1271 as_bad (_("section name '%s' already defined as another symbol"), name
);
1272 ignore_rest_of_line ();
1281 if (*input_line_pointer
== ',')
1283 /* Skip the comma. */
1284 ++input_line_pointer
;
1287 if (push
&& ISDIGIT (*input_line_pointer
))
1289 /* .pushsection has an optional subsection. */
1290 new_subsection
= (subsegT
) get_absolute_expression ();
1294 /* Stop if we don't see a comma. */
1295 if (*input_line_pointer
!= ',')
1298 /* Skip the comma. */
1299 ++input_line_pointer
;
1303 if (*input_line_pointer
== '"')
1308 beg
= demand_copy_C_string (&dummy
);
1311 ignore_rest_of_line ();
1314 attr
= obj_elf_parse_section_letters (beg
, strlen (beg
), &is_clone
,
1315 &inherit
, &gnu_attr
);
1318 attr
|= elf_section_flags (now_seg
);
1319 else if (inherit
< 0)
1320 attr
= elf_section_flags (now_seg
) & ~attr
;
1322 type
= elf_section_type (now_seg
);
1325 if (*input_line_pointer
== ',')
1328 char *save
= input_line_pointer
;
1330 ++input_line_pointer
;
1332 c
= *input_line_pointer
;
1335 beg
= demand_copy_C_string (&dummy
);
1338 ignore_rest_of_line ();
1341 type
= obj_elf_section_type (beg
, strlen (beg
), true);
1343 else if (c
== '@' || c
== '%')
1345 ++input_line_pointer
;
1347 if (ISDIGIT (* input_line_pointer
))
1348 type
= strtoul (input_line_pointer
, &input_line_pointer
, 0);
1351 c
= get_symbol_name (& beg
);
1352 (void) restore_line_pointer (c
);
1353 type
= obj_elf_section_type (beg
,
1354 input_line_pointer
- beg
,
1359 input_line_pointer
= save
;
1363 if ((attr
& SHF_MERGE
) != 0 && *input_line_pointer
== ',')
1365 ++input_line_pointer
;
1367 if (inherit
&& *input_line_pointer
== ','
1368 && (bfd_section_flags (now_seg
) & SEC_MERGE
) != 0)
1370 entsize
= get_absolute_expression ();
1374 as_warn (_("invalid merge entity size"));
1379 else if ((attr
& SHF_MERGE
) != 0 && inherit
1380 && (bfd_section_flags (now_seg
) & SEC_MERGE
) != 0)
1383 entsize
= now_seg
->entsize
;
1385 else if ((attr
& SHF_MERGE
) != 0)
1387 as_warn (_("entity size for SHF_MERGE not specified"));
1391 if ((attr
& SHF_LINK_ORDER
) != 0 && *input_line_pointer
== ',')
1393 ++input_line_pointer
;
1395 /* Check for a numeric section index, rather than a symbol name. */
1396 if (ISDIGIT (* input_line_pointer
))
1398 linked_to_section_index
= strtoul (input_line_pointer
, & input_line_pointer
, 0);
1400 else if (inherit
&& *input_line_pointer
== ','
1401 && (elf_section_flags (now_seg
) & SHF_LINK_ORDER
) != 0)
1402 goto fetch_linked_to
;
1406 unsigned int length
;
1408 c
= get_symbol_name (& beg
);
1409 (void) restore_line_pointer (c
);
1410 length
= input_line_pointer
- beg
;
1412 match
.linked_to_symbol_name
= xmemdup0 (beg
, length
);
1415 else if ((attr
& SHF_LINK_ORDER
) != 0 && inherit
1416 && (elf_section_flags (now_seg
) & SHF_LINK_ORDER
) != 0)
1419 if (now_seg
->map_head
.linked_to_symbol_name
)
1420 match
.linked_to_symbol_name
=
1421 now_seg
->map_head
.linked_to_symbol_name
;
1423 linked_to_section_index
=
1424 elf_section_data (now_seg
)->this_hdr
.sh_link
;
1427 if ((attr
& SHF_GROUP
) != 0 && is_clone
)
1429 as_warn (_("? section flag ignored with G present"));
1433 if ((attr
& SHF_GROUP
) != 0 && *input_line_pointer
== ',')
1435 ++input_line_pointer
;
1437 if (inherit
&& *input_line_pointer
== ','
1438 && (elf_section_flags (now_seg
) & SHF_GROUP
) != 0)
1440 match
.group_name
= obj_elf_section_name ();
1441 if (match
.group_name
== NULL
)
1443 else if (*input_line_pointer
== ',')
1445 ++input_line_pointer
;
1447 if (startswith (input_line_pointer
, "comdat"))
1449 input_line_pointer
+= 6;
1453 else if (startswith (name
, ".gnu.linkonce"))
1456 else if ((attr
& SHF_GROUP
) != 0 && inherit
1457 && (elf_section_flags (now_seg
) & SHF_GROUP
) != 0)
1460 match
.group_name
= elf_group_name (now_seg
);
1462 (bfd_section_flags (now_seg
) & SEC_LINK_ONCE
) != 0;
1464 else if ((attr
& SHF_GROUP
) != 0)
1466 as_warn (_("group name for SHF_GROUP not specified"));
1472 const char *now_group
= elf_group_name (now_seg
);
1473 if (now_group
!= NULL
)
1475 match
.group_name
= now_group
;
1476 linkonce
= (now_seg
->flags
& SEC_LINK_ONCE
) != 0;
1480 if ((gnu_attr
& SHF_GNU_MBIND
) != 0 && *input_line_pointer
== ',')
1482 char *save
= input_line_pointer
;
1483 ++input_line_pointer
;
1485 if (ISDIGIT (* input_line_pointer
))
1487 char *t
= input_line_pointer
;
1488 match
.sh_info
= strtoul (input_line_pointer
,
1489 &input_line_pointer
, 0);
1490 if (match
.sh_info
== (unsigned int) -1)
1492 as_warn (_("unsupported mbind section info: %s"), t
);
1497 input_line_pointer
= save
;
1500 if ((gnu_attr
& SHF_GNU_RETAIN
) != 0)
1501 match
.sh_flags
|= SHF_GNU_RETAIN
;
1503 if (*input_line_pointer
== ',')
1505 char *save
= input_line_pointer
;
1507 ++input_line_pointer
;
1509 if (startswith (input_line_pointer
, "unique"))
1511 input_line_pointer
+= 6;
1513 if (*input_line_pointer
== ',')
1515 ++input_line_pointer
;
1517 if (ISDIGIT (* input_line_pointer
))
1521 char *t
= input_line_pointer
;
1522 if (sizeof (bfd_vma
) <= sizeof (unsigned long))
1525 id
= strtoul (input_line_pointer
,
1526 &input_line_pointer
, 0);
1527 overflow
= (id
== (unsigned long) -1
1528 && errno
== ERANGE
);
1533 (input_line_pointer
,
1534 (const char **) &input_line_pointer
, 0);
1535 overflow
= id
== ~(bfd_vma
) 0;
1537 if (overflow
|| id
> (unsigned int) -1)
1539 char *linefeed
, saved_char
= 0;
1540 if ((linefeed
= strchr (t
, '\n')) != NULL
)
1542 saved_char
= *linefeed
;
1545 as_bad (_("unsupported section id: %s"), t
);
1547 *linefeed
= saved_char
;
1551 match
.section_id
= id
;
1552 match
.flags
|= SEC_ASSEMBLER_SECTION_ID
;
1558 input_line_pointer
= save
;
1569 if (*input_line_pointer
!= '#')
1571 as_bad (_("character following name is not '#'"));
1572 ignore_rest_of_line ();
1575 ++input_line_pointer
;
1576 c
= get_symbol_name (& beg
);
1577 (void) restore_line_pointer (c
);
1579 attr
|= obj_elf_section_word (beg
, input_line_pointer
- beg
,
1584 while (*input_line_pointer
++ == ',');
1585 --input_line_pointer
;
1591 demand_empty_rest_of_line ();
1593 if ((gnu_attr
& (SHF_GNU_MBIND
| SHF_GNU_RETAIN
)) != 0)
1595 const struct elf_backend_data
*bed
;
1596 bool mbind_p
= (gnu_attr
& SHF_GNU_MBIND
) != 0;
1598 if (mbind_p
&& (attr
& SHF_ALLOC
) == 0)
1599 as_bad (_("SHF_ALLOC isn't set for GNU_MBIND section: %s"), name
);
1601 bed
= get_elf_backend_data (stdoutput
);
1603 if (bed
->elf_osabi
!= ELFOSABI_GNU
1604 && bed
->elf_osabi
!= ELFOSABI_FREEBSD
1605 && bed
->elf_osabi
!= ELFOSABI_NONE
)
1606 as_bad (_("%s section is supported only by GNU and FreeBSD targets"),
1607 mbind_p
? "GNU_MBIND" : "GNU_RETAIN");
1611 elf_tdata (stdoutput
)->has_gnu_osabi
|= elf_gnu_osabi_mbind
;
1612 if ((gnu_attr
& SHF_GNU_RETAIN
) != 0)
1613 elf_tdata (stdoutput
)->has_gnu_osabi
|= elf_gnu_osabi_retain
;
1619 change_section (name
, type
, attr
, entsize
, &match
, linkonce
, push
,
1622 if (linked_to_section_index
!= -1UL)
1624 elf_section_flags (now_seg
) |= SHF_LINK_ORDER
;
1625 elf_section_data (now_seg
)->this_hdr
.sh_link
= linked_to_section_index
;
1626 /* FIXME: Should we perform some sanity checking on the section index ? */
1630 /* Change to the .bss section. */
1633 obj_elf_bss (int i ATTRIBUTE_UNUSED
)
1637 #ifdef md_flush_pending_output
1638 md_flush_pending_output ();
1641 obj_elf_section_change_hook ();
1643 temp
= get_absolute_expression ();
1644 subseg_set (bss_section
, (subsegT
) temp
);
1645 demand_empty_rest_of_line ();
1647 #ifdef md_elf_section_change_hook
1648 md_elf_section_change_hook ();
1652 /* Change to the .data section. */
1655 obj_elf_data (int i
)
1657 #ifdef md_flush_pending_output
1658 md_flush_pending_output ();
1661 obj_elf_section_change_hook ();
1665 #ifdef md_elf_section_change_hook
1666 md_elf_section_change_hook ();
1670 /* Change to the .text section. */
1673 obj_elf_text (int i
)
1675 #ifdef md_flush_pending_output
1676 md_flush_pending_output ();
1679 obj_elf_section_change_hook ();
1683 #ifdef md_elf_section_change_hook
1684 md_elf_section_change_hook ();
1688 /* Change to the *ABS* section. */
1691 obj_elf_struct (int i
)
1693 #ifdef md_flush_pending_output
1694 md_flush_pending_output ();
1697 obj_elf_section_change_hook ();
1701 #ifdef md_elf_section_change_hook
1702 md_elf_section_change_hook ();
1707 obj_elf_subsection (int ignore ATTRIBUTE_UNUSED
)
1711 #ifdef md_flush_pending_output
1712 md_flush_pending_output ();
1715 obj_elf_section_change_hook ();
1717 temp
= get_absolute_expression ();
1718 subseg_set (now_seg
, (subsegT
) temp
);
1719 demand_empty_rest_of_line ();
1721 #ifdef md_elf_section_change_hook
1722 md_elf_section_change_hook ();
1726 /* This can be called from the processor backends if they change
1730 obj_elf_section_change_hook (void)
1732 previous_section
= now_seg
;
1733 previous_subsection
= now_subseg
;
1737 obj_elf_previous (int ignore ATTRIBUTE_UNUSED
)
1742 if (previous_section
== 0)
1744 as_warn (_(".previous without corresponding .section; ignored"));
1748 #ifdef md_flush_pending_output
1749 md_flush_pending_output ();
1752 new_section
= previous_section
;
1753 new_subsection
= previous_subsection
;
1754 obj_elf_section_change_hook ();
1756 subseg_set (new_section
, new_subsection
);
1758 #ifdef md_elf_section_change_hook
1759 md_elf_section_change_hook ();
1764 obj_elf_popsection (int xxx ATTRIBUTE_UNUSED
)
1766 struct section_stack
*top
= section_stack
;
1770 as_warn (_(".popsection without corresponding .pushsection; ignored"));
1774 #ifdef md_flush_pending_output
1775 md_flush_pending_output ();
1778 section_stack
= top
->next
;
1779 previous_section
= top
->prev_seg
;
1780 previous_subsection
= top
->prev_subseg
;
1781 subseg_set (top
->seg
, top
->subseg
);
1784 #ifdef md_elf_section_change_hook
1785 md_elf_section_change_hook ();
1790 obj_elf_line (int ignore ATTRIBUTE_UNUSED
)
1792 /* Assume delimiter is part of expression. BSD4.2 as fails with
1793 delightful bug, so we are not being incompatible here. */
1794 new_logical_line (NULL
, get_absolute_expression ());
1795 demand_empty_rest_of_line ();
1798 static struct elf_versioned_name_list
*
1799 obj_elf_find_and_add_versioned_name (const char *version_name
,
1800 const char *sym_name
,
1802 struct elf_obj_sy
*sy_obj
)
1804 struct elf_versioned_name_list
*versioned_name
;
1807 for (p
= ver
+ 1; *p
== ELF_VER_CHR
; p
++)
1810 /* NB: Since some tests in ld/testsuite/ld-elfvers have no version
1811 names, we have to disable this. */
1812 if (0 && *p
== '\0')
1814 as_bad (_("missing version name in `%s' for symbol `%s'"),
1815 version_name
, sym_name
);
1819 versioned_name
= sy_obj
->versioned_name
;
1829 if (strcmp (versioned_name
->name
, version_name
) == 0)
1830 return versioned_name
;
1833 as_bad (_("only one version name with `@@@' is allowed "
1834 "for symbol `%s'"), sym_name
);
1838 sy_obj
->rename
= true;
1841 as_bad (_("invalid version name '%s' for symbol `%s'"),
1842 version_name
, sym_name
);
1847 versioned_name
!= NULL
;
1848 versioned_name
= versioned_name
->next
)
1849 if (strcmp (versioned_name
->name
, version_name
) == 0)
1850 return versioned_name
;
1852 /* Add this versioned name to the head of the list, */
1853 versioned_name
= (struct elf_versioned_name_list
*)
1854 xmalloc (sizeof (*versioned_name
));
1855 versioned_name
->name
= xstrdup (version_name
);
1856 versioned_name
->next
= sy_obj
->versioned_name
;
1857 sy_obj
->versioned_name
= versioned_name
;
1859 return versioned_name
;
1862 /* This handles the .symver pseudo-op, which is used to specify a
1863 symbol version. The syntax is ``.symver NAME,SYMVERNAME''.
1864 SYMVERNAME may contain ELF_VER_CHR ('@') characters. This
1865 pseudo-op causes the assembler to emit a symbol named SYMVERNAME
1866 with the same value as the symbol NAME. */
1869 obj_elf_symver (int ignore ATTRIBUTE_UNUSED
)
1872 const char *sym_name
;
1876 struct elf_obj_sy
*sy_obj
;
1879 sym
= get_sym_from_input_line_and_check ();
1881 if (*input_line_pointer
!= ',')
1883 as_bad (_("expected comma after name in .symver"));
1884 ignore_rest_of_line ();
1888 ++input_line_pointer
;
1891 /* Temporarily include '@' in symbol names. */
1892 old_lexat
= lex_type
[(unsigned char) '@'];
1893 lex_type
[(unsigned char) '@'] |= LEX_NAME
;
1894 c
= get_symbol_name (& name
);
1895 lex_type
[(unsigned char) '@'] = old_lexat
;
1896 sym_name
= S_GET_NAME (sym
);
1898 if (S_IS_COMMON (sym
))
1900 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
1902 ignore_rest_of_line ();
1906 p
= strchr (name
, ELF_VER_CHR
);
1909 as_bad (_("missing version name in `%s' for symbol `%s'"),
1911 ignore_rest_of_line ();
1915 sy_obj
= symbol_get_obj (sym
);
1916 if (obj_elf_find_and_add_versioned_name (name
, sym_name
,
1919 sy_obj
->bad_version
= true;
1920 ignore_rest_of_line ();
1924 (void) restore_line_pointer (c
);
1926 if (*input_line_pointer
== ',')
1928 char *save
= input_line_pointer
;
1930 ++input_line_pointer
;
1932 if (startswith (input_line_pointer
, "local"))
1934 input_line_pointer
+= 5;
1935 sy_obj
->visibility
= visibility_local
;
1937 else if (startswith (input_line_pointer
, "hidden"))
1939 input_line_pointer
+= 6;
1940 sy_obj
->visibility
= visibility_hidden
;
1942 else if (startswith (input_line_pointer
, "remove"))
1944 input_line_pointer
+= 6;
1945 sy_obj
->visibility
= visibility_remove
;
1948 input_line_pointer
= save
;
1951 demand_empty_rest_of_line ();
1954 /* This handles the .vtable_inherit pseudo-op, which is used to indicate
1955 to the linker the hierarchy in which a particular table resides. The
1956 syntax is ".vtable_inherit CHILDNAME, PARENTNAME". */
1959 obj_elf_get_vtable_inherit (void)
1961 char *cname
, *pname
;
1962 symbolS
*csym
, *psym
;
1965 if (*input_line_pointer
== '#')
1966 ++input_line_pointer
;
1968 c
= get_symbol_name (& cname
);
1969 csym
= symbol_find (cname
);
1971 /* GCFIXME: should check that we don't have two .vtable_inherits for
1972 the same child symbol. Also, we can currently only do this if the
1973 child symbol is already exists and is placed in a fragment. */
1975 if (csym
== NULL
|| symbol_get_frag (csym
) == NULL
)
1977 as_bad (_("expected `%s' to have already been set for .vtable_inherit"),
1982 *input_line_pointer
= c
;
1984 SKIP_WHITESPACE_AFTER_NAME ();
1985 if (*input_line_pointer
!= ',')
1987 as_bad (_("expected comma after name in .vtable_inherit"));
1988 ignore_rest_of_line ();
1992 ++input_line_pointer
;
1995 if (*input_line_pointer
== '#')
1996 ++input_line_pointer
;
1998 if (input_line_pointer
[0] == '0'
1999 && (input_line_pointer
[1] == '\0'
2000 || ISSPACE (input_line_pointer
[1])))
2002 psym
= section_symbol (absolute_section
);
2003 ++input_line_pointer
;
2007 c
= get_symbol_name (& pname
);
2008 psym
= symbol_find_or_make (pname
);
2009 restore_line_pointer (c
);
2012 demand_empty_rest_of_line ();
2017 gas_assert (symbol_get_value_expression (csym
)->X_op
== O_constant
);
2018 return fix_new (symbol_get_frag (csym
),
2019 symbol_get_value_expression (csym
)->X_add_number
,
2020 0, psym
, 0, 0, BFD_RELOC_VTABLE_INHERIT
);
2023 /* This is a version of obj_elf_get_vtable_inherit() that is
2024 suitable for use in struct _pseudo_type tables. */
2027 obj_elf_vtable_inherit (int ignore ATTRIBUTE_UNUSED
)
2029 (void) obj_elf_get_vtable_inherit ();
2032 /* This handles the .vtable_entry pseudo-op, which is used to indicate
2033 to the linker that a vtable slot was used. The syntax is
2034 ".vtable_entry tablename, offset". */
2037 obj_elf_get_vtable_entry (void)
2042 if (*input_line_pointer
== '#')
2043 ++input_line_pointer
;
2045 sym
= get_sym_from_input_line_and_check ();
2046 if (*input_line_pointer
!= ',')
2048 as_bad (_("expected comma after name in .vtable_entry"));
2049 ignore_rest_of_line ();
2053 ++input_line_pointer
;
2054 if (*input_line_pointer
== '#')
2055 ++input_line_pointer
;
2057 offset
= get_absolute_expression ();
2059 demand_empty_rest_of_line ();
2061 return fix_new (frag_now
, frag_now_fix (), 0, sym
, offset
, 0,
2062 BFD_RELOC_VTABLE_ENTRY
);
2065 /* This is a version of obj_elf_get_vtable_entry() that is
2066 suitable for use in struct _pseudo_type tables. */
2069 obj_elf_vtable_entry (int ignore ATTRIBUTE_UNUSED
)
2071 (void) obj_elf_get_vtable_entry ();
2074 #define skip_whitespace(str) do { if (*(str) == ' ') ++(str); } while (0)
2077 skip_past_char (char ** str
, char c
)
2087 #define skip_past_comma(str) skip_past_char (str, ',')
2089 /* A list of attributes that have been explicitly set by the assembly code.
2090 VENDOR is the vendor id, BASE is the tag shifted right by the number
2091 of bits in MASK, and bit N of MASK is set if tag BASE+N has been set. */
2092 struct recorded_attribute_info
{
2093 struct recorded_attribute_info
*next
;
2098 static struct recorded_attribute_info
*recorded_attributes
;
2100 /* Record that we have seen an explicit specification of attribute TAG
2101 for vendor VENDOR. */
2104 record_attribute (int vendor
, unsigned int tag
)
2108 struct recorded_attribute_info
*rai
;
2110 base
= tag
/ (8 * sizeof (rai
->mask
));
2111 mask
= 1UL << (tag
% (8 * sizeof (rai
->mask
)));
2112 for (rai
= recorded_attributes
; rai
; rai
= rai
->next
)
2113 if (rai
->vendor
== vendor
&& rai
->base
== base
)
2119 rai
= XNEW (struct recorded_attribute_info
);
2120 rai
->next
= recorded_attributes
;
2121 rai
->vendor
= vendor
;
2124 recorded_attributes
= rai
;
2127 /* Return true if we have seen an explicit specification of attribute TAG
2128 for vendor VENDOR. */
2131 obj_elf_seen_attribute (int vendor
, unsigned int tag
)
2135 struct recorded_attribute_info
*rai
;
2137 base
= tag
/ (8 * sizeof (rai
->mask
));
2138 mask
= 1UL << (tag
% (8 * sizeof (rai
->mask
)));
2139 for (rai
= recorded_attributes
; rai
; rai
= rai
->next
)
2140 if (rai
->vendor
== vendor
&& rai
->base
== base
)
2141 return (rai
->mask
& mask
) != 0;
2145 /* Parse an attribute directive for VENDOR.
2146 Returns the attribute number read, or zero on error. */
2149 obj_elf_vendor_attribute (int vendor
)
2157 /* Read the first number or name. */
2158 skip_whitespace (input_line_pointer
);
2159 s
= input_line_pointer
;
2160 if (ISDIGIT (*input_line_pointer
))
2163 if (exp
.X_op
!= O_constant
)
2165 tag
= exp
.X_add_number
;
2171 /* A name may contain '_', but no other punctuation. */
2172 for (; ISALNUM (*input_line_pointer
) || *input_line_pointer
== '_';
2173 ++input_line_pointer
)
2178 name
= xmemdup0 (s
, i
);
2180 #ifndef CONVERT_SYMBOLIC_ATTRIBUTE
2181 #define CONVERT_SYMBOLIC_ATTRIBUTE(a) -1
2184 tag
= CONVERT_SYMBOLIC_ATTRIBUTE (name
);
2187 as_bad (_("Attribute name not recognised: %s"), name
);
2188 ignore_rest_of_line ();
2195 type
= _bfd_elf_obj_attrs_arg_type (stdoutput
, vendor
, tag
);
2197 if (skip_past_comma (&input_line_pointer
) == -1)
2202 if (exp
.X_op
!= O_constant
)
2204 as_bad (_("expected numeric constant"));
2205 ignore_rest_of_line ();
2208 i
= exp
.X_add_number
;
2211 && skip_past_comma (&input_line_pointer
) == -1)
2213 as_bad (_("expected comma"));
2214 ignore_rest_of_line ();
2221 skip_whitespace (input_line_pointer
);
2222 if (*input_line_pointer
!= '"')
2224 s
= demand_copy_C_string (&len
);
2227 record_attribute (vendor
, tag
);
2232 ok
= bfd_elf_add_obj_attr_int_string (stdoutput
, vendor
, tag
, i
, s
);
2235 ok
= bfd_elf_add_obj_attr_string (stdoutput
, vendor
, tag
, s
);
2238 ok
= bfd_elf_add_obj_attr_int (stdoutput
, vendor
, tag
, i
);
2244 as_fatal (_("error adding attribute: %s"),
2245 bfd_errmsg (bfd_get_error ()));
2247 demand_empty_rest_of_line ();
2250 as_bad (_("bad string constant"));
2251 ignore_rest_of_line ();
2254 as_bad (_("expected <tag> , <value>"));
2255 ignore_rest_of_line ();
2259 /* Parse a .gnu_attribute directive. */
2262 obj_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED
)
2264 obj_elf_vendor_attribute (OBJ_ATTR_GNU
);
2268 elf_obj_read_begin_hook (void)
2270 #ifdef NEED_ECOFF_DEBUG
2271 if (ECOFF_DEBUGGING
)
2272 ecoff_read_begin_hook ();
2277 elf_obj_symbol_new_hook (symbolS
*symbolP
)
2279 struct elf_obj_sy
*sy_obj
;
2281 sy_obj
= symbol_get_obj (symbolP
);
2282 sy_obj
->size
= NULL
;
2283 sy_obj
->versioned_name
= NULL
;
2285 #ifdef NEED_ECOFF_DEBUG
2286 if (ECOFF_DEBUGGING
)
2287 ecoff_symbol_new_hook (symbolP
);
2291 /* Deduplicate size expressions. We might get into trouble with
2292 multiple freeing or use after free if we leave them pointing to the
2293 same expressionS. */
2296 elf_obj_symbol_clone_hook (symbolS
*newsym
, symbolS
*orgsym ATTRIBUTE_UNUSED
)
2298 struct elf_obj_sy
*newelf
= symbol_get_obj (newsym
);
2301 expressionS
*exp
= XNEW (expressionS
);
2302 *exp
= *newelf
->size
;
2308 elf_copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
2310 struct elf_obj_sy
*srcelf
= symbol_get_obj (src
);
2311 struct elf_obj_sy
*destelf
= symbol_get_obj (dest
);
2312 /* If size is unset, copy size from src. Because we don't track whether
2313 .size has been used, we can't differentiate .size dest, 0 from the case
2314 where dest's size is unset. */
2315 if (!destelf
->size
&& S_GET_SIZE (dest
) == 0)
2319 destelf
->size
= XNEW (expressionS
);
2320 *destelf
->size
= *srcelf
->size
;
2322 S_SET_SIZE (dest
, S_GET_SIZE (src
));
2324 /* Don't copy visibility. */
2325 S_SET_OTHER (dest
, (ELF_ST_VISIBILITY (S_GET_OTHER (dest
))
2326 | (S_GET_OTHER (src
) & ~ELF_ST_VISIBILITY (-1))));
2330 obj_elf_version (int ignore ATTRIBUTE_UNUSED
)
2335 asection
*seg
= now_seg
;
2336 subsegT subseg
= now_subseg
;
2337 Elf_Internal_Note i_note
;
2338 Elf_External_Note e_note
;
2339 asection
*note_secp
= NULL
;
2342 if (*input_line_pointer
== '\"')
2346 ++input_line_pointer
; /* -> 1st char of string. */
2347 name
= input_line_pointer
;
2349 while (is_a_char (c
= next_char_of_string ()))
2351 c
= *input_line_pointer
;
2352 *input_line_pointer
= '\0';
2353 *(input_line_pointer
- 1) = '\0';
2354 *input_line_pointer
= c
;
2356 /* Create the .note section. */
2357 note_secp
= subseg_new (".note", 0);
2358 bfd_set_section_flags (note_secp
, SEC_HAS_CONTENTS
| SEC_READONLY
);
2359 record_alignment (note_secp
, 2);
2361 /* Process the version string. */
2362 len
= strlen (name
) + 1;
2364 /* PR 3456: Although the name field is padded out to an 4-byte
2365 boundary, the namesz field should not be adjusted. */
2366 i_note
.namesz
= len
;
2367 i_note
.descsz
= 0; /* No description. */
2368 i_note
.type
= NT_VERSION
;
2369 p
= frag_more (sizeof (e_note
.namesz
));
2370 md_number_to_chars (p
, i_note
.namesz
, sizeof (e_note
.namesz
));
2371 p
= frag_more (sizeof (e_note
.descsz
));
2372 md_number_to_chars (p
, i_note
.descsz
, sizeof (e_note
.descsz
));
2373 p
= frag_more (sizeof (e_note
.type
));
2374 md_number_to_chars (p
, i_note
.type
, sizeof (e_note
.type
));
2375 p
= frag_more (len
);
2376 memcpy (p
, name
, len
);
2378 frag_align (2, 0, 0);
2380 subseg_set (seg
, subseg
);
2383 as_bad (_("expected quoted string"));
2385 demand_empty_rest_of_line ();
2389 obj_elf_size (int ignore ATTRIBUTE_UNUSED
)
2392 char c
= get_symbol_name (&name
);
2397 p
= input_line_pointer
;
2399 SKIP_WHITESPACE_AFTER_NAME ();
2400 if (*input_line_pointer
!= ',')
2403 as_bad (_("expected comma after name `%s' in .size directive"), name
);
2405 ignore_rest_of_line ();
2408 input_line_pointer
++;
2410 if (exp
.X_op
== O_absent
)
2412 as_bad (_("missing expression in .size directive"));
2413 exp
.X_op
= O_constant
;
2414 exp
.X_add_number
= 0;
2417 sym
= symbol_find_or_make (name
);
2419 if (exp
.X_op
== O_constant
)
2421 S_SET_SIZE (sym
, exp
.X_add_number
);
2422 xfree (symbol_get_obj (sym
)->size
);
2423 symbol_get_obj (sym
)->size
= NULL
;
2427 symbol_get_obj (sym
)->size
= XNEW (expressionS
);
2428 *symbol_get_obj (sym
)->size
= exp
;
2431 /* If the symbol in the directive matches the current function being
2432 processed, indicate end of the current stream of ginsns. */
2434 && S_IS_FUNCTION (sym
) && sym
== ginsn_data_func_symbol ())
2435 ginsn_data_end (symbol_temp_new_now ());
2437 demand_empty_rest_of_line ();
2440 /* Handle the ELF .type pseudo-op. This sets the type of a symbol.
2441 There are six syntaxes:
2443 The first (used on Solaris) is
2445 The second (used on UnixWare) is
2447 The third (reportedly to be used on Irix 6.0) is
2449 The fourth (used on NetBSD/Arm and Linux/ARM) is
2451 The fifth (used on SVR4/860) is
2452 .type SYM,"function"
2453 The sixth (emitted by recent SunPRO under Solaris) is
2455 where the integer is the STT_* value.
2459 obj_elf_type_name (char *cp
)
2463 p
= input_line_pointer
;
2464 if (*input_line_pointer
>= '0'
2465 && *input_line_pointer
<= '9')
2467 while (*input_line_pointer
>= '0'
2468 && *input_line_pointer
<= '9')
2469 ++input_line_pointer
;
2470 *cp
= *input_line_pointer
;
2471 *input_line_pointer
= '\0';
2474 *cp
= get_symbol_name (&p
);
2480 obj_elf_type (int ignore ATTRIBUTE_UNUSED
)
2484 const char *type_name
;
2486 elf_symbol_type
*elfsym
;
2488 sym
= get_sym_from_input_line_and_check ();
2489 c
= *input_line_pointer
;
2490 elfsym
= (elf_symbol_type
*) symbol_get_bfdsym (sym
);
2492 if (*input_line_pointer
== ',')
2493 ++input_line_pointer
;
2496 if ( *input_line_pointer
== '#'
2497 || *input_line_pointer
== '@'
2498 || *input_line_pointer
== '"'
2499 || *input_line_pointer
== '%')
2500 ++input_line_pointer
;
2502 type_name
= obj_elf_type_name (& c
);
2505 if (strcmp (type_name
, "function") == 0
2506 || strcmp (type_name
, "2") == 0
2507 || strcmp (type_name
, "STT_FUNC") == 0)
2508 type
= BSF_FUNCTION
;
2509 else if (strcmp (type_name
, "object") == 0
2510 || strcmp (type_name
, "1") == 0
2511 || strcmp (type_name
, "STT_OBJECT") == 0)
2513 else if (strcmp (type_name
, "tls_object") == 0
2514 || strcmp (type_name
, "6") == 0
2515 || strcmp (type_name
, "STT_TLS") == 0)
2516 type
= BSF_OBJECT
| BSF_THREAD_LOCAL
;
2517 else if (strcmp (type_name
, "notype") == 0
2518 || strcmp (type_name
, "0") == 0
2519 || strcmp (type_name
, "STT_NOTYPE") == 0)
2521 else if (strcmp (type_name
, "common") == 0
2522 || strcmp (type_name
, "5") == 0
2523 || strcmp (type_name
, "STT_COMMON") == 0)
2527 if (! S_IS_COMMON (sym
))
2529 if (S_IS_VOLATILE (sym
))
2531 sym
= symbol_clone (sym
, 1);
2532 S_SET_SEGMENT (sym
, bfd_com_section_ptr
);
2533 S_SET_VALUE (sym
, 0);
2534 S_SET_EXTERNAL (sym
);
2535 symbol_set_frag (sym
, &zero_address_frag
);
2536 S_CLEAR_VOLATILE (sym
);
2538 else if (S_IS_DEFINED (sym
) || symbol_equated_p (sym
))
2539 as_bad (_("symbol '%s' is already defined"), S_GET_NAME (sym
));
2542 /* FIXME: Is it safe to just change the section ? */
2543 S_SET_SEGMENT (sym
, bfd_com_section_ptr
);
2544 S_SET_VALUE (sym
, 0);
2545 S_SET_EXTERNAL (sym
);
2549 else if (strcmp (type_name
, "gnu_indirect_function") == 0
2550 || strcmp (type_name
, "10") == 0
2551 || strcmp (type_name
, "STT_GNU_IFUNC") == 0)
2553 const struct elf_backend_data
*bed
;
2555 bed
= get_elf_backend_data (stdoutput
);
2556 if (bed
->elf_osabi
!= ELFOSABI_NONE
2557 && bed
->elf_osabi
!= ELFOSABI_GNU
2558 && bed
->elf_osabi
!= ELFOSABI_FREEBSD
)
2559 as_bad (_("symbol type \"%s\" is supported only by GNU "
2560 "and FreeBSD targets"), type_name
);
2561 /* MIPS targets do not support IFUNCS. */
2562 else if (bed
->target_id
== MIPS_ELF_DATA
)
2563 as_bad (_("symbol type \"%s\" is not supported by "
2564 "MIPS targets"), type_name
);
2565 elf_tdata (stdoutput
)->has_gnu_osabi
|= elf_gnu_osabi_ifunc
;
2566 type
= BSF_FUNCTION
| BSF_GNU_INDIRECT_FUNCTION
;
2568 else if (strcmp (type_name
, "gnu_unique_object") == 0)
2570 const struct elf_backend_data
*bed
;
2572 bed
= get_elf_backend_data (stdoutput
);
2573 if (bed
->elf_osabi
!= ELFOSABI_NONE
2574 && bed
->elf_osabi
!= ELFOSABI_GNU
)
2575 as_bad (_("symbol type \"%s\" is supported only by GNU targets"),
2577 elf_tdata (stdoutput
)->has_gnu_osabi
|= elf_gnu_osabi_unique
;
2578 type
= BSF_OBJECT
| BSF_GNU_UNIQUE
;
2580 #ifdef md_elf_symbol_type
2581 else if ((type
= md_elf_symbol_type (type_name
, sym
, elfsym
)) != -1)
2585 as_bad (_("unrecognized symbol type \"%s\""), type_name
);
2587 *input_line_pointer
= c
;
2589 if (*input_line_pointer
== '"')
2590 ++input_line_pointer
;
2592 #ifdef md_elf_symbol_type_change
2593 if (!md_elf_symbol_type_change (sym
, elfsym
, type
))
2596 flagword mask
= BSF_FUNCTION
| BSF_OBJECT
;
2598 if (type
!= BSF_FUNCTION
)
2599 mask
|= BSF_GNU_INDIRECT_FUNCTION
;
2600 if (type
!= BSF_OBJECT
)
2602 mask
|= BSF_GNU_UNIQUE
| BSF_THREAD_LOCAL
;
2604 if (S_IS_COMMON (sym
))
2606 as_bad (_("cannot change type of common symbol '%s'"),
2612 /* Don't warn when changing to STT_NOTYPE. */
2615 flagword
new = (elfsym
->symbol
.flags
& ~mask
) | type
;
2617 if (new != (elfsym
->symbol
.flags
| type
))
2618 as_warn (_("symbol '%s' already has its type set"), S_GET_NAME (sym
));
2619 elfsym
->symbol
.flags
= new;
2622 elfsym
->symbol
.flags
&= ~mask
;
2625 if (S_IS_FUNCTION (sym
) && flag_synth_cfi
)
2627 /* When using SCFI, .type directive indicates start of a new FDE for SCFI
2628 processing. So, we must first demarcate the previous block of ginsns,
2629 if any, to mark the end of a previous FDE. */
2630 if (frchain_now
->frch_ginsn_data
)
2631 ginsn_data_end (symbol_temp_new_now ());
2632 ginsn_data_begin (sym
);
2635 demand_empty_rest_of_line ();
2638 static segT comment_section
;
2641 obj_elf_ident (int ignore ATTRIBUTE_UNUSED
)
2643 segT old_section
= now_seg
;
2644 int old_subsection
= now_subseg
;
2646 #ifdef md_flush_pending_output
2647 md_flush_pending_output ();
2650 if (!comment_section
)
2653 comment_section
= subseg_new (".comment", 0);
2654 bfd_set_section_flags (comment_section
, (SEC_READONLY
| SEC_HAS_CONTENTS
2655 | SEC_MERGE
| SEC_STRINGS
));
2656 comment_section
->entsize
= 1;
2657 #ifdef md_elf_section_change_hook
2658 md_elf_section_change_hook ();
2665 subseg_set (comment_section
, 0);
2666 #ifdef md_elf_section_change_hook
2667 md_elf_section_change_hook ();
2671 subseg_set (old_section
, old_subsection
);
2672 #ifdef md_elf_section_change_hook
2673 md_elf_section_change_hook ();
2677 #ifdef INIT_STAB_SECTION
2679 /* The first entry in a .stabs section is special. */
2682 obj_elf_init_stab_section (segT stab
, segT stabstr
)
2686 unsigned int stroff
;
2688 /* Force the section to align to a longword boundary. Without this,
2689 UnixWare ar crashes. */
2690 bfd_set_section_alignment (stab
, 2);
2692 /* Make space for this first symbol. */
2696 file
= remap_debug_filename (as_where (NULL
));
2697 stroff
= get_stab_string_offset (file
, stabstr
);
2698 know (stroff
== 1 || (stroff
== 0 && file
[0] == '\0'));
2699 md_number_to_chars (p
, stroff
, 4);
2700 seg_info (stab
)->stabu
.p
= p
;
2706 /* Called via bfd_map_over_sections. If SEC's linked_to_symbol_name
2707 isn't NULL, set up its linked-to section.
2708 For .stabs section, fill in the counts in the first entry. */
2711 set_additional_section_info (bfd
*abfd
,
2713 void *inf ATTRIBUTE_UNUSED
)
2715 if (sec
->map_head
.linked_to_symbol_name
)
2717 symbolS
*linked_to_sym
;
2718 linked_to_sym
= symbol_find (sec
->map_head
.linked_to_symbol_name
);
2719 if (!linked_to_sym
|| !S_IS_DEFINED (linked_to_sym
))
2720 as_bad (_("undefined linked-to symbol `%s' on section `%s'"),
2721 sec
->map_head
.linked_to_symbol_name
,
2722 bfd_section_name (sec
));
2724 elf_linked_to_section (sec
) = S_GET_SEGMENT (linked_to_sym
);
2732 if (!startswith (sec
->name
, ".stab"))
2734 if (!strcmp ("str", sec
->name
+ strlen (sec
->name
) - 3))
2737 name
= concat (sec
->name
, "str", NULL
);
2738 strsec
= bfd_get_section_by_name (abfd
, name
);
2740 strsz
= bfd_section_size (strsec
);
2743 nsyms
= bfd_section_size (sec
) / 12 - 1;
2745 p
= seg_info (sec
)->stabu
.p
;
2746 gas_assert (p
!= 0);
2748 bfd_h_put_16 (abfd
, nsyms
, p
+ 6);
2749 bfd_h_put_32 (abfd
, strsz
, p
+ 8);
2753 #ifdef NEED_ECOFF_DEBUG
2755 /* This function is called by the ECOFF code. It is supposed to
2756 record the external symbol information so that the backend can
2757 write it out correctly. The ELF backend doesn't actually handle
2758 this at the moment, so we do it ourselves. We save the information
2761 #ifdef OBJ_MAYBE_ELF
2765 elf_ecoff_set_ext (symbolS
*sym
, struct ecoff_extr
*ext
)
2767 symbol_get_bfdsym (sym
)->udata
.p
= ext
;
2770 /* This function is called by bfd_ecoff_debug_externals. It is
2771 supposed to *EXT to the external symbol information, and return
2772 whether the symbol should be used at all. */
2775 elf_get_extr (asymbol
*sym
, EXTR
*ext
)
2777 if (sym
->udata
.p
== NULL
)
2779 *ext
= *(EXTR
*) sym
->udata
.p
;
2783 /* This function is called by bfd_ecoff_debug_externals. It has
2784 nothing to do for ELF. */
2787 elf_set_index (asymbol
*sym ATTRIBUTE_UNUSED
,
2788 bfd_size_type indx ATTRIBUTE_UNUSED
)
2792 #endif /* NEED_ECOFF_DEBUG */
2795 elf_frob_symbol (symbolS
*symp
, int *puntp
)
2797 struct elf_obj_sy
*sy_obj
;
2799 struct elf_versioned_name_list
*versioned_name
;
2801 #ifdef NEED_ECOFF_DEBUG
2802 if (ECOFF_DEBUGGING
)
2803 ecoff_frob_symbol (symp
);
2806 sy_obj
= symbol_get_obj (symp
);
2808 size
= sy_obj
->size
;
2811 if (resolve_expression (size
)
2812 && size
->X_op
== O_constant
)
2813 S_SET_SIZE (symp
, size
->X_add_number
);
2816 if (!flag_allow_nonconst_size
)
2817 as_bad (_(".size expression for %s "
2818 "does not evaluate to a constant"), S_GET_NAME (symp
));
2820 as_warn (_(".size expression for %s "
2821 "does not evaluate to a constant"), S_GET_NAME (symp
));
2823 free (sy_obj
->size
);
2824 sy_obj
->size
= NULL
;
2827 versioned_name
= sy_obj
->versioned_name
;
2830 /* This symbol was given a new name with the .symver directive.
2831 If this is an external reference, just rename the symbol to
2832 include the version string. This will make the relocs be
2833 against the correct versioned symbol. */
2835 /* We will have already reported an version error. */
2836 if (sy_obj
->bad_version
)
2838 /* elf_frob_file_before_adjust only allows one version symbol for
2840 else if (sy_obj
->rename
)
2841 S_SET_NAME (symp
, versioned_name
->name
);
2842 else if (S_IS_COMMON (symp
))
2844 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
2845 versioned_name
->name
, S_GET_NAME (symp
));
2851 elf_symbol_type
*elfsym
;
2853 /* This is a definition. Add an alias for each version.
2854 FIXME: Using an alias will permit the debugging information
2855 to refer to the right symbol. However, it's not clear
2856 whether it is the best approach. */
2858 /* FIXME: Creating a new symbol here is risky. We're
2859 in the final loop over the symbol table. We can
2860 get away with it only because the symbol goes to
2861 the end of the list, where the loop will still see
2862 it. It would probably be better to do this in
2863 obj_frob_file_before_adjust. */
2864 for (; versioned_name
!= NULL
;
2865 versioned_name
= versioned_name
->next
)
2867 symbolS
*symp2
= symbol_find_or_make (versioned_name
->name
);
2869 S_SET_SEGMENT (symp2
, S_GET_SEGMENT (symp
));
2871 /* Subtracting out the frag address here is a hack
2872 because we are in the middle of the final loop. */
2875 - (symbol_get_frag (symp
)->fr_address
2876 / OCTETS_PER_BYTE
)));
2878 symbol_set_frag (symp2
, symbol_get_frag (symp
));
2880 /* This will copy over the size information. */
2881 copy_symbol_attributes (symp2
, symp
);
2883 S_SET_OTHER (symp2
, S_GET_OTHER (symp
));
2885 if (S_IS_WEAK (symp
))
2888 if (S_IS_EXTERNAL (symp
))
2889 S_SET_EXTERNAL (symp2
);
2892 switch (sy_obj
->visibility
)
2894 case visibility_unchanged
:
2896 case visibility_hidden
:
2897 bfdsym
= symbol_get_bfdsym (symp
);
2898 elfsym
= elf_symbol_from (bfdsym
);
2899 elfsym
->internal_elf_sym
.st_other
&= ~3;
2900 elfsym
->internal_elf_sym
.st_other
|= STV_HIDDEN
;
2902 case visibility_remove
:
2903 /* Don't remove the symbol if it is used in relocation.
2904 Instead, mark it as to be removed and issue an error
2905 if the symbol has more than one versioned name. */
2906 if (symbol_used_in_reloc_p (symp
))
2908 if (sy_obj
->versioned_name
->next
!= NULL
)
2909 as_bad (_("symbol '%s' with multiple versions cannot be used in relocation"),
2911 symbol_mark_removed (symp
);
2914 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2916 case visibility_local
:
2917 S_CLEAR_EXTERNAL (symp
);
2923 /* Double check weak symbols. */
2924 if (S_IS_WEAK (symp
))
2926 if (S_IS_COMMON (symp
))
2927 as_bad (_("symbol `%s' can not be both weak and common"),
2932 /* Fix up SYMPP which has been marked to be removed by .symver. */
2935 elf_fixup_removed_symbol (symbolS
**sympp
)
2937 symbolS
*symp
= *sympp
;
2938 struct elf_obj_sy
*sy_obj
= symbol_get_obj (symp
);
2940 /* Replace the removed symbol with the versioned symbol. */
2941 symp
= symbol_find (sy_obj
->versioned_name
->name
);
2945 /* Create symbols for group signature. */
2948 elf_adjust_symtab (void)
2952 /* Make the SHT_GROUP sections that describe each section group. We
2953 can't set up the section contents here yet, because elf section
2954 indices have yet to be calculated. elf.c:set_group_contents does
2955 the rest of the work. */
2956 for (i
= 0; i
< groups
.num_group
; i
++)
2958 const char *group_name
= elf_group_name (groups
.head
[i
]);
2959 const char *sec_name
;
2964 flags
= SEC_READONLY
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
| SEC_GROUP
;
2965 for (s
= groups
.head
[i
]; s
!= NULL
; s
= elf_next_in_group (s
))
2966 if ((s
->flags
^ flags
) & SEC_LINK_ONCE
)
2968 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2969 if (s
!= groups
.head
[i
])
2971 as_warn (_("assuming all members of group `%s' are COMDAT"),
2977 sec_name
= ".group";
2978 s
= subseg_force_new (sec_name
, 0);
2980 || !bfd_set_section_flags (s
, flags
)
2981 || !bfd_set_section_alignment (s
, 2))
2983 as_fatal (_("can't create group: %s"),
2984 bfd_errmsg (bfd_get_error ()));
2986 elf_section_type (s
) = SHT_GROUP
;
2988 /* Pass a pointer to the first section in this group. */
2989 elf_next_in_group (s
) = groups
.head
[i
];
2990 elf_sec_group (groups
.head
[i
]) = s
;
2991 /* Make sure that the signature symbol for the group has the
2992 name of the group. */
2993 sy
= symbol_find_exact (group_name
);
2994 if (!sy
|| !symbol_on_chain (sy
, symbol_rootP
, symbol_lastP
))
2996 /* Create the symbol now. */
2997 sy
= symbol_new (group_name
, now_seg
, frag_now
, 0);
2999 /* Before Solaris 11 build 154, Sun ld rejects local group
3000 signature symbols, so make them weak hidden instead. */
3001 symbol_get_bfdsym (sy
)->flags
|= BSF_WEAK
;
3002 S_SET_OTHER (sy
, STV_HIDDEN
);
3004 symbol_get_obj (sy
)->local
= 1;
3006 symbol_table_insert (sy
);
3008 elf_group_id (s
) = symbol_get_bfdsym (sy
);
3009 /* Mark the group signature symbol as used so that it will be
3010 included in the symbol table. */
3011 symbol_mark_used_in_reloc (sy
);
3016 elf_frob_file (void)
3018 bfd_map_over_sections (stdoutput
, set_additional_section_info
, NULL
);
3020 #ifdef elf_tc_final_processing
3021 elf_tc_final_processing ();
3025 /* It removes any unneeded versioned symbols from the symbol table. */
3028 elf_frob_file_before_adjust (void)
3034 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
3036 struct elf_obj_sy
*sy_obj
= symbol_get_obj (symp
);
3037 int is_defined
= !!S_IS_DEFINED (symp
);
3039 if (sy_obj
->versioned_name
)
3041 char *p
= strchr (sy_obj
->versioned_name
->name
,
3046 /* The @@@ syntax is a special case. If the symbol is
3047 not defined, 2 `@'s will be removed from the
3048 versioned_name. Otherwise, 1 `@' will be removed. */
3049 size_t l
= strlen (&p
[3]) + 1;
3050 memmove (&p
[1 + is_defined
], &p
[3], l
);
3055 /* Verify that the name isn't using the @@ syntax--this
3056 is reserved for definitions of the default version
3058 if (!sy_obj
->rename
&& p
[1] == ELF_VER_CHR
)
3060 as_bad (_("invalid attempt to declare external "
3061 "version name as default in symbol `%s'"),
3062 sy_obj
->versioned_name
->name
);
3066 /* Only one version symbol is allowed for undefined
3068 if (sy_obj
->versioned_name
->next
)
3070 as_bad (_("multiple versions [`%s'|`%s'] for "
3072 sy_obj
->versioned_name
->name
,
3073 sy_obj
->versioned_name
->next
->name
,
3078 sy_obj
->rename
= true;
3082 /* If there was .symver or .weak, but symbol was neither
3083 defined nor used anywhere, remove it. */
3085 && (sy_obj
->versioned_name
|| S_IS_WEAK (symp
))
3086 && symbol_used_p (symp
) == 0
3087 && symbol_used_in_reloc_p (symp
) == 0)
3088 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
3093 /* It is required that we let write_relocs have the opportunity to
3094 optimize away fixups before output has begun, since it is possible
3095 to eliminate all fixups for a section and thus we never should
3096 have generated the relocation section. */
3099 elf_frob_file_after_relocs (void)
3103 /* Set SHT_GROUP section size. */
3104 for (i
= 0; i
< groups
.num_group
; i
++)
3106 asection
*s
, *head
, *group
;
3109 head
= groups
.head
[i
];
3111 for (s
= head
; s
!= NULL
; s
= elf_next_in_group (s
))
3112 size
+= (s
->flags
& SEC_RELOC
) != 0 ? 8 : 4;
3114 group
= elf_sec_group (head
);
3115 subseg_set (group
, 0);
3116 bfd_set_section_size (group
, size
);
3117 group
->contents
= (unsigned char *) frag_more (size
);
3118 frag_now
->fr_fix
= frag_now_fix_octets ();
3119 frag_wane (frag_now
);
3122 #ifdef NEED_ECOFF_DEBUG
3123 if (ECOFF_DEBUGGING
)
3124 /* Generate the ECOFF debugging information. */
3126 const struct ecoff_debug_swap
*debug_swap
;
3127 struct ecoff_debug_info debug
;
3132 = get_elf_backend_data (stdoutput
)->elf_backend_ecoff_debug_swap
;
3133 know (debug_swap
!= NULL
);
3134 ecoff_build_debug (&debug
.symbolic_header
, &buf
, debug_swap
);
3136 /* Set up the pointers in debug. */
3137 debug
.alloc_syments
= true;
3138 #define SET(ptr, offset, type) \
3139 debug.ptr = (type) (buf + debug.symbolic_header.offset)
3141 SET (line
, cbLineOffset
, unsigned char *);
3142 SET (external_dnr
, cbDnOffset
, void *);
3143 SET (external_pdr
, cbPdOffset
, void *);
3144 SET (external_sym
, cbSymOffset
, void *);
3145 SET (external_opt
, cbOptOffset
, void *);
3146 SET (external_aux
, cbAuxOffset
, union aux_ext
*);
3147 SET (ss
, cbSsOffset
, char *);
3148 SET (external_fdr
, cbFdOffset
, void *);
3149 SET (external_rfd
, cbRfdOffset
, void *);
3150 /* ssext and external_ext are set up just below. */
3154 /* Set up the external symbols. */
3155 debug
.ssext
= debug
.ssext_end
= NULL
;
3156 debug
.external_ext
= debug
.external_ext_end
= NULL
;
3157 if (! bfd_ecoff_debug_externals (stdoutput
, &debug
, debug_swap
, true,
3158 elf_get_extr
, elf_set_index
))
3159 as_fatal (_("failed to set up debugging information: %s"),
3160 bfd_errmsg (bfd_get_error ()));
3162 sec
= bfd_get_section_by_name (stdoutput
, ".mdebug");
3163 gas_assert (sec
!= NULL
);
3165 know (!stdoutput
->output_has_begun
);
3167 /* We set the size of the section, call bfd_set_section_contents
3168 to force the ELF backend to allocate a file position, and then
3169 write out the data. FIXME: Is this really the best way to do
3171 bfd_set_section_size (sec
, bfd_ecoff_debug_size (stdoutput
, &debug
,
3174 /* Pass BUF to bfd_set_section_contents because this will
3175 eventually become a call to fwrite, and ISO C prohibits
3176 passing a NULL pointer to a stdio function even if the
3177 pointer will not be used. */
3178 if (! bfd_set_section_contents (stdoutput
, sec
, buf
, 0, 0))
3179 as_fatal (_("can't start writing .mdebug section: %s"),
3180 bfd_errmsg (bfd_get_error ()));
3182 know (stdoutput
->output_has_begun
);
3183 know (sec
->filepos
!= 0);
3185 if (! bfd_ecoff_write_debug (stdoutput
, &debug
, debug_swap
,
3187 as_fatal (_("could not write .mdebug section: %s"),
3188 bfd_errmsg (bfd_get_error ()));
3190 #endif /* NEED_ECOFF_DEBUG */
3194 elf_generate_asm_lineno (void)
3196 #ifdef NEED_ECOFF_DEBUG
3197 if (ECOFF_DEBUGGING
)
3198 ecoff_generate_asm_lineno ();
3203 elf_process_stab (int what ATTRIBUTE_UNUSED
,
3204 const char *string ATTRIBUTE_UNUSED
,
3205 int type ATTRIBUTE_UNUSED
,
3206 int other ATTRIBUTE_UNUSED
,
3207 int desc ATTRIBUTE_UNUSED
)
3209 #ifdef NEED_ECOFF_DEBUG
3210 if (ECOFF_DEBUGGING
)
3211 ecoff_stab (what
, string
, type
, other
, desc
);
3216 elf_separate_stab_sections (void)
3218 #ifdef NEED_ECOFF_DEBUG
3219 return (!ECOFF_DEBUGGING
);
3226 elf_init_stab_section (segT stab
, segT stabstr
)
3228 #ifdef NEED_ECOFF_DEBUG
3229 if (!ECOFF_DEBUGGING
)
3231 obj_elf_init_stab_section (stab
, stabstr
);
3234 /* This is called when the assembler starts. */
3241 /* Add symbols for the known sections to the symbol table. */
3242 s
= bfd_get_section_by_name (stdoutput
, TEXT_SECTION_NAME
);
3243 symbol_table_insert (section_symbol (s
));
3244 s
= bfd_get_section_by_name (stdoutput
, DATA_SECTION_NAME
);
3245 symbol_table_insert (section_symbol (s
));
3246 s
= bfd_get_section_by_name (stdoutput
, BSS_SECTION_NAME
);
3247 symbol_table_insert (section_symbol (s
));
3248 elf_com_section_ptr
= bfd_com_section_ptr
;
3249 previous_section
= NULL
;
3250 previous_subsection
= 0;
3251 comment_section
= NULL
;
3252 memset (&groups
, 0, sizeof (groups
));
3258 while (section_stack
)
3260 struct section_stack
*top
= section_stack
;
3261 section_stack
= top
->next
;
3264 while (recorded_attributes
)
3266 struct recorded_attribute_info
*rai
= recorded_attributes
;
3267 recorded_attributes
= rai
->next
;
3272 htab_delete (groups
.indexes
);
3277 const struct format_ops elf_format_ops
=
3279 bfd_target_elf_flavour
,
3280 0, /* dfl_leading_underscore */
3281 1, /* emit_section_symbols */
3287 elf_frob_file_before_adjust
,
3288 0, /* obj_frob_file_before_fix */
3289 elf_frob_file_after_relocs
,
3290 elf_s_get_size
, elf_s_set_size
,
3291 elf_s_get_align
, elf_s_set_align
,
3298 elf_copy_symbol_attributes
,
3299 elf_generate_asm_lineno
,
3301 elf_separate_stab_sections
,
3302 elf_init_stab_section
,
3303 elf_sec_sym_ok_for_reloc
,
3305 #ifdef NEED_ECOFF_DEBUG
3308 0, /* ecoff_set_ext */
3310 elf_obj_read_begin_hook
,
3311 elf_obj_symbol_new_hook
,
3312 elf_obj_symbol_clone_hook
,