1 /* ELF executable support for BFD. 2 3 Copyright 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 4 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc. 5 6 This file is part of BFD, the Binary File Descriptor library. 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 2 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 21 22 /* 23 SECTION 24 ELF backends 25 26 BFD support for ELF formats is being worked on. 27 Currently, the best supported back ends are for sparc and i386 28 (running svr4 or Solaris 2). 29 30 Documentation of the internals of the support code still needs 31 to be written. The code is changing quickly enough that we 32 haven't bothered yet. */ 33 34 /* For sparc64-cross-sparc32. */ 35 #define _SYSCALL32 36 #include "bfd.h" 37 #include "sysdep.h" 38 #include "bfdlink.h" 39 #include "libbfd.h" 40 #define ARCH_SIZE 0 41 #include "elf-bfd.h" 42 #include "libiberty.h" 43 44 static int elf_sort_sections (const void *, const void *); 45 static bfd_boolean assign_file_positions_except_relocs (bfd *, struct bfd_link_info *); 46 static bfd_boolean prep_headers (bfd *); 47 static bfd_boolean swap_out_syms (bfd *, struct bfd_strtab_hash **, int) ; 48 static bfd_boolean elfcore_read_notes (bfd *, file_ptr, bfd_size_type) ; 49 50 /* Swap version information in and out. The version information is 51 currently size independent. If that ever changes, this code will 52 need to move into elfcode.h. */ 53 54 /* Swap in a Verdef structure. */ 55 56 void 57 _bfd_elf_swap_verdef_in (bfd *abfd, 58 const Elf_External_Verdef *src, 59 Elf_Internal_Verdef *dst) 60 { 61 dst->vd_version = H_GET_16 (abfd, src->vd_version); 62 dst->vd_flags = H_GET_16 (abfd, src->vd_flags); 63 dst->vd_ndx = H_GET_16 (abfd, src->vd_ndx); 64 dst->vd_cnt = H_GET_16 (abfd, src->vd_cnt); 65 dst->vd_hash = H_GET_32 (abfd, src->vd_hash); 66 dst->vd_aux = H_GET_32 (abfd, src->vd_aux); 67 dst->vd_next = H_GET_32 (abfd, src->vd_next); 68 } 69 70 /* Swap out a Verdef structure. */ 71 72 void 73 _bfd_elf_swap_verdef_out (bfd *abfd, 74 const Elf_Internal_Verdef *src, 75 Elf_External_Verdef *dst) 76 { 77 H_PUT_16 (abfd, src->vd_version, dst->vd_version); 78 H_PUT_16 (abfd, src->vd_flags, dst->vd_flags); 79 H_PUT_16 (abfd, src->vd_ndx, dst->vd_ndx); 80 H_PUT_16 (abfd, src->vd_cnt, dst->vd_cnt); 81 H_PUT_32 (abfd, src->vd_hash, dst->vd_hash); 82 H_PUT_32 (abfd, src->vd_aux, dst->vd_aux); 83 H_PUT_32 (abfd, src->vd_next, dst->vd_next); 84 } 85 86 /* Swap in a Verdaux structure. */ 87 88 void 89 _bfd_elf_swap_verdaux_in (bfd *abfd, 90 const Elf_External_Verdaux *src, 91 Elf_Internal_Verdaux *dst) 92 { 93 dst->vda_name = H_GET_32 (abfd, src->vda_name); 94 dst->vda_next = H_GET_32 (abfd, src->vda_next); 95 } 96 97 /* Swap out a Verdaux structure. */ 98 99 void 100 _bfd_elf_swap_verdaux_out (bfd *abfd, 101 const Elf_Internal_Verdaux *src, 102 Elf_External_Verdaux *dst) 103 { 104 H_PUT_32 (abfd, src->vda_name, dst->vda_name); 105 H_PUT_32 (abfd, src->vda_next, dst->vda_next); 106 } 107 108 /* Swap in a Verneed structure. */ 109 110 void 111 _bfd_elf_swap_verneed_in (bfd *abfd, 112 const Elf_External_Verneed *src, 113 Elf_Internal_Verneed *dst) 114 { 115 dst->vn_version = H_GET_16 (abfd, src->vn_version); 116 dst->vn_cnt = H_GET_16 (abfd, src->vn_cnt); 117 dst->vn_file = H_GET_32 (abfd, src->vn_file); 118 dst->vn_aux = H_GET_32 (abfd, src->vn_aux); 119 dst->vn_next = H_GET_32 (abfd, src->vn_next); 120 } 121 122 /* Swap out a Verneed structure. */ 123 124 void 125 _bfd_elf_swap_verneed_out (bfd *abfd, 126 const Elf_Internal_Verneed *src, 127 Elf_External_Verneed *dst) 128 { 129 H_PUT_16 (abfd, src->vn_version, dst->vn_version); 130 H_PUT_16 (abfd, src->vn_cnt, dst->vn_cnt); 131 H_PUT_32 (abfd, src->vn_file, dst->vn_file); 132 H_PUT_32 (abfd, src->vn_aux, dst->vn_aux); 133 H_PUT_32 (abfd, src->vn_next, dst->vn_next); 134 } 135 136 /* Swap in a Vernaux structure. */ 137 138 void 139 _bfd_elf_swap_vernaux_in (bfd *abfd, 140 const Elf_External_Vernaux *src, 141 Elf_Internal_Vernaux *dst) 142 { 143 dst->vna_hash = H_GET_32 (abfd, src->vna_hash); 144 dst->vna_flags = H_GET_16 (abfd, src->vna_flags); 145 dst->vna_other = H_GET_16 (abfd, src->vna_other); 146 dst->vna_name = H_GET_32 (abfd, src->vna_name); 147 dst->vna_next = H_GET_32 (abfd, src->vna_next); 148 } 149 150 /* Swap out a Vernaux structure. */ 151 152 void 153 _bfd_elf_swap_vernaux_out (bfd *abfd, 154 const Elf_Internal_Vernaux *src, 155 Elf_External_Vernaux *dst) 156 { 157 H_PUT_32 (abfd, src->vna_hash, dst->vna_hash); 158 H_PUT_16 (abfd, src->vna_flags, dst->vna_flags); 159 H_PUT_16 (abfd, src->vna_other, dst->vna_other); 160 H_PUT_32 (abfd, src->vna_name, dst->vna_name); 161 H_PUT_32 (abfd, src->vna_next, dst->vna_next); 162 } 163 164 /* Swap in a Versym structure. */ 165 166 void 167 _bfd_elf_swap_versym_in (bfd *abfd, 168 const Elf_External_Versym *src, 169 Elf_Internal_Versym *dst) 170 { 171 dst->vs_vers = H_GET_16 (abfd, src->vs_vers); 172 } 173 174 /* Swap out a Versym structure. */ 175 176 void 177 _bfd_elf_swap_versym_out (bfd *abfd, 178 const Elf_Internal_Versym *src, 179 Elf_External_Versym *dst) 180 { 181 H_PUT_16 (abfd, src->vs_vers, dst->vs_vers); 182 } 183 184 /* Standard ELF hash function. Do not change this function; you will 185 cause invalid hash tables to be generated. */ 186 187 unsigned long 188 bfd_elf_hash (const char *namearg) 189 { 190 const unsigned char *name = (const unsigned char *) namearg; 191 unsigned long h = 0; 192 unsigned long g; 193 int ch; 194 195 while ((ch = *name++) != '\0') 196 { 197 h = (h << 4) + ch; 198 if ((g = (h & 0xf0000000)) != 0) 199 { 200 h ^= g >> 24; 201 /* The ELF ABI says `h &= ~g', but this is equivalent in 202 this case and on some machines one insn instead of two. */ 203 h ^= g; 204 } 205 } 206 return h & 0xffffffff; 207 } 208 209 /* DT_GNU_HASH hash function. Do not change this function; you will 210 cause invalid hash tables to be generated. */ 211 212 unsigned long 213 bfd_elf_gnu_hash (const char *namearg) 214 { 215 const unsigned char *name = (const unsigned char *) namearg; 216 unsigned long h = 5381; 217 unsigned char ch; 218 219 while ((ch = *name++) != '\0') 220 h = (h << 5) + h + ch; 221 return h & 0xffffffff; 222 } 223 224 bfd_boolean 225 bfd_elf_mkobject (bfd *abfd) 226 { 227 /* This just does initialization. */ 228 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */ 229 elf_tdata (abfd) = bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)); 230 if (elf_tdata (abfd) == 0) 231 return FALSE; 232 /* Since everything is done at close time, do we need any 233 initialization? */ 234 235 return TRUE; 236 } 237 238 bfd_boolean 239 bfd_elf_mkcorefile (bfd *abfd) 240 { 241 /* I think this can be done just like an object file. */ 242 return bfd_elf_mkobject (abfd); 243 } 244 245 char * 246 bfd_elf_get_str_section (bfd *abfd, unsigned int shindex) 247 { 248 Elf_Internal_Shdr **i_shdrp; 249 bfd_byte *shstrtab = NULL; 250 file_ptr offset; 251 bfd_size_type shstrtabsize; 252 253 i_shdrp = elf_elfsections (abfd); 254 if (i_shdrp == 0 || i_shdrp[shindex] == 0) 255 return NULL; 256 257 shstrtab = i_shdrp[shindex]->contents; 258 if (shstrtab == NULL) 259 { 260 /* No cached one, attempt to read, and cache what we read. */ 261 offset = i_shdrp[shindex]->sh_offset; 262 shstrtabsize = i_shdrp[shindex]->sh_size; 263 264 /* Allocate and clear an extra byte at the end, to prevent crashes 265 in case the string table is not terminated. */ 266 if (shstrtabsize + 1 == 0 267 || (shstrtab = bfd_alloc (abfd, shstrtabsize + 1)) == NULL 268 || bfd_seek (abfd, offset, SEEK_SET) != 0) 269 shstrtab = NULL; 270 else if (bfd_bread (shstrtab, shstrtabsize, abfd) != shstrtabsize) 271 { 272 if (bfd_get_error () != bfd_error_system_call) 273 bfd_set_error (bfd_error_file_truncated); 274 shstrtab = NULL; 275 } 276 else 277 shstrtab[shstrtabsize] = '\0'; 278 i_shdrp[shindex]->contents = shstrtab; 279 } 280 return (char *) shstrtab; 281 } 282 283 char * 284 bfd_elf_string_from_elf_section (bfd *abfd, 285 unsigned int shindex, 286 unsigned int strindex) 287 { 288 Elf_Internal_Shdr *hdr; 289 290 if (strindex == 0) 291 return ""; 292 293 hdr = elf_elfsections (abfd)[shindex]; 294 295 if (hdr->contents == NULL 296 && bfd_elf_get_str_section (abfd, shindex) == NULL) 297 return NULL; 298 299 if (strindex >= hdr->sh_size) 300 { 301 unsigned int shstrndx = elf_elfheader(abfd)->e_shstrndx; 302 (*_bfd_error_handler) 303 (_("%B: invalid string offset %u >= %lu for section `%s'"), 304 abfd, strindex, (unsigned long) hdr->sh_size, 305 (shindex == shstrndx && strindex == hdr->sh_name 306 ? ".shstrtab" 307 : bfd_elf_string_from_elf_section (abfd, shstrndx, hdr->sh_name))); 308 return ""; 309 } 310 311 return ((char *) hdr->contents) + strindex; 312 } 313 314 /* Read and convert symbols to internal format. 315 SYMCOUNT specifies the number of symbols to read, starting from 316 symbol SYMOFFSET. If any of INTSYM_BUF, EXTSYM_BUF or EXTSHNDX_BUF 317 are non-NULL, they are used to store the internal symbols, external 318 symbols, and symbol section index extensions, respectively. */ 319 320 Elf_Internal_Sym * 321 bfd_elf_get_elf_syms (bfd *ibfd, 322 Elf_Internal_Shdr *symtab_hdr, 323 size_t symcount, 324 size_t symoffset, 325 Elf_Internal_Sym *intsym_buf, 326 void *extsym_buf, 327 Elf_External_Sym_Shndx *extshndx_buf) 328 { 329 Elf_Internal_Shdr *shndx_hdr; 330 void *alloc_ext; 331 const bfd_byte *esym; 332 Elf_External_Sym_Shndx *alloc_extshndx; 333 Elf_External_Sym_Shndx *shndx; 334 Elf_Internal_Sym *isym; 335 Elf_Internal_Sym *isymend; 336 const struct elf_backend_data *bed; 337 size_t extsym_size; 338 bfd_size_type amt; 339 file_ptr pos; 340 341 if (symcount == 0) 342 return intsym_buf; 343 344 /* Normal syms might have section extension entries. */ 345 shndx_hdr = NULL; 346 if (symtab_hdr == &elf_tdata (ibfd)->symtab_hdr) 347 shndx_hdr = &elf_tdata (ibfd)->symtab_shndx_hdr; 348 349 /* Read the symbols. */ 350 alloc_ext = NULL; 351 alloc_extshndx = NULL; 352 bed = get_elf_backend_data (ibfd); 353 extsym_size = bed->s->sizeof_sym; 354 amt = symcount * extsym_size; 355 pos = symtab_hdr->sh_offset + symoffset * extsym_size; 356 if (extsym_buf == NULL) 357 { 358 alloc_ext = bfd_malloc2 (symcount, extsym_size); 359 extsym_buf = alloc_ext; 360 } 361 if (extsym_buf == NULL 362 || bfd_seek (ibfd, pos, SEEK_SET) != 0 363 || bfd_bread (extsym_buf, amt, ibfd) != amt) 364 { 365 intsym_buf = NULL; 366 goto out; 367 } 368 369 if (shndx_hdr == NULL || shndx_hdr->sh_size == 0) 370 extshndx_buf = NULL; 371 else 372 { 373 amt = symcount * sizeof (Elf_External_Sym_Shndx); 374 pos = shndx_hdr->sh_offset + symoffset * sizeof (Elf_External_Sym_Shndx); 375 if (extshndx_buf == NULL) 376 { 377 alloc_extshndx = bfd_malloc2 (symcount, 378 sizeof (Elf_External_Sym_Shndx)); 379 extshndx_buf = alloc_extshndx; 380 } 381 if (extshndx_buf == NULL 382 || bfd_seek (ibfd, pos, SEEK_SET) != 0 383 || bfd_bread (extshndx_buf, amt, ibfd) != amt) 384 { 385 intsym_buf = NULL; 386 goto out; 387 } 388 } 389 390 if (intsym_buf == NULL) 391 { 392 intsym_buf = bfd_malloc2 (symcount, sizeof (Elf_Internal_Sym)); 393 if (intsym_buf == NULL) 394 goto out; 395 } 396 397 /* Convert the symbols to internal form. */ 398 isymend = intsym_buf + symcount; 399 for (esym = extsym_buf, isym = intsym_buf, shndx = extshndx_buf; 400 isym < isymend; 401 esym += extsym_size, isym++, shndx = shndx != NULL ? shndx + 1 : NULL) 402 (*bed->s->swap_symbol_in) (ibfd, esym, shndx, isym); 403 404 out: 405 if (alloc_ext != NULL) 406 free (alloc_ext); 407 if (alloc_extshndx != NULL) 408 free (alloc_extshndx); 409 410 return intsym_buf; 411 } 412 413 /* Look up a symbol name. */ 414 const char * 415 bfd_elf_sym_name (bfd *abfd, 416 Elf_Internal_Shdr *symtab_hdr, 417 Elf_Internal_Sym *isym, 418 asection *sym_sec) 419 { 420 const char *name; 421 unsigned int iname = isym->st_name; 422 unsigned int shindex = symtab_hdr->sh_link; 423 424 if (iname == 0 && ELF_ST_TYPE (isym->st_info) == STT_SECTION 425 /* Check for a bogus st_shndx to avoid crashing. */ 426 && isym->st_shndx < elf_numsections (abfd) 427 && !(isym->st_shndx >= SHN_LORESERVE && isym->st_shndx <= SHN_HIRESERVE)) 428 { 429 iname = elf_elfsections (abfd)[isym->st_shndx]->sh_name; 430 shindex = elf_elfheader (abfd)->e_shstrndx; 431 } 432 433 name = bfd_elf_string_from_elf_section (abfd, shindex, iname); 434 if (name == NULL) 435 name = "(null)"; 436 else if (sym_sec && *name == '\0') 437 name = bfd_section_name (abfd, sym_sec); 438 439 return name; 440 } 441 442 /* Elf_Internal_Shdr->contents is an array of these for SHT_GROUP 443 sections. The first element is the flags, the rest are section 444 pointers. */ 445 446 typedef union elf_internal_group { 447 Elf_Internal_Shdr *shdr; 448 unsigned int flags; 449 } Elf_Internal_Group; 450 451 /* Return the name of the group signature symbol. Why isn't the 452 signature just a string? */ 453 454 static const char * 455 group_signature (bfd *abfd, Elf_Internal_Shdr *ghdr) 456 { 457 Elf_Internal_Shdr *hdr; 458 unsigned char esym[sizeof (Elf64_External_Sym)]; 459 Elf_External_Sym_Shndx eshndx; 460 Elf_Internal_Sym isym; 461 462 /* First we need to ensure the symbol table is available. Make sure 463 that it is a symbol table section. */ 464 hdr = elf_elfsections (abfd) [ghdr->sh_link]; 465 if (hdr->sh_type != SHT_SYMTAB 466 || ! bfd_section_from_shdr (abfd, ghdr->sh_link)) 467 return NULL; 468 469 /* Go read the symbol. */ 470 hdr = &elf_tdata (abfd)->symtab_hdr; 471 if (bfd_elf_get_elf_syms (abfd, hdr, 1, ghdr->sh_info, 472 &isym, esym, &eshndx) == NULL) 473 return NULL; 474 475 return bfd_elf_sym_name (abfd, hdr, &isym, NULL); 476 } 477 478 /* Set next_in_group list pointer, and group name for NEWSECT. */ 479 480 static bfd_boolean 481 setup_group (bfd *abfd, Elf_Internal_Shdr *hdr, asection *newsect) 482 { 483 unsigned int num_group = elf_tdata (abfd)->num_group; 484 485 /* If num_group is zero, read in all SHT_GROUP sections. The count 486 is set to -1 if there are no SHT_GROUP sections. */ 487 if (num_group == 0) 488 { 489 unsigned int i, shnum; 490 491 /* First count the number of groups. If we have a SHT_GROUP 492 section with just a flag word (ie. sh_size is 4), ignore it. */ 493 shnum = elf_numsections (abfd); 494 num_group = 0; 495 for (i = 0; i < shnum; i++) 496 { 497 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; 498 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8) 499 num_group += 1; 500 } 501 502 if (num_group == 0) 503 { 504 num_group = (unsigned) -1; 505 elf_tdata (abfd)->num_group = num_group; 506 } 507 else 508 { 509 /* We keep a list of elf section headers for group sections, 510 so we can find them quickly. */ 511 bfd_size_type amt; 512 513 elf_tdata (abfd)->num_group = num_group; 514 elf_tdata (abfd)->group_sect_ptr 515 = bfd_alloc2 (abfd, num_group, sizeof (Elf_Internal_Shdr *)); 516 if (elf_tdata (abfd)->group_sect_ptr == NULL) 517 return FALSE; 518 519 num_group = 0; 520 for (i = 0; i < shnum; i++) 521 { 522 Elf_Internal_Shdr *shdr = elf_elfsections (abfd)[i]; 523 if (shdr->sh_type == SHT_GROUP && shdr->sh_size >= 8) 524 { 525 unsigned char *src; 526 Elf_Internal_Group *dest; 527 528 /* Add to list of sections. */ 529 elf_tdata (abfd)->group_sect_ptr[num_group] = shdr; 530 num_group += 1; 531 532 /* Read the raw contents. */ 533 BFD_ASSERT (sizeof (*dest) >= 4); 534 amt = shdr->sh_size * sizeof (*dest) / 4; 535 shdr->contents = bfd_alloc2 (abfd, shdr->sh_size, 536 sizeof (*dest) / 4); 537 if (shdr->contents == NULL 538 || bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0 539 || (bfd_bread (shdr->contents, shdr->sh_size, abfd) 540 != shdr->sh_size)) 541 return FALSE; 542 543 /* Translate raw contents, a flag word followed by an 544 array of elf section indices all in target byte order, 545 to the flag word followed by an array of elf section 546 pointers. */ 547 src = shdr->contents + shdr->sh_size; 548 dest = (Elf_Internal_Group *) (shdr->contents + amt); 549 while (1) 550 { 551 unsigned int idx; 552 553 src -= 4; 554 --dest; 555 idx = H_GET_32 (abfd, src); 556 if (src == shdr->contents) 557 { 558 dest->flags = idx; 559 if (shdr->bfd_section != NULL && (idx & GRP_COMDAT)) 560 shdr->bfd_section->flags 561 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 562 break; 563 } 564 if (idx >= shnum) 565 { 566 ((*_bfd_error_handler) 567 (_("%B: invalid SHT_GROUP entry"), abfd)); 568 idx = 0; 569 } 570 dest->shdr = elf_elfsections (abfd)[idx]; 571 } 572 } 573 } 574 } 575 } 576 577 if (num_group != (unsigned) -1) 578 { 579 unsigned int i; 580 581 for (i = 0; i < num_group; i++) 582 { 583 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; 584 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; 585 unsigned int n_elt = shdr->sh_size / 4; 586 587 /* Look through this group's sections to see if current 588 section is a member. */ 589 while (--n_elt != 0) 590 if ((++idx)->shdr == hdr) 591 { 592 asection *s = NULL; 593 594 /* We are a member of this group. Go looking through 595 other members to see if any others are linked via 596 next_in_group. */ 597 idx = (Elf_Internal_Group *) shdr->contents; 598 n_elt = shdr->sh_size / 4; 599 while (--n_elt != 0) 600 if ((s = (++idx)->shdr->bfd_section) != NULL 601 && elf_next_in_group (s) != NULL) 602 break; 603 if (n_elt != 0) 604 { 605 /* Snarf the group name from other member, and 606 insert current section in circular list. */ 607 elf_group_name (newsect) = elf_group_name (s); 608 elf_next_in_group (newsect) = elf_next_in_group (s); 609 elf_next_in_group (s) = newsect; 610 } 611 else 612 { 613 const char *gname; 614 615 gname = group_signature (abfd, shdr); 616 if (gname == NULL) 617 return FALSE; 618 elf_group_name (newsect) = gname; 619 620 /* Start a circular list with one element. */ 621 elf_next_in_group (newsect) = newsect; 622 } 623 624 /* If the group section has been created, point to the 625 new member. */ 626 if (shdr->bfd_section != NULL) 627 elf_next_in_group (shdr->bfd_section) = newsect; 628 629 i = num_group - 1; 630 break; 631 } 632 } 633 } 634 635 if (elf_group_name (newsect) == NULL) 636 { 637 (*_bfd_error_handler) (_("%B: no group info for section %A"), 638 abfd, newsect); 639 } 640 return TRUE; 641 } 642 643 bfd_boolean 644 _bfd_elf_setup_sections (bfd *abfd) 645 { 646 unsigned int i; 647 unsigned int num_group = elf_tdata (abfd)->num_group; 648 bfd_boolean result = TRUE; 649 asection *s; 650 651 /* Process SHF_LINK_ORDER. */ 652 for (s = abfd->sections; s != NULL; s = s->next) 653 { 654 Elf_Internal_Shdr *this_hdr = &elf_section_data (s)->this_hdr; 655 if ((this_hdr->sh_flags & SHF_LINK_ORDER) != 0) 656 { 657 unsigned int elfsec = this_hdr->sh_link; 658 /* FIXME: The old Intel compiler and old strip/objcopy may 659 not set the sh_link or sh_info fields. Hence we could 660 get the situation where elfsec is 0. */ 661 if (elfsec == 0) 662 { 663 const struct elf_backend_data *bed 664 = get_elf_backend_data (abfd); 665 if (bed->link_order_error_handler) 666 bed->link_order_error_handler 667 (_("%B: warning: sh_link not set for section `%A'"), 668 abfd, s); 669 } 670 else 671 { 672 asection *link; 673 674 this_hdr = elf_elfsections (abfd)[elfsec]; 675 676 /* PR 1991, 2008: 677 Some strip/objcopy may leave an incorrect value in 678 sh_link. We don't want to proceed. */ 679 link = this_hdr->bfd_section; 680 if (link == NULL) 681 { 682 (*_bfd_error_handler) 683 (_("%B: sh_link [%d] in section `%A' is incorrect"), 684 s->owner, s, elfsec); 685 result = FALSE; 686 } 687 688 elf_linked_to_section (s) = link; 689 } 690 } 691 } 692 693 /* Process section groups. */ 694 if (num_group == (unsigned) -1) 695 return result; 696 697 for (i = 0; i < num_group; i++) 698 { 699 Elf_Internal_Shdr *shdr = elf_tdata (abfd)->group_sect_ptr[i]; 700 Elf_Internal_Group *idx = (Elf_Internal_Group *) shdr->contents; 701 unsigned int n_elt = shdr->sh_size / 4; 702 703 while (--n_elt != 0) 704 if ((++idx)->shdr->bfd_section) 705 elf_sec_group (idx->shdr->bfd_section) = shdr->bfd_section; 706 else if (idx->shdr->sh_type == SHT_RELA 707 || idx->shdr->sh_type == SHT_REL) 708 /* We won't include relocation sections in section groups in 709 output object files. We adjust the group section size here 710 so that relocatable link will work correctly when 711 relocation sections are in section group in input object 712 files. */ 713 shdr->bfd_section->size -= 4; 714 else 715 { 716 /* There are some unknown sections in the group. */ 717 (*_bfd_error_handler) 718 (_("%B: unknown [%d] section `%s' in group [%s]"), 719 abfd, 720 (unsigned int) idx->shdr->sh_type, 721 bfd_elf_string_from_elf_section (abfd, 722 (elf_elfheader (abfd) 723 ->e_shstrndx), 724 idx->shdr->sh_name), 725 shdr->bfd_section->name); 726 result = FALSE; 727 } 728 } 729 return result; 730 } 731 732 bfd_boolean 733 bfd_elf_is_group_section (bfd *abfd ATTRIBUTE_UNUSED, const asection *sec) 734 { 735 return elf_next_in_group (sec) != NULL; 736 } 737 738 /* Make a BFD section from an ELF section. We store a pointer to the 739 BFD section in the bfd_section field of the header. */ 740 741 bfd_boolean 742 _bfd_elf_make_section_from_shdr (bfd *abfd, 743 Elf_Internal_Shdr *hdr, 744 const char *name, 745 int shindex) 746 { 747 asection *newsect; 748 flagword flags; 749 const struct elf_backend_data *bed; 750 751 if (hdr->bfd_section != NULL) 752 { 753 BFD_ASSERT (strcmp (name, 754 bfd_get_section_name (abfd, hdr->bfd_section)) == 0); 755 return TRUE; 756 } 757 758 newsect = bfd_make_section_anyway (abfd, name); 759 if (newsect == NULL) 760 return FALSE; 761 762 hdr->bfd_section = newsect; 763 elf_section_data (newsect)->this_hdr = *hdr; 764 elf_section_data (newsect)->this_idx = shindex; 765 766 /* Always use the real type/flags. */ 767 elf_section_type (newsect) = hdr->sh_type; 768 elf_section_flags (newsect) = hdr->sh_flags; 769 770 newsect->filepos = hdr->sh_offset; 771 772 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) 773 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) 774 || ! bfd_set_section_alignment (abfd, newsect, 775 bfd_log2 ((bfd_vma) hdr->sh_addralign))) 776 return FALSE; 777 778 flags = SEC_NO_FLAGS; 779 if (hdr->sh_type != SHT_NOBITS) 780 flags |= SEC_HAS_CONTENTS; 781 if (hdr->sh_type == SHT_GROUP) 782 flags |= SEC_GROUP | SEC_EXCLUDE; 783 if ((hdr->sh_flags & SHF_ALLOC) != 0) 784 { 785 flags |= SEC_ALLOC; 786 if (hdr->sh_type != SHT_NOBITS) 787 flags |= SEC_LOAD; 788 } 789 if ((hdr->sh_flags & SHF_WRITE) == 0) 790 flags |= SEC_READONLY; 791 if ((hdr->sh_flags & SHF_EXECINSTR) != 0) 792 flags |= SEC_CODE; 793 else if ((flags & SEC_LOAD) != 0) 794 flags |= SEC_DATA; 795 if ((hdr->sh_flags & SHF_MERGE) != 0) 796 { 797 flags |= SEC_MERGE; 798 newsect->entsize = hdr->sh_entsize; 799 if ((hdr->sh_flags & SHF_STRINGS) != 0) 800 flags |= SEC_STRINGS; 801 } 802 if (hdr->sh_flags & SHF_GROUP) 803 if (!setup_group (abfd, hdr, newsect)) 804 return FALSE; 805 if ((hdr->sh_flags & SHF_TLS) != 0) 806 flags |= SEC_THREAD_LOCAL; 807 808 if ((flags & SEC_ALLOC) == 0) 809 { 810 /* The debugging sections appear to be recognized only by name, 811 not any sort of flag. Their SEC_ALLOC bits are cleared. */ 812 static const struct 813 { 814 const char *name; 815 int len; 816 } debug_sections [] = 817 { 818 { "debug", 5 }, /* 'd' */ 819 { NULL, 0 }, /* 'e' */ 820 { NULL, 0 }, /* 'f' */ 821 { "gnu.linkonce.wi.", 17 }, /* 'g' */ 822 { NULL, 0 }, /* 'h' */ 823 { NULL, 0 }, /* 'i' */ 824 { NULL, 0 }, /* 'j' */ 825 { NULL, 0 }, /* 'k' */ 826 { "line", 4 }, /* 'l' */ 827 { NULL, 0 }, /* 'm' */ 828 { NULL, 0 }, /* 'n' */ 829 { NULL, 0 }, /* 'o' */ 830 { NULL, 0 }, /* 'p' */ 831 { NULL, 0 }, /* 'q' */ 832 { NULL, 0 }, /* 'r' */ 833 { "stab", 4 } /* 's' */ 834 }; 835 836 if (name [0] == '.') 837 { 838 int i = name [1] - 'd'; 839 if (i >= 0 840 && i < (int) ARRAY_SIZE (debug_sections) 841 && debug_sections [i].name != NULL 842 && strncmp (&name [1], debug_sections [i].name, 843 debug_sections [i].len) == 0) 844 flags |= SEC_DEBUGGING; 845 } 846 } 847 848 /* As a GNU extension, if the name begins with .gnu.linkonce, we 849 only link a single copy of the section. This is used to support 850 g++. g++ will emit each template expansion in its own section. 851 The symbols will be defined as weak, so that multiple definitions 852 are permitted. The GNU linker extension is to actually discard 853 all but one of the sections. */ 854 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0 855 && elf_next_in_group (newsect) == NULL) 856 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 857 858 bed = get_elf_backend_data (abfd); 859 if (bed->elf_backend_section_flags) 860 if (! bed->elf_backend_section_flags (&flags, hdr)) 861 return FALSE; 862 863 if (! bfd_set_section_flags (abfd, newsect, flags)) 864 return FALSE; 865 866 if ((flags & SEC_ALLOC) != 0) 867 { 868 Elf_Internal_Phdr *phdr; 869 unsigned int i; 870 871 /* Look through the phdrs to see if we need to adjust the lma. 872 If all the p_paddr fields are zero, we ignore them, since 873 some ELF linkers produce such output. */ 874 phdr = elf_tdata (abfd)->phdr; 875 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 876 { 877 if (phdr->p_paddr != 0) 878 break; 879 } 880 if (i < elf_elfheader (abfd)->e_phnum) 881 { 882 phdr = elf_tdata (abfd)->phdr; 883 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 884 { 885 /* This section is part of this segment if its file 886 offset plus size lies within the segment's memory 887 span and, if the section is loaded, the extent of the 888 loaded data lies within the extent of the segment. 889 890 Note - we used to check the p_paddr field as well, and 891 refuse to set the LMA if it was 0. This is wrong 892 though, as a perfectly valid initialised segment can 893 have a p_paddr of zero. Some architectures, eg ARM, 894 place special significance on the address 0 and 895 executables need to be able to have a segment which 896 covers this address. */ 897 if (phdr->p_type == PT_LOAD 898 && (bfd_vma) hdr->sh_offset >= phdr->p_offset 899 && (hdr->sh_offset + hdr->sh_size 900 <= phdr->p_offset + phdr->p_memsz) 901 && ((flags & SEC_LOAD) == 0 902 || (hdr->sh_offset + hdr->sh_size 903 <= phdr->p_offset + phdr->p_filesz))) 904 { 905 if ((flags & SEC_LOAD) == 0) 906 newsect->lma = (phdr->p_paddr 907 + hdr->sh_addr - phdr->p_vaddr); 908 else 909 /* We used to use the same adjustment for SEC_LOAD 910 sections, but that doesn't work if the segment 911 is packed with code from multiple VMAs. 912 Instead we calculate the section LMA based on 913 the segment LMA. It is assumed that the 914 segment will contain sections with contiguous 915 LMAs, even if the VMAs are not. */ 916 newsect->lma = (phdr->p_paddr 917 + hdr->sh_offset - phdr->p_offset); 918 919 /* With contiguous segments, we can't tell from file 920 offsets whether a section with zero size should 921 be placed at the end of one segment or the 922 beginning of the next. Decide based on vaddr. */ 923 if (hdr->sh_addr >= phdr->p_vaddr 924 && (hdr->sh_addr + hdr->sh_size 925 <= phdr->p_vaddr + phdr->p_memsz)) 926 break; 927 } 928 } 929 } 930 } 931 932 return TRUE; 933 } 934 935 /* 936 INTERNAL_FUNCTION 937 bfd_elf_find_section 938 939 SYNOPSIS 940 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name); 941 942 DESCRIPTION 943 Helper functions for GDB to locate the string tables. 944 Since BFD hides string tables from callers, GDB needs to use an 945 internal hook to find them. Sun's .stabstr, in particular, 946 isn't even pointed to by the .stab section, so ordinary 947 mechanisms wouldn't work to find it, even if we had some. 948 */ 949 950 struct elf_internal_shdr * 951 bfd_elf_find_section (bfd *abfd, char *name) 952 { 953 Elf_Internal_Shdr **i_shdrp; 954 char *shstrtab; 955 unsigned int max; 956 unsigned int i; 957 958 i_shdrp = elf_elfsections (abfd); 959 if (i_shdrp != NULL) 960 { 961 shstrtab = bfd_elf_get_str_section (abfd, 962 elf_elfheader (abfd)->e_shstrndx); 963 if (shstrtab != NULL) 964 { 965 max = elf_numsections (abfd); 966 for (i = 1; i < max; i++) 967 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name)) 968 return i_shdrp[i]; 969 } 970 } 971 return 0; 972 } 973 974 const char *const bfd_elf_section_type_names[] = { 975 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", 976 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", 977 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", 978 }; 979 980 /* ELF relocs are against symbols. If we are producing relocatable 981 output, and the reloc is against an external symbol, and nothing 982 has given us any additional addend, the resulting reloc will also 983 be against the same symbol. In such a case, we don't want to 984 change anything about the way the reloc is handled, since it will 985 all be done at final link time. Rather than put special case code 986 into bfd_perform_relocation, all the reloc types use this howto 987 function. It just short circuits the reloc if producing 988 relocatable output against an external symbol. */ 989 990 bfd_reloc_status_type 991 bfd_elf_generic_reloc (bfd *abfd ATTRIBUTE_UNUSED, 992 arelent *reloc_entry, 993 asymbol *symbol, 994 void *data ATTRIBUTE_UNUSED, 995 asection *input_section, 996 bfd *output_bfd, 997 char **error_message ATTRIBUTE_UNUSED) 998 { 999 if (output_bfd != NULL 1000 && (symbol->flags & BSF_SECTION_SYM) == 0 1001 && (! reloc_entry->howto->partial_inplace 1002 || reloc_entry->addend == 0)) 1003 { 1004 reloc_entry->address += input_section->output_offset; 1005 return bfd_reloc_ok; 1006 } 1007 1008 return bfd_reloc_continue; 1009 } 1010 1011 /* Make sure sec_info_type is cleared if sec_info is cleared too. */ 1012 1013 static void 1014 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, 1015 asection *sec) 1016 { 1017 BFD_ASSERT (sec->sec_info_type == ELF_INFO_TYPE_MERGE); 1018 sec->sec_info_type = ELF_INFO_TYPE_NONE; 1019 } 1020 1021 /* Finish SHF_MERGE section merging. */ 1022 1023 bfd_boolean 1024 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info) 1025 { 1026 bfd *ibfd; 1027 asection *sec; 1028 1029 if (!is_elf_hash_table (info->hash)) 1030 return FALSE; 1031 1032 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) 1033 if ((ibfd->flags & DYNAMIC) == 0) 1034 for (sec = ibfd->sections; sec != NULL; sec = sec->next) 1035 if ((sec->flags & SEC_MERGE) != 0 1036 && !bfd_is_abs_section (sec->output_section)) 1037 { 1038 struct bfd_elf_section_data *secdata; 1039 1040 secdata = elf_section_data (sec); 1041 if (! _bfd_add_merge_section (abfd, 1042 &elf_hash_table (info)->merge_info, 1043 sec, &secdata->sec_info)) 1044 return FALSE; 1045 else if (secdata->sec_info) 1046 sec->sec_info_type = ELF_INFO_TYPE_MERGE; 1047 } 1048 1049 if (elf_hash_table (info)->merge_info != NULL) 1050 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info, 1051 merge_sections_remove_hook); 1052 return TRUE; 1053 } 1054 1055 void 1056 _bfd_elf_link_just_syms (asection *sec, struct bfd_link_info *info) 1057 { 1058 sec->output_section = bfd_abs_section_ptr; 1059 sec->output_offset = sec->vma; 1060 if (!is_elf_hash_table (info->hash)) 1061 return; 1062 1063 sec->sec_info_type = ELF_INFO_TYPE_JUST_SYMS; 1064 } 1065 1066 /* Copy the program header and other data from one object module to 1067 another. */ 1068 1069 bfd_boolean 1070 _bfd_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) 1071 { 1072 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 1073 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 1074 return TRUE; 1075 1076 BFD_ASSERT (!elf_flags_init (obfd) 1077 || (elf_elfheader (obfd)->e_flags 1078 == elf_elfheader (ibfd)->e_flags)); 1079 1080 elf_gp (obfd) = elf_gp (ibfd); 1081 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; 1082 elf_flags_init (obfd) = TRUE; 1083 return TRUE; 1084 } 1085 1086 static const char * 1087 get_segment_type (unsigned int p_type) 1088 { 1089 const char *pt; 1090 switch (p_type) 1091 { 1092 case PT_NULL: pt = "NULL"; break; 1093 case PT_LOAD: pt = "LOAD"; break; 1094 case PT_DYNAMIC: pt = "DYNAMIC"; break; 1095 case PT_INTERP: pt = "INTERP"; break; 1096 case PT_NOTE: pt = "NOTE"; break; 1097 case PT_SHLIB: pt = "SHLIB"; break; 1098 case PT_PHDR: pt = "PHDR"; break; 1099 case PT_TLS: pt = "TLS"; break; 1100 case PT_GNU_EH_FRAME: pt = "EH_FRAME"; break; 1101 case PT_GNU_STACK: pt = "STACK"; break; 1102 case PT_GNU_RELRO: pt = "RELRO"; break; 1103 case PT_OPENBSD_RANDOMIZE: pt = "OPENBSD_RANDOMIZE"; break; 1104 case PT_OPENBSD_WXNEEDED: pt = "OPENBSD_WXNEEDED"; break; 1105 case PT_OPENBSD_BOOTDATA: pt = "OPENBSD_BOOTDATA"; break; 1106 default: pt = NULL; break; 1107 } 1108 return pt; 1109 } 1110 1111 /* Print out the program headers. */ 1112 1113 bfd_boolean 1114 _bfd_elf_print_private_bfd_data (bfd *abfd, void *farg) 1115 { 1116 FILE *f = farg; 1117 Elf_Internal_Phdr *p; 1118 asection *s; 1119 bfd_byte *dynbuf = NULL; 1120 1121 p = elf_tdata (abfd)->phdr; 1122 if (p != NULL) 1123 { 1124 unsigned int i, c; 1125 1126 fprintf (f, _("\nProgram Header:\n")); 1127 c = elf_elfheader (abfd)->e_phnum; 1128 for (i = 0; i < c; i++, p++) 1129 { 1130 const char *pt = get_segment_type (p->p_type); 1131 char buf[20]; 1132 1133 if (pt == NULL) 1134 { 1135 sprintf (buf, "0x%lx", p->p_type); 1136 pt = buf; 1137 } 1138 fprintf (f, "%8s off 0x", pt); 1139 bfd_fprintf_vma (abfd, f, p->p_offset); 1140 fprintf (f, " vaddr 0x"); 1141 bfd_fprintf_vma (abfd, f, p->p_vaddr); 1142 fprintf (f, " paddr 0x"); 1143 bfd_fprintf_vma (abfd, f, p->p_paddr); 1144 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); 1145 fprintf (f, " filesz 0x"); 1146 bfd_fprintf_vma (abfd, f, p->p_filesz); 1147 fprintf (f, " memsz 0x"); 1148 bfd_fprintf_vma (abfd, f, p->p_memsz); 1149 fprintf (f, " flags %c%c%c", 1150 (p->p_flags & PF_R) != 0 ? 'r' : '-', 1151 (p->p_flags & PF_W) != 0 ? 'w' : '-', 1152 (p->p_flags & PF_X) != 0 ? 'x' : '-'); 1153 if ((p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)) != 0) 1154 fprintf (f, " %lx", p->p_flags &~ (unsigned) (PF_R | PF_W | PF_X)); 1155 fprintf (f, "\n"); 1156 } 1157 } 1158 1159 s = bfd_get_section_by_name (abfd, ".dynamic"); 1160 if (s != NULL) 1161 { 1162 int elfsec; 1163 unsigned long shlink; 1164 bfd_byte *extdyn, *extdynend; 1165 size_t extdynsize; 1166 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 1167 1168 fprintf (f, _("\nDynamic Section:\n")); 1169 1170 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) 1171 goto error_return; 1172 1173 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 1174 if (elfsec == -1) 1175 goto error_return; 1176 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 1177 1178 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 1179 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 1180 1181 extdyn = dynbuf; 1182 extdynend = extdyn + s->size; 1183 for (; extdyn < extdynend; extdyn += extdynsize) 1184 { 1185 Elf_Internal_Dyn dyn; 1186 const char *name; 1187 char ab[20]; 1188 bfd_boolean stringp; 1189 1190 (*swap_dyn_in) (abfd, extdyn, &dyn); 1191 1192 if (dyn.d_tag == DT_NULL) 1193 break; 1194 1195 stringp = FALSE; 1196 switch (dyn.d_tag) 1197 { 1198 default: 1199 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); 1200 name = ab; 1201 break; 1202 1203 case DT_NEEDED: name = "NEEDED"; stringp = TRUE; break; 1204 case DT_PLTRELSZ: name = "PLTRELSZ"; break; 1205 case DT_PLTGOT: name = "PLTGOT"; break; 1206 case DT_HASH: name = "HASH"; break; 1207 case DT_STRTAB: name = "STRTAB"; break; 1208 case DT_SYMTAB: name = "SYMTAB"; break; 1209 case DT_RELA: name = "RELA"; break; 1210 case DT_RELASZ: name = "RELASZ"; break; 1211 case DT_RELAENT: name = "RELAENT"; break; 1212 case DT_STRSZ: name = "STRSZ"; break; 1213 case DT_SYMENT: name = "SYMENT"; break; 1214 case DT_INIT: name = "INIT"; break; 1215 case DT_FINI: name = "FINI"; break; 1216 case DT_SONAME: name = "SONAME"; stringp = TRUE; break; 1217 case DT_RPATH: name = "RPATH"; stringp = TRUE; break; 1218 case DT_SYMBOLIC: name = "SYMBOLIC"; break; 1219 case DT_REL: name = "REL"; break; 1220 case DT_RELSZ: name = "RELSZ"; break; 1221 case DT_RELENT: name = "RELENT"; break; 1222 case DT_PLTREL: name = "PLTREL"; break; 1223 case DT_DEBUG: name = "DEBUG"; break; 1224 case DT_TEXTREL: name = "TEXTREL"; break; 1225 case DT_JMPREL: name = "JMPREL"; break; 1226 case DT_BIND_NOW: name = "BIND_NOW"; break; 1227 case DT_INIT_ARRAY: name = "INIT_ARRAY"; break; 1228 case DT_FINI_ARRAY: name = "FINI_ARRAY"; break; 1229 case DT_INIT_ARRAYSZ: name = "INIT_ARRAYSZ"; break; 1230 case DT_FINI_ARRAYSZ: name = "FINI_ARRAYSZ"; break; 1231 case DT_RUNPATH: name = "RUNPATH"; stringp = TRUE; break; 1232 case DT_FLAGS: name = "FLAGS"; break; 1233 case DT_PREINIT_ARRAY: name = "PREINIT_ARRAY"; break; 1234 case DT_PREINIT_ARRAYSZ: name = "PREINIT_ARRAYSZ"; break; 1235 case DT_CHECKSUM: name = "CHECKSUM"; break; 1236 case DT_PLTPADSZ: name = "PLTPADSZ"; break; 1237 case DT_MOVEENT: name = "MOVEENT"; break; 1238 case DT_MOVESZ: name = "MOVESZ"; break; 1239 case DT_FEATURE: name = "FEATURE"; break; 1240 case DT_POSFLAG_1: name = "POSFLAG_1"; break; 1241 case DT_SYMINSZ: name = "SYMINSZ"; break; 1242 case DT_SYMINENT: name = "SYMINENT"; break; 1243 case DT_CONFIG: name = "CONFIG"; stringp = TRUE; break; 1244 case DT_DEPAUDIT: name = "DEPAUDIT"; stringp = TRUE; break; 1245 case DT_AUDIT: name = "AUDIT"; stringp = TRUE; break; 1246 case DT_PLTPAD: name = "PLTPAD"; break; 1247 case DT_MOVETAB: name = "MOVETAB"; break; 1248 case DT_SYMINFO: name = "SYMINFO"; break; 1249 case DT_RELACOUNT: name = "RELACOUNT"; break; 1250 case DT_RELCOUNT: name = "RELCOUNT"; break; 1251 case DT_FLAGS_1: name = "FLAGS_1"; break; 1252 case DT_VERSYM: name = "VERSYM"; break; 1253 case DT_VERDEF: name = "VERDEF"; break; 1254 case DT_VERDEFNUM: name = "VERDEFNUM"; break; 1255 case DT_VERNEED: name = "VERNEED"; break; 1256 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; 1257 case DT_AUXILIARY: name = "AUXILIARY"; stringp = TRUE; break; 1258 case DT_USED: name = "USED"; break; 1259 case DT_FILTER: name = "FILTER"; stringp = TRUE; break; 1260 case DT_GNU_HASH: name = "GNU_HASH"; break; 1261 } 1262 1263 fprintf (f, " %-11s ", name); 1264 if (! stringp) 1265 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val); 1266 else 1267 { 1268 const char *string; 1269 unsigned int tagv = dyn.d_un.d_val; 1270 1271 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 1272 if (string == NULL) 1273 goto error_return; 1274 fprintf (f, "%s", string); 1275 } 1276 fprintf (f, "\n"); 1277 } 1278 1279 free (dynbuf); 1280 dynbuf = NULL; 1281 } 1282 1283 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) 1284 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) 1285 { 1286 if (! _bfd_elf_slurp_version_tables (abfd, FALSE)) 1287 return FALSE; 1288 } 1289 1290 if (elf_dynverdef (abfd) != 0) 1291 { 1292 Elf_Internal_Verdef *t; 1293 1294 fprintf (f, _("\nVersion definitions:\n")); 1295 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) 1296 { 1297 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, 1298 t->vd_flags, t->vd_hash, 1299 t->vd_nodename ? t->vd_nodename : "<corrupt>"); 1300 if (t->vd_auxptr != NULL && t->vd_auxptr->vda_nextptr != NULL) 1301 { 1302 Elf_Internal_Verdaux *a; 1303 1304 fprintf (f, "\t"); 1305 for (a = t->vd_auxptr->vda_nextptr; 1306 a != NULL; 1307 a = a->vda_nextptr) 1308 fprintf (f, "%s ", 1309 a->vda_nodename ? a->vda_nodename : "<corrupt>"); 1310 fprintf (f, "\n"); 1311 } 1312 } 1313 } 1314 1315 if (elf_dynverref (abfd) != 0) 1316 { 1317 Elf_Internal_Verneed *t; 1318 1319 fprintf (f, _("\nVersion References:\n")); 1320 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) 1321 { 1322 Elf_Internal_Vernaux *a; 1323 1324 fprintf (f, _(" required from %s:\n"), 1325 t->vn_filename ? t->vn_filename : "<corrupt>"); 1326 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 1327 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, 1328 a->vna_flags, a->vna_other, 1329 a->vna_nodename ? a->vna_nodename : "<corrupt>"); 1330 } 1331 } 1332 1333 return TRUE; 1334 1335 error_return: 1336 if (dynbuf != NULL) 1337 free (dynbuf); 1338 return FALSE; 1339 } 1340 1341 /* Display ELF-specific fields of a symbol. */ 1342 1343 void 1344 bfd_elf_print_symbol (bfd *abfd, 1345 void *filep, 1346 asymbol *symbol, 1347 bfd_print_symbol_type how) 1348 { 1349 FILE *file = filep; 1350 switch (how) 1351 { 1352 case bfd_print_symbol_name: 1353 fprintf (file, "%s", symbol->name); 1354 break; 1355 case bfd_print_symbol_more: 1356 fprintf (file, "elf "); 1357 bfd_fprintf_vma (abfd, file, symbol->value); 1358 fprintf (file, " %lx", (long) symbol->flags); 1359 break; 1360 case bfd_print_symbol_all: 1361 { 1362 const char *section_name; 1363 const char *name = NULL; 1364 const struct elf_backend_data *bed; 1365 unsigned char st_other; 1366 bfd_vma val; 1367 1368 section_name = symbol->section ? symbol->section->name : "(*none*)"; 1369 1370 bed = get_elf_backend_data (abfd); 1371 if (bed->elf_backend_print_symbol_all) 1372 name = (*bed->elf_backend_print_symbol_all) (abfd, filep, symbol); 1373 1374 if (name == NULL) 1375 { 1376 name = symbol->name; 1377 bfd_print_symbol_vandf (abfd, file, symbol); 1378 } 1379 1380 fprintf (file, " %s\t", section_name); 1381 /* Print the "other" value for a symbol. For common symbols, 1382 we've already printed the size; now print the alignment. 1383 For other symbols, we have no specified alignment, and 1384 we've printed the address; now print the size. */ 1385 if (bfd_is_com_section (symbol->section)) 1386 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_value; 1387 else 1388 val = ((elf_symbol_type *) symbol)->internal_elf_sym.st_size; 1389 bfd_fprintf_vma (abfd, file, val); 1390 1391 /* If we have version information, print it. */ 1392 if (elf_tdata (abfd)->dynversym_section != 0 1393 && (elf_tdata (abfd)->dynverdef_section != 0 1394 || elf_tdata (abfd)->dynverref_section != 0)) 1395 { 1396 unsigned int vernum; 1397 const char *version_string; 1398 1399 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION; 1400 1401 if (vernum == 0) 1402 version_string = ""; 1403 else if (vernum == 1) 1404 version_string = "Base"; 1405 else if (vernum <= elf_tdata (abfd)->cverdefs) 1406 version_string = 1407 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 1408 else 1409 { 1410 Elf_Internal_Verneed *t; 1411 1412 version_string = ""; 1413 for (t = elf_tdata (abfd)->verref; 1414 t != NULL; 1415 t = t->vn_nextref) 1416 { 1417 Elf_Internal_Vernaux *a; 1418 1419 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 1420 { 1421 if (a->vna_other == vernum) 1422 { 1423 version_string = a->vna_nodename; 1424 break; 1425 } 1426 } 1427 } 1428 } 1429 1430 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0) 1431 fprintf (file, " %-11s", version_string); 1432 else 1433 { 1434 int i; 1435 1436 fprintf (file, " (%s)", version_string); 1437 for (i = 10 - strlen (version_string); i > 0; --i) 1438 putc (' ', file); 1439 } 1440 } 1441 1442 /* If the st_other field is not zero, print it. */ 1443 st_other = ((elf_symbol_type *) symbol)->internal_elf_sym.st_other; 1444 1445 switch (st_other) 1446 { 1447 case 0: break; 1448 case STV_INTERNAL: fprintf (file, " .internal"); break; 1449 case STV_HIDDEN: fprintf (file, " .hidden"); break; 1450 case STV_PROTECTED: fprintf (file, " .protected"); break; 1451 default: 1452 /* Some other non-defined flags are also present, so print 1453 everything hex. */ 1454 fprintf (file, " 0x%02x", (unsigned int) st_other); 1455 } 1456 1457 fprintf (file, " %s", name); 1458 } 1459 break; 1460 } 1461 } 1462 1463 /* Create an entry in an ELF linker hash table. */ 1464 1465 struct bfd_hash_entry * 1466 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, 1467 struct bfd_hash_table *table, 1468 const char *string) 1469 { 1470 /* Allocate the structure if it has not already been allocated by a 1471 subclass. */ 1472 if (entry == NULL) 1473 { 1474 entry = bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); 1475 if (entry == NULL) 1476 return entry; 1477 } 1478 1479 /* Call the allocation method of the superclass. */ 1480 entry = _bfd_link_hash_newfunc (entry, table, string); 1481 if (entry != NULL) 1482 { 1483 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; 1484 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; 1485 1486 /* Set local fields. */ 1487 ret->indx = -1; 1488 ret->dynindx = -1; 1489 ret->got = htab->init_got_refcount; 1490 ret->plt = htab->init_plt_refcount; 1491 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) 1492 - offsetof (struct elf_link_hash_entry, size))); 1493 /* Assume that we have been called by a non-ELF symbol reader. 1494 This flag is then reset by the code which reads an ELF input 1495 file. This ensures that a symbol created by a non-ELF symbol 1496 reader will have the flag set correctly. */ 1497 ret->non_elf = 1; 1498 } 1499 1500 return entry; 1501 } 1502 1503 /* Copy data from an indirect symbol to its direct symbol, hiding the 1504 old indirect symbol. Also used for copying flags to a weakdef. */ 1505 1506 void 1507 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, 1508 struct elf_link_hash_entry *dir, 1509 struct elf_link_hash_entry *ind) 1510 { 1511 struct elf_link_hash_table *htab; 1512 1513 /* Copy down any references that we may have already seen to the 1514 symbol which just became indirect. */ 1515 1516 dir->ref_dynamic |= ind->ref_dynamic; 1517 dir->ref_regular |= ind->ref_regular; 1518 dir->ref_regular_nonweak |= ind->ref_regular_nonweak; 1519 dir->non_got_ref |= ind->non_got_ref; 1520 dir->needs_plt |= ind->needs_plt; 1521 dir->pointer_equality_needed |= ind->pointer_equality_needed; 1522 1523 if (ind->root.type != bfd_link_hash_indirect) 1524 return; 1525 1526 /* Copy over the global and procedure linkage table refcount entries. 1527 These may have been already set up by a check_relocs routine. */ 1528 htab = elf_hash_table (info); 1529 if (ind->got.refcount > htab->init_got_refcount.refcount) 1530 { 1531 if (dir->got.refcount < 0) 1532 dir->got.refcount = 0; 1533 dir->got.refcount += ind->got.refcount; 1534 ind->got.refcount = htab->init_got_refcount.refcount; 1535 } 1536 1537 if (ind->plt.refcount > htab->init_plt_refcount.refcount) 1538 { 1539 if (dir->plt.refcount < 0) 1540 dir->plt.refcount = 0; 1541 dir->plt.refcount += ind->plt.refcount; 1542 ind->plt.refcount = htab->init_plt_refcount.refcount; 1543 } 1544 1545 if (ind->dynindx != -1) 1546 { 1547 if (dir->dynindx != -1) 1548 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); 1549 dir->dynindx = ind->dynindx; 1550 dir->dynstr_index = ind->dynstr_index; 1551 ind->dynindx = -1; 1552 ind->dynstr_index = 0; 1553 } 1554 } 1555 1556 void 1557 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, 1558 struct elf_link_hash_entry *h, 1559 bfd_boolean force_local) 1560 { 1561 h->plt = elf_hash_table (info)->init_plt_offset; 1562 h->needs_plt = 0; 1563 if (force_local) 1564 { 1565 h->forced_local = 1; 1566 if (h->dynindx != -1) 1567 { 1568 h->dynindx = -1; 1569 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, 1570 h->dynstr_index); 1571 } 1572 } 1573 } 1574 1575 /* Initialize an ELF linker hash table. */ 1576 1577 bfd_boolean 1578 _bfd_elf_link_hash_table_init 1579 (struct elf_link_hash_table *table, 1580 bfd *abfd, 1581 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, 1582 struct bfd_hash_table *, 1583 const char *), 1584 unsigned int entsize) 1585 { 1586 bfd_boolean ret; 1587 int can_refcount = get_elf_backend_data (abfd)->can_refcount; 1588 1589 table->dynamic_sections_created = FALSE; 1590 table->dynobj = NULL; 1591 table->init_got_refcount.refcount = can_refcount - 1; 1592 table->init_plt_refcount.refcount = can_refcount - 1; 1593 table->init_got_offset.offset = -(bfd_vma) 1; 1594 table->init_plt_offset.offset = -(bfd_vma) 1; 1595 /* The first dynamic symbol is a dummy. */ 1596 table->dynsymcount = 1; 1597 table->dynstr = NULL; 1598 table->bucketcount = 0; 1599 table->needed = NULL; 1600 table->hgot = NULL; 1601 table->merge_info = NULL; 1602 memset (&table->stab_info, 0, sizeof (table->stab_info)); 1603 memset (&table->eh_info, 0, sizeof (table->eh_info)); 1604 table->dynlocal = NULL; 1605 table->runpath = NULL; 1606 table->tls_sec = NULL; 1607 table->tls_size = 0; 1608 table->loaded = NULL; 1609 table->is_relocatable_executable = FALSE; 1610 1611 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); 1612 table->root.type = bfd_link_elf_hash_table; 1613 1614 return ret; 1615 } 1616 1617 /* Create an ELF linker hash table. */ 1618 1619 struct bfd_link_hash_table * 1620 _bfd_elf_link_hash_table_create (bfd *abfd) 1621 { 1622 struct elf_link_hash_table *ret; 1623 bfd_size_type amt = sizeof (struct elf_link_hash_table); 1624 1625 ret = bfd_malloc (amt); 1626 if (ret == NULL) 1627 return NULL; 1628 1629 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, 1630 sizeof (struct elf_link_hash_entry))) 1631 { 1632 free (ret); 1633 return NULL; 1634 } 1635 1636 return &ret->root; 1637 } 1638 1639 /* This is a hook for the ELF emulation code in the generic linker to 1640 tell the backend linker what file name to use for the DT_NEEDED 1641 entry for a dynamic object. */ 1642 1643 void 1644 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) 1645 { 1646 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 1647 && bfd_get_format (abfd) == bfd_object) 1648 elf_dt_name (abfd) = name; 1649 } 1650 1651 int 1652 bfd_elf_get_dyn_lib_class (bfd *abfd) 1653 { 1654 int lib_class; 1655 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 1656 && bfd_get_format (abfd) == bfd_object) 1657 lib_class = elf_dyn_lib_class (abfd); 1658 else 1659 lib_class = 0; 1660 return lib_class; 1661 } 1662 1663 void 1664 bfd_elf_set_dyn_lib_class (bfd *abfd, int lib_class) 1665 { 1666 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 1667 && bfd_get_format (abfd) == bfd_object) 1668 elf_dyn_lib_class (abfd) = lib_class; 1669 } 1670 1671 /* Get the list of DT_NEEDED entries for a link. This is a hook for 1672 the linker ELF emulation code. */ 1673 1674 struct bfd_link_needed_list * 1675 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, 1676 struct bfd_link_info *info) 1677 { 1678 if (! is_elf_hash_table (info->hash)) 1679 return NULL; 1680 return elf_hash_table (info)->needed; 1681 } 1682 1683 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a 1684 hook for the linker ELF emulation code. */ 1685 1686 struct bfd_link_needed_list * 1687 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, 1688 struct bfd_link_info *info) 1689 { 1690 if (! is_elf_hash_table (info->hash)) 1691 return NULL; 1692 return elf_hash_table (info)->runpath; 1693 } 1694 1695 /* Get the name actually used for a dynamic object for a link. This 1696 is the SONAME entry if there is one. Otherwise, it is the string 1697 passed to bfd_elf_set_dt_needed_name, or it is the filename. */ 1698 1699 const char * 1700 bfd_elf_get_dt_soname (bfd *abfd) 1701 { 1702 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 1703 && bfd_get_format (abfd) == bfd_object) 1704 return elf_dt_name (abfd); 1705 return NULL; 1706 } 1707 1708 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for 1709 the ELF linker emulation code. */ 1710 1711 bfd_boolean 1712 bfd_elf_get_bfd_needed_list (bfd *abfd, 1713 struct bfd_link_needed_list **pneeded) 1714 { 1715 asection *s; 1716 bfd_byte *dynbuf = NULL; 1717 int elfsec; 1718 unsigned long shlink; 1719 bfd_byte *extdyn, *extdynend; 1720 size_t extdynsize; 1721 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 1722 1723 *pneeded = NULL; 1724 1725 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour 1726 || bfd_get_format (abfd) != bfd_object) 1727 return TRUE; 1728 1729 s = bfd_get_section_by_name (abfd, ".dynamic"); 1730 if (s == NULL || s->size == 0) 1731 return TRUE; 1732 1733 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) 1734 goto error_return; 1735 1736 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 1737 if (elfsec == -1) 1738 goto error_return; 1739 1740 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 1741 1742 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 1743 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 1744 1745 extdyn = dynbuf; 1746 extdynend = extdyn + s->size; 1747 for (; extdyn < extdynend; extdyn += extdynsize) 1748 { 1749 Elf_Internal_Dyn dyn; 1750 1751 (*swap_dyn_in) (abfd, extdyn, &dyn); 1752 1753 if (dyn.d_tag == DT_NULL) 1754 break; 1755 1756 if (dyn.d_tag == DT_NEEDED) 1757 { 1758 const char *string; 1759 struct bfd_link_needed_list *l; 1760 unsigned int tagv = dyn.d_un.d_val; 1761 bfd_size_type amt; 1762 1763 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 1764 if (string == NULL) 1765 goto error_return; 1766 1767 amt = sizeof *l; 1768 l = bfd_alloc (abfd, amt); 1769 if (l == NULL) 1770 goto error_return; 1771 1772 l->by = abfd; 1773 l->name = string; 1774 l->next = *pneeded; 1775 *pneeded = l; 1776 } 1777 } 1778 1779 free (dynbuf); 1780 1781 return TRUE; 1782 1783 error_return: 1784 if (dynbuf != NULL) 1785 free (dynbuf); 1786 return FALSE; 1787 } 1788 1789 /* Allocate an ELF string table--force the first byte to be zero. */ 1790 1791 struct bfd_strtab_hash * 1792 _bfd_elf_stringtab_init (void) 1793 { 1794 struct bfd_strtab_hash *ret; 1795 1796 ret = _bfd_stringtab_init (); 1797 if (ret != NULL) 1798 { 1799 bfd_size_type loc; 1800 1801 loc = _bfd_stringtab_add (ret, "", TRUE, FALSE); 1802 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); 1803 if (loc == (bfd_size_type) -1) 1804 { 1805 _bfd_stringtab_free (ret); 1806 ret = NULL; 1807 } 1808 } 1809 return ret; 1810 } 1811 1812 /* ELF .o/exec file reading */ 1813 1814 /* Create a new bfd section from an ELF section header. */ 1815 1816 bfd_boolean 1817 bfd_section_from_shdr (bfd *abfd, unsigned int shindex) 1818 { 1819 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex]; 1820 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); 1821 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 1822 const char *name; 1823 1824 name = bfd_elf_string_from_elf_section (abfd, 1825 elf_elfheader (abfd)->e_shstrndx, 1826 hdr->sh_name); 1827 if (name == NULL) 1828 return FALSE; 1829 1830 switch (hdr->sh_type) 1831 { 1832 case SHT_NULL: 1833 /* Inactive section. Throw it away. */ 1834 return TRUE; 1835 1836 case SHT_PROGBITS: /* Normal section with contents. */ 1837 case SHT_NOBITS: /* .bss section. */ 1838 case SHT_HASH: /* .hash section. */ 1839 case SHT_NOTE: /* .note section. */ 1840 case SHT_INIT_ARRAY: /* .init_array section. */ 1841 case SHT_FINI_ARRAY: /* .fini_array section. */ 1842 case SHT_PREINIT_ARRAY: /* .preinit_array section. */ 1843 case SHT_GNU_LIBLIST: /* .gnu.liblist section. */ 1844 case SHT_GNU_HASH: /* .gnu.hash section. */ 1845 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 1846 1847 case SHT_DYNAMIC: /* Dynamic linking information. */ 1848 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 1849 return FALSE; 1850 if (hdr->sh_link > elf_numsections (abfd) 1851 || elf_elfsections (abfd)[hdr->sh_link] == NULL) 1852 return FALSE; 1853 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_STRTAB) 1854 { 1855 Elf_Internal_Shdr *dynsymhdr; 1856 1857 /* The shared libraries distributed with hpux11 have a bogus 1858 sh_link field for the ".dynamic" section. Find the 1859 string table for the ".dynsym" section instead. */ 1860 if (elf_dynsymtab (abfd) != 0) 1861 { 1862 dynsymhdr = elf_elfsections (abfd)[elf_dynsymtab (abfd)]; 1863 hdr->sh_link = dynsymhdr->sh_link; 1864 } 1865 else 1866 { 1867 unsigned int i, num_sec; 1868 1869 num_sec = elf_numsections (abfd); 1870 for (i = 1; i < num_sec; i++) 1871 { 1872 dynsymhdr = elf_elfsections (abfd)[i]; 1873 if (dynsymhdr->sh_type == SHT_DYNSYM) 1874 { 1875 hdr->sh_link = dynsymhdr->sh_link; 1876 break; 1877 } 1878 } 1879 } 1880 } 1881 break; 1882 1883 case SHT_SYMTAB: /* A symbol table */ 1884 if (elf_onesymtab (abfd) == shindex) 1885 return TRUE; 1886 1887 if (hdr->sh_entsize != bed->s->sizeof_sym) 1888 return FALSE; 1889 BFD_ASSERT (elf_onesymtab (abfd) == 0); 1890 elf_onesymtab (abfd) = shindex; 1891 elf_tdata (abfd)->symtab_hdr = *hdr; 1892 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; 1893 abfd->flags |= HAS_SYMS; 1894 1895 /* Sometimes a shared object will map in the symbol table. If 1896 SHF_ALLOC is set, and this is a shared object, then we also 1897 treat this section as a BFD section. We can not base the 1898 decision purely on SHF_ALLOC, because that flag is sometimes 1899 set in a relocatable object file, which would confuse the 1900 linker. */ 1901 if ((hdr->sh_flags & SHF_ALLOC) != 0 1902 && (abfd->flags & DYNAMIC) != 0 1903 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name, 1904 shindex)) 1905 return FALSE; 1906 1907 /* Go looking for SHT_SYMTAB_SHNDX too, since if there is one we 1908 can't read symbols without that section loaded as well. It 1909 is most likely specified by the next section header. */ 1910 if (elf_elfsections (abfd)[elf_symtab_shndx (abfd)]->sh_link != shindex) 1911 { 1912 unsigned int i, num_sec; 1913 1914 num_sec = elf_numsections (abfd); 1915 for (i = shindex + 1; i < num_sec; i++) 1916 { 1917 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 1918 if (hdr2->sh_type == SHT_SYMTAB_SHNDX 1919 && hdr2->sh_link == shindex) 1920 break; 1921 } 1922 if (i == num_sec) 1923 for (i = 1; i < shindex; i++) 1924 { 1925 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 1926 if (hdr2->sh_type == SHT_SYMTAB_SHNDX 1927 && hdr2->sh_link == shindex) 1928 break; 1929 } 1930 if (i != shindex) 1931 return bfd_section_from_shdr (abfd, i); 1932 } 1933 return TRUE; 1934 1935 case SHT_DYNSYM: /* A dynamic symbol table */ 1936 if (elf_dynsymtab (abfd) == shindex) 1937 return TRUE; 1938 1939 if (hdr->sh_entsize != bed->s->sizeof_sym) 1940 return FALSE; 1941 BFD_ASSERT (elf_dynsymtab (abfd) == 0); 1942 elf_dynsymtab (abfd) = shindex; 1943 elf_tdata (abfd)->dynsymtab_hdr = *hdr; 1944 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; 1945 abfd->flags |= HAS_SYMS; 1946 1947 /* Besides being a symbol table, we also treat this as a regular 1948 section, so that objcopy can handle it. */ 1949 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 1950 1951 case SHT_SYMTAB_SHNDX: /* Symbol section indices when >64k sections */ 1952 if (elf_symtab_shndx (abfd) == shindex) 1953 return TRUE; 1954 1955 BFD_ASSERT (elf_symtab_shndx (abfd) == 0); 1956 elf_symtab_shndx (abfd) = shindex; 1957 elf_tdata (abfd)->symtab_shndx_hdr = *hdr; 1958 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->symtab_shndx_hdr; 1959 return TRUE; 1960 1961 case SHT_STRTAB: /* A string table */ 1962 if (hdr->bfd_section != NULL) 1963 return TRUE; 1964 if (ehdr->e_shstrndx == shindex) 1965 { 1966 elf_tdata (abfd)->shstrtab_hdr = *hdr; 1967 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; 1968 return TRUE; 1969 } 1970 if (elf_elfsections (abfd)[elf_onesymtab (abfd)]->sh_link == shindex) 1971 { 1972 symtab_strtab: 1973 elf_tdata (abfd)->strtab_hdr = *hdr; 1974 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->strtab_hdr; 1975 return TRUE; 1976 } 1977 if (elf_elfsections (abfd)[elf_dynsymtab (abfd)]->sh_link == shindex) 1978 { 1979 dynsymtab_strtab: 1980 elf_tdata (abfd)->dynstrtab_hdr = *hdr; 1981 hdr = &elf_tdata (abfd)->dynstrtab_hdr; 1982 elf_elfsections (abfd)[shindex] = hdr; 1983 /* We also treat this as a regular section, so that objcopy 1984 can handle it. */ 1985 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, 1986 shindex); 1987 } 1988 1989 /* If the string table isn't one of the above, then treat it as a 1990 regular section. We need to scan all the headers to be sure, 1991 just in case this strtab section appeared before the above. */ 1992 if (elf_onesymtab (abfd) == 0 || elf_dynsymtab (abfd) == 0) 1993 { 1994 unsigned int i, num_sec; 1995 1996 num_sec = elf_numsections (abfd); 1997 for (i = 1; i < num_sec; i++) 1998 { 1999 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 2000 if (hdr2->sh_link == shindex) 2001 { 2002 /* Prevent endless recursion on broken objects. */ 2003 if (i == shindex) 2004 return FALSE; 2005 if (! bfd_section_from_shdr (abfd, i)) 2006 return FALSE; 2007 if (elf_onesymtab (abfd) == i) 2008 goto symtab_strtab; 2009 if (elf_dynsymtab (abfd) == i) 2010 goto dynsymtab_strtab; 2011 } 2012 } 2013 } 2014 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2015 2016 case SHT_REL: 2017 case SHT_RELA: 2018 /* *These* do a lot of work -- but build no sections! */ 2019 { 2020 asection *target_sect; 2021 Elf_Internal_Shdr *hdr2; 2022 unsigned int num_sec = elf_numsections (abfd); 2023 2024 if (hdr->sh_entsize 2025 != (bfd_size_type) (hdr->sh_type == SHT_REL 2026 ? bed->s->sizeof_rel : bed->s->sizeof_rela)) 2027 return FALSE; 2028 2029 /* Check for a bogus link to avoid crashing. */ 2030 if ((hdr->sh_link >= SHN_LORESERVE && hdr->sh_link <= SHN_HIRESERVE) 2031 || hdr->sh_link >= num_sec) 2032 { 2033 ((*_bfd_error_handler) 2034 (_("%B: invalid link %lu for reloc section %s (index %u)"), 2035 abfd, hdr->sh_link, name, shindex)); 2036 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2037 shindex); 2038 } 2039 2040 /* For some incomprehensible reason Oracle distributes 2041 libraries for Solaris in which some of the objects have 2042 bogus sh_link fields. It would be nice if we could just 2043 reject them, but, unfortunately, some people need to use 2044 them. We scan through the section headers; if we find only 2045 one suitable symbol table, we clobber the sh_link to point 2046 to it. I hope this doesn't break anything. */ 2047 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB 2048 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) 2049 { 2050 unsigned int scan; 2051 int found; 2052 2053 found = 0; 2054 for (scan = 1; scan < num_sec; scan++) 2055 { 2056 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB 2057 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) 2058 { 2059 if (found != 0) 2060 { 2061 found = 0; 2062 break; 2063 } 2064 found = scan; 2065 } 2066 } 2067 if (found != 0) 2068 hdr->sh_link = found; 2069 } 2070 2071 /* Get the symbol table. */ 2072 if ((elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB 2073 || elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_DYNSYM) 2074 && ! bfd_section_from_shdr (abfd, hdr->sh_link)) 2075 return FALSE; 2076 2077 /* If this reloc section does not use the main symbol table we 2078 don't treat it as a reloc section. BFD can't adequately 2079 represent such a section, so at least for now, we don't 2080 try. We just present it as a normal section. We also 2081 can't use it as a reloc section if it points to the null 2082 section, an invalid section, or another reloc section. */ 2083 if (hdr->sh_link != elf_onesymtab (abfd) 2084 || hdr->sh_info == SHN_UNDEF 2085 || (hdr->sh_info >= SHN_LORESERVE && hdr->sh_info <= SHN_HIRESERVE) 2086 || hdr->sh_info >= num_sec 2087 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_REL 2088 || elf_elfsections (abfd)[hdr->sh_info]->sh_type == SHT_RELA) 2089 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, 2090 shindex); 2091 2092 if (! bfd_section_from_shdr (abfd, hdr->sh_info)) 2093 return FALSE; 2094 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); 2095 if (target_sect == NULL) 2096 return FALSE; 2097 2098 if ((target_sect->flags & SEC_RELOC) == 0 2099 || target_sect->reloc_count == 0) 2100 hdr2 = &elf_section_data (target_sect)->rel_hdr; 2101 else 2102 { 2103 bfd_size_type amt; 2104 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL); 2105 amt = sizeof (*hdr2); 2106 hdr2 = bfd_alloc (abfd, amt); 2107 elf_section_data (target_sect)->rel_hdr2 = hdr2; 2108 } 2109 *hdr2 = *hdr; 2110 elf_elfsections (abfd)[shindex] = hdr2; 2111 target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr); 2112 target_sect->flags |= SEC_RELOC; 2113 target_sect->relocation = NULL; 2114 target_sect->rel_filepos = hdr->sh_offset; 2115 /* In the section to which the relocations apply, mark whether 2116 its relocations are of the REL or RELA variety. */ 2117 if (hdr->sh_size != 0) 2118 target_sect->use_rela_p = hdr->sh_type == SHT_RELA; 2119 abfd->flags |= HAS_RELOC; 2120 return TRUE; 2121 } 2122 break; 2123 2124 case SHT_GNU_verdef: 2125 elf_dynverdef (abfd) = shindex; 2126 elf_tdata (abfd)->dynverdef_hdr = *hdr; 2127 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2128 break; 2129 2130 case SHT_GNU_versym: 2131 if (hdr->sh_entsize != sizeof (Elf_External_Versym)) 2132 return FALSE; 2133 elf_dynversym (abfd) = shindex; 2134 elf_tdata (abfd)->dynversym_hdr = *hdr; 2135 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2136 break; 2137 2138 case SHT_GNU_verneed: 2139 elf_dynverref (abfd) = shindex; 2140 elf_tdata (abfd)->dynverref_hdr = *hdr; 2141 return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex); 2142 break; 2143 2144 case SHT_SHLIB: 2145 return TRUE; 2146 2147 case SHT_GROUP: 2148 /* We need a BFD section for objcopy and relocatable linking, 2149 and it's handy to have the signature available as the section 2150 name. */ 2151 if (hdr->sh_entsize != GRP_ENTRY_SIZE) 2152 return FALSE; 2153 name = group_signature (abfd, hdr); 2154 if (name == NULL) 2155 return FALSE; 2156 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2157 return FALSE; 2158 if (hdr->contents != NULL) 2159 { 2160 Elf_Internal_Group *idx = (Elf_Internal_Group *) hdr->contents; 2161 unsigned int n_elt = hdr->sh_size / 4; 2162 asection *s; 2163 2164 if (idx->flags & GRP_COMDAT) 2165 hdr->bfd_section->flags 2166 |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 2167 2168 /* We try to keep the same section order as it comes in. */ 2169 idx += n_elt; 2170 while (--n_elt != 0) 2171 if ((s = (--idx)->shdr->bfd_section) != NULL 2172 && elf_next_in_group (s) != NULL) 2173 { 2174 elf_next_in_group (hdr->bfd_section) = s; 2175 break; 2176 } 2177 } 2178 break; 2179 2180 case SHT_LLVM_LINKER_OPTIONS: 2181 case SHT_LLVM_ADDRSIG: 2182 return TRUE; 2183 2184 default: 2185 /* Check for any processor-specific section types. */ 2186 return bed->elf_backend_section_from_shdr (abfd, hdr, name, 2187 shindex); 2188 } 2189 2190 return TRUE; 2191 } 2192 2193 /* Return the section for the local symbol specified by ABFD, R_SYMNDX. 2194 Return SEC for sections that have no elf section, and NULL on error. */ 2195 2196 asection * 2197 bfd_section_from_r_symndx (bfd *abfd, 2198 struct sym_sec_cache *cache, 2199 asection *sec, 2200 unsigned long r_symndx) 2201 { 2202 Elf_Internal_Shdr *symtab_hdr; 2203 unsigned char esym[sizeof (Elf64_External_Sym)]; 2204 Elf_External_Sym_Shndx eshndx; 2205 Elf_Internal_Sym isym; 2206 unsigned int ent = r_symndx % LOCAL_SYM_CACHE_SIZE; 2207 2208 if (cache->abfd == abfd && cache->indx[ent] == r_symndx) 2209 return cache->sec[ent]; 2210 2211 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2212 if (bfd_elf_get_elf_syms (abfd, symtab_hdr, 1, r_symndx, 2213 &isym, esym, &eshndx) == NULL) 2214 return NULL; 2215 2216 if (cache->abfd != abfd) 2217 { 2218 memset (cache->indx, -1, sizeof (cache->indx)); 2219 cache->abfd = abfd; 2220 } 2221 cache->indx[ent] = r_symndx; 2222 cache->sec[ent] = sec; 2223 if ((isym.st_shndx != SHN_UNDEF && isym.st_shndx < SHN_LORESERVE) 2224 || isym.st_shndx > SHN_HIRESERVE) 2225 { 2226 asection *s; 2227 s = bfd_section_from_elf_index (abfd, isym.st_shndx); 2228 if (s != NULL) 2229 cache->sec[ent] = s; 2230 } 2231 return cache->sec[ent]; 2232 } 2233 2234 /* Given an ELF section number, retrieve the corresponding BFD 2235 section. */ 2236 2237 asection * 2238 bfd_section_from_elf_index (bfd *abfd, unsigned int index) 2239 { 2240 if (index >= elf_numsections (abfd)) 2241 return NULL; 2242 return elf_elfsections (abfd)[index]->bfd_section; 2243 } 2244 2245 static const struct bfd_elf_special_section special_sections_b[] = 2246 { 2247 { ".bss", 4, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 2248 { NULL, 0, 0, 0, 0 } 2249 }; 2250 2251 static const struct bfd_elf_special_section special_sections_c[] = 2252 { 2253 { ".comment", 8, 0, SHT_PROGBITS, 0 }, 2254 { NULL, 0, 0, 0, 0 } 2255 }; 2256 2257 static const struct bfd_elf_special_section special_sections_d[] = 2258 { 2259 { ".data", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 2260 { ".data1", 6, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 2261 { ".debug", 6, 0, SHT_PROGBITS, 0 }, 2262 { ".debug_line", 11, 0, SHT_PROGBITS, 0 }, 2263 { ".debug_info", 11, 0, SHT_PROGBITS, 0 }, 2264 { ".debug_abbrev", 13, 0, SHT_PROGBITS, 0 }, 2265 { ".debug_aranges", 14, 0, SHT_PROGBITS, 0 }, 2266 { ".dynamic", 8, 0, SHT_DYNAMIC, SHF_ALLOC }, 2267 { ".dynstr", 7, 0, SHT_STRTAB, SHF_ALLOC }, 2268 { ".dynsym", 7, 0, SHT_DYNSYM, SHF_ALLOC }, 2269 { NULL, 0, 0, 0, 0 } 2270 }; 2271 2272 static const struct bfd_elf_special_section special_sections_f[] = 2273 { 2274 { ".fini", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 2275 { ".fini_array", 11, 0, SHT_FINI_ARRAY, SHF_ALLOC + SHF_WRITE }, 2276 { NULL, 0, 0, 0, 0 } 2277 }; 2278 2279 static const struct bfd_elf_special_section special_sections_g[] = 2280 { 2281 { ".gnu.linkonce.b",15, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, 2282 { ".got", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE }, 2283 { ".gnu.version", 12, 0, SHT_GNU_versym, 0 }, 2284 { ".gnu.version_d", 14, 0, SHT_GNU_verdef, 0 }, 2285 { ".gnu.version_r", 14, 0, SHT_GNU_verneed, 0 }, 2286 { ".gnu.liblist", 12, 0, SHT_GNU_LIBLIST, SHF_ALLOC }, 2287 { ".gnu.conflict", 13, 0, SHT_RELA, SHF_ALLOC }, 2288 { ".gnu.hash", 9, 0, SHT_GNU_HASH, SHF_ALLOC }, 2289 { NULL, 0, 0, 0, 0 } 2290 }; 2291 2292 static const struct bfd_elf_special_section special_sections_h[] = 2293 { 2294 { ".hash", 5, 0, SHT_HASH, SHF_ALLOC }, 2295 { NULL, 0, 0, 0, 0 } 2296 }; 2297 2298 static const struct bfd_elf_special_section special_sections_i[] = 2299 { 2300 { ".init", 5, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 2301 { ".init_array", 11, 0, SHT_INIT_ARRAY, SHF_ALLOC + SHF_WRITE }, 2302 { ".interp", 7, 0, SHT_PROGBITS, 0 }, 2303 { NULL, 0, 0, 0, 0 } 2304 }; 2305 2306 static const struct bfd_elf_special_section special_sections_l[] = 2307 { 2308 { ".line", 5, 0, SHT_PROGBITS, 0 }, 2309 { NULL, 0, 0, 0, 0 } 2310 }; 2311 2312 static const struct bfd_elf_special_section special_sections_n[] = 2313 { 2314 { ".note.GNU-stack",15, 0, SHT_PROGBITS, 0 }, 2315 { ".note", 5, -1, SHT_NOTE, 0 }, 2316 { NULL, 0, 0, 0, 0 } 2317 }; 2318 2319 static const struct bfd_elf_special_section special_sections_p[] = 2320 { 2321 { ".preinit_array", 14, 0, SHT_PREINIT_ARRAY, SHF_ALLOC + SHF_WRITE }, 2322 { ".plt", 4, 0, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 2323 { NULL, 0, 0, 0, 0 } 2324 }; 2325 2326 static const struct bfd_elf_special_section special_sections_r[] = 2327 { 2328 { ".rodata", 7, -2, SHT_PROGBITS, SHF_ALLOC }, 2329 { ".rodata1", 8, 0, SHT_PROGBITS, SHF_ALLOC }, 2330 { ".rela", 5, -1, SHT_RELA, 0 }, 2331 { ".rel", 4, -1, SHT_REL, 0 }, 2332 { NULL, 0, 0, 0, 0 } 2333 }; 2334 2335 static const struct bfd_elf_special_section special_sections_s[] = 2336 { 2337 { ".shstrtab", 9, 0, SHT_STRTAB, 0 }, 2338 { ".strtab", 7, 0, SHT_STRTAB, 0 }, 2339 { ".symtab", 7, 0, SHT_SYMTAB, 0 }, 2340 { ".stabstr", 5, 3, SHT_STRTAB, 0 }, 2341 { NULL, 0, 0, 0, 0 } 2342 }; 2343 2344 static const struct bfd_elf_special_section special_sections_t[] = 2345 { 2346 { ".text", 5, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR }, 2347 { ".tbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, 2348 { ".tdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE + SHF_TLS }, 2349 { NULL, 0, 0, 0, 0 } 2350 }; 2351 2352 static const struct bfd_elf_special_section *special_sections[] = 2353 { 2354 special_sections_b, /* 'b' */ 2355 special_sections_c, /* 'b' */ 2356 special_sections_d, /* 'd' */ 2357 NULL, /* 'e' */ 2358 special_sections_f, /* 'f' */ 2359 special_sections_g, /* 'g' */ 2360 special_sections_h, /* 'h' */ 2361 special_sections_i, /* 'i' */ 2362 NULL, /* 'j' */ 2363 NULL, /* 'k' */ 2364 special_sections_l, /* 'l' */ 2365 NULL, /* 'm' */ 2366 special_sections_n, /* 'n' */ 2367 NULL, /* 'o' */ 2368 special_sections_p, /* 'p' */ 2369 NULL, /* 'q' */ 2370 special_sections_r, /* 'r' */ 2371 special_sections_s, /* 's' */ 2372 special_sections_t, /* 't' */ 2373 }; 2374 2375 const struct bfd_elf_special_section * 2376 _bfd_elf_get_special_section (const char *name, 2377 const struct bfd_elf_special_section *spec, 2378 unsigned int rela) 2379 { 2380 int i; 2381 int len; 2382 2383 len = strlen (name); 2384 2385 for (i = 0; spec[i].prefix != NULL; i++) 2386 { 2387 int suffix_len; 2388 int prefix_len = spec[i].prefix_length; 2389 2390 if (len < prefix_len) 2391 continue; 2392 if (memcmp (name, spec[i].prefix, prefix_len) != 0) 2393 continue; 2394 2395 suffix_len = spec[i].suffix_length; 2396 if (suffix_len <= 0) 2397 { 2398 if (name[prefix_len] != 0) 2399 { 2400 if (suffix_len == 0) 2401 continue; 2402 if (name[prefix_len] != '.' 2403 && (suffix_len == -2 2404 || (rela && spec[i].type == SHT_REL))) 2405 continue; 2406 } 2407 } 2408 else 2409 { 2410 if (len < prefix_len + suffix_len) 2411 continue; 2412 if (memcmp (name + len - suffix_len, 2413 spec[i].prefix + prefix_len, 2414 suffix_len) != 0) 2415 continue; 2416 } 2417 return &spec[i]; 2418 } 2419 2420 return NULL; 2421 } 2422 2423 const struct bfd_elf_special_section * 2424 _bfd_elf_get_sec_type_attr (bfd *abfd, asection *sec) 2425 { 2426 int i; 2427 const struct bfd_elf_special_section *spec; 2428 const struct elf_backend_data *bed; 2429 2430 /* See if this is one of the special sections. */ 2431 if (sec->name == NULL) 2432 return NULL; 2433 2434 bed = get_elf_backend_data (abfd); 2435 spec = bed->special_sections; 2436 if (spec) 2437 { 2438 spec = _bfd_elf_get_special_section (sec->name, 2439 bed->special_sections, 2440 sec->use_rela_p); 2441 if (spec != NULL) 2442 return spec; 2443 } 2444 2445 if (sec->name[0] != '.') 2446 return NULL; 2447 2448 i = sec->name[1] - 'b'; 2449 if (i < 0 || i > 't' - 'b') 2450 return NULL; 2451 2452 spec = special_sections[i]; 2453 2454 if (spec == NULL) 2455 return NULL; 2456 2457 return _bfd_elf_get_special_section (sec->name, spec, sec->use_rela_p); 2458 } 2459 2460 bfd_boolean 2461 _bfd_elf_new_section_hook (bfd *abfd, asection *sec) 2462 { 2463 struct bfd_elf_section_data *sdata; 2464 const struct elf_backend_data *bed; 2465 const struct bfd_elf_special_section *ssect; 2466 2467 sdata = (struct bfd_elf_section_data *) sec->used_by_bfd; 2468 if (sdata == NULL) 2469 { 2470 sdata = bfd_zalloc (abfd, sizeof (*sdata)); 2471 if (sdata == NULL) 2472 return FALSE; 2473 sec->used_by_bfd = sdata; 2474 } 2475 2476 /* Indicate whether or not this section should use RELA relocations. */ 2477 bed = get_elf_backend_data (abfd); 2478 sec->use_rela_p = bed->default_use_rela_p; 2479 2480 /* When we read a file, we don't need section type and flags unless 2481 it is a linker created section. They will be overridden in 2482 _bfd_elf_make_section_from_shdr anyway. */ 2483 if (abfd->direction != read_direction 2484 || (sec->flags & SEC_LINKER_CREATED) != 0) 2485 { 2486 ssect = (*bed->get_sec_type_attr) (abfd, sec); 2487 if (ssect != NULL) 2488 { 2489 elf_section_type (sec) = ssect->type; 2490 elf_section_flags (sec) = ssect->attr; 2491 } 2492 } 2493 2494 return TRUE; 2495 } 2496 2497 /* Create a new bfd section from an ELF program header. 2498 2499 Since program segments have no names, we generate a synthetic name 2500 of the form segment<NUM>, where NUM is generally the index in the 2501 program header table. For segments that are split (see below) we 2502 generate the names segment<NUM>a and segment<NUM>b. 2503 2504 Note that some program segments may have a file size that is different than 2505 (less than) the memory size. All this means is that at execution the 2506 system must allocate the amount of memory specified by the memory size, 2507 but only initialize it with the first "file size" bytes read from the 2508 file. This would occur for example, with program segments consisting 2509 of combined data+bss. 2510 2511 To handle the above situation, this routine generates TWO bfd sections 2512 for the single program segment. The first has the length specified by 2513 the file size of the segment, and the second has the length specified 2514 by the difference between the two sizes. In effect, the segment is split 2515 into it's initialized and uninitialized parts. 2516 2517 */ 2518 2519 bfd_boolean 2520 _bfd_elf_make_section_from_phdr (bfd *abfd, 2521 Elf_Internal_Phdr *hdr, 2522 int index, 2523 const char *typename) 2524 { 2525 asection *newsect; 2526 char *name; 2527 char namebuf[64]; 2528 size_t len; 2529 int split; 2530 2531 split = ((hdr->p_memsz > 0) 2532 && (hdr->p_filesz > 0) 2533 && (hdr->p_memsz > hdr->p_filesz)); 2534 sprintf (namebuf, "%s%d%s", typename, index, split ? "a" : ""); 2535 len = strlen (namebuf) + 1; 2536 name = bfd_alloc (abfd, len); 2537 if (!name) 2538 return FALSE; 2539 memcpy (name, namebuf, len); 2540 newsect = bfd_make_section (abfd, name); 2541 if (newsect == NULL) 2542 return FALSE; 2543 newsect->vma = hdr->p_vaddr; 2544 newsect->lma = hdr->p_paddr; 2545 newsect->size = hdr->p_filesz; 2546 newsect->filepos = hdr->p_offset; 2547 newsect->flags |= SEC_HAS_CONTENTS; 2548 newsect->alignment_power = bfd_log2 (hdr->p_align); 2549 if (hdr->p_type == PT_LOAD) 2550 { 2551 newsect->flags |= SEC_ALLOC; 2552 newsect->flags |= SEC_LOAD; 2553 if (hdr->p_flags & PF_X) 2554 { 2555 /* FIXME: all we known is that it has execute PERMISSION, 2556 may be data. */ 2557 newsect->flags |= SEC_CODE; 2558 } 2559 } 2560 if (!(hdr->p_flags & PF_W)) 2561 { 2562 newsect->flags |= SEC_READONLY; 2563 } 2564 2565 if (split) 2566 { 2567 sprintf (namebuf, "%s%db", typename, index); 2568 len = strlen (namebuf) + 1; 2569 name = bfd_alloc (abfd, len); 2570 if (!name) 2571 return FALSE; 2572 memcpy (name, namebuf, len); 2573 newsect = bfd_make_section (abfd, name); 2574 if (newsect == NULL) 2575 return FALSE; 2576 newsect->vma = hdr->p_vaddr + hdr->p_filesz; 2577 newsect->lma = hdr->p_paddr + hdr->p_filesz; 2578 newsect->size = hdr->p_memsz - hdr->p_filesz; 2579 if (hdr->p_type == PT_LOAD) 2580 { 2581 newsect->flags |= SEC_ALLOC; 2582 if (hdr->p_flags & PF_X) 2583 newsect->flags |= SEC_CODE; 2584 } 2585 if (!(hdr->p_flags & PF_W)) 2586 newsect->flags |= SEC_READONLY; 2587 } 2588 2589 return TRUE; 2590 } 2591 2592 bfd_boolean 2593 bfd_section_from_phdr (bfd *abfd, Elf_Internal_Phdr *hdr, int index) 2594 { 2595 const struct elf_backend_data *bed; 2596 2597 switch (hdr->p_type) 2598 { 2599 case PT_NULL: 2600 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "null"); 2601 2602 case PT_LOAD: 2603 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "load"); 2604 2605 case PT_DYNAMIC: 2606 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "dynamic"); 2607 2608 case PT_INTERP: 2609 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "interp"); 2610 2611 case PT_NOTE: 2612 if (! _bfd_elf_make_section_from_phdr (abfd, hdr, index, "note")) 2613 return FALSE; 2614 if (! elfcore_read_notes (abfd, hdr->p_offset, hdr->p_filesz)) 2615 return FALSE; 2616 return TRUE; 2617 2618 case PT_SHLIB: 2619 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "shlib"); 2620 2621 case PT_PHDR: 2622 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "phdr"); 2623 2624 case PT_GNU_EH_FRAME: 2625 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, 2626 "eh_frame_hdr"); 2627 2628 case PT_GNU_STACK: 2629 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "stack"); 2630 2631 case PT_GNU_RELRO: 2632 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, "relro"); 2633 2634 case PT_OPENBSD_RANDOMIZE: 2635 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, 2636 "openbsd_randomize"); 2637 2638 case PT_OPENBSD_WXNEEDED: 2639 return _bfd_elf_make_section_from_phdr (abfd, hdr, index, 2640 "openbsd_wxneeded"); 2641 2642 default: 2643 /* Check for any processor-specific program segment types. */ 2644 bed = get_elf_backend_data (abfd); 2645 return bed->elf_backend_section_from_phdr (abfd, hdr, index, "proc"); 2646 } 2647 } 2648 2649 /* Initialize REL_HDR, the section-header for new section, containing 2650 relocations against ASECT. If USE_RELA_P is TRUE, we use RELA 2651 relocations; otherwise, we use REL relocations. */ 2652 2653 bfd_boolean 2654 _bfd_elf_init_reloc_shdr (bfd *abfd, 2655 Elf_Internal_Shdr *rel_hdr, 2656 asection *asect, 2657 bfd_boolean use_rela_p) 2658 { 2659 char *name; 2660 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 2661 bfd_size_type amt = sizeof ".rela" + strlen (asect->name); 2662 2663 name = bfd_alloc (abfd, amt); 2664 if (name == NULL) 2665 return FALSE; 2666 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); 2667 rel_hdr->sh_name = 2668 (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), name, 2669 FALSE); 2670 if (rel_hdr->sh_name == (unsigned int) -1) 2671 return FALSE; 2672 rel_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; 2673 rel_hdr->sh_entsize = (use_rela_p 2674 ? bed->s->sizeof_rela 2675 : bed->s->sizeof_rel); 2676 rel_hdr->sh_addralign = 1 << bed->s->log_file_align; 2677 rel_hdr->sh_flags = 0; 2678 rel_hdr->sh_addr = 0; 2679 rel_hdr->sh_size = 0; 2680 rel_hdr->sh_offset = 0; 2681 2682 return TRUE; 2683 } 2684 2685 /* Set up an ELF internal section header for a section. */ 2686 2687 static void 2688 elf_fake_sections (bfd *abfd, asection *asect, void *failedptrarg) 2689 { 2690 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 2691 bfd_boolean *failedptr = failedptrarg; 2692 Elf_Internal_Shdr *this_hdr; 2693 2694 if (*failedptr) 2695 { 2696 /* We already failed; just get out of the bfd_map_over_sections 2697 loop. */ 2698 return; 2699 } 2700 2701 this_hdr = &elf_section_data (asect)->this_hdr; 2702 2703 this_hdr->sh_name = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 2704 asect->name, FALSE); 2705 if (this_hdr->sh_name == (unsigned int) -1) 2706 { 2707 *failedptr = TRUE; 2708 return; 2709 } 2710 2711 /* Don't clear sh_flags. Assembler may set additional bits. */ 2712 2713 if ((asect->flags & SEC_ALLOC) != 0 2714 || asect->user_set_vma) 2715 this_hdr->sh_addr = asect->vma; 2716 else 2717 this_hdr->sh_addr = 0; 2718 2719 this_hdr->sh_offset = 0; 2720 this_hdr->sh_size = asect->size; 2721 this_hdr->sh_link = 0; 2722 this_hdr->sh_addralign = 1 << asect->alignment_power; 2723 /* The sh_entsize and sh_info fields may have been set already by 2724 copy_private_section_data. */ 2725 2726 this_hdr->bfd_section = asect; 2727 this_hdr->contents = NULL; 2728 2729 /* If the section type is unspecified, we set it based on 2730 asect->flags. */ 2731 if (this_hdr->sh_type == SHT_NULL) 2732 { 2733 if ((asect->flags & SEC_GROUP) != 0) 2734 this_hdr->sh_type = SHT_GROUP; 2735 else if ((asect->flags & SEC_ALLOC) != 0 2736 && (((asect->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0) 2737 || (asect->flags & SEC_NEVER_LOAD) != 0)) 2738 this_hdr->sh_type = SHT_NOBITS; 2739 else 2740 this_hdr->sh_type = SHT_PROGBITS; 2741 } 2742 2743 switch (this_hdr->sh_type) 2744 { 2745 default: 2746 break; 2747 2748 case SHT_STRTAB: 2749 case SHT_INIT_ARRAY: 2750 case SHT_FINI_ARRAY: 2751 case SHT_PREINIT_ARRAY: 2752 case SHT_NOTE: 2753 case SHT_NOBITS: 2754 case SHT_PROGBITS: 2755 break; 2756 2757 case SHT_HASH: 2758 this_hdr->sh_entsize = bed->s->sizeof_hash_entry; 2759 break; 2760 2761 case SHT_DYNSYM: 2762 this_hdr->sh_entsize = bed->s->sizeof_sym; 2763 break; 2764 2765 case SHT_DYNAMIC: 2766 this_hdr->sh_entsize = bed->s->sizeof_dyn; 2767 break; 2768 2769 case SHT_RELA: 2770 if (get_elf_backend_data (abfd)->may_use_rela_p) 2771 this_hdr->sh_entsize = bed->s->sizeof_rela; 2772 break; 2773 2774 case SHT_REL: 2775 if (get_elf_backend_data (abfd)->may_use_rel_p) 2776 this_hdr->sh_entsize = bed->s->sizeof_rel; 2777 break; 2778 2779 case SHT_GNU_versym: 2780 this_hdr->sh_entsize = sizeof (Elf_External_Versym); 2781 break; 2782 2783 case SHT_GNU_verdef: 2784 this_hdr->sh_entsize = 0; 2785 /* objcopy or strip will copy over sh_info, but may not set 2786 cverdefs. The linker will set cverdefs, but sh_info will be 2787 zero. */ 2788 if (this_hdr->sh_info == 0) 2789 this_hdr->sh_info = elf_tdata (abfd)->cverdefs; 2790 else 2791 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 2792 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); 2793 break; 2794 2795 case SHT_GNU_verneed: 2796 this_hdr->sh_entsize = 0; 2797 /* objcopy or strip will copy over sh_info, but may not set 2798 cverrefs. The linker will set cverrefs, but sh_info will be 2799 zero. */ 2800 if (this_hdr->sh_info == 0) 2801 this_hdr->sh_info = elf_tdata (abfd)->cverrefs; 2802 else 2803 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 2804 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); 2805 break; 2806 2807 case SHT_GROUP: 2808 this_hdr->sh_entsize = 4; 2809 break; 2810 2811 case SHT_GNU_HASH: 2812 this_hdr->sh_entsize = bed->s->arch_size == 64 ? 0 : 4; 2813 break; 2814 } 2815 2816 if ((asect->flags & SEC_ALLOC) != 0) 2817 this_hdr->sh_flags |= SHF_ALLOC; 2818 if ((asect->flags & SEC_READONLY) == 0) 2819 this_hdr->sh_flags |= SHF_WRITE; 2820 if ((asect->flags & SEC_CODE) != 0) 2821 this_hdr->sh_flags |= SHF_EXECINSTR; 2822 if ((asect->flags & SEC_MERGE) != 0) 2823 { 2824 this_hdr->sh_flags |= SHF_MERGE; 2825 this_hdr->sh_entsize = asect->entsize; 2826 if ((asect->flags & SEC_STRINGS) != 0) 2827 this_hdr->sh_flags |= SHF_STRINGS; 2828 } 2829 if ((asect->flags & SEC_GROUP) == 0 && elf_group_name (asect) != NULL) 2830 this_hdr->sh_flags |= SHF_GROUP; 2831 if ((asect->flags & SEC_THREAD_LOCAL) != 0) 2832 { 2833 this_hdr->sh_flags |= SHF_TLS; 2834 if (asect->size == 0 2835 && (asect->flags & SEC_HAS_CONTENTS) == 0) 2836 { 2837 struct bfd_link_order *o = asect->map_tail.link_order; 2838 2839 this_hdr->sh_size = 0; 2840 if (o != NULL) 2841 { 2842 this_hdr->sh_size = o->offset + o->size; 2843 if (this_hdr->sh_size != 0) 2844 this_hdr->sh_type = SHT_NOBITS; 2845 } 2846 } 2847 } 2848 2849 /* Check for processor-specific section types. */ 2850 if (bed->elf_backend_fake_sections 2851 && !(*bed->elf_backend_fake_sections) (abfd, this_hdr, asect)) 2852 *failedptr = TRUE; 2853 2854 /* If the section has relocs, set up a section header for the 2855 SHT_REL[A] section. If two relocation sections are required for 2856 this section, it is up to the processor-specific back-end to 2857 create the other. */ 2858 if ((asect->flags & SEC_RELOC) != 0 2859 && !_bfd_elf_init_reloc_shdr (abfd, 2860 &elf_section_data (asect)->rel_hdr, 2861 asect, 2862 asect->use_rela_p)) 2863 *failedptr = TRUE; 2864 } 2865 2866 /* Fill in the contents of a SHT_GROUP section. */ 2867 2868 void 2869 bfd_elf_set_group_contents (bfd *abfd, asection *sec, void *failedptrarg) 2870 { 2871 bfd_boolean *failedptr = failedptrarg; 2872 unsigned long symindx; 2873 asection *elt, *first; 2874 unsigned char *loc; 2875 bfd_boolean gas; 2876 2877 /* Ignore linker created group section. See elfNN_ia64_object_p in 2878 elfxx-ia64.c. */ 2879 if (((sec->flags & (SEC_GROUP | SEC_LINKER_CREATED)) != SEC_GROUP) 2880 || *failedptr) 2881 return; 2882 2883 symindx = 0; 2884 if (elf_group_id (sec) != NULL) 2885 symindx = elf_group_id (sec)->udata.i; 2886 2887 if (symindx == 0) 2888 { 2889 /* If called from the assembler, swap_out_syms will have set up 2890 elf_section_syms; If called for "ld -r", use target_index. */ 2891 if (elf_section_syms (abfd) != NULL) 2892 symindx = elf_section_syms (abfd)[sec->index]->udata.i; 2893 else 2894 symindx = sec->target_index; 2895 } 2896 elf_section_data (sec)->this_hdr.sh_info = symindx; 2897 2898 /* The contents won't be allocated for "ld -r" or objcopy. */ 2899 gas = TRUE; 2900 if (sec->contents == NULL) 2901 { 2902 gas = FALSE; 2903 sec->contents = bfd_alloc (abfd, sec->size); 2904 2905 /* Arrange for the section to be written out. */ 2906 elf_section_data (sec)->this_hdr.contents = sec->contents; 2907 if (sec->contents == NULL) 2908 { 2909 *failedptr = TRUE; 2910 return; 2911 } 2912 } 2913 2914 loc = sec->contents + sec->size; 2915 2916 /* Get the pointer to the first section in the group that gas 2917 squirreled away here. objcopy arranges for this to be set to the 2918 start of the input section group. */ 2919 first = elt = elf_next_in_group (sec); 2920 2921 /* First element is a flag word. Rest of section is elf section 2922 indices for all the sections of the group. Write them backwards 2923 just to keep the group in the same order as given in .section 2924 directives, not that it matters. */ 2925 while (elt != NULL) 2926 { 2927 asection *s; 2928 unsigned int idx; 2929 2930 loc -= 4; 2931 s = elt; 2932 if (!gas) 2933 s = s->output_section; 2934 idx = 0; 2935 if (s != NULL) 2936 idx = elf_section_data (s)->this_idx; 2937 H_PUT_32 (abfd, idx, loc); 2938 elt = elf_next_in_group (elt); 2939 if (elt == first) 2940 break; 2941 } 2942 2943 if ((loc -= 4) != sec->contents) 2944 abort (); 2945 2946 H_PUT_32 (abfd, sec->flags & SEC_LINK_ONCE ? GRP_COMDAT : 0, loc); 2947 } 2948 2949 /* Assign all ELF section numbers. The dummy first section is handled here 2950 too. The link/info pointers for the standard section types are filled 2951 in here too, while we're at it. */ 2952 2953 static bfd_boolean 2954 assign_section_numbers (bfd *abfd, struct bfd_link_info *link_info) 2955 { 2956 struct elf_obj_tdata *t = elf_tdata (abfd); 2957 asection *sec; 2958 unsigned int section_number, secn; 2959 Elf_Internal_Shdr **i_shdrp; 2960 struct bfd_elf_section_data *d; 2961 2962 section_number = 1; 2963 2964 _bfd_elf_strtab_clear_all_refs (elf_shstrtab (abfd)); 2965 2966 /* SHT_GROUP sections are in relocatable files only. */ 2967 if (link_info == NULL || link_info->relocatable) 2968 { 2969 /* Put SHT_GROUP sections first. */ 2970 for (sec = abfd->sections; sec != NULL; sec = sec->next) 2971 { 2972 d = elf_section_data (sec); 2973 2974 if (d->this_hdr.sh_type == SHT_GROUP) 2975 { 2976 if (sec->flags & SEC_LINKER_CREATED) 2977 { 2978 /* Remove the linker created SHT_GROUP sections. */ 2979 bfd_section_list_remove (abfd, sec); 2980 abfd->section_count--; 2981 } 2982 else 2983 { 2984 if (section_number == SHN_LORESERVE) 2985 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 2986 d->this_idx = section_number++; 2987 } 2988 } 2989 } 2990 } 2991 2992 for (sec = abfd->sections; sec; sec = sec->next) 2993 { 2994 d = elf_section_data (sec); 2995 2996 if (d->this_hdr.sh_type != SHT_GROUP) 2997 { 2998 if (section_number == SHN_LORESERVE) 2999 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 3000 d->this_idx = section_number++; 3001 } 3002 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->this_hdr.sh_name); 3003 if ((sec->flags & SEC_RELOC) == 0) 3004 d->rel_idx = 0; 3005 else 3006 { 3007 if (section_number == SHN_LORESERVE) 3008 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 3009 d->rel_idx = section_number++; 3010 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr.sh_name); 3011 } 3012 3013 if (d->rel_hdr2) 3014 { 3015 if (section_number == SHN_LORESERVE) 3016 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 3017 d->rel_idx2 = section_number++; 3018 _bfd_elf_strtab_addref (elf_shstrtab (abfd), d->rel_hdr2->sh_name); 3019 } 3020 else 3021 d->rel_idx2 = 0; 3022 } 3023 3024 if (section_number == SHN_LORESERVE) 3025 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 3026 t->shstrtab_section = section_number++; 3027 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->shstrtab_hdr.sh_name); 3028 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; 3029 3030 if (bfd_get_symcount (abfd) > 0) 3031 { 3032 if (section_number == SHN_LORESERVE) 3033 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 3034 t->symtab_section = section_number++; 3035 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->symtab_hdr.sh_name); 3036 if (section_number > SHN_LORESERVE - 2) 3037 { 3038 if (section_number == SHN_LORESERVE) 3039 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 3040 t->symtab_shndx_section = section_number++; 3041 t->symtab_shndx_hdr.sh_name 3042 = (unsigned int) _bfd_elf_strtab_add (elf_shstrtab (abfd), 3043 ".symtab_shndx", FALSE); 3044 if (t->symtab_shndx_hdr.sh_name == (unsigned int) -1) 3045 return FALSE; 3046 } 3047 if (section_number == SHN_LORESERVE) 3048 section_number += SHN_HIRESERVE + 1 - SHN_LORESERVE; 3049 t->strtab_section = section_number++; 3050 _bfd_elf_strtab_addref (elf_shstrtab (abfd), t->strtab_hdr.sh_name); 3051 } 3052 3053 _bfd_elf_strtab_finalize (elf_shstrtab (abfd)); 3054 t->shstrtab_hdr.sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); 3055 3056 elf_numsections (abfd) = section_number; 3057 elf_elfheader (abfd)->e_shnum = section_number; 3058 if (section_number > SHN_LORESERVE) 3059 elf_elfheader (abfd)->e_shnum -= SHN_HIRESERVE + 1 - SHN_LORESERVE; 3060 3061 /* Set up the list of section header pointers, in agreement with the 3062 indices. */ 3063 i_shdrp = bfd_zalloc2 (abfd, section_number, sizeof (Elf_Internal_Shdr *)); 3064 if (i_shdrp == NULL) 3065 return FALSE; 3066 3067 i_shdrp[0] = bfd_zalloc (abfd, sizeof (Elf_Internal_Shdr)); 3068 if (i_shdrp[0] == NULL) 3069 { 3070 bfd_release (abfd, i_shdrp); 3071 return FALSE; 3072 } 3073 3074 elf_elfsections (abfd) = i_shdrp; 3075 3076 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; 3077 if (bfd_get_symcount (abfd) > 0) 3078 { 3079 i_shdrp[t->symtab_section] = &t->symtab_hdr; 3080 if (elf_numsections (abfd) > SHN_LORESERVE) 3081 { 3082 i_shdrp[t->symtab_shndx_section] = &t->symtab_shndx_hdr; 3083 t->symtab_shndx_hdr.sh_link = t->symtab_section; 3084 } 3085 i_shdrp[t->strtab_section] = &t->strtab_hdr; 3086 t->symtab_hdr.sh_link = t->strtab_section; 3087 } 3088 3089 for (sec = abfd->sections; sec; sec = sec->next) 3090 { 3091 struct bfd_elf_section_data *d = elf_section_data (sec); 3092 asection *s; 3093 const char *name; 3094 3095 i_shdrp[d->this_idx] = &d->this_hdr; 3096 if (d->rel_idx != 0) 3097 i_shdrp[d->rel_idx] = &d->rel_hdr; 3098 if (d->rel_idx2 != 0) 3099 i_shdrp[d->rel_idx2] = d->rel_hdr2; 3100 3101 /* Fill in the sh_link and sh_info fields while we're at it. */ 3102 3103 /* sh_link of a reloc section is the section index of the symbol 3104 table. sh_info is the section index of the section to which 3105 the relocation entries apply. */ 3106 if (d->rel_idx != 0) 3107 { 3108 d->rel_hdr.sh_link = t->symtab_section; 3109 d->rel_hdr.sh_info = d->this_idx; 3110 } 3111 if (d->rel_idx2 != 0) 3112 { 3113 d->rel_hdr2->sh_link = t->symtab_section; 3114 d->rel_hdr2->sh_info = d->this_idx; 3115 } 3116 3117 /* We need to set up sh_link for SHF_LINK_ORDER. */ 3118 if ((d->this_hdr.sh_flags & SHF_LINK_ORDER) != 0) 3119 { 3120 s = elf_linked_to_section (sec); 3121 if (s) 3122 { 3123 /* elf_linked_to_section points to the input section. */ 3124 if (link_info != NULL) 3125 { 3126 /* Check discarded linkonce section. */ 3127 if (elf_discarded_section (s)) 3128 { 3129 asection *kept; 3130 (*_bfd_error_handler) 3131 (_("%B: sh_link of section `%A' points to discarded section `%A' of `%B'"), 3132 abfd, d->this_hdr.bfd_section, 3133 s, s->owner); 3134 /* Point to the kept section if it has the same 3135 size as the discarded one. */ 3136 kept = _bfd_elf_check_kept_section (s, link_info); 3137 if (kept == NULL) 3138 { 3139 bfd_set_error (bfd_error_bad_value); 3140 return FALSE; 3141 } 3142 s = kept; 3143 } 3144 3145 s = s->output_section; 3146 BFD_ASSERT (s != NULL); 3147 } 3148 else 3149 { 3150 /* Handle objcopy. */ 3151 if (s->output_section == NULL) 3152 { 3153 (*_bfd_error_handler) 3154 (_("%B: sh_link of section `%A' points to removed section `%A' of `%B'"), 3155 abfd, d->this_hdr.bfd_section, s, s->owner); 3156 bfd_set_error (bfd_error_bad_value); 3157 return FALSE; 3158 } 3159 s = s->output_section; 3160 } 3161 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 3162 } 3163 else 3164 { 3165 /* PR 290: 3166 The Intel C compiler generates SHT_IA_64_UNWIND with 3167 SHF_LINK_ORDER. But it doesn't set the sh_link or 3168 sh_info fields. Hence we could get the situation 3169 where s is NULL. */ 3170 const struct elf_backend_data *bed 3171 = get_elf_backend_data (abfd); 3172 if (bed->link_order_error_handler) 3173 bed->link_order_error_handler 3174 (_("%B: warning: sh_link not set for section `%A'"), 3175 abfd, sec); 3176 } 3177 } 3178 3179 switch (d->this_hdr.sh_type) 3180 { 3181 case SHT_REL: 3182 case SHT_RELA: 3183 /* A reloc section which we are treating as a normal BFD 3184 section. sh_link is the section index of the symbol 3185 table. sh_info is the section index of the section to 3186 which the relocation entries apply. We assume that an 3187 allocated reloc section uses the dynamic symbol table. 3188 FIXME: How can we be sure? */ 3189 s = bfd_get_section_by_name (abfd, ".dynsym"); 3190 if (s != NULL) 3191 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 3192 3193 /* We look up the section the relocs apply to by name. */ 3194 name = sec->name; 3195 if (d->this_hdr.sh_type == SHT_REL) 3196 name += 4; 3197 else 3198 name += 5; 3199 s = bfd_get_section_by_name (abfd, name); 3200 if (s != NULL) 3201 d->this_hdr.sh_info = elf_section_data (s)->this_idx; 3202 break; 3203 3204 case SHT_STRTAB: 3205 /* We assume that a section named .stab*str is a stabs 3206 string section. We look for a section with the same name 3207 but without the trailing ``str'', and set its sh_link 3208 field to point to this section. */ 3209 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0 3210 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) 3211 { 3212 size_t len; 3213 char *alc; 3214 3215 len = strlen (sec->name); 3216 alc = bfd_malloc (len - 2); 3217 if (alc == NULL) 3218 return FALSE; 3219 memcpy (alc, sec->name, len - 3); 3220 alc[len - 3] = '\0'; 3221 s = bfd_get_section_by_name (abfd, alc); 3222 free (alc); 3223 if (s != NULL) 3224 { 3225 elf_section_data (s)->this_hdr.sh_link = d->this_idx; 3226 3227 /* This is a .stab section. */ 3228 if (elf_section_data (s)->this_hdr.sh_entsize == 0) 3229 elf_section_data (s)->this_hdr.sh_entsize 3230 = 4 + 2 * bfd_get_arch_size (abfd) / 8; 3231 } 3232 } 3233 break; 3234 3235 case SHT_DYNAMIC: 3236 case SHT_DYNSYM: 3237 case SHT_GNU_verneed: 3238 case SHT_GNU_verdef: 3239 /* sh_link is the section header index of the string table 3240 used for the dynamic entries, or the symbol table, or the 3241 version strings. */ 3242 s = bfd_get_section_by_name (abfd, ".dynstr"); 3243 if (s != NULL) 3244 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 3245 break; 3246 3247 case SHT_GNU_LIBLIST: 3248 /* sh_link is the section header index of the prelink library 3249 list 3250 used for the dynamic entries, or the symbol table, or the 3251 version strings. */ 3252 s = bfd_get_section_by_name (abfd, (sec->flags & SEC_ALLOC) 3253 ? ".dynstr" : ".gnu.libstr"); 3254 if (s != NULL) 3255 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 3256 break; 3257 3258 case SHT_HASH: 3259 case SHT_GNU_HASH: 3260 case SHT_GNU_versym: 3261 /* sh_link is the section header index of the symbol table 3262 this hash table or version table is for. */ 3263 s = bfd_get_section_by_name (abfd, ".dynsym"); 3264 if (s != NULL) 3265 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 3266 break; 3267 3268 case SHT_GROUP: 3269 d->this_hdr.sh_link = t->symtab_section; 3270 } 3271 } 3272 3273 for (secn = 1; secn < section_number; ++secn) 3274 if (i_shdrp[secn] == NULL) 3275 i_shdrp[secn] = i_shdrp[0]; 3276 else 3277 i_shdrp[secn]->sh_name = _bfd_elf_strtab_offset (elf_shstrtab (abfd), 3278 i_shdrp[secn]->sh_name); 3279 return TRUE; 3280 } 3281 3282 /* Map symbol from it's internal number to the external number, moving 3283 all local symbols to be at the head of the list. */ 3284 3285 static int 3286 sym_is_global (bfd *abfd, asymbol *sym) 3287 { 3288 /* If the backend has a special mapping, use it. */ 3289 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3290 if (bed->elf_backend_sym_is_global) 3291 return (*bed->elf_backend_sym_is_global) (abfd, sym); 3292 3293 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0 3294 || bfd_is_und_section (bfd_get_section (sym)) 3295 || bfd_is_com_section (bfd_get_section (sym))); 3296 } 3297 3298 static bfd_boolean 3299 elf_map_symbols (bfd *abfd) 3300 { 3301 unsigned int symcount = bfd_get_symcount (abfd); 3302 asymbol **syms = bfd_get_outsymbols (abfd); 3303 asymbol **sect_syms; 3304 unsigned int num_locals = 0; 3305 unsigned int num_globals = 0; 3306 unsigned int num_locals2 = 0; 3307 unsigned int num_globals2 = 0; 3308 int max_index = 0; 3309 unsigned int idx; 3310 asection *asect; 3311 asymbol **new_syms; 3312 3313 #ifdef DEBUG 3314 fprintf (stderr, "elf_map_symbols\n"); 3315 fflush (stderr); 3316 #endif 3317 3318 for (asect = abfd->sections; asect; asect = asect->next) 3319 { 3320 if (max_index < asect->index) 3321 max_index = asect->index; 3322 } 3323 3324 max_index++; 3325 sect_syms = bfd_zalloc2 (abfd, max_index, sizeof (asymbol *)); 3326 if (sect_syms == NULL) 3327 return FALSE; 3328 elf_section_syms (abfd) = sect_syms; 3329 elf_num_section_syms (abfd) = max_index; 3330 3331 /* Init sect_syms entries for any section symbols we have already 3332 decided to output. */ 3333 for (idx = 0; idx < symcount; idx++) 3334 { 3335 asymbol *sym = syms[idx]; 3336 3337 if ((sym->flags & BSF_SECTION_SYM) != 0 3338 && sym->value == 0) 3339 { 3340 asection *sec; 3341 3342 sec = sym->section; 3343 3344 if (sec->owner != NULL) 3345 { 3346 if (sec->owner != abfd) 3347 { 3348 if (sec->output_offset != 0) 3349 continue; 3350 3351 sec = sec->output_section; 3352 3353 /* Empty sections in the input files may have had a 3354 section symbol created for them. (See the comment 3355 near the end of _bfd_generic_link_output_symbols in 3356 linker.c). If the linker script discards such 3357 sections then we will reach this point. Since we know 3358 that we cannot avoid this case, we detect it and skip 3359 the abort and the assignment to the sect_syms array. 3360 To reproduce this particular case try running the 3361 linker testsuite test ld-scripts/weak.exp for an ELF 3362 port that uses the generic linker. */ 3363 if (sec->owner == NULL) 3364 continue; 3365 3366 BFD_ASSERT (sec->owner == abfd); 3367 } 3368 sect_syms[sec->index] = syms[idx]; 3369 } 3370 } 3371 } 3372 3373 /* Classify all of the symbols. */ 3374 for (idx = 0; idx < symcount; idx++) 3375 { 3376 if (!sym_is_global (abfd, syms[idx])) 3377 num_locals++; 3378 else 3379 num_globals++; 3380 } 3381 3382 /* We will be adding a section symbol for each BFD section. Most normal 3383 sections will already have a section symbol in outsymbols, but 3384 eg. SHT_GROUP sections will not, and we need the section symbol mapped 3385 at least in that case. */ 3386 for (asect = abfd->sections; asect; asect = asect->next) 3387 { 3388 if (sect_syms[asect->index] == NULL) 3389 { 3390 if (!sym_is_global (abfd, asect->symbol)) 3391 num_locals++; 3392 else 3393 num_globals++; 3394 } 3395 } 3396 3397 /* Now sort the symbols so the local symbols are first. */ 3398 new_syms = bfd_alloc2 (abfd, num_locals + num_globals, sizeof (asymbol *)); 3399 3400 if (new_syms == NULL) 3401 return FALSE; 3402 3403 for (idx = 0; idx < symcount; idx++) 3404 { 3405 asymbol *sym = syms[idx]; 3406 unsigned int i; 3407 3408 if (!sym_is_global (abfd, sym)) 3409 i = num_locals2++; 3410 else 3411 i = num_locals + num_globals2++; 3412 new_syms[i] = sym; 3413 sym->udata.i = i + 1; 3414 } 3415 for (asect = abfd->sections; asect; asect = asect->next) 3416 { 3417 if (sect_syms[asect->index] == NULL) 3418 { 3419 asymbol *sym = asect->symbol; 3420 unsigned int i; 3421 3422 sect_syms[asect->index] = sym; 3423 if (!sym_is_global (abfd, sym)) 3424 i = num_locals2++; 3425 else 3426 i = num_locals + num_globals2++; 3427 new_syms[i] = sym; 3428 sym->udata.i = i + 1; 3429 } 3430 } 3431 3432 bfd_set_symtab (abfd, new_syms, num_locals + num_globals); 3433 3434 elf_num_locals (abfd) = num_locals; 3435 elf_num_globals (abfd) = num_globals; 3436 return TRUE; 3437 } 3438 3439 /* Align to the maximum file alignment that could be required for any 3440 ELF data structure. */ 3441 3442 static inline file_ptr 3443 align_file_position (file_ptr off, int align) 3444 { 3445 return (off + align - 1) & ~(align - 1); 3446 } 3447 3448 /* Assign a file position to a section, optionally aligning to the 3449 required section alignment. */ 3450 3451 file_ptr 3452 _bfd_elf_assign_file_position_for_section (Elf_Internal_Shdr *i_shdrp, 3453 file_ptr offset, 3454 bfd_boolean align) 3455 { 3456 if (align) 3457 { 3458 unsigned int al; 3459 3460 al = i_shdrp->sh_addralign; 3461 if (al > 1) 3462 offset = BFD_ALIGN (offset, al); 3463 } 3464 i_shdrp->sh_offset = offset; 3465 if (i_shdrp->bfd_section != NULL) 3466 i_shdrp->bfd_section->filepos = offset; 3467 if (i_shdrp->sh_type != SHT_NOBITS) 3468 offset += i_shdrp->sh_size; 3469 return offset; 3470 } 3471 3472 /* Compute the file positions we are going to put the sections at, and 3473 otherwise prepare to begin writing out the ELF file. If LINK_INFO 3474 is not NULL, this is being called by the ELF backend linker. */ 3475 3476 bfd_boolean 3477 _bfd_elf_compute_section_file_positions (bfd *abfd, 3478 struct bfd_link_info *link_info) 3479 { 3480 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3481 bfd_boolean failed; 3482 struct bfd_strtab_hash *strtab = NULL; 3483 Elf_Internal_Shdr *shstrtab_hdr; 3484 3485 if (abfd->output_has_begun) 3486 return TRUE; 3487 3488 /* Do any elf backend specific processing first. */ 3489 if (bed->elf_backend_begin_write_processing) 3490 (*bed->elf_backend_begin_write_processing) (abfd, link_info); 3491 3492 if (! prep_headers (abfd)) 3493 return FALSE; 3494 3495 /* Post process the headers if necessary. */ 3496 if (bed->elf_backend_post_process_headers) 3497 (*bed->elf_backend_post_process_headers) (abfd, link_info); 3498 3499 failed = FALSE; 3500 bfd_map_over_sections (abfd, elf_fake_sections, &failed); 3501 if (failed) 3502 return FALSE; 3503 3504 if (!assign_section_numbers (abfd, link_info)) 3505 return FALSE; 3506 3507 /* The backend linker builds symbol table information itself. */ 3508 if (link_info == NULL && bfd_get_symcount (abfd) > 0) 3509 { 3510 /* Non-zero if doing a relocatable link. */ 3511 int relocatable_p = ! (abfd->flags & (EXEC_P | DYNAMIC)); 3512 3513 if (! swap_out_syms (abfd, &strtab, relocatable_p)) 3514 return FALSE; 3515 } 3516 3517 if (link_info == NULL) 3518 { 3519 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); 3520 if (failed) 3521 return FALSE; 3522 } 3523 3524 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; 3525 /* sh_name was set in prep_headers. */ 3526 shstrtab_hdr->sh_type = SHT_STRTAB; 3527 shstrtab_hdr->sh_flags = 0; 3528 shstrtab_hdr->sh_addr = 0; 3529 shstrtab_hdr->sh_size = _bfd_elf_strtab_size (elf_shstrtab (abfd)); 3530 shstrtab_hdr->sh_entsize = 0; 3531 shstrtab_hdr->sh_link = 0; 3532 shstrtab_hdr->sh_info = 0; 3533 /* sh_offset is set in assign_file_positions_except_relocs. */ 3534 shstrtab_hdr->sh_addralign = 1; 3535 3536 if (!assign_file_positions_except_relocs (abfd, link_info)) 3537 return FALSE; 3538 3539 if (link_info == NULL && bfd_get_symcount (abfd) > 0) 3540 { 3541 file_ptr off; 3542 Elf_Internal_Shdr *hdr; 3543 3544 off = elf_tdata (abfd)->next_file_pos; 3545 3546 hdr = &elf_tdata (abfd)->symtab_hdr; 3547 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); 3548 3549 hdr = &elf_tdata (abfd)->symtab_shndx_hdr; 3550 if (hdr->sh_size != 0) 3551 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); 3552 3553 hdr = &elf_tdata (abfd)->strtab_hdr; 3554 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); 3555 3556 elf_tdata (abfd)->next_file_pos = off; 3557 3558 /* Now that we know where the .strtab section goes, write it 3559 out. */ 3560 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 3561 || ! _bfd_stringtab_emit (abfd, strtab)) 3562 return FALSE; 3563 _bfd_stringtab_free (strtab); 3564 } 3565 3566 abfd->output_has_begun = TRUE; 3567 3568 return TRUE; 3569 } 3570 3571 /* Create a mapping from a set of sections to a program segment. */ 3572 3573 static struct elf_segment_map * 3574 make_mapping (bfd *abfd, 3575 asection **sections, 3576 unsigned int from, 3577 unsigned int to, 3578 bfd_boolean phdr) 3579 { 3580 struct elf_segment_map *m; 3581 unsigned int i; 3582 asection **hdrpp; 3583 bfd_size_type amt; 3584 3585 amt = sizeof (struct elf_segment_map); 3586 amt += (to - from - 1) * sizeof (asection *); 3587 m = bfd_zalloc (abfd, amt); 3588 if (m == NULL) 3589 return NULL; 3590 m->next = NULL; 3591 m->p_type = PT_LOAD; 3592 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) 3593 m->sections[i - from] = *hdrpp; 3594 m->count = to - from; 3595 3596 if (from == 0 && phdr) 3597 { 3598 /* Include the headers in the first PT_LOAD segment. */ 3599 m->includes_filehdr = 1; 3600 m->includes_phdrs = 1; 3601 } 3602 3603 return m; 3604 } 3605 3606 /* Create the PT_DYNAMIC segment, which includes DYNSEC. Returns NULL 3607 on failure. */ 3608 3609 struct elf_segment_map * 3610 _bfd_elf_make_dynamic_segment (bfd *abfd, asection *dynsec) 3611 { 3612 struct elf_segment_map *m; 3613 3614 m = bfd_zalloc (abfd, sizeof (struct elf_segment_map)); 3615 if (m == NULL) 3616 return NULL; 3617 m->next = NULL; 3618 m->p_type = PT_DYNAMIC; 3619 m->count = 1; 3620 m->sections[0] = dynsec; 3621 3622 return m; 3623 } 3624 3625 /* Set up a mapping from BFD sections to program segments. */ 3626 3627 static bfd_boolean 3628 map_sections_to_segments (bfd *abfd) 3629 { 3630 asection **sections = NULL; 3631 asection *s; 3632 unsigned int i; 3633 unsigned int count; 3634 struct elf_segment_map *mfirst; 3635 struct elf_segment_map **pm; 3636 struct elf_segment_map *m; 3637 asection *last_hdr; 3638 bfd_vma last_size; 3639 unsigned int phdr_index; 3640 bfd_vma maxpagesize; 3641 asection **hdrpp; 3642 bfd_boolean phdr_in_segment = TRUE; 3643 bfd_boolean writable; 3644 int tls_count = 0; 3645 asection *first_tls = NULL; 3646 asection *dynsec, *eh_frame_hdr, *randomdata; 3647 bfd_size_type amt; 3648 3649 if (elf_tdata (abfd)->segment_map != NULL) 3650 return TRUE; 3651 3652 if (bfd_count_sections (abfd) == 0) 3653 return TRUE; 3654 3655 /* Select the allocated sections, and sort them. */ 3656 3657 sections = bfd_malloc2 (bfd_count_sections (abfd), sizeof (asection *)); 3658 if (sections == NULL) 3659 goto error_return; 3660 3661 i = 0; 3662 for (s = abfd->sections; s != NULL; s = s->next) 3663 { 3664 if ((s->flags & SEC_ALLOC) != 0) 3665 { 3666 sections[i] = s; 3667 ++i; 3668 } 3669 } 3670 BFD_ASSERT (i <= bfd_count_sections (abfd)); 3671 count = i; 3672 3673 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); 3674 3675 /* Build the mapping. */ 3676 3677 mfirst = NULL; 3678 pm = &mfirst; 3679 3680 /* If we have a .interp section, or are creating an executable and 3681 have a .dynamic section, then create a PT_PHDR segment for the 3682 program headers. */ 3683 s = bfd_get_section_by_name (abfd, ".interp"); 3684 if ((s != NULL && (s->flags & SEC_LOAD) != 0) || 3685 (bfd_get_section_by_name (abfd, ".dynamic") && 3686 elf_tdata (abfd)->executable)) 3687 { 3688 amt = sizeof (struct elf_segment_map); 3689 m = bfd_zalloc (abfd, amt); 3690 if (m == NULL) 3691 goto error_return; 3692 m->next = NULL; 3693 m->p_type = PT_PHDR; 3694 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ 3695 m->p_flags = PF_R | PF_X; 3696 m->p_flags_valid = 1; 3697 m->includes_phdrs = 1; 3698 3699 *pm = m; 3700 pm = &m->next; 3701 } 3702 3703 /* If we have a .interp section, then create a PT_INTERP segment for 3704 the .interp section. */ 3705 if (s != NULL && (s->flags & SEC_LOAD) != 0) 3706 { 3707 amt = sizeof (struct elf_segment_map); 3708 m = bfd_zalloc (abfd, amt); 3709 if (m == NULL) 3710 goto error_return; 3711 m->next = NULL; 3712 m->p_type = PT_INTERP; 3713 m->count = 1; 3714 m->sections[0] = s; 3715 3716 *pm = m; 3717 pm = &m->next; 3718 } 3719 3720 /* Look through the sections. We put sections in the same program 3721 segment when the start of the second section can be placed within 3722 a few bytes of the end of the first section. */ 3723 last_hdr = NULL; 3724 last_size = 0; 3725 phdr_index = 0; 3726 maxpagesize = get_elf_backend_data (abfd)->maxpagesize; 3727 writable = FALSE; 3728 dynsec = bfd_get_section_by_name (abfd, ".dynamic"); 3729 if (dynsec != NULL 3730 && (dynsec->flags & SEC_LOAD) == 0) 3731 dynsec = NULL; 3732 3733 /* Deal with -Ttext or something similar such that the first section 3734 is not adjacent to the program headers. This is an 3735 approximation, since at this point we don't know exactly how many 3736 program headers we will need. */ 3737 if (count > 0) 3738 { 3739 bfd_size_type phdr_size; 3740 3741 phdr_size = elf_tdata (abfd)->program_header_size; 3742 if (phdr_size == 0) 3743 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr; 3744 if ((abfd->flags & D_PAGED) == 0 3745 || sections[0]->lma < phdr_size 3746 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize) 3747 phdr_in_segment = FALSE; 3748 } 3749 3750 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) 3751 { 3752 asection *hdr; 3753 bfd_boolean new_segment; 3754 3755 hdr = *hdrpp; 3756 3757 /* See if this section and the last one will fit in the same 3758 segment. */ 3759 3760 if (last_hdr == NULL) 3761 { 3762 /* If we don't have a segment yet, then we don't need a new 3763 one (we build the last one after this loop). */ 3764 new_segment = FALSE; 3765 } 3766 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) 3767 { 3768 /* If this section has a different relation between the 3769 virtual address and the load address, then we need a new 3770 segment. */ 3771 new_segment = TRUE; 3772 } 3773 else if (BFD_ALIGN (last_hdr->lma + last_size, maxpagesize) 3774 < BFD_ALIGN (hdr->lma, maxpagesize)) 3775 { 3776 /* If putting this section in this segment would force us to 3777 skip a page in the segment, then we need a new segment. */ 3778 new_segment = TRUE; 3779 } 3780 else if ((last_hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0 3781 && (hdr->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) != 0) 3782 { 3783 /* We don't want to put a loadable section after a 3784 nonloadable section in the same segment. 3785 Consider .tbss sections as loadable for this purpose. */ 3786 new_segment = TRUE; 3787 } 3788 else if ((abfd->flags & D_PAGED) == 0) 3789 { 3790 /* If the file is not demand paged, which means that we 3791 don't require the sections to be correctly aligned in the 3792 file, then there is no other reason for a new segment. */ 3793 new_segment = FALSE; 3794 } 3795 else if (! writable 3796 && (hdr->flags & SEC_READONLY) == 0 3797 && (((last_hdr->lma + last_size - 1) 3798 & ~(maxpagesize - 1)) 3799 != (hdr->lma & ~(maxpagesize - 1)))) 3800 { 3801 /* We don't want to put a writable section in a read only 3802 segment, unless they are on the same page in memory 3803 anyhow. We already know that the last section does not 3804 bring us past the current section on the page, so the 3805 only case in which the new section is not on the same 3806 page as the previous section is when the previous section 3807 ends precisely on a page boundary. */ 3808 new_segment = TRUE; 3809 } 3810 else 3811 { 3812 /* Otherwise, we can use the same segment. */ 3813 new_segment = FALSE; 3814 } 3815 3816 if (! new_segment) 3817 { 3818 if ((hdr->flags & SEC_READONLY) == 0) 3819 writable = TRUE; 3820 last_hdr = hdr; 3821 /* .tbss sections effectively have zero size. */ 3822 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) 3823 last_size = hdr->size; 3824 else 3825 last_size = 0; 3826 continue; 3827 } 3828 3829 /* We need a new program segment. We must create a new program 3830 header holding all the sections from phdr_index until hdr. */ 3831 3832 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); 3833 if (m == NULL) 3834 goto error_return; 3835 3836 *pm = m; 3837 pm = &m->next; 3838 3839 if ((hdr->flags & SEC_READONLY) == 0) 3840 writable = TRUE; 3841 else 3842 writable = FALSE; 3843 3844 last_hdr = hdr; 3845 /* .tbss sections effectively have zero size. */ 3846 if ((hdr->flags & (SEC_THREAD_LOCAL | SEC_LOAD)) != SEC_THREAD_LOCAL) 3847 last_size = hdr->size; 3848 else 3849 last_size = 0; 3850 phdr_index = i; 3851 phdr_in_segment = FALSE; 3852 } 3853 3854 /* Create a final PT_LOAD program segment. */ 3855 if (last_hdr != NULL) 3856 { 3857 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_segment); 3858 if (m == NULL) 3859 goto error_return; 3860 3861 *pm = m; 3862 pm = &m->next; 3863 } 3864 3865 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ 3866 if (dynsec != NULL) 3867 { 3868 m = _bfd_elf_make_dynamic_segment (abfd, dynsec); 3869 if (m == NULL) 3870 goto error_return; 3871 *pm = m; 3872 pm = &m->next; 3873 } 3874 3875 /* For each loadable .note section, add a PT_NOTE segment. We don't 3876 use bfd_get_section_by_name, because if we link together 3877 nonloadable .note sections and loadable .note sections, we will 3878 generate two .note sections in the output file. FIXME: Using 3879 names for section types is bogus anyhow. */ 3880 for (s = abfd->sections; s != NULL; s = s->next) 3881 { 3882 if ((s->flags & SEC_LOAD) != 0 3883 && strncmp (s->name, ".note", 5) == 0) 3884 { 3885 amt = sizeof (struct elf_segment_map); 3886 m = bfd_zalloc (abfd, amt); 3887 if (m == NULL) 3888 goto error_return; 3889 m->next = NULL; 3890 m->p_type = PT_NOTE; 3891 m->count = 1; 3892 m->sections[0] = s; 3893 3894 *pm = m; 3895 pm = &m->next; 3896 } 3897 if (s->flags & SEC_THREAD_LOCAL) 3898 { 3899 if (! tls_count) 3900 first_tls = s; 3901 tls_count++; 3902 } 3903 } 3904 3905 /* If there are any SHF_TLS output sections, add PT_TLS segment. */ 3906 if (tls_count > 0) 3907 { 3908 int i; 3909 3910 amt = sizeof (struct elf_segment_map); 3911 amt += (tls_count - 1) * sizeof (asection *); 3912 m = bfd_zalloc (abfd, amt); 3913 if (m == NULL) 3914 goto error_return; 3915 m->next = NULL; 3916 m->p_type = PT_TLS; 3917 m->count = tls_count; 3918 /* Mandated PF_R. */ 3919 m->p_flags = PF_R; 3920 m->p_flags_valid = 1; 3921 for (i = 0; i < tls_count; ++i) 3922 { 3923 BFD_ASSERT (first_tls->flags & SEC_THREAD_LOCAL); 3924 m->sections[i] = first_tls; 3925 first_tls = first_tls->next; 3926 } 3927 3928 *pm = m; 3929 pm = &m->next; 3930 } 3931 3932 /* If there is a .eh_frame_hdr section, throw in a PT_GNU_EH_FRAME 3933 segment. */ 3934 eh_frame_hdr = elf_tdata (abfd)->eh_frame_hdr; 3935 if (eh_frame_hdr != NULL 3936 && (eh_frame_hdr->output_section->flags & SEC_LOAD) != 0) 3937 { 3938 amt = sizeof (struct elf_segment_map); 3939 m = bfd_zalloc (abfd, amt); 3940 if (m == NULL) 3941 goto error_return; 3942 m->next = NULL; 3943 m->p_type = PT_GNU_EH_FRAME; 3944 m->count = 1; 3945 m->sections[0] = eh_frame_hdr->output_section; 3946 3947 *pm = m; 3948 pm = &m->next; 3949 } 3950 3951 if (elf_tdata (abfd)->stack_flags) 3952 { 3953 amt = sizeof (struct elf_segment_map); 3954 m = bfd_zalloc (abfd, amt); 3955 if (m == NULL) 3956 goto error_return; 3957 m->next = NULL; 3958 m->p_type = PT_GNU_STACK; 3959 m->p_flags = elf_tdata (abfd)->stack_flags; 3960 m->p_flags_valid = 1; 3961 3962 *pm = m; 3963 pm = &m->next; 3964 } 3965 3966 if (elf_tdata (abfd)->wxneeded) 3967 { 3968 amt = sizeof (struct elf_segment_map); 3969 m = bfd_zalloc (abfd, amt); 3970 if (m == NULL) 3971 goto error_return; 3972 m->next = NULL; 3973 m->p_type = PT_OPENBSD_WXNEEDED; 3974 m->p_flags = 1; 3975 m->p_flags_valid = 1; 3976 3977 *pm = m; 3978 pm = &m->next; 3979 } 3980 3981 /* If there is a .openbsd.randomdata section, throw in a PT_OPENBSD_RANDOMIZE 3982 segment. */ 3983 randomdata = bfd_get_section_by_name (abfd, ".openbsd.randomdata"); 3984 if (randomdata != NULL && (randomdata->flags & SEC_LOAD) != 0) 3985 { 3986 amt = sizeof (struct elf_segment_map); 3987 m = bfd_zalloc (abfd, amt); 3988 if (m == NULL) 3989 goto error_return; 3990 m->next = NULL; 3991 m->p_type = PT_OPENBSD_RANDOMIZE; 3992 m->count = 1; 3993 m->sections[0] = randomdata->output_section; 3994 3995 *pm = m; 3996 pm = &m->next; 3997 } 3998 3999 if (elf_tdata (abfd)->relro) 4000 { 4001 amt = sizeof (struct elf_segment_map); 4002 m = bfd_zalloc (abfd, amt); 4003 if (m == NULL) 4004 goto error_return; 4005 m->next = NULL; 4006 m->p_type = PT_GNU_RELRO; 4007 m->p_flags = PF_R; 4008 m->p_flags_valid = 1; 4009 4010 *pm = m; 4011 pm = &m->next; 4012 } 4013 4014 free (sections); 4015 sections = NULL; 4016 4017 elf_tdata (abfd)->segment_map = mfirst; 4018 return TRUE; 4019 4020 error_return: 4021 if (sections != NULL) 4022 free (sections); 4023 return FALSE; 4024 } 4025 4026 /* Sort sections by address. */ 4027 4028 static int 4029 elf_sort_sections (const void *arg1, const void *arg2) 4030 { 4031 const asection *sec1 = *(const asection **) arg1; 4032 const asection *sec2 = *(const asection **) arg2; 4033 bfd_size_type size1, size2; 4034 4035 /* Sort by LMA first, since this is the address used to 4036 place the section into a segment. */ 4037 if (sec1->lma < sec2->lma) 4038 return -1; 4039 else if (sec1->lma > sec2->lma) 4040 return 1; 4041 4042 /* Then sort by VMA. Normally the LMA and the VMA will be 4043 the same, and this will do nothing. */ 4044 if (sec1->vma < sec2->vma) 4045 return -1; 4046 else if (sec1->vma > sec2->vma) 4047 return 1; 4048 4049 /* Put !SEC_LOAD sections after SEC_LOAD ones. */ 4050 4051 #define TOEND(x) (((x)->flags & (SEC_LOAD | SEC_THREAD_LOCAL)) == 0) 4052 4053 if (TOEND (sec1)) 4054 { 4055 if (TOEND (sec2)) 4056 { 4057 /* If the indicies are the same, do not return 0 4058 here, but continue to try the next comparison. */ 4059 if (sec1->target_index - sec2->target_index != 0) 4060 return sec1->target_index - sec2->target_index; 4061 } 4062 else 4063 return 1; 4064 } 4065 else if (TOEND (sec2)) 4066 return -1; 4067 4068 #undef TOEND 4069 4070 /* Sort by size, to put zero sized sections 4071 before others at the same address. */ 4072 4073 size1 = (sec1->flags & SEC_LOAD) ? sec1->size : 0; 4074 size2 = (sec2->flags & SEC_LOAD) ? sec2->size : 0; 4075 4076 if (size1 < size2) 4077 return -1; 4078 if (size1 > size2) 4079 return 1; 4080 4081 return sec1->target_index - sec2->target_index; 4082 } 4083 4084 /* Ian Lance Taylor writes: 4085 4086 We shouldn't be using % with a negative signed number. That's just 4087 not good. We have to make sure either that the number is not 4088 negative, or that the number has an unsigned type. When the types 4089 are all the same size they wind up as unsigned. When file_ptr is a 4090 larger signed type, the arithmetic winds up as signed long long, 4091 which is wrong. 4092 4093 What we're trying to say here is something like ``increase OFF by 4094 the least amount that will cause it to be equal to the VMA modulo 4095 the page size.'' */ 4096 /* In other words, something like: 4097 4098 vma_offset = m->sections[0]->vma % bed->maxpagesize; 4099 off_offset = off % bed->maxpagesize; 4100 if (vma_offset < off_offset) 4101 adjustment = vma_offset + bed->maxpagesize - off_offset; 4102 else 4103 adjustment = vma_offset - off_offset; 4104 4105 which can can be collapsed into the expression below. */ 4106 4107 static file_ptr 4108 vma_page_aligned_bias (bfd_vma vma, ufile_ptr off, bfd_vma maxpagesize) 4109 { 4110 return ((vma - off) % maxpagesize); 4111 } 4112 4113 static void 4114 print_segment_map (bfd *abfd) 4115 { 4116 struct elf_segment_map *m; 4117 unsigned int i, j; 4118 4119 fprintf (stderr, _(" Section to Segment mapping:\n")); 4120 fprintf (stderr, _(" Segment Sections...\n")); 4121 4122 for (i= 0, m = elf_tdata (abfd)->segment_map; 4123 m != NULL; 4124 i++, m = m->next) 4125 { 4126 const char *pt = get_segment_type (m->p_type); 4127 char buf[32]; 4128 4129 if (pt == NULL) 4130 { 4131 if (m->p_type >= PT_LOPROC && m->p_type <= PT_HIPROC) 4132 sprintf (buf, "LOPROC+%7.7x", 4133 (unsigned int) (m->p_type - PT_LOPROC)); 4134 else if (m->p_type >= PT_LOOS && m->p_type <= PT_HIOS) 4135 sprintf (buf, "LOOS+%7.7x", 4136 (unsigned int) (m->p_type - PT_LOOS)); 4137 else 4138 snprintf (buf, sizeof (buf), "%8.8x", 4139 (unsigned int) m->p_type); 4140 pt = buf; 4141 } 4142 fprintf (stderr, " %2.2d: %14.14s: ", i, pt); 4143 for (j = 0; j < m->count; j++) 4144 fprintf (stderr, "%s ", m->sections [j]->name); 4145 putc ('\n',stderr); 4146 } 4147 } 4148 4149 /* Assign file positions to the sections based on the mapping from 4150 sections to segments. This function also sets up some fields in 4151 the file header, and writes out the program headers. */ 4152 4153 static bfd_boolean 4154 assign_file_positions_for_segments (bfd *abfd, struct bfd_link_info *link_info) 4155 { 4156 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4157 unsigned int count; 4158 struct elf_segment_map *m; 4159 unsigned int alloc; 4160 Elf_Internal_Phdr *phdrs; 4161 file_ptr off, voff; 4162 bfd_vma filehdr_vaddr, filehdr_paddr; 4163 bfd_vma phdrs_vaddr, phdrs_paddr; 4164 Elf_Internal_Phdr *p; 4165 4166 if (elf_tdata (abfd)->segment_map == NULL) 4167 { 4168 if (! map_sections_to_segments (abfd)) 4169 return FALSE; 4170 } 4171 else 4172 { 4173 /* The placement algorithm assumes that non allocated sections are 4174 not in PT_LOAD segments. We ensure this here by removing such 4175 sections from the segment map. We also remove excluded 4176 sections. */ 4177 for (m = elf_tdata (abfd)->segment_map; 4178 m != NULL; 4179 m = m->next) 4180 { 4181 unsigned int new_count; 4182 unsigned int i; 4183 4184 new_count = 0; 4185 for (i = 0; i < m->count; i ++) 4186 { 4187 if ((m->sections[i]->flags & SEC_EXCLUDE) == 0 4188 && ((m->sections[i]->flags & SEC_ALLOC) != 0 4189 || m->p_type != PT_LOAD)) 4190 { 4191 if (i != new_count) 4192 m->sections[new_count] = m->sections[i]; 4193 4194 new_count ++; 4195 } 4196 } 4197 4198 if (new_count != m->count) 4199 m->count = new_count; 4200 } 4201 } 4202 4203 if (bed->elf_backend_modify_segment_map) 4204 { 4205 if (! (*bed->elf_backend_modify_segment_map) (abfd, link_info)) 4206 return FALSE; 4207 } 4208 4209 count = 0; 4210 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) 4211 ++count; 4212 4213 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; 4214 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; 4215 elf_elfheader (abfd)->e_phnum = count; 4216 4217 if (count == 0) 4218 { 4219 elf_tdata (abfd)->next_file_pos = bed->s->sizeof_ehdr; 4220 return TRUE; 4221 } 4222 4223 /* If we already counted the number of program segments, make sure 4224 that we allocated enough space. This happens when SIZEOF_HEADERS 4225 is used in a linker script. */ 4226 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr; 4227 if (alloc != 0 && count > alloc) 4228 { 4229 ((*_bfd_error_handler) 4230 (_("%B: Not enough room for program headers (allocated %u, need %u)"), 4231 abfd, alloc, count)); 4232 print_segment_map (abfd); 4233 bfd_set_error (bfd_error_bad_value); 4234 return FALSE; 4235 } 4236 4237 if (alloc == 0) 4238 alloc = count; 4239 4240 phdrs = bfd_alloc2 (abfd, alloc, sizeof (Elf_Internal_Phdr)); 4241 if (phdrs == NULL) 4242 return FALSE; 4243 4244 off = bed->s->sizeof_ehdr; 4245 off += alloc * bed->s->sizeof_phdr; 4246 4247 filehdr_vaddr = 0; 4248 filehdr_paddr = 0; 4249 phdrs_vaddr = 0; 4250 phdrs_paddr = 0; 4251 4252 for (m = elf_tdata (abfd)->segment_map, p = phdrs; 4253 m != NULL; 4254 m = m->next, p++) 4255 { 4256 unsigned int i; 4257 asection **secpp; 4258 4259 /* If elf_segment_map is not from map_sections_to_segments, the 4260 sections may not be correctly ordered. NOTE: sorting should 4261 not be done to the PT_NOTE section of a corefile, which may 4262 contain several pseudo-sections artificially created by bfd. 4263 Sorting these pseudo-sections breaks things badly. */ 4264 if (m->count > 1 4265 && !(elf_elfheader (abfd)->e_type == ET_CORE 4266 && m->p_type == PT_NOTE)) 4267 qsort (m->sections, (size_t) m->count, sizeof (asection *), 4268 elf_sort_sections); 4269 4270 /* An ELF segment (described by Elf_Internal_Phdr) may contain a 4271 number of sections with contents contributing to both p_filesz 4272 and p_memsz, followed by a number of sections with no contents 4273 that just contribute to p_memsz. In this loop, OFF tracks next 4274 available file offset for PT_LOAD and PT_NOTE segments. VOFF is 4275 an adjustment we use for segments that have no file contents 4276 but need zero filled memory allocation. */ 4277 voff = 0; 4278 p->p_type = m->p_type; 4279 p->p_flags = m->p_flags; 4280 4281 if (p->p_type == PT_LOAD 4282 && m->count > 0) 4283 { 4284 bfd_size_type align; 4285 bfd_vma adjust; 4286 unsigned int align_power = 0; 4287 4288 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 4289 { 4290 unsigned int secalign; 4291 4292 secalign = bfd_get_section_alignment (abfd, *secpp); 4293 if (secalign > align_power) 4294 align_power = secalign; 4295 } 4296 align = (bfd_size_type) 1 << align_power; 4297 4298 if ((abfd->flags & D_PAGED) != 0 && bed->maxpagesize > align) 4299 align = bed->maxpagesize; 4300 4301 adjust = vma_page_aligned_bias (m->sections[0]->vma, off, align); 4302 off += adjust; 4303 if (adjust != 0 4304 && !m->includes_filehdr 4305 && !m->includes_phdrs 4306 && (ufile_ptr) off >= align) 4307 { 4308 /* If the first section isn't loadable, the same holds for 4309 any other sections. Since the segment won't need file 4310 space, we can make p_offset overlap some prior segment. 4311 However, .tbss is special. If a segment starts with 4312 .tbss, we need to look at the next section to decide 4313 whether the segment has any loadable sections. */ 4314 i = 0; 4315 while ((m->sections[i]->flags & SEC_LOAD) == 0) 4316 { 4317 if ((m->sections[i]->flags & SEC_THREAD_LOCAL) == 0 4318 || ++i >= m->count) 4319 { 4320 off -= adjust; 4321 voff = adjust - align; 4322 break; 4323 } 4324 } 4325 } 4326 } 4327 /* Make sure the .dynamic section is the first section in the 4328 PT_DYNAMIC segment. */ 4329 else if (p->p_type == PT_DYNAMIC 4330 && m->count > 1 4331 && strcmp (m->sections[0]->name, ".dynamic") != 0) 4332 { 4333 _bfd_error_handler 4334 (_("%B: The first section in the PT_DYNAMIC segment is not the .dynamic section"), 4335 abfd); 4336 bfd_set_error (bfd_error_bad_value); 4337 return FALSE; 4338 } 4339 4340 if (m->count == 0) 4341 p->p_vaddr = 0; 4342 else 4343 p->p_vaddr = m->sections[0]->vma; 4344 4345 if (m->p_paddr_valid) 4346 p->p_paddr = m->p_paddr; 4347 else if (m->count == 0) 4348 p->p_paddr = 0; 4349 else 4350 p->p_paddr = m->sections[0]->lma; 4351 4352 if (p->p_type == PT_LOAD 4353 && (abfd->flags & D_PAGED) != 0) 4354 p->p_align = bed->maxpagesize; 4355 else if (m->count == 0) 4356 p->p_align = 1 << bed->s->log_file_align; 4357 else 4358 p->p_align = 0; 4359 4360 p->p_offset = 0; 4361 p->p_filesz = 0; 4362 p->p_memsz = 0; 4363 4364 if (m->includes_filehdr) 4365 { 4366 if (! m->p_flags_valid) 4367 p->p_flags |= PF_R; 4368 p->p_offset = 0; 4369 p->p_filesz = bed->s->sizeof_ehdr; 4370 p->p_memsz = bed->s->sizeof_ehdr; 4371 if (m->count > 0) 4372 { 4373 BFD_ASSERT (p->p_type == PT_LOAD); 4374 4375 if (p->p_vaddr < (bfd_vma) off) 4376 { 4377 (*_bfd_error_handler) 4378 (_("%B: Not enough room for program headers, try linking with -N"), 4379 abfd); 4380 bfd_set_error (bfd_error_bad_value); 4381 return FALSE; 4382 } 4383 4384 p->p_vaddr -= off; 4385 if (! m->p_paddr_valid) 4386 p->p_paddr -= off; 4387 } 4388 if (p->p_type == PT_LOAD) 4389 { 4390 filehdr_vaddr = p->p_vaddr; 4391 filehdr_paddr = p->p_paddr; 4392 } 4393 } 4394 4395 if (m->includes_phdrs) 4396 { 4397 if (! m->p_flags_valid) 4398 p->p_flags |= PF_R; 4399 4400 if (m->includes_filehdr) 4401 { 4402 if (p->p_type == PT_LOAD) 4403 { 4404 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr; 4405 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr; 4406 } 4407 } 4408 else 4409 { 4410 p->p_offset = bed->s->sizeof_ehdr; 4411 4412 if (m->count > 0) 4413 { 4414 BFD_ASSERT (p->p_type == PT_LOAD); 4415 p->p_vaddr -= off - p->p_offset; 4416 if (! m->p_paddr_valid) 4417 p->p_paddr -= off - p->p_offset; 4418 } 4419 4420 if (p->p_type == PT_LOAD) 4421 { 4422 phdrs_vaddr = p->p_vaddr; 4423 phdrs_paddr = p->p_paddr; 4424 } 4425 else 4426 phdrs_vaddr = bed->maxpagesize + bed->s->sizeof_ehdr; 4427 } 4428 4429 p->p_filesz += alloc * bed->s->sizeof_phdr; 4430 p->p_memsz += alloc * bed->s->sizeof_phdr; 4431 } 4432 4433 if (p->p_type == PT_LOAD 4434 || (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)) 4435 { 4436 if (! m->includes_filehdr && ! m->includes_phdrs) 4437 p->p_offset = off + voff; 4438 else 4439 { 4440 file_ptr adjust; 4441 4442 adjust = off - (p->p_offset + p->p_filesz); 4443 p->p_filesz += adjust; 4444 p->p_memsz += adjust; 4445 } 4446 } 4447 4448 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 4449 { 4450 asection *sec; 4451 flagword flags; 4452 bfd_size_type align; 4453 4454 sec = *secpp; 4455 flags = sec->flags; 4456 align = 1 << bfd_get_section_alignment (abfd, sec); 4457 4458 if (p->p_type == PT_LOAD 4459 || p->p_type == PT_TLS) 4460 { 4461 bfd_signed_vma adjust; 4462 4463 if ((flags & SEC_LOAD) != 0) 4464 { 4465 adjust = sec->lma - (p->p_paddr + p->p_filesz); 4466 if (adjust < 0) 4467 { 4468 (*_bfd_error_handler) 4469 (_("%B: section %A lma 0x%lx overlaps previous sections"), 4470 abfd, sec, (unsigned long) sec->lma); 4471 adjust = 0; 4472 } 4473 off += adjust; 4474 p->p_filesz += adjust; 4475 p->p_memsz += adjust; 4476 } 4477 /* .tbss is special. It doesn't contribute to p_memsz of 4478 normal segments. */ 4479 else if ((flags & SEC_THREAD_LOCAL) == 0 4480 || p->p_type == PT_TLS) 4481 { 4482 /* The section VMA must equal the file position 4483 modulo the page size. */ 4484 bfd_size_type page = align; 4485 if ((abfd->flags & D_PAGED) != 0 && bed->maxpagesize > page) 4486 page = bed->maxpagesize; 4487 adjust = vma_page_aligned_bias (sec->vma, 4488 p->p_vaddr + p->p_memsz, 4489 page); 4490 p->p_memsz += adjust; 4491 } 4492 } 4493 4494 if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core) 4495 { 4496 /* The section at i == 0 is the one that actually contains 4497 everything. */ 4498 if (i == 0) 4499 { 4500 sec->filepos = off; 4501 off += sec->size; 4502 p->p_filesz = sec->size; 4503 p->p_memsz = 0; 4504 p->p_align = 1; 4505 } 4506 else 4507 { 4508 /* The rest are fake sections that shouldn't be written. */ 4509 sec->filepos = 0; 4510 sec->size = 0; 4511 sec->flags = 0; 4512 continue; 4513 } 4514 } 4515 else 4516 { 4517 if (p->p_type == PT_LOAD) 4518 { 4519 sec->filepos = off; 4520 /* FIXME: The SEC_HAS_CONTENTS test here dates back to 4521 1997, and the exact reason for it isn't clear. One 4522 plausible explanation is that it is to work around 4523 a problem we have with linker scripts using data 4524 statements in NOLOAD sections. I don't think it 4525 makes a great deal of sense to have such a section 4526 assigned to a PT_LOAD segment, but apparently 4527 people do this. The data statement results in a 4528 bfd_data_link_order being built, and these need 4529 section contents to write into. Eventually, we get 4530 to _bfd_elf_write_object_contents which writes any 4531 section with contents to the output. Make room 4532 here for the write, so that following segments are 4533 not trashed. */ 4534 if ((flags & SEC_LOAD) != 0 4535 || (flags & SEC_HAS_CONTENTS) != 0) 4536 off += sec->size; 4537 } 4538 4539 if ((flags & SEC_LOAD) != 0) 4540 { 4541 p->p_filesz += sec->size; 4542 p->p_memsz += sec->size; 4543 } 4544 /* PR ld/594: Sections in note segments which are not loaded 4545 contribute to the file size but not the in-memory size. */ 4546 else if (p->p_type == PT_NOTE 4547 && (flags & SEC_HAS_CONTENTS) != 0) 4548 p->p_filesz += sec->size; 4549 4550 /* .tbss is special. It doesn't contribute to p_memsz of 4551 normal segments. */ 4552 else if ((flags & SEC_THREAD_LOCAL) == 0 4553 || p->p_type == PT_TLS) 4554 p->p_memsz += sec->size; 4555 4556 if (p->p_type == PT_TLS 4557 && sec->size == 0 4558 && (sec->flags & SEC_HAS_CONTENTS) == 0) 4559 { 4560 struct bfd_link_order *o = sec->map_tail.link_order; 4561 if (o != NULL) 4562 p->p_memsz += o->offset + o->size; 4563 } 4564 4565 if (align > p->p_align 4566 && (p->p_type != PT_LOAD || (abfd->flags & D_PAGED) == 0)) 4567 p->p_align = align; 4568 } 4569 4570 if (! m->p_flags_valid) 4571 { 4572 p->p_flags |= PF_R; 4573 if ((flags & SEC_CODE) != 0) 4574 p->p_flags |= PF_X; 4575 if ((flags & SEC_READONLY) == 0) 4576 p->p_flags |= PF_W; 4577 } 4578 } 4579 } 4580 4581 /* Now that we have set the section file positions, we can set up 4582 the file positions for the non PT_LOAD segments. */ 4583 for (m = elf_tdata (abfd)->segment_map, p = phdrs; 4584 m != NULL; 4585 m = m->next, p++) 4586 { 4587 if (p->p_type != PT_LOAD && m->count > 0) 4588 { 4589 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs); 4590 /* If the section has not yet been assigned a file position, 4591 do so now. The ARM BPABI requires that .dynamic section 4592 not be marked SEC_ALLOC because it is not part of any 4593 PT_LOAD segment, so it will not be processed above. */ 4594 if (p->p_type == PT_DYNAMIC && m->sections[0]->filepos == 0) 4595 { 4596 unsigned int i; 4597 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); 4598 4599 i = 1; 4600 while (i_shdrpp[i]->bfd_section != m->sections[0]) 4601 ++i; 4602 off = (_bfd_elf_assign_file_position_for_section 4603 (i_shdrpp[i], off, TRUE)); 4604 p->p_filesz = m->sections[0]->size; 4605 } 4606 p->p_offset = m->sections[0]->filepos; 4607 } 4608 if (m->count == 0) 4609 { 4610 if (m->includes_filehdr) 4611 { 4612 p->p_vaddr = filehdr_vaddr; 4613 if (! m->p_paddr_valid) 4614 p->p_paddr = filehdr_paddr; 4615 } 4616 else if (m->includes_phdrs) 4617 { 4618 p->p_vaddr = phdrs_vaddr; 4619 if (! m->p_paddr_valid) 4620 p->p_paddr = phdrs_paddr; 4621 } 4622 else if (p->p_type == PT_GNU_RELRO) 4623 { 4624 Elf_Internal_Phdr *lp; 4625 4626 for (lp = phdrs; lp < phdrs + count; ++lp) 4627 { 4628 if (lp->p_type == PT_LOAD 4629 && lp->p_vaddr <= link_info->relro_end 4630 && lp->p_vaddr >= link_info->relro_start 4631 && lp->p_vaddr + lp->p_filesz 4632 >= link_info->relro_end) 4633 break; 4634 } 4635 4636 if (lp < phdrs + count 4637 && link_info->relro_end > lp->p_vaddr) 4638 { 4639 p->p_vaddr = lp->p_vaddr; 4640 p->p_paddr = lp->p_paddr; 4641 p->p_offset = lp->p_offset; 4642 p->p_filesz = link_info->relro_end - lp->p_vaddr; 4643 p->p_memsz = p->p_filesz; 4644 p->p_align = 1; 4645 p->p_flags = (lp->p_flags & ~PF_W); 4646 } 4647 else 4648 { 4649 memset (p, 0, sizeof *p); 4650 p->p_type = PT_NULL; 4651 } 4652 } 4653 } 4654 } 4655 4656 /* Clear out any program headers we allocated but did not use. */ 4657 for (; count < alloc; count++, p++) 4658 { 4659 memset (p, 0, sizeof *p); 4660 p->p_type = PT_NULL; 4661 } 4662 4663 elf_tdata (abfd)->phdr = phdrs; 4664 4665 elf_tdata (abfd)->next_file_pos = off; 4666 4667 /* Write out the program headers. */ 4668 if (bfd_seek (abfd, (bfd_signed_vma) bed->s->sizeof_ehdr, SEEK_SET) != 0 4669 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0) 4670 return FALSE; 4671 4672 return TRUE; 4673 } 4674 4675 /* Get the size of the program header. 4676 4677 If this is called by the linker before any of the section VMA's are set, it 4678 can't calculate the correct value for a strange memory layout. This only 4679 happens when SIZEOF_HEADERS is used in a linker script. In this case, 4680 SORTED_HDRS is NULL and we assume the normal scenario of one text and one 4681 data segment (exclusive of .interp and .dynamic). 4682 4683 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there 4684 will be two segments. */ 4685 4686 static bfd_size_type 4687 get_program_header_size (bfd *abfd) 4688 { 4689 size_t segs; 4690 asection *s; 4691 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4692 4693 /* We can't return a different result each time we're called. */ 4694 if (elf_tdata (abfd)->program_header_size != 0) 4695 return elf_tdata (abfd)->program_header_size; 4696 4697 if (elf_tdata (abfd)->segment_map != NULL) 4698 { 4699 struct elf_segment_map *m; 4700 4701 segs = 0; 4702 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) 4703 ++segs; 4704 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; 4705 return elf_tdata (abfd)->program_header_size; 4706 } 4707 4708 /* We used to assume that two PT_LOAD segments would be enough, 4709 code and data, with the change to pad the PLT and GOT, this is no 4710 longer true. Now there can be several PT_LOAD sections. 7 seems 4711 to be enough with BSS_PLT and .rodata-X, where we have text, data, 4712 GOT, dynamic, PLT, bss */ 4713 segs = 7; 4714 4715 s = bfd_get_section_by_name (abfd, ".interp"); 4716 s = bfd_get_section_by_name (abfd, ".interp"); 4717 if ((s != NULL && (s->flags & SEC_LOAD) != 0) || 4718 (bfd_get_section_by_name (abfd, ".dynamic") && 4719 elf_tdata (abfd)->executable)) 4720 { 4721 /* We need a PT_PHDR segment. */ 4722 ++segs; 4723 } 4724 4725 if (s != NULL && (s->flags & SEC_LOAD) != 0) 4726 { 4727 /* If we have a loadable interpreter section, we need a 4728 PT_INTERP segment. */ 4729 ++segs; 4730 } 4731 4732 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) 4733 { 4734 /* We need a PT_DYNAMIC segment. */ 4735 ++segs; 4736 } 4737 4738 if (bfd_get_section_by_name (abfd, ".openbsd.randomdata") != NULL) 4739 { 4740 /* We need a PT_OPENBSD_RANDOMIZE segment. */ 4741 ++segs; 4742 } 4743 4744 if (elf_tdata (abfd)->eh_frame_hdr) 4745 { 4746 /* We need a PT_GNU_EH_FRAME segment. */ 4747 ++segs; 4748 } 4749 4750 if (elf_tdata (abfd)->stack_flags) 4751 { 4752 /* We need a PT_GNU_STACK segment. */ 4753 ++segs; 4754 } 4755 4756 if (elf_tdata (abfd)->relro) 4757 { 4758 /* We need a PT_GNU_RELRO segment. */ 4759 ++segs; 4760 } 4761 4762 if (elf_tdata (abfd)->wxneeded) 4763 { 4764 /* We need a PT_OPENBSD_WXNEEDED segment. */ 4765 ++segs; 4766 } 4767 4768 for (s = abfd->sections; s != NULL; s = s->next) 4769 { 4770 if ((s->flags & SEC_LOAD) != 0 4771 && strncmp (s->name, ".note", 5) == 0) 4772 { 4773 /* We need a PT_NOTE segment. */ 4774 ++segs; 4775 } 4776 } 4777 4778 for (s = abfd->sections; s != NULL; s = s->next) 4779 { 4780 if (s->flags & SEC_THREAD_LOCAL) 4781 { 4782 /* We need a PT_TLS segment. */ 4783 ++segs; 4784 break; 4785 } 4786 } 4787 4788 /* Let the backend count up any program headers it might need. */ 4789 if (bed->elf_backend_additional_program_headers) 4790 { 4791 int a; 4792 4793 a = (*bed->elf_backend_additional_program_headers) (abfd); 4794 if (a == -1) 4795 abort (); 4796 segs += a; 4797 } 4798 4799 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; 4800 return elf_tdata (abfd)->program_header_size; 4801 } 4802 4803 /* Work out the file positions of all the sections. This is called by 4804 _bfd_elf_compute_section_file_positions. All the section sizes and 4805 VMAs must be known before this is called. 4806 4807 Reloc sections come in two flavours: Those processed specially as 4808 "side-channel" data attached to a section to which they apply, and 4809 those that bfd doesn't process as relocations. The latter sort are 4810 stored in a normal bfd section by bfd_section_from_shdr. We don't 4811 consider the former sort here, unless they form part of the loadable 4812 image. Reloc sections not assigned here will be handled later by 4813 assign_file_positions_for_relocs. 4814 4815 We also don't set the positions of the .symtab and .strtab here. */ 4816 4817 static bfd_boolean 4818 assign_file_positions_except_relocs (bfd *abfd, 4819 struct bfd_link_info *link_info) 4820 { 4821 struct elf_obj_tdata * const tdata = elf_tdata (abfd); 4822 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd); 4823 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); 4824 unsigned int num_sec = elf_numsections (abfd); 4825 file_ptr off; 4826 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4827 4828 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0 4829 && bfd_get_format (abfd) != bfd_core) 4830 { 4831 Elf_Internal_Shdr **hdrpp; 4832 unsigned int i; 4833 4834 /* Start after the ELF header. */ 4835 off = i_ehdrp->e_ehsize; 4836 4837 /* We are not creating an executable, which means that we are 4838 not creating a program header, and that the actual order of 4839 the sections in the file is unimportant. */ 4840 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) 4841 { 4842 Elf_Internal_Shdr *hdr; 4843 4844 hdr = *hdrpp; 4845 if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 4846 && hdr->bfd_section == NULL) 4847 || i == tdata->symtab_section 4848 || i == tdata->symtab_shndx_section 4849 || i == tdata->strtab_section) 4850 { 4851 hdr->sh_offset = -1; 4852 } 4853 else 4854 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); 4855 4856 if (i == SHN_LORESERVE - 1) 4857 { 4858 i += SHN_HIRESERVE + 1 - SHN_LORESERVE; 4859 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE; 4860 } 4861 } 4862 } 4863 else 4864 { 4865 unsigned int i; 4866 Elf_Internal_Shdr **hdrpp; 4867 4868 /* Assign file positions for the loaded sections based on the 4869 assignment of sections to segments. */ 4870 if (! assign_file_positions_for_segments (abfd, link_info)) 4871 return FALSE; 4872 4873 /* Assign file positions for the other sections. */ 4874 4875 off = elf_tdata (abfd)->next_file_pos; 4876 for (i = 1, hdrpp = i_shdrpp + 1; i < num_sec; i++, hdrpp++) 4877 { 4878 Elf_Internal_Shdr *hdr; 4879 4880 hdr = *hdrpp; 4881 if (hdr->bfd_section != NULL 4882 && hdr->bfd_section->filepos != 0) 4883 hdr->sh_offset = hdr->bfd_section->filepos; 4884 else if ((hdr->sh_flags & SHF_ALLOC) != 0) 4885 { 4886 if (hdr->bfd_section->size != 0) 4887 { 4888 ((*_bfd_error_handler) 4889 (_("%B: warning: allocated section `%s' not in segment"), 4890 abfd, 4891 (hdr->bfd_section == NULL 4892 ? "*unknown*" 4893 : hdr->bfd_section->name))); 4894 } 4895 if ((abfd->flags & D_PAGED) != 0) 4896 off += vma_page_aligned_bias (hdr->sh_addr, off, 4897 bed->maxpagesize); 4898 else 4899 off += vma_page_aligned_bias (hdr->sh_addr, off, 4900 hdr->sh_addralign); 4901 off = _bfd_elf_assign_file_position_for_section (hdr, off, 4902 FALSE); 4903 } 4904 else if (((hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 4905 && hdr->bfd_section == NULL) 4906 || hdr == i_shdrpp[tdata->symtab_section] 4907 || hdr == i_shdrpp[tdata->symtab_shndx_section] 4908 || hdr == i_shdrpp[tdata->strtab_section]) 4909 hdr->sh_offset = -1; 4910 else 4911 off = _bfd_elf_assign_file_position_for_section (hdr, off, TRUE); 4912 4913 if (i == SHN_LORESERVE - 1) 4914 { 4915 i += SHN_HIRESERVE + 1 - SHN_LORESERVE; 4916 hdrpp += SHN_HIRESERVE + 1 - SHN_LORESERVE; 4917 } 4918 } 4919 } 4920 4921 /* Place the section headers. */ 4922 off = align_file_position (off, 1 << bed->s->log_file_align); 4923 i_ehdrp->e_shoff = off; 4924 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; 4925 4926 elf_tdata (abfd)->next_file_pos = off; 4927 4928 return TRUE; 4929 } 4930 4931 static bfd_boolean 4932 prep_headers (bfd *abfd) 4933 { 4934 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ 4935 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ 4936 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ 4937 struct elf_strtab_hash *shstrtab; 4938 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4939 4940 i_ehdrp = elf_elfheader (abfd); 4941 i_shdrp = elf_elfsections (abfd); 4942 4943 shstrtab = _bfd_elf_strtab_init (); 4944 if (shstrtab == NULL) 4945 return FALSE; 4946 4947 elf_shstrtab (abfd) = shstrtab; 4948 4949 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; 4950 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; 4951 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; 4952 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; 4953 4954 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; 4955 i_ehdrp->e_ident[EI_DATA] = 4956 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; 4957 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; 4958 4959 if ((abfd->flags & DYNAMIC) != 0) 4960 i_ehdrp->e_type = ET_DYN; 4961 else if ((abfd->flags & EXEC_P) != 0) 4962 i_ehdrp->e_type = ET_EXEC; 4963 else if (bfd_get_format (abfd) == bfd_core) 4964 i_ehdrp->e_type = ET_CORE; 4965 else 4966 i_ehdrp->e_type = ET_REL; 4967 4968 switch (bfd_get_arch (abfd)) 4969 { 4970 case bfd_arch_unknown: 4971 i_ehdrp->e_machine = EM_NONE; 4972 break; 4973 4974 /* There used to be a long list of cases here, each one setting 4975 e_machine to the same EM_* macro #defined as ELF_MACHINE_CODE 4976 in the corresponding bfd definition. To avoid duplication, 4977 the switch was removed. Machines that need special handling 4978 can generally do it in elf_backend_final_write_processing(), 4979 unless they need the information earlier than the final write. 4980 Such need can generally be supplied by replacing the tests for 4981 e_machine with the conditions used to determine it. */ 4982 default: 4983 i_ehdrp->e_machine = bed->elf_machine_code; 4984 } 4985 4986 i_ehdrp->e_version = bed->s->ev_current; 4987 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; 4988 4989 /* No program header, for now. */ 4990 i_ehdrp->e_phoff = 0; 4991 i_ehdrp->e_phentsize = 0; 4992 i_ehdrp->e_phnum = 0; 4993 4994 /* Each bfd section is section header entry. */ 4995 i_ehdrp->e_entry = bfd_get_start_address (abfd); 4996 i_ehdrp->e_shentsize = bed->s->sizeof_shdr; 4997 4998 /* If we're building an executable, we'll need a program header table. */ 4999 if (abfd->flags & EXEC_P) 5000 /* It all happens later. */ 5001 ; 5002 else 5003 { 5004 i_ehdrp->e_phentsize = 0; 5005 i_phdrp = 0; 5006 i_ehdrp->e_phoff = 0; 5007 } 5008 5009 elf_tdata (abfd)->symtab_hdr.sh_name = 5010 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".symtab", FALSE); 5011 elf_tdata (abfd)->strtab_hdr.sh_name = 5012 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".strtab", FALSE); 5013 elf_tdata (abfd)->shstrtab_hdr.sh_name = 5014 (unsigned int) _bfd_elf_strtab_add (shstrtab, ".shstrtab", FALSE); 5015 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 5016 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 5017 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) 5018 return FALSE; 5019 5020 return TRUE; 5021 } 5022 5023 /* Assign file positions for all the reloc sections which are not part 5024 of the loadable file image. */ 5025 5026 void 5027 _bfd_elf_assign_file_positions_for_relocs (bfd *abfd) 5028 { 5029 file_ptr off; 5030 unsigned int i, num_sec; 5031 Elf_Internal_Shdr **shdrpp; 5032 5033 off = elf_tdata (abfd)->next_file_pos; 5034 5035 num_sec = elf_numsections (abfd); 5036 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; i < num_sec; i++, shdrpp++) 5037 { 5038 Elf_Internal_Shdr *shdrp; 5039 5040 shdrp = *shdrpp; 5041 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) 5042 && shdrp->sh_offset == -1) 5043 off = _bfd_elf_assign_file_position_for_section (shdrp, off, TRUE); 5044 } 5045 5046 elf_tdata (abfd)->next_file_pos = off; 5047 } 5048 5049 bfd_boolean 5050 _bfd_elf_write_object_contents (bfd *abfd) 5051 { 5052 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 5053 Elf_Internal_Ehdr *i_ehdrp; 5054 Elf_Internal_Shdr **i_shdrp; 5055 bfd_boolean failed; 5056 unsigned int count, num_sec; 5057 5058 if (! abfd->output_has_begun 5059 && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) 5060 return FALSE; 5061 5062 i_shdrp = elf_elfsections (abfd); 5063 i_ehdrp = elf_elfheader (abfd); 5064 5065 failed = FALSE; 5066 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); 5067 if (failed) 5068 return FALSE; 5069 5070 _bfd_elf_assign_file_positions_for_relocs (abfd); 5071 5072 /* After writing the headers, we need to write the sections too... */ 5073 num_sec = elf_numsections (abfd); 5074 for (count = 1; count < num_sec; count++) 5075 { 5076 if (bed->elf_backend_section_processing) 5077 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); 5078 if (i_shdrp[count]->contents) 5079 { 5080 bfd_size_type amt = i_shdrp[count]->sh_size; 5081 5082 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 5083 || bfd_bwrite (i_shdrp[count]->contents, amt, abfd) != amt) 5084 return FALSE; 5085 } 5086 if (count == SHN_LORESERVE - 1) 5087 count += SHN_HIRESERVE + 1 - SHN_LORESERVE; 5088 } 5089 5090 /* Write out the section header names. */ 5091 if (elf_shstrtab (abfd) != NULL 5092 && (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 5093 || ! _bfd_elf_strtab_emit (abfd, elf_shstrtab (abfd)))) 5094 return FALSE; 5095 5096 if (bed->elf_backend_final_write_processing) 5097 (*bed->elf_backend_final_write_processing) (abfd, 5098 elf_tdata (abfd)->linker); 5099 5100 return bed->s->write_shdrs_and_ehdr (abfd); 5101 } 5102 5103 bfd_boolean 5104 _bfd_elf_write_corefile_contents (bfd *abfd) 5105 { 5106 /* Hopefully this can be done just like an object file. */ 5107 return _bfd_elf_write_object_contents (abfd); 5108 } 5109 5110 /* Given a section, search the header to find them. */ 5111 5112 int 5113 _bfd_elf_section_from_bfd_section (bfd *abfd, struct bfd_section *asect) 5114 { 5115 const struct elf_backend_data *bed; 5116 int index; 5117 5118 if (elf_section_data (asect) != NULL 5119 && elf_section_data (asect)->this_idx != 0) 5120 return elf_section_data (asect)->this_idx; 5121 5122 if (bfd_is_abs_section (asect)) 5123 index = SHN_ABS; 5124 else if (bfd_is_com_section (asect)) 5125 index = SHN_COMMON; 5126 else if (bfd_is_und_section (asect)) 5127 index = SHN_UNDEF; 5128 else 5129 index = -1; 5130 5131 bed = get_elf_backend_data (abfd); 5132 if (bed->elf_backend_section_from_bfd_section) 5133 { 5134 int retval = index; 5135 5136 if ((*bed->elf_backend_section_from_bfd_section) (abfd, asect, &retval)) 5137 return retval; 5138 } 5139 5140 if (index == -1) 5141 bfd_set_error (bfd_error_nonrepresentable_section); 5142 5143 return index; 5144 } 5145 5146 /* Given a BFD symbol, return the index in the ELF symbol table, or -1 5147 on error. */ 5148 5149 int 5150 _bfd_elf_symbol_from_bfd_symbol (bfd *abfd, asymbol **asym_ptr_ptr) 5151 { 5152 asymbol *asym_ptr = *asym_ptr_ptr; 5153 int idx; 5154 flagword flags = asym_ptr->flags; 5155 5156 /* When gas creates relocations against local labels, it creates its 5157 own symbol for the section, but does put the symbol into the 5158 symbol chain, so udata is 0. When the linker is generating 5159 relocatable output, this section symbol may be for one of the 5160 input sections rather than the output section. */ 5161 if (asym_ptr->udata.i == 0 5162 && (flags & BSF_SECTION_SYM) 5163 && asym_ptr->section) 5164 { 5165 int indx; 5166 5167 if (asym_ptr->section->output_section != NULL) 5168 indx = asym_ptr->section->output_section->index; 5169 else 5170 indx = asym_ptr->section->index; 5171 if (indx < elf_num_section_syms (abfd) 5172 && elf_section_syms (abfd)[indx] != NULL) 5173 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; 5174 } 5175 5176 idx = asym_ptr->udata.i; 5177 5178 if (idx == 0) 5179 { 5180 /* This case can occur when using --strip-symbol on a symbol 5181 which is used in a relocation entry. */ 5182 (*_bfd_error_handler) 5183 (_("%B: symbol `%s' required but not present"), 5184 abfd, bfd_asymbol_name (asym_ptr)); 5185 bfd_set_error (bfd_error_no_symbols); 5186 return -1; 5187 } 5188 5189 #if DEBUG & 4 5190 { 5191 fprintf (stderr, 5192 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n", 5193 (long) asym_ptr, asym_ptr->name, idx, flags, 5194 elf_symbol_flags (flags)); 5195 fflush (stderr); 5196 } 5197 #endif 5198 5199 return idx; 5200 } 5201 5202 /* Rewrite program header information. */ 5203 5204 static bfd_boolean 5205 rewrite_elf_program_header (bfd *ibfd, bfd *obfd) 5206 { 5207 Elf_Internal_Ehdr *iehdr; 5208 struct elf_segment_map *map; 5209 struct elf_segment_map *map_first; 5210 struct elf_segment_map **pointer_to_map; 5211 Elf_Internal_Phdr *segment; 5212 asection *section; 5213 unsigned int i; 5214 unsigned int num_segments; 5215 bfd_boolean phdr_included = FALSE; 5216 bfd_vma maxpagesize; 5217 struct elf_segment_map *phdr_adjust_seg = NULL; 5218 unsigned int phdr_adjust_num = 0; 5219 const struct elf_backend_data *bed; 5220 5221 bed = get_elf_backend_data (ibfd); 5222 iehdr = elf_elfheader (ibfd); 5223 5224 map_first = NULL; 5225 pointer_to_map = &map_first; 5226 5227 num_segments = elf_elfheader (ibfd)->e_phnum; 5228 maxpagesize = get_elf_backend_data (obfd)->maxpagesize; 5229 5230 /* Returns the end address of the segment + 1. */ 5231 #define SEGMENT_END(segment, start) \ 5232 (start + (segment->p_memsz > segment->p_filesz \ 5233 ? segment->p_memsz : segment->p_filesz)) 5234 5235 #define SECTION_SIZE(section, segment) \ 5236 (((section->flags & (SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) \ 5237 != SEC_THREAD_LOCAL || segment->p_type == PT_TLS) \ 5238 ? section->size : 0) 5239 5240 /* Returns TRUE if the given section is contained within 5241 the given segment. VMA addresses are compared. */ 5242 #define IS_CONTAINED_BY_VMA(section, segment) \ 5243 (section->vma >= segment->p_vaddr \ 5244 && (section->vma + SECTION_SIZE (section, segment) \ 5245 <= (SEGMENT_END (segment, segment->p_vaddr)))) 5246 5247 /* Returns TRUE if the given section is contained within 5248 the given segment. LMA addresses are compared. */ 5249 #define IS_CONTAINED_BY_LMA(section, segment, base) \ 5250 (section->lma >= base \ 5251 && (section->lma + SECTION_SIZE (section, segment) \ 5252 <= SEGMENT_END (segment, base))) 5253 5254 /* Special case: corefile "NOTE" section containing regs, prpsinfo etc. */ 5255 #define IS_COREFILE_NOTE(p, s) \ 5256 (p->p_type == PT_NOTE \ 5257 && bfd_get_format (ibfd) == bfd_core \ 5258 && s->vma == 0 && s->lma == 0 \ 5259 && (bfd_vma) s->filepos >= p->p_offset \ 5260 && ((bfd_vma) s->filepos + s->size \ 5261 <= p->p_offset + p->p_filesz)) 5262 5263 /* The complicated case when p_vaddr is 0 is to handle the Solaris 5264 linker, which generates a PT_INTERP section with p_vaddr and 5265 p_memsz set to 0. */ 5266 #define IS_SOLARIS_PT_INTERP(p, s) \ 5267 (p->p_vaddr == 0 \ 5268 && p->p_paddr == 0 \ 5269 && p->p_memsz == 0 \ 5270 && p->p_filesz > 0 \ 5271 && (s->flags & SEC_HAS_CONTENTS) != 0 \ 5272 && s->size > 0 \ 5273 && (bfd_vma) s->filepos >= p->p_offset \ 5274 && ((bfd_vma) s->filepos + s->size \ 5275 <= p->p_offset + p->p_filesz)) 5276 5277 /* Decide if the given section should be included in the given segment. 5278 A section will be included if: 5279 1. It is within the address space of the segment -- we use the LMA 5280 if that is set for the segment and the VMA otherwise, 5281 2. It is an allocated segment, 5282 3. There is an output section associated with it, 5283 4. The section has not already been allocated to a previous segment. 5284 5. PT_GNU_STACK segments do not include any sections. 5285 6. PT_TLS segment includes only SHF_TLS sections. 5286 7. SHF_TLS sections are only in PT_TLS or PT_LOAD segments. 5287 8. PT_DYNAMIC should not contain empty sections at the beginning 5288 (with the possible exception of .dynamic). */ 5289 #define INCLUDE_SECTION_IN_SEGMENT(section, segment, bed) \ 5290 ((((segment->p_paddr \ 5291 ? IS_CONTAINED_BY_LMA (section, segment, segment->p_paddr) \ 5292 : IS_CONTAINED_BY_VMA (section, segment)) \ 5293 && (section->flags & SEC_ALLOC) != 0) \ 5294 || IS_COREFILE_NOTE (segment, section)) \ 5295 && section->output_section != NULL \ 5296 && segment->p_type != PT_GNU_STACK \ 5297 && (segment->p_type != PT_TLS \ 5298 || (section->flags & SEC_THREAD_LOCAL)) \ 5299 && (segment->p_type == PT_LOAD \ 5300 || segment->p_type == PT_TLS \ 5301 || (section->flags & SEC_THREAD_LOCAL) == 0) \ 5302 && (segment->p_type != PT_DYNAMIC \ 5303 || SECTION_SIZE (section, segment) > 0 \ 5304 || (segment->p_paddr \ 5305 ? segment->p_paddr != section->lma \ 5306 : segment->p_vaddr != section->vma) \ 5307 || (strcmp (bfd_get_section_name (ibfd, section), ".dynamic") \ 5308 == 0)) \ 5309 && ! section->segment_mark) 5310 5311 /* Returns TRUE iff seg1 starts after the end of seg2. */ 5312 #define SEGMENT_AFTER_SEGMENT(seg1, seg2, field) \ 5313 (seg1->field >= SEGMENT_END (seg2, seg2->field)) 5314 5315 /* Returns TRUE iff seg1 and seg2 overlap. Segments overlap iff both 5316 their VMA address ranges and their LMA address ranges overlap. 5317 It is possible to have overlapping VMA ranges without overlapping LMA 5318 ranges. RedBoot images for example can have both .data and .bss mapped 5319 to the same VMA range, but with the .data section mapped to a different 5320 LMA. */ 5321 #define SEGMENT_OVERLAPS(seg1, seg2) \ 5322 ( !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_vaddr) \ 5323 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_vaddr)) \ 5324 && !(SEGMENT_AFTER_SEGMENT (seg1, seg2, p_paddr) \ 5325 || SEGMENT_AFTER_SEGMENT (seg2, seg1, p_paddr))) 5326 5327 /* Initialise the segment mark field. */ 5328 for (section = ibfd->sections; section != NULL; section = section->next) 5329 section->segment_mark = FALSE; 5330 5331 /* Scan through the segments specified in the program header 5332 of the input BFD. For this first scan we look for overlaps 5333 in the loadable segments. These can be created by weird 5334 parameters to objcopy. Also, fix some solaris weirdness. */ 5335 for (i = 0, segment = elf_tdata (ibfd)->phdr; 5336 i < num_segments; 5337 i++, segment++) 5338 { 5339 unsigned int j; 5340 Elf_Internal_Phdr *segment2; 5341 5342 if (segment->p_type == PT_INTERP) 5343 for (section = ibfd->sections; section; section = section->next) 5344 if (IS_SOLARIS_PT_INTERP (segment, section)) 5345 { 5346 /* Mininal change so that the normal section to segment 5347 assignment code will work. */ 5348 segment->p_vaddr = section->vma; 5349 break; 5350 } 5351 5352 if (segment->p_type != PT_LOAD) 5353 continue; 5354 5355 /* Determine if this segment overlaps any previous segments. */ 5356 for (j = 0, segment2 = elf_tdata (ibfd)->phdr; j < i; j++, segment2 ++) 5357 { 5358 bfd_signed_vma extra_length; 5359 5360 if (segment2->p_type != PT_LOAD 5361 || ! SEGMENT_OVERLAPS (segment, segment2)) 5362 continue; 5363 5364 /* Merge the two segments together. */ 5365 if (segment2->p_vaddr < segment->p_vaddr) 5366 { 5367 /* Extend SEGMENT2 to include SEGMENT and then delete 5368 SEGMENT. */ 5369 extra_length = 5370 SEGMENT_END (segment, segment->p_vaddr) 5371 - SEGMENT_END (segment2, segment2->p_vaddr); 5372 5373 if (extra_length > 0) 5374 { 5375 segment2->p_memsz += extra_length; 5376 segment2->p_filesz += extra_length; 5377 } 5378 5379 segment->p_type = PT_NULL; 5380 5381 /* Since we have deleted P we must restart the outer loop. */ 5382 i = 0; 5383 segment = elf_tdata (ibfd)->phdr; 5384 break; 5385 } 5386 else 5387 { 5388 /* Extend SEGMENT to include SEGMENT2 and then delete 5389 SEGMENT2. */ 5390 extra_length = 5391 SEGMENT_END (segment2, segment2->p_vaddr) 5392 - SEGMENT_END (segment, segment->p_vaddr); 5393 5394 if (extra_length > 0) 5395 { 5396 segment->p_memsz += extra_length; 5397 segment->p_filesz += extra_length; 5398 } 5399 5400 segment2->p_type = PT_NULL; 5401 } 5402 } 5403 } 5404 5405 /* The second scan attempts to assign sections to segments. */ 5406 for (i = 0, segment = elf_tdata (ibfd)->phdr; 5407 i < num_segments; 5408 i ++, segment ++) 5409 { 5410 unsigned int section_count; 5411 asection ** sections; 5412 asection * output_section; 5413 unsigned int isec; 5414 bfd_vma matching_lma; 5415 bfd_vma suggested_lma; 5416 unsigned int j; 5417 bfd_size_type amt; 5418 5419 if (segment->p_type == PT_NULL) 5420 continue; 5421 5422 /* Compute how many sections might be placed into this segment. */ 5423 for (section = ibfd->sections, section_count = 0; 5424 section != NULL; 5425 section = section->next) 5426 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) 5427 ++section_count; 5428 5429 /* Allocate a segment map big enough to contain 5430 all of the sections we have selected. */ 5431 amt = sizeof (struct elf_segment_map); 5432 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); 5433 map = bfd_alloc (obfd, amt); 5434 if (map == NULL) 5435 return FALSE; 5436 5437 /* Initialise the fields of the segment map. Default to 5438 using the physical address of the segment in the input BFD. */ 5439 map->next = NULL; 5440 map->p_type = segment->p_type; 5441 map->p_flags = segment->p_flags; 5442 map->p_flags_valid = 1; 5443 map->p_paddr = segment->p_paddr; 5444 map->p_paddr_valid = 1; 5445 5446 /* Determine if this segment contains the ELF file header 5447 and if it contains the program headers themselves. */ 5448 map->includes_filehdr = (segment->p_offset == 0 5449 && segment->p_filesz >= iehdr->e_ehsize); 5450 5451 map->includes_phdrs = 0; 5452 5453 if (! phdr_included || segment->p_type != PT_LOAD) 5454 { 5455 map->includes_phdrs = 5456 (segment->p_offset <= (bfd_vma) iehdr->e_phoff 5457 && (segment->p_offset + segment->p_filesz 5458 >= ((bfd_vma) iehdr->e_phoff 5459 + iehdr->e_phnum * iehdr->e_phentsize))); 5460 5461 if (segment->p_type == PT_LOAD && map->includes_phdrs) 5462 phdr_included = TRUE; 5463 } 5464 5465 if (section_count == 0) 5466 { 5467 /* Special segments, such as the PT_PHDR segment, may contain 5468 no sections, but ordinary, loadable segments should contain 5469 something. They are allowed by the ELF spec however, so only 5470 a warning is produced. */ 5471 if (segment->p_type == PT_LOAD) 5472 (*_bfd_error_handler) 5473 (_("%B: warning: Empty loadable segment detected, is this intentional ?\n"), 5474 ibfd); 5475 5476 map->count = 0; 5477 *pointer_to_map = map; 5478 pointer_to_map = &map->next; 5479 5480 continue; 5481 } 5482 5483 /* Now scan the sections in the input BFD again and attempt 5484 to add their corresponding output sections to the segment map. 5485 The problem here is how to handle an output section which has 5486 been moved (ie had its LMA changed). There are four possibilities: 5487 5488 1. None of the sections have been moved. 5489 In this case we can continue to use the segment LMA from the 5490 input BFD. 5491 5492 2. All of the sections have been moved by the same amount. 5493 In this case we can change the segment's LMA to match the LMA 5494 of the first section. 5495 5496 3. Some of the sections have been moved, others have not. 5497 In this case those sections which have not been moved can be 5498 placed in the current segment which will have to have its size, 5499 and possibly its LMA changed, and a new segment or segments will 5500 have to be created to contain the other sections. 5501 5502 4. The sections have been moved, but not by the same amount. 5503 In this case we can change the segment's LMA to match the LMA 5504 of the first section and we will have to create a new segment 5505 or segments to contain the other sections. 5506 5507 In order to save time, we allocate an array to hold the section 5508 pointers that we are interested in. As these sections get assigned 5509 to a segment, they are removed from this array. */ 5510 5511 /* Gcc 2.96 miscompiles this code on mips. Don't do casting here 5512 to work around this long long bug. */ 5513 sections = bfd_malloc2 (section_count, sizeof (asection *)); 5514 if (sections == NULL) 5515 return FALSE; 5516 5517 /* Step One: Scan for segment vs section LMA conflicts. 5518 Also add the sections to the section array allocated above. 5519 Also add the sections to the current segment. In the common 5520 case, where the sections have not been moved, this means that 5521 we have completely filled the segment, and there is nothing 5522 more to do. */ 5523 isec = 0; 5524 matching_lma = 0; 5525 suggested_lma = 0; 5526 5527 for (j = 0, section = ibfd->sections; 5528 section != NULL; 5529 section = section->next) 5530 { 5531 if (INCLUDE_SECTION_IN_SEGMENT (section, segment, bed)) 5532 { 5533 output_section = section->output_section; 5534 5535 sections[j ++] = section; 5536 5537 /* The Solaris native linker always sets p_paddr to 0. 5538 We try to catch that case here, and set it to the 5539 correct value. Note - some backends require that 5540 p_paddr be left as zero. */ 5541 if (segment->p_paddr == 0 5542 && segment->p_vaddr != 0 5543 && (! bed->want_p_paddr_set_to_zero) 5544 && isec == 0 5545 && output_section->lma != 0 5546 && (output_section->vma == (segment->p_vaddr 5547 + (map->includes_filehdr 5548 ? iehdr->e_ehsize 5549 : 0) 5550 + (map->includes_phdrs 5551 ? (iehdr->e_phnum 5552 * iehdr->e_phentsize) 5553 : 0)))) 5554 map->p_paddr = segment->p_vaddr; 5555 5556 /* Match up the physical address of the segment with the 5557 LMA address of the output section. */ 5558 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) 5559 || IS_COREFILE_NOTE (segment, section) 5560 || (bed->want_p_paddr_set_to_zero && 5561 IS_CONTAINED_BY_VMA (output_section, segment)) 5562 ) 5563 { 5564 if (matching_lma == 0) 5565 matching_lma = output_section->lma; 5566 5567 /* We assume that if the section fits within the segment 5568 then it does not overlap any other section within that 5569 segment. */ 5570 map->sections[isec ++] = output_section; 5571 } 5572 else if (suggested_lma == 0) 5573 suggested_lma = output_section->lma; 5574 } 5575 } 5576 5577 BFD_ASSERT (j == section_count); 5578 5579 /* Step Two: Adjust the physical address of the current segment, 5580 if necessary. */ 5581 if (isec == section_count) 5582 { 5583 /* All of the sections fitted within the segment as currently 5584 specified. This is the default case. Add the segment to 5585 the list of built segments and carry on to process the next 5586 program header in the input BFD. */ 5587 map->count = section_count; 5588 *pointer_to_map = map; 5589 pointer_to_map = &map->next; 5590 5591 free (sections); 5592 continue; 5593 } 5594 else 5595 { 5596 if (matching_lma != 0) 5597 { 5598 /* At least one section fits inside the current segment. 5599 Keep it, but modify its physical address to match the 5600 LMA of the first section that fitted. */ 5601 map->p_paddr = matching_lma; 5602 } 5603 else 5604 { 5605 /* None of the sections fitted inside the current segment. 5606 Change the current segment's physical address to match 5607 the LMA of the first section. */ 5608 map->p_paddr = suggested_lma; 5609 } 5610 5611 /* Offset the segment physical address from the lma 5612 to allow for space taken up by elf headers. */ 5613 if (map->includes_filehdr) 5614 map->p_paddr -= iehdr->e_ehsize; 5615 5616 if (map->includes_phdrs) 5617 { 5618 map->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize; 5619 5620 /* iehdr->e_phnum is just an estimate of the number 5621 of program headers that we will need. Make a note 5622 here of the number we used and the segment we chose 5623 to hold these headers, so that we can adjust the 5624 offset when we know the correct value. */ 5625 phdr_adjust_num = iehdr->e_phnum; 5626 phdr_adjust_seg = map; 5627 } 5628 } 5629 5630 /* Step Three: Loop over the sections again, this time assigning 5631 those that fit to the current segment and removing them from the 5632 sections array; but making sure not to leave large gaps. Once all 5633 possible sections have been assigned to the current segment it is 5634 added to the list of built segments and if sections still remain 5635 to be assigned, a new segment is constructed before repeating 5636 the loop. */ 5637 isec = 0; 5638 do 5639 { 5640 map->count = 0; 5641 suggested_lma = 0; 5642 5643 /* Fill the current segment with sections that fit. */ 5644 for (j = 0; j < section_count; j++) 5645 { 5646 section = sections[j]; 5647 5648 if (section == NULL) 5649 continue; 5650 5651 output_section = section->output_section; 5652 5653 BFD_ASSERT (output_section != NULL); 5654 5655 if (IS_CONTAINED_BY_LMA (output_section, segment, map->p_paddr) 5656 || IS_COREFILE_NOTE (segment, section)) 5657 { 5658 if (map->count == 0) 5659 { 5660 /* If the first section in a segment does not start at 5661 the beginning of the segment, then something is 5662 wrong. */ 5663 if (output_section->lma != 5664 (map->p_paddr 5665 + (map->includes_filehdr ? iehdr->e_ehsize : 0) 5666 + (map->includes_phdrs 5667 ? iehdr->e_phnum * iehdr->e_phentsize 5668 : 0))) 5669 abort (); 5670 } 5671 else 5672 { 5673 asection * prev_sec; 5674 5675 prev_sec = map->sections[map->count - 1]; 5676 5677 /* If the gap between the end of the previous section 5678 and the start of this section is more than 5679 maxpagesize then we need to start a new segment. */ 5680 if ((BFD_ALIGN (prev_sec->lma + prev_sec->size, 5681 maxpagesize) 5682 < BFD_ALIGN (output_section->lma, maxpagesize)) 5683 || ((prev_sec->lma + prev_sec->size) 5684 > output_section->lma)) 5685 { 5686 if (suggested_lma == 0) 5687 suggested_lma = output_section->lma; 5688 5689 continue; 5690 } 5691 } 5692 5693 map->sections[map->count++] = output_section; 5694 ++isec; 5695 sections[j] = NULL; 5696 section->segment_mark = TRUE; 5697 } 5698 else if (suggested_lma == 0) 5699 suggested_lma = output_section->lma; 5700 } 5701 5702 BFD_ASSERT (map->count > 0); 5703 5704 /* Add the current segment to the list of built segments. */ 5705 *pointer_to_map = map; 5706 pointer_to_map = &map->next; 5707 5708 if (isec < section_count) 5709 { 5710 /* We still have not allocated all of the sections to 5711 segments. Create a new segment here, initialise it 5712 and carry on looping. */ 5713 amt = sizeof (struct elf_segment_map); 5714 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); 5715 map = bfd_alloc (obfd, amt); 5716 if (map == NULL) 5717 { 5718 free (sections); 5719 return FALSE; 5720 } 5721 5722 /* Initialise the fields of the segment map. Set the physical 5723 physical address to the LMA of the first section that has 5724 not yet been assigned. */ 5725 map->next = NULL; 5726 map->p_type = segment->p_type; 5727 map->p_flags = segment->p_flags; 5728 map->p_flags_valid = 1; 5729 map->p_paddr = suggested_lma; 5730 map->p_paddr_valid = 1; 5731 map->includes_filehdr = 0; 5732 map->includes_phdrs = 0; 5733 } 5734 } 5735 while (isec < section_count); 5736 5737 free (sections); 5738 } 5739 5740 /* The Solaris linker creates program headers in which all the 5741 p_paddr fields are zero. When we try to objcopy or strip such a 5742 file, we get confused. Check for this case, and if we find it 5743 reset the p_paddr_valid fields. */ 5744 for (map = map_first; map != NULL; map = map->next) 5745 if (map->p_paddr != 0) 5746 break; 5747 if (map == NULL) 5748 for (map = map_first; map != NULL; map = map->next) 5749 map->p_paddr_valid = 0; 5750 5751 elf_tdata (obfd)->segment_map = map_first; 5752 5753 /* If we had to estimate the number of program headers that were 5754 going to be needed, then check our estimate now and adjust 5755 the offset if necessary. */ 5756 if (phdr_adjust_seg != NULL) 5757 { 5758 unsigned int count; 5759 5760 for (count = 0, map = map_first; map != NULL; map = map->next) 5761 count++; 5762 5763 if (count > phdr_adjust_num) 5764 phdr_adjust_seg->p_paddr 5765 -= (count - phdr_adjust_num) * iehdr->e_phentsize; 5766 } 5767 5768 #undef SEGMENT_END 5769 #undef SECTION_SIZE 5770 #undef IS_CONTAINED_BY_VMA 5771 #undef IS_CONTAINED_BY_LMA 5772 #undef IS_COREFILE_NOTE 5773 #undef IS_SOLARIS_PT_INTERP 5774 #undef INCLUDE_SECTION_IN_SEGMENT 5775 #undef SEGMENT_AFTER_SEGMENT 5776 #undef SEGMENT_OVERLAPS 5777 return TRUE; 5778 } 5779 5780 /* Copy ELF program header information. */ 5781 5782 static bfd_boolean 5783 copy_elf_program_header (bfd *ibfd, bfd *obfd) 5784 { 5785 Elf_Internal_Ehdr *iehdr; 5786 struct elf_segment_map *map; 5787 struct elf_segment_map *map_first; 5788 struct elf_segment_map **pointer_to_map; 5789 Elf_Internal_Phdr *segment; 5790 unsigned int i; 5791 unsigned int num_segments; 5792 bfd_boolean phdr_included = FALSE; 5793 5794 iehdr = elf_elfheader (ibfd); 5795 5796 map_first = NULL; 5797 pointer_to_map = &map_first; 5798 5799 num_segments = elf_elfheader (ibfd)->e_phnum; 5800 for (i = 0, segment = elf_tdata (ibfd)->phdr; 5801 i < num_segments; 5802 i++, segment++) 5803 { 5804 asection *section; 5805 unsigned int section_count; 5806 bfd_size_type amt; 5807 Elf_Internal_Shdr *this_hdr; 5808 5809 /* FIXME: Do we need to copy PT_NULL segment? */ 5810 if (segment->p_type == PT_NULL) 5811 continue; 5812 5813 /* Compute how many sections are in this segment. */ 5814 for (section = ibfd->sections, section_count = 0; 5815 section != NULL; 5816 section = section->next) 5817 { 5818 this_hdr = &(elf_section_data(section)->this_hdr); 5819 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) 5820 section_count++; 5821 } 5822 5823 /* Allocate a segment map big enough to contain 5824 all of the sections we have selected. */ 5825 amt = sizeof (struct elf_segment_map); 5826 if (section_count != 0) 5827 amt += ((bfd_size_type) section_count - 1) * sizeof (asection *); 5828 map = bfd_alloc (obfd, amt); 5829 if (map == NULL) 5830 return FALSE; 5831 5832 /* Initialize the fields of the output segment map with the 5833 input segment. */ 5834 map->next = NULL; 5835 map->p_type = segment->p_type; 5836 map->p_flags = segment->p_flags; 5837 map->p_flags_valid = 1; 5838 map->p_paddr = segment->p_paddr; 5839 map->p_paddr_valid = 1; 5840 5841 /* Determine if this segment contains the ELF file header 5842 and if it contains the program headers themselves. */ 5843 map->includes_filehdr = (segment->p_offset == 0 5844 && segment->p_filesz >= iehdr->e_ehsize); 5845 5846 map->includes_phdrs = 0; 5847 if (! phdr_included || segment->p_type != PT_LOAD) 5848 { 5849 map->includes_phdrs = 5850 (segment->p_offset <= (bfd_vma) iehdr->e_phoff 5851 && (segment->p_offset + segment->p_filesz 5852 >= ((bfd_vma) iehdr->e_phoff 5853 + iehdr->e_phnum * iehdr->e_phentsize))); 5854 5855 if (segment->p_type == PT_LOAD && map->includes_phdrs) 5856 phdr_included = TRUE; 5857 } 5858 5859 if (section_count != 0) 5860 { 5861 unsigned int isec = 0; 5862 5863 for (section = ibfd->sections; 5864 section != NULL; 5865 section = section->next) 5866 { 5867 this_hdr = &(elf_section_data(section)->this_hdr); 5868 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) 5869 map->sections[isec++] = section->output_section; 5870 } 5871 } 5872 5873 map->count = section_count; 5874 *pointer_to_map = map; 5875 pointer_to_map = &map->next; 5876 } 5877 5878 elf_tdata (obfd)->segment_map = map_first; 5879 return TRUE; 5880 } 5881 5882 /* Copy private BFD data. This copies or rewrites ELF program header 5883 information. */ 5884 5885 static bfd_boolean 5886 copy_private_bfd_data (bfd *ibfd, bfd *obfd) 5887 { 5888 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 5889 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 5890 return TRUE; 5891 5892 if (elf_tdata (ibfd)->phdr == NULL) 5893 return TRUE; 5894 5895 if (ibfd->xvec == obfd->xvec) 5896 { 5897 /* Check if any sections in the input BFD covered by ELF program 5898 header are changed. */ 5899 Elf_Internal_Phdr *segment; 5900 asection *section, *osec; 5901 unsigned int i, num_segments; 5902 Elf_Internal_Shdr *this_hdr; 5903 5904 /* Initialize the segment mark field. */ 5905 for (section = obfd->sections; section != NULL; 5906 section = section->next) 5907 section->segment_mark = FALSE; 5908 5909 num_segments = elf_elfheader (ibfd)->e_phnum; 5910 for (i = 0, segment = elf_tdata (ibfd)->phdr; 5911 i < num_segments; 5912 i++, segment++) 5913 { 5914 for (section = ibfd->sections; 5915 section != NULL; section = section->next) 5916 { 5917 /* We mark the output section so that we know it comes 5918 from the input BFD. */ 5919 osec = section->output_section; 5920 if (osec) 5921 osec->segment_mark = TRUE; 5922 5923 /* Check if this section is covered by the segment. */ 5924 this_hdr = &(elf_section_data(section)->this_hdr); 5925 if (ELF_IS_SECTION_IN_SEGMENT_FILE (this_hdr, segment)) 5926 { 5927 /* FIXME: Check if its output section is changed or 5928 removed. What else do we need to check? */ 5929 if (osec == NULL 5930 || section->flags != osec->flags 5931 || section->lma != osec->lma 5932 || section->vma != osec->vma 5933 || section->size != osec->size 5934 || section->rawsize != osec->rawsize 5935 || section->alignment_power != osec->alignment_power) 5936 goto rewrite; 5937 } 5938 } 5939 } 5940 5941 /* Check to see if any output section doesn't come from the 5942 input BFD. */ 5943 for (section = obfd->sections; section != NULL; 5944 section = section->next) 5945 { 5946 if (section->segment_mark == FALSE) 5947 goto rewrite; 5948 else 5949 section->segment_mark = FALSE; 5950 } 5951 5952 return copy_elf_program_header (ibfd, obfd); 5953 } 5954 5955 rewrite: 5956 return rewrite_elf_program_header (ibfd, obfd); 5957 } 5958 5959 /* Initialize private output section information from input section. */ 5960 5961 bfd_boolean 5962 _bfd_elf_init_private_section_data (bfd *ibfd, 5963 asection *isec, 5964 bfd *obfd, 5965 asection *osec, 5966 struct bfd_link_info *link_info) 5967 5968 { 5969 Elf_Internal_Shdr *ihdr, *ohdr; 5970 bfd_boolean need_group = link_info == NULL || link_info->relocatable; 5971 5972 if (ibfd->xvec->flavour != bfd_target_elf_flavour 5973 || obfd->xvec->flavour != bfd_target_elf_flavour) 5974 return TRUE; 5975 5976 /* FIXME: What if the output ELF section type has been set to 5977 something different? */ 5978 if (elf_section_type (osec) == SHT_NULL) 5979 elf_section_type (osec) = elf_section_type (isec); 5980 5981 /* Set things up for objcopy and relocatable link. The output 5982 SHT_GROUP section will have its elf_next_in_group pointing back 5983 to the input group members. Ignore linker created group section. 5984 See elfNN_ia64_object_p in elfxx-ia64.c. */ 5985 5986 if (need_group) 5987 { 5988 if (elf_sec_group (isec) == NULL 5989 || (elf_sec_group (isec)->flags & SEC_LINKER_CREATED) == 0) 5990 { 5991 if (elf_section_flags (isec) & SHF_GROUP) 5992 elf_section_flags (osec) |= SHF_GROUP; 5993 elf_next_in_group (osec) = elf_next_in_group (isec); 5994 elf_group_name (osec) = elf_group_name (isec); 5995 } 5996 } 5997 5998 ihdr = &elf_section_data (isec)->this_hdr; 5999 6000 /* We need to handle elf_linked_to_section for SHF_LINK_ORDER. We 6001 don't use the output section of the linked-to section since it 6002 may be NULL at this point. */ 6003 if ((ihdr->sh_flags & SHF_LINK_ORDER) != 0) 6004 { 6005 ohdr = &elf_section_data (osec)->this_hdr; 6006 ohdr->sh_flags |= SHF_LINK_ORDER; 6007 elf_linked_to_section (osec) = elf_linked_to_section (isec); 6008 } 6009 6010 osec->use_rela_p = isec->use_rela_p; 6011 6012 return TRUE; 6013 } 6014 6015 /* Copy private section information. This copies over the entsize 6016 field, and sometimes the info field. */ 6017 6018 bfd_boolean 6019 _bfd_elf_copy_private_section_data (bfd *ibfd, 6020 asection *isec, 6021 bfd *obfd, 6022 asection *osec) 6023 { 6024 Elf_Internal_Shdr *ihdr, *ohdr; 6025 6026 if (ibfd->xvec->flavour != bfd_target_elf_flavour 6027 || obfd->xvec->flavour != bfd_target_elf_flavour) 6028 return TRUE; 6029 6030 ihdr = &elf_section_data (isec)->this_hdr; 6031 ohdr = &elf_section_data (osec)->this_hdr; 6032 6033 ohdr->sh_entsize = ihdr->sh_entsize; 6034 6035 if (ihdr->sh_type == SHT_SYMTAB 6036 || ihdr->sh_type == SHT_DYNSYM 6037 || ihdr->sh_type == SHT_GNU_verneed 6038 || ihdr->sh_type == SHT_GNU_verdef) 6039 ohdr->sh_info = ihdr->sh_info; 6040 6041 return _bfd_elf_init_private_section_data (ibfd, isec, obfd, osec, 6042 NULL); 6043 } 6044 6045 /* Copy private header information. */ 6046 6047 bfd_boolean 6048 _bfd_elf_copy_private_header_data (bfd *ibfd, bfd *obfd) 6049 { 6050 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 6051 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 6052 return TRUE; 6053 6054 /* Copy over private BFD data if it has not already been copied. 6055 This must be done here, rather than in the copy_private_bfd_data 6056 entry point, because the latter is called after the section 6057 contents have been set, which means that the program headers have 6058 already been worked out. */ 6059 if (elf_tdata (obfd)->segment_map == NULL && elf_tdata (ibfd)->phdr != NULL) 6060 { 6061 if (! copy_private_bfd_data (ibfd, obfd)) 6062 return FALSE; 6063 } 6064 6065 return TRUE; 6066 } 6067 6068 /* Copy private symbol information. If this symbol is in a section 6069 which we did not map into a BFD section, try to map the section 6070 index correctly. We use special macro definitions for the mapped 6071 section indices; these definitions are interpreted by the 6072 swap_out_syms function. */ 6073 6074 #define MAP_ONESYMTAB (SHN_HIOS + 1) 6075 #define MAP_DYNSYMTAB (SHN_HIOS + 2) 6076 #define MAP_STRTAB (SHN_HIOS + 3) 6077 #define MAP_SHSTRTAB (SHN_HIOS + 4) 6078 #define MAP_SYM_SHNDX (SHN_HIOS + 5) 6079 6080 bfd_boolean 6081 _bfd_elf_copy_private_symbol_data (bfd *ibfd, 6082 asymbol *isymarg, 6083 bfd *obfd, 6084 asymbol *osymarg) 6085 { 6086 elf_symbol_type *isym, *osym; 6087 6088 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 6089 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 6090 return TRUE; 6091 6092 isym = elf_symbol_from (ibfd, isymarg); 6093 osym = elf_symbol_from (obfd, osymarg); 6094 6095 if (isym != NULL 6096 && osym != NULL 6097 && bfd_is_abs_section (isym->symbol.section)) 6098 { 6099 unsigned int shndx; 6100 6101 shndx = isym->internal_elf_sym.st_shndx; 6102 if (shndx == elf_onesymtab (ibfd)) 6103 shndx = MAP_ONESYMTAB; 6104 else if (shndx == elf_dynsymtab (ibfd)) 6105 shndx = MAP_DYNSYMTAB; 6106 else if (shndx == elf_tdata (ibfd)->strtab_section) 6107 shndx = MAP_STRTAB; 6108 else if (shndx == elf_tdata (ibfd)->shstrtab_section) 6109 shndx = MAP_SHSTRTAB; 6110 else if (shndx == elf_tdata (ibfd)->symtab_shndx_section) 6111 shndx = MAP_SYM_SHNDX; 6112 osym->internal_elf_sym.st_shndx = shndx; 6113 } 6114 6115 return TRUE; 6116 } 6117 6118 /* Swap out the symbols. */ 6119 6120 static bfd_boolean 6121 swap_out_syms (bfd *abfd, 6122 struct bfd_strtab_hash **sttp, 6123 int relocatable_p) 6124 { 6125 const struct elf_backend_data *bed; 6126 int symcount; 6127 asymbol **syms; 6128 struct bfd_strtab_hash *stt; 6129 Elf_Internal_Shdr *symtab_hdr; 6130 Elf_Internal_Shdr *symtab_shndx_hdr; 6131 Elf_Internal_Shdr *symstrtab_hdr; 6132 bfd_byte *outbound_syms; 6133 bfd_byte *outbound_shndx; 6134 int idx; 6135 bfd_size_type amt; 6136 bfd_boolean name_local_sections; 6137 6138 if (!elf_map_symbols (abfd)) 6139 return FALSE; 6140 6141 /* Dump out the symtabs. */ 6142 stt = _bfd_elf_stringtab_init (); 6143 if (stt == NULL) 6144 return FALSE; 6145 6146 bed = get_elf_backend_data (abfd); 6147 symcount = bfd_get_symcount (abfd); 6148 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 6149 symtab_hdr->sh_type = SHT_SYMTAB; 6150 symtab_hdr->sh_entsize = bed->s->sizeof_sym; 6151 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); 6152 symtab_hdr->sh_info = elf_num_locals (abfd) + 1; 6153 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align; 6154 6155 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; 6156 symstrtab_hdr->sh_type = SHT_STRTAB; 6157 6158 outbound_syms = bfd_alloc2 (abfd, 1 + symcount, bed->s->sizeof_sym); 6159 if (outbound_syms == NULL) 6160 { 6161 _bfd_stringtab_free (stt); 6162 return FALSE; 6163 } 6164 symtab_hdr->contents = outbound_syms; 6165 6166 outbound_shndx = NULL; 6167 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; 6168 if (symtab_shndx_hdr->sh_name != 0) 6169 { 6170 amt = (bfd_size_type) (1 + symcount) * sizeof (Elf_External_Sym_Shndx); 6171 outbound_shndx = bfd_zalloc2 (abfd, 1 + symcount, 6172 sizeof (Elf_External_Sym_Shndx)); 6173 if (outbound_shndx == NULL) 6174 { 6175 _bfd_stringtab_free (stt); 6176 return FALSE; 6177 } 6178 6179 symtab_shndx_hdr->contents = outbound_shndx; 6180 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 6181 symtab_shndx_hdr->sh_size = amt; 6182 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 6183 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 6184 } 6185 6186 /* Now generate the data (for "contents"). */ 6187 { 6188 /* Fill in zeroth symbol and swap it out. */ 6189 Elf_Internal_Sym sym; 6190 sym.st_name = 0; 6191 sym.st_value = 0; 6192 sym.st_size = 0; 6193 sym.st_info = 0; 6194 sym.st_other = 0; 6195 sym.st_shndx = SHN_UNDEF; 6196 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); 6197 outbound_syms += bed->s->sizeof_sym; 6198 if (outbound_shndx != NULL) 6199 outbound_shndx += sizeof (Elf_External_Sym_Shndx); 6200 } 6201 6202 name_local_sections 6203 = (bed->elf_backend_name_local_section_symbols 6204 && bed->elf_backend_name_local_section_symbols (abfd)); 6205 6206 syms = bfd_get_outsymbols (abfd); 6207 for (idx = 0; idx < symcount; idx++) 6208 { 6209 Elf_Internal_Sym sym; 6210 bfd_vma value = syms[idx]->value; 6211 elf_symbol_type *type_ptr; 6212 flagword flags = syms[idx]->flags; 6213 int type; 6214 6215 if (!name_local_sections 6216 && (flags & (BSF_SECTION_SYM | BSF_GLOBAL)) == BSF_SECTION_SYM) 6217 { 6218 /* Local section symbols have no name. */ 6219 sym.st_name = 0; 6220 } 6221 else 6222 { 6223 sym.st_name = (unsigned long) _bfd_stringtab_add (stt, 6224 syms[idx]->name, 6225 TRUE, FALSE); 6226 if (sym.st_name == (unsigned long) -1) 6227 { 6228 _bfd_stringtab_free (stt); 6229 return FALSE; 6230 } 6231 } 6232 6233 type_ptr = elf_symbol_from (abfd, syms[idx]); 6234 6235 if ((flags & BSF_SECTION_SYM) == 0 6236 && bfd_is_com_section (syms[idx]->section)) 6237 { 6238 /* ELF common symbols put the alignment into the `value' field, 6239 and the size into the `size' field. This is backwards from 6240 how BFD handles it, so reverse it here. */ 6241 sym.st_size = value; 6242 if (type_ptr == NULL 6243 || type_ptr->internal_elf_sym.st_value == 0) 6244 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); 6245 else 6246 sym.st_value = type_ptr->internal_elf_sym.st_value; 6247 sym.st_shndx = _bfd_elf_section_from_bfd_section 6248 (abfd, syms[idx]->section); 6249 } 6250 else 6251 { 6252 asection *sec = syms[idx]->section; 6253 int shndx; 6254 6255 if (sec->output_section) 6256 { 6257 value += sec->output_offset; 6258 sec = sec->output_section; 6259 } 6260 6261 /* Don't add in the section vma for relocatable output. */ 6262 if (! relocatable_p) 6263 value += sec->vma; 6264 sym.st_value = value; 6265 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; 6266 6267 if (bfd_is_abs_section (sec) 6268 && type_ptr != NULL 6269 && type_ptr->internal_elf_sym.st_shndx != 0) 6270 { 6271 /* This symbol is in a real ELF section which we did 6272 not create as a BFD section. Undo the mapping done 6273 by copy_private_symbol_data. */ 6274 shndx = type_ptr->internal_elf_sym.st_shndx; 6275 switch (shndx) 6276 { 6277 case MAP_ONESYMTAB: 6278 shndx = elf_onesymtab (abfd); 6279 break; 6280 case MAP_DYNSYMTAB: 6281 shndx = elf_dynsymtab (abfd); 6282 break; 6283 case MAP_STRTAB: 6284 shndx = elf_tdata (abfd)->strtab_section; 6285 break; 6286 case MAP_SHSTRTAB: 6287 shndx = elf_tdata (abfd)->shstrtab_section; 6288 break; 6289 case MAP_SYM_SHNDX: 6290 shndx = elf_tdata (abfd)->symtab_shndx_section; 6291 break; 6292 default: 6293 break; 6294 } 6295 } 6296 else 6297 { 6298 shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 6299 6300 if (shndx == -1) 6301 { 6302 asection *sec2; 6303 6304 /* Writing this would be a hell of a lot easier if 6305 we had some decent documentation on bfd, and 6306 knew what to expect of the library, and what to 6307 demand of applications. For example, it 6308 appears that `objcopy' might not set the 6309 section of a symbol to be a section that is 6310 actually in the output file. */ 6311 sec2 = bfd_get_section_by_name (abfd, sec->name); 6312 if (sec2 == NULL) 6313 { 6314 _bfd_error_handler (_("\ 6315 Unable to find equivalent output section for symbol '%s' from section '%s'"), 6316 syms[idx]->name ? syms[idx]->name : "<Local sym>", 6317 sec->name); 6318 bfd_set_error (bfd_error_invalid_operation); 6319 _bfd_stringtab_free (stt); 6320 return FALSE; 6321 } 6322 6323 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); 6324 BFD_ASSERT (shndx != -1); 6325 } 6326 } 6327 6328 sym.st_shndx = shndx; 6329 } 6330 6331 if ((flags & BSF_THREAD_LOCAL) != 0) 6332 type = STT_TLS; 6333 else if ((flags & BSF_FUNCTION) != 0) 6334 type = STT_FUNC; 6335 else if ((flags & BSF_OBJECT) != 0) 6336 type = STT_OBJECT; 6337 else 6338 type = STT_NOTYPE; 6339 6340 if (syms[idx]->section->flags & SEC_THREAD_LOCAL) 6341 type = STT_TLS; 6342 6343 /* Processor-specific types. */ 6344 if (type_ptr != NULL 6345 && bed->elf_backend_get_symbol_type) 6346 type = ((*bed->elf_backend_get_symbol_type) 6347 (&type_ptr->internal_elf_sym, type)); 6348 6349 if (flags & BSF_SECTION_SYM) 6350 { 6351 if (flags & BSF_GLOBAL) 6352 sym.st_info = ELF_ST_INFO (STB_GLOBAL, STT_SECTION); 6353 else 6354 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 6355 } 6356 else if (bfd_is_com_section (syms[idx]->section)) 6357 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); 6358 else if (bfd_is_und_section (syms[idx]->section)) 6359 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) 6360 ? STB_WEAK 6361 : STB_GLOBAL), 6362 type); 6363 else if (flags & BSF_FILE) 6364 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 6365 else 6366 { 6367 int bind = STB_LOCAL; 6368 6369 if (flags & BSF_LOCAL) 6370 bind = STB_LOCAL; 6371 else if (flags & BSF_WEAK) 6372 bind = STB_WEAK; 6373 else if (flags & BSF_GLOBAL) 6374 bind = STB_GLOBAL; 6375 6376 sym.st_info = ELF_ST_INFO (bind, type); 6377 } 6378 6379 if (type_ptr != NULL) 6380 sym.st_other = type_ptr->internal_elf_sym.st_other; 6381 else 6382 sym.st_other = 0; 6383 6384 bed->s->swap_symbol_out (abfd, &sym, outbound_syms, outbound_shndx); 6385 outbound_syms += bed->s->sizeof_sym; 6386 if (outbound_shndx != NULL) 6387 outbound_shndx += sizeof (Elf_External_Sym_Shndx); 6388 } 6389 6390 *sttp = stt; 6391 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); 6392 symstrtab_hdr->sh_type = SHT_STRTAB; 6393 6394 symstrtab_hdr->sh_flags = 0; 6395 symstrtab_hdr->sh_addr = 0; 6396 symstrtab_hdr->sh_entsize = 0; 6397 symstrtab_hdr->sh_link = 0; 6398 symstrtab_hdr->sh_info = 0; 6399 symstrtab_hdr->sh_addralign = 1; 6400 6401 return TRUE; 6402 } 6403 6404 /* Return the number of bytes required to hold the symtab vector. 6405 6406 Note that we base it on the count plus 1, since we will null terminate 6407 the vector allocated based on this size. However, the ELF symbol table 6408 always has a dummy entry as symbol #0, so it ends up even. */ 6409 6410 long 6411 _bfd_elf_get_symtab_upper_bound (bfd *abfd) 6412 { 6413 long symcount; 6414 long symtab_size; 6415 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; 6416 6417 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 6418 symtab_size = (symcount + 1) * (sizeof (asymbol *)); 6419 if (symcount > 0) 6420 symtab_size -= sizeof (asymbol *); 6421 6422 return symtab_size; 6423 } 6424 6425 long 6426 _bfd_elf_get_dynamic_symtab_upper_bound (bfd *abfd) 6427 { 6428 long symcount; 6429 long symtab_size; 6430 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; 6431 6432 if (elf_dynsymtab (abfd) == 0) 6433 { 6434 bfd_set_error (bfd_error_invalid_operation); 6435 return -1; 6436 } 6437 6438 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 6439 symtab_size = (symcount + 1) * (sizeof (asymbol *)); 6440 if (symcount > 0) 6441 symtab_size -= sizeof (asymbol *); 6442 6443 return symtab_size; 6444 } 6445 6446 long 6447 _bfd_elf_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, 6448 sec_ptr asect) 6449 { 6450 return (asect->reloc_count + 1) * sizeof (arelent *); 6451 } 6452 6453 /* Canonicalize the relocs. */ 6454 6455 long 6456 _bfd_elf_canonicalize_reloc (bfd *abfd, 6457 sec_ptr section, 6458 arelent **relptr, 6459 asymbol **symbols) 6460 { 6461 arelent *tblptr; 6462 unsigned int i; 6463 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6464 6465 if (! bed->s->slurp_reloc_table (abfd, section, symbols, FALSE)) 6466 return -1; 6467 6468 tblptr = section->relocation; 6469 for (i = 0; i < section->reloc_count; i++) 6470 *relptr++ = tblptr++; 6471 6472 *relptr = NULL; 6473 6474 return section->reloc_count; 6475 } 6476 6477 long 6478 _bfd_elf_canonicalize_symtab (bfd *abfd, asymbol **allocation) 6479 { 6480 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6481 long symcount = bed->s->slurp_symbol_table (abfd, allocation, FALSE); 6482 6483 if (symcount >= 0) 6484 bfd_get_symcount (abfd) = symcount; 6485 return symcount; 6486 } 6487 6488 long 6489 _bfd_elf_canonicalize_dynamic_symtab (bfd *abfd, 6490 asymbol **allocation) 6491 { 6492 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 6493 long symcount = bed->s->slurp_symbol_table (abfd, allocation, TRUE); 6494 6495 if (symcount >= 0) 6496 bfd_get_dynamic_symcount (abfd) = symcount; 6497 return symcount; 6498 } 6499 6500 /* Return the size required for the dynamic reloc entries. Any loadable 6501 section that was actually installed in the BFD, and has type SHT_REL 6502 or SHT_RELA, and uses the dynamic symbol table, is considered to be a 6503 dynamic reloc section. */ 6504 6505 long 6506 _bfd_elf_get_dynamic_reloc_upper_bound (bfd *abfd) 6507 { 6508 long ret; 6509 asection *s; 6510 6511 if (elf_dynsymtab (abfd) == 0) 6512 { 6513 bfd_set_error (bfd_error_invalid_operation); 6514 return -1; 6515 } 6516 6517 ret = sizeof (arelent *); 6518 for (s = abfd->sections; s != NULL; s = s->next) 6519 if ((s->flags & SEC_LOAD) != 0 6520 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 6521 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 6522 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) 6523 ret += ((s->size / elf_section_data (s)->this_hdr.sh_entsize) 6524 * sizeof (arelent *)); 6525 6526 return ret; 6527 } 6528 6529 /* Canonicalize the dynamic relocation entries. Note that we return the 6530 dynamic relocations as a single block, although they are actually 6531 associated with particular sections; the interface, which was 6532 designed for SunOS style shared libraries, expects that there is only 6533 one set of dynamic relocs. Any loadable section that was actually 6534 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses the 6535 dynamic symbol table, is considered to be a dynamic reloc section. */ 6536 6537 long 6538 _bfd_elf_canonicalize_dynamic_reloc (bfd *abfd, 6539 arelent **storage, 6540 asymbol **syms) 6541 { 6542 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); 6543 asection *s; 6544 long ret; 6545 6546 if (elf_dynsymtab (abfd) == 0) 6547 { 6548 bfd_set_error (bfd_error_invalid_operation); 6549 return -1; 6550 } 6551 6552 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; 6553 ret = 0; 6554 for (s = abfd->sections; s != NULL; s = s->next) 6555 { 6556 if ((s->flags & SEC_LOAD) != 0 6557 && elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 6558 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 6559 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) 6560 { 6561 arelent *p; 6562 long count, i; 6563 6564 if (! (*slurp_relocs) (abfd, s, syms, TRUE)) 6565 return -1; 6566 count = s->size / elf_section_data (s)->this_hdr.sh_entsize; 6567 p = s->relocation; 6568 for (i = 0; i < count; i++) 6569 *storage++ = p++; 6570 ret += count; 6571 } 6572 } 6573 6574 *storage = NULL; 6575 6576 return ret; 6577 } 6578 6579 /* Read in the version information. */ 6580 6581 bfd_boolean 6582 _bfd_elf_slurp_version_tables (bfd *abfd, bfd_boolean default_imported_symver) 6583 { 6584 bfd_byte *contents = NULL; 6585 unsigned int freeidx = 0; 6586 6587 if (elf_dynverref (abfd) != 0) 6588 { 6589 Elf_Internal_Shdr *hdr; 6590 Elf_External_Verneed *everneed; 6591 Elf_Internal_Verneed *iverneed; 6592 unsigned int i; 6593 bfd_byte *contents_end; 6594 6595 hdr = &elf_tdata (abfd)->dynverref_hdr; 6596 6597 elf_tdata (abfd)->verref = bfd_zalloc2 (abfd, hdr->sh_info, 6598 sizeof (Elf_Internal_Verneed)); 6599 if (elf_tdata (abfd)->verref == NULL) 6600 goto error_return; 6601 6602 elf_tdata (abfd)->cverrefs = hdr->sh_info; 6603 6604 contents = bfd_malloc (hdr->sh_size); 6605 if (contents == NULL) 6606 { 6607 error_return_verref: 6608 elf_tdata (abfd)->verref = NULL; 6609 elf_tdata (abfd)->cverrefs = 0; 6610 goto error_return; 6611 } 6612 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 6613 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) 6614 goto error_return_verref; 6615 6616 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verneed)) 6617 goto error_return_verref; 6618 6619 BFD_ASSERT (sizeof (Elf_External_Verneed) 6620 == sizeof (Elf_External_Vernaux)); 6621 contents_end = contents + hdr->sh_size - sizeof (Elf_External_Verneed); 6622 everneed = (Elf_External_Verneed *) contents; 6623 iverneed = elf_tdata (abfd)->verref; 6624 for (i = 0; i < hdr->sh_info; i++, iverneed++) 6625 { 6626 Elf_External_Vernaux *evernaux; 6627 Elf_Internal_Vernaux *ivernaux; 6628 unsigned int j; 6629 6630 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); 6631 6632 iverneed->vn_bfd = abfd; 6633 6634 iverneed->vn_filename = 6635 bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 6636 iverneed->vn_file); 6637 if (iverneed->vn_filename == NULL) 6638 goto error_return_verref; 6639 6640 if (iverneed->vn_cnt == 0) 6641 iverneed->vn_auxptr = NULL; 6642 else 6643 { 6644 iverneed->vn_auxptr = bfd_alloc2 (abfd, iverneed->vn_cnt, 6645 sizeof (Elf_Internal_Vernaux)); 6646 if (iverneed->vn_auxptr == NULL) 6647 goto error_return_verref; 6648 } 6649 6650 if (iverneed->vn_aux 6651 > (size_t) (contents_end - (bfd_byte *) everneed)) 6652 goto error_return_verref; 6653 6654 evernaux = ((Elf_External_Vernaux *) 6655 ((bfd_byte *) everneed + iverneed->vn_aux)); 6656 ivernaux = iverneed->vn_auxptr; 6657 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) 6658 { 6659 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); 6660 6661 ivernaux->vna_nodename = 6662 bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 6663 ivernaux->vna_name); 6664 if (ivernaux->vna_nodename == NULL) 6665 goto error_return_verref; 6666 6667 if (j + 1 < iverneed->vn_cnt) 6668 ivernaux->vna_nextptr = ivernaux + 1; 6669 else 6670 ivernaux->vna_nextptr = NULL; 6671 6672 if (ivernaux->vna_next 6673 > (size_t) (contents_end - (bfd_byte *) evernaux)) 6674 goto error_return_verref; 6675 6676 evernaux = ((Elf_External_Vernaux *) 6677 ((bfd_byte *) evernaux + ivernaux->vna_next)); 6678 6679 if (ivernaux->vna_other > freeidx) 6680 freeidx = ivernaux->vna_other; 6681 } 6682 6683 if (i + 1 < hdr->sh_info) 6684 iverneed->vn_nextref = iverneed + 1; 6685 else 6686 iverneed->vn_nextref = NULL; 6687 6688 if (iverneed->vn_next 6689 > (size_t) (contents_end - (bfd_byte *) everneed)) 6690 goto error_return_verref; 6691 6692 everneed = ((Elf_External_Verneed *) 6693 ((bfd_byte *) everneed + iverneed->vn_next)); 6694 } 6695 6696 free (contents); 6697 contents = NULL; 6698 } 6699 6700 if (elf_dynverdef (abfd) != 0) 6701 { 6702 Elf_Internal_Shdr *hdr; 6703 Elf_External_Verdef *everdef; 6704 Elf_Internal_Verdef *iverdef; 6705 Elf_Internal_Verdef *iverdefarr; 6706 Elf_Internal_Verdef iverdefmem; 6707 unsigned int i; 6708 unsigned int maxidx; 6709 bfd_byte *contents_end_def, *contents_end_aux; 6710 6711 hdr = &elf_tdata (abfd)->dynverdef_hdr; 6712 6713 contents = bfd_malloc (hdr->sh_size); 6714 if (contents == NULL) 6715 goto error_return; 6716 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 6717 || bfd_bread (contents, hdr->sh_size, abfd) != hdr->sh_size) 6718 goto error_return; 6719 6720 if (hdr->sh_info && hdr->sh_size < sizeof (Elf_External_Verdef)) 6721 goto error_return; 6722 6723 BFD_ASSERT (sizeof (Elf_External_Verdef) 6724 >= sizeof (Elf_External_Verdaux)); 6725 contents_end_def = contents + hdr->sh_size 6726 - sizeof (Elf_External_Verdef); 6727 contents_end_aux = contents + hdr->sh_size 6728 - sizeof (Elf_External_Verdaux); 6729 6730 /* We know the number of entries in the section but not the maximum 6731 index. Therefore we have to run through all entries and find 6732 the maximum. */ 6733 everdef = (Elf_External_Verdef *) contents; 6734 maxidx = 0; 6735 for (i = 0; i < hdr->sh_info; ++i) 6736 { 6737 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 6738 6739 if ((iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION)) > maxidx) 6740 maxidx = iverdefmem.vd_ndx & ((unsigned) VERSYM_VERSION); 6741 6742 if (iverdefmem.vd_next 6743 > (size_t) (contents_end_def - (bfd_byte *) everdef)) 6744 goto error_return; 6745 6746 everdef = ((Elf_External_Verdef *) 6747 ((bfd_byte *) everdef + iverdefmem.vd_next)); 6748 } 6749 6750 if (default_imported_symver) 6751 { 6752 if (freeidx > maxidx) 6753 maxidx = ++freeidx; 6754 else 6755 freeidx = ++maxidx; 6756 } 6757 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, maxidx, 6758 sizeof (Elf_Internal_Verdef)); 6759 if (elf_tdata (abfd)->verdef == NULL) 6760 goto error_return; 6761 6762 elf_tdata (abfd)->cverdefs = maxidx; 6763 6764 everdef = (Elf_External_Verdef *) contents; 6765 iverdefarr = elf_tdata (abfd)->verdef; 6766 for (i = 0; i < hdr->sh_info; i++) 6767 { 6768 Elf_External_Verdaux *everdaux; 6769 Elf_Internal_Verdaux *iverdaux; 6770 unsigned int j; 6771 6772 _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem); 6773 6774 if ((iverdefmem.vd_ndx & VERSYM_VERSION) == 0) 6775 { 6776 error_return_verdef: 6777 elf_tdata (abfd)->verdef = NULL; 6778 elf_tdata (abfd)->cverdefs = 0; 6779 goto error_return; 6780 } 6781 6782 iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1]; 6783 memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef)); 6784 6785 iverdef->vd_bfd = abfd; 6786 6787 if (iverdef->vd_cnt == 0) 6788 iverdef->vd_auxptr = NULL; 6789 else 6790 { 6791 iverdef->vd_auxptr = bfd_alloc2 (abfd, iverdef->vd_cnt, 6792 sizeof (Elf_Internal_Verdaux)); 6793 if (iverdef->vd_auxptr == NULL) 6794 goto error_return_verdef; 6795 } 6796 6797 if (iverdef->vd_aux 6798 > (size_t) (contents_end_aux - (bfd_byte *) everdef)) 6799 goto error_return_verdef; 6800 6801 everdaux = ((Elf_External_Verdaux *) 6802 ((bfd_byte *) everdef + iverdef->vd_aux)); 6803 iverdaux = iverdef->vd_auxptr; 6804 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) 6805 { 6806 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); 6807 6808 iverdaux->vda_nodename = 6809 bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 6810 iverdaux->vda_name); 6811 if (iverdaux->vda_nodename == NULL) 6812 goto error_return_verdef; 6813 6814 if (j + 1 < iverdef->vd_cnt) 6815 iverdaux->vda_nextptr = iverdaux + 1; 6816 else 6817 iverdaux->vda_nextptr = NULL; 6818 6819 if (iverdaux->vda_next 6820 > (size_t) (contents_end_aux - (bfd_byte *) everdaux)) 6821 goto error_return_verdef; 6822 6823 everdaux = ((Elf_External_Verdaux *) 6824 ((bfd_byte *) everdaux + iverdaux->vda_next)); 6825 } 6826 6827 if (iverdef->vd_cnt) 6828 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; 6829 6830 if ((size_t) (iverdef - iverdefarr) + 1 < maxidx) 6831 iverdef->vd_nextdef = iverdef + 1; 6832 else 6833 iverdef->vd_nextdef = NULL; 6834 6835 everdef = ((Elf_External_Verdef *) 6836 ((bfd_byte *) everdef + iverdef->vd_next)); 6837 } 6838 6839 free (contents); 6840 contents = NULL; 6841 } 6842 else if (default_imported_symver) 6843 { 6844 if (freeidx < 3) 6845 freeidx = 3; 6846 else 6847 freeidx++; 6848 6849 elf_tdata (abfd)->verdef = bfd_zalloc2 (abfd, freeidx, 6850 sizeof (Elf_Internal_Verdef)); 6851 if (elf_tdata (abfd)->verdef == NULL) 6852 goto error_return; 6853 6854 elf_tdata (abfd)->cverdefs = freeidx; 6855 } 6856 6857 /* Create a default version based on the soname. */ 6858 if (default_imported_symver) 6859 { 6860 Elf_Internal_Verdef *iverdef; 6861 Elf_Internal_Verdaux *iverdaux; 6862 6863 iverdef = &elf_tdata (abfd)->verdef[freeidx - 1];; 6864 6865 iverdef->vd_version = VER_DEF_CURRENT; 6866 iverdef->vd_flags = 0; 6867 iverdef->vd_ndx = freeidx; 6868 iverdef->vd_cnt = 1; 6869 6870 iverdef->vd_bfd = abfd; 6871 6872 iverdef->vd_nodename = bfd_elf_get_dt_soname (abfd); 6873 if (iverdef->vd_nodename == NULL) 6874 goto error_return_verdef; 6875 iverdef->vd_nextdef = NULL; 6876 iverdef->vd_auxptr = bfd_alloc (abfd, sizeof (Elf_Internal_Verdaux)); 6877 if (iverdef->vd_auxptr == NULL) 6878 goto error_return_verdef; 6879 6880 iverdaux = iverdef->vd_auxptr; 6881 iverdaux->vda_nodename = iverdef->vd_nodename; 6882 iverdaux->vda_nextptr = NULL; 6883 } 6884 6885 return TRUE; 6886 6887 error_return: 6888 if (contents != NULL) 6889 free (contents); 6890 return FALSE; 6891 } 6892 6893 asymbol * 6894 _bfd_elf_make_empty_symbol (bfd *abfd) 6895 { 6896 elf_symbol_type *newsym; 6897 bfd_size_type amt = sizeof (elf_symbol_type); 6898 6899 newsym = bfd_zalloc (abfd, amt); 6900 if (!newsym) 6901 return NULL; 6902 else 6903 { 6904 newsym->symbol.the_bfd = abfd; 6905 return &newsym->symbol; 6906 } 6907 } 6908 6909 void 6910 _bfd_elf_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED, 6911 asymbol *symbol, 6912 symbol_info *ret) 6913 { 6914 bfd_symbol_info (symbol, ret); 6915 } 6916 6917 /* Return whether a symbol name implies a local symbol. Most targets 6918 use this function for the is_local_label_name entry point, but some 6919 override it. */ 6920 6921 bfd_boolean 6922 _bfd_elf_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, 6923 const char *name) 6924 { 6925 /* Normal local symbols start with ``.L''. */ 6926 if (name[0] == '.' && name[1] == 'L') 6927 return TRUE; 6928 6929 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate 6930 DWARF debugging symbols starting with ``..''. */ 6931 if (name[0] == '.' && name[1] == '.') 6932 return TRUE; 6933 6934 /* gcc will sometimes generate symbols beginning with ``_.L_'' when 6935 emitting DWARF debugging output. I suspect this is actually a 6936 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call 6937 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading 6938 underscore to be emitted on some ELF targets). For ease of use, 6939 we treat such symbols as local. */ 6940 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') 6941 return TRUE; 6942 6943 return FALSE; 6944 } 6945 6946 alent * 6947 _bfd_elf_get_lineno (bfd *abfd ATTRIBUTE_UNUSED, 6948 asymbol *symbol ATTRIBUTE_UNUSED) 6949 { 6950 abort (); 6951 return NULL; 6952 } 6953 6954 bfd_boolean 6955 _bfd_elf_set_arch_mach (bfd *abfd, 6956 enum bfd_architecture arch, 6957 unsigned long machine) 6958 { 6959 /* If this isn't the right architecture for this backend, and this 6960 isn't the generic backend, fail. */ 6961 if (arch != get_elf_backend_data (abfd)->arch 6962 && arch != bfd_arch_unknown 6963 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) 6964 return FALSE; 6965 6966 return bfd_default_set_arch_mach (abfd, arch, machine); 6967 } 6968 6969 /* Find the function to a particular section and offset, 6970 for error reporting. */ 6971 6972 static bfd_boolean 6973 elf_find_function (bfd *abfd ATTRIBUTE_UNUSED, 6974 asection *section, 6975 asymbol **symbols, 6976 bfd_vma offset, 6977 const char **filename_ptr, 6978 const char **functionname_ptr) 6979 { 6980 const char *filename; 6981 asymbol *func, *file; 6982 bfd_vma low_func; 6983 asymbol **p; 6984 /* ??? Given multiple file symbols, it is impossible to reliably 6985 choose the right file name for global symbols. File symbols are 6986 local symbols, and thus all file symbols must sort before any 6987 global symbols. The ELF spec may be interpreted to say that a 6988 file symbol must sort before other local symbols, but currently 6989 ld -r doesn't do this. So, for ld -r output, it is possible to 6990 make a better choice of file name for local symbols by ignoring 6991 file symbols appearing after a given local symbol. */ 6992 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state; 6993 6994 filename = NULL; 6995 func = NULL; 6996 file = NULL; 6997 low_func = 0; 6998 state = nothing_seen; 6999 7000 for (p = symbols; *p != NULL; p++) 7001 { 7002 elf_symbol_type *q; 7003 7004 q = (elf_symbol_type *) *p; 7005 7006 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info)) 7007 { 7008 default: 7009 break; 7010 case STT_FILE: 7011 file = &q->symbol; 7012 if (state == symbol_seen) 7013 state = file_after_symbol_seen; 7014 continue; 7015 case STT_NOTYPE: 7016 case STT_FUNC: 7017 if (bfd_get_section (&q->symbol) == section 7018 && q->symbol.value >= low_func 7019 && q->symbol.value <= offset) 7020 { 7021 func = (asymbol *) q; 7022 low_func = q->symbol.value; 7023 filename = NULL; 7024 if (file != NULL 7025 && (ELF_ST_BIND (q->internal_elf_sym.st_info) == STB_LOCAL 7026 || state != file_after_symbol_seen)) 7027 filename = bfd_asymbol_name (file); 7028 } 7029 break; 7030 } 7031 if (state == nothing_seen) 7032 state = symbol_seen; 7033 } 7034 7035 if (func == NULL) 7036 return FALSE; 7037 7038 if (filename_ptr) 7039 *filename_ptr = filename; 7040 if (functionname_ptr) 7041 *functionname_ptr = bfd_asymbol_name (func); 7042 7043 return TRUE; 7044 } 7045 7046 /* Find the nearest line to a particular section and offset, 7047 for error reporting. */ 7048 7049 bfd_boolean 7050 _bfd_elf_find_nearest_line (bfd *abfd, 7051 asection *section, 7052 asymbol **symbols, 7053 bfd_vma offset, 7054 const char **filename_ptr, 7055 const char **functionname_ptr, 7056 unsigned int *line_ptr) 7057 { 7058 bfd_boolean found; 7059 7060 if (_bfd_dwarf1_find_nearest_line (abfd, section, symbols, offset, 7061 filename_ptr, functionname_ptr, 7062 line_ptr)) 7063 { 7064 if (!*functionname_ptr) 7065 elf_find_function (abfd, section, symbols, offset, 7066 *filename_ptr ? NULL : filename_ptr, 7067 functionname_ptr); 7068 7069 return TRUE; 7070 } 7071 7072 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, 7073 filename_ptr, functionname_ptr, 7074 line_ptr, 0, 7075 &elf_tdata (abfd)->dwarf2_find_line_info)) 7076 { 7077 if (!*functionname_ptr) 7078 elf_find_function (abfd, section, symbols, offset, 7079 *filename_ptr ? NULL : filename_ptr, 7080 functionname_ptr); 7081 7082 return TRUE; 7083 } 7084 7085 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, 7086 &found, filename_ptr, 7087 functionname_ptr, line_ptr, 7088 &elf_tdata (abfd)->line_info)) 7089 return FALSE; 7090 if (found && (*functionname_ptr || *line_ptr)) 7091 return TRUE; 7092 7093 if (symbols == NULL) 7094 return FALSE; 7095 7096 if (! elf_find_function (abfd, section, symbols, offset, 7097 filename_ptr, functionname_ptr)) 7098 return FALSE; 7099 7100 *line_ptr = 0; 7101 return TRUE; 7102 } 7103 7104 /* Find the line for a symbol. */ 7105 7106 bfd_boolean 7107 _bfd_elf_find_line (bfd *abfd, asymbol **symbols, asymbol *symbol, 7108 const char **filename_ptr, unsigned int *line_ptr) 7109 { 7110 return _bfd_dwarf2_find_line (abfd, symbols, symbol, 7111 filename_ptr, line_ptr, 0, 7112 &elf_tdata (abfd)->dwarf2_find_line_info); 7113 } 7114 7115 /* After a call to bfd_find_nearest_line, successive calls to 7116 bfd_find_inliner_info can be used to get source information about 7117 each level of function inlining that terminated at the address 7118 passed to bfd_find_nearest_line. Currently this is only supported 7119 for DWARF2 with appropriate DWARF3 extensions. */ 7120 7121 bfd_boolean 7122 _bfd_elf_find_inliner_info (bfd *abfd, 7123 const char **filename_ptr, 7124 const char **functionname_ptr, 7125 unsigned int *line_ptr) 7126 { 7127 bfd_boolean found; 7128 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, 7129 functionname_ptr, line_ptr, 7130 & elf_tdata (abfd)->dwarf2_find_line_info); 7131 return found; 7132 } 7133 7134 int 7135 _bfd_elf_sizeof_headers (bfd *abfd, bfd_boolean reloc) 7136 { 7137 int ret; 7138 7139 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr; 7140 if (! reloc) 7141 ret += get_program_header_size (abfd); 7142 return ret; 7143 } 7144 7145 bfd_boolean 7146 _bfd_elf_set_section_contents (bfd *abfd, 7147 sec_ptr section, 7148 const void *location, 7149 file_ptr offset, 7150 bfd_size_type count) 7151 { 7152 Elf_Internal_Shdr *hdr; 7153 bfd_signed_vma pos; 7154 7155 if (! abfd->output_has_begun 7156 && ! _bfd_elf_compute_section_file_positions (abfd, NULL)) 7157 return FALSE; 7158 7159 hdr = &elf_section_data (section)->this_hdr; 7160 pos = hdr->sh_offset + offset; 7161 if (bfd_seek (abfd, pos, SEEK_SET) != 0 7162 || bfd_bwrite (location, count, abfd) != count) 7163 return FALSE; 7164 7165 return TRUE; 7166 } 7167 7168 void 7169 _bfd_elf_no_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, 7170 arelent *cache_ptr ATTRIBUTE_UNUSED, 7171 Elf_Internal_Rela *dst ATTRIBUTE_UNUSED) 7172 { 7173 abort (); 7174 } 7175 7176 /* Try to convert a non-ELF reloc into an ELF one. */ 7177 7178 bfd_boolean 7179 _bfd_elf_validate_reloc (bfd *abfd, arelent *areloc) 7180 { 7181 /* Check whether we really have an ELF howto. */ 7182 7183 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) 7184 { 7185 bfd_reloc_code_real_type code; 7186 reloc_howto_type *howto; 7187 7188 /* Alien reloc: Try to determine its type to replace it with an 7189 equivalent ELF reloc. */ 7190 7191 if (areloc->howto->pc_relative) 7192 { 7193 switch (areloc->howto->bitsize) 7194 { 7195 case 8: 7196 code = BFD_RELOC_8_PCREL; 7197 break; 7198 case 12: 7199 code = BFD_RELOC_12_PCREL; 7200 break; 7201 case 16: 7202 code = BFD_RELOC_16_PCREL; 7203 break; 7204 case 24: 7205 code = BFD_RELOC_24_PCREL; 7206 break; 7207 case 32: 7208 code = BFD_RELOC_32_PCREL; 7209 break; 7210 case 64: 7211 code = BFD_RELOC_64_PCREL; 7212 break; 7213 default: 7214 goto fail; 7215 } 7216 7217 howto = bfd_reloc_type_lookup (abfd, code); 7218 7219 if (areloc->howto->pcrel_offset != howto->pcrel_offset) 7220 { 7221 if (howto->pcrel_offset) 7222 areloc->addend += areloc->address; 7223 else 7224 areloc->addend -= areloc->address; /* addend is unsigned!! */ 7225 } 7226 } 7227 else 7228 { 7229 switch (areloc->howto->bitsize) 7230 { 7231 case 8: 7232 code = BFD_RELOC_8; 7233 break; 7234 case 14: 7235 code = BFD_RELOC_14; 7236 break; 7237 case 16: 7238 code = BFD_RELOC_16; 7239 break; 7240 case 26: 7241 code = BFD_RELOC_26; 7242 break; 7243 case 32: 7244 code = BFD_RELOC_32; 7245 break; 7246 case 64: 7247 code = BFD_RELOC_64; 7248 break; 7249 default: 7250 goto fail; 7251 } 7252 7253 howto = bfd_reloc_type_lookup (abfd, code); 7254 } 7255 7256 if (howto) 7257 areloc->howto = howto; 7258 else 7259 goto fail; 7260 } 7261 7262 return TRUE; 7263 7264 fail: 7265 (*_bfd_error_handler) 7266 (_("%B: unsupported relocation type %s"), 7267 abfd, areloc->howto->name); 7268 bfd_set_error (bfd_error_bad_value); 7269 return FALSE; 7270 } 7271 7272 bfd_boolean 7273 _bfd_elf_close_and_cleanup (bfd *abfd) 7274 { 7275 if (bfd_get_format (abfd) == bfd_object) 7276 { 7277 if (elf_shstrtab (abfd) != NULL) 7278 _bfd_elf_strtab_free (elf_shstrtab (abfd)); 7279 _bfd_dwarf2_cleanup_debug_info (abfd); 7280 } 7281 7282 return _bfd_generic_close_and_cleanup (abfd); 7283 } 7284 7285 /* For Rel targets, we encode meaningful data for BFD_RELOC_VTABLE_ENTRY 7286 in the relocation's offset. Thus we cannot allow any sort of sanity 7287 range-checking to interfere. There is nothing else to do in processing 7288 this reloc. */ 7289 7290 bfd_reloc_status_type 7291 _bfd_elf_rel_vtable_reloc_fn 7292 (bfd *abfd ATTRIBUTE_UNUSED, arelent *re ATTRIBUTE_UNUSED, 7293 struct bfd_symbol *symbol ATTRIBUTE_UNUSED, 7294 void *data ATTRIBUTE_UNUSED, asection *is ATTRIBUTE_UNUSED, 7295 bfd *obfd ATTRIBUTE_UNUSED, char **errmsg ATTRIBUTE_UNUSED) 7296 { 7297 return bfd_reloc_ok; 7298 } 7299 7300 /* Elf core file support. Much of this only works on native 7301 toolchains, since we rely on knowing the 7302 machine-dependent procfs structure in order to pick 7303 out details about the corefile. */ 7304 7305 #ifdef HAVE_SYS_PROCFS_H 7306 # include <sys/procfs.h> 7307 #endif 7308 7309 /* FIXME: this is kinda wrong, but it's what gdb wants. */ 7310 7311 static int 7312 elfcore_make_pid (bfd *abfd) 7313 { 7314 return ((elf_tdata (abfd)->core_lwpid << 16) 7315 + (elf_tdata (abfd)->core_pid)); 7316 } 7317 7318 /* If there isn't a section called NAME, make one, using 7319 data from SECT. Note, this function will generate a 7320 reference to NAME, so you shouldn't deallocate or 7321 overwrite it. */ 7322 7323 static bfd_boolean 7324 elfcore_maybe_make_sect (bfd *abfd, char *name, asection *sect) 7325 { 7326 asection *sect2; 7327 7328 if (bfd_get_section_by_name (abfd, name) != NULL) 7329 return TRUE; 7330 7331 sect2 = bfd_make_section (abfd, name); 7332 if (sect2 == NULL) 7333 return FALSE; 7334 7335 sect2->size = sect->size; 7336 sect2->filepos = sect->filepos; 7337 sect2->flags = sect->flags; 7338 sect2->alignment_power = sect->alignment_power; 7339 return TRUE; 7340 } 7341 7342 /* Create a pseudosection containing SIZE bytes at FILEPOS. This 7343 actually creates up to two pseudosections: 7344 - For the single-threaded case, a section named NAME, unless 7345 such a section already exists. 7346 - For the multi-threaded case, a section named "NAME/PID", where 7347 PID is elfcore_make_pid (abfd). 7348 Both pseudosections have identical contents. */ 7349 bfd_boolean 7350 _bfd_elfcore_make_pseudosection (bfd *abfd, 7351 char *name, 7352 size_t size, 7353 ufile_ptr filepos) 7354 { 7355 char buf[100]; 7356 char *threaded_name; 7357 size_t len; 7358 asection *sect; 7359 7360 /* Build the section name. */ 7361 7362 sprintf (buf, "%s/%d", name, elfcore_make_pid (abfd)); 7363 len = strlen (buf) + 1; 7364 threaded_name = bfd_alloc (abfd, len); 7365 if (threaded_name == NULL) 7366 return FALSE; 7367 memcpy (threaded_name, buf, len); 7368 7369 sect = bfd_make_section_anyway (abfd, threaded_name); 7370 if (sect == NULL) 7371 return FALSE; 7372 sect->size = size; 7373 sect->filepos = filepos; 7374 sect->flags = SEC_HAS_CONTENTS; 7375 sect->alignment_power = 2; 7376 7377 return elfcore_maybe_make_sect (abfd, name, sect); 7378 } 7379 7380 /* prstatus_t exists on: 7381 solaris 2.5+ 7382 linux 2.[01] + glibc 7383 unixware 4.2 7384 */ 7385 7386 #if defined (HAVE_PRSTATUS_T) 7387 7388 static bfd_boolean 7389 elfcore_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) 7390 { 7391 size_t size; 7392 int offset; 7393 7394 if (note->descsz == sizeof (prstatus_t)) 7395 { 7396 prstatus_t prstat; 7397 7398 size = sizeof (prstat.pr_reg); 7399 offset = offsetof (prstatus_t, pr_reg); 7400 memcpy (&prstat, note->descdata, sizeof (prstat)); 7401 7402 /* Do not overwrite the core signal if it 7403 has already been set by another thread. */ 7404 if (elf_tdata (abfd)->core_signal == 0) 7405 elf_tdata (abfd)->core_signal = prstat.pr_cursig; 7406 elf_tdata (abfd)->core_pid = prstat.pr_pid; 7407 7408 /* pr_who exists on: 7409 solaris 2.5+ 7410 unixware 4.2 7411 pr_who doesn't exist on: 7412 linux 2.[01] 7413 */ 7414 #if defined (HAVE_PRSTATUS_T_PR_WHO) 7415 elf_tdata (abfd)->core_lwpid = prstat.pr_who; 7416 #endif 7417 } 7418 #if defined (HAVE_PRSTATUS32_T) 7419 else if (note->descsz == sizeof (prstatus32_t)) 7420 { 7421 /* 64-bit host, 32-bit corefile */ 7422 prstatus32_t prstat; 7423 7424 size = sizeof (prstat.pr_reg); 7425 offset = offsetof (prstatus32_t, pr_reg); 7426 memcpy (&prstat, note->descdata, sizeof (prstat)); 7427 7428 /* Do not overwrite the core signal if it 7429 has already been set by another thread. */ 7430 if (elf_tdata (abfd)->core_signal == 0) 7431 elf_tdata (abfd)->core_signal = prstat.pr_cursig; 7432 elf_tdata (abfd)->core_pid = prstat.pr_pid; 7433 7434 /* pr_who exists on: 7435 solaris 2.5+ 7436 unixware 4.2 7437 pr_who doesn't exist on: 7438 linux 2.[01] 7439 */ 7440 #if defined (HAVE_PRSTATUS32_T_PR_WHO) 7441 elf_tdata (abfd)->core_lwpid = prstat.pr_who; 7442 #endif 7443 } 7444 #endif /* HAVE_PRSTATUS32_T */ 7445 else 7446 { 7447 /* Fail - we don't know how to handle any other 7448 note size (ie. data object type). */ 7449 return TRUE; 7450 } 7451 7452 /* Make a ".reg/999" section and a ".reg" section. */ 7453 return _bfd_elfcore_make_pseudosection (abfd, ".reg", 7454 size, note->descpos + offset); 7455 } 7456 #endif /* defined (HAVE_PRSTATUS_T) */ 7457 7458 /* Create a pseudosection containing the exact contents of NOTE. */ 7459 static bfd_boolean 7460 elfcore_make_note_pseudosection (bfd *abfd, 7461 char *name, 7462 Elf_Internal_Note *note) 7463 { 7464 return _bfd_elfcore_make_pseudosection (abfd, name, 7465 note->descsz, note->descpos); 7466 } 7467 7468 /* There isn't a consistent prfpregset_t across platforms, 7469 but it doesn't matter, because we don't have to pick this 7470 data structure apart. */ 7471 7472 static bfd_boolean 7473 elfcore_grok_prfpreg (bfd *abfd, Elf_Internal_Note *note) 7474 { 7475 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 7476 } 7477 7478 /* Linux dumps the Intel SSE regs in a note named "LINUX" with a note 7479 type of 5 (NT_PRXFPREG). Just include the whole note's contents 7480 literally. */ 7481 7482 static bfd_boolean 7483 elfcore_grok_prxfpreg (bfd *abfd, Elf_Internal_Note *note) 7484 { 7485 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); 7486 } 7487 7488 #if defined (HAVE_PRPSINFO_T) 7489 typedef prpsinfo_t elfcore_psinfo_t; 7490 #if defined (HAVE_PRPSINFO32_T) /* Sparc64 cross Sparc32 */ 7491 typedef prpsinfo32_t elfcore_psinfo32_t; 7492 #endif 7493 #endif 7494 7495 #if defined (HAVE_PSINFO_T) 7496 typedef psinfo_t elfcore_psinfo_t; 7497 #if defined (HAVE_PSINFO32_T) /* Sparc64 cross Sparc32 */ 7498 typedef psinfo32_t elfcore_psinfo32_t; 7499 #endif 7500 #endif 7501 7502 /* return a malloc'ed copy of a string at START which is at 7503 most MAX bytes long, possibly without a terminating '\0'. 7504 the copy will always have a terminating '\0'. */ 7505 7506 char * 7507 _bfd_elfcore_strndup (bfd *abfd, char *start, size_t max) 7508 { 7509 char *dups; 7510 char *end = memchr (start, '\0', max); 7511 size_t len; 7512 7513 if (end == NULL) 7514 len = max; 7515 else 7516 len = end - start; 7517 7518 dups = bfd_alloc (abfd, len + 1); 7519 if (dups == NULL) 7520 return NULL; 7521 7522 memcpy (dups, start, len); 7523 dups[len] = '\0'; 7524 7525 return dups; 7526 } 7527 7528 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 7529 static bfd_boolean 7530 elfcore_grok_psinfo (bfd *abfd, Elf_Internal_Note *note) 7531 { 7532 if (note->descsz == sizeof (elfcore_psinfo_t)) 7533 { 7534 elfcore_psinfo_t psinfo; 7535 7536 memcpy (&psinfo, note->descdata, sizeof (psinfo)); 7537 7538 elf_tdata (abfd)->core_program 7539 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, 7540 sizeof (psinfo.pr_fname)); 7541 7542 elf_tdata (abfd)->core_command 7543 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, 7544 sizeof (psinfo.pr_psargs)); 7545 } 7546 #if defined (HAVE_PRPSINFO32_T) || defined (HAVE_PSINFO32_T) 7547 else if (note->descsz == sizeof (elfcore_psinfo32_t)) 7548 { 7549 /* 64-bit host, 32-bit corefile */ 7550 elfcore_psinfo32_t psinfo; 7551 7552 memcpy (&psinfo, note->descdata, sizeof (psinfo)); 7553 7554 elf_tdata (abfd)->core_program 7555 = _bfd_elfcore_strndup (abfd, psinfo.pr_fname, 7556 sizeof (psinfo.pr_fname)); 7557 7558 elf_tdata (abfd)->core_command 7559 = _bfd_elfcore_strndup (abfd, psinfo.pr_psargs, 7560 sizeof (psinfo.pr_psargs)); 7561 } 7562 #endif 7563 7564 else 7565 { 7566 /* Fail - we don't know how to handle any other 7567 note size (ie. data object type). */ 7568 return TRUE; 7569 } 7570 7571 /* Note that for some reason, a spurious space is tacked 7572 onto the end of the args in some (at least one anyway) 7573 implementations, so strip it off if it exists. */ 7574 7575 { 7576 char *command = elf_tdata (abfd)->core_command; 7577 int n = strlen (command); 7578 7579 if (0 < n && command[n - 1] == ' ') 7580 command[n - 1] = '\0'; 7581 } 7582 7583 return TRUE; 7584 } 7585 #endif /* defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) */ 7586 7587 #if defined (HAVE_PSTATUS_T) 7588 static bfd_boolean 7589 elfcore_grok_pstatus (bfd *abfd, Elf_Internal_Note *note) 7590 { 7591 if (note->descsz == sizeof (pstatus_t) 7592 #if defined (HAVE_PXSTATUS_T) 7593 || note->descsz == sizeof (pxstatus_t) 7594 #endif 7595 ) 7596 { 7597 pstatus_t pstat; 7598 7599 memcpy (&pstat, note->descdata, sizeof (pstat)); 7600 7601 elf_tdata (abfd)->core_pid = pstat.pr_pid; 7602 } 7603 #if defined (HAVE_PSTATUS32_T) 7604 else if (note->descsz == sizeof (pstatus32_t)) 7605 { 7606 /* 64-bit host, 32-bit corefile */ 7607 pstatus32_t pstat; 7608 7609 memcpy (&pstat, note->descdata, sizeof (pstat)); 7610 7611 elf_tdata (abfd)->core_pid = pstat.pr_pid; 7612 } 7613 #endif 7614 /* Could grab some more details from the "representative" 7615 lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an 7616 NT_LWPSTATUS note, presumably. */ 7617 7618 return TRUE; 7619 } 7620 #endif /* defined (HAVE_PSTATUS_T) */ 7621 7622 #if defined (HAVE_LWPSTATUS_T) 7623 static bfd_boolean 7624 elfcore_grok_lwpstatus (bfd *abfd, Elf_Internal_Note *note) 7625 { 7626 lwpstatus_t lwpstat; 7627 char buf[100]; 7628 char *name; 7629 size_t len; 7630 asection *sect; 7631 7632 if (note->descsz != sizeof (lwpstat) 7633 #if defined (HAVE_LWPXSTATUS_T) 7634 && note->descsz != sizeof (lwpxstatus_t) 7635 #endif 7636 ) 7637 return TRUE; 7638 7639 memcpy (&lwpstat, note->descdata, sizeof (lwpstat)); 7640 7641 elf_tdata (abfd)->core_lwpid = lwpstat.pr_lwpid; 7642 elf_tdata (abfd)->core_signal = lwpstat.pr_cursig; 7643 7644 /* Make a ".reg/999" section. */ 7645 7646 sprintf (buf, ".reg/%d", elfcore_make_pid (abfd)); 7647 len = strlen (buf) + 1; 7648 name = bfd_alloc (abfd, len); 7649 if (name == NULL) 7650 return FALSE; 7651 memcpy (name, buf, len); 7652 7653 sect = bfd_make_section_anyway (abfd, name); 7654 if (sect == NULL) 7655 return FALSE; 7656 7657 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 7658 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.gregs); 7659 sect->filepos = note->descpos 7660 + offsetof (lwpstatus_t, pr_context.uc_mcontext.gregs); 7661 #endif 7662 7663 #if defined (HAVE_LWPSTATUS_T_PR_REG) 7664 sect->size = sizeof (lwpstat.pr_reg); 7665 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_reg); 7666 #endif 7667 7668 sect->flags = SEC_HAS_CONTENTS; 7669 sect->alignment_power = 2; 7670 7671 if (!elfcore_maybe_make_sect (abfd, ".reg", sect)) 7672 return FALSE; 7673 7674 /* Make a ".reg2/999" section */ 7675 7676 sprintf (buf, ".reg2/%d", elfcore_make_pid (abfd)); 7677 len = strlen (buf) + 1; 7678 name = bfd_alloc (abfd, len); 7679 if (name == NULL) 7680 return FALSE; 7681 memcpy (name, buf, len); 7682 7683 sect = bfd_make_section_anyway (abfd, name); 7684 if (sect == NULL) 7685 return FALSE; 7686 7687 #if defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 7688 sect->size = sizeof (lwpstat.pr_context.uc_mcontext.fpregs); 7689 sect->filepos = note->descpos 7690 + offsetof (lwpstatus_t, pr_context.uc_mcontext.fpregs); 7691 #endif 7692 7693 #if defined (HAVE_LWPSTATUS_T_PR_FPREG) 7694 sect->size = sizeof (lwpstat.pr_fpreg); 7695 sect->filepos = note->descpos + offsetof (lwpstatus_t, pr_fpreg); 7696 #endif 7697 7698 sect->flags = SEC_HAS_CONTENTS; 7699 sect->alignment_power = 2; 7700 7701 return elfcore_maybe_make_sect (abfd, ".reg2", sect); 7702 } 7703 #endif /* defined (HAVE_LWPSTATUS_T) */ 7704 7705 #if defined (HAVE_WIN32_PSTATUS_T) 7706 static bfd_boolean 7707 elfcore_grok_win32pstatus (bfd *abfd, Elf_Internal_Note *note) 7708 { 7709 char buf[30]; 7710 char *name; 7711 size_t len; 7712 asection *sect; 7713 win32_pstatus_t pstatus; 7714 7715 if (note->descsz < sizeof (pstatus)) 7716 return TRUE; 7717 7718 memcpy (&pstatus, note->descdata, sizeof (pstatus)); 7719 7720 switch (pstatus.data_type) 7721 { 7722 case NOTE_INFO_PROCESS: 7723 /* FIXME: need to add ->core_command. */ 7724 elf_tdata (abfd)->core_signal = pstatus.data.process_info.signal; 7725 elf_tdata (abfd)->core_pid = pstatus.data.process_info.pid; 7726 break; 7727 7728 case NOTE_INFO_THREAD: 7729 /* Make a ".reg/999" section. */ 7730 sprintf (buf, ".reg/%ld", (long) pstatus.data.thread_info.tid); 7731 7732 len = strlen (buf) + 1; 7733 name = bfd_alloc (abfd, len); 7734 if (name == NULL) 7735 return FALSE; 7736 7737 memcpy (name, buf, len); 7738 7739 sect = bfd_make_section_anyway (abfd, name); 7740 if (sect == NULL) 7741 return FALSE; 7742 7743 sect->size = sizeof (pstatus.data.thread_info.thread_context); 7744 sect->filepos = (note->descpos 7745 + offsetof (struct win32_pstatus, 7746 data.thread_info.thread_context)); 7747 sect->flags = SEC_HAS_CONTENTS; 7748 sect->alignment_power = 2; 7749 7750 if (pstatus.data.thread_info.is_active_thread) 7751 if (! elfcore_maybe_make_sect (abfd, ".reg", sect)) 7752 return FALSE; 7753 break; 7754 7755 case NOTE_INFO_MODULE: 7756 /* Make a ".module/xxxxxxxx" section. */ 7757 sprintf (buf, ".module/%08lx", 7758 (long) pstatus.data.module_info.base_address); 7759 7760 len = strlen (buf) + 1; 7761 name = bfd_alloc (abfd, len); 7762 if (name == NULL) 7763 return FALSE; 7764 7765 memcpy (name, buf, len); 7766 7767 sect = bfd_make_section_anyway (abfd, name); 7768 7769 if (sect == NULL) 7770 return FALSE; 7771 7772 sect->size = note->descsz; 7773 sect->filepos = note->descpos; 7774 sect->flags = SEC_HAS_CONTENTS; 7775 sect->alignment_power = 2; 7776 break; 7777 7778 default: 7779 return TRUE; 7780 } 7781 7782 return TRUE; 7783 } 7784 #endif /* HAVE_WIN32_PSTATUS_T */ 7785 7786 static bfd_boolean 7787 elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note) 7788 { 7789 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 7790 7791 switch (note->type) 7792 { 7793 default: 7794 return TRUE; 7795 7796 case NT_PRSTATUS: 7797 if (bed->elf_backend_grok_prstatus) 7798 if ((*bed->elf_backend_grok_prstatus) (abfd, note)) 7799 return TRUE; 7800 #if defined (HAVE_PRSTATUS_T) 7801 return elfcore_grok_prstatus (abfd, note); 7802 #else 7803 return TRUE; 7804 #endif 7805 7806 #if defined (HAVE_PSTATUS_T) 7807 case NT_PSTATUS: 7808 return elfcore_grok_pstatus (abfd, note); 7809 #endif 7810 7811 #if defined (HAVE_LWPSTATUS_T) 7812 case NT_LWPSTATUS: 7813 return elfcore_grok_lwpstatus (abfd, note); 7814 #endif 7815 7816 case NT_FPREGSET: /* FIXME: rename to NT_PRFPREG */ 7817 return elfcore_grok_prfpreg (abfd, note); 7818 7819 #if defined (HAVE_WIN32_PSTATUS_T) 7820 case NT_WIN32PSTATUS: 7821 return elfcore_grok_win32pstatus (abfd, note); 7822 #endif 7823 7824 case NT_PRXFPREG: /* Linux SSE extension */ 7825 if (note->namesz == 6 7826 && strcmp (note->namedata, "LINUX") == 0) 7827 return elfcore_grok_prxfpreg (abfd, note); 7828 else 7829 return TRUE; 7830 7831 case NT_PRPSINFO: 7832 case NT_PSINFO: 7833 if (bed->elf_backend_grok_psinfo) 7834 if ((*bed->elf_backend_grok_psinfo) (abfd, note)) 7835 return TRUE; 7836 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 7837 return elfcore_grok_psinfo (abfd, note); 7838 #else 7839 return TRUE; 7840 #endif 7841 7842 case NT_AUXV: 7843 { 7844 asection *sect = bfd_make_section_anyway (abfd, ".auxv"); 7845 7846 if (sect == NULL) 7847 return FALSE; 7848 sect->size = note->descsz; 7849 sect->filepos = note->descpos; 7850 sect->flags = SEC_HAS_CONTENTS; 7851 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 7852 7853 return TRUE; 7854 } 7855 } 7856 } 7857 7858 static bfd_boolean 7859 elfcore_netbsd_get_lwpid (Elf_Internal_Note *note, int *lwpidp) 7860 { 7861 char *cp; 7862 7863 cp = strchr (note->namedata, '@'); 7864 if (cp != NULL) 7865 { 7866 *lwpidp = atoi(cp + 1); 7867 return TRUE; 7868 } 7869 return FALSE; 7870 } 7871 7872 static bfd_boolean 7873 elfcore_grok_netbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) 7874 { 7875 7876 /* Signal number at offset 0x08. */ 7877 elf_tdata (abfd)->core_signal 7878 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); 7879 7880 /* Process ID at offset 0x50. */ 7881 elf_tdata (abfd)->core_pid 7882 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x50); 7883 7884 /* Command name at 0x7c (max 32 bytes, including nul). */ 7885 elf_tdata (abfd)->core_command 7886 = _bfd_elfcore_strndup (abfd, note->descdata + 0x7c, 31); 7887 7888 return elfcore_make_note_pseudosection (abfd, ".note.netbsdcore.procinfo", 7889 note); 7890 } 7891 7892 static bfd_boolean 7893 elfcore_grok_netbsd_note (bfd *abfd, Elf_Internal_Note *note) 7894 { 7895 int lwp; 7896 7897 if (elfcore_netbsd_get_lwpid (note, &lwp)) 7898 elf_tdata (abfd)->core_lwpid = lwp; 7899 7900 if (note->type == NT_NETBSDCORE_PROCINFO) 7901 { 7902 /* NetBSD-specific core "procinfo". Note that we expect to 7903 find this note before any of the others, which is fine, 7904 since the kernel writes this note out first when it 7905 creates a core file. */ 7906 7907 return elfcore_grok_netbsd_procinfo (abfd, note); 7908 } 7909 7910 /* As of Jan 2002 there are no other machine-independent notes 7911 defined for NetBSD core files. If the note type is less 7912 than the start of the machine-dependent note types, we don't 7913 understand it. */ 7914 7915 if (note->type < NT_NETBSDCORE_FIRSTMACH) 7916 return TRUE; 7917 7918 7919 switch (bfd_get_arch (abfd)) 7920 { 7921 /* On the Alpha, SPARC (32-bit and 64-bit), PT_GETREGS == mach+0 and 7922 PT_GETFPREGS == mach+2. */ 7923 7924 case bfd_arch_alpha: 7925 case bfd_arch_sparc: 7926 switch (note->type) 7927 { 7928 case NT_NETBSDCORE_FIRSTMACH+0: 7929 return elfcore_make_note_pseudosection (abfd, ".reg", note); 7930 7931 case NT_NETBSDCORE_FIRSTMACH+2: 7932 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 7933 7934 default: 7935 return TRUE; 7936 } 7937 7938 /* On all other arch's, PT_GETREGS == mach+1 and 7939 PT_GETFPREGS == mach+3. */ 7940 7941 default: 7942 switch (note->type) 7943 { 7944 case NT_NETBSDCORE_FIRSTMACH+1: 7945 return elfcore_make_note_pseudosection (abfd, ".reg", note); 7946 7947 case NT_NETBSDCORE_FIRSTMACH+3: 7948 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 7949 7950 default: 7951 return TRUE; 7952 } 7953 } 7954 /* NOTREACHED */ 7955 } 7956 7957 static bfd_boolean 7958 elfcore_grok_openbsd_procinfo (bfd *abfd, Elf_Internal_Note *note) 7959 { 7960 /* Signal number at offset 0x08. */ 7961 elf_tdata (abfd)->core_signal 7962 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x08); 7963 7964 /* Process ID at offset 0x20. */ 7965 elf_tdata (abfd)->core_pid 7966 = bfd_h_get_32 (abfd, (bfd_byte *) note->descdata + 0x20); 7967 7968 /* Command name at 0x48 (max 32 bytes, including nul). */ 7969 elf_tdata (abfd)->core_command 7970 = _bfd_elfcore_strndup (abfd, note->descdata + 0x48, 31); 7971 7972 return TRUE; 7973 } 7974 7975 static bfd_boolean 7976 elfcore_grok_openbsd_note (bfd *abfd, Elf_Internal_Note *note) 7977 { 7978 int lwp; 7979 7980 if (elfcore_netbsd_get_lwpid (note, &lwp)) 7981 elf_tdata (abfd)->core_lwpid = lwp; 7982 7983 if (note->type == NT_OPENBSD_PROCINFO) 7984 return elfcore_grok_openbsd_procinfo (abfd, note); 7985 7986 if (note->type == NT_OPENBSD_REGS) 7987 return elfcore_make_note_pseudosection (abfd, ".reg", note); 7988 7989 if (note->type == NT_OPENBSD_FPREGS) 7990 return elfcore_make_note_pseudosection (abfd, ".reg2", note); 7991 7992 if (note->type == NT_OPENBSD_XFPREGS) 7993 return elfcore_make_note_pseudosection (abfd, ".reg-xfp", note); 7994 7995 if (note->type == NT_OPENBSD_AUXV) 7996 { 7997 asection *sect = bfd_make_section_anyway (abfd, ".auxv"); 7998 7999 if (sect == NULL) 8000 return FALSE; 8001 sect->size = note->descsz; 8002 sect->filepos = note->descpos; 8003 sect->flags = SEC_HAS_CONTENTS; 8004 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 8005 8006 return TRUE; 8007 } 8008 8009 if (note->type == NT_OPENBSD_WCOOKIE) 8010 { 8011 asection *sect = bfd_make_section_anyway (abfd, ".wcookie"); 8012 8013 if (sect == NULL) 8014 return FALSE; 8015 sect->size = note->descsz; 8016 sect->filepos = note->descpos; 8017 sect->flags = SEC_HAS_CONTENTS; 8018 sect->alignment_power = 1 + bfd_get_arch_size (abfd) / 32; 8019 8020 return TRUE; 8021 } 8022 8023 return TRUE; 8024 } 8025 8026 static bfd_boolean 8027 elfcore_grok_nto_status (bfd *abfd, Elf_Internal_Note *note, pid_t *tid) 8028 { 8029 void *ddata = note->descdata; 8030 char buf[100]; 8031 char *name; 8032 asection *sect; 8033 short sig; 8034 unsigned flags; 8035 8036 /* nto_procfs_status 'pid' field is at offset 0. */ 8037 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, (bfd_byte *) ddata); 8038 8039 /* nto_procfs_status 'tid' field is at offset 4. Pass it back. */ 8040 *tid = bfd_get_32 (abfd, (bfd_byte *) ddata + 4); 8041 8042 /* nto_procfs_status 'flags' field is at offset 8. */ 8043 flags = bfd_get_32 (abfd, (bfd_byte *) ddata + 8); 8044 8045 /* nto_procfs_status 'what' field is at offset 14. */ 8046 if ((sig = bfd_get_16 (abfd, (bfd_byte *) ddata + 14)) > 0) 8047 { 8048 elf_tdata (abfd)->core_signal = sig; 8049 elf_tdata (abfd)->core_lwpid = *tid; 8050 } 8051 8052 /* _DEBUG_FLAG_CURTID (current thread) is 0x80. Some cores 8053 do not come from signals so we make sure we set the current 8054 thread just in case. */ 8055 if (flags & 0x00000080) 8056 elf_tdata (abfd)->core_lwpid = *tid; 8057 8058 /* Make a ".qnx_core_status/%d" section. */ 8059 sprintf (buf, ".qnx_core_status/%ld", (long) *tid); 8060 8061 name = bfd_alloc (abfd, strlen (buf) + 1); 8062 if (name == NULL) 8063 return FALSE; 8064 strcpy (name, buf); 8065 8066 sect = bfd_make_section_anyway (abfd, name); 8067 if (sect == NULL) 8068 return FALSE; 8069 8070 sect->size = note->descsz; 8071 sect->filepos = note->descpos; 8072 sect->flags = SEC_HAS_CONTENTS; 8073 sect->alignment_power = 2; 8074 8075 return (elfcore_maybe_make_sect (abfd, ".qnx_core_status", sect)); 8076 } 8077 8078 static bfd_boolean 8079 elfcore_grok_nto_regs (bfd *abfd, 8080 Elf_Internal_Note *note, 8081 pid_t tid, 8082 char *base) 8083 { 8084 char buf[100]; 8085 char *name; 8086 asection *sect; 8087 8088 /* Make a "(base)/%d" section. */ 8089 sprintf (buf, "%s/%ld", base, (long) tid); 8090 8091 name = bfd_alloc (abfd, strlen (buf) + 1); 8092 if (name == NULL) 8093 return FALSE; 8094 strcpy (name, buf); 8095 8096 sect = bfd_make_section_anyway (abfd, name); 8097 if (sect == NULL) 8098 return FALSE; 8099 8100 sect->size = note->descsz; 8101 sect->filepos = note->descpos; 8102 sect->flags = SEC_HAS_CONTENTS; 8103 sect->alignment_power = 2; 8104 8105 /* This is the current thread. */ 8106 if (elf_tdata (abfd)->core_lwpid == tid) 8107 return elfcore_maybe_make_sect (abfd, base, sect); 8108 8109 return TRUE; 8110 } 8111 8112 #define BFD_QNT_CORE_INFO 7 8113 #define BFD_QNT_CORE_STATUS 8 8114 #define BFD_QNT_CORE_GREG 9 8115 #define BFD_QNT_CORE_FPREG 10 8116 8117 static bfd_boolean 8118 elfcore_grok_nto_note (bfd *abfd, Elf_Internal_Note *note) 8119 { 8120 /* Every GREG section has a STATUS section before it. Store the 8121 tid from the previous call to pass down to the next gregs 8122 function. */ 8123 static pid_t tid = 1; 8124 8125 switch (note->type) 8126 { 8127 case BFD_QNT_CORE_INFO: 8128 return elfcore_make_note_pseudosection (abfd, ".qnx_core_info", note); 8129 case BFD_QNT_CORE_STATUS: 8130 return elfcore_grok_nto_status (abfd, note, &tid); 8131 case BFD_QNT_CORE_GREG: 8132 return elfcore_grok_nto_regs (abfd, note, tid, ".reg"); 8133 case BFD_QNT_CORE_FPREG: 8134 return elfcore_grok_nto_regs (abfd, note, tid, ".reg2"); 8135 default: 8136 return TRUE; 8137 } 8138 } 8139 8140 /* Function: elfcore_write_note 8141 8142 Inputs: 8143 buffer to hold note 8144 name of note 8145 type of note 8146 data for note 8147 size of data for note 8148 8149 Return: 8150 End of buffer containing note. */ 8151 8152 char * 8153 elfcore_write_note (bfd *abfd, 8154 char *buf, 8155 int *bufsiz, 8156 const char *name, 8157 int type, 8158 const void *input, 8159 int size) 8160 { 8161 Elf_External_Note *xnp; 8162 size_t namesz; 8163 size_t pad; 8164 size_t newspace; 8165 char *p, *dest; 8166 8167 namesz = 0; 8168 pad = 0; 8169 if (name != NULL) 8170 { 8171 const struct elf_backend_data *bed; 8172 8173 namesz = strlen (name) + 1; 8174 bed = get_elf_backend_data (abfd); 8175 pad = -namesz & ((1 << bed->s->log_file_align) - 1); 8176 } 8177 8178 newspace = 12 + namesz + pad + size; 8179 8180 p = realloc (buf, *bufsiz + newspace); 8181 dest = p + *bufsiz; 8182 *bufsiz += newspace; 8183 xnp = (Elf_External_Note *) dest; 8184 H_PUT_32 (abfd, namesz, xnp->namesz); 8185 H_PUT_32 (abfd, size, xnp->descsz); 8186 H_PUT_32 (abfd, type, xnp->type); 8187 dest = xnp->name; 8188 if (name != NULL) 8189 { 8190 memcpy (dest, name, namesz); 8191 dest += namesz; 8192 while (pad != 0) 8193 { 8194 *dest++ = '\0'; 8195 --pad; 8196 } 8197 } 8198 memcpy (dest, input, size); 8199 return p; 8200 } 8201 8202 #if defined (HAVE_PRPSINFO_T) || defined (HAVE_PSINFO_T) 8203 char * 8204 elfcore_write_prpsinfo (bfd *abfd, 8205 char *buf, 8206 int *bufsiz, 8207 const char *fname, 8208 const char *psargs) 8209 { 8210 int note_type; 8211 char *note_name = "CORE"; 8212 8213 #if defined (HAVE_PSINFO_T) 8214 psinfo_t data; 8215 note_type = NT_PSINFO; 8216 #else 8217 prpsinfo_t data; 8218 note_type = NT_PRPSINFO; 8219 #endif 8220 8221 memset (&data, 0, sizeof (data)); 8222 strncpy (data.pr_fname, fname, sizeof (data.pr_fname)); 8223 strncpy (data.pr_psargs, psargs, sizeof (data.pr_psargs)); 8224 return elfcore_write_note (abfd, buf, bufsiz, 8225 note_name, note_type, &data, sizeof (data)); 8226 } 8227 #endif /* PSINFO_T or PRPSINFO_T */ 8228 8229 #if defined (HAVE_PRSTATUS_T) 8230 char * 8231 elfcore_write_prstatus (bfd *abfd, 8232 char *buf, 8233 int *bufsiz, 8234 long pid, 8235 int cursig, 8236 const void *gregs) 8237 { 8238 prstatus_t prstat; 8239 char *note_name = "CORE"; 8240 8241 memset (&prstat, 0, sizeof (prstat)); 8242 prstat.pr_pid = pid; 8243 prstat.pr_cursig = cursig; 8244 memcpy (&prstat.pr_reg, gregs, sizeof (prstat.pr_reg)); 8245 return elfcore_write_note (abfd, buf, bufsiz, 8246 note_name, NT_PRSTATUS, &prstat, sizeof (prstat)); 8247 } 8248 #endif /* HAVE_PRSTATUS_T */ 8249 8250 #if defined (HAVE_LWPSTATUS_T) 8251 char * 8252 elfcore_write_lwpstatus (bfd *abfd, 8253 char *buf, 8254 int *bufsiz, 8255 long pid, 8256 int cursig, 8257 const void *gregs) 8258 { 8259 lwpstatus_t lwpstat; 8260 char *note_name = "CORE"; 8261 8262 memset (&lwpstat, 0, sizeof (lwpstat)); 8263 lwpstat.pr_lwpid = pid >> 16; 8264 lwpstat.pr_cursig = cursig; 8265 #if defined (HAVE_LWPSTATUS_T_PR_REG) 8266 memcpy (lwpstat.pr_reg, gregs, sizeof (lwpstat.pr_reg)); 8267 #elif defined (HAVE_LWPSTATUS_T_PR_CONTEXT) 8268 #if !defined(gregs) 8269 memcpy (lwpstat.pr_context.uc_mcontext.gregs, 8270 gregs, sizeof (lwpstat.pr_context.uc_mcontext.gregs)); 8271 #else 8272 memcpy (lwpstat.pr_context.uc_mcontext.__gregs, 8273 gregs, sizeof (lwpstat.pr_context.uc_mcontext.__gregs)); 8274 #endif 8275 #endif 8276 return elfcore_write_note (abfd, buf, bufsiz, note_name, 8277 NT_LWPSTATUS, &lwpstat, sizeof (lwpstat)); 8278 } 8279 #endif /* HAVE_LWPSTATUS_T */ 8280 8281 #if defined (HAVE_PSTATUS_T) 8282 char * 8283 elfcore_write_pstatus (bfd *abfd, 8284 char *buf, 8285 int *bufsiz, 8286 long pid, 8287 int cursig ATTRIBUTE_UNUSED, 8288 const void *gregs ATTRIBUTE_UNUSED) 8289 { 8290 pstatus_t pstat; 8291 char *note_name = "CORE"; 8292 8293 memset (&pstat, 0, sizeof (pstat)); 8294 pstat.pr_pid = pid & 0xffff; 8295 buf = elfcore_write_note (abfd, buf, bufsiz, note_name, 8296 NT_PSTATUS, &pstat, sizeof (pstat)); 8297 return buf; 8298 } 8299 #endif /* HAVE_PSTATUS_T */ 8300 8301 char * 8302 elfcore_write_prfpreg (bfd *abfd, 8303 char *buf, 8304 int *bufsiz, 8305 const void *fpregs, 8306 int size) 8307 { 8308 char *note_name = "CORE"; 8309 return elfcore_write_note (abfd, buf, bufsiz, 8310 note_name, NT_FPREGSET, fpregs, size); 8311 } 8312 8313 char * 8314 elfcore_write_prxfpreg (bfd *abfd, 8315 char *buf, 8316 int *bufsiz, 8317 const void *xfpregs, 8318 int size) 8319 { 8320 char *note_name = "LINUX"; 8321 return elfcore_write_note (abfd, buf, bufsiz, 8322 note_name, NT_PRXFPREG, xfpregs, size); 8323 } 8324 8325 static bfd_boolean 8326 elfcore_read_notes (bfd *abfd, file_ptr offset, bfd_size_type size) 8327 { 8328 char *buf; 8329 char *p; 8330 8331 if (size <= 0) 8332 return TRUE; 8333 8334 if (bfd_seek (abfd, offset, SEEK_SET) != 0) 8335 return FALSE; 8336 8337 buf = bfd_malloc (size); 8338 if (buf == NULL) 8339 return FALSE; 8340 8341 if (bfd_bread (buf, size, abfd) != size) 8342 { 8343 error: 8344 free (buf); 8345 return FALSE; 8346 } 8347 8348 p = buf; 8349 while (p < buf + size) 8350 { 8351 /* FIXME: bad alignment assumption. */ 8352 Elf_External_Note *xnp = (Elf_External_Note *) p; 8353 Elf_Internal_Note in; 8354 8355 in.type = H_GET_32 (abfd, xnp->type); 8356 8357 in.namesz = H_GET_32 (abfd, xnp->namesz); 8358 in.namedata = xnp->name; 8359 8360 in.descsz = H_GET_32 (abfd, xnp->descsz); 8361 in.descdata = in.namedata + BFD_ALIGN (in.namesz, 4); 8362 in.descpos = offset + (in.descdata - buf); 8363 8364 if (strncmp (in.namedata, "NetBSD-CORE", 11) == 0) 8365 { 8366 if (! elfcore_grok_netbsd_note (abfd, &in)) 8367 goto error; 8368 } 8369 else if (strncmp (in.namedata, "OpenBSD", 7) == 0) 8370 { 8371 if (! elfcore_grok_openbsd_note (abfd, &in)) 8372 goto error; 8373 } 8374 else if (strncmp (in.namedata, "QNX", 3) == 0) 8375 { 8376 if (! elfcore_grok_nto_note (abfd, &in)) 8377 goto error; 8378 } 8379 else 8380 { 8381 if (! elfcore_grok_note (abfd, &in)) 8382 goto error; 8383 } 8384 8385 p = in.descdata + BFD_ALIGN (in.descsz, 4); 8386 } 8387 8388 free (buf); 8389 return TRUE; 8390 } 8391 8392 /* Providing external access to the ELF program header table. */ 8393 8394 /* Return an upper bound on the number of bytes required to store a 8395 copy of ABFD's program header table entries. Return -1 if an error 8396 occurs; bfd_get_error will return an appropriate code. */ 8397 8398 long 8399 bfd_get_elf_phdr_upper_bound (bfd *abfd) 8400 { 8401 if (abfd->xvec->flavour != bfd_target_elf_flavour) 8402 { 8403 bfd_set_error (bfd_error_wrong_format); 8404 return -1; 8405 } 8406 8407 return elf_elfheader (abfd)->e_phnum * sizeof (Elf_Internal_Phdr); 8408 } 8409 8410 /* Copy ABFD's program header table entries to *PHDRS. The entries 8411 will be stored as an array of Elf_Internal_Phdr structures, as 8412 defined in include/elf/internal.h. To find out how large the 8413 buffer needs to be, call bfd_get_elf_phdr_upper_bound. 8414 8415 Return the number of program header table entries read, or -1 if an 8416 error occurs; bfd_get_error will return an appropriate code. */ 8417 8418 int 8419 bfd_get_elf_phdrs (bfd *abfd, void *phdrs) 8420 { 8421 int num_phdrs; 8422 8423 if (abfd->xvec->flavour != bfd_target_elf_flavour) 8424 { 8425 bfd_set_error (bfd_error_wrong_format); 8426 return -1; 8427 } 8428 8429 num_phdrs = elf_elfheader (abfd)->e_phnum; 8430 memcpy (phdrs, elf_tdata (abfd)->phdr, 8431 num_phdrs * sizeof (Elf_Internal_Phdr)); 8432 8433 return num_phdrs; 8434 } 8435 8436 void 8437 _bfd_elf_sprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, char *buf, bfd_vma value) 8438 { 8439 #ifdef BFD64 8440 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ 8441 8442 i_ehdrp = elf_elfheader (abfd); 8443 if (i_ehdrp == NULL) 8444 sprintf_vma (buf, value); 8445 else 8446 { 8447 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64) 8448 { 8449 #if BFD_HOST_64BIT_LONG 8450 sprintf (buf, "%016lx", value); 8451 #else 8452 sprintf (buf, "%08lx%08lx", _bfd_int64_high (value), 8453 _bfd_int64_low (value)); 8454 #endif 8455 } 8456 else 8457 sprintf (buf, "%08lx", (unsigned long) (value & 0xffffffff)); 8458 } 8459 #else 8460 sprintf_vma (buf, value); 8461 #endif 8462 } 8463 8464 void 8465 _bfd_elf_fprintf_vma (bfd *abfd ATTRIBUTE_UNUSED, void *stream, bfd_vma value) 8466 { 8467 #ifdef BFD64 8468 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ 8469 8470 i_ehdrp = elf_elfheader (abfd); 8471 if (i_ehdrp == NULL) 8472 fprintf_vma ((FILE *) stream, value); 8473 else 8474 { 8475 if (i_ehdrp->e_ident[EI_CLASS] == ELFCLASS64) 8476 { 8477 #if BFD_HOST_64BIT_LONG 8478 fprintf ((FILE *) stream, "%016lx", value); 8479 #else 8480 fprintf ((FILE *) stream, "%08lx%08lx", 8481 _bfd_int64_high (value), _bfd_int64_low (value)); 8482 #endif 8483 } 8484 else 8485 fprintf ((FILE *) stream, "%08lx", 8486 (unsigned long) (value & 0xffffffff)); 8487 } 8488 #else 8489 fprintf_vma ((FILE *) stream, value); 8490 #endif 8491 } 8492 8493 enum elf_reloc_type_class 8494 _bfd_elf_reloc_type_class (const Elf_Internal_Rela *rela ATTRIBUTE_UNUSED) 8495 { 8496 return reloc_class_normal; 8497 } 8498 8499 /* For RELA architectures, return the relocation value for a 8500 relocation against a local symbol. */ 8501 8502 bfd_vma 8503 _bfd_elf_rela_local_sym (bfd *abfd, 8504 Elf_Internal_Sym *sym, 8505 asection **psec, 8506 Elf_Internal_Rela *rel) 8507 { 8508 asection *sec = *psec; 8509 bfd_vma relocation; 8510 8511 relocation = (sec->output_section->vma 8512 + sec->output_offset 8513 + sym->st_value); 8514 if ((sec->flags & SEC_MERGE) 8515 && ELF_ST_TYPE (sym->st_info) == STT_SECTION 8516 && sec->sec_info_type == ELF_INFO_TYPE_MERGE) 8517 { 8518 rel->r_addend = 8519 _bfd_merged_section_offset (abfd, psec, 8520 elf_section_data (sec)->sec_info, 8521 sym->st_value + rel->r_addend); 8522 if (sec != *psec) 8523 { 8524 /* If we have changed the section, and our original section is 8525 marked with SEC_EXCLUDE, it means that the original 8526 SEC_MERGE section has been completely subsumed in some 8527 other SEC_MERGE section. In this case, we need to leave 8528 some info around for --emit-relocs. */ 8529 if ((sec->flags & SEC_EXCLUDE) != 0) 8530 sec->kept_section = *psec; 8531 sec = *psec; 8532 } 8533 rel->r_addend -= relocation; 8534 rel->r_addend += sec->output_section->vma + sec->output_offset; 8535 } 8536 return relocation; 8537 } 8538 8539 bfd_vma 8540 _bfd_elf_rel_local_sym (bfd *abfd, 8541 Elf_Internal_Sym *sym, 8542 asection **psec, 8543 bfd_vma addend) 8544 { 8545 asection *sec = *psec; 8546 8547 if (sec->sec_info_type != ELF_INFO_TYPE_MERGE) 8548 return sym->st_value + addend; 8549 8550 return _bfd_merged_section_offset (abfd, psec, 8551 elf_section_data (sec)->sec_info, 8552 sym->st_value + addend); 8553 } 8554 8555 bfd_vma 8556 _bfd_elf_section_offset (bfd *abfd, 8557 struct bfd_link_info *info, 8558 asection *sec, 8559 bfd_vma offset) 8560 { 8561 switch (sec->sec_info_type) 8562 { 8563 case ELF_INFO_TYPE_STABS: 8564 return _bfd_stab_section_offset (sec, elf_section_data (sec)->sec_info, 8565 offset); 8566 case ELF_INFO_TYPE_EH_FRAME: 8567 return _bfd_elf_eh_frame_section_offset (abfd, info, sec, offset); 8568 default: 8569 return offset; 8570 } 8571 } 8572 8573 /* Create a new BFD as if by bfd_openr. Rather than opening a file, 8574 reconstruct an ELF file by reading the segments out of remote memory 8575 based on the ELF file header at EHDR_VMA and the ELF program headers it 8576 points to. If not null, *LOADBASEP is filled in with the difference 8577 between the VMAs from which the segments were read, and the VMAs the 8578 file headers (and hence BFD's idea of each section's VMA) put them at. 8579 8580 The function TARGET_READ_MEMORY is called to copy LEN bytes from the 8581 remote memory at target address VMA into the local buffer at MYADDR; it 8582 should return zero on success or an `errno' code on failure. TEMPL must 8583 be a BFD for an ELF target with the word size and byte order found in 8584 the remote memory. */ 8585 8586 bfd * 8587 bfd_elf_bfd_from_remote_memory 8588 (bfd *templ, 8589 bfd_vma ehdr_vma, 8590 bfd_vma *loadbasep, 8591 int (*target_read_memory) (bfd_vma, bfd_byte *, int)) 8592 { 8593 return (*get_elf_backend_data (templ)->elf_backend_bfd_from_remote_memory) 8594 (templ, ehdr_vma, loadbasep, target_read_memory); 8595 } 8596 8597 long 8598 _bfd_elf_get_synthetic_symtab (bfd *abfd, 8599 long symcount ATTRIBUTE_UNUSED, 8600 asymbol **syms ATTRIBUTE_UNUSED, 8601 long dynsymcount, 8602 asymbol **dynsyms, 8603 asymbol **ret) 8604 { 8605 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8606 asection *relplt; 8607 asymbol *s; 8608 const char *relplt_name; 8609 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean); 8610 arelent *p; 8611 long count, i, n; 8612 size_t size; 8613 Elf_Internal_Shdr *hdr; 8614 char *names; 8615 asection *plt; 8616 8617 *ret = NULL; 8618 8619 if ((abfd->flags & (DYNAMIC | EXEC_P)) == 0) 8620 return 0; 8621 8622 if (dynsymcount <= 0) 8623 return 0; 8624 8625 if (!bed->plt_sym_val) 8626 return 0; 8627 8628 relplt_name = bed->relplt_name; 8629 if (relplt_name == NULL) 8630 relplt_name = bed->default_use_rela_p ? ".rela.plt" : ".rel.plt"; 8631 relplt = bfd_get_section_by_name (abfd, relplt_name); 8632 if (relplt == NULL) 8633 return 0; 8634 8635 hdr = &elf_section_data (relplt)->this_hdr; 8636 if (hdr->sh_link != elf_dynsymtab (abfd) 8637 || (hdr->sh_type != SHT_REL && hdr->sh_type != SHT_RELA)) 8638 return 0; 8639 8640 plt = bfd_get_section_by_name (abfd, ".plt"); 8641 if (plt == NULL) 8642 return 0; 8643 8644 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; 8645 if (! (*slurp_relocs) (abfd, relplt, dynsyms, TRUE)) 8646 return -1; 8647 8648 count = relplt->size / hdr->sh_entsize; 8649 size = count * sizeof (asymbol); 8650 p = relplt->relocation; 8651 for (i = 0; i < count; i++, p++) 8652 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt"); 8653 8654 s = *ret = bfd_malloc (size); 8655 if (s == NULL) 8656 return -1; 8657 8658 names = (char *) (s + count); 8659 p = relplt->relocation; 8660 n = 0; 8661 for (i = 0; i < count; i++, s++, p++) 8662 { 8663 size_t len; 8664 bfd_vma addr; 8665 8666 addr = bed->plt_sym_val (i, plt, p); 8667 if (addr == (bfd_vma) -1) 8668 continue; 8669 8670 *s = **p->sym_ptr_ptr; 8671 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since 8672 we are defining a symbol, ensure one of them is set. */ 8673 if ((s->flags & BSF_LOCAL) == 0) 8674 s->flags |= BSF_GLOBAL; 8675 s->section = plt; 8676 s->value = addr - plt->vma; 8677 s->name = names; 8678 len = strlen ((*p->sym_ptr_ptr)->name); 8679 memcpy (names, (*p->sym_ptr_ptr)->name, len); 8680 names += len; 8681 memcpy (names, "@plt", sizeof ("@plt")); 8682 names += sizeof ("@plt"); 8683 ++n; 8684 } 8685 8686 return n; 8687 } 8688 8689 /* Sort symbol by binding and section. We want to put definitions 8690 sorted by section at the beginning. */ 8691 8692 static int 8693 elf_sort_elf_symbol (const void *arg1, const void *arg2) 8694 { 8695 const Elf_Internal_Sym *s1; 8696 const Elf_Internal_Sym *s2; 8697 int shndx; 8698 8699 /* Make sure that undefined symbols are at the end. */ 8700 s1 = (const Elf_Internal_Sym *) arg1; 8701 if (s1->st_shndx == SHN_UNDEF) 8702 return 1; 8703 s2 = (const Elf_Internal_Sym *) arg2; 8704 if (s2->st_shndx == SHN_UNDEF) 8705 return -1; 8706 8707 /* Sorted by section index. */ 8708 shndx = s1->st_shndx - s2->st_shndx; 8709 if (shndx != 0) 8710 return shndx; 8711 8712 /* Sorted by binding. */ 8713 return ELF_ST_BIND (s1->st_info) - ELF_ST_BIND (s2->st_info); 8714 } 8715 8716 struct elf_symbol 8717 { 8718 Elf_Internal_Sym *sym; 8719 const char *name; 8720 }; 8721 8722 static int 8723 elf_sym_name_compare (const void *arg1, const void *arg2) 8724 { 8725 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; 8726 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; 8727 return strcmp (s1->name, s2->name); 8728 } 8729 8730 /* Check if 2 sections define the same set of local and global 8731 symbols. */ 8732 8733 bfd_boolean 8734 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, 8735 struct bfd_link_info *info) 8736 { 8737 bfd *bfd1, *bfd2; 8738 const struct elf_backend_data *bed1, *bed2; 8739 Elf_Internal_Shdr *hdr1, *hdr2; 8740 bfd_size_type symcount1, symcount2; 8741 Elf_Internal_Sym *isymbuf1, *isymbuf2; 8742 Elf_Internal_Sym *isymstart1 = NULL, *isymstart2 = NULL, *isym; 8743 Elf_Internal_Sym *isymend; 8744 struct elf_symbol *symp, *symtable1 = NULL, *symtable2 = NULL; 8745 bfd_size_type count1, count2, i; 8746 int shndx1, shndx2; 8747 bfd_boolean result; 8748 8749 bfd1 = sec1->owner; 8750 bfd2 = sec2->owner; 8751 8752 /* If both are .gnu.linkonce sections, they have to have the same 8753 section name. */ 8754 if (strncmp (sec1->name, ".gnu.linkonce", 8755 sizeof ".gnu.linkonce" - 1) == 0 8756 && strncmp (sec2->name, ".gnu.linkonce", 8757 sizeof ".gnu.linkonce" - 1) == 0) 8758 return strcmp (sec1->name + sizeof ".gnu.linkonce", 8759 sec2->name + sizeof ".gnu.linkonce") == 0; 8760 8761 /* Both sections have to be in ELF. */ 8762 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour 8763 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) 8764 return FALSE; 8765 8766 if (elf_section_type (sec1) != elf_section_type (sec2)) 8767 return FALSE; 8768 8769 if ((elf_section_flags (sec1) & SHF_GROUP) != 0 8770 && (elf_section_flags (sec2) & SHF_GROUP) != 0) 8771 { 8772 /* If both are members of section groups, they have to have the 8773 same group name. */ 8774 if (strcmp (elf_group_name (sec1), elf_group_name (sec2)) != 0) 8775 return FALSE; 8776 } 8777 8778 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); 8779 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); 8780 if (shndx1 == -1 || shndx2 == -1) 8781 return FALSE; 8782 8783 bed1 = get_elf_backend_data (bfd1); 8784 bed2 = get_elf_backend_data (bfd2); 8785 hdr1 = &elf_tdata (bfd1)->symtab_hdr; 8786 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; 8787 hdr2 = &elf_tdata (bfd2)->symtab_hdr; 8788 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; 8789 8790 if (symcount1 == 0 || symcount2 == 0) 8791 return FALSE; 8792 8793 result = FALSE; 8794 isymbuf1 = elf_tdata (bfd1)->symbuf; 8795 isymbuf2 = elf_tdata (bfd2)->symbuf; 8796 8797 if (isymbuf1 == NULL) 8798 { 8799 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, 8800 NULL, NULL, NULL); 8801 if (isymbuf1 == NULL) 8802 goto done; 8803 /* Sort symbols by binding and section. Global definitions are at 8804 the beginning. */ 8805 qsort (isymbuf1, symcount1, sizeof (Elf_Internal_Sym), 8806 elf_sort_elf_symbol); 8807 if (!info->reduce_memory_overheads) 8808 elf_tdata (bfd1)->symbuf = isymbuf1; 8809 } 8810 8811 if (isymbuf2 == NULL) 8812 { 8813 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, 8814 NULL, NULL, NULL); 8815 if (isymbuf2 == NULL) 8816 goto done; 8817 /* Sort symbols by binding and section. Global definitions are at 8818 the beginning. */ 8819 qsort (isymbuf2, symcount2, sizeof (Elf_Internal_Sym), 8820 elf_sort_elf_symbol); 8821 if (!info->reduce_memory_overheads) 8822 elf_tdata (bfd2)->symbuf = isymbuf2; 8823 } 8824 8825 /* Count definitions in the section. */ 8826 count1 = 0; 8827 for (isym = isymbuf1, isymend = isym + symcount1; 8828 isym < isymend; isym++) 8829 { 8830 if (isym->st_shndx == (unsigned int) shndx1) 8831 { 8832 if (count1 == 0) 8833 isymstart1 = isym; 8834 count1++; 8835 } 8836 8837 if (count1 && isym->st_shndx != (unsigned int) shndx1) 8838 break; 8839 } 8840 8841 count2 = 0; 8842 for (isym = isymbuf2, isymend = isym + symcount2; 8843 isym < isymend; isym++) 8844 { 8845 if (isym->st_shndx == (unsigned int) shndx2) 8846 { 8847 if (count2 == 0) 8848 isymstart2 = isym; 8849 count2++; 8850 } 8851 8852 if (count2 && isym->st_shndx != (unsigned int) shndx2) 8853 break; 8854 } 8855 8856 if (count1 == 0 || count2 == 0 || count1 != count2) 8857 goto done; 8858 8859 symtable1 = bfd_malloc (count1 * sizeof (struct elf_symbol)); 8860 symtable2 = bfd_malloc (count1 * sizeof (struct elf_symbol)); 8861 8862 if (symtable1 == NULL || symtable2 == NULL) 8863 goto done; 8864 8865 symp = symtable1; 8866 for (isym = isymstart1, isymend = isym + count1; 8867 isym < isymend; isym++) 8868 { 8869 symp->sym = isym; 8870 symp->name = bfd_elf_string_from_elf_section (bfd1, 8871 hdr1->sh_link, 8872 isym->st_name); 8873 symp++; 8874 } 8875 8876 symp = symtable2; 8877 for (isym = isymstart2, isymend = isym + count1; 8878 isym < isymend; isym++) 8879 { 8880 symp->sym = isym; 8881 symp->name = bfd_elf_string_from_elf_section (bfd2, 8882 hdr2->sh_link, 8883 isym->st_name); 8884 symp++; 8885 } 8886 8887 /* Sort symbol by name. */ 8888 qsort (symtable1, count1, sizeof (struct elf_symbol), 8889 elf_sym_name_compare); 8890 qsort (symtable2, count1, sizeof (struct elf_symbol), 8891 elf_sym_name_compare); 8892 8893 for (i = 0; i < count1; i++) 8894 /* Two symbols must have the same binding, type and name. */ 8895 if (symtable1 [i].sym->st_info != symtable2 [i].sym->st_info 8896 || symtable1 [i].sym->st_other != symtable2 [i].sym->st_other 8897 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) 8898 goto done; 8899 8900 result = TRUE; 8901 8902 done: 8903 if (symtable1) 8904 free (symtable1); 8905 if (symtable2) 8906 free (symtable2); 8907 if (info->reduce_memory_overheads) 8908 { 8909 if (isymbuf1) 8910 free (isymbuf1); 8911 if (isymbuf2) 8912 free (isymbuf2); 8913 } 8914 8915 return result; 8916 } 8917 8918 /* It is only used by x86-64 so far. */ 8919 asection _bfd_elf_large_com_section 8920 = BFD_FAKE_SECTION (_bfd_elf_large_com_section, 8921 SEC_IS_COMMON, NULL, NULL, "LARGE_COMMON", 8922 0); 8923 8924 /* Return TRUE if 2 section types are compatible. */ 8925 8926 bfd_boolean 8927 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, 8928 bfd *bbfd, const asection *bsec) 8929 { 8930 if (asec == NULL 8931 || bsec == NULL 8932 || abfd->xvec->flavour != bfd_target_elf_flavour 8933 || bbfd->xvec->flavour != bfd_target_elf_flavour) 8934 return TRUE; 8935 8936 return elf_section_type (asec) == elf_section_type (bsec); 8937 } 8938