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