* libelf.h (struct elf_link_hash_table): Add needed field. Remove
[binutils-gdb.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 = (char *) i_shdrp[shindex]->contents;
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]->contents = (PTR) 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->contents == NULL
151 && elf_get_str_section (abfd, shindex) == NULL)
152 return NULL;
153
154 return ((char *) hdr->contents) + strindex;
155 }
156
157 /* Make a BFD section from an ELF section. We store a pointer to the
158 BFD section in the bfd_section 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->bfd_section != NULL)
170 {
171 BFD_ASSERT (strcmp (name,
172 bfd_get_section_name (abfd, hdr->bfd_section)) == 0);
173 return true;
174 }
175
176 newsect = bfd_make_section_anyway (abfd, name);
177 if (newsect == NULL)
178 return false;
179
180 newsect->filepos = hdr->sh_offset;
181
182 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr)
183 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size)
184 || ! bfd_set_section_alignment (abfd, newsect,
185 bfd_log2 (hdr->sh_addralign)))
186 return false;
187
188 flags = SEC_NO_FLAGS;
189 if (hdr->sh_type != SHT_NOBITS)
190 flags |= SEC_HAS_CONTENTS;
191 if ((hdr->sh_flags & SHF_ALLOC) != 0)
192 {
193 flags |= SEC_ALLOC;
194 if (hdr->sh_type != SHT_NOBITS)
195 flags |= SEC_LOAD;
196 }
197 if ((hdr->sh_flags & SHF_WRITE) == 0)
198 flags |= SEC_READONLY;
199 if ((hdr->sh_flags & SHF_EXECINSTR) != 0)
200 flags |= SEC_CODE;
201 else if ((flags & SEC_LOAD) != 0)
202 flags |= SEC_DATA;
203
204 /* The debugging sections appear to be recognized only by name, not
205 any sort of flag. */
206 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0
207 || strncmp (name, ".line", sizeof ".line" - 1) == 0
208 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0)
209 flags |= SEC_DEBUGGING;
210
211 if (! bfd_set_section_flags (abfd, newsect, flags))
212 return false;
213
214 hdr->bfd_section = newsect;
215 elf_section_data (newsect)->this_hdr = *hdr;
216
217 return true;
218 }
219
220 /*
221 INTERNAL_FUNCTION
222 bfd_elf_find_section
223
224 SYNOPSIS
225 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
226
227 DESCRIPTION
228 Helper functions for GDB to locate the string tables.
229 Since BFD hides string tables from callers, GDB needs to use an
230 internal hook to find them. Sun's .stabstr, in particular,
231 isn't even pointed to by the .stab section, so ordinary
232 mechanisms wouldn't work to find it, even if we had some.
233 */
234
235 struct elf_internal_shdr *
236 bfd_elf_find_section (abfd, name)
237 bfd * abfd;
238 char *name;
239 {
240 Elf_Internal_Shdr **i_shdrp;
241 char *shstrtab;
242 unsigned int max;
243 unsigned int i;
244
245 i_shdrp = elf_elfsections (abfd);
246 if (i_shdrp != NULL)
247 {
248 shstrtab = elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
249 if (shstrtab != NULL)
250 {
251 max = elf_elfheader (abfd)->e_shnum;
252 for (i = 1; i < max; i++)
253 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
254 return i_shdrp[i];
255 }
256 }
257 return 0;
258 }
259
260 const char *const bfd_elf_section_type_names[] = {
261 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
262 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
263 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
264 };
265
266 /* ELF relocs are against symbols. If we are producing relocateable
267 output, and the reloc is against an external symbol, and nothing
268 has given us any additional addend, the resulting reloc will also
269 be against the same symbol. In such a case, we don't want to
270 change anything about the way the reloc is handled, since it will
271 all be done at final link time. Rather than put special case code
272 into bfd_perform_relocation, all the reloc types use this howto
273 function. It just short circuits the reloc if producing
274 relocateable output against an external symbol. */
275
276 /*ARGSUSED*/
277 bfd_reloc_status_type
278 bfd_elf_generic_reloc (abfd,
279 reloc_entry,
280 symbol,
281 data,
282 input_section,
283 output_bfd,
284 error_message)
285 bfd *abfd;
286 arelent *reloc_entry;
287 asymbol *symbol;
288 PTR data;
289 asection *input_section;
290 bfd *output_bfd;
291 char **error_message;
292 {
293 if (output_bfd != (bfd *) NULL
294 && (symbol->flags & BSF_SECTION_SYM) == 0
295 && (! reloc_entry->howto->partial_inplace
296 || reloc_entry->addend == 0))
297 {
298 reloc_entry->address += input_section->output_offset;
299 return bfd_reloc_ok;
300 }
301
302 return bfd_reloc_continue;
303 }
304 \f
305 /* Display ELF-specific fields of a symbol. */
306 void
307 bfd_elf_print_symbol (ignore_abfd, filep, symbol, how)
308 bfd *ignore_abfd;
309 PTR filep;
310 asymbol *symbol;
311 bfd_print_symbol_type how;
312 {
313 FILE *file = (FILE *) filep;
314 switch (how)
315 {
316 case bfd_print_symbol_name:
317 fprintf (file, "%s", symbol->name);
318 break;
319 case bfd_print_symbol_more:
320 fprintf (file, "elf ");
321 fprintf_vma (file, symbol->value);
322 fprintf (file, " %lx", (long) symbol->flags);
323 break;
324 case bfd_print_symbol_all:
325 {
326 CONST char *section_name;
327 section_name = symbol->section ? symbol->section->name : "(*none*)";
328 bfd_print_symbol_vandf ((PTR) file, symbol);
329 fprintf (file, " %s\t", section_name);
330 /* Print the "other" value for a symbol. For common symbols,
331 we've already printed the size; now print the alignment.
332 For other symbols, we have no specified alignment, and
333 we've printed the address; now print the size. */
334 fprintf_vma (file,
335 (bfd_is_com_section (symbol->section)
336 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value
337 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size));
338 fprintf (file, " %s", symbol->name);
339 }
340 break;
341 }
342 }
343 \f
344 /* Create an entry in an ELF linker hash table. */
345
346 struct bfd_hash_entry *
347 _bfd_elf_link_hash_newfunc (entry, table, string)
348 struct bfd_hash_entry *entry;
349 struct bfd_hash_table *table;
350 const char *string;
351 {
352 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry;
353
354 /* Allocate the structure if it has not already been allocated by a
355 subclass. */
356 if (ret == (struct elf_link_hash_entry *) NULL)
357 ret = ((struct elf_link_hash_entry *)
358 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)));
359 if (ret == (struct elf_link_hash_entry *) NULL)
360 {
361 bfd_set_error (bfd_error_no_memory);
362 return (struct bfd_hash_entry *) ret;
363 }
364
365 /* Call the allocation method of the superclass. */
366 ret = ((struct elf_link_hash_entry *)
367 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
368 table, string));
369 if (ret != (struct elf_link_hash_entry *) NULL)
370 {
371 /* Set local fields. */
372 ret->indx = -1;
373 ret->size = 0;
374 ret->dynindx = -1;
375 ret->dynstr_index = 0;
376 ret->weakdef = NULL;
377 ret->got_offset = (bfd_vma) -1;
378 ret->plt_offset = (bfd_vma) -1;
379 ret->type = STT_NOTYPE;
380 ret->elf_link_hash_flags = 0;
381 }
382
383 return (struct bfd_hash_entry *) ret;
384 }
385
386 /* Initialize an ELF linker hash table. */
387
388 boolean
389 _bfd_elf_link_hash_table_init (table, abfd, newfunc)
390 struct elf_link_hash_table *table;
391 bfd *abfd;
392 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
393 struct bfd_hash_table *,
394 const char *));
395 {
396 table->dynamic_sections_created = false;
397 table->dynobj = NULL;
398 /* The first dynamic symbol is a dummy. */
399 table->dynsymcount = 1;
400 table->dynstr = NULL;
401 table->bucketcount = 0;
402 table->needed = NULL;
403 return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
404 }
405
406 /* Create an ELF linker hash table. */
407
408 struct bfd_link_hash_table *
409 _bfd_elf_link_hash_table_create (abfd)
410 bfd *abfd;
411 {
412 struct elf_link_hash_table *ret;
413
414 ret = ((struct elf_link_hash_table *)
415 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
416 if (ret == (struct elf_link_hash_table *) NULL)
417 {
418 bfd_set_error (bfd_error_no_memory);
419 return NULL;
420 }
421
422 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc))
423 {
424 bfd_release (abfd, ret);
425 return NULL;
426 }
427
428 return &ret->root;
429 }
430
431 /* This is a hook for the ELF emulation code in the generic linker to
432 tell the backend linker what file name to use for the DT_NEEDED
433 entry for a dynamic object. The generic linker passes name as an
434 empty string to indicate that no DT_NEEDED entry should be made. */
435
436 void
437 bfd_elf_set_dt_needed_name (abfd, name)
438 bfd *abfd;
439 const char *name;
440 {
441 elf_dt_needed_name (abfd) = name;
442 }
443
444 /* Get the list of DT_NEEDED entries for a link. */
445
446 struct bfd_elf_link_needed_list *
447 bfd_elf_get_needed_list (abfd, info)
448 bfd *abfd;
449 struct bfd_link_info *info;
450 {
451 return elf_hash_table (info)->needed;
452 }