1 /* Combine stripped files with separate symbols and debug information.
2 Copyright (C) 2007-2012 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Roland McGrath <roland@redhat.com>, 2007.
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.
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.
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/>. */
23 * prelink vs .debug_* linked addresses
42 #include <stdio_ext.h>
55 # define _(str) gettext (str)
58 /* Name and version of program. */
59 static void print_version (FILE *stream, struct argp_state *state);
60 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
62 /* Bug report address. */
63 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
65 /* Definitions of arguments for argp functions. */
66 static const struct argp_option options[] =
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 },
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"),
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 { NULL, 0, NULL, 0, NULL, 0 }
90 const char *output_file;
91 const char *output_dir;
102 /* Handle program arguments. */
104 parse_opt (int key, char *arg, struct argp_state *state)
106 struct arg_info *info = state->input;
111 state->child_inputs[0] = &info->dwfl;
115 if (info->output_file != NULL)
117 argp_error (state, _("-o option specified twice"));
120 info->output_file = arg;
124 if (info->output_dir != NULL)
126 argp_error (state, _("-d option specified twice"));
129 info->output_dir = arg;
133 info->modnames = true;
136 info->match_files = true;
148 info->relocate = true;
152 case ARGP_KEY_NO_ARGS:
153 /* We "consume" all the arguments here. */
154 info->args = &state->argv[state->next];
156 if (info->output_file != NULL && info->output_dir != NULL)
158 argp_error (state, _("only one of -o or -d allowed"));
162 if (info->list && (info->dwfl == NULL
163 || info->output_dir != NULL
164 || info->output_file != NULL))
167 _("-n cannot be used with explicit files or -o or -d"));
171 if (info->output_dir != NULL)
175 if (stat64 (info->output_dir, &st) < 0)
177 else if (!S_ISDIR (st.st_mode))
181 argp_failure (state, EXIT_FAILURE, fail,
182 _("output directory '%s'"), info->output_dir);
187 if (info->dwfl == NULL)
189 if (state->next + 2 != state->argc)
191 argp_error (state, _("exactly two file arguments are required"));
195 if (info->ignore || info->all || info->modnames || info->relocate)
197 argp_error (state, _("\
198 -m, -a, -R, and -i options not allowed with explicit files"));
202 /* Bail out immediately to prevent dwfl_standard_argp's parser
203 from defaulting to "-e a.out". */
206 else if (info->output_file == NULL && info->output_dir == NULL
210 _("-o or -d is required when using implicit files"));
216 return ARGP_ERR_UNKNOWN;
221 /* Print the version information. */
223 print_version (FILE *stream, struct argp_state *state __attribute__ ((unused)))
225 fprintf (stream, "unstrip (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION);
226 fprintf (stream, _("\
227 Copyright (C) %s Red Hat, Inc.\n\
228 This is free software; see the source for copying conditions. There is NO\n\
229 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
231 fprintf (stream, gettext ("Written by %s.\n"), "Roland McGrath");
234 #define ELF_CHECK(call, msg) \
238 error (EXIT_FAILURE, 0, msg, elf_errmsg (-1)); \
241 /* Copy INELF to newly-created OUTELF, exit via error for any problems. */
243 copy_elf (Elf *outelf, Elf *inelf)
245 ELF_CHECK (gelf_newehdr (outelf, gelf_getclass (inelf)),
246 _("cannot create ELF header: %s"));
249 GElf_Ehdr *ehdr = gelf_getehdr (inelf, &ehdr_mem);
250 ELF_CHECK (gelf_update_ehdr (outelf, ehdr),
251 _("cannot copy ELF header: %s"));
253 if (ehdr->e_phnum > 0)
255 ELF_CHECK (gelf_newphdr (outelf, ehdr->e_phnum),
256 _("cannot create program headers: %s"));
259 for (uint_fast16_t i = 0; i < ehdr->e_phnum; ++i)
260 ELF_CHECK (gelf_update_phdr (outelf, i,
261 gelf_getphdr (inelf, i, &phdr_mem)),
262 _("cannot copy program header: %s"));
266 while ((scn = elf_nextscn (inelf, scn)) != NULL)
268 Elf_Scn *newscn = elf_newscn (outelf);
271 ELF_CHECK (gelf_update_shdr (newscn, gelf_getshdr (scn, &shdr_mem)),
272 _("cannot copy section header: %s"));
274 Elf_Data *data = elf_getdata (scn, NULL);
275 ELF_CHECK (data != NULL, _("cannot get section data: %s"));
276 Elf_Data *newdata = elf_newdata (newscn);
277 ELF_CHECK (newdata != NULL, _("cannot copy section data: %s"));
279 elf_flagdata (newdata, ELF_C_SET, ELF_F_DIRTY);
283 /* Create directories containing PATH. */
285 make_directories (const char *path)
287 const char *lastslash = strrchr (path, '/');
288 if (lastslash == NULL)
291 while (lastslash > path && lastslash[-1] == '/')
293 if (lastslash == path)
296 char *dir = strndupa (path, lastslash - path);
297 while (mkdir (dir, 0777) < 0 && errno != EEXIST)
299 make_directories (dir);
301 error (EXIT_FAILURE, errno, _("cannot create directory '%s'"), dir);
305 /* The binutils linker leaves gratuitous section symbols in .symtab
306 that strip has to remove. Older linkers likewise include a
307 symbol for every section, even unallocated ones, in .dynsym.
308 Because of this, the related sections can shrink in the stripped
309 file from their original size. Older versions of strip do not
310 adjust the sh_size field in the debuginfo file's SHT_NOBITS
311 version of the section header, so it can appear larger. */
313 section_can_shrink (const GElf_Shdr *shdr)
315 switch (shdr->sh_type)
326 /* See if this symbol table has a leading section symbol for every single
327 section, in order. The binutils linker produces this. While we're here,
328 update each section symbol's st_value. */
330 symtab_count_leading_section_symbols (Elf *elf, Elf_Scn *scn, size_t shnum,
331 Elf_Data *newsymdata)
333 Elf_Data *data = elf_getdata (scn, NULL);
334 Elf_Data *shndxdata = NULL; /* XXX */
336 for (size_t i = 1; i < shnum; ++i)
339 GElf_Word shndx = SHN_UNDEF;
340 GElf_Sym *sym = gelf_getsymshndx (data, shndxdata, i, &sym_mem, &shndx);
341 ELF_CHECK (sym != NULL, _("cannot get symbol table entry: %s"));
344 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (elf, i), &shdr_mem);
345 ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
347 if (sym->st_shndx != SHN_XINDEX)
348 shndx = sym->st_shndx;
350 if (shndx != i || GELF_ST_TYPE (sym->st_info) != STT_SECTION)
353 sym->st_value = shdr->sh_addr;
354 if (sym->st_shndx != SHN_XINDEX)
356 ELF_CHECK (gelf_update_symshndx (newsymdata, shndxdata, i, sym, shndx),
357 _("cannot update symbol table: %s"));
364 update_shdr (Elf_Scn *outscn, GElf_Shdr *newshdr)
366 ELF_CHECK (gelf_update_shdr (outscn, newshdr),
367 _("cannot update section header: %s"));
370 /* We expanded the output section, so update its header. */
372 update_sh_size (Elf_Scn *outscn, const Elf_Data *data)
375 GElf_Shdr *newshdr = gelf_getshdr (outscn, &shdr_mem);
376 ELF_CHECK (newshdr != NULL, _("cannot get section header: %s"));
378 newshdr->sh_size = data->d_size;
380 update_shdr (outscn, newshdr);
383 /* Update relocation sections using the symbol table. */
385 adjust_relocs (Elf_Scn *outscn, Elf_Scn *inscn, const GElf_Shdr *shdr,
386 size_t map[], const GElf_Shdr *symshdr)
388 Elf_Data *data = elf_getdata (outscn, NULL);
390 inline void adjust_reloc (GElf_Xword *info)
392 size_t ndx = GELF_R_SYM (*info);
393 if (ndx != STN_UNDEF)
394 *info = GELF_R_INFO (map[ndx - 1], GELF_R_TYPE (*info));
397 switch (shdr->sh_type)
400 for (size_t i = 0; i < shdr->sh_size / shdr->sh_entsize; ++i)
403 GElf_Rel *rel = gelf_getrel (data, i, &rel_mem);
404 adjust_reloc (&rel->r_info);
405 ELF_CHECK (gelf_update_rel (data, i, rel),
406 _("cannot update relocation: %s"));
411 for (size_t i = 0; i < shdr->sh_size / shdr->sh_entsize; ++i)
414 GElf_Rela *rela = gelf_getrela (data, i, &rela_mem);
415 adjust_reloc (&rela->r_info);
416 ELF_CHECK (gelf_update_rela (data, i, rela),
417 _("cannot update relocation: %s"));
424 GElf_Shdr *newshdr = gelf_getshdr (outscn, &shdr_mem);
425 ELF_CHECK (newshdr != NULL, _("cannot get section header: %s"));
426 if (newshdr->sh_info != STN_UNDEF)
428 newshdr->sh_info = map[newshdr->sh_info - 1];
429 update_shdr (outscn, newshdr);
435 /* We must expand the table and rejigger its contents. */
437 const size_t nsym = symshdr->sh_size / symshdr->sh_entsize;
438 const size_t onent = shdr->sh_size / shdr->sh_entsize;
439 assert (data->d_size == shdr->sh_size);
441 #define CONVERT_HASH(Hash_Word) \
443 const Hash_Word *const old_hash = data->d_buf; \
444 const size_t nbucket = old_hash[0]; \
445 const size_t nchain = old_hash[1]; \
446 const Hash_Word *const old_bucket = &old_hash[2]; \
447 const Hash_Word *const old_chain = &old_bucket[nbucket]; \
448 assert (onent == 2 + nbucket + nchain); \
450 const size_t nent = 2 + nbucket + nsym; \
451 Hash_Word *const new_hash = xcalloc (nent, sizeof new_hash[0]); \
452 Hash_Word *const new_bucket = &new_hash[2]; \
453 Hash_Word *const new_chain = &new_bucket[nbucket]; \
455 new_hash[0] = nbucket; \
456 new_hash[1] = nsym; \
457 for (size_t i = 0; i < nbucket; ++i) \
458 if (old_bucket[i] != STN_UNDEF) \
459 new_bucket[i] = map[old_bucket[i] - 1]; \
461 for (size_t i = 1; i < nchain; ++i) \
462 if (old_chain[i] != STN_UNDEF) \
463 new_chain[map[i - 1]] = map[old_chain[i] - 1]; \
465 data->d_buf = new_hash; \
466 data->d_size = nent * sizeof new_hash[0]; \
469 switch (shdr->sh_entsize)
472 CONVERT_HASH (Elf32_Word);
475 CONVERT_HASH (Elf64_Xword);
481 elf_flagdata (data, ELF_C_SET, ELF_F_DIRTY);
482 update_sh_size (outscn, data);
489 /* We must expand the table and move its elements around. */
491 const size_t nent = symshdr->sh_size / symshdr->sh_entsize;
492 const size_t onent = shdr->sh_size / shdr->sh_entsize;
493 assert (nent >= onent);
495 /* We don't bother using gelf_update_versym because there is
496 really no conversion to be done. */
497 assert (sizeof (Elf32_Versym) == sizeof (GElf_Versym));
498 assert (sizeof (Elf64_Versym) == sizeof (GElf_Versym));
499 GElf_Versym *versym = xcalloc (nent, sizeof versym[0]);
501 for (size_t i = 1; i < onent; ++i)
503 GElf_Versym *v = gelf_getversym (data, i, &versym[map[i - 1]]);
504 ELF_CHECK (v != NULL, _("cannot get symbol version: %s"));
507 data->d_buf = versym;
508 data->d_size = nent * shdr->sh_entsize;
509 elf_flagdata (data, ELF_C_SET, ELF_F_DIRTY);
510 update_sh_size (outscn, data);
515 error (EXIT_FAILURE, 0,
516 _("unexpected section type in [%Zu] with sh_link to symtab"),
521 /* Adjust all the relocation sections in the file. */
523 adjust_all_relocs (Elf *elf, Elf_Scn *symtab, const GElf_Shdr *symshdr,
526 size_t new_sh_link = elf_ndxscn (symtab);
528 while ((scn = elf_nextscn (elf, scn)) != NULL)
532 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
533 ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
534 if (shdr->sh_type != SHT_NOBITS && shdr->sh_link == new_sh_link)
535 adjust_relocs (scn, scn, shdr, map, symshdr);
539 /* The original file probably had section symbols for all of its
540 sections, even the unallocated ones. To match it as closely as
541 possible, add in section symbols for the added sections. */
543 add_new_section_symbols (Elf_Scn *old_symscn, size_t old_shnum,
544 Elf *elf, bool rel, Elf_Scn *symscn, size_t shnum)
546 const size_t added = shnum - old_shnum;
549 GElf_Shdr *shdr = gelf_getshdr (symscn, &shdr_mem);
550 ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
552 const size_t nsym = shdr->sh_size / shdr->sh_entsize;
553 size_t symndx_map[nsym - 1];
555 shdr->sh_info += added;
556 shdr->sh_size += added * shdr->sh_entsize;
557 update_shdr (symscn, shdr);
559 Elf_Data *symdata = elf_getdata (symscn, NULL);
560 Elf_Data *shndxdata = NULL; /* XXX */
562 symdata->d_size = shdr->sh_size;
563 symdata->d_buf = xmalloc (symdata->d_size);
565 /* Copy the existing section symbols. */
566 Elf_Data *old_symdata = elf_getdata (old_symscn, NULL);
567 for (size_t i = 0; i < old_shnum; ++i)
570 GElf_Word shndx = SHN_UNDEF;
571 GElf_Sym *sym = gelf_getsymshndx (old_symdata, shndxdata,
572 i, &sym_mem, &shndx);
573 ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, i,
575 _("cannot update symbol table: %s"));
578 symndx_map[i - 1] = i;
581 /* Add in the new section symbols. */
582 for (size_t i = old_shnum; i < shnum; ++i)
584 GElf_Shdr i_shdr_mem;
585 GElf_Shdr *i_shdr = gelf_getshdr (elf_getscn (elf, i), &i_shdr_mem);
586 ELF_CHECK (i_shdr != NULL, _("cannot get section header: %s"));
589 .st_value = rel ? 0 : i_shdr->sh_addr,
590 .st_info = GELF_ST_INFO (STB_LOCAL, STT_SECTION),
591 .st_shndx = i < SHN_LORESERVE ? i : SHN_XINDEX
593 GElf_Word shndx = i < SHN_LORESERVE ? SHN_UNDEF : i;
594 ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, i,
596 _("cannot update symbol table: %s"));
599 /* Now copy the rest of the existing symbols. */
600 for (size_t i = old_shnum; i < nsym; ++i)
603 GElf_Word shndx = SHN_UNDEF;
604 GElf_Sym *sym = gelf_getsymshndx (old_symdata, shndxdata,
605 i, &sym_mem, &shndx);
606 ELF_CHECK (gelf_update_symshndx (symdata, shndxdata,
607 i + added, sym, shndx),
608 _("cannot update symbol table: %s"));
610 symndx_map[i - 1] = i + added;
613 /* Adjust any relocations referring to the old symbol table. */
614 adjust_all_relocs (elf, symscn, shdr, symndx_map);
619 /* This has the side effect of updating STT_SECTION symbols' values,
620 in case of prelink adjustments. */
622 check_symtab_section_symbols (Elf *elf, bool rel, Elf_Scn *scn,
623 size_t shnum, size_t shstrndx,
624 Elf_Scn *oscn, size_t oshnum, size_t oshstrndx,
627 size_t n = symtab_count_leading_section_symbols (elf, oscn, oshnum,
628 elf_getdata (scn, NULL));
631 return add_new_section_symbols (oscn, n, elf, rel, scn, shnum);
633 if (n == oshstrndx || (n == debuglink && n == oshstrndx - 1))
634 return add_new_section_symbols (oscn, n, elf, rel, scn, shstrndx);
644 struct Ebl_Strent *strent;
649 compare_alloc_sections (const struct section *s1, const struct section *s2,
654 /* Sort by address. */
655 if (s1->shdr.sh_addr < s2->shdr.sh_addr)
657 if (s1->shdr.sh_addr > s2->shdr.sh_addr)
661 /* At the same address, preserve original section order. */
662 return (ssize_t) elf_ndxscn (s1->scn) - (ssize_t) elf_ndxscn (s2->scn);
666 compare_unalloc_sections (const GElf_Shdr *shdr1, const GElf_Shdr *shdr2,
667 const char *name1, const char *name2)
669 /* Sort by sh_flags as an arbitrary ordering. */
670 if (shdr1->sh_flags < shdr2->sh_flags)
672 if (shdr1->sh_flags > shdr2->sh_flags)
675 /* Sort by name as last resort. */
676 return strcmp (name1, name2);
680 compare_sections (const void *a, const void *b, bool rel)
682 const struct section *s1 = a;
683 const struct section *s2 = b;
685 /* Sort all non-allocated sections last. */
686 if ((s1->shdr.sh_flags ^ s2->shdr.sh_flags) & SHF_ALLOC)
687 return (s1->shdr.sh_flags & SHF_ALLOC) ? -1 : 1;
689 return ((s1->shdr.sh_flags & SHF_ALLOC)
690 ? compare_alloc_sections (s1, s2, rel)
691 : compare_unalloc_sections (&s1->shdr, &s2->shdr,
692 s1->name, s2->name));
696 compare_sections_rel (const void *a, const void *b)
698 return compare_sections (a, b, true);
702 compare_sections_nonrel (const void *a, const void *b)
704 return compare_sections (a, b, false);
715 struct Ebl_Strent *strent;
735 /* For a symbol discarded after first sort, this matches its better's
741 /* Collect input symbols into our internal form. */
743 collect_symbols (Elf *outelf, bool rel, Elf_Scn *symscn, Elf_Scn *strscn,
744 const size_t nent, const GElf_Addr bias,
745 const size_t scnmap[], struct symbol *table, size_t *map,
746 struct section *split_bss)
748 Elf_Data *symdata = elf_getdata (symscn, NULL);
749 Elf_Data *strdata = elf_getdata (strscn, NULL);
750 Elf_Data *shndxdata = NULL; /* XXX */
752 for (size_t i = 1; i < nent; ++i)
755 GElf_Word shndx = SHN_UNDEF;
756 GElf_Sym *sym = gelf_getsymshndx (symdata, shndxdata, i,
758 ELF_CHECK (sym != NULL, _("cannot get symbol table entry: %s"));
759 if (sym->st_shndx != SHN_XINDEX)
760 shndx = sym->st_shndx;
762 if (sym->st_name >= strdata->d_size)
763 error (EXIT_FAILURE, 0,
764 _("invalid string offset in symbol [%Zu]"), i);
766 struct symbol *s = &table[i - 1];
767 s->map = &map[i - 1];
768 s->name = strdata->d_buf + sym->st_name;
769 s->value = sym->st_value + bias;
770 s->size = sym->st_size;
772 s->info.info = sym->st_info;
773 s->info.other = sym->st_other;
775 if (scnmap != NULL && shndx != SHN_UNDEF && shndx < SHN_LORESERVE)
776 s->shndx = scnmap[shndx - 1];
778 if (GELF_ST_TYPE (s->info.info) == STT_SECTION && !rel)
780 /* Update the value to match the output section. */
782 GElf_Shdr *shdr = gelf_getshdr (elf_getscn (outelf, s->shndx),
784 ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
785 s->value = shdr->sh_addr;
787 else if (split_bss != NULL
788 && s->value < split_bss->shdr.sh_addr
789 && s->value >= split_bss[-1].shdr.sh_addr
790 && shndx == elf_ndxscn (split_bss->outscn))
791 /* This symbol was in .bss and was split into .dynbss. */
792 s->shndx = elf_ndxscn (split_bss[-1].outscn);
798 if (s1->value < s2->value) \
800 if (s1->value > s2->value) \
803 /* Compare symbols with a consistent ordering,
804 but one only meaningful for equality. */
806 compare_symbols (const void *a, const void *b)
808 const struct symbol *s1 = a;
809 const struct symbol *s2 = b;
815 return (s1->compare - s2->compare) ?: strcmp (s1->name, s2->name);
818 /* Compare symbols for output order after slots have been assigned. */
820 compare_symbols_output (const void *a, const void *b)
822 const struct symbol *s1 = a;
823 const struct symbol *s2 = b;
826 /* Sort discarded symbols last. */
827 cmp = (s1->name == NULL) - (s2->name == NULL);
830 /* Local symbols must come first. */
831 cmp = ((GELF_ST_BIND (s2->info.info) == STB_LOCAL)
832 - (GELF_ST_BIND (s1->info.info) == STB_LOCAL));
835 /* binutils always puts section symbols first. */
836 cmp = ((GELF_ST_TYPE (s2->info.info) == STT_SECTION)
837 - (GELF_ST_TYPE (s1->info.info) == STT_SECTION));
841 if (GELF_ST_TYPE (s1->info.info) == STT_SECTION)
843 /* binutils always puts section symbols in section index order. */
849 /* Nothing really matters, so preserve the original order. */
860 /* Return true iff the flags, size, and name match. */
862 sections_match (const struct section *sections, size_t i,
863 const GElf_Shdr *shdr, const char *name)
865 return (sections[i].shdr.sh_flags == shdr->sh_flags
866 && (sections[i].shdr.sh_size == shdr->sh_size
867 || (sections[i].shdr.sh_size < shdr->sh_size
868 && section_can_shrink (§ions[i].shdr)))
869 && !strcmp (sections[i].name, name));
872 /* Locate a matching allocated section in SECTIONS. */
873 static struct section *
874 find_alloc_section (const GElf_Shdr *shdr, GElf_Addr bias, const char *name,
875 struct section sections[], size_t nalloc)
877 const GElf_Addr addr = shdr->sh_addr + bias;
878 size_t l = 0, u = nalloc;
881 size_t i = (l + u) / 2;
882 if (addr < sections[i].shdr.sh_addr)
884 else if (addr > sections[i].shdr.sh_addr)
888 /* We've found allocated sections with this address.
889 Find one with matching size, flags, and name. */
890 while (i > 0 && sections[i - 1].shdr.sh_addr == addr)
892 for (; i < nalloc && sections[i].shdr.sh_addr == addr;
894 if (sections_match (sections, i, shdr, name))
902 static inline const char *
903 get_section_name (size_t ndx, const GElf_Shdr *shdr, const Elf_Data *shstrtab)
905 if (shdr->sh_name >= shstrtab->d_size)
906 error (EXIT_FAILURE, 0, _("cannot read section [%Zu] name: %s"),
907 ndx, elf_errmsg (-1));
908 return shstrtab->d_buf + shdr->sh_name;
911 /* Fix things up when prelink has moved some allocated sections around
912 and the debuginfo file's section headers no longer match up.
913 This fills in SECTIONS[0..NALLOC-1].outscn or exits.
914 If there was a .bss section that was split into two sections
915 with the new one preceding it in sh_addr, we return that pointer. */
916 static struct section *
917 find_alloc_sections_prelink (Elf *debug, Elf_Data *debug_shstrtab,
918 Elf *main, const GElf_Ehdr *main_ehdr,
919 Elf_Data *main_shstrtab, GElf_Addr bias,
920 struct section *sections,
921 size_t nalloc, size_t nsections)
923 /* Clear assignments that might have been bogus. */
924 for (size_t i = 0; i < nalloc; ++i)
925 sections[i].outscn = NULL;
927 Elf_Scn *undo = NULL;
928 for (size_t i = nalloc; i < nsections; ++i)
930 const struct section *sec = §ions[i];
931 if (sec->shdr.sh_type == SHT_PROGBITS
932 && !(sec->shdr.sh_flags & SHF_ALLOC)
933 && !strcmp (sec->name, ".gnu.prelink_undo"))
940 /* Find the original allocated sections before prelinking. */
941 struct section *undo_sections = NULL;
942 size_t undo_nalloc = 0;
945 Elf_Data *undodata = elf_rawdata (undo, NULL);
946 ELF_CHECK (undodata != NULL,
947 _("cannot read '.gnu.prelink_undo' section: %s"));
957 .d_size = sizeof ehdr,
958 .d_type = ELF_T_EHDR,
959 .d_version = EV_CURRENT
961 Elf_Data src = *undodata;
962 src.d_size = gelf_fsize (main, ELF_T_EHDR, 1, EV_CURRENT);
963 src.d_type = ELF_T_EHDR;
964 ELF_CHECK (gelf_xlatetom (main, &dst, &src,
965 main_ehdr->e_ident[EI_DATA]) != NULL,
966 _("cannot read '.gnu.prelink_undo' section: %s"));
970 if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
972 phnum = ehdr.e32.e_phnum;
973 shnum = ehdr.e32.e_shnum;
977 phnum = ehdr.e64.e_phnum;
978 shnum = ehdr.e64.e_shnum;
981 size_t phsize = gelf_fsize (main, ELF_T_PHDR, phnum, EV_CURRENT);
982 src.d_buf += src.d_size + phsize;
983 src.d_size = gelf_fsize (main, ELF_T_SHDR, shnum - 1, EV_CURRENT);
984 src.d_type = ELF_T_SHDR;
985 if ((size_t) (src.d_buf - undodata->d_buf) > undodata->d_size
986 || undodata->d_size - (src.d_buf - undodata->d_buf) != src.d_size)
987 error (EXIT_FAILURE, 0, _("invalid contents in '%s' section"),
988 ".gnu.prelink_undo");
992 Elf32_Shdr s32[shnum - 1];
993 Elf64_Shdr s64[shnum - 1];
996 dst.d_size = sizeof shdr;
997 ELF_CHECK (gelf_xlatetom (main, &dst, &src,
998 main_ehdr->e_ident[EI_DATA]) != NULL,
999 _("cannot read '.gnu.prelink_undo' section: %s"));
1001 undo_sections = xmalloc ((shnum - 1) * sizeof undo_sections[0]);
1002 for (size_t i = 0; i < shnum - 1; ++i)
1004 struct section *sec = &undo_sections[undo_nalloc];
1005 if (ehdr.e32.e_ident[EI_CLASS] == ELFCLASS32)
1007 #define COPY(field) sec->shdr.field = shdr.s32[i].field
1016 COPY (sh_addralign);
1021 sec->shdr = shdr.s64[i];
1022 if (sec->shdr.sh_flags & SHF_ALLOC)
1024 sec->shdr.sh_addr += bias;
1025 sec->name = get_section_name (i + 1, &sec->shdr, main_shstrtab);
1026 sec->scn = elf_getscn (main, i + 1); /* Really just for ndx. */
1032 qsort (undo_sections, undo_nalloc,
1033 sizeof undo_sections[0], compare_sections_nonrel);
1037 inline void check_match (bool match, Elf_Scn *scn, const char *name)
1042 error (0, 0, _("cannot find matching section for [%Zu] '%s'"),
1043 elf_ndxscn (scn), name);
1047 Elf_Scn *scn = NULL;
1048 while ((scn = elf_nextscn (debug, scn)) != NULL)
1051 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1052 ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
1054 if (!(shdr->sh_flags & SHF_ALLOC))
1057 const char *name = get_section_name (elf_ndxscn (scn), shdr,
1060 if (undo_sections != NULL)
1062 struct section *sec = find_alloc_section (shdr, 0, name,
1072 /* If there is no prelink info, we are just here to find
1073 the sections to give error messages about. */
1074 for (size_t i = 0; shdr != NULL && i < nalloc; ++i)
1075 if (sections[i].outscn == scn)
1077 check_match (shdr == NULL, scn, name);
1081 exit (EXIT_FAILURE);
1083 /* Now we have lined up output sections for each of the original sections
1084 before prelinking. Translate those to the prelinked sections.
1085 This matches what prelink's undo_sections does. */
1086 struct section *split_bss = NULL;
1087 for (size_t i = 0; i < undo_nalloc; ++i)
1089 const struct section *undo_sec = &undo_sections[i];
1091 const char *name = undo_sec->name;
1092 scn = undo_sec->scn; /* This is just for elf_ndxscn. */
1094 for (size_t j = 0; j < nalloc; ++j)
1096 struct section *sec = §ions[j];
1097 #define RELA_SCALED(field) \
1098 (2 * sec->shdr.field == 3 * undo_sec->shdr.field)
1099 if (sec->outscn == NULL
1100 && sec->shdr.sh_name == undo_sec->shdr.sh_name
1101 && sec->shdr.sh_flags == undo_sec->shdr.sh_flags
1102 && sec->shdr.sh_addralign == undo_sec->shdr.sh_addralign
1103 && (((sec->shdr.sh_type == undo_sec->shdr.sh_type
1104 && sec->shdr.sh_entsize == undo_sec->shdr.sh_entsize
1105 && (sec->shdr.sh_size == undo_sec->shdr.sh_size
1106 || (sec->shdr.sh_size > undo_sec->shdr.sh_size
1107 && main_ehdr->e_type == ET_EXEC
1108 && !strcmp (sec->name, ".dynstr"))))
1109 || (sec->shdr.sh_size == undo_sec->shdr.sh_size
1110 && ((sec->shdr.sh_entsize == undo_sec->shdr.sh_entsize
1111 && undo_sec->shdr.sh_type == SHT_NOBITS)
1112 || undo_sec->shdr.sh_type == SHT_PROGBITS)
1113 && !strcmp (sec->name, ".plt")))
1114 || (sec->shdr.sh_type == SHT_RELA
1115 && undo_sec->shdr.sh_type == SHT_REL
1116 && RELA_SCALED (sh_entsize) && RELA_SCALED (sh_size))
1117 || (sec->shdr.sh_entsize == undo_sec->shdr.sh_entsize
1118 && (sec->shdr.sh_type == undo_sec->shdr.sh_type
1119 || (sec->shdr.sh_type == SHT_PROGBITS
1120 && undo_sec->shdr.sh_type == SHT_NOBITS))
1121 && sec->shdr.sh_size < undo_sec->shdr.sh_size
1122 && (!strcmp (sec->name, ".bss")
1123 || !strcmp (sec->name, ".sbss"))
1124 && (split_bss = sec) > sections)))
1126 sec->outscn = undo_sec->outscn;
1132 check_match (undo_sec == NULL, scn, name);
1135 free (undo_sections);
1138 exit (EXIT_FAILURE);
1143 /* Create new .shstrtab contents, subroutine of copy_elided_sections.
1144 This can't be open coded there and still use variable-length auto arrays,
1145 since the end of our block would free other VLAs too. */
1147 new_shstrtab (Elf *unstripped, size_t unstripped_shnum,
1148 Elf_Data *shstrtab, size_t unstripped_shstrndx,
1149 struct section *sections, size_t stripped_shnum,
1150 struct Ebl_Strtab *strtab)
1155 struct Ebl_Strent *unstripped_strent[unstripped_shnum - 1];
1156 memset (unstripped_strent, 0, sizeof unstripped_strent);
1157 for (struct section *sec = sections;
1158 sec < §ions[stripped_shnum - 1];
1160 if (sec->outscn != NULL)
1162 if (sec->strent == NULL)
1164 sec->strent = ebl_strtabadd (strtab, sec->name, 0);
1165 ELF_CHECK (sec->strent != NULL,
1166 _("cannot add section name to string table: %s"));
1168 unstripped_strent[elf_ndxscn (sec->outscn) - 1] = sec->strent;
1171 /* Add names of sections we aren't touching. */
1172 for (size_t i = 0; i < unstripped_shnum - 1; ++i)
1173 if (unstripped_strent[i] == NULL)
1175 Elf_Scn *scn = elf_getscn (unstripped, i + 1);
1177 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1178 const char *name = get_section_name (i + 1, shdr, shstrtab);
1179 unstripped_strent[i] = ebl_strtabadd (strtab, name, 0);
1180 ELF_CHECK (unstripped_strent[i] != NULL,
1181 _("cannot add section name to string table: %s"));
1184 unstripped_strent[i] = NULL;
1186 /* Now finalize the string table so we can get offsets. */
1187 Elf_Data *strtab_data = elf_getdata (elf_getscn (unstripped,
1188 unstripped_shstrndx), NULL);
1189 ELF_CHECK (elf_flagdata (strtab_data, ELF_C_SET, ELF_F_DIRTY),
1190 _("cannot update section header string table data: %s"));
1191 ebl_strtabfinalize (strtab, strtab_data);
1193 /* Update the sh_name fields of sections we aren't modifying later. */
1194 for (size_t i = 0; i < unstripped_shnum - 1; ++i)
1195 if (unstripped_strent[i] != NULL)
1197 Elf_Scn *scn = elf_getscn (unstripped, i + 1);
1199 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1200 shdr->sh_name = ebl_strtaboffset (unstripped_strent[i]);
1201 if (i + 1 == unstripped_shstrndx)
1202 shdr->sh_size = strtab_data->d_size;
1203 update_shdr (scn, shdr);
1209 /* Fill in any SHT_NOBITS sections in UNSTRIPPED by
1210 copying their contents and sh_type from STRIPPED. */
1212 copy_elided_sections (Elf *unstripped, Elf *stripped,
1213 const GElf_Ehdr *stripped_ehdr, GElf_Addr bias)
1215 size_t unstripped_shstrndx;
1216 ELF_CHECK (elf_getshdrstrndx (unstripped, &unstripped_shstrndx) == 0,
1217 _("cannot get section header string table section index: %s"));
1219 size_t stripped_shstrndx;
1220 ELF_CHECK (elf_getshdrstrndx (stripped, &stripped_shstrndx) == 0,
1221 _("cannot get section header string table section index: %s"));
1223 size_t unstripped_shnum;
1224 ELF_CHECK (elf_getshdrnum (unstripped, &unstripped_shnum) == 0,
1225 _("cannot get section count: %s"));
1227 size_t stripped_shnum;
1228 ELF_CHECK (elf_getshdrnum (stripped, &stripped_shnum) == 0,
1229 _("cannot get section count: %s"));
1231 if (unlikely (stripped_shnum > unstripped_shnum))
1232 error (EXIT_FAILURE, 0, _("\
1233 more sections in stripped file than debug file -- arguments reversed?"));
1235 /* Cache the stripped file's section details. */
1236 struct section sections[stripped_shnum - 1];
1237 Elf_Scn *scn = NULL;
1238 while ((scn = elf_nextscn (stripped, scn)) != NULL)
1240 size_t i = elf_ndxscn (scn) - 1;
1241 GElf_Shdr *shdr = gelf_getshdr (scn, §ions[i].shdr);
1242 ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
1243 sections[i].name = elf_strptr (stripped, stripped_shstrndx,
1245 if (sections[i].name == NULL)
1246 error (EXIT_FAILURE, 0, _("cannot read section [%Zu] name: %s"),
1247 elf_ndxscn (scn), elf_errmsg (-1));
1248 sections[i].scn = scn;
1249 sections[i].outscn = NULL;
1250 sections[i].strent = NULL;
1253 const struct section *stripped_symtab = NULL;
1255 /* Sort the sections, allocated by address and others after. */
1256 qsort (sections, stripped_shnum - 1, sizeof sections[0],
1257 stripped_ehdr->e_type == ET_REL
1258 ? compare_sections_rel : compare_sections_nonrel);
1259 size_t nalloc = stripped_shnum - 1;
1260 while (nalloc > 0 && !(sections[nalloc - 1].shdr.sh_flags & SHF_ALLOC))
1263 if (sections[nalloc].shdr.sh_type == SHT_SYMTAB)
1264 stripped_symtab = §ions[nalloc];
1267 /* Locate a matching unallocated section in SECTIONS. */
1268 inline struct section *find_unalloc_section (const GElf_Shdr *shdr,
1271 size_t l = nalloc, u = stripped_shnum - 1;
1274 size_t i = (l + u) / 2;
1275 struct section *sec = §ions[i];
1276 int cmp = compare_unalloc_sections (shdr, &sec->shdr,
1288 Elf_Data *shstrtab = elf_getdata (elf_getscn (unstripped,
1289 unstripped_shstrndx), NULL);
1290 ELF_CHECK (shstrtab != NULL,
1291 _("cannot read section header string table: %s"));
1293 /* Match each debuginfo section with its corresponding stripped section. */
1294 bool check_prelink = false;
1295 Elf_Scn *unstripped_symtab = NULL;
1296 size_t alloc_avail = 0;
1298 while ((scn = elf_nextscn (unstripped, scn)) != NULL)
1301 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1302 ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
1304 if (shdr->sh_type == SHT_SYMTAB)
1306 unstripped_symtab = scn;
1310 const size_t ndx = elf_ndxscn (scn);
1311 if (ndx == unstripped_shstrndx)
1314 const char *name = get_section_name (ndx, shdr, shstrtab);
1316 struct section *sec = NULL;
1317 if (shdr->sh_flags & SHF_ALLOC)
1319 if (stripped_ehdr->e_type != ET_REL)
1321 /* Look for the section that matches. */
1322 sec = find_alloc_section (shdr, bias, name, sections, nalloc);
1325 /* We couldn't figure it out. It may be a prelink issue. */
1326 check_prelink = true;
1332 /* The sh_addr of allocated sections does not help us,
1333 but the order usually matches. */
1334 if (likely (sections_match (sections, alloc_avail, shdr, name)))
1335 sec = §ions[alloc_avail++];
1337 for (size_t i = alloc_avail + 1; i < nalloc; ++i)
1338 if (sections_match (sections, i, shdr, name))
1347 /* Look for the section that matches. */
1348 sec = find_unalloc_section (shdr, name);
1351 /* An additional unallocated section is fine if not SHT_NOBITS.
1352 We looked it up anyway in case it's an unallocated section
1353 copied in both files (e.g. SHT_NOTE), and don't keep both. */
1354 if (shdr->sh_type != SHT_NOBITS)
1357 /* Somehow some old .debug files wound up with SHT_NOBITS
1358 .comment sections, so let those pass. */
1359 if (!strcmp (name, ".comment"))
1365 error (EXIT_FAILURE, 0,
1366 _("cannot find matching section for [%Zu] '%s'"),
1367 elf_ndxscn (scn), name);
1372 /* If that failed due to changes made by prelink, we take another tack.
1373 We keep track of a .bss section that was partly split into .dynbss
1374 so that collect_symbols can update symbols' st_shndx fields. */
1375 struct section *split_bss = NULL;
1378 Elf_Data *data = elf_getdata (elf_getscn (stripped, stripped_shstrndx),
1380 ELF_CHECK (data != NULL,
1381 _("cannot read section header string table: %s"));
1382 split_bss = find_alloc_sections_prelink (unstripped, shstrtab,
1383 stripped, stripped_ehdr,
1384 data, bias, sections,
1385 nalloc, stripped_shnum - 1);
1388 /* Make sure each main file section has a place to go. */
1389 const struct section *stripped_dynsym = NULL;
1390 size_t debuglink = SHN_UNDEF;
1391 size_t ndx_section[stripped_shnum - 1];
1392 struct Ebl_Strtab *strtab = NULL;
1393 for (struct section *sec = sections;
1394 sec < §ions[stripped_shnum - 1];
1397 size_t secndx = elf_ndxscn (sec->scn);
1399 if (sec->outscn == NULL)
1401 /* We didn't find any corresponding section for this. */
1403 if (secndx == stripped_shstrndx)
1405 /* We only need one .shstrtab. */
1406 ndx_section[secndx - 1] = unstripped_shstrndx;
1410 if (unstripped_symtab != NULL && sec == stripped_symtab)
1412 /* We don't need a second symbol table. */
1413 ndx_section[secndx - 1] = elf_ndxscn (unstripped_symtab);
1417 if (unstripped_symtab != NULL && stripped_symtab != NULL
1418 && secndx == stripped_symtab->shdr.sh_link)
1420 /* ... nor its string table. */
1422 GElf_Shdr *shdr = gelf_getshdr (unstripped_symtab, &shdr_mem);
1423 ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
1424 ndx_section[secndx - 1] = shdr->sh_link;
1428 if (!(sec->shdr.sh_flags & SHF_ALLOC)
1429 && !strcmp (sec->name, ".gnu_debuglink"))
1431 /* This was created by stripping. We don't want it. */
1433 ndx_section[secndx - 1] = SHN_UNDEF;
1437 sec->outscn = elf_newscn (unstripped);
1438 Elf_Data *newdata = elf_newdata (sec->outscn);
1439 ELF_CHECK (newdata != NULL && gelf_update_shdr (sec->outscn,
1441 _("cannot add new section: %s"));
1444 strtab = ebl_strtabinit (true);
1445 sec->strent = ebl_strtabadd (strtab, sec->name, 0);
1446 ELF_CHECK (sec->strent != NULL,
1447 _("cannot add section name to string table: %s"));
1450 /* Cache the mapping of original section indices to output sections. */
1451 ndx_section[secndx - 1] = elf_ndxscn (sec->outscn);
1454 /* We added some sections, so we need a new shstrtab. */
1455 Elf_Data *strtab_data = new_shstrtab (unstripped, unstripped_shnum,
1456 shstrtab, unstripped_shstrndx,
1457 sections, stripped_shnum,
1460 /* Get the updated section count. */
1461 ELF_CHECK (elf_getshdrnum (unstripped, &unstripped_shnum) == 0,
1462 _("cannot get section count: %s"));
1464 bool placed[unstripped_shnum - 1];
1465 memset (placed, 0, sizeof placed);
1467 /* Now update the output sections and copy in their data. */
1468 GElf_Off offset = 0;
1469 for (const struct section *sec = sections;
1470 sec < §ions[stripped_shnum - 1];
1472 if (sec->outscn != NULL)
1475 GElf_Shdr *shdr = gelf_getshdr (sec->outscn, &shdr_mem);
1476 ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
1478 /* In an ET_REL file under --relocate, the sh_addr of SHF_ALLOC
1479 sections will have been set nonzero by relocation. This
1480 touched the shdrs of whichever file had the symtab. sh_addr
1481 is still zero in the corresponding shdr. The relocated
1482 address is what we want to use. */
1483 if (stripped_ehdr->e_type != ET_REL
1484 || !(shdr_mem.sh_flags & SHF_ALLOC)
1485 || shdr_mem.sh_addr == 0)
1486 shdr_mem.sh_addr = sec->shdr.sh_addr;
1488 shdr_mem.sh_type = sec->shdr.sh_type;
1489 shdr_mem.sh_size = sec->shdr.sh_size;
1490 shdr_mem.sh_info = sec->shdr.sh_info;
1491 shdr_mem.sh_link = sec->shdr.sh_link;
1492 if (sec->shdr.sh_link != SHN_UNDEF)
1493 shdr_mem.sh_link = ndx_section[sec->shdr.sh_link - 1];
1494 if (shdr_mem.sh_flags & SHF_INFO_LINK)
1495 shdr_mem.sh_info = ndx_section[sec->shdr.sh_info - 1];
1498 shdr_mem.sh_name = ebl_strtaboffset (sec->strent);
1500 Elf_Data *indata = elf_getdata (sec->scn, NULL);
1501 ELF_CHECK (indata != NULL, _("cannot get section data: %s"));
1502 Elf_Data *outdata = elf_getdata (sec->outscn, NULL);
1503 ELF_CHECK (outdata != NULL, _("cannot copy section data: %s"));
1505 elf_flagdata (outdata, ELF_C_SET, ELF_F_DIRTY);
1507 /* Preserve the file layout of the allocated sections. */
1508 if (stripped_ehdr->e_type != ET_REL && (shdr_mem.sh_flags & SHF_ALLOC))
1510 shdr_mem.sh_offset = sec->shdr.sh_offset;
1511 placed[elf_ndxscn (sec->outscn) - 1] = true;
1513 const GElf_Off end_offset = (shdr_mem.sh_offset
1514 + (shdr_mem.sh_type == SHT_NOBITS
1515 ? 0 : shdr_mem.sh_size));
1516 if (end_offset > offset)
1517 offset = end_offset;
1520 update_shdr (sec->outscn, &shdr_mem);
1522 if (shdr_mem.sh_type == SHT_SYMTAB || shdr_mem.sh_type == SHT_DYNSYM)
1524 /* We must adjust all the section indices in the symbol table. */
1526 Elf_Data *shndxdata = NULL; /* XXX */
1528 for (size_t i = 1; i < shdr_mem.sh_size / shdr_mem.sh_entsize; ++i)
1531 GElf_Word shndx = SHN_UNDEF;
1532 GElf_Sym *sym = gelf_getsymshndx (outdata, shndxdata,
1533 i, &sym_mem, &shndx);
1534 ELF_CHECK (sym != NULL,
1535 _("cannot get symbol table entry: %s"));
1536 if (sym->st_shndx != SHN_XINDEX)
1537 shndx = sym->st_shndx;
1539 if (shndx != SHN_UNDEF && shndx < SHN_LORESERVE)
1541 if (shndx >= stripped_shnum)
1542 error (EXIT_FAILURE, 0,
1543 _("symbol [%Zu] has invalid section index"), i);
1545 shndx = ndx_section[shndx - 1];
1546 if (shndx < SHN_LORESERVE)
1548 sym->st_shndx = shndx;
1552 sym->st_shndx = SHN_XINDEX;
1554 ELF_CHECK (gelf_update_symshndx (outdata, shndxdata,
1556 _("cannot update symbol table: %s"));
1560 if (shdr_mem.sh_type == SHT_SYMTAB)
1561 stripped_symtab = sec;
1562 if (shdr_mem.sh_type == SHT_DYNSYM)
1563 stripped_dynsym = sec;
1567 /* We may need to update the symbol table. */
1568 Elf_Data *symdata = NULL;
1569 struct Ebl_Strtab *symstrtab = NULL;
1570 Elf_Data *symstrdata = NULL;
1571 if (unstripped_symtab != NULL && (stripped_symtab != NULL
1572 || check_prelink /* Section adjustments. */
1573 || (stripped_ehdr->e_type != ET_REL
1576 /* Merge the stripped file's symbol table into the unstripped one. */
1577 const size_t stripped_nsym = (stripped_symtab == NULL ? 1
1578 : (stripped_symtab->shdr.sh_size
1579 / stripped_symtab->shdr.sh_entsize));
1582 GElf_Shdr *shdr = gelf_getshdr (unstripped_symtab, &shdr_mem);
1583 ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
1584 const size_t unstripped_nsym = shdr->sh_size / shdr->sh_entsize;
1586 /* First collect all the symbols from both tables. */
1588 const size_t total_syms = stripped_nsym - 1 + unstripped_nsym - 1;
1589 struct symbol symbols[total_syms];
1590 size_t symndx_map[total_syms];
1592 if (stripped_symtab != NULL)
1593 collect_symbols (unstripped, stripped_ehdr->e_type == ET_REL,
1594 stripped_symtab->scn,
1595 elf_getscn (stripped, stripped_symtab->shdr.sh_link),
1596 stripped_nsym, 0, ndx_section,
1597 symbols, symndx_map, NULL);
1599 Elf_Scn *unstripped_strtab = elf_getscn (unstripped, shdr->sh_link);
1600 collect_symbols (unstripped, stripped_ehdr->e_type == ET_REL,
1601 unstripped_symtab, unstripped_strtab, unstripped_nsym,
1602 stripped_ehdr->e_type == ET_REL ? 0 : bias, NULL,
1603 &symbols[stripped_nsym - 1],
1604 &symndx_map[stripped_nsym - 1], split_bss);
1606 /* Next, sort our array of all symbols. */
1607 qsort (symbols, total_syms, sizeof symbols[0], compare_symbols);
1609 /* Now we can weed out the duplicates. Assign remaining symbols
1610 new slots, collecting a map from old indices to new. */
1612 for (struct symbol *s = symbols; s < &symbols[total_syms]; ++s)
1614 /* Skip a section symbol for a removed section. */
1615 if (s->shndx == SHN_UNDEF
1616 && GELF_ST_TYPE (s->info.info) == STT_SECTION)
1618 s->name = NULL; /* Mark as discarded. */
1619 *s->map = STN_UNDEF;
1620 s->duplicate = NULL;
1624 struct symbol *n = s;
1625 while (n + 1 < &symbols[total_syms] && !compare_symbols (s, n + 1))
1630 /* This is a duplicate. Its twin will get the next slot. */
1631 s->name = NULL; /* Mark as discarded. */
1632 s->duplicate = n->map;
1636 /* Allocate the next slot. */
1640 /* Now we sort again, to determine the order in the output. */
1641 qsort (symbols, total_syms, sizeof symbols[0], compare_symbols_output);
1643 if (nsym < total_syms)
1644 /* The discarded symbols are now at the end of the table. */
1645 assert (symbols[nsym].name == NULL);
1647 /* Now a final pass updates the map with the final order,
1648 and builds up the new string table. */
1649 symstrtab = ebl_strtabinit (true);
1650 for (size_t i = 0; i < nsym; ++i)
1652 assert (symbols[i].name != NULL);
1653 assert (*symbols[i].map != 0);
1654 *symbols[i].map = 1 + i;
1655 symbols[i].strent = ebl_strtabadd (symstrtab, symbols[i].name, 0);
1658 /* Scan the discarded symbols too, just to update their slots
1659 in SYMNDX_MAP to refer to their live duplicates. */
1660 for (size_t i = nsym; i < total_syms; ++i)
1662 assert (symbols[i].name == NULL);
1663 if (symbols[i].duplicate == NULL)
1664 assert (*symbols[i].map == STN_UNDEF);
1667 assert (*symbols[i].duplicate != STN_UNDEF);
1668 *symbols[i].map = *symbols[i].duplicate;
1672 /* Now we are ready to write the new symbol table. */
1673 symdata = elf_getdata (unstripped_symtab, NULL);
1674 symstrdata = elf_getdata (unstripped_strtab, NULL);
1675 Elf_Data *shndxdata = NULL; /* XXX */
1677 ebl_strtabfinalize (symstrtab, symstrdata);
1678 elf_flagdata (symstrdata, ELF_C_SET, ELF_F_DIRTY);
1680 shdr->sh_size = symdata->d_size = (1 + nsym) * shdr->sh_entsize;
1681 symdata->d_buf = xmalloc (symdata->d_size);
1684 memset (&sym, 0, sizeof sym);
1685 ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, 0, &sym, SHN_UNDEF),
1686 _("cannot update symbol table: %s"));
1689 for (size_t i = 0; i < nsym; ++i)
1691 struct symbol *s = &symbols[i];
1693 /* Fill in the symbol details. */
1694 sym.st_name = ebl_strtaboffset (s->strent);
1695 sym.st_value = s->value; /* Already biased to output address. */
1696 sym.st_size = s->size;
1697 sym.st_shndx = s->shndx; /* Already mapped to output index. */
1698 sym.st_info = s->info.info;
1699 sym.st_other = s->info.other;
1701 /* Keep track of the number of leading local symbols. */
1702 if (GELF_ST_BIND (sym.st_info) == STB_LOCAL)
1704 assert (shdr->sh_info == 1 + i);
1705 shdr->sh_info = 1 + i + 1;
1708 ELF_CHECK (gelf_update_symshndx (symdata, shndxdata, 1 + i,
1710 _("cannot update symbol table: %s"));
1713 elf_flagdata (symdata, ELF_C_SET, ELF_F_DIRTY);
1714 update_shdr (unstripped_symtab, shdr);
1716 if (stripped_symtab != NULL)
1718 /* Adjust any relocations referring to the old symbol table. */
1719 const size_t old_sh_link = elf_ndxscn (stripped_symtab->scn);
1720 for (const struct section *sec = sections;
1721 sec < §ions[stripped_shnum - 1];
1723 if (sec->outscn != NULL && sec->shdr.sh_link == old_sh_link)
1724 adjust_relocs (sec->outscn, sec->scn, &sec->shdr,
1728 /* Also adjust references to the other old symbol table. */
1729 adjust_all_relocs (unstripped, unstripped_symtab, shdr,
1730 &symndx_map[stripped_nsym - 1]);
1732 else if (stripped_symtab != NULL && stripped_shnum != unstripped_shnum)
1733 check_symtab_section_symbols (unstripped,
1734 stripped_ehdr->e_type == ET_REL,
1735 stripped_symtab->scn,
1736 unstripped_shnum, unstripped_shstrndx,
1737 stripped_symtab->outscn,
1738 stripped_shnum, stripped_shstrndx,
1741 if (stripped_dynsym != NULL)
1742 (void) check_symtab_section_symbols (unstripped,
1743 stripped_ehdr->e_type == ET_REL,
1744 stripped_dynsym->outscn,
1746 unstripped_shstrndx,
1747 stripped_dynsym->scn, stripped_shnum,
1748 stripped_shstrndx, debuglink);
1750 /* We need to preserve the layout of the stripped file so the
1751 phdrs will match up. This requires us to do our own layout of
1752 the added sections. We do manual layout even for ET_REL just
1753 so we can try to match what the original probably had. */
1755 elf_flagelf (unstripped, ELF_C_SET, ELF_F_LAYOUT);
1758 /* For ET_REL we are starting the layout from scratch. */
1759 offset = gelf_fsize (unstripped, ELF_T_EHDR, 1, EV_CURRENT);
1761 bool skip_reloc = false;
1764 skip_reloc = !skip_reloc;
1765 for (size_t i = 0; i < unstripped_shnum - 1; ++i)
1768 scn = elf_getscn (unstripped, 1 + i);
1771 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1772 ELF_CHECK (shdr != NULL, _("cannot get section header: %s"));
1774 /* We must make sure we have read in the data of all sections
1775 beforehand and marked them to be written out. When we're
1776 modifying the existing file in place, we might overwrite
1777 this part of the file before we get to handling the section. */
1779 ELF_CHECK (elf_flagdata (elf_getdata (scn, NULL),
1780 ELF_C_SET, ELF_F_DIRTY),
1781 _("cannot read section data: %s"));
1784 && (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA))
1787 GElf_Off align = shdr->sh_addralign ?: 1;
1788 offset = (offset + align - 1) & -align;
1789 shdr->sh_offset = offset;
1790 if (shdr->sh_type != SHT_NOBITS)
1791 offset += shdr->sh_size;
1793 update_shdr (scn, shdr);
1795 if (unstripped_shstrndx == 1 + i)
1797 /* Place the section headers immediately after
1798 .shstrtab, and update the ELF header. */
1801 GElf_Ehdr *ehdr = gelf_getehdr (unstripped, &ehdr_mem);
1802 ELF_CHECK (ehdr != NULL, _("cannot get ELF header: %s"));
1804 GElf_Off sh_align = gelf_getclass (unstripped) * 4;
1805 offset = (offset + sh_align - 1) & -sh_align;
1806 ehdr->e_shnum = unstripped_shnum;
1807 ehdr->e_shoff = offset;
1808 offset += unstripped_shnum * ehdr->e_shentsize;
1809 ELF_CHECK (gelf_update_ehdr (unstripped, ehdr),
1810 _("cannot update ELF header: %s"));
1818 if (stripped_ehdr->e_phnum > 0)
1819 ELF_CHECK (gelf_newphdr (unstripped, stripped_ehdr->e_phnum),
1820 _("cannot create program headers: %s"));
1822 /* Copy each program header from the stripped file. */
1823 for (uint_fast16_t i = 0; i < stripped_ehdr->e_phnum; ++i)
1826 GElf_Phdr *phdr = gelf_getphdr (stripped, i, &phdr_mem);
1827 ELF_CHECK (phdr != NULL, _("cannot get program header: %s"));
1829 ELF_CHECK (gelf_update_phdr (unstripped, i, phdr),
1830 _("cannot update program header: %s"));
1833 /* Finally, write out the file. */
1834 ELF_CHECK (elf_update (unstripped, ELF_C_WRITE) > 0,
1835 _("cannot write output file: %s"));
1839 ebl_strtabfree (strtab);
1840 free (strtab_data->d_buf);
1843 if (symdata != NULL)
1844 free (symdata->d_buf);
1845 if (symstrtab != NULL)
1847 ebl_strtabfree (symstrtab);
1848 free (symstrdata->d_buf);
1852 /* Process one pair of files, already opened. */
1854 handle_file (const char *output_file, bool create_dirs,
1855 Elf *stripped, const GElf_Ehdr *stripped_ehdr,
1858 /* Determine the address bias between the debuginfo file and the main
1859 file, which may have been modified by prelinking. */
1861 if (unstripped != NULL)
1862 for (uint_fast16_t i = 0; i < stripped_ehdr->e_phnum; ++i)
1865 GElf_Phdr *phdr = gelf_getphdr (stripped, i, &phdr_mem);
1866 ELF_CHECK (phdr != NULL, _("cannot get program header: %s"));
1867 if (phdr->p_type == PT_LOAD)
1869 GElf_Phdr unstripped_phdr_mem;
1870 GElf_Phdr *unstripped_phdr = gelf_getphdr (unstripped, i,
1871 &unstripped_phdr_mem);
1872 ELF_CHECK (unstripped_phdr != NULL,
1873 _("cannot get program header: %s"));
1874 bias = phdr->p_vaddr - unstripped_phdr->p_vaddr;
1879 /* One day we could adjust all the DWARF data (like prelink itself does). */
1882 if (output_file == NULL)
1884 DWARF data not adjusted for prelinking bias; consider prelink -u"));
1887 DWARF data in '%s' not adjusted for prelinking bias; consider prelink -u"),
1891 if (output_file == NULL)
1892 /* Modify the unstripped file in place. */
1893 copy_elided_sections (unstripped, stripped, stripped_ehdr, bias);
1897 make_directories (output_file);
1899 /* Copy the unstripped file and then modify it. */
1900 int outfd = open64 (output_file, O_RDWR | O_CREAT,
1901 stripped_ehdr->e_type == ET_REL ? 0666 : 0777);
1903 error (EXIT_FAILURE, errno, _("cannot open '%s'"), output_file);
1904 Elf *outelf = elf_begin (outfd, ELF_C_WRITE, NULL);
1905 ELF_CHECK (outelf != NULL, _("cannot create ELF descriptor: %s"));
1907 if (unstripped == NULL)
1909 /* Actually, we are just copying out the main file as it is. */
1910 copy_elf (outelf, stripped);
1911 if (stripped_ehdr->e_type != ET_REL)
1912 elf_flagelf (outelf, ELF_C_SET, ELF_F_LAYOUT);
1913 ELF_CHECK (elf_update (outelf, ELF_C_WRITE) > 0,
1914 _("cannot write output file: %s"));
1918 copy_elf (outelf, unstripped);
1919 copy_elided_sections (outelf, stripped, stripped_ehdr, bias);
1928 open_file (const char *file, bool writable)
1930 int fd = open64 (file, writable ? O_RDWR : O_RDONLY);
1932 error (EXIT_FAILURE, errno, _("cannot open '%s'"), file);
1936 /* Handle a pair of files we need to open by name. */
1938 handle_explicit_files (const char *output_file, bool create_dirs,
1939 const char *stripped_file, const char *unstripped_file)
1941 int stripped_fd = open_file (stripped_file, false);
1942 Elf *stripped = elf_begin (stripped_fd, ELF_C_READ, NULL);
1943 GElf_Ehdr stripped_ehdr;
1944 ELF_CHECK (gelf_getehdr (stripped, &stripped_ehdr),
1945 _("cannot create ELF descriptor: %s"));
1947 int unstripped_fd = -1;
1948 Elf *unstripped = NULL;
1949 if (unstripped_file != NULL)
1951 unstripped_fd = open_file (unstripped_file, output_file == NULL);
1952 unstripped = elf_begin (unstripped_fd,
1953 (output_file == NULL ? ELF_C_RDWR : ELF_C_READ),
1955 GElf_Ehdr unstripped_ehdr;
1956 ELF_CHECK (gelf_getehdr (unstripped, &unstripped_ehdr),
1957 _("cannot create ELF descriptor: %s"));
1959 if (memcmp (stripped_ehdr.e_ident, unstripped_ehdr.e_ident, EI_NIDENT)
1960 || stripped_ehdr.e_type != unstripped_ehdr.e_type
1961 || stripped_ehdr.e_machine != unstripped_ehdr.e_machine
1962 || stripped_ehdr.e_phnum != unstripped_ehdr.e_phnum)
1963 error (EXIT_FAILURE, 0, _("'%s' and '%s' do not seem to match"),
1964 stripped_file, unstripped_file);
1967 handle_file (output_file, create_dirs, stripped, &stripped_ehdr, unstripped);
1970 close (stripped_fd);
1972 elf_end (unstripped);
1973 close (unstripped_fd);
1977 /* Handle a pair of files opened implicitly by libdwfl for one module. */
1979 handle_dwfl_module (const char *output_file, bool create_dirs,
1980 Dwfl_Module *mod, bool all, bool ignore, bool relocate)
1983 Elf *stripped = dwfl_module_getelf (mod, &bias);
1984 if (stripped == NULL)
1990 const char *modname = dwfl_module_info (mod, NULL, NULL, NULL,
1991 NULL, NULL, &file, NULL);
1993 error (EXIT_FAILURE, 0,
1994 _("cannot find stripped file for module '%s': %s"),
1995 modname, dwfl_errmsg (-1));
1997 error (EXIT_FAILURE, 0,
1998 _("cannot open stripped file '%s' for module '%s': %s"),
1999 modname, file, dwfl_errmsg (-1));
2002 Elf *debug = dwarf_getelf (dwfl_module_getdwarf (mod, &bias));
2003 if (debug == NULL && !all)
2009 const char *modname = dwfl_module_info (mod, NULL, NULL, NULL,
2010 NULL, NULL, NULL, &file);
2012 error (EXIT_FAILURE, 0,
2013 _("cannot find debug file for module '%s': %s"),
2014 modname, dwfl_errmsg (-1));
2016 error (EXIT_FAILURE, 0,
2017 _("cannot open debug file '%s' for module '%s': %s"),
2018 modname, file, dwfl_errmsg (-1));
2021 if (debug == stripped)
2028 const char *modname = dwfl_module_info (mod, NULL, NULL, NULL,
2029 NULL, NULL, &file, NULL);
2030 error (EXIT_FAILURE, 0, _("module '%s' file '%s' is not stripped"),
2035 GElf_Ehdr stripped_ehdr;
2036 ELF_CHECK (gelf_getehdr (stripped, &stripped_ehdr),
2037 _("cannot create ELF descriptor: %s"));
2039 if (stripped_ehdr.e_type == ET_REL)
2043 /* We can't use the Elf handles already open,
2044 because the DWARF sections have been relocated. */
2046 const char *stripped_file = NULL;
2047 const char *unstripped_file = NULL;
2048 (void) dwfl_module_info (mod, NULL, NULL, NULL, NULL, NULL,
2049 &stripped_file, &unstripped_file);
2051 handle_explicit_files (output_file, create_dirs,
2052 stripped_file, unstripped_file);
2056 /* Relocation is what we want! This ensures that all sections that can
2057 get sh_addr values assigned have them, even ones not used in DWARF.
2058 They might still be used in the symbol table. */
2059 if (dwfl_module_relocations (mod) < 0)
2060 error (EXIT_FAILURE, 0,
2061 _("cannot cache section addresses for module '%s': %s"),
2062 dwfl_module_info (mod, NULL, NULL, NULL, NULL, NULL, NULL, NULL),
2066 handle_file (output_file, create_dirs, stripped, &stripped_ehdr, debug);
2069 /* Handle one module being written to the output directory. */
2071 handle_output_dir_module (const char *output_dir, Dwfl_Module *mod,
2072 bool all, bool ignore, bool modnames, bool relocate)
2076 /* Make sure we've searched for the ELF file. */
2078 (void) dwfl_module_getelf (mod, &bias);
2082 const char *name = dwfl_module_info (mod, NULL, NULL, NULL,
2083 NULL, NULL, &file, NULL);
2085 if (file == NULL && ignore)
2089 if (asprintf (&output_file, "%s/%s", output_dir, modnames ? name : file) < 0)
2090 error (EXIT_FAILURE, 0, _("memory exhausted"));
2092 handle_dwfl_module (output_file, true, mod, all, ignore, relocate);
2097 list_module (Dwfl_Module *mod)
2099 /* Make sure we have searched for the files. */
2101 bool have_elf = dwfl_module_getelf (mod, &bias) != NULL;
2102 bool have_dwarf = dwfl_module_getdwarf (mod, &bias) != NULL;
2108 const char *name = dwfl_module_info (mod, NULL, &start, &end,
2109 NULL, NULL, &file, &debug);
2110 if (file != NULL && debug != NULL && (debug == file || !strcmp (debug, file)))
2113 const unsigned char *id;
2115 int id_len = dwfl_module_build_id (mod, &id, &id_vaddr);
2117 printf ("%#" PRIx64 "+%#" PRIx64 " ", start, end - start);
2122 printf ("%02" PRIx8, *id++);
2123 while (--id_len > 0);
2125 printf ("@%#" PRIx64, id_vaddr);
2130 printf (" %s %s %s\n",
2131 file ?: have_elf ? "." : "-",
2132 debug ?: have_dwarf ? "." : "-",
2137 struct match_module_info
2145 match_module (Dwfl_Module *mod,
2146 void **userdata __attribute__ ((unused)),
2148 Dwarf_Addr start __attribute__ ((unused)),
2151 struct match_module_info *info = arg;
2153 if (info->patterns[0] == NULL) /* Match all. */
2157 return DWARF_CB_ABORT;
2160 if (info->match_files)
2162 /* Make sure we've searched for the ELF file. */
2164 (void) dwfl_module_getelf (mod, &bias);
2167 const char *check = dwfl_module_info (mod, NULL, NULL, NULL,
2168 NULL, NULL, &file, NULL);
2169 assert (check == name);
2176 for (char **p = info->patterns; *p != NULL; ++p)
2177 if (fnmatch (*p, name, 0) == 0)
2183 /* Handle files opened implicitly via libdwfl. */
2185 handle_implicit_modules (const struct arg_info *info)
2187 struct match_module_info mmi = { info->args, NULL, info->match_files };
2188 inline ptrdiff_t next (ptrdiff_t offset)
2190 return dwfl_getmodules (info->dwfl, &match_module, &mmi, offset);
2192 ptrdiff_t offset = next (0);
2194 error (EXIT_FAILURE, 0, _("no matching modules found"));
2198 list_module (mmi.found);
2199 while ((offset = next (offset)) > 0);
2200 else if (info->output_dir == NULL)
2202 if (next (offset) != 0)
2203 error (EXIT_FAILURE, 0, _("matched more than one module"));
2204 handle_dwfl_module (info->output_file, false, mmi.found,
2205 info->all, info->ignore, info->relocate);
2209 handle_output_dir_module (info->output_dir, mmi.found,
2210 info->all, info->ignore,
2211 info->modnames, info->relocate);
2212 while ((offset = next (offset)) > 0);
2216 main (int argc, char **argv)
2218 /* Make memory leak detection possible. */
2221 /* We use no threads here which can interfere with handling a stream. */
2222 __fsetlocking (stdin, FSETLOCKING_BYCALLER);
2223 __fsetlocking (stdout, FSETLOCKING_BYCALLER);
2224 __fsetlocking (stderr, FSETLOCKING_BYCALLER);
2227 setlocale (LC_ALL, "");
2229 /* Make sure the message catalog can be found. */
2230 bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
2232 /* Initialize the message catalog. */
2233 textdomain (PACKAGE_TARNAME);
2235 /* Parse and process arguments. */
2236 const struct argp_child argp_children[] =
2239 .argp = dwfl_standard_argp (),
2240 .header = N_("Input selection options:"),
2245 const struct argp argp =
2248 .parser = parse_opt,
2249 .children = argp_children,
2250 .args_doc = N_("STRIPPED-FILE DEBUG-FILE\n[MODULE...]"),
2252 Combine stripped files with separate symbols and debug information.\v\
2253 The first form puts the result in DEBUG-FILE if -o was not given.\n\
2255 MODULE arguments give file name patterns matching modules to process.\n\
2256 With -f these match the file name of the main (stripped) file \
2257 (slashes are never special), otherwise they match the simple module names. \
2258 With no arguments, process all modules found.\n\
2260 Multiple modules are written to files under OUTPUT-DIRECTORY, \
2261 creating subdirectories as needed. \
2262 With -m these files have simple module names, otherwise they have the \
2263 name of the main file complete with directory underneath OUTPUT-DIRECTORY.\n\
2265 With -n no files are written, but one line to standard output for each module:\
2266 \n\tSTART+SIZE BUILDID FILE DEBUGFILE MODULENAME\n\
2267 START and SIZE are hexadecimal giving the address bounds of the module. \
2268 BUILDID is hexadecimal for the build ID bits, or - if no ID is known; \
2269 the hexadecimal may be followed by @0xADDR giving the address where the \
2270 ID resides if that is known. \
2271 FILE is the file name found for the module, or - if none was found, \
2272 or . if an ELF image is available but not from any named file. \
2273 DEBUGFILE is the separate debuginfo file name, \
2274 or - if no debuginfo was found, or . if FILE contains the debug information.\
2279 struct arg_info info = { .args = NULL };
2280 error_t result = argp_parse (&argp, argc, argv, 0, &remaining, &info);
2281 if (result == ENOSYS)
2282 assert (info.dwfl == NULL);
2284 return EXIT_FAILURE;
2285 assert (info.args != NULL);
2287 /* Tell the library which version we are expecting. */
2288 elf_version (EV_CURRENT);
2290 if (info.dwfl == NULL)
2292 assert (result == ENOSYS);
2294 if (info.output_dir != NULL)
2297 if (asprintf (&file, "%s/%s", info.output_dir, info.args[0]) < 0)
2298 error (EXIT_FAILURE, 0, _("memory exhausted"));
2299 handle_explicit_files (file, true, info.args[0], info.args[1]);
2303 handle_explicit_files (info.output_file, false,
2304 info.args[0], info.args[1]);
2308 /* parse_opt checked this. */
2309 assert (info.output_file != NULL || info.output_dir != NULL || info.list);
2311 handle_implicit_modules (&info);
2313 dwfl_end (info.dwfl);
2320 #include "debugpred.h"