1 /* Combine stripped files with separate symbols and debug information. 2 Copyright (C) 2007-2012, 2014, 2015 Red Hat, Inc. 3 This file is part of elfutils. 4 Written by Roland McGrath <roland (at) redhat.com>, 2007. 5 6 This file is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 elfutils is distributed in the hope that it will be useful, but 12 WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18 19 /* TODO: 20 21 * SHX_XINDEX 22 23 * prelink vs .debug_* linked addresses 24 25 */ 26 27 #ifdef HAVE_CONFIG_H 28 # include <config.h> 29 #endif 30 31 #include <argp.h> 32 #include <assert.h> 33 #include <errno.h> 34 #include <fcntl.h> 35 #include <fnmatch.h> 36 #include <libintl.h> 37 #include <locale.h> 38 #include <stdbool.h> 39 #include <stdio.h> 40 #include <stdio_ext.h> 41 #include <inttypes.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 #include <sys/stat.h> 46 47 #include <gelf.h> 48 #include <libebl.h> 49 #include <libdwfl.h> 50 #include "system.h" 51 #include "libdwelf.h" 52 #include "libeu.h" 53 #include "printversion.h" 54 55 #ifndef _ 56 # define _(str) gettext (str) 57 #endif 58 59 /* Name and version of program. */ 60 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version; 61 62 /* Bug report address. */ 63 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT; 64 65 /* Definitions of arguments for argp functions. */ 66 static const struct argp_option options[] = 67 { 68 /* Group 2 will follow group 1 from dwfl_standard_argp. */ 69 { "match-file-names", 'f', NULL, 0, 70 N_("Match MODULE against file names, not module names"), 2 }, 71 { "ignore-missing", 'i', NULL, 0, N_("Silently skip unfindable files"), 0 }, 72 73 { NULL, 0, NULL, 0, N_("Output options:"), 0 }, 74 { "output", 'o', "FILE", 0, N_("Place output into FILE"), 0 }, 75 { "output-directory", 'd', "DIRECTORY", 76 0, N_("Create multiple output files under DIRECTORY"), 0 }, 77 { "module-names", 'm', NULL, 0, N_("Use module rather than file names"), 0 }, 78 { "all", 'a', NULL, 0, 79 N_("Create output for modules that have no separate debug information"), 80 0 }, 81 { "relocate", 'R', NULL, 0, 82 N_("Apply relocations to section contents in ET_REL files"), 0 }, 83 { "list-only", 'n', NULL, 0, 84 N_("Only list module and file names, build IDs"), 0 }, 85 { "force", 'F', NULL, 0, 86 N_("Force combining files even if some ELF headers don't seem to match"), 87 0 }, 88 { NULL, 0, NULL, 0, NULL, 0 } 89 }; 90 91 struct arg_info 92 { 93 const char *output_file; 94 const char *output_dir; 95 Dwfl *dwfl; 96 char **args; 97 bool list; 98 bool all; 99 bool ignore; 100 bool modnames; 101 bool match_files; 102 bool relocate; 103 bool force; 104 }; 105 106 /* Handle program arguments. */ 107 static error_t 108 parse_opt (int key, char *arg, struct argp_state *state) 109 { 110 struct arg_info *info = state->input; 111 112 switch (key) 113 { 114 case ARGP_KEY_INIT: 115 state->child_inputs[0] = &info->dwfl; 116 break; 117 118 case 'o': 119 if (info->output_file != NULL) 120 { 121 argp_error (state, _("-o option specified twice")); 122 return EINVAL; 123 } 124 info->output_file = arg; 125 break; 126 127 case 'd': 128 if (info->output_dir != NULL) 129 { 130 argp_error (state, _("-d option specified twice")); 131 return EINVAL; 132 } 133 info->output_dir = arg; 134 break; 135 136 case 'm': 137 info->modnames = true; 138 break; 139 case 'f': 140 info->match_files = true; 141 break; 142 case 'a': 143 info->all = true; 144 break; 145 case 'i': 146 info->ignore = true; 147 break; 148 case 'n': 149 info->list = true; 150 break; 151 case 'R': 152 info->relocate = true; 153 break; 154 case 'F': 155 info->force = true; 156 break; 157 158 case ARGP_KEY_ARGS: 159 case ARGP_KEY_NO_ARGS: 160 /* We "consume" all the arguments here. */ 161 info->args = &state->argv[state->next]; 162 163 if (info->output_file != NULL && info->output_dir != NULL) 164 { 165 argp_error (state, _("only one of -o or -d allowed")); 166 return EINVAL; 167 } 168 169 if (info->list && (info->dwfl == NULL 170 || info->output_dir != NULL 171 || info->output_file != NULL)) 172 { 173 argp_error (state, 174 _("-n cannot be used with explicit files or -o or -d")); 175 return EINVAL; 176 } 177 178 if (info->output_dir != NULL) 179 { 180 struct stat st; 181 error_t fail = 0; 182 if (stat (info->output_dir, &st) < 0) 183 fail = errno; 184 else if (!S_ISDIR (st.st_mode)) 185 fail = ENOTDIR; 186 if (fail) 187 { 188 argp_failure (state, EXIT_FAILURE, fail, 189 _("output directory '%s'"), info->output_dir); 190 return fail; 191 } 192 } 193 194 if (info->dwfl == NULL) 195 { 196 if (state->next + 2 != state->argc) 197 { 198 argp_error (state, _("exactly two file arguments are required")); 199 return EINVAL; 200 } 201 202 if (info->ignore || info->all || info->modnames || info->relocate) 203 { 204 argp_error (state, _("\ 205 -m, -a, -R, and -i options not allowed with explicit files")); 206 return EINVAL; 207 } 208 209 /* Bail out immediately to prevent dwfl_standard_argp's parser 210 from defaulting to "-e a.out". */ 211 return ENOSYS; 212 } 213 else if (info->output_file == NULL && info->output_dir == NULL 214 && !info->list) 215 { 216 argp_error (state, 217 _("-o or -d is required when using implicit files")); 218 return EINVAL; 219 } 220 break; 221 222 default: 223 return ARGP_ERR_UNKNOWN; 224 } 225 return 0; 226 } 227 228 #define ELF_CHECK(call, msg) \ 230 do \ 231 { \ 232 if (unlikely (!(call))) \ 233 error (EXIT_FAILURE, 0, msg, elf_errmsg (-1)); \ 234 } while (0) 235 236 /* Copy INELF to newly-created OUTELF, exit via error for any problems. */ 237 static void 238 copy_elf (Elf *outelf, Elf *inelf) 239 { 240 ELF_CHECK (gelf_newehdr (outelf, gelf_getclass (inelf)), 241 _("cannot create ELF header: %s")); 242 243 size_t shstrndx; 244 ELF_CHECK (elf_getshdrstrndx (inelf, &shstrndx) == 0, 245 _("cannot get shdrstrndx:%s")); 246 247 GElf_Ehdr ehdr_mem; 248 GElf_Ehdr *ehdr = gelf_getehdr (inelf, &ehdr_mem); 249 ELF_CHECK (ehdr != NULL, _("cannot get ELF header: %s")); 250 if (shstrndx < SHN_LORESERVE) 251 ehdr->e_shstrndx = shstrndx; 252 else 253 { 254 ehdr->e_shstrndx = SHN_XINDEX; 255 Elf_Scn *scn0 = elf_getscn (outelf, 0); 256 GElf_Shdr shdr0_mem; 257 GElf_Shdr *shdr0 = gelf_getshdr (scn0, &shdr0_mem); 258 ELF_CHECK (shdr0 != NULL, 259 _("cannot get new zero section: %s")); 260 shdr0->sh_link = shstrndx; 261 ELF_CHECK (gelf_update_shdr (scn0, shdr0), 262 _("cannot update new zero section: %s")); 263 } 264 265 ELF_CHECK (gelf_update_ehdr (outelf, ehdr), 266 _("cannot copy ELF header: %s")); 267 268 size_t phnum; 269 ELF_CHECK (elf_getphdrnum (inelf, &phnum) == 0, 270 _("cannot get number of program headers: %s")); 271 272 if (phnum > 0) 273 { 274 ELF_CHECK (gelf_newphdr (outelf, phnum), 275 _("cannot create program headers: %s")); 276 277 GElf_Phdr phdr_mem; 278 for (size_t i = 0; i < phnum; ++i) 279 ELF_CHECK (gelf_update_phdr (outelf, i, 280 gelf_getphdr (inelf, i, &phdr_mem)), 281 _("cannot copy program header: %s")); 282 } 283 284 Elf_Scn *scn = NULL; 285 while ((scn = elf_nextscn (inelf, scn)) != NULL) 286 { 287 Elf_Scn *newscn = elf_newscn (outelf); 288 289 GElf_Shdr shdr_mem; 290 ELF_CHECK (gelf_update_shdr (newscn, gelf_getshdr (scn, &shdr_mem)), 291 _("cannot copy section header: %s")); 292 293 Elf_Data *data = elf_getdata (scn, NULL); 294 ELF_CHECK (data != NULL, _("cannot get section data: %s")); 295 Elf_Data *newdata = elf_newdata (newscn); 296 ELF_CHECK (newdata != NULL, _("cannot copy section data: %s")); 297 *newdata = *data; 298 elf_flagdata (newdata, ELF_C_SET, ELF_F_DIRTY); 299 } 300 } 301 302 /* Create directories containing PATH. */ 303 static void 304 make_directories (const char *path) 305 { 306 const char *lastslash = strrchr (path, '/'); 307 if (lastslash == NULL) 308 return; 309 310 while (lastslash > path && lastslash[-1] == '/') 311 --lastslash; 312 if (lastslash == path) 313 return; 314 315 char *dir = strndupa (path, lastslash - path); 316 while (mkdir (dir, 0777) < 0 && errno != EEXIST) 317 if (errno == ENOENT) 318 make_directories (dir); 319 else 320 error (EXIT_FAILURE, errno, _("cannot create directory '%s'"), dir); 321 } 322 323 /* Keep track of new section data we are creating, so we can free it 324 when done. */ 325 struct data_list 326 { 327 void *data; 328 struct data_list *next; 329 }; 330 331 struct data_list *new_data_list; 332 333 static void 334 record_new_data (void *data) 335 { 336 struct data_list *next = new_data_list; 337 new_data_list = xmalloc (sizeof (struct data_list)); 338 new_data_list->data = data; 339 new_data_list->next = next; 340 } 341 342 static void 343 free_new_data (void) 344 { 345 struct data_list *list = new_data_list; 346 while (list != NULL) 347 { 348 struct data_list *next = list->next; 349 free (list->data); 350 free (list); 351 list = next; 352 } 353 new_data_list = NULL; 354 } 355 356 /* The binutils linker leaves gratuitous section symbols in .symtab 357 that strip has to remove. Older linkers likewise include a 358 symbol for every section, even unallocated ones, in .dynsym. 359 Because of this, the related sections can shrink in the stripped 360 file from their original size. Older versions of strip do not 361 adjust the sh_size field in the debuginfo file's SHT_NOBITS 362 version of the section header, so it can appear larger. */ 363 static bool 364 section_can_shrink (const GElf_Shdr *shdr) 365 { 366 switch (shdr->sh_type) 367 { 368 case SHT_SYMTAB: 369 case SHT_DYNSYM: 370 case SHT_HASH: 371 case SHT_GNU_versym: 372 return true; 373 } 374 return false; 375 } 376 377 /* See if this symbol table has a leading section symbol for every single 378 section, in order. The binutils linker produces this. While we're here, 379 update each section symbol's st_value. */ 380 static size_t 381 symtab_count_leading_section_symbols (Elf *elf, Elf_Scn *scn, size_t shnum, 382 Elf_Data *newsymdata) 383 { 384 Elf_Data *data = elf_getdata (scn, NULL); 385 Elf_Data *shndxdata = NULL; /* XXX */ 386 387 for (size_t i = 1; i < shnum; ++i) 388 { 389 GElf_Sym sym_mem; 390 GElf_Word shndx = SHN_UNDEF; 391 GElf_Sym *sym = gelf_getsymshndx (data, shndxdata, i, &sym_mem, &shndx); 392 ELF_CHECK (sym != NULL, _("cannot get symbol table entry: %s")); 393 394 GElf_Shdr shdr_mem; 395 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (elf, i), &shdr_mem); 396 ELF_CHECK (shdr != NULL, _("cannot get section header: %s")); 397 398 if (sym->st_shndx != SHN_XINDEX) 399 shndx = sym->st_shndx; 400 401 if (shndx != i || GELF_ST_TYPE (sym->st_info) != STT_SECTION) 402 return i; 403 404 sym->st_value = shdr->sh_addr; 405 if (sym->st_shndx != SHN_XINDEX) 406 shndx = SHN_UNDEF; 407 ELF_CHECK (gelf_update_symshndx (newsymdata, shndxdata, i, sym, shndx), 408 _("cannot update symbol table: %s")); 409 } 410 411 return shnum; 412 } 413 414 static void 415 update_shdr (Elf_Scn *outscn, GElf_Shdr *newshdr) 416 { 417 ELF_CHECK (gelf_update_shdr (outscn, newshdr), 418 _("cannot update section header: %s")); 419 } 420 421 /* We expanded the output section, so update its header. */ 422 static void 423 update_sh_size (Elf_Scn *outscn, const Elf_Data *data) 424 { 425 GElf_Shdr shdr_mem; 426 GElf_Shdr *newshdr = gelf_getshdr (outscn, &shdr_mem); 427 ELF_CHECK (newshdr != NULL, _("cannot get section header: %s")); 428 429 newshdr->sh_size = data->d_size; 430 431 update_shdr (outscn, newshdr); 432 } 433 434 /* Update relocation sections using the symbol table. */ 435 static void 436 adjust_relocs (Elf_Scn *outscn, Elf_Scn *inscn, const GElf_Shdr *shdr, 437 size_t map[], const GElf_Shdr *symshdr) 438 { 439 Elf_Data *data = elf_getdata (outscn, NULL); 440 441 inline void adjust_reloc (GElf_Xword *info) 442 { 443 size_t ndx = GELF_R_SYM (*info); 444 if (ndx != STN_UNDEF) 445 *info = GELF_R_INFO (map[ndx - 1], GELF_R_TYPE (*info)); 446 } 447 448 switch (shdr->sh_type) 449 { 450 case SHT_REL: 451 if (shdr->sh_entsize == 0) 452 error (EXIT_FAILURE, 0, "REL section cannot have zero sh_entsize"); 453 454 for (size_t i = 0; i < shdr->sh_size / shdr->sh_entsize; ++i) 455 { 456 GElf_Rel rel_mem; 457 GElf_Rel *rel = gelf_getrel (data, i, &rel_mem); 458 adjust_reloc (&rel->r_info); 459 ELF_CHECK (gelf_update_rel (data, i, rel), 460 _("cannot update relocation: %s")); 461 } 462 break; 463 464 case SHT_RELA: 465 if (shdr->sh_entsize == 0) 466 error (EXIT_FAILURE, 0, "RELA section cannot have zero sh_entsize"); 467 468 for (size_t i = 0; i < shdr->sh_size / shdr->sh_entsize; ++i) 469 { 470 GElf_Rela rela_mem; 471 GElf_Rela *rela = gelf_getrela (data, i, &rela_mem); 472 adjust_reloc (&rela->r_info); 473 ELF_CHECK (gelf_update_rela (data, i, rela), 474 _("cannot update relocation: %s")); 475 } 476 break; 477 478 case SHT_GROUP: 479 { 480 GElf_Shdr shdr_mem; 481 GElf_Shdr *newshdr = gelf_getshdr (outscn, &shdr_mem); 482 ELF_CHECK (newshdr != NULL, _("cannot get section header: %s")); 483 if (newshdr->sh_info != STN_UNDEF) 484 { 485 newshdr->sh_info = map[newshdr->sh_info - 1]; 486 update_shdr (outscn, newshdr); 487 } 488 break; 489 } 490 491 case SHT_HASH: 492 /* We must expand the table and rejigger its contents. */ 493 { 494 if (shdr->sh_entsize == 0) 495 error (EXIT_FAILURE, 0, "HASH section cannot have zero sh_entsize"); 496 if (symshdr->sh_entsize == 0) 497 error (EXIT_FAILURE, 0, "Symbol table cannot have zero sh_entsize"); 498 const size_t nsym = symshdr->sh_size / symshdr->sh_entsize; 499 const size_t onent = shdr->sh_size / shdr->sh_entsize; 500 assert (data->d_size == shdr->sh_size); 501 502 #define CONVERT_HASH(Hash_Word) \ 503 { \ 504 const Hash_Word *const old_hash = data->d_buf; \ 505 const size_t nbucket = old_hash[0]; \ 506 const size_t nchain = old_hash[1]; \ 507 const Hash_Word *const old_bucket = &old_hash[2]; \ 508 const Hash_Word *const old_chain = &old_bucket[nbucket]; \ 509 assert (onent == 2 + nbucket + nchain); \ 510 \ 511 const size_t nent = 2 + nbucket + nsym; \ 512 Hash_Word *const new_hash = xcalloc (nent, sizeof new_hash[0]); \ 513 Hash_Word *const new_bucket = &new_hash[2]; \ 514 Hash_Word *const new_chain = &new_bucket[nbucket]; \ 515 \ 516 new_hash[0] = nbucket; \ 517 new_hash[1] = nsym; \ 518 for (size_t i = 0; i < nbucket; ++i) \ 519 if (old_bucket[i] != STN_UNDEF) \ 520 new_bucket[i] = map[old_bucket[i] - 1]; \ 521 \ 522 for (size_t i = 1; i < nchain; ++i) \ 523 if (old_chain[i] != STN_UNDEF) \ 524 new_chain[map[i - 1]] = map[old_chain[i] - 1]; \ 525 \ 526 record_new_data (new_hash); \ 527 data->d_buf = new_hash; \ 528 data->d_size = nent * sizeof new_hash[0]; \ 529 } 530 531 switch (shdr->sh_entsize) 532 { 533 case 4: 534 CONVERT_HASH (Elf32_Word); 535 break; 536 case 8: 537 CONVERT_HASH (Elf64_Xword); 538 break; 539 default: 540 abort (); 541 } 542 543 elf_flagdata (data, ELF_C_SET, ELF_F_DIRTY); 544 update_sh_size (outscn, data); 545 546 #undef CONVERT_HASH 547 } 548 break; 549 550 case SHT_GNU_versym: 551 /* We must expand the table and move its elements around. */ 552 { 553 if (shdr->sh_entsize == 0) 554 error (EXIT_FAILURE, 0, 555 "GNU_versym section cannot have zero sh_entsize"); 556 if (symshdr->sh_entsize == 0) 557 error (EXIT_FAILURE, 0, "Symbol table cannot have zero sh_entsize"); 558 const size_t nent = symshdr->sh_size / symshdr->sh_entsize; 559 const size_t onent = shdr->sh_size / shdr->sh_entsize; 560 assert (nent >= onent); 561 562 /* We don't bother using gelf_update_versym because there is 563 really no conversion to be done. */ 564 assert (sizeof (Elf32_Versym) == sizeof (GElf_Versym)); 565 assert (sizeof (Elf64_Versym) == sizeof (GElf_Versym)); 566 GElf_Versym *versym = xcalloc (nent, sizeof versym[0]); 567 568 for (size_t i = 1; i < onent; ++i) 569 { 570 GElf_Versym *v = gelf_getversym (data, i, &versym[map[i - 1]]); 571 ELF_CHECK (v != NULL, _("cannot get symbol version: %s")); 572 } 573 574 record_new_data (versym); 575 data->d_buf = versym; 576 data->d_size = nent * shdr->sh_entsize; 577 elf_flagdata (data, ELF_C_SET, ELF_F_DIRTY); 578 update_sh_size (outscn, data); 579 } 580 break; 581 582 default: 583 error (EXIT_FAILURE, 0, 584 _("unexpected section type in [%zu] with sh_link to symtab"), 585 elf_ndxscn (inscn)); 586 } 587 } 588 589 /* Adjust all the relocation sections in the file. */ 590 static void 591 adjust_all_relocs (Elf *elf, Elf_Scn *symtab, const GElf_Shdr *symshdr, 592 size_t map[]) 593 { 594 size_t new_sh_link = elf_ndxscn (symtab); 595 Elf_Scn *scn = NULL; 596 while ((scn = elf_nextscn (elf, scn)) != NULL) 597 if (scn != symtab) 598 { 599 GElf_Shdr shdr_mem; 600 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 601 ELF_CHECK (shdr != NULL, _("cannot get section header: %s")); 602 /* Don't redo SHT_GROUP, groups are in both the stripped and debug, 603 it will already have been done by adjust_relocs for the 604 stripped_symtab. */ 605 if (shdr->sh_type != SHT_NOBITS && shdr->sh_type != SHT_GROUP 606 && shdr->sh_link == new_sh_link) 607 adjust_relocs (scn, scn, shdr, map, symshdr); 608 } 609 } 610 611 /* The original file probably had section symbols for all of its 612 sections, even the unallocated ones. To match it as closely as 613 possible, add in section symbols for the added sections. */ 614 static Elf_Data * 615 add_new_section_symbols (Elf_Scn *old_symscn, size_t old_shnum, 616 Elf *elf, bool rel, Elf_Scn *symscn, size_t shnum) 617 { 618 const size_t added = shnum - old_shnum; 619 620 GElf_Shdr shdr_mem; 621 GElf_Shdr *shdr = gelf_getshdr (symscn, &shdr_mem); 622 ELF_CHECK (shdr != NULL, _("cannot get section header: %s")); 623 if (shdr->sh_entsize == 0) 624 error (EXIT_FAILURE, 0, "Symbol table section cannot have zero sh_entsize"); 625 626 const size_t nsym = shdr->sh_size / shdr->sh_entsize; 627 size_t symndx_map[nsym - 1]; 628 629 shdr->sh_info += added; 630 shdr->sh_size += added * shdr->sh_entsize; 631 update_shdr (symscn, shdr); 632 633 Elf_Data *symdata = elf_getdata (symscn, NULL); 634 Elf_Data *shndxdata = NULL; /* XXX */ 635 636 symdata->d_size = shdr->sh_size; 637 symdata->d_buf = xmalloc (symdata->d_size); 638 record_new_data (symdata->d_buf); 639 640 /* Copy the existing section symbols. */ 641 Elf_Data *old_symdata = elf_getdata (old_symscn, NULL); 642 for (size_t i = 0; i < old_shnum; ++i) 643 { 644 GElf_Sym sym_mem; 645 GElf_Word shndx = SHN_UNDEF; 646 GElf_Sym *sym = gelf_getsymshndx (old_symdata, shndxdata, 647 i, &sym_mem, &shndx); 648 ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, i, 649 sym, shndx), 650 _("cannot update symbol table: %s")); 651 652 if (i > 0) 653 symndx_map[i - 1] = i; 654 } 655 656 /* Add in the new section symbols. */ 657 for (size_t i = old_shnum; i < shnum; ++i) 658 { 659 GElf_Shdr i_shdr_mem; 660 GElf_Shdr *i_shdr = gelf_getshdr (elf_getscn (elf, i), &i_shdr_mem); 661 ELF_CHECK (i_shdr != NULL, _("cannot get section header: %s")); 662 GElf_Sym sym = 663 { 664 .st_value = rel ? 0 : i_shdr->sh_addr, 665 .st_info = GELF_ST_INFO (STB_LOCAL, STT_SECTION), 666 .st_shndx = i < SHN_LORESERVE ? i : SHN_XINDEX 667 }; 668 GElf_Word shndx = i < SHN_LORESERVE ? SHN_UNDEF : i; 669 ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, i, 670 &sym, shndx), 671 _("cannot update symbol table: %s")); 672 } 673 674 /* Now copy the rest of the existing symbols. */ 675 for (size_t i = old_shnum; i < nsym; ++i) 676 { 677 GElf_Sym sym_mem; 678 GElf_Word shndx = SHN_UNDEF; 679 GElf_Sym *sym = gelf_getsymshndx (old_symdata, shndxdata, 680 i, &sym_mem, &shndx); 681 ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, 682 i + added, sym, shndx), 683 _("cannot update symbol table: %s")); 684 685 symndx_map[i - 1] = i + added; 686 } 687 688 /* Adjust any relocations referring to the old symbol table. */ 689 adjust_all_relocs (elf, symscn, shdr, symndx_map); 690 691 return symdata; 692 } 693 694 /* This has the side effect of updating STT_SECTION symbols' values, 695 in case of prelink adjustments. */ 696 static Elf_Data * 697 check_symtab_section_symbols (Elf *elf, bool rel, Elf_Scn *scn, 698 size_t shnum, size_t shstrndx, 699 Elf_Scn *oscn, size_t oshnum, size_t oshstrndx, 700 size_t debuglink) 701 { 702 size_t n = symtab_count_leading_section_symbols (elf, oscn, oshnum, 703 elf_getdata (scn, NULL)); 704 705 if (n == oshnum) 706 return add_new_section_symbols (oscn, n, elf, rel, scn, shnum); 707 708 if (n == oshstrndx || (n == debuglink && n == oshstrndx - 1)) 709 return add_new_section_symbols (oscn, n, elf, rel, scn, shstrndx); 710 711 return NULL; 712 } 713 714 struct section 716 { 717 Elf_Scn *scn; 718 const char *name; 719 const char *sig; 720 Elf_Scn *outscn; 721 Dwelf_Strent *strent; 722 GElf_Shdr shdr; 723 }; 724 725 static int 726 compare_alloc_sections (const struct section *s1, const struct section *s2, 727 bool rel) 728 { 729 if (!rel) 730 { 731 /* Sort by address. */ 732 if (s1->shdr.sh_addr < s2->shdr.sh_addr) 733 return -1; 734 if (s1->shdr.sh_addr > s2->shdr.sh_addr) 735 return 1; 736 } 737 738 /* At the same address, preserve original section order. */ 739 return (ssize_t) elf_ndxscn (s1->scn) - (ssize_t) elf_ndxscn (s2->scn); 740 } 741 742 static int 743 compare_unalloc_sections (const GElf_Shdr *shdr1, const GElf_Shdr *shdr2, 744 const char *name1, const char *name2, 745 const char *sig1, const char *sig2) 746 { 747 /* Sort by sh_flags as an arbitrary ordering. */ 748 if (shdr1->sh_flags < shdr2->sh_flags) 749 return -1; 750 if (shdr1->sh_flags > shdr2->sh_flags) 751 return 1; 752 753 /* Sizes should be the same. */ 754 if (shdr1->sh_size < shdr2->sh_size) 755 return -1; 756 if (shdr1->sh_size > shdr2->sh_size) 757 return 1; 758 759 /* Are they both SHT_GROUP sections? Then compare signatures. */ 760 if (sig1 != NULL && sig2 != NULL) 761 return strcmp (sig1, sig2); 762 763 /* Sort by name as last resort. */ 764 return strcmp (name1, name2); 765 } 766 767 static int 768 compare_sections (const void *a, const void *b, bool rel) 769 { 770 const struct section *s1 = a; 771 const struct section *s2 = b; 772 773 /* Sort all non-allocated sections last. */ 774 if ((s1->shdr.sh_flags ^ s2->shdr.sh_flags) & SHF_ALLOC) 775 return (s1->shdr.sh_flags & SHF_ALLOC) ? -1 : 1; 776 777 return ((s1->shdr.sh_flags & SHF_ALLOC) 778 ? compare_alloc_sections (s1, s2, rel) 779 : compare_unalloc_sections (&s1->shdr, &s2->shdr, 780 s1->name, s2->name, 781 s1->sig, s2->sig)); 782 } 783 784 static int 785 compare_sections_rel (const void *a, const void *b) 786 { 787 return compare_sections (a, b, true); 788 } 789 790 static int 791 compare_sections_nonrel (const void *a, const void *b) 792 { 793 return compare_sections (a, b, false); 794 } 795 796 797 struct symbol 798 { 799 size_t *map; 800 801 union 802 { 803 const char *name; 804 Dwelf_Strent *strent; 805 }; 806 union 807 { 808 struct 809 { 810 GElf_Addr value; 811 GElf_Xword size; 812 GElf_Word shndx; 813 union 814 { 815 struct 816 { 817 uint8_t info; 818 uint8_t other; 819 } info; 820 int16_t compare; 821 }; 822 }; 823 824 /* For a symbol discarded after first sort, this matches its better's 825 map pointer. */ 826 size_t *duplicate; 827 }; 828 }; 829 830 /* Collect input symbols into our internal form. */ 831 static void 832 collect_symbols (Elf *outelf, bool rel, Elf_Scn *symscn, Elf_Scn *strscn, 833 const size_t nent, const GElf_Addr bias, 834 const size_t scnmap[], struct symbol *table, size_t *map, 835 struct section *split_bss) 836 { 837 Elf_Data *symdata = elf_getdata (symscn, NULL); 838 Elf_Data *strdata = elf_getdata (strscn, NULL); 839 Elf_Data *shndxdata = NULL; /* XXX */ 840 841 for (size_t i = 1; i < nent; ++i) 842 { 843 GElf_Sym sym_mem; 844 GElf_Word shndx = SHN_UNDEF; 845 GElf_Sym *sym = gelf_getsymshndx (symdata, shndxdata, i, 846 &sym_mem, &shndx); 847 ELF_CHECK (sym != NULL, _("cannot get symbol table entry: %s")); 848 if (sym->st_shndx != SHN_XINDEX) 849 shndx = sym->st_shndx; 850 851 if (sym->st_name >= strdata->d_size) 852 error (EXIT_FAILURE, 0, 853 _("invalid string offset in symbol [%zu]"), i); 854 855 struct symbol *s = &table[i - 1]; 856 s->map = &map[i - 1]; 857 s->name = strdata->d_buf + sym->st_name; 858 s->value = sym->st_value + bias; 859 s->size = sym->st_size; 860 s->shndx = shndx; 861 s->info.info = sym->st_info; 862 s->info.other = sym->st_other; 863 864 if (scnmap != NULL && shndx != SHN_UNDEF && shndx < SHN_LORESERVE) 865 s->shndx = scnmap[shndx - 1]; 866 867 if (GELF_ST_TYPE (s->info.info) == STT_SECTION && !rel) 868 { 869 /* Update the value to match the output section. */ 870 GElf_Shdr shdr_mem; 871 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (outelf, s->shndx), 872 &shdr_mem); 873 ELF_CHECK (shdr != NULL, _("cannot get section header: %s")); 874 s->value = shdr->sh_addr; 875 } 876 else if (split_bss != NULL 877 && s->value < split_bss->shdr.sh_addr 878 && s->value >= split_bss[-1].shdr.sh_addr 879 && shndx == elf_ndxscn (split_bss->outscn)) 880 /* This symbol was in .bss and was split into .dynbss. */ 881 s->shndx = elf_ndxscn (split_bss[-1].outscn); 882 } 883 } 884 885 886 #define CMP(value) \ 887 if (s1->value < s2->value) \ 888 return -1; \ 889 if (s1->value > s2->value) \ 890 return 1 891 892 /* Compare symbols with a consistent ordering, 893 but one only meaningful for equality. */ 894 static int 895 compare_symbols (const void *a, const void *b) 896 { 897 const struct symbol *s1 = a; 898 const struct symbol *s2 = b; 899 900 CMP (value); 901 CMP (size); 902 CMP (shndx); 903 904 return (s1->compare - s2->compare) ?: strcmp (s1->name, s2->name); 905 } 906 907 /* Compare symbols for output order after slots have been assigned. */ 908 static int 909 compare_symbols_output (const void *a, const void *b) 910 { 911 const struct symbol *s1 = a; 912 const struct symbol *s2 = b; 913 int cmp; 914 915 /* Sort discarded symbols last. */ 916 cmp = (s1->name == NULL) - (s2->name == NULL); 917 918 if (cmp == 0) 919 /* Local symbols must come first. */ 920 cmp = ((GELF_ST_BIND (s2->info.info) == STB_LOCAL) 921 - (GELF_ST_BIND (s1->info.info) == STB_LOCAL)); 922 923 if (cmp == 0) 924 /* binutils always puts section symbols first. */ 925 cmp = ((GELF_ST_TYPE (s2->info.info) == STT_SECTION) 926 - (GELF_ST_TYPE (s1->info.info) == STT_SECTION)); 927 928 if (cmp == 0) 929 { 930 if (GELF_ST_TYPE (s1->info.info) == STT_SECTION) 931 { 932 /* binutils always puts section symbols in section index order. */ 933 CMP (shndx); 934 else 935 assert (s1 == s2); 936 } 937 938 /* Nothing really matters, so preserve the original order. */ 939 CMP (map); 940 else 941 assert (s1 == s2); 942 } 943 944 return cmp; 945 } 946 947 #undef CMP 948 949 /* Return true if the flags of the sections match, ignoring the SHF_INFO_LINK 950 flag if the section contains relocation information. */ 951 static bool 952 sections_flags_match (Elf64_Xword sh_flags1, Elf64_Xword sh_flags2, 953 Elf64_Word sh_type) 954 { 955 if (sh_type == SHT_REL || sh_type == SHT_RELA) 956 { 957 sh_flags1 &= ~SHF_INFO_LINK; 958 sh_flags2 &= ~SHF_INFO_LINK; 959 } 960 961 return sh_flags1 == sh_flags2; 962 } 963 964 /* Return true iff the flags, size, and name match. */ 965 static bool 966 sections_match (const struct section *sections, size_t i, 967 const GElf_Shdr *shdr, const char *name) 968 { 969 return (sections_flags_match (sections[i].shdr.sh_flags, shdr->sh_flags, 970 sections[i].shdr.sh_type) 971 && (sections[i].shdr.sh_size == shdr->sh_size 972 || (sections[i].shdr.sh_size < shdr->sh_size 973 && section_can_shrink (§ions[i].shdr))) 974 && !strcmp (sections[i].name, name)); 975 } 976 977 /* Locate a matching allocated section in SECTIONS. */ 978 static struct section * 979 find_alloc_section (const GElf_Shdr *shdr, GElf_Addr bias, const char *name, 980 struct section sections[], size_t nalloc) 981 { 982 const GElf_Addr addr = shdr->sh_addr + bias; 983 size_t l = 0, u = nalloc; 984 while (l < u) 985 { 986 size_t i = (l + u) / 2; 987 if (addr < sections[i].shdr.sh_addr) 988 u = i; 989 else if (addr > sections[i].shdr.sh_addr) 990 l = i + 1; 991 else 992 { 993 /* We've found allocated sections with this address. 994 Find one with matching size, flags, and name. */ 995 while (i > 0 && sections[i - 1].shdr.sh_addr == addr) 996 --i; 997 for (; i < nalloc && sections[i].shdr.sh_addr == addr; 998 ++i) 999 if (sections_match (sections, i, shdr, name)) 1000 return §ions[i]; 1001 break; 1002 } 1003 } 1004 return NULL; 1005 } 1006 1007 static inline const char * 1008 get_section_name (size_t ndx, const GElf_Shdr *shdr, const Elf_Data *shstrtab) 1009 { 1010 if (shdr->sh_name >= shstrtab->d_size) 1011 error (EXIT_FAILURE, 0, _("cannot read section [%zu] name: %s"), 1012 ndx, elf_errmsg (-1)); 1013 return shstrtab->d_buf + shdr->sh_name; 1014 } 1015 1016 /* Returns the signature of a group section, or NULL if the given 1017 section isn't a group. */ 1018 static const char * 1019 get_group_sig (Elf *elf, GElf_Shdr *shdr) 1020 { 1021 if (shdr->sh_type != SHT_GROUP) 1022 return NULL; 1023 1024 Elf_Scn *symscn = elf_getscn (elf, shdr->sh_link); 1025 if (symscn == NULL) 1026 error (EXIT_FAILURE, 0, _("bad sh_link for group section: %s"), 1027 elf_errmsg (-1)); 1028 1029 GElf_Shdr symshdr_mem; 1030 GElf_Shdr *symshdr = gelf_getshdr (symscn, &symshdr_mem); 1031 if (symshdr == NULL) 1032 error (EXIT_FAILURE, 0, _("couldn't get shdr for group section: %s"), 1033 elf_errmsg (-1)); 1034 1035 Elf_Data *symdata = elf_getdata (symscn, NULL); 1036 if (symdata == NULL) 1037 error (EXIT_FAILURE, 0, _("bad data for group symbol section: %s"), 1038 elf_errmsg (-1)); 1039 1040 GElf_Sym sym_mem; 1041 GElf_Sym *sym = gelf_getsym (symdata, shdr->sh_info, &sym_mem); 1042 if (sym == NULL) 1043 error (EXIT_FAILURE, 0, _("couldn't get symbol for group section: %s"), 1044 elf_errmsg (-1)); 1045 1046 const char *sig = elf_strptr (elf, symshdr->sh_link, sym->st_name); 1047 if (sig == NULL) 1048 error (EXIT_FAILURE, 0, _("bad symbol name for group section: %s"), 1049 elf_errmsg (-1)); 1050 1051 return sig; 1052 } 1053 1054 /* Fix things up when prelink has moved some allocated sections around 1055 and the debuginfo file's section headers no longer match up. 1056 This fills in SECTIONS[0..NALLOC-1].outscn or exits. 1057 If there was a .bss section that was split into two sections 1058 with the new one preceding it in sh_addr, we return that pointer. */ 1059 static struct section * 1060 find_alloc_sections_prelink (Elf *debug, Elf_Data *debug_shstrtab, 1061 Elf *main, const GElf_Ehdr *main_ehdr, 1062 Elf_Data *main_shstrtab, GElf_Addr bias, 1063 struct section *sections, 1064 size_t nalloc, size_t nsections) 1065 { 1066 Elf_Scn *undo = NULL; 1067 for (size_t i = nalloc; i < nsections; ++i) 1068 { 1069 const struct section *sec = §ions[i]; 1070 if (sec->shdr.sh_type == SHT_PROGBITS 1071 && !(sec->shdr.sh_flags & SHF_ALLOC) 1072 && !strcmp (sec->name, ".gnu.prelink_undo")) 1073 { 1074 undo = sec->scn; 1075 break; 1076 } 1077 } 1078 1079 /* Find the original allocated sections before prelinking. */ 1080 struct section *undo_sections = NULL; 1081 size_t undo_nalloc = 0; 1082 if (undo != NULL) 1083 { 1084 /* Clear assignments that might have been bogus. */ 1085 for (size_t i = 0; i < nalloc; ++i) 1086 sections[i].outscn = NULL; 1087 1088 Elf_Data *undodata = elf_rawdata (undo, NULL); 1089 ELF_CHECK (undodata != NULL, 1090 _("cannot read '.gnu.prelink_undo' section: %s")); 1091 1092 union 1093 { 1094 Elf32_Ehdr e32; 1095 Elf64_Ehdr e64; 1096 } ehdr; 1097 Elf_Data dst = 1098 { 1099 .d_buf = &ehdr, 1100 .d_size = sizeof ehdr, 1101 .d_type = ELF_T_EHDR, 1102 .d_version = EV_CURRENT 1103 }; 1104 Elf_Data src = *undodata; 1105 src.d_size = gelf_fsize (main, ELF_T_EHDR, 1, EV_CURRENT); 1106 src.d_type = ELF_T_EHDR; 1107 ELF_CHECK (gelf_xlatetom (main, &dst, &src, 1108 main_ehdr->e_ident[EI_DATA]) != NULL, 1109 _("cannot read '.gnu.prelink_undo' section: %s")); 1110 1111 uint_fast16_t phnum; 1112 uint_fast16_t shnum; /* prelink doesn't handle > SHN_LORESERVE. */ 1113 if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32) 1114 { 1115 phnum = ehdr.e32.e_phnum; 1116 shnum = ehdr.e32.e_shnum; 1117 } 1118 else 1119 { 1120 phnum = ehdr.e64.e_phnum; 1121 shnum = ehdr.e64.e_shnum; 1122 } 1123 1124 bool class32 = ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32; 1125 size_t shsize = class32 ? sizeof (Elf32_Shdr) : sizeof (Elf64_Shdr); 1126 if (unlikely (shnum == 0 || shnum > SIZE_MAX / shsize + 1)) 1127 error (EXIT_FAILURE, 0, _("overflow with shnum = %zu in '%s' section"), 1128 (size_t) shnum, ".gnu.prelink_undo"); 1129 1130 --shnum; 1131 1132 size_t phsize = gelf_fsize (main, ELF_T_PHDR, phnum, EV_CURRENT); 1133 src.d_buf += src.d_size + phsize; 1134 src.d_size = gelf_fsize (main, ELF_T_SHDR, shnum, EV_CURRENT); 1135 src.d_type = ELF_T_SHDR; 1136 if ((size_t) (src.d_buf - undodata->d_buf) > undodata->d_size 1137 || undodata->d_size - (src.d_buf - undodata->d_buf) != src.d_size) 1138 error (EXIT_FAILURE, 0, _("invalid contents in '%s' section"), 1139 ".gnu.prelink_undo"); 1140 1141 const size_t shdr_bytes = shnum * shsize; 1142 void *shdr = xmalloc (shdr_bytes); 1143 dst.d_buf = shdr; 1144 dst.d_size = shdr_bytes; 1145 ELF_CHECK (gelf_xlatetom (main, &dst, &src, 1146 main_ehdr->e_ident[EI_DATA]) != NULL, 1147 _("cannot read '.gnu.prelink_undo' section: %s")); 1148 1149 undo_sections = xmalloc (shnum * sizeof undo_sections[0]); 1150 for (size_t i = 0; i < shnum; ++i) 1151 { 1152 struct section *sec = &undo_sections[undo_nalloc]; 1153 Elf32_Shdr (*s32)[shnum] = shdr; 1154 Elf64_Shdr (*s64)[shnum] = shdr; 1155 if (class32) 1156 { 1157 #define COPY(field) sec->shdr.field = (*s32)[i].field 1158 COPY (sh_name); 1159 COPY (sh_type); 1160 COPY (sh_flags); 1161 COPY (sh_addr); 1162 COPY (sh_offset); 1163 COPY (sh_size); 1164 COPY (sh_link); 1165 COPY (sh_info); 1166 COPY (sh_addralign); 1167 COPY (sh_entsize); 1168 #undef COPY 1169 } 1170 else 1171 sec->shdr = (*s64)[i]; 1172 if (sec->shdr.sh_flags & SHF_ALLOC) 1173 { 1174 sec->shdr.sh_addr += bias; 1175 sec->name = get_section_name (i + 1, &sec->shdr, main_shstrtab); 1176 sec->scn = elf_getscn (main, i + 1); /* Really just for ndx. */ 1177 sec->outscn = NULL; 1178 sec->strent = NULL; 1179 sec->sig = get_group_sig (main, &sec->shdr); 1180 ++undo_nalloc; 1181 } 1182 } 1183 qsort (undo_sections, undo_nalloc, 1184 sizeof undo_sections[0], compare_sections_nonrel); 1185 free (shdr); 1186 } 1187 1188 bool fail = false; 1189 inline void check_match (bool match, Elf_Scn *scn, const char *name) 1190 { 1191 if (!match) 1192 { 1193 fail = true; 1194 error (0, 0, _("cannot find matching section for [%zu] '%s'"), 1195 elf_ndxscn (scn), name); 1196 } 1197 } 1198 1199 Elf_Scn *scn = NULL; 1200 while ((scn = elf_nextscn (debug, scn)) != NULL) 1201 { 1202 GElf_Shdr shdr_mem; 1203 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 1204 ELF_CHECK (shdr != NULL, _("cannot get section header: %s")); 1205 1206 if (!(shdr->sh_flags & SHF_ALLOC)) 1207 continue; 1208 1209 const char *name = get_section_name (elf_ndxscn (scn), shdr, 1210 debug_shstrtab); 1211 1212 if (undo_sections != NULL) 1213 { 1214 struct section *sec = find_alloc_section (shdr, 0, name, 1215 undo_sections, 1216 undo_nalloc); 1217 if (sec != NULL) 1218 { 1219 sec->outscn = scn; 1220 continue; 1221 } 1222 } 1223 1224 /* If there is no prelink info, we are just here to find 1225 the sections to give error messages about. */ 1226 for (size_t i = 0; shdr != NULL && i < nalloc; ++i) 1227 if (sections[i].outscn == scn) 1228 shdr = NULL; 1229 check_match (shdr == NULL, scn, name); 1230 } 1231 1232 if (fail) 1233 exit (EXIT_FAILURE); 1234 1235 /* Now we have lined up output sections for each of the original sections 1236 before prelinking. Translate those to the prelinked sections. 1237 This matches what prelink's undo_sections does. */ 1238 struct section *split_bss = NULL; 1239 for (size_t i = 0; i < undo_nalloc; ++i) 1240 { 1241 const struct section *undo_sec = &undo_sections[i]; 1242 1243 const char *name = undo_sec->name; 1244 scn = undo_sec->scn; /* This is just for elf_ndxscn. */ 1245 1246 for (size_t j = 0; j < nalloc; ++j) 1247 { 1248 struct section *sec = §ions[j]; 1249 #define RELA_SCALED(field) \ 1250 (2 * sec->shdr.field == 3 * undo_sec->shdr.field) 1251 if (sec->outscn == NULL 1252 && sec->shdr.sh_name == undo_sec->shdr.sh_name 1253 && sec->shdr.sh_flags == undo_sec->shdr.sh_flags 1254 && sec->shdr.sh_addralign == undo_sec->shdr.sh_addralign 1255 && (((sec->shdr.sh_type == undo_sec->shdr.sh_type 1256 && sec->shdr.sh_entsize == undo_sec->shdr.sh_entsize 1257 && (sec->shdr.sh_size == undo_sec->shdr.sh_size 1258 || (sec->shdr.sh_size > undo_sec->shdr.sh_size 1259 && main_ehdr->e_type == ET_EXEC 1260 && !strcmp (sec->name, ".dynstr")))) 1261 || (sec->shdr.sh_size == undo_sec->shdr.sh_size 1262 && ((sec->shdr.sh_entsize == undo_sec->shdr.sh_entsize 1263 && undo_sec->shdr.sh_type == SHT_NOBITS) 1264 || undo_sec->shdr.sh_type == SHT_PROGBITS) 1265 && !strcmp (sec->name, ".plt"))) 1266 || (sec->shdr.sh_type == SHT_RELA 1267 && undo_sec->shdr.sh_type == SHT_REL 1268 && RELA_SCALED (sh_entsize) && RELA_SCALED (sh_size)) 1269 || (sec->shdr.sh_entsize == undo_sec->shdr.sh_entsize 1270 && (sec->shdr.sh_type == undo_sec->shdr.sh_type 1271 || (sec->shdr.sh_type == SHT_PROGBITS 1272 && undo_sec->shdr.sh_type == SHT_NOBITS)) 1273 && sec->shdr.sh_size <= undo_sec->shdr.sh_size 1274 && (!strcmp (sec->name, ".bss") 1275 || !strcmp (sec->name, ".sbss")) 1276 && (sec->shdr.sh_size == undo_sec->shdr.sh_size 1277 || (split_bss = sec) > sections)))) 1278 { 1279 sec->outscn = undo_sec->outscn; 1280 undo_sec = NULL; 1281 break; 1282 } 1283 } 1284 1285 check_match (undo_sec == NULL, scn, name); 1286 } 1287 1288 free (undo_sections); 1289 1290 if (fail) 1291 exit (EXIT_FAILURE); 1292 1293 return split_bss; 1294 } 1295 1296 /* Create new .shstrtab contents, subroutine of copy_elided_sections. 1297 This can't be open coded there and still use variable-length auto arrays, 1298 since the end of our block would free other VLAs too. */ 1299 static Elf_Data * 1300 new_shstrtab (Elf *unstripped, size_t unstripped_shnum, 1301 Elf_Data *shstrtab, size_t unstripped_shstrndx, 1302 struct section *sections, size_t stripped_shnum, 1303 Dwelf_Strtab *strtab) 1304 { 1305 if (strtab == NULL) 1306 return NULL; 1307 1308 Dwelf_Strent *unstripped_strent[unstripped_shnum - 1]; 1309 memset (unstripped_strent, 0, sizeof unstripped_strent); 1310 for (struct section *sec = sections; 1311 sec < §ions[stripped_shnum - 1]; 1312 ++sec) 1313 if (sec->outscn != NULL) 1314 { 1315 if (sec->strent == NULL) 1316 { 1317 sec->strent = dwelf_strtab_add (strtab, sec->name); 1318 ELF_CHECK (sec->strent != NULL, 1319 _("cannot add section name to string table: %s")); 1320 } 1321 unstripped_strent[elf_ndxscn (sec->outscn) - 1] = sec->strent; 1322 } 1323 1324 /* Add names of sections we aren't touching. */ 1325 for (size_t i = 0; i < unstripped_shnum - 1; ++i) 1326 if (unstripped_strent[i] == NULL) 1327 { 1328 Elf_Scn *scn = elf_getscn (unstripped, i + 1); 1329 GElf_Shdr shdr_mem; 1330 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 1331 const char *name = get_section_name (i + 1, shdr, shstrtab); 1332 unstripped_strent[i] = dwelf_strtab_add (strtab, name); 1333 ELF_CHECK (unstripped_strent[i] != NULL, 1334 _("cannot add section name to string table: %s")); 1335 } 1336 else 1337 unstripped_strent[i] = NULL; 1338 1339 /* Now finalize the string table so we can get offsets. */ 1340 Elf_Data *strtab_data = elf_getdata (elf_getscn (unstripped, 1341 unstripped_shstrndx), NULL); 1342 ELF_CHECK (elf_flagdata (strtab_data, ELF_C_SET, ELF_F_DIRTY), 1343 _("cannot update section header string table data: %s")); 1344 if (dwelf_strtab_finalize (strtab, strtab_data) == NULL) 1345 error (EXIT_FAILURE, 0, "Not enough memory to create string table"); 1346 1347 /* Update the sh_name fields of sections we aren't modifying later. */ 1348 for (size_t i = 0; i < unstripped_shnum - 1; ++i) 1349 if (unstripped_strent[i] != NULL) 1350 { 1351 Elf_Scn *scn = elf_getscn (unstripped, i + 1); 1352 GElf_Shdr shdr_mem; 1353 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 1354 shdr->sh_name = dwelf_strent_off (unstripped_strent[i]); 1355 if (i + 1 == unstripped_shstrndx) 1356 shdr->sh_size = strtab_data->d_size; 1357 update_shdr (scn, shdr); 1358 } 1359 1360 return strtab_data; 1361 } 1362 1363 /* Fill in any SHT_NOBITS sections in UNSTRIPPED by 1364 copying their contents and sh_type from STRIPPED. */ 1365 static void 1366 copy_elided_sections (Elf *unstripped, Elf *stripped, 1367 const GElf_Ehdr *stripped_ehdr, GElf_Addr bias) 1368 { 1369 size_t unstripped_shstrndx; 1370 ELF_CHECK (elf_getshdrstrndx (unstripped, &unstripped_shstrndx) == 0, 1371 _("cannot get section header string table section index: %s")); 1372 1373 size_t stripped_shstrndx; 1374 ELF_CHECK (elf_getshdrstrndx (stripped, &stripped_shstrndx) == 0, 1375 _("cannot get section header string table section index: %s")); 1376 1377 size_t unstripped_shnum; 1378 ELF_CHECK (elf_getshdrnum (unstripped, &unstripped_shnum) == 0, 1379 _("cannot get section count: %s")); 1380 1381 size_t stripped_shnum; 1382 ELF_CHECK (elf_getshdrnum (stripped, &stripped_shnum) == 0, 1383 _("cannot get section count: %s")); 1384 1385 if (unlikely (stripped_shnum > unstripped_shnum)) 1386 error (EXIT_FAILURE, 0, _("\ 1387 more sections in stripped file than debug file -- arguments reversed?")); 1388 1389 /* Cache the stripped file's section details. */ 1390 struct section sections[stripped_shnum - 1]; 1391 Elf_Scn *scn = NULL; 1392 while ((scn = elf_nextscn (stripped, scn)) != NULL) 1393 { 1394 size_t i = elf_ndxscn (scn) - 1; 1395 GElf_Shdr *shdr = gelf_getshdr (scn, §ions[i].shdr); 1396 ELF_CHECK (shdr != NULL, _("cannot get section header: %s")); 1397 sections[i].name = elf_strptr (stripped, stripped_shstrndx, 1398 shdr->sh_name); 1399 if (sections[i].name == NULL) 1400 error (EXIT_FAILURE, 0, _("cannot read section [%zu] name: %s"), 1401 elf_ndxscn (scn), elf_errmsg (-1)); 1402 sections[i].scn = scn; 1403 sections[i].outscn = NULL; 1404 sections[i].strent = NULL; 1405 sections[i].sig = get_group_sig (stripped, shdr); 1406 } 1407 1408 const struct section *stripped_symtab = NULL; 1409 1410 /* Sort the sections, allocated by address and others after. */ 1411 qsort (sections, stripped_shnum - 1, sizeof sections[0], 1412 stripped_ehdr->e_type == ET_REL 1413 ? compare_sections_rel : compare_sections_nonrel); 1414 size_t nalloc = stripped_shnum - 1; 1415 while (nalloc > 0 && !(sections[nalloc - 1].shdr.sh_flags & SHF_ALLOC)) 1416 { 1417 --nalloc; 1418 if (sections[nalloc].shdr.sh_type == SHT_SYMTAB) 1419 stripped_symtab = §ions[nalloc]; 1420 } 1421 1422 /* Locate a matching unallocated section in SECTIONS. */ 1423 inline struct section *find_unalloc_section (const GElf_Shdr *shdr, 1424 const char *name, 1425 const char *sig) 1426 { 1427 size_t l = nalloc, u = stripped_shnum - 1; 1428 while (l < u) 1429 { 1430 size_t i = (l + u) / 2; 1431 struct section *sec = §ions[i]; 1432 int cmp = compare_unalloc_sections (shdr, &sec->shdr, 1433 name, sec->name, 1434 sig, sec->sig); 1435 if (cmp < 0) 1436 u = i; 1437 else if (cmp > 0) 1438 l = i + 1; 1439 else 1440 return sec; 1441 } 1442 return NULL; 1443 } 1444 1445 Elf_Data *shstrtab = elf_getdata (elf_getscn (unstripped, 1446 unstripped_shstrndx), NULL); 1447 ELF_CHECK (shstrtab != NULL, 1448 _("cannot read section header string table: %s")); 1449 1450 /* Match each debuginfo section with its corresponding stripped section. */ 1451 bool check_prelink = false; 1452 Elf_Scn *unstripped_symtab = NULL; 1453 size_t unstripped_strndx = 0; 1454 size_t alloc_avail = 0; 1455 scn = NULL; 1456 while ((scn = elf_nextscn (unstripped, scn)) != NULL) 1457 { 1458 GElf_Shdr shdr_mem; 1459 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 1460 ELF_CHECK (shdr != NULL, _("cannot get section header: %s")); 1461 1462 if (shdr->sh_type == SHT_SYMTAB) 1463 { 1464 unstripped_symtab = scn; 1465 unstripped_strndx = shdr->sh_link; 1466 continue; 1467 } 1468 1469 const size_t ndx = elf_ndxscn (scn); 1470 if (ndx == unstripped_shstrndx || ndx == unstripped_strndx) 1471 continue; 1472 1473 const char *name = get_section_name (ndx, shdr, shstrtab); 1474 1475 struct section *sec = NULL; 1476 if (shdr->sh_flags & SHF_ALLOC) 1477 { 1478 if (stripped_ehdr->e_type != ET_REL) 1479 { 1480 /* Look for the section that matches. */ 1481 sec = find_alloc_section (shdr, bias, name, sections, nalloc); 1482 if (sec == NULL) 1483 { 1484 /* We couldn't figure it out. It may be a prelink issue. */ 1485 check_prelink = true; 1486 continue; 1487 } 1488 } 1489 else 1490 { 1491 /* The sh_addr of allocated sections does not help us, 1492 but the order usually matches. */ 1493 if (likely (sections_match (sections, alloc_avail, shdr, name))) 1494 sec = §ions[alloc_avail++]; 1495 else 1496 for (size_t i = alloc_avail + 1; i < nalloc; ++i) 1497 if (sections_match (sections, i, shdr, name)) 1498 { 1499 sec = §ions[i]; 1500 break; 1501 } 1502 } 1503 } 1504 else 1505 { 1506 /* Look for the section that matches. */ 1507 sec = find_unalloc_section (shdr, name, 1508 get_group_sig (unstripped, shdr)); 1509 if (sec == NULL) 1510 { 1511 /* An additional unallocated section is fine if not SHT_NOBITS. 1512 We looked it up anyway in case it's an unallocated section 1513 copied in both files (e.g. SHT_NOTE), and don't keep both. */ 1514 if (shdr->sh_type != SHT_NOBITS) 1515 continue; 1516 1517 /* Somehow some old .debug files wound up with SHT_NOBITS 1518 .comment sections, so let those pass. */ 1519 if (!strcmp (name, ".comment")) 1520 continue; 1521 } 1522 } 1523 1524 if (sec == NULL) 1525 error (EXIT_FAILURE, 0, 1526 _("cannot find matching section for [%zu] '%s'"), 1527 elf_ndxscn (scn), name); 1528 1529 sec->outscn = scn; 1530 } 1531 1532 /* If that failed due to changes made by prelink, we take another tack. 1533 We keep track of a .bss section that was partly split into .dynbss 1534 so that collect_symbols can update symbols' st_shndx fields. */ 1535 struct section *split_bss = NULL; 1536 if (check_prelink) 1537 { 1538 Elf_Data *data = elf_getdata (elf_getscn (stripped, stripped_shstrndx), 1539 NULL); 1540 ELF_CHECK (data != NULL, 1541 _("cannot read section header string table: %s")); 1542 split_bss = find_alloc_sections_prelink (unstripped, shstrtab, 1543 stripped, stripped_ehdr, 1544 data, bias, sections, 1545 nalloc, stripped_shnum - 1); 1546 } 1547 1548 /* Make sure each main file section has a place to go. */ 1549 const struct section *stripped_dynsym = NULL; 1550 size_t debuglink = SHN_UNDEF; 1551 size_t ndx_section[stripped_shnum - 1]; 1552 Dwelf_Strtab *strtab = NULL; 1553 for (struct section *sec = sections; 1554 sec < §ions[stripped_shnum - 1]; 1555 ++sec) 1556 { 1557 size_t secndx = elf_ndxscn (sec->scn); 1558 1559 if (sec->outscn == NULL) 1560 { 1561 /* We didn't find any corresponding section for this. */ 1562 1563 if (secndx == stripped_shstrndx) 1564 { 1565 /* We only need one .shstrtab. */ 1566 ndx_section[secndx - 1] = unstripped_shstrndx; 1567 continue; 1568 } 1569 1570 if (unstripped_symtab != NULL && sec == stripped_symtab) 1571 { 1572 /* We don't need a second symbol table. */ 1573 ndx_section[secndx - 1] = elf_ndxscn (unstripped_symtab); 1574 continue; 1575 } 1576 1577 if (unstripped_symtab != NULL && stripped_symtab != NULL 1578 && secndx == stripped_symtab->shdr.sh_link 1579 && unstripped_strndx != 0) 1580 { 1581 /* ... nor its string table. */ 1582 ndx_section[secndx - 1] = unstripped_strndx; 1583 continue; 1584 } 1585 1586 if (!(sec->shdr.sh_flags & SHF_ALLOC) 1587 && !strcmp (sec->name, ".gnu_debuglink")) 1588 { 1589 /* This was created by stripping. We don't want it. */ 1590 debuglink = secndx; 1591 ndx_section[secndx - 1] = SHN_UNDEF; 1592 continue; 1593 } 1594 1595 sec->outscn = elf_newscn (unstripped); 1596 Elf_Data *newdata = elf_newdata (sec->outscn); 1597 ELF_CHECK (newdata != NULL && gelf_update_shdr (sec->outscn, 1598 &sec->shdr), 1599 _("cannot add new section: %s")); 1600 1601 if (strtab == NULL) 1602 strtab = dwelf_strtab_init (true); 1603 sec->strent = dwelf_strtab_add (strtab, sec->name); 1604 ELF_CHECK (sec->strent != NULL, 1605 _("cannot add section name to string table: %s")); 1606 } 1607 1608 /* Cache the mapping of original section indices to output sections. */ 1609 ndx_section[secndx - 1] = elf_ndxscn (sec->outscn); 1610 } 1611 1612 /* We added some sections, so we need a new shstrtab. */ 1613 Elf_Data *strtab_data = new_shstrtab (unstripped, unstripped_shnum, 1614 shstrtab, unstripped_shstrndx, 1615 sections, stripped_shnum, 1616 strtab); 1617 1618 /* Get the updated section count. */ 1619 ELF_CHECK (elf_getshdrnum (unstripped, &unstripped_shnum) == 0, 1620 _("cannot get section count: %s")); 1621 1622 bool placed[unstripped_shnum - 1]; 1623 memset (placed, 0, sizeof placed); 1624 1625 /* Now update the output sections and copy in their data. */ 1626 GElf_Off offset = 0; 1627 for (const struct section *sec = sections; 1628 sec < §ions[stripped_shnum - 1]; 1629 ++sec) 1630 if (sec->outscn != NULL) 1631 { 1632 GElf_Shdr shdr_mem; 1633 GElf_Shdr *shdr = gelf_getshdr (sec->outscn, &shdr_mem); 1634 ELF_CHECK (shdr != NULL, _("cannot get section header: %s")); 1635 1636 /* In an ET_REL file under --relocate, the sh_addr of SHF_ALLOC 1637 sections will have been set nonzero by relocation. This 1638 touched the shdrs of whichever file had the symtab. sh_addr 1639 is still zero in the corresponding shdr. The relocated 1640 address is what we want to use. */ 1641 if (stripped_ehdr->e_type != ET_REL 1642 || !(shdr_mem.sh_flags & SHF_ALLOC) 1643 || shdr_mem.sh_addr == 0) 1644 shdr_mem.sh_addr = sec->shdr.sh_addr; 1645 1646 shdr_mem.sh_type = sec->shdr.sh_type; 1647 shdr_mem.sh_size = sec->shdr.sh_size; 1648 shdr_mem.sh_info = sec->shdr.sh_info; 1649 shdr_mem.sh_link = sec->shdr.sh_link; 1650 1651 /* Buggy binutils objdump might have stripped the SHF_INFO_LINK 1652 put it back if necessary. */ 1653 if ((sec->shdr.sh_type == SHT_REL || sec->shdr.sh_type == SHT_RELA) 1654 && sec->shdr.sh_flags != shdr_mem.sh_flags 1655 && (sec->shdr.sh_flags & SHF_INFO_LINK) != 0) 1656 shdr_mem.sh_flags |= SHF_INFO_LINK; 1657 1658 if (sec->shdr.sh_link != SHN_UNDEF) 1659 shdr_mem.sh_link = ndx_section[sec->shdr.sh_link - 1]; 1660 if (SH_INFO_LINK_P (&sec->shdr) && sec->shdr.sh_info != 0) 1661 shdr_mem.sh_info = ndx_section[sec->shdr.sh_info - 1]; 1662 1663 if (strtab != NULL) 1664 shdr_mem.sh_name = dwelf_strent_off (sec->strent); 1665 1666 Elf_Data *indata = elf_getdata (sec->scn, NULL); 1667 ELF_CHECK (indata != NULL, _("cannot get section data: %s")); 1668 Elf_Data *outdata = elf_getdata (sec->outscn, NULL); 1669 ELF_CHECK (outdata != NULL, _("cannot copy section data: %s")); 1670 *outdata = *indata; 1671 elf_flagdata (outdata, ELF_C_SET, ELF_F_DIRTY); 1672 1673 /* Preserve the file layout of the allocated sections. */ 1674 if (stripped_ehdr->e_type != ET_REL && (shdr_mem.sh_flags & SHF_ALLOC)) 1675 { 1676 shdr_mem.sh_offset = sec->shdr.sh_offset; 1677 placed[elf_ndxscn (sec->outscn) - 1] = true; 1678 1679 const GElf_Off end_offset = (shdr_mem.sh_offset 1680 + (shdr_mem.sh_type == SHT_NOBITS 1681 ? 0 : shdr_mem.sh_size)); 1682 if (end_offset > offset) 1683 offset = end_offset; 1684 } 1685 1686 update_shdr (sec->outscn, &shdr_mem); 1687 1688 if (shdr_mem.sh_type == SHT_SYMTAB || shdr_mem.sh_type == SHT_DYNSYM) 1689 { 1690 /* We must adjust all the section indices in the symbol table. */ 1691 1692 Elf_Data *shndxdata = NULL; /* XXX */ 1693 1694 if (shdr_mem.sh_entsize == 0) 1695 error (EXIT_FAILURE, 0, 1696 "SYMTAB section cannot have zero sh_entsize"); 1697 for (size_t i = 1; i < shdr_mem.sh_size / shdr_mem.sh_entsize; ++i) 1698 { 1699 GElf_Sym sym_mem; 1700 GElf_Word shndx = SHN_UNDEF; 1701 GElf_Sym *sym = gelf_getsymshndx (outdata, shndxdata, 1702 i, &sym_mem, &shndx); 1703 ELF_CHECK (sym != NULL, 1704 _("cannot get symbol table entry: %s")); 1705 if (sym->st_shndx != SHN_XINDEX) 1706 shndx = sym->st_shndx; 1707 1708 if (shndx != SHN_UNDEF && shndx < SHN_LORESERVE) 1709 { 1710 if (shndx >= stripped_shnum) 1711 error (EXIT_FAILURE, 0, 1712 _("symbol [%zu] has invalid section index"), i); 1713 1714 shndx = ndx_section[shndx - 1]; 1715 if (shndx < SHN_LORESERVE) 1716 { 1717 sym->st_shndx = shndx; 1718 shndx = SHN_UNDEF; 1719 } 1720 else 1721 sym->st_shndx = SHN_XINDEX; 1722 1723 ELF_CHECK (gelf_update_symshndx (outdata, shndxdata, 1724 i, sym, shndx), 1725 _("cannot update symbol table: %s")); 1726 } 1727 } 1728 1729 if (shdr_mem.sh_type == SHT_SYMTAB) 1730 stripped_symtab = sec; 1731 if (shdr_mem.sh_type == SHT_DYNSYM) 1732 stripped_dynsym = sec; 1733 } 1734 1735 if (shdr_mem.sh_type == SHT_GROUP) 1736 { 1737 /* We must adjust all the section indices in the group. 1738 Skip the first word, which is the section group flag. 1739 Everything else is a section index. */ 1740 Elf32_Word *shndx = (Elf32_Word *) outdata->d_buf; 1741 for (size_t i = 1; i < shdr_mem.sh_size / sizeof (Elf32_Word); ++i) 1742 if (shndx[i] == SHN_UNDEF || shndx[i] >= stripped_shnum) 1743 error (EXIT_FAILURE, 0, 1744 _("group has invalid section index [%zd]"), i); 1745 else 1746 shndx[i] = ndx_section[shndx[i] - 1]; 1747 } 1748 } 1749 1750 /* We may need to update the symbol table. */ 1751 Elf_Data *symdata = NULL; 1752 Dwelf_Strtab *symstrtab = NULL; 1753 Elf_Data *symstrdata = NULL; 1754 if (unstripped_symtab != NULL && (stripped_symtab != NULL 1755 || check_prelink /* Section adjustments. */ 1756 || (stripped_ehdr->e_type != ET_REL 1757 && bias != 0))) 1758 { 1759 /* Merge the stripped file's symbol table into the unstripped one. */ 1760 const size_t stripped_nsym = (stripped_symtab == NULL ? 1 1761 : (stripped_symtab->shdr.sh_size 1762 / (stripped_symtab->shdr.sh_entsize == 0 1763 ? 1 1764 : stripped_symtab->shdr.sh_entsize))); 1765 1766 GElf_Shdr shdr_mem; 1767 GElf_Shdr *shdr = gelf_getshdr (unstripped_symtab, &shdr_mem); 1768 ELF_CHECK (shdr != NULL, _("cannot get section header: %s")); 1769 if (shdr->sh_entsize == 0) 1770 error (EXIT_FAILURE, 0, 1771 "unstripped SYMTAB section cannot have zero sh_entsize"); 1772 const size_t unstripped_nsym = shdr->sh_size / shdr->sh_entsize; 1773 1774 /* First collect all the symbols from both tables. */ 1775 1776 const size_t total_syms = stripped_nsym - 1 + unstripped_nsym - 1; 1777 struct symbol symbols[total_syms]; 1778 size_t symndx_map[total_syms]; 1779 1780 if (stripped_symtab != NULL) 1781 collect_symbols (unstripped, stripped_ehdr->e_type == ET_REL, 1782 stripped_symtab->scn, 1783 elf_getscn (stripped, stripped_symtab->shdr.sh_link), 1784 stripped_nsym, 0, ndx_section, 1785 symbols, symndx_map, NULL); 1786 1787 Elf_Scn *unstripped_strtab = elf_getscn (unstripped, shdr->sh_link); 1788 collect_symbols (unstripped, stripped_ehdr->e_type == ET_REL, 1789 unstripped_symtab, unstripped_strtab, unstripped_nsym, 1790 stripped_ehdr->e_type == ET_REL ? 0 : bias, NULL, 1791 &symbols[stripped_nsym - 1], 1792 &symndx_map[stripped_nsym - 1], split_bss); 1793 1794 /* Next, sort our array of all symbols. */ 1795 qsort (symbols, total_syms, sizeof symbols[0], compare_symbols); 1796 1797 /* Now we can weed out the duplicates. Assign remaining symbols 1798 new slots, collecting a map from old indices to new. */ 1799 size_t nsym = 0; 1800 for (struct symbol *s = symbols; s < &symbols[total_syms]; ++s) 1801 { 1802 /* Skip a section symbol for a removed section. */ 1803 if (s->shndx == SHN_UNDEF 1804 && GELF_ST_TYPE (s->info.info) == STT_SECTION) 1805 { 1806 s->name = NULL; /* Mark as discarded. */ 1807 *s->map = STN_UNDEF; 1808 s->duplicate = NULL; 1809 continue; 1810 } 1811 1812 struct symbol *n = s; 1813 while (n + 1 < &symbols[total_syms] && !compare_symbols (s, n + 1)) 1814 ++n; 1815 1816 while (s < n) 1817 { 1818 /* This is a duplicate. Its twin will get the next slot. */ 1819 s->name = NULL; /* Mark as discarded. */ 1820 s->duplicate = n->map; 1821 ++s; 1822 } 1823 1824 /* Allocate the next slot. */ 1825 *s->map = ++nsym; 1826 } 1827 1828 /* Now we sort again, to determine the order in the output. */ 1829 qsort (symbols, total_syms, sizeof symbols[0], compare_symbols_output); 1830 1831 if (nsym < total_syms) 1832 /* The discarded symbols are now at the end of the table. */ 1833 assert (symbols[nsym].name == NULL); 1834 1835 /* Now a final pass updates the map with the final order, 1836 and builds up the new string table. */ 1837 symstrtab = dwelf_strtab_init (true); 1838 for (size_t i = 0; i < nsym; ++i) 1839 { 1840 assert (symbols[i].name != NULL); 1841 assert (*symbols[i].map != 0); 1842 *symbols[i].map = 1 + i; 1843 symbols[i].strent = dwelf_strtab_add (symstrtab, symbols[i].name); 1844 } 1845 1846 /* Scan the discarded symbols too, just to update their slots 1847 in SYMNDX_MAP to refer to their live duplicates. */ 1848 for (size_t i = nsym; i < total_syms; ++i) 1849 { 1850 assert (symbols[i].name == NULL); 1851 if (symbols[i].duplicate == NULL) 1852 assert (*symbols[i].map == STN_UNDEF); 1853 else 1854 { 1855 assert (*symbols[i].duplicate != STN_UNDEF); 1856 *symbols[i].map = *symbols[i].duplicate; 1857 } 1858 } 1859 1860 /* Now we are ready to write the new symbol table. */ 1861 symdata = elf_getdata (unstripped_symtab, NULL); 1862 symstrdata = elf_getdata (unstripped_strtab, NULL); 1863 Elf_Data *shndxdata = NULL; /* XXX */ 1864 1865 /* If symtab and the section header table share the string table 1866 add the section names to the strtab and then (after finalizing) 1867 fixup the section header sh_names. Also dispose of the old data. */ 1868 Dwelf_Strent *unstripped_strent[unstripped_shnum - 1]; 1869 if (unstripped_shstrndx == elf_ndxscn (unstripped_strtab)) 1870 { 1871 for (size_t i = 0; i < unstripped_shnum - 1; ++i) 1872 { 1873 Elf_Scn *sec = elf_getscn (unstripped, i + 1); 1874 GElf_Shdr mem; 1875 GElf_Shdr *hdr = gelf_getshdr (sec, &mem); 1876 const char *name = get_section_name (i + 1, hdr, shstrtab); 1877 unstripped_strent[i] = dwelf_strtab_add (symstrtab, name); 1878 ELF_CHECK (unstripped_strent[i] != NULL, 1879 _("cannot add section name to string table: %s")); 1880 } 1881 1882 if (strtab != NULL) 1883 { 1884 dwelf_strtab_free (strtab); 1885 free (strtab_data->d_buf); 1886 strtab = NULL; 1887 } 1888 } 1889 1890 if (dwelf_strtab_finalize (symstrtab, symstrdata) == NULL) 1891 error (EXIT_FAILURE, 0, "Not enough memory to create symbol table"); 1892 1893 elf_flagdata (symstrdata, ELF_C_SET, ELF_F_DIRTY); 1894 1895 /* And update the section header names if necessary. */ 1896 if (unstripped_shstrndx == elf_ndxscn (unstripped_strtab)) 1897 { 1898 for (size_t i = 0; i < unstripped_shnum - 1; ++i) 1899 { 1900 Elf_Scn *sec = elf_getscn (unstripped, i + 1); 1901 GElf_Shdr mem; 1902 GElf_Shdr *hdr = gelf_getshdr (sec, &mem); 1903 shdr->sh_name = dwelf_strent_off (unstripped_strent[i]); 1904 update_shdr (sec, hdr); 1905 } 1906 } 1907 1908 /* Now update the symtab shdr. Reload symtab shdr because sh_name 1909 might have changed above. */ 1910 shdr = gelf_getshdr (unstripped_symtab, &shdr_mem); 1911 ELF_CHECK (shdr != NULL, _("cannot get section header: %s")); 1912 1913 shdr->sh_size = symdata->d_size = (1 + nsym) * shdr->sh_entsize; 1914 symdata->d_buf = xmalloc (symdata->d_size); 1915 record_new_data (symdata->d_buf); 1916 1917 GElf_Sym sym; 1918 memset (&sym, 0, sizeof sym); 1919 ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, 0, &sym, SHN_UNDEF), 1920 _("cannot update symbol table: %s")); 1921 1922 shdr->sh_info = 1; 1923 for (size_t i = 0; i < nsym; ++i) 1924 { 1925 struct symbol *s = &symbols[i]; 1926 1927 /* Fill in the symbol details. */ 1928 sym.st_name = dwelf_strent_off (s->strent); 1929 sym.st_value = s->value; /* Already biased to output address. */ 1930 sym.st_size = s->size; 1931 sym.st_shndx = s->shndx; /* Already mapped to output index. */ 1932 sym.st_info = s->info.info; 1933 sym.st_other = s->info.other; 1934 1935 /* Keep track of the number of leading local symbols. */ 1936 if (GELF_ST_BIND (sym.st_info) == STB_LOCAL) 1937 { 1938 assert (shdr->sh_info == 1 + i); 1939 shdr->sh_info = 1 + i + 1; 1940 } 1941 1942 ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, 1 + i, 1943 &sym, SHN_UNDEF), 1944 _("cannot update symbol table: %s")); 1945 1946 } 1947 elf_flagdata (symdata, ELF_C_SET, ELF_F_DIRTY); 1948 update_shdr (unstripped_symtab, shdr); 1949 1950 if (stripped_symtab != NULL) 1951 { 1952 /* Adjust any relocations referring to the old symbol table. */ 1953 const size_t old_sh_link = elf_ndxscn (stripped_symtab->scn); 1954 for (const struct section *sec = sections; 1955 sec < §ions[stripped_shnum - 1]; 1956 ++sec) 1957 if (sec->outscn != NULL && sec->shdr.sh_link == old_sh_link) 1958 adjust_relocs (sec->outscn, sec->scn, &sec->shdr, 1959 symndx_map, shdr); 1960 } 1961 1962 /* Also adjust references to the other old symbol table. */ 1963 adjust_all_relocs (unstripped, unstripped_symtab, shdr, 1964 &symndx_map[stripped_nsym - 1]); 1965 } 1966 else if (stripped_symtab != NULL && stripped_shnum != unstripped_shnum) 1967 check_symtab_section_symbols (unstripped, 1968 stripped_ehdr->e_type == ET_REL, 1969 stripped_symtab->scn, 1970 unstripped_shnum, unstripped_shstrndx, 1971 stripped_symtab->outscn, 1972 stripped_shnum, stripped_shstrndx, 1973 debuglink); 1974 1975 if (stripped_dynsym != NULL) 1976 (void) check_symtab_section_symbols (unstripped, 1977 stripped_ehdr->e_type == ET_REL, 1978 stripped_dynsym->outscn, 1979 unstripped_shnum, 1980 unstripped_shstrndx, 1981 stripped_dynsym->scn, stripped_shnum, 1982 stripped_shstrndx, debuglink); 1983 1984 /* We need to preserve the layout of the stripped file so the 1985 phdrs will match up. This requires us to do our own layout of 1986 the added sections. We do manual layout even for ET_REL just 1987 so we can try to match what the original probably had. */ 1988 1989 elf_flagelf (unstripped, ELF_C_SET, ELF_F_LAYOUT); 1990 1991 if (offset == 0) 1992 /* For ET_REL we are starting the layout from scratch. */ 1993 offset = gelf_fsize (unstripped, ELF_T_EHDR, 1, EV_CURRENT); 1994 1995 bool skip_reloc = false; 1996 do 1997 { 1998 skip_reloc = !skip_reloc; 1999 for (size_t i = 0; i < unstripped_shnum - 1; ++i) 2000 if (!placed[i]) 2001 { 2002 scn = elf_getscn (unstripped, 1 + i); 2003 2004 GElf_Shdr shdr_mem; 2005 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 2006 ELF_CHECK (shdr != NULL, _("cannot get section header: %s")); 2007 2008 /* We must make sure we have read in the data of all sections 2009 beforehand and marked them to be written out. When we're 2010 modifying the existing file in place, we might overwrite 2011 this part of the file before we get to handling the section. */ 2012 2013 ELF_CHECK (elf_flagdata (elf_getdata (scn, NULL), 2014 ELF_C_SET, ELF_F_DIRTY), 2015 _("cannot read section data: %s")); 2016 2017 if (skip_reloc 2018 && (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA)) 2019 continue; 2020 2021 GElf_Off align = shdr->sh_addralign ?: 1; 2022 offset = (offset + align - 1) & -align; 2023 shdr->sh_offset = offset; 2024 if (shdr->sh_type != SHT_NOBITS) 2025 offset += shdr->sh_size; 2026 2027 update_shdr (scn, shdr); 2028 2029 if (unstripped_shstrndx == 1 + i) 2030 { 2031 /* Place the section headers immediately after 2032 .shstrtab, and update the ELF header. */ 2033 2034 GElf_Ehdr ehdr_mem; 2035 GElf_Ehdr *ehdr = gelf_getehdr (unstripped, &ehdr_mem); 2036 ELF_CHECK (ehdr != NULL, _("cannot get ELF header: %s")); 2037 2038 GElf_Off sh_align = gelf_getclass (unstripped) * 4; 2039 offset = (offset + sh_align - 1) & -sh_align; 2040 ehdr->e_shnum = unstripped_shnum; 2041 ehdr->e_shoff = offset; 2042 offset += unstripped_shnum * ehdr->e_shentsize; 2043 ELF_CHECK (gelf_update_ehdr (unstripped, ehdr), 2044 _("cannot update ELF header: %s")); 2045 } 2046 2047 placed[i] = true; 2048 } 2049 } 2050 while (skip_reloc); 2051 2052 size_t phnum; 2053 ELF_CHECK (elf_getphdrnum (stripped, &phnum) == 0, 2054 _("cannot get number of program headers: %s")); 2055 2056 if (phnum > 0) 2057 ELF_CHECK (gelf_newphdr (unstripped, phnum), 2058 _("cannot create program headers: %s")); 2059 2060 /* Copy each program header from the stripped file. */ 2061 for (size_t i = 0; i < phnum; ++i) 2062 { 2063 GElf_Phdr phdr_mem; 2064 GElf_Phdr *phdr = gelf_getphdr (stripped, i, &phdr_mem); 2065 ELF_CHECK (phdr != NULL, _("cannot get program header: %s")); 2066 2067 ELF_CHECK (gelf_update_phdr (unstripped, i, phdr), 2068 _("cannot update program header: %s")); 2069 } 2070 2071 /* Finally, write out the file. */ 2072 ELF_CHECK (elf_update (unstripped, ELF_C_WRITE) > 0, 2073 _("cannot write output file: %s")); 2074 2075 if (strtab != NULL) 2076 { 2077 dwelf_strtab_free (strtab); 2078 free (strtab_data->d_buf); 2079 } 2080 2081 if (symstrtab != NULL) 2082 { 2083 dwelf_strtab_free (symstrtab); 2084 free (symstrdata->d_buf); 2085 } 2086 free_new_data (); 2087 } 2088 2089 /* Process one pair of files, already opened. */ 2090 static void 2091 handle_file (const char *output_file, bool create_dirs, 2092 Elf *stripped, const GElf_Ehdr *stripped_ehdr, 2093 Elf *unstripped) 2094 { 2095 size_t phnum; 2096 ELF_CHECK (elf_getphdrnum (stripped, &phnum) == 0, 2097 _("cannot get number of program headers: %s")); 2098 2099 /* Determine the address bias between the debuginfo file and the main 2100 file, which may have been modified by prelinking. */ 2101 GElf_Addr bias = 0; 2102 if (unstripped != NULL) 2103 for (size_t i = 0; i < phnum; ++i) 2104 { 2105 GElf_Phdr phdr_mem; 2106 GElf_Phdr *phdr = gelf_getphdr (stripped, i, &phdr_mem); 2107 ELF_CHECK (phdr != NULL, _("cannot get program header: %s")); 2108 if (phdr->p_type == PT_LOAD) 2109 { 2110 GElf_Phdr unstripped_phdr_mem; 2111 GElf_Phdr *unstripped_phdr = gelf_getphdr (unstripped, i, 2112 &unstripped_phdr_mem); 2113 ELF_CHECK (unstripped_phdr != NULL, 2114 _("cannot get program header: %s")); 2115 bias = phdr->p_vaddr - unstripped_phdr->p_vaddr; 2116 break; 2117 } 2118 } 2119 2120 /* One day we could adjust all the DWARF data (like prelink itself does). */ 2121 if (bias != 0) 2122 { 2123 if (output_file == NULL) 2124 error (0, 0, _("\ 2125 DWARF data not adjusted for prelinking bias; consider prelink -u")); 2126 else 2127 error (0, 0, _("\ 2128 DWARF data in '%s' not adjusted for prelinking bias; consider prelink -u"), 2129 output_file); 2130 } 2131 2132 if (output_file == NULL) 2133 /* Modify the unstripped file in place. */ 2134 copy_elided_sections (unstripped, stripped, stripped_ehdr, bias); 2135 else 2136 { 2137 if (create_dirs) 2138 make_directories (output_file); 2139 2140 /* Copy the unstripped file and then modify it. */ 2141 int outfd = open (output_file, O_RDWR | O_CREAT, 2142 stripped_ehdr->e_type == ET_REL ? 0666 : 0777); 2143 if (outfd < 0) 2144 error (EXIT_FAILURE, errno, _("cannot open '%s'"), output_file); 2145 Elf *outelf = elf_begin (outfd, ELF_C_WRITE, NULL); 2146 ELF_CHECK (outelf != NULL, _("cannot create ELF descriptor: %s")); 2147 2148 if (unstripped == NULL) 2149 { 2150 /* Actually, we are just copying out the main file as it is. */ 2151 copy_elf (outelf, stripped); 2152 if (stripped_ehdr->e_type != ET_REL) 2153 elf_flagelf (outelf, ELF_C_SET, ELF_F_LAYOUT); 2154 ELF_CHECK (elf_update (outelf, ELF_C_WRITE) > 0, 2155 _("cannot write output file: %s")); 2156 } 2157 else 2158 { 2159 copy_elf (outelf, unstripped); 2160 copy_elided_sections (outelf, stripped, stripped_ehdr, bias); 2161 } 2162 2163 elf_end (outelf); 2164 close (outfd); 2165 } 2166 } 2167 2168 static int 2169 open_file (const char *file, bool writable) 2170 { 2171 int fd = open (file, writable ? O_RDWR : O_RDONLY); 2172 if (fd < 0) 2173 error (EXIT_FAILURE, errno, _("cannot open '%s'"), file); 2174 return fd; 2175 } 2176 2177 /* Handle a pair of files we need to open by name. */ 2178 static void 2179 handle_explicit_files (const char *output_file, bool create_dirs, bool force, 2180 const char *stripped_file, const char *unstripped_file) 2181 { 2182 2183 /* Warn, and exit if not forced to continue, if some ELF header 2184 sanity check for the stripped and unstripped files failed. */ 2185 void warn (const char *msg) 2186 { 2187 error (force ? 0 : EXIT_FAILURE, 0, "%s'%s' and '%s' %s%s.", 2188 force ? _("WARNING: ") : "", 2189 stripped_file, unstripped_file, msg, 2190 force ? "" : _(", use --force")); 2191 } 2192 2193 int stripped_fd = open_file (stripped_file, false); 2194 Elf *stripped = elf_begin (stripped_fd, ELF_C_READ, NULL); 2195 GElf_Ehdr stripped_ehdr; 2196 ELF_CHECK (gelf_getehdr (stripped, &stripped_ehdr), 2197 _("cannot create ELF descriptor: %s")); 2198 2199 int unstripped_fd = -1; 2200 Elf *unstripped = NULL; 2201 if (unstripped_file != NULL) 2202 { 2203 unstripped_fd = open_file (unstripped_file, output_file == NULL); 2204 unstripped = elf_begin (unstripped_fd, 2205 (output_file == NULL ? ELF_C_RDWR : ELF_C_READ), 2206 NULL); 2207 GElf_Ehdr unstripped_ehdr; 2208 ELF_CHECK (gelf_getehdr (unstripped, &unstripped_ehdr), 2209 _("cannot create ELF descriptor: %s")); 2210 2211 if (memcmp (stripped_ehdr.e_ident, 2212 unstripped_ehdr.e_ident, EI_NIDENT) != 0) 2213 warn (_("ELF header identification (e_ident) different")); 2214 2215 if (stripped_ehdr.e_type != unstripped_ehdr.e_type) 2216 warn (_("ELF header type (e_type) different")); 2217 2218 if (stripped_ehdr.e_machine != unstripped_ehdr.e_machine) 2219 warn (_("ELF header machine type (e_machine) different")); 2220 2221 if (stripped_ehdr.e_phnum < unstripped_ehdr.e_phnum) 2222 warn (_("stripped program header (e_phnum) smaller than unstripped")); 2223 } 2224 2225 handle_file (output_file, create_dirs, stripped, &stripped_ehdr, unstripped); 2226 2227 elf_end (stripped); 2228 close (stripped_fd); 2229 2230 elf_end (unstripped); 2231 close (unstripped_fd); 2232 } 2233 2234 2235 /* Handle a pair of files opened implicitly by libdwfl for one module. */ 2236 static void 2237 handle_dwfl_module (const char *output_file, bool create_dirs, bool force, 2238 Dwfl_Module *mod, bool all, bool ignore, bool relocate) 2239 { 2240 GElf_Addr bias; 2241 Elf *stripped = dwfl_module_getelf (mod, &bias); 2242 if (stripped == NULL) 2243 { 2244 if (ignore) 2245 return; 2246 2247 const char *file; 2248 const char *modname = dwfl_module_info (mod, NULL, NULL, NULL, 2249 NULL, NULL, &file, NULL); 2250 if (file == NULL) 2251 error (EXIT_FAILURE, 0, 2252 _("cannot find stripped file for module '%s': %s"), 2253 modname, dwfl_errmsg (-1)); 2254 else 2255 error (EXIT_FAILURE, 0, 2256 _("cannot open stripped file '%s' for module '%s': %s"), 2257 modname, file, dwfl_errmsg (-1)); 2258 } 2259 2260 Elf *debug = dwarf_getelf (dwfl_module_getdwarf (mod, &bias)); 2261 if (debug == NULL && !all) 2262 { 2263 if (ignore) 2264 return; 2265 2266 const char *file; 2267 const char *modname = dwfl_module_info (mod, NULL, NULL, NULL, 2268 NULL, NULL, NULL, &file); 2269 if (file == NULL) 2270 error (EXIT_FAILURE, 0, 2271 _("cannot find debug file for module '%s': %s"), 2272 modname, dwfl_errmsg (-1)); 2273 else 2274 error (EXIT_FAILURE, 0, 2275 _("cannot open debug file '%s' for module '%s': %s"), 2276 modname, file, dwfl_errmsg (-1)); 2277 } 2278 2279 if (debug == stripped) 2280 { 2281 if (all) 2282 debug = NULL; 2283 else 2284 { 2285 const char *file; 2286 const char *modname = dwfl_module_info (mod, NULL, NULL, NULL, 2287 NULL, NULL, &file, NULL); 2288 error (EXIT_FAILURE, 0, _("module '%s' file '%s' is not stripped"), 2289 modname, file); 2290 } 2291 } 2292 2293 GElf_Ehdr stripped_ehdr; 2294 ELF_CHECK (gelf_getehdr (stripped, &stripped_ehdr), 2295 _("cannot create ELF descriptor: %s")); 2296 2297 if (stripped_ehdr.e_type == ET_REL) 2298 { 2299 if (!relocate) 2300 { 2301 /* We can't use the Elf handles already open, 2302 because the DWARF sections have been relocated. */ 2303 2304 const char *stripped_file = NULL; 2305 const char *unstripped_file = NULL; 2306 (void) dwfl_module_info (mod, NULL, NULL, NULL, NULL, NULL, 2307 &stripped_file, &unstripped_file); 2308 2309 handle_explicit_files (output_file, create_dirs, force, 2310 stripped_file, unstripped_file); 2311 return; 2312 } 2313 2314 /* Relocation is what we want! This ensures that all sections that can 2315 get sh_addr values assigned have them, even ones not used in DWARF. 2316 They might still be used in the symbol table. */ 2317 if (dwfl_module_relocations (mod) < 0) 2318 error (EXIT_FAILURE, 0, 2319 _("cannot cache section addresses for module '%s': %s"), 2320 dwfl_module_info (mod, NULL, NULL, NULL, NULL, NULL, NULL, NULL), 2321 dwfl_errmsg (-1)); 2322 } 2323 2324 handle_file (output_file, create_dirs, stripped, &stripped_ehdr, debug); 2325 } 2326 2327 /* Handle one module being written to the output directory. */ 2328 static void 2329 handle_output_dir_module (const char *output_dir, Dwfl_Module *mod, bool force, 2330 bool all, bool ignore, bool modnames, bool relocate) 2331 { 2332 if (! modnames) 2333 { 2334 /* Make sure we've searched for the ELF file. */ 2335 GElf_Addr bias; 2336 (void) dwfl_module_getelf (mod, &bias); 2337 } 2338 2339 const char *file; 2340 const char *name = dwfl_module_info (mod, NULL, NULL, NULL, 2341 NULL, NULL, &file, NULL); 2342 2343 if (file == NULL && ignore) 2344 return; 2345 2346 char *output_file; 2347 if (asprintf (&output_file, "%s/%s", output_dir, modnames ? name : file) < 0) 2348 error (EXIT_FAILURE, 0, _("memory exhausted")); 2349 2350 handle_dwfl_module (output_file, true, force, mod, all, ignore, relocate); 2351 } 2352 2353 2354 static void 2355 list_module (Dwfl_Module *mod) 2356 { 2357 /* Make sure we have searched for the files. */ 2358 GElf_Addr bias; 2359 bool have_elf = dwfl_module_getelf (mod, &bias) != NULL; 2360 bool have_dwarf = dwfl_module_getdwarf (mod, &bias) != NULL; 2361 2362 const char *file; 2363 const char *debug; 2364 Dwarf_Addr start; 2365 Dwarf_Addr end; 2366 const char *name = dwfl_module_info (mod, NULL, &start, &end, 2367 NULL, NULL, &file, &debug); 2368 if (file != NULL && debug != NULL && (debug == file || !strcmp (debug, file))) 2369 debug = "."; 2370 2371 const unsigned char *id; 2372 GElf_Addr id_vaddr; 2373 int id_len = dwfl_module_build_id (mod, &id, &id_vaddr); 2374 2375 printf ("%#" PRIx64 "+%#" PRIx64 " ", start, end - start); 2376 2377 if (id_len > 0) 2378 { 2379 do 2380 printf ("%02" PRIx8, *id++); 2381 while (--id_len > 0); 2382 if (id_vaddr != 0) 2383 printf ("@%#" PRIx64, id_vaddr); 2384 } 2385 else 2386 putchar ('-'); 2387 2388 printf (" %s %s %s\n", 2389 file ?: have_elf ? "." : "-", 2390 debug ?: have_dwarf ? "." : "-", 2391 name); 2392 } 2393 2394 2395 struct match_module_info 2396 { 2397 char **patterns; 2398 Dwfl_Module *found; 2399 bool match_files; 2400 }; 2401 2402 static int 2403 match_module (Dwfl_Module *mod, 2404 void **userdata __attribute__ ((unused)), 2405 const char *name, 2406 Dwarf_Addr start __attribute__ ((unused)), 2407 void *arg) 2408 { 2409 struct match_module_info *info = arg; 2410 2411 if (info->patterns[0] == NULL) /* Match all. */ 2412 { 2413 match: 2414 info->found = mod; 2415 return DWARF_CB_ABORT; 2416 } 2417 2418 if (info->match_files) 2419 { 2420 /* Make sure we've searched for the ELF file. */ 2421 GElf_Addr bias; 2422 (void) dwfl_module_getelf (mod, &bias); 2423 2424 const char *file; 2425 const char *check = dwfl_module_info (mod, NULL, NULL, NULL, 2426 NULL, NULL, &file, NULL); 2427 assert (check == name); 2428 if (file == NULL) 2429 return DWARF_CB_OK; 2430 2431 name = file; 2432 } 2433 2434 for (char **p = info->patterns; *p != NULL; ++p) 2435 if (fnmatch (*p, name, 0) == 0) 2436 goto match; 2437 2438 return DWARF_CB_OK; 2439 } 2440 2441 /* Handle files opened implicitly via libdwfl. */ 2442 static void 2443 handle_implicit_modules (const struct arg_info *info) 2444 { 2445 struct match_module_info mmi = { info->args, NULL, info->match_files }; 2446 inline ptrdiff_t next (ptrdiff_t offset) 2447 { 2448 return dwfl_getmodules (info->dwfl, &match_module, &mmi, offset); 2449 } 2450 ptrdiff_t offset = next (0); 2451 if (offset == 0) 2452 error (EXIT_FAILURE, 0, _("no matching modules found")); 2453 2454 if (info->list) 2455 do 2456 list_module (mmi.found); 2457 while ((offset = next (offset)) > 0); 2458 else if (info->output_dir == NULL) 2459 { 2460 if (next (offset) != 0) 2461 error (EXIT_FAILURE, 0, _("matched more than one module")); 2462 handle_dwfl_module (info->output_file, false, info->force, mmi.found, 2463 info->all, info->ignore, info->relocate); 2464 } 2465 else 2466 do 2467 handle_output_dir_module (info->output_dir, mmi.found, info->force, 2468 info->all, info->ignore, 2469 info->modnames, info->relocate); 2470 while ((offset = next (offset)) > 0); 2471 } 2472 2473 int 2475 main (int argc, char **argv) 2476 { 2477 /* We use no threads here which can interfere with handling a stream. */ 2478 __fsetlocking (stdin, FSETLOCKING_BYCALLER); 2479 __fsetlocking (stdout, FSETLOCKING_BYCALLER); 2480 __fsetlocking (stderr, FSETLOCKING_BYCALLER); 2481 2482 /* Set locale. */ 2483 setlocale (LC_ALL, ""); 2484 2485 /* Make sure the message catalog can be found. */ 2486 bindtextdomain (PACKAGE_TARNAME, LOCALEDIR); 2487 2488 /* Initialize the message catalog. */ 2489 textdomain (PACKAGE_TARNAME); 2490 2491 /* Parse and process arguments. */ 2492 const struct argp_child argp_children[] = 2493 { 2494 { 2495 .argp = dwfl_standard_argp (), 2496 .header = N_("Input selection options:"), 2497 .group = 1, 2498 }, 2499 { .argp = NULL }, 2500 }; 2501 const struct argp argp = 2502 { 2503 .options = options, 2504 .parser = parse_opt, 2505 .children = argp_children, 2506 .args_doc = N_("STRIPPED-FILE DEBUG-FILE\n[MODULE...]"), 2507 .doc = N_("\ 2508 Combine stripped files with separate symbols and debug information.\n\ 2509 \n\ 2510 The first form puts the result in DEBUG-FILE if -o was not given.\n\ 2511 \n\ 2512 MODULE arguments give file name patterns matching modules to process.\n\ 2513 With -f these match the file name of the main (stripped) file \ 2514 (slashes are never special), otherwise they match the simple module names. \ 2515 With no arguments, process all modules found.\n\ 2516 \n\ 2517 Multiple modules are written to files under OUTPUT-DIRECTORY, \ 2518 creating subdirectories as needed. \ 2519 With -m these files have simple module names, otherwise they have the \ 2520 name of the main file complete with directory underneath OUTPUT-DIRECTORY.\n\ 2521 \n\ 2522 With -n no files are written, but one line to standard output for each module:\ 2523 \n\tSTART+SIZE BUILDID FILE DEBUGFILE MODULENAME\n\ 2524 START and SIZE are hexadecimal giving the address bounds of the module. \ 2525 BUILDID is hexadecimal for the build ID bits, or - if no ID is known; \ 2526 the hexadecimal may be followed by @0xADDR giving the address where the \ 2527 ID resides if that is known. \ 2528 FILE is the file name found for the module, or - if none was found, \ 2529 or . if an ELF image is available but not from any named file. \ 2530 DEBUGFILE is the separate debuginfo file name, \ 2531 or - if no debuginfo was found, or . if FILE contains the debug information.\ 2532 ") 2533 }; 2534 2535 int remaining; 2536 struct arg_info info = { .args = NULL }; 2537 error_t result = argp_parse (&argp, argc, argv, 0, &remaining, &info); 2538 if (result == ENOSYS) 2539 assert (info.dwfl == NULL); 2540 else if (result) 2541 return EXIT_FAILURE; 2542 assert (info.args != NULL); 2543 2544 /* Tell the library which version we are expecting. */ 2545 elf_version (EV_CURRENT); 2546 2547 if (info.dwfl == NULL) 2548 { 2549 assert (result == ENOSYS); 2550 2551 if (info.output_dir != NULL) 2552 { 2553 char *file; 2554 if (asprintf (&file, "%s/%s", info.output_dir, info.args[0]) < 0) 2555 error (EXIT_FAILURE, 0, _("memory exhausted")); 2556 handle_explicit_files (file, true, info.force, 2557 info.args[0], info.args[1]); 2558 free (file); 2559 } 2560 else 2561 handle_explicit_files (info.output_file, false, info.force, 2562 info.args[0], info.args[1]); 2563 } 2564 else 2565 { 2566 /* parse_opt checked this. */ 2567 assert (info.output_file != NULL || info.output_dir != NULL || info.list); 2568 2569 handle_implicit_modules (&info); 2570 2571 dwfl_end (info.dwfl); 2572 } 2573 2574 return 0; 2575 } 2576 2577 2578 #include "debugpred.h" 2579