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 to OBJECT_ID. */ 226 bool 227 bfd_elf_allocate_object (bfd *abfd, 228 size_t object_size, 229 enum elf_target_id object_id) 230 { 231 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); 232 abfd->tdata.any = bfd_zalloc (abfd, object_size); 233 if (abfd->tdata.any == NULL) 234 return false; 235 236 elf_object_id (abfd) = object_id; 237 if (abfd->direction != read_direction) 238 { 239 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); 240 if (o == NULL) 241 return false; 242 elf_tdata (abfd)->o = o; 243 elf_program_header_size (abfd) = (bfd_size_type) -1; 244 } 245 return true; 246 } 247 248 249 bool 250 bfd_elf_make_object (bfd *abfd) 251 { 252 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 253 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), 254 bed->target_id); 255 } 256 257 bool 258 bfd_elf_mkcorefile (bfd *abfd) 259 { 260 /* I think this can be done just like an object file. */ 261 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) 262 return false; 263 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); 264 return elf_tdata (abfd)->core != NULL; 265 } 266 267 char * 268 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) 269 { 270 Elf_Internal_Shdr **i_shdrp; 271 bfd_byte *shstrtab = NULL; 272 file_ptr offset; 273 bfd_size_type shstrtabsize; 274 275 i_shdrp = elf_elfsections (abfd); 276 if (i_shdrp == 0 277 || shindex >= elf_numsections (abfd) 278 || i_shdrp[shindex] == 0) 279 return NULL; 280 281 shstrtab = i_shdrp[shindex]->contents; 282 if (shstrtab == NULL) 283 { 284 /* No cached one, attempt to read, and cache what we read. */ 285 offset = i_shdrp[shindex]->sh_offset; 286 shstrtabsize = i_shdrp[shindex]->sh_size; 287 288 /* Allocate and clear an extra byte at the end, to prevent crashes 289 in case the string table is not terminated. */ 290 if (shstrtabsize + 1 <= 1 291 || bfd_seek (abfd, offset, SEEK_SET) != 0 292 || (shstrtab 293 = _bfd_mmap_readonly_persistent (abfd, shstrtabsize)) == NULL) 294 { 295 /* Once we've failed to read it, make sure we don't keep 296 trying. Otherwise, we'll keep allocating space for 297 the string table over and over. */ 298 i_shdrp[shindex]->sh_size = 0; 299 } 300 else if (shstrtab[shstrtabsize - 1] != '\0') 301 { 302 /* It is an error if a string table isn't terminated. */ 303 _bfd_error_handler 304 /* xgettext:c-format */ 305 (_("%pB(%pA): string table is corrupt"), 306 abfd, i_shdrp[shindex]->bfd_section); 307 return NULL; 308 } 309 i_shdrp[shindex]->contents = shstrtab; 310 } 311 return (char *) shstrtab; 312 } 313 314 char * 315 bfd_elf_string_from_elf_section (bfd *abfd, 316 unsigned int shindex, 317 unsigned int strindex) 318 { 319 Elf_Internal_Shdr *hdr; 320 321 if (strindex == 0) 322 return ""; 323 324 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) 325 return NULL; 326 327 hdr = elf_elfsections (abfd)[shindex]; 328 329 if (hdr->contents == NULL) 330 { 331 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) 332 { 333 /* PR 17512: file: f057ec89. */ 334 /* xgettext:c-format */ 335 _bfd_error_handler (_("%pB: attempt to load strings from" 336 " a non-string section (number %d)"), 337 abfd, shindex); 338 return NULL; 339 } 340 341 if (bfd_elf_get_str_section (abfd, shindex) == NULL) 342 return NULL; 343 } 344 else 345 { 346 /* PR 24273: The string section's contents may have already 347 been loaded elsewhere, eg because a corrupt file has the 348 string section index in the ELF header pointing at a group 349 section. So be paranoid, and test that the last byte of 350 the section is zero. */ 351 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0) 352 return NULL; 353 } 354 355 if (strindex >= hdr->sh_size) 356 { 357 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; 358 _bfd_error_handler 359 /* xgettext:c-format */ 360 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"), 361 abfd, strindex, (uint64_t) hdr->sh_size, 362 (shindex == shstrndx && strindex == hdr->sh_name 363 ? ".shstrtab" 364 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); 365 return NULL; 366 } 367 368 return ((char *) hdr->contents) + strindex; 369 } 370 371 /* Read and convert symbols to internal format. 372 SYMCOUNT specifies the number of symbols to read, starting from 373 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF 374 are non-NULL, they are used to store the internal symbols, external 375 symbols, and symbol section index extensions, respectively. 376 Returns a pointer to the internal symbol buffer (malloced if necessary) 377 or NULL if there were no symbols or some kind of problem. */ 378 379 Elf_Internal_Sym * 380 bfd_elf_get_elf_syms (bfd *ibfd, 381 Elf_Internal_Shdr *symtab_hdr, 382 size_t symcount, 383 size_t symoffset, 384 Elf_Internal_Sym *intsym_buf, 385 void *extsym_buf, 386 Elf_External_Sym_Shndx *extshndx_buf) 387 { 388 Elf_Internal_Shdr *shndx_hdr; 389 void *alloc_ext; 390 const bfd_byte *esym; 391 Elf_External_Sym_Shndx *alloc_extshndx; 392 Elf_External_Sym_Shndx *shndx; 393 Elf_Internal_Sym *alloc_intsym; 394 Elf_Internal_Sym *isym; 395 Elf_Internal_Sym *isymend; 396 const struct elf_backend_data *bed; 397 size_t extsym_size; 398 size_t amt; 399 file_ptr pos; 400 401 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 402 abort (); 403 404 if (symcount == 0) 405 return intsym_buf; 406 407 if (elf_use_dt_symtab_p (ibfd)) 408 { 409 /* Use dynamic symbol table. */ 410 if (elf_tdata (ibfd)->dt_symtab_count != symcount + symoffset) 411 { 412 bfd_set_error (bfd_error_invalid_operation); 413 return NULL; 414 } 415 return elf_tdata (ibfd)->dt_symtab + symoffset; 416 } 417 418 /* Normal syms might have section extension entries. */ 419 shndx_hdr = NULL; 420 if (elf_symtab_shndx_list (ibfd) != NULL) 421 { 422 elf_section_list * entry; 423 Elf_Internal_Shdr **sections = elf_elfsections (ibfd); 424 425 /* Find an index section that is linked to this symtab section. */ 426 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) 427 { 428 /* PR 20063. */ 429 if (entry->hdr.sh_link >= elf_numsections (ibfd)) 430 continue; 431 432 if (sections[entry->hdr.sh_link] == symtab_hdr) 433 { 434 shndx_hdr = & entry->hdr; 435 break; 436 }; 437 } 438 439 if (shndx_hdr == NULL) 440 { 441 if (symtab_hdr == &elf_symtab_hdr (ibfd)) 442 /* Not really accurate, but this was how the old code used 443 to work. */ 444 shndx_hdr = &elf_symtab_shndx_list (ibfd)->hdr; 445 /* Otherwise we do nothing. The assumption is that 446 the index table will not be needed. */ 447 } 448 } 449 450 /* Read the symbols. */ 451 alloc_ext = NULL; 452 alloc_extshndx = NULL; 453 alloc_intsym = NULL; 454 bed = get_elf_backend_data (ibfd); 455 extsym_size = bed->s->sizeof_sym; 456 if (_bfd_mul_overflow (symcount, extsym_size, &amt)) 457 { 458 bfd_set_error (bfd_error_file_too_big); 459 return NULL; 460 } 461 pos = symtab_hdr->sh_offset + symoffset * extsym_size; 462 size_t alloc_ext_size = amt; 463 if (bfd_seek (ibfd, pos, SEEK_SET) != 0 464 || !_bfd_mmap_read_temporary (&extsym_buf, &alloc_ext_size, 465 &alloc_ext, ibfd, false)) 466 { 467 intsym_buf = NULL; 468 goto out2; 469 } 470 471 size_t alloc_extshndx_size = 0; 472 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) 473 extshndx_buf = NULL; 474 else 475 { 476 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt)) 477 { 478 bfd_set_error (bfd_error_file_too_big); 479 intsym_buf = NULL; 480 goto out1; 481 } 482 alloc_extshndx_size = amt; 483 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); 484 if (bfd_seek (ibfd, pos, SEEK_SET) != 0 485 || !_bfd_mmap_read_temporary ((void **) &extshndx_buf, 486 &alloc_extshndx_size, 487 (void **) &alloc_extshndx, 488 ibfd, false)) 489 { 490 intsym_buf = NULL; 491 goto out1; 492 } 493 } 494 495 if (intsym_buf == NULL) 496 { 497 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt)) 498 { 499 bfd_set_error (bfd_error_file_too_big); 500 goto out1; 501 } 502 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt); 503 intsym_buf = alloc_intsym; 504 if (intsym_buf == NULL) 505 goto out1; 506 } 507 508 /* Convert the symbols to internal form. */ 509 isymend = intsym_buf + symcount; 510 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, 511 shndx = extshndx_buf; 512 isym < isymend; 513 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) 514 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) 515 { 516 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; 517 /* xgettext:c-format */ 518 _bfd_error_handler (_("%pB symbol number %lu references" 519 " nonexistent SHT_SYMTAB_SHNDX section"), 520 ibfd, (unsigned long) symoffset); 521 free (alloc_intsym); 522 intsym_buf = NULL; 523 goto out1; 524 } 525 526 out1: 527 _bfd_munmap_readonly_temporary (alloc_extshndx, alloc_extshndx_size); 528 out2: 529 _bfd_munmap_readonly_temporary (alloc_ext, alloc_ext_size); 530 531 return intsym_buf; 532 } 533 534 /* Look up a symbol name. */ 535 const char * 536 bfd_elf_sym_name (bfd *abfd, 537 Elf_Internal_Shdr *symtab_hdr, 538 Elf_Internal_Sym *isym, 539 asection *sym_sec) 540 { 541 const char *name; 542 unsigned int iname = isym->st_name; 543 unsigned int shindex = symtab_hdr->sh_link; 544 545 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION 546 /* Check for a bogus st_shndx to avoid crashing. */ 547 && isym->st_shndx < elf_numsections (abfd)) 548 { 549 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; 550 shindex = elf_elfheader (abfd)->e_shstrndx; 551 } 552 553 name = bfd_elf_string_from_elf_section (abfd, shindex, iname); 554 if (name == NULL) 555 name = "(null)"; 556 else if (sym_sec && *name == '\0') 557 name = bfd_section_name (sym_sec); 558 559 return name; 560 } 561 562 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP 563 sections. The first element is the flags, the rest are section 564 pointers. */ 565 566 typedef union elf_internal_group { 567 Elf_Internal_Shdr *shdr; 568 unsigned int flags; 569 } Elf_Internal_Group; 570 571 /* Return the name of the group signature symbol. Why isn't the 572 signature just a string? */ 573 574 static const char * 575 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) 576 { 577 Elf_Internal_Shdr *hdr; 578 unsigned char esym[sizeof (Elf64_External_Sym)]; 579 Elf_External_Sym_Shndx eshndx; 580 Elf_Internal_Sym isym; 581 582 /* First we need to ensure the symbol table is available. Make sure 583 that it is a symbol table section. */ 584 if (ghdr->sh_link >= elf_numsections (abfd)) 585 return NULL; 586 hdr = elf_elfsections (abfd) [ghdr->sh_link]; 587 if (hdr->sh_type != SHT_SYMTAB 588 || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) 589 return NULL; 590 591 /* Go read the symbol. */ 592 hdr = &elf_tdata (abfd)->symtab_hdr; 593 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, 594 &isym, esym, &eshndx) == NULL) 595 return NULL; 596 597 return bfd_elf_sym_name (abfd, hdr, &isym, NULL); 598 } 599 600 /* Set next_in_group list pointer, and group name for NEWSECT. */ 601 602 static bool 603 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) 604 { 605 unsigned int num_group = elf_tdata (abfd)->num_group; 606 607 /* If num_group is zero, read in all SHT_GROUP sections. The count 608 is set to -1 if there are no SHT_GROUP sections. */ 609 if (num_group == 0) 610 { 611 unsigned int i, shnum; 612 613 /* First count the number of groups. If we have a SHT_GROUP 614 section with just a flag word (ie. sh_size is 4), ignore it. */ 615 shnum = elf_numsections (abfd); 616 num_group = 0; 617 618 #define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ 619 ( (shdr)->sh_type == SHT_GROUP \ 620 && (shdr)->sh_size >= minsize \ 621 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ 622 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) 623 624 for (i = 0; i < shnum; i++) 625 { 626 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; 627 628 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) 629 num_group += 1; 630 } 631 632 if (num_group == 0) 633 { 634 num_group = (unsigned) -1; 635 elf_tdata (abfd)->num_group = num_group; 636 elf_tdata (abfd)->group_sect_ptr = NULL; 637 } 638 else 639 { 640 /* We keep a list of elf section headers for group sections, 641 so we can find them quickly. */ 642 size_t amt; 643 644 elf_tdata (abfd)->num_group = num_group; 645 amt = num_group * sizeof (Elf_Internal_Shdr *); 646 elf_tdata (abfd)->group_sect_ptr 647 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt); 648 if (elf_tdata (abfd)->group_sect_ptr == NULL) 649 return false; 650 num_group = 0; 651 652 for (i = 0; i < shnum; i++) 653 { 654 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; 655 656 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) 657 { 658 unsigned char *src; 659 Elf_Internal_Group *dest; 660 661 /* Make sure the group section has a BFD section 662 attached to it. */ 663 if (!bfd_section_from_shdr (abfd, i)) 664 return false; 665 666 /* Add to list of sections. */ 667 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; 668 num_group += 1; 669 670 /* Read the raw contents. */ 671 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0); 672 shdr->contents = NULL; 673 if (_bfd_mul_overflow (shdr->sh_size, 674 sizeof (*dest) / 4, &amt) 675 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 676 || !(shdr->contents 677 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size))) 678 { 679 _bfd_error_handler 680 /* xgettext:c-format */ 681 (_("%pB: invalid size field in group section" 682 " header: %#" PRIx64 ""), 683 abfd, (uint64_t) shdr->sh_size); 684 bfd_set_error (bfd_error_bad_value); 685 -- num_group; 686 continue; 687 } 688 689 /* Translate raw contents, a flag word followed by an 690 array of elf section indices all in target byte order, 691 to the flag word followed by an array of elf section 692 pointers. */ 693 src = shdr->contents + shdr->sh_size; 694 dest = (Elf_Internal_Group *) (shdr->contents + amt); 695 696 while (1) 697 { 698 unsigned int idx; 699 700 src -= 4; 701 --dest; 702 idx = H_GET_32 (abfd, src); 703 if (src == shdr->contents) 704 { 705 dest->shdr = NULL; 706 dest->flags = idx; 707 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) 708 shdr->bfd_section->flags 709 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 710 break; 711 } 712 if (idx < shnum) 713 { 714 dest->shdr = elf_elfsections (abfd)[idx]; 715 /* PR binutils/23199: All sections in a 716 section group should be marked with 717 SHF_GROUP. But some tools generate 718 broken objects without SHF_GROUP. Fix 719 them up here. */ 720 dest->shdr->sh_flags |= SHF_GROUP; 721 } 722 if (idx >= shnum 723 || dest->shdr->sh_type == SHT_GROUP) 724 { 725 _bfd_error_handler 726 (_("%pB: invalid entry in SHT_GROUP section [%u]"), 727 abfd, i); 728 dest->shdr = NULL; 729 } 730 } 731 } 732 } 733 734 /* PR 17510: Corrupt binaries might contain invalid groups. */ 735 if (num_group != (unsigned) elf_tdata (abfd)->num_group) 736 { 737 elf_tdata (abfd)->num_group = num_group; 738 739 /* If all groups are invalid then fail. */ 740 if (num_group == 0) 741 { 742 elf_tdata (abfd)->group_sect_ptr = NULL; 743 elf_tdata (abfd)->num_group = num_group = -1; 744 _bfd_error_handler 745 (_("%pB: no valid group sections found"), abfd); 746 bfd_set_error (bfd_error_bad_value); 747 } 748 } 749 } 750 } 751 752 if (num_group != (unsigned) -1) 753 { 754 unsigned int search_offset = elf_tdata (abfd)->group_search_offset; 755 unsigned int j; 756 757 for (j = 0; j < num_group; j++) 758 { 759 /* Begin search from previous found group. */ 760 unsigned i = (j + search_offset) % num_group; 761 762 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; 763 Elf_Internal_Group *idx; 764 bfd_size_type n_elt; 765 766 if (shdr == NULL) 767 continue; 768 769 idx = (Elf_Internal_Group *) shdr->contents; 770 if (idx == NULL || shdr->sh_size < 4) 771 { 772 /* See PR 21957 for a reproducer. */ 773 /* xgettext:c-format */ 774 _bfd_error_handler (_("%pB: group section '%pA' has no contents"), 775 abfd, shdr->bfd_section); 776 elf_tdata (abfd)->group_sect_ptr[i] = NULL; 777 bfd_set_error (bfd_error_bad_value); 778 return false; 779 } 780 n_elt = shdr->sh_size / 4; 781 782 /* Look through this group's sections to see if current 783 section is a member. */ 784 while (--n_elt != 0) 785 if ((++idx)->shdr == hdr) 786 { 787 asection *s = NULL; 788 789 /* We are a member of this group. Go looking through 790 other members to see if any others are linked via 791 next_in_group. */ 792 idx = (Elf_Internal_Group *) shdr->contents; 793 n_elt = shdr->sh_size / 4; 794 while (--n_elt != 0) 795 if ((++idx)->shdr != NULL 796 && (s = idx->shdr->bfd_section) != NULL 797 && elf_next_in_group (s) != NULL) 798 break; 799 if (n_elt != 0) 800 { 801 /* Snarf the group name from other member, and 802 insert current section in circular list. */ 803 elf_group_name (newsect) = elf_group_name (s); 804 elf_next_in_group (newsect) = elf_next_in_group (s); 805 elf_next_in_group (s) = newsect; 806 } 807 else 808 { 809 const char *gname; 810 811 gname = group_signature (abfd, shdr); 812 if (gname == NULL) 813 return false; 814 elf_group_name (newsect) = gname; 815 816 /* Start a circular list with one element. */ 817 elf_next_in_group (newsect) = newsect; 818 } 819 820 /* If the group section has been created, point to the 821 new member. */ 822 if (shdr->bfd_section != NULL) 823 elf_next_in_group (shdr->bfd_section) = newsect; 824 825 elf_tdata (abfd)->group_search_offset = i; 826 j = num_group - 1; 827 break; 828 } 829 } 830 } 831 832 if (elf_group_name (newsect) == NULL) 833 { 834 /* xgettext:c-format */ 835 _bfd_error_handler (_("%pB: no group info for section '%pA'"), 836 abfd, newsect); 837 /* PR 29532: Return true here, even though the group info has not been 838 read. Separate debug info files can have empty group sections, but 839 we do not want this to prevent them from being loaded as otherwise 840 GDB will not be able to use them. */ 841 return true; 842 } 843 return true; 844 } 845 846 bool 847 _bfd_elf_setup_sections (bfd *abfd) 848 { 849 unsigned int i; 850 unsigned int num_group = elf_tdata (abfd)->num_group; 851 bool result = true; 852 asection *s; 853 854 /* Process SHF_LINK_ORDER. */ 855 for (s = abfd->sections; s != NULL; s = s->next) 856 { 857 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; 858 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) 859 { 860 unsigned int elfsec = this_hdr->sh_link; 861 /* An sh_link value of 0 is now allowed. It indicates that linked 862 to section has already been discarded, but that the current 863 section has been retained for some other reason. This linking 864 section is still a candidate for later garbage collection 865 however. */ 866 if (elfsec == 0) 867 { 868 elf_linked_to_section (s) = NULL; 869 } 870 else 871 { 872 asection *linksec = NULL; 873 874 if (elfsec < elf_numsections (abfd)) 875 { 876 this_hdr = elf_elfsections (abfd)[elfsec]; 877 linksec = this_hdr->bfd_section; 878 } 879 880 /* PR 1991, 2008: 881 Some strip/objcopy may leave an incorrect value in 882 sh_link. We don't want to proceed. */ 883 if (linksec == NULL) 884 { 885 _bfd_error_handler 886 /* xgettext:c-format */ 887 (_("%pB: sh_link [%d] in section `%pA' is incorrect"), 888 s->owner, elfsec, s); 889 result = false; 890 } 891 892 elf_linked_to_section (s) = linksec; 893 } 894 } 895 else if (this_hdr->sh_type == SHT_GROUP 896 && elf_next_in_group (s) == NULL) 897 { 898 _bfd_error_handler 899 /* xgettext:c-format */ 900 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"), 901 abfd, elf_section_data (s)->this_idx); 902 result = false; 903 } 904 } 905 906 /* Process section groups. */ 907 if (num_group == (unsigned) -1) 908 return result; 909 910 for (i = 0; i < num_group; i++) 911 { 912 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; 913 Elf_Internal_Group *idx; 914 unsigned int n_elt; 915 916 /* PR binutils/18758: Beware of corrupt binaries with invalid 917 group data. */ 918 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) 919 { 920 _bfd_error_handler 921 /* xgettext:c-format */ 922 (_("%pB: section group entry number %u is corrupt"), 923 abfd, i); 924 result = false; 925 continue; 926 } 927 928 idx = (Elf_Internal_Group *) shdr->contents; 929 n_elt = shdr->sh_size / 4; 930 931 while (--n_elt != 0) 932 { 933 ++ idx; 934 935 if (idx->shdr == NULL) 936 continue; 937 else if (idx->shdr->bfd_section) 938 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; 939 else if (idx->shdr->sh_type != SHT_RELA 940 && idx->shdr->sh_type != SHT_REL) 941 { 942 /* There are some unknown sections in the group. */ 943 _bfd_error_handler 944 /* xgettext:c-format */ 945 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"), 946 abfd, 947 idx->shdr->sh_type, 948 bfd_elf_string_from_elf_section (abfd, 949 (elf_elfheader (abfd) 950 ->e_shstrndx), 951 idx->shdr->sh_name), 952 shdr->bfd_section); 953 result = false; 954 } 955 } 956 } 957 958 return result; 959 } 960 961 bool 962 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) 963 { 964 return elf_next_in_group (sec) != NULL; 965 } 966 967 const char * 968 bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) 969 { 970 if (elf_sec_group (sec) != NULL) 971 return elf_group_name (sec); 972 return NULL; 973 } 974 975 /* Make a BFD section from an ELF section. We store a pointer to the 976 BFD section in the bfd_section field of the header. */ 977 978 bool 979 _bfd_elf_make_section_from_shdr (bfd *abfd, 980 Elf_Internal_Shdr *hdr, 981 const char *name, 982 int shindex) 983 { 984 asection *newsect; 985 flagword flags; 986 const struct elf_backend_data *bed; 987 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 988 989 if (hdr->bfd_section != NULL) 990 return true; 991 992 newsect = bfd_make_section_anyway (abfd, name); 993 if (newsect == NULL) 994 return false; 995 996 hdr->bfd_section = newsect; 997 elf_section_data (newsect)->this_hdr = *hdr; 998 elf_section_data (newsect)->this_idx = shindex; 999 1000 /* Always use the real type/flags. */ 1001 elf_section_type (newsect) = hdr->sh_type; 1002 elf_section_flags (newsect) = hdr->sh_flags; 1003 1004 newsect->filepos = hdr->sh_offset; 1005 1006 flags = SEC_NO_FLAGS; 1007 if (hdr->sh_type != SHT_NOBITS) 1008 flags |= SEC_HAS_CONTENTS; 1009 if (hdr->sh_type == SHT_GROUP) 1010 flags |= SEC_GROUP; 1011 if ((hdr->sh_flags & SHF_ALLOC) != 0) 1012 { 1013 flags |= SEC_ALLOC; 1014 if (hdr->sh_type != SHT_NOBITS) 1015 flags |= SEC_LOAD; 1016 } 1017 if ((hdr->sh_flags & SHF_WRITE) == 0) 1018 flags |= SEC_READONLY; 1019 if ((hdr->sh_flags & SHF_EXECINSTR) != 0) 1020 flags |= SEC_CODE; 1021 else if ((flags & SEC_LOAD) != 0) 1022 flags |= SEC_DATA; 1023 if ((hdr->sh_flags & SHF_MERGE) != 0) 1024 { 1025 flags |= SEC_MERGE; 1026 newsect->entsize = hdr->sh_entsize; 1027 } 1028 if ((hdr->sh_flags & SHF_STRINGS) != 0) 1029 flags |= SEC_STRINGS; 1030 if (hdr->sh_flags & SHF_GROUP) 1031 if (!setup_group (abfd, hdr, newsect)) 1032 return false; 1033 if ((hdr->sh_flags & SHF_TLS) != 0) 1034 flags |= SEC_THREAD_LOCAL; 1035 if ((hdr->sh_flags & SHF_EXCLUDE) != 0) 1036 flags |= SEC_EXCLUDE; 1037 1038 switch (elf_elfheader (abfd)->e_ident[EI_OSABI]) 1039 { 1040 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE, 1041 but binutils as of 2019-07-23 did not set the EI_OSABI header 1042 byte. */ 1043 case ELFOSABI_GNU: 1044 case ELFOSABI_FREEBSD: 1045 if ((hdr->sh_flags & SHF_GNU_RETAIN) != 0) 1046 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_retain; 1047 /* Fall through */ 1048 case ELFOSABI_NONE: 1049 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0) 1050 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind; 1051 break; 1052 } 1053 1054 if ((flags & SEC_ALLOC) == 0) 1055 { 1056 /* The debugging sections appear to be recognized only by name, 1057 not any sort of flag. Their SEC_ALLOC bits are cleared. */ 1058 if (name [0] == '.') 1059 { 1060 if (startswith (name, ".debug") 1061 || startswith (name, ".gnu.debuglto_.debug_") 1062 || startswith (name, ".gnu.linkonce.wi.") 1063 || startswith (name, ".zdebug")) 1064 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS; 1065 else if (startswith (name, GNU_BUILD_ATTRS_SECTION_NAME) 1066 || startswith (name, ".note.gnu")) 1067 { 1068 flags |= SEC_ELF_OCTETS; 1069 opb = 1; 1070 } 1071 else if (startswith (name, ".line") 1072 || startswith (name, ".stab") 1073 || strcmp (name, ".gdb_index") == 0) 1074 flags |= SEC_DEBUGGING; 1075 } 1076 } 1077 1078 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb) 1079 || !bfd_set_section_size (newsect, hdr->sh_size) 1080 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign 1081 & -hdr->sh_addralign))) 1082 return false; 1083 1084 /* As a GNU extension, if the name begins with .gnu.linkonce, we 1085 only link a single copy of the section. This is used to support 1086 g++. g++ will emit each template expansion in its own section. 1087 The symbols will be defined as weak, so that multiple definitions 1088 are permitted. The GNU linker extension is to actually discard 1089 all but one of the sections. */ 1090 if (startswith (name, ".gnu.linkonce") 1091 && elf_next_in_group (newsect) == NULL) 1092 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 1093 1094 if (!bfd_set_section_flags (newsect, flags)) 1095 return false; 1096 1097 bed = get_elf_backend_data (abfd); 1098 if (bed->elf_backend_section_flags) 1099 if (!bed->elf_backend_section_flags (hdr)) 1100 return false; 1101 1102 /* We do not parse the PT_NOTE segments as we are interested even in the 1103 separate debug info files which may have the segments offsets corrupted. 1104 PT_NOTEs from the core files are currently not parsed using BFD. */ 1105 if (hdr->sh_type == SHT_NOTE && hdr->sh_size != 0) 1106 { 1107 bfd_byte *contents; 1108 1109 if (!_bfd_elf_mmap_section_contents (abfd, newsect, &contents)) 1110 return false; 1111 1112 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, 1113 hdr->sh_offset, hdr->sh_addralign); 1114 _bfd_elf_munmap_section_contents (newsect, contents); 1115 } 1116 1117 if ((newsect->flags & SEC_ALLOC) != 0) 1118 { 1119 Elf_Internal_Phdr *phdr; 1120 unsigned int i, nload; 1121 1122 /* Some ELF linkers produce binaries with all the program header 1123 p_paddr fields zero. If we have such a binary with more than 1124 one PT_LOAD header, then leave the section lma equal to vma 1125 so that we don't create sections with overlapping lma. */ 1126 phdr = elf_tdata (abfd)->phdr; 1127 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 1128 if (phdr->p_paddr != 0) 1129 break; 1130 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) 1131 ++nload; 1132 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) 1133 return true; 1134 1135 phdr = elf_tdata (abfd)->phdr; 1136 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 1137 { 1138 if (((phdr->p_type == PT_LOAD 1139 && (hdr->sh_flags & SHF_TLS) == 0) 1140 || phdr->p_type == PT_TLS) 1141 && ELF_SECTION_IN_SEGMENT (hdr, phdr)) 1142 { 1143 if ((newsect->flags & SEC_LOAD) == 0) 1144 newsect->lma = (phdr->p_paddr 1145 + hdr->sh_addr - phdr->p_vaddr) / opb; 1146 else 1147 /* We used to use the same adjustment for SEC_LOAD 1148 sections, but that doesn't work if the segment 1149 is packed with code from multiple VMAs. 1150 Instead we calculate the section LMA based on 1151 the segment LMA. It is assumed that the 1152 segment will contain sections with contiguous 1153 LMAs, even if the VMAs are not. */ 1154 newsect->lma = (phdr->p_paddr 1155 + hdr->sh_offset - phdr->p_offset) / opb; 1156 1157 /* With contiguous segments, we can't tell from file 1158 offsets whether a section with zero size should 1159 be placed at the end of one segment or the 1160 beginning of the next. Decide based on vaddr. */ 1161 if (hdr->sh_addr >= phdr->p_vaddr 1162 && (hdr->sh_addr + hdr->sh_size 1163 <= phdr->p_vaddr + phdr->p_memsz)) 1164 break; 1165 } 1166 } 1167 } 1168 1169 /* Compress/decompress DWARF debug sections with names: .debug_*, 1170 .zdebug_*, .gnu.debuglto_.debug_, after the section flags is set. */ 1171 if ((newsect->flags & SEC_DEBUGGING) != 0 1172 && (newsect->flags & SEC_HAS_CONTENTS) != 0 1173 && (newsect->flags & SEC_ELF_OCTETS) != 0) 1174 { 1175 enum { nothing, compress, decompress } action = nothing; 1176 int compression_header_size; 1177 bfd_size_type uncompressed_size; 1178 unsigned int uncompressed_align_power; 1179 enum compression_type ch_type = ch_none; 1180 bool compressed 1181 = bfd_is_section_compressed_info (abfd, newsect, 1182 &compression_header_size, 1183 &uncompressed_size, 1184 &uncompressed_align_power, 1185 &ch_type); 1186 1187 /* Should we decompress? */ 1188 if ((abfd->flags & BFD_DECOMPRESS) != 0 && compressed) 1189 action = decompress; 1190 1191 /* Should we compress? Or convert to a different compression? */ 1192 else if ((abfd->flags & BFD_COMPRESS) != 0 1193 && newsect->size != 0 1194 && compression_header_size >= 0 1195 && uncompressed_size > 0) 1196 { 1197 if (!compressed) 1198 action = compress; 1199 else 1200 { 1201 enum compression_type new_ch_type = ch_none; 1202 if ((abfd->flags & BFD_COMPRESS_GABI) != 0) 1203 new_ch_type = ((abfd->flags & BFD_COMPRESS_ZSTD) != 0 1204 ? ch_compress_zstd : ch_compress_zlib); 1205 if (new_ch_type != ch_type) 1206 action = compress; 1207 } 1208 } 1209 1210 if (action == compress) 1211 { 1212 if (!bfd_init_section_compress_status (abfd, newsect)) 1213 { 1214 _bfd_error_handler 1215 /* xgettext:c-format */ 1216 (_("%pB: unable to compress section %s"), abfd, name); 1217 return false; 1218 } 1219 } 1220 else if (action == decompress) 1221 { 1222 if (!bfd_init_section_decompress_status (abfd, newsect)) 1223 { 1224 _bfd_error_handler 1225 /* xgettext:c-format */ 1226 (_("%pB: unable to decompress section %s"), abfd, name); 1227 return false; 1228 } 1229 #ifndef HAVE_ZSTD 1230 if (newsect->compress_status == DECOMPRESS_SECTION_ZSTD) 1231 { 1232 _bfd_error_handler 1233 /* xgettext:c-format */ 1234 (_ ("%pB: section %s is compressed with zstd, but BFD " 1235 "is not built with zstd support"), 1236 abfd, name); 1237 newsect->compress_status = COMPRESS_SECTION_NONE; 1238 return false; 1239 } 1240 #endif 1241 if (abfd->is_linker_input 1242 && name[1] == 'z') 1243 { 1244 /* Rename section from .zdebug_* to .debug_* so that ld 1245 scripts will see this section as a debug section. */ 1246 char *new_name = bfd_zdebug_name_to_debug (abfd, name); 1247 if (new_name == NULL) 1248 return false; 1249 bfd_rename_section (newsect, new_name); 1250 } 1251 } 1252 } 1253 1254 return true; 1255 } 1256 1257 const char *const bfd_elf_section_type_names[] = 1258 { 1259 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", 1260 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", 1261 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", 1262 }; 1263 1264 /* ELF relocs are against symbols. If we are producing relocatable 1265 output, and the reloc is against an external symbol, and nothing 1266 has given us any additional addend, the resulting reloc will also 1267 be against the same symbol. In such a case, we don't want to 1268 change anything about the way the reloc is handled, since it will 1269 all be done at final link time. Rather than put special case code 1270 into bfd_perform_relocation, all the reloc types use this howto 1271 function, or should call this function for relocatable output. */ 1272 1273 bfd_reloc_status_type 1274 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, 1275 arelent *reloc_entry, 1276 asymbol *symbol, 1277 void *data ATTRIBUTE_UNUSED, 1278 asection *input_section, 1279 bfd *output_bfd, 1280 char **error_message ATTRIBUTE_UNUSED) 1281 { 1282 if (output_bfd != NULL 1283 && (symbol->flags & BSF_SECTION_SYM) == 0 1284 && (! reloc_entry->howto->partial_inplace 1285 || reloc_entry->addend == 0)) 1286 { 1287 reloc_entry->address += input_section->output_offset; 1288 return bfd_reloc_ok; 1289 } 1290 1291 /* In some cases the relocation should be treated as output section 1292 relative, as when linking ELF DWARF into PE COFF. Many ELF 1293 targets lack section relative relocations and instead use 1294 ordinary absolute relocations for references between DWARF 1295 sections. That is arguably a bug in those targets but it happens 1296 to work for the usual case of linking to non-loaded ELF debug 1297 sections with VMAs forced to zero. PE COFF on the other hand 1298 doesn't allow a section VMA of zero. */ 1299 if (output_bfd == NULL 1300 && !reloc_entry->howto->pc_relative 1301 && (symbol->section->flags & SEC_DEBUGGING) != 0 1302 && (input_section->flags & SEC_DEBUGGING) != 0) 1303 reloc_entry->addend -= symbol->section->output_section->vma; 1304 1305 return bfd_reloc_continue; 1306 } 1307 1308 /* Returns TRUE if section A matches section B. 1309 Names, addresses and links may be different, but everything else 1310 should be the same. */ 1311 1312 static bool 1313 section_match (const Elf_Internal_Shdr * a, 1314 const Elf_Internal_Shdr * b) 1315 { 1316 if (a->sh_type != b->sh_type 1317 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0 1318 || a->sh_addralign != b->sh_addralign 1319 || a->sh_entsize != b->sh_entsize) 1320 return false; 1321 if (a->sh_type == SHT_SYMTAB 1322 || a->sh_type == SHT_STRTAB) 1323 return true; 1324 return a->sh_size == b->sh_size; 1325 } 1326 1327 /* Find a section in OBFD that has the same characteristics 1328 as IHEADER. Return the index of this section or SHN_UNDEF if 1329 none can be found. Check's section HINT first, as this is likely 1330 to be the correct section. */ 1331 1332 static unsigned int 1333 find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader, 1334 const unsigned int hint) 1335 { 1336 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); 1337 unsigned int i; 1338 1339 BFD_ASSERT (iheader != NULL); 1340 1341 /* See PR 20922 for a reproducer of the NULL test. */ 1342 if (hint < elf_numsections (obfd) 1343 && oheaders[hint] != NULL 1344 && section_match (oheaders[hint], iheader)) 1345 return hint; 1346 1347 for (i = 1; i < elf_numsections (obfd); i++) 1348 { 1349 Elf_Internal_Shdr * oheader = oheaders[i]; 1350 1351 if (oheader == NULL) 1352 continue; 1353 if (section_match (oheader, iheader)) 1354 /* FIXME: Do we care if there is a potential for 1355 multiple matches ? */ 1356 return i; 1357 } 1358 1359 return SHN_UNDEF; 1360 } 1361 1362 /* PR 19938: Attempt to set the ELF section header fields of an OS or 1363 Processor specific section, based upon a matching input section. 1364 Returns TRUE upon success, FALSE otherwise. */ 1365 1366 static bool 1367 copy_special_section_fields (const bfd *ibfd, 1368 bfd *obfd, 1369 const Elf_Internal_Shdr *iheader, 1370 Elf_Internal_Shdr *oheader, 1371 const unsigned int secnum) 1372 { 1373 const struct elf_backend_data *bed = get_elf_backend_data (obfd); 1374 const Elf_Internal_Shdr **iheaders 1375 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); 1376 bool changed = false; 1377 unsigned int sh_link; 1378 1379 if (oheader->sh_type == SHT_NOBITS) 1380 { 1381 /* This is a feature for objcopy --only-keep-debug: 1382 When a section's type is changed to NOBITS, we preserve 1383 the sh_link and sh_info fields so that they can be 1384 matched up with the original. 1385 1386 Note: Strictly speaking these assignments are wrong. 1387 The sh_link and sh_info fields should point to the 1388 relevent sections in the output BFD, which may not be in 1389 the same location as they were in the input BFD. But 1390 the whole point of this action is to preserve the 1391 original values of the sh_link and sh_info fields, so 1392 that they can be matched up with the section headers in 1393 the original file. So strictly speaking we may be 1394 creating an invalid ELF file, but it is only for a file 1395 that just contains debug info and only for sections 1396 without any contents. */ 1397 if (oheader->sh_link == 0) 1398 oheader->sh_link = iheader->sh_link; 1399 if (oheader->sh_info == 0) 1400 oheader->sh_info = iheader->sh_info; 1401 return true; 1402 } 1403 1404 /* Allow the target a chance to decide how these fields should be set. */ 1405 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd, 1406 iheader, oheader)) 1407 return true; 1408 1409 /* We have an iheader which might match oheader, and which has non-zero 1410 sh_info and/or sh_link fields. Attempt to follow those links and find 1411 the section in the output bfd which corresponds to the linked section 1412 in the input bfd. */ 1413 if (iheader->sh_link != SHN_UNDEF) 1414 { 1415 /* See PR 20931 for a reproducer. */ 1416 if (iheader->sh_link >= elf_numsections (ibfd)) 1417 { 1418 _bfd_error_handler 1419 /* xgettext:c-format */ 1420 (_("%pB: invalid sh_link field (%d) in section number %d"), 1421 ibfd, iheader->sh_link, secnum); 1422 return false; 1423 } 1424 1425 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); 1426 if (sh_link != SHN_UNDEF) 1427 { 1428 oheader->sh_link = sh_link; 1429 changed = true; 1430 } 1431 else 1432 /* FIXME: Should we install iheader->sh_link 1433 if we could not find a match ? */ 1434 _bfd_error_handler 1435 /* xgettext:c-format */ 1436 (_("%pB: failed to find link section for section %d"), obfd, secnum); 1437 } 1438 1439 if (iheader->sh_info) 1440 { 1441 /* The sh_info field can hold arbitrary information, but if the 1442 SHF_LINK_INFO flag is set then it should be interpreted as a 1443 section index. */ 1444 if (iheader->sh_flags & SHF_INFO_LINK) 1445 { 1446 sh_link = find_link (obfd, iheaders[iheader->sh_info], 1447 iheader->sh_info); 1448 if (sh_link != SHN_UNDEF) 1449 oheader->sh_flags |= SHF_INFO_LINK; 1450 } 1451 else 1452 /* No idea what it means - just copy it. */ 1453 sh_link = iheader->sh_info; 1454 1455 if (sh_link != SHN_UNDEF) 1456 { 1457 oheader->sh_info = sh_link; 1458 changed = true; 1459 } 1460 else 1461 _bfd_error_handler 1462 /* xgettext:c-format */ 1463 (_("%pB: failed to find info section for section %d"), obfd, secnum); 1464 } 1465 1466 return changed; 1467 } 1468 1469 /* Copy the program header and other data from one object module to 1470 another. */ 1471 1472 bool 1473 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) 1474 { 1475 const Elf_Internal_Shdr **iheaders 1476 = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); 1477 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); 1478 const struct elf_backend_data *bed; 1479 unsigned int i; 1480 1481 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 1482 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 1483 return true; 1484 1485 if (!elf_flags_init (obfd)) 1486 { 1487 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; 1488 elf_flags_init (obfd) = true; 1489 } 1490 1491 elf_gp (obfd) = elf_gp (ibfd); 1492 1493 /* Also copy the EI_OSABI field. */ 1494 elf_elfheader (obfd)->e_ident[EI_OSABI] = 1495 elf_elfheader (ibfd)->e_ident[EI_OSABI]; 1496 1497 /* If set, copy the EI_ABIVERSION field. */ 1498 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) 1499 elf_elfheader (obfd)->e_ident[EI_ABIVERSION] 1500 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; 1501 1502 /* Copy object attributes. */ 1503 _bfd_elf_copy_obj_attributes (ibfd, obfd); 1504 1505 if (iheaders == NULL || oheaders == NULL) 1506 return true; 1507 1508 bed = get_elf_backend_data (obfd); 1509 1510 /* Possibly copy other fields in the section header. */ 1511 for (i = 1; i < elf_numsections (obfd); i++) 1512 { 1513 unsigned int j; 1514 Elf_Internal_Shdr * oheader = oheaders[i]; 1515 1516 /* Ignore ordinary sections. SHT_NOBITS sections are considered however 1517 because of a special case need for generating separate debug info 1518 files. See below for more details. */ 1519 if (oheader == NULL 1520 || (oheader->sh_type != SHT_NOBITS 1521 && oheader->sh_type < SHT_LOOS)) 1522 continue; 1523 1524 /* Ignore empty sections, and sections whose 1525 fields have already been initialised. */ 1526 if (oheader->sh_size == 0 1527 || (oheader->sh_info != 0 && oheader->sh_link != 0)) 1528 continue; 1529 1530 /* Scan for the matching section in the input bfd. 1531 First we try for a direct mapping between the input and 1532 output sections. */ 1533 for (j = 1; j < elf_numsections (ibfd); j++) 1534 { 1535 const Elf_Internal_Shdr * iheader = iheaders[j]; 1536 1537 if (iheader == NULL) 1538 continue; 1539 1540 if (oheader->bfd_section != NULL 1541 && iheader->bfd_section != NULL 1542 && iheader->bfd_section->output_section != NULL 1543 && iheader->bfd_section->output_section == oheader->bfd_section) 1544 { 1545 /* We have found a connection from the input section to 1546 the output section. Attempt to copy the header fields. 1547 If this fails then do not try any further sections - 1548 there should only be a one-to-one mapping between 1549 input and output. */ 1550 if (!copy_special_section_fields (ibfd, obfd, 1551 iheader, oheader, i)) 1552 j = elf_numsections (ibfd); 1553 break; 1554 } 1555 } 1556 1557 if (j < elf_numsections (ibfd)) 1558 continue; 1559 1560 /* That failed. So try to deduce the corresponding input section. 1561 Unfortunately we cannot compare names as the output string table 1562 is empty, so instead we check size, address and type. */ 1563 for (j = 1; j < elf_numsections (ibfd); j++) 1564 { 1565 const Elf_Internal_Shdr * iheader = iheaders[j]; 1566 1567 if (iheader == NULL) 1568 continue; 1569 1570 /* Try matching fields in the input section's header. 1571 Since --only-keep-debug turns all non-debug sections into 1572 SHT_NOBITS sections, the output SHT_NOBITS type matches any 1573 input type. */ 1574 if ((oheader->sh_type == SHT_NOBITS 1575 || iheader->sh_type == oheader->sh_type) 1576 && (iheader->sh_flags & ~ SHF_INFO_LINK) 1577 == (oheader->sh_flags & ~ SHF_INFO_LINK) 1578 && iheader->sh_addralign == oheader->sh_addralign 1579 && iheader->sh_entsize == oheader->sh_entsize 1580 && iheader->sh_size == oheader->sh_size 1581 && iheader->sh_addr == oheader->sh_addr 1582 && (iheader->sh_info != oheader->sh_info 1583 || iheader->sh_link != oheader->sh_link)) 1584 { 1585 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) 1586 break; 1587 } 1588 } 1589 1590 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) 1591 { 1592 /* Final attempt. Call the backend copy function 1593 with a NULL input section. */ 1594 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd, 1595 NULL, oheader); 1596 } 1597 } 1598 1599 return true; 1600 } 1601 1602 static const char * 1603 get_segment_type (unsigned int p_type) 1604 { 1605 const char *pt; 1606 switch (p_type) 1607 { 1608 case PT_NULL: pt = "NULL"; break; 1609 case PT_LOAD: pt = "LOAD"; break; 1610 case PT_DYNAMIC: pt = "DYNAMIC"; break; 1611 case PT_INTERP: pt = "INTERP"; break; 1612 case PT_NOTE: pt = "NOTE"; break; 1613 case PT_SHLIB: pt = "SHLIB"; break; 1614 case PT_PHDR: pt = "PHDR"; break; 1615 case PT_TLS: pt = "TLS"; break; 1616 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; 1617 case PT_GNU_STACK: pt = "STACK"; break; 1618 case PT_GNU_RELRO: pt = "RELRO"; break; 1619 case PT_GNU_SFRAME: pt = "SFRAME"; break; 1620 default: pt = NULL; break; 1621 } 1622 return pt; 1623 } 1624 1625 /* Print out the program headers. */ 1626 1627 bool 1628 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) 1629 { 1630 FILE *f = (FILE *) farg; 1631 Elf_Internal_Phdr *p; 1632 asection *s; 1633 bfd_byte *dynbuf = NULL; 1634 1635 p = elf_tdata (abfd)->phdr; 1636 if (p != NULL) 1637 { 1638 unsigned int i, c; 1639 1640 fprintf (f, _("\nProgram Header:\n")); 1641 c = elf_elfheader (abfd)->e_phnum; 1642 for (i = 0; i < c; i++, p++) 1643 { 1644 const char *pt = get_segment_type (p->p_type); 1645 char buf[20]; 1646 1647 if (pt == NULL) 1648 { 1649 sprintf (buf, "0x%lx", p->p_type); 1650 pt = buf; 1651 } 1652 fprintf (f, "%8s off 0x", pt); 1653 bfd_fprintf_vma (abfd, f, p->p_offset); 1654 fprintf (f, " vaddr 0x"); 1655 bfd_fprintf_vma (abfd, f, p->p_vaddr); 1656 fprintf (f, " paddr 0x"); 1657 bfd_fprintf_vma (abfd, f, p->p_paddr); 1658 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); 1659 fprintf (f, " filesz 0x"); 1660 bfd_fprintf_vma (abfd, f, p->p_filesz); 1661 fprintf (f, " memsz 0x"); 1662 bfd_fprintf_vma (abfd, f, p->p_memsz); 1663 fprintf (f, " flags %c%c%c", 1664 (p->p_flags & PF_R) != 0 ? 'r' : '-', 1665 (p->p_flags & PF_W) != 0 ? 'w' : '-', 1666 (p->p_flags & PF_X) != 0 ? 'x' : '-'); 1667 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) 1668 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); 1669 fprintf (f, "\n"); 1670 } 1671 } 1672 1673 s = bfd_get_section_by_name (abfd, ".dynamic"); 1674 if (s != NULL && (s->flags & SEC_HAS_CONTENTS) != 0) 1675 { 1676 unsigned int elfsec; 1677 unsigned long shlink; 1678 bfd_byte *extdyn, *extdynend; 1679 size_t extdynsize; 1680 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 1681 1682 fprintf (f, _("\nDynamic Section:\n")); 1683 1684 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf)) 1685 goto error_return; 1686 1687 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 1688 if (elfsec == SHN_BAD) 1689 goto error_return; 1690 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 1691 1692 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 1693 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 1694 1695 for (extdyn = dynbuf, extdynend = dynbuf + s->size; 1696 (size_t) (extdynend - extdyn) >= extdynsize; 1697 extdyn += extdynsize) 1698 { 1699 Elf_Internal_Dyn dyn; 1700 const char *name = ""; 1701 char ab[20]; 1702 bool stringp; 1703 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 1704 1705 (*swap_dyn_in) (abfd, extdyn, &dyn); 1706 1707 if (dyn.d_tag == DT_NULL) 1708 break; 1709 1710 stringp = false; 1711 switch (dyn.d_tag) 1712 { 1713 default: 1714 if (bed->elf_backend_get_target_dtag) 1715 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); 1716 1717 if (!strcmp (name, "")) 1718 { 1719 sprintf (ab, "%#" PRIx64, (uint64_t) dyn.d_tag); 1720 name = ab; 1721 } 1722 break; 1723 1724 case DT_NEEDED: name = "NEEDED"; stringp = true; break; 1725 case DT_PLTRELSZ: name = "PLTRELSZ"; break; 1726 case DT_PLTGOT: name = "PLTGOT"; break; 1727 case DT_HASH: name = "HASH"; break; 1728 case DT_STRTAB: name = "STRTAB"; break; 1729 case DT_SYMTAB: name = "SYMTAB"; break; 1730 case DT_RELA: name = "RELA"; break; 1731 case DT_RELASZ: name = "RELASZ"; break; 1732 case DT_RELAENT: name = "RELAENT"; break; 1733 case DT_STRSZ: name = "STRSZ"; break; 1734 case DT_SYMENT: name = "SYMENT"; break; 1735 case DT_INIT: name = "INIT"; break; 1736 case DT_FINI: name = "FINI"; break; 1737 case DT_SONAME: name = "SONAME"; stringp = true; break; 1738 case DT_RPATH: name = "RPATH"; stringp = true; break; 1739 case DT_SYMBOLIC: name = "SYMBOLIC"; break; 1740 case DT_REL: name = "REL"; break; 1741 case DT_RELSZ: name = "RELSZ"; break; 1742 case DT_RELENT: name = "RELENT"; break; 1743 case DT_RELR: name = "RELR"; break; 1744 case DT_RELRSZ: name = "RELRSZ"; break; 1745 case DT_RELRENT: name = "RELRENT"; break; 1746 case DT_PLTREL: name = "PLTREL"; break; 1747 case DT_DEBUG: name = "DEBUG"; break; 1748 case DT_TEXTREL: name = "TEXTREL"; break; 1749 case DT_JMPREL: name = "JMPREL"; break; 1750 case DT_BIND_NOW: name = "BIND_NOW"; break; 1751 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; 1752 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; 1753 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; 1754 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; 1755 case DT_RUNPATH: name = "RUNPATH"; stringp = true; break; 1756 case DT_FLAGS: name = "FLAGS"; break; 1757 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; 1758 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; 1759 case DT_CHECKSUM: name = "CHECKSUM"; break; 1760 case DT_PLTPADSZ: name = "PLTPADSZ"; break; 1761 case DT_MOVEENT: name = "MOVEENT"; break; 1762 case DT_MOVESZ: name = "MOVESZ"; break; 1763 case DT_FEATURE: name = "FEATURE"; break; 1764 case DT_POSFLAG_1: name = "POSFLAG_1"; break; 1765 case DT_SYMINSZ: name = "SYMINSZ"; break; 1766 case DT_SYMINENT: name = "SYMINENT"; break; 1767 case DT_CONFIG: name = "CONFIG"; stringp = true; break; 1768 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = true; break; 1769 case DT_AUDIT: name = "AUDIT"; stringp = true; break; 1770 case DT_PLTPAD: name = "PLTPAD"; break; 1771 case DT_MOVETAB: name = "MOVETAB"; break; 1772 case DT_SYMINFO: name = "SYMINFO"; break; 1773 case DT_RELACOUNT: name = "RELACOUNT"; break; 1774 case DT_RELCOUNT: name = "RELCOUNT"; break; 1775 case DT_FLAGS_1: name = "FLAGS_1"; break; 1776 case DT_VERSYM: name = "VERSYM"; break; 1777 case DT_VERDEF: name = "VERDEF"; break; 1778 case DT_VERDEFNUM: name = "VERDEFNUM"; break; 1779 case DT_VERNEED: name = "VERNEED"; break; 1780 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; 1781 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break; 1782 case DT_USED: name = "USED"; break; 1783 case DT_FILTER: name = "FILTER"; stringp = true; break; 1784 case DT_GNU_HASH: name = "GNU_HASH"; break; 1785 } 1786 1787 fprintf (f, " %-20s ", name); 1788 if (! stringp) 1789 { 1790 fprintf (f, "0x"); 1791 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); 1792 } 1793 else 1794 { 1795 const char *string; 1796 unsigned int tagv = dyn.d_un.d_val; 1797 1798 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 1799 if (string == NULL) 1800 goto error_return; 1801 fprintf (f, "%s", string); 1802 } 1803 fprintf (f, "\n"); 1804 } 1805 1806 _bfd_elf_munmap_section_contents (s, dynbuf); 1807 dynbuf = NULL; 1808 } 1809 1810 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) 1811 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) 1812 { 1813 if (! _bfd_elf_slurp_version_tables (abfd, false)) 1814 return false; 1815 } 1816 1817 if (elf_dynverdef (abfd) != 0) 1818 { 1819 Elf_Internal_Verdef *t; 1820 1821 fprintf (f, _("\nVersion definitions:\n")); 1822 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) 1823 { 1824 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, 1825 t->vd_flags, t->vd_hash, 1826 t->vd_nodename ? t->vd_nodename : "<corrupt>"); 1827 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) 1828 { 1829 Elf_Internal_Verdaux *a; 1830 1831 fprintf (f, "\t"); 1832 for (a = t->vd_auxptr->vda_nextptr; 1833 a != NULL; 1834 a = a->vda_nextptr) 1835 fprintf (f, "%s ", 1836 a->vda_nodename ? a->vda_nodename : "<corrupt>"); 1837 fprintf (f, "\n"); 1838 } 1839 } 1840 } 1841 1842 if (elf_dynverref (abfd) != 0) 1843 { 1844 Elf_Internal_Verneed *t; 1845 1846 fprintf (f, _("\nVersion References:\n")); 1847 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) 1848 { 1849 Elf_Internal_Vernaux *a; 1850 1851 fprintf (f, _(" required from %s:\n"), 1852 t->vn_filename ? t->vn_filename : "<corrupt>"); 1853 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 1854 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, 1855 a->vna_flags, a->vna_other, 1856 a->vna_nodename ? a->vna_nodename : "<corrupt>"); 1857 } 1858 } 1859 1860 return true; 1861 1862 error_return: 1863 _bfd_elf_munmap_section_contents (s, dynbuf); 1864 return false; 1865 } 1866 1867 /* Find the file offset corresponding to VMA by using the program 1868 headers. */ 1869 1870 static file_ptr 1871 offset_from_vma (Elf_Internal_Phdr *phdrs, size_t phnum, bfd_vma vma, 1872 size_t size, size_t *max_size_p) 1873 { 1874 Elf_Internal_Phdr *seg; 1875 size_t i; 1876 1877 for (seg = phdrs, i = 0; i < phnum; ++seg, ++i) 1878 if (seg->p_type == PT_LOAD 1879 && vma >= (seg->p_vaddr & -seg->p_align) 1880 && vma + size <= seg->p_vaddr + seg->p_filesz) 1881 { 1882 if (max_size_p) 1883 *max_size_p = seg->p_vaddr + seg->p_filesz - vma; 1884 return vma - seg->p_vaddr + seg->p_offset; 1885 } 1886 1887 if (max_size_p) 1888 *max_size_p = 0; 1889 bfd_set_error (bfd_error_invalid_operation); 1890 return (file_ptr) -1; 1891 } 1892 1893 /* Convert hash table to internal form. */ 1894 1895 static bfd_vma * 1896 get_hash_table_data (bfd *abfd, bfd_size_type number, 1897 unsigned int ent_size, bfd_size_type filesize) 1898 { 1899 unsigned char *e_data = NULL; 1900 bfd_vma *i_data = NULL; 1901 bfd_size_type size; 1902 void *e_data_addr; 1903 size_t e_data_size ATTRIBUTE_UNUSED; 1904 1905 if (ent_size != 4 && ent_size != 8) 1906 return NULL; 1907 1908 if ((size_t) number != number) 1909 { 1910 bfd_set_error (bfd_error_file_too_big); 1911 return NULL; 1912 } 1913 1914 size = ent_size * number; 1915 /* Be kind to memory checkers (eg valgrind, address sanitizer) by not 1916 attempting to allocate memory when the read is bound to fail. */ 1917 if (size > filesize 1918 || number >= ~(size_t) 0 / ent_size 1919 || number >= ~(size_t) 0 / sizeof (*i_data)) 1920 { 1921 bfd_set_error (bfd_error_file_too_big); 1922 return NULL; 1923 } 1924 1925 e_data = _bfd_mmap_readonly_temporary (abfd, size, &e_data_addr, 1926 &e_data_size); 1927 if (e_data == NULL) 1928 return NULL; 1929 1930 i_data = (bfd_vma *) bfd_malloc (number * sizeof (*i_data)); 1931 if (i_data == NULL) 1932 { 1933 free (e_data); 1934 return NULL; 1935 } 1936 1937 if (ent_size == 4) 1938 while (number--) 1939 i_data[number] = bfd_get_32 (abfd, e_data + number * ent_size); 1940 else 1941 while (number--) 1942 i_data[number] = bfd_get_64 (abfd, e_data + number * ent_size); 1943 1944 _bfd_munmap_readonly_temporary (e_data_addr, e_data_size); 1945 return i_data; 1946 } 1947 1948 /* Address of .MIPS.xhash section. FIXME: What is the best way to 1949 support DT_MIPS_XHASH? */ 1950 #define DT_MIPS_XHASH 0x70000036 1951 1952 /* Reconstruct dynamic symbol table from PT_DYNAMIC segment. */ 1953 1954 bool 1955 _bfd_elf_get_dynamic_symbols (bfd *abfd, Elf_Internal_Phdr *phdr, 1956 Elf_Internal_Phdr *phdrs, size_t phnum, 1957 bfd_size_type filesize) 1958 { 1959 bfd_byte *extdyn, *extdynend; 1960 size_t extdynsize; 1961 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 1962 bool (*swap_symbol_in) (bfd *, const void *, const void *, 1963 Elf_Internal_Sym *); 1964 Elf_Internal_Dyn dyn; 1965 bfd_vma dt_hash = 0; 1966 bfd_vma dt_gnu_hash = 0; 1967 bfd_vma dt_mips_xhash = 0; 1968 bfd_vma dt_strtab = 0; 1969 bfd_vma dt_symtab = 0; 1970 size_t dt_strsz = 0; 1971 bfd_vma dt_versym = 0; 1972 bfd_vma dt_verdef = 0; 1973 bfd_vma dt_verneed = 0; 1974 bfd_byte *dynbuf = NULL; 1975 char *strbuf = NULL; 1976 bfd_vma *gnubuckets = NULL; 1977 bfd_vma *gnuchains = NULL; 1978 bfd_vma *mipsxlat = NULL; 1979 file_ptr saved_filepos, filepos; 1980 bool res = false; 1981 size_t amt; 1982 bfd_byte *esymbuf = NULL, *esym; 1983 bfd_size_type symcount; 1984 Elf_Internal_Sym *isymbuf = NULL; 1985 Elf_Internal_Sym *isym, *isymend; 1986 bfd_byte *versym = NULL; 1987 bfd_byte *verdef = NULL; 1988 bfd_byte *verneed = NULL; 1989 size_t verdef_size = 0; 1990 size_t verneed_size = 0; 1991 size_t extsym_size; 1992 const struct elf_backend_data *bed; 1993 void *dynbuf_addr = NULL; 1994 void *esymbuf_addr = NULL; 1995 size_t dynbuf_size = 0; 1996 size_t esymbuf_size = 0; 1997 1998 /* Return TRUE if symbol table is bad. */ 1999 if (elf_bad_symtab (abfd)) 2000 return true; 2001 2002 /* Return TRUE if DT_HASH/DT_GNU_HASH have bee processed before. */ 2003 if (elf_tdata (abfd)->dt_strtab != NULL) 2004 return true; 2005 2006 bed = get_elf_backend_data (abfd); 2007 2008 /* Save file position for elf_object_p. */ 2009 saved_filepos = bfd_tell (abfd); 2010 2011 if (bfd_seek (abfd, phdr->p_offset, SEEK_SET) != 0) 2012 goto error_return; 2013 2014 dynbuf_size = phdr->p_filesz; 2015 dynbuf = _bfd_mmap_readonly_temporary (abfd, dynbuf_size, 2016 &dynbuf_addr, &dynbuf_size); 2017 if (dynbuf == NULL) 2018 goto error_return; 2019 2020 extsym_size = bed->s->sizeof_sym; 2021 extdynsize = bed->s->sizeof_dyn; 2022 swap_dyn_in = bed->s->swap_dyn_in; 2023 2024 extdyn = dynbuf; 2025 if (phdr->p_filesz < extdynsize) 2026 goto error_return; 2027 extdynend = extdyn + phdr->p_filesz; 2028 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) 2029 { 2030 swap_dyn_in (abfd, extdyn, &dyn); 2031 2032 if (dyn.d_tag == DT_NULL) 2033 break; 2034 2035 switch (dyn.d_tag) 2036 { 2037 case DT_HASH: 2038 dt_hash = dyn.d_un.d_val; 2039 break; 2040 case DT_GNU_HASH: 2041 if (bed->elf_machine_code != EM_MIPS 2042 && bed->elf_machine_code != EM_MIPS_RS3_LE) 2043 dt_gnu_hash = dyn.d_un.d_val; 2044 break; 2045 case DT_STRTAB: 2046 dt_strtab = dyn.d_un.d_val; 2047 break; 2048 case DT_SYMTAB: 2049 dt_symtab = dyn.d_un.d_val; 2050 break; 2051 case DT_STRSZ: 2052 dt_strsz = dyn.d_un.d_val; 2053 break; 2054 case DT_SYMENT: 2055 if (dyn.d_un.d_val != extsym_size) 2056 goto error_return; 2057 break; 2058 case DT_VERSYM: 2059 dt_versym = dyn.d_un.d_val; 2060 break; 2061 case DT_VERDEF: 2062 dt_verdef = dyn.d_un.d_val; 2063 break; 2064 case DT_VERNEED: 2065 dt_verneed = dyn.d_un.d_val; 2066 break; 2067 default: 2068 if (dyn.d_tag == DT_MIPS_XHASH 2069 && (bed->elf_machine_code == EM_MIPS 2070 || bed->elf_machine_code == EM_MIPS_RS3_LE)) 2071 { 2072 dt_gnu_hash = dyn.d_un.d_val; 2073 dt_mips_xhash = dyn.d_un.d_val; 2074 } 2075 break; 2076 } 2077 } 2078 2079 /* Check if we can reconstruct dynamic symbol table from PT_DYNAMIC 2080 segment. */ 2081 if ((!dt_hash && !dt_gnu_hash) 2082 || !dt_strtab 2083 || !dt_symtab 2084 || !dt_strsz) 2085 goto error_return; 2086 2087 /* Get dynamic string table. */ 2088 filepos = offset_from_vma (phdrs, phnum, dt_strtab, dt_strsz, NULL); 2089 if (filepos == (file_ptr) -1 2090 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2091 goto error_return; 2092 2093 /* Dynamic string table must be valid until ABFD is closed. */ 2094 strbuf = (char *) _bfd_mmap_readonly_persistent (abfd, dt_strsz); 2095 if (strbuf == NULL) 2096 goto error_return; 2097 if (strbuf[dt_strsz - 1] != 0) 2098 { 2099 /* It is an error if a string table is't terminated. */ 2100 _bfd_error_handler 2101 /* xgettext:c-format */ 2102 (_("%pB: DT_STRTAB table is corrupt"), abfd); 2103 goto error_return; 2104 } 2105 2106 /* Get the real symbol count from DT_HASH or DT_GNU_HASH. Prefer 2107 DT_HASH since it is simpler than DT_GNU_HASH. */ 2108 if (dt_hash) 2109 { 2110 unsigned char nb[16]; 2111 unsigned int hash_ent_size; 2112 2113 switch (bed->elf_machine_code) 2114 { 2115 case EM_ALPHA: 2116 case EM_S390: 2117 case EM_S390_OLD: 2118 if (bed->s->elfclass == ELFCLASS64) 2119 { 2120 hash_ent_size = 8; 2121 break; 2122 } 2123 /* FALLTHROUGH */ 2124 default: 2125 hash_ent_size = 4; 2126 break; 2127 } 2128 2129 filepos = offset_from_vma (phdrs, phnum, dt_hash, sizeof (nb), 2130 NULL); 2131 if (filepos == (file_ptr) -1 2132 || bfd_seek (abfd, filepos, SEEK_SET) != 0 2133 || bfd_read (nb, 2 * hash_ent_size, abfd) != 2 * hash_ent_size) 2134 goto error_return; 2135 2136 /* The number of dynamic symbol table entries equals the number 2137 of chains. */ 2138 if (hash_ent_size == 8) 2139 symcount = bfd_get_64 (abfd, nb + hash_ent_size); 2140 else 2141 symcount = bfd_get_32 (abfd, nb + hash_ent_size); 2142 } 2143 else 2144 { 2145 /* For DT_GNU_HASH, only defined symbols with non-STB_LOCAL 2146 bindings are in hash table. Since in dynamic symbol table, 2147 all symbols with STB_LOCAL binding are placed before symbols 2148 with other bindings and all undefined symbols are placed 2149 before defined ones, the highest symbol index in DT_GNU_HASH 2150 is the highest dynamic symbol table index. */ 2151 unsigned char nb[16]; 2152 bfd_vma ngnubuckets; 2153 bfd_vma gnusymidx; 2154 size_t i, ngnuchains; 2155 bfd_vma maxchain = 0xffffffff, bitmaskwords; 2156 bfd_vma buckets_vma; 2157 2158 filepos = offset_from_vma (phdrs, phnum, dt_gnu_hash, 2159 sizeof (nb), NULL); 2160 if (filepos == (file_ptr) -1 2161 || bfd_seek (abfd, filepos, SEEK_SET) != 0 2162 || bfd_read (nb, sizeof (nb), abfd) != sizeof (nb)) 2163 goto error_return; 2164 2165 ngnubuckets = bfd_get_32 (abfd, nb); 2166 gnusymidx = bfd_get_32 (abfd, nb + 4); 2167 bitmaskwords = bfd_get_32 (abfd, nb + 8); 2168 buckets_vma = dt_gnu_hash + 16; 2169 if (bed->s->elfclass == ELFCLASS32) 2170 buckets_vma += bitmaskwords * 4; 2171 else 2172 buckets_vma += bitmaskwords * 8; 2173 filepos = offset_from_vma (phdrs, phnum, buckets_vma, 4, NULL); 2174 if (filepos == (file_ptr) -1 2175 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2176 goto error_return; 2177 2178 gnubuckets = get_hash_table_data (abfd, ngnubuckets, 4, filesize); 2179 if (gnubuckets == NULL) 2180 goto error_return; 2181 2182 for (i = 0; i < ngnubuckets; i++) 2183 if (gnubuckets[i] != 0) 2184 { 2185 if (gnubuckets[i] < gnusymidx) 2186 goto error_return; 2187 2188 if (maxchain == 0xffffffff || gnubuckets[i] > maxchain) 2189 maxchain = gnubuckets[i]; 2190 } 2191 2192 if (maxchain == 0xffffffff) 2193 { 2194 symcount = 0; 2195 goto empty_gnu_hash; 2196 } 2197 2198 maxchain -= gnusymidx; 2199 filepos = offset_from_vma (phdrs, phnum, 2200 (buckets_vma + 2201 4 * (ngnubuckets + maxchain)), 2202 4, NULL); 2203 if (filepos == (file_ptr) -1 2204 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2205 goto error_return; 2206 2207 do 2208 { 2209 if (bfd_read (nb, 4, abfd) != 4) 2210 goto error_return; 2211 ++maxchain; 2212 if (maxchain == 0) 2213 goto error_return; 2214 } 2215 while ((bfd_get_32 (abfd, nb) & 1) == 0); 2216 2217 filepos = offset_from_vma (phdrs, phnum, 2218 (buckets_vma + 4 * ngnubuckets), 2219 4, NULL); 2220 if (filepos == (file_ptr) -1 2221 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2222 goto error_return; 2223 2224 gnuchains = get_hash_table_data (abfd, maxchain, 4, filesize); 2225 if (gnuchains == NULL) 2226 goto error_return; 2227 ngnuchains = maxchain; 2228 2229 if (dt_mips_xhash) 2230 { 2231 filepos = offset_from_vma (phdrs, phnum, 2232 (buckets_vma 2233 + 4 * (ngnubuckets + maxchain)), 2234 4, NULL); 2235 if (filepos == (file_ptr) -1 2236 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2237 goto error_return; 2238 2239 mipsxlat = get_hash_table_data (abfd, maxchain, 4, filesize); 2240 if (mipsxlat == NULL) 2241 goto error_return; 2242 } 2243 2244 symcount = 0; 2245 for (i = 0; i < ngnubuckets; ++i) 2246 if (gnubuckets[i] != 0) 2247 { 2248 bfd_vma si = gnubuckets[i]; 2249 bfd_vma off = si - gnusymidx; 2250 do 2251 { 2252 if (mipsxlat) 2253 { 2254 if (mipsxlat[off] >= symcount) 2255 symcount = mipsxlat[off] + 1; 2256 } 2257 else 2258 { 2259 if (si >= symcount) 2260 symcount = si + 1; 2261 } 2262 si++; 2263 } 2264 while (off < ngnuchains && (gnuchains[off++] & 1) == 0); 2265 } 2266 } 2267 2268 /* Swap in dynamic symbol table. */ 2269 if (_bfd_mul_overflow (symcount, extsym_size, &amt)) 2270 { 2271 bfd_set_error (bfd_error_file_too_big); 2272 goto error_return; 2273 } 2274 2275 filepos = offset_from_vma (phdrs, phnum, dt_symtab, amt, NULL); 2276 if (filepos == (file_ptr) -1 2277 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2278 goto error_return; 2279 esymbuf_size = amt; 2280 esymbuf = _bfd_mmap_readonly_temporary (abfd, esymbuf_size, 2281 &esymbuf_addr, 2282 &esymbuf_size); 2283 if (esymbuf == NULL) 2284 goto error_return; 2285 2286 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt)) 2287 { 2288 bfd_set_error (bfd_error_file_too_big); 2289 goto error_return; 2290 } 2291 2292 /* Dynamic symbol table must be valid until ABFD is closed. */ 2293 isymbuf = (Elf_Internal_Sym *) bfd_alloc (abfd, amt); 2294 if (isymbuf == NULL) 2295 goto error_return; 2296 2297 swap_symbol_in = bed->s->swap_symbol_in; 2298 2299 /* Convert the symbols to internal form. */ 2300 isymend = isymbuf + symcount; 2301 for (esym = esymbuf, isym = isymbuf; 2302 isym < isymend; 2303 esym += extsym_size, isym++) 2304 if (!swap_symbol_in (abfd, esym, NULL, isym) 2305 || isym->st_name >= dt_strsz) 2306 { 2307 bfd_set_error (bfd_error_invalid_operation); 2308 goto error_return; 2309 } 2310 2311 if (dt_versym) 2312 { 2313 /* Swap in DT_VERSYM. */ 2314 if (_bfd_mul_overflow (symcount, 2, &amt)) 2315 { 2316 bfd_set_error (bfd_error_file_too_big); 2317 goto error_return; 2318 } 2319 2320 filepos = offset_from_vma (phdrs, phnum, dt_versym, amt, NULL); 2321 if (filepos == (file_ptr) -1 2322 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2323 goto error_return; 2324 2325 /* DT_VERSYM info must be valid until ABFD is closed. */ 2326 versym = _bfd_mmap_readonly_persistent (abfd, amt); 2327 2328 if (dt_verdef) 2329 { 2330 /* Read in DT_VERDEF. */ 2331 filepos = offset_from_vma (phdrs, phnum, dt_verdef, 2332 0, &verdef_size); 2333 if (filepos == (file_ptr) -1 2334 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2335 goto error_return; 2336 2337 /* DT_VERDEF info must be valid until ABFD is closed. */ 2338 verdef = _bfd_mmap_readonly_persistent (abfd, verdef_size); 2339 } 2340 2341 if (dt_verneed) 2342 { 2343 /* Read in DT_VERNEED. */ 2344 filepos = offset_from_vma (phdrs, phnum, dt_verneed, 2345 0, &verneed_size); 2346 if (filepos == (file_ptr) -1 2347 || bfd_seek (abfd, filepos, SEEK_SET) != 0) 2348 goto error_return; 2349 2350 /* DT_VERNEED info must be valid until ABFD is closed. */ 2351 verneed = _bfd_mmap_readonly_persistent (abfd, verneed_size); 2352 } 2353 } 2354 2355 empty_gnu_hash: 2356 elf_tdata (abfd)->dt_strtab = strbuf; 2357 elf_tdata (abfd)->dt_strsz = dt_strsz; 2358 elf_tdata (abfd)->dt_symtab = isymbuf; 2359 elf_tdata (abfd)->dt_symtab_count = symcount; 2360 elf_tdata (abfd)->dt_versym = versym; 2361 elf_tdata (abfd)->dt_verdef = verdef; 2362 elf_tdata (abfd)->dt_verneed = verneed; 2363 elf_tdata (abfd)->dt_verdef_count 2364 = verdef_size / sizeof (Elf_External_Verdef); 2365 elf_tdata (abfd)->dt_verneed_count 2366 = verneed_size / sizeof (Elf_External_Verneed); 2367 2368 res = true; 2369 2370 error_return: 2371 /* Restore file position for elf_object_p. */ 2372 if (bfd_seek (abfd, saved_filepos, SEEK_SET) != 0) 2373 res = false; 2374 _bfd_munmap_readonly_temporary (dynbuf_addr, dynbuf_size); 2375 _bfd_munmap_readonly_temporary (esymbuf_addr, esymbuf_size); 2376 free (gnubuckets); 2377 free (gnuchains); 2378 free (mipsxlat); 2379 return res; 2380 } 2381 2382 /* Reconstruct section from dynamic symbol. */ 2383 2384 asection * 2385 _bfd_elf_get_section_from_dynamic_symbol (bfd *abfd, 2386 Elf_Internal_Sym *isym) 2387 { 2388 asection *sec; 2389 flagword flags; 2390 2391 if (!elf_use_dt_symtab_p (abfd)) 2392 return NULL; 2393 2394 flags = SEC_ALLOC | SEC_LOAD; 2395 switch (ELF_ST_TYPE (isym->st_info)) 2396 { 2397 case STT_FUNC: 2398 case STT_GNU_IFUNC: 2399 sec = bfd_get_section_by_name (abfd, ".text"); 2400 if (sec == NULL) 2401 sec = bfd_make_section_with_flags (abfd, 2402 ".text", 2403 flags | SEC_CODE); 2404 break; 2405 case STT_COMMON: 2406 sec = bfd_com_section_ptr; 2407 break; 2408 case STT_OBJECT: 2409 sec = bfd_get_section_by_name (abfd, ".data"); 2410 if (sec == NULL) 2411 sec = bfd_make_section_with_flags (abfd, 2412 ".data", 2413 flags | SEC_DATA); 2414 break; 2415 case STT_TLS: 2416 sec = bfd_get_section_by_name (abfd, ".tdata"); 2417 if (sec == NULL) 2418 sec = bfd_make_section_with_flags (abfd, 2419 ".tdata", 2420 (flags 2421 | SEC_DATA 2422 | SEC_THREAD_LOCAL)); 2423 break; 2424 default: 2425 sec = bfd_abs_section_ptr; 2426 break; 2427 } 2428 2429 return sec; 2430 } 2431 2432 /* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE 2433 and return symbol version for symbol version itself. */ 2434 2435 const char * 2436 _bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, 2437 bool base_p, 2438 bool *hidden) 2439 { 2440 const char *version_string = NULL; 2441 if ((elf_dynversym (abfd) != 0 2442 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) 2443 || (elf_tdata (abfd)->dt_versym != NULL 2444 && (elf_tdata (abfd)->dt_verdef != NULL 2445 || elf_tdata (abfd)->dt_verneed != NULL))) 2446 { 2447 unsigned int vernum = ((elf_symbol_type *) symbol)->version; 2448 2449 *hidden = (vernum & VERSYM_HIDDEN) != 0; 2450 vernum &= VERSYM_VERSION; 2451 2452 if (vernum == 0) 2453 version_string = ""; 2454 else if (vernum == 1 2455 && (vernum > elf_tdata (abfd)->cverdefs 2456 || (elf_tdata (abfd)->verdef[0].vd_flags 2457 == VER_FLG_BASE))) 2458 version_string = base_p ? "Base" : ""; 2459 else if (vernum <= elf_tdata (abfd)->cverdefs) 2460 { 2461 const char *nodename 2462 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 2463 version_string = ""; 2464 if (base_p 2465 || nodename == NULL 2466 || symbol->name == NULL 2467 || strcmp (symbol->name, nodename) != 0) 2468 version_string = nodename; 2469 } 2470 else 2471 { 2472 Elf_Internal_Verneed *t; 2473 2474 version_string = _("<corrupt>"); 2475 for (t = elf_tdata (abfd)->verref; 2476 t != NULL; 2477 t = t->vn_nextref) 2478 { 2479 Elf_Internal_Vernaux *a; 2480 2481 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 2482 { 2483 if (a->vna_other == vernum) 2484 { 2485 *hidden = true; 2486 version_string = a->vna_nodename; 2487 break; 2488 } 2489 } 2490 } 2491 } 2492 } 2493 return version_string; 2494 } 2495 2496 /* Display ELF-specific fields of a symbol. */ 2497 2498 void 2499 bfd_elf_print_symbol (bfd *abfd, 2500 void *filep, 2501 asymbol *symbol, 2502 bfd_print_symbol_type how) 2503 { 2504 FILE *file = (FILE *) filep; 2505 switch (how) 2506 { 2507 case bfd_print_symbol_name: 2508 fprintf (file, "%s", symbol->name); 2509 break; 2510 case bfd_print_symbol_more: 2511 fprintf (file, "elf "); 2512 bfd_fprintf_vma (abfd, file, symbol->value); 2513 fprintf (file, " %x", symbol->flags); 2514 break; 2515 case bfd_print_symbol_all: 2516 { 2517 const char *section_name; 2518 const char *name = NULL; 2519 const struct elf_backend_data *bed; 2520 unsigned char st_other; 2521 bfd_vma val; 2522 const char *version_string; 2523 bool hidden; 2524 2525 section_name = symbol->section ? symbol->section->name : "(*none*)"; 2526 2527 bed = get_elf_backend_data (abfd); 2528 if (bed->elf_backend_print_symbol_all) 2529 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); 2530 2531 if (name == NULL) 2532 { 2533 name = symbol->name; 2534 bfd_print_symbol_vandf (abfd, file, symbol); 2535 } 2536 2537 fprintf (file, " %s\t", section_name); 2538 /* Print the "other" value for a symbol. For common symbols, 2539 we've already printed the size; now print the alignment. 2540 For other symbols, we have no specified alignment, and 2541 we've printed the address; now print the size. */ 2542 if (symbol->section && bfd_is_com_section (symbol->section)) 2543 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; 2544 else 2545 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; 2546 bfd_fprintf_vma (abfd, file, val); 2547 2548 /* If we have version information, print it. */ 2549 version_string = _bfd_elf_get_symbol_version_string (abfd, 2550 symbol, 2551 true, 2552 &hidden); 2553 if (version_string) 2554 { 2555 if (!hidden) 2556 fprintf (file, " %-11s", version_string); 2557 else 2558 { 2559 int i; 2560 2561 fprintf (file, " (%s)", version_string); 2562 for (i = 10 - strlen (version_string); i > 0; --i) 2563 putc (' ', file); 2564 } 2565 } 2566 2567 /* If the st_other field is not zero, print it. */ 2568 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; 2569 2570 switch (st_other) 2571 { 2572 case 0: break; 2573 case STV_INTERNAL: fprintf (file, " .internal"); break; 2574 case STV_HIDDEN: fprintf (file, " .hidden"); break; 2575 case STV_PROTECTED: fprintf (file, " .protected"); break; 2576 default: 2577 /* Some other non-defined flags are also present, so print 2578 everything hex. */ 2579 fprintf (file, " 0x%02x", (unsigned int) st_other); 2580 } 2581 2582 fprintf (file, " %s", name); 2583 } 2584 break; 2585 } 2586 } 2587 2588 /* ELF .o/exec file reading */ 2589 2590 /* Create a new bfd section from an ELF section header. */ 2591 2592 bool 2593 bfd_section_from_shdr (bfd *abfd, unsigned int shindex) 2594 { 2595 Elf_Internal_Shdr *hdr; 2596 Elf_Internal_Ehdr *ehdr; 2597 const struct elf_backend_data *bed; 2598 const char *name; 2599 bool ret = true; 2600 2601 if (shindex >= elf_numsections (abfd)) 2602 return false; 2603 2604 /* PR17512: A corrupt ELF binary might contain a loop of sections via 2605 sh_link or sh_info. Detect this here, by refusing to load a 2606 section that we are already in the process of loading. */ 2607 if (elf_tdata (abfd)->being_created[shindex]) 2608 { 2609 _bfd_error_handler 2610 (_("%pB: warning: loop in section dependencies detected"), abfd); 2611 return false; 2612 } 2613 elf_tdata (abfd)->being_created[shindex] = true; 2614 2615 hdr = elf_elfsections (abfd)[shindex]; 2616 ehdr = elf_elfheader (abfd); 2617 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, 2618 hdr->sh_name); 2619 if (name == NULL) 2620 goto fail; 2621 2622 bed = get_elf_backend_data (abfd); 2623 switch (hdr->sh_type) 2624 { 2625 case SHT_NULL: 2626 /* Inactive section. Throw it away. */ 2627 goto success; 2628 2629 case SHT_PROGBITS: /* Normal section with contents. */ 2630 case SHT_NOBITS: /* .bss section. */ 2631 case SHT_HASH: /* .hash section. */ 2632 case SHT_NOTE: /* .note section. */ 2633 case SHT_INIT_ARRAY: /* .init_array section. */ 2634 case SHT_FINI_ARRAY: /* .fini_array section. */ 2635 case SHT_PREINIT_ARRAY: /* .preinit_array section. */ 2636 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ 2637 case SHT_GNU_HASH: /* .gnu.hash section. */ 2638 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2639 goto success; 2640 2641 case SHT_DYNAMIC: /* Dynamic linking information. */ 2642 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2643 goto fail; 2644 2645 if (hdr->sh_link > elf_numsections (abfd)) 2646 { 2647 /* PR 10478: Accept Solaris binaries with a sh_link field 2648 set to SHN_BEFORE (LORESERVE) or SHN_AFTER (LORESERVE+1). */ 2649 switch (bfd_get_arch (abfd)) 2650 { 2651 case bfd_arch_i386: 2652 case bfd_arch_sparc: 2653 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) 2654 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff)) 2655 break; 2656 /* Otherwise fall through. */ 2657 default: 2658 goto fail; 2659 } 2660 } 2661 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) 2662 goto fail; 2663 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) 2664 { 2665 Elf_Internal_Shdr *dynsymhdr; 2666 2667 /* The shared libraries distributed with hpux11 have a bogus 2668 sh_link field for the ".dynamic" section. Find the 2669 string table for the ".dynsym" section instead. */ 2670 if (elf_dynsymtab (abfd) != 0) 2671 { 2672 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; 2673 hdr->sh_link = dynsymhdr->sh_link; 2674 } 2675 else 2676 { 2677 unsigned int i, num_sec; 2678 2679 num_sec = elf_numsections (abfd); 2680 for (i = 1; i < num_sec; i++) 2681 { 2682 dynsymhdr = elf_elfsections (abfd)[i]; 2683 if (dynsymhdr->sh_type == SHT_DYNSYM) 2684 { 2685 hdr->sh_link = dynsymhdr->sh_link; 2686 break; 2687 } 2688 } 2689 } 2690 } 2691 goto success; 2692 2693 case SHT_SYMTAB: /* A symbol table. */ 2694 if (elf_onesymtab (abfd) == shindex) 2695 goto success; 2696 2697 if (hdr->sh_entsize != bed->s->sizeof_sym) 2698 goto fail; 2699 2700 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) 2701 { 2702 if (hdr->sh_size != 0) 2703 goto fail; 2704 /* Some assemblers erroneously set sh_info to one with a 2705 zero sh_size. ld sees this as a global symbol count 2706 of (unsigned) -1. Fix it here. */ 2707 hdr->sh_info = 0; 2708 goto success; 2709 } 2710 2711 /* PR 18854: A binary might contain more than one symbol table. 2712 Unusual, but possible. Warn, but continue. */ 2713 if (elf_onesymtab (abfd) != 0) 2714 { 2715 _bfd_error_handler 2716 /* xgettext:c-format */ 2717 (_("%pB: warning: multiple symbol tables detected" 2718 " - ignoring the table in section %u"), 2719 abfd, shindex); 2720 goto success; 2721 } 2722 elf_onesymtab (abfd) = shindex; 2723 elf_symtab_hdr (abfd) = *hdr; 2724 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); 2725 abfd->flags |= HAS_SYMS; 2726 2727 /* Sometimes a shared object will map in the symbol table. If 2728 SHF_ALLOC is set, and this is a shared object, then we also 2729 treat this section as a BFD section. We can not base the 2730 decision purely on SHF_ALLOC, because that flag is sometimes 2731 set in a relocatable object file, which would confuse the 2732 linker. */ 2733 if ((hdr->sh_flags & SHF_ALLOC) != 0 2734 && (abfd->flags & DYNAMIC) != 0 2735 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2736 shindex)) 2737 goto fail; 2738 2739 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we 2740 can't read symbols without that section loaded as well. It 2741 is most likely specified by the next section header. */ 2742 { 2743 elf_section_list * entry; 2744 unsigned int i, num_sec; 2745 2746 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next) 2747 if (entry->hdr.sh_link == shindex) 2748 goto success; 2749 2750 num_sec = elf_numsections (abfd); 2751 for (i = shindex + 1; i < num_sec; i++) 2752 { 2753 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2754 2755 if (hdr2->sh_type == SHT_SYMTAB_SHNDX 2756 && hdr2->sh_link == shindex) 2757 break; 2758 } 2759 2760 if (i == num_sec) 2761 for (i = 1; i < shindex; i++) 2762 { 2763 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2764 2765 if (hdr2->sh_type == SHT_SYMTAB_SHNDX 2766 && hdr2->sh_link == shindex) 2767 break; 2768 } 2769 2770 if (i != shindex) 2771 ret = bfd_section_from_shdr (abfd, i); 2772 /* else FIXME: we have failed to find the symbol table. 2773 Should we issue an error? */ 2774 goto success; 2775 } 2776 2777 case SHT_DYNSYM: /* A dynamic symbol table. */ 2778 if (elf_dynsymtab (abfd) == shindex) 2779 goto success; 2780 2781 if (hdr->sh_entsize != bed->s->sizeof_sym) 2782 goto fail; 2783 2784 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) 2785 { 2786 if (hdr->sh_size != 0) 2787 goto fail; 2788 2789 /* Some linkers erroneously set sh_info to one with a 2790 zero sh_size. ld sees this as a global symbol count 2791 of (unsigned) -1. Fix it here. */ 2792 hdr->sh_info = 0; 2793 goto success; 2794 } 2795 2796 /* PR 18854: A binary might contain more than one dynamic symbol table. 2797 Unusual, but possible. Warn, but continue. */ 2798 if (elf_dynsymtab (abfd) != 0) 2799 { 2800 _bfd_error_handler 2801 /* xgettext:c-format */ 2802 (_("%pB: warning: multiple dynamic symbol tables detected" 2803 " - ignoring the table in section %u"), 2804 abfd, shindex); 2805 goto success; 2806 } 2807 elf_dynsymtab (abfd) = shindex; 2808 elf_tdata (abfd)->dynsymtab_hdr = *hdr; 2809 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; 2810 abfd->flags |= HAS_SYMS; 2811 2812 /* Besides being a symbol table, we also treat this as a regular 2813 section, so that objcopy can handle it. */ 2814 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2815 goto success; 2816 2817 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ 2818 { 2819 elf_section_list * entry; 2820 2821 for (entry = elf_symtab_shndx_list (abfd); entry; entry = entry->next) 2822 if (entry->ndx == shindex) 2823 goto success; 2824 2825 entry = bfd_alloc (abfd, sizeof (*entry)); 2826 if (entry == NULL) 2827 goto fail; 2828 entry->ndx = shindex; 2829 entry->hdr = * hdr; 2830 entry->next = elf_symtab_shndx_list (abfd); 2831 elf_symtab_shndx_list (abfd) = entry; 2832 elf_elfsections (abfd)[shindex] = & entry->hdr; 2833 goto success; 2834 } 2835 2836 case SHT_STRTAB: /* A string table. */ 2837 if (hdr->bfd_section != NULL) 2838 goto success; 2839 2840 if (ehdr->e_shstrndx == shindex) 2841 { 2842 elf_tdata (abfd)->shstrtab_hdr = *hdr; 2843 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; 2844 goto success; 2845 } 2846 2847 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) 2848 { 2849 symtab_strtab: 2850 elf_tdata (abfd)->strtab_hdr = *hdr; 2851 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; 2852 goto success; 2853 } 2854 2855 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) 2856 { 2857 dynsymtab_strtab: 2858 elf_tdata (abfd)->dynstrtab_hdr = *hdr; 2859 hdr = &elf_tdata (abfd)->dynstrtab_hdr; 2860 elf_elfsections (abfd)[shindex] = hdr; 2861 /* We also treat this as a regular section, so that objcopy 2862 can handle it. */ 2863 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2864 shindex); 2865 goto success; 2866 } 2867 2868 /* If the string table isn't one of the above, then treat it as a 2869 regular section. We need to scan all the headers to be sure, 2870 just in case this strtab section appeared before the above. */ 2871 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) 2872 { 2873 unsigned int i, num_sec; 2874 2875 num_sec = elf_numsections (abfd); 2876 for (i = 1; i < num_sec; i++) 2877 { 2878 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2879 if (hdr2->sh_link == shindex) 2880 { 2881 /* Prevent endless recursion on broken objects. */ 2882 if (i == shindex) 2883 goto fail; 2884 if (! bfd_section_from_shdr (abfd, i)) 2885 goto fail; 2886 if (elf_onesymtab (abfd) == i) 2887 goto symtab_strtab; 2888 if (elf_dynsymtab (abfd) == i) 2889 goto dynsymtab_strtab; 2890 } 2891 } 2892 } 2893 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2894 goto success; 2895 2896 case SHT_REL: 2897 case SHT_RELA: 2898 case SHT_RELR: 2899 /* *These* do a lot of work -- but build no sections! */ 2900 { 2901 asection *target_sect; 2902 Elf_Internal_Shdr *hdr2, **p_hdr; 2903 unsigned int num_sec = elf_numsections (abfd); 2904 struct bfd_elf_section_data *esdt; 2905 bfd_size_type size; 2906 2907 if (hdr->sh_type == SHT_REL) 2908 size = bed->s->sizeof_rel; 2909 else if (hdr->sh_type == SHT_RELA) 2910 size = bed->s->sizeof_rela; 2911 else 2912 size = bed->s->arch_size / 8; 2913 if (hdr->sh_entsize != size) 2914 goto fail; 2915 2916 /* Check for a bogus link to avoid crashing. */ 2917 if (hdr->sh_link >= num_sec) 2918 { 2919 _bfd_error_handler 2920 /* xgettext:c-format */ 2921 (_("%pB: invalid link %u for reloc section %s (index %u)"), 2922 abfd, hdr->sh_link, name, shindex); 2923 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2924 goto success; 2925 } 2926 2927 /* Get the symbol table. */ 2928 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB 2929 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) 2930 && ! bfd_section_from_shdr (abfd, hdr->sh_link)) 2931 goto fail; 2932 2933 /* If this is an alloc section in an executable or shared 2934 library, or the reloc section does not use the main symbol 2935 table we don't treat it as a reloc section. BFD can't 2936 adequately represent such a section, so at least for now, 2937 we don't try. We just present it as a normal section. We 2938 also can't use it as a reloc section if it points to the 2939 null section, an invalid section, another reloc section, or 2940 its sh_link points to the null section. */ 2941 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0 2942 && (hdr->sh_flags & SHF_ALLOC) != 0) 2943 || (hdr->sh_flags & SHF_COMPRESSED) != 0 2944 || hdr->sh_type == SHT_RELR 2945 || hdr->sh_link == SHN_UNDEF 2946 || hdr->sh_link != elf_onesymtab (abfd) 2947 || hdr->sh_info == SHN_UNDEF 2948 || hdr->sh_info >= num_sec 2949 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL 2950 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) 2951 { 2952 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2953 goto success; 2954 } 2955 2956 if (! bfd_section_from_shdr (abfd, hdr->sh_info)) 2957 goto fail; 2958 2959 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); 2960 if (target_sect == NULL) 2961 goto fail; 2962 2963 esdt = elf_section_data (target_sect); 2964 if (hdr->sh_type == SHT_RELA) 2965 p_hdr = &esdt->rela.hdr; 2966 else 2967 p_hdr = &esdt->rel.hdr; 2968 2969 /* PR 17512: file: 0b4f81b7. 2970 Also see PR 24456, for a file which deliberately has two reloc 2971 sections. */ 2972 if (*p_hdr != NULL) 2973 { 2974 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex)) 2975 { 2976 _bfd_error_handler 2977 /* xgettext:c-format */ 2978 (_("%pB: warning: secondary relocation section '%s' " 2979 "for section %pA found - ignoring"), 2980 abfd, name, target_sect); 2981 } 2982 else 2983 esdt->has_secondary_relocs = true; 2984 goto success; 2985 } 2986 2987 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); 2988 if (hdr2 == NULL) 2989 goto fail; 2990 *hdr2 = *hdr; 2991 *p_hdr = hdr2; 2992 elf_elfsections (abfd)[shindex] = hdr2; 2993 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr) 2994 * bed->s->int_rels_per_ext_rel); 2995 target_sect->flags |= SEC_RELOC; 2996 target_sect->relocation = NULL; 2997 target_sect->rel_filepos = hdr->sh_offset; 2998 /* In the section to which the relocations apply, mark whether 2999 its relocations are of the REL or RELA variety. */ 3000 if (hdr->sh_size != 0) 3001 { 3002 if (hdr->sh_type == SHT_RELA) 3003 target_sect->use_rela_p = 1; 3004 } 3005 abfd->flags |= HAS_RELOC; 3006 goto success; 3007 } 3008 3009 case SHT_GNU_verdef: 3010 if (hdr->sh_info != 0) 3011 elf_dynverdef (abfd) = shindex; 3012 elf_tdata (abfd)->dynverdef_hdr = *hdr; 3013 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 3014 goto success; 3015 3016 case SHT_GNU_versym: 3017 if (hdr->sh_entsize != sizeof (Elf_External_Versym)) 3018 goto fail; 3019 3020 elf_dynversym (abfd) = shindex; 3021 elf_tdata (abfd)->dynversym_hdr = *hdr; 3022 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 3023 goto success; 3024 3025 case SHT_GNU_verneed: 3026 if (hdr->sh_info != 0) 3027 elf_dynverref (abfd) = shindex; 3028 elf_tdata (abfd)->dynverref_hdr = *hdr; 3029 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 3030 goto success; 3031 3032 case SHT_SHLIB: 3033 goto success; 3034 3035 case SHT_GROUP: 3036 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) 3037 goto fail; 3038 3039 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 3040 goto fail; 3041 3042 goto success; 3043 3044 default: 3045 /* Possibly an attributes section. */ 3046 if (hdr->sh_type == SHT_GNU_ATTRIBUTES 3047 || hdr->sh_type == bed->obj_attrs_section_type) 3048 { 3049 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 3050 goto fail; 3051 _bfd_elf_parse_attributes (abfd, hdr); 3052 goto success; 3053 } 3054 3055 /* Check for any processor-specific section types. */ 3056 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) 3057 goto success; 3058 3059 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) 3060 { 3061 if ((hdr->sh_flags & SHF_ALLOC) != 0) 3062 /* FIXME: How to properly handle allocated section reserved 3063 for applications? */ 3064 _bfd_error_handler 3065 /* xgettext:c-format */ 3066 (_("%pB: unknown type [%#x] section `%s'"), 3067 abfd, hdr->sh_type, name); 3068 else 3069 { 3070 /* Allow sections reserved for applications. */ 3071 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 3072 goto success; 3073 } 3074 } 3075 else if (hdr->sh_type >= SHT_LOPROC 3076 && hdr->sh_type <= SHT_HIPROC) 3077 /* FIXME: We should handle this section. */ 3078 _bfd_error_handler 3079 /* xgettext:c-format */ 3080 (_("%pB: unknown type [%#x] section `%s'"), 3081 abfd, hdr->sh_type, name); 3082 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) 3083 { 3084 /* Unrecognised OS-specific sections. */ 3085 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) 3086 /* SHF_OS_NONCONFORMING indicates that special knowledge is 3087 required to correctly process the section and the file should 3088 be rejected with an error message. */ 3089 _bfd_error_handler 3090 /* xgettext:c-format */ 3091 (_("%pB: unknown type [%#x] section `%s'"), 3092 abfd, hdr->sh_type, name); 3093 else 3094 { 3095 /* Otherwise it should be processed. */ 3096 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 3097 goto success; 3098 } 3099 } 3100 else 3101 /* FIXME: We should handle this section. */ 3102 _bfd_error_handler 3103 /* xgettext:c-format */ 3104 (_("%pB: unknown type [%#x] section `%s'"), 3105 abfd, hdr->sh_type, name); 3106 3107 goto fail; 3108 } 3109 3110 fail: 3111 ret = false; 3112 success: 3113 elf_tdata (abfd)->being_created[shindex] = false; 3114 return ret; 3115 } 3116 3117 /* Return the local symbol specified by ABFD, R_SYMNDX. */ 3118 3119 Elf_Internal_Sym * 3120 bfd_sym_from_r_symndx (struct sym_cache *cache, 3121 bfd *abfd, 3122 unsigned long r_symndx) 3123 { 3124 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; 3125 3126 if (cache->abfd != abfd || cache->indx[ent] != r_symndx) 3127 { 3128 Elf_Internal_Shdr *symtab_hdr; 3129 unsigned char esym[sizeof (Elf64_External_Sym)]; 3130 Elf_External_Sym_Shndx eshndx; 3131 3132 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 3133 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, 3134 &cache->sym[ent], esym, &eshndx) == NULL) 3135 return NULL; 3136 3137 if (cache->abfd != abfd) 3138 { 3139 memset (cache->indx, -1, sizeof (cache->indx)); 3140 cache->abfd = abfd; 3141 } 3142 cache->indx[ent] = r_symndx; 3143 } 3144 3145 return &cache->sym[ent]; 3146 } 3147 3148 /* Given an ELF section number, retrieve the corresponding BFD 3149 section. */ 3150 3151 asection * 3152 bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) 3153 { 3154 if (sec_index >= elf_numsections (abfd)) 3155 return NULL; 3156 return elf_elfsections (abfd)[sec_index]->bfd_section; 3157 } 3158 3159 static const struct bfd_elf_special_section special_sections_b[] = 3160 { 3161 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3162 { NULL, 0, 0, 0, 0 } 3163 }; 3164 3165 static const struct bfd_elf_special_section special_sections_c[] = 3166 { 3167 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, 3168 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 }, 3169 { NULL, 0, 0, 0, 0 } 3170 }; 3171 3172 static const struct bfd_elf_special_section special_sections_d[] = 3173 { 3174 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3175 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3176 /* There are more DWARF sections than these, but they needn't be added here 3177 unless you have to cope with broken compilers that don't emit section 3178 attributes or you want to help the user writing assembler. */ 3179 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, 3180 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, 3181 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, 3182 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, 3183 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, 3184 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, 3185 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, 3186 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, 3187 { NULL, 0, 0, 0, 0 } 3188 }; 3189 3190 static const struct bfd_elf_special_section special_sections_f[] = 3191 { 3192 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3193 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, 3194 { NULL, 0 , 0, 0, 0 } 3195 }; 3196 3197 static const struct bfd_elf_special_section special_sections_g[] = 3198 { 3199 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3200 { STRING_COMMA_LEN (".gnu.linkonce.n"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3201 { STRING_COMMA_LEN (".gnu.linkonce.p"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3202 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, 3203 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3204 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, 3205 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, 3206 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, 3207 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, 3208 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, 3209 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, 3210 { NULL, 0, 0, 0, 0 } 3211 }; 3212 3213 static const struct bfd_elf_special_section special_sections_h[] = 3214 { 3215 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, 3216 { NULL, 0, 0, 0, 0 } 3217 }; 3218 3219 static const struct bfd_elf_special_section special_sections_i[] = 3220 { 3221 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3222 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, 3223 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, 3224 { NULL, 0, 0, 0, 0 } 3225 }; 3226 3227 static const struct bfd_elf_special_section special_sections_l[] = 3228 { 3229 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, 3230 { NULL, 0, 0, 0, 0 } 3231 }; 3232 3233 static const struct bfd_elf_special_section special_sections_n[] = 3234 { 3235 { STRING_COMMA_LEN (".noinit"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3236 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, 3237 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, 3238 { NULL, 0, 0, 0, 0 } 3239 }; 3240 3241 static const struct bfd_elf_special_section special_sections_p[] = 3242 { 3243 { STRING_COMMA_LEN (".persistent.bss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 3244 { STRING_COMMA_LEN (".persistent"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 3245 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, 3246 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3247 { NULL, 0, 0, 0, 0 } 3248 }; 3249 3250 static const struct bfd_elf_special_section special_sections_r[] = 3251 { 3252 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, 3253 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, 3254 { STRING_COMMA_LEN (".relr.dyn"), 0, SHT_RELR, SHF_ALLOC }, 3255 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, 3256 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, 3257 { NULL, 0, 0, 0, 0 } 3258 }; 3259 3260 static const struct bfd_elf_special_section special_sections_s[] = 3261 { 3262 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, 3263 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, 3264 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, 3265 /* See struct bfd_elf_special_section declaration for the semantics of 3266 this special case where .prefix_length != strlen (.prefix). */ 3267 { ".stabstr", 5, 3, SHT_STRTAB, 0 }, 3268 { NULL, 0, 0, 0, 0 } 3269 }; 3270 3271 static const struct bfd_elf_special_section special_sections_t[] = 3272 { 3273 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 3274 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, 3275 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, 3276 { NULL, 0, 0, 0, 0 } 3277 }; 3278 3279 static const struct bfd_elf_special_section special_sections_z[] = 3280 { 3281 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, 3282 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, 3283 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, 3284 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, 3285 { NULL, 0, 0, 0, 0 } 3286 }; 3287 3288 static const struct bfd_elf_special_section * const special_sections[] = 3289 { 3290 special_sections_b, /* 'b' */ 3291 special_sections_c, /* 'c' */ 3292 special_sections_d, /* 'd' */ 3293 NULL, /* 'e' */ 3294 special_sections_f, /* 'f' */ 3295 special_sections_g, /* 'g' */ 3296 special_sections_h, /* 'h' */ 3297 special_sections_i, /* 'i' */ 3298 NULL, /* 'j' */ 3299 NULL, /* 'k' */ 3300 special_sections_l, /* 'l' */ 3301 NULL, /* 'm' */ 3302 special_sections_n, /* 'n' */ 3303 NULL, /* 'o' */ 3304 special_sections_p, /* 'p' */ 3305 NULL, /* 'q' */ 3306 special_sections_r, /* 'r' */ 3307 special_sections_s, /* 's' */ 3308 special_sections_t, /* 't' */ 3309 NULL, /* 'u' */ 3310 NULL, /* 'v' */ 3311 NULL, /* 'w' */ 3312 NULL, /* 'x' */ 3313 NULL, /* 'y' */ 3314 special_sections_z /* 'z' */ 3315 }; 3316 3317 const struct bfd_elf_special_section * 3318 _bfd_elf_get_special_section (const char *name, 3319 const struct bfd_elf_special_section *spec, 3320 unsigned int rela) 3321 { 3322 int i; 3323 int len; 3324 3325 len = strlen (name); 3326 3327 for (i = 0; spec[i].prefix != NULL; i++) 3328 { 3329 int suffix_len; 3330 int prefix_len = spec[i].prefix_length; 3331 3332 if (len < prefix_len) 3333 continue; 3334 if (memcmp (name, spec[i].prefix, prefix_len) != 0) 3335 continue; 3336 3337 suffix_len = spec[i].suffix_length; 3338 if (suffix_len <= 0) 3339 { 3340 if (name[prefix_len] != 0) 3341 { 3342 if (suffix_len == 0) 3343 continue; 3344 if (name[prefix_len] != '.' 3345 && (suffix_len == -2 3346 || (rela && spec[i].type == SHT_REL))) 3347 continue; 3348 } 3349 } 3350 else 3351 { 3352 if (len < prefix_len + suffix_len) 3353 continue; 3354 if (memcmp (name + len - suffix_len, 3355 spec[i].prefix + prefix_len, 3356 suffix_len) != 0) 3357 continue; 3358 } 3359 return &spec[i]; 3360 } 3361 3362 return NULL; 3363 } 3364 3365 const struct bfd_elf_special_section * 3366 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) 3367 { 3368 int i; 3369 const struct bfd_elf_special_section *spec; 3370 const struct elf_backend_data *bed; 3371 3372 /* See if this is one of the special sections. */ 3373 if (sec->name == NULL) 3374 return NULL; 3375 3376 bed = get_elf_backend_data (abfd); 3377 spec = bed->special_sections; 3378 if (spec) 3379 { 3380 spec = _bfd_elf_get_special_section (sec->name, 3381 bed->special_sections, 3382 sec->use_rela_p); 3383 if (spec != NULL) 3384 return spec; 3385 } 3386 3387 if (sec->name[0] != '.') 3388 return NULL; 3389 3390 i = sec->name[1] - 'b'; 3391 if (i < 0 || i > 'z' - 'b') 3392 return NULL; 3393 3394 spec = special_sections[i]; 3395 3396 if (spec == NULL) 3397 return NULL; 3398 3399 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); 3400 } 3401 3402 bool 3403 _bfd_elf_new_section_hook (bfd *abfd, asection *sec) 3404 { 3405 struct bfd_elf_section_data *sdata; 3406 const struct elf_backend_data *bed; 3407 const struct bfd_elf_special_section *ssect; 3408 3409 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; 3410 if (sdata == NULL) 3411 { 3412 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, 3413 sizeof (*sdata)); 3414 if (sdata == NULL) 3415 return false; 3416 sec->used_by_bfd = sdata; 3417 } 3418 3419 /* Indicate whether or not this section should use RELA relocations. */ 3420 bed = get_elf_backend_data (abfd); 3421 sec->use_rela_p = bed->default_use_rela_p; 3422 3423 /* Set up ELF section type and flags for newly created sections, if 3424 there is an ABI mandated section. */ 3425 ssect = (*bed->get_sec_type_attr) (abfd, sec); 3426 if (ssect != NULL) 3427 { 3428 elf_section_type (sec) = ssect->type; 3429 elf_section_flags (sec) = ssect->attr; 3430 } 3431 3432 return _bfd_generic_new_section_hook (abfd, sec); 3433 } 3434 3435 /* Create a new bfd section from an ELF program header. 3436 3437 Since program segments have no names, we generate a synthetic name 3438 of the form segment<NUM>, where NUM is generally the index in the 3439 program header table. For segments that are split (see below) we 3440 generate the names segment<NUM>a and segment<NUM>b. 3441 3442 Note that some program segments may have a file size that is different than 3443 (less than) the memory size. All this means is that at execution the 3444 system must allocate the amount of memory specified by the memory size, 3445 but only initialize it with the first "file size" bytes read from the 3446 file. This would occur for example, with program segments consisting 3447 of combined data+bss. 3448 3449 To handle the above situation, this routine generates TWO bfd sections 3450 for the single program segment. The first has the length specified by 3451 the file size of the segment, and the second has the length specified 3452 by the difference between the two sizes. In effect, the segment is split 3453 into its initialized and uninitialized parts. */ 3454 3455 bool 3456 _bfd_elf_make_section_from_phdr (bfd *abfd, 3457 Elf_Internal_Phdr *hdr, 3458 int hdr_index, 3459 const char *type_name) 3460 { 3461 asection *newsect; 3462 char *name; 3463 char namebuf[64]; 3464 size_t len; 3465 int split; 3466 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 3467 3468 split = ((hdr->p_memsz > 0) 3469 && (hdr->p_filesz > 0) 3470 && (hdr->p_memsz > hdr->p_filesz)); 3471 3472 if (hdr->p_filesz > 0) 3473 { 3474 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); 3475 len = strlen (namebuf) + 1; 3476 name = (char *) bfd_alloc (abfd, len); 3477 if (!name) 3478 return false; 3479 memcpy (name, namebuf, len); 3480 newsect = bfd_make_section (abfd, name); 3481 if (newsect == NULL) 3482 return false; 3483 newsect->vma = hdr->p_vaddr / opb; 3484 newsect->lma = hdr->p_paddr / opb; 3485 newsect->size = hdr->p_filesz; 3486 newsect->filepos = hdr->p_offset; 3487 newsect->flags |= SEC_HAS_CONTENTS; 3488 newsect->alignment_power = bfd_log2 (hdr->p_align); 3489 if (hdr->p_type == PT_LOAD) 3490 { 3491 newsect->flags |= SEC_ALLOC; 3492 newsect->flags |= SEC_LOAD; 3493 if (hdr->p_flags & PF_X) 3494 { 3495 /* FIXME: all we known is that it has execute PERMISSION, 3496 may be data. */ 3497 newsect->flags |= SEC_CODE; 3498 } 3499 } 3500 if (!(hdr->p_flags & PF_W)) 3501 { 3502 newsect->flags |= SEC_READONLY; 3503 } 3504 } 3505 3506 if (hdr->p_memsz > hdr->p_filesz) 3507 { 3508 bfd_vma align; 3509 3510 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); 3511 len = strlen (namebuf) + 1; 3512 name = (char *) bfd_alloc (abfd, len); 3513 if (!name) 3514 return false; 3515 memcpy (name, namebuf, len); 3516 newsect = bfd_make_section (abfd, name); 3517 if (newsect == NULL) 3518 return false; 3519 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb; 3520 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb; 3521 newsect->size = hdr->p_memsz - hdr->p_filesz; 3522 newsect->filepos = hdr->p_offset + hdr->p_filesz; 3523 align = newsect->vma & -newsect->vma; 3524 if (align == 0 || align > hdr->p_align) 3525 align = hdr->p_align; 3526 newsect->alignment_power = bfd_log2 (align); 3527 if (hdr->p_type == PT_LOAD) 3528 { 3529 newsect->flags |= SEC_ALLOC; 3530 if (hdr->p_flags & PF_X) 3531 newsect->flags |= SEC_CODE; 3532 } 3533 if (!(hdr->p_flags & PF_W)) 3534 newsect->flags |= SEC_READONLY; 3535 } 3536 3537 return true; 3538 } 3539 3540 static bool 3541 _bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset) 3542 { 3543 /* The return value is ignored. Build-ids are considered optional. */ 3544 if (templ->xvec->flavour == bfd_target_elf_flavour) 3545 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id) 3546 (templ, offset); 3547 return false; 3548 } 3549 3550 bool 3551 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) 3552 { 3553 const struct elf_backend_data *bed; 3554 3555 switch (hdr->p_type) 3556 { 3557 case PT_NULL: 3558 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); 3559 3560 case PT_LOAD: 3561 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load")) 3562 return false; 3563 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL) 3564 _bfd_elf_core_find_build_id (abfd, hdr->p_offset); 3565 return true; 3566 3567 case PT_DYNAMIC: 3568 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); 3569 3570 case PT_INTERP: 3571 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); 3572 3573 case PT_NOTE: 3574 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) 3575 return false; 3576 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz, 3577 hdr->p_align)) 3578 return false; 3579 return true; 3580 3581 case PT_SHLIB: 3582 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); 3583 3584 case PT_PHDR: 3585 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); 3586 3587 case PT_GNU_EH_FRAME: 3588 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, 3589 "eh_frame_hdr"); 3590 3591 case PT_GNU_STACK: 3592 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); 3593 3594 case PT_GNU_RELRO: 3595 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); 3596 3597 case PT_GNU_SFRAME: 3598 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, 3599 "sframe"); 3600 3601 default: 3602 /* Check for any processor-specific program segment types. */ 3603 bed = get_elf_backend_data (abfd); 3604 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); 3605 } 3606 } 3607 3608 /* Return the REL_HDR for SEC, assuming there is only a single one, either 3609 REL or RELA. */ 3610 3611 Elf_Internal_Shdr * 3612 _bfd_elf_single_rel_hdr (asection *sec) 3613 { 3614 if (elf_section_data (sec)->rel.hdr) 3615 { 3616 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); 3617 return elf_section_data (sec)->rel.hdr; 3618 } 3619 else 3620 return elf_section_data (sec)->rela.hdr; 3621 } 3622 3623 static bool 3624 _bfd_elf_set_reloc_sh_name (bfd *abfd, 3625 Elf_Internal_Shdr *rel_hdr, 3626 const char *sec_name, 3627 bool use_rela_p) 3628 { 3629 char *name = (char *) bfd_alloc (abfd, 3630 sizeof ".rela" + strlen (sec_name)); 3631 if (name == NULL) 3632 return false; 3633 3634 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); 3635 rel_hdr->sh_name = 3636 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, 3637 false); 3638 if (rel_hdr->sh_name == (unsigned int) -1) 3639 return false; 3640 3641 return true; 3642 } 3643 3644 /* Allocate and initialize a section-header for a new reloc section, 3645 containing relocations against ASECT. It is stored in RELDATA. If 3646 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL 3647 relocations. */ 3648 3649 static bool 3650 _bfd_elf_init_reloc_shdr (bfd *abfd, 3651 struct bfd_elf_section_reloc_data *reldata, 3652 const char *sec_name, 3653 bool use_rela_p, 3654 bool delay_st_name_p) 3655 { 3656 Elf_Internal_Shdr *rel_hdr; 3657 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3658 3659 BFD_ASSERT (reldata->hdr == NULL); 3660 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); 3661 if (rel_hdr == NULL) 3662 return false; 3663 reldata->hdr = rel_hdr; 3664 3665 if (delay_st_name_p) 3666 rel_hdr->sh_name = (unsigned int) -1; 3667 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, 3668 use_rela_p)) 3669 return false; 3670 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; 3671 rel_hdr->sh_entsize = (use_rela_p 3672 ? bed->s->sizeof_rela 3673 : bed->s->sizeof_rel); 3674 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 3675 rel_hdr->sh_flags = 0; 3676 rel_hdr->sh_addr = 0; 3677 rel_hdr->sh_size = 0; 3678 rel_hdr->sh_offset = 0; 3679 3680 return true; 3681 } 3682 3683 /* Return the default section type based on the passed in section flags. */ 3684 3685 int 3686 bfd_elf_get_default_section_type (flagword flags) 3687 { 3688 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0 3689 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) 3690 return SHT_NOBITS; 3691 return SHT_PROGBITS; 3692 } 3693 3694 struct fake_section_arg 3695 { 3696 struct bfd_link_info *link_info; 3697 bool failed; 3698 }; 3699 3700 /* Set up an ELF internal section header for a section. */ 3701 3702 static void 3703 elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) 3704 { 3705 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; 3706 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3707 struct bfd_elf_section_data *esd = elf_section_data (asect); 3708 Elf_Internal_Shdr *this_hdr; 3709 unsigned int sh_type; 3710 const char *name = asect->name; 3711 bool delay_st_name_p = false; 3712 bfd_vma mask; 3713 3714 if (arg->failed) 3715 { 3716 /* We already failed; just get out of the bfd_map_over_sections 3717 loop. */ 3718 return; 3719 } 3720 3721 this_hdr = &esd->this_hdr; 3722 3723 /* ld: compress DWARF debug sections with names: .debug_*. */ 3724 if (arg->link_info 3725 && (abfd->flags & BFD_COMPRESS) != 0 3726 && (asect->flags & SEC_DEBUGGING) != 0 3727 && name[1] == 'd' 3728 && name[6] == '_') 3729 { 3730 /* If this section will be compressed, delay adding section 3731 name to section name section after it is compressed in 3732 _bfd_elf_assign_file_positions_for_non_load. */ 3733 delay_st_name_p = true; 3734 } 3735 3736 if (delay_st_name_p) 3737 this_hdr->sh_name = (unsigned int) -1; 3738 else 3739 { 3740 this_hdr->sh_name 3741 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 3742 name, false); 3743 if (this_hdr->sh_name == (unsigned int) -1) 3744 { 3745 arg->failed = true; 3746 return; 3747 } 3748 } 3749 3750 /* Don't clear sh_flags. Assembler may set additional bits. */ 3751 3752 if ((asect->flags & SEC_ALLOC) != 0 3753 || asect->user_set_vma) 3754 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect); 3755 else 3756 this_hdr->sh_addr = 0; 3757 3758 this_hdr->sh_offset = 0; 3759 this_hdr->sh_size = asect->size; 3760 this_hdr->sh_link = 0; 3761 /* PR 17512: file: 0eb809fe, 8b0535ee. */ 3762 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) 3763 { 3764 _bfd_error_handler 3765 /* xgettext:c-format */ 3766 (_("%pB: error: alignment power %d of section `%pA' is too big"), 3767 abfd, asect->alignment_power, asect); 3768 arg->failed = true; 3769 return; 3770 } 3771 /* Set sh_addralign to the highest power of two given by alignment 3772 consistent with the section VMA. Linker scripts can force VMA. */ 3773 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr; 3774 this_hdr->sh_addralign = mask & -mask; 3775 /* The sh_entsize and sh_info fields may have been set already by 3776 copy_private_section_data. */ 3777 3778 this_hdr->bfd_section = asect; 3779 this_hdr->contents = NULL; 3780 3781 /* If the section type is unspecified, we set it based on 3782 asect->flags. */ 3783 if (asect->type != 0) 3784 sh_type = asect->type; 3785 else if ((asect->flags & SEC_GROUP) != 0) 3786 sh_type = SHT_GROUP; 3787 else 3788 sh_type = bfd_elf_get_default_section_type (asect->flags); 3789 3790 if (this_hdr->sh_type == SHT_NULL) 3791 this_hdr->sh_type = sh_type; 3792 else if (this_hdr->sh_type == SHT_NOBITS 3793 && sh_type == SHT_PROGBITS 3794 && (asect->flags & SEC_ALLOC) != 0) 3795 { 3796 /* Warn if we are changing a NOBITS section to PROGBITS, but 3797 allow the link to proceed. This can happen when users link 3798 non-bss input sections to bss output sections, or emit data 3799 to a bss output section via a linker script. */ 3800 _bfd_error_handler 3801 (_("warning: section `%pA' type changed to PROGBITS"), asect); 3802 this_hdr->sh_type = sh_type; 3803 } 3804 3805 switch (this_hdr->sh_type) 3806 { 3807 default: 3808 break; 3809 3810 case SHT_STRTAB: 3811 case SHT_NOTE: 3812 case SHT_NOBITS: 3813 case SHT_PROGBITS: 3814 break; 3815 3816 case SHT_INIT_ARRAY: 3817 case SHT_FINI_ARRAY: 3818 case SHT_PREINIT_ARRAY: 3819 this_hdr->sh_entsize = bed->s->arch_size / 8; 3820 break; 3821 3822 case SHT_HASH: 3823 this_hdr->sh_entsize = bed->s->sizeof_hash_entry; 3824 break; 3825 3826 case SHT_DYNSYM: 3827 this_hdr->sh_entsize = bed->s->sizeof_sym; 3828 break; 3829 3830 case SHT_DYNAMIC: 3831 this_hdr->sh_entsize = bed->s->sizeof_dyn; 3832 break; 3833 3834 case SHT_RELA: 3835 if (get_elf_backend_data (abfd)->may_use_rela_p) 3836 this_hdr->sh_entsize = bed->s->sizeof_rela; 3837 break; 3838 3839 case SHT_REL: 3840 if (get_elf_backend_data (abfd)->may_use_rel_p) 3841 this_hdr->sh_entsize = bed->s->sizeof_rel; 3842 break; 3843 3844 case SHT_GNU_versym: 3845 this_hdr->sh_entsize = sizeof (Elf_External_Versym); 3846 break; 3847 3848 case SHT_GNU_verdef: 3849 this_hdr->sh_entsize = 0; 3850 /* objcopy or strip will copy over sh_info, but may not set 3851 cverdefs. The linker will set cverdefs, but sh_info will be 3852 zero. */ 3853 if (this_hdr->sh_info == 0) 3854 this_hdr->sh_info = elf_tdata (abfd)->cverdefs; 3855 else 3856 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 3857 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); 3858 break; 3859 3860 case SHT_GNU_verneed: 3861 this_hdr->sh_entsize = 0; 3862 /* objcopy or strip will copy over sh_info, but may not set 3863 cverrefs. The linker will set cverrefs, but sh_info will be 3864 zero. */ 3865 if (this_hdr->sh_info == 0) 3866 this_hdr->sh_info = elf_tdata (abfd)->cverrefs; 3867 else 3868 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 3869 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); 3870 break; 3871 3872 case SHT_GROUP: 3873 this_hdr->sh_entsize = GRP_ENTRY_SIZE; 3874 break; 3875 3876 case SHT_GNU_HASH: 3877 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; 3878 break; 3879 } 3880 3881 if ((asect->flags & SEC_ALLOC) != 0) 3882 this_hdr->sh_flags |= SHF_ALLOC; 3883 if ((asect->flags & SEC_READONLY) == 0) 3884 this_hdr->sh_flags |= SHF_WRITE; 3885 if ((asect->flags & SEC_CODE) != 0) 3886 this_hdr->sh_flags |= SHF_EXECINSTR; 3887 if ((asect->flags & SEC_MERGE) != 0) 3888 { 3889 this_hdr->sh_flags |= SHF_MERGE; 3890 this_hdr->sh_entsize = asect->entsize; 3891 } 3892 if ((asect->flags & SEC_STRINGS) != 0) 3893 this_hdr->sh_flags |= SHF_STRINGS; 3894 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) 3895 this_hdr->sh_flags |= SHF_GROUP; 3896 if ((asect->flags & SEC_THREAD_LOCAL) != 0) 3897 { 3898 this_hdr->sh_flags |= SHF_TLS; 3899 if (asect->size == 0 3900 && (asect->flags & SEC_HAS_CONTENTS) == 0) 3901 { 3902 struct bfd_link_order *o = asect->map_tail.link_order; 3903 3904 this_hdr->sh_size = 0; 3905 if (o != NULL) 3906 { 3907 this_hdr->sh_size = o->offset + o->size; 3908 if (this_hdr->sh_size != 0) 3909 this_hdr->sh_type = SHT_NOBITS; 3910 } 3911 } 3912 } 3913 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) 3914 this_hdr->sh_flags |= SHF_EXCLUDE; 3915 3916 /* If the section has relocs, set up a section header for the 3917 SHT_REL[A] section. If two relocation sections are required for 3918 this section, it is up to the processor-specific back-end to 3919 create the other. */ 3920 if ((asect->flags & SEC_RELOC) != 0) 3921 { 3922 /* When doing a relocatable link, create both REL and RELA sections if 3923 needed. */ 3924 if (arg->link_info 3925 /* Do the normal setup if we wouldn't create any sections here. */ 3926 && esd->rel.count + esd->rela.count > 0 3927 && (bfd_link_relocatable (arg->link_info) 3928 || arg->link_info->emitrelocations)) 3929 { 3930 if (esd->rel.count && esd->rel.hdr == NULL 3931 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, 3932 false, delay_st_name_p)) 3933 { 3934 arg->failed = true; 3935 return; 3936 } 3937 if (esd->rela.count && esd->rela.hdr == NULL 3938 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, 3939 true, delay_st_name_p)) 3940 { 3941 arg->failed = true; 3942 return; 3943 } 3944 } 3945 else if (!_bfd_elf_init_reloc_shdr (abfd, 3946 (asect->use_rela_p 3947 ? &esd->rela : &esd->rel), 3948 name, 3949 asect->use_rela_p, 3950 delay_st_name_p)) 3951 { 3952 arg->failed = true; 3953 return; 3954 } 3955 } 3956 3957 /* Check for processor-specific section types. */ 3958 sh_type = this_hdr->sh_type; 3959 if (bed->elf_backend_fake_sections 3960 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) 3961 { 3962 arg->failed = true; 3963 return; 3964 } 3965 3966 if (sh_type == SHT_NOBITS && asect->size != 0) 3967 { 3968 /* Don't change the header type from NOBITS if we are being 3969 called for objcopy --only-keep-debug. */ 3970 this_hdr->sh_type = sh_type; 3971 } 3972 } 3973 3974 /* Fill in the contents of a SHT_GROUP section. Called from 3975 _bfd_elf_compute_section_file_positions for gas, objcopy, and 3976 when ELF targets use the generic linker, ld. Called for ld -r 3977 from bfd_elf_final_link. */ 3978 3979 void 3980 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) 3981 { 3982 bool *failedptr = (bool *) failedptrarg; 3983 asection *elt, *first; 3984 unsigned char *loc; 3985 bool gas; 3986 3987 /* Ignore linker created group section. See elfNN_ia64_object_p in 3988 elfxx-ia64.c. */ 3989 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP 3990 || sec->size == 0 3991 || *failedptr) 3992 return; 3993 3994 if (elf_section_data (sec)->this_hdr.sh_info == 0) 3995 { 3996 unsigned long symindx = 0; 3997 3998 /* elf_group_id will have been set up by objcopy and the 3999 generic linker. */ 4000 if (elf_group_id (sec) != NULL) 4001 symindx = elf_group_id (sec)->udata.i; 4002 4003 if (symindx == 0) 4004 { 4005 /* If called from the assembler, swap_out_syms will have set up 4006 elf_section_syms. 4007 PR 25699: A corrupt input file could contain bogus group info. */ 4008 if (sec->index >= elf_num_section_syms (abfd) 4009 || elf_section_syms (abfd)[sec->index] == NULL) 4010 { 4011 *failedptr = true; 4012 return; 4013 } 4014 symindx = elf_section_syms (abfd)[sec->index]->udata.i; 4015 } 4016 elf_section_data (sec)->this_hdr.sh_info = symindx; 4017 } 4018 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) 4019 { 4020 /* The ELF backend linker sets sh_info to -2 when the group 4021 signature symbol is global, and thus the index can't be 4022 set until all local symbols are output. */ 4023 asection *igroup; 4024 struct bfd_elf_section_data *sec_data; 4025 unsigned long symndx; 4026 unsigned long extsymoff; 4027 struct elf_link_hash_entry *h; 4028 4029 /* The point of this little dance to the first SHF_GROUP section 4030 then back to the SHT_GROUP section is that this gets us to 4031 the SHT_GROUP in the input object. */ 4032 igroup = elf_sec_group (elf_next_in_group (sec)); 4033 sec_data = elf_section_data (igroup); 4034 symndx = sec_data->this_hdr.sh_info; 4035 extsymoff = 0; 4036 if (!elf_bad_symtab (igroup->owner)) 4037 { 4038 Elf_Internal_Shdr *symtab_hdr; 4039 4040 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; 4041 extsymoff = symtab_hdr->sh_info; 4042 } 4043 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; 4044 while (h->root.type == bfd_link_hash_indirect 4045 || h->root.type == bfd_link_hash_warning) 4046 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4047 4048 elf_section_data (sec)->this_hdr.sh_info = h->indx; 4049 } 4050 4051 /* The contents won't be allocated for "ld -r" or objcopy. */ 4052 gas = true; 4053 if (sec->contents == NULL) 4054 { 4055 gas = false; 4056 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); 4057 4058 /* Arrange for the section to be written out. */ 4059 elf_section_data (sec)->this_hdr.contents = sec->contents; 4060 if (sec->contents == NULL) 4061 { 4062 *failedptr = true; 4063 return; 4064 } 4065 } 4066 4067 loc = sec->contents + sec->size; 4068 4069 /* Get the pointer to the first section in the group that gas 4070 squirreled away here. objcopy arranges for this to be set to the 4071 start of the input section group. */ 4072 first = elt = elf_next_in_group (sec); 4073 4074 /* First element is a flag word. Rest of section is elf section 4075 indices for all the sections of the group. Write them backwards 4076 just to keep the group in the same order as given in .section 4077 directives, not that it matters. */ 4078 while (elt != NULL) 4079 { 4080 asection *s; 4081 4082 s = elt; 4083 if (!gas) 4084 s = s->output_section; 4085 if (s != NULL 4086 && !bfd_is_abs_section (s)) 4087 { 4088 struct bfd_elf_section_data *elf_sec = elf_section_data (s); 4089 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt); 4090 4091 if (elf_sec->rel.hdr != NULL 4092 && (gas 4093 || (input_elf_sec->rel.hdr != NULL 4094 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)) 4095 { 4096 elf_sec->rel.hdr->sh_flags |= SHF_GROUP; 4097 loc -= 4; 4098 if (loc == sec->contents) 4099 break; 4100 H_PUT_32 (abfd, elf_sec->rel.idx, loc); 4101 } 4102 if (elf_sec->rela.hdr != NULL 4103 && (gas 4104 || (input_elf_sec->rela.hdr != NULL 4105 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)) 4106 { 4107 elf_sec->rela.hdr->sh_flags |= SHF_GROUP; 4108 loc -= 4; 4109 if (loc == sec->contents) 4110 break; 4111 H_PUT_32 (abfd, elf_sec->rela.idx, loc); 4112 } 4113 loc -= 4; 4114 if (loc == sec->contents) 4115 break; 4116 H_PUT_32 (abfd, elf_sec->this_idx, loc); 4117 } 4118 elt = elf_next_in_group (elt); 4119 if (elt == first) 4120 break; 4121 } 4122 4123 /* We should always get here with loc == sec->contents + 4, but it is 4124 possible to craft bogus SHT_GROUP sections that will cause segfaults 4125 in objcopy without checking loc here and in the loop above. */ 4126 if (loc == sec->contents) 4127 BFD_ASSERT (0); 4128 else 4129 { 4130 loc -= 4; 4131 if (loc != sec->contents) 4132 { 4133 BFD_ASSERT (0); 4134 memset (sec->contents + 4, 0, loc - sec->contents); 4135 loc = sec->contents; 4136 } 4137 } 4138 4139 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); 4140 } 4141 4142 /* Given NAME, the name of a relocation section stripped of its 4143 .rel/.rela prefix, return the section in ABFD to which the 4144 relocations apply. */ 4145 4146 asection * 4147 _bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name) 4148 { 4149 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt 4150 section likely apply to .got.plt or .got section. */ 4151 if (get_elf_backend_data (abfd)->want_got_plt 4152 && strcmp (name, ".plt") == 0) 4153 { 4154 asection *sec; 4155 4156 name = ".got.plt"; 4157 sec = bfd_get_section_by_name (abfd, name); 4158 if (sec != NULL) 4159 return sec; 4160 name = ".got"; 4161 } 4162 4163 return bfd_get_section_by_name (abfd, name); 4164 } 4165 4166 /* Return the section to which RELOC_SEC applies. */ 4167 4168 static asection * 4169 elf_get_reloc_section (asection *reloc_sec) 4170 { 4171 const char *name; 4172 unsigned int type; 4173 bfd *abfd; 4174 const struct elf_backend_data *bed; 4175 4176 type = elf_section_data (reloc_sec)->this_hdr.sh_type; 4177 if (type != SHT_REL && type != SHT_RELA) 4178 return NULL; 4179 4180 /* We look up the section the relocs apply to by name. */ 4181 name = reloc_sec->name; 4182 if (!startswith (name, ".rel")) 4183 return NULL; 4184 name += 4; 4185 if (type == SHT_RELA && *name++ != 'a') 4186 return NULL; 4187 4188 abfd = reloc_sec->owner; 4189 bed = get_elf_backend_data (abfd); 4190 return bed->get_reloc_section (abfd, name); 4191 } 4192 4193 /* Assign all ELF section numbers. The dummy first section is handled here 4194 too. The link/info pointers for the standard section types are filled 4195 in here too, while we're at it. LINK_INFO will be 0 when arriving 4196 here for gas, objcopy, and when using the generic ELF linker. */ 4197 4198 static bool 4199 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) 4200 { 4201 struct elf_obj_tdata *t = elf_tdata (abfd); 4202 asection *sec; 4203 unsigned int section_number; 4204 Elf_Internal_Shdr **i_shdrp; 4205 struct bfd_elf_section_data *d; 4206 bool need_symtab; 4207 size_t amt; 4208 4209 section_number = 1; 4210 4211 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); 4212 4213 /* SHT_GROUP sections are in relocatable files only. */ 4214 if (link_info == NULL || !link_info->resolve_section_groups) 4215 { 4216 size_t reloc_count = 0; 4217 4218 /* Put SHT_GROUP sections first. */ 4219 for (sec = abfd->sections; sec != NULL; sec = sec->next) 4220 { 4221 d = elf_section_data (sec); 4222 4223 if (d->this_hdr.sh_type == SHT_GROUP) 4224 { 4225 if (sec->flags & SEC_LINKER_CREATED) 4226 { 4227 /* Remove the linker created SHT_GROUP sections. */ 4228 bfd_section_list_remove (abfd, sec); 4229 abfd->section_count--; 4230 } 4231 else 4232 d->this_idx = section_number++; 4233 } 4234 4235 /* Count relocations. */ 4236 reloc_count += sec->reloc_count; 4237 } 4238 4239 /* Set/clear HAS_RELOC depending on whether there are relocations. */ 4240 if (reloc_count == 0) 4241 abfd->flags &= ~HAS_RELOC; 4242 else 4243 abfd->flags |= HAS_RELOC; 4244 } 4245 4246 for (sec = abfd->sections; sec; sec = sec->next) 4247 { 4248 d = elf_section_data (sec); 4249 4250 if (d->this_hdr.sh_type != SHT_GROUP) 4251 d->this_idx = section_number++; 4252 if (d->this_hdr.sh_name != (unsigned int) -1) 4253 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); 4254 if (d->rel.hdr) 4255 { 4256 d->rel.idx = section_number++; 4257 if (d->rel.hdr->sh_name != (unsigned int) -1) 4258 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); 4259 } 4260 else 4261 d->rel.idx = 0; 4262 4263 if (d->rela.hdr) 4264 { 4265 d->rela.idx = section_number++; 4266 if (d->rela.hdr->sh_name != (unsigned int) -1) 4267 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); 4268 } 4269 else 4270 d->rela.idx = 0; 4271 } 4272 4273 need_symtab = (bfd_get_symcount (abfd) > 0 4274 || (link_info == NULL 4275 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) 4276 == HAS_RELOC))); 4277 if (need_symtab) 4278 { 4279 elf_onesymtab (abfd) = section_number++; 4280 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); 4281 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) 4282 { 4283 elf_section_list *entry; 4284 4285 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); 4286 4287 entry = bfd_zalloc (abfd, sizeof (*entry)); 4288 entry->ndx = section_number++; 4289 elf_symtab_shndx_list (abfd) = entry; 4290 entry->hdr.sh_name 4291 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 4292 ".symtab_shndx", false); 4293 if (entry->hdr.sh_name == (unsigned int) -1) 4294 return false; 4295 } 4296 elf_strtab_sec (abfd) = section_number++; 4297 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); 4298 } 4299 4300 elf_shstrtab_sec (abfd) = section_number++; 4301 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); 4302 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); 4303 4304 if (section_number >= SHN_LORESERVE) 4305 { 4306 /* xgettext:c-format */ 4307 _bfd_error_handler (_("%pB: too many sections: %u"), 4308 abfd, section_number); 4309 return false; 4310 } 4311 4312 elf_numsections (abfd) = section_number; 4313 elf_elfheader (abfd)->e_shnum = section_number; 4314 4315 /* Set up the list of section header pointers, in agreement with the 4316 indices. */ 4317 amt = section_number * sizeof (Elf_Internal_Shdr *); 4318 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt); 4319 if (i_shdrp == NULL) 4320 return false; 4321 4322 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, 4323 sizeof (Elf_Internal_Shdr)); 4324 if (i_shdrp[0] == NULL) 4325 { 4326 bfd_release (abfd, i_shdrp); 4327 return false; 4328 } 4329 4330 elf_elfsections (abfd) = i_shdrp; 4331 4332 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; 4333 if (need_symtab) 4334 { 4335 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; 4336 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) 4337 { 4338 elf_section_list * entry = elf_symtab_shndx_list (abfd); 4339 BFD_ASSERT (entry != NULL); 4340 i_shdrp[entry->ndx] = & entry->hdr; 4341 entry->hdr.sh_link = elf_onesymtab (abfd); 4342 } 4343 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; 4344 t->symtab_hdr.sh_link = elf_strtab_sec (abfd); 4345 } 4346 4347 for (sec = abfd->sections; sec; sec = sec->next) 4348 { 4349 asection *s; 4350 4351 d = elf_section_data (sec); 4352 4353 i_shdrp[d->this_idx] = &d->this_hdr; 4354 if (d->rel.idx != 0) 4355 i_shdrp[d->rel.idx] = d->rel.hdr; 4356 if (d->rela.idx != 0) 4357 i_shdrp[d->rela.idx] = d->rela.hdr; 4358 4359 /* Fill in the sh_link and sh_info fields while we're at it. */ 4360 4361 /* sh_link of a reloc section is the section index of the symbol 4362 table. sh_info is the section index of the section to which 4363 the relocation entries apply. */ 4364 if (d->rel.idx != 0) 4365 { 4366 d->rel.hdr->sh_link = elf_onesymtab (abfd); 4367 d->rel.hdr->sh_info = d->this_idx; 4368 d->rel.hdr->sh_flags |= SHF_INFO_LINK; 4369 } 4370 if (d->rela.idx != 0) 4371 { 4372 d->rela.hdr->sh_link = elf_onesymtab (abfd); 4373 d->rela.hdr->sh_info = d->this_idx; 4374 d->rela.hdr->sh_flags |= SHF_INFO_LINK; 4375 } 4376 4377 /* We need to set up sh_link for SHF_LINK_ORDER. */ 4378 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) 4379 { 4380 s = elf_linked_to_section (sec); 4381 /* We can now have a NULL linked section pointer. 4382 This happens when the sh_link field is 0, which is done 4383 when a linked to section is discarded but the linking 4384 section has been retained for some reason. */ 4385 if (s) 4386 { 4387 /* Check discarded linkonce section. */ 4388 if (discarded_section (s)) 4389 { 4390 asection *kept; 4391 _bfd_error_handler 4392 /* xgettext:c-format */ 4393 (_("%pB: sh_link of section `%pA' points to" 4394 " discarded section `%pA' of `%pB'"), 4395 abfd, d->this_hdr.bfd_section, s, s->owner); 4396 /* Point to the kept section if it has the same 4397 size as the discarded one. */ 4398 kept = _bfd_elf_check_kept_section (s, link_info); 4399 if (kept == NULL) 4400 { 4401 bfd_set_error (bfd_error_bad_value); 4402 return false; 4403 } 4404 s = kept; 4405 } 4406 /* Handle objcopy. */ 4407 else if (s->output_section == NULL) 4408 { 4409 _bfd_error_handler 4410 /* xgettext:c-format */ 4411 (_("%pB: sh_link of section `%pA' points to" 4412 " removed section `%pA' of `%pB'"), 4413 abfd, d->this_hdr.bfd_section, s, s->owner); 4414 bfd_set_error (bfd_error_bad_value); 4415 return false; 4416 } 4417 s = s->output_section; 4418 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4419 } 4420 } 4421 4422 switch (d->this_hdr.sh_type) 4423 { 4424 case SHT_REL: 4425 case SHT_RELA: 4426 /* sh_link is the section index of the symbol table. 4427 sh_info is the section index of the section to which the 4428 relocation entries apply. */ 4429 if (d->this_hdr.sh_link == 0) 4430 { 4431 /* FIXME maybe: If this is a reloc section which we are 4432 treating as a normal section then we likely should 4433 not be assuming its sh_link is .dynsym or .symtab. */ 4434 if ((sec->flags & SEC_ALLOC) != 0) 4435 { 4436 s = bfd_get_section_by_name (abfd, ".dynsym"); 4437 if (s != NULL) 4438 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4439 } 4440 else 4441 d->this_hdr.sh_link = elf_onesymtab (abfd); 4442 } 4443 4444 s = elf_get_reloc_section (sec); 4445 if (s != NULL) 4446 { 4447 d->this_hdr.sh_info = elf_section_data (s)->this_idx; 4448 d->this_hdr.sh_flags |= SHF_INFO_LINK; 4449 } 4450 break; 4451 4452 case SHT_STRTAB: 4453 /* We assume that a section named .stab*str is a stabs 4454 string section. We look for a section with the same name 4455 but without the trailing ``str'', and set its sh_link 4456 field to point to this section. */ 4457 if (startswith (sec->name, ".stab") 4458 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) 4459 { 4460 size_t len; 4461 char *alc; 4462 4463 len = strlen (sec->name); 4464 alc = (char *) bfd_malloc (len - 2); 4465 if (alc == NULL) 4466 return false; 4467 memcpy (alc, sec->name, len - 3); 4468 alc[len - 3] = '\0'; 4469 s = bfd_get_section_by_name (abfd, alc); 4470 free (alc); 4471 if (s != NULL) 4472 { 4473 elf_section_data (s)->this_hdr.sh_link = d->this_idx; 4474 4475 /* This is a .stab section. */ 4476 elf_section_data (s)->this_hdr.sh_entsize = 12; 4477 } 4478 } 4479 break; 4480 4481 case SHT_DYNAMIC: 4482 case SHT_DYNSYM: 4483 case SHT_GNU_verneed: 4484 case SHT_GNU_verdef: 4485 /* sh_link is the section header index of the string table 4486 used for the dynamic entries, or the symbol table, or the 4487 version strings. */ 4488 s = bfd_get_section_by_name (abfd, ".dynstr"); 4489 if (s != NULL) 4490 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4491 break; 4492 4493 case SHT_GNU_LIBLIST: 4494 /* sh_link is the section header index of the prelink library 4495 list used for the dynamic entries, or the symbol table, or 4496 the version strings. */ 4497 s = bfd_get_section_by_name (abfd, ((sec->flags & SEC_ALLOC) 4498 ? ".dynstr" : ".gnu.libstr")); 4499 if (s != NULL) 4500 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4501 break; 4502 4503 case SHT_HASH: 4504 case SHT_GNU_HASH: 4505 case SHT_GNU_versym: 4506 /* sh_link is the section header index of the symbol table 4507 this hash table or version table is for. */ 4508 s = bfd_get_section_by_name (abfd, ".dynsym"); 4509 if (s != NULL) 4510 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4511 break; 4512 4513 case SHT_GROUP: 4514 d->this_hdr.sh_link = elf_onesymtab (abfd); 4515 } 4516 } 4517 4518 /* Delay setting sh_name to _bfd_elf_write_object_contents so that 4519 _bfd_elf_assign_file_positions_for_non_load can convert DWARF 4520 debug section name from .debug_* to .zdebug_* if needed. */ 4521 4522 return true; 4523 } 4524 4525 static bool 4526 sym_is_global (bfd *abfd, asymbol *sym) 4527 { 4528 /* If the backend has a special mapping, use it. */ 4529 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4530 if (bed->elf_backend_sym_is_global) 4531 return (*bed->elf_backend_sym_is_global) (abfd, sym); 4532 4533 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 4534 || bfd_is_und_section (bfd_asymbol_section (sym)) 4535 || bfd_is_com_section (bfd_asymbol_section (sym))); 4536 } 4537 4538 /* Filter global symbols of ABFD to include in the import library. All 4539 SYMCOUNT symbols of ABFD can be examined from their pointers in 4540 SYMS. Pointers of symbols to keep should be stored contiguously at 4541 the beginning of that array. 4542 4543 Returns the number of symbols to keep. */ 4544 4545 unsigned int 4546 _bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, 4547 asymbol **syms, long symcount) 4548 { 4549 long src_count, dst_count = 0; 4550 4551 for (src_count = 0; src_count < symcount; src_count++) 4552 { 4553 asymbol *sym = syms[src_count]; 4554 char *name = (char *) bfd_asymbol_name (sym); 4555 struct bfd_link_hash_entry *h; 4556 4557 if (!sym_is_global (abfd, sym)) 4558 continue; 4559 4560 h = bfd_link_hash_lookup (info->hash, name, false, false, false); 4561 if (h == NULL) 4562 continue; 4563 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) 4564 continue; 4565 if (h->linker_def || h->ldscript_def) 4566 continue; 4567 4568 syms[dst_count++] = sym; 4569 } 4570 4571 syms[dst_count] = NULL; 4572 4573 return dst_count; 4574 } 4575 4576 /* Don't output symbols for sections that are not going to be output, 4577 that are duplicates or there is no BFD section. */ 4578 4579 static bool 4580 ignore_sym (asymbol *sym) 4581 { 4582 if (sym == NULL) 4583 return false; 4584 4585 if (sym->section == NULL) 4586 return true; 4587 4588 if ((sym->flags & BSF_SECTION_SYM) != 0) 4589 { 4590 if ((sym->flags & BSF_SECTION_SYM_USED) == 0) 4591 return true; 4592 /* With ld -r on generic elf targets it is possible to have 4593 multiple section symbols in the output for a given section. 4594 We'd like to get rid of all but the first one. This drops 4595 them if the first input section is non-zero size, but fails 4596 to do so if the first input section is zero sized. */ 4597 if (sym->section->output_offset != 0) 4598 return true; 4599 } 4600 4601 return discarded_section (sym->section); 4602 } 4603 4604 /* Map symbol from it's internal number to the external number, moving 4605 all local symbols to be at the head of the list. */ 4606 4607 static bool 4608 elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) 4609 { 4610 unsigned int symcount = bfd_get_symcount (abfd); 4611 asymbol **syms = bfd_get_outsymbols (abfd); 4612 asymbol **sect_syms; 4613 unsigned int num_locals = 0; 4614 unsigned int num_globals = 0; 4615 unsigned int max_index = 0; 4616 unsigned int idx; 4617 asection *asect; 4618 asymbol **new_syms; 4619 size_t amt; 4620 4621 #ifdef DEBUG 4622 fprintf (stderr, "elf_map_symbols\n"); 4623 fflush (stderr); 4624 #endif 4625 4626 for (asect = abfd->sections; asect; asect = asect->next) 4627 { 4628 if (max_index < asect->index) 4629 max_index = asect->index; 4630 } 4631 4632 max_index++; 4633 amt = max_index * sizeof (asymbol *); 4634 sect_syms = (asymbol **) bfd_zalloc (abfd, amt); 4635 if (sect_syms == NULL) 4636 return false; 4637 elf_section_syms (abfd) = sect_syms; 4638 elf_num_section_syms (abfd) = max_index; 4639 4640 /* Init sect_syms entries for any section symbols we have already 4641 decided to output. */ 4642 for (idx = 0; idx < symcount; idx++) 4643 { 4644 asymbol *sym = syms[idx]; 4645 4646 if ((sym->flags & BSF_SECTION_SYM) != 0 4647 && sym->value == 0 4648 && !ignore_sym (sym) 4649 && !bfd_is_abs_section (sym->section)) 4650 { 4651 asection *sec = sym->section; 4652 4653 if (sec->owner != abfd) 4654 sec = sec->output_section; 4655 4656 sect_syms[sec->index] = syms[idx]; 4657 } 4658 } 4659 4660 /* Classify all of the symbols. */ 4661 for (idx = 0; idx < symcount; idx++) 4662 { 4663 if (ignore_sym (syms[idx])) 4664 continue; 4665 if (sym_is_global (abfd, syms[idx])) 4666 num_globals++; 4667 else 4668 num_locals++; 4669 } 4670 4671 /* We will be adding a section symbol for each normal BFD section. Most 4672 sections will already have a section symbol in outsymbols, but 4673 eg. SHT_GROUP sections will not, and we need the section symbol mapped 4674 at least in that case. */ 4675 for (asect = abfd->sections; asect; asect = asect->next) 4676 { 4677 asymbol *sym = asect->symbol; 4678 /* Don't include ignored section symbols. */ 4679 if (!ignore_sym (sym) 4680 && sect_syms[asect->index] == NULL) 4681 { 4682 if (sym_is_global (abfd, asect->symbol)) 4683 num_globals++; 4684 else 4685 num_locals++; 4686 } 4687 } 4688 4689 /* Now sort the symbols so the local symbols are first. */ 4690 amt = (num_locals + num_globals) * sizeof (asymbol *); 4691 new_syms = (asymbol **) bfd_alloc (abfd, amt); 4692 if (new_syms == NULL) 4693 return false; 4694 4695 unsigned int num_globals2 = 0; 4696 unsigned int num_locals2 = 0; 4697 for (idx = 0; idx < symcount; idx++) 4698 { 4699 asymbol *sym = syms[idx]; 4700 unsigned int i; 4701 4702 if (ignore_sym (sym)) 4703 continue; 4704 4705 if (sym_is_global (abfd, sym)) 4706 i = num_locals + num_globals2++; 4707 else 4708 i = num_locals2++; 4709 new_syms[i] = sym; 4710 sym->udata.i = i + 1; 4711 } 4712 for (asect = abfd->sections; asect; asect = asect->next) 4713 { 4714 asymbol *sym = asect->symbol; 4715 if (!ignore_sym (sym) 4716 && sect_syms[asect->index] == NULL) 4717 { 4718 unsigned int i; 4719 4720 sect_syms[asect->index] = sym; 4721 if (sym_is_global (abfd, sym)) 4722 i = num_locals + num_globals2++; 4723 else 4724 i = num_locals2++; 4725 new_syms[i] = sym; 4726 sym->udata.i = i + 1; 4727 } 4728 } 4729 4730 bfd_set_symtab (abfd, new_syms, num_locals + num_globals); 4731 4732 *pnum_locals = num_locals; 4733 return true; 4734 } 4735 4736 /* Align to the maximum file alignment that could be required for any 4737 ELF data structure. */ 4738 4739 static inline file_ptr 4740 align_file_position (file_ptr off, int align) 4741 { 4742 return (off + align - 1) & ~(align - 1); 4743 } 4744 4745 /* Assign a file position to a section, optionally aligning to the 4746 required section alignment. */ 4747 4748 file_ptr 4749 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, 4750 file_ptr offset, 4751 bool align) 4752 { 4753 if (align && i_shdrp->sh_addralign > 1) 4754 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign & -i_shdrp->sh_addralign); 4755 i_shdrp->sh_offset = offset; 4756 if (i_shdrp->bfd_section != NULL) 4757 i_shdrp->bfd_section->filepos = offset; 4758 if (i_shdrp->sh_type != SHT_NOBITS) 4759 offset += i_shdrp->sh_size; 4760 return offset; 4761 } 4762 4763 /* Compute the file positions we are going to put the sections at, and 4764 otherwise prepare to begin writing out the ELF file. If LINK_INFO 4765 is not NULL, this is being called by the ELF backend linker. */ 4766 4767 bool 4768 _bfd_elf_compute_section_file_positions (bfd *abfd, 4769 struct bfd_link_info *link_info) 4770 { 4771 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4772 struct fake_section_arg fsargs; 4773 bool failed; 4774 struct elf_strtab_hash *strtab = NULL; 4775 Elf_Internal_Shdr *shstrtab_hdr; 4776 bool need_symtab; 4777 4778 if (abfd->output_has_begun) 4779 return true; 4780 4781 /* Do any elf backend specific processing first. */ 4782 if (bed->elf_backend_begin_write_processing) 4783 (*bed->elf_backend_begin_write_processing) (abfd, link_info); 4784 4785 if (!(*bed->elf_backend_init_file_header) (abfd, link_info)) 4786 return false; 4787 4788 fsargs.failed = false; 4789 fsargs.link_info = link_info; 4790 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); 4791 if (fsargs.failed) 4792 return false; 4793 4794 if (!assign_section_numbers (abfd, link_info)) 4795 return false; 4796 4797 /* The backend linker builds symbol table information itself. */ 4798 need_symtab = (link_info == NULL 4799 && (bfd_get_symcount (abfd) > 0 4800 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) 4801 == HAS_RELOC))); 4802 if (need_symtab) 4803 { 4804 /* Non-zero if doing a relocatable link. */ 4805 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); 4806 4807 if (! swap_out_syms (abfd, &strtab, relocatable_p, link_info)) 4808 return false; 4809 } 4810 4811 failed = false; 4812 if (link_info == NULL) 4813 { 4814 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); 4815 if (failed) 4816 goto err_free_strtab; 4817 } 4818 4819 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; 4820 /* sh_name was set in init_file_header. */ 4821 shstrtab_hdr->sh_type = SHT_STRTAB; 4822 shstrtab_hdr->sh_flags = bed->elf_strtab_flags; 4823 shstrtab_hdr->sh_addr = 0; 4824 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ 4825 shstrtab_hdr->sh_entsize = 0; 4826 shstrtab_hdr->sh_link = 0; 4827 shstrtab_hdr->sh_info = 0; 4828 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ 4829 shstrtab_hdr->sh_addralign = 1; 4830 4831 if (!assign_file_positions_except_relocs (abfd, link_info)) 4832 goto err_free_strtab; 4833 4834 if (strtab != NULL) 4835 { 4836 file_ptr off; 4837 Elf_Internal_Shdr *hdr; 4838 4839 off = elf_next_file_pos (abfd); 4840 4841 hdr = & elf_symtab_hdr (abfd); 4842 off = _bfd_elf_assign_file_position_for_section (hdr, off, true); 4843 4844 if (elf_symtab_shndx_list (abfd) != NULL) 4845 { 4846 hdr = & elf_symtab_shndx_list (abfd)->hdr; 4847 if (hdr->sh_size != 0) 4848 off = _bfd_elf_assign_file_position_for_section (hdr, off, true); 4849 /* FIXME: What about other symtab_shndx sections in the list ? */ 4850 } 4851 4852 hdr = &elf_tdata (abfd)->strtab_hdr; 4853 off = _bfd_elf_assign_file_position_for_section (hdr, off, true); 4854 4855 elf_next_file_pos (abfd) = off; 4856 4857 /* Now that we know where the .strtab section goes, write it 4858 out. */ 4859 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 4860 || ! _bfd_elf_strtab_emit (abfd, strtab)) 4861 goto err_free_strtab; 4862 _bfd_elf_strtab_free (strtab); 4863 } 4864 4865 abfd->output_has_begun = true; 4866 return true; 4867 4868 err_free_strtab: 4869 if (strtab != NULL) 4870 _bfd_elf_strtab_free (strtab); 4871 return false; 4872 } 4873 4874 /* Retrieve .eh_frame_hdr. Prior to size_dynamic_sections the 4875 function effectively returns whether --eh-frame-hdr is given on the 4876 command line. After size_dynamic_sections the result reflects 4877 whether .eh_frame_hdr will actually be output (sizing isn't done 4878 until ldemul_after_allocation). */ 4879 4880 static asection * 4881 elf_eh_frame_hdr (const struct bfd_link_info *info) 4882 { 4883 if (info != NULL && is_elf_hash_table (info->hash)) 4884 return elf_hash_table (info)->eh_info.hdr_sec; 4885 return NULL; 4886 } 4887 4888 /* Make an initial estimate of the size of the program header. If we 4889 get the number wrong here, we'll redo section placement. */ 4890 4891 static bfd_size_type 4892 get_program_header_size (bfd *abfd, struct bfd_link_info *info) 4893 { 4894 size_t segs; 4895 asection *s; 4896 const struct elf_backend_data *bed; 4897 4898 /* Assume we will need exactly two PT_LOAD segments: one for text 4899 and one for data. */ 4900 segs = 2; 4901 4902 s = bfd_get_section_by_name (abfd, ".interp"); 4903 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) 4904 { 4905 /* If we have a loadable interpreter section, we need a 4906 PT_INTERP segment. In this case, assume we also need a 4907 PT_PHDR segment, although that may not be true for all 4908 targets. */ 4909 segs += 2; 4910 } 4911 4912 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) 4913 { 4914 /* We need a PT_DYNAMIC segment. */ 4915 ++segs; 4916 } 4917 4918 if (info != NULL && info->relro) 4919 { 4920 /* We need a PT_GNU_RELRO segment. */ 4921 ++segs; 4922 } 4923 4924 if (elf_eh_frame_hdr (info)) 4925 { 4926 /* We need a PT_GNU_EH_FRAME segment. */ 4927 ++segs; 4928 } 4929 4930 if (elf_stack_flags (abfd)) 4931 { 4932 /* We need a PT_GNU_STACK segment. */ 4933 ++segs; 4934 } 4935 4936 if (elf_sframe (abfd)) 4937 { 4938 /* We need a PT_GNU_SFRAME segment. */ 4939 ++segs; 4940 } 4941 4942 s = bfd_get_section_by_name (abfd, 4943 NOTE_GNU_PROPERTY_SECTION_NAME); 4944 if (s != NULL && s->size != 0) 4945 { 4946 /* We need a PT_GNU_PROPERTY segment. */ 4947 ++segs; 4948 } 4949 4950 for (s = abfd->sections; s != NULL; s = s->next) 4951 { 4952 if ((s->flags & SEC_LOAD) != 0 4953 && elf_section_type (s) == SHT_NOTE) 4954 { 4955 unsigned int alignment_power; 4956 /* We need a PT_NOTE segment. */ 4957 ++segs; 4958 /* Try to create just one PT_NOTE segment for all adjacent 4959 loadable SHT_NOTE sections. gABI requires that within a 4960 PT_NOTE segment (and also inside of each SHT_NOTE section) 4961 each note should have the same alignment. So we check 4962 whether the sections are correctly aligned. */ 4963 alignment_power = s->alignment_power; 4964 while (s->next != NULL 4965 && s->next->alignment_power == alignment_power 4966 && (s->next->flags & SEC_LOAD) != 0 4967 && elf_section_type (s->next) == SHT_NOTE) 4968 s = s->next; 4969 } 4970 } 4971 4972 for (s = abfd->sections; s != NULL; s = s->next) 4973 { 4974 if (s->flags & SEC_THREAD_LOCAL) 4975 { 4976 /* We need a PT_TLS segment. */ 4977 ++segs; 4978 break; 4979 } 4980 } 4981 4982 bed = get_elf_backend_data (abfd); 4983 4984 if ((abfd->flags & D_PAGED) != 0 4985 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) 4986 { 4987 /* Add a PT_GNU_MBIND segment for each mbind section. */ 4988 bfd_vma commonpagesize; 4989 unsigned int page_align_power; 4990 4991 if (info != NULL) 4992 commonpagesize = info->commonpagesize; 4993 else 4994 commonpagesize = bed->commonpagesize; 4995 page_align_power = bfd_log2 (commonpagesize); 4996 for (s = abfd->sections; s != NULL; s = s->next) 4997 if (elf_section_flags (s) & SHF_GNU_MBIND) 4998 { 4999 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM) 5000 { 5001 _bfd_error_handler 5002 /* xgettext:c-format */ 5003 (_("%pB: GNU_MBIND section `%pA' has invalid " 5004 "sh_info field: %d"), 5005 abfd, s, elf_section_data (s)->this_hdr.sh_info); 5006 continue; 5007 } 5008 /* Align mbind section to page size. */ 5009 if (s->alignment_power < page_align_power) 5010 s->alignment_power = page_align_power; 5011 segs ++; 5012 } 5013 } 5014 5015 /* Let the backend count up any program headers it might need. */ 5016 if (bed->elf_backend_additional_program_headers) 5017 { 5018 int a; 5019 5020 a = (*bed->elf_backend_additional_program_headers) (abfd, info); 5021 if (a == -1) 5022 abort (); 5023 segs += a; 5024 } 5025 5026 return segs * bed->s->sizeof_phdr; 5027 } 5028 5029 /* Find the segment that contains the output_section of section. */ 5030 5031 Elf_Internal_Phdr * 5032 _bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) 5033 { 5034 struct elf_segment_map *m; 5035 Elf_Internal_Phdr *p; 5036 5037 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; 5038 m != NULL; 5039 m = m->next, p++) 5040 { 5041 int i; 5042 5043 for (i = m->count - 1; i >= 0; i--) 5044 if (m->sections[i] == section) 5045 return p; 5046 } 5047 5048 return NULL; 5049 } 5050 5051 /* Create a mapping from a set of sections to a program segment. */ 5052 5053 static struct elf_segment_map * 5054 make_mapping (bfd *abfd, 5055 asection **sections, 5056 unsigned int from, 5057 unsigned int to, 5058 bool phdr) 5059 { 5060 struct elf_segment_map *m; 5061 unsigned int i; 5062 asection **hdrpp; 5063 size_t amt; 5064 5065 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 5066 amt += (to - from) * sizeof (asection *); 5067 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5068 if (m == NULL) 5069 return NULL; 5070 m->next = NULL; 5071 m->p_type = PT_LOAD; 5072 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) 5073 m->sections[i - from] = *hdrpp; 5074 m->count = to - from; 5075 5076 if (from == 0 && phdr) 5077 { 5078 /* Include the headers in the first PT_LOAD segment. */ 5079 m->includes_filehdr = 1; 5080 m->includes_phdrs = 1; 5081 } 5082 5083 return m; 5084 } 5085 5086 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL 5087 on failure. */ 5088 5089 struct elf_segment_map * 5090 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) 5091 { 5092 struct elf_segment_map *m; 5093 5094 m = (struct elf_segment_map *) bfd_zalloc (abfd, 5095 sizeof (struct elf_segment_map)); 5096 if (m == NULL) 5097 return NULL; 5098 m->next = NULL; 5099 m->p_type = PT_DYNAMIC; 5100 m->count = 1; 5101 m->sections[0] = dynsec; 5102 5103 return m; 5104 } 5105 5106 /* Possibly add or remove segments from the segment map. */ 5107 5108 static bool 5109 elf_modify_segment_map (bfd *abfd, 5110 struct bfd_link_info *info, 5111 bool remove_empty_load) 5112 { 5113 struct elf_segment_map **m; 5114 const struct elf_backend_data *bed; 5115 5116 /* The placement algorithm assumes that non allocated sections are 5117 not in PT_LOAD segments. We ensure this here by removing such 5118 sections from the segment map. We also remove excluded 5119 sections. Finally, any PT_LOAD segment without sections is 5120 removed. */ 5121 m = &elf_seg_map (abfd); 5122 while (*m) 5123 { 5124 unsigned int i, new_count; 5125 5126 for (new_count = 0, i = 0; i < (*m)->count; i++) 5127 { 5128 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 5129 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 5130 || (*m)->p_type != PT_LOAD)) 5131 { 5132 (*m)->sections[new_count] = (*m)->sections[i]; 5133 new_count++; 5134 } 5135 } 5136 (*m)->count = new_count; 5137 5138 if (remove_empty_load 5139 && (*m)->p_type == PT_LOAD 5140 && (*m)->count == 0 5141 && !(*m)->includes_phdrs) 5142 *m = (*m)->next; 5143 else 5144 m = &(*m)->next; 5145 } 5146 5147 bed = get_elf_backend_data (abfd); 5148 if (bed->elf_backend_modify_segment_map != NULL) 5149 { 5150 if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) 5151 return false; 5152 } 5153 5154 return true; 5155 } 5156 5157 #define IS_TBSS(s) \ 5158 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL) 5159 5160 /* Set up a mapping from BFD sections to program segments. Update 5161 NEED_LAYOUT if the section layout is changed. */ 5162 5163 bool 5164 _bfd_elf_map_sections_to_segments (bfd *abfd, 5165 struct bfd_link_info *info, 5166 bool *need_layout) 5167 { 5168 unsigned int count; 5169 struct elf_segment_map *m; 5170 asection **sections = NULL; 5171 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 5172 bool no_user_phdrs; 5173 5174 no_user_phdrs = elf_seg_map (abfd) == NULL; 5175 5176 if (info != NULL) 5177 { 5178 info->user_phdrs = !no_user_phdrs; 5179 5180 /* Size the relative relocations if DT_RELR is enabled. */ 5181 if (info->enable_dt_relr 5182 && need_layout != NULL 5183 && bed->size_relative_relocs 5184 && !bed->size_relative_relocs (info, need_layout)) 5185 info->callbacks->einfo 5186 (_("%F%P: failed to size relative relocations\n")); 5187 } 5188 5189 if (no_user_phdrs && bfd_count_sections (abfd) != 0) 5190 { 5191 asection *s; 5192 unsigned int i; 5193 struct elf_segment_map *mfirst; 5194 struct elf_segment_map **pm; 5195 asection *last_hdr; 5196 bfd_vma last_size; 5197 unsigned int hdr_index; 5198 bfd_vma maxpagesize; 5199 asection **hdrpp; 5200 bool phdr_in_segment; 5201 bool writable; 5202 bool executable; 5203 unsigned int tls_count = 0; 5204 asection *first_tls = NULL; 5205 asection *first_mbind = NULL; 5206 asection *dynsec, *eh_frame_hdr; 5207 asection *sframe; 5208 size_t amt; 5209 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */ 5210 bfd_size_type phdr_size; /* Octets/bytes. */ 5211 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 5212 5213 /* Select the allocated sections, and sort them. */ 5214 5215 amt = bfd_count_sections (abfd) * sizeof (asection *); 5216 sections = (asection **) bfd_malloc (amt); 5217 if (sections == NULL) 5218 goto error_return; 5219 5220 /* Calculate top address, avoiding undefined behaviour of shift 5221 left operator when shift count is equal to size of type 5222 being shifted. */ 5223 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; 5224 addr_mask = (addr_mask << 1) + 1; 5225 5226 i = 0; 5227 for (s = abfd->sections; s != NULL; s = s->next) 5228 { 5229 if ((s->flags & SEC_ALLOC) != 0) 5230 { 5231 /* target_index is unused until bfd_elf_final_link 5232 starts output of section symbols. Use it to make 5233 qsort stable. */ 5234 s->target_index = i; 5235 sections[i] = s; 5236 ++i; 5237 /* A wrapping section potentially clashes with header. */ 5238 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask)) 5239 wrap_to = (s->lma + s->size / opb) & addr_mask; 5240 } 5241 } 5242 BFD_ASSERT (i <= bfd_count_sections (abfd)); 5243 count = i; 5244 5245 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); 5246 5247 phdr_size = elf_program_header_size (abfd); 5248 if (phdr_size == (bfd_size_type) -1) 5249 phdr_size = get_program_header_size (abfd, info); 5250 phdr_size += bed->s->sizeof_ehdr; 5251 /* phdr_size is compared to LMA values which are in bytes. */ 5252 phdr_size /= opb; 5253 if (info != NULL) 5254 maxpagesize = info->maxpagesize; 5255 else 5256 maxpagesize = bed->maxpagesize; 5257 if (maxpagesize == 0) 5258 maxpagesize = 1; 5259 phdr_in_segment = info != NULL && info->load_phdrs; 5260 if (count != 0 5261 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1)) 5262 >= (phdr_size & (maxpagesize - 1)))) 5263 /* For compatibility with old scripts that may not be using 5264 SIZEOF_HEADERS, add headers when it looks like space has 5265 been left for them. */ 5266 phdr_in_segment = true; 5267 5268 /* Build the mapping. */ 5269 mfirst = NULL; 5270 pm = &mfirst; 5271 5272 /* If we have a .interp section, then create a PT_PHDR segment for 5273 the program headers and a PT_INTERP segment for the .interp 5274 section. */ 5275 s = bfd_get_section_by_name (abfd, ".interp"); 5276 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) 5277 { 5278 amt = sizeof (struct elf_segment_map); 5279 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5280 if (m == NULL) 5281 goto error_return; 5282 m->next = NULL; 5283 m->p_type = PT_PHDR; 5284 m->p_flags = PF_R; 5285 m->p_flags_valid = 1; 5286 m->includes_phdrs = 1; 5287 phdr_in_segment = true; 5288 *pm = m; 5289 pm = &m->next; 5290 5291 amt = sizeof (struct elf_segment_map); 5292 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5293 if (m == NULL) 5294 goto error_return; 5295 m->next = NULL; 5296 m->p_type = PT_INTERP; 5297 m->count = 1; 5298 m->sections[0] = s; 5299 5300 *pm = m; 5301 pm = &m->next; 5302 } 5303 5304 /* Look through the sections. We put sections in the same program 5305 segment when the start of the second section can be placed within 5306 a few bytes of the end of the first section. */ 5307 last_hdr = NULL; 5308 last_size = 0; 5309 hdr_index = 0; 5310 writable = false; 5311 executable = false; 5312 dynsec = bfd_get_section_by_name (abfd, ".dynamic"); 5313 if (dynsec != NULL 5314 && (dynsec->flags & SEC_LOAD) == 0) 5315 dynsec = NULL; 5316 5317 if ((abfd->flags & D_PAGED) == 0) 5318 phdr_in_segment = false; 5319 5320 /* Deal with -Ttext or something similar such that the first section 5321 is not adjacent to the program headers. This is an 5322 approximation, since at this point we don't know exactly how many 5323 program headers we will need. */ 5324 if (phdr_in_segment && count > 0) 5325 { 5326 bfd_vma phdr_lma; /* Bytes. */ 5327 bool separate_phdr = false; 5328 5329 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize; 5330 if (info != NULL 5331 && info->separate_code 5332 && (sections[0]->flags & SEC_CODE) != 0) 5333 { 5334 /* If data sections should be separate from code and 5335 thus not executable, and the first section is 5336 executable then put the file and program headers in 5337 their own PT_LOAD. */ 5338 separate_phdr = true; 5339 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize) 5340 == (sections[0]->lma & addr_mask & -maxpagesize))) 5341 { 5342 /* The file and program headers are currently on the 5343 same page as the first section. Put them on the 5344 previous page if we can. */ 5345 if (phdr_lma >= maxpagesize) 5346 phdr_lma -= maxpagesize; 5347 else 5348 separate_phdr = false; 5349 } 5350 } 5351 if ((sections[0]->lma & addr_mask) < phdr_lma 5352 || (sections[0]->lma & addr_mask) < phdr_size) 5353 /* If file and program headers would be placed at the end 5354 of memory then it's probably better to omit them. */ 5355 phdr_in_segment = false; 5356 else if (phdr_lma < wrap_to) 5357 /* If a section wraps around to where we'll be placing 5358 file and program headers, then the headers will be 5359 overwritten. */ 5360 phdr_in_segment = false; 5361 else if (separate_phdr) 5362 { 5363 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment); 5364 if (m == NULL) 5365 goto error_return; 5366 m->p_paddr = phdr_lma * opb; 5367 m->p_vaddr_offset 5368 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize; 5369 m->p_paddr_valid = 1; 5370 *pm = m; 5371 pm = &m->next; 5372 phdr_in_segment = false; 5373 } 5374 } 5375 5376 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) 5377 { 5378 asection *hdr; 5379 bool new_segment; 5380 5381 hdr = *hdrpp; 5382 5383 /* See if this section and the last one will fit in the same 5384 segment. */ 5385 5386 if (last_hdr == NULL) 5387 { 5388 /* If we don't have a segment yet, then we don't need a new 5389 one (we build the last one after this loop). */ 5390 new_segment = false; 5391 } 5392 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) 5393 { 5394 /* If this section has a different relation between the 5395 virtual address and the load address, then we need a new 5396 segment. */ 5397 new_segment = true; 5398 } 5399 else if (hdr->lma < last_hdr->lma + last_size 5400 || last_hdr->lma + last_size < last_hdr->lma) 5401 { 5402 /* If this section has a load address that makes it overlap 5403 the previous section, then we need a new segment. */ 5404 new_segment = true; 5405 } 5406 else if ((abfd->flags & D_PAGED) != 0 5407 && (((last_hdr->lma + last_size - 1) & -maxpagesize) 5408 == (hdr->lma & -maxpagesize))) 5409 { 5410 /* If we are demand paged then we can't map two disk 5411 pages onto the same memory page. */ 5412 new_segment = false; 5413 } 5414 /* In the next test we have to be careful when last_hdr->lma is close 5415 to the end of the address space. If the aligned address wraps 5416 around to the start of the address space, then there are no more 5417 pages left in memory and it is OK to assume that the current 5418 section can be included in the current segment. */ 5419 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) 5420 + maxpagesize > last_hdr->lma) 5421 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) 5422 + maxpagesize <= hdr->lma)) 5423 { 5424 /* If putting this section in this segment would force us to 5425 skip a page in the segment, then we need a new segment. */ 5426 new_segment = true; 5427 } 5428 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 5429 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) 5430 { 5431 /* We don't want to put a loaded section after a 5432 nonloaded (ie. bss style) section in the same segment 5433 as that will force the non-loaded section to be loaded. 5434 Consider .tbss sections as loaded for this purpose. */ 5435 new_segment = true; 5436 } 5437 else if ((abfd->flags & D_PAGED) == 0) 5438 { 5439 /* If the file is not demand paged, which means that we 5440 don't require the sections to be correctly aligned in the 5441 file, then there is no other reason for a new segment. */ 5442 new_segment = false; 5443 } 5444 else if (info != NULL 5445 && info->separate_code 5446 && executable != ((hdr->flags & SEC_CODE) != 0)) 5447 { 5448 new_segment = true; 5449 } 5450 else if (! writable 5451 && (hdr->flags & SEC_READONLY) == 0) 5452 { 5453 /* We don't want to put a writable section in a read only 5454 segment. */ 5455 new_segment = true; 5456 } 5457 else 5458 { 5459 /* Otherwise, we can use the same segment. */ 5460 new_segment = false; 5461 } 5462 5463 /* Allow interested parties a chance to override our decision. */ 5464 if (last_hdr != NULL 5465 && info != NULL 5466 && info->callbacks->override_segment_assignment != NULL) 5467 new_segment 5468 = info->callbacks->override_segment_assignment (info, abfd, hdr, 5469 last_hdr, 5470 new_segment); 5471 5472 if (! new_segment) 5473 { 5474 if ((hdr->flags & SEC_READONLY) == 0) 5475 writable = true; 5476 if ((hdr->flags & SEC_CODE) != 0) 5477 executable = true; 5478 last_hdr = hdr; 5479 /* .tbss sections effectively have zero size. */ 5480 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; 5481 continue; 5482 } 5483 5484 /* We need a new program segment. We must create a new program 5485 header holding all the sections from hdr_index until hdr. */ 5486 5487 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); 5488 if (m == NULL) 5489 goto error_return; 5490 5491 *pm = m; 5492 pm = &m->next; 5493 5494 if ((hdr->flags & SEC_READONLY) == 0) 5495 writable = true; 5496 else 5497 writable = false; 5498 5499 if ((hdr->flags & SEC_CODE) == 0) 5500 executable = false; 5501 else 5502 executable = true; 5503 5504 last_hdr = hdr; 5505 /* .tbss sections effectively have zero size. */ 5506 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; 5507 hdr_index = i; 5508 phdr_in_segment = false; 5509 } 5510 5511 /* Create a final PT_LOAD program segment, but not if it's just 5512 for .tbss. */ 5513 if (last_hdr != NULL 5514 && (i - hdr_index != 1 5515 || !IS_TBSS (last_hdr))) 5516 { 5517 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); 5518 if (m == NULL) 5519 goto error_return; 5520 5521 *pm = m; 5522 pm = &m->next; 5523 } 5524 5525 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ 5526 if (dynsec != NULL) 5527 { 5528 m = _bfd_elf_make_dynamic_segment (abfd, dynsec); 5529 if (m == NULL) 5530 goto error_return; 5531 *pm = m; 5532 pm = &m->next; 5533 } 5534 5535 /* For each batch of consecutive loadable SHT_NOTE sections, 5536 add a PT_NOTE segment. We don't use bfd_get_section_by_name, 5537 because if we link together nonloadable .note sections and 5538 loadable .note sections, we will generate two .note sections 5539 in the output file. */ 5540 for (s = abfd->sections; s != NULL; s = s->next) 5541 { 5542 if ((s->flags & SEC_LOAD) != 0 5543 && elf_section_type (s) == SHT_NOTE) 5544 { 5545 asection *s2; 5546 unsigned int alignment_power = s->alignment_power; 5547 5548 count = 1; 5549 for (s2 = s; s2->next != NULL; s2 = s2->next) 5550 { 5551 if (s2->next->alignment_power == alignment_power 5552 && (s2->next->flags & SEC_LOAD) != 0 5553 && elf_section_type (s2->next) == SHT_NOTE 5554 && align_power (s2->lma + s2->size / opb, 5555 alignment_power) 5556 == s2->next->lma) 5557 count++; 5558 else 5559 break; 5560 } 5561 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 5562 amt += count * sizeof (asection *); 5563 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5564 if (m == NULL) 5565 goto error_return; 5566 m->next = NULL; 5567 m->p_type = PT_NOTE; 5568 m->count = count; 5569 while (count > 1) 5570 { 5571 m->sections[m->count - count--] = s; 5572 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); 5573 s = s->next; 5574 } 5575 m->sections[m->count - 1] = s; 5576 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); 5577 *pm = m; 5578 pm = &m->next; 5579 } 5580 if (s->flags & SEC_THREAD_LOCAL) 5581 { 5582 if (! tls_count) 5583 first_tls = s; 5584 tls_count++; 5585 } 5586 if (first_mbind == NULL 5587 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) 5588 first_mbind = s; 5589 } 5590 5591 /* If there are any SHF_TLS output sections, add PT_TLS segment. */ 5592 if (tls_count > 0) 5593 { 5594 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 5595 amt += tls_count * sizeof (asection *); 5596 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5597 if (m == NULL) 5598 goto error_return; 5599 m->next = NULL; 5600 m->p_type = PT_TLS; 5601 m->count = tls_count; 5602 /* Mandated PF_R. */ 5603 m->p_flags = PF_R; 5604 m->p_flags_valid = 1; 5605 s = first_tls; 5606 for (i = 0; i < tls_count; ++i) 5607 { 5608 if ((s->flags & SEC_THREAD_LOCAL) == 0) 5609 { 5610 _bfd_error_handler 5611 (_("%pB: TLS sections are not adjacent:"), abfd); 5612 s = first_tls; 5613 i = 0; 5614 while (i < tls_count) 5615 { 5616 if ((s->flags & SEC_THREAD_LOCAL) != 0) 5617 { 5618 _bfd_error_handler (_(" TLS: %pA"), s); 5619 i++; 5620 } 5621 else 5622 _bfd_error_handler (_(" non-TLS: %pA"), s); 5623 s = s->next; 5624 } 5625 bfd_set_error (bfd_error_bad_value); 5626 goto error_return; 5627 } 5628 m->sections[i] = s; 5629 s = s->next; 5630 } 5631 5632 *pm = m; 5633 pm = &m->next; 5634 } 5635 5636 if (first_mbind 5637 && (abfd->flags & D_PAGED) != 0 5638 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) 5639 for (s = first_mbind; s != NULL; s = s->next) 5640 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 5641 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM) 5642 { 5643 /* Mandated PF_R. */ 5644 unsigned long p_flags = PF_R; 5645 if ((s->flags & SEC_READONLY) == 0) 5646 p_flags |= PF_W; 5647 if ((s->flags & SEC_CODE) != 0) 5648 p_flags |= PF_X; 5649 5650 amt = sizeof (struct elf_segment_map) + sizeof (asection *); 5651 m = bfd_zalloc (abfd, amt); 5652 if (m == NULL) 5653 goto error_return; 5654 m->next = NULL; 5655 m->p_type = (PT_GNU_MBIND_LO 5656 + elf_section_data (s)->this_hdr.sh_info); 5657 m->count = 1; 5658 m->p_flags_valid = 1; 5659 m->sections[0] = s; 5660 m->p_flags = p_flags; 5661 5662 *pm = m; 5663 pm = &m->next; 5664 } 5665 5666 s = bfd_get_section_by_name (abfd, 5667 NOTE_GNU_PROPERTY_SECTION_NAME); 5668 if (s != NULL && s->size != 0) 5669 { 5670 amt = sizeof (struct elf_segment_map) + sizeof (asection *); 5671 m = bfd_zalloc (abfd, amt); 5672 if (m == NULL) 5673 goto error_return; 5674 m->next = NULL; 5675 m->p_type = PT_GNU_PROPERTY; 5676 m->count = 1; 5677 m->p_flags_valid = 1; 5678 m->sections[0] = s; 5679 m->p_flags = PF_R; 5680 *pm = m; 5681 pm = &m->next; 5682 } 5683 5684 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME 5685 segment. */ 5686 eh_frame_hdr = elf_eh_frame_hdr (info); 5687 if (eh_frame_hdr != NULL 5688 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) 5689 { 5690 amt = sizeof (struct elf_segment_map); 5691 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5692 if (m == NULL) 5693 goto error_return; 5694 m->next = NULL; 5695 m->p_type = PT_GNU_EH_FRAME; 5696 m->count = 1; 5697 m->sections[0] = eh_frame_hdr->output_section; 5698 5699 *pm = m; 5700 pm = &m->next; 5701 } 5702 5703 /* If there is a .sframe section, throw in a PT_GNU_SFRAME 5704 segment. */ 5705 sframe = elf_sframe (abfd); 5706 if (sframe != NULL 5707 && (sframe->output_section->flags & SEC_LOAD) != 0 5708 && sframe->size != 0) 5709 { 5710 amt = sizeof (struct elf_segment_map); 5711 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5712 if (m == NULL) 5713 goto error_return; 5714 m->next = NULL; 5715 m->p_type = PT_GNU_SFRAME; 5716 m->count = 1; 5717 m->sections[0] = sframe->output_section; 5718 5719 *pm = m; 5720 pm = &m->next; 5721 } 5722 5723 if (elf_stack_flags (abfd)) 5724 { 5725 amt = sizeof (struct elf_segment_map); 5726 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5727 if (m == NULL) 5728 goto error_return; 5729 m->next = NULL; 5730 m->p_type = PT_GNU_STACK; 5731 m->p_flags = elf_stack_flags (abfd); 5732 m->p_align = bed->stack_align; 5733 m->p_flags_valid = 1; 5734 m->p_align_valid = m->p_align != 0; 5735 if (info->stacksize > 0) 5736 { 5737 m->p_size = info->stacksize; 5738 m->p_size_valid = 1; 5739 } 5740 5741 *pm = m; 5742 pm = &m->next; 5743 } 5744 5745 if (info != NULL && info->relro) 5746 { 5747 for (m = mfirst; m != NULL; m = m->next) 5748 { 5749 if (m->p_type == PT_LOAD 5750 && m->count != 0 5751 && m->sections[0]->vma >= info->relro_start 5752 && m->sections[0]->vma < info->relro_end) 5753 { 5754 i = m->count; 5755 while (--i != (unsigned) -1) 5756 { 5757 if (m->sections[i]->size > 0 5758 && (m->sections[i]->flags & SEC_LOAD) != 0 5759 && (m->sections[i]->flags & SEC_HAS_CONTENTS) != 0) 5760 break; 5761 } 5762 5763 if (i != (unsigned) -1) 5764 break; 5765 } 5766 } 5767 5768 /* Make a PT_GNU_RELRO segment only when it isn't empty. */ 5769 if (m != NULL) 5770 { 5771 amt = sizeof (struct elf_segment_map); 5772 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5773 if (m == NULL) 5774 goto error_return; 5775 m->next = NULL; 5776 m->p_type = PT_GNU_RELRO; 5777 *pm = m; 5778 pm = &m->next; 5779 } 5780 } 5781 5782 free (sections); 5783 elf_seg_map (abfd) = mfirst; 5784 } 5785 5786 if (!elf_modify_segment_map (abfd, info, no_user_phdrs || info == NULL)) 5787 return false; 5788 5789 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) 5790 ++count; 5791 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; 5792 5793 return true; 5794 5795 error_return: 5796 free (sections); 5797 return false; 5798 } 5799 5800 /* Sort sections by address. */ 5801 5802 static int 5803 elf_sort_sections (const void *arg1, const void *arg2) 5804 { 5805 const asection *sec1 = *(const asection **) arg1; 5806 const asection *sec2 = *(const asection **) arg2; 5807 bfd_size_type size1, size2; 5808 5809 /* Sort by LMA first, since this is the address used to 5810 place the section into a segment. */ 5811 if (sec1->lma < sec2->lma) 5812 return -1; 5813 else if (sec1->lma > sec2->lma) 5814 return 1; 5815 5816 /* Then sort by VMA. Normally the LMA and the VMA will be 5817 the same, and this will do nothing. */ 5818 if (sec1->vma < sec2->vma) 5819 return -1; 5820 else if (sec1->vma > sec2->vma) 5821 return 1; 5822 5823 /* Put !SEC_LOAD sections after SEC_LOAD ones. */ 5824 5825 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 \ 5826 && (x)->size != 0) 5827 5828 if (TOEND (sec1)) 5829 { 5830 if (!TOEND (sec2)) 5831 return 1; 5832 } 5833 else if (TOEND (sec2)) 5834 return -1; 5835 5836 #undef TOEND 5837 5838 /* Sort by size, to put zero sized sections 5839 before others at the same address. */ 5840 5841 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; 5842 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; 5843 5844 if (size1 < size2) 5845 return -1; 5846 if (size1 > size2) 5847 return 1; 5848 5849 return sec1->target_index - sec2->target_index; 5850 } 5851 5852 /* This qsort comparison functions sorts PT_LOAD segments first and 5853 by p_paddr, for assign_file_positions_for_load_sections. */ 5854 5855 static int 5856 elf_sort_segments (const void *arg1, const void *arg2) 5857 { 5858 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1; 5859 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2; 5860 5861 if (m1->p_type != m2->p_type) 5862 { 5863 if (m1->p_type == PT_NULL) 5864 return 1; 5865 if (m2->p_type == PT_NULL) 5866 return -1; 5867 return m1->p_type < m2->p_type ? -1 : 1; 5868 } 5869 if (m1->includes_filehdr != m2->includes_filehdr) 5870 return m1->includes_filehdr ? -1 : 1; 5871 if (m1->no_sort_lma != m2->no_sort_lma) 5872 return m1->no_sort_lma ? -1 : 1; 5873 if (m1->p_type == PT_LOAD && !m1->no_sort_lma) 5874 { 5875 bfd_vma lma1, lma2; /* Octets. */ 5876 lma1 = 0; 5877 if (m1->p_paddr_valid) 5878 lma1 = m1->p_paddr; 5879 else if (m1->count != 0) 5880 { 5881 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner, 5882 m1->sections[0]); 5883 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb; 5884 } 5885 lma2 = 0; 5886 if (m2->p_paddr_valid) 5887 lma2 = m2->p_paddr; 5888 else if (m2->count != 0) 5889 { 5890 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner, 5891 m2->sections[0]); 5892 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb; 5893 } 5894 if (lma1 != lma2) 5895 return lma1 < lma2 ? -1 : 1; 5896 } 5897 if (m1->idx != m2->idx) 5898 return m1->idx < m2->idx ? -1 : 1; 5899 return 0; 5900 } 5901 5902 /* Ian Lance Taylor writes: 5903 5904 We shouldn't be using % with a negative signed number. That's just 5905 not good. We have to make sure either that the number is not 5906 negative, or that the number has an unsigned type. When the types 5907 are all the same size they wind up as unsigned. When file_ptr is a 5908 larger signed type, the arithmetic winds up as signed long long, 5909 which is wrong. 5910 5911 What we're trying to say here is something like ``increase OFF by 5912 the least amount that will cause it to be equal to the VMA modulo 5913 the page size.'' */ 5914 /* In other words, something like: 5915 5916 vma_offset = m->sections[0]->vma % bed->maxpagesize; 5917 off_offset = off % bed->maxpagesize; 5918 if (vma_offset < off_offset) 5919 adjustment = vma_offset + bed->maxpagesize - off_offset; 5920 else 5921 adjustment = vma_offset - off_offset; 5922 5923 which can be collapsed into the expression below. */ 5924 5925 static file_ptr 5926 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) 5927 { 5928 /* PR binutils/16199: Handle an alignment of zero. */ 5929 if (maxpagesize == 0) 5930 maxpagesize = 1; 5931 return ((vma - off) % maxpagesize); 5932 } 5933 5934 static void 5935 print_segment_map (const struct elf_segment_map *m) 5936 { 5937 unsigned int j; 5938 const char *pt = get_segment_type (m->p_type); 5939 char buf[32]; 5940 5941 if (pt == NULL) 5942 { 5943 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) 5944 sprintf (buf, "LOPROC+%7.7x", 5945 (unsigned int) (m->p_type - PT_LOPROC)); 5946 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) 5947 sprintf (buf, "LOOS+%7.7x", 5948 (unsigned int) (m->p_type - PT_LOOS)); 5949 else 5950 snprintf (buf, sizeof (buf), "%8.8x", 5951 (unsigned int) m->p_type); 5952 pt = buf; 5953 } 5954 fflush (stdout); 5955 fprintf (stderr, "%s:", pt); 5956 for (j = 0; j < m->count; j++) 5957 fprintf (stderr, " %s", m->sections [j]->name); 5958 putc ('\n',stderr); 5959 fflush (stderr); 5960 } 5961 5962 /* Assign file positions to the sections based on the mapping from 5963 sections to segments. This function also sets up some fields in 5964 the file header. */ 5965 5966 static bool 5967 assign_file_positions_for_load_sections (bfd *abfd, 5968 struct bfd_link_info *link_info) 5969 { 5970 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 5971 struct elf_segment_map *m; 5972 struct elf_segment_map *phdr_load_seg; 5973 Elf_Internal_Phdr *phdrs; 5974 Elf_Internal_Phdr *p; 5975 file_ptr off; /* Octets. */ 5976 bfd_size_type maxpagesize; 5977 unsigned int alloc, actual; 5978 unsigned int i, j; 5979 struct elf_segment_map **sorted_seg_map; 5980 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 5981 5982 if (link_info == NULL 5983 && !_bfd_elf_map_sections_to_segments (abfd, link_info, NULL)) 5984 return false; 5985 5986 alloc = 0; 5987 for (m = elf_seg_map (abfd); m != NULL; m = m->next) 5988 m->idx = alloc++; 5989 5990 if (alloc) 5991 { 5992 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; 5993 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; 5994 } 5995 else 5996 { 5997 /* PR binutils/12467. */ 5998 elf_elfheader (abfd)->e_phoff = 0; 5999 elf_elfheader (abfd)->e_phentsize = 0; 6000 } 6001 6002 elf_elfheader (abfd)->e_phnum = alloc; 6003 6004 if (elf_program_header_size (abfd) == (bfd_size_type) -1) 6005 { 6006 actual = alloc; 6007 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; 6008 } 6009 else 6010 { 6011 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr; 6012 BFD_ASSERT (elf_program_header_size (abfd) 6013 == actual * bed->s->sizeof_phdr); 6014 BFD_ASSERT (actual >= alloc); 6015 } 6016 6017 if (alloc == 0) 6018 { 6019 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; 6020 return true; 6021 } 6022 6023 /* We're writing the size in elf_program_header_size (abfd), 6024 see assign_file_positions_except_relocs, so make sure we have 6025 that amount allocated, with trailing space cleared. 6026 The variable alloc contains the computed need, while 6027 elf_program_header_size (abfd) contains the size used for the 6028 layout. 6029 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments 6030 where the layout is forced to according to a larger size in the 6031 last iterations for the testcase ld-elf/header. */ 6032 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs) 6033 + alloc * sizeof (*sorted_seg_map))); 6034 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual); 6035 elf_tdata (abfd)->phdr = phdrs; 6036 if (phdrs == NULL) 6037 return false; 6038 6039 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++) 6040 { 6041 sorted_seg_map[j] = m; 6042 /* If elf_segment_map is not from map_sections_to_segments, the 6043 sections may not be correctly ordered. NOTE: sorting should 6044 not be done to the PT_NOTE section of a corefile, which may 6045 contain several pseudo-sections artificially created by bfd. 6046 Sorting these pseudo-sections breaks things badly. */ 6047 if (m->count > 1 6048 && !(elf_elfheader (abfd)->e_type == ET_CORE 6049 && m->p_type == PT_NOTE)) 6050 { 6051 for (i = 0; i < m->count; i++) 6052 m->sections[i]->target_index = i; 6053 qsort (m->sections, (size_t) m->count, sizeof (asection *), 6054 elf_sort_sections); 6055 } 6056 } 6057 if (alloc > 1) 6058 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map), 6059 elf_sort_segments); 6060 6061 maxpagesize = 1; 6062 if ((abfd->flags & D_PAGED) != 0) 6063 { 6064 if (link_info != NULL) 6065 maxpagesize = link_info->maxpagesize; 6066 else 6067 maxpagesize = bed->maxpagesize; 6068 } 6069 6070 /* Sections must map to file offsets past the ELF file header. */ 6071 off = bed->s->sizeof_ehdr; 6072 /* And if one of the PT_LOAD headers doesn't include the program 6073 headers then we'll be mapping program headers in the usual 6074 position after the ELF file header. */ 6075 phdr_load_seg = NULL; 6076 for (j = 0; j < alloc; j++) 6077 { 6078 m = sorted_seg_map[j]; 6079 if (m->p_type != PT_LOAD) 6080 break; 6081 if (m->includes_phdrs) 6082 { 6083 phdr_load_seg = m; 6084 break; 6085 } 6086 } 6087 if (phdr_load_seg == NULL) 6088 off += actual * bed->s->sizeof_phdr; 6089 6090 for (j = 0; j < alloc; j++) 6091 { 6092 asection **secpp; 6093 bfd_vma off_adjust; /* Octets. */ 6094 bool no_contents; 6095 bfd_size_type p_align; 6096 bool p_align_p; 6097 6098 /* An ELF segment (described by Elf_Internal_Phdr) may contain a 6099 number of sections with contents contributing to both p_filesz 6100 and p_memsz, followed by a number of sections with no contents 6101 that just contribute to p_memsz. In this loop, OFF tracks next 6102 available file offset for PT_LOAD and PT_NOTE segments. */ 6103 m = sorted_seg_map[j]; 6104 p = phdrs + m->idx; 6105 p->p_type = m->p_type; 6106 p->p_flags = m->p_flags; 6107 p_align = bed->p_align; 6108 p_align_p = false; 6109 6110 if (m->count == 0) 6111 p->p_vaddr = m->p_vaddr_offset * opb; 6112 else 6113 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb; 6114 6115 if (m->p_paddr_valid) 6116 p->p_paddr = m->p_paddr; 6117 else if (m->count == 0) 6118 p->p_paddr = 0; 6119 else 6120 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb; 6121 6122 if (p->p_type == PT_LOAD 6123 && (abfd->flags & D_PAGED) != 0) 6124 { 6125 /* p_align in demand paged PT_LOAD segments effectively stores 6126 the maximum page size. When copying an executable with 6127 objcopy, we set m->p_align from the input file. Use this 6128 value for maxpagesize rather than bed->maxpagesize, which 6129 may be different. Note that we use maxpagesize for PT_TLS 6130 segment alignment later in this function, so we are relying 6131 on at least one PT_LOAD segment appearing before a PT_TLS 6132 segment. */ 6133 if (m->p_align_valid) 6134 maxpagesize = m->p_align; 6135 else if (p_align != 0 6136 && (link_info == NULL 6137 || !link_info->maxpagesize_is_set)) 6138 /* Set p_align to the default p_align value while laying 6139 out segments aligning to the maximum page size or the 6140 largest section alignment. The run-time loader can 6141 align segments to the default p_align value or the 6142 maximum page size, depending on system page size. */ 6143 p_align_p = true; 6144 6145 p->p_align = maxpagesize; 6146 } 6147 else if (m->p_align_valid) 6148 p->p_align = m->p_align; 6149 else if (m->count == 0) 6150 p->p_align = 1 << bed->s->log_file_align; 6151 6152 if (m == phdr_load_seg) 6153 { 6154 if (!m->includes_filehdr) 6155 p->p_offset = off; 6156 off += actual * bed->s->sizeof_phdr; 6157 } 6158 6159 no_contents = false; 6160 off_adjust = 0; 6161 if (p->p_type == PT_LOAD 6162 && m->count > 0) 6163 { 6164 bfd_size_type align; /* Bytes. */ 6165 unsigned int align_power = 0; 6166 6167 if (m->p_align_valid) 6168 align = p->p_align; 6169 else 6170 { 6171 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 6172 { 6173 unsigned int secalign; 6174 6175 secalign = bfd_section_alignment (*secpp); 6176 if (secalign > align_power) 6177 align_power = secalign; 6178 } 6179 align = (bfd_size_type) 1 << align_power; 6180 if (align < maxpagesize) 6181 { 6182 /* If a section requires alignment higher than the 6183 default p_align value, don't set p_align to the 6184 default p_align value. */ 6185 if (align > p_align) 6186 p_align_p = false; 6187 align = maxpagesize; 6188 } 6189 else 6190 { 6191 /* If a section requires alignment higher than the 6192 maximum page size, set p_align to the section 6193 alignment. */ 6194 p_align_p = true; 6195 p_align = align; 6196 } 6197 } 6198 6199 for (i = 0; i < m->count; i++) 6200 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) 6201 /* If we aren't making room for this section, then 6202 it must be SHT_NOBITS regardless of what we've 6203 set via struct bfd_elf_special_section. */ 6204 elf_section_type (m->sections[i]) = SHT_NOBITS; 6205 6206 /* Find out whether this segment contains any loadable 6207 sections. */ 6208 no_contents = true; 6209 for (i = 0; i < m->count; i++) 6210 if (elf_section_type (m->sections[i]) != SHT_NOBITS) 6211 { 6212 no_contents = false; 6213 break; 6214 } 6215 6216 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb); 6217 6218 /* Broken hardware and/or kernel require that files do not 6219 map the same page with different permissions on some hppa 6220 processors. */ 6221 if (j != 0 6222 && (abfd->flags & D_PAGED) != 0 6223 && bed->no_page_alias 6224 && (off & (maxpagesize - 1)) != 0 6225 && ((off & -maxpagesize) 6226 == ((off + off_adjust) & -maxpagesize))) 6227 off_adjust += maxpagesize; 6228 off += off_adjust; 6229 if (no_contents) 6230 { 6231 /* We shouldn't need to align the segment on disk since 6232 the segment doesn't need file space, but the gABI 6233 arguably requires the alignment and glibc ld.so 6234 checks it. So to comply with the alignment 6235 requirement but not waste file space, we adjust 6236 p_offset for just this segment. (OFF_ADJUST is 6237 subtracted from OFF later.) This may put p_offset 6238 past the end of file, but that shouldn't matter. */ 6239 } 6240 else 6241 off_adjust = 0; 6242 } 6243 /* Make sure the .dynamic section is the first section in the 6244 PT_DYNAMIC segment. */ 6245 else if (p->p_type == PT_DYNAMIC 6246 && m->count > 1 6247 && strcmp (m->sections[0]->name, ".dynamic") != 0) 6248 { 6249 _bfd_error_handler 6250 (_("%pB: The first section in the PT_DYNAMIC segment" 6251 " is not the .dynamic section"), 6252 abfd); 6253 bfd_set_error (bfd_error_bad_value); 6254 return false; 6255 } 6256 /* Set the note section type to SHT_NOTE. */ 6257 else if (p->p_type == PT_NOTE) 6258 for (i = 0; i < m->count; i++) 6259 elf_section_type (m->sections[i]) = SHT_NOTE; 6260 6261 if (m->includes_filehdr) 6262 { 6263 if (!m->p_flags_valid) 6264 p->p_flags |= PF_R; 6265 p->p_filesz = bed->s->sizeof_ehdr; 6266 p->p_memsz = bed->s->sizeof_ehdr; 6267 if (p->p_type == PT_LOAD) 6268 { 6269 if (m->count > 0) 6270 { 6271 if (p->p_vaddr < (bfd_vma) off 6272 || (!m->p_paddr_valid 6273 && p->p_paddr < (bfd_vma) off)) 6274 { 6275 _bfd_error_handler 6276 (_("%pB: not enough room for program headers," 6277 " try linking with -N"), 6278 abfd); 6279 bfd_set_error (bfd_error_bad_value); 6280 return false; 6281 } 6282 p->p_vaddr -= off; 6283 if (!m->p_paddr_valid) 6284 p->p_paddr -= off; 6285 } 6286 } 6287 else if (sorted_seg_map[0]->includes_filehdr) 6288 { 6289 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx; 6290 p->p_vaddr = filehdr->p_vaddr; 6291 if (!m->p_paddr_valid) 6292 p->p_paddr = filehdr->p_paddr; 6293 } 6294 } 6295 6296 if (m->includes_phdrs) 6297 { 6298 if (!m->p_flags_valid) 6299 p->p_flags |= PF_R; 6300 p->p_filesz += actual * bed->s->sizeof_phdr; 6301 p->p_memsz += actual * bed->s->sizeof_phdr; 6302 if (!m->includes_filehdr) 6303 { 6304 if (p->p_type == PT_LOAD) 6305 { 6306 elf_elfheader (abfd)->e_phoff = p->p_offset; 6307 if (m->count > 0) 6308 { 6309 p->p_vaddr -= off - p->p_offset; 6310 if (!m->p_paddr_valid) 6311 p->p_paddr -= off - p->p_offset; 6312 } 6313 } 6314 else if (phdr_load_seg != NULL) 6315 { 6316 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx; 6317 bfd_vma phdr_off = 0; /* Octets. */ 6318 if (phdr_load_seg->includes_filehdr) 6319 phdr_off = bed->s->sizeof_ehdr; 6320 p->p_vaddr = phdr->p_vaddr + phdr_off; 6321 if (!m->p_paddr_valid) 6322 p->p_paddr = phdr->p_paddr + phdr_off; 6323 p->p_offset = phdr->p_offset + phdr_off; 6324 } 6325 else 6326 p->p_offset = bed->s->sizeof_ehdr; 6327 } 6328 } 6329 6330 if (p->p_type == PT_LOAD 6331 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) 6332 { 6333 if (!m->includes_filehdr && !m->includes_phdrs) 6334 { 6335 p->p_offset = off; 6336 if (no_contents) 6337 { 6338 /* Put meaningless p_offset for PT_LOAD segments 6339 without file contents somewhere within the first 6340 page, in an attempt to not point past EOF. */ 6341 bfd_size_type align = maxpagesize; 6342 if (align < p->p_align) 6343 align = p->p_align; 6344 if (align < 1) 6345 align = 1; 6346 p->p_offset = off % align; 6347 } 6348 } 6349 else 6350 { 6351 file_ptr adjust; /* Octets. */ 6352 6353 adjust = off - (p->p_offset + p->p_filesz); 6354 if (!no_contents) 6355 p->p_filesz += adjust; 6356 p->p_memsz += adjust; 6357 } 6358 } 6359 6360 /* Set up p_filesz, p_memsz, p_align and p_flags from the section 6361 maps. Set filepos for sections in PT_LOAD segments, and in 6362 core files, for sections in PT_NOTE segments. 6363 assign_file_positions_for_non_load_sections will set filepos 6364 for other sections and update p_filesz for other segments. */ 6365 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 6366 { 6367 asection *sec; 6368 bfd_size_type align; 6369 Elf_Internal_Shdr *this_hdr; 6370 6371 sec = *secpp; 6372 this_hdr = &elf_section_data (sec)->this_hdr; 6373 align = (bfd_size_type) 1 << bfd_section_alignment (sec); 6374 6375 if ((p->p_type == PT_LOAD 6376 || p->p_type == PT_TLS) 6377 && (this_hdr->sh_type != SHT_NOBITS 6378 || ((this_hdr->sh_flags & SHF_ALLOC) != 0 6379 && ((this_hdr->sh_flags & SHF_TLS) == 0 6380 || p->p_type == PT_TLS)))) 6381 { 6382 bfd_vma p_start = p->p_paddr; /* Octets. */ 6383 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */ 6384 bfd_vma s_start = sec->lma * opb; /* Octets. */ 6385 bfd_vma adjust = s_start - p_end; /* Octets. */ 6386 6387 if (adjust != 0 6388 && (s_start < p_end 6389 || p_end < p_start)) 6390 { 6391 _bfd_error_handler 6392 /* xgettext:c-format */ 6393 (_("%pB: section %pA lma %#" PRIx64 6394 " adjusted to %#" PRIx64), 6395 abfd, sec, (uint64_t) s_start / opb, 6396 (uint64_t) p_end / opb); 6397 adjust = 0; 6398 sec->lma = p_end / opb; 6399 } 6400 p->p_memsz += adjust; 6401 6402 if (p->p_type == PT_LOAD) 6403 { 6404 if (this_hdr->sh_type != SHT_NOBITS) 6405 { 6406 off_adjust = 0; 6407 if (p->p_filesz + adjust < p->p_memsz) 6408 { 6409 /* We have a PROGBITS section following NOBITS ones. 6410 Allocate file space for the NOBITS section(s). 6411 We don't need to write out the zeros, posix 6412 fseek past the end of data already written 6413 followed by a write at that location is 6414 guaranteed to result in zeros being read 6415 from the gap. */ 6416 adjust = p->p_memsz - p->p_filesz; 6417 } 6418 } 6419 /* We only adjust sh_offset in SHT_NOBITS sections 6420 as would seem proper for their address when the 6421 section is first in the segment. sh_offset 6422 doesn't really have any significance for 6423 SHT_NOBITS anyway, apart from a notional position 6424 relative to other sections. Historically we 6425 didn't bother with adjusting sh_offset and some 6426 programs depend on it not being adjusted. See 6427 pr12921 and pr25662. */ 6428 if (this_hdr->sh_type != SHT_NOBITS || i == 0) 6429 { 6430 off += adjust; 6431 if (this_hdr->sh_type == SHT_NOBITS) 6432 off_adjust += adjust; 6433 } 6434 } 6435 if (this_hdr->sh_type != SHT_NOBITS) 6436 p->p_filesz += adjust; 6437 } 6438 6439 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) 6440 { 6441 /* The section at i == 0 is the one that actually contains 6442 everything. */ 6443 if (i == 0) 6444 { 6445 this_hdr->sh_offset = sec->filepos = off; 6446 off += this_hdr->sh_size; 6447 p->p_filesz = this_hdr->sh_size; 6448 p->p_memsz = 0; 6449 p->p_align = 1; 6450 } 6451 else 6452 { 6453 /* The rest are fake sections that shouldn't be written. */ 6454 sec->filepos = 0; 6455 sec->size = 0; 6456 sec->flags = 0; 6457 continue; 6458 } 6459 } 6460 else 6461 { 6462 if (p->p_type == PT_LOAD) 6463 { 6464 this_hdr->sh_offset = sec->filepos = off; 6465 if (this_hdr->sh_type != SHT_NOBITS) 6466 off += this_hdr->sh_size; 6467 } 6468 else if (this_hdr->sh_type == SHT_NOBITS 6469 && (this_hdr->sh_flags & SHF_TLS) != 0 6470 && this_hdr->sh_offset == 0) 6471 { 6472 /* This is a .tbss section that didn't get a PT_LOAD. 6473 (See _bfd_elf_map_sections_to_segments "Create a 6474 final PT_LOAD".) Set sh_offset to the value it 6475 would have if we had created a zero p_filesz and 6476 p_memsz PT_LOAD header for the section. This 6477 also makes the PT_TLS header have the same 6478 p_offset value. */ 6479 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, 6480 off, align); 6481 this_hdr->sh_offset = sec->filepos = off + adjust; 6482 } 6483 6484 if (this_hdr->sh_type != SHT_NOBITS) 6485 { 6486 p->p_filesz += this_hdr->sh_size; 6487 /* A load section without SHF_ALLOC is something like 6488 a note section in a PT_NOTE segment. These take 6489 file space but are not loaded into memory. */ 6490 if ((this_hdr->sh_flags & SHF_ALLOC) != 0) 6491 p->p_memsz += this_hdr->sh_size; 6492 } 6493 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) 6494 { 6495 if (p->p_type == PT_TLS) 6496 p->p_memsz += this_hdr->sh_size; 6497 6498 /* .tbss is special. It doesn't contribute to p_memsz of 6499 normal segments. */ 6500 else if ((this_hdr->sh_flags & SHF_TLS) == 0) 6501 p->p_memsz += this_hdr->sh_size; 6502 } 6503 6504 if (align > p->p_align 6505 && !m->p_align_valid 6506 && (p->p_type != PT_LOAD 6507 || (abfd->flags & D_PAGED) == 0)) 6508 p->p_align = align; 6509 } 6510 6511 if (!m->p_flags_valid) 6512 { 6513 p->p_flags |= PF_R; 6514 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) 6515 p->p_flags |= PF_X; 6516 if ((this_hdr->sh_flags & SHF_WRITE) != 0) 6517 p->p_flags |= PF_W; 6518 } 6519 } 6520 6521 off -= off_adjust; 6522 6523 /* PR ld/20815 - Check that the program header segment, if 6524 present, will be loaded into memory. */ 6525 if (p->p_type == PT_PHDR 6526 && phdr_load_seg == NULL 6527 && !(bed->elf_backend_allow_non_load_phdr != NULL 6528 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc))) 6529 { 6530 /* The fix for this error is usually to edit the linker script being 6531 used and set up the program headers manually. Either that or 6532 leave room for the headers at the start of the SECTIONS. */ 6533 _bfd_error_handler (_("%pB: error: PHDR segment not covered" 6534 " by LOAD segment"), 6535 abfd); 6536 if (link_info == NULL) 6537 return false; 6538 /* Arrange for the linker to exit with an error, deleting 6539 the output file unless --noinhibit-exec is given. */ 6540 link_info->callbacks->info ("%X"); 6541 } 6542 6543 /* Check that all sections are in a PT_LOAD segment. 6544 Don't check funky gdb generated core files. */ 6545 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) 6546 { 6547 bool check_vma = true; 6548 6549 for (i = 1; i < m->count; i++) 6550 if (m->sections[i]->vma == m->sections[i - 1]->vma 6551 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) 6552 ->this_hdr), p) != 0 6553 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) 6554 ->this_hdr), p) != 0) 6555 { 6556 /* Looks like we have overlays packed into the segment. */ 6557 check_vma = false; 6558 break; 6559 } 6560 6561 for (i = 0; i < m->count; i++) 6562 { 6563 Elf_Internal_Shdr *this_hdr; 6564 asection *sec; 6565 6566 sec = m->sections[i]; 6567 this_hdr = &(elf_section_data(sec)->this_hdr); 6568 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) 6569 && !ELF_TBSS_SPECIAL (this_hdr, p)) 6570 { 6571 _bfd_error_handler 6572 /* xgettext:c-format */ 6573 (_("%pB: section `%pA' can't be allocated in segment %d"), 6574 abfd, sec, j); 6575 print_segment_map (m); 6576 } 6577 } 6578 6579 if (p_align_p) 6580 p->p_align = p_align; 6581 } 6582 } 6583 6584 elf_next_file_pos (abfd) = off; 6585 6586 if (link_info != NULL 6587 && phdr_load_seg != NULL 6588 && phdr_load_seg->includes_filehdr) 6589 { 6590 /* There is a segment that contains both the file headers and the 6591 program headers, so provide a symbol __ehdr_start pointing there. 6592 A program can use this to examine itself robustly. */ 6593 6594 struct elf_link_hash_entry *hash 6595 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", 6596 false, false, true); 6597 /* If the symbol was referenced and not defined, define it. */ 6598 if (hash != NULL 6599 && (hash->root.type == bfd_link_hash_new 6600 || hash->root.type == bfd_link_hash_undefined 6601 || hash->root.type == bfd_link_hash_undefweak 6602 || hash->root.type == bfd_link_hash_common)) 6603 { 6604 asection *s = NULL; 6605 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb; 6606 6607 if (phdr_load_seg->count != 0) 6608 /* The segment contains sections, so use the first one. */ 6609 s = phdr_load_seg->sections[0]; 6610 else 6611 /* Use the first (i.e. lowest-addressed) section in any segment. */ 6612 for (m = elf_seg_map (abfd); m != NULL; m = m->next) 6613 if (m->p_type == PT_LOAD && m->count != 0) 6614 { 6615 s = m->sections[0]; 6616 break; 6617 } 6618 6619 if (s != NULL) 6620 { 6621 hash->root.u.def.value = filehdr_vaddr - s->vma; 6622 hash->root.u.def.section = s; 6623 } 6624 else 6625 { 6626 hash->root.u.def.value = filehdr_vaddr; 6627 hash->root.u.def.section = bfd_abs_section_ptr; 6628 } 6629 6630 hash->root.type = bfd_link_hash_defined; 6631 hash->def_regular = 1; 6632 hash->non_elf = 0; 6633 } 6634 } 6635 6636 return true; 6637 } 6638 6639 /* Determine if a bfd is a debuginfo file. Unfortunately there 6640 is no defined method for detecting such files, so we have to 6641 use heuristics instead. */ 6642 6643 bool 6644 is_debuginfo_file (bfd *abfd) 6645 { 6646 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour) 6647 return false; 6648 6649 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd); 6650 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd); 6651 Elf_Internal_Shdr **headerp; 6652 6653 for (headerp = start_headers; headerp < end_headers; headerp ++) 6654 { 6655 Elf_Internal_Shdr *header = * headerp; 6656 6657 /* Debuginfo files do not have any allocated SHT_PROGBITS sections. 6658 The only allocated sections are SHT_NOBITS or SHT_NOTES. */ 6659 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC 6660 && header->sh_type != SHT_NOBITS 6661 && header->sh_type != SHT_NOTE) 6662 return false; 6663 } 6664 6665 return true; 6666 } 6667 6668 /* Assign file positions for other sections, except for compressed debug 6669 and sections assigned in _bfd_elf_assign_file_positions_for_non_load. */ 6670 6671 static bool 6672 assign_file_positions_for_non_load_sections (bfd *abfd, 6673 struct bfd_link_info *link_info) 6674 { 6675 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6676 Elf_Internal_Shdr **i_shdrpp; 6677 Elf_Internal_Shdr **hdrpp, **end_hdrpp; 6678 Elf_Internal_Phdr *phdrs; 6679 Elf_Internal_Phdr *p; 6680 struct elf_segment_map *m; 6681 file_ptr off; 6682 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 6683 bfd_vma maxpagesize; 6684 6685 if (link_info != NULL) 6686 maxpagesize = link_info->maxpagesize; 6687 else 6688 maxpagesize = bed->maxpagesize; 6689 i_shdrpp = elf_elfsections (abfd); 6690 end_hdrpp = i_shdrpp + elf_numsections (abfd); 6691 off = elf_next_file_pos (abfd); 6692 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) 6693 { 6694 Elf_Internal_Shdr *hdr; 6695 bfd_vma align; 6696 6697 hdr = *hdrpp; 6698 if (hdr->bfd_section != NULL 6699 && (hdr->bfd_section->filepos != 0 6700 || (hdr->sh_type == SHT_NOBITS 6701 && hdr->contents == NULL))) 6702 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); 6703 else if ((hdr->sh_flags & SHF_ALLOC) != 0) 6704 { 6705 if (hdr->sh_size != 0 6706 /* PR 24717 - debuginfo files are known to be not strictly 6707 compliant with the ELF standard. In particular they often 6708 have .note.gnu.property sections that are outside of any 6709 loadable segment. This is not a problem for such files, 6710 so do not warn about them. */ 6711 && ! is_debuginfo_file (abfd)) 6712 _bfd_error_handler 6713 /* xgettext:c-format */ 6714 (_("%pB: warning: allocated section `%s' not in segment"), 6715 abfd, 6716 (hdr->bfd_section == NULL 6717 ? "*unknown*" 6718 : hdr->bfd_section->name)); 6719 /* We don't need to page align empty sections. */ 6720 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) 6721 align = maxpagesize; 6722 else 6723 align = hdr->sh_addralign & -hdr->sh_addralign; 6724 off += vma_page_aligned_bias (hdr->sh_addr, off, align); 6725 off = _bfd_elf_assign_file_position_for_section (hdr, off, 6726 false); 6727 } 6728 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 6729 && hdr->bfd_section == NULL) 6730 /* We don't know the offset of these sections yet: 6731 their size has not been decided. */ 6732 || (abfd->is_linker_output 6733 && hdr->bfd_section != NULL 6734 && (hdr->sh_name == -1u 6735 || bfd_section_is_ctf (hdr->bfd_section))) 6736 || hdr == i_shdrpp[elf_onesymtab (abfd)] 6737 || (elf_symtab_shndx_list (abfd) != NULL 6738 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) 6739 || hdr == i_shdrpp[elf_strtab_sec (abfd)] 6740 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) 6741 hdr->sh_offset = -1; 6742 else 6743 off = _bfd_elf_assign_file_position_for_section (hdr, off, true); 6744 } 6745 elf_next_file_pos (abfd) = off; 6746 6747 /* Now that we have set the section file positions, we can set up 6748 the file positions for the non PT_LOAD segments. */ 6749 phdrs = elf_tdata (abfd)->phdr; 6750 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) 6751 { 6752 if (p->p_type == PT_GNU_RELRO) 6753 { 6754 bfd_vma start, end; /* Bytes. */ 6755 bool ok; 6756 6757 if (link_info != NULL) 6758 { 6759 /* During linking the range of the RELRO segment is passed 6760 in link_info. Note that there may be padding between 6761 relro_start and the first RELRO section. */ 6762 start = link_info->relro_start; 6763 end = link_info->relro_end; 6764 } 6765 else if (m->count != 0) 6766 { 6767 if (!m->p_size_valid) 6768 abort (); 6769 start = m->sections[0]->vma; 6770 end = start + m->p_size / opb; 6771 } 6772 else 6773 { 6774 start = 0; 6775 end = 0; 6776 } 6777 6778 ok = false; 6779 if (start < end) 6780 { 6781 struct elf_segment_map *lm; 6782 const Elf_Internal_Phdr *lp; 6783 unsigned int i; 6784 6785 /* Find a LOAD segment containing a section in the RELRO 6786 segment. */ 6787 for (lm = elf_seg_map (abfd), lp = phdrs; 6788 lm != NULL; 6789 lm = lm->next, lp++) 6790 { 6791 if (lp->p_type == PT_LOAD 6792 && lm->count != 0 6793 && (lm->sections[lm->count - 1]->vma 6794 + (!IS_TBSS (lm->sections[lm->count - 1]) 6795 ? lm->sections[lm->count - 1]->size / opb 6796 : 0)) > start 6797 && lm->sections[0]->vma < end) 6798 break; 6799 } 6800 6801 if (lm != NULL) 6802 { 6803 /* Find the section starting the RELRO segment. */ 6804 for (i = 0; i < lm->count; i++) 6805 { 6806 asection *s = lm->sections[i]; 6807 if (s->vma >= start 6808 && s->vma < end 6809 && s->size != 0) 6810 break; 6811 } 6812 6813 if (i < lm->count) 6814 { 6815 p->p_vaddr = lm->sections[i]->vma * opb; 6816 p->p_paddr = lm->sections[i]->lma * opb; 6817 p->p_offset = lm->sections[i]->filepos; 6818 p->p_memsz = end * opb - p->p_vaddr; 6819 p->p_filesz = p->p_memsz; 6820 6821 /* The RELRO segment typically ends a few bytes 6822 into .got.plt but other layouts are possible. 6823 In cases where the end does not match any 6824 loaded section (for instance is in file 6825 padding), trim p_filesz back to correspond to 6826 the end of loaded section contents. */ 6827 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr) 6828 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr; 6829 6830 /* Preserve the alignment and flags if they are 6831 valid. The gold linker generates RW/4 for 6832 the PT_GNU_RELRO section. It is better for 6833 objcopy/strip to honor these attributes 6834 otherwise gdb will choke when using separate 6835 debug files. */ 6836 if (!m->p_align_valid) 6837 p->p_align = 1; 6838 if (!m->p_flags_valid) 6839 p->p_flags = PF_R; 6840 ok = true; 6841 } 6842 } 6843 } 6844 6845 if (!ok) 6846 { 6847 if (link_info != NULL) 6848 _bfd_error_handler 6849 (_("%pB: warning: unable to allocate any sections" 6850 " to PT_GNU_RELRO segment"), 6851 abfd); 6852 memset (p, 0, sizeof *p); 6853 } 6854 } 6855 else if (p->p_type == PT_GNU_STACK) 6856 { 6857 if (m->p_size_valid) 6858 p->p_memsz = m->p_size; 6859 } 6860 else if (m->count != 0) 6861 { 6862 unsigned int i; 6863 6864 if (p->p_type != PT_LOAD 6865 && (p->p_type != PT_NOTE 6866 || bfd_get_format (abfd) != bfd_core)) 6867 { 6868 /* A user specified segment layout may include a PHDR 6869 segment that overlaps with a LOAD segment... */ 6870 if (p->p_type == PT_PHDR) 6871 { 6872 m->count = 0; 6873 continue; 6874 } 6875 6876 if (m->includes_filehdr || m->includes_phdrs) 6877 { 6878 /* PR 17512: file: 2195325e. */ 6879 _bfd_error_handler 6880 (_("%pB: error: non-load segment %d includes file header " 6881 "and/or program header"), 6882 abfd, (int) (p - phdrs)); 6883 return false; 6884 } 6885 6886 p->p_filesz = 0; 6887 p->p_offset = m->sections[0]->filepos; 6888 for (i = m->count; i-- != 0;) 6889 { 6890 asection *sect = m->sections[i]; 6891 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; 6892 if (hdr->sh_type != SHT_NOBITS) 6893 { 6894 p->p_filesz = sect->filepos - p->p_offset + hdr->sh_size; 6895 /* NB: p_memsz of the loadable PT_NOTE segment 6896 should be the same as p_filesz. */ 6897 if (p->p_type == PT_NOTE 6898 && (hdr->sh_flags & SHF_ALLOC) != 0) 6899 p->p_memsz = p->p_filesz; 6900 break; 6901 } 6902 } 6903 } 6904 } 6905 } 6906 6907 return true; 6908 } 6909 6910 static elf_section_list * 6911 find_section_in_list (unsigned int i, elf_section_list * list) 6912 { 6913 for (;list != NULL; list = list->next) 6914 if (list->ndx == i) 6915 break; 6916 return list; 6917 } 6918 6919 /* Work out the file positions of all the sections. This is called by 6920 _bfd_elf_compute_section_file_positions. All the section sizes and 6921 VMAs must be known before this is called. 6922 6923 Reloc sections come in two flavours: Those processed specially as 6924 "side-channel" data attached to a section to which they apply, and 6925 those that bfd doesn't process as relocations. The latter sort are 6926 stored in a normal bfd section by bfd_section_from_shdr. We don't 6927 consider the former sort here, unless they form part of the loadable 6928 image. Reloc sections not assigned here (and compressed debugging 6929 sections and CTF sections which nothing else in the file can rely 6930 upon) will be handled later by assign_file_positions_for_relocs. 6931 6932 We also don't set the positions of the .symtab and .strtab here. */ 6933 6934 static bool 6935 assign_file_positions_except_relocs (bfd *abfd, 6936 struct bfd_link_info *link_info) 6937 { 6938 struct elf_obj_tdata *tdata = elf_tdata (abfd); 6939 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); 6940 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6941 unsigned int alloc; 6942 6943 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 6944 && bfd_get_format (abfd) != bfd_core) 6945 { 6946 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); 6947 unsigned int num_sec = elf_numsections (abfd); 6948 Elf_Internal_Shdr **hdrpp; 6949 unsigned int i; 6950 file_ptr off; 6951 6952 /* Start after the ELF header. */ 6953 off = i_ehdrp->e_ehsize; 6954 6955 /* We are not creating an executable, which means that we are 6956 not creating a program header, and that the actual order of 6957 the sections in the file is unimportant. */ 6958 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) 6959 { 6960 Elf_Internal_Shdr *hdr; 6961 6962 hdr = *hdrpp; 6963 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 6964 && hdr->bfd_section == NULL) 6965 /* Do not assign offsets for these sections yet: we don't know 6966 their sizes. */ 6967 || (abfd->is_linker_output 6968 && hdr->bfd_section != NULL 6969 && (hdr->sh_name == -1u 6970 || bfd_section_is_ctf (hdr->bfd_section))) 6971 || i == elf_onesymtab (abfd) 6972 || (elf_symtab_shndx_list (abfd) != NULL 6973 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) 6974 || i == elf_strtab_sec (abfd) 6975 || i == elf_shstrtab_sec (abfd)) 6976 { 6977 hdr->sh_offset = -1; 6978 } 6979 else 6980 off = _bfd_elf_assign_file_position_for_section (hdr, off, true); 6981 } 6982 6983 elf_next_file_pos (abfd) = off; 6984 elf_program_header_size (abfd) = 0; 6985 } 6986 else 6987 { 6988 /* Assign file positions for the loaded sections based on the 6989 assignment of sections to segments. */ 6990 if (!assign_file_positions_for_load_sections (abfd, link_info)) 6991 return false; 6992 6993 /* And for non-load sections. */ 6994 if (!assign_file_positions_for_non_load_sections (abfd, link_info)) 6995 return false; 6996 } 6997 6998 if (!(*bed->elf_backend_modify_headers) (abfd, link_info)) 6999 return false; 7000 7001 /* Write out the program headers. */ 7002 alloc = i_ehdrp->e_phnum; 7003 if (alloc != 0) 7004 { 7005 if (link_info != NULL && ! link_info->no_warn_rwx_segments) 7006 { 7007 bool warned_tls = false; 7008 bool warned_rwx = false; 7009 7010 /* Memory resident segments with non-zero size and RWX 7011 permissions are a security risk, so we generate a warning 7012 here if we are creating any. */ 7013 unsigned int i; 7014 7015 for (i = 0; i < alloc; i++) 7016 { 7017 const Elf_Internal_Phdr * phdr = tdata->phdr + i; 7018 7019 if (phdr->p_memsz == 0) 7020 continue; 7021 7022 if (! warned_tls 7023 && phdr->p_type == PT_TLS 7024 && (phdr->p_flags & PF_X)) 7025 { 7026 if (link_info->warn_is_error_for_rwx_segments) 7027 { 7028 _bfd_error_handler (_("\ 7029 error: %pB has a TLS segment with execute permission"), 7030 abfd); 7031 return false; 7032 } 7033 7034 _bfd_error_handler (_("\ 7035 warning: %pB has a TLS segment with execute permission"), 7036 abfd); 7037 if (warned_rwx) 7038 break; 7039 7040 warned_tls = true; 7041 } 7042 else if (! warned_rwx 7043 && phdr->p_type == PT_LOAD 7044 && ((phdr->p_flags & (PF_R | PF_W | PF_X)) 7045 == (PF_R | PF_W | PF_X))) 7046 { 7047 if (link_info->warn_is_error_for_rwx_segments) 7048 { 7049 _bfd_error_handler (_("\ 7050 error: %pB has a LOAD segment with RWX permissions"), 7051 abfd); 7052 return false; 7053 } 7054 7055 _bfd_error_handler (_("\ 7056 warning: %pB has a LOAD segment with RWX permissions"), 7057 abfd); 7058 if (warned_tls) 7059 break; 7060 7061 warned_rwx = true; 7062 } 7063 } 7064 } 7065 7066 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0 7067 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) 7068 return false; 7069 } 7070 7071 return true; 7072 } 7073 7074 bool 7075 _bfd_elf_init_file_header (bfd *abfd, 7076 struct bfd_link_info *info ATTRIBUTE_UNUSED) 7077 { 7078 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ 7079 struct elf_strtab_hash *shstrtab; 7080 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 7081 7082 i_ehdrp = elf_elfheader (abfd); 7083 7084 shstrtab = _bfd_elf_strtab_init (); 7085 if (shstrtab == NULL) 7086 return false; 7087 7088 elf_shstrtab (abfd) = shstrtab; 7089 7090 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; 7091 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; 7092 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; 7093 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; 7094 7095 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; 7096 i_ehdrp->e_ident[EI_DATA] = 7097 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; 7098 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; 7099 7100 if ((abfd->flags & DYNAMIC) != 0) 7101 i_ehdrp->e_type = ET_DYN; 7102 else if ((abfd->flags & EXEC_P) != 0) 7103 i_ehdrp->e_type = ET_EXEC; 7104 else if (bfd_get_format (abfd) == bfd_core) 7105 i_ehdrp->e_type = ET_CORE; 7106 else 7107 i_ehdrp->e_type = ET_REL; 7108 7109 switch (bfd_get_arch (abfd)) 7110 { 7111 case bfd_arch_unknown: 7112 i_ehdrp->e_machine = EM_NONE; 7113 break; 7114 7115 /* There used to be a long list of cases here, each one setting 7116 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE 7117 in the corresponding bfd definition. To avoid duplication, 7118 the switch was removed. Machines that need special handling 7119 can generally do it in elf_backend_final_write_processing(), 7120 unless they need the information earlier than the final write. 7121 Such need can generally be supplied by replacing the tests for 7122 e_machine with the conditions used to determine it. */ 7123 default: 7124 i_ehdrp->e_machine = bed->elf_machine_code; 7125 } 7126 7127 i_ehdrp->e_version = bed->s->ev_current; 7128 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; 7129 7130 /* No program header, for now. */ 7131 i_ehdrp->e_phoff = 0; 7132 i_ehdrp->e_phentsize = 0; 7133 i_ehdrp->e_phnum = 0; 7134 7135 /* Each bfd section is section header entry. */ 7136 i_ehdrp->e_entry = bfd_get_start_address (abfd); 7137 i_ehdrp->e_shentsize = bed->s->sizeof_shdr; 7138 7139 elf_tdata (abfd)->symtab_hdr.sh_name = 7140 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", false); 7141 elf_tdata (abfd)->strtab_hdr.sh_name = 7142 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", false); 7143 elf_tdata (abfd)->shstrtab_hdr.sh_name = 7144 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", false); 7145 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 7146 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 7147 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) 7148 return false; 7149 7150 return true; 7151 } 7152 7153 /* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. 7154 7155 FIXME: We used to have code here to sort the PT_LOAD segments into 7156 ascending order, as per the ELF spec. But this breaks some programs, 7157 including the Linux kernel. But really either the spec should be 7158 changed or the programs updated. */ 7159 7160 bool 7161 _bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info) 7162 { 7163 if (link_info != NULL && bfd_link_pie (link_info)) 7164 { 7165 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd); 7166 unsigned int num_segments = i_ehdrp->e_phnum; 7167 struct elf_obj_tdata *tdata = elf_tdata (obfd); 7168 Elf_Internal_Phdr *segment = tdata->phdr; 7169 Elf_Internal_Phdr *end_segment = &segment[num_segments]; 7170 7171 /* Find the lowest p_vaddr in PT_LOAD segments. */ 7172 bfd_vma p_vaddr = (bfd_vma) -1; 7173 for (; segment < end_segment; segment++) 7174 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) 7175 p_vaddr = segment->p_vaddr; 7176 7177 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD 7178 segments is non-zero. */ 7179 if (p_vaddr) 7180 i_ehdrp->e_type = ET_EXEC; 7181 } 7182 return true; 7183 } 7184 7185 /* Assign file positions for all the reloc sections which are not part 7186 of the loadable file image, and the file position of section headers. */ 7187 7188 static bool 7189 _bfd_elf_assign_file_positions_for_non_load (bfd *abfd) 7190 { 7191 file_ptr off; 7192 Elf_Internal_Shdr **shdrpp, **end_shdrpp; 7193 Elf_Internal_Shdr *shdrp; 7194 Elf_Internal_Ehdr *i_ehdrp; 7195 const struct elf_backend_data *bed; 7196 7197 /* Skip non-load sections without section header. */ 7198 if ((abfd->flags & BFD_NO_SECTION_HEADER) != 0) 7199 return true; 7200 7201 off = elf_next_file_pos (abfd); 7202 7203 shdrpp = elf_elfsections (abfd); 7204 end_shdrpp = shdrpp + elf_numsections (abfd); 7205 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) 7206 { 7207 shdrp = *shdrpp; 7208 if (shdrp->sh_offset == -1) 7209 { 7210 asection *sec = shdrp->bfd_section; 7211 if (sec == NULL 7212 || shdrp->sh_type == SHT_REL 7213 || shdrp->sh_type == SHT_RELA) 7214 ; 7215 else if (bfd_section_is_ctf (sec)) 7216 { 7217 /* Update section size and contents. */ 7218 shdrp->sh_size = sec->size; 7219 shdrp->contents = sec->contents; 7220 } 7221 else if (shdrp->sh_name == -1u) 7222 { 7223 const char *name = sec->name; 7224 struct bfd_elf_section_data *d; 7225 7226 /* Compress DWARF debug sections. */ 7227 if (!bfd_compress_section (abfd, sec, shdrp->contents)) 7228 return false; 7229 7230 if (sec->compress_status == COMPRESS_SECTION_DONE 7231 && (abfd->flags & BFD_COMPRESS_GABI) == 0 7232 && name[1] == 'd') 7233 { 7234 /* If section is compressed with zlib-gnu, convert 7235 section name from .debug_* to .zdebug_*. */ 7236 char *new_name = bfd_debug_name_to_zdebug (abfd, name); 7237 if (new_name == NULL) 7238 return false; 7239 name = new_name; 7240 } 7241 /* Add section name to section name section. */ 7242 shdrp->sh_name 7243 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 7244 name, false); 7245 d = elf_section_data (sec); 7246 7247 /* Add reloc section name to section name section. */ 7248 if (d->rel.hdr 7249 && !_bfd_elf_set_reloc_sh_name (abfd, d->rel.hdr, 7250 name, false)) 7251 return false; 7252 if (d->rela.hdr 7253 && !_bfd_elf_set_reloc_sh_name (abfd, d->rela.hdr, 7254 name, true)) 7255 return false; 7256 7257 /* Update section size and contents. */ 7258 shdrp->sh_size = sec->size; 7259 shdrp->contents = sec->contents; 7260 sec->contents = NULL; 7261 } 7262 7263 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true); 7264 } 7265 } 7266 7267 /* Place section name section after DWARF debug sections have been 7268 compressed. */ 7269 _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); 7270 shdrp = &elf_tdata (abfd)->shstrtab_hdr; 7271 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); 7272 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true); 7273 7274 /* Place the section headers. */ 7275 i_ehdrp = elf_elfheader (abfd); 7276 bed = get_elf_backend_data (abfd); 7277 off = align_file_position (off, 1 << bed->s->log_file_align); 7278 i_ehdrp->e_shoff = off; 7279 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; 7280 elf_next_file_pos (abfd) = off; 7281 7282 return true; 7283 } 7284 7285 bool 7286 _bfd_elf_write_object_contents (bfd *abfd) 7287 { 7288 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 7289 Elf_Internal_Shdr **i_shdrp; 7290 bool failed; 7291 unsigned int count, num_sec; 7292 struct elf_obj_tdata *t; 7293 7294 if (! abfd->output_has_begun 7295 && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) 7296 return false; 7297 /* Do not rewrite ELF data when the BFD has been opened for update. 7298 abfd->output_has_begun was set to TRUE on opening, so creation of 7299 new sections, and modification of existing section sizes was 7300 restricted. This means the ELF header, program headers and 7301 section headers can't have changed. If the contents of any 7302 sections has been modified, then those changes have already been 7303 written to the BFD. */ 7304 else if (abfd->direction == both_direction) 7305 { 7306 BFD_ASSERT (abfd->output_has_begun); 7307 return true; 7308 } 7309 7310 i_shdrp = elf_elfsections (abfd); 7311 7312 failed = false; 7313 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); 7314 if (failed) 7315 return false; 7316 7317 if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) 7318 return false; 7319 7320 /* After writing the headers, we need to write the sections too... */ 7321 num_sec = elf_numsections (abfd); 7322 for (count = 1; count < num_sec; count++) 7323 { 7324 /* Don't set the sh_name field without section header. */ 7325 if ((abfd->flags & BFD_NO_SECTION_HEADER) == 0) 7326 i_shdrp[count]->sh_name 7327 = _bfd_elf_strtab_offset (elf_shstrtab (abfd), 7328 i_shdrp[count]->sh_name); 7329 if (bed->elf_backend_section_processing) 7330 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count])) 7331 return false; 7332 if (i_shdrp[count]->contents) 7333 { 7334 bfd_size_type amt = i_shdrp[count]->sh_size; 7335 7336 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 7337 || bfd_write (i_shdrp[count]->contents, amt, abfd) != amt) 7338 return false; 7339 } 7340 } 7341 7342 /* Write out the section header names. */ 7343 t = elf_tdata (abfd); 7344 if (elf_shstrtab (abfd) != NULL 7345 && t->shstrtab_hdr.sh_offset != -1 7346 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 7347 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) 7348 return false; 7349 7350 if (!(*bed->elf_backend_final_write_processing) (abfd)) 7351 return false; 7352 7353 if (!bed->s->write_shdrs_and_ehdr (abfd)) 7354 return false; 7355 7356 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ 7357 if (t->o->build_id.after_write_object_contents != NULL 7358 && !(*t->o->build_id.after_write_object_contents) (abfd)) 7359 return false; 7360 if (t->o->package_metadata.after_write_object_contents != NULL 7361 && !(*t->o->package_metadata.after_write_object_contents) (abfd)) 7362 return false; 7363 7364 return true; 7365 } 7366 7367 bool 7368 _bfd_elf_write_corefile_contents (bfd *abfd) 7369 { 7370 /* Hopefully this can be done just like an object file. */ 7371 return _bfd_elf_write_object_contents (abfd); 7372 } 7373 7374 /* Given a section, search the header to find them. */ 7375 7376 unsigned int 7377 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) 7378 { 7379 const struct elf_backend_data *bed; 7380 unsigned int sec_index; 7381 7382 if (elf_section_data (asect) != NULL 7383 && elf_section_data (asect)->this_idx != 0) 7384 return elf_section_data (asect)->this_idx; 7385 7386 if (bfd_is_abs_section (asect)) 7387 sec_index = SHN_ABS; 7388 else if (bfd_is_com_section (asect)) 7389 sec_index = SHN_COMMON; 7390 else if (bfd_is_und_section (asect)) 7391 sec_index = SHN_UNDEF; 7392 else 7393 sec_index = SHN_BAD; 7394 7395 bed = get_elf_backend_data (abfd); 7396 if (bed->elf_backend_section_from_bfd_section) 7397 { 7398 int retval = sec_index; 7399 7400 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) 7401 return retval; 7402 } 7403 7404 if (sec_index == SHN_BAD) 7405 bfd_set_error (bfd_error_nonrepresentable_section); 7406 7407 return sec_index; 7408 } 7409 7410 /* Given a BFD symbol, return the index in the ELF symbol table, or -1 7411 on error. */ 7412 7413 int 7414 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) 7415 { 7416 asymbol *asym_ptr = *asym_ptr_ptr; 7417 int idx; 7418 flagword flags = asym_ptr->flags; 7419 7420 /* When gas creates relocations against local labels, it creates its 7421 own symbol for the section, but does put the symbol into the 7422 symbol chain, so udata is 0. When the linker is generating 7423 relocatable output, this section symbol may be for one of the 7424 input sections rather than the output section. */ 7425 if (asym_ptr->udata.i == 0 7426 && (flags & BSF_SECTION_SYM) 7427 && asym_ptr->section) 7428 { 7429 asection *sec; 7430 7431 sec = asym_ptr->section; 7432 if (sec->owner != abfd && sec->output_section != NULL) 7433 sec = sec->output_section; 7434 if (sec->owner == abfd 7435 && sec->index < elf_num_section_syms (abfd) 7436 && elf_section_syms (abfd)[sec->index] != NULL) 7437 asym_ptr->udata.i = elf_section_syms (abfd)[sec->index]->udata.i; 7438 } 7439 7440 idx = asym_ptr->udata.i; 7441 7442 if (idx == 0) 7443 { 7444 /* This case can occur when using --strip-symbol on a symbol 7445 which is used in a relocation entry. */ 7446 _bfd_error_handler 7447 /* xgettext:c-format */ 7448 (_("%pB: symbol `%s' required but not present"), 7449 abfd, bfd_asymbol_name (asym_ptr)); 7450 bfd_set_error (bfd_error_no_symbols); 7451 return -1; 7452 } 7453 7454 #if DEBUG & 4 7455 { 7456 fprintf (stderr, 7457 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d," 7458 " flags = 0x%.8x\n", 7459 (long) asym_ptr, asym_ptr->name, idx, flags); 7460 fflush (stderr); 7461 } 7462 #endif 7463 7464 return idx; 7465 } 7466 7467 static inline bfd_vma 7468 segment_size (Elf_Internal_Phdr *segment) 7469 { 7470 return (segment->p_memsz > segment->p_filesz 7471 ? segment->p_memsz : segment->p_filesz); 7472 } 7473 7474 7475 /* Returns the end address of the segment + 1. */ 7476 static inline bfd_vma 7477 segment_end (Elf_Internal_Phdr *segment, bfd_vma start) 7478 { 7479 return start + segment_size (segment); 7480 } 7481 7482 static inline bfd_size_type 7483 section_size (asection *section, Elf_Internal_Phdr *segment) 7484 { 7485 if ((section->flags & SEC_HAS_CONTENTS) != 0 7486 || (section->flags & SEC_THREAD_LOCAL) == 0 7487 || segment->p_type == PT_TLS) 7488 return section->size; 7489 return 0; 7490 } 7491 7492 /* Returns TRUE if the given section is contained within the given 7493 segment. LMA addresses are compared against PADDR when 7494 USE_VADDR is false, VMA against VADDR when true. */ 7495 static bool 7496 is_contained_by (asection *section, Elf_Internal_Phdr *segment, 7497 bfd_vma paddr, bfd_vma vaddr, unsigned int opb, 7498 bool use_vaddr) 7499 { 7500 bfd_vma seg_addr = !use_vaddr ? paddr : vaddr; 7501 bfd_vma addr = !use_vaddr ? section->lma : section->vma; 7502 bfd_vma octet; 7503 if (_bfd_mul_overflow (addr, opb, &octet)) 7504 return false; 7505 /* The third and fourth lines below are testing that the section end 7506 address is within the segment. It's written this way to avoid 7507 overflow. Add seg_addr + section_size to both sides of the 7508 inequality to make it obvious. */ 7509 return (octet >= seg_addr 7510 && segment_size (segment) >= section_size (section, segment) 7511 && (octet - seg_addr 7512 <= segment_size (segment) - section_size (section, segment))); 7513 } 7514 7515 /* Handle PT_NOTE segment. */ 7516 static bool 7517 is_note (asection *s, Elf_Internal_Phdr *p) 7518 { 7519 return (p->p_type == PT_NOTE 7520 && elf_section_type (s) == SHT_NOTE 7521 && (ufile_ptr) s->filepos >= p->p_offset 7522 && p->p_filesz >= s->size 7523 && (ufile_ptr) s->filepos - p->p_offset <= p->p_filesz - s->size); 7524 } 7525 7526 /* Rewrite program header information. */ 7527 7528 static bool 7529 rewrite_elf_program_header (bfd *ibfd, bfd *obfd, bfd_vma maxpagesize) 7530 { 7531 Elf_Internal_Ehdr *iehdr; 7532 struct elf_segment_map *map; 7533 struct elf_segment_map *map_first; 7534 struct elf_segment_map **pointer_to_map; 7535 Elf_Internal_Phdr *segment; 7536 asection *section; 7537 unsigned int i; 7538 unsigned int num_segments; 7539 bool phdr_included = false; 7540 bool p_paddr_valid; 7541 struct elf_segment_map *phdr_adjust_seg = NULL; 7542 unsigned int phdr_adjust_num = 0; 7543 const struct elf_backend_data *bed; 7544 unsigned int opb = bfd_octets_per_byte (ibfd, NULL); 7545 7546 bed = get_elf_backend_data (ibfd); 7547 iehdr = elf_elfheader (ibfd); 7548 7549 map_first = NULL; 7550 pointer_to_map = &map_first; 7551 7552 num_segments = elf_elfheader (ibfd)->e_phnum; 7553 7554 /* The complicated case when p_vaddr is 0 is to handle the Solaris 7555 linker, which generates a PT_INTERP section with p_vaddr and 7556 p_memsz set to 0. */ 7557 #define IS_SOLARIS_PT_INTERP(p, s) \ 7558 (p->p_vaddr == 0 \ 7559 && p->p_paddr == 0 \ 7560 && p->p_memsz == 0 \ 7561 && p->p_filesz > 0 \ 7562 && (s->flags & SEC_HAS_CONTENTS) != 0 \ 7563 && s->size > 0 \ 7564 && (bfd_vma) s->filepos >= p->p_offset \ 7565 && ((bfd_vma) s->filepos + s->size \ 7566 <= p->p_offset + p->p_filesz)) 7567 7568 /* Decide if the given section should be included in the given segment. 7569 A section will be included if: 7570 1. It is within the address space of the segment -- we use the LMA 7571 if that is set for the segment and the VMA otherwise, 7572 2. It is an allocated section or a NOTE section in a PT_NOTE 7573 segment. 7574 3. There is an output section associated with it, 7575 4. The section has not already been allocated to a previous segment. 7576 5. PT_GNU_STACK segments do not include any sections. 7577 6. PT_TLS segment includes only SHF_TLS sections. 7578 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. 7579 8. PT_DYNAMIC should not contain empty sections at the beginning 7580 (with the possible exception of .dynamic). */ 7581 #define IS_SECTION_IN_INPUT_SEGMENT(section, segment, opb, paddr_valid) \ 7582 (((is_contained_by (section, segment, segment->p_paddr, \ 7583 segment->p_vaddr, opb, !paddr_valid) \ 7584 && (section->flags & SEC_ALLOC) != 0) \ 7585 || is_note (section, segment)) \ 7586 && segment->p_type != PT_GNU_STACK \ 7587 && (segment->p_type != PT_TLS \ 7588 || (section->flags & SEC_THREAD_LOCAL)) \ 7589 && (segment->p_type == PT_LOAD \ 7590 || segment->p_type == PT_TLS \ 7591 || (section->flags & SEC_THREAD_LOCAL) == 0) \ 7592 && (segment->p_type != PT_DYNAMIC \ 7593 || section_size (section, segment) > 0 \ 7594 || (segment->p_paddr \ 7595 ? segment->p_paddr != section->lma * (opb) \ 7596 : segment->p_vaddr != section->vma * (opb)) \ 7597 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \ 7598 && (segment->p_type != PT_LOAD || !section->segment_mark)) 7599 7600 /* If the output section of a section in the input segment is NULL, 7601 it is removed from the corresponding output segment. */ 7602 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, opb, paddr_valid) \ 7603 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, paddr_valid) \ 7604 && section->output_section != NULL) 7605 7606 /* Returns TRUE iff seg1 starts after the end of seg2. */ 7607 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ 7608 (seg1->field >= segment_end (seg2, seg2->field)) 7609 7610 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both 7611 their VMA address ranges and their LMA address ranges overlap. 7612 It is possible to have overlapping VMA ranges without overlapping LMA 7613 ranges. RedBoot images for example can have both .data and .bss mapped 7614 to the same VMA range, but with the .data section mapped to a different 7615 LMA. */ 7616 #define SEGMENT_OVERLAPS(seg1, seg2) \ 7617 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ 7618 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ 7619 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ 7620 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) 7621 7622 /* Initialise the segment mark field, and discard stupid alignment. */ 7623 for (section = ibfd->sections; section != NULL; section = section->next) 7624 { 7625 asection *o = section->output_section; 7626 if (o != NULL && o->alignment_power >= (sizeof (bfd_vma) * 8) - 1) 7627 o->alignment_power = 0; 7628 section->segment_mark = false; 7629 } 7630 7631 /* The Solaris linker creates program headers in which all the 7632 p_paddr fields are zero. When we try to objcopy or strip such a 7633 file, we get confused. Check for this case, and if we find it 7634 don't set the p_paddr_valid fields. */ 7635 p_paddr_valid = false; 7636 for (i = 0, segment = elf_tdata (ibfd)->phdr; 7637 i < num_segments; 7638 i++, segment++) 7639 if (segment->p_paddr != 0) 7640 { 7641 p_paddr_valid = true; 7642 break; 7643 } 7644 7645 /* Scan through the segments specified in the program header 7646 of the input BFD. For this first scan we look for overlaps 7647 in the loadable segments. These can be created by weird 7648 parameters to objcopy. Also, fix some solaris weirdness. */ 7649 for (i = 0, segment = elf_tdata (ibfd)->phdr; 7650 i < num_segments; 7651 i++, segment++) 7652 { 7653 unsigned int j; 7654 Elf_Internal_Phdr *segment2; 7655 7656 if (segment->p_type == PT_INTERP) 7657 for (section = ibfd->sections; section; section = section->next) 7658 if (IS_SOLARIS_PT_INTERP (segment, section)) 7659 { 7660 /* Mininal change so that the normal section to segment 7661 assignment code will work. */ 7662 segment->p_vaddr = section->vma * opb; 7663 break; 7664 } 7665 7666 if (segment->p_type != PT_LOAD) 7667 { 7668 /* Remove PT_GNU_RELRO segment. */ 7669 if (segment->p_type == PT_GNU_RELRO) 7670 segment->p_type = PT_NULL; 7671 continue; 7672 } 7673 7674 /* Determine if this segment overlaps any previous segments. */ 7675 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) 7676 { 7677 bfd_signed_vma extra_length; 7678 7679 if (segment2->p_type != PT_LOAD 7680 || !SEGMENT_OVERLAPS (segment, segment2)) 7681 continue; 7682 7683 /* Merge the two segments together. */ 7684 if (segment2->p_vaddr < segment->p_vaddr) 7685 { 7686 /* Extend SEGMENT2 to include SEGMENT and then delete 7687 SEGMENT. */ 7688 extra_length = (segment_end (segment, segment->p_vaddr) 7689 - segment_end (segment2, segment2->p_vaddr)); 7690 7691 if (extra_length > 0) 7692 { 7693 segment2->p_memsz += extra_length; 7694 segment2->p_filesz += extra_length; 7695 } 7696 7697 segment->p_type = PT_NULL; 7698 7699 /* Since we have deleted P we must restart the outer loop. */ 7700 i = 0; 7701 segment = elf_tdata (ibfd)->phdr; 7702 break; 7703 } 7704 else 7705 { 7706 /* Extend SEGMENT to include SEGMENT2 and then delete 7707 SEGMENT2. */ 7708 extra_length = (segment_end (segment2, segment2->p_vaddr) 7709 - segment_end (segment, segment->p_vaddr)); 7710 7711 if (extra_length > 0) 7712 { 7713 segment->p_memsz += extra_length; 7714 segment->p_filesz += extra_length; 7715 } 7716 7717 segment2->p_type = PT_NULL; 7718 } 7719 } 7720 } 7721 7722 /* The second scan attempts to assign sections to segments. */ 7723 for (i = 0, segment = elf_tdata (ibfd)->phdr; 7724 i < num_segments; 7725 i++, segment++) 7726 { 7727 unsigned int section_count; 7728 asection **sections; 7729 asection *output_section; 7730 unsigned int isec; 7731 asection *matching_lma; 7732 asection *suggested_lma; 7733 unsigned int j; 7734 size_t amt; 7735 asection *first_section; 7736 7737 if (segment->p_type == PT_NULL) 7738 continue; 7739 7740 first_section = NULL; 7741 /* Compute how many sections might be placed into this segment. */ 7742 for (section = ibfd->sections, section_count = 0; 7743 section != NULL; 7744 section = section->next) 7745 { 7746 /* Find the first section in the input segment, which may be 7747 removed from the corresponding output segment. */ 7748 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, opb, p_paddr_valid)) 7749 { 7750 if (first_section == NULL) 7751 first_section = section; 7752 if (section->output_section != NULL) 7753 ++section_count; 7754 } 7755 } 7756 7757 /* Allocate a segment map big enough to contain 7758 all of the sections we have selected. */ 7759 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 7760 amt += section_count * sizeof (asection *); 7761 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 7762 if (map == NULL) 7763 return false; 7764 7765 /* Initialise the fields of the segment map. Default to 7766 using the physical address of the segment in the input BFD. */ 7767 map->next = NULL; 7768 map->p_type = segment->p_type; 7769 map->p_flags = segment->p_flags; 7770 map->p_flags_valid = 1; 7771 7772 if (map->p_type == PT_LOAD 7773 && (ibfd->flags & D_PAGED) != 0 7774 && maxpagesize > 1 7775 && segment->p_align > 1) 7776 { 7777 map->p_align = segment->p_align; 7778 if (segment->p_align > maxpagesize) 7779 map->p_align = maxpagesize; 7780 map->p_align_valid = 1; 7781 } 7782 7783 /* If the first section in the input segment is removed, there is 7784 no need to preserve segment physical address in the corresponding 7785 output segment. */ 7786 if (!first_section || first_section->output_section != NULL) 7787 { 7788 map->p_paddr = segment->p_paddr; 7789 map->p_paddr_valid = p_paddr_valid; 7790 } 7791 7792 /* Determine if this segment contains the ELF file header 7793 and if it contains the program headers themselves. */ 7794 map->includes_filehdr = (segment->p_offset == 0 7795 && segment->p_filesz >= iehdr->e_ehsize); 7796 map->includes_phdrs = 0; 7797 7798 if (!phdr_included || segment->p_type != PT_LOAD) 7799 { 7800 map->includes_phdrs = 7801 (segment->p_offset <= (bfd_vma) iehdr->e_phoff 7802 && (segment->p_offset + segment->p_filesz 7803 >= ((bfd_vma) iehdr->e_phoff 7804 + iehdr->e_phnum * iehdr->e_phentsize))); 7805 7806 if (segment->p_type == PT_LOAD && map->includes_phdrs) 7807 phdr_included = true; 7808 } 7809 7810 if (section_count == 0) 7811 { 7812 /* Special segments, such as the PT_PHDR segment, may contain 7813 no sections, but ordinary, loadable segments should contain 7814 something. They are allowed by the ELF spec however, so only 7815 a warning is produced. 7816 Don't warn if an empty PT_LOAD contains the program headers. 7817 There is however the valid use case of embedded systems which 7818 have segments with p_filesz of 0 and a p_memsz > 0 to initialize 7819 flash memory with zeros. No warning is shown for that case. */ 7820 if (segment->p_type == PT_LOAD 7821 && !map->includes_phdrs 7822 && (segment->p_filesz > 0 || segment->p_memsz == 0)) 7823 /* xgettext:c-format */ 7824 _bfd_error_handler 7825 (_("%pB: warning: empty loadable segment detected" 7826 " at vaddr=%#" PRIx64 ", is this intentional?"), 7827 ibfd, (uint64_t) segment->p_vaddr); 7828 7829 map->p_vaddr_offset = segment->p_vaddr / opb; 7830 map->count = 0; 7831 *pointer_to_map = map; 7832 pointer_to_map = &map->next; 7833 7834 continue; 7835 } 7836 7837 /* Now scan the sections in the input BFD again and attempt 7838 to add their corresponding output sections to the segment map. 7839 The problem here is how to handle an output section which has 7840 been moved (ie had its LMA changed). There are four possibilities: 7841 7842 1. None of the sections have been moved. 7843 In this case we can continue to use the segment LMA from the 7844 input BFD. 7845 7846 2. All of the sections have been moved by the same amount. 7847 In this case we can change the segment's LMA to match the LMA 7848 of the first section. 7849 7850 3. Some of the sections have been moved, others have not. 7851 In this case those sections which have not been moved can be 7852 placed in the current segment which will have to have its size, 7853 and possibly its LMA changed, and a new segment or segments will 7854 have to be created to contain the other sections. 7855 7856 4. The sections have been moved, but not by the same amount. 7857 In this case we can change the segment's LMA to match the LMA 7858 of the first section and we will have to create a new segment 7859 or segments to contain the other sections. 7860 7861 In order to save time, we allocate an array to hold the section 7862 pointers that we are interested in. As these sections get assigned 7863 to a segment, they are removed from this array. */ 7864 7865 amt = section_count * sizeof (asection *); 7866 sections = (asection **) bfd_malloc (amt); 7867 if (sections == NULL) 7868 return false; 7869 7870 /* Step One: Scan for segment vs section LMA conflicts. 7871 Also add the sections to the section array allocated above. 7872 Also add the sections to the current segment. In the common 7873 case, where the sections have not been moved, this means that 7874 we have completely filled the segment, and there is nothing 7875 more to do. */ 7876 isec = 0; 7877 matching_lma = NULL; 7878 suggested_lma = NULL; 7879 7880 for (section = first_section, j = 0; 7881 section != NULL; 7882 section = section->next) 7883 { 7884 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, opb, p_paddr_valid)) 7885 { 7886 output_section = section->output_section; 7887 7888 sections[j++] = section; 7889 7890 /* The Solaris native linker always sets p_paddr to 0. 7891 We try to catch that case here, and set it to the 7892 correct value. Note - some backends require that 7893 p_paddr be left as zero. */ 7894 if (!p_paddr_valid 7895 && segment->p_vaddr != 0 7896 && !bed->want_p_paddr_set_to_zero 7897 && isec == 0 7898 && output_section->lma != 0 7899 && (align_power (segment->p_vaddr 7900 + (map->includes_filehdr 7901 ? iehdr->e_ehsize : 0) 7902 + (map->includes_phdrs 7903 ? iehdr->e_phnum * iehdr->e_phentsize 7904 : 0), 7905 output_section->alignment_power * opb) 7906 == (output_section->vma * opb))) 7907 map->p_paddr = segment->p_vaddr; 7908 7909 /* Match up the physical address of the segment with the 7910 LMA address of the output section. */ 7911 if (is_contained_by (output_section, segment, map->p_paddr, 7912 0, opb, false) 7913 || is_note (section, segment)) 7914 { 7915 if (matching_lma == NULL 7916 || output_section->lma < matching_lma->lma) 7917 matching_lma = output_section; 7918 7919 /* We assume that if the section fits within the segment 7920 then it does not overlap any other section within that 7921 segment. */ 7922 map->sections[isec++] = output_section; 7923 } 7924 else if (suggested_lma == NULL) 7925 suggested_lma = output_section; 7926 7927 if (j == section_count) 7928 break; 7929 } 7930 } 7931 7932 BFD_ASSERT (j == section_count); 7933 7934 /* Step Two: Adjust the physical address of the current segment, 7935 if necessary. */ 7936 if (isec == section_count) 7937 { 7938 /* All of the sections fitted within the segment as currently 7939 specified. This is the default case. Add the segment to 7940 the list of built segments and carry on to process the next 7941 program header in the input BFD. */ 7942 map->count = section_count; 7943 *pointer_to_map = map; 7944 pointer_to_map = &map->next; 7945 7946 if (p_paddr_valid 7947 && !bed->want_p_paddr_set_to_zero) 7948 { 7949 bfd_vma hdr_size = 0; 7950 if (map->includes_filehdr) 7951 hdr_size = iehdr->e_ehsize; 7952 if (map->includes_phdrs) 7953 hdr_size += iehdr->e_phnum * iehdr->e_phentsize; 7954 7955 /* Account for padding before the first section in the 7956 segment. */ 7957 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb 7958 - matching_lma->lma); 7959 } 7960 7961 free (sections); 7962 continue; 7963 } 7964 else 7965 { 7966 /* Change the current segment's physical address to match 7967 the LMA of the first section that fitted, or if no 7968 section fitted, the first section. */ 7969 if (matching_lma == NULL) 7970 matching_lma = suggested_lma; 7971 7972 map->p_paddr = matching_lma->lma * opb; 7973 7974 /* Offset the segment physical address from the lma 7975 to allow for space taken up by elf headers. */ 7976 if (map->includes_phdrs) 7977 { 7978 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; 7979 7980 /* iehdr->e_phnum is just an estimate of the number 7981 of program headers that we will need. Make a note 7982 here of the number we used and the segment we chose 7983 to hold these headers, so that we can adjust the 7984 offset when we know the correct value. */ 7985 phdr_adjust_num = iehdr->e_phnum; 7986 phdr_adjust_seg = map; 7987 } 7988 7989 if (map->includes_filehdr) 7990 { 7991 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power; 7992 map->p_paddr -= iehdr->e_ehsize; 7993 /* We've subtracted off the size of headers from the 7994 first section lma, but there may have been some 7995 alignment padding before that section too. Try to 7996 account for that by adjusting the segment lma down to 7997 the same alignment. */ 7998 if (segment->p_align != 0 && segment->p_align < align) 7999 align = segment->p_align; 8000 map->p_paddr &= -(align * opb); 8001 } 8002 } 8003 8004 /* Step Three: Loop over the sections again, this time assigning 8005 those that fit to the current segment and removing them from the 8006 sections array; but making sure not to leave large gaps. Once all 8007 possible sections have been assigned to the current segment it is 8008 added to the list of built segments and if sections still remain 8009 to be assigned, a new segment is constructed before repeating 8010 the loop. */ 8011 isec = 0; 8012 do 8013 { 8014 map->count = 0; 8015 suggested_lma = NULL; 8016 8017 /* Fill the current segment with sections that fit. */ 8018 for (j = 0; j < section_count; j++) 8019 { 8020 section = sections[j]; 8021 8022 if (section == NULL) 8023 continue; 8024 8025 output_section = section->output_section; 8026 8027 BFD_ASSERT (output_section != NULL); 8028 8029 if (is_contained_by (output_section, segment, map->p_paddr, 8030 0, opb, false) 8031 || is_note (section, segment)) 8032 { 8033 if (map->count == 0) 8034 { 8035 /* If the first section in a segment does not start at 8036 the beginning of the segment, then something is 8037 wrong. */ 8038 if (align_power (map->p_paddr 8039 + (map->includes_filehdr 8040 ? iehdr->e_ehsize : 0) 8041 + (map->includes_phdrs 8042 ? iehdr->e_phnum * iehdr->e_phentsize 8043 : 0), 8044 output_section->alignment_power * opb) 8045 != output_section->lma * opb) 8046 goto sorry; 8047 } 8048 else 8049 { 8050 asection *prev_sec; 8051 8052 prev_sec = map->sections[map->count - 1]; 8053 8054 /* If the gap between the end of the previous section 8055 and the start of this section is more than 8056 maxpagesize then we need to start a new segment. */ 8057 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, 8058 maxpagesize) 8059 < BFD_ALIGN (output_section->lma, maxpagesize)) 8060 || (prev_sec->lma + prev_sec->size 8061 > output_section->lma)) 8062 { 8063 if (suggested_lma == NULL) 8064 suggested_lma = output_section; 8065 8066 continue; 8067 } 8068 } 8069 8070 map->sections[map->count++] = output_section; 8071 ++isec; 8072 sections[j] = NULL; 8073 if (segment->p_type == PT_LOAD) 8074 section->segment_mark = true; 8075 } 8076 else if (suggested_lma == NULL) 8077 suggested_lma = output_section; 8078 } 8079 8080 /* PR 23932. A corrupt input file may contain sections that cannot 8081 be assigned to any segment - because for example they have a 8082 negative size - or segments that do not contain any sections. 8083 But there are also valid reasons why a segment can be empty. 8084 So allow a count of zero. */ 8085 8086 /* Add the current segment to the list of built segments. */ 8087 *pointer_to_map = map; 8088 pointer_to_map = &map->next; 8089 8090 if (isec < section_count) 8091 { 8092 /* We still have not allocated all of the sections to 8093 segments. Create a new segment here, initialise it 8094 and carry on looping. */ 8095 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 8096 amt += section_count * sizeof (asection *); 8097 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 8098 if (map == NULL) 8099 { 8100 free (sections); 8101 return false; 8102 } 8103 8104 /* Initialise the fields of the segment map. Set the physical 8105 physical address to the LMA of the first section that has 8106 not yet been assigned. */ 8107 map->next = NULL; 8108 map->p_type = segment->p_type; 8109 map->p_flags = segment->p_flags; 8110 map->p_flags_valid = 1; 8111 map->p_paddr = suggested_lma->lma * opb; 8112 map->p_paddr_valid = p_paddr_valid; 8113 map->includes_filehdr = 0; 8114 map->includes_phdrs = 0; 8115 } 8116 8117 continue; 8118 sorry: 8119 bfd_set_error (bfd_error_sorry); 8120 free (sections); 8121 return false; 8122 } 8123 while (isec < section_count); 8124 8125 free (sections); 8126 } 8127 8128 elf_seg_map (obfd) = map_first; 8129 8130 /* If we had to estimate the number of program headers that were 8131 going to be needed, then check our estimate now and adjust 8132 the offset if necessary. */ 8133 if (phdr_adjust_seg != NULL) 8134 { 8135 unsigned int count; 8136 8137 for (count = 0, map = map_first; map != NULL; map = map->next) 8138 count++; 8139 8140 if (count > phdr_adjust_num) 8141 phdr_adjust_seg->p_paddr 8142 -= (count - phdr_adjust_num) * iehdr->e_phentsize; 8143 8144 for (map = map_first; map != NULL; map = map->next) 8145 if (map->p_type == PT_PHDR) 8146 { 8147 bfd_vma adjust 8148 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0; 8149 map->p_paddr = phdr_adjust_seg->p_paddr + adjust; 8150 break; 8151 } 8152 } 8153 8154 #undef IS_SOLARIS_PT_INTERP 8155 #undef IS_SECTION_IN_INPUT_SEGMENT 8156 #undef INCLUDE_SECTION_IN_SEGMENT 8157 #undef SEGMENT_AFTER_SEGMENT 8158 #undef SEGMENT_OVERLAPS 8159 return true; 8160 } 8161 8162 /* Return true if p_align in the ELF program header in ABFD is valid. */ 8163 8164 static bool 8165 elf_is_p_align_valid (bfd *abfd) 8166 { 8167 unsigned int i; 8168 Elf_Internal_Phdr *segment; 8169 unsigned int num_segments; 8170 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8171 bfd_size_type maxpagesize = bed->maxpagesize; 8172 bfd_size_type p_align = bed->p_align; 8173 8174 /* Return true if the default p_align value isn't set or the maximum 8175 page size is the same as the minimum page size. */ 8176 if (p_align == 0 || maxpagesize == bed->minpagesize) 8177 return true; 8178 8179 /* When the default p_align value is set, p_align may be set to the 8180 default p_align value while segments are aligned to the maximum 8181 page size. In this case, the input p_align will be ignored and 8182 the maximum page size will be used to align the output segments. */ 8183 segment = elf_tdata (abfd)->phdr; 8184 num_segments = elf_elfheader (abfd)->e_phnum; 8185 for (i = 0; i < num_segments; i++, segment++) 8186 if (segment->p_type == PT_LOAD 8187 && (segment->p_align != p_align 8188 || vma_page_aligned_bias (segment->p_vaddr, 8189 segment->p_offset, 8190 maxpagesize) != 0)) 8191 return true; 8192 8193 return false; 8194 } 8195 8196 /* Copy ELF program header information. */ 8197 8198 static bool 8199 copy_elf_program_header (bfd *ibfd, bfd *obfd) 8200 { 8201 Elf_Internal_Ehdr *iehdr; 8202 struct elf_segment_map *map; 8203 struct elf_segment_map *map_first; 8204 struct elf_segment_map **pointer_to_map; 8205 Elf_Internal_Phdr *segment; 8206 unsigned int i; 8207 unsigned int num_segments; 8208 bool phdr_included = false; 8209 bool p_paddr_valid; 8210 bool p_palign_valid; 8211 unsigned int opb = bfd_octets_per_byte (ibfd, NULL); 8212 8213 iehdr = elf_elfheader (ibfd); 8214 8215 map_first = NULL; 8216 pointer_to_map = &map_first; 8217 8218 /* If all the segment p_paddr fields are zero, don't set 8219 map->p_paddr_valid. */ 8220 p_paddr_valid = false; 8221 num_segments = elf_elfheader (ibfd)->e_phnum; 8222 for (i = 0, segment = elf_tdata (ibfd)->phdr; 8223 i < num_segments; 8224 i++, segment++) 8225 if (segment->p_paddr != 0) 8226 { 8227 p_paddr_valid = true; 8228 break; 8229 } 8230 8231 p_palign_valid = elf_is_p_align_valid (ibfd); 8232 8233 for (i = 0, segment = elf_tdata (ibfd)->phdr; 8234 i < num_segments; 8235 i++, segment++) 8236 { 8237 asection *section; 8238 unsigned int section_count; 8239 size_t amt; 8240 Elf_Internal_Shdr *this_hdr; 8241 asection *first_section = NULL; 8242 asection *lowest_section; 8243 8244 /* Compute how many sections are in this segment. */ 8245 for (section = ibfd->sections, section_count = 0; 8246 section != NULL; 8247 section = section->next) 8248 { 8249 this_hdr = &(elf_section_data(section)->this_hdr); 8250 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 8251 { 8252 if (first_section == NULL) 8253 first_section = section; 8254 section_count++; 8255 } 8256 } 8257 8258 /* Allocate a segment map big enough to contain 8259 all of the sections we have selected. */ 8260 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 8261 amt += section_count * sizeof (asection *); 8262 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 8263 if (map == NULL) 8264 return false; 8265 8266 /* Initialize the fields of the output segment map with the 8267 input segment. */ 8268 map->next = NULL; 8269 map->p_type = segment->p_type; 8270 map->p_flags = segment->p_flags; 8271 map->p_flags_valid = 1; 8272 map->p_paddr = segment->p_paddr; 8273 map->p_paddr_valid = p_paddr_valid; 8274 map->p_align = segment->p_align; 8275 /* Keep p_align of PT_GNU_STACK for stack alignment. */ 8276 map->p_align_valid = (map->p_type == PT_GNU_STACK 8277 || p_palign_valid); 8278 map->p_vaddr_offset = 0; 8279 8280 if (map->p_type == PT_GNU_RELRO 8281 || map->p_type == PT_GNU_STACK) 8282 { 8283 /* The PT_GNU_RELRO segment may contain the first a few 8284 bytes in the .got.plt section even if the whole .got.plt 8285 section isn't in the PT_GNU_RELRO segment. We won't 8286 change the size of the PT_GNU_RELRO segment. 8287 Similarly, PT_GNU_STACK size is significant on uclinux 8288 systems. */ 8289 map->p_size = segment->p_memsz; 8290 map->p_size_valid = 1; 8291 } 8292 8293 /* Determine if this segment contains the ELF file header 8294 and if it contains the program headers themselves. */ 8295 map->includes_filehdr = (segment->p_offset == 0 8296 && segment->p_filesz >= iehdr->e_ehsize); 8297 8298 map->includes_phdrs = 0; 8299 if (! phdr_included || segment->p_type != PT_LOAD) 8300 { 8301 map->includes_phdrs = 8302 (segment->p_offset <= (bfd_vma) iehdr->e_phoff 8303 && (segment->p_offset + segment->p_filesz 8304 >= ((bfd_vma) iehdr->e_phoff 8305 + iehdr->e_phnum * iehdr->e_phentsize))); 8306 8307 if (segment->p_type == PT_LOAD && map->includes_phdrs) 8308 phdr_included = true; 8309 } 8310 8311 lowest_section = NULL; 8312 if (section_count != 0) 8313 { 8314 unsigned int isec = 0; 8315 8316 for (section = first_section; 8317 section != NULL; 8318 section = section->next) 8319 { 8320 this_hdr = &(elf_section_data(section)->this_hdr); 8321 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 8322 { 8323 map->sections[isec++] = section->output_section; 8324 if ((section->flags & SEC_ALLOC) != 0) 8325 { 8326 bfd_vma seg_off; 8327 8328 if (lowest_section == NULL 8329 || section->lma < lowest_section->lma) 8330 lowest_section = section; 8331 8332 /* Section lmas are set up from PT_LOAD header 8333 p_paddr in _bfd_elf_make_section_from_shdr. 8334 If this header has a p_paddr that disagrees 8335 with the section lma, flag the p_paddr as 8336 invalid. */ 8337 if ((section->flags & SEC_LOAD) != 0) 8338 seg_off = this_hdr->sh_offset - segment->p_offset; 8339 else 8340 seg_off = this_hdr->sh_addr - segment->p_vaddr; 8341 if (section->lma * opb - segment->p_paddr != seg_off) 8342 map->p_paddr_valid = false; 8343 } 8344 if (isec == section_count) 8345 break; 8346 } 8347 } 8348 } 8349 8350 if (section_count == 0) 8351 map->p_vaddr_offset = segment->p_vaddr / opb; 8352 else if (map->p_paddr_valid) 8353 { 8354 /* Account for padding before the first section in the segment. */ 8355 bfd_vma hdr_size = 0; 8356 if (map->includes_filehdr) 8357 hdr_size = iehdr->e_ehsize; 8358 if (map->includes_phdrs) 8359 hdr_size += iehdr->e_phnum * iehdr->e_phentsize; 8360 8361 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb 8362 - (lowest_section ? lowest_section->lma : 0)); 8363 } 8364 8365 map->count = section_count; 8366 *pointer_to_map = map; 8367 pointer_to_map = &map->next; 8368 } 8369 8370 elf_seg_map (obfd) = map_first; 8371 return true; 8372 } 8373 8374 /* Copy private BFD data. This copies or rewrites ELF program header 8375 information. */ 8376 8377 static bool 8378 copy_private_bfd_data (bfd *ibfd, bfd *obfd) 8379 { 8380 bfd_vma maxpagesize; 8381 8382 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 8383 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 8384 return true; 8385 8386 if (elf_tdata (ibfd)->phdr == NULL) 8387 return true; 8388 8389 if (ibfd->xvec == obfd->xvec) 8390 { 8391 /* Check to see if any sections in the input BFD 8392 covered by ELF program header have changed. */ 8393 Elf_Internal_Phdr *segment; 8394 asection * section; 8395 asection * osec; 8396 asection * prev; 8397 unsigned int i, num_segments; 8398 Elf_Internal_Shdr *this_hdr; 8399 const struct elf_backend_data *bed; 8400 8401 bed = get_elf_backend_data (ibfd); 8402 8403 /* Regenerate the segment map if p_paddr is set to 0. */ 8404 if (bed->want_p_paddr_set_to_zero) 8405 goto rewrite; 8406 8407 /* Initialize the segment mark field. */ 8408 for (section = obfd->sections; section != NULL; 8409 section = section->next) 8410 section->segment_mark = false; 8411 8412 num_segments = elf_elfheader (ibfd)->e_phnum; 8413 for (i = 0, segment = elf_tdata (ibfd)->phdr; 8414 i < num_segments; 8415 i++, segment++) 8416 { 8417 /* PR binutils/3535. The Solaris linker always sets the p_paddr 8418 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 8419 which severly confuses things, so always regenerate the segment 8420 map in this case. */ 8421 if (segment->p_paddr == 0 8422 && segment->p_memsz == 0 8423 && (segment->p_type == PT_INTERP 8424 || segment->p_type == PT_DYNAMIC)) 8425 goto rewrite; 8426 8427 for (section = ibfd->sections, prev = NULL; 8428 section != NULL; section = section->next) 8429 { 8430 /* We mark the output section so that we know it comes 8431 from the input BFD. */ 8432 osec = section->output_section; 8433 if (osec) 8434 osec->segment_mark = true; 8435 8436 /* Check if this section is covered by the segment. */ 8437 this_hdr = &(elf_section_data(section)->this_hdr); 8438 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 8439 { 8440 /* FIXME: Check if its output section is changed or 8441 removed. What else do we need to check? */ 8442 if (osec == NULL 8443 || section->flags != osec->flags 8444 || section->lma != osec->lma 8445 || section->vma != osec->vma 8446 || section->size != osec->size 8447 || section->rawsize != osec->rawsize 8448 || section->alignment_power != osec->alignment_power) 8449 goto rewrite; 8450 8451 /* PR 31450: If this is an allocated section then make sure 8452 that this section's vma to lma relationship is the same 8453 as previous (allocated) section's. */ 8454 if (prev != NULL 8455 && section->flags & SEC_ALLOC 8456 && section->lma - section->vma != prev->lma - prev->vma) 8457 goto rewrite; 8458 8459 if (section->flags & SEC_ALLOC) 8460 prev = section; 8461 } 8462 } 8463 } 8464 8465 /* Check to see if any output section do not come from the 8466 input BFD. */ 8467 for (section = obfd->sections; section != NULL; 8468 section = section->next) 8469 { 8470 if (!section->segment_mark) 8471 goto rewrite; 8472 else 8473 section->segment_mark = false; 8474 } 8475 8476 return copy_elf_program_header (ibfd, obfd); 8477 } 8478 8479 rewrite: 8480 maxpagesize = 0; 8481 if (ibfd->xvec == obfd->xvec) 8482 { 8483 /* When rewriting program header, set the output maxpagesize to 8484 the maximum alignment of input PT_LOAD segments. */ 8485 Elf_Internal_Phdr *segment; 8486 unsigned int i; 8487 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; 8488 8489 for (i = 0, segment = elf_tdata (ibfd)->phdr; 8490 i < num_segments; 8491 i++, segment++) 8492 if (segment->p_type == PT_LOAD 8493 && maxpagesize < segment->p_align) 8494 { 8495 /* PR 17512: file: f17299af. */ 8496 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) 8497 /* xgettext:c-format */ 8498 _bfd_error_handler (_("%pB: warning: segment alignment of %#" 8499 PRIx64 " is too large"), 8500 ibfd, (uint64_t) segment->p_align); 8501 else 8502 maxpagesize = segment->p_align; 8503 } 8504 } 8505 if (maxpagesize == 0) 8506 maxpagesize = get_elf_backend_data (obfd)->maxpagesize; 8507 8508 return rewrite_elf_program_header (ibfd, obfd, maxpagesize); 8509 } 8510 8511 /* Initialize private output section information from input section. */ 8512 8513 bool 8514 _bfd_elf_init_private_section_data (bfd *ibfd, 8515 asection *isec, 8516 bfd *obfd, 8517 asection *osec, 8518 struct bfd_link_info *link_info) 8519 8520 { 8521 Elf_Internal_Shdr *ihdr, *ohdr; 8522 bool final_link = (link_info != NULL 8523 && !bfd_link_relocatable (link_info)); 8524 8525 if (ibfd->xvec->flavour != bfd_target_elf_flavour 8526 || obfd->xvec->flavour != bfd_target_elf_flavour) 8527 return true; 8528 8529 BFD_ASSERT (elf_section_data (osec) != NULL); 8530 8531 /* If this is a known ABI section, ELF section type and flags may 8532 have been set up when OSEC was created. For normal sections we 8533 allow the user to override the type and flags other than 8534 SHF_MASKOS and SHF_MASKPROC. */ 8535 if (elf_section_type (osec) == SHT_PROGBITS 8536 || elf_section_type (osec) == SHT_NOTE 8537 || elf_section_type (osec) == SHT_NOBITS) 8538 elf_section_type (osec) = SHT_NULL; 8539 /* For objcopy and relocatable link, copy the ELF section type from 8540 the input file if the BFD section flags are the same. (If they 8541 are different the user may be doing something like 8542 "objcopy --set-section-flags .text=alloc,data".) For a final 8543 link allow some flags that the linker clears to differ. */ 8544 if (elf_section_type (osec) == SHT_NULL 8545 && (osec->flags == isec->flags 8546 || (final_link 8547 && ((osec->flags ^ isec->flags) 8548 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) 8549 elf_section_type (osec) = elf_section_type (isec); 8550 8551 /* FIXME: Is this correct for all OS/PROC specific flags? */ 8552 elf_section_flags (osec) = (elf_section_flags (isec) 8553 & (SHF_MASKOS | SHF_MASKPROC)); 8554 8555 /* Copy sh_info from input for mbind section. */ 8556 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0 8557 && elf_section_flags (isec) & SHF_GNU_MBIND) 8558 elf_section_data (osec)->this_hdr.sh_info 8559 = elf_section_data (isec)->this_hdr.sh_info; 8560 8561 /* Set things up for objcopy and relocatable link. The output 8562 SHT_GROUP section will have its elf_next_in_group pointing back 8563 to the input group members. Ignore linker created group section. 8564 See elfNN_ia64_object_p in elfxx-ia64.c. */ 8565 if ((link_info == NULL 8566 || !link_info->resolve_section_groups) 8567 && (elf_sec_group (isec) == NULL 8568 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)) 8569 { 8570 if (elf_section_flags (isec) & SHF_GROUP) 8571 elf_section_flags (osec) |= SHF_GROUP; 8572 elf_next_in_group (osec) = elf_next_in_group (isec); 8573 elf_section_data (osec)->group = elf_section_data (isec)->group; 8574 } 8575 8576 /* If not decompress, preserve SHF_COMPRESSED. */ 8577 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0) 8578 elf_section_flags (osec) |= (elf_section_flags (isec) 8579 & SHF_COMPRESSED); 8580 8581 ihdr = &elf_section_data (isec)->this_hdr; 8582 8583 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We 8584 don't use the output section of the linked-to section since it 8585 may be NULL at this point. */ 8586 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) 8587 { 8588 ohdr = &elf_section_data (osec)->this_hdr; 8589 ohdr->sh_flags |= SHF_LINK_ORDER; 8590 elf_linked_to_section (osec) = elf_linked_to_section (isec); 8591 } 8592 8593 osec->use_rela_p = isec->use_rela_p; 8594 8595 return true; 8596 } 8597 8598 /* Copy private section information. This copies over the entsize 8599 field, and sometimes the info field. */ 8600 8601 bool 8602 _bfd_elf_copy_private_section_data (bfd *ibfd, 8603 asection *isec, 8604 bfd *obfd, 8605 asection *osec) 8606 { 8607 Elf_Internal_Shdr *ihdr, *ohdr; 8608 8609 if (ibfd->xvec->flavour != bfd_target_elf_flavour 8610 || obfd->xvec->flavour != bfd_target_elf_flavour) 8611 return true; 8612 8613 ihdr = &elf_section_data (isec)->this_hdr; 8614 ohdr = &elf_section_data (osec)->this_hdr; 8615 8616 ohdr->sh_entsize = ihdr->sh_entsize; 8617 8618 if (ihdr->sh_type == SHT_SYMTAB 8619 || ihdr->sh_type == SHT_DYNSYM 8620 || ihdr->sh_type == SHT_GNU_verneed 8621 || ihdr->sh_type == SHT_GNU_verdef) 8622 ohdr->sh_info = ihdr->sh_info; 8623 8624 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, 8625 NULL); 8626 } 8627 8628 /* Look at all the SHT_GROUP sections in IBFD, making any adjustments 8629 necessary if we are removing either the SHT_GROUP section or any of 8630 the group member sections. DISCARDED is the value that a section's 8631 output_section has if the section will be discarded, NULL when this 8632 function is called from objcopy, bfd_abs_section_ptr when called 8633 from the linker. */ 8634 8635 bool 8636 _bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) 8637 { 8638 asection *isec; 8639 8640 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 8641 if (elf_section_type (isec) == SHT_GROUP) 8642 { 8643 asection *first = elf_next_in_group (isec); 8644 asection *s = first; 8645 bfd_size_type removed = 0; 8646 8647 while (s != NULL) 8648 { 8649 /* If this member section is being output but the 8650 SHT_GROUP section is not, then clear the group info 8651 set up by _bfd_elf_copy_private_section_data. */ 8652 if (s->output_section != discarded 8653 && isec->output_section == discarded) 8654 { 8655 elf_section_flags (s->output_section) &= ~SHF_GROUP; 8656 elf_group_name (s->output_section) = NULL; 8657 } 8658 else 8659 { 8660 struct bfd_elf_section_data *elf_sec = elf_section_data (s); 8661 if (s->output_section == discarded 8662 && isec->output_section != discarded) 8663 { 8664 /* Conversely, if the member section is not being 8665 output but the SHT_GROUP section is, then adjust 8666 its size. */ 8667 removed += 4; 8668 if (elf_sec->rel.hdr != NULL 8669 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0) 8670 removed += 4; 8671 if (elf_sec->rela.hdr != NULL 8672 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0) 8673 removed += 4; 8674 } 8675 else 8676 { 8677 /* Also adjust for zero-sized relocation member 8678 section. */ 8679 if (elf_sec->rel.hdr != NULL 8680 && elf_sec->rel.hdr->sh_size == 0) 8681 removed += 4; 8682 if (elf_sec->rela.hdr != NULL 8683 && elf_sec->rela.hdr->sh_size == 0) 8684 removed += 4; 8685 } 8686 } 8687 s = elf_next_in_group (s); 8688 if (s == first) 8689 break; 8690 } 8691 if (removed != 0) 8692 { 8693 if (discarded != NULL) 8694 { 8695 /* If we've been called for ld -r, then we need to 8696 adjust the input section size. */ 8697 if (isec->rawsize == 0) 8698 isec->rawsize = isec->size; 8699 isec->size = isec->rawsize - removed; 8700 if (isec->size <= 4) 8701 { 8702 isec->size = 0; 8703 isec->flags |= SEC_EXCLUDE; 8704 } 8705 } 8706 else if (isec->output_section != NULL) 8707 { 8708 /* Adjust the output section size when called from 8709 objcopy. */ 8710 isec->output_section->size -= removed; 8711 if (isec->output_section->size <= 4) 8712 { 8713 isec->output_section->size = 0; 8714 isec->output_section->flags |= SEC_EXCLUDE; 8715 } 8716 } 8717 } 8718 } 8719 8720 return true; 8721 } 8722 8723 /* Copy private header information. */ 8724 8725 bool 8726 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) 8727 { 8728 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 8729 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 8730 return true; 8731 8732 /* Copy over private BFD data if it has not already been copied. 8733 This must be done here, rather than in the copy_private_bfd_data 8734 entry point, because the latter is called after the section 8735 contents have been set, which means that the program headers have 8736 already been worked out. */ 8737 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) 8738 { 8739 if (! copy_private_bfd_data (ibfd, obfd)) 8740 return false; 8741 } 8742 8743 return _bfd_elf_fixup_group_sections (ibfd, NULL); 8744 } 8745 8746 /* Copy private symbol information. If this symbol is in a section 8747 which we did not map into a BFD section, try to map the section 8748 index correctly. We use special macro definitions for the mapped 8749 section indices; these definitions are interpreted by the 8750 swap_out_syms function. */ 8751 8752 #define MAP_ONESYMTAB (SHN_HIOS + 1) 8753 #define MAP_DYNSYMTAB (SHN_HIOS + 2) 8754 #define MAP_STRTAB (SHN_HIOS + 3) 8755 #define MAP_SHSTRTAB (SHN_HIOS + 4) 8756 #define MAP_SYM_SHNDX (SHN_HIOS + 5) 8757 8758 bool 8759 _bfd_elf_copy_private_symbol_data (bfd *ibfd, 8760 asymbol *isymarg, 8761 bfd *obfd, 8762 asymbol *osymarg) 8763 { 8764 elf_symbol_type *isym, *osym; 8765 8766 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 8767 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 8768 return true; 8769 8770 isym = elf_symbol_from (isymarg); 8771 osym = elf_symbol_from (osymarg); 8772 8773 if (isym != NULL 8774 && isym->internal_elf_sym.st_shndx != 0 8775 && osym != NULL 8776 && bfd_is_abs_section (isym->symbol.section)) 8777 { 8778 unsigned int shndx; 8779 8780 shndx = isym->internal_elf_sym.st_shndx; 8781 if (shndx == elf_onesymtab (ibfd)) 8782 shndx = MAP_ONESYMTAB; 8783 else if (shndx == elf_dynsymtab (ibfd)) 8784 shndx = MAP_DYNSYMTAB; 8785 else if (shndx == elf_elfsections (ibfd)[elf_onesymtab (ibfd)]->sh_link) 8786 shndx = MAP_STRTAB; 8787 else if (shndx == elf_elfheader (ibfd)->e_shstrndx) 8788 shndx = MAP_SHSTRTAB; 8789 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) 8790 shndx = MAP_SYM_SHNDX; 8791 osym->internal_elf_sym.st_shndx = shndx; 8792 } 8793 8794 return true; 8795 } 8796 8797 /* Swap out the symbols. */ 8798 8799 static bool 8800 swap_out_syms (bfd *abfd, 8801 struct elf_strtab_hash **sttp, 8802 int relocatable_p, 8803 struct bfd_link_info *info) 8804 { 8805 const struct elf_backend_data *bed; 8806 unsigned int symcount; 8807 asymbol **syms; 8808 struct elf_strtab_hash *stt; 8809 Elf_Internal_Shdr *symtab_hdr; 8810 Elf_Internal_Shdr *symtab_shndx_hdr; 8811 Elf_Internal_Shdr *symstrtab_hdr; 8812 struct elf_sym_strtab *symstrtab; 8813 bfd_byte *outbound_syms; 8814 bfd_byte *outbound_shndx; 8815 unsigned long outbound_syms_index; 8816 unsigned int idx; 8817 unsigned int num_locals; 8818 size_t amt; 8819 bool name_local_sections; 8820 8821 if (!elf_map_symbols (abfd, &num_locals)) 8822 return false; 8823 8824 /* Dump out the symtabs. */ 8825 stt = _bfd_elf_strtab_init (); 8826 if (stt == NULL) 8827 return false; 8828 8829 bed = get_elf_backend_data (abfd); 8830 symcount = bfd_get_symcount (abfd); 8831 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 8832 symtab_hdr->sh_type = SHT_SYMTAB; 8833 symtab_hdr->sh_entsize = bed->s->sizeof_sym; 8834 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); 8835 symtab_hdr->sh_info = num_locals + 1; 8836 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 8837 8838 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; 8839 symstrtab_hdr->sh_type = SHT_STRTAB; 8840 8841 /* Allocate buffer to swap out the .strtab section. */ 8842 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt) 8843 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL) 8844 { 8845 bfd_set_error (bfd_error_no_memory); 8846 _bfd_elf_strtab_free (stt); 8847 return false; 8848 } 8849 8850 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt) 8851 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL) 8852 { 8853 error_no_mem: 8854 bfd_set_error (bfd_error_no_memory); 8855 error_return: 8856 free (symstrtab); 8857 _bfd_elf_strtab_free (stt); 8858 return false; 8859 } 8860 symtab_hdr->contents = outbound_syms; 8861 outbound_syms_index = 0; 8862 8863 outbound_shndx = NULL; 8864 8865 if (elf_symtab_shndx_list (abfd)) 8866 { 8867 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; 8868 if (symtab_shndx_hdr->sh_name != 0) 8869 { 8870 if (_bfd_mul_overflow (symcount + 1, 8871 sizeof (Elf_External_Sym_Shndx), &amt)) 8872 goto error_no_mem; 8873 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt); 8874 if (outbound_shndx == NULL) 8875 goto error_return; 8876 8877 symtab_shndx_hdr->contents = outbound_shndx; 8878 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 8879 symtab_shndx_hdr->sh_size = amt; 8880 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 8881 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 8882 } 8883 /* FIXME: What about any other headers in the list ? */ 8884 } 8885 8886 /* Now generate the data (for "contents"). */ 8887 { 8888 /* Fill in zeroth symbol and swap it out. */ 8889 Elf_Internal_Sym sym; 8890 sym.st_name = 0; 8891 sym.st_value = 0; 8892 sym.st_size = 0; 8893 sym.st_info = 0; 8894 sym.st_other = 0; 8895 sym.st_shndx = SHN_UNDEF; 8896 sym.st_target_internal = 0; 8897 symstrtab[outbound_syms_index].sym = sym; 8898 symstrtab[outbound_syms_index].dest_index = outbound_syms_index; 8899 outbound_syms_index++; 8900 } 8901 8902 name_local_sections 8903 = (bed->elf_backend_name_local_section_symbols 8904 && bed->elf_backend_name_local_section_symbols (abfd)); 8905 8906 syms = bfd_get_outsymbols (abfd); 8907 for (idx = 0; idx < symcount; idx++) 8908 { 8909 Elf_Internal_Sym sym; 8910 8911 flagword flags = syms[idx]->flags; 8912 if (!name_local_sections 8913 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) 8914 { 8915 /* Local section symbols have no name. */ 8916 sym.st_name = (unsigned long) -1; 8917 } 8918 else 8919 { 8920 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize 8921 to get the final offset for st_name. */ 8922 sym.st_name 8923 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, 8924 false); 8925 if (sym.st_name == (unsigned long) -1) 8926 goto error_return; 8927 } 8928 8929 bfd_vma value = syms[idx]->value; 8930 elf_symbol_type *type_ptr = elf_symbol_from (syms[idx]); 8931 asection *sec = syms[idx]->section; 8932 8933 if ((flags & BSF_SECTION_SYM) == 0 && bfd_is_com_section (sec)) 8934 { 8935 /* ELF common symbols put the alignment into the `value' field, 8936 and the size into the `size' field. This is backwards from 8937 how BFD handles it, so reverse it here. */ 8938 sym.st_size = value; 8939 if (type_ptr == NULL 8940 || type_ptr->internal_elf_sym.st_value == 0) 8941 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); 8942 else 8943 sym.st_value = type_ptr->internal_elf_sym.st_value; 8944 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 8945 } 8946 else 8947 { 8948 unsigned int shndx; 8949 8950 if (sec->output_section) 8951 { 8952 value += sec->output_offset; 8953 sec = sec->output_section; 8954 } 8955 8956 /* Don't add in the section vma for relocatable output. */ 8957 if (! relocatable_p) 8958 value += sec->vma; 8959 sym.st_value = value; 8960 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; 8961 8962 if (bfd_is_abs_section (sec) 8963 && type_ptr != NULL 8964 && type_ptr->internal_elf_sym.st_shndx != 0) 8965 { 8966 /* This symbol is in a real ELF section which we did 8967 not create as a BFD section. Undo the mapping done 8968 by copy_private_symbol_data. */ 8969 shndx = type_ptr->internal_elf_sym.st_shndx; 8970 switch (shndx) 8971 { 8972 case MAP_ONESYMTAB: 8973 shndx = elf_onesymtab (abfd); 8974 break; 8975 case MAP_DYNSYMTAB: 8976 shndx = elf_dynsymtab (abfd); 8977 break; 8978 case MAP_STRTAB: 8979 shndx = elf_strtab_sec (abfd); 8980 break; 8981 case MAP_SHSTRTAB: 8982 shndx = elf_shstrtab_sec (abfd); 8983 break; 8984 case MAP_SYM_SHNDX: 8985 if (elf_symtab_shndx_list (abfd)) 8986 shndx = elf_symtab_shndx_list (abfd)->ndx; 8987 break; 8988 case SHN_COMMON: 8989 case SHN_ABS: 8990 shndx = SHN_ABS; 8991 break; 8992 default: 8993 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS) 8994 { 8995 if (bed->symbol_section_index) 8996 shndx = bed->symbol_section_index (abfd, type_ptr); 8997 /* Otherwise just leave the index alone. */ 8998 } 8999 else 9000 { 9001 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE) 9002 _bfd_error_handler (_("%pB: \ 9003 Unable to handle section index %x in ELF symbol. Using ABS instead."), 9004 abfd, shndx); 9005 shndx = SHN_ABS; 9006 } 9007 break; 9008 } 9009 } 9010 else 9011 { 9012 shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 9013 9014 if (shndx == SHN_BAD) 9015 { 9016 asection *sec2; 9017 9018 /* Writing this would be a hell of a lot easier if 9019 we had some decent documentation on bfd, and 9020 knew what to expect of the library, and what to 9021 demand of applications. For example, it 9022 appears that `objcopy' might not set the 9023 section of a symbol to be a section that is 9024 actually in the output file. */ 9025 sec2 = bfd_get_section_by_name (abfd, sec->name); 9026 if (sec2 != NULL) 9027 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); 9028 if (shndx == SHN_BAD) 9029 { 9030 /* xgettext:c-format */ 9031 _bfd_error_handler 9032 (_("unable to find equivalent output section" 9033 " for symbol '%s' from section '%s'"), 9034 syms[idx]->name ? syms[idx]->name : "<Local sym>", 9035 sec->name); 9036 bfd_set_error (bfd_error_invalid_operation); 9037 goto error_return; 9038 } 9039 } 9040 } 9041 9042 sym.st_shndx = shndx; 9043 } 9044 9045 int type; 9046 if ((flags & BSF_THREAD_LOCAL) != 0) 9047 type = STT_TLS; 9048 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) 9049 type = STT_GNU_IFUNC; 9050 else if ((flags & BSF_FUNCTION) != 0) 9051 type = STT_FUNC; 9052 else if ((flags & BSF_OBJECT) != 0) 9053 type = STT_OBJECT; 9054 else if ((flags & BSF_RELC) != 0) 9055 type = STT_RELC; 9056 else if ((flags & BSF_SRELC) != 0) 9057 type = STT_SRELC; 9058 else 9059 type = STT_NOTYPE; 9060 9061 if (syms[idx]->section->flags & SEC_THREAD_LOCAL) 9062 type = STT_TLS; 9063 9064 /* Processor-specific types. */ 9065 if (type_ptr != NULL 9066 && bed->elf_backend_get_symbol_type) 9067 type = ((*bed->elf_backend_get_symbol_type) 9068 (&type_ptr->internal_elf_sym, type)); 9069 9070 if (flags & BSF_SECTION_SYM) 9071 { 9072 if (flags & BSF_GLOBAL) 9073 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); 9074 else 9075 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 9076 } 9077 else if (bfd_is_com_section (syms[idx]->section)) 9078 { 9079 if (type != STT_TLS) 9080 { 9081 if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) 9082 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) 9083 ? STT_COMMON : STT_OBJECT); 9084 else 9085 type = ((flags & BSF_ELF_COMMON) != 0 9086 ? STT_COMMON : STT_OBJECT); 9087 } 9088 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); 9089 } 9090 else if (bfd_is_und_section (syms[idx]->section)) 9091 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) 9092 ? STB_WEAK 9093 : STB_GLOBAL), 9094 type); 9095 else if (flags & BSF_FILE) 9096 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 9097 else 9098 { 9099 int bind = STB_LOCAL; 9100 9101 if (flags & BSF_LOCAL) 9102 bind = STB_LOCAL; 9103 else if (flags & BSF_GNU_UNIQUE) 9104 bind = STB_GNU_UNIQUE; 9105 else if (flags & BSF_WEAK) 9106 bind = STB_WEAK; 9107 else if (flags & BSF_GLOBAL) 9108 bind = STB_GLOBAL; 9109 9110 sym.st_info = ELF_ST_INFO (bind, type); 9111 } 9112 9113 if (type_ptr != NULL) 9114 { 9115 sym.st_other = type_ptr->internal_elf_sym.st_other; 9116 sym.st_target_internal 9117 = type_ptr->internal_elf_sym.st_target_internal; 9118 } 9119 else 9120 { 9121 sym.st_other = 0; 9122 sym.st_target_internal = 0; 9123 } 9124 9125 symstrtab[outbound_syms_index].sym = sym; 9126 symstrtab[outbound_syms_index].dest_index = outbound_syms_index; 9127 outbound_syms_index++; 9128 } 9129 9130 /* Finalize the .strtab section. */ 9131 _bfd_elf_strtab_finalize (stt); 9132 9133 /* Swap out the .strtab section. */ 9134 for (idx = 0; idx < outbound_syms_index; idx++) 9135 { 9136 struct elf_sym_strtab *elfsym = &symstrtab[idx]; 9137 if (elfsym->sym.st_name == (unsigned long) -1) 9138 elfsym->sym.st_name = 0; 9139 else 9140 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, 9141 elfsym->sym.st_name); 9142 if (info && info->callbacks->ctf_new_symbol) 9143 info->callbacks->ctf_new_symbol (elfsym->dest_index, 9144 &elfsym->sym); 9145 9146 /* Inform the linker of the addition of this symbol. */ 9147 9148 bed->s->swap_symbol_out (abfd, &elfsym->sym, 9149 (outbound_syms 9150 + (elfsym->dest_index 9151 * bed->s->sizeof_sym)), 9152 NPTR_ADD (outbound_shndx, 9153 (elfsym->dest_index 9154 * sizeof (Elf_External_Sym_Shndx)))); 9155 } 9156 free (symstrtab); 9157 9158 *sttp = stt; 9159 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); 9160 symstrtab_hdr->sh_type = SHT_STRTAB; 9161 symstrtab_hdr->sh_flags = bed->elf_strtab_flags; 9162 symstrtab_hdr->sh_addr = 0; 9163 symstrtab_hdr->sh_entsize = 0; 9164 symstrtab_hdr->sh_link = 0; 9165 symstrtab_hdr->sh_info = 0; 9166 symstrtab_hdr->sh_addralign = 1; 9167 9168 return true; 9169 } 9170 9171 /* Return the number of bytes required to hold the symtab vector. 9172 9173 Note that we base it on the count plus 1, since we will null terminate 9174 the vector allocated based on this size. However, the ELF symbol table 9175 always has a dummy entry as symbol #0, so it ends up even. */ 9176 9177 long 9178 _bfd_elf_get_symtab_upper_bound (bfd *abfd) 9179 { 9180 bfd_size_type symcount; 9181 long symtab_size; 9182 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; 9183 9184 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 9185 if (symcount > LONG_MAX / sizeof (asymbol *)) 9186 { 9187 bfd_set_error (bfd_error_file_too_big); 9188 return -1; 9189 } 9190 symtab_size = symcount * (sizeof (asymbol *)); 9191 if (symcount == 0) 9192 symtab_size = sizeof (asymbol *); 9193 else if (!bfd_write_p (abfd)) 9194 { 9195 ufile_ptr filesize = bfd_get_file_size (abfd); 9196 9197 if (filesize != 0 && (unsigned long) symtab_size > filesize) 9198 { 9199 bfd_set_error (bfd_error_file_truncated); 9200 return -1; 9201 } 9202 } 9203 9204 return symtab_size; 9205 } 9206 9207 long 9208 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) 9209 { 9210 bfd_size_type symcount; 9211 long symtab_size; 9212 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; 9213 9214 if (elf_dynsymtab (abfd) == 0) 9215 { 9216 /* Check if there is dynamic symbol table. */ 9217 symcount = elf_tdata (abfd)->dt_symtab_count; 9218 if (symcount) 9219 goto compute_symtab_size; 9220 9221 bfd_set_error (bfd_error_invalid_operation); 9222 return -1; 9223 } 9224 9225 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 9226 if (symcount > LONG_MAX / sizeof (asymbol *)) 9227 { 9228 bfd_set_error (bfd_error_file_too_big); 9229 return -1; 9230 } 9231 9232 compute_symtab_size: 9233 symtab_size = symcount * (sizeof (asymbol *)); 9234 if (symcount == 0) 9235 symtab_size = sizeof (asymbol *); 9236 else if (!bfd_write_p (abfd)) 9237 { 9238 ufile_ptr filesize = bfd_get_file_size (abfd); 9239 9240 if (filesize != 0 && (unsigned long) symtab_size > filesize) 9241 { 9242 bfd_set_error (bfd_error_file_truncated); 9243 return -1; 9244 } 9245 } 9246 9247 return symtab_size; 9248 } 9249 9250 long 9251 _bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect) 9252 { 9253 if (asect->reloc_count != 0 && !bfd_write_p (abfd)) 9254 { 9255 /* Sanity check reloc section size. */ 9256 ufile_ptr filesize = bfd_get_file_size (abfd); 9257 9258 if (filesize != 0) 9259 { 9260 struct bfd_elf_section_data *d = elf_section_data (asect); 9261 bfd_size_type rel_size = d->rel.hdr ? d->rel.hdr->sh_size : 0; 9262 bfd_size_type rela_size = d->rela.hdr ? d->rela.hdr->sh_size : 0; 9263 9264 if (rel_size + rela_size > filesize 9265 || rel_size + rela_size < rel_size) 9266 { 9267 bfd_set_error (bfd_error_file_truncated); 9268 return -1; 9269 } 9270 } 9271 } 9272 9273 #if SIZEOF_LONG == SIZEOF_INT 9274 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *)) 9275 { 9276 bfd_set_error (bfd_error_file_too_big); 9277 return -1; 9278 } 9279 #endif 9280 return (asect->reloc_count + 1L) * sizeof (arelent *); 9281 } 9282 9283 /* Canonicalize the relocs. */ 9284 9285 long 9286 _bfd_elf_canonicalize_reloc (bfd *abfd, 9287 sec_ptr section, 9288 arelent **relptr, 9289 asymbol **symbols) 9290 { 9291 arelent *tblptr; 9292 unsigned int i; 9293 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9294 9295 if (! bed->s->slurp_reloc_table (abfd, section, symbols, false)) 9296 return -1; 9297 9298 tblptr = section->relocation; 9299 for (i = 0; i < section->reloc_count; i++) 9300 *relptr++ = tblptr++; 9301 9302 *relptr = NULL; 9303 9304 return section->reloc_count; 9305 } 9306 9307 long 9308 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) 9309 { 9310 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9311 long symcount = bed->s->slurp_symbol_table (abfd, allocation, false); 9312 9313 if (symcount >= 0) 9314 abfd->symcount = symcount; 9315 return symcount; 9316 } 9317 9318 long 9319 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, 9320 asymbol **allocation) 9321 { 9322 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9323 long symcount = bed->s->slurp_symbol_table (abfd, allocation, true); 9324 9325 if (symcount >= 0) 9326 abfd->dynsymcount = symcount; 9327 return symcount; 9328 } 9329 9330 /* Return the size required for the dynamic reloc entries. Any loadable 9331 section that was actually installed in the BFD, and has type SHT_REL 9332 or SHT_RELA, and uses the dynamic symbol table, is considered to be a 9333 dynamic reloc section. */ 9334 9335 long 9336 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) 9337 { 9338 bfd_size_type count, ext_rel_size; 9339 asection *s; 9340 9341 if (elf_dynsymtab (abfd) == 0) 9342 { 9343 bfd_set_error (bfd_error_invalid_operation); 9344 return -1; 9345 } 9346 9347 count = 1; 9348 ext_rel_size = 0; 9349 for (s = abfd->sections; s != NULL; s = s->next) 9350 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 9351 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 9352 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA) 9353 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0) 9354 { 9355 ext_rel_size += elf_section_data (s)->this_hdr.sh_size; 9356 if (ext_rel_size < elf_section_data (s)->this_hdr.sh_size) 9357 { 9358 bfd_set_error (bfd_error_file_truncated); 9359 return -1; 9360 } 9361 count += NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr); 9362 if (count > LONG_MAX / sizeof (arelent *)) 9363 { 9364 bfd_set_error (bfd_error_file_too_big); 9365 return -1; 9366 } 9367 } 9368 if (count > 1 && !bfd_write_p (abfd)) 9369 { 9370 /* Sanity check reloc section sizes. */ 9371 ufile_ptr filesize = bfd_get_file_size (abfd); 9372 if (filesize != 0 && ext_rel_size > filesize) 9373 { 9374 bfd_set_error (bfd_error_file_truncated); 9375 return -1; 9376 } 9377 } 9378 return count * sizeof (arelent *); 9379 } 9380 9381 /* Canonicalize the dynamic relocation entries. Note that we return the 9382 dynamic relocations as a single block, although they are actually 9383 associated with particular sections; the interface, which was 9384 designed for SunOS style shared libraries, expects that there is only 9385 one set of dynamic relocs. Any loadable section that was actually 9386 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the 9387 dynamic symbol table, is considered to be a dynamic reloc section. */ 9388 9389 long 9390 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, 9391 arelent **storage, 9392 asymbol **syms) 9393 { 9394 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool); 9395 asection *s; 9396 long ret; 9397 9398 if (elf_dynsymtab (abfd) == 0) 9399 { 9400 bfd_set_error (bfd_error_invalid_operation); 9401 return -1; 9402 } 9403 9404 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; 9405 ret = 0; 9406 for (s = abfd->sections; s != NULL; s = s->next) 9407 { 9408 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 9409 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 9410 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA) 9411 && (elf_section_data (s)->this_hdr.sh_flags & SHF_COMPRESSED) == 0) 9412 { 9413 arelent *p; 9414 long count, i; 9415 9416 if (! (*slurp_relocs) (abfd, s, syms, true)) 9417 return -1; 9418 count = NUM_SHDR_ENTRIES (&elf_section_data (s)->this_hdr); 9419 p = s->relocation; 9420 for (i = 0; i < count; i++) 9421 *storage++ = p++; 9422 ret += count; 9423 } 9424 } 9425 9426 *storage = NULL; 9427 9428 return ret; 9429 } 9430 9431 /* Read in the version information. */ 9432 9433 bool 9434 _bfd_elf_slurp_version_tables (bfd *abfd, bool default_imported_symver) 9435 { 9436 bfd_byte *contents = NULL; 9437 unsigned int freeidx = 0; 9438 size_t amt; 9439 void *contents_addr = NULL; 9440 size_t contents_size = 0; 9441 9442 if (elf_dynverref (abfd) != 0 || elf_tdata (abfd)->dt_verneed != NULL) 9443 { 9444 Elf_Internal_Shdr *hdr; 9445 Elf_External_Verneed *everneed; 9446 Elf_Internal_Verneed *iverneed; 9447 unsigned int i; 9448 bfd_byte *contents_end; 9449 size_t verneed_count; 9450 size_t verneed_size; 9451 9452 if (elf_tdata (abfd)->dt_verneed != NULL) 9453 { 9454 hdr = NULL; 9455 contents = elf_tdata (abfd)->dt_verneed; 9456 verneed_count = elf_tdata (abfd)->dt_verneed_count; 9457 verneed_size = verneed_count * sizeof (Elf_External_Verneed); 9458 } 9459 else 9460 { 9461 hdr = &elf_tdata (abfd)->dynverref_hdr; 9462 9463 if (hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed)) 9464 { 9465 error_return_bad_verref: 9466 _bfd_error_handler 9467 (_("%pB: .gnu.version_r invalid entry"), abfd); 9468 bfd_set_error (bfd_error_bad_value); 9469 error_return_verref: 9470 elf_tdata (abfd)->verref = NULL; 9471 elf_tdata (abfd)->cverrefs = 0; 9472 goto error_return; 9473 } 9474 9475 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) 9476 goto error_return_verref; 9477 contents_size = hdr->sh_size; 9478 contents = _bfd_mmap_readonly_temporary (abfd, contents_size, 9479 &contents_addr, 9480 &contents_size); 9481 if (contents == NULL) 9482 goto error_return_verref; 9483 9484 verneed_size = hdr->sh_size; 9485 verneed_count = hdr->sh_info; 9486 } 9487 9488 if (_bfd_mul_overflow (verneed_count, 9489 sizeof (Elf_Internal_Verneed), &amt)) 9490 { 9491 bfd_set_error (bfd_error_file_too_big); 9492 goto error_return_verref; 9493 } 9494 if (amt == 0) 9495 goto error_return_verref; 9496 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_zalloc (abfd, amt); 9497 if (elf_tdata (abfd)->verref == NULL) 9498 goto error_return_verref; 9499 9500 BFD_ASSERT (sizeof (Elf_External_Verneed) 9501 == sizeof (Elf_External_Vernaux)); 9502 contents_end = (contents + verneed_size 9503 - sizeof (Elf_External_Verneed)); 9504 everneed = (Elf_External_Verneed *) contents; 9505 iverneed = elf_tdata (abfd)->verref; 9506 for (i = 0; i < verneed_count; i++, iverneed++) 9507 { 9508 Elf_External_Vernaux *evernaux; 9509 Elf_Internal_Vernaux *ivernaux; 9510 unsigned int j; 9511 9512 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); 9513 9514 iverneed->vn_bfd = abfd; 9515 9516 if (elf_use_dt_symtab_p (abfd)) 9517 { 9518 if (iverneed->vn_file < elf_tdata (abfd)->dt_strsz) 9519 iverneed->vn_filename 9520 = elf_tdata (abfd)->dt_strtab + iverneed->vn_file; 9521 else 9522 iverneed->vn_filename = NULL; 9523 } 9524 else if (hdr == NULL) 9525 goto error_return_bad_verref; 9526 else 9527 iverneed->vn_filename 9528 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 9529 iverneed->vn_file); 9530 if (iverneed->vn_filename == NULL) 9531 goto error_return_bad_verref; 9532 9533 if (iverneed->vn_cnt == 0) 9534 iverneed->vn_auxptr = NULL; 9535 else 9536 { 9537 if (_bfd_mul_overflow (iverneed->vn_cnt, 9538 sizeof (Elf_Internal_Vernaux), &amt)) 9539 { 9540 bfd_set_error (bfd_error_file_too_big); 9541 goto error_return_verref; 9542 } 9543 iverneed->vn_auxptr = (struct elf_internal_vernaux *) 9544 bfd_alloc (abfd, amt); 9545 if (iverneed->vn_auxptr == NULL) 9546 goto error_return_verref; 9547 } 9548 9549 if (iverneed->vn_aux 9550 > (size_t) (contents_end - (bfd_byte *) everneed)) 9551 goto error_return_bad_verref; 9552 9553 evernaux = ((Elf_External_Vernaux *) 9554 ((bfd_byte *) everneed + iverneed->vn_aux)); 9555 ivernaux = iverneed->vn_auxptr; 9556 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) 9557 { 9558 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); 9559 9560 if (elf_use_dt_symtab_p (abfd)) 9561 { 9562 if (ivernaux->vna_name < elf_tdata (abfd)->dt_strsz) 9563 ivernaux->vna_nodename 9564 = elf_tdata (abfd)->dt_strtab + ivernaux->vna_name; 9565 else 9566 ivernaux->vna_nodename = NULL; 9567 } 9568 else if (hdr == NULL) 9569 goto error_return_bad_verref; 9570 else 9571 ivernaux->vna_nodename 9572 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 9573 ivernaux->vna_name); 9574 if (ivernaux->vna_nodename == NULL) 9575 goto error_return_bad_verref; 9576 9577 if (ivernaux->vna_other > freeidx) 9578 freeidx = ivernaux->vna_other; 9579 9580 ivernaux->vna_nextptr = NULL; 9581 if (ivernaux->vna_next == 0) 9582 { 9583 iverneed->vn_cnt = j + 1; 9584 break; 9585 } 9586 if (j + 1 < iverneed->vn_cnt) 9587 ivernaux->vna_nextptr = ivernaux + 1; 9588 9589 if (ivernaux->vna_next 9590 > (size_t) (contents_end - (bfd_byte *) evernaux)) 9591 goto error_return_bad_verref; 9592 9593 evernaux = ((Elf_External_Vernaux *) 9594 ((bfd_byte *) evernaux + ivernaux->vna_next)); 9595 } 9596 9597 iverneed->vn_nextref = NULL; 9598 if (iverneed->vn_next == 0) 9599 break; 9600 if (hdr != NULL && (i + 1 < hdr->sh_info)) 9601 iverneed->vn_nextref = iverneed + 1; 9602 9603 if (iverneed->vn_next 9604 > (size_t) (contents_end - (bfd_byte *) everneed)) 9605 goto error_return_bad_verref; 9606 9607 everneed = ((Elf_External_Verneed *) 9608 ((bfd_byte *) everneed + iverneed->vn_next)); 9609 } 9610 elf_tdata (abfd)->cverrefs = i; 9611 9612 if (contents != elf_tdata (abfd)->dt_verneed) 9613 _bfd_munmap_readonly_temporary (contents_addr, contents_size); 9614 contents = NULL; 9615 contents_addr = NULL; 9616 } 9617 9618 if (elf_dynverdef (abfd) != 0 || elf_tdata (abfd)->dt_verdef != NULL) 9619 { 9620 Elf_Internal_Shdr *hdr; 9621 Elf_External_Verdef *everdef; 9622 Elf_Internal_Verdef *iverdef; 9623 Elf_Internal_Verdef *iverdefarr; 9624 Elf_Internal_Verdef iverdefmem; 9625 unsigned int i; 9626 unsigned int maxidx; 9627 bfd_byte *contents_end_def, *contents_end_aux; 9628 size_t verdef_count; 9629 size_t verdef_size; 9630 9631 if (elf_tdata (abfd)->dt_verdef != NULL) 9632 { 9633 hdr = NULL; 9634 contents = elf_tdata (abfd)->dt_verdef; 9635 verdef_count = elf_tdata (abfd)->dt_verdef_count; 9636 verdef_size = verdef_count * sizeof (Elf_External_Verdef); 9637 } 9638 else 9639 { 9640 hdr = &elf_tdata (abfd)->dynverdef_hdr; 9641 9642 if (hdr->sh_size < sizeof (Elf_External_Verdef)) 9643 { 9644 error_return_bad_verdef: 9645 _bfd_error_handler 9646 (_("%pB: .gnu.version_d invalid entry"), abfd); 9647 bfd_set_error (bfd_error_bad_value); 9648 error_return_verdef: 9649 elf_tdata (abfd)->verdef = NULL; 9650 elf_tdata (abfd)->cverdefs = 0; 9651 goto error_return; 9652 } 9653 9654 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) 9655 goto error_return_verdef; 9656 contents_size = hdr->sh_size; 9657 contents = _bfd_mmap_readonly_temporary (abfd, contents_size, 9658 &contents_addr, 9659 &contents_size); 9660 if (contents == NULL) 9661 goto error_return_verdef; 9662 9663 BFD_ASSERT (sizeof (Elf_External_Verdef) 9664 >= sizeof (Elf_External_Verdaux)); 9665 9666 verdef_count = hdr->sh_info; 9667 verdef_size = hdr->sh_size; 9668 } 9669 9670 contents_end_def = (contents + verdef_size 9671 - sizeof (Elf_External_Verdef)); 9672 contents_end_aux = (contents + verdef_size 9673 - sizeof (Elf_External_Verdaux)); 9674 9675 /* We know the number of entries in the section but not the maximum 9676 index. Therefore we have to run through all entries and find 9677 the maximum. */ 9678 everdef = (Elf_External_Verdef *) contents; 9679 maxidx = 0; 9680 for (i = 0; i < verdef_count; ++i) 9681 { 9682 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 9683 9684 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) 9685 goto error_return_bad_verdef; 9686 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) 9687 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); 9688 9689 if (iverdefmem.vd_next == 0) 9690 break; 9691 9692 if (iverdefmem.vd_next 9693 > (size_t) (contents_end_def - (bfd_byte *) everdef)) 9694 goto error_return_bad_verdef; 9695 9696 everdef = ((Elf_External_Verdef *) 9697 ((bfd_byte *) everdef + iverdefmem.vd_next)); 9698 } 9699 9700 if (default_imported_symver) 9701 { 9702 if (freeidx > maxidx) 9703 maxidx = ++freeidx; 9704 else 9705 freeidx = ++maxidx; 9706 } 9707 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt)) 9708 { 9709 bfd_set_error (bfd_error_file_too_big); 9710 goto error_return_verdef; 9711 } 9712 9713 if (amt == 0) 9714 goto error_return_verdef; 9715 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); 9716 if (elf_tdata (abfd)->verdef == NULL) 9717 goto error_return_verdef; 9718 9719 elf_tdata (abfd)->cverdefs = maxidx; 9720 9721 everdef = (Elf_External_Verdef *) contents; 9722 iverdefarr = elf_tdata (abfd)->verdef; 9723 for (i = 0; i < verdef_count; ++i) 9724 { 9725 Elf_External_Verdaux *everdaux; 9726 Elf_Internal_Verdaux *iverdaux; 9727 unsigned int j; 9728 9729 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 9730 9731 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) 9732 goto error_return_bad_verdef; 9733 9734 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; 9735 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); 9736 9737 iverdef->vd_bfd = abfd; 9738 9739 if (iverdef->vd_cnt == 0) 9740 iverdef->vd_auxptr = NULL; 9741 else 9742 { 9743 if (_bfd_mul_overflow (iverdef->vd_cnt, 9744 sizeof (Elf_Internal_Verdaux), &amt)) 9745 { 9746 bfd_set_error (bfd_error_file_too_big); 9747 goto error_return_verdef; 9748 } 9749 iverdef->vd_auxptr = (struct elf_internal_verdaux *) 9750 bfd_alloc (abfd, amt); 9751 if (iverdef->vd_auxptr == NULL) 9752 goto error_return_verdef; 9753 } 9754 9755 if (iverdef->vd_aux 9756 > (size_t) (contents_end_aux - (bfd_byte *) everdef)) 9757 goto error_return_bad_verdef; 9758 9759 everdaux = ((Elf_External_Verdaux *) 9760 ((bfd_byte *) everdef + iverdef->vd_aux)); 9761 iverdaux = iverdef->vd_auxptr; 9762 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) 9763 { 9764 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); 9765 9766 if (elf_use_dt_symtab_p (abfd)) 9767 { 9768 if (iverdaux->vda_name < elf_tdata (abfd)->dt_strsz) 9769 iverdaux->vda_nodename 9770 = elf_tdata (abfd)->dt_strtab + iverdaux->vda_name; 9771 else 9772 iverdaux->vda_nodename = NULL; 9773 } 9774 else 9775 iverdaux->vda_nodename 9776 = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 9777 iverdaux->vda_name); 9778 if (iverdaux->vda_nodename == NULL) 9779 goto error_return_bad_verdef; 9780 9781 iverdaux->vda_nextptr = NULL; 9782 if (iverdaux->vda_next == 0) 9783 { 9784 iverdef->vd_cnt = j + 1; 9785 break; 9786 } 9787 if (j + 1 < iverdef->vd_cnt) 9788 iverdaux->vda_nextptr = iverdaux + 1; 9789 9790 if (iverdaux->vda_next 9791 > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) 9792 goto error_return_bad_verdef; 9793 9794 everdaux = ((Elf_External_Verdaux *) 9795 ((bfd_byte *) everdaux + iverdaux->vda_next)); 9796 } 9797 9798 iverdef->vd_nodename = NULL; 9799 if (iverdef->vd_cnt) 9800 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; 9801 9802 iverdef->vd_nextdef = NULL; 9803 if (iverdef->vd_next == 0) 9804 break; 9805 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) 9806 iverdef->vd_nextdef = iverdef + 1; 9807 9808 everdef = ((Elf_External_Verdef *) 9809 ((bfd_byte *) everdef + iverdef->vd_next)); 9810 } 9811 9812 if (contents != elf_tdata (abfd)->dt_verdef) 9813 _bfd_munmap_readonly_temporary (contents_addr, contents_size); 9814 contents = NULL; 9815 contents_addr = NULL; 9816 } 9817 else if (default_imported_symver) 9818 { 9819 if (freeidx < 3) 9820 freeidx = 3; 9821 else 9822 freeidx++; 9823 9824 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt)) 9825 { 9826 bfd_set_error (bfd_error_file_too_big); 9827 goto error_return; 9828 } 9829 if (amt == 0) 9830 goto error_return; 9831 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); 9832 if (elf_tdata (abfd)->verdef == NULL) 9833 goto error_return; 9834 9835 elf_tdata (abfd)->cverdefs = freeidx; 9836 } 9837 9838 /* Create a default version based on the soname. */ 9839 if (default_imported_symver) 9840 { 9841 Elf_Internal_Verdef *iverdef; 9842 Elf_Internal_Verdaux *iverdaux; 9843 9844 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; 9845 9846 iverdef->vd_version = VER_DEF_CURRENT; 9847 iverdef->vd_flags = 0; 9848 iverdef->vd_ndx = freeidx; 9849 iverdef->vd_cnt = 1; 9850 9851 iverdef->vd_bfd = abfd; 9852 9853 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); 9854 if (iverdef->vd_nodename == NULL) 9855 goto error_return_verdef; 9856 iverdef->vd_nextdef = NULL; 9857 iverdef->vd_auxptr = ((struct elf_internal_verdaux *) 9858 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); 9859 if (iverdef->vd_auxptr == NULL) 9860 goto error_return_verdef; 9861 9862 iverdaux = iverdef->vd_auxptr; 9863 iverdaux->vda_nodename = iverdef->vd_nodename; 9864 } 9865 9866 return true; 9867 9868 error_return: 9869 if (contents != elf_tdata (abfd)->dt_verneed 9870 && contents != elf_tdata (abfd)->dt_verdef) 9871 _bfd_munmap_readonly_temporary (contents_addr, contents_size); 9872 return false; 9873 } 9874 9875 asymbol * 9876 _bfd_elf_make_empty_symbol (bfd *abfd) 9877 { 9878 elf_symbol_type *newsym; 9879 9880 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym)); 9881 if (!newsym) 9882 return NULL; 9883 newsym->symbol.the_bfd = abfd; 9884 return &newsym->symbol; 9885 } 9886 9887 void 9888 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, 9889 asymbol *symbol, 9890 symbol_info *ret) 9891 { 9892 bfd_symbol_info (symbol, ret); 9893 } 9894 9895 /* Return whether a symbol name implies a local symbol. Most targets 9896 use this function for the is_local_label_name entry point, but some 9897 override it. */ 9898 9899 bool 9900 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, 9901 const char *name) 9902 { 9903 /* Normal local symbols start with ``.L''. */ 9904 if (name[0] == '.' && name[1] == 'L') 9905 return true; 9906 9907 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate 9908 DWARF debugging symbols starting with ``..''. */ 9909 if (name[0] == '.' && name[1] == '.') 9910 return true; 9911 9912 /* gcc will sometimes generate symbols beginning with ``_.L_'' when 9913 emitting DWARF debugging output. I suspect this is actually a 9914 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call 9915 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading 9916 underscore to be emitted on some ELF targets). For ease of use, 9917 we treat such symbols as local. */ 9918 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') 9919 return true; 9920 9921 /* Treat assembler generated fake symbols, dollar local labels and 9922 forward-backward labels (aka local labels) as locals. 9923 These labels have the form: 9924 9925 L0^A.* (fake symbols) 9926 9927 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) 9928 9929 Versions which start with .L will have already been matched above, 9930 so we only need to match the rest. */ 9931 if (name[0] == 'L' && ISDIGIT (name[1])) 9932 { 9933 bool ret = false; 9934 const char * p; 9935 char c; 9936 9937 for (p = name + 2; (c = *p); p++) 9938 { 9939 if (c == 1 || c == 2) 9940 { 9941 if (c == 1 && p == name + 2) 9942 /* A fake symbol. */ 9943 return true; 9944 9945 /* FIXME: We are being paranoid here and treating symbols like 9946 L0^Bfoo as if there were non-local, on the grounds that the 9947 assembler will never generate them. But can any symbol 9948 containing an ASCII value in the range 1-31 ever be anything 9949 other than some kind of local ? */ 9950 ret = true; 9951 } 9952 9953 if (! ISDIGIT (c)) 9954 { 9955 ret = false; 9956 break; 9957 } 9958 } 9959 return ret; 9960 } 9961 9962 return false; 9963 } 9964 9965 alent * 9966 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, 9967 asymbol *symbol ATTRIBUTE_UNUSED) 9968 { 9969 abort (); 9970 return NULL; 9971 } 9972 9973 bool 9974 _bfd_elf_set_arch_mach (bfd *abfd, 9975 enum bfd_architecture arch, 9976 unsigned long machine) 9977 { 9978 /* If this isn't the right architecture for this backend, and this 9979 isn't the generic backend, fail. */ 9980 if (arch != get_elf_backend_data (abfd)->arch 9981 && arch != bfd_arch_unknown 9982 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) 9983 return false; 9984 9985 return bfd_default_set_arch_mach (abfd, arch, machine); 9986 } 9987 9988 /* Find the nearest line to a particular section and offset, 9989 for error reporting. */ 9990 9991 bool 9992 _bfd_elf_find_nearest_line (bfd *abfd, 9993 asymbol **symbols, 9994 asection *section, 9995 bfd_vma offset, 9996 const char **filename_ptr, 9997 const char **functionname_ptr, 9998 unsigned int *line_ptr, 9999 unsigned int *discriminator_ptr) 10000 { 10001 return _bfd_elf_find_nearest_line_with_alt (abfd, NULL, symbols, section, 10002 offset, filename_ptr, 10003 functionname_ptr, line_ptr, 10004 discriminator_ptr); 10005 } 10006 10007 /* Find the nearest line to a particular section and offset, 10008 for error reporting. ALT_BFD representing a .gnu_debugaltlink file 10009 can be optionally specified. */ 10010 10011 bool 10012 _bfd_elf_find_nearest_line_with_alt (bfd *abfd, 10013 const char *alt_filename, 10014 asymbol **symbols, 10015 asection *section, 10016 bfd_vma offset, 10017 const char **filename_ptr, 10018 const char **functionname_ptr, 10019 unsigned int *line_ptr, 10020 unsigned int *discriminator_ptr) 10021 { 10022 bool found; 10023 10024 if (_bfd_dwarf2_find_nearest_line_with_alt (abfd, alt_filename, symbols, NULL, 10025 section, offset, filename_ptr, 10026 functionname_ptr, line_ptr, 10027 discriminator_ptr, 10028 dwarf_debug_sections, 10029 &elf_tdata (abfd)->dwarf2_find_line_info)) 10030 return true; 10031 10032 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, 10033 filename_ptr, functionname_ptr, line_ptr)) 10034 { 10035 if (!*functionname_ptr) 10036 _bfd_elf_find_function (abfd, symbols, section, offset, 10037 *filename_ptr ? NULL : filename_ptr, 10038 functionname_ptr); 10039 return true; 10040 } 10041 10042 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, 10043 &found, filename_ptr, 10044 functionname_ptr, line_ptr, 10045 &elf_tdata (abfd)->line_info)) 10046 return false; 10047 if (found && (*functionname_ptr || *line_ptr)) 10048 return true; 10049 10050 if (symbols == NULL) 10051 return false; 10052 10053 if (! _bfd_elf_find_function (abfd, symbols, section, offset, 10054 filename_ptr, functionname_ptr)) 10055 return false; 10056 10057 *line_ptr = 0; 10058 return true; 10059 } 10060 10061 /* Find the line for a symbol. */ 10062 10063 bool 10064 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, 10065 const char **filename_ptr, unsigned int *line_ptr) 10066 { 10067 struct elf_obj_tdata *tdata = elf_tdata (abfd); 10068 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, 10069 filename_ptr, NULL, line_ptr, NULL, 10070 dwarf_debug_sections, 10071 &tdata->dwarf2_find_line_info); 10072 } 10073 10074 /* After a call to bfd_find_nearest_line, successive calls to 10075 bfd_find_inliner_info can be used to get source information about 10076 each level of function inlining that terminated at the address 10077 passed to bfd_find_nearest_line. Currently this is only supported 10078 for DWARF2 with appropriate DWARF3 extensions. */ 10079 10080 bool 10081 _bfd_elf_find_inliner_info (bfd *abfd, 10082 const char **filename_ptr, 10083 const char **functionname_ptr, 10084 unsigned int *line_ptr) 10085 { 10086 struct elf_obj_tdata *tdata = elf_tdata (abfd); 10087 return _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, 10088 functionname_ptr, line_ptr, 10089 &tdata->dwarf2_find_line_info); 10090 } 10091 10092 int 10093 _bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) 10094 { 10095 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 10096 int ret = bed->s->sizeof_ehdr; 10097 10098 if (!bfd_link_relocatable (info)) 10099 { 10100 bfd_size_type phdr_size = elf_program_header_size (abfd); 10101 10102 if (phdr_size == (bfd_size_type) -1) 10103 { 10104 struct elf_segment_map *m; 10105 10106 phdr_size = 0; 10107 for (m = elf_seg_map (abfd); m != NULL; m = m->next) 10108 phdr_size += bed->s->sizeof_phdr; 10109 10110 if (phdr_size == 0) 10111 phdr_size = get_program_header_size (abfd, info); 10112 } 10113 10114 elf_program_header_size (abfd) = phdr_size; 10115 ret += phdr_size; 10116 } 10117 10118 return ret; 10119 } 10120 10121 bool 10122 _bfd_elf_set_section_contents (bfd *abfd, 10123 sec_ptr section, 10124 const void *location, 10125 file_ptr offset, 10126 bfd_size_type count) 10127 { 10128 Elf_Internal_Shdr *hdr; 10129 10130 if (! abfd->output_has_begun 10131 && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) 10132 return false; 10133 10134 if (!count) 10135 return true; 10136 10137 hdr = &elf_section_data (section)->this_hdr; 10138 if (hdr->sh_offset == (file_ptr) -1) 10139 { 10140 unsigned char *contents; 10141 10142 if (bfd_section_is_ctf (section)) 10143 /* Nothing to do with this section: the contents are generated 10144 later. */ 10145 return true; 10146 10147 if ((offset + count) > hdr->sh_size) 10148 { 10149 _bfd_error_handler 10150 (_("%pB:%pA: error: attempting to write" 10151 " over the end of the section"), 10152 abfd, section); 10153 10154 bfd_set_error (bfd_error_invalid_operation); 10155 return false; 10156 } 10157 10158 contents = hdr->contents; 10159 if (contents == NULL) 10160 { 10161 _bfd_error_handler 10162 (_("%pB:%pA: error: attempting to write" 10163 " section into an empty buffer"), 10164 abfd, section); 10165 10166 bfd_set_error (bfd_error_invalid_operation); 10167 return false; 10168 } 10169 10170 memcpy (contents + offset, location, count); 10171 return true; 10172 } 10173 10174 return _bfd_generic_set_section_contents (abfd, section, 10175 location, offset, count); 10176 } 10177 10178 bool 10179 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, 10180 arelent *cache_ptr ATTRIBUTE_UNUSED, 10181 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) 10182 { 10183 abort (); 10184 return false; 10185 } 10186 10187 /* Try to convert a non-ELF reloc into an ELF one. */ 10188 10189 bool 10190 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) 10191 { 10192 /* Check whether we really have an ELF howto. */ 10193 10194 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) 10195 { 10196 bfd_reloc_code_real_type code; 10197 reloc_howto_type *howto; 10198 10199 /* Alien reloc: Try to determine its type to replace it with an 10200 equivalent ELF reloc. */ 10201 10202 if (areloc->howto->pc_relative) 10203 { 10204 switch (areloc->howto->bitsize) 10205 { 10206 case 8: 10207 code = BFD_RELOC_8_PCREL; 10208 break; 10209 case 12: 10210 code = BFD_RELOC_12_PCREL; 10211 break; 10212 case 16: 10213 code = BFD_RELOC_16_PCREL; 10214 break; 10215 case 24: 10216 code = BFD_RELOC_24_PCREL; 10217 break; 10218 case 32: 10219 code = BFD_RELOC_32_PCREL; 10220 break; 10221 case 64: 10222 code = BFD_RELOC_64_PCREL; 10223 break; 10224 default: 10225 goto fail; 10226 } 10227 10228 howto = bfd_reloc_type_lookup (abfd, code); 10229 10230 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset) 10231 { 10232 if (howto->pcrel_offset) 10233 areloc->addend += areloc->address; 10234 else 10235 areloc->addend -= areloc->address; /* addend is unsigned!! */ 10236 } 10237 } 10238 else 10239 { 10240 switch (areloc->howto->bitsize) 10241 { 10242 case 8: 10243 code = BFD_RELOC_8; 10244 break; 10245 case 14: 10246 code = BFD_RELOC_14; 10247 break; 10248 case 16: 10249 code = BFD_RELOC_16; 10250 break; 10251 case 26: 10252 code = BFD_RELOC_26; 10253 break; 10254 case 32: 10255 code = BFD_RELOC_32; 10256 break; 10257 case 64: 10258 code = BFD_RELOC_64; 10259 break; 10260 default: 10261 goto fail; 10262 } 10263 10264 howto = bfd_reloc_type_lookup (abfd, code); 10265 } 10266 10267 if (howto) 10268 areloc->howto = howto; 10269 else 10270 goto fail; 10271 } 10272 10273 return true; 10274 10275 fail: 10276 /* xgettext:c-format */ 10277 _bfd_error_handler (_("%pB: %s unsupported"), 10278 abfd, areloc->howto->name); 10279 bfd_set_error (bfd_error_sorry); 10280 return false; 10281 } 10282 10283 bool 10284 _bfd_elf_free_cached_info (bfd *abfd) 10285 { 10286 struct elf_obj_tdata *tdata; 10287 10288 if ((bfd_get_format (abfd) == bfd_object 10289 || bfd_get_format (abfd) == bfd_core) 10290 && (tdata = elf_tdata (abfd)) != NULL) 10291 { 10292 if (tdata->o != NULL && elf_shstrtab (abfd) != NULL) 10293 _bfd_elf_strtab_free (elf_shstrtab (abfd)); 10294 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); 10295 _bfd_dwarf1_cleanup_debug_info (abfd, &tdata->dwarf1_find_line_info); 10296 _bfd_stab_cleanup (abfd, &tdata->line_info); 10297 } 10298 10299 return _bfd_generic_bfd_free_cached_info (abfd); 10300 } 10301 10302 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY 10303 in the relocation's offset. Thus we cannot allow any sort of sanity 10304 range-checking to interfere. There is nothing else to do in processing 10305 this reloc. */ 10306 10307 bfd_reloc_status_type 10308 _bfd_elf_rel_vtable_reloc_fn 10309 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, 10310 struct bfd_symbol *symbol ATTRIBUTE_UNUSED, 10311 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, 10312 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) 10313 { 10314 return bfd_reloc_ok; 10315 } 10316 10317 /* Elf core file support. Much of this only works on native 10318 toolchains, since we rely on knowing the 10319 machine-dependent procfs structure in order to pick 10320 out details about the corefile. */ 10321 10322 #ifdef HAVE_SYS_PROCFS_H 10323 # include <sys/procfs.h> 10324 #endif 10325 10326 /* Return a PID that identifies a "thread" for threaded cores, or the 10327 PID of the main process for non-threaded cores. */ 10328 10329 static int 10330 elfcore_make_pid (bfd *abfd) 10331 { 10332 int pid; 10333 10334 pid = elf_tdata (abfd)->core->lwpid; 10335 if (pid == 0) 10336 pid = elf_tdata (abfd)->core->pid; 10337 10338 return pid; 10339 } 10340 10341 /* If there isn't a section called NAME, make one, using data from 10342 SECT. Note, this function will generate a reference to NAME, so 10343 you shouldn't deallocate or overwrite it. */ 10344 10345 static bool 10346 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) 10347 { 10348 asection *sect2; 10349 10350 if (bfd_get_section_by_name (abfd, name) != NULL) 10351 return true; 10352 10353 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); 10354 if (sect2 == NULL) 10355 return false; 10356 10357 sect2->size = sect->size; 10358 sect2->filepos = sect->filepos; 10359 sect2->alignment_power = sect->alignment_power; 10360 return true; 10361 } 10362 10363 /* Create a pseudosection containing SIZE bytes at FILEPOS. This 10364 actually creates up to two pseudosections: 10365 - For the single-threaded case, a section named NAME, unless 10366 such a section already exists. 10367 - For the multi-threaded case, a section named "NAME/PID", where 10368 PID is elfcore_make_pid (abfd). 10369 Both pseudosections have identical contents. */ 10370 bool 10371 _bfd_elfcore_make_pseudosection (bfd *abfd, 10372 char *name, 10373 size_t size, 10374 ufile_ptr filepos) 10375 { 10376 char buf[100]; 10377 char *threaded_name; 10378 size_t len; 10379 asection *sect; 10380 10381 /* Build the section name. */ 10382 10383 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); 10384 len = strlen (buf) + 1; 10385 threaded_name = (char *) bfd_alloc (abfd, len); 10386 if (threaded_name == NULL) 10387 return false; 10388 memcpy (threaded_name, buf, len); 10389 10390 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, 10391 SEC_HAS_CONTENTS); 10392 if (sect == NULL) 10393 return false; 10394 sect->size = size; 10395 sect->filepos = filepos; 10396 sect->alignment_power = 2; 10397 10398 return elfcore_maybe_make_sect (abfd, name, sect); 10399 } 10400 10401 static bool 10402 elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note, 10403 size_t offs) 10404 { 10405 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", 10406 SEC_HAS_CONTENTS); 10407 10408 if (sect == NULL) 10409 return false; 10410 10411 sect->size = note->descsz - offs; 10412 sect->filepos = note->descpos + offs; 10413 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 10414 10415 return true; 10416 } 10417 10418 /* prstatus_t exists on: 10419 solaris 2.5+ 10420 linux 2.[01] + glibc 10421 unixware 4.2 10422 */ 10423 10424 #if defined (HAVE_PRSTATUS_T) 10425 10426 static bool 10427 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) 10428 { 10429 size_t size; 10430 int offset; 10431 10432 if (note->descsz == sizeof (prstatus_t)) 10433 { 10434 prstatus_t prstat; 10435 10436 size = sizeof (prstat.pr_reg); 10437 offset = offsetof (prstatus_t, pr_reg); 10438 memcpy (&prstat, note->descdata, sizeof (prstat)); 10439 10440 /* Do not overwrite the core signal if it 10441 has already been set by another thread. */ 10442 if (elf_tdata (abfd)->core->signal == 0) 10443 elf_tdata (abfd)->core->signal = prstat.pr_cursig; 10444 if (elf_tdata (abfd)->core->pid == 0) 10445 elf_tdata (abfd)->core->pid = prstat.pr_pid; 10446 10447 /* pr_who exists on: 10448 solaris 2.5+ 10449 unixware 4.2 10450 pr_who doesn't exist on: 10451 linux 2.[01] 10452 */ 10453 #if defined (HAVE_PRSTATUS_T_PR_WHO) 10454 elf_tdata (abfd)->core->lwpid = prstat.pr_who; 10455 #else 10456 elf_tdata (abfd)->core->lwpid = prstat.pr_pid; 10457 #endif 10458 } 10459 #if defined (HAVE_PRSTATUS32_T) 10460 else if (note->descsz == sizeof (prstatus32_t)) 10461 { 10462 /* 64-bit host, 32-bit corefile */ 10463 prstatus32_t prstat; 10464 10465 size = sizeof (prstat.pr_reg); 10466 offset = offsetof (prstatus32_t, pr_reg); 10467 memcpy (&prstat, note->descdata, sizeof (prstat)); 10468 10469 /* Do not overwrite the core signal if it 10470 has already been set by another thread. */ 10471 if (elf_tdata (abfd)->core->signal == 0) 10472 elf_tdata (abfd)->core->signal = prstat.pr_cursig; 10473 if (elf_tdata (abfd)->core->pid == 0) 10474 elf_tdata (abfd)->core->pid = prstat.pr_pid; 10475 10476 /* pr_who exists on: 10477 solaris 2.5+ 10478 unixware 4.2 10479 pr_who doesn't exist on: 10480 linux 2.[01] 10481 */ 10482 #if defined (HAVE_PRSTATUS32_T_PR_WHO) 10483 elf_tdata (abfd)->core->lwpid = prstat.pr_who; 10484 #else 10485 elf_tdata (abfd)->core->lwpid = prstat.pr_pid; 10486 #endif 10487 } 10488 #endif /* HAVE_PRSTATUS32_T */ 10489 else 10490 { 10491 /* Fail - we don't know how to handle any other 10492 note size (ie. data object type). */ 10493 return true; 10494 } 10495 10496 /* Make a ".reg/999" section and a ".reg" section. */ 10497 return _bfd_elfcore_make_pseudosection (abfd, ".reg", 10498 size, note->descpos + offset); 10499 } 10500 #endif /* defined (HAVE_PRSTATUS_T) */ 10501 10502 /* Create a pseudosection containing the exact contents of NOTE. */ 10503 static bool 10504 elfcore_make_note_pseudosection (bfd *abfd, 10505 char *name, 10506 Elf_Internal_Note *note) 10507 { 10508 return _bfd_elfcore_make_pseudosection (abfd, name, 10509 note->descsz, note->descpos); 10510 } 10511 10512 /* There isn't a consistent prfpregset_t across platforms, 10513 but it doesn't matter, because we don't have to pick this 10514 data structure apart. */ 10515 10516 static bool 10517 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) 10518 { 10519 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 10520 } 10521 10522 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note 10523 type of NT_PRXFPREG. Just include the whole note's contents 10524 literally. */ 10525 10526 static bool 10527 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) 10528 { 10529 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); 10530 } 10531 10532 /* Linux dumps the Intel XSAVE extended state in a note named "LINUX" 10533 with a note type of NT_X86_XSTATE. Just include the whole note's 10534 contents literally. */ 10535 10536 static bool 10537 elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) 10538 { 10539 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); 10540 } 10541 10542 static bool 10543 elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) 10544 { 10545 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); 10546 } 10547 10548 static bool 10549 elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) 10550 { 10551 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); 10552 } 10553 10554 static bool 10555 elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note) 10556 { 10557 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note); 10558 } 10559 10560 static bool 10561 elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note) 10562 { 10563 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note); 10564 } 10565 10566 static bool 10567 elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note) 10568 { 10569 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note); 10570 } 10571 10572 static bool 10573 elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note) 10574 { 10575 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note); 10576 } 10577 10578 static bool 10579 elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note) 10580 { 10581 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note); 10582 } 10583 10584 static bool 10585 elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note) 10586 { 10587 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note); 10588 } 10589 10590 static bool 10591 elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note) 10592 { 10593 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note); 10594 } 10595 10596 static bool 10597 elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note) 10598 { 10599 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note); 10600 } 10601 10602 static bool 10603 elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note) 10604 { 10605 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note); 10606 } 10607 10608 static bool 10609 elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note) 10610 { 10611 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note); 10612 } 10613 10614 static bool 10615 elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note) 10616 { 10617 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note); 10618 } 10619 10620 static bool 10621 elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note) 10622 { 10623 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note); 10624 } 10625 10626 static bool 10627 elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note) 10628 { 10629 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note); 10630 } 10631 10632 static bool 10633 elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) 10634 { 10635 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); 10636 } 10637 10638 static bool 10639 elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) 10640 { 10641 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); 10642 } 10643 10644 static bool 10645 elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) 10646 { 10647 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); 10648 } 10649 10650 static bool 10651 elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) 10652 { 10653 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); 10654 } 10655 10656 static bool 10657 elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) 10658 { 10659 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); 10660 } 10661 10662 static bool 10663 elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) 10664 { 10665 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); 10666 } 10667 10668 static bool 10669 elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) 10670 { 10671 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); 10672 } 10673 10674 static bool 10675 elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) 10676 { 10677 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); 10678 } 10679 10680 static bool 10681 elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) 10682 { 10683 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); 10684 } 10685 10686 static bool 10687 elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) 10688 { 10689 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); 10690 } 10691 10692 static bool 10693 elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) 10694 { 10695 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); 10696 } 10697 10698 static bool 10699 elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note) 10700 { 10701 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note); 10702 } 10703 10704 static bool 10705 elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note) 10706 { 10707 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note); 10708 } 10709 10710 static bool 10711 elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) 10712 { 10713 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); 10714 } 10715 10716 static bool 10717 elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) 10718 { 10719 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); 10720 } 10721 10722 static bool 10723 elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) 10724 { 10725 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); 10726 } 10727 10728 static bool 10729 elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) 10730 { 10731 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); 10732 } 10733 10734 static bool 10735 elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note) 10736 { 10737 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note); 10738 } 10739 10740 static bool 10741 elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note) 10742 { 10743 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note); 10744 } 10745 10746 static bool 10747 elfcore_grok_aarch_mte (bfd *abfd, Elf_Internal_Note *note) 10748 { 10749 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-mte", 10750 note); 10751 } 10752 10753 static bool 10754 elfcore_grok_aarch_ssve (bfd *abfd, Elf_Internal_Note *note) 10755 { 10756 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-ssve", note); 10757 } 10758 10759 static bool 10760 elfcore_grok_aarch_za (bfd *abfd, Elf_Internal_Note *note) 10761 { 10762 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-za", note); 10763 } 10764 10765 /* Convert NOTE into a bfd_section called ".reg-aarch-zt". Return TRUE if 10766 successful, otherwise return FALSE. */ 10767 10768 static bool 10769 elfcore_grok_aarch_zt (bfd *abfd, Elf_Internal_Note *note) 10770 { 10771 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-zt", note); 10772 } 10773 10774 static bool 10775 elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note) 10776 { 10777 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note); 10778 } 10779 10780 /* Convert NOTE into a bfd_section called ".reg-riscv-csr". Return TRUE if 10781 successful otherwise, return FALSE. */ 10782 10783 static bool 10784 elfcore_grok_riscv_csr (bfd *abfd, Elf_Internal_Note *note) 10785 { 10786 return elfcore_make_note_pseudosection (abfd, ".reg-riscv-csr", note); 10787 } 10788 10789 /* Convert NOTE into a bfd_section called ".gdb-tdesc". Return TRUE if 10790 successful otherwise, return FALSE. */ 10791 10792 static bool 10793 elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note) 10794 { 10795 return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note); 10796 } 10797 10798 static bool 10799 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note) 10800 { 10801 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-cpucfg", note); 10802 } 10803 10804 static bool 10805 elfcore_grok_loongarch_lbt (bfd *abfd, Elf_Internal_Note *note) 10806 { 10807 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lbt", note); 10808 } 10809 10810 static bool 10811 elfcore_grok_loongarch_lsx (bfd *abfd, Elf_Internal_Note *note) 10812 { 10813 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lsx", note); 10814 } 10815 10816 static bool 10817 elfcore_grok_loongarch_lasx (bfd *abfd, Elf_Internal_Note *note) 10818 { 10819 return elfcore_make_note_pseudosection (abfd, ".reg-loongarch-lasx", note); 10820 } 10821 10822 #if defined (HAVE_PRPSINFO_T) 10823 typedef prpsinfo_t elfcore_psinfo_t; 10824 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ 10825 typedef prpsinfo32_t elfcore_psinfo32_t; 10826 #endif 10827 #endif 10828 10829 #if defined (HAVE_PSINFO_T) 10830 typedef psinfo_t elfcore_psinfo_t; 10831 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ 10832 typedef psinfo32_t elfcore_psinfo32_t; 10833 #endif 10834 #endif 10835 10836 /* return a malloc'ed copy of a string at START which is at 10837 most MAX bytes long, possibly without a terminating '\0'. 10838 the copy will always have a terminating '\0'. */ 10839 10840 char * 10841 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) 10842 { 10843 char *dups; 10844 char *end = (char *) memchr (start, '\0', max); 10845 size_t len; 10846 10847 if (end == NULL) 10848 len = max; 10849 else 10850 len = end - start; 10851 10852 dups = (char *) bfd_alloc (abfd, len + 1); 10853 if (dups == NULL) 10854 return NULL; 10855 10856 memcpy (dups, start, len); 10857 dups[len] = '\0'; 10858 10859 return dups; 10860 } 10861 10862 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 10863 static bool 10864 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) 10865 { 10866 if (note->descsz == sizeof (elfcore_psinfo_t)) 10867 { 10868 elfcore_psinfo_t psinfo; 10869 10870 memcpy (&psinfo, note->descdata, sizeof (psinfo)); 10871 10872 #if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) 10873 elf_tdata (abfd)->core->pid = psinfo.pr_pid; 10874 #endif 10875 elf_tdata (abfd)->core->program 10876 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, 10877 sizeof (psinfo.pr_fname)); 10878 10879 elf_tdata (abfd)->core->command 10880 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, 10881 sizeof (psinfo.pr_psargs)); 10882 } 10883 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) 10884 else if (note->descsz == sizeof (elfcore_psinfo32_t)) 10885 { 10886 /* 64-bit host, 32-bit corefile */ 10887 elfcore_psinfo32_t psinfo; 10888 10889 memcpy (&psinfo, note->descdata, sizeof (psinfo)); 10890 10891 #if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) 10892 elf_tdata (abfd)->core->pid = psinfo.pr_pid; 10893 #endif 10894 elf_tdata (abfd)->core->program 10895 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, 10896 sizeof (psinfo.pr_fname)); 10897 10898 elf_tdata (abfd)->core->command 10899 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, 10900 sizeof (psinfo.pr_psargs)); 10901 } 10902 #endif 10903 10904 else 10905 { 10906 /* Fail - we don't know how to handle any other 10907 note size (ie. data object type). */ 10908 return true; 10909 } 10910 10911 /* Note that for some reason, a spurious space is tacked 10912 onto the end of the args in some (at least one anyway) 10913 implementations, so strip it off if it exists. */ 10914 10915 { 10916 char *command = elf_tdata (abfd)->core->command; 10917 int n = strlen (command); 10918 10919 if (0 < n && command[n - 1] == ' ') 10920 command[n - 1] = '\0'; 10921 } 10922 10923 return true; 10924 } 10925 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ 10926 10927 #if defined (HAVE_PSTATUS_T) 10928 static bool 10929 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) 10930 { 10931 if (note->descsz == sizeof (pstatus_t) 10932 #if defined (HAVE_PXSTATUS_T) 10933 || note->descsz == sizeof (pxstatus_t) 10934 #endif 10935 ) 10936 { 10937 pstatus_t pstat; 10938 10939 memcpy (&pstat, note->descdata, sizeof (pstat)); 10940 10941 elf_tdata (abfd)->core->pid = pstat.pr_pid; 10942 } 10943 #if defined (HAVE_PSTATUS32_T) 10944 else if (note->descsz == sizeof (pstatus32_t)) 10945 { 10946 /* 64-bit host, 32-bit corefile */ 10947 pstatus32_t pstat; 10948 10949 memcpy (&pstat, note->descdata, sizeof (pstat)); 10950 10951 elf_tdata (abfd)->core->pid = pstat.pr_pid; 10952 } 10953 #endif 10954 /* Could grab some more details from the "representative" 10955 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an 10956 NT_LWPSTATUS note, presumably. */ 10957 10958 return true; 10959 } 10960 #endif /* defined (HAVE_PSTATUS_T) */ 10961 10962 #if defined (HAVE_LWPSTATUS_T) 10963 static bool 10964 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) 10965 { 10966 lwpstatus_t lwpstat; 10967 char buf[100]; 10968 char *name; 10969 size_t len; 10970 asection *sect; 10971 10972 if (note->descsz != sizeof (lwpstat) 10973 #if defined (HAVE_LWPXSTATUS_T) 10974 && note->descsz != sizeof (lwpxstatus_t) 10975 #endif 10976 ) 10977 return true; 10978 10979 memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); 10980 10981 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; 10982 /* Do not overwrite the core signal if it has already been set by 10983 another thread. */ 10984 if (elf_tdata (abfd)->core->signal == 0) 10985 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; 10986 10987 /* Make a ".reg/999" section. */ 10988 10989 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); 10990 len = strlen (buf) + 1; 10991 name = bfd_alloc (abfd, len); 10992 if (name == NULL) 10993 return false; 10994 memcpy (name, buf, len); 10995 10996 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 10997 if (sect == NULL) 10998 return false; 10999 11000 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 11001 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); 11002 sect->filepos = note->descpos 11003 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); 11004 #endif 11005 11006 #if defined (HAVE_LWPSTATUS_T_PR_REG) 11007 sect->size = sizeof (lwpstat.pr_reg); 11008 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); 11009 #endif 11010 11011 sect->alignment_power = 2; 11012 11013 if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) 11014 return false; 11015 11016 /* Make a ".reg2/999" section */ 11017 11018 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); 11019 len = strlen (buf) + 1; 11020 name = bfd_alloc (abfd, len); 11021 if (name == NULL) 11022 return false; 11023 memcpy (name, buf, len); 11024 11025 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 11026 if (sect == NULL) 11027 return false; 11028 11029 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 11030 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); 11031 sect->filepos = note->descpos 11032 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); 11033 #endif 11034 11035 #if defined (HAVE_LWPSTATUS_T_PR_FPREG) 11036 sect->size = sizeof (lwpstat.pr_fpreg); 11037 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); 11038 #endif 11039 11040 sect->alignment_power = 2; 11041 11042 return elfcore_maybe_make_sect (abfd, ".reg2", sect); 11043 } 11044 #endif /* defined (HAVE_LWPSTATUS_T) */ 11045 11046 /* These constants, and the structure offsets used below, are defined by 11047 Cygwin's core_dump.h */ 11048 #define NOTE_INFO_PROCESS 1 11049 #define NOTE_INFO_THREAD 2 11050 #define NOTE_INFO_MODULE 3 11051 #define NOTE_INFO_MODULE64 4 11052 11053 static bool 11054 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) 11055 { 11056 char buf[30]; 11057 char *name; 11058 size_t len; 11059 unsigned int name_size; 11060 asection *sect; 11061 unsigned int type; 11062 int is_active_thread; 11063 bfd_vma base_addr; 11064 11065 if (note->descsz < 4) 11066 return true; 11067 11068 if (! startswith (note->namedata, "win32")) 11069 return true; 11070 11071 type = bfd_get_32 (abfd, note->descdata); 11072 11073 struct 11074 { 11075 const char *type_name; 11076 unsigned long min_size; 11077 } size_check[] = 11078 { 11079 { "NOTE_INFO_PROCESS", 12 }, 11080 { "NOTE_INFO_THREAD", 12 }, 11081 { "NOTE_INFO_MODULE", 12 }, 11082 { "NOTE_INFO_MODULE64", 16 }, 11083 }; 11084 11085 if (type == 0 || type > (sizeof(size_check)/sizeof(size_check[0]))) 11086 return true; 11087 11088 if (note->descsz < size_check[type - 1].min_size) 11089 { 11090 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes" 11091 " is too small"), 11092 abfd, size_check[type - 1].type_name, note->descsz); 11093 return true; 11094 } 11095 11096 switch (type) 11097 { 11098 case NOTE_INFO_PROCESS: 11099 /* FIXME: need to add ->core->command. */ 11100 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4); 11101 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8); 11102 break; 11103 11104 case NOTE_INFO_THREAD: 11105 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT 11106 structure. */ 11107 /* thread_info.tid */ 11108 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4)); 11109 11110 len = strlen (buf) + 1; 11111 name = (char *) bfd_alloc (abfd, len); 11112 if (name == NULL) 11113 return false; 11114 11115 memcpy (name, buf, len); 11116 11117 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 11118 if (sect == NULL) 11119 return false; 11120 11121 /* sizeof (thread_info.thread_context) */ 11122 sect->size = note->descsz - 12; 11123 /* offsetof (thread_info.thread_context) */ 11124 sect->filepos = note->descpos + 12; 11125 sect->alignment_power = 2; 11126 11127 /* thread_info.is_active_thread */ 11128 is_active_thread = bfd_get_32 (abfd, note->descdata + 8); 11129 11130 if (is_active_thread) 11131 if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) 11132 return false; 11133 break; 11134 11135 case NOTE_INFO_MODULE: 11136 case NOTE_INFO_MODULE64: 11137 /* Make a ".module/xxxxxxxx" section. */ 11138 if (type == NOTE_INFO_MODULE) 11139 { 11140 /* module_info.base_address */ 11141 base_addr = bfd_get_32 (abfd, note->descdata + 4); 11142 sprintf (buf, ".module/%08lx", (unsigned long) base_addr); 11143 /* module_info.module_name_size */ 11144 name_size = bfd_get_32 (abfd, note->descdata + 8); 11145 } 11146 else /* NOTE_INFO_MODULE64 */ 11147 { 11148 /* module_info.base_address */ 11149 base_addr = bfd_get_64 (abfd, note->descdata + 4); 11150 sprintf (buf, ".module/%016lx", (unsigned long) base_addr); 11151 /* module_info.module_name_size */ 11152 name_size = bfd_get_32 (abfd, note->descdata + 12); 11153 } 11154 11155 len = strlen (buf) + 1; 11156 name = (char *) bfd_alloc (abfd, len); 11157 if (name == NULL) 11158 return false; 11159 11160 memcpy (name, buf, len); 11161 11162 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 11163 11164 if (sect == NULL) 11165 return false; 11166 11167 if (note->descsz < 12 + name_size) 11168 { 11169 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu" 11170 " is too small to contain a name of size %u"), 11171 abfd, note->descsz, name_size); 11172 return true; 11173 } 11174 11175 sect->size = note->descsz; 11176 sect->filepos = note->descpos; 11177 sect->alignment_power = 2; 11178 break; 11179 11180 default: 11181 return true; 11182 } 11183 11184 return true; 11185 } 11186 11187 static bool 11188 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) 11189 { 11190 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11191 11192 switch (note->type) 11193 { 11194 default: 11195 return true; 11196 11197 case NT_PRSTATUS: 11198 if (bed->elf_backend_grok_prstatus) 11199 if ((*bed->elf_backend_grok_prstatus) (abfd, note)) 11200 return true; 11201 #if defined (HAVE_PRSTATUS_T) 11202 return elfcore_grok_prstatus (abfd, note); 11203 #else 11204 return true; 11205 #endif 11206 11207 #if defined (HAVE_PSTATUS_T) 11208 case NT_PSTATUS: 11209 return elfcore_grok_pstatus (abfd, note); 11210 #endif 11211 11212 #if defined (HAVE_LWPSTATUS_T) 11213 case NT_LWPSTATUS: 11214 return elfcore_grok_lwpstatus (abfd, note); 11215 #endif 11216 11217 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ 11218 return elfcore_grok_prfpreg (abfd, note); 11219 11220 case NT_WIN32PSTATUS: 11221 return elfcore_grok_win32pstatus (abfd, note); 11222 11223 case NT_PRXFPREG: /* Linux SSE extension */ 11224 if (note->namesz == 6 11225 && strcmp (note->namedata, "LINUX") == 0) 11226 return elfcore_grok_prxfpreg (abfd, note); 11227 else 11228 return true; 11229 11230 case NT_X86_XSTATE: /* Linux XSAVE extension */ 11231 if (note->namesz == 6 11232 && strcmp (note->namedata, "LINUX") == 0) 11233 return elfcore_grok_xstatereg (abfd, note); 11234 else 11235 return true; 11236 11237 case NT_PPC_VMX: 11238 if (note->namesz == 6 11239 && strcmp (note->namedata, "LINUX") == 0) 11240 return elfcore_grok_ppc_vmx (abfd, note); 11241 else 11242 return true; 11243 11244 case NT_PPC_VSX: 11245 if (note->namesz == 6 11246 && strcmp (note->namedata, "LINUX") == 0) 11247 return elfcore_grok_ppc_vsx (abfd, note); 11248 else 11249 return true; 11250 11251 case NT_PPC_TAR: 11252 if (note->namesz == 6 11253 && strcmp (note->namedata, "LINUX") == 0) 11254 return elfcore_grok_ppc_tar (abfd, note); 11255 else 11256 return true; 11257 11258 case NT_PPC_PPR: 11259 if (note->namesz == 6 11260 && strcmp (note->namedata, "LINUX") == 0) 11261 return elfcore_grok_ppc_ppr (abfd, note); 11262 else 11263 return true; 11264 11265 case NT_PPC_DSCR: 11266 if (note->namesz == 6 11267 && strcmp (note->namedata, "LINUX") == 0) 11268 return elfcore_grok_ppc_dscr (abfd, note); 11269 else 11270 return true; 11271 11272 case NT_PPC_EBB: 11273 if (note->namesz == 6 11274 && strcmp (note->namedata, "LINUX") == 0) 11275 return elfcore_grok_ppc_ebb (abfd, note); 11276 else 11277 return true; 11278 11279 case NT_PPC_PMU: 11280 if (note->namesz == 6 11281 && strcmp (note->namedata, "LINUX") == 0) 11282 return elfcore_grok_ppc_pmu (abfd, note); 11283 else 11284 return true; 11285 11286 case NT_PPC_TM_CGPR: 11287 if (note->namesz == 6 11288 && strcmp (note->namedata, "LINUX") == 0) 11289 return elfcore_grok_ppc_tm_cgpr (abfd, note); 11290 else 11291 return true; 11292 11293 case NT_PPC_TM_CFPR: 11294 if (note->namesz == 6 11295 && strcmp (note->namedata, "LINUX") == 0) 11296 return elfcore_grok_ppc_tm_cfpr (abfd, note); 11297 else 11298 return true; 11299 11300 case NT_PPC_TM_CVMX: 11301 if (note->namesz == 6 11302 && strcmp (note->namedata, "LINUX") == 0) 11303 return elfcore_grok_ppc_tm_cvmx (abfd, note); 11304 else 11305 return true; 11306 11307 case NT_PPC_TM_CVSX: 11308 if (note->namesz == 6 11309 && strcmp (note->namedata, "LINUX") == 0) 11310 return elfcore_grok_ppc_tm_cvsx (abfd, note); 11311 else 11312 return true; 11313 11314 case NT_PPC_TM_SPR: 11315 if (note->namesz == 6 11316 && strcmp (note->namedata, "LINUX") == 0) 11317 return elfcore_grok_ppc_tm_spr (abfd, note); 11318 else 11319 return true; 11320 11321 case NT_PPC_TM_CTAR: 11322 if (note->namesz == 6 11323 && strcmp (note->namedata, "LINUX") == 0) 11324 return elfcore_grok_ppc_tm_ctar (abfd, note); 11325 else 11326 return true; 11327 11328 case NT_PPC_TM_CPPR: 11329 if (note->namesz == 6 11330 && strcmp (note->namedata, "LINUX") == 0) 11331 return elfcore_grok_ppc_tm_cppr (abfd, note); 11332 else 11333 return true; 11334 11335 case NT_PPC_TM_CDSCR: 11336 if (note->namesz == 6 11337 && strcmp (note->namedata, "LINUX") == 0) 11338 return elfcore_grok_ppc_tm_cdscr (abfd, note); 11339 else 11340 return true; 11341 11342 case NT_S390_HIGH_GPRS: 11343 if (note->namesz == 6 11344 && strcmp (note->namedata, "LINUX") == 0) 11345 return elfcore_grok_s390_high_gprs (abfd, note); 11346 else 11347 return true; 11348 11349 case NT_S390_TIMER: 11350 if (note->namesz == 6 11351 && strcmp (note->namedata, "LINUX") == 0) 11352 return elfcore_grok_s390_timer (abfd, note); 11353 else 11354 return true; 11355 11356 case NT_S390_TODCMP: 11357 if (note->namesz == 6 11358 && strcmp (note->namedata, "LINUX") == 0) 11359 return elfcore_grok_s390_todcmp (abfd, note); 11360 else 11361 return true; 11362 11363 case NT_S390_TODPREG: 11364 if (note->namesz == 6 11365 && strcmp (note->namedata, "LINUX") == 0) 11366 return elfcore_grok_s390_todpreg (abfd, note); 11367 else 11368 return true; 11369 11370 case NT_S390_CTRS: 11371 if (note->namesz == 6 11372 && strcmp (note->namedata, "LINUX") == 0) 11373 return elfcore_grok_s390_ctrs (abfd, note); 11374 else 11375 return true; 11376 11377 case NT_S390_PREFIX: 11378 if (note->namesz == 6 11379 && strcmp (note->namedata, "LINUX") == 0) 11380 return elfcore_grok_s390_prefix (abfd, note); 11381 else 11382 return true; 11383 11384 case NT_S390_LAST_BREAK: 11385 if (note->namesz == 6 11386 && strcmp (note->namedata, "LINUX") == 0) 11387 return elfcore_grok_s390_last_break (abfd, note); 11388 else 11389 return true; 11390 11391 case NT_S390_SYSTEM_CALL: 11392 if (note->namesz == 6 11393 && strcmp (note->namedata, "LINUX") == 0) 11394 return elfcore_grok_s390_system_call (abfd, note); 11395 else 11396 return true; 11397 11398 case NT_S390_TDB: 11399 if (note->namesz == 6 11400 && strcmp (note->namedata, "LINUX") == 0) 11401 return elfcore_grok_s390_tdb (abfd, note); 11402 else 11403 return true; 11404 11405 case NT_S390_VXRS_LOW: 11406 if (note->namesz == 6 11407 && strcmp (note->namedata, "LINUX") == 0) 11408 return elfcore_grok_s390_vxrs_low (abfd, note); 11409 else 11410 return true; 11411 11412 case NT_S390_VXRS_HIGH: 11413 if (note->namesz == 6 11414 && strcmp (note->namedata, "LINUX") == 0) 11415 return elfcore_grok_s390_vxrs_high (abfd, note); 11416 else 11417 return true; 11418 11419 case NT_S390_GS_CB: 11420 if (note->namesz == 6 11421 && strcmp (note->namedata, "LINUX") == 0) 11422 return elfcore_grok_s390_gs_cb (abfd, note); 11423 else 11424 return true; 11425 11426 case NT_S390_GS_BC: 11427 if (note->namesz == 6 11428 && strcmp (note->namedata, "LINUX") == 0) 11429 return elfcore_grok_s390_gs_bc (abfd, note); 11430 else 11431 return true; 11432 11433 case NT_ARC_V2: 11434 if (note->namesz == 6 11435 && strcmp (note->namedata, "LINUX") == 0) 11436 return elfcore_grok_arc_v2 (abfd, note); 11437 else 11438 return true; 11439 11440 case NT_ARM_VFP: 11441 if (note->namesz == 6 11442 && strcmp (note->namedata, "LINUX") == 0) 11443 return elfcore_grok_arm_vfp (abfd, note); 11444 else 11445 return true; 11446 11447 case NT_ARM_TLS: 11448 if (note->namesz == 6 11449 && strcmp (note->namedata, "LINUX") == 0) 11450 return elfcore_grok_aarch_tls (abfd, note); 11451 else 11452 return true; 11453 11454 case NT_ARM_HW_BREAK: 11455 if (note->namesz == 6 11456 && strcmp (note->namedata, "LINUX") == 0) 11457 return elfcore_grok_aarch_hw_break (abfd, note); 11458 else 11459 return true; 11460 11461 case NT_ARM_HW_WATCH: 11462 if (note->namesz == 6 11463 && strcmp (note->namedata, "LINUX") == 0) 11464 return elfcore_grok_aarch_hw_watch (abfd, note); 11465 else 11466 return true; 11467 11468 case NT_ARM_SVE: 11469 if (note->namesz == 6 11470 && strcmp (note->namedata, "LINUX") == 0) 11471 return elfcore_grok_aarch_sve (abfd, note); 11472 else 11473 return true; 11474 11475 case NT_ARM_PAC_MASK: 11476 if (note->namesz == 6 11477 && strcmp (note->namedata, "LINUX") == 0) 11478 return elfcore_grok_aarch_pauth (abfd, note); 11479 else 11480 return true; 11481 11482 case NT_ARM_TAGGED_ADDR_CTRL: 11483 if (note->namesz == 6 11484 && strcmp (note->namedata, "LINUX") == 0) 11485 return elfcore_grok_aarch_mte (abfd, note); 11486 else 11487 return true; 11488 11489 case NT_ARM_SSVE: 11490 if (note->namesz == 6 11491 && strcmp (note->namedata, "LINUX") == 0) 11492 return elfcore_grok_aarch_ssve (abfd, note); 11493 else 11494 return true; 11495 11496 case NT_ARM_ZA: 11497 if (note->namesz == 6 11498 && strcmp (note->namedata, "LINUX") == 0) 11499 return elfcore_grok_aarch_za (abfd, note); 11500 else 11501 return true; 11502 11503 case NT_ARM_ZT: 11504 if (note->namesz == 6 11505 && strcmp (note->namedata, "LINUX") == 0) 11506 return elfcore_grok_aarch_zt (abfd, note); 11507 else 11508 return true; 11509 11510 case NT_GDB_TDESC: 11511 if (note->namesz == 4 11512 && strcmp (note->namedata, "GDB") == 0) 11513 return elfcore_grok_gdb_tdesc (abfd, note); 11514 else 11515 return true; 11516 11517 case NT_RISCV_CSR: 11518 if (note->namesz == 4 11519 && strcmp (note->namedata, "GDB") == 0) 11520 return elfcore_grok_riscv_csr (abfd, note); 11521 else 11522 return true; 11523 11524 case NT_LARCH_CPUCFG: 11525 if (note->namesz == 6 11526 && strcmp (note->namedata, "LINUX") == 0) 11527 return elfcore_grok_loongarch_cpucfg (abfd, note); 11528 else 11529 return true; 11530 11531 case NT_LARCH_LBT: 11532 if (note->namesz == 6 11533 && strcmp (note->namedata, "LINUX") == 0) 11534 return elfcore_grok_loongarch_lbt (abfd, note); 11535 else 11536 return true; 11537 11538 case NT_LARCH_LSX: 11539 if (note->namesz == 6 11540 && strcmp (note->namedata, "LINUX") == 0) 11541 return elfcore_grok_loongarch_lsx (abfd, note); 11542 else 11543 return true; 11544 11545 case NT_LARCH_LASX: 11546 if (note->namesz == 6 11547 && strcmp (note->namedata, "LINUX") == 0) 11548 return elfcore_grok_loongarch_lasx (abfd, note); 11549 else 11550 return true; 11551 11552 case NT_PRPSINFO: 11553 case NT_PSINFO: 11554 if (bed->elf_backend_grok_psinfo) 11555 if ((*bed->elf_backend_grok_psinfo) (abfd, note)) 11556 return true; 11557 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 11558 return elfcore_grok_psinfo (abfd, note); 11559 #else 11560 return true; 11561 #endif 11562 11563 case NT_AUXV: 11564 return elfcore_make_auxv_note_section (abfd, note, 0); 11565 11566 case NT_FILE: 11567 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", 11568 note); 11569 11570 case NT_SIGINFO: 11571 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", 11572 note); 11573 11574 } 11575 } 11576 11577 static bool 11578 elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) 11579 { 11580 struct bfd_build_id* build_id; 11581 11582 if (note->descsz == 0) 11583 return false; 11584 11585 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); 11586 if (build_id == NULL) 11587 return false; 11588 11589 build_id->size = note->descsz; 11590 memcpy (build_id->data, note->descdata, note->descsz); 11591 abfd->build_id = build_id; 11592 11593 return true; 11594 } 11595 11596 static bool 11597 elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) 11598 { 11599 switch (note->type) 11600 { 11601 default: 11602 return true; 11603 11604 case NT_GNU_PROPERTY_TYPE_0: 11605 return _bfd_elf_parse_gnu_properties (abfd, note); 11606 11607 case NT_GNU_BUILD_ID: 11608 return elfobj_grok_gnu_build_id (abfd, note); 11609 } 11610 } 11611 11612 static bool 11613 elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) 11614 { 11615 struct sdt_note *cur = 11616 (struct sdt_note *) bfd_alloc (abfd, 11617 sizeof (struct sdt_note) + note->descsz); 11618 11619 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; 11620 cur->size = (bfd_size_type) note->descsz; 11621 memcpy (cur->data, note->descdata, note->descsz); 11622 11623 elf_tdata (abfd)->sdt_note_head = cur; 11624 11625 return true; 11626 } 11627 11628 static bool 11629 elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) 11630 { 11631 switch (note->type) 11632 { 11633 case NT_STAPSDT: 11634 return elfobj_grok_stapsdt_note_1 (abfd, note); 11635 11636 default: 11637 return true; 11638 } 11639 } 11640 11641 static bool 11642 elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) 11643 { 11644 size_t offset; 11645 11646 switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) 11647 { 11648 case ELFCLASS32: 11649 if (note->descsz < 108) 11650 return false; 11651 break; 11652 11653 case ELFCLASS64: 11654 if (note->descsz < 120) 11655 return false; 11656 break; 11657 11658 default: 11659 return false; 11660 } 11661 11662 /* Check for version 1 in pr_version. */ 11663 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) 11664 return false; 11665 11666 offset = 4; 11667 11668 /* Skip over pr_psinfosz. */ 11669 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) 11670 offset += 4; 11671 else 11672 { 11673 offset += 4; /* Padding before pr_psinfosz. */ 11674 offset += 8; 11675 } 11676 11677 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ 11678 elf_tdata (abfd)->core->program 11679 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); 11680 offset += 17; 11681 11682 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ 11683 elf_tdata (abfd)->core->command 11684 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); 11685 offset += 81; 11686 11687 /* Padding before pr_pid. */ 11688 offset += 2; 11689 11690 /* The pr_pid field was added in version "1a". */ 11691 if (note->descsz < offset + 4) 11692 return true; 11693 11694 elf_tdata (abfd)->core->pid 11695 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11696 11697 return true; 11698 } 11699 11700 static bool 11701 elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) 11702 { 11703 size_t offset; 11704 size_t size; 11705 size_t min_size; 11706 11707 /* Compute offset of pr_getregsz, skipping over pr_statussz. 11708 Also compute minimum size of this note. */ 11709 switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) 11710 { 11711 case ELFCLASS32: 11712 offset = 4 + 4; 11713 min_size = offset + (4 * 2) + 4 + 4 + 4; 11714 break; 11715 11716 case ELFCLASS64: 11717 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */ 11718 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4; 11719 break; 11720 11721 default: 11722 return false; 11723 } 11724 11725 if (note->descsz < min_size) 11726 return false; 11727 11728 /* Check for version 1 in pr_version. */ 11729 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) 11730 return false; 11731 11732 /* Extract size of pr_reg from pr_gregsetsz. */ 11733 /* Skip over pr_gregsetsz and pr_fpregsetsz. */ 11734 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) 11735 { 11736 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11737 offset += 4 * 2; 11738 } 11739 else 11740 { 11741 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); 11742 offset += 8 * 2; 11743 } 11744 11745 /* Skip over pr_osreldate. */ 11746 offset += 4; 11747 11748 /* Read signal from pr_cursig. */ 11749 if (elf_tdata (abfd)->core->signal == 0) 11750 elf_tdata (abfd)->core->signal 11751 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11752 offset += 4; 11753 11754 /* Read TID from pr_pid. */ 11755 elf_tdata (abfd)->core->lwpid 11756 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 11757 offset += 4; 11758 11759 /* Padding before pr_reg. */ 11760 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) 11761 offset += 4; 11762 11763 /* Make sure that there is enough data remaining in the note. */ 11764 if ((note->descsz - offset) < size) 11765 return false; 11766 11767 /* Make a ".reg/999" section and a ".reg" section. */ 11768 return _bfd_elfcore_make_pseudosection (abfd, ".reg", 11769 size, note->descpos + offset); 11770 } 11771 11772 static bool 11773 elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) 11774 { 11775 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11776 11777 switch (note->type) 11778 { 11779 case NT_PRSTATUS: 11780 if (bed->elf_backend_grok_freebsd_prstatus) 11781 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note)) 11782 return true; 11783 return elfcore_grok_freebsd_prstatus (abfd, note); 11784 11785 case NT_FPREGSET: 11786 return elfcore_grok_prfpreg (abfd, note); 11787 11788 case NT_PRPSINFO: 11789 return elfcore_grok_freebsd_psinfo (abfd, note); 11790 11791 case NT_FREEBSD_THRMISC: 11792 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); 11793 11794 case NT_FREEBSD_PROCSTAT_PROC: 11795 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc", 11796 note); 11797 11798 case NT_FREEBSD_PROCSTAT_FILES: 11799 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files", 11800 note); 11801 11802 case NT_FREEBSD_PROCSTAT_VMMAP: 11803 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap", 11804 note); 11805 11806 case NT_FREEBSD_PROCSTAT_AUXV: 11807 return elfcore_make_auxv_note_section (abfd, note, 4); 11808 11809 case NT_FREEBSD_X86_SEGBASES: 11810 return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note); 11811 11812 case NT_X86_XSTATE: 11813 return elfcore_grok_xstatereg (abfd, note); 11814 11815 case NT_FREEBSD_PTLWPINFO: 11816 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo", 11817 note); 11818 11819 case NT_ARM_TLS: 11820 return elfcore_grok_aarch_tls (abfd, note); 11821 11822 case NT_ARM_VFP: 11823 return elfcore_grok_arm_vfp (abfd, note); 11824 11825 default: 11826 return true; 11827 } 11828 } 11829 11830 static bool 11831 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) 11832 { 11833 char *cp; 11834 11835 cp = strchr (note->namedata, '@'); 11836 if (cp != NULL) 11837 { 11838 *lwpidp = atoi(cp + 1); 11839 return true; 11840 } 11841 return false; 11842 } 11843 11844 static bool 11845 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) 11846 { 11847 if (note->descsz <= 0x7c + 31) 11848 return false; 11849 11850 /* Signal number at offset 0x08. */ 11851 elf_tdata (abfd)->core->signal 11852 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); 11853 11854 /* Process ID at offset 0x50. */ 11855 elf_tdata (abfd)->core->pid 11856 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); 11857 11858 /* Command name at 0x7c (max 32 bytes, including nul). */ 11859 elf_tdata (abfd)->core->command 11860 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); 11861 11862 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", 11863 note); 11864 } 11865 11866 static bool 11867 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) 11868 { 11869 int lwp; 11870 11871 if (elfcore_netbsd_get_lwpid (note, &lwp)) 11872 elf_tdata (abfd)->core->lwpid = lwp; 11873 11874 switch (note->type) 11875 { 11876 case NT_NETBSDCORE_PROCINFO: 11877 /* NetBSD-specific core "procinfo". Note that we expect to 11878 find this note before any of the others, which is fine, 11879 since the kernel writes this note out first when it 11880 creates a core file. */ 11881 return elfcore_grok_netbsd_procinfo (abfd, note); 11882 case NT_NETBSDCORE_AUXV: 11883 /* NetBSD-specific Elf Auxiliary Vector data. */ 11884 return elfcore_make_auxv_note_section (abfd, note, 0); 11885 case NT_NETBSDCORE_LWPSTATUS: 11886 return elfcore_make_note_pseudosection (abfd, 11887 ".note.netbsdcore.lwpstatus", 11888 note); 11889 default: 11890 break; 11891 } 11892 11893 if (note->type == NT_NETBSDCORE_AUXV) 11894 { 11895 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", 11896 SEC_HAS_CONTENTS); 11897 11898 if (sect == NULL) 11899 return false; 11900 sect->size = note->descsz; 11901 sect->filepos = note->descpos; 11902 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 11903 11904 return true; 11905 } 11906 11907 /* As of March 2020 there are no other machine-independent notes 11908 defined for NetBSD core files. If the note type is less 11909 than the start of the machine-dependent note types, we don't 11910 understand it. */ 11911 11912 if (note->type < NT_NETBSDCORE_FIRSTMACH) 11913 return true; 11914 11915 11916 switch (bfd_get_arch (abfd)) 11917 { 11918 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and 11919 PT_GETFPREGS == mach+2. */ 11920 11921 case bfd_arch_aarch64: 11922 case bfd_arch_alpha: 11923 case bfd_arch_riscv: 11924 case bfd_arch_sparc: 11925 switch (note->type) 11926 { 11927 case NT_NETBSDCORE_FIRSTMACH+0: 11928 return elfcore_make_note_pseudosection (abfd, ".reg", note); 11929 11930 case NT_NETBSDCORE_FIRSTMACH+2: 11931 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 11932 11933 default: 11934 return true; 11935 } 11936 11937 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5. 11938 There's also old PT___GETREGS40 == mach + 1 for old reg 11939 structure which lacks GBR. */ 11940 11941 case bfd_arch_sh: 11942 switch (note->type) 11943 { 11944 case NT_NETBSDCORE_FIRSTMACH+3: 11945 return elfcore_make_note_pseudosection (abfd, ".reg", note); 11946 11947 case NT_NETBSDCORE_FIRSTMACH+5: 11948 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 11949 11950 default: 11951 return true; 11952 } 11953 11954 /* On all other arch's, PT_GETREGS == mach+1 and 11955 PT_GETFPREGS == mach+3. */ 11956 11957 default: 11958 switch (note->type) 11959 { 11960 case NT_NETBSDCORE_FIRSTMACH+1: 11961 return elfcore_make_note_pseudosection (abfd, ".reg", note); 11962 11963 case NT_NETBSDCORE_FIRSTMACH+3: 11964 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 11965 11966 default: 11967 return true; 11968 } 11969 } 11970 /* NOTREACHED */ 11971 } 11972 11973 static bool 11974 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) 11975 { 11976 if (note->descsz <= 0x48 + 31) 11977 return false; 11978 11979 /* Signal number at offset 0x08. */ 11980 elf_tdata (abfd)->core->signal 11981 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); 11982 11983 /* Process ID at offset 0x20. */ 11984 elf_tdata (abfd)->core->pid 11985 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); 11986 11987 /* Command name at 0x48 (max 32 bytes, including nul). */ 11988 elf_tdata (abfd)->core->command 11989 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); 11990 11991 return true; 11992 } 11993 11994 /* Processes Solaris's process status note. 11995 sig_off ~ offsetof(prstatus_t, pr_cursig) 11996 pid_off ~ offsetof(prstatus_t, pr_pid) 11997 lwpid_off ~ offsetof(prstatus_t, pr_who) 11998 gregset_size ~ sizeof(gregset_t) 11999 gregset_offset ~ offsetof(prstatus_t, pr_reg) */ 12000 12001 static bool 12002 elfcore_grok_solaris_prstatus (bfd *abfd, Elf_Internal_Note* note, int sig_off, 12003 int pid_off, int lwpid_off, size_t gregset_size, 12004 size_t gregset_offset) 12005 { 12006 asection *sect = NULL; 12007 elf_tdata (abfd)->core->signal 12008 = bfd_get_16 (abfd, note->descdata + sig_off); 12009 elf_tdata (abfd)->core->pid 12010 = bfd_get_32 (abfd, note->descdata + pid_off); 12011 elf_tdata (abfd)->core->lwpid 12012 = bfd_get_32 (abfd, note->descdata + lwpid_off); 12013 12014 sect = bfd_get_section_by_name (abfd, ".reg"); 12015 if (sect != NULL) 12016 sect->size = gregset_size; 12017 12018 return _bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size, 12019 note->descpos + gregset_offset); 12020 } 12021 12022 /* Gets program and arguments from a core. 12023 prog_off ~ offsetof(prpsinfo | psinfo_t, pr_fname) 12024 comm_off ~ offsetof(prpsinfo | psinfo_t, pr_psargs) */ 12025 12026 static bool 12027 elfcore_grok_solaris_info(bfd *abfd, Elf_Internal_Note* note, 12028 int prog_off, int comm_off) 12029 { 12030 elf_tdata (abfd)->core->program 12031 = _bfd_elfcore_strndup (abfd, note->descdata + prog_off, 16); 12032 elf_tdata (abfd)->core->command 12033 = _bfd_elfcore_strndup (abfd, note->descdata + comm_off, 80); 12034 12035 return true; 12036 } 12037 12038 /* Processes Solaris's LWP status note. 12039 gregset_size ~ sizeof(gregset_t) 12040 gregset_off ~ offsetof(lwpstatus_t, pr_reg) 12041 fpregset_size ~ sizeof(fpregset_t) 12042 fpregset_off ~ offsetof(lwpstatus_t, pr_fpreg) */ 12043 12044 static bool 12045 elfcore_grok_solaris_lwpstatus (bfd *abfd, Elf_Internal_Note* note, 12046 size_t gregset_size, int gregset_off, 12047 size_t fpregset_size, int fpregset_off) 12048 { 12049 asection *sect = NULL; 12050 char reg2_section_name[16] = { 0 }; 12051 12052 (void) snprintf (reg2_section_name, 16, "%s/%i", ".reg2", 12053 elf_tdata (abfd)->core->lwpid); 12054 12055 /* offsetof(lwpstatus_t, pr_lwpid) */ 12056 elf_tdata (abfd)->core->lwpid 12057 = bfd_get_32 (abfd, note->descdata + 4); 12058 /* offsetof(lwpstatus_t, pr_cursig) */ 12059 elf_tdata (abfd)->core->signal 12060 = bfd_get_16 (abfd, note->descdata + 12); 12061 12062 sect = bfd_get_section_by_name (abfd, ".reg"); 12063 if (sect != NULL) 12064 sect->size = gregset_size; 12065 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg", gregset_size, 12066 note->descpos + gregset_off)) 12067 return false; 12068 12069 sect = bfd_get_section_by_name (abfd, reg2_section_name); 12070 if (sect != NULL) 12071 { 12072 sect->size = fpregset_size; 12073 sect->filepos = note->descpos + fpregset_off; 12074 sect->alignment_power = 2; 12075 } 12076 else if (!_bfd_elfcore_make_pseudosection (abfd, ".reg2", fpregset_size, 12077 note->descpos + fpregset_off)) 12078 return false; 12079 12080 return true; 12081 } 12082 12083 static bool 12084 elfcore_grok_solaris_note_impl (bfd *abfd, Elf_Internal_Note *note) 12085 { 12086 if (note == NULL) 12087 return false; 12088 12089 /* core files are identified as 32- or 64-bit, SPARC or x86, 12090 by the size of the descsz which matches the sizeof() 12091 the type appropriate for that note type (e.g., prstatus_t for 12092 SOLARIS_NT_PRSTATUS) for the corresponding architecture 12093 on Solaris. The core file bitness may differ from the bitness of 12094 gdb itself, so fixed values are used instead of sizeof(). 12095 Appropriate fixed offsets are also used to obtain data from 12096 the note. */ 12097 12098 switch ((int) note->type) 12099 { 12100 case SOLARIS_NT_PRSTATUS: 12101 switch (note->descsz) 12102 { 12103 case 508: /* sizeof(prstatus_t) SPARC 32-bit */ 12104 return elfcore_grok_solaris_prstatus(abfd, note, 12105 136, 216, 308, 152, 356); 12106 case 904: /* sizeof(prstatus_t) SPARC 64-bit */ 12107 return elfcore_grok_solaris_prstatus(abfd, note, 12108 264, 360, 520, 304, 600); 12109 case 432: /* sizeof(prstatus_t) Intel 32-bit */ 12110 return elfcore_grok_solaris_prstatus(abfd, note, 12111 136, 216, 308, 76, 356); 12112 case 824: /* sizeof(prstatus_t) Intel 64-bit */ 12113 return elfcore_grok_solaris_prstatus(abfd, note, 12114 264, 360, 520, 224, 600); 12115 default: 12116 return true; 12117 } 12118 12119 case SOLARIS_NT_PSINFO: 12120 case SOLARIS_NT_PRPSINFO: 12121 switch (note->descsz) 12122 { 12123 case 260: /* sizeof(prpsinfo_t) SPARC and Intel 32-bit */ 12124 return elfcore_grok_solaris_info(abfd, note, 84, 100); 12125 case 328: /* sizeof(prpsinfo_t) SPARC and Intel 64-bit */ 12126 return elfcore_grok_solaris_info(abfd, note, 120, 136); 12127 case 360: /* sizeof(psinfo_t) SPARC and Intel 32-bit */ 12128 return elfcore_grok_solaris_info(abfd, note, 88, 104); 12129 case 440: /* sizeof(psinfo_t) SPARC and Intel 64-bit */ 12130 return elfcore_grok_solaris_info(abfd, note, 136, 152); 12131 default: 12132 return true; 12133 } 12134 12135 case SOLARIS_NT_LWPSTATUS: 12136 switch (note->descsz) 12137 { 12138 case 896: /* sizeof(lwpstatus_t) SPARC 32-bit */ 12139 return elfcore_grok_solaris_lwpstatus(abfd, note, 12140 152, 344, 400, 496); 12141 case 1392: /* sizeof(lwpstatus_t) SPARC 64-bit */ 12142 return elfcore_grok_solaris_lwpstatus(abfd, note, 12143 304, 544, 544, 848); 12144 case 800: /* sizeof(lwpstatus_t) Intel 32-bit */ 12145 return elfcore_grok_solaris_lwpstatus(abfd, note, 12146 76, 344, 380, 420); 12147 case 1296: /* sizeof(lwpstatus_t) Intel 64-bit */ 12148 return elfcore_grok_solaris_lwpstatus(abfd, note, 12149 224, 544, 528, 768); 12150 default: 12151 return true; 12152 } 12153 12154 case SOLARIS_NT_LWPSINFO: 12155 /* sizeof(lwpsinfo_t) on 32- and 64-bit, respectively */ 12156 if (note->descsz == 128 || note->descsz == 152) 12157 elf_tdata (abfd)->core->lwpid = 12158 bfd_get_32 (abfd, note->descdata + 4); 12159 break; 12160 12161 default: 12162 break; 12163 } 12164 12165 return true; 12166 } 12167 12168 /* For name starting with "CORE" this may be either a Solaris 12169 core file or a gdb-generated core file. Do Solaris-specific 12170 processing on selected note types first with 12171 elfcore_grok_solaris_note(), then process the note 12172 in elfcore_grok_note(). */ 12173 12174 static bool 12175 elfcore_grok_solaris_note (bfd *abfd, Elf_Internal_Note *note) 12176 { 12177 if (!elfcore_grok_solaris_note_impl (abfd, note)) 12178 return false; 12179 12180 return elfcore_grok_note (abfd, note); 12181 } 12182 12183 static bool 12184 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) 12185 { 12186 if (note->type == NT_OPENBSD_PROCINFO) 12187 return elfcore_grok_openbsd_procinfo (abfd, note); 12188 12189 if (note->type == NT_OPENBSD_REGS) 12190 return elfcore_make_note_pseudosection (abfd, ".reg", note); 12191 12192 if (note->type == NT_OPENBSD_FPREGS) 12193 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 12194 12195 if (note->type == NT_OPENBSD_XFPREGS) 12196 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); 12197 12198 if (note->type == NT_OPENBSD_AUXV) 12199 return elfcore_make_auxv_note_section (abfd, note, 0); 12200 12201 if (note->type == NT_OPENBSD_WCOOKIE) 12202 { 12203 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".wcookie", 12204 SEC_HAS_CONTENTS); 12205 12206 if (sect == NULL) 12207 return false; 12208 sect->size = note->descsz; 12209 sect->filepos = note->descpos; 12210 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 12211 12212 return true; 12213 } 12214 12215 return true; 12216 } 12217 12218 static bool 12219 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) 12220 { 12221 void *ddata = note->descdata; 12222 char buf[100]; 12223 char *name; 12224 asection *sect; 12225 short sig; 12226 unsigned flags; 12227 12228 if (note->descsz < 16) 12229 return false; 12230 12231 /* nto_procfs_status 'pid' field is at offset 0. */ 12232 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); 12233 12234 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ 12235 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); 12236 12237 /* nto_procfs_status 'flags' field is at offset 8. */ 12238 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); 12239 12240 /* nto_procfs_status 'what' field is at offset 14. */ 12241 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) 12242 { 12243 elf_tdata (abfd)->core->signal = sig; 12244 elf_tdata (abfd)->core->lwpid = *tid; 12245 } 12246 12247 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores 12248 do not come from signals so we make sure we set the current 12249 thread just in case. */ 12250 if (flags & 0x00000080) 12251 elf_tdata (abfd)->core->lwpid = *tid; 12252 12253 /* Make a ".qnx_core_status/%d" section. */ 12254 sprintf (buf, ".qnx_core_status/%ld", *tid); 12255 12256 name = (char *) bfd_alloc (abfd, strlen (buf) + 1); 12257 if (name == NULL) 12258 return false; 12259 strcpy (name, buf); 12260 12261 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 12262 if (sect == NULL) 12263 return false; 12264 12265 sect->size = note->descsz; 12266 sect->filepos = note->descpos; 12267 sect->alignment_power = 2; 12268 12269 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); 12270 } 12271 12272 static bool 12273 elfcore_grok_nto_regs (bfd *abfd, 12274 Elf_Internal_Note *note, 12275 long tid, 12276 char *base) 12277 { 12278 char buf[100]; 12279 char *name; 12280 asection *sect; 12281 12282 /* Make a "(base)/%d" section. */ 12283 sprintf (buf, "%s/%ld", base, tid); 12284 12285 name = (char *) bfd_alloc (abfd, strlen (buf) + 1); 12286 if (name == NULL) 12287 return false; 12288 strcpy (name, buf); 12289 12290 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 12291 if (sect == NULL) 12292 return false; 12293 12294 sect->size = note->descsz; 12295 sect->filepos = note->descpos; 12296 sect->alignment_power = 2; 12297 12298 /* This is the current thread. */ 12299 if (elf_tdata (abfd)->core->lwpid == tid) 12300 return elfcore_maybe_make_sect (abfd, base, sect); 12301 12302 return true; 12303 } 12304 12305 static bool 12306 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) 12307 { 12308 /* Every GREG section has a STATUS section before it. Store the 12309 tid from the previous call to pass down to the next gregs 12310 function. */ 12311 static long tid = 1; 12312 12313 switch (note->type) 12314 { 12315 case QNT_CORE_INFO: 12316 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); 12317 case QNT_CORE_STATUS: 12318 return elfcore_grok_nto_status (abfd, note, &tid); 12319 case QNT_CORE_GREG: 12320 return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); 12321 case QNT_CORE_FPREG: 12322 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); 12323 default: 12324 return true; 12325 } 12326 } 12327 12328 static bool 12329 elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) 12330 { 12331 char *name; 12332 asection *sect; 12333 size_t len; 12334 12335 /* Use note name as section name. */ 12336 len = note->namesz; 12337 name = (char *) bfd_alloc (abfd, len); 12338 if (name == NULL) 12339 return false; 12340 memcpy (name, note->namedata, len); 12341 name[len - 1] = '\0'; 12342 12343 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 12344 if (sect == NULL) 12345 return false; 12346 12347 sect->size = note->descsz; 12348 sect->filepos = note->descpos; 12349 sect->alignment_power = 1; 12350 12351 return true; 12352 } 12353 12354 /* Function: elfcore_write_note 12355 12356 Inputs: 12357 buffer to hold note, and current size of buffer 12358 name of note 12359 type of note 12360 data for note 12361 size of data for note 12362 12363 Writes note to end of buffer. ELF64 notes are written exactly as 12364 for ELF32, despite the current (as of 2006) ELF gabi specifying 12365 that they ought to have 8-byte namesz and descsz field, and have 12366 8-byte alignment. Other writers, eg. Linux kernel, do the same. 12367 12368 Return: 12369 Pointer to realloc'd buffer, *BUFSIZ updated. */ 12370 12371 char * 12372 elfcore_write_note (bfd *abfd, 12373 char *buf, 12374 int *bufsiz, 12375 const char *name, 12376 int type, 12377 const void *input, 12378 int size) 12379 { 12380 Elf_External_Note *xnp; 12381 size_t namesz; 12382 size_t newspace; 12383 char *dest; 12384 12385 namesz = 0; 12386 if (name != NULL) 12387 namesz = strlen (name) + 1; 12388 12389 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); 12390 12391 buf = (char *) realloc (buf, *bufsiz + newspace); 12392 if (buf == NULL) 12393 return buf; 12394 dest = buf + *bufsiz; 12395 *bufsiz += newspace; 12396 xnp = (Elf_External_Note *) dest; 12397 H_PUT_32 (abfd, namesz, xnp->namesz); 12398 H_PUT_32 (abfd, size, xnp->descsz); 12399 H_PUT_32 (abfd, type, xnp->type); 12400 dest = xnp->name; 12401 if (name != NULL) 12402 { 12403 memcpy (dest, name, namesz); 12404 dest += namesz; 12405 while (namesz & 3) 12406 { 12407 *dest++ = '\0'; 12408 ++namesz; 12409 } 12410 } 12411 memcpy (dest, input, size); 12412 dest += size; 12413 while (size & 3) 12414 { 12415 *dest++ = '\0'; 12416 ++size; 12417 } 12418 return buf; 12419 } 12420 12421 /* gcc-8 warns (*) on all the strncpy calls in this function about 12422 possible string truncation. The "truncation" is not a bug. We 12423 have an external representation of structs with fields that are not 12424 necessarily NULL terminated and corresponding internal 12425 representation fields that are one larger so that they can always 12426 be NULL terminated. 12427 gcc versions between 4.2 and 4.6 do not allow pragma control of 12428 diagnostics inside functions, giving a hard error if you try to use 12429 the finer control available with later versions. 12430 gcc prior to 4.2 warns about diagnostic push and pop. 12431 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown, 12432 unless you also add #pragma GCC diagnostic ignored "-Wpragma". 12433 (*) Depending on your system header files! */ 12434 #if GCC_VERSION >= 8000 12435 # pragma GCC diagnostic push 12436 # pragma GCC diagnostic ignored "-Wstringop-truncation" 12437 #endif 12438 char * 12439 elfcore_write_prpsinfo (bfd *abfd, 12440 char *buf, 12441 int *bufsiz, 12442 const char *fname, 12443 const char *psargs) 12444 { 12445 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12446 12447 if (bed->elf_backend_write_core_note != NULL) 12448 { 12449 char *ret; 12450 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, 12451 NT_PRPSINFO, fname, psargs); 12452 if (ret != NULL) 12453 return ret; 12454 } 12455 12456 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 12457 # if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) 12458 if (bed->s->elfclass == ELFCLASS32) 12459 { 12460 # if defined (HAVE_PSINFO32_T) 12461 psinfo32_t data; 12462 int note_type = NT_PSINFO; 12463 # else 12464 prpsinfo32_t data; 12465 int note_type = NT_PRPSINFO; 12466 # endif 12467 12468 memset (&data, 0, sizeof (data)); 12469 strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); 12470 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); 12471 return elfcore_write_note (abfd, buf, bufsiz, 12472 "CORE", note_type, &data, sizeof (data)); 12473 } 12474 else 12475 # endif 12476 { 12477 # if defined (HAVE_PSINFO_T) 12478 psinfo_t data; 12479 int note_type = NT_PSINFO; 12480 # else 12481 prpsinfo_t data; 12482 int note_type = NT_PRPSINFO; 12483 # endif 12484 12485 memset (&data, 0, sizeof (data)); 12486 strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); 12487 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); 12488 return elfcore_write_note (abfd, buf, bufsiz, 12489 "CORE", note_type, &data, sizeof (data)); 12490 } 12491 #endif /* PSINFO_T or PRPSINFO_T */ 12492 12493 free (buf); 12494 return NULL; 12495 } 12496 #if GCC_VERSION >= 8000 12497 # pragma GCC diagnostic pop 12498 #endif 12499 12500 char * 12501 elfcore_write_linux_prpsinfo32 12502 (bfd *abfd, char *buf, int *bufsiz, 12503 const struct elf_internal_linux_prpsinfo *prpsinfo) 12504 { 12505 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16) 12506 { 12507 struct elf_external_linux_prpsinfo32_ugid16 data; 12508 12509 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data); 12510 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, 12511 &data, sizeof (data)); 12512 } 12513 else 12514 { 12515 struct elf_external_linux_prpsinfo32_ugid32 data; 12516 12517 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data); 12518 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, 12519 &data, sizeof (data)); 12520 } 12521 } 12522 12523 char * 12524 elfcore_write_linux_prpsinfo64 12525 (bfd *abfd, char *buf, int *bufsiz, 12526 const struct elf_internal_linux_prpsinfo *prpsinfo) 12527 { 12528 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16) 12529 { 12530 struct elf_external_linux_prpsinfo64_ugid16 data; 12531 12532 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data); 12533 return elfcore_write_note (abfd, buf, bufsiz, 12534 "CORE", NT_PRPSINFO, &data, sizeof (data)); 12535 } 12536 else 12537 { 12538 struct elf_external_linux_prpsinfo64_ugid32 data; 12539 12540 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data); 12541 return elfcore_write_note (abfd, buf, bufsiz, 12542 "CORE", NT_PRPSINFO, &data, sizeof (data)); 12543 } 12544 } 12545 12546 char * 12547 elfcore_write_prstatus (bfd *abfd, 12548 char *buf, 12549 int *bufsiz, 12550 long pid, 12551 int cursig, 12552 const void *gregs) 12553 { 12554 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12555 12556 if (bed->elf_backend_write_core_note != NULL) 12557 { 12558 char *ret; 12559 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, 12560 NT_PRSTATUS, 12561 pid, cursig, gregs); 12562 if (ret != NULL) 12563 return ret; 12564 } 12565 12566 #if defined (HAVE_PRSTATUS_T) 12567 #if defined (HAVE_PRSTATUS32_T) 12568 if (bed->s->elfclass == ELFCLASS32) 12569 { 12570 prstatus32_t prstat; 12571 12572 memset (&prstat, 0, sizeof (prstat)); 12573 prstat.pr_pid = pid; 12574 prstat.pr_cursig = cursig; 12575 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); 12576 return elfcore_write_note (abfd, buf, bufsiz, "CORE", 12577 NT_PRSTATUS, &prstat, sizeof (prstat)); 12578 } 12579 else 12580 #endif 12581 { 12582 prstatus_t prstat; 12583 12584 memset (&prstat, 0, sizeof (prstat)); 12585 prstat.pr_pid = pid; 12586 prstat.pr_cursig = cursig; 12587 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); 12588 return elfcore_write_note (abfd, buf, bufsiz, "CORE", 12589 NT_PRSTATUS, &prstat, sizeof (prstat)); 12590 } 12591 #endif /* HAVE_PRSTATUS_T */ 12592 12593 free (buf); 12594 return NULL; 12595 } 12596 12597 #if defined (HAVE_LWPSTATUS_T) 12598 char * 12599 elfcore_write_lwpstatus (bfd *abfd, 12600 char *buf, 12601 int *bufsiz, 12602 long pid, 12603 int cursig, 12604 const void *gregs) 12605 { 12606 lwpstatus_t lwpstat; 12607 const char *note_name = "CORE"; 12608 12609 memset (&lwpstat, 0, sizeof (lwpstat)); 12610 lwpstat.pr_lwpid = pid >> 16; 12611 lwpstat.pr_cursig = cursig; 12612 #if defined (HAVE_LWPSTATUS_T_PR_REG) 12613 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); 12614 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 12615 #if !defined(gregs) 12616 memcpy (lwpstat.pr_context.uc_mcontext.gregs, 12617 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); 12618 #else 12619 memcpy (lwpstat.pr_context.uc_mcontext.__gregs, 12620 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); 12621 #endif 12622 #endif 12623 return elfcore_write_note (abfd, buf, bufsiz, note_name, 12624 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); 12625 } 12626 #endif /* HAVE_LWPSTATUS_T */ 12627 12628 #if defined (HAVE_PSTATUS_T) 12629 char * 12630 elfcore_write_pstatus (bfd *abfd, 12631 char *buf, 12632 int *bufsiz, 12633 long pid, 12634 int cursig ATTRIBUTE_UNUSED, 12635 const void *gregs ATTRIBUTE_UNUSED) 12636 { 12637 const char *note_name = "CORE"; 12638 #if defined (HAVE_PSTATUS32_T) 12639 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12640 12641 if (bed->s->elfclass == ELFCLASS32) 12642 { 12643 pstatus32_t pstat; 12644 12645 memset (&pstat, 0, sizeof (pstat)); 12646 pstat.pr_pid = pid & 0xffff; 12647 buf = elfcore_write_note (abfd, buf, bufsiz, note_name, 12648 NT_PSTATUS, &pstat, sizeof (pstat)); 12649 return buf; 12650 } 12651 else 12652 #endif 12653 { 12654 pstatus_t pstat; 12655 12656 memset (&pstat, 0, sizeof (pstat)); 12657 pstat.pr_pid = pid & 0xffff; 12658 buf = elfcore_write_note (abfd, buf, bufsiz, note_name, 12659 NT_PSTATUS, &pstat, sizeof (pstat)); 12660 return buf; 12661 } 12662 } 12663 #endif /* HAVE_PSTATUS_T */ 12664 12665 char * 12666 elfcore_write_prfpreg (bfd *abfd, 12667 char *buf, 12668 int *bufsiz, 12669 const void *fpregs, 12670 int size) 12671 { 12672 const char *note_name = "CORE"; 12673 return elfcore_write_note (abfd, buf, bufsiz, 12674 note_name, NT_FPREGSET, fpregs, size); 12675 } 12676 12677 char * 12678 elfcore_write_prxfpreg (bfd *abfd, 12679 char *buf, 12680 int *bufsiz, 12681 const void *xfpregs, 12682 int size) 12683 { 12684 char *note_name = "LINUX"; 12685 return elfcore_write_note (abfd, buf, bufsiz, 12686 note_name, NT_PRXFPREG, xfpregs, size); 12687 } 12688 12689 char * 12690 elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, 12691 const void *xfpregs, int size) 12692 { 12693 char *note_name; 12694 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) 12695 note_name = "FreeBSD"; 12696 else 12697 note_name = "LINUX"; 12698 return elfcore_write_note (abfd, buf, bufsiz, 12699 note_name, NT_X86_XSTATE, xfpregs, size); 12700 } 12701 12702 char * 12703 elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz, 12704 const void *regs, int size) 12705 { 12706 char *note_name = "FreeBSD"; 12707 return elfcore_write_note (abfd, buf, bufsiz, 12708 note_name, NT_FREEBSD_X86_SEGBASES, regs, size); 12709 } 12710 12711 char * 12712 elfcore_write_ppc_vmx (bfd *abfd, 12713 char *buf, 12714 int *bufsiz, 12715 const void *ppc_vmx, 12716 int size) 12717 { 12718 char *note_name = "LINUX"; 12719 return elfcore_write_note (abfd, buf, bufsiz, 12720 note_name, NT_PPC_VMX, ppc_vmx, size); 12721 } 12722 12723 char * 12724 elfcore_write_ppc_vsx (bfd *abfd, 12725 char *buf, 12726 int *bufsiz, 12727 const void *ppc_vsx, 12728 int size) 12729 { 12730 char *note_name = "LINUX"; 12731 return elfcore_write_note (abfd, buf, bufsiz, 12732 note_name, NT_PPC_VSX, ppc_vsx, size); 12733 } 12734 12735 char * 12736 elfcore_write_ppc_tar (bfd *abfd, 12737 char *buf, 12738 int *bufsiz, 12739 const void *ppc_tar, 12740 int size) 12741 { 12742 char *note_name = "LINUX"; 12743 return elfcore_write_note (abfd, buf, bufsiz, 12744 note_name, NT_PPC_TAR, ppc_tar, size); 12745 } 12746 12747 char * 12748 elfcore_write_ppc_ppr (bfd *abfd, 12749 char *buf, 12750 int *bufsiz, 12751 const void *ppc_ppr, 12752 int size) 12753 { 12754 char *note_name = "LINUX"; 12755 return elfcore_write_note (abfd, buf, bufsiz, 12756 note_name, NT_PPC_PPR, ppc_ppr, size); 12757 } 12758 12759 char * 12760 elfcore_write_ppc_dscr (bfd *abfd, 12761 char *buf, 12762 int *bufsiz, 12763 const void *ppc_dscr, 12764 int size) 12765 { 12766 char *note_name = "LINUX"; 12767 return elfcore_write_note (abfd, buf, bufsiz, 12768 note_name, NT_PPC_DSCR, ppc_dscr, size); 12769 } 12770 12771 char * 12772 elfcore_write_ppc_ebb (bfd *abfd, 12773 char *buf, 12774 int *bufsiz, 12775 const void *ppc_ebb, 12776 int size) 12777 { 12778 char *note_name = "LINUX"; 12779 return elfcore_write_note (abfd, buf, bufsiz, 12780 note_name, NT_PPC_EBB, ppc_ebb, size); 12781 } 12782 12783 char * 12784 elfcore_write_ppc_pmu (bfd *abfd, 12785 char *buf, 12786 int *bufsiz, 12787 const void *ppc_pmu, 12788 int size) 12789 { 12790 char *note_name = "LINUX"; 12791 return elfcore_write_note (abfd, buf, bufsiz, 12792 note_name, NT_PPC_PMU, ppc_pmu, size); 12793 } 12794 12795 char * 12796 elfcore_write_ppc_tm_cgpr (bfd *abfd, 12797 char *buf, 12798 int *bufsiz, 12799 const void *ppc_tm_cgpr, 12800 int size) 12801 { 12802 char *note_name = "LINUX"; 12803 return elfcore_write_note (abfd, buf, bufsiz, 12804 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size); 12805 } 12806 12807 char * 12808 elfcore_write_ppc_tm_cfpr (bfd *abfd, 12809 char *buf, 12810 int *bufsiz, 12811 const void *ppc_tm_cfpr, 12812 int size) 12813 { 12814 char *note_name = "LINUX"; 12815 return elfcore_write_note (abfd, buf, bufsiz, 12816 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size); 12817 } 12818 12819 char * 12820 elfcore_write_ppc_tm_cvmx (bfd *abfd, 12821 char *buf, 12822 int *bufsiz, 12823 const void *ppc_tm_cvmx, 12824 int size) 12825 { 12826 char *note_name = "LINUX"; 12827 return elfcore_write_note (abfd, buf, bufsiz, 12828 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size); 12829 } 12830 12831 char * 12832 elfcore_write_ppc_tm_cvsx (bfd *abfd, 12833 char *buf, 12834 int *bufsiz, 12835 const void *ppc_tm_cvsx, 12836 int size) 12837 { 12838 char *note_name = "LINUX"; 12839 return elfcore_write_note (abfd, buf, bufsiz, 12840 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size); 12841 } 12842 12843 char * 12844 elfcore_write_ppc_tm_spr (bfd *abfd, 12845 char *buf, 12846 int *bufsiz, 12847 const void *ppc_tm_spr, 12848 int size) 12849 { 12850 char *note_name = "LINUX"; 12851 return elfcore_write_note (abfd, buf, bufsiz, 12852 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size); 12853 } 12854 12855 char * 12856 elfcore_write_ppc_tm_ctar (bfd *abfd, 12857 char *buf, 12858 int *bufsiz, 12859 const void *ppc_tm_ctar, 12860 int size) 12861 { 12862 char *note_name = "LINUX"; 12863 return elfcore_write_note (abfd, buf, bufsiz, 12864 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size); 12865 } 12866 12867 char * 12868 elfcore_write_ppc_tm_cppr (bfd *abfd, 12869 char *buf, 12870 int *bufsiz, 12871 const void *ppc_tm_cppr, 12872 int size) 12873 { 12874 char *note_name = "LINUX"; 12875 return elfcore_write_note (abfd, buf, bufsiz, 12876 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size); 12877 } 12878 12879 char * 12880 elfcore_write_ppc_tm_cdscr (bfd *abfd, 12881 char *buf, 12882 int *bufsiz, 12883 const void *ppc_tm_cdscr, 12884 int size) 12885 { 12886 char *note_name = "LINUX"; 12887 return elfcore_write_note (abfd, buf, bufsiz, 12888 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size); 12889 } 12890 12891 static char * 12892 elfcore_write_s390_high_gprs (bfd *abfd, 12893 char *buf, 12894 int *bufsiz, 12895 const void *s390_high_gprs, 12896 int size) 12897 { 12898 char *note_name = "LINUX"; 12899 return elfcore_write_note (abfd, buf, bufsiz, 12900 note_name, NT_S390_HIGH_GPRS, 12901 s390_high_gprs, size); 12902 } 12903 12904 char * 12905 elfcore_write_s390_timer (bfd *abfd, 12906 char *buf, 12907 int *bufsiz, 12908 const void *s390_timer, 12909 int size) 12910 { 12911 char *note_name = "LINUX"; 12912 return elfcore_write_note (abfd, buf, bufsiz, 12913 note_name, NT_S390_TIMER, s390_timer, size); 12914 } 12915 12916 char * 12917 elfcore_write_s390_todcmp (bfd *abfd, 12918 char *buf, 12919 int *bufsiz, 12920 const void *s390_todcmp, 12921 int size) 12922 { 12923 char *note_name = "LINUX"; 12924 return elfcore_write_note (abfd, buf, bufsiz, 12925 note_name, NT_S390_TODCMP, s390_todcmp, size); 12926 } 12927 12928 char * 12929 elfcore_write_s390_todpreg (bfd *abfd, 12930 char *buf, 12931 int *bufsiz, 12932 const void *s390_todpreg, 12933 int size) 12934 { 12935 char *note_name = "LINUX"; 12936 return elfcore_write_note (abfd, buf, bufsiz, 12937 note_name, NT_S390_TODPREG, s390_todpreg, size); 12938 } 12939 12940 char * 12941 elfcore_write_s390_ctrs (bfd *abfd, 12942 char *buf, 12943 int *bufsiz, 12944 const void *s390_ctrs, 12945 int size) 12946 { 12947 char *note_name = "LINUX"; 12948 return elfcore_write_note (abfd, buf, bufsiz, 12949 note_name, NT_S390_CTRS, s390_ctrs, size); 12950 } 12951 12952 char * 12953 elfcore_write_s390_prefix (bfd *abfd, 12954 char *buf, 12955 int *bufsiz, 12956 const void *s390_prefix, 12957 int size) 12958 { 12959 char *note_name = "LINUX"; 12960 return elfcore_write_note (abfd, buf, bufsiz, 12961 note_name, NT_S390_PREFIX, s390_prefix, size); 12962 } 12963 12964 char * 12965 elfcore_write_s390_last_break (bfd *abfd, 12966 char *buf, 12967 int *bufsiz, 12968 const void *s390_last_break, 12969 int size) 12970 { 12971 char *note_name = "LINUX"; 12972 return elfcore_write_note (abfd, buf, bufsiz, 12973 note_name, NT_S390_LAST_BREAK, 12974 s390_last_break, size); 12975 } 12976 12977 char * 12978 elfcore_write_s390_system_call (bfd *abfd, 12979 char *buf, 12980 int *bufsiz, 12981 const void *s390_system_call, 12982 int size) 12983 { 12984 char *note_name = "LINUX"; 12985 return elfcore_write_note (abfd, buf, bufsiz, 12986 note_name, NT_S390_SYSTEM_CALL, 12987 s390_system_call, size); 12988 } 12989 12990 char * 12991 elfcore_write_s390_tdb (bfd *abfd, 12992 char *buf, 12993 int *bufsiz, 12994 const void *s390_tdb, 12995 int size) 12996 { 12997 char *note_name = "LINUX"; 12998 return elfcore_write_note (abfd, buf, bufsiz, 12999 note_name, NT_S390_TDB, s390_tdb, size); 13000 } 13001 13002 char * 13003 elfcore_write_s390_vxrs_low (bfd *abfd, 13004 char *buf, 13005 int *bufsiz, 13006 const void *s390_vxrs_low, 13007 int size) 13008 { 13009 char *note_name = "LINUX"; 13010 return elfcore_write_note (abfd, buf, bufsiz, 13011 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); 13012 } 13013 13014 char * 13015 elfcore_write_s390_vxrs_high (bfd *abfd, 13016 char *buf, 13017 int *bufsiz, 13018 const void *s390_vxrs_high, 13019 int size) 13020 { 13021 char *note_name = "LINUX"; 13022 return elfcore_write_note (abfd, buf, bufsiz, 13023 note_name, NT_S390_VXRS_HIGH, 13024 s390_vxrs_high, size); 13025 } 13026 13027 char * 13028 elfcore_write_s390_gs_cb (bfd *abfd, 13029 char *buf, 13030 int *bufsiz, 13031 const void *s390_gs_cb, 13032 int size) 13033 { 13034 char *note_name = "LINUX"; 13035 return elfcore_write_note (abfd, buf, bufsiz, 13036 note_name, NT_S390_GS_CB, 13037 s390_gs_cb, size); 13038 } 13039 13040 char * 13041 elfcore_write_s390_gs_bc (bfd *abfd, 13042 char *buf, 13043 int *bufsiz, 13044 const void *s390_gs_bc, 13045 int size) 13046 { 13047 char *note_name = "LINUX"; 13048 return elfcore_write_note (abfd, buf, bufsiz, 13049 note_name, NT_S390_GS_BC, 13050 s390_gs_bc, size); 13051 } 13052 13053 char * 13054 elfcore_write_arm_vfp (bfd *abfd, 13055 char *buf, 13056 int *bufsiz, 13057 const void *arm_vfp, 13058 int size) 13059 { 13060 char *note_name = "LINUX"; 13061 return elfcore_write_note (abfd, buf, bufsiz, 13062 note_name, NT_ARM_VFP, arm_vfp, size); 13063 } 13064 13065 char * 13066 elfcore_write_aarch_tls (bfd *abfd, 13067 char *buf, 13068 int *bufsiz, 13069 const void *aarch_tls, 13070 int size) 13071 { 13072 char *note_name = "LINUX"; 13073 return elfcore_write_note (abfd, buf, bufsiz, 13074 note_name, NT_ARM_TLS, aarch_tls, size); 13075 } 13076 13077 char * 13078 elfcore_write_aarch_hw_break (bfd *abfd, 13079 char *buf, 13080 int *bufsiz, 13081 const void *aarch_hw_break, 13082 int size) 13083 { 13084 char *note_name = "LINUX"; 13085 return elfcore_write_note (abfd, buf, bufsiz, 13086 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); 13087 } 13088 13089 char * 13090 elfcore_write_aarch_hw_watch (bfd *abfd, 13091 char *buf, 13092 int *bufsiz, 13093 const void *aarch_hw_watch, 13094 int size) 13095 { 13096 char *note_name = "LINUX"; 13097 return elfcore_write_note (abfd, buf, bufsiz, 13098 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); 13099 } 13100 13101 char * 13102 elfcore_write_aarch_sve (bfd *abfd, 13103 char *buf, 13104 int *bufsiz, 13105 const void *aarch_sve, 13106 int size) 13107 { 13108 char *note_name = "LINUX"; 13109 return elfcore_write_note (abfd, buf, bufsiz, 13110 note_name, NT_ARM_SVE, aarch_sve, size); 13111 } 13112 13113 char * 13114 elfcore_write_aarch_pauth (bfd *abfd, 13115 char *buf, 13116 int *bufsiz, 13117 const void *aarch_pauth, 13118 int size) 13119 { 13120 char *note_name = "LINUX"; 13121 return elfcore_write_note (abfd, buf, bufsiz, 13122 note_name, NT_ARM_PAC_MASK, aarch_pauth, size); 13123 } 13124 13125 char * 13126 elfcore_write_aarch_mte (bfd *abfd, 13127 char *buf, 13128 int *bufsiz, 13129 const void *aarch_mte, 13130 int size) 13131 { 13132 char *note_name = "LINUX"; 13133 return elfcore_write_note (abfd, buf, bufsiz, 13134 note_name, NT_ARM_TAGGED_ADDR_CTRL, 13135 aarch_mte, 13136 size); 13137 } 13138 13139 char * 13140 elfcore_write_aarch_ssve (bfd *abfd, 13141 char *buf, 13142 int *bufsiz, 13143 const void *aarch_ssve, 13144 int size) 13145 { 13146 char *note_name = "LINUX"; 13147 return elfcore_write_note (abfd, buf, bufsiz, 13148 note_name, NT_ARM_SSVE, 13149 aarch_ssve, 13150 size); 13151 } 13152 13153 char * 13154 elfcore_write_aarch_za (bfd *abfd, 13155 char *buf, 13156 int *bufsiz, 13157 const void *aarch_za, 13158 int size) 13159 { 13160 char *note_name = "LINUX"; 13161 return elfcore_write_note (abfd, buf, bufsiz, 13162 note_name, NT_ARM_ZA, 13163 aarch_za, 13164 size); 13165 } 13166 13167 /* Write the buffer of zt register values in aarch_zt (length SIZE) into 13168 the note buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being 13169 written into. Return a pointer to the new start of the note buffer, to 13170 replace BUF which may no longer be valid. */ 13171 13172 char * 13173 elfcore_write_aarch_zt (bfd *abfd, 13174 char *buf, 13175 int *bufsiz, 13176 const void *aarch_zt, 13177 int size) 13178 { 13179 char *note_name = "LINUX"; 13180 return elfcore_write_note (abfd, buf, bufsiz, 13181 note_name, NT_ARM_ZT, 13182 aarch_zt, 13183 size); 13184 } 13185 13186 char * 13187 elfcore_write_arc_v2 (bfd *abfd, 13188 char *buf, 13189 int *bufsiz, 13190 const void *arc_v2, 13191 int size) 13192 { 13193 char *note_name = "LINUX"; 13194 return elfcore_write_note (abfd, buf, bufsiz, 13195 note_name, NT_ARC_V2, arc_v2, size); 13196 } 13197 13198 char * 13199 elfcore_write_loongarch_cpucfg (bfd *abfd, 13200 char *buf, 13201 int *bufsiz, 13202 const void *loongarch_cpucfg, 13203 int size) 13204 { 13205 char *note_name = "LINUX"; 13206 return elfcore_write_note (abfd, buf, bufsiz, 13207 note_name, NT_LARCH_CPUCFG, 13208 loongarch_cpucfg, size); 13209 } 13210 13211 char * 13212 elfcore_write_loongarch_lbt (bfd *abfd, 13213 char *buf, 13214 int *bufsiz, 13215 const void *loongarch_lbt, 13216 int size) 13217 { 13218 char *note_name = "LINUX"; 13219 return elfcore_write_note (abfd, buf, bufsiz, 13220 note_name, NT_LARCH_LBT, loongarch_lbt, size); 13221 } 13222 13223 char * 13224 elfcore_write_loongarch_lsx (bfd *abfd, 13225 char *buf, 13226 int *bufsiz, 13227 const void *loongarch_lsx, 13228 int size) 13229 { 13230 char *note_name = "LINUX"; 13231 return elfcore_write_note (abfd, buf, bufsiz, 13232 note_name, NT_LARCH_LSX, loongarch_lsx, size); 13233 } 13234 13235 char * 13236 elfcore_write_loongarch_lasx (bfd *abfd, 13237 char *buf, 13238 int *bufsiz, 13239 const void *loongarch_lasx, 13240 int size) 13241 { 13242 char *note_name = "LINUX"; 13243 return elfcore_write_note (abfd, buf, bufsiz, 13244 note_name, NT_LARCH_LASX, loongarch_lasx, size); 13245 } 13246 13247 /* Write the buffer of csr values in CSRS (length SIZE) into the note 13248 buffer BUF and update *BUFSIZ. ABFD is the bfd the note is being 13249 written into. Return a pointer to the new start of the note buffer, to 13250 replace BUF which may no longer be valid. */ 13251 13252 char * 13253 elfcore_write_riscv_csr (bfd *abfd, 13254 char *buf, 13255 int *bufsiz, 13256 const void *csrs, 13257 int size) 13258 { 13259 const char *note_name = "GDB"; 13260 return elfcore_write_note (abfd, buf, bufsiz, 13261 note_name, NT_RISCV_CSR, csrs, size); 13262 } 13263 13264 /* Write the target description (a string) pointed to by TDESC, length 13265 SIZE, into the note buffer BUF, and update *BUFSIZ. ABFD is the bfd the 13266 note is being written into. Return a pointer to the new start of the 13267 note buffer, to replace BUF which may no longer be valid. */ 13268 13269 char * 13270 elfcore_write_gdb_tdesc (bfd *abfd, 13271 char *buf, 13272 int *bufsiz, 13273 const void *tdesc, 13274 int size) 13275 { 13276 const char *note_name = "GDB"; 13277 return elfcore_write_note (abfd, buf, bufsiz, 13278 note_name, NT_GDB_TDESC, tdesc, size); 13279 } 13280 13281 char * 13282 elfcore_write_register_note (bfd *abfd, 13283 char *buf, 13284 int *bufsiz, 13285 const char *section, 13286 const void *data, 13287 int size) 13288 { 13289 if (strcmp (section, ".reg2") == 0) 13290 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); 13291 if (strcmp (section, ".reg-xfp") == 0) 13292 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); 13293 if (strcmp (section, ".reg-xstate") == 0) 13294 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); 13295 if (strcmp (section, ".reg-x86-segbases") == 0) 13296 return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size); 13297 if (strcmp (section, ".reg-ppc-vmx") == 0) 13298 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); 13299 if (strcmp (section, ".reg-ppc-vsx") == 0) 13300 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); 13301 if (strcmp (section, ".reg-ppc-tar") == 0) 13302 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size); 13303 if (strcmp (section, ".reg-ppc-ppr") == 0) 13304 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size); 13305 if (strcmp (section, ".reg-ppc-dscr") == 0) 13306 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size); 13307 if (strcmp (section, ".reg-ppc-ebb") == 0) 13308 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size); 13309 if (strcmp (section, ".reg-ppc-pmu") == 0) 13310 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size); 13311 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0) 13312 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size); 13313 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0) 13314 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size); 13315 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0) 13316 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size); 13317 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0) 13318 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size); 13319 if (strcmp (section, ".reg-ppc-tm-spr") == 0) 13320 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size); 13321 if (strcmp (section, ".reg-ppc-tm-ctar") == 0) 13322 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size); 13323 if (strcmp (section, ".reg-ppc-tm-cppr") == 0) 13324 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size); 13325 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0) 13326 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size); 13327 if (strcmp (section, ".reg-s390-high-gprs") == 0) 13328 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); 13329 if (strcmp (section, ".reg-s390-timer") == 0) 13330 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); 13331 if (strcmp (section, ".reg-s390-todcmp") == 0) 13332 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); 13333 if (strcmp (section, ".reg-s390-todpreg") == 0) 13334 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); 13335 if (strcmp (section, ".reg-s390-ctrs") == 0) 13336 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); 13337 if (strcmp (section, ".reg-s390-prefix") == 0) 13338 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); 13339 if (strcmp (section, ".reg-s390-last-break") == 0) 13340 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); 13341 if (strcmp (section, ".reg-s390-system-call") == 0) 13342 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); 13343 if (strcmp (section, ".reg-s390-tdb") == 0) 13344 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); 13345 if (strcmp (section, ".reg-s390-vxrs-low") == 0) 13346 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); 13347 if (strcmp (section, ".reg-s390-vxrs-high") == 0) 13348 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); 13349 if (strcmp (section, ".reg-s390-gs-cb") == 0) 13350 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size); 13351 if (strcmp (section, ".reg-s390-gs-bc") == 0) 13352 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size); 13353 if (strcmp (section, ".reg-arm-vfp") == 0) 13354 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); 13355 if (strcmp (section, ".reg-aarch-tls") == 0) 13356 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); 13357 if (strcmp (section, ".reg-aarch-hw-break") == 0) 13358 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); 13359 if (strcmp (section, ".reg-aarch-hw-watch") == 0) 13360 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); 13361 if (strcmp (section, ".reg-aarch-sve") == 0) 13362 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size); 13363 if (strcmp (section, ".reg-aarch-pauth") == 0) 13364 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size); 13365 if (strcmp (section, ".reg-aarch-mte") == 0) 13366 return elfcore_write_aarch_mte (abfd, buf, bufsiz, data, size); 13367 if (strcmp (section, ".reg-aarch-ssve") == 0) 13368 return elfcore_write_aarch_ssve (abfd, buf, bufsiz, data, size); 13369 if (strcmp (section, ".reg-aarch-za") == 0) 13370 return elfcore_write_aarch_za (abfd, buf, bufsiz, data, size); 13371 if (strcmp (section, ".reg-aarch-zt") == 0) 13372 return elfcore_write_aarch_zt (abfd, buf, bufsiz, data, size); 13373 if (strcmp (section, ".reg-arc-v2") == 0) 13374 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size); 13375 if (strcmp (section, ".gdb-tdesc") == 0) 13376 return elfcore_write_gdb_tdesc (abfd, buf, bufsiz, data, size); 13377 if (strcmp (section, ".reg-riscv-csr") == 0) 13378 return elfcore_write_riscv_csr (abfd, buf, bufsiz, data, size); 13379 if (strcmp (section, ".reg-loongarch-cpucfg") == 0) 13380 return elfcore_write_loongarch_cpucfg (abfd, buf, bufsiz, data, size); 13381 if (strcmp (section, ".reg-loongarch-lbt") == 0) 13382 return elfcore_write_loongarch_lbt (abfd, buf, bufsiz, data, size); 13383 if (strcmp (section, ".reg-loongarch-lsx") == 0) 13384 return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size); 13385 if (strcmp (section, ".reg-loongarch-lasx") == 0) 13386 return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size); 13387 return NULL; 13388 } 13389 13390 char * 13391 elfcore_write_file_note (bfd *obfd, char *note_data, int *note_size, 13392 const void *buf, int bufsiz) 13393 { 13394 return elfcore_write_note (obfd, note_data, note_size, 13395 "CORE", NT_FILE, buf, bufsiz); 13396 } 13397 13398 static bool 13399 elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset, 13400 size_t align) 13401 { 13402 char *p; 13403 13404 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1. 13405 gABI specifies that PT_NOTE alignment should be aligned to 4 13406 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If 13407 align is less than 4, we use 4 byte alignment. */ 13408 if (align < 4) 13409 align = 4; 13410 if (align != 4 && align != 8) 13411 return false; 13412 13413 p = buf; 13414 while (p < buf + size) 13415 { 13416 Elf_External_Note *xnp = (Elf_External_Note *) p; 13417 Elf_Internal_Note in; 13418 13419 if (offsetof (Elf_External_Note, name) > buf - p + size) 13420 return false; 13421 13422 in.type = H_GET_32 (abfd, xnp->type); 13423 13424 in.namesz = H_GET_32 (abfd, xnp->namesz); 13425 in.namedata = xnp->name; 13426 if (in.namesz > buf - in.namedata + size) 13427 return false; 13428 13429 in.descsz = H_GET_32 (abfd, xnp->descsz); 13430 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align); 13431 in.descpos = offset + (in.descdata - buf); 13432 if (in.descsz != 0 13433 && (in.descdata >= buf + size 13434 || in.descsz > buf - in.descdata + size)) 13435 return false; 13436 13437 switch (bfd_get_format (abfd)) 13438 { 13439 default: 13440 return true; 13441 13442 case bfd_core: 13443 { 13444 #define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} 13445 struct 13446 { 13447 const char * string; 13448 size_t len; 13449 bool (*func) (bfd *, Elf_Internal_Note *); 13450 } 13451 grokers[] = 13452 { 13453 GROKER_ELEMENT ("", elfcore_grok_note), 13454 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), 13455 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), 13456 GROKER_ELEMENT ("OpenBSD", elfcore_grok_openbsd_note), 13457 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), 13458 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note), 13459 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note), 13460 GROKER_ELEMENT ("CORE", elfcore_grok_solaris_note) 13461 }; 13462 #undef GROKER_ELEMENT 13463 int i; 13464 13465 for (i = ARRAY_SIZE (grokers); i--;) 13466 { 13467 if (in.namesz >= grokers[i].len 13468 && strncmp (in.namedata, grokers[i].string, 13469 grokers[i].len) == 0) 13470 { 13471 if (! grokers[i].func (abfd, & in)) 13472 return false; 13473 break; 13474 } 13475 } 13476 break; 13477 } 13478 13479 case bfd_object: 13480 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) 13481 { 13482 if (! elfobj_grok_gnu_note (abfd, &in)) 13483 return false; 13484 } 13485 else if (in.namesz == sizeof "stapsdt" 13486 && strcmp (in.namedata, "stapsdt") == 0) 13487 { 13488 if (! elfobj_grok_stapsdt_note (abfd, &in)) 13489 return false; 13490 } 13491 break; 13492 } 13493 13494 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align); 13495 } 13496 13497 return true; 13498 } 13499 13500 bool 13501 elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size, 13502 size_t align) 13503 { 13504 char *buf; 13505 13506 if (size == 0 || (size + 1) == 0) 13507 return true; 13508 13509 if (bfd_seek (abfd, offset, SEEK_SET) != 0) 13510 return false; 13511 13512 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size); 13513 if (buf == NULL) 13514 return false; 13515 13516 /* PR 17512: file: ec08f814 13517 0-termintate the buffer so that string searches will not overflow. */ 13518 buf[size] = 0; 13519 13520 if (!elf_parse_notes (abfd, buf, size, offset, align)) 13521 { 13522 free (buf); 13523 return false; 13524 } 13525 13526 free (buf); 13527 return true; 13528 } 13529 13530 /* Providing external access to the ELF program header table. */ 13531 13532 /* Return an upper bound on the number of bytes required to store a 13533 copy of ABFD's program header table entries. Return -1 if an error 13534 occurs; bfd_get_error will return an appropriate code. */ 13535 13536 long 13537 bfd_get_elf_phdr_upper_bound (bfd *abfd) 13538 { 13539 if (abfd->xvec->flavour != bfd_target_elf_flavour) 13540 { 13541 bfd_set_error (bfd_error_wrong_format); 13542 return -1; 13543 } 13544 13545 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); 13546 } 13547 13548 /* Copy ABFD's program header table entries to *PHDRS. The entries 13549 will be stored as an array of Elf_Internal_Phdr structures, as 13550 defined in include/elf/internal.h. To find out how large the 13551 buffer needs to be, call bfd_get_elf_phdr_upper_bound. 13552 13553 Return the number of program header table entries read, or -1 if an 13554 error occurs; bfd_get_error will return an appropriate code. */ 13555 13556 int 13557 bfd_get_elf_phdrs (bfd *abfd, void *phdrs) 13558 { 13559 int num_phdrs; 13560 13561 if (abfd->xvec->flavour != bfd_target_elf_flavour) 13562 { 13563 bfd_set_error (bfd_error_wrong_format); 13564 return -1; 13565 } 13566 13567 num_phdrs = elf_elfheader (abfd)->e_phnum; 13568 if (num_phdrs != 0) 13569 memcpy (phdrs, elf_tdata (abfd)->phdr, 13570 num_phdrs * sizeof (Elf_Internal_Phdr)); 13571 13572 return num_phdrs; 13573 } 13574 13575 enum elf_reloc_type_class 13576 _bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, 13577 const asection *rel_sec ATTRIBUTE_UNUSED, 13578 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) 13579 { 13580 return reloc_class_normal; 13581 } 13582 13583 /* For RELA architectures, return the relocation value for a 13584 relocation against a local symbol. */ 13585 13586 bfd_vma 13587 _bfd_elf_rela_local_sym (bfd *abfd, 13588 Elf_Internal_Sym *sym, 13589 asection **psec, 13590 Elf_Internal_Rela *rel) 13591 { 13592 asection *sec = *psec; 13593 bfd_vma relocation; 13594 13595 relocation = (sec->output_section->vma 13596 + sec->output_offset 13597 + sym->st_value); 13598 if ((sec->flags & SEC_MERGE) 13599 && ELF_ST_TYPE (sym->st_info) == STT_SECTION 13600 && sec->sec_info_type == SEC_INFO_TYPE_MERGE) 13601 { 13602 rel->r_addend = 13603 _bfd_merged_section_offset (abfd, psec, 13604 elf_section_data (sec)->sec_info, 13605 sym->st_value + rel->r_addend); 13606 if (sec != *psec) 13607 { 13608 /* If we have changed the section, and our original section is 13609 marked with SEC_EXCLUDE, it means that the original 13610 SEC_MERGE section has been completely subsumed in some 13611 other SEC_MERGE section. In this case, we need to leave 13612 some info around for --emit-relocs. */ 13613 if ((sec->flags & SEC_EXCLUDE) != 0) 13614 sec->kept_section = *psec; 13615 sec = *psec; 13616 } 13617 rel->r_addend -= relocation; 13618 rel->r_addend += sec->output_section->vma + sec->output_offset; 13619 } 13620 return relocation; 13621 } 13622 13623 bfd_vma 13624 _bfd_elf_rel_local_sym (bfd *abfd, 13625 Elf_Internal_Sym *sym, 13626 asection **psec, 13627 bfd_vma addend) 13628 { 13629 asection *sec = *psec; 13630 13631 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) 13632 return sym->st_value + addend; 13633 13634 return _bfd_merged_section_offset (abfd, psec, 13635 elf_section_data (sec)->sec_info, 13636 sym->st_value + addend); 13637 } 13638 13639 /* Adjust an address within a section. Given OFFSET within SEC, return 13640 the new offset within the section, based upon changes made to the 13641 section. Returns -1 if the offset is now invalid. 13642 The offset (in abnd out) is in target sized bytes, however big a 13643 byte may be. */ 13644 13645 bfd_vma 13646 _bfd_elf_section_offset (bfd *abfd, 13647 struct bfd_link_info *info, 13648 asection *sec, 13649 bfd_vma offset) 13650 { 13651 switch (sec->sec_info_type) 13652 { 13653 case SEC_INFO_TYPE_STABS: 13654 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, 13655 offset); 13656 case SEC_INFO_TYPE_EH_FRAME: 13657 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); 13658 13659 default: 13660 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) 13661 { 13662 /* Reverse the offset. */ 13663 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13664 bfd_size_type address_size = bed->s->arch_size / 8; 13665 13666 /* address_size and sec->size are in octets. Convert 13667 to bytes before subtracting the original offset. */ 13668 offset = ((sec->size - address_size) 13669 / bfd_octets_per_byte (abfd, sec) - offset); 13670 } 13671 return offset; 13672 } 13673 } 13674 13675 long 13676 _bfd_elf_get_synthetic_symtab (bfd *abfd, 13677 long symcount ATTRIBUTE_UNUSED, 13678 asymbol **syms ATTRIBUTE_UNUSED, 13679 long dynsymcount, 13680 asymbol **dynsyms, 13681 asymbol **ret) 13682 { 13683 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13684 asection *relplt; 13685 asymbol *s; 13686 const char *relplt_name; 13687 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool); 13688 arelent *p; 13689 long count, i, n; 13690 size_t size; 13691 Elf_Internal_Shdr *hdr; 13692 char *names; 13693 asection *plt; 13694 13695 *ret = NULL; 13696 13697 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) 13698 return 0; 13699 13700 if (dynsymcount <= 0) 13701 return 0; 13702 13703 if (!bed->plt_sym_val) 13704 return 0; 13705 13706 relplt_name = bed->relplt_name; 13707 if (relplt_name == NULL) 13708 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; 13709 relplt = bfd_get_section_by_name (abfd, relplt_name); 13710 if (relplt == NULL) 13711 return 0; 13712 13713 hdr = &elf_section_data (relplt)->this_hdr; 13714 if (hdr->sh_link != elf_dynsymtab (abfd) 13715 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) 13716 return 0; 13717 13718 plt = bfd_get_section_by_name (abfd, ".plt"); 13719 if (plt == NULL) 13720 return 0; 13721 13722 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; 13723 if (! (*slurp_relocs) (abfd, relplt, dynsyms, true)) 13724 return -1; 13725 13726 count = NUM_SHDR_ENTRIES (hdr); 13727 size = count * sizeof (asymbol); 13728 p = relplt->relocation; 13729 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) 13730 { 13731 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); 13732 if (p->addend != 0) 13733 { 13734 #ifdef BFD64 13735 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); 13736 #else 13737 size += sizeof ("+0x") - 1 + 8; 13738 #endif 13739 } 13740 } 13741 13742 s = *ret = (asymbol *) bfd_malloc (size); 13743 if (s == NULL) 13744 return -1; 13745 13746 names = (char *) (s + count); 13747 p = relplt->relocation; 13748 n = 0; 13749 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) 13750 { 13751 size_t len; 13752 bfd_vma addr; 13753 13754 addr = bed->plt_sym_val (i, plt, p); 13755 if (addr == (bfd_vma) -1) 13756 continue; 13757 13758 *s = **p->sym_ptr_ptr; 13759 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since 13760 we are defining a symbol, ensure one of them is set. */ 13761 if ((s->flags & BSF_LOCAL) == 0) 13762 s->flags |= BSF_GLOBAL; 13763 s->flags |= BSF_SYNTHETIC; 13764 s->section = plt; 13765 s->value = addr - plt->vma; 13766 s->name = names; 13767 s->udata.p = NULL; 13768 len = strlen ((*p->sym_ptr_ptr)->name); 13769 memcpy (names, (*p->sym_ptr_ptr)->name, len); 13770 names += len; 13771 if (p->addend != 0) 13772 { 13773 char buf[30], *a; 13774 13775 memcpy (names, "+0x", sizeof ("+0x") - 1); 13776 names += sizeof ("+0x") - 1; 13777 bfd_sprintf_vma (abfd, buf, p->addend); 13778 for (a = buf; *a == '0'; ++a) 13779 ; 13780 len = strlen (a); 13781 memcpy (names, a, len); 13782 names += len; 13783 } 13784 memcpy (names, "@plt", sizeof ("@plt")); 13785 names += sizeof ("@plt"); 13786 ++s, ++n; 13787 } 13788 13789 return n; 13790 } 13791 13792 /* It is only used by x86-64 so far. 13793 ??? This repeats *COM* id of zero. sec->id is supposed to be unique, 13794 but current usage would allow all of _bfd_std_section to be zero. */ 13795 static const asymbol lcomm_sym 13796 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section); 13797 asection _bfd_elf_large_com_section 13798 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym, 13799 "LARGE_COMMON", 0, SEC_IS_COMMON); 13800 13801 bool 13802 _bfd_elf_final_write_processing (bfd *abfd) 13803 { 13804 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */ 13805 13806 i_ehdrp = elf_elfheader (abfd); 13807 13808 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) 13809 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; 13810 13811 /* Set the osabi field to ELFOSABI_GNU if the binary contains 13812 SHF_GNU_MBIND or SHF_GNU_RETAIN sections or symbols of STT_GNU_IFUNC type 13813 or STB_GNU_UNIQUE binding. */ 13814 if (elf_tdata (abfd)->has_gnu_osabi != 0) 13815 { 13816 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) 13817 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; 13818 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU 13819 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD) 13820 { 13821 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) 13822 _bfd_error_handler (_("GNU_MBIND section is supported only by GNU " 13823 "and FreeBSD targets")); 13824 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc) 13825 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is supported " 13826 "only by GNU and FreeBSD targets")); 13827 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique) 13828 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is supported " 13829 "only by GNU and FreeBSD targets")); 13830 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_retain) 13831 _bfd_error_handler (_("GNU_RETAIN section is supported " 13832 "only by GNU and FreeBSD targets")); 13833 bfd_set_error (bfd_error_sorry); 13834 return false; 13835 } 13836 } 13837 return true; 13838 } 13839 13840 13841 /* Return TRUE for ELF symbol types that represent functions. 13842 This is the default version of this function, which is sufficient for 13843 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ 13844 13845 bool 13846 _bfd_elf_is_function_type (unsigned int type) 13847 { 13848 return (type == STT_FUNC 13849 || type == STT_GNU_IFUNC); 13850 } 13851 13852 /* If the ELF symbol SYM might be a function in SEC, return the 13853 function size and set *CODE_OFF to the function's entry point, 13854 otherwise return zero. */ 13855 13856 bfd_size_type 13857 _bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, 13858 bfd_vma *code_off) 13859 { 13860 bfd_size_type size; 13861 elf_symbol_type * elf_sym = (elf_symbol_type *) sym; 13862 13863 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT 13864 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 13865 || sym->section != sec) 13866 return 0; 13867 13868 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size; 13869 13870 /* In theory we should check that the symbol's type satisfies 13871 _bfd_elf_is_function_type(), but there are some function-like 13872 symbols which would fail this test. (eg _start). Instead 13873 we check for hidden, local, notype symbols with zero size. 13874 This type of symbol is generated by the annobin plugin for gcc 13875 and clang, and should not be considered to be a function symbol. */ 13876 if (size == 0 13877 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL) 13878 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE 13879 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN) 13880 return 0; 13881 13882 *code_off = sym->value; 13883 /* Do not return 0 for the function's size. */ 13884 return size ? size : 1; 13885 } 13886 13887 /* Set to non-zero to enable some debug messages. */ 13888 #define DEBUG_SECONDARY_RELOCS 0 13889 13890 /* An internal-to-the-bfd-library only section type 13891 used to indicate a cached secondary reloc section. */ 13892 #define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA) 13893 13894 /* Create a BFD section to hold a secondary reloc section. */ 13895 13896 bool 13897 _bfd_elf_init_secondary_reloc_section (bfd * abfd, 13898 Elf_Internal_Shdr *hdr, 13899 const char * name, 13900 unsigned int shindex) 13901 { 13902 /* We only support RELA secondary relocs. */ 13903 if (hdr->sh_type != SHT_RELA) 13904 return false; 13905 13906 #if DEBUG_SECONDARY_RELOCS 13907 fprintf (stderr, "secondary reloc section %s encountered\n", name); 13908 #endif 13909 hdr->sh_type = SHT_SECONDARY_RELOC; 13910 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 13911 } 13912 13913 /* Read in any secondary relocs associated with SEC. */ 13914 13915 bool 13916 _bfd_elf_slurp_secondary_reloc_section (bfd * abfd, 13917 asection * sec, 13918 asymbol ** symbols, 13919 bool dynamic) 13920 { 13921 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); 13922 asection * relsec; 13923 bool result = true; 13924 bfd_vma (*r_sym) (bfd_vma); 13925 ufile_ptr filesize; 13926 13927 #if BFD_DEFAULT_TARGET_SIZE > 32 13928 if (bfd_arch_bits_per_address (abfd) != 32) 13929 r_sym = elf64_r_sym; 13930 else 13931 #endif 13932 r_sym = elf32_r_sym; 13933 13934 if (!elf_section_data (sec)->has_secondary_relocs) 13935 return true; 13936 13937 /* Discover if there are any secondary reloc sections 13938 associated with SEC. */ 13939 filesize = bfd_get_file_size (abfd); 13940 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) 13941 { 13942 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr; 13943 13944 if (hdr->sh_type == SHT_SECONDARY_RELOC 13945 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx 13946 && (hdr->sh_entsize == ebd->s->sizeof_rel 13947 || hdr->sh_entsize == ebd->s->sizeof_rela)) 13948 { 13949 bfd_byte * native_relocs; 13950 bfd_byte * native_reloc; 13951 arelent * internal_relocs; 13952 arelent * internal_reloc; 13953 size_t i; 13954 unsigned int entsize; 13955 unsigned int symcount; 13956 bfd_size_type reloc_count; 13957 size_t amt; 13958 13959 if (ebd->elf_info_to_howto == NULL) 13960 return false; 13961 13962 #if DEBUG_SECONDARY_RELOCS 13963 fprintf (stderr, "read secondary relocs for %s from %s\n", 13964 sec->name, relsec->name); 13965 #endif 13966 entsize = hdr->sh_entsize; 13967 13968 if (filesize != 0 13969 && ((ufile_ptr) hdr->sh_offset > filesize 13970 || hdr->sh_size > filesize - hdr->sh_offset)) 13971 { 13972 bfd_set_error (bfd_error_file_truncated); 13973 result = false; 13974 continue; 13975 } 13976 13977 native_relocs = bfd_malloc (hdr->sh_size); 13978 if (native_relocs == NULL) 13979 { 13980 result = false; 13981 continue; 13982 } 13983 13984 reloc_count = NUM_SHDR_ENTRIES (hdr); 13985 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt)) 13986 { 13987 free (native_relocs); 13988 bfd_set_error (bfd_error_file_too_big); 13989 result = false; 13990 continue; 13991 } 13992 13993 internal_relocs = (arelent *) bfd_alloc (abfd, amt); 13994 if (internal_relocs == NULL) 13995 { 13996 free (native_relocs); 13997 result = false; 13998 continue; 13999 } 14000 14001 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 14002 || bfd_read (native_relocs, hdr->sh_size, abfd) != hdr->sh_size) 14003 { 14004 free (native_relocs); 14005 /* The internal_relocs will be freed when 14006 the memory for the bfd is released. */ 14007 result = false; 14008 continue; 14009 } 14010 14011 if (dynamic) 14012 symcount = bfd_get_dynamic_symcount (abfd); 14013 else 14014 symcount = bfd_get_symcount (abfd); 14015 14016 for (i = 0, internal_reloc = internal_relocs, 14017 native_reloc = native_relocs; 14018 i < reloc_count; 14019 i++, internal_reloc++, native_reloc += entsize) 14020 { 14021 bool res; 14022 Elf_Internal_Rela rela; 14023 14024 if (entsize == ebd->s->sizeof_rel) 14025 ebd->s->swap_reloc_in (abfd, native_reloc, & rela); 14026 else /* entsize == ebd->s->sizeof_rela */ 14027 ebd->s->swap_reloca_in (abfd, native_reloc, & rela); 14028 14029 /* The address of an ELF reloc is section relative for an object 14030 file, and absolute for an executable file or shared library. 14031 The address of a normal BFD reloc is always section relative, 14032 and the address of a dynamic reloc is absolute.. */ 14033 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) 14034 internal_reloc->address = rela.r_offset; 14035 else 14036 internal_reloc->address = rela.r_offset - sec->vma; 14037 14038 if (r_sym (rela.r_info) == STN_UNDEF) 14039 { 14040 /* FIXME: This and the error case below mean that we 14041 have a symbol on relocs that is not elf_symbol_type. */ 14042 internal_reloc->sym_ptr_ptr = 14043 bfd_abs_section_ptr->symbol_ptr_ptr; 14044 } 14045 else if (r_sym (rela.r_info) > symcount) 14046 { 14047 _bfd_error_handler 14048 /* xgettext:c-format */ 14049 (_("%pB(%pA): relocation %zu has invalid symbol index %lu"), 14050 abfd, sec, i, (long) r_sym (rela.r_info)); 14051 bfd_set_error (bfd_error_bad_value); 14052 internal_reloc->sym_ptr_ptr = 14053 bfd_abs_section_ptr->symbol_ptr_ptr; 14054 result = false; 14055 } 14056 else 14057 { 14058 asymbol **ps; 14059 14060 ps = symbols + r_sym (rela.r_info) - 1; 14061 internal_reloc->sym_ptr_ptr = ps; 14062 /* Make sure that this symbol is not removed by strip. */ 14063 (*ps)->flags |= BSF_KEEP; 14064 } 14065 14066 internal_reloc->addend = rela.r_addend; 14067 14068 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela); 14069 if (! res || internal_reloc->howto == NULL) 14070 { 14071 #if DEBUG_SECONDARY_RELOCS 14072 fprintf (stderr, 14073 "there is no howto associated with reloc %lx\n", 14074 rela.r_info); 14075 #endif 14076 result = false; 14077 } 14078 } 14079 14080 free (native_relocs); 14081 /* Store the internal relocs. */ 14082 elf_section_data (relsec)->sec_info = internal_relocs; 14083 } 14084 } 14085 14086 return result; 14087 } 14088 14089 /* Set the ELF section header fields of an output secondary reloc section. */ 14090 14091 bool 14092 _bfd_elf_copy_special_section_fields (const bfd *ibfd ATTRIBUTE_UNUSED, 14093 bfd *obfd ATTRIBUTE_UNUSED, 14094 const Elf_Internal_Shdr *isection, 14095 Elf_Internal_Shdr *osection) 14096 { 14097 asection * isec; 14098 asection * osec; 14099 struct bfd_elf_section_data * esd; 14100 14101 if (isection == NULL) 14102 return false; 14103 14104 if (isection->sh_type != SHT_SECONDARY_RELOC) 14105 return true; 14106 14107 isec = isection->bfd_section; 14108 if (isec == NULL) 14109 return false; 14110 14111 osec = osection->bfd_section; 14112 if (osec == NULL) 14113 return false; 14114 14115 esd = elf_section_data (osec); 14116 BFD_ASSERT (esd->sec_info == NULL); 14117 esd->sec_info = elf_section_data (isec)->sec_info; 14118 osection->sh_type = SHT_RELA; 14119 osection->sh_link = elf_onesymtab (obfd); 14120 if (osection->sh_link == 0) 14121 { 14122 /* There is no symbol table - we are hosed... */ 14123 _bfd_error_handler 14124 /* xgettext:c-format */ 14125 (_("%pB(%pA): link section cannot be set" 14126 " because the output file does not have a symbol table"), 14127 obfd, osec); 14128 bfd_set_error (bfd_error_bad_value); 14129 return false; 14130 } 14131 14132 /* Find the output section that corresponds to the isection's 14133 sh_info link. */ 14134 if (isection->sh_info == 0 14135 || isection->sh_info >= elf_numsections (ibfd)) 14136 { 14137 _bfd_error_handler 14138 /* xgettext:c-format */ 14139 (_("%pB(%pA): info section index is invalid"), 14140 obfd, osec); 14141 bfd_set_error (bfd_error_bad_value); 14142 return false; 14143 } 14144 14145 isection = elf_elfsections (ibfd)[isection->sh_info]; 14146 14147 if (isection == NULL 14148 || isection->bfd_section == NULL 14149 || isection->bfd_section->output_section == NULL) 14150 { 14151 _bfd_error_handler 14152 /* xgettext:c-format */ 14153 (_("%pB(%pA): info section index cannot be set" 14154 " because the section is not in the output"), 14155 obfd, osec); 14156 bfd_set_error (bfd_error_bad_value); 14157 return false; 14158 } 14159 14160 esd = elf_section_data (isection->bfd_section->output_section); 14161 BFD_ASSERT (esd != NULL); 14162 osection->sh_info = esd->this_idx; 14163 esd->has_secondary_relocs = true; 14164 #if DEBUG_SECONDARY_RELOCS 14165 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n", 14166 osec->name, osection->sh_link, osection->sh_info); 14167 fprintf (stderr, "mark section %s as having secondary relocs\n", 14168 bfd_section_name (isection->bfd_section->output_section)); 14169 #endif 14170 14171 return true; 14172 } 14173 14174 /* Write out a secondary reloc section. 14175 14176 FIXME: Currently this function can result in a serious performance penalty 14177 for files with secondary relocs and lots of sections. The proper way to 14178 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary 14179 relocs together and then to have this function just walk that chain. */ 14180 14181 bool 14182 _bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec) 14183 { 14184 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); 14185 bfd_vma addr_offset; 14186 asection * relsec; 14187 bfd_vma (*r_info) (bfd_vma, bfd_vma); 14188 bool result = true; 14189 14190 if (sec == NULL) 14191 return false; 14192 14193 #if BFD_DEFAULT_TARGET_SIZE > 32 14194 if (bfd_arch_bits_per_address (abfd) != 32) 14195 r_info = elf64_r_info; 14196 else 14197 #endif 14198 r_info = elf32_r_info; 14199 14200 /* The address of an ELF reloc is section relative for an object 14201 file, and absolute for an executable file or shared library. 14202 The address of a BFD reloc is always section relative. */ 14203 addr_offset = 0; 14204 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) 14205 addr_offset = sec->vma; 14206 14207 /* Discover if there are any secondary reloc sections 14208 associated with SEC. */ 14209 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) 14210 { 14211 const struct bfd_elf_section_data * const esd = elf_section_data (relsec); 14212 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr; 14213 14214 if (hdr->sh_type == SHT_RELA 14215 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx) 14216 { 14217 asymbol * last_sym; 14218 int last_sym_idx; 14219 size_t reloc_count; 14220 size_t idx; 14221 bfd_size_type entsize; 14222 arelent * src_irel; 14223 bfd_byte * dst_rela; 14224 14225 if (hdr->contents != NULL) 14226 { 14227 _bfd_error_handler 14228 /* xgettext:c-format */ 14229 (_("%pB(%pA): error: secondary reloc section processed twice"), 14230 abfd, relsec); 14231 bfd_set_error (bfd_error_bad_value); 14232 result = false; 14233 continue; 14234 } 14235 14236 entsize = hdr->sh_entsize; 14237 if (entsize == 0) 14238 { 14239 _bfd_error_handler 14240 /* xgettext:c-format */ 14241 (_("%pB(%pA): error: secondary reloc section" 14242 " has zero sized entries"), 14243 abfd, relsec); 14244 bfd_set_error (bfd_error_bad_value); 14245 result = false; 14246 continue; 14247 } 14248 else if (entsize != ebd->s->sizeof_rel 14249 && entsize != ebd->s->sizeof_rela) 14250 { 14251 _bfd_error_handler 14252 /* xgettext:c-format */ 14253 (_("%pB(%pA): error: secondary reloc section" 14254 " has non-standard sized entries"), 14255 abfd, relsec); 14256 bfd_set_error (bfd_error_bad_value); 14257 result = false; 14258 continue; 14259 } 14260 14261 reloc_count = hdr->sh_size / entsize; 14262 hdr->sh_size = entsize * reloc_count; 14263 if (reloc_count == 0) 14264 { 14265 _bfd_error_handler 14266 /* xgettext:c-format */ 14267 (_("%pB(%pA): error: secondary reloc section is empty!"), 14268 abfd, relsec); 14269 bfd_set_error (bfd_error_bad_value); 14270 result = false; 14271 continue; 14272 } 14273 14274 hdr->contents = bfd_alloc (abfd, hdr->sh_size); 14275 if (hdr->contents == NULL) 14276 continue; 14277 14278 #if DEBUG_SECONDARY_RELOCS 14279 fprintf (stderr, "write %u secondary relocs for %s from %s\n", 14280 reloc_count, sec->name, relsec->name); 14281 #endif 14282 last_sym = NULL; 14283 last_sym_idx = 0; 14284 dst_rela = hdr->contents; 14285 src_irel = (arelent *) esd->sec_info; 14286 if (src_irel == NULL) 14287 { 14288 _bfd_error_handler 14289 /* xgettext:c-format */ 14290 (_("%pB(%pA): error: internal relocs missing" 14291 " for secondary reloc section"), 14292 abfd, relsec); 14293 bfd_set_error (bfd_error_bad_value); 14294 result = false; 14295 continue; 14296 } 14297 14298 for (idx = 0; idx < reloc_count; idx++, dst_rela += entsize) 14299 { 14300 Elf_Internal_Rela src_rela; 14301 arelent *ptr; 14302 asymbol *sym; 14303 int n; 14304 14305 ptr = src_irel + idx; 14306 if (ptr == NULL) 14307 { 14308 _bfd_error_handler 14309 /* xgettext:c-format */ 14310 (_("%pB(%pA): error: reloc table entry %zu is empty"), 14311 abfd, relsec, idx); 14312 bfd_set_error (bfd_error_bad_value); 14313 result = false; 14314 break; 14315 } 14316 14317 if (ptr->sym_ptr_ptr == NULL) 14318 { 14319 /* FIXME: Is this an error ? */ 14320 n = 0; 14321 } 14322 else 14323 { 14324 sym = *ptr->sym_ptr_ptr; 14325 14326 if (sym == last_sym) 14327 n = last_sym_idx; 14328 else 14329 { 14330 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym); 14331 if (n < 0) 14332 { 14333 _bfd_error_handler 14334 /* xgettext:c-format */ 14335 (_("%pB(%pA): error: secondary reloc %zu" 14336 " references a missing symbol"), 14337 abfd, relsec, idx); 14338 bfd_set_error (bfd_error_bad_value); 14339 result = false; 14340 n = 0; 14341 } 14342 14343 last_sym = sym; 14344 last_sym_idx = n; 14345 } 14346 14347 if (sym->the_bfd != NULL 14348 && sym->the_bfd->xvec != abfd->xvec 14349 && ! _bfd_elf_validate_reloc (abfd, ptr)) 14350 { 14351 _bfd_error_handler 14352 /* xgettext:c-format */ 14353 (_("%pB(%pA): error: secondary reloc %zu" 14354 " references a deleted symbol"), 14355 abfd, relsec, idx); 14356 bfd_set_error (bfd_error_bad_value); 14357 result = false; 14358 n = 0; 14359 } 14360 } 14361 14362 src_rela.r_offset = ptr->address + addr_offset; 14363 if (ptr->howto == NULL) 14364 { 14365 _bfd_error_handler 14366 /* xgettext:c-format */ 14367 (_("%pB(%pA): error: secondary reloc %zu" 14368 " is of an unknown type"), 14369 abfd, relsec, idx); 14370 bfd_set_error (bfd_error_bad_value); 14371 result = false; 14372 src_rela.r_info = r_info (0, 0); 14373 } 14374 else 14375 src_rela.r_info = r_info (n, ptr->howto->type); 14376 src_rela.r_addend = ptr->addend; 14377 14378 if (entsize == ebd->s->sizeof_rel) 14379 ebd->s->swap_reloc_out (abfd, &src_rela, dst_rela); 14380 else /* entsize == ebd->s->sizeof_rela */ 14381 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela); 14382 } 14383 } 14384 } 14385 14386 return result; 14387 } 14388 14389 /* Mmap in section contents. If FINAL_LINK is false, set *BUF to NULL 14390 before calling bfd_get_full_section_contents. */ 14391 14392 static bool 14393 elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf, 14394 bool final_link) 14395 { 14396 #ifdef USE_MMAP 14397 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14398 if (bed->use_mmap 14399 && sec->compress_status == COMPRESS_SECTION_NONE 14400 && (sec->flags & SEC_LINKER_CREATED) == 0) 14401 { 14402 /* Use mmap only if section size >= the minimum mmap section 14403 size. */ 14404 size_t readsz = bfd_get_section_limit_octets (abfd, sec); 14405 size_t allocsz = bfd_get_section_alloc_size (abfd, sec); 14406 if (readsz == allocsz && readsz >= _bfd_minimum_mmap_size) 14407 { 14408 if (sec->contents != NULL) 14409 { 14410 if (!sec->mmapped_p) 14411 abort (); 14412 *buf = sec->contents; 14413 return true; 14414 } 14415 if (sec->mmapped_p) 14416 abort (); 14417 sec->mmapped_p = 1; 14418 14419 /* Never use the preallocated buffer if mmapp is used. */ 14420 *buf = NULL; 14421 } 14422 } 14423 #endif 14424 /* NB: When this is called from elf_link_input_bfd, FINAL_LINK is 14425 true. If FINAL_LINK is false, *BUF is set to the preallocated 14426 buffer if USE_MMAP is undefined and *BUF is set to NULL if 14427 USE_MMAP is defined. */ 14428 if (!final_link) 14429 *buf = NULL; 14430 bool ret = bfd_get_full_section_contents (abfd, sec, buf); 14431 if (ret && sec->mmapped_p) 14432 *buf = sec->contents; 14433 return ret; 14434 } 14435 14436 /* Mmap in section contents. */ 14437 14438 bool 14439 _bfd_elf_mmap_section_contents (bfd *abfd, sec_ptr sec, bfd_byte **buf) 14440 { 14441 return elf_mmap_section_contents (abfd, sec, buf, false); 14442 } 14443 14444 /* Mmap in the full section contents for the final link. */ 14445 14446 bool 14447 _bfd_elf_link_mmap_section_contents (bfd *abfd, sec_ptr sec, 14448 bfd_byte **buf) 14449 { 14450 return elf_mmap_section_contents (abfd, sec, buf, true); 14451 } 14452 14453 /* Munmap section contents. */ 14454 14455 void 14456 _bfd_elf_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED, 14457 void *contents) 14458 { 14459 /* NB: Since _bfd_elf_munmap_section_contents is called like free, 14460 CONTENTS may be NULL. */ 14461 if (contents == NULL) 14462 return; 14463 14464 #ifdef USE_MMAP 14465 if (sec->mmapped_p) 14466 { 14467 /* _bfd_elf_mmap_section_contents may return the previously 14468 mapped section contents. Munmap the section contents only 14469 if they haven't been cached. */ 14470 if (elf_section_data (sec)->this_hdr.contents == contents) 14471 return; 14472 14473 /* When _bfd_elf_mmap_section_contents returns CONTENTS as 14474 malloced, CONTENTS_ADDR is set to NULL. */ 14475 if (elf_section_data (sec)->contents_addr != NULL) 14476 { 14477 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */ 14478 if (munmap (elf_section_data (sec)->contents_addr, 14479 elf_section_data (sec)->contents_size) != 0) 14480 abort (); 14481 sec->mmapped_p = 0; 14482 sec->contents = NULL; 14483 elf_section_data (sec)->contents_addr = NULL; 14484 elf_section_data (sec)->contents_size = 0; 14485 return; 14486 } 14487 } 14488 #endif 14489 14490 free (contents); 14491 } 14492 14493 /* Munmap the full section contents for the final link. */ 14494 14495 void 14496 _bfd_elf_link_munmap_section_contents (asection *sec ATTRIBUTE_UNUSED) 14497 { 14498 #ifdef USE_MMAP 14499 if (sec->mmapped_p && elf_section_data (sec)->contents_addr != NULL) 14500 { 14501 /* When _bfd_elf_link_mmap_section_contents returns CONTENTS as 14502 malloced, CONTENTS_ADDR is set to NULL. */ 14503 /* NB: CONTENTS_ADDR and CONTENTS_SIZE must be valid. */ 14504 if (munmap (elf_section_data (sec)->contents_addr, 14505 elf_section_data (sec)->contents_size) != 0) 14506 abort (); 14507 sec->mmapped_p = 0; 14508 sec->contents = NULL; 14509 elf_section_data (sec)->contents_addr = NULL; 14510 elf_section_data (sec)->contents_size = 0; 14511 } 14512 #endif 14513 } 14514