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