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