1 /* Discard section not used at runtime from object files.
2 Copyright (C) 2000-2012, 2014, 2015, 2016, 2017 Red Hat, Inc.
3 This file is part of elfutils.
4 Written by Ulrich Drepper <drepper@redhat.com>, 2000.
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/>. */
35 #include <stdio_ext.h>
42 #include <elf-knowledge.h>
47 #include <printversion.h>
49 typedef uint8_t GElf_Byte;
51 /* Name and version of program. */
52 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version;
54 /* Bug report address. */
55 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT;
58 /* Values for the parameters which have no short form. */
59 #define OPT_REMOVE_COMMENT 0x100
60 #define OPT_PERMISSIVE 0x101
61 #define OPT_STRIP_SECTIONS 0x102
62 #define OPT_RELOC_DEBUG 0x103
63 #define OPT_KEEP_SECTION 0x104
66 /* Definitions of arguments for argp functions. */
67 static const struct argp_option options[] =
69 { NULL, 0, NULL, 0, N_("Output selection:"), 0 },
70 { "output", 'o', "FILE", 0, N_("Place stripped output into FILE"), 0 },
71 { NULL, 'f', "FILE", 0, N_("Extract the removed sections into FILE"), 0 },
72 { NULL, 'F', "FILE", 0, N_("Embed name FILE instead of -f argument"), 0 },
74 { NULL, 0, NULL, 0, N_("Output options:"), 0 },
75 { "strip-all", 's', NULL, OPTION_HIDDEN, NULL, 0 },
76 { "strip-debug", 'g', NULL, 0, N_("Remove all debugging symbols"), 0 },
77 { NULL, 'd', NULL, OPTION_ALIAS, NULL, 0 },
78 { NULL, 'S', NULL, OPTION_ALIAS, NULL, 0 },
79 { "strip-sections", OPT_STRIP_SECTIONS, NULL, 0,
80 N_("Remove section headers (not recommended)"), 0 },
81 { "preserve-dates", 'p', NULL, 0,
82 N_("Copy modified/access timestamps to the output"), 0 },
83 { "reloc-debug-sections", OPT_RELOC_DEBUG, NULL, 0,
84 N_("Resolve all trivial relocations between debug sections if the removed sections are placed in a debug file (only relevant for ET_REL files, operation is not reversable, needs -f)"), 0 },
85 { "remove-comment", OPT_REMOVE_COMMENT, NULL, 0,
86 N_("Remove .comment section"), 0 },
87 { "remove-section", 'R', "SECTION", 0, N_("Remove the named section. SECTION is an extended wildcard pattern. May be given more than once. Only non-allocated sections can be removed."), 0 },
88 { "keep-section", OPT_KEEP_SECTION, "SECTION", 0, N_("Keep the named section. SECTION is an extended wildcard pattern. May be given more than once."), 0 },
89 { "permissive", OPT_PERMISSIVE, NULL, 0,
90 N_("Relax a few rules to handle slightly broken ELF files"), 0 },
91 { NULL, 0, NULL, 0, NULL, 0 }
94 /* Short description of program. */
95 static const char doc[] = N_("Discard symbols from object files.");
97 /* Strings for arguments in help texts. */
98 static const char args_doc[] = N_("[FILE...]");
100 /* Prototype for option handler. */
101 static error_t parse_opt (int key, char *arg, struct argp_state *state);
103 /* Data structure to communicate with argp functions. */
104 static struct argp argp =
106 options, parse_opt, args_doc, doc, NULL, NULL, NULL
110 /* Print symbols in file named FNAME. */
111 static int process_file (const char *fname);
113 /* Handle one ELF file. */
114 static int handle_elf (int fd, Elf *elf, const char *prefix,
115 const char *fname, mode_t mode, struct timespec tvp[2]);
117 /* Handle all files contained in the archive. */
118 static int handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
119 struct timespec tvp[2]) __attribute__ ((unused));
121 static int debug_fd = -1;
122 static char *tmp_debug_fname = NULL;
124 /* Close debug file descriptor, if opened. And remove temporary debug file. */
125 static void cleanup_debug (void);
127 #define INTERNAL_ERROR(fname) \
130 error (EXIT_FAILURE, 0, gettext ("%s: INTERNAL ERROR %d (%s): %s"), \
131 fname, __LINE__, PACKAGE_VERSION, elf_errmsg (-1)); \
135 /* Name of the output file. */
136 static const char *output_fname;
138 /* Name of the debug output file. */
139 static const char *debug_fname;
141 /* Name to pretend the debug output file has. */
142 static const char *debug_fname_embed;
144 /* If true output files shall have same date as the input file. */
145 static bool preserve_dates;
147 /* If true .comment sections will be removed. */
148 static bool remove_comment;
150 /* If true remove all debug sections. */
151 static bool remove_debug;
153 /* If true remove all section headers. */
154 static bool remove_shdrs;
156 /* If true relax some ELF rules for input files. */
157 static bool permissive;
159 /* If true perform relocations between debug sections. */
160 static bool reloc_debug;
162 /* Sections the user explicitly wants to keep or remove. */
163 struct section_pattern
166 struct section_pattern *next;
169 static struct section_pattern *keep_secs = NULL;
170 static struct section_pattern *remove_secs = NULL;
173 add_pattern (struct section_pattern **patterns, const char *pattern)
175 struct section_pattern *p = xmalloc (sizeof *p);
176 p->pattern = xstrdup (pattern);
182 free_sec_patterns (struct section_pattern *patterns)
184 struct section_pattern *pattern = patterns;
185 while (pattern != NULL)
187 struct section_pattern *p = pattern;
197 free_sec_patterns (keep_secs);
198 free_sec_patterns (remove_secs);
202 section_name_matches (struct section_pattern *patterns, const char *name)
204 struct section_pattern *pattern = patterns;
205 while (pattern != NULL)
207 if (fnmatch (pattern->pattern, name, FNM_EXTMATCH) == 0)
209 pattern = pattern->next;
216 main (int argc, char *argv[])
221 /* We use no threads here which can interfere with handling a stream. */
222 __fsetlocking (stdin, FSETLOCKING_BYCALLER);
223 __fsetlocking (stdout, FSETLOCKING_BYCALLER);
224 __fsetlocking (stderr, FSETLOCKING_BYCALLER);
227 setlocale (LC_ALL, "");
229 /* Make sure the message catalog can be found. */
230 bindtextdomain (PACKAGE_TARNAME, LOCALEDIR);
232 /* Initialize the message catalog. */
233 textdomain (PACKAGE_TARNAME);
235 /* Parse and process arguments. */
236 if (argp_parse (&argp, argc, argv, 0, &remaining, NULL) != 0)
239 if (reloc_debug && debug_fname == NULL)
240 error (EXIT_FAILURE, 0,
241 gettext ("--reloc-debug-sections used without -f"));
243 /* Tell the library which version we are expecting. */
244 elf_version (EV_CURRENT);
246 if (remaining == argc)
247 /* The user didn't specify a name so we use a.out. */
248 result = process_file ("a.out");
251 /* If we have seen the '-o' or '-f' option there must be exactly one
253 if ((output_fname != NULL || debug_fname != NULL)
254 && remaining + 1 < argc)
255 error (EXIT_FAILURE, 0, gettext ("\
256 Only one input file allowed together with '-o' and '-f'"));
258 /* Process all the remaining files. */
260 result |= process_file (argv[remaining]);
261 while (++remaining < argc);
269 /* Handle program arguments. */
271 parse_opt (int key, char *arg, struct argp_state *state)
276 if (debug_fname != NULL)
278 error (0, 0, gettext ("-f option specified twice"));
285 if (debug_fname_embed != NULL)
287 error (0, 0, gettext ("-F option specified twice"));
290 debug_fname_embed = arg;
294 if (output_fname != NULL)
296 error (0, 0, gettext ("-o option specified twice"));
303 preserve_dates = true;
306 case OPT_RELOC_DEBUG:
310 case OPT_REMOVE_COMMENT:
311 remove_comment = true;
315 if (fnmatch (arg, ".comment", FNM_EXTMATCH) == 0)
316 remove_comment = true;
317 add_pattern (&remove_secs, arg);
320 case OPT_KEEP_SECTION:
321 add_pattern (&keep_secs, arg);
330 case OPT_STRIP_SECTIONS:
338 case 's': /* Ignored for compatibility. */
341 case ARGP_KEY_SUCCESS:
342 if (remove_comment == true
343 && section_name_matches (keep_secs, ".comment"))
346 gettext ("cannot both keep and remove .comment section"));
352 return ARGP_ERR_UNKNOWN;
358 secndx_name (Elf *elf, size_t ndx)
362 Elf_Scn *sec = elf_getscn (elf, ndx);
363 GElf_Shdr *shdr = gelf_getshdr (sec, &mem);
364 if (shdr == NULL || elf_getshdrstrndx (elf, &shstrndx) < 0)
366 return elf_strptr (elf, shstrndx, shdr->sh_name) ?: "???";
369 /* Get the extended section index table data for a symbol table section. */
371 get_xndxdata (Elf *elf, Elf_Scn *symscn)
373 Elf_Data *xndxdata = NULL;
375 GElf_Shdr *shdr = gelf_getshdr (symscn, &shdr_mem);
376 if (shdr != NULL && shdr->sh_type == SHT_SYMTAB)
378 size_t scnndx = elf_ndxscn (symscn);
379 Elf_Scn *xndxscn = NULL;
380 while ((xndxscn = elf_nextscn (elf, xndxscn)) != NULL)
382 GElf_Shdr xndxshdr_mem;
383 GElf_Shdr *xndxshdr = gelf_getshdr (xndxscn, &xndxshdr_mem);
386 && xndxshdr->sh_type == SHT_SYMTAB_SHNDX
387 && xndxshdr->sh_link == scnndx)
389 xndxdata = elf_getdata (xndxscn, NULL);
398 /* Remove any relocations between debug sections in ET_REL
399 for the debug file when requested. These relocations are always
400 zero based between the unallocated sections. */
402 remove_debug_relocations (Ebl *ebl, Elf *elf, GElf_Ehdr *ehdr,
403 const char *fname, size_t shstrndx)
406 while ((scn = elf_nextscn (elf, scn)) != NULL)
408 /* We need the actual section and header from the elf
409 not just the cached original in shdr_info because we
410 might want to change the size. */
412 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
413 if (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA)
415 /* Make sure that this relocation section points to a
416 section to relocate with contents, that isn't
417 allocated and that is a debug section. */
418 Elf_Scn *tscn = elf_getscn (elf, shdr->sh_info);
420 GElf_Shdr *tshdr = gelf_getshdr (tscn, &tshdr_mem);
421 if (tshdr->sh_type == SHT_NOBITS
422 || tshdr->sh_size == 0
423 || (tshdr->sh_flags & SHF_ALLOC) != 0)
426 const char *tname = elf_strptr (elf, shstrndx,
428 if (! tname || ! ebl_debugscn_p (ebl, tname))
431 /* OK, lets relocate all trivial cross debug section
433 Elf_Data *reldata = elf_getdata (scn, NULL);
434 if (reldata == NULL || reldata->d_buf == NULL)
435 INTERNAL_ERROR (fname);
437 /* Make sure we adjust the uncompressed debug data
438 (and recompress if necessary at the end). */
440 int tcompress_type = 0;
441 if (gelf_getchdr (tscn, &tchdr) != NULL)
443 tcompress_type = tchdr.ch_type;
444 if (elf_compress (tscn, 0, 0) != 1)
445 INTERNAL_ERROR (fname);
448 Elf_Data *tdata = elf_getdata (tscn, NULL);
449 if (tdata == NULL || tdata->d_buf == NULL
450 || tdata->d_type != ELF_T_BYTE)
451 INTERNAL_ERROR (fname);
453 /* Pick up the symbol table and shndx table to
454 resolve relocation symbol indexes. */
455 Elf64_Word symt = shdr->sh_link;
456 Elf_Data *symdata, *xndxdata;
457 Elf_Scn * symscn = elf_getscn (elf, symt);
458 symdata = elf_getdata (symscn, NULL);
459 xndxdata = get_xndxdata (elf, symscn);
461 INTERNAL_ERROR (fname);
463 /* Apply one relocation. Returns true when trivial
464 relocation actually done. */
465 bool relocate (GElf_Addr offset, const GElf_Sxword addend,
466 bool is_rela, int rtype, int symndx)
468 /* R_*_NONE relocs can always just be removed. */
472 /* We only do simple absolute relocations. */
474 Elf_Type type = ebl_reloc_simple_type (ebl, rtype, &addsub);
475 if (type == ELF_T_NUM)
478 /* These are the types we can relocate. */
479 #define TYPES DO_TYPE (BYTE, Byte); DO_TYPE (HALF, Half); \
480 DO_TYPE (WORD, Word); DO_TYPE (SWORD, Sword); \
481 DO_TYPE (XWORD, Xword); DO_TYPE (SXWORD, Sxword)
483 /* And only for relocations against other debug sections. */
486 GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
489 Elf32_Word sec = (sym->st_shndx == SHN_XINDEX
490 ? xndx : sym->st_shndx);
492 if (ebl_debugscn_p (ebl, secndx_name (elf, sec)))
496 #define DO_TYPE(NAME, Name) GElf_##Name Name;
497 union { TYPES; } tmpbuf;
502 #define DO_TYPE(NAME, Name) \
504 size = sizeof (GElf_##Name); \
513 if (offset > tdata->d_size
514 || tdata->d_size - offset < size)
517 error (EXIT_FAILURE, 0, gettext ("bad relocation"));
520 /* When the symbol value is zero then for SHT_REL
521 sections this is all that needs to be checked.
522 The addend is contained in the original data at
523 the offset already. So if the (section) symbol
524 address is zero and the given addend is zero
525 just remove the relocation, it isn't needed
527 if (addend == 0 && sym->st_value == 0)
535 .d_version = EV_CURRENT,
540 .d_buf = tdata->d_buf + offset,
542 .d_version = EV_CURRENT,
545 GElf_Addr value = sym->st_value;
548 /* For SHT_RELA sections we just take the
549 given addend and add it to the value. */
551 /* For ADD/SUB relocations we need to fetch the
552 current section contents. */
555 Elf_Data *d = gelf_xlatetom (elf, &tmpdata,
557 ehdr->e_ident[EI_DATA]);
559 INTERNAL_ERROR (fname);
560 assert (d == &tmpdata);
565 /* For SHT_REL sections we have to peek at
566 what is already in the section at the given
567 offset to get the addend. */
568 Elf_Data *d = gelf_xlatetom (elf, &tmpdata,
570 ehdr->e_ident[EI_DATA]);
572 INTERNAL_ERROR (fname);
573 assert (d == &tmpdata);
578 #define DO_TYPE(NAME, Name) \
581 tmpbuf.Name -= (GElf_##Name) value; \
583 tmpbuf.Name += (GElf_##Name) value; \
591 /* Now finally put in the new value. */
592 Elf_Data *s = gelf_xlatetof (elf, &rdata,
594 ehdr->e_ident[EI_DATA]);
596 INTERNAL_ERROR (fname);
597 assert (s == &rdata);
604 if (shdr->sh_entsize == 0)
605 INTERNAL_ERROR (fname);
607 size_t nrels = shdr->sh_size / shdr->sh_entsize;
609 if (shdr->sh_type == SHT_REL)
610 for (size_t relidx = 0; relidx < nrels; ++relidx)
613 GElf_Rel *r = gelf_getrel (reldata, relidx, &rel_mem);
614 if (! relocate (r->r_offset, 0, false,
615 GELF_R_TYPE (r->r_info),
616 GELF_R_SYM (r->r_info)))
619 gelf_update_rel (reldata, next, r);
624 for (size_t relidx = 0; relidx < nrels; ++relidx)
627 GElf_Rela *r = gelf_getrela (reldata, relidx, &rela_mem);
628 if (! relocate (r->r_offset, r->r_addend, true,
629 GELF_R_TYPE (r->r_info),
630 GELF_R_SYM (r->r_info)))
633 gelf_update_rela (reldata, next, r);
639 shdr->sh_size = reldata->d_size = nrels * shdr->sh_entsize;
640 gelf_update_shdr (scn, shdr);
642 if (tcompress_type != 0)
643 if (elf_compress (tscn, tcompress_type, ELF_CHF_FORCE) != 1)
644 INTERNAL_ERROR (fname);
650 process_file (const char *fname)
652 /* If we have to preserve the modify and access timestamps get them
653 now. We cannot use fstat() after opening the file since the open
654 would change the access time. */
656 struct timespec tv[2];
660 if (stat (fname, &pre_st) != 0)
662 error (0, errno, gettext ("cannot stat input file '%s'"), fname);
666 /* If we have to preserve the timestamp, we need it in the
667 format utimes() understands. */
668 tv[0] = pre_st.st_atim;
669 tv[1] = pre_st.st_mtim;
673 int fd = open (fname, output_fname == NULL ? O_RDWR : O_RDONLY);
676 error (0, errno, gettext ("while opening '%s'"), fname);
680 /* We always use fstat() even if we called stat() before. This is
681 done to make sure the information returned by stat() is for the
684 if (fstat (fd, &st) != 0)
686 error (0, errno, gettext ("cannot stat input file '%s'"), fname);
689 /* Paranoid mode on. */
691 && (st.st_ino != pre_st.st_ino || st.st_dev != pre_st.st_dev))
693 /* We detected a race. Try again. */
698 /* Now get the ELF descriptor. */
699 Elf *elf = elf_begin (fd, output_fname == NULL ? ELF_C_RDWR : ELF_C_READ,
702 switch (elf_kind (elf))
705 result = handle_elf (fd, elf, NULL, fname, st.st_mode & ACCESSPERMS,
706 preserve_dates ? tv : NULL);
710 /* It is not possible to strip the content of an archive direct
711 the output to a specific file. */
712 if (unlikely (output_fname != NULL || debug_fname != NULL))
714 error (0, 0, gettext ("%s: cannot use -o or -f when stripping archive"),
720 /* We would like to support ar archives, but currently it just
721 doesn't work at all since we call elf_clone on the members
722 which doesn't really support ar members.
723 result = handle_ar (fd, elf, NULL, fname,
724 preserve_dates ? tv : NULL);
726 error (0, 0, gettext ("%s: no support for stripping archive"),
733 error (0, 0, gettext ("%s: File format not recognized"), fname);
738 if (unlikely (elf_end (elf) != 0))
739 INTERNAL_ERROR (fname);
747 /* Maximum size of array allocated on stack. */
748 #define MAX_STACK_ALLOC (400 * 1024)
751 handle_elf (int fd, Elf *elf, const char *prefix, const char *fname,
752 mode_t mode, struct timespec tvp[2])
754 size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
755 size_t fname_len = strlen (fname) + 1;
756 char *fullname = alloca (prefix_len + 1 + fname_len);
758 Elf *debugelf = NULL;
759 tmp_debug_fname = NULL;
768 Elf_Data *debug_data;
770 Elf32_Word idx; /* Index in new file. */
771 Elf32_Word old_sh_link; /* Original value of shdr.sh_link. */
772 Elf32_Word symtab_idx;
773 Elf32_Word version_idx;
774 Elf32_Word group_idx;
775 Elf32_Word group_cnt;
778 Elf32_Word *newsymidx;
784 GElf_Ehdr newehdr_mem;
786 GElf_Ehdr debugehdr_mem;
787 GElf_Ehdr *debugehdr;
788 Dwelf_Strtab *shst = NULL;
789 Elf_Data debuglink_crc_data;
790 bool any_symtab_changes = false;
791 Elf_Data *shstrtab_data = NULL;
792 void *debuglink_buf = NULL;
794 /* Create the full name of the file. */
797 cp = mempcpy (cp, prefix, prefix_len);
800 memcpy (cp, fname, fname_len);
802 /* If we are not replacing the input file open a new file here. */
803 if (output_fname != NULL)
805 fd = open (output_fname, O_RDWR | O_CREAT, mode);
806 if (unlikely (fd == -1))
808 error (0, errno, gettext ("cannot open '%s'"), output_fname);
815 /* Get the EBL handling. Removing all debugging symbols with the -g
816 option or resolving all relocations between debug sections with
817 the --reloc-debug-sections option are currently the only reasons
818 we need EBL so don't open the backend unless necessary. */
820 if (remove_debug || reloc_debug)
822 ebl = ebl_openbackend (elf);
825 error (0, errno, gettext ("cannot open EBL backend"));
831 /* Open the additional file the debug information will be stored in. */
832 if (debug_fname != NULL)
834 /* Create a temporary file name. We do not want to overwrite
835 the debug file if the file would not contain any
837 size_t debug_fname_len = strlen (debug_fname);
838 tmp_debug_fname = (char *) xmalloc (debug_fname_len + sizeof (".XXXXXX"));
839 strcpy (mempcpy (tmp_debug_fname, debug_fname, debug_fname_len),
842 debug_fd = mkstemp (tmp_debug_fname);
843 if (unlikely (debug_fd == -1))
845 error (0, errno, gettext ("cannot open '%s'"), debug_fname);
851 /* Get the information from the old file. */
853 GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
855 INTERNAL_ERROR (fname);
857 /* Get the section header string table index. */
858 if (unlikely (elf_getshdrstrndx (elf, &shstrndx) < 0))
861 error (EXIT_FAILURE, 0,
862 gettext ("cannot get section header string table index"));
865 /* Get the number of phdrs in the old file. */
867 if (elf_getphdrnum (elf, &phnum) != 0)
870 error (EXIT_FAILURE, 0, gettext ("cannot get number of phdrs"));
873 /* We now create a new ELF descriptor for the same file. We
874 construct it almost exactly in the same way with some information
877 if (output_fname != NULL)
878 newelf = elf_begin (fd, ELF_C_WRITE_MMAP, NULL);
880 newelf = elf_clone (elf, ELF_C_EMPTY);
882 if (unlikely (gelf_newehdr (newelf, gelf_getclass (elf)) == 0))
884 error (0, 0, gettext ("cannot create new ehdr for file '%s': %s"),
885 output_fname ?: fname, elf_errmsg (-1));
889 /* Copy over the old program header if needed. */
892 if (unlikely (gelf_newphdr (newelf, phnum) == 0))
894 error (0, 0, gettext ("cannot create new phdr for file '%s': %s"),
895 output_fname ?: fname, elf_errmsg (-1));
899 for (cnt = 0; cnt < phnum; ++cnt)
902 GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem);
904 || unlikely (gelf_update_phdr (newelf, cnt, phdr) == 0))
905 INTERNAL_ERROR (fname);
909 if (debug_fname != NULL)
911 /* Also create an ELF descriptor for the debug file */
912 debugelf = elf_begin (debug_fd, ELF_C_WRITE_MMAP, NULL);
913 if (unlikely (gelf_newehdr (debugelf, gelf_getclass (elf)) == 0))
915 error (0, 0, gettext ("cannot create new ehdr for file '%s': %s"),
916 debug_fname, elf_errmsg (-1));
920 /* Copy over the old program header if needed. */
923 if (unlikely (gelf_newphdr (debugelf, phnum) == 0))
925 error (0, 0, gettext ("cannot create new phdr for file '%s': %s"),
926 debug_fname, elf_errmsg (-1));
930 for (cnt = 0; cnt < phnum; ++cnt)
933 GElf_Phdr *phdr = gelf_getphdr (elf, cnt, &phdr_mem);
935 || unlikely (gelf_update_phdr (debugelf, cnt, phdr) == 0))
936 INTERNAL_ERROR (fname);
941 /* Number of sections. */
943 if (unlikely (elf_getshdrnum (elf, &shnum) < 0))
945 error (0, 0, gettext ("cannot determine number of sections: %s"),
950 if (shstrndx >= shnum)
953 #define elf_assert(test) do { if (!(test)) goto illformed; } while (0)
955 /* Storage for section information. We leave room for two more
956 entries since we unconditionally create a section header string
957 table. Maybe some weird tool created an ELF file without one.
958 The other one is used for the debug link section. */
959 if ((shnum + 2) * sizeof (struct shdr_info) > MAX_STACK_ALLOC)
960 shdr_info = (struct shdr_info *) xcalloc (shnum + 2,
961 sizeof (struct shdr_info));
964 shdr_info = (struct shdr_info *) alloca ((shnum + 2)
965 * sizeof (struct shdr_info));
966 memset (shdr_info, '\0', (shnum + 2) * sizeof (struct shdr_info));
969 /* Track whether allocated sections all come before non-allocated ones. */
970 bool seen_allocated = false;
971 bool seen_unallocated = false;
972 bool mixed_allocated_unallocated = false;
974 /* Prepare section information data structure. */
977 while ((scn = elf_nextscn (elf, scn)) != NULL)
979 /* This should always be true (i.e., there should not be any
980 holes in the numbering). */
981 elf_assert (elf_ndxscn (scn) == cnt);
983 shdr_info[cnt].scn = scn;
985 /* Get the header. */
986 if (gelf_getshdr (scn, &shdr_info[cnt].shdr) == NULL)
987 INTERNAL_ERROR (fname);
989 /* Normally (in non-ET_REL files) we see all allocated sections first,
990 then all non-allocated. */
991 if ((shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) == 0)
992 seen_unallocated = true;
995 if (seen_unallocated && seen_allocated)
996 mixed_allocated_unallocated = true;
997 seen_allocated = true;
1000 /* Get the name of the section. */
1001 shdr_info[cnt].name = elf_strptr (elf, shstrndx,
1002 shdr_info[cnt].shdr.sh_name);
1003 if (shdr_info[cnt].name == NULL)
1006 error (0, 0, gettext ("illformed file '%s'"), fname);
1010 /* Sanity check the user. */
1011 if (section_name_matches (remove_secs, shdr_info[cnt].name))
1013 if ((shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) != 0)
1016 gettext ("Cannot remove allocated section '%s'"),
1017 shdr_info[cnt].name);
1022 if (section_name_matches (keep_secs, shdr_info[cnt].name))
1025 gettext ("Cannot both keep and remove section '%s'"),
1026 shdr_info[cnt].name);
1032 /* Mark them as present but not yet investigated. */
1033 shdr_info[cnt].idx = 1;
1035 /* Remember the shdr.sh_link value. */
1036 shdr_info[cnt].old_sh_link = shdr_info[cnt].shdr.sh_link;
1037 if (shdr_info[cnt].old_sh_link >= shnum)
1040 /* Sections in files other than relocatable object files which
1041 not loaded can be freely moved by us. In theory we can also
1042 freely move around allocated nobits sections. But we don't
1043 to keep the layout of all allocated sections as similar as
1044 possible to the original file. In relocatable object files
1045 everything can be moved. */
1047 || (shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) == 0)
1048 shdr_info[cnt].shdr.sh_offset = 0;
1050 /* If this is an extended section index table store an
1051 appropriate reference. */
1052 if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX))
1054 elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx == 0);
1055 shdr_info[shdr_info[cnt].shdr.sh_link].symtab_idx = cnt;
1057 else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GROUP))
1059 /* Cross-reference the sections contained in the section
1061 shdr_info[cnt].data = elf_getdata (shdr_info[cnt].scn, NULL);
1062 if (shdr_info[cnt].data == NULL
1063 || shdr_info[cnt].data->d_size < sizeof (Elf32_Word))
1064 INTERNAL_ERROR (fname);
1066 /* XXX Fix for unaligned access. */
1067 Elf32_Word *grpref = (Elf32_Word *) shdr_info[cnt].data->d_buf;
1070 inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
1073 if (grpref[inner] < shnum)
1074 shdr_info[grpref[inner]].group_idx = cnt;
1079 if (inner == 1 || (inner == 2 && (grpref[0] & GRP_COMDAT) == 0))
1080 /* If the section group contains only one element and this
1081 is n COMDAT section we can drop it right away. */
1082 shdr_info[cnt].idx = 0;
1084 shdr_info[cnt].group_cnt = inner - 1;
1086 else if (unlikely (shdr_info[cnt].shdr.sh_type == SHT_GNU_versym))
1088 elf_assert (shdr_info[shdr_info[cnt].shdr.sh_link].version_idx == 0);
1089 shdr_info[shdr_info[cnt].shdr.sh_link].version_idx = cnt;
1092 /* If this section is part of a group make sure it is not
1093 discarded right away. */
1094 if ((shdr_info[cnt].shdr.sh_flags & SHF_GROUP) != 0)
1096 elf_assert (shdr_info[cnt].group_idx != 0);
1098 if (shdr_info[shdr_info[cnt].group_idx].idx == 0)
1100 /* The section group section might be removed.
1101 Don't remove the SHF_GROUP flag. The section is
1102 either also removed, in which case the flag doesn't matter.
1103 Or it moves with the group into the debug file, then
1104 it will be reconnected with the new group and should
1105 still have the flag set. */
1106 shdr_info[cnt].group_idx = 0;
1110 /* Increment the counter. */
1114 /* Now determine which sections can go away. The general rule is that
1115 all sections which are not used at runtime are stripped out. But
1116 there are a few exceptions:
1118 - special sections named ".comment" and ".note" are kept
1119 - OS or architecture specific sections are kept since we might not
1120 know how to handle them
1121 - if a section is referred to from a section which is not removed
1122 in the sh_link or sh_info element it cannot be removed either
1123 - the user might have explicitly said to remove or keep a section
1125 for (cnt = 1; cnt < shnum; ++cnt)
1126 /* Check whether the section can be removed. Since we will create
1127 a new .shstrtab assume it will be removed too. */
1128 if (remove_shdrs ? !(shdr_info[cnt].shdr.sh_flags & SHF_ALLOC)
1129 : (ebl_section_strip_p (ebl, &shdr_info[cnt].shdr,
1130 shdr_info[cnt].name, remove_comment,
1133 || section_name_matches (remove_secs, shdr_info[cnt].name)))
1135 /* The user might want to explicitly keep this one. */
1136 if (section_name_matches (keep_secs, shdr_info[cnt].name))
1139 /* For now assume this section will be removed. */
1140 shdr_info[cnt].idx = 0;
1142 idx = shdr_info[cnt].group_idx;
1145 /* The section group data is already loaded. */
1146 elf_assert (shdr_info[idx].data != NULL
1147 && shdr_info[idx].data->d_buf != NULL
1148 && shdr_info[idx].data->d_size >= sizeof (Elf32_Word));
1150 /* If the references section group is a normal section
1151 group and has one element remaining, or if it is an
1152 empty COMDAT section group it is removed. */
1153 bool is_comdat = (((Elf32_Word *) shdr_info[idx].data->d_buf)[0]
1156 --shdr_info[idx].group_cnt;
1157 if ((!is_comdat && shdr_info[idx].group_cnt == 1)
1158 || (is_comdat && shdr_info[idx].group_cnt == 0))
1160 shdr_info[idx].idx = 0;
1161 /* Continue recursively. */
1162 idx = shdr_info[idx].group_idx;
1169 /* Mark the SHT_NULL section as handled. */
1170 shdr_info[0].idx = 2;
1173 /* Handle exceptions: section groups and cross-references. We might
1174 have to repeat this a few times since the resetting of the flag
1180 for (cnt = 1; cnt < shnum; ++cnt)
1182 if (shdr_info[cnt].idx == 0)
1184 /* If a relocation section is marked as being removed make
1185 sure the section it is relocating is removed, too. */
1186 if (shdr_info[cnt].shdr.sh_type == SHT_REL
1187 || shdr_info[cnt].shdr.sh_type == SHT_RELA)
1189 if (shdr_info[cnt].shdr.sh_info >= shnum)
1191 else if (shdr_info[shdr_info[cnt].shdr.sh_info].idx != 0)
1192 shdr_info[cnt].idx = 1;
1195 /* If a group section is marked as being removed make
1196 sure all the sections it contains are being removed, too. */
1197 if (shdr_info[cnt].shdr.sh_type == SHT_GROUP)
1200 grpref = (Elf32_Word *) shdr_info[cnt].data->d_buf;
1202 in < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
1204 if (grpref[in] < shnum)
1206 if (shdr_info[grpref[in]].idx != 0)
1208 shdr_info[cnt].idx = 1;
1217 if (shdr_info[cnt].idx == 1)
1219 /* The content of symbol tables we don't remove must not
1220 reference any section which we do remove. Otherwise
1221 we cannot remove the section. */
1222 if (debug_fname != NULL
1223 && shdr_info[cnt].debug_data == NULL
1224 && (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM
1225 || shdr_info[cnt].shdr.sh_type == SHT_SYMTAB))
1227 /* Make sure the data is loaded. */
1228 if (shdr_info[cnt].data == NULL)
1231 = elf_getdata (shdr_info[cnt].scn, NULL);
1232 if (shdr_info[cnt].data == NULL)
1233 INTERNAL_ERROR (fname);
1235 Elf_Data *symdata = shdr_info[cnt].data;
1237 /* If there is an extended section index table load it
1239 if (shdr_info[cnt].symtab_idx != 0
1240 && shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
1242 elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB);
1244 shdr_info[shdr_info[cnt].symtab_idx].data
1245 = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
1247 if (shdr_info[shdr_info[cnt].symtab_idx].data == NULL)
1248 INTERNAL_ERROR (fname);
1251 = shdr_info[shdr_info[cnt].symtab_idx].data;
1253 /* Go through all symbols and make sure the section they
1254 reference is not removed. */
1255 size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
1257 for (size_t inner = 0;
1258 inner < shdr_info[cnt].data->d_size / elsize;
1263 GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata,
1267 INTERNAL_ERROR (fname);
1269 size_t scnidx = sym->st_shndx;
1270 if (scnidx == SHN_UNDEF || scnidx >= shnum
1271 || (scnidx >= SHN_LORESERVE
1272 && scnidx <= SHN_HIRESERVE
1273 && scnidx != SHN_XINDEX)
1274 /* Don't count in the section symbols. */
1275 || GELF_ST_TYPE (sym->st_info) == STT_SECTION)
1276 /* This is no section index, leave it alone. */
1278 else if (scnidx == SHN_XINDEX)
1281 if (scnidx >= shnum)
1284 if (shdr_info[scnidx].idx == 0)
1285 /* This symbol table has a real symbol in
1286 a discarded section. So preserve the
1287 original table in the debug file. Unless
1288 it is a redundant data marker to a debug
1289 (data only) section. */
1290 if (! (ebl_section_strip_p (ebl,
1291 &shdr_info[scnidx].shdr,
1292 shdr_info[scnidx].name,
1295 && ebl_data_marker_symbol (ebl, sym,
1297 shdr_info[cnt].shdr.sh_link,
1299 shdr_info[cnt].debug_data = symdata;
1303 /* Cross referencing happens:
1304 - for the cases the ELF specification says. That are
1305 + SHT_DYNAMIC in sh_link to string table
1306 + SHT_HASH in sh_link to symbol table
1307 + SHT_REL and SHT_RELA in sh_link to symbol table
1308 + SHT_SYMTAB and SHT_DYNSYM in sh_link to string table
1309 + SHT_GROUP in sh_link to symbol table
1310 + SHT_SYMTAB_SHNDX in sh_link to symbol table
1311 Other (OS or architecture-specific) sections might as
1312 well use this field so we process it unconditionally.
1313 - references inside section groups
1314 - specially marked references in sh_info if the SHF_INFO_LINK
1318 if (shdr_info[shdr_info[cnt].shdr.sh_link].idx == 0)
1320 shdr_info[shdr_info[cnt].shdr.sh_link].idx = 1;
1321 changes |= shdr_info[cnt].shdr.sh_link < cnt;
1324 /* Handle references through sh_info. */
1325 if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
1327 if (shdr_info[cnt].shdr.sh_info >= shnum)
1329 else if ( shdr_info[shdr_info[cnt].shdr.sh_info].idx == 0)
1331 shdr_info[shdr_info[cnt].shdr.sh_info].idx = 1;
1332 changes |= shdr_info[cnt].shdr.sh_info < cnt;
1336 /* Mark the section as investigated. */
1337 shdr_info[cnt].idx = 2;
1340 if (debug_fname != NULL
1341 && (shdr_info[cnt].idx == 0 || shdr_info[cnt].debug_data != NULL))
1343 /* This section is being preserved in the debug file.
1344 Sections it refers to must be preserved there too.
1346 In this pass we mark sections to be preserved in both
1347 files by setting the .debug_data pointer to the original
1348 file's .data pointer. Below, we'll copy the section
1351 inline void check_preserved (size_t i)
1353 if (i != 0 && i < shnum + 2 && shdr_info[i].idx != 0
1354 && shdr_info[i].debug_data == NULL)
1356 if (shdr_info[i].data == NULL)
1357 shdr_info[i].data = elf_getdata (shdr_info[i].scn, NULL);
1358 if (shdr_info[i].data == NULL)
1359 INTERNAL_ERROR (fname);
1361 shdr_info[i].debug_data = shdr_info[i].data;
1366 check_preserved (shdr_info[cnt].shdr.sh_link);
1367 if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
1368 check_preserved (shdr_info[cnt].shdr.sh_info);
1374 /* Copy the removed sections to the debug output file.
1375 The ones that are not removed in the stripped file are SHT_NOBITS. */
1376 if (debug_fname != NULL)
1378 for (cnt = 1; cnt < shnum; ++cnt)
1380 scn = elf_newscn (debugelf);
1384 error (EXIT_FAILURE, 0,
1385 gettext ("while generating output file: %s"),
1389 bool discard_section = (shdr_info[cnt].idx > 0
1390 && shdr_info[cnt].debug_data == NULL
1391 && shdr_info[cnt].shdr.sh_type != SHT_NOTE
1392 && shdr_info[cnt].shdr.sh_type != SHT_GROUP
1393 && cnt != shstrndx);
1395 /* Set the section header in the new file. */
1396 GElf_Shdr debugshdr = shdr_info[cnt].shdr;
1397 if (discard_section)
1398 debugshdr.sh_type = SHT_NOBITS;
1400 if (unlikely (gelf_update_shdr (scn, &debugshdr) == 0))
1401 /* There cannot be any overflows. */
1402 INTERNAL_ERROR (fname);
1404 /* Get the data from the old file if necessary. */
1405 if (shdr_info[cnt].data == NULL)
1407 shdr_info[cnt].data = elf_getdata (shdr_info[cnt].scn, NULL);
1408 if (shdr_info[cnt].data == NULL)
1409 INTERNAL_ERROR (fname);
1412 /* Set the data. This is done by copying from the old file. */
1413 Elf_Data *debugdata = elf_newdata (scn);
1414 if (debugdata == NULL)
1415 INTERNAL_ERROR (fname);
1417 /* Copy the structure. This data may be modified in place
1418 before we write out the file. */
1419 *debugdata = *shdr_info[cnt].data;
1420 if (discard_section)
1421 debugdata->d_buf = NULL;
1422 else if (shdr_info[cnt].debug_data != NULL
1423 || shdr_info[cnt].shdr.sh_type == SHT_GROUP)
1425 /* Copy the original data before it gets modified. */
1426 shdr_info[cnt].debug_data = debugdata;
1427 if (debugdata->d_buf == NULL)
1428 INTERNAL_ERROR (fname);
1429 debugdata->d_buf = memcpy (xmalloc (debugdata->d_size),
1430 debugdata->d_buf, debugdata->d_size);
1434 /* Finish the ELF header. Fill in the fields not handled by
1435 libelf from the old file. */
1436 debugehdr = gelf_getehdr (debugelf, &debugehdr_mem);
1437 if (debugehdr == NULL)
1438 INTERNAL_ERROR (fname);
1440 memcpy (debugehdr->e_ident, ehdr->e_ident, EI_NIDENT);
1441 debugehdr->e_type = ehdr->e_type;
1442 debugehdr->e_machine = ehdr->e_machine;
1443 debugehdr->e_version = ehdr->e_version;
1444 debugehdr->e_entry = ehdr->e_entry;
1445 debugehdr->e_flags = ehdr->e_flags;
1448 if (elf_getshdrstrndx (elf, &shdrstrndx) < 0)
1450 error (0, 0, gettext ("%s: error while getting shdrstrndx: %s"),
1451 fname, elf_errmsg (-1));
1456 if (shstrndx < SHN_LORESERVE)
1457 debugehdr->e_shstrndx = shdrstrndx;
1460 debugehdr->e_shstrndx = SHN_XINDEX;
1461 Elf_Scn *scn0 = elf_getscn (debugelf, 0);
1462 GElf_Shdr shdr0_mem;
1463 GElf_Shdr *shdr0 = gelf_getshdr (scn0, &shdr0_mem);
1466 error (0, 0, gettext ("%s: error getting zero section: %s"),
1467 debug_fname, elf_errmsg (-1));
1472 shdr0->sh_link = shdrstrndx;
1473 if (gelf_update_shdr (scn0, shdr0) == 0)
1475 error (0, 0, gettext ("%s: error while updating zero section: %s"),
1476 debug_fname, elf_errmsg (-1));
1483 if (unlikely (gelf_update_ehdr (debugelf, debugehdr) == 0))
1485 error (0, 0, gettext ("%s: error while creating ELF header: %s"),
1486 debug_fname, elf_errmsg (-1));
1492 /* Although we always create a new section header string table we
1493 don't explicitly mark the existing one as unused. It can still
1494 be used through a symbol table section we are keeping. If not it
1495 will already be marked as unused. */
1497 /* We need a string table for the section headers. */
1498 shst = dwelf_strtab_init (true);
1502 error (EXIT_FAILURE, errno, gettext ("while preparing output for '%s'"),
1503 output_fname ?: fname);
1506 /* Assign new section numbers. */
1507 shdr_info[0].idx = 0;
1508 for (cnt = idx = 1; cnt < shnum; ++cnt)
1509 if (shdr_info[cnt].idx > 0)
1511 shdr_info[cnt].idx = idx++;
1513 /* Create a new section. */
1514 shdr_info[cnt].newscn = elf_newscn (newelf);
1515 if (shdr_info[cnt].newscn == NULL)
1518 error (EXIT_FAILURE, 0,
1519 gettext ("while generating output file: %s"),
1523 elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
1525 /* Add this name to the section header string table. */
1526 shdr_info[cnt].se = dwelf_strtab_add (shst, shdr_info[cnt].name);
1529 /* Test whether we are doing anything at all. Either all removable
1530 sections are already gone. Or the only section we would remove is
1531 the .shstrtab section which we would add again. */
1532 bool removing_sections = !(cnt == idx
1534 && shdr_info[shstrndx].idx == 0));
1535 if (output_fname == NULL && !removing_sections)
1538 /* Create the reference to the file with the debug info (if any). */
1539 if (debug_fname != NULL && !remove_shdrs && removing_sections)
1541 /* Add the section header string table section name. */
1542 shdr_info[cnt].se = dwelf_strtab_add_len (shst, ".gnu_debuglink", 15);
1543 shdr_info[cnt].idx = idx++;
1545 /* Create the section header. */
1546 shdr_info[cnt].shdr.sh_type = SHT_PROGBITS;
1547 shdr_info[cnt].shdr.sh_flags = 0;
1548 shdr_info[cnt].shdr.sh_addr = 0;
1549 shdr_info[cnt].shdr.sh_link = SHN_UNDEF;
1550 shdr_info[cnt].shdr.sh_info = SHN_UNDEF;
1551 shdr_info[cnt].shdr.sh_entsize = 0;
1552 shdr_info[cnt].shdr.sh_addralign = 4;
1553 /* We set the offset to zero here. Before we write the ELF file the
1554 field must have the correct value. This is done in the final
1555 loop over all section. Then we have all the information needed. */
1556 shdr_info[cnt].shdr.sh_offset = 0;
1558 /* Create the section. */
1559 shdr_info[cnt].newscn = elf_newscn (newelf);
1560 if (shdr_info[cnt].newscn == NULL)
1563 error (EXIT_FAILURE, 0,
1564 gettext ("while create section header section: %s"),
1567 elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == shdr_info[cnt].idx);
1569 shdr_info[cnt].data = elf_newdata (shdr_info[cnt].newscn);
1570 if (shdr_info[cnt].data == NULL)
1573 error (EXIT_FAILURE, 0, gettext ("cannot allocate section data: %s"),
1577 char *debug_basename = basename (debug_fname_embed ?: debug_fname);
1578 off_t crc_offset = strlen (debug_basename) + 1;
1579 /* Align to 4 byte boundary */
1580 crc_offset = ((crc_offset - 1) & ~3) + 4;
1582 shdr_info[cnt].data->d_align = 4;
1583 shdr_info[cnt].shdr.sh_size = shdr_info[cnt].data->d_size
1585 debuglink_buf = xcalloc (1, shdr_info[cnt].data->d_size);
1586 shdr_info[cnt].data->d_buf = debuglink_buf;
1588 strcpy (shdr_info[cnt].data->d_buf, debug_basename);
1590 /* Cache this Elf_Data describing the CRC32 word in the section.
1591 We'll fill this in when we have written the debug file. */
1592 debuglink_crc_data = *shdr_info[cnt].data;
1593 debuglink_crc_data.d_buf = ((char *) debuglink_crc_data.d_buf
1595 debuglink_crc_data.d_size = 4;
1597 /* One more section done. */
1601 /* Index of the section header table in the shdr_info array. */
1604 /* Add the section header string table section name. */
1605 shdr_info[cnt].se = dwelf_strtab_add_len (shst, ".shstrtab", 10);
1606 shdr_info[cnt].idx = idx;
1608 /* Create the section header. */
1609 shdr_info[cnt].shdr.sh_type = SHT_STRTAB;
1610 shdr_info[cnt].shdr.sh_flags = 0;
1611 shdr_info[cnt].shdr.sh_addr = 0;
1612 shdr_info[cnt].shdr.sh_link = SHN_UNDEF;
1613 shdr_info[cnt].shdr.sh_info = SHN_UNDEF;
1614 shdr_info[cnt].shdr.sh_entsize = 0;
1615 /* We set the offset to zero here. Before we write the ELF file the
1616 field must have the correct value. This is done in the final
1617 loop over all section. Then we have all the information needed. */
1618 shdr_info[cnt].shdr.sh_offset = 0;
1619 shdr_info[cnt].shdr.sh_addralign = 1;
1621 /* Create the section. */
1622 shdr_info[cnt].newscn = elf_newscn (newelf);
1623 if (shdr_info[cnt].newscn == NULL)
1626 error (EXIT_FAILURE, 0,
1627 gettext ("while create section header section: %s"),
1630 elf_assert (elf_ndxscn (shdr_info[cnt].newscn) == idx);
1632 /* Finalize the string table and fill in the correct indices in the
1634 shstrtab_data = elf_newdata (shdr_info[cnt].newscn);
1635 if (shstrtab_data == NULL)
1638 error (EXIT_FAILURE, 0,
1639 gettext ("while create section header string table: %s"),
1642 if (dwelf_strtab_finalize (shst, shstrtab_data) == NULL)
1645 error (EXIT_FAILURE, 0,
1646 gettext ("no memory to create section header string table"));
1649 /* We have to set the section size. */
1650 shdr_info[cnt].shdr.sh_size = shstrtab_data->d_size;
1652 /* Update the section information. */
1653 GElf_Off lastoffset = 0;
1654 for (cnt = 1; cnt <= shdridx; ++cnt)
1655 if (shdr_info[cnt].idx > 0)
1659 scn = elf_getscn (newelf, shdr_info[cnt].idx);
1660 elf_assert (scn != NULL);
1662 /* Update the name. */
1663 shdr_info[cnt].shdr.sh_name = dwelf_strent_off (shdr_info[cnt].se);
1665 /* Update the section header from the input file. Some fields
1666 might be section indeces which now have to be adjusted. Keep
1667 the index to the "current" sh_link in case we need it to lookup
1668 symbol table names. */
1669 size_t sh_link = shdr_info[cnt].shdr.sh_link;
1670 if (shdr_info[cnt].shdr.sh_link != 0)
1671 shdr_info[cnt].shdr.sh_link =
1672 shdr_info[shdr_info[cnt].shdr.sh_link].idx;
1674 if (shdr_info[cnt].shdr.sh_type == SHT_GROUP)
1676 elf_assert (shdr_info[cnt].data != NULL
1677 && shdr_info[cnt].data->d_buf != NULL);
1679 Elf32_Word *grpref = (Elf32_Word *) shdr_info[cnt].data->d_buf;
1680 /* First word is the section group flag.
1681 Followed by section indexes, that need to be renumbered. */
1682 for (size_t inner = 1;
1683 inner < shdr_info[cnt].data->d_size / sizeof (Elf32_Word);
1685 if (grpref[inner] < shnum)
1686 grpref[inner] = shdr_info[grpref[inner]].idx;
1691 /* Handle the SHT_REL, SHT_RELA, and SHF_INFO_LINK flag. */
1692 if (SH_INFO_LINK_P (&shdr_info[cnt].shdr))
1693 shdr_info[cnt].shdr.sh_info =
1694 shdr_info[shdr_info[cnt].shdr.sh_info].idx;
1696 /* Get the data from the old file if necessary. We already
1697 created the data for the section header string table. */
1700 if (shdr_info[cnt].data == NULL)
1702 shdr_info[cnt].data = elf_getdata (shdr_info[cnt].scn, NULL);
1703 if (shdr_info[cnt].data == NULL)
1704 INTERNAL_ERROR (fname);
1707 /* Set the data. This is done by copying from the old file. */
1708 newdata = elf_newdata (scn);
1709 if (newdata == NULL)
1710 INTERNAL_ERROR (fname);
1712 /* Copy the structure. */
1713 *newdata = *shdr_info[cnt].data;
1715 /* We know the size. */
1716 shdr_info[cnt].shdr.sh_size = shdr_info[cnt].data->d_size;
1718 /* We have to adjust symbol tables. The st_shndx member might
1719 have to be updated. */
1720 if (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM
1721 || shdr_info[cnt].shdr.sh_type == SHT_SYMTAB)
1723 Elf_Data *versiondata = NULL;
1724 Elf_Data *shndxdata = NULL;
1726 size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
1728 if (shdr_info[cnt].symtab_idx != 0)
1730 elf_assert (shdr_info[cnt].shdr.sh_type == SHT_SYMTAB_SHNDX);
1731 /* This section has extended section information.
1732 We have to modify that information, too. */
1733 shndxdata = elf_getdata (shdr_info[shdr_info[cnt].symtab_idx].scn,
1736 elf_assert (shndxdata != NULL
1737 && shndxdata->d_buf != NULL
1738 && ((shndxdata->d_size / sizeof (Elf32_Word))
1739 >= shdr_info[cnt].data->d_size / elsize));
1742 if (shdr_info[cnt].version_idx != 0)
1744 elf_assert (shdr_info[cnt].shdr.sh_type == SHT_DYNSYM);
1745 /* This section has associated version
1746 information. We have to modify that
1747 information, too. */
1748 versiondata = elf_getdata (shdr_info[shdr_info[cnt].version_idx].scn,
1751 elf_assert (versiondata != NULL
1752 && versiondata->d_buf != NULL
1753 && ((versiondata->d_size / sizeof (GElf_Versym))
1754 >= shdr_info[cnt].data->d_size / elsize));
1757 shdr_info[cnt].newsymidx
1758 = (Elf32_Word *) xcalloc (shdr_info[cnt].data->d_size
1759 / elsize, sizeof (Elf32_Word));
1761 bool last_was_local = true;
1764 for (destidx = inner = 1;
1765 inner < shdr_info[cnt].data->d_size / elsize;
1771 GElf_Sym *sym = gelf_getsymshndx (shdr_info[cnt].data,
1775 INTERNAL_ERROR (fname);
1777 if (sym->st_shndx == SHN_UNDEF
1778 || (sym->st_shndx >= shnum
1779 && sym->st_shndx != SHN_XINDEX))
1781 /* This is no section index, leave it alone
1782 unless it is moved. */
1783 if (destidx != inner
1784 && gelf_update_symshndx (shdr_info[cnt].data,
1788 INTERNAL_ERROR (fname);
1790 shdr_info[cnt].newsymidx[inner] = destidx++;
1793 && GELF_ST_BIND (sym->st_info) != STB_LOCAL)
1795 last_was_local = false;
1796 shdr_info[cnt].shdr.sh_info = destidx - 1;
1802 /* Get the full section index, if necessary from the
1804 if (sym->st_shndx == SHN_XINDEX)
1805 elf_assert (shndxdata != NULL
1806 && shndxdata->d_buf != NULL);
1807 size_t sidx = (sym->st_shndx != SHN_XINDEX
1808 ? sym->st_shndx : xshndx);
1809 sec = shdr_info[sidx].idx;
1813 GElf_Section nshndx;
1816 if (sec < SHN_LORESERVE)
1823 nshndx = SHN_XINDEX;
1827 elf_assert (sec < SHN_LORESERVE || shndxdata != NULL);
1829 if ((inner != destidx || nshndx != sym->st_shndx
1830 || (shndxdata != NULL && nxshndx != xshndx))
1831 && (sym->st_shndx = nshndx,
1832 gelf_update_symshndx (shdr_info[cnt].data,
1836 INTERNAL_ERROR (fname);
1838 shdr_info[cnt].newsymidx[inner] = destidx++;
1841 && GELF_ST_BIND (sym->st_info) != STB_LOCAL)
1843 last_was_local = false;
1844 shdr_info[cnt].shdr.sh_info = destidx - 1;
1847 else if ((shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) != 0
1848 && GELF_ST_TYPE (sym->st_info) != STT_SECTION
1849 && shdr_info[sidx].shdr.sh_type != SHT_GROUP)
1851 /* Removing a real symbol from an allocated
1852 symbol table is hard and probably a
1853 mistake. Really removing it means
1854 rewriting the dynamic segment and hash
1855 sections. Just warn and set the symbol
1856 section to UNDEF. */
1858 gettext ("Cannot remove symbol [%zd] from allocated symbol table [%zd]"), inner, cnt);
1859 sym->st_shndx = SHN_UNDEF;
1860 if (gelf_update_sym (shdr_info[cnt].data, destidx,
1862 INTERNAL_ERROR (fname);
1863 shdr_info[cnt].newsymidx[inner] = destidx++;
1865 else if (debug_fname != NULL
1866 && shdr_info[cnt].debug_data == NULL)
1867 /* The symbol points to a section that is discarded
1868 but isn't preserved in the debug file. Check that
1869 this is a section or group signature symbol
1870 for a section which has been removed. Or a special
1871 data marker symbol to a debug section. */
1873 elf_assert (GELF_ST_TYPE (sym->st_info) == STT_SECTION
1874 || ((shdr_info[sidx].shdr.sh_type
1876 && (shdr_info[sidx].shdr.sh_info
1878 || ebl_data_marker_symbol (ebl, sym,
1879 elf_strptr (elf, sh_link,
1884 if (destidx != inner)
1886 /* The size of the symbol table changed. */
1887 shdr_info[cnt].shdr.sh_size = newdata->d_size
1889 any_symtab_changes = true;
1893 /* The symbol table didn't really change. */
1894 free (shdr_info[cnt].newsymidx);
1895 shdr_info[cnt].newsymidx = NULL;
1900 /* If we have to, compute the offset of the section.
1901 If allocate and unallocated sections are mixed, we only update
1902 the allocated ones now. The unallocated ones come second. */
1903 if (! mixed_allocated_unallocated
1904 || (shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) != 0)
1906 if (shdr_info[cnt].shdr.sh_offset == 0)
1907 shdr_info[cnt].shdr.sh_offset
1908 = ((lastoffset + shdr_info[cnt].shdr.sh_addralign - 1)
1909 & ~((GElf_Off) (shdr_info[cnt].shdr.sh_addralign - 1)));
1911 /* Set the section header in the new file. */
1912 if (unlikely (gelf_update_shdr (scn, &shdr_info[cnt].shdr) == 0))
1913 /* There cannot be any overflows. */
1914 INTERNAL_ERROR (fname);
1916 /* Remember the last section written so far. */
1917 GElf_Off filesz = (shdr_info[cnt].shdr.sh_type != SHT_NOBITS
1918 ? shdr_info[cnt].shdr.sh_size : 0);
1919 if (lastoffset < shdr_info[cnt].shdr.sh_offset + filesz)
1920 lastoffset = shdr_info[cnt].shdr.sh_offset + filesz;
1924 /* We might have to update the unallocated sections after we done the
1925 allocated ones. lastoffset is set to right after the last allocated
1927 if (mixed_allocated_unallocated)
1928 for (cnt = 1; cnt <= shdridx; ++cnt)
1929 if (shdr_info[cnt].idx > 0)
1931 scn = elf_getscn (newelf, shdr_info[cnt].idx);
1932 if ((shdr_info[cnt].shdr.sh_flags & SHF_ALLOC) == 0)
1934 if (shdr_info[cnt].shdr.sh_offset == 0)
1935 shdr_info[cnt].shdr.sh_offset
1936 = ((lastoffset + shdr_info[cnt].shdr.sh_addralign - 1)
1937 & ~((GElf_Off) (shdr_info[cnt].shdr.sh_addralign - 1)));
1939 /* Set the section header in the new file. */
1940 if (unlikely (gelf_update_shdr (scn, &shdr_info[cnt].shdr) == 0))
1941 /* There cannot be any overflows. */
1942 INTERNAL_ERROR (fname);
1944 /* Remember the last section written so far. */
1945 GElf_Off filesz = (shdr_info[cnt].shdr.sh_type != SHT_NOBITS
1946 ? shdr_info[cnt].shdr.sh_size : 0);
1947 if (lastoffset < shdr_info[cnt].shdr.sh_offset + filesz)
1948 lastoffset = shdr_info[cnt].shdr.sh_offset + filesz;
1952 /* Adjust symbol references if symbol tables changed. */
1953 if (any_symtab_changes)
1954 /* Find all relocation sections which use this symbol table. */
1955 for (cnt = 1; cnt <= shdridx; ++cnt)
1957 /* Update section headers when the data size has changed.
1958 We also update the SHT_NOBITS section in the debug
1959 file so that the section headers match in sh_size. */
1960 inline void update_section_size (const Elf_Data *newdata)
1963 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
1964 shdr->sh_size = newdata->d_size;
1965 (void) gelf_update_shdr (scn, shdr);
1966 if (debugelf != NULL)
1968 /* libelf will use d_size to set sh_size. */
1969 Elf_Data *debugdata = elf_getdata (elf_getscn (debugelf,
1971 if (debugdata == NULL)
1972 INTERNAL_ERROR (fname);
1973 debugdata->d_size = newdata->d_size;
1977 if (shdr_info[cnt].idx == 0 && debug_fname == NULL)
1978 /* Ignore sections which are discarded. When we are saving a
1979 relocation section in a separate debug file, we must fix up
1980 the symbol table references. */
1983 const Elf32_Word symtabidx = shdr_info[cnt].old_sh_link;
1984 elf_assert (symtabidx < shnum + 2);
1985 const Elf32_Word *const newsymidx = shdr_info[symtabidx].newsymidx;
1986 switch (shdr_info[cnt].shdr.sh_type)
1988 inline bool no_symtab_updates (void)
1990 /* If the symbol table hasn't changed, do not do anything. */
1991 if (shdr_info[symtabidx].newsymidx == NULL)
1994 /* If the symbol table is not discarded, but additionally
1995 duplicated in the separate debug file and this section
1996 is discarded, don't adjust anything. */
1997 return (shdr_info[cnt].idx == 0
1998 && shdr_info[symtabidx].debug_data != NULL);
2003 if (no_symtab_updates ())
2006 Elf_Data *d = elf_getdata (shdr_info[cnt].idx == 0
2007 ? elf_getscn (debugelf, cnt)
2008 : elf_getscn (newelf,
2009 shdr_info[cnt].idx),
2011 elf_assert (d != NULL && d->d_buf != NULL
2012 && shdr_info[cnt].shdr.sh_entsize != 0);
2013 size_t nrels = (shdr_info[cnt].shdr.sh_size
2014 / shdr_info[cnt].shdr.sh_entsize);
2016 size_t symsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2017 const Elf32_Word symidxn = (shdr_info[symtabidx].data->d_size
2019 if (shdr_info[cnt].shdr.sh_type == SHT_REL)
2020 for (size_t relidx = 0; relidx < nrels; ++relidx)
2023 if (gelf_getrel (d, relidx, &rel_mem) == NULL)
2024 INTERNAL_ERROR (fname);
2026 size_t symidx = GELF_R_SYM (rel_mem.r_info);
2027 elf_assert (symidx < symidxn);
2028 if (newsymidx[symidx] != symidx)
2031 = GELF_R_INFO (newsymidx[symidx],
2032 GELF_R_TYPE (rel_mem.r_info));
2034 if (gelf_update_rel (d, relidx, &rel_mem) == 0)
2035 INTERNAL_ERROR (fname);
2039 for (size_t relidx = 0; relidx < nrels; ++relidx)
2042 if (gelf_getrela (d, relidx, &rel_mem) == NULL)
2043 INTERNAL_ERROR (fname);
2045 size_t symidx = GELF_R_SYM (rel_mem.r_info);
2046 elf_assert (symidx < symidxn);
2047 if (newsymidx[symidx] != symidx)
2050 = GELF_R_INFO (newsymidx[symidx],
2051 GELF_R_TYPE (rel_mem.r_info));
2053 if (gelf_update_rela (d, relidx, &rel_mem) == 0)
2054 INTERNAL_ERROR (fname);
2060 if (no_symtab_updates ())
2063 /* We have to recompute the hash table. */
2065 elf_assert (shdr_info[cnt].idx > 0);
2067 /* The hash section in the new file. */
2068 scn = elf_getscn (newelf, shdr_info[cnt].idx);
2070 /* The symbol table data. */
2071 Elf_Data *symd = elf_getdata (elf_getscn (newelf,
2072 shdr_info[symtabidx].idx),
2074 elf_assert (symd != NULL && symd->d_buf != NULL);
2076 /* The hash table data. */
2077 Elf_Data *hashd = elf_getdata (scn, NULL);
2078 elf_assert (hashd != NULL && hashd->d_buf != NULL);
2080 if (shdr_info[cnt].shdr.sh_entsize == sizeof (Elf32_Word))
2082 /* Sane arches first. */
2083 elf_assert (hashd->d_size >= 2 * sizeof (Elf32_Word));
2084 Elf32_Word *bucket = (Elf32_Word *) hashd->d_buf;
2086 size_t strshndx = shdr_info[symtabidx].old_sh_link;
2087 size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2089 Elf32_Word nchain = bucket[1];
2090 Elf32_Word nbucket = bucket[0];
2091 uint64_t used_buf = ((2ULL + nchain + nbucket)
2092 * sizeof (Elf32_Word));
2093 elf_assert (used_buf <= hashd->d_size);
2095 /* Adjust the nchain value. The symbol table size
2096 changed. We keep the same size for the bucket array. */
2097 bucket[1] = symd->d_size / elsize;
2099 Elf32_Word *chain = bucket + nbucket;
2101 /* New size of the section. */
2102 size_t n_size = ((2 + symd->d_size / elsize + nbucket)
2103 * sizeof (Elf32_Word));
2104 elf_assert (n_size <= hashd->d_size);
2105 hashd->d_size = n_size;
2106 update_section_size (hashd);
2108 /* Clear the arrays. */
2109 memset (bucket, '\0',
2110 (symd->d_size / elsize + nbucket)
2111 * sizeof (Elf32_Word));
2113 for (size_t inner = shdr_info[symtabidx].shdr.sh_info;
2114 inner < symd->d_size / elsize; ++inner)
2117 GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
2118 elf_assert (sym != NULL);
2120 const char *name = elf_strptr (elf, strshndx,
2122 elf_assert (name != NULL && nbucket != 0);
2123 size_t hidx = elf_hash (name) % nbucket;
2125 if (bucket[hidx] == 0)
2126 bucket[hidx] = inner;
2129 hidx = bucket[hidx];
2131 while (chain[hidx] != 0 && chain[hidx] < nchain)
2134 chain[hidx] = inner;
2140 /* Alpha and S390 64-bit use 64-bit SHT_HASH entries. */
2141 elf_assert (shdr_info[cnt].shdr.sh_entsize
2142 == sizeof (Elf64_Xword));
2144 Elf64_Xword *bucket = (Elf64_Xword *) hashd->d_buf;
2146 size_t strshndx = shdr_info[symtabidx].old_sh_link;
2147 size_t elsize = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2149 elf_assert (symd->d_size >= 2 * sizeof (Elf64_Xword));
2150 Elf64_Xword nbucket = bucket[0];
2151 Elf64_Xword nchain = bucket[1];
2152 uint64_t maxwords = hashd->d_size / sizeof (Elf64_Xword);
2153 elf_assert (maxwords >= 2
2154 && maxwords - 2 >= nbucket
2155 && maxwords - 2 - nbucket >= nchain);
2157 /* Adjust the nchain value. The symbol table size
2158 changed. We keep the same size for the bucket array. */
2159 bucket[1] = symd->d_size / elsize;
2161 Elf64_Xword *chain = bucket + nbucket;
2163 /* New size of the section. */
2164 size_t n_size = ((2 + symd->d_size / elsize + nbucket)
2165 * sizeof (Elf64_Xword));
2166 elf_assert (n_size <= hashd->d_size);
2167 hashd->d_size = n_size;
2168 update_section_size (hashd);
2170 /* Clear the arrays. */
2171 memset (bucket, '\0',
2172 (symd->d_size / elsize + nbucket)
2173 * sizeof (Elf64_Xword));
2175 for (size_t inner = shdr_info[symtabidx].shdr.sh_info;
2176 inner < symd->d_size / elsize; ++inner)
2179 GElf_Sym *sym = gelf_getsym (symd, inner, &sym_mem);
2180 elf_assert (sym != NULL);
2182 const char *name = elf_strptr (elf, strshndx,
2184 elf_assert (name != NULL && nbucket != 0);
2185 size_t hidx = elf_hash (name) % nbucket;
2187 if (bucket[hidx] == 0)
2188 bucket[hidx] = inner;
2191 hidx = bucket[hidx];
2193 while (chain[hidx] != 0 && chain[hidx] < nchain)
2196 chain[hidx] = inner;
2202 case SHT_GNU_versym:
2203 /* If the symbol table changed we have to adjust the entries. */
2204 if (no_symtab_updates ())
2207 elf_assert (shdr_info[cnt].idx > 0);
2209 /* The symbol version section in the new file. */
2210 scn = elf_getscn (newelf, shdr_info[cnt].idx);
2212 /* The symbol table data. */
2213 symd = elf_getdata (elf_getscn (newelf, shdr_info[symtabidx].idx),
2215 elf_assert (symd != NULL && symd->d_buf != NULL);
2216 size_t symz = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2217 const Elf32_Word syms = (shdr_info[symtabidx].data->d_size / symz);
2219 /* The version symbol data. */
2220 Elf_Data *verd = elf_getdata (scn, NULL);
2221 elf_assert (verd != NULL && verd->d_buf != NULL);
2223 /* The symbol version array. */
2224 GElf_Half *verstab = (GElf_Half *) verd->d_buf;
2226 /* Walk through the list and */
2227 size_t elsize = gelf_fsize (elf, verd->d_type, 1, EV_CURRENT);
2228 Elf32_Word vers = verd->d_size / elsize;
2229 for (size_t inner = 1; inner < vers && inner < syms; ++inner)
2230 if (newsymidx[inner] != 0 && newsymidx[inner] < vers)
2231 /* Overwriting the same array works since the
2232 reordering can only move entries to lower indices
2234 verstab[newsymidx[inner]] = verstab[inner];
2236 /* New size of the section. */
2237 verd->d_size = gelf_fsize (newelf, verd->d_type,
2239 / gelf_fsize (elf, symd->d_type, 1,
2242 update_section_size (verd);
2246 if (no_symtab_updates ())
2249 /* Yes, the symbol table changed.
2250 Update the section header of the section group. */
2251 scn = elf_getscn (newelf, shdr_info[cnt].idx);
2253 GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
2254 elf_assert (shdr != NULL);
2256 size_t symsz = gelf_fsize (elf, ELF_T_SYM, 1, EV_CURRENT);
2257 const Elf32_Word symn = (shdr_info[symtabidx].data->d_size
2259 elf_assert (shdr->sh_info < symn);
2260 shdr->sh_info = newsymidx[shdr->sh_info];
2262 (void) gelf_update_shdr (scn, shdr);
2267 /* Remove any relocations between debug sections in ET_REL
2268 for the debug file when requested. These relocations are always
2269 zero based between the unallocated sections. */
2270 if (debug_fname != NULL && removing_sections
2271 && reloc_debug && ehdr->e_type == ET_REL)
2272 remove_debug_relocations (ebl, debugelf, ehdr, fname, shstrndx);
2274 /* Now that we have done all adjustments to the data,
2275 we can actually write out the debug file. */
2276 if (debug_fname != NULL && removing_sections)
2278 /* Finally write the file. */
2279 if (unlikely (elf_update (debugelf, ELF_C_WRITE) == -1))
2281 error (0, 0, gettext ("while writing '%s': %s"),
2282 tmp_debug_fname, elf_errmsg (-1));
2287 /* Create the real output file. First rename, then change the
2289 if (rename (tmp_debug_fname, debug_fname) != 0
2290 || fchmod (debug_fd, mode) != 0)
2292 error (0, errno, gettext ("while creating '%s'"), debug_fname);
2297 /* The temporary file does not exist anymore. */
2298 free (tmp_debug_fname);
2299 tmp_debug_fname = NULL;
2304 Elf_Data debug_crc_data =
2306 .d_type = ELF_T_WORD,
2307 .d_buf = &debug_crc,
2308 .d_size = sizeof (debug_crc),
2309 .d_version = EV_CURRENT
2312 /* Compute the checksum which we will add to the executable. */
2313 if (crc32_file (debug_fd, &debug_crc) != 0)
2315 error (0, errno, gettext ("\
2316 while computing checksum for debug information"));
2317 unlink (debug_fname);
2322 /* Store it in the debuglink section data. */
2323 if (unlikely (gelf_xlatetof (newelf, &debuglink_crc_data,
2324 &debug_crc_data, ehdr->e_ident[EI_DATA])
2325 != &debuglink_crc_data))
2326 INTERNAL_ERROR (fname);
2330 /* Finally finish the ELF header. Fill in the fields not handled by
2331 libelf from the old file. */
2332 newehdr = gelf_getehdr (newelf, &newehdr_mem);
2333 if (newehdr == NULL)
2334 INTERNAL_ERROR (fname);
2336 memcpy (newehdr->e_ident, ehdr->e_ident, EI_NIDENT);
2337 newehdr->e_type = ehdr->e_type;
2338 newehdr->e_machine = ehdr->e_machine;
2339 newehdr->e_version = ehdr->e_version;
2340 newehdr->e_entry = ehdr->e_entry;
2341 newehdr->e_flags = ehdr->e_flags;
2342 newehdr->e_phoff = ehdr->e_phoff;
2344 /* We need to position the section header table. */
2345 const size_t offsize = gelf_fsize (elf, ELF_T_OFF, 1, EV_CURRENT);
2346 newehdr->e_shoff = ((shdr_info[shdridx].shdr.sh_offset
2347 + shdr_info[shdridx].shdr.sh_size + offsize - 1)
2348 & ~((GElf_Off) (offsize - 1)));
2349 newehdr->e_shentsize = gelf_fsize (elf, ELF_T_SHDR, 1, EV_CURRENT);
2351 /* The new section header string table index. */
2352 if (likely (idx < SHN_HIRESERVE) && likely (idx != SHN_XINDEX))
2353 newehdr->e_shstrndx = idx;
2356 /* The index does not fit in the ELF header field. */
2357 shdr_info[0].scn = elf_getscn (elf, 0);
2359 if (gelf_getshdr (shdr_info[0].scn, &shdr_info[0].shdr) == NULL)
2360 INTERNAL_ERROR (fname);
2362 shdr_info[0].shdr.sh_link = idx;
2363 (void) gelf_update_shdr (shdr_info[0].scn, &shdr_info[0].shdr);
2365 newehdr->e_shstrndx = SHN_XINDEX;
2368 if (gelf_update_ehdr (newelf, newehdr) == 0)
2370 error (0, 0, gettext ("%s: error while creating ELF header: %s"),
2371 output_fname ?: fname, elf_errmsg (-1));
2376 /* We have everything from the old file. */
2377 if (elf_cntl (elf, ELF_C_FDDONE) != 0)
2379 error (0, 0, gettext ("%s: error while reading the file: %s"),
2380 fname, elf_errmsg (-1));
2385 /* The ELF library better follows our layout when this is not a
2386 relocatable object file. */
2387 elf_flagelf (newelf, ELF_C_SET,
2388 (phnum > 0 ? ELF_F_LAYOUT : 0)
2389 | (permissive ? ELF_F_PERMISSIVE : 0));
2391 /* Finally write the file. */
2392 if (elf_update (newelf, ELF_C_WRITE) == -1)
2394 error (0, 0, gettext ("while writing '%s': %s"),
2395 output_fname ?: fname, elf_errmsg (-1));
2401 /* libelf can't cope without the section headers being properly intact.
2402 So we just let it write them normally, and then we nuke them later. */
2404 if (newehdr->e_ident[EI_CLASS] == ELFCLASS32)
2406 assert (offsetof (Elf32_Ehdr, e_shentsize) + sizeof (Elf32_Half)
2407 == offsetof (Elf32_Ehdr, e_shnum));
2408 assert (offsetof (Elf32_Ehdr, e_shnum) + sizeof (Elf32_Half)
2409 == offsetof (Elf32_Ehdr, e_shstrndx));
2410 const Elf32_Off zero_off = 0;
2411 const Elf32_Half zero[3] = { 0, 0, SHN_UNDEF };
2412 if (pwrite_retry (fd, &zero_off, sizeof zero_off,
2413 offsetof (Elf32_Ehdr, e_shoff)) != sizeof zero_off
2414 || (pwrite_retry (fd, zero, sizeof zero,
2415 offsetof (Elf32_Ehdr, e_shentsize))
2417 || ftruncate (fd, shdr_info[shdridx].shdr.sh_offset) < 0)
2419 error (0, errno, gettext ("while writing '%s'"),
2420 output_fname ?: fname);
2426 assert (offsetof (Elf64_Ehdr, e_shentsize) + sizeof (Elf64_Half)
2427 == offsetof (Elf64_Ehdr, e_shnum));
2428 assert (offsetof (Elf64_Ehdr, e_shnum) + sizeof (Elf64_Half)
2429 == offsetof (Elf64_Ehdr, e_shstrndx));
2430 const Elf64_Off zero_off = 0;
2431 const Elf64_Half zero[3] = { 0, 0, SHN_UNDEF };
2432 if (pwrite_retry (fd, &zero_off, sizeof zero_off,
2433 offsetof (Elf64_Ehdr, e_shoff)) != sizeof zero_off
2434 || (pwrite_retry (fd, zero, sizeof zero,
2435 offsetof (Elf64_Ehdr, e_shentsize))
2437 || ftruncate (fd, shdr_info[shdridx].shdr.sh_offset) < 0)
2439 error (0, errno, gettext ("while writing '%s'"),
2440 output_fname ?: fname);
2447 if (shdr_info != NULL)
2449 /* For some sections we might have created an table to map symbol
2450 table indices. Or we might kept (original) data around to put
2451 into the .debug file. */
2452 for (cnt = 1; cnt <= shdridx; ++cnt)
2454 free (shdr_info[cnt].newsymidx);
2455 if (shdr_info[cnt].debug_data != NULL)
2456 free (shdr_info[cnt].debug_data->d_buf);
2459 /* Free data we allocated for the .gnu_debuglink section. */
2460 free (debuglink_buf);
2462 /* Free the memory. */
2463 if ((shnum + 2) * sizeof (struct shdr_info) > MAX_STACK_ALLOC)
2467 /* Free other resources. */
2468 if (shstrtab_data != NULL)
2469 free (shstrtab_data->d_buf);
2471 dwelf_strtab_free (shst);
2473 /* That was it. Close the descriptors. */
2474 if (elf_end (newelf) != 0)
2476 error (0, 0, gettext ("error while finishing '%s': %s"),
2477 output_fname ?: fname, elf_errmsg (-1));
2481 if (debugelf != NULL && elf_end (debugelf) != 0)
2483 error (0, 0, gettext ("error while finishing '%s': %s"), debug_fname,
2489 /* Close the EBL backend. */
2491 ebl_closebackend (ebl);
2495 /* If requested, preserve the timestamp. */
2498 if (futimens (fd, tvp) != 0)
2500 error (0, errno, gettext ("\
2501 cannot set access and modification date of '%s'"),
2502 output_fname ?: fname);
2507 /* Close the file descriptor if we created a new file. */
2508 if (output_fname != NULL)
2512 unlink (output_fname);
2519 cleanup_debug (void)
2523 if (tmp_debug_fname != NULL)
2525 unlink (tmp_debug_fname);
2526 free (tmp_debug_fname);
2527 tmp_debug_fname = NULL;
2535 handle_ar (int fd, Elf *elf, const char *prefix, const char *fname,
2536 struct timespec tvp[2])
2538 size_t prefix_len = prefix == NULL ? 0 : strlen (prefix);
2539 size_t fname_len = strlen (fname) + 1;
2540 char new_prefix[prefix_len + 1 + fname_len];
2541 char *cp = new_prefix;
2543 /* Create the full name of the file. */
2546 cp = mempcpy (cp, prefix, prefix_len);
2549 memcpy (cp, fname, fname_len);
2552 /* Process all the files contained in the archive. */
2554 Elf_Cmd cmd = ELF_C_RDWR;
2556 while ((subelf = elf_begin (fd, cmd, elf)) != NULL)
2558 /* The the header for this element. */
2559 Elf_Arhdr *arhdr = elf_getarhdr (subelf);
2561 if (elf_kind (subelf) == ELF_K_ELF)
2562 result |= handle_elf (fd, subelf, new_prefix, arhdr->ar_name, 0, NULL);
2563 else if (elf_kind (subelf) == ELF_K_AR)
2564 result |= handle_ar (fd, subelf, new_prefix, arhdr->ar_name, NULL);
2566 /* Get next archive element. */
2567 cmd = elf_next (subelf);
2568 if (unlikely (elf_end (subelf) != 0))
2569 INTERNAL_ERROR (fname);
2574 if (unlikely (futimens (fd, tvp) != 0))
2576 error (0, errno, gettext ("\
2577 cannot set access and modification date of '%s'"), fname);
2582 if (unlikely (close (fd) != 0))
2583 error (EXIT_FAILURE, errno, gettext ("while closing '%s'"), fname);
2589 #include "debugpred.h"