elf.c revision 104838
1/* ELF executable support for BFD. 2 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 3 Free Software Foundation, Inc. 4 5This file is part of BFD, the Binary File Descriptor library. 6 7This program is free software; you can redistribute it and/or modify 8it under the terms of the GNU General Public License as published by 9the Free Software Foundation; either version 2 of the License, or 10(at your option) any later version. 11 12This program is distributed in the hope that it will be useful, 13but WITHOUT ANY WARRANTY; without even the implied warranty of 14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15GNU General Public License for more details. 16 17You should have received a copy of the GNU General Public License 18along with this program; if not, write to the Free Software 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 20 21 22/* $FreeBSD: head/contrib/binutils/bfd/elf.c 104838 2002-10-11 06:08:01Z obrien $ */ 23 24 25/* SECTION 26 27 ELF backends 28 29 BFD support for ELF formats is being worked on. 30 Currently, the best supported back ends are for sparc and i386 31 (running svr4 or Solaris 2). 32 33 Documentation of the internals of the support code still needs 34 to be written. The code is changing quickly enough that we 35 haven't bothered yet. */ 36 37/* For sparc64-cross-sparc32. */ 38#define _SYSCALL32 39#include "bfd.h" 40#include "sysdep.h" 41#include "bfdlink.h" 42#include "libbfd.h" 43#define ARCH_SIZE 0 44#include "elf-bfd.h" 45#include "libiberty.h" 46 47static INLINE struct elf_segment_map *make_mapping 48 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean)); 49static boolean map_sections_to_segments PARAMS ((bfd *)); 50static int elf_sort_sections PARAMS ((const PTR, const PTR)); 51static boolean assign_file_positions_for_segments PARAMS ((bfd *)); 52static boolean assign_file_positions_except_relocs PARAMS ((bfd *)); 53static boolean prep_headers PARAMS ((bfd *)); 54static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **, int)); 55static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *)); 56static char *elf_read PARAMS ((bfd *, file_ptr, bfd_size_type)); 57static const char *group_signature PARAMS ((bfd *, Elf_Internal_Shdr *)); 58static boolean setup_group PARAMS ((bfd *, Elf_Internal_Shdr *, asection *)); 59static void merge_sections_remove_hook PARAMS ((bfd *, asection *)); 60static void elf_fake_sections PARAMS ((bfd *, asection *, PTR)); 61static boolean assign_section_numbers PARAMS ((bfd *)); 62static INLINE int sym_is_global PARAMS ((bfd *, asymbol *)); 63static boolean elf_map_symbols PARAMS ((bfd *)); 64static bfd_size_type get_program_header_size PARAMS ((bfd *)); 65static boolean elfcore_read_notes PARAMS ((bfd *, file_ptr, bfd_size_type)); 66static boolean elf_find_function PARAMS ((bfd *, asection *, asymbol **, 67 bfd_vma, const char **, 68 const char **)); 69static int elfcore_make_pid PARAMS ((bfd *)); 70static boolean elfcore_maybe_make_sect PARAMS ((bfd *, char *, asection *)); 71static boolean elfcore_make_note_pseudosection PARAMS ((bfd *, char *, 72 Elf_Internal_Note *)); 73static boolean elfcore_grok_prfpreg PARAMS ((bfd *, Elf_Internal_Note *)); 74static boolean elfcore_grok_prxfpreg PARAMS ((bfd *, Elf_Internal_Note *)); 75static boolean elfcore_grok_note PARAMS ((bfd *, Elf_Internal_Note *)); 76 77static boolean elfcore_netbsd_get_lwpid PARAMS ((Elf_Internal_Note *, int *)); 78static boolean elfcore_grok_netbsd_procinfo PARAMS ((bfd *, 79 Elf_Internal_Note *)); 80static boolean elfcore_grok_netbsd_note PARAMS ((bfd *, Elf_Internal_Note *)); 81 82/* Swap version information in and out. The version information is 83 currently size independent. If that ever changes, this code will 84 need to move into elfcode.h. */ 85 86/* Swap in a Verdef structure. */ 87 88void 89_bfd_elf_swap_verdef_in (abfd, src, dst) 90 bfd *abfd; 91 const Elf_External_Verdef *src; 92 Elf_Internal_Verdef *dst; 93{ 94 dst->vd_version = H_GET_16 (abfd, src->vd_version); 95 dst->vd_flags = H_GET_16 (abfd, src->vd_flags); 96 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); 97 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); 98 dst->vd_hash = H_GET_32 (abfd, src->vd_hash); 99 dst->vd_aux = H_GET_32 (abfd, src->vd_aux); 100 dst->vd_next = H_GET_32 (abfd, src->vd_next); 101} 102 103/* Swap out a Verdef structure. */ 104 105void 106_bfd_elf_swap_verdef_out (abfd, src, dst) 107 bfd *abfd; 108 const Elf_Internal_Verdef *src; 109 Elf_External_Verdef *dst; 110{ 111 H_PUT_16 (abfd, src->vd_version, dst->vd_version); 112 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); 113 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); 114 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); 115 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); 116 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); 117 H_PUT_32 (abfd, src->vd_next, dst->vd_next); 118} 119 120/* Swap in a Verdaux structure. */ 121 122void 123_bfd_elf_swap_verdaux_in (abfd, src, dst) 124 bfd *abfd; 125 const Elf_External_Verdaux *src; 126 Elf_Internal_Verdaux *dst; 127{ 128 dst->vda_name = H_GET_32 (abfd, src->vda_name); 129 dst->vda_next = H_GET_32 (abfd, src->vda_next); 130} 131 132/* Swap out a Verdaux structure. */ 133 134void 135_bfd_elf_swap_verdaux_out (abfd, src, dst) 136 bfd *abfd; 137 const Elf_Internal_Verdaux *src; 138 Elf_External_Verdaux *dst; 139{ 140 H_PUT_32 (abfd, src->vda_name, dst->vda_name); 141 H_PUT_32 (abfd, src->vda_next, dst->vda_next); 142} 143 144/* Swap in a Verneed structure. */ 145 146void 147_bfd_elf_swap_verneed_in (abfd, src, dst) 148 bfd *abfd; 149 const Elf_External_Verneed *src; 150 Elf_Internal_Verneed *dst; 151{ 152 dst->vn_version = H_GET_16 (abfd, src->vn_version); 153 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); 154 dst->vn_file = H_GET_32 (abfd, src->vn_file); 155 dst->vn_aux = H_GET_32 (abfd, src->vn_aux); 156 dst->vn_next = H_GET_32 (abfd, src->vn_next); 157} 158 159/* Swap out a Verneed structure. */ 160 161void 162_bfd_elf_swap_verneed_out (abfd, src, dst) 163 bfd *abfd; 164 const Elf_Internal_Verneed *src; 165 Elf_External_Verneed *dst; 166{ 167 H_PUT_16 (abfd, src->vn_version, dst->vn_version); 168 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); 169 H_PUT_32 (abfd, src->vn_file, dst->vn_file); 170 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); 171 H_PUT_32 (abfd, src->vn_next, dst->vn_next); 172} 173 174/* Swap in a Vernaux structure. */ 175 176void 177_bfd_elf_swap_vernaux_in (abfd, src, dst) 178 bfd *abfd; 179 const Elf_External_Vernaux *src; 180 Elf_Internal_Vernaux *dst; 181{ 182 dst->vna_hash = H_GET_32 (abfd, src->vna_hash); 183 dst->vna_flags = H_GET_16 (abfd, src->vna_flags); 184 dst->vna_other = H_GET_16 (abfd, src->vna_other); 185 dst->vna_name = H_GET_32 (abfd, src->vna_name); 186 dst->vna_next = H_GET_32 (abfd, src->vna_next); 187} 188 189/* Swap out a Vernaux structure. */ 190 191void 192_bfd_elf_swap_vernaux_out (abfd, src, dst) 193 bfd *abfd; 194 const Elf_Internal_Vernaux *src; 195 Elf_External_Vernaux *dst; 196{ 197 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); 198 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); 199 H_PUT_16 (abfd, src->vna_other, dst->vna_other); 200 H_PUT_32 (abfd, src->vna_name, dst->vna_name); 201 H_PUT_32 (abfd, src->vna_next, dst->vna_next); 202} 203 204/* Swap in a Versym structure. */ 205 206void 207_bfd_elf_swap_versym_in (abfd, src, dst) 208 bfd *abfd; 209 const Elf_External_Versym *src; 210 Elf_Internal_Versym *dst; 211{ 212 dst->vs_vers = H_GET_16 (abfd, src->vs_vers); 213} 214 215/* Swap out a Versym structure. */ 216 217void 218_bfd_elf_swap_versym_out (abfd, src, dst) 219 bfd *abfd; 220 const Elf_Internal_Versym *src; 221 Elf_External_Versym *dst; 222{ 223 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); 224} 225 226/* Standard ELF hash function. Do not change this function; you will 227 cause invalid hash tables to be generated. */ 228 229unsigned long 230bfd_elf_hash (namearg) 231 const char *namearg; 232{ 233 const unsigned char *name = (const unsigned char *) namearg; 234 unsigned long h = 0; 235 unsigned long g; 236 int ch; 237 238 while ((ch = *name++) != '\0') 239 { 240 h = (h << 4) + ch; 241 if ((g = (h & 0xf0000000)) != 0) 242 { 243 h ^= g >> 24; 244 /* The ELF ABI says `h &= ~g', but this is equivalent in 245 this case and on some machines one insn instead of two. */ 246 h ^= g; 247 } 248 } 249 return h; 250} 251 252/* Read a specified number of bytes at a specified offset in an ELF 253 file, into a newly allocated buffer, and return a pointer to the 254 buffer. */ 255 256static char * 257elf_read (abfd, offset, size) 258 bfd *abfd; 259 file_ptr offset; 260 bfd_size_type size; 261{ 262 char *buf; 263 264 if ((buf = bfd_alloc (abfd, size)) == NULL) 265 return NULL; 266 if (bfd_seek (abfd, offset, SEEK_SET) != 0) 267 return NULL; 268 if (bfd_bread ((PTR) buf, size, abfd) != size) 269 { 270 if (bfd_get_error () != bfd_error_system_call) 271 bfd_set_error (bfd_error_file_truncated); 272 return NULL; 273 } 274 return buf; 275} 276 277boolean 278bfd_elf_mkobject (abfd) 279 bfd *abfd; 280{ 281 /* This just does initialization. */ 282 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */ 283 bfd_size_type amt = sizeof (struct elf_obj_tdata); 284 elf_tdata (abfd) = (struct elf_obj_tdata *) bfd_zalloc (abfd, amt); 285 if (elf_tdata (abfd) == 0) 286 return false; 287 /* Since everything is done at close time, do we need any 288 initialization? */ 289 290 return true; 291} 292 293boolean 294bfd_elf_mkcorefile (abfd) 295 bfd *abfd; 296{ 297 /* I think this can be done just like an object file. */ 298 return bfd_elf_mkobject (abfd); 299} 300 301char * 302bfd_elf_get_str_section (abfd, shindex) 303 bfd *abfd; 304 unsigned int shindex; 305{ 306 Elf_Internal_Shdr **i_shdrp; 307 char *shstrtab = NULL; 308 file_ptr offset; 309 bfd_size_type shstrtabsize; 310 311 i_shdrp = elf_elfsections (abfd); 312 if (i_shdrp == 0 || i_shdrp[shindex] == 0) 313 return 0; 314 315 shstrtab = (char *) i_shdrp[shindex]->contents; 316 if (shstrtab == NULL) 317 { 318 /* No cached one, attempt to read, and cache what we read. */ 319 offset = i_shdrp[shindex]->sh_offset; 320 shstrtabsize = i_shdrp[shindex]->sh_size; 321 shstrtab = elf_read (abfd, offset, shstrtabsize); 322 i_shdrp[shindex]->contents = (PTR) shstrtab; 323 } 324 return shstrtab; 325} 326 327char * 328bfd_elf_string_from_elf_section (abfd, shindex, strindex) 329 bfd *abfd; 330 unsigned int shindex; 331 unsigned int strindex; 332{ 333 Elf_Internal_Shdr *hdr; 334 335 if (strindex == 0) 336 return ""; 337 338 hdr = elf_elfsections (abfd)[shindex]; 339 340 if (hdr->contents == NULL 341 && bfd_elf_get_str_section (abfd, shindex) == NULL) 342 return NULL; 343 344 if (strindex >= hdr->sh_size) 345 { 346 (*_bfd_error_handler) 347 (_("%s: invalid string offset %u >= %lu for section `%s'"), 348 bfd_archive_filename (abfd), strindex, (unsigned long) hdr->sh_size, 349 ((shindex == elf_elfheader(abfd)->e_shstrndx 350 && strindex == hdr->sh_name) 351 ? ".shstrtab" 352 : elf_string_from_elf_strtab (abfd, hdr->sh_name))); 353 return ""; 354 } 355 356 return ((char *) hdr->contents) + strindex; 357} 358 359/* Read and convert symbols to internal format. 360 SYMCOUNT specifies the number of symbols to read, starting from 361 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF 362 are non-NULL, they are used to store the internal symbols, external 363 symbols, and symbol section index extensions, respectively. */ 364 365Elf_Internal_Sym * 366bfd_elf_get_elf_syms (ibfd, symtab_hdr, symcount, symoffset, 367 intsym_buf, extsym_buf, extshndx_buf) 368 bfd *ibfd; 369 Elf_Internal_Shdr *symtab_hdr; 370 size_t symcount; 371 size_t symoffset; 372 Elf_Internal_Sym *intsym_buf; 373 PTR extsym_buf; 374 Elf_External_Sym_Shndx *extshndx_buf; 375{ 376 Elf_Internal_Shdr *shndx_hdr; 377 PTR alloc_ext; 378 const bfd_byte *esym; 379 Elf_External_Sym_Shndx *alloc_extshndx; 380 Elf_External_Sym_Shndx *shndx; 381 Elf_Internal_Sym *isym; 382 Elf_Internal_Sym *isymend; 383 struct elf_backend_data *bed; 384 size_t extsym_size; 385 bfd_size_type amt; 386 file_ptr pos; 387 388 if (symcount == 0) 389 return intsym_buf; 390 391 /* Normal syms might have section extension entries. */ 392 shndx_hdr = NULL; 393 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr) 394 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr; 395 396 /* Read the symbols. */ 397 alloc_ext = NULL; 398 alloc_extshndx = NULL; 399 bed = get_elf_backend_data (ibfd); 400 extsym_size = bed->s->sizeof_sym; 401 amt = symcount * extsym_size; 402 pos = symtab_hdr->sh_offset + symoffset * extsym_size; 403 if (extsym_buf == NULL) 404 { 405 alloc_ext = bfd_malloc (amt); 406 extsym_buf = alloc_ext; 407 } 408 if (extsym_buf == NULL 409 || bfd_seek (ibfd, pos, SEEK_SET) != 0 410 || bfd_bread (extsym_buf, amt, ibfd) != amt) 411 { 412 intsym_buf = NULL; 413 goto out; 414 } 415 416 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) 417 extshndx_buf = NULL; 418 else 419 { 420 amt = symcount * sizeof (Elf_External_Sym_Shndx); 421 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); 422 if (extshndx_buf == NULL) 423 { 424 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); 425 extshndx_buf = alloc_extshndx; 426 } 427 if (extshndx_buf == NULL 428 || bfd_seek (ibfd, pos, SEEK_SET) != 0 429 || bfd_bread (extshndx_buf, amt, ibfd) != amt) 430 { 431 intsym_buf = NULL; 432 goto out; 433 } 434 } 435 436 if (intsym_buf == NULL) 437 { 438 bfd_size_type amt = symcount * sizeof (Elf_Internal_Sym); 439 intsym_buf = (Elf_Internal_Sym *) bfd_malloc (amt); 440 if (intsym_buf == NULL) 441 goto out; 442 } 443 444 /* Convert the symbols to internal form. */ 445 isymend = intsym_buf + symcount; 446 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf; 447 isym < isymend; 448 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) 449 (*bed->s->swap_symbol_in) (ibfd, esym, (const PTR) shndx, isym); 450 451 out: 452 if (alloc_ext != NULL) 453 free (alloc_ext); 454 if (alloc_extshndx != NULL) 455 free (alloc_extshndx); 456 457 return intsym_buf; 458} 459 460/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP 461 sections. The first element is the flags, the rest are section 462 pointers. */ 463 464typedef union elf_internal_group { 465 Elf_Internal_Shdr *shdr; 466 unsigned int flags; 467} Elf_Internal_Group; 468 469/* Return the name of the group signature symbol. Why isn't the 470 signature just a string? */ 471 472static const char * 473group_signature (abfd, ghdr) 474 bfd *abfd; 475 Elf_Internal_Shdr *ghdr; 476{ 477 Elf_Internal_Shdr *hdr; 478 unsigned char esym[sizeof (Elf64_External_Sym)]; 479 Elf_External_Sym_Shndx eshndx; 480 Elf_Internal_Sym isym; 481 unsigned int iname; 482 unsigned int shindex; 483 484 /* First we need to ensure the symbol table is available. */ 485 if (! bfd_section_from_shdr (abfd, ghdr->sh_link)) 486 return NULL; 487 488 /* Go read the symbol. */ 489 hdr = &elf_tdata (abfd)->symtab_hdr; 490 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, 491 &isym, esym, &eshndx) == NULL) 492 return NULL; 493 494 /* Look up the symbol name. */ 495 iname = isym.st_name; 496 shindex = hdr->sh_link; 497 if (iname == 0 && ELF_ST_TYPE (isym.st_info) == STT_SECTION) 498 { 499 iname = elf_elfsections (abfd)[isym.st_shndx]->sh_name; 500 shindex = elf_elfheader (abfd)->e_shstrndx; 501 } 502 503 return bfd_elf_string_from_elf_section (abfd, shindex, iname); 504} 505 506/* Set next_in_group list pointer, and group name for NEWSECT. */ 507 508static boolean 509setup_group (abfd, hdr, newsect) 510 bfd *abfd; 511 Elf_Internal_Shdr *hdr; 512 asection *newsect; 513{ 514 unsigned int num_group = elf_tdata (abfd)->num_group; 515 516 /* If num_group is zero, read in all SHT_GROUP sections. The count 517 is set to -1 if there are no SHT_GROUP sections. */ 518 if (num_group == 0) 519 { 520 unsigned int i, shnum; 521 522 /* First count the number of groups. If we have a SHT_GROUP 523 section with just a flag word (ie. sh_size is 4), ignore it. */ 524 shnum = elf_numsections (abfd); 525 num_group = 0; 526 for (i = 0; i < shnum; i++) 527 { 528 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; 529 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8) 530 num_group += 1; 531 } 532 533 if (num_group == 0) 534 num_group = (unsigned) -1; 535 elf_tdata (abfd)->num_group = num_group; 536 537 if (num_group > 0) 538 { 539 /* We keep a list of elf section headers for group sections, 540 so we can find them quickly. */ 541 bfd_size_type amt = num_group * sizeof (Elf_Internal_Shdr *); 542 elf_tdata (abfd)->group_sect_ptr = bfd_alloc (abfd, amt); 543 if (elf_tdata (abfd)->group_sect_ptr == NULL) 544 return false; 545 546 num_group = 0; 547 for (i = 0; i < shnum; i++) 548 { 549 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; 550 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8) 551 { 552 unsigned char *src; 553 Elf_Internal_Group *dest; 554 555 /* Add to list of sections. */ 556 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; 557 num_group += 1; 558 559 /* Read the raw contents. */ 560 BFD_ASSERT (sizeof (*dest) >= 4); 561 amt = shdr->sh_size * sizeof (*dest) / 4; 562 shdr->contents = bfd_alloc (abfd, amt); 563 if (shdr->contents == NULL 564 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 565 || (bfd_bread (shdr->contents, shdr->sh_size, abfd) 566 != shdr->sh_size)) 567 return false; 568 569 /* Translate raw contents, a flag word followed by an 570 array of elf section indices all in target byte order, 571 to the flag word followed by an array of elf section 572 pointers. */ 573 src = shdr->contents + shdr->sh_size; 574 dest = (Elf_Internal_Group *) (shdr->contents + amt); 575 while (1) 576 { 577 unsigned int idx; 578 579 src -= 4; 580 --dest; 581 idx = H_GET_32 (abfd, src); 582 if (src == shdr->contents) 583 { 584 dest->flags = idx; 585 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) 586 shdr->bfd_section->flags 587 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 588 break; 589 } 590 if (idx >= shnum) 591 { 592 ((*_bfd_error_handler) 593 (_("%s: invalid SHT_GROUP entry"), 594 bfd_archive_filename (abfd))); 595 idx = 0; 596 } 597 dest->shdr = elf_elfsections (abfd)[idx]; 598 } 599 } 600 } 601 } 602 } 603 604 if (num_group != (unsigned) -1) 605 { 606 unsigned int i; 607 608 for (i = 0; i < num_group; i++) 609 { 610 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; 611 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; 612 unsigned int n_elt = shdr->sh_size / 4; 613 614 /* Look through this group's sections to see if current 615 section is a member. */ 616 while (--n_elt != 0) 617 if ((++idx)->shdr == hdr) 618 { 619 asection *s = NULL; 620 621 /* We are a member of this group. Go looking through 622 other members to see if any others are linked via 623 next_in_group. */ 624 idx = (Elf_Internal_Group *) shdr->contents; 625 n_elt = shdr->sh_size / 4; 626 while (--n_elt != 0) 627 if ((s = (++idx)->shdr->bfd_section) != NULL 628 && elf_next_in_group (s) != NULL) 629 break; 630 if (n_elt != 0) 631 { 632 /* Snarf the group name from other member, and 633 insert current section in circular list. */ 634 elf_group_name (newsect) = elf_group_name (s); 635 elf_next_in_group (newsect) = elf_next_in_group (s); 636 elf_next_in_group (s) = newsect; 637 } 638 else 639 { 640 const char *gname; 641 642 gname = group_signature (abfd, shdr); 643 if (gname == NULL) 644 return false; 645 elf_group_name (newsect) = gname; 646 647 /* Start a circular list with one element. */ 648 elf_next_in_group (newsect) = newsect; 649 } 650 651 /* If the group section has been created, point to the 652 new member. */ 653 if (shdr->bfd_section != NULL) 654 elf_next_in_group (shdr->bfd_section) = newsect; 655 656 i = num_group - 1; 657 break; 658 } 659 } 660 } 661 662 if (elf_group_name (newsect) == NULL) 663 { 664 (*_bfd_error_handler) (_("%s: no group info for section %s"), 665 bfd_archive_filename (abfd), newsect->name); 666 } 667 return true; 668} 669 670boolean 671bfd_elf_discard_group (abfd, group) 672 bfd *abfd ATTRIBUTE_UNUSED; 673 asection *group; 674{ 675 asection *first = elf_next_in_group (group); 676 asection *s = first; 677 678 while (s != NULL) 679 { 680 s->output_section = bfd_abs_section_ptr; 681 s = elf_next_in_group (s); 682 /* These lists are circular. */ 683 if (s == first) 684 break; 685 } 686 return true; 687} 688 689/* Make a BFD section from an ELF section. We store a pointer to the 690 BFD section in the bfd_section field of the header. */ 691 692boolean 693_bfd_elf_make_section_from_shdr (abfd, hdr, name) 694 bfd *abfd; 695 Elf_Internal_Shdr *hdr; 696 const char *name; 697{ 698 asection *newsect; 699 flagword flags; 700 struct elf_backend_data *bed; 701 702 if (hdr->bfd_section != NULL) 703 { 704 BFD_ASSERT (strcmp (name, 705 bfd_get_section_name (abfd, hdr->bfd_section)) == 0); 706 return true; 707 } 708 709 newsect = bfd_make_section_anyway (abfd, name); 710 if (newsect == NULL) 711 return false; 712 713 newsect->filepos = hdr->sh_offset; 714 715 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) 716 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) 717 || ! bfd_set_section_alignment (abfd, newsect, 718 bfd_log2 ((bfd_vma) hdr->sh_addralign))) 719 return false; 720 721 flags = SEC_NO_FLAGS; 722 if (hdr->sh_type != SHT_NOBITS) 723 flags |= SEC_HAS_CONTENTS; 724 if (hdr->sh_type == SHT_GROUP) 725 flags |= SEC_GROUP | SEC_EXCLUDE; 726 if ((hdr->sh_flags & SHF_ALLOC) != 0) 727 { 728 flags |= SEC_ALLOC; 729 if (hdr->sh_type != SHT_NOBITS) 730 flags |= SEC_LOAD; 731 } 732 if ((hdr->sh_flags & SHF_WRITE) == 0) 733 flags |= SEC_READONLY; 734 if ((hdr->sh_flags & SHF_EXECINSTR) != 0) 735 flags |= SEC_CODE; 736 else if ((flags & SEC_LOAD) != 0) 737 flags |= SEC_DATA; 738 if ((hdr->sh_flags & SHF_MERGE) != 0) 739 { 740 flags |= SEC_MERGE; 741 newsect->entsize = hdr->sh_entsize; 742 if ((hdr->sh_flags & SHF_STRINGS) != 0) 743 flags |= SEC_STRINGS; 744 } 745 if (hdr->sh_flags & SHF_GROUP) 746 if (!setup_group (abfd, hdr, newsect)) 747 return false; 748 if ((hdr->sh_flags & SHF_TLS) != 0) 749 flags |= SEC_THREAD_LOCAL; 750 751 /* The debugging sections appear to be recognized only by name, not 752 any sort of flag. */ 753 { 754 static const char *debug_sec_names [] = 755 { 756 ".debug", 757 ".gnu.linkonce.wi.", 758 ".line", 759 ".stab" 760 }; 761 int i; 762 763 for (i = ARRAY_SIZE (debug_sec_names); i--;) 764 if (strncmp (name, debug_sec_names[i], strlen (debug_sec_names[i])) == 0) 765 break; 766 767 if (i >= 0) 768 flags |= SEC_DEBUGGING; 769 } 770 771 /* As a GNU extension, if the name begins with .gnu.linkonce, we 772 only link a single copy of the section. This is used to support 773 g++. g++ will emit each template expansion in its own section. 774 The symbols will be defined as weak, so that multiple definitions 775 are permitted. The GNU linker extension is to actually discard 776 all but one of the sections. */ 777 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0 778 && elf_next_in_group (newsect) == NULL) 779 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 780 781 bed = get_elf_backend_data (abfd); 782 if (bed->elf_backend_section_flags) 783 if (! bed->elf_backend_section_flags (&flags, hdr)) 784 return false; 785 786 if (! bfd_set_section_flags (abfd, newsect, flags)) 787 return false; 788 789 if ((flags & SEC_ALLOC) != 0) 790 { 791 Elf_Internal_Phdr *phdr; 792 unsigned int i; 793 794 /* Look through the phdrs to see if we need to adjust the lma. 795 If all the p_paddr fields are zero, we ignore them, since 796 some ELF linkers produce such output. */ 797 phdr = elf_tdata (abfd)->phdr; 798 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 799 { 800 if (phdr->p_paddr != 0) 801 break; 802 } 803 if (i < elf_elfheader (abfd)->e_phnum) 804 { 805 phdr = elf_tdata (abfd)->phdr; 806 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 807 { 808 /* This section is part of this segment if its file 809 offset plus size lies within the segment's memory 810 span and, if the section is loaded, the extent of the 811 loaded data lies within the extent of the segment. 812 813 Note - we used to check the p_paddr field as well, and 814 refuse to set the LMA if it was 0. This is wrong 815 though, as a perfectly valid initialised segment can 816 have a p_paddr of zero. Some architectures, eg ARM, 817 place special significance on the address 0 and 818 executables need to be able to have a segment which 819 covers this address. */ 820 if (phdr->p_type == PT_LOAD 821 && (bfd_vma) hdr->sh_offset >= phdr->p_offset 822 && (hdr->sh_offset + hdr->sh_size 823 <= phdr->p_offset + phdr->p_memsz) 824 && ((flags & SEC_LOAD) == 0 825 || (hdr->sh_offset + hdr->sh_size 826 <= phdr->p_offset + phdr->p_filesz))) 827 { 828 if ((flags & SEC_LOAD) == 0) 829 newsect->lma = (phdr->p_paddr 830 + hdr->sh_addr - phdr->p_vaddr); 831 else 832 /* We used to use the same adjustment for SEC_LOAD 833 sections, but that doesn't work if the segment 834 is packed with code from multiple VMAs. 835 Instead we calculate the section LMA based on 836 the segment LMA. It is assumed that the 837 segment will contain sections with contiguous 838 LMAs, even if the VMAs are not. */ 839 newsect->lma = (phdr->p_paddr 840 + hdr->sh_offset - phdr->p_offset); 841 842 /* With contiguous segments, we can't tell from file 843 offsets whether a section with zero size should 844 be placed at the end of one segment or the 845 beginning of the next. Decide based on vaddr. */ 846 if (hdr->sh_addr >= phdr->p_vaddr 847 && (hdr->sh_addr + hdr->sh_size 848 <= phdr->p_vaddr + phdr->p_memsz)) 849 break; 850 } 851 } 852 } 853 } 854 855 hdr->bfd_section = newsect; 856 elf_section_data (newsect)->this_hdr = *hdr; 857 858 return true; 859} 860 861/* 862INTERNAL_FUNCTION 863 bfd_elf_find_section 864 865SYNOPSIS 866 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name); 867 868DESCRIPTION 869 Helper functions for GDB to locate the string tables. 870 Since BFD hides string tables from callers, GDB needs to use an 871 internal hook to find them. Sun's .stabstr, in particular, 872 isn't even pointed to by the .stab section, so ordinary 873 mechanisms wouldn't work to find it, even if we had some. 874*/ 875 876struct elf_internal_shdr * 877bfd_elf_find_section (abfd, name) 878 bfd *abfd; 879 char *name; 880{ 881 Elf_Internal_Shdr **i_shdrp; 882 char *shstrtab; 883 unsigned int max; 884 unsigned int i; 885 886 i_shdrp = elf_elfsections (abfd); 887 if (i_shdrp != NULL) 888 { 889 shstrtab = bfd_elf_get_str_section (abfd, 890 elf_elfheader (abfd)->e_shstrndx); 891 if (shstrtab != NULL) 892 { 893 max = elf_numsections (abfd); 894 for (i = 1; i < max; i++) 895 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name)) 896 return i_shdrp[i]; 897 } 898 } 899 return 0; 900} 901 902const char *const bfd_elf_section_type_names[] = { 903 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", 904 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", 905 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", 906}; 907 908/* ELF relocs are against symbols. If we are producing relocateable 909 output, and the reloc is against an external symbol, and nothing 910 has given us any additional addend, the resulting reloc will also 911 be against the same symbol. In such a case, we don't want to 912 change anything about the way the reloc is handled, since it will 913 all be done at final link time. Rather than put special case code 914 into bfd_perform_relocation, all the reloc types use this howto 915 function. It just short circuits the reloc if producing 916 relocateable output against an external symbol. */ 917 918bfd_reloc_status_type 919bfd_elf_generic_reloc (abfd, 920 reloc_entry, 921 symbol, 922 data, 923 input_section, 924 output_bfd, 925 error_message) 926 bfd *abfd ATTRIBUTE_UNUSED; 927 arelent *reloc_entry; 928 asymbol *symbol; 929 PTR data ATTRIBUTE_UNUSED; 930 asection *input_section; 931 bfd *output_bfd; 932 char **error_message ATTRIBUTE_UNUSED; 933{ 934 if (output_bfd != (bfd *) NULL 935 && (symbol->flags & BSF_SECTION_SYM) == 0 936 && (! reloc_entry->howto->partial_inplace 937 || reloc_entry->addend == 0)) 938 { 939 reloc_entry->address += input_section->output_offset; 940 return bfd_reloc_ok; 941 } 942 943 return bfd_reloc_continue; 944} 945 946/* Make sure sec_info_type is cleared if sec_info is cleared too. */ 947 948static void 949merge_sections_remove_hook (abfd, sec) 950 bfd *abfd ATTRIBUTE_UNUSED; 951 asection *sec; 952{ 953 struct bfd_elf_section_data *sec_data; 954 955 sec_data = elf_section_data (sec); 956 BFD_ASSERT (sec_data->sec_info_type == ELF_INFO_TYPE_MERGE); 957 sec_data->sec_info_type = ELF_INFO_TYPE_NONE; 958} 959 960/* Finish SHF_MERGE section merging. */ 961 962boolean 963_bfd_elf_merge_sections (abfd, info) 964 bfd *abfd; 965 struct bfd_link_info *info; 966{ 967 if (!is_elf_hash_table (info)) 968 return false; 969 if (elf_hash_table (info)->merge_info) 970 _bfd_merge_sections (abfd, elf_hash_table (info)->merge_info, 971 merge_sections_remove_hook); 972 return true; 973} 974 975void 976_bfd_elf_link_just_syms (sec, info) 977 asection *sec; 978 struct bfd_link_info *info; 979{ 980 sec->output_section = bfd_abs_section_ptr; 981 sec->output_offset = sec->vma; 982 if (!is_elf_hash_table (info)) 983 return; 984 985 elf_section_data (sec)->sec_info_type = ELF_INFO_TYPE_JUST_SYMS; 986} 987 988/* Copy the program header and other data from one object module to 989 another. */ 990 991boolean 992_bfd_elf_copy_private_bfd_data (ibfd, obfd) 993 bfd *ibfd; 994 bfd *obfd; 995{ 996 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 997 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 998 return true; 999 1000 BFD_ASSERT (!elf_flags_init (obfd) 1001 || (elf_elfheader (obfd)->e_flags 1002 == elf_elfheader (ibfd)->e_flags)); 1003 1004 elf_gp (obfd) = elf_gp (ibfd); 1005 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; 1006 elf_flags_init (obfd) = true; 1007 return true; 1008} 1009 1010/* Print out the program headers. */ 1011 1012boolean 1013_bfd_elf_print_private_bfd_data (abfd, farg) 1014 bfd *abfd; 1015 PTR farg; 1016{ 1017 FILE *f = (FILE *) farg; 1018 Elf_Internal_Phdr *p; 1019 asection *s; 1020 bfd_byte *dynbuf = NULL; 1021 1022 p = elf_tdata (abfd)->phdr; 1023 if (p != NULL) 1024 { 1025 unsigned int i, c; 1026 1027 fprintf (f, _("\nProgram Header:\n")); 1028 c = elf_elfheader (abfd)->e_phnum; 1029 for (i = 0; i < c; i++, p++) 1030 { 1031 const char *pt; 1032 char buf[20]; 1033 1034 switch (p->p_type) 1035 { 1036 case PT_NULL: pt = "NULL"; break; 1037 case PT_LOAD: pt = "LOAD"; break; 1038 case PT_DYNAMIC: pt = "DYNAMIC"; break; 1039 case PT_INTERP: pt = "INTERP"; break; 1040 case PT_NOTE: pt = "NOTE"; break; 1041 case PT_SHLIB: pt = "SHLIB"; break; 1042 case PT_PHDR: pt = "PHDR"; break; 1043 case PT_TLS: pt = "TLS"; break; 1044 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; 1045 default: sprintf (buf, "0x%lx", p->p_type); pt = buf; break; 1046 } 1047 fprintf (f, "%8s off 0x", pt); 1048 bfd_fprintf_vma (abfd, f, p->p_offset); 1049 fprintf (f, " vaddr 0x"); 1050 bfd_fprintf_vma (abfd, f, p->p_vaddr); 1051 fprintf (f, " paddr 0x"); 1052 bfd_fprintf_vma (abfd, f, p->p_paddr); 1053 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); 1054 fprintf (f, " filesz 0x"); 1055 bfd_fprintf_vma (abfd, f, p->p_filesz); 1056 fprintf (f, " memsz 0x"); 1057 bfd_fprintf_vma (abfd, f, p->p_memsz); 1058 fprintf (f, " flags %c%c%c", 1059 (p->p_flags & PF_R) != 0 ? 'r' : '-', 1060 (p->p_flags & PF_W) != 0 ? 'w' : '-', 1061 (p->p_flags & PF_X) != 0 ? 'x' : '-'); 1062 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) 1063 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); 1064 fprintf (f, "\n"); 1065 } 1066 } 1067 1068 s = bfd_get_section_by_name (abfd, ".dynamic"); 1069 if (s != NULL) 1070 { 1071 int elfsec; 1072 unsigned long shlink; 1073 bfd_byte *extdyn, *extdynend; 1074 size_t extdynsize; 1075 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *)); 1076 1077 fprintf (f, _("\nDynamic Section:\n")); 1078 1079 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size); 1080 if (dynbuf == NULL) 1081 goto error_return; 1082 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0, 1083 s->_raw_size)) 1084 goto error_return; 1085 1086 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 1087 if (elfsec == -1) 1088 goto error_return; 1089 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 1090 1091 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 1092 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 1093 1094 extdyn = dynbuf; 1095 extdynend = extdyn + s->_raw_size; 1096 for (; extdyn < extdynend; extdyn += extdynsize) 1097 { 1098 Elf_Internal_Dyn dyn; 1099 const char *name; 1100 char ab[20]; 1101 boolean stringp; 1102 1103 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn); 1104 1105 if (dyn.d_tag == DT_NULL) 1106 break; 1107 1108 stringp = false; 1109 switch (dyn.d_tag) 1110 { 1111 default: 1112 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); 1113 name = ab; 1114 break; 1115 1116 case DT_NEEDED: name = "NEEDED"; stringp = true; break; 1117 case DT_PLTRELSZ: name = "PLTRELSZ"; break; 1118 case DT_PLTGOT: name = "PLTGOT"; break; 1119 case DT_HASH: name = "HASH"; break; 1120 case DT_STRTAB: name = "STRTAB"; break; 1121 case DT_SYMTAB: name = "SYMTAB"; break; 1122 case DT_RELA: name = "RELA"; break; 1123 case DT_RELASZ: name = "RELASZ"; break; 1124 case DT_RELAENT: name = "RELAENT"; break; 1125 case DT_STRSZ: name = "STRSZ"; break; 1126 case DT_SYMENT: name = "SYMENT"; break; 1127 case DT_INIT: name = "INIT"; break; 1128 case DT_FINI: name = "FINI"; break; 1129 case DT_SONAME: name = "SONAME"; stringp = true; break; 1130 case DT_RPATH: name = "RPATH"; stringp = true; break; 1131 case DT_SYMBOLIC: name = "SYMBOLIC"; break; 1132 case DT_REL: name = "REL"; break; 1133 case DT_RELSZ: name = "RELSZ"; break; 1134 case DT_RELENT: name = "RELENT"; break; 1135 case DT_PLTREL: name = "PLTREL"; break; 1136 case DT_DEBUG: name = "DEBUG"; break; 1137 case DT_TEXTREL: name = "TEXTREL"; break; 1138 case DT_JMPREL: name = "JMPREL"; break; 1139 case DT_BIND_NOW: name = "BIND_NOW"; break; 1140 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; 1141 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; 1142 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; 1143 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; 1144 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break; 1145 case DT_FLAGS: name = "FLAGS"; break; 1146 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; 1147 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; 1148 case DT_CHECKSUM: name = "CHECKSUM"; break; 1149 case DT_PLTPADSZ: name = "PLTPADSZ"; break; 1150 case DT_MOVEENT: name = "MOVEENT"; break; 1151 case DT_MOVESZ: name = "MOVESZ"; break; 1152 case DT_FEATURE: name = "FEATURE"; break; 1153 case DT_POSFLAG_1: name = "POSFLAG_1"; break; 1154 case DT_SYMINSZ: name = "SYMINSZ"; break; 1155 case DT_SYMINENT: name = "SYMINENT"; break; 1156 case DT_CONFIG: name = "CONFIG"; stringp = true; break; 1157 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break; 1158 case DT_AUDIT: name = "AUDIT"; stringp = true; break; 1159 case DT_PLTPAD: name = "PLTPAD"; break; 1160 case DT_MOVETAB: name = "MOVETAB"; break; 1161 case DT_SYMINFO: name = "SYMINFO"; break; 1162 case DT_RELACOUNT: name = "RELACOUNT"; break; 1163 case DT_RELCOUNT: name = "RELCOUNT"; break; 1164 case DT_FLAGS_1: name = "FLAGS_1"; break; 1165 case DT_VERSYM: name = "VERSYM"; break; 1166 case DT_VERDEF: name = "VERDEF"; break; 1167 case DT_VERDEFNUM: name = "VERDEFNUM"; break; 1168 case DT_VERNEED: name = "VERNEED"; break; 1169 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; 1170 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break; 1171 case DT_USED: name = "USED"; break; 1172 case DT_FILTER: name = "FILTER"; stringp = true; break; 1173 } 1174 1175 fprintf (f, " %-11s ", name); 1176 if (! stringp) 1177 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val); 1178 else 1179 { 1180 const char *string; 1181 unsigned int tagv = dyn.d_un.d_val; 1182 1183 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 1184 if (string == NULL) 1185 goto error_return; 1186 fprintf (f, "%s", string); 1187 } 1188 fprintf (f, "\n"); 1189 } 1190 1191 free (dynbuf); 1192 dynbuf = NULL; 1193 } 1194 1195 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) 1196 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) 1197 { 1198 if (! _bfd_elf_slurp_version_tables (abfd)) 1199 return false; 1200 } 1201 1202 if (elf_dynverdef (abfd) != 0) 1203 { 1204 Elf_Internal_Verdef *t; 1205 1206 fprintf (f, _("\nVersion definitions:\n")); 1207 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) 1208 { 1209 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, 1210 t->vd_flags, t->vd_hash, t->vd_nodename); 1211 if (t->vd_auxptr->vda_nextptr != NULL) 1212 { 1213 Elf_Internal_Verdaux *a; 1214 1215 fprintf (f, "\t"); 1216 for (a = t->vd_auxptr->vda_nextptr; 1217 a != NULL; 1218 a = a->vda_nextptr) 1219 fprintf (f, "%s ", a->vda_nodename); 1220 fprintf (f, "\n"); 1221 } 1222 } 1223 } 1224 1225 if (elf_dynverref (abfd) != 0) 1226 { 1227 Elf_Internal_Verneed *t; 1228 1229 fprintf (f, _("\nVersion References:\n")); 1230 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) 1231 { 1232 Elf_Internal_Vernaux *a; 1233 1234 fprintf (f, _(" required from %s:\n"), t->vn_filename); 1235 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 1236 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, 1237 a->vna_flags, a->vna_other, a->vna_nodename); 1238 } 1239 } 1240 1241 return true; 1242 1243 error_return: 1244 if (dynbuf != NULL) 1245 free (dynbuf); 1246 return false; 1247} 1248 1249/* Display ELF-specific fields of a symbol. */ 1250 1251void 1252bfd_elf_print_symbol (abfd, filep, symbol, how) 1253 bfd *abfd; 1254 PTR filep; 1255 asymbol *symbol; 1256 bfd_print_symbol_type how; 1257{ 1258 FILE *file = (FILE *) filep; 1259 switch (how) 1260 { 1261 case bfd_print_symbol_name: 1262 fprintf (file, "%s", symbol->name); 1263 break; 1264 case bfd_print_symbol_more: 1265 fprintf (file, "elf "); 1266 bfd_fprintf_vma (abfd, file, symbol->value); 1267 fprintf (file, " %lx", (long) symbol->flags); 1268 break; 1269 case bfd_print_symbol_all: 1270 { 1271 const char *section_name; 1272 const char *name = NULL; 1273 struct elf_backend_data *bed; 1274 unsigned char st_other; 1275 bfd_vma val; 1276 1277 section_name = symbol->section ? symbol->section->name : "(*none*)"; 1278 1279 bed = get_elf_backend_data (abfd); 1280 if (bed->elf_backend_print_symbol_all) 1281 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); 1282 1283 if (name == NULL) 1284 { 1285 name = symbol->name; 1286 bfd_print_symbol_vandf (abfd, (PTR) file, symbol); 1287 } 1288 1289 fprintf (file, " %s\t", section_name); 1290 /* Print the "other" value for a symbol. For common symbols, 1291 we've already printed the size; now print the alignment. 1292 For other symbols, we have no specified alignment, and 1293 we've printed the address; now print the size. */ 1294 if (bfd_is_com_section (symbol->section)) 1295 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; 1296 else 1297 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; 1298 bfd_fprintf_vma (abfd, file, val); 1299 1300 /* If we have version information, print it. */ 1301 if (elf_tdata (abfd)->dynversym_section != 0 1302 && (elf_tdata (abfd)->dynverdef_section != 0 1303 || elf_tdata (abfd)->dynverref_section != 0)) 1304 { 1305 unsigned int vernum; 1306 const char *version_string; 1307 1308 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION; 1309 1310 if (vernum == 0) 1311 version_string = ""; 1312 else if (vernum == 1) 1313 version_string = "Base"; 1314 else if (vernum <= elf_tdata (abfd)->cverdefs) 1315 version_string = 1316 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 1317 else 1318 { 1319 Elf_Internal_Verneed *t; 1320 1321 version_string = ""; 1322 for (t = elf_tdata (abfd)->verref; 1323 t != NULL; 1324 t = t->vn_nextref) 1325 { 1326 Elf_Internal_Vernaux *a; 1327 1328 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 1329 { 1330 if (a->vna_other == vernum) 1331 { 1332 version_string = a->vna_nodename; 1333 break; 1334 } 1335 } 1336 } 1337 } 1338 1339 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0) 1340 fprintf (file, " %-11s", version_string); 1341 else 1342 { 1343 int i; 1344 1345 fprintf (file, " (%s)", version_string); 1346 for (i = 10 - strlen (version_string); i > 0; --i) 1347 putc (' ', file); 1348 } 1349 } 1350 1351 /* If the st_other field is not zero, print it. */ 1352 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; 1353 1354 switch (st_other) 1355 { 1356 case 0: break; 1357 case STV_INTERNAL: fprintf (file, " .internal"); break; 1358 case STV_HIDDEN: fprintf (file, " .hidden"); break; 1359 case STV_PROTECTED: fprintf (file, " .protected"); break; 1360 default: 1361 /* Some other non-defined flags are also present, so print 1362 everything hex. */ 1363 fprintf (file, " 0x%02x", (unsigned int) st_other); 1364 } 1365 1366 fprintf (file, " %s", name); 1367 } 1368 break; 1369 } 1370} 1371 1372/* Create an entry in an ELF linker hash table. */ 1373 1374struct bfd_hash_entry * 1375_bfd_elf_link_hash_newfunc (entry, table, string) 1376 struct bfd_hash_entry *entry; 1377 struct bfd_hash_table *table; 1378 const char *string; 1379{ 1380 /* Allocate the structure if it has not already been allocated by a 1381 subclass. */ 1382 if (entry == NULL) 1383 { 1384 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); 1385 if (entry == NULL) 1386 return entry; 1387 } 1388 1389 /* Call the allocation method of the superclass. */ 1390 entry = _bfd_link_hash_newfunc (entry, table, string); 1391 if (entry != NULL) 1392 { 1393 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; 1394 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; 1395 1396 /* Set local fields. */ 1397 ret->indx = -1; 1398 ret->size = 0; 1399 ret->dynindx = -1; 1400 ret->dynstr_index = 0; 1401 ret->weakdef = NULL; 1402 ret->got.refcount = htab->init_refcount; 1403 ret->plt.refcount = htab->init_refcount; 1404 ret->linker_section_pointer = NULL; 1405 ret->verinfo.verdef = NULL; 1406 ret->vtable_entries_used = NULL; 1407 ret->vtable_entries_size = 0; 1408 ret->vtable_parent = NULL; 1409 ret->type = STT_NOTYPE; 1410 ret->other = 0; 1411 /* Assume that we have been called by a non-ELF symbol reader. 1412 This flag is then reset by the code which reads an ELF input 1413 file. This ensures that a symbol created by a non-ELF symbol 1414 reader will have the flag set correctly. */ 1415 ret->elf_link_hash_flags = ELF_LINK_NON_ELF; 1416 } 1417 1418 return entry; 1419} 1420 1421/* Copy data from an indirect symbol to its direct symbol, hiding the 1422 old indirect symbol. Also used for copying flags to a weakdef. */ 1423 1424void 1425_bfd_elf_link_hash_copy_indirect (bed, dir, ind) 1426 struct elf_backend_data *bed; 1427 struct elf_link_hash_entry *dir, *ind; 1428{ 1429 bfd_signed_vma tmp; 1430 bfd_signed_vma lowest_valid = bed->can_refcount; 1431 1432 /* Copy down any references that we may have already seen to the 1433 symbol which just became indirect. */ 1434 1435 dir->elf_link_hash_flags |= 1436 (ind->elf_link_hash_flags 1437 & (ELF_LINK_HASH_REF_DYNAMIC 1438 | ELF_LINK_HASH_REF_REGULAR 1439 | ELF_LINK_HASH_REF_REGULAR_NONWEAK 1440 | ELF_LINK_NON_GOT_REF)); 1441 1442 if (ind->root.type != bfd_link_hash_indirect) 1443 return; 1444 1445 /* Copy over the global and procedure linkage table refcount entries. 1446 These may have been already set up by a check_relocs routine. */ 1447 tmp = dir->got.refcount; 1448 if (tmp < lowest_valid) 1449 { 1450 dir->got.refcount = ind->got.refcount; 1451 ind->got.refcount = tmp; 1452 } 1453 else 1454 BFD_ASSERT (ind->got.refcount < lowest_valid); 1455 1456 tmp = dir->plt.refcount; 1457 if (tmp < lowest_valid) 1458 { 1459 dir->plt.refcount = ind->plt.refcount; 1460 ind->plt.refcount = tmp; 1461 } 1462 else 1463 BFD_ASSERT (ind->plt.refcount < lowest_valid); 1464 1465 if (dir->dynindx == -1) 1466 { 1467 dir->dynindx = ind->dynindx; 1468 dir->dynstr_index = ind->dynstr_index; 1469 ind->dynindx = -1; 1470 ind->dynstr_index = 0; 1471 } 1472 else 1473 BFD_ASSERT (ind->dynindx == -1); 1474} 1475 1476void 1477_bfd_elf_link_hash_hide_symbol (info, h, force_local) 1478 struct bfd_link_info *info; 1479 struct elf_link_hash_entry *h; 1480 boolean force_local; 1481{ 1482 h->plt.offset = (bfd_vma) -1; 1483 h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; 1484 if (force_local) 1485 { 1486 h->elf_link_hash_flags |= ELF_LINK_FORCED_LOCAL; 1487 if (h->dynindx != -1) 1488 { 1489 h->dynindx = -1; 1490 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, 1491 h->dynstr_index); 1492 } 1493 } 1494} 1495 1496/* Initialize an ELF linker hash table. */ 1497 1498boolean 1499_bfd_elf_link_hash_table_init (table, abfd, newfunc) 1500 struct elf_link_hash_table *table; 1501 bfd *abfd; 1502 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *, 1503 struct bfd_hash_table *, 1504 const char *)); 1505{ 1506 boolean ret; 1507 1508 table->dynamic_sections_created = false; 1509 table->dynobj = NULL; 1510 table->init_refcount = get_elf_backend_data (abfd)->can_refcount - 1; 1511 /* The first dynamic symbol is a dummy. */ 1512 table->dynsymcount = 1; 1513 table->dynstr = NULL; 1514 table->bucketcount = 0; 1515 table->needed = NULL; 1516 table->runpath = NULL; 1517 table->loaded = NULL; 1518 table->hgot = NULL; 1519 table->stab_info = NULL; 1520 table->merge_info = NULL; 1521 table->dynlocal = NULL; 1522 ret = _bfd_link_hash_table_init (& table->root, abfd, newfunc); 1523 table->root.type = bfd_link_elf_hash_table; 1524 1525 return ret; 1526} 1527 1528/* Create an ELF linker hash table. */ 1529 1530struct bfd_link_hash_table * 1531_bfd_elf_link_hash_table_create (abfd) 1532 bfd *abfd; 1533{ 1534 struct elf_link_hash_table *ret; 1535 bfd_size_type amt = sizeof (struct elf_link_hash_table); 1536 1537 ret = (struct elf_link_hash_table *) bfd_malloc (amt); 1538 if (ret == (struct elf_link_hash_table *) NULL) 1539 return NULL; 1540 1541 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc)) 1542 { 1543 free (ret); 1544 return NULL; 1545 } 1546 1547 return &ret->root; 1548} 1549 1550/* This is a hook for the ELF emulation code in the generic linker to 1551 tell the backend linker what file name to use for the DT_NEEDED 1552 entry for a dynamic object. The generic linker passes name as an 1553 empty string to indicate that no DT_NEEDED entry should be made. */ 1554 1555void 1556bfd_elf_set_dt_needed_name (abfd, name) 1557 bfd *abfd; 1558 const char *name; 1559{ 1560 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 1561 && bfd_get_format (abfd) == bfd_object) 1562 elf_dt_name (abfd) = name; 1563} 1564 1565void 1566bfd_elf_set_dt_needed_soname (abfd, name) 1567 bfd *abfd; 1568 const char *name; 1569{ 1570 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 1571 && bfd_get_format (abfd) == bfd_object) 1572 elf_dt_soname (abfd) = name; 1573} 1574 1575/* Get the list of DT_NEEDED entries for a link. This is a hook for 1576 the linker ELF emulation code. */ 1577 1578struct bfd_link_needed_list * 1579bfd_elf_get_needed_list (abfd, info) 1580 bfd *abfd ATTRIBUTE_UNUSED; 1581 struct bfd_link_info *info; 1582{ 1583 if (info->hash->creator->flavour != bfd_target_elf_flavour) 1584 return NULL; 1585 return elf_hash_table (info)->needed; 1586} 1587 1588/* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a 1589 hook for the linker ELF emulation code. */ 1590 1591struct bfd_link_needed_list * 1592bfd_elf_get_runpath_list (abfd, info) 1593 bfd *abfd ATTRIBUTE_UNUSED; 1594 struct bfd_link_info *info; 1595{ 1596 if (info->hash->creator->flavour != bfd_target_elf_flavour) 1597 return NULL; 1598 return elf_hash_table (info)->runpath; 1599} 1600 1601/* Get the name actually used for a dynamic object for a link. This 1602 is the SONAME entry if there is one. Otherwise, it is the string 1603 passed to bfd_elf_set_dt_needed_name, or it is the filename. */ 1604 1605const char * 1606bfd_elf_get_dt_soname (abfd) 1607 bfd *abfd; 1608{ 1609 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 1610 && bfd_get_format (abfd) == bfd_object) 1611 return elf_dt_name (abfd); 1612 return NULL; 1613} 1614 1615/* Get the list of DT_NEEDED entries from a BFD. This is a hook for 1616 the ELF linker emulation code. */ 1617 1618boolean 1619bfd_elf_get_bfd_needed_list (abfd, pneeded) 1620 bfd *abfd; 1621 struct bfd_link_needed_list **pneeded; 1622{ 1623 asection *s; 1624 bfd_byte *dynbuf = NULL; 1625 int elfsec; 1626 unsigned long shlink; 1627 bfd_byte *extdyn, *extdynend; 1628 size_t extdynsize; 1629 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *)); 1630 1631 *pneeded = NULL; 1632 1633 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour 1634 || bfd_get_format (abfd) != bfd_object) 1635 return true; 1636 1637 s = bfd_get_section_by_name (abfd, ".dynamic"); 1638 if (s == NULL || s->_raw_size == 0) 1639 return true; 1640 1641 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size); 1642 if (dynbuf == NULL) 1643 goto error_return; 1644 1645 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0, 1646 s->_raw_size)) 1647 goto error_return; 1648 1649 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 1650 if (elfsec == -1) 1651 goto error_return; 1652 1653 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 1654 1655 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 1656 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 1657 1658 extdyn = dynbuf; 1659 extdynend = extdyn + s->_raw_size; 1660 for (; extdyn < extdynend; extdyn += extdynsize) 1661 { 1662 Elf_Internal_Dyn dyn; 1663 1664 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn); 1665 1666 if (dyn.d_tag == DT_NULL) 1667 break; 1668 1669 if (dyn.d_tag == DT_NEEDED) 1670 { 1671 const char *string; 1672 struct bfd_link_needed_list *l; 1673 unsigned int tagv = dyn.d_un.d_val; 1674 bfd_size_type amt; 1675 1676 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 1677 if (string == NULL) 1678 goto error_return; 1679 1680 amt = sizeof *l; 1681 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 1682 if (l == NULL) 1683 goto error_return; 1684 1685 l->by = abfd; 1686 l->name = string; 1687 l->next = *pneeded; 1688 *pneeded = l; 1689 } 1690 } 1691 1692 free (dynbuf); 1693 1694 return true; 1695 1696 error_return: 1697 if (dynbuf != NULL) 1698 free (dynbuf); 1699 return false; 1700} 1701 1702/* Allocate an ELF string table--force the first byte to be zero. */ 1703 1704struct bfd_strtab_hash * 1705_bfd_elf_stringtab_init () 1706{ 1707 struct bfd_strtab_hash *ret; 1708 1709 ret = _bfd_stringtab_init (); 1710 if (ret != NULL) 1711 { 1712 bfd_size_type loc; 1713 1714 loc = _bfd_stringtab_add (ret, "", true, false); 1715 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); 1716 if (loc == (bfd_size_type) -1) 1717 { 1718 _bfd_stringtab_free (ret); 1719 ret = NULL; 1720 } 1721 } 1722 return ret; 1723} 1724 1725/* ELF .o/exec file reading */ 1726 1727/* Create a new bfd section from an ELF section header. */ 1728 1729boolean 1730bfd_section_from_shdr (abfd, shindex) 1731 bfd *abfd; 1732 unsigned int shindex; 1733{ 1734 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex]; 1735 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); 1736 struct elf_backend_data *bed = get_elf_backend_data (abfd); 1737 const char *name; 1738 1739 name = elf_string_from_elf_strtab (abfd, hdr->sh_name); 1740 1741 switch (hdr->sh_type) 1742 { 1743 case SHT_NULL: 1744 /* Inactive section. Throw it away. */ 1745 return true; 1746 1747 case SHT_PROGBITS: /* Normal section with contents. */ 1748 case SHT_NOBITS: /* .bss section. */ 1749 case SHT_HASH: /* .hash section. */ 1750 case SHT_NOTE: /* .note section. */ 1751 case SHT_INIT_ARRAY: /* .init_array section. */ 1752 case SHT_FINI_ARRAY: /* .fini_array section. */ 1753 case SHT_PREINIT_ARRAY: /* .preinit_array section. */ 1754 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 1755 1756 case SHT_DYNAMIC: /* Dynamic linking information. */ 1757 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) 1758 return false; 1759 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) 1760 { 1761 Elf_Internal_Shdr *dynsymhdr; 1762 1763 /* The shared libraries distributed with hpux11 have a bogus 1764 sh_link field for the ".dynamic" section. Find the 1765 string table for the ".dynsym" section instead. */ 1766 if (elf_dynsymtab (abfd) != 0) 1767 { 1768 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; 1769 hdr->sh_link = dynsymhdr->sh_link; 1770 } 1771 else 1772 { 1773 unsigned int i, num_sec; 1774 1775 num_sec = elf_numsections (abfd); 1776 for (i = 1; i < num_sec; i++) 1777 { 1778 dynsymhdr = elf_elfsections (abfd)[i]; 1779 if (dynsymhdr->sh_type == SHT_DYNSYM) 1780 { 1781 hdr->sh_link = dynsymhdr->sh_link; 1782 break; 1783 } 1784 } 1785 } 1786 } 1787 break; 1788 1789 case SHT_SYMTAB: /* A symbol table */ 1790 if (elf_onesymtab (abfd) == shindex) 1791 return true; 1792 1793 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym); 1794 BFD_ASSERT (elf_onesymtab (abfd) == 0); 1795 elf_onesymtab (abfd) = shindex; 1796 elf_tdata (abfd)->symtab_hdr = *hdr; 1797 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; 1798 abfd->flags |= HAS_SYMS; 1799 1800 /* Sometimes a shared object will map in the symbol table. If 1801 SHF_ALLOC is set, and this is a shared object, then we also 1802 treat this section as a BFD section. We can not base the 1803 decision purely on SHF_ALLOC, because that flag is sometimes 1804 set in a relocateable object file, which would confuse the 1805 linker. */ 1806 if ((hdr->sh_flags & SHF_ALLOC) != 0 1807 && (abfd->flags & DYNAMIC) != 0 1808 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) 1809 return false; 1810 1811 return true; 1812 1813 case SHT_DYNSYM: /* A dynamic symbol table */ 1814 if (elf_dynsymtab (abfd) == shindex) 1815 return true; 1816 1817 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym); 1818 BFD_ASSERT (elf_dynsymtab (abfd) == 0); 1819 elf_dynsymtab (abfd) = shindex; 1820 elf_tdata (abfd)->dynsymtab_hdr = *hdr; 1821 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; 1822 abfd->flags |= HAS_SYMS; 1823 1824 /* Besides being a symbol table, we also treat this as a regular 1825 section, so that objcopy can handle it. */ 1826 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 1827 1828 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */ 1829 if (elf_symtab_shndx (abfd) == shindex) 1830 return true; 1831 1832 /* Get the associated symbol table. */ 1833 if (! bfd_section_from_shdr (abfd, hdr->sh_link) 1834 || hdr->sh_link != elf_onesymtab (abfd)) 1835 return false; 1836 1837 elf_symtab_shndx (abfd) = shindex; 1838 elf_tdata (abfd)->symtab_shndx_hdr = *hdr; 1839 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; 1840 return true; 1841 1842 case SHT_STRTAB: /* A string table */ 1843 if (hdr->bfd_section != NULL) 1844 return true; 1845 if (ehdr->e_shstrndx == shindex) 1846 { 1847 elf_tdata (abfd)->shstrtab_hdr = *hdr; 1848 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; 1849 return true; 1850 } 1851 { 1852 unsigned int i, num_sec; 1853 1854 num_sec = elf_numsections (abfd); 1855 for (i = 1; i < num_sec; i++) 1856 { 1857 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 1858 if (hdr2->sh_link == shindex) 1859 { 1860 if (! bfd_section_from_shdr (abfd, i)) 1861 return false; 1862 if (elf_onesymtab (abfd) == i) 1863 { 1864 elf_tdata (abfd)->strtab_hdr = *hdr; 1865 elf_elfsections (abfd)[shindex] = 1866 &elf_tdata (abfd)->strtab_hdr; 1867 return true; 1868 } 1869 if (elf_dynsymtab (abfd) == i) 1870 { 1871 elf_tdata (abfd)->dynstrtab_hdr = *hdr; 1872 elf_elfsections (abfd)[shindex] = hdr = 1873 &elf_tdata (abfd)->dynstrtab_hdr; 1874 /* We also treat this as a regular section, so 1875 that objcopy can handle it. */ 1876 break; 1877 } 1878#if 0 /* Not handling other string tables specially right now. */ 1879 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */ 1880 /* We have a strtab for some random other section. */ 1881 newsect = (asection *) hdr2->bfd_section; 1882 if (!newsect) 1883 break; 1884 hdr->bfd_section = newsect; 1885 hdr2 = &elf_section_data (newsect)->str_hdr; 1886 *hdr2 = *hdr; 1887 elf_elfsections (abfd)[shindex] = hdr2; 1888#endif 1889 } 1890 } 1891 } 1892 1893 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 1894 1895 case SHT_REL: 1896 case SHT_RELA: 1897 /* *These* do a lot of work -- but build no sections! */ 1898 { 1899 asection *target_sect; 1900 Elf_Internal_Shdr *hdr2; 1901 unsigned int num_sec = elf_numsections (abfd); 1902 1903 /* Check for a bogus link to avoid crashing. */ 1904 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE) 1905 || hdr->sh_link >= num_sec) 1906 { 1907 ((*_bfd_error_handler) 1908 (_("%s: invalid link %lu for reloc section %s (index %u)"), 1909 bfd_archive_filename (abfd), hdr->sh_link, name, shindex)); 1910 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 1911 } 1912 1913 /* For some incomprehensible reason Oracle distributes 1914 libraries for Solaris in which some of the objects have 1915 bogus sh_link fields. It would be nice if we could just 1916 reject them, but, unfortunately, some people need to use 1917 them. We scan through the section headers; if we find only 1918 one suitable symbol table, we clobber the sh_link to point 1919 to it. I hope this doesn't break anything. */ 1920 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB 1921 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) 1922 { 1923 unsigned int scan; 1924 int found; 1925 1926 found = 0; 1927 for (scan = 1; scan < num_sec; scan++) 1928 { 1929 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB 1930 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) 1931 { 1932 if (found != 0) 1933 { 1934 found = 0; 1935 break; 1936 } 1937 found = scan; 1938 } 1939 } 1940 if (found != 0) 1941 hdr->sh_link = found; 1942 } 1943 1944 /* Get the symbol table. */ 1945 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB 1946 && ! bfd_section_from_shdr (abfd, hdr->sh_link)) 1947 return false; 1948 1949 /* If this reloc section does not use the main symbol table we 1950 don't treat it as a reloc section. BFD can't adequately 1951 represent such a section, so at least for now, we don't 1952 try. We just present it as a normal section. We also 1953 can't use it as a reloc section if it points to the null 1954 section. */ 1955 if (hdr->sh_link != elf_onesymtab (abfd) || hdr->sh_info == SHN_UNDEF) 1956 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 1957 1958 if (! bfd_section_from_shdr (abfd, hdr->sh_info)) 1959 return false; 1960 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); 1961 if (target_sect == NULL) 1962 return false; 1963 1964 if ((target_sect->flags & SEC_RELOC) == 0 1965 || target_sect->reloc_count == 0) 1966 hdr2 = &elf_section_data (target_sect)->rel_hdr; 1967 else 1968 { 1969 bfd_size_type amt; 1970 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL); 1971 amt = sizeof (*hdr2); 1972 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); 1973 elf_section_data (target_sect)->rel_hdr2 = hdr2; 1974 } 1975 *hdr2 = *hdr; 1976 elf_elfsections (abfd)[shindex] = hdr2; 1977 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); 1978 target_sect->flags |= SEC_RELOC; 1979 target_sect->relocation = NULL; 1980 target_sect->rel_filepos = hdr->sh_offset; 1981 /* In the section to which the relocations apply, mark whether 1982 its relocations are of the REL or RELA variety. */ 1983 if (hdr->sh_size != 0) 1984 elf_section_data (target_sect)->use_rela_p 1985 = (hdr->sh_type == SHT_RELA); 1986 abfd->flags |= HAS_RELOC; 1987 return true; 1988 } 1989 break; 1990 1991 case SHT_GNU_verdef: 1992 elf_dynverdef (abfd) = shindex; 1993 elf_tdata (abfd)->dynverdef_hdr = *hdr; 1994 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 1995 break; 1996 1997 case SHT_GNU_versym: 1998 elf_dynversym (abfd) = shindex; 1999 elf_tdata (abfd)->dynversym_hdr = *hdr; 2000 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 2001 break; 2002 2003 case SHT_GNU_verneed: 2004 elf_dynverref (abfd) = shindex; 2005 elf_tdata (abfd)->dynverref_hdr = *hdr; 2006 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 2007 break; 2008 2009 case SHT_SHLIB: 2010 return true; 2011 2012 case SHT_GROUP: 2013 /* We need a BFD section for objcopy and relocatable linking, 2014 and it's handy to have the signature available as the section 2015 name. */ 2016 name = group_signature (abfd, hdr); 2017 if (name == NULL) 2018 return false; 2019 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name)) 2020 return false; 2021 if (hdr->contents != NULL) 2022 { 2023 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; 2024 unsigned int n_elt = hdr->sh_size / 4; 2025 asection *s; 2026 2027 if (idx->flags & GRP_COMDAT) 2028 hdr->bfd_section->flags 2029 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 2030 2031 while (--n_elt != 0) 2032 if ((s = (++idx)->shdr->bfd_section) != NULL 2033 && elf_next_in_group (s) != NULL) 2034 { 2035 elf_next_in_group (hdr->bfd_section) = s; 2036 break; 2037 } 2038 } 2039 break; 2040 2041 default: 2042 /* Check for any processor-specific section types. */ 2043 { 2044 if (bed->elf_backend_section_from_shdr) 2045 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name); 2046 } 2047 break; 2048 } 2049 2050 return true; 2051} 2052 2053/* Return the section for the local symbol specified by ABFD, R_SYMNDX. 2054 Return SEC for sections that have no elf section, and NULL on error. */ 2055 2056asection * 2057bfd_section_from_r_symndx (abfd, cache, sec, r_symndx) 2058 bfd *abfd; 2059 struct sym_sec_cache *cache; 2060 asection *sec; 2061 unsigned long r_symndx; 2062{ 2063 Elf_Internal_Shdr *symtab_hdr; 2064 unsigned char esym[sizeof (Elf64_External_Sym)]; 2065 Elf_External_Sym_Shndx eshndx; 2066 Elf_Internal_Sym isym; 2067 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; 2068 2069 if (cache->abfd == abfd && cache->indx[ent] == r_symndx) 2070 return cache->sec[ent]; 2071 2072 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2073 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, 2074 &isym, esym, &eshndx) == NULL) 2075 return NULL; 2076 2077 if (cache->abfd != abfd) 2078 { 2079 memset (cache->indx, -1, sizeof (cache->indx)); 2080 cache->abfd = abfd; 2081 } 2082 cache->indx[ent] = r_symndx; 2083 cache->sec[ent] = sec; 2084 if (isym.st_shndx < SHN_LORESERVE || isym.st_shndx > SHN_HIRESERVE) 2085 { 2086 asection *s; 2087 s = bfd_section_from_elf_index (abfd, isym.st_shndx); 2088 if (s != NULL) 2089 cache->sec[ent] = s; 2090 } 2091 return cache->sec[ent]; 2092} 2093 2094/* Given an ELF section number, retrieve the corresponding BFD 2095 section. */ 2096 2097asection * 2098bfd_section_from_elf_index (abfd, index) 2099 bfd *abfd; 2100 unsigned int index; 2101{ 2102 if (index >= elf_numsections (abfd)) 2103 return NULL; 2104 return elf_elfsections (abfd)[index]->bfd_section; 2105} 2106 2107boolean 2108_bfd_elf_new_section_hook (abfd, sec) 2109 bfd *abfd; 2110 asection *sec; 2111{ 2112 struct bfd_elf_section_data *sdata; 2113 bfd_size_type amt = sizeof (*sdata); 2114 2115 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, amt); 2116 if (!sdata) 2117 return false; 2118 sec->used_by_bfd = (PTR) sdata; 2119 2120 /* Indicate whether or not this section should use RELA relocations. */ 2121 sdata->use_rela_p 2122 = get_elf_backend_data (abfd)->default_use_rela_p; 2123 2124 return true; 2125} 2126 2127/* Create a new bfd section from an ELF program header. 2128 2129 Since program segments have no names, we generate a synthetic name 2130 of the form segment<NUM>, where NUM is generally the index in the 2131 program header table. For segments that are split (see below) we 2132 generate the names segment<NUM>a and segment<NUM>b. 2133 2134 Note that some program segments may have a file size that is different than 2135 (less than) the memory size. All this means is that at execution the 2136 system must allocate the amount of memory specified by the memory size, 2137 but only initialize it with the first "file size" bytes read from the 2138 file. This would occur for example, with program segments consisting 2139 of combined data+bss. 2140 2141 To handle the above situation, this routine generates TWO bfd sections 2142 for the single program segment. The first has the length specified by 2143 the file size of the segment, and the second has the length specified 2144 by the difference between the two sizes. In effect, the segment is split 2145 into it's initialized and uninitialized parts. 2146 2147 */ 2148 2149boolean 2150_bfd_elf_make_section_from_phdr (abfd, hdr, index, typename) 2151 bfd *abfd; 2152 Elf_Internal_Phdr *hdr; 2153 int index; 2154 const char *typename; 2155{ 2156 asection *newsect; 2157 char *name; 2158 char namebuf[64]; 2159 size_t len; 2160 int split; 2161 2162 split = ((hdr->p_memsz > 0) 2163 && (hdr->p_filesz > 0) 2164 && (hdr->p_memsz > hdr->p_filesz)); 2165 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : ""); 2166 len = strlen (namebuf) + 1; 2167 name = bfd_alloc (abfd, (bfd_size_type) len); 2168 if (!name) 2169 return false; 2170 memcpy (name, namebuf, len); 2171 newsect = bfd_make_section (abfd, name); 2172 if (newsect == NULL) 2173 return false; 2174 newsect->vma = hdr->p_vaddr; 2175 newsect->lma = hdr->p_paddr; 2176 newsect->_raw_size = hdr->p_filesz; 2177 newsect->filepos = hdr->p_offset; 2178 newsect->flags |= SEC_HAS_CONTENTS; 2179 if (hdr->p_type == PT_LOAD) 2180 { 2181 newsect->flags |= SEC_ALLOC; 2182 newsect->flags |= SEC_LOAD; 2183 if (hdr->p_flags & PF_X) 2184 { 2185 /* FIXME: all we known is that it has execute PERMISSION, 2186 may be data. */ 2187 newsect->flags |= SEC_CODE; 2188 } 2189 } 2190 if (!(hdr->p_flags & PF_W)) 2191 { 2192 newsect->flags |= SEC_READONLY; 2193 } 2194 2195 if (split) 2196 { 2197 sprintf (namebuf, "%s%db", typename, index); 2198 len = strlen (namebuf) + 1; 2199 name = bfd_alloc (abfd, (bfd_size_type) len); 2200 if (!name) 2201 return false; 2202 memcpy (name, namebuf, len); 2203 newsect = bfd_make_section (abfd, name); 2204 if (newsect == NULL) 2205 return false; 2206 newsect->vma = hdr->p_vaddr + hdr->p_filesz; 2207 newsect->lma = hdr->p_paddr + hdr->p_filesz; 2208 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz; 2209 if (hdr->p_type == PT_LOAD) 2210 { 2211 newsect->flags |= SEC_ALLOC; 2212 if (hdr->p_flags & PF_X) 2213 newsect->flags |= SEC_CODE; 2214 } 2215 if (!(hdr->p_flags & PF_W)) 2216 newsect->flags |= SEC_READONLY; 2217 } 2218 2219 return true; 2220} 2221 2222boolean 2223bfd_section_from_phdr (abfd, hdr, index) 2224 bfd *abfd; 2225 Elf_Internal_Phdr *hdr; 2226 int index; 2227{ 2228 struct elf_backend_data *bed; 2229 2230 switch (hdr->p_type) 2231 { 2232 case PT_NULL: 2233 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null"); 2234 2235 case PT_LOAD: 2236 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load"); 2237 2238 case PT_DYNAMIC: 2239 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic"); 2240 2241 case PT_INTERP: 2242 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp"); 2243 2244 case PT_NOTE: 2245 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note")) 2246 return false; 2247 if (! elfcore_read_notes (abfd, (file_ptr) hdr->p_offset, hdr->p_filesz)) 2248 return false; 2249 return true; 2250 2251 case PT_SHLIB: 2252 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib"); 2253 2254 case PT_PHDR: 2255 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr"); 2256 2257 default: 2258 /* Check for any processor-specific program segment types. 2259 If no handler for them, default to making "segment" sections. */ 2260 bed = get_elf_backend_data (abfd); 2261 if (bed->elf_backend_section_from_phdr) 2262 return (*bed->elf_backend_section_from_phdr) (abfd, hdr, index); 2263 else 2264 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "segment"); 2265 } 2266} 2267 2268/* Initialize REL_HDR, the section-header for new section, containing 2269 relocations against ASECT. If USE_RELA_P is true, we use RELA 2270 relocations; otherwise, we use REL relocations. */ 2271 2272boolean 2273_bfd_elf_init_reloc_shdr (abfd, rel_hdr, asect, use_rela_p) 2274 bfd *abfd; 2275 Elf_Internal_Shdr *rel_hdr; 2276 asection *asect; 2277 boolean use_rela_p; 2278{ 2279 char *name; 2280 struct elf_backend_data *bed = get_elf_backend_data (abfd); 2281 bfd_size_type amt = sizeof ".rela" + strlen (asect->name); 2282 2283 name = bfd_alloc (abfd, amt); 2284 if (name == NULL) 2285 return false; 2286 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); 2287 rel_hdr->sh_name = 2288 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, 2289 false); 2290 if (rel_hdr->sh_name == (unsigned int) -1) 2291 return false; 2292 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; 2293 rel_hdr->sh_entsize = (use_rela_p 2294 ? bed->s->sizeof_rela 2295 : bed->s->sizeof_rel); 2296 rel_hdr->sh_addralign = bed->s->file_align; 2297 rel_hdr->sh_flags = 0; 2298 rel_hdr->sh_addr = 0; 2299 rel_hdr->sh_size = 0; 2300 rel_hdr->sh_offset = 0; 2301 2302 return true; 2303} 2304 2305/* Set up an ELF internal section header for a section. */ 2306 2307static void 2308elf_fake_sections (abfd, asect, failedptrarg) 2309 bfd *abfd; 2310 asection *asect; 2311 PTR failedptrarg; 2312{ 2313 struct elf_backend_data *bed = get_elf_backend_data (abfd); 2314 boolean *failedptr = (boolean *) failedptrarg; 2315 Elf_Internal_Shdr *this_hdr; 2316 2317 if (*failedptr) 2318 { 2319 /* We already failed; just get out of the bfd_map_over_sections 2320 loop. */ 2321 return; 2322 } 2323 2324 this_hdr = &elf_section_data (asect)->this_hdr; 2325 2326 this_hdr->sh_name = (unsigned long) _bfd_elf_strtab_add (elf_shstrtab (abfd), 2327 asect->name, false); 2328 if (this_hdr->sh_name == (unsigned long) -1) 2329 { 2330 *failedptr = true; 2331 return; 2332 } 2333 2334 this_hdr->sh_flags = 0; 2335 2336 if ((asect->flags & SEC_ALLOC) != 0 2337 || asect->user_set_vma) 2338 this_hdr->sh_addr = asect->vma; 2339 else 2340 this_hdr->sh_addr = 0; 2341 2342 this_hdr->sh_offset = 0; 2343 this_hdr->sh_size = asect->_raw_size; 2344 this_hdr->sh_link = 0; 2345 this_hdr->sh_addralign = 1 << asect->alignment_power; 2346 /* The sh_entsize and sh_info fields may have been set already by 2347 copy_private_section_data. */ 2348 2349 this_hdr->bfd_section = asect; 2350 this_hdr->contents = NULL; 2351 2352 /* FIXME: This should not be based on section names. */ 2353 if (strcmp (asect->name, ".dynstr") == 0) 2354 this_hdr->sh_type = SHT_STRTAB; 2355 else if (strcmp (asect->name, ".hash") == 0) 2356 { 2357 this_hdr->sh_type = SHT_HASH; 2358 this_hdr->sh_entsize = bed->s->sizeof_hash_entry; 2359 } 2360 else if (strcmp (asect->name, ".dynsym") == 0) 2361 { 2362 this_hdr->sh_type = SHT_DYNSYM; 2363 this_hdr->sh_entsize = bed->s->sizeof_sym; 2364 } 2365 else if (strcmp (asect->name, ".dynamic") == 0) 2366 { 2367 this_hdr->sh_type = SHT_DYNAMIC; 2368 this_hdr->sh_entsize = bed->s->sizeof_dyn; 2369 } 2370 else if (strncmp (asect->name, ".rela", 5) == 0 2371 && get_elf_backend_data (abfd)->may_use_rela_p) 2372 { 2373 this_hdr->sh_type = SHT_RELA; 2374 this_hdr->sh_entsize = bed->s->sizeof_rela; 2375 } 2376 else if (strncmp (asect->name, ".rel", 4) == 0 2377 && get_elf_backend_data (abfd)->may_use_rel_p) 2378 { 2379 this_hdr->sh_type = SHT_REL; 2380 this_hdr->sh_entsize = bed->s->sizeof_rel; 2381 } 2382 else if (strcmp (asect->name, ".init_array") == 0) 2383 this_hdr->sh_type = SHT_INIT_ARRAY; 2384 else if (strcmp (asect->name, ".fini_array") == 0) 2385 this_hdr->sh_type = SHT_FINI_ARRAY; 2386 else if (strcmp (asect->name, ".preinit_array") == 0) 2387 this_hdr->sh_type = SHT_PREINIT_ARRAY; 2388 else if (strncmp (asect->name, ".note", 5) == 0) 2389 this_hdr->sh_type = SHT_NOTE; 2390 else if (strncmp (asect->name, ".stab", 5) == 0 2391 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0) 2392 this_hdr->sh_type = SHT_STRTAB; 2393 else if (strcmp (asect->name, ".gnu.version") == 0) 2394 { 2395 this_hdr->sh_type = SHT_GNU_versym; 2396 this_hdr->sh_entsize = sizeof (Elf_External_Versym); 2397 } 2398 else if (strcmp (asect->name, ".gnu.version_d") == 0) 2399 { 2400 this_hdr->sh_type = SHT_GNU_verdef; 2401 this_hdr->sh_entsize = 0; 2402 /* objcopy or strip will copy over sh_info, but may not set 2403 cverdefs. The linker will set cverdefs, but sh_info will be 2404 zero. */ 2405 if (this_hdr->sh_info == 0) 2406 this_hdr->sh_info = elf_tdata (abfd)->cverdefs; 2407 else 2408 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 2409 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); 2410 } 2411 else if (strcmp (asect->name, ".gnu.version_r") == 0) 2412 { 2413 this_hdr->sh_type = SHT_GNU_verneed; 2414 this_hdr->sh_entsize = 0; 2415 /* objcopy or strip will copy over sh_info, but may not set 2416 cverrefs. The linker will set cverrefs, but sh_info will be 2417 zero. */ 2418 if (this_hdr->sh_info == 0) 2419 this_hdr->sh_info = elf_tdata (abfd)->cverrefs; 2420 else 2421 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 2422 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); 2423 } 2424 else if ((asect->flags & SEC_GROUP) != 0) 2425 { 2426 this_hdr->sh_type = SHT_GROUP; 2427 this_hdr->sh_entsize = 4; 2428 } 2429 else if ((asect->flags & SEC_ALLOC) != 0 2430 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) 2431 || (asect->flags & SEC_NEVER_LOAD) != 0)) 2432 this_hdr->sh_type = SHT_NOBITS; 2433 else 2434 this_hdr->sh_type = SHT_PROGBITS; 2435 2436 if ((asect->flags & SEC_ALLOC) != 0) 2437 this_hdr->sh_flags |= SHF_ALLOC; 2438 if ((asect->flags & SEC_READONLY) == 0) 2439 this_hdr->sh_flags |= SHF_WRITE; 2440 if ((asect->flags & SEC_CODE) != 0) 2441 this_hdr->sh_flags |= SHF_EXECINSTR; 2442 if ((asect->flags & SEC_MERGE) != 0) 2443 { 2444 this_hdr->sh_flags |= SHF_MERGE; 2445 this_hdr->sh_entsize = asect->entsize; 2446 if ((asect->flags & SEC_STRINGS) != 0) 2447 this_hdr->sh_flags |= SHF_STRINGS; 2448 } 2449 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) 2450 this_hdr->sh_flags |= SHF_GROUP; 2451 if ((asect->flags & SEC_THREAD_LOCAL) != 0) 2452 { 2453 this_hdr->sh_flags |= SHF_TLS; 2454 if (asect->_raw_size == 0 && (asect->flags & SEC_HAS_CONTENTS) == 0) 2455 { 2456 struct bfd_link_order *o; 2457 2458 this_hdr->sh_size = 0; 2459 for (o = asect->link_order_head; o != NULL; o = o->next) 2460 if (this_hdr->sh_size < o->offset + o->size) 2461 this_hdr->sh_size = o->offset + o->size; 2462 if (this_hdr->sh_size) 2463 this_hdr->sh_type = SHT_NOBITS; 2464 } 2465 } 2466 2467 /* Check for processor-specific section types. */ 2468 if (bed->elf_backend_fake_sections 2469 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) 2470 *failedptr = true; 2471 2472 /* If the section has relocs, set up a section header for the 2473 SHT_REL[A] section. If two relocation sections are required for 2474 this section, it is up to the processor-specific back-end to 2475 create the other. */ 2476 if ((asect->flags & SEC_RELOC) != 0 2477 && !_bfd_elf_init_reloc_shdr (abfd, 2478 &elf_section_data (asect)->rel_hdr, 2479 asect, 2480 elf_section_data (asect)->use_rela_p)) 2481 *failedptr = true; 2482} 2483 2484/* Fill in the contents of a SHT_GROUP section. */ 2485 2486void 2487bfd_elf_set_group_contents (abfd, sec, failedptrarg) 2488 bfd *abfd; 2489 asection *sec; 2490 PTR failedptrarg; 2491{ 2492 boolean *failedptr = (boolean *) failedptrarg; 2493 unsigned long symindx; 2494 asection *elt, *first; 2495 unsigned char *loc; 2496 struct bfd_link_order *l; 2497 boolean gas; 2498 2499 if (elf_section_data (sec)->this_hdr.sh_type != SHT_GROUP 2500 || *failedptr) 2501 return; 2502 2503 symindx = 0; 2504 if (elf_group_id (sec) != NULL) 2505 symindx = elf_group_id (sec)->udata.i; 2506 2507 if (symindx == 0) 2508 { 2509 /* If called from the assembler, swap_out_syms will have set up 2510 elf_section_syms; If called for "ld -r", use target_index. */ 2511 if (elf_section_syms (abfd) != NULL) 2512 symindx = elf_section_syms (abfd)[sec->index]->udata.i; 2513 else 2514 symindx = sec->target_index; 2515 } 2516 elf_section_data (sec)->this_hdr.sh_info = symindx; 2517 2518 /* The contents won't be allocated for "ld -r" or objcopy. */ 2519 gas = true; 2520 if (sec->contents == NULL) 2521 { 2522 gas = false; 2523 sec->contents = bfd_alloc (abfd, sec->_raw_size); 2524 2525 /* Arrange for the section to be written out. */ 2526 elf_section_data (sec)->this_hdr.contents = sec->contents; 2527 if (sec->contents == NULL) 2528 { 2529 *failedptr = true; 2530 return; 2531 } 2532 } 2533 2534 loc = sec->contents + sec->_raw_size; 2535 2536 /* Get the pointer to the first section in the group that gas 2537 squirreled away here. objcopy arranges for this to be set to the 2538 start of the input section group. */ 2539 first = elt = elf_next_in_group (sec); 2540 2541 /* First element is a flag word. Rest of section is elf section 2542 indices for all the sections of the group. Write them backwards 2543 just to keep the group in the same order as given in .section 2544 directives, not that it matters. */ 2545 while (elt != NULL) 2546 { 2547 asection *s; 2548 unsigned int idx; 2549 2550 loc -= 4; 2551 s = elt; 2552 if (!gas) 2553 s = s->output_section; 2554 idx = 0; 2555 if (s != NULL) 2556 idx = elf_section_data (s)->this_idx; 2557 H_PUT_32 (abfd, idx, loc); 2558 elt = elf_next_in_group (elt); 2559 if (elt == first) 2560 break; 2561 } 2562 2563 /* If this is a relocatable link, then the above did nothing because 2564 SEC is the output section. Look through the input sections 2565 instead. */ 2566 for (l = sec->link_order_head; l != NULL; l = l->next) 2567 if (l->type == bfd_indirect_link_order 2568 && (elt = elf_next_in_group (l->u.indirect.section)) != NULL) 2569 do 2570 { 2571 loc -= 4; 2572 H_PUT_32 (abfd, 2573 elf_section_data (elt->output_section)->this_idx, loc); 2574 elt = elf_next_in_group (elt); 2575 /* During a relocatable link, the lists are circular. */ 2576 } 2577 while (elt != elf_next_in_group (l->u.indirect.section)); 2578 2579 /* With ld -r, merging SHT_GROUP sections results in wasted space 2580 due to allowing for the flag word on each input. We may well 2581 duplicate entries too. */ 2582 while ((loc -= 4) > sec->contents) 2583 H_PUT_32 (abfd, 0, loc); 2584 2585 if (loc != sec->contents) 2586 abort (); 2587 2588 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); 2589} 2590 2591/* Assign all ELF section numbers. The dummy first section is handled here 2592 too. The link/info pointers for the standard section types are filled 2593 in here too, while we're at it. */ 2594 2595static boolean 2596assign_section_numbers (abfd) 2597 bfd *abfd; 2598{ 2599 struct elf_obj_tdata *t = elf_tdata (abfd); 2600 asection *sec; 2601 unsigned int section_number, secn; 2602 Elf_Internal_Shdr **i_shdrp; 2603 bfd_size_type amt; 2604 2605 section_number = 1; 2606 2607 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); 2608 2609 for (sec = abfd->sections; sec; sec = sec->next) 2610 { 2611 struct bfd_elf_section_data *d = elf_section_data (sec); 2612 2613 if (section_number == SHN_LORESERVE) 2614 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 2615 d->this_idx = section_number++; 2616 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); 2617 if ((sec->flags & SEC_RELOC) == 0) 2618 d->rel_idx = 0; 2619 else 2620 { 2621 if (section_number == SHN_LORESERVE) 2622 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 2623 d->rel_idx = section_number++; 2624 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name); 2625 } 2626 2627 if (d->rel_hdr2) 2628 { 2629 if (section_number == SHN_LORESERVE) 2630 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 2631 d->rel_idx2 = section_number++; 2632 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name); 2633 } 2634 else 2635 d->rel_idx2 = 0; 2636 } 2637 2638 if (section_number == SHN_LORESERVE) 2639 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 2640 t->shstrtab_section = section_number++; 2641 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); 2642 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; 2643 2644 if (bfd_get_symcount (abfd) > 0) 2645 { 2646 if (section_number == SHN_LORESERVE) 2647 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 2648 t->symtab_section = section_number++; 2649 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); 2650 if (section_number > SHN_LORESERVE - 2) 2651 { 2652 if (section_number == SHN_LORESERVE) 2653 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 2654 t->symtab_shndx_section = section_number++; 2655 t->symtab_shndx_hdr.sh_name 2656 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 2657 ".symtab_shndx", false); 2658 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1) 2659 return false; 2660 } 2661 if (section_number == SHN_LORESERVE) 2662 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 2663 t->strtab_section = section_number++; 2664 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); 2665 } 2666 2667 _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); 2668 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); 2669 2670 elf_numsections (abfd) = section_number; 2671 elf_elfheader (abfd)->e_shnum = section_number; 2672 if (section_number > SHN_LORESERVE) 2673 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE; 2674 2675 /* Set up the list of section header pointers, in agreement with the 2676 indices. */ 2677 amt = section_number * sizeof (Elf_Internal_Shdr *); 2678 i_shdrp = (Elf_Internal_Shdr **) bfd_alloc (abfd, amt); 2679 if (i_shdrp == NULL) 2680 return false; 2681 2682 amt = sizeof (Elf_Internal_Shdr); 2683 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_alloc (abfd, amt); 2684 if (i_shdrp[0] == NULL) 2685 { 2686 bfd_release (abfd, i_shdrp); 2687 return false; 2688 } 2689 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr)); 2690 2691 elf_elfsections (abfd) = i_shdrp; 2692 2693 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; 2694 if (bfd_get_symcount (abfd) > 0) 2695 { 2696 i_shdrp[t->symtab_section] = &t->symtab_hdr; 2697 if (elf_numsections (abfd) > SHN_LORESERVE) 2698 { 2699 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr; 2700 t->symtab_shndx_hdr.sh_link = t->symtab_section; 2701 } 2702 i_shdrp[t->strtab_section] = &t->strtab_hdr; 2703 t->symtab_hdr.sh_link = t->strtab_section; 2704 } 2705 for (sec = abfd->sections; sec; sec = sec->next) 2706 { 2707 struct bfd_elf_section_data *d = elf_section_data (sec); 2708 asection *s; 2709 const char *name; 2710 2711 i_shdrp[d->this_idx] = &d->this_hdr; 2712 if (d->rel_idx != 0) 2713 i_shdrp[d->rel_idx] = &d->rel_hdr; 2714 if (d->rel_idx2 != 0) 2715 i_shdrp[d->rel_idx2] = d->rel_hdr2; 2716 2717 /* Fill in the sh_link and sh_info fields while we're at it. */ 2718 2719 /* sh_link of a reloc section is the section index of the symbol 2720 table. sh_info is the section index of the section to which 2721 the relocation entries apply. */ 2722 if (d->rel_idx != 0) 2723 { 2724 d->rel_hdr.sh_link = t->symtab_section; 2725 d->rel_hdr.sh_info = d->this_idx; 2726 } 2727 if (d->rel_idx2 != 0) 2728 { 2729 d->rel_hdr2->sh_link = t->symtab_section; 2730 d->rel_hdr2->sh_info = d->this_idx; 2731 } 2732 2733 switch (d->this_hdr.sh_type) 2734 { 2735 case SHT_REL: 2736 case SHT_RELA: 2737 /* A reloc section which we are treating as a normal BFD 2738 section. sh_link is the section index of the symbol 2739 table. sh_info is the section index of the section to 2740 which the relocation entries apply. We assume that an 2741 allocated reloc section uses the dynamic symbol table. 2742 FIXME: How can we be sure? */ 2743 s = bfd_get_section_by_name (abfd, ".dynsym"); 2744 if (s != NULL) 2745 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 2746 2747 /* We look up the section the relocs apply to by name. */ 2748 name = sec->name; 2749 if (d->this_hdr.sh_type == SHT_REL) 2750 name += 4; 2751 else 2752 name += 5; 2753 s = bfd_get_section_by_name (abfd, name); 2754 if (s != NULL) 2755 d->this_hdr.sh_info = elf_section_data (s)->this_idx; 2756 break; 2757 2758 case SHT_STRTAB: 2759 /* We assume that a section named .stab*str is a stabs 2760 string section. We look for a section with the same name 2761 but without the trailing ``str'', and set its sh_link 2762 field to point to this section. */ 2763 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0 2764 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) 2765 { 2766 size_t len; 2767 char *alc; 2768 2769 len = strlen (sec->name); 2770 alc = (char *) bfd_malloc ((bfd_size_type) (len - 2)); 2771 if (alc == NULL) 2772 return false; 2773 memcpy (alc, sec->name, len - 3); 2774 alc[len - 3] = '\0'; 2775 s = bfd_get_section_by_name (abfd, alc); 2776 free (alc); 2777 if (s != NULL) 2778 { 2779 elf_section_data (s)->this_hdr.sh_link = d->this_idx; 2780 2781 /* This is a .stab section. */ 2782 if (elf_section_data (s)->this_hdr.sh_entsize == 0) 2783 elf_section_data (s)->this_hdr.sh_entsize 2784 = 4 + 2 * bfd_get_arch_size (abfd) / 8; 2785 } 2786 } 2787 break; 2788 2789 case SHT_DYNAMIC: 2790 case SHT_DYNSYM: 2791 case SHT_GNU_verneed: 2792 case SHT_GNU_verdef: 2793 /* sh_link is the section header index of the string table 2794 used for the dynamic entries, or the symbol table, or the 2795 version strings. */ 2796 s = bfd_get_section_by_name (abfd, ".dynstr"); 2797 if (s != NULL) 2798 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 2799 break; 2800 2801 case SHT_HASH: 2802 case SHT_GNU_versym: 2803 /* sh_link is the section header index of the symbol table 2804 this hash table or version table is for. */ 2805 s = bfd_get_section_by_name (abfd, ".dynsym"); 2806 if (s != NULL) 2807 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 2808 break; 2809 2810 case SHT_GROUP: 2811 d->this_hdr.sh_link = t->symtab_section; 2812 } 2813 } 2814 2815 for (secn = 1; secn < section_number; ++secn) 2816 if (i_shdrp[secn] == NULL) 2817 i_shdrp[secn] = i_shdrp[0]; 2818 else 2819 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), 2820 i_shdrp[secn]->sh_name); 2821 return true; 2822} 2823 2824/* Map symbol from it's internal number to the external number, moving 2825 all local symbols to be at the head of the list. */ 2826 2827static INLINE int 2828sym_is_global (abfd, sym) 2829 bfd *abfd; 2830 asymbol *sym; 2831{ 2832 /* If the backend has a special mapping, use it. */ 2833 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global) 2834 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global) 2835 (abfd, sym)); 2836 2837 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0 2838 || bfd_is_und_section (bfd_get_section (sym)) 2839 || bfd_is_com_section (bfd_get_section (sym))); 2840} 2841 2842static boolean 2843elf_map_symbols (abfd) 2844 bfd *abfd; 2845{ 2846 unsigned int symcount = bfd_get_symcount (abfd); 2847 asymbol **syms = bfd_get_outsymbols (abfd); 2848 asymbol **sect_syms; 2849 unsigned int num_locals = 0; 2850 unsigned int num_globals = 0; 2851 unsigned int num_locals2 = 0; 2852 unsigned int num_globals2 = 0; 2853 int max_index = 0; 2854 unsigned int idx; 2855 asection *asect; 2856 asymbol **new_syms; 2857 bfd_size_type amt; 2858 2859#ifdef DEBUG 2860 fprintf (stderr, "elf_map_symbols\n"); 2861 fflush (stderr); 2862#endif 2863 2864 for (asect = abfd->sections; asect; asect = asect->next) 2865 { 2866 if (max_index < asect->index) 2867 max_index = asect->index; 2868 } 2869 2870 max_index++; 2871 amt = max_index * sizeof (asymbol *); 2872 sect_syms = (asymbol **) bfd_zalloc (abfd, amt); 2873 if (sect_syms == NULL) 2874 return false; 2875 elf_section_syms (abfd) = sect_syms; 2876 elf_num_section_syms (abfd) = max_index; 2877 2878 /* Init sect_syms entries for any section symbols we have already 2879 decided to output. */ 2880 for (idx = 0; idx < symcount; idx++) 2881 { 2882 asymbol *sym = syms[idx]; 2883 2884 if ((sym->flags & BSF_SECTION_SYM) != 0 2885 && sym->value == 0) 2886 { 2887 asection *sec; 2888 2889 sec = sym->section; 2890 2891 if (sec->owner != NULL) 2892 { 2893 if (sec->owner != abfd) 2894 { 2895 if (sec->output_offset != 0) 2896 continue; 2897 2898 sec = sec->output_section; 2899 2900 /* Empty sections in the input files may have had a 2901 section symbol created for them. (See the comment 2902 near the end of _bfd_generic_link_output_symbols in 2903 linker.c). If the linker script discards such 2904 sections then we will reach this point. Since we know 2905 that we cannot avoid this case, we detect it and skip 2906 the abort and the assignment to the sect_syms array. 2907 To reproduce this particular case try running the 2908 linker testsuite test ld-scripts/weak.exp for an ELF 2909 port that uses the generic linker. */ 2910 if (sec->owner == NULL) 2911 continue; 2912 2913 BFD_ASSERT (sec->owner == abfd); 2914 } 2915 sect_syms[sec->index] = syms[idx]; 2916 } 2917 } 2918 } 2919 2920 /* Classify all of the symbols. */ 2921 for (idx = 0; idx < symcount; idx++) 2922 { 2923 if (!sym_is_global (abfd, syms[idx])) 2924 num_locals++; 2925 else 2926 num_globals++; 2927 } 2928 2929 /* We will be adding a section symbol for each BFD section. Most normal 2930 sections will already have a section symbol in outsymbols, but 2931 eg. SHT_GROUP sections will not, and we need the section symbol mapped 2932 at least in that case. */ 2933 for (asect = abfd->sections; asect; asect = asect->next) 2934 { 2935 if (sect_syms[asect->index] == NULL) 2936 { 2937 if (!sym_is_global (abfd, asect->symbol)) 2938 num_locals++; 2939 else 2940 num_globals++; 2941 } 2942 } 2943 2944 /* Now sort the symbols so the local symbols are first. */ 2945 amt = (num_locals + num_globals) * sizeof (asymbol *); 2946 new_syms = (asymbol **) bfd_alloc (abfd, amt); 2947 2948 if (new_syms == NULL) 2949 return false; 2950 2951 for (idx = 0; idx < symcount; idx++) 2952 { 2953 asymbol *sym = syms[idx]; 2954 unsigned int i; 2955 2956 if (!sym_is_global (abfd, sym)) 2957 i = num_locals2++; 2958 else 2959 i = num_locals + num_globals2++; 2960 new_syms[i] = sym; 2961 sym->udata.i = i + 1; 2962 } 2963 for (asect = abfd->sections; asect; asect = asect->next) 2964 { 2965 if (sect_syms[asect->index] == NULL) 2966 { 2967 asymbol *sym = asect->symbol; 2968 unsigned int i; 2969 2970 sect_syms[asect->index] = sym; 2971 if (!sym_is_global (abfd, sym)) 2972 i = num_locals2++; 2973 else 2974 i = num_locals + num_globals2++; 2975 new_syms[i] = sym; 2976 sym->udata.i = i + 1; 2977 } 2978 } 2979 2980 bfd_set_symtab (abfd, new_syms, num_locals + num_globals); 2981 2982 elf_num_locals (abfd) = num_locals; 2983 elf_num_globals (abfd) = num_globals; 2984 return true; 2985} 2986 2987/* Align to the maximum file alignment that could be required for any 2988 ELF data structure. */ 2989 2990static INLINE file_ptr align_file_position PARAMS ((file_ptr, int)); 2991static INLINE file_ptr 2992align_file_position (off, align) 2993 file_ptr off; 2994 int align; 2995{ 2996 return (off + align - 1) & ~(align - 1); 2997} 2998 2999/* Assign a file position to a section, optionally aligning to the 3000 required section alignment. */ 3001 3002INLINE file_ptr 3003_bfd_elf_assign_file_position_for_section (i_shdrp, offset, align) 3004 Elf_Internal_Shdr *i_shdrp; 3005 file_ptr offset; 3006 boolean align; 3007{ 3008 if (align) 3009 { 3010 unsigned int al; 3011 3012 al = i_shdrp->sh_addralign; 3013 if (al > 1) 3014 offset = BFD_ALIGN (offset, al); 3015 } 3016 i_shdrp->sh_offset = offset; 3017 if (i_shdrp->bfd_section != NULL) 3018 i_shdrp->bfd_section->filepos = offset; 3019 if (i_shdrp->sh_type != SHT_NOBITS) 3020 offset += i_shdrp->sh_size; 3021 return offset; 3022} 3023 3024/* Compute the file positions we are going to put the sections at, and 3025 otherwise prepare to begin writing out the ELF file. If LINK_INFO 3026 is not NULL, this is being called by the ELF backend linker. */ 3027 3028boolean 3029_bfd_elf_compute_section_file_positions (abfd, link_info) 3030 bfd *abfd; 3031 struct bfd_link_info *link_info; 3032{ 3033 struct elf_backend_data *bed = get_elf_backend_data (abfd); 3034 boolean failed; 3035 struct bfd_strtab_hash *strtab; 3036 Elf_Internal_Shdr *shstrtab_hdr; 3037 3038 if (abfd->output_has_begun) 3039 return true; 3040 3041 /* Do any elf backend specific processing first. */ 3042 if (bed->elf_backend_begin_write_processing) 3043 (*bed->elf_backend_begin_write_processing) (abfd, link_info); 3044 3045 if (! prep_headers (abfd)) 3046 return false; 3047 3048 /* Post process the headers if necessary. */ 3049 if (bed->elf_backend_post_process_headers) 3050 (*bed->elf_backend_post_process_headers) (abfd, link_info); 3051 3052 failed = false; 3053 bfd_map_over_sections (abfd, elf_fake_sections, &failed); 3054 if (failed) 3055 return false; 3056 3057 if (!assign_section_numbers (abfd)) 3058 return false; 3059 3060 /* The backend linker builds symbol table information itself. */ 3061 if (link_info == NULL && bfd_get_symcount (abfd) > 0) 3062 { 3063 /* Non-zero if doing a relocatable link. */ 3064 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); 3065 3066 if (! swap_out_syms (abfd, &strtab, relocatable_p)) 3067 return false; 3068 } 3069 3070 if (link_info == NULL) 3071 { 3072 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); 3073 if (failed) 3074 return false; 3075 } 3076 3077 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; 3078 /* sh_name was set in prep_headers. */ 3079 shstrtab_hdr->sh_type = SHT_STRTAB; 3080 shstrtab_hdr->sh_flags = 0; 3081 shstrtab_hdr->sh_addr = 0; 3082 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); 3083 shstrtab_hdr->sh_entsize = 0; 3084 shstrtab_hdr->sh_link = 0; 3085 shstrtab_hdr->sh_info = 0; 3086 /* sh_offset is set in assign_file_positions_except_relocs. */ 3087 shstrtab_hdr->sh_addralign = 1; 3088 3089 if (!assign_file_positions_except_relocs (abfd)) 3090 return false; 3091 3092 if (link_info == NULL && bfd_get_symcount (abfd) > 0) 3093 { 3094 file_ptr off; 3095 Elf_Internal_Shdr *hdr; 3096 3097 off = elf_tdata (abfd)->next_file_pos; 3098 3099 hdr = &elf_tdata (abfd)->symtab_hdr; 3100 off = _bfd_elf_assign_file_position_for_section (hdr, off, true); 3101 3102 hdr = &elf_tdata (abfd)->symtab_shndx_hdr; 3103 if (hdr->sh_size != 0) 3104 off = _bfd_elf_assign_file_position_for_section (hdr, off, true); 3105 3106 hdr = &elf_tdata (abfd)->strtab_hdr; 3107 off = _bfd_elf_assign_file_position_for_section (hdr, off, true); 3108 3109 elf_tdata (abfd)->next_file_pos = off; 3110 3111 /* Now that we know where the .strtab section goes, write it 3112 out. */ 3113 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 3114 || ! _bfd_stringtab_emit (abfd, strtab)) 3115 return false; 3116 _bfd_stringtab_free (strtab); 3117 } 3118 3119 abfd->output_has_begun = true; 3120 3121 return true; 3122} 3123 3124/* Create a mapping from a set of sections to a program segment. */ 3125 3126static INLINE struct elf_segment_map * 3127make_mapping (abfd, sections, from, to, phdr) 3128 bfd *abfd; 3129 asection **sections; 3130 unsigned int from; 3131 unsigned int to; 3132 boolean phdr; 3133{ 3134 struct elf_segment_map *m; 3135 unsigned int i; 3136 asection **hdrpp; 3137 bfd_size_type amt; 3138 3139 amt = sizeof (struct elf_segment_map); 3140 amt += (to - from - 1) * sizeof (asection *); 3141 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 3142 if (m == NULL) 3143 return NULL; 3144 m->next = NULL; 3145 m->p_type = PT_LOAD; 3146 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) 3147 m->sections[i - from] = *hdrpp; 3148 m->count = to - from; 3149 3150 if (from == 0 && phdr) 3151 { 3152 /* Include the headers in the first PT_LOAD segment. */ 3153 m->includes_filehdr = 1; 3154 m->includes_phdrs = 1; 3155 } 3156 3157 return m; 3158} 3159 3160/* Set up a mapping from BFD sections to program segments. */ 3161 3162static boolean 3163map_sections_to_segments (abfd) 3164 bfd *abfd; 3165{ 3166 asection **sections = NULL; 3167 asection *s; 3168 unsigned int i; 3169 unsigned int count; 3170 struct elf_segment_map *mfirst; 3171 struct elf_segment_map **pm; 3172 struct elf_segment_map *m; 3173 asection *last_hdr; 3174 unsigned int phdr_index; 3175 bfd_vma maxpagesize; 3176 asection **hdrpp; 3177 boolean phdr_in_segment = true; 3178 boolean writable; 3179 int tls_count = 0; 3180 asection *first_tls = NULL; 3181 asection *dynsec, *eh_frame_hdr; 3182 bfd_size_type amt; 3183 3184 if (elf_tdata (abfd)->segment_map != NULL) 3185 return true; 3186 3187 if (bfd_count_sections (abfd) == 0) 3188 return true; 3189 3190 /* Select the allocated sections, and sort them. */ 3191 3192 amt = bfd_count_sections (abfd) * sizeof (asection *); 3193 sections = (asection **) bfd_malloc (amt); 3194 if (sections == NULL) 3195 goto error_return; 3196 3197 i = 0; 3198 for (s = abfd->sections; s != NULL; s = s->next) 3199 { 3200 if ((s->flags & SEC_ALLOC) != 0) 3201 { 3202 sections[i] = s; 3203 ++i; 3204 } 3205 } 3206 BFD_ASSERT (i <= bfd_count_sections (abfd)); 3207 count = i; 3208 3209 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); 3210 3211 /* Build the mapping. */ 3212 3213 mfirst = NULL; 3214 pm = &mfirst; 3215 3216 /* If we have a .interp section, then create a PT_PHDR segment for 3217 the program headers and a PT_INTERP segment for the .interp 3218 section. */ 3219 s = bfd_get_section_by_name (abfd, ".interp"); 3220 if (s != NULL && (s->flags & SEC_LOAD) != 0) 3221 { 3222 amt = sizeof (struct elf_segment_map); 3223 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 3224 if (m == NULL) 3225 goto error_return; 3226 m->next = NULL; 3227 m->p_type = PT_PHDR; 3228 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ 3229 m->p_flags = PF_R | PF_X; 3230 m->p_flags_valid = 1; 3231 m->includes_phdrs = 1; 3232 3233 *pm = m; 3234 pm = &m->next; 3235 3236 amt = sizeof (struct elf_segment_map); 3237 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 3238 if (m == NULL) 3239 goto error_return; 3240 m->next = NULL; 3241 m->p_type = PT_INTERP; 3242 m->count = 1; 3243 m->sections[0] = s; 3244 3245 *pm = m; 3246 pm = &m->next; 3247 } 3248 3249 /* Look through the sections. We put sections in the same program 3250 segment when the start of the second section can be placed within 3251 a few bytes of the end of the first section. */ 3252 last_hdr = NULL; 3253 phdr_index = 0; 3254 maxpagesize = get_elf_backend_data (abfd)->maxpagesize; 3255 writable = false; 3256 dynsec = bfd_get_section_by_name (abfd, ".dynamic"); 3257 if (dynsec != NULL 3258 && (dynsec->flags & SEC_LOAD) == 0) 3259 dynsec = NULL; 3260 3261 /* Deal with -Ttext or something similar such that the first section 3262 is not adjacent to the program headers. This is an 3263 approximation, since at this point we don't know exactly how many 3264 program headers we will need. */ 3265 if (count > 0) 3266 { 3267 bfd_size_type phdr_size; 3268 3269 phdr_size = elf_tdata (abfd)->program_header_size; 3270 if (phdr_size == 0) 3271 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr; 3272 if ((abfd->flags & D_PAGED) == 0 3273 || sections[0]->lma < phdr_size 3274 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize) 3275 phdr_in_segment = false; 3276 } 3277 3278 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) 3279 { 3280 asection *hdr; 3281 boolean new_segment; 3282 3283 hdr = *hdrpp; 3284 3285 /* See if this section and the last one will fit in the same 3286 segment. */ 3287 3288 if (last_hdr == NULL) 3289 { 3290 /* If we don't have a segment yet, then we don't need a new 3291 one (we build the last one after this loop). */ 3292 new_segment = false; 3293 } 3294 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) 3295 { 3296 /* If this section has a different relation between the 3297 virtual address and the load address, then we need a new 3298 segment. */ 3299 new_segment = true; 3300 } 3301 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize) 3302 < BFD_ALIGN (hdr->lma, maxpagesize)) 3303 { 3304 /* If putting this section in this segment would force us to 3305 skip a page in the segment, then we need a new segment. */ 3306 new_segment = true; 3307 } 3308 else if ((last_hdr->flags & SEC_LOAD) == 0 3309 && (hdr->flags & SEC_LOAD) != 0) 3310 { 3311 /* We don't want to put a loadable section after a 3312 nonloadable section in the same segment. */ 3313 new_segment = true; 3314 } 3315 else if ((abfd->flags & D_PAGED) == 0) 3316 { 3317 /* If the file is not demand paged, which means that we 3318 don't require the sections to be correctly aligned in the 3319 file, then there is no other reason for a new segment. */ 3320 new_segment = false; 3321 } 3322 else if (! writable 3323 && (hdr->flags & SEC_READONLY) == 0 3324 && (((last_hdr->lma + last_hdr->_raw_size - 1) 3325 & ~(maxpagesize - 1)) 3326 != (hdr->lma & ~(maxpagesize - 1)))) 3327 { 3328 /* We don't want to put a writable section in a read only 3329 segment, unless they are on the same page in memory 3330 anyhow. We already know that the last section does not 3331 bring us past the current section on the page, so the 3332 only case in which the new section is not on the same 3333 page as the previous section is when the previous section 3334 ends precisely on a page boundary. */ 3335 new_segment = true; 3336 } 3337 else 3338 { 3339 /* Otherwise, we can use the same segment. */ 3340 new_segment = false; 3341 } 3342 3343 if (! new_segment) 3344 { 3345 if ((hdr->flags & SEC_READONLY) == 0) 3346 writable = true; 3347 last_hdr = hdr; 3348 continue; 3349 } 3350 3351 /* We need a new program segment. We must create a new program 3352 header holding all the sections from phdr_index until hdr. */ 3353 3354 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); 3355 if (m == NULL) 3356 goto error_return; 3357 3358 *pm = m; 3359 pm = &m->next; 3360 3361 if ((hdr->flags & SEC_READONLY) == 0) 3362 writable = true; 3363 else 3364 writable = false; 3365 3366 last_hdr = hdr; 3367 phdr_index = i; 3368 phdr_in_segment = false; 3369 } 3370 3371 /* Create a final PT_LOAD program segment. */ 3372 if (last_hdr != NULL) 3373 { 3374 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); 3375 if (m == NULL) 3376 goto error_return; 3377 3378 *pm = m; 3379 pm = &m->next; 3380 } 3381 3382 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ 3383 if (dynsec != NULL) 3384 { 3385 amt = sizeof (struct elf_segment_map); 3386 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 3387 if (m == NULL) 3388 goto error_return; 3389 m->next = NULL; 3390 m->p_type = PT_DYNAMIC; 3391 m->count = 1; 3392 m->sections[0] = dynsec; 3393 3394 *pm = m; 3395 pm = &m->next; 3396 } 3397 3398 /* For each loadable .note section, add a PT_NOTE segment. We don't 3399 use bfd_get_section_by_name, because if we link together 3400 nonloadable .note sections and loadable .note sections, we will 3401 generate two .note sections in the output file. FIXME: Using 3402 names for section types is bogus anyhow. */ 3403 for (s = abfd->sections; s != NULL; s = s->next) 3404 { 3405 if ((s->flags & SEC_LOAD) != 0 3406 && strncmp (s->name, ".note", 5) == 0) 3407 { 3408 amt = sizeof (struct elf_segment_map); 3409 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 3410 if (m == NULL) 3411 goto error_return; 3412 m->next = NULL; 3413 m->p_type = PT_NOTE; 3414 m->count = 1; 3415 m->sections[0] = s; 3416 3417 *pm = m; 3418 pm = &m->next; 3419 } 3420 if (s->flags & SEC_THREAD_LOCAL) 3421 { 3422 if (! tls_count) 3423 first_tls = s; 3424 tls_count++; 3425 } 3426 } 3427 3428 /* If there are any SHF_TLS output sections, add PT_TLS segment. */ 3429 if (tls_count > 0) 3430 { 3431 int i; 3432 3433 amt = sizeof (struct elf_segment_map); 3434 amt += (tls_count - 1) * sizeof (asection *); 3435 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 3436 if (m == NULL) 3437 goto error_return; 3438 m->next = NULL; 3439 m->p_type = PT_TLS; 3440 m->count = tls_count; 3441 /* Mandated PF_R. */ 3442 m->p_flags = PF_R; 3443 m->p_flags_valid = 1; 3444 for (i = 0; i < tls_count; ++i) 3445 { 3446 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL); 3447 m->sections[i] = first_tls; 3448 first_tls = first_tls->next; 3449 } 3450 3451 *pm = m; 3452 pm = &m->next; 3453 } 3454 3455 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME 3456 segment. */ 3457 eh_frame_hdr = NULL; 3458 if (elf_tdata (abfd)->eh_frame_hdr) 3459 eh_frame_hdr = bfd_get_section_by_name (abfd, ".eh_frame_hdr"); 3460 if (eh_frame_hdr != NULL && (eh_frame_hdr->flags & SEC_LOAD)) 3461 { 3462 amt = sizeof (struct elf_segment_map); 3463 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 3464 if (m == NULL) 3465 goto error_return; 3466 m->next = NULL; 3467 m->p_type = PT_GNU_EH_FRAME; 3468 m->count = 1; 3469 m->sections[0] = eh_frame_hdr; 3470 3471 *pm = m; 3472 pm = &m->next; 3473 } 3474 3475 free (sections); 3476 sections = NULL; 3477 3478 elf_tdata (abfd)->segment_map = mfirst; 3479 return true; 3480 3481 error_return: 3482 if (sections != NULL) 3483 free (sections); 3484 return false; 3485} 3486 3487/* Sort sections by address. */ 3488 3489static int 3490elf_sort_sections (arg1, arg2) 3491 const PTR arg1; 3492 const PTR arg2; 3493{ 3494 const asection *sec1 = *(const asection **) arg1; 3495 const asection *sec2 = *(const asection **) arg2; 3496 3497 /* Sort by LMA first, since this is the address used to 3498 place the section into a segment. */ 3499 if (sec1->lma < sec2->lma) 3500 return -1; 3501 else if (sec1->lma > sec2->lma) 3502 return 1; 3503 3504 /* Then sort by VMA. Normally the LMA and the VMA will be 3505 the same, and this will do nothing. */ 3506 if (sec1->vma < sec2->vma) 3507 return -1; 3508 else if (sec1->vma > sec2->vma) 3509 return 1; 3510 3511 /* Put !SEC_LOAD sections after SEC_LOAD ones. */ 3512 3513#define TOEND(x) (((x)->flags & SEC_LOAD) == 0) 3514 3515 if (TOEND (sec1)) 3516 { 3517 if (TOEND (sec2)) 3518 { 3519 /* If the indicies are the same, do not return 0 3520 here, but continue to try the next comparison. */ 3521 if (sec1->target_index - sec2->target_index != 0) 3522 return sec1->target_index - sec2->target_index; 3523 } 3524 else 3525 return 1; 3526 } 3527 else if (TOEND (sec2)) 3528 return -1; 3529 3530#undef TOEND 3531 3532 /* Sort by size, to put zero sized sections 3533 before others at the same address. */ 3534 3535 if (sec1->_raw_size < sec2->_raw_size) 3536 return -1; 3537 if (sec1->_raw_size > sec2->_raw_size) 3538 return 1; 3539 3540 return sec1->target_index - sec2->target_index; 3541} 3542 3543/* Assign file positions to the sections based on the mapping from 3544 sections to segments. This function also sets up some fields in 3545 the file header, and writes out the program headers. */ 3546 3547static boolean 3548assign_file_positions_for_segments (abfd) 3549 bfd *abfd; 3550{ 3551 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3552 unsigned int count; 3553 struct elf_segment_map *m; 3554 unsigned int alloc; 3555 Elf_Internal_Phdr *phdrs; 3556 file_ptr off, voff; 3557 bfd_vma filehdr_vaddr, filehdr_paddr; 3558 bfd_vma phdrs_vaddr, phdrs_paddr; 3559 Elf_Internal_Phdr *p; 3560 bfd_size_type amt; 3561 3562 if (elf_tdata (abfd)->segment_map == NULL) 3563 { 3564 if (! map_sections_to_segments (abfd)) 3565 return false; 3566 } 3567 else 3568 { 3569 /* The placement algorithm assumes that non allocated sections are 3570 not in PT_LOAD segments. We ensure this here by removing such 3571 sections from the segment map. */ 3572 for (m = elf_tdata (abfd)->segment_map; 3573 m != NULL; 3574 m = m->next) 3575 { 3576 unsigned int new_count; 3577 unsigned int i; 3578 3579 if (m->p_type != PT_LOAD) 3580 continue; 3581 3582 new_count = 0; 3583 for (i = 0; i < m->count; i ++) 3584 { 3585 if ((m->sections[i]->flags & SEC_ALLOC) != 0) 3586 { 3587 if (i != new_count) 3588 m->sections[new_count] = m->sections[i]; 3589 3590 new_count ++; 3591 } 3592 } 3593 3594 if (new_count != m->count) 3595 m->count = new_count; 3596 } 3597 } 3598 3599 if (bed->elf_backend_modify_segment_map) 3600 { 3601 if (! (*bed->elf_backend_modify_segment_map) (abfd)) 3602 return false; 3603 } 3604 3605 count = 0; 3606 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) 3607 ++count; 3608 3609 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; 3610 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; 3611 elf_elfheader (abfd)->e_phnum = count; 3612 3613 if (count == 0) 3614 return true; 3615 3616 /* If we already counted the number of program segments, make sure 3617 that we allocated enough space. This happens when SIZEOF_HEADERS 3618 is used in a linker script. */ 3619 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr; 3620 if (alloc != 0 && count > alloc) 3621 { 3622 ((*_bfd_error_handler) 3623 (_("%s: Not enough room for program headers (allocated %u, need %u)"), 3624 bfd_get_filename (abfd), alloc, count)); 3625 bfd_set_error (bfd_error_bad_value); 3626 return false; 3627 } 3628 3629 if (alloc == 0) 3630 alloc = count; 3631 3632 amt = alloc * sizeof (Elf_Internal_Phdr); 3633 phdrs = (Elf_Internal_Phdr *) bfd_alloc (abfd, amt); 3634 if (phdrs == NULL) 3635 return false; 3636 3637 off = bed->s->sizeof_ehdr; 3638 off += alloc * bed->s->sizeof_phdr; 3639 3640 filehdr_vaddr = 0; 3641 filehdr_paddr = 0; 3642 phdrs_vaddr = 0; 3643 phdrs_paddr = 0; 3644 3645 for (m = elf_tdata (abfd)->segment_map, p = phdrs; 3646 m != NULL; 3647 m = m->next, p++) 3648 { 3649 unsigned int i; 3650 asection **secpp; 3651 3652 /* If elf_segment_map is not from map_sections_to_segments, the 3653 sections may not be correctly ordered. NOTE: sorting should 3654 not be done to the PT_NOTE section of a corefile, which may 3655 contain several pseudo-sections artificially created by bfd. 3656 Sorting these pseudo-sections breaks things badly. */ 3657 if (m->count > 1 3658 && !(elf_elfheader (abfd)->e_type == ET_CORE 3659 && m->p_type == PT_NOTE)) 3660 qsort (m->sections, (size_t) m->count, sizeof (asection *), 3661 elf_sort_sections); 3662 3663 p->p_type = m->p_type; 3664 p->p_flags = m->p_flags; 3665 3666 if (p->p_type == PT_LOAD 3667 && m->count > 0 3668 && (m->sections[0]->flags & SEC_ALLOC) != 0) 3669 { 3670 if ((abfd->flags & D_PAGED) != 0) 3671 off += (m->sections[0]->vma - off) % bed->maxpagesize; 3672 else 3673 { 3674 bfd_size_type align; 3675 3676 align = 0; 3677 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 3678 { 3679 bfd_size_type secalign; 3680 3681 secalign = bfd_get_section_alignment (abfd, *secpp); 3682 if (secalign > align) 3683 align = secalign; 3684 } 3685 3686 off += (m->sections[0]->vma - off) % (1 << align); 3687 } 3688 } 3689 3690 if (m->count == 0) 3691 p->p_vaddr = 0; 3692 else 3693 p->p_vaddr = m->sections[0]->vma; 3694 3695 if (m->p_paddr_valid) 3696 p->p_paddr = m->p_paddr; 3697 else if (m->count == 0) 3698 p->p_paddr = 0; 3699 else 3700 p->p_paddr = m->sections[0]->lma; 3701 3702 if (p->p_type == PT_LOAD 3703 && (abfd->flags & D_PAGED) != 0) 3704 p->p_align = bed->maxpagesize; 3705 else if (m->count == 0) 3706 p->p_align = bed->s->file_align; 3707 else 3708 p->p_align = 0; 3709 3710 p->p_offset = 0; 3711 p->p_filesz = 0; 3712 p->p_memsz = 0; 3713 3714 if (m->includes_filehdr) 3715 { 3716 if (! m->p_flags_valid) 3717 p->p_flags |= PF_R; 3718 p->p_offset = 0; 3719 p->p_filesz = bed->s->sizeof_ehdr; 3720 p->p_memsz = bed->s->sizeof_ehdr; 3721 if (m->count > 0) 3722 { 3723 BFD_ASSERT (p->p_type == PT_LOAD); 3724 3725 if (p->p_vaddr < (bfd_vma) off) 3726 { 3727 (*_bfd_error_handler) 3728 (_("%s: Not enough room for program headers, try linking with -N"), 3729 bfd_get_filename (abfd)); 3730 bfd_set_error (bfd_error_bad_value); 3731 return false; 3732 } 3733 3734 p->p_vaddr -= off; 3735 if (! m->p_paddr_valid) 3736 p->p_paddr -= off; 3737 } 3738 if (p->p_type == PT_LOAD) 3739 { 3740 filehdr_vaddr = p->p_vaddr; 3741 filehdr_paddr = p->p_paddr; 3742 } 3743 } 3744 3745 if (m->includes_phdrs) 3746 { 3747 if (! m->p_flags_valid) 3748 p->p_flags |= PF_R; 3749 3750 if (m->includes_filehdr) 3751 { 3752 if (p->p_type == PT_LOAD) 3753 { 3754 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr; 3755 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr; 3756 } 3757 } 3758 else 3759 { 3760 p->p_offset = bed->s->sizeof_ehdr; 3761 3762 if (m->count > 0) 3763 { 3764 BFD_ASSERT (p->p_type == PT_LOAD); 3765 p->p_vaddr -= off - p->p_offset; 3766 if (! m->p_paddr_valid) 3767 p->p_paddr -= off - p->p_offset; 3768 } 3769 3770 if (p->p_type == PT_LOAD) 3771 { 3772 phdrs_vaddr = p->p_vaddr; 3773 phdrs_paddr = p->p_paddr; 3774 } 3775 else 3776 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; 3777 } 3778 3779 p->p_filesz += alloc * bed->s->sizeof_phdr; 3780 p->p_memsz += alloc * bed->s->sizeof_phdr; 3781 } 3782 3783 if (p->p_type == PT_LOAD 3784 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) 3785 { 3786 if (! m->includes_filehdr && ! m->includes_phdrs) 3787 p->p_offset = off; 3788 else 3789 { 3790 file_ptr adjust; 3791 3792 adjust = off - (p->p_offset + p->p_filesz); 3793 p->p_filesz += adjust; 3794 p->p_memsz += adjust; 3795 } 3796 } 3797 3798 voff = off; 3799 3800 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 3801 { 3802 asection *sec; 3803 flagword flags; 3804 bfd_size_type align; 3805 3806 sec = *secpp; 3807 flags = sec->flags; 3808 align = 1 << bfd_get_section_alignment (abfd, sec); 3809 3810 /* The section may have artificial alignment forced by a 3811 link script. Notice this case by the gap between the 3812 cumulative phdr lma and the section's lma. */ 3813 if (p->p_paddr + p->p_memsz < sec->lma) 3814 { 3815 bfd_vma adjust = sec->lma - (p->p_paddr + p->p_memsz); 3816 3817 p->p_memsz += adjust; 3818 off += adjust; 3819 voff += adjust; 3820 if ((flags & SEC_LOAD) != 0) 3821 p->p_filesz += adjust; 3822 } 3823 3824 if (p->p_type == PT_LOAD) 3825 { 3826 bfd_signed_vma adjust; 3827 3828 if ((flags & SEC_LOAD) != 0) 3829 { 3830 adjust = sec->lma - (p->p_paddr + p->p_memsz); 3831 if (adjust < 0) 3832 adjust = 0; 3833 } 3834 else if ((flags & SEC_ALLOC) != 0) 3835 { 3836 /* The section VMA must equal the file position 3837 modulo the page size. FIXME: I'm not sure if 3838 this adjustment is really necessary. We used to 3839 not have the SEC_LOAD case just above, and then 3840 this was necessary, but now I'm not sure. */ 3841 if ((abfd->flags & D_PAGED) != 0) 3842 adjust = (sec->vma - voff) % bed->maxpagesize; 3843 else 3844 adjust = (sec->vma - voff) % align; 3845 } 3846 else 3847 adjust = 0; 3848 3849 if (adjust != 0) 3850 { 3851 if (i == 0) 3852 { 3853 (* _bfd_error_handler) (_("\ 3854Error: First section in segment (%s) starts at 0x%x whereas the segment starts at 0x%x"), 3855 bfd_section_name (abfd, sec), 3856 sec->lma, 3857 p->p_paddr); 3858 return false; 3859 } 3860 p->p_memsz += adjust; 3861 off += adjust; 3862 voff += adjust; 3863 if ((flags & SEC_LOAD) != 0) 3864 p->p_filesz += adjust; 3865 } 3866 3867 sec->filepos = off; 3868 3869 /* We check SEC_HAS_CONTENTS here because if NOLOAD is 3870 used in a linker script we may have a section with 3871 SEC_LOAD clear but which is supposed to have 3872 contents. */ 3873 if ((flags & SEC_LOAD) != 0 3874 || (flags & SEC_HAS_CONTENTS) != 0) 3875 off += sec->_raw_size; 3876 3877 if ((flags & SEC_ALLOC) != 0) 3878 voff += sec->_raw_size; 3879 } 3880 3881 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) 3882 { 3883 /* The actual "note" segment has i == 0. 3884 This is the one that actually contains everything. */ 3885 if (i == 0) 3886 { 3887 sec->filepos = off; 3888 p->p_filesz = sec->_raw_size; 3889 off += sec->_raw_size; 3890 voff = off; 3891 } 3892 else 3893 { 3894 /* Fake sections -- don't need to be written. */ 3895 sec->filepos = 0; 3896 sec->_raw_size = 0; 3897 flags = sec->flags = 0; 3898 } 3899 p->p_memsz = 0; 3900 p->p_align = 1; 3901 } 3902 else 3903 { 3904 p->p_memsz += sec->_raw_size; 3905 3906 if ((flags & SEC_LOAD) != 0) 3907 p->p_filesz += sec->_raw_size; 3908 3909 if (p->p_type == PT_TLS 3910 && sec->_raw_size == 0 3911 && (sec->flags & SEC_HAS_CONTENTS) == 0) 3912 { 3913 struct bfd_link_order *o; 3914 bfd_vma tbss_size = 0; 3915 3916 for (o = sec->link_order_head; o != NULL; o = o->next) 3917 if (tbss_size < o->offset + o->size) 3918 tbss_size = o->offset + o->size; 3919 3920 p->p_memsz += tbss_size; 3921 } 3922 3923 if (align > p->p_align 3924 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0)) 3925 p->p_align = align; 3926 } 3927 3928 if (! m->p_flags_valid) 3929 { 3930 p->p_flags |= PF_R; 3931 if ((flags & SEC_CODE) != 0) 3932 p->p_flags |= PF_X; 3933 if ((flags & SEC_READONLY) == 0) 3934 p->p_flags |= PF_W; 3935 } 3936 } 3937 } 3938 3939 /* Now that we have set the section file positions, we can set up 3940 the file positions for the non PT_LOAD segments. */ 3941 for (m = elf_tdata (abfd)->segment_map, p = phdrs; 3942 m != NULL; 3943 m = m->next, p++) 3944 { 3945 if (p->p_type != PT_LOAD && m->count > 0) 3946 { 3947 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs); 3948 p->p_offset = m->sections[0]->filepos; 3949 } 3950 if (m->count == 0) 3951 { 3952 if (m->includes_filehdr) 3953 { 3954 p->p_vaddr = filehdr_vaddr; 3955 if (! m->p_paddr_valid) 3956 p->p_paddr = filehdr_paddr; 3957 } 3958 else if (m->includes_phdrs) 3959 { 3960 p->p_vaddr = phdrs_vaddr; 3961 if (! m->p_paddr_valid) 3962 p->p_paddr = phdrs_paddr; 3963 } 3964 } 3965 } 3966 3967 /* If additional nonloadable filepos adjustments are required, 3968 do them now. */ 3969 if (bed->set_nonloadable_filepos) 3970 (*bed->set_nonloadable_filepos) (abfd, phdrs); 3971 3972 /* Clear out any program headers we allocated but did not use. */ 3973 for (; count < alloc; count++, p++) 3974 { 3975 memset (p, 0, sizeof *p); 3976 p->p_type = PT_NULL; 3977 } 3978 3979 elf_tdata (abfd)->phdr = phdrs; 3980 3981 elf_tdata (abfd)->next_file_pos = off; 3982 3983 /* Write out the program headers. */ 3984 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 3985 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0) 3986 return false; 3987 3988 return true; 3989} 3990 3991/* Get the size of the program header. 3992 3993 If this is called by the linker before any of the section VMA's are set, it 3994 can't calculate the correct value for a strange memory layout. This only 3995 happens when SIZEOF_HEADERS is used in a linker script. In this case, 3996 SORTED_HDRS is NULL and we assume the normal scenario of one text and one 3997 data segment (exclusive of .interp and .dynamic). 3998 3999 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there 4000 will be two segments. */ 4001 4002static bfd_size_type 4003get_program_header_size (abfd) 4004 bfd *abfd; 4005{ 4006 size_t segs; 4007 asection *s; 4008 struct elf_backend_data *bed = get_elf_backend_data (abfd); 4009 4010 /* We can't return a different result each time we're called. */ 4011 if (elf_tdata (abfd)->program_header_size != 0) 4012 return elf_tdata (abfd)->program_header_size; 4013 4014 if (elf_tdata (abfd)->segment_map != NULL) 4015 { 4016 struct elf_segment_map *m; 4017 4018 segs = 0; 4019 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) 4020 ++segs; 4021 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; 4022 return elf_tdata (abfd)->program_header_size; 4023 } 4024 4025 /* Assume we will need exactly two PT_LOAD segments: one for text 4026 and one for data. */ 4027 segs = 2; 4028 4029 s = bfd_get_section_by_name (abfd, ".interp"); 4030 if (s != NULL && (s->flags & SEC_LOAD) != 0) 4031 { 4032 /* If we have a loadable interpreter section, we need a 4033 PT_INTERP segment. In this case, assume we also need a 4034 PT_PHDR segment, although that may not be true for all 4035 targets. */ 4036 segs += 2; 4037 } 4038 4039 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) 4040 { 4041 /* We need a PT_DYNAMIC segment. */ 4042 ++segs; 4043 } 4044 4045 if (elf_tdata (abfd)->eh_frame_hdr 4046 && bfd_get_section_by_name (abfd, ".eh_frame_hdr") != NULL) 4047 { 4048 /* We need a PT_GNU_EH_FRAME segment. */ 4049 ++segs; 4050 } 4051 4052 for (s = abfd->sections; s != NULL; s = s->next) 4053 { 4054 if ((s->flags & SEC_LOAD) != 0 4055 && strncmp (s->name, ".note", 5) == 0) 4056 { 4057 /* We need a PT_NOTE segment. */ 4058 ++segs; 4059 } 4060 } 4061 4062 for (s = abfd->sections; s != NULL; s = s->next) 4063 { 4064 if (s->flags & SEC_THREAD_LOCAL) 4065 { 4066 /* We need a PT_TLS segment. */ 4067 ++segs; 4068 break; 4069 } 4070 } 4071 4072 /* Let the backend count up any program headers it might need. */ 4073 if (bed->elf_backend_additional_program_headers) 4074 { 4075 int a; 4076 4077 a = (*bed->elf_backend_additional_program_headers) (abfd); 4078 if (a == -1) 4079 abort (); 4080 segs += a; 4081 } 4082 4083 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; 4084 return elf_tdata (abfd)->program_header_size; 4085} 4086 4087/* Work out the file positions of all the sections. This is called by 4088 _bfd_elf_compute_section_file_positions. All the section sizes and 4089 VMAs must be known before this is called. 4090 4091 We do not consider reloc sections at this point, unless they form 4092 part of the loadable image. Reloc sections are assigned file 4093 positions in assign_file_positions_for_relocs, which is called by 4094 write_object_contents and final_link. 4095 4096 We also don't set the positions of the .symtab and .strtab here. */ 4097 4098static boolean 4099assign_file_positions_except_relocs (abfd) 4100 bfd *abfd; 4101{ 4102 struct elf_obj_tdata * const tdata = elf_tdata (abfd); 4103 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd); 4104 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); 4105 unsigned int num_sec = elf_numsections (abfd); 4106 file_ptr off; 4107 struct elf_backend_data *bed = get_elf_backend_data (abfd); 4108 4109 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 4110 && bfd_get_format (abfd) != bfd_core) 4111 { 4112 Elf_Internal_Shdr **hdrpp; 4113 unsigned int i; 4114 4115 /* Start after the ELF header. */ 4116 off = i_ehdrp->e_ehsize; 4117 4118 /* We are not creating an executable, which means that we are 4119 not creating a program header, and that the actual order of 4120 the sections in the file is unimportant. */ 4121 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) 4122 { 4123 Elf_Internal_Shdr *hdr; 4124 4125 hdr = *hdrpp; 4126 if (hdr->sh_type == SHT_REL 4127 || hdr->sh_type == SHT_RELA 4128 || i == tdata->symtab_section 4129 || i == tdata->symtab_shndx_section 4130 || i == tdata->strtab_section) 4131 { 4132 hdr->sh_offset = -1; 4133 } 4134 else 4135 off = _bfd_elf_assign_file_position_for_section (hdr, off, true); 4136 4137 if (i == SHN_LORESERVE - 1) 4138 { 4139 i += SHN_HIRESERVE + 1 - SHN_LORESERVE; 4140 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE; 4141 } 4142 } 4143 } 4144 else 4145 { 4146 unsigned int i; 4147 Elf_Internal_Shdr **hdrpp; 4148 4149 /* Assign file positions for the loaded sections based on the 4150 assignment of sections to segments. */ 4151 if (! assign_file_positions_for_segments (abfd)) 4152 return false; 4153 4154 /* Assign file positions for the other sections. */ 4155 4156 off = elf_tdata (abfd)->next_file_pos; 4157 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) 4158 { 4159 Elf_Internal_Shdr *hdr; 4160 4161 hdr = *hdrpp; 4162 if (hdr->bfd_section != NULL 4163 && hdr->bfd_section->filepos != 0) 4164 hdr->sh_offset = hdr->bfd_section->filepos; 4165 else if ((hdr->sh_flags & SHF_ALLOC) != 0) 4166 { 4167 ((*_bfd_error_handler) 4168 (_("%s: warning: allocated section `%s' not in segment"), 4169 bfd_get_filename (abfd), 4170 (hdr->bfd_section == NULL 4171 ? "*unknown*" 4172 : hdr->bfd_section->name))); 4173 if ((abfd->flags & D_PAGED) != 0) 4174 off += (hdr->sh_addr - off) % bed->maxpagesize; 4175 else 4176 off += (hdr->sh_addr - off) % hdr->sh_addralign; 4177 off = _bfd_elf_assign_file_position_for_section (hdr, off, 4178 false); 4179 } 4180 else if (hdr->sh_type == SHT_REL 4181 || hdr->sh_type == SHT_RELA 4182 || hdr == i_shdrpp[tdata->symtab_section] 4183 || hdr == i_shdrpp[tdata->symtab_shndx_section] 4184 || hdr == i_shdrpp[tdata->strtab_section]) 4185 hdr->sh_offset = -1; 4186 else 4187 off = _bfd_elf_assign_file_position_for_section (hdr, off, true); 4188 4189 if (i == SHN_LORESERVE - 1) 4190 { 4191 i += SHN_HIRESERVE + 1 - SHN_LORESERVE; 4192 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE; 4193 } 4194 } 4195 } 4196 4197 /* Place the section headers. */ 4198 off = align_file_position (off, bed->s->file_align); 4199 i_ehdrp->e_shoff = off; 4200 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; 4201 4202 elf_tdata (abfd)->next_file_pos = off; 4203 4204 return true; 4205} 4206 4207static boolean 4208prep_headers (abfd) 4209 bfd *abfd; 4210{ 4211 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ 4212 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ 4213 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ 4214 struct elf_strtab_hash *shstrtab; 4215 struct elf_backend_data *bed = get_elf_backend_data (abfd); 4216 4217 i_ehdrp = elf_elfheader (abfd); 4218 i_shdrp = elf_elfsections (abfd); 4219 4220 shstrtab = _bfd_elf_strtab_init (); 4221 if (shstrtab == NULL) 4222 return false; 4223 4224 elf_shstrtab (abfd) = shstrtab; 4225 4226 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; 4227 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; 4228 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; 4229 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; 4230 4231 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; 4232 i_ehdrp->e_ident[EI_DATA] = 4233 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; 4234 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; 4235 4236 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 4237#ifdef WANT_OLD_BRANDELF_METHOD 4238#define _OLD_EI_BRAND_OFFSET 8 4239#define _OLD_BRANDING "FreeBSD" 4240 strncpy((char *) &i_ehdrp->e_ident[_OLD_EI_BRAND_OFFSET], _OLD_BRANDING, 4241 EI_NIDENT-_OLD_EI_BRAND_OFFSET); 4242#endif 4243 4244 if ((abfd->flags & DYNAMIC) != 0) 4245 i_ehdrp->e_type = ET_DYN; 4246 else if ((abfd->flags & EXEC_P) != 0) 4247 i_ehdrp->e_type = ET_EXEC; 4248 else if (bfd_get_format (abfd) == bfd_core) 4249 i_ehdrp->e_type = ET_CORE; 4250 else 4251 i_ehdrp->e_type = ET_REL; 4252 4253 switch (bfd_get_arch (abfd)) 4254 { 4255 case bfd_arch_unknown: 4256 i_ehdrp->e_machine = EM_NONE; 4257 break; 4258 4259 /* There used to be a long list of cases here, each one setting 4260 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE 4261 in the corresponding bfd definition. To avoid duplication, 4262 the switch was removed. Machines that need special handling 4263 can generally do it in elf_backend_final_write_processing(), 4264 unless they need the information earlier than the final write. 4265 Such need can generally be supplied by replacing the tests for 4266 e_machine with the conditions used to determine it. */ 4267 default: 4268 if (get_elf_backend_data (abfd) != NULL) 4269 i_ehdrp->e_machine = get_elf_backend_data (abfd)->elf_machine_code; 4270 else 4271 i_ehdrp->e_machine = EM_NONE; 4272 } 4273 4274 i_ehdrp->e_version = bed->s->ev_current; 4275 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; 4276 4277 /* No program header, for now. */ 4278 i_ehdrp->e_phoff = 0; 4279 i_ehdrp->e_phentsize = 0; 4280 i_ehdrp->e_phnum = 0; 4281 4282 /* Each bfd section is section header entry. */ 4283 i_ehdrp->e_entry = bfd_get_start_address (abfd); 4284 i_ehdrp->e_shentsize = bed->s->sizeof_shdr; 4285 4286 /* If we're building an executable, we'll need a program header table. */ 4287 if (abfd->flags & EXEC_P) 4288 { 4289 /* It all happens later. */ 4290#if 0 4291 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr); 4292 4293 /* elf_build_phdrs() returns a (NULL-terminated) array of 4294 Elf_Internal_Phdrs. */ 4295 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum); 4296 i_ehdrp->e_phoff = outbase; 4297 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum; 4298#endif 4299 } 4300 else 4301 { 4302 i_ehdrp->e_phentsize = 0; 4303 i_phdrp = 0; 4304 i_ehdrp->e_phoff = 0; 4305 } 4306 4307 elf_tdata (abfd)->symtab_hdr.sh_name = 4308 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false); 4309 elf_tdata (abfd)->strtab_hdr.sh_name = 4310 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false); 4311 elf_tdata (abfd)->shstrtab_hdr.sh_name = 4312 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false); 4313 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 4314 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 4315 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) 4316 return false; 4317 4318 return true; 4319} 4320 4321/* Assign file positions for all the reloc sections which are not part 4322 of the loadable file image. */ 4323 4324void 4325_bfd_elf_assign_file_positions_for_relocs (abfd) 4326 bfd *abfd; 4327{ 4328 file_ptr off; 4329 unsigned int i, num_sec; 4330 Elf_Internal_Shdr **shdrpp; 4331 4332 off = elf_tdata (abfd)->next_file_pos; 4333 4334 num_sec = elf_numsections (abfd); 4335 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++) 4336 { 4337 Elf_Internal_Shdr *shdrp; 4338 4339 shdrp = *shdrpp; 4340 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) 4341 && shdrp->sh_offset == -1) 4342 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true); 4343 } 4344 4345 elf_tdata (abfd)->next_file_pos = off; 4346} 4347 4348boolean 4349_bfd_elf_write_object_contents (abfd) 4350 bfd *abfd; 4351{ 4352 struct elf_backend_data *bed = get_elf_backend_data (abfd); 4353 Elf_Internal_Ehdr *i_ehdrp; 4354 Elf_Internal_Shdr **i_shdrp; 4355 boolean failed; 4356 unsigned int count, num_sec; 4357 4358 if (! abfd->output_has_begun 4359 && ! _bfd_elf_compute_section_file_positions 4360 (abfd, (struct bfd_link_info *) NULL)) 4361 return false; 4362 4363 i_shdrp = elf_elfsections (abfd); 4364 i_ehdrp = elf_elfheader (abfd); 4365 4366 failed = false; 4367 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); 4368 if (failed) 4369 return false; 4370 4371 _bfd_elf_assign_file_positions_for_relocs (abfd); 4372 4373 /* After writing the headers, we need to write the sections too... */ 4374 num_sec = elf_numsections (abfd); 4375 for (count = 1; count < num_sec; count++) 4376 { 4377 if (bed->elf_backend_section_processing) 4378 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); 4379 if (i_shdrp[count]->contents) 4380 { 4381 bfd_size_type amt = i_shdrp[count]->sh_size; 4382 4383 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 4384 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) 4385 return false; 4386 } 4387 if (count == SHN_LORESERVE - 1) 4388 count += SHN_HIRESERVE + 1 - SHN_LORESERVE; 4389 } 4390 4391 /* Write out the section header names. */ 4392 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 4393 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd))) 4394 return false; 4395 4396 if (bed->elf_backend_final_write_processing) 4397 (*bed->elf_backend_final_write_processing) (abfd, 4398 elf_tdata (abfd)->linker); 4399 4400 return bed->s->write_shdrs_and_ehdr (abfd); 4401} 4402 4403boolean 4404_bfd_elf_write_corefile_contents (abfd) 4405 bfd *abfd; 4406{ 4407 /* Hopefully this can be done just like an object file. */ 4408 return _bfd_elf_write_object_contents (abfd); 4409} 4410 4411/* Given a section, search the header to find them. */ 4412 4413int 4414_bfd_elf_section_from_bfd_section (abfd, asect) 4415 bfd *abfd; 4416 struct sec *asect; 4417{ 4418 struct elf_backend_data *bed; 4419 int index; 4420 4421 if (elf_section_data (asect) != NULL 4422 && elf_section_data (asect)->this_idx != 0) 4423 return elf_section_data (asect)->this_idx; 4424 4425 if (bfd_is_abs_section (asect)) 4426 index = SHN_ABS; 4427 else if (bfd_is_com_section (asect)) 4428 index = SHN_COMMON; 4429 else if (bfd_is_und_section (asect)) 4430 index = SHN_UNDEF; 4431 else 4432 { 4433 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd); 4434 int maxindex = elf_numsections (abfd); 4435 4436 for (index = 1; index < maxindex; index++) 4437 { 4438 Elf_Internal_Shdr *hdr = i_shdrp[index]; 4439 4440 if (hdr != NULL && hdr->bfd_section == asect) 4441 return index; 4442 } 4443 index = -1; 4444 } 4445 4446 bed = get_elf_backend_data (abfd); 4447 if (bed->elf_backend_section_from_bfd_section) 4448 { 4449 int retval = index; 4450 4451 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) 4452 return retval; 4453 } 4454 4455 if (index == -1) 4456 bfd_set_error (bfd_error_nonrepresentable_section); 4457 4458 return index; 4459} 4460 4461/* Given a BFD symbol, return the index in the ELF symbol table, or -1 4462 on error. */ 4463 4464int 4465_bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr) 4466 bfd *abfd; 4467 asymbol **asym_ptr_ptr; 4468{ 4469 asymbol *asym_ptr = *asym_ptr_ptr; 4470 int idx; 4471 flagword flags = asym_ptr->flags; 4472 4473 /* When gas creates relocations against local labels, it creates its 4474 own symbol for the section, but does put the symbol into the 4475 symbol chain, so udata is 0. When the linker is generating 4476 relocatable output, this section symbol may be for one of the 4477 input sections rather than the output section. */ 4478 if (asym_ptr->udata.i == 0 4479 && (flags & BSF_SECTION_SYM) 4480 && asym_ptr->section) 4481 { 4482 int indx; 4483 4484 if (asym_ptr->section->output_section != NULL) 4485 indx = asym_ptr->section->output_section->index; 4486 else 4487 indx = asym_ptr->section->index; 4488 if (indx < elf_num_section_syms (abfd) 4489 && elf_section_syms (abfd)[indx] != NULL) 4490 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; 4491 } 4492 4493 idx = asym_ptr->udata.i; 4494 4495 if (idx == 0) 4496 { 4497 /* This case can occur when using --strip-symbol on a symbol 4498 which is used in a relocation entry. */ 4499 (*_bfd_error_handler) 4500 (_("%s: symbol `%s' required but not present"), 4501 bfd_archive_filename (abfd), bfd_asymbol_name (asym_ptr)); 4502 bfd_set_error (bfd_error_no_symbols); 4503 return -1; 4504 } 4505 4506#if DEBUG & 4 4507 { 4508 fprintf (stderr, 4509 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n", 4510 (long) asym_ptr, asym_ptr->name, idx, flags, 4511 elf_symbol_flags (flags)); 4512 fflush (stderr); 4513 } 4514#endif 4515 4516 return idx; 4517} 4518 4519/* Copy private BFD data. This copies any program header information. */ 4520 4521static boolean 4522copy_private_bfd_data (ibfd, obfd) 4523 bfd *ibfd; 4524 bfd *obfd; 4525{ 4526 Elf_Internal_Ehdr * iehdr; 4527 struct elf_segment_map * map; 4528 struct elf_segment_map * map_first; 4529 struct elf_segment_map ** pointer_to_map; 4530 Elf_Internal_Phdr * segment; 4531 asection * section; 4532 unsigned int i; 4533 unsigned int num_segments; 4534 boolean phdr_included = false; 4535 bfd_vma maxpagesize; 4536 struct elf_segment_map * phdr_adjust_seg = NULL; 4537 unsigned int phdr_adjust_num = 0; 4538 struct elf_backend_data * bed; 4539 4540 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 4541 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 4542 return true; 4543 4544 if (elf_tdata (ibfd)->phdr == NULL) 4545 return true; 4546 4547 bed = get_elf_backend_data (ibfd); 4548 iehdr = elf_elfheader (ibfd); 4549 4550 map_first = NULL; 4551 pointer_to_map = &map_first; 4552 4553 num_segments = elf_elfheader (ibfd)->e_phnum; 4554 maxpagesize = get_elf_backend_data (obfd)->maxpagesize; 4555 4556 /* Returns the end address of the segment + 1. */ 4557#define SEGMENT_END(segment, start) \ 4558 (start + (segment->p_memsz > segment->p_filesz \ 4559 ? segment->p_memsz : segment->p_filesz)) 4560 4561 /* Returns true if the given section is contained within 4562 the given segment. VMA addresses are compared. */ 4563#define IS_CONTAINED_BY_VMA(section, segment) \ 4564 (section->vma >= segment->p_vaddr \ 4565 && (section->vma + section->_raw_size \ 4566 <= (SEGMENT_END (segment, segment->p_vaddr)))) 4567 4568 /* Returns true if the given section is contained within 4569 the given segment. LMA addresses are compared. */ 4570#define IS_CONTAINED_BY_LMA(section, segment, base) \ 4571 (section->lma >= base \ 4572 && (section->lma + section->_raw_size \ 4573 <= SEGMENT_END (segment, base))) 4574 4575 /* Returns true if the given section is contained within the 4576 given segment. Filepos addresses are compared in an elf 4577 backend function. */ 4578#define IS_CONTAINED_BY_FILEPOS(sec, seg, bed) \ 4579 (bed->is_contained_by_filepos \ 4580 && (*bed->is_contained_by_filepos) (sec, seg)) 4581 4582 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */ 4583#define IS_COREFILE_NOTE(p, s) \ 4584 (p->p_type == PT_NOTE \ 4585 && bfd_get_format (ibfd) == bfd_core \ 4586 && s->vma == 0 && s->lma == 0 \ 4587 && (bfd_vma) s->filepos >= p->p_offset \ 4588 && ((bfd_vma) s->filepos + s->_raw_size \ 4589 <= p->p_offset + p->p_filesz)) 4590 4591 /* The complicated case when p_vaddr is 0 is to handle the Solaris 4592 linker, which generates a PT_INTERP section with p_vaddr and 4593 p_memsz set to 0. */ 4594#define IS_SOLARIS_PT_INTERP(p, s) \ 4595 (p->p_vaddr == 0 \ 4596 && p->p_paddr == 0 \ 4597 && p->p_memsz == 0 \ 4598 && p->p_filesz > 0 \ 4599 && (s->flags & SEC_HAS_CONTENTS) != 0 \ 4600 && s->_raw_size > 0 \ 4601 && (bfd_vma) s->filepos >= p->p_offset \ 4602 && ((bfd_vma) s->filepos + s->_raw_size \ 4603 <= p->p_offset + p->p_filesz)) 4604 4605 /* Decide if the given section should be included in the given segment. 4606 A section will be included if: 4607 1. It is within the address space of the segment -- we use the LMA 4608 if that is set for the segment and the VMA otherwise, 4609 2. It is an allocated segment, 4610 3. There is an output section associated with it, 4611 4. The section has not already been allocated to a previous segment. */ 4612#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ 4613 ((((segment->p_paddr \ 4614 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ 4615 : IS_CONTAINED_BY_VMA (section, segment)) \ 4616 && (section->flags & SEC_ALLOC) != 0) \ 4617 || IS_COREFILE_NOTE (segment, section) \ 4618 || (IS_CONTAINED_BY_FILEPOS (section, segment, bed) \ 4619 && (section->flags & SEC_ALLOC) == 0)) \ 4620 && section->output_section != NULL \ 4621 && ! section->segment_mark) 4622 4623 /* Returns true iff seg1 starts after the end of seg2. */ 4624#define SEGMENT_AFTER_SEGMENT(seg1, seg2) \ 4625 (seg1->p_vaddr >= SEGMENT_END (seg2, seg2->p_vaddr)) 4626 4627 /* Returns true iff seg1 and seg2 overlap. */ 4628#define SEGMENT_OVERLAPS(seg1, seg2) \ 4629 (!(SEGMENT_AFTER_SEGMENT (seg1, seg2) \ 4630 || SEGMENT_AFTER_SEGMENT (seg2, seg1))) 4631 4632 /* Initialise the segment mark field. */ 4633 for (section = ibfd->sections; section != NULL; section = section->next) 4634 section->segment_mark = false; 4635 4636 /* Scan through the segments specified in the program header 4637 of the input BFD. For this first scan we look for overlaps 4638 in the loadable segments. These can be created by weird 4639 parameters to objcopy. Also, fix some solaris weirdness. */ 4640 for (i = 0, segment = elf_tdata (ibfd)->phdr; 4641 i < num_segments; 4642 i++, segment++) 4643 { 4644 unsigned int j; 4645 Elf_Internal_Phdr *segment2; 4646 4647 if (segment->p_type == PT_INTERP) 4648 for (section = ibfd->sections; section; section = section->next) 4649 if (IS_SOLARIS_PT_INTERP (segment, section)) 4650 { 4651 /* Mininal change so that the normal section to segment 4652 assigment code will work. */ 4653 segment->p_vaddr = section->vma; 4654 break; 4655 } 4656 4657 if (segment->p_type != PT_LOAD) 4658 continue; 4659 4660 /* Determine if this segment overlaps any previous segments. */ 4661 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++) 4662 { 4663 bfd_signed_vma extra_length; 4664 4665 if (segment2->p_type != PT_LOAD 4666 || ! SEGMENT_OVERLAPS (segment, segment2)) 4667 continue; 4668 4669 /* Merge the two segments together. */ 4670 if (segment2->p_vaddr < segment->p_vaddr) 4671 { 4672 /* Extend SEGMENT2 to include SEGMENT and then delete 4673 SEGMENT. */ 4674 extra_length = 4675 SEGMENT_END (segment, segment->p_vaddr) 4676 - SEGMENT_END (segment2, segment2->p_vaddr); 4677 4678 if (extra_length > 0) 4679 { 4680 segment2->p_memsz += extra_length; 4681 segment2->p_filesz += extra_length; 4682 } 4683 4684 segment->p_type = PT_NULL; 4685 4686 /* Since we have deleted P we must restart the outer loop. */ 4687 i = 0; 4688 segment = elf_tdata (ibfd)->phdr; 4689 break; 4690 } 4691 else 4692 { 4693 /* Extend SEGMENT to include SEGMENT2 and then delete 4694 SEGMENT2. */ 4695 extra_length = 4696 SEGMENT_END (segment2, segment2->p_vaddr) 4697 - SEGMENT_END (segment, segment->p_vaddr); 4698 4699 if (extra_length > 0) 4700 { 4701 segment->p_memsz += extra_length; 4702 segment->p_filesz += extra_length; 4703 } 4704 4705 segment2->p_type = PT_NULL; 4706 } 4707 } 4708 } 4709 4710 /* The second scan attempts to assign sections to segments. */ 4711 for (i = 0, segment = elf_tdata (ibfd)->phdr; 4712 i < num_segments; 4713 i ++, segment ++) 4714 { 4715 unsigned int section_count; 4716 asection ** sections; 4717 asection * output_section; 4718 unsigned int isec; 4719 bfd_vma matching_lma; 4720 bfd_vma suggested_lma; 4721 unsigned int j; 4722 bfd_size_type amt; 4723 4724 if (segment->p_type == PT_NULL) 4725 continue; 4726 4727 /* Compute how many sections might be placed into this segment. */ 4728 section_count = 0; 4729 for (section = ibfd->sections; section != NULL; section = section->next) 4730 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) 4731 ++section_count; 4732 4733 /* Allocate a segment map big enough to contain all of the 4734 sections we have selected. */ 4735 amt = sizeof (struct elf_segment_map); 4736 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); 4737 map = (struct elf_segment_map *) bfd_alloc (obfd, amt); 4738 if (map == NULL) 4739 return false; 4740 4741 /* Initialise the fields of the segment map. Default to 4742 using the physical address of the segment in the input BFD. */ 4743 map->next = NULL; 4744 map->p_type = segment->p_type; 4745 map->p_flags = segment->p_flags; 4746 map->p_flags_valid = 1; 4747 map->p_paddr = segment->p_paddr; 4748 map->p_paddr_valid = 1; 4749 4750 /* Determine if this segment contains the ELF file header 4751 and if it contains the program headers themselves. */ 4752 map->includes_filehdr = (segment->p_offset == 0 4753 && segment->p_filesz >= iehdr->e_ehsize); 4754 4755 map->includes_phdrs = 0; 4756 4757 if (! phdr_included || segment->p_type != PT_LOAD) 4758 { 4759 map->includes_phdrs = 4760 (segment->p_offset <= (bfd_vma) iehdr->e_phoff 4761 && (segment->p_offset + segment->p_filesz 4762 >= ((bfd_vma) iehdr->e_phoff 4763 + iehdr->e_phnum * iehdr->e_phentsize))); 4764 4765 if (segment->p_type == PT_LOAD && map->includes_phdrs) 4766 phdr_included = true; 4767 } 4768 4769 if (section_count == 0) 4770 { 4771 /* Special segments, such as the PT_PHDR segment, may contain 4772 no sections, but ordinary, loadable segments should contain 4773 something. They are allowed by the ELF spec however, so only 4774 a warning is produced. */ 4775 if (segment->p_type == PT_LOAD) 4776 (*_bfd_error_handler) 4777 (_("%s: warning: Empty loadable segment detected, is this intentional ?\n"), 4778 bfd_archive_filename (ibfd)); 4779 4780 map->count = 0; 4781 *pointer_to_map = map; 4782 pointer_to_map = &map->next; 4783 4784 continue; 4785 } 4786 4787 /* Now scan the sections in the input BFD again and attempt 4788 to add their corresponding output sections to the segment map. 4789 The problem here is how to handle an output section which has 4790 been moved (ie had its LMA changed). There are four possibilities: 4791 4792 1. None of the sections have been moved. 4793 In this case we can continue to use the segment LMA from the 4794 input BFD. 4795 4796 2. All of the sections have been moved by the same amount. 4797 In this case we can change the segment's LMA to match the LMA 4798 of the first section. 4799 4800 3. Some of the sections have been moved, others have not. 4801 In this case those sections which have not been moved can be 4802 placed in the current segment which will have to have its size, 4803 and possibly its LMA changed, and a new segment or segments will 4804 have to be created to contain the other sections. 4805 4806 4. The sections have been moved, but not be the same amount. 4807 In this case we can change the segment's LMA to match the LMA 4808 of the first section and we will have to create a new segment 4809 or segments to contain the other sections. 4810 4811 In order to save time, we allocate an array to hold the section 4812 pointers that we are interested in. As these sections get assigned 4813 to a segment, they are removed from this array. */ 4814 4815 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here 4816 to work around this long long bug. */ 4817 amt = section_count * sizeof (asection *); 4818 sections = (asection **) bfd_malloc (amt); 4819 if (sections == NULL) 4820 return false; 4821 4822 /* Step One: Scan for segment vs section LMA conflicts. 4823 Also add the sections to the section array allocated above. 4824 Also add the sections to the current segment. In the common 4825 case, where the sections have not been moved, this means that 4826 we have completely filled the segment, and there is nothing 4827 more to do. */ 4828 isec = 0; 4829 matching_lma = 0; 4830 suggested_lma = 0; 4831 4832 for (j = 0, section = ibfd->sections; 4833 section != NULL; 4834 section = section->next) 4835 { 4836 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) 4837 { 4838 output_section = section->output_section; 4839 4840 sections[j ++] = section; 4841 4842 /* The Solaris native linker always sets p_paddr to 0. 4843 We try to catch that case here, and set it to the 4844 correct value. Note - some backends require that 4845 p_paddr be left as zero. */ 4846 if (segment->p_paddr == 0 4847 && segment->p_vaddr != 0 4848 && (! bed->want_p_paddr_set_to_zero) 4849 && isec == 0 4850 && output_section->lma != 0 4851 && (output_section->vma == (segment->p_vaddr 4852 + (map->includes_filehdr 4853 ? iehdr->e_ehsize 4854 : 0) 4855 + (map->includes_phdrs 4856 ? (iehdr->e_phnum 4857 * iehdr->e_phentsize) 4858 : 0)))) 4859 map->p_paddr = segment->p_vaddr; 4860 4861 /* Match up the physical address of the segment with the 4862 LMA address of the output section. */ 4863 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) 4864 || IS_CONTAINED_BY_FILEPOS (section, segment, bed) 4865 || IS_COREFILE_NOTE (segment, section) 4866 || (bed->want_p_paddr_set_to_zero && 4867 IS_CONTAINED_BY_VMA (output_section, segment)) 4868 ) 4869 { 4870 if (matching_lma == 0) 4871 matching_lma = output_section->lma; 4872 4873 /* We assume that if the section fits within the segment 4874 then it does not overlap any other section within that 4875 segment. */ 4876 map->sections[isec ++] = output_section; 4877 } 4878 else if (suggested_lma == 0) 4879 suggested_lma = output_section->lma; 4880 } 4881 } 4882 4883 BFD_ASSERT (j == section_count); 4884 4885 /* Step Two: Adjust the physical address of the current segment, 4886 if necessary. */ 4887 if (isec == section_count) 4888 { 4889 /* All of the sections fitted within the segment as currently 4890 specified. This is the default case. Add the segment to 4891 the list of built segments and carry on to process the next 4892 program header in the input BFD. */ 4893 map->count = section_count; 4894 *pointer_to_map = map; 4895 pointer_to_map = &map->next; 4896 4897 free (sections); 4898 continue; 4899 } 4900 else 4901 { 4902 if (matching_lma != 0) 4903 { 4904 /* At least one section fits inside the current segment. 4905 Keep it, but modify its physical address to match the 4906 LMA of the first section that fitted. */ 4907 map->p_paddr = matching_lma; 4908 } 4909 else 4910 { 4911 /* None of the sections fitted inside the current segment. 4912 Change the current segment's physical address to match 4913 the LMA of the first section. */ 4914 map->p_paddr = suggested_lma; 4915 } 4916 4917 /* Offset the segment physical address from the lma 4918 to allow for space taken up by elf headers. */ 4919 if (map->includes_filehdr) 4920 map->p_paddr -= iehdr->e_ehsize; 4921 4922 if (map->includes_phdrs) 4923 { 4924 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; 4925 4926 /* iehdr->e_phnum is just an estimate of the number 4927 of program headers that we will need. Make a note 4928 here of the number we used and the segment we chose 4929 to hold these headers, so that we can adjust the 4930 offset when we know the correct value. */ 4931 phdr_adjust_num = iehdr->e_phnum; 4932 phdr_adjust_seg = map; 4933 } 4934 } 4935 4936 /* Step Three: Loop over the sections again, this time assigning 4937 those that fit to the current segment and removing them from the 4938 sections array; but making sure not to leave large gaps. Once all 4939 possible sections have been assigned to the current segment it is 4940 added to the list of built segments and if sections still remain 4941 to be assigned, a new segment is constructed before repeating 4942 the loop. */ 4943 isec = 0; 4944 do 4945 { 4946 map->count = 0; 4947 suggested_lma = 0; 4948 4949 /* Fill the current segment with sections that fit. */ 4950 for (j = 0; j < section_count; j++) 4951 { 4952 section = sections[j]; 4953 4954 if (section == NULL) 4955 continue; 4956 4957 output_section = section->output_section; 4958 4959 BFD_ASSERT (output_section != NULL); 4960 4961 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) 4962 || IS_COREFILE_NOTE (segment, section)) 4963 { 4964 if (map->count == 0) 4965 { 4966 /* If the first section in a segment does not start at 4967 the beginning of the segment, then something is 4968 wrong. */ 4969 if (output_section->lma != 4970 (map->p_paddr 4971 + (map->includes_filehdr ? iehdr->e_ehsize : 0) 4972 + (map->includes_phdrs 4973 ? iehdr->e_phnum * iehdr->e_phentsize 4974 : 0))) 4975 abort (); 4976 } 4977 else 4978 { 4979 asection * prev_sec; 4980 4981 prev_sec = map->sections[map->count - 1]; 4982 4983 /* If the gap between the end of the previous section 4984 and the start of this section is more than 4985 maxpagesize then we need to start a new segment. */ 4986 if ((BFD_ALIGN (prev_sec->lma + prev_sec->_raw_size, 4987 maxpagesize) 4988 < BFD_ALIGN (output_section->lma, maxpagesize)) 4989 || ((prev_sec->lma + prev_sec->_raw_size) 4990 > output_section->lma)) 4991 { 4992 if (suggested_lma == 0) 4993 suggested_lma = output_section->lma; 4994 4995 continue; 4996 } 4997 } 4998 4999 map->sections[map->count++] = output_section; 5000 ++isec; 5001 sections[j] = NULL; 5002 section->segment_mark = true; 5003 } 5004 else if (suggested_lma == 0) 5005 suggested_lma = output_section->lma; 5006 } 5007 5008 BFD_ASSERT (map->count > 0); 5009 5010 /* Add the current segment to the list of built segments. */ 5011 *pointer_to_map = map; 5012 pointer_to_map = &map->next; 5013 5014 if (isec < section_count) 5015 { 5016 /* We still have not allocated all of the sections to 5017 segments. Create a new segment here, initialise it 5018 and carry on looping. */ 5019 amt = sizeof (struct elf_segment_map); 5020 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); 5021 map = (struct elf_segment_map *) bfd_alloc (obfd, amt); 5022 if (map == NULL) 5023 return false; 5024 5025 /* Initialise the fields of the segment map. Set the physical 5026 physical address to the LMA of the first section that has 5027 not yet been assigned. */ 5028 map->next = NULL; 5029 map->p_type = segment->p_type; 5030 map->p_flags = segment->p_flags; 5031 map->p_flags_valid = 1; 5032 map->p_paddr = suggested_lma; 5033 map->p_paddr_valid = 1; 5034 map->includes_filehdr = 0; 5035 map->includes_phdrs = 0; 5036 } 5037 } 5038 while (isec < section_count); 5039 5040 free (sections); 5041 } 5042 5043 /* The Solaris linker creates program headers in which all the 5044 p_paddr fields are zero. When we try to objcopy or strip such a 5045 file, we get confused. Check for this case, and if we find it 5046 reset the p_paddr_valid fields. */ 5047 for (map = map_first; map != NULL; map = map->next) 5048 if (map->p_paddr != 0) 5049 break; 5050 if (map == NULL) 5051 { 5052 for (map = map_first; map != NULL; map = map->next) 5053 map->p_paddr_valid = 0; 5054 } 5055 5056 elf_tdata (obfd)->segment_map = map_first; 5057 5058 /* If we had to estimate the number of program headers that were 5059 going to be needed, then check our estimate now and adjust 5060 the offset if necessary. */ 5061 if (phdr_adjust_seg != NULL) 5062 { 5063 unsigned int count; 5064 5065 for (count = 0, map = map_first; map != NULL; map = map->next) 5066 count++; 5067 5068 if (count > phdr_adjust_num) 5069 phdr_adjust_seg->p_paddr 5070 -= (count - phdr_adjust_num) * iehdr->e_phentsize; 5071 } 5072 5073#if 0 5074 /* Final Step: Sort the segments into ascending order of physical 5075 address. */ 5076 if (map_first != NULL) 5077 { 5078 struct elf_segment_map *prev; 5079 5080 prev = map_first; 5081 for (map = map_first->next; map != NULL; prev = map, map = map->next) 5082 { 5083 /* Yes I know - its a bubble sort.... */ 5084 if (map->next != NULL && (map->next->p_paddr < map->p_paddr)) 5085 { 5086 /* Swap map and map->next. */ 5087 prev->next = map->next; 5088 map->next = map->next->next; 5089 prev->next->next = map; 5090 5091 /* Restart loop. */ 5092 map = map_first; 5093 } 5094 } 5095 } 5096#endif 5097 5098#undef SEGMENT_END 5099#undef IS_CONTAINED_BY_VMA 5100#undef IS_CONTAINED_BY_LMA 5101#undef IS_CONTAINED_BY_FILEPOS 5102#undef IS_COREFILE_NOTE 5103#undef IS_SOLARIS_PT_INTERP 5104#undef INCLUDE_SECTION_IN_SEGMENT 5105#undef SEGMENT_AFTER_SEGMENT 5106#undef SEGMENT_OVERLAPS 5107 return true; 5108} 5109 5110/* Copy private section information. This copies over the entsize 5111 field, and sometimes the info field. */ 5112 5113boolean 5114_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec) 5115 bfd *ibfd; 5116 asection *isec; 5117 bfd *obfd; 5118 asection *osec; 5119{ 5120 Elf_Internal_Shdr *ihdr, *ohdr; 5121 const struct elf_backend_data *bed = get_elf_backend_data (ibfd); 5122 5123 if (ibfd->xvec->flavour != bfd_target_elf_flavour 5124 || obfd->xvec->flavour != bfd_target_elf_flavour) 5125 return true; 5126 5127 /* Copy over private BFD data if it has not already been copied. 5128 This must be done here, rather than in the copy_private_bfd_data 5129 entry point, because the latter is called after the section 5130 contents have been set, which means that the program headers have 5131 already been worked out. The backend function provides a way to 5132 override the test conditions and code path for the call to 5133 copy_private_bfd_data. */ 5134 if (bed->copy_private_bfd_data_p) 5135 { 5136 if ((*bed->copy_private_bfd_data_p) (ibfd, isec, obfd, osec)) 5137 if (! copy_private_bfd_data (ibfd, obfd)) 5138 return false; 5139 } 5140 else if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL) 5141 { 5142 asection *s; 5143 5144 /* Only set up the segments if there are no more SEC_ALLOC 5145 sections. FIXME: This won't do the right thing if objcopy is 5146 used to remove the last SEC_ALLOC section, since objcopy 5147 won't call this routine in that case. */ 5148 for (s = isec->next; s != NULL; s = s->next) 5149 if ((s->flags & SEC_ALLOC) != 0) 5150 break; 5151 if (s == NULL) 5152 { 5153 if (! copy_private_bfd_data (ibfd, obfd)) 5154 return false; 5155 } 5156 } 5157 5158 ihdr = &elf_section_data (isec)->this_hdr; 5159 ohdr = &elf_section_data (osec)->this_hdr; 5160 5161 ohdr->sh_entsize = ihdr->sh_entsize; 5162 5163 if (ihdr->sh_type == SHT_SYMTAB 5164 || ihdr->sh_type == SHT_DYNSYM 5165 || ihdr->sh_type == SHT_GNU_verneed 5166 || ihdr->sh_type == SHT_GNU_verdef) 5167 ohdr->sh_info = ihdr->sh_info; 5168 5169 /* Set things up for objcopy. The output SHT_GROUP section will 5170 have its elf_next_in_group pointing back to the input group 5171 members. */ 5172 elf_next_in_group (osec) = elf_next_in_group (isec); 5173 elf_group_name (osec) = elf_group_name (isec); 5174 5175 elf_section_data (osec)->use_rela_p 5176 = elf_section_data (isec)->use_rela_p; 5177 5178 return true; 5179} 5180 5181/* Copy private symbol information. If this symbol is in a section 5182 which we did not map into a BFD section, try to map the section 5183 index correctly. We use special macro definitions for the mapped 5184 section indices; these definitions are interpreted by the 5185 swap_out_syms function. */ 5186 5187#define MAP_ONESYMTAB (SHN_HIOS + 1) 5188#define MAP_DYNSYMTAB (SHN_HIOS + 2) 5189#define MAP_STRTAB (SHN_HIOS + 3) 5190#define MAP_SHSTRTAB (SHN_HIOS + 4) 5191#define MAP_SYM_SHNDX (SHN_HIOS + 5) 5192 5193boolean 5194_bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg) 5195 bfd *ibfd; 5196 asymbol *isymarg; 5197 bfd *obfd; 5198 asymbol *osymarg; 5199{ 5200 elf_symbol_type *isym, *osym; 5201 5202 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 5203 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 5204 return true; 5205 5206 isym = elf_symbol_from (ibfd, isymarg); 5207 osym = elf_symbol_from (obfd, osymarg); 5208 5209 if (isym != NULL 5210 && osym != NULL 5211 && bfd_is_abs_section (isym->symbol.section)) 5212 { 5213 unsigned int shndx; 5214 5215 shndx = isym->internal_elf_sym.st_shndx; 5216 if (shndx == elf_onesymtab (ibfd)) 5217 shndx = MAP_ONESYMTAB; 5218 else if (shndx == elf_dynsymtab (ibfd)) 5219 shndx = MAP_DYNSYMTAB; 5220 else if (shndx == elf_tdata (ibfd)->strtab_section) 5221 shndx = MAP_STRTAB; 5222 else if (shndx == elf_tdata (ibfd)->shstrtab_section) 5223 shndx = MAP_SHSTRTAB; 5224 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section) 5225 shndx = MAP_SYM_SHNDX; 5226 osym->internal_elf_sym.st_shndx = shndx; 5227 } 5228 5229 return true; 5230} 5231 5232/* Swap out the symbols. */ 5233 5234static boolean 5235swap_out_syms (abfd, sttp, relocatable_p) 5236 bfd *abfd; 5237 struct bfd_strtab_hash **sttp; 5238 int relocatable_p; 5239{ 5240 struct elf_backend_data *bed; 5241 int symcount; 5242 asymbol **syms; 5243 struct bfd_strtab_hash *stt; 5244 Elf_Internal_Shdr *symtab_hdr; 5245 Elf_Internal_Shdr *symtab_shndx_hdr; 5246 Elf_Internal_Shdr *symstrtab_hdr; 5247 char *outbound_syms; 5248 char *outbound_shndx; 5249 int idx; 5250 bfd_size_type amt; 5251 5252 if (!elf_map_symbols (abfd)) 5253 return false; 5254 5255 /* Dump out the symtabs. */ 5256 stt = _bfd_elf_stringtab_init (); 5257 if (stt == NULL) 5258 return false; 5259 5260 bed = get_elf_backend_data (abfd); 5261 symcount = bfd_get_symcount (abfd); 5262 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 5263 symtab_hdr->sh_type = SHT_SYMTAB; 5264 symtab_hdr->sh_entsize = bed->s->sizeof_sym; 5265 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); 5266 symtab_hdr->sh_info = elf_num_locals (abfd) + 1; 5267 symtab_hdr->sh_addralign = bed->s->file_align; 5268 5269 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; 5270 symstrtab_hdr->sh_type = SHT_STRTAB; 5271 5272 amt = (bfd_size_type) (1 + symcount) * bed->s->sizeof_sym; 5273 outbound_syms = bfd_alloc (abfd, amt); 5274 if (outbound_syms == NULL) 5275 return false; 5276 symtab_hdr->contents = (PTR) outbound_syms; 5277 5278 outbound_shndx = NULL; 5279 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; 5280 if (symtab_shndx_hdr->sh_name != 0) 5281 { 5282 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); 5283 outbound_shndx = bfd_zalloc (abfd, amt); 5284 if (outbound_shndx == NULL) 5285 return false; 5286 symtab_shndx_hdr->contents = outbound_shndx; 5287 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 5288 symtab_shndx_hdr->sh_size = amt; 5289 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 5290 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 5291 } 5292 5293 /* now generate the data (for "contents") */ 5294 { 5295 /* Fill in zeroth symbol and swap it out. */ 5296 Elf_Internal_Sym sym; 5297 sym.st_name = 0; 5298 sym.st_value = 0; 5299 sym.st_size = 0; 5300 sym.st_info = 0; 5301 sym.st_other = 0; 5302 sym.st_shndx = SHN_UNDEF; 5303 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); 5304 outbound_syms += bed->s->sizeof_sym; 5305 if (outbound_shndx != NULL) 5306 outbound_shndx += sizeof (Elf_External_Sym_Shndx); 5307 } 5308 5309 syms = bfd_get_outsymbols (abfd); 5310 for (idx = 0; idx < symcount; idx++) 5311 { 5312 Elf_Internal_Sym sym; 5313 bfd_vma value = syms[idx]->value; 5314 elf_symbol_type *type_ptr; 5315 flagword flags = syms[idx]->flags; 5316 int type; 5317 5318 if ((flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) 5319 { 5320 /* Local section symbols have no name. */ 5321 sym.st_name = 0; 5322 } 5323 else 5324 { 5325 sym.st_name = (unsigned long) _bfd_stringtab_add (stt, 5326 syms[idx]->name, 5327 true, false); 5328 if (sym.st_name == (unsigned long) -1) 5329 return false; 5330 } 5331 5332 type_ptr = elf_symbol_from (abfd, syms[idx]); 5333 5334 if ((flags & BSF_SECTION_SYM) == 0 5335 && bfd_is_com_section (syms[idx]->section)) 5336 { 5337 /* ELF common symbols put the alignment into the `value' field, 5338 and the size into the `size' field. This is backwards from 5339 how BFD handles it, so reverse it here. */ 5340 sym.st_size = value; 5341 if (type_ptr == NULL 5342 || type_ptr->internal_elf_sym.st_value == 0) 5343 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); 5344 else 5345 sym.st_value = type_ptr->internal_elf_sym.st_value; 5346 sym.st_shndx = _bfd_elf_section_from_bfd_section 5347 (abfd, syms[idx]->section); 5348 } 5349 else 5350 { 5351 asection *sec = syms[idx]->section; 5352 int shndx; 5353 5354 if (sec->output_section) 5355 { 5356 value += sec->output_offset; 5357 sec = sec->output_section; 5358 } 5359 /* Don't add in the section vma for relocatable output. */ 5360 if (! relocatable_p) 5361 value += sec->vma; 5362 sym.st_value = value; 5363 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; 5364 5365 if (bfd_is_abs_section (sec) 5366 && type_ptr != NULL 5367 && type_ptr->internal_elf_sym.st_shndx != 0) 5368 { 5369 /* This symbol is in a real ELF section which we did 5370 not create as a BFD section. Undo the mapping done 5371 by copy_private_symbol_data. */ 5372 shndx = type_ptr->internal_elf_sym.st_shndx; 5373 switch (shndx) 5374 { 5375 case MAP_ONESYMTAB: 5376 shndx = elf_onesymtab (abfd); 5377 break; 5378 case MAP_DYNSYMTAB: 5379 shndx = elf_dynsymtab (abfd); 5380 break; 5381 case MAP_STRTAB: 5382 shndx = elf_tdata (abfd)->strtab_section; 5383 break; 5384 case MAP_SHSTRTAB: 5385 shndx = elf_tdata (abfd)->shstrtab_section; 5386 break; 5387 case MAP_SYM_SHNDX: 5388 shndx = elf_tdata (abfd)->symtab_shndx_section; 5389 break; 5390 default: 5391 break; 5392 } 5393 } 5394 else 5395 { 5396 shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 5397 5398 if (shndx == -1) 5399 { 5400 asection *sec2; 5401 5402 /* Writing this would be a hell of a lot easier if 5403 we had some decent documentation on bfd, and 5404 knew what to expect of the library, and what to 5405 demand of applications. For example, it 5406 appears that `objcopy' might not set the 5407 section of a symbol to be a section that is 5408 actually in the output file. */ 5409 sec2 = bfd_get_section_by_name (abfd, sec->name); 5410 BFD_ASSERT (sec2 != 0); 5411 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); 5412 BFD_ASSERT (shndx != -1); 5413 } 5414 } 5415 5416 sym.st_shndx = shndx; 5417 } 5418 5419 if ((flags & BSF_THREAD_LOCAL) != 0) 5420 type = STT_TLS; 5421 else if ((flags & BSF_FUNCTION) != 0) 5422 type = STT_FUNC; 5423 else if ((flags & BSF_OBJECT) != 0) 5424 type = STT_OBJECT; 5425 else 5426 type = STT_NOTYPE; 5427 5428 if (syms[idx]->section->flags & SEC_THREAD_LOCAL) 5429 type = STT_TLS; 5430 5431 /* Processor-specific types */ 5432 if (type_ptr != NULL 5433 && bed->elf_backend_get_symbol_type) 5434 type = ((*bed->elf_backend_get_symbol_type) 5435 (&type_ptr->internal_elf_sym, type)); 5436 5437 if (flags & BSF_SECTION_SYM) 5438 { 5439 if (flags & BSF_GLOBAL) 5440 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); 5441 else 5442 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 5443 } 5444 else if (bfd_is_com_section (syms[idx]->section)) 5445 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); 5446 else if (bfd_is_und_section (syms[idx]->section)) 5447 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) 5448 ? STB_WEAK 5449 : STB_GLOBAL), 5450 type); 5451 else if (flags & BSF_FILE) 5452 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 5453 else 5454 { 5455 int bind = STB_LOCAL; 5456 5457 if (flags & BSF_LOCAL) 5458 bind = STB_LOCAL; 5459 else if (flags & BSF_WEAK) 5460 bind = STB_WEAK; 5461 else if (flags & BSF_GLOBAL) 5462 bind = STB_GLOBAL; 5463 5464 sym.st_info = ELF_ST_INFO (bind, type); 5465 } 5466 5467 if (type_ptr != NULL) 5468 sym.st_other = type_ptr->internal_elf_sym.st_other; 5469 else 5470 sym.st_other = 0; 5471 5472 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); 5473 outbound_syms += bed->s->sizeof_sym; 5474 if (outbound_shndx != NULL) 5475 outbound_shndx += sizeof (Elf_External_Sym_Shndx); 5476 } 5477 5478 *sttp = stt; 5479 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); 5480 symstrtab_hdr->sh_type = SHT_STRTAB; 5481 5482 symstrtab_hdr->sh_flags = 0; 5483 symstrtab_hdr->sh_addr = 0; 5484 symstrtab_hdr->sh_entsize = 0; 5485 symstrtab_hdr->sh_link = 0; 5486 symstrtab_hdr->sh_info = 0; 5487 symstrtab_hdr->sh_addralign = 1; 5488 5489 return true; 5490} 5491 5492/* Return the number of bytes required to hold the symtab vector. 5493 5494 Note that we base it on the count plus 1, since we will null terminate 5495 the vector allocated based on this size. However, the ELF symbol table 5496 always has a dummy entry as symbol #0, so it ends up even. */ 5497 5498long 5499_bfd_elf_get_symtab_upper_bound (abfd) 5500 bfd *abfd; 5501{ 5502 long symcount; 5503 long symtab_size; 5504 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; 5505 5506 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 5507 symtab_size = (symcount + 1) * (sizeof (asymbol *)); 5508 if (symcount > 0) 5509 symtab_size -= sizeof (asymbol *); 5510 5511 return symtab_size; 5512} 5513 5514long 5515_bfd_elf_get_dynamic_symtab_upper_bound (abfd) 5516 bfd *abfd; 5517{ 5518 long symcount; 5519 long symtab_size; 5520 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; 5521 5522 if (elf_dynsymtab (abfd) == 0) 5523 { 5524 bfd_set_error (bfd_error_invalid_operation); 5525 return -1; 5526 } 5527 5528 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 5529 symtab_size = (symcount + 1) * (sizeof (asymbol *)); 5530 if (symcount > 0) 5531 symtab_size -= sizeof (asymbol *); 5532 5533 return symtab_size; 5534} 5535 5536long 5537_bfd_elf_get_reloc_upper_bound (abfd, asect) 5538 bfd *abfd ATTRIBUTE_UNUSED; 5539 sec_ptr asect; 5540{ 5541 return (asect->reloc_count + 1) * sizeof (arelent *); 5542} 5543 5544/* Canonicalize the relocs. */ 5545 5546long 5547_bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols) 5548 bfd *abfd; 5549 sec_ptr section; 5550 arelent **relptr; 5551 asymbol **symbols; 5552{ 5553 arelent *tblptr; 5554 unsigned int i; 5555 struct elf_backend_data *bed = get_elf_backend_data (abfd); 5556 5557 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false)) 5558 return -1; 5559 5560 tblptr = section->relocation; 5561 for (i = 0; i < section->reloc_count; i++) 5562 *relptr++ = tblptr++; 5563 5564 *relptr = NULL; 5565 5566 return section->reloc_count; 5567} 5568 5569long 5570_bfd_elf_get_symtab (abfd, alocation) 5571 bfd *abfd; 5572 asymbol **alocation; 5573{ 5574 struct elf_backend_data *bed = get_elf_backend_data (abfd); 5575 long symcount = bed->s->slurp_symbol_table (abfd, alocation, false); 5576 5577 if (symcount >= 0) 5578 bfd_get_symcount (abfd) = symcount; 5579 return symcount; 5580} 5581 5582long 5583_bfd_elf_canonicalize_dynamic_symtab (abfd, alocation) 5584 bfd *abfd; 5585 asymbol **alocation; 5586{ 5587 struct elf_backend_data *bed = get_elf_backend_data (abfd); 5588 long symcount = bed->s->slurp_symbol_table (abfd, alocation, true); 5589 5590 if (symcount >= 0) 5591 bfd_get_dynamic_symcount (abfd) = symcount; 5592 return symcount; 5593} 5594 5595/* Return the size required for the dynamic reloc entries. Any 5596 section that was actually installed in the BFD, and has type 5597 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is 5598 considered to be a dynamic reloc section. */ 5599 5600long 5601_bfd_elf_get_dynamic_reloc_upper_bound (abfd) 5602 bfd *abfd; 5603{ 5604 long ret; 5605 asection *s; 5606 5607 if (elf_dynsymtab (abfd) == 0) 5608 { 5609 bfd_set_error (bfd_error_invalid_operation); 5610 return -1; 5611 } 5612 5613 ret = sizeof (arelent *); 5614 for (s = abfd->sections; s != NULL; s = s->next) 5615 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 5616 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 5617 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) 5618 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize) 5619 * sizeof (arelent *)); 5620 5621 return ret; 5622} 5623 5624/* Canonicalize the dynamic relocation entries. Note that we return 5625 the dynamic relocations as a single block, although they are 5626 actually associated with particular sections; the interface, which 5627 was designed for SunOS style shared libraries, expects that there 5628 is only one set of dynamic relocs. Any section that was actually 5629 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses 5630 the dynamic symbol table, is considered to be a dynamic reloc 5631 section. */ 5632 5633long 5634_bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms) 5635 bfd *abfd; 5636 arelent **storage; 5637 asymbol **syms; 5638{ 5639 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean)); 5640 asection *s; 5641 long ret; 5642 5643 if (elf_dynsymtab (abfd) == 0) 5644 { 5645 bfd_set_error (bfd_error_invalid_operation); 5646 return -1; 5647 } 5648 5649 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; 5650 ret = 0; 5651 for (s = abfd->sections; s != NULL; s = s->next) 5652 { 5653 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 5654 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 5655 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) 5656 { 5657 arelent *p; 5658 long count, i; 5659 5660 if (! (*slurp_relocs) (abfd, s, syms, true)) 5661 return -1; 5662 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize; 5663 p = s->relocation; 5664 for (i = 0; i < count; i++) 5665 *storage++ = p++; 5666 ret += count; 5667 } 5668 } 5669 5670 *storage = NULL; 5671 5672 return ret; 5673} 5674 5675/* Read in the version information. */ 5676 5677boolean 5678_bfd_elf_slurp_version_tables (abfd) 5679 bfd *abfd; 5680{ 5681 bfd_byte *contents = NULL; 5682 bfd_size_type amt; 5683 5684 if (elf_dynverdef (abfd) != 0) 5685 { 5686 Elf_Internal_Shdr *hdr; 5687 Elf_External_Verdef *everdef; 5688 Elf_Internal_Verdef *iverdef; 5689 Elf_Internal_Verdef *iverdefarr; 5690 Elf_Internal_Verdef iverdefmem; 5691 unsigned int i; 5692 unsigned int maxidx; 5693 5694 hdr = &elf_tdata (abfd)->dynverdef_hdr; 5695 5696 contents = (bfd_byte *) bfd_malloc (hdr->sh_size); 5697 if (contents == NULL) 5698 goto error_return; 5699 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 5700 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size) 5701 goto error_return; 5702 5703 /* We know the number of entries in the section but not the maximum 5704 index. Therefore we have to run through all entries and find 5705 the maximum. */ 5706 everdef = (Elf_External_Verdef *) contents; 5707 maxidx = 0; 5708 for (i = 0; i < hdr->sh_info; ++i) 5709 { 5710 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 5711 5712 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) 5713 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); 5714 5715 everdef = ((Elf_External_Verdef *) 5716 ((bfd_byte *) everdef + iverdefmem.vd_next)); 5717 } 5718 5719 amt = (bfd_size_type) maxidx * sizeof (Elf_Internal_Verdef); 5720 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); 5721 if (elf_tdata (abfd)->verdef == NULL) 5722 goto error_return; 5723 5724 elf_tdata (abfd)->cverdefs = maxidx; 5725 5726 everdef = (Elf_External_Verdef *) contents; 5727 iverdefarr = elf_tdata (abfd)->verdef; 5728 for (i = 0; i < hdr->sh_info; i++) 5729 { 5730 Elf_External_Verdaux *everdaux; 5731 Elf_Internal_Verdaux *iverdaux; 5732 unsigned int j; 5733 5734 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 5735 5736 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; 5737 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); 5738 5739 iverdef->vd_bfd = abfd; 5740 5741 amt = (bfd_size_type) iverdef->vd_cnt * sizeof (Elf_Internal_Verdaux); 5742 iverdef->vd_auxptr = (Elf_Internal_Verdaux *) bfd_alloc (abfd, amt); 5743 if (iverdef->vd_auxptr == NULL) 5744 goto error_return; 5745 5746 everdaux = ((Elf_External_Verdaux *) 5747 ((bfd_byte *) everdef + iverdef->vd_aux)); 5748 iverdaux = iverdef->vd_auxptr; 5749 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) 5750 { 5751 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); 5752 5753 iverdaux->vda_nodename = 5754 bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 5755 iverdaux->vda_name); 5756 if (iverdaux->vda_nodename == NULL) 5757 goto error_return; 5758 5759 if (j + 1 < iverdef->vd_cnt) 5760 iverdaux->vda_nextptr = iverdaux + 1; 5761 else 5762 iverdaux->vda_nextptr = NULL; 5763 5764 everdaux = ((Elf_External_Verdaux *) 5765 ((bfd_byte *) everdaux + iverdaux->vda_next)); 5766 } 5767 5768 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; 5769 5770 if (i + 1 < hdr->sh_info) 5771 iverdef->vd_nextdef = iverdef + 1; 5772 else 5773 iverdef->vd_nextdef = NULL; 5774 5775 everdef = ((Elf_External_Verdef *) 5776 ((bfd_byte *) everdef + iverdef->vd_next)); 5777 } 5778 5779 free (contents); 5780 contents = NULL; 5781 } 5782 5783 if (elf_dynverref (abfd) != 0) 5784 { 5785 Elf_Internal_Shdr *hdr; 5786 Elf_External_Verneed *everneed; 5787 Elf_Internal_Verneed *iverneed; 5788 unsigned int i; 5789 5790 hdr = &elf_tdata (abfd)->dynverref_hdr; 5791 5792 amt = (bfd_size_type) hdr->sh_info * sizeof (Elf_Internal_Verneed); 5793 elf_tdata (abfd)->verref = 5794 (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt); 5795 if (elf_tdata (abfd)->verref == NULL) 5796 goto error_return; 5797 5798 elf_tdata (abfd)->cverrefs = hdr->sh_info; 5799 5800 contents = (bfd_byte *) bfd_malloc (hdr->sh_size); 5801 if (contents == NULL) 5802 goto error_return; 5803 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 5804 || bfd_bread ((PTR) contents, hdr->sh_size, abfd) != hdr->sh_size) 5805 goto error_return; 5806 5807 everneed = (Elf_External_Verneed *) contents; 5808 iverneed = elf_tdata (abfd)->verref; 5809 for (i = 0; i < hdr->sh_info; i++, iverneed++) 5810 { 5811 Elf_External_Vernaux *evernaux; 5812 Elf_Internal_Vernaux *ivernaux; 5813 unsigned int j; 5814 5815 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); 5816 5817 iverneed->vn_bfd = abfd; 5818 5819 iverneed->vn_filename = 5820 bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 5821 iverneed->vn_file); 5822 if (iverneed->vn_filename == NULL) 5823 goto error_return; 5824 5825 amt = iverneed->vn_cnt; 5826 amt *= sizeof (Elf_Internal_Vernaux); 5827 iverneed->vn_auxptr = (Elf_Internal_Vernaux *) bfd_alloc (abfd, amt); 5828 5829 evernaux = ((Elf_External_Vernaux *) 5830 ((bfd_byte *) everneed + iverneed->vn_aux)); 5831 ivernaux = iverneed->vn_auxptr; 5832 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) 5833 { 5834 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); 5835 5836 ivernaux->vna_nodename = 5837 bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 5838 ivernaux->vna_name); 5839 if (ivernaux->vna_nodename == NULL) 5840 goto error_return; 5841 5842 if (j + 1 < iverneed->vn_cnt) 5843 ivernaux->vna_nextptr = ivernaux + 1; 5844 else 5845 ivernaux->vna_nextptr = NULL; 5846 5847 evernaux = ((Elf_External_Vernaux *) 5848 ((bfd_byte *) evernaux + ivernaux->vna_next)); 5849 } 5850 5851 if (i + 1 < hdr->sh_info) 5852 iverneed->vn_nextref = iverneed + 1; 5853 else 5854 iverneed->vn_nextref = NULL; 5855 5856 everneed = ((Elf_External_Verneed *) 5857 ((bfd_byte *) everneed + iverneed->vn_next)); 5858 } 5859 5860 free (contents); 5861 contents = NULL; 5862 } 5863 5864 return true; 5865 5866 error_return: 5867 if (contents == NULL) 5868 free (contents); 5869 return false; 5870} 5871 5872asymbol * 5873_bfd_elf_make_empty_symbol (abfd) 5874 bfd *abfd; 5875{ 5876 elf_symbol_type *newsym; 5877 bfd_size_type amt = sizeof (elf_symbol_type); 5878 5879 newsym = (elf_symbol_type *) bfd_zalloc (abfd, amt); 5880 if (!newsym) 5881 return NULL; 5882 else 5883 { 5884 newsym->symbol.the_bfd = abfd; 5885 return &newsym->symbol; 5886 } 5887} 5888 5889void 5890_bfd_elf_get_symbol_info (ignore_abfd, symbol, ret) 5891 bfd *ignore_abfd ATTRIBUTE_UNUSED; 5892 asymbol *symbol; 5893 symbol_info *ret; 5894{ 5895 bfd_symbol_info (symbol, ret); 5896} 5897 5898/* Return whether a symbol name implies a local symbol. Most targets 5899 use this function for the is_local_label_name entry point, but some 5900 override it. */ 5901 5902boolean 5903_bfd_elf_is_local_label_name (abfd, name) 5904 bfd *abfd ATTRIBUTE_UNUSED; 5905 const char *name; 5906{ 5907 /* Normal local symbols start with ``.L''. */ 5908 if (name[0] == '.' && name[1] == 'L') 5909 return true; 5910 5911 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate 5912 DWARF debugging symbols starting with ``..''. */ 5913 if (name[0] == '.' && name[1] == '.') 5914 return true; 5915 5916 /* gcc will sometimes generate symbols beginning with ``_.L_'' when 5917 emitting DWARF debugging output. I suspect this is actually a 5918 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call 5919 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading 5920 underscore to be emitted on some ELF targets). For ease of use, 5921 we treat such symbols as local. */ 5922 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') 5923 return true; 5924 5925 return false; 5926} 5927 5928alent * 5929_bfd_elf_get_lineno (ignore_abfd, symbol) 5930 bfd *ignore_abfd ATTRIBUTE_UNUSED; 5931 asymbol *symbol ATTRIBUTE_UNUSED; 5932{ 5933 abort (); 5934 return NULL; 5935} 5936 5937boolean 5938_bfd_elf_set_arch_mach (abfd, arch, machine) 5939 bfd *abfd; 5940 enum bfd_architecture arch; 5941 unsigned long machine; 5942{ 5943 /* If this isn't the right architecture for this backend, and this 5944 isn't the generic backend, fail. */ 5945 if (arch != get_elf_backend_data (abfd)->arch 5946 && arch != bfd_arch_unknown 5947 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) 5948 return false; 5949 5950 return bfd_default_set_arch_mach (abfd, arch, machine); 5951} 5952 5953/* Find the function to a particular section and offset, 5954 for error reporting. */ 5955 5956static boolean 5957elf_find_function (abfd, section, symbols, offset, 5958 filename_ptr, functionname_ptr) 5959 bfd *abfd ATTRIBUTE_UNUSED; 5960 asection *section; 5961 asymbol **symbols; 5962 bfd_vma offset; 5963 const char **filename_ptr; 5964 const char **functionname_ptr; 5965{ 5966 const char *filename; 5967 asymbol *func; 5968 bfd_vma low_func; 5969 asymbol **p; 5970 5971 filename = NULL; 5972 func = NULL; 5973 low_func = 0; 5974 5975 for (p = symbols; *p != NULL; p++) 5976 { 5977 elf_symbol_type *q; 5978 5979 q = (elf_symbol_type *) *p; 5980 5981 if (bfd_get_section (&q->symbol) != section) 5982 continue; 5983 5984 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info)) 5985 { 5986 default: 5987 break; 5988 case STT_FILE: 5989 filename = bfd_asymbol_name (&q->symbol); 5990 break; 5991 case STT_NOTYPE: 5992 case STT_FUNC: 5993 if (q->symbol.section == section 5994 && q->symbol.value >= low_func 5995 && q->symbol.value <= offset) 5996 { 5997 func = (asymbol *) q; 5998 low_func = q->symbol.value; 5999 } 6000 break; 6001 } 6002 } 6003 6004 if (func == NULL) 6005 return false; 6006 6007 if (filename_ptr) 6008 *filename_ptr = filename; 6009 if (functionname_ptr) 6010 *functionname_ptr = bfd_asymbol_name (func); 6011 6012 return true; 6013} 6014 6015/* Find the nearest line to a particular section and offset, 6016 for error reporting. */ 6017 6018boolean 6019_bfd_elf_find_nearest_line (abfd, section, symbols, offset, 6020 filename_ptr, functionname_ptr, line_ptr) 6021 bfd *abfd; 6022 asection *section; 6023 asymbol **symbols; 6024 bfd_vma offset; 6025 const char **filename_ptr; 6026 const char **functionname_ptr; 6027 unsigned int *line_ptr; 6028{ 6029 boolean found; 6030 6031 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset, 6032 filename_ptr, functionname_ptr, 6033 line_ptr)) 6034 { 6035 if (!*functionname_ptr) 6036 elf_find_function (abfd, section, symbols, offset, 6037 *filename_ptr ? NULL : filename_ptr, 6038 functionname_ptr); 6039 6040 return true; 6041 } 6042 6043 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, 6044 filename_ptr, functionname_ptr, 6045 line_ptr, 0, 6046 &elf_tdata (abfd)->dwarf2_find_line_info)) 6047 { 6048 if (!*functionname_ptr) 6049 elf_find_function (abfd, section, symbols, offset, 6050 *filename_ptr ? NULL : filename_ptr, 6051 functionname_ptr); 6052 6053 return true; 6054 } 6055 6056 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, 6057 &found, filename_ptr, 6058 functionname_ptr, line_ptr, 6059 &elf_tdata (abfd)->line_info)) 6060 return false; 6061 if (found && (*functionname_ptr || *line_ptr)) 6062 return true; 6063 6064 if (symbols == NULL) 6065 return false; 6066 6067 if (! elf_find_function (abfd, section, symbols, offset, 6068 filename_ptr, functionname_ptr)) 6069 return false; 6070 6071 *line_ptr = 0; 6072 return true; 6073} 6074 6075int 6076_bfd_elf_sizeof_headers (abfd, reloc) 6077 bfd *abfd; 6078 boolean reloc; 6079{ 6080 int ret; 6081 6082 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr; 6083 if (! reloc) 6084 ret += get_program_header_size (abfd); 6085 return ret; 6086} 6087 6088boolean 6089_bfd_elf_set_section_contents (abfd, section, location, offset, count) 6090 bfd *abfd; 6091 sec_ptr section; 6092 PTR location; 6093 file_ptr offset; 6094 bfd_size_type count; 6095{ 6096 Elf_Internal_Shdr *hdr; 6097 bfd_signed_vma pos; 6098 6099 if (! abfd->output_has_begun 6100 && ! (_bfd_elf_compute_section_file_positions 6101 (abfd, (struct bfd_link_info *) NULL))) 6102 return false; 6103 6104 hdr = &elf_section_data (section)->this_hdr; 6105 pos = hdr->sh_offset + offset; 6106 if (bfd_seek (abfd, pos, SEEK_SET) != 0 6107 || bfd_bwrite (location, count, abfd) != count) 6108 return false; 6109 6110 return true; 6111} 6112 6113void 6114_bfd_elf_no_info_to_howto (abfd, cache_ptr, dst) 6115 bfd *abfd ATTRIBUTE_UNUSED; 6116 arelent *cache_ptr ATTRIBUTE_UNUSED; 6117 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED; 6118{ 6119 abort (); 6120} 6121 6122#if 0 6123void 6124_bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst) 6125 bfd *abfd; 6126 arelent *cache_ptr; 6127 Elf_Internal_Rel *dst; 6128{ 6129 abort (); 6130} 6131#endif 6132 6133/* Try to convert a non-ELF reloc into an ELF one. */ 6134 6135boolean 6136_bfd_elf_validate_reloc (abfd, areloc) 6137 bfd *abfd; 6138 arelent *areloc; 6139{ 6140 /* Check whether we really have an ELF howto. */ 6141 6142 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) 6143 { 6144 bfd_reloc_code_real_type code; 6145 reloc_howto_type *howto; 6146 6147 /* Alien reloc: Try to determine its type to replace it with an 6148 equivalent ELF reloc. */ 6149 6150 if (areloc->howto->pc_relative) 6151 { 6152 switch (areloc->howto->bitsize) 6153 { 6154 case 8: 6155 code = BFD_RELOC_8_PCREL; 6156 break; 6157 case 12: 6158 code = BFD_RELOC_12_PCREL; 6159 break; 6160 case 16: 6161 code = BFD_RELOC_16_PCREL; 6162 break; 6163 case 24: 6164 code = BFD_RELOC_24_PCREL; 6165 break; 6166 case 32: 6167 code = BFD_RELOC_32_PCREL; 6168 break; 6169 case 64: 6170 code = BFD_RELOC_64_PCREL; 6171 break; 6172 default: 6173 goto fail; 6174 } 6175 6176 howto = bfd_reloc_type_lookup (abfd, code); 6177 6178 if (areloc->howto->pcrel_offset != howto->pcrel_offset) 6179 { 6180 if (howto->pcrel_offset) 6181 areloc->addend += areloc->address; 6182 else 6183 areloc->addend -= areloc->address; /* addend is unsigned!! */ 6184 } 6185 } 6186 else 6187 { 6188 switch (areloc->howto->bitsize) 6189 { 6190 case 8: 6191 code = BFD_RELOC_8; 6192 break; 6193 case 14: 6194 code = BFD_RELOC_14; 6195 break; 6196 case 16: 6197 code = BFD_RELOC_16; 6198 break; 6199 case 26: 6200 code = BFD_RELOC_26; 6201 break; 6202 case 32: 6203 code = BFD_RELOC_32; 6204 break; 6205 case 64: 6206 code = BFD_RELOC_64; 6207 break; 6208 default: 6209 goto fail; 6210 } 6211 6212 howto = bfd_reloc_type_lookup (abfd, code); 6213 } 6214 6215 if (howto) 6216 areloc->howto = howto; 6217 else 6218 goto fail; 6219 } 6220 6221 return true; 6222 6223 fail: 6224 (*_bfd_error_handler) 6225 (_("%s: unsupported relocation type %s"), 6226 bfd_archive_filename (abfd), areloc->howto->name); 6227 bfd_set_error (bfd_error_bad_value); 6228 return false; 6229} 6230 6231boolean 6232_bfd_elf_close_and_cleanup (abfd) 6233 bfd *abfd; 6234{ 6235 if (bfd_get_format (abfd) == bfd_object) 6236 { 6237 if (elf_shstrtab (abfd) != NULL) 6238 _bfd_elf_strtab_free (elf_shstrtab (abfd)); 6239 } 6240 6241 return _bfd_generic_close_and_cleanup (abfd); 6242} 6243 6244/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY 6245 in the relocation's offset. Thus we cannot allow any sort of sanity 6246 range-checking to interfere. There is nothing else to do in processing 6247 this reloc. */ 6248 6249bfd_reloc_status_type 6250_bfd_elf_rel_vtable_reloc_fn (abfd, re, symbol, data, is, obfd, errmsg) 6251 bfd *abfd ATTRIBUTE_UNUSED; 6252 arelent *re ATTRIBUTE_UNUSED; 6253 struct symbol_cache_entry *symbol ATTRIBUTE_UNUSED; 6254 PTR data ATTRIBUTE_UNUSED; 6255 asection *is ATTRIBUTE_UNUSED; 6256 bfd *obfd ATTRIBUTE_UNUSED; 6257 char **errmsg ATTRIBUTE_UNUSED; 6258{ 6259 return bfd_reloc_ok; 6260} 6261 6262/* Elf core file support. Much of this only works on native 6263 toolchains, since we rely on knowing the 6264 machine-dependent procfs structure in order to pick 6265 out details about the corefile. */ 6266 6267#ifdef HAVE_SYS_PROCFS_H 6268# include <sys/procfs.h> 6269#endif 6270 6271/* FIXME: this is kinda wrong, but it's what gdb wants. */ 6272 6273static int 6274elfcore_make_pid (abfd) 6275 bfd *abfd; 6276{ 6277 return ((elf_tdata (abfd)->core_lwpid << 16) 6278 + (elf_tdata (abfd)->core_pid)); 6279} 6280 6281/* If there isn't a section called NAME, make one, using 6282 data from SECT. Note, this function will generate a 6283 reference to NAME, so you shouldn't deallocate or 6284 overwrite it. */ 6285 6286static boolean 6287elfcore_maybe_make_sect (abfd, name, sect) 6288 bfd *abfd; 6289 char *name; 6290 asection *sect; 6291{ 6292 asection *sect2; 6293 6294 if (bfd_get_section_by_name (abfd, name) != NULL) 6295 return true; 6296 6297 sect2 = bfd_make_section (abfd, name); 6298 if (sect2 == NULL) 6299 return false; 6300 6301 sect2->_raw_size = sect->_raw_size; 6302 sect2->filepos = sect->filepos; 6303 sect2->flags = sect->flags; 6304 sect2->alignment_power = sect->alignment_power; 6305 return true; 6306} 6307 6308/* Create a pseudosection containing SIZE bytes at FILEPOS. This 6309 actually creates up to two pseudosections: 6310 - For the single-threaded case, a section named NAME, unless 6311 such a section already exists. 6312 - For the multi-threaded case, a section named "NAME/PID", where 6313 PID is elfcore_make_pid (abfd). 6314 Both pseudosections have identical contents. */ 6315boolean 6316_bfd_elfcore_make_pseudosection (abfd, name, size, filepos) 6317 bfd *abfd; 6318 char *name; 6319 size_t size; 6320 ufile_ptr filepos; 6321{ 6322 char buf[100]; 6323 char *threaded_name; 6324 size_t len; 6325 asection *sect; 6326 6327 /* Build the section name. */ 6328 6329 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); 6330 len = strlen (buf) + 1; 6331 threaded_name = bfd_alloc (abfd, (bfd_size_type) len); 6332 if (threaded_name == NULL) 6333 return false; 6334 memcpy (threaded_name, buf, len); 6335 6336 sect = bfd_make_section (abfd, threaded_name); 6337 if (sect == NULL) 6338 return false; 6339 sect->_raw_size = size; 6340 sect->filepos = filepos; 6341 sect->flags = SEC_HAS_CONTENTS; 6342 sect->alignment_power = 2; 6343 6344 return elfcore_maybe_make_sect (abfd, name, sect); 6345} 6346 6347/* prstatus_t exists on: 6348 solaris 2.5+ 6349 linux 2.[01] + glibc 6350 unixware 4.2 6351*/ 6352 6353#if defined (HAVE_PRSTATUS_T) 6354static boolean elfcore_grok_prstatus PARAMS ((bfd *, Elf_Internal_Note *)); 6355 6356static boolean 6357elfcore_grok_prstatus (abfd, note) 6358 bfd *abfd; 6359 Elf_Internal_Note *note; 6360{ 6361 size_t raw_size; 6362 int offset; 6363 6364 if (note->descsz == sizeof (prstatus_t)) 6365 { 6366 prstatus_t prstat; 6367 6368 raw_size = sizeof (prstat.pr_reg); 6369 offset = offsetof (prstatus_t, pr_reg); 6370 memcpy (&prstat, note->descdata, sizeof (prstat)); 6371 6372 /* Do not overwrite the core signal if it 6373 has already been set by another thread. */ 6374 if (elf_tdata (abfd)->core_signal == 0) 6375 elf_tdata (abfd)->core_signal = prstat.pr_cursig; 6376 elf_tdata (abfd)->core_pid = prstat.pr_pid; 6377 6378 /* pr_who exists on: 6379 solaris 2.5+ 6380 unixware 4.2 6381 pr_who doesn't exist on: 6382 linux 2.[01] 6383 */ 6384#if defined (HAVE_PRSTATUS_T_PR_WHO) 6385 elf_tdata (abfd)->core_lwpid = prstat.pr_who; 6386#endif 6387 } 6388#if defined (HAVE_PRSTATUS32_T) 6389 else if (note->descsz == sizeof (prstatus32_t)) 6390 { 6391 /* 64-bit host, 32-bit corefile */ 6392 prstatus32_t prstat; 6393 6394 raw_size = sizeof (prstat.pr_reg); 6395 offset = offsetof (prstatus32_t, pr_reg); 6396 memcpy (&prstat, note->descdata, sizeof (prstat)); 6397 6398 /* Do not overwrite the core signal if it 6399 has already been set by another thread. */ 6400 if (elf_tdata (abfd)->core_signal == 0) 6401 elf_tdata (abfd)->core_signal = prstat.pr_cursig; 6402 elf_tdata (abfd)->core_pid = prstat.pr_pid; 6403 6404 /* pr_who exists on: 6405 solaris 2.5+ 6406 unixware 4.2 6407 pr_who doesn't exist on: 6408 linux 2.[01] 6409 */ 6410#if defined (HAVE_PRSTATUS32_T_PR_WHO) 6411 elf_tdata (abfd)->core_lwpid = prstat.pr_who; 6412#endif 6413 } 6414#endif /* HAVE_PRSTATUS32_T */ 6415 else 6416 { 6417 /* Fail - we don't know how to handle any other 6418 note size (ie. data object type). */ 6419 return true; 6420 } 6421 6422 /* Make a ".reg/999" section and a ".reg" section. */ 6423 return _bfd_elfcore_make_pseudosection (abfd, ".reg", 6424 raw_size, note->descpos + offset); 6425} 6426#endif /* defined (HAVE_PRSTATUS_T) */ 6427 6428/* Create a pseudosection containing the exact contents of NOTE. */ 6429static boolean 6430elfcore_make_note_pseudosection (abfd, name, note) 6431 bfd *abfd; 6432 char *name; 6433 Elf_Internal_Note *note; 6434{ 6435 return _bfd_elfcore_make_pseudosection (abfd, name, 6436 note->descsz, note->descpos); 6437} 6438 6439/* There isn't a consistent prfpregset_t across platforms, 6440 but it doesn't matter, because we don't have to pick this 6441 data structure apart. */ 6442 6443static boolean 6444elfcore_grok_prfpreg (abfd, note) 6445 bfd *abfd; 6446 Elf_Internal_Note *note; 6447{ 6448 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 6449} 6450 6451/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note 6452 type of 5 (NT_PRXFPREG). Just include the whole note's contents 6453 literally. */ 6454 6455static boolean 6456elfcore_grok_prxfpreg (abfd, note) 6457 bfd *abfd; 6458 Elf_Internal_Note *note; 6459{ 6460 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); 6461} 6462 6463#if defined (HAVE_PRPSINFO_T) 6464typedef prpsinfo_t elfcore_psinfo_t; 6465#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ 6466typedef prpsinfo32_t elfcore_psinfo32_t; 6467#endif 6468#endif 6469 6470#if defined (HAVE_PSINFO_T) 6471typedef psinfo_t elfcore_psinfo_t; 6472#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ 6473typedef psinfo32_t elfcore_psinfo32_t; 6474#endif 6475#endif 6476 6477/* return a malloc'ed copy of a string at START which is at 6478 most MAX bytes long, possibly without a terminating '\0'. 6479 the copy will always have a terminating '\0'. */ 6480 6481char * 6482_bfd_elfcore_strndup (abfd, start, max) 6483 bfd *abfd; 6484 char *start; 6485 size_t max; 6486{ 6487 char *dups; 6488 char *end = memchr (start, '\0', max); 6489 size_t len; 6490 6491 if (end == NULL) 6492 len = max; 6493 else 6494 len = end - start; 6495 6496 dups = bfd_alloc (abfd, (bfd_size_type) len + 1); 6497 if (dups == NULL) 6498 return NULL; 6499 6500 memcpy (dups, start, len); 6501 dups[len] = '\0'; 6502 6503 return dups; 6504} 6505 6506#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 6507static boolean elfcore_grok_psinfo PARAMS ((bfd *, Elf_Internal_Note *)); 6508 6509static boolean 6510elfcore_grok_psinfo (abfd, note) 6511 bfd *abfd; 6512 Elf_Internal_Note *note; 6513{ 6514 if (note->descsz == sizeof (elfcore_psinfo_t)) 6515 { 6516 elfcore_psinfo_t psinfo; 6517 6518 memcpy (&psinfo, note->descdata, sizeof (psinfo)); 6519 6520 elf_tdata (abfd)->core_program 6521 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, 6522 sizeof (psinfo.pr_fname)); 6523 6524 elf_tdata (abfd)->core_command 6525 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, 6526 sizeof (psinfo.pr_psargs)); 6527 } 6528#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) 6529 else if (note->descsz == sizeof (elfcore_psinfo32_t)) 6530 { 6531 /* 64-bit host, 32-bit corefile */ 6532 elfcore_psinfo32_t psinfo; 6533 6534 memcpy (&psinfo, note->descdata, sizeof (psinfo)); 6535 6536 elf_tdata (abfd)->core_program 6537 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, 6538 sizeof (psinfo.pr_fname)); 6539 6540 elf_tdata (abfd)->core_command 6541 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, 6542 sizeof (psinfo.pr_psargs)); 6543 } 6544#endif 6545 6546 else 6547 { 6548 /* Fail - we don't know how to handle any other 6549 note size (ie. data object type). */ 6550 return true; 6551 } 6552 6553 /* Note that for some reason, a spurious space is tacked 6554 onto the end of the args in some (at least one anyway) 6555 implementations, so strip it off if it exists. */ 6556 6557 { 6558 char *command = elf_tdata (abfd)->core_command; 6559 int n = strlen (command); 6560 6561 if (0 < n && command[n - 1] == ' ') 6562 command[n - 1] = '\0'; 6563 } 6564 6565 return true; 6566} 6567#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ 6568 6569#if defined (HAVE_PSTATUS_T) 6570static boolean elfcore_grok_pstatus PARAMS ((bfd *, Elf_Internal_Note *)); 6571 6572static boolean 6573elfcore_grok_pstatus (abfd, note) 6574 bfd *abfd; 6575 Elf_Internal_Note *note; 6576{ 6577 if (note->descsz == sizeof (pstatus_t) 6578#if defined (HAVE_PXSTATUS_T) 6579 || note->descsz == sizeof (pxstatus_t) 6580#endif 6581 ) 6582 { 6583 pstatus_t pstat; 6584 6585 memcpy (&pstat, note->descdata, sizeof (pstat)); 6586 6587 elf_tdata (abfd)->core_pid = pstat.pr_pid; 6588 } 6589#if defined (HAVE_PSTATUS32_T) 6590 else if (note->descsz == sizeof (pstatus32_t)) 6591 { 6592 /* 64-bit host, 32-bit corefile */ 6593 pstatus32_t pstat; 6594 6595 memcpy (&pstat, note->descdata, sizeof (pstat)); 6596 6597 elf_tdata (abfd)->core_pid = pstat.pr_pid; 6598 } 6599#endif 6600 /* Could grab some more details from the "representative" 6601 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an 6602 NT_LWPSTATUS note, presumably. */ 6603 6604 return true; 6605} 6606#endif /* defined (HAVE_PSTATUS_T) */ 6607 6608#if defined (HAVE_LWPSTATUS_T) 6609static boolean elfcore_grok_lwpstatus PARAMS ((bfd *, Elf_Internal_Note *)); 6610 6611static boolean 6612elfcore_grok_lwpstatus (abfd, note) 6613 bfd *abfd; 6614 Elf_Internal_Note *note; 6615{ 6616 lwpstatus_t lwpstat; 6617 char buf[100]; 6618 char *name; 6619 size_t len; 6620 asection *sect; 6621 6622 if (note->descsz != sizeof (lwpstat) 6623#if defined (HAVE_LWPXSTATUS_T) 6624 && note->descsz != sizeof (lwpxstatus_t) 6625#endif 6626 ) 6627 return true; 6628 6629 memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); 6630 6631 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid; 6632 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig; 6633 6634 /* Make a ".reg/999" section. */ 6635 6636 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); 6637 len = strlen (buf) + 1; 6638 name = bfd_alloc (abfd, (bfd_size_type) len); 6639 if (name == NULL) 6640 return false; 6641 memcpy (name, buf, len); 6642 6643 sect = bfd_make_section (abfd, name); 6644 if (sect == NULL) 6645 return false; 6646 6647#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 6648 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); 6649 sect->filepos = note->descpos 6650 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); 6651#endif 6652 6653#if defined (HAVE_LWPSTATUS_T_PR_REG) 6654 sect->_raw_size = sizeof (lwpstat.pr_reg); 6655 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); 6656#endif 6657 6658 sect->flags = SEC_HAS_CONTENTS; 6659 sect->alignment_power = 2; 6660 6661 if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) 6662 return false; 6663 6664 /* Make a ".reg2/999" section */ 6665 6666 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); 6667 len = strlen (buf) + 1; 6668 name = bfd_alloc (abfd, (bfd_size_type) len); 6669 if (name == NULL) 6670 return false; 6671 memcpy (name, buf, len); 6672 6673 sect = bfd_make_section (abfd, name); 6674 if (sect == NULL) 6675 return false; 6676 6677#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 6678 sect->_raw_size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); 6679 sect->filepos = note->descpos 6680 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); 6681#endif 6682 6683#if defined (HAVE_LWPSTATUS_T_PR_FPREG) 6684 sect->_raw_size = sizeof (lwpstat.pr_fpreg); 6685 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); 6686#endif 6687 6688 sect->flags = SEC_HAS_CONTENTS; 6689 sect->alignment_power = 2; 6690 6691 return elfcore_maybe_make_sect (abfd, ".reg2", sect); 6692} 6693#endif /* defined (HAVE_LWPSTATUS_T) */ 6694 6695#if defined (HAVE_WIN32_PSTATUS_T) 6696static boolean 6697elfcore_grok_win32pstatus (abfd, note) 6698 bfd *abfd; 6699 Elf_Internal_Note *note; 6700{ 6701 char buf[30]; 6702 char *name; 6703 size_t len; 6704 asection *sect; 6705 win32_pstatus_t pstatus; 6706 6707 if (note->descsz < sizeof (pstatus)) 6708 return true; 6709 6710 memcpy (&pstatus, note->descdata, sizeof (pstatus)); 6711 6712 switch (pstatus.data_type) 6713 { 6714 case NOTE_INFO_PROCESS: 6715 /* FIXME: need to add ->core_command. */ 6716 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal; 6717 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid; 6718 break; 6719 6720 case NOTE_INFO_THREAD: 6721 /* Make a ".reg/999" section. */ 6722 sprintf (buf, ".reg/%d", pstatus.data.thread_info.tid); 6723 6724 len = strlen (buf) + 1; 6725 name = bfd_alloc (abfd, (bfd_size_type) len); 6726 if (name == NULL) 6727 return false; 6728 6729 memcpy (name, buf, len); 6730 6731 sect = bfd_make_section (abfd, name); 6732 if (sect == NULL) 6733 return false; 6734 6735 sect->_raw_size = sizeof (pstatus.data.thread_info.thread_context); 6736 sect->filepos = (note->descpos 6737 + offsetof (struct win32_pstatus, 6738 data.thread_info.thread_context)); 6739 sect->flags = SEC_HAS_CONTENTS; 6740 sect->alignment_power = 2; 6741 6742 if (pstatus.data.thread_info.is_active_thread) 6743 if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) 6744 return false; 6745 break; 6746 6747 case NOTE_INFO_MODULE: 6748 /* Make a ".module/xxxxxxxx" section. */ 6749 sprintf (buf, ".module/%08x", pstatus.data.module_info.base_address); 6750 6751 len = strlen (buf) + 1; 6752 name = bfd_alloc (abfd, (bfd_size_type) len); 6753 if (name == NULL) 6754 return false; 6755 6756 memcpy (name, buf, len); 6757 6758 sect = bfd_make_section (abfd, name); 6759 6760 if (sect == NULL) 6761 return false; 6762 6763 sect->_raw_size = note->descsz; 6764 sect->filepos = note->descpos; 6765 sect->flags = SEC_HAS_CONTENTS; 6766 sect->alignment_power = 2; 6767 break; 6768 6769 default: 6770 return true; 6771 } 6772 6773 return true; 6774} 6775#endif /* HAVE_WIN32_PSTATUS_T */ 6776 6777static boolean 6778elfcore_grok_note (abfd, note) 6779 bfd *abfd; 6780 Elf_Internal_Note *note; 6781{ 6782 struct elf_backend_data *bed = get_elf_backend_data (abfd); 6783 6784 switch (note->type) 6785 { 6786 default: 6787 return true; 6788 6789 case NT_PRSTATUS: 6790 if (bed->elf_backend_grok_prstatus) 6791 if ((*bed->elf_backend_grok_prstatus) (abfd, note)) 6792 return true; 6793#if defined (HAVE_PRSTATUS_T) 6794 return elfcore_grok_prstatus (abfd, note); 6795#else 6796 return true; 6797#endif 6798 6799#if defined (HAVE_PSTATUS_T) 6800 case NT_PSTATUS: 6801 return elfcore_grok_pstatus (abfd, note); 6802#endif 6803 6804#if defined (HAVE_LWPSTATUS_T) 6805 case NT_LWPSTATUS: 6806 return elfcore_grok_lwpstatus (abfd, note); 6807#endif 6808 6809 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ 6810 return elfcore_grok_prfpreg (abfd, note); 6811 6812#if defined (HAVE_WIN32_PSTATUS_T) 6813 case NT_WIN32PSTATUS: 6814 return elfcore_grok_win32pstatus (abfd, note); 6815#endif 6816 6817 case NT_PRXFPREG: /* Linux SSE extension */ 6818 if (note->namesz == 5 6819 && ! strcmp (note->namedata, "LINUX")) 6820 return elfcore_grok_prxfpreg (abfd, note); 6821 else 6822 return true; 6823 6824 case NT_PRPSINFO: 6825 case NT_PSINFO: 6826 if (bed->elf_backend_grok_psinfo) 6827 if ((*bed->elf_backend_grok_psinfo) (abfd, note)) 6828 return true; 6829#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 6830 return elfcore_grok_psinfo (abfd, note); 6831#else 6832 return true; 6833#endif 6834 } 6835} 6836 6837static boolean 6838elfcore_netbsd_get_lwpid (note, lwpidp) 6839 Elf_Internal_Note *note; 6840 int *lwpidp; 6841{ 6842 char *cp; 6843 6844 cp = strchr (note->namedata, '@'); 6845 if (cp != NULL) 6846 { 6847 *lwpidp = atoi(cp + 1); 6848 return true; 6849 } 6850 return false; 6851} 6852 6853static boolean 6854elfcore_grok_netbsd_procinfo (abfd, note) 6855 bfd *abfd; 6856 Elf_Internal_Note *note; 6857{ 6858 6859 /* Signal number at offset 0x08. */ 6860 elf_tdata (abfd)->core_signal 6861 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); 6862 6863 /* Process ID at offset 0x50. */ 6864 elf_tdata (abfd)->core_pid 6865 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); 6866 6867 /* Command name at 0x7c (max 32 bytes, including nul). */ 6868 elf_tdata (abfd)->core_command 6869 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); 6870 6871 return true; 6872} 6873 6874static boolean 6875elfcore_grok_netbsd_note (abfd, note) 6876 bfd *abfd; 6877 Elf_Internal_Note *note; 6878{ 6879 int lwp; 6880 6881 if (elfcore_netbsd_get_lwpid (note, &lwp)) 6882 elf_tdata (abfd)->core_lwpid = lwp; 6883 6884 if (note->type == NT_NETBSDCORE_PROCINFO) 6885 { 6886 /* NetBSD-specific core "procinfo". Note that we expect to 6887 find this note before any of the others, which is fine, 6888 since the kernel writes this note out first when it 6889 creates a core file. */ 6890 6891 return elfcore_grok_netbsd_procinfo (abfd, note); 6892 } 6893 6894 /* As of Jan 2002 there are no other machine-independent notes 6895 defined for NetBSD core files. If the note type is less 6896 than the start of the machine-dependent note types, we don't 6897 understand it. */ 6898 6899 if (note->type < NT_NETBSDCORE_FIRSTMACH) 6900 return true; 6901 6902 6903 switch (bfd_get_arch (abfd)) 6904 { 6905 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and 6906 PT_GETFPREGS == mach+2. */ 6907 6908 case bfd_arch_alpha: 6909 case bfd_arch_sparc: 6910 switch (note->type) 6911 { 6912 case NT_NETBSDCORE_FIRSTMACH+0: 6913 return elfcore_make_note_pseudosection (abfd, ".reg", note); 6914 6915 case NT_NETBSDCORE_FIRSTMACH+2: 6916 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 6917 6918 default: 6919 return true; 6920 } 6921 6922 /* On all other arch's, PT_GETREGS == mach+1 and 6923 PT_GETFPREGS == mach+3. */ 6924 6925 default: 6926 switch (note->type) 6927 { 6928 case NT_NETBSDCORE_FIRSTMACH+1: 6929 return elfcore_make_note_pseudosection (abfd, ".reg", note); 6930 6931 case NT_NETBSDCORE_FIRSTMACH+3: 6932 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 6933 6934 default: 6935 return true; 6936 } 6937 } 6938 /* NOTREACHED */ 6939} 6940 6941/* Function: elfcore_write_note 6942 6943 Inputs: 6944 buffer to hold note 6945 name of note 6946 type of note 6947 data for note 6948 size of data for note 6949 6950 Return: 6951 End of buffer containing note. */ 6952 6953char * 6954elfcore_write_note (abfd, buf, bufsiz, name, type, input, size) 6955 bfd *abfd; 6956 char *buf; 6957 int *bufsiz; 6958 const char *name; 6959 int type; 6960 const PTR input; 6961 int size; 6962{ 6963 Elf_External_Note *xnp; 6964 size_t namesz; 6965 size_t pad; 6966 size_t newspace; 6967 char *p, *dest; 6968 6969 namesz = 0; 6970 pad = 0; 6971 if (name != NULL) 6972 { 6973 struct elf_backend_data *bed; 6974 6975 namesz = strlen (name) + 1; 6976 bed = get_elf_backend_data (abfd); 6977 pad = -namesz & (bed->s->file_align - 1); 6978 } 6979 6980 newspace = sizeof (Elf_External_Note) - 1 + namesz + pad + size; 6981 6982 p = realloc (buf, *bufsiz + newspace); 6983 dest = p + *bufsiz; 6984 *bufsiz += newspace; 6985 xnp = (Elf_External_Note *) dest; 6986 H_PUT_32 (abfd, namesz, xnp->namesz); 6987 H_PUT_32 (abfd, size, xnp->descsz); 6988 H_PUT_32 (abfd, type, xnp->type); 6989 dest = xnp->name; 6990 if (name != NULL) 6991 { 6992 memcpy (dest, name, namesz); 6993 dest += namesz; 6994 while (pad != 0) 6995 { 6996 *dest++ = '\0'; 6997 --pad; 6998 } 6999 } 7000 memcpy (dest, input, size); 7001 return p; 7002} 7003 7004#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 7005char * 7006elfcore_write_prpsinfo (abfd, buf, bufsiz, fname, psargs) 7007 bfd *abfd; 7008 char *buf; 7009 int *bufsiz; 7010 const char *fname; 7011 const char *psargs; 7012{ 7013 int note_type; 7014 char *note_name = "CORE"; 7015 7016#if defined (HAVE_PSINFO_T) 7017 psinfo_t data; 7018 note_type = NT_PSINFO; 7019#else 7020 prpsinfo_t data; 7021 note_type = NT_PRPSINFO; 7022#endif 7023 7024 memset (&data, 0, sizeof (data)); 7025 strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); 7026 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); 7027 return elfcore_write_note (abfd, buf, bufsiz, 7028 note_name, note_type, &data, sizeof (data)); 7029} 7030#endif /* PSINFO_T or PRPSINFO_T */ 7031 7032#if defined (HAVE_PRSTATUS_T) 7033char * 7034elfcore_write_prstatus (abfd, buf, bufsiz, pid, cursig, gregs) 7035 bfd *abfd; 7036 char *buf; 7037 int *bufsiz; 7038 long pid; 7039 int cursig; 7040 const PTR gregs; 7041{ 7042 prstatus_t prstat; 7043 char *note_name = "CORE"; 7044 7045 memset (&prstat, 0, sizeof (prstat)); 7046 prstat.pr_pid = pid; 7047 prstat.pr_cursig = cursig; 7048 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); 7049 return elfcore_write_note (abfd, buf, bufsiz, 7050 note_name, NT_PRSTATUS, &prstat, sizeof (prstat)); 7051} 7052#endif /* HAVE_PRSTATUS_T */ 7053 7054#if defined (HAVE_LWPSTATUS_T) 7055char * 7056elfcore_write_lwpstatus (abfd, buf, bufsiz, pid, cursig, gregs) 7057 bfd *abfd; 7058 char *buf; 7059 int *bufsiz; 7060 long pid; 7061 int cursig; 7062 const PTR gregs; 7063{ 7064 lwpstatus_t lwpstat; 7065 char *note_name = "CORE"; 7066 7067 memset (&lwpstat, 0, sizeof (lwpstat)); 7068 lwpstat.pr_lwpid = pid >> 16; 7069 lwpstat.pr_cursig = cursig; 7070#if defined (HAVE_LWPSTATUS_T_PR_REG) 7071 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); 7072#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 7073#if !defined(gregs) 7074 memcpy (lwpstat.pr_context.uc_mcontext.gregs, 7075 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); 7076#else 7077 memcpy (lwpstat.pr_context.uc_mcontext.__gregs, 7078 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); 7079#endif 7080#endif 7081 return elfcore_write_note (abfd, buf, bufsiz, note_name, 7082 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); 7083} 7084#endif /* HAVE_LWPSTATUS_T */ 7085 7086#if defined (HAVE_PSTATUS_T) 7087char * 7088elfcore_write_pstatus (abfd, buf, bufsiz, pid, cursig, gregs) 7089 bfd *abfd; 7090 char *buf; 7091 int *bufsiz; 7092 long pid; 7093 int cursig; 7094 const PTR gregs; 7095{ 7096 pstatus_t pstat; 7097 char *note_name = "CORE"; 7098 7099 memset (&pstat, 0, sizeof (pstat)); 7100 pstat.pr_pid = pid & 0xffff; 7101 buf = elfcore_write_note (abfd, buf, bufsiz, note_name, 7102 NT_PSTATUS, &pstat, sizeof (pstat)); 7103 return buf; 7104} 7105#endif /* HAVE_PSTATUS_T */ 7106 7107char * 7108elfcore_write_prfpreg (abfd, buf, bufsiz, fpregs, size) 7109 bfd *abfd; 7110 char *buf; 7111 int *bufsiz; 7112 const PTR fpregs; 7113 int size; 7114{ 7115 char *note_name = "CORE"; 7116 return elfcore_write_note (abfd, buf, bufsiz, 7117 note_name, NT_FPREGSET, fpregs, size); 7118} 7119 7120char * 7121elfcore_write_prxfpreg (abfd, buf, bufsiz, xfpregs, size) 7122 bfd *abfd; 7123 char *buf; 7124 int *bufsiz; 7125 const PTR xfpregs; 7126 int size; 7127{ 7128 char *note_name = "LINUX"; 7129 return elfcore_write_note (abfd, buf, bufsiz, 7130 note_name, NT_PRXFPREG, xfpregs, size); 7131} 7132 7133static boolean 7134elfcore_read_notes (abfd, offset, size) 7135 bfd *abfd; 7136 file_ptr offset; 7137 bfd_size_type size; 7138{ 7139 char *buf; 7140 char *p; 7141 7142 if (size <= 0) 7143 return true; 7144 7145 if (bfd_seek (abfd, offset, SEEK_SET) != 0) 7146 return false; 7147 7148 buf = bfd_malloc (size); 7149 if (buf == NULL) 7150 return false; 7151 7152 if (bfd_bread (buf, size, abfd) != size) 7153 { 7154 error: 7155 free (buf); 7156 return false; 7157 } 7158 7159 p = buf; 7160 while (p < buf + size) 7161 { 7162 /* FIXME: bad alignment assumption. */ 7163 Elf_External_Note *xnp = (Elf_External_Note *) p; 7164 Elf_Internal_Note in; 7165 7166 in.type = H_GET_32 (abfd, xnp->type); 7167 7168 in.namesz = H_GET_32 (abfd, xnp->namesz); 7169 in.namedata = xnp->name; 7170 7171 in.descsz = H_GET_32 (abfd, xnp->descsz); 7172 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); 7173 in.descpos = offset + (in.descdata - buf); 7174 7175 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0) 7176 { 7177 if (! elfcore_grok_netbsd_note (abfd, &in)) 7178 goto error; 7179 } 7180 else 7181 { 7182 if (! elfcore_grok_note (abfd, &in)) 7183 goto error; 7184 } 7185 7186 p = in.descdata + BFD_ALIGN (in.descsz, 4); 7187 } 7188 7189 free (buf); 7190 return true; 7191} 7192 7193/* Providing external access to the ELF program header table. */ 7194 7195/* Return an upper bound on the number of bytes required to store a 7196 copy of ABFD's program header table entries. Return -1 if an error 7197 occurs; bfd_get_error will return an appropriate code. */ 7198 7199long 7200bfd_get_elf_phdr_upper_bound (abfd) 7201 bfd *abfd; 7202{ 7203 if (abfd->xvec->flavour != bfd_target_elf_flavour) 7204 { 7205 bfd_set_error (bfd_error_wrong_format); 7206 return -1; 7207 } 7208 7209 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); 7210} 7211 7212/* Copy ABFD's program header table entries to *PHDRS. The entries 7213 will be stored as an array of Elf_Internal_Phdr structures, as 7214 defined in include/elf/internal.h. To find out how large the 7215 buffer needs to be, call bfd_get_elf_phdr_upper_bound. 7216 7217 Return the number of program header table entries read, or -1 if an 7218 error occurs; bfd_get_error will return an appropriate code. */ 7219 7220int 7221bfd_get_elf_phdrs (abfd, phdrs) 7222 bfd *abfd; 7223 void *phdrs; 7224{ 7225 int num_phdrs; 7226 7227 if (abfd->xvec->flavour != bfd_target_elf_flavour) 7228 { 7229 bfd_set_error (bfd_error_wrong_format); 7230 return -1; 7231 } 7232 7233 num_phdrs = elf_elfheader (abfd)->e_phnum; 7234 memcpy (phdrs, elf_tdata (abfd)->phdr, 7235 num_phdrs * sizeof (Elf_Internal_Phdr)); 7236 7237 return num_phdrs; 7238} 7239 7240void 7241_bfd_elf_sprintf_vma (abfd, buf, value) 7242 bfd *abfd ATTRIBUTE_UNUSED; 7243 char *buf; 7244 bfd_vma value; 7245{ 7246#ifdef BFD64 7247 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ 7248 7249 i_ehdrp = elf_elfheader (abfd); 7250 if (i_ehdrp == NULL) 7251 sprintf_vma (buf, value); 7252 else 7253 { 7254 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64) 7255 { 7256#if BFD_HOST_64BIT_LONG 7257 sprintf (buf, "%016lx", value); 7258#else 7259 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value), 7260 _bfd_int64_low (value)); 7261#endif 7262 } 7263 else 7264 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff)); 7265 } 7266#else 7267 sprintf_vma (buf, value); 7268#endif 7269} 7270 7271void 7272_bfd_elf_fprintf_vma (abfd, stream, value) 7273 bfd *abfd ATTRIBUTE_UNUSED; 7274 PTR stream; 7275 bfd_vma value; 7276{ 7277#ifdef BFD64 7278 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ 7279 7280 i_ehdrp = elf_elfheader (abfd); 7281 if (i_ehdrp == NULL) 7282 fprintf_vma ((FILE *) stream, value); 7283 else 7284 { 7285 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64) 7286 { 7287#if BFD_HOST_64BIT_LONG 7288 fprintf ((FILE *) stream, "%016lx", value); 7289#else 7290 fprintf ((FILE *) stream, "%08lx%08lx", 7291 _bfd_int64_high (value), _bfd_int64_low (value)); 7292#endif 7293 } 7294 else 7295 fprintf ((FILE *) stream, "%08lx", 7296 (unsigned long) (value & 0xffffffff)); 7297 } 7298#else 7299 fprintf_vma ((FILE *) stream, value); 7300#endif 7301} 7302 7303enum elf_reloc_type_class 7304_bfd_elf_reloc_type_class (rela) 7305 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED; 7306{ 7307 return reloc_class_normal; 7308} 7309 7310/* For RELA architectures, return the relocation value for a 7311 relocation against a local symbol. */ 7312 7313bfd_vma 7314_bfd_elf_rela_local_sym (abfd, sym, sec, rel) 7315 bfd *abfd; 7316 Elf_Internal_Sym *sym; 7317 asection *sec; 7318 Elf_Internal_Rela *rel; 7319{ 7320 bfd_vma relocation; 7321 7322 relocation = (sec->output_section->vma 7323 + sec->output_offset 7324 + sym->st_value); 7325 if ((sec->flags & SEC_MERGE) 7326 && ELF_ST_TYPE (sym->st_info) == STT_SECTION 7327 && elf_section_data (sec)->sec_info_type == ELF_INFO_TYPE_MERGE) 7328 { 7329 asection *msec; 7330 7331 msec = sec; 7332 rel->r_addend = 7333 _bfd_merged_section_offset (abfd, &msec, 7334 elf_section_data (sec)->sec_info, 7335 sym->st_value + rel->r_addend, 7336 (bfd_vma) 0) 7337 - relocation; 7338 rel->r_addend += msec->output_section->vma + msec->output_offset; 7339 } 7340 return relocation; 7341} 7342 7343bfd_vma 7344_bfd_elf_rel_local_sym (abfd, sym, psec, addend) 7345 bfd *abfd; 7346 Elf_Internal_Sym *sym; 7347 asection **psec; 7348 bfd_vma addend; 7349{ 7350 asection *sec = *psec; 7351 7352 if (elf_section_data (sec)->sec_info_type != ELF_INFO_TYPE_MERGE) 7353 return sym->st_value + addend; 7354 7355 return _bfd_merged_section_offset (abfd, psec, 7356 elf_section_data (sec)->sec_info, 7357 sym->st_value + addend, (bfd_vma) 0); 7358} 7359 7360bfd_vma 7361_bfd_elf_section_offset (abfd, info, sec, offset) 7362 bfd *abfd; 7363 struct bfd_link_info *info; 7364 asection *sec; 7365 bfd_vma offset; 7366{ 7367 struct bfd_elf_section_data *sec_data; 7368 7369 sec_data = elf_section_data (sec); 7370 switch (sec_data->sec_info_type) 7371 { 7372 case ELF_INFO_TYPE_STABS: 7373 return _bfd_stab_section_offset 7374 (abfd, &elf_hash_table (info)->merge_info, sec, &sec_data->sec_info, 7375 offset); 7376 case ELF_INFO_TYPE_EH_FRAME: 7377 return _bfd_elf_eh_frame_section_offset (abfd, sec, offset); 7378 default: 7379 return offset; 7380 } 7381} 7382