1 /* Discard section not used at runtime from object files.
2 Copyright (C) 2000-2012, 2014, 2015, 2016, 2017, 2018 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
64 #define OPT_RELOC_DEBUG_ONLY 0x105
67 /* Definitions of arguments for argp functions. */
68 static const struct argp_option options
[] =
70 { NULL
, 0, NULL
, 0, N_("Output selection:"), 0 },
71 { "output", 'o', "FILE", 0, N_("Place stripped output into FILE"), 0 },
72 { NULL
, 'f', "FILE", 0, N_("Extract the removed sections into FILE"), 0 },
73 { NULL
, 'F', "FILE", 0, N_("Embed name FILE instead of -f argument"), 0 },
75 { NULL
, 0, NULL
, 0, N_("Output options:"), 0 },
76 { "strip-all", 's', NULL
, OPTION_HIDDEN
, NULL
, 0 },
77 { "strip-debug", 'g', NULL
, 0, N_("Remove all debugging symbols"), 0 },
78 { NULL
, 'd', NULL
, OPTION_ALIAS
, NULL
, 0 },
79 { NULL
, 'S', NULL
, OPTION_ALIAS
, NULL
, 0 },
80 { "strip-sections", OPT_STRIP_SECTIONS
, NULL
, 0,
81 N_("Remove section headers (not recommended)"), 0 },
82 { "preserve-dates", 'p', NULL
, 0,
83 N_("Copy modified/access timestamps to the output"), 0 },
84 { "reloc-debug-sections", OPT_RELOC_DEBUG
, NULL
, 0,
85 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 reversible, needs -f)"), 0 },
86 { "reloc-debug-sections-only", OPT_RELOC_DEBUG_ONLY
, NULL
, 0,
87 N_("Similar to --reloc-debug-sections, but resolve all trivial relocations between debug sections in place. No other stripping is performed (operation is not reversible, incompatible with -f, -g, --remove-comment and --remove-section)"), 0 },
88 { "remove-comment", OPT_REMOVE_COMMENT
, NULL
, 0,
89 N_("Remove .comment section"), 0 },
90 { "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 },
91 { "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 },
92 { "permissive", OPT_PERMISSIVE
, NULL
, 0,
93 N_("Relax a few rules to handle slightly broken ELF files"), 0 },
94 { NULL
, 0, NULL
, 0, NULL
, 0 }
97 /* Short description of program. */
98 static const char doc
[] = N_("Discard symbols from object files.");
100 /* Strings for arguments in help texts. */
101 static const char args_doc
[] = N_("[FILE...]");
103 /* Prototype for option handler. */
104 static error_t
parse_opt (int key
, char *arg
, struct argp_state
*state
);
106 /* Data structure to communicate with argp functions. */
107 static struct argp argp
=
109 options
, parse_opt
, args_doc
, doc
, NULL
, NULL
, NULL
113 /* Print symbols in file named FNAME. */
114 static int process_file (const char *fname
);
116 /* Handle one ELF file. */
117 static int handle_elf (int fd
, Elf
*elf
, const char *prefix
,
118 const char *fname
, mode_t mode
, struct timespec tvp
[2]);
120 /* Handle all files contained in the archive. */
121 static int handle_ar (int fd
, Elf
*elf
, const char *prefix
, const char *fname
,
122 struct timespec tvp
[2]) __attribute__ ((unused
));
124 static int debug_fd
= -1;
125 static char *tmp_debug_fname
= NULL
;
127 /* Close debug file descriptor, if opened. And remove temporary debug file. */
128 static void cleanup_debug (void);
130 #define INTERNAL_ERROR(fname) \
133 error (EXIT_FAILURE, 0, _("%s: INTERNAL ERROR %d (%s): %s"), \
134 fname, __LINE__, PACKAGE_VERSION, elf_errmsg (-1)); \
138 /* Name of the output file. */
139 static const char *output_fname
;
141 /* Name of the debug output file. */
142 static const char *debug_fname
;
144 /* Name to pretend the debug output file has. */
145 static const char *debug_fname_embed
;
147 /* If true output files shall have same date as the input file. */
148 static bool preserve_dates
;
150 /* If true .comment sections will be removed. */
151 static bool remove_comment
;
153 /* If true remove all debug sections. */
154 static bool remove_debug
;
156 /* If true remove all section headers. */
157 static bool remove_shdrs
;
159 /* If true relax some ELF rules for input files. */
160 static bool permissive
;
162 /* If true perform relocations between debug sections. */
163 static bool reloc_debug
;
165 /* If true perform relocations between debug sections only. */
166 static bool reloc_debug_only
;
168 /* Sections the user explicitly wants to keep or remove. */
169 struct section_pattern
172 struct section_pattern
*next
;
175 static struct section_pattern
*keep_secs
= NULL
;
176 static struct section_pattern
*remove_secs
= NULL
;
179 add_pattern (struct section_pattern
**patterns
, const char *pattern
)
181 struct section_pattern
*p
= xmalloc (sizeof *p
);
182 p
->pattern
= xstrdup (pattern
);
188 free_sec_patterns (struct section_pattern
*patterns
)
190 struct section_pattern
*pattern
= patterns
;
191 while (pattern
!= NULL
)
193 struct section_pattern
*p
= pattern
;
203 free_sec_patterns (keep_secs
);
204 free_sec_patterns (remove_secs
);
208 section_name_matches (struct section_pattern
*patterns
, const char *name
)
210 struct section_pattern
*pattern
= patterns
;
211 while (pattern
!= NULL
)
213 if (fnmatch (pattern
->pattern
, name
, FNM_EXTMATCH
) == 0)
215 pattern
= pattern
->next
;
222 main (int argc
, char *argv
[])
227 /* We use no threads here which can interfere with handling a stream. */
228 __fsetlocking (stdin
, FSETLOCKING_BYCALLER
);
229 __fsetlocking (stdout
, FSETLOCKING_BYCALLER
);
230 __fsetlocking (stderr
, FSETLOCKING_BYCALLER
);
233 setlocale (LC_ALL
, "");
235 /* Make sure the message catalog can be found. */
236 bindtextdomain (PACKAGE_TARNAME
, LOCALEDIR
);
238 /* Initialize the message catalog. */
239 textdomain (PACKAGE_TARNAME
);
241 /* Parse and process arguments. */
242 if (argp_parse (&argp
, argc
, argv
, 0, &remaining
, NULL
) != 0)
245 if (reloc_debug
&& debug_fname
== NULL
)
246 error (EXIT_FAILURE
, 0,
247 _("--reloc-debug-sections used without -f"));
249 if (reloc_debug_only
&&
250 (debug_fname
!= NULL
|| remove_secs
!= NULL
251 || remove_comment
== true || remove_debug
== true))
252 error (EXIT_FAILURE
, 0,
253 _("--reloc-debug-sections-only incompatible with -f, -g, --remove-comment and --remove-section"));
255 /* Tell the library which version we are expecting. */
256 elf_version (EV_CURRENT
);
258 if (remaining
== argc
)
259 /* The user didn't specify a name so we use a.out. */
260 result
= process_file ("a.out");
263 /* If we have seen the '-o' or '-f' option there must be exactly one
265 if ((output_fname
!= NULL
|| debug_fname
!= NULL
)
266 && remaining
+ 1 < argc
)
267 error (EXIT_FAILURE
, 0, _("\
268 Only one input file allowed together with '-o' and '-f'"));
270 /* Process all the remaining files. */
272 result
|= process_file (argv
[remaining
]);
273 while (++remaining
< argc
);
281 /* Handle program arguments. */
283 parse_opt (int key
, char *arg
, struct argp_state
*state
)
288 if (debug_fname
!= NULL
)
290 error (0, 0, _("-f option specified twice"));
297 if (debug_fname_embed
!= NULL
)
299 error (0, 0, _("-F option specified twice"));
302 debug_fname_embed
= arg
;
306 if (output_fname
!= NULL
)
308 error (0, 0, _("-o option specified twice"));
315 preserve_dates
= true;
318 case OPT_RELOC_DEBUG
:
322 case OPT_RELOC_DEBUG_ONLY
:
323 reloc_debug_only
= true;
326 case OPT_REMOVE_COMMENT
:
327 remove_comment
= true;
331 if (fnmatch (arg
, ".comment", FNM_EXTMATCH
) == 0)
332 remove_comment
= true;
333 add_pattern (&remove_secs
, arg
);
336 case OPT_KEEP_SECTION
:
337 add_pattern (&keep_secs
, arg
);
346 case OPT_STRIP_SECTIONS
:
354 case 's': /* Ignored for compatibility. */
357 case ARGP_KEY_SUCCESS
:
358 if (remove_comment
== true
359 && section_name_matches (keep_secs
, ".comment"))
362 _("cannot both keep and remove .comment section"));
368 return ARGP_ERR_UNKNOWN
;
374 secndx_name (Elf
*elf
, size_t ndx
)
378 Elf_Scn
*sec
= elf_getscn (elf
, ndx
);
379 GElf_Shdr
*shdr
= gelf_getshdr (sec
, &mem
);
380 if (shdr
== NULL
|| elf_getshdrstrndx (elf
, &shstrndx
) < 0)
382 return elf_strptr (elf
, shstrndx
, shdr
->sh_name
) ?: "???";
385 /* Get the extended section index table data for a symbol table section. */
387 get_xndxdata (Elf
*elf
, Elf_Scn
*symscn
)
389 Elf_Data
*xndxdata
= NULL
;
391 GElf_Shdr
*shdr
= gelf_getshdr (symscn
, &shdr_mem
);
392 if (shdr
!= NULL
&& shdr
->sh_type
== SHT_SYMTAB
)
394 size_t scnndx
= elf_ndxscn (symscn
);
395 Elf_Scn
*xndxscn
= NULL
;
396 while ((xndxscn
= elf_nextscn (elf
, xndxscn
)) != NULL
)
398 GElf_Shdr xndxshdr_mem
;
399 GElf_Shdr
*xndxshdr
= gelf_getshdr (xndxscn
, &xndxshdr_mem
);
402 && xndxshdr
->sh_type
== SHT_SYMTAB_SHNDX
403 && xndxshdr
->sh_link
== scnndx
)
405 xndxdata
= elf_getdata (xndxscn
, NULL
);
414 /* Updates the shdrstrndx for the given Elf by updating the Ehdr and
415 possibly the section zero extension field. Returns zero on success. */
417 update_shdrstrndx (Elf
*elf
, size_t shdrstrndx
)
420 if (gelf_getehdr (elf
, &ehdr
) == 0)
423 if (shdrstrndx
< SHN_LORESERVE
)
424 ehdr
.e_shstrndx
= shdrstrndx
;
427 ehdr
.e_shstrndx
= SHN_XINDEX
;
428 Elf_Scn
*scn0
= elf_getscn (elf
, 0);
430 GElf_Shdr
*shdr0
= gelf_getshdr (scn0
, &shdr0_mem
);
434 shdr0
->sh_link
= shdrstrndx
;
435 if (gelf_update_shdr (scn0
, shdr0
) == 0)
439 if (unlikely (gelf_update_ehdr (elf
, &ehdr
) == 0))
446 /* Apply one relocation. Returns true when trivial
447 relocation actually done. */
449 relocate (Elf
*elf
, GElf_Addr offset
, const GElf_Sxword addend
,
450 Elf_Data
*tdata
, unsigned int ei_data
, const char *fname
,
451 bool is_rela
, GElf_Sym
*sym
, int addsub
, Elf_Type type
)
453 /* These are the types we can relocate. */
454 #define TYPES DO_TYPE (BYTE, Byte); DO_TYPE (HALF, Half); \
455 DO_TYPE (WORD, Word); DO_TYPE (SWORD, Sword); \
456 DO_TYPE (XWORD, Xword); DO_TYPE (SXWORD, Sxword)
460 #define DO_TYPE(NAME, Name) GElf_##Name Name;
461 union { TYPES
; } tmpbuf
;
466 #define DO_TYPE(NAME, Name) \
468 size = sizeof (GElf_##Name); \
477 if (offset
> tdata
->d_size
478 || tdata
->d_size
- offset
< size
)
481 error (EXIT_FAILURE
, 0, _("bad relocation"));
484 /* When the symbol value is zero then for SHT_REL
485 sections this is all that needs to be checked.
486 The addend is contained in the original data at
487 the offset already. So if the (section) symbol
488 address is zero and the given addend is zero
489 just remove the relocation, it isn't needed
491 if (addend
== 0 && sym
->st_value
== 0)
499 .d_version
= EV_CURRENT
,
504 .d_buf
= tdata
->d_buf
+ offset
,
506 .d_version
= EV_CURRENT
,
509 GElf_Addr value
= sym
->st_value
;
512 /* For SHT_RELA sections we just take the
513 given addend and add it to the value. */
515 /* For ADD/SUB relocations we need to fetch the
516 current section contents. */
519 Elf_Data
*d
= gelf_xlatetom (elf
, &tmpdata
,
523 INTERNAL_ERROR (fname
);
524 assert (d
== &tmpdata
);
529 /* For SHT_REL sections we have to peek at
530 what is already in the section at the given
531 offset to get the addend. */
532 Elf_Data
*d
= gelf_xlatetom (elf
, &tmpdata
,
536 INTERNAL_ERROR (fname
);
537 assert (d
== &tmpdata
);
542 #define DO_TYPE(NAME, Name) \
545 tmpbuf.Name -= (GElf_##Name) value; \
547 tmpbuf.Name += (GElf_##Name) value; \
555 /* Now finally put in the new value. */
556 Elf_Data
*s
= gelf_xlatetof (elf
, &rdata
,
560 INTERNAL_ERROR (fname
);
561 assert (s
== &rdata
);
566 /* Remove any relocations between debug sections in ET_REL
567 for the debug file when requested. These relocations are always
568 zero based between the unallocated sections. */
570 remove_debug_relocations (Ebl
*ebl
, Elf
*elf
, GElf_Ehdr
*ehdr
,
571 const char *fname
, size_t shstrndx
)
574 while ((scn
= elf_nextscn (elf
, scn
)) != NULL
)
576 /* We need the actual section and header from the elf
577 not just the cached original in shdr_info because we
578 might want to change the size. */
580 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &shdr_mem
);
581 if (shdr
->sh_type
== SHT_REL
|| shdr
->sh_type
== SHT_RELA
)
583 /* Make sure that this relocation section points to a
584 section to relocate with contents, that isn't
585 allocated and that is a debug section. */
586 Elf_Scn
*tscn
= elf_getscn (elf
, shdr
->sh_info
);
588 GElf_Shdr
*tshdr
= gelf_getshdr (tscn
, &tshdr_mem
);
589 if (tshdr
->sh_type
== SHT_NOBITS
590 || tshdr
->sh_size
== 0
591 || (tshdr
->sh_flags
& SHF_ALLOC
) != 0)
594 const char *tname
= elf_strptr (elf
, shstrndx
,
596 if (! tname
|| ! ebl_debugscn_p (ebl
, tname
))
599 /* OK, lets relocate all trivial cross debug section
601 Elf_Data
*reldata
= elf_getdata (scn
, NULL
);
602 if (reldata
== NULL
|| reldata
->d_buf
== NULL
)
603 INTERNAL_ERROR (fname
);
605 /* Make sure we adjust the uncompressed debug data
606 (and recompress if necessary at the end). */
608 int tcompress_type
= 0;
609 bool is_gnu_compressed
= false;
610 if (strncmp (tname
, ".zdebug", strlen ("zdebug")) == 0)
612 is_gnu_compressed
= true;
613 if (elf_compress_gnu (tscn
, 0, 0) != 1)
614 INTERNAL_ERROR (fname
);
618 if (gelf_getchdr (tscn
, &tchdr
) != NULL
)
620 tcompress_type
= tchdr
.ch_type
;
621 if (elf_compress (tscn
, 0, 0) != 1)
622 INTERNAL_ERROR (fname
);
626 Elf_Data
*tdata
= elf_getdata (tscn
, NULL
);
627 if (tdata
== NULL
|| tdata
->d_buf
== NULL
628 || tdata
->d_type
!= ELF_T_BYTE
)
629 INTERNAL_ERROR (fname
);
631 /* Pick up the symbol table and shndx table to
632 resolve relocation symbol indexes. */
633 Elf64_Word symt
= shdr
->sh_link
;
634 Elf_Data
*symdata
, *xndxdata
;
635 Elf_Scn
* symscn
= elf_getscn (elf
, symt
);
636 symdata
= elf_getdata (symscn
, NULL
);
637 xndxdata
= get_xndxdata (elf
, symscn
);
639 INTERNAL_ERROR (fname
);
641 if (shdr
->sh_entsize
== 0)
642 INTERNAL_ERROR (fname
);
644 size_t nrels
= shdr
->sh_size
/ shdr
->sh_entsize
;
646 const bool is_rela
= (shdr
->sh_type
== SHT_RELA
);
647 const unsigned int ei_data
= ehdr
->e_ident
[EI_DATA
];
649 for (size_t relidx
= 0; relidx
< nrels
; ++relidx
)
651 int rtype
, symndx
, offset
, addend
;
652 union { GElf_Rela rela
; GElf_Rel rel
; } mem
;
653 void *rel_p
; /* Pointer to either rela or rel above */
657 GElf_Rela
*r
= gelf_getrela (reldata
, relidx
, &mem
.rela
);
658 offset
= r
->r_offset
;
659 addend
= r
->r_addend
;
660 rtype
= GELF_R_TYPE (r
->r_info
);
661 symndx
= GELF_R_SYM (r
->r_info
);
666 GElf_Rel
*r
= gelf_getrel (reldata
, relidx
, &mem
.rel
);
667 offset
= r
->r_offset
;
669 rtype
= GELF_R_TYPE (r
->r_info
);
670 symndx
= GELF_R_SYM (r
->r_info
);
674 /* R_*_NONE relocs can always just be removed. */
678 /* We only do simple absolute relocations. */
680 Elf_Type type
= ebl_reloc_simple_type (ebl
, rtype
, &addsub
);
681 if (type
== ELF_T_NUM
)
682 goto relocate_failed
;
684 /* And only for relocations against other debug sections. */
687 GElf_Sym
*sym
= gelf_getsymshndx (symdata
, xndxdata
,
690 Elf32_Word sec
= (sym
->st_shndx
== SHN_XINDEX
691 ? xndx
: sym
->st_shndx
);
693 bool dbg_scn
= ebl_debugscn_p (ebl
, secndx_name (elf
, sec
));
696 goto relocate_failed
;
698 if (! relocate (elf
, offset
, addend
,
699 tdata
, ei_data
, fname
, is_rela
,
701 goto relocate_failed
;
709 gelf_update_rela (reldata
, next
, rel_p
);
711 gelf_update_rel (reldata
, next
, rel_p
);
717 shdr
->sh_size
= reldata
->d_size
= nrels
* shdr
->sh_entsize
;
718 gelf_update_shdr (scn
, shdr
);
720 if (is_gnu_compressed
)
722 if (elf_compress_gnu (tscn
, 1, ELF_CHF_FORCE
) != 1)
723 INTERNAL_ERROR (fname
);
725 else if (tcompress_type
!= 0)
727 if (elf_compress (tscn
, tcompress_type
, ELF_CHF_FORCE
) != 1)
728 INTERNAL_ERROR (fname
);
735 process_file (const char *fname
)
737 /* If we have to preserve the modify and access timestamps get them
738 now. We cannot use fstat() after opening the file since the open
739 would change the access time. */
741 struct timespec tv
[2];
745 if (stat (fname
, &pre_st
) != 0)
747 error (0, errno
, _("cannot stat input file '%s'"), fname
);
751 /* If we have to preserve the timestamp, we need it in the
752 format utimes() understands. */
753 tv
[0] = pre_st
.st_atim
;
754 tv
[1] = pre_st
.st_mtim
;
758 int fd
= open (fname
, output_fname
== NULL
? O_RDWR
: O_RDONLY
);
761 error (0, errno
, _("while opening '%s'"), fname
);
765 /* We always use fstat() even if we called stat() before. This is
766 done to make sure the information returned by stat() is for the
769 if (fstat (fd
, &st
) != 0)
771 error (0, errno
, _("cannot stat input file '%s'"), fname
);
774 /* Paranoid mode on. */
776 && (st
.st_ino
!= pre_st
.st_ino
|| st
.st_dev
!= pre_st
.st_dev
))
778 /* We detected a race. Try again. */
783 /* Now get the ELF descriptor. */
784 Elf
*elf
= elf_begin (fd
, output_fname
== NULL
? ELF_C_RDWR
: ELF_C_READ
,
787 switch (elf_kind (elf
))
790 result
= handle_elf (fd
, elf
, NULL
, fname
, st
.st_mode
& ACCESSPERMS
,
791 preserve_dates
? tv
: NULL
);
795 /* It is not possible to strip the content of an archive direct
796 the output to a specific file. */
797 if (unlikely (output_fname
!= NULL
|| debug_fname
!= NULL
))
799 error (0, 0, _("%s: cannot use -o or -f when stripping archive"),
805 /* We would like to support ar archives, but currently it just
806 doesn't work at all since we call elf_clone on the members
807 which doesn't really support ar members.
808 result = handle_ar (fd, elf, NULL, fname,
809 preserve_dates ? tv : NULL);
811 error (0, 0, _("%s: no support for stripping archive"),
818 error (0, 0, _("%s: File format not recognized"), fname
);
823 if (unlikely (elf_end (elf
) != 0))
824 INTERNAL_ERROR (fname
);
831 /* Processing for --reloc-debug-sections-only. */
833 handle_debug_relocs (Elf
*elf
, Ebl
*ebl
, Elf
*new_elf
,
834 GElf_Ehdr
*ehdr
, const char *fname
, size_t shstrndx
,
835 GElf_Off
*last_offset
, GElf_Xword
*last_size
)
838 /* Copy over the ELF header. */
839 if (gelf_update_ehdr (new_elf
, ehdr
) == 0)
841 error (0, 0, "couldn't update new ehdr: %s", elf_errmsg (-1));
845 /* Copy over sections and record end of allocated sections. */
846 GElf_Off lastoffset
= 0;
848 while ((scn
= elf_nextscn (elf
, scn
)) != NULL
)
850 /* Get the header. */
852 if (gelf_getshdr (scn
, &shdr
) == NULL
)
854 error (0, 0, "couldn't get shdr: %s", elf_errmsg (-1));
858 /* Create new section. */
859 Elf_Scn
*new_scn
= elf_newscn (new_elf
);
862 error (0, 0, "couldn't create new section: %s", elf_errmsg (-1));
866 if (gelf_update_shdr (new_scn
, &shdr
) == 0)
868 error (0, 0, "couldn't update shdr: %s", elf_errmsg (-1));
872 /* Copy over section data. */
873 Elf_Data
*data
= NULL
;
874 while ((data
= elf_getdata (scn
, data
)) != NULL
)
876 Elf_Data
*new_data
= elf_newdata (new_scn
);
877 if (new_data
== NULL
)
879 error (0, 0, "couldn't create new section data: %s",
886 /* Record last offset of allocated section. */
887 if ((shdr
.sh_flags
& SHF_ALLOC
) != 0)
889 GElf_Off filesz
= (shdr
.sh_type
!= SHT_NOBITS
891 if (lastoffset
< shdr
.sh_offset
+ filesz
)
892 lastoffset
= shdr
.sh_offset
+ filesz
;
896 /* Make sure section header name table is setup correctly, we'll
897 need it to determine whether to relocate sections. */
898 if (update_shdrstrndx (new_elf
, shstrndx
) != 0)
900 error (0, 0, "error updating shdrstrndx: %s", elf_errmsg (-1));
904 /* Adjust the relocation sections. */
905 remove_debug_relocations (ebl
, new_elf
, ehdr
, fname
, shstrndx
);
907 /* Adjust the offsets of the non-allocated sections, so they come after
908 the allocated sections. */
910 while ((scn
= elf_nextscn (new_elf
, scn
)) != NULL
)
912 /* Get the header. */
914 if (gelf_getshdr (scn
, &shdr
) == NULL
)
916 error (0, 0, "couldn't get shdr: %s", elf_errmsg (-1));
920 /* Adjust non-allocated section offsets to be after any allocated. */
921 if ((shdr
.sh_flags
& SHF_ALLOC
) == 0)
923 shdr
.sh_offset
= ((lastoffset
+ shdr
.sh_addralign
- 1)
924 & ~((GElf_Off
) (shdr
.sh_addralign
- 1)));
925 if (gelf_update_shdr (scn
, &shdr
) == 0)
927 error (0, 0, "couldn't update shdr: %s", elf_errmsg (-1));
931 GElf_Off filesz
= (shdr
.sh_type
!= SHT_NOBITS
933 lastoffset
= shdr
.sh_offset
+ filesz
;
934 *last_offset
= shdr
.sh_offset
;
942 /* Maximum size of array allocated on stack. */
943 #define MAX_STACK_ALLOC (400 * 1024)
946 handle_elf (int fd
, Elf
*elf
, const char *prefix
, const char *fname
,
947 mode_t mode
, struct timespec tvp
[2])
949 size_t prefix_len
= prefix
== NULL
? 0 : strlen (prefix
);
950 size_t fname_len
= strlen (fname
) + 1;
951 char *fullname
= alloca (prefix_len
+ 1 + fname_len
);
953 Elf
*debugelf
= NULL
;
954 tmp_debug_fname
= NULL
;
957 GElf_Off lastsec_offset
= 0;
958 Elf64_Xword lastsec_size
= 0;
965 Elf_Data
*debug_data
;
967 Elf32_Word idx
; /* Index in new file. */
968 Elf32_Word old_sh_link
; /* Original value of shdr.sh_link. */
969 Elf32_Word symtab_idx
;
970 Elf32_Word version_idx
;
971 Elf32_Word group_idx
;
972 Elf32_Word group_cnt
;
975 Elf32_Word
*newsymidx
;
981 GElf_Ehdr newehdr_mem
;
983 GElf_Ehdr debugehdr_mem
;
984 GElf_Ehdr
*debugehdr
;
985 Dwelf_Strtab
*shst
= NULL
;
986 Elf_Data debuglink_crc_data
;
987 bool any_symtab_changes
= false;
988 Elf_Data
*shstrtab_data
= NULL
;
989 void *debuglink_buf
= NULL
;
991 /* Create the full name of the file. */
994 cp
= mempcpy (cp
, prefix
, prefix_len
);
997 memcpy (cp
, fname
, fname_len
);
999 /* If we are not replacing the input file open a new file here. */
1000 if (output_fname
!= NULL
)
1002 fd
= open (output_fname
, O_RDWR
| O_CREAT
, mode
);
1003 if (unlikely (fd
== -1))
1005 error (0, errno
, _("cannot open '%s'"), output_fname
);
1012 /* Get the EBL handling. Removing all debugging symbols with the -g
1013 option or resolving all relocations between debug sections with
1014 the --reloc-debug-sections option are currently the only reasons
1015 we need EBL so don't open the backend unless necessary. */
1017 if (remove_debug
|| reloc_debug
|| reloc_debug_only
)
1019 ebl
= ebl_openbackend (elf
);
1022 error (0, errno
, _("cannot open EBL backend"));
1028 /* Open the additional file the debug information will be stored in. */
1029 if (debug_fname
!= NULL
)
1031 /* Create a temporary file name. We do not want to overwrite
1032 the debug file if the file would not contain any
1034 size_t debug_fname_len
= strlen (debug_fname
);
1035 tmp_debug_fname
= (char *) xmalloc (debug_fname_len
+ sizeof (".XXXXXX"));
1036 strcpy (mempcpy (tmp_debug_fname
, debug_fname
, debug_fname_len
),
1039 debug_fd
= mkstemp (tmp_debug_fname
);
1040 if (unlikely (debug_fd
== -1))
1042 error (0, errno
, _("cannot open '%s'"), debug_fname
);
1048 /* Get the information from the old file. */
1050 GElf_Ehdr
*ehdr
= gelf_getehdr (elf
, &ehdr_mem
);
1052 INTERNAL_ERROR (fname
);
1054 /* Get the section header string table index. */
1055 if (unlikely (elf_getshdrstrndx (elf
, &shstrndx
) < 0))
1058 error (EXIT_FAILURE
, 0,
1059 _("cannot get section header string table index"));
1062 /* Get the number of phdrs in the old file. */
1064 if (elf_getphdrnum (elf
, &phnum
) != 0)
1067 error (EXIT_FAILURE
, 0, _("cannot get number of phdrs"));
1070 /* We now create a new ELF descriptor for the same file. We
1071 construct it almost exactly in the same way with some information
1074 if (output_fname
!= NULL
)
1075 newelf
= elf_begin (fd
, ELF_C_WRITE_MMAP
, NULL
);
1077 newelf
= elf_clone (elf
, ELF_C_EMPTY
);
1079 if (unlikely (gelf_newehdr (newelf
, gelf_getclass (elf
)) == 0))
1081 error (0, 0, _("cannot create new ehdr for file '%s': %s"),
1082 output_fname
?: fname
, elf_errmsg (-1));
1086 /* Copy over the old program header if needed. */
1089 if (unlikely (gelf_newphdr (newelf
, phnum
) == 0))
1091 error (0, 0, _("cannot create new phdr for file '%s': %s"),
1092 output_fname
?: fname
, elf_errmsg (-1));
1096 for (cnt
= 0; cnt
< phnum
; ++cnt
)
1099 GElf_Phdr
*phdr
= gelf_getphdr (elf
, cnt
, &phdr_mem
);
1101 || unlikely (gelf_update_phdr (newelf
, cnt
, phdr
) == 0))
1102 INTERNAL_ERROR (fname
);
1106 if (reloc_debug_only
)
1108 if (handle_debug_relocs (elf
, ebl
, newelf
, ehdr
, fname
, shstrndx
,
1109 &lastsec_offset
, &lastsec_size
) != 0)
1115 goto done
; /* Skip all actual stripping operations. */
1118 if (debug_fname
!= NULL
)
1120 /* Also create an ELF descriptor for the debug file */
1121 debugelf
= elf_begin (debug_fd
, ELF_C_WRITE
, NULL
);
1122 if (unlikely (gelf_newehdr (debugelf
, gelf_getclass (elf
)) == 0))
1124 error (0, 0, _("cannot create new ehdr for file '%s': %s"),
1125 debug_fname
, elf_errmsg (-1));
1129 /* Copy over the old program header if needed. */
1132 if (unlikely (gelf_newphdr (debugelf
, phnum
) == 0))
1134 error (0, 0, _("cannot create new phdr for file '%s': %s"),
1135 debug_fname
, elf_errmsg (-1));
1139 for (cnt
= 0; cnt
< phnum
; ++cnt
)
1142 GElf_Phdr
*phdr
= gelf_getphdr (elf
, cnt
, &phdr_mem
);
1144 || unlikely (gelf_update_phdr (debugelf
, cnt
, phdr
) == 0))
1145 INTERNAL_ERROR (fname
);
1150 /* Number of sections. */
1152 if (unlikely (elf_getshdrnum (elf
, &shnum
) < 0))
1154 error (0, 0, _("cannot determine number of sections: %s"),
1159 if (shstrndx
>= shnum
)
1162 #define elf_assert(test) do { if (!(test)) goto illformed; } while (0)
1164 /* Storage for section information. We leave room for two more
1165 entries since we unconditionally create a section header string
1166 table. Maybe some weird tool created an ELF file without one.
1167 The other one is used for the debug link section. */
1168 if ((shnum
+ 2) * sizeof (struct shdr_info
) > MAX_STACK_ALLOC
)
1169 shdr_info
= (struct shdr_info
*) xcalloc (shnum
+ 2,
1170 sizeof (struct shdr_info
));
1173 shdr_info
= (struct shdr_info
*) alloca ((shnum
+ 2)
1174 * sizeof (struct shdr_info
));
1175 memset (shdr_info
, '\0', (shnum
+ 2) * sizeof (struct shdr_info
));
1178 /* Track whether allocated sections all come before non-allocated ones. */
1179 bool seen_allocated
= false;
1180 bool seen_unallocated
= false;
1181 bool mixed_allocated_unallocated
= false;
1183 /* Prepare section information data structure. */
1186 while ((scn
= elf_nextscn (elf
, scn
)) != NULL
)
1188 /* This should always be true (i.e., there should not be any
1189 holes in the numbering). */
1190 elf_assert (elf_ndxscn (scn
) == cnt
);
1192 shdr_info
[cnt
].scn
= scn
;
1194 /* Get the header. */
1195 if (gelf_getshdr (scn
, &shdr_info
[cnt
].shdr
) == NULL
)
1196 INTERNAL_ERROR (fname
);
1198 /* Normally (in non-ET_REL files) we see all allocated sections first,
1199 then all non-allocated. */
1200 if ((shdr_info
[cnt
].shdr
.sh_flags
& SHF_ALLOC
) == 0)
1201 seen_unallocated
= true;
1204 if (seen_unallocated
&& seen_allocated
)
1205 mixed_allocated_unallocated
= true;
1206 seen_allocated
= true;
1209 /* Get the name of the section. */
1210 shdr_info
[cnt
].name
= elf_strptr (elf
, shstrndx
,
1211 shdr_info
[cnt
].shdr
.sh_name
);
1212 if (shdr_info
[cnt
].name
== NULL
)
1215 error (0, 0, _("illformed file '%s'"), fname
);
1219 /* Sanity check the user. */
1220 if (section_name_matches (remove_secs
, shdr_info
[cnt
].name
))
1222 if ((shdr_info
[cnt
].shdr
.sh_flags
& SHF_ALLOC
) != 0)
1225 _("Cannot remove allocated section '%s'"),
1226 shdr_info
[cnt
].name
);
1231 if (section_name_matches (keep_secs
, shdr_info
[cnt
].name
))
1234 _("Cannot both keep and remove section '%s'"),
1235 shdr_info
[cnt
].name
);
1241 /* Mark them as present but not yet investigated. */
1242 shdr_info
[cnt
].idx
= 1;
1244 /* Remember the shdr.sh_link value. */
1245 shdr_info
[cnt
].old_sh_link
= shdr_info
[cnt
].shdr
.sh_link
;
1246 if (shdr_info
[cnt
].old_sh_link
>= shnum
)
1249 /* Sections in files other than relocatable object files which
1250 not loaded can be freely moved by us. In theory we can also
1251 freely move around allocated nobits sections. But we don't
1252 to keep the layout of all allocated sections as similar as
1253 possible to the original file. In relocatable object files
1254 everything can be moved. */
1256 || (shdr_info
[cnt
].shdr
.sh_flags
& SHF_ALLOC
) == 0)
1257 shdr_info
[cnt
].shdr
.sh_offset
= 0;
1259 /* If this is an extended section index table store an
1260 appropriate reference. */
1261 if (unlikely (shdr_info
[cnt
].shdr
.sh_type
== SHT_SYMTAB_SHNDX
))
1263 elf_assert (shdr_info
[shdr_info
[cnt
].shdr
.sh_link
].symtab_idx
== 0);
1264 shdr_info
[shdr_info
[cnt
].shdr
.sh_link
].symtab_idx
= cnt
;
1266 else if (unlikely (shdr_info
[cnt
].shdr
.sh_type
== SHT_GROUP
))
1268 /* Cross-reference the sections contained in the section
1270 shdr_info
[cnt
].data
= elf_getdata (shdr_info
[cnt
].scn
, NULL
);
1271 if (shdr_info
[cnt
].data
== NULL
1272 || shdr_info
[cnt
].data
->d_size
< sizeof (Elf32_Word
))
1273 INTERNAL_ERROR (fname
);
1275 /* XXX Fix for unaligned access. */
1276 Elf32_Word
*grpref
= (Elf32_Word
*) shdr_info
[cnt
].data
->d_buf
;
1279 inner
< shdr_info
[cnt
].data
->d_size
/ sizeof (Elf32_Word
);
1282 if (grpref
[inner
] < shnum
)
1283 shdr_info
[grpref
[inner
]].group_idx
= cnt
;
1288 if (inner
== 1 || (inner
== 2 && (grpref
[0] & GRP_COMDAT
) == 0))
1289 /* If the section group contains only one element and this
1290 is n COMDAT section we can drop it right away. */
1291 shdr_info
[cnt
].idx
= 0;
1293 shdr_info
[cnt
].group_cnt
= inner
- 1;
1295 else if (unlikely (shdr_info
[cnt
].shdr
.sh_type
== SHT_GNU_versym
))
1297 elf_assert (shdr_info
[shdr_info
[cnt
].shdr
.sh_link
].version_idx
== 0);
1298 shdr_info
[shdr_info
[cnt
].shdr
.sh_link
].version_idx
= cnt
;
1301 /* If this section is part of a group make sure it is not
1302 discarded right away. */
1303 if ((shdr_info
[cnt
].shdr
.sh_flags
& SHF_GROUP
) != 0)
1305 elf_assert (shdr_info
[cnt
].group_idx
!= 0);
1307 if (shdr_info
[shdr_info
[cnt
].group_idx
].idx
== 0)
1309 /* The section group section might be removed.
1310 Don't remove the SHF_GROUP flag. The section is
1311 either also removed, in which case the flag doesn't matter.
1312 Or it moves with the group into the debug file, then
1313 it will be reconnected with the new group and should
1314 still have the flag set. */
1315 shdr_info
[cnt
].group_idx
= 0;
1319 /* Increment the counter. */
1323 /* Now determine which sections can go away. The general rule is that
1324 all sections which are not used at runtime are stripped out. But
1325 there are a few exceptions:
1327 - special sections named ".comment" and ".note" are kept
1328 - OS or architecture specific sections are kept since we might not
1329 know how to handle them
1330 - if a section is referred to from a section which is not removed
1331 in the sh_link or sh_info element it cannot be removed either
1332 - the user might have explicitly said to remove or keep a section
1334 for (cnt
= 1; cnt
< shnum
; ++cnt
)
1335 /* Check whether the section can be removed. Since we will create
1336 a new .shstrtab assume it will be removed too. */
1337 if (remove_shdrs
? !(shdr_info
[cnt
].shdr
.sh_flags
& SHF_ALLOC
)
1338 : (ebl_section_strip_p (ebl
, &shdr_info
[cnt
].shdr
,
1339 shdr_info
[cnt
].name
, remove_comment
,
1342 || section_name_matches (remove_secs
, shdr_info
[cnt
].name
)))
1344 /* The user might want to explicitly keep this one. */
1345 if (section_name_matches (keep_secs
, shdr_info
[cnt
].name
))
1348 /* For now assume this section will be removed. */
1349 shdr_info
[cnt
].idx
= 0;
1351 idx
= shdr_info
[cnt
].group_idx
;
1354 /* The section group data is already loaded. */
1355 elf_assert (shdr_info
[idx
].data
!= NULL
1356 && shdr_info
[idx
].data
->d_buf
!= NULL
1357 && shdr_info
[idx
].data
->d_size
>= sizeof (Elf32_Word
));
1359 /* If the references section group is a normal section
1360 group and has one element remaining, or if it is an
1361 empty COMDAT section group it is removed. */
1362 bool is_comdat
= (((Elf32_Word
*) shdr_info
[idx
].data
->d_buf
)[0]
1365 --shdr_info
[idx
].group_cnt
;
1366 if ((!is_comdat
&& shdr_info
[idx
].group_cnt
== 1)
1367 || (is_comdat
&& shdr_info
[idx
].group_cnt
== 0))
1369 shdr_info
[idx
].idx
= 0;
1370 /* Continue recursively. */
1371 idx
= shdr_info
[idx
].group_idx
;
1378 /* Mark the SHT_NULL section as handled. */
1379 shdr_info
[0].idx
= 2;
1382 /* Handle exceptions: section groups and cross-references. We might
1383 have to repeat this a few times since the resetting of the flag
1389 for (cnt
= 1; cnt
< shnum
; ++cnt
)
1391 if (shdr_info
[cnt
].idx
== 0)
1393 /* If a relocation section is marked as being removed make
1394 sure the section it is relocating is removed, too. */
1395 if (shdr_info
[cnt
].shdr
.sh_type
== SHT_REL
1396 || shdr_info
[cnt
].shdr
.sh_type
== SHT_RELA
)
1398 if (shdr_info
[cnt
].shdr
.sh_info
>= shnum
)
1400 else if (shdr_info
[shdr_info
[cnt
].shdr
.sh_info
].idx
!= 0)
1401 shdr_info
[cnt
].idx
= 1;
1404 /* If a group section is marked as being removed make
1405 sure all the sections it contains are being removed, too. */
1406 if (shdr_info
[cnt
].shdr
.sh_type
== SHT_GROUP
)
1409 grpref
= (Elf32_Word
*) shdr_info
[cnt
].data
->d_buf
;
1411 in
< shdr_info
[cnt
].data
->d_size
/ sizeof (Elf32_Word
);
1413 if (grpref
[in
] < shnum
)
1415 if (shdr_info
[grpref
[in
]].idx
!= 0)
1417 shdr_info
[cnt
].idx
= 1;
1426 if (shdr_info
[cnt
].idx
== 1)
1428 /* The content of symbol tables we don't remove must not
1429 reference any section which we do remove. Otherwise
1430 we cannot remove the section. */
1431 if (debug_fname
!= NULL
1432 && shdr_info
[cnt
].debug_data
== NULL
1433 && (shdr_info
[cnt
].shdr
.sh_type
== SHT_DYNSYM
1434 || shdr_info
[cnt
].shdr
.sh_type
== SHT_SYMTAB
))
1436 /* Make sure the data is loaded. */
1437 if (shdr_info
[cnt
].data
== NULL
)
1440 = elf_getdata (shdr_info
[cnt
].scn
, NULL
);
1441 if (shdr_info
[cnt
].data
== NULL
)
1442 INTERNAL_ERROR (fname
);
1444 Elf_Data
*symdata
= shdr_info
[cnt
].data
;
1446 /* If there is an extended section index table load it
1448 if (shdr_info
[cnt
].symtab_idx
!= 0
1449 && shdr_info
[shdr_info
[cnt
].symtab_idx
].data
== NULL
)
1451 elf_assert (shdr_info
[cnt
].shdr
.sh_type
== SHT_SYMTAB
);
1453 shdr_info
[shdr_info
[cnt
].symtab_idx
].data
1454 = elf_getdata (shdr_info
[shdr_info
[cnt
].symtab_idx
].scn
,
1456 if (shdr_info
[shdr_info
[cnt
].symtab_idx
].data
== NULL
)
1457 INTERNAL_ERROR (fname
);
1460 = shdr_info
[shdr_info
[cnt
].symtab_idx
].data
;
1462 /* Go through all symbols and make sure the section they
1463 reference is not removed. */
1464 size_t elsize
= gelf_fsize (elf
, ELF_T_SYM
, 1, EV_CURRENT
);
1466 for (size_t inner
= 0;
1467 inner
< shdr_info
[cnt
].data
->d_size
/ elsize
;
1472 GElf_Sym
*sym
= gelf_getsymshndx (symdata
, xndxdata
,
1476 INTERNAL_ERROR (fname
);
1478 size_t scnidx
= sym
->st_shndx
;
1479 if (scnidx
== SHN_UNDEF
|| scnidx
>= shnum
1480 || (scnidx
>= SHN_LORESERVE
1481 && scnidx
<= SHN_HIRESERVE
1482 && scnidx
!= SHN_XINDEX
)
1483 /* Don't count in the section symbols. */
1484 || GELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
1485 /* This is no section index, leave it alone. */
1487 else if (scnidx
== SHN_XINDEX
)
1490 if (scnidx
>= shnum
)
1493 if (shdr_info
[scnidx
].idx
== 0)
1494 /* This symbol table has a real symbol in
1495 a discarded section. So preserve the
1496 original table in the debug file. Unless
1497 it is a redundant data marker to a debug
1498 (data only) section. */
1499 if (! (ebl_section_strip_p (ebl
,
1500 &shdr_info
[scnidx
].shdr
,
1501 shdr_info
[scnidx
].name
,
1504 && ebl_data_marker_symbol (ebl
, sym
,
1506 shdr_info
[cnt
].shdr
.sh_link
,
1508 shdr_info
[cnt
].debug_data
= symdata
;
1512 /* Cross referencing happens:
1513 - for the cases the ELF specification says. That are
1514 + SHT_DYNAMIC in sh_link to string table
1515 + SHT_HASH in sh_link to symbol table
1516 + SHT_REL and SHT_RELA in sh_link to symbol table
1517 + SHT_SYMTAB and SHT_DYNSYM in sh_link to string table
1518 + SHT_GROUP in sh_link to symbol table
1519 + SHT_SYMTAB_SHNDX in sh_link to symbol table
1520 Other (OS or architecture-specific) sections might as
1521 well use this field so we process it unconditionally.
1522 - references inside section groups
1523 - specially marked references in sh_info if the SHF_INFO_LINK
1527 if (shdr_info
[shdr_info
[cnt
].shdr
.sh_link
].idx
== 0)
1529 shdr_info
[shdr_info
[cnt
].shdr
.sh_link
].idx
= 1;
1530 changes
|= shdr_info
[cnt
].shdr
.sh_link
< cnt
;
1533 /* Handle references through sh_info. */
1534 if (SH_INFO_LINK_P (&shdr_info
[cnt
].shdr
))
1536 if (shdr_info
[cnt
].shdr
.sh_info
>= shnum
)
1538 else if ( shdr_info
[shdr_info
[cnt
].shdr
.sh_info
].idx
== 0)
1540 shdr_info
[shdr_info
[cnt
].shdr
.sh_info
].idx
= 1;
1541 changes
|= shdr_info
[cnt
].shdr
.sh_info
< cnt
;
1545 /* Mark the section as investigated. */
1546 shdr_info
[cnt
].idx
= 2;
1549 if (debug_fname
!= NULL
1550 && (shdr_info
[cnt
].idx
== 0 || shdr_info
[cnt
].debug_data
!= NULL
))
1552 /* This section is being preserved in the debug file.
1553 Sections it refers to must be preserved there too.
1555 In this pass we mark sections to be preserved in both
1556 files by setting the .debug_data pointer to the original
1557 file's .data pointer. Below, we'll copy the section
1559 size_t shdr_indices
[2] = { shdr_info
[cnt
].shdr
.sh_link
, 0 };
1562 if (SH_INFO_LINK_P (&shdr_info
[cnt
].shdr
))
1564 shdr_indices
[1] = shdr_info
[cnt
].shdr
.sh_info
;
1568 for (int j
= 0; j
< n
; j
++)
1570 size_t i
= shdr_indices
[j
];
1571 if (i
!= 0 && i
< shnum
+ 2 && shdr_info
[i
].idx
!= 0
1572 && shdr_info
[i
].debug_data
== NULL
)
1574 if (shdr_info
[i
].data
== NULL
)
1575 shdr_info
[i
].data
= elf_getdata (shdr_info
[i
].scn
, NULL
);
1576 if (shdr_info
[i
].data
== NULL
)
1577 INTERNAL_ERROR (fname
);
1579 shdr_info
[i
].debug_data
= shdr_info
[i
].data
;
1588 /* Copy the removed sections to the debug output file.
1589 The ones that are not removed in the stripped file are SHT_NOBITS. */
1590 if (debug_fname
!= NULL
)
1592 for (cnt
= 1; cnt
< shnum
; ++cnt
)
1594 scn
= elf_newscn (debugelf
);
1598 error (EXIT_FAILURE
, 0,
1599 _("while generating output file: %s"),
1603 bool discard_section
= (shdr_info
[cnt
].idx
> 0
1604 && shdr_info
[cnt
].debug_data
== NULL
1605 && shdr_info
[cnt
].shdr
.sh_type
!= SHT_NOTE
1606 && shdr_info
[cnt
].shdr
.sh_type
!= SHT_GROUP
1607 && cnt
!= shstrndx
);
1609 /* Set the section header in the new file. */
1610 GElf_Shdr debugshdr
= shdr_info
[cnt
].shdr
;
1611 if (discard_section
)
1612 debugshdr
.sh_type
= SHT_NOBITS
;
1614 if (unlikely (gelf_update_shdr (scn
, &debugshdr
) == 0))
1615 /* There cannot be any overflows. */
1616 INTERNAL_ERROR (fname
);
1618 /* Get the data from the old file if necessary. */
1619 if (shdr_info
[cnt
].data
== NULL
)
1621 shdr_info
[cnt
].data
= elf_getdata (shdr_info
[cnt
].scn
, NULL
);
1622 if (shdr_info
[cnt
].data
== NULL
)
1623 INTERNAL_ERROR (fname
);
1626 /* Set the data. This is done by copying from the old file. */
1627 Elf_Data
*debugdata
= elf_newdata (scn
);
1628 if (debugdata
== NULL
)
1629 INTERNAL_ERROR (fname
);
1631 /* Copy the structure. This data may be modified in place
1632 before we write out the file. */
1633 *debugdata
= *shdr_info
[cnt
].data
;
1634 if (discard_section
)
1635 debugdata
->d_buf
= NULL
;
1636 else if (shdr_info
[cnt
].debug_data
!= NULL
1637 || shdr_info
[cnt
].shdr
.sh_type
== SHT_GROUP
)
1639 /* Copy the original data before it gets modified. */
1640 shdr_info
[cnt
].debug_data
= debugdata
;
1641 if (debugdata
->d_buf
== NULL
)
1642 INTERNAL_ERROR (fname
);
1643 debugdata
->d_buf
= memcpy (xmalloc (debugdata
->d_size
),
1644 debugdata
->d_buf
, debugdata
->d_size
);
1648 /* Finish the ELF header. Fill in the fields not handled by
1649 libelf from the old file. */
1650 debugehdr
= gelf_getehdr (debugelf
, &debugehdr_mem
);
1651 if (debugehdr
== NULL
)
1652 INTERNAL_ERROR (fname
);
1654 memcpy (debugehdr
->e_ident
, ehdr
->e_ident
, EI_NIDENT
);
1655 debugehdr
->e_type
= ehdr
->e_type
;
1656 debugehdr
->e_machine
= ehdr
->e_machine
;
1657 debugehdr
->e_version
= ehdr
->e_version
;
1658 debugehdr
->e_entry
= ehdr
->e_entry
;
1659 debugehdr
->e_flags
= ehdr
->e_flags
;
1661 if (unlikely (gelf_update_ehdr (debugelf
, debugehdr
) == 0))
1663 error (0, 0, _("%s: error while updating ELF header: %s"),
1664 debug_fname
, elf_errmsg (-1));
1670 if (elf_getshdrstrndx (elf
, &shdrstrndx
) < 0)
1672 error (0, 0, _("%s: error while getting shdrstrndx: %s"),
1673 fname
, elf_errmsg (-1));
1678 if (update_shdrstrndx (debugelf
, shdrstrndx
) != 0)
1680 error (0, 0, _("%s: error updating shdrstrndx: %s"),
1681 debug_fname
, elf_errmsg (-1));
1687 /* Although we always create a new section header string table we
1688 don't explicitly mark the existing one as unused. It can still
1689 be used through a symbol table section we are keeping. If not it
1690 will already be marked as unused. */
1692 /* We need a string table for the section headers. */
1693 shst
= dwelf_strtab_init (true);
1697 error (EXIT_FAILURE
, errno
, _("while preparing output for '%s'"),
1698 output_fname
?: fname
);
1701 /* Assign new section numbers. */
1702 shdr_info
[0].idx
= 0;
1703 for (cnt
= idx
= 1; cnt
< shnum
; ++cnt
)
1704 if (shdr_info
[cnt
].idx
> 0)
1706 shdr_info
[cnt
].idx
= idx
++;
1708 /* Create a new section. */
1709 shdr_info
[cnt
].newscn
= elf_newscn (newelf
);
1710 if (shdr_info
[cnt
].newscn
== NULL
)
1713 error (EXIT_FAILURE
, 0,
1714 _("while generating output file: %s"),
1718 elf_assert (elf_ndxscn (shdr_info
[cnt
].newscn
) == shdr_info
[cnt
].idx
);
1720 /* Add this name to the section header string table. */
1721 shdr_info
[cnt
].se
= dwelf_strtab_add (shst
, shdr_info
[cnt
].name
);
1724 /* Test whether we are doing anything at all. Either all removable
1725 sections are already gone. Or the only section we would remove is
1726 the .shstrtab section which we would add again. */
1727 bool removing_sections
= !(cnt
== idx
1729 && shdr_info
[shstrndx
].idx
== 0));
1730 if (output_fname
== NULL
&& !removing_sections
)
1733 /* Create the reference to the file with the debug info (if any). */
1734 if (debug_fname
!= NULL
&& !remove_shdrs
&& removing_sections
)
1736 /* Add the section header string table section name. */
1737 shdr_info
[cnt
].se
= dwelf_strtab_add_len (shst
, ".gnu_debuglink", 15);
1738 shdr_info
[cnt
].idx
= idx
++;
1740 /* Create the section header. */
1741 shdr_info
[cnt
].shdr
.sh_type
= SHT_PROGBITS
;
1742 shdr_info
[cnt
].shdr
.sh_flags
= 0;
1743 shdr_info
[cnt
].shdr
.sh_addr
= 0;
1744 shdr_info
[cnt
].shdr
.sh_link
= SHN_UNDEF
;
1745 shdr_info
[cnt
].shdr
.sh_info
= SHN_UNDEF
;
1746 shdr_info
[cnt
].shdr
.sh_entsize
= 0;
1747 shdr_info
[cnt
].shdr
.sh_addralign
= 4;
1748 /* We set the offset to zero here. Before we write the ELF file the
1749 field must have the correct value. This is done in the final
1750 loop over all section. Then we have all the information needed. */
1751 shdr_info
[cnt
].shdr
.sh_offset
= 0;
1753 /* Create the section. */
1754 shdr_info
[cnt
].newscn
= elf_newscn (newelf
);
1755 if (shdr_info
[cnt
].newscn
== NULL
)
1758 error (EXIT_FAILURE
, 0,
1759 _("while create section header section: %s"),
1762 elf_assert (elf_ndxscn (shdr_info
[cnt
].newscn
) == shdr_info
[cnt
].idx
);
1764 shdr_info
[cnt
].data
= elf_newdata (shdr_info
[cnt
].newscn
);
1765 if (shdr_info
[cnt
].data
== NULL
)
1768 error (EXIT_FAILURE
, 0, _("cannot allocate section data: %s"),
1772 char *debug_basename
= basename (debug_fname_embed
?: debug_fname
);
1773 off_t crc_offset
= strlen (debug_basename
) + 1;
1774 /* Align to 4 byte boundary */
1775 crc_offset
= ((crc_offset
- 1) & ~3) + 4;
1777 shdr_info
[cnt
].data
->d_align
= 4;
1778 shdr_info
[cnt
].shdr
.sh_size
= shdr_info
[cnt
].data
->d_size
1780 debuglink_buf
= xcalloc (1, shdr_info
[cnt
].data
->d_size
);
1781 shdr_info
[cnt
].data
->d_buf
= debuglink_buf
;
1783 strcpy (shdr_info
[cnt
].data
->d_buf
, debug_basename
);
1785 /* Cache this Elf_Data describing the CRC32 word in the section.
1786 We'll fill this in when we have written the debug file. */
1787 debuglink_crc_data
= *shdr_info
[cnt
].data
;
1788 debuglink_crc_data
.d_buf
= ((char *) debuglink_crc_data
.d_buf
1790 debuglink_crc_data
.d_size
= 4;
1792 /* One more section done. */
1796 /* Index of the section header table in the shdr_info array. */
1799 /* Add the section header string table section name. */
1800 shdr_info
[cnt
].se
= dwelf_strtab_add_len (shst
, ".shstrtab", 10);
1801 shdr_info
[cnt
].idx
= idx
;
1803 /* Create the section header. */
1804 shdr_info
[cnt
].shdr
.sh_type
= SHT_STRTAB
;
1805 shdr_info
[cnt
].shdr
.sh_flags
= 0;
1806 shdr_info
[cnt
].shdr
.sh_addr
= 0;
1807 shdr_info
[cnt
].shdr
.sh_link
= SHN_UNDEF
;
1808 shdr_info
[cnt
].shdr
.sh_info
= SHN_UNDEF
;
1809 shdr_info
[cnt
].shdr
.sh_entsize
= 0;
1810 /* We set the offset to zero here. Before we write the ELF file the
1811 field must have the correct value. This is done in the final
1812 loop over all section. Then we have all the information needed. */
1813 shdr_info
[cnt
].shdr
.sh_offset
= 0;
1814 shdr_info
[cnt
].shdr
.sh_addralign
= 1;
1816 /* Create the section. */
1817 shdr_info
[cnt
].newscn
= elf_newscn (newelf
);
1818 if (shdr_info
[cnt
].newscn
== NULL
)
1821 error (EXIT_FAILURE
, 0,
1822 _("while create section header section: %s"),
1825 elf_assert (elf_ndxscn (shdr_info
[cnt
].newscn
) == idx
);
1827 /* Finalize the string table and fill in the correct indices in the
1829 shstrtab_data
= elf_newdata (shdr_info
[cnt
].newscn
);
1830 if (shstrtab_data
== NULL
)
1833 error (EXIT_FAILURE
, 0,
1834 _("while create section header string table: %s"),
1837 if (dwelf_strtab_finalize (shst
, shstrtab_data
) == NULL
)
1840 error (EXIT_FAILURE
, 0,
1841 _("no memory to create section header string table"));
1844 /* We have to set the section size. */
1845 shdr_info
[cnt
].shdr
.sh_size
= shstrtab_data
->d_size
;
1847 /* Update the section information. */
1848 GElf_Off lastoffset
= 0;
1849 for (cnt
= 1; cnt
<= shdridx
; ++cnt
)
1850 if (shdr_info
[cnt
].idx
> 0)
1854 scn
= elf_getscn (newelf
, shdr_info
[cnt
].idx
);
1855 elf_assert (scn
!= NULL
);
1857 /* Update the name. */
1858 shdr_info
[cnt
].shdr
.sh_name
= dwelf_strent_off (shdr_info
[cnt
].se
);
1860 /* Update the section header from the input file. Some fields
1861 might be section indices which now have to be adjusted. Keep
1862 the index to the "current" sh_link in case we need it to lookup
1863 symbol table names. */
1864 size_t sh_link
= shdr_info
[cnt
].shdr
.sh_link
;
1865 if (shdr_info
[cnt
].shdr
.sh_link
!= 0)
1866 shdr_info
[cnt
].shdr
.sh_link
=
1867 shdr_info
[shdr_info
[cnt
].shdr
.sh_link
].idx
;
1869 if (shdr_info
[cnt
].shdr
.sh_type
== SHT_GROUP
)
1871 elf_assert (shdr_info
[cnt
].data
!= NULL
1872 && shdr_info
[cnt
].data
->d_buf
!= NULL
);
1874 Elf32_Word
*grpref
= (Elf32_Word
*) shdr_info
[cnt
].data
->d_buf
;
1875 /* First word is the section group flag.
1876 Followed by section indexes, that need to be renumbered. */
1877 for (size_t inner
= 1;
1878 inner
< shdr_info
[cnt
].data
->d_size
/ sizeof (Elf32_Word
);
1880 if (grpref
[inner
] < shnum
)
1881 grpref
[inner
] = shdr_info
[grpref
[inner
]].idx
;
1886 /* Handle the SHT_REL, SHT_RELA, and SHF_INFO_LINK flag. */
1887 if (SH_INFO_LINK_P (&shdr_info
[cnt
].shdr
))
1888 shdr_info
[cnt
].shdr
.sh_info
=
1889 shdr_info
[shdr_info
[cnt
].shdr
.sh_info
].idx
;
1891 /* Get the data from the old file if necessary. We already
1892 created the data for the section header string table. */
1895 if (shdr_info
[cnt
].data
== NULL
)
1897 shdr_info
[cnt
].data
= elf_getdata (shdr_info
[cnt
].scn
, NULL
);
1898 if (shdr_info
[cnt
].data
== NULL
)
1899 INTERNAL_ERROR (fname
);
1902 /* Set the data. This is done by copying from the old file. */
1903 newdata
= elf_newdata (scn
);
1904 if (newdata
== NULL
)
1905 INTERNAL_ERROR (fname
);
1907 /* Copy the structure. */
1908 *newdata
= *shdr_info
[cnt
].data
;
1910 /* We know the size. */
1911 shdr_info
[cnt
].shdr
.sh_size
= shdr_info
[cnt
].data
->d_size
;
1913 /* We have to adjust symbol tables. The st_shndx member might
1914 have to be updated. */
1915 if (shdr_info
[cnt
].shdr
.sh_type
== SHT_DYNSYM
1916 || shdr_info
[cnt
].shdr
.sh_type
== SHT_SYMTAB
)
1918 Elf_Data
*versiondata
= NULL
;
1919 Elf_Data
*shndxdata
= NULL
;
1921 size_t elsize
= gelf_fsize (elf
, ELF_T_SYM
, 1, EV_CURRENT
);
1923 if (shdr_info
[cnt
].symtab_idx
!= 0)
1925 elf_assert (shdr_info
[cnt
].shdr
.sh_type
== SHT_SYMTAB_SHNDX
);
1926 /* This section has extended section information.
1927 We have to modify that information, too. */
1928 shndxdata
= elf_getdata (shdr_info
[shdr_info
[cnt
].symtab_idx
].scn
,
1931 elf_assert (shndxdata
!= NULL
1932 && shndxdata
->d_buf
!= NULL
1933 && ((shndxdata
->d_size
/ sizeof (Elf32_Word
))
1934 >= shdr_info
[cnt
].data
->d_size
/ elsize
));
1937 if (shdr_info
[cnt
].version_idx
!= 0)
1939 elf_assert (shdr_info
[cnt
].shdr
.sh_type
== SHT_DYNSYM
);
1940 /* This section has associated version
1941 information. We have to modify that
1942 information, too. */
1943 versiondata
= elf_getdata (shdr_info
[shdr_info
[cnt
].version_idx
].scn
,
1946 elf_assert (versiondata
!= NULL
1947 && versiondata
->d_buf
!= NULL
1948 && ((versiondata
->d_size
/ sizeof (GElf_Versym
))
1949 >= shdr_info
[cnt
].data
->d_size
/ elsize
));
1952 shdr_info
[cnt
].newsymidx
1953 = (Elf32_Word
*) xcalloc (shdr_info
[cnt
].data
->d_size
1954 / elsize
, sizeof (Elf32_Word
));
1956 bool last_was_local
= true;
1959 for (destidx
= inner
= 1;
1960 inner
< shdr_info
[cnt
].data
->d_size
/ elsize
;
1966 GElf_Sym
*sym
= gelf_getsymshndx (shdr_info
[cnt
].data
,
1970 INTERNAL_ERROR (fname
);
1972 if (sym
->st_shndx
== SHN_UNDEF
1973 || (sym
->st_shndx
>= SHN_LORESERVE
1974 && sym
->st_shndx
!= SHN_XINDEX
))
1976 /* This is no section index, leave it alone
1977 unless it is moved. */
1978 if (destidx
!= inner
1979 && gelf_update_symshndx (shdr_info
[cnt
].data
,
1983 INTERNAL_ERROR (fname
);
1985 shdr_info
[cnt
].newsymidx
[inner
] = destidx
++;
1988 && GELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
1990 last_was_local
= false;
1991 shdr_info
[cnt
].shdr
.sh_info
= destidx
- 1;
1997 /* Get the full section index, if necessary from the
1999 if (sym
->st_shndx
== SHN_XINDEX
)
2000 elf_assert (shndxdata
!= NULL
2001 && shndxdata
->d_buf
!= NULL
);
2002 size_t sidx
= (sym
->st_shndx
!= SHN_XINDEX
2003 ? sym
->st_shndx
: xshndx
);
2004 elf_assert (sidx
< shnum
);
2005 sec
= shdr_info
[sidx
].idx
;
2009 GElf_Section nshndx
;
2012 if (sec
< SHN_LORESERVE
)
2019 nshndx
= SHN_XINDEX
;
2023 elf_assert (sec
< SHN_LORESERVE
|| shndxdata
!= NULL
);
2025 if ((inner
!= destidx
|| nshndx
!= sym
->st_shndx
2026 || (shndxdata
!= NULL
&& nxshndx
!= xshndx
))
2027 && (sym
->st_shndx
= nshndx
,
2028 gelf_update_symshndx (shdr_info
[cnt
].data
,
2032 INTERNAL_ERROR (fname
);
2034 shdr_info
[cnt
].newsymidx
[inner
] = destidx
++;
2037 && GELF_ST_BIND (sym
->st_info
) != STB_LOCAL
)
2039 last_was_local
= false;
2040 shdr_info
[cnt
].shdr
.sh_info
= destidx
- 1;
2043 else if ((shdr_info
[cnt
].shdr
.sh_flags
& SHF_ALLOC
) != 0
2044 && GELF_ST_TYPE (sym
->st_info
) != STT_SECTION
2045 && shdr_info
[sidx
].shdr
.sh_type
!= SHT_GROUP
)
2047 /* Removing a real symbol from an allocated
2048 symbol table is hard and probably a
2049 mistake. Really removing it means
2050 rewriting the dynamic segment and hash
2051 sections. Just warn and set the symbol
2052 section to UNDEF. */
2054 _("Cannot remove symbol [%zd] from allocated symbol table [%zd]"), inner
, cnt
);
2055 sym
->st_shndx
= SHN_UNDEF
;
2056 if (gelf_update_sym (shdr_info
[cnt
].data
, destidx
,
2058 INTERNAL_ERROR (fname
);
2059 shdr_info
[cnt
].newsymidx
[inner
] = destidx
++;
2061 else if (debug_fname
!= NULL
2062 && shdr_info
[cnt
].debug_data
== NULL
)
2063 /* The symbol points to a section that is discarded
2064 but isn't preserved in the debug file. Check that
2065 this is a section or group signature symbol
2066 for a section which has been removed. Or a special
2067 data marker symbol to a debug section. */
2069 elf_assert (GELF_ST_TYPE (sym
->st_info
) == STT_SECTION
2070 || ((shdr_info
[sidx
].shdr
.sh_type
2072 && (shdr_info
[sidx
].shdr
.sh_info
2074 || ebl_data_marker_symbol (ebl
, sym
,
2075 elf_strptr (elf
, sh_link
,
2080 if (destidx
!= inner
)
2082 /* The size of the symbol table changed. */
2083 shdr_info
[cnt
].shdr
.sh_size
= newdata
->d_size
2085 any_symtab_changes
= true;
2089 /* The symbol table didn't really change. */
2090 free (shdr_info
[cnt
].newsymidx
);
2091 shdr_info
[cnt
].newsymidx
= NULL
;
2096 /* If we have to, compute the offset of the section.
2097 If allocate and unallocated sections are mixed, we only update
2098 the allocated ones now. The unallocated ones come second. */
2099 if (! mixed_allocated_unallocated
2100 || (shdr_info
[cnt
].shdr
.sh_flags
& SHF_ALLOC
) != 0)
2102 if (shdr_info
[cnt
].shdr
.sh_offset
== 0)
2103 shdr_info
[cnt
].shdr
.sh_offset
2104 = ((lastoffset
+ shdr_info
[cnt
].shdr
.sh_addralign
- 1)
2105 & ~((GElf_Off
) (shdr_info
[cnt
].shdr
.sh_addralign
- 1)));
2107 /* Set the section header in the new file. */
2108 if (unlikely (gelf_update_shdr (scn
, &shdr_info
[cnt
].shdr
) == 0))
2109 /* There cannot be any overflows. */
2110 INTERNAL_ERROR (fname
);
2112 /* Remember the last section written so far. */
2113 GElf_Off filesz
= (shdr_info
[cnt
].shdr
.sh_type
!= SHT_NOBITS
2114 ? shdr_info
[cnt
].shdr
.sh_size
: 0);
2115 if (lastoffset
< shdr_info
[cnt
].shdr
.sh_offset
+ filesz
)
2116 lastoffset
= shdr_info
[cnt
].shdr
.sh_offset
+ filesz
;
2120 /* We might have to update the unallocated sections after we done the
2121 allocated ones. lastoffset is set to right after the last allocated
2123 if (mixed_allocated_unallocated
)
2124 for (cnt
= 1; cnt
<= shdridx
; ++cnt
)
2125 if (shdr_info
[cnt
].idx
> 0)
2127 scn
= elf_getscn (newelf
, shdr_info
[cnt
].idx
);
2128 if ((shdr_info
[cnt
].shdr
.sh_flags
& SHF_ALLOC
) == 0)
2130 if (shdr_info
[cnt
].shdr
.sh_offset
== 0)
2131 shdr_info
[cnt
].shdr
.sh_offset
2132 = ((lastoffset
+ shdr_info
[cnt
].shdr
.sh_addralign
- 1)
2133 & ~((GElf_Off
) (shdr_info
[cnt
].shdr
.sh_addralign
- 1)));
2135 /* Set the section header in the new file. */
2136 if (unlikely (gelf_update_shdr (scn
, &shdr_info
[cnt
].shdr
) == 0))
2137 /* There cannot be any overflows. */
2138 INTERNAL_ERROR (fname
);
2140 /* Remember the last section written so far. */
2141 GElf_Off filesz
= (shdr_info
[cnt
].shdr
.sh_type
!= SHT_NOBITS
2142 ? shdr_info
[cnt
].shdr
.sh_size
: 0);
2143 if (lastoffset
< shdr_info
[cnt
].shdr
.sh_offset
+ filesz
)
2144 lastoffset
= shdr_info
[cnt
].shdr
.sh_offset
+ filesz
;
2148 /* Adjust symbol references if symbol tables changed. */
2149 if (any_symtab_changes
)
2150 /* Find all relocation sections which use this symbol table. */
2151 for (cnt
= 1; cnt
<= shdridx
; ++cnt
)
2153 /* Update section headers when the data size has changed.
2154 We also update the SHT_NOBITS section in the debug
2155 file so that the section headers match in sh_size. */
2156 inline void update_section_size (const Elf_Data
*newdata
)
2159 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &shdr_mem
);
2160 shdr
->sh_size
= newdata
->d_size
;
2161 (void) gelf_update_shdr (scn
, shdr
);
2162 if (debugelf
!= NULL
)
2164 /* libelf will use d_size to set sh_size. */
2165 Elf_Data
*debugdata
= elf_getdata (elf_getscn (debugelf
,
2167 if (debugdata
== NULL
)
2168 INTERNAL_ERROR (fname
);
2169 debugdata
->d_size
= newdata
->d_size
;
2173 if (shdr_info
[cnt
].idx
== 0 && debug_fname
== NULL
)
2174 /* Ignore sections which are discarded. When we are saving a
2175 relocation section in a separate debug file, we must fix up
2176 the symbol table references. */
2179 const Elf32_Word symtabidx
= shdr_info
[cnt
].old_sh_link
;
2180 elf_assert (symtabidx
< shnum
+ 2);
2181 const Elf32_Word
*const newsymidx
= shdr_info
[symtabidx
].newsymidx
;
2182 switch (shdr_info
[cnt
].shdr
.sh_type
)
2184 inline bool no_symtab_updates (void)
2186 /* If the symbol table hasn't changed, do not do anything. */
2187 if (shdr_info
[symtabidx
].newsymidx
== NULL
)
2190 /* If the symbol table is not discarded, but additionally
2191 duplicated in the separate debug file and this section
2192 is discarded, don't adjust anything. */
2193 return (shdr_info
[cnt
].idx
== 0
2194 && shdr_info
[symtabidx
].debug_data
!= NULL
);
2199 if (no_symtab_updates ())
2202 Elf_Data
*d
= elf_getdata (shdr_info
[cnt
].idx
== 0
2203 ? elf_getscn (debugelf
, cnt
)
2204 : elf_getscn (newelf
,
2205 shdr_info
[cnt
].idx
),
2207 elf_assert (d
!= NULL
&& d
->d_buf
!= NULL
2208 && shdr_info
[cnt
].shdr
.sh_entsize
!= 0);
2209 size_t nrels
= (shdr_info
[cnt
].shdr
.sh_size
2210 / shdr_info
[cnt
].shdr
.sh_entsize
);
2212 size_t symsize
= gelf_fsize (elf
, ELF_T_SYM
, 1, EV_CURRENT
);
2213 const Elf32_Word symidxn
= (shdr_info
[symtabidx
].data
->d_size
2215 if (shdr_info
[cnt
].shdr
.sh_type
== SHT_REL
)
2216 for (size_t relidx
= 0; relidx
< nrels
; ++relidx
)
2219 if (gelf_getrel (d
, relidx
, &rel_mem
) == NULL
)
2220 INTERNAL_ERROR (fname
);
2222 size_t symidx
= GELF_R_SYM (rel_mem
.r_info
);
2223 elf_assert (symidx
< symidxn
);
2224 if (newsymidx
[symidx
] != symidx
)
2227 = GELF_R_INFO (newsymidx
[symidx
],
2228 GELF_R_TYPE (rel_mem
.r_info
));
2230 if (gelf_update_rel (d
, relidx
, &rel_mem
) == 0)
2231 INTERNAL_ERROR (fname
);
2235 for (size_t relidx
= 0; relidx
< nrels
; ++relidx
)
2238 if (gelf_getrela (d
, relidx
, &rel_mem
) == NULL
)
2239 INTERNAL_ERROR (fname
);
2241 size_t symidx
= GELF_R_SYM (rel_mem
.r_info
);
2242 elf_assert (symidx
< symidxn
);
2243 if (newsymidx
[symidx
] != symidx
)
2246 = GELF_R_INFO (newsymidx
[symidx
],
2247 GELF_R_TYPE (rel_mem
.r_info
));
2249 if (gelf_update_rela (d
, relidx
, &rel_mem
) == 0)
2250 INTERNAL_ERROR (fname
);
2256 if (no_symtab_updates ())
2259 /* We have to recompute the hash table. */
2261 elf_assert (shdr_info
[cnt
].idx
> 0);
2263 /* The hash section in the new file. */
2264 scn
= elf_getscn (newelf
, shdr_info
[cnt
].idx
);
2266 /* The symbol table data. */
2267 Elf_Data
*symd
= elf_getdata (elf_getscn (newelf
,
2268 shdr_info
[symtabidx
].idx
),
2270 elf_assert (symd
!= NULL
&& symd
->d_buf
!= NULL
);
2272 /* The hash table data. */
2273 Elf_Data
*hashd
= elf_getdata (scn
, NULL
);
2274 elf_assert (hashd
!= NULL
&& hashd
->d_buf
!= NULL
);
2276 if (shdr_info
[cnt
].shdr
.sh_entsize
== sizeof (Elf32_Word
))
2278 /* Sane arches first. */
2279 elf_assert (hashd
->d_size
>= 2 * sizeof (Elf32_Word
));
2280 Elf32_Word
*bucket
= (Elf32_Word
*) hashd
->d_buf
;
2282 size_t strshndx
= shdr_info
[symtabidx
].old_sh_link
;
2283 size_t elsize
= gelf_fsize (elf
, ELF_T_SYM
, 1, EV_CURRENT
);
2285 Elf32_Word nchain
= bucket
[1];
2286 Elf32_Word nbucket
= bucket
[0];
2287 uint64_t used_buf
= ((2ULL + nchain
+ nbucket
)
2288 * sizeof (Elf32_Word
));
2289 elf_assert (used_buf
<= hashd
->d_size
);
2291 /* Adjust the nchain value. The symbol table size
2292 changed. We keep the same size for the bucket array. */
2293 bucket
[1] = symd
->d_size
/ elsize
;
2295 Elf32_Word
*chain
= bucket
+ nbucket
;
2297 /* New size of the section. */
2298 size_t n_size
= ((2 + symd
->d_size
/ elsize
+ nbucket
)
2299 * sizeof (Elf32_Word
));
2300 elf_assert (n_size
<= hashd
->d_size
);
2301 hashd
->d_size
= n_size
;
2302 update_section_size (hashd
);
2304 /* Clear the arrays. */
2305 memset (bucket
, '\0',
2306 (symd
->d_size
/ elsize
+ nbucket
)
2307 * sizeof (Elf32_Word
));
2309 for (size_t inner
= shdr_info
[symtabidx
].shdr
.sh_info
;
2310 inner
< symd
->d_size
/ elsize
; ++inner
)
2313 GElf_Sym
*sym
= gelf_getsym (symd
, inner
, &sym_mem
);
2314 elf_assert (sym
!= NULL
);
2316 const char *name
= elf_strptr (elf
, strshndx
,
2318 elf_assert (name
!= NULL
&& nbucket
!= 0);
2319 size_t hidx
= elf_hash (name
) % nbucket
;
2321 if (bucket
[hidx
] == 0)
2322 bucket
[hidx
] = inner
;
2325 hidx
= bucket
[hidx
];
2327 while (chain
[hidx
] != 0 && chain
[hidx
] < nchain
)
2330 chain
[hidx
] = inner
;
2336 /* Alpha and S390 64-bit use 64-bit SHT_HASH entries. */
2337 elf_assert (shdr_info
[cnt
].shdr
.sh_entsize
2338 == sizeof (Elf64_Xword
));
2340 Elf64_Xword
*bucket
= (Elf64_Xword
*) hashd
->d_buf
;
2342 size_t strshndx
= shdr_info
[symtabidx
].old_sh_link
;
2343 size_t elsize
= gelf_fsize (elf
, ELF_T_SYM
, 1, EV_CURRENT
);
2345 elf_assert (symd
->d_size
>= 2 * sizeof (Elf64_Xword
));
2346 Elf64_Xword nbucket
= bucket
[0];
2347 Elf64_Xword nchain
= bucket
[1];
2348 uint64_t maxwords
= hashd
->d_size
/ sizeof (Elf64_Xword
);
2349 elf_assert (maxwords
>= 2
2350 && maxwords
- 2 >= nbucket
2351 && maxwords
- 2 - nbucket
>= nchain
);
2353 /* Adjust the nchain value. The symbol table size
2354 changed. We keep the same size for the bucket array. */
2355 bucket
[1] = symd
->d_size
/ elsize
;
2357 Elf64_Xword
*chain
= bucket
+ nbucket
;
2359 /* New size of the section. */
2360 size_t n_size
= ((2 + symd
->d_size
/ elsize
+ nbucket
)
2361 * sizeof (Elf64_Xword
));
2362 elf_assert (n_size
<= hashd
->d_size
);
2363 hashd
->d_size
= n_size
;
2364 update_section_size (hashd
);
2366 /* Clear the arrays. */
2367 memset (bucket
, '\0',
2368 (symd
->d_size
/ elsize
+ nbucket
)
2369 * sizeof (Elf64_Xword
));
2371 for (size_t inner
= shdr_info
[symtabidx
].shdr
.sh_info
;
2372 inner
< symd
->d_size
/ elsize
; ++inner
)
2375 GElf_Sym
*sym
= gelf_getsym (symd
, inner
, &sym_mem
);
2376 elf_assert (sym
!= NULL
);
2378 const char *name
= elf_strptr (elf
, strshndx
,
2380 elf_assert (name
!= NULL
&& nbucket
!= 0);
2381 size_t hidx
= elf_hash (name
) % nbucket
;
2383 if (bucket
[hidx
] == 0)
2384 bucket
[hidx
] = inner
;
2387 hidx
= bucket
[hidx
];
2389 while (chain
[hidx
] != 0 && chain
[hidx
] < nchain
)
2392 chain
[hidx
] = inner
;
2398 case SHT_GNU_versym
:
2399 /* If the symbol table changed we have to adjust the entries. */
2400 if (no_symtab_updates ())
2403 elf_assert (shdr_info
[cnt
].idx
> 0);
2405 /* The symbol version section in the new file. */
2406 scn
= elf_getscn (newelf
, shdr_info
[cnt
].idx
);
2408 /* The symbol table data. */
2409 symd
= elf_getdata (elf_getscn (newelf
, shdr_info
[symtabidx
].idx
),
2411 elf_assert (symd
!= NULL
&& symd
->d_buf
!= NULL
);
2412 size_t symz
= gelf_fsize (elf
, ELF_T_SYM
, 1, EV_CURRENT
);
2413 const Elf32_Word syms
= (shdr_info
[symtabidx
].data
->d_size
/ symz
);
2415 /* The version symbol data. */
2416 Elf_Data
*verd
= elf_getdata (scn
, NULL
);
2417 elf_assert (verd
!= NULL
&& verd
->d_buf
!= NULL
);
2419 /* The symbol version array. */
2420 GElf_Half
*verstab
= (GElf_Half
*) verd
->d_buf
;
2422 /* Walk through the list and */
2423 size_t elsize
= gelf_fsize (elf
, verd
->d_type
, 1, EV_CURRENT
);
2424 Elf32_Word vers
= verd
->d_size
/ elsize
;
2425 for (size_t inner
= 1; inner
< vers
&& inner
< syms
; ++inner
)
2426 if (newsymidx
[inner
] != 0 && newsymidx
[inner
] < vers
)
2427 /* Overwriting the same array works since the
2428 reordering can only move entries to lower indices
2430 verstab
[newsymidx
[inner
]] = verstab
[inner
];
2432 /* New size of the section. */
2433 verd
->d_size
= gelf_fsize (newelf
, verd
->d_type
,
2435 / gelf_fsize (elf
, symd
->d_type
, 1,
2438 update_section_size (verd
);
2442 if (no_symtab_updates ())
2445 /* Yes, the symbol table changed.
2446 Update the section header of the section group. */
2447 scn
= elf_getscn (newelf
, shdr_info
[cnt
].idx
);
2449 GElf_Shdr
*shdr
= gelf_getshdr (scn
, &shdr_mem
);
2450 elf_assert (shdr
!= NULL
);
2452 size_t symsz
= gelf_fsize (elf
, ELF_T_SYM
, 1, EV_CURRENT
);
2453 const Elf32_Word symn
= (shdr_info
[symtabidx
].data
->d_size
2455 elf_assert (shdr
->sh_info
< symn
);
2456 shdr
->sh_info
= newsymidx
[shdr
->sh_info
];
2458 (void) gelf_update_shdr (scn
, shdr
);
2463 /* Remove any relocations between debug sections in ET_REL
2464 for the debug file when requested. These relocations are always
2465 zero based between the unallocated sections. */
2466 if (debug_fname
!= NULL
&& removing_sections
2467 && reloc_debug
&& ehdr
->e_type
== ET_REL
)
2468 remove_debug_relocations (ebl
, debugelf
, ehdr
, fname
, shstrndx
);
2470 /* Now that we have done all adjustments to the data,
2471 we can actually write out the debug file. */
2472 if (debug_fname
!= NULL
&& removing_sections
)
2474 /* Finally write the file. */
2475 if (unlikely (elf_update (debugelf
, ELF_C_WRITE
) == -1))
2477 error (0, 0, _("while writing '%s': %s"),
2478 tmp_debug_fname
, elf_errmsg (-1));
2483 /* Create the real output file. First rename, then change the
2485 if (rename (tmp_debug_fname
, debug_fname
) != 0
2486 || fchmod (debug_fd
, mode
) != 0)
2488 error (0, errno
, _("while creating '%s'"), debug_fname
);
2493 /* The temporary file does not exist anymore. */
2494 free (tmp_debug_fname
);
2495 tmp_debug_fname
= NULL
;
2500 Elf_Data debug_crc_data
=
2502 .d_type
= ELF_T_WORD
,
2503 .d_buf
= &debug_crc
,
2504 .d_size
= sizeof (debug_crc
),
2505 .d_version
= EV_CURRENT
2508 /* Compute the checksum which we will add to the executable. */
2509 if (crc32_file (debug_fd
, &debug_crc
) != 0)
2511 error (0, errno
, _("\
2512 while computing checksum for debug information"));
2513 unlink (debug_fname
);
2518 /* Store it in the debuglink section data. */
2519 if (unlikely (gelf_xlatetof (newelf
, &debuglink_crc_data
,
2520 &debug_crc_data
, ehdr
->e_ident
[EI_DATA
])
2521 != &debuglink_crc_data
))
2522 INTERNAL_ERROR (fname
);
2526 lastsec_offset
= shdr_info
[shdridx
].shdr
.sh_offset
;
2527 lastsec_size
= shdr_info
[shdridx
].shdr
.sh_size
;
2530 /* Finally finish the ELF header. Fill in the fields not handled by
2531 libelf from the old file. */
2532 newehdr
= gelf_getehdr (newelf
, &newehdr_mem
);
2533 if (newehdr
== NULL
)
2534 INTERNAL_ERROR (fname
);
2536 memcpy (newehdr
->e_ident
, ehdr
->e_ident
, EI_NIDENT
);
2537 newehdr
->e_type
= ehdr
->e_type
;
2538 newehdr
->e_machine
= ehdr
->e_machine
;
2539 newehdr
->e_version
= ehdr
->e_version
;
2540 newehdr
->e_entry
= ehdr
->e_entry
;
2541 newehdr
->e_flags
= ehdr
->e_flags
;
2542 newehdr
->e_phoff
= ehdr
->e_phoff
;
2544 /* We need to position the section header table. */
2545 const size_t offsize
= gelf_fsize (elf
, ELF_T_OFF
, 1, EV_CURRENT
);
2546 newehdr
->e_shoff
= ((lastsec_offset
+ lastsec_size
+ offsize
- 1)
2547 & ~((GElf_Off
) (offsize
- 1)));
2548 newehdr
->e_shentsize
= gelf_fsize (elf
, ELF_T_SHDR
, 1, EV_CURRENT
);
2550 if (gelf_update_ehdr (newelf
, newehdr
) == 0)
2552 error (0, 0, _("%s: error while creating ELF header: %s"),
2553 output_fname
?: fname
, elf_errmsg (-1));
2558 /* The new section header string table index. */
2559 if (update_shdrstrndx (newelf
, idx
) != 0)
2561 error (0, 0, _("%s: error updating shdrstrndx: %s"),
2562 output_fname
?: fname
, elf_errmsg (-1));
2567 /* We have everything from the old file. */
2568 if (elf_cntl (elf
, ELF_C_FDDONE
) != 0)
2570 error (0, 0, _("%s: error while reading the file: %s"),
2571 fname
, elf_errmsg (-1));
2576 /* The ELF library better follows our layout when this is not a
2577 relocatable object file. */
2578 elf_flagelf (newelf
, ELF_C_SET
,
2579 (phnum
> 0 ? ELF_F_LAYOUT
: 0)
2580 | (permissive
? ELF_F_PERMISSIVE
: 0));
2582 /* Finally write the file. */
2583 if (elf_update (newelf
, ELF_C_WRITE
) == -1)
2585 error (0, 0, _("while writing '%s': %s"),
2586 output_fname
?: fname
, elf_errmsg (-1));
2592 /* libelf can't cope without the section headers being properly intact.
2593 So we just let it write them normally, and then we nuke them later. */
2595 if (newehdr
->e_ident
[EI_CLASS
] == ELFCLASS32
)
2597 assert (offsetof (Elf32_Ehdr
, e_shentsize
) + sizeof (Elf32_Half
)
2598 == offsetof (Elf32_Ehdr
, e_shnum
));
2599 assert (offsetof (Elf32_Ehdr
, e_shnum
) + sizeof (Elf32_Half
)
2600 == offsetof (Elf32_Ehdr
, e_shstrndx
));
2601 const Elf32_Off zero_off
= 0;
2602 const Elf32_Half zero
[3] = { 0, 0, SHN_UNDEF
};
2603 if (pwrite_retry (fd
, &zero_off
, sizeof zero_off
,
2604 offsetof (Elf32_Ehdr
, e_shoff
)) != sizeof zero_off
2605 || (pwrite_retry (fd
, zero
, sizeof zero
,
2606 offsetof (Elf32_Ehdr
, e_shentsize
))
2608 || ftruncate (fd
, lastsec_offset
) < 0)
2610 error (0, errno
, _("while writing '%s'"),
2611 output_fname
?: fname
);
2617 assert (offsetof (Elf64_Ehdr
, e_shentsize
) + sizeof (Elf64_Half
)
2618 == offsetof (Elf64_Ehdr
, e_shnum
));
2619 assert (offsetof (Elf64_Ehdr
, e_shnum
) + sizeof (Elf64_Half
)
2620 == offsetof (Elf64_Ehdr
, e_shstrndx
));
2621 const Elf64_Off zero_off
= 0;
2622 const Elf64_Half zero
[3] = { 0, 0, SHN_UNDEF
};
2623 if (pwrite_retry (fd
, &zero_off
, sizeof zero_off
,
2624 offsetof (Elf64_Ehdr
, e_shoff
)) != sizeof zero_off
2625 || (pwrite_retry (fd
, zero
, sizeof zero
,
2626 offsetof (Elf64_Ehdr
, e_shentsize
))
2628 || ftruncate (fd
, lastsec_offset
) < 0)
2630 error (0, errno
, _("while writing '%s'"),
2631 output_fname
?: fname
);
2638 if (shdr_info
!= NULL
)
2640 /* For some sections we might have created an table to map symbol
2641 table indices. Or we might kept (original) data around to put
2642 into the .debug file. */
2643 for (cnt
= 1; cnt
<= shdridx
; ++cnt
)
2645 free (shdr_info
[cnt
].newsymidx
);
2646 if (shdr_info
[cnt
].debug_data
!= NULL
)
2647 free (shdr_info
[cnt
].debug_data
->d_buf
);
2650 /* Free data we allocated for the .gnu_debuglink section. */
2651 free (debuglink_buf
);
2653 /* Free the memory. */
2654 if ((shnum
+ 2) * sizeof (struct shdr_info
) > MAX_STACK_ALLOC
)
2658 /* Free other resources. */
2659 if (shstrtab_data
!= NULL
)
2660 free (shstrtab_data
->d_buf
);
2662 dwelf_strtab_free (shst
);
2664 /* That was it. Close the descriptors. */
2665 if (elf_end (newelf
) != 0)
2667 error (0, 0, _("error while finishing '%s': %s"),
2668 output_fname
?: fname
, elf_errmsg (-1));
2672 if (debugelf
!= NULL
&& elf_end (debugelf
) != 0)
2674 error (0, 0, _("error while finishing '%s': %s"), debug_fname
,
2680 /* Close the EBL backend. */
2682 ebl_closebackend (ebl
);
2686 /* If requested, preserve the timestamp. */
2689 if (futimens (fd
, tvp
) != 0)
2691 error (0, errno
, _("\
2692 cannot set access and modification date of '%s'"),
2693 output_fname
?: fname
);
2698 /* Close the file descriptor if we created a new file. */
2699 if (output_fname
!= NULL
)
2703 unlink (output_fname
);
2710 cleanup_debug (void)
2714 if (tmp_debug_fname
!= NULL
)
2716 unlink (tmp_debug_fname
);
2717 free (tmp_debug_fname
);
2718 tmp_debug_fname
= NULL
;
2726 handle_ar (int fd
, Elf
*elf
, const char *prefix
, const char *fname
,
2727 struct timespec tvp
[2])
2729 size_t prefix_len
= prefix
== NULL
? 0 : strlen (prefix
);
2730 size_t fname_len
= strlen (fname
) + 1;
2731 char new_prefix
[prefix_len
+ 1 + fname_len
];
2732 char *cp
= new_prefix
;
2734 /* Create the full name of the file. */
2737 cp
= mempcpy (cp
, prefix
, prefix_len
);
2740 memcpy (cp
, fname
, fname_len
);
2743 /* Process all the files contained in the archive. */
2745 Elf_Cmd cmd
= ELF_C_RDWR
;
2747 while ((subelf
= elf_begin (fd
, cmd
, elf
)) != NULL
)
2749 /* The the header for this element. */
2750 Elf_Arhdr
*arhdr
= elf_getarhdr (subelf
);
2752 if (elf_kind (subelf
) == ELF_K_ELF
)
2753 result
|= handle_elf (fd
, subelf
, new_prefix
, arhdr
->ar_name
, 0, NULL
);
2754 else if (elf_kind (subelf
) == ELF_K_AR
)
2755 result
|= handle_ar (fd
, subelf
, new_prefix
, arhdr
->ar_name
, NULL
);
2757 /* Get next archive element. */
2758 cmd
= elf_next (subelf
);
2759 if (unlikely (elf_end (subelf
) != 0))
2760 INTERNAL_ERROR (fname
);
2765 if (unlikely (futimens (fd
, tvp
) != 0))
2767 error (0, errno
, _("\
2768 cannot set access and modification date of '%s'"), fname
);
2773 if (unlikely (close (fd
) != 0))
2774 error (EXIT_FAILURE
, errno
, _("while closing '%s'"), fname
);
2780 #include "debugpred.h"