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
41 /* Standard ELF hash function. Do not change this function; you will
42 cause invalid hash tables to be generated. (Well, you would if this
43 were being used yet.) */
46 CONST unsigned char *name;
52 while ((ch = *name++) != '\0')
55 if ((g = (h & 0xf0000000)) != 0)
64 /* Read a specified number of bytes at a specified offset in an ELF
65 file, into a newly allocated buffer, and return a pointer to the
69 elf_read (abfd, offset, size)
76 if ((buf = bfd_alloc (abfd, size)) == NULL)
78 bfd_set_error (bfd_error_no_memory);
81 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
83 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
85 if (bfd_get_error () != bfd_error_system_call)
86 bfd_set_error (bfd_error_file_truncated);
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_set_error (bfd_error_no_memory);
105 /* since everything is done at close time, do we need any
112 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 elf_string_from_elf_section (abfd, shindex, strindex)
140 unsigned int shindex;
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;
157 /* Make a BFD section from an ELF section. We store a pointer to the
158 BFD section in the rawdata field of the header. */
161 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
163 Elf_Internal_Shdr *hdr;
169 if (hdr->rawdata != NULL)
171 BFD_ASSERT (strcmp (name, ((asection *) hdr->rawdata)->name) == 0);
175 newsect = bfd_make_section_anyway (abfd, name);
179 newsect->filepos = hdr->sh_offset;
181 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
182 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
183 || ! bfd_set_section_alignment (abfd, newsect,
184 bfd_log2 (hdr->sh_addralign)))
187 flags = SEC_NO_FLAGS;
188 if (hdr->sh_type != SHT_NOBITS)
189 flags |= SEC_HAS_CONTENTS;
190 if ((hdr->sh_flags & SHF_ALLOC) != 0)
193 if (hdr->sh_type != SHT_NOBITS)
196 if ((hdr->sh_flags & SHF_WRITE) == 0)
197 flags |= SEC_READONLY;
198 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
200 else if ((flags & SEC_ALLOC) != 0)
203 /* The debugging sections appear to be recognized only by name, not
205 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
206 || strncmp (name, ".line", sizeof ".line" - 1) == 0
207 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
208 flags |= SEC_DEBUGGING;
210 if (! bfd_set_section_flags (abfd, newsect, flags))
213 hdr->rawdata = (PTR) newsect;
214 elf_section_data (newsect)->this_hdr = *hdr;
224 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
227 Helper functions for GDB to locate the string tables.
228 Since BFD hides string tables from callers, GDB needs to use an
229 internal hook to find them. Sun's .stabstr, in particular,
230 isn't even pointed to by the .stab section, so ordinary
231 mechanisms wouldn't work to find it, even if we had some.
234 struct elf_internal_shdr *
235 bfd_elf_find_section (abfd, name)
239 Elf_Internal_Shdr **i_shdrp;
244 i_shdrp = elf_elfsections (abfd);
247 shstrtab = elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
248 if (shstrtab != NULL)
250 max = elf_elfheader (abfd)->e_shnum;
251 for (i = 1; i < max; i++)
252 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
259 const char *const bfd_elf_section_type_names[] = {
260 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
261 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
262 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
265 /* ELF relocs are against symbols. If we are producing relocateable
266 output, and the reloc is against an external symbol, and nothing
267 has given us any additional addend, the resulting reloc will also
268 be against the same symbol. In such a case, we don't want to
269 change anything about the way the reloc is handled, since it will
270 all be done at final link time. Rather than put special case code
271 into bfd_perform_relocation, all the reloc types use this howto
272 function. It just short circuits the reloc if producing
273 relocateable output against an external symbol. */
276 bfd_reloc_status_type
277 bfd_elf_generic_reloc (abfd,
285 arelent *reloc_entry;
288 asection *input_section;
290 char **error_message;
292 if (output_bfd != (bfd *) NULL
293 && (symbol->flags & BSF_SECTION_SYM) == 0
294 && (! reloc_entry->howto->partial_inplace
295 || reloc_entry->addend == 0))
297 reloc_entry->address += input_section->output_offset;
301 return bfd_reloc_continue;
304 /* Create an entry in an ELF linker hash table. */
306 struct bfd_hash_entry *
307 _bfd_elf_link_hash_newfunc (entry, table, string)
308 struct bfd_hash_entry *entry;
309 struct bfd_hash_table *table;
312 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
314 /* Allocate the structure if it has not already been allocated by a
316 if (ret == (struct elf_link_hash_entry *) NULL)
317 ret = ((struct elf_link_hash_entry *)
318 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
319 if (ret == (struct elf_link_hash_entry *) NULL)
321 bfd_set_error (bfd_error_no_memory);
322 return (struct bfd_hash_entry *) ret;
325 /* Call the allocation method of the superclass. */
326 ret = ((struct elf_link_hash_entry *)
327 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
329 if (ret != (struct elf_link_hash_entry *) NULL)
331 /* Set local fields. */
336 ret->dynstr_index = 0;
338 ret->type = STT_NOTYPE;
339 ret->elf_link_hash_flags = 0;
342 return (struct bfd_hash_entry *) ret;
345 /* Initialize an ELF linker hash table. */
348 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
349 struct elf_link_hash_table *table;
351 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
352 struct bfd_hash_table *,
355 table->dynobj = NULL;
356 table->dynsymcount = 0;
357 table->dynstr = NULL;
358 table->bucketcount = 0;
359 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
362 /* Create an ELF linker hash table. */
364 struct bfd_link_hash_table *
365 _bfd_elf_link_hash_table_create (abfd)
368 struct elf_link_hash_table *ret;
370 ret = ((struct elf_link_hash_table *)
371 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
372 if (ret == (struct elf_link_hash_table *) NULL)
374 bfd_set_error (bfd_error_no_memory);
378 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
380 bfd_release (abfd, ret);