1 /* ELF object file format
2 Copyright (C) 1992, 1993 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 2,
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
17 License along with GAS; see the file COPYING. If not, write
18 to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 #include "aout/stab_gnu.h"
25 static void obj_elf_stab
PARAMS ((int what
));
26 static void obj_elf_xstab
PARAMS ((int what
));
27 static void obj_elf_line
PARAMS ((void));
28 void obj_elf_desc
PARAMS ((void));
29 void obj_elf_version
PARAMS ((void));
30 static void obj_elf_size
PARAMS ((void));
31 static void obj_elf_type
PARAMS ((void));
32 static void obj_elf_ident
PARAMS ((void));
33 static void obj_elf_weak
PARAMS ((void));
35 const pseudo_typeS obj_pseudo_table
[] =
37 {"ident", obj_elf_ident
, 0},
38 {"previous", obj_elf_previous
, 0},
39 {"section", obj_elf_section
, 0},
40 {"size", obj_elf_size
, 0},
41 {"type", obj_elf_type
, 0},
42 {"version", obj_elf_version
, 0},
43 {"weak", obj_elf_weak
, 0},
45 /* These are used for stabs-in-elf configurations. */
46 {"desc", obj_elf_desc
, 0},
47 {"line", obj_elf_line
, 0},
48 {"stabd", obj_elf_stab
, 'd'},
49 {"stabn", obj_elf_stab
, 'n'},
50 {"stabs", obj_elf_stab
, 's'},
51 /* This is used on Solaris 2.x on SPARC, but not supported yet. */
52 {"xstabs", obj_elf_xstab
, 's'},
54 /* These are used for dwarf. */
59 {NULL
} /* end sentinel */
63 #include "aout/aout64.h"
71 sym
= symbol_new (s
, absolute_section
, (valueT
) 0, (struct frag
*) 0);
72 sym
->sy_frag
= &zero_address_frag
;
73 sym
->bsym
->flags
|= BSF_FILE
;
75 if (symbol_rootP
!= sym
)
77 symbol_remove (sym
, &symbol_rootP
, &symbol_lastP
);
78 symbol_insert (sym
, symbol_rootP
, &symbol_rootP
, &symbol_lastP
);
80 verify_symbol_chain (symbol_rootP
, symbol_lastP
);
94 name
= input_line_pointer
;
95 c
= get_symbol_end ();
96 symbolP
= symbol_find_or_make (name
);
97 *input_line_pointer
= c
;
103 input_line_pointer
++;
105 if (*input_line_pointer
== '\n')
110 demand_empty_rest_of_line ();
113 static segT previous_section
;
114 static int previous_subsection
;
117 obj_elf_section (xxx
)
123 /* Initialize this with inclusive-or of all flags that can be cleared
124 by attributes, but not set by them. Also include flags that won't
125 get set properly in the assembler, but which the user/compiler
126 shouldn't be expected to set. */
127 flagword flags
= SEC_READONLY
| SEC_ALLOC
| SEC_RELOC
;
128 /* Initialize this with the default flags to be used if none are
130 flagword default_flags
= 0;
133 /* Get name of section. */
134 if (*input_line_pointer
== '"')
135 string
= demand_copy_C_string (&xxx
);
138 char *p
= input_line_pointer
;
140 while (0 == strchr ("\n\t,; ", *p
))
144 string
= xmalloc (p
- input_line_pointer
+ 1);
145 strcpy (string
, input_line_pointer
);
147 input_line_pointer
= p
;
149 if (!strcmp (string
, ".rodata"))
150 default_flags
= SEC_ALLOC
| SEC_READONLY
| SEC_RELOC
;
151 else if (!strcmp (string
, ".init")
152 || !strcmp (string
, ".fini"))
153 default_flags
= SEC_ALLOC
| SEC_READONLY
| SEC_RELOC
| SEC_CODE
;
156 if (*input_line_pointer
!= ',')
157 flags
= default_flags
;
158 while (*input_line_pointer
== ',')
164 input_line_pointer
++;
166 /* Under i386-svr4, gcc emits a string here. I don't know what this
167 string is supposed to signify or how to handle it. Ignore it for
168 now, unless it becomes a problem. */
169 if (*input_line_pointer
== '"')
171 demand_copy_C_string (&xxx
);
176 if (*input_line_pointer
!= '#' && *input_line_pointer
!= '@')
178 as_bad ("unrecognized syntax in .section command");
179 ignore_rest_of_line ();
182 input_line_pointer
++;
184 #define CHECK(X,BIT,NEG) \
185 if (!strncmp(X,input_line_pointer,len = sizeof(X) - 1)) { \
186 bit = BIT; inv = NEG; goto match; }
188 CHECK ("write", SEC_READONLY
, 1);
189 CHECK ("alloc", SEC_ALLOC
, 0);
190 CHECK ("execinstr", SEC_CODE
, 1);
191 CHECK ("progbits", SEC_LOAD
, 1);
194 p
= input_line_pointer
;
195 while (!is_end_of_line
[*p
] && *p
!= 0 && *p
!= ',')
199 as_bad ("unrecognized section attribute `%s' ignored",
209 input_line_pointer
+= len
;
211 demand_empty_rest_of_line ();
213 /* If the C string wasn't valid, `string' could be null. */
217 sec
= bfd_get_section_by_name (stdoutput
, string
);
220 sec
= subseg_new (string
, 0);
221 bfd_set_section_flags (stdoutput
, sec
, flags
);
222 sec
->output_section
= sec
;
224 previous_section
= now_seg
;
225 previous_subsection
= now_subseg
;
232 if (previous_section
== 0)
234 as_bad (".previous without corresponding .section; ignored");
237 subseg_set (previous_section
, previous_subsection
);
238 previous_section
= 0;
242 obj_elf_write_symbol_p (sym
)
245 /* If this is a local symbol, are there any relocations for which
248 /* To find this out, we examine all relocations in all bfd sections
249 that have relocations. If there is one that references this
250 symbol, we need to keep this symbol. In this case, we return a
251 true status. In all other cases, we return a false status. */
253 if (S_IS_LOCAL (sym
))
255 asymbol
*bsym
= sym
->bsym
;
256 bfd
*abfd
= bsym
->the_bfd
;
259 for (bsec
= abfd
->sections
; bsec
; bsec
= bsec
->next
)
261 struct reloc_cache_entry
**rlocs
= bsec
->orelocation
;
262 int rcnt
= bsec
->reloc_count
;
268 for (i
= 0; i
< rcnt
; i
++)
269 if (rlocs
[i
]->sym_ptr_ptr
270 && rlocs
[i
]->sym_ptr_ptr
[0] == bsym
)
275 /* No relocations for this section. Check the seg_info
276 structure to see if there are any fixups for this
278 segment_info_type
*seginfo
= seg_info (bsec
);
281 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
282 if ((fixp
->fx_addsy
&& fixp
->fx_addsy
->bsym
== bsym
)
283 || (fixp
->fx_subsy
&& fixp
->fx_subsy
->bsym
== bsym
))
292 obj_elf_write_symbol (sym
)
295 return /* obj_elf_write_symbol_p (sym) || */ !S_IS_LOCAL (sym
);
299 obj_elf_frob_symbol (sym
, punt
)
303 #if 0 /* ?? The return value is ignored. Only the value of *punt is
305 return obj_elf_write_symbol_p (sym
);
312 /* Assume delimiter is part of expression. BSD4.2 as fails with
313 delightful bug, so we are not being incompatible here. */
314 new_logical_line ((char *) NULL
, (int) (get_absolute_expression ()));
315 demand_empty_rest_of_line ();
321 * Handle .stabX directives, which used to be open-coded.
322 * So much creeping featurism overloaded the semantics that we decided
323 * to put all .stabX thinking in one place. Here.
325 * We try to make any .stabX directive legal. Other people's AS will often
326 * do assembly-time consistency checks: eg assigning meaning to n_type bits
327 * and "protecting" you from setting them to certain values. (They also zero
328 * certain bits before emitting symbols. Tut tut.)
330 * If an expression is not absolute we either gripe or use the relocation
331 * information. Other people's assemblers silently forget information they
332 * don't need and invent information they need that you didn't supply.
334 * .stabX directives always make a symbol table entry. It may be junk if
335 * the rest of your .stabX directive is malformed.
339 * elf_stab_symbol_string()
341 * Build a string dictionary entry for a .stabX symbol.
342 * The symbol is added to the .stabstr section.
347 elf_stab_symbol_string (string
, secname
)
348 char *string
, *secname
;
354 unsigned int old_gdb_string_index
;
358 /* @@FIXME -- there should be no static data here!
359 This also has the effect of making all stab string tables large enough
360 to contain all the contents written to any of them. This only matters
361 with the Solaris native compiler for the moment, but it should be fixed
363 static unsigned int gdb_string_index
= 0;
365 old_gdb_string_index
= 0;
366 length
= strlen (string
);
367 clengthP
= (char *) &length
;
369 { /* Ordinary case. */
371 save_subseg
= now_subseg
;
373 /* Create the stab sections, if they are not already created. */
375 char *newsecname
= xmalloc (strlen (secname
) + 4);
376 strcpy (newsecname
, secname
);
377 strcat (newsecname
, "str");
378 seg
= bfd_get_section_by_name (stdoutput
, newsecname
);
381 seg
= bfd_make_section_old_way (stdoutput
, newsecname
);
382 bfd_set_section_flags (stdoutput
, seg
, SEC_READONLY
| SEC_ALLOC
);
384 /* free (newsecname);*/
386 subseg_new ((char *) seg
->name
, save_subseg
);
387 old_gdb_string_index
= gdb_string_index
;
389 while ((c
= *string
++))
393 FRAG_APPEND_1_CHAR (c
);
396 FRAG_APPEND_1_CHAR ((char) 0);
402 FRAG_APPEND_1_CHAR ((char) 0);
406 subseg_new ((char *) save_seg
->name
, save_subseg
);
409 return old_gdb_string_index
;
413 DEFUN (elf_stab_symbol
, (symbolP
, stab_type
),
420 /* the string index portion of the stab */
421 md_number_to_chars (toP
, (valueT
) symbolP
->sy_name_offset
, 4);
422 md_number_to_chars (toP
+ 4, (valueT
) S_GET_TYPE (symbolP
), 1);
423 md_number_to_chars (toP
+ 5, (valueT
) S_GET_OTHER (symbolP
), 1);
424 md_number_to_chars (toP
+ 6, (valueT
) S_GET_DESC (symbolP
), 2);
425 /* The n_value field doesn't get written here, it gets done below. It
426 may be an expression needing relocating. */
430 obj_elf_stab_generic (what
, secname
)
436 symbolS
*symbolP
= 0;
443 asection
*saved_seg
= now_seg
;
445 subsegT subseg
= now_subseg
;
448 /* This function doesn't work yet.
450 Actually, this function is okay, but some finalizations are
451 needed before writing the object file; that's not done yet, and
452 the Solaris linker chokes without it.
454 In any case, this should effectively disable it for now. */
456 demand_copy_C_string (&length
);
461 seg
= bfd_get_section_by_name (stdoutput
, secname
);
464 seg
= subseg_new (secname
, 0);
465 bfd_set_section_flags (stdoutput
, seg
,
466 SEC_READONLY
| SEC_ALLOC
| SEC_RELOC
);
467 subseg_set (saved_seg
, subseg
);
472 * Enter with input_line_pointer pointing past .stabX and any following
477 string
= demand_copy_C_string (&length
);
479 if (*input_line_pointer
== ',')
480 input_line_pointer
++;
483 as_bad ("I need a comma after symbol's name");
491 * Input_line_pointer->after ','. String->symbol name.
495 symbolP
= symbol_new (string
, &bfd_und_section
, (valueT
) 0, (struct frag
*) 0);
497 /* enter the string in the .stab string table (section .stabstr) */
498 symbolP
->sy_name_offset
= elf_stab_symbol_string (string
, secname
);
503 S_SET_NAME (symbolP
, NULL
); /* .stabd feature. */
504 S_SET_VALUE (symbolP
,
505 (valueT
) ((char*) obstack_next_free (&frags
) - frag_now
->fr_literal
));
506 S_SET_SEGMENT (symbolP
, now_seg
);
507 symbolP
->sy_frag
= frag_now
;
511 symbolP
->sy_frag
= &zero_address_frag
;
515 symbolP
->sy_frag
= &zero_address_frag
;
523 if (get_absolute_expression_and_terminator (&longint
) == ',')
525 saved_type
= longint
;
526 S_SET_TYPE (symbolP
, saved_type
);
530 as_bad ("I want a comma after the n_type expression");
532 input_line_pointer
--; /* Backup over a non-',' char. */
538 if (get_absolute_expression_and_terminator (&longint
) == ',')
539 S_SET_OTHER (symbolP
, longint
);
542 as_bad ("I want a comma after the n_other expression");
544 input_line_pointer
--; /* Backup over a non-',' char. */
550 S_SET_DESC (symbolP
, get_absolute_expression ());
551 if (what
== 's' || what
== 'n')
553 if (*input_line_pointer
!= ',')
555 as_bad ("I want a comma after the n_desc expression");
560 input_line_pointer
++;
567 ignore_rest_of_line ();
571 subseg_new ((char *) seg
->name
, subseg
);
574 /* allocate and discard -- filled in later */
575 (void) frag_more (12);
577 /* Emit the stab symbol. */
578 elf_stab_symbol (symbolP
, what
);
580 if (what
== 's' || what
== 'n')
583 input_line_pointer
--;
587 char *p
= frag_more (4);
588 md_number_to_chars (p
, 0, 0);
591 subseg_new ((char *) saved_seg
->name
, subseg
);
593 if ((what
== 's' || what
== 'n')
594 && symbolP
->sy_value
.X_op
== O_constant
)
596 /* symbol is not needed in the regular symbol table */
597 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
600 if (what
== 's' && S_GET_TYPE (symbolP
) == N_SO
)
602 fragS
*fragp
= seg_info (seg
)->frchainP
->frch_root
;
604 && fragp
->fr_address
+ fragp
->fr_fix
< 12)
605 fragp
= fragp
->fr_next
;
607 assert (fragp
->fr_type
== rs_fill
);
608 assert (fragp
->fr_address
== 0 && fragp
->fr_fix
>= 12);
609 md_number_to_chars (fragp
->fr_literal
, (valueT
) symbolP
->sy_name_offset
,
614 switch (S_GET_TYPE (symbolP
))
617 listing_source_line (S_GET_DESC (symbolP
));
621 listing_source_file (string
);
625 demand_empty_rest_of_line ();
632 obj_elf_stab_generic (what
, ".stab");
642 secname
= demand_copy_C_string (&length
);
644 if (*input_line_pointer
== ',')
645 input_line_pointer
++;
648 as_bad ("comma missing in .xstabs");
649 ignore_rest_of_line ();
652 obj_elf_stab_generic (what
, secname
);
664 /* Frob invented at RMS' request. Set the n_desc of a symbol. */
665 name
= input_line_pointer
;
666 c
= get_symbol_end ();
667 p
= input_line_pointer
;
670 if (*input_line_pointer
!= ',')
673 as_bad ("Expected comma after name \"%s\"", name
);
675 ignore_rest_of_line ();
679 input_line_pointer
++;
680 temp
= get_absolute_expression ();
682 symbolP
= symbol_find_or_make (name
);
684 S_SET_DESC (symbolP
, temp
);
686 demand_empty_rest_of_line ();
687 } /* obj_elf_desc() */
690 obj_read_begin_hook ()
695 obj_symbol_new_hook (symbolP
)
698 #if 0 /* BFD already takes care of this */
699 elf32_symbol_type
*esym
= (elf32_symbol_type
*) symbolP
;
701 /* There is an Elf_Internal_Sym and an Elf_External_Sym. For now,
702 just zero them out. */
704 bzero ((char *) &esym
->internal_elf_sym
, sizeof (esym
->internal_elf_sym
));
705 bzero ((char *) &esym
->native_elf_sym
, sizeof (esym
->native_elf_sym
));
706 bzero ((char *) &esym
->tc_data
, sizeof (esym
->tc_data
));
717 asection
*seg
= now_seg
;
718 subsegT subseg
= now_subseg
;
719 Elf_Internal_Note i_note
;
720 Elf_External_Note e_note
;
721 asection
*note_secp
= (asection
*) NULL
;
725 if (*input_line_pointer
== '\"')
727 ++input_line_pointer
; /* -> 1st char of string. */
728 name
= input_line_pointer
;
730 while (is_a_char (c
= next_char_of_string ()))
732 c
= *input_line_pointer
;
733 *input_line_pointer
= '\0';
734 *(input_line_pointer
- 1) = '\0';
735 *input_line_pointer
= c
;
737 /* create the .note section if this is the first version string */
739 note_secp
= bfd_get_section_by_name (stdoutput
, ".note");
740 if (note_secp
== (asection
*) NULL
)
742 note_secp
= bfd_make_section_old_way (stdoutput
, ".note");
743 bfd_set_section_flags (stdoutput
,
745 SEC_LOAD
| SEC_ALLOC
| SEC_HAS_CONTENTS
);
748 /* process the version string */
750 subseg_new ((char *) note_secp
->name
, 0);
753 i_note
.namesz
= ((len
+ 1) + 3) & ~3; /* round this to word boundary */
754 i_note
.descsz
= 0; /* no description */
755 i_note
.type
= NT_VERSION
;
756 p
= frag_more (sizeof (e_note
.namesz
));
757 md_number_to_chars (p
, (valueT
) i_note
.namesz
, 4);
758 p
= frag_more (sizeof (e_note
.descsz
));
759 md_number_to_chars (p
, (valueT
) i_note
.descsz
, 4);
760 p
= frag_more (sizeof (e_note
.type
));
761 md_number_to_chars (p
, (valueT
) i_note
.type
, 4);
763 for (i
= 0; i
< len
; i
++)
767 FRAG_APPEND_1_CHAR (ch
);
772 subseg_new ((char *) seg
->name
, subseg
);
776 as_bad ("Expected quoted string");
778 demand_empty_rest_of_line ();
784 char *name
= input_line_pointer
;
785 char c
= get_symbol_end ();
790 p
= input_line_pointer
;
793 if (*input_line_pointer
!= ',')
796 as_bad ("expected comma after name `%s' in .size directive", name
);
798 ignore_rest_of_line ();
801 input_line_pointer
++;
803 if (exp
.X_op
== O_absent
)
805 as_bad ("missing expression in .size directive");
806 exp
.X_op
= O_constant
;
807 exp
.X_add_number
= 0;
810 sym
= symbol_find_or_make (name
);
812 if (exp
.X_op
== O_constant
)
813 S_SET_SIZE (sym
, exp
.X_add_number
);
820 as_tsktsk (".size expressions not yet supported, ignored");
825 demand_empty_rest_of_line ();
831 char *name
= input_line_pointer
;
832 char c
= get_symbol_end ();
837 p
= input_line_pointer
;
840 if (*input_line_pointer
!= ',')
842 as_bad ("expected comma after name in .type directive");
844 ignore_rest_of_line ();
847 input_line_pointer
++;
849 if (*input_line_pointer
!= '#' && *input_line_pointer
!= '@')
851 as_bad ("expected `#' or `@' after comma in .type directive");
854 input_line_pointer
++;
855 if (!strncmp ("function", input_line_pointer
, sizeof ("function") - 1))
858 input_line_pointer
+= sizeof ("function") - 1;
860 else if (!strncmp ("object", input_line_pointer
, sizeof ("object") - 1))
862 input_line_pointer
+= sizeof ("object") - 1;
866 as_bad ("unrecognized symbol type, ignored");
869 demand_empty_rest_of_line ();
871 sym
= symbol_find_or_make (name
);
872 sym
->bsym
->flags
|= type
;
878 static segT comment_section
;
879 segT old_section
= now_seg
;
880 int old_subsection
= now_subseg
;
882 if (!comment_section
)
885 comment_section
= subseg_new (".comment", 0);
886 bfd_set_section_flags (stdoutput
, comment_section
, SEC_HAS_CONTENTS
);
891 subseg_set (comment_section
, 0);
893 subseg_set (old_section
, old_subsection
);
897 adjust_stab_sections (abfd
, sec
, xxx
)
907 if (strncmp (".stab", sec
->name
, 5))
909 if (!strcmp ("str", sec
->name
+ strlen (sec
->name
) - 3))
912 name
= (char *) alloca (strlen (sec
->name
) + 4);
913 strcpy (name
, sec
->name
);
914 strcat (name
, "str");
915 strsec
= bfd_get_section_by_name (abfd
, name
);
917 strsz
= bfd_section_size (abfd
, strsec
);
920 nsyms
= bfd_section_size (abfd
, sec
) / 12 - 1;
922 fragp
= seg_info (sec
)->frchainP
->frch_root
;
924 && fragp
->fr_address
+ fragp
->fr_fix
< 12)
925 fragp
= fragp
->fr_next
;
927 assert (fragp
->fr_type
== rs_fill
);
928 assert (fragp
->fr_address
== 0 && fragp
->fr_fix
>= 12);
930 bfd_h_put_16 (abfd
, nsyms
, fragp
->fr_literal
+ 6);
931 bfd_h_put_32 (abfd
, strsz
, fragp
->fr_literal
+ 8);
937 bfd_map_over_sections (stdoutput
, adjust_stab_sections
, (PTR
) 0);
943 for (i
= 0; i
< stdoutput
->symcount
; i
++)
944 elf_tc_symbol (stdoutput
, (elf_symbol_type
*) (stdoutput
->outsymbols
[i
]),
949 #ifdef elf_tc_final_processing
950 elf_tc_final_processing_hook ();
953 /* Finally, we must make any target-specific sections. */
955 #ifdef elf_tc_make_sections
956 elf_tc_make_sections (stdoutput
, NULL
);