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));
35 const pseudo_typeS obj_pseudo_table
[] =
37 {"ident", obj_elf_ident
, 0},
38 {"section", obj_elf_section
, 0},
39 {"size", obj_elf_size
, 0},
40 {"type", obj_elf_type
, 0},
41 {"version", obj_elf_version
, 0},
43 /* These are used for stabs-in-elf configurations. */
44 {"desc", obj_elf_desc
, 0},
45 {"line", obj_elf_line
, 0},
46 {"stabd", obj_elf_stab
, 'd'},
47 {"stabn", obj_elf_stab
, 'n'},
48 {"stabs", obj_elf_stab
, 's'},
49 /* This is used on Solaris 2.x on SPARC, but not supported yet. */
50 {"xstabs", obj_elf_xstab
, 's'},
52 {NULL
} /* end sentinel */
55 #include "aout/aout64.h"
63 sym
= symbol_new (s
, absolute_section
, (valueT
) 0, (struct frag
*) 0);
64 sym
->sy_frag
= &zero_address_frag
;
65 sym
->bsym
->flags
|= BSF_FILE
;
67 if (symbol_rootP
!= sym
)
69 symbol_remove (sym
, &symbol_rootP
, &symbol_lastP
);
70 symbol_insert (sym
, symbol_rootP
, &symbol_rootP
, &symbol_lastP
);
72 verify_symbol_chain (symbol_rootP
, symbol_lastP
);
84 /* Initialize this with inclusive-or of all flags that can be cleared
85 by attributes, but not set by them. Also include flags that won't
86 get set properly in the assembler, but which the user/compiler
87 shouldn't be expected to set. */
88 flagword flags
= SEC_READONLY
| SEC_ALLOC
| SEC_RELOC
;
89 /* Initialize this with the default flags to be used if none are
91 flagword default_flags
= SEC_ALLOC
| SEC_RELOC
;
93 string
= demand_copy_C_string (&xxx
);
95 if (*input_line_pointer
!= ',')
96 flags
= default_flags
;
97 while (*input_line_pointer
== ',')
103 input_line_pointer
++;
104 if (*input_line_pointer
!= '#' && *input_line_pointer
!= '@')
106 as_bad ("unrecognized syntax in .section command");
107 ignore_rest_of_line ();
110 input_line_pointer
++;
112 #define CHECK(X,BIT,NEG) \
113 if (!strncmp(X,input_line_pointer,len = sizeof(X) - 1)) { \
114 bit = BIT; inv = NEG; goto match; }
116 CHECK ("write", SEC_READONLY
, 1);
117 CHECK ("alloc", SEC_ALLOC
, 0);
118 CHECK ("execinstr", SEC_CODE
, 1);
121 p
= input_line_pointer
;
122 while (!is_end_of_line
[*p
] && *p
!= 0 && *p
!= ',')
126 as_bad ("unrecognized section attribute `%s' ignored",
136 input_line_pointer
+= len
;
138 demand_empty_rest_of_line ();
140 sec
= bfd_get_section_by_name (stdoutput
, string
);
143 sec
= subseg_new (string
, 0);
144 bfd_set_section_flags (stdoutput
, sec
, flags
);
145 sec
->output_section
= sec
;
151 obj_elf_write_symbol_p (sym
)
154 /* If this is a local symbol, are there any relocations for which
157 /* To find this out, we examine all relocations in all bfd sections
158 that have relocations. If there is one that references this
159 symbol, we need to keep this symbol. In this case, we return a
160 true status. In all other cases, we return a false status. */
162 if (S_IS_LOCAL (sym
))
164 asymbol
*bsym
= sym
->bsym
;
165 bfd
*abfd
= bsym
->the_bfd
;
168 for (bsec
= abfd
->sections
; bsec
; bsec
= bsec
->next
)
170 struct reloc_cache_entry
**rlocs
= bsec
->orelocation
;
171 int rcnt
= bsec
->reloc_count
;
177 for (i
= 0; i
< rcnt
; i
++)
178 if (rlocs
[i
]->sym_ptr_ptr
179 && rlocs
[i
]->sym_ptr_ptr
[0] == bsym
)
184 /* No relocations for this section. Check the seg_info
185 structure to see if there are any fixups for this
187 segment_info_type
*seginfo
= seg_info (bsec
);
190 for (fixp
= seginfo
->fix_root
; fixp
; fixp
= fixp
->fx_next
)
191 if ((fixp
->fx_addsy
&& fixp
->fx_addsy
->bsym
== bsym
)
192 || (fixp
->fx_subsy
&& fixp
->fx_subsy
->bsym
== bsym
))
201 obj_elf_write_symbol (sym
)
204 return /* obj_elf_write_symbol_p (sym) || */ !S_IS_LOCAL (sym
);
208 obj_elf_frob_symbol (sym
, punt
)
212 return obj_elf_write_symbol_p (sym
);
218 /* Assume delimiter is part of expression. BSD4.2 as fails with
219 delightful bug, so we are not being incompatible here. */
220 new_logical_line ((char *) NULL
, (int) (get_absolute_expression ()));
221 demand_empty_rest_of_line ();
227 * Handle .stabX directives, which used to be open-coded.
228 * So much creeping featurism overloaded the semantics that we decided
229 * to put all .stabX thinking in one place. Here.
231 * We try to make any .stabX directive legal. Other people's AS will often
232 * do assembly-time consistency checks: eg assigning meaning to n_type bits
233 * and "protecting" you from setting them to certain values. (They also zero
234 * certain bits before emitting symbols. Tut tut.)
236 * If an expression is not absolute we either gripe or use the relocation
237 * information. Other people's assemblers silently forget information they
238 * don't need and invent information they need that you didn't supply.
240 * .stabX directives always make a symbol table entry. It may be junk if
241 * the rest of your .stabX directive is malformed.
245 * elf_stab_symbol_string()
247 * Build a string dictionary entry for a .stabX symbol.
248 * The symbol is added to the .stabstr section.
253 elf_stab_symbol_string (string
, secname
)
254 char *string
, *secname
;
260 unsigned int old_gdb_string_index
;
264 /* @@FIXME -- there should be no static data here!
265 This also has the effect of making all stab string tables large enough
266 to contain all the contents written to any of them. This only matters
267 with the Solaris native compiler for the moment, but it should be fixed
269 static unsigned int gdb_string_index
= 0;
271 old_gdb_string_index
= 0;
272 length
= strlen (string
);
273 clengthP
= (char *) &length
;
275 { /* Ordinary case. */
277 save_subseg
= now_subseg
;
279 /* Create the stab sections, if they are not already created. */
281 char *newsecname
= xmalloc (strlen (secname
) + 4);
282 strcpy (newsecname
, secname
);
283 strcat (newsecname
, "str");
284 seg
= bfd_get_section_by_name (stdoutput
, newsecname
);
287 seg
= bfd_make_section_old_way (stdoutput
, newsecname
);
288 bfd_set_section_flags (stdoutput
, seg
, SEC_READONLY
| SEC_ALLOC
);
290 /* free (newsecname);*/
292 subseg_new ((char *) seg
->name
, save_subseg
);
293 old_gdb_string_index
= gdb_string_index
;
295 while ((c
= *string
++))
299 FRAG_APPEND_1_CHAR (c
);
302 FRAG_APPEND_1_CHAR ((char) 0);
308 FRAG_APPEND_1_CHAR ((char) 0);
312 subseg_new ((char *) save_seg
->name
, save_subseg
);
315 return old_gdb_string_index
;
319 DEFUN (elf_stab_symbol
, (symbolP
, stab_type
),
326 /* the string index portion of the stab */
327 md_number_to_chars (toP
, (valueT
) symbolP
->sy_name_offset
, 4);
328 md_number_to_chars (toP
+ 4, (valueT
) S_GET_TYPE (symbolP
), 1);
329 md_number_to_chars (toP
+ 5, (valueT
) S_GET_OTHER (symbolP
), 1);
330 md_number_to_chars (toP
+ 6, (valueT
) S_GET_DESC (symbolP
), 2);
331 /* The n_value field doesn't get written here, it gets done below. It
332 may be an expression needing relocating. */
336 obj_elf_stab_generic (what
, secname
)
342 symbolS
*symbolP
= 0;
348 asection
*saved_seg
= now_seg
;
350 subsegT subseg
= now_subseg
;
353 /* This function doesn't work yet.
355 Actually, this function is okay, but some finalizations are needed
356 before writing the object file; that's not done yet, and the Solaris
357 linker chokes without it.
359 In any case, this should effectively disable it for now. */
361 demand_copy_C_string (&length
);
366 seg
= bfd_get_section_by_name (stdoutput
, secname
);
369 seg
= subseg_new (secname
, 0);
370 bfd_set_section_flags (stdoutput
, seg
,
371 SEC_READONLY
| SEC_ALLOC
| SEC_RELOC
);
372 subseg_set (saved_seg
, subseg
);
376 * Enter with input_line_pointer pointing past .stabX and any following
381 string
= demand_copy_C_string (&length
);
383 if (*input_line_pointer
== ',')
384 input_line_pointer
++;
387 as_bad ("I need a comma after symbol's name");
395 * Input_line_pointer->after ','. String->symbol name.
399 symbolP
= symbol_new (string
, &bfd_und_section
, (valueT
) 0, (struct frag
*) 0);
401 /* enter the string in the .stab string table (section .stabstr) */
402 symbolP
->sy_name_offset
= elf_stab_symbol_string (string
, secname
);
407 S_SET_NAME (symbolP
, NULL
); /* .stabd feature. */
408 S_SET_VALUE (symbolP
,
409 (valueT
) ((char*) obstack_next_free (&frags
) - frag_now
->fr_literal
));
410 S_SET_SEGMENT (symbolP
, now_seg
);
411 symbolP
->sy_frag
= frag_now
;
415 symbolP
->sy_frag
= &zero_address_frag
;
419 symbolP
->sy_frag
= &zero_address_frag
;
427 if (get_absolute_expression_and_terminator (&longint
) == ',')
429 saved_type
= longint
;
430 S_SET_TYPE (symbolP
, saved_type
);
434 as_bad ("I want a comma after the n_type expression");
436 input_line_pointer
--; /* Backup over a non-',' char. */
442 if (get_absolute_expression_and_terminator (&longint
) == ',')
443 S_SET_OTHER (symbolP
, longint
);
446 as_bad ("I want a comma after the n_other expression");
448 input_line_pointer
--; /* Backup over a non-',' char. */
454 S_SET_DESC (symbolP
, get_absolute_expression ());
455 if (what
== 's' || what
== 'n')
457 if (*input_line_pointer
!= ',')
459 as_bad ("I want a comma after the n_desc expression");
464 input_line_pointer
++;
471 subseg_new ((char *) seg
->name
, subseg
);
473 /* Emit the stab symbol. */
474 elf_stab_symbol (symbolP
, what
);
476 if (what
== 's' || what
== 'n')
479 input_line_pointer
--;
483 char *p
= frag_more (4);
484 md_number_to_chars (p
, 0, 0);
486 subseg_new ((char *) saved_seg
->name
, subseg
);
488 if ((what
== 's' || what
== 'n')
489 && symbolP
->sy_forward
== NULL
)
491 /* symbol is not needed in the regular symbol table */
492 symbol_remove (symbolP
, &symbol_rootP
, &symbol_lastP
);
498 if (listing
&& !goof
)
499 switch (S_GET_TYPE (symbolP
))
502 listing_source_line (S_GET_DESC (symbolP
));
506 listing_source_file (string
);
512 ignore_rest_of_line ();
514 demand_empty_rest_of_line ();
521 obj_elf_stab_generic (what
, ".stab");
531 secname
= demand_copy_C_string (&length
);
533 if (*input_line_pointer
== ',')
534 input_line_pointer
++;
537 as_bad ("comma missing in .xstabs");
538 ignore_rest_of_line ();
541 obj_elf_stab_generic (what
, secname
);
553 /* Frob invented at RMS' request. Set the n_desc of a symbol. */
554 name
= input_line_pointer
;
555 c
= get_symbol_end ();
556 p
= input_line_pointer
;
559 if (*input_line_pointer
!= ',')
562 as_bad ("Expected comma after name \"%s\"", name
);
564 ignore_rest_of_line ();
568 input_line_pointer
++;
569 temp
= get_absolute_expression ();
571 symbolP
= symbol_find_or_make (name
);
573 S_SET_DESC (symbolP
, temp
);
575 demand_empty_rest_of_line ();
576 } /* obj_elf_desc() */
579 obj_read_begin_hook ()
584 obj_symbol_new_hook (symbolP
)
587 #if 0 /* BFD already takes care of this */
588 elf32_symbol_type
*esym
= (elf32_symbol_type
*) symbolP
;
590 /* There is an Elf_Internal_Sym and an Elf_External_Sym. For now,
591 just zero them out. */
593 bzero ((char *) &esym
->internal_elf_sym
, sizeof (esym
->internal_elf_sym
));
594 bzero ((char *) &esym
->native_elf_sym
, sizeof (esym
->native_elf_sym
));
595 bzero ((char *) &esym
->tc_data
, sizeof (esym
->tc_data
));
606 asection
*seg
= now_seg
;
607 subsegT subseg
= now_subseg
;
608 Elf_Internal_Note i_note
;
609 Elf_External_Note e_note
;
610 asection
*note_secp
= (asection
*) NULL
;
614 if (*input_line_pointer
== '\"')
616 ++input_line_pointer
; /* -> 1st char of string. */
617 name
= input_line_pointer
;
619 while (is_a_char (c
= next_char_of_string ()))
621 c
= *input_line_pointer
;
622 *input_line_pointer
= '\0';
623 *(input_line_pointer
- 1) = '\0';
624 *input_line_pointer
= c
;
626 /* create the .note section if this is the first version string */
628 note_secp
= bfd_get_section_by_name (stdoutput
, ".note");
629 if (note_secp
== (asection
*) NULL
)
631 note_secp
= bfd_make_section_old_way (stdoutput
, ".note");
632 bfd_set_section_flags (stdoutput
,
634 SEC_LOAD
| SEC_ALLOC
| SEC_HAS_CONTENTS
);
637 /* process the version string */
639 subseg_new ((char *) note_secp
->name
, 0);
642 i_note
.namesz
= ((len
+ 1) + 3) & ~3; /* round this to word boundary */
643 i_note
.descsz
= 0; /* no description */
644 i_note
.type
= NT_VERSION
;
645 p
= frag_more (sizeof (e_note
.namesz
));
646 md_number_to_chars (p
, (valueT
) i_note
.namesz
, 4);
647 p
= frag_more (sizeof (e_note
.descsz
));
648 md_number_to_chars (p
, (valueT
) i_note
.descsz
, 4);
649 p
= frag_more (sizeof (e_note
.type
));
650 md_number_to_chars (p
, (valueT
) i_note
.type
, 4);
652 for (i
= 0; i
< len
; i
++)
656 FRAG_APPEND_1_CHAR (ch
);
661 subseg_new ((char *) seg
->name
, subseg
);
665 as_bad ("Expected \"-ed string");
667 demand_empty_rest_of_line ();
673 char *name
= input_line_pointer
;
674 char c
= get_symbol_end ();
680 p
= input_line_pointer
;
683 if (*input_line_pointer
!= ',')
686 as_bad ("expected comma after name `%s' in .size directive", name
);
688 ignore_rest_of_line ();
691 input_line_pointer
++;
692 sec
= expression (&exp
);
693 if (sec
== absent_section
)
695 as_bad ("missing expression in .size directive");
696 exp
.X_seg
= absolute_section
;
697 exp
.X_add_number
= 0;
700 sym
= symbol_find_or_make (name
);
702 if (sec
== absolute_section
)
703 S_SET_SIZE (sym
, exp
.X_add_number
);
710 as_tsktsk (".size expressions not yet supported, ignored");
715 demand_empty_rest_of_line ();
721 char *name
= input_line_pointer
;
722 char c
= get_symbol_end ();
727 p
= input_line_pointer
;
730 if (*input_line_pointer
!= ',')
732 as_bad ("expected comma after name in .type directive");
734 ignore_rest_of_line ();
737 input_line_pointer
++;
739 if (*input_line_pointer
!= '#')
741 as_bad ("expected `#' after comma in .type directive");
744 input_line_pointer
++;
745 if (!strncmp ("function", input_line_pointer
, sizeof ("function") - 1))
748 input_line_pointer
+= sizeof ("function") - 1;
750 else if (!strncmp ("object", input_line_pointer
, sizeof ("object") - 1))
752 input_line_pointer
+= sizeof ("object") - 1;
756 as_bad ("unrecognized symbol type, ignored");
759 demand_empty_rest_of_line ();
761 sym
= symbol_find_or_make (name
);
762 sym
->bsym
->flags
|= type
;
768 static segT comment_section
;
769 segT old_section
= now_seg
;
770 int old_subsection
= now_subseg
;
772 if (!comment_section
)
775 comment_section
= subseg_new (".comment", 0);
776 bfd_set_section_flags (stdoutput
, comment_section
, SEC_HAS_CONTENTS
);
781 subseg_set (comment_section
, 0);
783 subseg_set (old_section
, old_subsection
);
792 for (i
= 0; i
< stdoutput
->symcount
; i
++)
793 elf_tc_symbol (stdoutput
, (elf_symbol_type
*) (stdoutput
->outsymbols
[i
]), i
+ 1);
795 #ifdef elf_tc_final_processing
796 elf_tc_final_processing_hook ();
799 /* Finally, we must make any target-specific sections. */
801 #ifdef elf_tc_make_sections
802 elf_tc_make_sections (stdoutput
, NULL
);