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
= (char *) i_shdrp
[shindex
]->contents
;
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
]->contents
= (PTR
) 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
];
150 if (hdr
->contents
== NULL
151 && elf_get_str_section (abfd
, shindex
) == NULL
)
154 return ((char *) hdr
->contents
) + strindex
;
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. */
161 _bfd_elf_make_section_from_shdr (abfd
, hdr
, name
)
163 Elf_Internal_Shdr
*hdr
;
169 if (hdr
->bfd_section
!= NULL
)
171 BFD_ASSERT (strcmp (name
,
172 bfd_get_section_name (abfd
, hdr
->bfd_section
)) == 0);
176 newsect
= bfd_make_section_anyway (abfd
, name
);
180 newsect
->filepos
= hdr
->sh_offset
;
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
)))
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)
194 if (hdr
->sh_type
!= SHT_NOBITS
)
197 if ((hdr
->sh_flags
& SHF_WRITE
) == 0)
198 flags
|= SEC_READONLY
;
199 if ((hdr
->sh_flags
& SHF_EXECINSTR
) != 0)
201 else if ((flags
& SEC_LOAD
) != 0)
204 /* The debugging sections appear to be recognized only by name, not
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
;
211 if (! bfd_set_section_flags (abfd
, newsect
, flags
))
214 hdr
->bfd_section
= newsect
;
215 elf_section_data (newsect
)->this_hdr
= *hdr
;
225 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
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.
235 struct elf_internal_shdr
*
236 bfd_elf_find_section (abfd
, name
)
240 Elf_Internal_Shdr
**i_shdrp
;
245 i_shdrp
= elf_elfsections (abfd
);
248 shstrtab
= elf_get_str_section (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
249 if (shstrtab
!= NULL
)
251 max
= elf_elfheader (abfd
)->e_shnum
;
252 for (i
= 1; i
< max
; i
++)
253 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
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",
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. */
277 bfd_reloc_status_type
278 bfd_elf_generic_reloc (abfd
,
286 arelent
*reloc_entry
;
289 asection
*input_section
;
291 char **error_message
;
293 if (output_bfd
!= (bfd
*) NULL
294 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
295 && (! reloc_entry
->howto
->partial_inplace
296 || reloc_entry
->addend
== 0))
298 reloc_entry
->address
+= input_section
->output_offset
;
302 return bfd_reloc_continue
;
305 /* Display ELF-specific fields of a symbol. */
307 bfd_elf_print_symbol (ignore_abfd
, filep
, symbol
, how
)
311 bfd_print_symbol_type how
;
313 FILE *file
= (FILE *) filep
;
316 case bfd_print_symbol_name
:
317 fprintf (file
, "%s", symbol
->name
);
319 case bfd_print_symbol_more
:
320 fprintf (file
, "elf ");
321 fprintf_vma (file
, symbol
->value
);
322 fprintf (file
, " %lx", (long) symbol
->flags
);
324 case bfd_print_symbol_all
:
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. */
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
);
344 /* Create an entry in an ELF linker hash table. */
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
;
352 struct elf_link_hash_entry
*ret
= (struct elf_link_hash_entry
*) entry
;
354 /* Allocate the structure if it has not already been allocated by a
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
)
361 bfd_set_error (bfd_error_no_memory
);
362 return (struct bfd_hash_entry
*) ret
;
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
,
369 if (ret
!= (struct elf_link_hash_entry
*) NULL
)
371 /* Set local fields. */
375 ret
->dynstr_index
= 0;
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;
383 return (struct bfd_hash_entry
*) ret
;
386 /* Initialize an ELF linker hash table. */
389 _bfd_elf_link_hash_table_init (table
, abfd
, newfunc
)
390 struct elf_link_hash_table
*table
;
392 struct bfd_hash_entry
*(*newfunc
) PARAMS ((struct bfd_hash_entry
*,
393 struct bfd_hash_table
*,
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
);
406 /* Create an ELF linker hash table. */
408 struct bfd_link_hash_table
*
409 _bfd_elf_link_hash_table_create (abfd
)
412 struct elf_link_hash_table
*ret
;
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
)
418 bfd_set_error (bfd_error_no_memory
);
422 if (! _bfd_elf_link_hash_table_init (ret
, abfd
, _bfd_elf_link_hash_newfunc
))
424 bfd_release (abfd
, ret
);
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. */
437 bfd_elf_set_dt_needed_name (abfd
, name
)
441 elf_dt_needed_name (abfd
) = name
;
444 /* Get the list of DT_NEEDED entries for a link. */
446 struct bfd_elf_link_needed_list
*
447 bfd_elf_get_needed_list (abfd
, info
)
449 struct bfd_link_info
*info
;
451 return elf_hash_table (info
)->needed
;