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