* section.c (bfd_get_section_contents): Put in parens to get
[deliverable/binutils-gdb.git] / bfd / elf.c
CommitLineData
32090b8e
KR
1/* ELF executable support for BFD.
2 Copyright 1993 Free Software Foundation, Inc.
3
4This file is part of BFD, the Binary File Descriptor library.
5
6This program is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
10
11This program is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
d1b44e83
ILT
20/*
21
22SECTION
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
32090b8e
KR
34#include "bfd.h"
35#include "sysdep.h"
013dec1a 36#include "bfdlink.h"
32090b8e
KR
37#include "libbfd.h"
38#define ARCH_SIZE 0
39#include "libelf.h"
40
32090b8e
KR
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.) */
44unsigned long
013dec1a
ILT
45bfd_elf_hash (name)
46 CONST unsigned char *name;
32090b8e
KR
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
68static char *
013dec1a
ILT
69elf_read (abfd, offset, size)
70 bfd * abfd;
71 long offset;
72 int size;
32090b8e
KR
73{
74 char *buf;
75
76 if ((buf = bfd_alloc (abfd, size)) == NULL)
77 {
013dec1a 78 bfd_set_error (bfd_error_no_memory);
32090b8e
KR
79 return NULL;
80 }
81 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
013dec1a 82 return NULL;
32090b8e
KR
83 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
84 {
013dec1a
ILT
85 if (bfd_get_error () != bfd_error_system_call)
86 bfd_set_error (bfd_error_file_truncated);
32090b8e
KR
87 return NULL;
88 }
89 return buf;
90}
91
92boolean
013dec1a
ILT
93elf_mkobject (abfd)
94 bfd * abfd;
32090b8e
KR
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 {
013dec1a 102 bfd_set_error (bfd_error_no_memory);
32090b8e
KR
103 return false;
104 }
105 /* since everything is done at close time, do we need any
106 initialization? */
107
108 return true;
109}
110
111char *
013dec1a
ILT
112elf_get_str_section (abfd, shindex)
113 bfd * abfd;
114 unsigned int shindex;
32090b8e
KR
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
137char *
013dec1a
ILT
138elf_string_from_elf_section (abfd, shindex, strindex)
139 bfd * abfd;
140 unsigned int shindex;
141 unsigned int strindex;
32090b8e
KR
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
497c5434
ILT
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
160boolean
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
32090b8e
KR
219/*
220INTERNAL_FUNCTION
221 bfd_elf_find_section
222
223SYNOPSIS
224 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
225
226DESCRIPTION
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
234struct elf_internal_shdr *
013dec1a
ILT
235bfd_elf_find_section (abfd, name)
236 bfd * abfd;
237 char *name;
32090b8e
KR
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
32090b8e
KR
259const 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
013dec1a 275/*ARGSUSED*/
32090b8e
KR
276bfd_reloc_status_type
277bfd_elf_generic_reloc (abfd,
278 reloc_entry,
279 symbol,
280 data,
281 input_section,
4c3721d5
ILT
282 output_bfd,
283 error_message)
32090b8e
KR
284 bfd *abfd;
285 arelent *reloc_entry;
286 asymbol *symbol;
287 PTR data;
288 asection *input_section;
289 bfd *output_bfd;
4c3721d5 290 char **error_message;
32090b8e
KR
291{
292 if (output_bfd != (bfd *) NULL
293 && (symbol->flags & BSF_SECTION_SYM) == 0
d1b44e83
ILT
294 && (! reloc_entry->howto->partial_inplace
295 || reloc_entry->addend == 0))
32090b8e
KR
296 {
297 reloc_entry->address += input_section->output_offset;
298 return bfd_reloc_ok;
299 }
300
301 return bfd_reloc_continue;
302}
013dec1a 303\f
013dec1a
ILT
304/* Create an entry in an ELF linker hash table. */
305
5315c428
ILT
306struct bfd_hash_entry *
307_bfd_elf_link_hash_newfunc (entry, table, string)
013dec1a
ILT
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
5315c428
ILT
345/* Initialize an ELF linker hash table. */
346
347boolean
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
013dec1a
ILT
362/* Create an ELF linker hash table. */
363
364struct 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 }
5315c428
ILT
377
378 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
013dec1a
ILT
379 {
380 bfd_release (abfd, ret);
381 return NULL;
382 }
383
013dec1a
ILT
384 return &ret->root;
385}
This page took 0.087243 seconds and 4 git commands to generate.