1 /* ELF object file format
2 Copyright (C) 1992-2018 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 "struc-symbol.h"
27 #include "dwarf2dbg.h"
29 #ifndef ECOFF_DEBUGGING
30 #define ECOFF_DEBUGGING 0
32 #define NEED_ECOFF_DEBUG
35 #ifdef NEED_ECOFF_DEBUG
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);
82 static const pseudo_typeS elf_pseudo_table
[] =
84 {"comm", obj_elf_common
, 0},
85 {"common", obj_elf_common
, 1},
86 {"ident", obj_elf_ident
, 0},
87 {"lcomm", obj_elf_lcomm
, 0},
88 {"local", obj_elf_local
, 0},
89 {"previous", obj_elf_previous
, 0},
90 {"section", obj_elf_section
, 0},
91 {"section.s", obj_elf_section
, 0},
92 {"sect", obj_elf_section
, 0},
93 {"sect.s", obj_elf_section
, 0},
94 {"pushsection", obj_elf_section
, 1},
95 {"popsection", obj_elf_popsection
, 0},
96 {"size", obj_elf_size
, 0},
97 {"type", obj_elf_type
, 0},
98 {"version", obj_elf_version
, 0},
99 {"weak", obj_elf_weak
, 0},
101 /* These define symbol visibility. */
102 {"internal", obj_elf_visibility
, STV_INTERNAL
},
103 {"hidden", obj_elf_visibility
, STV_HIDDEN
},
104 {"protected", obj_elf_visibility
, STV_PROTECTED
},
106 /* These are used for stabs-in-elf configurations. */
107 {"line", obj_elf_line
, 0},
109 /* This is a GNU extension to handle symbol versions. */
110 {"symver", obj_elf_symver
, 0},
112 /* A GNU extension to change subsection only. */
113 {"subsection", obj_elf_subsection
, 0},
115 /* These are GNU extensions to aid in garbage collecting C++ vtables. */
116 {"vtable_inherit", obj_elf_vtable_inherit
, 0},
117 {"vtable_entry", obj_elf_vtable_entry
, 0},
119 /* A GNU extension for object attributes. */
120 {"gnu_attribute", obj_elf_gnu_attribute
, 0},
122 /* These are used for dwarf. */
126 /* These are used for dwarf2. */
127 { "file", dwarf2_directive_file
, 0 },
128 { "loc", dwarf2_directive_loc
, 0 },
129 { "loc_mark_labels", dwarf2_directive_loc_mark_labels
, 0 },
131 /* We need to trap the section changing calls to handle .previous. */
132 {"data", obj_elf_data
, 0},
133 {"offset", obj_elf_struct
, 0},
134 {"struct", obj_elf_struct
, 0},
135 {"text", obj_elf_text
, 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 /* This is called when the assembler starts. */
194 asection
*elf_com_section_ptr
;
201 /* Add symbols for the known sections to the symbol table. */
202 s
= bfd_get_section_by_name (stdoutput
, TEXT_SECTION_NAME
);
203 symbol_table_insert (section_symbol (s
));
204 s
= bfd_get_section_by_name (stdoutput
, DATA_SECTION_NAME
);
205 symbol_table_insert (section_symbol (s
));
206 s
= bfd_get_section_by_name (stdoutput
, BSS_SECTION_NAME
);
207 symbol_table_insert (section_symbol (s
));
208 elf_com_section_ptr
= bfd_com_section_ptr
;
212 elf_pop_insert (void)
214 pop_insert (elf_pseudo_table
);
216 pop_insert (ecoff_debug_pseudo_table
);
220 elf_s_get_size (symbolS
*sym
)
222 return S_GET_SIZE (sym
);
226 elf_s_set_size (symbolS
*sym
, bfd_vma sz
)
228 S_SET_SIZE (sym
, sz
);
232 elf_s_get_align (symbolS
*sym
)
234 return S_GET_ALIGN (sym
);
238 elf_s_set_align (symbolS
*sym
, bfd_vma align
)
240 S_SET_ALIGN (sym
, align
);
244 elf_s_get_other (symbolS
*sym
)
246 return elf_symbol (symbol_get_bfdsym (sym
))->internal_elf_sym
.st_other
;
250 elf_s_set_other (symbolS
*sym
, int other
)
252 S_SET_OTHER (sym
, other
);
256 elf_sec_sym_ok_for_reloc (asection
*sec
)
258 return obj_sec_sym_ok_for_reloc (sec
);
262 elf_file_symbol (const char *s
, int appfile
)
265 || symbol_rootP
== NULL
266 || symbol_rootP
->bsym
== NULL
267 || (symbol_rootP
->bsym
->flags
& BSF_FILE
) == 0)
272 sym
= symbol_new (s
, absolute_section
, 0, NULL
);
273 symbol_set_frag (sym
, &zero_address_frag
);
275 name_length
= strlen (s
);
276 if (name_length
> strlen (S_GET_NAME (sym
)))
278 obstack_grow (¬es
, s
, name_length
+ 1);
279 S_SET_NAME (sym
, (const char *) obstack_finish (¬es
));
282 strcpy ((char *) S_GET_NAME (sym
), s
);
284 symbol_get_bfdsym (sym
)->flags
|= BSF_FILE
;
286 if (symbol_rootP
!= sym
287 && (symbol_rootP
->bsym
== NULL
288 || !(symbol_rootP
->bsym
->flags
& BSF_FILE
)))
290 symbol_remove (sym
, &symbol_rootP
, &symbol_lastP
);
291 symbol_insert (sym
, symbol_rootP
, &symbol_rootP
, &symbol_lastP
);
295 verify_symbol_chain (symbol_rootP
, symbol_lastP
);
299 #ifdef NEED_ECOFF_DEBUG
300 ecoff_new_file (s
, appfile
);
304 /* Called from read.c:s_comm after we've parsed .comm symbol, size.
305 Parse a possible alignment value. */
308 elf_common_parse (int ignore ATTRIBUTE_UNUSED
, symbolS
*symbolP
, addressT size
)
311 int is_local
= symbol_get_obj (symbolP
)->local
;
313 if (*input_line_pointer
== ',')
315 char *save
= input_line_pointer
;
317 input_line_pointer
++;
320 if (*input_line_pointer
== '"')
322 /* For sparc. Accept .common symbol, length, "bss" */
323 input_line_pointer
++;
324 /* Some use the dot, some don't. */
325 if (*input_line_pointer
== '.')
326 input_line_pointer
++;
327 /* Some say data, some say bss. */
328 if (strncmp (input_line_pointer
, "bss\"", 4) == 0)
329 input_line_pointer
+= 4;
330 else if (strncmp (input_line_pointer
, "data\"", 5) == 0)
331 input_line_pointer
+= 5;
334 char *p
= input_line_pointer
;
339 while (!is_end_of_line
[(unsigned char) *input_line_pointer
])
340 if (*input_line_pointer
++ == '"')
342 c
= *input_line_pointer
;
343 *input_line_pointer
= '\0';
344 as_bad (_("bad .common segment %s"), p
);
345 *input_line_pointer
= c
;
346 ignore_rest_of_line ();
349 /* ??? Don't ask me why these are always global. */
354 input_line_pointer
= save
;
355 align
= parse_align (is_local
);
356 if (align
== (addressT
) -1)
363 bss_alloc (symbolP
, size
, align
);
364 S_CLEAR_EXTERNAL (symbolP
);
368 S_SET_VALUE (symbolP
, size
);
369 S_SET_ALIGN (symbolP
, align
);
370 S_SET_EXTERNAL (symbolP
);
371 S_SET_SEGMENT (symbolP
, elf_com_section_ptr
);
374 symbol_get_bfdsym (symbolP
)->flags
|= BSF_OBJECT
;
380 obj_elf_common (int is_common
)
382 if (flag_mri
&& is_common
)
385 s_comm_internal (0, elf_common_parse
);
389 obj_elf_tls_common (int ignore ATTRIBUTE_UNUSED
)
391 symbolS
*symbolP
= s_comm_internal (0, elf_common_parse
);
394 symbol_get_bfdsym (symbolP
)->flags
|= BSF_THREAD_LOCAL
;
398 obj_elf_lcomm (int ignore ATTRIBUTE_UNUSED
)
400 symbolS
*symbolP
= s_comm_internal (0, s_lcomm_internal
);
403 symbol_get_bfdsym (symbolP
)->flags
|= BSF_OBJECT
;
407 get_sym_from_input_line_and_check (void)
413 c
= get_symbol_name (& name
);
414 sym
= symbol_find_or_make (name
);
415 *input_line_pointer
= c
;
416 SKIP_WHITESPACE_AFTER_NAME ();
418 /* There is no symbol name if input_line_pointer has not moved. */
419 if (name
== input_line_pointer
)
420 as_bad (_("Missing symbol name in directive"));
425 obj_elf_local (int ignore ATTRIBUTE_UNUSED
)
432 symbolP
= get_sym_from_input_line_and_check ();
433 c
= *input_line_pointer
;
434 S_CLEAR_EXTERNAL (symbolP
);
435 symbol_get_obj (symbolP
)->local
= 1;
438 input_line_pointer
++;
440 if (*input_line_pointer
== '\n')
445 demand_empty_rest_of_line ();
449 obj_elf_weak (int ignore ATTRIBUTE_UNUSED
)
456 symbolP
= get_sym_from_input_line_and_check ();
457 c
= *input_line_pointer
;
458 S_SET_WEAK (symbolP
);
461 input_line_pointer
++;
463 if (*input_line_pointer
== '\n')
468 demand_empty_rest_of_line ();
472 obj_elf_visibility (int visibility
)
477 elf_symbol_type
*elfsym
;
481 symbolP
= get_sym_from_input_line_and_check ();
483 bfdsym
= symbol_get_bfdsym (symbolP
);
484 elfsym
= elf_symbol_from (bfd_asymbol_bfd (bfdsym
), bfdsym
);
488 elfsym
->internal_elf_sym
.st_other
&= ~3;
489 elfsym
->internal_elf_sym
.st_other
|= visibility
;
491 c
= *input_line_pointer
;
494 input_line_pointer
++;
498 if (*input_line_pointer
== '\n')
504 demand_empty_rest_of_line ();
507 static segT previous_section
;
508 static int previous_subsection
;
512 struct section_stack
*next
;
514 int subseg
, prev_subseg
;
517 static struct section_stack
*section_stack
;
519 /* Match both section group name and the sh_info field. */
522 const char *group_name
;
527 get_section (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *inf
)
529 struct section_match
*match
= (struct section_match
*) inf
;
530 const char *gname
= match
->group_name
;
531 const char *group_name
= elf_group_name (sec
);
532 unsigned int info
= elf_section_data (sec
)->this_hdr
.sh_info
;
534 return (info
== match
->info
535 && (group_name
== gname
536 || (group_name
!= NULL
538 && strcmp (group_name
, gname
) == 0)));
541 /* Handle the .section pseudo-op. This code supports two different
544 The first is found on Solaris, and looks like
545 .section ".sec1",#alloc,#execinstr,#write
546 Here the names after '#' are the SHF_* flags to turn on for the
547 section. I'm not sure how it determines the SHT_* type (BFD
548 doesn't really give us control over the type, anyhow).
550 The second format is found on UnixWare, and probably most SVR4
551 machines, and looks like
552 .section .sec1,"a",@progbits
553 The quoted string may contain any combination of a, w, x, and
554 represents the SHF_* flags to turn on for the section. The string
555 beginning with '@' can be progbits or nobits. There should be
556 other possibilities, but I don't know what they are. In any case,
557 BFD doesn't really let us set the section type. */
560 obj_elf_change_section (const char *name
,
565 const char *group_name
,
572 const struct elf_backend_data
*bed
;
573 const struct bfd_elf_special_section
*ssect
;
574 struct section_match match
;
576 #ifdef md_flush_pending_output
577 md_flush_pending_output ();
580 /* Switch to the section, creating it if necessary. */
583 struct section_stack
*elt
;
584 elt
= XNEW (struct section_stack
);
585 elt
->next
= section_stack
;
587 elt
->prev_seg
= previous_section
;
588 elt
->subseg
= now_subseg
;
589 elt
->prev_subseg
= previous_subsection
;
592 previous_section
= now_seg
;
593 previous_subsection
= now_subseg
;
595 match
.group_name
= group_name
;
597 old_sec
= bfd_get_section_by_name_if (stdoutput
, name
, get_section
,
605 sec
= subseg_force_new (name
, 0);
607 bed
= get_elf_backend_data (stdoutput
);
608 ssect
= (*bed
->get_sec_type_attr
) (stdoutput
, sec
);
612 bfd_boolean override
= FALSE
;
614 if (type
== SHT_NULL
)
616 else if (type
!= ssect
->type
)
619 /* Some older versions of gcc will emit
621 .section .init_array,"aw",@progbits
623 for __attribute__ ((section (".init_array"))).
624 "@progbits" is incorrect. Also for x86-64 large bss
625 sections, some older versions of gcc will emit
627 .section .lbss,"aw",@progbits
629 "@progbits" is incorrect. */
631 && (bed
->s
->arch_size
!= 64
632 || !(ssect
->attr
& SHF_X86_64_LARGE
))
634 && ssect
->type
!= SHT_INIT_ARRAY
635 && ssect
->type
!= SHT_FINI_ARRAY
636 && ssect
->type
!= SHT_PREINIT_ARRAY
)
638 /* We allow to specify any type for a .note section. */
639 if (ssect
->type
!= SHT_NOTE
640 /* Processor and application defined types are allowed too. */
641 && type
< SHT_LOPROC
)
642 as_warn (_("setting incorrect section type for %s"),
647 as_warn (_("ignoring incorrect section type for %s"),
653 if (old_sec
== NULL
&& ((attr
& ~(SHF_MASKOS
| SHF_MASKPROC
))
654 & ~ssect
->attr
) != 0)
656 /* As a GNU extension, we permit a .note section to be
657 allocatable. If the linker sees an allocatable .note
658 section, it will create a PT_NOTE segment in the output
659 file. We also allow "x" for .note.GNU-stack. */
660 if (ssect
->type
== SHT_NOTE
661 && (attr
== SHF_ALLOC
|| attr
== SHF_EXECINSTR
))
663 /* Allow different SHF_MERGE and SHF_STRINGS if we have
664 something like .rodata.str. */
665 else if (ssect
->suffix_length
== -2
666 && name
[ssect
->prefix_length
] == '.'
670 & ~SHF_STRINGS
) == 0)
672 /* .interp, .strtab and .symtab can have SHF_ALLOC. */
673 else if (attr
== SHF_ALLOC
674 && (strcmp (name
, ".interp") == 0
675 || strcmp (name
, ".strtab") == 0
676 || strcmp (name
, ".symtab") == 0))
678 /* .note.GNU-stack can have SHF_EXECINSTR. */
679 else if (attr
== SHF_EXECINSTR
680 && strcmp (name
, ".note.GNU-stack") == 0)
683 /* A section on Alpha may have SHF_ALPHA_GPREL. */
684 else if ((attr
& ~ssect
->attr
) == SHF_ALPHA_GPREL
)
688 else if (attr
== (SHF_EXECINSTR
| SHF_WRITE
| SHF_ALLOC
)
689 && (ssect
->type
== SHT_INIT_ARRAY
690 || ssect
->type
== SHT_FINI_ARRAY
691 || ssect
->type
== SHT_PREINIT_ARRAY
))
692 /* RX init/fini arrays can and should have the "awx" attributes set. */
697 if (group_name
== NULL
)
698 as_warn (_("setting incorrect section attributes for %s"),
704 if (!override
&& old_sec
== NULL
)
708 if ((attr
& (SHF_ALLOC
| SHF_GNU_MBIND
)) == SHF_GNU_MBIND
)
709 as_fatal (_("SHF_ALLOC isn't set for GNU_MBIND section: %s"), name
);
711 /* Convert ELF type and flags to BFD flags. */
713 | ((attr
& SHF_WRITE
) ? 0 : SEC_READONLY
)
714 | ((attr
& SHF_ALLOC
) ? SEC_ALLOC
: 0)
715 | (((attr
& SHF_ALLOC
) && type
!= SHT_NOBITS
) ? SEC_LOAD
: 0)
716 | ((attr
& SHF_EXECINSTR
) ? SEC_CODE
: 0)
717 | ((attr
& SHF_MERGE
) ? SEC_MERGE
: 0)
718 | ((attr
& SHF_STRINGS
) ? SEC_STRINGS
: 0)
719 | ((attr
& SHF_EXCLUDE
) ? SEC_EXCLUDE
: 0)
720 | ((attr
& SHF_TLS
) ? SEC_THREAD_LOCAL
: 0));
721 #ifdef md_elf_section_flags
722 flags
= md_elf_section_flags (flags
, attr
, type
);
726 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
732 if (type
== SHT_NULL
)
733 type
= bfd_elf_get_default_section_type (flags
);
734 elf_section_type (sec
) = type
;
735 elf_section_flags (sec
) = attr
;
736 elf_section_data (sec
)->this_hdr
.sh_info
= info
;
738 /* Prevent SEC_HAS_CONTENTS from being inadvertently set. */
739 if (type
== SHT_NOBITS
)
740 seg_info (sec
)->bss
= 1;
742 bfd_set_section_flags (stdoutput
, sec
, flags
);
743 if (flags
& SEC_MERGE
)
744 sec
->entsize
= entsize
;
745 elf_group_name (sec
) = group_name
;
747 /* Add a symbol for this section to the symbol table. */
748 secsym
= symbol_find (name
);
750 symbol_set_bfdsym (secsym
, sec
->symbol
);
752 symbol_table_insert (section_symbol (sec
));
757 && (unsigned) type
!= elf_section_type (old_sec
))
758 as_warn (_("ignoring changed section type for %s"), name
);
762 /* If section attributes are specified the second time we see a
763 particular section, then check that they are the same as we
764 saw the first time. */
765 if (((old_sec
->flags
^ flags
)
766 & (SEC_ALLOC
| SEC_LOAD
| SEC_READONLY
| SEC_CODE
767 | SEC_EXCLUDE
| SEC_SORT_ENTRIES
| SEC_MERGE
| SEC_STRINGS
768 | SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
769 | SEC_THREAD_LOCAL
)))
770 as_warn (_("ignoring changed section attributes for %s"), name
);
772 /* FIXME: Maybe we should consider removing a previously set
773 processor or application specific attribute as suspicious ? */
774 elf_section_flags (sec
) = attr
;
776 if ((flags
& SEC_MERGE
) && old_sec
->entsize
!= (unsigned) entsize
)
777 as_warn (_("ignoring changed section entity size for %s"), name
);
781 #ifdef md_elf_section_change_hook
782 md_elf_section_change_hook ();
787 obj_elf_parse_section_letters (char *str
, size_t len
, bfd_boolean
*is_clone
)
806 attr
|= SHF_EXECINSTR
;
821 attr
|= SHF_GNU_MBIND
;
828 if (*(str
- 1) == 'a')
831 if (len
> 1 && str
[1] == 's')
841 const char *bad_msg
= _("unrecognized .section attribute:"
842 " want a,e,w,x,M,S,G,T or number");
843 #ifdef md_elf_section_letter
844 bfd_vma md_attr
= md_elf_section_letter (*str
, &bad_msg
);
845 if (md_attr
!= (bfd_vma
) -1)
853 attr
|= strtoul (str
, & end
, 0);
854 /* Update str and len, allowing for the fact that
855 we will execute str++ and len-- below. */
861 as_fatal ("%s", bad_msg
);
872 obj_elf_section_type (char *str
, size_t len
, bfd_boolean warn
)
874 if (len
== 8 && strncmp (str
, "progbits", 8) == 0)
876 if (len
== 6 && strncmp (str
, "nobits", 6) == 0)
878 if (len
== 4 && strncmp (str
, "note", 4) == 0)
880 if (len
== 10 && strncmp (str
, "init_array", 10) == 0)
881 return SHT_INIT_ARRAY
;
882 if (len
== 10 && strncmp (str
, "fini_array", 10) == 0)
883 return SHT_FINI_ARRAY
;
884 if (len
== 13 && strncmp (str
, "preinit_array", 13) == 0)
885 return SHT_PREINIT_ARRAY
;
887 #ifdef md_elf_section_type
889 int md_type
= md_elf_section_type (str
, len
);
898 int type
= strtoul (str
, & end
, 0);
900 if (warn
&& (size_t) (end
- str
) != len
)
901 as_warn (_("extraneous characters at end of numeric section type"));
907 as_warn (_("unrecognized section type"));
912 obj_elf_section_word (char *str
, size_t len
, int *type
)
916 if (len
== 5 && strncmp (str
, "write", 5) == 0)
918 if (len
== 5 && strncmp (str
, "alloc", 5) == 0)
920 if (len
== 9 && strncmp (str
, "execinstr", 9) == 0)
921 return SHF_EXECINSTR
;
922 if (len
== 7 && strncmp (str
, "exclude", 7) == 0)
924 if (len
== 3 && strncmp (str
, "tls", 3) == 0)
927 #ifdef md_elf_section_word
929 bfd_vma md_attr
= md_elf_section_word (str
, len
);
935 ret
= obj_elf_section_type (str
, len
, FALSE
);
939 as_warn (_("unrecognized section attribute"));
944 /* Get name of section. */
946 obj_elf_section_name (void)
951 if (*input_line_pointer
== '"')
955 name
= demand_copy_C_string (&dummy
);
958 ignore_rest_of_line ();
964 char *end
= input_line_pointer
;
966 while (0 == strchr ("\n\t,; ", *end
))
968 if (end
== input_line_pointer
)
970 as_bad (_("missing name"));
971 ignore_rest_of_line ();
975 name
= xmemdup0 (input_line_pointer
, end
- input_line_pointer
);
977 while (flag_sectname_subst
)
979 char *subst
= strchr (name
, '%');
980 if (subst
&& subst
[1] == 'S')
982 int oldlen
= strlen (name
);
983 int substlen
= strlen (now_seg
->name
);
984 int newlen
= oldlen
- 2 + substlen
;
985 char *newname
= XNEWVEC (char, newlen
+ 1);
986 int headlen
= subst
- name
;
987 memcpy (newname
, name
, headlen
);
988 strcpy (newname
+ headlen
, now_seg
->name
);
989 strcat (newname
+ headlen
, subst
+ 2);
997 #ifdef tc_canonicalize_section_name
998 name
= tc_canonicalize_section_name (name
);
1000 input_line_pointer
= end
;
1007 obj_elf_section (int push
)
1009 const char *name
, *group_name
;
1015 subsegT new_subsection
= -1;
1016 unsigned int info
= 0;
1022 #ifdef md_flush_pending_output
1023 md_flush_pending_output ();
1026 previous_section
= now_seg
;
1027 previous_subsection
= now_subseg
;
1029 s_mri_sect (&mri_type
);
1031 #ifdef md_elf_section_change_hook
1032 md_elf_section_change_hook ();
1038 name
= obj_elf_section_name ();
1047 if (*input_line_pointer
== ',')
1049 /* Skip the comma. */
1050 ++input_line_pointer
;
1053 if (push
&& ISDIGIT (*input_line_pointer
))
1055 /* .pushsection has an optional subsection. */
1056 new_subsection
= (subsegT
) get_absolute_expression ();
1060 /* Stop if we don't see a comma. */
1061 if (*input_line_pointer
!= ',')
1064 /* Skip the comma. */
1065 ++input_line_pointer
;
1069 if (*input_line_pointer
== '"')
1071 bfd_boolean is_clone
;
1073 beg
= demand_copy_C_string (&dummy
);
1076 ignore_rest_of_line ();
1079 attr
|= obj_elf_parse_section_letters (beg
, strlen (beg
), &is_clone
);
1082 if (*input_line_pointer
== ',')
1085 char *save
= input_line_pointer
;
1087 ++input_line_pointer
;
1089 c
= *input_line_pointer
;
1092 beg
= demand_copy_C_string (&dummy
);
1095 ignore_rest_of_line ();
1098 type
= obj_elf_section_type (beg
, strlen (beg
), TRUE
);
1100 else if (c
== '@' || c
== '%')
1102 ++input_line_pointer
;
1104 if (ISDIGIT (* input_line_pointer
))
1106 type
= strtoul (input_line_pointer
, & input_line_pointer
, 0);
1110 c
= get_symbol_name (& beg
);
1111 (void) restore_line_pointer (c
);
1112 type
= obj_elf_section_type (beg
, input_line_pointer
- beg
, TRUE
);
1116 input_line_pointer
= save
;
1120 if ((attr
& SHF_MERGE
) != 0 && *input_line_pointer
== ',')
1122 ++input_line_pointer
;
1124 entsize
= get_absolute_expression ();
1128 as_warn (_("invalid merge entity size"));
1133 else if ((attr
& SHF_MERGE
) != 0)
1135 as_warn (_("entity size for SHF_MERGE not specified"));
1139 if ((attr
& SHF_GROUP
) != 0 && is_clone
)
1141 as_warn (_("? section flag ignored with G present"));
1144 if ((attr
& SHF_GROUP
) != 0 && *input_line_pointer
== ',')
1146 ++input_line_pointer
;
1147 group_name
= obj_elf_section_name ();
1148 if (group_name
== NULL
)
1150 else if (*input_line_pointer
== ',')
1152 ++input_line_pointer
;
1154 if (strncmp (input_line_pointer
, "comdat", 6) == 0)
1156 input_line_pointer
+= 6;
1160 else if (strncmp (name
, ".gnu.linkonce", 13) == 0)
1163 else if ((attr
& SHF_GROUP
) != 0)
1165 as_warn (_("group name for SHF_GROUP not specified"));
1171 const char *now_group
= elf_group_name (now_seg
);
1172 if (now_group
!= NULL
)
1174 group_name
= xstrdup (now_group
);
1175 linkonce
= (now_seg
->flags
& SEC_LINK_ONCE
) != 0;
1179 if ((attr
& SHF_GNU_MBIND
) != 0 && *input_line_pointer
== ',')
1181 ++input_line_pointer
;
1183 if (ISDIGIT (* input_line_pointer
))
1185 char *t
= input_line_pointer
;
1186 info
= strtoul (input_line_pointer
,
1187 &input_line_pointer
, 0);
1188 if (info
== (unsigned int) -1)
1190 as_warn (_("unsupported mbind section info: %s"), t
);
1203 if (*input_line_pointer
!= '#')
1205 as_bad (_("character following name is not '#'"));
1206 ignore_rest_of_line ();
1209 ++input_line_pointer
;
1210 c
= get_symbol_name (& beg
);
1211 (void) restore_line_pointer (c
);
1213 attr
|= obj_elf_section_word (beg
, input_line_pointer
- beg
, & type
);
1217 while (*input_line_pointer
++ == ',');
1218 --input_line_pointer
;
1223 demand_empty_rest_of_line ();
1225 obj_elf_change_section (name
, type
, info
, attr
, entsize
, group_name
,
1228 if (push
&& new_subsection
!= -1)
1229 subseg_set (now_seg
, new_subsection
);
1232 /* Change to the .data section. */
1235 obj_elf_data (int i
)
1237 #ifdef md_flush_pending_output
1238 md_flush_pending_output ();
1241 previous_section
= now_seg
;
1242 previous_subsection
= now_subseg
;
1245 #ifdef md_elf_section_change_hook
1246 md_elf_section_change_hook ();
1250 /* Change to the .text section. */
1253 obj_elf_text (int i
)
1255 #ifdef md_flush_pending_output
1256 md_flush_pending_output ();
1259 previous_section
= now_seg
;
1260 previous_subsection
= now_subseg
;
1263 #ifdef md_elf_section_change_hook
1264 md_elf_section_change_hook ();
1268 /* Change to the *ABS* section. */
1271 obj_elf_struct (int i
)
1273 #ifdef md_flush_pending_output
1274 md_flush_pending_output ();
1277 previous_section
= now_seg
;
1278 previous_subsection
= now_subseg
;
1281 #ifdef md_elf_section_change_hook
1282 md_elf_section_change_hook ();
1287 obj_elf_subsection (int ignore ATTRIBUTE_UNUSED
)
1291 #ifdef md_flush_pending_output
1292 md_flush_pending_output ();
1295 previous_section
= now_seg
;
1296 previous_subsection
= now_subseg
;
1298 temp
= get_absolute_expression ();
1299 subseg_set (now_seg
, (subsegT
) temp
);
1300 demand_empty_rest_of_line ();
1302 #ifdef md_elf_section_change_hook
1303 md_elf_section_change_hook ();
1307 /* This can be called from the processor backends if they change
1311 obj_elf_section_change_hook (void)
1313 previous_section
= now_seg
;
1314 previous_subsection
= now_subseg
;
1318 obj_elf_previous (int ignore ATTRIBUTE_UNUSED
)
1323 if (previous_section
== 0)
1325 as_warn (_(".previous without corresponding .section; ignored"));
1329 #ifdef md_flush_pending_output
1330 md_flush_pending_output ();
1333 new_section
= previous_section
;
1334 new_subsection
= previous_subsection
;
1335 previous_section
= now_seg
;
1336 previous_subsection
= now_subseg
;
1337 subseg_set (new_section
, new_subsection
);
1339 #ifdef md_elf_section_change_hook
1340 md_elf_section_change_hook ();
1345 obj_elf_popsection (int xxx ATTRIBUTE_UNUSED
)
1347 struct section_stack
*top
= section_stack
;
1351 as_warn (_(".popsection without corresponding .pushsection; ignored"));
1355 #ifdef md_flush_pending_output
1356 md_flush_pending_output ();
1359 section_stack
= top
->next
;
1360 previous_section
= top
->prev_seg
;
1361 previous_subsection
= top
->prev_subseg
;
1362 subseg_set (top
->seg
, top
->subseg
);
1365 #ifdef md_elf_section_change_hook
1366 md_elf_section_change_hook ();
1371 obj_elf_line (int ignore ATTRIBUTE_UNUSED
)
1373 /* Assume delimiter is part of expression. BSD4.2 as fails with
1374 delightful bug, so we are not being incompatible here. */
1375 new_logical_line (NULL
, get_absolute_expression ());
1376 demand_empty_rest_of_line ();
1379 /* This handles the .symver pseudo-op, which is used to specify a
1380 symbol version. The syntax is ``.symver NAME,SYMVERNAME''.
1381 SYMVERNAME may contain ELF_VER_CHR ('@') characters. This
1382 pseudo-op causes the assembler to emit a symbol named SYMVERNAME
1383 with the same value as the symbol NAME. */
1386 obj_elf_symver (int ignore ATTRIBUTE_UNUSED
)
1393 sym
= get_sym_from_input_line_and_check ();
1395 if (*input_line_pointer
!= ',')
1397 as_bad (_("expected comma after name in .symver"));
1398 ignore_rest_of_line ();
1402 ++input_line_pointer
;
1405 /* Temporarily include '@' in symbol names. */
1406 old_lexat
= lex_type
[(unsigned char) '@'];
1407 lex_type
[(unsigned char) '@'] |= LEX_NAME
;
1408 c
= get_symbol_name (& name
);
1409 lex_type
[(unsigned char) '@'] = old_lexat
;
1411 if (S_IS_COMMON (sym
))
1413 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
1414 name
, S_GET_NAME (sym
));
1415 ignore_rest_of_line ();
1419 if (symbol_get_obj (sym
)->versioned_name
== NULL
)
1421 symbol_get_obj (sym
)->versioned_name
= xstrdup (name
);
1423 (void) restore_line_pointer (c
);
1425 if (strchr (symbol_get_obj (sym
)->versioned_name
,
1426 ELF_VER_CHR
) == NULL
)
1428 as_bad (_("missing version name in `%s' for symbol `%s'"),
1429 symbol_get_obj (sym
)->versioned_name
,
1431 ignore_rest_of_line ();
1437 if (strcmp (symbol_get_obj (sym
)->versioned_name
, name
))
1439 as_bad (_("multiple versions [`%s'|`%s'] for symbol `%s'"),
1440 name
, symbol_get_obj (sym
)->versioned_name
,
1442 ignore_rest_of_line ();
1446 (void) restore_line_pointer (c
);
1449 demand_empty_rest_of_line ();
1452 /* This handles the .vtable_inherit pseudo-op, which is used to indicate
1453 to the linker the hierarchy in which a particular table resides. The
1454 syntax is ".vtable_inherit CHILDNAME, PARENTNAME". */
1457 obj_elf_get_vtable_inherit (void)
1459 char *cname
, *pname
;
1460 symbolS
*csym
, *psym
;
1463 if (*input_line_pointer
== '#')
1464 ++input_line_pointer
;
1466 c
= get_symbol_name (& cname
);
1467 csym
= symbol_find (cname
);
1469 /* GCFIXME: should check that we don't have two .vtable_inherits for
1470 the same child symbol. Also, we can currently only do this if the
1471 child symbol is already exists and is placed in a fragment. */
1473 if (csym
== NULL
|| symbol_get_frag (csym
) == NULL
)
1475 as_bad (_("expected `%s' to have already been set for .vtable_inherit"),
1480 *input_line_pointer
= c
;
1482 SKIP_WHITESPACE_AFTER_NAME ();
1483 if (*input_line_pointer
!= ',')
1485 as_bad (_("expected comma after name in .vtable_inherit"));
1486 ignore_rest_of_line ();
1490 ++input_line_pointer
;
1493 if (*input_line_pointer
== '#')
1494 ++input_line_pointer
;
1496 if (input_line_pointer
[0] == '0'
1497 && (input_line_pointer
[1] == '\0'
1498 || ISSPACE (input_line_pointer
[1])))
1500 psym
= section_symbol (absolute_section
);
1501 ++input_line_pointer
;
1505 c
= get_symbol_name (& pname
);
1506 psym
= symbol_find_or_make (pname
);
1507 restore_line_pointer (c
);
1510 demand_empty_rest_of_line ();
1515 gas_assert (symbol_get_value_expression (csym
)->X_op
== O_constant
);
1516 return fix_new (symbol_get_frag (csym
),
1517 symbol_get_value_expression (csym
)->X_add_number
,
1518 0, psym
, 0, 0, BFD_RELOC_VTABLE_INHERIT
);
1521 /* This is a version of obj_elf_get_vtable_inherit() that is
1522 suitable for use in struct _pseudo_type tables. */
1525 obj_elf_vtable_inherit (int ignore ATTRIBUTE_UNUSED
)
1527 (void) obj_elf_get_vtable_inherit ();
1530 /* This handles the .vtable_entry pseudo-op, which is used to indicate
1531 to the linker that a vtable slot was used. The syntax is
1532 ".vtable_entry tablename, offset". */
1535 obj_elf_get_vtable_entry (void)
1540 if (*input_line_pointer
== '#')
1541 ++input_line_pointer
;
1543 sym
= get_sym_from_input_line_and_check ();
1544 if (*input_line_pointer
!= ',')
1546 as_bad (_("expected comma after name in .vtable_entry"));
1547 ignore_rest_of_line ();
1551 ++input_line_pointer
;
1552 if (*input_line_pointer
== '#')
1553 ++input_line_pointer
;
1555 offset
= get_absolute_expression ();
1557 demand_empty_rest_of_line ();
1559 return fix_new (frag_now
, frag_now_fix (), 0, sym
, offset
, 0,
1560 BFD_RELOC_VTABLE_ENTRY
);
1563 /* This is a version of obj_elf_get_vtable_entry() that is
1564 suitable for use in struct _pseudo_type tables. */
1567 obj_elf_vtable_entry (int ignore ATTRIBUTE_UNUSED
)
1569 (void) obj_elf_get_vtable_entry ();
1572 #define skip_whitespace(str) do { if (*(str) == ' ') ++(str); } while (0)
1575 skip_past_char (char ** str
, char c
)
1585 #define skip_past_comma(str) skip_past_char (str, ',')
1587 /* A list of attributes that have been explicitly set by the assembly code.
1588 VENDOR is the vendor id, BASE is the tag shifted right by the number
1589 of bits in MASK, and bit N of MASK is set if tag BASE+N has been set. */
1590 struct recorded_attribute_info
{
1591 struct recorded_attribute_info
*next
;
1596 static struct recorded_attribute_info
*recorded_attributes
;
1598 /* Record that we have seen an explicit specification of attribute TAG
1599 for vendor VENDOR. */
1602 record_attribute (int vendor
, unsigned int tag
)
1606 struct recorded_attribute_info
*rai
;
1608 base
= tag
/ (8 * sizeof (rai
->mask
));
1609 mask
= 1UL << (tag
% (8 * sizeof (rai
->mask
)));
1610 for (rai
= recorded_attributes
; rai
; rai
= rai
->next
)
1611 if (rai
->vendor
== vendor
&& rai
->base
== base
)
1617 rai
= XNEW (struct recorded_attribute_info
);
1618 rai
->next
= recorded_attributes
;
1619 rai
->vendor
= vendor
;
1622 recorded_attributes
= rai
;
1625 /* Return true if we have seen an explicit specification of attribute TAG
1626 for vendor VENDOR. */
1629 obj_elf_seen_attribute (int vendor
, unsigned int tag
)
1633 struct recorded_attribute_info
*rai
;
1635 base
= tag
/ (8 * sizeof (rai
->mask
));
1636 mask
= 1UL << (tag
% (8 * sizeof (rai
->mask
)));
1637 for (rai
= recorded_attributes
; rai
; rai
= rai
->next
)
1638 if (rai
->vendor
== vendor
&& rai
->base
== base
)
1639 return (rai
->mask
& mask
) != 0;
1643 /* Parse an attribute directive for VENDOR.
1644 Returns the attribute number read, or zero on error. */
1647 obj_elf_vendor_attribute (int vendor
)
1655 /* Read the first number or name. */
1656 skip_whitespace (input_line_pointer
);
1657 s
= input_line_pointer
;
1658 if (ISDIGIT (*input_line_pointer
))
1661 if (exp
.X_op
!= O_constant
)
1663 tag
= exp
.X_add_number
;
1669 /* A name may contain '_', but no other punctuation. */
1670 for (; ISALNUM (*input_line_pointer
) || *input_line_pointer
== '_';
1671 ++input_line_pointer
)
1676 name
= xstrndup (s
, i
);
1678 #ifndef CONVERT_SYMBOLIC_ATTRIBUTE
1679 #define CONVERT_SYMBOLIC_ATTRIBUTE(a) -1
1682 tag
= CONVERT_SYMBOLIC_ATTRIBUTE (name
);
1685 as_bad (_("Attribute name not recognised: %s"), name
);
1686 ignore_rest_of_line ();
1693 type
= _bfd_elf_obj_attrs_arg_type (stdoutput
, vendor
, tag
);
1695 if (skip_past_comma (&input_line_pointer
) == -1)
1700 if (exp
.X_op
!= O_constant
)
1702 as_bad (_("expected numeric constant"));
1703 ignore_rest_of_line ();
1706 i
= exp
.X_add_number
;
1709 && skip_past_comma (&input_line_pointer
) == -1)
1711 as_bad (_("expected comma"));
1712 ignore_rest_of_line ();
1719 skip_whitespace (input_line_pointer
);
1720 if (*input_line_pointer
!= '"')
1722 s
= demand_copy_C_string (&len
);
1725 record_attribute (vendor
, tag
);
1729 bfd_elf_add_obj_attr_int_string (stdoutput
, vendor
, tag
, i
, s
);
1732 bfd_elf_add_obj_attr_string (stdoutput
, vendor
, tag
, s
);
1735 bfd_elf_add_obj_attr_int (stdoutput
, vendor
, tag
, i
);
1741 demand_empty_rest_of_line ();
1744 as_bad (_("bad string constant"));
1745 ignore_rest_of_line ();
1748 as_bad (_("expected <tag> , <value>"));
1749 ignore_rest_of_line ();
1753 /* Parse a .gnu_attribute directive. */
1756 obj_elf_gnu_attribute (int ignored ATTRIBUTE_UNUSED
)
1758 obj_elf_vendor_attribute (OBJ_ATTR_GNU
);
1762 elf_obj_read_begin_hook (void)
1764 #ifdef NEED_ECOFF_DEBUG
1765 if (ECOFF_DEBUGGING
)
1766 ecoff_read_begin_hook ();
1771 elf_obj_symbol_new_hook (symbolS
*symbolP
)
1773 struct elf_obj_sy
*sy_obj
;
1775 sy_obj
= symbol_get_obj (symbolP
);
1776 sy_obj
->size
= NULL
;
1777 sy_obj
->versioned_name
= NULL
;
1779 #ifdef NEED_ECOFF_DEBUG
1780 if (ECOFF_DEBUGGING
)
1781 ecoff_symbol_new_hook (symbolP
);
1785 /* When setting one symbol equal to another, by default we probably
1786 want them to have the same "size", whatever it means in the current
1790 elf_copy_symbol_attributes (symbolS
*dest
, symbolS
*src
)
1792 struct elf_obj_sy
*srcelf
= symbol_get_obj (src
);
1793 struct elf_obj_sy
*destelf
= symbol_get_obj (dest
);
1796 if (destelf
->size
== NULL
)
1797 destelf
->size
= XNEW (expressionS
);
1798 *destelf
->size
= *srcelf
->size
;
1802 if (destelf
->size
!= NULL
)
1803 free (destelf
->size
);
1804 destelf
->size
= NULL
;
1806 S_SET_SIZE (dest
, S_GET_SIZE (src
));
1807 /* Don't copy visibility. */
1808 S_SET_OTHER (dest
, (ELF_ST_VISIBILITY (S_GET_OTHER (dest
))
1809 | (S_GET_OTHER (src
) & ~ELF_ST_VISIBILITY (-1))));
1813 obj_elf_version (int ignore ATTRIBUTE_UNUSED
)
1818 asection
*seg
= now_seg
;
1819 subsegT subseg
= now_subseg
;
1820 Elf_Internal_Note i_note
;
1821 Elf_External_Note e_note
;
1822 asection
*note_secp
= NULL
;
1825 if (*input_line_pointer
== '\"')
1829 ++input_line_pointer
; /* -> 1st char of string. */
1830 name
= input_line_pointer
;
1832 while (is_a_char (c
= next_char_of_string ()))
1834 c
= *input_line_pointer
;
1835 *input_line_pointer
= '\0';
1836 *(input_line_pointer
- 1) = '\0';
1837 *input_line_pointer
= c
;
1839 /* Create the .note section. */
1840 note_secp
= subseg_new (".note", 0);
1841 bfd_set_section_flags (stdoutput
,
1843 SEC_HAS_CONTENTS
| SEC_READONLY
);
1844 record_alignment (note_secp
, 2);
1846 /* Process the version string. */
1847 len
= strlen (name
) + 1;
1849 /* PR 3456: Although the name field is padded out to an 4-byte
1850 boundary, the namesz field should not be adjusted. */
1851 i_note
.namesz
= len
;
1852 i_note
.descsz
= 0; /* No description. */
1853 i_note
.type
= NT_VERSION
;
1854 p
= frag_more (sizeof (e_note
.namesz
));
1855 md_number_to_chars (p
, i_note
.namesz
, sizeof (e_note
.namesz
));
1856 p
= frag_more (sizeof (e_note
.descsz
));
1857 md_number_to_chars (p
, i_note
.descsz
, sizeof (e_note
.descsz
));
1858 p
= frag_more (sizeof (e_note
.type
));
1859 md_number_to_chars (p
, i_note
.type
, sizeof (e_note
.type
));
1860 p
= frag_more (len
);
1861 memcpy (p
, name
, len
);
1863 frag_align (2, 0, 0);
1865 subseg_set (seg
, subseg
);
1868 as_bad (_("expected quoted string"));
1870 demand_empty_rest_of_line ();
1874 obj_elf_size (int ignore ATTRIBUTE_UNUSED
)
1877 char c
= get_symbol_name (&name
);
1882 p
= input_line_pointer
;
1884 SKIP_WHITESPACE_AFTER_NAME ();
1885 if (*input_line_pointer
!= ',')
1888 as_bad (_("expected comma after name `%s' in .size directive"), name
);
1890 ignore_rest_of_line ();
1893 input_line_pointer
++;
1895 if (exp
.X_op
== O_absent
)
1897 as_bad (_("missing expression in .size directive"));
1898 exp
.X_op
= O_constant
;
1899 exp
.X_add_number
= 0;
1902 sym
= symbol_find_or_make (name
);
1904 if (exp
.X_op
== O_constant
)
1906 S_SET_SIZE (sym
, exp
.X_add_number
);
1907 if (symbol_get_obj (sym
)->size
)
1909 xfree (symbol_get_obj (sym
)->size
);
1910 symbol_get_obj (sym
)->size
= NULL
;
1915 symbol_get_obj (sym
)->size
= XNEW (expressionS
);
1916 *symbol_get_obj (sym
)->size
= exp
;
1918 demand_empty_rest_of_line ();
1921 /* Handle the ELF .type pseudo-op. This sets the type of a symbol.
1922 There are six syntaxes:
1924 The first (used on Solaris) is
1926 The second (used on UnixWare) is
1928 The third (reportedly to be used on Irix 6.0) is
1930 The fourth (used on NetBSD/Arm and Linux/ARM) is
1932 The fifth (used on SVR4/860) is
1933 .type SYM,"function"
1934 The sixth (emitted by recent SunPRO under Solaris) is
1936 where the integer is the STT_* value.
1940 obj_elf_type_name (char *cp
)
1944 p
= input_line_pointer
;
1945 if (*input_line_pointer
>= '0'
1946 && *input_line_pointer
<= '9')
1948 while (*input_line_pointer
>= '0'
1949 && *input_line_pointer
<= '9')
1950 ++input_line_pointer
;
1951 *cp
= *input_line_pointer
;
1952 *input_line_pointer
= '\0';
1955 *cp
= get_symbol_name (&p
);
1961 obj_elf_type (int ignore ATTRIBUTE_UNUSED
)
1965 const char *type_name
;
1967 elf_symbol_type
*elfsym
;
1969 sym
= get_sym_from_input_line_and_check ();
1970 c
= *input_line_pointer
;
1971 elfsym
= (elf_symbol_type
*) symbol_get_bfdsym (sym
);
1973 if (*input_line_pointer
== ',')
1974 ++input_line_pointer
;
1977 if ( *input_line_pointer
== '#'
1978 || *input_line_pointer
== '@'
1979 || *input_line_pointer
== '"'
1980 || *input_line_pointer
== '%')
1981 ++input_line_pointer
;
1983 type_name
= obj_elf_type_name (& c
);
1986 if (strcmp (type_name
, "function") == 0
1987 || strcmp (type_name
, "2") == 0
1988 || strcmp (type_name
, "STT_FUNC") == 0)
1989 type
= BSF_FUNCTION
;
1990 else if (strcmp (type_name
, "object") == 0
1991 || strcmp (type_name
, "1") == 0
1992 || strcmp (type_name
, "STT_OBJECT") == 0)
1994 else if (strcmp (type_name
, "tls_object") == 0
1995 || strcmp (type_name
, "6") == 0
1996 || strcmp (type_name
, "STT_TLS") == 0)
1997 type
= BSF_OBJECT
| BSF_THREAD_LOCAL
;
1998 else if (strcmp (type_name
, "notype") == 0
1999 || strcmp (type_name
, "0") == 0
2000 || strcmp (type_name
, "STT_NOTYPE") == 0)
2002 else if (strcmp (type_name
, "common") == 0
2003 || strcmp (type_name
, "5") == 0
2004 || strcmp (type_name
, "STT_COMMON") == 0)
2008 if (! S_IS_COMMON (sym
))
2010 if (S_IS_VOLATILE (sym
))
2012 sym
= symbol_clone (sym
, 1);
2013 S_SET_SEGMENT (sym
, bfd_com_section_ptr
);
2014 S_SET_VALUE (sym
, 0);
2015 S_SET_EXTERNAL (sym
);
2016 symbol_set_frag (sym
, &zero_address_frag
);
2017 S_CLEAR_VOLATILE (sym
);
2019 else if (S_IS_DEFINED (sym
) || symbol_equated_p (sym
))
2020 as_bad (_("symbol '%s' is already defined"), S_GET_NAME (sym
));
2023 /* FIXME: Is it safe to just change the section ? */
2024 S_SET_SEGMENT (sym
, bfd_com_section_ptr
);
2025 S_SET_VALUE (sym
, 0);
2026 S_SET_EXTERNAL (sym
);
2030 else if (strcmp (type_name
, "gnu_indirect_function") == 0
2031 || strcmp (type_name
, "10") == 0
2032 || strcmp (type_name
, "STT_GNU_IFUNC") == 0)
2034 const struct elf_backend_data
*bed
;
2036 bed
= get_elf_backend_data (stdoutput
);
2037 if (!(bed
->elf_osabi
== ELFOSABI_GNU
2038 || bed
->elf_osabi
== ELFOSABI_FREEBSD
2039 /* GNU is still using the default value 0. */
2040 || bed
->elf_osabi
== ELFOSABI_NONE
))
2041 as_bad (_("symbol type \"%s\" is supported only by GNU and FreeBSD targets"),
2043 type
= BSF_FUNCTION
| BSF_GNU_INDIRECT_FUNCTION
;
2045 else if (strcmp (type_name
, "gnu_unique_object") == 0)
2047 struct elf_backend_data
*bed
;
2049 bed
= (struct elf_backend_data
*) get_elf_backend_data (stdoutput
);
2050 if (!(bed
->elf_osabi
== ELFOSABI_GNU
2051 /* GNU is still using the default value 0. */
2052 || bed
->elf_osabi
== ELFOSABI_NONE
))
2053 as_bad (_("symbol type \"%s\" is supported only by GNU targets"),
2055 type
= BSF_OBJECT
| BSF_GNU_UNIQUE
;
2056 /* PR 10549: Always set OSABI field to GNU for objects containing unique symbols. */
2057 bed
->elf_osabi
= ELFOSABI_GNU
;
2059 #ifdef md_elf_symbol_type
2060 else if ((type
= md_elf_symbol_type (type_name
, sym
, elfsym
)) != -1)
2064 as_bad (_("unrecognized symbol type \"%s\""), type_name
);
2066 *input_line_pointer
= c
;
2068 if (*input_line_pointer
== '"')
2069 ++input_line_pointer
;
2071 elfsym
->symbol
.flags
|= type
;
2073 demand_empty_rest_of_line ();
2077 obj_elf_ident (int ignore ATTRIBUTE_UNUSED
)
2079 static segT comment_section
;
2080 segT old_section
= now_seg
;
2081 int old_subsection
= now_subseg
;
2083 #ifdef md_flush_pending_output
2084 md_flush_pending_output ();
2087 if (!comment_section
)
2090 comment_section
= subseg_new (".comment", 0);
2091 bfd_set_section_flags (stdoutput
, comment_section
,
2092 SEC_READONLY
| SEC_HAS_CONTENTS
2093 | SEC_MERGE
| SEC_STRINGS
);
2094 comment_section
->entsize
= 1;
2095 #ifdef md_elf_section_change_hook
2096 md_elf_section_change_hook ();
2102 subseg_set (comment_section
, 0);
2104 subseg_set (old_section
, old_subsection
);
2107 #ifdef INIT_STAB_SECTION
2109 /* The first entry in a .stabs section is special. */
2112 obj_elf_init_stab_section (segT seg
)
2117 unsigned int stroff
;
2119 /* Force the section to align to a longword boundary. Without this,
2120 UnixWare ar crashes. */
2121 bfd_set_section_alignment (stdoutput
, seg
, 2);
2123 /* Make space for this first symbol. */
2127 file
= as_where (NULL
);
2128 stabstr_name
= concat (segment_name (seg
), "str", (char *) NULL
);
2129 stroff
= get_stab_string_offset (file
, stabstr_name
);
2130 know (stroff
== 1 || (stroff
== 0 && file
[0] == '\0'));
2131 md_number_to_chars (p
, stroff
, 4);
2132 seg_info (seg
)->stabu
.p
= p
;
2137 /* Fill in the counts in the first entry in a .stabs section. */
2140 adjust_stab_sections (bfd
*abfd
, asection
*sec
, void *xxx ATTRIBUTE_UNUSED
)
2147 if (strncmp (".stab", sec
->name
, 5))
2149 if (!strcmp ("str", sec
->name
+ strlen (sec
->name
) - 3))
2152 name
= concat (sec
->name
, "str", NULL
);
2153 strsec
= bfd_get_section_by_name (abfd
, name
);
2155 strsz
= bfd_section_size (abfd
, strsec
);
2158 nsyms
= bfd_section_size (abfd
, sec
) / 12 - 1;
2160 p
= seg_info (sec
)->stabu
.p
;
2161 gas_assert (p
!= 0);
2163 bfd_h_put_16 (abfd
, nsyms
, p
+ 6);
2164 bfd_h_put_32 (abfd
, strsz
, p
+ 8);
2168 #ifdef NEED_ECOFF_DEBUG
2170 /* This function is called by the ECOFF code. It is supposed to
2171 record the external symbol information so that the backend can
2172 write it out correctly. The ELF backend doesn't actually handle
2173 this at the moment, so we do it ourselves. We save the information
2176 #ifdef OBJ_MAYBE_ELF
2180 elf_ecoff_set_ext (symbolS
*sym
, struct ecoff_extr
*ext
)
2182 symbol_get_bfdsym (sym
)->udata
.p
= ext
;
2185 /* This function is called by bfd_ecoff_debug_externals. It is
2186 supposed to *EXT to the external symbol information, and return
2187 whether the symbol should be used at all. */
2190 elf_get_extr (asymbol
*sym
, EXTR
*ext
)
2192 if (sym
->udata
.p
== NULL
)
2194 *ext
= *(EXTR
*) sym
->udata
.p
;
2198 /* This function is called by bfd_ecoff_debug_externals. It has
2199 nothing to do for ELF. */
2202 elf_set_index (asymbol
*sym ATTRIBUTE_UNUSED
,
2203 bfd_size_type indx ATTRIBUTE_UNUSED
)
2207 #endif /* NEED_ECOFF_DEBUG */
2210 elf_frob_symbol (symbolS
*symp
, int *puntp
)
2212 struct elf_obj_sy
*sy_obj
;
2215 #ifdef NEED_ECOFF_DEBUG
2216 if (ECOFF_DEBUGGING
)
2217 ecoff_frob_symbol (symp
);
2220 sy_obj
= symbol_get_obj (symp
);
2222 size
= sy_obj
->size
;
2225 if (resolve_expression (size
)
2226 && size
->X_op
== O_constant
)
2227 S_SET_SIZE (symp
, size
->X_add_number
);
2230 if (!flag_allow_nonconst_size
)
2231 as_bad (_(".size expression for %s "
2232 "does not evaluate to a constant"), S_GET_NAME (symp
));
2234 as_warn (_(".size expression for %s "
2235 "does not evaluate to a constant"), S_GET_NAME (symp
));
2237 free (sy_obj
->size
);
2238 sy_obj
->size
= NULL
;
2241 if (sy_obj
->versioned_name
!= NULL
)
2245 p
= strchr (sy_obj
->versioned_name
, ELF_VER_CHR
);
2247 /* We will have already reported an error about a missing version. */
2250 /* This symbol was given a new name with the .symver directive.
2252 If this is an external reference, just rename the symbol to
2253 include the version string. This will make the relocs be
2254 against the correct versioned symbol.
2256 If this is a definition, add an alias. FIXME: Using an alias
2257 will permit the debugging information to refer to the right
2258 symbol. However, it's not clear whether it is the best
2261 else if (! S_IS_DEFINED (symp
))
2263 /* Verify that the name isn't using the @@ syntax--this is
2264 reserved for definitions of the default version to link
2266 if (p
[1] == ELF_VER_CHR
)
2268 as_bad (_("invalid attempt to declare external version name"
2269 " as default in symbol `%s'"),
2270 sy_obj
->versioned_name
);
2273 S_SET_NAME (symp
, sy_obj
->versioned_name
);
2277 if (p
[1] == ELF_VER_CHR
&& p
[2] == ELF_VER_CHR
)
2281 /* The @@@ syntax is a special case. It renames the
2282 symbol name to versioned_name with one `@' removed. */
2283 l
= strlen (&p
[3]) + 1;
2284 memmove (&p
[2], &p
[3], l
);
2285 S_SET_NAME (symp
, sy_obj
->versioned_name
);
2291 /* FIXME: Creating a new symbol here is risky. We're
2292 in the final loop over the symbol table. We can
2293 get away with it only because the symbol goes to
2294 the end of the list, where the loop will still see
2295 it. It would probably be better to do this in
2296 obj_frob_file_before_adjust. */
2298 symp2
= symbol_find_or_make (sy_obj
->versioned_name
);
2300 /* Now we act as though we saw symp2 = sym. */
2301 if (S_IS_COMMON (symp
))
2303 as_bad (_("`%s' can't be versioned to common symbol '%s'"),
2304 sy_obj
->versioned_name
, S_GET_NAME (symp
));
2309 S_SET_SEGMENT (symp2
, S_GET_SEGMENT (symp
));
2311 /* Subtracting out the frag address here is a hack
2312 because we are in the middle of the final loop. */
2315 - symbol_get_frag (symp
)->fr_address
));
2317 symbol_set_frag (symp2
, symbol_get_frag (symp
));
2319 /* This will copy over the size information. */
2320 copy_symbol_attributes (symp2
, symp
);
2322 S_SET_OTHER (symp2
, S_GET_OTHER (symp
));
2324 if (S_IS_WEAK (symp
))
2327 if (S_IS_EXTERNAL (symp
))
2328 S_SET_EXTERNAL (symp2
);
2333 /* Double check weak symbols. */
2334 if (S_IS_WEAK (symp
))
2336 if (S_IS_COMMON (symp
))
2337 as_bad (_("symbol `%s' can not be both weak and common"),
2342 /* The Irix 5 and 6 assemblers set the type of any common symbol and
2343 any undefined non-function symbol to STT_OBJECT. We try to be
2344 compatible, since newer Irix 5 and 6 linkers care. However, we
2345 only set undefined symbols to be STT_OBJECT if we are on Irix,
2346 because that is the only time gcc will generate the necessary
2347 .global directives to mark functions. */
2349 if (S_IS_COMMON (symp
))
2350 symbol_get_bfdsym (symp
)->flags
|= BSF_OBJECT
;
2352 if (strstr (TARGET_OS
, "irix") != NULL
2353 && ! S_IS_DEFINED (symp
)
2354 && (symbol_get_bfdsym (symp
)->flags
& BSF_FUNCTION
) == 0)
2355 symbol_get_bfdsym (symp
)->flags
|= BSF_OBJECT
;
2361 asection
**head
; /* Section lists. */
2362 unsigned int num_group
; /* Number of lists. */
2363 struct hash_control
*indexes
; /* Maps group name to index in head array. */
2366 static struct group_list groups
;
2368 /* Called via bfd_map_over_sections. If SEC is a member of a group,
2369 add it to a list of sections belonging to the group. INF is a
2370 pointer to a struct group_list, which is where we store the head of
2374 build_group_lists (bfd
*abfd ATTRIBUTE_UNUSED
, asection
*sec
, void *inf
)
2376 struct group_list
*list
= (struct group_list
*) inf
;
2377 const char *group_name
= elf_group_name (sec
);
2379 unsigned int *elem_idx
;
2380 unsigned int *idx_ptr
;
2382 if (group_name
== NULL
)
2385 /* If this group already has a list, add the section to the head of
2387 elem_idx
= (unsigned int *) hash_find (list
->indexes
, group_name
);
2388 if (elem_idx
!= NULL
)
2390 elf_next_in_group (sec
) = list
->head
[*elem_idx
];
2391 list
->head
[*elem_idx
] = sec
;
2395 /* New group. Make the arrays bigger in chunks to minimize calls to
2397 i
= list
->num_group
;
2400 unsigned int newsize
= i
+ 128;
2401 list
->head
= XRESIZEVEC (asection
*, list
->head
, newsize
);
2403 list
->head
[i
] = sec
;
2404 list
->num_group
+= 1;
2406 /* Add index to hash. */
2407 idx_ptr
= XNEW (unsigned int);
2409 hash_insert (list
->indexes
, group_name
, idx_ptr
);
2412 static void free_section_idx (const char *key ATTRIBUTE_UNUSED
, void *val
)
2414 free ((unsigned int *) val
);
2417 /* Create symbols for group signature. */
2420 elf_adjust_symtab (void)
2424 /* Go find section groups. */
2425 groups
.num_group
= 0;
2427 groups
.indexes
= hash_new ();
2428 bfd_map_over_sections (stdoutput
, build_group_lists
, &groups
);
2430 /* Make the SHT_GROUP sections that describe each section group. We
2431 can't set up the section contents here yet, because elf section
2432 indices have yet to be calculated. elf.c:set_group_contents does
2433 the rest of the work. */
2434 for (i
= 0; i
< groups
.num_group
; i
++)
2436 const char *group_name
= elf_group_name (groups
.head
[i
]);
2437 const char *sec_name
;
2442 flags
= SEC_READONLY
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
| SEC_GROUP
;
2443 for (s
= groups
.head
[i
]; s
!= NULL
; s
= elf_next_in_group (s
))
2444 if ((s
->flags
^ flags
) & SEC_LINK_ONCE
)
2446 flags
|= SEC_LINK_ONCE
| SEC_LINK_DUPLICATES_DISCARD
;
2447 if (s
!= groups
.head
[i
])
2449 as_warn (_("assuming all members of group `%s' are COMDAT"),
2455 sec_name
= ".group";
2456 s
= subseg_force_new (sec_name
, 0);
2458 || !bfd_set_section_flags (stdoutput
, s
, flags
)
2459 || !bfd_set_section_alignment (stdoutput
, s
, 2))
2461 as_fatal (_("can't create group: %s"),
2462 bfd_errmsg (bfd_get_error ()));
2464 elf_section_type (s
) = SHT_GROUP
;
2466 /* Pass a pointer to the first section in this group. */
2467 elf_next_in_group (s
) = groups
.head
[i
];
2468 elf_sec_group (groups
.head
[i
]) = s
;
2469 /* Make sure that the signature symbol for the group has the
2470 name of the group. */
2471 sy
= symbol_find_exact (group_name
);
2473 || (sy
!= symbol_lastP
2474 && (sy
->sy_flags
.sy_local_symbol
2475 || sy
->sy_next
== NULL
2476 || sy
->sy_next
->sy_previous
!= sy
)))
2478 /* Create the symbol now. */
2479 sy
= symbol_new (group_name
, now_seg
, (valueT
) 0, frag_now
);
2481 /* Before Solaris 11 build 154, Sun ld rejects local group
2482 signature symbols, so make them weak hidden instead. */
2483 symbol_get_bfdsym (sy
)->flags
|= BSF_WEAK
;
2484 S_SET_OTHER (sy
, STV_HIDDEN
);
2486 symbol_get_obj (sy
)->local
= 1;
2488 symbol_table_insert (sy
);
2490 elf_group_id (s
) = symbol_get_bfdsym (sy
);
2495 elf_frob_file (void)
2497 bfd_map_over_sections (stdoutput
, adjust_stab_sections
, NULL
);
2499 #ifdef elf_tc_final_processing
2500 elf_tc_final_processing ();
2504 /* It removes any unneeded versioned symbols from the symbol table. */
2507 elf_frob_file_before_adjust (void)
2513 for (symp
= symbol_rootP
; symp
; symp
= symbol_next (symp
))
2514 if (!S_IS_DEFINED (symp
))
2516 if (symbol_get_obj (symp
)->versioned_name
)
2520 /* The @@@ syntax is a special case. If the symbol is
2521 not defined, 2 `@'s will be removed from the
2524 p
= strchr (symbol_get_obj (symp
)->versioned_name
,
2526 if (p
!= NULL
&& p
[1] == ELF_VER_CHR
&& p
[2] == ELF_VER_CHR
)
2528 size_t l
= strlen (&p
[3]) + 1;
2529 memmove (&p
[1], &p
[3], l
);
2531 if (symbol_used_p (symp
) == 0
2532 && symbol_used_in_reloc_p (symp
) == 0)
2533 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2536 /* If there was .weak foo, but foo was neither defined nor
2537 used anywhere, remove it. */
2539 else if (S_IS_WEAK (symp
)
2540 && symbol_used_p (symp
) == 0
2541 && symbol_used_in_reloc_p (symp
) == 0)
2542 symbol_remove (symp
, &symbol_rootP
, &symbol_lastP
);
2547 /* It is required that we let write_relocs have the opportunity to
2548 optimize away fixups before output has begun, since it is possible
2549 to eliminate all fixups for a section and thus we never should
2550 have generated the relocation section. */
2553 elf_frob_file_after_relocs (void)
2557 /* Set SHT_GROUP section size. */
2558 for (i
= 0; i
< groups
.num_group
; i
++)
2560 asection
*s
, *head
, *group
;
2563 head
= groups
.head
[i
];
2565 for (s
= head
; s
!= NULL
; s
= elf_next_in_group (s
))
2566 size
+= (s
->flags
& SEC_RELOC
) != 0 ? 8 : 4;
2568 group
= elf_sec_group (head
);
2569 subseg_set (group
, 0);
2570 bfd_set_section_size (stdoutput
, group
, size
);
2571 group
->contents
= (unsigned char *) frag_more (size
);
2572 frag_now
->fr_fix
= frag_now_fix_octets ();
2573 frag_wane (frag_now
);
2577 hash_traverse (groups
.indexes
, free_section_idx
);
2578 hash_die (groups
.indexes
);
2580 #ifdef NEED_ECOFF_DEBUG
2581 if (ECOFF_DEBUGGING
)
2582 /* Generate the ECOFF debugging information. */
2584 const struct ecoff_debug_swap
*debug_swap
;
2585 struct ecoff_debug_info debug
;
2590 = get_elf_backend_data (stdoutput
)->elf_backend_ecoff_debug_swap
;
2591 know (debug_swap
!= NULL
);
2592 ecoff_build_debug (&debug
.symbolic_header
, &buf
, debug_swap
);
2594 /* Set up the pointers in debug. */
2595 #define SET(ptr, offset, type) \
2596 debug.ptr = (type) (buf + debug.symbolic_header.offset)
2598 SET (line
, cbLineOffset
, unsigned char *);
2599 SET (external_dnr
, cbDnOffset
, void *);
2600 SET (external_pdr
, cbPdOffset
, void *);
2601 SET (external_sym
, cbSymOffset
, void *);
2602 SET (external_opt
, cbOptOffset
, void *);
2603 SET (external_aux
, cbAuxOffset
, union aux_ext
*);
2604 SET (ss
, cbSsOffset
, char *);
2605 SET (external_fdr
, cbFdOffset
, void *);
2606 SET (external_rfd
, cbRfdOffset
, void *);
2607 /* ssext and external_ext are set up just below. */
2611 /* Set up the external symbols. */
2612 debug
.ssext
= debug
.ssext_end
= NULL
;
2613 debug
.external_ext
= debug
.external_ext_end
= NULL
;
2614 if (! bfd_ecoff_debug_externals (stdoutput
, &debug
, debug_swap
, TRUE
,
2615 elf_get_extr
, elf_set_index
))
2616 as_fatal (_("failed to set up debugging information: %s"),
2617 bfd_errmsg (bfd_get_error ()));
2619 sec
= bfd_get_section_by_name (stdoutput
, ".mdebug");
2620 gas_assert (sec
!= NULL
);
2622 know (!stdoutput
->output_has_begun
);
2624 /* We set the size of the section, call bfd_set_section_contents
2625 to force the ELF backend to allocate a file position, and then
2626 write out the data. FIXME: Is this really the best way to do
2628 bfd_set_section_size
2629 (stdoutput
, sec
, bfd_ecoff_debug_size (stdoutput
, &debug
, debug_swap
));
2631 /* Pass BUF to bfd_set_section_contents because this will
2632 eventually become a call to fwrite, and ISO C prohibits
2633 passing a NULL pointer to a stdio function even if the
2634 pointer will not be used. */
2635 if (! bfd_set_section_contents (stdoutput
, sec
, buf
, 0, 0))
2636 as_fatal (_("can't start writing .mdebug section: %s"),
2637 bfd_errmsg (bfd_get_error ()));
2639 know (stdoutput
->output_has_begun
);
2640 know (sec
->filepos
!= 0);
2642 if (! bfd_ecoff_write_debug (stdoutput
, &debug
, debug_swap
,
2644 as_fatal (_("could not write .mdebug section: %s"),
2645 bfd_errmsg (bfd_get_error ()));
2647 #endif /* NEED_ECOFF_DEBUG */
2651 elf_generate_asm_lineno (void)
2653 #ifdef NEED_ECOFF_DEBUG
2654 if (ECOFF_DEBUGGING
)
2655 ecoff_generate_asm_lineno ();
2660 elf_process_stab (segT sec ATTRIBUTE_UNUSED
,
2661 int what ATTRIBUTE_UNUSED
,
2662 const char *string ATTRIBUTE_UNUSED
,
2663 int type ATTRIBUTE_UNUSED
,
2664 int other ATTRIBUTE_UNUSED
,
2665 int desc ATTRIBUTE_UNUSED
)
2667 #ifdef NEED_ECOFF_DEBUG
2668 if (ECOFF_DEBUGGING
)
2669 ecoff_stab (sec
, what
, string
, type
, other
, desc
);
2674 elf_separate_stab_sections (void)
2676 #ifdef NEED_ECOFF_DEBUG
2677 return (!ECOFF_DEBUGGING
);
2684 elf_init_stab_section (segT seg
)
2686 #ifdef NEED_ECOFF_DEBUG
2687 if (!ECOFF_DEBUGGING
)
2689 obj_elf_init_stab_section (seg
);
2692 const struct format_ops elf_format_ops
=
2694 bfd_target_elf_flavour
,
2695 0, /* dfl_leading_underscore */
2696 1, /* emit_section_symbols */
2701 elf_frob_file_before_adjust
,
2702 0, /* obj_frob_file_before_fix */
2703 elf_frob_file_after_relocs
,
2704 elf_s_get_size
, elf_s_set_size
,
2705 elf_s_get_align
, elf_s_set_align
,
2712 elf_copy_symbol_attributes
,
2713 elf_generate_asm_lineno
,
2715 elf_separate_stab_sections
,
2716 elf_init_stab_section
,
2717 elf_sec_sym_ok_for_reloc
,
2719 #ifdef NEED_ECOFF_DEBUG
2722 0, /* ecoff_set_ext */
2724 elf_obj_read_begin_hook
,
2725 elf_obj_symbol_new_hook
,