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. */
24 static int obj_elf_write_symbol_p PARAMS ((symbolS *sym));
26 static void obj_elf_line PARAMS ((int));
27 void obj_elf_version PARAMS ((int));
28 static void obj_elf_size PARAMS ((int));
29 static void obj_elf_type PARAMS ((int));
30 static void obj_elf_ident PARAMS ((int));
31 static void obj_elf_weak PARAMS ((int));
32 static void obj_elf_local PARAMS ((int));
33 static void obj_elf_common PARAMS ((int));
34 static void obj_elf_data PARAMS ((int));
35 static void obj_elf_text PARAMS ((int));
37 const pseudo_typeS obj_pseudo_table[] =
39 {"comm", obj_elf_common, 0},
40 {"ident", obj_elf_ident, 0},
41 {"local", obj_elf_local, 0},
42 {"previous", obj_elf_previous, 0},
43 {"section", obj_elf_section, 0},
44 {"size", obj_elf_size, 0},
45 {"type", obj_elf_type, 0},
46 {"version", obj_elf_version, 0},
47 {"weak", obj_elf_weak, 0},
49 /* These are used for stabs-in-elf configurations. */
50 {"line", obj_elf_line, 0},
52 /* These are used for dwarf. */
57 /* We need to trap the section changing calls to handle .previous. */
58 {"data", obj_elf_data, 0},
59 {"text", obj_elf_text, 0},
61 {NULL} /* end sentinel */
65 #include "aout/aout64.h"
73 sym = symbol_new (s, absolute_section, (valueT) 0, (struct frag *) 0);
74 sym->sy_frag = &zero_address_frag;
75 sym->bsym->flags |= BSF_FILE;
77 if (symbol_rootP != sym)
79 symbol_remove (sym, &symbol_rootP, &symbol_lastP);
80 symbol_insert (sym, symbol_rootP, &symbol_rootP, &symbol_lastP);
82 verify_symbol_chain (symbol_rootP, symbol_lastP);
88 obj_elf_common (ignore)
98 name = input_line_pointer;
99 c = get_symbol_end ();
100 /* just after name is now '\0' */
101 p = input_line_pointer;
104 if (*input_line_pointer != ',')
106 as_bad ("Expected comma after symbol-name");
107 ignore_rest_of_line ();
110 input_line_pointer++; /* skip ',' */
111 if ((temp = get_absolute_expression ()) < 0)
113 as_bad (".COMMon length (%d.) <0! Ignored.", temp);
114 ignore_rest_of_line ();
119 symbolP = symbol_find_or_make (name);
121 if (S_IS_DEFINED (symbolP))
123 as_bad ("Ignoring attempt to re-define symbol");
124 ignore_rest_of_line ();
127 if (S_GET_VALUE (symbolP) != 0)
129 if (S_GET_VALUE (symbolP) != size)
131 as_warn ("Length of .comm \"%s\" is already %ld. Not changed to %d.",
132 S_GET_NAME (symbolP), (long) S_GET_VALUE (symbolP), size);
135 know (symbolP->sy_frag == &zero_address_frag);
136 if (*input_line_pointer != ',')
141 input_line_pointer++;
144 if (! have_align || *input_line_pointer != '"')
150 temp = get_absolute_expression ();
154 as_warn ("Common alignment negative; 0 assumed");
166 old_subsec = now_subseg;
168 record_alignment (bss_section, align);
169 subseg_set (bss_section, 0);
171 frag_align (align, 0);
172 if (S_GET_SEGMENT (symbolP) == bss_section)
173 symbolP->sy_frag->fr_symbol = 0;
174 symbolP->sy_frag = frag_now;
175 pfrag = frag_var (rs_org, 1, 1, (relax_substateT) 0, symbolP, size,
178 S_SET_SEGMENT (symbolP, bss_section);
179 S_CLEAR_EXTERNAL (symbolP);
180 subseg_set (old_sec, old_subsec);
185 S_SET_VALUE (symbolP, (valueT) size);
186 S_SET_EXTERNAL (symbolP);
187 /* should be common, but this is how gas does it for now */
188 S_SET_SEGMENT (symbolP, &bfd_und_section);
193 input_line_pointer++;
194 /* @@ Some use the dot, some don't. Can we get some consistency?? */
195 if (*input_line_pointer == '.')
196 input_line_pointer++;
197 /* @@ Some say data, some say bss. */
198 if (strncmp (input_line_pointer, "bss\"", 4)
199 && strncmp (input_line_pointer, "data\"", 5))
201 while (*--input_line_pointer != '"')
203 input_line_pointer--;
204 goto bad_common_segment;
206 while (*input_line_pointer++ != '"')
208 goto allocate_common;
210 demand_empty_rest_of_line ();
215 p = input_line_pointer;
216 while (*p && *p != '\n')
220 as_bad ("bad .common segment %s", input_line_pointer + 1);
222 input_line_pointer = p;
223 ignore_rest_of_line ();
229 obj_elf_local (ignore)
238 name = input_line_pointer;
239 c = get_symbol_end ();
240 symbolP = symbol_find_or_make (name);
241 *input_line_pointer = c;
243 S_CLEAR_EXTERNAL (symbolP);
247 input_line_pointer++;
249 if (*input_line_pointer == '\n')
254 demand_empty_rest_of_line ();
258 obj_elf_weak (ignore)
267 name = input_line_pointer;
268 c = get_symbol_end ();
269 symbolP = symbol_find_or_make (name);
270 *input_line_pointer = c;
272 S_SET_WEAK (symbolP);
276 input_line_pointer++;
278 if (*input_line_pointer == '\n')
283 demand_empty_rest_of_line ();
286 static segT previous_section;
287 static int previous_subsection;
289 /* Handle the .section pseudo-op. This code supports two different
292 The first is found on Solaris, and looks like
293 .section ".sec1",#alloc,#execinstr,#write
294 Here the names after '#' are the SHF_* flags to turn on for the
295 section. I'm not sure how it determines the SHT_* type (BFD
296 doesn't really give us control over the type, anyhow).
298 The second format is found on UnixWare, and probably most SVR4
299 machines, and looks like
300 .section .sec1,"a",@progbits
301 The quoted string may contain any combination of a, w, x, and
302 represents the SHF_* flags to turn on for the section. The string
303 beginning with '@' can be progbits or nobits. There should be
304 other possibilities, but I don't know what they are. In any case,
305 BFD doesn't really let us set the section type. */
308 obj_elf_section (xxx)
316 /* Get name of section. */
318 if (*input_line_pointer == '"')
320 string = demand_copy_C_string (&xxx);
323 ignore_rest_of_line ();
329 char *p = input_line_pointer;
331 while (0 == strchr ("\n\t,; ", *p))
333 if (p == input_line_pointer)
335 as_warn ("Missing section name");
336 ignore_rest_of_line ();
341 string = xmalloc ((unsigned long) (p - input_line_pointer + 1));
342 strcpy (string, input_line_pointer);
344 input_line_pointer = p;
347 /* Switch to the section, creating it if necessary. */
348 previous_section = now_seg;
349 previous_subsection = now_subseg;
351 new_sec = bfd_get_section_by_name (stdoutput, string) == NULL;
352 sec = subseg_new (string, 0);
354 /* If this section already existed, we don't bother to change the
358 while (! is_end_of_line[(unsigned char) *input_line_pointer])
359 ++input_line_pointer;
360 ++input_line_pointer;
365 if (*input_line_pointer != ',')
367 /* No flags given. Guess at some useful defaults. */
368 if (strcmp (string, ".data") == 0
369 || strcmp (string, ".data1") == 0
370 || strcmp (string, ".sdata") == 0
371 || strcmp (string, ".rodata") == 0
372 || strcmp (string, ".rodata1") == 0)
373 flags = SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_RELOC | SEC_DATA;
374 else if (strcmp (string, ".text") == 0
375 || strcmp (string, ".init") == 0
376 || strcmp (string, ".fini") == 0)
377 flags = SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_RELOC | SEC_CODE;
378 else if (strcmp (string, ".bss") == 0
379 || strcmp (string, ".sbss") == 0)
386 /* Skip the comma. */
387 ++input_line_pointer;
390 if (*input_line_pointer == '"')
392 /* Pick up a string with a combination of a, w, x. */
393 flags = SEC_READONLY | SEC_RELOC;
394 ++input_line_pointer;
395 while (*input_line_pointer != '"')
397 switch (*input_line_pointer)
400 flags |= SEC_ALLOC | SEC_LOAD;
403 flags &=~ SEC_READONLY;
409 as_warn ("Bad .section directive: want a,w,x in string");
410 ignore_rest_of_line ();
413 ++input_line_pointer;
416 /* Skip the closing quote. */
417 ++input_line_pointer;
420 if (*input_line_pointer == ',')
422 ++input_line_pointer;
424 if (*input_line_pointer == '@')
426 ++input_line_pointer;
427 if (strncmp (input_line_pointer, "progbits",
428 sizeof "progbits" - 1) == 0)
430 flags |= SEC_ALLOC | SEC_LOAD;
431 input_line_pointer += sizeof "progbits" - 1;
433 else if (strncmp (input_line_pointer, "nobits",
434 sizeof "nobits" - 1) == 0)
437 input_line_pointer += sizeof "nobits" - 1;
441 as_warn ("Unrecognized section type");
442 ignore_rest_of_line ();
449 flags = SEC_READONLY | SEC_RELOC;
453 if (*input_line_pointer != '#')
455 as_warn ("Bad .section directive");
456 ignore_rest_of_line ();
459 ++input_line_pointer;
460 if (strncmp (input_line_pointer, "write",
461 sizeof "write" - 1) == 0)
463 flags &=~ SEC_READONLY;
464 input_line_pointer += sizeof "write" - 1;
466 else if (strncmp (input_line_pointer, "alloc",
467 sizeof "alloc" - 1) == 0)
469 flags |= SEC_ALLOC | SEC_LOAD;
470 input_line_pointer += sizeof "alloc" - 1;
472 else if (strncmp (input_line_pointer, "execinstr",
473 sizeof "execinstr" - 1) == 0)
476 input_line_pointer += sizeof "execinstr" - 1;
480 as_warn ("Unrecognized section attribute");
481 ignore_rest_of_line ();
486 while (*input_line_pointer++ == ',');
487 --input_line_pointer;
491 bfd_set_section_flags (stdoutput, sec, flags);
493 demand_empty_rest_of_line ();
496 /* Change to the .data section. */
502 previous_section = now_seg;
503 previous_subsection = now_subseg;
507 /* Change to the .text section. */
513 previous_section = now_seg;
514 previous_subsection = now_subseg;
519 obj_elf_previous (ignore)
522 if (previous_section == 0)
524 as_bad (".previous without corresponding .section; ignored");
527 subseg_set (previous_section, previous_subsection);
528 previous_section = 0;
532 obj_elf_write_symbol_p (sym)
535 /* If this is a local symbol, are there any relocations for which
538 /* To find this out, we examine all relocations in all bfd sections
539 that have relocations. If there is one that references this
540 symbol, we need to keep this symbol. In this case, we return a
541 true status. In all other cases, we return a false status. */
543 if (S_IS_LOCAL (sym))
545 asymbol *bsym = sym->bsym;
546 bfd *abfd = bsym->the_bfd;
549 for (bsec = abfd->sections; bsec; bsec = bsec->next)
551 struct reloc_cache_entry **rlocs = bsec->orelocation;
552 int rcnt = bsec->reloc_count;
558 for (i = 0; i < rcnt; i++)
559 if (rlocs[i]->sym_ptr_ptr
560 && rlocs[i]->sym_ptr_ptr[0] == bsym)
565 /* No relocations for this section. Check the seg_info
566 structure to see if there are any fixups for this
568 segment_info_type *seginfo = seg_info (bsec);
571 for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
572 if ((fixp->fx_addsy && fixp->fx_addsy->bsym == bsym)
573 || (fixp->fx_subsy && fixp->fx_subsy->bsym == bsym))
582 obj_elf_write_symbol (sym)
585 return /* obj_elf_write_symbol_p (sym) || */ !S_IS_LOCAL (sym);
589 obj_elf_frob_symbol (sym, punt)
593 #if 0 /* ?? The return value is ignored. Only the value of *punt is
595 return obj_elf_write_symbol_p (sym);
597 /* FIXME: Just return 0 until is fixed. */
602 obj_elf_line (ignore)
605 /* Assume delimiter is part of expression. BSD4.2 as fails with
606 delightful bug, so we are not being incompatible here. */
607 new_logical_line ((char *) NULL, (int) (get_absolute_expression ()));
608 demand_empty_rest_of_line ();
612 obj_read_begin_hook ()
617 obj_symbol_new_hook (symbolP)
620 #if 0 /* BFD already takes care of this */
621 elf32_symbol_type *esym = (elf32_symbol_type *) symbolP;
623 /* There is an Elf_Internal_Sym and an Elf_External_Sym. For now,
624 just zero them out. */
626 bzero ((char *) &esym->internal_elf_sym, sizeof (esym->internal_elf_sym));
627 bzero ((char *) &esym->native_elf_sym, sizeof (esym->native_elf_sym));
628 bzero ((char *) &esym->tc_data, sizeof (esym->tc_data));
633 obj_elf_version (ignore)
640 asection *seg = now_seg;
641 subsegT subseg = now_subseg;
642 Elf_Internal_Note i_note;
643 Elf_External_Note e_note;
644 asection *note_secp = (asection *) NULL;
648 if (*input_line_pointer == '\"')
650 ++input_line_pointer; /* -> 1st char of string. */
651 name = input_line_pointer;
653 while (is_a_char (c = next_char_of_string ()))
655 c = *input_line_pointer;
656 *input_line_pointer = '\0';
657 *(input_line_pointer - 1) = '\0';
658 *input_line_pointer = c;
660 /* create the .note section */
662 note_secp = subseg_new (".note", 0);
663 bfd_set_section_flags (stdoutput,
665 SEC_HAS_CONTENTS | SEC_READONLY);
667 /* process the version string */
671 i_note.namesz = ((len + 1) + 3) & ~3; /* round this to word boundary */
672 i_note.descsz = 0; /* no description */
673 i_note.type = NT_VERSION;
674 p = frag_more (sizeof (e_note.namesz));
675 md_number_to_chars (p, (valueT) i_note.namesz, 4);
676 p = frag_more (sizeof (e_note.descsz));
677 md_number_to_chars (p, (valueT) i_note.descsz, 4);
678 p = frag_more (sizeof (e_note.type));
679 md_number_to_chars (p, (valueT) i_note.type, 4);
681 for (i = 0; i < len; i++)
685 FRAG_APPEND_1_CHAR (ch);
690 subseg_set (seg, subseg);
694 as_bad ("Expected quoted string");
696 demand_empty_rest_of_line ();
700 obj_elf_size (ignore)
703 char *name = input_line_pointer;
704 char c = get_symbol_end ();
709 p = input_line_pointer;
712 if (*input_line_pointer != ',')
715 as_bad ("expected comma after name `%s' in .size directive", name);
717 ignore_rest_of_line ();
720 input_line_pointer++;
722 if (exp.X_op == O_absent)
724 as_bad ("missing expression in .size directive");
725 exp.X_op = O_constant;
726 exp.X_add_number = 0;
729 sym = symbol_find_or_make (name);
731 if (exp.X_op == O_constant)
732 S_SET_SIZE (sym, exp.X_add_number);
739 as_tsktsk (".size expressions not yet supported, ignored");
744 demand_empty_rest_of_line ();
748 obj_elf_type (ignore)
751 char *name = input_line_pointer;
752 char c = get_symbol_end ();
757 p = input_line_pointer;
760 if (*input_line_pointer != ',')
762 as_bad ("expected comma after name in .type directive");
764 ignore_rest_of_line ();
767 input_line_pointer++;
769 if (*input_line_pointer != '#' && *input_line_pointer != '@')
771 as_bad ("expected `#' or `@' after comma in .type directive");
774 input_line_pointer++;
775 if (!strncmp ("function", input_line_pointer, sizeof ("function") - 1))
778 input_line_pointer += sizeof ("function") - 1;
780 else if (!strncmp ("object", input_line_pointer, sizeof ("object") - 1))
782 input_line_pointer += sizeof ("object") - 1;
786 as_bad ("unrecognized symbol type, ignored");
789 demand_empty_rest_of_line ();
791 sym = symbol_find_or_make (name);
792 sym->bsym->flags |= type;
796 obj_elf_ident (ignore)
799 static segT comment_section;
800 segT old_section = now_seg;
801 int old_subsection = now_subseg;
803 if (!comment_section)
806 comment_section = subseg_new (".comment", 0);
807 bfd_set_section_flags (stdoutput, comment_section,
808 SEC_READONLY | SEC_HAS_CONTENTS);
813 subseg_set (comment_section, 0);
815 subseg_set (old_section, old_subsection);
818 /* The first entry in a .stabs section is special. */
821 obj_elf_init_stab_section (seg)
828 /* Force the section to align to a longword boundary. Without this,
829 UnixWare ar crashes. */
830 bfd_set_section_alignment (stdoutput, seg, 2);
833 as_where (&file, (unsigned int *) NULL);
834 stroff = get_stab_string_offset (file, segment_name (seg));
836 md_number_to_chars (p, stroff, 4);
837 seg_info (seg)->stabu.p = p;
840 /* Fill in the counts in the first entry in a .stabs section. */
843 adjust_stab_sections (abfd, sec, xxx)
853 if (strncmp (".stab", sec->name, 5))
855 if (!strcmp ("str", sec->name + strlen (sec->name) - 3))
858 name = (char *) alloca (strlen (sec->name) + 4);
859 strcpy (name, sec->name);
860 strcat (name, "str");
861 strsec = bfd_get_section_by_name (abfd, name);
863 strsz = bfd_section_size (abfd, strsec);
866 nsyms = bfd_section_size (abfd, sec) / 12 - 1;
868 p = seg_info (sec)->stabu.p;
871 bfd_h_put_16 (abfd, (bfd_vma) nsyms, (bfd_byte *) p + 6);
872 bfd_h_put_32 (abfd, (bfd_vma) strsz, (bfd_byte *) p + 8);
878 bfd_map_over_sections (stdoutput, adjust_stab_sections, (PTR) 0);
884 for (i = 0; i < stdoutput->symcount; i++)
885 elf_tc_symbol (stdoutput, (PTR) (stdoutput->outsymbols[i]),
890 #ifdef elf_tc_final_processing
891 elf_tc_final_processing ();
894 /* Finally, we must make any target-specific sections. */
896 #ifdef elf_tc_make_sections
897 elf_tc_make_sections (stdoutput);