1 /* ELF executable support for BFD. 2 3 Copyright (C) 1993-2024 Free Software Foundation, Inc. 4 5 This file is part of BFD, the Binary File Descriptor library. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 20 MA 02110-1301, USA. */ 21 22 23 /* 24 SECTION 25 ELF backends 26 27 BFD support for ELF formats is being worked on. 28 Currently, the best supported back ends are for sparc and i386 29 (running svr4 or Solaris 2). 30 31 Documentation of the internals of the support code still needs 32 to be written. The code is changing quickly enough that we 33 haven't bothered yet. */ 34 35 /* For sparc64-cross-sparc32. */ 36 #define _SYSCALL32 37 #include "sysdep.h" 38 #include <limits.h> 39 #include "bfd.h" 40 #include "bfdlink.h" 41 #include "libbfd.h" 42 #define ARCH_SIZE 0 43 #include "elf-bfd.h" 44 #include "libiberty.h" 45 #include "safe-ctype.h" 46 #include "elf-linux-core.h" 47 48 #ifdef CORE_HEADER 49 #include CORE_HEADER 50 #endif 51 52 static int elf_sort_sections (const void *, const void *); 53 static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); 54 static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int, 55 struct bfd_link_info *); 56 static bool elf_parse_notes (bfd *abfd, char *buf, size_t size, 57 file_ptr offset, size_t align); 58 59 /* Swap version information in and out. The version information is 60 currently size independent. If that ever changes, this code will 61 need to move into elfcode.h. */ 62 63 /* Swap in a Verdef structure. */ 64 65 void 66 _bfd_elf_swap_verdef_in (bfd *abfd, 67 const Elf_External_Verdef *src, 68 Elf_Internal_Verdef *dst) 69 { 70 dst->vd_version = H_GET_16 (abfd, src->vd_version); 71 dst->vd_flags = H_GET_16 (abfd, src->vd_flags); 72 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); 73 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); 74 dst->vd_hash = H_GET_32 (abfd, src->vd_hash); 75 dst->vd_aux = H_GET_32 (abfd, src->vd_aux); 76 dst->vd_next = H_GET_32 (abfd, src->vd_next); 77 } 78 79 /* Swap out a Verdef structure. */ 80 81 void 82 _bfd_elf_swap_verdef_out (bfd *abfd, 83 const Elf_Internal_Verdef *src, 84 Elf_External_Verdef *dst) 85 { 86 H_PUT_16 (abfd, src->vd_version, dst->vd_version); 87 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); 88 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); 89 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); 90 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); 91 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); 92 H_PUT_32 (abfd, src->vd_next, dst->vd_next); 93 } 94 95 /* Swap in a Verdaux structure. */ 96 97 void 98 _bfd_elf_swap_verdaux_in (bfd *abfd, 99 const Elf_External_Verdaux *src, 100 Elf_Internal_Verdaux *dst) 101 { 102 dst->vda_name = H_GET_32 (abfd, src->vda_name); 103 dst->vda_next = H_GET_32 (abfd, src->vda_next); 104 } 105 106 /* Swap out a Verdaux structure. */ 107 108 void 109 _bfd_elf_swap_verdaux_out (bfd *abfd, 110 const Elf_Internal_Verdaux *src, 111 Elf_External_Verdaux *dst) 112 { 113 H_PUT_32 (abfd, src->vda_name, dst->vda_name); 114 H_PUT_32 (abfd, src->vda_next, dst->vda_next); 115 } 116 117 /* Swap in a Verneed structure. */ 118 119 void 120 _bfd_elf_swap_verneed_in (bfd *abfd, 121 const Elf_External_Verneed *src, 122 Elf_Internal_Verneed *dst) 123 { 124 dst->vn_version = H_GET_16 (abfd, src->vn_version); 125 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); 126 dst->vn_file = H_GET_32 (abfd, src->vn_file); 127 dst->vn_aux = H_GET_32 (abfd, src->vn_aux); 128 dst->vn_next = H_GET_32 (abfd, src->vn_next); 129 } 130 131 /* Swap out a Verneed structure. */ 132 133 void 134 _bfd_elf_swap_verneed_out (bfd *abfd, 135 const Elf_Internal_Verneed *src, 136 Elf_External_Verneed *dst) 137 { 138 H_PUT_16 (abfd, src->vn_version, dst->vn_version); 139 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); 140 H_PUT_32 (abfd, src->vn_file, dst->vn_file); 141 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); 142 H_PUT_32 (abfd, src->vn_next, dst->vn_next); 143 } 144 145 /* Swap in a Vernaux structure. */ 146 147 void 148 _bfd_elf_swap_vernaux_in (bfd *abfd, 149 const Elf_External_Vernaux *src, 150 Elf_Internal_Vernaux *dst) 151 { 152 dst->vna_hash = H_GET_32 (abfd, src->vna_hash); 153 dst->vna_flags = H_GET_16 (abfd, src->vna_flags); 154 dst->vna_other = H_GET_16 (abfd, src->vna_other); 155 dst->vna_name = H_GET_32 (abfd, src->vna_name); 156 dst->vna_next = H_GET_32 (abfd, src->vna_next); 157 } 158 159 /* Swap out a Vernaux structure. */ 160 161 void 162 _bfd_elf_swap_vernaux_out (bfd *abfd, 163 const Elf_Internal_Vernaux *src, 164 Elf_External_Vernaux *dst) 165 { 166 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); 167 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); 168 H_PUT_16 (abfd, src->vna_other, dst->vna_other); 169 H_PUT_32 (abfd, src->vna_name, dst->vna_name); 170 H_PUT_32 (abfd, src->vna_next, dst->vna_next); 171 } 172 173 /* Swap in a Versym structure. */ 174 175 void 176 _bfd_elf_swap_versym_in (bfd *abfd, 177 const Elf_External_Versym *src, 178 Elf_Internal_Versym *dst) 179 { 180 dst->vs_vers = H_GET_16 (abfd, src->vs_vers); 181 } 182 183 /* Swap out a Versym structure. */ 184 185 void 186 _bfd_elf_swap_versym_out (bfd *abfd, 187 const Elf_Internal_Versym *src, 188 Elf_External_Versym *dst) 189 { 190 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); 191 } 192 193 /* Standard ELF hash function. Do not change this function; you will 194 cause invalid hash tables to be generated. */ 195 196 unsigned long 197 bfd_elf_hash (const char *namearg) 198 { 199 uint32_t h = 0; 200 201 for (const unsigned char *name = (const unsigned char *) namearg; 202 *name; name++) 203 { 204 h = (h << 4) + *name; 205 h ^= (h >> 24) & 0xf0; 206 } 207 return h & 0x0fffffff; 208 } 209 210 /* DT_GNU_HASH hash function. Do not change this function; you will 211 cause invalid hash tables to be generated. */ 212 213 unsigned long 214 bfd_elf_gnu_hash (const char *namearg) 215 { 216 uint32_t h = 5381; 217 218 for (const unsigned char *name = (const unsigned char *) namearg; 219 *name; name++) 220 h = (h << 5) + h + *name; 221 return h; 222 } 223 224 /* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with 225 the object_id field of an elf_obj_tdata field set. */ 226 bool 227 bfd_elf_allocate_object (bfd *abfd, 228 size_t object_size) 229 { 230 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); 231 abfd->tdata.any = bfd_zalloc (abfd, object_size); 232 if (abfd->tdata.any == NULL) 233 return false; 234 235 elf_object_id (abfd) = get_elf_backend_data (abfd)->target_id; 236 if (abfd->direction != read_direction) 237 { 238 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); 239 if (o == NULL) 240 return false; 241 elf_tdata (abfd)->o = o; 242 elf_program_header_size (abfd) = (bfd_size_type) -1; 243 } 244 return true; 245 } 246 247 248 bool 249 bfd_elf_make_object (bfd *abfd) 250 { 251 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata)); 252 } 253 254 bool 255 bfd_elf_mkcorefile (bfd *abfd) 256 { 257 /* I think this can be done just like an object file. */ 258 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) 259 return false; 260 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); 261 return elf_tdata (abfd)->core != NULL; 262 } 263 264 char * 265 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) 266 { 267 Elf_Internal_Shdr **i_shdrp; 268 bfd_byte *shstrtab = NULL; 269 file_ptr offset; 270 bfd_size_type shstrtabsize; 271 272 i_shdrp = elf_elfsections (abfd); 273 if (i_shdrp == 0 274 || shindex >= elf_numsections (abfd) 275 || i_shdrp[shindex] == 0) 276 return NULL; 277 278 shstrtab = i_shdrp[shindex]->contents; 279 if (shstrtab == NULL) 280 { 281 /* No cached one, attempt to read, and cache what we read. */ 282 offset = i_shdrp[shindex]->sh_offset; 283 shstrtabsize = i_shdrp[shindex]->sh_size; 284 285 if (shstrtabsize == 0 286 || bfd_seek (abfd, offset, SEEK_SET) != 0 287 || (shstrtab = _bfd_mmap_persistent (abfd, shstrtabsize)) == NULL) 288 { 289 /* Once we've failed to read it, make sure we don't keep 290 trying. Otherwise, we'll keep allocating space for 291 the string table over and over. */ 292 i_shdrp[shindex]->sh_size = 0; 293 } 294 else if (shstrtab[shstrtabsize - 1] != 0) 295 { 296 /* It is an error if a string table isn't terminated. */ 297 _bfd_error_handler 298 /* xgettext:c-format */ 299 (_("%pB: string table [%u] is corrupt"), abfd, shindex); 300 shstrtab[shstrtabsize - 1] = 0; 301 } 302 i_shdrp[shindex]->contents = shstrtab; 303 } 304 return (char *) shstrtab; 305 } 306 307 char * 308 bfd_elf_string_from_elf_section (bfd *abfd, 309 unsigned int shindex, 310 unsigned int strindex) 311 { 312 Elf_Internal_Shdr *hdr; 313 314 if (strindex == 0) 315 return ""; 316 317 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) 318 return NULL; 319 320 hdr = elf_elfsections (abfd)[shindex]; 321 322 if (hdr->contents == NULL) 323 { 324 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) 325 { 326 /* PR 17512: file: f057ec89. */ 327 /* xgettext:c-format */ 328 _bfd_error_handler (_("%pB: attempt to load strings from" 329 " a non-string section (number %d)"), 330 abfd, shindex); 331 return NULL; 332 } 333 334 if (bfd_elf_get_str_section (abfd, shindex) == NULL) 335 return NULL; 336 } 337 else 338 { 339 /* PR 24273: The string section's contents may have already 340 been loaded elsewhere, eg because a corrupt file has the 341 string section index in the ELF header pointing at a group 342 section. So be paranoid, and test that the last byte of 343 the section is zero. */ 344 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0) 345 return NULL; 346 } 347 348 if (strindex >= hdr->sh_size) 349 { 350 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; 351 _bfd_error_handler 352 /* xgettext:c-format */ 353 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"), 354 abfd, strindex, (uint64_t) hdr->sh_size, 355 (shindex == shstrndx && strindex == hdr->sh_name 356 ? ".shstrtab" 357 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); 358 return NULL; 359 } 360 361 return ((char *) hdr->contents) + strindex; 362 } 363 364 /* Read and convert symbols to internal format. 365 SYMCOUNT specifies the number of symbols to read, starting from 366 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF 367 are non-NULL, they are used to store the internal symbols, external 368 symbols, and symbol section index extensions, respectively. 369 Returns a pointer to the internal symbol buffer (malloced if necessary) 370 or NULL if there were no symbols or some kind of problem. */ 371 372 Elf_Internal_Sym * 373 bfd_elf_get_elf_syms (bfd *ibfd, 374 Elf_Internal_Shdr *symtab_hdr, 375 size_t symcount, 376 size_t symoffset, 377 Elf_Internal_Sym *intsym_buf, 378 void *extsym_buf, 379 Elf_External_Sym_Shndx *extshndx_buf) 380 { 381 Elf_Internal_Shdr *shndx_hdr; 382 void *alloc_ext; 383 const bfd_byte *esym; 384 Elf_External_Sym_Shndx *alloc_extshndx; 385 Elf_External_Sym_Shndx *shndx; 386 Elf_Internal_Sym *alloc_intsym; 387 Elf_Internal_Sym *isym; 388 Elf_Internal_Sym *isymend; 389 const struct elf_backend_data *bed; 390 size_t extsym_size; 391 size_t amt; 392 file_ptr pos; 393 394 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 395 abort (); 396 397 if (symcount == 0) 398 return intsym_buf; 399 400 if (elf_use_dt_symtab_p (ibfd)) 401 { 402 /* Use dynamic symbol table. */ 403 if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset) 404 { 405 bfd_set_error (bfd_error_invalid_operation); 406 return NULL; 407 } 408 return elf_tdata (ibfd)->dt_symtab + symoffset; 409 } 410 411 /* Normal syms might have section extension entries. */ 412 shndx_hdr = NULL; 413 if (elf_symtab_shndx_list (ibfd) != NULL) 414 { 415 elf_section_list * entry; 416 Elf_Internal_Shdr **sections = elf_elfsections (ibfd); 417 418 /* Find an index section that is linked to this symtab section. */ 419 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) 420 { 421 /* PR 20063. */ 422 if (entry->hdr.sh_link >= elf_numsections (ibfd)) 423 continue; 424 425 if (sections[entry->hdr.sh_link] == symtab_hdr) 426 { 427 shndx_hdr = & entry->hdr; 428 break; 429 }; 430 } 431 432 if (shndx_hdr == NULL) 433 { 434 if (symtab_hdr == &elf_symtab_hdr (ibfd)) 435 /* Not really accurate, but this was how the old code used 436 to work. */ 437 shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr; 438 /* Otherwise we do nothing. The assumption is that 439 the index table will not be needed. */ 440 } 441 } 442 443 /* Read the symbols. */ 444 alloc_ext = NULL; 445 alloc_extshndx = NULL; 446 alloc_intsym = NULL; 447 bed = get_elf_backend_data (ibfd); 448 extsym_size = bed->s->sizeof_sym; 449 if (_bfd_mul_overflow (symcount, extsym_size, &amt)) 450 { 451 bfd_set_error (bfd_error_file_too_big); 452 return NULL; 453 } 454 pos = symtab_hdr->sh_offset + symoffset * extsym_size; 455 size_t alloc_ext_size = amt; 456 if (bfd_seek (ibfd, pos, SEEK_SET) != 0 457 || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size, 458 &alloc_ext, ibfd, false)) 459 { 460 intsym_buf = NULL; 461 goto out2; 462 } 463 464 size_t alloc_extshndx_size = 0; 465 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) 466 extshndx_buf = NULL; 467 else 468 { 469 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt)) 470 { 471 bfd_set_error (bfd_error_file_too_big); 472 intsym_buf = NULL; 473 goto out1; 474 } 475 alloc_extshndx_size = amt; 476 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); 477 if (bfd_seek (ibfd, pos, SEEK_SET) != 0 478 || !_bfd_mmap_read_temporary ((void **) &extshndx_buf, 479 &alloc_extshndx_size, 480 (void **) &alloc_extshndx, 481 ibfd, false)) 482 { 483 intsym_buf = NULL; 484 goto out1; 485 } 486 } 487 488 if (intsym_buf == NULL) 489 { 490 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt)) 491 { 492 bfd_set_error (bfd_error_file_too_big); 493 goto out1; 494 } 495 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt); 496 intsym_buf = alloc_intsym; 497 if (intsym_buf == NULL) 498 goto out1; 499 } 500 501 /* Convert the symbols to internal form. */ 502 isymend = intsym_buf + symcount; 503 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, 504 shndx = extshndx_buf; 505 isym < isymend; 506 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) 507 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) 508 { 509 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; 510 /* xgettext:c-format */ 511 _bfd_error_handler (_("%pB symbol number %lu references" 512 " nonexistent SHT_SYMTAB_SHNDX section"), 513 ibfd, (unsigned long) symoffset); 514 free (alloc_intsym); 515 intsym_buf = NULL; 516 goto out1; 517 } 518 519 out1: 520 _bfd_munmap_temporary (alloc_extshndx, alloc_extshndx_size); 521 out2: 522 _bfd_munmap_temporary (alloc_ext, alloc_ext_size); 523 524 return intsym_buf; 525 } 526 527 /* Look up a symbol name. */ 528 static const char * 529 bfd_elf_sym_name_raw (bfd *abfd, 530 Elf_Internal_Shdr *symtab_hdr, 531 Elf_Internal_Sym *isym) 532 { 533 unsigned int iname = isym->st_name; 534 unsigned int shindex = symtab_hdr->sh_link; 535 536 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION 537 /* Check for a bogus st_shndx to avoid crashing. */ 538 && isym->st_shndx < elf_numsections (abfd)) 539 { 540 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; 541 shindex = elf_elfheader (abfd)->e_shstrndx; 542 } 543 544 return bfd_elf_string_from_elf_section (abfd, shindex, iname); 545 } 546 547 const char * 548 bfd_elf_sym_name (bfd *abfd, 549 Elf_Internal_Shdr *symtab_hdr, 550 Elf_Internal_Sym *isym, 551 asection *sym_sec) 552 { 553 const char *name = bfd_elf_sym_name_raw (abfd, symtab_hdr, isym); 554 if (name == NULL) 555 name = bfd_symbol_error_name; 556 else if (sym_sec && *name == '\0') 557 name = bfd_section_name (sym_sec); 558 559 return name; 560 } 561 562 /* Return the name of the group signature symbol. Why isn't the 563 signature just a string? */ 564 565 static const char * 566 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) 567 { 568 Elf_Internal_Shdr *hdr; 569 unsigned char esym[sizeof (Elf64_External_Sym)]; 570 Elf_External_Sym_Shndx eshndx; 571 Elf_Internal_Sym isym; 572 573 /* First we need to ensure the symbol table is available. Make sure 574 that it is a symbol table section. */ 575 if (ghdr->sh_link >= elf_numsections (abfd)) 576 return NULL; 577 hdr = elf_elfsections (abfd) [ghdr->sh_link]; 578 if (hdr->sh_type != SHT_SYMTAB 579 || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) 580 return NULL; 581 582 /* Go read the symbol. */ 583 hdr = &elf_tdata (abfd)->symtab_hdr; 584 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, 585 &isym, esym, &eshndx) == NULL) 586 return NULL; 587 588 return bfd_elf_sym_name_raw (abfd, hdr, &isym); 589 } 590 591 static bool 592 is_valid_group_section_header (Elf_Internal_Shdr *shdr, size_t minsize) 593 { 594 return (shdr->sh_size >= minsize 595 && shdr->sh_entsize == GRP_ENTRY_SIZE 596 && shdr->sh_size % GRP_ENTRY_SIZE == 0 597 && shdr->bfd_section != NULL); 598 } 599 600 601 /* Set next_in_group, sec_group list pointers, and group names. */ 602 603 static bool 604 process_sht_group_entries (bfd *abfd, 605 Elf_Internal_Shdr *ghdr, unsigned int gidx) 606 { 607 unsigned char *contents; 608 609 /* Read the raw contents. */ 610 if (!bfd_malloc_and_get_section (abfd, ghdr->bfd_section, &contents)) 611 { 612 _bfd_error_handler 613 /* xgettext:c-format */ 614 (_("%pB: could not read contents of group [%u]"), abfd, gidx); 615 return false; 616 } 617 618 asection *last_elt = NULL; 619 const char *gname = NULL; 620 unsigned char *p = contents + ghdr->sh_size; 621 while (1) 622 { 623 unsigned int idx; 624 Elf_Internal_Shdr *shdr; 625 asection *elt; 626 627 p -= 4; 628 idx = H_GET_32 (abfd, p); 629 if (p == contents) 630 { 631 if ((idx & GRP_COMDAT) != 0) 632 ghdr->bfd_section->flags 633 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 634 break; 635 } 636 637 if (idx == 0 638 || idx >= elf_numsections (abfd) 639 || (shdr = elf_elfsections (abfd)[idx])->sh_type == SHT_GROUP 640 || ((elt = shdr->bfd_section) != NULL 641 && elf_sec_group (elt) != NULL 642 && elf_sec_group (elt) != ghdr->bfd_section)) 643 { 644 _bfd_error_handler 645 (_("%pB: invalid entry (%#x) in group [%u]"), 646 abfd, idx, gidx); 647 continue; 648 } 649 650 /* PR binutils/23199: According to the ELF gABI all sections in 651 a group must be marked with SHF_GROUP, but some tools 652 generate broken objects. Fix them up here. */ 653 shdr->sh_flags |= SHF_GROUP; 654 655 if (elt == NULL) 656 { 657 if (shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL) 658 { 659 const char *name = bfd_elf_string_from_elf_section 660 (abfd, elf_elfheader (abfd)->e_shstrndx, shdr->sh_name); 661 662 _bfd_error_handler 663 /* xgettext:c-format */ 664 (_("%pB: unexpected type (%#x) section `%s' in group [%u]"), 665 abfd, shdr->sh_type, name, gidx); 666 } 667 continue; 668 } 669 670 /* Don't try to add a section to elf_next_in_group list twice. */ 671 if (elf_sec_group (elt) != NULL) 672 continue; 673 674 if (last_elt == NULL) 675 { 676 /* Start a circular list with one element. 677 It will be in reverse order to match what gas does. */ 678 elf_next_in_group (elt) = elt; 679 /* Point the group section to it. */ 680 elf_next_in_group (ghdr->bfd_section) = elt; 681 gname = group_signature (abfd, ghdr); 682 if (gname == NULL) 683 { 684 free (contents); 685 return false; 686 } 687 } 688 else 689 { 690 elf_next_in_group (elt) = elf_next_in_group (last_elt); 691 elf_next_in_group (last_elt) = elt; 692 } 693 last_elt = elt; 694 elf_group_name (elt) = gname; 695 elf_sec_group (elt) = ghdr->bfd_section; 696 } 697 698 free (contents); 699 return true; 700 } 701 702 bool 703 _bfd_elf_setup_sections (bfd *abfd) 704 { 705 bool result = true; 706 707 /* Process SHF_LINK_ORDER. */ 708 for (asection *s = abfd->sections; s != NULL; s = s->next) 709 { 710 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; 711 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) 712 { 713 unsigned int elfsec = this_hdr->sh_link; 714 /* An sh_link value of 0 is now allowed. It indicates that linked 715 to section has already been discarded, but that the current 716 section has been retained for some other reason. This linking 717 section is still a candidate for later garbage collection 718 however. */ 719 if (elfsec == 0) 720 { 721 elf_linked_to_section (s) = NULL; 722 } 723 else 724 { 725 asection *linksec = NULL; 726 727 if (elfsec < elf_numsections (abfd)) 728 { 729 this_hdr = elf_elfsections (abfd)[elfsec]; 730 linksec = this_hdr->bfd_section; 731 } 732 733 /* PR 1991, 2008: 734 Some strip/objcopy may leave an incorrect value in 735 sh_link. We don't want to proceed. */ 736 if (linksec == NULL) 737 { 738 _bfd_error_handler 739 /* xgettext:c-format */ 740 (_("%pB: sh_link [%d] in section `%pA' is incorrect"), 741 s->owner, elfsec, s); 742 result = false; 743 } 744 745 elf_linked_to_section (s) = linksec; 746 } 747 } 748 } 749 750 /* Process section groups. */ 751 for (unsigned int i = 1; i < elf_numsections (abfd); i++) 752 { 753 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; 754 755 if (shdr && shdr->sh_type == SHT_GROUP) 756 { 757 if (is_valid_group_section_header (shdr, GRP_ENTRY_SIZE)) 758 { 759 if (shdr->sh_size >= 2 * GRP_ENTRY_SIZE 760 && !process_sht_group_entries (abfd, shdr, i)) 761 result = false; 762 } 763 else 764 { 765 /* PR binutils/18758: Beware of corrupt binaries with 766 invalid group data. */ 767 _bfd_error_handler 768 /* xgettext:c-format */ 769 (_("%pB: section group entry number %u is corrupt"), abfd, i); 770 result = false; 771 } 772 } 773 } 774 775 return result; 776 } 777 778 bool 779 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) 780 { 781 return elf_next_in_group (sec) != NULL; 782 } 783 784 const char * 785 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) 786 { 787 if (elf_sec_group (sec) != NULL) 788 return elf_group_name (sec); 789 return NULL; 790 } 791 792 /* Make a BFD section from an ELF section. We store a pointer to the 793 BFD section in the bfd_section field of the header. */ 794 795 bool 796 _bfd_elf_make_section_from_shdr (bfd *abfd, 797 Elf_Internal_Shdr *hdr, 798 const char *name, 799 int shindex) 800 { 801 asection *newsect; 802 flagword flags; 803 const struct elf_backend_data *bed; 804 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 805 806 if (hdr->bfd_section != NULL) 807 return true; 808 809 newsect = bfd_make_section_anyway (abfd, name); 810 if (newsect == NULL) 811 return false; 812 813 hdr->bfd_section = newsect; 814 elf_section_data (newsect)->this_hdr = *hdr; 815 elf_section_data (newsect)->this_idx = shindex; 816 817 /* Always use the real type/flags. */ 818 elf_section_type (newsect) = hdr->sh_type; 819 elf_section_flags (newsect) = hdr->sh_flags; 820 821 newsect->filepos = hdr->sh_offset; 822 823 flags = SEC_NO_FLAGS; 824 if (hdr->sh_type != SHT_NOBITS) 825 flags |= SEC_HAS_CONTENTS; 826 if (hdr->sh_type == SHT_GROUP) 827 flags |= SEC_GROUP; 828 if ((hdr->sh_flags & SHF_ALLOC) != 0) 829 { 830 flags |= SEC_ALLOC; 831 if (hdr->sh_type != SHT_NOBITS) 832 flags |= SEC_LOAD; 833 } 834 if ((hdr->sh_flags & SHF_WRITE) == 0) 835 flags |= SEC_READONLY; 836 if ((hdr->sh_flags & SHF_EXECINSTR) != 0) 837 flags |= SEC_CODE; 838 else if ((flags & SEC_LOAD) != 0) 839 flags |= SEC_DATA; 840 if ((hdr->sh_flags & SHF_MERGE) != 0) 841 { 842 flags |= SEC_MERGE; 843 newsect->entsize = hdr->sh_entsize; 844 } 845 if ((hdr->sh_flags & SHF_STRINGS) != 0) 846 { 847 flags |= SEC_STRINGS; 848 newsect->entsize = hdr->sh_entsize; 849 } 850 if ((hdr->sh_flags & SHF_TLS) != 0) 851 flags |= SEC_THREAD_LOCAL; 852 if ((hdr->sh_flags & SHF_EXCLUDE) != 0) 853 flags |= SEC_EXCLUDE; 854 855 switch (elf_elfheader (abfd)->e_ident[EI_OSABI]) 856 { 857 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE, 858 but binutils as of 2019-07-23 did not set the EI_OSABI header 859 byte. */ 860 case ELFOSABI_GNU: 861 case ELFOSABI_FREEBSD: 862 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0) 863 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain; 864 /* Fall through */ 865 case ELFOSABI_NONE: 866 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0) 867 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind; 868 break; 869 } 870 871 if ((flags & SEC_ALLOC) == 0) 872 { 873 /* The debugging sections appear to be recognized only by name, 874 not any sort of flag. Their SEC_ALLOC bits are cleared. */ 875 if (name [0] == '.') 876 { 877 if (startswith (name, ".debug") 878 || startswith (name, ".gnu.debuglto_.debug_") 879 || startswith (name, ".gnu.linkonce.wi.") 880 || startswith (name, ".zdebug")) 881 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS; 882 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME) 883 || startswith (name, ".note.gnu")) 884 { 885 flags |= SEC_ELF_OCTETS; 886 opb = 1; 887 } 888 else if (startswith (name, ".line") 889 || startswith (name, ".stab") 890 || strcmp (name, ".gdb_index") == 0) 891 flags |= SEC_DEBUGGING; 892 } 893 } 894 895 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb) 896 || !bfd_set_section_size (newsect, hdr->sh_size) 897 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign 898 & -hdr->sh_addralign))) 899 return false; 900 901 /* As a GNU extension, if the name begins with .gnu.linkonce, we 902 only link a single copy of the section. This is used to support 903 g++. g++ will emit each template expansion in its own section. 904 The symbols will be defined as weak, so that multiple definitions 905 are permitted. The GNU linker extension is to actually discard 906 all but one of the sections. */ 907 if (startswith (name, ".gnu.linkonce") 908 && elf_next_in_group (newsect) == NULL) 909 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 910 911 if (!bfd_set_section_flags (newsect, flags)) 912 return false; 913 914 bed = get_elf_backend_data (abfd); 915 if (bed->elf_backend_section_flags) 916 if (!bed->elf_backend_section_flags (hdr)) 917 return false; 918 919 /* We do not parse the PT_NOTE segments as we are interested even in the 920 separate debug info files which may have the segments offsets corrupted. 921 PT_NOTEs from the core files are currently not parsed using BFD. */ 922 if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0) 923 { 924 bfd_byte *contents; 925 926 if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents)) 927 return false; 928 929 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, 930 hdr->sh_offset, hdr->sh_addralign); 931 _bfd_elf_munmap_section_contents (newsect, contents); 932 } 933 934 if ((newsect->flags & SEC_ALLOC) != 0) 935 { 936 Elf_Internal_Phdr *phdr; 937 unsigned int i, nload; 938 939 /* Some ELF linkers produce binaries with all the program header 940 p_paddr fields zero. If we have such a binary with more than 941 one PT_LOAD header, then leave the section lma equal to vma 942 so that we don't create sections with overlapping lma. */ 943 phdr = elf_tdata (abfd)->phdr; 944 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 945 if (phdr->p_paddr != 0) 946 break; 947 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) 948 ++nload; 949 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) 950 return true; 951 952 phdr = elf_tdata (abfd)->phdr; 953 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 954 { 955 if (((phdr->p_type == PT_LOAD 956 && (hdr->sh_flags & SHF_TLS) == 0) 957 || phdr->p_type == PT_TLS) 958 && ELF_SECTION_IN_SEGMENT (hdr, phdr)) 959 { 960 if ((newsect->flags & SEC_LOAD) == 0) 961 newsect->lma = (phdr->p_paddr 962 + hdr->sh_addr - phdr->p_vaddr) / opb; 963 else 964 /* We used to use the same adjustment for SEC_LOAD 965 sections, but that doesn't work if the segment 966 is packed with code from multiple VMAs. 967 Instead we calculate the section LMA based on 968 the segment LMA. It is assumed that the 969 segment will contain sections with contiguous 970 LMAs, even if the VMAs are not. */ 971 newsect->lma = (phdr->p_paddr 972 + hdr->sh_offset - phdr->p_offset) / opb; 973 974 /* With contiguous segments, we can't tell from file 975 offsets whether a section with zero size should 976 be placed at the end of one segment or the 977 beginning of the next. Decide based on vaddr. */ 978 if (hdr->sh_addr >= phdr->p_vaddr 979 && (hdr->sh_addr + hdr->sh_size 980 <= phdr->p_vaddr + phdr->p_memsz)) 981 break; 982 } 983 } 984 } 985 986 /* Compress/decompress DWARF debug sections with names: .debug_*, 987 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */ 988 if ((newsect->flags & SEC_DEBUGGING) != 0 989 && (newsect->flags & SEC_HAS_CONTENTS) != 0 990 && (newsect->flags & SEC_ELF_OCTETS) != 0) 991 { 992 enum { nothing, compress, decompress } action = nothing; 993 int compression_header_size; 994 bfd_size_type uncompressed_size; 995 unsigned int uncompressed_align_power; 996 enum compression_type ch_type = ch_none; 997 bool compressed 998 = bfd_is_section_compressed_info (abfd, newsect, 999 &compression_header_size, 1000 &uncompressed_size, 1001 &uncompressed_align_power, 1002 &ch_type); 1003 1004 /* Should we decompress? */ 1005 if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed) 1006 action = decompress; 1007 1008 /* Should we compress? Or convert to a different compression? */ 1009 else if ((abfd->flags & BFD_COMPRESS) != 0 1010 && newsect->size != 0 1011 && compression_header_size >= 0 1012 && uncompressed_size > 0) 1013 { 1014 if (!compressed) 1015 action = compress; 1016 else 1017 { 1018 enum compression_type new_ch_type = ch_none; 1019 if ((abfd->flags & BFD_COMPRESS_GABI) != 0) 1020 new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0 1021 ? ch_compress_zstd : ch_compress_zlib); 1022 if (new_ch_type != ch_type) 1023 action = compress; 1024 } 1025 } 1026 1027 if (action == compress) 1028 { 1029 if (!bfd_init_section_compress_status (abfd, newsect)) 1030 { 1031 _bfd_error_handler 1032 /* xgettext:c-format */ 1033 (_("%pB: unable to compress section %s"), abfd, name); 1034 return false; 1035 } 1036 } 1037 else if (action == decompress) 1038 { 1039 if (!bfd_init_section_decompress_status (abfd, newsect)) 1040 { 1041 _bfd_error_handler 1042 /* xgettext:c-format */ 1043 (_("%pB: unable to decompress section %s"), abfd, name); 1044 return false; 1045 } 1046 #ifndef HAVE_ZSTD 1047 if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD) 1048 { 1049 _bfd_error_handler 1050 /* xgettext:c-format */ 1051 (_ ("%pB: section %s is compressed with zstd, but BFD " 1052 "is not built with zstd support"), 1053 abfd, name); 1054 newsect->compress_status = COMPRESS_SECTION_NONE; 1055 return false; 1056 } 1057 #endif 1058 if (abfd->is_linker_input 1059 && name[1] == 'z') 1060 { 1061 /* Rename section from .zdebug_* to .debug_* so that ld 1062 scripts will see this section as a debug section. */ 1063 char *new_name = bfd_zdebug_name_to_debug (abfd, name); 1064 if (new_name == NULL) 1065 return false; 1066 bfd_rename_section (newsect, new_name); 1067 } 1068 } 1069 } 1070 1071 return true; 1072 } 1073 1074 const char *const bfd_elf_section_type_names[] = 1075 { 1076 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", 1077 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", 1078 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", 1079 }; 1080 1081 /* ELF relocs are against symbols. If we are producing relocatable 1082 output, and the reloc is against an external symbol, and nothing 1083 has given us any additional addend, the resulting reloc will also 1084 be against the same symbol. In such a case, we don't want to 1085 change anything about the way the reloc is handled, since it will 1086 all be done at final link time. Rather than put special case code 1087 into bfd_perform_relocation, all the reloc types use this howto 1088 function, or should call this function for relocatable output. */ 1089 1090 bfd_reloc_status_type 1091 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, 1092 arelent *reloc_entry, 1093 asymbol *symbol, 1094 void *data ATTRIBUTE_UNUSED, 1095 asection *input_section, 1096 bfd *output_bfd, 1097 char **error_message ATTRIBUTE_UNUSED) 1098 { 1099 if (output_bfd != NULL 1100 && (symbol->flags & BSF_SECTION_SYM) == 0 1101 && (! reloc_entry->howto->partial_inplace 1102 || reloc_entry->addend == 0)) 1103 { 1104 reloc_entry->address += input_section->output_offset; 1105 return bfd_reloc_ok; 1106 } 1107 1108 /* In some cases the relocation should be treated as output section 1109 relative, as when linking ELF DWARF into PE COFF. Many ELF 1110 targets lack section relative relocations and instead use 1111 ordinary absolute relocations for references between DWARF 1112 sections. That is arguably a bug in those targets but it happens 1113 to work for the usual case of linking to non-loaded ELF debug 1114 sections with VMAs forced to zero. PE COFF on the other hand 1115 doesn't allow a section VMA of zero. */ 1116 if (output_bfd == NULL 1117 && !reloc_entry->howto->pc_relative 1118 && (symbol->section->flags & SEC_DEBUGGING) != 0 1119 && (input_section->flags & SEC_DEBUGGING) != 0) 1120 reloc_entry->addend -= symbol->section->output_section->vma; 1121 1122 return bfd_reloc_continue; 1123 } 1124 1125 /* Returns TRUE if section A matches section B. 1127 Names, addresses and links may be different, but everything else 1128 should be the same. */ 1129 1130 static bool 1131 section_match (const Elf_Internal_Shdr * a, 1132 const Elf_Internal_Shdr * b) 1133 { 1134 if (a->sh_type != b->sh_type 1135 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0 1136 || a->sh_addralign != b->sh_addralign 1137 || a->sh_entsize != b->sh_entsize) 1138 return false; 1139 if (a->sh_type == SHT_SYMTAB 1140 || a->sh_type == SHT_STRTAB) 1141 return true; 1142 return a->sh_size == b->sh_size; 1143 } 1144 1145 /* Find a section in OBFD that has the same characteristics 1146 as IHEADER. Return the index of this section or SHN_UNDEF if 1147 none can be found. Check's section HINT first, as this is likely 1148 to be the correct section. */ 1149 1150 static unsigned int 1151 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader, 1152 const unsigned int hint) 1153 { 1154 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); 1155 unsigned int i; 1156 1157 BFD_ASSERT (iheader != NULL); 1158 1159 /* See PR 20922 for a reproducer of the NULL test. */ 1160 if (hint < elf_numsections (obfd) 1161 && oheaders[hint] != NULL 1162 && section_match (oheaders[hint], iheader)) 1163 return hint; 1164 1165 for (i = 1; i < elf_numsections (obfd); i++) 1166 { 1167 Elf_Internal_Shdr * oheader = oheaders[i]; 1168 1169 if (oheader == NULL) 1170 continue; 1171 if (section_match (oheader, iheader)) 1172 /* FIXME: Do we care if there is a potential for 1173 multiple matches ? */ 1174 return i; 1175 } 1176 1177 return SHN_UNDEF; 1178 } 1179 1180 /* PR 19938: Attempt to set the ELF section header fields of an OS or 1181 Processor specific section, based upon a matching input section. 1182 Returns TRUE upon success, FALSE otherwise. */ 1183 1184 static bool 1185 copy_special_section_fields (const bfd *ibfd, 1186 bfd *obfd, 1187 const Elf_Internal_Shdr *iheader, 1188 Elf_Internal_Shdr *oheader, 1189 const unsigned int secnum) 1190 { 1191 const struct elf_backend_data *bed = get_elf_backend_data (obfd); 1192 const Elf_Internal_Shdr **iheaders 1193 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); 1194 bool changed = false; 1195 unsigned int sh_link; 1196 1197 if (oheader->sh_type == SHT_NOBITS) 1198 { 1199 /* This is a feature for objcopy --only-keep-debug: 1200 When a section's type is changed to NOBITS, we preserve 1201 the sh_link and sh_info fields so that they can be 1202 matched up with the original. 1203 1204 Note: Strictly speaking these assignments are wrong. 1205 The sh_link and sh_info fields should point to the 1206 relevent sections in the output BFD, which may not be in 1207 the same location as they were in the input BFD. But 1208 the whole point of this action is to preserve the 1209 original values of the sh_link and sh_info fields, so 1210 that they can be matched up with the section headers in 1211 the original file. So strictly speaking we may be 1212 creating an invalid ELF file, but it is only for a file 1213 that just contains debug info and only for sections 1214 without any contents. */ 1215 if (oheader->sh_link == 0) 1216 oheader->sh_link = iheader->sh_link; 1217 if (oheader->sh_info == 0) 1218 oheader->sh_info = iheader->sh_info; 1219 return true; 1220 } 1221 1222 /* Allow the target a chance to decide how these fields should be set. */ 1223 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd, 1224 iheader, oheader)) 1225 return true; 1226 1227 /* We have an iheader which might match oheader, and which has non-zero 1228 sh_info and/or sh_link fields. Attempt to follow those links and find 1229 the section in the output bfd which corresponds to the linked section 1230 in the input bfd. */ 1231 if (iheader->sh_link != SHN_UNDEF) 1232 { 1233 /* See PR 20931 for a reproducer. */ 1234 if (iheader->sh_link >= elf_numsections (ibfd)) 1235 { 1236 _bfd_error_handler 1237 /* xgettext:c-format */ 1238 (_("%pB: invalid sh_link field (%d) in section number %d"), 1239 ibfd, iheader->sh_link, secnum); 1240 return false; 1241 } 1242 1243 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); 1244 if (sh_link != SHN_UNDEF) 1245 { 1246 oheader->sh_link = sh_link; 1247 changed = true; 1248 } 1249 else 1250 /* FIXME: Should we install iheader->sh_link 1251 if we could not find a match ? */ 1252 _bfd_error_handler 1253 /* xgettext:c-format */ 1254 (_("%pB: failed to find link section for section %d"), obfd, secnum); 1255 } 1256 1257 if (iheader->sh_info) 1258 { 1259 /* The sh_info field can hold arbitrary information, but if the 1260 SHF_LINK_INFO flag is set then it should be interpreted as a 1261 section index. */ 1262 if (iheader->sh_flags & SHF_INFO_LINK) 1263 { 1264 sh_link = find_link (obfd, iheaders[iheader->sh_info], 1265 iheader->sh_info); 1266 if (sh_link != SHN_UNDEF) 1267 oheader->sh_flags |= SHF_INFO_LINK; 1268 } 1269 else 1270 /* No idea what it means - just copy it. */ 1271 sh_link = iheader->sh_info; 1272 1273 if (sh_link != SHN_UNDEF) 1274 { 1275 oheader->sh_info = sh_link; 1276 changed = true; 1277 } 1278 else 1279 _bfd_error_handler 1280 /* xgettext:c-format */ 1281 (_("%pB: failed to find info section for section %d"), obfd, secnum); 1282 } 1283 1284 return changed; 1285 } 1286 1287 /* Copy the program header and other data from one object module to 1288 another. */ 1289 1290 bool 1291 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) 1292 { 1293 const Elf_Internal_Shdr **iheaders 1294 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); 1295 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); 1296 const struct elf_backend_data *bed; 1297 unsigned int i; 1298 1299 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 1300 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 1301 return true; 1302 1303 if (!elf_flags_init (obfd)) 1304 { 1305 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; 1306 elf_flags_init (obfd) = true; 1307 } 1308 1309 elf_gp (obfd) = elf_gp (ibfd); 1310 1311 /* Also copy the EI_OSABI field. */ 1312 elf_elfheader (obfd)->e_ident[EI_OSABI] = 1313 elf_elfheader (ibfd)->e_ident[EI_OSABI]; 1314 1315 /* If set, copy the EI_ABIVERSION field. */ 1316 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) 1317 elf_elfheader (obfd)->e_ident[EI_ABIVERSION] 1318 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; 1319 1320 /* Copy object attributes. */ 1321 _bfd_elf_copy_obj_attributes (ibfd, obfd); 1322 1323 if (iheaders == NULL || oheaders == NULL) 1324 return true; 1325 1326 bed = get_elf_backend_data (obfd); 1327 1328 /* Possibly copy other fields in the section header. */ 1329 for (i = 1; i < elf_numsections (obfd); i++) 1330 { 1331 unsigned int j; 1332 Elf_Internal_Shdr * oheader = oheaders[i]; 1333 1334 /* Ignore ordinary sections. SHT_NOBITS sections are considered however 1335 because of a special case need for generating separate debug info 1336 files. See below for more details. */ 1337 if (oheader == NULL 1338 || (oheader->sh_type != SHT_NOBITS 1339 && oheader->sh_type < SHT_LOOS)) 1340 continue; 1341 1342 /* Ignore empty sections, and sections whose 1343 fields have already been initialised. */ 1344 if (oheader->sh_size == 0 1345 || (oheader->sh_info != 0 && oheader->sh_link != 0)) 1346 continue; 1347 1348 /* Scan for the matching section in the input bfd. 1349 First we try for a direct mapping between the input and 1350 output sections. */ 1351 for (j = 1; j < elf_numsections (ibfd); j++) 1352 { 1353 const Elf_Internal_Shdr * iheader = iheaders[j]; 1354 1355 if (iheader == NULL) 1356 continue; 1357 1358 if (oheader->bfd_section != NULL 1359 && iheader->bfd_section != NULL 1360 && iheader->bfd_section->output_section != NULL 1361 && iheader->bfd_section->output_section == oheader->bfd_section) 1362 { 1363 /* We have found a connection from the input section to 1364 the output section. Attempt to copy the header fields. 1365 If this fails then do not try any further sections - 1366 there should only be a one-to-one mapping between 1367 input and output. */ 1368 if (!copy_special_section_fields (ibfd, obfd, 1369 iheader, oheader, i)) 1370 j = elf_numsections (ibfd); 1371 break; 1372 } 1373 } 1374 1375 if (j < elf_numsections (ibfd)) 1376 continue; 1377 1378 /* That failed. So try to deduce the corresponding input section. 1379 Unfortunately we cannot compare names as the output string table 1380 is empty, so instead we check size, address and type. */ 1381 for (j = 1; j < elf_numsections (ibfd); j++) 1382 { 1383 const Elf_Internal_Shdr * iheader = iheaders[j]; 1384 1385 if (iheader == NULL) 1386 continue; 1387 1388 /* Try matching fields in the input section's header. 1389 Since --only-keep-debug turns all non-debug sections into 1390 SHT_NOBITS sections, the output SHT_NOBITS type matches any 1391 input type. */ 1392 if ((oheader->sh_type == SHT_NOBITS 1393 || iheader->sh_type == oheader->sh_type) 1394 && (iheader->sh_flags & ~ SHF_INFO_LINK) 1395 == (oheader->sh_flags & ~ SHF_INFO_LINK) 1396 && iheader->sh_addralign == oheader->sh_addralign 1397 && iheader->sh_entsize == oheader->sh_entsize 1398 && iheader->sh_size == oheader->sh_size 1399 && iheader->sh_addr == oheader->sh_addr 1400 && (iheader->sh_info != oheader->sh_info 1401 || iheader->sh_link != oheader->sh_link)) 1402 { 1403 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) 1404 break; 1405 } 1406 } 1407 1408 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) 1409 { 1410 /* Final attempt. Call the backend copy function 1411 with a NULL input section. */ 1412 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd, 1413 NULL, oheader); 1414 } 1415 } 1416 1417 return true; 1418 } 1419 1420 static const char * 1421 get_segment_type (unsigned int p_type) 1422 { 1423 const char *pt; 1424 switch (p_type) 1425 { 1426 case PT_NULL: pt = "NULL"; break; 1427 case PT_LOAD: pt = "LOAD"; break; 1428 case PT_DYNAMIC: pt = "DYNAMIC"; break; 1429 case PT_INTERP: pt = "INTERP"; break; 1430 case PT_NOTE: pt = "NOTE"; break; 1431 case PT_SHLIB: pt = "SHLIB"; break; 1432 case PT_PHDR: pt = "PHDR"; break; 1433 case PT_TLS: pt = "TLS"; break; 1434 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; 1435 case PT_GNU_STACK: pt = "STACK"; break; 1436 case PT_GNU_RELRO: pt = "RELRO"; break; 1437 case PT_GNU_SFRAME: pt = "SFRAME"; break; 1438 default: pt = NULL; break; 1439 } 1440 return pt; 1441 } 1442 1443 /* Print out the program headers. */ 1444 1445 bool 1446 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) 1447 { 1448 FILE *f = (FILE *) farg; 1449 Elf_Internal_Phdr *p; 1450 asection *s; 1451 bfd_byte *dynbuf = NULL; 1452 1453 p = elf_tdata (abfd)->phdr; 1454 if (p != NULL) 1455 { 1456 unsigned int i, c; 1457 1458 fprintf (f, _("\nProgram Header:\n")); 1459 c = elf_elfheader (abfd)->e_phnum; 1460 for (i = 0; i < c; i++, p++) 1461 { 1462 const char *pt = get_segment_type (p->p_type); 1463 char buf[20]; 1464 1465 if (pt == NULL) 1466 { 1467 sprintf (buf, "0x%lx", p->p_type); 1468 pt = buf; 1469 } 1470 fprintf (f, "%8s off 0x", pt); 1471 bfd_fprintf_vma (abfd, f, p->p_offset); 1472 fprintf (f, " vaddr 0x"); 1473 bfd_fprintf_vma (abfd, f, p->p_vaddr); 1474 fprintf (f, " paddr 0x"); 1475 bfd_fprintf_vma (abfd, f, p->p_paddr); 1476 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); 1477 fprintf (f, " filesz 0x"); 1478 bfd_fprintf_vma (abfd, f, p->p_filesz); 1479 fprintf (f, " memsz 0x"); 1480 bfd_fprintf_vma (abfd, f, p->p_memsz); 1481 fprintf (f, " flags %c%c%c", 1482 (p->p_flags & PF_R) != 0 ? 'r' : '-', 1483 (p->p_flags & PF_W) != 0 ? 'w' : '-', 1484 (p->p_flags & PF_X) != 0 ? 'x' : '-'); 1485 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) 1486 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); 1487 fprintf (f, "\n"); 1488 } 1489 } 1490 1491 s = bfd_get_section_by_name (abfd, ".dynamic"); 1492 if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0) 1493 { 1494 unsigned int elfsec; 1495 unsigned long shlink; 1496 bfd_byte *extdyn, *extdynend; 1497 size_t extdynsize; 1498 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 1499 1500 fprintf (f, _("\nDynamic Section:\n")); 1501 1502 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf)) 1503 goto error_return; 1504 1505 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 1506 if (elfsec == SHN_BAD) 1507 goto error_return; 1508 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 1509 1510 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 1511 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 1512 1513 for (extdyn = dynbuf, extdynend = dynbuf + s->size; 1514 (size_t) (extdynend - extdyn) >= extdynsize; 1515 extdyn += extdynsize) 1516 { 1517 Elf_Internal_Dyn dyn; 1518 const char *name = ""; 1519 char ab[20]; 1520 bool stringp; 1521 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 1522 1523 (*swap_dyn_in) (abfd, extdyn, &dyn); 1524 1525 if (dyn.d_tag == DT_NULL) 1526 break; 1527 1528 stringp = false; 1529 switch (dyn.d_tag) 1530 { 1531 default: 1532 if (bed->elf_backend_get_target_dtag) 1533 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); 1534 1535 if (!strcmp (name, "")) 1536 { 1537 sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag); 1538 name = ab; 1539 } 1540 break; 1541 1542 case DT_NEEDED: name = "NEEDED"; stringp = true; break; 1543 case DT_PLTRELSZ: name = "PLTRELSZ"; break; 1544 case DT_PLTGOT: name = "PLTGOT"; break; 1545 case DT_HASH: name = "HASH"; break; 1546 case DT_STRTAB: name = "STRTAB"; break; 1547 case DT_SYMTAB: name = "SYMTAB"; break; 1548 case DT_RELA: name = "RELA"; break; 1549 case DT_RELASZ: name = "RELASZ"; break; 1550 case DT_RELAENT: name = "RELAENT"; break; 1551 case DT_STRSZ: name = "STRSZ"; break; 1552 case DT_SYMENT: name = "SYMENT"; break; 1553 case DT_INIT: name = "INIT"; break; 1554 case DT_FINI: name = "FINI"; break; 1555 case DT_SONAME: name = "SONAME"; stringp = true; break; 1556 case DT_RPATH: name = "RPATH"; stringp = true; break; 1557 case DT_SYMBOLIC: name = "SYMBOLIC"; break; 1558 case DT_REL: name = "REL"; break; 1559 case DT_RELSZ: name = "RELSZ"; break; 1560 case DT_RELENT: name = "RELENT"; break; 1561 case DT_RELR: name = "RELR"; break; 1562 case DT_RELRSZ: name = "RELRSZ"; break; 1563 case DT_RELRENT: name = "RELRENT"; break; 1564 case DT_PLTREL: name = "PLTREL"; break; 1565 case DT_DEBUG: name = "DEBUG"; break; 1566 case DT_TEXTREL: name = "TEXTREL"; break; 1567 case DT_JMPREL: name = "JMPREL"; break; 1568 case DT_BIND_NOW: name = "BIND_NOW"; break; 1569 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; 1570 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; 1571 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; 1572 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; 1573 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break; 1574 case DT_FLAGS: name = "FLAGS"; break; 1575 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; 1576 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; 1577 case DT_CHECKSUM: name = "CHECKSUM"; break; 1578 case DT_PLTPADSZ: name = "PLTPADSZ"; break; 1579 case DT_MOVEENT: name = "MOVEENT"; break; 1580 case DT_MOVESZ: name = "MOVESZ"; break; 1581 case DT_FEATURE: name = "FEATURE"; break; 1582 case DT_POSFLAG_1: name = "POSFLAG_1"; break; 1583 case DT_SYMINSZ: name = "SYMINSZ"; break; 1584 case DT_SYMINENT: name = "SYMINENT"; break; 1585 case DT_CONFIG: name = "CONFIG"; stringp = true; break; 1586 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break; 1587 case DT_AUDIT: name = "AUDIT"; stringp = true; break; 1588 case DT_PLTPAD: name = "PLTPAD"; break; 1589 case DT_MOVETAB: name = "MOVETAB"; break; 1590 case DT_SYMINFO: name = "SYMINFO"; break; 1591 case DT_RELACOUNT: name = "RELACOUNT"; break; 1592 case DT_RELCOUNT: name = "RELCOUNT"; break; 1593 case DT_FLAGS_1: name = "FLAGS_1"; break; 1594 case DT_VERSYM: name = "VERSYM"; break; 1595 case DT_VERDEF: name = "VERDEF"; break; 1596 case DT_VERDEFNUM: name = "VERDEFNUM"; break; 1597 case DT_VERNEED: name = "VERNEED"; break; 1598 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; 1599 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break; 1600 case DT_USED: name = "USED"; break; 1601 case DT_FILTER: name = "FILTER"; stringp = true; break; 1602 case DT_GNU_HASH: name = "GNU_HASH"; break; 1603 } 1604 1605 fprintf (f, " %-20s ", name); 1606 if (! stringp) 1607 { 1608 fprintf (f, "0x"); 1609 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); 1610 } 1611 else 1612 { 1613 const char *string; 1614 unsigned int tagv = dyn.d_un.d_val; 1615 1616 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 1617 if (string == NULL) 1618 goto error_return; 1619 fprintf (f, "%s", string); 1620 } 1621 fprintf (f, "\n"); 1622 } 1623 1624 _bfd_elf_munmap_section_contents (s, dynbuf); 1625 dynbuf = NULL; 1626 } 1627 1628 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) 1629 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) 1630 { 1631 if (! _bfd_elf_slurp_version_tables (abfd, false)) 1632 return false; 1633 } 1634 1635 if (elf_dynverdef (abfd) != 0) 1636 { 1637 Elf_Internal_Verdef *t; 1638 1639 fprintf (f, _("\nVersion definitions:\n")); 1640 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) 1641 { 1642 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, 1643 t->vd_flags, t->vd_hash, 1644 t->vd_nodename ? t->vd_nodename : "<corrupt>"); 1645 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) 1646 { 1647 Elf_Internal_Verdaux *a; 1648 1649 fprintf (f, "\t"); 1650 for (a = t->vd_auxptr->vda_nextptr; 1651 a != NULL; 1652 a = a->vda_nextptr) 1653 fprintf (f, "%s ", 1654 a->vda_nodename ? a->vda_nodename : "<corrupt>"); 1655 fprintf (f, "\n"); 1656 } 1657 } 1658 } 1659 1660 if (elf_dynverref (abfd) != 0) 1661 { 1662 Elf_Internal_Verneed *t; 1663 1664 fprintf (f, _("\nVersion References:\n")); 1665 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) 1666 { 1667 Elf_Internal_Vernaux *a; 1668 1669 fprintf (f, _(" required from %s:\n"), 1670 t->vn_filename ? t->vn_filename : "<corrupt>"); 1671 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 1672 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, 1673 a->vna_flags, a->vna_other, 1674 a->vna_nodename ? a->vna_nodename : "<corrupt>"); 1675 } 1676 } 1677 1678 return true; 1679 1680 error_return: 1681 _bfd_elf_munmap_section_contents (s, dynbuf); 1682 return false; 1683 } 1684 1685 /* Find the file offset corresponding to VMA by using the program 1686 headers. */ 1687 1688 static file_ptr 1689 offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma, 1690 size_t size, size_t *max_size_p) 1691 { 1692 Elf_Internal_Phdr *seg; 1693 size_t i; 1694 1695 for (seg = phdrs, i = 0; i < phnum; ++seg, ++i) 1696 if (seg->p_type == PT_LOAD 1697 && vma >= (seg->p_vaddr & -seg->p_align) 1698 && vma + size <= seg->p_vaddr + seg->p_filesz) 1699 { 1700 if (max_size_p) 1701 *max_size_p = seg->p_vaddr + seg->p_filesz - vma; 1702 return vma - seg->p_vaddr + seg->p_offset; 1703 } 1704 1705 if (max_size_p) 1706 *max_size_p = 0; 1707 bfd_set_error (bfd_error_invalid_operation); 1708 return (file_ptr) -1; 1709 } 1710 1711 /* Convert hash table to internal form. */ 1712 1713 static bfd_vma * 1714 get_hash_table_data (bfd *abfd, bfd_size_type number, 1715 unsigned int ent_size, bfd_size_type filesize) 1716 { 1717 unsigned char *e_data = NULL; 1718 bfd_vma *i_data = NULL; 1719 bfd_size_type size; 1720 void *e_data_addr; 1721 size_t e_data_size ATTRIBUTE_UNUSED; 1722 1723 if (ent_size != 4 && ent_size != 8) 1724 return NULL; 1725 1726 if ((size_t) number != number) 1727 { 1728 bfd_set_error (bfd_error_file_too_big); 1729 return NULL; 1730 } 1731 1732 size = ent_size * number; 1733 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not 1734 attempting to allocate memory when the read is bound to fail. */ 1735 if (size > filesize 1736 || number >= ~(size_t) 0 / ent_size 1737 || number >= ~(size_t) 0 / sizeof (*i_data)) 1738 { 1739 bfd_set_error (bfd_error_file_too_big); 1740 return NULL; 1741 } 1742 1743 e_data = _bfd_mmap_temporary (abfd, size, &e_data_addr, &e_data_size); 1744 if (e_data == NULL) 1745 return NULL; 1746 1747 i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data)); 1748 if (i_data == NULL) 1749 { 1750 free (e_data); 1751 return NULL; 1752 } 1753 1754 if (ent_size == 4) 1755 while (number--) 1756 i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size); 1757 else 1758 while (number--) 1759 i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size); 1760 1761 _bfd_munmap_temporary (e_data_addr, e_data_size); 1762 return i_data; 1763 } 1764 1765 /* Address of .MIPS.xhash section. FIXME: What is the best way to 1766 support DT_MIPS_XHASH? */ 1767 #define DT_MIPS_XHASH 0x70000036 1768 1769 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */ 1770 1771 bool 1772 _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr, 1773 Elf_Internal_Phdr *phdrs, size_t phnum, 1774 bfd_size_type filesize) 1775 { 1776 bfd_byte *extdyn, *extdynend; 1777 size_t extdynsize; 1778 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 1779 bool (*swap_symbol_in) (bfd *, const void *, const void *, 1780 Elf_Internal_Sym *); 1781 Elf_Internal_Dyn dyn; 1782 bfd_vma dt_hash = 0; 1783 bfd_vma dt_gnu_hash = 0; 1784 bfd_vma dt_mips_xhash = 0; 1785 bfd_vma dt_strtab = 0; 1786 bfd_vma dt_symtab = 0; 1787 size_t dt_strsz = 0; 1788 bfd_vma dt_versym = 0; 1789 bfd_vma dt_verdef = 0; 1790 bfd_vma dt_verneed = 0; 1791 bfd_byte *dynbuf = NULL; 1792 char *strbuf = NULL; 1793 bfd_vma *gnubuckets = NULL; 1794 bfd_vma *gnuchains = NULL; 1795 bfd_vma *mipsxlat = NULL; 1796 file_ptr saved_filepos, filepos; 1797 bool res = false; 1798 size_t amt; 1799 bfd_byte *esymbuf = NULL, *esym; 1800 bfd_size_type symcount; 1801 Elf_Internal_Sym *isymbuf = NULL; 1802 Elf_Internal_Sym *isym, *isymend; 1803 bfd_byte *versym = NULL; 1804 bfd_byte *verdef = NULL; 1805 bfd_byte *verneed = NULL; 1806 size_t verdef_size = 0; 1807 size_t verneed_size = 0; 1808 size_t extsym_size; 1809 const struct elf_backend_data *bed; 1810 void *dynbuf_addr = NULL; 1811 void *esymbuf_addr = NULL; 1812 size_t dynbuf_size = 0; 1813 size_t esymbuf_size = 0; 1814 1815 /* Return TRUE if symbol table is bad. */ 1816 if (elf_bad_symtab (abfd)) 1817 return true; 1818 1819 /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */ 1820 if (elf_tdata (abfd)->dt_strtab != NULL) 1821 return true; 1822 1823 bed = get_elf_backend_data (abfd); 1824 1825 /* Save file position for elf_object_p. */ 1826 saved_filepos = bfd_tell (abfd); 1827 1828 if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0) 1829 goto error_return; 1830 1831 dynbuf_size = phdr->p_filesz; 1832 dynbuf = _bfd_mmap_temporary (abfd, dynbuf_size, &dynbuf_addr, &dynbuf_size); 1833 if (dynbuf == NULL) 1834 goto error_return; 1835 1836 extsym_size = bed->s->sizeof_sym; 1837 extdynsize = bed->s->sizeof_dyn; 1838 swap_dyn_in = bed->s->swap_dyn_in; 1839 1840 extdyn = dynbuf; 1841 if (phdr->p_filesz < extdynsize) 1842 goto error_return; 1843 extdynend = extdyn + phdr->p_filesz; 1844 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) 1845 { 1846 swap_dyn_in (abfd, extdyn, &dyn); 1847 1848 if (dyn.d_tag == DT_NULL) 1849 break; 1850 1851 switch (dyn.d_tag) 1852 { 1853 case DT_HASH: 1854 dt_hash = dyn.d_un.d_val; 1855 break; 1856 case DT_GNU_HASH: 1857 if (bed->elf_machine_code != EM_MIPS 1858 && bed->elf_machine_code != EM_MIPS_RS3_LE) 1859 dt_gnu_hash = dyn.d_un.d_val; 1860 break; 1861 case DT_STRTAB: 1862 dt_strtab = dyn.d_un.d_val; 1863 break; 1864 case DT_SYMTAB: 1865 dt_symtab = dyn.d_un.d_val; 1866 break; 1867 case DT_STRSZ: 1868 dt_strsz = dyn.d_un.d_val; 1869 break; 1870 case DT_SYMENT: 1871 if (dyn.d_un.d_val != extsym_size) 1872 goto error_return; 1873 break; 1874 case DT_VERSYM: 1875 dt_versym = dyn.d_un.d_val; 1876 break; 1877 case DT_VERDEF: 1878 dt_verdef = dyn.d_un.d_val; 1879 break; 1880 case DT_VERNEED: 1881 dt_verneed = dyn.d_un.d_val; 1882 break; 1883 default: 1884 if (dyn.d_tag == DT_MIPS_XHASH 1885 && (bed->elf_machine_code == EM_MIPS 1886 || bed->elf_machine_code == EM_MIPS_RS3_LE)) 1887 { 1888 dt_gnu_hash = dyn.d_un.d_val; 1889 dt_mips_xhash = dyn.d_un.d_val; 1890 } 1891 break; 1892 } 1893 } 1894 1895 /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC 1896 segment. */ 1897 if ((!dt_hash && !dt_gnu_hash) 1898 || !dt_strtab 1899 || !dt_symtab 1900 || !dt_strsz) 1901 goto error_return; 1902 1903 /* Get dynamic string table. */ 1904 filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL); 1905 if (filepos == (file_ptr) -1 1906 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 1907 goto error_return; 1908 1909 /* Dynamic string table must be valid until ABFD is closed. */ 1910 strbuf = (char *) _bfd_mmap_persistent (abfd, dt_strsz); 1911 if (strbuf == NULL) 1912 goto error_return; 1913 if (strbuf[dt_strsz - 1] != 0) 1914 { 1915 /* It is an error if a string table is't terminated. */ 1916 _bfd_error_handler 1917 /* xgettext:c-format */ 1918 (_("%pB: DT_STRTAB table is corrupt"), abfd); 1919 strbuf[dt_strsz - 1] = 0; 1920 } 1921 1922 /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer 1923 DT_HASH since it is simpler than DT_GNU_HASH. */ 1924 if (dt_hash) 1925 { 1926 unsigned char nb[16]; 1927 unsigned int hash_ent_size; 1928 1929 switch (bed->elf_machine_code) 1930 { 1931 case EM_ALPHA: 1932 case EM_S390: 1933 case EM_S390_OLD: 1934 if (bed->s->elfclass == ELFCLASS64) 1935 { 1936 hash_ent_size = 8; 1937 break; 1938 } 1939 /* FALLTHROUGH */ 1940 default: 1941 hash_ent_size = 4; 1942 break; 1943 } 1944 1945 filepos = offset_from_vma (phdrs, phnum, dt_hash, sizeof (nb), 1946 NULL); 1947 if (filepos == (file_ptr) -1 1948 || bfd_seek (abfd, filepos, SEEK_SET) != 0 1949 || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size) 1950 goto error_return; 1951 1952 /* The number of dynamic symbol table entries equals the number 1953 of chains. */ 1954 if (hash_ent_size == 8) 1955 symcount = bfd_get_64 (abfd, nb + hash_ent_size); 1956 else 1957 symcount = bfd_get_32 (abfd, nb + hash_ent_size); 1958 } 1959 else 1960 { 1961 /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL 1962 bindings are in hash table. Since in dynamic symbol table, 1963 all symbols with STB_LOCAL binding are placed before symbols 1964 with other bindings and all undefined symbols are placed 1965 before defined ones, the highest symbol index in DT_GNU_HASH 1966 is the highest dynamic symbol table index. */ 1967 unsigned char nb[16]; 1968 bfd_vma ngnubuckets; 1969 bfd_vma gnusymidx; 1970 size_t i, ngnuchains; 1971 bfd_vma maxchain = 0xffffffff, bitmaskwords; 1972 bfd_vma buckets_vma; 1973 1974 filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash, 1975 sizeof (nb), NULL); 1976 if (filepos == (file_ptr) -1 1977 || bfd_seek (abfd, filepos, SEEK_SET) != 0 1978 || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb)) 1979 goto error_return; 1980 1981 ngnubuckets = bfd_get_32 (abfd, nb); 1982 gnusymidx = bfd_get_32 (abfd, nb + 4); 1983 bitmaskwords = bfd_get_32 (abfd, nb + 8); 1984 buckets_vma = dt_gnu_hash + 16; 1985 if (bed->s->elfclass == ELFCLASS32) 1986 buckets_vma += bitmaskwords * 4; 1987 else 1988 buckets_vma += bitmaskwords * 8; 1989 filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL); 1990 if (filepos == (file_ptr) -1 1991 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 1992 goto error_return; 1993 1994 gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize); 1995 if (gnubuckets == NULL) 1996 goto error_return; 1997 1998 for (i = 0; i < ngnubuckets; i++) 1999 if (gnubuckets[i] != 0) 2000 { 2001 if (gnubuckets[i] < gnusymidx) 2002 goto error_return; 2003 2004 if (maxchain == 0xffffffff || gnubuckets[i] > maxchain) 2005 maxchain = gnubuckets[i]; 2006 } 2007 2008 if (maxchain == 0xffffffff) 2009 { 2010 symcount = 0; 2011 goto empty_gnu_hash; 2012 } 2013 2014 maxchain -= gnusymidx; 2015 filepos = offset_from_vma (phdrs, phnum, 2016 (buckets_vma + 2017 4 * (ngnubuckets + maxchain)), 2018 4, NULL); 2019 if (filepos == (file_ptr) -1 2020 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2021 goto error_return; 2022 2023 do 2024 { 2025 if (bfd_read (nb, 4, abfd) != 4) 2026 goto error_return; 2027 ++maxchain; 2028 if (maxchain == 0) 2029 goto error_return; 2030 } 2031 while ((bfd_get_32 (abfd, nb) & 1) == 0); 2032 2033 filepos = offset_from_vma (phdrs, phnum, 2034 (buckets_vma + 4 * ngnubuckets), 2035 4, NULL); 2036 if (filepos == (file_ptr) -1 2037 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2038 goto error_return; 2039 2040 gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize); 2041 if (gnuchains == NULL) 2042 goto error_return; 2043 ngnuchains = maxchain; 2044 2045 if (dt_mips_xhash) 2046 { 2047 filepos = offset_from_vma (phdrs, phnum, 2048 (buckets_vma 2049 + 4 * (ngnubuckets + maxchain)), 2050 4, NULL); 2051 if (filepos == (file_ptr) -1 2052 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2053 goto error_return; 2054 2055 mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize); 2056 if (mipsxlat == NULL) 2057 goto error_return; 2058 } 2059 2060 symcount = 0; 2061 for (i = 0; i < ngnubuckets; ++i) 2062 if (gnubuckets[i] != 0) 2063 { 2064 bfd_vma si = gnubuckets[i]; 2065 bfd_vma off = si - gnusymidx; 2066 do 2067 { 2068 if (mipsxlat) 2069 { 2070 if (mipsxlat[off] >= symcount) 2071 symcount = mipsxlat[off] + 1; 2072 } 2073 else 2074 { 2075 if (si >= symcount) 2076 symcount = si + 1; 2077 } 2078 si++; 2079 } 2080 while (off < ngnuchains && (gnuchains[off++] & 1) == 0); 2081 } 2082 } 2083 2084 /* Swap in dynamic symbol table. */ 2085 if (_bfd_mul_overflow (symcount, extsym_size, &amt)) 2086 { 2087 bfd_set_error (bfd_error_file_too_big); 2088 goto error_return; 2089 } 2090 2091 filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL); 2092 if (filepos == (file_ptr) -1 2093 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2094 goto error_return; 2095 esymbuf_size = amt; 2096 esymbuf = _bfd_mmap_temporary (abfd, esymbuf_size, 2097 &esymbuf_addr, &esymbuf_size); 2098 if (esymbuf == NULL) 2099 goto error_return; 2100 2101 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt)) 2102 { 2103 bfd_set_error (bfd_error_file_too_big); 2104 goto error_return; 2105 } 2106 2107 /* Dynamic symbol table must be valid until ABFD is closed. */ 2108 isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt); 2109 if (isymbuf == NULL) 2110 goto error_return; 2111 2112 swap_symbol_in = bed->s->swap_symbol_in; 2113 2114 /* Convert the symbols to internal form. */ 2115 isymend = isymbuf + symcount; 2116 for (esym = esymbuf, isym = isymbuf; 2117 isym < isymend; 2118 esym += extsym_size, isym++) 2119 if (!swap_symbol_in (abfd, esym, NULL, isym) 2120 || isym->st_name >= dt_strsz) 2121 { 2122 bfd_set_error (bfd_error_invalid_operation); 2123 goto error_return; 2124 } 2125 2126 if (dt_versym) 2127 { 2128 /* Swap in DT_VERSYM. */ 2129 if (_bfd_mul_overflow (symcount, 2, &amt)) 2130 { 2131 bfd_set_error (bfd_error_file_too_big); 2132 goto error_return; 2133 } 2134 2135 filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL); 2136 if (filepos == (file_ptr) -1 2137 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2138 goto error_return; 2139 2140 /* DT_VERSYM info must be valid until ABFD is closed. */ 2141 versym = _bfd_mmap_persistent (abfd, amt); 2142 2143 if (dt_verdef) 2144 { 2145 /* Read in DT_VERDEF. */ 2146 filepos = offset_from_vma (phdrs, phnum, dt_verdef, 2147 0, &verdef_size); 2148 if (filepos == (file_ptr) -1 2149 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2150 goto error_return; 2151 2152 /* DT_VERDEF info must be valid until ABFD is closed. */ 2153 verdef = _bfd_mmap_persistent (abfd, verdef_size); 2154 } 2155 2156 if (dt_verneed) 2157 { 2158 /* Read in DT_VERNEED. */ 2159 filepos = offset_from_vma (phdrs, phnum, dt_verneed, 2160 0, &verneed_size); 2161 if (filepos == (file_ptr) -1 2162 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2163 goto error_return; 2164 2165 /* DT_VERNEED info must be valid until ABFD is closed. */ 2166 verneed = _bfd_mmap_persistent (abfd, verneed_size); 2167 } 2168 } 2169 2170 empty_gnu_hash: 2171 elf_tdata (abfd)->dt_strtab = strbuf; 2172 elf_tdata (abfd)->dt_strsz = dt_strsz; 2173 elf_tdata (abfd)->dt_symtab = isymbuf; 2174 elf_tdata (abfd)->dt_symtab_count = symcount; 2175 elf_tdata (abfd)->dt_versym = versym; 2176 elf_tdata (abfd)->dt_verdef = verdef; 2177 elf_tdata (abfd)->dt_verneed = verneed; 2178 elf_tdata (abfd)->dt_verdef_count 2179 = verdef_size / sizeof (Elf_External_Verdef); 2180 elf_tdata (abfd)->dt_verneed_count 2181 = verneed_size / sizeof (Elf_External_Verneed); 2182 2183 res = true; 2184 2185 error_return: 2186 /* Restore file position for elf_object_p. */ 2187 if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0) 2188 res = false; 2189 _bfd_munmap_temporary (dynbuf_addr, dynbuf_size); 2190 _bfd_munmap_temporary (esymbuf_addr, esymbuf_size); 2191 free (gnubuckets); 2192 free (gnuchains); 2193 free (mipsxlat); 2194 return res; 2195 } 2196 2197 /* Reconstruct section from dynamic symbol. */ 2198 2199 asection * 2200 _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd, 2201 Elf_Internal_Sym *isym) 2202 { 2203 asection *sec; 2204 flagword flags; 2205 2206 if (!elf_use_dt_symtab_p (abfd)) 2207 return NULL; 2208 2209 flags = SEC_ALLOC | SEC_LOAD; 2210 switch (ELF_ST_TYPE (isym->st_info)) 2211 { 2212 case STT_FUNC: 2213 case STT_GNU_IFUNC: 2214 sec = bfd_get_section_by_name (abfd, ".text"); 2215 if (sec == NULL) 2216 sec = bfd_make_section_with_flags (abfd, 2217 ".text", 2218 flags | SEC_CODE); 2219 break; 2220 case STT_COMMON: 2221 sec = bfd_com_section_ptr; 2222 break; 2223 case STT_OBJECT: 2224 sec = bfd_get_section_by_name (abfd, ".data"); 2225 if (sec == NULL) 2226 sec = bfd_make_section_with_flags (abfd, 2227 ".data", 2228 flags | SEC_DATA); 2229 break; 2230 case STT_TLS: 2231 sec = bfd_get_section_by_name (abfd, ".tdata"); 2232 if (sec == NULL) 2233 sec = bfd_make_section_with_flags (abfd, 2234 ".tdata", 2235 (flags 2236 | SEC_DATA 2237 | SEC_THREAD_LOCAL)); 2238 break; 2239 default: 2240 sec = bfd_abs_section_ptr; 2241 break; 2242 } 2243 2244 return sec; 2245 } 2246 2247 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE 2248 and return symbol version for symbol version itself. */ 2249 2250 const char * 2251 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, 2252 bool base_p, 2253 bool *hidden) 2254 { 2255 const char *version_string = NULL; 2256 if ((elf_dynversym (abfd) != 0 2257 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) 2258 || (elf_tdata (abfd)->dt_versym != NULL 2259 && (elf_tdata (abfd)->dt_verdef != NULL 2260 || elf_tdata (abfd)->dt_verneed != NULL))) 2261 { 2262 unsigned int vernum = ((elf_symbol_type *) symbol)->version; 2263 2264 *hidden = (vernum & VERSYM_HIDDEN) != 0; 2265 vernum &= VERSYM_VERSION; 2266 2267 if (vernum == 0) 2268 version_string = ""; 2269 else if (vernum == 1 2270 && (vernum > elf_tdata (abfd)->cverdefs 2271 || (elf_tdata (abfd)->verdef[0].vd_flags 2272 == VER_FLG_BASE))) 2273 version_string = base_p ? "Base" : ""; 2274 else if (vernum <= elf_tdata (abfd)->cverdefs) 2275 { 2276 const char *nodename 2277 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 2278 version_string = ""; 2279 if (base_p 2280 || nodename == NULL 2281 || symbol->name == NULL 2282 || strcmp (symbol->name, nodename) != 0) 2283 version_string = nodename; 2284 } 2285 else 2286 { 2287 Elf_Internal_Verneed *t; 2288 2289 version_string = _("<corrupt>"); 2290 for (t = elf_tdata (abfd)->verref; 2291 t != NULL; 2292 t = t->vn_nextref) 2293 { 2294 Elf_Internal_Vernaux *a; 2295 2296 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 2297 { 2298 if (a->vna_other == vernum) 2299 { 2300 *hidden = true; 2301 version_string = a->vna_nodename; 2302 break; 2303 } 2304 } 2305 } 2306 } 2307 } 2308 return version_string; 2309 } 2310 2311 /* Display ELF-specific fields of a symbol. */ 2312 2313 void 2314 bfd_elf_print_symbol (bfd *abfd, 2315 void *filep, 2316 asymbol *symbol, 2317 bfd_print_symbol_type how) 2318 { 2319 FILE *file = (FILE *) filep; 2320 const char *symname = (symbol->name != bfd_symbol_error_name 2321 ? symbol->name : _("<corrupt>")); 2322 2323 switch (how) 2324 { 2325 case bfd_print_symbol_name: 2326 fprintf (file, "%s", symname); 2327 break; 2328 case bfd_print_symbol_more: 2329 fprintf (file, "elf "); 2330 bfd_fprintf_vma (abfd, file, symbol->value); 2331 fprintf (file, " %x", symbol->flags); 2332 break; 2333 case bfd_print_symbol_all: 2334 { 2335 const char *section_name; 2336 const char *name = NULL; 2337 const struct elf_backend_data *bed; 2338 unsigned char st_other; 2339 bfd_vma val; 2340 const char *version_string; 2341 bool hidden; 2342 2343 section_name = symbol->section ? symbol->section->name : "(*none*)"; 2344 2345 bed = get_elf_backend_data (abfd); 2346 if (bed->elf_backend_print_symbol_all) 2347 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); 2348 2349 if (name != NULL) 2350 symname = name; 2351 else 2352 bfd_print_symbol_vandf (abfd, file, symbol); 2353 2354 fprintf (file, " %s\t", section_name); 2355 /* Print the "other" value for a symbol. For common symbols, 2356 we've already printed the size; now print the alignment. 2357 For other symbols, we have no specified alignment, and 2358 we've printed the address; now print the size. */ 2359 if (symbol->section && bfd_is_com_section (symbol->section)) 2360 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; 2361 else 2362 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; 2363 bfd_fprintf_vma (abfd, file, val); 2364 2365 /* If we have version information, print it. */ 2366 version_string = _bfd_elf_get_symbol_version_string (abfd, 2367 symbol, 2368 true, 2369 &hidden); 2370 if (version_string) 2371 { 2372 if (!hidden) 2373 fprintf (file, " %-11s", version_string); 2374 else 2375 { 2376 int i; 2377 2378 fprintf (file, " (%s)", version_string); 2379 for (i = 10 - strlen (version_string); i > 0; --i) 2380 putc (' ', file); 2381 } 2382 } 2383 2384 /* If the st_other field is not zero, print it. */ 2385 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; 2386 2387 switch (st_other) 2388 { 2389 case 0: break; 2390 case STV_INTERNAL: fprintf (file, " .internal"); break; 2391 case STV_HIDDEN: fprintf (file, " .hidden"); break; 2392 case STV_PROTECTED: fprintf (file, " .protected"); break; 2393 default: 2394 /* Some other non-defined flags are also present, so print 2395 everything hex. */ 2396 fprintf (file, " 0x%02x", (unsigned int) st_other); 2397 } 2398 2399 fprintf (file, " %s", symname); 2400 } 2401 break; 2402 } 2403 } 2404 2405 /* ELF .o/exec file reading */ 2407 2408 /* Create a new bfd section from an ELF section header. */ 2409 2410 bool 2411 bfd_section_from_shdr (bfd *abfd, unsigned int shindex) 2412 { 2413 Elf_Internal_Shdr *hdr; 2414 Elf_Internal_Ehdr *ehdr; 2415 const struct elf_backend_data *bed; 2416 const char *name; 2417 bool ret = true; 2418 2419 if (shindex >= elf_numsections (abfd)) 2420 return false; 2421 2422 /* PR17512: A corrupt ELF binary might contain a loop of sections via 2423 sh_link or sh_info. Detect this here, by refusing to load a 2424 section that we are already in the process of loading. */ 2425 if (elf_tdata (abfd)->being_created[shindex]) 2426 { 2427 _bfd_error_handler 2428 (_("%pB: warning: loop in section dependencies detected"), abfd); 2429 return false; 2430 } 2431 elf_tdata (abfd)->being_created[shindex] = true; 2432 2433 hdr = elf_elfsections (abfd)[shindex]; 2434 ehdr = elf_elfheader (abfd); 2435 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, 2436 hdr->sh_name); 2437 if (name == NULL) 2438 goto fail; 2439 2440 bed = get_elf_backend_data (abfd); 2441 switch (hdr->sh_type) 2442 { 2443 case SHT_NULL: 2444 /* Inactive section. Throw it away. */ 2445 goto success; 2446 2447 case SHT_PROGBITS: /* Normal section with contents. */ 2448 case SHT_NOBITS: /* .bss section. */ 2449 case SHT_HASH: /* .hash section. */ 2450 case SHT_NOTE: /* .note section. */ 2451 case SHT_INIT_ARRAY: /* .init_array section. */ 2452 case SHT_FINI_ARRAY: /* .fini_array section. */ 2453 case SHT_PREINIT_ARRAY: /* .preinit_array section. */ 2454 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ 2455 case SHT_GNU_HASH: /* .gnu.hash section. */ 2456 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2457 goto success; 2458 2459 case SHT_DYNAMIC: /* Dynamic linking information. */ 2460 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2461 goto fail; 2462 2463 if (hdr->sh_link > elf_numsections (abfd)) 2464 { 2465 /* PR 10478: Accept Solaris binaries with a sh_link field 2466 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */ 2467 switch (bfd_get_arch (abfd)) 2468 { 2469 case bfd_arch_i386: 2470 case bfd_arch_sparc: 2471 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) 2472 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff)) 2473 break; 2474 /* Otherwise fall through. */ 2475 default: 2476 goto fail; 2477 } 2478 } 2479 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) 2480 goto fail; 2481 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) 2482 { 2483 Elf_Internal_Shdr *dynsymhdr; 2484 2485 /* The shared libraries distributed with hpux11 have a bogus 2486 sh_link field for the ".dynamic" section. Find the 2487 string table for the ".dynsym" section instead. */ 2488 if (elf_dynsymtab (abfd) != 0) 2489 { 2490 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; 2491 hdr->sh_link = dynsymhdr->sh_link; 2492 } 2493 else 2494 { 2495 unsigned int i, num_sec; 2496 2497 num_sec = elf_numsections (abfd); 2498 for (i = 1; i < num_sec; i++) 2499 { 2500 dynsymhdr = elf_elfsections (abfd)[i]; 2501 if (dynsymhdr->sh_type == SHT_DYNSYM) 2502 { 2503 hdr->sh_link = dynsymhdr->sh_link; 2504 break; 2505 } 2506 } 2507 } 2508 } 2509 goto success; 2510 2511 case SHT_SYMTAB: /* A symbol table. */ 2512 if (elf_onesymtab (abfd) == shindex) 2513 goto success; 2514 2515 if (hdr->sh_entsize != bed->s->sizeof_sym) 2516 goto fail; 2517 2518 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) 2519 { 2520 if (hdr->sh_size != 0) 2521 goto fail; 2522 /* Some assemblers erroneously set sh_info to one with a 2523 zero sh_size. ld sees this as a global symbol count 2524 of (unsigned) -1. Fix it here. */ 2525 hdr->sh_info = 0; 2526 goto success; 2527 } 2528 2529 /* PR 18854: A binary might contain more than one symbol table. 2530 Unusual, but possible. Warn, but continue. */ 2531 if (elf_onesymtab (abfd) != 0) 2532 { 2533 _bfd_error_handler 2534 /* xgettext:c-format */ 2535 (_("%pB: warning: multiple symbol tables detected" 2536 " - ignoring the table in section %u"), 2537 abfd, shindex); 2538 goto success; 2539 } 2540 elf_onesymtab (abfd) = shindex; 2541 elf_symtab_hdr (abfd) = *hdr; 2542 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); 2543 abfd->flags |= HAS_SYMS; 2544 2545 /* Sometimes a shared object will map in the symbol table. If 2546 SHF_ALLOC is set, and this is a shared object, then we also 2547 treat this section as a BFD section. We can not base the 2548 decision purely on SHF_ALLOC, because that flag is sometimes 2549 set in a relocatable object file, which would confuse the 2550 linker. */ 2551 if ((hdr->sh_flags & SHF_ALLOC) != 0 2552 && (abfd->flags & DYNAMIC) != 0 2553 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2554 shindex)) 2555 goto fail; 2556 2557 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we 2558 can't read symbols without that section loaded as well. It 2559 is most likely specified by the next section header. */ 2560 { 2561 elf_section_list * entry; 2562 unsigned int i, num_sec; 2563 2564 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next) 2565 if (entry->hdr.sh_link == shindex) 2566 goto success; 2567 2568 num_sec = elf_numsections (abfd); 2569 for (i = shindex + 1; i < num_sec; i++) 2570 { 2571 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2572 2573 if (hdr2->sh_type == SHT_SYMTAB_SHNDX 2574 && hdr2->sh_link == shindex) 2575 break; 2576 } 2577 2578 if (i == num_sec) 2579 for (i = 1; i < shindex; i++) 2580 { 2581 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2582 2583 if (hdr2->sh_type == SHT_SYMTAB_SHNDX 2584 && hdr2->sh_link == shindex) 2585 break; 2586 } 2587 2588 if (i != shindex) 2589 ret = bfd_section_from_shdr (abfd, i); 2590 /* else FIXME: we have failed to find the symbol table. 2591 Should we issue an error? */ 2592 goto success; 2593 } 2594 2595 case SHT_DYNSYM: /* A dynamic symbol table. */ 2596 if (elf_dynsymtab (abfd) == shindex) 2597 goto success; 2598 2599 if (hdr->sh_entsize != bed->s->sizeof_sym) 2600 goto fail; 2601 2602 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) 2603 { 2604 if (hdr->sh_size != 0) 2605 goto fail; 2606 2607 /* Some linkers erroneously set sh_info to one with a 2608 zero sh_size. ld sees this as a global symbol count 2609 of (unsigned) -1. Fix it here. */ 2610 hdr->sh_info = 0; 2611 goto success; 2612 } 2613 2614 /* PR 18854: A binary might contain more than one dynamic symbol table. 2615 Unusual, but possible. Warn, but continue. */ 2616 if (elf_dynsymtab (abfd) != 0) 2617 { 2618 _bfd_error_handler 2619 /* xgettext:c-format */ 2620 (_("%pB: warning: multiple dynamic symbol tables detected" 2621 " - ignoring the table in section %u"), 2622 abfd, shindex); 2623 goto success; 2624 } 2625 elf_dynsymtab (abfd) = shindex; 2626 elf_tdata (abfd)->dynsymtab_hdr = *hdr; 2627 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; 2628 abfd->flags |= HAS_SYMS; 2629 2630 /* Besides being a symbol table, we also treat this as a regular 2631 section, so that objcopy can handle it. */ 2632 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2633 goto success; 2634 2635 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ 2636 { 2637 elf_section_list * entry; 2638 2639 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next) 2640 if (entry->ndx == shindex) 2641 goto success; 2642 2643 entry = bfd_alloc (abfd, sizeof (*entry)); 2644 if (entry == NULL) 2645 goto fail; 2646 entry->ndx = shindex; 2647 entry->hdr = * hdr; 2648 entry->next = elf_symtab_shndx_list (abfd); 2649 elf_symtab_shndx_list (abfd) = entry; 2650 elf_elfsections (abfd)[shindex] = & entry->hdr; 2651 goto success; 2652 } 2653 2654 case SHT_STRTAB: /* A string table. */ 2655 if (hdr->bfd_section != NULL) 2656 goto success; 2657 2658 if (ehdr->e_shstrndx == shindex) 2659 { 2660 elf_tdata (abfd)->shstrtab_hdr = *hdr; 2661 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; 2662 goto success; 2663 } 2664 2665 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) 2666 { 2667 symtab_strtab: 2668 elf_tdata (abfd)->strtab_hdr = *hdr; 2669 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; 2670 goto success; 2671 } 2672 2673 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) 2674 { 2675 dynsymtab_strtab: 2676 elf_tdata (abfd)->dynstrtab_hdr = *hdr; 2677 hdr = &elf_tdata (abfd)->dynstrtab_hdr; 2678 elf_elfsections (abfd)[shindex] = hdr; 2679 /* We also treat this as a regular section, so that objcopy 2680 can handle it. */ 2681 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2682 shindex); 2683 goto success; 2684 } 2685 2686 /* If the string table isn't one of the above, then treat it as a 2687 regular section. We need to scan all the headers to be sure, 2688 just in case this strtab section appeared before the above. */ 2689 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) 2690 { 2691 unsigned int i, num_sec; 2692 2693 num_sec = elf_numsections (abfd); 2694 for (i = 1; i < num_sec; i++) 2695 { 2696 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2697 if (hdr2->sh_link == shindex) 2698 { 2699 /* Prevent endless recursion on broken objects. */ 2700 if (i == shindex) 2701 goto fail; 2702 if (! bfd_section_from_shdr (abfd, i)) 2703 goto fail; 2704 if (elf_onesymtab (abfd) == i) 2705 goto symtab_strtab; 2706 if (elf_dynsymtab (abfd) == i) 2707 goto dynsymtab_strtab; 2708 } 2709 } 2710 } 2711 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2712 goto success; 2713 2714 case SHT_REL: 2715 case SHT_RELA: 2716 case SHT_RELR: 2717 /* *These* do a lot of work -- but build no sections! */ 2718 { 2719 asection *target_sect; 2720 Elf_Internal_Shdr *hdr2, **p_hdr; 2721 unsigned int num_sec = elf_numsections (abfd); 2722 struct bfd_elf_section_data *esdt; 2723 bfd_size_type size; 2724 2725 if (hdr->sh_type == SHT_REL) 2726 size = bed->s->sizeof_rel; 2727 else if (hdr->sh_type == SHT_RELA) 2728 size = bed->s->sizeof_rela; 2729 else 2730 size = bed->s->arch_size / 8; 2731 if (hdr->sh_entsize != size) 2732 goto fail; 2733 2734 /* Check for a bogus link to avoid crashing. */ 2735 if (hdr->sh_link >= num_sec) 2736 { 2737 _bfd_error_handler 2738 /* xgettext:c-format */ 2739 (_("%pB: invalid link %u for reloc section %s (index %u)"), 2740 abfd, hdr->sh_link, name, shindex); 2741 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2742 goto success; 2743 } 2744 2745 /* Get the symbol table. */ 2746 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB 2747 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) 2748 && ! bfd_section_from_shdr (abfd, hdr->sh_link)) 2749 goto fail; 2750 2751 /* If this is an alloc section in an executable or shared 2752 library, or the reloc section does not use the main symbol 2753 table we don't treat it as a reloc section. BFD can't 2754 adequately represent such a section, so at least for now, 2755 we don't try. We just present it as a normal section. We 2756 also can't use it as a reloc section if it points to the 2757 null section, an invalid section, another reloc section, or 2758 its sh_link points to the null section. */ 2759 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0 2760 && (hdr->sh_flags & SHF_ALLOC) != 0) 2761 || (hdr->sh_flags & SHF_COMPRESSED) != 0 2762 || hdr->sh_type == SHT_RELR 2763 || hdr->sh_link == SHN_UNDEF 2764 || hdr->sh_link != elf_onesymtab (abfd) 2765 || hdr->sh_info == SHN_UNDEF 2766 || hdr->sh_info >= num_sec 2767 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL 2768 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) 2769 { 2770 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2771 goto success; 2772 } 2773 2774 if (! bfd_section_from_shdr (abfd, hdr->sh_info)) 2775 goto fail; 2776 2777 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); 2778 if (target_sect == NULL) 2779 goto fail; 2780 2781 esdt = elf_section_data (target_sect); 2782 if (hdr->sh_type == SHT_RELA) 2783 p_hdr = &esdt->rela.hdr; 2784 else 2785 p_hdr = &esdt->rel.hdr; 2786 2787 /* PR 17512: file: 0b4f81b7. 2788 Also see PR 24456, for a file which deliberately has two reloc 2789 sections. */ 2790 if (*p_hdr != NULL) 2791 { 2792 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex)) 2793 { 2794 _bfd_error_handler 2795 /* xgettext:c-format */ 2796 (_("%pB: warning: secondary relocation section '%s' " 2797 "for section %pA found - ignoring"), 2798 abfd, name, target_sect); 2799 } 2800 else 2801 esdt->has_secondary_relocs = true; 2802 goto success; 2803 } 2804 2805 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); 2806 if (hdr2 == NULL) 2807 goto fail; 2808 *hdr2 = *hdr; 2809 *p_hdr = hdr2; 2810 elf_elfsections (abfd)[shindex] = hdr2; 2811 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr) 2812 * bed->s->int_rels_per_ext_rel); 2813 target_sect->flags |= SEC_RELOC; 2814 target_sect->relocation = NULL; 2815 target_sect->rel_filepos = hdr->sh_offset; 2816 /* In the section to which the relocations apply, mark whether 2817 its relocations are of the REL or RELA variety. */ 2818 if (hdr->sh_size != 0) 2819 { 2820 if (hdr->sh_type == SHT_RELA) 2821 target_sect->use_rela_p = 1; 2822 } 2823 abfd->flags |= HAS_RELOC; 2824 goto success; 2825 } 2826 2827 case SHT_GNU_verdef: 2828 if (hdr->sh_info != 0) 2829 elf_dynverdef (abfd) = shindex; 2830 elf_tdata (abfd)->dynverdef_hdr = *hdr; 2831 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2832 goto success; 2833 2834 case SHT_GNU_versym: 2835 if (hdr->sh_entsize != sizeof (Elf_External_Versym)) 2836 goto fail; 2837 2838 elf_dynversym (abfd) = shindex; 2839 elf_tdata (abfd)->dynversym_hdr = *hdr; 2840 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2841 goto success; 2842 2843 case SHT_GNU_verneed: 2844 if (hdr->sh_info != 0) 2845 elf_dynverref (abfd) = shindex; 2846 elf_tdata (abfd)->dynverref_hdr = *hdr; 2847 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2848 goto success; 2849 2850 case SHT_SHLIB: 2851 goto success; 2852 2853 case SHT_GROUP: 2854 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2855 goto fail; 2856 2857 goto success; 2858 2859 default: 2860 /* Possibly an attributes section. */ 2861 if (hdr->sh_type == SHT_GNU_ATTRIBUTES 2862 || hdr->sh_type == bed->obj_attrs_section_type) 2863 { 2864 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2865 goto fail; 2866 _bfd_elf_parse_attributes (abfd, hdr); 2867 goto success; 2868 } 2869 2870 /* Check for any processor-specific section types. */ 2871 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) 2872 goto success; 2873 2874 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) 2875 { 2876 if ((hdr->sh_flags & SHF_ALLOC) != 0) 2877 /* FIXME: How to properly handle allocated section reserved 2878 for applications? */ 2879 _bfd_error_handler 2880 /* xgettext:c-format */ 2881 (_("%pB: unknown type [%#x] section `%s'"), 2882 abfd, hdr->sh_type, name); 2883 else 2884 { 2885 /* Allow sections reserved for applications. */ 2886 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2887 goto success; 2888 } 2889 } 2890 else if (hdr->sh_type >= SHT_LOPROC 2891 && hdr->sh_type <= SHT_HIPROC) 2892 /* FIXME: We should handle this section. */ 2893 _bfd_error_handler 2894 /* xgettext:c-format */ 2895 (_("%pB: unknown type [%#x] section `%s'"), 2896 abfd, hdr->sh_type, name); 2897 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) 2898 { 2899 /* Unrecognised OS-specific sections. */ 2900 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) 2901 /* SHF_OS_NONCONFORMING indicates that special knowledge is 2902 required to correctly process the section and the file should 2903 be rejected with an error message. */ 2904 _bfd_error_handler 2905 /* xgettext:c-format */ 2906 (_("%pB: unknown type [%#x] section `%s'"), 2907 abfd, hdr->sh_type, name); 2908 else 2909 { 2910 /* Otherwise it should be processed. */ 2911 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2912 goto success; 2913 } 2914 } 2915 else 2916 /* FIXME: We should handle this section. */ 2917 _bfd_error_handler 2918 /* xgettext:c-format */ 2919 (_("%pB: unknown type [%#x] section `%s'"), 2920 abfd, hdr->sh_type, name); 2921 2922 goto fail; 2923 } 2924 2925 fail: 2926 ret = false; 2927 success: 2928 elf_tdata (abfd)->being_created[shindex] = false; 2929 return ret; 2930 } 2931 2932 /* Return the local symbol specified by ABFD, R_SYMNDX. */ 2933 2934 Elf_Internal_Sym * 2935 bfd_sym_from_r_symndx (struct sym_cache *cache, 2936 bfd *abfd, 2937 unsigned long r_symndx) 2938 { 2939 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; 2940 2941 if (cache->abfd != abfd || cache->indx[ent] != r_symndx) 2942 { 2943 Elf_Internal_Shdr *symtab_hdr; 2944 unsigned char esym[sizeof (Elf64_External_Sym)]; 2945 Elf_External_Sym_Shndx eshndx; 2946 2947 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2948 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, 2949 &cache->sym[ent], esym, &eshndx) == NULL) 2950 return NULL; 2951 2952 if (cache->abfd != abfd) 2953 { 2954 memset (cache->indx, -1, sizeof (cache->indx)); 2955 cache->abfd = abfd; 2956 } 2957 cache->indx[ent] = r_symndx; 2958 } 2959 2960 return &cache->sym[ent]; 2961 } 2962 2963 /* Given an ELF section number, retrieve the corresponding BFD 2964 section. */ 2965 2966 asection * 2967 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) 2968 { 2969 if (sec_index >= elf_numsections (abfd)) 2970 return NULL; 2971 return elf_elfsections (abfd)[sec_index]->bfd_section; 2972 } 2973 2974 static const struct bfd_elf_special_section special_sections_b[] = 2975 { 2976 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 2977 { NULL, 0, 0, 0, 0 } 2978 }; 2979 2980 static const struct bfd_elf_special_section special_sections_c[] = 2981 { 2982 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, 2983 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 }, 2984 { NULL, 0, 0, 0, 0 } 2985 }; 2986 2987 static const struct bfd_elf_special_section special_sections_d[] = 2988 { 2989 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 2990 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 2991 /* There are more DWARF sections than these, but they needn't be added here 2992 unless you have to cope with broken compilers that don't emit section 2993 attributes or you want to help the user writing assembler. */ 2994 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, 2995 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, 2996 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, 2997 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, 2998 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, 2999 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, 3000 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, 3001 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, 3002 { NULL, 0, 0, 0, 0 } 3003 }; 3004 3005 static const struct bfd_elf_special_section special_sections_f[] = 3006 { 3007 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3008 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, 3009 { NULL, 0 , 0, 0, 0 } 3010 }; 3011 3012 static const struct bfd_elf_special_section special_sections_g[] = 3013 { 3014 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3015 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3016 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3017 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, 3018 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3019 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, 3020 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, 3021 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, 3022 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, 3023 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, 3024 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, 3025 { NULL, 0, 0, 0, 0 } 3026 }; 3027 3028 static const struct bfd_elf_special_section special_sections_h[] = 3029 { 3030 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, 3031 { NULL, 0, 0, 0, 0 } 3032 }; 3033 3034 static const struct bfd_elf_special_section special_sections_i[] = 3035 { 3036 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3037 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, 3038 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, 3039 { NULL, 0, 0, 0, 0 } 3040 }; 3041 3042 static const struct bfd_elf_special_section special_sections_l[] = 3043 { 3044 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, 3045 { NULL, 0, 0, 0, 0 } 3046 }; 3047 3048 static const struct bfd_elf_special_section special_sections_n[] = 3049 { 3050 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3051 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, 3052 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, 3053 { NULL, 0, 0, 0, 0 } 3054 }; 3055 3056 static const struct bfd_elf_special_section special_sections_p[] = 3057 { 3058 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3059 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3060 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, 3061 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3062 { NULL, 0, 0, 0, 0 } 3063 }; 3064 3065 static const struct bfd_elf_special_section special_sections_r[] = 3066 { 3067 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, 3068 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, 3069 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC }, 3070 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, 3071 /* .relro_padding is generated by lld. It should not be confused with a 3072 reloc containing section, because otherwise elf_fake_sections() will 3073 set the entsize to 8, which may not be an actual multiple of the 3074 section's size. 3075 Note - this entry must appear before the ".rel" entry below. */ 3076 { STRING_COMMA_LEN (".relro_padding"), 0, SHT_NOBITS, SHF_ALLOC | SHF_WRITE }, 3077 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, 3078 { NULL, 0, 0, 0, 0 } 3079 }; 3080 3081 static const struct bfd_elf_special_section special_sections_s[] = 3082 { 3083 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, 3084 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, 3085 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, 3086 /* See struct bfd_elf_special_section declaration for the semantics of 3087 this special case where .prefix_length != strlen (.prefix). */ 3088 { ".stabstr", 5, 3, SHT_STRTAB, 0 }, 3089 { NULL, 0, 0, 0, 0 } 3090 }; 3091 3092 static const struct bfd_elf_special_section special_sections_t[] = 3093 { 3094 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3095 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, 3096 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, 3097 { NULL, 0, 0, 0, 0 } 3098 }; 3099 3100 static const struct bfd_elf_special_section special_sections_z[] = 3101 { 3102 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, 3103 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, 3104 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, 3105 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, 3106 { NULL, 0, 0, 0, 0 } 3107 }; 3108 3109 static const struct bfd_elf_special_section * const special_sections[] = 3110 { 3111 special_sections_b, /* 'b' */ 3112 special_sections_c, /* 'c' */ 3113 special_sections_d, /* 'd' */ 3114 NULL, /* 'e' */ 3115 special_sections_f, /* 'f' */ 3116 special_sections_g, /* 'g' */ 3117 special_sections_h, /* 'h' */ 3118 special_sections_i, /* 'i' */ 3119 NULL, /* 'j' */ 3120 NULL, /* 'k' */ 3121 special_sections_l, /* 'l' */ 3122 NULL, /* 'm' */ 3123 special_sections_n, /* 'n' */ 3124 NULL, /* 'o' */ 3125 special_sections_p, /* 'p' */ 3126 NULL, /* 'q' */ 3127 special_sections_r, /* 'r' */ 3128 special_sections_s, /* 's' */ 3129 special_sections_t, /* 't' */ 3130 NULL, /* 'u' */ 3131 NULL, /* 'v' */ 3132 NULL, /* 'w' */ 3133 NULL, /* 'x' */ 3134 NULL, /* 'y' */ 3135 special_sections_z /* 'z' */ 3136 }; 3137 3138 const struct bfd_elf_special_section * 3139 _bfd_elf_get_special_section (const char *name, 3140 const struct bfd_elf_special_section *spec, 3141 unsigned int rela) 3142 { 3143 int i; 3144 int len; 3145 3146 len = strlen (name); 3147 3148 for (i = 0; spec[i].prefix != NULL; i++) 3149 { 3150 int suffix_len; 3151 int prefix_len = spec[i].prefix_length; 3152 3153 if (len < prefix_len) 3154 continue; 3155 if (memcmp (name, spec[i].prefix, prefix_len) != 0) 3156 continue; 3157 3158 suffix_len = spec[i].suffix_length; 3159 if (suffix_len <= 0) 3160 { 3161 if (name[prefix_len] != 0) 3162 { 3163 if (suffix_len == 0) 3164 continue; 3165 if (name[prefix_len] != '.' 3166 && (suffix_len == -2 3167 || (rela && spec[i].type == SHT_REL))) 3168 continue; 3169 } 3170 } 3171 else 3172 { 3173 if (len < prefix_len + suffix_len) 3174 continue; 3175 if (memcmp (name + len - suffix_len, 3176 spec[i].prefix + prefix_len, 3177 suffix_len) != 0) 3178 continue; 3179 } 3180 return &spec[i]; 3181 } 3182 3183 return NULL; 3184 } 3185 3186 const struct bfd_elf_special_section * 3187 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) 3188 { 3189 int i; 3190 const struct bfd_elf_special_section *spec; 3191 const struct elf_backend_data *bed; 3192 3193 /* See if this is one of the special sections. */ 3194 if (sec->name == NULL) 3195 return NULL; 3196 3197 bed = get_elf_backend_data (abfd); 3198 spec = bed->special_sections; 3199 if (spec) 3200 { 3201 spec = _bfd_elf_get_special_section (sec->name, 3202 bed->special_sections, 3203 sec->use_rela_p); 3204 if (spec != NULL) 3205 return spec; 3206 } 3207 3208 if (sec->name[0] != '.') 3209 return NULL; 3210 3211 i = sec->name[1] - 'b'; 3212 if (i < 0 || i > 'z' - 'b') 3213 return NULL; 3214 3215 spec = special_sections[i]; 3216 3217 if (spec == NULL) 3218 return NULL; 3219 3220 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); 3221 } 3222 3223 bool 3224 _bfd_elf_new_section_hook (bfd *abfd, asection *sec) 3225 { 3226 struct bfd_elf_section_data *sdata; 3227 const struct elf_backend_data *bed; 3228 const struct bfd_elf_special_section *ssect; 3229 3230 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; 3231 if (sdata == NULL) 3232 { 3233 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, 3234 sizeof (*sdata)); 3235 if (sdata == NULL) 3236 return false; 3237 sec->used_by_bfd = sdata; 3238 } 3239 3240 /* Indicate whether or not this section should use RELA relocations. */ 3241 bed = get_elf_backend_data (abfd); 3242 sec->use_rela_p = bed->default_use_rela_p; 3243 3244 /* Set up ELF section type and flags for newly created sections, if 3245 there is an ABI mandated section. */ 3246 ssect = (*bed->get_sec_type_attr) (abfd, sec); 3247 if (ssect != NULL) 3248 { 3249 elf_section_type (sec) = ssect->type; 3250 elf_section_flags (sec) = ssect->attr; 3251 } 3252 3253 return _bfd_generic_new_section_hook (abfd, sec); 3254 } 3255 3256 /* Create a new bfd section from an ELF program header. 3257 3258 Since program segments have no names, we generate a synthetic name 3259 of the form segment<NUM>, where NUM is generally the index in the 3260 program header table. For segments that are split (see below) we 3261 generate the names segment<NUM>a and segment<NUM>b. 3262 3263 Note that some program segments may have a file size that is different than 3264 (less than) the memory size. All this means is that at execution the 3265 system must allocate the amount of memory specified by the memory size, 3266 but only initialize it with the first "file size" bytes read from the 3267 file. This would occur for example, with program segments consisting 3268 of combined data+bss. 3269 3270 To handle the above situation, this routine generates TWO bfd sections 3271 for the single program segment. The first has the length specified by 3272 the file size of the segment, and the second has the length specified 3273 by the difference between the two sizes. In effect, the segment is split 3274 into its initialized and uninitialized parts. */ 3275 3276 bool 3277 _bfd_elf_make_section_from_phdr (bfd *abfd, 3278 Elf_Internal_Phdr *hdr, 3279 int hdr_index, 3280 const char *type_name) 3281 { 3282 asection *newsect; 3283 char *name; 3284 char namebuf[64]; 3285 size_t len; 3286 int split; 3287 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 3288 3289 split = ((hdr->p_memsz > 0) 3290 && (hdr->p_filesz > 0) 3291 && (hdr->p_memsz > hdr->p_filesz)); 3292 3293 if (hdr->p_filesz > 0) 3294 { 3295 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); 3296 len = strlen (namebuf) + 1; 3297 name = (char *) bfd_alloc (abfd, len); 3298 if (!name) 3299 return false; 3300 memcpy (name, namebuf, len); 3301 newsect = bfd_make_section (abfd, name); 3302 if (newsect == NULL) 3303 return false; 3304 newsect->vma = hdr->p_vaddr / opb; 3305 newsect->lma = hdr->p_paddr / opb; 3306 newsect->size = hdr->p_filesz; 3307 newsect->filepos = hdr->p_offset; 3308 newsect->flags |= SEC_HAS_CONTENTS; 3309 newsect->alignment_power = bfd_log2 (hdr->p_align); 3310 if (hdr->p_type == PT_LOAD) 3311 { 3312 newsect->flags |= SEC_ALLOC; 3313 newsect->flags |= SEC_LOAD; 3314 if (hdr->p_flags & PF_X) 3315 { 3316 /* FIXME: all we known is that it has execute PERMISSION, 3317 may be data. */ 3318 newsect->flags |= SEC_CODE; 3319 } 3320 } 3321 if (!(hdr->p_flags & PF_W)) 3322 { 3323 newsect->flags |= SEC_READONLY; 3324 } 3325 } 3326 3327 if (hdr->p_memsz > hdr->p_filesz) 3328 { 3329 bfd_vma align; 3330 3331 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); 3332 len = strlen (namebuf) + 1; 3333 name = (char *) bfd_alloc (abfd, len); 3334 if (!name) 3335 return false; 3336 memcpy (name, namebuf, len); 3337 newsect = bfd_make_section (abfd, name); 3338 if (newsect == NULL) 3339 return false; 3340 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb; 3341 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb; 3342 newsect->size = hdr->p_memsz - hdr->p_filesz; 3343 newsect->filepos = hdr->p_offset + hdr->p_filesz; 3344 align = newsect->vma & -newsect->vma; 3345 if (align == 0 || align > hdr->p_align) 3346 align = hdr->p_align; 3347 newsect->alignment_power = bfd_log2 (align); 3348 if (hdr->p_type == PT_LOAD) 3349 { 3350 newsect->flags |= SEC_ALLOC; 3351 if (hdr->p_flags & PF_X) 3352 newsect->flags |= SEC_CODE; 3353 } 3354 if (!(hdr->p_flags & PF_W)) 3355 newsect->flags |= SEC_READONLY; 3356 } 3357 3358 return true; 3359 } 3360 3361 static bool 3362 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset) 3363 { 3364 /* The return value is ignored. Build-ids are considered optional. */ 3365 if (templ->xvec->flavour == bfd_target_elf_flavour) 3366 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id) 3367 (templ, offset); 3368 return false; 3369 } 3370 3371 bool 3372 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) 3373 { 3374 const struct elf_backend_data *bed; 3375 3376 switch (hdr->p_type) 3377 { 3378 case PT_NULL: 3379 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); 3380 3381 case PT_LOAD: 3382 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load")) 3383 return false; 3384 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL) 3385 _bfd_elf_core_find_build_id (abfd, hdr->p_offset); 3386 return true; 3387 3388 case PT_DYNAMIC: 3389 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); 3390 3391 case PT_INTERP: 3392 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); 3393 3394 case PT_NOTE: 3395 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) 3396 return false; 3397 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz, 3398 hdr->p_align)) 3399 return false; 3400 return true; 3401 3402 case PT_SHLIB: 3403 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); 3404 3405 case PT_PHDR: 3406 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); 3407 3408 case PT_GNU_EH_FRAME: 3409 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, 3410 "eh_frame_hdr"); 3411 3412 case PT_GNU_STACK: 3413 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); 3414 3415 case PT_GNU_RELRO: 3416 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); 3417 3418 case PT_GNU_SFRAME: 3419 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, 3420 "sframe"); 3421 3422 default: 3423 /* Check for any processor-specific program segment types. */ 3424 bed = get_elf_backend_data (abfd); 3425 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); 3426 } 3427 } 3428 3429 /* Return the REL_HDR for SEC, assuming there is only a single one, either 3430 REL or RELA. */ 3431 3432 Elf_Internal_Shdr * 3433 _bfd_elf_single_rel_hdr (asection *sec) 3434 { 3435 if (elf_section_data (sec)->rel.hdr) 3436 { 3437 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); 3438 return elf_section_data (sec)->rel.hdr; 3439 } 3440 else 3441 return elf_section_data (sec)->rela.hdr; 3442 } 3443 3444 static bool 3445 _bfd_elf_set_reloc_sh_name (bfd *abfd, 3446 Elf_Internal_Shdr *rel_hdr, 3447 const char *sec_name, 3448 bool use_rela_p) 3449 { 3450 char *name = (char *) bfd_alloc (abfd, 3451 sizeof ".rela" + strlen (sec_name)); 3452 if (name == NULL) 3453 return false; 3454 3455 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); 3456 rel_hdr->sh_name = 3457 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, 3458 false); 3459 if (rel_hdr->sh_name == (unsigned int) -1) 3460 return false; 3461 3462 return true; 3463 } 3464 3465 /* Allocate and initialize a section-header for a new reloc section, 3466 containing relocations against ASECT. It is stored in RELDATA. If 3467 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL 3468 relocations. */ 3469 3470 static bool 3471 _bfd_elf_init_reloc_shdr (bfd *abfd, 3472 struct bfd_elf_section_reloc_data *reldata, 3473 const char *sec_name, 3474 bool use_rela_p, 3475 bool delay_st_name_p) 3476 { 3477 Elf_Internal_Shdr *rel_hdr; 3478 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3479 3480 BFD_ASSERT (reldata->hdr == NULL); 3481 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); 3482 if (rel_hdr == NULL) 3483 return false; 3484 reldata->hdr = rel_hdr; 3485 3486 if (delay_st_name_p) 3487 rel_hdr->sh_name = (unsigned int) -1; 3488 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, 3489 use_rela_p)) 3490 return false; 3491 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; 3492 rel_hdr->sh_entsize = (use_rela_p 3493 ? bed->s->sizeof_rela 3494 : bed->s->sizeof_rel); 3495 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 3496 rel_hdr->sh_flags = 0; 3497 rel_hdr->sh_addr = 0; 3498 rel_hdr->sh_size = 0; 3499 rel_hdr->sh_offset = 0; 3500 3501 return true; 3502 } 3503 3504 /* Return the default section type based on the passed in section flags. */ 3505 3506 int 3507 bfd_elf_get_default_section_type (flagword flags) 3508 { 3509 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0 3510 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) 3511 return SHT_NOBITS; 3512 return SHT_PROGBITS; 3513 } 3514 3515 struct fake_section_arg 3516 { 3517 struct bfd_link_info *link_info; 3518 bool failed; 3519 }; 3520 3521 /* Set up an ELF internal section header for a section. */ 3522 3523 static void 3524 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) 3525 { 3526 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; 3527 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3528 struct bfd_elf_section_data *esd = elf_section_data (asect); 3529 Elf_Internal_Shdr *this_hdr; 3530 unsigned int sh_type; 3531 const char *name = asect->name; 3532 bool delay_st_name_p = false; 3533 bfd_vma mask; 3534 3535 if (arg->failed) 3536 { 3537 /* We already failed; just get out of the bfd_map_over_sections 3538 loop. */ 3539 return; 3540 } 3541 3542 this_hdr = &esd->this_hdr; 3543 3544 /* ld: compress DWARF debug sections with names: .debug_*. */ 3545 if (arg->link_info 3546 && (abfd->flags & BFD_COMPRESS) != 0 3547 && (asect->flags & SEC_DEBUGGING) != 0 3548 && name[1] == 'd' 3549 && name[6] == '_') 3550 { 3551 /* If this section will be compressed, delay adding section 3552 name to section name section after it is compressed in 3553 _bfd_elf_assign_file_positions_for_non_load. */ 3554 delay_st_name_p = true; 3555 } 3556 3557 if (delay_st_name_p) 3558 this_hdr->sh_name = (unsigned int) -1; 3559 else 3560 { 3561 this_hdr->sh_name 3562 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 3563 name, false); 3564 if (this_hdr->sh_name == (unsigned int) -1) 3565 { 3566 arg->failed = true; 3567 return; 3568 } 3569 } 3570 3571 /* Don't clear sh_flags. Assembler may set additional bits. */ 3572 3573 if ((asect->flags & SEC_ALLOC) != 0 3574 || asect->user_set_vma) 3575 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect); 3576 else 3577 this_hdr->sh_addr = 0; 3578 3579 this_hdr->sh_offset = 0; 3580 this_hdr->sh_size = asect->size; 3581 this_hdr->sh_link = 0; 3582 /* PR 17512: file: 0eb809fe, 8b0535ee. */ 3583 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) 3584 { 3585 _bfd_error_handler 3586 /* xgettext:c-format */ 3587 (_("%pB: error: alignment power %d of section `%pA' is too big"), 3588 abfd, asect->alignment_power, asect); 3589 arg->failed = true; 3590 return; 3591 } 3592 /* Set sh_addralign to the highest power of two given by alignment 3593 consistent with the section VMA. Linker scripts can force VMA. */ 3594 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr; 3595 this_hdr->sh_addralign = mask & -mask; 3596 /* The sh_entsize and sh_info fields may have been set already by 3597 copy_private_section_data. */ 3598 3599 this_hdr->bfd_section = asect; 3600 this_hdr->contents = NULL; 3601 3602 /* If the section type is unspecified, we set it based on 3603 asect->flags. */ 3604 if (asect->type != 0) 3605 sh_type = asect->type; 3606 else if ((asect->flags & SEC_GROUP) != 0) 3607 sh_type = SHT_GROUP; 3608 else 3609 sh_type = bfd_elf_get_default_section_type (asect->flags); 3610 3611 if (this_hdr->sh_type == SHT_NULL) 3612 this_hdr->sh_type = sh_type; 3613 else if (this_hdr->sh_type == SHT_NOBITS 3614 && sh_type == SHT_PROGBITS 3615 && (asect->flags & SEC_ALLOC) != 0) 3616 { 3617 /* Warn if we are changing a NOBITS section to PROGBITS, but 3618 allow the link to proceed. This can happen when users link 3619 non-bss input sections to bss output sections, or emit data 3620 to a bss output section via a linker script. */ 3621 _bfd_error_handler 3622 (_("warning: section `%pA' type changed to PROGBITS"), asect); 3623 this_hdr->sh_type = sh_type; 3624 } 3625 3626 switch (this_hdr->sh_type) 3627 { 3628 default: 3629 break; 3630 3631 case SHT_STRTAB: 3632 case SHT_NOTE: 3633 case SHT_NOBITS: 3634 case SHT_PROGBITS: 3635 break; 3636 3637 case SHT_INIT_ARRAY: 3638 case SHT_FINI_ARRAY: 3639 case SHT_PREINIT_ARRAY: 3640 this_hdr->sh_entsize = bed->s->arch_size / 8; 3641 break; 3642 3643 case SHT_HASH: 3644 this_hdr->sh_entsize = bed->s->sizeof_hash_entry; 3645 break; 3646 3647 case SHT_DYNSYM: 3648 this_hdr->sh_entsize = bed->s->sizeof_sym; 3649 break; 3650 3651 case SHT_DYNAMIC: 3652 this_hdr->sh_entsize = bed->s->sizeof_dyn; 3653 break; 3654 3655 case SHT_RELA: 3656 if (get_elf_backend_data (abfd)->may_use_rela_p) 3657 this_hdr->sh_entsize = bed->s->sizeof_rela; 3658 break; 3659 3660 case SHT_REL: 3661 if (get_elf_backend_data (abfd)->may_use_rel_p) 3662 this_hdr->sh_entsize = bed->s->sizeof_rel; 3663 break; 3664 3665 case SHT_GNU_versym: 3666 this_hdr->sh_entsize = sizeof (Elf_External_Versym); 3667 break; 3668 3669 case SHT_GNU_verdef: 3670 this_hdr->sh_entsize = 0; 3671 /* objcopy or strip will copy over sh_info, but may not set 3672 cverdefs. The linker will set cverdefs, but sh_info will be 3673 zero. */ 3674 if (this_hdr->sh_info == 0) 3675 this_hdr->sh_info = elf_tdata (abfd)->cverdefs; 3676 else 3677 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 3678 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); 3679 break; 3680 3681 case SHT_GNU_verneed: 3682 this_hdr->sh_entsize = 0; 3683 /* objcopy or strip will copy over sh_info, but may not set 3684 cverrefs. The linker will set cverrefs, but sh_info will be 3685 zero. */ 3686 if (this_hdr->sh_info == 0) 3687 this_hdr->sh_info = elf_tdata (abfd)->cverrefs; 3688 else 3689 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 3690 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); 3691 break; 3692 3693 case SHT_GROUP: 3694 this_hdr->sh_entsize = GRP_ENTRY_SIZE; 3695 break; 3696 3697 case SHT_GNU_HASH: 3698 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; 3699 break; 3700 } 3701 3702 if ((asect->flags & SEC_ALLOC) != 0) 3703 this_hdr->sh_flags |= SHF_ALLOC; 3704 if ((asect->flags & SEC_READONLY) == 0) 3705 this_hdr->sh_flags |= SHF_WRITE; 3706 if ((asect->flags & SEC_CODE) != 0) 3707 this_hdr->sh_flags |= SHF_EXECINSTR; 3708 if ((asect->flags & SEC_MERGE) != 0) 3709 { 3710 this_hdr->sh_flags |= SHF_MERGE; 3711 this_hdr->sh_entsize = asect->entsize; 3712 } 3713 if ((asect->flags & SEC_STRINGS) != 0) 3714 { 3715 this_hdr->sh_flags |= SHF_STRINGS; 3716 this_hdr->sh_entsize = asect->entsize; 3717 } 3718 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) 3719 this_hdr->sh_flags |= SHF_GROUP; 3720 if ((asect->flags & SEC_THREAD_LOCAL) != 0) 3721 { 3722 this_hdr->sh_flags |= SHF_TLS; 3723 if (asect->size == 0 3724 && (asect->flags & SEC_HAS_CONTENTS) == 0) 3725 { 3726 struct bfd_link_order *o = asect->map_tail.link_order; 3727 3728 this_hdr->sh_size = 0; 3729 if (o != NULL) 3730 { 3731 this_hdr->sh_size = o->offset + o->size; 3732 if (this_hdr->sh_size != 0) 3733 this_hdr->sh_type = SHT_NOBITS; 3734 } 3735 } 3736 } 3737 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) 3738 this_hdr->sh_flags |= SHF_EXCLUDE; 3739 3740 /* If the section has relocs, set up a section header for the 3741 SHT_REL[A] section. If two relocation sections are required for 3742 this section, it is up to the processor-specific back-end to 3743 create the other. */ 3744 if ((asect->flags & SEC_RELOC) != 0) 3745 { 3746 /* When doing a relocatable link, create both REL and RELA sections if 3747 needed. */ 3748 if (arg->link_info 3749 /* Do the normal setup if we wouldn't create any sections here. */ 3750 && esd->rel.count + esd->rela.count > 0 3751 && (bfd_link_relocatable (arg->link_info) 3752 || arg->link_info->emitrelocations)) 3753 { 3754 if (esd->rel.count && esd->rel.hdr == NULL 3755 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, 3756 false, delay_st_name_p)) 3757 { 3758 arg->failed = true; 3759 return; 3760 } 3761 if (esd->rela.count && esd->rela.hdr == NULL 3762 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, 3763 true, delay_st_name_p)) 3764 { 3765 arg->failed = true; 3766 return; 3767 } 3768 } 3769 else if (!_bfd_elf_init_reloc_shdr (abfd, 3770 (asect->use_rela_p 3771 ? &esd->rela : &esd->rel), 3772 name, 3773 asect->use_rela_p, 3774 delay_st_name_p)) 3775 { 3776 arg->failed = true; 3777 return; 3778 } 3779 } 3780 3781 /* Check for processor-specific section types. */ 3782 sh_type = this_hdr->sh_type; 3783 if (bed->elf_backend_fake_sections 3784 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) 3785 { 3786 arg->failed = true; 3787 return; 3788 } 3789 3790 if (sh_type == SHT_NOBITS && asect->size != 0) 3791 { 3792 /* Don't change the header type from NOBITS if we are being 3793 called for objcopy --only-keep-debug. */ 3794 this_hdr->sh_type = sh_type; 3795 } 3796 } 3797 3798 /* Fill in the contents of a SHT_GROUP section. Called from 3799 _bfd_elf_compute_section_file_positions for gas, objcopy, and 3800 when ELF targets use the generic linker, ld. Called for ld -r 3801 from bfd_elf_final_link. */ 3802 3803 void 3804 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) 3805 { 3806 bool *failedptr = (bool *) failedptrarg; 3807 asection *elt, *first; 3808 unsigned char *loc; 3809 bool gas; 3810 3811 /* Ignore linker created group section. See elfNN_ia64_object_p in 3812 elfxx-ia64.c. */ 3813 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP 3814 || sec->size == 0 3815 || *failedptr) 3816 return; 3817 3818 if (elf_section_data (sec)->this_hdr.sh_info == 0) 3819 { 3820 unsigned long symindx = 0; 3821 3822 /* elf_group_id will have been set up by objcopy and the 3823 generic linker. */ 3824 if (elf_group_id (sec) != NULL) 3825 symindx = elf_group_id (sec)->udata.i; 3826 3827 if (symindx == 0) 3828 { 3829 /* If called from the assembler, swap_out_syms will have set up 3830 elf_section_syms. 3831 PR 25699: A corrupt input file could contain bogus group info. */ 3832 if (sec->index >= elf_num_section_syms (abfd) 3833 || elf_section_syms (abfd)[sec->index] == NULL) 3834 { 3835 *failedptr = true; 3836 return; 3837 } 3838 symindx = elf_section_syms (abfd)[sec->index]->udata.i; 3839 } 3840 elf_section_data (sec)->this_hdr.sh_info = symindx; 3841 } 3842 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) 3843 { 3844 /* The ELF backend linker sets sh_info to -2 when the group 3845 signature symbol is global, and thus the index can't be 3846 set until all local symbols are output. */ 3847 asection *igroup; 3848 struct bfd_elf_section_data *sec_data; 3849 unsigned long symndx; 3850 unsigned long extsymoff; 3851 struct elf_link_hash_entry *h; 3852 3853 /* The point of this little dance to the first SHF_GROUP section 3854 then back to the SHT_GROUP section is that this gets us to 3855 the SHT_GROUP in the input object. */ 3856 igroup = elf_sec_group (elf_next_in_group (sec)); 3857 sec_data = elf_section_data (igroup); 3858 symndx = sec_data->this_hdr.sh_info; 3859 extsymoff = 0; 3860 if (!elf_bad_symtab (igroup->owner)) 3861 { 3862 Elf_Internal_Shdr *symtab_hdr; 3863 3864 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; 3865 extsymoff = symtab_hdr->sh_info; 3866 } 3867 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; 3868 while (h->root.type == bfd_link_hash_indirect 3869 || h->root.type == bfd_link_hash_warning) 3870 h = (struct elf_link_hash_entry *) h->root.u.i.link; 3871 3872 elf_section_data (sec)->this_hdr.sh_info = h->indx; 3873 } 3874 3875 /* The contents won't be allocated for "ld -r" or objcopy. */ 3876 gas = true; 3877 if (sec->contents == NULL) 3878 { 3879 gas = false; 3880 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); 3881 3882 /* Arrange for the section to be written out. */ 3883 elf_section_data (sec)->this_hdr.contents = sec->contents; 3884 if (sec->contents == NULL) 3885 { 3886 *failedptr = true; 3887 return; 3888 } 3889 } 3890 3891 loc = sec->contents + sec->size; 3892 3893 /* Get the pointer to the first section in the group that gas 3894 squirreled away here. objcopy arranges for this to be set to the 3895 start of the input section group. */ 3896 first = elt = elf_next_in_group (sec); 3897 3898 /* First element is a flag word. Rest of section is elf section 3899 indices for all the sections of the group. Write them backwards 3900 just to keep the group in the same order as given in .section 3901 directives, not that it matters. */ 3902 while (elt != NULL) 3903 { 3904 asection *s; 3905 3906 s = elt; 3907 if (!gas) 3908 s = s->output_section; 3909 if (s != NULL 3910 && !bfd_is_abs_section (s)) 3911 { 3912 struct bfd_elf_section_data *elf_sec = elf_section_data (s); 3913 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt); 3914 3915 if (elf_sec->rel.hdr != NULL 3916 && (gas 3917 || (input_elf_sec->rel.hdr != NULL 3918 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)) 3919 { 3920 elf_sec->rel.hdr->sh_flags |= SHF_GROUP; 3921 loc -= 4; 3922 if (loc == sec->contents) 3923 break; 3924 H_PUT_32 (abfd, elf_sec->rel.idx, loc); 3925 } 3926 if (elf_sec->rela.hdr != NULL 3927 && (gas 3928 || (input_elf_sec->rela.hdr != NULL 3929 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)) 3930 { 3931 elf_sec->rela.hdr->sh_flags |= SHF_GROUP; 3932 loc -= 4; 3933 if (loc == sec->contents) 3934 break; 3935 H_PUT_32 (abfd, elf_sec->rela.idx, loc); 3936 } 3937 loc -= 4; 3938 if (loc == sec->contents) 3939 break; 3940 H_PUT_32 (abfd, elf_sec->this_idx, loc); 3941 } 3942 elt = elf_next_in_group (elt); 3943 if (elt == first) 3944 break; 3945 } 3946 3947 /* We should always get here with loc == sec->contents + 4, but it is 3948 possible to craft bogus SHT_GROUP sections that will cause segfaults 3949 in objcopy without checking loc here and in the loop above. */ 3950 if (loc == sec->contents) 3951 BFD_ASSERT (0); 3952 else 3953 { 3954 loc -= 4; 3955 if (loc != sec->contents) 3956 { 3957 BFD_ASSERT (0); 3958 memset (sec->contents + 4, 0, loc - sec->contents); 3959 loc = sec->contents; 3960 } 3961 } 3962 3963 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); 3964 } 3965 3966 /* Given NAME, the name of a relocation section stripped of its 3967 .rel/.rela prefix, return the section in ABFD to which the 3968 relocations apply. */ 3969 3970 asection * 3971 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name) 3972 { 3973 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt 3974 section likely apply to .got.plt or .got section. */ 3975 if (get_elf_backend_data (abfd)->want_got_plt 3976 && strcmp (name, ".plt") == 0) 3977 { 3978 asection *sec; 3979 3980 name = ".got.plt"; 3981 sec = bfd_get_section_by_name (abfd, name); 3982 if (sec != NULL) 3983 return sec; 3984 name = ".got"; 3985 } 3986 3987 return bfd_get_section_by_name (abfd, name); 3988 } 3989 3990 /* Return the section to which RELOC_SEC applies. */ 3991 3992 static asection * 3993 elf_get_reloc_section (asection *reloc_sec) 3994 { 3995 const char *name; 3996 unsigned int type; 3997 bfd *abfd; 3998 const struct elf_backend_data *bed; 3999 4000 type = elf_section_data (reloc_sec)->this_hdr.sh_type; 4001 if (type != SHT_REL && type != SHT_RELA) 4002 return NULL; 4003 4004 /* We look up the section the relocs apply to by name. */ 4005 name = reloc_sec->name; 4006 if (!startswith (name, ".rel")) 4007 return NULL; 4008 name += 4; 4009 if (type == SHT_RELA && *name++ != 'a') 4010 return NULL; 4011 4012 abfd = reloc_sec->owner; 4013 bed = get_elf_backend_data (abfd); 4014 return bed->get_reloc_section (abfd, name); 4015 } 4016 4017 /* Assign all ELF section numbers. The dummy first section is handled here 4018 too. The link/info pointers for the standard section types are filled 4019 in here too, while we're at it. LINK_INFO will be 0 when arriving 4020 here for gas, objcopy, and when using the generic ELF linker. */ 4021 4022 static bool 4023 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) 4024 { 4025 struct elf_obj_tdata *t = elf_tdata (abfd); 4026 asection *sec; 4027 unsigned int section_number; 4028 Elf_Internal_Shdr **i_shdrp; 4029 struct bfd_elf_section_data *d; 4030 bool need_symtab; 4031 size_t amt; 4032 4033 section_number = 1; 4034 4035 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); 4036 4037 /* SHT_GROUP sections are in relocatable files only. */ 4038 if (link_info == NULL || !link_info->resolve_section_groups) 4039 { 4040 size_t reloc_count = 0; 4041 4042 /* Put SHT_GROUP sections first. */ 4043 for (sec = abfd->sections; sec != NULL; sec = sec->next) 4044 { 4045 d = elf_section_data (sec); 4046 4047 if (d->this_hdr.sh_type == SHT_GROUP) 4048 { 4049 if (sec->flags & SEC_LINKER_CREATED) 4050 { 4051 /* Remove the linker created SHT_GROUP sections. */ 4052 bfd_section_list_remove (abfd, sec); 4053 abfd->section_count--; 4054 } 4055 else 4056 d->this_idx = section_number++; 4057 } 4058 4059 /* Count relocations. */ 4060 reloc_count += sec->reloc_count; 4061 } 4062 4063 /* Set/clear HAS_RELOC depending on whether there are relocations. */ 4064 if (reloc_count == 0) 4065 abfd->flags &= ~HAS_RELOC; 4066 else 4067 abfd->flags |= HAS_RELOC; 4068 } 4069 4070 for (sec = abfd->sections; sec; sec = sec->next) 4071 { 4072 d = elf_section_data (sec); 4073 4074 if (d->this_hdr.sh_type != SHT_GROUP) 4075 d->this_idx = section_number++; 4076 if (d->this_hdr.sh_name != (unsigned int) -1) 4077 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); 4078 if (d->rel.hdr) 4079 { 4080 d->rel.idx = section_number++; 4081 if (d->rel.hdr->sh_name != (unsigned int) -1) 4082 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); 4083 } 4084 else 4085 d->rel.idx = 0; 4086 4087 if (d->rela.hdr) 4088 { 4089 d->rela.idx = section_number++; 4090 if (d->rela.hdr->sh_name != (unsigned int) -1) 4091 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); 4092 } 4093 else 4094 d->rela.idx = 0; 4095 } 4096 4097 need_symtab = (bfd_get_symcount (abfd) > 0 4098 || (link_info == NULL 4099 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) 4100 == HAS_RELOC))); 4101 if (need_symtab) 4102 { 4103 elf_onesymtab (abfd) = section_number++; 4104 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); 4105 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) 4106 { 4107 elf_section_list *entry; 4108 4109 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); 4110 4111 entry = bfd_zalloc (abfd, sizeof (*entry)); 4112 entry->ndx = section_number++; 4113 elf_symtab_shndx_list (abfd) = entry; 4114 entry->hdr.sh_name 4115 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 4116 ".symtab_shndx", false); 4117 if (entry->hdr.sh_name == (unsigned int) -1) 4118 return false; 4119 } 4120 elf_strtab_sec (abfd) = section_number++; 4121 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); 4122 } 4123 4124 elf_shstrtab_sec (abfd) = section_number++; 4125 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); 4126 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); 4127 4128 if (section_number >= SHN_LORESERVE) 4129 { 4130 /* xgettext:c-format */ 4131 _bfd_error_handler (_("%pB: too many sections: %u"), 4132 abfd, section_number); 4133 return false; 4134 } 4135 4136 elf_numsections (abfd) = section_number; 4137 elf_elfheader (abfd)->e_shnum = section_number; 4138 4139 /* Set up the list of section header pointers, in agreement with the 4140 indices. */ 4141 amt = section_number * sizeof (Elf_Internal_Shdr *); 4142 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt); 4143 if (i_shdrp == NULL) 4144 return false; 4145 4146 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, 4147 sizeof (Elf_Internal_Shdr)); 4148 if (i_shdrp[0] == NULL) 4149 { 4150 bfd_release (abfd, i_shdrp); 4151 return false; 4152 } 4153 4154 elf_elfsections (abfd) = i_shdrp; 4155 4156 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; 4157 if (need_symtab) 4158 { 4159 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; 4160 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) 4161 { 4162 elf_section_list * entry = elf_symtab_shndx_list (abfd); 4163 BFD_ASSERT (entry != NULL); 4164 i_shdrp[entry->ndx] = & entry->hdr; 4165 entry->hdr.sh_link = elf_onesymtab (abfd); 4166 } 4167 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; 4168 t->symtab_hdr.sh_link = elf_strtab_sec (abfd); 4169 } 4170 4171 for (sec = abfd->sections; sec; sec = sec->next) 4172 { 4173 asection *s; 4174 4175 d = elf_section_data (sec); 4176 4177 i_shdrp[d->this_idx] = &d->this_hdr; 4178 if (d->rel.idx != 0) 4179 i_shdrp[d->rel.idx] = d->rel.hdr; 4180 if (d->rela.idx != 0) 4181 i_shdrp[d->rela.idx] = d->rela.hdr; 4182 4183 /* Fill in the sh_link and sh_info fields while we're at it. */ 4184 4185 /* sh_link of a reloc section is the section index of the symbol 4186 table. sh_info is the section index of the section to which 4187 the relocation entries apply. */ 4188 if (d->rel.idx != 0) 4189 { 4190 d->rel.hdr->sh_link = elf_onesymtab (abfd); 4191 d->rel.hdr->sh_info = d->this_idx; 4192 d->rel.hdr->sh_flags |= SHF_INFO_LINK; 4193 } 4194 if (d->rela.idx != 0) 4195 { 4196 d->rela.hdr->sh_link = elf_onesymtab (abfd); 4197 d->rela.hdr->sh_info = d->this_idx; 4198 d->rela.hdr->sh_flags |= SHF_INFO_LINK; 4199 } 4200 4201 /* We need to set up sh_link for SHF_LINK_ORDER. */ 4202 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) 4203 { 4204 s = elf_linked_to_section (sec); 4205 /* We can now have a NULL linked section pointer. 4206 This happens when the sh_link field is 0, which is done 4207 when a linked to section is discarded but the linking 4208 section has been retained for some reason. */ 4209 if (s) 4210 { 4211 /* Check discarded linkonce section. */ 4212 if (discarded_section (s)) 4213 { 4214 asection *kept; 4215 _bfd_error_handler 4216 /* xgettext:c-format */ 4217 (_("%pB: sh_link of section `%pA' points to" 4218 " discarded section `%pA' of `%pB'"), 4219 abfd, d->this_hdr.bfd_section, s, s->owner); 4220 /* Point to the kept section if it has the same 4221 size as the discarded one. */ 4222 kept = _bfd_elf_check_kept_section (s, link_info); 4223 if (kept == NULL) 4224 { 4225 bfd_set_error (bfd_error_bad_value); 4226 return false; 4227 } 4228 s = kept; 4229 } 4230 /* Handle objcopy. */ 4231 else if (s->output_section == NULL) 4232 { 4233 _bfd_error_handler 4234 /* xgettext:c-format */ 4235 (_("%pB: sh_link of section `%pA' points to" 4236 " removed section `%pA' of `%pB'"), 4237 abfd, d->this_hdr.bfd_section, s, s->owner); 4238 bfd_set_error (bfd_error_bad_value); 4239 return false; 4240 } 4241 s = s->output_section; 4242 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4243 } 4244 } 4245 4246 switch (d->this_hdr.sh_type) 4247 { 4248 case SHT_REL: 4249 case SHT_RELA: 4250 /* sh_link is the section index of the symbol table. 4251 sh_info is the section index of the section to which the 4252 relocation entries apply. */ 4253 if (d->this_hdr.sh_link == 0) 4254 { 4255 /* FIXME maybe: If this is a reloc section which we are 4256 treating as a normal section then we likely should 4257 not be assuming its sh_link is .dynsym or .symtab. */ 4258 if ((sec->flags & SEC_ALLOC) != 0) 4259 { 4260 s = bfd_get_section_by_name (abfd, ".dynsym"); 4261 if (s != NULL) 4262 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4263 } 4264 else 4265 d->this_hdr.sh_link = elf_onesymtab (abfd); 4266 } 4267 4268 s = elf_get_reloc_section (sec); 4269 if (s != NULL) 4270 { 4271 d->this_hdr.sh_info = elf_section_data (s)->this_idx; 4272 d->this_hdr.sh_flags |= SHF_INFO_LINK; 4273 } 4274 break; 4275 4276 case SHT_STRTAB: 4277 /* We assume that a section named .stab*str is a stabs 4278 string section. We look for a section with the same name 4279 but without the trailing ``str'', and set its sh_link 4280 field to point to this section. */ 4281 if (startswith (sec->name, ".stab") 4282 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) 4283 { 4284 size_t len; 4285 char *alc; 4286 4287 len = strlen (sec->name); 4288 alc = (char *) bfd_malloc (len - 2); 4289 if (alc == NULL) 4290 return false; 4291 memcpy (alc, sec->name, len - 3); 4292 alc[len - 3] = '\0'; 4293 s = bfd_get_section_by_name (abfd, alc); 4294 free (alc); 4295 if (s != NULL) 4296 { 4297 elf_section_data (s)->this_hdr.sh_link = d->this_idx; 4298 4299 /* This is a .stab section. */ 4300 elf_section_data (s)->this_hdr.sh_entsize = 12; 4301 } 4302 } 4303 break; 4304 4305 case SHT_DYNAMIC: 4306 case SHT_DYNSYM: 4307 case SHT_GNU_verneed: 4308 case SHT_GNU_verdef: 4309 /* sh_link is the section header index of the string table 4310 used for the dynamic entries, or the symbol table, or the 4311 version strings. */ 4312 s = bfd_get_section_by_name (abfd, ".dynstr"); 4313 if (s != NULL) 4314 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4315 break; 4316 4317 case SHT_GNU_LIBLIST: 4318 /* sh_link is the section header index of the prelink library 4319 list used for the dynamic entries, or the symbol table, or 4320 the version strings. */ 4321 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC) 4322 ? ".dynstr" : ".gnu.libstr")); 4323 if (s != NULL) 4324 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4325 break; 4326 4327 case SHT_HASH: 4328 case SHT_GNU_HASH: 4329 case SHT_GNU_versym: 4330 /* sh_link is the section header index of the symbol table 4331 this hash table or version table is for. */ 4332 s = bfd_get_section_by_name (abfd, ".dynsym"); 4333 if (s != NULL) 4334 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4335 break; 4336 4337 case SHT_GROUP: 4338 d->this_hdr.sh_link = elf_onesymtab (abfd); 4339 } 4340 } 4341 4342 /* Delay setting sh_name to _bfd_elf_write_object_contents so that 4343 _bfd_elf_assign_file_positions_for_non_load can convert DWARF 4344 debug section name from .debug_* to .zdebug_* if needed. */ 4345 4346 return true; 4347 } 4348 4349 static bool 4350 sym_is_global (bfd *abfd, asymbol *sym) 4351 { 4352 /* If the backend has a special mapping, use it. */ 4353 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4354 if (bed->elf_backend_sym_is_global) 4355 return (*bed->elf_backend_sym_is_global) (abfd, sym); 4356 4357 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 4358 || bfd_is_und_section (bfd_asymbol_section (sym)) 4359 || bfd_is_com_section (bfd_asymbol_section (sym))); 4360 } 4361 4362 /* Filter global symbols of ABFD to include in the import library. All 4363 SYMCOUNT symbols of ABFD can be examined from their pointers in 4364 SYMS. Pointers of symbols to keep should be stored contiguously at 4365 the beginning of that array. 4366 4367 Returns the number of symbols to keep. */ 4368 4369 unsigned int 4370 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, 4371 asymbol **syms, long symcount) 4372 { 4373 long src_count, dst_count = 0; 4374 4375 for (src_count = 0; src_count < symcount; src_count++) 4376 { 4377 asymbol *sym = syms[src_count]; 4378 char *name = (char *) bfd_asymbol_name (sym); 4379 struct bfd_link_hash_entry *h; 4380 4381 if (!sym_is_global (abfd, sym)) 4382 continue; 4383 4384 h = bfd_link_hash_lookup (info->hash, name, false, false, false); 4385 if (h == NULL) 4386 continue; 4387 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) 4388 continue; 4389 if (h->linker_def || h->ldscript_def) 4390 continue; 4391 4392 syms[dst_count++] = sym; 4393 } 4394 4395 syms[dst_count] = NULL; 4396 4397 return dst_count; 4398 } 4399 4400 /* Don't output symbols for sections that are not going to be output, 4401 that are duplicates or there is no BFD section. */ 4402 4403 static bool 4404 ignore_sym (asymbol *sym) 4405 { 4406 if (sym == NULL) 4407 return false; 4408 4409 if (sym->section == NULL) 4410 return true; 4411 4412 if ((sym->flags & BSF_SECTION_SYM) != 0) 4413 { 4414 if ((sym->flags & BSF_SECTION_SYM_USED) == 0) 4415 return true; 4416 /* With ld -r on generic elf targets it is possible to have 4417 multiple section symbols in the output for a given section. 4418 We'd like to get rid of all but the first one. This drops 4419 them if the first input section is non-zero size, but fails 4420 to do so if the first input section is zero sized. */ 4421 if (sym->section->output_offset != 0) 4422 return true; 4423 } 4424 4425 return discarded_section (sym->section); 4426 } 4427 4428 /* Map symbol from it's internal number to the external number, moving 4429 all local symbols to be at the head of the list. */ 4430 4431 static bool 4432 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) 4433 { 4434 unsigned int symcount = bfd_get_symcount (abfd); 4435 asymbol **syms = bfd_get_outsymbols (abfd); 4436 asymbol **sect_syms; 4437 unsigned int num_locals = 0; 4438 unsigned int num_globals = 0; 4439 unsigned int max_index = 0; 4440 unsigned int idx; 4441 asection *asect; 4442 asymbol **new_syms; 4443 size_t amt; 4444 4445 #ifdef DEBUG 4446 fprintf (stderr, "elf_map_symbols\n"); 4447 fflush (stderr); 4448 #endif 4449 4450 for (asect = abfd->sections; asect; asect = asect->next) 4451 { 4452 if (max_index < asect->index) 4453 max_index = asect->index; 4454 } 4455 4456 max_index++; 4457 amt = max_index * sizeof (asymbol *); 4458 sect_syms = (asymbol **) bfd_zalloc (abfd, amt); 4459 if (sect_syms == NULL) 4460 return false; 4461 elf_section_syms (abfd) = sect_syms; 4462 elf_num_section_syms (abfd) = max_index; 4463 4464 /* Init sect_syms entries for any section symbols we have already 4465 decided to output. */ 4466 for (idx = 0; idx < symcount; idx++) 4467 { 4468 asymbol *sym = syms[idx]; 4469 4470 if ((sym->flags & BSF_SECTION_SYM) != 0 4471 && sym->value == 0 4472 && !ignore_sym (sym) 4473 && !bfd_is_abs_section (sym->section)) 4474 { 4475 asection *sec = sym->section; 4476 4477 if (sec->owner != abfd) 4478 sec = sec->output_section; 4479 4480 sect_syms[sec->index] = syms[idx]; 4481 } 4482 } 4483 4484 /* Classify all of the symbols. */ 4485 for (idx = 0; idx < symcount; idx++) 4486 { 4487 if (ignore_sym (syms[idx])) 4488 continue; 4489 if (sym_is_global (abfd, syms[idx])) 4490 num_globals++; 4491 else 4492 num_locals++; 4493 } 4494 4495 /* We will be adding a section symbol for each normal BFD section. Most 4496 sections will already have a section symbol in outsymbols, but 4497 eg. SHT_GROUP sections will not, and we need the section symbol mapped 4498 at least in that case. */ 4499 for (asect = abfd->sections; asect; asect = asect->next) 4500 { 4501 asymbol *sym = asect->symbol; 4502 /* Don't include ignored section symbols. */ 4503 if (!ignore_sym (sym) 4504 && sect_syms[asect->index] == NULL) 4505 { 4506 if (sym_is_global (abfd, asect->symbol)) 4507 num_globals++; 4508 else 4509 num_locals++; 4510 } 4511 } 4512 4513 /* Now sort the symbols so the local symbols are first. */ 4514 amt = (num_locals + num_globals) * sizeof (asymbol *); 4515 new_syms = (asymbol **) bfd_alloc (abfd, amt); 4516 if (new_syms == NULL) 4517 return false; 4518 4519 unsigned int num_globals2 = 0; 4520 unsigned int num_locals2 = 0; 4521 for (idx = 0; idx < symcount; idx++) 4522 { 4523 asymbol *sym = syms[idx]; 4524 unsigned int i; 4525 4526 if (ignore_sym (sym)) 4527 continue; 4528 4529 if (sym_is_global (abfd, sym)) 4530 i = num_locals + num_globals2++; 4531 else 4532 i = num_locals2++; 4533 new_syms[i] = sym; 4534 sym->udata.i = i + 1; 4535 } 4536 for (asect = abfd->sections; asect; asect = asect->next) 4537 { 4538 asymbol *sym = asect->symbol; 4539 if (!ignore_sym (sym) 4540 && sect_syms[asect->index] == NULL) 4541 { 4542 unsigned int i; 4543 4544 sect_syms[asect->index] = sym; 4545 if (sym_is_global (abfd, sym)) 4546 i = num_locals + num_globals2++; 4547 else 4548 i = num_locals2++; 4549 new_syms[i] = sym; 4550 sym->udata.i = i + 1; 4551 } 4552 } 4553 4554 bfd_set_symtab (abfd, new_syms, num_locals + num_globals); 4555 4556 *pnum_locals = num_locals; 4557 return true; 4558 } 4559 4560 /* Assign a file position to a section, optionally aligning to the 4561 required section alignment. */ 4562 4563 file_ptr 4564 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, 4565 file_ptr offset, 4566 bool align, 4567 unsigned char log_file_align) 4568 { 4569 if (i_shdrp->sh_addralign > 1) 4570 { 4571 file_ptr salign = i_shdrp->sh_addralign & -i_shdrp->sh_addralign; 4572 4573 if (align) 4574 offset = BFD_ALIGN (offset, salign); 4575 else if (log_file_align) 4576 { 4577 /* Heuristic: Cap alignment at log_file_align. */ 4578 file_ptr falign = 1u << log_file_align; 4579 4580 offset = BFD_ALIGN (offset, salign < falign ? salign : falign); 4581 } 4582 } 4583 i_shdrp->sh_offset = offset; 4584 if (i_shdrp->bfd_section != NULL) 4585 i_shdrp->bfd_section->filepos = offset; 4586 if (i_shdrp->sh_type != SHT_NOBITS) 4587 offset += i_shdrp->sh_size; 4588 return offset; 4589 } 4590 4591 /* Compute the file positions we are going to put the sections at, and 4592 otherwise prepare to begin writing out the ELF file. If LINK_INFO 4593 is not NULL, this is being called by the ELF backend linker. */ 4594 4595 bool 4596 _bfd_elf_compute_section_file_positions (bfd *abfd, 4597 struct bfd_link_info *link_info) 4598 { 4599 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4600 struct fake_section_arg fsargs; 4601 bool failed; 4602 struct elf_strtab_hash *strtab = NULL; 4603 Elf_Internal_Shdr *shstrtab_hdr; 4604 bool need_symtab; 4605 4606 if (abfd->output_has_begun) 4607 return true; 4608 4609 /* Do any elf backend specific processing first. */ 4610 if (bed->elf_backend_begin_write_processing) 4611 (*bed->elf_backend_begin_write_processing) (abfd, link_info); 4612 4613 if (!(*bed->elf_backend_init_file_header) (abfd, link_info)) 4614 return false; 4615 4616 fsargs.failed = false; 4617 fsargs.link_info = link_info; 4618 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); 4619 if (fsargs.failed) 4620 return false; 4621 4622 if (!assign_section_numbers (abfd, link_info)) 4623 return false; 4624 4625 /* The backend linker builds symbol table information itself. */ 4626 need_symtab = (link_info == NULL 4627 && (bfd_get_symcount (abfd) > 0 4628 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) 4629 == HAS_RELOC))); 4630 if (need_symtab) 4631 { 4632 /* Non-zero if doing a relocatable link. */ 4633 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); 4634 4635 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info)) 4636 return false; 4637 } 4638 4639 failed = false; 4640 if (link_info == NULL) 4641 { 4642 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); 4643 if (failed) 4644 goto err_free_strtab; 4645 } 4646 4647 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; 4648 /* sh_name was set in init_file_header. */ 4649 shstrtab_hdr->sh_type = SHT_STRTAB; 4650 shstrtab_hdr->sh_flags = bed->elf_strtab_flags; 4651 shstrtab_hdr->sh_addr = 0; 4652 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ 4653 shstrtab_hdr->sh_entsize = 0; 4654 shstrtab_hdr->sh_link = 0; 4655 shstrtab_hdr->sh_info = 0; 4656 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ 4657 shstrtab_hdr->sh_addralign = 1; 4658 4659 if (!assign_file_positions_except_relocs (abfd, link_info)) 4660 goto err_free_strtab; 4661 4662 if (strtab != NULL) 4663 { 4664 file_ptr off; 4665 Elf_Internal_Shdr *hdr; 4666 4667 off = elf_next_file_pos (abfd); 4668 4669 hdr = & elf_symtab_hdr (abfd); 4670 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0); 4671 4672 if (elf_symtab_shndx_list (abfd) != NULL) 4673 { 4674 hdr = & elf_symtab_shndx_list (abfd)->hdr; 4675 if (hdr->sh_size != 0) 4676 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0); 4677 /* FIXME: What about other symtab_shndx sections in the list ? */ 4678 } 4679 4680 hdr = &elf_tdata (abfd)->strtab_hdr; 4681 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0); 4682 4683 elf_next_file_pos (abfd) = off; 4684 4685 /* Now that we know where the .strtab section goes, write it 4686 out. */ 4687 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 4688 || ! _bfd_elf_strtab_emit (abfd, strtab)) 4689 goto err_free_strtab; 4690 _bfd_elf_strtab_free (strtab); 4691 } 4692 4693 abfd->output_has_begun = true; 4694 return true; 4695 4696 err_free_strtab: 4697 if (strtab != NULL) 4698 _bfd_elf_strtab_free (strtab); 4699 return false; 4700 } 4701 4702 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the 4703 function effectively returns whether --eh-frame-hdr is given on the 4704 command line. After size_dynamic_sections the result reflects 4705 whether .eh_frame_hdr will actually be output (sizing isn't done 4706 until ldemul_after_allocation). */ 4707 4708 static asection * 4709 elf_eh_frame_hdr (const struct bfd_link_info *info) 4710 { 4711 if (info != NULL && is_elf_hash_table (info->hash)) 4712 return elf_hash_table (info)->eh_info.hdr_sec; 4713 return NULL; 4714 } 4715 4716 /* Make an initial estimate of the size of the program header. If we 4717 get the number wrong here, we'll redo section placement. */ 4718 4719 static bfd_size_type 4720 get_program_header_size (bfd *abfd, struct bfd_link_info *info) 4721 { 4722 size_t segs; 4723 asection *s; 4724 const struct elf_backend_data *bed; 4725 4726 /* Assume we will need exactly two PT_LOAD segments: one for text 4727 and one for data. */ 4728 segs = 2; 4729 4730 s = bfd_get_section_by_name (abfd, ".interp"); 4731 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) 4732 { 4733 /* If we have a loadable interpreter section, we need a 4734 PT_INTERP segment. In this case, assume we also need a 4735 PT_PHDR segment, although that may not be true for all 4736 targets. */ 4737 segs += 2; 4738 } 4739 4740 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) 4741 { 4742 /* We need a PT_DYNAMIC segment. */ 4743 ++segs; 4744 } 4745 4746 if (info != NULL && info->relro) 4747 { 4748 /* We need a PT_GNU_RELRO segment. */ 4749 ++segs; 4750 } 4751 4752 if (elf_eh_frame_hdr (info)) 4753 { 4754 /* We need a PT_GNU_EH_FRAME segment. */ 4755 ++segs; 4756 } 4757 4758 if (elf_stack_flags (abfd)) 4759 { 4760 /* We need a PT_GNU_STACK segment. */ 4761 ++segs; 4762 } 4763 4764 if (elf_sframe (abfd)) 4765 { 4766 /* We need a PT_GNU_SFRAME segment. */ 4767 ++segs; 4768 } 4769 4770 s = bfd_get_section_by_name (abfd, 4771 NOTE_GNU_PROPERTY_SECTION_NAME); 4772 if (s != NULL && s->size != 0) 4773 { 4774 /* We need a PT_GNU_PROPERTY segment. */ 4775 ++segs; 4776 } 4777 4778 for (s = abfd->sections; s != NULL; s = s->next) 4779 { 4780 if ((s->flags & SEC_LOAD) != 0 4781 && elf_section_type (s) == SHT_NOTE) 4782 { 4783 unsigned int alignment_power; 4784 /* We need a PT_NOTE segment. */ 4785 ++segs; 4786 /* Try to create just one PT_NOTE segment for all adjacent 4787 loadable SHT_NOTE sections. gABI requires that within a 4788 PT_NOTE segment (and also inside of each SHT_NOTE section) 4789 each note should have the same alignment. So we check 4790 whether the sections are correctly aligned. */ 4791 alignment_power = s->alignment_power; 4792 while (s->next != NULL 4793 && s->next->alignment_power == alignment_power 4794 && (s->next->flags & SEC_LOAD) != 0 4795 && elf_section_type (s->next) == SHT_NOTE) 4796 s = s->next; 4797 } 4798 } 4799 4800 for (s = abfd->sections; s != NULL; s = s->next) 4801 { 4802 if (s->flags & SEC_THREAD_LOCAL) 4803 { 4804 /* We need a PT_TLS segment. */ 4805 ++segs; 4806 break; 4807 } 4808 } 4809 4810 bed = get_elf_backend_data (abfd); 4811 4812 if ((abfd->flags & D_PAGED) != 0 4813 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) 4814 { 4815 /* Add a PT_GNU_MBIND segment for each mbind section. */ 4816 bfd_vma commonpagesize; 4817 unsigned int page_align_power; 4818 4819 if (info != NULL) 4820 commonpagesize = info->commonpagesize; 4821 else 4822 commonpagesize = bed->commonpagesize; 4823 page_align_power = bfd_log2 (commonpagesize); 4824 for (s = abfd->sections; s != NULL; s = s->next) 4825 if (elf_section_flags (s) & SHF_GNU_MBIND) 4826 { 4827 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM) 4828 { 4829 _bfd_error_handler 4830 /* xgettext:c-format */ 4831 (_("%pB: GNU_MBIND section `%pA' has invalid " 4832 "sh_info field: %d"), 4833 abfd, s, elf_section_data (s)->this_hdr.sh_info); 4834 continue; 4835 } 4836 /* Align mbind section to page size. */ 4837 if (s->alignment_power < page_align_power) 4838 s->alignment_power = page_align_power; 4839 segs ++; 4840 } 4841 } 4842 4843 /* Let the backend count up any program headers it might need. */ 4844 if (bed->elf_backend_additional_program_headers) 4845 { 4846 int a; 4847 4848 a = (*bed->elf_backend_additional_program_headers) (abfd, info); 4849 if (a == -1) 4850 abort (); 4851 segs += a; 4852 } 4853 4854 return segs * bed->s->sizeof_phdr; 4855 } 4856 4857 /* Find the segment that contains the output_section of section. */ 4858 4859 Elf_Internal_Phdr * 4860 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) 4861 { 4862 struct elf_segment_map *m; 4863 Elf_Internal_Phdr *p; 4864 4865 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; 4866 m != NULL; 4867 m = m->next, p++) 4868 { 4869 int i; 4870 4871 for (i = m->count - 1; i >= 0; i--) 4872 if (m->sections[i] == section) 4873 return p; 4874 } 4875 4876 return NULL; 4877 } 4878 4879 /* Create a mapping from a set of sections to a program segment. */ 4880 4881 static struct elf_segment_map * 4882 make_mapping (bfd *abfd, 4883 asection **sections, 4884 unsigned int from, 4885 unsigned int to, 4886 bool phdr) 4887 { 4888 struct elf_segment_map *m; 4889 unsigned int i; 4890 asection **hdrpp; 4891 size_t amt; 4892 4893 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 4894 amt += (to - from) * sizeof (asection *); 4895 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 4896 if (m == NULL) 4897 return NULL; 4898 m->next = NULL; 4899 m->p_type = PT_LOAD; 4900 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) 4901 m->sections[i - from] = *hdrpp; 4902 m->count = to - from; 4903 4904 if (from == 0 && phdr) 4905 { 4906 /* Include the headers in the first PT_LOAD segment. */ 4907 m->includes_filehdr = 1; 4908 m->includes_phdrs = 1; 4909 } 4910 4911 return m; 4912 } 4913 4914 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL 4915 on failure. */ 4916 4917 struct elf_segment_map * 4918 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) 4919 { 4920 struct elf_segment_map *m; 4921 4922 m = (struct elf_segment_map *) bfd_zalloc (abfd, 4923 sizeof (struct elf_segment_map)); 4924 if (m == NULL) 4925 return NULL; 4926 m->next = NULL; 4927 m->p_type = PT_DYNAMIC; 4928 m->count = 1; 4929 m->sections[0] = dynsec; 4930 4931 return m; 4932 } 4933 4934 /* Possibly add or remove segments from the segment map. */ 4935 4936 static bool 4937 elf_modify_segment_map (bfd *abfd, 4938 struct bfd_link_info *info, 4939 bool remove_empty_load) 4940 { 4941 struct elf_segment_map **m; 4942 const struct elf_backend_data *bed; 4943 4944 /* The placement algorithm assumes that non allocated sections are 4945 not in PT_LOAD segments. We ensure this here by removing such 4946 sections from the segment map. We also remove excluded 4947 sections. Finally, any PT_LOAD segment without sections is 4948 removed. */ 4949 m = &elf_seg_map (abfd); 4950 while (*m) 4951 { 4952 unsigned int i, new_count; 4953 4954 for (new_count = 0, i = 0; i < (*m)->count; i++) 4955 { 4956 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 4957 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 4958 || (*m)->p_type != PT_LOAD)) 4959 { 4960 (*m)->sections[new_count] = (*m)->sections[i]; 4961 new_count++; 4962 } 4963 } 4964 (*m)->count = new_count; 4965 4966 if (remove_empty_load 4967 && (*m)->p_type == PT_LOAD 4968 && (*m)->count == 0 4969 && !(*m)->includes_phdrs) 4970 *m = (*m)->next; 4971 else 4972 m = &(*m)->next; 4973 } 4974 4975 bed = get_elf_backend_data (abfd); 4976 if (bed->elf_backend_modify_segment_map != NULL) 4977 { 4978 if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) 4979 return false; 4980 } 4981 4982 return true; 4983 } 4984 4985 #define IS_TBSS(s) \ 4986 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL) 4987 4988 /* Set up a mapping from BFD sections to program segments. Update 4989 NEED_LAYOUT if the section layout is changed. */ 4990 4991 bool 4992 _bfd_elf_map_sections_to_segments (bfd *abfd, 4993 struct bfd_link_info *info, 4994 bool *need_layout) 4995 { 4996 unsigned int count; 4997 struct elf_segment_map *m; 4998 asection **sections = NULL; 4999 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 5000 bool no_user_phdrs; 5001 5002 no_user_phdrs = elf_seg_map (abfd) == NULL; 5003 5004 if (info != NULL) 5005 { 5006 info->user_phdrs = !no_user_phdrs; 5007 5008 /* Size the relative relocations if DT_RELR is enabled. */ 5009 if (info->enable_dt_relr 5010 && need_layout != NULL 5011 && bed->size_relative_relocs 5012 && !bed->size_relative_relocs (info, need_layout)) 5013 info->callbacks->einfo 5014 (_("%F%P: failed to size relative relocations\n")); 5015 } 5016 5017 if (no_user_phdrs && bfd_count_sections (abfd) != 0) 5018 { 5019 asection *s; 5020 unsigned int i; 5021 struct elf_segment_map *mfirst; 5022 struct elf_segment_map **pm; 5023 asection *last_hdr; 5024 bfd_vma last_size; 5025 unsigned int hdr_index; 5026 bfd_vma maxpagesize; 5027 asection **hdrpp; 5028 bool phdr_in_segment; 5029 bool writable; 5030 bool executable; 5031 unsigned int tls_count = 0; 5032 asection *first_tls = NULL; 5033 asection *first_mbind = NULL; 5034 asection *dynsec, *eh_frame_hdr; 5035 asection *sframe; 5036 size_t amt; 5037 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */ 5038 bfd_size_type phdr_size; /* Octets/bytes. */ 5039 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 5040 5041 /* Select the allocated sections, and sort them. */ 5042 5043 amt = bfd_count_sections (abfd) * sizeof (asection *); 5044 sections = (asection **) bfd_malloc (amt); 5045 if (sections == NULL) 5046 goto error_return; 5047 5048 /* Calculate top address, avoiding undefined behaviour of shift 5049 left operator when shift count is equal to size of type 5050 being shifted. */ 5051 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; 5052 addr_mask = (addr_mask << 1) + 1; 5053 5054 i = 0; 5055 for (s = abfd->sections; s != NULL; s = s->next) 5056 { 5057 if ((s->flags & SEC_ALLOC) != 0) 5058 { 5059 /* target_index is unused until bfd_elf_final_link 5060 starts output of section symbols. Use it to make 5061 qsort stable. */ 5062 s->target_index = i; 5063 sections[i] = s; 5064 ++i; 5065 /* A wrapping section potentially clashes with header. */ 5066 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask)) 5067 wrap_to = (s->lma + s->size / opb) & addr_mask; 5068 } 5069 } 5070 BFD_ASSERT (i <= bfd_count_sections (abfd)); 5071 count = i; 5072 5073 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); 5074 5075 phdr_size = elf_program_header_size (abfd); 5076 if (phdr_size == (bfd_size_type) -1) 5077 phdr_size = get_program_header_size (abfd, info); 5078 phdr_size += bed->s->sizeof_ehdr; 5079 /* phdr_size is compared to LMA values which are in bytes. */ 5080 phdr_size /= opb; 5081 if (info != NULL) 5082 maxpagesize = info->maxpagesize; 5083 else 5084 maxpagesize = bed->maxpagesize; 5085 if (maxpagesize == 0) 5086 maxpagesize = 1; 5087 phdr_in_segment = info != NULL && info->load_phdrs; 5088 if (count != 0 5089 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1)) 5090 >= (phdr_size & (maxpagesize - 1)))) 5091 /* For compatibility with old scripts that may not be using 5092 SIZEOF_HEADERS, add headers when it looks like space has 5093 been left for them. */ 5094 phdr_in_segment = true; 5095 5096 /* Build the mapping. */ 5097 mfirst = NULL; 5098 pm = &mfirst; 5099 5100 /* If we have a .interp section, then create a PT_PHDR segment for 5101 the program headers and a PT_INTERP segment for the .interp 5102 section. */ 5103 s = bfd_get_section_by_name (abfd, ".interp"); 5104 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) 5105 { 5106 amt = sizeof (struct elf_segment_map); 5107 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5108 if (m == NULL) 5109 goto error_return; 5110 m->next = NULL; 5111 m->p_type = PT_PHDR; 5112 m->p_flags = PF_R; 5113 m->p_flags_valid = 1; 5114 m->includes_phdrs = 1; 5115 phdr_in_segment = true; 5116 *pm = m; 5117 pm = &m->next; 5118 5119 amt = sizeof (struct elf_segment_map); 5120 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5121 if (m == NULL) 5122 goto error_return; 5123 m->next = NULL; 5124 m->p_type = PT_INTERP; 5125 m->count = 1; 5126 m->sections[0] = s; 5127 5128 *pm = m; 5129 pm = &m->next; 5130 } 5131 5132 /* Look through the sections. We put sections in the same program 5133 segment when the start of the second section can be placed within 5134 a few bytes of the end of the first section. */ 5135 last_hdr = NULL; 5136 last_size = 0; 5137 hdr_index = 0; 5138 writable = false; 5139 executable = false; 5140 dynsec = bfd_get_section_by_name (abfd, ".dynamic"); 5141 if (dynsec != NULL 5142 && (dynsec->flags & SEC_LOAD) == 0) 5143 dynsec = NULL; 5144 5145 if ((abfd->flags & D_PAGED) == 0) 5146 phdr_in_segment = false; 5147 5148 /* Deal with -Ttext or something similar such that the first section 5149 is not adjacent to the program headers. This is an 5150 approximation, since at this point we don't know exactly how many 5151 program headers we will need. */ 5152 if (phdr_in_segment && count > 0) 5153 { 5154 bfd_vma phdr_lma; /* Bytes. */ 5155 bool separate_phdr = false; 5156 5157 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize; 5158 if (info != NULL 5159 && info->separate_code 5160 && (sections[0]->flags & SEC_CODE) != 0) 5161 { 5162 /* If data sections should be separate from code and 5163 thus not executable, and the first section is 5164 executable then put the file and program headers in 5165 their own PT_LOAD. */ 5166 if (!info->one_rosegment) 5167 separate_phdr = true; 5168 5169 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize) 5170 == (sections[0]->lma & addr_mask & -maxpagesize))) 5171 { 5172 /* The file and program headers are currently on the 5173 same page as the first section. Put them on the 5174 previous page if we can. */ 5175 if (phdr_lma >= maxpagesize) 5176 phdr_lma -= maxpagesize; 5177 else 5178 separate_phdr = false; 5179 } 5180 } 5181 if ((sections[0]->lma & addr_mask) < phdr_lma 5182 || (sections[0]->lma & addr_mask) < phdr_size) 5183 /* If file and program headers would be placed at the end 5184 of memory then it's probably better to omit them. */ 5185 phdr_in_segment = false; 5186 else if (phdr_lma < wrap_to) 5187 /* If a section wraps around to where we'll be placing 5188 file and program headers, then the headers will be 5189 overwritten. */ 5190 phdr_in_segment = false; 5191 else if (separate_phdr) 5192 { 5193 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment); 5194 if (m == NULL) 5195 goto error_return; 5196 m->p_paddr = phdr_lma * opb; 5197 m->p_vaddr_offset 5198 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize; 5199 m->p_paddr_valid = 1; 5200 *pm = m; 5201 pm = &m->next; 5202 phdr_in_segment = false; 5203 } 5204 } 5205 5206 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) 5207 { 5208 asection *hdr; 5209 bool new_segment; 5210 5211 hdr = *hdrpp; 5212 5213 /* See if this section and the last one will fit in the same 5214 segment. */ 5215 5216 if (last_hdr == NULL) 5217 { 5218 /* If we don't have a segment yet, then we don't need a new 5219 one (we build the last one after this loop). */ 5220 new_segment = false; 5221 } 5222 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) 5223 { 5224 /* If this section has a different relation between the 5225 virtual address and the load address, then we need a new 5226 segment. */ 5227 new_segment = true; 5228 } 5229 else if (hdr->lma < last_hdr->lma + last_size 5230 || last_hdr->lma + last_size < last_hdr->lma) 5231 { 5232 /* If this section has a load address that makes it overlap 5233 the previous section, then we need a new segment. */ 5234 new_segment = true; 5235 } 5236 else if ((abfd->flags & D_PAGED) != 0 5237 && (((last_hdr->lma + last_size - 1) & -maxpagesize) 5238 == (hdr->lma & -maxpagesize))) 5239 { 5240 /* If we are demand paged then we can't map two disk 5241 pages onto the same memory page. */ 5242 new_segment = false; 5243 } 5244 /* In the next test we have to be careful when last_hdr->lma is close 5245 to the end of the address space. If the aligned address wraps 5246 around to the start of the address space, then there are no more 5247 pages left in memory and it is OK to assume that the current 5248 section can be included in the current segment. */ 5249 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) 5250 + maxpagesize > last_hdr->lma) 5251 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) 5252 + maxpagesize <= hdr->lma)) 5253 { 5254 /* If putting this section in this segment would force us to 5255 skip a page in the segment, then we need a new segment. */ 5256 new_segment = true; 5257 } 5258 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 5259 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) 5260 { 5261 /* We don't want to put a loaded section after a 5262 nonloaded (ie. bss style) section in the same segment 5263 as that will force the non-loaded section to be loaded. 5264 Consider .tbss sections as loaded for this purpose. */ 5265 new_segment = true; 5266 } 5267 else if ((abfd->flags & D_PAGED) == 0) 5268 { 5269 /* If the file is not demand paged, which means that we 5270 don't require the sections to be correctly aligned in the 5271 file, then there is no other reason for a new segment. */ 5272 new_segment = false; 5273 } 5274 else if (info != NULL 5275 && info->separate_code 5276 && executable != ((hdr->flags & SEC_CODE) != 0)) 5277 { 5278 new_segment = true; 5279 } 5280 else if (! writable 5281 && (hdr->flags & SEC_READONLY) == 0) 5282 { 5283 /* We don't want to put a writable section in a read only 5284 segment. */ 5285 new_segment = true; 5286 } 5287 else 5288 { 5289 /* Otherwise, we can use the same segment. */ 5290 new_segment = false; 5291 } 5292 5293 /* Allow interested parties a chance to override our decision. */ 5294 if (last_hdr != NULL 5295 && info != NULL 5296 && info->callbacks->override_segment_assignment != NULL) 5297 new_segment 5298 = info->callbacks->override_segment_assignment (info, abfd, hdr, 5299 last_hdr, 5300 new_segment); 5301 5302 if (! new_segment) 5303 { 5304 if ((hdr->flags & SEC_READONLY) == 0) 5305 writable = true; 5306 if ((hdr->flags & SEC_CODE) != 0) 5307 executable = true; 5308 last_hdr = hdr; 5309 /* .tbss sections effectively have zero size. */ 5310 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; 5311 continue; 5312 } 5313 5314 /* We need a new program segment. We must create a new program 5315 header holding all the sections from hdr_index until hdr. */ 5316 5317 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); 5318 if (m == NULL) 5319 goto error_return; 5320 5321 *pm = m; 5322 pm = &m->next; 5323 5324 if ((hdr->flags & SEC_READONLY) == 0) 5325 writable = true; 5326 else 5327 writable = false; 5328 5329 if ((hdr->flags & SEC_CODE) == 0) 5330 executable = false; 5331 else 5332 executable = true; 5333 5334 last_hdr = hdr; 5335 /* .tbss sections effectively have zero size. */ 5336 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; 5337 hdr_index = i; 5338 phdr_in_segment = false; 5339 } 5340 5341 /* Create a final PT_LOAD program segment, but not if it's just 5342 for .tbss. */ 5343 if (last_hdr != NULL 5344 && (i - hdr_index != 1 5345 || !IS_TBSS (last_hdr))) 5346 { 5347 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); 5348 if (m == NULL) 5349 goto error_return; 5350 5351 *pm = m; 5352 pm = &m->next; 5353 } 5354 5355 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ 5356 if (dynsec != NULL) 5357 { 5358 m = _bfd_elf_make_dynamic_segment (abfd, dynsec); 5359 if (m == NULL) 5360 goto error_return; 5361 *pm = m; 5362 pm = &m->next; 5363 } 5364 5365 /* For each batch of consecutive loadable SHT_NOTE sections, 5366 add a PT_NOTE segment. We don't use bfd_get_section_by_name, 5367 because if we link together nonloadable .note sections and 5368 loadable .note sections, we will generate two .note sections 5369 in the output file. */ 5370 for (s = abfd->sections; s != NULL; s = s->next) 5371 { 5372 if ((s->flags & SEC_LOAD) != 0 5373 && elf_section_type (s) == SHT_NOTE) 5374 { 5375 asection *s2; 5376 unsigned int alignment_power = s->alignment_power; 5377 5378 count = 1; 5379 for (s2 = s; s2->next != NULL; s2 = s2->next) 5380 { 5381 if (s2->next->alignment_power == alignment_power 5382 && (s2->next->flags & SEC_LOAD) != 0 5383 && elf_section_type (s2->next) == SHT_NOTE 5384 && align_power (s2->lma + s2->size / opb, 5385 alignment_power) 5386 == s2->next->lma) 5387 count++; 5388 else 5389 break; 5390 } 5391 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 5392 amt += count * sizeof (asection *); 5393 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5394 if (m == NULL) 5395 goto error_return; 5396 m->next = NULL; 5397 m->p_type = PT_NOTE; 5398 m->count = count; 5399 while (count > 1) 5400 { 5401 m->sections[m->count - count--] = s; 5402 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); 5403 s = s->next; 5404 } 5405 m->sections[m->count - 1] = s; 5406 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); 5407 *pm = m; 5408 pm = &m->next; 5409 } 5410 if (s->flags & SEC_THREAD_LOCAL) 5411 { 5412 if (! tls_count) 5413 first_tls = s; 5414 tls_count++; 5415 } 5416 if (first_mbind == NULL 5417 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) 5418 first_mbind = s; 5419 } 5420 5421 /* If there are any SHF_TLS output sections, add PT_TLS segment. */ 5422 if (tls_count > 0) 5423 { 5424 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 5425 amt += tls_count * sizeof (asection *); 5426 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5427 if (m == NULL) 5428 goto error_return; 5429 m->next = NULL; 5430 m->p_type = PT_TLS; 5431 m->count = tls_count; 5432 /* Mandated PF_R. */ 5433 m->p_flags = PF_R; 5434 m->p_flags_valid = 1; 5435 s = first_tls; 5436 for (i = 0; i < tls_count; ++i) 5437 { 5438 if ((s->flags & SEC_THREAD_LOCAL) == 0) 5439 { 5440 _bfd_error_handler 5441 (_("%pB: TLS sections are not adjacent:"), abfd); 5442 s = first_tls; 5443 i = 0; 5444 while (i < tls_count) 5445 { 5446 if ((s->flags & SEC_THREAD_LOCAL) != 0) 5447 { 5448 _bfd_error_handler (_(" TLS: %pA"), s); 5449 i++; 5450 } 5451 else 5452 _bfd_error_handler (_(" non-TLS: %pA"), s); 5453 s = s->next; 5454 } 5455 bfd_set_error (bfd_error_bad_value); 5456 goto error_return; 5457 } 5458 m->sections[i] = s; 5459 s = s->next; 5460 } 5461 5462 *pm = m; 5463 pm = &m->next; 5464 } 5465 5466 if (first_mbind 5467 && (abfd->flags & D_PAGED) != 0 5468 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) 5469 for (s = first_mbind; s != NULL; s = s->next) 5470 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 5471 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM) 5472 { 5473 /* Mandated PF_R. */ 5474 unsigned long p_flags = PF_R; 5475 if ((s->flags & SEC_READONLY) == 0) 5476 p_flags |= PF_W; 5477 if ((s->flags & SEC_CODE) != 0) 5478 p_flags |= PF_X; 5479 5480 amt = sizeof (struct elf_segment_map) + sizeof (asection *); 5481 m = bfd_zalloc (abfd, amt); 5482 if (m == NULL) 5483 goto error_return; 5484 m->next = NULL; 5485 m->p_type = (PT_GNU_MBIND_LO 5486 + elf_section_data (s)->this_hdr.sh_info); 5487 m->count = 1; 5488 m->p_flags_valid = 1; 5489 m->sections[0] = s; 5490 m->p_flags = p_flags; 5491 5492 *pm = m; 5493 pm = &m->next; 5494 } 5495 5496 s = bfd_get_section_by_name (abfd, 5497 NOTE_GNU_PROPERTY_SECTION_NAME); 5498 if (s != NULL && s->size != 0) 5499 { 5500 amt = sizeof (struct elf_segment_map) + sizeof (asection *); 5501 m = bfd_zalloc (abfd, amt); 5502 if (m == NULL) 5503 goto error_return; 5504 m->next = NULL; 5505 m->p_type = PT_GNU_PROPERTY; 5506 m->count = 1; 5507 m->p_flags_valid = 1; 5508 m->sections[0] = s; 5509 m->p_flags = PF_R; 5510 *pm = m; 5511 pm = &m->next; 5512 } 5513 5514 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME 5515 segment. */ 5516 eh_frame_hdr = elf_eh_frame_hdr (info); 5517 if (eh_frame_hdr != NULL 5518 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) 5519 { 5520 amt = sizeof (struct elf_segment_map); 5521 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5522 if (m == NULL) 5523 goto error_return; 5524 m->next = NULL; 5525 m->p_type = PT_GNU_EH_FRAME; 5526 m->count = 1; 5527 m->sections[0] = eh_frame_hdr->output_section; 5528 5529 *pm = m; 5530 pm = &m->next; 5531 } 5532 5533 /* If there is a .sframe section, throw in a PT_GNU_SFRAME 5534 segment. */ 5535 sframe = elf_sframe (abfd); 5536 if (sframe != NULL 5537 && (sframe->output_section->flags & SEC_LOAD) != 0 5538 && sframe->size != 0) 5539 { 5540 amt = sizeof (struct elf_segment_map); 5541 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5542 if (m == NULL) 5543 goto error_return; 5544 m->next = NULL; 5545 m->p_type = PT_GNU_SFRAME; 5546 m->count = 1; 5547 m->sections[0] = sframe->output_section; 5548 5549 *pm = m; 5550 pm = &m->next; 5551 } 5552 5553 if (elf_stack_flags (abfd)) 5554 { 5555 amt = sizeof (struct elf_segment_map); 5556 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5557 if (m == NULL) 5558 goto error_return; 5559 m->next = NULL; 5560 m->p_type = PT_GNU_STACK; 5561 m->p_flags = elf_stack_flags (abfd); 5562 m->p_align = bed->stack_align; 5563 m->p_flags_valid = 1; 5564 m->p_align_valid = m->p_align != 0; 5565 if (info->stacksize > 0) 5566 { 5567 m->p_size = info->stacksize; 5568 m->p_size_valid = 1; 5569 } 5570 5571 *pm = m; 5572 pm = &m->next; 5573 } 5574 5575 if (info != NULL && info->relro) 5576 { 5577 for (m = mfirst; m != NULL; m = m->next) 5578 { 5579 if (m->p_type == PT_LOAD 5580 && m->count != 0 5581 && m->sections[0]->vma >= info->relro_start 5582 && m->sections[0]->vma < info->relro_end) 5583 { 5584 i = m->count; 5585 while (--i != (unsigned) -1) 5586 { 5587 if (m->sections[i]->size > 0 5588 && (m->sections[i]->flags & SEC_LOAD) != 0 5589 && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0) 5590 break; 5591 } 5592 5593 if (i != (unsigned) -1) 5594 break; 5595 } 5596 } 5597 5598 /* Make a PT_GNU_RELRO segment only when it isn't empty. */ 5599 if (m != NULL) 5600 { 5601 amt = sizeof (struct elf_segment_map); 5602 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5603 if (m == NULL) 5604 goto error_return; 5605 m->next = NULL; 5606 m->p_type = PT_GNU_RELRO; 5607 *pm = m; 5608 pm = &m->next; 5609 } 5610 } 5611 5612 free (sections); 5613 elf_seg_map (abfd) = mfirst; 5614 } 5615 5616 if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL)) 5617 return false; 5618 5619 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) 5620 ++count; 5621 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; 5622 5623 return true; 5624 5625 error_return: 5626 free (sections); 5627 return false; 5628 } 5629 5630 /* Sort sections by address. */ 5631 5632 static int 5633 elf_sort_sections (const void *arg1, const void *arg2) 5634 { 5635 const asection *sec1 = *(const asection **) arg1; 5636 const asection *sec2 = *(const asection **) arg2; 5637 bfd_size_type size1, size2; 5638 5639 /* Sort by LMA first, since this is the address used to 5640 place the section into a segment. */ 5641 if (sec1->lma < sec2->lma) 5642 return -1; 5643 else if (sec1->lma > sec2->lma) 5644 return 1; 5645 5646 /* Then sort by VMA. Normally the LMA and the VMA will be 5647 the same, and this will do nothing. */ 5648 if (sec1->vma < sec2->vma) 5649 return -1; 5650 else if (sec1->vma > sec2->vma) 5651 return 1; 5652 5653 /* Put !SEC_LOAD sections after SEC_LOAD ones. */ 5654 5655 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \ 5656 && (x)->size != 0) 5657 5658 if (TOEND (sec1)) 5659 { 5660 if (!TOEND (sec2)) 5661 return 1; 5662 } 5663 else if (TOEND (sec2)) 5664 return -1; 5665 5666 #undef TOEND 5667 5668 /* Sort by size, to put zero sized sections 5669 before others at the same address. */ 5670 5671 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; 5672 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; 5673 5674 if (size1 < size2) 5675 return -1; 5676 if (size1 > size2) 5677 return 1; 5678 5679 return sec1->target_index - sec2->target_index; 5680 } 5681 5682 /* This qsort comparison functions sorts PT_LOAD segments first and 5683 by p_paddr, for assign_file_positions_for_load_sections. */ 5684 5685 static int 5686 elf_sort_segments (const void *arg1, const void *arg2) 5687 { 5688 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1; 5689 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2; 5690 5691 if (m1->p_type != m2->p_type) 5692 { 5693 if (m1->p_type == PT_NULL) 5694 return 1; 5695 if (m2->p_type == PT_NULL) 5696 return -1; 5697 return m1->p_type < m2->p_type ? -1 : 1; 5698 } 5699 if (m1->includes_filehdr != m2->includes_filehdr) 5700 return m1->includes_filehdr ? -1 : 1; 5701 if (m1->no_sort_lma != m2->no_sort_lma) 5702 return m1->no_sort_lma ? -1 : 1; 5703 if (m1->p_type == PT_LOAD && !m1->no_sort_lma) 5704 { 5705 bfd_vma lma1, lma2; /* Octets. */ 5706 lma1 = 0; 5707 if (m1->p_paddr_valid) 5708 lma1 = m1->p_paddr; 5709 else if (m1->count != 0) 5710 { 5711 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner, 5712 m1->sections[0]); 5713 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb; 5714 } 5715 lma2 = 0; 5716 if (m2->p_paddr_valid) 5717 lma2 = m2->p_paddr; 5718 else if (m2->count != 0) 5719 { 5720 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner, 5721 m2->sections[0]); 5722 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb; 5723 } 5724 if (lma1 != lma2) 5725 return lma1 < lma2 ? -1 : 1; 5726 } 5727 if (m1->idx != m2->idx) 5728 return m1->idx < m2->idx ? -1 : 1; 5729 return 0; 5730 } 5731 5732 /* Ian Lance Taylor writes: 5733 5734 We shouldn't be using % with a negative signed number. That's just 5735 not good. We have to make sure either that the number is not 5736 negative, or that the number has an unsigned type. When the types 5737 are all the same size they wind up as unsigned. When file_ptr is a 5738 larger signed type, the arithmetic winds up as signed long long, 5739 which is wrong. 5740 5741 What we're trying to say here is something like ``increase OFF by 5742 the least amount that will cause it to be equal to the VMA modulo 5743 the page size.'' */ 5744 /* In other words, something like: 5745 5746 vma_offset = m->sections[0]->vma % bed->maxpagesize; 5747 off_offset = off % bed->maxpagesize; 5748 if (vma_offset < off_offset) 5749 adjustment = vma_offset + bed->maxpagesize - off_offset; 5750 else 5751 adjustment = vma_offset - off_offset; 5752 5753 which can be collapsed into the expression below. */ 5754 5755 static file_ptr 5756 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) 5757 { 5758 /* PR binutils/16199: Handle an alignment of zero. */ 5759 if (maxpagesize == 0) 5760 maxpagesize = 1; 5761 return ((vma - off) % maxpagesize); 5762 } 5763 5764 static void 5765 print_segment_map (const struct elf_segment_map *m) 5766 { 5767 unsigned int j; 5768 const char *pt = get_segment_type (m->p_type); 5769 char buf[32]; 5770 5771 if (pt == NULL) 5772 { 5773 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) 5774 sprintf (buf, "LOPROC+%7.7x", 5775 (unsigned int) (m->p_type - PT_LOPROC)); 5776 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) 5777 sprintf (buf, "LOOS+%7.7x", 5778 (unsigned int) (m->p_type - PT_LOOS)); 5779 else 5780 snprintf (buf, sizeof (buf), "%8.8x", 5781 (unsigned int) m->p_type); 5782 pt = buf; 5783 } 5784 fflush (stdout); 5785 fprintf (stderr, "%s:", pt); 5786 for (j = 0; j < m->count; j++) 5787 fprintf (stderr, " %s", m->sections [j]->name); 5788 putc ('\n',stderr); 5789 fflush (stderr); 5790 } 5791 5792 /* Assign file positions to the sections based on the mapping from 5793 sections to segments. This function also sets up some fields in 5794 the file header. */ 5795 5796 static bool 5797 assign_file_positions_for_load_sections (bfd *abfd, 5798 struct bfd_link_info *link_info) 5799 { 5800 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 5801 struct elf_segment_map *m; 5802 struct elf_segment_map *phdr_load_seg; 5803 Elf_Internal_Phdr *phdrs; 5804 Elf_Internal_Phdr *p; 5805 file_ptr off; /* Octets. */ 5806 bfd_size_type maxpagesize; 5807 unsigned int alloc, actual; 5808 unsigned int i, j; 5809 struct elf_segment_map **sorted_seg_map; 5810 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 5811 5812 if (link_info == NULL 5813 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL)) 5814 return false; 5815 5816 alloc = 0; 5817 for (m = elf_seg_map (abfd); m != NULL; m = m->next) 5818 m->idx = alloc++; 5819 5820 if (alloc) 5821 { 5822 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; 5823 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; 5824 } 5825 else 5826 { 5827 /* PR binutils/12467. */ 5828 elf_elfheader (abfd)->e_phoff = 0; 5829 elf_elfheader (abfd)->e_phentsize = 0; 5830 } 5831 5832 elf_elfheader (abfd)->e_phnum = alloc; 5833 5834 if (elf_program_header_size (abfd) == (bfd_size_type) -1) 5835 { 5836 actual = alloc; 5837 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; 5838 } 5839 else 5840 { 5841 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr; 5842 BFD_ASSERT (elf_program_header_size (abfd) 5843 == actual * bed->s->sizeof_phdr); 5844 BFD_ASSERT (actual >= alloc); 5845 } 5846 5847 if (alloc == 0) 5848 { 5849 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; 5850 return true; 5851 } 5852 5853 /* We're writing the size in elf_program_header_size (abfd), 5854 see assign_file_positions_except_relocs, so make sure we have 5855 that amount allocated, with trailing space cleared. 5856 The variable alloc contains the computed need, while 5857 elf_program_header_size (abfd) contains the size used for the 5858 layout. 5859 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments 5860 where the layout is forced to according to a larger size in the 5861 last iterations for the testcase ld-elf/header. */ 5862 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs) 5863 + alloc * sizeof (*sorted_seg_map))); 5864 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual); 5865 elf_tdata (abfd)->phdr = phdrs; 5866 if (phdrs == NULL) 5867 return false; 5868 5869 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++) 5870 { 5871 sorted_seg_map[j] = m; 5872 /* If elf_segment_map is not from map_sections_to_segments, the 5873 sections may not be correctly ordered. NOTE: sorting should 5874 not be done to the PT_NOTE section of a corefile, which may 5875 contain several pseudo-sections artificially created by bfd. 5876 Sorting these pseudo-sections breaks things badly. */ 5877 if (m->count > 1 5878 && !(elf_elfheader (abfd)->e_type == ET_CORE 5879 && m->p_type == PT_NOTE)) 5880 { 5881 for (i = 0; i < m->count; i++) 5882 m->sections[i]->target_index = i; 5883 qsort (m->sections, (size_t) m->count, sizeof (asection *), 5884 elf_sort_sections); 5885 } 5886 } 5887 if (alloc > 1) 5888 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map), 5889 elf_sort_segments); 5890 5891 maxpagesize = 1; 5892 if ((abfd->flags & D_PAGED) != 0) 5893 { 5894 if (link_info != NULL) 5895 maxpagesize = link_info->maxpagesize; 5896 else 5897 maxpagesize = bed->maxpagesize; 5898 } 5899 5900 /* Sections must map to file offsets past the ELF file header. */ 5901 off = bed->s->sizeof_ehdr; 5902 /* And if one of the PT_LOAD headers doesn't include the program 5903 headers then we'll be mapping program headers in the usual 5904 position after the ELF file header. */ 5905 phdr_load_seg = NULL; 5906 for (j = 0; j < alloc; j++) 5907 { 5908 m = sorted_seg_map[j]; 5909 if (m->p_type != PT_LOAD) 5910 break; 5911 if (m->includes_phdrs) 5912 { 5913 phdr_load_seg = m; 5914 break; 5915 } 5916 } 5917 if (phdr_load_seg == NULL) 5918 off += actual * bed->s->sizeof_phdr; 5919 5920 for (j = 0; j < alloc; j++) 5921 { 5922 asection **secpp; 5923 bfd_vma off_adjust; /* Octets. */ 5924 bool no_contents; 5925 bfd_size_type p_align; 5926 bool p_align_p; 5927 5928 /* An ELF segment (described by Elf_Internal_Phdr) may contain a 5929 number of sections with contents contributing to both p_filesz 5930 and p_memsz, followed by a number of sections with no contents 5931 that just contribute to p_memsz. In this loop, OFF tracks next 5932 available file offset for PT_LOAD and PT_NOTE segments. */ 5933 m = sorted_seg_map[j]; 5934 p = phdrs + m->idx; 5935 p->p_type = m->p_type; 5936 p->p_flags = m->p_flags; 5937 p_align = bed->p_align; 5938 p_align_p = false; 5939 5940 if (m->count == 0) 5941 p->p_vaddr = m->p_vaddr_offset * opb; 5942 else 5943 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb; 5944 5945 if (m->p_paddr_valid) 5946 p->p_paddr = m->p_paddr; 5947 else if (m->count == 0) 5948 p->p_paddr = 0; 5949 else 5950 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb; 5951 5952 if (p->p_type == PT_LOAD 5953 && (abfd->flags & D_PAGED) != 0) 5954 { 5955 /* p_align in demand paged PT_LOAD segments effectively stores 5956 the maximum page size. When copying an executable with 5957 objcopy, we set m->p_align from the input file. Use this 5958 value for maxpagesize rather than bed->maxpagesize, which 5959 may be different. Note that we use maxpagesize for PT_TLS 5960 segment alignment later in this function, so we are relying 5961 on at least one PT_LOAD segment appearing before a PT_TLS 5962 segment. */ 5963 if (m->p_align_valid) 5964 maxpagesize = m->p_align; 5965 else if (p_align != 0 5966 && (link_info == NULL 5967 || !link_info->maxpagesize_is_set)) 5968 /* Set p_align to the default p_align value while laying 5969 out segments aligning to the maximum page size or the 5970 largest section alignment. The run-time loader can 5971 align segments to the default p_align value or the 5972 maximum page size, depending on system page size. */ 5973 p_align_p = true; 5974 5975 p->p_align = maxpagesize; 5976 } 5977 else if (m->p_align_valid) 5978 p->p_align = m->p_align; 5979 else if (m->count == 0) 5980 p->p_align = 1 << bed->s->log_file_align; 5981 5982 if (m == phdr_load_seg) 5983 off += actual * bed->s->sizeof_phdr; 5984 5985 no_contents = false; 5986 off_adjust = 0; 5987 if (p->p_type == PT_LOAD 5988 && m->count > 0) 5989 { 5990 bfd_size_type align; /* Bytes. */ 5991 unsigned int align_power = 0; 5992 5993 if (m->p_align_valid) 5994 align = p->p_align; 5995 else 5996 { 5997 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 5998 { 5999 unsigned int secalign; 6000 6001 secalign = bfd_section_alignment (*secpp); 6002 if (secalign > align_power) 6003 align_power = secalign; 6004 } 6005 align = (bfd_size_type) 1 << align_power; 6006 if (align < maxpagesize) 6007 { 6008 /* If a section requires alignment higher than the 6009 default p_align value, don't set p_align to the 6010 default p_align value. */ 6011 if (align > p_align) 6012 p_align_p = false; 6013 align = maxpagesize; 6014 } 6015 else 6016 { 6017 /* If a section requires alignment higher than the 6018 maximum page size, set p_align to the section 6019 alignment. */ 6020 p_align_p = true; 6021 p_align = align; 6022 } 6023 } 6024 6025 for (i = 0; i < m->count; i++) 6026 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) 6027 /* If we aren't making room for this section, then 6028 it must be SHT_NOBITS regardless of what we've 6029 set via struct bfd_elf_special_section. */ 6030 elf_section_type (m->sections[i]) = SHT_NOBITS; 6031 6032 /* Find out whether this segment contains any loadable 6033 sections. */ 6034 no_contents = true; 6035 for (i = 0; i < m->count; i++) 6036 if (elf_section_type (m->sections[i]) != SHT_NOBITS) 6037 { 6038 no_contents = false; 6039 break; 6040 } 6041 6042 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb); 6043 6044 /* Broken hardware and/or kernel require that files do not 6045 map the same page with different permissions on some hppa 6046 processors. */ 6047 if (j != 0 6048 && (abfd->flags & D_PAGED) != 0 6049 && bed->no_page_alias 6050 && (off & (maxpagesize - 1)) != 0 6051 && ((off & -maxpagesize) 6052 == ((off + off_adjust) & -maxpagesize))) 6053 off_adjust += maxpagesize; 6054 off += off_adjust; 6055 if (no_contents) 6056 { 6057 /* We shouldn't need to align the segment on disk since 6058 the segment doesn't need file space, but the gABI 6059 arguably requires the alignment and glibc ld.so 6060 checks it. So to comply with the alignment 6061 requirement but not waste file space, we adjust 6062 p_offset for just this segment. (OFF_ADJUST is 6063 subtracted from OFF later.) This may put p_offset 6064 past the end of file, but that shouldn't matter. */ 6065 } 6066 else 6067 off_adjust = 0; 6068 } 6069 /* Make sure the .dynamic section is the first section in the 6070 PT_DYNAMIC segment. */ 6071 else if (p->p_type == PT_DYNAMIC 6072 && m->count > 1 6073 && strcmp (m->sections[0]->name, ".dynamic") != 0) 6074 { 6075 _bfd_error_handler 6076 (_("%pB: The first section in the PT_DYNAMIC segment" 6077 " is not the .dynamic section"), 6078 abfd); 6079 bfd_set_error (bfd_error_bad_value); 6080 return false; 6081 } 6082 /* Set the note section type to SHT_NOTE. */ 6083 else if (p->p_type == PT_NOTE) 6084 for (i = 0; i < m->count; i++) 6085 elf_section_type (m->sections[i]) = SHT_NOTE; 6086 6087 if (m->includes_filehdr) 6088 { 6089 if (!m->p_flags_valid) 6090 p->p_flags |= PF_R; 6091 p->p_filesz = bed->s->sizeof_ehdr; 6092 p->p_memsz = bed->s->sizeof_ehdr; 6093 if (p->p_type == PT_LOAD) 6094 { 6095 if (m->count > 0) 6096 { 6097 if (p->p_vaddr < (bfd_vma) off 6098 || (!m->p_paddr_valid 6099 && p->p_paddr < (bfd_vma) off)) 6100 { 6101 _bfd_error_handler 6102 (_("%pB: not enough room for program headers," 6103 " try linking with -N"), 6104 abfd); 6105 bfd_set_error (bfd_error_bad_value); 6106 return false; 6107 } 6108 p->p_vaddr -= off; 6109 if (!m->p_paddr_valid) 6110 p->p_paddr -= off; 6111 } 6112 } 6113 else if (sorted_seg_map[0]->includes_filehdr) 6114 { 6115 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx; 6116 p->p_vaddr = filehdr->p_vaddr; 6117 if (!m->p_paddr_valid) 6118 p->p_paddr = filehdr->p_paddr; 6119 } 6120 } 6121 6122 if (m->includes_phdrs) 6123 { 6124 if (!m->p_flags_valid) 6125 p->p_flags |= PF_R; 6126 p->p_filesz += actual * bed->s->sizeof_phdr; 6127 p->p_memsz += actual * bed->s->sizeof_phdr; 6128 if (!m->includes_filehdr) 6129 { 6130 if (p->p_type == PT_LOAD) 6131 { 6132 p->p_offset = off - actual * bed->s->sizeof_phdr; 6133 elf_elfheader (abfd)->e_phoff = p->p_offset; 6134 if (m->count > 0) 6135 { 6136 p->p_vaddr -= off - p->p_offset; 6137 if (!m->p_paddr_valid) 6138 p->p_paddr -= off - p->p_offset; 6139 } 6140 } 6141 else if (phdr_load_seg != NULL) 6142 { 6143 /* Also set PT_PHDR to match phdr_load_seg. We've 6144 sorted segments so that phdr_load_seg will 6145 already be set by the code immediately above. */ 6146 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx; 6147 bfd_vma phdr_off = 0; /* Octets. */ 6148 if (phdr_load_seg->includes_filehdr) 6149 phdr_off = bed->s->sizeof_ehdr; 6150 p->p_vaddr = phdr->p_vaddr + phdr_off; 6151 if (!m->p_paddr_valid) 6152 p->p_paddr = phdr->p_paddr + phdr_off; 6153 p->p_offset = phdr->p_offset + phdr_off; 6154 } 6155 else 6156 p->p_offset = bed->s->sizeof_ehdr; 6157 } 6158 } 6159 6160 if (p->p_type == PT_LOAD 6161 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) 6162 { 6163 if (!m->includes_filehdr && !m->includes_phdrs) 6164 { 6165 p->p_offset = off; 6166 if (no_contents) 6167 { 6168 /* Put meaningless p_offset for PT_LOAD segments 6169 without file contents somewhere within the first 6170 page, in an attempt to not point past EOF. */ 6171 bfd_size_type align = maxpagesize; 6172 if (align < p->p_align) 6173 align = p->p_align; 6174 if (align < 1) 6175 align = 1; 6176 p->p_offset = off % align; 6177 } 6178 } 6179 else 6180 { 6181 file_ptr adjust; /* Octets. */ 6182 6183 adjust = off - (p->p_offset + p->p_filesz); 6184 if (!no_contents) 6185 p->p_filesz += adjust; 6186 p->p_memsz += adjust; 6187 } 6188 } 6189 6190 /* Set up p_filesz, p_memsz, p_align and p_flags from the section 6191 maps. Set filepos for sections in PT_LOAD segments, and in 6192 core files, for sections in PT_NOTE segments. 6193 assign_file_positions_for_non_load_sections will set filepos 6194 for other sections and update p_filesz for other segments. */ 6195 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 6196 { 6197 asection *sec; 6198 bfd_size_type align; 6199 Elf_Internal_Shdr *this_hdr; 6200 6201 sec = *secpp; 6202 this_hdr = &elf_section_data (sec)->this_hdr; 6203 align = (bfd_size_type) 1 << bfd_section_alignment (sec); 6204 6205 if ((p->p_type == PT_LOAD 6206 || p->p_type == PT_TLS) 6207 && (this_hdr->sh_type != SHT_NOBITS 6208 || ((this_hdr->sh_flags & SHF_ALLOC) != 0 6209 && ((this_hdr->sh_flags & SHF_TLS) == 0 6210 || p->p_type == PT_TLS)))) 6211 { 6212 bfd_vma p_start = p->p_paddr; /* Octets. */ 6213 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */ 6214 bfd_vma s_start = sec->lma * opb; /* Octets. */ 6215 bfd_vma adjust = s_start - p_end; /* Octets. */ 6216 6217 if (adjust != 0 6218 && (s_start < p_end 6219 || p_end < p_start)) 6220 { 6221 _bfd_error_handler 6222 /* xgettext:c-format */ 6223 (_("%pB: section %pA lma %#" PRIx64 6224 " adjusted to %#" PRIx64), 6225 abfd, sec, (uint64_t) s_start / opb, 6226 (uint64_t) p_end / opb); 6227 adjust = 0; 6228 sec->lma = p_end / opb; 6229 } 6230 p->p_memsz += adjust; 6231 6232 if (p->p_type == PT_LOAD) 6233 { 6234 if (this_hdr->sh_type != SHT_NOBITS) 6235 { 6236 off_adjust = 0; 6237 if (p->p_filesz + adjust < p->p_memsz) 6238 { 6239 /* We have a PROGBITS section following NOBITS ones. 6240 Allocate file space for the NOBITS section(s). 6241 We don't need to write out the zeros, posix 6242 fseek past the end of data already written 6243 followed by a write at that location is 6244 guaranteed to result in zeros being read 6245 from the gap. */ 6246 adjust = p->p_memsz - p->p_filesz; 6247 } 6248 } 6249 /* We only adjust sh_offset in SHT_NOBITS sections 6250 as would seem proper for their address when the 6251 section is first in the segment. sh_offset 6252 doesn't really have any significance for 6253 SHT_NOBITS anyway, apart from a notional position 6254 relative to other sections. Historically we 6255 didn't bother with adjusting sh_offset and some 6256 programs depend on it not being adjusted. See 6257 pr12921 and pr25662. */ 6258 if (this_hdr->sh_type != SHT_NOBITS || i == 0) 6259 { 6260 off += adjust; 6261 if (this_hdr->sh_type == SHT_NOBITS) 6262 off_adjust += adjust; 6263 } 6264 } 6265 if (this_hdr->sh_type != SHT_NOBITS) 6266 p->p_filesz += adjust; 6267 } 6268 6269 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) 6270 { 6271 /* The section at i == 0 is the one that actually contains 6272 everything. */ 6273 if (i == 0) 6274 { 6275 this_hdr->sh_offset = sec->filepos = off; 6276 off += this_hdr->sh_size; 6277 p->p_filesz = this_hdr->sh_size; 6278 p->p_memsz = 0; 6279 p->p_align = 1; 6280 } 6281 else 6282 { 6283 /* The rest are fake sections that shouldn't be written. */ 6284 sec->filepos = 0; 6285 sec->size = 0; 6286 sec->flags = 0; 6287 continue; 6288 } 6289 } 6290 else 6291 { 6292 if (p->p_type == PT_LOAD) 6293 { 6294 this_hdr->sh_offset = sec->filepos = off; 6295 if (this_hdr->sh_type != SHT_NOBITS) 6296 off += this_hdr->sh_size; 6297 } 6298 else if (this_hdr->sh_type == SHT_NOBITS 6299 && (this_hdr->sh_flags & SHF_TLS) != 0 6300 && this_hdr->sh_offset == 0) 6301 { 6302 /* This is a .tbss section that didn't get a PT_LOAD. 6303 (See _bfd_elf_map_sections_to_segments "Create a 6304 final PT_LOAD".) Set sh_offset to the value it 6305 would have if we had created a zero p_filesz and 6306 p_memsz PT_LOAD header for the section. This 6307 also makes the PT_TLS header have the same 6308 p_offset value. */ 6309 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, 6310 off, align); 6311 this_hdr->sh_offset = sec->filepos = off + adjust; 6312 } 6313 6314 if (this_hdr->sh_type != SHT_NOBITS) 6315 { 6316 p->p_filesz += this_hdr->sh_size; 6317 /* A load section without SHF_ALLOC is something like 6318 a note section in a PT_NOTE segment. These take 6319 file space but are not loaded into memory. */ 6320 if ((this_hdr->sh_flags & SHF_ALLOC) != 0) 6321 p->p_memsz += this_hdr->sh_size; 6322 } 6323 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) 6324 { 6325 if (p->p_type == PT_TLS) 6326 p->p_memsz += this_hdr->sh_size; 6327 6328 /* .tbss is special. It doesn't contribute to p_memsz of 6329 normal segments. */ 6330 else if ((this_hdr->sh_flags & SHF_TLS) == 0) 6331 p->p_memsz += this_hdr->sh_size; 6332 } 6333 6334 if (align > p->p_align 6335 && !m->p_align_valid 6336 && (p->p_type != PT_LOAD 6337 || (abfd->flags & D_PAGED) == 0)) 6338 p->p_align = align; 6339 } 6340 6341 if (!m->p_flags_valid) 6342 { 6343 p->p_flags |= PF_R; 6344 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) 6345 p->p_flags |= PF_X; 6346 if ((this_hdr->sh_flags & SHF_WRITE) != 0) 6347 p->p_flags |= PF_W; 6348 } 6349 } 6350 6351 off -= off_adjust; 6352 6353 /* PR ld/20815 - Check that the program header segment, if 6354 present, will be loaded into memory. */ 6355 if (p->p_type == PT_PHDR 6356 && phdr_load_seg == NULL 6357 && !(bed->elf_backend_allow_non_load_phdr != NULL 6358 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc))) 6359 { 6360 /* The fix for this error is usually to edit the linker script being 6361 used and set up the program headers manually. Either that or 6362 leave room for the headers at the start of the SECTIONS. */ 6363 _bfd_error_handler (_("%pB: error: PHDR segment not covered" 6364 " by LOAD segment"), 6365 abfd); 6366 if (link_info == NULL) 6367 return false; 6368 /* Arrange for the linker to exit with an error, deleting 6369 the output file unless --noinhibit-exec is given. */ 6370 link_info->callbacks->info ("%X"); 6371 } 6372 6373 /* Check that all sections are in a PT_LOAD segment. 6374 Don't check funky gdb generated core files. */ 6375 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) 6376 { 6377 bool check_vma = true; 6378 6379 for (i = 1; i < m->count; i++) 6380 if (m->sections[i]->vma == m->sections[i - 1]->vma 6381 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) 6382 ->this_hdr), p) != 0 6383 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) 6384 ->this_hdr), p) != 0) 6385 { 6386 /* Looks like we have overlays packed into the segment. */ 6387 check_vma = false; 6388 break; 6389 } 6390 6391 for (i = 0; i < m->count; i++) 6392 { 6393 Elf_Internal_Shdr *this_hdr; 6394 asection *sec; 6395 6396 sec = m->sections[i]; 6397 this_hdr = &(elf_section_data(sec)->this_hdr); 6398 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) 6399 && !ELF_TBSS_SPECIAL (this_hdr, p)) 6400 { 6401 _bfd_error_handler 6402 /* xgettext:c-format */ 6403 (_("%pB: section `%pA' can't be allocated in segment %u"), 6404 abfd, sec, m->idx); 6405 print_segment_map (m); 6406 } 6407 } 6408 6409 if (p_align_p) 6410 p->p_align = p_align; 6411 } 6412 } 6413 6414 elf_next_file_pos (abfd) = off; 6415 6416 if (link_info != NULL 6417 && phdr_load_seg != NULL 6418 && phdr_load_seg->includes_filehdr) 6419 { 6420 /* There is a segment that contains both the file headers and the 6421 program headers, so provide a symbol __ehdr_start pointing there. 6422 A program can use this to examine itself robustly. */ 6423 6424 struct elf_link_hash_entry *hash 6425 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", 6426 false, false, true); 6427 /* If the symbol was referenced and not defined, define it. */ 6428 if (hash != NULL 6429 && (hash->root.type == bfd_link_hash_new 6430 || hash->root.type == bfd_link_hash_undefined 6431 || hash->root.type == bfd_link_hash_undefweak 6432 || hash->root.type == bfd_link_hash_common)) 6433 { 6434 asection *s = NULL; 6435 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb; 6436 6437 if (phdr_load_seg->count != 0) 6438 /* The segment contains sections, so use the first one. */ 6439 s = phdr_load_seg->sections[0]; 6440 else 6441 /* Use the first (i.e. lowest-addressed) section in any segment. */ 6442 for (m = elf_seg_map (abfd); m != NULL; m = m->next) 6443 if (m->p_type == PT_LOAD && m->count != 0) 6444 { 6445 s = m->sections[0]; 6446 break; 6447 } 6448 6449 if (s != NULL) 6450 { 6451 hash->root.u.def.value = filehdr_vaddr - s->vma; 6452 hash->root.u.def.section = s; 6453 } 6454 else 6455 { 6456 hash->root.u.def.value = filehdr_vaddr; 6457 hash->root.u.def.section = bfd_abs_section_ptr; 6458 } 6459 6460 hash->root.type = bfd_link_hash_defined; 6461 hash->def_regular = 1; 6462 hash->non_elf = 0; 6463 } 6464 } 6465 6466 return true; 6467 } 6468 6469 /* Determine if a bfd is a debuginfo file. Unfortunately there 6470 is no defined method for detecting such files, so we have to 6471 use heuristics instead. */ 6472 6473 bool 6474 is_debuginfo_file (bfd *abfd) 6475 { 6476 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour) 6477 return false; 6478 6479 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd); 6480 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd); 6481 Elf_Internal_Shdr **headerp; 6482 6483 for (headerp = start_headers; headerp < end_headers; headerp ++) 6484 { 6485 Elf_Internal_Shdr *header = * headerp; 6486 6487 /* Debuginfo files do not have any allocated SHT_PROGBITS sections. 6488 The only allocated sections are SHT_NOBITS or SHT_NOTES. */ 6489 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC 6490 && header->sh_type != SHT_NOBITS 6491 && header->sh_type != SHT_NOTE) 6492 return false; 6493 } 6494 6495 return true; 6496 } 6497 6498 /* Assign file positions for other sections, except for compressed debug 6499 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */ 6500 6501 static bool 6502 assign_file_positions_for_non_load_sections (bfd *abfd, 6503 struct bfd_link_info *link_info) 6504 { 6505 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6506 Elf_Internal_Shdr **i_shdrpp; 6507 Elf_Internal_Shdr **hdrpp, **end_hdrpp; 6508 Elf_Internal_Phdr *phdrs; 6509 Elf_Internal_Phdr *p; 6510 struct elf_segment_map *m; 6511 file_ptr off; 6512 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 6513 bfd_vma maxpagesize; 6514 6515 if (link_info != NULL) 6516 maxpagesize = link_info->maxpagesize; 6517 else 6518 maxpagesize = bed->maxpagesize; 6519 i_shdrpp = elf_elfsections (abfd); 6520 end_hdrpp = i_shdrpp + elf_numsections (abfd); 6521 off = elf_next_file_pos (abfd); 6522 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) 6523 { 6524 Elf_Internal_Shdr *hdr; 6525 bfd_vma align; 6526 6527 hdr = *hdrpp; 6528 if (hdr->bfd_section != NULL 6529 && (hdr->bfd_section->filepos != 0 6530 || (hdr->sh_type == SHT_NOBITS 6531 && hdr->contents == NULL))) 6532 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); 6533 else if ((hdr->sh_flags & SHF_ALLOC) != 0) 6534 { 6535 if (hdr->sh_size != 0 6536 /* PR 24717 - debuginfo files are known to be not strictly 6537 compliant with the ELF standard. In particular they often 6538 have .note.gnu.property sections that are outside of any 6539 loadable segment. This is not a problem for such files, 6540 so do not warn about them. */ 6541 && ! is_debuginfo_file (abfd)) 6542 _bfd_error_handler 6543 /* xgettext:c-format */ 6544 (_("%pB: warning: allocated section `%s' not in segment"), 6545 abfd, 6546 (hdr->bfd_section == NULL 6547 ? "*unknown*" 6548 : hdr->bfd_section->name)); 6549 /* We don't need to page align empty sections. */ 6550 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) 6551 align = maxpagesize; 6552 else 6553 align = hdr->sh_addralign & -hdr->sh_addralign; 6554 off += vma_page_aligned_bias (hdr->sh_addr, off, align); 6555 off = _bfd_elf_assign_file_position_for_section (hdr, off, false, 6556 bed->s->log_file_align); 6557 } 6558 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 6559 && hdr->bfd_section == NULL) 6560 /* We don't know the offset of these sections yet: 6561 their size has not been decided. */ 6562 || (abfd->is_linker_output 6563 && hdr->bfd_section != NULL 6564 && (hdr->sh_name == -1u 6565 || bfd_section_is_ctf (hdr->bfd_section))) 6566 || hdr == i_shdrpp[elf_onesymtab (abfd)] 6567 || (elf_symtab_shndx_list (abfd) != NULL 6568 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) 6569 || hdr == i_shdrpp[elf_strtab_sec (abfd)] 6570 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) 6571 hdr->sh_offset = -1; 6572 else 6573 off = _bfd_elf_assign_file_position_for_section (hdr, off, true, 0); 6574 } 6575 elf_next_file_pos (abfd) = off; 6576 6577 /* Now that we have set the section file positions, we can set up 6578 the file positions for the non PT_LOAD segments. */ 6579 phdrs = elf_tdata (abfd)->phdr; 6580 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) 6581 { 6582 if (p->p_type == PT_GNU_RELRO) 6583 { 6584 bfd_vma start, end; /* Bytes. */ 6585 bool ok; 6586 6587 if (link_info != NULL) 6588 { 6589 /* During linking the range of the RELRO segment is passed 6590 in link_info. Note that there may be padding between 6591 relro_start and the first RELRO section. */ 6592 start = link_info->relro_start; 6593 end = link_info->relro_end; 6594 } 6595 else if (m->count != 0) 6596 { 6597 if (!m->p_size_valid) 6598 abort (); 6599 start = m->sections[0]->vma; 6600 end = start + m->p_size / opb; 6601 } 6602 else 6603 { 6604 start = 0; 6605 end = 0; 6606 } 6607 6608 ok = false; 6609 if (start < end) 6610 { 6611 struct elf_segment_map *lm; 6612 const Elf_Internal_Phdr *lp; 6613 unsigned int i; 6614 6615 /* Find a LOAD segment containing a section in the RELRO 6616 segment. */ 6617 for (lm = elf_seg_map (abfd), lp = phdrs; 6618 lm != NULL; 6619 lm = lm->next, lp++) 6620 { 6621 if (lp->p_type == PT_LOAD 6622 && lm->count != 0 6623 && (lm->sections[lm->count - 1]->vma 6624 + (!IS_TBSS (lm->sections[lm->count - 1]) 6625 ? lm->sections[lm->count - 1]->size / opb 6626 : 0)) > start 6627 && lm->sections[0]->vma < end) 6628 break; 6629 } 6630 6631 if (lm != NULL) 6632 { 6633 /* Find the section starting the RELRO segment. */ 6634 for (i = 0; i < lm->count; i++) 6635 { 6636 asection *s = lm->sections[i]; 6637 if (s->vma >= start 6638 && s->vma < end 6639 && s->size != 0) 6640 break; 6641 } 6642 6643 if (i < lm->count) 6644 { 6645 p->p_vaddr = lm->sections[i]->vma * opb; 6646 p->p_paddr = lm->sections[i]->lma * opb; 6647 p->p_offset = lm->sections[i]->filepos; 6648 p->p_memsz = end * opb - p->p_vaddr; 6649 p->p_filesz = p->p_memsz; 6650 6651 /* The RELRO segment typically ends a few bytes 6652 into .got.plt but other layouts are possible. 6653 In cases where the end does not match any 6654 loaded section (for instance is in file 6655 padding), trim p_filesz back to correspond to 6656 the end of loaded section contents. */ 6657 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr) 6658 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr; 6659 6660 /* Preserve the alignment and flags if they are 6661 valid. The gold linker generates RW/4 for 6662 the PT_GNU_RELRO section. It is better for 6663 objcopy/strip to honor these attributes 6664 otherwise gdb will choke when using separate 6665 debug files. */ 6666 if (!m->p_align_valid) 6667 p->p_align = 1; 6668 if (!m->p_flags_valid) 6669 p->p_flags = PF_R; 6670 ok = true; 6671 } 6672 } 6673 } 6674 6675 if (!ok) 6676 { 6677 if (link_info != NULL) 6678 _bfd_error_handler 6679 (_("%pB: warning: unable to allocate any sections" 6680 " to PT_GNU_RELRO segment"), 6681 abfd); 6682 memset (p, 0, sizeof *p); 6683 } 6684 } 6685 else if (p->p_type == PT_GNU_STACK) 6686 { 6687 if (m->p_size_valid) 6688 p->p_memsz = m->p_size; 6689 } 6690 else if (m->count != 0) 6691 { 6692 unsigned int i; 6693 6694 if (p->p_type != PT_LOAD 6695 && (p->p_type != PT_NOTE 6696 || bfd_get_format (abfd) != bfd_core)) 6697 { 6698 /* A user specified segment layout may include a PHDR 6699 segment that overlaps with a LOAD segment... */ 6700 if (p->p_type == PT_PHDR) 6701 { 6702 m->count = 0; 6703 continue; 6704 } 6705 6706 if (m->includes_filehdr || m->includes_phdrs) 6707 { 6708 /* PR 17512: file: 2195325e. */ 6709 _bfd_error_handler 6710 (_("%pB: error: non-load segment %d includes file header " 6711 "and/or program header"), 6712 abfd, (int) (p - phdrs)); 6713 return false; 6714 } 6715 6716 p->p_filesz = 0; 6717 p->p_offset = m->sections[0]->filepos; 6718 for (i = m->count; i-- != 0;) 6719 { 6720 asection *sect = m->sections[i]; 6721 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; 6722 if (hdr->sh_type != SHT_NOBITS) 6723 { 6724 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size; 6725 /* NB: p_memsz of the loadable PT_NOTE segment 6726 should be the same as p_filesz. */ 6727 if (p->p_type == PT_NOTE 6728 && (hdr->sh_flags & SHF_ALLOC) != 0) 6729 p->p_memsz = p->p_filesz; 6730 break; 6731 } 6732 } 6733 } 6734 } 6735 } 6736 6737 return true; 6738 } 6739 6740 static elf_section_list * 6741 find_section_in_list (unsigned int i, elf_section_list * list) 6742 { 6743 for (;list != NULL; list = list->next) 6744 if (list->ndx == i) 6745 break; 6746 return list; 6747 } 6748 6749 /* Work out the file positions of all the sections. This is called by 6750 _bfd_elf_compute_section_file_positions. All the section sizes and 6751 VMAs must be known before this is called. 6752 6753 Reloc sections come in two flavours: Those processed specially as 6754 "side-channel" data attached to a section to which they apply, and 6755 those that bfd doesn't process as relocations. The latter sort are 6756 stored in a normal bfd section by bfd_section_from_shdr. We don't 6757 consider the former sort here, unless they form part of the loadable 6758 image. Reloc sections not assigned here (and compressed debugging 6759 sections and CTF sections which nothing else in the file can rely 6760 upon) will be handled later by assign_file_positions_for_relocs. 6761 6762 We also don't set the positions of the .symtab and .strtab here. */ 6763 6764 static bool 6765 assign_file_positions_except_relocs (bfd *abfd, 6766 struct bfd_link_info *link_info) 6767 { 6768 struct elf_obj_tdata *tdata = elf_tdata (abfd); 6769 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); 6770 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6771 unsigned int alloc; 6772 6773 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 6774 && bfd_get_format (abfd) != bfd_core) 6775 { 6776 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); 6777 unsigned int num_sec = elf_numsections (abfd); 6778 Elf_Internal_Shdr **hdrpp; 6779 unsigned int i; 6780 file_ptr off; 6781 6782 /* Start after the ELF header. */ 6783 off = i_ehdrp->e_ehsize; 6784 6785 /* We are not creating an executable, which means that we are 6786 not creating a program header, and that the actual order of 6787 the sections in the file is unimportant. */ 6788 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) 6789 { 6790 Elf_Internal_Shdr *hdr; 6791 6792 hdr = *hdrpp; 6793 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 6794 && hdr->bfd_section == NULL) 6795 /* Do not assign offsets for these sections yet: we don't know 6796 their sizes. */ 6797 || (abfd->is_linker_output 6798 && hdr->bfd_section != NULL 6799 && (hdr->sh_name == -1u 6800 || bfd_section_is_ctf (hdr->bfd_section))) 6801 || i == elf_onesymtab (abfd) 6802 || (elf_symtab_shndx_list (abfd) != NULL 6803 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) 6804 || i == elf_strtab_sec (abfd) 6805 || i == elf_shstrtab_sec (abfd)) 6806 { 6807 hdr->sh_offset = -1; 6808 } 6809 else 6810 off = _bfd_elf_assign_file_position_for_section (hdr, off, false, 6811 0); 6812 } 6813 6814 elf_next_file_pos (abfd) = off; 6815 elf_program_header_size (abfd) = 0; 6816 } 6817 else 6818 { 6819 /* Assign file positions for the loaded sections based on the 6820 assignment of sections to segments. */ 6821 if (!assign_file_positions_for_load_sections (abfd, link_info)) 6822 return false; 6823 6824 /* And for non-load sections. */ 6825 if (!assign_file_positions_for_non_load_sections (abfd, link_info)) 6826 return false; 6827 } 6828 6829 if (!(*bed->elf_backend_modify_headers) (abfd, link_info)) 6830 return false; 6831 6832 /* Write out the program headers. */ 6833 alloc = i_ehdrp->e_phnum; 6834 if (alloc != 0) 6835 { 6836 if (link_info != NULL && ! link_info->no_warn_rwx_segments) 6837 { 6838 bool warned_tls = false; 6839 bool warned_rwx = false; 6840 6841 /* Memory resident segments with non-zero size and RWX 6842 permissions are a security risk, so we generate a warning 6843 here if we are creating any. */ 6844 unsigned int i; 6845 6846 for (i = 0; i < alloc; i++) 6847 { 6848 const Elf_Internal_Phdr * phdr = tdata->phdr + i; 6849 6850 if (phdr->p_memsz == 0) 6851 continue; 6852 6853 if (! warned_tls 6854 && phdr->p_type == PT_TLS 6855 && (phdr->p_flags & PF_X)) 6856 { 6857 if (link_info->warn_is_error_for_rwx_segments) 6858 { 6859 _bfd_error_handler (_("\ 6860 error: %pB has a TLS segment with execute permission"), 6861 abfd); 6862 return false; 6863 } 6864 6865 _bfd_error_handler (_("\ 6866 warning: %pB has a TLS segment with execute permission"), 6867 abfd); 6868 if (warned_rwx) 6869 break; 6870 6871 warned_tls = true; 6872 } 6873 else if (! warned_rwx 6874 && phdr->p_type == PT_LOAD 6875 && ((phdr->p_flags & (PF_R | PF_W | PF_X)) 6876 == (PF_R | PF_W | PF_X))) 6877 { 6878 if (link_info->warn_is_error_for_rwx_segments) 6879 { 6880 _bfd_error_handler (_("\ 6881 error: %pB has a LOAD segment with RWX permissions"), 6882 abfd); 6883 return false; 6884 } 6885 6886 _bfd_error_handler (_("\ 6887 warning: %pB has a LOAD segment with RWX permissions"), 6888 abfd); 6889 if (warned_tls) 6890 break; 6891 6892 warned_rwx = true; 6893 } 6894 } 6895 } 6896 6897 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0 6898 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) 6899 return false; 6900 } 6901 6902 return true; 6903 } 6904 6905 bool 6906 _bfd_elf_init_file_header (bfd *abfd, 6907 struct bfd_link_info *info ATTRIBUTE_UNUSED) 6908 { 6909 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ 6910 struct elf_strtab_hash *shstrtab; 6911 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6912 6913 i_ehdrp = elf_elfheader (abfd); 6914 6915 shstrtab = _bfd_elf_strtab_init (); 6916 if (shstrtab == NULL) 6917 return false; 6918 6919 elf_shstrtab (abfd) = shstrtab; 6920 6921 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; 6922 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; 6923 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; 6924 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; 6925 6926 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; 6927 i_ehdrp->e_ident[EI_DATA] = 6928 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; 6929 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; 6930 6931 if ((abfd->flags & DYNAMIC) != 0) 6932 i_ehdrp->e_type = ET_DYN; 6933 else if ((abfd->flags & EXEC_P) != 0) 6934 i_ehdrp->e_type = ET_EXEC; 6935 else if (bfd_get_format (abfd) == bfd_core) 6936 i_ehdrp->e_type = ET_CORE; 6937 else 6938 i_ehdrp->e_type = ET_REL; 6939 6940 switch (bfd_get_arch (abfd)) 6941 { 6942 case bfd_arch_unknown: 6943 i_ehdrp->e_machine = EM_NONE; 6944 break; 6945 6946 /* There used to be a long list of cases here, each one setting 6947 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE 6948 in the corresponding bfd definition. To avoid duplication, 6949 the switch was removed. Machines that need special handling 6950 can generally do it in elf_backend_final_write_processing(), 6951 unless they need the information earlier than the final write. 6952 Such need can generally be supplied by replacing the tests for 6953 e_machine with the conditions used to determine it. */ 6954 default: 6955 i_ehdrp->e_machine = bed->elf_machine_code; 6956 } 6957 6958 i_ehdrp->e_version = bed->s->ev_current; 6959 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; 6960 6961 /* No program header, for now. */ 6962 i_ehdrp->e_phoff = 0; 6963 i_ehdrp->e_phentsize = 0; 6964 i_ehdrp->e_phnum = 0; 6965 6966 /* Each bfd section is section header entry. */ 6967 i_ehdrp->e_entry = bfd_get_start_address (abfd); 6968 i_ehdrp->e_shentsize = bed->s->sizeof_shdr; 6969 6970 elf_tdata (abfd)->symtab_hdr.sh_name = 6971 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false); 6972 elf_tdata (abfd)->strtab_hdr.sh_name = 6973 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false); 6974 elf_tdata (abfd)->shstrtab_hdr.sh_name = 6975 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false); 6976 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 6977 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 6978 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) 6979 return false; 6980 6981 return true; 6982 } 6983 6984 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. 6985 6986 FIXME: We used to have code here to sort the PT_LOAD segments into 6987 ascending order, as per the ELF spec. But this breaks some programs, 6988 including the Linux kernel. But really either the spec should be 6989 changed or the programs updated. */ 6990 6991 bool 6992 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info) 6993 { 6994 if (link_info != NULL && bfd_link_pie (link_info)) 6995 { 6996 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd); 6997 unsigned int num_segments = i_ehdrp->e_phnum; 6998 struct elf_obj_tdata *tdata = elf_tdata (obfd); 6999 Elf_Internal_Phdr *segment = tdata->phdr; 7000 Elf_Internal_Phdr *end_segment = &segment[num_segments]; 7001 7002 /* Find the lowest p_vaddr in PT_LOAD segments. */ 7003 bfd_vma p_vaddr = (bfd_vma) -1; 7004 for (; segment < end_segment; segment++) 7005 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) 7006 p_vaddr = segment->p_vaddr; 7007 7008 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD 7009 segments is non-zero. */ 7010 if (p_vaddr) 7011 i_ehdrp->e_type = ET_EXEC; 7012 } 7013 return true; 7014 } 7015 7016 /* Assign file positions for all the reloc sections which are not part 7017 of the loadable file image, and the file position of section headers. */ 7018 7019 static bool 7020 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) 7021 { 7022 file_ptr off; 7023 Elf_Internal_Shdr **shdrpp, **end_shdrpp; 7024 Elf_Internal_Shdr *shdrp; 7025 Elf_Internal_Ehdr *i_ehdrp; 7026 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 7027 7028 /* Skip non-load sections without section header. */ 7029 if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0) 7030 return true; 7031 7032 off = elf_next_file_pos (abfd); 7033 7034 shdrpp = elf_elfsections (abfd); 7035 end_shdrpp = shdrpp + elf_numsections (abfd); 7036 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) 7037 { 7038 shdrp = *shdrpp; 7039 if (shdrp->sh_offset == -1) 7040 { 7041 asection *sec = shdrp->bfd_section; 7042 if (sec == NULL 7043 || shdrp->sh_type == SHT_REL 7044 || shdrp->sh_type == SHT_RELA) 7045 ; 7046 else if (bfd_section_is_ctf (sec)) 7047 { 7048 /* Update section size and contents. */ 7049 shdrp->sh_size = sec->size; 7050 shdrp->contents = sec->contents; 7051 } 7052 else if (shdrp->sh_name == -1u) 7053 { 7054 const char *name = sec->name; 7055 struct bfd_elf_section_data *d; 7056 7057 /* Compress DWARF debug sections. */ 7058 if (!bfd_compress_section (abfd, sec, shdrp->contents)) 7059 return false; 7060 7061 if (sec->compress_status == COMPRESS_SECTION_DONE 7062 && (abfd->flags & BFD_COMPRESS_GABI) == 0 7063 && name[1] == 'd') 7064 { 7065 /* If section is compressed with zlib-gnu, convert 7066 section name from .debug_* to .zdebug_*. */ 7067 char *new_name = bfd_debug_name_to_zdebug (abfd, name); 7068 if (new_name == NULL) 7069 return false; 7070 name = new_name; 7071 } 7072 /* Add section name to section name section. */ 7073 shdrp->sh_name 7074 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 7075 name, false); 7076 d = elf_section_data (sec); 7077 7078 /* Add reloc section name to section name section. */ 7079 if (d->rel.hdr 7080 && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr, 7081 name, false)) 7082 return false; 7083 if (d->rela.hdr 7084 && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr, 7085 name, true)) 7086 return false; 7087 7088 /* Update section size and contents. */ 7089 shdrp->sh_size = sec->size; 7090 shdrp->contents = sec->contents; 7091 sec->contents = NULL; 7092 } 7093 7094 off = _bfd_elf_assign_file_position_for_section (shdrp, off, 7095 (abfd->flags & (EXEC_P | DYNAMIC)) 7096 || bfd_get_format (abfd) == bfd_core, 7097 bed->s->log_file_align); 7098 } 7099 } 7100 7101 /* Place section name section after DWARF debug sections have been 7102 compressed. */ 7103 _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); 7104 shdrp = &elf_tdata (abfd)->shstrtab_hdr; 7105 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); 7106 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true, 0); 7107 7108 /* Place the section headers. */ 7109 i_ehdrp = elf_elfheader (abfd); 7110 off = BFD_ALIGN (off, 1u << bed->s->log_file_align); 7111 i_ehdrp->e_shoff = off; 7112 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; 7113 elf_next_file_pos (abfd) = off; 7114 7115 return true; 7116 } 7117 7118 bool 7119 _bfd_elf_write_object_contents (bfd *abfd) 7120 { 7121 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 7122 Elf_Internal_Shdr **i_shdrp; 7123 bool failed; 7124 unsigned int count, num_sec; 7125 struct elf_obj_tdata *t; 7126 7127 if (! abfd->output_has_begun 7128 && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) 7129 return false; 7130 /* Do not rewrite ELF data when the BFD has been opened for update. 7131 abfd->output_has_begun was set to TRUE on opening, so creation of 7132 new sections, and modification of existing section sizes was 7133 restricted. This means the ELF header, program headers and 7134 section headers can't have changed. If the contents of any 7135 sections has been modified, then those changes have already been 7136 written to the BFD. */ 7137 else if (abfd->direction == both_direction) 7138 { 7139 BFD_ASSERT (abfd->output_has_begun); 7140 return true; 7141 } 7142 7143 i_shdrp = elf_elfsections (abfd); 7144 7145 failed = false; 7146 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); 7147 if (failed) 7148 return false; 7149 7150 if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) 7151 return false; 7152 7153 /* After writing the headers, we need to write the sections too... */ 7154 num_sec = elf_numsections (abfd); 7155 for (count = 1; count < num_sec; count++) 7156 { 7157 /* Don't set the sh_name field without section header. */ 7158 if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0) 7159 i_shdrp[count]->sh_name 7160 = _bfd_elf_strtab_offset (elf_shstrtab (abfd), 7161 i_shdrp[count]->sh_name); 7162 if (bed->elf_backend_section_processing) 7163 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count])) 7164 return false; 7165 if (i_shdrp[count]->contents) 7166 { 7167 bfd_size_type amt = i_shdrp[count]->sh_size; 7168 7169 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 7170 || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt) 7171 return false; 7172 } 7173 } 7174 7175 /* Write out the section header names. */ 7176 t = elf_tdata (abfd); 7177 if (elf_shstrtab (abfd) != NULL 7178 && t->shstrtab_hdr.sh_offset != -1 7179 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 7180 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) 7181 return false; 7182 7183 if (!(*bed->elf_backend_final_write_processing) (abfd)) 7184 return false; 7185 7186 if (!bed->s->write_shdrs_and_ehdr (abfd)) 7187 return false; 7188 7189 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ 7190 if (t->o->build_id.after_write_object_contents != NULL 7191 && !(*t->o->build_id.after_write_object_contents) (abfd)) 7192 return false; 7193 if (t->o->package_metadata.after_write_object_contents != NULL 7194 && !(*t->o->package_metadata.after_write_object_contents) (abfd)) 7195 return false; 7196 7197 return true; 7198 } 7199 7200 bool 7201 _bfd_elf_write_corefile_contents (bfd *abfd) 7202 { 7203 /* Hopefully this can be done just like an object file. */ 7204 return _bfd_elf_write_object_contents (abfd); 7205 } 7206 7207 /* Given a section, search the header to find them. */ 7208 7209 unsigned int 7210 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) 7211 { 7212 const struct elf_backend_data *bed; 7213 unsigned int sec_index; 7214 7215 if (elf_section_data (asect) != NULL 7216 && elf_section_data (asect)->this_idx != 0) 7217 return elf_section_data (asect)->this_idx; 7218 7219 if (bfd_is_abs_section (asect)) 7220 sec_index = SHN_ABS; 7221 else if (bfd_is_com_section (asect)) 7222 sec_index = SHN_COMMON; 7223 else if (bfd_is_und_section (asect)) 7224 sec_index = SHN_UNDEF; 7225 else 7226 sec_index = SHN_BAD; 7227 7228 bed = get_elf_backend_data (abfd); 7229 if (bed->elf_backend_section_from_bfd_section) 7230 { 7231 int retval = sec_index; 7232 7233 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) 7234 return retval; 7235 } 7236 7237 if (sec_index == SHN_BAD) 7238 bfd_set_error (bfd_error_nonrepresentable_section); 7239 7240 return sec_index; 7241 } 7242 7243 /* Given a BFD symbol, return the index in the ELF symbol table, or -1 7244 on error. */ 7245 7246 int 7247 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) 7248 { 7249 asymbol *asym_ptr = *asym_ptr_ptr; 7250 int idx; 7251 flagword flags = asym_ptr->flags; 7252 7253 /* When gas creates relocations against local labels, it creates its 7254 own symbol for the section, but does put the symbol into the 7255 symbol chain, so udata is 0. When the linker is generating 7256 relocatable output, this section symbol may be for one of the 7257 input sections rather than the output section. */ 7258 if (asym_ptr->udata.i == 0 7259 && (flags & BSF_SECTION_SYM) 7260 && asym_ptr->section) 7261 { 7262 asection *sec; 7263 7264 sec = asym_ptr->section; 7265 if (sec->owner != abfd && sec->output_section != NULL) 7266 sec = sec->output_section; 7267 if (sec->owner == abfd 7268 && sec->index < elf_num_section_syms (abfd) 7269 && elf_section_syms (abfd)[sec->index] != NULL) 7270 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i; 7271 } 7272 7273 idx = asym_ptr->udata.i; 7274 7275 if (idx == 0) 7276 { 7277 /* This case can occur when using --strip-symbol on a symbol 7278 which is used in a relocation entry. */ 7279 _bfd_error_handler 7280 /* xgettext:c-format */ 7281 (_("%pB: symbol `%s' required but not present"), 7282 abfd, bfd_asymbol_name (asym_ptr)); 7283 bfd_set_error (bfd_error_no_symbols); 7284 return -1; 7285 } 7286 7287 #if DEBUG & 4 7288 { 7289 fprintf (stderr, 7290 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d," 7291 " flags = 0x%.8x\n", 7292 (long) asym_ptr, asym_ptr->name, idx, flags); 7293 fflush (stderr); 7294 } 7295 #endif 7296 7297 return idx; 7298 } 7299 7300 static inline bfd_vma 7301 segment_size (Elf_Internal_Phdr *segment) 7302 { 7303 return (segment->p_memsz > segment->p_filesz 7304 ? segment->p_memsz : segment->p_filesz); 7305 } 7306 7307 7308 /* Returns the end address of the segment + 1. */ 7309 static inline bfd_vma 7310 segment_end (Elf_Internal_Phdr *segment, bfd_vma start) 7311 { 7312 return start + segment_size (segment); 7313 } 7314 7315 static inline bfd_size_type 7316 section_size (asection *section, Elf_Internal_Phdr *segment) 7317 { 7318 if ((section->flags & SEC_HAS_CONTENTS) != 0 7319 || (section->flags & SEC_THREAD_LOCAL) == 0 7320 || segment->p_type == PT_TLS) 7321 return section->size; 7322 return 0; 7323 } 7324 7325 /* Returns TRUE if the given section is contained within the given 7326 segment. LMA addresses are compared against PADDR when 7327 USE_VADDR is false, VMA against VADDR when true. */ 7328 static bool 7329 is_contained_by (asection *section, Elf_Internal_Phdr *segment, 7330 bfd_vma paddr, bfd_vma vaddr, unsigned int opb, 7331 bool use_vaddr) 7332 { 7333 bfd_vma seg_addr = !use_vaddr ? paddr : vaddr; 7334 bfd_vma addr = !use_vaddr ? section->lma : section->vma; 7335 bfd_vma octet; 7336 if (_bfd_mul_overflow (addr, opb, &octet)) 7337 return false; 7338 /* The third and fourth lines below are testing that the section end 7339 address is within the segment. It's written this way to avoid 7340 overflow. Add seg_addr + section_size to both sides of the 7341 inequality to make it obvious. */ 7342 return (octet >= seg_addr 7343 && segment_size (segment) >= section_size (section, segment) 7344 && (octet - seg_addr 7345 <= segment_size (segment) - section_size (section, segment))); 7346 } 7347 7348 /* Handle PT_NOTE segment. */ 7349 static bool 7350 is_note (asection *s, Elf_Internal_Phdr *p) 7351 { 7352 return (p->p_type == PT_NOTE 7353 && elf_section_type (s) == SHT_NOTE 7354 && (ufile_ptr) s->filepos >= p->p_offset 7355 && p->p_filesz >= s->size 7356 && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size); 7357 } 7358 7359 /* Rewrite program header information. */ 7360 7361 static bool 7362 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize) 7363 { 7364 Elf_Internal_Ehdr *iehdr; 7365 struct elf_segment_map *map; 7366 struct elf_segment_map *map_first; 7367 struct elf_segment_map **pointer_to_map; 7368 Elf_Internal_Phdr *segment; 7369 asection *section; 7370 unsigned int i; 7371 unsigned int num_segments; 7372 bool phdr_included = false; 7373 bool p_paddr_valid; 7374 struct elf_segment_map *phdr_adjust_seg = NULL; 7375 unsigned int phdr_adjust_num = 0; 7376 const struct elf_backend_data *bed; 7377 unsigned int opb = bfd_octets_per_byte (ibfd, NULL); 7378 7379 bed = get_elf_backend_data (ibfd); 7380 iehdr = elf_elfheader (ibfd); 7381 7382 map_first = NULL; 7383 pointer_to_map = &map_first; 7384 7385 num_segments = elf_elfheader (ibfd)->e_phnum; 7386 7387 /* The complicated case when p_vaddr is 0 is to handle the Solaris 7388 linker, which generates a PT_INTERP section with p_vaddr and 7389 p_memsz set to 0. */ 7390 #define IS_SOLARIS_PT_INTERP(p, s) \ 7391 (p->p_vaddr == 0 \ 7392 && p->p_paddr == 0 \ 7393 && p->p_memsz == 0 \ 7394 && p->p_filesz > 0 \ 7395 && (s->flags & SEC_HAS_CONTENTS) != 0 \ 7396 && s->size > 0 \ 7397 && (bfd_vma) s->filepos >= p->p_offset \ 7398 && ((bfd_vma) s->filepos + s->size \ 7399 <= p->p_offset + p->p_filesz)) 7400 7401 /* Decide if the given section should be included in the given segment. 7402 A section will be included if: 7403 1. It is within the address space of the segment -- we use the LMA 7404 if that is set for the segment and the VMA otherwise, 7405 2. It is an allocated section or a NOTE section in a PT_NOTE 7406 segment. 7407 3. There is an output section associated with it, 7408 4. The section has not already been allocated to a previous segment. 7409 5. PT_GNU_STACK segments do not include any sections. 7410 6. PT_TLS segment includes only SHF_TLS sections. 7411 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. 7412 8. PT_DYNAMIC should not contain empty sections at the beginning 7413 (with the possible exception of .dynamic). */ 7414 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \ 7415 (((is_contained_by (section, segment, segment->p_paddr, \ 7416 segment->p_vaddr, opb, !paddr_valid) \ 7417 && (section->flags & SEC_ALLOC) != 0) \ 7418 || is_note (section, segment)) \ 7419 && segment->p_type != PT_GNU_STACK \ 7420 && (segment->p_type != PT_TLS \ 7421 || (section->flags & SEC_THREAD_LOCAL)) \ 7422 && (segment->p_type == PT_LOAD \ 7423 || segment->p_type == PT_TLS \ 7424 || (section->flags & SEC_THREAD_LOCAL) == 0) \ 7425 && (segment->p_type != PT_DYNAMIC \ 7426 || section_size (section, segment) > 0 \ 7427 || (segment->p_paddr \ 7428 ? segment->p_paddr != section->lma * (opb) \ 7429 : segment->p_vaddr != section->vma * (opb)) \ 7430 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \ 7431 && (segment->p_type != PT_LOAD || !section->segment_mark)) 7432 7433 /* If the output section of a section in the input segment is NULL, 7434 it is removed from the corresponding output segment. */ 7435 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid) \ 7436 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \ 7437 && section->output_section != NULL) 7438 7439 /* Returns TRUE iff seg1 starts after the end of seg2. */ 7440 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ 7441 (seg1->field >= segment_end (seg2, seg2->field)) 7442 7443 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both 7444 their VMA address ranges and their LMA address ranges overlap. 7445 It is possible to have overlapping VMA ranges without overlapping LMA 7446 ranges. RedBoot images for example can have both .data and .bss mapped 7447 to the same VMA range, but with the .data section mapped to a different 7448 LMA. */ 7449 #define SEGMENT_OVERLAPS(seg1, seg2) \ 7450 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ 7451 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ 7452 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ 7453 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) 7454 7455 /* Initialise the segment mark field, and discard stupid alignment. */ 7456 for (section = ibfd->sections; section != NULL; section = section->next) 7457 { 7458 asection *o = section->output_section; 7459 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1) 7460 o->alignment_power = 0; 7461 section->segment_mark = false; 7462 } 7463 7464 /* The Solaris linker creates program headers in which all the 7465 p_paddr fields are zero. When we try to objcopy or strip such a 7466 file, we get confused. Check for this case, and if we find it 7467 don't set the p_paddr_valid fields. */ 7468 p_paddr_valid = false; 7469 for (i = 0, segment = elf_tdata (ibfd)->phdr; 7470 i < num_segments; 7471 i++, segment++) 7472 if (segment->p_paddr != 0) 7473 { 7474 p_paddr_valid = true; 7475 break; 7476 } 7477 7478 /* Scan through the segments specified in the program header 7479 of the input BFD. For this first scan we look for overlaps 7480 in the loadable segments. These can be created by weird 7481 parameters to objcopy. Also, fix some solaris weirdness. */ 7482 for (i = 0, segment = elf_tdata (ibfd)->phdr; 7483 i < num_segments; 7484 i++, segment++) 7485 { 7486 unsigned int j; 7487 Elf_Internal_Phdr *segment2; 7488 7489 if (segment->p_type == PT_INTERP) 7490 for (section = ibfd->sections; section; section = section->next) 7491 if (IS_SOLARIS_PT_INTERP (segment, section)) 7492 { 7493 /* Mininal change so that the normal section to segment 7494 assignment code will work. */ 7495 segment->p_vaddr = section->vma * opb; 7496 break; 7497 } 7498 7499 if (segment->p_type != PT_LOAD) 7500 { 7501 /* Remove PT_GNU_RELRO segment. */ 7502 if (segment->p_type == PT_GNU_RELRO) 7503 segment->p_type = PT_NULL; 7504 continue; 7505 } 7506 7507 /* Determine if this segment overlaps any previous segments. */ 7508 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) 7509 { 7510 bfd_signed_vma extra_length; 7511 7512 if (segment2->p_type != PT_LOAD 7513 || !SEGMENT_OVERLAPS (segment, segment2)) 7514 continue; 7515 7516 /* Merge the two segments together. */ 7517 if (segment2->p_vaddr < segment->p_vaddr) 7518 { 7519 /* Extend SEGMENT2 to include SEGMENT and then delete 7520 SEGMENT. */ 7521 extra_length = (segment_end (segment, segment->p_vaddr) 7522 - segment_end (segment2, segment2->p_vaddr)); 7523 7524 if (extra_length > 0) 7525 { 7526 segment2->p_memsz += extra_length; 7527 segment2->p_filesz += extra_length; 7528 } 7529 7530 segment->p_type = PT_NULL; 7531 7532 /* Since we have deleted P we must restart the outer loop. */ 7533 i = 0; 7534 segment = elf_tdata (ibfd)->phdr; 7535 break; 7536 } 7537 else 7538 { 7539 /* Extend SEGMENT to include SEGMENT2 and then delete 7540 SEGMENT2. */ 7541 extra_length = (segment_end (segment2, segment2->p_vaddr) 7542 - segment_end (segment, segment->p_vaddr)); 7543 7544 if (extra_length > 0) 7545 { 7546 segment->p_memsz += extra_length; 7547 segment->p_filesz += extra_length; 7548 } 7549 7550 segment2->p_type = PT_NULL; 7551 } 7552 } 7553 } 7554 7555 /* The second scan attempts to assign sections to segments. */ 7556 for (i = 0, segment = elf_tdata (ibfd)->phdr; 7557 i < num_segments; 7558 i++, segment++) 7559 { 7560 unsigned int section_count; 7561 asection **sections; 7562 asection *output_section; 7563 unsigned int isec; 7564 asection *matching_lma; 7565 asection *suggested_lma; 7566 unsigned int j; 7567 size_t amt; 7568 asection *first_section; 7569 7570 if (segment->p_type == PT_NULL) 7571 continue; 7572 7573 first_section = NULL; 7574 /* Compute how many sections might be placed into this segment. */ 7575 for (section = ibfd->sections, section_count = 0; 7576 section != NULL; 7577 section = section->next) 7578 { 7579 /* Find the first section in the input segment, which may be 7580 removed from the corresponding output segment. */ 7581 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid)) 7582 { 7583 if (first_section == NULL) 7584 first_section = section; 7585 if (section->output_section != NULL) 7586 ++section_count; 7587 } 7588 } 7589 7590 /* Allocate a segment map big enough to contain 7591 all of the sections we have selected. */ 7592 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 7593 amt += section_count * sizeof (asection *); 7594 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 7595 if (map == NULL) 7596 return false; 7597 7598 /* Initialise the fields of the segment map. Default to 7599 using the physical address of the segment in the input BFD. */ 7600 map->next = NULL; 7601 map->p_type = segment->p_type; 7602 map->p_flags = segment->p_flags; 7603 map->p_flags_valid = 1; 7604 7605 if (map->p_type == PT_LOAD 7606 && (ibfd->flags & D_PAGED) != 0 7607 && maxpagesize > 1 7608 && segment->p_align > 1) 7609 { 7610 map->p_align = segment->p_align; 7611 if (segment->p_align > maxpagesize) 7612 map->p_align = maxpagesize; 7613 map->p_align_valid = 1; 7614 } 7615 7616 /* If the first section in the input segment is removed, there is 7617 no need to preserve segment physical address in the corresponding 7618 output segment. */ 7619 if (!first_section || first_section->output_section != NULL) 7620 { 7621 map->p_paddr = segment->p_paddr; 7622 map->p_paddr_valid = p_paddr_valid; 7623 } 7624 7625 /* Determine if this segment contains the ELF file header 7626 and if it contains the program headers themselves. */ 7627 map->includes_filehdr = (segment->p_offset == 0 7628 && segment->p_filesz >= iehdr->e_ehsize); 7629 map->includes_phdrs = 0; 7630 7631 if (!phdr_included || segment->p_type != PT_LOAD) 7632 { 7633 map->includes_phdrs = 7634 (segment->p_offset <= (bfd_vma) iehdr->e_phoff 7635 && (segment->p_offset + segment->p_filesz 7636 >= ((bfd_vma) iehdr->e_phoff 7637 + iehdr->e_phnum * iehdr->e_phentsize))); 7638 7639 if (segment->p_type == PT_LOAD && map->includes_phdrs) 7640 phdr_included = true; 7641 } 7642 7643 if (section_count == 0) 7644 { 7645 /* Special segments, such as the PT_PHDR segment, may contain 7646 no sections, but ordinary, loadable segments should contain 7647 something. They are allowed by the ELF spec however, so only 7648 a warning is produced. 7649 Don't warn if an empty PT_LOAD contains the program headers. 7650 There is however the valid use case of embedded systems which 7651 have segments with p_filesz of 0 and a p_memsz > 0 to initialize 7652 flash memory with zeros. No warning is shown for that case. */ 7653 if (segment->p_type == PT_LOAD 7654 && !map->includes_phdrs 7655 && (segment->p_filesz > 0 || segment->p_memsz == 0)) 7656 /* xgettext:c-format */ 7657 _bfd_error_handler 7658 (_("%pB: warning: empty loadable segment detected" 7659 " at vaddr=%#" PRIx64 ", is this intentional?"), 7660 ibfd, (uint64_t) segment->p_vaddr); 7661 7662 map->p_vaddr_offset = segment->p_vaddr / opb; 7663 map->count = 0; 7664 *pointer_to_map = map; 7665 pointer_to_map = &map->next; 7666 7667 continue; 7668 } 7669 7670 /* Now scan the sections in the input BFD again and attempt 7671 to add their corresponding output sections to the segment map. 7672 The problem here is how to handle an output section which has 7673 been moved (ie had its LMA changed). There are four possibilities: 7674 7675 1. None of the sections have been moved. 7676 In this case we can continue to use the segment LMA from the 7677 input BFD. 7678 7679 2. All of the sections have been moved by the same amount. 7680 In this case we can change the segment's LMA to match the LMA 7681 of the first section. 7682 7683 3. Some of the sections have been moved, others have not. 7684 In this case those sections which have not been moved can be 7685 placed in the current segment which will have to have its size, 7686 and possibly its LMA changed, and a new segment or segments will 7687 have to be created to contain the other sections. 7688 7689 4. The sections have been moved, but not by the same amount. 7690 In this case we can change the segment's LMA to match the LMA 7691 of the first section and we will have to create a new segment 7692 or segments to contain the other sections. 7693 7694 In order to save time, we allocate an array to hold the section 7695 pointers that we are interested in. As these sections get assigned 7696 to a segment, they are removed from this array. */ 7697 7698 amt = section_count * sizeof (asection *); 7699 sections = (asection **) bfd_malloc (amt); 7700 if (sections == NULL) 7701 return false; 7702 7703 /* Step One: Scan for segment vs section LMA conflicts. 7704 Also add the sections to the section array allocated above. 7705 Also add the sections to the current segment. In the common 7706 case, where the sections have not been moved, this means that 7707 we have completely filled the segment, and there is nothing 7708 more to do. */ 7709 isec = 0; 7710 matching_lma = NULL; 7711 suggested_lma = NULL; 7712 7713 for (section = first_section, j = 0; 7714 section != NULL; 7715 section = section->next) 7716 { 7717 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid)) 7718 { 7719 output_section = section->output_section; 7720 7721 sections[j++] = section; 7722 7723 /* The Solaris native linker always sets p_paddr to 0. 7724 We try to catch that case here, and set it to the 7725 correct value. Note - some backends require that 7726 p_paddr be left as zero. */ 7727 if (!p_paddr_valid 7728 && segment->p_vaddr != 0 7729 && !bed->want_p_paddr_set_to_zero 7730 && isec == 0 7731 && output_section->lma != 0 7732 && (align_power (segment->p_vaddr 7733 + (map->includes_filehdr 7734 ? iehdr->e_ehsize : 0) 7735 + (map->includes_phdrs 7736 ? iehdr->e_phnum * iehdr->e_phentsize 7737 : 0), 7738 output_section->alignment_power * opb) 7739 == (output_section->vma * opb))) 7740 map->p_paddr = segment->p_vaddr; 7741 7742 /* Match up the physical address of the segment with the 7743 LMA address of the output section. */ 7744 if (is_contained_by (output_section, segment, map->p_paddr, 7745 0, opb, false) 7746 || is_note (section, segment)) 7747 { 7748 if (matching_lma == NULL 7749 || output_section->lma < matching_lma->lma) 7750 matching_lma = output_section; 7751 7752 /* We assume that if the section fits within the segment 7753 then it does not overlap any other section within that 7754 segment. */ 7755 map->sections[isec++] = output_section; 7756 } 7757 else if (suggested_lma == NULL) 7758 suggested_lma = output_section; 7759 7760 if (j == section_count) 7761 break; 7762 } 7763 } 7764 7765 BFD_ASSERT (j == section_count); 7766 7767 /* Step Two: Adjust the physical address of the current segment, 7768 if necessary. */ 7769 if (isec == section_count) 7770 { 7771 /* All of the sections fitted within the segment as currently 7772 specified. This is the default case. Add the segment to 7773 the list of built segments and carry on to process the next 7774 program header in the input BFD. */ 7775 map->count = section_count; 7776 *pointer_to_map = map; 7777 pointer_to_map = &map->next; 7778 7779 if (p_paddr_valid 7780 && !bed->want_p_paddr_set_to_zero) 7781 { 7782 bfd_vma hdr_size = 0; 7783 if (map->includes_filehdr) 7784 hdr_size = iehdr->e_ehsize; 7785 if (map->includes_phdrs) 7786 hdr_size += iehdr->e_phnum * iehdr->e_phentsize; 7787 7788 /* Account for padding before the first section in the 7789 segment. */ 7790 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb 7791 - matching_lma->lma); 7792 } 7793 7794 free (sections); 7795 continue; 7796 } 7797 else 7798 { 7799 /* Change the current segment's physical address to match 7800 the LMA of the first section that fitted, or if no 7801 section fitted, the first section. */ 7802 if (matching_lma == NULL) 7803 matching_lma = suggested_lma; 7804 7805 map->p_paddr = matching_lma->lma * opb; 7806 7807 /* Offset the segment physical address from the lma 7808 to allow for space taken up by elf headers. */ 7809 if (map->includes_phdrs) 7810 { 7811 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; 7812 7813 /* iehdr->e_phnum is just an estimate of the number 7814 of program headers that we will need. Make a note 7815 here of the number we used and the segment we chose 7816 to hold these headers, so that we can adjust the 7817 offset when we know the correct value. */ 7818 phdr_adjust_num = iehdr->e_phnum; 7819 phdr_adjust_seg = map; 7820 } 7821 7822 if (map->includes_filehdr) 7823 { 7824 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power; 7825 map->p_paddr -= iehdr->e_ehsize; 7826 /* We've subtracted off the size of headers from the 7827 first section lma, but there may have been some 7828 alignment padding before that section too. Try to 7829 account for that by adjusting the segment lma down to 7830 the same alignment. */ 7831 if (segment->p_align != 0 && segment->p_align < align) 7832 align = segment->p_align; 7833 map->p_paddr &= -(align * opb); 7834 } 7835 } 7836 7837 /* Step Three: Loop over the sections again, this time assigning 7838 those that fit to the current segment and removing them from the 7839 sections array; but making sure not to leave large gaps. Once all 7840 possible sections have been assigned to the current segment it is 7841 added to the list of built segments and if sections still remain 7842 to be assigned, a new segment is constructed before repeating 7843 the loop. */ 7844 isec = 0; 7845 do 7846 { 7847 map->count = 0; 7848 suggested_lma = NULL; 7849 7850 /* Fill the current segment with sections that fit. */ 7851 for (j = 0; j < section_count; j++) 7852 { 7853 section = sections[j]; 7854 7855 if (section == NULL) 7856 continue; 7857 7858 output_section = section->output_section; 7859 7860 BFD_ASSERT (output_section != NULL); 7861 7862 if (is_contained_by (output_section, segment, map->p_paddr, 7863 0, opb, false) 7864 || is_note (section, segment)) 7865 { 7866 if (map->count == 0) 7867 { 7868 /* If the first section in a segment does not start at 7869 the beginning of the segment, then something is 7870 wrong. */ 7871 if (align_power (map->p_paddr 7872 + (map->includes_filehdr 7873 ? iehdr->e_ehsize : 0) 7874 + (map->includes_phdrs 7875 ? iehdr->e_phnum * iehdr->e_phentsize 7876 : 0), 7877 output_section->alignment_power * opb) 7878 != output_section->lma * opb) 7879 goto sorry; 7880 } 7881 else 7882 { 7883 asection *prev_sec; 7884 7885 prev_sec = map->sections[map->count - 1]; 7886 7887 /* If the gap between the end of the previous section 7888 and the start of this section is more than 7889 maxpagesize then we need to start a new segment. */ 7890 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, 7891 maxpagesize) 7892 < BFD_ALIGN (output_section->lma, maxpagesize)) 7893 || (prev_sec->lma + prev_sec->size 7894 > output_section->lma)) 7895 { 7896 if (suggested_lma == NULL) 7897 suggested_lma = output_section; 7898 7899 continue; 7900 } 7901 } 7902 7903 map->sections[map->count++] = output_section; 7904 ++isec; 7905 sections[j] = NULL; 7906 if (segment->p_type == PT_LOAD) 7907 section->segment_mark = true; 7908 } 7909 else if (suggested_lma == NULL) 7910 suggested_lma = output_section; 7911 } 7912 7913 /* PR 23932. A corrupt input file may contain sections that cannot 7914 be assigned to any segment - because for example they have a 7915 negative size - or segments that do not contain any sections. 7916 But there are also valid reasons why a segment can be empty. 7917 So allow a count of zero. */ 7918 7919 /* Add the current segment to the list of built segments. */ 7920 *pointer_to_map = map; 7921 pointer_to_map = &map->next; 7922 7923 if (isec < section_count) 7924 { 7925 /* We still have not allocated all of the sections to 7926 segments. Create a new segment here, initialise it 7927 and carry on looping. */ 7928 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 7929 amt += section_count * sizeof (asection *); 7930 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 7931 if (map == NULL) 7932 { 7933 free (sections); 7934 return false; 7935 } 7936 7937 /* Initialise the fields of the segment map. Set the physical 7938 physical address to the LMA of the first section that has 7939 not yet been assigned. */ 7940 map->next = NULL; 7941 map->p_type = segment->p_type; 7942 map->p_flags = segment->p_flags; 7943 map->p_flags_valid = 1; 7944 map->p_paddr = suggested_lma->lma * opb; 7945 map->p_paddr_valid = p_paddr_valid; 7946 map->includes_filehdr = 0; 7947 map->includes_phdrs = 0; 7948 } 7949 7950 continue; 7951 sorry: 7952 bfd_set_error (bfd_error_sorry); 7953 free (sections); 7954 return false; 7955 } 7956 while (isec < section_count); 7957 7958 free (sections); 7959 } 7960 7961 elf_seg_map (obfd) = map_first; 7962 7963 /* If we had to estimate the number of program headers that were 7964 going to be needed, then check our estimate now and adjust 7965 the offset if necessary. */ 7966 if (phdr_adjust_seg != NULL) 7967 { 7968 unsigned int count; 7969 7970 for (count = 0, map = map_first; map != NULL; map = map->next) 7971 count++; 7972 7973 if (count > phdr_adjust_num) 7974 phdr_adjust_seg->p_paddr 7975 -= (count - phdr_adjust_num) * iehdr->e_phentsize; 7976 7977 for (map = map_first; map != NULL; map = map->next) 7978 if (map->p_type == PT_PHDR) 7979 { 7980 bfd_vma adjust 7981 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0; 7982 map->p_paddr = phdr_adjust_seg->p_paddr + adjust; 7983 break; 7984 } 7985 } 7986 7987 #undef IS_SOLARIS_PT_INTERP 7988 #undef IS_SECTION_IN_INPUT_SEGMENT 7989 #undef INCLUDE_SECTION_IN_SEGMENT 7990 #undef SEGMENT_AFTER_SEGMENT 7991 #undef SEGMENT_OVERLAPS 7992 return true; 7993 } 7994 7995 /* Return true if p_align in the ELF program header in ABFD is valid. */ 7996 7997 static bool 7998 elf_is_p_align_valid (bfd *abfd) 7999 { 8000 unsigned int i; 8001 Elf_Internal_Phdr *segment; 8002 unsigned int num_segments; 8003 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8004 bfd_size_type maxpagesize = bed->maxpagesize; 8005 bfd_size_type p_align = bed->p_align; 8006 8007 /* Return true if the default p_align value isn't set or the maximum 8008 page size is the same as the minimum page size. */ 8009 if (p_align == 0 || maxpagesize == bed->minpagesize) 8010 return true; 8011 8012 /* When the default p_align value is set, p_align may be set to the 8013 default p_align value while segments are aligned to the maximum 8014 page size. In this case, the input p_align will be ignored and 8015 the maximum page size will be used to align the output segments. */ 8016 segment = elf_tdata (abfd)->phdr; 8017 num_segments = elf_elfheader (abfd)->e_phnum; 8018 for (i = 0; i < num_segments; i++, segment++) 8019 if (segment->p_type == PT_LOAD 8020 && (segment->p_align != p_align 8021 || vma_page_aligned_bias (segment->p_vaddr, 8022 segment->p_offset, 8023 maxpagesize) != 0)) 8024 return true; 8025 8026 return false; 8027 } 8028 8029 /* Copy ELF program header information. */ 8030 8031 static bool 8032 copy_elf_program_header (bfd *ibfd, bfd *obfd) 8033 { 8034 Elf_Internal_Ehdr *iehdr; 8035 struct elf_segment_map *map; 8036 struct elf_segment_map *map_first; 8037 struct elf_segment_map **pointer_to_map; 8038 Elf_Internal_Phdr *segment; 8039 unsigned int i; 8040 unsigned int num_segments; 8041 bool phdr_included = false; 8042 bool p_paddr_valid; 8043 bool p_palign_valid; 8044 unsigned int opb = bfd_octets_per_byte (ibfd, NULL); 8045 8046 iehdr = elf_elfheader (ibfd); 8047 8048 map_first = NULL; 8049 pointer_to_map = &map_first; 8050 8051 /* If all the segment p_paddr fields are zero, don't set 8052 map->p_paddr_valid. */ 8053 p_paddr_valid = false; 8054 num_segments = elf_elfheader (ibfd)->e_phnum; 8055 for (i = 0, segment = elf_tdata (ibfd)->phdr; 8056 i < num_segments; 8057 i++, segment++) 8058 if (segment->p_paddr != 0) 8059 { 8060 p_paddr_valid = true; 8061 break; 8062 } 8063 8064 p_palign_valid = elf_is_p_align_valid (ibfd); 8065 8066 for (i = 0, segment = elf_tdata (ibfd)->phdr; 8067 i < num_segments; 8068 i++, segment++) 8069 { 8070 asection *section; 8071 unsigned int section_count; 8072 size_t amt; 8073 Elf_Internal_Shdr *this_hdr; 8074 asection *first_section = NULL; 8075 asection *lowest_section; 8076 8077 /* Compute how many sections are in this segment. */ 8078 for (section = ibfd->sections, section_count = 0; 8079 section != NULL; 8080 section = section->next) 8081 { 8082 this_hdr = &(elf_section_data(section)->this_hdr); 8083 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 8084 { 8085 if (first_section == NULL) 8086 first_section = section; 8087 section_count++; 8088 } 8089 } 8090 8091 /* Allocate a segment map big enough to contain 8092 all of the sections we have selected. */ 8093 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 8094 amt += section_count * sizeof (asection *); 8095 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 8096 if (map == NULL) 8097 return false; 8098 8099 /* Initialize the fields of the output segment map with the 8100 input segment. */ 8101 map->next = NULL; 8102 map->p_type = segment->p_type; 8103 map->p_flags = segment->p_flags; 8104 map->p_flags_valid = 1; 8105 map->p_paddr = segment->p_paddr; 8106 map->p_paddr_valid = p_paddr_valid; 8107 map->p_align = segment->p_align; 8108 /* Keep p_align of PT_GNU_STACK for stack alignment. */ 8109 map->p_align_valid = (map->p_type == PT_GNU_STACK 8110 || p_palign_valid); 8111 map->p_vaddr_offset = 0; 8112 8113 if (map->p_type == PT_GNU_RELRO 8114 || map->p_type == PT_GNU_STACK) 8115 { 8116 /* The PT_GNU_RELRO segment may contain the first a few 8117 bytes in the .got.plt section even if the whole .got.plt 8118 section isn't in the PT_GNU_RELRO segment. We won't 8119 change the size of the PT_GNU_RELRO segment. 8120 Similarly, PT_GNU_STACK size is significant on uclinux 8121 systems. */ 8122 map->p_size = segment->p_memsz; 8123 map->p_size_valid = 1; 8124 } 8125 8126 /* Determine if this segment contains the ELF file header 8127 and if it contains the program headers themselves. */ 8128 map->includes_filehdr = (segment->p_offset == 0 8129 && segment->p_filesz >= iehdr->e_ehsize); 8130 8131 map->includes_phdrs = 0; 8132 if (! phdr_included || segment->p_type != PT_LOAD) 8133 { 8134 map->includes_phdrs = 8135 (segment->p_offset <= (bfd_vma) iehdr->e_phoff 8136 && (segment->p_offset + segment->p_filesz 8137 >= ((bfd_vma) iehdr->e_phoff 8138 + iehdr->e_phnum * iehdr->e_phentsize))); 8139 8140 if (segment->p_type == PT_LOAD && map->includes_phdrs) 8141 phdr_included = true; 8142 } 8143 8144 lowest_section = NULL; 8145 if (section_count != 0) 8146 { 8147 unsigned int isec = 0; 8148 8149 for (section = first_section; 8150 section != NULL; 8151 section = section->next) 8152 { 8153 this_hdr = &(elf_section_data(section)->this_hdr); 8154 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 8155 { 8156 map->sections[isec++] = section->output_section; 8157 if ((section->flags & SEC_ALLOC) != 0) 8158 { 8159 bfd_vma seg_off; 8160 8161 if (lowest_section == NULL 8162 || section->lma < lowest_section->lma) 8163 lowest_section = section; 8164 8165 /* Section lmas are set up from PT_LOAD header 8166 p_paddr in _bfd_elf_make_section_from_shdr. 8167 If this header has a p_paddr that disagrees 8168 with the section lma, flag the p_paddr as 8169 invalid. */ 8170 if ((section->flags & SEC_LOAD) != 0) 8171 seg_off = this_hdr->sh_offset - segment->p_offset; 8172 else 8173 seg_off = this_hdr->sh_addr - segment->p_vaddr; 8174 if (section->lma * opb - segment->p_paddr != seg_off) 8175 map->p_paddr_valid = false; 8176 } 8177 if (isec == section_count) 8178 break; 8179 } 8180 } 8181 } 8182 8183 if (section_count == 0) 8184 map->p_vaddr_offset = segment->p_vaddr / opb; 8185 else if (map->p_paddr_valid) 8186 { 8187 /* Account for padding before the first section in the segment. */ 8188 bfd_vma hdr_size = 0; 8189 if (map->includes_filehdr) 8190 hdr_size = iehdr->e_ehsize; 8191 if (map->includes_phdrs) 8192 hdr_size += iehdr->e_phnum * iehdr->e_phentsize; 8193 8194 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb 8195 - (lowest_section ? lowest_section->lma : 0)); 8196 } 8197 8198 map->count = section_count; 8199 *pointer_to_map = map; 8200 pointer_to_map = &map->next; 8201 } 8202 8203 elf_seg_map (obfd) = map_first; 8204 return true; 8205 } 8206 8207 /* Copy private BFD data. This copies or rewrites ELF program header 8208 information. */ 8209 8210 static bool 8211 copy_private_bfd_data (bfd *ibfd, bfd *obfd) 8212 { 8213 bfd_vma maxpagesize; 8214 8215 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 8216 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 8217 return true; 8218 8219 if (elf_tdata (ibfd)->phdr == NULL) 8220 return true; 8221 8222 if (ibfd->xvec == obfd->xvec) 8223 { 8224 /* Check to see if any sections in the input BFD 8225 covered by ELF program header have changed. */ 8226 Elf_Internal_Phdr *segment; 8227 asection * section; 8228 asection * osec; 8229 asection * prev; 8230 unsigned int i, num_segments; 8231 Elf_Internal_Shdr *this_hdr; 8232 const struct elf_backend_data *bed; 8233 8234 bed = get_elf_backend_data (ibfd); 8235 8236 /* Regenerate the segment map if p_paddr is set to 0. */ 8237 if (bed->want_p_paddr_set_to_zero) 8238 goto rewrite; 8239 8240 /* Initialize the segment mark field. */ 8241 for (section = obfd->sections; section != NULL; 8242 section = section->next) 8243 section->segment_mark = false; 8244 8245 num_segments = elf_elfheader (ibfd)->e_phnum; 8246 for (i = 0, segment = elf_tdata (ibfd)->phdr; 8247 i < num_segments; 8248 i++, segment++) 8249 { 8250 /* PR binutils/3535. The Solaris linker always sets the p_paddr 8251 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 8252 which severly confuses things, so always regenerate the segment 8253 map in this case. */ 8254 if (segment->p_paddr == 0 8255 && segment->p_memsz == 0 8256 && (segment->p_type == PT_INTERP 8257 || segment->p_type == PT_DYNAMIC)) 8258 goto rewrite; 8259 8260 for (section = ibfd->sections, prev = NULL; 8261 section != NULL; section = section->next) 8262 { 8263 /* We mark the output section so that we know it comes 8264 from the input BFD. */ 8265 osec = section->output_section; 8266 if (osec) 8267 osec->segment_mark = true; 8268 8269 /* Check if this section is covered by the segment. */ 8270 this_hdr = &(elf_section_data(section)->this_hdr); 8271 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 8272 { 8273 /* FIXME: Check if its output section is changed or 8274 removed. What else do we need to check? */ 8275 if (osec == NULL 8276 || section->flags != osec->flags 8277 || section->lma != osec->lma 8278 || section->vma != osec->vma 8279 || section->size != osec->size 8280 || section->rawsize != osec->rawsize 8281 || section->alignment_power != osec->alignment_power) 8282 goto rewrite; 8283 8284 /* PR 31450: If this is an allocated section then make sure 8285 that this section's vma to lma relationship is the same 8286 as previous (allocated) section's. */ 8287 if (prev != NULL 8288 && section->flags & SEC_ALLOC 8289 && section->lma - section->vma != prev->lma - prev->vma) 8290 goto rewrite; 8291 8292 if (section->flags & SEC_ALLOC) 8293 prev = section; 8294 } 8295 } 8296 } 8297 8298 /* Check to see if any output section do not come from the 8299 input BFD. */ 8300 for (section = obfd->sections; section != NULL; 8301 section = section->next) 8302 { 8303 if (!section->segment_mark) 8304 goto rewrite; 8305 else 8306 section->segment_mark = false; 8307 } 8308 8309 return copy_elf_program_header (ibfd, obfd); 8310 } 8311 8312 rewrite: 8313 maxpagesize = 0; 8314 if (ibfd->xvec == obfd->xvec) 8315 { 8316 /* When rewriting program header, set the output maxpagesize to 8317 the maximum alignment of input PT_LOAD segments. */ 8318 Elf_Internal_Phdr *segment; 8319 unsigned int i; 8320 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; 8321 8322 for (i = 0, segment = elf_tdata (ibfd)->phdr; 8323 i < num_segments; 8324 i++, segment++) 8325 if (segment->p_type == PT_LOAD 8326 && maxpagesize < segment->p_align) 8327 { 8328 /* PR 17512: file: f17299af. */ 8329 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) 8330 /* xgettext:c-format */ 8331 _bfd_error_handler (_("%pB: warning: segment alignment of %#" 8332 PRIx64 " is too large"), 8333 ibfd, (uint64_t) segment->p_align); 8334 else 8335 maxpagesize = segment->p_align; 8336 } 8337 } 8338 if (maxpagesize == 0) 8339 maxpagesize = get_elf_backend_data (obfd)->maxpagesize; 8340 8341 return rewrite_elf_program_header (ibfd, obfd, maxpagesize); 8342 } 8343 8344 /* Initialize private output section information from input section. */ 8345 8346 bool 8347 _bfd_elf_init_private_section_data (bfd *ibfd, 8348 asection *isec, 8349 bfd *obfd, 8350 asection *osec, 8351 struct bfd_link_info *link_info) 8352 8353 { 8354 Elf_Internal_Shdr *ihdr, *ohdr; 8355 bool final_link = (link_info != NULL 8356 && !bfd_link_relocatable (link_info)); 8357 8358 if (ibfd->xvec->flavour != bfd_target_elf_flavour 8359 || obfd->xvec->flavour != bfd_target_elf_flavour) 8360 return true; 8361 8362 BFD_ASSERT (elf_section_data (osec) != NULL); 8363 8364 /* If this is a known ABI section, ELF section type and flags may 8365 have been set up when OSEC was created. For normal sections we 8366 allow the user to override the type and flags other than 8367 SHF_MASKOS and SHF_MASKPROC. */ 8368 if (elf_section_type (osec) == SHT_PROGBITS 8369 || elf_section_type (osec) == SHT_NOTE 8370 || elf_section_type (osec) == SHT_NOBITS) 8371 elf_section_type (osec) = SHT_NULL; 8372 /* For objcopy and relocatable link, copy the ELF section type from 8373 the input file if the BFD section flags are the same. (If they 8374 are different the user may be doing something like 8375 "objcopy --set-section-flags .text=alloc,data".) For a final 8376 link allow some flags that the linker clears to differ. */ 8377 if (elf_section_type (osec) == SHT_NULL 8378 && (osec->flags == isec->flags 8379 || (final_link 8380 && ((osec->flags ^ isec->flags) 8381 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) 8382 elf_section_type (osec) = elf_section_type (isec); 8383 8384 /* FIXME: Is this correct for all OS/PROC specific flags? */ 8385 elf_section_flags (osec) = (elf_section_flags (isec) 8386 & (SHF_MASKOS | SHF_MASKPROC)); 8387 8388 /* Copy sh_info from input for mbind section. */ 8389 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0 8390 && elf_section_flags (isec) & SHF_GNU_MBIND) 8391 elf_section_data (osec)->this_hdr.sh_info 8392 = elf_section_data (isec)->this_hdr.sh_info; 8393 8394 /* Set things up for objcopy and relocatable link. The output 8395 SHT_GROUP section will have its elf_next_in_group pointing back 8396 to the input group members. Ignore linker created group section. 8397 See elfNN_ia64_object_p in elfxx-ia64.c. */ 8398 if ((link_info == NULL 8399 || !link_info->resolve_section_groups) 8400 && (elf_sec_group (isec) == NULL 8401 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)) 8402 { 8403 if (elf_section_flags (isec) & SHF_GROUP) 8404 elf_section_flags (osec) |= SHF_GROUP; 8405 elf_next_in_group (osec) = elf_next_in_group (isec); 8406 elf_section_data (osec)->group = elf_section_data (isec)->group; 8407 } 8408 8409 /* If not decompress, preserve SHF_COMPRESSED. */ 8410 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0) 8411 elf_section_flags (osec) |= (elf_section_flags (isec) 8412 & SHF_COMPRESSED); 8413 8414 ihdr = &elf_section_data (isec)->this_hdr; 8415 8416 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We 8417 don't use the output section of the linked-to section since it 8418 may be NULL at this point. */ 8419 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) 8420 { 8421 ohdr = &elf_section_data (osec)->this_hdr; 8422 ohdr->sh_flags |= SHF_LINK_ORDER; 8423 elf_linked_to_section (osec) = elf_linked_to_section (isec); 8424 } 8425 8426 osec->use_rela_p = isec->use_rela_p; 8427 8428 return true; 8429 } 8430 8431 /* Copy private section information. This copies over the entsize 8432 field, and sometimes the info field. */ 8433 8434 bool 8435 _bfd_elf_copy_private_section_data (bfd *ibfd, 8436 asection *isec, 8437 bfd *obfd, 8438 asection *osec) 8439 { 8440 Elf_Internal_Shdr *ihdr, *ohdr; 8441 8442 if (ibfd->xvec->flavour != bfd_target_elf_flavour 8443 || obfd->xvec->flavour != bfd_target_elf_flavour) 8444 return true; 8445 8446 ihdr = &elf_section_data (isec)->this_hdr; 8447 ohdr = &elf_section_data (osec)->this_hdr; 8448 8449 ohdr->sh_entsize = ihdr->sh_entsize; 8450 8451 if (ihdr->sh_type == SHT_SYMTAB 8452 || ihdr->sh_type == SHT_DYNSYM 8453 || ihdr->sh_type == SHT_GNU_verneed 8454 || ihdr->sh_type == SHT_GNU_verdef) 8455 ohdr->sh_info = ihdr->sh_info; 8456 8457 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, 8458 NULL); 8459 } 8460 8461 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments 8462 necessary if we are removing either the SHT_GROUP section or any of 8463 the group member sections. DISCARDED is the value that a section's 8464 output_section has if the section will be discarded, NULL when this 8465 function is called from objcopy, bfd_abs_section_ptr when called 8466 from the linker. */ 8467 8468 bool 8469 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) 8470 { 8471 asection *isec; 8472 8473 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 8474 if (elf_section_type (isec) == SHT_GROUP) 8475 { 8476 asection *first = elf_next_in_group (isec); 8477 asection *s = first; 8478 bfd_size_type removed = 0; 8479 8480 while (s != NULL) 8481 { 8482 /* If this member section is being output but the 8483 SHT_GROUP section is not, then clear the group info 8484 set up by _bfd_elf_copy_private_section_data. */ 8485 if (s->output_section != discarded 8486 && isec->output_section == discarded) 8487 { 8488 elf_section_flags (s->output_section) &= ~SHF_GROUP; 8489 elf_group_name (s->output_section) = NULL; 8490 } 8491 else 8492 { 8493 struct bfd_elf_section_data *elf_sec = elf_section_data (s); 8494 if (s->output_section == discarded 8495 && isec->output_section != discarded) 8496 { 8497 /* Conversely, if the member section is not being 8498 output but the SHT_GROUP section is, then adjust 8499 its size. */ 8500 removed += 4; 8501 if (elf_sec->rel.hdr != NULL 8502 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0) 8503 removed += 4; 8504 if (elf_sec->rela.hdr != NULL 8505 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0) 8506 removed += 4; 8507 } 8508 else 8509 { 8510 /* Also adjust for zero-sized relocation member 8511 section. */ 8512 if (elf_sec->rel.hdr != NULL 8513 && elf_sec->rel.hdr->sh_size == 0) 8514 removed += 4; 8515 if (elf_sec->rela.hdr != NULL 8516 && elf_sec->rela.hdr->sh_size == 0) 8517 removed += 4; 8518 } 8519 } 8520 s = elf_next_in_group (s); 8521 if (s == first) 8522 break; 8523 } 8524 if (removed != 0) 8525 { 8526 if (discarded != NULL) 8527 { 8528 /* If we've been called for ld -r, then we need to 8529 adjust the input section size. */ 8530 if (isec->rawsize == 0) 8531 isec->rawsize = isec->size; 8532 isec->size = isec->rawsize - removed; 8533 if (isec->size <= 4) 8534 { 8535 isec->size = 0; 8536 isec->flags |= SEC_EXCLUDE; 8537 } 8538 } 8539 else if (isec->output_section != NULL) 8540 { 8541 /* Adjust the output section size when called from 8542 objcopy. */ 8543 isec->output_section->size -= removed; 8544 if (isec->output_section->size <= 4) 8545 { 8546 isec->output_section->size = 0; 8547 isec->output_section->flags |= SEC_EXCLUDE; 8548 } 8549 } 8550 } 8551 } 8552 8553 return true; 8554 } 8555 8556 /* Copy private header information. */ 8557 8558 bool 8559 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) 8560 { 8561 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 8562 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 8563 return true; 8564 8565 /* Copy over private BFD data if it has not already been copied. 8566 This must be done here, rather than in the copy_private_bfd_data 8567 entry point, because the latter is called after the section 8568 contents have been set, which means that the program headers have 8569 already been worked out. */ 8570 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) 8571 { 8572 if (! copy_private_bfd_data (ibfd, obfd)) 8573 return false; 8574 } 8575 8576 return _bfd_elf_fixup_group_sections (ibfd, NULL); 8577 } 8578 8579 /* Copy private symbol information. If this symbol is in a section 8580 which we did not map into a BFD section, try to map the section 8581 index correctly. We use special macro definitions for the mapped 8582 section indices; these definitions are interpreted by the 8583 swap_out_syms function. */ 8584 8585 #define MAP_ONESYMTAB (SHN_HIOS + 1) 8586 #define MAP_DYNSYMTAB (SHN_HIOS + 2) 8587 #define MAP_STRTAB (SHN_HIOS + 3) 8588 #define MAP_SHSTRTAB (SHN_HIOS + 4) 8589 #define MAP_SYM_SHNDX (SHN_HIOS + 5) 8590 8591 bool 8592 _bfd_elf_copy_private_symbol_data (bfd *ibfd, 8593 asymbol *isymarg, 8594 bfd *obfd, 8595 asymbol *osymarg) 8596 { 8597 elf_symbol_type *isym, *osym; 8598 8599 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 8600 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 8601 return true; 8602 8603 isym = elf_symbol_from (isymarg); 8604 osym = elf_symbol_from (osymarg); 8605 8606 if (isym != NULL 8607 && isym->internal_elf_sym.st_shndx != 0 8608 && osym != NULL 8609 && bfd_is_abs_section (isym->symbol.section)) 8610 { 8611 unsigned int shndx; 8612 8613 shndx = isym->internal_elf_sym.st_shndx; 8614 if (shndx == elf_onesymtab (ibfd)) 8615 shndx = MAP_ONESYMTAB; 8616 else if (shndx == elf_dynsymtab (ibfd)) 8617 shndx = MAP_DYNSYMTAB; 8618 else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link) 8619 shndx = MAP_STRTAB; 8620 else if (shndx == elf_elfheader (ibfd)->e_shstrndx) 8621 shndx = MAP_SHSTRTAB; 8622 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) 8623 shndx = MAP_SYM_SHNDX; 8624 osym->internal_elf_sym.st_shndx = shndx; 8625 } 8626 8627 return true; 8628 } 8629 8630 /* Swap out the symbols. */ 8631 8632 static bool 8633 swap_out_syms (bfd *abfd, 8634 struct elf_strtab_hash **sttp, 8635 int relocatable_p, 8636 struct bfd_link_info *info) 8637 { 8638 const struct elf_backend_data *bed; 8639 unsigned int symcount; 8640 asymbol **syms; 8641 struct elf_strtab_hash *stt; 8642 Elf_Internal_Shdr *symtab_hdr; 8643 Elf_Internal_Shdr *symtab_shndx_hdr; 8644 Elf_Internal_Shdr *symstrtab_hdr; 8645 struct elf_sym_strtab *symstrtab; 8646 bfd_byte *outbound_syms; 8647 bfd_byte *outbound_shndx; 8648 unsigned long outbound_syms_index; 8649 unsigned int idx; 8650 unsigned int num_locals; 8651 size_t amt; 8652 bool name_local_sections; 8653 8654 if (!elf_map_symbols (abfd, &num_locals)) 8655 return false; 8656 8657 /* Dump out the symtabs. */ 8658 stt = _bfd_elf_strtab_init (); 8659 if (stt == NULL) 8660 return false; 8661 8662 bed = get_elf_backend_data (abfd); 8663 symcount = bfd_get_symcount (abfd); 8664 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 8665 symtab_hdr->sh_type = SHT_SYMTAB; 8666 symtab_hdr->sh_entsize = bed->s->sizeof_sym; 8667 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); 8668 symtab_hdr->sh_info = num_locals + 1; 8669 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 8670 8671 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; 8672 symstrtab_hdr->sh_type = SHT_STRTAB; 8673 8674 /* Allocate buffer to swap out the .strtab section. */ 8675 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt) 8676 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL) 8677 { 8678 bfd_set_error (bfd_error_no_memory); 8679 _bfd_elf_strtab_free (stt); 8680 return false; 8681 } 8682 8683 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt) 8684 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL) 8685 { 8686 error_no_mem: 8687 bfd_set_error (bfd_error_no_memory); 8688 error_return: 8689 free (symstrtab); 8690 _bfd_elf_strtab_free (stt); 8691 return false; 8692 } 8693 symtab_hdr->contents = outbound_syms; 8694 outbound_syms_index = 0; 8695 8696 outbound_shndx = NULL; 8697 8698 if (elf_symtab_shndx_list (abfd)) 8699 { 8700 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; 8701 if (symtab_shndx_hdr->sh_name != 0) 8702 { 8703 if (_bfd_mul_overflow (symcount + 1, 8704 sizeof (Elf_External_Sym_Shndx), &amt)) 8705 goto error_no_mem; 8706 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt); 8707 if (outbound_shndx == NULL) 8708 goto error_return; 8709 8710 symtab_shndx_hdr->contents = outbound_shndx; 8711 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 8712 symtab_shndx_hdr->sh_size = amt; 8713 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 8714 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 8715 } 8716 /* FIXME: What about any other headers in the list ? */ 8717 } 8718 8719 /* Now generate the data (for "contents"). */ 8720 { 8721 /* Fill in zeroth symbol and swap it out. */ 8722 Elf_Internal_Sym sym; 8723 sym.st_name = 0; 8724 sym.st_value = 0; 8725 sym.st_size = 0; 8726 sym.st_info = 0; 8727 sym.st_other = 0; 8728 sym.st_shndx = SHN_UNDEF; 8729 sym.st_target_internal = 0; 8730 symstrtab[outbound_syms_index].sym = sym; 8731 symstrtab[outbound_syms_index].dest_index = outbound_syms_index; 8732 outbound_syms_index++; 8733 } 8734 8735 name_local_sections 8736 = (bed->elf_backend_name_local_section_symbols 8737 && bed->elf_backend_name_local_section_symbols (abfd)); 8738 8739 syms = bfd_get_outsymbols (abfd); 8740 for (idx = 0; idx < symcount; idx++) 8741 { 8742 Elf_Internal_Sym sym; 8743 8744 flagword flags = syms[idx]->flags; 8745 if (!name_local_sections 8746 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) 8747 { 8748 /* Local section symbols have no name. */ 8749 sym.st_name = 0; 8750 } 8751 else 8752 { 8753 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize 8754 to get the final offset for st_name. */ 8755 size_t stridx = _bfd_elf_strtab_add (stt, syms[idx]->name, false); 8756 if (stridx == (size_t) -1) 8757 goto error_return; 8758 sym.st_name = stridx; 8759 } 8760 8761 bfd_vma value = syms[idx]->value; 8762 elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]); 8763 asection *sec = syms[idx]->section; 8764 8765 if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec)) 8766 { 8767 /* ELF common symbols put the alignment into the `value' field, 8768 and the size into the `size' field. This is backwards from 8769 how BFD handles it, so reverse it here. */ 8770 sym.st_size = value; 8771 if (type_ptr == NULL 8772 || type_ptr->internal_elf_sym.st_value == 0) 8773 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); 8774 else 8775 sym.st_value = type_ptr->internal_elf_sym.st_value; 8776 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 8777 } 8778 else 8779 { 8780 unsigned int shndx; 8781 8782 if (sec->output_section) 8783 { 8784 value += sec->output_offset; 8785 sec = sec->output_section; 8786 } 8787 8788 /* Don't add in the section vma for relocatable output. */ 8789 if (! relocatable_p) 8790 value += sec->vma; 8791 sym.st_value = value; 8792 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; 8793 8794 if (bfd_is_abs_section (sec) 8795 && type_ptr != NULL 8796 && type_ptr->internal_elf_sym.st_shndx != 0) 8797 { 8798 /* This symbol is in a real ELF section which we did 8799 not create as a BFD section. Undo the mapping done 8800 by copy_private_symbol_data. */ 8801 shndx = type_ptr->internal_elf_sym.st_shndx; 8802 switch (shndx) 8803 { 8804 case MAP_ONESYMTAB: 8805 shndx = elf_onesymtab (abfd); 8806 break; 8807 case MAP_DYNSYMTAB: 8808 shndx = elf_dynsymtab (abfd); 8809 break; 8810 case MAP_STRTAB: 8811 shndx = elf_strtab_sec (abfd); 8812 break; 8813 case MAP_SHSTRTAB: 8814 shndx = elf_shstrtab_sec (abfd); 8815 break; 8816 case MAP_SYM_SHNDX: 8817 if (elf_symtab_shndx_list (abfd)) 8818 shndx = elf_symtab_shndx_list (abfd)->ndx; 8819 break; 8820 case SHN_COMMON: 8821 case SHN_ABS: 8822 shndx = SHN_ABS; 8823 break; 8824 default: 8825 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS) 8826 { 8827 if (bed->symbol_section_index) 8828 shndx = bed->symbol_section_index (abfd, type_ptr); 8829 /* Otherwise just leave the index alone. */ 8830 } 8831 else 8832 { 8833 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE) 8834 _bfd_error_handler (_("%pB: \ 8835 Unable to handle section index %x in ELF symbol. Using ABS instead."), 8836 abfd, shndx); 8837 shndx = SHN_ABS; 8838 } 8839 break; 8840 } 8841 } 8842 else 8843 { 8844 shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 8845 8846 if (shndx == SHN_BAD) 8847 { 8848 asection *sec2; 8849 8850 /* Writing this would be a hell of a lot easier if 8851 we had some decent documentation on bfd, and 8852 knew what to expect of the library, and what to 8853 demand of applications. For example, it 8854 appears that `objcopy' might not set the 8855 section of a symbol to be a section that is 8856 actually in the output file. */ 8857 sec2 = bfd_get_section_by_name (abfd, sec->name); 8858 if (sec2 != NULL) 8859 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); 8860 if (shndx == SHN_BAD) 8861 { 8862 /* xgettext:c-format */ 8863 _bfd_error_handler 8864 (_("unable to find equivalent output section" 8865 " for symbol '%s' from section '%s'"), 8866 syms[idx]->name ? syms[idx]->name : "<Local sym>", 8867 sec->name); 8868 bfd_set_error (bfd_error_invalid_operation); 8869 goto error_return; 8870 } 8871 } 8872 } 8873 8874 sym.st_shndx = shndx; 8875 } 8876 8877 int type; 8878 if ((flags & BSF_THREAD_LOCAL) != 0) 8879 type = STT_TLS; 8880 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) 8881 type = STT_GNU_IFUNC; 8882 else if ((flags & BSF_FUNCTION) != 0) 8883 type = STT_FUNC; 8884 else if ((flags & BSF_OBJECT) != 0) 8885 type = STT_OBJECT; 8886 else if ((flags & BSF_RELC) != 0) 8887 type = STT_RELC; 8888 else if ((flags & BSF_SRELC) != 0) 8889 type = STT_SRELC; 8890 else 8891 type = STT_NOTYPE; 8892 8893 if (syms[idx]->section->flags & SEC_THREAD_LOCAL) 8894 type = STT_TLS; 8895 8896 /* Processor-specific types. */ 8897 if (type_ptr != NULL 8898 && bed->elf_backend_get_symbol_type) 8899 type = ((*bed->elf_backend_get_symbol_type) 8900 (&type_ptr->internal_elf_sym, type)); 8901 8902 if (flags & BSF_SECTION_SYM) 8903 { 8904 if (flags & BSF_GLOBAL) 8905 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); 8906 else 8907 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 8908 } 8909 else if (bfd_is_com_section (syms[idx]->section)) 8910 { 8911 if (type != STT_TLS) 8912 { 8913 if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) 8914 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) 8915 ? STT_COMMON : STT_OBJECT); 8916 else 8917 type = ((flags & BSF_ELF_COMMON) != 0 8918 ? STT_COMMON : STT_OBJECT); 8919 } 8920 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); 8921 } 8922 else if (bfd_is_und_section (syms[idx]->section)) 8923 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) 8924 ? STB_WEAK 8925 : STB_GLOBAL), 8926 type); 8927 else if (flags & BSF_FILE) 8928 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 8929 else 8930 { 8931 int bind = STB_LOCAL; 8932 8933 if (flags & BSF_LOCAL) 8934 bind = STB_LOCAL; 8935 else if (flags & BSF_GNU_UNIQUE) 8936 bind = STB_GNU_UNIQUE; 8937 else if (flags & BSF_WEAK) 8938 bind = STB_WEAK; 8939 else if (flags & BSF_GLOBAL) 8940 bind = STB_GLOBAL; 8941 8942 sym.st_info = ELF_ST_INFO (bind, type); 8943 } 8944 8945 if (type_ptr != NULL) 8946 { 8947 sym.st_other = type_ptr->internal_elf_sym.st_other; 8948 sym.st_target_internal 8949 = type_ptr->internal_elf_sym.st_target_internal; 8950 } 8951 else 8952 { 8953 sym.st_other = 0; 8954 sym.st_target_internal = 0; 8955 } 8956 8957 symstrtab[outbound_syms_index].sym = sym; 8958 symstrtab[outbound_syms_index].dest_index = outbound_syms_index; 8959 outbound_syms_index++; 8960 } 8961 8962 /* Finalize the .strtab section. */ 8963 _bfd_elf_strtab_finalize (stt); 8964 8965 /* Swap out the .strtab section. */ 8966 for (idx = 0; idx < outbound_syms_index; idx++) 8967 { 8968 struct elf_sym_strtab *elfsym = &symstrtab[idx]; 8969 if (elfsym->sym.st_name != 0) 8970 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, 8971 elfsym->sym.st_name); 8972 if (info && info->callbacks->ctf_new_symbol) 8973 info->callbacks->ctf_new_symbol (elfsym->dest_index, 8974 &elfsym->sym); 8975 8976 /* Inform the linker of the addition of this symbol. */ 8977 8978 bed->s->swap_symbol_out (abfd, &elfsym->sym, 8979 (outbound_syms 8980 + (elfsym->dest_index 8981 * bed->s->sizeof_sym)), 8982 NPTR_ADD (outbound_shndx, 8983 (elfsym->dest_index 8984 * sizeof (Elf_External_Sym_Shndx)))); 8985 } 8986 free (symstrtab); 8987 8988 *sttp = stt; 8989 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); 8990 symstrtab_hdr->sh_type = SHT_STRTAB; 8991 symstrtab_hdr->sh_flags = bed->elf_strtab_flags; 8992 symstrtab_hdr->sh_addr = 0; 8993 symstrtab_hdr->sh_entsize = 0; 8994 symstrtab_hdr->sh_link = 0; 8995 symstrtab_hdr->sh_info = 0; 8996 symstrtab_hdr->sh_addralign = 1; 8997 8998 return true; 8999 } 9000 9001 /* Return the number of bytes required to hold the symtab vector. 9002 9003 Note that we base it on the count plus 1, since we will null terminate 9004 the vector allocated based on this size. However, the ELF symbol table 9005 always has a dummy entry as symbol #0, so it ends up even. */ 9006 9007 long 9008 _bfd_elf_get_symtab_upper_bound (bfd *abfd) 9009 { 9010 bfd_size_type symcount; 9011 long symtab_size; 9012 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; 9013 9014 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 9015 if (symcount > LONG_MAX / sizeof (asymbol *)) 9016 { 9017 bfd_set_error (bfd_error_file_too_big); 9018 return -1; 9019 } 9020 symtab_size = symcount * (sizeof (asymbol *)); 9021 if (symcount == 0) 9022 symtab_size = sizeof (asymbol *); 9023 else if (!bfd_write_p (abfd)) 9024 { 9025 ufile_ptr filesize = bfd_get_file_size (abfd); 9026 9027 if (filesize != 0 && (unsigned long) symtab_size > filesize) 9028 { 9029 bfd_set_error (bfd_error_file_truncated); 9030 return -1; 9031 } 9032 } 9033 9034 return symtab_size; 9035 } 9036 9037 long 9038 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) 9039 { 9040 bfd_size_type symcount; 9041 long symtab_size; 9042 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; 9043 9044 if (elf_dynsymtab (abfd) == 0) 9045 { 9046 /* Check if there is dynamic symbol table. */ 9047 symcount = elf_tdata (abfd)->dt_symtab_count; 9048 if (symcount) 9049 goto compute_symtab_size; 9050 9051 bfd_set_error (bfd_error_invalid_operation); 9052 return -1; 9053 } 9054 9055 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 9056 if (symcount > LONG_MAX / sizeof (asymbol *)) 9057 { 9058 bfd_set_error (bfd_error_file_too_big); 9059 return -1; 9060 } 9061 9062 compute_symtab_size: 9063 symtab_size = symcount * (sizeof (asymbol *)); 9064 if (symcount == 0) 9065 symtab_size = sizeof (asymbol *); 9066 else if (!bfd_write_p (abfd)) 9067 { 9068 ufile_ptr filesize = bfd_get_file_size (abfd); 9069 9070 if (filesize != 0 && (unsigned long) symtab_size > filesize) 9071 { 9072 bfd_set_error (bfd_error_file_truncated); 9073 return -1; 9074 } 9075 } 9076 9077 return symtab_size; 9078 } 9079 9080 long 9081 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect) 9082 { 9083 if (asect->reloc_count != 0 && !bfd_write_p (abfd)) 9084 { 9085 /* Sanity check reloc section size. */ 9086 ufile_ptr filesize = bfd_get_file_size (abfd); 9087 9088 if (filesize != 0) 9089 { 9090 struct bfd_elf_section_data *d = elf_section_data (asect); 9091 bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0; 9092 bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0; 9093 9094 if (rel_size + rela_size > filesize 9095 || rel_size + rela_size < rel_size) 9096 { 9097 bfd_set_error (bfd_error_file_truncated); 9098 return -1; 9099 } 9100 } 9101 } 9102 9103 #if SIZEOF_LONG == SIZEOF_INT 9104 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *)) 9105 { 9106 bfd_set_error (bfd_error_file_too_big); 9107 return -1; 9108 } 9109 #endif 9110 return (asect->reloc_count + 1L) * sizeof (arelent *); 9111 } 9112 9113 /* Canonicalize the relocs. */ 9114 9115 long 9116 _bfd_elf_canonicalize_reloc (bfd *abfd, 9117 sec_ptr section, 9118 arelent **relptr, 9119 asymbol **symbols) 9120 { 9121 arelent *tblptr; 9122 unsigned int i; 9123 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9124 9125 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false)) 9126 return -1; 9127 9128 tblptr = section->relocation; 9129 for (i = 0; i < section->reloc_count; i++) 9130 *relptr++ = tblptr++; 9131 9132 *relptr = NULL; 9133 9134 return section->reloc_count; 9135 } 9136 9137 long 9138 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) 9139 { 9140 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9141 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false); 9142 9143 if (symcount >= 0) 9144 abfd->symcount = symcount; 9145 return symcount; 9146 } 9147 9148 long 9149 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, 9150 asymbol **allocation) 9151 { 9152 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9153 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true); 9154 9155 if (symcount >= 0) 9156 abfd->dynsymcount = symcount; 9157 return symcount; 9158 } 9159 9160 /* Return the size required for the dynamic reloc entries. Any loadable 9161 section that was actually installed in the BFD, and has type SHT_REL 9162 or SHT_RELA, and uses the dynamic symbol table, is considered to be a 9163 dynamic reloc section. */ 9164 9165 long 9166 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) 9167 { 9168 bfd_size_type count, ext_rel_size; 9169 asection *s; 9170 9171 if (elf_dynsymtab (abfd) == 0) 9172 { 9173 bfd_set_error (bfd_error_invalid_operation); 9174 return -1; 9175 } 9176 9177 count = 1; 9178 ext_rel_size = 0; 9179 for (s = abfd->sections; s != NULL; s = s->next) 9180 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 9181 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 9182 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA) 9183 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0) 9184 { 9185 ext_rel_size += elf_section_data (s)->this_hdr.sh_size; 9186 if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size) 9187 { 9188 bfd_set_error (bfd_error_file_truncated); 9189 return -1; 9190 } 9191 count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr); 9192 if (count > LONG_MAX / sizeof (arelent *)) 9193 { 9194 bfd_set_error (bfd_error_file_too_big); 9195 return -1; 9196 } 9197 } 9198 if (count > 1 && !bfd_write_p (abfd)) 9199 { 9200 /* Sanity check reloc section sizes. */ 9201 ufile_ptr filesize = bfd_get_file_size (abfd); 9202 if (filesize != 0 && ext_rel_size > filesize) 9203 { 9204 bfd_set_error (bfd_error_file_truncated); 9205 return -1; 9206 } 9207 } 9208 return count * sizeof (arelent *); 9209 } 9210 9211 /* Canonicalize the dynamic relocation entries. Note that we return the 9212 dynamic relocations as a single block, although they are actually 9213 associated with particular sections; the interface, which was 9214 designed for SunOS style shared libraries, expects that there is only 9215 one set of dynamic relocs. Any loadable section that was actually 9216 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the 9217 dynamic symbol table, is considered to be a dynamic reloc section. */ 9218 9219 long 9220 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, 9221 arelent **storage, 9222 asymbol **syms) 9223 { 9224 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool); 9225 asection *s; 9226 long ret; 9227 9228 if (elf_dynsymtab (abfd) == 0) 9229 { 9230 bfd_set_error (bfd_error_invalid_operation); 9231 return -1; 9232 } 9233 9234 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; 9235 ret = 0; 9236 for (s = abfd->sections; s != NULL; s = s->next) 9237 { 9238 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 9239 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 9240 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA) 9241 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0) 9242 { 9243 arelent *p; 9244 long count, i; 9245 9246 if (! (*slurp_relocs) (abfd, s, syms, true)) 9247 return -1; 9248 count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr); 9249 p = s->relocation; 9250 for (i = 0; i < count; i++) 9251 *storage++ = p++; 9252 ret += count; 9253 } 9254 } 9255 9256 *storage = NULL; 9257 9258 return ret; 9259 } 9260 9261 /* Read in the version information. */ 9263 9264 bool 9265 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver) 9266 { 9267 bfd_byte *contents = NULL; 9268 unsigned int freeidx = 0; 9269 size_t amt; 9270 void *contents_addr = NULL; 9271 size_t contents_size = 0; 9272 9273 if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL) 9274 { 9275 Elf_Internal_Shdr *hdr; 9276 Elf_External_Verneed *everneed; 9277 Elf_Internal_Verneed *iverneed; 9278 unsigned int i; 9279 bfd_byte *contents_end; 9280 size_t verneed_count; 9281 size_t verneed_size; 9282 9283 if (elf_tdata (abfd)->dt_verneed != NULL) 9284 { 9285 hdr = NULL; 9286 contents = elf_tdata (abfd)->dt_verneed; 9287 verneed_count = elf_tdata (abfd)->dt_verneed_count; 9288 verneed_size = verneed_count * sizeof (Elf_External_Verneed); 9289 } 9290 else 9291 { 9292 hdr = &elf_tdata (abfd)->dynverref_hdr; 9293 9294 if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed)) 9295 { 9296 error_return_bad_verref: 9297 _bfd_error_handler 9298 (_("%pB: .gnu.version_r invalid entry"), abfd); 9299 bfd_set_error (bfd_error_bad_value); 9300 error_return_verref: 9301 elf_tdata (abfd)->verref = NULL; 9302 elf_tdata (abfd)->cverrefs = 0; 9303 goto error_return; 9304 } 9305 9306 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) 9307 goto error_return_verref; 9308 contents_size = hdr->sh_size; 9309 contents = _bfd_mmap_temporary (abfd, contents_size, 9310 &contents_addr, &contents_size); 9311 if (contents == NULL) 9312 goto error_return_verref; 9313 9314 verneed_size = hdr->sh_size; 9315 verneed_count = hdr->sh_info; 9316 } 9317 9318 if (_bfd_mul_overflow (verneed_count, 9319 sizeof (Elf_Internal_Verneed), &amt)) 9320 { 9321 bfd_set_error (bfd_error_file_too_big); 9322 goto error_return_verref; 9323 } 9324 if (amt == 0) 9325 goto error_return_verref; 9326 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt); 9327 if (elf_tdata (abfd)->verref == NULL) 9328 goto error_return_verref; 9329 9330 BFD_ASSERT (sizeof (Elf_External_Verneed) 9331 == sizeof (Elf_External_Vernaux)); 9332 contents_end = (contents + verneed_size 9333 - sizeof (Elf_External_Verneed)); 9334 everneed = (Elf_External_Verneed *) contents; 9335 iverneed = elf_tdata (abfd)->verref; 9336 for (i = 0; i < verneed_count; i++, iverneed++) 9337 { 9338 Elf_External_Vernaux *evernaux; 9339 Elf_Internal_Vernaux *ivernaux; 9340 unsigned int j; 9341 9342 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); 9343 9344 iverneed->vn_bfd = abfd; 9345 9346 if (elf_use_dt_symtab_p (abfd)) 9347 { 9348 if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz) 9349 iverneed->vn_filename 9350 = elf_tdata (abfd)->dt_strtab + iverneed->vn_file; 9351 else 9352 iverneed->vn_filename = NULL; 9353 } 9354 else if (hdr == NULL) 9355 goto error_return_bad_verref; 9356 else 9357 iverneed->vn_filename 9358 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 9359 iverneed->vn_file); 9360 if (iverneed->vn_filename == NULL) 9361 goto error_return_bad_verref; 9362 9363 if (iverneed->vn_cnt == 0) 9364 iverneed->vn_auxptr = NULL; 9365 else 9366 { 9367 if (_bfd_mul_overflow (iverneed->vn_cnt, 9368 sizeof (Elf_Internal_Vernaux), &amt)) 9369 { 9370 bfd_set_error (bfd_error_file_too_big); 9371 goto error_return_verref; 9372 } 9373 iverneed->vn_auxptr = (struct elf_internal_vernaux *) 9374 bfd_alloc (abfd, amt); 9375 if (iverneed->vn_auxptr == NULL) 9376 goto error_return_verref; 9377 } 9378 9379 if (iverneed->vn_aux 9380 > (size_t) (contents_end - (bfd_byte *) everneed)) 9381 goto error_return_bad_verref; 9382 9383 evernaux = ((Elf_External_Vernaux *) 9384 ((bfd_byte *) everneed + iverneed->vn_aux)); 9385 ivernaux = iverneed->vn_auxptr; 9386 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) 9387 { 9388 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); 9389 9390 if (elf_use_dt_symtab_p (abfd)) 9391 { 9392 if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz) 9393 ivernaux->vna_nodename 9394 = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name; 9395 else 9396 ivernaux->vna_nodename = NULL; 9397 } 9398 else if (hdr == NULL) 9399 goto error_return_bad_verref; 9400 else 9401 ivernaux->vna_nodename 9402 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 9403 ivernaux->vna_name); 9404 if (ivernaux->vna_nodename == NULL) 9405 goto error_return_bad_verref; 9406 9407 if (ivernaux->vna_other > freeidx) 9408 freeidx = ivernaux->vna_other; 9409 9410 ivernaux->vna_nextptr = NULL; 9411 if (ivernaux->vna_next == 0) 9412 { 9413 iverneed->vn_cnt = j + 1; 9414 break; 9415 } 9416 if (j + 1 < iverneed->vn_cnt) 9417 ivernaux->vna_nextptr = ivernaux + 1; 9418 9419 if (ivernaux->vna_next 9420 > (size_t) (contents_end - (bfd_byte *) evernaux)) 9421 goto error_return_bad_verref; 9422 9423 evernaux = ((Elf_External_Vernaux *) 9424 ((bfd_byte *) evernaux + ivernaux->vna_next)); 9425 } 9426 9427 iverneed->vn_nextref = NULL; 9428 if (iverneed->vn_next == 0) 9429 break; 9430 if (hdr != NULL && (i + 1 < hdr->sh_info)) 9431 iverneed->vn_nextref = iverneed + 1; 9432 9433 if (iverneed->vn_next 9434 > (size_t) (contents_end - (bfd_byte *) everneed)) 9435 goto error_return_bad_verref; 9436 9437 everneed = ((Elf_External_Verneed *) 9438 ((bfd_byte *) everneed + iverneed->vn_next)); 9439 } 9440 elf_tdata (abfd)->cverrefs = i; 9441 9442 if (contents != elf_tdata (abfd)->dt_verneed) 9443 _bfd_munmap_temporary (contents_addr, contents_size); 9444 contents = NULL; 9445 contents_addr = NULL; 9446 } 9447 9448 if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL) 9449 { 9450 Elf_Internal_Shdr *hdr; 9451 Elf_External_Verdef *everdef; 9452 Elf_Internal_Verdef *iverdef; 9453 Elf_Internal_Verdef *iverdefarr; 9454 Elf_Internal_Verdef iverdefmem; 9455 unsigned int i; 9456 unsigned int maxidx; 9457 bfd_byte *contents_end_def, *contents_end_aux; 9458 size_t verdef_count; 9459 size_t verdef_size; 9460 9461 if (elf_tdata (abfd)->dt_verdef != NULL) 9462 { 9463 hdr = NULL; 9464 contents = elf_tdata (abfd)->dt_verdef; 9465 verdef_count = elf_tdata (abfd)->dt_verdef_count; 9466 verdef_size = verdef_count * sizeof (Elf_External_Verdef); 9467 } 9468 else 9469 { 9470 hdr = &elf_tdata (abfd)->dynverdef_hdr; 9471 9472 if (hdr->sh_size < sizeof (Elf_External_Verdef)) 9473 { 9474 error_return_bad_verdef: 9475 _bfd_error_handler 9476 (_("%pB: .gnu.version_d invalid entry"), abfd); 9477 bfd_set_error (bfd_error_bad_value); 9478 error_return_verdef: 9479 elf_tdata (abfd)->verdef = NULL; 9480 elf_tdata (abfd)->cverdefs = 0; 9481 goto error_return; 9482 } 9483 9484 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) 9485 goto error_return_verdef; 9486 contents_size = hdr->sh_size; 9487 contents = _bfd_mmap_temporary (abfd, contents_size, 9488 &contents_addr, &contents_size); 9489 if (contents == NULL) 9490 goto error_return_verdef; 9491 9492 BFD_ASSERT (sizeof (Elf_External_Verdef) 9493 >= sizeof (Elf_External_Verdaux)); 9494 9495 verdef_count = hdr->sh_info; 9496 verdef_size = hdr->sh_size; 9497 } 9498 9499 contents_end_def = (contents + verdef_size 9500 - sizeof (Elf_External_Verdef)); 9501 contents_end_aux = (contents + verdef_size 9502 - sizeof (Elf_External_Verdaux)); 9503 9504 /* We know the number of entries in the section but not the maximum 9505 index. Therefore we have to run through all entries and find 9506 the maximum. */ 9507 everdef = (Elf_External_Verdef *) contents; 9508 maxidx = 0; 9509 for (i = 0; i < verdef_count; ++i) 9510 { 9511 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 9512 9513 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) 9514 goto error_return_bad_verdef; 9515 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) 9516 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); 9517 9518 if (iverdefmem.vd_next == 0) 9519 break; 9520 9521 if (iverdefmem.vd_next 9522 > (size_t) (contents_end_def - (bfd_byte *) everdef)) 9523 goto error_return_bad_verdef; 9524 9525 everdef = ((Elf_External_Verdef *) 9526 ((bfd_byte *) everdef + iverdefmem.vd_next)); 9527 } 9528 9529 if (default_imported_symver) 9530 { 9531 if (freeidx > maxidx) 9532 maxidx = ++freeidx; 9533 else 9534 freeidx = ++maxidx; 9535 } 9536 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt)) 9537 { 9538 bfd_set_error (bfd_error_file_too_big); 9539 goto error_return_verdef; 9540 } 9541 9542 if (amt == 0) 9543 goto error_return_verdef; 9544 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); 9545 if (elf_tdata (abfd)->verdef == NULL) 9546 goto error_return_verdef; 9547 9548 elf_tdata (abfd)->cverdefs = maxidx; 9549 9550 everdef = (Elf_External_Verdef *) contents; 9551 iverdefarr = elf_tdata (abfd)->verdef; 9552 for (i = 0; i < verdef_count; ++i) 9553 { 9554 Elf_External_Verdaux *everdaux; 9555 Elf_Internal_Verdaux *iverdaux; 9556 unsigned int j; 9557 9558 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 9559 9560 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) 9561 goto error_return_bad_verdef; 9562 9563 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; 9564 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); 9565 9566 iverdef->vd_bfd = abfd; 9567 9568 if (iverdef->vd_cnt == 0) 9569 iverdef->vd_auxptr = NULL; 9570 else 9571 { 9572 if (_bfd_mul_overflow (iverdef->vd_cnt, 9573 sizeof (Elf_Internal_Verdaux), &amt)) 9574 { 9575 bfd_set_error (bfd_error_file_too_big); 9576 goto error_return_verdef; 9577 } 9578 iverdef->vd_auxptr = (struct elf_internal_verdaux *) 9579 bfd_alloc (abfd, amt); 9580 if (iverdef->vd_auxptr == NULL) 9581 goto error_return_verdef; 9582 } 9583 9584 if (iverdef->vd_aux 9585 > (size_t) (contents_end_aux - (bfd_byte *) everdef)) 9586 goto error_return_bad_verdef; 9587 9588 everdaux = ((Elf_External_Verdaux *) 9589 ((bfd_byte *) everdef + iverdef->vd_aux)); 9590 iverdaux = iverdef->vd_auxptr; 9591 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) 9592 { 9593 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); 9594 9595 if (elf_use_dt_symtab_p (abfd)) 9596 { 9597 if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz) 9598 iverdaux->vda_nodename 9599 = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name; 9600 else 9601 iverdaux->vda_nodename = NULL; 9602 } 9603 else 9604 iverdaux->vda_nodename 9605 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 9606 iverdaux->vda_name); 9607 if (iverdaux->vda_nodename == NULL) 9608 goto error_return_bad_verdef; 9609 9610 iverdaux->vda_nextptr = NULL; 9611 if (iverdaux->vda_next == 0) 9612 { 9613 iverdef->vd_cnt = j + 1; 9614 break; 9615 } 9616 if (j + 1 < iverdef->vd_cnt) 9617 iverdaux->vda_nextptr = iverdaux + 1; 9618 9619 if (iverdaux->vda_next 9620 > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) 9621 goto error_return_bad_verdef; 9622 9623 everdaux = ((Elf_External_Verdaux *) 9624 ((bfd_byte *) everdaux + iverdaux->vda_next)); 9625 } 9626 9627 iverdef->vd_nodename = NULL; 9628 if (iverdef->vd_cnt) 9629 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; 9630 9631 iverdef->vd_nextdef = NULL; 9632 if (iverdef->vd_next == 0) 9633 break; 9634 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) 9635 iverdef->vd_nextdef = iverdef + 1; 9636 9637 everdef = ((Elf_External_Verdef *) 9638 ((bfd_byte *) everdef + iverdef->vd_next)); 9639 } 9640 9641 if (contents != elf_tdata (abfd)->dt_verdef) 9642 _bfd_munmap_temporary (contents_addr, contents_size); 9643 contents = NULL; 9644 contents_addr = NULL; 9645 } 9646 else if (default_imported_symver) 9647 { 9648 if (freeidx < 3) 9649 freeidx = 3; 9650 else 9651 freeidx++; 9652 9653 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt)) 9654 { 9655 bfd_set_error (bfd_error_file_too_big); 9656 goto error_return; 9657 } 9658 if (amt == 0) 9659 goto error_return; 9660 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); 9661 if (elf_tdata (abfd)->verdef == NULL) 9662 goto error_return; 9663 9664 elf_tdata (abfd)->cverdefs = freeidx; 9665 } 9666 9667 /* Create a default version based on the soname. */ 9668 if (default_imported_symver) 9669 { 9670 Elf_Internal_Verdef *iverdef; 9671 Elf_Internal_Verdaux *iverdaux; 9672 9673 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; 9674 9675 iverdef->vd_version = VER_DEF_CURRENT; 9676 iverdef->vd_flags = 0; 9677 iverdef->vd_ndx = freeidx; 9678 iverdef->vd_cnt = 1; 9679 9680 iverdef->vd_bfd = abfd; 9681 9682 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); 9683 if (iverdef->vd_nodename == NULL) 9684 goto error_return_verdef; 9685 iverdef->vd_nextdef = NULL; 9686 iverdef->vd_auxptr = ((struct elf_internal_verdaux *) 9687 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); 9688 if (iverdef->vd_auxptr == NULL) 9689 goto error_return_verdef; 9690 9691 iverdaux = iverdef->vd_auxptr; 9692 iverdaux->vda_nodename = iverdef->vd_nodename; 9693 } 9694 9695 return true; 9696 9697 error_return: 9698 if (contents != elf_tdata (abfd)->dt_verneed 9699 && contents != elf_tdata (abfd)->dt_verdef) 9700 _bfd_munmap_temporary (contents_addr, contents_size); 9701 return false; 9702 } 9703 9704 asymbol * 9706 _bfd_elf_make_empty_symbol (bfd *abfd) 9707 { 9708 elf_symbol_type *newsym; 9709 9710 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym)); 9711 if (!newsym) 9712 return NULL; 9713 newsym->symbol.the_bfd = abfd; 9714 return &newsym->symbol; 9715 } 9716 9717 void 9718 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, 9719 asymbol *symbol, 9720 symbol_info *ret) 9721 { 9722 bfd_symbol_info (symbol, ret); 9723 } 9724 9725 /* Return whether a symbol name implies a local symbol. Most targets 9726 use this function for the is_local_label_name entry point, but some 9727 override it. */ 9728 9729 bool 9730 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, 9731 const char *name) 9732 { 9733 /* Normal local symbols start with ``.L''. */ 9734 if (name[0] == '.' && name[1] == 'L') 9735 return true; 9736 9737 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate 9738 DWARF debugging symbols starting with ``..''. */ 9739 if (name[0] == '.' && name[1] == '.') 9740 return true; 9741 9742 /* gcc will sometimes generate symbols beginning with ``_.L_'' when 9743 emitting DWARF debugging output. I suspect this is actually a 9744 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call 9745 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading 9746 underscore to be emitted on some ELF targets). For ease of use, 9747 we treat such symbols as local. */ 9748 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') 9749 return true; 9750 9751 /* Treat assembler generated fake symbols, dollar local labels and 9752 forward-backward labels (aka local labels) as locals. 9753 These labels have the form: 9754 9755 L0^A.* (fake symbols) 9756 9757 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) 9758 9759 Versions which start with .L will have already been matched above, 9760 so we only need to match the rest. */ 9761 if (name[0] == 'L' && ISDIGIT (name[1])) 9762 { 9763 bool ret = false; 9764 const char * p; 9765 char c; 9766 9767 for (p = name + 2; (c = *p); p++) 9768 { 9769 if (c == 1 || c == 2) 9770 { 9771 if (c == 1 && p == name + 2) 9772 /* A fake symbol. */ 9773 return true; 9774 9775 /* FIXME: We are being paranoid here and treating symbols like 9776 L0^Bfoo as if there were non-local, on the grounds that the 9777 assembler will never generate them. But can any symbol 9778 containing an ASCII value in the range 1-31 ever be anything 9779 other than some kind of local ? */ 9780 ret = true; 9781 } 9782 9783 if (! ISDIGIT (c)) 9784 { 9785 ret = false; 9786 break; 9787 } 9788 } 9789 return ret; 9790 } 9791 9792 return false; 9793 } 9794 9795 alent * 9796 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, 9797 asymbol *symbol ATTRIBUTE_UNUSED) 9798 { 9799 abort (); 9800 return NULL; 9801 } 9802 9803 bool 9804 _bfd_elf_set_arch_mach (bfd *abfd, 9805 enum bfd_architecture arch, 9806 unsigned long machine) 9807 { 9808 /* If this isn't the right architecture for this backend, and this 9809 isn't the generic backend, fail. */ 9810 if (arch != get_elf_backend_data (abfd)->arch 9811 && arch != bfd_arch_unknown 9812 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) 9813 return false; 9814 9815 return bfd_default_set_arch_mach (abfd, arch, machine); 9816 } 9817 9818 /* Find the nearest line to a particular section and offset, 9819 for error reporting. */ 9820 9821 bool 9822 _bfd_elf_find_nearest_line (bfd *abfd, 9823 asymbol **symbols, 9824 asection *section, 9825 bfd_vma offset, 9826 const char **filename_ptr, 9827 const char **functionname_ptr, 9828 unsigned int *line_ptr, 9829 unsigned int *discriminator_ptr) 9830 { 9831 return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section, 9832 offset, filename_ptr, 9833 functionname_ptr, line_ptr, 9834 discriminator_ptr); 9835 } 9836 9837 /* Find the nearest line to a particular section and offset, 9838 for error reporting. ALT_BFD representing a .gnu_debugaltlink file 9839 can be optionally specified. */ 9840 9841 bool 9842 _bfd_elf_find_nearest_line_with_alt (bfd *abfd, 9843 const char *alt_filename, 9844 asymbol **symbols, 9845 asection *section, 9846 bfd_vma offset, 9847 const char **filename_ptr, 9848 const char **functionname_ptr, 9849 unsigned int *line_ptr, 9850 unsigned int *discriminator_ptr) 9851 { 9852 bool found; 9853 9854 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL, 9855 section, offset, filename_ptr, 9856 functionname_ptr, line_ptr, 9857 discriminator_ptr, 9858 dwarf_debug_sections, 9859 &elf_tdata (abfd)->dwarf2_find_line_info)) 9860 return true; 9861 9862 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, 9863 filename_ptr, functionname_ptr, line_ptr)) 9864 { 9865 if (!*functionname_ptr) 9866 _bfd_elf_find_function (abfd, symbols, section, offset, 9867 *filename_ptr ? NULL : filename_ptr, 9868 functionname_ptr); 9869 return true; 9870 } 9871 9872 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, 9873 &found, filename_ptr, 9874 functionname_ptr, line_ptr, 9875 &elf_tdata (abfd)->line_info)) 9876 return false; 9877 if (found && (*functionname_ptr || *line_ptr)) 9878 return true; 9879 9880 if (symbols == NULL) 9881 return false; 9882 9883 if (! _bfd_elf_find_function (abfd, symbols, section, offset, 9884 filename_ptr, functionname_ptr)) 9885 return false; 9886 9887 *line_ptr = 0; 9888 return true; 9889 } 9890 9891 /* Find the line for a symbol. */ 9892 9893 bool 9894 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, 9895 const char **filename_ptr, unsigned int *line_ptr) 9896 { 9897 struct elf_obj_tdata *tdata = elf_tdata (abfd); 9898 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, 9899 filename_ptr, NULL, line_ptr, NULL, 9900 dwarf_debug_sections, 9901 &tdata->dwarf2_find_line_info); 9902 } 9903 9904 /* After a call to bfd_find_nearest_line, successive calls to 9905 bfd_find_inliner_info can be used to get source information about 9906 each level of function inlining that terminated at the address 9907 passed to bfd_find_nearest_line. Currently this is only supported 9908 for DWARF2 with appropriate DWARF3 extensions. */ 9909 9910 bool 9911 _bfd_elf_find_inliner_info (bfd *abfd, 9912 const char **filename_ptr, 9913 const char **functionname_ptr, 9914 unsigned int *line_ptr) 9915 { 9916 struct elf_obj_tdata *tdata = elf_tdata (abfd); 9917 return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, 9918 functionname_ptr, line_ptr, 9919 &tdata->dwarf2_find_line_info); 9920 } 9921 9922 int 9923 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) 9924 { 9925 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9926 int ret = bed->s->sizeof_ehdr; 9927 9928 if (!bfd_link_relocatable (info)) 9929 { 9930 bfd_size_type phdr_size = elf_program_header_size (abfd); 9931 9932 if (phdr_size == (bfd_size_type) -1) 9933 { 9934 struct elf_segment_map *m; 9935 9936 phdr_size = 0; 9937 for (m = elf_seg_map (abfd); m != NULL; m = m->next) 9938 phdr_size += bed->s->sizeof_phdr; 9939 9940 if (phdr_size == 0) 9941 phdr_size = get_program_header_size (abfd, info); 9942 } 9943 9944 elf_program_header_size (abfd) = phdr_size; 9945 ret += phdr_size; 9946 } 9947 9948 return ret; 9949 } 9950 9951 bool 9952 _bfd_elf_set_section_contents (bfd *abfd, 9953 sec_ptr section, 9954 const void *location, 9955 file_ptr offset, 9956 bfd_size_type count) 9957 { 9958 Elf_Internal_Shdr *hdr; 9959 9960 if (! abfd->output_has_begun 9961 && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) 9962 return false; 9963 9964 if (!count) 9965 return true; 9966 9967 hdr = &elf_section_data (section)->this_hdr; 9968 if (hdr->sh_offset == (file_ptr) -1) 9969 { 9970 unsigned char *contents; 9971 9972 if (bfd_section_is_ctf (section)) 9973 /* Nothing to do with this section: the contents are generated 9974 later. */ 9975 return true; 9976 9977 if ((offset + count) > hdr->sh_size) 9978 { 9979 _bfd_error_handler 9980 (_("%pB:%pA: error: attempting to write" 9981 " over the end of the section"), 9982 abfd, section); 9983 9984 bfd_set_error (bfd_error_invalid_operation); 9985 return false; 9986 } 9987 9988 contents = hdr->contents; 9989 if (contents == NULL) 9990 { 9991 _bfd_error_handler 9992 (_("%pB:%pA: error: attempting to write" 9993 " section into an empty buffer"), 9994 abfd, section); 9995 9996 bfd_set_error (bfd_error_invalid_operation); 9997 return false; 9998 } 9999 10000 memcpy (contents + offset, location, count); 10001 return true; 10002 } 10003 10004 return _bfd_generic_set_section_contents (abfd, section, 10005 location, offset, count); 10006 } 10007 10008 bool 10009 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, 10010 arelent *cache_ptr ATTRIBUTE_UNUSED, 10011 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) 10012 { 10013 abort (); 10014 return false; 10015 } 10016 10017 /* Try to convert a non-ELF reloc into an ELF one. */ 10018 10019 bool 10020 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) 10021 { 10022 /* Check whether we really have an ELF howto. */ 10023 10024 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) 10025 { 10026 bfd_reloc_code_real_type code; 10027 reloc_howto_type *howto; 10028 10029 /* Alien reloc: Try to determine its type to replace it with an 10030 equivalent ELF reloc. */ 10031 10032 if (areloc->howto->pc_relative) 10033 { 10034 switch (areloc->howto->bitsize) 10035 { 10036 case 8: 10037 code = BFD_RELOC_8_PCREL; 10038 break; 10039 case 12: 10040 code = BFD_RELOC_12_PCREL; 10041 break; 10042 case 16: 10043 code = BFD_RELOC_16_PCREL; 10044 break; 10045 case 24: 10046 code = BFD_RELOC_24_PCREL; 10047 break; 10048 case 32: 10049 code = BFD_RELOC_32_PCREL; 10050 break; 10051 case 64: 10052 code = BFD_RELOC_64_PCREL; 10053 break; 10054 default: 10055 goto fail; 10056 } 10057 10058 howto = bfd_reloc_type_lookup (abfd, code); 10059 10060 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset) 10061 { 10062 if (howto->pcrel_offset) 10063 areloc->addend += areloc->address; 10064 else 10065 areloc->addend -= areloc->address; /* addend is unsigned!! */ 10066 } 10067 } 10068 else 10069 { 10070 switch (areloc->howto->bitsize) 10071 { 10072 case 8: 10073 code = BFD_RELOC_8; 10074 break; 10075 case 14: 10076 code = BFD_RELOC_14; 10077 break; 10078 case 16: 10079 code = BFD_RELOC_16; 10080 break; 10081 case 26: 10082 code = BFD_RELOC_26; 10083 break; 10084 case 32: 10085 code = BFD_RELOC_32; 10086 break; 10087 case 64: 10088 code = BFD_RELOC_64; 10089 break; 10090 default: 10091 goto fail; 10092 } 10093 10094 howto = bfd_reloc_type_lookup (abfd, code); 10095 } 10096 10097 if (howto) 10098 areloc->howto = howto; 10099 else 10100 goto fail; 10101 } 10102 10103 return true; 10104 10105 fail: 10106 /* xgettext:c-format */ 10107 _bfd_error_handler (_("%pB: %s unsupported"), 10108 abfd, areloc->howto->name); 10109 bfd_set_error (bfd_error_sorry); 10110 return false; 10111 } 10112 10113 bool 10114 _bfd_elf_free_cached_info (bfd *abfd) 10115 { 10116 struct elf_obj_tdata *tdata; 10117 10118 if ((bfd_get_format (abfd) == bfd_object 10119 || bfd_get_format (abfd) == bfd_core) 10120 && (tdata = elf_tdata (abfd)) != NULL) 10121 { 10122 if (tdata->o != NULL && elf_shstrtab (abfd) != NULL) 10123 _bfd_elf_strtab_free (elf_shstrtab (abfd)); 10124 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); 10125 _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info); 10126 _bfd_stab_cleanup (abfd, &tdata->line_info); 10127 } 10128 10129 return _bfd_generic_bfd_free_cached_info (abfd); 10130 } 10131 10132 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY 10133 in the relocation's offset. Thus we cannot allow any sort of sanity 10134 range-checking to interfere. There is nothing else to do in processing 10135 this reloc. */ 10136 10137 bfd_reloc_status_type 10138 _bfd_elf_rel_vtable_reloc_fn 10139 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, 10140 struct bfd_symbol *symbol ATTRIBUTE_UNUSED, 10141 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, 10142 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) 10143 { 10144 return bfd_reloc_ok; 10145 } 10146 10147 /* Elf core file support. Much of this only works on native 10149 toolchains, since we rely on knowing the 10150 machine-dependent procfs structure in order to pick 10151 out details about the corefile. */ 10152 10153 #ifdef HAVE_SYS_PROCFS_H 10154 # include <sys/procfs.h> 10155 #endif 10156 10157 /* Return a PID that identifies a "thread" for threaded cores, or the 10158 PID of the main process for non-threaded cores. */ 10159 10160 static int 10161 elfcore_make_pid (bfd *abfd) 10162 { 10163 int pid; 10164 10165 pid = elf_tdata (abfd)->core->lwpid; 10166 if (pid == 0) 10167 pid = elf_tdata (abfd)->core->pid; 10168 10169 return pid; 10170 } 10171 10172 /* If there isn't a section called NAME, make one, using data from 10173 SECT. Note, this function will generate a reference to NAME, so 10174 you shouldn't deallocate or overwrite it. */ 10175 10176 static bool 10177 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) 10178 { 10179 asection *sect2; 10180 10181 if (bfd_get_section_by_name (abfd, name) != NULL) 10182 return true; 10183 10184 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); 10185 if (sect2 == NULL) 10186 return false; 10187 10188 sect2->size = sect->size; 10189 sect2->filepos = sect->filepos; 10190 sect2->alignment_power = sect->alignment_power; 10191 return true; 10192 } 10193 10194 /* Create a pseudosection containing SIZE bytes at FILEPOS. This 10195 actually creates up to two pseudosections: 10196 - For the single-threaded case, a section named NAME, unless 10197 such a section already exists. 10198 - For the multi-threaded case, a section named "NAME/PID", where 10199 PID is elfcore_make_pid (abfd). 10200 Both pseudosections have identical contents. */ 10201 bool 10202 _bfd_elfcore_make_pseudosection (bfd *abfd, 10203 char *name, 10204 size_t size, 10205 ufile_ptr filepos) 10206 { 10207 char buf[100]; 10208 char *threaded_name; 10209 size_t len; 10210 asection *sect; 10211 10212 /* Build the section name. */ 10213 10214 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); 10215 len = strlen (buf) + 1; 10216 threaded_name = (char *) bfd_alloc (abfd, len); 10217 if (threaded_name == NULL) 10218 return false; 10219 memcpy (threaded_name, buf, len); 10220 10221 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, 10222 SEC_HAS_CONTENTS); 10223 if (sect == NULL) 10224 return false; 10225 sect->size = size; 10226 sect->filepos = filepos; 10227 sect->alignment_power = 2; 10228 10229 return elfcore_maybe_make_sect (abfd, name, sect); 10230 } 10231 10232 static bool 10233 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note, 10234 size_t offs) 10235 { 10236 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", 10237 SEC_HAS_CONTENTS); 10238 10239 if (sect == NULL) 10240 return false; 10241 10242 sect->size = note->descsz - offs; 10243 sect->filepos = note->descpos + offs; 10244 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 10245 10246 return true; 10247 } 10248 10249 /* prstatus_t exists on: 10250 solaris 2.5+ 10251 linux 2.[01] + glibc 10252 unixware 4.2 10253 */ 10254 10255 #if defined (HAVE_PRSTATUS_T) 10256 10257 static bool 10258 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) 10259 { 10260 size_t size; 10261 int offset; 10262 10263 if (note->descsz == sizeof (prstatus_t)) 10264 { 10265 prstatus_t prstat; 10266 10267 size = sizeof (prstat.pr_reg); 10268 offset = offsetof (prstatus_t, pr_reg); 10269 memcpy (&prstat, note->descdata, sizeof (prstat)); 10270 10271 /* Do not overwrite the core signal if it 10272 has already been set by another thread. */ 10273 if (elf_tdata (abfd)->core->signal == 0) 10274 elf_tdata (abfd)->core->signal = prstat.pr_cursig; 10275 if (elf_tdata (abfd)->core->pid == 0) 10276 elf_tdata (abfd)->core->pid = prstat.pr_pid; 10277 10278 /* pr_who exists on: 10279 solaris 2.5+ 10280 unixware 4.2 10281 pr_who doesn't exist on: 10282 linux 2.[01] 10283 */ 10284 #if defined (HAVE_PRSTATUS_T_PR_WHO) 10285 elf_tdata (abfd)->core->lwpid = prstat.pr_who; 10286 #else 10287 elf_tdata (abfd)->core->lwpid = prstat.pr_pid; 10288 #endif 10289 } 10290 #if defined (HAVE_PRSTATUS32_T) 10291 else if (note->descsz == sizeof (prstatus32_t)) 10292 { 10293 /* 64-bit host, 32-bit corefile */ 10294 prstatus32_t prstat; 10295 10296 size = sizeof (prstat.pr_reg); 10297 offset = offsetof (prstatus32_t, pr_reg); 10298 memcpy (&prstat, note->descdata, sizeof (prstat)); 10299 10300 /* Do not overwrite the core signal if it 10301 has already been set by another thread. */ 10302 if (elf_tdata (abfd)->core->signal == 0) 10303 elf_tdata (abfd)->core->signal = prstat.pr_cursig; 10304 if (elf_tdata (abfd)->core->pid == 0) 10305 elf_tdata (abfd)->core->pid = prstat.pr_pid; 10306 10307 /* pr_who exists on: 10308 solaris 2.5+ 10309 unixware 4.2 10310 pr_who doesn't exist on: 10311 linux 2.[01] 10312 */ 10313 #if defined (HAVE_PRSTATUS32_T_PR_WHO) 10314 elf_tdata (abfd)->core->lwpid = prstat.pr_who; 10315 #else 10316 elf_tdata (abfd)->core->lwpid = prstat.pr_pid; 10317 #endif 10318 } 10319 #endif /* HAVE_PRSTATUS32_T */ 10320 else 10321 { 10322 /* Fail - we don't know how to handle any other 10323 note size (ie. data object type). */ 10324 return true; 10325 } 10326 10327 /* Make a ".reg/999" section and a ".reg" section. */ 10328 return _bfd_elfcore_make_pseudosection (abfd, ".reg", 10329 size, note->descpos + offset); 10330 } 10331 #endif /* defined (HAVE_PRSTATUS_T) */ 10332 10333 /* Create a pseudosection containing the exact contents of NOTE. */ 10334 static bool 10335 elfcore_make_note_pseudosection (bfd *abfd, 10336 char *name, 10337 Elf_Internal_Note *note) 10338 { 10339 return _bfd_elfcore_make_pseudosection (abfd, name, 10340 note->descsz, note->descpos); 10341 } 10342 10343 /* There isn't a consistent prfpregset_t across platforms, 10344 but it doesn't matter, because we don't have to pick this 10345 data structure apart. */ 10346 10347 static bool 10348 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) 10349 { 10350 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 10351 } 10352 10353 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note 10354 type of NT_PRXFPREG. Just include the whole note's contents 10355 literally. */ 10356 10357 static bool 10358 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) 10359 { 10360 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); 10361 } 10362 10363 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" 10364 with a note type of NT_X86_XSTATE. Just include the whole note's 10365 contents literally. */ 10366 10367 static bool 10368 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) 10369 { 10370 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); 10371 } 10372 10373 static bool 10374 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) 10375 { 10376 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); 10377 } 10378 10379 static bool 10380 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) 10381 { 10382 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); 10383 } 10384 10385 static bool 10386 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note) 10387 { 10388 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note); 10389 } 10390 10391 static bool 10392 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note) 10393 { 10394 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note); 10395 } 10396 10397 static bool 10398 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note) 10399 { 10400 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note); 10401 } 10402 10403 static bool 10404 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note) 10405 { 10406 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note); 10407 } 10408 10409 static bool 10410 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note) 10411 { 10412 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note); 10413 } 10414 10415 static bool 10416 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note) 10417 { 10418 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note); 10419 } 10420 10421 static bool 10422 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note) 10423 { 10424 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note); 10425 } 10426 10427 static bool 10428 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note) 10429 { 10430 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note); 10431 } 10432 10433 static bool 10434 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note) 10435 { 10436 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note); 10437 } 10438 10439 static bool 10440 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note) 10441 { 10442 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note); 10443 } 10444 10445 static bool 10446 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note) 10447 { 10448 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note); 10449 } 10450 10451 static bool 10452 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note) 10453 { 10454 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note); 10455 } 10456 10457 static bool 10458 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note) 10459 { 10460 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note); 10461 } 10462 10463 static bool 10464 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) 10465 { 10466 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); 10467 } 10468 10469 static bool 10470 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) 10471 { 10472 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); 10473 } 10474 10475 static bool 10476 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) 10477 { 10478 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); 10479 } 10480 10481 static bool 10482 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) 10483 { 10484 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); 10485 } 10486 10487 static bool 10488 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) 10489 { 10490 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); 10491 } 10492 10493 static bool 10494 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) 10495 { 10496 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); 10497 } 10498 10499 static bool 10500 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) 10501 { 10502 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); 10503 } 10504 10505 static bool 10506 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) 10507 { 10508 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); 10509 } 10510 10511 static bool 10512 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) 10513 { 10514 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); 10515 } 10516 10517 static bool 10518 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) 10519 { 10520 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); 10521 } 10522 10523 static bool 10524 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) 10525 { 10526 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); 10527 } 10528 10529 static bool 10530 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note) 10531 { 10532 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note); 10533 } 10534 10535 static bool 10536 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note) 10537 { 10538 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note); 10539 } 10540 10541 static bool 10542 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) 10543 { 10544 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); 10545 } 10546 10547 static bool 10548 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) 10549 { 10550 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); 10551 } 10552 10553 static bool 10554 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) 10555 { 10556 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); 10557 } 10558 10559 static bool 10560 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) 10561 { 10562 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); 10563 } 10564 10565 static bool 10566 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note) 10567 { 10568 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note); 10569 } 10570 10571 static bool 10572 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note) 10573 { 10574 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note); 10575 } 10576 10577 static bool 10578 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note) 10579 { 10580 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte", 10581 note); 10582 } 10583 10584 static bool 10585 elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note) 10586 { 10587 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note); 10588 } 10589 10590 static bool 10591 elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note) 10592 { 10593 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note); 10594 } 10595 10596 /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if 10597 successful, otherwise return FALSE. */ 10598 10599 static bool 10600 elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note) 10601 { 10602 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note); 10603 } 10604 10605 static bool 10606 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note) 10607 { 10608 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note); 10609 } 10610 10611 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if 10612 successful otherwise, return FALSE. */ 10613 10614 static bool 10615 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note) 10616 { 10617 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note); 10618 } 10619 10620 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if 10621 successful otherwise, return FALSE. */ 10622 10623 static bool 10624 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note) 10625 { 10626 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note); 10627 } 10628 10629 static bool 10630 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note) 10631 { 10632 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note); 10633 } 10634 10635 static bool 10636 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note) 10637 { 10638 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note); 10639 } 10640 10641 static bool 10642 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note) 10643 { 10644 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note); 10645 } 10646 10647 static bool 10648 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note) 10649 { 10650 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note); 10651 } 10652 10653 #if defined (HAVE_PRPSINFO_T) 10654 typedef prpsinfo_t elfcore_psinfo_t; 10655 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ 10656 typedef prpsinfo32_t elfcore_psinfo32_t; 10657 #endif 10658 #endif 10659 10660 #if defined (HAVE_PSINFO_T) 10661 typedef psinfo_t elfcore_psinfo_t; 10662 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ 10663 typedef psinfo32_t elfcore_psinfo32_t; 10664 #endif 10665 #endif 10666 10667 /* return a malloc'ed copy of a string at START which is at 10668 most MAX bytes long, possibly without a terminating '\0'. 10669 the copy will always have a terminating '\0'. */ 10670 10671 char * 10672 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) 10673 { 10674 char *dups; 10675 char *end = (char *) memchr (start, '\0', max); 10676 size_t len; 10677 10678 if (end == NULL) 10679 len = max; 10680 else 10681 len = end - start; 10682 10683 dups = (char *) bfd_alloc (abfd, len + 1); 10684 if (dups == NULL) 10685 return NULL; 10686 10687 memcpy (dups, start, len); 10688 dups[len] = '\0'; 10689 10690 return dups; 10691 } 10692 10693 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 10694 static bool 10695 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) 10696 { 10697 if (note->descsz == sizeof (elfcore_psinfo_t)) 10698 { 10699 elfcore_psinfo_t psinfo; 10700 10701 memcpy (&psinfo, note->descdata, sizeof (psinfo)); 10702 10703 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) 10704 elf_tdata (abfd)->core->pid = psinfo.pr_pid; 10705 #endif 10706 elf_tdata (abfd)->core->program 10707 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, 10708 sizeof (psinfo.pr_fname)); 10709 10710 elf_tdata (abfd)->core->command 10711 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, 10712 sizeof (psinfo.pr_psargs)); 10713 } 10714 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) 10715 else if (note->descsz == sizeof (elfcore_psinfo32_t)) 10716 { 10717 /* 64-bit host, 32-bit corefile */ 10718 elfcore_psinfo32_t psinfo; 10719 10720 memcpy (&psinfo, note->descdata, sizeof (psinfo)); 10721 10722 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) 10723 elf_tdata (abfd)->core->pid = psinfo.pr_pid; 10724 #endif 10725 elf_tdata (abfd)->core->program 10726 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, 10727 sizeof (psinfo.pr_fname)); 10728 10729 elf_tdata (abfd)->core->command 10730 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, 10731 sizeof (psinfo.pr_psargs)); 10732 } 10733 #endif 10734 10735 else 10736 { 10737 /* Fail - we don't know how to handle any other 10738 note size (ie. data object type). */ 10739 return true; 10740 } 10741 10742 /* Note that for some reason, a spurious space is tacked 10743 onto the end of the args in some (at least one anyway) 10744 implementations, so strip it off if it exists. */ 10745 10746 { 10747 char *command = elf_tdata (abfd)->core->command; 10748 int n = strlen (command); 10749 10750 if (0 < n && command[n - 1] == ' ') 10751 command[n - 1] = '\0'; 10752 } 10753 10754 return true; 10755 } 10756 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ 10757 10758 #if defined (HAVE_PSTATUS_T) 10759 static bool 10760 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) 10761 { 10762 if (note->descsz == sizeof (pstatus_t) 10763 #if defined (HAVE_PXSTATUS_T) 10764 || note->descsz == sizeof (pxstatus_t) 10765 #endif 10766 ) 10767 { 10768 pstatus_t pstat; 10769 10770 memcpy (&pstat, note->descdata, sizeof (pstat)); 10771 10772 elf_tdata (abfd)->core->pid = pstat.pr_pid; 10773 } 10774 #if defined (HAVE_PSTATUS32_T) 10775 else if (note->descsz == sizeof (pstatus32_t)) 10776 { 10777 /* 64-bit host, 32-bit corefile */ 10778 pstatus32_t pstat; 10779 10780 memcpy (&pstat, note->descdata, sizeof (pstat)); 10781 10782 elf_tdata (abfd)->core->pid = pstat.pr_pid; 10783 } 10784 #endif 10785 /* Could grab some more details from the "representative" 10786 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an 10787 NT_LWPSTATUS note, presumably. */ 10788 10789 return true; 10790 } 10791 #endif /* defined (HAVE_PSTATUS_T) */ 10792 10793 #if defined (HAVE_LWPSTATUS_T) 10794 static bool 10795 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) 10796 { 10797 lwpstatus_t lwpstat; 10798 char buf[100]; 10799 char *name; 10800 size_t len; 10801 asection *sect; 10802 10803 if (note->descsz != sizeof (lwpstat) 10804 #if defined (HAVE_LWPXSTATUS_T) 10805 && note->descsz != sizeof (lwpxstatus_t) 10806 #endif 10807 ) 10808 return true; 10809 10810 memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); 10811 10812 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; 10813 /* Do not overwrite the core signal if it has already been set by 10814 another thread. */ 10815 if (elf_tdata (abfd)->core->signal == 0) 10816 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; 10817 10818 /* Make a ".reg/999" section. */ 10819 10820 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); 10821 len = strlen (buf) + 1; 10822 name = bfd_alloc (abfd, len); 10823 if (name == NULL) 10824 return false; 10825 memcpy (name, buf, len); 10826 10827 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 10828 if (sect == NULL) 10829 return false; 10830 10831 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 10832 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); 10833 sect->filepos = note->descpos 10834 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); 10835 #endif 10836 10837 #if defined (HAVE_LWPSTATUS_T_PR_REG) 10838 sect->size = sizeof (lwpstat.pr_reg); 10839 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); 10840 #endif 10841 10842 sect->alignment_power = 2; 10843 10844 if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) 10845 return false; 10846 10847 /* Make a ".reg2/999" section */ 10848 10849 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); 10850 len = strlen (buf) + 1; 10851 name = bfd_alloc (abfd, len); 10852 if (name == NULL) 10853 return false; 10854 memcpy (name, buf, len); 10855 10856 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 10857 if (sect == NULL) 10858 return false; 10859 10860 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 10861 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); 10862 sect->filepos = note->descpos 10863 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); 10864 #endif 10865 10866 #if defined (HAVE_LWPSTATUS_T_PR_FPREG) 10867 sect->size = sizeof (lwpstat.pr_fpreg); 10868 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); 10869 #endif 10870 10871 sect->alignment_power = 2; 10872 10873 return elfcore_maybe_make_sect (abfd, ".reg2", sect); 10874 } 10875 #endif /* defined (HAVE_LWPSTATUS_T) */ 10876 10877 /* These constants, and the structure offsets used below, are defined by 10878 Cygwin's core_dump.h */ 10879 #define NOTE_INFO_PROCESS 1 10880 #define NOTE_INFO_THREAD 2 10881 #define NOTE_INFO_MODULE 3 10882 #define NOTE_INFO_MODULE64 4 10883 10884 static bool 10885 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) 10886 { 10887 char buf[30]; 10888 char *name; 10889 size_t len; 10890 unsigned int name_size; 10891 asection *sect; 10892 unsigned int type; 10893 int is_active_thread; 10894 bfd_vma base_addr; 10895 10896 if (note->descsz < 4) 10897 return true; 10898 10899 if (! startswith (note->namedata, "win32")) 10900 return true; 10901 10902 type = bfd_get_32 (abfd, note->descdata); 10903 10904 struct 10905 { 10906 const char *type_name; 10907 unsigned long min_size; 10908 } size_check[] = 10909 { 10910 { "NOTE_INFO_PROCESS", 12 }, 10911 { "NOTE_INFO_THREAD", 12 }, 10912 { "NOTE_INFO_MODULE", 12 }, 10913 { "NOTE_INFO_MODULE64", 16 }, 10914 }; 10915 10916 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0]))) 10917 return true; 10918 10919 if (note->descsz < size_check[type - 1].min_size) 10920 { 10921 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes" 10922 " is too small"), 10923 abfd, size_check[type - 1].type_name, note->descsz); 10924 return true; 10925 } 10926 10927 switch (type) 10928 { 10929 case NOTE_INFO_PROCESS: 10930 /* FIXME: need to add ->core->command. */ 10931 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4); 10932 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8); 10933 break; 10934 10935 case NOTE_INFO_THREAD: 10936 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT 10937 structure. */ 10938 /* thread_info.tid */ 10939 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4)); 10940 10941 len = strlen (buf) + 1; 10942 name = (char *) bfd_alloc (abfd, len); 10943 if (name == NULL) 10944 return false; 10945 10946 memcpy (name, buf, len); 10947 10948 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 10949 if (sect == NULL) 10950 return false; 10951 10952 /* sizeof (thread_info.thread_context) */ 10953 sect->size = note->descsz - 12; 10954 /* offsetof (thread_info.thread_context) */ 10955 sect->filepos = note->descpos + 12; 10956 sect->alignment_power = 2; 10957 10958 /* thread_info.is_active_thread */ 10959 is_active_thread = bfd_get_32 (abfd, note->descdata + 8); 10960 10961 if (is_active_thread) 10962 if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) 10963 return false; 10964 break; 10965 10966 case NOTE_INFO_MODULE: 10967 case NOTE_INFO_MODULE64: 10968 /* Make a ".module/xxxxxxxx" section. */ 10969 if (type == NOTE_INFO_MODULE) 10970 { 10971 /* module_info.base_address */ 10972 base_addr = bfd_get_32 (abfd, note->descdata + 4); 10973 sprintf (buf, ".module/%08lx", (unsigned long) base_addr); 10974 /* module_info.module_name_size */ 10975 name_size = bfd_get_32 (abfd, note->descdata + 8); 10976 } 10977 else /* NOTE_INFO_MODULE64 */ 10978 { 10979 /* module_info.base_address */ 10980 base_addr = bfd_get_64 (abfd, note->descdata + 4); 10981 sprintf (buf, ".module/%016lx", (unsigned long) base_addr); 10982 /* module_info.module_name_size */ 10983 name_size = bfd_get_32 (abfd, note->descdata + 12); 10984 } 10985 10986 len = strlen (buf) + 1; 10987 name = (char *) bfd_alloc (abfd, len); 10988 if (name == NULL) 10989 return false; 10990 10991 memcpy (name, buf, len); 10992 10993 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 10994 10995 if (sect == NULL) 10996 return false; 10997 10998 if (note->descsz < 12 + name_size) 10999 { 11000 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu" 11001 " is too small to contain a name of size %u"), 11002 abfd, note->descsz, name_size); 11003 return true; 11004 } 11005 11006 sect->size = note->descsz; 11007 sect->filepos = note->descpos; 11008 sect->alignment_power = 2; 11009 break; 11010 11011 default: 11012 return true; 11013 } 11014 11015 return true; 11016 } 11017 11018 static bool 11019 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) 11020 { 11021 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11022 11023 switch (note->type) 11024 { 11025 default: 11026 return true; 11027 11028 case NT_PRSTATUS: 11029 if (bed->elf_backend_grok_prstatus) 11030 if ((*bed->elf_backend_grok_prstatus) (abfd, note)) 11031 return true; 11032 #if defined (HAVE_PRSTATUS_T) 11033 return elfcore_grok_prstatus (abfd, note); 11034 #else 11035 return true; 11036 #endif 11037 11038 #if defined (HAVE_PSTATUS_T) 11039 case NT_PSTATUS: 11040 return elfcore_grok_pstatus (abfd, note); 11041 #endif 11042 11043 #if defined (HAVE_LWPSTATUS_T) 11044 case NT_LWPSTATUS: 11045 return elfcore_grok_lwpstatus (abfd, note); 11046 #endif 11047 11048 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ 11049 return elfcore_grok_prfpreg (abfd, note); 11050 11051 case NT_WIN32PSTATUS: 11052 return elfcore_grok_win32pstatus (abfd, note); 11053 11054 case NT_PRXFPREG: /* Linux SSE extension */ 11055 if (note->namesz == 6 11056 && strcmp (note->namedata, "LINUX") == 0) 11057 return elfcore_grok_prxfpreg (abfd, note); 11058 else 11059 return true; 11060 11061 case NT_X86_XSTATE: /* Linux XSAVE extension */ 11062 if (note->namesz == 6 11063 && strcmp (note->namedata, "LINUX") == 0) 11064 return elfcore_grok_xstatereg (abfd, note); 11065 else 11066 return true; 11067 11068 case NT_PPC_VMX: 11069 if (note->namesz == 6 11070 && strcmp (note->namedata, "LINUX") == 0) 11071 return elfcore_grok_ppc_vmx (abfd, note); 11072 else 11073 return true; 11074 11075 case NT_PPC_VSX: 11076 if (note->namesz == 6 11077 && strcmp (note->namedata, "LINUX") == 0) 11078 return elfcore_grok_ppc_vsx (abfd, note); 11079 else 11080 return true; 11081 11082 case NT_PPC_TAR: 11083 if (note->namesz == 6 11084 && strcmp (note->namedata, "LINUX") == 0) 11085 return elfcore_grok_ppc_tar (abfd, note); 11086 else 11087 return true; 11088 11089 case NT_PPC_PPR: 11090 if (note->namesz == 6 11091 && strcmp (note->namedata, "LINUX") == 0) 11092 return elfcore_grok_ppc_ppr (abfd, note); 11093 else 11094 return true; 11095 11096 case NT_PPC_DSCR: 11097 if (note->namesz == 6 11098 && strcmp (note->namedata, "LINUX") == 0) 11099 return elfcore_grok_ppc_dscr (abfd, note); 11100 else 11101 return true; 11102 11103 case NT_PPC_EBB: 11104 if (note->namesz == 6 11105 && strcmp (note->namedata, "LINUX") == 0) 11106 return elfcore_grok_ppc_ebb (abfd, note); 11107 else 11108 return true; 11109 11110 case NT_PPC_PMU: 11111 if (note->namesz == 6 11112 && strcmp (note->namedata, "LINUX") == 0) 11113 return elfcore_grok_ppc_pmu (abfd, note); 11114 else 11115 return true; 11116 11117 case NT_PPC_TM_CGPR: 11118 if (note->namesz == 6 11119 && strcmp (note->namedata, "LINUX") == 0) 11120 return elfcore_grok_ppc_tm_cgpr (abfd, note); 11121 else 11122 return true; 11123 11124 case NT_PPC_TM_CFPR: 11125 if (note->namesz == 6 11126 && strcmp (note->namedata, "LINUX") == 0) 11127 return elfcore_grok_ppc_tm_cfpr (abfd, note); 11128 else 11129 return true; 11130 11131 case NT_PPC_TM_CVMX: 11132 if (note->namesz == 6 11133 && strcmp (note->namedata, "LINUX") == 0) 11134 return elfcore_grok_ppc_tm_cvmx (abfd, note); 11135 else 11136 return true; 11137 11138 case NT_PPC_TM_CVSX: 11139 if (note->namesz == 6 11140 && strcmp (note->namedata, "LINUX") == 0) 11141 return elfcore_grok_ppc_tm_cvsx (abfd, note); 11142 else 11143 return true; 11144 11145 case NT_PPC_TM_SPR: 11146 if (note->namesz == 6 11147 && strcmp (note->namedata, "LINUX") == 0) 11148 return elfcore_grok_ppc_tm_spr (abfd, note); 11149 else 11150 return true; 11151 11152 case NT_PPC_TM_CTAR: 11153 if (note->namesz == 6 11154 && strcmp (note->namedata, "LINUX") == 0) 11155 return elfcore_grok_ppc_tm_ctar (abfd, note); 11156 else 11157 return true; 11158 11159 case NT_PPC_TM_CPPR: 11160 if (note->namesz == 6 11161 && strcmp (note->namedata, "LINUX") == 0) 11162 return elfcore_grok_ppc_tm_cppr (abfd, note); 11163 else 11164 return true; 11165 11166 case NT_PPC_TM_CDSCR: 11167 if (note->namesz == 6 11168 && strcmp (note->namedata, "LINUX") == 0) 11169 return elfcore_grok_ppc_tm_cdscr (abfd, note); 11170 else 11171 return true; 11172 11173 case NT_S390_HIGH_GPRS: 11174 if (note->namesz == 6 11175 && strcmp (note->namedata, "LINUX") == 0) 11176 return elfcore_grok_s390_high_gprs (abfd, note); 11177 else 11178 return true; 11179 11180 case NT_S390_TIMER: 11181 if (note->namesz == 6 11182 && strcmp (note->namedata, "LINUX") == 0) 11183 return elfcore_grok_s390_timer (abfd, note); 11184 else 11185 return true; 11186 11187 case NT_S390_TODCMP: 11188 if (note->namesz == 6 11189 && strcmp (note->namedata, "LINUX") == 0) 11190 return elfcore_grok_s390_todcmp (abfd, note); 11191 else 11192 return true; 11193 11194 case NT_S390_TODPREG: 11195 if (note->namesz == 6 11196 && strcmp (note->namedata, "LINUX") == 0) 11197 return elfcore_grok_s390_todpreg (abfd, note); 11198 else 11199 return true; 11200 11201 case NT_S390_CTRS: 11202 if (note->namesz == 6 11203 && strcmp (note->namedata, "LINUX") == 0) 11204 return elfcore_grok_s390_ctrs (abfd, note); 11205 else 11206 return true; 11207 11208 case NT_S390_PREFIX: 11209 if (note->namesz == 6 11210 && strcmp (note->namedata, "LINUX") == 0) 11211 return elfcore_grok_s390_prefix (abfd, note); 11212 else 11213 return true; 11214 11215 case NT_S390_LAST_BREAK: 11216 if (note->namesz == 6 11217 && strcmp (note->namedata, "LINUX") == 0) 11218 return elfcore_grok_s390_last_break (abfd, note); 11219 else 11220 return true; 11221 11222 case NT_S390_SYSTEM_CALL: 11223 if (note->namesz == 6 11224 && strcmp (note->namedata, "LINUX") == 0) 11225 return elfcore_grok_s390_system_call (abfd, note); 11226 else 11227 return true; 11228 11229 case NT_S390_TDB: 11230 if (note->namesz == 6 11231 && strcmp (note->namedata, "LINUX") == 0) 11232 return elfcore_grok_s390_tdb (abfd, note); 11233 else 11234 return true; 11235 11236 case NT_S390_VXRS_LOW: 11237 if (note->namesz == 6 11238 && strcmp (note->namedata, "LINUX") == 0) 11239 return elfcore_grok_s390_vxrs_low (abfd, note); 11240 else 11241 return true; 11242 11243 case NT_S390_VXRS_HIGH: 11244 if (note->namesz == 6 11245 && strcmp (note->namedata, "LINUX") == 0) 11246 return elfcore_grok_s390_vxrs_high (abfd, note); 11247 else 11248 return true; 11249 11250 case NT_S390_GS_CB: 11251 if (note->namesz == 6 11252 && strcmp (note->namedata, "LINUX") == 0) 11253 return elfcore_grok_s390_gs_cb (abfd, note); 11254 else 11255 return true; 11256 11257 case NT_S390_GS_BC: 11258 if (note->namesz == 6 11259 && strcmp (note->namedata, "LINUX") == 0) 11260 return elfcore_grok_s390_gs_bc (abfd, note); 11261 else 11262 return true; 11263 11264 case NT_ARC_V2: 11265 if (note->namesz == 6 11266 && strcmp (note->namedata, "LINUX") == 0) 11267 return elfcore_grok_arc_v2 (abfd, note); 11268 else 11269 return true; 11270 11271 case NT_ARM_VFP: 11272 if (note->namesz == 6 11273 && strcmp (note->namedata, "LINUX") == 0) 11274 return elfcore_grok_arm_vfp (abfd, note); 11275 else 11276 return true; 11277 11278 case NT_ARM_TLS: 11279 if (note->namesz == 6 11280 && strcmp (note->namedata, "LINUX") == 0) 11281 return elfcore_grok_aarch_tls (abfd, note); 11282 else 11283 return true; 11284 11285 case NT_ARM_HW_BREAK: 11286 if (note->namesz == 6 11287 && strcmp (note->namedata, "LINUX") == 0) 11288 return elfcore_grok_aarch_hw_break (abfd, note); 11289 else 11290 return true; 11291 11292 case NT_ARM_HW_WATCH: 11293 if (note->namesz == 6 11294 && strcmp (note->namedata, "LINUX") == 0) 11295 return elfcore_grok_aarch_hw_watch (abfd, note); 11296 else 11297 return true; 11298 11299 case NT_ARM_SVE: 11300 if (note->namesz == 6 11301 && strcmp (note->namedata, "LINUX") == 0) 11302 return elfcore_grok_aarch_sve (abfd, note); 11303 else 11304 return true; 11305 11306 case NT_ARM_PAC_MASK: 11307 if (note->namesz == 6 11308 && strcmp (note->namedata, "LINUX") == 0) 11309 return elfcore_grok_aarch_pauth (abfd, note); 11310 else 11311 return true; 11312 11313 case NT_ARM_TAGGED_ADDR_CTRL: 11314 if (note->namesz == 6 11315 && strcmp (note->namedata, "LINUX") == 0) 11316 return elfcore_grok_aarch_mte (abfd, note); 11317 else 11318 return true; 11319 11320 case NT_ARM_SSVE: 11321 if (note->namesz == 6 11322 && strcmp (note->namedata, "LINUX") == 0) 11323 return elfcore_grok_aarch_ssve (abfd, note); 11324 else 11325 return true; 11326 11327 case NT_ARM_ZA: 11328 if (note->namesz == 6 11329 && strcmp (note->namedata, "LINUX") == 0) 11330 return elfcore_grok_aarch_za (abfd, note); 11331 else 11332 return true; 11333 11334 case NT_ARM_ZT: 11335 if (note->namesz == 6 11336 && strcmp (note->namedata, "LINUX") == 0) 11337 return elfcore_grok_aarch_zt (abfd, note); 11338 else 11339 return true; 11340 11341 case NT_GDB_TDESC: 11342 if (note->namesz == 4 11343 && strcmp (note->namedata, "GDB") == 0) 11344 return elfcore_grok_gdb_tdesc (abfd, note); 11345 else 11346 return true; 11347 11348 case NT_RISCV_CSR: 11349 if (note->namesz == 4 11350 && strcmp (note->namedata, "GDB") == 0) 11351 return elfcore_grok_riscv_csr (abfd, note); 11352 else 11353 return true; 11354 11355 case NT_LARCH_CPUCFG: 11356 if (note->namesz == 6 11357 && strcmp (note->namedata, "LINUX") == 0) 11358 return elfcore_grok_loongarch_cpucfg (abfd, note); 11359 else 11360 return true; 11361 11362 case NT_LARCH_LBT: 11363 if (note->namesz == 6 11364 && strcmp (note->namedata, "LINUX") == 0) 11365 return elfcore_grok_loongarch_lbt (abfd, note); 11366 else 11367 return true; 11368 11369 case NT_LARCH_LSX: 11370 if (note->namesz == 6 11371 && strcmp (note->namedata, "LINUX") == 0) 11372 return elfcore_grok_loongarch_lsx (abfd, note); 11373 else 11374 return true; 11375 11376 case NT_LARCH_LASX: 11377 if (note->namesz == 6 11378 && strcmp (note->namedata, "LINUX") == 0) 11379 return elfcore_grok_loongarch_lasx (abfd, note); 11380 else 11381 return true; 11382 11383 case NT_PRPSINFO: 11384 case NT_PSINFO: 11385 if (bed->elf_backend_grok_psinfo) 11386 if ((*bed->elf_backend_grok_psinfo) (abfd, note)) 11387 return true; 11388 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 11389 return elfcore_grok_psinfo (abfd, note); 11390 #else 11391 return true; 11392 #endif 11393 11394 case NT_AUXV: 11395 return elfcore_make_auxv_note_section (abfd, note, 0); 11396 11397 case NT_FILE: 11398 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", 11399 note); 11400 11401 case NT_SIGINFO: 11402 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", 11403 note); 11404 11405 } 11406 } 11407 11408 static bool 11409 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) 11410 { 11411 struct bfd_build_id* build_id; 11412 11413 if (note->descsz == 0) 11414 return false; 11415 11416 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); 11417 if (build_id == NULL) 11418 return false; 11419 11420 build_id->size = note->descsz; 11421 memcpy (build_id->data, note->descdata, note->descsz); 11422 abfd->build_id = build_id; 11423 11424 return true; 11425 } 11426 11427 static bool 11428 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) 11429 { 11430 switch (note->type) 11431 { 11432 default: 11433 return true; 11434 11435 case NT_GNU_PROPERTY_TYPE_0: 11436 return _bfd_elf_parse_gnu_properties (abfd, note); 11437 11438 case NT_GNU_BUILD_ID: 11439 return elfobj_grok_gnu_build_id (abfd, note); 11440 } 11441 } 11442 11443 static bool 11444 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) 11445 { 11446 struct sdt_note *cur = 11447 (struct sdt_note *) bfd_alloc (abfd, 11448 sizeof (struct sdt_note) + note->descsz); 11449 11450 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; 11451 cur->size = (bfd_size_type) note->descsz; 11452 memcpy (cur->data, note->descdata, note->descsz); 11453 11454 elf_tdata (abfd)->sdt_note_head = cur; 11455 11456 return true; 11457 } 11458 11459 static bool 11460 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) 11461 { 11462 switch (note->type) 11463 { 11464 case NT_STAPSDT: 11465 return elfobj_grok_stapsdt_note_1 (abfd, note); 11466 11467 default: 11468 return true; 11469 } 11470 } 11471 11472 static bool 11473 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) 11474 { 11475 size_t offset; 11476 11477 switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) 11478 { 11479 case ELFCLASS32: 11480 if (note->descsz < 108) 11481 return false; 11482 break; 11483 11484 case ELFCLASS64: 11485 if (note->descsz < 120) 11486 return false; 11487 break; 11488 11489 default: 11490 return false; 11491 } 11492 11493 /* Check for version 1 in pr_version. */ 11494 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) 11495 return false; 11496 11497 offset = 4; 11498 11499 /* Skip over pr_psinfosz. */ 11500 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) 11501 offset += 4; 11502 else 11503 { 11504 offset += 4; /* Padding before pr_psinfosz. */ 11505 offset += 8; 11506 } 11507 11508 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ 11509 elf_tdata (abfd)->core->program 11510 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); 11511 offset += 17; 11512 11513 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ 11514 elf_tdata (abfd)->core->command 11515 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); 11516 offset += 81; 11517 11518 /* Padding before pr_pid. */ 11519 offset += 2; 11520 11521 /* The pr_pid field was added in version "1a". */ 11522 if (note->descsz < offset + 4) 11523 return true; 11524 11525 elf_tdata (abfd)->core->pid 11526 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11527 11528 return true; 11529 } 11530 11531 static bool 11532 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) 11533 { 11534 size_t offset; 11535 size_t size; 11536 size_t min_size; 11537 11538 /* Compute offset of pr_getregsz, skipping over pr_statussz. 11539 Also compute minimum size of this note. */ 11540 switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) 11541 { 11542 case ELFCLASS32: 11543 offset = 4 + 4; 11544 min_size = offset + (4 * 2) + 4 + 4 + 4; 11545 break; 11546 11547 case ELFCLASS64: 11548 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */ 11549 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4; 11550 break; 11551 11552 default: 11553 return false; 11554 } 11555 11556 if (note->descsz < min_size) 11557 return false; 11558 11559 /* Check for version 1 in pr_version. */ 11560 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) 11561 return false; 11562 11563 /* Extract size of pr_reg from pr_gregsetsz. */ 11564 /* Skip over pr_gregsetsz and pr_fpregsetsz. */ 11565 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) 11566 { 11567 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11568 offset += 4 * 2; 11569 } 11570 else 11571 { 11572 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); 11573 offset += 8 * 2; 11574 } 11575 11576 /* Skip over pr_osreldate. */ 11577 offset += 4; 11578 11579 /* Read signal from pr_cursig. */ 11580 if (elf_tdata (abfd)->core->signal == 0) 11581 elf_tdata (abfd)->core->signal 11582 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11583 offset += 4; 11584 11585 /* Read TID from pr_pid. */ 11586 elf_tdata (abfd)->core->lwpid 11587 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11588 offset += 4; 11589 11590 /* Padding before pr_reg. */ 11591 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) 11592 offset += 4; 11593 11594 /* Make sure that there is enough data remaining in the note. */ 11595 if ((note->descsz - offset) < size) 11596 return false; 11597 11598 /* Make a ".reg/999" section and a ".reg" section. */ 11599 return _bfd_elfcore_make_pseudosection (abfd, ".reg", 11600 size, note->descpos + offset); 11601 } 11602 11603 static bool 11604 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) 11605 { 11606 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11607 11608 switch (note->type) 11609 { 11610 case NT_PRSTATUS: 11611 if (bed->elf_backend_grok_freebsd_prstatus) 11612 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note)) 11613 return true; 11614 return elfcore_grok_freebsd_prstatus (abfd, note); 11615 11616 case NT_FPREGSET: 11617 return elfcore_grok_prfpreg (abfd, note); 11618 11619 case NT_PRPSINFO: 11620 return elfcore_grok_freebsd_psinfo (abfd, note); 11621 11622 case NT_FREEBSD_THRMISC: 11623 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); 11624 11625 case NT_FREEBSD_PROCSTAT_PROC: 11626 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc", 11627 note); 11628 11629 case NT_FREEBSD_PROCSTAT_FILES: 11630 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files", 11631 note); 11632 11633 case NT_FREEBSD_PROCSTAT_VMMAP: 11634 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap", 11635 note); 11636 11637 case NT_FREEBSD_PROCSTAT_AUXV: 11638 return elfcore_make_auxv_note_section (abfd, note, 4); 11639 11640 case NT_FREEBSD_X86_SEGBASES: 11641 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note); 11642 11643 case NT_X86_XSTATE: 11644 return elfcore_grok_xstatereg (abfd, note); 11645 11646 case NT_FREEBSD_PTLWPINFO: 11647 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo", 11648 note); 11649 11650 case NT_ARM_TLS: 11651 return elfcore_grok_aarch_tls (abfd, note); 11652 11653 case NT_ARM_VFP: 11654 return elfcore_grok_arm_vfp (abfd, note); 11655 11656 default: 11657 return true; 11658 } 11659 } 11660 11661 static bool 11662 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) 11663 { 11664 char *cp; 11665 11666 cp = strchr (note->namedata, '@'); 11667 if (cp != NULL) 11668 { 11669 *lwpidp = atoi(cp + 1); 11670 return true; 11671 } 11672 return false; 11673 } 11674 11675 static bool 11676 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) 11677 { 11678 if (note->descsz <= 0x7c + 31) 11679 return false; 11680 11681 /* Signal number at offset 0x08. */ 11682 elf_tdata (abfd)->core->signal 11683 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); 11684 11685 /* Process ID at offset 0x50. */ 11686 elf_tdata (abfd)->core->pid 11687 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); 11688 11689 /* Command name at 0x7c (max 32 bytes, including nul). */ 11690 elf_tdata (abfd)->core->command 11691 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); 11692 11693 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", 11694 note); 11695 } 11696 11697 static bool 11698 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) 11699 { 11700 int lwp; 11701 11702 if (elfcore_netbsd_get_lwpid (note, &lwp)) 11703 elf_tdata (abfd)->core->lwpid = lwp; 11704 11705 switch (note->type) 11706 { 11707 case NT_NETBSDCORE_PROCINFO: 11708 /* NetBSD-specific core "procinfo". Note that we expect to 11709 find this note before any of the others, which is fine, 11710 since the kernel writes this note out first when it 11711 creates a core file. */ 11712 return elfcore_grok_netbsd_procinfo (abfd, note); 11713 case NT_NETBSDCORE_AUXV: 11714 /* NetBSD-specific Elf Auxiliary Vector data. */ 11715 return elfcore_make_auxv_note_section (abfd, note, 0); 11716 case NT_NETBSDCORE_LWPSTATUS: 11717 return elfcore_make_note_pseudosection (abfd, 11718 ".note.netbsdcore.lwpstatus", 11719 note); 11720 default: 11721 break; 11722 } 11723 11724 if (note->type == NT_NETBSDCORE_AUXV) 11725 { 11726 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", 11727 SEC_HAS_CONTENTS); 11728 11729 if (sect == NULL) 11730 return false; 11731 sect->size = note->descsz; 11732 sect->filepos = note->descpos; 11733 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 11734 11735 return true; 11736 } 11737 11738 /* As of March 2020 there are no other machine-independent notes 11739 defined for NetBSD core files. If the note type is less 11740 than the start of the machine-dependent note types, we don't 11741 understand it. */ 11742 11743 if (note->type < NT_NETBSDCORE_FIRSTMACH) 11744 return true; 11745 11746 11747 switch (bfd_get_arch (abfd)) 11748 { 11749 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and 11750 PT_GETFPREGS == mach+2. */ 11751 11752 case bfd_arch_aarch64: 11753 case bfd_arch_alpha: 11754 case bfd_arch_riscv: 11755 case bfd_arch_sparc: 11756 switch (note->type) 11757 { 11758 case NT_NETBSDCORE_FIRSTMACH+0: 11759 return elfcore_make_note_pseudosection (abfd, ".reg", note); 11760 11761 case NT_NETBSDCORE_FIRSTMACH+2: 11762 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 11763 11764 default: 11765 return true; 11766 } 11767 11768 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5. 11769 There's also old PT___GETREGS40 == mach + 1 for old reg 11770 structure which lacks GBR. */ 11771 11772 case bfd_arch_sh: 11773 switch (note->type) 11774 { 11775 case NT_NETBSDCORE_FIRSTMACH+3: 11776 return elfcore_make_note_pseudosection (abfd, ".reg", note); 11777 11778 case NT_NETBSDCORE_FIRSTMACH+5: 11779 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 11780 11781 default: 11782 return true; 11783 } 11784 11785 /* On all other arch's, PT_GETREGS == mach+1 and 11786 PT_GETFPREGS == mach+3. */ 11787 11788 default: 11789 switch (note->type) 11790 { 11791 case NT_NETBSDCORE_FIRSTMACH+1: 11792 return elfcore_make_note_pseudosection (abfd, ".reg", note); 11793 11794 case NT_NETBSDCORE_FIRSTMACH+3: 11795 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 11796 11797 default: 11798 return true; 11799 } 11800 } 11801 /* NOTREACHED */ 11802 } 11803 11804 static bool 11805 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) 11806 { 11807 if (note->descsz <= 0x48 + 31) 11808 return false; 11809 11810 /* Signal number at offset 0x08. */ 11811 elf_tdata (abfd)->core->signal 11812 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); 11813 11814 /* Process ID at offset 0x20. */ 11815 elf_tdata (abfd)->core->pid 11816 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); 11817 11818 /* Command name at 0x48 (max 32 bytes, including nul). */ 11819 elf_tdata (abfd)->core->command 11820 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); 11821 11822 return true; 11823 } 11824 11825 /* Processes Solaris's process status note. 11826 sig_off ~ offsetof(prstatus_t, pr_cursig) 11827 pid_off ~ offsetof(prstatus_t, pr_pid) 11828 lwpid_off ~ offsetof(prstatus_t, pr_who) 11829 gregset_size ~ sizeof(gregset_t) 11830 gregset_offset ~ offsetof(prstatus_t, pr_reg) */ 11831 11832 static bool 11833 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off, 11834 int pid_off, int lwpid_off, size_t gregset_size, 11835 size_t gregset_offset) 11836 { 11837 asection *sect = NULL; 11838 elf_tdata (abfd)->core->signal 11839 = bfd_get_16 (abfd, note->descdata + sig_off); 11840 elf_tdata (abfd)->core->pid 11841 = bfd_get_32 (abfd, note->descdata + pid_off); 11842 elf_tdata (abfd)->core->lwpid 11843 = bfd_get_32 (abfd, note->descdata + lwpid_off); 11844 11845 sect = bfd_get_section_by_name (abfd, ".reg"); 11846 if (sect != NULL) 11847 sect->size = gregset_size; 11848 11849 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size, 11850 note->descpos + gregset_offset); 11851 } 11852 11853 /* Gets program and arguments from a core. 11854 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname) 11855 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */ 11856 11857 static bool 11858 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note, 11859 int prog_off, int comm_off) 11860 { 11861 elf_tdata (abfd)->core->program 11862 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16); 11863 elf_tdata (abfd)->core->command 11864 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80); 11865 11866 return true; 11867 } 11868 11869 /* Processes Solaris's LWP status note. 11870 gregset_size ~ sizeof(gregset_t) 11871 gregset_off ~ offsetof(lwpstatus_t, pr_reg) 11872 fpregset_size ~ sizeof(fpregset_t) 11873 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */ 11874 11875 static bool 11876 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note, 11877 size_t gregset_size, int gregset_off, 11878 size_t fpregset_size, int fpregset_off) 11879 { 11880 asection *sect = NULL; 11881 char reg2_section_name[16] = { 0 }; 11882 11883 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2", 11884 elf_tdata (abfd)->core->lwpid); 11885 11886 /* offsetof(lwpstatus_t, pr_lwpid) */ 11887 elf_tdata (abfd)->core->lwpid 11888 = bfd_get_32 (abfd, note->descdata + 4); 11889 /* offsetof(lwpstatus_t, pr_cursig) */ 11890 elf_tdata (abfd)->core->signal 11891 = bfd_get_16 (abfd, note->descdata + 12); 11892 11893 sect = bfd_get_section_by_name (abfd, ".reg"); 11894 if (sect != NULL) 11895 sect->size = gregset_size; 11896 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size, 11897 note->descpos + gregset_off)) 11898 return false; 11899 11900 sect = bfd_get_section_by_name (abfd, reg2_section_name); 11901 if (sect != NULL) 11902 { 11903 sect->size = fpregset_size; 11904 sect->filepos = note->descpos + fpregset_off; 11905 sect->alignment_power = 2; 11906 } 11907 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size, 11908 note->descpos + fpregset_off)) 11909 return false; 11910 11911 return true; 11912 } 11913 11914 static bool 11915 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note) 11916 { 11917 if (note == NULL) 11918 return false; 11919 11920 /* core files are identified as 32- or 64-bit, SPARC or x86, 11921 by the size of the descsz which matches the sizeof() 11922 the type appropriate for that note type (e.g., prstatus_t for 11923 SOLARIS_NT_PRSTATUS) for the corresponding architecture 11924 on Solaris. The core file bitness may differ from the bitness of 11925 gdb itself, so fixed values are used instead of sizeof(). 11926 Appropriate fixed offsets are also used to obtain data from 11927 the note. */ 11928 11929 switch ((int) note->type) 11930 { 11931 case SOLARIS_NT_PRSTATUS: 11932 switch (note->descsz) 11933 { 11934 case 508: /* sizeof(prstatus_t) SPARC 32-bit */ 11935 return elfcore_grok_solaris_prstatus(abfd, note, 11936 136, 216, 308, 152, 356); 11937 case 904: /* sizeof(prstatus_t) SPARC 64-bit */ 11938 return elfcore_grok_solaris_prstatus(abfd, note, 11939 264, 360, 520, 304, 600); 11940 case 432: /* sizeof(prstatus_t) Intel 32-bit */ 11941 return elfcore_grok_solaris_prstatus(abfd, note, 11942 136, 216, 308, 76, 356); 11943 case 824: /* sizeof(prstatus_t) Intel 64-bit */ 11944 return elfcore_grok_solaris_prstatus(abfd, note, 11945 264, 360, 520, 224, 600); 11946 default: 11947 return true; 11948 } 11949 11950 case SOLARIS_NT_PSINFO: 11951 case SOLARIS_NT_PRPSINFO: 11952 switch (note->descsz) 11953 { 11954 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */ 11955 return elfcore_grok_solaris_info(abfd, note, 84, 100); 11956 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */ 11957 return elfcore_grok_solaris_info(abfd, note, 120, 136); 11958 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */ 11959 return elfcore_grok_solaris_info(abfd, note, 88, 104); 11960 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */ 11961 return elfcore_grok_solaris_info(abfd, note, 136, 152); 11962 default: 11963 return true; 11964 } 11965 11966 case SOLARIS_NT_LWPSTATUS: 11967 switch (note->descsz) 11968 { 11969 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */ 11970 return elfcore_grok_solaris_lwpstatus(abfd, note, 11971 152, 344, 400, 496); 11972 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */ 11973 return elfcore_grok_solaris_lwpstatus(abfd, note, 11974 304, 544, 544, 848); 11975 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */ 11976 return elfcore_grok_solaris_lwpstatus(abfd, note, 11977 76, 344, 380, 420); 11978 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */ 11979 return elfcore_grok_solaris_lwpstatus(abfd, note, 11980 224, 544, 528, 768); 11981 default: 11982 return true; 11983 } 11984 11985 case SOLARIS_NT_LWPSINFO: 11986 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */ 11987 if (note->descsz == 128 || note->descsz == 152) 11988 elf_tdata (abfd)->core->lwpid = 11989 bfd_get_32 (abfd, note->descdata + 4); 11990 break; 11991 11992 default: 11993 break; 11994 } 11995 11996 return true; 11997 } 11998 11999 /* For name starting with "CORE" this may be either a Solaris 12000 core file or a gdb-generated core file. Do Solaris-specific 12001 processing on selected note types first with 12002 elfcore_grok_solaris_note(), then process the note 12003 in elfcore_grok_note(). */ 12004 12005 static bool 12006 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note) 12007 { 12008 if (!elfcore_grok_solaris_note_impl (abfd, note)) 12009 return false; 12010 12011 return elfcore_grok_note (abfd, note); 12012 } 12013 12014 static bool 12015 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) 12016 { 12017 if (note->type == NT_OPENBSD_PROCINFO) 12018 return elfcore_grok_openbsd_procinfo (abfd, note); 12019 12020 if (note->type == NT_OPENBSD_REGS) 12021 return elfcore_make_note_pseudosection (abfd, ".reg", note); 12022 12023 if (note->type == NT_OPENBSD_FPREGS) 12024 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 12025 12026 if (note->type == NT_OPENBSD_XFPREGS) 12027 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); 12028 12029 if (note->type == NT_OPENBSD_AUXV) 12030 return elfcore_make_auxv_note_section (abfd, note, 0); 12031 12032 if (note->type == NT_OPENBSD_WCOOKIE) 12033 { 12034 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", 12035 SEC_HAS_CONTENTS); 12036 12037 if (sect == NULL) 12038 return false; 12039 sect->size = note->descsz; 12040 sect->filepos = note->descpos; 12041 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 12042 12043 return true; 12044 } 12045 12046 return true; 12047 } 12048 12049 static bool 12050 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) 12051 { 12052 void *ddata = note->descdata; 12053 char buf[100]; 12054 char *name; 12055 asection *sect; 12056 short sig; 12057 unsigned flags; 12058 12059 if (note->descsz < 16) 12060 return false; 12061 12062 /* nto_procfs_status 'pid' field is at offset 0. */ 12063 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); 12064 12065 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ 12066 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); 12067 12068 /* nto_procfs_status 'flags' field is at offset 8. */ 12069 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); 12070 12071 /* nto_procfs_status 'what' field is at offset 14. */ 12072 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) 12073 { 12074 elf_tdata (abfd)->core->signal = sig; 12075 elf_tdata (abfd)->core->lwpid = *tid; 12076 } 12077 12078 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores 12079 do not come from signals so we make sure we set the current 12080 thread just in case. */ 12081 if (flags & 0x00000080) 12082 elf_tdata (abfd)->core->lwpid = *tid; 12083 12084 /* Make a ".qnx_core_status/%d" section. */ 12085 sprintf (buf, ".qnx_core_status/%ld", *tid); 12086 12087 name = (char *) bfd_alloc (abfd, strlen (buf) + 1); 12088 if (name == NULL) 12089 return false; 12090 strcpy (name, buf); 12091 12092 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 12093 if (sect == NULL) 12094 return false; 12095 12096 sect->size = note->descsz; 12097 sect->filepos = note->descpos; 12098 sect->alignment_power = 2; 12099 12100 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); 12101 } 12102 12103 static bool 12104 elfcore_grok_nto_regs (bfd *abfd, 12105 Elf_Internal_Note *note, 12106 long tid, 12107 char *base) 12108 { 12109 char buf[100]; 12110 char *name; 12111 asection *sect; 12112 12113 /* Make a "(base)/%d" section. */ 12114 sprintf (buf, "%s/%ld", base, tid); 12115 12116 name = (char *) bfd_alloc (abfd, strlen (buf) + 1); 12117 if (name == NULL) 12118 return false; 12119 strcpy (name, buf); 12120 12121 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 12122 if (sect == NULL) 12123 return false; 12124 12125 sect->size = note->descsz; 12126 sect->filepos = note->descpos; 12127 sect->alignment_power = 2; 12128 12129 /* This is the current thread. */ 12130 if (elf_tdata (abfd)->core->lwpid == tid) 12131 return elfcore_maybe_make_sect (abfd, base, sect); 12132 12133 return true; 12134 } 12135 12136 static bool 12137 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) 12138 { 12139 /* Every GREG section has a STATUS section before it. Store the 12140 tid from the previous call to pass down to the next gregs 12141 function. */ 12142 static long tid = 1; 12143 12144 switch (note->type) 12145 { 12146 case QNT_CORE_INFO: 12147 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); 12148 case QNT_CORE_STATUS: 12149 return elfcore_grok_nto_status (abfd, note, &tid); 12150 case QNT_CORE_GREG: 12151 return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); 12152 case QNT_CORE_FPREG: 12153 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); 12154 default: 12155 return true; 12156 } 12157 } 12158 12159 static bool 12160 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) 12161 { 12162 char *name; 12163 asection *sect; 12164 size_t len; 12165 12166 /* Use note name as section name. */ 12167 len = note->namesz; 12168 name = (char *) bfd_alloc (abfd, len); 12169 if (name == NULL) 12170 return false; 12171 memcpy (name, note->namedata, len); 12172 name[len - 1] = '\0'; 12173 12174 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 12175 if (sect == NULL) 12176 return false; 12177 12178 sect->size = note->descsz; 12179 sect->filepos = note->descpos; 12180 sect->alignment_power = 1; 12181 12182 return true; 12183 } 12184 12185 /* Function: elfcore_write_note 12186 12187 Inputs: 12188 buffer to hold note, and current size of buffer 12189 name of note 12190 type of note 12191 data for note 12192 size of data for note 12193 12194 Writes note to end of buffer. ELF64 notes are written exactly as 12195 for ELF32, despite the current (as of 2006) ELF gabi specifying 12196 that they ought to have 8-byte namesz and descsz field, and have 12197 8-byte alignment. Other writers, eg. Linux kernel, do the same. 12198 12199 Return: 12200 Pointer to realloc'd buffer, *BUFSIZ updated. */ 12201 12202 char * 12203 elfcore_write_note (bfd *abfd, 12204 char *buf, 12205 int *bufsiz, 12206 const char *name, 12207 int type, 12208 const void *input, 12209 int size) 12210 { 12211 Elf_External_Note *xnp; 12212 size_t namesz; 12213 size_t newspace; 12214 char *dest; 12215 12216 namesz = 0; 12217 if (name != NULL) 12218 namesz = strlen (name) + 1; 12219 12220 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); 12221 12222 buf = (char *) realloc (buf, *bufsiz + newspace); 12223 if (buf == NULL) 12224 return buf; 12225 dest = buf + *bufsiz; 12226 *bufsiz += newspace; 12227 xnp = (Elf_External_Note *) dest; 12228 H_PUT_32 (abfd, namesz, xnp->namesz); 12229 H_PUT_32 (abfd, size, xnp->descsz); 12230 H_PUT_32 (abfd, type, xnp->type); 12231 dest = xnp->name; 12232 if (name != NULL) 12233 { 12234 memcpy (dest, name, namesz); 12235 dest += namesz; 12236 while (namesz & 3) 12237 { 12238 *dest++ = '\0'; 12239 ++namesz; 12240 } 12241 } 12242 memcpy (dest, input, size); 12243 dest += size; 12244 while (size & 3) 12245 { 12246 *dest++ = '\0'; 12247 ++size; 12248 } 12249 return buf; 12250 } 12251 12252 /* gcc-8 warns (*) on all the strncpy calls in this function about 12253 possible string truncation. The "truncation" is not a bug. We 12254 have an external representation of structs with fields that are not 12255 necessarily NULL terminated and corresponding internal 12256 representation fields that are one larger so that they can always 12257 be NULL terminated. 12258 gcc versions between 4.2 and 4.6 do not allow pragma control of 12259 diagnostics inside functions, giving a hard error if you try to use 12260 the finer control available with later versions. 12261 gcc prior to 4.2 warns about diagnostic push and pop. 12262 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown, 12263 unless you also add #pragma GCC diagnostic ignored "-Wpragma". 12264 (*) Depending on your system header files! */ 12265 #if GCC_VERSION >= 8000 12266 # pragma GCC diagnostic push 12267 # pragma GCC diagnostic ignored "-Wstringop-truncation" 12268 #endif 12269 char * 12270 elfcore_write_prpsinfo (bfd *abfd, 12271 char *buf, 12272 int *bufsiz, 12273 const char *fname, 12274 const char *psargs) 12275 { 12276 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12277 12278 if (bed->elf_backend_write_core_note != NULL) 12279 { 12280 char *ret; 12281 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, 12282 NT_PRPSINFO, fname, psargs); 12283 if (ret != NULL) 12284 return ret; 12285 } 12286 12287 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 12288 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) 12289 if (bed->s->elfclass == ELFCLASS32) 12290 { 12291 # if defined (HAVE_PSINFO32_T) 12292 psinfo32_t data; 12293 int note_type = NT_PSINFO; 12294 # else 12295 prpsinfo32_t data; 12296 int note_type = NT_PRPSINFO; 12297 # endif 12298 12299 memset (&data, 0, sizeof (data)); 12300 strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); 12301 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); 12302 return elfcore_write_note (abfd, buf, bufsiz, 12303 "CORE", note_type, &data, sizeof (data)); 12304 } 12305 else 12306 # endif 12307 { 12308 # if defined (HAVE_PSINFO_T) 12309 psinfo_t data; 12310 int note_type = NT_PSINFO; 12311 # else 12312 prpsinfo_t data; 12313 int note_type = NT_PRPSINFO; 12314 # endif 12315 12316 memset (&data, 0, sizeof (data)); 12317 strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); 12318 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); 12319 return elfcore_write_note (abfd, buf, bufsiz, 12320 "CORE", note_type, &data, sizeof (data)); 12321 } 12322 #endif /* PSINFO_T or PRPSINFO_T */ 12323 12324 free (buf); 12325 return NULL; 12326 } 12327 #if GCC_VERSION >= 8000 12328 # pragma GCC diagnostic pop 12329 #endif 12330 12331 char * 12332 elfcore_write_linux_prpsinfo32 12333 (bfd *abfd, char *buf, int *bufsiz, 12334 const struct elf_internal_linux_prpsinfo *prpsinfo) 12335 { 12336 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16) 12337 { 12338 struct elf_external_linux_prpsinfo32_ugid16 data; 12339 12340 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data); 12341 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, 12342 &data, sizeof (data)); 12343 } 12344 else 12345 { 12346 struct elf_external_linux_prpsinfo32_ugid32 data; 12347 12348 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data); 12349 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, 12350 &data, sizeof (data)); 12351 } 12352 } 12353 12354 char * 12355 elfcore_write_linux_prpsinfo64 12356 (bfd *abfd, char *buf, int *bufsiz, 12357 const struct elf_internal_linux_prpsinfo *prpsinfo) 12358 { 12359 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16) 12360 { 12361 struct elf_external_linux_prpsinfo64_ugid16 data; 12362 12363 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data); 12364 return elfcore_write_note (abfd, buf, bufsiz, 12365 "CORE", NT_PRPSINFO, &data, sizeof (data)); 12366 } 12367 else 12368 { 12369 struct elf_external_linux_prpsinfo64_ugid32 data; 12370 12371 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data); 12372 return elfcore_write_note (abfd, buf, bufsiz, 12373 "CORE", NT_PRPSINFO, &data, sizeof (data)); 12374 } 12375 } 12376 12377 char * 12378 elfcore_write_prstatus (bfd *abfd, 12379 char *buf, 12380 int *bufsiz, 12381 long pid, 12382 int cursig, 12383 const void *gregs) 12384 { 12385 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12386 12387 if (bed->elf_backend_write_core_note != NULL) 12388 { 12389 char *ret; 12390 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, 12391 NT_PRSTATUS, 12392 pid, cursig, gregs); 12393 if (ret != NULL) 12394 return ret; 12395 } 12396 12397 #if defined (HAVE_PRSTATUS_T) 12398 #if defined (HAVE_PRSTATUS32_T) 12399 if (bed->s->elfclass == ELFCLASS32) 12400 { 12401 prstatus32_t prstat; 12402 12403 memset (&prstat, 0, sizeof (prstat)); 12404 prstat.pr_pid = pid; 12405 prstat.pr_cursig = cursig; 12406 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); 12407 return elfcore_write_note (abfd, buf, bufsiz, "CORE", 12408 NT_PRSTATUS, &prstat, sizeof (prstat)); 12409 } 12410 else 12411 #endif 12412 { 12413 prstatus_t prstat; 12414 12415 memset (&prstat, 0, sizeof (prstat)); 12416 prstat.pr_pid = pid; 12417 prstat.pr_cursig = cursig; 12418 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); 12419 return elfcore_write_note (abfd, buf, bufsiz, "CORE", 12420 NT_PRSTATUS, &prstat, sizeof (prstat)); 12421 } 12422 #endif /* HAVE_PRSTATUS_T */ 12423 12424 free (buf); 12425 return NULL; 12426 } 12427 12428 #if defined (HAVE_LWPSTATUS_T) 12429 char * 12430 elfcore_write_lwpstatus (bfd *abfd, 12431 char *buf, 12432 int *bufsiz, 12433 long pid, 12434 int cursig, 12435 const void *gregs) 12436 { 12437 lwpstatus_t lwpstat; 12438 const char *note_name = "CORE"; 12439 12440 memset (&lwpstat, 0, sizeof (lwpstat)); 12441 lwpstat.pr_lwpid = pid >> 16; 12442 lwpstat.pr_cursig = cursig; 12443 #if defined (HAVE_LWPSTATUS_T_PR_REG) 12444 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); 12445 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 12446 #if !defined(gregs) 12447 memcpy (lwpstat.pr_context.uc_mcontext.gregs, 12448 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); 12449 #else 12450 memcpy (lwpstat.pr_context.uc_mcontext.__gregs, 12451 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); 12452 #endif 12453 #endif 12454 return elfcore_write_note (abfd, buf, bufsiz, note_name, 12455 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); 12456 } 12457 #endif /* HAVE_LWPSTATUS_T */ 12458 12459 #if defined (HAVE_PSTATUS_T) 12460 char * 12461 elfcore_write_pstatus (bfd *abfd, 12462 char *buf, 12463 int *bufsiz, 12464 long pid, 12465 int cursig ATTRIBUTE_UNUSED, 12466 const void *gregs ATTRIBUTE_UNUSED) 12467 { 12468 const char *note_name = "CORE"; 12469 #if defined (HAVE_PSTATUS32_T) 12470 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12471 12472 if (bed->s->elfclass == ELFCLASS32) 12473 { 12474 pstatus32_t pstat; 12475 12476 memset (&pstat, 0, sizeof (pstat)); 12477 pstat.pr_pid = pid & 0xffff; 12478 buf = elfcore_write_note (abfd, buf, bufsiz, note_name, 12479 NT_PSTATUS, &pstat, sizeof (pstat)); 12480 return buf; 12481 } 12482 else 12483 #endif 12484 { 12485 pstatus_t pstat; 12486 12487 memset (&pstat, 0, sizeof (pstat)); 12488 pstat.pr_pid = pid & 0xffff; 12489 buf = elfcore_write_note (abfd, buf, bufsiz, note_name, 12490 NT_PSTATUS, &pstat, sizeof (pstat)); 12491 return buf; 12492 } 12493 } 12494 #endif /* HAVE_PSTATUS_T */ 12495 12496 char * 12497 elfcore_write_prfpreg (bfd *abfd, 12498 char *buf, 12499 int *bufsiz, 12500 const void *fpregs, 12501 int size) 12502 { 12503 const char *note_name = "CORE"; 12504 return elfcore_write_note (abfd, buf, bufsiz, 12505 note_name, NT_FPREGSET, fpregs, size); 12506 } 12507 12508 char * 12509 elfcore_write_prxfpreg (bfd *abfd, 12510 char *buf, 12511 int *bufsiz, 12512 const void *xfpregs, 12513 int size) 12514 { 12515 char *note_name = "LINUX"; 12516 return elfcore_write_note (abfd, buf, bufsiz, 12517 note_name, NT_PRXFPREG, xfpregs, size); 12518 } 12519 12520 char * 12521 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, 12522 const void *xfpregs, int size) 12523 { 12524 char *note_name; 12525 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) 12526 note_name = "FreeBSD"; 12527 else 12528 note_name = "LINUX"; 12529 return elfcore_write_note (abfd, buf, bufsiz, 12530 note_name, NT_X86_XSTATE, xfpregs, size); 12531 } 12532 12533 char * 12534 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz, 12535 const void *regs, int size) 12536 { 12537 char *note_name = "FreeBSD"; 12538 return elfcore_write_note (abfd, buf, bufsiz, 12539 note_name, NT_FREEBSD_X86_SEGBASES, regs, size); 12540 } 12541 12542 char * 12543 elfcore_write_ppc_vmx (bfd *abfd, 12544 char *buf, 12545 int *bufsiz, 12546 const void *ppc_vmx, 12547 int size) 12548 { 12549 char *note_name = "LINUX"; 12550 return elfcore_write_note (abfd, buf, bufsiz, 12551 note_name, NT_PPC_VMX, ppc_vmx, size); 12552 } 12553 12554 char * 12555 elfcore_write_ppc_vsx (bfd *abfd, 12556 char *buf, 12557 int *bufsiz, 12558 const void *ppc_vsx, 12559 int size) 12560 { 12561 char *note_name = "LINUX"; 12562 return elfcore_write_note (abfd, buf, bufsiz, 12563 note_name, NT_PPC_VSX, ppc_vsx, size); 12564 } 12565 12566 char * 12567 elfcore_write_ppc_tar (bfd *abfd, 12568 char *buf, 12569 int *bufsiz, 12570 const void *ppc_tar, 12571 int size) 12572 { 12573 char *note_name = "LINUX"; 12574 return elfcore_write_note (abfd, buf, bufsiz, 12575 note_name, NT_PPC_TAR, ppc_tar, size); 12576 } 12577 12578 char * 12579 elfcore_write_ppc_ppr (bfd *abfd, 12580 char *buf, 12581 int *bufsiz, 12582 const void *ppc_ppr, 12583 int size) 12584 { 12585 char *note_name = "LINUX"; 12586 return elfcore_write_note (abfd, buf, bufsiz, 12587 note_name, NT_PPC_PPR, ppc_ppr, size); 12588 } 12589 12590 char * 12591 elfcore_write_ppc_dscr (bfd *abfd, 12592 char *buf, 12593 int *bufsiz, 12594 const void *ppc_dscr, 12595 int size) 12596 { 12597 char *note_name = "LINUX"; 12598 return elfcore_write_note (abfd, buf, bufsiz, 12599 note_name, NT_PPC_DSCR, ppc_dscr, size); 12600 } 12601 12602 char * 12603 elfcore_write_ppc_ebb (bfd *abfd, 12604 char *buf, 12605 int *bufsiz, 12606 const void *ppc_ebb, 12607 int size) 12608 { 12609 char *note_name = "LINUX"; 12610 return elfcore_write_note (abfd, buf, bufsiz, 12611 note_name, NT_PPC_EBB, ppc_ebb, size); 12612 } 12613 12614 char * 12615 elfcore_write_ppc_pmu (bfd *abfd, 12616 char *buf, 12617 int *bufsiz, 12618 const void *ppc_pmu, 12619 int size) 12620 { 12621 char *note_name = "LINUX"; 12622 return elfcore_write_note (abfd, buf, bufsiz, 12623 note_name, NT_PPC_PMU, ppc_pmu, size); 12624 } 12625 12626 char * 12627 elfcore_write_ppc_tm_cgpr (bfd *abfd, 12628 char *buf, 12629 int *bufsiz, 12630 const void *ppc_tm_cgpr, 12631 int size) 12632 { 12633 char *note_name = "LINUX"; 12634 return elfcore_write_note (abfd, buf, bufsiz, 12635 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size); 12636 } 12637 12638 char * 12639 elfcore_write_ppc_tm_cfpr (bfd *abfd, 12640 char *buf, 12641 int *bufsiz, 12642 const void *ppc_tm_cfpr, 12643 int size) 12644 { 12645 char *note_name = "LINUX"; 12646 return elfcore_write_note (abfd, buf, bufsiz, 12647 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size); 12648 } 12649 12650 char * 12651 elfcore_write_ppc_tm_cvmx (bfd *abfd, 12652 char *buf, 12653 int *bufsiz, 12654 const void *ppc_tm_cvmx, 12655 int size) 12656 { 12657 char *note_name = "LINUX"; 12658 return elfcore_write_note (abfd, buf, bufsiz, 12659 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size); 12660 } 12661 12662 char * 12663 elfcore_write_ppc_tm_cvsx (bfd *abfd, 12664 char *buf, 12665 int *bufsiz, 12666 const void *ppc_tm_cvsx, 12667 int size) 12668 { 12669 char *note_name = "LINUX"; 12670 return elfcore_write_note (abfd, buf, bufsiz, 12671 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size); 12672 } 12673 12674 char * 12675 elfcore_write_ppc_tm_spr (bfd *abfd, 12676 char *buf, 12677 int *bufsiz, 12678 const void *ppc_tm_spr, 12679 int size) 12680 { 12681 char *note_name = "LINUX"; 12682 return elfcore_write_note (abfd, buf, bufsiz, 12683 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size); 12684 } 12685 12686 char * 12687 elfcore_write_ppc_tm_ctar (bfd *abfd, 12688 char *buf, 12689 int *bufsiz, 12690 const void *ppc_tm_ctar, 12691 int size) 12692 { 12693 char *note_name = "LINUX"; 12694 return elfcore_write_note (abfd, buf, bufsiz, 12695 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size); 12696 } 12697 12698 char * 12699 elfcore_write_ppc_tm_cppr (bfd *abfd, 12700 char *buf, 12701 int *bufsiz, 12702 const void *ppc_tm_cppr, 12703 int size) 12704 { 12705 char *note_name = "LINUX"; 12706 return elfcore_write_note (abfd, buf, bufsiz, 12707 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size); 12708 } 12709 12710 char * 12711 elfcore_write_ppc_tm_cdscr (bfd *abfd, 12712 char *buf, 12713 int *bufsiz, 12714 const void *ppc_tm_cdscr, 12715 int size) 12716 { 12717 char *note_name = "LINUX"; 12718 return elfcore_write_note (abfd, buf, bufsiz, 12719 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size); 12720 } 12721 12722 static char * 12723 elfcore_write_s390_high_gprs (bfd *abfd, 12724 char *buf, 12725 int *bufsiz, 12726 const void *s390_high_gprs, 12727 int size) 12728 { 12729 char *note_name = "LINUX"; 12730 return elfcore_write_note (abfd, buf, bufsiz, 12731 note_name, NT_S390_HIGH_GPRS, 12732 s390_high_gprs, size); 12733 } 12734 12735 char * 12736 elfcore_write_s390_timer (bfd *abfd, 12737 char *buf, 12738 int *bufsiz, 12739 const void *s390_timer, 12740 int size) 12741 { 12742 char *note_name = "LINUX"; 12743 return elfcore_write_note (abfd, buf, bufsiz, 12744 note_name, NT_S390_TIMER, s390_timer, size); 12745 } 12746 12747 char * 12748 elfcore_write_s390_todcmp (bfd *abfd, 12749 char *buf, 12750 int *bufsiz, 12751 const void *s390_todcmp, 12752 int size) 12753 { 12754 char *note_name = "LINUX"; 12755 return elfcore_write_note (abfd, buf, bufsiz, 12756 note_name, NT_S390_TODCMP, s390_todcmp, size); 12757 } 12758 12759 char * 12760 elfcore_write_s390_todpreg (bfd *abfd, 12761 char *buf, 12762 int *bufsiz, 12763 const void *s390_todpreg, 12764 int size) 12765 { 12766 char *note_name = "LINUX"; 12767 return elfcore_write_note (abfd, buf, bufsiz, 12768 note_name, NT_S390_TODPREG, s390_todpreg, size); 12769 } 12770 12771 char * 12772 elfcore_write_s390_ctrs (bfd *abfd, 12773 char *buf, 12774 int *bufsiz, 12775 const void *s390_ctrs, 12776 int size) 12777 { 12778 char *note_name = "LINUX"; 12779 return elfcore_write_note (abfd, buf, bufsiz, 12780 note_name, NT_S390_CTRS, s390_ctrs, size); 12781 } 12782 12783 char * 12784 elfcore_write_s390_prefix (bfd *abfd, 12785 char *buf, 12786 int *bufsiz, 12787 const void *s390_prefix, 12788 int size) 12789 { 12790 char *note_name = "LINUX"; 12791 return elfcore_write_note (abfd, buf, bufsiz, 12792 note_name, NT_S390_PREFIX, s390_prefix, size); 12793 } 12794 12795 char * 12796 elfcore_write_s390_last_break (bfd *abfd, 12797 char *buf, 12798 int *bufsiz, 12799 const void *s390_last_break, 12800 int size) 12801 { 12802 char *note_name = "LINUX"; 12803 return elfcore_write_note (abfd, buf, bufsiz, 12804 note_name, NT_S390_LAST_BREAK, 12805 s390_last_break, size); 12806 } 12807 12808 char * 12809 elfcore_write_s390_system_call (bfd *abfd, 12810 char *buf, 12811 int *bufsiz, 12812 const void *s390_system_call, 12813 int size) 12814 { 12815 char *note_name = "LINUX"; 12816 return elfcore_write_note (abfd, buf, bufsiz, 12817 note_name, NT_S390_SYSTEM_CALL, 12818 s390_system_call, size); 12819 } 12820 12821 char * 12822 elfcore_write_s390_tdb (bfd *abfd, 12823 char *buf, 12824 int *bufsiz, 12825 const void *s390_tdb, 12826 int size) 12827 { 12828 char *note_name = "LINUX"; 12829 return elfcore_write_note (abfd, buf, bufsiz, 12830 note_name, NT_S390_TDB, s390_tdb, size); 12831 } 12832 12833 char * 12834 elfcore_write_s390_vxrs_low (bfd *abfd, 12835 char *buf, 12836 int *bufsiz, 12837 const void *s390_vxrs_low, 12838 int size) 12839 { 12840 char *note_name = "LINUX"; 12841 return elfcore_write_note (abfd, buf, bufsiz, 12842 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); 12843 } 12844 12845 char * 12846 elfcore_write_s390_vxrs_high (bfd *abfd, 12847 char *buf, 12848 int *bufsiz, 12849 const void *s390_vxrs_high, 12850 int size) 12851 { 12852 char *note_name = "LINUX"; 12853 return elfcore_write_note (abfd, buf, bufsiz, 12854 note_name, NT_S390_VXRS_HIGH, 12855 s390_vxrs_high, size); 12856 } 12857 12858 char * 12859 elfcore_write_s390_gs_cb (bfd *abfd, 12860 char *buf, 12861 int *bufsiz, 12862 const void *s390_gs_cb, 12863 int size) 12864 { 12865 char *note_name = "LINUX"; 12866 return elfcore_write_note (abfd, buf, bufsiz, 12867 note_name, NT_S390_GS_CB, 12868 s390_gs_cb, size); 12869 } 12870 12871 char * 12872 elfcore_write_s390_gs_bc (bfd *abfd, 12873 char *buf, 12874 int *bufsiz, 12875 const void *s390_gs_bc, 12876 int size) 12877 { 12878 char *note_name = "LINUX"; 12879 return elfcore_write_note (abfd, buf, bufsiz, 12880 note_name, NT_S390_GS_BC, 12881 s390_gs_bc, size); 12882 } 12883 12884 char * 12885 elfcore_write_arm_vfp (bfd *abfd, 12886 char *buf, 12887 int *bufsiz, 12888 const void *arm_vfp, 12889 int size) 12890 { 12891 char *note_name = "LINUX"; 12892 return elfcore_write_note (abfd, buf, bufsiz, 12893 note_name, NT_ARM_VFP, arm_vfp, size); 12894 } 12895 12896 char * 12897 elfcore_write_aarch_tls (bfd *abfd, 12898 char *buf, 12899 int *bufsiz, 12900 const void *aarch_tls, 12901 int size) 12902 { 12903 char *note_name = "LINUX"; 12904 return elfcore_write_note (abfd, buf, bufsiz, 12905 note_name, NT_ARM_TLS, aarch_tls, size); 12906 } 12907 12908 char * 12909 elfcore_write_aarch_hw_break (bfd *abfd, 12910 char *buf, 12911 int *bufsiz, 12912 const void *aarch_hw_break, 12913 int size) 12914 { 12915 char *note_name = "LINUX"; 12916 return elfcore_write_note (abfd, buf, bufsiz, 12917 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); 12918 } 12919 12920 char * 12921 elfcore_write_aarch_hw_watch (bfd *abfd, 12922 char *buf, 12923 int *bufsiz, 12924 const void *aarch_hw_watch, 12925 int size) 12926 { 12927 char *note_name = "LINUX"; 12928 return elfcore_write_note (abfd, buf, bufsiz, 12929 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); 12930 } 12931 12932 char * 12933 elfcore_write_aarch_sve (bfd *abfd, 12934 char *buf, 12935 int *bufsiz, 12936 const void *aarch_sve, 12937 int size) 12938 { 12939 char *note_name = "LINUX"; 12940 return elfcore_write_note (abfd, buf, bufsiz, 12941 note_name, NT_ARM_SVE, aarch_sve, size); 12942 } 12943 12944 char * 12945 elfcore_write_aarch_pauth (bfd *abfd, 12946 char *buf, 12947 int *bufsiz, 12948 const void *aarch_pauth, 12949 int size) 12950 { 12951 char *note_name = "LINUX"; 12952 return elfcore_write_note (abfd, buf, bufsiz, 12953 note_name, NT_ARM_PAC_MASK, aarch_pauth, size); 12954 } 12955 12956 char * 12957 elfcore_write_aarch_mte (bfd *abfd, 12958 char *buf, 12959 int *bufsiz, 12960 const void *aarch_mte, 12961 int size) 12962 { 12963 char *note_name = "LINUX"; 12964 return elfcore_write_note (abfd, buf, bufsiz, 12965 note_name, NT_ARM_TAGGED_ADDR_CTRL, 12966 aarch_mte, 12967 size); 12968 } 12969 12970 char * 12971 elfcore_write_aarch_ssve (bfd *abfd, 12972 char *buf, 12973 int *bufsiz, 12974 const void *aarch_ssve, 12975 int size) 12976 { 12977 char *note_name = "LINUX"; 12978 return elfcore_write_note (abfd, buf, bufsiz, 12979 note_name, NT_ARM_SSVE, 12980 aarch_ssve, 12981 size); 12982 } 12983 12984 char * 12985 elfcore_write_aarch_za (bfd *abfd, 12986 char *buf, 12987 int *bufsiz, 12988 const void *aarch_za, 12989 int size) 12990 { 12991 char *note_name = "LINUX"; 12992 return elfcore_write_note (abfd, buf, bufsiz, 12993 note_name, NT_ARM_ZA, 12994 aarch_za, 12995 size); 12996 } 12997 12998 /* Write the buffer of zt register values in aarch_zt (length SIZE) into 12999 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being 13000 written into. Return a pointer to the new start of the note buffer, to 13001 replace BUF which may no longer be valid. */ 13002 13003 char * 13004 elfcore_write_aarch_zt (bfd *abfd, 13005 char *buf, 13006 int *bufsiz, 13007 const void *aarch_zt, 13008 int size) 13009 { 13010 char *note_name = "LINUX"; 13011 return elfcore_write_note (abfd, buf, bufsiz, 13012 note_name, NT_ARM_ZT, 13013 aarch_zt, 13014 size); 13015 } 13016 13017 char * 13018 elfcore_write_arc_v2 (bfd *abfd, 13019 char *buf, 13020 int *bufsiz, 13021 const void *arc_v2, 13022 int size) 13023 { 13024 char *note_name = "LINUX"; 13025 return elfcore_write_note (abfd, buf, bufsiz, 13026 note_name, NT_ARC_V2, arc_v2, size); 13027 } 13028 13029 char * 13030 elfcore_write_loongarch_cpucfg (bfd *abfd, 13031 char *buf, 13032 int *bufsiz, 13033 const void *loongarch_cpucfg, 13034 int size) 13035 { 13036 char *note_name = "LINUX"; 13037 return elfcore_write_note (abfd, buf, bufsiz, 13038 note_name, NT_LARCH_CPUCFG, 13039 loongarch_cpucfg, size); 13040 } 13041 13042 char * 13043 elfcore_write_loongarch_lbt (bfd *abfd, 13044 char *buf, 13045 int *bufsiz, 13046 const void *loongarch_lbt, 13047 int size) 13048 { 13049 char *note_name = "LINUX"; 13050 return elfcore_write_note (abfd, buf, bufsiz, 13051 note_name, NT_LARCH_LBT, loongarch_lbt, size); 13052 } 13053 13054 char * 13055 elfcore_write_loongarch_lsx (bfd *abfd, 13056 char *buf, 13057 int *bufsiz, 13058 const void *loongarch_lsx, 13059 int size) 13060 { 13061 char *note_name = "LINUX"; 13062 return elfcore_write_note (abfd, buf, bufsiz, 13063 note_name, NT_LARCH_LSX, loongarch_lsx, size); 13064 } 13065 13066 char * 13067 elfcore_write_loongarch_lasx (bfd *abfd, 13068 char *buf, 13069 int *bufsiz, 13070 const void *loongarch_lasx, 13071 int size) 13072 { 13073 char *note_name = "LINUX"; 13074 return elfcore_write_note (abfd, buf, bufsiz, 13075 note_name, NT_LARCH_LASX, loongarch_lasx, size); 13076 } 13077 13078 /* Write the buffer of csr values in CSRS (length SIZE) into the note 13079 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being 13080 written into. Return a pointer to the new start of the note buffer, to 13081 replace BUF which may no longer be valid. */ 13082 13083 char * 13084 elfcore_write_riscv_csr (bfd *abfd, 13085 char *buf, 13086 int *bufsiz, 13087 const void *csrs, 13088 int size) 13089 { 13090 const char *note_name = "GDB"; 13091 return elfcore_write_note (abfd, buf, bufsiz, 13092 note_name, NT_RISCV_CSR, csrs, size); 13093 } 13094 13095 /* Write the target description (a string) pointed to by TDESC, length 13096 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the 13097 note is being written into. Return a pointer to the new start of the 13098 note buffer, to replace BUF which may no longer be valid. */ 13099 13100 char * 13101 elfcore_write_gdb_tdesc (bfd *abfd, 13102 char *buf, 13103 int *bufsiz, 13104 const void *tdesc, 13105 int size) 13106 { 13107 const char *note_name = "GDB"; 13108 return elfcore_write_note (abfd, buf, bufsiz, 13109 note_name, NT_GDB_TDESC, tdesc, size); 13110 } 13111 13112 char * 13113 elfcore_write_register_note (bfd *abfd, 13114 char *buf, 13115 int *bufsiz, 13116 const char *section, 13117 const void *data, 13118 int size) 13119 { 13120 if (strcmp (section, ".reg2") == 0) 13121 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); 13122 if (strcmp (section, ".reg-xfp") == 0) 13123 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); 13124 if (strcmp (section, ".reg-xstate") == 0) 13125 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); 13126 if (strcmp (section, ".reg-x86-segbases") == 0) 13127 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size); 13128 if (strcmp (section, ".reg-ppc-vmx") == 0) 13129 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); 13130 if (strcmp (section, ".reg-ppc-vsx") == 0) 13131 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); 13132 if (strcmp (section, ".reg-ppc-tar") == 0) 13133 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size); 13134 if (strcmp (section, ".reg-ppc-ppr") == 0) 13135 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size); 13136 if (strcmp (section, ".reg-ppc-dscr") == 0) 13137 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size); 13138 if (strcmp (section, ".reg-ppc-ebb") == 0) 13139 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size); 13140 if (strcmp (section, ".reg-ppc-pmu") == 0) 13141 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size); 13142 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0) 13143 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size); 13144 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0) 13145 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size); 13146 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0) 13147 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size); 13148 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0) 13149 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size); 13150 if (strcmp (section, ".reg-ppc-tm-spr") == 0) 13151 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size); 13152 if (strcmp (section, ".reg-ppc-tm-ctar") == 0) 13153 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size); 13154 if (strcmp (section, ".reg-ppc-tm-cppr") == 0) 13155 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size); 13156 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0) 13157 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size); 13158 if (strcmp (section, ".reg-s390-high-gprs") == 0) 13159 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); 13160 if (strcmp (section, ".reg-s390-timer") == 0) 13161 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); 13162 if (strcmp (section, ".reg-s390-todcmp") == 0) 13163 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); 13164 if (strcmp (section, ".reg-s390-todpreg") == 0) 13165 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); 13166 if (strcmp (section, ".reg-s390-ctrs") == 0) 13167 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); 13168 if (strcmp (section, ".reg-s390-prefix") == 0) 13169 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); 13170 if (strcmp (section, ".reg-s390-last-break") == 0) 13171 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); 13172 if (strcmp (section, ".reg-s390-system-call") == 0) 13173 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); 13174 if (strcmp (section, ".reg-s390-tdb") == 0) 13175 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); 13176 if (strcmp (section, ".reg-s390-vxrs-low") == 0) 13177 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); 13178 if (strcmp (section, ".reg-s390-vxrs-high") == 0) 13179 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); 13180 if (strcmp (section, ".reg-s390-gs-cb") == 0) 13181 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size); 13182 if (strcmp (section, ".reg-s390-gs-bc") == 0) 13183 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size); 13184 if (strcmp (section, ".reg-arm-vfp") == 0) 13185 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); 13186 if (strcmp (section, ".reg-aarch-tls") == 0) 13187 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); 13188 if (strcmp (section, ".reg-aarch-hw-break") == 0) 13189 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); 13190 if (strcmp (section, ".reg-aarch-hw-watch") == 0) 13191 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); 13192 if (strcmp (section, ".reg-aarch-sve") == 0) 13193 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size); 13194 if (strcmp (section, ".reg-aarch-pauth") == 0) 13195 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size); 13196 if (strcmp (section, ".reg-aarch-mte") == 0) 13197 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size); 13198 if (strcmp (section, ".reg-aarch-ssve") == 0) 13199 return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size); 13200 if (strcmp (section, ".reg-aarch-za") == 0) 13201 return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size); 13202 if (strcmp (section, ".reg-aarch-zt") == 0) 13203 return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size); 13204 if (strcmp (section, ".reg-arc-v2") == 0) 13205 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size); 13206 if (strcmp (section, ".gdb-tdesc") == 0) 13207 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size); 13208 if (strcmp (section, ".reg-riscv-csr") == 0) 13209 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size); 13210 if (strcmp (section, ".reg-loongarch-cpucfg") == 0) 13211 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size); 13212 if (strcmp (section, ".reg-loongarch-lbt") == 0) 13213 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size); 13214 if (strcmp (section, ".reg-loongarch-lsx") == 0) 13215 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size); 13216 if (strcmp (section, ".reg-loongarch-lasx") == 0) 13217 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size); 13218 return NULL; 13219 } 13220 13221 char * 13222 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size, 13223 const void *buf, int bufsiz) 13224 { 13225 return elfcore_write_note (obfd, note_data, note_size, 13226 "CORE", NT_FILE, buf, bufsiz); 13227 } 13228 13229 static bool 13230 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset, 13231 size_t align) 13232 { 13233 char *p; 13234 13235 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1. 13236 gABI specifies that PT_NOTE alignment should be aligned to 4 13237 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If 13238 align is less than 4, we use 4 byte alignment. */ 13239 if (align < 4) 13240 align = 4; 13241 if (align != 4 && align != 8) 13242 return false; 13243 13244 p = buf; 13245 while (p < buf + size) 13246 { 13247 Elf_External_Note *xnp = (Elf_External_Note *) p; 13248 Elf_Internal_Note in; 13249 13250 if (offsetof (Elf_External_Note, name) > buf - p + size) 13251 return false; 13252 13253 in.type = H_GET_32 (abfd, xnp->type); 13254 13255 in.namesz = H_GET_32 (abfd, xnp->namesz); 13256 in.namedata = xnp->name; 13257 if (in.namesz > buf - in.namedata + size) 13258 return false; 13259 13260 in.descsz = H_GET_32 (abfd, xnp->descsz); 13261 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align); 13262 in.descpos = offset + (in.descdata - buf); 13263 if (in.descsz != 0 13264 && (in.descdata >= buf + size 13265 || in.descsz > buf - in.descdata + size)) 13266 return false; 13267 13268 switch (bfd_get_format (abfd)) 13269 { 13270 default: 13271 return true; 13272 13273 case bfd_core: 13274 { 13275 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} 13276 struct 13277 { 13278 const char * string; 13279 size_t len; 13280 bool (*func) (bfd *, Elf_Internal_Note *); 13281 } 13282 grokers[] = 13283 { 13284 GROKER_ELEMENT ("", elfcore_grok_note), 13285 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), 13286 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), 13287 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note), 13288 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), 13289 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note), 13290 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note), 13291 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note) 13292 }; 13293 #undef GROKER_ELEMENT 13294 int i; 13295 13296 for (i = ARRAY_SIZE (grokers); i--;) 13297 { 13298 if (in.namesz >= grokers[i].len 13299 && strncmp (in.namedata, grokers[i].string, 13300 grokers[i].len) == 0) 13301 { 13302 if (! grokers[i].func (abfd, & in)) 13303 return false; 13304 break; 13305 } 13306 } 13307 break; 13308 } 13309 13310 case bfd_object: 13311 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) 13312 { 13313 if (! elfobj_grok_gnu_note (abfd, &in)) 13314 return false; 13315 } 13316 else if (in.namesz == sizeof "stapsdt" 13317 && strcmp (in.namedata, "stapsdt") == 0) 13318 { 13319 if (! elfobj_grok_stapsdt_note (abfd, &in)) 13320 return false; 13321 } 13322 break; 13323 } 13324 13325 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align); 13326 } 13327 13328 return true; 13329 } 13330 13331 bool 13332 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size, 13333 size_t align) 13334 { 13335 char *buf; 13336 13337 if (size == 0 || (size + 1) == 0) 13338 return true; 13339 13340 if (bfd_seek (abfd, offset, SEEK_SET) != 0) 13341 return false; 13342 13343 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size); 13344 if (buf == NULL) 13345 return false; 13346 13347 /* PR 17512: file: ec08f814 13348 0-termintate the buffer so that string searches will not overflow. */ 13349 buf[size] = 0; 13350 13351 if (!elf_parse_notes (abfd, buf, size, offset, align)) 13352 { 13353 free (buf); 13354 return false; 13355 } 13356 13357 free (buf); 13358 return true; 13359 } 13360 13361 /* Providing external access to the ELF program header table. */ 13363 13364 /* Return an upper bound on the number of bytes required to store a 13365 copy of ABFD's program header table entries. Return -1 if an error 13366 occurs; bfd_get_error will return an appropriate code. */ 13367 13368 long 13369 bfd_get_elf_phdr_upper_bound (bfd *abfd) 13370 { 13371 if (abfd->xvec->flavour != bfd_target_elf_flavour) 13372 { 13373 bfd_set_error (bfd_error_wrong_format); 13374 return -1; 13375 } 13376 13377 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); 13378 } 13379 13380 /* Copy ABFD's program header table entries to *PHDRS. The entries 13381 will be stored as an array of Elf_Internal_Phdr structures, as 13382 defined in include/elf/internal.h. To find out how large the 13383 buffer needs to be, call bfd_get_elf_phdr_upper_bound. 13384 13385 Return the number of program header table entries read, or -1 if an 13386 error occurs; bfd_get_error will return an appropriate code. */ 13387 13388 int 13389 bfd_get_elf_phdrs (bfd *abfd, void *phdrs) 13390 { 13391 int num_phdrs; 13392 13393 if (abfd->xvec->flavour != bfd_target_elf_flavour) 13394 { 13395 bfd_set_error (bfd_error_wrong_format); 13396 return -1; 13397 } 13398 13399 num_phdrs = elf_elfheader (abfd)->e_phnum; 13400 if (num_phdrs != 0) 13401 memcpy (phdrs, elf_tdata (abfd)->phdr, 13402 num_phdrs * sizeof (Elf_Internal_Phdr)); 13403 13404 return num_phdrs; 13405 } 13406 13407 enum elf_reloc_type_class 13408 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, 13409 const asection *rel_sec ATTRIBUTE_UNUSED, 13410 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) 13411 { 13412 return reloc_class_normal; 13413 } 13414 13415 /* For RELA architectures, return the relocation value for a 13416 relocation against a local symbol. */ 13417 13418 bfd_vma 13419 _bfd_elf_rela_local_sym (bfd *abfd, 13420 Elf_Internal_Sym *sym, 13421 asection **psec, 13422 Elf_Internal_Rela *rel) 13423 { 13424 asection *sec = *psec; 13425 bfd_vma relocation; 13426 13427 relocation = (sec->output_section->vma 13428 + sec->output_offset 13429 + sym->st_value); 13430 if ((sec->flags & SEC_MERGE) 13431 && ELF_ST_TYPE (sym->st_info) == STT_SECTION 13432 && sec->sec_info_type == SEC_INFO_TYPE_MERGE) 13433 { 13434 rel->r_addend = 13435 _bfd_merged_section_offset (abfd, psec, 13436 elf_section_data (sec)->sec_info, 13437 sym->st_value + rel->r_addend); 13438 if (sec != *psec) 13439 { 13440 /* If we have changed the section, and our original section is 13441 marked with SEC_EXCLUDE, it means that the original 13442 SEC_MERGE section has been completely subsumed in some 13443 other SEC_MERGE section. In this case, we need to leave 13444 some info around for --emit-relocs. */ 13445 if ((sec->flags & SEC_EXCLUDE) != 0) 13446 sec->kept_section = *psec; 13447 sec = *psec; 13448 } 13449 rel->r_addend -= relocation; 13450 rel->r_addend += sec->output_section->vma + sec->output_offset; 13451 } 13452 return relocation; 13453 } 13454 13455 bfd_vma 13456 _bfd_elf_rel_local_sym (bfd *abfd, 13457 Elf_Internal_Sym *sym, 13458 asection **psec, 13459 bfd_vma addend) 13460 { 13461 asection *sec = *psec; 13462 13463 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) 13464 return sym->st_value + addend; 13465 13466 return _bfd_merged_section_offset (abfd, psec, 13467 elf_section_data (sec)->sec_info, 13468 sym->st_value + addend); 13469 } 13470 13471 /* Adjust an address within a section. Given OFFSET within SEC, return 13472 the new offset within the section, based upon changes made to the 13473 section. Returns -1 if the offset is now invalid. 13474 The offset (in abnd out) is in target sized bytes, however big a 13475 byte may be. */ 13476 13477 bfd_vma 13478 _bfd_elf_section_offset (bfd *abfd, 13479 struct bfd_link_info *info, 13480 asection *sec, 13481 bfd_vma offset) 13482 { 13483 switch (sec->sec_info_type) 13484 { 13485 case SEC_INFO_TYPE_STABS: 13486 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, 13487 offset); 13488 case SEC_INFO_TYPE_EH_FRAME: 13489 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); 13490 13491 default: 13492 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) 13493 { 13494 /* Reverse the offset. */ 13495 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13496 bfd_size_type address_size = bed->s->arch_size / 8; 13497 13498 /* address_size and sec->size are in octets. Convert 13499 to bytes before subtracting the original offset. */ 13500 offset = ((sec->size - address_size) 13501 / bfd_octets_per_byte (abfd, sec) - offset); 13502 } 13503 return offset; 13504 } 13505 } 13506 13507 long 13509 _bfd_elf_get_synthetic_symtab (bfd *abfd, 13510 long symcount ATTRIBUTE_UNUSED, 13511 asymbol **syms ATTRIBUTE_UNUSED, 13512 long dynsymcount, 13513 asymbol **dynsyms, 13514 asymbol **ret) 13515 { 13516 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13517 asection *relplt; 13518 asymbol *s; 13519 const char *relplt_name; 13520 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool); 13521 arelent *p; 13522 long count, i, n; 13523 size_t size; 13524 Elf_Internal_Shdr *hdr; 13525 char *names; 13526 asection *plt; 13527 13528 *ret = NULL; 13529 13530 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) 13531 return 0; 13532 13533 if (dynsymcount <= 0) 13534 return 0; 13535 13536 if (!bed->plt_sym_val) 13537 return 0; 13538 13539 relplt_name = bed->relplt_name; 13540 if (relplt_name == NULL) 13541 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; 13542 relplt = bfd_get_section_by_name (abfd, relplt_name); 13543 if (relplt == NULL) 13544 return 0; 13545 13546 hdr = &elf_section_data (relplt)->this_hdr; 13547 if (hdr->sh_link != elf_dynsymtab (abfd) 13548 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) 13549 return 0; 13550 13551 plt = bfd_get_section_by_name (abfd, ".plt"); 13552 if (plt == NULL) 13553 return 0; 13554 13555 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; 13556 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true)) 13557 return -1; 13558 13559 count = NUM_SHDR_ENTRIES (hdr); 13560 size = count * sizeof (asymbol); 13561 p = relplt->relocation; 13562 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) 13563 { 13564 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); 13565 if (p->addend != 0) 13566 { 13567 #ifdef BFD64 13568 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); 13569 #else 13570 size += sizeof ("+0x") - 1 + 8; 13571 #endif 13572 } 13573 } 13574 13575 s = *ret = (asymbol *) bfd_malloc (size); 13576 if (s == NULL) 13577 return -1; 13578 13579 names = (char *) (s + count); 13580 p = relplt->relocation; 13581 n = 0; 13582 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) 13583 { 13584 size_t len; 13585 bfd_vma addr; 13586 13587 addr = bed->plt_sym_val (i, plt, p); 13588 if (addr == (bfd_vma) -1) 13589 continue; 13590 13591 *s = **p->sym_ptr_ptr; 13592 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since 13593 we are defining a symbol, ensure one of them is set. */ 13594 if ((s->flags & BSF_LOCAL) == 0) 13595 s->flags |= BSF_GLOBAL; 13596 s->flags |= BSF_SYNTHETIC; 13597 s->section = plt; 13598 s->value = addr - plt->vma; 13599 s->name = names; 13600 s->udata.p = NULL; 13601 len = strlen ((*p->sym_ptr_ptr)->name); 13602 memcpy (names, (*p->sym_ptr_ptr)->name, len); 13603 names += len; 13604 if (p->addend != 0) 13605 { 13606 char buf[30], *a; 13607 13608 memcpy (names, "+0x", sizeof ("+0x") - 1); 13609 names += sizeof ("+0x") - 1; 13610 bfd_sprintf_vma (abfd, buf, p->addend); 13611 for (a = buf; *a == '0'; ++a) 13612 ; 13613 len = strlen (a); 13614 memcpy (names, a, len); 13615 names += len; 13616 } 13617 memcpy (names, "@plt", sizeof ("@plt")); 13618 names += sizeof ("@plt"); 13619 ++s, ++n; 13620 } 13621 13622 return n; 13623 } 13624 13625 /* It is only used by x86-64 so far. 13626 ??? This repeats *COM* id of zero. sec->id is supposed to be unique, 13627 but current usage would allow all of _bfd_std_section to be zero. */ 13628 static const asymbol lcomm_sym 13629 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section); 13630 asection _bfd_elf_large_com_section 13631 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym, 13632 "LARGE_COMMON", 0, SEC_IS_COMMON); 13633 13634 bool 13635 _bfd_elf_final_write_processing (bfd *abfd) 13636 { 13637 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */ 13638 13639 i_ehdrp = elf_elfheader (abfd); 13640 13641 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) 13642 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; 13643 13644 /* Set the osabi field to ELFOSABI_GNU if the binary contains 13645 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type 13646 or STB_GNU_UNIQUE binding. */ 13647 if (elf_tdata (abfd)->has_gnu_osabi != 0) 13648 { 13649 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) 13650 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; 13651 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU 13652 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD) 13653 { 13654 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) 13655 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU " 13656 "and FreeBSD targets")); 13657 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc) 13658 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported " 13659 "only by GNU and FreeBSD targets")); 13660 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique) 13661 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported " 13662 "only by GNU and FreeBSD targets")); 13663 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain) 13664 _bfd_error_handler (_("GNU_RETAIN section is supported " 13665 "only by GNU and FreeBSD targets")); 13666 bfd_set_error (bfd_error_sorry); 13667 return false; 13668 } 13669 } 13670 return true; 13671 } 13672 13673 13674 /* Return TRUE for ELF symbol types that represent functions. 13675 This is the default version of this function, which is sufficient for 13676 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ 13677 13678 bool 13679 _bfd_elf_is_function_type (unsigned int type) 13680 { 13681 return (type == STT_FUNC 13682 || type == STT_GNU_IFUNC); 13683 } 13684 13685 /* If the ELF symbol SYM might be a function in SEC, return the 13686 function size and set *CODE_OFF to the function's entry point, 13687 otherwise return zero. */ 13688 13689 bfd_size_type 13690 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, 13691 bfd_vma *code_off) 13692 { 13693 bfd_size_type size; 13694 elf_symbol_type * elf_sym = (elf_symbol_type *) sym; 13695 13696 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT 13697 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 13698 || sym->section != sec) 13699 return 0; 13700 13701 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size; 13702 13703 /* In theory we should check that the symbol's type satisfies 13704 _bfd_elf_is_function_type(), but there are some function-like 13705 symbols which would fail this test. (eg _start). Instead 13706 we check for hidden, local, notype symbols with zero size. 13707 This type of symbol is generated by the annobin plugin for gcc 13708 and clang, and should not be considered to be a function symbol. */ 13709 if (size == 0 13710 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL) 13711 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE 13712 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN) 13713 return 0; 13714 13715 *code_off = sym->value; 13716 /* Do not return 0 for the function's size. */ 13717 return size ? size : 1; 13718 } 13719 13720 /* Set to non-zero to enable some debug messages. */ 13721 #define DEBUG_SECONDARY_RELOCS 0 13722 13723 /* An internal-to-the-bfd-library only section type 13724 used to indicate a cached secondary reloc section. */ 13725 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA) 13726 13727 /* Create a BFD section to hold a secondary reloc section. */ 13728 13729 bool 13730 _bfd_elf_init_secondary_reloc_section (bfd * abfd, 13731 Elf_Internal_Shdr *hdr, 13732 const char * name, 13733 unsigned int shindex) 13734 { 13735 /* We only support RELA secondary relocs. */ 13736 if (hdr->sh_type != SHT_RELA) 13737 return false; 13738 13739 #if DEBUG_SECONDARY_RELOCS 13740 fprintf (stderr, "secondary reloc section %s encountered\n", name); 13741 #endif 13742 hdr->sh_type = SHT_SECONDARY_RELOC; 13743 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 13744 } 13745 13746 /* Read in any secondary relocs associated with SEC. */ 13747 13748 bool 13749 _bfd_elf_slurp_secondary_reloc_section (bfd * abfd, 13750 asection * sec, 13751 asymbol ** symbols, 13752 bool dynamic) 13753 { 13754 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); 13755 asection * relsec; 13756 bool result = true; 13757 bfd_vma (*r_sym) (bfd_vma); 13758 ufile_ptr filesize; 13759 13760 #if BFD_DEFAULT_TARGET_SIZE > 32 13761 if (bfd_arch_bits_per_address (abfd) != 32) 13762 r_sym = elf64_r_sym; 13763 else 13764 #endif 13765 r_sym = elf32_r_sym; 13766 13767 if (!elf_section_data (sec)->has_secondary_relocs) 13768 return true; 13769 13770 /* Discover if there are any secondary reloc sections 13771 associated with SEC. */ 13772 filesize = bfd_get_file_size (abfd); 13773 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) 13774 { 13775 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr; 13776 13777 if (hdr->sh_type == SHT_SECONDARY_RELOC 13778 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx 13779 && (hdr->sh_entsize == ebd->s->sizeof_rel 13780 || hdr->sh_entsize == ebd->s->sizeof_rela)) 13781 { 13782 bfd_byte * native_relocs; 13783 bfd_byte * native_reloc; 13784 arelent * internal_relocs; 13785 arelent * internal_reloc; 13786 size_t i; 13787 unsigned int entsize; 13788 unsigned int symcount; 13789 bfd_size_type reloc_count; 13790 size_t amt; 13791 13792 if (ebd->elf_info_to_howto == NULL) 13793 return false; 13794 13795 #if DEBUG_SECONDARY_RELOCS 13796 fprintf (stderr, "read secondary relocs for %s from %s\n", 13797 sec->name, relsec->name); 13798 #endif 13799 entsize = hdr->sh_entsize; 13800 13801 if (filesize != 0 13802 && ((ufile_ptr) hdr->sh_offset > filesize 13803 || hdr->sh_size > filesize - hdr->sh_offset)) 13804 { 13805 bfd_set_error (bfd_error_file_truncated); 13806 result = false; 13807 continue; 13808 } 13809 13810 native_relocs = bfd_malloc (hdr->sh_size); 13811 if (native_relocs == NULL) 13812 { 13813 result = false; 13814 continue; 13815 } 13816 13817 reloc_count = NUM_SHDR_ENTRIES (hdr); 13818 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt)) 13819 { 13820 free (native_relocs); 13821 bfd_set_error (bfd_error_file_too_big); 13822 result = false; 13823 continue; 13824 } 13825 13826 internal_relocs = (arelent *) bfd_alloc (abfd, amt); 13827 if (internal_relocs == NULL) 13828 { 13829 free (native_relocs); 13830 result = false; 13831 continue; 13832 } 13833 13834 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 13835 || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size) 13836 { 13837 free (native_relocs); 13838 /* The internal_relocs will be freed when 13839 the memory for the bfd is released. */ 13840 result = false; 13841 continue; 13842 } 13843 13844 if (dynamic) 13845 symcount = bfd_get_dynamic_symcount (abfd); 13846 else 13847 symcount = bfd_get_symcount (abfd); 13848 13849 for (i = 0, internal_reloc = internal_relocs, 13850 native_reloc = native_relocs; 13851 i < reloc_count; 13852 i++, internal_reloc++, native_reloc += entsize) 13853 { 13854 bool res; 13855 Elf_Internal_Rela rela; 13856 13857 if (entsize == ebd->s->sizeof_rel) 13858 ebd->s->swap_reloc_in (abfd, native_reloc, & rela); 13859 else /* entsize == ebd->s->sizeof_rela */ 13860 ebd->s->swap_reloca_in (abfd, native_reloc, & rela); 13861 13862 /* The address of an ELF reloc is section relative for an object 13863 file, and absolute for an executable file or shared library. 13864 The address of a normal BFD reloc is always section relative, 13865 and the address of a dynamic reloc is absolute.. */ 13866 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) 13867 internal_reloc->address = rela.r_offset; 13868 else 13869 internal_reloc->address = rela.r_offset - sec->vma; 13870 13871 if (r_sym (rela.r_info) == STN_UNDEF) 13872 { 13873 /* FIXME: This and the error case below mean that we 13874 have a symbol on relocs that is not elf_symbol_type. */ 13875 internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol; 13876 } 13877 else if (r_sym (rela.r_info) > symcount) 13878 { 13879 _bfd_error_handler 13880 /* xgettext:c-format */ 13881 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"), 13882 abfd, sec, i, (long) r_sym (rela.r_info)); 13883 bfd_set_error (bfd_error_bad_value); 13884 internal_reloc->sym_ptr_ptr = &bfd_abs_section_ptr->symbol; 13885 result = false; 13886 } 13887 else 13888 { 13889 asymbol **ps; 13890 13891 ps = symbols + r_sym (rela.r_info) - 1; 13892 internal_reloc->sym_ptr_ptr = ps; 13893 /* Make sure that this symbol is not removed by strip. */ 13894 (*ps)->flags |= BSF_KEEP; 13895 } 13896 13897 internal_reloc->addend = rela.r_addend; 13898 13899 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela); 13900 if (! res || internal_reloc->howto == NULL) 13901 { 13902 #if DEBUG_SECONDARY_RELOCS 13903 fprintf (stderr, 13904 "there is no howto associated with reloc %lx\n", 13905 rela.r_info); 13906 #endif 13907 result = false; 13908 } 13909 } 13910 13911 free (native_relocs); 13912 /* Store the internal relocs. */ 13913 elf_section_data (relsec)->sec_info = internal_relocs; 13914 } 13915 } 13916 13917 return result; 13918 } 13919 13920 /* Set the ELF section header fields of an output secondary reloc section. */ 13921 13922 bool 13923 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED, 13924 bfd *obfd ATTRIBUTE_UNUSED, 13925 const Elf_Internal_Shdr *isection, 13926 Elf_Internal_Shdr *osection) 13927 { 13928 asection * isec; 13929 asection * osec; 13930 struct bfd_elf_section_data * esd; 13931 13932 if (isection == NULL) 13933 return false; 13934 13935 if (isection->sh_type != SHT_SECONDARY_RELOC) 13936 return true; 13937 13938 isec = isection->bfd_section; 13939 if (isec == NULL) 13940 return false; 13941 13942 osec = osection->bfd_section; 13943 if (osec == NULL) 13944 return false; 13945 13946 esd = elf_section_data (osec); 13947 BFD_ASSERT (esd->sec_info == NULL); 13948 esd->sec_info = elf_section_data (isec)->sec_info; 13949 osection->sh_type = SHT_RELA; 13950 osection->sh_link = elf_onesymtab (obfd); 13951 if (osection->sh_link == 0) 13952 { 13953 /* There is no symbol table - we are hosed... */ 13954 _bfd_error_handler 13955 /* xgettext:c-format */ 13956 (_("%pB(%pA): link section cannot be set" 13957 " because the output file does not have a symbol table"), 13958 obfd, osec); 13959 bfd_set_error (bfd_error_bad_value); 13960 return false; 13961 } 13962 13963 /* Find the output section that corresponds to the isection's 13964 sh_info link. */ 13965 if (isection->sh_info == 0 13966 || isection->sh_info >= elf_numsections (ibfd)) 13967 { 13968 _bfd_error_handler 13969 /* xgettext:c-format */ 13970 (_("%pB(%pA): info section index is invalid"), 13971 obfd, osec); 13972 bfd_set_error (bfd_error_bad_value); 13973 return false; 13974 } 13975 13976 isection = elf_elfsections (ibfd)[isection->sh_info]; 13977 13978 if (isection == NULL 13979 || isection->bfd_section == NULL 13980 || isection->bfd_section->output_section == NULL) 13981 { 13982 _bfd_error_handler 13983 /* xgettext:c-format */ 13984 (_("%pB(%pA): info section index cannot be set" 13985 " because the section is not in the output"), 13986 obfd, osec); 13987 bfd_set_error (bfd_error_bad_value); 13988 return false; 13989 } 13990 13991 esd = elf_section_data (isection->bfd_section->output_section); 13992 BFD_ASSERT (esd != NULL); 13993 osection->sh_info = esd->this_idx; 13994 esd->has_secondary_relocs = true; 13995 #if DEBUG_SECONDARY_RELOCS 13996 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n", 13997 osec->name, osection->sh_link, osection->sh_info); 13998 fprintf (stderr, "mark section %s as having secondary relocs\n", 13999 bfd_section_name (isection->bfd_section->output_section)); 14000 #endif 14001 14002 return true; 14003 } 14004 14005 /* Write out a secondary reloc section. 14006 14007 FIXME: Currently this function can result in a serious performance penalty 14008 for files with secondary relocs and lots of sections. The proper way to 14009 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary 14010 relocs together and then to have this function just walk that chain. */ 14011 14012 bool 14013 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec) 14014 { 14015 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); 14016 bfd_vma addr_offset; 14017 asection * relsec; 14018 bfd_vma (*r_info) (bfd_vma, bfd_vma); 14019 bool result = true; 14020 14021 if (sec == NULL) 14022 return false; 14023 14024 #if BFD_DEFAULT_TARGET_SIZE > 32 14025 if (bfd_arch_bits_per_address (abfd) != 32) 14026 r_info = elf64_r_info; 14027 else 14028 #endif 14029 r_info = elf32_r_info; 14030 14031 /* The address of an ELF reloc is section relative for an object 14032 file, and absolute for an executable file or shared library. 14033 The address of a BFD reloc is always section relative. */ 14034 addr_offset = 0; 14035 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) 14036 addr_offset = sec->vma; 14037 14038 /* Discover if there are any secondary reloc sections 14039 associated with SEC. */ 14040 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) 14041 { 14042 const struct bfd_elf_section_data * const esd = elf_section_data (relsec); 14043 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr; 14044 14045 if (hdr->sh_type == SHT_RELA 14046 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx) 14047 { 14048 asymbol * last_sym; 14049 int last_sym_idx; 14050 size_t reloc_count; 14051 size_t idx; 14052 bfd_size_type entsize; 14053 arelent * src_irel; 14054 bfd_byte * dst_rela; 14055 14056 if (hdr->contents != NULL) 14057 { 14058 _bfd_error_handler 14059 /* xgettext:c-format */ 14060 (_("%pB(%pA): error: secondary reloc section processed twice"), 14061 abfd, relsec); 14062 bfd_set_error (bfd_error_bad_value); 14063 result = false; 14064 continue; 14065 } 14066 14067 entsize = hdr->sh_entsize; 14068 if (entsize == 0) 14069 { 14070 _bfd_error_handler 14071 /* xgettext:c-format */ 14072 (_("%pB(%pA): error: secondary reloc section" 14073 " has zero sized entries"), 14074 abfd, relsec); 14075 bfd_set_error (bfd_error_bad_value); 14076 result = false; 14077 continue; 14078 } 14079 else if (entsize != ebd->s->sizeof_rel 14080 && entsize != ebd->s->sizeof_rela) 14081 { 14082 _bfd_error_handler 14083 /* xgettext:c-format */ 14084 (_("%pB(%pA): error: secondary reloc section" 14085 " has non-standard sized entries"), 14086 abfd, relsec); 14087 bfd_set_error (bfd_error_bad_value); 14088 result = false; 14089 continue; 14090 } 14091 14092 reloc_count = hdr->sh_size / entsize; 14093 hdr->sh_size = entsize * reloc_count; 14094 if (reloc_count == 0) 14095 { 14096 _bfd_error_handler 14097 /* xgettext:c-format */ 14098 (_("%pB(%pA): error: secondary reloc section is empty!"), 14099 abfd, relsec); 14100 bfd_set_error (bfd_error_bad_value); 14101 result = false; 14102 continue; 14103 } 14104 14105 hdr->contents = bfd_alloc (abfd, hdr->sh_size); 14106 if (hdr->contents == NULL) 14107 continue; 14108 14109 #if DEBUG_SECONDARY_RELOCS 14110 fprintf (stderr, "write %u secondary relocs for %s from %s\n", 14111 reloc_count, sec->name, relsec->name); 14112 #endif 14113 last_sym = NULL; 14114 last_sym_idx = 0; 14115 dst_rela = hdr->contents; 14116 src_irel = (arelent *) esd->sec_info; 14117 if (src_irel == NULL) 14118 { 14119 _bfd_error_handler 14120 /* xgettext:c-format */ 14121 (_("%pB(%pA): error: internal relocs missing" 14122 " for secondary reloc section"), 14123 abfd, relsec); 14124 bfd_set_error (bfd_error_bad_value); 14125 result = false; 14126 continue; 14127 } 14128 14129 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize) 14130 { 14131 Elf_Internal_Rela src_rela; 14132 arelent *ptr; 14133 asymbol *sym; 14134 int n; 14135 14136 ptr = src_irel + idx; 14137 if (ptr == NULL) 14138 { 14139 _bfd_error_handler 14140 /* xgettext:c-format */ 14141 (_("%pB(%pA): error: reloc table entry %zu is empty"), 14142 abfd, relsec, idx); 14143 bfd_set_error (bfd_error_bad_value); 14144 result = false; 14145 break; 14146 } 14147 14148 if (ptr->sym_ptr_ptr == NULL) 14149 { 14150 /* FIXME: Is this an error ? */ 14151 n = 0; 14152 } 14153 else 14154 { 14155 sym = *ptr->sym_ptr_ptr; 14156 14157 if (sym == last_sym) 14158 n = last_sym_idx; 14159 else 14160 { 14161 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym); 14162 if (n < 0) 14163 { 14164 _bfd_error_handler 14165 /* xgettext:c-format */ 14166 (_("%pB(%pA): error: secondary reloc %zu" 14167 " references a missing symbol"), 14168 abfd, relsec, idx); 14169 bfd_set_error (bfd_error_bad_value); 14170 result = false; 14171 n = 0; 14172 } 14173 14174 last_sym = sym; 14175 last_sym_idx = n; 14176 } 14177 14178 if (sym->the_bfd != NULL 14179 && sym->the_bfd->xvec != abfd->xvec 14180 && ! _bfd_elf_validate_reloc (abfd, ptr)) 14181 { 14182 _bfd_error_handler 14183 /* xgettext:c-format */ 14184 (_("%pB(%pA): error: secondary reloc %zu" 14185 " references a deleted symbol"), 14186 abfd, relsec, idx); 14187 bfd_set_error (bfd_error_bad_value); 14188 result = false; 14189 n = 0; 14190 } 14191 } 14192 14193 src_rela.r_offset = ptr->address + addr_offset; 14194 if (ptr->howto == NULL) 14195 { 14196 _bfd_error_handler 14197 /* xgettext:c-format */ 14198 (_("%pB(%pA): error: secondary reloc %zu" 14199 " is of an unknown type"), 14200 abfd, relsec, idx); 14201 bfd_set_error (bfd_error_bad_value); 14202 result = false; 14203 src_rela.r_info = r_info (0, 0); 14204 } 14205 else 14206 src_rela.r_info = r_info (n, ptr->howto->type); 14207 src_rela.r_addend = ptr->addend; 14208 14209 if (entsize == ebd->s->sizeof_rel) 14210 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela); 14211 else /* entsize == ebd->s->sizeof_rela */ 14212 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela); 14213 } 14214 } 14215 } 14216 14217 return result; 14218 } 14219 14220 /* Mmap in section contents. If FINAL_LINK is false, set *BUF to NULL 14221 before calling bfd_get_full_section_contents. */ 14222 14223 static bool 14224 elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf, 14225 bool final_link) 14226 { 14227 #ifdef USE_MMAP 14228 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14229 if (bed->use_mmap 14230 && sec->compress_status == COMPRESS_SECTION_NONE 14231 && (sec->flags & SEC_LINKER_CREATED) == 0) 14232 { 14233 /* Use mmap only if section size >= the minimum mmap section 14234 size. */ 14235 size_t readsz = bfd_get_section_limit_octets (abfd, sec); 14236 size_t allocsz = bfd_get_section_alloc_size (abfd, sec); 14237 if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size) 14238 { 14239 if (sec->contents != NULL) 14240 { 14241 if (!sec->mmapped_p) 14242 abort (); 14243 *buf = sec->contents; 14244 return true; 14245 } 14246 if (sec->mmapped_p) 14247 abort (); 14248 sec->mmapped_p = 1; 14249 14250 /* Never use the preallocated buffer if mmapp is used. */ 14251 *buf = NULL; 14252 } 14253 } 14254 #endif 14255 /* NB: When this is called from elf_link_input_bfd, FINAL_LINK is 14256 true. If FINAL_LINK is false, *BUF is set to the preallocated 14257 buffer if USE_MMAP is undefined and *BUF is set to NULL if 14258 USE_MMAP is defined. */ 14259 if (!final_link) 14260 *buf = NULL; 14261 bool ret = bfd_get_full_section_contents (abfd, sec, buf); 14262 if (ret && sec->mmapped_p) 14263 *buf = sec->contents; 14264 return ret; 14265 } 14266 14267 /* Mmap in section contents. */ 14268 14269 bool 14270 _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf) 14271 { 14272 return elf_mmap_section_contents (abfd, sec, buf, false); 14273 } 14274 14275 /* Mmap in the full section contents for the final link. */ 14276 14277 bool 14278 _bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec, 14279 bfd_byte **buf) 14280 { 14281 return elf_mmap_section_contents (abfd, sec, buf, true); 14282 } 14283 14284 /* Munmap section contents. */ 14285 14286 void 14287 _bfd_elf_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED, 14288 void *contents) 14289 { 14290 /* NB: Since _bfd_elf_munmap_section_contents is called like free, 14291 CONTENTS may be NULL. */ 14292 if (contents == NULL) 14293 return; 14294 14295 #ifdef USE_MMAP 14296 if (sec->mmapped_p) 14297 { 14298 /* _bfd_elf_mmap_section_contents may return the previously 14299 mapped section contents. Munmap the section contents only 14300 if they haven't been cached. */ 14301 if (elf_section_data (sec)->this_hdr.contents == contents) 14302 return; 14303 14304 /* When _bfd_elf_mmap_section_contents returns CONTENTS as 14305 malloced, CONTENTS_ADDR is set to NULL. */ 14306 if (elf_section_data (sec)->contents_addr != NULL) 14307 { 14308 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */ 14309 if (munmap (elf_section_data (sec)->contents_addr, 14310 elf_section_data (sec)->contents_size) != 0) 14311 abort (); 14312 sec->mmapped_p = 0; 14313 sec->contents = NULL; 14314 elf_section_data (sec)->contents_addr = NULL; 14315 elf_section_data (sec)->contents_size = 0; 14316 return; 14317 } 14318 } 14319 #endif 14320 14321 free (contents); 14322 } 14323 14324 /* Munmap the full section contents for the final link. */ 14325 14326 void 14327 _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED) 14328 { 14329 #ifdef USE_MMAP 14330 if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL) 14331 { 14332 /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as 14333 malloced, CONTENTS_ADDR is set to NULL. */ 14334 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */ 14335 if (munmap (elf_section_data (sec)->contents_addr, 14336 elf_section_data (sec)->contents_size) != 0) 14337 abort (); 14338 sec->mmapped_p = 0; 14339 sec->contents = NULL; 14340 elf_section_data (sec)->contents_addr = NULL; 14341 elf_section_data (sec)->contents_size = 0; 14342 } 14343 #endif 14344 } 14345