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_section
PARAMS ((int));
31 static void obj_elf_size
PARAMS ((void));
32 static void obj_elf_type
PARAMS ((void));
33 static void obj_elf_ident
PARAMS ((void));
34 static void obj_elf_previous
PARAMS ((void));
36 const pseudo_typeS obj_pseudo_table
[] =
38 {"ident", obj_elf_ident
, 0},
39 {"previous", obj_elf_previous
, 0},
40 {"section", obj_elf_section
, 0},
41 {"size", obj_elf_size
, 0},
42 {"type", obj_elf_type
, 0},
43 {"version", obj_elf_version
, 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. */
58 {NULL
} /* end sentinel */
61 #include "aout/aout64.h"
69 sym
= symbol_new (s
, absolute_section
, (valueT
) 0, (struct frag
*) 0);
70 sym
->sy_frag
= &zero_address_frag
;
71 sym
->bsym
->flags
|= BSF_FILE
;
73 if (symbol_rootP
!= sym
)
75 symbol_remove (sym
, &symbol_rootP
, &symbol_lastP
);
76 symbol_insert (sym
, symbol_rootP
, &symbol_rootP
, &symbol_lastP
);
78 verify_symbol_chain (symbol_rootP
, symbol_lastP
);
83 static segT previous_section
;
84 static int previous_subsection
;
93 /* Initialize this with inclusive-or of all flags that can be cleared
94 by attributes, but not set by them. Also include flags that won't
95 get set properly in the assembler, but which the user/compiler
96 shouldn't be expected to set. */
97 flagword flags
= SEC_READONLY
| SEC_ALLOC
| SEC_RELOC
;
98 /* Initialize this with the default flags to be used if none are
100 flagword default_flags
= 0;
103 if (*input_line_pointer
== '"')
104 string
= demand_copy_C_string (&xxx
);
107 char *p
= input_line_pointer
;
109 while (0 == strchr ("\n\t,; ", *p
))
113 string
= xmalloc (p
- input_line_pointer
+ 1);
114 strcpy (string
, input_line_pointer
);
116 input_line_pointer
= p
;
118 if (!strcmp (string
, ".rodata"))
119 default_flags
= SEC_ALLOC
| SEC_READONLY
| SEC_RELOC
;
121 if (*input_line_pointer
!= ',')
122 flags
= default_flags
;
123 while (*input_line_pointer
== ',')
129 input_line_pointer
++;
130 if (*input_line_pointer
!= '#' && *input_line_pointer
!= '@')
132 as_bad ("unrecognized syntax in .section command");
133 ignore_rest_of_line ();
136 input_line_pointer
++;
138 #define CHECK(X,BIT,NEG) \
139 if (!strncmp(X,input_line_pointer,len = sizeof(X) - 1)) { \
140 bit = BIT; inv = NEG; goto match; }
142 CHECK ("write", SEC_READONLY
, 1);
143 CHECK ("alloc", SEC_ALLOC
, 0);
144 CHECK ("execinstr", SEC_CODE
, 1);
147 p
= input_line_pointer
;
148 while (!is_end_of_line
[*p
] && *p
!= 0 && *p
!= ',')
152 as_bad ("unrecognized section attribute `%s' ignored",
162 input_line_pointer
+= len
;
164 demand_empty_rest_of_line ();
166 /* If the C string wasn't valid, `string' could be null. */
170 sec
= bfd_get_section_by_name (stdoutput
, string
);
173 sec
= subseg_new (string
, 0);
174 bfd_set_section_flags (stdoutput
, sec
, flags
);
175 sec
->output_section
= sec
;
177 previous_section
= now_seg
;
178 previous_subsection
= now_subseg
;
185 if (previous_section
== 0)
187 as_bad (".previous without corresponding .section; ignored");
190 subseg_set (previous_section
, previous_subsection
);
191 previous_section
= 0;
195 obj_elf_write_symbol_p (sym
)
198 /* If this is a local symbol, are there any relocations for which
201 /* To find this out, we examine all relocations in all bfd sections
202 that have relocations. If there is one that references this
203 symbol, we need to keep this symbol. In this case, we return a
204 true status. In all other cases, we return a false status. */
206 if (S_IS_LOCAL (sym
))
208 asymbol
*bsym
= sym
->bsym
;
209 bfd
*abfd
= bsym
->the_bfd
;
212 for (bsec
= abfd
->sections
; bsec
; bsec
= bsec
->next
)
214 struct reloc_cache_entry
**rlocs
= bsec
->orelocation
;
215 int rcnt
= bsec
->reloc_count
;
221 for (i
= 0; i
< rcnt
; i
++)
222 if (rlocs
[i
]->sym_ptr_ptr
223 && rlocs
[i
]->sym_ptr_ptr
[0] == bsym
)
228 /* No relocations for this section. Check the seg_info
229 structure to see if there are any fixups for this
231 segment_info_type
*seginfo
= seg_info (bsec
);
234 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
235 if ((fixp
->fx_addsy
&& fixp
->fx_addsy
->bsym
== bsym
)
236 || (fixp
->fx_subsy
&& fixp
->fx_subsy
->bsym
== bsym
))
245 obj_elf_write_symbol (sym
)
248 return /* obj_elf_write_symbol_p (sym) || */ !S_IS_LOCAL (sym
);
252 obj_elf_frob_symbol (sym
, punt
)
256 return obj_elf_write_symbol_p (sym
);
262 /* Assume delimiter is part of expression. BSD4.2 as fails with
263 delightful bug, so we are not being incompatible here. */
264 new_logical_line ((char *) NULL
, (int) (get_absolute_expression ()));
265 demand_empty_rest_of_line ();
271 * Handle .stabX directives, which used to be open-coded.
272 * So much creeping featurism overloaded the semantics that we decided
273 * to put all .stabX thinking in one place. Here.
275 * We try to make any .stabX directive legal. Other people's AS will often
276 * do assembly-time consistency checks: eg assigning meaning to n_type bits
277 * and "protecting" you from setting them to certain values. (They also zero
278 * certain bits before emitting symbols. Tut tut.)
280 * If an expression is not absolute we either gripe or use the relocation
281 * information. Other people's assemblers silently forget information they
282 * don't need and invent information they need that you didn't supply.
284 * .stabX directives always make a symbol table entry. It may be junk if
285 * the rest of your .stabX directive is malformed.
289 * elf_stab_symbol_string()
291 * Build a string dictionary entry for a .stabX symbol.
292 * The symbol is added to the .stabstr section.
297 elf_stab_symbol_string (string
, secname
)
298 char *string
, *secname
;
304 unsigned int old_gdb_string_index
;
308 /* @@FIXME -- there should be no static data here!
309 This also has the effect of making all stab string tables large enough
310 to contain all the contents written to any of them. This only matters
311 with the Solaris native compiler for the moment, but it should be fixed
313 static unsigned int gdb_string_index
= 0;
315 old_gdb_string_index
= 0;
316 length
= strlen (string
);
317 clengthP
= (char *) &length
;
319 { /* Ordinary case. */
321 save_subseg
= now_subseg
;
323 /* Create the stab sections, if they are not already created. */
325 char *newsecname
= xmalloc (strlen (secname
) + 4);
326 strcpy (newsecname
, secname
);
327 strcat (newsecname
, "str");
328 seg
= bfd_get_section_by_name (stdoutput
, newsecname
);
331 seg
= bfd_make_section_old_way (stdoutput
, newsecname
);
332 bfd_set_section_flags (stdoutput
, seg
, SEC_READONLY
| SEC_ALLOC
);
334 /* free (newsecname);*/
336 subseg_new ((char *) seg
->name
, save_subseg
);
337 old_gdb_string_index
= gdb_string_index
;
339 while ((c
= *string
++))
343 FRAG_APPEND_1_CHAR (c
);
346 FRAG_APPEND_1_CHAR ((char) 0);
352 FRAG_APPEND_1_CHAR ((char) 0);
356 subseg_new ((char *) save_seg
->name
, save_subseg
);
359 return old_gdb_string_index
;
363 DEFUN (elf_stab_symbol
, (symbolP
, stab_type
),
370 /* the string index portion of the stab */
371 md_number_to_chars (toP
, (valueT
) symbolP
->sy_name_offset
, 4);
372 md_number_to_chars (toP
+ 4, (valueT
) S_GET_TYPE (symbolP
), 1);
373 md_number_to_chars (toP
+ 5, (valueT
) S_GET_OTHER (symbolP
), 1);
374 md_number_to_chars (toP
+ 6, (valueT
) S_GET_DESC (symbolP
), 2);
375 /* The n_value field doesn't get written here, it gets done below. It
376 may be an expression needing relocating. */
380 obj_elf_stab_generic (what
, secname
)
386 symbolS
*symbolP
= 0;
392 asection
*saved_seg
= now_seg
;
394 subsegT subseg
= now_subseg
;
397 /* This function doesn't work yet.
399 Actually, this function is okay, but some finalizations are needed
400 before writing the object file; that's not done yet, and the Solaris
401 linker chokes without it.
403 In any case, this should effectively disable it for now. */
405 demand_copy_C_string (&length
);
410 seg
= bfd_get_section_by_name (stdoutput
, secname
);
413 seg
= subseg_new (secname
, 0);
414 bfd_set_section_flags (stdoutput
, seg
,
415 SEC_READONLY
| SEC_ALLOC
| SEC_RELOC
);
416 subseg_set (saved_seg
, subseg
);
420 * Enter with input_line_pointer pointing past .stabX and any following
425 string
= demand_copy_C_string (&length
);
427 if (*input_line_pointer
== ',')
428 input_line_pointer
++;
431 as_bad ("I need a comma after symbol's name");
439 * Input_line_pointer->after ','. String->symbol name.
443 symbolP
= symbol_new (string
, &bfd_und_section
, (valueT
) 0, (struct frag
*) 0);
445 /* enter the string in the .stab string table (section .stabstr) */
446 symbolP
->sy_name_offset
= elf_stab_symbol_string (string
, secname
);
451 S_SET_NAME (symbolP
, NULL
); /* .stabd feature. */
452 S_SET_VALUE (symbolP
,
453 (valueT
) ((char*) obstack_next_free (&frags
) - frag_now
->fr_literal
));
454 S_SET_SEGMENT (symbolP
, now_seg
);
455 symbolP
->sy_frag
= frag_now
;
459 symbolP
->sy_frag
= &zero_address_frag
;
463 symbolP
->sy_frag
= &zero_address_frag
;
471 if (get_absolute_expression_and_terminator (&longint
) == ',')
473 saved_type
= longint
;
474 S_SET_TYPE (symbolP
, saved_type
);
478 as_bad ("I want a comma after the n_type expression");
480 input_line_pointer
--; /* Backup over a non-',' char. */
486 if (get_absolute_expression_and_terminator (&longint
) == ',')
487 S_SET_OTHER (symbolP
, longint
);
490 as_bad ("I want a comma after the n_other expression");
492 input_line_pointer
--; /* Backup over a non-',' char. */
498 S_SET_DESC (symbolP
, get_absolute_expression ());
499 if (what
== 's' || what
== 'n')
501 if (*input_line_pointer
!= ',')
503 as_bad ("I want a comma after the n_desc expression");
508 input_line_pointer
++;
515 subseg_new ((char *) seg
->name
, subseg
);
517 /* Emit the stab symbol. */
518 elf_stab_symbol (symbolP
, what
);
520 if (what
== 's' || what
== 'n')
523 input_line_pointer
--;
527 char *p
= frag_more (4);
528 md_number_to_chars (p
, 0, 0);
530 subseg_new ((char *) saved_seg
->name
, subseg
);
532 if ((what
== 's' || what
== 'n')
533 && symbolP
->sy_value
.X_op
== O_constant
)
535 /* symbol is not needed in the regular symbol table */
536 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
542 if (listing
&& !goof
)
543 switch (S_GET_TYPE (symbolP
))
546 listing_source_line (S_GET_DESC (symbolP
));
550 listing_source_file (string
);
556 ignore_rest_of_line ();
558 demand_empty_rest_of_line ();
565 obj_elf_stab_generic (what
, ".stab");
575 secname
= demand_copy_C_string (&length
);
577 if (*input_line_pointer
== ',')
578 input_line_pointer
++;
581 as_bad ("comma missing in .xstabs");
582 ignore_rest_of_line ();
585 obj_elf_stab_generic (what
, secname
);
597 /* Frob invented at RMS' request. Set the n_desc of a symbol. */
598 name
= input_line_pointer
;
599 c
= get_symbol_end ();
600 p
= input_line_pointer
;
603 if (*input_line_pointer
!= ',')
606 as_bad ("Expected comma after name \"%s\"", name
);
608 ignore_rest_of_line ();
612 input_line_pointer
++;
613 temp
= get_absolute_expression ();
615 symbolP
= symbol_find_or_make (name
);
617 S_SET_DESC (symbolP
, temp
);
619 demand_empty_rest_of_line ();
620 } /* obj_elf_desc() */
623 obj_read_begin_hook ()
628 obj_symbol_new_hook (symbolP
)
631 #if 0 /* BFD already takes care of this */
632 elf32_symbol_type
*esym
= (elf32_symbol_type
*) symbolP
;
634 /* There is an Elf_Internal_Sym and an Elf_External_Sym. For now,
635 just zero them out. */
637 bzero ((char *) &esym
->internal_elf_sym
, sizeof (esym
->internal_elf_sym
));
638 bzero ((char *) &esym
->native_elf_sym
, sizeof (esym
->native_elf_sym
));
639 bzero ((char *) &esym
->tc_data
, sizeof (esym
->tc_data
));
650 asection
*seg
= now_seg
;
651 subsegT subseg
= now_subseg
;
652 Elf_Internal_Note i_note
;
653 Elf_External_Note e_note
;
654 asection
*note_secp
= (asection
*) NULL
;
658 if (*input_line_pointer
== '\"')
660 ++input_line_pointer
; /* -> 1st char of string. */
661 name
= input_line_pointer
;
663 while (is_a_char (c
= next_char_of_string ()))
665 c
= *input_line_pointer
;
666 *input_line_pointer
= '\0';
667 *(input_line_pointer
- 1) = '\0';
668 *input_line_pointer
= c
;
670 /* create the .note section if this is the first version string */
672 note_secp
= bfd_get_section_by_name (stdoutput
, ".note");
673 if (note_secp
== (asection
*) NULL
)
675 note_secp
= bfd_make_section_old_way (stdoutput
, ".note");
676 bfd_set_section_flags (stdoutput
,
678 SEC_LOAD
| SEC_ALLOC
| SEC_HAS_CONTENTS
);
681 /* process the version string */
683 subseg_new ((char *) note_secp
->name
, 0);
686 i_note
.namesz
= ((len
+ 1) + 3) & ~3; /* round this to word boundary */
687 i_note
.descsz
= 0; /* no description */
688 i_note
.type
= NT_VERSION
;
689 p
= frag_more (sizeof (e_note
.namesz
));
690 md_number_to_chars (p
, (valueT
) i_note
.namesz
, 4);
691 p
= frag_more (sizeof (e_note
.descsz
));
692 md_number_to_chars (p
, (valueT
) i_note
.descsz
, 4);
693 p
= frag_more (sizeof (e_note
.type
));
694 md_number_to_chars (p
, (valueT
) i_note
.type
, 4);
696 for (i
= 0; i
< len
; i
++)
700 FRAG_APPEND_1_CHAR (ch
);
705 subseg_new ((char *) seg
->name
, subseg
);
709 as_bad ("Expected \"-ed string");
711 demand_empty_rest_of_line ();
717 char *name
= input_line_pointer
;
718 char c
= get_symbol_end ();
723 p
= input_line_pointer
;
726 if (*input_line_pointer
!= ',')
729 as_bad ("expected comma after name `%s' in .size directive", name
);
731 ignore_rest_of_line ();
734 input_line_pointer
++;
736 if (exp
.X_op
== O_absent
)
738 as_bad ("missing expression in .size directive");
739 exp
.X_op
= O_constant
;
740 exp
.X_add_number
= 0;
743 sym
= symbol_find_or_make (name
);
745 if (exp
.X_op
== O_constant
)
746 S_SET_SIZE (sym
, exp
.X_add_number
);
753 as_tsktsk (".size expressions not yet supported, ignored");
758 demand_empty_rest_of_line ();
764 char *name
= input_line_pointer
;
765 char c
= get_symbol_end ();
770 p
= input_line_pointer
;
773 if (*input_line_pointer
!= ',')
775 as_bad ("expected comma after name in .type directive");
777 ignore_rest_of_line ();
780 input_line_pointer
++;
782 if (*input_line_pointer
!= '#')
784 as_bad ("expected `#' after comma in .type directive");
787 input_line_pointer
++;
788 if (!strncmp ("function", input_line_pointer
, sizeof ("function") - 1))
791 input_line_pointer
+= sizeof ("function") - 1;
793 else if (!strncmp ("object", input_line_pointer
, sizeof ("object") - 1))
795 input_line_pointer
+= sizeof ("object") - 1;
799 as_bad ("unrecognized symbol type, ignored");
802 demand_empty_rest_of_line ();
804 sym
= symbol_find_or_make (name
);
805 sym
->bsym
->flags
|= type
;
811 static segT comment_section
;
812 segT old_section
= now_seg
;
813 int old_subsection
= now_subseg
;
815 if (!comment_section
)
818 comment_section
= subseg_new (".comment", 0);
819 bfd_set_section_flags (stdoutput
, comment_section
, SEC_HAS_CONTENTS
);
824 subseg_set (comment_section
, 0);
826 subseg_set (old_section
, old_subsection
);
835 for (i
= 0; i
< stdoutput
->symcount
; i
++)
836 elf_tc_symbol (stdoutput
, (elf_symbol_type
*) (stdoutput
->outsymbols
[i
]), i
+ 1);
838 #ifdef elf_tc_final_processing
839 elf_tc_final_processing_hook ();
842 /* Finally, we must make any target-specific sections. */
844 #ifdef elf_tc_make_sections
845 elf_tc_make_sections (stdoutput
, NULL
);