1 /* ELF executable support for BFD.
2 Copyright 1993 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 BFD support for ELF formats is being worked on.
26 Currently, the best supported back ends are for sparc and i386
27 (running svr4 or Solaris 2).
29 Documentation of the internals of the support code still needs
30 to be written. The code is changing quickly enough that we
40 /* Standard ELF hash function. Do not change this function; you will
41 cause invalid hash tables to be generated. (Well, you would if this
42 were being used yet.) */
44 DEFUN (bfd_elf_hash, (name),
45 CONST unsigned char *name)
51 while ((ch = *name++) != '\0')
54 if ((g = (h & 0xf0000000)) != 0)
63 /* Read a specified number of bytes at a specified offset in an ELF
64 file, into a newly allocated buffer, and return a pointer to the
68 DEFUN (elf_read, (abfd, offset, size),
75 if ((buf = bfd_alloc (abfd, size)) == NULL)
77 bfd_error = no_memory;
80 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
82 bfd_error = system_call_error;
85 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
87 bfd_error = system_call_error;
94 DEFUN (elf_mkobject, (abfd), bfd * abfd)
96 /* this just does initialization */
97 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
98 elf_tdata (abfd) = (struct elf_obj_tdata *)
99 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata));
100 if (elf_tdata (abfd) == 0)
102 bfd_error = no_memory;
105 /* since everything is done at close time, do we need any
112 DEFUN (elf_get_str_section, (abfd, shindex),
114 unsigned int shindex)
116 Elf_Internal_Shdr **i_shdrp;
117 char *shstrtab = NULL;
119 unsigned int shstrtabsize;
121 i_shdrp = elf_elfsections (abfd);
122 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
125 shstrtab = i_shdrp[shindex]->rawdata;
126 if (shstrtab == NULL)
128 /* No cached one, attempt to read, and cache what we read. */
129 offset = i_shdrp[shindex]->sh_offset;
130 shstrtabsize = i_shdrp[shindex]->sh_size;
131 shstrtab = elf_read (abfd, offset, shstrtabsize);
132 i_shdrp[shindex]->rawdata = (void *) shstrtab;
138 DEFUN (elf_string_from_elf_section, (abfd, shindex, strindex),
140 unsigned int shindex AND
141 unsigned int strindex)
143 Elf_Internal_Shdr *hdr;
148 hdr = elf_elfsections (abfd)[shindex];
151 && elf_get_str_section (abfd, shindex) == NULL)
154 return ((char *) hdr->rawdata) + strindex;
162 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
165 Helper functions for GDB to locate the string tables.
166 Since BFD hides string tables from callers, GDB needs to use an
167 internal hook to find them. Sun's .stabstr, in particular,
168 isn't even pointed to by the .stab section, so ordinary
169 mechanisms wouldn't work to find it, even if we had some.
172 struct elf_internal_shdr *
173 DEFUN (bfd_elf_find_section, (abfd, name),
177 Elf_Internal_Shdr **i_shdrp;
182 i_shdrp = elf_elfsections (abfd);
185 shstrtab = elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
186 if (shstrtab != NULL)
188 max = elf_elfheader (abfd)->e_shnum;
189 for (i = 1; i < max; i++)
190 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
197 const char *const bfd_elf_section_type_names[] = {
198 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
199 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
200 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
203 /* ELF relocs are against symbols. If we are producing relocateable
204 output, and the reloc is against an external symbol, and nothing
205 has given us any additional addend, the resulting reloc will also
206 be against the same symbol. In such a case, we don't want to
207 change anything about the way the reloc is handled, since it will
208 all be done at final link time. Rather than put special case code
209 into bfd_perform_relocation, all the reloc types use this howto
210 function. It just short circuits the reloc if producing
211 relocateable output against an external symbol. */
213 bfd_reloc_status_type
214 bfd_elf_generic_reloc (abfd,
222 arelent *reloc_entry;
225 asection *input_section;
227 char **error_message;
229 if (output_bfd != (bfd *) NULL
230 && (symbol->flags & BSF_SECTION_SYM) == 0
231 && (! reloc_entry->howto->partial_inplace
232 || reloc_entry->addend == 0))
234 reloc_entry->address += input_section->output_offset;
238 return bfd_reloc_continue;