]> Git Repo - binutils.git/blob - bfd/elf.c
Sun Jul 10 09:12:02 1994 D. V. Henkel-Wallace ([email protected])
[binutils.git] / bfd / elf.c
1 /* ELF executable support for BFD.
2    Copyright 1993 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
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.
10
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.
15
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.  */
19
20 /*
21
22 SECTION
23         ELF backends
24
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).
28
29         Documentation of the internals of the support code still needs
30         to be written.  The code is changing quickly enough that we
31         haven't bothered yet.
32  */
33
34 #include "bfd.h"
35 #include "sysdep.h"
36 #include "bfdlink.h"
37 #include "libbfd.h"
38 #define ARCH_SIZE 0
39 #include "libelf.h"
40
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.)  */
44 unsigned long
45 bfd_elf_hash (name)
46      CONST unsigned char *name;
47 {
48   unsigned long h = 0;
49   unsigned long g;
50   int ch;
51
52   while ((ch = *name++) != '\0')
53     {
54       h = (h << 4) + ch;
55       if ((g = (h & 0xf0000000)) != 0)
56         {
57           h ^= g >> 24;
58           h &= ~g;
59         }
60     }
61   return h;
62 }
63
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
66    buffer. */
67
68 static char *
69 elf_read (abfd, offset, size)
70      bfd * abfd;
71      long offset;
72      int size;
73 {
74   char *buf;
75
76   if ((buf = bfd_alloc (abfd, size)) == NULL)
77     {
78       bfd_set_error (bfd_error_no_memory);
79       return NULL;
80     }
81   if (bfd_seek (abfd, offset, SEEK_SET) == -1)
82     return NULL;
83   if (bfd_read ((PTR) buf, size, 1, abfd) != size)
84     {
85       if (bfd_get_error () != bfd_error_system_call)
86         bfd_set_error (bfd_error_file_truncated);
87       return NULL;
88     }
89   return buf;
90 }
91
92 boolean
93 elf_mkobject (abfd)
94      bfd * abfd;
95 {
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)
101     {
102       bfd_set_error (bfd_error_no_memory);
103       return false;
104     }
105   /* since everything is done at close time, do we need any
106      initialization? */
107
108   return true;
109 }
110
111 char *
112 elf_get_str_section (abfd, shindex)
113      bfd * abfd;
114      unsigned int shindex;
115 {
116   Elf_Internal_Shdr **i_shdrp;
117   char *shstrtab = NULL;
118   unsigned int offset;
119   unsigned int shstrtabsize;
120
121   i_shdrp = elf_elfsections (abfd);
122   if (i_shdrp == 0 || i_shdrp[shindex] == 0)
123     return 0;
124
125   shstrtab = i_shdrp[shindex]->rawdata;
126   if (shstrtab == NULL)
127     {
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;
133     }
134   return shstrtab;
135 }
136
137 char *
138 elf_string_from_elf_section (abfd, shindex, strindex)
139      bfd * abfd;
140      unsigned int shindex;
141      unsigned int strindex;
142 {
143   Elf_Internal_Shdr *hdr;
144
145   if (strindex == 0)
146     return "";
147
148   hdr = elf_elfsections (abfd)[shindex];
149
150   if (!hdr->rawdata
151       && elf_get_str_section (abfd, shindex) == NULL)
152     return NULL;
153
154   return ((char *) hdr->rawdata) + strindex;
155 }
156
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.  */
159
160 boolean
161 _bfd_elf_make_section_from_shdr (abfd, hdr, name)
162      bfd *abfd;
163      Elf_Internal_Shdr *hdr;
164      const char *name;
165 {
166   asection *newsect;
167   flagword flags;
168
169   if (hdr->rawdata != NULL)
170     {
171       BFD_ASSERT (strcmp (name, ((asection *) hdr->rawdata)->name) == 0);
172       return true;
173     }
174
175   newsect = bfd_make_section_anyway (abfd, name);
176   if (newsect == NULL)
177     return false;
178
179   newsect->filepos = hdr->sh_offset;
180
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)))
185     return false;
186
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)
191     {
192       flags |= SEC_ALLOC;
193       if (hdr->sh_type != SHT_NOBITS)
194         flags |= SEC_LOAD;
195     }
196   if ((hdr->sh_flags & SHF_WRITE) == 0)
197     flags |= SEC_READONLY;
198   if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
199     flags |= SEC_CODE;
200   else if ((flags & SEC_ALLOC) != 0)
201     flags |= SEC_DATA;
202
203   /* The debugging sections appear to be recognized only by name, not
204      any sort of flag.  */
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;
209
210   if (! bfd_set_section_flags (abfd, newsect, flags))
211     return false;
212
213   hdr->rawdata = (PTR) newsect;
214   elf_section_data (newsect)->this_hdr = *hdr;
215
216   return true;
217 }
218
219 /*
220 INTERNAL_FUNCTION
221         bfd_elf_find_section
222
223 SYNOPSIS
224         struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
225
226 DESCRIPTION
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.
232 */
233
234 struct elf_internal_shdr *
235 bfd_elf_find_section (abfd, name)
236      bfd * abfd;
237      char *name;
238 {
239   Elf_Internal_Shdr **i_shdrp;
240   char *shstrtab;
241   unsigned int max;
242   unsigned int i;
243
244   i_shdrp = elf_elfsections (abfd);
245   if (i_shdrp != NULL)
246     {
247       shstrtab = elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
248       if (shstrtab != NULL)
249         {
250           max = elf_elfheader (abfd)->e_shnum;
251           for (i = 1; i < max; i++)
252             if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
253               return i_shdrp[i];
254         }
255     }
256   return 0;
257 }
258
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",
263 };
264
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.  */
274
275 /*ARGSUSED*/
276 bfd_reloc_status_type
277 bfd_elf_generic_reloc (abfd,
278                        reloc_entry,
279                        symbol,
280                        data,
281                        input_section,
282                        output_bfd,
283                        error_message)
284      bfd *abfd;
285      arelent *reloc_entry;
286      asymbol *symbol;
287      PTR data;
288      asection *input_section;
289      bfd *output_bfd;
290      char **error_message;
291 {
292   if (output_bfd != (bfd *) NULL
293       && (symbol->flags & BSF_SECTION_SYM) == 0
294       && (! reloc_entry->howto->partial_inplace
295           || reloc_entry->addend == 0))
296     {
297       reloc_entry->address += input_section->output_offset;
298       return bfd_reloc_ok;
299     }
300
301   return bfd_reloc_continue;
302 }
303 \f
304 /* Create an entry in an ELF linker hash table.  */
305
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;
310      const char *string;
311 {
312   struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
313
314   /* Allocate the structure if it has not already been allocated by a
315      subclass.  */
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)
320     {
321       bfd_set_error (bfd_error_no_memory);
322       return (struct bfd_hash_entry *) ret;
323     }
324
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,
328                                  table, string));
329   if (ret != (struct elf_link_hash_entry *) NULL)
330     {
331       /* Set local fields.  */
332       ret->indx = -1;
333       ret->size = 0;
334       ret->align = 0;
335       ret->dynindx = -1;
336       ret->dynstr_index = 0;
337       ret->weakdef = NULL;
338       ret->type = STT_NOTYPE;
339       ret->elf_link_hash_flags = 0;
340     }
341
342   return (struct bfd_hash_entry *) ret;
343 }
344
345 /* Initialize an ELF linker hash table.  */
346
347 boolean
348 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
349      struct elf_link_hash_table *table;
350      bfd *abfd;
351      struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
352                                                 struct bfd_hash_table *,
353                                                 const char *));
354 {
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);
360 }
361
362 /* Create an ELF linker hash table.  */
363
364 struct bfd_link_hash_table *
365 _bfd_elf_link_hash_table_create (abfd)
366      bfd *abfd;
367 {
368   struct elf_link_hash_table *ret;
369
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)
373     {
374       bfd_set_error (bfd_error_no_memory);
375       return NULL;
376     }
377
378   if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
379     {
380       bfd_release (abfd, ret);
381       return NULL;
382     }
383
384   return &ret->root;
385 }
This page took 0.046172 seconds and 4 git commands to generate.