elfcode.h revision 33965
1/* ELF executable support for BFD. 2 Copyright 1991, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc. 3 4 Written by Fred Fish @ Cygnus Support, from information published 5 in "UNIX System V Release 4, Programmers Guide: ANSI C and 6 Programming Support Tools". Sufficient support for gdb. 7 8 Rewritten by Mark Eichin @ Cygnus Support, from information 9 published in "System V Application Binary Interface", chapters 4 10 and 5, as well as the various "Processor Supplement" documents 11 derived from it. Added support for assembler and other object file 12 utilities. Further work done by Ken Raeburn (Cygnus Support), Michael 13 Meissner (Open Software Foundation), and Peter Hoogenboom (University 14 of Utah) to finish and extend this. 15 16This file is part of BFD, the Binary File Descriptor library. 17 18This program is free software; you can redistribute it and/or modify 19it under the terms of the GNU General Public License as published by 20the Free Software Foundation; either version 2 of the License, or 21(at your option) any later version. 22 23This program is distributed in the hope that it will be useful, 24but WITHOUT ANY WARRANTY; without even the implied warranty of 25MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 26GNU General Public License for more details. 27 28You should have received a copy of the GNU General Public License 29along with this program; if not, write to the Free Software 30Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 31 32/* Problems and other issues to resolve. 33 34 (1) BFD expects there to be some fixed number of "sections" in 35 the object file. I.E. there is a "section_count" variable in the 36 bfd structure which contains the number of sections. However, ELF 37 supports multiple "views" of a file. In particular, with current 38 implementations, executable files typically have two tables, a 39 program header table and a section header table, both of which 40 partition the executable. 41 42 In ELF-speak, the "linking view" of the file uses the section header 43 table to access "sections" within the file, and the "execution view" 44 uses the program header table to access "segments" within the file. 45 "Segments" typically may contain all the data from one or more 46 "sections". 47 48 Note that the section header table is optional in ELF executables, 49 but it is this information that is most useful to gdb. If the 50 section header table is missing, then gdb should probably try 51 to make do with the program header table. (FIXME) 52 53 (2) The code in this file is compiled twice, once in 32-bit mode and 54 once in 64-bit mode. More of it should be made size-independent 55 and moved into elf.c. 56 57 (3) ELF section symbols are handled rather sloppily now. This should 58 be cleaned up, and ELF section symbols reconciled with BFD section 59 symbols. 60 61 (4) We need a published spec for 64-bit ELF. We've got some stuff here 62 that we're using for SPARC V9 64-bit chips, but don't assume that 63 it's cast in stone. 64 */ 65 66#include "bfd.h" 67#include "sysdep.h" 68#include "bfdlink.h" 69#include "libbfd.h" 70#include "elf-bfd.h" 71#include "fnmatch.h" 72 73/* Renaming structures, typedefs, macros and functions to be size-specific. */ 74#define Elf_External_Ehdr NAME(Elf,External_Ehdr) 75#define Elf_External_Sym NAME(Elf,External_Sym) 76#define Elf_External_Shdr NAME(Elf,External_Shdr) 77#define Elf_External_Phdr NAME(Elf,External_Phdr) 78#define Elf_External_Rel NAME(Elf,External_Rel) 79#define Elf_External_Rela NAME(Elf,External_Rela) 80#define Elf_External_Dyn NAME(Elf,External_Dyn) 81 82#define elf_core_file_failing_command NAME(bfd_elf,core_file_failing_command) 83#define elf_core_file_failing_signal NAME(bfd_elf,core_file_failing_signal) 84#define elf_core_file_matches_executable_p \ 85 NAME(bfd_elf,core_file_matches_executable_p) 86#define elf_object_p NAME(bfd_elf,object_p) 87#define elf_core_file_p NAME(bfd_elf,core_file_p) 88#define elf_get_symtab_upper_bound NAME(bfd_elf,get_symtab_upper_bound) 89#define elf_get_dynamic_symtab_upper_bound \ 90 NAME(bfd_elf,get_dynamic_symtab_upper_bound) 91#define elf_swap_reloc_in NAME(bfd_elf,swap_reloc_in) 92#define elf_swap_reloca_in NAME(bfd_elf,swap_reloca_in) 93#define elf_swap_reloc_out NAME(bfd_elf,swap_reloc_out) 94#define elf_swap_reloca_out NAME(bfd_elf,swap_reloca_out) 95#define elf_swap_symbol_in NAME(bfd_elf,swap_symbol_in) 96#define elf_swap_symbol_out NAME(bfd_elf,swap_symbol_out) 97#define elf_swap_phdr_in NAME(bfd_elf,swap_phdr_in) 98#define elf_swap_phdr_out NAME(bfd_elf,swap_phdr_out) 99#define elf_swap_dyn_in NAME(bfd_elf,swap_dyn_in) 100#define elf_swap_dyn_out NAME(bfd_elf,swap_dyn_out) 101#define elf_get_reloc_upper_bound NAME(bfd_elf,get_reloc_upper_bound) 102#define elf_canonicalize_reloc NAME(bfd_elf,canonicalize_reloc) 103#define elf_slurp_symbol_table NAME(bfd_elf,slurp_symbol_table) 104#define elf_get_symtab NAME(bfd_elf,get_symtab) 105#define elf_canonicalize_dynamic_symtab \ 106 NAME(bfd_elf,canonicalize_dynamic_symtab) 107#define elf_make_empty_symbol NAME(bfd_elf,make_empty_symbol) 108#define elf_get_symbol_info NAME(bfd_elf,get_symbol_info) 109#define elf_get_lineno NAME(bfd_elf,get_lineno) 110#define elf_set_arch_mach NAME(bfd_elf,set_arch_mach) 111#define elf_find_nearest_line NAME(bfd_elf,find_nearest_line) 112#define elf_sizeof_headers NAME(bfd_elf,sizeof_headers) 113#define elf_set_section_contents NAME(bfd_elf,set_section_contents) 114#define elf_no_info_to_howto NAME(bfd_elf,no_info_to_howto) 115#define elf_no_info_to_howto_rel NAME(bfd_elf,no_info_to_howto_rel) 116#define elf_find_section NAME(bfd_elf,find_section) 117#define elf_bfd_link_add_symbols NAME(bfd_elf,bfd_link_add_symbols) 118#define elf_add_dynamic_entry NAME(bfd_elf,add_dynamic_entry) 119#define elf_write_shdrs_and_ehdr NAME(bfd_elf,write_shdrs_and_ehdr) 120#define elf_write_out_phdrs NAME(bfd_elf,write_out_phdrs) 121#define elf_link_create_dynamic_sections \ 122 NAME(bfd_elf,link_create_dynamic_sections) 123#define elf_link_record_dynamic_symbol _bfd_elf_link_record_dynamic_symbol 124#define elf_bfd_final_link NAME(bfd_elf,bfd_final_link) 125#define elf_create_pointer_linker_section NAME(bfd_elf,create_pointer_linker_section) 126#define elf_finish_pointer_linker_section NAME(bfd_elf,finish_pointer_linker_section) 127 128#if ARCH_SIZE == 64 129#define ELF_R_INFO(X,Y) ELF64_R_INFO(X,Y) 130#define ELF_R_SYM(X) ELF64_R_SYM(X) 131#define ELF_R_TYPE(X) ELF64_R_TYPE(X) 132#define ELFCLASS ELFCLASS64 133#define FILE_ALIGN 8 134#define LOG_FILE_ALIGN 3 135#endif 136#if ARCH_SIZE == 32 137#define ELF_R_INFO(X,Y) ELF32_R_INFO(X,Y) 138#define ELF_R_SYM(X) ELF32_R_SYM(X) 139#define ELF_R_TYPE(X) ELF32_R_TYPE(X) 140#define ELFCLASS ELFCLASS32 141#define FILE_ALIGN 4 142#define LOG_FILE_ALIGN 2 143#endif 144 145/* Static functions */ 146 147static void elf_swap_ehdr_in 148 PARAMS ((bfd *, const Elf_External_Ehdr *, Elf_Internal_Ehdr *)); 149static void elf_swap_ehdr_out 150 PARAMS ((bfd *, const Elf_Internal_Ehdr *, Elf_External_Ehdr *)); 151static void elf_swap_shdr_in 152 PARAMS ((bfd *, const Elf_External_Shdr *, Elf_Internal_Shdr *)); 153static void elf_swap_shdr_out 154 PARAMS ((bfd *, const Elf_Internal_Shdr *, Elf_External_Shdr *)); 155 156#define elf_stringtab_init _bfd_elf_stringtab_init 157 158#define section_from_elf_index bfd_section_from_elf_index 159 160static boolean elf_slurp_reloc_table 161 PARAMS ((bfd *, asection *, asymbol **, boolean)); 162 163static void write_relocs PARAMS ((bfd *, asection *, PTR)); 164 165static boolean elf_file_p PARAMS ((Elf_External_Ehdr *)); 166 167#ifdef DEBUG 168static void elf_debug_section PARAMS ((int, Elf_Internal_Shdr *)); 169static void elf_debug_file PARAMS ((Elf_Internal_Ehdr *)); 170static char *elf_symbol_flags PARAMS ((flagword)); 171#endif 172 173/* Structure swapping routines */ 174 175/* Should perhaps use put_offset, put_word, etc. For now, the two versions 176 can be handled by explicitly specifying 32 bits or "the long type". */ 177#if ARCH_SIZE == 64 178#define put_word bfd_h_put_64 179#define get_word bfd_h_get_64 180#endif 181#if ARCH_SIZE == 32 182#define put_word bfd_h_put_32 183#define get_word bfd_h_get_32 184#endif 185 186/* Translate an ELF symbol in external format into an ELF symbol in internal 187 format. */ 188 189void 190elf_swap_symbol_in (abfd, src, dst) 191 bfd *abfd; 192 const Elf_External_Sym *src; 193 Elf_Internal_Sym *dst; 194{ 195 dst->st_name = bfd_h_get_32 (abfd, (bfd_byte *) src->st_name); 196 dst->st_value = get_word (abfd, (bfd_byte *) src->st_value); 197 dst->st_size = get_word (abfd, (bfd_byte *) src->st_size); 198 dst->st_info = bfd_h_get_8 (abfd, (bfd_byte *) src->st_info); 199 dst->st_other = bfd_h_get_8 (abfd, (bfd_byte *) src->st_other); 200 dst->st_shndx = bfd_h_get_16 (abfd, (bfd_byte *) src->st_shndx); 201} 202 203/* Translate an ELF symbol in internal format into an ELF symbol in external 204 format. */ 205 206void 207elf_swap_symbol_out (abfd, src, cdst) 208 bfd *abfd; 209 const Elf_Internal_Sym *src; 210 PTR cdst; 211{ 212 Elf_External_Sym *dst = (Elf_External_Sym *) cdst; 213 bfd_h_put_32 (abfd, src->st_name, dst->st_name); 214 put_word (abfd, src->st_value, dst->st_value); 215 put_word (abfd, src->st_size, dst->st_size); 216 bfd_h_put_8 (abfd, src->st_info, dst->st_info); 217 bfd_h_put_8 (abfd, src->st_other, dst->st_other); 218 bfd_h_put_16 (abfd, src->st_shndx, dst->st_shndx); 219} 220 221 222/* Translate an ELF file header in external format into an ELF file header in 223 internal format. */ 224 225static void 226elf_swap_ehdr_in (abfd, src, dst) 227 bfd *abfd; 228 const Elf_External_Ehdr *src; 229 Elf_Internal_Ehdr *dst; 230{ 231 memcpy (dst->e_ident, src->e_ident, EI_NIDENT); 232 dst->e_type = bfd_h_get_16 (abfd, (bfd_byte *) src->e_type); 233 dst->e_machine = bfd_h_get_16 (abfd, (bfd_byte *) src->e_machine); 234 dst->e_version = bfd_h_get_32 (abfd, (bfd_byte *) src->e_version); 235 dst->e_entry = get_word (abfd, (bfd_byte *) src->e_entry); 236 dst->e_phoff = get_word (abfd, (bfd_byte *) src->e_phoff); 237 dst->e_shoff = get_word (abfd, (bfd_byte *) src->e_shoff); 238 dst->e_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->e_flags); 239 dst->e_ehsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_ehsize); 240 dst->e_phentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phentsize); 241 dst->e_phnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_phnum); 242 dst->e_shentsize = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shentsize); 243 dst->e_shnum = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shnum); 244 dst->e_shstrndx = bfd_h_get_16 (abfd, (bfd_byte *) src->e_shstrndx); 245} 246 247/* Translate an ELF file header in internal format into an ELF file header in 248 external format. */ 249 250static void 251elf_swap_ehdr_out (abfd, src, dst) 252 bfd *abfd; 253 const Elf_Internal_Ehdr *src; 254 Elf_External_Ehdr *dst; 255{ 256 memcpy (dst->e_ident, src->e_ident, EI_NIDENT); 257 /* note that all elements of dst are *arrays of unsigned char* already... */ 258 bfd_h_put_16 (abfd, src->e_type, dst->e_type); 259 bfd_h_put_16 (abfd, src->e_machine, dst->e_machine); 260 bfd_h_put_32 (abfd, src->e_version, dst->e_version); 261 put_word (abfd, src->e_entry, dst->e_entry); 262 put_word (abfd, src->e_phoff, dst->e_phoff); 263 put_word (abfd, src->e_shoff, dst->e_shoff); 264 bfd_h_put_32 (abfd, src->e_flags, dst->e_flags); 265 bfd_h_put_16 (abfd, src->e_ehsize, dst->e_ehsize); 266 bfd_h_put_16 (abfd, src->e_phentsize, dst->e_phentsize); 267 bfd_h_put_16 (abfd, src->e_phnum, dst->e_phnum); 268 bfd_h_put_16 (abfd, src->e_shentsize, dst->e_shentsize); 269 bfd_h_put_16 (abfd, src->e_shnum, dst->e_shnum); 270 bfd_h_put_16 (abfd, src->e_shstrndx, dst->e_shstrndx); 271} 272 273 274/* Translate an ELF section header table entry in external format into an 275 ELF section header table entry in internal format. */ 276 277static void 278elf_swap_shdr_in (abfd, src, dst) 279 bfd *abfd; 280 const Elf_External_Shdr *src; 281 Elf_Internal_Shdr *dst; 282{ 283 dst->sh_name = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_name); 284 dst->sh_type = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_type); 285 dst->sh_flags = get_word (abfd, (bfd_byte *) src->sh_flags); 286 dst->sh_addr = get_word (abfd, (bfd_byte *) src->sh_addr); 287 dst->sh_offset = get_word (abfd, (bfd_byte *) src->sh_offset); 288 dst->sh_size = get_word (abfd, (bfd_byte *) src->sh_size); 289 dst->sh_link = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_link); 290 dst->sh_info = bfd_h_get_32 (abfd, (bfd_byte *) src->sh_info); 291 dst->sh_addralign = get_word (abfd, (bfd_byte *) src->sh_addralign); 292 dst->sh_entsize = get_word (abfd, (bfd_byte *) src->sh_entsize); 293 dst->bfd_section = NULL; 294 dst->contents = NULL; 295} 296 297/* Translate an ELF section header table entry in internal format into an 298 ELF section header table entry in external format. */ 299 300static void 301elf_swap_shdr_out (abfd, src, dst) 302 bfd *abfd; 303 const Elf_Internal_Shdr *src; 304 Elf_External_Shdr *dst; 305{ 306 /* note that all elements of dst are *arrays of unsigned char* already... */ 307 bfd_h_put_32 (abfd, src->sh_name, dst->sh_name); 308 bfd_h_put_32 (abfd, src->sh_type, dst->sh_type); 309 put_word (abfd, src->sh_flags, dst->sh_flags); 310 put_word (abfd, src->sh_addr, dst->sh_addr); 311 put_word (abfd, src->sh_offset, dst->sh_offset); 312 put_word (abfd, src->sh_size, dst->sh_size); 313 bfd_h_put_32 (abfd, src->sh_link, dst->sh_link); 314 bfd_h_put_32 (abfd, src->sh_info, dst->sh_info); 315 put_word (abfd, src->sh_addralign, dst->sh_addralign); 316 put_word (abfd, src->sh_entsize, dst->sh_entsize); 317} 318 319 320/* Translate an ELF program header table entry in external format into an 321 ELF program header table entry in internal format. */ 322 323void 324elf_swap_phdr_in (abfd, src, dst) 325 bfd *abfd; 326 const Elf_External_Phdr *src; 327 Elf_Internal_Phdr *dst; 328{ 329 dst->p_type = bfd_h_get_32 (abfd, (bfd_byte *) src->p_type); 330 dst->p_flags = bfd_h_get_32 (abfd, (bfd_byte *) src->p_flags); 331 dst->p_offset = get_word (abfd, (bfd_byte *) src->p_offset); 332 dst->p_vaddr = get_word (abfd, (bfd_byte *) src->p_vaddr); 333 dst->p_paddr = get_word (abfd, (bfd_byte *) src->p_paddr); 334 dst->p_filesz = get_word (abfd, (bfd_byte *) src->p_filesz); 335 dst->p_memsz = get_word (abfd, (bfd_byte *) src->p_memsz); 336 dst->p_align = get_word (abfd, (bfd_byte *) src->p_align); 337} 338 339void 340elf_swap_phdr_out (abfd, src, dst) 341 bfd *abfd; 342 const Elf_Internal_Phdr *src; 343 Elf_External_Phdr *dst; 344{ 345 /* note that all elements of dst are *arrays of unsigned char* already... */ 346 bfd_h_put_32 (abfd, src->p_type, dst->p_type); 347 put_word (abfd, src->p_offset, dst->p_offset); 348 put_word (abfd, src->p_vaddr, dst->p_vaddr); 349 put_word (abfd, src->p_paddr, dst->p_paddr); 350 put_word (abfd, src->p_filesz, dst->p_filesz); 351 put_word (abfd, src->p_memsz, dst->p_memsz); 352 bfd_h_put_32 (abfd, src->p_flags, dst->p_flags); 353 put_word (abfd, src->p_align, dst->p_align); 354} 355 356/* Translate an ELF reloc from external format to internal format. */ 357INLINE void 358elf_swap_reloc_in (abfd, src, dst) 359 bfd *abfd; 360 const Elf_External_Rel *src; 361 Elf_Internal_Rel *dst; 362{ 363 dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset); 364 dst->r_info = get_word (abfd, (bfd_byte *) src->r_info); 365} 366 367INLINE void 368elf_swap_reloca_in (abfd, src, dst) 369 bfd *abfd; 370 const Elf_External_Rela *src; 371 Elf_Internal_Rela *dst; 372{ 373 dst->r_offset = get_word (abfd, (bfd_byte *) src->r_offset); 374 dst->r_info = get_word (abfd, (bfd_byte *) src->r_info); 375 dst->r_addend = get_word (abfd, (bfd_byte *) src->r_addend); 376} 377 378/* Translate an ELF reloc from internal format to external format. */ 379INLINE void 380elf_swap_reloc_out (abfd, src, dst) 381 bfd *abfd; 382 const Elf_Internal_Rel *src; 383 Elf_External_Rel *dst; 384{ 385 put_word (abfd, src->r_offset, dst->r_offset); 386 put_word (abfd, src->r_info, dst->r_info); 387} 388 389INLINE void 390elf_swap_reloca_out (abfd, src, dst) 391 bfd *abfd; 392 const Elf_Internal_Rela *src; 393 Elf_External_Rela *dst; 394{ 395 put_word (abfd, src->r_offset, dst->r_offset); 396 put_word (abfd, src->r_info, dst->r_info); 397 put_word (abfd, src->r_addend, dst->r_addend); 398} 399 400INLINE void 401elf_swap_dyn_in (abfd, p, dst) 402 bfd *abfd; 403 const PTR p; 404 Elf_Internal_Dyn *dst; 405{ 406 const Elf_External_Dyn *src = (const Elf_External_Dyn *) p; 407 408 dst->d_tag = get_word (abfd, src->d_tag); 409 dst->d_un.d_val = get_word (abfd, src->d_un.d_val); 410} 411 412INLINE void 413elf_swap_dyn_out (abfd, src, dst) 414 bfd *abfd; 415 const Elf_Internal_Dyn *src; 416 Elf_External_Dyn *dst; 417{ 418 put_word (abfd, src->d_tag, dst->d_tag); 419 put_word (abfd, src->d_un.d_val, dst->d_un.d_val); 420} 421 422/* ELF .o/exec file reading */ 423 424 425/* Begin processing a given object. 426 427 First we validate the file by reading in the ELF header and checking 428 the magic number. */ 429 430static INLINE boolean 431elf_file_p (x_ehdrp) 432 Elf_External_Ehdr *x_ehdrp; 433{ 434 return ((x_ehdrp->e_ident[EI_MAG0] == ELFMAG0) 435 && (x_ehdrp->e_ident[EI_MAG1] == ELFMAG1) 436 && (x_ehdrp->e_ident[EI_MAG2] == ELFMAG2) 437 && (x_ehdrp->e_ident[EI_MAG3] == ELFMAG3)); 438} 439 440/* Check to see if the file associated with ABFD matches the target vector 441 that ABFD points to. 442 443 Note that we may be called several times with the same ABFD, but different 444 target vectors, most of which will not match. We have to avoid leaving 445 any side effects in ABFD, or any data it points to (like tdata), if the 446 file does not match the target vector. */ 447 448const bfd_target * 449elf_object_p (abfd) 450 bfd *abfd; 451{ 452 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ 453 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ 454 Elf_External_Shdr x_shdr; /* Section header table entry, external form */ 455 Elf_Internal_Shdr *i_shdrp = NULL; /* Section header table, internal form */ 456 unsigned int shindex; 457 char *shstrtab; /* Internal copy of section header stringtab */ 458 struct elf_backend_data *ebd; 459 struct elf_obj_tdata *preserved_tdata = elf_tdata (abfd); 460 struct elf_obj_tdata *new_tdata = NULL; 461 asection *s; 462 463 /* Read in the ELF header in external format. */ 464 465 if (bfd_read ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) != sizeof (x_ehdr)) 466 { 467 if (bfd_get_error () != bfd_error_system_call) 468 goto got_wrong_format_error; 469 else 470 goto got_no_match; 471 } 472 473 /* Now check to see if we have a valid ELF file, and one that BFD can 474 make use of. The magic number must match, the address size ('class') 475 and byte-swapping must match our XVEC entry, and it must have a 476 section header table (FIXME: See comments re sections at top of this 477 file). */ 478 479 if ((elf_file_p (&x_ehdr) == false) || 480 (x_ehdr.e_ident[EI_VERSION] != EV_CURRENT) || 481 (x_ehdr.e_ident[EI_CLASS] != ELFCLASS)) 482 goto got_wrong_format_error; 483 484 /* Check that file's byte order matches xvec's */ 485 switch (x_ehdr.e_ident[EI_DATA]) 486 { 487 case ELFDATA2MSB: /* Big-endian */ 488 if (! bfd_header_big_endian (abfd)) 489 goto got_wrong_format_error; 490 break; 491 case ELFDATA2LSB: /* Little-endian */ 492 if (! bfd_header_little_endian (abfd)) 493 goto got_wrong_format_error; 494 break; 495 case ELFDATANONE: /* No data encoding specified */ 496 default: /* Unknown data encoding specified */ 497 goto got_wrong_format_error; 498 } 499 500 /* Allocate an instance of the elf_obj_tdata structure and hook it up to 501 the tdata pointer in the bfd. */ 502 503 new_tdata = ((struct elf_obj_tdata *) 504 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata))); 505 if (new_tdata == NULL) 506 goto got_no_match; 507 elf_tdata (abfd) = new_tdata; 508 509 /* Now that we know the byte order, swap in the rest of the header */ 510 i_ehdrp = elf_elfheader (abfd); 511 elf_swap_ehdr_in (abfd, &x_ehdr, i_ehdrp); 512#if DEBUG & 1 513 elf_debug_file (i_ehdrp); 514#endif 515 516 /* If there is no section header table, we're hosed. */ 517 if (i_ehdrp->e_shoff == 0) 518 goto got_wrong_format_error; 519 520 /* As a simple sanity check, verify that the what BFD thinks is the 521 size of each section header table entry actually matches the size 522 recorded in the file. */ 523 if (i_ehdrp->e_shentsize != sizeof (x_shdr)) 524 goto got_wrong_format_error; 525 526 ebd = get_elf_backend_data (abfd); 527 528 /* Check that the ELF e_machine field matches what this particular 529 BFD format expects. */ 530 if (ebd->elf_machine_code != i_ehdrp->e_machine 531 && (ebd->elf_machine_alt1 == 0 || i_ehdrp->e_machine != ebd->elf_machine_alt1) 532 && (ebd->elf_machine_alt2 == 0 || i_ehdrp->e_machine != ebd->elf_machine_alt2)) 533 { 534 const bfd_target * const *target_ptr; 535 536 if (ebd->elf_machine_code != EM_NONE) 537 goto got_wrong_format_error; 538 539 /* This is the generic ELF target. Let it match any ELF target 540 for which we do not have a specific backend. */ 541 for (target_ptr = bfd_target_vector; *target_ptr != NULL; target_ptr++) 542 { 543 struct elf_backend_data *back; 544 545 if ((*target_ptr)->flavour != bfd_target_elf_flavour) 546 continue; 547 back = (struct elf_backend_data *) (*target_ptr)->backend_data; 548 if (back->elf_machine_code == i_ehdrp->e_machine 549 || (back->elf_machine_alt1 != 0 550 && back->elf_machine_alt1 == i_ehdrp->e_machine) 551 || (back->elf_machine_alt2 != 0 552 && back->elf_machine_alt2 == i_ehdrp->e_machine)) 553 { 554 /* target_ptr is an ELF backend which matches this 555 object file, so reject the generic ELF target. */ 556 goto got_wrong_format_error; 557 } 558 } 559 } 560 561 if (i_ehdrp->e_type == ET_EXEC) 562 abfd->flags |= EXEC_P; 563 else if (i_ehdrp->e_type == ET_DYN) 564 abfd->flags |= DYNAMIC; 565 566 if (i_ehdrp->e_phnum > 0) 567 abfd->flags |= D_PAGED; 568 569 if (! bfd_default_set_arch_mach (abfd, ebd->arch, 0)) 570 goto got_no_match; 571 572 /* Remember the entry point specified in the ELF file header. */ 573 bfd_get_start_address (abfd) = i_ehdrp->e_entry; 574 575 /* Allocate space for a copy of the section header table in 576 internal form, seek to the section header table in the file, 577 read it in, and convert it to internal form. */ 578 i_shdrp = ((Elf_Internal_Shdr *) 579 bfd_alloc (abfd, sizeof (*i_shdrp) * i_ehdrp->e_shnum)); 580 elf_elfsections (abfd) = ((Elf_Internal_Shdr **) 581 bfd_alloc (abfd, 582 sizeof (i_shdrp) * i_ehdrp->e_shnum)); 583 if (!i_shdrp || !elf_elfsections (abfd)) 584 goto got_no_match; 585 if (bfd_seek (abfd, i_ehdrp->e_shoff, SEEK_SET) != 0) 586 goto got_no_match; 587 for (shindex = 0; shindex < i_ehdrp->e_shnum; shindex++) 588 { 589 if (bfd_read ((PTR) & x_shdr, sizeof x_shdr, 1, abfd) != sizeof (x_shdr)) 590 goto got_no_match; 591 elf_swap_shdr_in (abfd, &x_shdr, i_shdrp + shindex); 592 elf_elfsections (abfd)[shindex] = i_shdrp + shindex; 593 594 /* If the section is loaded, but not page aligned, clear 595 D_PAGED. */ 596 if ((i_shdrp[shindex].sh_flags & SHF_ALLOC) != 0 597 && i_shdrp[shindex].sh_type != SHT_NOBITS 598 && (((i_shdrp[shindex].sh_addr - i_shdrp[shindex].sh_offset) 599 % ebd->maxpagesize) 600 != 0)) 601 abfd->flags &= ~D_PAGED; 602 } 603 if (i_ehdrp->e_shstrndx) 604 { 605 if (! bfd_section_from_shdr (abfd, i_ehdrp->e_shstrndx)) 606 goto got_no_match; 607 } 608 609 /* Read in the program headers. */ 610 if (i_ehdrp->e_phnum == 0) 611 elf_tdata (abfd)->phdr = NULL; 612 else 613 { 614 Elf_Internal_Phdr *i_phdr; 615 unsigned int i; 616 617 elf_tdata (abfd)->phdr = ((Elf_Internal_Phdr *) 618 bfd_alloc (abfd, 619 (i_ehdrp->e_phnum 620 * sizeof (Elf_Internal_Phdr)))); 621 if (elf_tdata (abfd)->phdr == NULL) 622 goto got_no_match; 623 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0) 624 goto got_no_match; 625 i_phdr = elf_tdata (abfd)->phdr; 626 for (i = 0; i < i_ehdrp->e_phnum; i++, i_phdr++) 627 { 628 Elf_External_Phdr x_phdr; 629 630 if (bfd_read ((PTR) &x_phdr, sizeof x_phdr, 1, abfd) 631 != sizeof x_phdr) 632 goto got_no_match; 633 elf_swap_phdr_in (abfd, &x_phdr, i_phdr); 634 } 635 } 636 637 /* Read in the string table containing the names of the sections. We 638 will need the base pointer to this table later. */ 639 /* We read this inline now, so that we don't have to go through 640 bfd_section_from_shdr with it (since this particular strtab is 641 used to find all of the ELF section names.) */ 642 643 shstrtab = bfd_elf_get_str_section (abfd, i_ehdrp->e_shstrndx); 644 if (!shstrtab) 645 goto got_no_match; 646 647 /* Once all of the section headers have been read and converted, we 648 can start processing them. Note that the first section header is 649 a dummy placeholder entry, so we ignore it. */ 650 651 for (shindex = 1; shindex < i_ehdrp->e_shnum; shindex++) 652 { 653 if (! bfd_section_from_shdr (abfd, shindex)) 654 goto got_no_match; 655 } 656 657 /* Let the backend double check the format and override global 658 information. */ 659 if (ebd->elf_backend_object_p) 660 { 661 if ((*ebd->elf_backend_object_p) (abfd) == false) 662 goto got_wrong_format_error; 663 } 664 665 /* If we have created any reloc sections that are associated with 666 debugging sections, mark the reloc sections as debugging as well. */ 667 for (s = abfd->sections; s != NULL; s = s->next) 668 { 669 if ((elf_section_data (s)->this_hdr.sh_type == SHT_REL 670 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA) 671 && elf_section_data (s)->this_hdr.sh_info > 0) 672 { 673 unsigned long targ_index; 674 asection *targ_sec; 675 676 targ_index = elf_section_data (s)->this_hdr.sh_info; 677 targ_sec = bfd_section_from_elf_index (abfd, targ_index); 678 if (targ_sec != NULL 679 && (targ_sec->flags & SEC_DEBUGGING) != 0) 680 s->flags |= SEC_DEBUGGING; 681 } 682 } 683 684 return (abfd->xvec); 685 686got_wrong_format_error: 687 bfd_set_error (bfd_error_wrong_format); 688 goto got_no_match; 689got_no_match: 690 if (new_tdata != NULL 691 && new_tdata->elf_sect_ptr != NULL) 692 bfd_release (abfd, new_tdata->elf_sect_ptr); 693 if (i_shdrp != NULL) 694 bfd_release (abfd, i_shdrp); 695 if (new_tdata != NULL) 696 bfd_release (abfd, new_tdata); 697 elf_tdata (abfd) = preserved_tdata; 698 return (NULL); 699} 700 701/* ELF .o/exec file writing */ 702 703/* Write out the relocs. */ 704 705static void 706write_relocs (abfd, sec, data) 707 bfd *abfd; 708 asection *sec; 709 PTR data; 710{ 711 boolean *failedp = (boolean *) data; 712 Elf_Internal_Shdr *rela_hdr; 713 Elf_External_Rela *outbound_relocas; 714 Elf_External_Rel *outbound_relocs; 715 unsigned int idx; 716 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; 717 asymbol *last_sym = 0; 718 int last_sym_idx = 0; 719 720 /* If we have already failed, don't do anything. */ 721 if (*failedp) 722 return; 723 724 if ((sec->flags & SEC_RELOC) == 0) 725 return; 726 727 /* The linker backend writes the relocs out itself, and sets the 728 reloc_count field to zero to inhibit writing them here. Also, 729 sometimes the SEC_RELOC flag gets set even when there aren't any 730 relocs. */ 731 if (sec->reloc_count == 0) 732 return; 733 734 rela_hdr = &elf_section_data (sec)->rel_hdr; 735 736 rela_hdr->sh_size = rela_hdr->sh_entsize * sec->reloc_count; 737 rela_hdr->contents = (PTR) bfd_alloc (abfd, rela_hdr->sh_size); 738 if (rela_hdr->contents == NULL) 739 { 740 *failedp = true; 741 return; 742 } 743 744 /* orelocation has the data, reloc_count has the count... */ 745 if (use_rela_p) 746 { 747 outbound_relocas = (Elf_External_Rela *) rela_hdr->contents; 748 749 for (idx = 0; idx < sec->reloc_count; idx++) 750 { 751 Elf_Internal_Rela dst_rela; 752 Elf_External_Rela *src_rela; 753 arelent *ptr; 754 asymbol *sym; 755 int n; 756 757 ptr = sec->orelocation[idx]; 758 src_rela = outbound_relocas + idx; 759 760 /* The address of an ELF reloc is section relative for an object 761 file, and absolute for an executable file or shared library. 762 The address of a BFD reloc is always section relative. */ 763 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) 764 dst_rela.r_offset = ptr->address; 765 else 766 dst_rela.r_offset = ptr->address + sec->vma; 767 768 sym = *ptr->sym_ptr_ptr; 769 if (sym == last_sym) 770 n = last_sym_idx; 771 else if (bfd_is_abs_section (sym->section) && sym->value == 0) 772 n = STN_UNDEF; 773 else 774 { 775 last_sym = sym; 776 n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym); 777 if (n < 0) 778 { 779 *failedp = true; 780 return; 781 } 782 last_sym_idx = n; 783 } 784 785 if ((*ptr->sym_ptr_ptr)->the_bfd != NULL 786 && (*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec 787 && ! _bfd_elf_validate_reloc (abfd, ptr)) 788 { 789 *failedp = true; 790 return; 791 } 792 793 dst_rela.r_info = ELF_R_INFO (n, ptr->howto->type); 794 795 dst_rela.r_addend = ptr->addend; 796 elf_swap_reloca_out (abfd, &dst_rela, src_rela); 797 } 798 } 799 else 800 /* REL relocations */ 801 { 802 outbound_relocs = (Elf_External_Rel *) rela_hdr->contents; 803 804 for (idx = 0; idx < sec->reloc_count; idx++) 805 { 806 Elf_Internal_Rel dst_rel; 807 Elf_External_Rel *src_rel; 808 arelent *ptr; 809 int n; 810 asymbol *sym; 811 812 ptr = sec->orelocation[idx]; 813 sym = *ptr->sym_ptr_ptr; 814 src_rel = outbound_relocs + idx; 815 816 /* The address of an ELF reloc is section relative for an object 817 file, and absolute for an executable file or shared library. 818 The address of a BFD reloc is always section relative. */ 819 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) 820 dst_rel.r_offset = ptr->address; 821 else 822 dst_rel.r_offset = ptr->address + sec->vma; 823 824 if (sym == last_sym) 825 n = last_sym_idx; 826 else 827 { 828 last_sym = sym; 829 n = _bfd_elf_symbol_from_bfd_symbol (abfd, &sym); 830 if (n < 0) 831 { 832 *failedp = true; 833 return; 834 } 835 last_sym_idx = n; 836 } 837 838 if ((*ptr->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec 839 && ! _bfd_elf_validate_reloc (abfd, ptr)) 840 { 841 *failedp = true; 842 return; 843 } 844 845 dst_rel.r_info = ELF_R_INFO (n, ptr->howto->type); 846 847 elf_swap_reloc_out (abfd, &dst_rel, src_rel); 848 } 849 } 850} 851 852/* Write out the program headers. */ 853 854int 855elf_write_out_phdrs (abfd, phdr, count) 856 bfd *abfd; 857 const Elf_Internal_Phdr *phdr; 858 int count; 859{ 860 while (count--) 861 { 862 Elf_External_Phdr extphdr; 863 elf_swap_phdr_out (abfd, phdr, &extphdr); 864 if (bfd_write (&extphdr, sizeof (Elf_External_Phdr), 1, abfd) 865 != sizeof (Elf_External_Phdr)) 866 return -1; 867 phdr++; 868 } 869 return 0; 870} 871 872/* Write out the section headers and the ELF file header. */ 873 874boolean 875elf_write_shdrs_and_ehdr (abfd) 876 bfd *abfd; 877{ 878 Elf_External_Ehdr x_ehdr; /* Elf file header, external form */ 879 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ 880 Elf_External_Shdr *x_shdrp; /* Section header table, external form */ 881 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ 882 unsigned int count; 883 884 i_ehdrp = elf_elfheader (abfd); 885 i_shdrp = elf_elfsections (abfd); 886 887 /* swap the header before spitting it out... */ 888 889#if DEBUG & 1 890 elf_debug_file (i_ehdrp); 891#endif 892 elf_swap_ehdr_out (abfd, i_ehdrp, &x_ehdr); 893 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0 894 || (bfd_write ((PTR) & x_ehdr, sizeof (x_ehdr), 1, abfd) 895 != sizeof (x_ehdr))) 896 return false; 897 898 /* at this point we've concocted all the ELF sections... */ 899 x_shdrp = (Elf_External_Shdr *) 900 bfd_alloc (abfd, sizeof (*x_shdrp) * (i_ehdrp->e_shnum)); 901 if (!x_shdrp) 902 return false; 903 904 for (count = 0; count < i_ehdrp->e_shnum; count++) 905 { 906#if DEBUG & 2 907 elf_debug_section (count, i_shdrp[count]); 908#endif 909 elf_swap_shdr_out (abfd, i_shdrp[count], x_shdrp + count); 910 } 911 if (bfd_seek (abfd, (file_ptr) i_ehdrp->e_shoff, SEEK_SET) != 0 912 || (bfd_write ((PTR) x_shdrp, sizeof (*x_shdrp), i_ehdrp->e_shnum, abfd) 913 != sizeof (*x_shdrp) * i_ehdrp->e_shnum)) 914 return false; 915 916 /* need to dump the string table too... */ 917 918 return true; 919} 920 921long 922elf_slurp_symbol_table (abfd, symptrs, dynamic) 923 bfd *abfd; 924 asymbol **symptrs; /* Buffer for generated bfd symbols */ 925 boolean dynamic; 926{ 927 Elf_Internal_Shdr *hdr; 928 Elf_Internal_Shdr *verhdr; 929 long symcount; /* Number of external ELF symbols */ 930 elf_symbol_type *sym; /* Pointer to current bfd symbol */ 931 elf_symbol_type *symbase; /* Buffer for generated bfd symbols */ 932 Elf_Internal_Sym i_sym; 933 Elf_External_Sym *x_symp = NULL; 934 Elf_External_Versym *x_versymp = NULL; 935 936 /* Read each raw ELF symbol, converting from external ELF form to 937 internal ELF form, and then using the information to create a 938 canonical bfd symbol table entry. 939 940 Note that we allocate the initial bfd canonical symbol buffer 941 based on a one-to-one mapping of the ELF symbols to canonical 942 symbols. We actually use all the ELF symbols, so there will be no 943 space left over at the end. When we have all the symbols, we 944 build the caller's pointer vector. */ 945 946 if (! dynamic) 947 { 948 hdr = &elf_tdata (abfd)->symtab_hdr; 949 verhdr = NULL; 950 } 951 else 952 { 953 hdr = &elf_tdata (abfd)->dynsymtab_hdr; 954 if (elf_dynversym (abfd) == 0) 955 verhdr = NULL; 956 else 957 verhdr = &elf_tdata (abfd)->dynversym_hdr; 958 if ((elf_tdata (abfd)->dynverdef_section != 0 959 && elf_tdata (abfd)->verdef == NULL) 960 || (elf_tdata (abfd)->dynverref_section != 0 961 && elf_tdata (abfd)->verref == NULL)) 962 { 963 if (! _bfd_elf_slurp_version_tables (abfd)) 964 return -1; 965 } 966 } 967 968 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1) 969 return -1; 970 971 symcount = hdr->sh_size / sizeof (Elf_External_Sym); 972 973 if (symcount == 0) 974 sym = symbase = NULL; 975 else 976 { 977 long i; 978 979 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) == -1) 980 return -1; 981 982 symbase = ((elf_symbol_type *) 983 bfd_zalloc (abfd, symcount * sizeof (elf_symbol_type))); 984 if (symbase == (elf_symbol_type *) NULL) 985 return -1; 986 sym = symbase; 987 988 /* Temporarily allocate room for the raw ELF symbols. */ 989 x_symp = ((Elf_External_Sym *) 990 bfd_malloc (symcount * sizeof (Elf_External_Sym))); 991 if (x_symp == NULL && symcount != 0) 992 goto error_return; 993 994 if (bfd_read ((PTR) x_symp, sizeof (Elf_External_Sym), symcount, abfd) 995 != symcount * sizeof (Elf_External_Sym)) 996 goto error_return; 997 998 /* Read the raw ELF version symbol information. */ 999 1000 if (verhdr != NULL 1001 && verhdr->sh_size / sizeof (Elf_External_Versym) != symcount) 1002 { 1003 (*_bfd_error_handler) 1004 ("%s: version count (%ld) does not match symbol count (%ld)", 1005 abfd->filename, 1006 (long) (verhdr->sh_size / sizeof (Elf_External_Versym)), 1007 symcount); 1008 1009 /* Slurp in the symbols without the version information, 1010 since that is more helpful than just quitting. */ 1011 verhdr = NULL; 1012 } 1013 1014 if (verhdr != NULL) 1015 { 1016 if (bfd_seek (abfd, verhdr->sh_offset, SEEK_SET) != 0) 1017 goto error_return; 1018 1019 x_versymp = (Elf_External_Versym *) bfd_malloc (verhdr->sh_size); 1020 if (x_versymp == NULL && verhdr->sh_size != 0) 1021 goto error_return; 1022 1023 if (bfd_read ((PTR) x_versymp, 1, verhdr->sh_size, abfd) 1024 != verhdr->sh_size) 1025 goto error_return; 1026 } 1027 1028 /* Skip first symbol, which is a null dummy. */ 1029 for (i = 1; i < symcount; i++) 1030 { 1031 elf_swap_symbol_in (abfd, x_symp + i, &i_sym); 1032 memcpy (&sym->internal_elf_sym, &i_sym, sizeof (Elf_Internal_Sym)); 1033#ifdef ELF_KEEP_EXTSYM 1034 memcpy (&sym->native_elf_sym, x_symp + i, sizeof (Elf_External_Sym)); 1035#endif 1036 sym->symbol.the_bfd = abfd; 1037 1038 sym->symbol.name = bfd_elf_string_from_elf_section (abfd, 1039 hdr->sh_link, 1040 i_sym.st_name); 1041 1042 sym->symbol.value = i_sym.st_value; 1043 1044 if (i_sym.st_shndx > 0 && i_sym.st_shndx < SHN_LORESERVE) 1045 { 1046 sym->symbol.section = section_from_elf_index (abfd, 1047 i_sym.st_shndx); 1048 if (sym->symbol.section == NULL) 1049 { 1050 /* This symbol is in a section for which we did not 1051 create a BFD section. Just use bfd_abs_section, 1052 although it is wrong. FIXME. */ 1053 sym->symbol.section = bfd_abs_section_ptr; 1054 } 1055 } 1056 else if (i_sym.st_shndx == SHN_ABS) 1057 { 1058 sym->symbol.section = bfd_abs_section_ptr; 1059 } 1060 else if (i_sym.st_shndx == SHN_COMMON) 1061 { 1062 sym->symbol.section = bfd_com_section_ptr; 1063 /* Elf puts the alignment into the `value' field, and 1064 the size into the `size' field. BFD wants to see the 1065 size in the value field, and doesn't care (at the 1066 moment) about the alignment. */ 1067 sym->symbol.value = i_sym.st_size; 1068 } 1069 else if (i_sym.st_shndx == SHN_UNDEF) 1070 { 1071 sym->symbol.section = bfd_und_section_ptr; 1072 } 1073 else 1074 sym->symbol.section = bfd_abs_section_ptr; 1075 1076 /* If this is a relocateable file, then the symbol value is 1077 already section relative. */ 1078 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) 1079 sym->symbol.value -= sym->symbol.section->vma; 1080 1081 switch (ELF_ST_BIND (i_sym.st_info)) 1082 { 1083 case STB_LOCAL: 1084 sym->symbol.flags |= BSF_LOCAL; 1085 break; 1086 case STB_GLOBAL: 1087 if (i_sym.st_shndx != SHN_UNDEF 1088 && i_sym.st_shndx != SHN_COMMON) 1089 sym->symbol.flags |= BSF_GLOBAL; 1090 break; 1091 case STB_WEAK: 1092 sym->symbol.flags |= BSF_WEAK; 1093 break; 1094 } 1095 1096 switch (ELF_ST_TYPE (i_sym.st_info)) 1097 { 1098 case STT_SECTION: 1099 sym->symbol.flags |= BSF_SECTION_SYM | BSF_DEBUGGING; 1100 break; 1101 case STT_FILE: 1102 sym->symbol.flags |= BSF_FILE | BSF_DEBUGGING; 1103 break; 1104 case STT_FUNC: 1105 sym->symbol.flags |= BSF_FUNCTION; 1106 break; 1107 case STT_OBJECT: 1108 sym->symbol.flags |= BSF_OBJECT; 1109 break; 1110 } 1111 1112 if (dynamic) 1113 sym->symbol.flags |= BSF_DYNAMIC; 1114 1115 if (x_versymp != NULL) 1116 { 1117 Elf_Internal_Versym iversym; 1118 1119 _bfd_elf_swap_versym_in (abfd, x_versymp + i, &iversym); 1120 sym->version = iversym.vs_vers; 1121 } 1122 1123 /* Do some backend-specific processing on this symbol. */ 1124 { 1125 struct elf_backend_data *ebd = get_elf_backend_data (abfd); 1126 if (ebd->elf_backend_symbol_processing) 1127 (*ebd->elf_backend_symbol_processing) (abfd, &sym->symbol); 1128 } 1129 1130 sym++; 1131 } 1132 } 1133 1134 /* Do some backend-specific processing on this symbol table. */ 1135 { 1136 struct elf_backend_data *ebd = get_elf_backend_data (abfd); 1137 if (ebd->elf_backend_symbol_table_processing) 1138 (*ebd->elf_backend_symbol_table_processing) (abfd, symbase, symcount); 1139 } 1140 1141 /* We rely on the zalloc to clear out the final symbol entry. */ 1142 1143 symcount = sym - symbase; 1144 1145 /* Fill in the user's symbol pointer vector if needed. */ 1146 if (symptrs) 1147 { 1148 long l = symcount; 1149 1150 sym = symbase; 1151 while (l-- > 0) 1152 { 1153 *symptrs++ = &sym->symbol; 1154 sym++; 1155 } 1156 *symptrs = 0; /* Final null pointer */ 1157 } 1158 1159 if (x_versymp != NULL) 1160 free (x_versymp); 1161 if (x_symp != NULL) 1162 free (x_symp); 1163 return symcount; 1164error_return: 1165 if (x_versymp != NULL) 1166 free (x_versymp); 1167 if (x_symp != NULL) 1168 free (x_symp); 1169 return -1; 1170} 1171 1172/* Read in and swap the external relocs. */ 1173 1174static boolean 1175elf_slurp_reloc_table (abfd, asect, symbols, dynamic) 1176 bfd *abfd; 1177 asection *asect; 1178 asymbol **symbols; 1179 boolean dynamic; 1180{ 1181 struct elf_backend_data * const ebd = get_elf_backend_data (abfd); 1182 struct bfd_elf_section_data * const d = elf_section_data (asect); 1183 Elf_Internal_Shdr *rel_hdr; 1184 bfd_size_type reloc_count; 1185 PTR allocated = NULL; 1186 bfd_byte *native_relocs; 1187 arelent *relents; 1188 arelent *relent; 1189 unsigned int i; 1190 int entsize; 1191 1192 if (asect->relocation != NULL) 1193 return true; 1194 1195 if (! dynamic) 1196 { 1197 if ((asect->flags & SEC_RELOC) == 0 1198 || asect->reloc_count == 0) 1199 return true; 1200 1201 rel_hdr = &d->rel_hdr; 1202 reloc_count = asect->reloc_count; 1203 1204 BFD_ASSERT (asect->rel_filepos == rel_hdr->sh_offset 1205 && reloc_count == rel_hdr->sh_size / rel_hdr->sh_entsize); 1206 } 1207 else 1208 { 1209 if (asect->_raw_size == 0) 1210 return true; 1211 1212 rel_hdr = &d->this_hdr; 1213 reloc_count = rel_hdr->sh_size / rel_hdr->sh_entsize; 1214 } 1215 1216 allocated = (PTR) bfd_malloc ((size_t) rel_hdr->sh_size); 1217 if (allocated == NULL) 1218 goto error_return; 1219 1220 if (bfd_seek (abfd, rel_hdr->sh_offset, SEEK_SET) != 0 1221 || (bfd_read (allocated, 1, rel_hdr->sh_size, abfd) 1222 != rel_hdr->sh_size)) 1223 goto error_return; 1224 1225 native_relocs = (bfd_byte *) allocated; 1226 1227 relents = (arelent *) bfd_alloc (abfd, reloc_count * sizeof (arelent)); 1228 if (relents == NULL) 1229 goto error_return; 1230 1231 entsize = rel_hdr->sh_entsize; 1232 BFD_ASSERT (entsize == sizeof (Elf_External_Rel) 1233 || entsize == sizeof (Elf_External_Rela)); 1234 1235 for (i = 0, relent = relents; 1236 i < reloc_count; 1237 i++, relent++, native_relocs += entsize) 1238 { 1239 Elf_Internal_Rela rela; 1240 Elf_Internal_Rel rel; 1241 1242 if (entsize == sizeof (Elf_External_Rela)) 1243 elf_swap_reloca_in (abfd, (Elf_External_Rela *) native_relocs, &rela); 1244 else 1245 { 1246 elf_swap_reloc_in (abfd, (Elf_External_Rel *) native_relocs, &rel); 1247 rela.r_offset = rel.r_offset; 1248 rela.r_info = rel.r_info; 1249 rela.r_addend = 0; 1250 } 1251 1252 /* The address of an ELF reloc is section relative for an object 1253 file, and absolute for an executable file or shared library. 1254 The address of a normal BFD reloc is always section relative, 1255 and the address of a dynamic reloc is absolute.. */ 1256 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 || dynamic) 1257 relent->address = rela.r_offset; 1258 else 1259 relent->address = rela.r_offset - asect->vma; 1260 1261 if (ELF_R_SYM (rela.r_info) == 0) 1262 relent->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr; 1263 else 1264 { 1265 asymbol **ps, *s; 1266 1267 ps = symbols + ELF_R_SYM (rela.r_info) - 1; 1268 s = *ps; 1269 1270 /* Canonicalize ELF section symbols. FIXME: Why? */ 1271 if ((s->flags & BSF_SECTION_SYM) == 0) 1272 relent->sym_ptr_ptr = ps; 1273 else 1274 relent->sym_ptr_ptr = s->section->symbol_ptr_ptr; 1275 } 1276 1277 relent->addend = rela.r_addend; 1278 1279 if (entsize == sizeof (Elf_External_Rela)) 1280 (*ebd->elf_info_to_howto) (abfd, relent, &rela); 1281 else 1282 (*ebd->elf_info_to_howto_rel) (abfd, relent, &rel); 1283 } 1284 1285 asect->relocation = relents; 1286 1287 if (allocated != NULL) 1288 free (allocated); 1289 1290 return true; 1291 1292 error_return: 1293 if (allocated != NULL) 1294 free (allocated); 1295 return false; 1296} 1297 1298#ifdef DEBUG 1299static void 1300elf_debug_section (num, hdr) 1301 int num; 1302 Elf_Internal_Shdr *hdr; 1303{ 1304 fprintf (stderr, "\nSection#%d '%s' 0x%.8lx\n", num, 1305 hdr->bfd_section != NULL ? hdr->bfd_section->name : "", 1306 (long) hdr); 1307 fprintf (stderr, 1308 "sh_name = %ld\tsh_type = %ld\tsh_flags = %ld\n", 1309 (long) hdr->sh_name, 1310 (long) hdr->sh_type, 1311 (long) hdr->sh_flags); 1312 fprintf (stderr, 1313 "sh_addr = %ld\tsh_offset = %ld\tsh_size = %ld\n", 1314 (long) hdr->sh_addr, 1315 (long) hdr->sh_offset, 1316 (long) hdr->sh_size); 1317 fprintf (stderr, 1318 "sh_link = %ld\tsh_info = %ld\tsh_addralign = %ld\n", 1319 (long) hdr->sh_link, 1320 (long) hdr->sh_info, 1321 (long) hdr->sh_addralign); 1322 fprintf (stderr, "sh_entsize = %ld\n", 1323 (long) hdr->sh_entsize); 1324 fflush (stderr); 1325} 1326 1327static void 1328elf_debug_file (ehdrp) 1329 Elf_Internal_Ehdr *ehdrp; 1330{ 1331 fprintf (stderr, "e_entry = 0x%.8lx\n", (long) ehdrp->e_entry); 1332 fprintf (stderr, "e_phoff = %ld\n", (long) ehdrp->e_phoff); 1333 fprintf (stderr, "e_phnum = %ld\n", (long) ehdrp->e_phnum); 1334 fprintf (stderr, "e_phentsize = %ld\n", (long) ehdrp->e_phentsize); 1335 fprintf (stderr, "e_shoff = %ld\n", (long) ehdrp->e_shoff); 1336 fprintf (stderr, "e_shnum = %ld\n", (long) ehdrp->e_shnum); 1337 fprintf (stderr, "e_shentsize = %ld\n", (long) ehdrp->e_shentsize); 1338} 1339 1340static char * 1341elf_symbol_flags (flags) 1342 flagword flags; 1343{ 1344 static char buffer[1024]; 1345 1346 buffer[0] = '\0'; 1347 if (flags & BSF_LOCAL) 1348 strcat (buffer, " local"); 1349 1350 if (flags & BSF_GLOBAL) 1351 strcat (buffer, " global"); 1352 1353 if (flags & BSF_DEBUGGING) 1354 strcat (buffer, " debug"); 1355 1356 if (flags & BSF_FUNCTION) 1357 strcat (buffer, " function"); 1358 1359 if (flags & BSF_KEEP) 1360 strcat (buffer, " keep"); 1361 1362 if (flags & BSF_KEEP_G) 1363 strcat (buffer, " keep_g"); 1364 1365 if (flags & BSF_WEAK) 1366 strcat (buffer, " weak"); 1367 1368 if (flags & BSF_SECTION_SYM) 1369 strcat (buffer, " section-sym"); 1370 1371 if (flags & BSF_OLD_COMMON) 1372 strcat (buffer, " old-common"); 1373 1374 if (flags & BSF_NOT_AT_END) 1375 strcat (buffer, " not-at-end"); 1376 1377 if (flags & BSF_CONSTRUCTOR) 1378 strcat (buffer, " constructor"); 1379 1380 if (flags & BSF_WARNING) 1381 strcat (buffer, " warning"); 1382 1383 if (flags & BSF_INDIRECT) 1384 strcat (buffer, " indirect"); 1385 1386 if (flags & BSF_FILE) 1387 strcat (buffer, " file"); 1388 1389 if (flags & DYNAMIC) 1390 strcat (buffer, " dynamic"); 1391 1392 if (flags & ~(BSF_LOCAL 1393 | BSF_GLOBAL 1394 | BSF_DEBUGGING 1395 | BSF_FUNCTION 1396 | BSF_KEEP 1397 | BSF_KEEP_G 1398 | BSF_WEAK 1399 | BSF_SECTION_SYM 1400 | BSF_OLD_COMMON 1401 | BSF_NOT_AT_END 1402 | BSF_CONSTRUCTOR 1403 | BSF_WARNING 1404 | BSF_INDIRECT 1405 | BSF_FILE 1406 | BSF_DYNAMIC)) 1407 strcat (buffer, " unknown-bits"); 1408 1409 return buffer; 1410} 1411#endif 1412 1413#include "elfcore.h" 1414#include "elflink.h" 1415 1416/* Size-dependent data and functions. */ 1417const struct elf_size_info NAME(_bfd_elf,size_info) = { 1418 sizeof (Elf_External_Ehdr), 1419 sizeof (Elf_External_Phdr), 1420 sizeof (Elf_External_Shdr), 1421 sizeof (Elf_External_Rel), 1422 sizeof (Elf_External_Rela), 1423 sizeof (Elf_External_Sym), 1424 sizeof (Elf_External_Dyn), 1425 sizeof (Elf_External_Note), 1426 1427 ARCH_SIZE, FILE_ALIGN, 1428 ELFCLASS, EV_CURRENT, 1429 elf_write_out_phdrs, 1430 elf_write_shdrs_and_ehdr, 1431 write_relocs, 1432 elf_swap_symbol_out, 1433 elf_slurp_reloc_table, 1434 elf_slurp_symbol_table, 1435 elf_swap_dyn_in 1436}; 1437