1/* Copyright (C) 2001, 2002, 2003, 2004 Red Hat, Inc. 2 Written by Ulrich Drepper <drepper@redhat.com>, 2001. 3 4 This program is Open Source software; you can redistribute it and/or 5 modify it under the terms of the Open Software License version 1.0 as 6 published by the Open Source Initiative. 7 8 You should have received a copy of the Open Software License along 9 with this program; if not, you may obtain a copy of the Open Software 10 License version 1.0 from http://www.opensource.org/licenses/osl.php or 11 by writing the Open Source Initiative c/o Lawrence Rosen, Esq., 12 3001 King Ranch Road, Ukiah, CA 95482. */ 13 14#ifdef HAVE_CONFIG_H 15# include <config.h> 16#endif 17 18#include <assert.h> 19#include <dlfcn.h> 20#include <errno.h> 21#include <error.h> 22#include <fcntl.h> 23#include <fnmatch.h> 24#include <gelf.h> 25#include <inttypes.h> 26#include <libintl.h> 27#include <stdbool.h> 28#include <stdio_ext.h> 29#include <stdlib.h> 30#include <string.h> 31#include <unistd.h> 32#include <sys/param.h> 33#include <sys/stat.h> 34 35#include <system.h> 36#include "ld.h" 37#include "list.h" 38 39 40/* Prototypes for local functions. */ 41static const char **ld_generic_lib_extensions (struct ld_state *) 42 __attribute__ ((__const__)); 43static int ld_generic_file_close (struct usedfiles *fileinfo, 44 struct ld_state *statep); 45static int ld_generic_file_process (int fd, struct usedfiles *fileinfo, 46 struct ld_state *statep, 47 struct usedfiles **nextp); 48static void ld_generic_generate_sections (struct ld_state *statep); 49static void ld_generic_create_sections (struct ld_state *statep); 50static int ld_generic_flag_unresolved (struct ld_state *statep); 51static int ld_generic_open_outfile (struct ld_state *statep, int machine, 52 int class, int data); 53static int ld_generic_create_outfile (struct ld_state *statep); 54static void ld_generic_relocate_section (struct ld_state *statep, 55 Elf_Scn *outscn, 56 struct scninfo *firstp, 57 const Elf32_Word *dblindirect); 58static int ld_generic_finalize (struct ld_state *statep); 59static bool ld_generic_special_section_number_p (struct ld_state *statep, 60 size_t number); 61static bool ld_generic_section_type_p (struct ld_state *statep, 62 XElf_Word type); 63static XElf_Xword ld_generic_dynamic_section_flags (struct ld_state *statep); 64static void ld_generic_initialize_plt (struct ld_state *statep, Elf_Scn *scn); 65static void ld_generic_initialize_pltrel (struct ld_state *statep, 66 Elf_Scn *scn); 67static void ld_generic_initialize_got (struct ld_state *statep, Elf_Scn *scn); 68static void ld_generic_finalize_plt (struct ld_state *statep, size_t nsym, 69 size_t nsym_dyn); 70static int ld_generic_rel_type (struct ld_state *statep); 71static void ld_generic_count_relocations (struct ld_state *statep, 72 struct scninfo *scninfo); 73static void ld_generic_create_relocations (struct ld_state *statep, 74 const Elf32_Word *dblindirect); 75 76static int file_process2 (struct usedfiles *fileinfo); 77static void mark_section_used (struct scninfo *scninfo, Elf32_Word shndx, 78 struct scninfo **grpscnp); 79 80 81/* Map symbol index to struct symbol record. */ 82static struct symbol **ndxtosym; 83 84/* String table reference to all symbols in the symbol table. */ 85static struct Ebl_Strent **symstrent; 86 87 88/* Check whether file associated with FD is a DSO. */ 89static bool 90is_dso_p (int fd) 91{ 92 /* We have to read the 'e_type' field. It has the same size (16 93 bits) in 32- and 64-bit ELF. */ 94 XElf_Half e_type; 95 96 return (pread (fd, &e_type, sizeof (e_type), offsetof (XElf_Ehdr, e_type)) 97 == sizeof (e_type) 98 && e_type == ET_DYN); 99} 100 101 102/* Print the complete name of a file, including the archive it is 103 contained in. */ 104static int 105print_file_name (FILE *s, struct usedfiles *fileinfo, int first_level, 106 int newline) 107{ 108 int npar = 0; 109 110 if (fileinfo->archive_file != NULL) 111 { 112 npar = print_file_name (s, fileinfo->archive_file, 0, 0) + 1; 113 fputc_unlocked ('(', s); 114 fputs_unlocked (fileinfo->rfname, s); 115 116 if (first_level) 117 while (npar-- > 0) 118 fputc_unlocked (')', s); 119 } 120 else 121 fputs_unlocked (fileinfo->rfname, s); 122 123 if (first_level && newline) 124 fputc_unlocked ('\n', s); 125 126 return npar; 127} 128 129 130/* Function to determine whether an object will be dynamically linked. */ 131bool 132dynamically_linked_p (void) 133{ 134 return (ld_state.file_type == dso_file_type || ld_state.nplt > 0 135 || ld_state.ngot > 0); 136} 137 138 139bool 140linked_from_dso_p (struct scninfo *scninfo, int symidx) 141{ 142 struct usedfiles *file = scninfo->fileinfo; 143 144 /* If this symbol is not undefined in this file it cannot come from 145 a DSO. */ 146 if (symidx < file->nlocalsymbols) 147 return false; 148 149 struct symbol *sym = file->symref[symidx]; 150 151 return sym->defined && sym->in_dso; 152} 153 154 155/* Initialize state object. This callback function is called after the 156 parameters are parsed but before any file is searched for. */ 157int 158ld_prepare_state (const char *emulation) 159{ 160 /* When generating DSO we normally allow undefined symbols. */ 161 ld_state.nodefs = true; 162 163 /* To be able to detect problems we add a .comment section entry by 164 default. */ 165 ld_state.add_ld_comment = true; 166 167 /* XXX We probably should find a better place for this. The index 168 of the first user-defined version is 2. */ 169 ld_state.nextveridx = 2; 170 171 /* Pick an not too small number for the initial size of the tables. */ 172 ld_symbol_tab_init (&ld_state.symbol_tab, 1027); 173 ld_section_tab_init (&ld_state.section_tab, 67); 174 ld_version_str_tab_init (&ld_state.version_str_tab, 67); 175 176 /* Initialize the section header string table. */ 177 ld_state.shstrtab = ebl_strtabinit (true); 178 if (ld_state.shstrtab == NULL) 179 error (EXIT_FAILURE, errno, gettext ("cannot create string table")); 180 181 /* Initialize the callbacks. These are the defaults, the appropriate 182 backend can later install its own callbacks. */ 183 ld_state.callbacks.lib_extensions = ld_generic_lib_extensions; 184 ld_state.callbacks.file_process = ld_generic_file_process; 185 ld_state.callbacks.file_close = ld_generic_file_close; 186 ld_state.callbacks.generate_sections = ld_generic_generate_sections; 187 ld_state.callbacks.create_sections = ld_generic_create_sections; 188 ld_state.callbacks.flag_unresolved = ld_generic_flag_unresolved; 189 ld_state.callbacks.open_outfile = ld_generic_open_outfile; 190 ld_state.callbacks.create_outfile = ld_generic_create_outfile; 191 ld_state.callbacks.relocate_section = ld_generic_relocate_section; 192 ld_state.callbacks.finalize = ld_generic_finalize; 193 ld_state.callbacks.special_section_number_p = 194 ld_generic_special_section_number_p; 195 ld_state.callbacks.section_type_p = ld_generic_section_type_p; 196 ld_state.callbacks.dynamic_section_flags = ld_generic_dynamic_section_flags; 197 ld_state.callbacks.initialize_plt = ld_generic_initialize_plt; 198 ld_state.callbacks.initialize_pltrel = ld_generic_initialize_pltrel; 199 ld_state.callbacks.initialize_got = ld_generic_initialize_got; 200 ld_state.callbacks.finalize_plt = ld_generic_finalize_plt; 201 ld_state.callbacks.rel_type = ld_generic_rel_type; 202 ld_state.callbacks.count_relocations = ld_generic_count_relocations; 203 ld_state.callbacks.create_relocations = ld_generic_create_relocations; 204 205#ifndef BASE_ELF_NAME 206 /* Find the ld backend library. Use EBL to determine the name if 207 the user hasn't provided one on the command line. */ 208 if (emulation == NULL) 209 { 210 emulation = ebl_backend_name (ld_state.ebl); 211 assert (emulation != NULL); 212 } 213 size_t emulation_len = strlen (emulation); 214 215 /* Construct the file name. */ 216 char *fname = (char *) alloca (sizeof "libld_" - 1 + emulation_len 217 + sizeof ".so"); 218 strcpy (mempcpy (stpcpy (fname, "libld_"), emulation, emulation_len), ".so"); 219 220 /* Try loading. */ 221 void *h = dlopen (fname, RTLD_LAZY); 222 if (h == NULL) 223 error (EXIT_FAILURE, 0, 224 gettext ("cannot load ld backend library '%s': %s"), 225 fname, dlerror ()); 226 227 /* Find the initializer. It must be present. */ 228 char *initname = (char *) alloca (emulation_len + sizeof "_ld_init"); 229 strcpy (mempcpy (initname, emulation, emulation_len), "_ld_init"); 230 int (*initfct) (struct ld_state *) 231 = (int (*) (struct ld_state *)) dlsym (h, initname); 232 233 if (initfct == NULL) 234 error (EXIT_FAILURE, 0, gettext ("\ 235cannot find init function in ld backend library '%s': %s"), 236 fname, dlerror ()); 237 238 /* Store the handle. */ 239 ld_state.ldlib = h; 240 241 /* Call the init function. */ 242 return initfct (&ld_state); 243#else 244# define INIT_FCT_NAME(base) _INIT_FCT_NAME(base) 245# define _INIT_FCT_NAME(base) base##_ld_init 246 /* Declare and call the initialization function. */ 247 extern int INIT_FCT_NAME(BASE_ELF_NAME) (struct ld_state *); 248 return INIT_FCT_NAME(BASE_ELF_NAME) (&ld_state); 249#endif 250} 251 252 253static int 254check_for_duplicate2 (struct usedfiles *newp, struct usedfiles *list) 255{ 256 struct usedfiles *first; 257 struct usedfiles *prevp; 258 259 if (list == NULL) 260 return 0; 261 262 prevp = list; 263 list = first = list->next; 264 do 265 { 266 /* When searching the needed list we might come across entries 267 for files which are not yet opened. Stop then, there is 268 nothing more to test. */ 269 if (likely (list->status == not_opened)) 270 break; 271 272 if (unlikely (list->ino == newp->ino) 273 && unlikely (list->dev == newp->dev)) 274 { 275 close (newp->fd); 276 newp->fd = -1; 277 newp->status = closed; 278 if (newp->file_type == relocatable_file_type) 279 error (0, 0, gettext ("%s listed more than once as input"), 280 newp->rfname); 281 282 return 1; 283 } 284 list = list->next; 285 } 286 while (likely (list != first)); 287 288 return 0; 289} 290 291 292static int 293check_for_duplicate (struct usedfiles *newp) 294{ 295 struct stat st; 296 297 if (unlikely (fstat (newp->fd, &st) < 0)) 298 { 299 close (newp->fd); 300 return errno; 301 } 302 303 newp->dev = st.st_dev; 304 newp->ino = st.st_ino; 305 306 return (check_for_duplicate2 (newp, ld_state.relfiles) 307 || check_for_duplicate2 (newp, ld_state.dsofiles) 308 || check_for_duplicate2 (newp, ld_state.needed)); 309} 310 311 312/* Find a file along the path described in the state. */ 313static int 314open_along_path2 (struct usedfiles *fileinfo, struct pathelement *path) 315{ 316 const char *fname = fileinfo->fname; 317 size_t fnamelen = strlen (fname); 318 int err = ENOENT; 319 struct pathelement *firstp = path; 320 321 if (path == NULL) 322 /* Cannot find anything since we have no path. */ 323 return ENOENT; 324 325 do 326 { 327 if (likely (path->exist >= 0)) 328 { 329 /* Create the file name. */ 330 char *rfname = NULL; 331 size_t dirlen = strlen (path->pname); 332 int fd = -1; 333 334 if (fileinfo->file_type == archive_file_type) 335 { 336 const char **exts = (ld_state.statically 337 ? (const char *[2]) { ".a", NULL } 338 : LIB_EXTENSION (&ld_state)); 339 340 /* We have to create the actual file name. We prepend "lib" 341 and add one of the extensions the platform has. */ 342 while (*exts != NULL) 343 { 344 size_t extlen = strlen (*exts); 345 rfname = (char *) alloca (dirlen + 5 + fnamelen + extlen); 346 memcpy (mempcpy (stpcpy (mempcpy (rfname, path->pname, 347 dirlen), 348 "/lib"), 349 fname, fnamelen), 350 *exts, extlen + 1); 351 352 fd = open (rfname, O_RDONLY); 353 if (likely (fd != -1) || errno != ENOENT) 354 { 355 err = fd == -1 ? errno : 0; 356 break; 357 } 358 359 /* Next extension. */ 360 ++exts; 361 } 362 } 363 else 364 { 365 assert (fileinfo->file_type == dso_file_type 366 || fileinfo->file_type == dso_needed_file_type); 367 368 rfname = (char *) alloca (dirlen + 1 + fnamelen + 1); 369 memcpy (stpcpy (mempcpy (rfname, path->pname, dirlen), "/"), 370 fname, fnamelen + 1); 371 372 fd = open (rfname, O_RDONLY); 373 if (unlikely (fd == -1)) 374 err = errno; 375 } 376 377 if (likely (fd != -1)) 378 { 379 /* We found the file. This also means the directory 380 exists. */ 381 fileinfo->fd = fd; 382 path->exist = 1; 383 384 /* Check whether we have this file already loaded. */ 385 if (unlikely (check_for_duplicate (fileinfo) != 0)) 386 return EAGAIN; 387 388 /* Make a copy of the name. */ 389 fileinfo->rfname = obstack_strdup (&ld_state.smem, rfname); 390 391 if (unlikely (ld_state.trace_files)) 392 printf (fileinfo->file_type == archive_file_type 393 ? gettext ("%s (for -l%s)\n") 394 : gettext ("%s (for DT_NEEDED %s)\n"), 395 rfname, fname); 396 397 return 0; 398 } 399 400 /* The file does not exist. Maybe the whole directory doesn't. 401 Check it unless we know it exists. */ 402 if (unlikely (path->exist == 0)) 403 { 404 struct stat st; 405 406 /* Keep only the directory name. Note that the path 407 might be relative. This doesn't matter here. We do 408 the test in any case even if there is the chance that 409 somebody wants to change the programs working 410 directory at some point which would make the result 411 of this test void. Since changing the working 412 directory is completely wrong we are not taking this 413 case into account. */ 414 rfname[dirlen] = '\0'; 415 if (unlikely (stat (rfname, &st) < 0) || ! S_ISDIR (st.st_mode)) 416 /* The directory does not exist or the named file is no 417 directory. */ 418 path->exist = -1; 419 else 420 path->exist = 1; 421 } 422 } 423 424 /* Next path element. */ 425 path = path->next; 426 } 427 while (likely (err == ENOENT && path != firstp)); 428 429 return err; 430} 431 432 433static int 434open_along_path (struct usedfiles *fileinfo) 435{ 436 const char *fname = fileinfo->fname; 437 int err = ENOENT; 438 439 if (fileinfo->file_type == relocatable_file_type) 440 { 441 /* Only libraries are searched along the path. */ 442 fileinfo->fd = open (fname, O_RDONLY); 443 444 if (likely (fileinfo->fd != -1)) 445 { 446 /* We found the file. */ 447 if (unlikely (ld_state.trace_files)) 448 print_file_name (stdout, fileinfo, 1, 1); 449 450 return check_for_duplicate (fileinfo); 451 } 452 453 /* If the name is an absolute path we are done. */ 454 err = errno; 455 } 456 else 457 { 458 /* If the user specified two parts to the LD_LIBRARY_PATH variable 459 try the first part now. */ 460 err = open_along_path2 (fileinfo, ld_state.ld_library_path1); 461 462 /* Try the user-specified path next. */ 463 if (err == ENOENT) 464 err = open_along_path2 (fileinfo, 465 fileinfo->file_type == archive_file_type 466 ? ld_state.paths : ld_state.rpath_link); 467 468 /* Then the second part of the LD_LIBRARY_PATH value. */ 469 if (unlikely (err == ENOENT)) 470 { 471 err = open_along_path2 (fileinfo, ld_state.ld_library_path2); 472 473 /* In case we look for a DSO handle now the RUNPATH. */ 474 if (err == ENOENT) 475 { 476 if (fileinfo->file_type == dso_file_type) 477 err = open_along_path2 (fileinfo, ld_state.runpath_link); 478 479 /* Finally the path from the default linker script. */ 480 if (err == ENOENT) 481 err = open_along_path2 (fileinfo, ld_state.default_paths); 482 } 483 } 484 } 485 486 if (unlikely (err != 0) 487 && (err != EAGAIN || fileinfo->file_type == relocatable_file_type)) 488 error (0, err, gettext ("cannot open %s"), fileinfo->fname); 489 490 return err; 491} 492 493 494static void 495check_type_and_size (const XElf_Sym *sym, struct usedfiles *fileinfo, 496 struct symbol *oldp) 497{ 498 /* We check the type and size of the symbols. In both cases the 499 information can be missing (size is zero, type is STT_NOTYPE) in 500 which case we issue no warnings. Otherwise everything must 501 match. If the type does not match there is no point in checking 502 the size. */ 503 504 if (XELF_ST_TYPE (sym->st_info) != STT_NOTYPE && oldp->type != STT_NOTYPE 505 && unlikely (oldp->type != XELF_ST_TYPE (sym->st_info))) 506 { 507 char buf1[64]; 508 char buf2[64]; 509 510 error (0, 0, gettext ("\ 511Warning: type of `%s' changed from %s in %s to %s in %s"), 512 oldp->name, 513 ebl_symbol_type_name (ld_state.ebl, oldp->type, 514 buf1, sizeof (buf1)), 515 oldp->file->rfname, 516 ebl_symbol_type_name (ld_state.ebl, XELF_ST_TYPE (sym->st_info), 517 buf2, sizeof (buf2)), 518 fileinfo->rfname); 519 } 520 else if (XELF_ST_TYPE (sym->st_info) == STT_OBJECT 521 && oldp->size != 0 522 && unlikely (oldp->size != sym->st_size)) 523 error (0, 0, gettext ("\ 524Warning: size of `%s' changed from %" PRIu64 " in %s to %" PRIu64 " in %s"), 525 oldp->name, (uint64_t) oldp->size, oldp->file->rfname, 526 (uint64_t) sym->st_size, fileinfo->rfname); 527} 528 529 530static int 531check_definition (const XElf_Sym *sym, size_t symidx, 532 struct usedfiles *fileinfo, struct symbol *oldp) 533{ 534 int result = 0; 535 bool old_in_dso = FILEINFO_EHDR (oldp->file->ehdr).e_type == ET_DYN; 536 bool new_in_dso = FILEINFO_EHDR (fileinfo->ehdr).e_type == ET_DYN; 537 bool use_new_def = false; 538 539 if (sym->st_shndx != SHN_UNDEF 540 && (! oldp->defined 541 || (sym->st_shndx != SHN_COMMON && oldp->common && ! new_in_dso) 542 || (old_in_dso && ! new_in_dso))) 543 { 544 /* We found a definition for a previously undefined symbol or a 545 real definition for a previous common-only definition or a 546 redefinition of a symbol definition in an object file 547 previously defined in a DSO. First perform some tests which 548 will show whether the common is really matching the 549 definition. */ 550 check_type_and_size (sym, fileinfo, oldp); 551 552 /* We leave the next element intact to not interrupt the list 553 with the unresolved symbols. Whoever walks the list will 554 have to check the `defined' flag. But we remember that this 555 list element is not unresolved anymore. */ 556 if (! oldp->defined) 557 { 558 /* Remove from the list. */ 559 --ld_state.nunresolved; 560 if (! oldp->weak) 561 --ld_state.nunresolved_nonweak; 562 CDBL_LIST_DEL (ld_state.unresolved, oldp); 563 } 564 else if (oldp->common) 565 /* Remove from the list. */ 566 CDBL_LIST_DEL (ld_state.common_syms, oldp); 567 568 /* Use the values of the definition from now on. */ 569 use_new_def = true; 570 } 571 else if (sym->st_shndx != SHN_UNDEF 572 && unlikely (! oldp->common) 573 && oldp->defined 574 && sym->st_shndx != SHN_COMMON 575 /* Multiple definitions are no fatal errors if the -z muldefs flag 576 is used. We don't warn about the multiple definition unless we 577 are told to be verbose. */ 578 && (!ld_state.muldefs || verbose) 579 && ! old_in_dso && fileinfo->file_type == relocatable_file_type) 580 { 581 /* We have a double definition. This is a problem. */ 582 char buf[64]; 583 XElf_Sym_vardef (oldsym); 584 struct usedfiles *oldfile; 585 const char *scnname; 586 Elf32_Word xndx; 587 size_t shndx; 588 size_t shnum; 589 590 if (elf_getshnum (fileinfo->elf, &shnum) < 0) 591 error (EXIT_FAILURE, 0, 592 gettext ("cannot determine number of sections: %s"), 593 elf_errmsg (-1)); 594 595 /* XXX Use only ebl_section_name. */ 596 if (sym->st_shndx < SHN_LORESERVE // || sym->st_shndx > SHN_HIRESERVE 597 && sym->st_shndx < shnum) 598 scnname = elf_strptr (fileinfo->elf, 599 fileinfo->shstrndx, 600 SCNINFO_SHDR (fileinfo->scninfo[sym->st_shndx].shdr).sh_name); 601 else 602 // XXX extended section 603 scnname = ebl_section_name (ld_state.ebl, sym->st_shndx, 0, 604 buf, sizeof (buf), NULL, shnum); 605 606 /* XXX Print source file and line number. */ 607 print_file_name (stderr, fileinfo, 1, 0); 608 fprintf (stderr, 609 gettext ("(%s+%#" PRIx64 "): multiple definition of %s `%s'\n"), 610 scnname, 611 (uint64_t) sym->st_value, 612 ebl_symbol_type_name (ld_state.ebl, XELF_ST_TYPE (sym->st_info), 613 buf, sizeof (buf)), 614 oldp->name); 615 616 oldfile = oldp->file; 617 xelf_getsymshndx (oldfile->symtabdata, oldfile->xndxdata, oldp->symidx, 618 oldsym, xndx); 619 if (oldsym == NULL) 620 /* This should never happen since the same call 621 succeeded before. */ 622 abort (); 623 624 shndx = oldsym->st_shndx; 625 if (unlikely (oldsym->st_shndx == SHN_XINDEX)) 626 shndx = xndx; 627 628 /* XXX Use only ebl_section_name. */ 629 if (shndx < SHN_LORESERVE || shndx > SHN_HIRESERVE) 630 scnname = elf_strptr (oldfile->elf, 631 oldfile->shstrndx, 632 SCNINFO_SHDR (oldfile->scninfo[shndx].shdr).sh_name); 633 else 634 scnname = ebl_section_name (ld_state.ebl, oldsym->st_shndx, shndx, buf, 635 sizeof (buf), NULL, shnum); 636 637 /* XXX Print source file and line number. */ 638 print_file_name (stderr, oldfile, 1, 0); 639 fprintf (stderr, gettext ("(%s+%#" PRIx64 "): first defined here\n"), 640 scnname, (uint64_t) oldsym->st_value); 641 642 if (likely (!ld_state.muldefs)) 643 result = 1; 644 } 645 else if (old_in_dso && fileinfo->file_type == relocatable_file_type 646 && sym->st_shndx != SHN_UNDEF) 647 /* We use the definition from a normal relocatable file over the 648 definition in a DSO. This is what the dynamic linker would 649 do, too. */ 650 use_new_def = true; 651 else if (old_in_dso && !new_in_dso && oldp->defined && !oldp->on_dsolist) 652 { 653 CDBL_LIST_ADD_REAR (ld_state.from_dso, oldp); 654 ++ld_state.nfrom_dso; 655 656 /* If the object is a function we allocate a PLT entry, 657 otherwise only a GOT entry. */ 658 if (oldp->type == STT_FUNC) 659 ++ld_state.nplt; 660 else 661 ++ld_state.ngot; 662 663 oldp->on_dsolist = 1; 664 } 665 else if (oldp->common && sym->st_shndx == SHN_COMMON) 666 { 667 /* The symbol size is the largest of all common definitions. */ 668 oldp->size = MAX (oldp->size, sym->st_size); 669 /* Similarly for the alignment. */ 670 oldp->merge.value = MAX (oldp->merge.value, sym->st_value); 671 } 672 673 if (unlikely (use_new_def)) 674 { 675 /* Adjust the symbol record appropriately and remove 676 the symbol from the list of symbols which are taken from DSOs. */ 677 if (old_in_dso && fileinfo->file_type == relocatable_file_type) 678 { 679 CDBL_LIST_DEL (ld_state.from_dso, oldp); 680 --ld_state.nfrom_dso; 681 682 if (likely (oldp->type == STT_FUNC)) 683 --ld_state.nplt; 684 else 685 --ld_state.ngot; 686 687 oldp->on_dsolist = 0; 688 } 689 690 /* Use the values of the definition from now on. */ 691 oldp->size = sym->st_size; 692 oldp->type = XELF_ST_TYPE (sym->st_info); 693 oldp->symidx = symidx; 694 oldp->scndx = sym->st_shndx; 695 //oldp->symscndx = THESYMSCNDX must be passed; 696 oldp->file = fileinfo; 697 oldp->defined = 1; 698 oldp->in_dso = new_in_dso; 699 oldp->common = sym->st_shndx == SHN_COMMON; 700 if (likely (fileinfo->file_type == relocatable_file_type)) 701 { 702 /* If the definition comes from a DSO we pertain the weak flag 703 and it's indicating whether the reference is weak or not. */ 704 oldp->weak = XELF_ST_BIND (sym->st_info) == STB_WEAK; 705 706 if (sym->st_shndx != SHN_COMMON) 707 { 708 struct scninfo *ignore; 709 mark_section_used (&fileinfo->scninfo[sym->st_shndx], 710 sym->st_shndx, &ignore); 711 } 712 } 713 714 /* Add to the list of symbols used from DSOs if necessary. */ 715 if (new_in_dso && !old_in_dso) 716 { 717 CDBL_LIST_ADD_REAR (ld_state.from_dso, oldp); 718 ++ld_state.nfrom_dso; 719 720 /* If the object is a function we allocate a PLT entry, 721 otherwise only a GOT entry. */ 722 if (oldp->type == STT_FUNC) 723 ++ld_state.nplt; 724 else 725 ++ld_state.ngot; 726 727 oldp->on_dsolist = 1; 728 } 729 else if (sym->st_shndx == SHN_COMMON) 730 { 731 /* Store the alignment. */ 732 oldp->merge.value = sym->st_value; 733 734 CDBL_LIST_ADD_REAR (ld_state.common_syms, oldp); 735 } 736 } 737 738 return result; 739} 740 741 742static struct scninfo * 743find_section_group (struct usedfiles *fileinfo, Elf32_Word shndx, 744 Elf_Data **datap) 745{ 746 struct scninfo *runp; 747 748 for (runp = fileinfo->groups; runp != NULL; runp = runp->next) 749 if (!runp->used) 750 { 751 Elf32_Word *grpref; 752 size_t cnt; 753 Elf_Data *data; 754 755 data = elf_getdata (runp->scn, NULL); 756 if (data == NULL) 757 error (EXIT_FAILURE, 0, 758 gettext ("%s: cannot get section group data: %s"), 759 fileinfo->fname, elf_errmsg (-1)); 760 761 /* There cannot be another data block. */ 762 assert (elf_getdata (runp->scn, data) == NULL); 763 764 grpref = (Elf32_Word *) data->d_buf; 765 cnt = data->d_size / sizeof (Elf32_Word); 766 /* Note that we stop after looking at index 1 since index 0 767 contains the flags for the section group. */ 768 while (cnt > 1) 769 if (grpref[--cnt] == shndx) 770 { 771 *datap = data; 772 return runp; 773 } 774 } 775 776 /* If we come here no section group contained the given section 777 despite the SHF_GROUP flag. This is an error in the input 778 file. */ 779 error (EXIT_FAILURE, 0, gettext ("\ 780%s: section '%s' with group flag set does not belong to any group"), 781 fileinfo->fname, 782 elf_strptr (fileinfo->elf, fileinfo->shstrndx, 783 SCNINFO_SHDR (fileinfo->scninfo[shndx].shdr).sh_name)); 784 return NULL; 785} 786 787 788/* Mark all sections which belong to the same group as section SHNDX 789 as used. */ 790static void 791mark_section_group (struct usedfiles *fileinfo, Elf32_Word shndx, 792 struct scninfo **grpscnp) 793{ 794 /* First locate the section group. There can be several (many) of 795 them. */ 796 size_t cnt; 797 Elf32_Word *grpref; 798 Elf_Data *data; 799 struct scninfo *grpscn = find_section_group (fileinfo, shndx, &data); 800 *grpscnp = grpscn; 801 802 /* Mark all the sections as used. 803 804 XXX Two possible problems here: 805 806 - the gABI says "The section must be referenced by a section of type 807 SHT_GROUP". I hope everybody reads this as "exactly one section". 808 809 - section groups are also useful to mark the debugging section which 810 belongs to a text section. Unconditionally adding debugging sections 811 is therefore probably not what is wanted if stripping is required. */ 812 813 /* Mark the section group as handled. */ 814 grpscn->used = true; 815 816 grpref = (Elf32_Word *) data->d_buf; 817 cnt = data->d_size / sizeof (Elf32_Word); 818 while (cnt > 1) 819 { 820 Elf32_Word idx = grpref[--cnt]; 821 XElf_Shdr *shdr = &SCNINFO_SHDR (fileinfo->scninfo[idx].shdr); 822 823 if (fileinfo->scninfo[idx].grpid != 0) 824 error (EXIT_FAILURE, 0, gettext ("\ 825%s: section [%2d] '%s' is in more than one section group"), 826 fileinfo->fname, (int) idx, 827 elf_strptr (fileinfo->elf, fileinfo->shstrndx, shdr->sh_name)); 828 829 fileinfo->scninfo[idx].grpid = grpscn->grpid; 830 831 if (ld_state.strip == strip_none 832 /* If we are stripping, remove debug sections. */ 833 || (!ebl_debugscn_p (ld_state.ebl, 834 elf_strptr (fileinfo->elf, fileinfo->shstrndx, 835 shdr->sh_name)) 836 /* And the relocation sections for the debug sections. */ 837 && ((shdr->sh_type != SHT_RELA && shdr->sh_type != SHT_REL) 838 || !ebl_debugscn_p (ld_state.ebl, 839 elf_strptr (fileinfo->elf, 840 fileinfo->shstrndx, 841 SCNINFO_SHDR (fileinfo->scninfo[shdr->sh_info].shdr).sh_name))))) 842 { 843 struct scninfo *ignore; 844 845 mark_section_used (&fileinfo->scninfo[idx], idx, &ignore); 846 } 847 } 848} 849 850 851static void 852mark_section_used (struct scninfo *scninfo, Elf32_Word shndx, 853 struct scninfo **grpscnp) 854{ 855 if (likely (scninfo->used)) 856 /* Nothing to be done. */ 857 return; 858 859 /* We need this section. */ 860 scninfo->used = true; 861 862 /* Make sure the section header has been read from the file. */ 863 XElf_Shdr *shdr = &SCNINFO_SHDR (scninfo->shdr); 864#if NATIVE_ELF 865 if (unlikely (scninfo->shdr == NULL)) 866#else 867 if (unlikely (scninfo->shdr.sh_type == SHT_NULL)) 868#endif 869 { 870#if NATIVE_ELF != 0 871 shdr = xelf_getshdr (scninfo->scn, scninfo->shdr); 872#else 873 xelf_getshdr_copy (scninfo->scn, shdr, scninfo->shdr); 874#endif 875 if (unlikely (shdr == NULL)) 876 /* Something is very wrong. The calling code will notice it 877 soon and print a message. */ 878 return; 879 } 880 881 /* Handle section linked by 'sh_link'. */ 882 if (unlikely (shdr->sh_link != 0)) 883 { 884 struct scninfo *ignore; 885 mark_section_used (&scninfo->fileinfo->scninfo[shdr->sh_link], 886 shdr->sh_link, &ignore); 887 } 888 889 /* Handle section linked by 'sh_info'. */ 890 if (unlikely (shdr->sh_info != 0) && (shdr->sh_flags & SHF_INFO_LINK)) 891 { 892 struct scninfo *ignore; 893 mark_section_used (&scninfo->fileinfo->scninfo[shdr->sh_info], 894 shdr->sh_info, &ignore); 895 } 896 897 if (unlikely (shdr->sh_flags & SHF_GROUP) && ld_state.gc_sections) 898 /* Find the section group which contains this section. */ 899 mark_section_group (scninfo->fileinfo, shndx, grpscnp); 900} 901 902 903/* We collect all sections in a hashing table. All sections with the 904 same name are collected in a list. Note that we do not determine 905 which sections are finally collected in the same output section 906 here. This would be terribly inefficient. It will be done later. */ 907static void 908add_section (struct usedfiles *fileinfo, struct scninfo *scninfo) 909{ 910 struct scnhead *queued; 911 struct scnhead search; 912 unsigned long int hval; 913 XElf_Shdr *shdr = &SCNINFO_SHDR (scninfo->shdr); 914 struct scninfo *grpscn = NULL; 915 Elf_Data *grpscndata = NULL; 916 917 /* See whether we can determine right away whether we need this 918 section in the output. 919 920 XXX I assume here that --gc-sections only affects extraction 921 from an archive. If it also affects objects files given on 922 the command line then somebody must explain to me how the 923 dependency analysis should work. Should the entry point be 924 the root? What if it is a numeric value? */ 925 if (!scninfo->used 926 && (ld_state.strip == strip_none 927 || (shdr->sh_flags & SHF_ALLOC) != 0 928 || shdr->sh_type == SHT_NOTE 929 || (shdr->sh_type == SHT_PROGBITS 930 && strcmp (elf_strptr (fileinfo->elf, 931 fileinfo->shstrndx, 932 shdr->sh_name), ".comment") == 0)) 933 && (fileinfo->status != in_archive || !ld_state.gc_sections)) 934 /* Mark as used and handle reference recursively if necessary. */ 935 mark_section_used (scninfo, elf_ndxscn (scninfo->scn), &grpscn); 936 937 if ((shdr->sh_flags & SHF_GROUP) && grpscn == NULL) 938 /* Determine the symbol which name constitutes the signature 939 for the section group. */ 940 grpscn = find_section_group (fileinfo, elf_ndxscn (scninfo->scn), 941 &grpscndata); 942 assert (grpscn == NULL || grpscn->symbols->name != NULL); 943 944 /* Determine the section name. */ 945 search.name = elf_strptr (fileinfo->elf, fileinfo->shstrndx, shdr->sh_name); 946 search.type = shdr->sh_type; 947 search.flags = shdr->sh_flags; 948 search.entsize = shdr->sh_entsize; 949 search.grp_signature = grpscn != NULL ? grpscn->symbols->name : NULL; 950 search.kind = scn_normal; 951 hval = elf_hash (search.name); 952 953 /* Find already queued sections. */ 954 queued = ld_section_tab_find (&ld_state.section_tab, hval, &search); 955 if (queued != NULL) 956 { 957 bool is_comdat = false; 958 959 /* If this section is part of a COMDAT section group we simply 960 ignore it since we already have a copy. */ 961 if (unlikely (shdr->sh_flags & SHF_GROUP)) 962 { 963 /* Get the data of the section group section. */ 964 if (grpscndata == NULL) 965 { 966 grpscndata = elf_getdata (grpscn->scn, NULL); 967 assert (grpscndata != NULL); 968 } 969 970 /* XXX Possibly unaligned memory access. */ 971 is_comdat = ((Elf32_Word *) grpscndata->d_buf)[0] & GRP_COMDAT; 972 } 973 974 if (!is_comdat) 975 { 976 /* No COMDAT section, we use the data. */ 977 scninfo->next = queued->last->next; 978 queued->last = queued->last->next = scninfo; 979 980 queued->flags = SH_FLAGS_COMBINE (queued->flags, shdr->sh_flags); 981 queued->align = MAX (queued->align, shdr->sh_addralign); 982 } 983 } 984 else 985 { 986 /* We do not use obstacks here since the memory might be 987 deallocated. */ 988 queued = (struct scnhead *) xcalloc (sizeof (struct scnhead), 1); 989 queued->kind = scn_normal; 990 queued->name = search.name; 991 queued->type = shdr->sh_type; 992 queued->flags = shdr->sh_flags; 993 queued->align = shdr->sh_addralign; 994 queued->entsize = shdr->sh_entsize; 995 queued->grp_signature = grpscn != NULL ? grpscn->symbols->name : NULL; 996 queued->segment_nr = ~0; 997 queued->last = scninfo->next = scninfo; 998 999 /* Add to the hash table and possibly overwrite existing value. */ 1000 ld_section_tab_insert (&ld_state.section_tab, hval, queued); 1001 } 1002} 1003 1004 1005static int 1006add_relocatable_file (struct usedfiles *fileinfo, int secttype) 1007{ 1008 size_t scncnt; 1009 size_t cnt; 1010 Elf_Data *symtabdata = NULL; 1011 Elf_Data *xndxdata = NULL; 1012 Elf_Data *versymdata = NULL; 1013 Elf_Data *verdefdata = NULL; 1014 Elf_Data *verneeddata = NULL; 1015 size_t symstridx = 0; 1016 size_t nsymbols = 0; 1017 size_t nlocalsymbols = 0; 1018 bool has_merge_sections = false; 1019 1020 /* Prerequisites. */ 1021 assert (fileinfo->elf != NULL); 1022 1023 /* Allocate memory for the sections. */ 1024 if (unlikely (elf_getshnum (fileinfo->elf, &scncnt) < 0)) 1025 error (EXIT_FAILURE, 0, 1026 gettext ("cannot determine number of sections: %s"), 1027 elf_errmsg (-1)); 1028 1029 fileinfo->scninfo = (struct scninfo *) 1030 obstack_calloc (&ld_state.smem, scncnt * sizeof (struct scninfo)); 1031 1032 /* Read all the section headers and find the symbol table. Note 1033 that we don't skip the section with index zero. Even though the 1034 section itself is always empty the section header contains 1035 informaton for the case when the section index for the section 1036 header string table is too large to fit in the ELF header. */ 1037 for (cnt = 0; cnt < scncnt; ++cnt) 1038 { 1039 /* Store the handle for the section. */ 1040 fileinfo->scninfo[cnt].scn = elf_getscn (fileinfo->elf, cnt); 1041 1042 /* Get the ELF section header and data. */ 1043 XElf_Shdr *shdr; 1044#if NATIVE_ELF != 0 1045 if (fileinfo->scninfo[cnt].shdr == NULL) 1046#else 1047 if (fileinfo->scninfo[cnt].shdr.sh_type == SHT_NULL) 1048#endif 1049 { 1050#if NATIVE_ELF != 0 1051 shdr = xelf_getshdr (fileinfo->scninfo[cnt].scn, 1052 fileinfo->scninfo[cnt].shdr); 1053#else 1054 xelf_getshdr_copy (fileinfo->scninfo[cnt].scn, shdr, 1055 fileinfo->scninfo[cnt].shdr); 1056#endif 1057 if (shdr == NULL) 1058 { 1059 /* This should never happen. */ 1060 fprintf (stderr, gettext ("%s: invalid ELF file (%s:%d)\n"), 1061 fileinfo->rfname, __FILE__, __LINE__); 1062 return 1; 1063 } 1064 } 1065 else 1066 shdr = &SCNINFO_SHDR (fileinfo->scninfo[cnt].shdr); 1067 1068 Elf_Data *data = elf_getdata (fileinfo->scninfo[cnt].scn, NULL); 1069 1070 /* Check whether this section is marked as merge-able. */ 1071 has_merge_sections |= (shdr->sh_flags & SHF_MERGE) != 0; 1072 1073 /* Get the ELF section header and data. */ 1074 /* Make the file structure available. */ 1075 fileinfo->scninfo[cnt].fileinfo = fileinfo; 1076 1077 if (unlikely (shdr->sh_type == SHT_SYMTAB) 1078 || unlikely (shdr->sh_type == SHT_DYNSYM)) 1079 { 1080 if (shdr->sh_type == SHT_SYMTAB) 1081 { 1082 assert (fileinfo->symtabdata == NULL); 1083 fileinfo->symtabdata = data; 1084 fileinfo->nsymtab = shdr->sh_size / shdr->sh_entsize; 1085 fileinfo->nlocalsymbols = shdr->sh_info; 1086 fileinfo->symstridx = shdr->sh_link; 1087 } 1088 else 1089 { 1090 assert (fileinfo->dynsymtabdata == NULL); 1091 fileinfo->dynsymtabdata = data; 1092 fileinfo->ndynsymtab = shdr->sh_size / shdr->sh_entsize; 1093 fileinfo->dynsymstridx = shdr->sh_link; 1094 } 1095 1096 /* If we are looking for the normal symbol table we just 1097 found it. */ 1098 if (secttype == shdr->sh_type) 1099 { 1100 assert (symtabdata == NULL); 1101 symtabdata = data; 1102 symstridx = shdr->sh_link; 1103 nsymbols = shdr->sh_size / shdr->sh_entsize; 1104 nlocalsymbols = shdr->sh_info; 1105 } 1106 } 1107 else if (unlikely (shdr->sh_type == SHT_SYMTAB_SHNDX)) 1108 { 1109 assert (xndxdata == NULL); 1110 fileinfo->xndxdata = xndxdata = data; 1111 } 1112 else if (unlikely (shdr->sh_type == SHT_GNU_versym)) 1113 { 1114 assert (versymdata == 0); 1115 fileinfo->versymdata = versymdata = data; 1116 } 1117 else if (unlikely (shdr->sh_type == SHT_GNU_verdef)) 1118 { 1119 size_t nversions; 1120 1121 assert (verdefdata == 0); 1122 fileinfo->verdefdata = verdefdata = data; 1123 1124 /* Allocate the arrays flagging the use of the version and 1125 to track of allocated names. */ 1126 fileinfo->nverdef = nversions = shdr->sh_info; 1127 /* We have NVERSIONS + 1 because the indeces used to access the 1128 sectino start with one; zero represents local binding. */ 1129 fileinfo->verdefused = (XElf_Versym *) 1130 obstack_calloc (&ld_state.smem, 1131 sizeof (XElf_Versym) * (nversions + 1)); 1132 fileinfo->verdefent = (struct Ebl_Strent **) 1133 obstack_alloc (&ld_state.smem, 1134 sizeof (struct Ebl_Strent *) * (nversions + 1)); 1135 } 1136 else if (unlikely (shdr->sh_type == SHT_GNU_verneed)) 1137 { 1138 assert (verneeddata == 0); 1139 fileinfo->verneeddata = verneeddata = data; 1140 } 1141 else if (unlikely (shdr->sh_type == SHT_DYNAMIC)) 1142 { 1143 assert (fileinfo->dynscn == NULL); 1144 fileinfo->dynscn = fileinfo->scninfo[cnt].scn; 1145 } 1146 else if (unlikely (shdr->sh_type == SHT_GROUP)) 1147 { 1148 Elf_Scn *symscn; 1149 XElf_Shdr_vardef (symshdr); 1150 Elf_Data *symdata; 1151 1152 if (FILEINFO_EHDR (fileinfo->ehdr).e_type != ET_REL) 1153 error (EXIT_FAILURE, 0, gettext ("\ 1154%s: only files of type ET_REL might contain section groups"), 1155 fileinfo->fname); 1156 1157 fileinfo->scninfo[cnt].next = fileinfo->groups; 1158 fileinfo->scninfo[cnt].grpid = cnt; 1159 fileinfo->groups = &fileinfo->scninfo[cnt]; 1160 1161 /* Determine the signature. We create a symbol record for 1162 it. Only the name element is important. */ 1163 fileinfo->scninfo[cnt].symbols = (struct symbol *) 1164 obstack_calloc (&ld_state.smem, sizeof (struct symbol)); 1165 1166 symscn = elf_getscn (fileinfo->elf, shdr->sh_link); 1167 xelf_getshdr (symscn, symshdr); 1168 symdata = elf_getdata (symscn, NULL); 1169 if (symshdr != NULL) 1170 { 1171 XElf_Sym_vardef (sym); 1172 1173 /* We don't need the section index and therefore we don't 1174 have to use 'xelf_getsymshndx'. */ 1175 xelf_getsym (symdata, shdr->sh_info, sym); 1176 if (sym != NULL) 1177 { 1178 struct symbol *symbol = fileinfo->scninfo[cnt].symbols; 1179 1180 symbol->name = elf_strptr (fileinfo->elf, symshdr->sh_link, 1181 sym->st_name); 1182 symbol->symidx = shdr->sh_info; 1183 symbol->file = fileinfo; 1184 } 1185 } 1186 if (fileinfo->scninfo[cnt].symbols->name == NULL) 1187 error (EXIT_FAILURE, 0, gettext ("\ 1188%s: cannot determine signature of section group [%2zd] '%s': %s"), 1189 fileinfo->fname, 1190 elf_ndxscn (fileinfo->scninfo[cnt].scn), 1191 elf_strptr (fileinfo->elf, fileinfo->shstrndx, 1192 shdr->sh_name), 1193 elf_errmsg (-1)); 1194 1195 /* The 'used' flag is used to indicate when the information 1196 in the section group is used to mark all other sections 1197 as used. So it must not be true yet. */ 1198 assert (fileinfo->scninfo[cnt].used == false); 1199 } 1200 else if (! SECTION_TYPE_P (&ld_state, shdr->sh_type) 1201 && unlikely ((shdr->sh_flags & SHF_OS_NONCONFORMING) != 0)) 1202 /* According to the gABI it is a fatal error if the file contains 1203 a section with unknown type and the SHF_OS_NONCONFORMING flag 1204 set. */ 1205 error (EXIT_FAILURE, 0, 1206 gettext ("%s: section '%s' has unknown type: %d"), 1207 fileinfo->fname, 1208 elf_strptr (fileinfo->elf, fileinfo->shstrndx, 1209 shdr->sh_name), 1210 (int) shdr->sh_type); 1211 /* We don't have to add a few section types here. These will be 1212 generated from scratch for the new output file. We also 1213 don't add the sections of DSOs here since these sections are 1214 not used in the resulting object file. */ 1215 else if (likely (fileinfo->file_type == relocatable_file_type) 1216 && likely (cnt > 0) 1217 && likely (shdr->sh_type == SHT_PROGBITS 1218 || shdr->sh_type == SHT_RELA 1219 || shdr->sh_type == SHT_REL 1220 || shdr->sh_type == SHT_NOTE 1221 || shdr->sh_type == SHT_NOBITS 1222 || shdr->sh_type == SHT_INIT_ARRAY 1223 || shdr->sh_type == SHT_FINI_ARRAY 1224 || shdr->sh_type == SHT_PREINIT_ARRAY)) 1225 add_section (fileinfo, &fileinfo->scninfo[cnt]); 1226 } 1227 1228 /* Handle the symbols. Record defined and undefined symbols in the 1229 hash table. In theory there can be a file without any symbol 1230 table. */ 1231 if (likely (symtabdata != NULL)) 1232 { 1233 /* In case this file contains merge-able sections we have to 1234 locate the symbols which are in these sections. */ 1235 fileinfo->has_merge_sections = has_merge_sections; 1236 if (likely (has_merge_sections)) 1237 { 1238 fileinfo->symref = (struct symbol **) 1239 obstack_calloc (&ld_state.smem, 1240 nsymbols * sizeof (struct symbol *)); 1241 1242 /* Only handle the local symbols here. */ 1243 for (cnt = 0; cnt < nlocalsymbols; ++cnt) 1244 { 1245 Elf32_Word shndx; 1246 XElf_Sym_vardef (sym); 1247 1248 xelf_getsymshndx (symtabdata, xndxdata, cnt, sym, shndx); 1249 if (sym == NULL) 1250 { 1251 /* This should never happen. */ 1252 fprintf (stderr, gettext ("%s: invalid ELF file (%s:%d)\n"), 1253 fileinfo->rfname, __FILE__, __LINE__); 1254 return 1; 1255 } 1256 1257 if (likely (shndx != SHN_XINDEX)) 1258 shndx = sym->st_shndx; 1259 else if (unlikely (shndx == 0)) 1260 { 1261 fprintf (stderr, gettext ("%s: invalid ELF file (%s:%d)\n"), 1262 fileinfo->rfname, __FILE__, __LINE__); 1263 return 1; 1264 } 1265 1266 if (XELF_ST_TYPE (sym->st_info) != STT_SECTION 1267 && (shndx < SHN_LORESERVE || shndx > SHN_HIRESERVE) 1268 && (SCNINFO_SHDR (fileinfo->scninfo[shndx].shdr).sh_flags 1269 & SHF_MERGE)) 1270 { 1271 /* Create a symbol record for this symbol and add it 1272 to the list for this section. */ 1273 struct symbol *newp; 1274 1275 newp = (struct symbol *) 1276 obstack_calloc (&ld_state.smem, sizeof (struct symbol)); 1277 1278 newp->symidx = cnt; 1279 newp->scndx = shndx; 1280 newp->file = fileinfo; 1281 fileinfo->symref[cnt] = newp; 1282 1283 if (fileinfo->scninfo[shndx].symbols == NULL) 1284 fileinfo->scninfo[shndx].symbols = newp->next_in_scn 1285 = newp; 1286 else 1287 { 1288 newp->next_in_scn 1289 = fileinfo->scninfo[shndx].symbols->next_in_scn; 1290 fileinfo->scninfo[shndx].symbols 1291 = fileinfo->scninfo[shndx].symbols->next_in_scn = newp; 1292 } 1293 } 1294 } 1295 } 1296 else 1297 /* Create array with pointers to the symbol definitions. Note 1298 that we only allocate memory for the non-local symbols 1299 since we have no merge-able sections. But we store the 1300 pointer as if it was for the whole symbol table. This 1301 saves some memory. */ 1302 fileinfo->symref = (struct symbol **) 1303 obstack_calloc (&ld_state.smem, ((nsymbols - nlocalsymbols) 1304 * sizeof (struct symbol *))) 1305 - nlocalsymbols; 1306 1307 /* Don't handle local symbols here. It's either not necessary 1308 at all or has already happened. */ 1309 for (cnt = nlocalsymbols; cnt < nsymbols; ++cnt) 1310 { 1311 XElf_Sym_vardef (sym); 1312 Elf32_Word shndx; 1313 xelf_getsymshndx (symtabdata, xndxdata, cnt, sym, shndx); 1314 1315 if (sym == NULL) 1316 { 1317 /* This should never happen. */ 1318 fprintf (stderr, gettext ("%s: invalid ELF file (%s:%d)\n"), 1319 fileinfo->rfname, __FILE__, __LINE__); 1320 return 1; 1321 } 1322 1323 if (likely (shndx != SHN_XINDEX)) 1324 shndx = sym->st_shndx; 1325 else if (unlikely (shndx == 0)) 1326 { 1327 fprintf (stderr, gettext ("%s: invalid ELF file (%s:%d)\n"), 1328 fileinfo->rfname, __FILE__, __LINE__); 1329 return 1; 1330 } 1331 1332 /* We ignore ABS symbols from DSOs. */ 1333 // XXX Is this correct? 1334 if (unlikely (shndx == SHN_ABS) && secttype == SHT_DYNSYM) 1335 continue; 1336 1337 /* If the DSO uses symbols determine whether this is the default 1338 version. Otherwise we'll ignore the symbol. */ 1339 if (versymdata != NULL) 1340 { 1341 XElf_Versym versym; 1342 1343 if (xelf_getversym_copy (versymdata, cnt, versym) == NULL) 1344 /* XXX Should we handle faulty input files more graceful? */ 1345 assert (! "xelf_getversym failed"); 1346 1347 if ((versym & 0x8000) != 0) 1348 /* Ignore the symbol, it's not the default version. */ 1349 continue; 1350 } 1351 1352 /* See whether we know anything about this symbol. */ 1353 struct symbol search; 1354 search.name = elf_strptr (fileinfo->elf, symstridx, sym->st_name); 1355 unsigned long int hval = elf_hash (search.name); 1356 1357 /* We ignore the symbols the linker generates. This are 1358 _GLOBAL_OFFSET_TABLE_, _DYNAMIC. */ 1359 // XXX This loop is hot and the following tests hardly ever match. 1360 // XXX Maybe move the tests somewhere they are executed less often. 1361 if (((unlikely (hval == 165832675) 1362 && strcmp (search.name, "_DYNAMIC") == 0) 1363 || (unlikely (hval == 102264335) 1364 && strcmp (search.name, "_GLOBAL_OFFSET_TABLE_") == 0)) 1365 && sym->st_shndx != SHN_UNDEF 1366 /* If somebody defines such a variable in a relocatable we 1367 don't ignore it. Let the user get what s/he deserves. */ 1368 && fileinfo->file_type != relocatable_file_type) 1369 continue; 1370 1371 struct symbol *oldp = ld_symbol_tab_find (&ld_state.symbol_tab, 1372 hval, &search); 1373 struct symbol *newp; 1374 if (likely (oldp == NULL)) 1375 { 1376 /* No symbol of this name know. Add it. */ 1377 newp = (struct symbol *) obstack_alloc (&ld_state.smem, 1378 sizeof (*newp)); 1379 newp->name = search.name; 1380 newp->size = sym->st_size; 1381 newp->type = XELF_ST_TYPE (sym->st_info); 1382 newp->symidx = cnt; 1383 newp->outsymidx = 0; 1384 newp->outdynsymidx = 0; 1385 newp->scndx = shndx; 1386 newp->file = fileinfo; 1387 newp->defined = newp->scndx != SHN_UNDEF; 1388 newp->common = newp->scndx == SHN_COMMON; 1389 newp->weak = XELF_ST_BIND (sym->st_info) == STB_WEAK; 1390 newp->added = 0; 1391 newp->merged = 0; 1392 newp->need_copy = 0; 1393 newp->on_dsolist = 0; 1394 newp->in_dso = secttype == SHT_DYNSYM; 1395 newp->next_in_scn = NULL; 1396#ifndef NDEBUG 1397 newp->next = NULL; 1398 newp->previous = NULL; 1399#endif 1400 1401 if (newp->scndx == SHN_UNDEF) 1402 { 1403 CDBL_LIST_ADD_REAR (ld_state.unresolved, newp); 1404 ++ld_state.nunresolved; 1405 if (! newp->weak) 1406 ++ld_state.nunresolved_nonweak; 1407 } 1408 else if (newp->scndx == SHN_COMMON) 1409 { 1410 /* Store the alignment requirement. */ 1411 newp->merge.value = sym->st_value; 1412 1413 CDBL_LIST_ADD_REAR (ld_state.common_syms, newp); 1414 } 1415 1416 /* Insert the new symbol. */ 1417 if (unlikely (ld_symbol_tab_insert (&ld_state.symbol_tab, 1418 hval, newp) != 0)) 1419 /* This cannot happen. */ 1420 abort (); 1421 1422 fileinfo->symref[cnt] = newp; 1423 1424 /* We have a few special symbols to recognize. The symbols 1425 _init and _fini are the initialization and finalization 1426 functions respectively. They have to be made known in 1427 the dynamic section and therefore we have to find out 1428 now whether these functions exist or not. */ 1429 if (hval == 6685956 && strcmp (newp->name, "_init") == 0) 1430 ld_state.init_symbol = newp; 1431 else if (hval == 6672457 && strcmp (newp->name, "_fini") == 0) 1432 ld_state.fini_symbol = newp; 1433 } 1434 else if (unlikely (check_definition (sym, cnt, fileinfo, oldp) != 0)) 1435 /* A fatal error (multiple definition of a symbol) 1436 occurred, no need to continue. */ 1437 return 1; 1438 else 1439 /* Use the previously allocated symbol record. It has 1440 been updated in check_definition(), if necessary. */ 1441 newp = fileinfo->symref[cnt] = oldp; 1442 1443 /* Mark the section the symbol we need comes from as used. */ 1444 if (shndx != SHN_UNDEF 1445 && (shndx < SHN_LORESERVE || shndx > SHN_HIRESERVE)) 1446 { 1447 struct scninfo *ignore; 1448 1449#ifndef NDEBUG 1450 size_t shnum; 1451 assert (elf_getshnum (fileinfo->elf, &shnum) == 0); 1452 assert (shndx < shnum); 1453#endif 1454 1455 /* Mark section (and all dependencies) as used. */ 1456 mark_section_used (&fileinfo->scninfo[shndx], shndx, &ignore); 1457 1458 /* Check whether the section is merge-able. In this case we 1459 have to record the symbol. */ 1460 if (SCNINFO_SHDR (fileinfo->scninfo[shndx].shdr).sh_flags 1461 & SHF_MERGE) 1462 { 1463 if (fileinfo->scninfo[shndx].symbols == NULL) 1464 fileinfo->scninfo[shndx].symbols = newp->next_in_scn 1465 = newp; 1466 else 1467 { 1468 newp->next_in_scn 1469 = fileinfo->scninfo[shndx].symbols->next_in_scn; 1470 fileinfo->scninfo[shndx].symbols 1471 = fileinfo->scninfo[shndx].symbols->next_in_scn = newp; 1472 } 1473 } 1474 } 1475 } 1476 1477 /* This file is used. */ 1478 if (likely (fileinfo->file_type == relocatable_file_type)) 1479 { 1480 if (unlikely (ld_state.relfiles == NULL)) 1481 ld_state.relfiles = fileinfo->next = fileinfo; 1482 else 1483 { 1484 fileinfo->next = ld_state.relfiles->next; 1485 ld_state.relfiles = ld_state.relfiles->next = fileinfo; 1486 } 1487 1488 /* Update some summary information in the state structure. */ 1489 ld_state.nsymtab += fileinfo->nsymtab; 1490 ld_state.nlocalsymbols += fileinfo->nlocalsymbols; 1491 } 1492 else if (likely (fileinfo->file_type == dso_file_type)) 1493 { 1494 CSNGL_LIST_ADD_REAR (ld_state.dsofiles, fileinfo); 1495 ++ld_state.ndsofiles; 1496 1497 if (fileinfo->lazyload) 1498 /* We have to create another dynamic section entry for the 1499 DT_POSFLAG_1 entry. 1500 1501 XXX Once more functionality than the lazyloading flag 1502 are suppported the test must be extended. */ 1503 ++ld_state.ndsofiles; 1504 } 1505 } 1506 1507 return 0; 1508} 1509 1510 1511int 1512ld_handle_filename_list (struct filename_list *fnames) 1513{ 1514 struct filename_list *runp; 1515 int res = 0; 1516 1517 for (runp = fnames; runp != NULL; runp = runp->next) 1518 { 1519 struct usedfiles *curp; 1520 1521 /* Create a record for the new file. */ 1522 curp = runp->real = ld_new_inputfile (runp->name, relocatable_file_type); 1523 1524 /* Set flags for group handling. */ 1525 runp->real->group_start = runp->group_start; 1526 runp->real->group_end = runp->group_end; 1527 1528 /* Read the file and everything else which comes up, including 1529 handling groups. */ 1530 do 1531 res |= FILE_PROCESS (-1, curp, &ld_state, &curp); 1532 while (curp != NULL); 1533 } 1534 1535 /* Free the list. */ 1536 while (fnames != NULL) 1537 { 1538 runp = fnames; 1539 fnames = fnames->next; 1540 free (runp); 1541 } 1542 1543 return res; 1544} 1545 1546 1547/* Handle opening of the given file with ELF descriptor. */ 1548static int 1549open_elf (struct usedfiles *fileinfo, Elf *elf) 1550{ 1551 int res = 0; 1552 1553 if (elf == NULL) 1554 error (EXIT_FAILURE, 0, 1555 gettext ("cannot get descriptor for ELF file (%s:%d): %s\n"), 1556 __FILE__, __LINE__, elf_errmsg (-1)); 1557 1558 if (unlikely (elf_kind (elf) == ELF_K_NONE)) 1559 { 1560 struct filename_list *fnames; 1561 1562 /* We don't have to look at this file again. */ 1563 fileinfo->status = closed; 1564 1565 /* Let's see whether this is a linker script. */ 1566 if (fileinfo->fd != -1) 1567 /* Create a stream from the file handle we know. */ 1568 ldin = fdopen (fileinfo->fd, "r"); 1569 else 1570 { 1571 /* Get the memory for the archive member. */ 1572 char *content; 1573 size_t contentsize; 1574 1575 /* Get the content of the file. */ 1576 content = elf_rawfile (elf, &contentsize); 1577 if (content == NULL) 1578 { 1579 fprintf (stderr, gettext ("%s: invalid ELF file (%s:%d)\n"), 1580 fileinfo->rfname, __FILE__, __LINE__); 1581 return 1; 1582 } 1583 1584 /* The content of the file is available in memory. Read the 1585 memory region as a stream. */ 1586 ldin = fmemopen (content, contentsize, "r"); 1587 } 1588 1589 /* No need for locking. */ 1590 __fsetlocking (ldin, FSETLOCKING_BYCALLER); 1591 1592 if (ldin == NULL) 1593 error (EXIT_FAILURE, errno, gettext ("cannot open \"%s\""), 1594 fileinfo->rfname); 1595 1596 /* Parse the file. If it is a linker script no problems will be 1597 reported. */ 1598 ld_state.srcfiles = NULL; 1599 ldlineno = 1; 1600 ld_scan_version_script = 0; 1601 ldin_fname = fileinfo->rfname; 1602 res = ldparse (); 1603 1604 fclose (ldin); 1605 if (fileinfo->fd != -1 && !fileinfo->fd_passed) 1606 { 1607 /* We won't need the file descriptor again. */ 1608 close (fileinfo->fd); 1609 fileinfo->fd = -1; 1610 } 1611 1612 elf_end (elf); 1613 1614 if (unlikely (res != 0)) 1615 /* Something went wrong during parsing. */ 1616 return 1; 1617 1618 /* This is no ELF file. */ 1619 fileinfo->elf = NULL; 1620 1621 /* Now we have to handle eventual INPUT and GROUP statements in 1622 the script. Read the files mentioned. */ 1623 fnames = ld_state.srcfiles; 1624 if (fnames != NULL) 1625 { 1626 struct filename_list *oldp; 1627 1628 /* Convert the list into a normal single-linked list. */ 1629 oldp = fnames; 1630 fnames = fnames->next; 1631 oldp->next = NULL; 1632 1633 /* Remove the list from the state structure. */ 1634 ld_state.srcfiles = NULL; 1635 1636 if (unlikely (ld_handle_filename_list (fnames) != 0)) 1637 return 1; 1638 } 1639 1640 return 0; 1641 } 1642 1643 /* Store the file info. */ 1644 fileinfo->elf = elf; 1645 1646 /* The file is ready for action. */ 1647 fileinfo->status = opened; 1648 1649 return 0; 1650} 1651 1652 1653static int 1654add_whole_archive (struct usedfiles *fileinfo) 1655{ 1656 Elf *arelf; 1657 Elf_Cmd cmd = ELF_C_READ_MMAP_PRIVATE; 1658 int res = 0; 1659 1660 while ((arelf = elf_begin (fileinfo->fd, cmd, fileinfo->elf)) != NULL) 1661 { 1662 Elf_Arhdr *arhdr = elf_getarhdr (arelf); 1663 struct usedfiles *newp; 1664 1665 if (arhdr == NULL) 1666 abort (); 1667 1668 /* Just to be sure; since these are no files in the archive 1669 these names should never be returned. */ 1670 assert (strcmp (arhdr->ar_name, "/") != 0); 1671 assert (strcmp (arhdr->ar_name, "//") != 0); 1672 1673 newp = ld_new_inputfile (arhdr->ar_name, relocatable_file_type); 1674 newp->archive_file = fileinfo; 1675 1676 if (unlikely (ld_state.trace_files)) 1677 print_file_name (stdout, newp, 1, 1); 1678 1679 /* This shows that this file is contained in an archive. */ 1680 newp->fd = -1; 1681 /* Store the ELF descriptor. */ 1682 newp->elf = arelf; 1683 /* Show that we are open for business. */ 1684 newp->status = opened; 1685 1686 /* Proces the file, add all the symbols etc. */ 1687 res = file_process2 (newp); 1688 if (unlikely (res != 0)) 1689 break; 1690 1691 /* Advance to the next archive element. */ 1692 cmd = elf_next (arelf); 1693 } 1694 1695 return res; 1696} 1697 1698 1699static int 1700extract_from_archive (struct usedfiles *fileinfo) 1701{ 1702 static int archive_seq; 1703 int res = 0; 1704 1705 /* This is an archive we are not using completely. Give it a 1706 unique number. */ 1707 fileinfo->archive_seq = ++archive_seq; 1708 1709 /* If there are no unresolved symbols don't do anything. */ 1710 if ((likely (ld_state.extract_rule == defaultextract) 1711 && ld_state.nunresolved_nonweak == 0) 1712 || (unlikely (ld_state.extract_rule == weakextract) 1713 && ld_state.nunresolved == 0)) 1714 return 0; 1715 1716 Elf_Arsym *syms; 1717 size_t nsyms; 1718 1719 /* Get all the symbols. */ 1720 syms = elf_getarsym (fileinfo->elf, &nsyms); 1721 if (syms == NULL) 1722 { 1723 cannot_read_archive: 1724 error (0, 0, gettext ("cannot read archive `%s': %s"), 1725 fileinfo->rfname, elf_errmsg (-1)); 1726 1727 /* We cannot use this archive anymore. */ 1728 fileinfo->status = closed; 1729 1730 return 1; 1731 } 1732 1733 /* Now add all the symbols to the hash table. Note that there 1734 can potentially be duplicate definitions. We'll always use 1735 the first definition. */ 1736 // XXX Is this a compatible behavior? 1737 bool any_used; 1738 int nround = 0; 1739 do 1740 { 1741 any_used = false; 1742 1743 size_t cnt; 1744 for (cnt = 0; cnt < nsyms; ++cnt) 1745 { 1746 struct symbol search = { .name = syms[cnt].as_name }; 1747 struct symbol *sym = ld_symbol_tab_find (&ld_state.symbol_tab, 1748 syms[cnt].as_hash, &search); 1749 if (sym != NULL && ! sym->defined) 1750 { 1751 /* The symbol is referenced and not defined. */ 1752 Elf *arelf; 1753 Elf_Arhdr *arhdr; 1754 struct usedfiles *newp; 1755 1756 /* Find the archive member for this symbol. */ 1757 if (unlikely (elf_rand (fileinfo->elf, syms[cnt].as_off) 1758 != syms[cnt].as_off)) 1759 goto cannot_read_archive; 1760 1761 /* Note: no test of a failing 'elf_begin' call. That's fine 1762 since 'elf'getarhdr' will report the problem. */ 1763 arelf = elf_begin (fileinfo->fd, ELF_C_READ_MMAP_PRIVATE, 1764 fileinfo->elf); 1765 arhdr = elf_getarhdr (arelf); 1766 if (arhdr == NULL) 1767 goto cannot_read_archive; 1768 1769 /* We have all the information and an ELF handle for the 1770 archive member. Create the normal data structure for 1771 a file now. */ 1772 newp = ld_new_inputfile (obstack_strdup (&ld_state.smem, 1773 arhdr->ar_name), 1774 relocatable_file_type); 1775 newp->archive_file = fileinfo; 1776 1777 if (unlikely (ld_state.trace_files)) 1778 print_file_name (stdout, newp, 1, 1); 1779 1780 /* This shows that this file is contained in an archive. */ 1781 newp->fd = -1; 1782 /* Store the ELF descriptor. */ 1783 newp->elf = arelf; 1784 /* Show that we are open for business. */ 1785 newp->status = in_archive; 1786 1787 /* Now read the file and add all the symbols. */ 1788 res = file_process2 (newp); 1789 if (unlikely (res != 0)) 1790 return res; 1791 1792 any_used = true; 1793 } 1794 } 1795 1796 if (++nround == 1) 1797 { 1798 /* This is an archive therefore it must have a number. */ 1799 assert (fileinfo->archive_seq != 0); 1800 ld_state.last_archive_used = fileinfo->archive_seq; 1801 } 1802 } 1803 while (any_used); 1804 1805 return res; 1806} 1807 1808 1809static int 1810file_process2 (struct usedfiles *fileinfo) 1811{ 1812 int res; 1813 1814 if (likely (elf_kind (fileinfo->elf) == ELF_K_ELF)) 1815 { 1816 /* The first time we get here we read the ELF header. */ 1817#if NATIVE_ELF != 0 1818 if (likely (fileinfo->ehdr == NULL)) 1819#else 1820 if (likely (FILEINFO_EHDR (fileinfo->ehdr).e_type == ET_NONE)) 1821#endif 1822 { 1823 XElf_Ehdr *ehdr; 1824#if NATIVE_ELF != 0 1825 ehdr = xelf_getehdr (fileinfo->elf, fileinfo->ehdr); 1826#else 1827 xelf_getehdr_copy (fileinfo->elf, ehdr, fileinfo->ehdr); 1828#endif 1829 if (ehdr == NULL) 1830 { 1831 fprintf (stderr, gettext ("%s: invalid ELF file (%s:%d)\n"), 1832 fileinfo->rfname, __FILE__, __LINE__); 1833 fileinfo->status = closed; 1834 return 1; 1835 } 1836 1837 if (FILEINFO_EHDR (fileinfo->ehdr).e_type != ET_REL 1838 && unlikely (FILEINFO_EHDR (fileinfo->ehdr).e_type != ET_DYN)) 1839 /* XXX Add ebl* function to query types which are allowed 1840 to link in. */ 1841 { 1842 char buf[64]; 1843 1844 print_file_name (stderr, fileinfo, 1, 0); 1845 fprintf (stderr, 1846 gettext ("file of type %s cannot be linked in\n"), 1847 ebl_object_type_name (ld_state.ebl, 1848 FILEINFO_EHDR (fileinfo->ehdr).e_type, 1849 buf, sizeof (buf))); 1850 fileinfo->status = closed; 1851 return 1; 1852 } 1853 1854 /* Determine the section header string table section index. */ 1855 if (unlikely (elf_getshstrndx (fileinfo->elf, &fileinfo->shstrndx) 1856 < 0)) 1857 { 1858 fprintf (stderr, gettext ("\ 1859%s: cannot get section header string table index: %s\n"), 1860 fileinfo->rfname, elf_errmsg (-1)); 1861 fileinfo->status = closed; 1862 return 1; 1863 } 1864 } 1865 1866 /* Now handle the different types of files. */ 1867 if (FILEINFO_EHDR (fileinfo->ehdr).e_type == ET_REL) 1868 { 1869 /* Add all the symbol. Relocatable files have symbol 1870 tables. */ 1871 res = add_relocatable_file (fileinfo, SHT_SYMTAB); 1872 } 1873 else 1874 { 1875 bool has_l_name = fileinfo->file_type == archive_file_type; 1876 1877 assert (FILEINFO_EHDR (fileinfo->ehdr).e_type == ET_DYN); 1878 1879 /* If the file is a DT_NEEDED dependency then the type is 1880 already correctly specified. */ 1881 if (fileinfo->file_type != dso_needed_file_type) 1882 fileinfo->file_type = dso_file_type; 1883 1884 /* We cannot use DSOs when generating relocatable objects. */ 1885 if (ld_state.file_type == relocatable_file_type) 1886 { 1887 error (0, 0, gettext ("\ 1888cannot use DSO '%s' when generating relocatable object file"), 1889 fileinfo->fname); 1890 return 1; 1891 } 1892 1893 /* Add all the symbols. For DSOs we are looking at the 1894 dynamic symbol table. */ 1895 res = add_relocatable_file (fileinfo, SHT_DYNSYM); 1896 1897 /* We always have to have a dynamic section. */ 1898 assert (fileinfo->dynscn != NULL); 1899 1900 /* We have to remember the dependencies for this object. It 1901 is necessary to look them up. */ 1902 XElf_Shdr_vardef (dynshdr); 1903 xelf_getshdr (fileinfo->dynscn, dynshdr); 1904 1905 Elf_Data *dyndata = elf_getdata (fileinfo->dynscn, NULL); 1906 /* XXX Should we flag the failure to get the dynamic section? */ 1907 if (dynshdr != NULL) 1908 { 1909 int cnt = dynshdr->sh_size / dynshdr->sh_entsize; 1910 XElf_Dyn_vardef (dyn); 1911 1912 while (--cnt >= 0) 1913 { 1914 xelf_getdyn (dyndata, cnt, dyn); 1915 if (dyn != NULL) 1916 { 1917 if(dyn->d_tag == DT_NEEDED) 1918 { 1919 struct usedfiles *newp; 1920 1921 newp = ld_new_inputfile (elf_strptr (fileinfo->elf, 1922 dynshdr->sh_link, 1923 dyn->d_un.d_val), 1924 dso_needed_file_type); 1925 1926 /* Enqueue the newly found dependencies. */ 1927 // XXX Check that there not already a file with the 1928 // same name. 1929 CSNGL_LIST_ADD_REAR (ld_state.needed, newp); 1930 } 1931 else if (dyn->d_tag == DT_SONAME) 1932 { 1933 /* We use the DT_SONAME (this is what's there 1934 for). */ 1935 fileinfo->soname = elf_strptr (fileinfo->elf, 1936 dynshdr->sh_link, 1937 dyn->d_un.d_val); 1938 has_l_name = false; 1939 } 1940 } 1941 } 1942 } 1943 1944 /* Construct the file name if the DSO has no SONAME and the 1945 file name comes from a -lXX parameter on the comment 1946 line. */ 1947 if (unlikely (has_l_name)) 1948 { 1949 /* The FNAME is the parameter the user specified on the 1950 command line. We prepend "lib" and append ".so". */ 1951 size_t len = strlen (fileinfo->fname) + 7; 1952 char *newp; 1953 1954 newp = (char *) obstack_alloc (&ld_state.smem, len); 1955 strcpy (stpcpy (stpcpy (newp, "lib"), fileinfo->fname), ".so"); 1956 1957 fileinfo->soname = newp; 1958 } 1959 } 1960 } 1961 else if (likely (elf_kind (fileinfo->elf) == ELF_K_AR)) 1962 { 1963 if (unlikely (ld_state.extract_rule == allextract)) 1964 /* Which this option enabled we have to add all the object 1965 files in the archive. */ 1966 res = add_whole_archive (fileinfo); 1967 else if (ld_state.file_type == relocatable_file_type) 1968 { 1969 /* When generating a relocatable object we don't find files 1970 in archives. */ 1971 if (verbose) 1972 error (0, 0, gettext ("input file '%s' ignored"), fileinfo->fname); 1973 1974 res = 0; 1975 } 1976 else 1977 /* Extract only the members from the archive which are 1978 currently referenced by unresolved symbols. */ 1979 res = extract_from_archive (fileinfo); 1980 } 1981 else 1982 /* This should never happen, we know about no other types. */ 1983 abort (); 1984 1985 return res; 1986} 1987 1988 1989/* Process a given file. The first parameter is a file descriptor for 1990 the file which can be -1 to indicate the file has not yet been 1991 found. The second parameter describes the file to be opened, the 1992 last one is the state of the linker which among other information 1993 contain the paths we look at. */ 1994static int 1995ld_generic_file_process (int fd, struct usedfiles *fileinfo, 1996 struct ld_state *statep, struct usedfiles **nextp) 1997{ 1998 int res = 0; 1999 2000 /* By default we go to the next file in the list. */ 2001 *nextp = fileinfo->next; 2002 2003 /* Set the flag to signal we are looking for a group start. */ 2004 if (unlikely (fileinfo->group_start)) 2005 { 2006 ld_state.group_start_requested = true; 2007 fileinfo->group_start = false; 2008 } 2009 2010 /* If the file isn't open yet, open it now. */ 2011 if (likely (fileinfo->status == not_opened)) 2012 { 2013 bool fd_passed = true; 2014 2015 if (likely (fd == -1)) 2016 { 2017 /* Find the file ourselves. */ 2018 int err = open_along_path (fileinfo); 2019 if (unlikely (err != 0)) 2020 /* We allow libraries and DSOs to be named more than once. 2021 Don't report an error to the caller. */ 2022 return err == EAGAIN ? 0 : err; 2023 2024 fd_passed = false; 2025 } 2026 else 2027 fileinfo->fd = fd; 2028 2029 /* Remember where we got the descriptor from. */ 2030 fileinfo->fd_passed = fd_passed; 2031 2032 /* We found the file. Now test whether it is a file type we can 2033 handle. 2034 2035 XXX Do we have to have the ability to start from a given 2036 position in the search path again to look for another file if 2037 the one found has not the right type? */ 2038 res = open_elf (fileinfo, elf_begin (fileinfo->fd, 2039 is_dso_p (fileinfo->fd) 2040 ? ELF_C_READ_MMAP 2041 : ELF_C_READ_MMAP_PRIVATE, NULL)); 2042 if (unlikely (res != 0)) 2043 return res; 2044 } 2045 2046 /* Now that we have opened the file start processing it. */ 2047 if (likely (fileinfo->status != closed)) 2048 res = file_process2 (fileinfo); 2049 2050 /* Determine which file to look at next. */ 2051 if (unlikely (fileinfo->group_backref != NULL)) 2052 { 2053 /* We only go back if an archive other than the one we would go 2054 back to has been used in the last round. */ 2055 if (ld_state.last_archive_used > fileinfo->group_backref->archive_seq) 2056 { 2057 *nextp = fileinfo->group_backref; 2058 ld_state.last_archive_used = 0; 2059 } 2060 else 2061 { 2062 /* If we come here this means that the archives we read so 2063 far are not needed anymore. We can free some of the data 2064 now. */ 2065 struct usedfiles *runp = ld_state.archives; 2066 2067 do 2068 { 2069 /* We don't need the ELF descriptor anymore. Unless there 2070 are no files from the archive used this will not free 2071 the whole file but only some data structures. */ 2072 elf_end (runp->elf); 2073 runp->elf = NULL; 2074 2075 runp = runp->next; 2076 } 2077 while (runp != fileinfo->next); 2078 } 2079 } 2080 else if (unlikely (fileinfo->group_end)) 2081 { 2082 /* This is the end of a group. We possibly of to go back. 2083 Determine which file we would go back to and see whether it 2084 makes sense. If there has not been an archive we don't have 2085 to do anything. */ 2086 if (!ld_state.group_start_requested) 2087 { 2088 if (ld_state.group_start_archive != ld_state.tailarchives) 2089 /* The loop would include more than one archive, add the 2090 pointer. */ 2091 { 2092 *nextp = ld_state.tailarchives->group_backref = 2093 ld_state.group_start_archive; 2094 ld_state.last_archive_used = 0; 2095 } 2096 else 2097 /* We might still have to go back to the beginning of the 2098 group if since the last archive other files have been 2099 added. But we go back exactly once. */ 2100 if (ld_state.tailarchives != fileinfo) 2101 { 2102 *nextp = ld_state.group_start_archive; 2103 ld_state.last_archive_used = 0; 2104 } 2105 } 2106 2107 /* Clear the flags. */ 2108 ld_state.group_start_requested = false; 2109 fileinfo->group_end = false; 2110 } 2111 2112 return res; 2113} 2114 2115 2116/* Library names passed to the linker as -lXX represent files named 2117 libXX.YY. The YY part can have different forms, depending on the 2118 platform. The generic set is .so and .a (in this order). */ 2119static const char ** 2120ld_generic_lib_extensions (struct ld_state *statep __attribute__ ((__unused__))) 2121{ 2122 static const char *exts[] = 2123 { 2124 ".so", ".a", NULL 2125 }; 2126 2127 return exts; 2128} 2129 2130 2131/* Flag unresolved symbols. */ 2132static int 2133ld_generic_flag_unresolved (struct ld_state *statep) 2134{ 2135 int retval = 0; 2136 2137 if (ld_state.nunresolved_nonweak > 0) 2138 { 2139 /* Go through the list and determine the unresolved symbols. */ 2140 struct symbol *first; 2141 struct symbol *s; 2142 2143 s = first = ld_state.unresolved->next; 2144 do 2145 { 2146 if (! s->defined && ! s->weak) 2147 { 2148 /* Two special symbol we recognize: the symbol for the 2149 GOT and the dynamic section. */ 2150 if (strcmp (s->name, "_GLOBAL_OFFSET_TABLE_") == 0 2151 || strcmp (s->name, "_DYNAMIC") == 0) 2152 { 2153 /* We will have to fill in more information later. */ 2154 ld_state.need_got = true; 2155 2156 /* Remember that we found it. */ 2157 if (s->name[1] == 'G') 2158 ld_state.got_symbol = s; 2159 else 2160 ld_state.dyn_symbol = s; 2161 } 2162 else if (ld_state.file_type != dso_file_type || !ld_state.nodefs) 2163 { 2164 /* XXX The error message should get better. It should use 2165 the debugging information if present to tell where in the 2166 sources the undefined reference is. */ 2167 error (0, 0, gettext ("undefined symbol `%s' in %s"), 2168 s->name, s->file->fname); 2169 2170 retval = 1; 2171 } 2172 } 2173 2174 /* We cannot decide here what to do with undefined 2175 references which will come from DSO since we do not know 2176 what kind of symbol we expect. Only when looking at the 2177 relocations we can see whether we need a PLT entry or 2178 only a GOT entry. */ 2179 2180 s = s->next; 2181 } 2182 while (s != first); 2183 } 2184 2185 return retval; 2186} 2187 2188 2189/* Close the given file. */ 2190static int 2191ld_generic_file_close (struct usedfiles *fileinfo, struct ld_state *statep) 2192{ 2193 /* Close the ELF descriptor. */ 2194 elf_end (fileinfo->elf); 2195 2196 /* If we have opened the file descriptor close it. But we might 2197 have done this already in which case FD is -1. */ 2198 if (!fileinfo->fd_passed && fileinfo->fd != -1) 2199 close (fileinfo->fd); 2200 2201 /* We allocated the resolved file name. */ 2202 if (fileinfo->fname != fileinfo->rfname) 2203 free ((char *) fileinfo->rfname); 2204 2205 return 0; 2206} 2207 2208 2209static void 2210new_generated_scn (enum scn_kind kind, const char *name, int type, int flags, 2211 int entsize, int align) 2212{ 2213 struct scnhead *newp; 2214 2215 newp = (struct scnhead *) obstack_calloc (&ld_state.smem, 2216 sizeof (struct scnhead)); 2217 newp->kind = kind; 2218 newp->name = name; 2219 newp->nameent = ebl_strtabadd (ld_state.shstrtab, name, 0); 2220 newp->type = type; 2221 newp->flags = flags; 2222 newp->entsize = entsize; 2223 newp->align = align; 2224 newp->grp_signature = NULL; 2225 newp->used = true; 2226 2227 /* All is well. Create now the data for the section and insert it 2228 into the section table. */ 2229 ld_section_tab_insert (&ld_state.section_tab, elf_hash (name), newp); 2230} 2231 2232 2233/* Create the sections which are generated by the linker and are not 2234 present in the input file. */ 2235static void 2236ld_generic_generate_sections (struct ld_state *statep) 2237{ 2238 /* The relocation section type. */ 2239 int rel_type = REL_TYPE (&ld_state) == DT_REL ? SHT_REL : SHT_RELA; 2240 2241 /* When building dynamically linked object we have to include a 2242 section containing a string describing the interpreter. This 2243 should be at the very beginning of the file together with the 2244 other information the ELF loader (kernel or wherever) has to look 2245 at. We put it as the first section in the file. 2246 2247 We also have to create the dynamic segment which is a special 2248 section the dynamic linker locates through an entry in the 2249 program header. */ 2250 if (dynamically_linked_p ()) 2251 { 2252 int ndt_needed; 2253 /* Use any versioning (defined or required)? */ 2254 bool use_versioning = false; 2255 /* Use version requirements? */ 2256 bool need_version = false; 2257 2258 /* First the .interp section. */ 2259 new_generated_scn (scn_dot_interp, ".interp", SHT_PROGBITS, SHF_ALLOC, 2260 0, 1); 2261 2262 /* Now the .dynamic section. */ 2263 new_generated_scn (scn_dot_dynamic, ".dynamic", SHT_DYNAMIC, 2264 DYNAMIC_SECTION_FLAGS (&ld_state), 2265 xelf_fsize (ld_state.outelf, ELF_T_DYN, 1), 2266 xelf_fsize (ld_state.outelf, ELF_T_ADDR, 1)); 2267 2268 /* We will need in any case the dynamic symbol table (even in 2269 the unlikely case that no symbol is exported or referenced 2270 from a DSO). */ 2271 ld_state.need_dynsym = true; 2272 new_generated_scn (scn_dot_dynsym, ".dynsym", SHT_DYNSYM, SHF_ALLOC, 2273 xelf_fsize (ld_state.outelf, ELF_T_SYM, 1), 2274 xelf_fsize (ld_state.outelf, ELF_T_ADDR, 1)); 2275 /* It comes with a string table. */ 2276 new_generated_scn (scn_dot_dynstr, ".dynstr", SHT_STRTAB, SHF_ALLOC, 2277 0, 1); 2278 /* And a hashing table. */ 2279 // XXX For Linux/Alpha we need other sizes unless they change... 2280 new_generated_scn (scn_dot_hash, ".hash", SHT_HASH, SHF_ALLOC, 2281 sizeof (Elf32_Word), sizeof (Elf32_Word)); 2282 2283 /* By default we add all DSOs provided on the command line. If 2284 the user added '-z ignore' to the command line we only add 2285 those which are actually used. */ 2286 ndt_needed = ld_state.ignore_unused_dsos ? 0 : ld_state.ndsofiles; 2287 2288 /* Create the section associated with the PLT if necessary. */ 2289 if (ld_state.nplt > 0) 2290 { 2291 /* Create the .plt section. */ 2292 /* XXX We might need a function which returns the section flags. */ 2293 new_generated_scn (scn_dot_plt, ".plt", SHT_PROGBITS, 2294 SHF_ALLOC | SHF_EXECINSTR, 2295 /* XXX Is the size correct? */ 2296 xelf_fsize (ld_state.outelf, ELF_T_ADDR, 1), 2297 xelf_fsize (ld_state.outelf, ELF_T_ADDR, 1)); 2298 2299 /* Create the relocation section for the .plt. This is always 2300 separate even if the other relocation sections are combined. */ 2301 new_generated_scn (scn_dot_pltrel, ".rel.plt", rel_type, SHF_ALLOC, 2302 rel_type == SHT_REL 2303 ? xelf_fsize (ld_state.outelf, ELF_T_REL, 1) 2304 : xelf_fsize (ld_state.outelf, ELF_T_RELA, 1), 2305 xelf_fsize (ld_state.outelf, ELF_T_ADDR, 1)); 2306 2307 /* This means we will also need the .got section. */ 2308 ld_state.need_got = true; 2309 2310 /* Mark all used DSOs as used. Determine whether any referenced 2311 object uses symbol versioning. */ 2312 if (ld_state.from_dso != NULL) 2313 { 2314 struct symbol *srunp = ld_state.from_dso; 2315 2316 do 2317 { 2318 srunp->file->used = true; 2319 2320 if (srunp->file->verdefdata != NULL) 2321 { 2322 XElf_Versym versym; 2323 2324 /* The input DSO uses versioning. */ 2325 use_versioning = true; 2326 /* We reference versions. */ 2327 need_version = true; 2328 2329 if (xelf_getversym_copy (srunp->file->versymdata, 2330 srunp->symidx, versym) == NULL) 2331 assert (! "xelf_getversym failed"); 2332 2333 /* We cannot link explicitly with an older 2334 version of a symbol. */ 2335 assert ((versym & 0x8000) == 0); 2336 /* We cannot reference local (index 0) or plain 2337 global (index 1) versions. */ 2338 assert (versym > 1); 2339 2340 /* Check whether we have already seen the 2341 version and if not add it to the referenced 2342 versions in the output file. */ 2343 if (! srunp->file->verdefused[versym]) 2344 { 2345 srunp->file->verdefused[versym] = 1; 2346 2347 if (++srunp->file->nverdefused == 1) 2348 /* Count the file if it is using versioning. */ 2349 ++ld_state.nverdeffile; 2350 ++ld_state.nverdefused; 2351 } 2352 } 2353 } 2354 while ((srunp = srunp->next) != ld_state.from_dso); 2355 } 2356 2357 /* Create the sections used to record version dependencies. */ 2358 if (need_version) 2359 new_generated_scn (scn_dot_version_r, ".gnu.version_r", 2360 SHT_GNU_verneed, SHF_ALLOC, 0, 2361 xelf_fsize (ld_state.outelf, ELF_T_WORD, 1)); 2362 2363 /* Now count the used DSOs since this is what the user 2364 wants. */ 2365 ndt_needed = 0; 2366 if (ld_state.ndsofiles > 0) 2367 { 2368 struct usedfiles *frunp = ld_state.dsofiles; 2369 2370 do 2371 if (! ld_state.ignore_unused_dsos || frunp->used) 2372 { 2373 ++ndt_needed; 2374 if (frunp->lazyload) 2375 /* We have to create another dynamic section 2376 entry for the DT_POSFLAG_1 entry. 2377 2378 XXX Once more functionality than the 2379 lazyloading flag are suppported the test 2380 must be extended. */ 2381 ++ndt_needed; 2382 } 2383 while ((frunp = frunp->next) != ld_state.dsofiles); 2384 } 2385 } 2386 2387 if (use_versioning) 2388 new_generated_scn (scn_dot_version, ".gnu.version", SHT_GNU_versym, 2389 SHF_ALLOC, 2390 xelf_fsize (ld_state.outelf, ELF_T_HALF, 1), 2391 xelf_fsize (ld_state.outelf, ELF_T_HALF, 1)); 2392 2393 /* We need some entries all the time. */ 2394 ld_state.ndynamic = (7 + (ld_state.runpath != NULL 2395 || ld_state.rpath != NULL) 2396 + ndt_needed 2397 + (ld_state.init_symbol != NULL ? 1 : 0) 2398 + (ld_state.fini_symbol != NULL ? 1 : 0) 2399 + (use_versioning ? 1 : 0) 2400 + (need_version ? 2 : 0) 2401 + (ld_state.nplt > 0 ? 4 : 0) 2402 + (ld_state.relsize_total > 0 ? 3 : 0)); 2403 } 2404 2405 /* When creating a relocatable file or when we are not stripping the 2406 output file we create a symbol table. */ 2407 ld_state.need_symtab = (ld_state.file_type == relocatable_file_type 2408 || ld_state.strip == strip_none); 2409 2410 /* Add the .got section if needed. */ 2411 if (ld_state.need_got) 2412 /* XXX We might need a function which returns the section flags. */ 2413 new_generated_scn (scn_dot_got, ".got", SHT_PROGBITS, 2414 SHF_ALLOC | SHF_WRITE, 2415 xelf_fsize (ld_state.outelf, ELF_T_ADDR, 1), 2416 xelf_fsize (ld_state.outelf, ELF_T_ADDR, 1)); 2417 2418 /* Add the .rel.dyn section. */ 2419 if (ld_state.relsize_total > 0) 2420 new_generated_scn (scn_dot_dynrel, ".rel.dyn", rel_type, SHF_ALLOC, 2421 rel_type == SHT_REL 2422 ? xelf_fsize (ld_state.outelf, ELF_T_REL, 1) 2423 : xelf_fsize (ld_state.outelf, ELF_T_RELA, 1), 2424 xelf_fsize (ld_state.outelf, ELF_T_ADDR, 1)); 2425} 2426 2427 2428/* Callback function registered with on_exit to make sure the temporary 2429 files gets removed if something goes wrong. */ 2430static void 2431remove_tempfile (int status, void *arg) 2432{ 2433 if (status != 0 && ld_state.tempfname != NULL) 2434 unlink (ld_state.tempfname); 2435} 2436 2437 2438/* Create the output file. The file name is given or "a.out". We 2439 create as much of the ELF structure as possible. */ 2440static int 2441ld_generic_open_outfile (struct ld_state *statep, int machine, int klass, 2442 int data) 2443{ 2444 /* We do not create the new file right away with the final name. 2445 This would destroy an existing file with this name before a 2446 replacement is finalized. We create instead a temporary file in 2447 the same directory. */ 2448 if (ld_state.outfname == NULL) 2449 ld_state.outfname = "a.out"; 2450 2451 size_t outfname_len = strlen (ld_state.outfname); 2452 char *tempfname = (char *) obstack_alloc (&ld_state.smem, 2453 outfname_len + sizeof (".XXXXXX")); 2454 ld_state.tempfname = tempfname; 2455 2456 int fd; 2457 int try = 0; 2458 while (1) 2459 { 2460 strcpy (mempcpy (tempfname, ld_state.outfname, outfname_len), ".XXXXXX"); 2461 2462 /* The useof mktemp() here is fine. We do not want to use 2463 mkstemp() since then the umask isn't used. And the output 2464 file will have these permissions anyhow. Any intruder could 2465 change the file later if it would be possible now. */ 2466 if (mktemp (tempfname) != NULL 2467 && (fd = open (tempfname, O_RDWR | O_EXCL | O_CREAT | O_NOFOLLOW, 2468 ld_state.file_type == relocatable_file_type 2469 ? DEFFILEMODE : ACCESSPERMS)) != -1) 2470 break; 2471 2472 /* Failed this round. We keep trying a number of times. */ 2473 if (++try >= 10) 2474 error (EXIT_FAILURE, errno, gettext ("cannot create output file")); 2475 } 2476 ld_state.outfd = fd; 2477 2478 /* Make sure we remove the temporary file in case something goes 2479 wrong. */ 2480 on_exit (remove_tempfile, NULL); 2481 2482 /* Create the ELF file data for the output file. */ 2483 Elf *elf = ld_state.outelf = elf_begin (fd, 2484 conserve_memory 2485 ? ELF_C_WRITE : ELF_C_WRITE_MMAP, 2486 NULL); 2487 if (elf == NULL) 2488 error (EXIT_FAILURE, 0, 2489 gettext ("cannot create ELF descriptor for output file: %s"), 2490 elf_errmsg (-1)); 2491 2492 /* Create the basic data structures. */ 2493 if (! xelf_newehdr (elf, klass)) 2494 /* Couldn't create the ELF header. Very bad. */ 2495 error (EXIT_FAILURE, 0, 2496 gettext ("could not create ELF header for output file: %s"), 2497 elf_errmsg (-1)); 2498 2499 /* And get the current header so that we can modify it. */ 2500 XElf_Ehdr_vardef (ehdr); 2501 xelf_getehdr (elf, ehdr); 2502 assert (ehdr != NULL); 2503 2504 /* Set the machine type. */ 2505 ehdr->e_machine = machine; 2506 2507 /* Modify it according to the info we have here and now. */ 2508 if (ld_state.file_type == executable_file_type) 2509 ehdr->e_type = ET_EXEC; 2510 else if (ld_state.file_type == dso_file_type) 2511 ehdr->e_type = ET_DYN; 2512 else 2513 { 2514 assert (ld_state.file_type == relocatable_file_type); 2515 ehdr->e_type = ET_REL; 2516 } 2517 2518 /* Set the ELF version. */ 2519 ehdr->e_version = EV_CURRENT; 2520 2521 /* Set the endianness. */ 2522 ehdr->e_ident[EI_DATA] = data; 2523 2524 /* Write the ELF header information back. */ 2525 (void) xelf_update_ehdr (elf, ehdr); 2526 2527 return 0; 2528} 2529 2530 2531/* We compute the offsets of the various copied objects and the total 2532 size of the memory needed. */ 2533// XXX The method used here is simple: go from front to back and pack 2534// the objects in this order. A more space efficient way would 2535// actually trying to pack the objects as dense as possible. But this 2536// is more expensive. 2537static void 2538compute_copy_reloc_offset (XElf_Shdr *shdr) 2539{ 2540 struct symbol *runp = ld_state.from_dso; 2541 assert (runp != NULL); 2542 2543 XElf_Off maxalign = 1; 2544 XElf_Off offset = 0; 2545 2546 do 2547 if (runp->need_copy) 2548 { 2549 /* Determine alignment for the symbol. */ 2550 // XXX The question is how? The symbol record itself does not 2551 // have the information. So we have to be conservative and 2552 // assume the alignment of the section the symbol is in. 2553 2554 // XXX We can be more precise. Use the offset from the beginning 2555 // of the section and determine the largest power of two with 2556 // module zero. 2557 XElf_Off symalign = MAX (SCNINFO_SHDR (runp->file->scninfo[runp->scndx].shdr).sh_addralign, 1); 2558 /* Keep track of the maximum alignment requirement. */ 2559 maxalign = MAX (maxalign, symalign); 2560 2561 /* Align current position. */ 2562 offset = (offset + symalign - 1) & ~(symalign - 1); 2563 2564 runp->merge.value = offset; 2565 2566 offset += runp->size; 2567 } 2568 while ((runp = runp->next) != ld_state.from_dso); 2569 2570 shdr->sh_type = SHT_NOBITS; 2571 shdr->sh_size = offset; 2572 shdr->sh_addralign = maxalign; 2573} 2574 2575 2576static void 2577compute_common_symbol_offset (XElf_Shdr *shdr) 2578{ 2579 struct symbol *runp = ld_state.common_syms; 2580 assert (runp != NULL); 2581 2582 XElf_Off maxalign = 1; 2583 XElf_Off offset = 0; 2584 2585 do 2586 { 2587 /* Determine alignment for the symbol. */ 2588 XElf_Off symalign = runp->merge.value; 2589 2590 /* Keep track of the maximum alignment requirement. */ 2591 maxalign = MAX (maxalign, symalign); 2592 2593 /* Align current position. */ 2594 offset = (offset + symalign - 1) & ~(symalign - 1); 2595 2596 runp->merge.value = offset; 2597 2598 offset += runp->size; 2599 } 2600 while ((runp = runp->next) != ld_state.common_syms); 2601 2602 shdr->sh_type = SHT_NOBITS; 2603 shdr->sh_size = offset; 2604 shdr->sh_addralign = maxalign; 2605} 2606 2607 2608static void 2609sort_sections_generic (void) 2610{ 2611 /* XXX TBI */ 2612 abort (); 2613} 2614 2615 2616static int 2617match_section (const char *osectname, struct filemask_section_name *sectmask, 2618 struct scnhead **scnhead, bool new_section, size_t segment_nr) 2619{ 2620 struct scninfo *prevp; 2621 struct scninfo *runp; 2622 struct scninfo *notused; 2623 2624 if (fnmatch (sectmask->section_name->name, (*scnhead)->name, 0) != 0) 2625 /* The section name does not match. */ 2626 return new_section; 2627 2628 /* If this is a section generated by the linker it doesn't contain 2629 the regular information (i.e., input section data etc) and must 2630 be handle special. */ 2631 if ((*scnhead)->kind != scn_normal) 2632 { 2633 (*scnhead)->name = osectname; 2634 (*scnhead)->segment_nr = segment_nr; 2635 2636 /* We have to count note section since they get their own 2637 program header entry. */ 2638 if ((*scnhead)->type == SHT_NOTE) 2639 ++ld_state.nnotesections; 2640 2641 ld_state.allsections[ld_state.nallsections++] = (*scnhead); 2642 return true; 2643 } 2644 2645 /* Now we have to match the file names of the input files. Some of 2646 the sections here might not match. */ 2647 runp = (*scnhead)->last->next; 2648 prevp = (*scnhead)->last; 2649 notused = NULL; 2650 2651 do 2652 { 2653 /* Base of the file name the section comes from. */ 2654 const char *brfname = basename (runp->fileinfo->rfname); 2655 2656 /* If the section isn't used, the name doesn't match the positive 2657 inclusion list or the name does match the negative inclusion 2658 list, ignore the section. */ 2659 if (!runp->used 2660 || (sectmask->filemask != NULL 2661 && fnmatch (sectmask->filemask, brfname, 0) != 0) 2662 || (sectmask->excludemask != NULL 2663 && fnmatch (sectmask->excludemask, brfname, 0) == 0)) 2664 { 2665 /* This file does not match the file name masks. */ 2666 if (notused == NULL) 2667 notused = runp; 2668 2669 prevp = runp; 2670 runp = runp->next; 2671 if (runp == notused) 2672 runp = NULL; 2673 } 2674 /* The section fulfills all requirements, add it to the output 2675 file with the correct section name etc. */ 2676 else 2677 { 2678 struct scninfo *found = runp; 2679 2680 /* Remove this input section data buffer from the list. */ 2681 if (prevp != runp) 2682 runp = prevp->next = runp->next; 2683 else 2684 { 2685 free (*scnhead); 2686 *scnhead = NULL; 2687 runp = NULL; 2688 } 2689 2690 /* Create a new section for the output file if the 'new_section' 2691 flag says so. Otherwise append the buffer to the last 2692 section which we created in one of the last calls. */ 2693 if (new_section) 2694 { 2695 struct scnhead *newp; 2696 2697 newp = (struct scnhead *) obstack_calloc (&ld_state.smem, 2698 sizeof (*newp)); 2699 newp->kind = scn_normal; 2700 newp->name = osectname; 2701 newp->type = SCNINFO_SHDR (found->shdr).sh_type; 2702 newp->flags = SCNINFO_SHDR (found->shdr).sh_flags; 2703 newp->segment_nr = segment_nr; 2704 newp->last = found->next = found; 2705 newp->used = true; 2706 newp->relsize = found->relsize; 2707 newp->entsize = SCNINFO_SHDR (found->shdr).sh_entsize; 2708 2709 /* We have to count note section since they get their own 2710 program header entry. */ 2711 if (newp->type == SHT_NOTE) 2712 ++ld_state.nnotesections; 2713 2714 ld_state.allsections[ld_state.nallsections++] = newp; 2715 new_section = false; 2716 } 2717 else 2718 { 2719 struct scnhead *queued; 2720 2721 queued = ld_state.allsections[ld_state.nallsections - 1]; 2722 2723 found->next = queued->last->next; 2724 queued->last = queued->last->next = found; 2725 2726 /* If the linker script forces us to add incompatible 2727 sections together do so. But reflect this in the 2728 type and flags of the resulting file. */ 2729 if (queued->type != SCNINFO_SHDR (found->shdr).sh_type) 2730 /* XXX Any better choice? */ 2731 queued->type = SHT_PROGBITS; 2732 if (queued->flags != SCNINFO_SHDR (found->shdr).sh_flags) 2733 queued->flags = ebl_sh_flags_combine (ld_state.ebl, 2734 queued->flags, 2735 SCNINFO_SHDR (found->shdr).sh_flags); 2736 2737 /* Accumulate the relocation section size. */ 2738 queued->relsize += found->relsize; 2739 } 2740 } 2741 } 2742 while (runp != NULL); 2743 2744 return new_section; 2745} 2746 2747 2748static void 2749sort_sections_lscript (void) 2750{ 2751 struct scnhead *temp[ld_state.nallsections]; 2752 2753 /* Make a copy of the section head pointer array. */ 2754 memcpy (temp, ld_state.allsections, 2755 ld_state.nallsections * sizeof (temp[0])); 2756 size_t nallsections = ld_state.nallsections; 2757 2758 /* Convert the output segment list in a single-linked list. */ 2759 struct output_segment *segment = ld_state.output_segments->next; 2760 ld_state.output_segments->next = NULL; 2761 ld_state.output_segments = segment; 2762 2763 /* Put the sections in the correct order in the array in the state 2764 structure. This might involve merging of sections and also 2765 renaming the containing section in the output file. */ 2766 ld_state.nallsections = 0; 2767 size_t segment_nr; 2768 size_t last_writable = ~0; 2769 for (segment_nr = 0; segment != NULL; segment = segment->next, ++segment_nr) 2770 { 2771 struct output_rule *orule; 2772 2773 for (orule = segment->output_rules; orule != NULL; orule = orule->next) 2774 if (orule->tag == output_section) 2775 { 2776 struct input_rule *irule; 2777 bool new_section = true; 2778 2779 for (irule = orule->val.section.input; irule != NULL; 2780 irule = irule->next) 2781 if (irule->tag == input_section) 2782 { 2783 size_t cnt; 2784 2785 for (cnt = 0; cnt < nallsections; ++cnt) 2786 if (temp[cnt] != NULL) 2787 new_section = 2788 match_section (orule->val.section.name, 2789 irule->val.section, &temp[cnt], 2790 new_section, segment_nr); 2791 } 2792 } 2793 2794 if ((segment->mode & PF_W) != 0) 2795 last_writable = ld_state.nallsections - 1; 2796 } 2797 2798 /* In case we have to create copy relocations or we have common 2799 symbols, find the last writable segment and add one more data 2800 block. It will be a NOBITS block and take up no disk space. 2801 This is why it is important to get the last block. */ 2802 if (ld_state.ncopy > 0 || ld_state.common_syms != NULL) 2803 { 2804 if (last_writable == ~0) 2805 error (EXIT_FAILURE, 0, "no writable segment"); 2806 2807 if (ld_state.allsections[last_writable]->type != SHT_NOBITS) 2808 { 2809 /* Make room in the ALLSECTIONS array for a new section. 2810 There is guaranteed room in the array. We add the new 2811 entry after the last writable section. */ 2812 ++last_writable; 2813 memmove (&ld_state.allsections[last_writable + 1], 2814 &ld_state.allsections[last_writable], 2815 (ld_state.nallsections - last_writable) 2816 * sizeof (ld_state.allsections[0])); 2817 2818 ld_state.allsections[last_writable] = (struct scnhead *) 2819 obstack_calloc (&ld_state.smem, sizeof (struct scnhead)); 2820 2821 /* Name for the new section. */ 2822 ld_state.allsections[last_writable]->name = ".bss"; 2823 /* Type: NOBITS. */ 2824 ld_state.allsections[last_writable]->type = SHT_NOBITS; 2825 /* Same segment as the last writable section. */ 2826 ld_state.allsections[last_writable]->segment_nr 2827 = ld_state.allsections[last_writable - 1]->segment_nr; 2828 } 2829 } 2830 2831 /* Create common symbol data block. */ 2832 if (ld_state.ncopy > 0) 2833 { 2834#if NATIVE_ELF 2835 struct scninfo *si = (struct scninfo *) 2836 obstack_calloc (&ld_state.smem, sizeof (*si) + sizeof (XElf_Shdr)); 2837 si->shdr = (XElf_Shdr *) (si + 1); 2838#else 2839 struct scninfo *si = (struct scninfo *) obstack_calloc (&ld_state.smem, 2840 sizeof (*si)); 2841#endif 2842 2843 /* Get the information regarding the symbols with copy relocations. */ 2844 compute_copy_reloc_offset (&SCNINFO_SHDR (si->shdr)); 2845 2846 /* This section is needed. */ 2847 si->used = true; 2848 /* Remember for later the section data structure. */ 2849 ld_state.copy_section = si; 2850 2851 if (likely (ld_state.allsections[last_writable]->last != NULL)) 2852 { 2853 si->next = ld_state.allsections[last_writable]->last->next; 2854 ld_state.allsections[last_writable]->last->next = si; 2855 ld_state.allsections[last_writable]->last = si; 2856 } 2857 else 2858 ld_state.allsections[last_writable]->last = si->next = si; 2859 } 2860 2861 /* Create common symbol data block. */ 2862 if (ld_state.common_syms != NULL) 2863 { 2864#if NATIVE_ELF 2865 struct scninfo *si = (struct scninfo *) 2866 obstack_calloc (&ld_state.smem, sizeof (*si) + sizeof (XElf_Shdr)); 2867 si->shdr = (XElf_Shdr *) (si + 1); 2868#else 2869 struct scninfo *si = (struct scninfo *) obstack_calloc (&ld_state.smem, 2870 sizeof (*si)); 2871#endif 2872 2873 /* Get the information regarding the symbols with copy relocations. */ 2874 compute_common_symbol_offset (&SCNINFO_SHDR (si->shdr)); 2875 2876 /* This section is needed. */ 2877 si->used = true; 2878 /* Remember for later the section data structure. */ 2879 ld_state.common_section = si; 2880 2881 if (likely (ld_state.allsections[last_writable]->last != NULL)) 2882 { 2883 si->next = ld_state.allsections[last_writable]->last->next; 2884 ld_state.allsections[last_writable]->last->next = si; 2885 ld_state.allsections[last_writable]->last = si; 2886 } 2887 else 2888 ld_state.allsections[last_writable]->last = si->next = si; 2889 } 2890} 2891 2892 2893/* Create the output sections now. This requires knowledge about all 2894 the sections we will need. It may be necessary to sort sections in 2895 the order they are supposed to appear in the executable. The 2896 sorting use many different kinds of information to optimize the 2897 resulting binary. Important is to respect segment boundaries and 2898 the needed alignment. The mode of the segments will be determined 2899 afterwards automatically by the output routines. 2900 2901 The generic sorting routines work in one of two possible ways: 2902 2903 - if a linker script specifies the sections to be used in the 2904 output and assigns them to a segment this information is used; 2905 2906 - otherwise the linker will order the sections based on permissions 2907 and some special knowledge about section names.*/ 2908static void 2909ld_generic_create_sections (struct ld_state *statep) 2910{ 2911 struct scngroup *groups; 2912 size_t cnt; 2913 2914 /* For relocatable object we don't have to bother sorting the 2915 sections and we do want to preserve the relocation sections as 2916 they appear in the input files. */ 2917 if (ld_state.file_type != relocatable_file_type) 2918 { 2919 /* Collect all the relocation sections. They are handled 2920 separately. */ 2921 struct scninfo *list = NULL; 2922 for (cnt = 0; cnt < ld_state.nallsections; ++cnt) 2923 if ((ld_state.allsections[cnt]->type == SHT_REL 2924 || ld_state.allsections[cnt]->type == SHT_RELA) 2925 /* The generated relocation sections are not of any 2926 interest here. */ 2927 && ld_state.allsections[cnt]->last != NULL) 2928 { 2929 if (list == NULL) 2930 list = ld_state.allsections[cnt]->last; 2931 else 2932 { 2933 /* Merge the sections list. */ 2934 struct scninfo *first = list->next; 2935 list->next = ld_state.allsections[cnt]->last->next; 2936 ld_state.allsections[cnt]->last->next = first; 2937 list = ld_state.allsections[cnt]->last; 2938 } 2939 2940 /* Remove the entry from the section list. */ 2941 ld_state.allsections[cnt] = NULL; 2942 } 2943 ld_state.rellist = list; 2944 2945 if (ld_state.output_segments == NULL) 2946 /* Sort using builtin rules. */ 2947 sort_sections_generic (); 2948 else 2949 sort_sections_lscript (); 2950 } 2951 2952 /* Now iterate over the input sections and create the sections in the 2953 order they are required in the output file. */ 2954 for (cnt = 0; cnt < ld_state.nallsections; ++cnt) 2955 { 2956 struct scnhead *head = ld_state.allsections[cnt]; 2957 Elf_Scn *scn; 2958 XElf_Shdr_vardef (shdr); 2959 2960 /* Don't handle unused sections. */ 2961 if (!head->used) 2962 continue; 2963 2964 /* We first have to create the section group if necessary. 2965 Section group sections must come (in section index order) 2966 before any of the section contained. This all is necessary 2967 only for relocatable object as other object types are not 2968 allowed to contain section groups. */ 2969 if (ld_state.file_type == relocatable_file_type 2970 && unlikely (head->flags & SHF_GROUP)) 2971 { 2972 /* There is at least one section which is contained in a 2973 section group in the input file. This means we must 2974 create a section group here as well. The only problem is 2975 that not all input files have to have to same kind of 2976 partitioning of the sections. I.e., sections A and B in 2977 one input file and sections B and C in another input file 2978 can be in one group. That will result in a group 2979 containing the sections A, B, and C in the output 2980 file. */ 2981 struct scninfo *runp; 2982 Elf32_Word here_groupidx = 0; 2983 struct scngroup *here_group; 2984 struct member *newp; 2985 2986 /* First check whether any section is already in a group. 2987 In this case we have to add this output section, too. */ 2988 runp = head->last; 2989 do 2990 { 2991 assert (runp->grpid != 0); 2992 2993 here_groupidx = runp->fileinfo->scninfo[runp->grpid].outscnndx; 2994 if (here_groupidx != 0) 2995 break; 2996 } 2997 while ((runp = runp->next) != head->last); 2998 2999 if (here_groupidx == 0) 3000 { 3001 /* We need a new section group section. */ 3002 scn = elf_newscn (ld_state.outelf); 3003 xelf_getshdr (scn, shdr); 3004 if (shdr == NULL) 3005 error (EXIT_FAILURE, 0, 3006 gettext ("cannot create section for output file: %s"), 3007 elf_errmsg (-1)); 3008 3009 here_group = (struct scngroup *) xmalloc (sizeof (*here_group)); 3010 here_group->outscnidx = here_groupidx = elf_ndxscn (scn); 3011 here_group->nscns = 0; 3012 here_group->member = NULL; 3013 here_group->next = ld_state.groups; 3014 /* Pick a name for the section. To keep it meaningful 3015 we use a name used in the input files. If the 3016 section group in the output file should contain 3017 section which were in section groups of different 3018 names in the input files this is the users 3019 problem. */ 3020 here_group->nameent 3021 = ebl_strtabadd (ld_state.shstrtab, 3022 elf_strptr (runp->fileinfo->elf, 3023 runp->fileinfo->shstrndx, 3024 SCNINFO_SHDR (runp->shdr).sh_name), 3025 0); 3026 /* Signature symbol. */ 3027 here_group->symbol 3028 = runp->fileinfo->scninfo[runp->grpid].symbols; 3029 3030 ld_state.groups = here_group; 3031 } 3032 else 3033 { 3034 /* Search for the group with this index. */ 3035 here_group = ld_state.groups; 3036 while (here_group->outscnidx != here_groupidx) 3037 here_group = here_group->next; 3038 } 3039 3040 /* Add the new output section. */ 3041 newp = (struct member *) alloca (sizeof (*newp)); 3042 newp->scn = head; 3043#ifndef NDT_NEEDED 3044 newp->next = NULL; 3045#endif 3046 CSNGL_LIST_ADD_REAR (here_group->member, newp); 3047 ++here_group->nscns; 3048 3049 /* Store the section group index in all input files. */ 3050 runp = head->last; 3051 do 3052 { 3053 assert (runp->grpid != 0); 3054 3055 if (runp->fileinfo->scninfo[runp->grpid].outscnndx == 0) 3056 runp->fileinfo->scninfo[runp->grpid].outscnndx = here_groupidx; 3057 else 3058 assert (runp->fileinfo->scninfo[runp->grpid].outscnndx 3059 == here_groupidx); 3060 } 3061 while ((runp = runp->next) != head->last); 3062 } 3063 3064 /* We'll use this section so get it's name in the section header 3065 string table. */ 3066 if (head->kind == scn_normal) 3067 head->nameent = ebl_strtabadd (ld_state.shstrtab, head->name, 0); 3068 3069 /* Create a new section in the output file and add all data 3070 from all the sections we read. */ 3071 scn = elf_newscn (ld_state.outelf); 3072 head->scnidx = elf_ndxscn (scn); 3073 xelf_getshdr (scn, shdr); 3074 if (shdr == NULL) 3075 error (EXIT_FAILURE, 0, 3076 gettext ("cannot create section for output file: %s"), 3077 elf_errmsg (-1)); 3078 3079 assert (head->type != SHT_NULL); 3080 assert (head->type != SHT_SYMTAB); 3081 assert (head->type != SHT_DYNSYM || head->kind != scn_normal); 3082 assert (head->type != SHT_STRTAB || head->kind != scn_normal); 3083 assert (head->type != SHT_GROUP); 3084 shdr->sh_type = head->type; 3085 shdr->sh_flags = head->flags; 3086 shdr->sh_addralign = head->align; 3087 shdr->sh_entsize = head->entsize; 3088 assert (shdr->sh_entsize != 0 || (shdr->sh_flags & SHF_MERGE) == 0); 3089 (void) xelf_update_shdr (scn, shdr); 3090 3091 /* We have to know the section index of the dynamic symbol table 3092 right away. */ 3093 if (head->kind == scn_dot_dynsym) 3094 ld_state.dynsymscnidx = elf_ndxscn (scn); 3095 } 3096 3097 /* Actually create the section group sections. */ 3098 groups = ld_state.groups; 3099 while (groups != NULL) 3100 { 3101 Elf_Scn *scn; 3102 Elf_Data *data; 3103 Elf32_Word *grpdata; 3104 struct member *runp; 3105 3106 scn = elf_getscn (ld_state.outelf, groups->outscnidx); 3107 assert (scn != NULL); 3108 3109 data = elf_newdata (scn); 3110 if (data == NULL) 3111 error (EXIT_FAILURE, 0, 3112 gettext ("cannot create section for output file: %s"), 3113 elf_errmsg (-1)); 3114 3115 data->d_size = (groups->nscns + 1) * sizeof (Elf32_Word); 3116 data->d_buf = grpdata = (Elf32_Word *) xmalloc (data->d_size); 3117 data->d_type = ELF_T_WORD; 3118 data->d_version = EV_CURRENT; 3119 data->d_off = 0; 3120 /* XXX What better to use? */ 3121 data->d_align = sizeof (Elf32_Word); 3122 3123 /* The first word in the section is the flag word. */ 3124 /* XXX Set COMDATA flag is necessary. */ 3125 grpdata[0] = 0; 3126 3127 runp = groups->member->next; 3128 cnt = 1; 3129 do 3130 /* Fill in the index of the section. */ 3131 grpdata[cnt++] = runp->scn->scnidx; 3132 while ((runp = runp->next) != groups->member->next); 3133 3134 groups = groups->next; 3135 } 3136} 3137 3138 3139static bool 3140reduce_symbol_p (XElf_Sym *sym, struct Ebl_Strent *strent) 3141{ 3142 const char *str; 3143 const char *version; 3144 struct id_list search; 3145 struct id_list *verp; 3146 bool result = ld_state.default_bind_local; 3147 3148 if (XELF_ST_BIND (sym->st_info) == STB_LOCAL || sym->st_shndx == SHN_UNDEF) 3149 /* We don't have to do anything to local symbols here. */ 3150 /* XXX Any section value in [SHN_LORESERVER,SHN_XINDEX) need 3151 special treatment? */ 3152 return false; 3153 3154 /* XXX Handle other symbol bindings. */ 3155 assert (XELF_ST_BIND (sym->st_info) == STB_GLOBAL 3156 || XELF_ST_BIND (sym->st_info) == STB_WEAK); 3157 3158 str = ebl_string (strent); 3159 version = strchr (str, VER_CHR); 3160 if (version != NULL) 3161 { 3162 search.id = strndupa (str, version - str); 3163 if (*++version == VER_CHR) 3164 /* Skip the second '@' signalling a default definition. */ 3165 ++version; 3166 } 3167 else 3168 { 3169 search.id = str; 3170 version = ""; 3171 } 3172 3173 verp = ld_version_str_tab_find (&ld_state.version_str_tab, 3174 elf_hash (search.id), &search); 3175 while (verp != NULL) 3176 { 3177 /* We have this symbol in the version hash table. Now match the 3178 version name. */ 3179 if (strcmp (verp->u.s.versionname, version) == 0) 3180 /* Match! */ 3181 return verp->u.s.local; 3182 3183 verp = verp->next; 3184 } 3185 3186 /* XXX Add test for wildcard version symbols. */ 3187 3188 return result; 3189} 3190 3191 3192static XElf_Addr 3193eval_expression (struct expression *expr, XElf_Addr addr) 3194{ 3195 XElf_Addr val = ~((XElf_Addr) 0); 3196 3197 switch (expr->tag) 3198 { 3199 case exp_num: 3200 val = expr->val.num; 3201 break; 3202 3203 case exp_sizeof_headers: 3204 { 3205 /* The 'elf_update' call determine the offset of the first 3206 section. The the size of the header. */ 3207 XElf_Shdr_vardef (shdr); 3208 3209 xelf_getshdr (elf_getscn (ld_state.outelf, 1), shdr); 3210 assert (shdr != NULL); 3211 3212 val = shdr->sh_offset; 3213 } 3214 break; 3215 3216 case exp_pagesize: 3217 val = ld_state.pagesize; 3218 break; 3219 3220 case exp_id: 3221 /* We are here computing only address expressions. It seems not 3222 to be necessary to handle any variable but ".". Let's avoid 3223 the complication. If it turns up to be needed we can add 3224 it. */ 3225 if (strcmp (expr->val.str, ".") != 0) 3226 error (EXIT_FAILURE, 0, gettext ("\ 3227address computation expression contains variable '%s'"), 3228 expr->val.str); 3229 3230 val = addr; 3231 break; 3232 3233 case exp_mult: 3234 val = (eval_expression (expr->val.binary.left, addr) 3235 * eval_expression (expr->val.binary.right, addr)); 3236 break; 3237 3238 case exp_div: 3239 val = (eval_expression (expr->val.binary.left, addr) 3240 / eval_expression (expr->val.binary.right, addr)); 3241 break; 3242 3243 case exp_mod: 3244 val = (eval_expression (expr->val.binary.left, addr) 3245 % eval_expression (expr->val.binary.right, addr)); 3246 break; 3247 3248 case exp_plus: 3249 val = (eval_expression (expr->val.binary.left, addr) 3250 + eval_expression (expr->val.binary.right, addr)); 3251 break; 3252 3253 case exp_minus: 3254 val = (eval_expression (expr->val.binary.left, addr) 3255 - eval_expression (expr->val.binary.right, addr)); 3256 break; 3257 3258 case exp_and: 3259 val = (eval_expression (expr->val.binary.left, addr) 3260 & eval_expression (expr->val.binary.right, addr)); 3261 break; 3262 3263 case exp_or: 3264 val = (eval_expression (expr->val.binary.left, addr) 3265 | eval_expression (expr->val.binary.right, addr)); 3266 break; 3267 3268 case exp_align: 3269 val = eval_expression (expr->val.child, addr); 3270 if ((val & (val - 1)) != 0) 3271 error (EXIT_FAILURE, 0, gettext ("argument '%" PRIuMAX "' of ALIGN in address computation expression is no power of two"), 3272 (uintmax_t) val); 3273 val = (addr + val - 1) & ~(val - 1); 3274 break; 3275 } 3276 3277 return val; 3278} 3279 3280 3281/* Find a good as possible size for the hash table so that all the 3282 non-zero entries in HASHCODES don't collide too much and the table 3283 isn't too large. There is no exact formular for this so we use a 3284 heuristic. Depending on the optimization level the search is 3285 longer or shorter. */ 3286static size_t 3287optimal_bucket_size (Elf32_Word *hashcodes, size_t maxcnt, int optlevel) 3288{ 3289 size_t minsize; 3290 size_t maxsize; 3291 size_t bestsize; 3292 uint64_t bestcost; 3293 size_t size; 3294 uint32_t *counts; 3295 uint32_t *lengths; 3296 3297 if (maxcnt == 0) 3298 return 0; 3299 3300 /* When we are not optimizing we run only very few tests. */ 3301 if (optlevel <= 0) 3302 { 3303 minsize = maxcnt; 3304 maxsize = maxcnt + 10000 / maxcnt; 3305 } 3306 else 3307 { 3308 /* Does not make much sense to start with a smaller table than 3309 one which has at least four collisions. */ 3310 minsize = MAX (1, maxcnt / 4); 3311 /* We look for a best fit in the range of up to eigth times the 3312 number of elements. */ 3313 maxsize = 2 * maxcnt + (6 * MIN (optlevel, 100) * maxcnt) / 100; 3314 } 3315 bestsize = maxcnt; 3316 bestcost = UINT_MAX; 3317 3318 /* Array for counting the collisions and chain lengths. */ 3319 counts = (uint32_t *) xmalloc ((maxcnt + 1 + maxsize) * sizeof (uint32_t)); 3320 lengths = &counts[maxcnt + 1]; 3321 3322 for (size = minsize; size <= maxsize; ++size) 3323 { 3324 size_t inner; 3325 uint64_t cost; 3326 uint32_t maxlength; 3327 uint64_t success; 3328 uint32_t acc; 3329 double factor; 3330 3331 memset (lengths, '\0', size * sizeof (uint32_t)); 3332 memset (counts, '\0', (maxcnt + 1) * sizeof (uint32_t)); 3333 3334 /* Determine how often each hash bucket is used. */ 3335 for (inner = 0; inner < maxcnt; ++inner) 3336 ++lengths[hashcodes[inner] % size]; 3337 3338 /* Determine the lengths. */ 3339 maxlength = 0; 3340 for (inner = 0; inner < size; ++inner) 3341 { 3342 ++counts[lengths[inner]]; 3343 3344 if (lengths[inner] > maxlength) 3345 maxlength = lengths[inner]; 3346 } 3347 3348 /* Determine successful lookup length. */ 3349 acc = 0; 3350 success = 0; 3351 for (inner = 0; inner <= maxlength; ++inner) 3352 { 3353 acc += inner; 3354 success += counts[inner] * acc; 3355 } 3356 3357 /* We can compute two factors now: the average length of a 3358 positive search and the average length of a negative search. 3359 We count the number of comparisons which have to look at the 3360 names themselves. Recognizing that the chain ended is not 3361 accounted for since it's almost for free. 3362 3363 Which lookup is more important depends on the kind of DSO. 3364 If it is a system DSO like libc it is expected that most 3365 lookups succeed. Otherwise most lookups fail. */ 3366 if (ld_state.is_system_library) 3367 factor = (1.0 * (double) success / (double) maxcnt 3368 + 0.3 * (double) maxcnt / (double) size); 3369 else 3370 factor = (0.3 * (double) success / (double) maxcnt 3371 + 1.0 * (double) maxcnt / (double) size); 3372 3373 /* Combine the lookup cost factor. The 1/16th addend adds 3374 penalties for too large table sizes. */ 3375 cost = (2 + maxcnt + size) * (factor + 1.0 / 16.0); 3376 3377#if 0 3378 printf ("maxcnt = %d, size = %d, cost = %Ld, success = %g, fail = %g, factor = %g\n", 3379 maxcnt, size, cost, (double) success / (double) maxcnt, (double) maxcnt / (double) size, factor); 3380#endif 3381 3382 /* Compare with current best results. */ 3383 if (cost < bestcost) 3384 { 3385 bestcost = cost; 3386 bestsize = size; 3387 } 3388 } 3389 3390 free (counts); 3391 3392 return bestsize; 3393} 3394 3395 3396static XElf_Addr 3397find_entry_point (void) 3398{ 3399 XElf_Addr result; 3400 3401 if (ld_state.entry != NULL) 3402 { 3403 struct symbol search = { .name = ld_state.entry }; 3404 struct symbol *syment; 3405 3406 syment = ld_symbol_tab_find (&ld_state.symbol_tab, 3407 elf_hash (ld_state.entry), &search); 3408 if (syment != NULL && syment->defined) 3409 { 3410 /* We found the symbol. */ 3411 Elf_Data *data = elf_getdata (elf_getscn (ld_state.outelf, 3412 ld_state.symscnidx), NULL); 3413 3414 XElf_Sym_vardef (sym); 3415 3416 sym = NULL; 3417 if (data != NULL) 3418 xelf_getsym (data, ld_state.dblindirect[syment->outsymidx], sym); 3419 3420 if (sym == NULL && ld_state.need_dynsym && syment->outdynsymidx != 0) 3421 { 3422 /* Use the dynamic symbol table if available. */ 3423 data = elf_getdata (elf_getscn (ld_state.outelf, 3424 ld_state.dynsymscnidx), NULL); 3425 3426 sym = NULL; 3427 if (data != NULL) 3428 xelf_getsym (data, syment->outdynsymidx, sym); 3429 } 3430 3431 if (sym != NULL) 3432 return sym->st_value; 3433 3434 /* XXX What to do if the output has no non-dynamic symbol 3435 table and the dynamic symbol table does not contain the 3436 symbol? */ 3437 assert (ld_state.need_symtab); 3438 assert (ld_state.symscnidx != 0); 3439 } 3440 } 3441 3442 /* We couldn't find the symbol or none was given. Use the first 3443 address of the ".text" section then. */ 3444 3445 3446 result = 0; 3447 3448 /* In DSOs this is no fatal error. They usually have no entry 3449 points. In this case we set the entry point to zero, which makes 3450 sure it will always fail. */ 3451 if (ld_state.file_type == executable_file_type) 3452 { 3453 if (ld_state.entry != NULL) 3454 error (0, 0, gettext ("\ 3455cannot find entry symbol \"%s\": defaulting to %#0*" PRIx64), 3456 ld_state.entry, 3457 xelf_getclass (ld_state.outelf) == ELFCLASS32 ? 10 : 18, 3458 (uint64_t) result); 3459 else 3460 error (0, 0, gettext ("\ 3461no entry symbol specified: defaulting to %#0*" PRIx64), 3462 xelf_getclass (ld_state.outelf) == ELFCLASS32 ? 10 : 18, 3463 (uint64_t) result); 3464 } 3465 3466 return result; 3467} 3468 3469 3470static void 3471fillin_special_symbol (struct symbol *symst, size_t scnidx, size_t nsym, 3472 Elf_Data *symdata, struct Ebl_Strtab *strtab) 3473{ 3474 assert (ld_state.file_type != relocatable_file_type); 3475 3476 XElf_Sym_vardef (sym); 3477 xelf_getsym_ptr (symdata, nsym, sym); 3478 3479 /* The name offset will be filled in later. */ 3480 sym->st_name = 0; 3481 /* Traditionally: globally visible. */ 3482 sym->st_info = XELF_ST_INFO (STB_GLOBAL, symst->type); 3483 /* No special visibility or so. */ 3484 sym->st_other = 0; 3485 /* Reference to the GOT or dynamic section. Since the GOT and 3486 dynamic section are only created for executables and DSOs it 3487 cannot be that the section index is too large. */ 3488 assert (scnidx != 0); 3489 assert (scnidx < SHN_LORESERVE || scnidx == SHN_ABS); 3490 sym->st_shndx = scnidx; 3491 /* We want the beginning of the section. */ 3492 sym->st_value = 0; 3493 3494 /* Determine the size of the section. */ 3495 if (scnidx != SHN_ABS) 3496 { 3497 Elf_Data *data = elf_getdata (elf_getscn (ld_state.outelf, scnidx), 3498 NULL); 3499 assert (data != NULL); 3500 sym->st_size = data->d_size; 3501 /* Make sure there is no second data block. */ 3502 assert (elf_getdata (elf_getscn (ld_state.outelf, scnidx), data) 3503 == NULL); 3504 } 3505 3506 /* Insert symbol into the symbol table. Note that we do not have to 3507 use xelf_update_symshdx. */ 3508 (void) xelf_update_sym (symdata, nsym, sym); 3509 3510 /* Cross-references. */ 3511 ndxtosym[nsym] = symst; 3512 symst->outsymidx = nsym; 3513 3514 /* Add the name to the string table. */ 3515 symstrent[nsym] = ebl_strtabadd (strtab, symst->name, 0); 3516} 3517 3518 3519static void 3520new_dynamic_entry (Elf_Data *data, int idx, XElf_Sxword tag, XElf_Addr val) 3521{ 3522 XElf_Dyn_vardef (dyn); 3523 xelf_getdyn_ptr (data, idx, dyn); 3524 dyn->d_tag = tag; 3525 dyn->d_un.d_ptr = val; 3526 (void) xelf_update_dyn (data, idx, dyn); 3527} 3528 3529 3530static void 3531allocate_version_names (struct usedfiles *runp, struct Ebl_Strtab *dynstrtab) 3532{ 3533 /* If this DSO has no versions skip it. */ 3534 if (runp->status != opened || runp->verdefdata == NULL) 3535 return; 3536 3537 /* Add the object name. */ 3538 int offset = 0; 3539 while (1) 3540 { 3541 XElf_Verdef_vardef (def); 3542 XElf_Verdaux_vardef (aux); 3543 3544 /* Get data at the next offset. */ 3545 xelf_getverdef (runp->verdefdata, offset, def); 3546 assert (def != NULL); 3547 xelf_getverdaux (runp->verdefdata, offset + def->vd_aux, aux); 3548 assert (aux != NULL); 3549 3550 assert (def->vd_ndx <= runp->nverdef); 3551 if (def->vd_ndx == 1 || runp->verdefused[def->vd_ndx] != 0) 3552 { 3553 runp->verdefent[def->vd_ndx] 3554 = ebl_strtabadd (dynstrtab, elf_strptr (runp->elf, 3555 runp->dynsymstridx, 3556 aux->vda_name), 0); 3557 3558 if (def->vd_ndx > 1) 3559 runp->verdefused[def->vd_ndx] = ld_state.nextveridx++; 3560 } 3561 3562 if (def->vd_next == 0) 3563 /* That were all versions. */ 3564 break; 3565 3566 offset += def->vd_next; 3567 } 3568} 3569 3570 3571XElf_Off 3572create_verneed_data (XElf_Off offset, Elf_Data *verneeddata, 3573 struct usedfiles *runp, int *ntotal) 3574{ 3575 size_t verneed_size = xelf_fsize (ld_state.outelf, ELF_T_VNEED, 1); 3576 size_t vernaux_size = xelf_fsize (ld_state.outelf, ELF_T_VNAUX, 1); 3577 int need_offset; 3578 bool filled = false; 3579 GElf_Verneed verneed; 3580 GElf_Vernaux vernaux; 3581 int ndef = 0; 3582size_t cnt; 3583 3584 /* If this DSO has no versions skip it. */ 3585 if (runp->nverdefused == 0) 3586 return offset; 3587 3588 /* We fill in the Verneed record last. Remember the 3589 offset. */ 3590 need_offset = offset; 3591 offset += verneed_size; 3592 3593 for (cnt = 2; cnt <= runp->nverdef; ++cnt) 3594 if (runp->verdefused[cnt] != 0) 3595 { 3596 assert (runp->verdefent[cnt] != NULL); 3597 3598 if (filled) 3599 { 3600 vernaux.vna_next = vernaux_size; 3601 (void) gelf_update_vernaux (verneeddata, offset, 3602 &vernaux); 3603 offset += vernaux_size; 3604 } 3605 3606 vernaux.vna_hash 3607 = elf_hash (ebl_string (runp->verdefent[cnt])); 3608 vernaux.vna_flags = 0; 3609 vernaux.vna_other = runp->verdefused[cnt]; 3610 vernaux.vna_name = ebl_strtaboffset (runp->verdefent[cnt]); 3611 filled = true; 3612 ++ndef; 3613 } 3614 3615 assert (filled); 3616 vernaux.vna_next = 0; 3617 (void) gelf_update_vernaux (verneeddata, offset, &vernaux); 3618 offset += vernaux_size; 3619 3620 verneed.vn_version = VER_NEED_CURRENT; 3621 verneed.vn_cnt = ndef; 3622 verneed.vn_file = ebl_strtaboffset (runp->verdefent[1]); 3623 /* The first auxiliary entry is always found directly 3624 after the verneed entry. */ 3625 verneed.vn_aux = verneed_size; 3626 verneed.vn_next = --*ntotal > 0 ? offset - need_offset : 0; 3627 (void) gelf_update_verneed (verneeddata, need_offset, 3628 &verneed); 3629 3630 return offset; 3631} 3632 3633 3634/* Create the output file. 3635 3636 For relocatable files what basically has to happen is that all 3637 sections from all input files are written into the output file. 3638 Sections with the same name are combined (offsets adjusted 3639 accordingly). The symbol tables are combined in one single table. 3640 When stripping certain symbol table entries are omitted. 3641 3642 For executables (shared or not) we have to create the program header, 3643 additional sections like the .interp, eventually (in addition) create 3644 a dynamic symbol table and a dynamic section. Also the relocations 3645have to be processed differently. */ 3646static int 3647ld_generic_create_outfile (struct ld_state *statep) 3648{ 3649 struct scnlist 3650 { 3651 size_t scnidx; 3652 struct scninfo *scninfo; 3653 struct scnlist *next; 3654 }; 3655 struct scnlist *rellist = NULL; 3656 size_t cnt; 3657 Elf_Scn *symscn = NULL; 3658 Elf_Scn *xndxscn = NULL; 3659 Elf_Scn *strscn = NULL; 3660 struct Ebl_Strtab *strtab = NULL; 3661 struct Ebl_Strtab *dynstrtab = NULL; 3662 XElf_Shdr_vardef (shdr); 3663 Elf_Data *data; 3664 Elf_Data *symdata = NULL; 3665 Elf_Data *xndxdata = NULL; 3666 struct usedfiles *file; 3667 size_t nsym; 3668 size_t nsym_local; 3669 size_t nsym_allocated; 3670 size_t nsym_dyn = 0; 3671 Elf32_Word *dblindirect = NULL; 3672#ifndef NDEBUG 3673 bool need_xndx; 3674#endif 3675 Elf_Scn *shstrtab_scn; 3676 size_t shstrtab_ndx; 3677 XElf_Ehdr_vardef (ehdr); 3678 struct Ebl_Strent *symtab_ent = NULL; 3679 struct Ebl_Strent *xndx_ent = NULL; 3680 struct Ebl_Strent *strtab_ent = NULL; 3681 struct Ebl_Strent *shstrtab_ent; 3682 struct scngroup *groups; 3683 Elf_Scn *dynsymscn = NULL; 3684 Elf_Data *dynsymdata = NULL; 3685 Elf_Data *dynstrdata = NULL; 3686 Elf32_Word *hashcodes = NULL; 3687 size_t nsym_dyn_allocated = 0; 3688 Elf_Scn *versymscn = NULL; 3689 Elf_Data *versymdata = NULL; 3690 3691 if (ld_state.need_symtab) 3692 { 3693 /* First create the symbol table. We need the symbol section itself 3694 and the string table for it. */ 3695 symscn = elf_newscn (ld_state.outelf); 3696 ld_state.symscnidx = elf_ndxscn (symscn); 3697 symdata = elf_newdata (symscn); 3698 if (symdata == NULL) 3699 error (EXIT_FAILURE, 0, 3700 gettext ("cannot create symbol table for output file: %s"), 3701 elf_errmsg (-1)); 3702 3703 symdata->d_type = ELF_T_SYM; 3704 /* This is an estimated size, but it will definitely cap the real value. 3705 We might have to adjust the number later. */ 3706 nsym_allocated = (1 + ld_state.nsymtab + ld_state.nplt + ld_state.ngot 3707 + ld_state.nusedsections + ld_state.nlscript_syms); 3708 symdata->d_size = xelf_fsize (ld_state.outelf, ELF_T_SYM, 3709 nsym_allocated); 3710 3711 /* Optionally the extended section table. */ 3712 /* XXX Is SHN_LORESERVE correct? Do we need some other sections? */ 3713 if (unlikely (ld_state.nusedsections >= SHN_LORESERVE)) 3714 { 3715 xndxscn = elf_newscn (ld_state.outelf); 3716 ld_state.xndxscnidx = elf_ndxscn (xndxscn); 3717 3718 xndxdata = elf_newdata (xndxscn); 3719 if (xndxdata == NULL) 3720 error (EXIT_FAILURE, 0, 3721 gettext ("cannot create symbol table for output file: %s"), 3722 elf_errmsg (-1)); 3723 3724 /* The following relies on the fact that Elf32_Word and Elf64_Word 3725 have the same size. */ 3726 xndxdata->d_type = ELF_T_WORD; 3727 /* This is an estimated size, but it will definitely cap the 3728 real value. we might have to adjust the number later. */ 3729 xndxdata->d_size = xelf_fsize (ld_state.outelf, ELF_T_WORD, 3730 nsym_allocated); 3731 /* The first entry is left empty, clear it here and now. */ 3732 xndxdata->d_buf = memset (xmalloc (xndxdata->d_size), '\0', 3733 xelf_fsize (ld_state.outelf, ELF_T_WORD, 3734 1)); 3735 xndxdata->d_off = 0; 3736 /* XXX Should use an ebl function. */ 3737 xndxdata->d_align = sizeof (Elf32_Word); 3738 } 3739 } 3740 else 3741 { 3742 assert (ld_state.need_dynsym); 3743 3744 /* First create the symbol table. We need the symbol section itself 3745 and the string table for it. */ 3746 symscn = elf_getscn (ld_state.outelf, ld_state.dynsymscnidx); 3747 symdata = elf_newdata (symscn); 3748 if (symdata == NULL) 3749 error (EXIT_FAILURE, 0, 3750 gettext ("cannot create symbol table for output file: %s"), 3751 elf_errmsg (-1)); 3752 3753 symdata->d_version = EV_CURRENT; 3754 symdata->d_type = ELF_T_SYM; 3755 /* This is an estimated size, but it will definitely cap the real value. 3756 We might have to adjust the number later. */ 3757 nsym_allocated = (1 + ld_state.nsymtab + ld_state.nplt + ld_state.ngot 3758 - ld_state.nlocalsymbols + ld_state.nlscript_syms); 3759 symdata->d_size = xelf_fsize (ld_state.outelf, ELF_T_SYM, 3760 nsym_allocated); 3761 } 3762 3763 /* The first entry is left empty, clear it here and now. */ 3764 symdata->d_buf = memset (xmalloc (symdata->d_size), '\0', 3765 xelf_fsize (ld_state.outelf, ELF_T_SYM, 1)); 3766 symdata->d_off = 0; 3767 /* XXX This is ugly but how else can it be done. */ 3768 symdata->d_align = xelf_fsize (ld_state.outelf, ELF_T_ADDR, 1); 3769 3770 /* Allocate another array to keep track of the handles for the symbol 3771 names. */ 3772 symstrent = (struct Ebl_Strent **) xcalloc (nsym_allocated, 3773 sizeof (struct Ebl_Strent *)); 3774 3775 /* By starting at 1 we effectively add a null entry. */ 3776 nsym = 1; 3777 3778 /* Iteration over all sections. */ 3779 for (cnt = 0; cnt < ld_state.nallsections; ++cnt) 3780 { 3781 struct scnhead *head = ld_state.allsections[cnt]; 3782 Elf_Scn *scn; 3783 struct scninfo *runp; 3784 XElf_Off offset; 3785 Elf32_Word xndx; 3786 3787 /* Don't handle unused sections at all. */ 3788 if (!head->used) 3789 continue; 3790 3791 /* Get the section handle. */ 3792 scn = elf_getscn (ld_state.outelf, head->scnidx); 3793 3794 if (unlikely (head->kind == scn_dot_interp)) 3795 { 3796 Elf_Data *outdata = elf_newdata (scn); 3797 if (outdata == NULL) 3798 error (EXIT_FAILURE, 0, 3799 gettext ("cannot create section for output file: %s"), 3800 elf_errmsg (-1)); 3801 3802 /* This is the string we'll put in the section. */ 3803 const char *interp = ld_state.interp ?: "/lib/ld.so.1"; 3804 3805 /* Create the section data. */ 3806 outdata->d_buf = (void *) interp; 3807 outdata->d_size = strlen (interp) + 1; 3808 outdata->d_type = ELF_T_BYTE; 3809 outdata->d_off = 0; 3810 outdata->d_align = 1; 3811 outdata->d_version = EV_CURRENT; 3812 3813 /* Remember the index of this section. */ 3814 ld_state.interpscnidx = head->scnidx; 3815 3816 continue; 3817 } 3818 3819 if (unlikely (head->kind == scn_dot_got)) 3820 { 3821 /* Remember the index of this section. */ 3822 ld_state.gotscnidx = elf_ndxscn (scn); 3823 3824 /* Give the backend the change to initialize the section. */ 3825 INITIALIZE_GOT (&ld_state, scn); 3826 3827 continue; 3828 } 3829 3830 if (unlikely (head->kind == scn_dot_dynrel)) 3831 { 3832 Elf_Data *outdata; 3833 3834 outdata = elf_newdata (scn); 3835 if (outdata == NULL) 3836 error (EXIT_FAILURE, 0, 3837 gettext ("cannot create section for output file: %s"), 3838 elf_errmsg (-1)); 3839 3840 outdata->d_size = ld_state.relsize_total; 3841 outdata->d_buf = xmalloc (outdata->d_size); 3842 outdata->d_type = (REL_TYPE (&ld_state) == DT_REL 3843 ? ELF_T_REL : ELF_T_RELA); 3844 outdata->d_off = 0; 3845 outdata->d_align = xelf_fsize (ld_state.outelf, ELF_T_ADDR, 1); 3846 3847 /* Remember the index of this section. */ 3848 ld_state.reldynscnidx = elf_ndxscn (scn); 3849 3850 continue; 3851 } 3852 3853 if (unlikely (head->kind == scn_dot_dynamic)) 3854 { 3855 /* Only create the data for now. */ 3856 Elf_Data *outdata; 3857 3858 /* Account for a few more entries we have to add. */ 3859 if (ld_state.dt_flags != 0) 3860 ++ld_state.ndynamic; 3861 if (ld_state.dt_flags_1 != 0) 3862 ++ld_state.ndynamic; 3863 if (ld_state.dt_feature_1 != 0) 3864 ++ld_state.ndynamic; 3865 3866 outdata = elf_newdata (scn); 3867 if (outdata == NULL) 3868 error (EXIT_FAILURE, 0, 3869 gettext ("cannot create section for output file: %s"), 3870 elf_errmsg (-1)); 3871 3872 /* Create the section data. */ 3873 outdata->d_size = xelf_fsize (ld_state.outelf, ELF_T_DYN, 3874 ld_state.ndynamic); 3875 outdata->d_buf = xcalloc (1, outdata->d_size); 3876 outdata->d_type = ELF_T_DYN; 3877 outdata->d_off = 0; 3878 outdata->d_align = xelf_fsize (ld_state.outelf, ELF_T_ADDR, 1); 3879 3880 /* Remember the index of this section. */ 3881 ld_state.dynamicscnidx = elf_ndxscn (scn); 3882 3883 continue; 3884 } 3885 3886 if (unlikely (head->kind == scn_dot_dynsym)) 3887 { 3888 /* We already know the section index. */ 3889 assert (ld_state.dynsymscnidx == elf_ndxscn (scn)); 3890 3891 continue; 3892 } 3893 3894 if (unlikely (head->kind == scn_dot_dynstr)) 3895 { 3896 /* Remember the index of this section. */ 3897 ld_state.dynstrscnidx = elf_ndxscn (scn); 3898 3899 /* Create the string table. */ 3900 dynstrtab = ebl_strtabinit (true); 3901 3902 /* XXX TBI 3903 We have to add all the strings which are needed in the 3904 dynamic section here. This means DT_FILTER, 3905 DT_AUXILIARY, ... entries. */ 3906 if (ld_state.ndsofiles > 0) 3907 { 3908 struct usedfiles *frunp = ld_state.dsofiles; 3909 3910 do 3911 if (! ld_state.ignore_unused_dsos || frunp->used) 3912 frunp->sonameent = ebl_strtabadd (dynstrtab, frunp->soname, 3913 0); 3914 while ((frunp = frunp->next) != ld_state.dsofiles); 3915 } 3916 3917 3918 /* Add the runtime path information. The strings are stored 3919 in the .dynstr section. If both rpath and runpath are defined 3920 the runpath information is used. */ 3921 if (ld_state.runpath != NULL || ld_state.rpath != NULL) 3922 { 3923 struct pathelement *startp; 3924 struct pathelement *prunp; 3925 int tag; 3926 size_t len; 3927 char *str; 3928 char *cp; 3929 3930 if (ld_state.runpath != NULL) 3931 { 3932 startp = ld_state.runpath; 3933 tag = DT_RUNPATH; 3934 } 3935 else 3936 { 3937 startp = ld_state.rpath; 3938 tag = DT_RPATH; 3939 } 3940 3941 /* Determine how long the string will be. */ 3942 for (len = 0, prunp = startp; prunp != NULL; prunp = prunp->next) 3943 len += strlen (prunp->pname) + 1; 3944 3945 cp = str = (char *) obstack_alloc (&ld_state.smem, len); 3946 /* Copy the string. */ 3947 for (prunp = startp; prunp != NULL; prunp = prunp->next) 3948 { 3949 cp = stpcpy (cp, prunp->pname); 3950 *cp++ = ':'; 3951 } 3952 /* Remove the last colon. */ 3953 cp[-1] = '\0'; 3954 3955 /* Remember the values until we can generate the dynamic 3956 section. */ 3957 ld_state.rxxpath_strent = ebl_strtabadd (dynstrtab, str, len); 3958 ld_state.rxxpath_tag = tag; 3959 } 3960 3961 continue; 3962 } 3963 3964 if (unlikely (head->kind == scn_dot_hash)) 3965 { 3966 /* Remember the index of this section. */ 3967 ld_state.hashscnidx = elf_ndxscn (scn); 3968 3969 continue; 3970 } 3971 3972 if (unlikely (head->kind == scn_dot_plt)) 3973 { 3974 /* Remember the index of this section. */ 3975 ld_state.pltscnidx = elf_ndxscn (scn); 3976 3977 /* Give the backend the change to initialize the section. */ 3978 INITIALIZE_PLT (&ld_state, scn); 3979 3980 continue; 3981 } 3982 3983 if (unlikely (head->kind == scn_dot_pltrel)) 3984 { 3985 /* Remember the index of this section. */ 3986 ld_state.pltrelscnidx = elf_ndxscn (scn); 3987 3988 /* Give the backend the change to initialize the section. */ 3989 INITIALIZE_PLTREL (&ld_state, scn); 3990 3991 continue; 3992 } 3993 3994 if (unlikely (head->kind == scn_dot_version)) 3995 { 3996 /* Remember the index of this section. */ 3997 ld_state.versymscnidx = elf_ndxscn (scn); 3998 3999 continue; 4000 } 4001 4002 if (unlikely (head->kind == scn_dot_version_r)) 4003 { 4004 /* Remember the index of this section. */ 4005 ld_state.verneedscnidx = elf_ndxscn (scn); 4006 4007 continue; 4008 } 4009 4010 /* If we come here we must be handling a normal section. */ 4011 assert (head->kind == scn_normal); 4012 4013 /* Create an STT_SECTION entry in the symbol table. But not for 4014 the symbolic symbol table. */ 4015 if (ld_state.need_symtab) 4016 { 4017 /* XXX Can we be cleverer and do this only if needed? */ 4018 XElf_Sym_vardef (sym); 4019 4020 /* Optimization ahead: in the native linker we get a pointer 4021 to the final location so that the following code writes 4022 directly in the correct place. Otherwise we write into 4023 the local variable first. */ 4024 xelf_getsym_ptr (symdata, nsym, sym); 4025 4026 /* Usual section symbol: local, no specific information, 4027 except the section index. The offset here is zero, the 4028 start address will later be added. */ 4029 sym->st_name = 0; 4030 sym->st_info = XELF_ST_INFO (STB_LOCAL, STT_SECTION); 4031 sym->st_other = 0; 4032 sym->st_value = 0; 4033 sym->st_size = 0; 4034 /* In relocatable files the section index can be too big for 4035 the ElfXX_Sym struct. we have to deal with the extended 4036 symbol table. */ 4037 if (likely (head->scnidx < SHN_LORESERVE)) 4038 { 4039 sym->st_shndx = head->scnidx; 4040 xndx = 0; 4041 } 4042 else 4043 { 4044 sym->st_shndx = SHN_XINDEX; 4045 xndx = head->scnidx; 4046 } 4047 /* Commit the change. See the optimization above, this does 4048 not change the symbol table entry. But the extended 4049 section index table entry is always written, if there is 4050 such a table. */ 4051 assert (nsym < nsym_allocated); 4052 xelf_update_symshndx (symdata, xndxdata, nsym, sym, xndx, 0); 4053 4054 /* Remember the symbol's index in the symbol table. */ 4055 head->scnsymidx = nsym++; 4056 } 4057 4058 if (head->type == SHT_REL || head->type == SHT_RELA) 4059 { 4060 /* Remember that we have to fill in the symbol table section 4061 index. */ 4062 if (ld_state.file_type == relocatable_file_type) 4063 { 4064 struct scnlist *newp; 4065 4066 newp = (struct scnlist *) alloca (sizeof (*newp)); 4067 newp->scnidx = head->scnidx; 4068 newp->scninfo = head->last->next; 4069#ifndef NDEBUG 4070 newp->next = NULL; 4071#endif 4072 SNGL_LIST_PUSH (rellist, newp); 4073 } 4074 else 4075 { 4076 /* When we create an executable or a DSO we don't simply 4077 copy the existing relocations. Instead many will be 4078 resolved, others will be converted. Create a data buffer 4079 large enough to contain the contents which we will fill 4080 in later. */ 4081 int type = head->type == SHT_REL ? ELF_T_REL : ELF_T_RELA; 4082 4083 data = elf_newdata (scn); 4084 if (data == NULL) 4085 error (EXIT_FAILURE, 0, 4086 gettext ("cannot create section for output file: %s"), 4087 elf_errmsg (-1)); 4088 4089 data->d_size = xelf_fsize (ld_state.outelf, type, head->relsize); 4090 data->d_buf = xcalloc (data->d_size, 1); 4091 data->d_type = type; 4092 data->d_align = xelf_fsize (ld_state.outelf, ELF_T_ADDR, 1); 4093 data->d_off = 0; 4094 4095 continue; 4096 } 4097 } 4098 4099 /* Recognize string and merge flag and handle them. */ 4100 if (head->flags & SHF_MERGE) 4101 { 4102 /* We merge the contents of the sections. For this we do 4103 not look at the contents of section directly. Instead we 4104 look at the symbols of the section. */ 4105 Elf_Data *outdata; 4106 4107 /* Concatenate the lists of symbols for all sections. 4108 4109 XXX In case any input section has no symbols associated 4110 (this happens for debug sections) we cannot use this 4111 method. Implement parsing the other debug sections and 4112 find the string pointers. For now we don't merge. */ 4113 runp = head->last->next; 4114 if (runp->symbols == NULL) 4115 { 4116 head->flags &= ~SHF_MERGE; 4117 goto no_merge; 4118 } 4119 head->symbols = runp->symbols; 4120 4121 while ((runp = runp->next) != head->last->next) 4122 { 4123 if (runp->symbols == NULL) 4124 { 4125 head->flags &= ~SHF_MERGE; 4126 head->symbols = NULL; 4127 goto no_merge; 4128 } 4129 4130 struct symbol *oldhead = head->symbols->next_in_scn; 4131 4132 head->symbols->next_in_scn = runp->symbols->next_in_scn; 4133 runp->symbols->next_in_scn = oldhead; 4134 head->symbols = runp->symbols; 4135 } 4136 4137 /* Create the output section. */ 4138 outdata = elf_newdata (scn); 4139 if (outdata == NULL) 4140 error (EXIT_FAILURE, 0, 4141 gettext ("cannot create section for output file: %s"), 4142 elf_errmsg (-1)); 4143 4144 /* We use different merging algorithms for performance 4145 reasons. We can easily handle single-byte and 4146 wchar_t-wide character strings. All other cases (which 4147 really should happen in real life) are handled by the 4148 generic code. */ 4149 if (SCNINFO_SHDR (head->last->shdr).sh_entsize == 1 4150 && (head->flags & SHF_STRINGS)) 4151 { 4152 /* Simple, single-byte string matching. */ 4153 struct Ebl_Strtab *mergestrtab; 4154 struct symbol *symrunp; 4155 Elf_Data *locsymdata = NULL; 4156 Elf_Data *locdata = NULL; 4157 4158 mergestrtab = ebl_strtabinit (false); 4159 4160 symrunp = head->symbols->next_in_scn; 4161 file = NULL; 4162 do 4163 { 4164 /* Accelarate the loop. We cache the file 4165 information since it might very well be the case 4166 that the previous entry was from the same 4167 file. */ 4168 if (symrunp->file != file) 4169 { 4170 /* Remember the file. */ 4171 file = symrunp->file; 4172 /* Symbol table data from that file. */ 4173 locsymdata = file->symtabdata; 4174 /* String section data. */ 4175 locdata = elf_rawdata (file->scninfo[symrunp->scndx].scn, 4176 NULL); 4177 assert (locdata != NULL); 4178 /* While we are at it, remember the output 4179 section. If we don't access the string data 4180 section the section won't be in the output 4181 file. So it is sufficient to do the work 4182 here. */ 4183 file->scninfo[symrunp->scndx].outscnndx = head->scnidx; 4184 } 4185 4186 /* Get the symbol information. This provides us the 4187 offset into the string data section. */ 4188 XElf_Sym_vardef (sym); 4189 xelf_getsym (locsymdata, symrunp->symidx, sym); 4190 assert (sym != NULL); 4191 4192 /* Get the data from the file. Note that we access 4193 the raw section data; no endian-ness issues with 4194 single-byte strings. */ 4195 symrunp->merge.handle 4196 = ebl_strtabadd (mergestrtab, 4197 (char *) locdata->d_buf + sym->st_value, 4198 0); 4199 } 4200 while ((symrunp = symrunp->next_in_scn) 4201 != head->symbols->next_in_scn); 4202 4203 /* All strings have been added. Create the final table. */ 4204 ebl_strtabfinalize (mergestrtab, outdata); 4205 4206 /* Compute the final offsets in the section. */ 4207 symrunp = runp->symbols; 4208 do 4209 { 4210 symrunp->merge.value 4211 = ebl_strtaboffset (symrunp->merge.handle); 4212 symrunp->merged = 1; 4213 } 4214 while ((symrunp = symrunp->next_in_scn) != runp->symbols); 4215 4216 /* We don't need the string table anymore. */ 4217 ebl_strtabfree (mergestrtab); 4218 } 4219 else if (likely (SCNINFO_SHDR (head->last->shdr).sh_entsize 4220 == sizeof (wchar_t)) 4221 && likely (head->flags & SHF_STRINGS)) 4222 { 4223 /* Simple, wchar_t string merging. */ 4224 struct Ebl_WStrtab *mergestrtab; 4225 struct symbol *symrunp; 4226 Elf_Data *locsymdata = NULL; 4227 Elf_Data *locdata = NULL; 4228 4229 mergestrtab = ebl_wstrtabinit (false); 4230 4231 symrunp = runp->symbols; 4232 file = NULL; 4233 do 4234 { 4235 /* Accelarate the loop. We cache the file 4236 information since it might very well be the case 4237 that the previous entry was from the same 4238 file. */ 4239 if (symrunp->file != file) 4240 { 4241 /* Remember the file. */ 4242 file = symrunp->file; 4243 /* Symbol table data from that file. */ 4244 locsymdata = file->symtabdata; 4245 /* String section data. */ 4246 locdata = elf_rawdata (file->scninfo[symrunp->scndx].scn, 4247 NULL); 4248 assert (locdata != NULL); 4249 4250 /* While we are at it, remember the output 4251 section. If we don't access the string data 4252 section the section won't be in the output 4253 file. So it is sufficient to do the work 4254 here. */ 4255 file->scninfo[symrunp->scndx].outscnndx = head->scnidx; 4256 } 4257 4258 /* Get the symbol information. This provides us the 4259 offset into the string data section. */ 4260 XElf_Sym_vardef (sym); 4261 xelf_getsym (locsymdata, symrunp->symidx, sym); 4262 assert (sym != NULL); 4263 4264 /* Get the data from the file. Using the raw 4265 section data here is possible since we don't 4266 interpret the string themselves except for 4267 looking for the wide NUL character. The NUL 4268 character has fortunately the same representation 4269 regardless of the byte order. */ 4270 symrunp->merge.handle 4271 = ebl_wstrtabadd (mergestrtab, 4272 (wchar_t *) ((char *) locdata->d_buf 4273 + sym->st_value), 0); 4274 } 4275 while ((symrunp = symrunp->next_in_scn) != runp->symbols); 4276 4277 /* All strings have been added. Create the final table. */ 4278 ebl_wstrtabfinalize (mergestrtab, outdata); 4279 4280 /* Compute the final offsets in the section. */ 4281 symrunp = runp->symbols; 4282 do 4283 { 4284 symrunp->merge.value 4285 = ebl_wstrtaboffset (symrunp->merge.handle); 4286 symrunp->merged = 1; 4287 } 4288 while ((symrunp = symrunp->next_in_scn) != runp->symbols); 4289 4290 /* We don't need the string table anymore. */ 4291 ebl_wstrtabfree (mergestrtab); 4292 } 4293 else 4294 { 4295 /* Non-standard merging. */ 4296 struct Ebl_GStrtab *mergestrtab; 4297 struct symbol *symrunp; 4298 Elf_Data *locsymdata = NULL; 4299 Elf_Data *locdata = NULL; 4300 /* If this is no string section the length of each "string" 4301 is always one. */ 4302 unsigned int len = (head->flags & SHF_STRINGS) ? 0 : 1; 4303 4304 /* This is the generic string table functionality. Much 4305 slower than the specialized code. */ 4306 mergestrtab 4307 = ebl_gstrtabinit (SCNINFO_SHDR (head->last->shdr).sh_entsize, 4308 false); 4309 4310 symrunp = runp->symbols; 4311 file = NULL; 4312 do 4313 { 4314 /* Accelarate the loop. We cache the file 4315 information since it might very well be the case 4316 that the previous entry was from the same 4317 file. */ 4318 if (symrunp->file != file) 4319 { 4320 /* Remember the file. */ 4321 file = symrunp->file; 4322 /* Symbol table data from that file. */ 4323 locsymdata = file->symtabdata; 4324 /* String section data. */ 4325 locdata = elf_rawdata (file->scninfo[symrunp->scndx].scn, 4326 NULL); 4327 assert (locdata != NULL); 4328 4329 /* While we are at it, remember the output 4330 section. If we don't access the string data 4331 section the section won't be in the output 4332 file. So it is sufficient to do the work 4333 here. */ 4334 file->scninfo[symrunp->scndx].outscnndx = head->scnidx; 4335 } 4336 4337 /* Get the symbol information. This provides us the 4338 offset into the string data section. */ 4339 XElf_Sym_vardef (sym); 4340 xelf_getsym (locsymdata, symrunp->symidx, sym); 4341 assert (sym != NULL); 4342 4343 /* Get the data from the file. Using the raw 4344 section data here is possible since we don't 4345 interpret the string themselves except for 4346 looking for the wide NUL character. The NUL 4347 character has fortunately the same representation 4348 regardless of the byte order. */ 4349 symrunp->merge.handle 4350 = ebl_gstrtabadd (mergestrtab, 4351 (char *) locdata->d_buf + sym->st_value, 4352 len); 4353 } 4354 while ((symrunp = symrunp->next_in_scn) != runp->symbols); 4355 4356 /* Create the final table. */ 4357 ebl_gstrtabfinalize (mergestrtab, outdata); 4358 4359 /* Compute the final offsets in the section. */ 4360 symrunp = runp->symbols; 4361 do 4362 { 4363 symrunp->merge.value 4364 = ebl_gstrtaboffset (symrunp->merge.handle); 4365 symrunp->merged = 1; 4366 } 4367 while ((symrunp = symrunp->next_in_scn) != runp->symbols); 4368 4369 /* We don't need the string table anymore. */ 4370 ebl_gstrtabfree (mergestrtab); 4371 } 4372 } 4373 else 4374 { 4375 no_merge: 4376 assert (head->scnidx == elf_ndxscn (scn)); 4377 4378 /* It is important to start with the first list entry (and 4379 not just any one) to add the sections in the correct 4380 order. */ 4381 runp = head->last->next; 4382 offset = 0; 4383 do 4384 { 4385 Elf_Data *outdata = elf_newdata (scn); 4386 if (outdata == NULL) 4387 error (EXIT_FAILURE, 0, 4388 gettext ("cannot create section for output file: %s"), 4389 elf_errmsg (-1)); 4390 4391 /* Exceptional case: if we synthesize a data block SCN 4392 is NULL and the sectio header info must be for a 4393 SHT_NOBITS block and the size and alignment are 4394 filled in. */ 4395 if (likely (runp->scn != NULL)) 4396 { 4397 data = elf_getdata (runp->scn, NULL); 4398 assert (data != NULL); 4399 4400 /* We reuse the data buffer in the input file. */ 4401 *outdata = *data; 4402 4403 /* Given that we read the input file from disk we know there 4404 cannot be another data part. */ 4405 assert (elf_getdata (runp->scn, data) == NULL); 4406 } 4407 else 4408 { 4409 /* Must be a NOBITS section. */ 4410 assert (SCNINFO_SHDR (runp->shdr).sh_type == SHT_NOBITS); 4411 4412 outdata->d_buf = NULL; /* Not needed. */ 4413 outdata->d_type = ELF_T_BYTE; 4414 outdata->d_version = EV_CURRENT; 4415 outdata->d_size = SCNINFO_SHDR (runp->shdr).sh_size; 4416 outdata->d_align = SCNINFO_SHDR (runp->shdr).sh_addralign; 4417 } 4418 4419 XElf_Off align = MAX (1, outdata->d_align); 4420 assert (powerof2 (align)); 4421 offset = ((offset + align - 1) & ~(align - 1)); 4422 4423 runp->offset = offset; 4424 runp->outscnndx = head->scnidx; 4425 runp->allsectionsidx = cnt; 4426 4427 outdata->d_off = offset; 4428 4429 offset += outdata->d_size; 4430 } 4431 while ((runp = runp->next) != head->last->next); 4432 4433 /* If necessary add the additional line to the .comment section. */ 4434 if (ld_state.add_ld_comment 4435 && head->flags == 0 4436 && head->type == SHT_PROGBITS 4437 && strcmp (head->name, ".comment") == 0 4438 && head->entsize == 0) 4439 { 4440 Elf_Data *outdata = elf_newdata (scn); 4441 4442 if (outdata == NULL) 4443 error (EXIT_FAILURE, 0, 4444 gettext ("cannot create section for output file: %s"), 4445 elf_errmsg (-1)); 4446 4447 outdata->d_buf = (void *) "\0ld (Red Hat " PACKAGE ") " VERSION; 4448 outdata->d_size = strlen ((char *) outdata->d_buf + 1) + 2; 4449 outdata->d_off = offset; 4450 outdata->d_type = ELF_T_BYTE; 4451 outdata->d_align = 1; 4452 } 4453 /* XXX We should create a .comment section if none exists. 4454 This requires that we early on detect that no such 4455 section exists. This should probably be implemented 4456 together with some merging of the section contents. 4457 Currently identical entries are not merged. */ 4458 } 4459 } 4460 4461 /* The table we collect the strings in. */ 4462 strtab = ebl_strtabinit (true); 4463 if (strtab == NULL) 4464 error (EXIT_FAILURE, errno, gettext ("cannot create string table")); 4465 4466 4467#ifndef NDEBUG 4468 /* Keep track of the use of the XINDEX. */ 4469 need_xndx = false; 4470#endif 4471 4472 /* We we generate a normal symbol table for an executable and the 4473 --export-dynamic option is not given, we need an extra table 4474 which keeps track of the symbol entry belonging to the symbol 4475 table entry. Note that EXPORT_ALL_DYNAMIC is always set if we 4476 generate a DSO so we do not have to test this separately. */ 4477 ndxtosym = (struct symbol **) xcalloc (nsym_allocated, 4478 sizeof (struct symbol)); 4479 4480 /* Create the special symbol for the GOT section. */ 4481 if (ld_state.got_symbol != NULL) 4482 { 4483 assert (nsym < nsym_allocated); 4484 fillin_special_symbol (ld_state.got_symbol, ld_state.gotscnidx, 4485 nsym++, symdata, strtab); 4486 } 4487 4488 /* Similarly for the dynamic section symbol. */ 4489 if (ld_state.dyn_symbol != NULL) 4490 { 4491 assert (nsym < nsym_allocated); 4492 fillin_special_symbol (ld_state.dyn_symbol, ld_state.dynamicscnidx, 4493 nsym++, symdata, strtab); 4494 } 4495 4496 /* Create symbol table entries for the symbols defined in the linker 4497 script. */ 4498 if (ld_state.lscript_syms != NULL) 4499 { 4500 struct symbol *rsym = ld_state.lscript_syms; 4501 do 4502 { 4503 assert (nsym < nsym_allocated); 4504 fillin_special_symbol (rsym, SHN_ABS, nsym++, symdata, strtab); 4505 } 4506 while ((rsym = rsym->next) != NULL); 4507 } 4508 4509 /* Iterate over all input files to collect the symbols. */ 4510 file = ld_state.relfiles->next; 4511 symdata = elf_getdata (elf_getscn (ld_state.outelf, ld_state.symscnidx), 4512 NULL); 4513 do 4514 { 4515 size_t maxcnt; 4516 Elf_Data *insymdata; 4517 Elf_Data *inxndxdata; 4518 4519 /* There must be no dynamic symbol table when creating 4520 relocatable files. */ 4521 assert (ld_state.file_type != relocatable_file_type 4522 || file->dynsymtabdata == NULL); 4523 4524 insymdata = file->symtabdata; 4525 assert (insymdata != NULL); 4526 inxndxdata = file->xndxdata; 4527 4528 maxcnt = file->nsymtab; 4529 4530 file->symindirect = (Elf32_Word *) xcalloc (maxcnt, sizeof (Elf32_Word)); 4531 4532 /* The dynamic symbol table does not contain local symbols. So 4533 we skip those entries. */ 4534 for (cnt = ld_state.need_symtab ? 1 : file->nlocalsymbols; cnt < maxcnt; 4535 ++cnt) 4536 { 4537 XElf_Sym_vardef (sym); 4538 Elf32_Word xndx; 4539 struct symbol *defp = NULL; 4540 4541 xelf_getsymshndx (insymdata, inxndxdata, cnt, sym, xndx); 4542 assert (sym != NULL); 4543 4544 if (unlikely (XELF_ST_TYPE (sym->st_info) == STT_SECTION)) 4545 { 4546 /* Section symbols should always be local but who knows... */ 4547 if (ld_state.need_symtab) 4548 { 4549 /* Determine the real section index in the source file. 4550 Use the XINDEX section content if necessary. We don't 4551 add this information to the dynamic symbol table. */ 4552 if (sym->st_shndx != SHN_XINDEX) 4553 xndx = sym->st_shndx; 4554 4555 assert (file->scninfo[xndx].allsectionsidx 4556 < ld_state.nallsections); 4557 file->symindirect[cnt] = ld_state.allsections[file->scninfo[xndx].allsectionsidx]->scnsymidx; 4558 /* Note that the resulting index can be zero here. There is 4559 no guarantee that the output file will contain all the 4560 sections the input file did. */ 4561 } 4562 continue; 4563 } 4564 4565 if ((ld_state.strip >= strip_all || !ld_state.need_symtab) 4566 /* XXX Do we need these entries? */ 4567 && XELF_ST_TYPE (sym->st_info) == STT_FILE) 4568 continue; 4569 4570#if NATIVE_ELF != 0 4571 /* Copy old data. */ 4572 XElf_Sym *sym2 = sym; 4573 assert (nsym < nsym_allocated); 4574 xelf_getsym (symdata, nsym, sym); 4575 *sym = *sym2; 4576#endif 4577 4578 if (sym->st_shndx != SHN_UNDEF 4579 && (sym->st_shndx < SHN_LORESERVE 4580 || sym->st_shndx == SHN_XINDEX)) 4581 { 4582 /* If we are creating an executable with no normal 4583 symbol table and we do not export all symbols and 4584 this symbol is not defined in a DSO as well, ignore 4585 it. */ 4586 if (!ld_state.export_all_dynamic && !ld_state.need_symtab) 4587 { 4588 assert (cnt >= file->nlocalsymbols); 4589 defp = file->symref[cnt]; 4590 assert (defp != NULL); 4591 4592 if (!defp->in_dso) 4593 /* Ignore it. */ 4594 continue; 4595 } 4596 4597 /* Determine the real section index in the source file. Use 4598 the XINDEX section content if necessary. */ 4599 if (sym->st_shndx != SHN_XINDEX) 4600 xndx = sym->st_shndx; 4601 4602 sym->st_value += file->scninfo[xndx].offset; 4603 4604 assert (file->scninfo[xndx].outscnndx < SHN_LORESERVE 4605 || file->scninfo[xndx].outscnndx > SHN_HIRESERVE); 4606 if (unlikely (file->scninfo[xndx].outscnndx > SHN_LORESERVE)) 4607 { 4608 /* It is not possible to have an extended section index 4609 table for the dynamic symbol table. */ 4610 if (!ld_state.need_symtab) 4611 error (EXIT_FAILURE, 0, gettext ("\ 4612section index too large in dynamic symbol table")); 4613 4614 assert (xndxdata != NULL); 4615 sym->st_shndx = SHN_XINDEX; 4616 xndx = file->scninfo[xndx].outscnndx; 4617#ifndef NDEBUG 4618 need_xndx = true; 4619#endif 4620 } 4621 else 4622 { 4623 sym->st_shndx = file->scninfo[xndx].outscnndx; 4624 xndx = 0; 4625 } 4626 } 4627 else if (sym->st_shndx == SHN_COMMON || sym->st_shndx == SHN_UNDEF) 4628 { 4629 /* Check whether we have a (real) definition for this 4630 symbol. If this is the case we skip this symbol 4631 table entry. */ 4632 assert (cnt >= file->nlocalsymbols); 4633 defp = file->symref[cnt]; 4634 assert (defp != NULL); 4635 4636 assert (sym->st_shndx != SHN_COMMON || defp->defined); 4637 4638 if ((sym->st_shndx == SHN_COMMON && !defp->common) 4639 || (sym->st_shndx == SHN_UNDEF && defp->defined) 4640 || defp->added) 4641 /* Ignore this symbol table entry, there is a 4642 "better" one or we already added it. */ 4643 continue; 4644 4645 /* Remember that we already added this symbol. */ 4646 defp->added = 1; 4647 4648 /* Adjust the section number for common symbols. */ 4649 if (sym->st_shndx == SHN_COMMON) 4650 { 4651 sym->st_value = (ld_state.common_section->offset 4652 + file->symref[cnt]->merge.value); 4653 assert (ld_state.common_section->outscnndx < SHN_LORESERVE); 4654 sym->st_shndx = ld_state.common_section->outscnndx; 4655 xndx = 0; 4656 } 4657 } 4658 else if (unlikely (sym->st_shndx != SHN_ABS)) 4659 { 4660 if (SPECIAL_SECTION_NUMBER_P (&ld_state, sym->st_shndx)) 4661 /* XXX Add code to handle machine specific special 4662 sections. */ 4663 abort (); 4664 } 4665 4666 /* Add the symbol name to the string table. If the user 4667 chooses the highest level of stripping avoid adding names 4668 for local symbols in the string table. */ 4669 if (sym->st_name != 0 4670 && (ld_state.strip < strip_everything 4671 || XELF_ST_BIND (sym->st_info) != STB_LOCAL)) 4672 symstrent[nsym] = ebl_strtabadd (strtab, 4673 elf_strptr (file->elf, 4674 file->symstridx, 4675 sym->st_name), 0); 4676 4677 /* Once we know the name this field will get the correct 4678 offset. For now set it to zero which means no name 4679 associated. */ 4680 sym->st_name = 0; 4681 4682 /* If we had to merge sections we have a completely new 4683 offset for the symbol. */ 4684 if (file->has_merge_sections && file->symref[cnt] != NULL 4685 && file->symref[cnt]->merged) 4686 sym->st_value = file->symref[cnt]->merge.value; 4687 4688 /* Create the record in the output sections. */ 4689 assert (nsym < nsym_allocated); 4690 xelf_update_symshndx (symdata, xndxdata, nsym, sym, xndx, 0); 4691 4692 /* Add the reference to the symbol record in case we need it. 4693 Find the symbol if this has not happened yet. We do 4694 not need the information for local symbols. */ 4695 if (defp == NULL && cnt >= file->nlocalsymbols) 4696 { 4697 defp = file->symref[cnt]; 4698 assert (defp != NULL); 4699 } 4700 4701 /* Store the reference to the symbol record. The 4702 sorting code will have to keep this array in the 4703 correct order, too. */ 4704 ndxtosym[nsym] = defp; 4705 4706 /* One more entry finished. */ 4707 if (cnt >= file->nlocalsymbols) 4708 { 4709 assert (file->symref[cnt]->outsymidx == 0); 4710 file->symref[cnt]->outsymidx = nsym; 4711 } 4712 file->symindirect[cnt] = nsym++; 4713 } 4714 } 4715 while ((file = file->next) != ld_state.relfiles->next); 4716 /* Make sure we didn't create the extended section index table for 4717 nothing. */ 4718 assert (xndxdata == NULL || need_xndx); 4719 4720 4721 /* Create the version related sections. */ 4722 if (ld_state.verneedscnidx != 0) 4723 { 4724 /* We know the number of input files and total number of 4725 referenced versions. This allows us to allocate the memory 4726 and then we iterate over the DSOs to get the version 4727 information. */ 4728 struct usedfiles *runp; 4729 4730 runp = ld_state.dsofiles->next; 4731 do 4732 allocate_version_names (runp, dynstrtab); 4733 while ((runp = runp->next) != ld_state.dsofiles->next); 4734 4735 if (ld_state.needed != NULL) 4736 { 4737 runp = ld_state.needed->next; 4738 do 4739 allocate_version_names (runp, dynstrtab); 4740 while ((runp = runp->next) != ld_state.needed->next); 4741 } 4742 } 4743 4744 /* At this point we should hide symbols and so on. */ 4745 if (ld_state.default_bind_local || ld_state.version_str_tab.filled > 0) 4746 /* XXX Add one more test when handling of wildcard symbol names 4747 is supported. */ 4748 { 4749 /* Check all non-local symbols whether they are on the export list. */ 4750 bool any_reduced = false; 4751 4752 for (cnt = 1; cnt < nsym; ++cnt) 4753 { 4754 XElf_Sym_vardef (sym); 4755 4756 /* Note that we don't have to use 'xelf_getsymshndx' since we 4757 only need the binding and the symbol name. */ 4758 xelf_getsym (symdata, cnt, sym); 4759 assert (sym != NULL); 4760 4761 if (reduce_symbol_p (sym, symstrent[cnt])) 4762 { 4763 sym->st_info = XELF_ST_INFO (STB_LOCAL, 4764 XELF_ST_TYPE (sym->st_info)); 4765 (void) xelf_update_sym (symdata, cnt, sym); 4766 4767 /* Show that we don't need this string anymore. */ 4768 if (ld_state.strip == strip_everything) 4769 { 4770 symstrent[cnt] = NULL; 4771 any_reduced = true; 4772 } 4773 } 4774 } 4775 4776 if (unlikely (any_reduced)) 4777 { 4778 /* Since we will not write names of local symbols in the 4779 output file and we have reduced the binding of some 4780 symbols the string table previously constructed contains 4781 too many string. Correct it. */ 4782 struct Ebl_Strtab *newp = ebl_strtabinit (true); 4783 4784 for (cnt = 1; cnt < nsym; ++cnt) 4785 if (symstrent[cnt] != NULL) 4786 symstrent[cnt] = ebl_strtabadd (newp, 4787 ebl_string (symstrent[cnt]), 0); 4788 4789 ebl_strtabfree (strtab); 4790 strtab = newp; 4791 } 4792 } 4793 4794 /* Add the references to DSOs. We can add these entries this late 4795 (after sorting out versioning) because references to DSOs are not 4796 effected. */ 4797 if (ld_state.from_dso != NULL) 4798 { 4799 struct symbol *runp; 4800 size_t plt_base = nsym + ld_state.nfrom_dso - ld_state.nplt; 4801 size_t plt_idx = 0; 4802 size_t obj_idx = 0; 4803 4804 assert (ld_state.nfrom_dso >= ld_state.nplt); 4805 runp = ld_state.from_dso; 4806 do 4807 { 4808 // XXX What about functions which are only referenced via 4809 // pointers and not PLT entries? Can we distinguish such uses? 4810 size_t idx; 4811 if (runp->type == STT_FUNC) 4812 { 4813 /* Store the PLT entry number. */ 4814 runp->merge.value = plt_idx + 1; 4815 idx = plt_base + plt_idx++; 4816 } 4817 else 4818 idx = nsym + obj_idx++; 4819 4820 XElf_Sym_vardef (sym); 4821 xelf_getsym_ptr (symdata, idx, sym); 4822 4823 sym->st_value = 0; 4824 sym->st_size = runp->size; 4825 sym->st_info = XELF_ST_INFO (runp->weak ? STB_WEAK : STB_GLOBAL, 4826 runp->type); 4827 sym->st_other = STV_DEFAULT; 4828 sym->st_shndx = SHN_UNDEF; 4829 4830 /* Create the record in the output sections. */ 4831 xelf_update_symshndx (symdata, xndxdata, idx, sym, 0, 0); 4832 4833 const char *name = runp->name; 4834 size_t namelen = 0; 4835 4836 if (runp->file->verdefdata != NULL) 4837 { 4838 // XXX Is it useful to add the versym value to struct symbol? 4839 XElf_Versym versym; 4840 4841 (void) xelf_getversym_copy (runp->file->versymdata, runp->symidx, 4842 versym); 4843 4844 /* One can only link with the default version. */ 4845 assert ((versym & 0x8000) == 0); 4846 4847 const char *versname 4848 = ebl_string (runp->file->verdefent[versym]); 4849 4850 size_t versname_len = strlen (versname) + 1; 4851 namelen = strlen (name) + versname_len + 2; 4852 char *newp = (char *) obstack_alloc (&ld_state.smem, namelen); 4853 memcpy (stpcpy (stpcpy (newp, name), "@@"), 4854 versname, versname_len); 4855 name = newp; 4856 } 4857 4858 symstrent[idx] = ebl_strtabadd (strtab, name, namelen); 4859 4860 /* Record the initial index in the symbol table. */ 4861 runp->outsymidx = idx; 4862 4863 /* Remember the symbol record this ELF symbol came from. */ 4864 ndxtosym[idx] = runp; 4865 } 4866 while ((runp = runp->next) != ld_state.from_dso); 4867 4868 assert (nsym + obj_idx == plt_base); 4869 assert (plt_idx == ld_state.nplt); 4870 nsym = plt_base + plt_idx; 4871 } 4872 4873 /* Now we know how many symbols will be in the output file. Adjust 4874 the count in the section data. */ 4875 symdata->d_size = xelf_fsize (ld_state.outelf, ELF_T_SYM, nsym); 4876 if (unlikely (xndxdata != NULL)) 4877 xndxdata->d_size = xelf_fsize (ld_state.outelf, ELF_T_WORD, nsym); 4878 4879 /* Create the symbol string table section. */ 4880 strscn = elf_newscn (ld_state.outelf); 4881 ld_state.strscnidx = elf_ndxscn (strscn); 4882 data = elf_newdata (strscn); 4883 xelf_getshdr (strscn, shdr); 4884 if (data == NULL || shdr == NULL) 4885 error (EXIT_FAILURE, 0, 4886 gettext ("cannot create section for output file: %s"), 4887 elf_errmsg (-1)); 4888 4889 /* Create a compact string table, allocate the memory for it, and 4890 fill in the section data information. */ 4891 ebl_strtabfinalize (strtab, data); 4892 4893 shdr->sh_type = SHT_STRTAB; 4894 assert (shdr->sh_entsize == 0); 4895 4896 if (unlikely (xelf_update_shdr (strscn, shdr) == 0)) 4897 error (EXIT_FAILURE, 0, 4898 gettext ("cannot create section for output file: %s"), 4899 elf_errmsg (-1)); 4900 4901 /* Fill in the offsets of the symbol names. */ 4902 for (cnt = 1; cnt < nsym; ++cnt) 4903 if (symstrent[cnt] != NULL) 4904 { 4905 XElf_Sym_vardef (sym); 4906 4907 /* Note that we don't have to use 'xelf_getsymshndx' since we don't 4908 modify the section index. */ 4909 xelf_getsym (symdata, cnt, sym); 4910 /* This better worked, we did it before. */ 4911 assert (sym != NULL); 4912 sym->st_name = ebl_strtaboffset (symstrent[cnt]); 4913 (void) xelf_update_sym (symdata, cnt, sym); 4914 } 4915 4916 /* Since we are going to reorder the symbol table but still have to 4917 be able to find the new position based on the old one (since the 4918 latter is stored in 'symindirect' information of the input file 4919 data structure) we have to create yet another indirection 4920 table. */ 4921 ld_state.dblindirect = dblindirect 4922 = (Elf32_Word *) xmalloc (nsym * sizeof (Elf32_Word)); 4923 4924 /* Sort the symbol table so that the local symbols come first. */ 4925 /* XXX We don't use stable sorting here. It seems not necessary and 4926 would be more expensive. If it turns out to be necessary this can 4927 be fixed easily. */ 4928 nsym_local = 1; 4929 cnt = nsym - 1; 4930 while (nsym_local < cnt) 4931 { 4932 XElf_Sym_vardef (locsym); 4933 Elf32_Word locxndx; 4934 XElf_Sym_vardef (globsym); 4935 Elf32_Word globxndx; 4936 4937 do 4938 { 4939 xelf_getsymshndx (symdata, xndxdata, nsym_local, locsym, locxndx); 4940 /* This better works. */ 4941 assert (locsym != NULL); 4942 4943 if (XELF_ST_BIND (locsym->st_info) != STB_LOCAL 4944 && (ld_state.need_symtab || ld_state.export_all_dynamic)) 4945 { 4946 do 4947 { 4948 xelf_getsymshndx (symdata, xndxdata, cnt, globsym, globxndx); 4949 /* This better works. */ 4950 assert (globsym != NULL); 4951 4952 if (unlikely (XELF_ST_BIND (globsym->st_info) == STB_LOCAL)) 4953 { 4954 /* We swap the two entries. */ 4955#if NATIVE_ELF != 0 4956 /* Since we directly modify the data in the ELF 4957 data structure we have to make a copy of one 4958 of the entries. */ 4959 XElf_Sym locsym_copy = *locsym; 4960 locsym = &locsym_copy; 4961#endif 4962 xelf_update_symshndx (symdata, xndxdata, nsym_local, 4963 globsym, globxndx, 1); 4964 xelf_update_symshndx (symdata, xndxdata, cnt, 4965 locsym, locxndx, 1); 4966 4967 /* Also swap the cross references. */ 4968 dblindirect[nsym_local] = cnt; 4969 dblindirect[cnt] = nsym_local; 4970 4971 /* And the entries for the symbol names. */ 4972 struct Ebl_Strent *strtmp = symstrent[nsym_local]; 4973 symstrent[nsym_local] = symstrent[cnt]; 4974 symstrent[cnt] = strtmp; 4975 4976 /* And the mapping from symbol table entry to 4977 struct symbol record. */ 4978 struct symbol *symtmp = ndxtosym[nsym_local]; 4979 ndxtosym[nsym_local] = ndxtosym[cnt]; 4980 ndxtosym[cnt] = symtmp; 4981 4982 /* Go to the next entry. */ 4983 ++nsym_local; 4984 --cnt; 4985 4986 break; 4987 } 4988 4989 dblindirect[cnt] = cnt; 4990 } 4991 while (nsym_local < --cnt); 4992 4993 break; 4994 } 4995 4996 dblindirect[nsym_local] = nsym_local; 4997 } 4998 while (++nsym_local < cnt); 4999 } 5000 5001 /* The symbol 'nsym_local' is currently pointing to might be local, 5002 too. Check and increment the variable if this is the case. */ 5003 if (likely (nsym_local < nsym)) 5004 { 5005 XElf_Sym_vardef (locsym); 5006 5007 /* This entry isn't moved. */ 5008 dblindirect[nsym_local] = nsym_local; 5009 5010 /* Note that it is OK to not use 'xelf_getsymshndx' here. */ 5011 xelf_getsym (symdata, nsym_local, locsym); 5012 /* This better works. */ 5013 assert (locsym != NULL); 5014 5015 if (XELF_ST_BIND (locsym->st_info) == STB_LOCAL) 5016 ++nsym_local; 5017 } 5018 5019 5020 /* We need the versym array right away to keep track of the version 5021 symbols. */ 5022 if (ld_state.versymscnidx != 0) 5023 { 5024 /* We allocate more memory than we need since the array is morroring 5025 the dynamic symbol table and not the normal symbol table. I.e., 5026 no local symbols are present. */ 5027 versymscn = elf_getscn (ld_state.outelf, ld_state.versymscnidx); 5028 versymdata = elf_newdata (versymscn); 5029 if (versymdata == NULL) 5030 error (EXIT_FAILURE, 0, 5031 gettext ("cannot create versioning section: %s"), 5032 elf_errmsg (-1)); 5033 5034 versymdata->d_size = xelf_fsize (ld_state.outelf, ELF_T_HALF, 5035 nsym - nsym_local + 1); 5036 versymdata->d_buf = xcalloc (1, versymdata->d_size); 5037 versymdata->d_align = xelf_fsize (ld_state.outelf, ELF_T_HALF, 1); 5038 versymdata->d_off = 0; 5039 versymdata->d_type = ELF_T_HALF; 5040 } 5041 5042 5043 /* If we have to construct the dynamic symbol table we must not include 5044 the local symbols. If the normal symbol has to be emitted as well 5045 we haven't done anything else yet and we can construct it from 5046 scratch now. */ 5047 if (unlikely (!ld_state.need_symtab)) 5048 { 5049 /* Note that the following code works even if there is no entry 5050 to remove since the zeroth entry is always local. */ 5051 size_t reduce = xelf_fsize (ld_state.outelf, ELF_T_SYM, nsym_local - 1); 5052 5053 XElf_Sym_vardef (nullsym); 5054 xelf_getsym_ptr (symdata, nsym_local - 1, nullsym); 5055 5056 /* Note that we don't have to use 'xelf_update_symshndx' since 5057 this is the dynamic symbol table we write. */ 5058 (void) xelf_update_sym (symdata, nsym_local - 1, 5059 memset (nullsym, '\0', sizeof (*nullsym))); 5060 5061 /* Update the buffer pointer and size in the output data. */ 5062 symdata->d_buf = (char *) symdata->d_buf + reduce; 5063 symdata->d_size -= reduce; 5064 5065 /* Add the version symbol information. */ 5066 if (versymdata != NULL) 5067 { 5068 nsym_dyn = 1; 5069 for (cnt = nsym_local; cnt < nsym; ++cnt, ++nsym_dyn) 5070 { 5071 struct symbol *symp = ndxtosym[cnt]; 5072 5073 if (symp->file->versymdata != NULL) 5074 { 5075 GElf_Versym versym; 5076 5077 gelf_getversym (symp->file->versymdata, symp->symidx, 5078 &versym); 5079 5080 (void) gelf_update_versym (versymdata, nsym_dyn, 5081 &symp->file->verdefused[versym]); 5082 } 5083 } 5084 } 5085 5086 /* Since we only created the dynamic symbol table the number of 5087 dynamic symbols is the total number of symbols. */ 5088 nsym_dyn = nsym - nsym_local + 1; 5089 5090 /* XXX TBI. Create whatever data structure is missing. */ 5091 abort (); 5092 } 5093 else if (ld_state.need_dynsym) 5094 { 5095 /* Create the dynamic symbol table section data along with the 5096 string table. We look at all non-local symbols we found for 5097 the normal symbol table and add those. */ 5098 dynsymscn = elf_getscn (ld_state.outelf, ld_state.dynsymscnidx); 5099 dynsymdata = elf_newdata (dynsymscn); 5100 5101 dynstrdata = elf_newdata (elf_getscn (ld_state.outelf, 5102 ld_state.dynstrscnidx)); 5103 if (dynsymdata == NULL || dynstrdata == NULL) 5104 error (EXIT_FAILURE, 0, gettext ("\ 5105cannot create dynamic symbol table for output file: %s"), 5106 elf_errmsg (-1)); 5107 5108 nsym_dyn_allocated = nsym - nsym_local + 1; 5109 dynsymdata->d_size = xelf_fsize (ld_state.outelf, ELF_T_SYM, 5110 nsym_dyn_allocated); 5111 dynsymdata->d_buf = memset (xmalloc (dynsymdata->d_size), '\0', 5112 xelf_fsize (ld_state.outelf, ELF_T_SYM, 1)); 5113 dynsymdata->d_type = ELF_T_SYM; 5114 dynsymdata->d_off = 0; 5115 dynsymdata->d_align = xelf_fsize (ld_state.outelf, ELF_T_ADDR, 1); 5116 5117 /* We need one more array which contains the hash codes of the 5118 symbol names. */ 5119 hashcodes = (Elf32_Word *) xcalloc (nsym_dyn_allocated, 5120 sizeof (Elf32_Word)); 5121 5122 /* We have and empty entry at the beginning. */ 5123 nsym_dyn = 1; 5124 5125 /* We don't mix PLT symbols and others. */ 5126 size_t plt_idx = 1; 5127 size_t obj_idx = 1 + ld_state.nplt; 5128 5129 /* Populate the table. */ 5130 for (cnt = nsym_local; cnt < nsym; ++cnt) 5131 { 5132 XElf_Sym_vardef (sym); 5133 5134 xelf_getsym (symdata, cnt, sym); 5135 assert (sym != NULL); 5136 5137 if (sym->st_shndx == SHN_XINDEX) 5138 error (EXIT_FAILURE, 0, gettext ("\ 5139section index too large in dynamic symbol table")); 5140 5141 /* We do not add the symbol to the dynamic symbol table if 5142 5143 - the symbol is for a file 5144 - it is not externally visible (internal, hidden) 5145 - if export_all_dynamic is not set and is only defined in 5146 the executable (i.e., it is defined, but not (also) in 5147 in DSO) 5148 5149 Set symstrent[cnt] to NULL in case an entry is ignored. */ 5150 if (XELF_ST_TYPE (sym->st_info) == STT_FILE 5151 || XELF_ST_VISIBILITY (sym->st_other) == STV_INTERNAL 5152 || XELF_ST_VISIBILITY (sym->st_other) == STV_HIDDEN 5153 || (!ndxtosym[cnt]->in_dso && ndxtosym[cnt]->defined)) 5154 { 5155 symstrent[cnt] = NULL; 5156 continue; 5157 } 5158 5159 size_t idx; 5160 if (ndxtosym[cnt]->in_dso && ndxtosym[cnt]->type == STT_FUNC) 5161 { 5162 idx = plt_idx++; 5163 assert (idx < 1 + ld_state.nplt); 5164 } 5165 else 5166 { 5167 idx = obj_idx++; 5168 assert (idx < nsym_dyn_allocated); 5169 } 5170 5171 /* Add the version information. */ 5172 if (versymdata != NULL) 5173 { 5174 struct symbol *symp = ndxtosym[cnt]; 5175 5176 if (symp->file->verdefdata != NULL) 5177 { 5178 GElf_Versym versym; 5179 5180 gelf_getversym (symp->file->versymdata, symp->symidx, 5181 &versym); 5182 5183 (void) gelf_update_versym (versymdata, idx, 5184 &symp->file->verdefused[versym]); 5185 } 5186 else 5187 { 5188 /* XXX Add support for version definitions. */ 5189 GElf_Versym global = VER_NDX_GLOBAL; 5190 (void) gelf_update_versym (versymdata, idx, &global); 5191 } 5192 } 5193 5194 /* Store the index of the symbol in the dynamic symbol table. */ 5195 ndxtosym[cnt]->outdynsymidx = idx; 5196 5197 /* Create a new string table entry. */ 5198 const char *str = ndxtosym[cnt]->name; 5199 symstrent[cnt] = ebl_strtabadd (dynstrtab, str, 0); 5200 hashcodes[idx] = elf_hash (str); 5201 ++nsym_dyn; 5202 } 5203 assert (nsym_dyn == obj_idx); 5204 assert (ld_state.nplt + 1 == plt_idx); 5205 5206 /* Update the information about the symbol section. */ 5207 if (versymdata != NULL) 5208 { 5209 /* Correct the size now that we know how many entries the 5210 dynamic symbol table has. */ 5211 versymdata->d_size = xelf_fsize (ld_state.outelf, ELF_T_HALF, 5212 nsym_dyn); 5213 5214 /* Add the reference to the symbol table. */ 5215 xelf_getshdr (versymscn, shdr); 5216 assert (shdr != NULL); 5217 5218 shdr->sh_link = ld_state.dynsymscnidx; 5219 5220 (void) xelf_update_shdr (versymscn, shdr); 5221 } 5222 } 5223 5224 if (ld_state.file_type != relocatable_file_type) 5225 { 5226 size_t nbucket; 5227 Elf32_Word *bucket; 5228 Elf32_Word *chain; 5229 size_t nchain; 5230 Elf_Scn *hashscn; 5231 Elf_Data *hashdata; 5232 5233 /* Finalize the dynamic string table. */ 5234 ebl_strtabfinalize (dynstrtab, dynstrdata); 5235 5236 /* Determine the "optimal" bucket size. */ 5237 nbucket = optimal_bucket_size (hashcodes, nsym_dyn, ld_state.optlevel); 5238 5239 /* Create the .hash section data structures. */ 5240 assert (ld_state.hashscnidx != 0); 5241 hashscn = elf_getscn (ld_state.outelf, ld_state.hashscnidx); 5242 xelf_getshdr (hashscn, shdr); 5243 hashdata = elf_newdata (hashscn); 5244 if (shdr == NULL || hashdata == NULL) 5245 error (EXIT_FAILURE, 0, gettext ("\ 5246cannot create hash table section for output file: %s"), 5247 elf_errmsg (-1)); 5248 5249 shdr->sh_link = ld_state.dynsymscnidx; 5250 (void) xelf_update_shdr (hashscn, shdr); 5251 5252 hashdata->d_size = (2 + nsym_dyn + nbucket) * sizeof (Elf32_Word); 5253 hashdata->d_buf = xcalloc (1, hashdata->d_size); 5254 hashdata->d_align = sizeof (Elf32_Word); 5255 hashdata->d_type = ELF_T_WORD; 5256 hashdata->d_off = 0; 5257 5258 ((Elf32_Word *) hashdata->d_buf)[0] = nbucket; 5259 ((Elf32_Word *) hashdata->d_buf)[1] = nsym_dyn; 5260 bucket = &((Elf32_Word *) hashdata->d_buf)[2]; 5261 chain = &((Elf32_Word *) hashdata->d_buf)[2 + nbucket]; 5262 5263 /* Haven't yet filled in any chain value. */ 5264 nchain = 0; 5265 5266 /* Now put the names in. */ 5267 for (cnt = nsym_local; cnt < nsym; ++cnt) 5268 if (symstrent[cnt] != NULL) 5269 { 5270 XElf_Sym_vardef (sym); 5271 size_t hashidx; 5272 size_t dynidx = ndxtosym[cnt]->outdynsymidx; 5273 5274#if NATIVE_ELF != 0 5275 XElf_Sym *osym; 5276 memcpy (xelf_getsym (dynsymdata, dynidx, sym), 5277 xelf_getsym (symdata, cnt, osym), 5278 sizeof (XElf_Sym)); 5279#else 5280 xelf_getsym (symdata, cnt, sym); 5281 assert (sym != NULL); 5282#endif 5283 5284 sym->st_name = ebl_strtaboffset (symstrent[cnt]); 5285 5286 (void) xelf_update_sym (dynsymdata, dynidx, sym); 5287 5288 /* Add to the hash table. */ 5289 hashidx = hashcodes[dynidx] % nbucket; 5290 if (bucket[hashidx] == 0) 5291 bucket[hashidx] = dynidx; 5292 else 5293 { 5294 hashidx = bucket[hashidx]; 5295 while (chain[hashidx] != 0) 5296 hashidx = chain[hashidx]; 5297 5298 chain[hashidx] = dynidx; 5299 } 5300 } 5301 5302 free (hashcodes); 5303 5304 /* We don't need the map from the symbol table index to the symbol 5305 structure anymore. */ 5306 free (ndxtosym); 5307 5308 /* Create the required version section. */ 5309 if (ld_state.verneedscnidx != 0) 5310 { 5311 Elf_Scn *verneedscn; 5312 Elf_Data *verneeddata; 5313 struct usedfiles *runp; 5314 size_t verneed_size = xelf_fsize (ld_state.outelf, ELF_T_VNEED, 1); 5315 size_t vernaux_size = xelf_fsize (ld_state.outelf, ELF_T_VNAUX, 1); 5316 size_t offset; 5317 int ntotal; 5318 5319 verneedscn = elf_getscn (ld_state.outelf, ld_state.verneedscnidx); 5320 xelf_getshdr (verneedscn, shdr); 5321 verneeddata = elf_newdata (verneedscn); 5322 if (shdr == NULL || verneeddata == NULL) 5323 error (EXIT_FAILURE, 0, 5324 gettext ("cannot create versioning data: %s"), 5325 elf_errmsg (-1)); 5326 5327 verneeddata->d_size = (ld_state.nverdeffile * verneed_size 5328 + ld_state.nverdefused * vernaux_size); 5329 verneeddata->d_buf = xmalloc (verneeddata->d_size); 5330 verneeddata->d_type = ELF_T_VNEED; 5331 verneeddata->d_align = xelf_fsize (ld_state.outelf, ELF_T_WORD, 1); 5332 verneeddata->d_off = 0; 5333 5334 offset = 0; 5335 ntotal = ld_state.nverdeffile; 5336 runp = ld_state.dsofiles->next; 5337 do 5338 { 5339 offset = create_verneed_data (offset, verneeddata, runp, 5340 &ntotal); 5341 runp = runp->next; 5342 } 5343 while (ntotal > 0 && runp != ld_state.dsofiles->next); 5344 5345 if (ntotal > 0) 5346 { 5347 runp = ld_state.needed->next; 5348 do 5349 { 5350 offset = create_verneed_data (offset, verneeddata, runp, 5351 &ntotal); 5352 runp = runp->next; 5353 } 5354 while (ntotal > 0 && runp != ld_state.needed->next); 5355 } 5356 5357 assert (offset == verneeddata->d_size); 5358 5359 /* Add the needed information to the section header. */ 5360 shdr->sh_link = ld_state.dynstrscnidx; 5361 shdr->sh_info = ld_state.nverdeffile; 5362 (void) xelf_update_shdr (verneedscn, shdr); 5363 } 5364 5365 /* Adjust the section size. */ 5366 dynsymdata->d_size = xelf_fsize (ld_state.outelf, ELF_T_SYM, nsym_dyn); 5367 if (versymdata != NULL) 5368 versymdata->d_size = xelf_fsize (ld_state.outelf, ELF_T_HALF, 5369 nsym_dyn); 5370 5371 /* Add the remaining information to the section header. */ 5372 xelf_getshdr (dynsymscn, shdr); 5373 /* There is always exactly one local symbol. */ 5374 shdr->sh_info = 1; 5375 /* Reference the string table. */ 5376 shdr->sh_link = ld_state.dynstrscnidx; 5377 /* Write the updated info back. */ 5378 (void) xelf_update_shdr (dynsymscn, shdr); 5379 } 5380 else 5381 /* We don't need the map from the symbol table index to the symbol 5382 structure anymore. */ 5383 free (ndxtosym); 5384 5385 /* We don't need the string table anymore. */ 5386 free (symstrent); 5387 5388 /* Remember the total number of symbols in the dynamic symbol table. */ 5389 ld_state.ndynsym = nsym_dyn; 5390 5391 /* Fill in the section header information. */ 5392 symscn = elf_getscn (ld_state.outelf, ld_state.symscnidx); 5393 xelf_getshdr (symscn, shdr); 5394 if (shdr == NULL) 5395 error (EXIT_FAILURE, 0, 5396 gettext ("cannot create symbol table for output file: %s"), 5397 elf_errmsg (-1)); 5398 5399 shdr->sh_type = SHT_SYMTAB; 5400 shdr->sh_link = ld_state.strscnidx; 5401 shdr->sh_info = nsym_local; 5402 shdr->sh_entsize = xelf_fsize (ld_state.outelf, ELF_T_SYM, 1); 5403 5404 (void) xelf_update_shdr (symscn, shdr); 5405 5406 5407 /* Add names for the generated sections. */ 5408 if (ld_state.symscnidx != 0) 5409 symtab_ent = ebl_strtabadd (ld_state.shstrtab, ".symtab", 8); 5410 if (ld_state.xndxscnidx != 0) 5411 xndx_ent = ebl_strtabadd (ld_state.shstrtab, ".symtab_shndx", 14); 5412 if (ld_state.strscnidx != 0) 5413 strtab_ent = ebl_strtabadd (ld_state.shstrtab, ".strtab", 8); 5414 /* At this point we would have to test for failures in the 5415 allocation. But we skip this. First, the problem will be caught 5416 latter when doing more allocations for the section header table. 5417 Even if this would not be the case all that would happen is that 5418 the section names are empty. The binary would still be usable if 5419 it is an executable or a DSO. Not adding the test here saves 5420 quite a bit of code. */ 5421 5422 5423 /* Finally create the section for the section header string table. */ 5424 shstrtab_scn = elf_newscn (ld_state.outelf); 5425 shstrtab_ndx = elf_ndxscn (shstrtab_scn); 5426 if (unlikely (shstrtab_ndx == SHN_UNDEF)) 5427 error (EXIT_FAILURE, 0, 5428 gettext ("cannot create section header string section: %s"), 5429 elf_errmsg (-1)); 5430 5431 /* Add the name of the section to the string table. */ 5432 shstrtab_ent = ebl_strtabadd (ld_state.shstrtab, ".shstrtab", 10); 5433 if (unlikely (shstrtab_ent == NULL)) 5434 error (EXIT_FAILURE, errno, 5435 gettext ("cannot create section header string section")); 5436 5437 /* Finalize the section header string table. */ 5438 data = elf_newdata (shstrtab_scn); 5439 if (data == NULL) 5440 error (EXIT_FAILURE, 0, 5441 gettext ("cannot create section header string section: %s"), 5442 elf_errmsg (-1)); 5443 ebl_strtabfinalize (ld_state.shstrtab, data); 5444 5445 /* Now we know the string offsets for all section names. */ 5446 for (cnt = 0; cnt < ld_state.nallsections; ++cnt) 5447 if (ld_state.allsections[cnt]->scnidx != 0) 5448 { 5449 Elf_Scn *scn; 5450 5451 scn = elf_getscn (ld_state.outelf, ld_state.allsections[cnt]->scnidx); 5452 5453 xelf_getshdr (scn, shdr); 5454 assert (shdr != NULL); 5455 5456 shdr->sh_name = ebl_strtaboffset (ld_state.allsections[cnt]->nameent); 5457 5458 if (xelf_update_shdr (scn, shdr) == 0) 5459 assert (0); 5460 } 5461 5462 /* Add the names for the generated sections to the respective 5463 section headers. */ 5464 if (symtab_ent != NULL) 5465 { 5466 Elf_Scn *scn = elf_getscn (ld_state.outelf, ld_state.symscnidx); 5467 5468 xelf_getshdr (scn, shdr); 5469 /* This cannot fail, we already accessed the header before. */ 5470 assert (shdr != NULL); 5471 5472 shdr->sh_name = ebl_strtaboffset (symtab_ent); 5473 5474 (void) xelf_update_shdr (scn, shdr); 5475 } 5476 if (xndx_ent != NULL) 5477 { 5478 Elf_Scn *scn = elf_getscn (ld_state.outelf, ld_state.xndxscnidx); 5479 5480 xelf_getshdr (scn, shdr); 5481 /* This cannot fail, we already accessed the header before. */ 5482 assert (shdr != NULL); 5483 5484 shdr->sh_name = ebl_strtaboffset (xndx_ent); 5485 5486 (void) xelf_update_shdr (scn, shdr); 5487 } 5488 if (strtab_ent != NULL) 5489 { 5490 Elf_Scn *scn = elf_getscn (ld_state.outelf, ld_state.strscnidx); 5491 5492 xelf_getshdr (scn, shdr); 5493 /* This cannot fail, we already accessed the header before. */ 5494 assert (shdr != NULL); 5495 5496 shdr->sh_name = ebl_strtaboffset (strtab_ent); 5497 5498 (void) xelf_update_shdr (scn, shdr); 5499 } 5500 5501 /* And the section header table section itself. */ 5502 xelf_getshdr (shstrtab_scn, shdr); 5503 if (shdr == NULL) 5504 error (EXIT_FAILURE, 0, 5505 gettext ("cannot create section header string section: %s"), 5506 elf_errmsg (-1)); 5507 5508 shdr->sh_name = ebl_strtaboffset (shstrtab_ent); 5509 shdr->sh_type = SHT_STRTAB; 5510 5511 if (unlikely (xelf_update_shdr (shstrtab_scn, shdr) == 0)) 5512 error (EXIT_FAILURE, 0, 5513 gettext ("cannot create section header string section: %s"), 5514 elf_errmsg (-1)); 5515 5516 5517 /* Add the correct section header info to the section group sections. */ 5518 groups = ld_state.groups; 5519 while (groups != NULL) 5520 { 5521 Elf_Scn *scn; 5522 struct scngroup *oldp; 5523 Elf32_Word si; 5524 5525 scn = elf_getscn (ld_state.outelf, groups->outscnidx); 5526 xelf_getshdr (scn, shdr); 5527 assert (shdr != NULL); 5528 5529 shdr->sh_name = ebl_strtaboffset (groups->nameent); 5530 shdr->sh_type = SHT_GROUP; 5531 shdr->sh_flags = 0; 5532 shdr->sh_link = ld_state.symscnidx; 5533 shdr->sh_entsize = sizeof (Elf32_Word); 5534 5535 /* Determine the index for the signature symbol. */ 5536 si = groups->symbol->file->symindirect[groups->symbol->symidx]; 5537 if (si == 0) 5538 { 5539 assert (groups->symbol->file->symref[groups->symbol->symidx] 5540 != NULL); 5541 si = groups->symbol->file->symref[groups->symbol->symidx]->outsymidx; 5542 assert (si != 0); 5543 } 5544 shdr->sh_info = ld_state.dblindirect[si]; 5545 5546 (void) xelf_update_shdr (scn, shdr); 5547 5548 oldp = groups; 5549 groups = groups->next; 5550 free (oldp); 5551 } 5552 5553 5554 if (ld_state.file_type != relocatable_file_type) 5555 { 5556 size_t nphdr; 5557 XElf_Addr addr; 5558 struct output_segment *segment; 5559 Elf_Scn *scn; 5560 Elf32_Word nsec; 5561 XElf_Phdr_vardef (phdr); 5562 5563 /* Every executable needs a program header. The number of entries 5564 varies. One exists for each segment. Each SHT_NOTE section gets 5565 one, too. For dynamically linked executables we have to create 5566 one for the program header, the interpreter, and the dynamic 5567 section. First count the number of segments. 5568 5569 XXX Determine whether the segment is non-empty. */ 5570 nphdr = 0; 5571 segment = ld_state.output_segments; 5572 while (segment != NULL) 5573 { 5574 ++nphdr; 5575 segment = segment->next; 5576 } 5577 5578 /* Add the number of SHT_NOTE sections. We counted them earlier. */ 5579 nphdr += ld_state.nnotesections; 5580 5581 /* If we create a DSO or the file is linked against DSOs we have three 5582 more entries: INTERP, PHDR, DYNAMIC. */ 5583 if (dynamically_linked_p ()) 5584 nphdr += 3; 5585 5586 /* Create the program header structure. */ 5587 if (xelf_newphdr (ld_state.outelf, nphdr) == 0) 5588 error (EXIT_FAILURE, 0, gettext ("cannot create program header: %s"), 5589 elf_errmsg (-1)); 5590 5591 5592 /* Determine the section sizes and offsets. We have to do this 5593 to be able to determine the memory layout (which normally 5594 differs from the file layout). */ 5595 if (elf_update (ld_state.outelf, ELF_C_NULL) == -1) 5596 error (EXIT_FAILURE, 0, gettext ("while determining file layout: %s"), 5597 elf_errmsg (-1)); 5598 5599 5600 /* Now determine the memory addresses of all the sections and 5601 segments. */ 5602 nsec = 0; 5603 scn = elf_getscn (ld_state.outelf, ld_state.allsections[nsec]->scnidx); 5604 xelf_getshdr (scn, shdr); 5605 assert (shdr != NULL); 5606 5607 /* The address we start with is the offset of the first (not 5608 zeroth) section. */ 5609 addr = shdr->sh_offset; 5610 5611 /* The index of the first loadable segment. */ 5612 nphdr = 1 + (dynamically_linked_p () == true) * 2; 5613 5614 segment = ld_state.output_segments; 5615 while (segment != NULL) 5616 { 5617 struct output_rule *orule; 5618 bool first_section = true; 5619 XElf_Off nobits_size = 0; 5620 XElf_Off memsize = 0; 5621 5622 /* the minimum alignment is a page size. */ 5623 segment->align = ld_state.pagesize; 5624 5625 for (orule = segment->output_rules; orule != NULL; 5626 orule = orule->next) 5627 if (orule->tag == output_section) 5628 { 5629 XElf_Off oldoff; 5630 5631 /* See whether this output rule corresponds to the next 5632 section. Yes, this is a pointer comparison. */ 5633 if (ld_state.allsections[nsec]->name 5634 != orule->val.section.name) 5635 /* No, ignore this output rule. */ 5636 continue; 5637 5638 /* We assign addresses only in segments which are actually 5639 loaded. */ 5640 if (segment->mode != 0) 5641 { 5642 /* Adjust the offset of the input sections. */ 5643 struct scninfo *isect; 5644 struct scninfo *first; 5645 5646 isect = first = ld_state.allsections[nsec]->last; 5647 if (isect != NULL) 5648 do 5649 isect->offset += addr; 5650 while ((isect = isect->next) != first); 5651 5652 /* Set the address of current section. */ 5653 shdr->sh_addr = addr; 5654 5655 /* Write the result back. */ 5656 (void) xelf_update_shdr (scn, shdr); 5657 5658 /* Remember the address. */ 5659 ld_state.allsections[nsec]->addr = addr; 5660 } 5661 5662 if (first_section) 5663 { 5664 /* The first segment starts at offset zero. */ 5665 if (segment == ld_state.output_segments) 5666 { 5667 segment->offset = 0; 5668 segment->addr = addr - shdr->sh_offset; 5669 } 5670 else 5671 { 5672 segment->offset = shdr->sh_offset; 5673 segment->addr = addr; 5674 } 5675 5676 /* Determine the maximum alignment requirement. */ 5677 segment->align = MAX (segment->align, shdr->sh_addralign); 5678 5679 first_section = false; 5680 } 5681 5682 memsize = shdr->sh_offset - segment->offset + shdr->sh_size; 5683 if (nobits_size != 0 && shdr->sh_type != SHT_NOTE) 5684 error (EXIT_FAILURE, 0, gettext ("\ 5685internal error: nobits section follows nobits section")); 5686 if (shdr->sh_type == SHT_NOBITS) 5687 nobits_size += shdr->sh_size; 5688 5689 /* Determine the new address which is computed using 5690 the difference of the offsets on the sections. Note 5691 that this assumes that the sections following each 5692 other in the section header table are also 5693 consecutive in the file. This is true here because 5694 libelf constructs files this way. */ 5695 oldoff = shdr->sh_offset; 5696 5697 if (++nsec >= ld_state.nallsections) 5698 break; 5699 5700 scn = elf_getscn (ld_state.outelf, 5701 ld_state.allsections[nsec]->scnidx); 5702 xelf_getshdr (scn, shdr); 5703 assert (shdr != NULL); 5704 5705 /* This is the new address resulting from the offsets 5706 in the file. */ 5707 assert (oldoff <= shdr->sh_offset); 5708 addr += shdr->sh_offset - oldoff; 5709 } 5710 else 5711 { 5712 assert (orule->tag == output_assignment); 5713 5714 if (strcmp (orule->val.assignment->variable, ".") == 0) 5715 /* This is a change of the address. */ 5716 addr = eval_expression (orule->val.assignment->expression, 5717 addr); 5718 else if (orule->val.assignment->sym != NULL) 5719 { 5720 /* This symbol is used. Update the symbol table 5721 entry. */ 5722 XElf_Sym_vardef (sym); 5723 size_t idx; 5724 5725 /* Note that we do not have to use 5726 xelf_getsymshndx since we only update the 5727 symbol address, not the section 5728 information. */ 5729 idx = dblindirect[orule->val.assignment->sym->outsymidx]; 5730 xelf_getsym (symdata, idx, sym); 5731 sym->st_value = addr; 5732 (void) xelf_update_sym (symdata, idx, sym); 5733 5734 idx = orule->val.assignment->sym->outdynsymidx; 5735 if (idx != 0) 5736 { 5737 assert (dynsymdata != NULL); 5738 xelf_getsym (dynsymdata, idx, sym); 5739 sym->st_value = addr; 5740 (void) xelf_update_sym (dynsymdata, idx, sym); 5741 } 5742 } 5743 } 5744 5745 /* Store the segment parameter for loadable segments. */ 5746 if (segment->mode != 0) 5747 { 5748 xelf_getphdr_ptr (ld_state.outelf, nphdr, phdr); 5749 5750 phdr->p_type = PT_LOAD; 5751 phdr->p_offset = segment->offset; 5752 phdr->p_vaddr = segment->addr; 5753 phdr->p_paddr = phdr->p_vaddr; 5754 phdr->p_filesz = memsize - nobits_size; 5755 phdr->p_memsz = memsize; 5756 phdr->p_flags = segment->mode; 5757 phdr->p_align = segment->align; 5758 5759 (void) xelf_update_phdr (ld_state.outelf, nphdr, phdr); 5760 ++nphdr; 5761 } 5762 5763 segment = segment->next; 5764 } 5765 5766 /* Create the other program header entries. */ 5767 xelf_getehdr (ld_state.outelf, ehdr); 5768 assert (ehdr != NULL); 5769 5770 xelf_getphdr_ptr (ld_state.outelf, 1, phdr); 5771 phdr->p_type = PT_PHDR; 5772 phdr->p_offset = ehdr->e_phoff; 5773 phdr->p_vaddr = ld_state.output_segments->addr + phdr->p_offset; 5774 phdr->p_paddr = phdr->p_vaddr; 5775 phdr->p_filesz = ehdr->e_phnum * ehdr->e_phentsize; 5776 phdr->p_memsz = phdr->p_filesz; 5777 phdr->p_flags = 0; /* No need to set PF_R or so. */ 5778 phdr->p_align = xelf_fsize (ld_state.outelf, ELF_T_ADDR, 1); 5779 (void) xelf_update_phdr (ld_state.outelf, 0, phdr); 5780 5781 5782 /* Adjust the addresses in the addresses of the symbol according 5783 to the load addresses of the sections. */ 5784 if (ld_state.need_symtab) 5785 for (cnt = 1; cnt < nsym; ++cnt) 5786 { 5787 XElf_Sym_vardef (sym); 5788 Elf32_Word shndx; 5789 5790 xelf_getsymshndx (symdata, xndxdata, cnt, sym, shndx); 5791 assert (sym != NULL); 5792 5793 if (sym->st_shndx != SHN_XINDEX) 5794 shndx = sym->st_shndx; 5795 5796 if ((shndx > SHN_UNDEF && shndx < SHN_LORESERVE) 5797 || shndx > SHN_HIRESERVE) 5798 { 5799 /* Note we subtract 1 from the section index since ALLSECTIONS 5800 does not store the dummy section with offset zero. */ 5801 sym->st_value += ld_state.allsections[shndx - 1]->addr; 5802 5803 /* We don't have to use 'xelf_update_symshndx' since the 5804 section number doesn't change. */ 5805 (void) xelf_update_sym (symdata, cnt, sym); 5806 } 5807 } 5808 5809 if (ld_state.need_dynsym) 5810 for (cnt = 1; cnt < nsym_dyn; ++cnt) 5811 { 5812 XElf_Sym_vardef (sym); 5813 5814 xelf_getsym (dynsymdata, cnt, sym); 5815 assert (sym != NULL); 5816 5817 if (sym->st_shndx > SHN_UNDEF && sym->st_shndx < SHN_LORESERVE) 5818 { 5819 /* Note we subtract 1 from the section index since ALLSECTIONS 5820 does not store the dummy section with offset zero. */ 5821 sym->st_value += ld_state.allsections[sym->st_shndx - 1]->addr; 5822 5823 /* We don't have to use 'xelf_update_symshndx' since the 5824 section number doesn't change. */ 5825 (void) xelf_update_sym (dynsymdata, cnt, sym); 5826 } 5827 } 5828 5829 5830 /* Now is a good time to determine the values of all the symbols 5831 we encountered. */ 5832 // XXX This loop is very inefficient. The hash tab iterator also 5833 // returns all symbols in DSOs. 5834 struct symbol *se; 5835 void *p = NULL; 5836 while ((se = ld_symbol_tab_iterate (&ld_state.symbol_tab, &p)) != NULL) 5837 if (! se->in_dso) 5838 { 5839 XElf_Sym_vardef (sym); 5840 5841 addr = 0; 5842 5843 if (se->outdynsymidx != 0) 5844 { 5845 xelf_getsym (dynsymdata, se->outdynsymidx, sym); 5846 assert (sym != NULL); 5847 addr = sym->st_value; 5848 } 5849 else if (se->outsymidx != 0) 5850 { 5851 assert (dblindirect[se->outsymidx] != 0); 5852 xelf_getsym (symdata, dblindirect[se->outsymidx], sym); 5853 assert (sym != NULL); 5854 addr = sym->st_value; 5855 } 5856 else 5857 abort (); 5858 5859 se->merge.value = addr; 5860 } 5861 5862 /* Complete the header of the .rel.dyn/.rela.dyn section. Point 5863 to the symbol table. The sh_info field is left zero since 5864 there is no specific section the contained relocations are 5865 for. */ 5866 if (ld_state.reldynscnidx != 0) 5867 { 5868 assert (ld_state.dynsymscnidx != 0); 5869 scn = elf_getscn (ld_state.outelf, ld_state.reldynscnidx); 5870 xelf_getshdr (scn, shdr); 5871 assert (shdr != NULL); 5872 5873 shdr->sh_link = ld_state.dynsymscnidx; 5874 5875 (void) xelf_update_shdr (scn, shdr); 5876 } 5877 5878 /* Fill in the dynamic segment/section. */ 5879 if (dynamically_linked_p ()) 5880 { 5881 Elf_Scn *outscn; 5882 5883 assert (ld_state.interpscnidx != 0); 5884 xelf_getshdr (elf_getscn (ld_state.outelf, ld_state.interpscnidx), 5885 shdr); 5886 assert (shdr != NULL); 5887 5888 /* The interpreter string. */ 5889 // XXX Do we need to support files (DSOs) without interpreters? 5890 xelf_getphdr_ptr (ld_state.outelf, 1, phdr); 5891 phdr->p_type = PT_INTERP; 5892 phdr->p_offset = shdr->sh_offset; 5893 phdr->p_vaddr = shdr->sh_addr; 5894 phdr->p_paddr = phdr->p_vaddr; 5895 phdr->p_filesz = shdr->sh_size; 5896 phdr->p_memsz = phdr->p_filesz; 5897 phdr->p_flags = 0; /* No need to set PF_R or so. */ 5898 phdr->p_align = 1; /* It's a string. */ 5899 5900 (void) xelf_update_phdr (ld_state.outelf, 1, phdr); 5901 5902 /* The pointer to the dynamic section. We this we need to 5903 get the information for the dynamic section first. */ 5904 assert (ld_state.dynamicscnidx); 5905 outscn = elf_getscn (ld_state.outelf, ld_state.dynamicscnidx); 5906 xelf_getshdr (outscn, shdr); 5907 assert (shdr != NULL); 5908 5909 xelf_getphdr_ptr (ld_state.outelf, 2, phdr); 5910 phdr->p_type = PT_DYNAMIC; 5911 phdr->p_offset = shdr->sh_offset; 5912 phdr->p_vaddr = shdr->sh_addr; 5913 phdr->p_paddr = phdr->p_vaddr; 5914 phdr->p_filesz = shdr->sh_size; 5915 phdr->p_memsz = phdr->p_filesz; 5916 phdr->p_flags = 0; /* No need to set PF_R or so. */ 5917 phdr->p_align = shdr->sh_addralign; 5918 5919 (void) xelf_update_phdr (ld_state.outelf, 2, phdr); 5920 5921 /* Fill in the reference to the .dynstr section. */ 5922 assert (ld_state.dynstrscnidx != 0); 5923 shdr->sh_link = ld_state.dynstrscnidx; 5924 (void) xelf_update_shdr (outscn, shdr); 5925 5926 /* And fill the remaining entries. */ 5927 Elf_Data *dyndata = elf_getdata (outscn, NULL); 5928 assert (dyndata != NULL); 5929 5930 /* Add the DT_NEEDED entries. */ 5931 if (ld_state.ndsofiles > 0) 5932 { 5933 struct usedfiles *runp = ld_state.dsofiles->next; 5934 5935 do 5936 if (! ld_state.ignore_unused_dsos || runp->used) 5937 { 5938 /* Add the position-dependent flag if necessary. */ 5939 if (runp->lazyload) 5940 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 5941 DT_POSFLAG_1, DF_P1_LAZYLOAD); 5942 5943 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 5944 DT_NEEDED, 5945 ebl_strtaboffset (runp->sonameent)); 5946 } 5947 while ((runp = runp->next) != ld_state.dsofiles->next); 5948 } 5949 5950 /* We can finish the DT_RUNPATH/DT_RPATH entries now. */ 5951 if (ld_state.rxxpath_strent != NULL) 5952 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 5953 ld_state.rxxpath_tag, 5954 ebl_strtaboffset (ld_state.rxxpath_strent)); 5955 5956 /* Reference to initialization and finalization functions. */ 5957 // XXX This code depends on symbol table being relocated. 5958 if (ld_state.init_symbol != NULL) 5959 { 5960 XElf_Sym_vardef (sym); 5961 5962 if (ld_state.need_symtab) 5963 xelf_getsym (symdata, 5964 dblindirect[ld_state.init_symbol->outsymidx], 5965 sym); 5966 else 5967 xelf_getsym (dynsymdata, ld_state.init_symbol->outdynsymidx, 5968 sym); 5969 assert (sym != NULL); 5970 5971 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 5972 DT_INIT, sym->st_value); 5973 } 5974 if (ld_state.fini_symbol != NULL) 5975 { 5976 XElf_Sym_vardef (sym); 5977 5978 if (ld_state.need_symtab) 5979 xelf_getsym (symdata, 5980 dblindirect[ld_state.fini_symbol->outsymidx], 5981 sym); 5982 else 5983 xelf_getsym (dynsymdata, ld_state.fini_symbol->outdynsymidx, 5984 sym); 5985 assert (sym != NULL); 5986 5987 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 5988 DT_FINI, sym->st_value); 5989 } 5990 // XXX Support init,fini,preinit arrays 5991 5992 /* The hash table which comes with dynamic symbol table. */ 5993 xelf_getshdr (elf_getscn (ld_state.outelf, ld_state.hashscnidx), 5994 shdr); 5995 assert (shdr != NULL); 5996 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, DT_HASH, 5997 shdr->sh_addr); 5998 5999 /* Reference to the symbol table section. */ 6000 assert (ld_state.dynsymscnidx != 0); 6001 xelf_getshdr (elf_getscn (ld_state.outelf, ld_state.dynsymscnidx), 6002 shdr); 6003 assert (shdr != NULL); 6004 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, DT_SYMTAB, 6005 shdr->sh_addr); 6006 6007 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, DT_SYMENT, 6008 xelf_fsize (ld_state.outelf, ELF_T_SYM, 1)); 6009 6010 /* And the string table which comes with it. */ 6011 xelf_getshdr (elf_getscn (ld_state.outelf, ld_state.dynstrscnidx), 6012 shdr); 6013 assert (shdr != NULL); 6014 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, DT_STRTAB, 6015 shdr->sh_addr); 6016 6017 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, DT_STRSZ, 6018 shdr->sh_size); 6019 6020 /* Add the entries related to the .plt. */ 6021 if (ld_state.nplt > 0) 6022 { 6023 xelf_getshdr (elf_getscn (ld_state.outelf, ld_state.gotscnidx), 6024 shdr); 6025 assert (shdr != NULL); 6026 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 6027 // XXX This should probably be machine 6028 // dependent. 6029 DT_PLTGOT, shdr->sh_addr); 6030 6031 xelf_getshdr (elf_getscn (ld_state.outelf, 6032 ld_state.pltrelscnidx), shdr); 6033 assert (shdr != NULL); 6034 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 6035 DT_PLTRELSZ, shdr->sh_size); 6036 6037 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 6038 DT_JMPREL, shdr->sh_addr); 6039 6040 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 6041 DT_PLTREL, REL_TYPE (statep)); 6042 } 6043 6044 if (ld_state.relsize_total > 0) 6045 { 6046 int rel = REL_TYPE (statep); 6047 xelf_getshdr (elf_getscn (ld_state.outelf, 6048 ld_state.reldynscnidx), shdr); 6049 assert (shdr != NULL); 6050 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 6051 rel, shdr->sh_addr); 6052 6053 /* Trick ahead. Use arithmetic to get the right tag. 6054 We check the validity of this assumption in the asserts. */ 6055 assert (DT_RELASZ - DT_RELA == 1); 6056 assert (DT_RELSZ - DT_REL == 1); 6057 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 6058 rel + 1, shdr->sh_size); 6059 6060 /* Similar for the entry size tag. */ 6061 assert (DT_RELAENT - DT_RELA == 2); 6062 assert (DT_RELENT - DT_REL == 2); 6063 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 6064 rel + 2, 6065 rel == DT_REL 6066 ? xelf_fsize (ld_state.outelf, ELF_T_REL, 1) 6067 : xelf_fsize (ld_state.outelf, ELF_T_RELA, 6068 1)); 6069 } 6070 6071 if (ld_state.verneedscnidx != 0) 6072 { 6073 xelf_getshdr (elf_getscn (ld_state.outelf, 6074 ld_state.verneedscnidx), shdr); 6075 assert (shdr != NULL); 6076 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 6077 DT_VERNEED, shdr->sh_addr); 6078 6079 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 6080 DT_VERNEEDNUM, ld_state.nverdeffile); 6081 } 6082 6083 if (ld_state.versymscnidx != 0) 6084 { 6085 xelf_getshdr (elf_getscn (ld_state.outelf, 6086 ld_state.versymscnidx), shdr); 6087 assert (shdr != NULL); 6088 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 6089 DT_VERSYM, shdr->sh_addr); 6090 } 6091 6092 /* We always create the DT_DEBUG entry. */ 6093 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, DT_DEBUG, 0); 6094 assert (ld_state.ndynamic_filled < ld_state.ndynamic); 6095 6096 /* Add the flag words if necessary. */ 6097 if (ld_state.dt_flags != 0) 6098 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, DT_FLAGS, 6099 ld_state.dt_flags); 6100 6101 /* Create entry for the DT_FLAGS_1 flag. */ 6102 if (ld_state.dt_flags_1 != 0) 6103 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 6104 DT_FLAGS_1, ld_state.dt_flags_1); 6105 6106 /* Create entry for the DT_FEATURE_1 flag. */ 6107 if (ld_state.dt_feature_1 != 0) 6108 new_dynamic_entry (dyndata, ld_state.ndynamic_filled++, 6109 DT_FEATURE_1, ld_state.dt_feature_1); 6110 6111 assert (ld_state.ndynamic_filled <= ld_state.ndynamic); 6112 } 6113 } 6114 6115 6116 // XXX The following code isn't nice. We use two different 6117 // mechanisms to handle relocations, one for relocatable files, one 6118 // for executables and DSOs. Maybe this is the best method but also 6119 // maybe it can be somewhat unified. 6120 6121 /* Now that we created the symbol table we can add the reference to 6122 it in the sh_link field of the section headers of the relocation 6123 sections. */ 6124 while (rellist != NULL) 6125 { 6126 assert (ld_state.file_type == relocatable_file_type); 6127 Elf_Scn *outscn; 6128 6129 outscn = elf_getscn (ld_state.outelf, rellist->scnidx); 6130 xelf_getshdr (outscn, shdr); 6131 /* This must not fail since we did it before. */ 6132 assert (shdr != NULL); 6133 6134 /* Remember the symbol table which belongs to the relocation section. */ 6135 shdr->sh_link = ld_state.symscnidx; 6136 6137 /* And the reference to the section which is relocated by this 6138 relocation section. We use the info from the first input 6139 section but all records should have the same information. */ 6140 shdr->sh_info = 6141 rellist->scninfo->fileinfo->scninfo[SCNINFO_SHDR (rellist->scninfo->shdr).sh_info].outscnndx; 6142 6143 6144 /* Perform the actual relocations. We only have to adjust 6145 offsets and symbol indices. */ 6146 RELOCATE_SECTION (statep, outscn, rellist->scninfo, dblindirect); 6147 6148 /* Store the changes. */ 6149 (void) xelf_update_shdr (outscn, shdr); 6150 6151 /* Up to the next relocation section. */ 6152 rellist = rellist->next; 6153 } 6154 6155 if (ld_state.rellist != NULL) 6156 { 6157 assert (ld_state.file_type != relocatable_file_type); 6158 /* Create the relocations for the output file. */ 6159 CREATE_RELOCATIONS (statep, dblindirect); 6160 } 6161 6162 6163 /* We need the ELF header once more. */ 6164 xelf_getehdr (ld_state.outelf, ehdr); 6165 assert (ehdr != NULL); 6166 6167 /* Set the section header string table index. */ 6168 if (likely (shstrtab_ndx < SHN_HIRESERVE) 6169 && likely (shstrtab_ndx != SHN_XINDEX)) 6170 ehdr->e_shstrndx = shstrtab_ndx; 6171 else 6172 { 6173 /* We have to put the section index in the sh_link field of the 6174 zeroth section header. */ 6175 Elf_Scn *scn = elf_getscn (ld_state.outelf, 0); 6176 6177 xelf_getshdr (scn, shdr); 6178 if (unlikely (shdr == NULL)) 6179 error (EXIT_FAILURE, 0, 6180 gettext ("cannot get header of 0th section: %s"), 6181 elf_errmsg (-1)); 6182 6183 shdr->sh_link = shstrtab_ndx; 6184 6185 (void) xelf_update_shdr (scn, shdr); 6186 6187 ehdr->e_shstrndx = SHN_XINDEX; 6188 } 6189 6190 if (ld_state.file_type != relocatable_file_type) 6191 /* DSOs and executables have to define the entry point symbol. */ 6192 ehdr->e_entry = find_entry_point (); 6193 6194 if (unlikely (xelf_update_ehdr (ld_state.outelf, ehdr) == 0)) 6195 error (EXIT_FAILURE, 0, 6196 gettext ("cannot update ELF header: %s"), 6197 elf_errmsg (-1)); 6198 6199 6200 /* Free the data which we don't need anymore. */ 6201 free (ld_state.dblindirect); 6202 6203 6204 /* Finalize the .plt section the what belongs to them. */ 6205 FINALIZE_PLT (statep, nsym, nsym_dyn); 6206 6207 return 0; 6208} 6209 6210 6211/* This is a function which must be specified in all backends. */ 6212static void 6213ld_generic_relocate_section (struct ld_state *statep, Elf_Scn *outscn, 6214 struct scninfo *firstp, 6215 const Elf32_Word *dblindirect) 6216{ 6217 error (EXIT_FAILURE, 0, gettext ("\ 6218linker backend didn't specify function to relocate section")); 6219 /* NOTREACHED */ 6220} 6221 6222 6223/* Finalize the output file. */ 6224static int 6225ld_generic_finalize (struct ld_state *statep) 6226{ 6227 /* Write out the ELF file data. */ 6228 if (elf_update (ld_state.outelf, ELF_C_WRITE) == -1) 6229 error (EXIT_FAILURE, 0, gettext ("while writing output file: %s"), 6230 elf_errmsg (-1)); 6231 6232 /* Free the resources. */ 6233 if (elf_end (ld_state.outelf) != 0) 6234 error (EXIT_FAILURE, 0, gettext ("while finishing output file: %s"), 6235 elf_errmsg (-1)); 6236 6237 /* Get the file status of the temporary file. */ 6238 struct stat temp_st; 6239 if (fstat (ld_state.outfd, &temp_st) != 0) 6240 error (EXIT_FAILURE, errno, gettext ("cannot stat output file")); 6241 6242 /* Now it's time to rename the file. Remove an old existing file 6243 first. */ 6244 if (rename (ld_state.tempfname, ld_state.outfname) != 0) 6245 /* Something went wrong. */ 6246 error (EXIT_FAILURE, errno, gettext ("cannot rename output file")); 6247 6248 /* Make sure the output file is really the one we created. */ 6249 struct stat new_st; 6250 if (stat (ld_state.outfname, &new_st) != 0 6251 || new_st.st_ino != temp_st.st_ino 6252 || new_st.st_dev != temp_st.st_dev) 6253 { 6254 /* Wow, somebody overwrote the output file, probably some intruder. */ 6255 unlink (ld_state.outfname); 6256 error (EXIT_FAILURE, 0, gettext ("\ 6257WARNING: temporary output file overwritten before linking finished")); 6258 } 6259 6260 /* Close the file descriptor. */ 6261 (void) close (ld_state.outfd); 6262 6263 /* Signal the cleanup handler that the file is correctly created. */ 6264 ld_state.tempfname = NULL; 6265 6266 return 0; 6267} 6268 6269 6270static bool 6271ld_generic_special_section_number_p (struct ld_state *statep, size_t number) 6272{ 6273 /* There are no special section numbers in the gABI. */ 6274 return false; 6275} 6276 6277 6278static bool 6279ld_generic_section_type_p (struct ld_state *statep, GElf_Word type) 6280{ 6281 if ((type >= SHT_NULL && type < SHT_NUM) 6282 /* XXX Enable the following two when implemented. */ 6283 // || type == SHT_GNU_LIBLIST 6284 // || type == SHT_CHECKSUM 6285 /* XXX Eventually include SHT_SUNW_move, SHT_SUNW_COMDAT, and 6286 SHT_SUNW_syminfo. */ 6287 || (type >= SHT_GNU_verdef && type <= SHT_GNU_versym)) 6288 return true; 6289 6290 return false; 6291} 6292 6293 6294static XElf_Xword 6295ld_generic_dynamic_section_flags (struct ld_state *statep) 6296{ 6297 /* By default the .dynamic section is writable (and is of course 6298 loaded). Few architecture differ from this. */ 6299 return SHF_ALLOC | SHF_WRITE; 6300} 6301 6302 6303static void 6304ld_generic_initialize_plt (struct ld_state *statep, Elf_Scn *scn) 6305{ 6306 /* This cannot be implemented generally. There should have been a 6307 machine dependent implementation and we should never have arrived 6308 here. */ 6309 error (EXIT_FAILURE, 0, gettext ("no machine specific '%s' implementation"), 6310 "initialize_plt"); 6311} 6312 6313 6314static void 6315ld_generic_initialize_pltrel (struct ld_state *statep, Elf_Scn *scn) 6316{ 6317 /* This cannot be implemented generally. There should have been a 6318 machine dependent implementation and we should never have arrived 6319 here. */ 6320 error (EXIT_FAILURE, 0, gettext ("no machine specific '%s' implementation"), 6321 "initialize_pltrel"); 6322} 6323 6324 6325static void 6326ld_generic_initialize_got (struct ld_state *statep, Elf_Scn *scn) 6327{ 6328 /* This cannot be implemented generally. There should have been a 6329 machine dependent implementation and we should never have arrived 6330 here. */ 6331 error (EXIT_FAILURE, 0, gettext ("no machine specific '%s' implementation"), 6332 "initialize_got"); 6333} 6334 6335 6336static void 6337ld_generic_finalize_plt (struct ld_state *statep, size_t nsym, size_t nsym_dyn) 6338{ 6339 /* By default we assume that nothing has to be done. */ 6340} 6341 6342 6343static int 6344ld_generic_rel_type (struct ld_state *statep) 6345{ 6346 /* This cannot be implemented generally. There should have been a 6347 machine dependent implementation and we should never have arrived 6348 here. */ 6349 error (EXIT_FAILURE, 0, gettext ("no machine specific '%s' implementation"), 6350 "rel_type"); 6351 /* Just to keep the compiler calm. */ 6352 return 0; 6353} 6354 6355 6356static void 6357ld_generic_count_relocations (struct ld_state *statep, struct scninfo *scninfo) 6358{ 6359 /* This cannot be implemented generally. There should have been a 6360 machine dependent implementation and we should never have arrived 6361 here. */ 6362 error (EXIT_FAILURE, 0, gettext ("no machine specific '%s' implementation"), 6363 "count_relocations"); 6364} 6365 6366 6367static void 6368ld_generic_create_relocations (struct ld_state *statep, 6369 const Elf32_Word *dblindirect) 6370{ 6371 /* This cannot be implemented generally. There should have been a 6372 machine dependent implementation and we should never have arrived 6373 here. */ 6374 error (EXIT_FAILURE, 0, gettext ("no machine specific '%s' implementation"), 6375 "create_relocations"); 6376} 6377