1/* ELF executable support for BFD. 2 3 Copyright (C) 1993-2020 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/* 24SECTION 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 52static int elf_sort_sections (const void *, const void *); 53static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); 54static bfd_boolean swap_out_syms (bfd *, struct elf_strtab_hash **, int) ; 55static bfd_boolean elf_parse_notes (bfd *abfd, char *buf, size_t size, 56 file_ptr offset, size_t align); 57 58/* Swap version information in and out. The version information is 59 currently size independent. If that ever changes, this code will 60 need to move into elfcode.h. */ 61 62/* Swap in a Verdef structure. */ 63 64void 65_bfd_elf_swap_verdef_in (bfd *abfd, 66 const Elf_External_Verdef *src, 67 Elf_Internal_Verdef *dst) 68{ 69 dst->vd_version = H_GET_16 (abfd, src->vd_version); 70 dst->vd_flags = H_GET_16 (abfd, src->vd_flags); 71 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); 72 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); 73 dst->vd_hash = H_GET_32 (abfd, src->vd_hash); 74 dst->vd_aux = H_GET_32 (abfd, src->vd_aux); 75 dst->vd_next = H_GET_32 (abfd, src->vd_next); 76} 77 78/* Swap out a Verdef structure. */ 79 80void 81_bfd_elf_swap_verdef_out (bfd *abfd, 82 const Elf_Internal_Verdef *src, 83 Elf_External_Verdef *dst) 84{ 85 H_PUT_16 (abfd, src->vd_version, dst->vd_version); 86 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); 87 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); 88 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); 89 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); 90 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); 91 H_PUT_32 (abfd, src->vd_next, dst->vd_next); 92} 93 94/* Swap in a Verdaux structure. */ 95 96void 97_bfd_elf_swap_verdaux_in (bfd *abfd, 98 const Elf_External_Verdaux *src, 99 Elf_Internal_Verdaux *dst) 100{ 101 dst->vda_name = H_GET_32 (abfd, src->vda_name); 102 dst->vda_next = H_GET_32 (abfd, src->vda_next); 103} 104 105/* Swap out a Verdaux structure. */ 106 107void 108_bfd_elf_swap_verdaux_out (bfd *abfd, 109 const Elf_Internal_Verdaux *src, 110 Elf_External_Verdaux *dst) 111{ 112 H_PUT_32 (abfd, src->vda_name, dst->vda_name); 113 H_PUT_32 (abfd, src->vda_next, dst->vda_next); 114} 115 116/* Swap in a Verneed structure. */ 117 118void 119_bfd_elf_swap_verneed_in (bfd *abfd, 120 const Elf_External_Verneed *src, 121 Elf_Internal_Verneed *dst) 122{ 123 dst->vn_version = H_GET_16 (abfd, src->vn_version); 124 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); 125 dst->vn_file = H_GET_32 (abfd, src->vn_file); 126 dst->vn_aux = H_GET_32 (abfd, src->vn_aux); 127 dst->vn_next = H_GET_32 (abfd, src->vn_next); 128} 129 130/* Swap out a Verneed structure. */ 131 132void 133_bfd_elf_swap_verneed_out (bfd *abfd, 134 const Elf_Internal_Verneed *src, 135 Elf_External_Verneed *dst) 136{ 137 H_PUT_16 (abfd, src->vn_version, dst->vn_version); 138 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); 139 H_PUT_32 (abfd, src->vn_file, dst->vn_file); 140 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); 141 H_PUT_32 (abfd, src->vn_next, dst->vn_next); 142} 143 144/* Swap in a Vernaux structure. */ 145 146void 147_bfd_elf_swap_vernaux_in (bfd *abfd, 148 const Elf_External_Vernaux *src, 149 Elf_Internal_Vernaux *dst) 150{ 151 dst->vna_hash = H_GET_32 (abfd, src->vna_hash); 152 dst->vna_flags = H_GET_16 (abfd, src->vna_flags); 153 dst->vna_other = H_GET_16 (abfd, src->vna_other); 154 dst->vna_name = H_GET_32 (abfd, src->vna_name); 155 dst->vna_next = H_GET_32 (abfd, src->vna_next); 156} 157 158/* Swap out a Vernaux structure. */ 159 160void 161_bfd_elf_swap_vernaux_out (bfd *abfd, 162 const Elf_Internal_Vernaux *src, 163 Elf_External_Vernaux *dst) 164{ 165 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); 166 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); 167 H_PUT_16 (abfd, src->vna_other, dst->vna_other); 168 H_PUT_32 (abfd, src->vna_name, dst->vna_name); 169 H_PUT_32 (abfd, src->vna_next, dst->vna_next); 170} 171 172/* Swap in a Versym structure. */ 173 174void 175_bfd_elf_swap_versym_in (bfd *abfd, 176 const Elf_External_Versym *src, 177 Elf_Internal_Versym *dst) 178{ 179 dst->vs_vers = H_GET_16 (abfd, src->vs_vers); 180} 181 182/* Swap out a Versym structure. */ 183 184void 185_bfd_elf_swap_versym_out (bfd *abfd, 186 const Elf_Internal_Versym *src, 187 Elf_External_Versym *dst) 188{ 189 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); 190} 191 192/* Standard ELF hash function. Do not change this function; you will 193 cause invalid hash tables to be generated. */ 194 195unsigned long 196bfd_elf_hash (const char *namearg) 197{ 198 const unsigned char *name = (const unsigned char *) namearg; 199 unsigned long h = 0; 200 unsigned long g; 201 int ch; 202 203 while ((ch = *name++) != '\0') 204 { 205 h = (h << 4) + ch; 206 if ((g = (h & 0xf0000000)) != 0) 207 { 208 h ^= g >> 24; 209 /* The ELF ABI says `h &= ~g', but this is equivalent in 210 this case and on some machines one insn instead of two. */ 211 h ^= g; 212 } 213 } 214 return h & 0xffffffff; 215} 216 217/* DT_GNU_HASH hash function. Do not change this function; you will 218 cause invalid hash tables to be generated. */ 219 220unsigned long 221bfd_elf_gnu_hash (const char *namearg) 222{ 223 const unsigned char *name = (const unsigned char *) namearg; 224 unsigned long h = 5381; 225 unsigned char ch; 226 227 while ((ch = *name++) != '\0') 228 h = (h << 5) + h + ch; 229 return h & 0xffffffff; 230} 231 232/* Create a tdata field OBJECT_SIZE bytes in length, zeroed out and with 233 the object_id field of an elf_obj_tdata field set to OBJECT_ID. */ 234bfd_boolean 235bfd_elf_allocate_object (bfd *abfd, 236 size_t object_size, 237 enum elf_target_id object_id) 238{ 239 BFD_ASSERT (object_size >= sizeof (struct elf_obj_tdata)); 240 abfd->tdata.any = bfd_zalloc (abfd, object_size); 241 if (abfd->tdata.any == NULL) 242 return FALSE; 243 244 elf_object_id (abfd) = object_id; 245 if (abfd->direction != read_direction) 246 { 247 struct output_elf_obj_tdata *o = bfd_zalloc (abfd, sizeof *o); 248 if (o == NULL) 249 return FALSE; 250 elf_tdata (abfd)->o = o; 251 elf_program_header_size (abfd) = (bfd_size_type) -1; 252 } 253 return TRUE; 254} 255 256 257bfd_boolean 258bfd_elf_make_object (bfd *abfd) 259{ 260 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 261 return bfd_elf_allocate_object (abfd, sizeof (struct elf_obj_tdata), 262 bed->target_id); 263} 264 265bfd_boolean 266bfd_elf_mkcorefile (bfd *abfd) 267{ 268 /* I think this can be done just like an object file. */ 269 if (!abfd->xvec->_bfd_set_format[(int) bfd_object] (abfd)) 270 return FALSE; 271 elf_tdata (abfd)->core = bfd_zalloc (abfd, sizeof (*elf_tdata (abfd)->core)); 272 return elf_tdata (abfd)->core != NULL; 273} 274 275char * 276bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) 277{ 278 Elf_Internal_Shdr **i_shdrp; 279 bfd_byte *shstrtab = NULL; 280 file_ptr offset; 281 bfd_size_type shstrtabsize; 282 283 i_shdrp = elf_elfsections (abfd); 284 if (i_shdrp == 0 285 || shindex >= elf_numsections (abfd) 286 || i_shdrp[shindex] == 0) 287 return NULL; 288 289 shstrtab = i_shdrp[shindex]->contents; 290 if (shstrtab == NULL) 291 { 292 /* No cached one, attempt to read, and cache what we read. */ 293 offset = i_shdrp[shindex]->sh_offset; 294 shstrtabsize = i_shdrp[shindex]->sh_size; 295 296 /* Allocate and clear an extra byte at the end, to prevent crashes 297 in case the string table is not terminated. */ 298 if (shstrtabsize + 1 <= 1 299 || bfd_seek (abfd, offset, SEEK_SET) != 0 300 || (shstrtab = _bfd_alloc_and_read (abfd, shstrtabsize + 1, 301 shstrtabsize)) == NULL) 302 { 303 /* Once we've failed to read it, make sure we don't keep 304 trying. Otherwise, we'll keep allocating space for 305 the string table over and over. */ 306 i_shdrp[shindex]->sh_size = 0; 307 } 308 else 309 shstrtab[shstrtabsize] = '\0'; 310 i_shdrp[shindex]->contents = shstrtab; 311 } 312 return (char *) shstrtab; 313} 314 315char * 316bfd_elf_string_from_elf_section (bfd *abfd, 317 unsigned int shindex, 318 unsigned int strindex) 319{ 320 Elf_Internal_Shdr *hdr; 321 322 if (strindex == 0) 323 return ""; 324 325 if (elf_elfsections (abfd) == NULL || shindex >= elf_numsections (abfd)) 326 return NULL; 327 328 hdr = elf_elfsections (abfd)[shindex]; 329 330 if (hdr->contents == NULL) 331 { 332 if (hdr->sh_type != SHT_STRTAB && hdr->sh_type < SHT_LOOS) 333 { 334 /* PR 17512: file: f057ec89. */ 335 /* xgettext:c-format */ 336 _bfd_error_handler (_("%pB: attempt to load strings from" 337 " a non-string section (number %d)"), 338 abfd, shindex); 339 return NULL; 340 } 341 342 if (bfd_elf_get_str_section (abfd, shindex) == NULL) 343 return NULL; 344 } 345 else 346 { 347 /* PR 24273: The string section's contents may have already 348 been loaded elsewhere, eg because a corrupt file has the 349 string section index in the ELF header pointing at a group 350 section. So be paranoid, and test that the last byte of 351 the section is zero. */ 352 if (hdr->sh_size == 0 || hdr->contents[hdr->sh_size - 1] != 0) 353 return NULL; 354 } 355 356 if (strindex >= hdr->sh_size) 357 { 358 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; 359 _bfd_error_handler 360 /* xgettext:c-format */ 361 (_("%pB: invalid string offset %u >= %" PRIu64 " for section `%s'"), 362 abfd, strindex, (uint64_t) hdr->sh_size, 363 (shindex == shstrndx && strindex == hdr->sh_name 364 ? ".shstrtab" 365 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); 366 return NULL; 367 } 368 369 return ((char *) hdr->contents) + strindex; 370} 371 372/* Read and convert symbols to internal format. 373 SYMCOUNT specifies the number of symbols to read, starting from 374 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF 375 are non-NULL, they are used to store the internal symbols, external 376 symbols, and symbol section index extensions, respectively. 377 Returns a pointer to the internal symbol buffer (malloced if necessary) 378 or NULL if there were no symbols or some kind of problem. */ 379 380Elf_Internal_Sym * 381bfd_elf_get_elf_syms (bfd *ibfd, 382 Elf_Internal_Shdr *symtab_hdr, 383 size_t symcount, 384 size_t symoffset, 385 Elf_Internal_Sym *intsym_buf, 386 void *extsym_buf, 387 Elf_External_Sym_Shndx *extshndx_buf) 388{ 389 Elf_Internal_Shdr *shndx_hdr; 390 void *alloc_ext; 391 const bfd_byte *esym; 392 Elf_External_Sym_Shndx *alloc_extshndx; 393 Elf_External_Sym_Shndx *shndx; 394 Elf_Internal_Sym *alloc_intsym; 395 Elf_Internal_Sym *isym; 396 Elf_Internal_Sym *isymend; 397 const struct elf_backend_data *bed; 398 size_t extsym_size; 399 size_t amt; 400 file_ptr pos; 401 402 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 403 abort (); 404 405 if (symcount == 0) 406 return intsym_buf; 407 408 /* Normal syms might have section extension entries. */ 409 shndx_hdr = NULL; 410 if (elf_symtab_shndx_list (ibfd) != NULL) 411 { 412 elf_section_list * entry; 413 Elf_Internal_Shdr **sections = elf_elfsections (ibfd); 414 415 /* Find an index section that is linked to this symtab section. */ 416 for (entry = elf_symtab_shndx_list (ibfd); entry != NULL; entry = entry->next) 417 { 418 /* PR 20063. */ 419 if (entry->hdr.sh_link >= elf_numsections (ibfd)) 420 continue; 421 422 if (sections[entry->hdr.sh_link] == symtab_hdr) 423 { 424 shndx_hdr = & entry->hdr; 425 break; 426 }; 427 } 428 429 if (shndx_hdr == NULL) 430 { 431 if (symtab_hdr == & elf_symtab_hdr (ibfd)) 432 /* Not really accurate, but this was how the old code used to work. */ 433 shndx_hdr = & elf_symtab_shndx_list (ibfd)->hdr; 434 /* Otherwise we do nothing. The assumption is that 435 the index table will not be needed. */ 436 } 437 } 438 439 /* Read the symbols. */ 440 alloc_ext = NULL; 441 alloc_extshndx = NULL; 442 alloc_intsym = NULL; 443 bed = get_elf_backend_data (ibfd); 444 extsym_size = bed->s->sizeof_sym; 445 if (_bfd_mul_overflow (symcount, extsym_size, &amt)) 446 { 447 bfd_set_error (bfd_error_file_too_big); 448 intsym_buf = NULL; 449 goto out; 450 } 451 pos = symtab_hdr->sh_offset + symoffset * extsym_size; 452 if (extsym_buf == NULL) 453 { 454 alloc_ext = bfd_malloc (amt); 455 extsym_buf = alloc_ext; 456 } 457 if (extsym_buf == NULL 458 || bfd_seek (ibfd, pos, SEEK_SET) != 0 459 || bfd_bread (extsym_buf, amt, ibfd) != amt) 460 { 461 intsym_buf = NULL; 462 goto out; 463 } 464 465 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) 466 extshndx_buf = NULL; 467 else 468 { 469 if (_bfd_mul_overflow (symcount, sizeof (Elf_External_Sym_Shndx), &amt)) 470 { 471 bfd_set_error (bfd_error_file_too_big); 472 intsym_buf = NULL; 473 goto out; 474 } 475 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); 476 if (extshndx_buf == NULL) 477 { 478 alloc_extshndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); 479 extshndx_buf = alloc_extshndx; 480 } 481 if (extshndx_buf == NULL 482 || bfd_seek (ibfd, pos, SEEK_SET) != 0 483 || bfd_bread (extshndx_buf, amt, ibfd) != amt) 484 { 485 intsym_buf = NULL; 486 goto out; 487 } 488 } 489 490 if (intsym_buf == NULL) 491 { 492 if (_bfd_mul_overflow (symcount, sizeof (Elf_Internal_Sym), &amt)) 493 { 494 bfd_set_error (bfd_error_file_too_big); 495 goto out; 496 } 497 alloc_intsym = (Elf_Internal_Sym *) bfd_malloc (amt); 498 intsym_buf = alloc_intsym; 499 if (intsym_buf == NULL) 500 goto out; 501 } 502 503 /* Convert the symbols to internal form. */ 504 isymend = intsym_buf + symcount; 505 for (esym = (const bfd_byte *) extsym_buf, isym = intsym_buf, 506 shndx = extshndx_buf; 507 isym < isymend; 508 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) 509 if (!(*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym)) 510 { 511 symoffset += (esym - (bfd_byte *) extsym_buf) / extsym_size; 512 /* xgettext:c-format */ 513 _bfd_error_handler (_("%pB symbol number %lu references" 514 " nonexistent SHT_SYMTAB_SHNDX section"), 515 ibfd, (unsigned long) symoffset); 516 free (alloc_intsym); 517 intsym_buf = NULL; 518 goto out; 519 } 520 521 out: 522 free (alloc_ext); 523 free (alloc_extshndx); 524 525 return intsym_buf; 526} 527 528/* Look up a symbol name. */ 529const char * 530bfd_elf_sym_name (bfd *abfd, 531 Elf_Internal_Shdr *symtab_hdr, 532 Elf_Internal_Sym *isym, 533 asection *sym_sec) 534{ 535 const char *name; 536 unsigned int iname = isym->st_name; 537 unsigned int shindex = symtab_hdr->sh_link; 538 539 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION 540 /* Check for a bogus st_shndx to avoid crashing. */ 541 && isym->st_shndx < elf_numsections (abfd)) 542 { 543 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; 544 shindex = elf_elfheader (abfd)->e_shstrndx; 545 } 546 547 name = bfd_elf_string_from_elf_section (abfd, shindex, iname); 548 if (name == NULL) 549 name = "(null)"; 550 else if (sym_sec && *name == '\0') 551 name = bfd_section_name (sym_sec); 552 553 return name; 554} 555 556/* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP 557 sections. The first element is the flags, the rest are section 558 pointers. */ 559 560typedef union elf_internal_group { 561 Elf_Internal_Shdr *shdr; 562 unsigned int flags; 563} Elf_Internal_Group; 564 565/* Return the name of the group signature symbol. Why isn't the 566 signature just a string? */ 567 568static const char * 569group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) 570{ 571 Elf_Internal_Shdr *hdr; 572 unsigned char esym[sizeof (Elf64_External_Sym)]; 573 Elf_External_Sym_Shndx eshndx; 574 Elf_Internal_Sym isym; 575 576 /* First we need to ensure the symbol table is available. Make sure 577 that it is a symbol table section. */ 578 if (ghdr->sh_link >= elf_numsections (abfd)) 579 return NULL; 580 hdr = elf_elfsections (abfd) [ghdr->sh_link]; 581 if (hdr->sh_type != SHT_SYMTAB 582 || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) 583 return NULL; 584 585 /* Go read the symbol. */ 586 hdr = &elf_tdata (abfd)->symtab_hdr; 587 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, 588 &isym, esym, &eshndx) == NULL) 589 return NULL; 590 591 return bfd_elf_sym_name (abfd, hdr, &isym, NULL); 592} 593 594/* Set next_in_group list pointer, and group name for NEWSECT. */ 595 596static bfd_boolean 597setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) 598{ 599 unsigned int num_group = elf_tdata (abfd)->num_group; 600 601 /* If num_group is zero, read in all SHT_GROUP sections. The count 602 is set to -1 if there are no SHT_GROUP sections. */ 603 if (num_group == 0) 604 { 605 unsigned int i, shnum; 606 607 /* First count the number of groups. If we have a SHT_GROUP 608 section with just a flag word (ie. sh_size is 4), ignore it. */ 609 shnum = elf_numsections (abfd); 610 num_group = 0; 611 612#define IS_VALID_GROUP_SECTION_HEADER(shdr, minsize) \ 613 ( (shdr)->sh_type == SHT_GROUP \ 614 && (shdr)->sh_size >= minsize \ 615 && (shdr)->sh_entsize == GRP_ENTRY_SIZE \ 616 && ((shdr)->sh_size % GRP_ENTRY_SIZE) == 0) 617 618 for (i = 0; i < shnum; i++) 619 { 620 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; 621 622 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) 623 num_group += 1; 624 } 625 626 if (num_group == 0) 627 { 628 num_group = (unsigned) -1; 629 elf_tdata (abfd)->num_group = num_group; 630 elf_tdata (abfd)->group_sect_ptr = NULL; 631 } 632 else 633 { 634 /* We keep a list of elf section headers for group sections, 635 so we can find them quickly. */ 636 size_t amt; 637 638 elf_tdata (abfd)->num_group = num_group; 639 amt = num_group * sizeof (Elf_Internal_Shdr *); 640 elf_tdata (abfd)->group_sect_ptr 641 = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt); 642 if (elf_tdata (abfd)->group_sect_ptr == NULL) 643 return FALSE; 644 num_group = 0; 645 646 for (i = 0; i < shnum; i++) 647 { 648 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; 649 650 if (IS_VALID_GROUP_SECTION_HEADER (shdr, 2 * GRP_ENTRY_SIZE)) 651 { 652 unsigned char *src; 653 Elf_Internal_Group *dest; 654 655 /* Make sure the group section has a BFD section 656 attached to it. */ 657 if (!bfd_section_from_shdr (abfd, i)) 658 return FALSE; 659 660 /* Add to list of sections. */ 661 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; 662 num_group += 1; 663 664 /* Read the raw contents. */ 665 BFD_ASSERT (sizeof (*dest) >= 4 && sizeof (*dest) % 4 == 0); 666 shdr->contents = NULL; 667 if (_bfd_mul_overflow (shdr->sh_size, 668 sizeof (*dest) / 4, &amt) 669 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 670 || !(shdr->contents 671 = _bfd_alloc_and_read (abfd, amt, shdr->sh_size))) 672 { 673 _bfd_error_handler 674 /* xgettext:c-format */ 675 (_("%pB: invalid size field in group section" 676 " header: %#" PRIx64 ""), 677 abfd, (uint64_t) shdr->sh_size); 678 bfd_set_error (bfd_error_bad_value); 679 -- num_group; 680 continue; 681 } 682 683 /* Translate raw contents, a flag word followed by an 684 array of elf section indices all in target byte order, 685 to the flag word followed by an array of elf section 686 pointers. */ 687 src = shdr->contents + shdr->sh_size; 688 dest = (Elf_Internal_Group *) (shdr->contents + amt); 689 690 while (1) 691 { 692 unsigned int idx; 693 694 src -= 4; 695 --dest; 696 idx = H_GET_32 (abfd, src); 697 if (src == shdr->contents) 698 { 699 dest->shdr = NULL; 700 dest->flags = idx; 701 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) 702 shdr->bfd_section->flags 703 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 704 break; 705 } 706 if (idx < shnum) 707 { 708 dest->shdr = elf_elfsections (abfd)[idx]; 709 /* PR binutils/23199: All sections in a 710 section group should be marked with 711 SHF_GROUP. But some tools generate 712 broken objects without SHF_GROUP. Fix 713 them up here. */ 714 dest->shdr->sh_flags |= SHF_GROUP; 715 } 716 if (idx >= shnum 717 || dest->shdr->sh_type == SHT_GROUP) 718 { 719 _bfd_error_handler 720 (_("%pB: invalid entry in SHT_GROUP section [%u]"), 721 abfd, i); 722 dest->shdr = NULL; 723 } 724 } 725 } 726 } 727 728 /* PR 17510: Corrupt binaries might contain invalid groups. */ 729 if (num_group != (unsigned) elf_tdata (abfd)->num_group) 730 { 731 elf_tdata (abfd)->num_group = num_group; 732 733 /* If all groups are invalid then fail. */ 734 if (num_group == 0) 735 { 736 elf_tdata (abfd)->group_sect_ptr = NULL; 737 elf_tdata (abfd)->num_group = num_group = -1; 738 _bfd_error_handler 739 (_("%pB: no valid group sections found"), abfd); 740 bfd_set_error (bfd_error_bad_value); 741 } 742 } 743 } 744 } 745 746 if (num_group != (unsigned) -1) 747 { 748 unsigned int search_offset = elf_tdata (abfd)->group_search_offset; 749 unsigned int j; 750 751 for (j = 0; j < num_group; j++) 752 { 753 /* Begin search from previous found group. */ 754 unsigned i = (j + search_offset) % num_group; 755 756 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; 757 Elf_Internal_Group *idx; 758 bfd_size_type n_elt; 759 760 if (shdr == NULL) 761 continue; 762 763 idx = (Elf_Internal_Group *) shdr->contents; 764 if (idx == NULL || shdr->sh_size < 4) 765 { 766 /* See PR 21957 for a reproducer. */ 767 /* xgettext:c-format */ 768 _bfd_error_handler (_("%pB: group section '%pA' has no contents"), 769 abfd, shdr->bfd_section); 770 elf_tdata (abfd)->group_sect_ptr[i] = NULL; 771 bfd_set_error (bfd_error_bad_value); 772 return FALSE; 773 } 774 n_elt = shdr->sh_size / 4; 775 776 /* Look through this group's sections to see if current 777 section is a member. */ 778 while (--n_elt != 0) 779 if ((++idx)->shdr == hdr) 780 { 781 asection *s = NULL; 782 783 /* We are a member of this group. Go looking through 784 other members to see if any others are linked via 785 next_in_group. */ 786 idx = (Elf_Internal_Group *) shdr->contents; 787 n_elt = shdr->sh_size / 4; 788 while (--n_elt != 0) 789 if ((++idx)->shdr != NULL 790 && (s = idx->shdr->bfd_section) != NULL 791 && elf_next_in_group (s) != NULL) 792 break; 793 if (n_elt != 0) 794 { 795 /* Snarf the group name from other member, and 796 insert current section in circular list. */ 797 elf_group_name (newsect) = elf_group_name (s); 798 elf_next_in_group (newsect) = elf_next_in_group (s); 799 elf_next_in_group (s) = newsect; 800 } 801 else 802 { 803 const char *gname; 804 805 gname = group_signature (abfd, shdr); 806 if (gname == NULL) 807 return FALSE; 808 elf_group_name (newsect) = gname; 809 810 /* Start a circular list with one element. */ 811 elf_next_in_group (newsect) = newsect; 812 } 813 814 /* If the group section has been created, point to the 815 new member. */ 816 if (shdr->bfd_section != NULL) 817 elf_next_in_group (shdr->bfd_section) = newsect; 818 819 elf_tdata (abfd)->group_search_offset = i; 820 j = num_group - 1; 821 break; 822 } 823 } 824 } 825 826 if (elf_group_name (newsect) == NULL) 827 { 828 /* xgettext:c-format */ 829 _bfd_error_handler (_("%pB: no group info for section '%pA'"), 830 abfd, newsect); 831 return FALSE; 832 } 833 return TRUE; 834} 835 836bfd_boolean 837_bfd_elf_setup_sections (bfd *abfd) 838{ 839 unsigned int i; 840 unsigned int num_group = elf_tdata (abfd)->num_group; 841 bfd_boolean result = TRUE; 842 asection *s; 843 844 /* Process SHF_LINK_ORDER. */ 845 for (s = abfd->sections; s != NULL; s = s->next) 846 { 847 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; 848 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) 849 { 850 unsigned int elfsec = this_hdr->sh_link; 851 /* FIXME: The old Intel compiler and old strip/objcopy may 852 not set the sh_link or sh_info fields. Hence we could 853 get the situation where elfsec is 0. */ 854 if (elfsec == 0) 855 { 856 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 857 bed->link_order_error_handler 858 /* xgettext:c-format */ 859 (_("%pB: warning: sh_link not set for section `%pA'"), 860 abfd, s); 861 } 862 else 863 { 864 asection *linksec = NULL; 865 866 if (elfsec < elf_numsections (abfd)) 867 { 868 this_hdr = elf_elfsections (abfd)[elfsec]; 869 linksec = this_hdr->bfd_section; 870 } 871 872 /* PR 1991, 2008: 873 Some strip/objcopy may leave an incorrect value in 874 sh_link. We don't want to proceed. */ 875 if (linksec == NULL) 876 { 877 _bfd_error_handler 878 /* xgettext:c-format */ 879 (_("%pB: sh_link [%d] in section `%pA' is incorrect"), 880 s->owner, elfsec, s); 881 result = FALSE; 882 } 883 884 elf_linked_to_section (s) = linksec; 885 } 886 } 887 else if (this_hdr->sh_type == SHT_GROUP 888 && elf_next_in_group (s) == NULL) 889 { 890 _bfd_error_handler 891 /* xgettext:c-format */ 892 (_("%pB: SHT_GROUP section [index %d] has no SHF_GROUP sections"), 893 abfd, elf_section_data (s)->this_idx); 894 result = FALSE; 895 } 896 } 897 898 /* Process section groups. */ 899 if (num_group == (unsigned) -1) 900 return result; 901 902 for (i = 0; i < num_group; i++) 903 { 904 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; 905 Elf_Internal_Group *idx; 906 unsigned int n_elt; 907 908 /* PR binutils/18758: Beware of corrupt binaries with invalid group data. */ 909 if (shdr == NULL || shdr->bfd_section == NULL || shdr->contents == NULL) 910 { 911 _bfd_error_handler 912 /* xgettext:c-format */ 913 (_("%pB: section group entry number %u is corrupt"), 914 abfd, i); 915 result = FALSE; 916 continue; 917 } 918 919 idx = (Elf_Internal_Group *) shdr->contents; 920 n_elt = shdr->sh_size / 4; 921 922 while (--n_elt != 0) 923 { 924 ++ idx; 925 926 if (idx->shdr == NULL) 927 continue; 928 else if (idx->shdr->bfd_section) 929 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; 930 else if (idx->shdr->sh_type != SHT_RELA 931 && idx->shdr->sh_type != SHT_REL) 932 { 933 /* There are some unknown sections in the group. */ 934 _bfd_error_handler 935 /* xgettext:c-format */ 936 (_("%pB: unknown type [%#x] section `%s' in group [%pA]"), 937 abfd, 938 idx->shdr->sh_type, 939 bfd_elf_string_from_elf_section (abfd, 940 (elf_elfheader (abfd) 941 ->e_shstrndx), 942 idx->shdr->sh_name), 943 shdr->bfd_section); 944 result = FALSE; 945 } 946 } 947 } 948 949 return result; 950} 951 952bfd_boolean 953bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) 954{ 955 return elf_next_in_group (sec) != NULL; 956} 957 958const char * 959bfd_elf_group_name (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) 960{ 961 if (elf_sec_group (sec) != NULL) 962 return elf_group_name (sec); 963 return NULL; 964} 965 966static char * 967convert_debug_to_zdebug (bfd *abfd, const char *name) 968{ 969 unsigned int len = strlen (name); 970 char *new_name = bfd_alloc (abfd, len + 2); 971 if (new_name == NULL) 972 return NULL; 973 new_name[0] = '.'; 974 new_name[1] = 'z'; 975 memcpy (new_name + 2, name + 1, len); 976 return new_name; 977} 978 979static char * 980convert_zdebug_to_debug (bfd *abfd, const char *name) 981{ 982 unsigned int len = strlen (name); 983 char *new_name = bfd_alloc (abfd, len); 984 if (new_name == NULL) 985 return NULL; 986 new_name[0] = '.'; 987 memcpy (new_name + 1, name + 2, len - 1); 988 return new_name; 989} 990 991/* This a copy of lto_section defined in GCC (lto-streamer.h). */ 992 993struct lto_section 994{ 995 int16_t major_version; 996 int16_t minor_version; 997 unsigned char slim_object; 998 999 /* Flags is a private field that is not defined publicly. */ 1000 uint16_t flags; 1001}; 1002 1003/* Make a BFD section from an ELF section. We store a pointer to the 1004 BFD section in the bfd_section field of the header. */ 1005 1006bfd_boolean 1007_bfd_elf_make_section_from_shdr (bfd *abfd, 1008 Elf_Internal_Shdr *hdr, 1009 const char *name, 1010 int shindex) 1011{ 1012 asection *newsect; 1013 flagword flags; 1014 const struct elf_backend_data *bed; 1015 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 1016 1017 if (hdr->bfd_section != NULL) 1018 return TRUE; 1019 1020 newsect = bfd_make_section_anyway (abfd, name); 1021 if (newsect == NULL) 1022 return FALSE; 1023 1024 hdr->bfd_section = newsect; 1025 elf_section_data (newsect)->this_hdr = *hdr; 1026 elf_section_data (newsect)->this_idx = shindex; 1027 1028 /* Always use the real type/flags. */ 1029 elf_section_type (newsect) = hdr->sh_type; 1030 elf_section_flags (newsect) = hdr->sh_flags; 1031 1032 newsect->filepos = hdr->sh_offset; 1033 1034 flags = SEC_NO_FLAGS; 1035 if (hdr->sh_type != SHT_NOBITS) 1036 flags |= SEC_HAS_CONTENTS; 1037 if (hdr->sh_type == SHT_GROUP) 1038 flags |= SEC_GROUP; 1039 if ((hdr->sh_flags & SHF_ALLOC) != 0) 1040 { 1041 flags |= SEC_ALLOC; 1042 if (hdr->sh_type != SHT_NOBITS) 1043 flags |= SEC_LOAD; 1044 } 1045 if ((hdr->sh_flags & SHF_WRITE) == 0) 1046 flags |= SEC_READONLY; 1047 if ((hdr->sh_flags & SHF_EXECINSTR) != 0) 1048 flags |= SEC_CODE; 1049 else if ((flags & SEC_LOAD) != 0) 1050 flags |= SEC_DATA; 1051 if ((hdr->sh_flags & SHF_MERGE) != 0) 1052 { 1053 flags |= SEC_MERGE; 1054 newsect->entsize = hdr->sh_entsize; 1055 } 1056 if ((hdr->sh_flags & SHF_STRINGS) != 0) 1057 flags |= SEC_STRINGS; 1058 if (hdr->sh_flags & SHF_GROUP) 1059 if (!setup_group (abfd, hdr, newsect)) 1060 return FALSE; 1061 if ((hdr->sh_flags & SHF_TLS) != 0) 1062 flags |= SEC_THREAD_LOCAL; 1063 if ((hdr->sh_flags & SHF_EXCLUDE) != 0) 1064 flags |= SEC_EXCLUDE; 1065 1066 switch (elf_elfheader (abfd)->e_ident[EI_OSABI]) 1067 { 1068 /* FIXME: We should not recognize SHF_GNU_MBIND for ELFOSABI_NONE, 1069 but binutils as of 2019-07-23 did not set the EI_OSABI header 1070 byte. */ 1071 case ELFOSABI_NONE: 1072 case ELFOSABI_GNU: 1073 case ELFOSABI_FREEBSD: 1074 if ((hdr->sh_flags & SHF_GNU_MBIND) != 0) 1075 elf_tdata (abfd)->has_gnu_osabi |= elf_gnu_osabi_mbind; 1076 break; 1077 } 1078 1079 if ((flags & SEC_ALLOC) == 0) 1080 { 1081 /* The debugging sections appear to be recognized only by name, 1082 not any sort of flag. Their SEC_ALLOC bits are cleared. */ 1083 if (name [0] == '.') 1084 { 1085 if (strncmp (name, ".debug", 6) == 0 1086 || strncmp (name, ".gnu.linkonce.wi.", 17) == 0 1087 || strncmp (name, ".zdebug", 7) == 0) 1088 flags |= SEC_DEBUGGING | SEC_ELF_OCTETS; 1089 else if (strncmp (name, GNU_BUILD_ATTRS_SECTION_NAME, 21) == 0 1090 || strncmp (name, ".note.gnu", 9) == 0) 1091 { 1092 flags |= SEC_ELF_OCTETS; 1093 opb = 1; 1094 } 1095 else if (strncmp (name, ".line", 5) == 0 1096 || strncmp (name, ".stab", 5) == 0 1097 || strcmp (name, ".gdb_index") == 0) 1098 flags |= SEC_DEBUGGING; 1099 } 1100 } 1101 1102 if (!bfd_set_section_vma (newsect, hdr->sh_addr / opb) 1103 || !bfd_set_section_size (newsect, hdr->sh_size) 1104 || !bfd_set_section_alignment (newsect, bfd_log2 (hdr->sh_addralign))) 1105 return FALSE; 1106 1107 /* As a GNU extension, if the name begins with .gnu.linkonce, we 1108 only link a single copy of the section. This is used to support 1109 g++. g++ will emit each template expansion in its own section. 1110 The symbols will be defined as weak, so that multiple definitions 1111 are permitted. The GNU linker extension is to actually discard 1112 all but one of the sections. */ 1113 if (CONST_STRNEQ (name, ".gnu.linkonce") 1114 && elf_next_in_group (newsect) == NULL) 1115 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 1116 1117 if (!bfd_set_section_flags (newsect, flags)) 1118 return FALSE; 1119 1120 bed = get_elf_backend_data (abfd); 1121 if (bed->elf_backend_section_flags) 1122 if (!bed->elf_backend_section_flags (hdr)) 1123 return FALSE; 1124 1125 /* We do not parse the PT_NOTE segments as we are interested even in the 1126 separate debug info files which may have the segments offsets corrupted. 1127 PT_NOTEs from the core files are currently not parsed using BFD. */ 1128 if (hdr->sh_type == SHT_NOTE) 1129 { 1130 bfd_byte *contents; 1131 1132 if (!bfd_malloc_and_get_section (abfd, newsect, &contents)) 1133 return FALSE; 1134 1135 elf_parse_notes (abfd, (char *) contents, hdr->sh_size, 1136 hdr->sh_offset, hdr->sh_addralign); 1137 free (contents); 1138 } 1139 1140 if ((newsect->flags & SEC_ALLOC) != 0) 1141 { 1142 Elf_Internal_Phdr *phdr; 1143 unsigned int i, nload; 1144 1145 /* Some ELF linkers produce binaries with all the program header 1146 p_paddr fields zero. If we have such a binary with more than 1147 one PT_LOAD header, then leave the section lma equal to vma 1148 so that we don't create sections with overlapping lma. */ 1149 phdr = elf_tdata (abfd)->phdr; 1150 for (nload = 0, i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 1151 if (phdr->p_paddr != 0) 1152 break; 1153 else if (phdr->p_type == PT_LOAD && phdr->p_memsz != 0) 1154 ++nload; 1155 if (i >= elf_elfheader (abfd)->e_phnum && nload > 1) 1156 return TRUE; 1157 1158 phdr = elf_tdata (abfd)->phdr; 1159 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 1160 { 1161 if (((phdr->p_type == PT_LOAD 1162 && (hdr->sh_flags & SHF_TLS) == 0) 1163 || phdr->p_type == PT_TLS) 1164 && ELF_SECTION_IN_SEGMENT (hdr, phdr)) 1165 { 1166 if ((newsect->flags & SEC_LOAD) == 0) 1167 newsect->lma = (phdr->p_paddr 1168 + hdr->sh_addr - phdr->p_vaddr) / opb; 1169 else 1170 /* We used to use the same adjustment for SEC_LOAD 1171 sections, but that doesn't work if the segment 1172 is packed with code from multiple VMAs. 1173 Instead we calculate the section LMA based on 1174 the segment LMA. It is assumed that the 1175 segment will contain sections with contiguous 1176 LMAs, even if the VMAs are not. */ 1177 newsect->lma = (phdr->p_paddr 1178 + hdr->sh_offset - phdr->p_offset) / opb; 1179 1180 /* With contiguous segments, we can't tell from file 1181 offsets whether a section with zero size should 1182 be placed at the end of one segment or the 1183 beginning of the next. Decide based on vaddr. */ 1184 if (hdr->sh_addr >= phdr->p_vaddr 1185 && (hdr->sh_addr + hdr->sh_size 1186 <= phdr->p_vaddr + phdr->p_memsz)) 1187 break; 1188 } 1189 } 1190 } 1191 1192 /* Compress/decompress DWARF debug sections with names: .debug_* and 1193 .zdebug_*, after the section flags is set. */ 1194 if ((newsect->flags & SEC_DEBUGGING) 1195 && ((name[1] == 'd' && name[6] == '_') 1196 || (name[1] == 'z' && name[7] == '_'))) 1197 { 1198 enum { nothing, compress, decompress } action = nothing; 1199 int compression_header_size; 1200 bfd_size_type uncompressed_size; 1201 unsigned int uncompressed_align_power; 1202 bfd_boolean compressed 1203 = bfd_is_section_compressed_with_header (abfd, newsect, 1204 &compression_header_size, 1205 &uncompressed_size, 1206 &uncompressed_align_power); 1207 if (compressed) 1208 { 1209 /* Compressed section. Check if we should decompress. */ 1210 if ((abfd->flags & BFD_DECOMPRESS)) 1211 action = decompress; 1212 } 1213 1214 /* Compress the uncompressed section or convert from/to .zdebug* 1215 section. Check if we should compress. */ 1216 if (action == nothing) 1217 { 1218 if (newsect->size != 0 1219 && (abfd->flags & BFD_COMPRESS) 1220 && compression_header_size >= 0 1221 && uncompressed_size > 0 1222 && (!compressed 1223 || ((compression_header_size > 0) 1224 != ((abfd->flags & BFD_COMPRESS_GABI) != 0)))) 1225 action = compress; 1226 else 1227 return TRUE; 1228 } 1229 1230 if (action == compress) 1231 { 1232 if (!bfd_init_section_compress_status (abfd, newsect)) 1233 { 1234 _bfd_error_handler 1235 /* xgettext:c-format */ 1236 (_("%pB: unable to initialize compress status for section %s"), 1237 abfd, name); 1238 return FALSE; 1239 } 1240 } 1241 else 1242 { 1243 if (!bfd_init_section_decompress_status (abfd, newsect)) 1244 { 1245 _bfd_error_handler 1246 /* xgettext:c-format */ 1247 (_("%pB: unable to initialize decompress status for section %s"), 1248 abfd, name); 1249 return FALSE; 1250 } 1251 } 1252 1253 if (abfd->is_linker_input) 1254 { 1255 if (name[1] == 'z' 1256 && (action == decompress 1257 || (action == compress 1258 && (abfd->flags & BFD_COMPRESS_GABI) != 0))) 1259 { 1260 /* Convert section name from .zdebug_* to .debug_* so 1261 that linker will consider this section as a debug 1262 section. */ 1263 char *new_name = convert_zdebug_to_debug (abfd, name); 1264 if (new_name == NULL) 1265 return FALSE; 1266 bfd_rename_section (newsect, new_name); 1267 } 1268 } 1269 else 1270 /* For objdump, don't rename the section. For objcopy, delay 1271 section rename to elf_fake_sections. */ 1272 newsect->flags |= SEC_ELF_RENAME; 1273 } 1274 1275 /* GCC uses .gnu.lto_.lto.<some_hash> as a LTO bytecode information 1276 section. */ 1277 const char *lto_section_name = ".gnu.lto_.lto."; 1278 if (strncmp (name, lto_section_name, strlen (lto_section_name)) == 0) 1279 { 1280 struct lto_section lsection; 1281 if (bfd_get_section_contents (abfd, newsect, &lsection, 0, 1282 sizeof (struct lto_section))) 1283 abfd->lto_slim_object = lsection.slim_object; 1284 } 1285 1286 return TRUE; 1287} 1288 1289const char *const bfd_elf_section_type_names[] = 1290{ 1291 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", 1292 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", 1293 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", 1294}; 1295 1296/* ELF relocs are against symbols. If we are producing relocatable 1297 output, and the reloc is against an external symbol, and nothing 1298 has given us any additional addend, the resulting reloc will also 1299 be against the same symbol. In such a case, we don't want to 1300 change anything about the way the reloc is handled, since it will 1301 all be done at final link time. Rather than put special case code 1302 into bfd_perform_relocation, all the reloc types use this howto 1303 function. It just short circuits the reloc if producing 1304 relocatable output against an external symbol. */ 1305 1306bfd_reloc_status_type 1307bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, 1308 arelent *reloc_entry, 1309 asymbol *symbol, 1310 void *data ATTRIBUTE_UNUSED, 1311 asection *input_section, 1312 bfd *output_bfd, 1313 char **error_message ATTRIBUTE_UNUSED) 1314{ 1315 if (output_bfd != NULL 1316 && (symbol->flags & BSF_SECTION_SYM) == 0 1317 && (! reloc_entry->howto->partial_inplace 1318 || reloc_entry->addend == 0)) 1319 { 1320 reloc_entry->address += input_section->output_offset; 1321 return bfd_reloc_ok; 1322 } 1323 1324 return bfd_reloc_continue; 1325} 1326 1327/* Returns TRUE if section A matches section B. 1328 Names, addresses and links may be different, but everything else 1329 should be the same. */ 1330 1331static bfd_boolean 1332section_match (const Elf_Internal_Shdr * a, 1333 const Elf_Internal_Shdr * b) 1334{ 1335 if (a->sh_type != b->sh_type 1336 || ((a->sh_flags ^ b->sh_flags) & ~SHF_INFO_LINK) != 0 1337 || a->sh_addralign != b->sh_addralign 1338 || a->sh_entsize != b->sh_entsize) 1339 return FALSE; 1340 if (a->sh_type == SHT_SYMTAB 1341 || a->sh_type == SHT_STRTAB) 1342 return TRUE; 1343 return a->sh_size == b->sh_size; 1344} 1345 1346/* Find a section in OBFD that has the same characteristics 1347 as IHEADER. Return the index of this section or SHN_UNDEF if 1348 none can be found. Check's section HINT first, as this is likely 1349 to be the correct section. */ 1350 1351static unsigned int 1352find_link (const bfd *obfd, const Elf_Internal_Shdr *iheader, 1353 const unsigned int hint) 1354{ 1355 Elf_Internal_Shdr ** oheaders = elf_elfsections (obfd); 1356 unsigned int i; 1357 1358 BFD_ASSERT (iheader != NULL); 1359 1360 /* See PR 20922 for a reproducer of the NULL test. */ 1361 if (hint < elf_numsections (obfd) 1362 && oheaders[hint] != NULL 1363 && section_match (oheaders[hint], iheader)) 1364 return hint; 1365 1366 for (i = 1; i < elf_numsections (obfd); i++) 1367 { 1368 Elf_Internal_Shdr * oheader = oheaders[i]; 1369 1370 if (oheader == NULL) 1371 continue; 1372 if (section_match (oheader, iheader)) 1373 /* FIXME: Do we care if there is a potential for 1374 multiple matches ? */ 1375 return i; 1376 } 1377 1378 return SHN_UNDEF; 1379} 1380 1381/* PR 19938: Attempt to set the ELF section header fields of an OS or 1382 Processor specific section, based upon a matching input section. 1383 Returns TRUE upon success, FALSE otherwise. */ 1384 1385static bfd_boolean 1386copy_special_section_fields (const bfd *ibfd, 1387 bfd *obfd, 1388 const Elf_Internal_Shdr *iheader, 1389 Elf_Internal_Shdr *oheader, 1390 const unsigned int secnum) 1391{ 1392 const struct elf_backend_data *bed = get_elf_backend_data (obfd); 1393 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); 1394 bfd_boolean changed = FALSE; 1395 unsigned int sh_link; 1396 1397 if (oheader->sh_type == SHT_NOBITS) 1398 { 1399 /* This is a feature for objcopy --only-keep-debug: 1400 When a section's type is changed to NOBITS, we preserve 1401 the sh_link and sh_info fields so that they can be 1402 matched up with the original. 1403 1404 Note: Strictly speaking these assignments are wrong. 1405 The sh_link and sh_info fields should point to the 1406 relevent sections in the output BFD, which may not be in 1407 the same location as they were in the input BFD. But 1408 the whole point of this action is to preserve the 1409 original values of the sh_link and sh_info fields, so 1410 that they can be matched up with the section headers in 1411 the original file. So strictly speaking we may be 1412 creating an invalid ELF file, but it is only for a file 1413 that just contains debug info and only for sections 1414 without any contents. */ 1415 if (oheader->sh_link == 0) 1416 oheader->sh_link = iheader->sh_link; 1417 if (oheader->sh_info == 0) 1418 oheader->sh_info = iheader->sh_info; 1419 return TRUE; 1420 } 1421 1422 /* Allow the target a chance to decide how these fields should be set. */ 1423 if (bed->elf_backend_copy_special_section_fields (ibfd, obfd, 1424 iheader, oheader)) 1425 return TRUE; 1426 1427 /* We have an iheader which might match oheader, and which has non-zero 1428 sh_info and/or sh_link fields. Attempt to follow those links and find 1429 the section in the output bfd which corresponds to the linked section 1430 in the input bfd. */ 1431 if (iheader->sh_link != SHN_UNDEF) 1432 { 1433 /* See PR 20931 for a reproducer. */ 1434 if (iheader->sh_link >= elf_numsections (ibfd)) 1435 { 1436 _bfd_error_handler 1437 /* xgettext:c-format */ 1438 (_("%pB: invalid sh_link field (%d) in section number %d"), 1439 ibfd, iheader->sh_link, secnum); 1440 return FALSE; 1441 } 1442 1443 sh_link = find_link (obfd, iheaders[iheader->sh_link], iheader->sh_link); 1444 if (sh_link != SHN_UNDEF) 1445 { 1446 oheader->sh_link = sh_link; 1447 changed = TRUE; 1448 } 1449 else 1450 /* FIXME: Should we install iheader->sh_link 1451 if we could not find a match ? */ 1452 _bfd_error_handler 1453 /* xgettext:c-format */ 1454 (_("%pB: failed to find link section for section %d"), obfd, secnum); 1455 } 1456 1457 if (iheader->sh_info) 1458 { 1459 /* The sh_info field can hold arbitrary information, but if the 1460 SHF_LINK_INFO flag is set then it should be interpreted as a 1461 section index. */ 1462 if (iheader->sh_flags & SHF_INFO_LINK) 1463 { 1464 sh_link = find_link (obfd, iheaders[iheader->sh_info], 1465 iheader->sh_info); 1466 if (sh_link != SHN_UNDEF) 1467 oheader->sh_flags |= SHF_INFO_LINK; 1468 } 1469 else 1470 /* No idea what it means - just copy it. */ 1471 sh_link = iheader->sh_info; 1472 1473 if (sh_link != SHN_UNDEF) 1474 { 1475 oheader->sh_info = sh_link; 1476 changed = TRUE; 1477 } 1478 else 1479 _bfd_error_handler 1480 /* xgettext:c-format */ 1481 (_("%pB: failed to find info section for section %d"), obfd, secnum); 1482 } 1483 1484 return changed; 1485} 1486 1487/* Copy the program header and other data from one object module to 1488 another. */ 1489 1490bfd_boolean 1491_bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) 1492{ 1493 const Elf_Internal_Shdr **iheaders = (const Elf_Internal_Shdr **) elf_elfsections (ibfd); 1494 Elf_Internal_Shdr **oheaders = elf_elfsections (obfd); 1495 const struct elf_backend_data *bed; 1496 unsigned int i; 1497 1498 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 1499 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 1500 return TRUE; 1501 1502 if (!elf_flags_init (obfd)) 1503 { 1504 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; 1505 elf_flags_init (obfd) = TRUE; 1506 } 1507 1508 elf_gp (obfd) = elf_gp (ibfd); 1509 1510 /* Also copy the EI_OSABI field. */ 1511 elf_elfheader (obfd)->e_ident[EI_OSABI] = 1512 elf_elfheader (ibfd)->e_ident[EI_OSABI]; 1513 1514 /* If set, copy the EI_ABIVERSION field. */ 1515 if (elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]) 1516 elf_elfheader (obfd)->e_ident[EI_ABIVERSION] 1517 = elf_elfheader (ibfd)->e_ident[EI_ABIVERSION]; 1518 1519 /* Copy object attributes. */ 1520 _bfd_elf_copy_obj_attributes (ibfd, obfd); 1521 1522 if (iheaders == NULL || oheaders == NULL) 1523 return TRUE; 1524 1525 bed = get_elf_backend_data (obfd); 1526 1527 /* Possibly copy other fields in the section header. */ 1528 for (i = 1; i < elf_numsections (obfd); i++) 1529 { 1530 unsigned int j; 1531 Elf_Internal_Shdr * oheader = oheaders[i]; 1532 1533 /* Ignore ordinary sections. SHT_NOBITS sections are considered however 1534 because of a special case need for generating separate debug info 1535 files. See below for more details. */ 1536 if (oheader == NULL 1537 || (oheader->sh_type != SHT_NOBITS 1538 && oheader->sh_type < SHT_LOOS)) 1539 continue; 1540 1541 /* Ignore empty sections, and sections whose 1542 fields have already been initialised. */ 1543 if (oheader->sh_size == 0 1544 || (oheader->sh_info != 0 && oheader->sh_link != 0)) 1545 continue; 1546 1547 /* Scan for the matching section in the input bfd. 1548 First we try for a direct mapping between the input and output sections. */ 1549 for (j = 1; j < elf_numsections (ibfd); j++) 1550 { 1551 const Elf_Internal_Shdr * iheader = iheaders[j]; 1552 1553 if (iheader == NULL) 1554 continue; 1555 1556 if (oheader->bfd_section != NULL 1557 && iheader->bfd_section != NULL 1558 && iheader->bfd_section->output_section != NULL 1559 && iheader->bfd_section->output_section == oheader->bfd_section) 1560 { 1561 /* We have found a connection from the input section to the 1562 output section. Attempt to copy the header fields. If 1563 this fails then do not try any further sections - there 1564 should only be a one-to-one mapping between input and output. */ 1565 if (! copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) 1566 j = elf_numsections (ibfd); 1567 break; 1568 } 1569 } 1570 1571 if (j < elf_numsections (ibfd)) 1572 continue; 1573 1574 /* That failed. So try to deduce the corresponding input section. 1575 Unfortunately we cannot compare names as the output string table 1576 is empty, so instead we check size, address and type. */ 1577 for (j = 1; j < elf_numsections (ibfd); j++) 1578 { 1579 const Elf_Internal_Shdr * iheader = iheaders[j]; 1580 1581 if (iheader == NULL) 1582 continue; 1583 1584 /* Try matching fields in the input section's header. 1585 Since --only-keep-debug turns all non-debug sections into 1586 SHT_NOBITS sections, the output SHT_NOBITS type matches any 1587 input type. */ 1588 if ((oheader->sh_type == SHT_NOBITS 1589 || iheader->sh_type == oheader->sh_type) 1590 && (iheader->sh_flags & ~ SHF_INFO_LINK) 1591 == (oheader->sh_flags & ~ SHF_INFO_LINK) 1592 && iheader->sh_addralign == oheader->sh_addralign 1593 && iheader->sh_entsize == oheader->sh_entsize 1594 && iheader->sh_size == oheader->sh_size 1595 && iheader->sh_addr == oheader->sh_addr 1596 && (iheader->sh_info != oheader->sh_info 1597 || iheader->sh_link != oheader->sh_link)) 1598 { 1599 if (copy_special_section_fields (ibfd, obfd, iheader, oheader, i)) 1600 break; 1601 } 1602 } 1603 1604 if (j == elf_numsections (ibfd) && oheader->sh_type >= SHT_LOOS) 1605 { 1606 /* Final attempt. Call the backend copy function 1607 with a NULL input section. */ 1608 (void) bed->elf_backend_copy_special_section_fields (ibfd, obfd, 1609 NULL, oheader); 1610 } 1611 } 1612 1613 return TRUE; 1614} 1615 1616static const char * 1617get_segment_type (unsigned int p_type) 1618{ 1619 const char *pt; 1620 switch (p_type) 1621 { 1622 case PT_NULL: pt = "NULL"; break; 1623 case PT_LOAD: pt = "LOAD"; break; 1624 case PT_DYNAMIC: pt = "DYNAMIC"; break; 1625 case PT_INTERP: pt = "INTERP"; break; 1626 case PT_NOTE: pt = "NOTE"; break; 1627 case PT_SHLIB: pt = "SHLIB"; break; 1628 case PT_PHDR: pt = "PHDR"; break; 1629 case PT_TLS: pt = "TLS"; break; 1630 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; 1631 case PT_GNU_STACK: pt = "STACK"; break; 1632 case PT_GNU_RELRO: pt = "RELRO"; break; 1633 default: pt = NULL; break; 1634 } 1635 return pt; 1636} 1637 1638/* Print out the program headers. */ 1639 1640bfd_boolean 1641_bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) 1642{ 1643 FILE *f = (FILE *) farg; 1644 Elf_Internal_Phdr *p; 1645 asection *s; 1646 bfd_byte *dynbuf = NULL; 1647 1648 p = elf_tdata (abfd)->phdr; 1649 if (p != NULL) 1650 { 1651 unsigned int i, c; 1652 1653 fprintf (f, _("\nProgram Header:\n")); 1654 c = elf_elfheader (abfd)->e_phnum; 1655 for (i = 0; i < c; i++, p++) 1656 { 1657 const char *pt = get_segment_type (p->p_type); 1658 char buf[20]; 1659 1660 if (pt == NULL) 1661 { 1662 sprintf (buf, "0x%lx", p->p_type); 1663 pt = buf; 1664 } 1665 fprintf (f, "%8s off 0x", pt); 1666 bfd_fprintf_vma (abfd, f, p->p_offset); 1667 fprintf (f, " vaddr 0x"); 1668 bfd_fprintf_vma (abfd, f, p->p_vaddr); 1669 fprintf (f, " paddr 0x"); 1670 bfd_fprintf_vma (abfd, f, p->p_paddr); 1671 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); 1672 fprintf (f, " filesz 0x"); 1673 bfd_fprintf_vma (abfd, f, p->p_filesz); 1674 fprintf (f, " memsz 0x"); 1675 bfd_fprintf_vma (abfd, f, p->p_memsz); 1676 fprintf (f, " flags %c%c%c", 1677 (p->p_flags & PF_R) != 0 ? 'r' : '-', 1678 (p->p_flags & PF_W) != 0 ? 'w' : '-', 1679 (p->p_flags & PF_X) != 0 ? 'x' : '-'); 1680 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) 1681 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); 1682 fprintf (f, "\n"); 1683 } 1684 } 1685 1686 s = bfd_get_section_by_name (abfd, ".dynamic"); 1687 if (s != NULL) 1688 { 1689 unsigned int elfsec; 1690 unsigned long shlink; 1691 bfd_byte *extdyn, *extdynend; 1692 size_t extdynsize; 1693 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 1694 1695 fprintf (f, _("\nDynamic Section:\n")); 1696 1697 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) 1698 goto error_return; 1699 1700 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 1701 if (elfsec == SHN_BAD) 1702 goto error_return; 1703 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 1704 1705 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 1706 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 1707 1708 extdyn = dynbuf; 1709 /* PR 17512: file: 6f427532. */ 1710 if (s->size < extdynsize) 1711 goto error_return; 1712 extdynend = extdyn + s->size; 1713 /* PR 17512: file: id:000006,sig:06,src:000000,op:flip4,pos:5664. 1714 Fix range check. */ 1715 for (; extdyn <= (extdynend - extdynsize); extdyn += extdynsize) 1716 { 1717 Elf_Internal_Dyn dyn; 1718 const char *name = ""; 1719 char ab[20]; 1720 bfd_boolean stringp; 1721 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 1722 1723 (*swap_dyn_in) (abfd, extdyn, &dyn); 1724 1725 if (dyn.d_tag == DT_NULL) 1726 break; 1727 1728 stringp = FALSE; 1729 switch (dyn.d_tag) 1730 { 1731 default: 1732 if (bed->elf_backend_get_target_dtag) 1733 name = (*bed->elf_backend_get_target_dtag) (dyn.d_tag); 1734 1735 if (!strcmp (name, "")) 1736 { 1737 sprintf (ab, "%#" BFD_VMA_FMT "x", dyn.d_tag); 1738 name = ab; 1739 } 1740 break; 1741 1742 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; 1743 case DT_PLTRELSZ: name = "PLTRELSZ"; break; 1744 case DT_PLTGOT: name = "PLTGOT"; break; 1745 case DT_HASH: name = "HASH"; break; 1746 case DT_STRTAB: name = "STRTAB"; break; 1747 case DT_SYMTAB: name = "SYMTAB"; break; 1748 case DT_RELA: name = "RELA"; break; 1749 case DT_RELASZ: name = "RELASZ"; break; 1750 case DT_RELAENT: name = "RELAENT"; break; 1751 case DT_STRSZ: name = "STRSZ"; break; 1752 case DT_SYMENT: name = "SYMENT"; break; 1753 case DT_INIT: name = "INIT"; break; 1754 case DT_FINI: name = "FINI"; break; 1755 case DT_SONAME: name = "SONAME"; stringp = TRUE; break; 1756 case DT_RPATH: name = "RPATH"; stringp = TRUE; break; 1757 case DT_SYMBOLIC: name = "SYMBOLIC"; break; 1758 case DT_REL: name = "REL"; break; 1759 case DT_RELSZ: name = "RELSZ"; break; 1760 case DT_RELENT: name = "RELENT"; break; 1761 case DT_PLTREL: name = "PLTREL"; break; 1762 case DT_DEBUG: name = "DEBUG"; break; 1763 case DT_TEXTREL: name = "TEXTREL"; break; 1764 case DT_JMPREL: name = "JMPREL"; break; 1765 case DT_BIND_NOW: name = "BIND_NOW"; break; 1766 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; 1767 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; 1768 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; 1769 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; 1770 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; 1771 case DT_FLAGS: name = "FLAGS"; break; 1772 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; 1773 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; 1774 case DT_CHECKSUM: name = "CHECKSUM"; break; 1775 case DT_PLTPADSZ: name = "PLTPADSZ"; break; 1776 case DT_MOVEENT: name = "MOVEENT"; break; 1777 case DT_MOVESZ: name = "MOVESZ"; break; 1778 case DT_FEATURE: name = "FEATURE"; break; 1779 case DT_POSFLAG_1: name = "POSFLAG_1"; break; 1780 case DT_SYMINSZ: name = "SYMINSZ"; break; 1781 case DT_SYMINENT: name = "SYMINENT"; break; 1782 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; 1783 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; 1784 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; 1785 case DT_PLTPAD: name = "PLTPAD"; break; 1786 case DT_MOVETAB: name = "MOVETAB"; break; 1787 case DT_SYMINFO: name = "SYMINFO"; break; 1788 case DT_RELACOUNT: name = "RELACOUNT"; break; 1789 case DT_RELCOUNT: name = "RELCOUNT"; break; 1790 case DT_FLAGS_1: name = "FLAGS_1"; break; 1791 case DT_VERSYM: name = "VERSYM"; break; 1792 case DT_VERDEF: name = "VERDEF"; break; 1793 case DT_VERDEFNUM: name = "VERDEFNUM"; break; 1794 case DT_VERNEED: name = "VERNEED"; break; 1795 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; 1796 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; 1797 case DT_USED: name = "USED"; break; 1798 case DT_FILTER: name = "FILTER"; stringp = TRUE; break; 1799 case DT_GNU_HASH: name = "GNU_HASH"; break; 1800 } 1801 1802 fprintf (f, " %-20s ", name); 1803 if (! stringp) 1804 { 1805 fprintf (f, "0x"); 1806 bfd_fprintf_vma (abfd, f, dyn.d_un.d_val); 1807 } 1808 else 1809 { 1810 const char *string; 1811 unsigned int tagv = dyn.d_un.d_val; 1812 1813 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 1814 if (string == NULL) 1815 goto error_return; 1816 fprintf (f, "%s", string); 1817 } 1818 fprintf (f, "\n"); 1819 } 1820 1821 free (dynbuf); 1822 dynbuf = NULL; 1823 } 1824 1825 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) 1826 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) 1827 { 1828 if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) 1829 return FALSE; 1830 } 1831 1832 if (elf_dynverdef (abfd) != 0) 1833 { 1834 Elf_Internal_Verdef *t; 1835 1836 fprintf (f, _("\nVersion definitions:\n")); 1837 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) 1838 { 1839 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, 1840 t->vd_flags, t->vd_hash, 1841 t->vd_nodename ? t->vd_nodename : "<corrupt>"); 1842 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) 1843 { 1844 Elf_Internal_Verdaux *a; 1845 1846 fprintf (f, "\t"); 1847 for (a = t->vd_auxptr->vda_nextptr; 1848 a != NULL; 1849 a = a->vda_nextptr) 1850 fprintf (f, "%s ", 1851 a->vda_nodename ? a->vda_nodename : "<corrupt>"); 1852 fprintf (f, "\n"); 1853 } 1854 } 1855 } 1856 1857 if (elf_dynverref (abfd) != 0) 1858 { 1859 Elf_Internal_Verneed *t; 1860 1861 fprintf (f, _("\nVersion References:\n")); 1862 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) 1863 { 1864 Elf_Internal_Vernaux *a; 1865 1866 fprintf (f, _(" required from %s:\n"), 1867 t->vn_filename ? t->vn_filename : "<corrupt>"); 1868 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 1869 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, 1870 a->vna_flags, a->vna_other, 1871 a->vna_nodename ? a->vna_nodename : "<corrupt>"); 1872 } 1873 } 1874 1875 return TRUE; 1876 1877 error_return: 1878 free (dynbuf); 1879 return FALSE; 1880} 1881 1882/* Get version name. If BASE_P is TRUE, return "Base" for VER_FLG_BASE 1883 and return symbol version for symbol version itself. */ 1884 1885const char * 1886_bfd_elf_get_symbol_version_string (bfd *abfd, asymbol *symbol, 1887 bfd_boolean base_p, 1888 bfd_boolean *hidden) 1889{ 1890 const char *version_string = NULL; 1891 if (elf_dynversym (abfd) != 0 1892 && (elf_dynverdef (abfd) != 0 || elf_dynverref (abfd) != 0)) 1893 { 1894 unsigned int vernum = ((elf_symbol_type *) symbol)->version; 1895 1896 *hidden = (vernum & VERSYM_HIDDEN) != 0; 1897 vernum &= VERSYM_VERSION; 1898 1899 if (vernum == 0) 1900 version_string = ""; 1901 else if (vernum == 1 1902 && (vernum > elf_tdata (abfd)->cverdefs 1903 || (elf_tdata (abfd)->verdef[0].vd_flags 1904 == VER_FLG_BASE))) 1905 version_string = base_p ? "Base" : ""; 1906 else if (vernum <= elf_tdata (abfd)->cverdefs) 1907 { 1908 const char *nodename 1909 = elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 1910 version_string = ""; 1911 if (base_p 1912 || nodename == NULL 1913 || symbol->name == NULL 1914 || strcmp (symbol->name, nodename) != 0) 1915 version_string = nodename; 1916 } 1917 else 1918 { 1919 Elf_Internal_Verneed *t; 1920 1921 version_string = _("<corrupt>"); 1922 for (t = elf_tdata (abfd)->verref; 1923 t != NULL; 1924 t = t->vn_nextref) 1925 { 1926 Elf_Internal_Vernaux *a; 1927 1928 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 1929 { 1930 if (a->vna_other == vernum) 1931 { 1932 version_string = a->vna_nodename; 1933 break; 1934 } 1935 } 1936 } 1937 } 1938 } 1939 return version_string; 1940} 1941 1942/* Display ELF-specific fields of a symbol. */ 1943 1944void 1945bfd_elf_print_symbol (bfd *abfd, 1946 void *filep, 1947 asymbol *symbol, 1948 bfd_print_symbol_type how) 1949{ 1950 FILE *file = (FILE *) filep; 1951 switch (how) 1952 { 1953 case bfd_print_symbol_name: 1954 fprintf (file, "%s", symbol->name); 1955 break; 1956 case bfd_print_symbol_more: 1957 fprintf (file, "elf "); 1958 bfd_fprintf_vma (abfd, file, symbol->value); 1959 fprintf (file, " %x", symbol->flags); 1960 break; 1961 case bfd_print_symbol_all: 1962 { 1963 const char *section_name; 1964 const char *name = NULL; 1965 const struct elf_backend_data *bed; 1966 unsigned char st_other; 1967 bfd_vma val; 1968 const char *version_string; 1969 bfd_boolean hidden; 1970 1971 section_name = symbol->section ? symbol->section->name : "(*none*)"; 1972 1973 bed = get_elf_backend_data (abfd); 1974 if (bed->elf_backend_print_symbol_all) 1975 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); 1976 1977 if (name == NULL) 1978 { 1979 name = symbol->name; 1980 bfd_print_symbol_vandf (abfd, file, symbol); 1981 } 1982 1983 fprintf (file, " %s\t", section_name); 1984 /* Print the "other" value for a symbol. For common symbols, 1985 we've already printed the size; now print the alignment. 1986 For other symbols, we have no specified alignment, and 1987 we've printed the address; now print the size. */ 1988 if (symbol->section && bfd_is_com_section (symbol->section)) 1989 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; 1990 else 1991 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; 1992 bfd_fprintf_vma (abfd, file, val); 1993 1994 /* If we have version information, print it. */ 1995 version_string = _bfd_elf_get_symbol_version_string (abfd, 1996 symbol, 1997 TRUE, 1998 &hidden); 1999 if (version_string) 2000 { 2001 if (!hidden) 2002 fprintf (file, " %-11s", version_string); 2003 else 2004 { 2005 int i; 2006 2007 fprintf (file, " (%s)", version_string); 2008 for (i = 10 - strlen (version_string); i > 0; --i) 2009 putc (' ', file); 2010 } 2011 } 2012 2013 /* If the st_other field is not zero, print it. */ 2014 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; 2015 2016 switch (st_other) 2017 { 2018 case 0: break; 2019 case STV_INTERNAL: fprintf (file, " .internal"); break; 2020 case STV_HIDDEN: fprintf (file, " .hidden"); break; 2021 case STV_PROTECTED: fprintf (file, " .protected"); break; 2022 default: 2023 /* Some other non-defined flags are also present, so print 2024 everything hex. */ 2025 fprintf (file, " 0x%02x", (unsigned int) st_other); 2026 } 2027 2028 fprintf (file, " %s", name); 2029 } 2030 break; 2031 } 2032} 2033 2034/* ELF .o/exec file reading */ 2035 2036/* Create a new bfd section from an ELF section header. */ 2037 2038bfd_boolean 2039bfd_section_from_shdr (bfd *abfd, unsigned int shindex) 2040{ 2041 Elf_Internal_Shdr *hdr; 2042 Elf_Internal_Ehdr *ehdr; 2043 const struct elf_backend_data *bed; 2044 const char *name; 2045 bfd_boolean ret = TRUE; 2046 static bfd_boolean * sections_being_created = NULL; 2047 static bfd * sections_being_created_abfd = NULL; 2048 static unsigned int nesting = 0; 2049 2050 if (shindex >= elf_numsections (abfd)) 2051 return FALSE; 2052 2053 if (++ nesting > 3) 2054 { 2055 /* PR17512: A corrupt ELF binary might contain a recursive group of 2056 sections, with each the string indices pointing to the next in the 2057 loop. Detect this here, by refusing to load a section that we are 2058 already in the process of loading. We only trigger this test if 2059 we have nested at least three sections deep as normal ELF binaries 2060 can expect to recurse at least once. 2061 2062 FIXME: It would be better if this array was attached to the bfd, 2063 rather than being held in a static pointer. */ 2064 2065 if (sections_being_created_abfd != abfd) 2066 sections_being_created = NULL; 2067 if (sections_being_created == NULL) 2068 { 2069 size_t amt = elf_numsections (abfd) * sizeof (bfd_boolean); 2070 2071 /* PR 26005: Do not use bfd_zalloc here as the memory might 2072 be released before the bfd has been fully scanned. */ 2073 sections_being_created = (bfd_boolean *) bfd_malloc (amt); 2074 if (sections_being_created == NULL) 2075 return FALSE; 2076 memset (sections_being_created, FALSE, amt); 2077 sections_being_created_abfd = abfd; 2078 } 2079 if (sections_being_created [shindex]) 2080 { 2081 _bfd_error_handler 2082 (_("%pB: warning: loop in section dependencies detected"), abfd); 2083 return FALSE; 2084 } 2085 sections_being_created [shindex] = TRUE; 2086 } 2087 2088 hdr = elf_elfsections (abfd)[shindex]; 2089 ehdr = elf_elfheader (abfd); 2090 name = bfd_elf_string_from_elf_section (abfd, ehdr->e_shstrndx, 2091 hdr->sh_name); 2092 if (name == NULL) 2093 goto fail; 2094 2095 bed = get_elf_backend_data (abfd); 2096 switch (hdr->sh_type) 2097 { 2098 case SHT_NULL: 2099 /* Inactive section. Throw it away. */ 2100 goto success; 2101 2102 case SHT_PROGBITS: /* Normal section with contents. */ 2103 case SHT_NOBITS: /* .bss section. */ 2104 case SHT_HASH: /* .hash section. */ 2105 case SHT_NOTE: /* .note section. */ 2106 case SHT_INIT_ARRAY: /* .init_array section. */ 2107 case SHT_FINI_ARRAY: /* .fini_array section. */ 2108 case SHT_PREINIT_ARRAY: /* .preinit_array section. */ 2109 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ 2110 case SHT_GNU_HASH: /* .gnu.hash section. */ 2111 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2112 goto success; 2113 2114 case SHT_DYNAMIC: /* Dynamic linking information. */ 2115 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2116 goto fail; 2117 2118 if (hdr->sh_link > elf_numsections (abfd)) 2119 { 2120 /* PR 10478: Accept Solaris binaries with a sh_link 2121 field set to SHN_BEFORE or SHN_AFTER. */ 2122 switch (bfd_get_arch (abfd)) 2123 { 2124 case bfd_arch_i386: 2125 case bfd_arch_sparc: 2126 if (hdr->sh_link == (SHN_LORESERVE & 0xffff) /* SHN_BEFORE */ 2127 || hdr->sh_link == ((SHN_LORESERVE + 1) & 0xffff) /* SHN_AFTER */) 2128 break; 2129 /* Otherwise fall through. */ 2130 default: 2131 goto fail; 2132 } 2133 } 2134 else if (elf_elfsections (abfd)[hdr->sh_link] == NULL) 2135 goto fail; 2136 else if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) 2137 { 2138 Elf_Internal_Shdr *dynsymhdr; 2139 2140 /* The shared libraries distributed with hpux11 have a bogus 2141 sh_link field for the ".dynamic" section. Find the 2142 string table for the ".dynsym" section instead. */ 2143 if (elf_dynsymtab (abfd) != 0) 2144 { 2145 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; 2146 hdr->sh_link = dynsymhdr->sh_link; 2147 } 2148 else 2149 { 2150 unsigned int i, num_sec; 2151 2152 num_sec = elf_numsections (abfd); 2153 for (i = 1; i < num_sec; i++) 2154 { 2155 dynsymhdr = elf_elfsections (abfd)[i]; 2156 if (dynsymhdr->sh_type == SHT_DYNSYM) 2157 { 2158 hdr->sh_link = dynsymhdr->sh_link; 2159 break; 2160 } 2161 } 2162 } 2163 } 2164 goto success; 2165 2166 case SHT_SYMTAB: /* A symbol table. */ 2167 if (elf_onesymtab (abfd) == shindex) 2168 goto success; 2169 2170 if (hdr->sh_entsize != bed->s->sizeof_sym) 2171 goto fail; 2172 2173 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) 2174 { 2175 if (hdr->sh_size != 0) 2176 goto fail; 2177 /* Some assemblers erroneously set sh_info to one with a 2178 zero sh_size. ld sees this as a global symbol count 2179 of (unsigned) -1. Fix it here. */ 2180 hdr->sh_info = 0; 2181 goto success; 2182 } 2183 2184 /* PR 18854: A binary might contain more than one symbol table. 2185 Unusual, but possible. Warn, but continue. */ 2186 if (elf_onesymtab (abfd) != 0) 2187 { 2188 _bfd_error_handler 2189 /* xgettext:c-format */ 2190 (_("%pB: warning: multiple symbol tables detected" 2191 " - ignoring the table in section %u"), 2192 abfd, shindex); 2193 goto success; 2194 } 2195 elf_onesymtab (abfd) = shindex; 2196 elf_symtab_hdr (abfd) = *hdr; 2197 elf_elfsections (abfd)[shindex] = hdr = & elf_symtab_hdr (abfd); 2198 abfd->flags |= HAS_SYMS; 2199 2200 /* Sometimes a shared object will map in the symbol table. If 2201 SHF_ALLOC is set, and this is a shared object, then we also 2202 treat this section as a BFD section. We can not base the 2203 decision purely on SHF_ALLOC, because that flag is sometimes 2204 set in a relocatable object file, which would confuse the 2205 linker. */ 2206 if ((hdr->sh_flags & SHF_ALLOC) != 0 2207 && (abfd->flags & DYNAMIC) != 0 2208 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2209 shindex)) 2210 goto fail; 2211 2212 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we 2213 can't read symbols without that section loaded as well. It 2214 is most likely specified by the next section header. */ 2215 { 2216 elf_section_list * entry; 2217 unsigned int i, num_sec; 2218 2219 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) 2220 if (entry->hdr.sh_link == shindex) 2221 goto success; 2222 2223 num_sec = elf_numsections (abfd); 2224 for (i = shindex + 1; i < num_sec; i++) 2225 { 2226 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2227 2228 if (hdr2->sh_type == SHT_SYMTAB_SHNDX 2229 && hdr2->sh_link == shindex) 2230 break; 2231 } 2232 2233 if (i == num_sec) 2234 for (i = 1; i < shindex; i++) 2235 { 2236 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2237 2238 if (hdr2->sh_type == SHT_SYMTAB_SHNDX 2239 && hdr2->sh_link == shindex) 2240 break; 2241 } 2242 2243 if (i != shindex) 2244 ret = bfd_section_from_shdr (abfd, i); 2245 /* else FIXME: we have failed to find the symbol table - should we issue an error ? */ 2246 goto success; 2247 } 2248 2249 case SHT_DYNSYM: /* A dynamic symbol table. */ 2250 if (elf_dynsymtab (abfd) == shindex) 2251 goto success; 2252 2253 if (hdr->sh_entsize != bed->s->sizeof_sym) 2254 goto fail; 2255 2256 if (hdr->sh_info * hdr->sh_entsize > hdr->sh_size) 2257 { 2258 if (hdr->sh_size != 0) 2259 goto fail; 2260 2261 /* Some linkers erroneously set sh_info to one with a 2262 zero sh_size. ld sees this as a global symbol count 2263 of (unsigned) -1. Fix it here. */ 2264 hdr->sh_info = 0; 2265 goto success; 2266 } 2267 2268 /* PR 18854: A binary might contain more than one dynamic symbol table. 2269 Unusual, but possible. Warn, but continue. */ 2270 if (elf_dynsymtab (abfd) != 0) 2271 { 2272 _bfd_error_handler 2273 /* xgettext:c-format */ 2274 (_("%pB: warning: multiple dynamic symbol tables detected" 2275 " - ignoring the table in section %u"), 2276 abfd, shindex); 2277 goto success; 2278 } 2279 elf_dynsymtab (abfd) = shindex; 2280 elf_tdata (abfd)->dynsymtab_hdr = *hdr; 2281 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; 2282 abfd->flags |= HAS_SYMS; 2283 2284 /* Besides being a symbol table, we also treat this as a regular 2285 section, so that objcopy can handle it. */ 2286 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2287 goto success; 2288 2289 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections. */ 2290 { 2291 elf_section_list * entry; 2292 2293 for (entry = elf_symtab_shndx_list (abfd); entry != NULL; entry = entry->next) 2294 if (entry->ndx == shindex) 2295 goto success; 2296 2297 entry = bfd_alloc (abfd, sizeof (*entry)); 2298 if (entry == NULL) 2299 goto fail; 2300 entry->ndx = shindex; 2301 entry->hdr = * hdr; 2302 entry->next = elf_symtab_shndx_list (abfd); 2303 elf_symtab_shndx_list (abfd) = entry; 2304 elf_elfsections (abfd)[shindex] = & entry->hdr; 2305 goto success; 2306 } 2307 2308 case SHT_STRTAB: /* A string table. */ 2309 if (hdr->bfd_section != NULL) 2310 goto success; 2311 2312 if (ehdr->e_shstrndx == shindex) 2313 { 2314 elf_tdata (abfd)->shstrtab_hdr = *hdr; 2315 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; 2316 goto success; 2317 } 2318 2319 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) 2320 { 2321 symtab_strtab: 2322 elf_tdata (abfd)->strtab_hdr = *hdr; 2323 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; 2324 goto success; 2325 } 2326 2327 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) 2328 { 2329 dynsymtab_strtab: 2330 elf_tdata (abfd)->dynstrtab_hdr = *hdr; 2331 hdr = &elf_tdata (abfd)->dynstrtab_hdr; 2332 elf_elfsections (abfd)[shindex] = hdr; 2333 /* We also treat this as a regular section, so that objcopy 2334 can handle it. */ 2335 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2336 shindex); 2337 goto success; 2338 } 2339 2340 /* If the string table isn't one of the above, then treat it as a 2341 regular section. We need to scan all the headers to be sure, 2342 just in case this strtab section appeared before the above. */ 2343 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) 2344 { 2345 unsigned int i, num_sec; 2346 2347 num_sec = elf_numsections (abfd); 2348 for (i = 1; i < num_sec; i++) 2349 { 2350 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2351 if (hdr2->sh_link == shindex) 2352 { 2353 /* Prevent endless recursion on broken objects. */ 2354 if (i == shindex) 2355 goto fail; 2356 if (! bfd_section_from_shdr (abfd, i)) 2357 goto fail; 2358 if (elf_onesymtab (abfd) == i) 2359 goto symtab_strtab; 2360 if (elf_dynsymtab (abfd) == i) 2361 goto dynsymtab_strtab; 2362 } 2363 } 2364 } 2365 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2366 goto success; 2367 2368 case SHT_REL: 2369 case SHT_RELA: 2370 /* *These* do a lot of work -- but build no sections! */ 2371 { 2372 asection *target_sect; 2373 Elf_Internal_Shdr *hdr2, **p_hdr; 2374 unsigned int num_sec = elf_numsections (abfd); 2375 struct bfd_elf_section_data *esdt; 2376 2377 if (hdr->sh_entsize 2378 != (bfd_size_type) (hdr->sh_type == SHT_REL 2379 ? bed->s->sizeof_rel : bed->s->sizeof_rela)) 2380 goto fail; 2381 2382 /* Check for a bogus link to avoid crashing. */ 2383 if (hdr->sh_link >= num_sec) 2384 { 2385 _bfd_error_handler 2386 /* xgettext:c-format */ 2387 (_("%pB: invalid link %u for reloc section %s (index %u)"), 2388 abfd, hdr->sh_link, name, shindex); 2389 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2390 shindex); 2391 goto success; 2392 } 2393 2394 /* For some incomprehensible reason Oracle distributes 2395 libraries for Solaris in which some of the objects have 2396 bogus sh_link fields. It would be nice if we could just 2397 reject them, but, unfortunately, some people need to use 2398 them. We scan through the section headers; if we find only 2399 one suitable symbol table, we clobber the sh_link to point 2400 to it. I hope this doesn't break anything. 2401 2402 Don't do it on executable nor shared library. */ 2403 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0 2404 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB 2405 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) 2406 { 2407 unsigned int scan; 2408 int found; 2409 2410 found = 0; 2411 for (scan = 1; scan < num_sec; scan++) 2412 { 2413 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB 2414 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) 2415 { 2416 if (found != 0) 2417 { 2418 found = 0; 2419 break; 2420 } 2421 found = scan; 2422 } 2423 } 2424 if (found != 0) 2425 hdr->sh_link = found; 2426 } 2427 2428 /* Get the symbol table. */ 2429 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB 2430 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) 2431 && ! bfd_section_from_shdr (abfd, hdr->sh_link)) 2432 goto fail; 2433 2434 /* If this is an alloc section in an executable or shared 2435 library, or the reloc section does not use the main symbol 2436 table we don't treat it as a reloc section. BFD can't 2437 adequately represent such a section, so at least for now, 2438 we don't try. We just present it as a normal section. We 2439 also can't use it as a reloc section if it points to the 2440 null section, an invalid section, another reloc section, or 2441 its sh_link points to the null section. */ 2442 if (((abfd->flags & (DYNAMIC | EXEC_P)) != 0 2443 && (hdr->sh_flags & SHF_ALLOC) != 0) 2444 || hdr->sh_link == SHN_UNDEF 2445 || hdr->sh_link != elf_onesymtab (abfd) 2446 || hdr->sh_info == SHN_UNDEF 2447 || hdr->sh_info >= num_sec 2448 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL 2449 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) 2450 { 2451 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2452 shindex); 2453 goto success; 2454 } 2455 2456 if (! bfd_section_from_shdr (abfd, hdr->sh_info)) 2457 goto fail; 2458 2459 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); 2460 if (target_sect == NULL) 2461 goto fail; 2462 2463 esdt = elf_section_data (target_sect); 2464 if (hdr->sh_type == SHT_RELA) 2465 p_hdr = &esdt->rela.hdr; 2466 else 2467 p_hdr = &esdt->rel.hdr; 2468 2469 /* PR 17512: file: 0b4f81b7. 2470 Also see PR 24456, for a file which deliberately has two reloc 2471 sections. */ 2472 if (*p_hdr != NULL) 2473 { 2474 if (!bed->init_secondary_reloc_section (abfd, hdr, name, shindex)) 2475 { 2476 _bfd_error_handler 2477 /* xgettext:c-format */ 2478 (_("%pB: warning: secondary relocation section '%s' " 2479 "for section %pA found - ignoring"), 2480 abfd, name, target_sect); 2481 } 2482 goto success; 2483 } 2484 2485 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); 2486 if (hdr2 == NULL) 2487 goto fail; 2488 *hdr2 = *hdr; 2489 *p_hdr = hdr2; 2490 elf_elfsections (abfd)[shindex] = hdr2; 2491 target_sect->reloc_count += (NUM_SHDR_ENTRIES (hdr) 2492 * bed->s->int_rels_per_ext_rel); 2493 target_sect->flags |= SEC_RELOC; 2494 target_sect->relocation = NULL; 2495 target_sect->rel_filepos = hdr->sh_offset; 2496 /* In the section to which the relocations apply, mark whether 2497 its relocations are of the REL or RELA variety. */ 2498 if (hdr->sh_size != 0) 2499 { 2500 if (hdr->sh_type == SHT_RELA) 2501 target_sect->use_rela_p = 1; 2502 } 2503 abfd->flags |= HAS_RELOC; 2504 goto success; 2505 } 2506 2507 case SHT_GNU_verdef: 2508 elf_dynverdef (abfd) = shindex; 2509 elf_tdata (abfd)->dynverdef_hdr = *hdr; 2510 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2511 goto success; 2512 2513 case SHT_GNU_versym: 2514 if (hdr->sh_entsize != sizeof (Elf_External_Versym)) 2515 goto fail; 2516 2517 elf_dynversym (abfd) = shindex; 2518 elf_tdata (abfd)->dynversym_hdr = *hdr; 2519 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2520 goto success; 2521 2522 case SHT_GNU_verneed: 2523 elf_dynverref (abfd) = shindex; 2524 elf_tdata (abfd)->dynverref_hdr = *hdr; 2525 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2526 goto success; 2527 2528 case SHT_SHLIB: 2529 goto success; 2530 2531 case SHT_GROUP: 2532 if (! IS_VALID_GROUP_SECTION_HEADER (hdr, GRP_ENTRY_SIZE)) 2533 goto fail; 2534 2535 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2536 goto fail; 2537 2538 goto success; 2539 2540 default: 2541 /* Possibly an attributes section. */ 2542 if (hdr->sh_type == SHT_GNU_ATTRIBUTES 2543 || hdr->sh_type == bed->obj_attrs_section_type) 2544 { 2545 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2546 goto fail; 2547 _bfd_elf_parse_attributes (abfd, hdr); 2548 goto success; 2549 } 2550 2551 /* Check for any processor-specific section types. */ 2552 if (bed->elf_backend_section_from_shdr (abfd, hdr, name, shindex)) 2553 goto success; 2554 2555 if (hdr->sh_type >= SHT_LOUSER && hdr->sh_type <= SHT_HIUSER) 2556 { 2557 if ((hdr->sh_flags & SHF_ALLOC) != 0) 2558 /* FIXME: How to properly handle allocated section reserved 2559 for applications? */ 2560 _bfd_error_handler 2561 /* xgettext:c-format */ 2562 (_("%pB: unknown type [%#x] section `%s'"), 2563 abfd, hdr->sh_type, name); 2564 else 2565 { 2566 /* Allow sections reserved for applications. */ 2567 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2568 shindex); 2569 goto success; 2570 } 2571 } 2572 else if (hdr->sh_type >= SHT_LOPROC 2573 && hdr->sh_type <= SHT_HIPROC) 2574 /* FIXME: We should handle this section. */ 2575 _bfd_error_handler 2576 /* xgettext:c-format */ 2577 (_("%pB: unknown type [%#x] section `%s'"), 2578 abfd, hdr->sh_type, name); 2579 else if (hdr->sh_type >= SHT_LOOS && hdr->sh_type <= SHT_HIOS) 2580 { 2581 /* Unrecognised OS-specific sections. */ 2582 if ((hdr->sh_flags & SHF_OS_NONCONFORMING) != 0) 2583 /* SHF_OS_NONCONFORMING indicates that special knowledge is 2584 required to correctly process the section and the file should 2585 be rejected with an error message. */ 2586 _bfd_error_handler 2587 /* xgettext:c-format */ 2588 (_("%pB: unknown type [%#x] section `%s'"), 2589 abfd, hdr->sh_type, name); 2590 else 2591 { 2592 /* Otherwise it should be processed. */ 2593 ret = _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2594 goto success; 2595 } 2596 } 2597 else 2598 /* FIXME: We should handle this section. */ 2599 _bfd_error_handler 2600 /* xgettext:c-format */ 2601 (_("%pB: unknown type [%#x] section `%s'"), 2602 abfd, hdr->sh_type, name); 2603 2604 goto fail; 2605 } 2606 2607 fail: 2608 ret = FALSE; 2609 success: 2610 if (sections_being_created && sections_being_created_abfd == abfd) 2611 sections_being_created [shindex] = FALSE; 2612 if (-- nesting == 0) 2613 { 2614 free (sections_being_created); 2615 sections_being_created = NULL; 2616 sections_being_created_abfd = NULL; 2617 } 2618 return ret; 2619} 2620 2621/* Return the local symbol specified by ABFD, R_SYMNDX. */ 2622 2623Elf_Internal_Sym * 2624bfd_sym_from_r_symndx (struct sym_cache *cache, 2625 bfd *abfd, 2626 unsigned long r_symndx) 2627{ 2628 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; 2629 2630 if (cache->abfd != abfd || cache->indx[ent] != r_symndx) 2631 { 2632 Elf_Internal_Shdr *symtab_hdr; 2633 unsigned char esym[sizeof (Elf64_External_Sym)]; 2634 Elf_External_Sym_Shndx eshndx; 2635 2636 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2637 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, 2638 &cache->sym[ent], esym, &eshndx) == NULL) 2639 return NULL; 2640 2641 if (cache->abfd != abfd) 2642 { 2643 memset (cache->indx, -1, sizeof (cache->indx)); 2644 cache->abfd = abfd; 2645 } 2646 cache->indx[ent] = r_symndx; 2647 } 2648 2649 return &cache->sym[ent]; 2650} 2651 2652/* Given an ELF section number, retrieve the corresponding BFD 2653 section. */ 2654 2655asection * 2656bfd_section_from_elf_index (bfd *abfd, unsigned int sec_index) 2657{ 2658 if (sec_index >= elf_numsections (abfd)) 2659 return NULL; 2660 return elf_elfsections (abfd)[sec_index]->bfd_section; 2661} 2662 2663static const struct bfd_elf_special_section special_sections_b[] = 2664{ 2665 { STRING_COMMA_LEN (".bss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 2666 { NULL, 0, 0, 0, 0 } 2667}; 2668 2669static const struct bfd_elf_special_section special_sections_c[] = 2670{ 2671 { STRING_COMMA_LEN (".comment"), 0, SHT_PROGBITS, 0 }, 2672 { STRING_COMMA_LEN (".ctf"), 0, SHT_PROGBITS, 0 }, 2673 { NULL, 0, 0, 0, 0 } 2674}; 2675 2676static const struct bfd_elf_special_section special_sections_d[] = 2677{ 2678 { STRING_COMMA_LEN (".data"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 2679 { STRING_COMMA_LEN (".data1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 2680 /* There are more DWARF sections than these, but they needn't be added here 2681 unless you have to cope with broken compilers that don't emit section 2682 attributes or you want to help the user writing assembler. */ 2683 { STRING_COMMA_LEN (".debug"), 0, SHT_PROGBITS, 0 }, 2684 { STRING_COMMA_LEN (".debug_line"), 0, SHT_PROGBITS, 0 }, 2685 { STRING_COMMA_LEN (".debug_info"), 0, SHT_PROGBITS, 0 }, 2686 { STRING_COMMA_LEN (".debug_abbrev"), 0, SHT_PROGBITS, 0 }, 2687 { STRING_COMMA_LEN (".debug_aranges"), 0, SHT_PROGBITS, 0 }, 2688 { STRING_COMMA_LEN (".dynamic"), 0, SHT_DYNAMIC, SHF_ALLOC }, 2689 { STRING_COMMA_LEN (".dynstr"), 0, SHT_STRTAB, SHF_ALLOC }, 2690 { STRING_COMMA_LEN (".dynsym"), 0, SHT_DYNSYM, SHF_ALLOC }, 2691 { NULL, 0, 0, 0, 0 } 2692}; 2693 2694static const struct bfd_elf_special_section special_sections_f[] = 2695{ 2696 { STRING_COMMA_LEN (".fini"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 2697 { STRING_COMMA_LEN (".fini_array"), -2, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, 2698 { NULL, 0 , 0, 0, 0 } 2699}; 2700 2701static const struct bfd_elf_special_section special_sections_g[] = 2702{ 2703 { STRING_COMMA_LEN (".gnu.linkonce.b"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 2704 { STRING_COMMA_LEN (".gnu.lto_"), -1, SHT_PROGBITS, SHF_EXCLUDE }, 2705 { STRING_COMMA_LEN (".got"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 2706 { STRING_COMMA_LEN (".gnu.version"), 0, SHT_GNU_versym, 0 }, 2707 { STRING_COMMA_LEN (".gnu.version_d"), 0, SHT_GNU_verdef, 0 }, 2708 { STRING_COMMA_LEN (".gnu.version_r"), 0, SHT_GNU_verneed, 0 }, 2709 { STRING_COMMA_LEN (".gnu.liblist"), 0, SHT_GNU_LIBLIST, SHF_ALLOC }, 2710 { STRING_COMMA_LEN (".gnu.conflict"), 0, SHT_RELA, SHF_ALLOC }, 2711 { STRING_COMMA_LEN (".gnu.hash"), 0, SHT_GNU_HASH, SHF_ALLOC }, 2712 { NULL, 0, 0, 0, 0 } 2713}; 2714 2715static const struct bfd_elf_special_section special_sections_h[] = 2716{ 2717 { STRING_COMMA_LEN (".hash"), 0, SHT_HASH, SHF_ALLOC }, 2718 { NULL, 0, 0, 0, 0 } 2719}; 2720 2721static const struct bfd_elf_special_section special_sections_i[] = 2722{ 2723 { STRING_COMMA_LEN (".init"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 2724 { STRING_COMMA_LEN (".init_array"), -2, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, 2725 { STRING_COMMA_LEN (".interp"), 0, SHT_PROGBITS, 0 }, 2726 { NULL, 0, 0, 0, 0 } 2727}; 2728 2729static const struct bfd_elf_special_section special_sections_l[] = 2730{ 2731 { STRING_COMMA_LEN (".line"), 0, SHT_PROGBITS, 0 }, 2732 { NULL, 0, 0, 0, 0 } 2733}; 2734 2735static const struct bfd_elf_special_section special_sections_n[] = 2736{ 2737 { STRING_COMMA_LEN (".note.GNU-stack"), 0, SHT_PROGBITS, 0 }, 2738 { STRING_COMMA_LEN (".note"), -1, SHT_NOTE, 0 }, 2739 { NULL, 0, 0, 0, 0 } 2740}; 2741 2742static const struct bfd_elf_special_section special_sections_p[] = 2743{ 2744 { STRING_COMMA_LEN (".preinit_array"), -2, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, 2745 { STRING_COMMA_LEN (".plt"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 2746 { NULL, 0, 0, 0, 0 } 2747}; 2748 2749static const struct bfd_elf_special_section special_sections_r[] = 2750{ 2751 { STRING_COMMA_LEN (".rodata"), -2, SHT_PROGBITS, SHF_ALLOC }, 2752 { STRING_COMMA_LEN (".rodata1"), 0, SHT_PROGBITS, SHF_ALLOC }, 2753 { STRING_COMMA_LEN (".rela"), -1, SHT_RELA, 0 }, 2754 { STRING_COMMA_LEN (".rel"), -1, SHT_REL, 0 }, 2755 { NULL, 0, 0, 0, 0 } 2756}; 2757 2758static const struct bfd_elf_special_section special_sections_s[] = 2759{ 2760 { STRING_COMMA_LEN (".shstrtab"), 0, SHT_STRTAB, 0 }, 2761 { STRING_COMMA_LEN (".strtab"), 0, SHT_STRTAB, 0 }, 2762 { STRING_COMMA_LEN (".symtab"), 0, SHT_SYMTAB, 0 }, 2763 /* See struct bfd_elf_special_section declaration for the semantics of 2764 this special case where .prefix_length != strlen (.prefix). */ 2765 { ".stabstr", 5, 3, SHT_STRTAB, 0 }, 2766 { NULL, 0, 0, 0, 0 } 2767}; 2768 2769static const struct bfd_elf_special_section special_sections_t[] = 2770{ 2771 { STRING_COMMA_LEN (".text"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 2772 { STRING_COMMA_LEN (".tbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, 2773 { STRING_COMMA_LEN (".tdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, 2774 { NULL, 0, 0, 0, 0 } 2775}; 2776 2777static const struct bfd_elf_special_section special_sections_z[] = 2778{ 2779 { STRING_COMMA_LEN (".zdebug_line"), 0, SHT_PROGBITS, 0 }, 2780 { STRING_COMMA_LEN (".zdebug_info"), 0, SHT_PROGBITS, 0 }, 2781 { STRING_COMMA_LEN (".zdebug_abbrev"), 0, SHT_PROGBITS, 0 }, 2782 { STRING_COMMA_LEN (".zdebug_aranges"), 0, SHT_PROGBITS, 0 }, 2783 { NULL, 0, 0, 0, 0 } 2784}; 2785 2786static const struct bfd_elf_special_section * const special_sections[] = 2787{ 2788 special_sections_b, /* 'b' */ 2789 special_sections_c, /* 'c' */ 2790 special_sections_d, /* 'd' */ 2791 NULL, /* 'e' */ 2792 special_sections_f, /* 'f' */ 2793 special_sections_g, /* 'g' */ 2794 special_sections_h, /* 'h' */ 2795 special_sections_i, /* 'i' */ 2796 NULL, /* 'j' */ 2797 NULL, /* 'k' */ 2798 special_sections_l, /* 'l' */ 2799 NULL, /* 'm' */ 2800 special_sections_n, /* 'n' */ 2801 NULL, /* 'o' */ 2802 special_sections_p, /* 'p' */ 2803 NULL, /* 'q' */ 2804 special_sections_r, /* 'r' */ 2805 special_sections_s, /* 's' */ 2806 special_sections_t, /* 't' */ 2807 NULL, /* 'u' */ 2808 NULL, /* 'v' */ 2809 NULL, /* 'w' */ 2810 NULL, /* 'x' */ 2811 NULL, /* 'y' */ 2812 special_sections_z /* 'z' */ 2813}; 2814 2815const struct bfd_elf_special_section * 2816_bfd_elf_get_special_section (const char *name, 2817 const struct bfd_elf_special_section *spec, 2818 unsigned int rela) 2819{ 2820 int i; 2821 int len; 2822 2823 len = strlen (name); 2824 2825 for (i = 0; spec[i].prefix != NULL; i++) 2826 { 2827 int suffix_len; 2828 int prefix_len = spec[i].prefix_length; 2829 2830 if (len < prefix_len) 2831 continue; 2832 if (memcmp (name, spec[i].prefix, prefix_len) != 0) 2833 continue; 2834 2835 suffix_len = spec[i].suffix_length; 2836 if (suffix_len <= 0) 2837 { 2838 if (name[prefix_len] != 0) 2839 { 2840 if (suffix_len == 0) 2841 continue; 2842 if (name[prefix_len] != '.' 2843 && (suffix_len == -2 2844 || (rela && spec[i].type == SHT_REL))) 2845 continue; 2846 } 2847 } 2848 else 2849 { 2850 if (len < prefix_len + suffix_len) 2851 continue; 2852 if (memcmp (name + len - suffix_len, 2853 spec[i].prefix + prefix_len, 2854 suffix_len) != 0) 2855 continue; 2856 } 2857 return &spec[i]; 2858 } 2859 2860 return NULL; 2861} 2862 2863const struct bfd_elf_special_section * 2864_bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) 2865{ 2866 int i; 2867 const struct bfd_elf_special_section *spec; 2868 const struct elf_backend_data *bed; 2869 2870 /* See if this is one of the special sections. */ 2871 if (sec->name == NULL) 2872 return NULL; 2873 2874 bed = get_elf_backend_data (abfd); 2875 spec = bed->special_sections; 2876 if (spec) 2877 { 2878 spec = _bfd_elf_get_special_section (sec->name, 2879 bed->special_sections, 2880 sec->use_rela_p); 2881 if (spec != NULL) 2882 return spec; 2883 } 2884 2885 if (sec->name[0] != '.') 2886 return NULL; 2887 2888 i = sec->name[1] - 'b'; 2889 if (i < 0 || i > 'z' - 'b') 2890 return NULL; 2891 2892 spec = special_sections[i]; 2893 2894 if (spec == NULL) 2895 return NULL; 2896 2897 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); 2898} 2899 2900bfd_boolean 2901_bfd_elf_new_section_hook (bfd *abfd, asection *sec) 2902{ 2903 struct bfd_elf_section_data *sdata; 2904 const struct elf_backend_data *bed; 2905 const struct bfd_elf_special_section *ssect; 2906 2907 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; 2908 if (sdata == NULL) 2909 { 2910 sdata = (struct bfd_elf_section_data *) bfd_zalloc (abfd, 2911 sizeof (*sdata)); 2912 if (sdata == NULL) 2913 return FALSE; 2914 sec->used_by_bfd = sdata; 2915 } 2916 2917 /* Indicate whether or not this section should use RELA relocations. */ 2918 bed = get_elf_backend_data (abfd); 2919 sec->use_rela_p = bed->default_use_rela_p; 2920 2921 /* Set up ELF section type and flags for newly created sections, if 2922 there is an ABI mandated section. */ 2923 ssect = (*bed->get_sec_type_attr) (abfd, sec); 2924 if (ssect != NULL) 2925 { 2926 elf_section_type (sec) = ssect->type; 2927 elf_section_flags (sec) = ssect->attr; 2928 } 2929 2930 return _bfd_generic_new_section_hook (abfd, sec); 2931} 2932 2933/* Create a new bfd section from an ELF program header. 2934 2935 Since program segments have no names, we generate a synthetic name 2936 of the form segment<NUM>, where NUM is generally the index in the 2937 program header table. For segments that are split (see below) we 2938 generate the names segment<NUM>a and segment<NUM>b. 2939 2940 Note that some program segments may have a file size that is different than 2941 (less than) the memory size. All this means is that at execution the 2942 system must allocate the amount of memory specified by the memory size, 2943 but only initialize it with the first "file size" bytes read from the 2944 file. This would occur for example, with program segments consisting 2945 of combined data+bss. 2946 2947 To handle the above situation, this routine generates TWO bfd sections 2948 for the single program segment. The first has the length specified by 2949 the file size of the segment, and the second has the length specified 2950 by the difference between the two sizes. In effect, the segment is split 2951 into its initialized and uninitialized parts. 2952 2953 */ 2954 2955bfd_boolean 2956_bfd_elf_make_section_from_phdr (bfd *abfd, 2957 Elf_Internal_Phdr *hdr, 2958 int hdr_index, 2959 const char *type_name) 2960{ 2961 asection *newsect; 2962 char *name; 2963 char namebuf[64]; 2964 size_t len; 2965 int split; 2966 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 2967 2968 split = ((hdr->p_memsz > 0) 2969 && (hdr->p_filesz > 0) 2970 && (hdr->p_memsz > hdr->p_filesz)); 2971 2972 if (hdr->p_filesz > 0) 2973 { 2974 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "a" : ""); 2975 len = strlen (namebuf) + 1; 2976 name = (char *) bfd_alloc (abfd, len); 2977 if (!name) 2978 return FALSE; 2979 memcpy (name, namebuf, len); 2980 newsect = bfd_make_section (abfd, name); 2981 if (newsect == NULL) 2982 return FALSE; 2983 newsect->vma = hdr->p_vaddr / opb; 2984 newsect->lma = hdr->p_paddr / opb; 2985 newsect->size = hdr->p_filesz; 2986 newsect->filepos = hdr->p_offset; 2987 newsect->flags |= SEC_HAS_CONTENTS; 2988 newsect->alignment_power = bfd_log2 (hdr->p_align); 2989 if (hdr->p_type == PT_LOAD) 2990 { 2991 newsect->flags |= SEC_ALLOC; 2992 newsect->flags |= SEC_LOAD; 2993 if (hdr->p_flags & PF_X) 2994 { 2995 /* FIXME: all we known is that it has execute PERMISSION, 2996 may be data. */ 2997 newsect->flags |= SEC_CODE; 2998 } 2999 } 3000 if (!(hdr->p_flags & PF_W)) 3001 { 3002 newsect->flags |= SEC_READONLY; 3003 } 3004 } 3005 3006 if (hdr->p_memsz > hdr->p_filesz) 3007 { 3008 bfd_vma align; 3009 3010 sprintf (namebuf, "%s%d%s", type_name, hdr_index, split ? "b" : ""); 3011 len = strlen (namebuf) + 1; 3012 name = (char *) bfd_alloc (abfd, len); 3013 if (!name) 3014 return FALSE; 3015 memcpy (name, namebuf, len); 3016 newsect = bfd_make_section (abfd, name); 3017 if (newsect == NULL) 3018 return FALSE; 3019 newsect->vma = (hdr->p_vaddr + hdr->p_filesz) / opb; 3020 newsect->lma = (hdr->p_paddr + hdr->p_filesz) / opb; 3021 newsect->size = hdr->p_memsz - hdr->p_filesz; 3022 newsect->filepos = hdr->p_offset + hdr->p_filesz; 3023 align = newsect->vma & -newsect->vma; 3024 if (align == 0 || align > hdr->p_align) 3025 align = hdr->p_align; 3026 newsect->alignment_power = bfd_log2 (align); 3027 if (hdr->p_type == PT_LOAD) 3028 { 3029 newsect->flags |= SEC_ALLOC; 3030 if (hdr->p_flags & PF_X) 3031 newsect->flags |= SEC_CODE; 3032 } 3033 if (!(hdr->p_flags & PF_W)) 3034 newsect->flags |= SEC_READONLY; 3035 } 3036 3037 return TRUE; 3038} 3039 3040static bfd_boolean 3041_bfd_elf_core_find_build_id (bfd *templ, bfd_vma offset) 3042{ 3043 /* The return value is ignored. Build-ids are considered optional. */ 3044 if (templ->xvec->flavour == bfd_target_elf_flavour) 3045 return (*get_elf_backend_data (templ)->elf_backend_core_find_build_id) 3046 (templ, offset); 3047 return FALSE; 3048} 3049 3050bfd_boolean 3051bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int hdr_index) 3052{ 3053 const struct elf_backend_data *bed; 3054 3055 switch (hdr->p_type) 3056 { 3057 case PT_NULL: 3058 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "null"); 3059 3060 case PT_LOAD: 3061 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "load")) 3062 return FALSE; 3063 if (bfd_get_format (abfd) == bfd_core && abfd->build_id == NULL) 3064 _bfd_elf_core_find_build_id (abfd, hdr->p_offset); 3065 return TRUE; 3066 3067 case PT_DYNAMIC: 3068 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "dynamic"); 3069 3070 case PT_INTERP: 3071 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "interp"); 3072 3073 case PT_NOTE: 3074 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "note")) 3075 return FALSE; 3076 if (! elf_read_notes (abfd, hdr->p_offset, hdr->p_filesz, 3077 hdr->p_align)) 3078 return FALSE; 3079 return TRUE; 3080 3081 case PT_SHLIB: 3082 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "shlib"); 3083 3084 case PT_PHDR: 3085 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "phdr"); 3086 3087 case PT_GNU_EH_FRAME: 3088 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, 3089 "eh_frame_hdr"); 3090 3091 case PT_GNU_STACK: 3092 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "stack"); 3093 3094 case PT_GNU_RELRO: 3095 return _bfd_elf_make_section_from_phdr (abfd, hdr, hdr_index, "relro"); 3096 3097 default: 3098 /* Check for any processor-specific program segment types. */ 3099 bed = get_elf_backend_data (abfd); 3100 return bed->elf_backend_section_from_phdr (abfd, hdr, hdr_index, "proc"); 3101 } 3102} 3103 3104/* Return the REL_HDR for SEC, assuming there is only a single one, either 3105 REL or RELA. */ 3106 3107Elf_Internal_Shdr * 3108_bfd_elf_single_rel_hdr (asection *sec) 3109{ 3110 if (elf_section_data (sec)->rel.hdr) 3111 { 3112 BFD_ASSERT (elf_section_data (sec)->rela.hdr == NULL); 3113 return elf_section_data (sec)->rel.hdr; 3114 } 3115 else 3116 return elf_section_data (sec)->rela.hdr; 3117} 3118 3119static bfd_boolean 3120_bfd_elf_set_reloc_sh_name (bfd *abfd, 3121 Elf_Internal_Shdr *rel_hdr, 3122 const char *sec_name, 3123 bfd_boolean use_rela_p) 3124{ 3125 char *name = (char *) bfd_alloc (abfd, 3126 sizeof ".rela" + strlen (sec_name)); 3127 if (name == NULL) 3128 return FALSE; 3129 3130 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", sec_name); 3131 rel_hdr->sh_name = 3132 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, 3133 FALSE); 3134 if (rel_hdr->sh_name == (unsigned int) -1) 3135 return FALSE; 3136 3137 return TRUE; 3138} 3139 3140/* Allocate and initialize a section-header for a new reloc section, 3141 containing relocations against ASECT. It is stored in RELDATA. If 3142 USE_RELA_P is TRUE, we use RELA relocations; otherwise, we use REL 3143 relocations. */ 3144 3145static bfd_boolean 3146_bfd_elf_init_reloc_shdr (bfd *abfd, 3147 struct bfd_elf_section_reloc_data *reldata, 3148 const char *sec_name, 3149 bfd_boolean use_rela_p, 3150 bfd_boolean delay_st_name_p) 3151{ 3152 Elf_Internal_Shdr *rel_hdr; 3153 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3154 3155 BFD_ASSERT (reldata->hdr == NULL); 3156 rel_hdr = bfd_zalloc (abfd, sizeof (*rel_hdr)); 3157 reldata->hdr = rel_hdr; 3158 3159 if (delay_st_name_p) 3160 rel_hdr->sh_name = (unsigned int) -1; 3161 else if (!_bfd_elf_set_reloc_sh_name (abfd, rel_hdr, sec_name, 3162 use_rela_p)) 3163 return FALSE; 3164 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; 3165 rel_hdr->sh_entsize = (use_rela_p 3166 ? bed->s->sizeof_rela 3167 : bed->s->sizeof_rel); 3168 rel_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 3169 rel_hdr->sh_flags = 0; 3170 rel_hdr->sh_addr = 0; 3171 rel_hdr->sh_size = 0; 3172 rel_hdr->sh_offset = 0; 3173 3174 return TRUE; 3175} 3176 3177/* Return the default section type based on the passed in section flags. */ 3178 3179int 3180bfd_elf_get_default_section_type (flagword flags) 3181{ 3182 if ((flags & (SEC_ALLOC | SEC_IS_COMMON)) != 0 3183 && (flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) 3184 return SHT_NOBITS; 3185 return SHT_PROGBITS; 3186} 3187 3188struct fake_section_arg 3189{ 3190 struct bfd_link_info *link_info; 3191 bfd_boolean failed; 3192}; 3193 3194/* Set up an ELF internal section header for a section. */ 3195 3196static void 3197elf_fake_sections (bfd *abfd, asection *asect, void *fsarg) 3198{ 3199 struct fake_section_arg *arg = (struct fake_section_arg *)fsarg; 3200 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3201 struct bfd_elf_section_data *esd = elf_section_data (asect); 3202 Elf_Internal_Shdr *this_hdr; 3203 unsigned int sh_type; 3204 const char *name = asect->name; 3205 bfd_boolean delay_st_name_p = FALSE; 3206 bfd_vma mask; 3207 3208 if (arg->failed) 3209 { 3210 /* We already failed; just get out of the bfd_map_over_sections 3211 loop. */ 3212 return; 3213 } 3214 3215 this_hdr = &esd->this_hdr; 3216 3217 if (arg->link_info) 3218 { 3219 /* ld: compress DWARF debug sections with names: .debug_*. */ 3220 if ((arg->link_info->compress_debug & COMPRESS_DEBUG) 3221 && (asect->flags & SEC_DEBUGGING) 3222 && name[1] == 'd' 3223 && name[6] == '_') 3224 { 3225 /* Set SEC_ELF_COMPRESS to indicate this section should be 3226 compressed. */ 3227 asect->flags |= SEC_ELF_COMPRESS; 3228 /* If this section will be compressed, delay adding section 3229 name to section name section after it is compressed in 3230 _bfd_elf_assign_file_positions_for_non_load. */ 3231 delay_st_name_p = TRUE; 3232 } 3233 } 3234 else if ((asect->flags & SEC_ELF_RENAME)) 3235 { 3236 /* objcopy: rename output DWARF debug section. */ 3237 if ((abfd->flags & (BFD_DECOMPRESS | BFD_COMPRESS_GABI))) 3238 { 3239 /* When we decompress or compress with SHF_COMPRESSED, 3240 convert section name from .zdebug_* to .debug_* if 3241 needed. */ 3242 if (name[1] == 'z') 3243 { 3244 char *new_name = convert_zdebug_to_debug (abfd, name); 3245 if (new_name == NULL) 3246 { 3247 arg->failed = TRUE; 3248 return; 3249 } 3250 name = new_name; 3251 } 3252 } 3253 else if (asect->compress_status == COMPRESS_SECTION_DONE) 3254 { 3255 /* PR binutils/18087: Compression does not always make a 3256 section smaller. So only rename the section when 3257 compression has actually taken place. If input section 3258 name is .zdebug_*, we should never compress it again. */ 3259 char *new_name = convert_debug_to_zdebug (abfd, name); 3260 if (new_name == NULL) 3261 { 3262 arg->failed = TRUE; 3263 return; 3264 } 3265 BFD_ASSERT (name[1] != 'z'); 3266 name = new_name; 3267 } 3268 } 3269 3270 if (delay_st_name_p) 3271 this_hdr->sh_name = (unsigned int) -1; 3272 else 3273 { 3274 this_hdr->sh_name 3275 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 3276 name, FALSE); 3277 if (this_hdr->sh_name == (unsigned int) -1) 3278 { 3279 arg->failed = TRUE; 3280 return; 3281 } 3282 } 3283 3284 /* Don't clear sh_flags. Assembler may set additional bits. */ 3285 3286 if ((asect->flags & SEC_ALLOC) != 0 3287 || asect->user_set_vma) 3288 this_hdr->sh_addr = asect->vma * bfd_octets_per_byte (abfd, asect); 3289 else 3290 this_hdr->sh_addr = 0; 3291 3292 this_hdr->sh_offset = 0; 3293 this_hdr->sh_size = asect->size; 3294 this_hdr->sh_link = 0; 3295 /* PR 17512: file: 0eb809fe, 8b0535ee. */ 3296 if (asect->alignment_power >= (sizeof (bfd_vma) * 8) - 1) 3297 { 3298 _bfd_error_handler 3299 /* xgettext:c-format */ 3300 (_("%pB: error: alignment power %d of section `%pA' is too big"), 3301 abfd, asect->alignment_power, asect); 3302 arg->failed = TRUE; 3303 return; 3304 } 3305 /* Set sh_addralign to the highest power of two given by alignment 3306 consistent with the section VMA. Linker scripts can force VMA. */ 3307 mask = ((bfd_vma) 1 << asect->alignment_power) | this_hdr->sh_addr; 3308 this_hdr->sh_addralign = mask & -mask; 3309 /* The sh_entsize and sh_info fields may have been set already by 3310 copy_private_section_data. */ 3311 3312 this_hdr->bfd_section = asect; 3313 this_hdr->contents = NULL; 3314 3315 /* If the section type is unspecified, we set it based on 3316 asect->flags. */ 3317 if ((asect->flags & SEC_GROUP) != 0) 3318 sh_type = SHT_GROUP; 3319 else 3320 sh_type = bfd_elf_get_default_section_type (asect->flags); 3321 3322 if (this_hdr->sh_type == SHT_NULL) 3323 this_hdr->sh_type = sh_type; 3324 else if (this_hdr->sh_type == SHT_NOBITS 3325 && sh_type == SHT_PROGBITS 3326 && (asect->flags & SEC_ALLOC) != 0) 3327 { 3328 /* Warn if we are changing a NOBITS section to PROGBITS, but 3329 allow the link to proceed. This can happen when users link 3330 non-bss input sections to bss output sections, or emit data 3331 to a bss output section via a linker script. */ 3332 _bfd_error_handler 3333 (_("warning: section `%pA' type changed to PROGBITS"), asect); 3334 this_hdr->sh_type = sh_type; 3335 } 3336 3337 switch (this_hdr->sh_type) 3338 { 3339 default: 3340 break; 3341 3342 case SHT_STRTAB: 3343 case SHT_NOTE: 3344 case SHT_NOBITS: 3345 case SHT_PROGBITS: 3346 break; 3347 3348 case SHT_INIT_ARRAY: 3349 case SHT_FINI_ARRAY: 3350 case SHT_PREINIT_ARRAY: 3351 this_hdr->sh_entsize = bed->s->arch_size / 8; 3352 break; 3353 3354 case SHT_HASH: 3355 this_hdr->sh_entsize = bed->s->sizeof_hash_entry; 3356 break; 3357 3358 case SHT_DYNSYM: 3359 this_hdr->sh_entsize = bed->s->sizeof_sym; 3360 break; 3361 3362 case SHT_DYNAMIC: 3363 this_hdr->sh_entsize = bed->s->sizeof_dyn; 3364 break; 3365 3366 case SHT_RELA: 3367 if (get_elf_backend_data (abfd)->may_use_rela_p) 3368 this_hdr->sh_entsize = bed->s->sizeof_rela; 3369 break; 3370 3371 case SHT_REL: 3372 if (get_elf_backend_data (abfd)->may_use_rel_p) 3373 this_hdr->sh_entsize = bed->s->sizeof_rel; 3374 break; 3375 3376 case SHT_GNU_versym: 3377 this_hdr->sh_entsize = sizeof (Elf_External_Versym); 3378 break; 3379 3380 case SHT_GNU_verdef: 3381 this_hdr->sh_entsize = 0; 3382 /* objcopy or strip will copy over sh_info, but may not set 3383 cverdefs. The linker will set cverdefs, but sh_info will be 3384 zero. */ 3385 if (this_hdr->sh_info == 0) 3386 this_hdr->sh_info = elf_tdata (abfd)->cverdefs; 3387 else 3388 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 3389 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); 3390 break; 3391 3392 case SHT_GNU_verneed: 3393 this_hdr->sh_entsize = 0; 3394 /* objcopy or strip will copy over sh_info, but may not set 3395 cverrefs. The linker will set cverrefs, but sh_info will be 3396 zero. */ 3397 if (this_hdr->sh_info == 0) 3398 this_hdr->sh_info = elf_tdata (abfd)->cverrefs; 3399 else 3400 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 3401 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); 3402 break; 3403 3404 case SHT_GROUP: 3405 this_hdr->sh_entsize = GRP_ENTRY_SIZE; 3406 break; 3407 3408 case SHT_GNU_HASH: 3409 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; 3410 break; 3411 } 3412 3413 if ((asect->flags & SEC_ALLOC) != 0) 3414 this_hdr->sh_flags |= SHF_ALLOC; 3415 if ((asect->flags & SEC_READONLY) == 0) 3416 this_hdr->sh_flags |= SHF_WRITE; 3417 if ((asect->flags & SEC_CODE) != 0) 3418 this_hdr->sh_flags |= SHF_EXECINSTR; 3419 if ((asect->flags & SEC_MERGE) != 0) 3420 { 3421 this_hdr->sh_flags |= SHF_MERGE; 3422 this_hdr->sh_entsize = asect->entsize; 3423 } 3424 if ((asect->flags & SEC_STRINGS) != 0) 3425 this_hdr->sh_flags |= SHF_STRINGS; 3426 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) 3427 this_hdr->sh_flags |= SHF_GROUP; 3428 if ((asect->flags & SEC_THREAD_LOCAL) != 0) 3429 { 3430 this_hdr->sh_flags |= SHF_TLS; 3431 if (asect->size == 0 3432 && (asect->flags & SEC_HAS_CONTENTS) == 0) 3433 { 3434 struct bfd_link_order *o = asect->map_tail.link_order; 3435 3436 this_hdr->sh_size = 0; 3437 if (o != NULL) 3438 { 3439 this_hdr->sh_size = o->offset + o->size; 3440 if (this_hdr->sh_size != 0) 3441 this_hdr->sh_type = SHT_NOBITS; 3442 } 3443 } 3444 } 3445 if ((asect->flags & (SEC_GROUP | SEC_EXCLUDE)) == SEC_EXCLUDE) 3446 this_hdr->sh_flags |= SHF_EXCLUDE; 3447 3448 /* If the section has relocs, set up a section header for the 3449 SHT_REL[A] section. If two relocation sections are required for 3450 this section, it is up to the processor-specific back-end to 3451 create the other. */ 3452 if ((asect->flags & SEC_RELOC) != 0) 3453 { 3454 /* When doing a relocatable link, create both REL and RELA sections if 3455 needed. */ 3456 if (arg->link_info 3457 /* Do the normal setup if we wouldn't create any sections here. */ 3458 && esd->rel.count + esd->rela.count > 0 3459 && (bfd_link_relocatable (arg->link_info) 3460 || arg->link_info->emitrelocations)) 3461 { 3462 if (esd->rel.count && esd->rel.hdr == NULL 3463 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rel, name, 3464 FALSE, delay_st_name_p)) 3465 { 3466 arg->failed = TRUE; 3467 return; 3468 } 3469 if (esd->rela.count && esd->rela.hdr == NULL 3470 && !_bfd_elf_init_reloc_shdr (abfd, &esd->rela, name, 3471 TRUE, delay_st_name_p)) 3472 { 3473 arg->failed = TRUE; 3474 return; 3475 } 3476 } 3477 else if (!_bfd_elf_init_reloc_shdr (abfd, 3478 (asect->use_rela_p 3479 ? &esd->rela : &esd->rel), 3480 name, 3481 asect->use_rela_p, 3482 delay_st_name_p)) 3483 { 3484 arg->failed = TRUE; 3485 return; 3486 } 3487 } 3488 3489 /* Check for processor-specific section types. */ 3490 sh_type = this_hdr->sh_type; 3491 if (bed->elf_backend_fake_sections 3492 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) 3493 { 3494 arg->failed = TRUE; 3495 return; 3496 } 3497 3498 if (sh_type == SHT_NOBITS && asect->size != 0) 3499 { 3500 /* Don't change the header type from NOBITS if we are being 3501 called for objcopy --only-keep-debug. */ 3502 this_hdr->sh_type = sh_type; 3503 } 3504} 3505 3506/* Fill in the contents of a SHT_GROUP section. Called from 3507 _bfd_elf_compute_section_file_positions for gas, objcopy, and 3508 when ELF targets use the generic linker, ld. Called for ld -r 3509 from bfd_elf_final_link. */ 3510 3511void 3512bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) 3513{ 3514 bfd_boolean *failedptr = (bfd_boolean *) failedptrarg; 3515 asection *elt, *first; 3516 unsigned char *loc; 3517 bfd_boolean gas; 3518 3519 /* Ignore linker created group section. See elfNN_ia64_object_p in 3520 elfxx-ia64.c. */ 3521 if ((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP 3522 || sec->size == 0 3523 || *failedptr) 3524 return; 3525 3526 if (elf_section_data (sec)->this_hdr.sh_info == 0) 3527 { 3528 unsigned long symindx = 0; 3529 3530 /* elf_group_id will have been set up by objcopy and the 3531 generic linker. */ 3532 if (elf_group_id (sec) != NULL) 3533 symindx = elf_group_id (sec)->udata.i; 3534 3535 if (symindx == 0) 3536 { 3537 /* If called from the assembler, swap_out_syms will have set up 3538 elf_section_syms. 3539 PR 25699: A corrupt input file could contain bogus group info. */ 3540 if (elf_section_syms (abfd) == NULL) 3541 { 3542 *failedptr = TRUE; 3543 return; 3544 } 3545 symindx = elf_section_syms (abfd)[sec->index]->udata.i; 3546 } 3547 elf_section_data (sec)->this_hdr.sh_info = symindx; 3548 } 3549 else if (elf_section_data (sec)->this_hdr.sh_info == (unsigned int) -2) 3550 { 3551 /* The ELF backend linker sets sh_info to -2 when the group 3552 signature symbol is global, and thus the index can't be 3553 set until all local symbols are output. */ 3554 asection *igroup; 3555 struct bfd_elf_section_data *sec_data; 3556 unsigned long symndx; 3557 unsigned long extsymoff; 3558 struct elf_link_hash_entry *h; 3559 3560 /* The point of this little dance to the first SHF_GROUP section 3561 then back to the SHT_GROUP section is that this gets us to 3562 the SHT_GROUP in the input object. */ 3563 igroup = elf_sec_group (elf_next_in_group (sec)); 3564 sec_data = elf_section_data (igroup); 3565 symndx = sec_data->this_hdr.sh_info; 3566 extsymoff = 0; 3567 if (!elf_bad_symtab (igroup->owner)) 3568 { 3569 Elf_Internal_Shdr *symtab_hdr; 3570 3571 symtab_hdr = &elf_tdata (igroup->owner)->symtab_hdr; 3572 extsymoff = symtab_hdr->sh_info; 3573 } 3574 h = elf_sym_hashes (igroup->owner)[symndx - extsymoff]; 3575 while (h->root.type == bfd_link_hash_indirect 3576 || h->root.type == bfd_link_hash_warning) 3577 h = (struct elf_link_hash_entry *) h->root.u.i.link; 3578 3579 elf_section_data (sec)->this_hdr.sh_info = h->indx; 3580 } 3581 3582 /* The contents won't be allocated for "ld -r" or objcopy. */ 3583 gas = TRUE; 3584 if (sec->contents == NULL) 3585 { 3586 gas = FALSE; 3587 sec->contents = (unsigned char *) bfd_alloc (abfd, sec->size); 3588 3589 /* Arrange for the section to be written out. */ 3590 elf_section_data (sec)->this_hdr.contents = sec->contents; 3591 if (sec->contents == NULL) 3592 { 3593 *failedptr = TRUE; 3594 return; 3595 } 3596 } 3597 3598 loc = sec->contents + sec->size; 3599 3600 /* Get the pointer to the first section in the group that gas 3601 squirreled away here. objcopy arranges for this to be set to the 3602 start of the input section group. */ 3603 first = elt = elf_next_in_group (sec); 3604 3605 /* First element is a flag word. Rest of section is elf section 3606 indices for all the sections of the group. Write them backwards 3607 just to keep the group in the same order as given in .section 3608 directives, not that it matters. */ 3609 while (elt != NULL) 3610 { 3611 asection *s; 3612 3613 s = elt; 3614 if (!gas) 3615 s = s->output_section; 3616 if (s != NULL 3617 && !bfd_is_abs_section (s)) 3618 { 3619 struct bfd_elf_section_data *elf_sec = elf_section_data (s); 3620 struct bfd_elf_section_data *input_elf_sec = elf_section_data (elt); 3621 3622 if (elf_sec->rel.hdr != NULL 3623 && (gas 3624 || (input_elf_sec->rel.hdr != NULL 3625 && input_elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0)) 3626 { 3627 elf_sec->rel.hdr->sh_flags |= SHF_GROUP; 3628 loc -= 4; 3629 H_PUT_32 (abfd, elf_sec->rel.idx, loc); 3630 } 3631 if (elf_sec->rela.hdr != NULL 3632 && (gas 3633 || (input_elf_sec->rela.hdr != NULL 3634 && input_elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0)) 3635 { 3636 elf_sec->rela.hdr->sh_flags |= SHF_GROUP; 3637 loc -= 4; 3638 H_PUT_32 (abfd, elf_sec->rela.idx, loc); 3639 } 3640 loc -= 4; 3641 H_PUT_32 (abfd, elf_sec->this_idx, loc); 3642 } 3643 elt = elf_next_in_group (elt); 3644 if (elt == first) 3645 break; 3646 } 3647 3648 loc -= 4; 3649 BFD_ASSERT (loc == sec->contents); 3650 3651 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); 3652} 3653 3654/* Given NAME, the name of a relocation section stripped of its 3655 .rel/.rela prefix, return the section in ABFD to which the 3656 relocations apply. */ 3657 3658asection * 3659_bfd_elf_plt_get_reloc_section (bfd *abfd, const char *name) 3660{ 3661 /* If a target needs .got.plt section, relocations in rela.plt/rel.plt 3662 section likely apply to .got.plt or .got section. */ 3663 if (get_elf_backend_data (abfd)->want_got_plt 3664 && strcmp (name, ".plt") == 0) 3665 { 3666 asection *sec; 3667 3668 name = ".got.plt"; 3669 sec = bfd_get_section_by_name (abfd, name); 3670 if (sec != NULL) 3671 return sec; 3672 name = ".got"; 3673 } 3674 3675 return bfd_get_section_by_name (abfd, name); 3676} 3677 3678/* Return the section to which RELOC_SEC applies. */ 3679 3680static asection * 3681elf_get_reloc_section (asection *reloc_sec) 3682{ 3683 const char *name; 3684 unsigned int type; 3685 bfd *abfd; 3686 const struct elf_backend_data *bed; 3687 3688 type = elf_section_data (reloc_sec)->this_hdr.sh_type; 3689 if (type != SHT_REL && type != SHT_RELA) 3690 return NULL; 3691 3692 /* We look up the section the relocs apply to by name. */ 3693 name = reloc_sec->name; 3694 if (strncmp (name, ".rel", 4) != 0) 3695 return NULL; 3696 name += 4; 3697 if (type == SHT_RELA && *name++ != 'a') 3698 return NULL; 3699 3700 abfd = reloc_sec->owner; 3701 bed = get_elf_backend_data (abfd); 3702 return bed->get_reloc_section (abfd, name); 3703} 3704 3705/* Assign all ELF section numbers. The dummy first section is handled here 3706 too. The link/info pointers for the standard section types are filled 3707 in here too, while we're at it. LINK_INFO will be 0 when arriving 3708 here for objcopy, and when using the generic ELF linker. */ 3709 3710static bfd_boolean 3711assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) 3712{ 3713 struct elf_obj_tdata *t = elf_tdata (abfd); 3714 asection *sec; 3715 unsigned int section_number; 3716 Elf_Internal_Shdr **i_shdrp; 3717 struct bfd_elf_section_data *d; 3718 bfd_boolean need_symtab; 3719 size_t amt; 3720 3721 section_number = 1; 3722 3723 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); 3724 3725 /* SHT_GROUP sections are in relocatable files only. */ 3726 if (link_info == NULL || !link_info->resolve_section_groups) 3727 { 3728 size_t reloc_count = 0; 3729 3730 /* Put SHT_GROUP sections first. */ 3731 for (sec = abfd->sections; sec != NULL; sec = sec->next) 3732 { 3733 d = elf_section_data (sec); 3734 3735 if (d->this_hdr.sh_type == SHT_GROUP) 3736 { 3737 if (sec->flags & SEC_LINKER_CREATED) 3738 { 3739 /* Remove the linker created SHT_GROUP sections. */ 3740 bfd_section_list_remove (abfd, sec); 3741 abfd->section_count--; 3742 } 3743 else 3744 d->this_idx = section_number++; 3745 } 3746 3747 /* Count relocations. */ 3748 reloc_count += sec->reloc_count; 3749 } 3750 3751 /* Clear HAS_RELOC if there are no relocations. */ 3752 if (reloc_count == 0) 3753 abfd->flags &= ~HAS_RELOC; 3754 } 3755 3756 for (sec = abfd->sections; sec; sec = sec->next) 3757 { 3758 d = elf_section_data (sec); 3759 3760 if (d->this_hdr.sh_type != SHT_GROUP) 3761 d->this_idx = section_number++; 3762 if (d->this_hdr.sh_name != (unsigned int) -1) 3763 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); 3764 if (d->rel.hdr) 3765 { 3766 d->rel.idx = section_number++; 3767 if (d->rel.hdr->sh_name != (unsigned int) -1) 3768 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel.hdr->sh_name); 3769 } 3770 else 3771 d->rel.idx = 0; 3772 3773 if (d->rela.hdr) 3774 { 3775 d->rela.idx = section_number++; 3776 if (d->rela.hdr->sh_name != (unsigned int) -1) 3777 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rela.hdr->sh_name); 3778 } 3779 else 3780 d->rela.idx = 0; 3781 } 3782 3783 need_symtab = (bfd_get_symcount (abfd) > 0 3784 || (link_info == NULL 3785 && ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) 3786 == HAS_RELOC))); 3787 if (need_symtab) 3788 { 3789 elf_onesymtab (abfd) = section_number++; 3790 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); 3791 if (section_number > ((SHN_LORESERVE - 2) & 0xFFFF)) 3792 { 3793 elf_section_list *entry; 3794 3795 BFD_ASSERT (elf_symtab_shndx_list (abfd) == NULL); 3796 3797 entry = bfd_zalloc (abfd, sizeof (*entry)); 3798 entry->ndx = section_number++; 3799 elf_symtab_shndx_list (abfd) = entry; 3800 entry->hdr.sh_name 3801 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 3802 ".symtab_shndx", FALSE); 3803 if (entry->hdr.sh_name == (unsigned int) -1) 3804 return FALSE; 3805 } 3806 elf_strtab_sec (abfd) = section_number++; 3807 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); 3808 } 3809 3810 elf_shstrtab_sec (abfd) = section_number++; 3811 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); 3812 elf_elfheader (abfd)->e_shstrndx = elf_shstrtab_sec (abfd); 3813 3814 if (section_number >= SHN_LORESERVE) 3815 { 3816 /* xgettext:c-format */ 3817 _bfd_error_handler (_("%pB: too many sections: %u"), 3818 abfd, section_number); 3819 return FALSE; 3820 } 3821 3822 elf_numsections (abfd) = section_number; 3823 elf_elfheader (abfd)->e_shnum = section_number; 3824 3825 /* Set up the list of section header pointers, in agreement with the 3826 indices. */ 3827 amt = section_number * sizeof (Elf_Internal_Shdr *); 3828 i_shdrp = (Elf_Internal_Shdr **) bfd_zalloc (abfd, amt); 3829 if (i_shdrp == NULL) 3830 return FALSE; 3831 3832 i_shdrp[0] = (Elf_Internal_Shdr *) bfd_zalloc (abfd, 3833 sizeof (Elf_Internal_Shdr)); 3834 if (i_shdrp[0] == NULL) 3835 { 3836 bfd_release (abfd, i_shdrp); 3837 return FALSE; 3838 } 3839 3840 elf_elfsections (abfd) = i_shdrp; 3841 3842 i_shdrp[elf_shstrtab_sec (abfd)] = &t->shstrtab_hdr; 3843 if (need_symtab) 3844 { 3845 i_shdrp[elf_onesymtab (abfd)] = &t->symtab_hdr; 3846 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) 3847 { 3848 elf_section_list * entry = elf_symtab_shndx_list (abfd); 3849 BFD_ASSERT (entry != NULL); 3850 i_shdrp[entry->ndx] = & entry->hdr; 3851 entry->hdr.sh_link = elf_onesymtab (abfd); 3852 } 3853 i_shdrp[elf_strtab_sec (abfd)] = &t->strtab_hdr; 3854 t->symtab_hdr.sh_link = elf_strtab_sec (abfd); 3855 } 3856 3857 for (sec = abfd->sections; sec; sec = sec->next) 3858 { 3859 asection *s; 3860 3861 d = elf_section_data (sec); 3862 3863 i_shdrp[d->this_idx] = &d->this_hdr; 3864 if (d->rel.idx != 0) 3865 i_shdrp[d->rel.idx] = d->rel.hdr; 3866 if (d->rela.idx != 0) 3867 i_shdrp[d->rela.idx] = d->rela.hdr; 3868 3869 /* Fill in the sh_link and sh_info fields while we're at it. */ 3870 3871 /* sh_link of a reloc section is the section index of the symbol 3872 table. sh_info is the section index of the section to which 3873 the relocation entries apply. */ 3874 if (d->rel.idx != 0) 3875 { 3876 d->rel.hdr->sh_link = elf_onesymtab (abfd); 3877 d->rel.hdr->sh_info = d->this_idx; 3878 d->rel.hdr->sh_flags |= SHF_INFO_LINK; 3879 } 3880 if (d->rela.idx != 0) 3881 { 3882 d->rela.hdr->sh_link = elf_onesymtab (abfd); 3883 d->rela.hdr->sh_info = d->this_idx; 3884 d->rela.hdr->sh_flags |= SHF_INFO_LINK; 3885 } 3886 3887 /* We need to set up sh_link for SHF_LINK_ORDER. */ 3888 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) 3889 { 3890 s = elf_linked_to_section (sec); 3891 if (s) 3892 { 3893 /* Check discarded linkonce section. */ 3894 if (discarded_section (s)) 3895 { 3896 asection *kept; 3897 _bfd_error_handler 3898 /* xgettext:c-format */ 3899 (_("%pB: sh_link of section `%pA' points to" 3900 " discarded section `%pA' of `%pB'"), 3901 abfd, d->this_hdr.bfd_section, s, s->owner); 3902 /* Point to the kept section if it has the same 3903 size as the discarded one. */ 3904 kept = _bfd_elf_check_kept_section (s, link_info); 3905 if (kept == NULL) 3906 { 3907 bfd_set_error (bfd_error_bad_value); 3908 return FALSE; 3909 } 3910 s = kept; 3911 } 3912 /* Handle objcopy. */ 3913 else if (s->output_section == NULL) 3914 { 3915 _bfd_error_handler 3916 /* xgettext:c-format */ 3917 (_("%pB: sh_link of section `%pA' points to" 3918 " removed section `%pA' of `%pB'"), 3919 abfd, d->this_hdr.bfd_section, s, s->owner); 3920 bfd_set_error (bfd_error_bad_value); 3921 return FALSE; 3922 } 3923 s = s->output_section; 3924 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 3925 } 3926 else 3927 { 3928 /* PR 290: 3929 The Intel C compiler generates SHT_IA_64_UNWIND with 3930 SHF_LINK_ORDER. But it doesn't set the sh_link or 3931 sh_info fields. Hence we could get the situation 3932 where s is NULL. */ 3933 const struct elf_backend_data *bed 3934 = get_elf_backend_data (abfd); 3935 bed->link_order_error_handler 3936 /* xgettext:c-format */ 3937 (_("%pB: warning: sh_link not set for section `%pA'"), 3938 abfd, sec); 3939 } 3940 } 3941 3942 switch (d->this_hdr.sh_type) 3943 { 3944 case SHT_REL: 3945 case SHT_RELA: 3946 /* A reloc section which we are treating as a normal BFD 3947 section. sh_link is the section index of the symbol 3948 table. sh_info is the section index of the section to 3949 which the relocation entries apply. We assume that an 3950 allocated reloc section uses the dynamic symbol table. 3951 FIXME: How can we be sure? */ 3952 s = bfd_get_section_by_name (abfd, ".dynsym"); 3953 if (s != NULL) 3954 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 3955 3956 s = elf_get_reloc_section (sec); 3957 if (s != NULL) 3958 { 3959 d->this_hdr.sh_info = elf_section_data (s)->this_idx; 3960 d->this_hdr.sh_flags |= SHF_INFO_LINK; 3961 } 3962 break; 3963 3964 case SHT_STRTAB: 3965 /* We assume that a section named .stab*str is a stabs 3966 string section. We look for a section with the same name 3967 but without the trailing ``str'', and set its sh_link 3968 field to point to this section. */ 3969 if (CONST_STRNEQ (sec->name, ".stab") 3970 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) 3971 { 3972 size_t len; 3973 char *alc; 3974 3975 len = strlen (sec->name); 3976 alc = (char *) bfd_malloc (len - 2); 3977 if (alc == NULL) 3978 return FALSE; 3979 memcpy (alc, sec->name, len - 3); 3980 alc[len - 3] = '\0'; 3981 s = bfd_get_section_by_name (abfd, alc); 3982 free (alc); 3983 if (s != NULL) 3984 { 3985 elf_section_data (s)->this_hdr.sh_link = d->this_idx; 3986 3987 /* This is a .stab section. */ 3988 elf_section_data (s)->this_hdr.sh_entsize = 12; 3989 } 3990 } 3991 break; 3992 3993 case SHT_DYNAMIC: 3994 case SHT_DYNSYM: 3995 case SHT_GNU_verneed: 3996 case SHT_GNU_verdef: 3997 /* sh_link is the section header index of the string table 3998 used for the dynamic entries, or the symbol table, or the 3999 version strings. */ 4000 s = bfd_get_section_by_name (abfd, ".dynstr"); 4001 if (s != NULL) 4002 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4003 break; 4004 4005 case SHT_GNU_LIBLIST: 4006 /* sh_link is the section header index of the prelink library 4007 list used for the dynamic entries, or the symbol table, or 4008 the version strings. */ 4009 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) 4010 ? ".dynstr" : ".gnu.libstr"); 4011 if (s != NULL) 4012 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4013 break; 4014 4015 case SHT_HASH: 4016 case SHT_GNU_HASH: 4017 case SHT_GNU_versym: 4018 /* sh_link is the section header index of the symbol table 4019 this hash table or version table is for. */ 4020 s = bfd_get_section_by_name (abfd, ".dynsym"); 4021 if (s != NULL) 4022 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 4023 break; 4024 4025 case SHT_GROUP: 4026 d->this_hdr.sh_link = elf_onesymtab (abfd); 4027 } 4028 } 4029 4030 /* Delay setting sh_name to _bfd_elf_write_object_contents so that 4031 _bfd_elf_assign_file_positions_for_non_load can convert DWARF 4032 debug section name from .debug_* to .zdebug_* if needed. */ 4033 4034 return TRUE; 4035} 4036 4037static bfd_boolean 4038sym_is_global (bfd *abfd, asymbol *sym) 4039{ 4040 /* If the backend has a special mapping, use it. */ 4041 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4042 if (bed->elf_backend_sym_is_global) 4043 return (*bed->elf_backend_sym_is_global) (abfd, sym); 4044 4045 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK | BSF_GNU_UNIQUE)) != 0 4046 || bfd_is_und_section (bfd_asymbol_section (sym)) 4047 || bfd_is_com_section (bfd_asymbol_section (sym))); 4048} 4049 4050/* Filter global symbols of ABFD to include in the import library. All 4051 SYMCOUNT symbols of ABFD can be examined from their pointers in 4052 SYMS. Pointers of symbols to keep should be stored contiguously at 4053 the beginning of that array. 4054 4055 Returns the number of symbols to keep. */ 4056 4057unsigned int 4058_bfd_elf_filter_global_symbols (bfd *abfd, struct bfd_link_info *info, 4059 asymbol **syms, long symcount) 4060{ 4061 long src_count, dst_count = 0; 4062 4063 for (src_count = 0; src_count < symcount; src_count++) 4064 { 4065 asymbol *sym = syms[src_count]; 4066 char *name = (char *) bfd_asymbol_name (sym); 4067 struct bfd_link_hash_entry *h; 4068 4069 if (!sym_is_global (abfd, sym)) 4070 continue; 4071 4072 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE); 4073 if (h == NULL) 4074 continue; 4075 if (h->type != bfd_link_hash_defined && h->type != bfd_link_hash_defweak) 4076 continue; 4077 if (h->linker_def || h->ldscript_def) 4078 continue; 4079 4080 syms[dst_count++] = sym; 4081 } 4082 4083 syms[dst_count] = NULL; 4084 4085 return dst_count; 4086} 4087 4088/* Don't output section symbols for sections that are not going to be 4089 output, that are duplicates or there is no BFD section. */ 4090 4091static bfd_boolean 4092ignore_section_sym (bfd *abfd, asymbol *sym) 4093{ 4094 elf_symbol_type *type_ptr; 4095 4096 if (sym == NULL) 4097 return FALSE; 4098 4099 if ((sym->flags & BSF_SECTION_SYM) == 0) 4100 return FALSE; 4101 4102 if (sym->section == NULL) 4103 return TRUE; 4104 4105 type_ptr = elf_symbol_from (abfd, sym); 4106 return ((type_ptr != NULL 4107 && type_ptr->internal_elf_sym.st_shndx != 0 4108 && bfd_is_abs_section (sym->section)) 4109 || !(sym->section->owner == abfd 4110 || (sym->section->output_section != NULL 4111 && sym->section->output_section->owner == abfd 4112 && sym->section->output_offset == 0) 4113 || bfd_is_abs_section (sym->section))); 4114} 4115 4116/* Map symbol from it's internal number to the external number, moving 4117 all local symbols to be at the head of the list. */ 4118 4119static bfd_boolean 4120elf_map_symbols (bfd *abfd, unsigned int *pnum_locals) 4121{ 4122 unsigned int symcount = bfd_get_symcount (abfd); 4123 asymbol **syms = bfd_get_outsymbols (abfd); 4124 asymbol **sect_syms; 4125 unsigned int num_locals = 0; 4126 unsigned int num_globals = 0; 4127 unsigned int num_locals2 = 0; 4128 unsigned int num_globals2 = 0; 4129 unsigned int max_index = 0; 4130 unsigned int idx; 4131 asection *asect; 4132 asymbol **new_syms; 4133 size_t amt; 4134 4135#ifdef DEBUG 4136 fprintf (stderr, "elf_map_symbols\n"); 4137 fflush (stderr); 4138#endif 4139 4140 for (asect = abfd->sections; asect; asect = asect->next) 4141 { 4142 if (max_index < asect->index) 4143 max_index = asect->index; 4144 } 4145 4146 max_index++; 4147 amt = max_index * sizeof (asymbol *); 4148 sect_syms = (asymbol **) bfd_zalloc (abfd, amt); 4149 if (sect_syms == NULL) 4150 return FALSE; 4151 elf_section_syms (abfd) = sect_syms; 4152 elf_num_section_syms (abfd) = max_index; 4153 4154 /* Init sect_syms entries for any section symbols we have already 4155 decided to output. */ 4156 for (idx = 0; idx < symcount; idx++) 4157 { 4158 asymbol *sym = syms[idx]; 4159 4160 if ((sym->flags & BSF_SECTION_SYM) != 0 4161 && sym->value == 0 4162 && !ignore_section_sym (abfd, sym) 4163 && !bfd_is_abs_section (sym->section)) 4164 { 4165 asection *sec = sym->section; 4166 4167 if (sec->owner != abfd) 4168 sec = sec->output_section; 4169 4170 sect_syms[sec->index] = syms[idx]; 4171 } 4172 } 4173 4174 /* Classify all of the symbols. */ 4175 for (idx = 0; idx < symcount; idx++) 4176 { 4177 if (sym_is_global (abfd, syms[idx])) 4178 num_globals++; 4179 else if (!ignore_section_sym (abfd, syms[idx])) 4180 num_locals++; 4181 } 4182 4183 /* We will be adding a section symbol for each normal BFD section. Most 4184 sections will already have a section symbol in outsymbols, but 4185 eg. SHT_GROUP sections will not, and we need the section symbol mapped 4186 at least in that case. */ 4187 for (asect = abfd->sections; asect; asect = asect->next) 4188 { 4189 if (sect_syms[asect->index] == NULL) 4190 { 4191 if (!sym_is_global (abfd, asect->symbol)) 4192 num_locals++; 4193 else 4194 num_globals++; 4195 } 4196 } 4197 4198 /* Now sort the symbols so the local symbols are first. */ 4199 amt = (num_locals + num_globals) * sizeof (asymbol *); 4200 new_syms = (asymbol **) bfd_alloc (abfd, amt); 4201 if (new_syms == NULL) 4202 return FALSE; 4203 4204 for (idx = 0; idx < symcount; idx++) 4205 { 4206 asymbol *sym = syms[idx]; 4207 unsigned int i; 4208 4209 if (sym_is_global (abfd, sym)) 4210 i = num_locals + num_globals2++; 4211 else if (!ignore_section_sym (abfd, sym)) 4212 i = num_locals2++; 4213 else 4214 continue; 4215 new_syms[i] = sym; 4216 sym->udata.i = i + 1; 4217 } 4218 for (asect = abfd->sections; asect; asect = asect->next) 4219 { 4220 if (sect_syms[asect->index] == NULL) 4221 { 4222 asymbol *sym = asect->symbol; 4223 unsigned int i; 4224 4225 sect_syms[asect->index] = sym; 4226 if (!sym_is_global (abfd, sym)) 4227 i = num_locals2++; 4228 else 4229 i = num_locals + num_globals2++; 4230 new_syms[i] = sym; 4231 sym->udata.i = i + 1; 4232 } 4233 } 4234 4235 bfd_set_symtab (abfd, new_syms, num_locals + num_globals); 4236 4237 *pnum_locals = num_locals; 4238 return TRUE; 4239} 4240 4241/* Align to the maximum file alignment that could be required for any 4242 ELF data structure. */ 4243 4244static inline file_ptr 4245align_file_position (file_ptr off, int align) 4246{ 4247 return (off + align - 1) & ~(align - 1); 4248} 4249 4250/* Assign a file position to a section, optionally aligning to the 4251 required section alignment. */ 4252 4253file_ptr 4254_bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, 4255 file_ptr offset, 4256 bfd_boolean align) 4257{ 4258 if (align && i_shdrp->sh_addralign > 1) 4259 offset = BFD_ALIGN (offset, i_shdrp->sh_addralign); 4260 i_shdrp->sh_offset = offset; 4261 if (i_shdrp->bfd_section != NULL) 4262 i_shdrp->bfd_section->filepos = offset; 4263 if (i_shdrp->sh_type != SHT_NOBITS) 4264 offset += i_shdrp->sh_size; 4265 return offset; 4266} 4267 4268/* Compute the file positions we are going to put the sections at, and 4269 otherwise prepare to begin writing out the ELF file. If LINK_INFO 4270 is not NULL, this is being called by the ELF backend linker. */ 4271 4272bfd_boolean 4273_bfd_elf_compute_section_file_positions (bfd *abfd, 4274 struct bfd_link_info *link_info) 4275{ 4276 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4277 struct fake_section_arg fsargs; 4278 bfd_boolean failed; 4279 struct elf_strtab_hash *strtab = NULL; 4280 Elf_Internal_Shdr *shstrtab_hdr; 4281 bfd_boolean need_symtab; 4282 4283 if (abfd->output_has_begun) 4284 return TRUE; 4285 4286 /* Do any elf backend specific processing first. */ 4287 if (bed->elf_backend_begin_write_processing) 4288 (*bed->elf_backend_begin_write_processing) (abfd, link_info); 4289 4290 if (!(*bed->elf_backend_init_file_header) (abfd, link_info)) 4291 return FALSE; 4292 4293 fsargs.failed = FALSE; 4294 fsargs.link_info = link_info; 4295 bfd_map_over_sections (abfd, elf_fake_sections, &fsargs); 4296 if (fsargs.failed) 4297 return FALSE; 4298 4299 if (!assign_section_numbers (abfd, link_info)) 4300 return FALSE; 4301 4302 /* The backend linker builds symbol table information itself. */ 4303 need_symtab = (link_info == NULL 4304 && (bfd_get_symcount (abfd) > 0 4305 || ((abfd->flags & (EXEC_P | DYNAMIC | HAS_RELOC)) 4306 == HAS_RELOC))); 4307 if (need_symtab) 4308 { 4309 /* Non-zero if doing a relocatable link. */ 4310 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); 4311 4312 if (! swap_out_syms (abfd, &strtab, relocatable_p)) 4313 return FALSE; 4314 } 4315 4316 failed = FALSE; 4317 if (link_info == NULL) 4318 { 4319 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); 4320 if (failed) 4321 return FALSE; 4322 } 4323 4324 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; 4325 /* sh_name was set in init_file_header. */ 4326 shstrtab_hdr->sh_type = SHT_STRTAB; 4327 shstrtab_hdr->sh_flags = bed->elf_strtab_flags; 4328 shstrtab_hdr->sh_addr = 0; 4329 /* sh_size is set in _bfd_elf_assign_file_positions_for_non_load. */ 4330 shstrtab_hdr->sh_entsize = 0; 4331 shstrtab_hdr->sh_link = 0; 4332 shstrtab_hdr->sh_info = 0; 4333 /* sh_offset is set in _bfd_elf_assign_file_positions_for_non_load. */ 4334 shstrtab_hdr->sh_addralign = 1; 4335 4336 if (!assign_file_positions_except_relocs (abfd, link_info)) 4337 return FALSE; 4338 4339 if (need_symtab) 4340 { 4341 file_ptr off; 4342 Elf_Internal_Shdr *hdr; 4343 4344 off = elf_next_file_pos (abfd); 4345 4346 hdr = & elf_symtab_hdr (abfd); 4347 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); 4348 4349 if (elf_symtab_shndx_list (abfd) != NULL) 4350 { 4351 hdr = & elf_symtab_shndx_list (abfd)->hdr; 4352 if (hdr->sh_size != 0) 4353 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); 4354 /* FIXME: What about other symtab_shndx sections in the list ? */ 4355 } 4356 4357 hdr = &elf_tdata (abfd)->strtab_hdr; 4358 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); 4359 4360 elf_next_file_pos (abfd) = off; 4361 4362 /* Now that we know where the .strtab section goes, write it 4363 out. */ 4364 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 4365 || ! _bfd_elf_strtab_emit (abfd, strtab)) 4366 return FALSE; 4367 _bfd_elf_strtab_free (strtab); 4368 } 4369 4370 abfd->output_has_begun = TRUE; 4371 4372 return TRUE; 4373} 4374 4375/* Make an initial estimate of the size of the program header. If we 4376 get the number wrong here, we'll redo section placement. */ 4377 4378static bfd_size_type 4379get_program_header_size (bfd *abfd, struct bfd_link_info *info) 4380{ 4381 size_t segs; 4382 asection *s; 4383 const struct elf_backend_data *bed; 4384 4385 /* Assume we will need exactly two PT_LOAD segments: one for text 4386 and one for data. */ 4387 segs = 2; 4388 4389 s = bfd_get_section_by_name (abfd, ".interp"); 4390 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) 4391 { 4392 /* If we have a loadable interpreter section, we need a 4393 PT_INTERP segment. In this case, assume we also need a 4394 PT_PHDR segment, although that may not be true for all 4395 targets. */ 4396 segs += 2; 4397 } 4398 4399 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) 4400 { 4401 /* We need a PT_DYNAMIC segment. */ 4402 ++segs; 4403 } 4404 4405 if (info != NULL && info->relro) 4406 { 4407 /* We need a PT_GNU_RELRO segment. */ 4408 ++segs; 4409 } 4410 4411 if (elf_eh_frame_hdr (abfd)) 4412 { 4413 /* We need a PT_GNU_EH_FRAME segment. */ 4414 ++segs; 4415 } 4416 4417 if (elf_stack_flags (abfd)) 4418 { 4419 /* We need a PT_GNU_STACK segment. */ 4420 ++segs; 4421 } 4422 4423 s = bfd_get_section_by_name (abfd, 4424 NOTE_GNU_PROPERTY_SECTION_NAME); 4425 if (s != NULL && s->size != 0) 4426 { 4427 /* We need a PT_GNU_PROPERTY segment. */ 4428 ++segs; 4429 } 4430 4431 for (s = abfd->sections; s != NULL; s = s->next) 4432 { 4433 if ((s->flags & SEC_LOAD) != 0 4434 && elf_section_type (s) == SHT_NOTE) 4435 { 4436 unsigned int alignment_power; 4437 /* We need a PT_NOTE segment. */ 4438 ++segs; 4439 /* Try to create just one PT_NOTE segment for all adjacent 4440 loadable SHT_NOTE sections. gABI requires that within a 4441 PT_NOTE segment (and also inside of each SHT_NOTE section) 4442 each note should have the same alignment. So we check 4443 whether the sections are correctly aligned. */ 4444 alignment_power = s->alignment_power; 4445 while (s->next != NULL 4446 && s->next->alignment_power == alignment_power 4447 && (s->next->flags & SEC_LOAD) != 0 4448 && elf_section_type (s->next) == SHT_NOTE) 4449 s = s->next; 4450 } 4451 } 4452 4453 for (s = abfd->sections; s != NULL; s = s->next) 4454 { 4455 if (s->flags & SEC_THREAD_LOCAL) 4456 { 4457 /* We need a PT_TLS segment. */ 4458 ++segs; 4459 break; 4460 } 4461 } 4462 4463 bed = get_elf_backend_data (abfd); 4464 4465 if ((abfd->flags & D_PAGED) != 0 4466 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) 4467 { 4468 /* Add a PT_GNU_MBIND segment for each mbind section. */ 4469 unsigned int page_align_power = bfd_log2 (bed->commonpagesize); 4470 for (s = abfd->sections; s != NULL; s = s->next) 4471 if (elf_section_flags (s) & SHF_GNU_MBIND) 4472 { 4473 if (elf_section_data (s)->this_hdr.sh_info > PT_GNU_MBIND_NUM) 4474 { 4475 _bfd_error_handler 4476 /* xgettext:c-format */ 4477 (_("%pB: GNU_MBIND section `%pA' has invalid " 4478 "sh_info field: %d"), 4479 abfd, s, elf_section_data (s)->this_hdr.sh_info); 4480 continue; 4481 } 4482 /* Align mbind section to page size. */ 4483 if (s->alignment_power < page_align_power) 4484 s->alignment_power = page_align_power; 4485 segs ++; 4486 } 4487 } 4488 4489 /* Let the backend count up any program headers it might need. */ 4490 if (bed->elf_backend_additional_program_headers) 4491 { 4492 int a; 4493 4494 a = (*bed->elf_backend_additional_program_headers) (abfd, info); 4495 if (a == -1) 4496 abort (); 4497 segs += a; 4498 } 4499 4500 return segs * bed->s->sizeof_phdr; 4501} 4502 4503/* Find the segment that contains the output_section of section. */ 4504 4505Elf_Internal_Phdr * 4506_bfd_elf_find_segment_containing_section (bfd * abfd, asection * section) 4507{ 4508 struct elf_segment_map *m; 4509 Elf_Internal_Phdr *p; 4510 4511 for (m = elf_seg_map (abfd), p = elf_tdata (abfd)->phdr; 4512 m != NULL; 4513 m = m->next, p++) 4514 { 4515 int i; 4516 4517 for (i = m->count - 1; i >= 0; i--) 4518 if (m->sections[i] == section) 4519 return p; 4520 } 4521 4522 return NULL; 4523} 4524 4525/* Create a mapping from a set of sections to a program segment. */ 4526 4527static struct elf_segment_map * 4528make_mapping (bfd *abfd, 4529 asection **sections, 4530 unsigned int from, 4531 unsigned int to, 4532 bfd_boolean phdr) 4533{ 4534 struct elf_segment_map *m; 4535 unsigned int i; 4536 asection **hdrpp; 4537 size_t amt; 4538 4539 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 4540 amt += (to - from) * sizeof (asection *); 4541 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 4542 if (m == NULL) 4543 return NULL; 4544 m->next = NULL; 4545 m->p_type = PT_LOAD; 4546 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) 4547 m->sections[i - from] = *hdrpp; 4548 m->count = to - from; 4549 4550 if (from == 0 && phdr) 4551 { 4552 /* Include the headers in the first PT_LOAD segment. */ 4553 m->includes_filehdr = 1; 4554 m->includes_phdrs = 1; 4555 } 4556 4557 return m; 4558} 4559 4560/* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL 4561 on failure. */ 4562 4563struct elf_segment_map * 4564_bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) 4565{ 4566 struct elf_segment_map *m; 4567 4568 m = (struct elf_segment_map *) bfd_zalloc (abfd, 4569 sizeof (struct elf_segment_map)); 4570 if (m == NULL) 4571 return NULL; 4572 m->next = NULL; 4573 m->p_type = PT_DYNAMIC; 4574 m->count = 1; 4575 m->sections[0] = dynsec; 4576 4577 return m; 4578} 4579 4580/* Possibly add or remove segments from the segment map. */ 4581 4582static bfd_boolean 4583elf_modify_segment_map (bfd *abfd, 4584 struct bfd_link_info *info, 4585 bfd_boolean remove_empty_load) 4586{ 4587 struct elf_segment_map **m; 4588 const struct elf_backend_data *bed; 4589 4590 /* The placement algorithm assumes that non allocated sections are 4591 not in PT_LOAD segments. We ensure this here by removing such 4592 sections from the segment map. We also remove excluded 4593 sections. Finally, any PT_LOAD segment without sections is 4594 removed. */ 4595 m = &elf_seg_map (abfd); 4596 while (*m) 4597 { 4598 unsigned int i, new_count; 4599 4600 for (new_count = 0, i = 0; i < (*m)->count; i++) 4601 { 4602 if (((*m)->sections[i]->flags & SEC_EXCLUDE) == 0 4603 && (((*m)->sections[i]->flags & SEC_ALLOC) != 0 4604 || (*m)->p_type != PT_LOAD)) 4605 { 4606 (*m)->sections[new_count] = (*m)->sections[i]; 4607 new_count++; 4608 } 4609 } 4610 (*m)->count = new_count; 4611 4612 if (remove_empty_load 4613 && (*m)->p_type == PT_LOAD 4614 && (*m)->count == 0 4615 && !(*m)->includes_phdrs) 4616 *m = (*m)->next; 4617 else 4618 m = &(*m)->next; 4619 } 4620 4621 bed = get_elf_backend_data (abfd); 4622 if (bed->elf_backend_modify_segment_map != NULL) 4623 { 4624 if (!(*bed->elf_backend_modify_segment_map) (abfd, info)) 4625 return FALSE; 4626 } 4627 4628 return TRUE; 4629} 4630 4631#define IS_TBSS(s) \ 4632 ((s->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) == SEC_THREAD_LOCAL) 4633 4634/* Set up a mapping from BFD sections to program segments. */ 4635 4636bfd_boolean 4637_bfd_elf_map_sections_to_segments (bfd *abfd, struct bfd_link_info *info) 4638{ 4639 unsigned int count; 4640 struct elf_segment_map *m; 4641 asection **sections = NULL; 4642 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4643 bfd_boolean no_user_phdrs; 4644 4645 no_user_phdrs = elf_seg_map (abfd) == NULL; 4646 4647 if (info != NULL) 4648 info->user_phdrs = !no_user_phdrs; 4649 4650 if (no_user_phdrs && bfd_count_sections (abfd) != 0) 4651 { 4652 asection *s; 4653 unsigned int i; 4654 struct elf_segment_map *mfirst; 4655 struct elf_segment_map **pm; 4656 asection *last_hdr; 4657 bfd_vma last_size; 4658 unsigned int hdr_index; 4659 bfd_vma maxpagesize; 4660 asection **hdrpp; 4661 bfd_boolean phdr_in_segment; 4662 bfd_boolean writable; 4663 bfd_boolean executable; 4664 unsigned int tls_count = 0; 4665 asection *first_tls = NULL; 4666 asection *first_mbind = NULL; 4667 asection *dynsec, *eh_frame_hdr; 4668 size_t amt; 4669 bfd_vma addr_mask, wrap_to = 0; /* Bytes. */ 4670 bfd_size_type phdr_size; /* Octets/bytes. */ 4671 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 4672 4673 /* Select the allocated sections, and sort them. */ 4674 4675 amt = bfd_count_sections (abfd) * sizeof (asection *); 4676 sections = (asection **) bfd_malloc (amt); 4677 if (sections == NULL) 4678 goto error_return; 4679 4680 /* Calculate top address, avoiding undefined behaviour of shift 4681 left operator when shift count is equal to size of type 4682 being shifted. */ 4683 addr_mask = ((bfd_vma) 1 << (bfd_arch_bits_per_address (abfd) - 1)) - 1; 4684 addr_mask = (addr_mask << 1) + 1; 4685 4686 i = 0; 4687 for (s = abfd->sections; s != NULL; s = s->next) 4688 { 4689 if ((s->flags & SEC_ALLOC) != 0) 4690 { 4691 /* target_index is unused until bfd_elf_final_link 4692 starts output of section symbols. Use it to make 4693 qsort stable. */ 4694 s->target_index = i; 4695 sections[i] = s; 4696 ++i; 4697 /* A wrapping section potentially clashes with header. */ 4698 if (((s->lma + s->size / opb) & addr_mask) < (s->lma & addr_mask)) 4699 wrap_to = (s->lma + s->size / opb) & addr_mask; 4700 } 4701 } 4702 BFD_ASSERT (i <= bfd_count_sections (abfd)); 4703 count = i; 4704 4705 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); 4706 4707 phdr_size = elf_program_header_size (abfd); 4708 if (phdr_size == (bfd_size_type) -1) 4709 phdr_size = get_program_header_size (abfd, info); 4710 phdr_size += bed->s->sizeof_ehdr; 4711 /* phdr_size is compared to LMA values which are in bytes. */ 4712 phdr_size /= opb; 4713 maxpagesize = bed->maxpagesize; 4714 if (maxpagesize == 0) 4715 maxpagesize = 1; 4716 phdr_in_segment = info != NULL && info->load_phdrs; 4717 if (count != 0 4718 && (((sections[0]->lma & addr_mask) & (maxpagesize - 1)) 4719 >= (phdr_size & (maxpagesize - 1)))) 4720 /* For compatibility with old scripts that may not be using 4721 SIZEOF_HEADERS, add headers when it looks like space has 4722 been left for them. */ 4723 phdr_in_segment = TRUE; 4724 4725 /* Build the mapping. */ 4726 mfirst = NULL; 4727 pm = &mfirst; 4728 4729 /* If we have a .interp section, then create a PT_PHDR segment for 4730 the program headers and a PT_INTERP segment for the .interp 4731 section. */ 4732 s = bfd_get_section_by_name (abfd, ".interp"); 4733 if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size != 0) 4734 { 4735 amt = sizeof (struct elf_segment_map); 4736 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 4737 if (m == NULL) 4738 goto error_return; 4739 m->next = NULL; 4740 m->p_type = PT_PHDR; 4741 m->p_flags = PF_R; 4742 m->p_flags_valid = 1; 4743 m->includes_phdrs = 1; 4744 phdr_in_segment = TRUE; 4745 *pm = m; 4746 pm = &m->next; 4747 4748 amt = sizeof (struct elf_segment_map); 4749 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 4750 if (m == NULL) 4751 goto error_return; 4752 m->next = NULL; 4753 m->p_type = PT_INTERP; 4754 m->count = 1; 4755 m->sections[0] = s; 4756 4757 *pm = m; 4758 pm = &m->next; 4759 } 4760 4761 /* Look through the sections. We put sections in the same program 4762 segment when the start of the second section can be placed within 4763 a few bytes of the end of the first section. */ 4764 last_hdr = NULL; 4765 last_size = 0; 4766 hdr_index = 0; 4767 writable = FALSE; 4768 executable = FALSE; 4769 dynsec = bfd_get_section_by_name (abfd, ".dynamic"); 4770 if (dynsec != NULL 4771 && (dynsec->flags & SEC_LOAD) == 0) 4772 dynsec = NULL; 4773 4774 if ((abfd->flags & D_PAGED) == 0) 4775 phdr_in_segment = FALSE; 4776 4777 /* Deal with -Ttext or something similar such that the first section 4778 is not adjacent to the program headers. This is an 4779 approximation, since at this point we don't know exactly how many 4780 program headers we will need. */ 4781 if (phdr_in_segment && count > 0) 4782 { 4783 bfd_vma phdr_lma; /* Bytes. */ 4784 bfd_boolean separate_phdr = FALSE; 4785 4786 phdr_lma = (sections[0]->lma - phdr_size) & addr_mask & -maxpagesize; 4787 if (info != NULL 4788 && info->separate_code 4789 && (sections[0]->flags & SEC_CODE) != 0) 4790 { 4791 /* If data sections should be separate from code and 4792 thus not executable, and the first section is 4793 executable then put the file and program headers in 4794 their own PT_LOAD. */ 4795 separate_phdr = TRUE; 4796 if ((((phdr_lma + phdr_size - 1) & addr_mask & -maxpagesize) 4797 == (sections[0]->lma & addr_mask & -maxpagesize))) 4798 { 4799 /* The file and program headers are currently on the 4800 same page as the first section. Put them on the 4801 previous page if we can. */ 4802 if (phdr_lma >= maxpagesize) 4803 phdr_lma -= maxpagesize; 4804 else 4805 separate_phdr = FALSE; 4806 } 4807 } 4808 if ((sections[0]->lma & addr_mask) < phdr_lma 4809 || (sections[0]->lma & addr_mask) < phdr_size) 4810 /* If file and program headers would be placed at the end 4811 of memory then it's probably better to omit them. */ 4812 phdr_in_segment = FALSE; 4813 else if (phdr_lma < wrap_to) 4814 /* If a section wraps around to where we'll be placing 4815 file and program headers, then the headers will be 4816 overwritten. */ 4817 phdr_in_segment = FALSE; 4818 else if (separate_phdr) 4819 { 4820 m = make_mapping (abfd, sections, 0, 0, phdr_in_segment); 4821 if (m == NULL) 4822 goto error_return; 4823 m->p_paddr = phdr_lma * opb; 4824 m->p_vaddr_offset 4825 = (sections[0]->vma - phdr_size) & addr_mask & -maxpagesize; 4826 m->p_paddr_valid = 1; 4827 *pm = m; 4828 pm = &m->next; 4829 phdr_in_segment = FALSE; 4830 } 4831 } 4832 4833 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) 4834 { 4835 asection *hdr; 4836 bfd_boolean new_segment; 4837 4838 hdr = *hdrpp; 4839 4840 /* See if this section and the last one will fit in the same 4841 segment. */ 4842 4843 if (last_hdr == NULL) 4844 { 4845 /* If we don't have a segment yet, then we don't need a new 4846 one (we build the last one after this loop). */ 4847 new_segment = FALSE; 4848 } 4849 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) 4850 { 4851 /* If this section has a different relation between the 4852 virtual address and the load address, then we need a new 4853 segment. */ 4854 new_segment = TRUE; 4855 } 4856 else if (hdr->lma < last_hdr->lma + last_size 4857 || last_hdr->lma + last_size < last_hdr->lma) 4858 { 4859 /* If this section has a load address that makes it overlap 4860 the previous section, then we need a new segment. */ 4861 new_segment = TRUE; 4862 } 4863 else if ((abfd->flags & D_PAGED) != 0 4864 && (((last_hdr->lma + last_size - 1) & -maxpagesize) 4865 == (hdr->lma & -maxpagesize))) 4866 { 4867 /* If we are demand paged then we can't map two disk 4868 pages onto the same memory page. */ 4869 new_segment = FALSE; 4870 } 4871 /* In the next test we have to be careful when last_hdr->lma is close 4872 to the end of the address space. If the aligned address wraps 4873 around to the start of the address space, then there are no more 4874 pages left in memory and it is OK to assume that the current 4875 section can be included in the current segment. */ 4876 else if ((BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) 4877 + maxpagesize > last_hdr->lma) 4878 && (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) 4879 + maxpagesize <= hdr->lma)) 4880 { 4881 /* If putting this section in this segment would force us to 4882 skip a page in the segment, then we need a new segment. */ 4883 new_segment = TRUE; 4884 } 4885 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 4886 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) 4887 { 4888 /* We don't want to put a loaded section after a 4889 nonloaded (ie. bss style) section in the same segment 4890 as that will force the non-loaded section to be loaded. 4891 Consider .tbss sections as loaded for this purpose. */ 4892 new_segment = TRUE; 4893 } 4894 else if ((abfd->flags & D_PAGED) == 0) 4895 { 4896 /* If the file is not demand paged, which means that we 4897 don't require the sections to be correctly aligned in the 4898 file, then there is no other reason for a new segment. */ 4899 new_segment = FALSE; 4900 } 4901 else if (info != NULL 4902 && info->separate_code 4903 && executable != ((hdr->flags & SEC_CODE) != 0)) 4904 { 4905 new_segment = TRUE; 4906 } 4907 else if (! writable 4908 && (hdr->flags & SEC_READONLY) == 0) 4909 { 4910 /* We don't want to put a writable section in a read only 4911 segment. */ 4912 new_segment = TRUE; 4913 } 4914 else 4915 { 4916 /* Otherwise, we can use the same segment. */ 4917 new_segment = FALSE; 4918 } 4919 4920 /* Allow interested parties a chance to override our decision. */ 4921 if (last_hdr != NULL 4922 && info != NULL 4923 && info->callbacks->override_segment_assignment != NULL) 4924 new_segment 4925 = info->callbacks->override_segment_assignment (info, abfd, hdr, 4926 last_hdr, 4927 new_segment); 4928 4929 if (! new_segment) 4930 { 4931 if ((hdr->flags & SEC_READONLY) == 0) 4932 writable = TRUE; 4933 if ((hdr->flags & SEC_CODE) != 0) 4934 executable = TRUE; 4935 last_hdr = hdr; 4936 /* .tbss sections effectively have zero size. */ 4937 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; 4938 continue; 4939 } 4940 4941 /* We need a new program segment. We must create a new program 4942 header holding all the sections from hdr_index until hdr. */ 4943 4944 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); 4945 if (m == NULL) 4946 goto error_return; 4947 4948 *pm = m; 4949 pm = &m->next; 4950 4951 if ((hdr->flags & SEC_READONLY) == 0) 4952 writable = TRUE; 4953 else 4954 writable = FALSE; 4955 4956 if ((hdr->flags & SEC_CODE) == 0) 4957 executable = FALSE; 4958 else 4959 executable = TRUE; 4960 4961 last_hdr = hdr; 4962 /* .tbss sections effectively have zero size. */ 4963 last_size = (!IS_TBSS (hdr) ? hdr->size : 0) / opb; 4964 hdr_index = i; 4965 phdr_in_segment = FALSE; 4966 } 4967 4968 /* Create a final PT_LOAD program segment, but not if it's just 4969 for .tbss. */ 4970 if (last_hdr != NULL 4971 && (i - hdr_index != 1 4972 || !IS_TBSS (last_hdr))) 4973 { 4974 m = make_mapping (abfd, sections, hdr_index, i, phdr_in_segment); 4975 if (m == NULL) 4976 goto error_return; 4977 4978 *pm = m; 4979 pm = &m->next; 4980 } 4981 4982 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ 4983 if (dynsec != NULL) 4984 { 4985 m = _bfd_elf_make_dynamic_segment (abfd, dynsec); 4986 if (m == NULL) 4987 goto error_return; 4988 *pm = m; 4989 pm = &m->next; 4990 } 4991 4992 /* For each batch of consecutive loadable SHT_NOTE sections, 4993 add a PT_NOTE segment. We don't use bfd_get_section_by_name, 4994 because if we link together nonloadable .note sections and 4995 loadable .note sections, we will generate two .note sections 4996 in the output file. */ 4997 for (s = abfd->sections; s != NULL; s = s->next) 4998 { 4999 if ((s->flags & SEC_LOAD) != 0 5000 && elf_section_type (s) == SHT_NOTE) 5001 { 5002 asection *s2; 5003 unsigned int alignment_power = s->alignment_power; 5004 5005 count = 1; 5006 for (s2 = s; s2->next != NULL; s2 = s2->next) 5007 { 5008 if (s2->next->alignment_power == alignment_power 5009 && (s2->next->flags & SEC_LOAD) != 0 5010 && elf_section_type (s2->next) == SHT_NOTE 5011 && align_power (s2->lma + s2->size / opb, 5012 alignment_power) 5013 == s2->next->lma) 5014 count++; 5015 else 5016 break; 5017 } 5018 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 5019 amt += count * sizeof (asection *); 5020 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5021 if (m == NULL) 5022 goto error_return; 5023 m->next = NULL; 5024 m->p_type = PT_NOTE; 5025 m->count = count; 5026 while (count > 1) 5027 { 5028 m->sections[m->count - count--] = s; 5029 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); 5030 s = s->next; 5031 } 5032 m->sections[m->count - 1] = s; 5033 BFD_ASSERT ((s->flags & SEC_THREAD_LOCAL) == 0); 5034 *pm = m; 5035 pm = &m->next; 5036 } 5037 if (s->flags & SEC_THREAD_LOCAL) 5038 { 5039 if (! tls_count) 5040 first_tls = s; 5041 tls_count++; 5042 } 5043 if (first_mbind == NULL 5044 && (elf_section_flags (s) & SHF_GNU_MBIND) != 0) 5045 first_mbind = s; 5046 } 5047 5048 /* If there are any SHF_TLS output sections, add PT_TLS segment. */ 5049 if (tls_count > 0) 5050 { 5051 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 5052 amt += tls_count * sizeof (asection *); 5053 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5054 if (m == NULL) 5055 goto error_return; 5056 m->next = NULL; 5057 m->p_type = PT_TLS; 5058 m->count = tls_count; 5059 /* Mandated PF_R. */ 5060 m->p_flags = PF_R; 5061 m->p_flags_valid = 1; 5062 s = first_tls; 5063 for (i = 0; i < tls_count; ++i) 5064 { 5065 if ((s->flags & SEC_THREAD_LOCAL) == 0) 5066 { 5067 _bfd_error_handler 5068 (_("%pB: TLS sections are not adjacent:"), abfd); 5069 s = first_tls; 5070 i = 0; 5071 while (i < tls_count) 5072 { 5073 if ((s->flags & SEC_THREAD_LOCAL) != 0) 5074 { 5075 _bfd_error_handler (_(" TLS: %pA"), s); 5076 i++; 5077 } 5078 else 5079 _bfd_error_handler (_(" non-TLS: %pA"), s); 5080 s = s->next; 5081 } 5082 bfd_set_error (bfd_error_bad_value); 5083 goto error_return; 5084 } 5085 m->sections[i] = s; 5086 s = s->next; 5087 } 5088 5089 *pm = m; 5090 pm = &m->next; 5091 } 5092 5093 if (first_mbind 5094 && (abfd->flags & D_PAGED) != 0 5095 && (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0) 5096 for (s = first_mbind; s != NULL; s = s->next) 5097 if ((elf_section_flags (s) & SHF_GNU_MBIND) != 0 5098 && elf_section_data (s)->this_hdr.sh_info <= PT_GNU_MBIND_NUM) 5099 { 5100 /* Mandated PF_R. */ 5101 unsigned long p_flags = PF_R; 5102 if ((s->flags & SEC_READONLY) == 0) 5103 p_flags |= PF_W; 5104 if ((s->flags & SEC_CODE) != 0) 5105 p_flags |= PF_X; 5106 5107 amt = sizeof (struct elf_segment_map) + sizeof (asection *); 5108 m = bfd_zalloc (abfd, amt); 5109 if (m == NULL) 5110 goto error_return; 5111 m->next = NULL; 5112 m->p_type = (PT_GNU_MBIND_LO 5113 + elf_section_data (s)->this_hdr.sh_info); 5114 m->count = 1; 5115 m->p_flags_valid = 1; 5116 m->sections[0] = s; 5117 m->p_flags = p_flags; 5118 5119 *pm = m; 5120 pm = &m->next; 5121 } 5122 5123 s = bfd_get_section_by_name (abfd, 5124 NOTE_GNU_PROPERTY_SECTION_NAME); 5125 if (s != NULL && s->size != 0) 5126 { 5127 amt = sizeof (struct elf_segment_map) + sizeof (asection *); 5128 m = bfd_zalloc (abfd, amt); 5129 if (m == NULL) 5130 goto error_return; 5131 m->next = NULL; 5132 m->p_type = PT_GNU_PROPERTY; 5133 m->count = 1; 5134 m->p_flags_valid = 1; 5135 m->sections[0] = s; 5136 m->p_flags = PF_R; 5137 *pm = m; 5138 pm = &m->next; 5139 } 5140 5141 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME 5142 segment. */ 5143 eh_frame_hdr = elf_eh_frame_hdr (abfd); 5144 if (eh_frame_hdr != NULL 5145 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) 5146 { 5147 amt = sizeof (struct elf_segment_map); 5148 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5149 if (m == NULL) 5150 goto error_return; 5151 m->next = NULL; 5152 m->p_type = PT_GNU_EH_FRAME; 5153 m->count = 1; 5154 m->sections[0] = eh_frame_hdr->output_section; 5155 5156 *pm = m; 5157 pm = &m->next; 5158 } 5159 5160 if (elf_stack_flags (abfd)) 5161 { 5162 amt = sizeof (struct elf_segment_map); 5163 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5164 if (m == NULL) 5165 goto error_return; 5166 m->next = NULL; 5167 m->p_type = PT_GNU_STACK; 5168 m->p_flags = elf_stack_flags (abfd); 5169 m->p_align = bed->stack_align; 5170 m->p_flags_valid = 1; 5171 m->p_align_valid = m->p_align != 0; 5172 if (info->stacksize > 0) 5173 { 5174 m->p_size = info->stacksize; 5175 m->p_size_valid = 1; 5176 } 5177 5178 *pm = m; 5179 pm = &m->next; 5180 } 5181 5182 if (info != NULL && info->relro) 5183 { 5184 for (m = mfirst; m != NULL; m = m->next) 5185 { 5186 if (m->p_type == PT_LOAD 5187 && m->count != 0 5188 && m->sections[0]->vma >= info->relro_start 5189 && m->sections[0]->vma < info->relro_end) 5190 { 5191 i = m->count; 5192 while (--i != (unsigned) -1) 5193 { 5194 if (m->sections[i]->size > 0 5195 && (m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) 5196 == (SEC_LOAD | SEC_HAS_CONTENTS)) 5197 break; 5198 } 5199 5200 if (i != (unsigned) -1) 5201 break; 5202 } 5203 } 5204 5205 /* Make a PT_GNU_RELRO segment only when it isn't empty. */ 5206 if (m != NULL) 5207 { 5208 amt = sizeof (struct elf_segment_map); 5209 m = (struct elf_segment_map *) bfd_zalloc (abfd, amt); 5210 if (m == NULL) 5211 goto error_return; 5212 m->next = NULL; 5213 m->p_type = PT_GNU_RELRO; 5214 *pm = m; 5215 pm = &m->next; 5216 } 5217 } 5218 5219 free (sections); 5220 elf_seg_map (abfd) = mfirst; 5221 } 5222 5223 if (!elf_modify_segment_map (abfd, info, no_user_phdrs)) 5224 return FALSE; 5225 5226 for (count = 0, m = elf_seg_map (abfd); m != NULL; m = m->next) 5227 ++count; 5228 elf_program_header_size (abfd) = count * bed->s->sizeof_phdr; 5229 5230 return TRUE; 5231 5232 error_return: 5233 free (sections); 5234 return FALSE; 5235} 5236 5237/* Sort sections by address. */ 5238 5239static int 5240elf_sort_sections (const void *arg1, const void *arg2) 5241{ 5242 const asection *sec1 = *(const asection **) arg1; 5243 const asection *sec2 = *(const asection **) arg2; 5244 bfd_size_type size1, size2; 5245 5246 /* Sort by LMA first, since this is the address used to 5247 place the section into a segment. */ 5248 if (sec1->lma < sec2->lma) 5249 return -1; 5250 else if (sec1->lma > sec2->lma) 5251 return 1; 5252 5253 /* Then sort by VMA. Normally the LMA and the VMA will be 5254 the same, and this will do nothing. */ 5255 if (sec1->vma < sec2->vma) 5256 return -1; 5257 else if (sec1->vma > sec2->vma) 5258 return 1; 5259 5260 /* Put !SEC_LOAD sections after SEC_LOAD ones. */ 5261 5262#define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) 5263 5264 if (TOEND (sec1)) 5265 { 5266 if (!TOEND (sec2)) 5267 return 1; 5268 } 5269 else if (TOEND (sec2)) 5270 return -1; 5271 5272#undef TOEND 5273 5274 /* Sort by size, to put zero sized sections 5275 before others at the same address. */ 5276 5277 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; 5278 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; 5279 5280 if (size1 < size2) 5281 return -1; 5282 if (size1 > size2) 5283 return 1; 5284 5285 return sec1->target_index - sec2->target_index; 5286} 5287 5288/* This qsort comparison functions sorts PT_LOAD segments first and 5289 by p_paddr, for assign_file_positions_for_load_sections. */ 5290 5291static int 5292elf_sort_segments (const void *arg1, const void *arg2) 5293{ 5294 const struct elf_segment_map *m1 = *(const struct elf_segment_map **) arg1; 5295 const struct elf_segment_map *m2 = *(const struct elf_segment_map **) arg2; 5296 5297 if (m1->p_type != m2->p_type) 5298 { 5299 if (m1->p_type == PT_NULL) 5300 return 1; 5301 if (m2->p_type == PT_NULL) 5302 return -1; 5303 return m1->p_type < m2->p_type ? -1 : 1; 5304 } 5305 if (m1->includes_filehdr != m2->includes_filehdr) 5306 return m1->includes_filehdr ? -1 : 1; 5307 if (m1->no_sort_lma != m2->no_sort_lma) 5308 return m1->no_sort_lma ? -1 : 1; 5309 if (m1->p_type == PT_LOAD && !m1->no_sort_lma) 5310 { 5311 bfd_vma lma1, lma2; /* Octets. */ 5312 lma1 = 0; 5313 if (m1->p_paddr_valid) 5314 lma1 = m1->p_paddr; 5315 else if (m1->count != 0) 5316 { 5317 unsigned int opb = bfd_octets_per_byte (m1->sections[0]->owner, 5318 m1->sections[0]); 5319 lma1 = (m1->sections[0]->lma + m1->p_vaddr_offset) * opb; 5320 } 5321 lma2 = 0; 5322 if (m2->p_paddr_valid) 5323 lma2 = m2->p_paddr; 5324 else if (m2->count != 0) 5325 { 5326 unsigned int opb = bfd_octets_per_byte (m2->sections[0]->owner, 5327 m2->sections[0]); 5328 lma2 = (m2->sections[0]->lma + m2->p_vaddr_offset) * opb; 5329 } 5330 if (lma1 != lma2) 5331 return lma1 < lma2 ? -1 : 1; 5332 } 5333 if (m1->idx != m2->idx) 5334 return m1->idx < m2->idx ? -1 : 1; 5335 return 0; 5336} 5337 5338/* Ian Lance Taylor writes: 5339 5340 We shouldn't be using % with a negative signed number. That's just 5341 not good. We have to make sure either that the number is not 5342 negative, or that the number has an unsigned type. When the types 5343 are all the same size they wind up as unsigned. When file_ptr is a 5344 larger signed type, the arithmetic winds up as signed long long, 5345 which is wrong. 5346 5347 What we're trying to say here is something like ``increase OFF by 5348 the least amount that will cause it to be equal to the VMA modulo 5349 the page size.'' */ 5350/* In other words, something like: 5351 5352 vma_offset = m->sections[0]->vma % bed->maxpagesize; 5353 off_offset = off % bed->maxpagesize; 5354 if (vma_offset < off_offset) 5355 adjustment = vma_offset + bed->maxpagesize - off_offset; 5356 else 5357 adjustment = vma_offset - off_offset; 5358 5359 which can be collapsed into the expression below. */ 5360 5361static file_ptr 5362vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) 5363{ 5364 /* PR binutils/16199: Handle an alignment of zero. */ 5365 if (maxpagesize == 0) 5366 maxpagesize = 1; 5367 return ((vma - off) % maxpagesize); 5368} 5369 5370static void 5371print_segment_map (const struct elf_segment_map *m) 5372{ 5373 unsigned int j; 5374 const char *pt = get_segment_type (m->p_type); 5375 char buf[32]; 5376 5377 if (pt == NULL) 5378 { 5379 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) 5380 sprintf (buf, "LOPROC+%7.7x", 5381 (unsigned int) (m->p_type - PT_LOPROC)); 5382 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) 5383 sprintf (buf, "LOOS+%7.7x", 5384 (unsigned int) (m->p_type - PT_LOOS)); 5385 else 5386 snprintf (buf, sizeof (buf), "%8.8x", 5387 (unsigned int) m->p_type); 5388 pt = buf; 5389 } 5390 fflush (stdout); 5391 fprintf (stderr, "%s:", pt); 5392 for (j = 0; j < m->count; j++) 5393 fprintf (stderr, " %s", m->sections [j]->name); 5394 putc ('\n',stderr); 5395 fflush (stderr); 5396} 5397 5398static bfd_boolean 5399write_zeros (bfd *abfd, file_ptr pos, bfd_size_type len) 5400{ 5401 void *buf; 5402 bfd_boolean ret; 5403 5404 if (bfd_seek (abfd, pos, SEEK_SET) != 0) 5405 return FALSE; 5406 buf = bfd_zmalloc (len); 5407 if (buf == NULL) 5408 return FALSE; 5409 ret = bfd_bwrite (buf, len, abfd) == len; 5410 free (buf); 5411 return ret; 5412} 5413 5414/* Assign file positions to the sections based on the mapping from 5415 sections to segments. This function also sets up some fields in 5416 the file header. */ 5417 5418static bfd_boolean 5419assign_file_positions_for_load_sections (bfd *abfd, 5420 struct bfd_link_info *link_info) 5421{ 5422 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 5423 struct elf_segment_map *m; 5424 struct elf_segment_map *phdr_load_seg; 5425 Elf_Internal_Phdr *phdrs; 5426 Elf_Internal_Phdr *p; 5427 file_ptr off; /* Octets. */ 5428 bfd_size_type maxpagesize; 5429 unsigned int alloc, actual; 5430 unsigned int i, j; 5431 struct elf_segment_map **sorted_seg_map; 5432 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 5433 5434 if (link_info == NULL 5435 && !_bfd_elf_map_sections_to_segments (abfd, link_info)) 5436 return FALSE; 5437 5438 alloc = 0; 5439 for (m = elf_seg_map (abfd); m != NULL; m = m->next) 5440 m->idx = alloc++; 5441 5442 if (alloc) 5443 { 5444 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; 5445 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; 5446 } 5447 else 5448 { 5449 /* PR binutils/12467. */ 5450 elf_elfheader (abfd)->e_phoff = 0; 5451 elf_elfheader (abfd)->e_phentsize = 0; 5452 } 5453 5454 elf_elfheader (abfd)->e_phnum = alloc; 5455 5456 if (elf_program_header_size (abfd) == (bfd_size_type) -1) 5457 { 5458 actual = alloc; 5459 elf_program_header_size (abfd) = alloc * bed->s->sizeof_phdr; 5460 } 5461 else 5462 { 5463 actual = elf_program_header_size (abfd) / bed->s->sizeof_phdr; 5464 BFD_ASSERT (elf_program_header_size (abfd) 5465 == actual * bed->s->sizeof_phdr); 5466 BFD_ASSERT (actual >= alloc); 5467 } 5468 5469 if (alloc == 0) 5470 { 5471 elf_next_file_pos (abfd) = bed->s->sizeof_ehdr; 5472 return TRUE; 5473 } 5474 5475 /* We're writing the size in elf_program_header_size (abfd), 5476 see assign_file_positions_except_relocs, so make sure we have 5477 that amount allocated, with trailing space cleared. 5478 The variable alloc contains the computed need, while 5479 elf_program_header_size (abfd) contains the size used for the 5480 layout. 5481 See ld/emultempl/elf-generic.em:gld${EMULATION_NAME}_map_segments 5482 where the layout is forced to according to a larger size in the 5483 last iterations for the testcase ld-elf/header. */ 5484 phdrs = bfd_zalloc (abfd, (actual * sizeof (*phdrs) 5485 + alloc * sizeof (*sorted_seg_map))); 5486 sorted_seg_map = (struct elf_segment_map **) (phdrs + actual); 5487 elf_tdata (abfd)->phdr = phdrs; 5488 if (phdrs == NULL) 5489 return FALSE; 5490 5491 for (m = elf_seg_map (abfd), j = 0; m != NULL; m = m->next, j++) 5492 { 5493 sorted_seg_map[j] = m; 5494 /* If elf_segment_map is not from map_sections_to_segments, the 5495 sections may not be correctly ordered. NOTE: sorting should 5496 not be done to the PT_NOTE section of a corefile, which may 5497 contain several pseudo-sections artificially created by bfd. 5498 Sorting these pseudo-sections breaks things badly. */ 5499 if (m->count > 1 5500 && !(elf_elfheader (abfd)->e_type == ET_CORE 5501 && m->p_type == PT_NOTE)) 5502 { 5503 for (i = 0; i < m->count; i++) 5504 m->sections[i]->target_index = i; 5505 qsort (m->sections, (size_t) m->count, sizeof (asection *), 5506 elf_sort_sections); 5507 } 5508 } 5509 if (alloc > 1) 5510 qsort (sorted_seg_map, alloc, sizeof (*sorted_seg_map), 5511 elf_sort_segments); 5512 5513 maxpagesize = 1; 5514 if ((abfd->flags & D_PAGED) != 0) 5515 maxpagesize = bed->maxpagesize; 5516 5517 /* Sections must map to file offsets past the ELF file header. */ 5518 off = bed->s->sizeof_ehdr; 5519 /* And if one of the PT_LOAD headers doesn't include the program 5520 headers then we'll be mapping program headers in the usual 5521 position after the ELF file header. */ 5522 phdr_load_seg = NULL; 5523 for (j = 0; j < alloc; j++) 5524 { 5525 m = sorted_seg_map[j]; 5526 if (m->p_type != PT_LOAD) 5527 break; 5528 if (m->includes_phdrs) 5529 { 5530 phdr_load_seg = m; 5531 break; 5532 } 5533 } 5534 if (phdr_load_seg == NULL) 5535 off += actual * bed->s->sizeof_phdr; 5536 5537 for (j = 0; j < alloc; j++) 5538 { 5539 asection **secpp; 5540 bfd_vma off_adjust; /* Octets. */ 5541 bfd_boolean no_contents; 5542 5543 /* An ELF segment (described by Elf_Internal_Phdr) may contain a 5544 number of sections with contents contributing to both p_filesz 5545 and p_memsz, followed by a number of sections with no contents 5546 that just contribute to p_memsz. In this loop, OFF tracks next 5547 available file offset for PT_LOAD and PT_NOTE segments. */ 5548 m = sorted_seg_map[j]; 5549 p = phdrs + m->idx; 5550 p->p_type = m->p_type; 5551 p->p_flags = m->p_flags; 5552 5553 if (m->count == 0) 5554 p->p_vaddr = m->p_vaddr_offset * opb; 5555 else 5556 p->p_vaddr = (m->sections[0]->vma + m->p_vaddr_offset) * opb; 5557 5558 if (m->p_paddr_valid) 5559 p->p_paddr = m->p_paddr; 5560 else if (m->count == 0) 5561 p->p_paddr = 0; 5562 else 5563 p->p_paddr = (m->sections[0]->lma + m->p_vaddr_offset) * opb; 5564 5565 if (p->p_type == PT_LOAD 5566 && (abfd->flags & D_PAGED) != 0) 5567 { 5568 /* p_align in demand paged PT_LOAD segments effectively stores 5569 the maximum page size. When copying an executable with 5570 objcopy, we set m->p_align from the input file. Use this 5571 value for maxpagesize rather than bed->maxpagesize, which 5572 may be different. Note that we use maxpagesize for PT_TLS 5573 segment alignment later in this function, so we are relying 5574 on at least one PT_LOAD segment appearing before a PT_TLS 5575 segment. */ 5576 if (m->p_align_valid) 5577 maxpagesize = m->p_align; 5578 5579 p->p_align = maxpagesize; 5580 } 5581 else if (m->p_align_valid) 5582 p->p_align = m->p_align; 5583 else if (m->count == 0) 5584 p->p_align = 1 << bed->s->log_file_align; 5585 5586 if (m == phdr_load_seg) 5587 { 5588 if (!m->includes_filehdr) 5589 p->p_offset = off; 5590 off += actual * bed->s->sizeof_phdr; 5591 } 5592 5593 no_contents = FALSE; 5594 off_adjust = 0; 5595 if (p->p_type == PT_LOAD 5596 && m->count > 0) 5597 { 5598 bfd_size_type align; /* Bytes. */ 5599 unsigned int align_power = 0; 5600 5601 if (m->p_align_valid) 5602 align = p->p_align; 5603 else 5604 { 5605 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 5606 { 5607 unsigned int secalign; 5608 5609 secalign = bfd_section_alignment (*secpp); 5610 if (secalign > align_power) 5611 align_power = secalign; 5612 } 5613 align = (bfd_size_type) 1 << align_power; 5614 if (align < maxpagesize) 5615 align = maxpagesize; 5616 } 5617 5618 for (i = 0; i < m->count; i++) 5619 if ((m->sections[i]->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) 5620 /* If we aren't making room for this section, then 5621 it must be SHT_NOBITS regardless of what we've 5622 set via struct bfd_elf_special_section. */ 5623 elf_section_type (m->sections[i]) = SHT_NOBITS; 5624 5625 /* Find out whether this segment contains any loadable 5626 sections. */ 5627 no_contents = TRUE; 5628 for (i = 0; i < m->count; i++) 5629 if (elf_section_type (m->sections[i]) != SHT_NOBITS) 5630 { 5631 no_contents = FALSE; 5632 break; 5633 } 5634 5635 off_adjust = vma_page_aligned_bias (p->p_vaddr, off, align * opb); 5636 5637 /* Broken hardware and/or kernel require that files do not 5638 map the same page with different permissions on some hppa 5639 processors. */ 5640 if (j != 0 5641 && (abfd->flags & D_PAGED) != 0 5642 && bed->no_page_alias 5643 && (off & (maxpagesize - 1)) != 0 5644 && ((off & -maxpagesize) 5645 == ((off + off_adjust) & -maxpagesize))) 5646 off_adjust += maxpagesize; 5647 off += off_adjust; 5648 if (no_contents) 5649 { 5650 /* We shouldn't need to align the segment on disk since 5651 the segment doesn't need file space, but the gABI 5652 arguably requires the alignment and glibc ld.so 5653 checks it. So to comply with the alignment 5654 requirement but not waste file space, we adjust 5655 p_offset for just this segment. (OFF_ADJUST is 5656 subtracted from OFF later.) This may put p_offset 5657 past the end of file, but that shouldn't matter. */ 5658 } 5659 else 5660 off_adjust = 0; 5661 } 5662 /* Make sure the .dynamic section is the first section in the 5663 PT_DYNAMIC segment. */ 5664 else if (p->p_type == PT_DYNAMIC 5665 && m->count > 1 5666 && strcmp (m->sections[0]->name, ".dynamic") != 0) 5667 { 5668 _bfd_error_handler 5669 (_("%pB: The first section in the PT_DYNAMIC segment" 5670 " is not the .dynamic section"), 5671 abfd); 5672 bfd_set_error (bfd_error_bad_value); 5673 return FALSE; 5674 } 5675 /* Set the note section type to SHT_NOTE. */ 5676 else if (p->p_type == PT_NOTE) 5677 for (i = 0; i < m->count; i++) 5678 elf_section_type (m->sections[i]) = SHT_NOTE; 5679 5680 if (m->includes_filehdr) 5681 { 5682 if (!m->p_flags_valid) 5683 p->p_flags |= PF_R; 5684 p->p_filesz = bed->s->sizeof_ehdr; 5685 p->p_memsz = bed->s->sizeof_ehdr; 5686 if (p->p_type == PT_LOAD) 5687 { 5688 if (m->count > 0) 5689 { 5690 if (p->p_vaddr < (bfd_vma) off 5691 || (!m->p_paddr_valid 5692 && p->p_paddr < (bfd_vma) off)) 5693 { 5694 _bfd_error_handler 5695 (_("%pB: not enough room for program headers," 5696 " try linking with -N"), 5697 abfd); 5698 bfd_set_error (bfd_error_bad_value); 5699 return FALSE; 5700 } 5701 p->p_vaddr -= off; 5702 if (!m->p_paddr_valid) 5703 p->p_paddr -= off; 5704 } 5705 } 5706 else if (sorted_seg_map[0]->includes_filehdr) 5707 { 5708 Elf_Internal_Phdr *filehdr = phdrs + sorted_seg_map[0]->idx; 5709 p->p_vaddr = filehdr->p_vaddr; 5710 if (!m->p_paddr_valid) 5711 p->p_paddr = filehdr->p_paddr; 5712 } 5713 } 5714 5715 if (m->includes_phdrs) 5716 { 5717 if (!m->p_flags_valid) 5718 p->p_flags |= PF_R; 5719 p->p_filesz += actual * bed->s->sizeof_phdr; 5720 p->p_memsz += actual * bed->s->sizeof_phdr; 5721 if (!m->includes_filehdr) 5722 { 5723 if (p->p_type == PT_LOAD) 5724 { 5725 elf_elfheader (abfd)->e_phoff = p->p_offset; 5726 if (m->count > 0) 5727 { 5728 p->p_vaddr -= off - p->p_offset; 5729 if (!m->p_paddr_valid) 5730 p->p_paddr -= off - p->p_offset; 5731 } 5732 } 5733 else if (phdr_load_seg != NULL) 5734 { 5735 Elf_Internal_Phdr *phdr = phdrs + phdr_load_seg->idx; 5736 bfd_vma phdr_off = 0; /* Octets. */ 5737 if (phdr_load_seg->includes_filehdr) 5738 phdr_off = bed->s->sizeof_ehdr; 5739 p->p_vaddr = phdr->p_vaddr + phdr_off; 5740 if (!m->p_paddr_valid) 5741 p->p_paddr = phdr->p_paddr + phdr_off; 5742 p->p_offset = phdr->p_offset + phdr_off; 5743 } 5744 else 5745 p->p_offset = bed->s->sizeof_ehdr; 5746 } 5747 } 5748 5749 if (p->p_type == PT_LOAD 5750 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) 5751 { 5752 if (!m->includes_filehdr && !m->includes_phdrs) 5753 { 5754 p->p_offset = off; 5755 if (no_contents) 5756 { 5757 /* Put meaningless p_offset for PT_LOAD segments 5758 without file contents somewhere within the first 5759 page, in an attempt to not point past EOF. */ 5760 bfd_size_type align = maxpagesize; 5761 if (align < p->p_align) 5762 align = p->p_align; 5763 if (align < 1) 5764 align = 1; 5765 p->p_offset = off % align; 5766 } 5767 } 5768 else 5769 { 5770 file_ptr adjust; /* Octets. */ 5771 5772 adjust = off - (p->p_offset + p->p_filesz); 5773 if (!no_contents) 5774 p->p_filesz += adjust; 5775 p->p_memsz += adjust; 5776 } 5777 } 5778 5779 /* Set up p_filesz, p_memsz, p_align and p_flags from the section 5780 maps. Set filepos for sections in PT_LOAD segments, and in 5781 core files, for sections in PT_NOTE segments. 5782 assign_file_positions_for_non_load_sections will set filepos 5783 for other sections and update p_filesz for other segments. */ 5784 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 5785 { 5786 asection *sec; 5787 bfd_size_type align; 5788 Elf_Internal_Shdr *this_hdr; 5789 5790 sec = *secpp; 5791 this_hdr = &elf_section_data (sec)->this_hdr; 5792 align = (bfd_size_type) 1 << bfd_section_alignment (sec); 5793 5794 if ((p->p_type == PT_LOAD 5795 || p->p_type == PT_TLS) 5796 && (this_hdr->sh_type != SHT_NOBITS 5797 || ((this_hdr->sh_flags & SHF_ALLOC) != 0 5798 && ((this_hdr->sh_flags & SHF_TLS) == 0 5799 || p->p_type == PT_TLS)))) 5800 { 5801 bfd_vma p_start = p->p_paddr; /* Octets. */ 5802 bfd_vma p_end = p_start + p->p_memsz; /* Octets. */ 5803 bfd_vma s_start = sec->lma * opb; /* Octets. */ 5804 bfd_vma adjust = s_start - p_end; /* Octets. */ 5805 5806 if (adjust != 0 5807 && (s_start < p_end 5808 || p_end < p_start)) 5809 { 5810 _bfd_error_handler 5811 /* xgettext:c-format */ 5812 (_("%pB: section %pA lma %#" PRIx64 " adjusted to %#" PRIx64), 5813 abfd, sec, (uint64_t) s_start / opb, 5814 (uint64_t) p_end / opb); 5815 adjust = 0; 5816 sec->lma = p_end / opb; 5817 } 5818 p->p_memsz += adjust; 5819 5820 if (p->p_type == PT_LOAD) 5821 { 5822 if (this_hdr->sh_type != SHT_NOBITS) 5823 { 5824 off_adjust = 0; 5825 if (p->p_filesz + adjust < p->p_memsz) 5826 { 5827 /* We have a PROGBITS section following NOBITS ones. 5828 Allocate file space for the NOBITS section(s) and 5829 zero it. */ 5830 adjust = p->p_memsz - p->p_filesz; 5831 if (!write_zeros (abfd, off, adjust)) 5832 return FALSE; 5833 } 5834 } 5835 /* We only adjust sh_offset in SHT_NOBITS sections 5836 as would seem proper for their address when the 5837 section is first in the segment. sh_offset 5838 doesn't really have any significance for 5839 SHT_NOBITS anyway, apart from a notional position 5840 relative to other sections. Historically we 5841 didn't bother with adjusting sh_offset and some 5842 programs depend on it not being adjusted. See 5843 pr12921 and pr25662. */ 5844 if (this_hdr->sh_type != SHT_NOBITS || i == 0) 5845 { 5846 off += adjust; 5847 if (this_hdr->sh_type == SHT_NOBITS) 5848 off_adjust += adjust; 5849 } 5850 } 5851 if (this_hdr->sh_type != SHT_NOBITS) 5852 p->p_filesz += adjust; 5853 } 5854 5855 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) 5856 { 5857 /* The section at i == 0 is the one that actually contains 5858 everything. */ 5859 if (i == 0) 5860 { 5861 this_hdr->sh_offset = sec->filepos = off; 5862 off += this_hdr->sh_size; 5863 p->p_filesz = this_hdr->sh_size; 5864 p->p_memsz = 0; 5865 p->p_align = 1; 5866 } 5867 else 5868 { 5869 /* The rest are fake sections that shouldn't be written. */ 5870 sec->filepos = 0; 5871 sec->size = 0; 5872 sec->flags = 0; 5873 continue; 5874 } 5875 } 5876 else 5877 { 5878 if (p->p_type == PT_LOAD) 5879 { 5880 this_hdr->sh_offset = sec->filepos = off; 5881 if (this_hdr->sh_type != SHT_NOBITS) 5882 off += this_hdr->sh_size; 5883 } 5884 else if (this_hdr->sh_type == SHT_NOBITS 5885 && (this_hdr->sh_flags & SHF_TLS) != 0 5886 && this_hdr->sh_offset == 0) 5887 { 5888 /* This is a .tbss section that didn't get a PT_LOAD. 5889 (See _bfd_elf_map_sections_to_segments "Create a 5890 final PT_LOAD".) Set sh_offset to the value it 5891 would have if we had created a zero p_filesz and 5892 p_memsz PT_LOAD header for the section. This 5893 also makes the PT_TLS header have the same 5894 p_offset value. */ 5895 bfd_vma adjust = vma_page_aligned_bias (this_hdr->sh_addr, 5896 off, align); 5897 this_hdr->sh_offset = sec->filepos = off + adjust; 5898 } 5899 5900 if (this_hdr->sh_type != SHT_NOBITS) 5901 { 5902 p->p_filesz += this_hdr->sh_size; 5903 /* A load section without SHF_ALLOC is something like 5904 a note section in a PT_NOTE segment. These take 5905 file space but are not loaded into memory. */ 5906 if ((this_hdr->sh_flags & SHF_ALLOC) != 0) 5907 p->p_memsz += this_hdr->sh_size; 5908 } 5909 else if ((this_hdr->sh_flags & SHF_ALLOC) != 0) 5910 { 5911 if (p->p_type == PT_TLS) 5912 p->p_memsz += this_hdr->sh_size; 5913 5914 /* .tbss is special. It doesn't contribute to p_memsz of 5915 normal segments. */ 5916 else if ((this_hdr->sh_flags & SHF_TLS) == 0) 5917 p->p_memsz += this_hdr->sh_size; 5918 } 5919 5920 if (align > p->p_align 5921 && !m->p_align_valid 5922 && (p->p_type != PT_LOAD 5923 || (abfd->flags & D_PAGED) == 0)) 5924 p->p_align = align; 5925 } 5926 5927 if (!m->p_flags_valid) 5928 { 5929 p->p_flags |= PF_R; 5930 if ((this_hdr->sh_flags & SHF_EXECINSTR) != 0) 5931 p->p_flags |= PF_X; 5932 if ((this_hdr->sh_flags & SHF_WRITE) != 0) 5933 p->p_flags |= PF_W; 5934 } 5935 } 5936 5937 off -= off_adjust; 5938 5939 /* PR ld/20815 - Check that the program header segment, if 5940 present, will be loaded into memory. */ 5941 if (p->p_type == PT_PHDR 5942 && phdr_load_seg == NULL 5943 && !(bed->elf_backend_allow_non_load_phdr != NULL 5944 && bed->elf_backend_allow_non_load_phdr (abfd, phdrs, alloc))) 5945 { 5946 /* The fix for this error is usually to edit the linker script being 5947 used and set up the program headers manually. Either that or 5948 leave room for the headers at the start of the SECTIONS. */ 5949 _bfd_error_handler (_("%pB: error: PHDR segment not covered" 5950 " by LOAD segment"), 5951 abfd); 5952 if (link_info == NULL) 5953 return FALSE; 5954 /* Arrange for the linker to exit with an error, deleting 5955 the output file unless --noinhibit-exec is given. */ 5956 link_info->callbacks->info ("%X"); 5957 } 5958 5959 /* Check that all sections are in a PT_LOAD segment. 5960 Don't check funky gdb generated core files. */ 5961 if (p->p_type == PT_LOAD && bfd_get_format (abfd) != bfd_core) 5962 { 5963 bfd_boolean check_vma = TRUE; 5964 5965 for (i = 1; i < m->count; i++) 5966 if (m->sections[i]->vma == m->sections[i - 1]->vma 5967 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i]) 5968 ->this_hdr), p) != 0 5969 && ELF_SECTION_SIZE (&(elf_section_data (m->sections[i - 1]) 5970 ->this_hdr), p) != 0) 5971 { 5972 /* Looks like we have overlays packed into the segment. */ 5973 check_vma = FALSE; 5974 break; 5975 } 5976 5977 for (i = 0; i < m->count; i++) 5978 { 5979 Elf_Internal_Shdr *this_hdr; 5980 asection *sec; 5981 5982 sec = m->sections[i]; 5983 this_hdr = &(elf_section_data(sec)->this_hdr); 5984 if (!ELF_SECTION_IN_SEGMENT_1 (this_hdr, p, check_vma, 0) 5985 && !ELF_TBSS_SPECIAL (this_hdr, p)) 5986 { 5987 _bfd_error_handler 5988 /* xgettext:c-format */ 5989 (_("%pB: section `%pA' can't be allocated in segment %d"), 5990 abfd, sec, j); 5991 print_segment_map (m); 5992 } 5993 } 5994 } 5995 } 5996 5997 elf_next_file_pos (abfd) = off; 5998 5999 if (link_info != NULL 6000 && phdr_load_seg != NULL 6001 && phdr_load_seg->includes_filehdr) 6002 { 6003 /* There is a segment that contains both the file headers and the 6004 program headers, so provide a symbol __ehdr_start pointing there. 6005 A program can use this to examine itself robustly. */ 6006 6007 struct elf_link_hash_entry *hash 6008 = elf_link_hash_lookup (elf_hash_table (link_info), "__ehdr_start", 6009 FALSE, FALSE, TRUE); 6010 /* If the symbol was referenced and not defined, define it. */ 6011 if (hash != NULL 6012 && (hash->root.type == bfd_link_hash_new 6013 || hash->root.type == bfd_link_hash_undefined 6014 || hash->root.type == bfd_link_hash_undefweak 6015 || hash->root.type == bfd_link_hash_common)) 6016 { 6017 asection *s = NULL; 6018 bfd_vma filehdr_vaddr = phdrs[phdr_load_seg->idx].p_vaddr / opb; 6019 6020 if (phdr_load_seg->count != 0) 6021 /* The segment contains sections, so use the first one. */ 6022 s = phdr_load_seg->sections[0]; 6023 else 6024 /* Use the first (i.e. lowest-addressed) section in any segment. */ 6025 for (m = elf_seg_map (abfd); m != NULL; m = m->next) 6026 if (m->p_type == PT_LOAD && m->count != 0) 6027 { 6028 s = m->sections[0]; 6029 break; 6030 } 6031 6032 if (s != NULL) 6033 { 6034 hash->root.u.def.value = filehdr_vaddr - s->vma; 6035 hash->root.u.def.section = s; 6036 } 6037 else 6038 { 6039 hash->root.u.def.value = filehdr_vaddr; 6040 hash->root.u.def.section = bfd_abs_section_ptr; 6041 } 6042 6043 hash->root.type = bfd_link_hash_defined; 6044 hash->def_regular = 1; 6045 hash->non_elf = 0; 6046 } 6047 } 6048 6049 return TRUE; 6050} 6051 6052/* Determine if a bfd is a debuginfo file. Unfortunately there 6053 is no defined method for detecting such files, so we have to 6054 use heuristics instead. */ 6055 6056bfd_boolean 6057is_debuginfo_file (bfd *abfd) 6058{ 6059 if (abfd == NULL || bfd_get_flavour (abfd) != bfd_target_elf_flavour) 6060 return FALSE; 6061 6062 Elf_Internal_Shdr **start_headers = elf_elfsections (abfd); 6063 Elf_Internal_Shdr **end_headers = start_headers + elf_numsections (abfd); 6064 Elf_Internal_Shdr **headerp; 6065 6066 for (headerp = start_headers; headerp < end_headers; headerp ++) 6067 { 6068 Elf_Internal_Shdr *header = * headerp; 6069 6070 /* Debuginfo files do not have any allocated SHT_PROGBITS sections. 6071 The only allocated sections are SHT_NOBITS or SHT_NOTES. */ 6072 if ((header->sh_flags & SHF_ALLOC) == SHF_ALLOC 6073 && header->sh_type != SHT_NOBITS 6074 && header->sh_type != SHT_NOTE) 6075 return FALSE; 6076 } 6077 6078 return TRUE; 6079} 6080 6081/* Assign file positions for the other sections, except for compressed debugging 6082 and other sections assigned in _bfd_elf_assign_file_positions_for_non_load(). */ 6083 6084static bfd_boolean 6085assign_file_positions_for_non_load_sections (bfd *abfd, 6086 struct bfd_link_info *link_info) 6087{ 6088 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6089 Elf_Internal_Shdr **i_shdrpp; 6090 Elf_Internal_Shdr **hdrpp, **end_hdrpp; 6091 Elf_Internal_Phdr *phdrs; 6092 Elf_Internal_Phdr *p; 6093 struct elf_segment_map *m; 6094 file_ptr off; 6095 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 6096 6097 i_shdrpp = elf_elfsections (abfd); 6098 end_hdrpp = i_shdrpp + elf_numsections (abfd); 6099 off = elf_next_file_pos (abfd); 6100 for (hdrpp = i_shdrpp + 1; hdrpp < end_hdrpp; hdrpp++) 6101 { 6102 Elf_Internal_Shdr *hdr; 6103 6104 hdr = *hdrpp; 6105 if (hdr->bfd_section != NULL 6106 && (hdr->bfd_section->filepos != 0 6107 || (hdr->sh_type == SHT_NOBITS 6108 && hdr->contents == NULL))) 6109 BFD_ASSERT (hdr->sh_offset == hdr->bfd_section->filepos); 6110 else if ((hdr->sh_flags & SHF_ALLOC) != 0) 6111 { 6112 if (hdr->sh_size != 0 6113 /* PR 24717 - debuginfo files are known to be not strictly 6114 compliant with the ELF standard. In particular they often 6115 have .note.gnu.property sections that are outside of any 6116 loadable segment. This is not a problem for such files, 6117 so do not warn about them. */ 6118 && ! is_debuginfo_file (abfd)) 6119 _bfd_error_handler 6120 /* xgettext:c-format */ 6121 (_("%pB: warning: allocated section `%s' not in segment"), 6122 abfd, 6123 (hdr->bfd_section == NULL 6124 ? "*unknown*" 6125 : hdr->bfd_section->name)); 6126 /* We don't need to page align empty sections. */ 6127 if ((abfd->flags & D_PAGED) != 0 && hdr->sh_size != 0) 6128 off += vma_page_aligned_bias (hdr->sh_addr, off, 6129 bed->maxpagesize); 6130 else 6131 off += vma_page_aligned_bias (hdr->sh_addr, off, 6132 hdr->sh_addralign); 6133 off = _bfd_elf_assign_file_position_for_section (hdr, off, 6134 FALSE); 6135 } 6136 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 6137 && hdr->bfd_section == NULL) 6138 /* We don't know the offset of these sections yet: their size has 6139 not been decided. */ 6140 || (hdr->bfd_section != NULL 6141 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS 6142 || (bfd_section_is_ctf (hdr->bfd_section) 6143 && abfd->is_linker_output))) 6144 || hdr == i_shdrpp[elf_onesymtab (abfd)] 6145 || (elf_symtab_shndx_list (abfd) != NULL 6146 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) 6147 || hdr == i_shdrpp[elf_strtab_sec (abfd)] 6148 || hdr == i_shdrpp[elf_shstrtab_sec (abfd)]) 6149 hdr->sh_offset = -1; 6150 else 6151 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); 6152 } 6153 elf_next_file_pos (abfd) = off; 6154 6155 /* Now that we have set the section file positions, we can set up 6156 the file positions for the non PT_LOAD segments. */ 6157 phdrs = elf_tdata (abfd)->phdr; 6158 for (m = elf_seg_map (abfd), p = phdrs; m != NULL; m = m->next, p++) 6159 { 6160 if (p->p_type == PT_GNU_RELRO) 6161 { 6162 bfd_vma start, end; /* Bytes. */ 6163 bfd_boolean ok; 6164 6165 if (link_info != NULL) 6166 { 6167 /* During linking the range of the RELRO segment is passed 6168 in link_info. Note that there may be padding between 6169 relro_start and the first RELRO section. */ 6170 start = link_info->relro_start; 6171 end = link_info->relro_end; 6172 } 6173 else if (m->count != 0) 6174 { 6175 if (!m->p_size_valid) 6176 abort (); 6177 start = m->sections[0]->vma; 6178 end = start + m->p_size / opb; 6179 } 6180 else 6181 { 6182 start = 0; 6183 end = 0; 6184 } 6185 6186 ok = FALSE; 6187 if (start < end) 6188 { 6189 struct elf_segment_map *lm; 6190 const Elf_Internal_Phdr *lp; 6191 unsigned int i; 6192 6193 /* Find a LOAD segment containing a section in the RELRO 6194 segment. */ 6195 for (lm = elf_seg_map (abfd), lp = phdrs; 6196 lm != NULL; 6197 lm = lm->next, lp++) 6198 { 6199 if (lp->p_type == PT_LOAD 6200 && lm->count != 0 6201 && (lm->sections[lm->count - 1]->vma 6202 + (!IS_TBSS (lm->sections[lm->count - 1]) 6203 ? lm->sections[lm->count - 1]->size / opb 6204 : 0)) > start 6205 && lm->sections[0]->vma < end) 6206 break; 6207 } 6208 6209 if (lm != NULL) 6210 { 6211 /* Find the section starting the RELRO segment. */ 6212 for (i = 0; i < lm->count; i++) 6213 { 6214 asection *s = lm->sections[i]; 6215 if (s->vma >= start 6216 && s->vma < end 6217 && s->size != 0) 6218 break; 6219 } 6220 6221 if (i < lm->count) 6222 { 6223 p->p_vaddr = lm->sections[i]->vma * opb; 6224 p->p_paddr = lm->sections[i]->lma * opb; 6225 p->p_offset = lm->sections[i]->filepos; 6226 p->p_memsz = end * opb - p->p_vaddr; 6227 p->p_filesz = p->p_memsz; 6228 6229 /* The RELRO segment typically ends a few bytes 6230 into .got.plt but other layouts are possible. 6231 In cases where the end does not match any 6232 loaded section (for instance is in file 6233 padding), trim p_filesz back to correspond to 6234 the end of loaded section contents. */ 6235 if (p->p_filesz > lp->p_vaddr + lp->p_filesz - p->p_vaddr) 6236 p->p_filesz = lp->p_vaddr + lp->p_filesz - p->p_vaddr; 6237 6238 /* Preserve the alignment and flags if they are 6239 valid. The gold linker generates RW/4 for 6240 the PT_GNU_RELRO section. It is better for 6241 objcopy/strip to honor these attributes 6242 otherwise gdb will choke when using separate 6243 debug files. */ 6244 if (!m->p_align_valid) 6245 p->p_align = 1; 6246 if (!m->p_flags_valid) 6247 p->p_flags = PF_R; 6248 ok = TRUE; 6249 } 6250 } 6251 } 6252 if (link_info != NULL) 6253 BFD_ASSERT (ok); 6254 if (!ok) 6255 memset (p, 0, sizeof *p); 6256 } 6257 else if (p->p_type == PT_GNU_STACK) 6258 { 6259 if (m->p_size_valid) 6260 p->p_memsz = m->p_size; 6261 } 6262 else if (m->count != 0) 6263 { 6264 unsigned int i; 6265 6266 if (p->p_type != PT_LOAD 6267 && (p->p_type != PT_NOTE 6268 || bfd_get_format (abfd) != bfd_core)) 6269 { 6270 /* A user specified segment layout may include a PHDR 6271 segment that overlaps with a LOAD segment... */ 6272 if (p->p_type == PT_PHDR) 6273 { 6274 m->count = 0; 6275 continue; 6276 } 6277 6278 if (m->includes_filehdr || m->includes_phdrs) 6279 { 6280 /* PR 17512: file: 2195325e. */ 6281 _bfd_error_handler 6282 (_("%pB: error: non-load segment %d includes file header " 6283 "and/or program header"), 6284 abfd, (int) (p - phdrs)); 6285 return FALSE; 6286 } 6287 6288 p->p_filesz = 0; 6289 p->p_offset = m->sections[0]->filepos; 6290 for (i = m->count; i-- != 0;) 6291 { 6292 asection *sect = m->sections[i]; 6293 Elf_Internal_Shdr *hdr = &elf_section_data (sect)->this_hdr; 6294 if (hdr->sh_type != SHT_NOBITS) 6295 { 6296 p->p_filesz = (sect->filepos - m->sections[0]->filepos 6297 + hdr->sh_size); 6298 break; 6299 } 6300 } 6301 } 6302 } 6303 } 6304 6305 return TRUE; 6306} 6307 6308static elf_section_list * 6309find_section_in_list (unsigned int i, elf_section_list * list) 6310{ 6311 for (;list != NULL; list = list->next) 6312 if (list->ndx == i) 6313 break; 6314 return list; 6315} 6316 6317/* Work out the file positions of all the sections. This is called by 6318 _bfd_elf_compute_section_file_positions. All the section sizes and 6319 VMAs must be known before this is called. 6320 6321 Reloc sections come in two flavours: Those processed specially as 6322 "side-channel" data attached to a section to which they apply, and those that 6323 bfd doesn't process as relocations. The latter sort are stored in a normal 6324 bfd section by bfd_section_from_shdr. We don't consider the former sort 6325 here, unless they form part of the loadable image. Reloc sections not 6326 assigned here (and compressed debugging sections and CTF sections which 6327 nothing else in the file can rely upon) will be handled later by 6328 assign_file_positions_for_relocs. 6329 6330 We also don't set the positions of the .symtab and .strtab here. */ 6331 6332static bfd_boolean 6333assign_file_positions_except_relocs (bfd *abfd, 6334 struct bfd_link_info *link_info) 6335{ 6336 struct elf_obj_tdata *tdata = elf_tdata (abfd); 6337 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (abfd); 6338 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6339 unsigned int alloc; 6340 6341 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 6342 && bfd_get_format (abfd) != bfd_core) 6343 { 6344 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); 6345 unsigned int num_sec = elf_numsections (abfd); 6346 Elf_Internal_Shdr **hdrpp; 6347 unsigned int i; 6348 file_ptr off; 6349 6350 /* Start after the ELF header. */ 6351 off = i_ehdrp->e_ehsize; 6352 6353 /* We are not creating an executable, which means that we are 6354 not creating a program header, and that the actual order of 6355 the sections in the file is unimportant. */ 6356 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) 6357 { 6358 Elf_Internal_Shdr *hdr; 6359 6360 hdr = *hdrpp; 6361 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 6362 && hdr->bfd_section == NULL) 6363 /* Do not assign offsets for these sections yet: we don't know 6364 their sizes. */ 6365 || (hdr->bfd_section != NULL 6366 && (hdr->bfd_section->flags & SEC_ELF_COMPRESS 6367 || (bfd_section_is_ctf (hdr->bfd_section) 6368 && abfd->is_linker_output))) 6369 || i == elf_onesymtab (abfd) 6370 || (elf_symtab_shndx_list (abfd) != NULL 6371 && hdr == i_shdrpp[elf_symtab_shndx_list (abfd)->ndx]) 6372 || i == elf_strtab_sec (abfd) 6373 || i == elf_shstrtab_sec (abfd)) 6374 { 6375 hdr->sh_offset = -1; 6376 } 6377 else 6378 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); 6379 } 6380 6381 elf_next_file_pos (abfd) = off; 6382 elf_program_header_size (abfd) = 0; 6383 } 6384 else 6385 { 6386 /* Assign file positions for the loaded sections based on the 6387 assignment of sections to segments. */ 6388 if (!assign_file_positions_for_load_sections (abfd, link_info)) 6389 return FALSE; 6390 6391 /* And for non-load sections. */ 6392 if (!assign_file_positions_for_non_load_sections (abfd, link_info)) 6393 return FALSE; 6394 } 6395 6396 if (!(*bed->elf_backend_modify_headers) (abfd, link_info)) 6397 return FALSE; 6398 6399 /* Write out the program headers. */ 6400 alloc = i_ehdrp->e_phnum; 6401 if (alloc != 0) 6402 { 6403 if (bfd_seek (abfd, i_ehdrp->e_phoff, SEEK_SET) != 0 6404 || bed->s->write_out_phdrs (abfd, tdata->phdr, alloc) != 0) 6405 return FALSE; 6406 } 6407 6408 return TRUE; 6409} 6410 6411bfd_boolean 6412_bfd_elf_init_file_header (bfd *abfd, 6413 struct bfd_link_info *info ATTRIBUTE_UNUSED) 6414{ 6415 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form. */ 6416 struct elf_strtab_hash *shstrtab; 6417 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6418 6419 i_ehdrp = elf_elfheader (abfd); 6420 6421 shstrtab = _bfd_elf_strtab_init (); 6422 if (shstrtab == NULL) 6423 return FALSE; 6424 6425 elf_shstrtab (abfd) = shstrtab; 6426 6427 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; 6428 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; 6429 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; 6430 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; 6431 6432 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; 6433 i_ehdrp->e_ident[EI_DATA] = 6434 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; 6435 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; 6436 6437 if ((abfd->flags & DYNAMIC) != 0) 6438 i_ehdrp->e_type = ET_DYN; 6439 else if ((abfd->flags & EXEC_P) != 0) 6440 i_ehdrp->e_type = ET_EXEC; 6441 else if (bfd_get_format (abfd) == bfd_core) 6442 i_ehdrp->e_type = ET_CORE; 6443 else 6444 i_ehdrp->e_type = ET_REL; 6445 6446 switch (bfd_get_arch (abfd)) 6447 { 6448 case bfd_arch_unknown: 6449 i_ehdrp->e_machine = EM_NONE; 6450 break; 6451 6452 /* There used to be a long list of cases here, each one setting 6453 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE 6454 in the corresponding bfd definition. To avoid duplication, 6455 the switch was removed. Machines that need special handling 6456 can generally do it in elf_backend_final_write_processing(), 6457 unless they need the information earlier than the final write. 6458 Such need can generally be supplied by replacing the tests for 6459 e_machine with the conditions used to determine it. */ 6460 default: 6461 i_ehdrp->e_machine = bed->elf_machine_code; 6462 } 6463 6464 i_ehdrp->e_version = bed->s->ev_current; 6465 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; 6466 6467 /* No program header, for now. */ 6468 i_ehdrp->e_phoff = 0; 6469 i_ehdrp->e_phentsize = 0; 6470 i_ehdrp->e_phnum = 0; 6471 6472 /* Each bfd section is section header entry. */ 6473 i_ehdrp->e_entry = bfd_get_start_address (abfd); 6474 i_ehdrp->e_shentsize = bed->s->sizeof_shdr; 6475 6476 elf_tdata (abfd)->symtab_hdr.sh_name = 6477 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); 6478 elf_tdata (abfd)->strtab_hdr.sh_name = 6479 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); 6480 elf_tdata (abfd)->shstrtab_hdr.sh_name = 6481 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); 6482 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 6483 || elf_tdata (abfd)->strtab_hdr.sh_name == (unsigned int) -1 6484 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) 6485 return FALSE; 6486 6487 return TRUE; 6488} 6489 6490/* Set e_type in ELF header to ET_EXEC for -pie -Ttext-segment=. 6491 6492 FIXME: We used to have code here to sort the PT_LOAD segments into 6493 ascending order, as per the ELF spec. But this breaks some programs, 6494 including the Linux kernel. But really either the spec should be 6495 changed or the programs updated. */ 6496 6497bfd_boolean 6498_bfd_elf_modify_headers (bfd *obfd, struct bfd_link_info *link_info) 6499{ 6500 if (link_info != NULL && bfd_link_pie (link_info)) 6501 { 6502 Elf_Internal_Ehdr *i_ehdrp = elf_elfheader (obfd); 6503 unsigned int num_segments = i_ehdrp->e_phnum; 6504 struct elf_obj_tdata *tdata = elf_tdata (obfd); 6505 Elf_Internal_Phdr *segment = tdata->phdr; 6506 Elf_Internal_Phdr *end_segment = &segment[num_segments]; 6507 6508 /* Find the lowest p_vaddr in PT_LOAD segments. */ 6509 bfd_vma p_vaddr = (bfd_vma) -1; 6510 for (; segment < end_segment; segment++) 6511 if (segment->p_type == PT_LOAD && p_vaddr > segment->p_vaddr) 6512 p_vaddr = segment->p_vaddr; 6513 6514 /* Set e_type to ET_EXEC if the lowest p_vaddr in PT_LOAD 6515 segments is non-zero. */ 6516 if (p_vaddr) 6517 i_ehdrp->e_type = ET_EXEC; 6518 } 6519 return TRUE; 6520} 6521 6522/* Assign file positions for all the reloc sections which are not part 6523 of the loadable file image, and the file position of section headers. */ 6524 6525static bfd_boolean 6526_bfd_elf_assign_file_positions_for_non_load (bfd *abfd) 6527{ 6528 file_ptr off; 6529 Elf_Internal_Shdr **shdrpp, **end_shdrpp; 6530 Elf_Internal_Shdr *shdrp; 6531 Elf_Internal_Ehdr *i_ehdrp; 6532 const struct elf_backend_data *bed; 6533 6534 off = elf_next_file_pos (abfd); 6535 6536 shdrpp = elf_elfsections (abfd); 6537 end_shdrpp = shdrpp + elf_numsections (abfd); 6538 for (shdrpp++; shdrpp < end_shdrpp; shdrpp++) 6539 { 6540 shdrp = *shdrpp; 6541 if (shdrp->sh_offset == -1) 6542 { 6543 asection *sec = shdrp->bfd_section; 6544 bfd_boolean is_rel = (shdrp->sh_type == SHT_REL 6545 || shdrp->sh_type == SHT_RELA); 6546 bfd_boolean is_ctf = sec && bfd_section_is_ctf (sec); 6547 if (is_rel 6548 || is_ctf 6549 || (sec != NULL && (sec->flags & SEC_ELF_COMPRESS))) 6550 { 6551 if (!is_rel && !is_ctf) 6552 { 6553 const char *name = sec->name; 6554 struct bfd_elf_section_data *d; 6555 6556 /* Compress DWARF debug sections. */ 6557 if (!bfd_compress_section (abfd, sec, 6558 shdrp->contents)) 6559 return FALSE; 6560 6561 if (sec->compress_status == COMPRESS_SECTION_DONE 6562 && (abfd->flags & BFD_COMPRESS_GABI) == 0) 6563 { 6564 /* If section is compressed with zlib-gnu, convert 6565 section name from .debug_* to .zdebug_*. */ 6566 char *new_name 6567 = convert_debug_to_zdebug (abfd, name); 6568 if (new_name == NULL) 6569 return FALSE; 6570 name = new_name; 6571 } 6572 /* Add section name to section name section. */ 6573 if (shdrp->sh_name != (unsigned int) -1) 6574 abort (); 6575 shdrp->sh_name 6576 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 6577 name, FALSE); 6578 d = elf_section_data (sec); 6579 6580 /* Add reloc section name to section name section. */ 6581 if (d->rel.hdr 6582 && !_bfd_elf_set_reloc_sh_name (abfd, 6583 d->rel.hdr, 6584 name, FALSE)) 6585 return FALSE; 6586 if (d->rela.hdr 6587 && !_bfd_elf_set_reloc_sh_name (abfd, 6588 d->rela.hdr, 6589 name, TRUE)) 6590 return FALSE; 6591 6592 /* Update section size and contents. */ 6593 shdrp->sh_size = sec->size; 6594 shdrp->contents = sec->contents; 6595 shdrp->bfd_section->contents = NULL; 6596 } 6597 else if (is_ctf) 6598 { 6599 /* Update section size and contents. */ 6600 shdrp->sh_size = sec->size; 6601 shdrp->contents = sec->contents; 6602 } 6603 6604 off = _bfd_elf_assign_file_position_for_section (shdrp, 6605 off, 6606 TRUE); 6607 } 6608 } 6609 } 6610 6611 /* Place section name section after DWARF debug sections have been 6612 compressed. */ 6613 _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); 6614 shdrp = &elf_tdata (abfd)->shstrtab_hdr; 6615 shdrp->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); 6616 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); 6617 6618 /* Place the section headers. */ 6619 i_ehdrp = elf_elfheader (abfd); 6620 bed = get_elf_backend_data (abfd); 6621 off = align_file_position (off, 1 << bed->s->log_file_align); 6622 i_ehdrp->e_shoff = off; 6623 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; 6624 elf_next_file_pos (abfd) = off; 6625 6626 return TRUE; 6627} 6628 6629bfd_boolean 6630_bfd_elf_write_object_contents (bfd *abfd) 6631{ 6632 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6633 Elf_Internal_Shdr **i_shdrp; 6634 bfd_boolean failed; 6635 unsigned int count, num_sec; 6636 struct elf_obj_tdata *t; 6637 6638 if (! abfd->output_has_begun 6639 && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) 6640 return FALSE; 6641 /* Do not rewrite ELF data when the BFD has been opened for update. 6642 abfd->output_has_begun was set to TRUE on opening, so creation of new 6643 sections, and modification of existing section sizes was restricted. 6644 This means the ELF header, program headers and section headers can't have 6645 changed. 6646 If the contents of any sections has been modified, then those changes have 6647 already been written to the BFD. */ 6648 else if (abfd->direction == both_direction) 6649 { 6650 BFD_ASSERT (abfd->output_has_begun); 6651 return TRUE; 6652 } 6653 6654 i_shdrp = elf_elfsections (abfd); 6655 6656 failed = FALSE; 6657 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); 6658 if (failed) 6659 return FALSE; 6660 6661 if (!_bfd_elf_assign_file_positions_for_non_load (abfd)) 6662 return FALSE; 6663 6664 /* After writing the headers, we need to write the sections too... */ 6665 num_sec = elf_numsections (abfd); 6666 for (count = 1; count < num_sec; count++) 6667 { 6668 i_shdrp[count]->sh_name 6669 = _bfd_elf_strtab_offset (elf_shstrtab (abfd), 6670 i_shdrp[count]->sh_name); 6671 if (bed->elf_backend_section_processing) 6672 if (!(*bed->elf_backend_section_processing) (abfd, i_shdrp[count])) 6673 return FALSE; 6674 if (i_shdrp[count]->contents) 6675 { 6676 bfd_size_type amt = i_shdrp[count]->sh_size; 6677 6678 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 6679 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) 6680 return FALSE; 6681 } 6682 } 6683 6684 /* Write out the section header names. */ 6685 t = elf_tdata (abfd); 6686 if (elf_shstrtab (abfd) != NULL 6687 && (bfd_seek (abfd, t->shstrtab_hdr.sh_offset, SEEK_SET) != 0 6688 || !_bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) 6689 return FALSE; 6690 6691 if (!(*bed->elf_backend_final_write_processing) (abfd)) 6692 return FALSE; 6693 6694 if (!bed->s->write_shdrs_and_ehdr (abfd)) 6695 return FALSE; 6696 6697 /* This is last since write_shdrs_and_ehdr can touch i_shdrp[0]. */ 6698 if (t->o->build_id.after_write_object_contents != NULL) 6699 return (*t->o->build_id.after_write_object_contents) (abfd); 6700 6701 return TRUE; 6702} 6703 6704bfd_boolean 6705_bfd_elf_write_corefile_contents (bfd *abfd) 6706{ 6707 /* Hopefully this can be done just like an object file. */ 6708 return _bfd_elf_write_object_contents (abfd); 6709} 6710 6711/* Given a section, search the header to find them. */ 6712 6713unsigned int 6714_bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) 6715{ 6716 const struct elf_backend_data *bed; 6717 unsigned int sec_index; 6718 6719 if (elf_section_data (asect) != NULL 6720 && elf_section_data (asect)->this_idx != 0) 6721 return elf_section_data (asect)->this_idx; 6722 6723 if (bfd_is_abs_section (asect)) 6724 sec_index = SHN_ABS; 6725 else if (bfd_is_com_section (asect)) 6726 sec_index = SHN_COMMON; 6727 else if (bfd_is_und_section (asect)) 6728 sec_index = SHN_UNDEF; 6729 else 6730 sec_index = SHN_BAD; 6731 6732 bed = get_elf_backend_data (abfd); 6733 if (bed->elf_backend_section_from_bfd_section) 6734 { 6735 int retval = sec_index; 6736 6737 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) 6738 return retval; 6739 } 6740 6741 if (sec_index == SHN_BAD) 6742 bfd_set_error (bfd_error_nonrepresentable_section); 6743 6744 return sec_index; 6745} 6746 6747/* Given a BFD symbol, return the index in the ELF symbol table, or -1 6748 on error. */ 6749 6750int 6751_bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) 6752{ 6753 asymbol *asym_ptr = *asym_ptr_ptr; 6754 int idx; 6755 flagword flags = asym_ptr->flags; 6756 6757 /* When gas creates relocations against local labels, it creates its 6758 own symbol for the section, but does put the symbol into the 6759 symbol chain, so udata is 0. When the linker is generating 6760 relocatable output, this section symbol may be for one of the 6761 input sections rather than the output section. */ 6762 if (asym_ptr->udata.i == 0 6763 && (flags & BSF_SECTION_SYM) 6764 && asym_ptr->section) 6765 { 6766 asection *sec; 6767 int indx; 6768 6769 sec = asym_ptr->section; 6770 if (sec->owner != abfd && sec->output_section != NULL) 6771 sec = sec->output_section; 6772 if (sec->owner == abfd 6773 && (indx = sec->index) < elf_num_section_syms (abfd) 6774 && elf_section_syms (abfd)[indx] != NULL) 6775 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; 6776 } 6777 6778 idx = asym_ptr->udata.i; 6779 6780 if (idx == 0) 6781 { 6782 /* This case can occur when using --strip-symbol on a symbol 6783 which is used in a relocation entry. */ 6784 _bfd_error_handler 6785 /* xgettext:c-format */ 6786 (_("%pB: symbol `%s' required but not present"), 6787 abfd, bfd_asymbol_name (asym_ptr)); 6788 bfd_set_error (bfd_error_no_symbols); 6789 return -1; 6790 } 6791 6792#if DEBUG & 4 6793 { 6794 fprintf (stderr, 6795 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8x\n", 6796 (long) asym_ptr, asym_ptr->name, idx, flags); 6797 fflush (stderr); 6798 } 6799#endif 6800 6801 return idx; 6802} 6803 6804/* Rewrite program header information. */ 6805 6806static bfd_boolean 6807rewrite_elf_program_header (bfd *ibfd, bfd *obfd) 6808{ 6809 Elf_Internal_Ehdr *iehdr; 6810 struct elf_segment_map *map; 6811 struct elf_segment_map *map_first; 6812 struct elf_segment_map **pointer_to_map; 6813 Elf_Internal_Phdr *segment; 6814 asection *section; 6815 unsigned int i; 6816 unsigned int num_segments; 6817 bfd_boolean phdr_included = FALSE; 6818 bfd_boolean p_paddr_valid; 6819 bfd_vma maxpagesize; 6820 struct elf_segment_map *phdr_adjust_seg = NULL; 6821 unsigned int phdr_adjust_num = 0; 6822 const struct elf_backend_data *bed; 6823 unsigned int opb = bfd_octets_per_byte (ibfd, NULL); 6824 6825 bed = get_elf_backend_data (ibfd); 6826 iehdr = elf_elfheader (ibfd); 6827 6828 map_first = NULL; 6829 pointer_to_map = &map_first; 6830 6831 num_segments = elf_elfheader (ibfd)->e_phnum; 6832 maxpagesize = get_elf_backend_data (obfd)->maxpagesize; 6833 6834 /* Returns the end address of the segment + 1. */ 6835#define SEGMENT_END(segment, start) \ 6836 (start + (segment->p_memsz > segment->p_filesz \ 6837 ? segment->p_memsz : segment->p_filesz)) 6838 6839#define SECTION_SIZE(section, segment) \ 6840 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ 6841 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ 6842 ? section->size : 0) 6843 6844 /* Returns TRUE if the given section is contained within 6845 the given segment. VMA addresses are compared. */ 6846#define IS_CONTAINED_BY_VMA(section, segment, opb) \ 6847 (section->vma * (opb) >= segment->p_vaddr \ 6848 && (section->vma * (opb) + SECTION_SIZE (section, segment) \ 6849 <= (SEGMENT_END (segment, segment->p_vaddr)))) 6850 6851 /* Returns TRUE if the given section is contained within 6852 the given segment. LMA addresses are compared. */ 6853#define IS_CONTAINED_BY_LMA(section, segment, base, opb) \ 6854 (section->lma * (opb) >= base \ 6855 && (section->lma + SECTION_SIZE (section, segment) / (opb) >= section->lma) \ 6856 && (section->lma * (opb) + SECTION_SIZE (section, segment) \ 6857 <= SEGMENT_END (segment, base))) 6858 6859 /* Handle PT_NOTE segment. */ 6860#define IS_NOTE(p, s) \ 6861 (p->p_type == PT_NOTE \ 6862 && elf_section_type (s) == SHT_NOTE \ 6863 && (bfd_vma) s->filepos >= p->p_offset \ 6864 && ((bfd_vma) s->filepos + s->size \ 6865 <= p->p_offset + p->p_filesz)) 6866 6867 /* Special case: corefile "NOTE" section containing regs, prpsinfo 6868 etc. */ 6869#define IS_COREFILE_NOTE(p, s) \ 6870 (IS_NOTE (p, s) \ 6871 && bfd_get_format (ibfd) == bfd_core \ 6872 && s->vma == 0 \ 6873 && s->lma == 0) 6874 6875 /* The complicated case when p_vaddr is 0 is to handle the Solaris 6876 linker, which generates a PT_INTERP section with p_vaddr and 6877 p_memsz set to 0. */ 6878#define IS_SOLARIS_PT_INTERP(p, s) \ 6879 (p->p_vaddr == 0 \ 6880 && p->p_paddr == 0 \ 6881 && p->p_memsz == 0 \ 6882 && p->p_filesz > 0 \ 6883 && (s->flags & SEC_HAS_CONTENTS) != 0 \ 6884 && s->size > 0 \ 6885 && (bfd_vma) s->filepos >= p->p_offset \ 6886 && ((bfd_vma) s->filepos + s->size \ 6887 <= p->p_offset + p->p_filesz)) 6888 6889 /* Decide if the given section should be included in the given segment. 6890 A section will be included if: 6891 1. It is within the address space of the segment -- we use the LMA 6892 if that is set for the segment and the VMA otherwise, 6893 2. It is an allocated section or a NOTE section in a PT_NOTE 6894 segment. 6895 3. There is an output section associated with it, 6896 4. The section has not already been allocated to a previous segment. 6897 5. PT_GNU_STACK segments do not include any sections. 6898 6. PT_TLS segment includes only SHF_TLS sections. 6899 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. 6900 8. PT_DYNAMIC should not contain empty sections at the beginning 6901 (with the possible exception of .dynamic). */ 6902#define IS_SECTION_IN_INPUT_SEGMENT(section, segment, bed, opb) \ 6903 ((((segment->p_paddr \ 6904 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr, opb) \ 6905 : IS_CONTAINED_BY_VMA (section, segment, opb)) \ 6906 && (section->flags & SEC_ALLOC) != 0) \ 6907 || IS_NOTE (segment, section)) \ 6908 && segment->p_type != PT_GNU_STACK \ 6909 && (segment->p_type != PT_TLS \ 6910 || (section->flags & SEC_THREAD_LOCAL)) \ 6911 && (segment->p_type == PT_LOAD \ 6912 || segment->p_type == PT_TLS \ 6913 || (section->flags & SEC_THREAD_LOCAL) == 0) \ 6914 && (segment->p_type != PT_DYNAMIC \ 6915 || SECTION_SIZE (section, segment) > 0 \ 6916 || (segment->p_paddr \ 6917 ? segment->p_paddr != section->lma * (opb) \ 6918 : segment->p_vaddr != section->vma * (opb)) \ 6919 || (strcmp (bfd_section_name (section), ".dynamic") == 0)) \ 6920 && (segment->p_type != PT_LOAD || !section->segment_mark)) 6921 6922/* If the output section of a section in the input segment is NULL, 6923 it is removed from the corresponding output segment. */ 6924#define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed, opb) \ 6925 (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb) \ 6926 && section->output_section != NULL) 6927 6928 /* Returns TRUE iff seg1 starts after the end of seg2. */ 6929#define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ 6930 (seg1->field >= SEGMENT_END (seg2, seg2->field)) 6931 6932 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both 6933 their VMA address ranges and their LMA address ranges overlap. 6934 It is possible to have overlapping VMA ranges without overlapping LMA 6935 ranges. RedBoot images for example can have both .data and .bss mapped 6936 to the same VMA range, but with the .data section mapped to a different 6937 LMA. */ 6938#define SEGMENT_OVERLAPS(seg1, seg2) \ 6939 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ 6940 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ 6941 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ 6942 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) 6943 6944 /* Initialise the segment mark field. */ 6945 for (section = ibfd->sections; section != NULL; section = section->next) 6946 section->segment_mark = FALSE; 6947 6948 /* The Solaris linker creates program headers in which all the 6949 p_paddr fields are zero. When we try to objcopy or strip such a 6950 file, we get confused. Check for this case, and if we find it 6951 don't set the p_paddr_valid fields. */ 6952 p_paddr_valid = FALSE; 6953 for (i = 0, segment = elf_tdata (ibfd)->phdr; 6954 i < num_segments; 6955 i++, segment++) 6956 if (segment->p_paddr != 0) 6957 { 6958 p_paddr_valid = TRUE; 6959 break; 6960 } 6961 6962 /* Scan through the segments specified in the program header 6963 of the input BFD. For this first scan we look for overlaps 6964 in the loadable segments. These can be created by weird 6965 parameters to objcopy. Also, fix some solaris weirdness. */ 6966 for (i = 0, segment = elf_tdata (ibfd)->phdr; 6967 i < num_segments; 6968 i++, segment++) 6969 { 6970 unsigned int j; 6971 Elf_Internal_Phdr *segment2; 6972 6973 if (segment->p_type == PT_INTERP) 6974 for (section = ibfd->sections; section; section = section->next) 6975 if (IS_SOLARIS_PT_INTERP (segment, section)) 6976 { 6977 /* Mininal change so that the normal section to segment 6978 assignment code will work. */ 6979 segment->p_vaddr = section->vma * opb; 6980 break; 6981 } 6982 6983 if (segment->p_type != PT_LOAD) 6984 { 6985 /* Remove PT_GNU_RELRO segment. */ 6986 if (segment->p_type == PT_GNU_RELRO) 6987 segment->p_type = PT_NULL; 6988 continue; 6989 } 6990 6991 /* Determine if this segment overlaps any previous segments. */ 6992 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2++) 6993 { 6994 bfd_signed_vma extra_length; 6995 6996 if (segment2->p_type != PT_LOAD 6997 || !SEGMENT_OVERLAPS (segment, segment2)) 6998 continue; 6999 7000 /* Merge the two segments together. */ 7001 if (segment2->p_vaddr < segment->p_vaddr) 7002 { 7003 /* Extend SEGMENT2 to include SEGMENT and then delete 7004 SEGMENT. */ 7005 extra_length = (SEGMENT_END (segment, segment->p_vaddr) 7006 - SEGMENT_END (segment2, segment2->p_vaddr)); 7007 7008 if (extra_length > 0) 7009 { 7010 segment2->p_memsz += extra_length; 7011 segment2->p_filesz += extra_length; 7012 } 7013 7014 segment->p_type = PT_NULL; 7015 7016 /* Since we have deleted P we must restart the outer loop. */ 7017 i = 0; 7018 segment = elf_tdata (ibfd)->phdr; 7019 break; 7020 } 7021 else 7022 { 7023 /* Extend SEGMENT to include SEGMENT2 and then delete 7024 SEGMENT2. */ 7025 extra_length = (SEGMENT_END (segment2, segment2->p_vaddr) 7026 - SEGMENT_END (segment, segment->p_vaddr)); 7027 7028 if (extra_length > 0) 7029 { 7030 segment->p_memsz += extra_length; 7031 segment->p_filesz += extra_length; 7032 } 7033 7034 segment2->p_type = PT_NULL; 7035 } 7036 } 7037 } 7038 7039 /* The second scan attempts to assign sections to segments. */ 7040 for (i = 0, segment = elf_tdata (ibfd)->phdr; 7041 i < num_segments; 7042 i++, segment++) 7043 { 7044 unsigned int section_count; 7045 asection **sections; 7046 asection *output_section; 7047 unsigned int isec; 7048 asection *matching_lma; 7049 asection *suggested_lma; 7050 unsigned int j; 7051 size_t amt; 7052 asection *first_section; 7053 7054 if (segment->p_type == PT_NULL) 7055 continue; 7056 7057 first_section = NULL; 7058 /* Compute how many sections might be placed into this segment. */ 7059 for (section = ibfd->sections, section_count = 0; 7060 section != NULL; 7061 section = section->next) 7062 { 7063 /* Find the first section in the input segment, which may be 7064 removed from the corresponding output segment. */ 7065 if (IS_SECTION_IN_INPUT_SEGMENT (section, segment, bed, opb)) 7066 { 7067 if (first_section == NULL) 7068 first_section = section; 7069 if (section->output_section != NULL) 7070 ++section_count; 7071 } 7072 } 7073 7074 /* Allocate a segment map big enough to contain 7075 all of the sections we have selected. */ 7076 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 7077 amt += section_count * sizeof (asection *); 7078 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 7079 if (map == NULL) 7080 return FALSE; 7081 7082 /* Initialise the fields of the segment map. Default to 7083 using the physical address of the segment in the input BFD. */ 7084 map->next = NULL; 7085 map->p_type = segment->p_type; 7086 map->p_flags = segment->p_flags; 7087 map->p_flags_valid = 1; 7088 7089 /* If the first section in the input segment is removed, there is 7090 no need to preserve segment physical address in the corresponding 7091 output segment. */ 7092 if (!first_section || first_section->output_section != NULL) 7093 { 7094 map->p_paddr = segment->p_paddr; 7095 map->p_paddr_valid = p_paddr_valid; 7096 } 7097 7098 /* Determine if this segment contains the ELF file header 7099 and if it contains the program headers themselves. */ 7100 map->includes_filehdr = (segment->p_offset == 0 7101 && segment->p_filesz >= iehdr->e_ehsize); 7102 map->includes_phdrs = 0; 7103 7104 if (!phdr_included || segment->p_type != PT_LOAD) 7105 { 7106 map->includes_phdrs = 7107 (segment->p_offset <= (bfd_vma) iehdr->e_phoff 7108 && (segment->p_offset + segment->p_filesz 7109 >= ((bfd_vma) iehdr->e_phoff 7110 + iehdr->e_phnum * iehdr->e_phentsize))); 7111 7112 if (segment->p_type == PT_LOAD && map->includes_phdrs) 7113 phdr_included = TRUE; 7114 } 7115 7116 if (section_count == 0) 7117 { 7118 /* Special segments, such as the PT_PHDR segment, may contain 7119 no sections, but ordinary, loadable segments should contain 7120 something. They are allowed by the ELF spec however, so only 7121 a warning is produced. 7122 There is however the valid use case of embedded systems which 7123 have segments with p_filesz of 0 and a p_memsz > 0 to initialize 7124 flash memory with zeros. No warning is shown for that case. */ 7125 if (segment->p_type == PT_LOAD 7126 && (segment->p_filesz > 0 || segment->p_memsz == 0)) 7127 /* xgettext:c-format */ 7128 _bfd_error_handler 7129 (_("%pB: warning: empty loadable segment detected" 7130 " at vaddr=%#" PRIx64 ", is this intentional?"), 7131 ibfd, (uint64_t) segment->p_vaddr); 7132 7133 map->p_vaddr_offset = segment->p_vaddr / opb; 7134 map->count = 0; 7135 *pointer_to_map = map; 7136 pointer_to_map = &map->next; 7137 7138 continue; 7139 } 7140 7141 /* Now scan the sections in the input BFD again and attempt 7142 to add their corresponding output sections to the segment map. 7143 The problem here is how to handle an output section which has 7144 been moved (ie had its LMA changed). There are four possibilities: 7145 7146 1. None of the sections have been moved. 7147 In this case we can continue to use the segment LMA from the 7148 input BFD. 7149 7150 2. All of the sections have been moved by the same amount. 7151 In this case we can change the segment's LMA to match the LMA 7152 of the first section. 7153 7154 3. Some of the sections have been moved, others have not. 7155 In this case those sections which have not been moved can be 7156 placed in the current segment which will have to have its size, 7157 and possibly its LMA changed, and a new segment or segments will 7158 have to be created to contain the other sections. 7159 7160 4. The sections have been moved, but not by the same amount. 7161 In this case we can change the segment's LMA to match the LMA 7162 of the first section and we will have to create a new segment 7163 or segments to contain the other sections. 7164 7165 In order to save time, we allocate an array to hold the section 7166 pointers that we are interested in. As these sections get assigned 7167 to a segment, they are removed from this array. */ 7168 7169 amt = section_count * sizeof (asection *); 7170 sections = (asection **) bfd_malloc (amt); 7171 if (sections == NULL) 7172 return FALSE; 7173 7174 /* Step One: Scan for segment vs section LMA conflicts. 7175 Also add the sections to the section array allocated above. 7176 Also add the sections to the current segment. In the common 7177 case, where the sections have not been moved, this means that 7178 we have completely filled the segment, and there is nothing 7179 more to do. */ 7180 isec = 0; 7181 matching_lma = NULL; 7182 suggested_lma = NULL; 7183 7184 for (section = first_section, j = 0; 7185 section != NULL; 7186 section = section->next) 7187 { 7188 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed, opb)) 7189 { 7190 output_section = section->output_section; 7191 7192 sections[j++] = section; 7193 7194 /* The Solaris native linker always sets p_paddr to 0. 7195 We try to catch that case here, and set it to the 7196 correct value. Note - some backends require that 7197 p_paddr be left as zero. */ 7198 if (!p_paddr_valid 7199 && segment->p_vaddr != 0 7200 && !bed->want_p_paddr_set_to_zero 7201 && isec == 0 7202 && output_section->lma != 0 7203 && (align_power (segment->p_vaddr 7204 + (map->includes_filehdr 7205 ? iehdr->e_ehsize : 0) 7206 + (map->includes_phdrs 7207 ? iehdr->e_phnum * iehdr->e_phentsize 7208 : 0), 7209 output_section->alignment_power * opb) 7210 == (output_section->vma * opb))) 7211 map->p_paddr = segment->p_vaddr; 7212 7213 /* Match up the physical address of the segment with the 7214 LMA address of the output section. */ 7215 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr, 7216 opb) 7217 || IS_COREFILE_NOTE (segment, section) 7218 || (bed->want_p_paddr_set_to_zero 7219 && IS_CONTAINED_BY_VMA (output_section, segment, opb))) 7220 { 7221 if (matching_lma == NULL 7222 || output_section->lma < matching_lma->lma) 7223 matching_lma = output_section; 7224 7225 /* We assume that if the section fits within the segment 7226 then it does not overlap any other section within that 7227 segment. */ 7228 map->sections[isec++] = output_section; 7229 } 7230 else if (suggested_lma == NULL) 7231 suggested_lma = output_section; 7232 7233 if (j == section_count) 7234 break; 7235 } 7236 } 7237 7238 BFD_ASSERT (j == section_count); 7239 7240 /* Step Two: Adjust the physical address of the current segment, 7241 if necessary. */ 7242 if (isec == section_count) 7243 { 7244 /* All of the sections fitted within the segment as currently 7245 specified. This is the default case. Add the segment to 7246 the list of built segments and carry on to process the next 7247 program header in the input BFD. */ 7248 map->count = section_count; 7249 *pointer_to_map = map; 7250 pointer_to_map = &map->next; 7251 7252 if (p_paddr_valid 7253 && !bed->want_p_paddr_set_to_zero) 7254 { 7255 bfd_vma hdr_size = 0; 7256 if (map->includes_filehdr) 7257 hdr_size = iehdr->e_ehsize; 7258 if (map->includes_phdrs) 7259 hdr_size += iehdr->e_phnum * iehdr->e_phentsize; 7260 7261 /* Account for padding before the first section in the 7262 segment. */ 7263 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb 7264 - matching_lma->lma); 7265 } 7266 7267 free (sections); 7268 continue; 7269 } 7270 else 7271 { 7272 /* Change the current segment's physical address to match 7273 the LMA of the first section that fitted, or if no 7274 section fitted, the first section. */ 7275 if (matching_lma == NULL) 7276 matching_lma = suggested_lma; 7277 7278 map->p_paddr = matching_lma->lma * opb; 7279 7280 /* Offset the segment physical address from the lma 7281 to allow for space taken up by elf headers. */ 7282 if (map->includes_phdrs) 7283 { 7284 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; 7285 7286 /* iehdr->e_phnum is just an estimate of the number 7287 of program headers that we will need. Make a note 7288 here of the number we used and the segment we chose 7289 to hold these headers, so that we can adjust the 7290 offset when we know the correct value. */ 7291 phdr_adjust_num = iehdr->e_phnum; 7292 phdr_adjust_seg = map; 7293 } 7294 7295 if (map->includes_filehdr) 7296 { 7297 bfd_vma align = (bfd_vma) 1 << matching_lma->alignment_power; 7298 map->p_paddr -= iehdr->e_ehsize; 7299 /* We've subtracted off the size of headers from the 7300 first section lma, but there may have been some 7301 alignment padding before that section too. Try to 7302 account for that by adjusting the segment lma down to 7303 the same alignment. */ 7304 if (segment->p_align != 0 && segment->p_align < align) 7305 align = segment->p_align; 7306 map->p_paddr &= -(align * opb); 7307 } 7308 } 7309 7310 /* Step Three: Loop over the sections again, this time assigning 7311 those that fit to the current segment and removing them from the 7312 sections array; but making sure not to leave large gaps. Once all 7313 possible sections have been assigned to the current segment it is 7314 added to the list of built segments and if sections still remain 7315 to be assigned, a new segment is constructed before repeating 7316 the loop. */ 7317 isec = 0; 7318 do 7319 { 7320 map->count = 0; 7321 suggested_lma = NULL; 7322 7323 /* Fill the current segment with sections that fit. */ 7324 for (j = 0; j < section_count; j++) 7325 { 7326 section = sections[j]; 7327 7328 if (section == NULL) 7329 continue; 7330 7331 output_section = section->output_section; 7332 7333 BFD_ASSERT (output_section != NULL); 7334 7335 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr, 7336 opb) 7337 || IS_COREFILE_NOTE (segment, section)) 7338 { 7339 if (map->count == 0) 7340 { 7341 /* If the first section in a segment does not start at 7342 the beginning of the segment, then something is 7343 wrong. */ 7344 if (align_power (map->p_paddr 7345 + (map->includes_filehdr 7346 ? iehdr->e_ehsize : 0) 7347 + (map->includes_phdrs 7348 ? iehdr->e_phnum * iehdr->e_phentsize 7349 : 0), 7350 output_section->alignment_power * opb) 7351 != output_section->lma * opb) 7352 goto sorry; 7353 } 7354 else 7355 { 7356 asection *prev_sec; 7357 7358 prev_sec = map->sections[map->count - 1]; 7359 7360 /* If the gap between the end of the previous section 7361 and the start of this section is more than 7362 maxpagesize then we need to start a new segment. */ 7363 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, 7364 maxpagesize) 7365 < BFD_ALIGN (output_section->lma, maxpagesize)) 7366 || (prev_sec->lma + prev_sec->size 7367 > output_section->lma)) 7368 { 7369 if (suggested_lma == NULL) 7370 suggested_lma = output_section; 7371 7372 continue; 7373 } 7374 } 7375 7376 map->sections[map->count++] = output_section; 7377 ++isec; 7378 sections[j] = NULL; 7379 if (segment->p_type == PT_LOAD) 7380 section->segment_mark = TRUE; 7381 } 7382 else if (suggested_lma == NULL) 7383 suggested_lma = output_section; 7384 } 7385 7386 /* PR 23932. A corrupt input file may contain sections that cannot 7387 be assigned to any segment - because for example they have a 7388 negative size - or segments that do not contain any sections. 7389 But there are also valid reasons why a segment can be empty. 7390 So allow a count of zero. */ 7391 7392 /* Add the current segment to the list of built segments. */ 7393 *pointer_to_map = map; 7394 pointer_to_map = &map->next; 7395 7396 if (isec < section_count) 7397 { 7398 /* We still have not allocated all of the sections to 7399 segments. Create a new segment here, initialise it 7400 and carry on looping. */ 7401 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 7402 amt += section_count * sizeof (asection *); 7403 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 7404 if (map == NULL) 7405 { 7406 free (sections); 7407 return FALSE; 7408 } 7409 7410 /* Initialise the fields of the segment map. Set the physical 7411 physical address to the LMA of the first section that has 7412 not yet been assigned. */ 7413 map->next = NULL; 7414 map->p_type = segment->p_type; 7415 map->p_flags = segment->p_flags; 7416 map->p_flags_valid = 1; 7417 map->p_paddr = suggested_lma->lma * opb; 7418 map->p_paddr_valid = p_paddr_valid; 7419 map->includes_filehdr = 0; 7420 map->includes_phdrs = 0; 7421 } 7422 7423 continue; 7424 sorry: 7425 bfd_set_error (bfd_error_sorry); 7426 free (sections); 7427 return FALSE; 7428 } 7429 while (isec < section_count); 7430 7431 free (sections); 7432 } 7433 7434 elf_seg_map (obfd) = map_first; 7435 7436 /* If we had to estimate the number of program headers that were 7437 going to be needed, then check our estimate now and adjust 7438 the offset if necessary. */ 7439 if (phdr_adjust_seg != NULL) 7440 { 7441 unsigned int count; 7442 7443 for (count = 0, map = map_first; map != NULL; map = map->next) 7444 count++; 7445 7446 if (count > phdr_adjust_num) 7447 phdr_adjust_seg->p_paddr 7448 -= (count - phdr_adjust_num) * iehdr->e_phentsize; 7449 7450 for (map = map_first; map != NULL; map = map->next) 7451 if (map->p_type == PT_PHDR) 7452 { 7453 bfd_vma adjust 7454 = phdr_adjust_seg->includes_filehdr ? iehdr->e_ehsize : 0; 7455 map->p_paddr = phdr_adjust_seg->p_paddr + adjust; 7456 break; 7457 } 7458 } 7459 7460#undef SEGMENT_END 7461#undef SECTION_SIZE 7462#undef IS_CONTAINED_BY_VMA 7463#undef IS_CONTAINED_BY_LMA 7464#undef IS_NOTE 7465#undef IS_COREFILE_NOTE 7466#undef IS_SOLARIS_PT_INTERP 7467#undef IS_SECTION_IN_INPUT_SEGMENT 7468#undef INCLUDE_SECTION_IN_SEGMENT 7469#undef SEGMENT_AFTER_SEGMENT 7470#undef SEGMENT_OVERLAPS 7471 return TRUE; 7472} 7473 7474/* Copy ELF program header information. */ 7475 7476static bfd_boolean 7477copy_elf_program_header (bfd *ibfd, bfd *obfd) 7478{ 7479 Elf_Internal_Ehdr *iehdr; 7480 struct elf_segment_map *map; 7481 struct elf_segment_map *map_first; 7482 struct elf_segment_map **pointer_to_map; 7483 Elf_Internal_Phdr *segment; 7484 unsigned int i; 7485 unsigned int num_segments; 7486 bfd_boolean phdr_included = FALSE; 7487 bfd_boolean p_paddr_valid; 7488 unsigned int opb = bfd_octets_per_byte (ibfd, NULL); 7489 7490 iehdr = elf_elfheader (ibfd); 7491 7492 map_first = NULL; 7493 pointer_to_map = &map_first; 7494 7495 /* If all the segment p_paddr fields are zero, don't set 7496 map->p_paddr_valid. */ 7497 p_paddr_valid = FALSE; 7498 num_segments = elf_elfheader (ibfd)->e_phnum; 7499 for (i = 0, segment = elf_tdata (ibfd)->phdr; 7500 i < num_segments; 7501 i++, segment++) 7502 if (segment->p_paddr != 0) 7503 { 7504 p_paddr_valid = TRUE; 7505 break; 7506 } 7507 7508 for (i = 0, segment = elf_tdata (ibfd)->phdr; 7509 i < num_segments; 7510 i++, segment++) 7511 { 7512 asection *section; 7513 unsigned int section_count; 7514 size_t amt; 7515 Elf_Internal_Shdr *this_hdr; 7516 asection *first_section = NULL; 7517 asection *lowest_section; 7518 7519 /* Compute how many sections are in this segment. */ 7520 for (section = ibfd->sections, section_count = 0; 7521 section != NULL; 7522 section = section->next) 7523 { 7524 this_hdr = &(elf_section_data(section)->this_hdr); 7525 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 7526 { 7527 if (first_section == NULL) 7528 first_section = section; 7529 section_count++; 7530 } 7531 } 7532 7533 /* Allocate a segment map big enough to contain 7534 all of the sections we have selected. */ 7535 amt = sizeof (struct elf_segment_map) - sizeof (asection *); 7536 amt += section_count * sizeof (asection *); 7537 map = (struct elf_segment_map *) bfd_zalloc (obfd, amt); 7538 if (map == NULL) 7539 return FALSE; 7540 7541 /* Initialize the fields of the output segment map with the 7542 input segment. */ 7543 map->next = NULL; 7544 map->p_type = segment->p_type; 7545 map->p_flags = segment->p_flags; 7546 map->p_flags_valid = 1; 7547 map->p_paddr = segment->p_paddr; 7548 map->p_paddr_valid = p_paddr_valid; 7549 map->p_align = segment->p_align; 7550 map->p_align_valid = 1; 7551 map->p_vaddr_offset = 0; 7552 7553 if (map->p_type == PT_GNU_RELRO 7554 || map->p_type == PT_GNU_STACK) 7555 { 7556 /* The PT_GNU_RELRO segment may contain the first a few 7557 bytes in the .got.plt section even if the whole .got.plt 7558 section isn't in the PT_GNU_RELRO segment. We won't 7559 change the size of the PT_GNU_RELRO segment. 7560 Similarly, PT_GNU_STACK size is significant on uclinux 7561 systems. */ 7562 map->p_size = segment->p_memsz; 7563 map->p_size_valid = 1; 7564 } 7565 7566 /* Determine if this segment contains the ELF file header 7567 and if it contains the program headers themselves. */ 7568 map->includes_filehdr = (segment->p_offset == 0 7569 && segment->p_filesz >= iehdr->e_ehsize); 7570 7571 map->includes_phdrs = 0; 7572 if (! phdr_included || segment->p_type != PT_LOAD) 7573 { 7574 map->includes_phdrs = 7575 (segment->p_offset <= (bfd_vma) iehdr->e_phoff 7576 && (segment->p_offset + segment->p_filesz 7577 >= ((bfd_vma) iehdr->e_phoff 7578 + iehdr->e_phnum * iehdr->e_phentsize))); 7579 7580 if (segment->p_type == PT_LOAD && map->includes_phdrs) 7581 phdr_included = TRUE; 7582 } 7583 7584 lowest_section = NULL; 7585 if (section_count != 0) 7586 { 7587 unsigned int isec = 0; 7588 7589 for (section = first_section; 7590 section != NULL; 7591 section = section->next) 7592 { 7593 this_hdr = &(elf_section_data(section)->this_hdr); 7594 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 7595 { 7596 map->sections[isec++] = section->output_section; 7597 if ((section->flags & SEC_ALLOC) != 0) 7598 { 7599 bfd_vma seg_off; 7600 7601 if (lowest_section == NULL 7602 || section->lma < lowest_section->lma) 7603 lowest_section = section; 7604 7605 /* Section lmas are set up from PT_LOAD header 7606 p_paddr in _bfd_elf_make_section_from_shdr. 7607 If this header has a p_paddr that disagrees 7608 with the section lma, flag the p_paddr as 7609 invalid. */ 7610 if ((section->flags & SEC_LOAD) != 0) 7611 seg_off = this_hdr->sh_offset - segment->p_offset; 7612 else 7613 seg_off = this_hdr->sh_addr - segment->p_vaddr; 7614 if (section->lma * opb - segment->p_paddr != seg_off) 7615 map->p_paddr_valid = FALSE; 7616 } 7617 if (isec == section_count) 7618 break; 7619 } 7620 } 7621 } 7622 7623 if (section_count == 0) 7624 map->p_vaddr_offset = segment->p_vaddr / opb; 7625 else if (map->p_paddr_valid) 7626 { 7627 /* Account for padding before the first section in the segment. */ 7628 bfd_vma hdr_size = 0; 7629 if (map->includes_filehdr) 7630 hdr_size = iehdr->e_ehsize; 7631 if (map->includes_phdrs) 7632 hdr_size += iehdr->e_phnum * iehdr->e_phentsize; 7633 7634 map->p_vaddr_offset = ((map->p_paddr + hdr_size) / opb 7635 - (lowest_section ? lowest_section->lma : 0)); 7636 } 7637 7638 map->count = section_count; 7639 *pointer_to_map = map; 7640 pointer_to_map = &map->next; 7641 } 7642 7643 elf_seg_map (obfd) = map_first; 7644 return TRUE; 7645} 7646 7647/* Copy private BFD data. This copies or rewrites ELF program header 7648 information. */ 7649 7650static bfd_boolean 7651copy_private_bfd_data (bfd *ibfd, bfd *obfd) 7652{ 7653 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 7654 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 7655 return TRUE; 7656 7657 if (elf_tdata (ibfd)->phdr == NULL) 7658 return TRUE; 7659 7660 if (ibfd->xvec == obfd->xvec) 7661 { 7662 /* Check to see if any sections in the input BFD 7663 covered by ELF program header have changed. */ 7664 Elf_Internal_Phdr *segment; 7665 asection *section, *osec; 7666 unsigned int i, num_segments; 7667 Elf_Internal_Shdr *this_hdr; 7668 const struct elf_backend_data *bed; 7669 7670 bed = get_elf_backend_data (ibfd); 7671 7672 /* Regenerate the segment map if p_paddr is set to 0. */ 7673 if (bed->want_p_paddr_set_to_zero) 7674 goto rewrite; 7675 7676 /* Initialize the segment mark field. */ 7677 for (section = obfd->sections; section != NULL; 7678 section = section->next) 7679 section->segment_mark = FALSE; 7680 7681 num_segments = elf_elfheader (ibfd)->e_phnum; 7682 for (i = 0, segment = elf_tdata (ibfd)->phdr; 7683 i < num_segments; 7684 i++, segment++) 7685 { 7686 /* PR binutils/3535. The Solaris linker always sets the p_paddr 7687 and p_memsz fields of special segments (DYNAMIC, INTERP) to 0 7688 which severly confuses things, so always regenerate the segment 7689 map in this case. */ 7690 if (segment->p_paddr == 0 7691 && segment->p_memsz == 0 7692 && (segment->p_type == PT_INTERP || segment->p_type == PT_DYNAMIC)) 7693 goto rewrite; 7694 7695 for (section = ibfd->sections; 7696 section != NULL; section = section->next) 7697 { 7698 /* We mark the output section so that we know it comes 7699 from the input BFD. */ 7700 osec = section->output_section; 7701 if (osec) 7702 osec->segment_mark = TRUE; 7703 7704 /* Check if this section is covered by the segment. */ 7705 this_hdr = &(elf_section_data(section)->this_hdr); 7706 if (ELF_SECTION_IN_SEGMENT (this_hdr, segment)) 7707 { 7708 /* FIXME: Check if its output section is changed or 7709 removed. What else do we need to check? */ 7710 if (osec == NULL 7711 || section->flags != osec->flags 7712 || section->lma != osec->lma 7713 || section->vma != osec->vma 7714 || section->size != osec->size 7715 || section->rawsize != osec->rawsize 7716 || section->alignment_power != osec->alignment_power) 7717 goto rewrite; 7718 } 7719 } 7720 } 7721 7722 /* Check to see if any output section do not come from the 7723 input BFD. */ 7724 for (section = obfd->sections; section != NULL; 7725 section = section->next) 7726 { 7727 if (!section->segment_mark) 7728 goto rewrite; 7729 else 7730 section->segment_mark = FALSE; 7731 } 7732 7733 return copy_elf_program_header (ibfd, obfd); 7734 } 7735 7736 rewrite: 7737 if (ibfd->xvec == obfd->xvec) 7738 { 7739 /* When rewriting program header, set the output maxpagesize to 7740 the maximum alignment of input PT_LOAD segments. */ 7741 Elf_Internal_Phdr *segment; 7742 unsigned int i; 7743 unsigned int num_segments = elf_elfheader (ibfd)->e_phnum; 7744 bfd_vma maxpagesize = 0; 7745 7746 for (i = 0, segment = elf_tdata (ibfd)->phdr; 7747 i < num_segments; 7748 i++, segment++) 7749 if (segment->p_type == PT_LOAD 7750 && maxpagesize < segment->p_align) 7751 { 7752 /* PR 17512: file: f17299af. */ 7753 if (segment->p_align > (bfd_vma) 1 << ((sizeof (bfd_vma) * 8) - 2)) 7754 /* xgettext:c-format */ 7755 _bfd_error_handler (_("%pB: warning: segment alignment of %#" 7756 PRIx64 " is too large"), 7757 ibfd, (uint64_t) segment->p_align); 7758 else 7759 maxpagesize = segment->p_align; 7760 } 7761 7762 if (maxpagesize != get_elf_backend_data (obfd)->maxpagesize) 7763 bfd_emul_set_maxpagesize (bfd_get_target (obfd), maxpagesize); 7764 } 7765 7766 return rewrite_elf_program_header (ibfd, obfd); 7767} 7768 7769/* Initialize private output section information from input section. */ 7770 7771bfd_boolean 7772_bfd_elf_init_private_section_data (bfd *ibfd, 7773 asection *isec, 7774 bfd *obfd, 7775 asection *osec, 7776 struct bfd_link_info *link_info) 7777 7778{ 7779 Elf_Internal_Shdr *ihdr, *ohdr; 7780 bfd_boolean final_link = (link_info != NULL 7781 && !bfd_link_relocatable (link_info)); 7782 7783 if (ibfd->xvec->flavour != bfd_target_elf_flavour 7784 || obfd->xvec->flavour != bfd_target_elf_flavour) 7785 return TRUE; 7786 7787 BFD_ASSERT (elf_section_data (osec) != NULL); 7788 7789 /* If this is a known ABI section, ELF section type and flags may 7790 have been set up when OSEC was created. For normal sections we 7791 allow the user to override the type and flags other than 7792 SHF_MASKOS and SHF_MASKPROC. */ 7793 if (elf_section_type (osec) == SHT_PROGBITS 7794 || elf_section_type (osec) == SHT_NOTE 7795 || elf_section_type (osec) == SHT_NOBITS) 7796 elf_section_type (osec) = SHT_NULL; 7797 /* For objcopy and relocatable link, copy the ELF section type from 7798 the input file if the BFD section flags are the same. (If they 7799 are different the user may be doing something like 7800 "objcopy --set-section-flags .text=alloc,data".) For a final 7801 link allow some flags that the linker clears to differ. */ 7802 if (elf_section_type (osec) == SHT_NULL 7803 && (osec->flags == isec->flags 7804 || (final_link 7805 && ((osec->flags ^ isec->flags) 7806 & ~(SEC_LINK_ONCE | SEC_LINK_DUPLICATES | SEC_RELOC)) == 0))) 7807 elf_section_type (osec) = elf_section_type (isec); 7808 7809 /* FIXME: Is this correct for all OS/PROC specific flags? */ 7810 elf_section_flags (osec) = (elf_section_flags (isec) 7811 & (SHF_MASKOS | SHF_MASKPROC)); 7812 7813 /* Copy sh_info from input for mbind section. */ 7814 if ((elf_tdata (ibfd)->has_gnu_osabi & elf_gnu_osabi_mbind) != 0 7815 && elf_section_flags (isec) & SHF_GNU_MBIND) 7816 elf_section_data (osec)->this_hdr.sh_info 7817 = elf_section_data (isec)->this_hdr.sh_info; 7818 7819 /* Set things up for objcopy and relocatable link. The output 7820 SHT_GROUP section will have its elf_next_in_group pointing back 7821 to the input group members. Ignore linker created group section. 7822 See elfNN_ia64_object_p in elfxx-ia64.c. */ 7823 if ((link_info == NULL 7824 || !link_info->resolve_section_groups) 7825 && (elf_sec_group (isec) == NULL 7826 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0)) 7827 { 7828 if (elf_section_flags (isec) & SHF_GROUP) 7829 elf_section_flags (osec) |= SHF_GROUP; 7830 elf_next_in_group (osec) = elf_next_in_group (isec); 7831 elf_section_data (osec)->group = elf_section_data (isec)->group; 7832 } 7833 7834 /* If not decompress, preserve SHF_COMPRESSED. */ 7835 if (!final_link && (ibfd->flags & BFD_DECOMPRESS) == 0) 7836 elf_section_flags (osec) |= (elf_section_flags (isec) 7837 & SHF_COMPRESSED); 7838 7839 ihdr = &elf_section_data (isec)->this_hdr; 7840 7841 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We 7842 don't use the output section of the linked-to section since it 7843 may be NULL at this point. */ 7844 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) 7845 { 7846 ohdr = &elf_section_data (osec)->this_hdr; 7847 ohdr->sh_flags |= SHF_LINK_ORDER; 7848 elf_linked_to_section (osec) = elf_linked_to_section (isec); 7849 } 7850 7851 osec->use_rela_p = isec->use_rela_p; 7852 7853 return TRUE; 7854} 7855 7856/* Copy private section information. This copies over the entsize 7857 field, and sometimes the info field. */ 7858 7859bfd_boolean 7860_bfd_elf_copy_private_section_data (bfd *ibfd, 7861 asection *isec, 7862 bfd *obfd, 7863 asection *osec) 7864{ 7865 Elf_Internal_Shdr *ihdr, *ohdr; 7866 7867 if (ibfd->xvec->flavour != bfd_target_elf_flavour 7868 || obfd->xvec->flavour != bfd_target_elf_flavour) 7869 return TRUE; 7870 7871 ihdr = &elf_section_data (isec)->this_hdr; 7872 ohdr = &elf_section_data (osec)->this_hdr; 7873 7874 ohdr->sh_entsize = ihdr->sh_entsize; 7875 7876 if (ihdr->sh_type == SHT_SYMTAB 7877 || ihdr->sh_type == SHT_DYNSYM 7878 || ihdr->sh_type == SHT_GNU_verneed 7879 || ihdr->sh_type == SHT_GNU_verdef) 7880 ohdr->sh_info = ihdr->sh_info; 7881 7882 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, 7883 NULL); 7884} 7885 7886/* Look at all the SHT_GROUP sections in IBFD, making any adjustments 7887 necessary if we are removing either the SHT_GROUP section or any of 7888 the group member sections. DISCARDED is the value that a section's 7889 output_section has if the section will be discarded, NULL when this 7890 function is called from objcopy, bfd_abs_section_ptr when called 7891 from the linker. */ 7892 7893bfd_boolean 7894_bfd_elf_fixup_group_sections (bfd *ibfd, asection *discarded) 7895{ 7896 asection *isec; 7897 7898 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 7899 if (elf_section_type (isec) == SHT_GROUP) 7900 { 7901 asection *first = elf_next_in_group (isec); 7902 asection *s = first; 7903 bfd_size_type removed = 0; 7904 7905 while (s != NULL) 7906 { 7907 /* If this member section is being output but the 7908 SHT_GROUP section is not, then clear the group info 7909 set up by _bfd_elf_copy_private_section_data. */ 7910 if (s->output_section != discarded 7911 && isec->output_section == discarded) 7912 { 7913 elf_section_flags (s->output_section) &= ~SHF_GROUP; 7914 elf_group_name (s->output_section) = NULL; 7915 } 7916 else 7917 { 7918 struct bfd_elf_section_data *elf_sec = elf_section_data (s); 7919 if (s->output_section == discarded 7920 && isec->output_section != discarded) 7921 { 7922 /* Conversely, if the member section is not being 7923 output but the SHT_GROUP section is, then adjust 7924 its size. */ 7925 removed += 4; 7926 if (elf_sec->rel.hdr != NULL 7927 && (elf_sec->rel.hdr->sh_flags & SHF_GROUP) != 0) 7928 removed += 4; 7929 if (elf_sec->rela.hdr != NULL 7930 && (elf_sec->rela.hdr->sh_flags & SHF_GROUP) != 0) 7931 removed += 4; 7932 } 7933 else 7934 { 7935 /* Also adjust for zero-sized relocation member 7936 section. */ 7937 if (elf_sec->rel.hdr != NULL 7938 && elf_sec->rel.hdr->sh_size == 0) 7939 removed += 4; 7940 if (elf_sec->rela.hdr != NULL 7941 && elf_sec->rela.hdr->sh_size == 0) 7942 removed += 4; 7943 } 7944 } 7945 s = elf_next_in_group (s); 7946 if (s == first) 7947 break; 7948 } 7949 if (removed != 0) 7950 { 7951 if (discarded != NULL) 7952 { 7953 /* If we've been called for ld -r, then we need to 7954 adjust the input section size. */ 7955 if (isec->rawsize == 0) 7956 isec->rawsize = isec->size; 7957 isec->size = isec->rawsize - removed; 7958 if (isec->size <= 4) 7959 { 7960 isec->size = 0; 7961 isec->flags |= SEC_EXCLUDE; 7962 } 7963 } 7964 else 7965 { 7966 /* Adjust the output section size when called from 7967 objcopy. */ 7968 isec->output_section->size -= removed; 7969 if (isec->output_section->size <= 4) 7970 { 7971 isec->output_section->size = 0; 7972 isec->output_section->flags |= SEC_EXCLUDE; 7973 } 7974 } 7975 } 7976 } 7977 7978 return TRUE; 7979} 7980 7981/* Copy private header information. */ 7982 7983bfd_boolean 7984_bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) 7985{ 7986 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 7987 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 7988 return TRUE; 7989 7990 /* Copy over private BFD data if it has not already been copied. 7991 This must be done here, rather than in the copy_private_bfd_data 7992 entry point, because the latter is called after the section 7993 contents have been set, which means that the program headers have 7994 already been worked out. */ 7995 if (elf_seg_map (obfd) == NULL && elf_tdata (ibfd)->phdr != NULL) 7996 { 7997 if (! copy_private_bfd_data (ibfd, obfd)) 7998 return FALSE; 7999 } 8000 8001 return _bfd_elf_fixup_group_sections (ibfd, NULL); 8002} 8003 8004/* Copy private symbol information. If this symbol is in a section 8005 which we did not map into a BFD section, try to map the section 8006 index correctly. We use special macro definitions for the mapped 8007 section indices; these definitions are interpreted by the 8008 swap_out_syms function. */ 8009 8010#define MAP_ONESYMTAB (SHN_HIOS + 1) 8011#define MAP_DYNSYMTAB (SHN_HIOS + 2) 8012#define MAP_STRTAB (SHN_HIOS + 3) 8013#define MAP_SHSTRTAB (SHN_HIOS + 4) 8014#define MAP_SYM_SHNDX (SHN_HIOS + 5) 8015 8016bfd_boolean 8017_bfd_elf_copy_private_symbol_data (bfd *ibfd, 8018 asymbol *isymarg, 8019 bfd *obfd, 8020 asymbol *osymarg) 8021{ 8022 elf_symbol_type *isym, *osym; 8023 8024 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 8025 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 8026 return TRUE; 8027 8028 isym = elf_symbol_from (ibfd, isymarg); 8029 osym = elf_symbol_from (obfd, osymarg); 8030 8031 if (isym != NULL 8032 && isym->internal_elf_sym.st_shndx != 0 8033 && osym != NULL 8034 && bfd_is_abs_section (isym->symbol.section)) 8035 { 8036 unsigned int shndx; 8037 8038 shndx = isym->internal_elf_sym.st_shndx; 8039 if (shndx == elf_onesymtab (ibfd)) 8040 shndx = MAP_ONESYMTAB; 8041 else if (shndx == elf_dynsymtab (ibfd)) 8042 shndx = MAP_DYNSYMTAB; 8043 else if (shndx == elf_strtab_sec (ibfd)) 8044 shndx = MAP_STRTAB; 8045 else if (shndx == elf_shstrtab_sec (ibfd)) 8046 shndx = MAP_SHSTRTAB; 8047 else if (find_section_in_list (shndx, elf_symtab_shndx_list (ibfd))) 8048 shndx = MAP_SYM_SHNDX; 8049 osym->internal_elf_sym.st_shndx = shndx; 8050 } 8051 8052 return TRUE; 8053} 8054 8055/* Swap out the symbols. */ 8056 8057static bfd_boolean 8058swap_out_syms (bfd *abfd, 8059 struct elf_strtab_hash **sttp, 8060 int relocatable_p) 8061{ 8062 const struct elf_backend_data *bed; 8063 unsigned int symcount; 8064 asymbol **syms; 8065 struct elf_strtab_hash *stt; 8066 Elf_Internal_Shdr *symtab_hdr; 8067 Elf_Internal_Shdr *symtab_shndx_hdr; 8068 Elf_Internal_Shdr *symstrtab_hdr; 8069 struct elf_sym_strtab *symstrtab; 8070 bfd_byte *outbound_syms; 8071 bfd_byte *outbound_shndx; 8072 unsigned long outbound_syms_index; 8073 unsigned long outbound_shndx_index; 8074 unsigned int idx; 8075 unsigned int num_locals; 8076 size_t amt; 8077 bfd_boolean name_local_sections; 8078 8079 if (!elf_map_symbols (abfd, &num_locals)) 8080 return FALSE; 8081 8082 /* Dump out the symtabs. */ 8083 stt = _bfd_elf_strtab_init (); 8084 if (stt == NULL) 8085 return FALSE; 8086 8087 bed = get_elf_backend_data (abfd); 8088 symcount = bfd_get_symcount (abfd); 8089 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 8090 symtab_hdr->sh_type = SHT_SYMTAB; 8091 symtab_hdr->sh_entsize = bed->s->sizeof_sym; 8092 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); 8093 symtab_hdr->sh_info = num_locals + 1; 8094 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 8095 8096 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; 8097 symstrtab_hdr->sh_type = SHT_STRTAB; 8098 8099 /* Allocate buffer to swap out the .strtab section. */ 8100 if (_bfd_mul_overflow (symcount + 1, sizeof (*symstrtab), &amt) 8101 || (symstrtab = (struct elf_sym_strtab *) bfd_malloc (amt)) == NULL) 8102 { 8103 bfd_set_error (bfd_error_no_memory); 8104 _bfd_elf_strtab_free (stt); 8105 return FALSE; 8106 } 8107 8108 if (_bfd_mul_overflow (symcount + 1, bed->s->sizeof_sym, &amt) 8109 || (outbound_syms = (bfd_byte *) bfd_alloc (abfd, amt)) == NULL) 8110 { 8111 error_no_mem: 8112 bfd_set_error (bfd_error_no_memory); 8113 error_return: 8114 free (symstrtab); 8115 _bfd_elf_strtab_free (stt); 8116 return FALSE; 8117 } 8118 symtab_hdr->contents = outbound_syms; 8119 outbound_syms_index = 0; 8120 8121 outbound_shndx = NULL; 8122 outbound_shndx_index = 0; 8123 8124 if (elf_symtab_shndx_list (abfd)) 8125 { 8126 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; 8127 if (symtab_shndx_hdr->sh_name != 0) 8128 { 8129 if (_bfd_mul_overflow (symcount + 1, 8130 sizeof (Elf_External_Sym_Shndx), &amt)) 8131 goto error_no_mem; 8132 outbound_shndx = (bfd_byte *) bfd_zalloc (abfd, amt); 8133 if (outbound_shndx == NULL) 8134 goto error_return; 8135 8136 symtab_shndx_hdr->contents = outbound_shndx; 8137 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 8138 symtab_shndx_hdr->sh_size = amt; 8139 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 8140 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 8141 } 8142 /* FIXME: What about any other headers in the list ? */ 8143 } 8144 8145 /* Now generate the data (for "contents"). */ 8146 { 8147 /* Fill in zeroth symbol and swap it out. */ 8148 Elf_Internal_Sym sym; 8149 sym.st_name = 0; 8150 sym.st_value = 0; 8151 sym.st_size = 0; 8152 sym.st_info = 0; 8153 sym.st_other = 0; 8154 sym.st_shndx = SHN_UNDEF; 8155 sym.st_target_internal = 0; 8156 symstrtab[0].sym = sym; 8157 symstrtab[0].dest_index = outbound_syms_index; 8158 symstrtab[0].destshndx_index = outbound_shndx_index; 8159 outbound_syms_index++; 8160 if (outbound_shndx != NULL) 8161 outbound_shndx_index++; 8162 } 8163 8164 name_local_sections 8165 = (bed->elf_backend_name_local_section_symbols 8166 && bed->elf_backend_name_local_section_symbols (abfd)); 8167 8168 syms = bfd_get_outsymbols (abfd); 8169 for (idx = 0; idx < symcount;) 8170 { 8171 Elf_Internal_Sym sym; 8172 bfd_vma value = syms[idx]->value; 8173 elf_symbol_type *type_ptr; 8174 flagword flags = syms[idx]->flags; 8175 int type; 8176 8177 if (!name_local_sections 8178 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) 8179 { 8180 /* Local section symbols have no name. */ 8181 sym.st_name = (unsigned long) -1; 8182 } 8183 else 8184 { 8185 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize 8186 to get the final offset for st_name. */ 8187 sym.st_name 8188 = (unsigned long) _bfd_elf_strtab_add (stt, syms[idx]->name, 8189 FALSE); 8190 if (sym.st_name == (unsigned long) -1) 8191 goto error_return; 8192 } 8193 8194 type_ptr = elf_symbol_from (abfd, syms[idx]); 8195 8196 if ((flags & BSF_SECTION_SYM) == 0 8197 && bfd_is_com_section (syms[idx]->section)) 8198 { 8199 /* ELF common symbols put the alignment into the `value' field, 8200 and the size into the `size' field. This is backwards from 8201 how BFD handles it, so reverse it here. */ 8202 sym.st_size = value; 8203 if (type_ptr == NULL 8204 || type_ptr->internal_elf_sym.st_value == 0) 8205 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); 8206 else 8207 sym.st_value = type_ptr->internal_elf_sym.st_value; 8208 sym.st_shndx = _bfd_elf_section_from_bfd_section 8209 (abfd, syms[idx]->section); 8210 } 8211 else 8212 { 8213 asection *sec = syms[idx]->section; 8214 unsigned int shndx; 8215 8216 if (sec->output_section) 8217 { 8218 value += sec->output_offset; 8219 sec = sec->output_section; 8220 } 8221 8222 /* Don't add in the section vma for relocatable output. */ 8223 if (! relocatable_p) 8224 value += sec->vma; 8225 sym.st_value = value; 8226 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; 8227 8228 if (bfd_is_abs_section (sec) 8229 && type_ptr != NULL 8230 && type_ptr->internal_elf_sym.st_shndx != 0) 8231 { 8232 /* This symbol is in a real ELF section which we did 8233 not create as a BFD section. Undo the mapping done 8234 by copy_private_symbol_data. */ 8235 shndx = type_ptr->internal_elf_sym.st_shndx; 8236 switch (shndx) 8237 { 8238 case MAP_ONESYMTAB: 8239 shndx = elf_onesymtab (abfd); 8240 break; 8241 case MAP_DYNSYMTAB: 8242 shndx = elf_dynsymtab (abfd); 8243 break; 8244 case MAP_STRTAB: 8245 shndx = elf_strtab_sec (abfd); 8246 break; 8247 case MAP_SHSTRTAB: 8248 shndx = elf_shstrtab_sec (abfd); 8249 break; 8250 case MAP_SYM_SHNDX: 8251 if (elf_symtab_shndx_list (abfd)) 8252 shndx = elf_symtab_shndx_list (abfd)->ndx; 8253 break; 8254 case SHN_COMMON: 8255 case SHN_ABS: 8256 shndx = SHN_ABS; 8257 break; 8258 default: 8259 if (shndx >= SHN_LOPROC && shndx <= SHN_HIOS) 8260 { 8261 if (bed->symbol_section_index) 8262 shndx = bed->symbol_section_index (abfd, type_ptr); 8263 /* Otherwise just leave the index alone. */ 8264 } 8265 else 8266 { 8267 if (shndx > SHN_HIOS && shndx < SHN_HIRESERVE) 8268 _bfd_error_handler (_("%pB: \ 8269Unable to handle section index %x in ELF symbol. Using ABS instead."), 8270 abfd, shndx); 8271 shndx = SHN_ABS; 8272 } 8273 break; 8274 } 8275 } 8276 else 8277 { 8278 shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 8279 8280 if (shndx == SHN_BAD) 8281 { 8282 asection *sec2; 8283 8284 /* Writing this would be a hell of a lot easier if 8285 we had some decent documentation on bfd, and 8286 knew what to expect of the library, and what to 8287 demand of applications. For example, it 8288 appears that `objcopy' might not set the 8289 section of a symbol to be a section that is 8290 actually in the output file. */ 8291 sec2 = bfd_get_section_by_name (abfd, sec->name); 8292 if (sec2 != NULL) 8293 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); 8294 if (shndx == SHN_BAD) 8295 { 8296 /* xgettext:c-format */ 8297 _bfd_error_handler 8298 (_("unable to find equivalent output section" 8299 " for symbol '%s' from section '%s'"), 8300 syms[idx]->name ? syms[idx]->name : "<Local sym>", 8301 sec->name); 8302 bfd_set_error (bfd_error_invalid_operation); 8303 goto error_return; 8304 } 8305 } 8306 } 8307 8308 sym.st_shndx = shndx; 8309 } 8310 8311 if ((flags & BSF_THREAD_LOCAL) != 0) 8312 type = STT_TLS; 8313 else if ((flags & BSF_GNU_INDIRECT_FUNCTION) != 0) 8314 type = STT_GNU_IFUNC; 8315 else if ((flags & BSF_FUNCTION) != 0) 8316 type = STT_FUNC; 8317 else if ((flags & BSF_OBJECT) != 0) 8318 type = STT_OBJECT; 8319 else if ((flags & BSF_RELC) != 0) 8320 type = STT_RELC; 8321 else if ((flags & BSF_SRELC) != 0) 8322 type = STT_SRELC; 8323 else 8324 type = STT_NOTYPE; 8325 8326 if (syms[idx]->section->flags & SEC_THREAD_LOCAL) 8327 type = STT_TLS; 8328 8329 /* Processor-specific types. */ 8330 if (type_ptr != NULL 8331 && bed->elf_backend_get_symbol_type) 8332 type = ((*bed->elf_backend_get_symbol_type) 8333 (&type_ptr->internal_elf_sym, type)); 8334 8335 if (flags & BSF_SECTION_SYM) 8336 { 8337 if (flags & BSF_GLOBAL) 8338 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); 8339 else 8340 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 8341 } 8342 else if (bfd_is_com_section (syms[idx]->section)) 8343 { 8344 if (type != STT_TLS) 8345 { 8346 if ((abfd->flags & BFD_CONVERT_ELF_COMMON)) 8347 type = ((abfd->flags & BFD_USE_ELF_STT_COMMON) 8348 ? STT_COMMON : STT_OBJECT); 8349 else 8350 type = ((flags & BSF_ELF_COMMON) != 0 8351 ? STT_COMMON : STT_OBJECT); 8352 } 8353 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); 8354 } 8355 else if (bfd_is_und_section (syms[idx]->section)) 8356 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) 8357 ? STB_WEAK 8358 : STB_GLOBAL), 8359 type); 8360 else if (flags & BSF_FILE) 8361 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 8362 else 8363 { 8364 int bind = STB_LOCAL; 8365 8366 if (flags & BSF_LOCAL) 8367 bind = STB_LOCAL; 8368 else if (flags & BSF_GNU_UNIQUE) 8369 bind = STB_GNU_UNIQUE; 8370 else if (flags & BSF_WEAK) 8371 bind = STB_WEAK; 8372 else if (flags & BSF_GLOBAL) 8373 bind = STB_GLOBAL; 8374 8375 sym.st_info = ELF_ST_INFO (bind, type); 8376 } 8377 8378 if (type_ptr != NULL) 8379 { 8380 sym.st_other = type_ptr->internal_elf_sym.st_other; 8381 sym.st_target_internal 8382 = type_ptr->internal_elf_sym.st_target_internal; 8383 } 8384 else 8385 { 8386 sym.st_other = 0; 8387 sym.st_target_internal = 0; 8388 } 8389 8390 idx++; 8391 symstrtab[idx].sym = sym; 8392 symstrtab[idx].dest_index = outbound_syms_index; 8393 symstrtab[idx].destshndx_index = outbound_shndx_index; 8394 8395 outbound_syms_index++; 8396 if (outbound_shndx != NULL) 8397 outbound_shndx_index++; 8398 } 8399 8400 /* Finalize the .strtab section. */ 8401 _bfd_elf_strtab_finalize (stt); 8402 8403 /* Swap out the .strtab section. */ 8404 for (idx = 0; idx <= symcount; idx++) 8405 { 8406 struct elf_sym_strtab *elfsym = &symstrtab[idx]; 8407 if (elfsym->sym.st_name == (unsigned long) -1) 8408 elfsym->sym.st_name = 0; 8409 else 8410 elfsym->sym.st_name = _bfd_elf_strtab_offset (stt, 8411 elfsym->sym.st_name); 8412 bed->s->swap_symbol_out (abfd, &elfsym->sym, 8413 (outbound_syms 8414 + (elfsym->dest_index 8415 * bed->s->sizeof_sym)), 8416 (outbound_shndx 8417 + (elfsym->destshndx_index 8418 * sizeof (Elf_External_Sym_Shndx)))); 8419 } 8420 free (symstrtab); 8421 8422 *sttp = stt; 8423 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (stt); 8424 symstrtab_hdr->sh_type = SHT_STRTAB; 8425 symstrtab_hdr->sh_flags = bed->elf_strtab_flags; 8426 symstrtab_hdr->sh_addr = 0; 8427 symstrtab_hdr->sh_entsize = 0; 8428 symstrtab_hdr->sh_link = 0; 8429 symstrtab_hdr->sh_info = 0; 8430 symstrtab_hdr->sh_addralign = 1; 8431 8432 return TRUE; 8433} 8434 8435/* Return the number of bytes required to hold the symtab vector. 8436 8437 Note that we base it on the count plus 1, since we will null terminate 8438 the vector allocated based on this size. However, the ELF symbol table 8439 always has a dummy entry as symbol #0, so it ends up even. */ 8440 8441long 8442_bfd_elf_get_symtab_upper_bound (bfd *abfd) 8443{ 8444 bfd_size_type symcount; 8445 long symtab_size; 8446 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; 8447 8448 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 8449 if (symcount > LONG_MAX / sizeof (asymbol *)) 8450 { 8451 bfd_set_error (bfd_error_file_too_big); 8452 return -1; 8453 } 8454 symtab_size = symcount * (sizeof (asymbol *)); 8455 if (symcount == 0) 8456 symtab_size = sizeof (asymbol *); 8457 else if (!bfd_write_p (abfd)) 8458 { 8459 ufile_ptr filesize = bfd_get_file_size (abfd); 8460 8461 if (filesize != 0 && (unsigned long) symtab_size > filesize) 8462 { 8463 bfd_set_error (bfd_error_file_truncated); 8464 return -1; 8465 } 8466 } 8467 8468 return symtab_size; 8469} 8470 8471long 8472_bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) 8473{ 8474 bfd_size_type symcount; 8475 long symtab_size; 8476 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; 8477 8478 if (elf_dynsymtab (abfd) == 0) 8479 { 8480 bfd_set_error (bfd_error_invalid_operation); 8481 return -1; 8482 } 8483 8484 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 8485 if (symcount > LONG_MAX / sizeof (asymbol *)) 8486 { 8487 bfd_set_error (bfd_error_file_too_big); 8488 return -1; 8489 } 8490 symtab_size = symcount * (sizeof (asymbol *)); 8491 if (symcount == 0) 8492 symtab_size = sizeof (asymbol *); 8493 else if (!bfd_write_p (abfd)) 8494 { 8495 ufile_ptr filesize = bfd_get_file_size (abfd); 8496 8497 if (filesize != 0 && (unsigned long) symtab_size > filesize) 8498 { 8499 bfd_set_error (bfd_error_file_truncated); 8500 return -1; 8501 } 8502 } 8503 8504 return symtab_size; 8505} 8506 8507long 8508_bfd_elf_get_reloc_upper_bound (bfd *abfd, sec_ptr asect) 8509{ 8510 if (asect->reloc_count != 0 && !bfd_write_p (abfd)) 8511 { 8512 /* Sanity check reloc section size. */ 8513 struct bfd_elf_section_data *d = elf_section_data (asect); 8514 Elf_Internal_Shdr *rel_hdr = &d->this_hdr; 8515 bfd_size_type ext_rel_size = rel_hdr->sh_size; 8516 ufile_ptr filesize = bfd_get_file_size (abfd); 8517 8518 if (filesize != 0 && ext_rel_size > filesize) 8519 { 8520 bfd_set_error (bfd_error_file_truncated); 8521 return -1; 8522 } 8523 } 8524 8525#if SIZEOF_LONG == SIZEOF_INT 8526 if (asect->reloc_count >= LONG_MAX / sizeof (arelent *)) 8527 { 8528 bfd_set_error (bfd_error_file_too_big); 8529 return -1; 8530 } 8531#endif 8532 return (asect->reloc_count + 1) * sizeof (arelent *); 8533} 8534 8535/* Canonicalize the relocs. */ 8536 8537long 8538_bfd_elf_canonicalize_reloc (bfd *abfd, 8539 sec_ptr section, 8540 arelent **relptr, 8541 asymbol **symbols) 8542{ 8543 arelent *tblptr; 8544 unsigned int i; 8545 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8546 8547 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) 8548 return -1; 8549 8550 tblptr = section->relocation; 8551 for (i = 0; i < section->reloc_count; i++) 8552 *relptr++ = tblptr++; 8553 8554 *relptr = NULL; 8555 8556 return section->reloc_count; 8557} 8558 8559long 8560_bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) 8561{ 8562 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8563 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); 8564 8565 if (symcount >= 0) 8566 abfd->symcount = symcount; 8567 return symcount; 8568} 8569 8570long 8571_bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, 8572 asymbol **allocation) 8573{ 8574 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8575 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); 8576 8577 if (symcount >= 0) 8578 abfd->dynsymcount = symcount; 8579 return symcount; 8580} 8581 8582/* Return the size required for the dynamic reloc entries. Any loadable 8583 section that was actually installed in the BFD, and has type SHT_REL 8584 or SHT_RELA, and uses the dynamic symbol table, is considered to be a 8585 dynamic reloc section. */ 8586 8587long 8588_bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) 8589{ 8590 bfd_size_type count, ext_rel_size; 8591 asection *s; 8592 8593 if (elf_dynsymtab (abfd) == 0) 8594 { 8595 bfd_set_error (bfd_error_invalid_operation); 8596 return -1; 8597 } 8598 8599 count = 1; 8600 ext_rel_size = 0; 8601 for (s = abfd->sections; s != NULL; s = s->next) 8602 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 8603 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 8604 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) 8605 { 8606 ext_rel_size += s->size; 8607 if (ext_rel_size < s->size) 8608 { 8609 bfd_set_error (bfd_error_file_truncated); 8610 return -1; 8611 } 8612 count += s->size / elf_section_data (s)->this_hdr.sh_entsize; 8613 if (count > LONG_MAX / sizeof (arelent *)) 8614 { 8615 bfd_set_error (bfd_error_file_too_big); 8616 return -1; 8617 } 8618 } 8619 if (count > 1 && !bfd_write_p (abfd)) 8620 { 8621 /* Sanity check reloc section sizes. */ 8622 ufile_ptr filesize = bfd_get_file_size (abfd); 8623 if (filesize != 0 && ext_rel_size > filesize) 8624 { 8625 bfd_set_error (bfd_error_file_truncated); 8626 return -1; 8627 } 8628 } 8629 return count * sizeof (arelent *); 8630} 8631 8632/* Canonicalize the dynamic relocation entries. Note that we return the 8633 dynamic relocations as a single block, although they are actually 8634 associated with particular sections; the interface, which was 8635 designed for SunOS style shared libraries, expects that there is only 8636 one set of dynamic relocs. Any loadable section that was actually 8637 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the 8638 dynamic symbol table, is considered to be a dynamic reloc section. */ 8639 8640long 8641_bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, 8642 arelent **storage, 8643 asymbol **syms) 8644{ 8645 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); 8646 asection *s; 8647 long ret; 8648 8649 if (elf_dynsymtab (abfd) == 0) 8650 { 8651 bfd_set_error (bfd_error_invalid_operation); 8652 return -1; 8653 } 8654 8655 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; 8656 ret = 0; 8657 for (s = abfd->sections; s != NULL; s = s->next) 8658 { 8659 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 8660 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 8661 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) 8662 { 8663 arelent *p; 8664 long count, i; 8665 8666 if (! (*slurp_relocs) (abfd, s, syms, TRUE)) 8667 return -1; 8668 count = s->size / elf_section_data (s)->this_hdr.sh_entsize; 8669 p = s->relocation; 8670 for (i = 0; i < count; i++) 8671 *storage++ = p++; 8672 ret += count; 8673 } 8674 } 8675 8676 *storage = NULL; 8677 8678 return ret; 8679} 8680 8681/* Read in the version information. */ 8682 8683bfd_boolean 8684_bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) 8685{ 8686 bfd_byte *contents = NULL; 8687 unsigned int freeidx = 0; 8688 size_t amt; 8689 8690 if (elf_dynverref (abfd) != 0) 8691 { 8692 Elf_Internal_Shdr *hdr; 8693 Elf_External_Verneed *everneed; 8694 Elf_Internal_Verneed *iverneed; 8695 unsigned int i; 8696 bfd_byte *contents_end; 8697 8698 hdr = &elf_tdata (abfd)->dynverref_hdr; 8699 8700 if (hdr->sh_info == 0 8701 || hdr->sh_info > hdr->sh_size / sizeof (Elf_External_Verneed)) 8702 { 8703 error_return_bad_verref: 8704 _bfd_error_handler 8705 (_("%pB: .gnu.version_r invalid entry"), abfd); 8706 bfd_set_error (bfd_error_bad_value); 8707 error_return_verref: 8708 elf_tdata (abfd)->verref = NULL; 8709 elf_tdata (abfd)->cverrefs = 0; 8710 goto error_return; 8711 } 8712 8713 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) 8714 goto error_return_verref; 8715 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size); 8716 if (contents == NULL) 8717 goto error_return_verref; 8718 8719 if (_bfd_mul_overflow (hdr->sh_info, sizeof (Elf_Internal_Verneed), &amt)) 8720 { 8721 bfd_set_error (bfd_error_file_too_big); 8722 goto error_return_verref; 8723 } 8724 elf_tdata (abfd)->verref = (Elf_Internal_Verneed *) bfd_alloc (abfd, amt); 8725 if (elf_tdata (abfd)->verref == NULL) 8726 goto error_return_verref; 8727 8728 BFD_ASSERT (sizeof (Elf_External_Verneed) 8729 == sizeof (Elf_External_Vernaux)); 8730 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); 8731 everneed = (Elf_External_Verneed *) contents; 8732 iverneed = elf_tdata (abfd)->verref; 8733 for (i = 0; i < hdr->sh_info; i++, iverneed++) 8734 { 8735 Elf_External_Vernaux *evernaux; 8736 Elf_Internal_Vernaux *ivernaux; 8737 unsigned int j; 8738 8739 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); 8740 8741 iverneed->vn_bfd = abfd; 8742 8743 iverneed->vn_filename = 8744 bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 8745 iverneed->vn_file); 8746 if (iverneed->vn_filename == NULL) 8747 goto error_return_bad_verref; 8748 8749 if (iverneed->vn_cnt == 0) 8750 iverneed->vn_auxptr = NULL; 8751 else 8752 { 8753 if (_bfd_mul_overflow (iverneed->vn_cnt, 8754 sizeof (Elf_Internal_Vernaux), &amt)) 8755 { 8756 bfd_set_error (bfd_error_file_too_big); 8757 goto error_return_verref; 8758 } 8759 iverneed->vn_auxptr = (struct elf_internal_vernaux *) 8760 bfd_alloc (abfd, amt); 8761 if (iverneed->vn_auxptr == NULL) 8762 goto error_return_verref; 8763 } 8764 8765 if (iverneed->vn_aux 8766 > (size_t) (contents_end - (bfd_byte *) everneed)) 8767 goto error_return_bad_verref; 8768 8769 evernaux = ((Elf_External_Vernaux *) 8770 ((bfd_byte *) everneed + iverneed->vn_aux)); 8771 ivernaux = iverneed->vn_auxptr; 8772 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) 8773 { 8774 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); 8775 8776 ivernaux->vna_nodename = 8777 bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 8778 ivernaux->vna_name); 8779 if (ivernaux->vna_nodename == NULL) 8780 goto error_return_bad_verref; 8781 8782 if (ivernaux->vna_other > freeidx) 8783 freeidx = ivernaux->vna_other; 8784 8785 ivernaux->vna_nextptr = NULL; 8786 if (ivernaux->vna_next == 0) 8787 { 8788 iverneed->vn_cnt = j + 1; 8789 break; 8790 } 8791 if (j + 1 < iverneed->vn_cnt) 8792 ivernaux->vna_nextptr = ivernaux + 1; 8793 8794 if (ivernaux->vna_next 8795 > (size_t) (contents_end - (bfd_byte *) evernaux)) 8796 goto error_return_bad_verref; 8797 8798 evernaux = ((Elf_External_Vernaux *) 8799 ((bfd_byte *) evernaux + ivernaux->vna_next)); 8800 } 8801 8802 iverneed->vn_nextref = NULL; 8803 if (iverneed->vn_next == 0) 8804 break; 8805 if (i + 1 < hdr->sh_info) 8806 iverneed->vn_nextref = iverneed + 1; 8807 8808 if (iverneed->vn_next 8809 > (size_t) (contents_end - (bfd_byte *) everneed)) 8810 goto error_return_bad_verref; 8811 8812 everneed = ((Elf_External_Verneed *) 8813 ((bfd_byte *) everneed + iverneed->vn_next)); 8814 } 8815 elf_tdata (abfd)->cverrefs = i; 8816 8817 free (contents); 8818 contents = NULL; 8819 } 8820 8821 if (elf_dynverdef (abfd) != 0) 8822 { 8823 Elf_Internal_Shdr *hdr; 8824 Elf_External_Verdef *everdef; 8825 Elf_Internal_Verdef *iverdef; 8826 Elf_Internal_Verdef *iverdefarr; 8827 Elf_Internal_Verdef iverdefmem; 8828 unsigned int i; 8829 unsigned int maxidx; 8830 bfd_byte *contents_end_def, *contents_end_aux; 8831 8832 hdr = &elf_tdata (abfd)->dynverdef_hdr; 8833 8834 if (hdr->sh_info == 0 || hdr->sh_size < sizeof (Elf_External_Verdef)) 8835 { 8836 error_return_bad_verdef: 8837 _bfd_error_handler 8838 (_("%pB: .gnu.version_d invalid entry"), abfd); 8839 bfd_set_error (bfd_error_bad_value); 8840 error_return_verdef: 8841 elf_tdata (abfd)->verdef = NULL; 8842 elf_tdata (abfd)->cverdefs = 0; 8843 goto error_return; 8844 } 8845 8846 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0) 8847 goto error_return_verdef; 8848 contents = _bfd_malloc_and_read (abfd, hdr->sh_size, hdr->sh_size); 8849 if (contents == NULL) 8850 goto error_return_verdef; 8851 8852 BFD_ASSERT (sizeof (Elf_External_Verdef) 8853 >= sizeof (Elf_External_Verdaux)); 8854 contents_end_def = contents + hdr->sh_size 8855 - sizeof (Elf_External_Verdef); 8856 contents_end_aux = contents + hdr->sh_size 8857 - sizeof (Elf_External_Verdaux); 8858 8859 /* We know the number of entries in the section but not the maximum 8860 index. Therefore we have to run through all entries and find 8861 the maximum. */ 8862 everdef = (Elf_External_Verdef *) contents; 8863 maxidx = 0; 8864 for (i = 0; i < hdr->sh_info; ++i) 8865 { 8866 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 8867 8868 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) == 0) 8869 goto error_return_bad_verdef; 8870 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) 8871 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); 8872 8873 if (iverdefmem.vd_next == 0) 8874 break; 8875 8876 if (iverdefmem.vd_next 8877 > (size_t) (contents_end_def - (bfd_byte *) everdef)) 8878 goto error_return_bad_verdef; 8879 8880 everdef = ((Elf_External_Verdef *) 8881 ((bfd_byte *) everdef + iverdefmem.vd_next)); 8882 } 8883 8884 if (default_imported_symver) 8885 { 8886 if (freeidx > maxidx) 8887 maxidx = ++freeidx; 8888 else 8889 freeidx = ++maxidx; 8890 } 8891 if (_bfd_mul_overflow (maxidx, sizeof (Elf_Internal_Verdef), &amt)) 8892 { 8893 bfd_set_error (bfd_error_file_too_big); 8894 goto error_return_verdef; 8895 } 8896 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); 8897 if (elf_tdata (abfd)->verdef == NULL) 8898 goto error_return_verdef; 8899 8900 elf_tdata (abfd)->cverdefs = maxidx; 8901 8902 everdef = (Elf_External_Verdef *) contents; 8903 iverdefarr = elf_tdata (abfd)->verdef; 8904 for (i = 0; i < hdr->sh_info; i++) 8905 { 8906 Elf_External_Verdaux *everdaux; 8907 Elf_Internal_Verdaux *iverdaux; 8908 unsigned int j; 8909 8910 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 8911 8912 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) 8913 goto error_return_bad_verdef; 8914 8915 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; 8916 memcpy (iverdef, &iverdefmem, offsetof (Elf_Internal_Verdef, vd_bfd)); 8917 8918 iverdef->vd_bfd = abfd; 8919 8920 if (iverdef->vd_cnt == 0) 8921 iverdef->vd_auxptr = NULL; 8922 else 8923 { 8924 if (_bfd_mul_overflow (iverdef->vd_cnt, 8925 sizeof (Elf_Internal_Verdaux), &amt)) 8926 { 8927 bfd_set_error (bfd_error_file_too_big); 8928 goto error_return_verdef; 8929 } 8930 iverdef->vd_auxptr = (struct elf_internal_verdaux *) 8931 bfd_alloc (abfd, amt); 8932 if (iverdef->vd_auxptr == NULL) 8933 goto error_return_verdef; 8934 } 8935 8936 if (iverdef->vd_aux 8937 > (size_t) (contents_end_aux - (bfd_byte *) everdef)) 8938 goto error_return_bad_verdef; 8939 8940 everdaux = ((Elf_External_Verdaux *) 8941 ((bfd_byte *) everdef + iverdef->vd_aux)); 8942 iverdaux = iverdef->vd_auxptr; 8943 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) 8944 { 8945 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); 8946 8947 iverdaux->vda_nodename = 8948 bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 8949 iverdaux->vda_name); 8950 if (iverdaux->vda_nodename == NULL) 8951 goto error_return_bad_verdef; 8952 8953 iverdaux->vda_nextptr = NULL; 8954 if (iverdaux->vda_next == 0) 8955 { 8956 iverdef->vd_cnt = j + 1; 8957 break; 8958 } 8959 if (j + 1 < iverdef->vd_cnt) 8960 iverdaux->vda_nextptr = iverdaux + 1; 8961 8962 if (iverdaux->vda_next 8963 > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) 8964 goto error_return_bad_verdef; 8965 8966 everdaux = ((Elf_External_Verdaux *) 8967 ((bfd_byte *) everdaux + iverdaux->vda_next)); 8968 } 8969 8970 iverdef->vd_nodename = NULL; 8971 if (iverdef->vd_cnt) 8972 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; 8973 8974 iverdef->vd_nextdef = NULL; 8975 if (iverdef->vd_next == 0) 8976 break; 8977 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) 8978 iverdef->vd_nextdef = iverdef + 1; 8979 8980 everdef = ((Elf_External_Verdef *) 8981 ((bfd_byte *) everdef + iverdef->vd_next)); 8982 } 8983 8984 free (contents); 8985 contents = NULL; 8986 } 8987 else if (default_imported_symver) 8988 { 8989 if (freeidx < 3) 8990 freeidx = 3; 8991 else 8992 freeidx++; 8993 8994 if (_bfd_mul_overflow (freeidx, sizeof (Elf_Internal_Verdef), &amt)) 8995 { 8996 bfd_set_error (bfd_error_file_too_big); 8997 goto error_return; 8998 } 8999 elf_tdata (abfd)->verdef = (Elf_Internal_Verdef *) bfd_zalloc (abfd, amt); 9000 if (elf_tdata (abfd)->verdef == NULL) 9001 goto error_return; 9002 9003 elf_tdata (abfd)->cverdefs = freeidx; 9004 } 9005 9006 /* Create a default version based on the soname. */ 9007 if (default_imported_symver) 9008 { 9009 Elf_Internal_Verdef *iverdef; 9010 Elf_Internal_Verdaux *iverdaux; 9011 9012 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1]; 9013 9014 iverdef->vd_version = VER_DEF_CURRENT; 9015 iverdef->vd_flags = 0; 9016 iverdef->vd_ndx = freeidx; 9017 iverdef->vd_cnt = 1; 9018 9019 iverdef->vd_bfd = abfd; 9020 9021 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); 9022 if (iverdef->vd_nodename == NULL) 9023 goto error_return_verdef; 9024 iverdef->vd_nextdef = NULL; 9025 iverdef->vd_auxptr = ((struct elf_internal_verdaux *) 9026 bfd_zalloc (abfd, sizeof (Elf_Internal_Verdaux))); 9027 if (iverdef->vd_auxptr == NULL) 9028 goto error_return_verdef; 9029 9030 iverdaux = iverdef->vd_auxptr; 9031 iverdaux->vda_nodename = iverdef->vd_nodename; 9032 } 9033 9034 return TRUE; 9035 9036 error_return: 9037 free (contents); 9038 return FALSE; 9039} 9040 9041asymbol * 9042_bfd_elf_make_empty_symbol (bfd *abfd) 9043{ 9044 elf_symbol_type *newsym; 9045 9046 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (*newsym)); 9047 if (!newsym) 9048 return NULL; 9049 newsym->symbol.the_bfd = abfd; 9050 return &newsym->symbol; 9051} 9052 9053void 9054_bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, 9055 asymbol *symbol, 9056 symbol_info *ret) 9057{ 9058 bfd_symbol_info (symbol, ret); 9059} 9060 9061/* Return whether a symbol name implies a local symbol. Most targets 9062 use this function for the is_local_label_name entry point, but some 9063 override it. */ 9064 9065bfd_boolean 9066_bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, 9067 const char *name) 9068{ 9069 /* Normal local symbols start with ``.L''. */ 9070 if (name[0] == '.' && name[1] == 'L') 9071 return TRUE; 9072 9073 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate 9074 DWARF debugging symbols starting with ``..''. */ 9075 if (name[0] == '.' && name[1] == '.') 9076 return TRUE; 9077 9078 /* gcc will sometimes generate symbols beginning with ``_.L_'' when 9079 emitting DWARF debugging output. I suspect this is actually a 9080 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call 9081 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading 9082 underscore to be emitted on some ELF targets). For ease of use, 9083 we treat such symbols as local. */ 9084 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') 9085 return TRUE; 9086 9087 /* Treat assembler generated fake symbols, dollar local labels and 9088 forward-backward labels (aka local labels) as locals. 9089 These labels have the form: 9090 9091 L0^A.* (fake symbols) 9092 9093 [.]?L[0123456789]+{^A|^B}[0123456789]* (local labels) 9094 9095 Versions which start with .L will have already been matched above, 9096 so we only need to match the rest. */ 9097 if (name[0] == 'L' && ISDIGIT (name[1])) 9098 { 9099 bfd_boolean ret = FALSE; 9100 const char * p; 9101 char c; 9102 9103 for (p = name + 2; (c = *p); p++) 9104 { 9105 if (c == 1 || c == 2) 9106 { 9107 if (c == 1 && p == name + 2) 9108 /* A fake symbol. */ 9109 return TRUE; 9110 9111 /* FIXME: We are being paranoid here and treating symbols like 9112 L0^Bfoo as if there were non-local, on the grounds that the 9113 assembler will never generate them. But can any symbol 9114 containing an ASCII value in the range 1-31 ever be anything 9115 other than some kind of local ? */ 9116 ret = TRUE; 9117 } 9118 9119 if (! ISDIGIT (c)) 9120 { 9121 ret = FALSE; 9122 break; 9123 } 9124 } 9125 return ret; 9126 } 9127 9128 return FALSE; 9129} 9130 9131alent * 9132_bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, 9133 asymbol *symbol ATTRIBUTE_UNUSED) 9134{ 9135 abort (); 9136 return NULL; 9137} 9138 9139bfd_boolean 9140_bfd_elf_set_arch_mach (bfd *abfd, 9141 enum bfd_architecture arch, 9142 unsigned long machine) 9143{ 9144 /* If this isn't the right architecture for this backend, and this 9145 isn't the generic backend, fail. */ 9146 if (arch != get_elf_backend_data (abfd)->arch 9147 && arch != bfd_arch_unknown 9148 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) 9149 return FALSE; 9150 9151 return bfd_default_set_arch_mach (abfd, arch, machine); 9152} 9153 9154/* Find the nearest line to a particular section and offset, 9155 for error reporting. */ 9156 9157bfd_boolean 9158_bfd_elf_find_nearest_line (bfd *abfd, 9159 asymbol **symbols, 9160 asection *section, 9161 bfd_vma offset, 9162 const char **filename_ptr, 9163 const char **functionname_ptr, 9164 unsigned int *line_ptr, 9165 unsigned int *discriminator_ptr) 9166{ 9167 bfd_boolean found; 9168 9169 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, 9170 filename_ptr, functionname_ptr, 9171 line_ptr, discriminator_ptr, 9172 dwarf_debug_sections, 9173 &elf_tdata (abfd)->dwarf2_find_line_info)) 9174 return TRUE; 9175 9176 if (_bfd_dwarf1_find_nearest_line (abfd, symbols, section, offset, 9177 filename_ptr, functionname_ptr, line_ptr)) 9178 { 9179 if (!*functionname_ptr) 9180 _bfd_elf_find_function (abfd, symbols, section, offset, 9181 *filename_ptr ? NULL : filename_ptr, 9182 functionname_ptr); 9183 return TRUE; 9184 } 9185 9186 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, 9187 &found, filename_ptr, 9188 functionname_ptr, line_ptr, 9189 &elf_tdata (abfd)->line_info)) 9190 return FALSE; 9191 if (found && (*functionname_ptr || *line_ptr)) 9192 return TRUE; 9193 9194 if (symbols == NULL) 9195 return FALSE; 9196 9197 if (! _bfd_elf_find_function (abfd, symbols, section, offset, 9198 filename_ptr, functionname_ptr)) 9199 return FALSE; 9200 9201 *line_ptr = 0; 9202 return TRUE; 9203} 9204 9205/* Find the line for a symbol. */ 9206 9207bfd_boolean 9208_bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, 9209 const char **filename_ptr, unsigned int *line_ptr) 9210{ 9211 return _bfd_dwarf2_find_nearest_line (abfd, symbols, symbol, NULL, 0, 9212 filename_ptr, NULL, line_ptr, NULL, 9213 dwarf_debug_sections, 9214 &elf_tdata (abfd)->dwarf2_find_line_info); 9215} 9216 9217/* After a call to bfd_find_nearest_line, successive calls to 9218 bfd_find_inliner_info can be used to get source information about 9219 each level of function inlining that terminated at the address 9220 passed to bfd_find_nearest_line. Currently this is only supported 9221 for DWARF2 with appropriate DWARF3 extensions. */ 9222 9223bfd_boolean 9224_bfd_elf_find_inliner_info (bfd *abfd, 9225 const char **filename_ptr, 9226 const char **functionname_ptr, 9227 unsigned int *line_ptr) 9228{ 9229 bfd_boolean found; 9230 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, 9231 functionname_ptr, line_ptr, 9232 & elf_tdata (abfd)->dwarf2_find_line_info); 9233 return found; 9234} 9235 9236int 9237_bfd_elf_sizeof_headers (bfd *abfd, struct bfd_link_info *info) 9238{ 9239 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9240 int ret = bed->s->sizeof_ehdr; 9241 9242 if (!bfd_link_relocatable (info)) 9243 { 9244 bfd_size_type phdr_size = elf_program_header_size (abfd); 9245 9246 if (phdr_size == (bfd_size_type) -1) 9247 { 9248 struct elf_segment_map *m; 9249 9250 phdr_size = 0; 9251 for (m = elf_seg_map (abfd); m != NULL; m = m->next) 9252 phdr_size += bed->s->sizeof_phdr; 9253 9254 if (phdr_size == 0) 9255 phdr_size = get_program_header_size (abfd, info); 9256 } 9257 9258 elf_program_header_size (abfd) = phdr_size; 9259 ret += phdr_size; 9260 } 9261 9262 return ret; 9263} 9264 9265bfd_boolean 9266_bfd_elf_set_section_contents (bfd *abfd, 9267 sec_ptr section, 9268 const void *location, 9269 file_ptr offset, 9270 bfd_size_type count) 9271{ 9272 Elf_Internal_Shdr *hdr; 9273 file_ptr pos; 9274 9275 if (! abfd->output_has_begun 9276 && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) 9277 return FALSE; 9278 9279 if (!count) 9280 return TRUE; 9281 9282 hdr = &elf_section_data (section)->this_hdr; 9283 if (hdr->sh_offset == (file_ptr) -1) 9284 { 9285 unsigned char *contents; 9286 9287 if (bfd_section_is_ctf (section)) 9288 /* Nothing to do with this section: the contents are generated 9289 later. */ 9290 return TRUE; 9291 9292 if ((section->flags & SEC_ELF_COMPRESS) == 0) 9293 { 9294 _bfd_error_handler 9295 (_("%pB:%pA: error: attempting to write into an unallocated compressed section"), 9296 abfd, section); 9297 bfd_set_error (bfd_error_invalid_operation); 9298 return FALSE; 9299 } 9300 9301 if ((offset + count) > hdr->sh_size) 9302 { 9303 _bfd_error_handler 9304 (_("%pB:%pA: error: attempting to write over the end of the section"), 9305 abfd, section); 9306 9307 bfd_set_error (bfd_error_invalid_operation); 9308 return FALSE; 9309 } 9310 9311 contents = hdr->contents; 9312 if (contents == NULL) 9313 { 9314 _bfd_error_handler 9315 (_("%pB:%pA: error: attempting to write section into an empty buffer"), 9316 abfd, section); 9317 9318 bfd_set_error (bfd_error_invalid_operation); 9319 return FALSE; 9320 } 9321 9322 memcpy (contents + offset, location, count); 9323 return TRUE; 9324 } 9325 9326 pos = hdr->sh_offset + offset; 9327 if (bfd_seek (abfd, pos, SEEK_SET) != 0 9328 || bfd_bwrite (location, count, abfd) != count) 9329 return FALSE; 9330 9331 return TRUE; 9332} 9333 9334bfd_boolean 9335_bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, 9336 arelent *cache_ptr ATTRIBUTE_UNUSED, 9337 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) 9338{ 9339 abort (); 9340 return FALSE; 9341} 9342 9343/* Try to convert a non-ELF reloc into an ELF one. */ 9344 9345bfd_boolean 9346_bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) 9347{ 9348 /* Check whether we really have an ELF howto. */ 9349 9350 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) 9351 { 9352 bfd_reloc_code_real_type code; 9353 reloc_howto_type *howto; 9354 9355 /* Alien reloc: Try to determine its type to replace it with an 9356 equivalent ELF reloc. */ 9357 9358 if (areloc->howto->pc_relative) 9359 { 9360 switch (areloc->howto->bitsize) 9361 { 9362 case 8: 9363 code = BFD_RELOC_8_PCREL; 9364 break; 9365 case 12: 9366 code = BFD_RELOC_12_PCREL; 9367 break; 9368 case 16: 9369 code = BFD_RELOC_16_PCREL; 9370 break; 9371 case 24: 9372 code = BFD_RELOC_24_PCREL; 9373 break; 9374 case 32: 9375 code = BFD_RELOC_32_PCREL; 9376 break; 9377 case 64: 9378 code = BFD_RELOC_64_PCREL; 9379 break; 9380 default: 9381 goto fail; 9382 } 9383 9384 howto = bfd_reloc_type_lookup (abfd, code); 9385 9386 if (howto && areloc->howto->pcrel_offset != howto->pcrel_offset) 9387 { 9388 if (howto->pcrel_offset) 9389 areloc->addend += areloc->address; 9390 else 9391 areloc->addend -= areloc->address; /* addend is unsigned!! */ 9392 } 9393 } 9394 else 9395 { 9396 switch (areloc->howto->bitsize) 9397 { 9398 case 8: 9399 code = BFD_RELOC_8; 9400 break; 9401 case 14: 9402 code = BFD_RELOC_14; 9403 break; 9404 case 16: 9405 code = BFD_RELOC_16; 9406 break; 9407 case 26: 9408 code = BFD_RELOC_26; 9409 break; 9410 case 32: 9411 code = BFD_RELOC_32; 9412 break; 9413 case 64: 9414 code = BFD_RELOC_64; 9415 break; 9416 default: 9417 goto fail; 9418 } 9419 9420 howto = bfd_reloc_type_lookup (abfd, code); 9421 } 9422 9423 if (howto) 9424 areloc->howto = howto; 9425 else 9426 goto fail; 9427 } 9428 9429 return TRUE; 9430 9431 fail: 9432 /* xgettext:c-format */ 9433 _bfd_error_handler (_("%pB: %s unsupported"), 9434 abfd, areloc->howto->name); 9435 bfd_set_error (bfd_error_sorry); 9436 return FALSE; 9437} 9438 9439bfd_boolean 9440_bfd_elf_close_and_cleanup (bfd *abfd) 9441{ 9442 struct elf_obj_tdata *tdata = elf_tdata (abfd); 9443 if (tdata != NULL 9444 && (bfd_get_format (abfd) == bfd_object 9445 || bfd_get_format (abfd) == bfd_core)) 9446 { 9447 if (elf_tdata (abfd)->o != NULL && elf_shstrtab (abfd) != NULL) 9448 _bfd_elf_strtab_free (elf_shstrtab (abfd)); 9449 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); 9450 } 9451 9452 return _bfd_generic_close_and_cleanup (abfd); 9453} 9454 9455/* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY 9456 in the relocation's offset. Thus we cannot allow any sort of sanity 9457 range-checking to interfere. There is nothing else to do in processing 9458 this reloc. */ 9459 9460bfd_reloc_status_type 9461_bfd_elf_rel_vtable_reloc_fn 9462 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, 9463 struct bfd_symbol *symbol ATTRIBUTE_UNUSED, 9464 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, 9465 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) 9466{ 9467 return bfd_reloc_ok; 9468} 9469 9470/* Elf core file support. Much of this only works on native 9471 toolchains, since we rely on knowing the 9472 machine-dependent procfs structure in order to pick 9473 out details about the corefile. */ 9474 9475#ifdef HAVE_SYS_PROCFS_H 9476# include <sys/procfs.h> 9477#endif 9478 9479/* Return a PID that identifies a "thread" for threaded cores, or the 9480 PID of the main process for non-threaded cores. */ 9481 9482static int 9483elfcore_make_pid (bfd *abfd) 9484{ 9485 int pid; 9486 9487 pid = elf_tdata (abfd)->core->lwpid; 9488 if (pid == 0) 9489 pid = elf_tdata (abfd)->core->pid; 9490 9491 return pid; 9492} 9493 9494/* If there isn't a section called NAME, make one, using 9495 data from SECT. Note, this function will generate a 9496 reference to NAME, so you shouldn't deallocate or 9497 overwrite it. */ 9498 9499static bfd_boolean 9500elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) 9501{ 9502 asection *sect2; 9503 9504 if (bfd_get_section_by_name (abfd, name) != NULL) 9505 return TRUE; 9506 9507 sect2 = bfd_make_section_with_flags (abfd, name, sect->flags); 9508 if (sect2 == NULL) 9509 return FALSE; 9510 9511 sect2->size = sect->size; 9512 sect2->filepos = sect->filepos; 9513 sect2->alignment_power = sect->alignment_power; 9514 return TRUE; 9515} 9516 9517/* Create a pseudosection containing SIZE bytes at FILEPOS. This 9518 actually creates up to two pseudosections: 9519 - For the single-threaded case, a section named NAME, unless 9520 such a section already exists. 9521 - For the multi-threaded case, a section named "NAME/PID", where 9522 PID is elfcore_make_pid (abfd). 9523 Both pseudosections have identical contents. */ 9524bfd_boolean 9525_bfd_elfcore_make_pseudosection (bfd *abfd, 9526 char *name, 9527 size_t size, 9528 ufile_ptr filepos) 9529{ 9530 char buf[100]; 9531 char *threaded_name; 9532 size_t len; 9533 asection *sect; 9534 9535 /* Build the section name. */ 9536 9537 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); 9538 len = strlen (buf) + 1; 9539 threaded_name = (char *) bfd_alloc (abfd, len); 9540 if (threaded_name == NULL) 9541 return FALSE; 9542 memcpy (threaded_name, buf, len); 9543 9544 sect = bfd_make_section_anyway_with_flags (abfd, threaded_name, 9545 SEC_HAS_CONTENTS); 9546 if (sect == NULL) 9547 return FALSE; 9548 sect->size = size; 9549 sect->filepos = filepos; 9550 sect->alignment_power = 2; 9551 9552 return elfcore_maybe_make_sect (abfd, name, sect); 9553} 9554 9555static bfd_boolean 9556elfcore_make_auxv_note_section (bfd *abfd, Elf_Internal_Note *note, 9557 size_t offs) 9558{ 9559 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", 9560 SEC_HAS_CONTENTS); 9561 9562 if (sect == NULL) 9563 return FALSE; 9564 9565 sect->size = note->descsz - offs; 9566 sect->filepos = note->descpos + offs; 9567 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 9568 9569 return TRUE; 9570} 9571 9572/* prstatus_t exists on: 9573 solaris 2.5+ 9574 linux 2.[01] + glibc 9575 unixware 4.2 9576*/ 9577 9578#if defined (HAVE_PRSTATUS_T) 9579 9580static bfd_boolean 9581elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) 9582{ 9583 size_t size; 9584 int offset; 9585 9586 if (note->descsz == sizeof (prstatus_t)) 9587 { 9588 prstatus_t prstat; 9589 9590 size = sizeof (prstat.pr_reg); 9591 offset = offsetof (prstatus_t, pr_reg); 9592 memcpy (&prstat, note->descdata, sizeof (prstat)); 9593 9594 /* Do not overwrite the core signal if it 9595 has already been set by another thread. */ 9596 if (elf_tdata (abfd)->core->signal == 0) 9597 elf_tdata (abfd)->core->signal = prstat.pr_cursig; 9598 if (elf_tdata (abfd)->core->pid == 0) 9599 elf_tdata (abfd)->core->pid = prstat.pr_pid; 9600 9601 /* pr_who exists on: 9602 solaris 2.5+ 9603 unixware 4.2 9604 pr_who doesn't exist on: 9605 linux 2.[01] 9606 */ 9607#if defined (HAVE_PRSTATUS_T_PR_WHO) 9608 elf_tdata (abfd)->core->lwpid = prstat.pr_who; 9609#else 9610 elf_tdata (abfd)->core->lwpid = prstat.pr_pid; 9611#endif 9612 } 9613#if defined (HAVE_PRSTATUS32_T) 9614 else if (note->descsz == sizeof (prstatus32_t)) 9615 { 9616 /* 64-bit host, 32-bit corefile */ 9617 prstatus32_t prstat; 9618 9619 size = sizeof (prstat.pr_reg); 9620 offset = offsetof (prstatus32_t, pr_reg); 9621 memcpy (&prstat, note->descdata, sizeof (prstat)); 9622 9623 /* Do not overwrite the core signal if it 9624 has already been set by another thread. */ 9625 if (elf_tdata (abfd)->core->signal == 0) 9626 elf_tdata (abfd)->core->signal = prstat.pr_cursig; 9627 if (elf_tdata (abfd)->core->pid == 0) 9628 elf_tdata (abfd)->core->pid = prstat.pr_pid; 9629 9630 /* pr_who exists on: 9631 solaris 2.5+ 9632 unixware 4.2 9633 pr_who doesn't exist on: 9634 linux 2.[01] 9635 */ 9636#if defined (HAVE_PRSTATUS32_T_PR_WHO) 9637 elf_tdata (abfd)->core->lwpid = prstat.pr_who; 9638#else 9639 elf_tdata (abfd)->core->lwpid = prstat.pr_pid; 9640#endif 9641 } 9642#endif /* HAVE_PRSTATUS32_T */ 9643 else 9644 { 9645 /* Fail - we don't know how to handle any other 9646 note size (ie. data object type). */ 9647 return TRUE; 9648 } 9649 9650 /* Make a ".reg/999" section and a ".reg" section. */ 9651 return _bfd_elfcore_make_pseudosection (abfd, ".reg", 9652 size, note->descpos + offset); 9653} 9654#endif /* defined (HAVE_PRSTATUS_T) */ 9655 9656/* Create a pseudosection containing the exact contents of NOTE. */ 9657static bfd_boolean 9658elfcore_make_note_pseudosection (bfd *abfd, 9659 char *name, 9660 Elf_Internal_Note *note) 9661{ 9662 return _bfd_elfcore_make_pseudosection (abfd, name, 9663 note->descsz, note->descpos); 9664} 9665 9666/* There isn't a consistent prfpregset_t across platforms, 9667 but it doesn't matter, because we don't have to pick this 9668 data structure apart. */ 9669 9670static bfd_boolean 9671elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) 9672{ 9673 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 9674} 9675 9676/* Linux dumps the Intel SSE regs in a note named "LINUX" with a note 9677 type of NT_PRXFPREG. Just include the whole note's contents 9678 literally. */ 9679 9680static bfd_boolean 9681elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) 9682{ 9683 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); 9684} 9685 9686/* Linux dumps the Intel XSAVE extended state in a note named "LINUX" 9687 with a note type of NT_X86_XSTATE. Just include the whole note's 9688 contents literally. */ 9689 9690static bfd_boolean 9691elfcore_grok_xstatereg (bfd *abfd, Elf_Internal_Note *note) 9692{ 9693 return elfcore_make_note_pseudosection (abfd, ".reg-xstate", note); 9694} 9695 9696static bfd_boolean 9697elfcore_grok_ppc_vmx (bfd *abfd, Elf_Internal_Note *note) 9698{ 9699 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vmx", note); 9700} 9701 9702static bfd_boolean 9703elfcore_grok_ppc_vsx (bfd *abfd, Elf_Internal_Note *note) 9704{ 9705 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-vsx", note); 9706} 9707 9708static bfd_boolean 9709elfcore_grok_ppc_tar (bfd *abfd, Elf_Internal_Note *note) 9710{ 9711 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tar", note); 9712} 9713 9714static bfd_boolean 9715elfcore_grok_ppc_ppr (bfd *abfd, Elf_Internal_Note *note) 9716{ 9717 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ppr", note); 9718} 9719 9720static bfd_boolean 9721elfcore_grok_ppc_dscr (bfd *abfd, Elf_Internal_Note *note) 9722{ 9723 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-dscr", note); 9724} 9725 9726static bfd_boolean 9727elfcore_grok_ppc_ebb (bfd *abfd, Elf_Internal_Note *note) 9728{ 9729 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-ebb", note); 9730} 9731 9732static bfd_boolean 9733elfcore_grok_ppc_pmu (bfd *abfd, Elf_Internal_Note *note) 9734{ 9735 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-pmu", note); 9736} 9737 9738static bfd_boolean 9739elfcore_grok_ppc_tm_cgpr (bfd *abfd, Elf_Internal_Note *note) 9740{ 9741 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cgpr", note); 9742} 9743 9744static bfd_boolean 9745elfcore_grok_ppc_tm_cfpr (bfd *abfd, Elf_Internal_Note *note) 9746{ 9747 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cfpr", note); 9748} 9749 9750static bfd_boolean 9751elfcore_grok_ppc_tm_cvmx (bfd *abfd, Elf_Internal_Note *note) 9752{ 9753 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvmx", note); 9754} 9755 9756static bfd_boolean 9757elfcore_grok_ppc_tm_cvsx (bfd *abfd, Elf_Internal_Note *note) 9758{ 9759 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cvsx", note); 9760} 9761 9762static bfd_boolean 9763elfcore_grok_ppc_tm_spr (bfd *abfd, Elf_Internal_Note *note) 9764{ 9765 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-spr", note); 9766} 9767 9768static bfd_boolean 9769elfcore_grok_ppc_tm_ctar (bfd *abfd, Elf_Internal_Note *note) 9770{ 9771 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-ctar", note); 9772} 9773 9774static bfd_boolean 9775elfcore_grok_ppc_tm_cppr (bfd *abfd, Elf_Internal_Note *note) 9776{ 9777 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cppr", note); 9778} 9779 9780static bfd_boolean 9781elfcore_grok_ppc_tm_cdscr (bfd *abfd, Elf_Internal_Note *note) 9782{ 9783 return elfcore_make_note_pseudosection (abfd, ".reg-ppc-tm-cdscr", note); 9784} 9785 9786static bfd_boolean 9787elfcore_grok_s390_high_gprs (bfd *abfd, Elf_Internal_Note *note) 9788{ 9789 return elfcore_make_note_pseudosection (abfd, ".reg-s390-high-gprs", note); 9790} 9791 9792static bfd_boolean 9793elfcore_grok_s390_timer (bfd *abfd, Elf_Internal_Note *note) 9794{ 9795 return elfcore_make_note_pseudosection (abfd, ".reg-s390-timer", note); 9796} 9797 9798static bfd_boolean 9799elfcore_grok_s390_todcmp (bfd *abfd, Elf_Internal_Note *note) 9800{ 9801 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todcmp", note); 9802} 9803 9804static bfd_boolean 9805elfcore_grok_s390_todpreg (bfd *abfd, Elf_Internal_Note *note) 9806{ 9807 return elfcore_make_note_pseudosection (abfd, ".reg-s390-todpreg", note); 9808} 9809 9810static bfd_boolean 9811elfcore_grok_s390_ctrs (bfd *abfd, Elf_Internal_Note *note) 9812{ 9813 return elfcore_make_note_pseudosection (abfd, ".reg-s390-ctrs", note); 9814} 9815 9816static bfd_boolean 9817elfcore_grok_s390_prefix (bfd *abfd, Elf_Internal_Note *note) 9818{ 9819 return elfcore_make_note_pseudosection (abfd, ".reg-s390-prefix", note); 9820} 9821 9822static bfd_boolean 9823elfcore_grok_s390_last_break (bfd *abfd, Elf_Internal_Note *note) 9824{ 9825 return elfcore_make_note_pseudosection (abfd, ".reg-s390-last-break", note); 9826} 9827 9828static bfd_boolean 9829elfcore_grok_s390_system_call (bfd *abfd, Elf_Internal_Note *note) 9830{ 9831 return elfcore_make_note_pseudosection (abfd, ".reg-s390-system-call", note); 9832} 9833 9834static bfd_boolean 9835elfcore_grok_s390_tdb (bfd *abfd, Elf_Internal_Note *note) 9836{ 9837 return elfcore_make_note_pseudosection (abfd, ".reg-s390-tdb", note); 9838} 9839 9840static bfd_boolean 9841elfcore_grok_s390_vxrs_low (bfd *abfd, Elf_Internal_Note *note) 9842{ 9843 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-low", note); 9844} 9845 9846static bfd_boolean 9847elfcore_grok_s390_vxrs_high (bfd *abfd, Elf_Internal_Note *note) 9848{ 9849 return elfcore_make_note_pseudosection (abfd, ".reg-s390-vxrs-high", note); 9850} 9851 9852static bfd_boolean 9853elfcore_grok_s390_gs_cb (bfd *abfd, Elf_Internal_Note *note) 9854{ 9855 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-cb", note); 9856} 9857 9858static bfd_boolean 9859elfcore_grok_s390_gs_bc (bfd *abfd, Elf_Internal_Note *note) 9860{ 9861 return elfcore_make_note_pseudosection (abfd, ".reg-s390-gs-bc", note); 9862} 9863 9864static bfd_boolean 9865elfcore_grok_arm_vfp (bfd *abfd, Elf_Internal_Note *note) 9866{ 9867 return elfcore_make_note_pseudosection (abfd, ".reg-arm-vfp", note); 9868} 9869 9870static bfd_boolean 9871elfcore_grok_aarch_tls (bfd *abfd, Elf_Internal_Note *note) 9872{ 9873 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-tls", note); 9874} 9875 9876static bfd_boolean 9877elfcore_grok_aarch_hw_break (bfd *abfd, Elf_Internal_Note *note) 9878{ 9879 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-break", note); 9880} 9881 9882static bfd_boolean 9883elfcore_grok_aarch_hw_watch (bfd *abfd, Elf_Internal_Note *note) 9884{ 9885 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-hw-watch", note); 9886} 9887 9888static bfd_boolean 9889elfcore_grok_aarch_sve (bfd *abfd, Elf_Internal_Note *note) 9890{ 9891 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-sve", note); 9892} 9893 9894static bfd_boolean 9895elfcore_grok_aarch_pauth (bfd *abfd, Elf_Internal_Note *note) 9896{ 9897 return elfcore_make_note_pseudosection (abfd, ".reg-aarch-pauth", note); 9898} 9899 9900static bfd_boolean 9901elfcore_grok_arc_v2 (bfd *abfd, Elf_Internal_Note *note) 9902{ 9903 return elfcore_make_note_pseudosection (abfd, ".reg-arc-v2", note); 9904} 9905 9906#if defined (HAVE_PRPSINFO_T) 9907typedef prpsinfo_t elfcore_psinfo_t; 9908#if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ 9909typedef prpsinfo32_t elfcore_psinfo32_t; 9910#endif 9911#endif 9912 9913#if defined (HAVE_PSINFO_T) 9914typedef psinfo_t elfcore_psinfo_t; 9915#if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ 9916typedef psinfo32_t elfcore_psinfo32_t; 9917#endif 9918#endif 9919 9920/* return a malloc'ed copy of a string at START which is at 9921 most MAX bytes long, possibly without a terminating '\0'. 9922 the copy will always have a terminating '\0'. */ 9923 9924char * 9925_bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) 9926{ 9927 char *dups; 9928 char *end = (char *) memchr (start, '\0', max); 9929 size_t len; 9930 9931 if (end == NULL) 9932 len = max; 9933 else 9934 len = end - start; 9935 9936 dups = (char *) bfd_alloc (abfd, len + 1); 9937 if (dups == NULL) 9938 return NULL; 9939 9940 memcpy (dups, start, len); 9941 dups[len] = '\0'; 9942 9943 return dups; 9944} 9945 9946#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 9947static bfd_boolean 9948elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) 9949{ 9950 if (note->descsz == sizeof (elfcore_psinfo_t)) 9951 { 9952 elfcore_psinfo_t psinfo; 9953 9954 memcpy (&psinfo, note->descdata, sizeof (psinfo)); 9955 9956#if defined (HAVE_PSINFO_T_PR_PID) || defined (HAVE_PRPSINFO_T_PR_PID) 9957 elf_tdata (abfd)->core->pid = psinfo.pr_pid; 9958#endif 9959 elf_tdata (abfd)->core->program 9960 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, 9961 sizeof (psinfo.pr_fname)); 9962 9963 elf_tdata (abfd)->core->command 9964 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, 9965 sizeof (psinfo.pr_psargs)); 9966 } 9967#if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) 9968 else if (note->descsz == sizeof (elfcore_psinfo32_t)) 9969 { 9970 /* 64-bit host, 32-bit corefile */ 9971 elfcore_psinfo32_t psinfo; 9972 9973 memcpy (&psinfo, note->descdata, sizeof (psinfo)); 9974 9975#if defined (HAVE_PSINFO32_T_PR_PID) || defined (HAVE_PRPSINFO32_T_PR_PID) 9976 elf_tdata (abfd)->core->pid = psinfo.pr_pid; 9977#endif 9978 elf_tdata (abfd)->core->program 9979 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, 9980 sizeof (psinfo.pr_fname)); 9981 9982 elf_tdata (abfd)->core->command 9983 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, 9984 sizeof (psinfo.pr_psargs)); 9985 } 9986#endif 9987 9988 else 9989 { 9990 /* Fail - we don't know how to handle any other 9991 note size (ie. data object type). */ 9992 return TRUE; 9993 } 9994 9995 /* Note that for some reason, a spurious space is tacked 9996 onto the end of the args in some (at least one anyway) 9997 implementations, so strip it off if it exists. */ 9998 9999 { 10000 char *command = elf_tdata (abfd)->core->command; 10001 int n = strlen (command); 10002 10003 if (0 < n && command[n - 1] == ' ') 10004 command[n - 1] = '\0'; 10005 } 10006 10007 return TRUE; 10008} 10009#endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ 10010 10011#if defined (HAVE_PSTATUS_T) 10012static bfd_boolean 10013elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) 10014{ 10015 if (note->descsz == sizeof (pstatus_t) 10016#if defined (HAVE_PXSTATUS_T) 10017 || note->descsz == sizeof (pxstatus_t) 10018#endif 10019 ) 10020 { 10021 pstatus_t pstat; 10022 10023 memcpy (&pstat, note->descdata, sizeof (pstat)); 10024 10025 elf_tdata (abfd)->core->pid = pstat.pr_pid; 10026 } 10027#if defined (HAVE_PSTATUS32_T) 10028 else if (note->descsz == sizeof (pstatus32_t)) 10029 { 10030 /* 64-bit host, 32-bit corefile */ 10031 pstatus32_t pstat; 10032 10033 memcpy (&pstat, note->descdata, sizeof (pstat)); 10034 10035 elf_tdata (abfd)->core->pid = pstat.pr_pid; 10036 } 10037#endif 10038 /* Could grab some more details from the "representative" 10039 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an 10040 NT_LWPSTATUS note, presumably. */ 10041 10042 return TRUE; 10043} 10044#endif /* defined (HAVE_PSTATUS_T) */ 10045 10046#if defined (HAVE_LWPSTATUS_T) 10047static bfd_boolean 10048elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) 10049{ 10050 lwpstatus_t lwpstat; 10051 char buf[100]; 10052 char *name; 10053 size_t len; 10054 asection *sect; 10055 10056 if (note->descsz != sizeof (lwpstat) 10057#if defined (HAVE_LWPXSTATUS_T) 10058 && note->descsz != sizeof (lwpxstatus_t) 10059#endif 10060 ) 10061 return TRUE; 10062 10063 memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); 10064 10065 elf_tdata (abfd)->core->lwpid = lwpstat.pr_lwpid; 10066 /* Do not overwrite the core signal if it has already been set by 10067 another thread. */ 10068 if (elf_tdata (abfd)->core->signal == 0) 10069 elf_tdata (abfd)->core->signal = lwpstat.pr_cursig; 10070 10071 /* Make a ".reg/999" section. */ 10072 10073 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); 10074 len = strlen (buf) + 1; 10075 name = bfd_alloc (abfd, len); 10076 if (name == NULL) 10077 return FALSE; 10078 memcpy (name, buf, len); 10079 10080 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 10081 if (sect == NULL) 10082 return FALSE; 10083 10084#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 10085 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); 10086 sect->filepos = note->descpos 10087 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); 10088#endif 10089 10090#if defined (HAVE_LWPSTATUS_T_PR_REG) 10091 sect->size = sizeof (lwpstat.pr_reg); 10092 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); 10093#endif 10094 10095 sect->alignment_power = 2; 10096 10097 if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) 10098 return FALSE; 10099 10100 /* Make a ".reg2/999" section */ 10101 10102 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); 10103 len = strlen (buf) + 1; 10104 name = bfd_alloc (abfd, len); 10105 if (name == NULL) 10106 return FALSE; 10107 memcpy (name, buf, len); 10108 10109 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 10110 if (sect == NULL) 10111 return FALSE; 10112 10113#if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 10114 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); 10115 sect->filepos = note->descpos 10116 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); 10117#endif 10118 10119#if defined (HAVE_LWPSTATUS_T_PR_FPREG) 10120 sect->size = sizeof (lwpstat.pr_fpreg); 10121 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); 10122#endif 10123 10124 sect->alignment_power = 2; 10125 10126 return elfcore_maybe_make_sect (abfd, ".reg2", sect); 10127} 10128#endif /* defined (HAVE_LWPSTATUS_T) */ 10129 10130/* These constants, and the structure offsets used below, are defined by 10131 Cygwin's core_dump.h */ 10132#define NOTE_INFO_PROCESS 1 10133#define NOTE_INFO_THREAD 2 10134#define NOTE_INFO_MODULE 3 10135#define NOTE_INFO_MODULE64 4 10136 10137static bfd_boolean 10138elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) 10139{ 10140 char buf[30]; 10141 char *name; 10142 size_t len; 10143 unsigned int name_size; 10144 asection *sect; 10145 unsigned int type; 10146 int is_active_thread; 10147 bfd_vma base_addr; 10148 10149 if (note->descsz < 4) 10150 return TRUE; 10151 10152 if (! CONST_STRNEQ (note->namedata, "win32")) 10153 return TRUE; 10154 10155 type = bfd_get_32 (abfd, note->descdata); 10156 10157 struct { 10158 const char *type_name; 10159 unsigned long min_size; 10160 } size_check[] = 10161 { 10162 { "NOTE_INFO_PROCESS", 12 }, 10163 { "NOTE_INFO_THREAD", 12 }, 10164 { "NOTE_INFO_MODULE", 12 }, 10165 { "NOTE_INFO_MODULE64", 16 }, 10166 }; 10167 10168 if (type > (sizeof(size_check)/sizeof(size_check[0]))) 10169 return TRUE; 10170 10171 if (note->descsz < size_check[type - 1].min_size) 10172 { 10173 _bfd_error_handler (_("%pB: warning: win32pstatus %s of size %lu bytes is too small"), 10174 abfd, size_check[type - 1].type_name, note->descsz); 10175 return TRUE; 10176 } 10177 10178 switch (type) 10179 { 10180 case NOTE_INFO_PROCESS: 10181 /* FIXME: need to add ->core->command. */ 10182 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 4); 10183 elf_tdata (abfd)->core->signal = bfd_get_32 (abfd, note->descdata + 8); 10184 break; 10185 10186 case NOTE_INFO_THREAD: 10187 /* Make a ".reg/<tid>" section containing the Win32 API thread CONTEXT 10188 structure. */ 10189 /* thread_info.tid */ 10190 sprintf (buf, ".reg/%ld", (long) bfd_get_32 (abfd, note->descdata + 4)); 10191 10192 len = strlen (buf) + 1; 10193 name = (char *) bfd_alloc (abfd, len); 10194 if (name == NULL) 10195 return FALSE; 10196 10197 memcpy (name, buf, len); 10198 10199 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 10200 if (sect == NULL) 10201 return FALSE; 10202 10203 /* sizeof (thread_info.thread_context) */ 10204 sect->size = note->descsz - 12; 10205 /* offsetof (thread_info.thread_context) */ 10206 sect->filepos = note->descpos + 12; 10207 sect->alignment_power = 2; 10208 10209 /* thread_info.is_active_thread */ 10210 is_active_thread = bfd_get_32 (abfd, note->descdata + 8); 10211 10212 if (is_active_thread) 10213 if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) 10214 return FALSE; 10215 break; 10216 10217 case NOTE_INFO_MODULE: 10218 case NOTE_INFO_MODULE64: 10219 /* Make a ".module/xxxxxxxx" section. */ 10220 if (type == NOTE_INFO_MODULE) 10221 { 10222 /* module_info.base_address */ 10223 base_addr = bfd_get_32 (abfd, note->descdata + 4); 10224 sprintf (buf, ".module/%08lx", (unsigned long) base_addr); 10225 /* module_info.module_name_size */ 10226 name_size = bfd_get_32 (abfd, note->descdata + 8); 10227 } 10228 else /* NOTE_INFO_MODULE64 */ 10229 { 10230 /* module_info.base_address */ 10231 base_addr = bfd_get_64 (abfd, note->descdata + 4); 10232 sprintf (buf, ".module/%016lx", (unsigned long) base_addr); 10233 /* module_info.module_name_size */ 10234 name_size = bfd_get_32 (abfd, note->descdata + 12); 10235 } 10236 10237 len = strlen (buf) + 1; 10238 name = (char *) bfd_alloc (abfd, len); 10239 if (name == NULL) 10240 return FALSE; 10241 10242 memcpy (name, buf, len); 10243 10244 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 10245 10246 if (sect == NULL) 10247 return FALSE; 10248 10249 if (note->descsz < 12 + name_size) 10250 { 10251 _bfd_error_handler (_("%pB: win32pstatus NOTE_INFO_MODULE of size %lu is too small to contain a name of size %u"), 10252 abfd, note->descsz, name_size); 10253 return TRUE; 10254 } 10255 10256 sect->size = note->descsz; 10257 sect->filepos = note->descpos; 10258 sect->alignment_power = 2; 10259 break; 10260 10261 default: 10262 return TRUE; 10263 } 10264 10265 return TRUE; 10266} 10267 10268static bfd_boolean 10269elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) 10270{ 10271 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 10272 10273 switch (note->type) 10274 { 10275 default: 10276 return TRUE; 10277 10278 case NT_PRSTATUS: 10279 if (bed->elf_backend_grok_prstatus) 10280 if ((*bed->elf_backend_grok_prstatus) (abfd, note)) 10281 return TRUE; 10282#if defined (HAVE_PRSTATUS_T) 10283 return elfcore_grok_prstatus (abfd, note); 10284#else 10285 return TRUE; 10286#endif 10287 10288#if defined (HAVE_PSTATUS_T) 10289 case NT_PSTATUS: 10290 return elfcore_grok_pstatus (abfd, note); 10291#endif 10292 10293#if defined (HAVE_LWPSTATUS_T) 10294 case NT_LWPSTATUS: 10295 return elfcore_grok_lwpstatus (abfd, note); 10296#endif 10297 10298 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ 10299 return elfcore_grok_prfpreg (abfd, note); 10300 10301 case NT_WIN32PSTATUS: 10302 return elfcore_grok_win32pstatus (abfd, note); 10303 10304 case NT_PRXFPREG: /* Linux SSE extension */ 10305 if (note->namesz == 6 10306 && strcmp (note->namedata, "LINUX") == 0) 10307 return elfcore_grok_prxfpreg (abfd, note); 10308 else 10309 return TRUE; 10310 10311 case NT_X86_XSTATE: /* Linux XSAVE extension */ 10312 if (note->namesz == 6 10313 && strcmp (note->namedata, "LINUX") == 0) 10314 return elfcore_grok_xstatereg (abfd, note); 10315 else 10316 return TRUE; 10317 10318 case NT_PPC_VMX: 10319 if (note->namesz == 6 10320 && strcmp (note->namedata, "LINUX") == 0) 10321 return elfcore_grok_ppc_vmx (abfd, note); 10322 else 10323 return TRUE; 10324 10325 case NT_PPC_VSX: 10326 if (note->namesz == 6 10327 && strcmp (note->namedata, "LINUX") == 0) 10328 return elfcore_grok_ppc_vsx (abfd, note); 10329 else 10330 return TRUE; 10331 10332 case NT_PPC_TAR: 10333 if (note->namesz == 6 10334 && strcmp (note->namedata, "LINUX") == 0) 10335 return elfcore_grok_ppc_tar (abfd, note); 10336 else 10337 return TRUE; 10338 10339 case NT_PPC_PPR: 10340 if (note->namesz == 6 10341 && strcmp (note->namedata, "LINUX") == 0) 10342 return elfcore_grok_ppc_ppr (abfd, note); 10343 else 10344 return TRUE; 10345 10346 case NT_PPC_DSCR: 10347 if (note->namesz == 6 10348 && strcmp (note->namedata, "LINUX") == 0) 10349 return elfcore_grok_ppc_dscr (abfd, note); 10350 else 10351 return TRUE; 10352 10353 case NT_PPC_EBB: 10354 if (note->namesz == 6 10355 && strcmp (note->namedata, "LINUX") == 0) 10356 return elfcore_grok_ppc_ebb (abfd, note); 10357 else 10358 return TRUE; 10359 10360 case NT_PPC_PMU: 10361 if (note->namesz == 6 10362 && strcmp (note->namedata, "LINUX") == 0) 10363 return elfcore_grok_ppc_pmu (abfd, note); 10364 else 10365 return TRUE; 10366 10367 case NT_PPC_TM_CGPR: 10368 if (note->namesz == 6 10369 && strcmp (note->namedata, "LINUX") == 0) 10370 return elfcore_grok_ppc_tm_cgpr (abfd, note); 10371 else 10372 return TRUE; 10373 10374 case NT_PPC_TM_CFPR: 10375 if (note->namesz == 6 10376 && strcmp (note->namedata, "LINUX") == 0) 10377 return elfcore_grok_ppc_tm_cfpr (abfd, note); 10378 else 10379 return TRUE; 10380 10381 case NT_PPC_TM_CVMX: 10382 if (note->namesz == 6 10383 && strcmp (note->namedata, "LINUX") == 0) 10384 return elfcore_grok_ppc_tm_cvmx (abfd, note); 10385 else 10386 return TRUE; 10387 10388 case NT_PPC_TM_CVSX: 10389 if (note->namesz == 6 10390 && strcmp (note->namedata, "LINUX") == 0) 10391 return elfcore_grok_ppc_tm_cvsx (abfd, note); 10392 else 10393 return TRUE; 10394 10395 case NT_PPC_TM_SPR: 10396 if (note->namesz == 6 10397 && strcmp (note->namedata, "LINUX") == 0) 10398 return elfcore_grok_ppc_tm_spr (abfd, note); 10399 else 10400 return TRUE; 10401 10402 case NT_PPC_TM_CTAR: 10403 if (note->namesz == 6 10404 && strcmp (note->namedata, "LINUX") == 0) 10405 return elfcore_grok_ppc_tm_ctar (abfd, note); 10406 else 10407 return TRUE; 10408 10409 case NT_PPC_TM_CPPR: 10410 if (note->namesz == 6 10411 && strcmp (note->namedata, "LINUX") == 0) 10412 return elfcore_grok_ppc_tm_cppr (abfd, note); 10413 else 10414 return TRUE; 10415 10416 case NT_PPC_TM_CDSCR: 10417 if (note->namesz == 6 10418 && strcmp (note->namedata, "LINUX") == 0) 10419 return elfcore_grok_ppc_tm_cdscr (abfd, note); 10420 else 10421 return TRUE; 10422 10423 case NT_S390_HIGH_GPRS: 10424 if (note->namesz == 6 10425 && strcmp (note->namedata, "LINUX") == 0) 10426 return elfcore_grok_s390_high_gprs (abfd, note); 10427 else 10428 return TRUE; 10429 10430 case NT_S390_TIMER: 10431 if (note->namesz == 6 10432 && strcmp (note->namedata, "LINUX") == 0) 10433 return elfcore_grok_s390_timer (abfd, note); 10434 else 10435 return TRUE; 10436 10437 case NT_S390_TODCMP: 10438 if (note->namesz == 6 10439 && strcmp (note->namedata, "LINUX") == 0) 10440 return elfcore_grok_s390_todcmp (abfd, note); 10441 else 10442 return TRUE; 10443 10444 case NT_S390_TODPREG: 10445 if (note->namesz == 6 10446 && strcmp (note->namedata, "LINUX") == 0) 10447 return elfcore_grok_s390_todpreg (abfd, note); 10448 else 10449 return TRUE; 10450 10451 case NT_S390_CTRS: 10452 if (note->namesz == 6 10453 && strcmp (note->namedata, "LINUX") == 0) 10454 return elfcore_grok_s390_ctrs (abfd, note); 10455 else 10456 return TRUE; 10457 10458 case NT_S390_PREFIX: 10459 if (note->namesz == 6 10460 && strcmp (note->namedata, "LINUX") == 0) 10461 return elfcore_grok_s390_prefix (abfd, note); 10462 else 10463 return TRUE; 10464 10465 case NT_S390_LAST_BREAK: 10466 if (note->namesz == 6 10467 && strcmp (note->namedata, "LINUX") == 0) 10468 return elfcore_grok_s390_last_break (abfd, note); 10469 else 10470 return TRUE; 10471 10472 case NT_S390_SYSTEM_CALL: 10473 if (note->namesz == 6 10474 && strcmp (note->namedata, "LINUX") == 0) 10475 return elfcore_grok_s390_system_call (abfd, note); 10476 else 10477 return TRUE; 10478 10479 case NT_S390_TDB: 10480 if (note->namesz == 6 10481 && strcmp (note->namedata, "LINUX") == 0) 10482 return elfcore_grok_s390_tdb (abfd, note); 10483 else 10484 return TRUE; 10485 10486 case NT_S390_VXRS_LOW: 10487 if (note->namesz == 6 10488 && strcmp (note->namedata, "LINUX") == 0) 10489 return elfcore_grok_s390_vxrs_low (abfd, note); 10490 else 10491 return TRUE; 10492 10493 case NT_S390_VXRS_HIGH: 10494 if (note->namesz == 6 10495 && strcmp (note->namedata, "LINUX") == 0) 10496 return elfcore_grok_s390_vxrs_high (abfd, note); 10497 else 10498 return TRUE; 10499 10500 case NT_S390_GS_CB: 10501 if (note->namesz == 6 10502 && strcmp (note->namedata, "LINUX") == 0) 10503 return elfcore_grok_s390_gs_cb (abfd, note); 10504 else 10505 return TRUE; 10506 10507 case NT_S390_GS_BC: 10508 if (note->namesz == 6 10509 && strcmp (note->namedata, "LINUX") == 0) 10510 return elfcore_grok_s390_gs_bc (abfd, note); 10511 else 10512 return TRUE; 10513 10514 case NT_ARC_V2: 10515 if (note->namesz == 6 10516 && strcmp (note->namedata, "LINUX") == 0) 10517 return elfcore_grok_arc_v2 (abfd, note); 10518 else 10519 return TRUE; 10520 10521 case NT_ARM_VFP: 10522 if (note->namesz == 6 10523 && strcmp (note->namedata, "LINUX") == 0) 10524 return elfcore_grok_arm_vfp (abfd, note); 10525 else 10526 return TRUE; 10527 10528 case NT_ARM_TLS: 10529 if (note->namesz == 6 10530 && strcmp (note->namedata, "LINUX") == 0) 10531 return elfcore_grok_aarch_tls (abfd, note); 10532 else 10533 return TRUE; 10534 10535 case NT_ARM_HW_BREAK: 10536 if (note->namesz == 6 10537 && strcmp (note->namedata, "LINUX") == 0) 10538 return elfcore_grok_aarch_hw_break (abfd, note); 10539 else 10540 return TRUE; 10541 10542 case NT_ARM_HW_WATCH: 10543 if (note->namesz == 6 10544 && strcmp (note->namedata, "LINUX") == 0) 10545 return elfcore_grok_aarch_hw_watch (abfd, note); 10546 else 10547 return TRUE; 10548 10549 case NT_ARM_SVE: 10550 if (note->namesz == 6 10551 && strcmp (note->namedata, "LINUX") == 0) 10552 return elfcore_grok_aarch_sve (abfd, note); 10553 else 10554 return TRUE; 10555 10556 case NT_ARM_PAC_MASK: 10557 if (note->namesz == 6 10558 && strcmp (note->namedata, "LINUX") == 0) 10559 return elfcore_grok_aarch_pauth (abfd, note); 10560 else 10561 return TRUE; 10562 10563 case NT_PRPSINFO: 10564 case NT_PSINFO: 10565 if (bed->elf_backend_grok_psinfo) 10566 if ((*bed->elf_backend_grok_psinfo) (abfd, note)) 10567 return TRUE; 10568#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 10569 return elfcore_grok_psinfo (abfd, note); 10570#else 10571 return TRUE; 10572#endif 10573 10574 case NT_AUXV: 10575 return elfcore_make_auxv_note_section (abfd, note, 0); 10576 10577 case NT_FILE: 10578 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.file", 10579 note); 10580 10581 case NT_SIGINFO: 10582 return elfcore_make_note_pseudosection (abfd, ".note.linuxcore.siginfo", 10583 note); 10584 10585 } 10586} 10587 10588static bfd_boolean 10589elfobj_grok_gnu_build_id (bfd *abfd, Elf_Internal_Note *note) 10590{ 10591 struct bfd_build_id* build_id; 10592 10593 if (note->descsz == 0) 10594 return FALSE; 10595 10596 build_id = bfd_alloc (abfd, sizeof (struct bfd_build_id) - 1 + note->descsz); 10597 if (build_id == NULL) 10598 return FALSE; 10599 10600 build_id->size = note->descsz; 10601 memcpy (build_id->data, note->descdata, note->descsz); 10602 abfd->build_id = build_id; 10603 10604 return TRUE; 10605} 10606 10607static bfd_boolean 10608elfobj_grok_gnu_note (bfd *abfd, Elf_Internal_Note *note) 10609{ 10610 switch (note->type) 10611 { 10612 default: 10613 return TRUE; 10614 10615 case NT_GNU_PROPERTY_TYPE_0: 10616 return _bfd_elf_parse_gnu_properties (abfd, note); 10617 10618 case NT_GNU_BUILD_ID: 10619 return elfobj_grok_gnu_build_id (abfd, note); 10620 } 10621} 10622 10623static bfd_boolean 10624elfobj_grok_stapsdt_note_1 (bfd *abfd, Elf_Internal_Note *note) 10625{ 10626 struct sdt_note *cur = 10627 (struct sdt_note *) bfd_alloc (abfd, 10628 sizeof (struct sdt_note) + note->descsz); 10629 10630 cur->next = (struct sdt_note *) (elf_tdata (abfd))->sdt_note_head; 10631 cur->size = (bfd_size_type) note->descsz; 10632 memcpy (cur->data, note->descdata, note->descsz); 10633 10634 elf_tdata (abfd)->sdt_note_head = cur; 10635 10636 return TRUE; 10637} 10638 10639static bfd_boolean 10640elfobj_grok_stapsdt_note (bfd *abfd, Elf_Internal_Note *note) 10641{ 10642 switch (note->type) 10643 { 10644 case NT_STAPSDT: 10645 return elfobj_grok_stapsdt_note_1 (abfd, note); 10646 10647 default: 10648 return TRUE; 10649 } 10650} 10651 10652static bfd_boolean 10653elfcore_grok_freebsd_psinfo (bfd *abfd, Elf_Internal_Note *note) 10654{ 10655 size_t offset; 10656 10657 switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) 10658 { 10659 case ELFCLASS32: 10660 if (note->descsz < 108) 10661 return FALSE; 10662 break; 10663 10664 case ELFCLASS64: 10665 if (note->descsz < 120) 10666 return FALSE; 10667 break; 10668 10669 default: 10670 return FALSE; 10671 } 10672 10673 /* Check for version 1 in pr_version. */ 10674 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) 10675 return FALSE; 10676 10677 offset = 4; 10678 10679 /* Skip over pr_psinfosz. */ 10680 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) 10681 offset += 4; 10682 else 10683 { 10684 offset += 4; /* Padding before pr_psinfosz. */ 10685 offset += 8; 10686 } 10687 10688 /* pr_fname is PRFNAMESZ (16) + 1 bytes in size. */ 10689 elf_tdata (abfd)->core->program 10690 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 17); 10691 offset += 17; 10692 10693 /* pr_psargs is PRARGSZ (80) + 1 bytes in size. */ 10694 elf_tdata (abfd)->core->command 10695 = _bfd_elfcore_strndup (abfd, note->descdata + offset, 81); 10696 offset += 81; 10697 10698 /* Padding before pr_pid. */ 10699 offset += 2; 10700 10701 /* The pr_pid field was added in version "1a". */ 10702 if (note->descsz < offset + 4) 10703 return TRUE; 10704 10705 elf_tdata (abfd)->core->pid 10706 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 10707 10708 return TRUE; 10709} 10710 10711static bfd_boolean 10712elfcore_grok_freebsd_prstatus (bfd *abfd, Elf_Internal_Note *note) 10713{ 10714 size_t offset; 10715 size_t size; 10716 size_t min_size; 10717 10718 /* Compute offset of pr_getregsz, skipping over pr_statussz. 10719 Also compute minimum size of this note. */ 10720 switch (elf_elfheader (abfd)->e_ident[EI_CLASS]) 10721 { 10722 case ELFCLASS32: 10723 offset = 4 + 4; 10724 min_size = offset + (4 * 2) + 4 + 4 + 4; 10725 break; 10726 10727 case ELFCLASS64: 10728 offset = 4 + 4 + 8; /* Includes padding before pr_statussz. */ 10729 min_size = offset + (8 * 2) + 4 + 4 + 4 + 4; 10730 break; 10731 10732 default: 10733 return FALSE; 10734 } 10735 10736 if (note->descsz < min_size) 10737 return FALSE; 10738 10739 /* Check for version 1 in pr_version. */ 10740 if (bfd_h_get_32 (abfd, (bfd_byte *) note->descdata) != 1) 10741 return FALSE; 10742 10743 /* Extract size of pr_reg from pr_gregsetsz. */ 10744 /* Skip over pr_gregsetsz and pr_fpregsetsz. */ 10745 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS32) 10746 { 10747 size = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 10748 offset += 4 * 2; 10749 } 10750 else 10751 { 10752 size = bfd_h_get_64 (abfd, (bfd_byte *) note->descdata + offset); 10753 offset += 8 * 2; 10754 } 10755 10756 /* Skip over pr_osreldate. */ 10757 offset += 4; 10758 10759 /* Read signal from pr_cursig. */ 10760 if (elf_tdata (abfd)->core->signal == 0) 10761 elf_tdata (abfd)->core->signal 10762 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 10763 offset += 4; 10764 10765 /* Read TID from pr_pid. */ 10766 elf_tdata (abfd)->core->lwpid 10767 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + offset); 10768 offset += 4; 10769 10770 /* Padding before pr_reg. */ 10771 if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64) 10772 offset += 4; 10773 10774 /* Make sure that there is enough data remaining in the note. */ 10775 if ((note->descsz - offset) < size) 10776 return FALSE; 10777 10778 /* Make a ".reg/999" section and a ".reg" section. */ 10779 return _bfd_elfcore_make_pseudosection (abfd, ".reg", 10780 size, note->descpos + offset); 10781} 10782 10783static bfd_boolean 10784elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note) 10785{ 10786 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 10787 10788 switch (note->type) 10789 { 10790 case NT_PRSTATUS: 10791 if (bed->elf_backend_grok_freebsd_prstatus) 10792 if ((*bed->elf_backend_grok_freebsd_prstatus) (abfd, note)) 10793 return TRUE; 10794 return elfcore_grok_freebsd_prstatus (abfd, note); 10795 10796 case NT_FPREGSET: 10797 return elfcore_grok_prfpreg (abfd, note); 10798 10799 case NT_PRPSINFO: 10800 return elfcore_grok_freebsd_psinfo (abfd, note); 10801 10802 case NT_FREEBSD_THRMISC: 10803 if (note->namesz == 8) 10804 return elfcore_make_note_pseudosection (abfd, ".thrmisc", note); 10805 else 10806 return TRUE; 10807 10808 case NT_FREEBSD_PROCSTAT_PROC: 10809 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc", 10810 note); 10811 10812 case NT_FREEBSD_PROCSTAT_FILES: 10813 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.files", 10814 note); 10815 10816 case NT_FREEBSD_PROCSTAT_VMMAP: 10817 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.vmmap", 10818 note); 10819 10820 case NT_FREEBSD_PROCSTAT_AUXV: 10821 return elfcore_make_auxv_note_section (abfd, note, 4); 10822 10823 case NT_X86_XSTATE: 10824 if (note->namesz == 8) 10825 return elfcore_grok_xstatereg (abfd, note); 10826 else 10827 return TRUE; 10828 10829 case NT_FREEBSD_PTLWPINFO: 10830 return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo", 10831 note); 10832 10833 case NT_ARM_VFP: 10834 return elfcore_grok_arm_vfp (abfd, note); 10835 10836 default: 10837 return TRUE; 10838 } 10839} 10840 10841static bfd_boolean 10842elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) 10843{ 10844 char *cp; 10845 10846 cp = strchr (note->namedata, '@'); 10847 if (cp != NULL) 10848 { 10849 *lwpidp = atoi(cp + 1); 10850 return TRUE; 10851 } 10852 return FALSE; 10853} 10854 10855static bfd_boolean 10856elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) 10857{ 10858 if (note->descsz <= 0x7c + 31) 10859 return FALSE; 10860 10861 /* Signal number at offset 0x08. */ 10862 elf_tdata (abfd)->core->signal 10863 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); 10864 10865 /* Process ID at offset 0x50. */ 10866 elf_tdata (abfd)->core->pid 10867 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); 10868 10869 /* Command name at 0x7c (max 32 bytes, including nul). */ 10870 elf_tdata (abfd)->core->command 10871 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); 10872 10873 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", 10874 note); 10875} 10876 10877static bfd_boolean 10878elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) 10879{ 10880 int lwp; 10881 10882 if (elfcore_netbsd_get_lwpid (note, &lwp)) 10883 elf_tdata (abfd)->core->lwpid = lwp; 10884 10885 switch (note->type) 10886 { 10887 case NT_NETBSDCORE_PROCINFO: 10888 /* NetBSD-specific core "procinfo". Note that we expect to 10889 find this note before any of the others, which is fine, 10890 since the kernel writes this note out first when it 10891 creates a core file. */ 10892 return elfcore_grok_netbsd_procinfo (abfd, note); 10893#ifdef NT_NETBSDCORE_AUXV 10894 case NT_NETBSDCORE_AUXV: 10895 /* NetBSD-specific Elf Auxiliary Vector data. */ 10896 return elfcore_make_auxv_note_section (abfd, note, 0); 10897#endif 10898#ifdef NT_NETBSDCORE_LWPSTATUS 10899 case NT_NETBSDCORE_LWPSTATUS: 10900 return elfcore_make_note_pseudosection (abfd, 10901 ".note.netbsdcore.lwpstatus", 10902 note); 10903#endif 10904 default: 10905 break; 10906 } 10907 10908 if (note->type == NT_NETBSDCORE_AUXV) 10909 { 10910 asection *sect = bfd_make_section_anyway_with_flags (abfd, ".auxv", 10911 SEC_HAS_CONTENTS); 10912 10913 if (sect == NULL) 10914 return FALSE; 10915 sect->size = note->descsz; 10916 sect->filepos = note->descpos; 10917 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 10918 10919 return TRUE; 10920 } 10921 10922 /* As of March 2020 there are no other machine-independent notes 10923 defined for NetBSD core files. If the note type is less 10924 than the start of the machine-dependent note types, we don't 10925 understand it. */ 10926 10927 if (note->type < NT_NETBSDCORE_FIRSTMACH) 10928 return TRUE; 10929 10930 10931 switch (bfd_get_arch (abfd)) 10932 { 10933 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and 10934 PT_GETFPREGS == mach+2. */ 10935 10936 case bfd_arch_aarch64: 10937 case bfd_arch_alpha: 10938 case bfd_arch_sparc: 10939 switch (note->type) 10940 { 10941 case NT_NETBSDCORE_FIRSTMACH+0: 10942 return elfcore_make_note_pseudosection (abfd, ".reg", note); 10943 10944 case NT_NETBSDCORE_FIRSTMACH+2: 10945 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 10946 10947 default: 10948 return TRUE; 10949 } 10950 10951 /* On SuperH, PT_GETREGS == mach+3 and PT_GETFPREGS == mach+5. 10952 There's also old PT___GETREGS40 == mach + 1 for old reg 10953 structure which lacks GBR. */ 10954 10955 case bfd_arch_sh: 10956 switch (note->type) 10957 { 10958 case NT_NETBSDCORE_FIRSTMACH+3: 10959 return elfcore_make_note_pseudosection (abfd, ".reg", note); 10960 10961 case NT_NETBSDCORE_FIRSTMACH+5: 10962 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 10963 10964 default: 10965 return TRUE; 10966 } 10967 10968 /* On all other arch's, PT_GETREGS == mach+1 and 10969 PT_GETFPREGS == mach+3. */ 10970 10971 default: 10972 switch (note->type) 10973 { 10974 case NT_NETBSDCORE_FIRSTMACH+1: 10975 return elfcore_make_note_pseudosection (abfd, ".reg", note); 10976 10977 case NT_NETBSDCORE_FIRSTMACH+3: 10978 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 10979 10980 default: 10981 return TRUE; 10982 } 10983 } 10984 /* NOTREACHED */ 10985} 10986 10987static bfd_boolean 10988elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) 10989{ 10990 if (note->descsz <= 0x48 + 31) 10991 return FALSE; 10992 10993 /* Signal number at offset 0x08. */ 10994 elf_tdata (abfd)->core->signal 10995 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); 10996 10997 /* Process ID at offset 0x20. */ 10998 elf_tdata (abfd)->core->pid 10999 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); 11000 11001 /* Command name at 0x48 (max 32 bytes, including nul). */ 11002 elf_tdata (abfd)->core->command 11003 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); 11004 11005 return TRUE; 11006} 11007 11008static bfd_boolean 11009elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) 11010{ 11011 if (note->type == NT_OPENBSD_PROCINFO) 11012 return elfcore_grok_openbsd_procinfo (abfd, note); 11013 11014 if (note->type == NT_OPENBSD_REGS) 11015 return elfcore_make_note_pseudosection (abfd, ".reg", note); 11016 11017 if (note->type == NT_OPENBSD_FPREGS) 11018 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 11019 11020 if (note->type == NT_OPENBSD_XFPREGS) 11021 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); 11022 11023 if (note->type == NT_OPENBSD_AUXV) 11024 return elfcore_make_auxv_note_section (abfd, note, 0); 11025 11026 return TRUE; 11027} 11028 11029static bfd_boolean 11030elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, long *tid) 11031{ 11032 void *ddata = note->descdata; 11033 char buf[100]; 11034 char *name; 11035 asection *sect; 11036 short sig; 11037 unsigned flags; 11038 11039 if (note->descsz < 16) 11040 return FALSE; 11041 11042 /* nto_procfs_status 'pid' field is at offset 0. */ 11043 elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, (bfd_byte *) ddata); 11044 11045 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ 11046 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); 11047 11048 /* nto_procfs_status 'flags' field is at offset 8. */ 11049 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); 11050 11051 /* nto_procfs_status 'what' field is at offset 14. */ 11052 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) 11053 { 11054 elf_tdata (abfd)->core->signal = sig; 11055 elf_tdata (abfd)->core->lwpid = *tid; 11056 } 11057 11058 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores 11059 do not come from signals so we make sure we set the current 11060 thread just in case. */ 11061 if (flags & 0x00000080) 11062 elf_tdata (abfd)->core->lwpid = *tid; 11063 11064 /* Make a ".qnx_core_status/%d" section. */ 11065 sprintf (buf, ".qnx_core_status/%ld", *tid); 11066 11067 name = (char *) bfd_alloc (abfd, strlen (buf) + 1); 11068 if (name == NULL) 11069 return FALSE; 11070 strcpy (name, buf); 11071 11072 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 11073 if (sect == NULL) 11074 return FALSE; 11075 11076 sect->size = note->descsz; 11077 sect->filepos = note->descpos; 11078 sect->alignment_power = 2; 11079 11080 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); 11081} 11082 11083static bfd_boolean 11084elfcore_grok_nto_regs (bfd *abfd, 11085 Elf_Internal_Note *note, 11086 long tid, 11087 char *base) 11088{ 11089 char buf[100]; 11090 char *name; 11091 asection *sect; 11092 11093 /* Make a "(base)/%d" section. */ 11094 sprintf (buf, "%s/%ld", base, tid); 11095 11096 name = (char *) bfd_alloc (abfd, strlen (buf) + 1); 11097 if (name == NULL) 11098 return FALSE; 11099 strcpy (name, buf); 11100 11101 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 11102 if (sect == NULL) 11103 return FALSE; 11104 11105 sect->size = note->descsz; 11106 sect->filepos = note->descpos; 11107 sect->alignment_power = 2; 11108 11109 /* This is the current thread. */ 11110 if (elf_tdata (abfd)->core->lwpid == tid) 11111 return elfcore_maybe_make_sect (abfd, base, sect); 11112 11113 return TRUE; 11114} 11115 11116#define BFD_QNT_CORE_INFO 7 11117#define BFD_QNT_CORE_STATUS 8 11118#define BFD_QNT_CORE_GREG 9 11119#define BFD_QNT_CORE_FPREG 10 11120 11121static bfd_boolean 11122elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) 11123{ 11124 /* Every GREG section has a STATUS section before it. Store the 11125 tid from the previous call to pass down to the next gregs 11126 function. */ 11127 static long tid = 1; 11128 11129 switch (note->type) 11130 { 11131 case BFD_QNT_CORE_INFO: 11132 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); 11133 case BFD_QNT_CORE_STATUS: 11134 return elfcore_grok_nto_status (abfd, note, &tid); 11135 case BFD_QNT_CORE_GREG: 11136 return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); 11137 case BFD_QNT_CORE_FPREG: 11138 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); 11139 default: 11140 return TRUE; 11141 } 11142} 11143 11144static bfd_boolean 11145elfcore_grok_spu_note (bfd *abfd, Elf_Internal_Note *note) 11146{ 11147 char *name; 11148 asection *sect; 11149 size_t len; 11150 11151 /* Use note name as section name. */ 11152 len = note->namesz; 11153 name = (char *) bfd_alloc (abfd, len); 11154 if (name == NULL) 11155 return FALSE; 11156 memcpy (name, note->namedata, len); 11157 name[len - 1] = '\0'; 11158 11159 sect = bfd_make_section_anyway_with_flags (abfd, name, SEC_HAS_CONTENTS); 11160 if (sect == NULL) 11161 return FALSE; 11162 11163 sect->size = note->descsz; 11164 sect->filepos = note->descpos; 11165 sect->alignment_power = 1; 11166 11167 return TRUE; 11168} 11169 11170/* Function: elfcore_write_note 11171 11172 Inputs: 11173 buffer to hold note, and current size of buffer 11174 name of note 11175 type of note 11176 data for note 11177 size of data for note 11178 11179 Writes note to end of buffer. ELF64 notes are written exactly as 11180 for ELF32, despite the current (as of 2006) ELF gabi specifying 11181 that they ought to have 8-byte namesz and descsz field, and have 11182 8-byte alignment. Other writers, eg. Linux kernel, do the same. 11183 11184 Return: 11185 Pointer to realloc'd buffer, *BUFSIZ updated. */ 11186 11187char * 11188elfcore_write_note (bfd *abfd, 11189 char *buf, 11190 int *bufsiz, 11191 const char *name, 11192 int type, 11193 const void *input, 11194 int size) 11195{ 11196 Elf_External_Note *xnp; 11197 size_t namesz; 11198 size_t newspace; 11199 char *dest; 11200 11201 namesz = 0; 11202 if (name != NULL) 11203 namesz = strlen (name) + 1; 11204 11205 newspace = 12 + ((namesz + 3) & -4) + ((size + 3) & -4); 11206 11207 buf = (char *) realloc (buf, *bufsiz + newspace); 11208 if (buf == NULL) 11209 return buf; 11210 dest = buf + *bufsiz; 11211 *bufsiz += newspace; 11212 xnp = (Elf_External_Note *) dest; 11213 H_PUT_32 (abfd, namesz, xnp->namesz); 11214 H_PUT_32 (abfd, size, xnp->descsz); 11215 H_PUT_32 (abfd, type, xnp->type); 11216 dest = xnp->name; 11217 if (name != NULL) 11218 { 11219 memcpy (dest, name, namesz); 11220 dest += namesz; 11221 while (namesz & 3) 11222 { 11223 *dest++ = '\0'; 11224 ++namesz; 11225 } 11226 } 11227 memcpy (dest, input, size); 11228 dest += size; 11229 while (size & 3) 11230 { 11231 *dest++ = '\0'; 11232 ++size; 11233 } 11234 return buf; 11235} 11236 11237/* gcc-8 warns (*) on all the strncpy calls in this function about 11238 possible string truncation. The "truncation" is not a bug. We 11239 have an external representation of structs with fields that are not 11240 necessarily NULL terminated and corresponding internal 11241 representation fields that are one larger so that they can always 11242 be NULL terminated. 11243 gcc versions between 4.2 and 4.6 do not allow pragma control of 11244 diagnostics inside functions, giving a hard error if you try to use 11245 the finer control available with later versions. 11246 gcc prior to 4.2 warns about diagnostic push and pop. 11247 gcc-5, gcc-6 and gcc-7 warn that -Wstringop-truncation is unknown, 11248 unless you also add #pragma GCC diagnostic ignored "-Wpragma". 11249 (*) Depending on your system header files! */ 11250#if GCC_VERSION >= 8000 11251# pragma GCC diagnostic push 11252# pragma GCC diagnostic ignored "-Wstringop-truncation" 11253#endif 11254char * 11255elfcore_write_prpsinfo (bfd *abfd, 11256 char *buf, 11257 int *bufsiz, 11258 const char *fname, 11259 const char *psargs) 11260{ 11261 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11262 11263 if (bed->elf_backend_write_core_note != NULL) 11264 { 11265 char *ret; 11266 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, 11267 NT_PRPSINFO, fname, psargs); 11268 if (ret != NULL) 11269 return ret; 11270 } 11271 11272#if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 11273# if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) 11274 if (bed->s->elfclass == ELFCLASS32) 11275 { 11276# if defined (HAVE_PSINFO32_T) 11277 psinfo32_t data; 11278 int note_type = NT_PSINFO; 11279# else 11280 prpsinfo32_t data; 11281 int note_type = NT_PRPSINFO; 11282# endif 11283 11284 memset (&data, 0, sizeof (data)); 11285 strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); 11286 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); 11287 return elfcore_write_note (abfd, buf, bufsiz, 11288 "CORE", note_type, &data, sizeof (data)); 11289 } 11290 else 11291# endif 11292 { 11293# if defined (HAVE_PSINFO_T) 11294 psinfo_t data; 11295 int note_type = NT_PSINFO; 11296# else 11297 prpsinfo_t data; 11298 int note_type = NT_PRPSINFO; 11299# endif 11300 11301 memset (&data, 0, sizeof (data)); 11302 strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); 11303 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); 11304 return elfcore_write_note (abfd, buf, bufsiz, 11305 "CORE", note_type, &data, sizeof (data)); 11306 } 11307#endif /* PSINFO_T or PRPSINFO_T */ 11308 11309 free (buf); 11310 return NULL; 11311} 11312#if GCC_VERSION >= 8000 11313# pragma GCC diagnostic pop 11314#endif 11315 11316char * 11317elfcore_write_linux_prpsinfo32 11318 (bfd *abfd, char *buf, int *bufsiz, 11319 const struct elf_internal_linux_prpsinfo *prpsinfo) 11320{ 11321 if (get_elf_backend_data (abfd)->linux_prpsinfo32_ugid16) 11322 { 11323 struct elf_external_linux_prpsinfo32_ugid16 data; 11324 11325 swap_linux_prpsinfo32_ugid16_out (abfd, prpsinfo, &data); 11326 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, 11327 &data, sizeof (data)); 11328 } 11329 else 11330 { 11331 struct elf_external_linux_prpsinfo32_ugid32 data; 11332 11333 swap_linux_prpsinfo32_ugid32_out (abfd, prpsinfo, &data); 11334 return elfcore_write_note (abfd, buf, bufsiz, "CORE", NT_PRPSINFO, 11335 &data, sizeof (data)); 11336 } 11337} 11338 11339char * 11340elfcore_write_linux_prpsinfo64 11341 (bfd *abfd, char *buf, int *bufsiz, 11342 const struct elf_internal_linux_prpsinfo *prpsinfo) 11343{ 11344 if (get_elf_backend_data (abfd)->linux_prpsinfo64_ugid16) 11345 { 11346 struct elf_external_linux_prpsinfo64_ugid16 data; 11347 11348 swap_linux_prpsinfo64_ugid16_out (abfd, prpsinfo, &data); 11349 return elfcore_write_note (abfd, buf, bufsiz, 11350 "CORE", NT_PRPSINFO, &data, sizeof (data)); 11351 } 11352 else 11353 { 11354 struct elf_external_linux_prpsinfo64_ugid32 data; 11355 11356 swap_linux_prpsinfo64_ugid32_out (abfd, prpsinfo, &data); 11357 return elfcore_write_note (abfd, buf, bufsiz, 11358 "CORE", NT_PRPSINFO, &data, sizeof (data)); 11359 } 11360} 11361 11362char * 11363elfcore_write_prstatus (bfd *abfd, 11364 char *buf, 11365 int *bufsiz, 11366 long pid, 11367 int cursig, 11368 const void *gregs) 11369{ 11370 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11371 11372 if (bed->elf_backend_write_core_note != NULL) 11373 { 11374 char *ret; 11375 ret = (*bed->elf_backend_write_core_note) (abfd, buf, bufsiz, 11376 NT_PRSTATUS, 11377 pid, cursig, gregs); 11378 if (ret != NULL) 11379 return ret; 11380 } 11381 11382#if defined (HAVE_PRSTATUS_T) 11383#if defined (HAVE_PRSTATUS32_T) 11384 if (bed->s->elfclass == ELFCLASS32) 11385 { 11386 prstatus32_t prstat; 11387 11388 memset (&prstat, 0, sizeof (prstat)); 11389 prstat.pr_pid = pid; 11390 prstat.pr_cursig = cursig; 11391 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); 11392 return elfcore_write_note (abfd, buf, bufsiz, "CORE", 11393 NT_PRSTATUS, &prstat, sizeof (prstat)); 11394 } 11395 else 11396#endif 11397 { 11398 prstatus_t prstat; 11399 11400 memset (&prstat, 0, sizeof (prstat)); 11401 prstat.pr_pid = pid; 11402 prstat.pr_cursig = cursig; 11403 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); 11404 return elfcore_write_note (abfd, buf, bufsiz, "CORE", 11405 NT_PRSTATUS, &prstat, sizeof (prstat)); 11406 } 11407#endif /* HAVE_PRSTATUS_T */ 11408 11409 free (buf); 11410 return NULL; 11411} 11412 11413#if defined (HAVE_LWPSTATUS_T) 11414char * 11415elfcore_write_lwpstatus (bfd *abfd, 11416 char *buf, 11417 int *bufsiz, 11418 long pid, 11419 int cursig, 11420 const void *gregs) 11421{ 11422 lwpstatus_t lwpstat; 11423 const char *note_name = "CORE"; 11424 11425 memset (&lwpstat, 0, sizeof (lwpstat)); 11426 lwpstat.pr_lwpid = pid >> 16; 11427 lwpstat.pr_cursig = cursig; 11428#if defined (HAVE_LWPSTATUS_T_PR_REG) 11429 memcpy (&lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); 11430#elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 11431#if !defined(gregs) 11432 memcpy (lwpstat.pr_context.uc_mcontext.gregs, 11433 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); 11434#else 11435 memcpy (lwpstat.pr_context.uc_mcontext.__gregs, 11436 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); 11437#endif 11438#endif 11439 return elfcore_write_note (abfd, buf, bufsiz, note_name, 11440 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); 11441} 11442#endif /* HAVE_LWPSTATUS_T */ 11443 11444#if defined (HAVE_PSTATUS_T) 11445char * 11446elfcore_write_pstatus (bfd *abfd, 11447 char *buf, 11448 int *bufsiz, 11449 long pid, 11450 int cursig ATTRIBUTE_UNUSED, 11451 const void *gregs ATTRIBUTE_UNUSED) 11452{ 11453 const char *note_name = "CORE"; 11454#if defined (HAVE_PSTATUS32_T) 11455 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11456 11457 if (bed->s->elfclass == ELFCLASS32) 11458 { 11459 pstatus32_t pstat; 11460 11461 memset (&pstat, 0, sizeof (pstat)); 11462 pstat.pr_pid = pid & 0xffff; 11463 buf = elfcore_write_note (abfd, buf, bufsiz, note_name, 11464 NT_PSTATUS, &pstat, sizeof (pstat)); 11465 return buf; 11466 } 11467 else 11468#endif 11469 { 11470 pstatus_t pstat; 11471 11472 memset (&pstat, 0, sizeof (pstat)); 11473 pstat.pr_pid = pid & 0xffff; 11474 buf = elfcore_write_note (abfd, buf, bufsiz, note_name, 11475 NT_PSTATUS, &pstat, sizeof (pstat)); 11476 return buf; 11477 } 11478} 11479#endif /* HAVE_PSTATUS_T */ 11480 11481char * 11482elfcore_write_prfpreg (bfd *abfd, 11483 char *buf, 11484 int *bufsiz, 11485 const void *fpregs, 11486 int size) 11487{ 11488 const char *note_name = "CORE"; 11489 return elfcore_write_note (abfd, buf, bufsiz, 11490 note_name, NT_FPREGSET, fpregs, size); 11491} 11492 11493char * 11494elfcore_write_prxfpreg (bfd *abfd, 11495 char *buf, 11496 int *bufsiz, 11497 const void *xfpregs, 11498 int size) 11499{ 11500 char *note_name = "LINUX"; 11501 return elfcore_write_note (abfd, buf, bufsiz, 11502 note_name, NT_PRXFPREG, xfpregs, size); 11503} 11504 11505char * 11506elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz, 11507 const void *xfpregs, int size) 11508{ 11509 char *note_name; 11510 if (get_elf_backend_data (abfd)->elf_osabi == ELFOSABI_FREEBSD) 11511 note_name = "FreeBSD"; 11512 else 11513 note_name = "LINUX"; 11514 return elfcore_write_note (abfd, buf, bufsiz, 11515 note_name, NT_X86_XSTATE, xfpregs, size); 11516} 11517 11518char * 11519elfcore_write_ppc_vmx (bfd *abfd, 11520 char *buf, 11521 int *bufsiz, 11522 const void *ppc_vmx, 11523 int size) 11524{ 11525 char *note_name = "LINUX"; 11526 return elfcore_write_note (abfd, buf, bufsiz, 11527 note_name, NT_PPC_VMX, ppc_vmx, size); 11528} 11529 11530char * 11531elfcore_write_ppc_vsx (bfd *abfd, 11532 char *buf, 11533 int *bufsiz, 11534 const void *ppc_vsx, 11535 int size) 11536{ 11537 char *note_name = "LINUX"; 11538 return elfcore_write_note (abfd, buf, bufsiz, 11539 note_name, NT_PPC_VSX, ppc_vsx, size); 11540} 11541 11542char * 11543elfcore_write_ppc_tar (bfd *abfd, 11544 char *buf, 11545 int *bufsiz, 11546 const void *ppc_tar, 11547 int size) 11548{ 11549 char *note_name = "LINUX"; 11550 return elfcore_write_note (abfd, buf, bufsiz, 11551 note_name, NT_PPC_TAR, ppc_tar, size); 11552} 11553 11554char * 11555elfcore_write_ppc_ppr (bfd *abfd, 11556 char *buf, 11557 int *bufsiz, 11558 const void *ppc_ppr, 11559 int size) 11560{ 11561 char *note_name = "LINUX"; 11562 return elfcore_write_note (abfd, buf, bufsiz, 11563 note_name, NT_PPC_PPR, ppc_ppr, size); 11564} 11565 11566char * 11567elfcore_write_ppc_dscr (bfd *abfd, 11568 char *buf, 11569 int *bufsiz, 11570 const void *ppc_dscr, 11571 int size) 11572{ 11573 char *note_name = "LINUX"; 11574 return elfcore_write_note (abfd, buf, bufsiz, 11575 note_name, NT_PPC_DSCR, ppc_dscr, size); 11576} 11577 11578char * 11579elfcore_write_ppc_ebb (bfd *abfd, 11580 char *buf, 11581 int *bufsiz, 11582 const void *ppc_ebb, 11583 int size) 11584{ 11585 char *note_name = "LINUX"; 11586 return elfcore_write_note (abfd, buf, bufsiz, 11587 note_name, NT_PPC_EBB, ppc_ebb, size); 11588} 11589 11590char * 11591elfcore_write_ppc_pmu (bfd *abfd, 11592 char *buf, 11593 int *bufsiz, 11594 const void *ppc_pmu, 11595 int size) 11596{ 11597 char *note_name = "LINUX"; 11598 return elfcore_write_note (abfd, buf, bufsiz, 11599 note_name, NT_PPC_PMU, ppc_pmu, size); 11600} 11601 11602char * 11603elfcore_write_ppc_tm_cgpr (bfd *abfd, 11604 char *buf, 11605 int *bufsiz, 11606 const void *ppc_tm_cgpr, 11607 int size) 11608{ 11609 char *note_name = "LINUX"; 11610 return elfcore_write_note (abfd, buf, bufsiz, 11611 note_name, NT_PPC_TM_CGPR, ppc_tm_cgpr, size); 11612} 11613 11614char * 11615elfcore_write_ppc_tm_cfpr (bfd *abfd, 11616 char *buf, 11617 int *bufsiz, 11618 const void *ppc_tm_cfpr, 11619 int size) 11620{ 11621 char *note_name = "LINUX"; 11622 return elfcore_write_note (abfd, buf, bufsiz, 11623 note_name, NT_PPC_TM_CFPR, ppc_tm_cfpr, size); 11624} 11625 11626char * 11627elfcore_write_ppc_tm_cvmx (bfd *abfd, 11628 char *buf, 11629 int *bufsiz, 11630 const void *ppc_tm_cvmx, 11631 int size) 11632{ 11633 char *note_name = "LINUX"; 11634 return elfcore_write_note (abfd, buf, bufsiz, 11635 note_name, NT_PPC_TM_CVMX, ppc_tm_cvmx, size); 11636} 11637 11638char * 11639elfcore_write_ppc_tm_cvsx (bfd *abfd, 11640 char *buf, 11641 int *bufsiz, 11642 const void *ppc_tm_cvsx, 11643 int size) 11644{ 11645 char *note_name = "LINUX"; 11646 return elfcore_write_note (abfd, buf, bufsiz, 11647 note_name, NT_PPC_TM_CVSX, ppc_tm_cvsx, size); 11648} 11649 11650char * 11651elfcore_write_ppc_tm_spr (bfd *abfd, 11652 char *buf, 11653 int *bufsiz, 11654 const void *ppc_tm_spr, 11655 int size) 11656{ 11657 char *note_name = "LINUX"; 11658 return elfcore_write_note (abfd, buf, bufsiz, 11659 note_name, NT_PPC_TM_SPR, ppc_tm_spr, size); 11660} 11661 11662char * 11663elfcore_write_ppc_tm_ctar (bfd *abfd, 11664 char *buf, 11665 int *bufsiz, 11666 const void *ppc_tm_ctar, 11667 int size) 11668{ 11669 char *note_name = "LINUX"; 11670 return elfcore_write_note (abfd, buf, bufsiz, 11671 note_name, NT_PPC_TM_CTAR, ppc_tm_ctar, size); 11672} 11673 11674char * 11675elfcore_write_ppc_tm_cppr (bfd *abfd, 11676 char *buf, 11677 int *bufsiz, 11678 const void *ppc_tm_cppr, 11679 int size) 11680{ 11681 char *note_name = "LINUX"; 11682 return elfcore_write_note (abfd, buf, bufsiz, 11683 note_name, NT_PPC_TM_CPPR, ppc_tm_cppr, size); 11684} 11685 11686char * 11687elfcore_write_ppc_tm_cdscr (bfd *abfd, 11688 char *buf, 11689 int *bufsiz, 11690 const void *ppc_tm_cdscr, 11691 int size) 11692{ 11693 char *note_name = "LINUX"; 11694 return elfcore_write_note (abfd, buf, bufsiz, 11695 note_name, NT_PPC_TM_CDSCR, ppc_tm_cdscr, size); 11696} 11697 11698static char * 11699elfcore_write_s390_high_gprs (bfd *abfd, 11700 char *buf, 11701 int *bufsiz, 11702 const void *s390_high_gprs, 11703 int size) 11704{ 11705 char *note_name = "LINUX"; 11706 return elfcore_write_note (abfd, buf, bufsiz, 11707 note_name, NT_S390_HIGH_GPRS, 11708 s390_high_gprs, size); 11709} 11710 11711char * 11712elfcore_write_s390_timer (bfd *abfd, 11713 char *buf, 11714 int *bufsiz, 11715 const void *s390_timer, 11716 int size) 11717{ 11718 char *note_name = "LINUX"; 11719 return elfcore_write_note (abfd, buf, bufsiz, 11720 note_name, NT_S390_TIMER, s390_timer, size); 11721} 11722 11723char * 11724elfcore_write_s390_todcmp (bfd *abfd, 11725 char *buf, 11726 int *bufsiz, 11727 const void *s390_todcmp, 11728 int size) 11729{ 11730 char *note_name = "LINUX"; 11731 return elfcore_write_note (abfd, buf, bufsiz, 11732 note_name, NT_S390_TODCMP, s390_todcmp, size); 11733} 11734 11735char * 11736elfcore_write_s390_todpreg (bfd *abfd, 11737 char *buf, 11738 int *bufsiz, 11739 const void *s390_todpreg, 11740 int size) 11741{ 11742 char *note_name = "LINUX"; 11743 return elfcore_write_note (abfd, buf, bufsiz, 11744 note_name, NT_S390_TODPREG, s390_todpreg, size); 11745} 11746 11747char * 11748elfcore_write_s390_ctrs (bfd *abfd, 11749 char *buf, 11750 int *bufsiz, 11751 const void *s390_ctrs, 11752 int size) 11753{ 11754 char *note_name = "LINUX"; 11755 return elfcore_write_note (abfd, buf, bufsiz, 11756 note_name, NT_S390_CTRS, s390_ctrs, size); 11757} 11758 11759char * 11760elfcore_write_s390_prefix (bfd *abfd, 11761 char *buf, 11762 int *bufsiz, 11763 const void *s390_prefix, 11764 int size) 11765{ 11766 char *note_name = "LINUX"; 11767 return elfcore_write_note (abfd, buf, bufsiz, 11768 note_name, NT_S390_PREFIX, s390_prefix, size); 11769} 11770 11771char * 11772elfcore_write_s390_last_break (bfd *abfd, 11773 char *buf, 11774 int *bufsiz, 11775 const void *s390_last_break, 11776 int size) 11777{ 11778 char *note_name = "LINUX"; 11779 return elfcore_write_note (abfd, buf, bufsiz, 11780 note_name, NT_S390_LAST_BREAK, 11781 s390_last_break, size); 11782} 11783 11784char * 11785elfcore_write_s390_system_call (bfd *abfd, 11786 char *buf, 11787 int *bufsiz, 11788 const void *s390_system_call, 11789 int size) 11790{ 11791 char *note_name = "LINUX"; 11792 return elfcore_write_note (abfd, buf, bufsiz, 11793 note_name, NT_S390_SYSTEM_CALL, 11794 s390_system_call, size); 11795} 11796 11797char * 11798elfcore_write_s390_tdb (bfd *abfd, 11799 char *buf, 11800 int *bufsiz, 11801 const void *s390_tdb, 11802 int size) 11803{ 11804 char *note_name = "LINUX"; 11805 return elfcore_write_note (abfd, buf, bufsiz, 11806 note_name, NT_S390_TDB, s390_tdb, size); 11807} 11808 11809char * 11810elfcore_write_s390_vxrs_low (bfd *abfd, 11811 char *buf, 11812 int *bufsiz, 11813 const void *s390_vxrs_low, 11814 int size) 11815{ 11816 char *note_name = "LINUX"; 11817 return elfcore_write_note (abfd, buf, bufsiz, 11818 note_name, NT_S390_VXRS_LOW, s390_vxrs_low, size); 11819} 11820 11821char * 11822elfcore_write_s390_vxrs_high (bfd *abfd, 11823 char *buf, 11824 int *bufsiz, 11825 const void *s390_vxrs_high, 11826 int size) 11827{ 11828 char *note_name = "LINUX"; 11829 return elfcore_write_note (abfd, buf, bufsiz, 11830 note_name, NT_S390_VXRS_HIGH, 11831 s390_vxrs_high, size); 11832} 11833 11834char * 11835elfcore_write_s390_gs_cb (bfd *abfd, 11836 char *buf, 11837 int *bufsiz, 11838 const void *s390_gs_cb, 11839 int size) 11840{ 11841 char *note_name = "LINUX"; 11842 return elfcore_write_note (abfd, buf, bufsiz, 11843 note_name, NT_S390_GS_CB, 11844 s390_gs_cb, size); 11845} 11846 11847char * 11848elfcore_write_s390_gs_bc (bfd *abfd, 11849 char *buf, 11850 int *bufsiz, 11851 const void *s390_gs_bc, 11852 int size) 11853{ 11854 char *note_name = "LINUX"; 11855 return elfcore_write_note (abfd, buf, bufsiz, 11856 note_name, NT_S390_GS_BC, 11857 s390_gs_bc, size); 11858} 11859 11860char * 11861elfcore_write_arm_vfp (bfd *abfd, 11862 char *buf, 11863 int *bufsiz, 11864 const void *arm_vfp, 11865 int size) 11866{ 11867 char *note_name = "LINUX"; 11868 return elfcore_write_note (abfd, buf, bufsiz, 11869 note_name, NT_ARM_VFP, arm_vfp, size); 11870} 11871 11872char * 11873elfcore_write_aarch_tls (bfd *abfd, 11874 char *buf, 11875 int *bufsiz, 11876 const void *aarch_tls, 11877 int size) 11878{ 11879 char *note_name = "LINUX"; 11880 return elfcore_write_note (abfd, buf, bufsiz, 11881 note_name, NT_ARM_TLS, aarch_tls, size); 11882} 11883 11884char * 11885elfcore_write_aarch_hw_break (bfd *abfd, 11886 char *buf, 11887 int *bufsiz, 11888 const void *aarch_hw_break, 11889 int size) 11890{ 11891 char *note_name = "LINUX"; 11892 return elfcore_write_note (abfd, buf, bufsiz, 11893 note_name, NT_ARM_HW_BREAK, aarch_hw_break, size); 11894} 11895 11896char * 11897elfcore_write_aarch_hw_watch (bfd *abfd, 11898 char *buf, 11899 int *bufsiz, 11900 const void *aarch_hw_watch, 11901 int size) 11902{ 11903 char *note_name = "LINUX"; 11904 return elfcore_write_note (abfd, buf, bufsiz, 11905 note_name, NT_ARM_HW_WATCH, aarch_hw_watch, size); 11906} 11907 11908char * 11909elfcore_write_aarch_sve (bfd *abfd, 11910 char *buf, 11911 int *bufsiz, 11912 const void *aarch_sve, 11913 int size) 11914{ 11915 char *note_name = "LINUX"; 11916 return elfcore_write_note (abfd, buf, bufsiz, 11917 note_name, NT_ARM_SVE, aarch_sve, size); 11918} 11919 11920char * 11921elfcore_write_aarch_pauth (bfd *abfd, 11922 char *buf, 11923 int *bufsiz, 11924 const void *aarch_pauth, 11925 int size) 11926{ 11927 char *note_name = "LINUX"; 11928 return elfcore_write_note (abfd, buf, bufsiz, 11929 note_name, NT_ARM_PAC_MASK, aarch_pauth, size); 11930} 11931 11932char * 11933elfcore_write_arc_v2 (bfd *abfd, 11934 char *buf, 11935 int *bufsiz, 11936 const void *arc_v2, 11937 int size) 11938{ 11939 char *note_name = "LINUX"; 11940 return elfcore_write_note (abfd, buf, bufsiz, 11941 note_name, NT_ARC_V2, arc_v2, size); 11942} 11943 11944char * 11945elfcore_write_register_note (bfd *abfd, 11946 char *buf, 11947 int *bufsiz, 11948 const char *section, 11949 const void *data, 11950 int size) 11951{ 11952 if (strcmp (section, ".reg2") == 0) 11953 return elfcore_write_prfpreg (abfd, buf, bufsiz, data, size); 11954 if (strcmp (section, ".reg-xfp") == 0) 11955 return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size); 11956 if (strcmp (section, ".reg-xstate") == 0) 11957 return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size); 11958 if (strcmp (section, ".reg-ppc-vmx") == 0) 11959 return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size); 11960 if (strcmp (section, ".reg-ppc-vsx") == 0) 11961 return elfcore_write_ppc_vsx (abfd, buf, bufsiz, data, size); 11962 if (strcmp (section, ".reg-ppc-tar") == 0) 11963 return elfcore_write_ppc_tar (abfd, buf, bufsiz, data, size); 11964 if (strcmp (section, ".reg-ppc-ppr") == 0) 11965 return elfcore_write_ppc_ppr (abfd, buf, bufsiz, data, size); 11966 if (strcmp (section, ".reg-ppc-dscr") == 0) 11967 return elfcore_write_ppc_dscr (abfd, buf, bufsiz, data, size); 11968 if (strcmp (section, ".reg-ppc-ebb") == 0) 11969 return elfcore_write_ppc_ebb (abfd, buf, bufsiz, data, size); 11970 if (strcmp (section, ".reg-ppc-pmu") == 0) 11971 return elfcore_write_ppc_pmu (abfd, buf, bufsiz, data, size); 11972 if (strcmp (section, ".reg-ppc-tm-cgpr") == 0) 11973 return elfcore_write_ppc_tm_cgpr (abfd, buf, bufsiz, data, size); 11974 if (strcmp (section, ".reg-ppc-tm-cfpr") == 0) 11975 return elfcore_write_ppc_tm_cfpr (abfd, buf, bufsiz, data, size); 11976 if (strcmp (section, ".reg-ppc-tm-cvmx") == 0) 11977 return elfcore_write_ppc_tm_cvmx (abfd, buf, bufsiz, data, size); 11978 if (strcmp (section, ".reg-ppc-tm-cvsx") == 0) 11979 return elfcore_write_ppc_tm_cvsx (abfd, buf, bufsiz, data, size); 11980 if (strcmp (section, ".reg-ppc-tm-spr") == 0) 11981 return elfcore_write_ppc_tm_spr (abfd, buf, bufsiz, data, size); 11982 if (strcmp (section, ".reg-ppc-tm-ctar") == 0) 11983 return elfcore_write_ppc_tm_ctar (abfd, buf, bufsiz, data, size); 11984 if (strcmp (section, ".reg-ppc-tm-cppr") == 0) 11985 return elfcore_write_ppc_tm_cppr (abfd, buf, bufsiz, data, size); 11986 if (strcmp (section, ".reg-ppc-tm-cdscr") == 0) 11987 return elfcore_write_ppc_tm_cdscr (abfd, buf, bufsiz, data, size); 11988 if (strcmp (section, ".reg-s390-high-gprs") == 0) 11989 return elfcore_write_s390_high_gprs (abfd, buf, bufsiz, data, size); 11990 if (strcmp (section, ".reg-s390-timer") == 0) 11991 return elfcore_write_s390_timer (abfd, buf, bufsiz, data, size); 11992 if (strcmp (section, ".reg-s390-todcmp") == 0) 11993 return elfcore_write_s390_todcmp (abfd, buf, bufsiz, data, size); 11994 if (strcmp (section, ".reg-s390-todpreg") == 0) 11995 return elfcore_write_s390_todpreg (abfd, buf, bufsiz, data, size); 11996 if (strcmp (section, ".reg-s390-ctrs") == 0) 11997 return elfcore_write_s390_ctrs (abfd, buf, bufsiz, data, size); 11998 if (strcmp (section, ".reg-s390-prefix") == 0) 11999 return elfcore_write_s390_prefix (abfd, buf, bufsiz, data, size); 12000 if (strcmp (section, ".reg-s390-last-break") == 0) 12001 return elfcore_write_s390_last_break (abfd, buf, bufsiz, data, size); 12002 if (strcmp (section, ".reg-s390-system-call") == 0) 12003 return elfcore_write_s390_system_call (abfd, buf, bufsiz, data, size); 12004 if (strcmp (section, ".reg-s390-tdb") == 0) 12005 return elfcore_write_s390_tdb (abfd, buf, bufsiz, data, size); 12006 if (strcmp (section, ".reg-s390-vxrs-low") == 0) 12007 return elfcore_write_s390_vxrs_low (abfd, buf, bufsiz, data, size); 12008 if (strcmp (section, ".reg-s390-vxrs-high") == 0) 12009 return elfcore_write_s390_vxrs_high (abfd, buf, bufsiz, data, size); 12010 if (strcmp (section, ".reg-s390-gs-cb") == 0) 12011 return elfcore_write_s390_gs_cb (abfd, buf, bufsiz, data, size); 12012 if (strcmp (section, ".reg-s390-gs-bc") == 0) 12013 return elfcore_write_s390_gs_bc (abfd, buf, bufsiz, data, size); 12014 if (strcmp (section, ".reg-arm-vfp") == 0) 12015 return elfcore_write_arm_vfp (abfd, buf, bufsiz, data, size); 12016 if (strcmp (section, ".reg-aarch-tls") == 0) 12017 return elfcore_write_aarch_tls (abfd, buf, bufsiz, data, size); 12018 if (strcmp (section, ".reg-aarch-hw-break") == 0) 12019 return elfcore_write_aarch_hw_break (abfd, buf, bufsiz, data, size); 12020 if (strcmp (section, ".reg-aarch-hw-watch") == 0) 12021 return elfcore_write_aarch_hw_watch (abfd, buf, bufsiz, data, size); 12022 if (strcmp (section, ".reg-aarch-sve") == 0) 12023 return elfcore_write_aarch_sve (abfd, buf, bufsiz, data, size); 12024 if (strcmp (section, ".reg-aarch-pauth") == 0) 12025 return elfcore_write_aarch_pauth (abfd, buf, bufsiz, data, size); 12026 if (strcmp (section, ".reg-arc-v2") == 0) 12027 return elfcore_write_arc_v2 (abfd, buf, bufsiz, data, size); 12028 return NULL; 12029} 12030 12031static bfd_boolean 12032elf_parse_notes (bfd *abfd, char *buf, size_t size, file_ptr offset, 12033 size_t align) 12034{ 12035 char *p; 12036 12037 /* NB: CORE PT_NOTE segments may have p_align values of 0 or 1. 12038 gABI specifies that PT_NOTE alignment should be aligned to 4 12039 bytes for 32-bit objects and to 8 bytes for 64-bit objects. If 12040 align is less than 4, we use 4 byte alignment. */ 12041 if (align < 4) 12042 align = 4; 12043 if (align != 4 && align != 8) 12044 return FALSE; 12045 12046 p = buf; 12047 while (p < buf + size) 12048 { 12049 Elf_External_Note *xnp = (Elf_External_Note *) p; 12050 Elf_Internal_Note in; 12051 12052 if (offsetof (Elf_External_Note, name) > buf - p + size) 12053 return FALSE; 12054 12055 in.type = H_GET_32 (abfd, xnp->type); 12056 12057 in.namesz = H_GET_32 (abfd, xnp->namesz); 12058 in.namedata = xnp->name; 12059 if (in.namesz > buf - in.namedata + size) 12060 return FALSE; 12061 12062 in.descsz = H_GET_32 (abfd, xnp->descsz); 12063 in.descdata = p + ELF_NOTE_DESC_OFFSET (in.namesz, align); 12064 in.descpos = offset + (in.descdata - buf); 12065 if (in.descsz != 0 12066 && (in.descdata >= buf + size 12067 || in.descsz > buf - in.descdata + size)) 12068 return FALSE; 12069 12070 switch (bfd_get_format (abfd)) 12071 { 12072 default: 12073 return TRUE; 12074 12075 case bfd_core: 12076 { 12077#define GROKER_ELEMENT(S,F) {S, sizeof (S) - 1, F} 12078 struct 12079 { 12080 const char * string; 12081 size_t len; 12082 bfd_boolean (* func)(bfd *, Elf_Internal_Note *); 12083 } 12084 grokers[] = 12085 { 12086 GROKER_ELEMENT ("", elfcore_grok_note), 12087 GROKER_ELEMENT ("FreeBSD", elfcore_grok_freebsd_note), 12088 GROKER_ELEMENT ("NetBSD-CORE", elfcore_grok_netbsd_note), 12089 GROKER_ELEMENT ( "OpenBSD", elfcore_grok_openbsd_note), 12090 GROKER_ELEMENT ("QNX", elfcore_grok_nto_note), 12091 GROKER_ELEMENT ("SPU/", elfcore_grok_spu_note), 12092 GROKER_ELEMENT ("GNU", elfobj_grok_gnu_note) 12093 }; 12094#undef GROKER_ELEMENT 12095 int i; 12096 12097 for (i = ARRAY_SIZE (grokers); i--;) 12098 { 12099 if (in.namesz >= grokers[i].len 12100 && strncmp (in.namedata, grokers[i].string, 12101 grokers[i].len) == 0) 12102 { 12103 if (! grokers[i].func (abfd, & in)) 12104 return FALSE; 12105 break; 12106 } 12107 } 12108 break; 12109 } 12110 12111 case bfd_object: 12112 if (in.namesz == sizeof "GNU" && strcmp (in.namedata, "GNU") == 0) 12113 { 12114 if (! elfobj_grok_gnu_note (abfd, &in)) 12115 return FALSE; 12116 } 12117 else if (in.namesz == sizeof "stapsdt" 12118 && strcmp (in.namedata, "stapsdt") == 0) 12119 { 12120 if (! elfobj_grok_stapsdt_note (abfd, &in)) 12121 return FALSE; 12122 } 12123 break; 12124 } 12125 12126 p += ELF_NOTE_NEXT_OFFSET (in.namesz, in.descsz, align); 12127 } 12128 12129 return TRUE; 12130} 12131 12132bfd_boolean 12133elf_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size, 12134 size_t align) 12135{ 12136 char *buf; 12137 12138 if (size == 0 || (size + 1) == 0) 12139 return TRUE; 12140 12141 if (bfd_seek (abfd, offset, SEEK_SET) != 0) 12142 return FALSE; 12143 12144 buf = (char *) _bfd_malloc_and_read (abfd, size + 1, size); 12145 if (buf == NULL) 12146 return FALSE; 12147 12148 /* PR 17512: file: ec08f814 12149 0-termintate the buffer so that string searches will not overflow. */ 12150 buf[size] = 0; 12151 12152 if (!elf_parse_notes (abfd, buf, size, offset, align)) 12153 { 12154 free (buf); 12155 return FALSE; 12156 } 12157 12158 free (buf); 12159 return TRUE; 12160} 12161 12162/* Providing external access to the ELF program header table. */ 12163 12164/* Return an upper bound on the number of bytes required to store a 12165 copy of ABFD's program header table entries. Return -1 if an error 12166 occurs; bfd_get_error will return an appropriate code. */ 12167 12168long 12169bfd_get_elf_phdr_upper_bound (bfd *abfd) 12170{ 12171 if (abfd->xvec->flavour != bfd_target_elf_flavour) 12172 { 12173 bfd_set_error (bfd_error_wrong_format); 12174 return -1; 12175 } 12176 12177 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); 12178} 12179 12180/* Copy ABFD's program header table entries to *PHDRS. The entries 12181 will be stored as an array of Elf_Internal_Phdr structures, as 12182 defined in include/elf/internal.h. To find out how large the 12183 buffer needs to be, call bfd_get_elf_phdr_upper_bound. 12184 12185 Return the number of program header table entries read, or -1 if an 12186 error occurs; bfd_get_error will return an appropriate code. */ 12187 12188int 12189bfd_get_elf_phdrs (bfd *abfd, void *phdrs) 12190{ 12191 int num_phdrs; 12192 12193 if (abfd->xvec->flavour != bfd_target_elf_flavour) 12194 { 12195 bfd_set_error (bfd_error_wrong_format); 12196 return -1; 12197 } 12198 12199 num_phdrs = elf_elfheader (abfd)->e_phnum; 12200 if (num_phdrs != 0) 12201 memcpy (phdrs, elf_tdata (abfd)->phdr, 12202 num_phdrs * sizeof (Elf_Internal_Phdr)); 12203 12204 return num_phdrs; 12205} 12206 12207enum elf_reloc_type_class 12208_bfd_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, 12209 const asection *rel_sec ATTRIBUTE_UNUSED, 12210 const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) 12211{ 12212 return reloc_class_normal; 12213} 12214 12215/* For RELA architectures, return the relocation value for a 12216 relocation against a local symbol. */ 12217 12218bfd_vma 12219_bfd_elf_rela_local_sym (bfd *abfd, 12220 Elf_Internal_Sym *sym, 12221 asection **psec, 12222 Elf_Internal_Rela *rel) 12223{ 12224 asection *sec = *psec; 12225 bfd_vma relocation; 12226 12227 relocation = (sec->output_section->vma 12228 + sec->output_offset 12229 + sym->st_value); 12230 if ((sec->flags & SEC_MERGE) 12231 && ELF_ST_TYPE (sym->st_info) == STT_SECTION 12232 && sec->sec_info_type == SEC_INFO_TYPE_MERGE) 12233 { 12234 rel->r_addend = 12235 _bfd_merged_section_offset (abfd, psec, 12236 elf_section_data (sec)->sec_info, 12237 sym->st_value + rel->r_addend); 12238 if (sec != *psec) 12239 { 12240 /* If we have changed the section, and our original section is 12241 marked with SEC_EXCLUDE, it means that the original 12242 SEC_MERGE section has been completely subsumed in some 12243 other SEC_MERGE section. In this case, we need to leave 12244 some info around for --emit-relocs. */ 12245 if ((sec->flags & SEC_EXCLUDE) != 0) 12246 sec->kept_section = *psec; 12247 sec = *psec; 12248 } 12249 rel->r_addend -= relocation; 12250 rel->r_addend += sec->output_section->vma + sec->output_offset; 12251 } 12252 return relocation; 12253} 12254 12255bfd_vma 12256_bfd_elf_rel_local_sym (bfd *abfd, 12257 Elf_Internal_Sym *sym, 12258 asection **psec, 12259 bfd_vma addend) 12260{ 12261 asection *sec = *psec; 12262 12263 if (sec->sec_info_type != SEC_INFO_TYPE_MERGE) 12264 return sym->st_value + addend; 12265 12266 return _bfd_merged_section_offset (abfd, psec, 12267 elf_section_data (sec)->sec_info, 12268 sym->st_value + addend); 12269} 12270 12271/* Adjust an address within a section. Given OFFSET within SEC, return 12272 the new offset within the section, based upon changes made to the 12273 section. Returns -1 if the offset is now invalid. 12274 The offset (in abnd out) is in target sized bytes, however big a 12275 byte may be. */ 12276 12277bfd_vma 12278_bfd_elf_section_offset (bfd *abfd, 12279 struct bfd_link_info *info, 12280 asection *sec, 12281 bfd_vma offset) 12282{ 12283 switch (sec->sec_info_type) 12284 { 12285 case SEC_INFO_TYPE_STABS: 12286 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, 12287 offset); 12288 case SEC_INFO_TYPE_EH_FRAME: 12289 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); 12290 12291 default: 12292 if ((sec->flags & SEC_ELF_REVERSE_COPY) != 0) 12293 { 12294 /* Reverse the offset. */ 12295 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12296 bfd_size_type address_size = bed->s->arch_size / 8; 12297 12298 /* address_size and sec->size are in octets. Convert 12299 to bytes before subtracting the original offset. */ 12300 offset = ((sec->size - address_size) 12301 / bfd_octets_per_byte (abfd, sec) - offset); 12302 } 12303 return offset; 12304 } 12305} 12306 12307/* Create a new BFD as if by bfd_openr. Rather than opening a file, 12308 reconstruct an ELF file by reading the segments out of remote memory 12309 based on the ELF file header at EHDR_VMA and the ELF program headers it 12310 points to. If not null, *LOADBASEP is filled in with the difference 12311 between the VMAs from which the segments were read, and the VMAs the 12312 file headers (and hence BFD's idea of each section's VMA) put them at. 12313 12314 The function TARGET_READ_MEMORY is called to copy LEN bytes from the 12315 remote memory at target address VMA into the local buffer at MYADDR; it 12316 should return zero on success or an `errno' code on failure. TEMPL must 12317 be a BFD for an ELF target with the word size and byte order found in 12318 the remote memory. */ 12319 12320bfd * 12321bfd_elf_bfd_from_remote_memory 12322 (bfd *templ, 12323 bfd_vma ehdr_vma, 12324 bfd_size_type size, 12325 bfd_vma *loadbasep, 12326 int (*target_read_memory) (bfd_vma, bfd_byte *, bfd_size_type)) 12327{ 12328 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) 12329 (templ, ehdr_vma, size, loadbasep, target_read_memory); 12330} 12331 12332long 12333_bfd_elf_get_synthetic_symtab (bfd *abfd, 12334 long symcount ATTRIBUTE_UNUSED, 12335 asymbol **syms ATTRIBUTE_UNUSED, 12336 long dynsymcount, 12337 asymbol **dynsyms, 12338 asymbol **ret) 12339{ 12340 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12341 asection *relplt; 12342 asymbol *s; 12343 const char *relplt_name; 12344 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); 12345 arelent *p; 12346 long count, i, n; 12347 size_t size; 12348 Elf_Internal_Shdr *hdr; 12349 char *names; 12350 asection *plt; 12351 12352 *ret = NULL; 12353 12354 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) 12355 return 0; 12356 12357 if (dynsymcount <= 0) 12358 return 0; 12359 12360 if (!bed->plt_sym_val) 12361 return 0; 12362 12363 relplt_name = bed->relplt_name; 12364 if (relplt_name == NULL) 12365 relplt_name = bed->rela_plts_and_copies_p ? ".rela.plt" : ".rel.plt"; 12366 relplt = bfd_get_section_by_name (abfd, relplt_name); 12367 if (relplt == NULL) 12368 return 0; 12369 12370 hdr = &elf_section_data (relplt)->this_hdr; 12371 if (hdr->sh_link != elf_dynsymtab (abfd) 12372 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) 12373 return 0; 12374 12375 plt = bfd_get_section_by_name (abfd, ".plt"); 12376 if (plt == NULL) 12377 return 0; 12378 12379 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; 12380 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) 12381 return -1; 12382 12383 count = relplt->size / hdr->sh_entsize; 12384 size = count * sizeof (asymbol); 12385 p = relplt->relocation; 12386 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) 12387 { 12388 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); 12389 if (p->addend != 0) 12390 { 12391#ifdef BFD64 12392 size += sizeof ("+0x") - 1 + 8 + 8 * (bed->s->elfclass == ELFCLASS64); 12393#else 12394 size += sizeof ("+0x") - 1 + 8; 12395#endif 12396 } 12397 } 12398 12399 s = *ret = (asymbol *) bfd_malloc (size); 12400 if (s == NULL) 12401 return -1; 12402 12403 names = (char *) (s + count); 12404 p = relplt->relocation; 12405 n = 0; 12406 for (i = 0; i < count; i++, p += bed->s->int_rels_per_ext_rel) 12407 { 12408 size_t len; 12409 bfd_vma addr; 12410 12411 addr = bed->plt_sym_val (i, plt, p); 12412 if (addr == (bfd_vma) -1) 12413 continue; 12414 12415 *s = **p->sym_ptr_ptr; 12416 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since 12417 we are defining a symbol, ensure one of them is set. */ 12418 if ((s->flags & BSF_LOCAL) == 0) 12419 s->flags |= BSF_GLOBAL; 12420 s->flags |= BSF_SYNTHETIC; 12421 s->section = plt; 12422 s->value = addr - plt->vma; 12423 s->name = names; 12424 s->udata.p = NULL; 12425 len = strlen ((*p->sym_ptr_ptr)->name); 12426 memcpy (names, (*p->sym_ptr_ptr)->name, len); 12427 names += len; 12428 if (p->addend != 0) 12429 { 12430 char buf[30], *a; 12431 12432 memcpy (names, "+0x", sizeof ("+0x") - 1); 12433 names += sizeof ("+0x") - 1; 12434 bfd_sprintf_vma (abfd, buf, p->addend); 12435 for (a = buf; *a == '0'; ++a) 12436 ; 12437 len = strlen (a); 12438 memcpy (names, a, len); 12439 names += len; 12440 } 12441 memcpy (names, "@plt", sizeof ("@plt")); 12442 names += sizeof ("@plt"); 12443 ++s, ++n; 12444 } 12445 12446 return n; 12447} 12448 12449/* It is only used by x86-64 so far. 12450 ??? This repeats *COM* id of zero. sec->id is supposed to be unique, 12451 but current usage would allow all of _bfd_std_section to be zero. */ 12452static const asymbol lcomm_sym 12453 = GLOBAL_SYM_INIT ("LARGE_COMMON", &_bfd_elf_large_com_section); 12454asection _bfd_elf_large_com_section 12455 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, &lcomm_sym, 12456 "LARGE_COMMON", 0, SEC_IS_COMMON); 12457 12458bfd_boolean 12459_bfd_elf_final_write_processing (bfd *abfd) 12460{ 12461 Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form. */ 12462 12463 i_ehdrp = elf_elfheader (abfd); 12464 12465 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) 12466 i_ehdrp->e_ident[EI_OSABI] = get_elf_backend_data (abfd)->elf_osabi; 12467 12468 /* Set the osabi field to ELFOSABI_GNU if the binary contains 12469 SHF_GNU_MBIND sections or symbols of STT_GNU_IFUNC type or 12470 STB_GNU_UNIQUE binding. */ 12471 if (elf_tdata (abfd)->has_gnu_osabi != 0) 12472 { 12473 if (i_ehdrp->e_ident[EI_OSABI] == ELFOSABI_NONE) 12474 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_GNU; 12475 else if (i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_GNU 12476 && i_ehdrp->e_ident[EI_OSABI] != ELFOSABI_FREEBSD) 12477 { 12478 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_mbind) 12479 _bfd_error_handler (_("GNU_MBIND section is unsupported")); 12480 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_ifunc) 12481 _bfd_error_handler (_("symbol type STT_GNU_IFUNC is unsupported")); 12482 if (elf_tdata (abfd)->has_gnu_osabi & elf_gnu_osabi_unique) 12483 _bfd_error_handler (_("symbol binding STB_GNU_UNIQUE is unsupported")); 12484 bfd_set_error (bfd_error_sorry); 12485 return FALSE; 12486 } 12487 } 12488 return TRUE; 12489} 12490 12491 12492/* Return TRUE for ELF symbol types that represent functions. 12493 This is the default version of this function, which is sufficient for 12494 most targets. It returns true if TYPE is STT_FUNC or STT_GNU_IFUNC. */ 12495 12496bfd_boolean 12497_bfd_elf_is_function_type (unsigned int type) 12498{ 12499 return (type == STT_FUNC 12500 || type == STT_GNU_IFUNC); 12501} 12502 12503/* If the ELF symbol SYM might be a function in SEC, return the 12504 function size and set *CODE_OFF to the function's entry point, 12505 otherwise return zero. */ 12506 12507bfd_size_type 12508_bfd_elf_maybe_function_sym (const asymbol *sym, asection *sec, 12509 bfd_vma *code_off) 12510{ 12511 bfd_size_type size; 12512 12513 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT 12514 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0 12515 || sym->section != sec) 12516 return 0; 12517 12518 *code_off = sym->value; 12519 size = 0; 12520 if (!(sym->flags & BSF_SYNTHETIC)) 12521 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size; 12522 if (size == 0) 12523 size = 1; 12524 return size; 12525} 12526 12527/* Set to non-zero to enable some debug messages. */ 12528#define DEBUG_SECONDARY_RELOCS 0 12529 12530/* An internal-to-the-bfd-library only section type 12531 used to indicate a cached secondary reloc section. */ 12532#define SHT_SECONDARY_RELOC (SHT_LOOS + SHT_RELA) 12533 12534/* Create a BFD section to hold a secondary reloc section. */ 12535 12536bfd_boolean 12537_bfd_elf_init_secondary_reloc_section (bfd * abfd, 12538 Elf_Internal_Shdr *hdr, 12539 const char * name, 12540 unsigned int shindex) 12541{ 12542 /* We only support RELA secondary relocs. */ 12543 if (hdr->sh_type != SHT_RELA) 12544 return FALSE; 12545 12546#if DEBUG_SECONDARY_RELOCS 12547 fprintf (stderr, "secondary reloc section %s encountered\n", name); 12548#endif 12549 hdr->sh_type = SHT_SECONDARY_RELOC; 12550 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 12551} 12552 12553/* Read in any secondary relocs associated with SEC. */ 12554 12555bfd_boolean 12556_bfd_elf_slurp_secondary_reloc_section (bfd * abfd, 12557 asection * sec, 12558 asymbol ** symbols) 12559{ 12560 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); 12561 asection * relsec; 12562 bfd_boolean result = TRUE; 12563 bfd_vma (*r_sym) (bfd_vma); 12564 12565#if BFD_DEFAULT_TARGET_SIZE > 32 12566 if (bfd_arch_bits_per_address (abfd) != 32) 12567 r_sym = elf64_r_sym; 12568 else 12569#endif 12570 r_sym = elf32_r_sym; 12571 12572 /* Discover if there are any secondary reloc sections 12573 associated with SEC. */ 12574 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) 12575 { 12576 Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr; 12577 12578 if (hdr->sh_type == SHT_SECONDARY_RELOC 12579 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx 12580 && (hdr->sh_entsize == ebd->s->sizeof_rel 12581 || hdr->sh_entsize == ebd->s->sizeof_rela)) 12582 { 12583 bfd_byte * native_relocs; 12584 bfd_byte * native_reloc; 12585 arelent * internal_relocs; 12586 arelent * internal_reloc; 12587 unsigned int i; 12588 unsigned int entsize; 12589 unsigned int symcount; 12590 unsigned int reloc_count; 12591 size_t amt; 12592 12593 if (ebd->elf_info_to_howto == NULL) 12594 return FALSE; 12595 12596#if DEBUG_SECONDARY_RELOCS 12597 fprintf (stderr, "read secondary relocs for %s from %s\n", 12598 sec->name, relsec->name); 12599#endif 12600 entsize = hdr->sh_entsize; 12601 12602 native_relocs = bfd_malloc (hdr->sh_size); 12603 if (native_relocs == NULL) 12604 { 12605 result = FALSE; 12606 continue; 12607 } 12608 12609 reloc_count = NUM_SHDR_ENTRIES (hdr); 12610 if (_bfd_mul_overflow (reloc_count, sizeof (arelent), & amt)) 12611 { 12612 free (native_relocs); 12613 bfd_set_error (bfd_error_file_too_big); 12614 result = FALSE; 12615 continue; 12616 } 12617 12618 internal_relocs = (arelent *) bfd_alloc (abfd, amt); 12619 if (internal_relocs == NULL) 12620 { 12621 free (native_relocs); 12622 result = FALSE; 12623 continue; 12624 } 12625 12626 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 12627 || (bfd_bread (native_relocs, hdr->sh_size, abfd) 12628 != hdr->sh_size)) 12629 { 12630 free (native_relocs); 12631 /* The internal_relocs will be freed when 12632 the memory for the bfd is released. */ 12633 result = FALSE; 12634 continue; 12635 } 12636 12637 symcount = bfd_get_symcount (abfd); 12638 12639 for (i = 0, internal_reloc = internal_relocs, 12640 native_reloc = native_relocs; 12641 i < reloc_count; 12642 i++, internal_reloc++, native_reloc += entsize) 12643 { 12644 bfd_boolean res; 12645 Elf_Internal_Rela rela; 12646 12647 ebd->s->swap_reloca_in (abfd, native_reloc, & rela); 12648 12649 /* The address of an ELF reloc is section relative for an object 12650 file, and absolute for an executable file or shared library. 12651 The address of a normal BFD reloc is always section relative, 12652 and the address of a dynamic reloc is absolute.. */ 12653 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) 12654 internal_reloc->address = rela.r_offset; 12655 else 12656 internal_reloc->address = rela.r_offset - sec->vma; 12657 12658 if (r_sym (rela.r_info) == STN_UNDEF) 12659 { 12660 /* FIXME: This and the error case below mean that we 12661 have a symbol on relocs that is not elf_symbol_type. */ 12662 internal_reloc->sym_ptr_ptr = 12663 bfd_abs_section_ptr->symbol_ptr_ptr; 12664 } 12665 else if (r_sym (rela.r_info) > symcount) 12666 { 12667 _bfd_error_handler 12668 /* xgettext:c-format */ 12669 (_("%pB(%pA): relocation %d has invalid symbol index %ld"), 12670 abfd, sec, i, (long) r_sym (rela.r_info)); 12671 bfd_set_error (bfd_error_bad_value); 12672 internal_reloc->sym_ptr_ptr = 12673 bfd_abs_section_ptr->symbol_ptr_ptr; 12674 result = FALSE; 12675 } 12676 else 12677 { 12678 asymbol **ps; 12679 12680 ps = symbols + r_sym (rela.r_info) - 1; 12681 12682 internal_reloc->sym_ptr_ptr = ps; 12683 /* Make sure that this symbol is not removed by strip. */ 12684 (*ps)->flags |= BSF_KEEP; 12685 } 12686 12687 internal_reloc->addend = rela.r_addend; 12688 12689 res = ebd->elf_info_to_howto (abfd, internal_reloc, & rela); 12690 if (! res || internal_reloc->howto == NULL) 12691 { 12692#if DEBUG_SECONDARY_RELOCS 12693 fprintf (stderr, "there is no howto associated with reloc %lx\n", 12694 rela.r_info); 12695#endif 12696 result = FALSE; 12697 } 12698 } 12699 12700 free (native_relocs); 12701 /* Store the internal relocs. */ 12702 elf_section_data (relsec)->sec_info = internal_relocs; 12703 } 12704 } 12705 12706 return result; 12707} 12708 12709/* Set the ELF section header fields of an output secondary reloc section. */ 12710 12711bfd_boolean 12712_bfd_elf_copy_special_section_fields (const bfd * ibfd ATTRIBUTE_UNUSED, 12713 bfd * obfd ATTRIBUTE_UNUSED, 12714 const Elf_Internal_Shdr * isection, 12715 Elf_Internal_Shdr * osection) 12716{ 12717 asection * isec; 12718 asection * osec; 12719 struct bfd_elf_section_data * esd; 12720 12721 if (isection == NULL) 12722 return FALSE; 12723 12724 if (isection->sh_type != SHT_SECONDARY_RELOC) 12725 return TRUE; 12726 12727 isec = isection->bfd_section; 12728 if (isec == NULL) 12729 return FALSE; 12730 12731 osec = osection->bfd_section; 12732 if (osec == NULL) 12733 return FALSE; 12734 12735 esd = elf_section_data (osec); 12736 BFD_ASSERT (esd->sec_info == NULL); 12737 esd->sec_info = elf_section_data (isec)->sec_info; 12738 osection->sh_type = SHT_RELA; 12739 osection->sh_link = elf_onesymtab (obfd); 12740 if (osection->sh_link == 0) 12741 { 12742 /* There is no symbol table - we are hosed... */ 12743 _bfd_error_handler 12744 /* xgettext:c-format */ 12745 (_("%pB(%pA): link section cannot be set because the output file does not have a symbol table"), 12746 obfd, osec); 12747 bfd_set_error (bfd_error_bad_value); 12748 return FALSE; 12749 } 12750 12751 /* Find the output section that corresponds to the isection's sh_info link. */ 12752 if (isection->sh_info == 0 12753 || isection->sh_info >= elf_numsections (ibfd)) 12754 { 12755 _bfd_error_handler 12756 /* xgettext:c-format */ 12757 (_("%pB(%pA): info section index is invalid"), 12758 obfd, osec); 12759 bfd_set_error (bfd_error_bad_value); 12760 return FALSE; 12761 } 12762 12763 isection = elf_elfsections (ibfd)[isection->sh_info]; 12764 12765 if (isection == NULL 12766 || isection->bfd_section == NULL 12767 || isection->bfd_section->output_section == NULL) 12768 { 12769 _bfd_error_handler 12770 /* xgettext:c-format */ 12771 (_("%pB(%pA): info section index cannot be set because the section is not in the output"), 12772 obfd, osec); 12773 bfd_set_error (bfd_error_bad_value); 12774 return FALSE; 12775 } 12776 12777 esd = elf_section_data (isection->bfd_section->output_section); 12778 BFD_ASSERT (esd != NULL); 12779 osection->sh_info = esd->this_idx; 12780 esd->has_secondary_relocs = TRUE; 12781#if DEBUG_SECONDARY_RELOCS 12782 fprintf (stderr, "update header of %s, sh_link = %u, sh_info = %u\n", 12783 osec->name, osection->sh_link, osection->sh_info); 12784 fprintf (stderr, "mark section %s as having secondary relocs\n", 12785 bfd_section_name (isection->bfd_section->output_section)); 12786#endif 12787 12788 return TRUE; 12789} 12790 12791/* Write out a secondary reloc section. 12792 12793 FIXME: Currently this function can result in a serious performance penalty 12794 for files with secondary relocs and lots of sections. The proper way to 12795 fix this is for _bfd_elf_copy_special_section_fields() to chain secondary 12796 relocs together and then to have this function just walk that chain. */ 12797 12798bfd_boolean 12799_bfd_elf_write_secondary_reloc_section (bfd *abfd, asection *sec) 12800{ 12801 const struct elf_backend_data * const ebd = get_elf_backend_data (abfd); 12802 bfd_vma addr_offset; 12803 asection * relsec; 12804 bfd_vma (*r_info) (bfd_vma, bfd_vma); 12805 bfd_boolean result = TRUE; 12806 12807 if (sec == NULL) 12808 return FALSE; 12809 12810#if BFD_DEFAULT_TARGET_SIZE > 32 12811 if (bfd_arch_bits_per_address (abfd) != 32) 12812 r_info = elf64_r_info; 12813 else 12814#endif 12815 r_info = elf32_r_info; 12816 12817 /* The address of an ELF reloc is section relative for an object 12818 file, and absolute for an executable file or shared library. 12819 The address of a BFD reloc is always section relative. */ 12820 addr_offset = 0; 12821 if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) 12822 addr_offset = sec->vma; 12823 12824 /* Discover if there are any secondary reloc sections 12825 associated with SEC. */ 12826 for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) 12827 { 12828 const struct bfd_elf_section_data * const esd = elf_section_data (relsec); 12829 Elf_Internal_Shdr * const hdr = (Elf_Internal_Shdr *) & esd->this_hdr; 12830 12831 if (hdr->sh_type == SHT_RELA 12832 && hdr->sh_info == (unsigned) elf_section_data (sec)->this_idx) 12833 { 12834 asymbol * last_sym; 12835 int last_sym_idx; 12836 unsigned int reloc_count; 12837 unsigned int idx; 12838 arelent * src_irel; 12839 bfd_byte * dst_rela; 12840 12841 if (hdr->contents != NULL) 12842 { 12843 _bfd_error_handler 12844 /* xgettext:c-format */ 12845 (_("%pB(%pA): error: secondary reloc section processed twice"), 12846 abfd, relsec); 12847 bfd_set_error (bfd_error_bad_value); 12848 result = FALSE; 12849 continue; 12850 } 12851 12852 if (hdr->sh_entsize == 0) 12853 { 12854 _bfd_error_handler 12855 /* xgettext:c-format */ 12856 (_("%pB(%pA): error: secondary reloc section has zero sized entries"), 12857 abfd, relsec); 12858 bfd_set_error (bfd_error_bad_value); 12859 result = FALSE; 12860 continue; 12861 } 12862 12863 reloc_count = hdr->sh_size / hdr->sh_entsize; 12864 if (reloc_count <= 0) 12865 { 12866 _bfd_error_handler 12867 /* xgettext:c-format */ 12868 (_("%pB(%pA): error: secondary reloc section is empty!"), 12869 abfd, relsec); 12870 bfd_set_error (bfd_error_bad_value); 12871 result = FALSE; 12872 continue; 12873 } 12874 12875 hdr->contents = bfd_alloc (abfd, hdr->sh_size); 12876 if (hdr->contents == NULL) 12877 continue; 12878 12879#if DEBUG_SECONDARY_RELOCS 12880 fprintf (stderr, "write %u secondary relocs for %s from %s\n", 12881 reloc_count, sec->name, relsec->name); 12882#endif 12883 last_sym = NULL; 12884 last_sym_idx = 0; 12885 dst_rela = hdr->contents; 12886 src_irel = (arelent *) esd->sec_info; 12887 if (src_irel == NULL) 12888 { 12889 _bfd_error_handler 12890 /* xgettext:c-format */ 12891 (_("%pB(%pA): error: internal relocs missing for secondary reloc section"), 12892 abfd, relsec); 12893 bfd_set_error (bfd_error_bad_value); 12894 result = FALSE; 12895 continue; 12896 } 12897 12898 for (idx = 0; idx < reloc_count; idx++, dst_rela += hdr->sh_entsize) 12899 { 12900 Elf_Internal_Rela src_rela; 12901 arelent *ptr; 12902 asymbol *sym; 12903 int n; 12904 12905 ptr = src_irel + idx; 12906 if (ptr == NULL) 12907 { 12908 _bfd_error_handler 12909 /* xgettext:c-format */ 12910 (_("%pB(%pA): error: reloc table entry %u is empty"), 12911 abfd, relsec, idx); 12912 bfd_set_error (bfd_error_bad_value); 12913 result = FALSE; 12914 break; 12915 } 12916 12917 if (ptr->sym_ptr_ptr == NULL) 12918 { 12919 /* FIXME: Is this an error ? */ 12920 n = 0; 12921 } 12922 else 12923 { 12924 sym = *ptr->sym_ptr_ptr; 12925 12926 if (sym == last_sym) 12927 n = last_sym_idx; 12928 else 12929 { 12930 n = _bfd_elf_symbol_from_bfd_symbol (abfd, & sym); 12931 if (n < 0) 12932 { 12933 _bfd_error_handler 12934 /* xgettext:c-format */ 12935 (_("%pB(%pA): error: secondary reloc %u references a missing symbol"), 12936 abfd, relsec, idx); 12937 bfd_set_error (bfd_error_bad_value); 12938 result = FALSE; 12939 n = 0; 12940 } 12941 12942 last_sym = sym; 12943 last_sym_idx = n; 12944 } 12945 12946 if (sym->the_bfd != NULL 12947 && sym->the_bfd->xvec != abfd->xvec 12948 && ! _bfd_elf_validate_reloc (abfd, ptr)) 12949 { 12950 _bfd_error_handler 12951 /* xgettext:c-format */ 12952 (_("%pB(%pA): error: secondary reloc %u references a deleted symbol"), 12953 abfd, relsec, idx); 12954 bfd_set_error (bfd_error_bad_value); 12955 result = FALSE; 12956 n = 0; 12957 } 12958 } 12959 12960 src_rela.r_offset = ptr->address + addr_offset; 12961 if (ptr->howto == NULL) 12962 { 12963 _bfd_error_handler 12964 /* xgettext:c-format */ 12965 (_("%pB(%pA): error: secondary reloc %u is of an unknown type"), 12966 abfd, relsec, idx); 12967 bfd_set_error (bfd_error_bad_value); 12968 result = FALSE; 12969 src_rela.r_info = r_info (0, 0); 12970 } 12971 else 12972 src_rela.r_info = r_info (n, ptr->howto->type); 12973 src_rela.r_addend = ptr->addend; 12974 ebd->s->swap_reloca_out (abfd, &src_rela, dst_rela); 12975 } 12976 } 12977 } 12978 12979 return result; 12980} 12981