elf.c revision 59342
1/* ELF executable support for BFD. 2 Copyright 1993, 94, 95, 96, 97, 1998 Free Software Foundation, Inc. 3 4This file is part of BFD, the Binary File Descriptor library. 5 6This program is free software; you can redistribute it and/or modify 7it under the terms of the GNU General Public License as published by 8the Free Software Foundation; either version 2 of the License, or 9(at your option) any later version. 10 11This program is distributed in the hope that it will be useful, 12but WITHOUT ANY WARRANTY; without even the implied warranty of 13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14GNU General Public License for more details. 15 16You should have received a copy of the GNU General Public License 17along with this program; if not, write to the Free Software 18Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 19 20/* $FreeBSD: head/contrib/binutils/bfd/elf.c 59342 2000-04-18 02:39:26Z obrien $ */ 21 22/* 23 24SECTION 25 ELF backends 26 27 BFD support for ELF formats is being worked on. 28 Currently, the best supported back ends are for sparc and i386 29 (running svr4 or Solaris 2). 30 31 Documentation of the internals of the support code still needs 32 to be written. The code is changing quickly enough that we 33 haven't bothered yet. 34 */ 35 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 43static INLINE struct elf_segment_map *make_mapping 44 PARAMS ((bfd *, asection **, unsigned int, unsigned int, boolean)); 45static boolean map_sections_to_segments PARAMS ((bfd *)); 46static int elf_sort_sections PARAMS ((const PTR, const PTR)); 47static boolean assign_file_positions_for_segments PARAMS ((bfd *)); 48static boolean assign_file_positions_except_relocs PARAMS ((bfd *)); 49static boolean prep_headers PARAMS ((bfd *)); 50static boolean swap_out_syms PARAMS ((bfd *, struct bfd_strtab_hash **)); 51static boolean copy_private_bfd_data PARAMS ((bfd *, bfd *)); 52static char *elf_read PARAMS ((bfd *, long, unsigned int)); 53static void elf_fake_sections PARAMS ((bfd *, asection *, PTR)); 54static boolean assign_section_numbers PARAMS ((bfd *)); 55static INLINE int sym_is_global PARAMS ((bfd *, asymbol *)); 56static boolean elf_map_symbols PARAMS ((bfd *)); 57static bfd_size_type get_program_header_size PARAMS ((bfd *)); 58 59/* Swap version information in and out. The version information is 60 currently size independent. If that ever changes, this code will 61 need to move into elfcode.h. */ 62 63/* Swap in a Verdef structure. */ 64 65void 66_bfd_elf_swap_verdef_in (abfd, src, dst) 67 bfd *abfd; 68 const Elf_External_Verdef *src; 69 Elf_Internal_Verdef *dst; 70{ 71 dst->vd_version = bfd_h_get_16 (abfd, src->vd_version); 72 dst->vd_flags = bfd_h_get_16 (abfd, src->vd_flags); 73 dst->vd_ndx = bfd_h_get_16 (abfd, src->vd_ndx); 74 dst->vd_cnt = bfd_h_get_16 (abfd, src->vd_cnt); 75 dst->vd_hash = bfd_h_get_32 (abfd, src->vd_hash); 76 dst->vd_aux = bfd_h_get_32 (abfd, src->vd_aux); 77 dst->vd_next = bfd_h_get_32 (abfd, src->vd_next); 78} 79 80/* Swap out a Verdef structure. */ 81 82void 83_bfd_elf_swap_verdef_out (abfd, src, dst) 84 bfd *abfd; 85 const Elf_Internal_Verdef *src; 86 Elf_External_Verdef *dst; 87{ 88 bfd_h_put_16 (abfd, src->vd_version, dst->vd_version); 89 bfd_h_put_16 (abfd, src->vd_flags, dst->vd_flags); 90 bfd_h_put_16 (abfd, src->vd_ndx, dst->vd_ndx); 91 bfd_h_put_16 (abfd, src->vd_cnt, dst->vd_cnt); 92 bfd_h_put_32 (abfd, src->vd_hash, dst->vd_hash); 93 bfd_h_put_32 (abfd, src->vd_aux, dst->vd_aux); 94 bfd_h_put_32 (abfd, src->vd_next, dst->vd_next); 95} 96 97/* Swap in a Verdaux structure. */ 98 99void 100_bfd_elf_swap_verdaux_in (abfd, src, dst) 101 bfd *abfd; 102 const Elf_External_Verdaux *src; 103 Elf_Internal_Verdaux *dst; 104{ 105 dst->vda_name = bfd_h_get_32 (abfd, src->vda_name); 106 dst->vda_next = bfd_h_get_32 (abfd, src->vda_next); 107} 108 109/* Swap out a Verdaux structure. */ 110 111void 112_bfd_elf_swap_verdaux_out (abfd, src, dst) 113 bfd *abfd; 114 const Elf_Internal_Verdaux *src; 115 Elf_External_Verdaux *dst; 116{ 117 bfd_h_put_32 (abfd, src->vda_name, dst->vda_name); 118 bfd_h_put_32 (abfd, src->vda_next, dst->vda_next); 119} 120 121/* Swap in a Verneed structure. */ 122 123void 124_bfd_elf_swap_verneed_in (abfd, src, dst) 125 bfd *abfd; 126 const Elf_External_Verneed *src; 127 Elf_Internal_Verneed *dst; 128{ 129 dst->vn_version = bfd_h_get_16 (abfd, src->vn_version); 130 dst->vn_cnt = bfd_h_get_16 (abfd, src->vn_cnt); 131 dst->vn_file = bfd_h_get_32 (abfd, src->vn_file); 132 dst->vn_aux = bfd_h_get_32 (abfd, src->vn_aux); 133 dst->vn_next = bfd_h_get_32 (abfd, src->vn_next); 134} 135 136/* Swap out a Verneed structure. */ 137 138void 139_bfd_elf_swap_verneed_out (abfd, src, dst) 140 bfd *abfd; 141 const Elf_Internal_Verneed *src; 142 Elf_External_Verneed *dst; 143{ 144 bfd_h_put_16 (abfd, src->vn_version, dst->vn_version); 145 bfd_h_put_16 (abfd, src->vn_cnt, dst->vn_cnt); 146 bfd_h_put_32 (abfd, src->vn_file, dst->vn_file); 147 bfd_h_put_32 (abfd, src->vn_aux, dst->vn_aux); 148 bfd_h_put_32 (abfd, src->vn_next, dst->vn_next); 149} 150 151/* Swap in a Vernaux structure. */ 152 153void 154_bfd_elf_swap_vernaux_in (abfd, src, dst) 155 bfd *abfd; 156 const Elf_External_Vernaux *src; 157 Elf_Internal_Vernaux *dst; 158{ 159 dst->vna_hash = bfd_h_get_32 (abfd, src->vna_hash); 160 dst->vna_flags = bfd_h_get_16 (abfd, src->vna_flags); 161 dst->vna_other = bfd_h_get_16 (abfd, src->vna_other); 162 dst->vna_name = bfd_h_get_32 (abfd, src->vna_name); 163 dst->vna_next = bfd_h_get_32 (abfd, src->vna_next); 164} 165 166/* Swap out a Vernaux structure. */ 167 168void 169_bfd_elf_swap_vernaux_out (abfd, src, dst) 170 bfd *abfd; 171 const Elf_Internal_Vernaux *src; 172 Elf_External_Vernaux *dst; 173{ 174 bfd_h_put_32 (abfd, src->vna_hash, dst->vna_hash); 175 bfd_h_put_16 (abfd, src->vna_flags, dst->vna_flags); 176 bfd_h_put_16 (abfd, src->vna_other, dst->vna_other); 177 bfd_h_put_32 (abfd, src->vna_name, dst->vna_name); 178 bfd_h_put_32 (abfd, src->vna_next, dst->vna_next); 179} 180 181/* Swap in a Versym structure. */ 182 183void 184_bfd_elf_swap_versym_in (abfd, src, dst) 185 bfd *abfd; 186 const Elf_External_Versym *src; 187 Elf_Internal_Versym *dst; 188{ 189 dst->vs_vers = bfd_h_get_16 (abfd, src->vs_vers); 190} 191 192/* Swap out a Versym structure. */ 193 194void 195_bfd_elf_swap_versym_out (abfd, src, dst) 196 bfd *abfd; 197 const Elf_Internal_Versym *src; 198 Elf_External_Versym *dst; 199{ 200 bfd_h_put_16 (abfd, src->vs_vers, dst->vs_vers); 201} 202 203/* Standard ELF hash function. Do not change this function; you will 204 cause invalid hash tables to be generated. (Well, you would if this 205 were being used yet.) */ 206unsigned long 207bfd_elf_hash (name) 208 CONST unsigned char *name; 209{ 210 unsigned long h = 0; 211 unsigned long g; 212 int ch; 213 214 while ((ch = *name++) != '\0') 215 { 216 h = (h << 4) + ch; 217 if ((g = (h & 0xf0000000)) != 0) 218 { 219 h ^= g >> 24; 220 h &= ~g; 221 } 222 } 223 return h; 224} 225 226/* Read a specified number of bytes at a specified offset in an ELF 227 file, into a newly allocated buffer, and return a pointer to the 228 buffer. */ 229 230static char * 231elf_read (abfd, offset, size) 232 bfd * abfd; 233 long offset; 234 unsigned int size; 235{ 236 char *buf; 237 238 if ((buf = bfd_alloc (abfd, size)) == NULL) 239 return NULL; 240 if (bfd_seek (abfd, offset, SEEK_SET) == -1) 241 return NULL; 242 if (bfd_read ((PTR) buf, size, 1, abfd) != size) 243 { 244 if (bfd_get_error () != bfd_error_system_call) 245 bfd_set_error (bfd_error_file_truncated); 246 return NULL; 247 } 248 return buf; 249} 250 251boolean 252bfd_elf_mkobject (abfd) 253 bfd * abfd; 254{ 255 /* this just does initialization */ 256 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */ 257 elf_tdata (abfd) = (struct elf_obj_tdata *) 258 bfd_zalloc (abfd, sizeof (struct elf_obj_tdata)); 259 if (elf_tdata (abfd) == 0) 260 return false; 261 /* since everything is done at close time, do we need any 262 initialization? */ 263 264 return true; 265} 266 267char * 268bfd_elf_get_str_section (abfd, shindex) 269 bfd * abfd; 270 unsigned int shindex; 271{ 272 Elf_Internal_Shdr **i_shdrp; 273 char *shstrtab = NULL; 274 unsigned int offset; 275 unsigned int shstrtabsize; 276 277 i_shdrp = elf_elfsections (abfd); 278 if (i_shdrp == 0 || i_shdrp[shindex] == 0) 279 return 0; 280 281 shstrtab = (char *) i_shdrp[shindex]->contents; 282 if (shstrtab == NULL) 283 { 284 /* No cached one, attempt to read, and cache what we read. */ 285 offset = i_shdrp[shindex]->sh_offset; 286 shstrtabsize = i_shdrp[shindex]->sh_size; 287 shstrtab = elf_read (abfd, offset, shstrtabsize); 288 i_shdrp[shindex]->contents = (PTR) shstrtab; 289 } 290 return shstrtab; 291} 292 293char * 294bfd_elf_string_from_elf_section (abfd, shindex, strindex) 295 bfd * abfd; 296 unsigned int shindex; 297 unsigned int strindex; 298{ 299 Elf_Internal_Shdr *hdr; 300 301 if (strindex == 0) 302 return ""; 303 304 hdr = elf_elfsections (abfd)[shindex]; 305 306 if (hdr->contents == NULL 307 && bfd_elf_get_str_section (abfd, shindex) == NULL) 308 return NULL; 309 310 if (strindex >= hdr->sh_size) 311 { 312 (*_bfd_error_handler) 313 ("%s: invalid string offset %u >= %lu for section `%s'", 314 bfd_get_filename (abfd), strindex, (unsigned long) hdr->sh_size, 315 ((shindex == elf_elfheader(abfd)->e_shstrndx 316 && strindex == hdr->sh_name) 317 ? ".shstrtab" 318 : elf_string_from_elf_strtab (abfd, hdr->sh_name))); 319 return ""; 320 } 321 322 return ((char *) hdr->contents) + strindex; 323} 324 325/* Make a BFD section from an ELF section. We store a pointer to the 326 BFD section in the bfd_section field of the header. */ 327 328boolean 329_bfd_elf_make_section_from_shdr (abfd, hdr, name) 330 bfd *abfd; 331 Elf_Internal_Shdr *hdr; 332 const char *name; 333{ 334 asection *newsect; 335 flagword flags; 336 337 if (hdr->bfd_section != NULL) 338 { 339 BFD_ASSERT (strcmp (name, 340 bfd_get_section_name (abfd, hdr->bfd_section)) == 0); 341 return true; 342 } 343 344 newsect = bfd_make_section_anyway (abfd, name); 345 if (newsect == NULL) 346 return false; 347 348 newsect->filepos = hdr->sh_offset; 349 350 if (! bfd_set_section_vma (abfd, newsect, hdr->sh_addr) 351 || ! bfd_set_section_size (abfd, newsect, hdr->sh_size) 352 || ! bfd_set_section_alignment (abfd, newsect, 353 bfd_log2 (hdr->sh_addralign))) 354 return false; 355 356 flags = SEC_NO_FLAGS; 357 if (hdr->sh_type != SHT_NOBITS) 358 flags |= SEC_HAS_CONTENTS; 359 if ((hdr->sh_flags & SHF_ALLOC) != 0) 360 { 361 flags |= SEC_ALLOC; 362 if (hdr->sh_type != SHT_NOBITS) 363 flags |= SEC_LOAD; 364 } 365 if ((hdr->sh_flags & SHF_WRITE) == 0) 366 flags |= SEC_READONLY; 367 if ((hdr->sh_flags & SHF_EXECINSTR) != 0) 368 flags |= SEC_CODE; 369 else if ((flags & SEC_LOAD) != 0) 370 flags |= SEC_DATA; 371 372 /* The debugging sections appear to be recognized only by name, not 373 any sort of flag. */ 374 if (strncmp (name, ".debug", sizeof ".debug" - 1) == 0 375 || strncmp (name, ".line", sizeof ".line" - 1) == 0 376 || strncmp (name, ".stab", sizeof ".stab" - 1) == 0) 377 flags |= SEC_DEBUGGING; 378 379 /* As a GNU extension, if the name begins with .gnu.linkonce, we 380 only link a single copy of the section. This is used to support 381 g++. g++ will emit each template expansion in its own section. 382 The symbols will be defined as weak, so that multiple definitions 383 are permitted. The GNU linker extension is to actually discard 384 all but one of the sections. */ 385 if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0) 386 flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD; 387 388 if (! bfd_set_section_flags (abfd, newsect, flags)) 389 return false; 390 391 if ((flags & SEC_ALLOC) != 0) 392 { 393 Elf_Internal_Phdr *phdr; 394 unsigned int i; 395 396 /* Look through the phdrs to see if we need to adjust the lma. */ 397 phdr = elf_tdata (abfd)->phdr; 398 for (i = 0; i < elf_elfheader (abfd)->e_phnum; i++, phdr++) 399 { 400 if (phdr->p_type == PT_LOAD 401 && phdr->p_paddr != 0 402 && phdr->p_vaddr != phdr->p_paddr 403 && phdr->p_vaddr <= hdr->sh_addr 404 && phdr->p_vaddr + phdr->p_memsz >= hdr->sh_addr + hdr->sh_size 405 && ((flags & SEC_LOAD) == 0 406 || (phdr->p_offset <= (bfd_vma) hdr->sh_offset 407 && (phdr->p_offset + phdr->p_filesz 408 >= hdr->sh_offset + hdr->sh_size)))) 409 { 410 newsect->lma += phdr->p_paddr - phdr->p_vaddr; 411 break; 412 } 413 } 414 } 415 416 hdr->bfd_section = newsect; 417 elf_section_data (newsect)->this_hdr = *hdr; 418 419 return true; 420} 421 422/* 423INTERNAL_FUNCTION 424 bfd_elf_find_section 425 426SYNOPSIS 427 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name); 428 429DESCRIPTION 430 Helper functions for GDB to locate the string tables. 431 Since BFD hides string tables from callers, GDB needs to use an 432 internal hook to find them. Sun's .stabstr, in particular, 433 isn't even pointed to by the .stab section, so ordinary 434 mechanisms wouldn't work to find it, even if we had some. 435*/ 436 437struct elf_internal_shdr * 438bfd_elf_find_section (abfd, name) 439 bfd * abfd; 440 char *name; 441{ 442 Elf_Internal_Shdr **i_shdrp; 443 char *shstrtab; 444 unsigned int max; 445 unsigned int i; 446 447 i_shdrp = elf_elfsections (abfd); 448 if (i_shdrp != NULL) 449 { 450 shstrtab = bfd_elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx); 451 if (shstrtab != NULL) 452 { 453 max = elf_elfheader (abfd)->e_shnum; 454 for (i = 1; i < max; i++) 455 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name)) 456 return i_shdrp[i]; 457 } 458 } 459 return 0; 460} 461 462const char *const bfd_elf_section_type_names[] = { 463 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB", 464 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE", 465 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM", 466}; 467 468/* ELF relocs are against symbols. If we are producing relocateable 469 output, and the reloc is against an external symbol, and nothing 470 has given us any additional addend, the resulting reloc will also 471 be against the same symbol. In such a case, we don't want to 472 change anything about the way the reloc is handled, since it will 473 all be done at final link time. Rather than put special case code 474 into bfd_perform_relocation, all the reloc types use this howto 475 function. It just short circuits the reloc if producing 476 relocateable output against an external symbol. */ 477 478/*ARGSUSED*/ 479bfd_reloc_status_type 480bfd_elf_generic_reloc (abfd, 481 reloc_entry, 482 symbol, 483 data, 484 input_section, 485 output_bfd, 486 error_message) 487 bfd *abfd; 488 arelent *reloc_entry; 489 asymbol *symbol; 490 PTR data; 491 asection *input_section; 492 bfd *output_bfd; 493 char **error_message; 494{ 495 if (output_bfd != (bfd *) NULL 496 && (symbol->flags & BSF_SECTION_SYM) == 0 497 && (! reloc_entry->howto->partial_inplace 498 || reloc_entry->addend == 0)) 499 { 500 reloc_entry->address += input_section->output_offset; 501 return bfd_reloc_ok; 502 } 503 504 return bfd_reloc_continue; 505} 506 507/* Print out the program headers. */ 508 509boolean 510_bfd_elf_print_private_bfd_data (abfd, farg) 511 bfd *abfd; 512 PTR farg; 513{ 514 FILE *f = (FILE *) farg; 515 Elf_Internal_Phdr *p; 516 asection *s; 517 bfd_byte *dynbuf = NULL; 518 519 p = elf_tdata (abfd)->phdr; 520 if (p != NULL) 521 { 522 unsigned int i, c; 523 524 fprintf (f, "\nProgram Header:\n"); 525 c = elf_elfheader (abfd)->e_phnum; 526 for (i = 0; i < c; i++, p++) 527 { 528 const char *s; 529 char buf[20]; 530 531 switch (p->p_type) 532 { 533 case PT_NULL: s = "NULL"; break; 534 case PT_LOAD: s = "LOAD"; break; 535 case PT_DYNAMIC: s = "DYNAMIC"; break; 536 case PT_INTERP: s = "INTERP"; break; 537 case PT_NOTE: s = "NOTE"; break; 538 case PT_SHLIB: s = "SHLIB"; break; 539 case PT_PHDR: s = "PHDR"; break; 540 default: sprintf (buf, "0x%lx", p->p_type); s = buf; break; 541 } 542 fprintf (f, "%8s off 0x", s); 543 fprintf_vma (f, p->p_offset); 544 fprintf (f, " vaddr 0x"); 545 fprintf_vma (f, p->p_vaddr); 546 fprintf (f, " paddr 0x"); 547 fprintf_vma (f, p->p_paddr); 548 fprintf (f, " align 2**%u\n", bfd_log2 (p->p_align)); 549 fprintf (f, " filesz 0x"); 550 fprintf_vma (f, p->p_filesz); 551 fprintf (f, " memsz 0x"); 552 fprintf_vma (f, p->p_memsz); 553 fprintf (f, " flags %c%c%c", 554 (p->p_flags & PF_R) != 0 ? 'r' : '-', 555 (p->p_flags & PF_W) != 0 ? 'w' : '-', 556 (p->p_flags & PF_X) != 0 ? 'x' : '-'); 557 if ((p->p_flags &~ (PF_R | PF_W | PF_X)) != 0) 558 fprintf (f, " %lx", p->p_flags &~ (PF_R | PF_W | PF_X)); 559 fprintf (f, "\n"); 560 } 561 } 562 563 s = bfd_get_section_by_name (abfd, ".dynamic"); 564 if (s != NULL) 565 { 566 int elfsec; 567 unsigned long link; 568 bfd_byte *extdyn, *extdynend; 569 size_t extdynsize; 570 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *)); 571 572 fprintf (f, "\nDynamic Section:\n"); 573 574 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size); 575 if (dynbuf == NULL) 576 goto error_return; 577 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0, 578 s->_raw_size)) 579 goto error_return; 580 581 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 582 if (elfsec == -1) 583 goto error_return; 584 link = elf_elfsections (abfd)[elfsec]->sh_link; 585 586 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 587 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 588 589 extdyn = dynbuf; 590 extdynend = extdyn + s->_raw_size; 591 for (; extdyn < extdynend; extdyn += extdynsize) 592 { 593 Elf_Internal_Dyn dyn; 594 const char *name; 595 char ab[20]; 596 boolean stringp; 597 598 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn); 599 600 if (dyn.d_tag == DT_NULL) 601 break; 602 603 stringp = false; 604 switch (dyn.d_tag) 605 { 606 default: 607 sprintf (ab, "0x%lx", (unsigned long) dyn.d_tag); 608 name = ab; 609 break; 610 611 case DT_NEEDED: name = "NEEDED"; stringp = true; break; 612 case DT_PLTRELSZ: name = "PLTRELSZ"; break; 613 case DT_PLTGOT: name = "PLTGOT"; break; 614 case DT_HASH: name = "HASH"; break; 615 case DT_STRTAB: name = "STRTAB"; break; 616 case DT_SYMTAB: name = "SYMTAB"; break; 617 case DT_RELA: name = "RELA"; break; 618 case DT_RELASZ: name = "RELASZ"; break; 619 case DT_RELAENT: name = "RELAENT"; break; 620 case DT_STRSZ: name = "STRSZ"; break; 621 case DT_SYMENT: name = "SYMENT"; break; 622 case DT_INIT: name = "INIT"; break; 623 case DT_FINI: name = "FINI"; break; 624 case DT_SONAME: name = "SONAME"; stringp = true; break; 625 case DT_RPATH: name = "RPATH"; stringp = true; break; 626 case DT_SYMBOLIC: name = "SYMBOLIC"; break; 627 case DT_REL: name = "REL"; break; 628 case DT_RELSZ: name = "RELSZ"; break; 629 case DT_RELENT: name = "RELENT"; break; 630 case DT_PLTREL: name = "PLTREL"; break; 631 case DT_DEBUG: name = "DEBUG"; break; 632 case DT_TEXTREL: name = "TEXTREL"; break; 633 case DT_JMPREL: name = "JMPREL"; break; 634 case DT_AUXILIARY: name = "AUXILIARY"; stringp = true; break; 635 case DT_FILTER: name = "FILTER"; stringp = true; break; 636 case DT_VERSYM: name = "VERSYM"; break; 637 case DT_VERDEF: name = "VERDEF"; break; 638 case DT_VERDEFNUM: name = "VERDEFNUM"; break; 639 case DT_VERNEED: name = "VERNEED"; break; 640 case DT_VERNEEDNUM: name = "VERNEEDNUM"; break; 641 } 642 643 fprintf (f, " %-11s ", name); 644 if (! stringp) 645 fprintf (f, "0x%lx", (unsigned long) dyn.d_un.d_val); 646 else 647 { 648 const char *string; 649 650 string = bfd_elf_string_from_elf_section (abfd, link, 651 dyn.d_un.d_val); 652 if (string == NULL) 653 goto error_return; 654 fprintf (f, "%s", string); 655 } 656 fprintf (f, "\n"); 657 } 658 659 free (dynbuf); 660 dynbuf = NULL; 661 } 662 663 if ((elf_dynverdef (abfd) != 0 && elf_tdata (abfd)->verdef == NULL) 664 || (elf_dynverref (abfd) != 0 && elf_tdata (abfd)->verref == NULL)) 665 { 666 if (! _bfd_elf_slurp_version_tables (abfd)) 667 return false; 668 } 669 670 if (elf_dynverdef (abfd) != 0) 671 { 672 Elf_Internal_Verdef *t; 673 674 fprintf (f, "\nVersion definitions:\n"); 675 for (t = elf_tdata (abfd)->verdef; t != NULL; t = t->vd_nextdef) 676 { 677 fprintf (f, "%d 0x%2.2x 0x%8.8lx %s\n", t->vd_ndx, 678 t->vd_flags, t->vd_hash, t->vd_nodename); 679 if (t->vd_auxptr->vda_nextptr != NULL) 680 { 681 Elf_Internal_Verdaux *a; 682 683 fprintf (f, "\t"); 684 for (a = t->vd_auxptr->vda_nextptr; 685 a != NULL; 686 a = a->vda_nextptr) 687 fprintf (f, "%s ", a->vda_nodename); 688 fprintf (f, "\n"); 689 } 690 } 691 } 692 693 if (elf_dynverref (abfd) != 0) 694 { 695 Elf_Internal_Verneed *t; 696 697 fprintf (f, "\nVersion References:\n"); 698 for (t = elf_tdata (abfd)->verref; t != NULL; t = t->vn_nextref) 699 { 700 Elf_Internal_Vernaux *a; 701 702 fprintf (f, " required from %s:\n", t->vn_filename); 703 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 704 fprintf (f, " 0x%8.8lx 0x%2.2x %2.2d %s\n", a->vna_hash, 705 a->vna_flags, a->vna_other, a->vna_nodename); 706 } 707 } 708 709 return true; 710 711 error_return: 712 if (dynbuf != NULL) 713 free (dynbuf); 714 return false; 715} 716 717/* Display ELF-specific fields of a symbol. */ 718 719void 720bfd_elf_print_symbol (abfd, filep, symbol, how) 721 bfd *abfd; 722 PTR filep; 723 asymbol *symbol; 724 bfd_print_symbol_type how; 725{ 726 FILE *file = (FILE *) filep; 727 switch (how) 728 { 729 case bfd_print_symbol_name: 730 fprintf (file, "%s", symbol->name); 731 break; 732 case bfd_print_symbol_more: 733 fprintf (file, "elf "); 734 fprintf_vma (file, symbol->value); 735 fprintf (file, " %lx", (long) symbol->flags); 736 break; 737 case bfd_print_symbol_all: 738 { 739 CONST char *section_name; 740 section_name = symbol->section ? symbol->section->name : "(*none*)"; 741 bfd_print_symbol_vandf ((PTR) file, symbol); 742 fprintf (file, " %s\t", section_name); 743 /* Print the "other" value for a symbol. For common symbols, 744 we've already printed the size; now print the alignment. 745 For other symbols, we have no specified alignment, and 746 we've printed the address; now print the size. */ 747 fprintf_vma (file, 748 (bfd_is_com_section (symbol->section) 749 ? ((elf_symbol_type *) symbol)->internal_elf_sym.st_value 750 : ((elf_symbol_type *) symbol)->internal_elf_sym.st_size)); 751 752 /* If we have version information, print it. */ 753 if (elf_tdata (abfd)->dynversym_section != 0 754 && (elf_tdata (abfd)->dynverdef_section != 0 755 || elf_tdata (abfd)->dynverref_section != 0)) 756 { 757 unsigned int vernum; 758 const char *version_string; 759 760 vernum = ((elf_symbol_type *) symbol)->version & VERSYM_VERSION; 761 762 if (vernum == 0) 763 version_string = ""; 764 else if (vernum == 1) 765 version_string = "Base"; 766 else if (vernum <= elf_tdata (abfd)->cverdefs) 767 version_string = 768 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 769 else 770 { 771 Elf_Internal_Verneed *t; 772 773 version_string = ""; 774 for (t = elf_tdata (abfd)->verref; 775 t != NULL; 776 t = t->vn_nextref) 777 { 778 Elf_Internal_Vernaux *a; 779 780 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 781 { 782 if (a->vna_other == vernum) 783 { 784 version_string = a->vna_nodename; 785 break; 786 } 787 } 788 } 789 } 790 791 if ((((elf_symbol_type *) symbol)->version & VERSYM_HIDDEN) == 0) 792 fprintf (file, " %-11s", version_string); 793 else 794 { 795 int i; 796 797 fprintf (file, " (%s)", version_string); 798 for (i = 10 - strlen (version_string); i > 0; --i) 799 putc (' ', file); 800 } 801 } 802 803 /* If the st_other field is not zero, print it. */ 804 if (((elf_symbol_type *) symbol)->internal_elf_sym.st_other != 0) 805 fprintf (file, " 0x%02x", 806 ((unsigned int) 807 ((elf_symbol_type *) symbol)->internal_elf_sym.st_other)); 808 809 fprintf (file, " %s", symbol->name); 810 } 811 break; 812 } 813} 814 815/* Create an entry in an ELF linker hash table. */ 816 817struct bfd_hash_entry * 818_bfd_elf_link_hash_newfunc (entry, table, string) 819 struct bfd_hash_entry *entry; 820 struct bfd_hash_table *table; 821 const char *string; 822{ 823 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; 824 825 /* Allocate the structure if it has not already been allocated by a 826 subclass. */ 827 if (ret == (struct elf_link_hash_entry *) NULL) 828 ret = ((struct elf_link_hash_entry *) 829 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry))); 830 if (ret == (struct elf_link_hash_entry *) NULL) 831 return (struct bfd_hash_entry *) ret; 832 833 /* Call the allocation method of the superclass. */ 834 ret = ((struct elf_link_hash_entry *) 835 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, 836 table, string)); 837 if (ret != (struct elf_link_hash_entry *) NULL) 838 { 839 /* Set local fields. */ 840 ret->indx = -1; 841 ret->size = 0; 842 ret->dynindx = -1; 843 ret->dynstr_index = 0; 844 ret->weakdef = NULL; 845 ret->got_offset = (bfd_vma) -1; 846 ret->plt_offset = (bfd_vma) -1; 847 ret->linker_section_pointer = (elf_linker_section_pointers_t *)0; 848 ret->verinfo.verdef = NULL; 849 ret->type = STT_NOTYPE; 850 ret->other = 0; 851 /* Assume that we have been called by a non-ELF symbol reader. 852 This flag is then reset by the code which reads an ELF input 853 file. This ensures that a symbol created by a non-ELF symbol 854 reader will have the flag set correctly. */ 855 ret->elf_link_hash_flags = ELF_LINK_NON_ELF; 856 } 857 858 return (struct bfd_hash_entry *) ret; 859} 860 861/* Initialize an ELF linker hash table. */ 862 863boolean 864_bfd_elf_link_hash_table_init (table, abfd, newfunc) 865 struct elf_link_hash_table *table; 866 bfd *abfd; 867 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *, 868 struct bfd_hash_table *, 869 const char *)); 870{ 871 table->dynamic_sections_created = false; 872 table->dynobj = NULL; 873 /* The first dynamic symbol is a dummy. */ 874 table->dynsymcount = 1; 875 table->dynstr = NULL; 876 table->bucketcount = 0; 877 table->needed = NULL; 878 table->hgot = NULL; 879 table->stab_info = NULL; 880 return _bfd_link_hash_table_init (&table->root, abfd, newfunc); 881} 882 883/* Create an ELF linker hash table. */ 884 885struct bfd_link_hash_table * 886_bfd_elf_link_hash_table_create (abfd) 887 bfd *abfd; 888{ 889 struct elf_link_hash_table *ret; 890 891 ret = ((struct elf_link_hash_table *) 892 bfd_alloc (abfd, sizeof (struct elf_link_hash_table))); 893 if (ret == (struct elf_link_hash_table *) NULL) 894 return NULL; 895 896 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc)) 897 { 898 bfd_release (abfd, ret); 899 return NULL; 900 } 901 902 return &ret->root; 903} 904 905/* This is a hook for the ELF emulation code in the generic linker to 906 tell the backend linker what file name to use for the DT_NEEDED 907 entry for a dynamic object. The generic linker passes name as an 908 empty string to indicate that no DT_NEEDED entry should be made. */ 909 910void 911bfd_elf_set_dt_needed_name (abfd, name) 912 bfd *abfd; 913 const char *name; 914{ 915 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 916 && bfd_get_format (abfd) == bfd_object) 917 elf_dt_name (abfd) = name; 918} 919 920/* Get the list of DT_NEEDED entries for a link. This is a hook for 921 the ELF emulation code. */ 922 923struct bfd_link_needed_list * 924bfd_elf_get_needed_list (abfd, info) 925 bfd *abfd; 926 struct bfd_link_info *info; 927{ 928 if (info->hash->creator->flavour != bfd_target_elf_flavour) 929 return NULL; 930 return elf_hash_table (info)->needed; 931} 932 933/* Get the name actually used for a dynamic object for a link. This 934 is the SONAME entry if there is one. Otherwise, it is the string 935 passed to bfd_elf_set_dt_needed_name, or it is the filename. */ 936 937const char * 938bfd_elf_get_dt_soname (abfd) 939 bfd *abfd; 940{ 941 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 942 && bfd_get_format (abfd) == bfd_object) 943 return elf_dt_name (abfd); 944 return NULL; 945} 946 947/* Get the list of DT_NEEDED entries from a BFD. This is a hook for 948 the ELF linker emulation code. */ 949 950boolean 951bfd_elf_get_bfd_needed_list (abfd, pneeded) 952 bfd *abfd; 953 struct bfd_link_needed_list **pneeded; 954{ 955 asection *s; 956 bfd_byte *dynbuf = NULL; 957 int elfsec; 958 unsigned long link; 959 bfd_byte *extdyn, *extdynend; 960 size_t extdynsize; 961 void (*swap_dyn_in) PARAMS ((bfd *, const PTR, Elf_Internal_Dyn *)); 962 963 *pneeded = NULL; 964 965 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour 966 || bfd_get_format (abfd) != bfd_object) 967 return true; 968 969 s = bfd_get_section_by_name (abfd, ".dynamic"); 970 if (s == NULL || s->_raw_size == 0) 971 return true; 972 973 dynbuf = (bfd_byte *) bfd_malloc (s->_raw_size); 974 if (dynbuf == NULL) 975 goto error_return; 976 977 if (! bfd_get_section_contents (abfd, s, (PTR) dynbuf, (file_ptr) 0, 978 s->_raw_size)) 979 goto error_return; 980 981 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 982 if (elfsec == -1) 983 goto error_return; 984 985 link = elf_elfsections (abfd)[elfsec]->sh_link; 986 987 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 988 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 989 990 extdyn = dynbuf; 991 extdynend = extdyn + s->_raw_size; 992 for (; extdyn < extdynend; extdyn += extdynsize) 993 { 994 Elf_Internal_Dyn dyn; 995 996 (*swap_dyn_in) (abfd, (PTR) extdyn, &dyn); 997 998 if (dyn.d_tag == DT_NULL) 999 break; 1000 1001 if (dyn.d_tag == DT_NEEDED) 1002 { 1003 const char *string; 1004 struct bfd_link_needed_list *l; 1005 1006 string = bfd_elf_string_from_elf_section (abfd, link, 1007 dyn.d_un.d_val); 1008 if (string == NULL) 1009 goto error_return; 1010 1011 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, sizeof *l); 1012 if (l == NULL) 1013 goto error_return; 1014 1015 l->by = abfd; 1016 l->name = string; 1017 l->next = *pneeded; 1018 *pneeded = l; 1019 } 1020 } 1021 1022 free (dynbuf); 1023 1024 return true; 1025 1026 error_return: 1027 if (dynbuf != NULL) 1028 free (dynbuf); 1029 return false; 1030} 1031 1032/* Allocate an ELF string table--force the first byte to be zero. */ 1033 1034struct bfd_strtab_hash * 1035_bfd_elf_stringtab_init () 1036{ 1037 struct bfd_strtab_hash *ret; 1038 1039 ret = _bfd_stringtab_init (); 1040 if (ret != NULL) 1041 { 1042 bfd_size_type loc; 1043 1044 loc = _bfd_stringtab_add (ret, "", true, false); 1045 BFD_ASSERT (loc == 0 || loc == (bfd_size_type) -1); 1046 if (loc == (bfd_size_type) -1) 1047 { 1048 _bfd_stringtab_free (ret); 1049 ret = NULL; 1050 } 1051 } 1052 return ret; 1053} 1054 1055/* ELF .o/exec file reading */ 1056 1057/* Create a new bfd section from an ELF section header. */ 1058 1059boolean 1060bfd_section_from_shdr (abfd, shindex) 1061 bfd *abfd; 1062 unsigned int shindex; 1063{ 1064 Elf_Internal_Shdr *hdr = elf_elfsections (abfd)[shindex]; 1065 Elf_Internal_Ehdr *ehdr = elf_elfheader (abfd); 1066 struct elf_backend_data *bed = get_elf_backend_data (abfd); 1067 char *name; 1068 1069 name = elf_string_from_elf_strtab (abfd, hdr->sh_name); 1070 1071 switch (hdr->sh_type) 1072 { 1073 case SHT_NULL: 1074 /* Inactive section. Throw it away. */ 1075 return true; 1076 1077 case SHT_PROGBITS: /* Normal section with contents. */ 1078 case SHT_DYNAMIC: /* Dynamic linking information. */ 1079 case SHT_NOBITS: /* .bss section. */ 1080 case SHT_HASH: /* .hash section. */ 1081 case SHT_NOTE: /* .note section. */ 1082 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 1083 1084 case SHT_SYMTAB: /* A symbol table */ 1085 if (elf_onesymtab (abfd) == shindex) 1086 return true; 1087 1088 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym); 1089 BFD_ASSERT (elf_onesymtab (abfd) == 0); 1090 elf_onesymtab (abfd) = shindex; 1091 elf_tdata (abfd)->symtab_hdr = *hdr; 1092 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->symtab_hdr; 1093 abfd->flags |= HAS_SYMS; 1094 1095 /* Sometimes a shared object will map in the symbol table. If 1096 SHF_ALLOC is set, and this is a shared object, then we also 1097 treat this section as a BFD section. We can not base the 1098 decision purely on SHF_ALLOC, because that flag is sometimes 1099 set in a relocateable object file, which would confuse the 1100 linker. */ 1101 if ((hdr->sh_flags & SHF_ALLOC) != 0 1102 && (abfd->flags & DYNAMIC) != 0 1103 && ! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) 1104 return false; 1105 1106 return true; 1107 1108 case SHT_DYNSYM: /* A dynamic symbol table */ 1109 if (elf_dynsymtab (abfd) == shindex) 1110 return true; 1111 1112 BFD_ASSERT (hdr->sh_entsize == bed->s->sizeof_sym); 1113 BFD_ASSERT (elf_dynsymtab (abfd) == 0); 1114 elf_dynsymtab (abfd) = shindex; 1115 elf_tdata (abfd)->dynsymtab_hdr = *hdr; 1116 elf_elfsections (abfd)[shindex] = hdr = &elf_tdata (abfd)->dynsymtab_hdr; 1117 abfd->flags |= HAS_SYMS; 1118 1119 /* Besides being a symbol table, we also treat this as a regular 1120 section, so that objcopy can handle it. */ 1121 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 1122 1123 case SHT_STRTAB: /* A string table */ 1124 if (hdr->bfd_section != NULL) 1125 return true; 1126 if (ehdr->e_shstrndx == shindex) 1127 { 1128 elf_tdata (abfd)->shstrtab_hdr = *hdr; 1129 elf_elfsections (abfd)[shindex] = &elf_tdata (abfd)->shstrtab_hdr; 1130 return true; 1131 } 1132 { 1133 unsigned int i; 1134 1135 for (i = 1; i < ehdr->e_shnum; i++) 1136 { 1137 Elf_Internal_Shdr *hdr2 = elf_elfsections (abfd)[i]; 1138 if (hdr2->sh_link == shindex) 1139 { 1140 if (! bfd_section_from_shdr (abfd, i)) 1141 return false; 1142 if (elf_onesymtab (abfd) == i) 1143 { 1144 elf_tdata (abfd)->strtab_hdr = *hdr; 1145 elf_elfsections (abfd)[shindex] = 1146 &elf_tdata (abfd)->strtab_hdr; 1147 return true; 1148 } 1149 if (elf_dynsymtab (abfd) == i) 1150 { 1151 elf_tdata (abfd)->dynstrtab_hdr = *hdr; 1152 elf_elfsections (abfd)[shindex] = hdr = 1153 &elf_tdata (abfd)->dynstrtab_hdr; 1154 /* We also treat this as a regular section, so 1155 that objcopy can handle it. */ 1156 break; 1157 } 1158#if 0 /* Not handling other string tables specially right now. */ 1159 hdr2 = elf_elfsections (abfd)[i]; /* in case it moved */ 1160 /* We have a strtab for some random other section. */ 1161 newsect = (asection *) hdr2->bfd_section; 1162 if (!newsect) 1163 break; 1164 hdr->bfd_section = newsect; 1165 hdr2 = &elf_section_data (newsect)->str_hdr; 1166 *hdr2 = *hdr; 1167 elf_elfsections (abfd)[shindex] = hdr2; 1168#endif 1169 } 1170 } 1171 } 1172 1173 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 1174 1175 case SHT_REL: 1176 case SHT_RELA: 1177 /* *These* do a lot of work -- but build no sections! */ 1178 { 1179 asection *target_sect; 1180 Elf_Internal_Shdr *hdr2; 1181 1182 /* For some incomprehensible reason Oracle distributes 1183 libraries for Solaris in which some of the objects have 1184 bogus sh_link fields. It would be nice if we could just 1185 reject them, but, unfortunately, some people need to use 1186 them. We scan through the section headers; if we find only 1187 one suitable symbol table, we clobber the sh_link to point 1188 to it. I hope this doesn't break anything. */ 1189 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_SYMTAB 1190 && elf_elfsections (abfd)[hdr->sh_link]->sh_type != SHT_DYNSYM) 1191 { 1192 int scan; 1193 int found; 1194 1195 found = 0; 1196 for (scan = 1; scan < ehdr->e_shnum; scan++) 1197 { 1198 if (elf_elfsections (abfd)[scan]->sh_type == SHT_SYMTAB 1199 || elf_elfsections (abfd)[scan]->sh_type == SHT_DYNSYM) 1200 { 1201 if (found != 0) 1202 { 1203 found = 0; 1204 break; 1205 } 1206 found = scan; 1207 } 1208 } 1209 if (found != 0) 1210 hdr->sh_link = found; 1211 } 1212 1213 /* Get the symbol table. */ 1214 if (elf_elfsections (abfd)[hdr->sh_link]->sh_type == SHT_SYMTAB 1215 && ! bfd_section_from_shdr (abfd, hdr->sh_link)) 1216 return false; 1217 1218 /* If this reloc section does not use the main symbol table we 1219 don't treat it as a reloc section. BFD can't adequately 1220 represent such a section, so at least for now, we don't 1221 try. We just present it as a normal section. */ 1222 if (hdr->sh_link != elf_onesymtab (abfd)) 1223 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 1224 1225 if (! bfd_section_from_shdr (abfd, hdr->sh_info)) 1226 return false; 1227 target_sect = bfd_section_from_elf_index (abfd, hdr->sh_info); 1228 if (target_sect == NULL) 1229 return false; 1230 1231 if ((target_sect->flags & SEC_RELOC) == 0 1232 || target_sect->reloc_count == 0) 1233 hdr2 = &elf_section_data (target_sect)->rel_hdr; 1234 else 1235 { 1236 BFD_ASSERT (elf_section_data (target_sect)->rel_hdr2 == NULL); 1237 hdr2 = (Elf_Internal_Shdr *) bfd_alloc (abfd, sizeof (*hdr2)); 1238 elf_section_data (target_sect)->rel_hdr2 = hdr2; 1239 } 1240 *hdr2 = *hdr; 1241 elf_elfsections (abfd)[shindex] = hdr2; 1242 target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize; 1243 target_sect->flags |= SEC_RELOC; 1244 target_sect->relocation = NULL; 1245 target_sect->rel_filepos = hdr->sh_offset; 1246 abfd->flags |= HAS_RELOC; 1247 return true; 1248 } 1249 break; 1250 1251 case SHT_GNU_verdef: 1252 elf_dynverdef (abfd) = shindex; 1253 elf_tdata (abfd)->dynverdef_hdr = *hdr; 1254 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 1255 break; 1256 1257 case SHT_GNU_versym: 1258 elf_dynversym (abfd) = shindex; 1259 elf_tdata (abfd)->dynversym_hdr = *hdr; 1260 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 1261 break; 1262 1263 case SHT_GNU_verneed: 1264 elf_dynverref (abfd) = shindex; 1265 elf_tdata (abfd)->dynverref_hdr = *hdr; 1266 return _bfd_elf_make_section_from_shdr (abfd, hdr, name); 1267 break; 1268 1269 case SHT_SHLIB: 1270 return true; 1271 1272 default: 1273 /* Check for any processor-specific section types. */ 1274 { 1275 if (bed->elf_backend_section_from_shdr) 1276 (*bed->elf_backend_section_from_shdr) (abfd, hdr, name); 1277 } 1278 break; 1279 } 1280 1281 return true; 1282} 1283 1284/* Given an ELF section number, retrieve the corresponding BFD 1285 section. */ 1286 1287asection * 1288bfd_section_from_elf_index (abfd, index) 1289 bfd *abfd; 1290 unsigned int index; 1291{ 1292 BFD_ASSERT (index > 0 && index < SHN_LORESERVE); 1293 if (index >= elf_elfheader (abfd)->e_shnum) 1294 return NULL; 1295 return elf_elfsections (abfd)[index]->bfd_section; 1296} 1297 1298boolean 1299_bfd_elf_new_section_hook (abfd, sec) 1300 bfd *abfd; 1301 asection *sec; 1302{ 1303 struct bfd_elf_section_data *sdata; 1304 1305 sdata = (struct bfd_elf_section_data *) bfd_alloc (abfd, sizeof (*sdata)); 1306 if (!sdata) 1307 return false; 1308 sec->used_by_bfd = (PTR) sdata; 1309 memset (sdata, 0, sizeof (*sdata)); 1310 return true; 1311} 1312 1313/* Create a new bfd section from an ELF program header. 1314 1315 Since program segments have no names, we generate a synthetic name 1316 of the form segment<NUM>, where NUM is generally the index in the 1317 program header table. For segments that are split (see below) we 1318 generate the names segment<NUM>a and segment<NUM>b. 1319 1320 Note that some program segments may have a file size that is different than 1321 (less than) the memory size. All this means is that at execution the 1322 system must allocate the amount of memory specified by the memory size, 1323 but only initialize it with the first "file size" bytes read from the 1324 file. This would occur for example, with program segments consisting 1325 of combined data+bss. 1326 1327 To handle the above situation, this routine generates TWO bfd sections 1328 for the single program segment. The first has the length specified by 1329 the file size of the segment, and the second has the length specified 1330 by the difference between the two sizes. In effect, the segment is split 1331 into it's initialized and uninitialized parts. 1332 1333 */ 1334 1335boolean 1336bfd_section_from_phdr (abfd, hdr, index) 1337 bfd *abfd; 1338 Elf_Internal_Phdr *hdr; 1339 int index; 1340{ 1341 asection *newsect; 1342 char *name; 1343 char namebuf[64]; 1344 int split; 1345 1346 split = ((hdr->p_memsz > 0) && 1347 (hdr->p_filesz > 0) && 1348 (hdr->p_memsz > hdr->p_filesz)); 1349 sprintf (namebuf, split ? "segment%da" : "segment%d", index); 1350 name = bfd_alloc (abfd, strlen (namebuf) + 1); 1351 if (!name) 1352 return false; 1353 strcpy (name, namebuf); 1354 newsect = bfd_make_section (abfd, name); 1355 if (newsect == NULL) 1356 return false; 1357 newsect->vma = hdr->p_vaddr; 1358 newsect->lma = hdr->p_paddr; 1359 newsect->_raw_size = hdr->p_filesz; 1360 newsect->filepos = hdr->p_offset; 1361 newsect->flags |= SEC_HAS_CONTENTS; 1362 if (hdr->p_type == PT_LOAD) 1363 { 1364 newsect->flags |= SEC_ALLOC; 1365 newsect->flags |= SEC_LOAD; 1366 if (hdr->p_flags & PF_X) 1367 { 1368 /* FIXME: all we known is that it has execute PERMISSION, 1369 may be data. */ 1370 newsect->flags |= SEC_CODE; 1371 } 1372 } 1373 if (!(hdr->p_flags & PF_W)) 1374 { 1375 newsect->flags |= SEC_READONLY; 1376 } 1377 1378 if (split) 1379 { 1380 sprintf (namebuf, "segment%db", index); 1381 name = bfd_alloc (abfd, strlen (namebuf) + 1); 1382 if (!name) 1383 return false; 1384 strcpy (name, namebuf); 1385 newsect = bfd_make_section (abfd, name); 1386 if (newsect == NULL) 1387 return false; 1388 newsect->vma = hdr->p_vaddr + hdr->p_filesz; 1389 newsect->lma = hdr->p_paddr + hdr->p_filesz; 1390 newsect->_raw_size = hdr->p_memsz - hdr->p_filesz; 1391 if (hdr->p_type == PT_LOAD) 1392 { 1393 newsect->flags |= SEC_ALLOC; 1394 if (hdr->p_flags & PF_X) 1395 newsect->flags |= SEC_CODE; 1396 } 1397 if (!(hdr->p_flags & PF_W)) 1398 newsect->flags |= SEC_READONLY; 1399 } 1400 1401 return true; 1402} 1403 1404/* Set up an ELF internal section header for a section. */ 1405 1406/*ARGSUSED*/ 1407static void 1408elf_fake_sections (abfd, asect, failedptrarg) 1409 bfd *abfd; 1410 asection *asect; 1411 PTR failedptrarg; 1412{ 1413 struct elf_backend_data *bed = get_elf_backend_data (abfd); 1414 boolean *failedptr = (boolean *) failedptrarg; 1415 Elf_Internal_Shdr *this_hdr; 1416 1417 if (*failedptr) 1418 { 1419 /* We already failed; just get out of the bfd_map_over_sections 1420 loop. */ 1421 return; 1422 } 1423 1424 this_hdr = &elf_section_data (asect)->this_hdr; 1425 1426 this_hdr->sh_name = (unsigned long) _bfd_stringtab_add (elf_shstrtab (abfd), 1427 asect->name, 1428 true, false); 1429 if (this_hdr->sh_name == (unsigned long) -1) 1430 { 1431 *failedptr = true; 1432 return; 1433 } 1434 1435 this_hdr->sh_flags = 0; 1436 1437 if ((asect->flags & SEC_ALLOC) != 0 1438 || asect->user_set_vma) 1439 this_hdr->sh_addr = asect->vma; 1440 else 1441 this_hdr->sh_addr = 0; 1442 1443 this_hdr->sh_offset = 0; 1444 this_hdr->sh_size = asect->_raw_size; 1445 this_hdr->sh_link = 0; 1446 this_hdr->sh_addralign = 1 << asect->alignment_power; 1447 /* The sh_entsize and sh_info fields may have been set already by 1448 copy_private_section_data. */ 1449 1450 this_hdr->bfd_section = asect; 1451 this_hdr->contents = NULL; 1452 1453 /* FIXME: This should not be based on section names. */ 1454 if (strcmp (asect->name, ".dynstr") == 0) 1455 this_hdr->sh_type = SHT_STRTAB; 1456 else if (strcmp (asect->name, ".hash") == 0) 1457 { 1458 this_hdr->sh_type = SHT_HASH; 1459 this_hdr->sh_entsize = bed->s->arch_size / 8; 1460 } 1461 else if (strcmp (asect->name, ".dynsym") == 0) 1462 { 1463 this_hdr->sh_type = SHT_DYNSYM; 1464 this_hdr->sh_entsize = bed->s->sizeof_sym; 1465 } 1466 else if (strcmp (asect->name, ".dynamic") == 0) 1467 { 1468 this_hdr->sh_type = SHT_DYNAMIC; 1469 this_hdr->sh_entsize = bed->s->sizeof_dyn; 1470 } 1471 else if (strncmp (asect->name, ".rela", 5) == 0 1472 && get_elf_backend_data (abfd)->use_rela_p) 1473 { 1474 this_hdr->sh_type = SHT_RELA; 1475 this_hdr->sh_entsize = bed->s->sizeof_rela; 1476 } 1477 else if (strncmp (asect->name, ".rel", 4) == 0 1478 && ! get_elf_backend_data (abfd)->use_rela_p) 1479 { 1480 this_hdr->sh_type = SHT_REL; 1481 this_hdr->sh_entsize = bed->s->sizeof_rel; 1482 } 1483 else if (strncmp (asect->name, ".note", 5) == 0) 1484 this_hdr->sh_type = SHT_NOTE; 1485 else if (strncmp (asect->name, ".stab", 5) == 0 1486 && strcmp (asect->name + strlen (asect->name) - 3, "str") == 0) 1487 this_hdr->sh_type = SHT_STRTAB; 1488 else if (strcmp (asect->name, ".gnu.version") == 0) 1489 { 1490 this_hdr->sh_type = SHT_GNU_versym; 1491 this_hdr->sh_entsize = sizeof (Elf_External_Versym); 1492 } 1493 else if (strcmp (asect->name, ".gnu.version_d") == 0) 1494 { 1495 this_hdr->sh_type = SHT_GNU_verdef; 1496 this_hdr->sh_entsize = 0; 1497 /* objcopy or strip will copy over sh_info, but may not set 1498 cverdefs. The linker will set cverdefs, but sh_info will be 1499 zero. */ 1500 if (this_hdr->sh_info == 0) 1501 this_hdr->sh_info = elf_tdata (abfd)->cverdefs; 1502 else 1503 BFD_ASSERT (elf_tdata (abfd)->cverdefs == 0 1504 || this_hdr->sh_info == elf_tdata (abfd)->cverdefs); 1505 } 1506 else if (strcmp (asect->name, ".gnu.version_r") == 0) 1507 { 1508 this_hdr->sh_type = SHT_GNU_verneed; 1509 this_hdr->sh_entsize = 0; 1510 /* objcopy or strip will copy over sh_info, but may not set 1511 cverrefs. The linker will set cverrefs, but sh_info will be 1512 zero. */ 1513 if (this_hdr->sh_info == 0) 1514 this_hdr->sh_info = elf_tdata (abfd)->cverrefs; 1515 else 1516 BFD_ASSERT (elf_tdata (abfd)->cverrefs == 0 1517 || this_hdr->sh_info == elf_tdata (abfd)->cverrefs); 1518 } 1519 else if ((asect->flags & SEC_ALLOC) != 0 1520 && (asect->flags & SEC_LOAD) != 0) 1521 this_hdr->sh_type = SHT_PROGBITS; 1522 else if ((asect->flags & SEC_ALLOC) != 0 1523 && ((asect->flags & SEC_LOAD) == 0)) 1524 this_hdr->sh_type = SHT_NOBITS; 1525 else 1526 { 1527 /* Who knows? */ 1528 this_hdr->sh_type = SHT_PROGBITS; 1529 } 1530 1531 if ((asect->flags & SEC_ALLOC) != 0) 1532 this_hdr->sh_flags |= SHF_ALLOC; 1533 if ((asect->flags & SEC_READONLY) == 0) 1534 this_hdr->sh_flags |= SHF_WRITE; 1535 if ((asect->flags & SEC_CODE) != 0) 1536 this_hdr->sh_flags |= SHF_EXECINSTR; 1537 1538 /* Check for processor-specific section types. */ 1539 { 1540 struct elf_backend_data *bed = get_elf_backend_data (abfd); 1541 1542 if (bed->elf_backend_fake_sections) 1543 (*bed->elf_backend_fake_sections) (abfd, this_hdr, asect); 1544 } 1545 1546 /* If the section has relocs, set up a section header for the 1547 SHT_REL[A] section. */ 1548 if ((asect->flags & SEC_RELOC) != 0) 1549 { 1550 Elf_Internal_Shdr *rela_hdr; 1551 int use_rela_p = get_elf_backend_data (abfd)->use_rela_p; 1552 char *name; 1553 1554 rela_hdr = &elf_section_data (asect)->rel_hdr; 1555 name = bfd_alloc (abfd, sizeof ".rela" + strlen (asect->name)); 1556 if (name == NULL) 1557 { 1558 *failedptr = true; 1559 return; 1560 } 1561 sprintf (name, "%s%s", use_rela_p ? ".rela" : ".rel", asect->name); 1562 rela_hdr->sh_name = 1563 (unsigned int) _bfd_stringtab_add (elf_shstrtab (abfd), name, 1564 true, false); 1565 if (rela_hdr->sh_name == (unsigned int) -1) 1566 { 1567 *failedptr = true; 1568 return; 1569 } 1570 rela_hdr->sh_type = use_rela_p ? SHT_RELA : SHT_REL; 1571 rela_hdr->sh_entsize = (use_rela_p 1572 ? bed->s->sizeof_rela 1573 : bed->s->sizeof_rel); 1574 rela_hdr->sh_addralign = bed->s->file_align; 1575 rela_hdr->sh_flags = 0; 1576 rela_hdr->sh_addr = 0; 1577 rela_hdr->sh_size = 0; 1578 rela_hdr->sh_offset = 0; 1579 } 1580} 1581 1582/* Assign all ELF section numbers. The dummy first section is handled here 1583 too. The link/info pointers for the standard section types are filled 1584 in here too, while we're at it. */ 1585 1586static boolean 1587assign_section_numbers (abfd) 1588 bfd *abfd; 1589{ 1590 struct elf_obj_tdata *t = elf_tdata (abfd); 1591 asection *sec; 1592 unsigned int section_number; 1593 Elf_Internal_Shdr **i_shdrp; 1594 struct elf_backend_data *bed = get_elf_backend_data (abfd); 1595 1596 section_number = 1; 1597 1598 for (sec = abfd->sections; sec; sec = sec->next) 1599 { 1600 struct bfd_elf_section_data *d = elf_section_data (sec); 1601 1602 d->this_idx = section_number++; 1603 if ((sec->flags & SEC_RELOC) == 0) 1604 d->rel_idx = 0; 1605 else 1606 d->rel_idx = section_number++; 1607 } 1608 1609 t->shstrtab_section = section_number++; 1610 elf_elfheader (abfd)->e_shstrndx = t->shstrtab_section; 1611 t->shstrtab_hdr.sh_size = _bfd_stringtab_size (elf_shstrtab (abfd)); 1612 1613 if (abfd->symcount > 0) 1614 { 1615 t->symtab_section = section_number++; 1616 t->strtab_section = section_number++; 1617 } 1618 1619 elf_elfheader (abfd)->e_shnum = section_number; 1620 1621 /* Set up the list of section header pointers, in agreement with the 1622 indices. */ 1623 i_shdrp = ((Elf_Internal_Shdr **) 1624 bfd_alloc (abfd, section_number * sizeof (Elf_Internal_Shdr *))); 1625 if (i_shdrp == NULL) 1626 return false; 1627 1628 i_shdrp[0] = ((Elf_Internal_Shdr *) 1629 bfd_alloc (abfd, sizeof (Elf_Internal_Shdr))); 1630 if (i_shdrp[0] == NULL) 1631 { 1632 bfd_release (abfd, i_shdrp); 1633 return false; 1634 } 1635 memset (i_shdrp[0], 0, sizeof (Elf_Internal_Shdr)); 1636 1637 elf_elfsections (abfd) = i_shdrp; 1638 1639 i_shdrp[t->shstrtab_section] = &t->shstrtab_hdr; 1640 if (abfd->symcount > 0) 1641 { 1642 i_shdrp[t->symtab_section] = &t->symtab_hdr; 1643 i_shdrp[t->strtab_section] = &t->strtab_hdr; 1644 t->symtab_hdr.sh_link = t->strtab_section; 1645 } 1646 for (sec = abfd->sections; sec; sec = sec->next) 1647 { 1648 struct bfd_elf_section_data *d = elf_section_data (sec); 1649 asection *s; 1650 const char *name; 1651 1652 i_shdrp[d->this_idx] = &d->this_hdr; 1653 if (d->rel_idx != 0) 1654 i_shdrp[d->rel_idx] = &d->rel_hdr; 1655 1656 /* Fill in the sh_link and sh_info fields while we're at it. */ 1657 1658 /* sh_link of a reloc section is the section index of the symbol 1659 table. sh_info is the section index of the section to which 1660 the relocation entries apply. */ 1661 if (d->rel_idx != 0) 1662 { 1663 d->rel_hdr.sh_link = t->symtab_section; 1664 d->rel_hdr.sh_info = d->this_idx; 1665 } 1666 1667 switch (d->this_hdr.sh_type) 1668 { 1669 case SHT_REL: 1670 case SHT_RELA: 1671 /* A reloc section which we are treating as a normal BFD 1672 section. sh_link is the section index of the symbol 1673 table. sh_info is the section index of the section to 1674 which the relocation entries apply. We assume that an 1675 allocated reloc section uses the dynamic symbol table. 1676 FIXME: How can we be sure? */ 1677 s = bfd_get_section_by_name (abfd, ".dynsym"); 1678 if (s != NULL) 1679 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 1680 1681 /* We look up the section the relocs apply to by name. */ 1682 name = sec->name; 1683 if (d->this_hdr.sh_type == SHT_REL) 1684 name += 4; 1685 else 1686 name += 5; 1687 s = bfd_get_section_by_name (abfd, name); 1688 if (s != NULL) 1689 d->this_hdr.sh_info = elf_section_data (s)->this_idx; 1690 break; 1691 1692 case SHT_STRTAB: 1693 /* We assume that a section named .stab*str is a stabs 1694 string section. We look for a section with the same name 1695 but without the trailing ``str'', and set its sh_link 1696 field to point to this section. */ 1697 if (strncmp (sec->name, ".stab", sizeof ".stab" - 1) == 0 1698 && strcmp (sec->name + strlen (sec->name) - 3, "str") == 0) 1699 { 1700 size_t len; 1701 char *alc; 1702 1703 len = strlen (sec->name); 1704 alc = (char *) bfd_malloc (len - 2); 1705 if (alc == NULL) 1706 return false; 1707 strncpy (alc, sec->name, len - 3); 1708 alc[len - 3] = '\0'; 1709 s = bfd_get_section_by_name (abfd, alc); 1710 free (alc); 1711 if (s != NULL) 1712 { 1713 elf_section_data (s)->this_hdr.sh_link = d->this_idx; 1714 1715 /* This is a .stab section. */ 1716 elf_section_data (s)->this_hdr.sh_entsize = 1717 4 + 2 * (bed->s->arch_size / 8); 1718 } 1719 } 1720 break; 1721 1722 case SHT_DYNAMIC: 1723 case SHT_DYNSYM: 1724 case SHT_GNU_verneed: 1725 case SHT_GNU_verdef: 1726 /* sh_link is the section header index of the string table 1727 used for the dynamic entries, or the symbol table, or the 1728 version strings. */ 1729 s = bfd_get_section_by_name (abfd, ".dynstr"); 1730 if (s != NULL) 1731 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 1732 break; 1733 1734 case SHT_HASH: 1735 case SHT_GNU_versym: 1736 /* sh_link is the section header index of the symbol table 1737 this hash table or version table is for. */ 1738 s = bfd_get_section_by_name (abfd, ".dynsym"); 1739 if (s != NULL) 1740 d->this_hdr.sh_link = elf_section_data (s)->this_idx; 1741 break; 1742 } 1743 } 1744 1745 return true; 1746} 1747 1748/* Map symbol from it's internal number to the external number, moving 1749 all local symbols to be at the head of the list. */ 1750 1751static INLINE int 1752sym_is_global (abfd, sym) 1753 bfd *abfd; 1754 asymbol *sym; 1755{ 1756 /* If the backend has a special mapping, use it. */ 1757 if (get_elf_backend_data (abfd)->elf_backend_sym_is_global) 1758 return ((*get_elf_backend_data (abfd)->elf_backend_sym_is_global) 1759 (abfd, sym)); 1760 1761 return ((sym->flags & (BSF_GLOBAL | BSF_WEAK)) != 0 1762 || bfd_is_und_section (bfd_get_section (sym)) 1763 || bfd_is_com_section (bfd_get_section (sym))); 1764} 1765 1766static boolean 1767elf_map_symbols (abfd) 1768 bfd *abfd; 1769{ 1770 int symcount = bfd_get_symcount (abfd); 1771 asymbol **syms = bfd_get_outsymbols (abfd); 1772 asymbol **sect_syms; 1773 int num_locals = 0; 1774 int num_globals = 0; 1775 int num_locals2 = 0; 1776 int num_globals2 = 0; 1777 int max_index = 0; 1778 int num_sections = 0; 1779 int idx; 1780 asection *asect; 1781 asymbol **new_syms; 1782 1783#ifdef DEBUG 1784 fprintf (stderr, "elf_map_symbols\n"); 1785 fflush (stderr); 1786#endif 1787 1788 /* Add a section symbol for each BFD section. FIXME: Is this really 1789 necessary? */ 1790 for (asect = abfd->sections; asect; asect = asect->next) 1791 { 1792 if (max_index < asect->index) 1793 max_index = asect->index; 1794 } 1795 1796 max_index++; 1797 sect_syms = (asymbol **) bfd_zalloc (abfd, max_index * sizeof (asymbol *)); 1798 if (sect_syms == NULL) 1799 return false; 1800 elf_section_syms (abfd) = sect_syms; 1801 1802 for (idx = 0; idx < symcount; idx++) 1803 { 1804 if ((syms[idx]->flags & BSF_SECTION_SYM) != 0 1805 && (syms[idx]->value + syms[idx]->section->vma) == 0) 1806 { 1807 asection *sec; 1808 1809 sec = syms[idx]->section; 1810 if (sec->owner != NULL) 1811 { 1812 if (sec->owner != abfd) 1813 { 1814 if (sec->output_offset != 0) 1815 continue; 1816 sec = sec->output_section; 1817 BFD_ASSERT (sec->owner == abfd); 1818 } 1819 sect_syms[sec->index] = syms[idx]; 1820 } 1821 } 1822 } 1823 1824 for (asect = abfd->sections; asect; asect = asect->next) 1825 { 1826 asymbol *sym; 1827 1828 if (sect_syms[asect->index] != NULL) 1829 continue; 1830 1831 sym = bfd_make_empty_symbol (abfd); 1832 if (sym == NULL) 1833 return false; 1834 sym->the_bfd = abfd; 1835 sym->name = asect->name; 1836 sym->value = 0; 1837 /* Set the flags to 0 to indicate that this one was newly added. */ 1838 sym->flags = 0; 1839 sym->section = asect; 1840 sect_syms[asect->index] = sym; 1841 num_sections++; 1842#ifdef DEBUG 1843 fprintf (stderr, 1844 "creating section symbol, name = %s, value = 0x%.8lx, index = %d, section = 0x%.8lx\n", 1845 asect->name, (long) asect->vma, asect->index, (long) asect); 1846#endif 1847 } 1848 1849 /* Classify all of the symbols. */ 1850 for (idx = 0; idx < symcount; idx++) 1851 { 1852 if (!sym_is_global (abfd, syms[idx])) 1853 num_locals++; 1854 else 1855 num_globals++; 1856 } 1857 for (asect = abfd->sections; asect; asect = asect->next) 1858 { 1859 if (sect_syms[asect->index] != NULL 1860 && sect_syms[asect->index]->flags == 0) 1861 { 1862 sect_syms[asect->index]->flags = BSF_SECTION_SYM; 1863 if (!sym_is_global (abfd, sect_syms[asect->index])) 1864 num_locals++; 1865 else 1866 num_globals++; 1867 sect_syms[asect->index]->flags = 0; 1868 } 1869 } 1870 1871 /* Now sort the symbols so the local symbols are first. */ 1872 new_syms = ((asymbol **) 1873 bfd_alloc (abfd, 1874 (num_locals + num_globals) * sizeof (asymbol *))); 1875 if (new_syms == NULL) 1876 return false; 1877 1878 for (idx = 0; idx < symcount; idx++) 1879 { 1880 asymbol *sym = syms[idx]; 1881 int i; 1882 1883 if (!sym_is_global (abfd, sym)) 1884 i = num_locals2++; 1885 else 1886 i = num_locals + num_globals2++; 1887 new_syms[i] = sym; 1888 sym->udata.i = i + 1; 1889 } 1890 for (asect = abfd->sections; asect; asect = asect->next) 1891 { 1892 if (sect_syms[asect->index] != NULL 1893 && sect_syms[asect->index]->flags == 0) 1894 { 1895 asymbol *sym = sect_syms[asect->index]; 1896 int i; 1897 1898 sym->flags = BSF_SECTION_SYM; 1899 if (!sym_is_global (abfd, sym)) 1900 i = num_locals2++; 1901 else 1902 i = num_locals + num_globals2++; 1903 new_syms[i] = sym; 1904 sym->udata.i = i + 1; 1905 } 1906 } 1907 1908 bfd_set_symtab (abfd, new_syms, num_locals + num_globals); 1909 1910 elf_num_locals (abfd) = num_locals; 1911 elf_num_globals (abfd) = num_globals; 1912 return true; 1913} 1914 1915/* Align to the maximum file alignment that could be required for any 1916 ELF data structure. */ 1917 1918static INLINE file_ptr align_file_position PARAMS ((file_ptr, int)); 1919static INLINE file_ptr 1920align_file_position (off, align) 1921 file_ptr off; 1922 int align; 1923{ 1924 return (off + align - 1) & ~(align - 1); 1925} 1926 1927/* Assign a file position to a section, optionally aligning to the 1928 required section alignment. */ 1929 1930INLINE file_ptr 1931_bfd_elf_assign_file_position_for_section (i_shdrp, offset, align) 1932 Elf_Internal_Shdr *i_shdrp; 1933 file_ptr offset; 1934 boolean align; 1935{ 1936 if (align) 1937 { 1938 unsigned int al; 1939 1940 al = i_shdrp->sh_addralign; 1941 if (al > 1) 1942 offset = BFD_ALIGN (offset, al); 1943 } 1944 i_shdrp->sh_offset = offset; 1945 if (i_shdrp->bfd_section != NULL) 1946 i_shdrp->bfd_section->filepos = offset; 1947 if (i_shdrp->sh_type != SHT_NOBITS) 1948 offset += i_shdrp->sh_size; 1949 return offset; 1950} 1951 1952/* Compute the file positions we are going to put the sections at, and 1953 otherwise prepare to begin writing out the ELF file. If LINK_INFO 1954 is not NULL, this is being called by the ELF backend linker. */ 1955 1956boolean 1957_bfd_elf_compute_section_file_positions (abfd, link_info) 1958 bfd *abfd; 1959 struct bfd_link_info *link_info; 1960{ 1961 struct elf_backend_data *bed = get_elf_backend_data (abfd); 1962 boolean failed; 1963 struct bfd_strtab_hash *strtab; 1964 Elf_Internal_Shdr *shstrtab_hdr; 1965 1966 if (abfd->output_has_begun) 1967 return true; 1968 1969 /* Do any elf backend specific processing first. */ 1970 if (bed->elf_backend_begin_write_processing) 1971 (*bed->elf_backend_begin_write_processing) (abfd, link_info); 1972 1973 if (! prep_headers (abfd)) 1974 return false; 1975 1976 failed = false; 1977 bfd_map_over_sections (abfd, elf_fake_sections, &failed); 1978 if (failed) 1979 return false; 1980 1981 if (!assign_section_numbers (abfd)) 1982 return false; 1983 1984 /* The backend linker builds symbol table information itself. */ 1985 if (link_info == NULL && abfd->symcount > 0) 1986 { 1987 if (! swap_out_syms (abfd, &strtab)) 1988 return false; 1989 } 1990 1991 shstrtab_hdr = &elf_tdata (abfd)->shstrtab_hdr; 1992 /* sh_name was set in prep_headers. */ 1993 shstrtab_hdr->sh_type = SHT_STRTAB; 1994 shstrtab_hdr->sh_flags = 0; 1995 shstrtab_hdr->sh_addr = 0; 1996 shstrtab_hdr->sh_size = _bfd_stringtab_size (elf_shstrtab (abfd)); 1997 shstrtab_hdr->sh_entsize = 0; 1998 shstrtab_hdr->sh_link = 0; 1999 shstrtab_hdr->sh_info = 0; 2000 /* sh_offset is set in assign_file_positions_except_relocs. */ 2001 shstrtab_hdr->sh_addralign = 1; 2002 2003 if (!assign_file_positions_except_relocs (abfd)) 2004 return false; 2005 2006 if (link_info == NULL && abfd->symcount > 0) 2007 { 2008 file_ptr off; 2009 Elf_Internal_Shdr *hdr; 2010 2011 off = elf_tdata (abfd)->next_file_pos; 2012 2013 hdr = &elf_tdata (abfd)->symtab_hdr; 2014 off = _bfd_elf_assign_file_position_for_section (hdr, off, true); 2015 2016 hdr = &elf_tdata (abfd)->strtab_hdr; 2017 off = _bfd_elf_assign_file_position_for_section (hdr, off, true); 2018 2019 elf_tdata (abfd)->next_file_pos = off; 2020 2021 /* Now that we know where the .strtab section goes, write it 2022 out. */ 2023 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 2024 || ! _bfd_stringtab_emit (abfd, strtab)) 2025 return false; 2026 _bfd_stringtab_free (strtab); 2027 } 2028 2029 abfd->output_has_begun = true; 2030 2031 return true; 2032} 2033 2034/* Create a mapping from a set of sections to a program segment. */ 2035 2036static INLINE struct elf_segment_map * 2037make_mapping (abfd, sections, from, to, phdr) 2038 bfd *abfd; 2039 asection **sections; 2040 unsigned int from; 2041 unsigned int to; 2042 boolean phdr; 2043{ 2044 struct elf_segment_map *m; 2045 unsigned int i; 2046 asection **hdrpp; 2047 2048 m = ((struct elf_segment_map *) 2049 bfd_zalloc (abfd, 2050 (sizeof (struct elf_segment_map) 2051 + (to - from - 1) * sizeof (asection *)))); 2052 if (m == NULL) 2053 return NULL; 2054 m->next = NULL; 2055 m->p_type = PT_LOAD; 2056 for (i = from, hdrpp = sections + from; i < to; i++, hdrpp++) 2057 m->sections[i - from] = *hdrpp; 2058 m->count = to - from; 2059 2060 if (from == 0 && phdr) 2061 { 2062 /* Include the headers in the first PT_LOAD segment. */ 2063 m->includes_filehdr = 1; 2064 m->includes_phdrs = 1; 2065 } 2066 2067 return m; 2068} 2069 2070/* Set up a mapping from BFD sections to program segments. */ 2071 2072static boolean 2073map_sections_to_segments (abfd) 2074 bfd *abfd; 2075{ 2076 asection **sections = NULL; 2077 asection *s; 2078 unsigned int i; 2079 unsigned int count; 2080 struct elf_segment_map *mfirst; 2081 struct elf_segment_map **pm; 2082 struct elf_segment_map *m; 2083 asection *last_hdr; 2084 unsigned int phdr_index; 2085 bfd_vma maxpagesize; 2086 asection **hdrpp; 2087 boolean phdr_in_section = true; 2088 boolean writable; 2089 asection *dynsec; 2090 2091 if (elf_tdata (abfd)->segment_map != NULL) 2092 return true; 2093 2094 if (bfd_count_sections (abfd) == 0) 2095 return true; 2096 2097 /* Select the allocated sections, and sort them. */ 2098 2099 sections = (asection **) bfd_malloc (bfd_count_sections (abfd) 2100 * sizeof (asection *)); 2101 if (sections == NULL) 2102 goto error_return; 2103 2104 i = 0; 2105 for (s = abfd->sections; s != NULL; s = s->next) 2106 { 2107 if ((s->flags & SEC_ALLOC) != 0) 2108 { 2109 sections[i] = s; 2110 ++i; 2111 } 2112 } 2113 BFD_ASSERT (i <= bfd_count_sections (abfd)); 2114 count = i; 2115 2116 qsort (sections, (size_t) count, sizeof (asection *), elf_sort_sections); 2117 2118 /* Build the mapping. */ 2119 2120 mfirst = NULL; 2121 pm = &mfirst; 2122 2123 /* If we have a .interp section, then create a PT_PHDR segment for 2124 the program headers and a PT_INTERP segment for the .interp 2125 section. */ 2126 s = bfd_get_section_by_name (abfd, ".interp"); 2127 if (s != NULL && (s->flags & SEC_LOAD) != 0) 2128 { 2129 m = ((struct elf_segment_map *) 2130 bfd_zalloc (abfd, sizeof (struct elf_segment_map))); 2131 if (m == NULL) 2132 goto error_return; 2133 m->next = NULL; 2134 m->p_type = PT_PHDR; 2135 /* FIXME: UnixWare and Solaris set PF_X, Irix 5 does not. */ 2136 m->p_flags = PF_R | PF_X; 2137 m->p_flags_valid = 1; 2138 m->includes_phdrs = 1; 2139 2140 *pm = m; 2141 pm = &m->next; 2142 2143 m = ((struct elf_segment_map *) 2144 bfd_zalloc (abfd, sizeof (struct elf_segment_map))); 2145 if (m == NULL) 2146 goto error_return; 2147 m->next = NULL; 2148 m->p_type = PT_INTERP; 2149 m->count = 1; 2150 m->sections[0] = s; 2151 2152 *pm = m; 2153 pm = &m->next; 2154 } 2155 2156 /* Look through the sections. We put sections in the same program 2157 segment when the start of the second section can be placed within 2158 a few bytes of the end of the first section. */ 2159 last_hdr = NULL; 2160 phdr_index = 0; 2161 maxpagesize = get_elf_backend_data (abfd)->maxpagesize; 2162 writable = false; 2163 dynsec = bfd_get_section_by_name (abfd, ".dynamic"); 2164 if (dynsec != NULL 2165 && (dynsec->flags & SEC_LOAD) == 0) 2166 dynsec = NULL; 2167 2168 /* Deal with -Ttext or something similar such that the first section 2169 is not adjacent to the program headers. This is an 2170 approximation, since at this point we don't know exactly how many 2171 program headers we will need. */ 2172 if (count > 0) 2173 { 2174 bfd_size_type phdr_size; 2175 2176 phdr_size = elf_tdata (abfd)->program_header_size; 2177 if (phdr_size == 0) 2178 phdr_size = get_elf_backend_data (abfd)->s->sizeof_phdr; 2179 if ((abfd->flags & D_PAGED) == 0 2180 || sections[0]->lma % maxpagesize < phdr_size % maxpagesize) 2181 phdr_in_section = false; 2182 } 2183 2184 for (i = 0, hdrpp = sections; i < count; i++, hdrpp++) 2185 { 2186 asection *hdr; 2187 boolean new_segment; 2188 2189 hdr = *hdrpp; 2190 2191 /* See if this section and the last one will fit in the same 2192 segment. */ 2193 2194 if (last_hdr == NULL) 2195 { 2196 /* If we don't have a segment yet, then we don't need a new 2197 one (we build the last one after this loop). */ 2198 new_segment = false; 2199 } 2200 else if (last_hdr->lma - last_hdr->vma != hdr->lma - hdr->vma) 2201 { 2202 /* If this section has a different relation between the 2203 virtual address and the load address, then we need a new 2204 segment. */ 2205 new_segment = true; 2206 } 2207 else if (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize) 2208 < BFD_ALIGN (hdr->lma, maxpagesize)) 2209 { 2210 /* If putting this section in this segment would force us to 2211 skip a page in the segment, then we need a new segment. */ 2212 new_segment = true; 2213 } 2214 else if ((last_hdr->flags & SEC_LOAD) == 0 2215 && (hdr->flags & SEC_LOAD) != 0) 2216 { 2217 /* We don't want to put a loadable section after a 2218 nonloadable section in the same segment. */ 2219 new_segment = true; 2220 } 2221 else if ((abfd->flags & D_PAGED) == 0) 2222 { 2223 /* If the file is not demand paged, which means that we 2224 don't require the sections to be correctly aligned in the 2225 file, then there is no other reason for a new segment. */ 2226 new_segment = false; 2227 } 2228 else if (! writable 2229 && (hdr->flags & SEC_READONLY) == 0 2230 && (BFD_ALIGN (last_hdr->lma + last_hdr->_raw_size, maxpagesize) 2231 == hdr->lma)) 2232 { 2233 /* We don't want to put a writable section in a read only 2234 segment, unless they are on the same page in memory 2235 anyhow. We already know that the last section does not 2236 bring us past the current section on the page, so the 2237 only case in which the new section is not on the same 2238 page as the previous section is when the previous section 2239 ends precisely on a page boundary. */ 2240 new_segment = true; 2241 } 2242 else 2243 { 2244 /* Otherwise, we can use the same segment. */ 2245 new_segment = false; 2246 } 2247 2248 if (! new_segment) 2249 { 2250 if ((hdr->flags & SEC_READONLY) == 0) 2251 writable = true; 2252 last_hdr = hdr; 2253 continue; 2254 } 2255 2256 /* We need a new program segment. We must create a new program 2257 header holding all the sections from phdr_index until hdr. */ 2258 2259 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section); 2260 if (m == NULL) 2261 goto error_return; 2262 2263 *pm = m; 2264 pm = &m->next; 2265 2266 if ((hdr->flags & SEC_READONLY) == 0) 2267 writable = true; 2268 else 2269 writable = false; 2270 2271 last_hdr = hdr; 2272 phdr_index = i; 2273 phdr_in_section = false; 2274 } 2275 2276 /* Create a final PT_LOAD program segment. */ 2277 if (last_hdr != NULL) 2278 { 2279 m = make_mapping (abfd, sections, phdr_index, i, phdr_in_section); 2280 if (m == NULL) 2281 goto error_return; 2282 2283 *pm = m; 2284 pm = &m->next; 2285 } 2286 2287 /* If there is a .dynamic section, throw in a PT_DYNAMIC segment. */ 2288 if (dynsec != NULL) 2289 { 2290 m = ((struct elf_segment_map *) 2291 bfd_zalloc (abfd, sizeof (struct elf_segment_map))); 2292 if (m == NULL) 2293 goto error_return; 2294 m->next = NULL; 2295 m->p_type = PT_DYNAMIC; 2296 m->count = 1; 2297 m->sections[0] = dynsec; 2298 2299 *pm = m; 2300 pm = &m->next; 2301 } 2302 2303 /* For each loadable .note section, add a PT_NOTE segment. We don't 2304 use bfd_get_section_by_name, because if we link together 2305 nonloadable .note sections and loadable .note sections, we will 2306 generate two .note sections in the output file. FIXME: Using 2307 names for section types is bogus anyhow. */ 2308 for (s = abfd->sections; s != NULL; s = s->next) 2309 { 2310 if ((s->flags & SEC_LOAD) != 0 2311 && strncmp (s->name, ".note", 5) == 0) 2312 { 2313 m = ((struct elf_segment_map *) 2314 bfd_zalloc (abfd, sizeof (struct elf_segment_map))); 2315 if (m == NULL) 2316 goto error_return; 2317 m->next = NULL; 2318 m->p_type = PT_NOTE; 2319 m->count = 1; 2320 m->sections[0] = s; 2321 2322 *pm = m; 2323 pm = &m->next; 2324 } 2325 } 2326 2327 free (sections); 2328 sections = NULL; 2329 2330 elf_tdata (abfd)->segment_map = mfirst; 2331 return true; 2332 2333 error_return: 2334 if (sections != NULL) 2335 free (sections); 2336 return false; 2337} 2338 2339/* Sort sections by VMA. */ 2340 2341static int 2342elf_sort_sections (arg1, arg2) 2343 const PTR arg1; 2344 const PTR arg2; 2345{ 2346 const asection *sec1 = *(const asection **) arg1; 2347 const asection *sec2 = *(const asection **) arg2; 2348 2349 if (sec1->vma < sec2->vma) 2350 return -1; 2351 else if (sec1->vma > sec2->vma) 2352 return 1; 2353 2354 /* Sort by LMA. Normally the LMA and the VMA will be the same, and 2355 this will do nothing. */ 2356 if (sec1->lma < sec2->lma) 2357 return -1; 2358 else if (sec1->lma > sec2->lma) 2359 return 1; 2360 2361 /* Put !SEC_LOAD sections after SEC_LOAD ones. */ 2362 2363#define TOEND(x) (((x)->flags & SEC_LOAD) == 0) 2364 2365 if (TOEND (sec1)) 2366 { 2367 if (TOEND (sec2)) 2368 return sec1->target_index - sec2->target_index; 2369 else 2370 return 1; 2371 } 2372 2373 if (TOEND (sec2)) 2374 return -1; 2375 2376#undef TOEND 2377 2378 /* Sort by size, to put zero sized sections before others at the 2379 same address. */ 2380 2381 if (sec1->_raw_size < sec2->_raw_size) 2382 return -1; 2383 if (sec1->_raw_size > sec2->_raw_size) 2384 return 1; 2385 2386 return sec1->target_index - sec2->target_index; 2387} 2388 2389/* Assign file positions to the sections based on the mapping from 2390 sections to segments. This function also sets up some fields in 2391 the file header, and writes out the program headers. */ 2392 2393static boolean 2394assign_file_positions_for_segments (abfd) 2395 bfd *abfd; 2396{ 2397 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 2398 unsigned int count; 2399 struct elf_segment_map *m; 2400 unsigned int alloc; 2401 Elf_Internal_Phdr *phdrs; 2402 file_ptr off, voff; 2403 bfd_vma filehdr_vaddr, filehdr_paddr; 2404 bfd_vma phdrs_vaddr, phdrs_paddr; 2405 Elf_Internal_Phdr *p; 2406 2407 if (elf_tdata (abfd)->segment_map == NULL) 2408 { 2409 if (! map_sections_to_segments (abfd)) 2410 return false; 2411 } 2412 2413 if (bed->elf_backend_modify_segment_map) 2414 { 2415 if (! (*bed->elf_backend_modify_segment_map) (abfd)) 2416 return false; 2417 } 2418 2419 count = 0; 2420 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) 2421 ++count; 2422 2423 elf_elfheader (abfd)->e_phoff = bed->s->sizeof_ehdr; 2424 elf_elfheader (abfd)->e_phentsize = bed->s->sizeof_phdr; 2425 elf_elfheader (abfd)->e_phnum = count; 2426 2427 if (count == 0) 2428 return true; 2429 2430 /* If we already counted the number of program segments, make sure 2431 that we allocated enough space. This happens when SIZEOF_HEADERS 2432 is used in a linker script. */ 2433 alloc = elf_tdata (abfd)->program_header_size / bed->s->sizeof_phdr; 2434 if (alloc != 0 && count > alloc) 2435 { 2436 ((*_bfd_error_handler) 2437 ("%s: Not enough room for program headers (allocated %u, need %u)", 2438 bfd_get_filename (abfd), alloc, count)); 2439 bfd_set_error (bfd_error_bad_value); 2440 return false; 2441 } 2442 2443 if (alloc == 0) 2444 alloc = count; 2445 2446 phdrs = ((Elf_Internal_Phdr *) 2447 bfd_alloc (abfd, alloc * sizeof (Elf_Internal_Phdr))); 2448 if (phdrs == NULL) 2449 return false; 2450 2451 off = bed->s->sizeof_ehdr; 2452 off += alloc * bed->s->sizeof_phdr; 2453 2454 filehdr_vaddr = 0; 2455 filehdr_paddr = 0; 2456 phdrs_vaddr = 0; 2457 phdrs_paddr = 0; 2458 for (m = elf_tdata (abfd)->segment_map, p = phdrs; 2459 m != NULL; 2460 m = m->next, p++) 2461 { 2462 unsigned int i; 2463 asection **secpp; 2464 2465 /* If elf_segment_map is not from map_sections_to_segments, the 2466 sections may not be correctly ordered. */ 2467 if (m->count > 0) 2468 qsort (m->sections, (size_t) m->count, sizeof (asection *), 2469 elf_sort_sections); 2470 2471 p->p_type = m->p_type; 2472 2473 if (m->p_flags_valid) 2474 p->p_flags = m->p_flags; 2475 else 2476 p->p_flags = 0; 2477 2478 if (p->p_type == PT_LOAD 2479 && m->count > 0 2480 && (m->sections[0]->flags & SEC_ALLOC) != 0) 2481 { 2482 if ((abfd->flags & D_PAGED) != 0) 2483 off += (m->sections[0]->vma - off) % bed->maxpagesize; 2484 else 2485 off += ((m->sections[0]->vma - off) 2486 % (1 << bfd_get_section_alignment (abfd, m->sections[0]))); 2487 } 2488 2489 if (m->count == 0) 2490 p->p_vaddr = 0; 2491 else 2492 p->p_vaddr = m->sections[0]->vma; 2493 2494 if (m->p_paddr_valid) 2495 p->p_paddr = m->p_paddr; 2496 else if (m->count == 0) 2497 p->p_paddr = 0; 2498 else 2499 p->p_paddr = m->sections[0]->lma; 2500 2501 if (p->p_type == PT_LOAD 2502 && (abfd->flags & D_PAGED) != 0) 2503 p->p_align = bed->maxpagesize; 2504 else if (m->count == 0) 2505 p->p_align = bed->s->file_align; 2506 else 2507 p->p_align = 0; 2508 2509 p->p_offset = 0; 2510 p->p_filesz = 0; 2511 p->p_memsz = 0; 2512 2513 if (m->includes_filehdr) 2514 { 2515 if (! m->p_flags_valid) 2516 p->p_flags |= PF_R; 2517 p->p_offset = 0; 2518 p->p_filesz = bed->s->sizeof_ehdr; 2519 p->p_memsz = bed->s->sizeof_ehdr; 2520 if (m->count > 0) 2521 { 2522 BFD_ASSERT (p->p_type == PT_LOAD); 2523 2524 if (p->p_vaddr < (bfd_vma) off) 2525 { 2526 _bfd_error_handler ("%s: Not enough room for program headers, try linking with -N", 2527 bfd_get_filename (abfd)); 2528 bfd_set_error (bfd_error_bad_value); 2529 return false; 2530 } 2531 2532 p->p_vaddr -= off; 2533 if (! m->p_paddr_valid) 2534 p->p_paddr -= off; 2535 } 2536 if (p->p_type == PT_LOAD) 2537 { 2538 filehdr_vaddr = p->p_vaddr; 2539 filehdr_paddr = p->p_paddr; 2540 } 2541 } 2542 2543 if (m->includes_phdrs) 2544 { 2545 if (! m->p_flags_valid) 2546 p->p_flags |= PF_R; 2547 if (m->includes_filehdr) 2548 { 2549 if (p->p_type == PT_LOAD) 2550 { 2551 phdrs_vaddr = p->p_vaddr + bed->s->sizeof_ehdr; 2552 phdrs_paddr = p->p_paddr + bed->s->sizeof_ehdr; 2553 } 2554 } 2555 else 2556 { 2557 p->p_offset = bed->s->sizeof_ehdr; 2558 if (m->count > 0) 2559 { 2560 BFD_ASSERT (p->p_type == PT_LOAD); 2561 p->p_vaddr -= off - p->p_offset; 2562 if (! m->p_paddr_valid) 2563 p->p_paddr -= off - p->p_offset; 2564 } 2565 if (p->p_type == PT_LOAD) 2566 { 2567 phdrs_vaddr = p->p_vaddr; 2568 phdrs_paddr = p->p_paddr; 2569 } 2570 } 2571 p->p_filesz += alloc * bed->s->sizeof_phdr; 2572 p->p_memsz += alloc * bed->s->sizeof_phdr; 2573 } 2574 2575 if (p->p_type == PT_LOAD) 2576 { 2577 if (! m->includes_filehdr && ! m->includes_phdrs) 2578 p->p_offset = off; 2579 else 2580 { 2581 file_ptr adjust; 2582 2583 adjust = off - (p->p_offset + p->p_filesz); 2584 p->p_filesz += adjust; 2585 p->p_memsz += adjust; 2586 } 2587 } 2588 2589 voff = off; 2590 for (i = 0, secpp = m->sections; i < m->count; i++, secpp++) 2591 { 2592 asection *sec; 2593 flagword flags; 2594 bfd_size_type align; 2595 2596 sec = *secpp; 2597 flags = sec->flags; 2598 align = 1 << bfd_get_section_alignment (abfd, sec); 2599 2600 if (p->p_type == PT_LOAD) 2601 { 2602 bfd_vma adjust; 2603 2604 if ((flags & SEC_LOAD) != 0) 2605 adjust = sec->lma - (p->p_paddr + p->p_memsz); 2606 else if ((flags & SEC_ALLOC) != 0) 2607 { 2608 /* The section VMA must equal the file position 2609 modulo the page size. FIXME: I'm not sure if 2610 this adjustment is really necessary. We used to 2611 not have the SEC_LOAD case just above, and then 2612 this was necessary, but now I'm not sure. */ 2613 if ((abfd->flags & D_PAGED) != 0) 2614 adjust = (sec->vma - voff) % bed->maxpagesize; 2615 else 2616 adjust = (sec->vma - voff) % align; 2617 } 2618 else 2619 adjust = 0; 2620 2621 if (adjust != 0) 2622 { 2623 if (i == 0) 2624 abort (); 2625 p->p_memsz += adjust; 2626 off += adjust; 2627 voff += adjust; 2628 if ((flags & SEC_LOAD) != 0) 2629 p->p_filesz += adjust; 2630 } 2631 2632 sec->filepos = off; 2633 2634 /* We check SEC_HAS_CONTENTS here because if NOLOAD is 2635 used in a linker script we may have a section with 2636 SEC_LOAD clear but which is supposed to have 2637 contents. */ 2638 if ((flags & SEC_LOAD) != 0 2639 || (flags & SEC_HAS_CONTENTS) != 0) 2640 off += sec->_raw_size; 2641 if ((flags & SEC_ALLOC) != 0) 2642 voff += sec->_raw_size; 2643 } 2644 2645 p->p_memsz += sec->_raw_size; 2646 2647 if ((flags & SEC_LOAD) != 0) 2648 p->p_filesz += sec->_raw_size; 2649 2650 if (align > p->p_align) 2651 p->p_align = align; 2652 2653 if (! m->p_flags_valid) 2654 { 2655 p->p_flags |= PF_R; 2656 if ((flags & SEC_CODE) != 0) 2657 p->p_flags |= PF_X; 2658 if ((flags & SEC_READONLY) == 0) 2659 p->p_flags |= PF_W; 2660 } 2661 } 2662 } 2663 2664 /* Now that we have set the section file positions, we can set up 2665 the file positions for the non PT_LOAD segments. */ 2666 for (m = elf_tdata (abfd)->segment_map, p = phdrs; 2667 m != NULL; 2668 m = m->next, p++) 2669 { 2670 if (p->p_type != PT_LOAD && m->count > 0) 2671 { 2672 BFD_ASSERT (! m->includes_filehdr && ! m->includes_phdrs); 2673 p->p_offset = m->sections[0]->filepos; 2674 } 2675 if (m->count == 0) 2676 { 2677 if (m->includes_filehdr) 2678 { 2679 p->p_vaddr = filehdr_vaddr; 2680 if (! m->p_paddr_valid) 2681 p->p_paddr = filehdr_paddr; 2682 } 2683 else if (m->includes_phdrs) 2684 { 2685 p->p_vaddr = phdrs_vaddr; 2686 if (! m->p_paddr_valid) 2687 p->p_paddr = phdrs_paddr; 2688 } 2689 } 2690 } 2691 2692 /* Clear out any program headers we allocated but did not use. */ 2693 for (; count < alloc; count++, p++) 2694 { 2695 memset (p, 0, sizeof *p); 2696 p->p_type = PT_NULL; 2697 } 2698 2699 elf_tdata (abfd)->phdr = phdrs; 2700 2701 elf_tdata (abfd)->next_file_pos = off; 2702 2703 /* Write out the program headers. */ 2704 if (bfd_seek (abfd, bed->s->sizeof_ehdr, SEEK_SET) != 0 2705 || bed->s->write_out_phdrs (abfd, phdrs, alloc) != 0) 2706 return false; 2707 2708 return true; 2709} 2710 2711/* Get the size of the program header. 2712 2713 If this is called by the linker before any of the section VMA's are set, it 2714 can't calculate the correct value for a strange memory layout. This only 2715 happens when SIZEOF_HEADERS is used in a linker script. In this case, 2716 SORTED_HDRS is NULL and we assume the normal scenario of one text and one 2717 data segment (exclusive of .interp and .dynamic). 2718 2719 ??? User written scripts must either not use SIZEOF_HEADERS, or assume there 2720 will be two segments. */ 2721 2722static bfd_size_type 2723get_program_header_size (abfd) 2724 bfd *abfd; 2725{ 2726 size_t segs; 2727 asection *s; 2728 struct elf_backend_data *bed = get_elf_backend_data (abfd); 2729 2730 /* We can't return a different result each time we're called. */ 2731 if (elf_tdata (abfd)->program_header_size != 0) 2732 return elf_tdata (abfd)->program_header_size; 2733 2734 if (elf_tdata (abfd)->segment_map != NULL) 2735 { 2736 struct elf_segment_map *m; 2737 2738 segs = 0; 2739 for (m = elf_tdata (abfd)->segment_map; m != NULL; m = m->next) 2740 ++segs; 2741 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; 2742 return elf_tdata (abfd)->program_header_size; 2743 } 2744 2745 /* Assume we will need exactly two PT_LOAD segments: one for text 2746 and one for data. */ 2747 segs = 2; 2748 2749 s = bfd_get_section_by_name (abfd, ".interp"); 2750 if (s != NULL && (s->flags & SEC_LOAD) != 0) 2751 { 2752 /* If we have a loadable interpreter section, we need a 2753 PT_INTERP segment. In this case, assume we also need a 2754 PT_PHDR segment, although that may not be true for all 2755 targets. */ 2756 segs += 2; 2757 } 2758 2759 if (bfd_get_section_by_name (abfd, ".dynamic") != NULL) 2760 { 2761 /* We need a PT_DYNAMIC segment. */ 2762 ++segs; 2763 } 2764 2765 for (s = abfd->sections; s != NULL; s = s->next) 2766 { 2767 if ((s->flags & SEC_LOAD) != 0 2768 && strncmp (s->name, ".note", 5) == 0) 2769 { 2770 /* We need a PT_NOTE segment. */ 2771 ++segs; 2772 } 2773 } 2774 2775 /* Let the backend count up any program headers it might need. */ 2776 if (bed->elf_backend_additional_program_headers) 2777 { 2778 int a; 2779 2780 a = (*bed->elf_backend_additional_program_headers) (abfd); 2781 if (a == -1) 2782 abort (); 2783 segs += a; 2784 } 2785 2786 elf_tdata (abfd)->program_header_size = segs * bed->s->sizeof_phdr; 2787 return elf_tdata (abfd)->program_header_size; 2788} 2789 2790/* Work out the file positions of all the sections. This is called by 2791 _bfd_elf_compute_section_file_positions. All the section sizes and 2792 VMAs must be known before this is called. 2793 2794 We do not consider reloc sections at this point, unless they form 2795 part of the loadable image. Reloc sections are assigned file 2796 positions in assign_file_positions_for_relocs, which is called by 2797 write_object_contents and final_link. 2798 2799 We also don't set the positions of the .symtab and .strtab here. */ 2800 2801static boolean 2802assign_file_positions_except_relocs (abfd) 2803 bfd *abfd; 2804{ 2805 struct elf_obj_tdata * const tdata = elf_tdata (abfd); 2806 Elf_Internal_Ehdr * const i_ehdrp = elf_elfheader (abfd); 2807 Elf_Internal_Shdr ** const i_shdrpp = elf_elfsections (abfd); 2808 file_ptr off; 2809 struct elf_backend_data *bed = get_elf_backend_data (abfd); 2810 2811 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) 2812 { 2813 Elf_Internal_Shdr **hdrpp; 2814 unsigned int i; 2815 2816 /* Start after the ELF header. */ 2817 off = i_ehdrp->e_ehsize; 2818 2819 /* We are not creating an executable, which means that we are 2820 not creating a program header, and that the actual order of 2821 the sections in the file is unimportant. */ 2822 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++) 2823 { 2824 Elf_Internal_Shdr *hdr; 2825 2826 hdr = *hdrpp; 2827 if (hdr->sh_type == SHT_REL || hdr->sh_type == SHT_RELA) 2828 { 2829 hdr->sh_offset = -1; 2830 continue; 2831 } 2832 if (i == tdata->symtab_section 2833 || i == tdata->strtab_section) 2834 { 2835 hdr->sh_offset = -1; 2836 continue; 2837 } 2838 2839 off = _bfd_elf_assign_file_position_for_section (hdr, off, true); 2840 } 2841 } 2842 else 2843 { 2844 unsigned int i; 2845 Elf_Internal_Shdr **hdrpp; 2846 2847 /* Assign file positions for the loaded sections based on the 2848 assignment of sections to segments. */ 2849 if (! assign_file_positions_for_segments (abfd)) 2850 return false; 2851 2852 /* Assign file positions for the other sections. */ 2853 2854 off = elf_tdata (abfd)->next_file_pos; 2855 for (i = 1, hdrpp = i_shdrpp + 1; i < i_ehdrp->e_shnum; i++, hdrpp++) 2856 { 2857 Elf_Internal_Shdr *hdr; 2858 2859 hdr = *hdrpp; 2860 if (hdr->bfd_section != NULL 2861 && hdr->bfd_section->filepos != 0) 2862 hdr->sh_offset = hdr->bfd_section->filepos; 2863 else if ((hdr->sh_flags & SHF_ALLOC) != 0) 2864 { 2865 ((*_bfd_error_handler) 2866 ("%s: warning: allocated section `%s' not in segment", 2867 bfd_get_filename (abfd), 2868 (hdr->bfd_section == NULL 2869 ? "*unknown*" 2870 : hdr->bfd_section->name))); 2871 if ((abfd->flags & D_PAGED) != 0) 2872 off += (hdr->sh_addr - off) % bed->maxpagesize; 2873 else 2874 off += (hdr->sh_addr - off) % hdr->sh_addralign; 2875 off = _bfd_elf_assign_file_position_for_section (hdr, off, 2876 false); 2877 } 2878 else if (hdr->sh_type == SHT_REL 2879 || hdr->sh_type == SHT_RELA 2880 || hdr == i_shdrpp[tdata->symtab_section] 2881 || hdr == i_shdrpp[tdata->strtab_section]) 2882 hdr->sh_offset = -1; 2883 else 2884 off = _bfd_elf_assign_file_position_for_section (hdr, off, true); 2885 } 2886 } 2887 2888 /* Place the section headers. */ 2889 off = align_file_position (off, bed->s->file_align); 2890 i_ehdrp->e_shoff = off; 2891 off += i_ehdrp->e_shnum * i_ehdrp->e_shentsize; 2892 2893 elf_tdata (abfd)->next_file_pos = off; 2894 2895 return true; 2896} 2897 2898static boolean 2899prep_headers (abfd) 2900 bfd *abfd; 2901{ 2902 Elf_Internal_Ehdr *i_ehdrp; /* Elf file header, internal form */ 2903 Elf_Internal_Phdr *i_phdrp = 0; /* Program header table, internal form */ 2904 Elf_Internal_Shdr **i_shdrp; /* Section header table, internal form */ 2905 int count; 2906 struct bfd_strtab_hash *shstrtab; 2907 struct elf_backend_data *bed = get_elf_backend_data (abfd); 2908 2909 i_ehdrp = elf_elfheader (abfd); 2910 i_shdrp = elf_elfsections (abfd); 2911 2912 shstrtab = _bfd_elf_stringtab_init (); 2913 if (shstrtab == NULL) 2914 return false; 2915 2916 elf_shstrtab (abfd) = shstrtab; 2917 2918 i_ehdrp->e_ident[EI_MAG0] = ELFMAG0; 2919 i_ehdrp->e_ident[EI_MAG1] = ELFMAG1; 2920 i_ehdrp->e_ident[EI_MAG2] = ELFMAG2; 2921 i_ehdrp->e_ident[EI_MAG3] = ELFMAG3; 2922 2923 i_ehdrp->e_ident[EI_CLASS] = bed->s->elfclass; 2924 i_ehdrp->e_ident[EI_DATA] = 2925 bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB; 2926 i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current; 2927 2928#ifdef __FreeBSD__ 2929 /* Quick and dirty hack to brand the file as a FreeBSD ELF file. */ 2930 i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_FREEBSD; 2931 i_ehdrp->e_ident[EI_ABIVERSION] = 0; 2932#endif 2933 2934 for (count = EI_PAD; count < EI_NIDENT; count++) 2935 i_ehdrp->e_ident[count] = 0; 2936 2937#ifdef __FreeBSD__ 2938/* #ifdef BRANDELF_CHANGE_BOOTSTRAP */ 2939#define _OLD_EI_BRAND_OFFSET 8 2940#define _OLD_BRANDING "FreeBSD" 2941 strncpy((char *) &i_ehdrp->e_ident[_OLD_EI_BRAND_OFFSET], _OLD_BRANDING, 2942 EI_NIDENT-_OLD_EI_BRAND_OFFSET); 2943/* #endif */ 2944#endif 2945 2946 if ((abfd->flags & DYNAMIC) != 0) 2947 i_ehdrp->e_type = ET_DYN; 2948 else if ((abfd->flags & EXEC_P) != 0) 2949 i_ehdrp->e_type = ET_EXEC; 2950 else 2951 i_ehdrp->e_type = ET_REL; 2952 2953 switch (bfd_get_arch (abfd)) 2954 { 2955 case bfd_arch_unknown: 2956 i_ehdrp->e_machine = EM_NONE; 2957 break; 2958 case bfd_arch_sparc: 2959 if (bed->s->arch_size == 64) 2960 i_ehdrp->e_machine = EM_SPARCV9; 2961 else 2962 i_ehdrp->e_machine = EM_SPARC; 2963 break; 2964 case bfd_arch_i386: 2965 i_ehdrp->e_machine = EM_386; 2966 break; 2967 case bfd_arch_m68k: 2968 i_ehdrp->e_machine = EM_68K; 2969 break; 2970 case bfd_arch_m88k: 2971 i_ehdrp->e_machine = EM_88K; 2972 break; 2973 case bfd_arch_i860: 2974 i_ehdrp->e_machine = EM_860; 2975 break; 2976 case bfd_arch_mips: /* MIPS Rxxxx */ 2977 i_ehdrp->e_machine = EM_MIPS; /* only MIPS R3000 */ 2978 break; 2979 case bfd_arch_hppa: 2980 i_ehdrp->e_machine = EM_PARISC; 2981 break; 2982 case bfd_arch_powerpc: 2983 i_ehdrp->e_machine = EM_PPC; 2984 break; 2985 case bfd_arch_alpha: 2986 i_ehdrp->e_machine = EM_ALPHA; 2987 break; 2988 case bfd_arch_sh: 2989 i_ehdrp->e_machine = EM_SH; 2990 break; 2991 case bfd_arch_d10v: 2992 i_ehdrp->e_machine = EM_CYGNUS_D10V; 2993 break; 2994 case bfd_arch_v850: 2995 switch (bfd_get_mach (abfd)) 2996 { 2997 default: 2998 case 0: i_ehdrp->e_machine = EM_CYGNUS_V850; break; 2999 } 3000 break; 3001 case bfd_arch_arc: 3002 i_ehdrp->e_machine = EM_CYGNUS_ARC; 3003 break; 3004 case bfd_arch_m32r: 3005 i_ehdrp->e_machine = EM_CYGNUS_M32R; 3006 break; 3007 case bfd_arch_mn10200: 3008 i_ehdrp->e_machine = EM_CYGNUS_MN10200; 3009 break; 3010 case bfd_arch_mn10300: 3011 i_ehdrp->e_machine = EM_CYGNUS_MN10300; 3012 break; 3013 /* also note that EM_M32, AT&T WE32100 is unknown to bfd */ 3014 default: 3015 i_ehdrp->e_machine = EM_NONE; 3016 } 3017 i_ehdrp->e_version = bed->s->ev_current; 3018 i_ehdrp->e_ehsize = bed->s->sizeof_ehdr; 3019 3020 /* no program header, for now. */ 3021 i_ehdrp->e_phoff = 0; 3022 i_ehdrp->e_phentsize = 0; 3023 i_ehdrp->e_phnum = 0; 3024 3025 /* each bfd section is section header entry */ 3026 i_ehdrp->e_entry = bfd_get_start_address (abfd); 3027 i_ehdrp->e_shentsize = bed->s->sizeof_shdr; 3028 3029 /* if we're building an executable, we'll need a program header table */ 3030 if (abfd->flags & EXEC_P) 3031 { 3032 /* it all happens later */ 3033#if 0 3034 i_ehdrp->e_phentsize = sizeof (Elf_External_Phdr); 3035 3036 /* elf_build_phdrs() returns a (NULL-terminated) array of 3037 Elf_Internal_Phdrs */ 3038 i_phdrp = elf_build_phdrs (abfd, i_ehdrp, i_shdrp, &i_ehdrp->e_phnum); 3039 i_ehdrp->e_phoff = outbase; 3040 outbase += i_ehdrp->e_phentsize * i_ehdrp->e_phnum; 3041#endif 3042 } 3043 else 3044 { 3045 i_ehdrp->e_phentsize = 0; 3046 i_phdrp = 0; 3047 i_ehdrp->e_phoff = 0; 3048 } 3049 3050 elf_tdata (abfd)->symtab_hdr.sh_name = 3051 (unsigned int) _bfd_stringtab_add (shstrtab, ".symtab", true, false); 3052 elf_tdata (abfd)->strtab_hdr.sh_name = 3053 (unsigned int) _bfd_stringtab_add (shstrtab, ".strtab", true, false); 3054 elf_tdata (abfd)->shstrtab_hdr.sh_name = 3055 (unsigned int) _bfd_stringtab_add (shstrtab, ".shstrtab", true, false); 3056 if (elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 3057 || elf_tdata (abfd)->symtab_hdr.sh_name == (unsigned int) -1 3058 || elf_tdata (abfd)->shstrtab_hdr.sh_name == (unsigned int) -1) 3059 return false; 3060 3061 return true; 3062} 3063 3064/* Assign file positions for all the reloc sections which are not part 3065 of the loadable file image. */ 3066 3067void 3068_bfd_elf_assign_file_positions_for_relocs (abfd) 3069 bfd *abfd; 3070{ 3071 file_ptr off; 3072 unsigned int i; 3073 Elf_Internal_Shdr **shdrpp; 3074 3075 off = elf_tdata (abfd)->next_file_pos; 3076 3077 for (i = 1, shdrpp = elf_elfsections (abfd) + 1; 3078 i < elf_elfheader (abfd)->e_shnum; 3079 i++, shdrpp++) 3080 { 3081 Elf_Internal_Shdr *shdrp; 3082 3083 shdrp = *shdrpp; 3084 if ((shdrp->sh_type == SHT_REL || shdrp->sh_type == SHT_RELA) 3085 && shdrp->sh_offset == -1) 3086 off = _bfd_elf_assign_file_position_for_section (shdrp, off, true); 3087 } 3088 3089 elf_tdata (abfd)->next_file_pos = off; 3090} 3091 3092boolean 3093_bfd_elf_write_object_contents (abfd) 3094 bfd *abfd; 3095{ 3096 struct elf_backend_data *bed = get_elf_backend_data (abfd); 3097 Elf_Internal_Ehdr *i_ehdrp; 3098 Elf_Internal_Shdr **i_shdrp; 3099 boolean failed; 3100 unsigned int count; 3101 3102 if (! abfd->output_has_begun 3103 && ! _bfd_elf_compute_section_file_positions (abfd, 3104 (struct bfd_link_info *) NULL)) 3105 return false; 3106 3107 i_shdrp = elf_elfsections (abfd); 3108 i_ehdrp = elf_elfheader (abfd); 3109 3110 failed = false; 3111 bfd_map_over_sections (abfd, bed->s->write_relocs, &failed); 3112 if (failed) 3113 return false; 3114 _bfd_elf_assign_file_positions_for_relocs (abfd); 3115 3116 /* After writing the headers, we need to write the sections too... */ 3117 for (count = 1; count < i_ehdrp->e_shnum; count++) 3118 { 3119 if (bed->elf_backend_section_processing) 3120 (*bed->elf_backend_section_processing) (abfd, i_shdrp[count]); 3121 if (i_shdrp[count]->contents) 3122 { 3123 if (bfd_seek (abfd, i_shdrp[count]->sh_offset, SEEK_SET) != 0 3124 || (bfd_write (i_shdrp[count]->contents, i_shdrp[count]->sh_size, 3125 1, abfd) 3126 != i_shdrp[count]->sh_size)) 3127 return false; 3128 } 3129 } 3130 3131 /* Write out the section header names. */ 3132 if (bfd_seek (abfd, elf_tdata (abfd)->shstrtab_hdr.sh_offset, SEEK_SET) != 0 3133 || ! _bfd_stringtab_emit (abfd, elf_shstrtab (abfd))) 3134 return false; 3135 3136 if (bed->elf_backend_final_write_processing) 3137 (*bed->elf_backend_final_write_processing) (abfd, 3138 elf_tdata (abfd)->linker); 3139 3140 return bed->s->write_shdrs_and_ehdr (abfd); 3141} 3142 3143/* given a section, search the header to find them... */ 3144int 3145_bfd_elf_section_from_bfd_section (abfd, asect) 3146 bfd *abfd; 3147 struct sec *asect; 3148{ 3149 struct elf_backend_data *bed = get_elf_backend_data (abfd); 3150 Elf_Internal_Shdr **i_shdrp = elf_elfsections (abfd); 3151 int index; 3152 Elf_Internal_Shdr *hdr; 3153 int maxindex = elf_elfheader (abfd)->e_shnum; 3154 3155 for (index = 0; index < maxindex; index++) 3156 { 3157 hdr = i_shdrp[index]; 3158 if (hdr->bfd_section == asect) 3159 return index; 3160 } 3161 3162 if (bed->elf_backend_section_from_bfd_section) 3163 { 3164 for (index = 0; index < maxindex; index++) 3165 { 3166 int retval; 3167 3168 hdr = i_shdrp[index]; 3169 retval = index; 3170 if ((*bed->elf_backend_section_from_bfd_section) 3171 (abfd, hdr, asect, &retval)) 3172 return retval; 3173 } 3174 } 3175 3176 if (bfd_is_abs_section (asect)) 3177 return SHN_ABS; 3178 if (bfd_is_com_section (asect)) 3179 return SHN_COMMON; 3180 if (bfd_is_und_section (asect)) 3181 return SHN_UNDEF; 3182 3183 return -1; 3184} 3185 3186/* Given a BFD symbol, return the index in the ELF symbol table, or -1 3187 on error. */ 3188 3189int 3190_bfd_elf_symbol_from_bfd_symbol (abfd, asym_ptr_ptr) 3191 bfd *abfd; 3192 asymbol **asym_ptr_ptr; 3193{ 3194 asymbol *asym_ptr = *asym_ptr_ptr; 3195 int idx; 3196 flagword flags = asym_ptr->flags; 3197 3198 /* When gas creates relocations against local labels, it creates its 3199 own symbol for the section, but does put the symbol into the 3200 symbol chain, so udata is 0. When the linker is generating 3201 relocatable output, this section symbol may be for one of the 3202 input sections rather than the output section. */ 3203 if (asym_ptr->udata.i == 0 3204 && (flags & BSF_SECTION_SYM) 3205 && asym_ptr->section) 3206 { 3207 int indx; 3208 3209 if (asym_ptr->section->output_section != NULL) 3210 indx = asym_ptr->section->output_section->index; 3211 else 3212 indx = asym_ptr->section->index; 3213 if (elf_section_syms (abfd)[indx]) 3214 asym_ptr->udata.i = elf_section_syms (abfd)[indx]->udata.i; 3215 } 3216 3217 idx = asym_ptr->udata.i; 3218 3219 if (idx == 0) 3220 { 3221 /* This case can occur when using --strip-symbol on a symbol 3222 which is used in a relocation entry. */ 3223 (*_bfd_error_handler) 3224 ("%s: symbol `%s' required but not present", 3225 bfd_get_filename (abfd), bfd_asymbol_name (asym_ptr)); 3226 bfd_set_error (bfd_error_no_symbols); 3227 return -1; 3228 } 3229 3230#if DEBUG & 4 3231 { 3232 fprintf (stderr, 3233 "elf_symbol_from_bfd_symbol 0x%.8lx, name = %s, sym num = %d, flags = 0x%.8lx%s\n", 3234 (long) asym_ptr, asym_ptr->name, idx, flags, 3235 elf_symbol_flags (flags)); 3236 fflush (stderr); 3237 } 3238#endif 3239 3240 return idx; 3241} 3242 3243/* Copy private BFD data. This copies any program header information. */ 3244 3245static boolean 3246copy_private_bfd_data (ibfd, obfd) 3247 bfd *ibfd; 3248 bfd *obfd; 3249{ 3250 Elf_Internal_Ehdr *iehdr; 3251 struct elf_segment_map *mfirst; 3252 struct elf_segment_map **pm; 3253 struct elf_segment_map *m; 3254 Elf_Internal_Phdr *p; 3255 unsigned int i, c; 3256 3257 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 3258 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 3259 return true; 3260 3261 if (elf_tdata (ibfd)->phdr == NULL) 3262 return true; 3263 3264 iehdr = elf_elfheader (ibfd); 3265 3266 mfirst = NULL; 3267 pm = &mfirst; 3268 3269 c = elf_elfheader (ibfd)->e_phnum; 3270 for (i = 0, p = elf_tdata (ibfd)->phdr; i < c; i++, p++) 3271 { 3272 unsigned int csecs; 3273 asection *s; 3274 unsigned int isec; 3275 3276 csecs = 0; 3277 3278 /* The complicated case when p_vaddr is 0 is to handle the 3279 Solaris linker, which generates a PT_INTERP section with 3280 p_vaddr and p_memsz set to 0. */ 3281 for (s = ibfd->sections; s != NULL; s = s->next) 3282 if (((s->vma >= p->p_vaddr 3283 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz 3284 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz)) 3285 || (p->p_vaddr == 0 3286 && p->p_filesz > 0 3287 && (s->flags & SEC_HAS_CONTENTS) != 0 3288 && (bfd_vma) s->filepos >= p->p_offset 3289 && ((bfd_vma) s->filepos + s->_raw_size 3290 <= p->p_offset + p->p_filesz))) 3291 && (s->flags & SEC_ALLOC) != 0 3292 && s->output_section != NULL) 3293 ++csecs; 3294 3295 m = ((struct elf_segment_map *) 3296 bfd_alloc (obfd, 3297 (sizeof (struct elf_segment_map) 3298 + ((size_t) csecs - 1) * sizeof (asection *)))); 3299 if (m == NULL) 3300 return false; 3301 3302 m->next = NULL; 3303 m->p_type = p->p_type; 3304 m->p_flags = p->p_flags; 3305 m->p_flags_valid = 1; 3306 m->p_paddr = p->p_paddr; 3307 m->p_paddr_valid = 1; 3308 3309 m->includes_filehdr = (p->p_offset == 0 3310 && p->p_filesz >= iehdr->e_ehsize); 3311 3312 m->includes_phdrs = (p->p_offset <= (bfd_vma) iehdr->e_phoff 3313 && (p->p_offset + p->p_filesz 3314 >= ((bfd_vma) iehdr->e_phoff 3315 + iehdr->e_phnum * iehdr->e_phentsize))); 3316 3317 isec = 0; 3318 for (s = ibfd->sections; s != NULL; s = s->next) 3319 { 3320 if (((s->vma >= p->p_vaddr 3321 && (s->vma + s->_raw_size <= p->p_vaddr + p->p_memsz 3322 || s->vma + s->_raw_size <= p->p_vaddr + p->p_filesz)) 3323 || (p->p_vaddr == 0 3324 && p->p_filesz > 0 3325 && (s->flags & SEC_HAS_CONTENTS) != 0 3326 && (bfd_vma) s->filepos >= p->p_offset 3327 && ((bfd_vma) s->filepos + s->_raw_size 3328 <= p->p_offset + p->p_filesz))) 3329 && (s->flags & SEC_ALLOC) != 0 3330 && s->output_section != NULL) 3331 { 3332 m->sections[isec] = s->output_section; 3333 ++isec; 3334 } 3335 } 3336 BFD_ASSERT (isec == csecs); 3337 m->count = csecs; 3338 3339 *pm = m; 3340 pm = &m->next; 3341 } 3342 3343 /* The Solaris linker creates program headers in which all the 3344 p_paddr fields are zero. When we try to objcopy or strip such a 3345 file, we get confused. Check for this case, and if we find it 3346 reset the p_paddr_valid fields. */ 3347 for (m = mfirst; m != NULL; m = m->next) 3348 if (m->p_paddr != 0) 3349 break; 3350 if (m == NULL) 3351 { 3352 for (m = mfirst; m != NULL; m = m->next) 3353 m->p_paddr_valid = 0; 3354 } 3355 3356 elf_tdata (obfd)->segment_map = mfirst; 3357 3358 return true; 3359} 3360 3361/* Copy private section information. This copies over the entsize 3362 field, and sometimes the info field. */ 3363 3364boolean 3365_bfd_elf_copy_private_section_data (ibfd, isec, obfd, osec) 3366 bfd *ibfd; 3367 asection *isec; 3368 bfd *obfd; 3369 asection *osec; 3370{ 3371 Elf_Internal_Shdr *ihdr, *ohdr; 3372 3373 if (ibfd->xvec->flavour != bfd_target_elf_flavour 3374 || obfd->xvec->flavour != bfd_target_elf_flavour) 3375 return true; 3376 3377 /* Copy over private BFD data if it has not already been copied. 3378 This must be done here, rather than in the copy_private_bfd_data 3379 entry point, because the latter is called after the section 3380 contents have been set, which means that the program headers have 3381 already been worked out. */ 3382 if (elf_tdata (obfd)->segment_map == NULL 3383 && elf_tdata (ibfd)->phdr != NULL) 3384 { 3385 asection *s; 3386 3387 /* Only set up the segments if there are no more SEC_ALLOC 3388 sections. FIXME: This won't do the right thing if objcopy is 3389 used to remove the last SEC_ALLOC section, since objcopy 3390 won't call this routine in that case. */ 3391 for (s = isec->next; s != NULL; s = s->next) 3392 if ((s->flags & SEC_ALLOC) != 0) 3393 break; 3394 if (s == NULL) 3395 { 3396 if (! copy_private_bfd_data (ibfd, obfd)) 3397 return false; 3398 } 3399 } 3400 3401 ihdr = &elf_section_data (isec)->this_hdr; 3402 ohdr = &elf_section_data (osec)->this_hdr; 3403 3404 ohdr->sh_entsize = ihdr->sh_entsize; 3405 3406 if (ihdr->sh_type == SHT_SYMTAB 3407 || ihdr->sh_type == SHT_DYNSYM 3408 || ihdr->sh_type == SHT_GNU_verneed 3409 || ihdr->sh_type == SHT_GNU_verdef) 3410 ohdr->sh_info = ihdr->sh_info; 3411 3412 return true; 3413} 3414 3415/* Copy private symbol information. If this symbol is in a section 3416 which we did not map into a BFD section, try to map the section 3417 index correctly. We use special macro definitions for the mapped 3418 section indices; these definitions are interpreted by the 3419 swap_out_syms function. */ 3420 3421#define MAP_ONESYMTAB (SHN_LORESERVE - 1) 3422#define MAP_DYNSYMTAB (SHN_LORESERVE - 2) 3423#define MAP_STRTAB (SHN_LORESERVE - 3) 3424#define MAP_SHSTRTAB (SHN_LORESERVE - 4) 3425 3426boolean 3427_bfd_elf_copy_private_symbol_data (ibfd, isymarg, obfd, osymarg) 3428 bfd *ibfd; 3429 asymbol *isymarg; 3430 bfd *obfd; 3431 asymbol *osymarg; 3432{ 3433 elf_symbol_type *isym, *osym; 3434 3435 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 3436 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 3437 return true; 3438 3439 isym = elf_symbol_from (ibfd, isymarg); 3440 osym = elf_symbol_from (obfd, osymarg); 3441 3442 if (isym != NULL 3443 && osym != NULL 3444 && bfd_is_abs_section (isym->symbol.section)) 3445 { 3446 unsigned int shndx; 3447 3448 shndx = isym->internal_elf_sym.st_shndx; 3449 if (shndx == elf_onesymtab (ibfd)) 3450 shndx = MAP_ONESYMTAB; 3451 else if (shndx == elf_dynsymtab (ibfd)) 3452 shndx = MAP_DYNSYMTAB; 3453 else if (shndx == elf_tdata (ibfd)->strtab_section) 3454 shndx = MAP_STRTAB; 3455 else if (shndx == elf_tdata (ibfd)->shstrtab_section) 3456 shndx = MAP_SHSTRTAB; 3457 osym->internal_elf_sym.st_shndx = shndx; 3458 } 3459 3460 return true; 3461} 3462 3463/* Swap out the symbols. */ 3464 3465static boolean 3466swap_out_syms (abfd, sttp) 3467 bfd *abfd; 3468 struct bfd_strtab_hash **sttp; 3469{ 3470 struct elf_backend_data *bed = get_elf_backend_data (abfd); 3471 3472 if (!elf_map_symbols (abfd)) 3473 return false; 3474 3475 /* Dump out the symtabs. */ 3476 { 3477 int symcount = bfd_get_symcount (abfd); 3478 asymbol **syms = bfd_get_outsymbols (abfd); 3479 struct bfd_strtab_hash *stt; 3480 Elf_Internal_Shdr *symtab_hdr; 3481 Elf_Internal_Shdr *symstrtab_hdr; 3482 char *outbound_syms; 3483 int idx; 3484 3485 stt = _bfd_elf_stringtab_init (); 3486 if (stt == NULL) 3487 return false; 3488 3489 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 3490 symtab_hdr->sh_type = SHT_SYMTAB; 3491 symtab_hdr->sh_entsize = bed->s->sizeof_sym; 3492 symtab_hdr->sh_size = symtab_hdr->sh_entsize * (symcount + 1); 3493 symtab_hdr->sh_info = elf_num_locals (abfd) + 1; 3494 symtab_hdr->sh_addralign = bed->s->file_align; 3495 3496 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; 3497 symstrtab_hdr->sh_type = SHT_STRTAB; 3498 3499 outbound_syms = bfd_alloc (abfd, 3500 (1 + symcount) * bed->s->sizeof_sym); 3501 if (outbound_syms == NULL) 3502 return false; 3503 symtab_hdr->contents = (PTR) outbound_syms; 3504 3505 /* now generate the data (for "contents") */ 3506 { 3507 /* Fill in zeroth symbol and swap it out. */ 3508 Elf_Internal_Sym sym; 3509 sym.st_name = 0; 3510 sym.st_value = 0; 3511 sym.st_size = 0; 3512 sym.st_info = 0; 3513 sym.st_other = 0; 3514 sym.st_shndx = SHN_UNDEF; 3515 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms); 3516 outbound_syms += bed->s->sizeof_sym; 3517 } 3518 for (idx = 0; idx < symcount; idx++) 3519 { 3520 Elf_Internal_Sym sym; 3521 bfd_vma value = syms[idx]->value; 3522 elf_symbol_type *type_ptr; 3523 flagword flags = syms[idx]->flags; 3524 int type; 3525 3526 if (flags & BSF_SECTION_SYM) 3527 /* Section symbols have no names. */ 3528 sym.st_name = 0; 3529 else 3530 { 3531 sym.st_name = (unsigned long) _bfd_stringtab_add (stt, 3532 syms[idx]->name, 3533 true, false); 3534 if (sym.st_name == (unsigned long) -1) 3535 return false; 3536 } 3537 3538 type_ptr = elf_symbol_from (abfd, syms[idx]); 3539 3540 if (bfd_is_com_section (syms[idx]->section)) 3541 { 3542 /* ELF common symbols put the alignment into the `value' field, 3543 and the size into the `size' field. This is backwards from 3544 how BFD handles it, so reverse it here. */ 3545 sym.st_size = value; 3546 if (type_ptr == NULL 3547 || type_ptr->internal_elf_sym.st_value == 0) 3548 sym.st_value = value >= 16 ? 16 : (1 << bfd_log2 (value)); 3549 else 3550 sym.st_value = type_ptr->internal_elf_sym.st_value; 3551 sym.st_shndx = _bfd_elf_section_from_bfd_section (abfd, 3552 syms[idx]->section); 3553 } 3554 else 3555 { 3556 asection *sec = syms[idx]->section; 3557 int shndx; 3558 3559 if (sec->output_section) 3560 { 3561 value += sec->output_offset; 3562 sec = sec->output_section; 3563 } 3564 value += sec->vma; 3565 sym.st_value = value; 3566 sym.st_size = type_ptr ? type_ptr->internal_elf_sym.st_size : 0; 3567 3568 if (bfd_is_abs_section (sec) 3569 && type_ptr != NULL 3570 && type_ptr->internal_elf_sym.st_shndx != 0) 3571 { 3572 /* This symbol is in a real ELF section which we did 3573 not create as a BFD section. Undo the mapping done 3574 by copy_private_symbol_data. */ 3575 shndx = type_ptr->internal_elf_sym.st_shndx; 3576 switch (shndx) 3577 { 3578 case MAP_ONESYMTAB: 3579 shndx = elf_onesymtab (abfd); 3580 break; 3581 case MAP_DYNSYMTAB: 3582 shndx = elf_dynsymtab (abfd); 3583 break; 3584 case MAP_STRTAB: 3585 shndx = elf_tdata (abfd)->strtab_section; 3586 break; 3587 case MAP_SHSTRTAB: 3588 shndx = elf_tdata (abfd)->shstrtab_section; 3589 break; 3590 default: 3591 break; 3592 } 3593 } 3594 else 3595 { 3596 shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 3597 3598 if (shndx == -1) 3599 { 3600 asection *sec2; 3601 3602 /* Writing this would be a hell of a lot easier if 3603 we had some decent documentation on bfd, and 3604 knew what to expect of the library, and what to 3605 demand of applications. For example, it 3606 appears that `objcopy' might not set the 3607 section of a symbol to be a section that is 3608 actually in the output file. */ 3609 sec2 = bfd_get_section_by_name (abfd, sec->name); 3610 BFD_ASSERT (sec2 != 0); 3611 shndx = _bfd_elf_section_from_bfd_section (abfd, sec2); 3612 BFD_ASSERT (shndx != -1); 3613 } 3614 } 3615 3616 sym.st_shndx = shndx; 3617 } 3618 3619 if ((flags & BSF_FUNCTION) != 0) 3620 type = STT_FUNC; 3621 else if ((flags & BSF_OBJECT) != 0) 3622 type = STT_OBJECT; 3623 else 3624 type = STT_NOTYPE; 3625 3626 if (bfd_is_com_section (syms[idx]->section)) 3627 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); 3628 else if (bfd_is_und_section (syms[idx]->section)) 3629 sym.st_info = ELF_ST_INFO (((flags & BSF_WEAK) 3630 ? STB_WEAK 3631 : STB_GLOBAL), 3632 type); 3633 else if (flags & BSF_SECTION_SYM) 3634 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 3635 else if (flags & BSF_FILE) 3636 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 3637 else 3638 { 3639 int bind = STB_LOCAL; 3640 3641 if (flags & BSF_LOCAL) 3642 bind = STB_LOCAL; 3643 else if (flags & BSF_WEAK) 3644 bind = STB_WEAK; 3645 else if (flags & BSF_GLOBAL) 3646 bind = STB_GLOBAL; 3647 3648 sym.st_info = ELF_ST_INFO (bind, type); 3649 } 3650 3651 if (type_ptr != NULL) 3652 sym.st_other = type_ptr->internal_elf_sym.st_other; 3653 else 3654 sym.st_other = 0; 3655 3656 bed->s->swap_symbol_out (abfd, &sym, (PTR) outbound_syms); 3657 outbound_syms += bed->s->sizeof_sym; 3658 } 3659 3660 *sttp = stt; 3661 symstrtab_hdr->sh_size = _bfd_stringtab_size (stt); 3662 symstrtab_hdr->sh_type = SHT_STRTAB; 3663 3664 symstrtab_hdr->sh_flags = 0; 3665 symstrtab_hdr->sh_addr = 0; 3666 symstrtab_hdr->sh_entsize = 0; 3667 symstrtab_hdr->sh_link = 0; 3668 symstrtab_hdr->sh_info = 0; 3669 symstrtab_hdr->sh_addralign = 1; 3670 } 3671 3672 return true; 3673} 3674 3675/* Return the number of bytes required to hold the symtab vector. 3676 3677 Note that we base it on the count plus 1, since we will null terminate 3678 the vector allocated based on this size. However, the ELF symbol table 3679 always has a dummy entry as symbol #0, so it ends up even. */ 3680 3681long 3682_bfd_elf_get_symtab_upper_bound (abfd) 3683 bfd *abfd; 3684{ 3685 long symcount; 3686 long symtab_size; 3687 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->symtab_hdr; 3688 3689 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 3690 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); 3691 3692 return symtab_size; 3693} 3694 3695long 3696_bfd_elf_get_dynamic_symtab_upper_bound (abfd) 3697 bfd *abfd; 3698{ 3699 long symcount; 3700 long symtab_size; 3701 Elf_Internal_Shdr *hdr = &elf_tdata (abfd)->dynsymtab_hdr; 3702 3703 if (elf_dynsymtab (abfd) == 0) 3704 { 3705 bfd_set_error (bfd_error_invalid_operation); 3706 return -1; 3707 } 3708 3709 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 3710 symtab_size = (symcount - 1 + 1) * (sizeof (asymbol *)); 3711 3712 return symtab_size; 3713} 3714 3715long 3716_bfd_elf_get_reloc_upper_bound (abfd, asect) 3717 bfd *abfd; 3718 sec_ptr asect; 3719{ 3720 return (asect->reloc_count + 1) * sizeof (arelent *); 3721} 3722 3723/* Canonicalize the relocs. */ 3724 3725long 3726_bfd_elf_canonicalize_reloc (abfd, section, relptr, symbols) 3727 bfd *abfd; 3728 sec_ptr section; 3729 arelent **relptr; 3730 asymbol **symbols; 3731{ 3732 arelent *tblptr; 3733 unsigned int i; 3734 3735 if (! get_elf_backend_data (abfd)->s->slurp_reloc_table (abfd, 3736 section, 3737 symbols, 3738 false)) 3739 return -1; 3740 3741 tblptr = section->relocation; 3742 for (i = 0; i < section->reloc_count; i++) 3743 *relptr++ = tblptr++; 3744 3745 *relptr = NULL; 3746 3747 return section->reloc_count; 3748} 3749 3750long 3751_bfd_elf_get_symtab (abfd, alocation) 3752 bfd *abfd; 3753 asymbol **alocation; 3754{ 3755 long symcount = get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, false); 3756 3757 if (symcount >= 0) 3758 bfd_get_symcount (abfd) = symcount; 3759 return symcount; 3760} 3761 3762long 3763_bfd_elf_canonicalize_dynamic_symtab (abfd, alocation) 3764 bfd *abfd; 3765 asymbol **alocation; 3766{ 3767 return get_elf_backend_data (abfd)->s->slurp_symbol_table (abfd, alocation, true); 3768} 3769 3770/* Return the size required for the dynamic reloc entries. Any 3771 section that was actually installed in the BFD, and has type 3772 SHT_REL or SHT_RELA, and uses the dynamic symbol table, is 3773 considered to be a dynamic reloc section. */ 3774 3775long 3776_bfd_elf_get_dynamic_reloc_upper_bound (abfd) 3777 bfd *abfd; 3778{ 3779 long ret; 3780 asection *s; 3781 3782 if (elf_dynsymtab (abfd) == 0) 3783 { 3784 bfd_set_error (bfd_error_invalid_operation); 3785 return -1; 3786 } 3787 3788 ret = sizeof (arelent *); 3789 for (s = abfd->sections; s != NULL; s = s->next) 3790 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 3791 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 3792 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) 3793 ret += ((s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize) 3794 * sizeof (arelent *)); 3795 3796 return ret; 3797} 3798 3799/* Canonicalize the dynamic relocation entries. Note that we return 3800 the dynamic relocations as a single block, although they are 3801 actually associated with particular sections; the interface, which 3802 was designed for SunOS style shared libraries, expects that there 3803 is only one set of dynamic relocs. Any section that was actually 3804 installed in the BFD, and has type SHT_REL or SHT_RELA, and uses 3805 the dynamic symbol table, is considered to be a dynamic reloc 3806 section. */ 3807 3808long 3809_bfd_elf_canonicalize_dynamic_reloc (abfd, storage, syms) 3810 bfd *abfd; 3811 arelent **storage; 3812 asymbol **syms; 3813{ 3814 boolean (*slurp_relocs) PARAMS ((bfd *, asection *, asymbol **, boolean)); 3815 asection *s; 3816 long ret; 3817 3818 if (elf_dynsymtab (abfd) == 0) 3819 { 3820 bfd_set_error (bfd_error_invalid_operation); 3821 return -1; 3822 } 3823 3824 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table; 3825 ret = 0; 3826 for (s = abfd->sections; s != NULL; s = s->next) 3827 { 3828 if (elf_section_data (s)->this_hdr.sh_link == elf_dynsymtab (abfd) 3829 && (elf_section_data (s)->this_hdr.sh_type == SHT_REL 3830 || elf_section_data (s)->this_hdr.sh_type == SHT_RELA)) 3831 { 3832 arelent *p; 3833 long count, i; 3834 3835 if (! (*slurp_relocs) (abfd, s, syms, true)) 3836 return -1; 3837 count = s->_raw_size / elf_section_data (s)->this_hdr.sh_entsize; 3838 p = s->relocation; 3839 for (i = 0; i < count; i++) 3840 *storage++ = p++; 3841 ret += count; 3842 } 3843 } 3844 3845 *storage = NULL; 3846 3847 return ret; 3848} 3849 3850/* Read in the version information. */ 3851 3852boolean 3853_bfd_elf_slurp_version_tables (abfd) 3854 bfd *abfd; 3855{ 3856 bfd_byte *contents = NULL; 3857 3858 if (elf_dynverdef (abfd) != 0) 3859 { 3860 Elf_Internal_Shdr *hdr; 3861 Elf_External_Verdef *everdef; 3862 Elf_Internal_Verdef *iverdef; 3863 unsigned int i; 3864 3865 hdr = &elf_tdata (abfd)->dynverdef_hdr; 3866 3867 elf_tdata (abfd)->verdef = 3868 ((Elf_Internal_Verdef *) 3869 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef))); 3870 if (elf_tdata (abfd)->verdef == NULL) 3871 goto error_return; 3872 3873 elf_tdata (abfd)->cverdefs = hdr->sh_info; 3874 3875 contents = (bfd_byte *) bfd_malloc (hdr->sh_size); 3876 if (contents == NULL) 3877 goto error_return; 3878 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 3879 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size) 3880 goto error_return; 3881 3882 everdef = (Elf_External_Verdef *) contents; 3883 iverdef = elf_tdata (abfd)->verdef; 3884 for (i = 0; i < hdr->sh_info; i++, iverdef++) 3885 { 3886 Elf_External_Verdaux *everdaux; 3887 Elf_Internal_Verdaux *iverdaux; 3888 unsigned int j; 3889 3890 _bfd_elf_swap_verdef_in (abfd, everdef, iverdef); 3891 3892 iverdef->vd_bfd = abfd; 3893 3894 iverdef->vd_auxptr = ((Elf_Internal_Verdaux *) 3895 bfd_alloc (abfd, 3896 (iverdef->vd_cnt 3897 * sizeof (Elf_Internal_Verdaux)))); 3898 if (iverdef->vd_auxptr == NULL) 3899 goto error_return; 3900 3901 everdaux = ((Elf_External_Verdaux *) 3902 ((bfd_byte *) everdef + iverdef->vd_aux)); 3903 iverdaux = iverdef->vd_auxptr; 3904 for (j = 0; j < iverdef->vd_cnt; j++, iverdaux++) 3905 { 3906 _bfd_elf_swap_verdaux_in (abfd, everdaux, iverdaux); 3907 3908 iverdaux->vda_nodename = 3909 bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 3910 iverdaux->vda_name); 3911 if (iverdaux->vda_nodename == NULL) 3912 goto error_return; 3913 3914 if (j + 1 < iverdef->vd_cnt) 3915 iverdaux->vda_nextptr = iverdaux + 1; 3916 else 3917 iverdaux->vda_nextptr = NULL; 3918 3919 everdaux = ((Elf_External_Verdaux *) 3920 ((bfd_byte *) everdaux + iverdaux->vda_next)); 3921 } 3922 3923 iverdef->vd_nodename = iverdef->vd_auxptr->vda_nodename; 3924 3925 if (i + 1 < hdr->sh_info) 3926 iverdef->vd_nextdef = iverdef + 1; 3927 else 3928 iverdef->vd_nextdef = NULL; 3929 3930 everdef = ((Elf_External_Verdef *) 3931 ((bfd_byte *) everdef + iverdef->vd_next)); 3932 } 3933 3934 free (contents); 3935 contents = NULL; 3936 } 3937 3938 if (elf_dynverref (abfd) != 0) 3939 { 3940 Elf_Internal_Shdr *hdr; 3941 Elf_External_Verneed *everneed; 3942 Elf_Internal_Verneed *iverneed; 3943 unsigned int i; 3944 3945 hdr = &elf_tdata (abfd)->dynverref_hdr; 3946 3947 elf_tdata (abfd)->verref = 3948 ((Elf_Internal_Verneed *) 3949 bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verneed))); 3950 if (elf_tdata (abfd)->verref == NULL) 3951 goto error_return; 3952 3953 elf_tdata (abfd)->cverrefs = hdr->sh_info; 3954 3955 contents = (bfd_byte *) bfd_malloc (hdr->sh_size); 3956 if (contents == NULL) 3957 goto error_return; 3958 if (bfd_seek (abfd, hdr->sh_offset, SEEK_SET) != 0 3959 || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size) 3960 goto error_return; 3961 3962 everneed = (Elf_External_Verneed *) contents; 3963 iverneed = elf_tdata (abfd)->verref; 3964 for (i = 0; i < hdr->sh_info; i++, iverneed++) 3965 { 3966 Elf_External_Vernaux *evernaux; 3967 Elf_Internal_Vernaux *ivernaux; 3968 unsigned int j; 3969 3970 _bfd_elf_swap_verneed_in (abfd, everneed, iverneed); 3971 3972 iverneed->vn_bfd = abfd; 3973 3974 iverneed->vn_filename = 3975 bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 3976 iverneed->vn_file); 3977 if (iverneed->vn_filename == NULL) 3978 goto error_return; 3979 3980 iverneed->vn_auxptr = 3981 ((Elf_Internal_Vernaux *) 3982 bfd_alloc (abfd, 3983 iverneed->vn_cnt * sizeof (Elf_Internal_Vernaux))); 3984 3985 evernaux = ((Elf_External_Vernaux *) 3986 ((bfd_byte *) everneed + iverneed->vn_aux)); 3987 ivernaux = iverneed->vn_auxptr; 3988 for (j = 0; j < iverneed->vn_cnt; j++, ivernaux++) 3989 { 3990 _bfd_elf_swap_vernaux_in (abfd, evernaux, ivernaux); 3991 3992 ivernaux->vna_nodename = 3993 bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 3994 ivernaux->vna_name); 3995 if (ivernaux->vna_nodename == NULL) 3996 goto error_return; 3997 3998 if (j + 1 < iverneed->vn_cnt) 3999 ivernaux->vna_nextptr = ivernaux + 1; 4000 else 4001 ivernaux->vna_nextptr = NULL; 4002 4003 evernaux = ((Elf_External_Vernaux *) 4004 ((bfd_byte *) evernaux + ivernaux->vna_next)); 4005 } 4006 4007 if (i + 1 < hdr->sh_info) 4008 iverneed->vn_nextref = iverneed + 1; 4009 else 4010 iverneed->vn_nextref = NULL; 4011 4012 everneed = ((Elf_External_Verneed *) 4013 ((bfd_byte *) everneed + iverneed->vn_next)); 4014 } 4015 4016 free (contents); 4017 contents = NULL; 4018 } 4019 4020 return true; 4021 4022 error_return: 4023 if (contents == NULL) 4024 free (contents); 4025 return false; 4026} 4027 4028asymbol * 4029_bfd_elf_make_empty_symbol (abfd) 4030 bfd *abfd; 4031{ 4032 elf_symbol_type *newsym; 4033 4034 newsym = (elf_symbol_type *) bfd_zalloc (abfd, sizeof (elf_symbol_type)); 4035 if (!newsym) 4036 return NULL; 4037 else 4038 { 4039 newsym->symbol.the_bfd = abfd; 4040 return &newsym->symbol; 4041 } 4042} 4043 4044void 4045_bfd_elf_get_symbol_info (ignore_abfd, symbol, ret) 4046 bfd *ignore_abfd; 4047 asymbol *symbol; 4048 symbol_info *ret; 4049{ 4050 bfd_symbol_info (symbol, ret); 4051} 4052 4053/* Return whether a symbol name implies a local symbol. Most targets 4054 use this function for the is_local_label_name entry point, but some 4055 override it. */ 4056 4057boolean 4058_bfd_elf_is_local_label_name (abfd, name) 4059 bfd *abfd; 4060 const char *name; 4061{ 4062 /* Normal local symbols start with ``.L''. */ 4063 if (name[0] == '.' && name[1] == 'L') 4064 return true; 4065 4066 /* At least some SVR4 compilers (e.g., UnixWare 2.1 cc) generate 4067 DWARF debugging symbols starting with ``..''. */ 4068 if (name[0] == '.' && name[1] == '.') 4069 return true; 4070 4071 /* gcc will sometimes generate symbols beginning with ``_.L_'' when 4072 emitting DWARF debugging output. I suspect this is actually a 4073 small bug in gcc (it calls ASM_OUTPUT_LABEL when it should call 4074 ASM_GENERATE_INTERNAL_LABEL, and this causes the leading 4075 underscore to be emitted on some ELF targets). For ease of use, 4076 we treat such symbols as local. */ 4077 if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_') 4078 return true; 4079 4080 return false; 4081} 4082 4083alent * 4084_bfd_elf_get_lineno (ignore_abfd, symbol) 4085 bfd *ignore_abfd; 4086 asymbol *symbol; 4087{ 4088 abort (); 4089 return NULL; 4090} 4091 4092boolean 4093_bfd_elf_set_arch_mach (abfd, arch, machine) 4094 bfd *abfd; 4095 enum bfd_architecture arch; 4096 unsigned long machine; 4097{ 4098 /* If this isn't the right architecture for this backend, and this 4099 isn't the generic backend, fail. */ 4100 if (arch != get_elf_backend_data (abfd)->arch 4101 && arch != bfd_arch_unknown 4102 && get_elf_backend_data (abfd)->arch != bfd_arch_unknown) 4103 return false; 4104 4105 return bfd_default_set_arch_mach (abfd, arch, machine); 4106} 4107 4108/* Find the nearest line to a particular section and offset, for error 4109 reporting. */ 4110 4111boolean 4112_bfd_elf_find_nearest_line (abfd, 4113 section, 4114 symbols, 4115 offset, 4116 filename_ptr, 4117 functionname_ptr, 4118 line_ptr) 4119 bfd *abfd; 4120 asection *section; 4121 asymbol **symbols; 4122 bfd_vma offset; 4123 CONST char **filename_ptr; 4124 CONST char **functionname_ptr; 4125 unsigned int *line_ptr; 4126{ 4127 boolean found; 4128 const char *filename; 4129 asymbol *func; 4130 bfd_vma low_func; 4131 asymbol **p; 4132 4133 if (_bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset, 4134 filename_ptr, functionname_ptr, 4135 line_ptr)) 4136 return true; 4137 4138 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, 4139 &found, filename_ptr, 4140 functionname_ptr, line_ptr, 4141 &elf_tdata (abfd)->line_info)) 4142 return false; 4143 if (found) 4144 return true; 4145 4146 if (symbols == NULL) 4147 return false; 4148 4149 filename = NULL; 4150 func = NULL; 4151 low_func = 0; 4152 4153 for (p = symbols; *p != NULL; p++) 4154 { 4155 elf_symbol_type *q; 4156 4157 q = (elf_symbol_type *) *p; 4158 4159 if (bfd_get_section (&q->symbol) != section) 4160 continue; 4161 4162 switch (ELF_ST_TYPE (q->internal_elf_sym.st_info)) 4163 { 4164 default: 4165 break; 4166 case STT_FILE: 4167 filename = bfd_asymbol_name (&q->symbol); 4168 break; 4169 case STT_FUNC: 4170 if (q->symbol.section == section 4171 && q->symbol.value >= low_func 4172 && q->symbol.value <= offset) 4173 { 4174 func = (asymbol *) q; 4175 low_func = q->symbol.value; 4176 } 4177 break; 4178 } 4179 } 4180 4181 if (func == NULL) 4182 return false; 4183 4184 *filename_ptr = filename; 4185 *functionname_ptr = bfd_asymbol_name (func); 4186 *line_ptr = 0; 4187 return true; 4188} 4189 4190int 4191_bfd_elf_sizeof_headers (abfd, reloc) 4192 bfd *abfd; 4193 boolean reloc; 4194{ 4195 int ret; 4196 4197 ret = get_elf_backend_data (abfd)->s->sizeof_ehdr; 4198 if (! reloc) 4199 ret += get_program_header_size (abfd); 4200 return ret; 4201} 4202 4203boolean 4204_bfd_elf_set_section_contents (abfd, section, location, offset, count) 4205 bfd *abfd; 4206 sec_ptr section; 4207 PTR location; 4208 file_ptr offset; 4209 bfd_size_type count; 4210{ 4211 Elf_Internal_Shdr *hdr; 4212 4213 if (! abfd->output_has_begun 4214 && ! _bfd_elf_compute_section_file_positions (abfd, 4215 (struct bfd_link_info *) NULL)) 4216 return false; 4217 4218 hdr = &elf_section_data (section)->this_hdr; 4219 4220 if (bfd_seek (abfd, hdr->sh_offset + offset, SEEK_SET) == -1) 4221 return false; 4222 if (bfd_write (location, 1, count, abfd) != count) 4223 return false; 4224 4225 return true; 4226} 4227 4228void 4229_bfd_elf_no_info_to_howto (abfd, cache_ptr, dst) 4230 bfd *abfd; 4231 arelent *cache_ptr; 4232 Elf_Internal_Rela *dst; 4233{ 4234 abort (); 4235} 4236 4237#if 0 4238void 4239_bfd_elf_no_info_to_howto_rel (abfd, cache_ptr, dst) 4240 bfd *abfd; 4241 arelent *cache_ptr; 4242 Elf_Internal_Rel *dst; 4243{ 4244 abort (); 4245} 4246#endif 4247 4248/* Try to convert a non-ELF reloc into an ELF one. */ 4249 4250boolean 4251_bfd_elf_validate_reloc (abfd, areloc) 4252 bfd *abfd; 4253 arelent *areloc; 4254{ 4255 /* Check whether we really have an ELF howto. */ 4256 4257 if ((*areloc->sym_ptr_ptr)->the_bfd->xvec != abfd->xvec) 4258 { 4259 bfd_reloc_code_real_type code; 4260 reloc_howto_type *howto; 4261 4262 /* Alien reloc: Try to determine its type to replace it with an 4263 equivalent ELF reloc. */ 4264 4265 if (areloc->howto->pc_relative) 4266 { 4267 switch (areloc->howto->bitsize) 4268 { 4269 case 8: 4270 code = BFD_RELOC_8_PCREL; 4271 break; 4272 case 12: 4273 code = BFD_RELOC_12_PCREL; 4274 break; 4275 case 16: 4276 code = BFD_RELOC_16_PCREL; 4277 break; 4278 case 24: 4279 code = BFD_RELOC_24_PCREL; 4280 break; 4281 case 32: 4282 code = BFD_RELOC_32_PCREL; 4283 break; 4284 case 64: 4285 code = BFD_RELOC_64_PCREL; 4286 break; 4287 default: 4288 goto fail; 4289 } 4290 4291 howto = bfd_reloc_type_lookup (abfd, code); 4292 4293 if (areloc->howto->pcrel_offset != howto->pcrel_offset) 4294 { 4295 if (howto->pcrel_offset) 4296 areloc->addend += areloc->address; 4297 else 4298 areloc->addend -= areloc->address; /* addend is unsigned!! */ 4299 } 4300 } 4301 else 4302 { 4303 switch (areloc->howto->bitsize) 4304 { 4305 case 8: 4306 code = BFD_RELOC_8; 4307 break; 4308 case 14: 4309 code = BFD_RELOC_14; 4310 break; 4311 case 16: 4312 code = BFD_RELOC_16; 4313 break; 4314 case 26: 4315 code = BFD_RELOC_26; 4316 break; 4317 case 32: 4318 code = BFD_RELOC_32; 4319 break; 4320 case 64: 4321 code = BFD_RELOC_64; 4322 break; 4323 default: 4324 goto fail; 4325 } 4326 4327 howto = bfd_reloc_type_lookup (abfd, code); 4328 } 4329 4330 if (howto) 4331 areloc->howto = howto; 4332 else 4333 goto fail; 4334 } 4335 4336 return true; 4337 4338 fail: 4339 (*_bfd_error_handler) 4340 ("%s: unsupported relocation type %s", 4341 bfd_get_filename (abfd), areloc->howto->name); 4342 bfd_set_error (bfd_error_bad_value); 4343 return false; 4344} 4345 4346boolean 4347_bfd_elf_close_and_cleanup (abfd) 4348 bfd *abfd; 4349{ 4350 if (bfd_get_format (abfd) == bfd_object) 4351 { 4352 if (elf_shstrtab (abfd) != NULL) 4353 _bfd_stringtab_free (elf_shstrtab (abfd)); 4354 } 4355 4356 return _bfd_generic_close_and_cleanup (abfd); 4357} 4358