summaryrefslogtreecommitdiffstats
path: root/src/strip.c
Commit message (Collapse)AuthorAgeFilesLines
* Move the #include <libintl.h> into eu-config.hYonggang Luo2022-10-161-1/+0
| | | | | | | | | | So we do not need include in each file. And indeed the macro #define _(Str) dgettext ("elfutils", Str) access libintl function dgettext, so it's make more sense #include <libintl.h> in file eu-config.h Signed-off-by: Yonggang Luo <[email protected]>
* strip: Add more NULL checkMark Wielaard2022-05-141-2/+10
| | | | | | | | When gelf_getshdr, gelf_getrela, gelf_getrel or gelf_getsymshndx return NULL it is an internal error which we want to report instead of crashing. Signed-off-by: Mark Wielaard <[email protected]>
* Introduce error_exit as a noreturn variant of error (EXIT_FAILURE, ...)Mark Wielaard2022-03-301-33/+25
| | | | | | | | | | | | error (EXIT_FAILURE, ...) should be noreturn but on some systems it isn't. This may cause warnings about code that should not be reachable. So have an explicit error_exit wrapper that is noreturn (because it calls exit explicitly). Use error_exit in all tools under the src directory. https://bugzilla.redhat.com/show_bug.cgi?id=2068692 Signed-off-by: Mark Wielaard <[email protected]>
* Remove redundant casts of memory allocating functions returning void *Dmitry V. Levin2021-09-091-5/+4
| | | | | | | Return values of functions returning "void *", e.g. calloc, malloc, realloc, xcalloc, xmalloc, and xrealloc, do not need explicit casts. Signed-off-by: Dmitry V. Levin <[email protected]>
* strip: Always check gelf_update results.Mark Wielaard2021-07-031-4/+9
| | | | Signed-off-by: Mark Wielaard <[email protected]>
* Come up with startswith function.Martin Liska2021-05-121-1/+1
| | | | | | | New function in system.h that returns true if a string has a given prefix, false otherwise. Use it in place of strncmp. Signed-off-by: Martin Liška <[email protected]>
* strip: Remove no_symtab_updates() functionTimm Bäder2021-01-281-45/+29
| | | | | | | | | The no_symtab_updates() function was being called at the beginning of all case labels in this switch, so we can just call it once before the switch. Then it only has one call-site, so inline this short function there. Signed-off-by: Timm Bäder <[email protected]>
* strip: Pull update_section_size() into file scopeTimm Bäder2021-01-281-23/+28
| | | | | | Get rid of a nested function this way. Signed-off-by: Timm Bäder <[email protected]>
* strip: Pull relocate() info file scopeTimm Bäder2021-01-281-163/+184
| | | | | | | | Pull relocate() info file scope and get rid of a nested function this way. Refactor remove_debug_relocations() to minimize the parameters we need to pass to relocate(). Signed-off-by: Timm Bäder <[email protected]>
* strip: Replace nested check_preserved function with loopTimm Bäder2021-01-281-16/+21
| | | | Signed-off-by: Timm Bäder <[email protected]>
* src: consistently use _(Str) instead of gettext(Str)Dmitry V. Levin2020-12-161-54/+54
| | | | | | | | | Make use of the _(Str) macro provided by <config.h>. The change was made automatically using the following command: $ git grep -l '\<gettext *(' src |xargs sed -i 's/\<gettext *(/_(/g' Signed-off-by: Dmitry V. Levin <[email protected]>
* src: fix spelling typos in comments and ChangeLogDmitry V. Levin2020-12-121-1/+1
| | | | | | | | | | | | | | | | | | Indeces -> Indices adress -> address affort -> afford dont' -> don't futher -> further higest -> highest indeces -> indices interate -> iterate occured -> occurred overlow -> overflow sectin -> section succesful -> successful teminated -> terminated Signed-off-by: Dmitry V. Levin <[email protected]>
* src: fix spelling typos in argp help text and error diagnosticsDmitry V. Levin2020-12-121-2/+2
| | | | | | | | | | | | Since all these help text strings are marked for translation, apply the fixes to translation strings as well, this helps to avoid translations becoming fuzzy. lenght -> length occured -> occurred endianess -> endianness reversable -> reversible Signed-off-by: Dmitry V. Levin <[email protected]>
* strip don't mmap debug output file.Mark Wielaard2019-06-241-1/+1
| | | | | | | | | | | | | | | | | Using ELF_C_WRITE_MMAP sometimes causes unexpected errors when disk space is low. When writing out the file, the output file is first extended so that it covers the whole file/mmap size. But it might be that the file system allowed the extension as a sparse file. In that case writing to the file through the mmap might still fail and produce a SIGBUS if the disk is full. This is confusing to the user. Using ELF_C_WRITE will produce "normal" errors when the file cannot be written out. It also seems to use less memory because the debug file is created from scratch. So the memory is first read into the ELF data structure buffers, then written out as a whole. In this case the mmap output buffer is just overhead. Signed-off-by: Mark Wielaard <[email protected]>
* strip: Files with symbols referring to non-existing sections are illformedMark Wielaard2019-03-271-0/+1
| | | | | | | | | | The check added in commit 4540ea98c "strip: Fix check test for SHN_XINDEX symbol" was not complete. The (extended) section index should also exist. If it doesn't exist, mark the file as illformed. https://sourceware.org/bugzilla/show_bug.cgi?id=24385 Signed-off-by: Mark Wielaard <[email protected]>
* strip: Fix check test for SHN_XINDEX symbol.Mark Wielaard2019-01-241-1/+1
| | | | | | | | | | | | | The check for whether a symbol used the extended section table was wrong causing the run-strip-test-many.sh testcase to declare the testfile was an illformed file. Fixing this exposed a strict elfutils check for the '.shstrtab' section having this exact name and a SHT_STRTAB type. This might be a little too strict, but easily worked around by changing the name of the "old" shstrtab section in the addsections program. Signed-off-by: Mark Wielaard <[email protected]>
* strip: Also handle gnu compressed debug sections with --reloc-debug-sectionsMark Wielaard2018-11-131-6/+23
| | | | | | | | | | | | | Check whether a section was gnu compressed and decompress it first before trying to resolve relocations. Recompress it afterwards. This found a bug in elf_compress_gnu which would use the "raw" file contents even if the user had just created the section (copying over the section from the original input file). Add compressed ET_REL tests to run-strip-reloc.sh testcase. Signed-off-by: Mark Wielaard <[email protected]>
* strip: Add --reloc-debug-sections-only option.Mark Wielaard2018-11-061-6/+149
| | | | | | | | | | | This option does the same thing as --reloc-debug-sections without doing any other strip operation. This is useful when you want to remove the debug section relocations in a separate ET_REL debug file that was created without --reloc-debug-sections, or for a file (like the linux debug vmlinux) that you don't want to strip, but for which the debug section relocations can be resolved already. Signed-off-by: Mark Wielaard <[email protected]>
* strip: Extract code to update shdrstrndx into new common function.Mark Wielaard2018-11-061-45/+49
| | | | Signed-off-by: Mark Wielaard <[email protected]>
* strip: Split out debug section relocation into separate helper functions.Mark Wielaard2018-11-061-249/+292
| | | | | | | | Extract a couple of helper functions out of handle_elf (secndx_name, get_xndxdata and remove_debug_relocations) so they can be reused more easily in the future. Signed-off-by: Mark Wielaard <[email protected]>
* strip: Always copy over any phdrs if there are any.Mark Wielaard2018-11-061-28/+42
| | | | | | | | Ignore the type of ELF file, just copy over any phdrs if the original file contained any. Also refuse to move around any allocated sections based on whether there are any phdrs instead of on ELF file type. Signed-off-by: Mark Wielaard <[email protected]>
* strip, unstrip: Handle SHT_GROUP correctly.Mark Wielaard2018-10-171-3/+9
| | | | | | | | | | | | | | | | | | | | The usage of annobin in Fedora showed a couple of bugs when using eu-strip and eu-unstrip on ET_REL files that contain multiple group sections. When stripping we should not remove the SHF_GROUP flag from sections even if the group section itself might be removed. Either the section itself gets removed, and so the flag doesn't matter. Or it gets moved together with the group section into the debug file, and then it still needs to have the flag set. Also we would "renumber" the section group flag field (which isn't a section index, and so shouldn't be changed). Often the group sections have the exact same name (".group"), flags (none) and sometimes the same sizes. Which makes matching them hard. Extract the group signature and compare those when comparing two group sections. Signed-off-by: Mark Wielaard <[email protected]>
* Handle ADD/SUB relocationsAndreas Schwab2018-10-131-4/+19
| | | | | | This adds support for ADD and SUB relocations as seen on RISC-V. Signed-off-by: Andreas Schwab <[email protected]>
* strip,unstrip: Use and set shdrstrndx consistently.Mark Wielaard2018-09-141-4/+39
| | | | | | | | | | | In various places in strip we used e_shstrndx instead of shdrstrndx and we didn't setup the shdrstrndx for the debug file. In unstrip we forgot to copy the shdrstrndx in case the -o output option was used. Added a new testcase that adds many sections to a testfile and runs strip, elflint, unstrip and elfcmp. Signed-off-by: Mark Wielaard <[email protected]>
* libebl: Use elf_getshdrstrndx in ebl_section_strip_p.Mark Wielaard2018-09-131-2/+2
| | | | | | | | | | The ebl_section_strip_p function used the Ehdr e_shstrndx field to get at the name of the (debug) sections. This is not correct if there are more than SHN_LORESERVE sections. Use elf_getshdrstrndx to get at the shstrtab section. And drop the Ehdr argument that isn't necessary anymore. Signed-off-by: Mark Wielaard <[email protected]>
* strip: Handle mixed allocated/non-allocated sections.Mark Wielaard2018-08-301-15/+65
| | | | | | | | | | | | | | | | | | Normally in non-ET_REL files all allocated sections come before all non-allocated sections. eu-strip relies on this when stripping a file and calculating the file offsets. But recently on Fedora there are non-allocated .gnu.build.attributes NOTE sections in the middle of the allocated sections, with a sh_offset field that is larger then the next section. This confuses eu-strip so much that it might corrupt the stripped file. Work around this by calculating the sh_offset fields in two phases when detecting mixed allocated/non-allocated sections. First handle the allocated ones, then use the offset after the last allocated section to calculate the offsets of the non-allocated sections left in the stripped file. Signed-off-by: Mark Wielaard <[email protected]>
* Consolidate error.h inclusion in system.hRoss Burton2018-07-051-1/+0
| | | | | | | | | error.h isn't standard and so isn't part of the musl C library. To easy future porting, consolidate the inclusion of error.h into system.h. https://sourceware.org/bugzilla/show_bug.cgi?id=21008 Signed-off-by: Ross Burton <[email protected]>
* strip: Deal with ARM data marker symbols pointing to debug sections.Mark Wielaard2017-07-241-6/+24
| | | | | | | | | | | | | | | | | | | ARM data marker symbols "$d" indicate the start of a sequence of data items in a section. For data only sections no data marker symbol is necessary, but may be put pointing to the start of the section. binutils however has a bug which places a data marker symbol somewhere inside the section (at least for .debug_frame). https://sourceware.org/bugzilla/show_bug.cgi?id=21809 When strip finds a symbol pointing to a debug section that would be put into the .debug file then it will copy over the whole symbol table. This isn't necessary because the symbol is redundant. Add an ebl hook to recognize data marker symbols with implementations for arm and aarch64. Use it in strip to strip such symbols from the symbol table if they point to a debug section. Signed-off-by: Mark Wielaard <[email protected]>
* strip: Add --keep-section=SECTION and --remove-section=SECTION.Mark Wielaard2017-07-171-17/+110
| | | | | | | | | | | | | | | | | | | | | Adds two new output options: --keep-section=SECTION Keep the named section. SECTION is an extended wildcard pattern. May be given more than once. --remove-section=SECTION Remove the named section. SECTION is an extended wildcard pattern. May be given more than once. Only non-allocated sections can be removed. The --remove-section was already partially implemented, but only for the .comment section. The short option -R is to be compatible with binutils. The new testcase makes sure that various combinations of kept/removed sections pull the correct dependencies into the output and/or debug files. https://bugzilla.redhat.com/show_bug.cgi?id=1465997 Signed-off-by: Mark Wielaard <[email protected]>
* strip: Don't generate empty output file when nothing to do.Mark Wielaard2017-06-141-12/+19
| | | | | | | | | | | | | | | | | If there was nothing to do strip would skip generating a separate debug file if one was requested, but it would also not finish the creation of a new output file (with the non-stripped sections). Also if there was an error any partially created output would be kept. Make sure that when the -o output file option is given we always generate a complete output file (except on error). Also make sure that when the -f debug file option is given it is only generated when it is not empty. Add testcase run-strip-nothing.sh that tests the various combinations. https://sourceware.org/bugzilla/show_bug.cgi?id=21522 Signed-off-by: Mark Wielaard <[email protected]>
* strip: Make sure old .shstrab is removed when eu-strip recreates it.Mark Wielaard2017-06-141-6/+10
| | | | | | | | | | | | | | | | Although we always recreate the .shstrtab section for the new output file we never explicitly assumed it could be removed. It might not be possible to remove it when the section string table is shared with a symbol table. But if it is removable we should (and recreate it for the new section list). Regression introduced in commit elfutils-0.163-33-gdf7dfab. "Handle merged strtab/shstrtab string tables in strip and unstrip." Add extra testcase to explicitly check for this case. https://sourceware.org/bugzilla/show_bug.cgi?id=21525 Signed-off-by: Mark Wielaard <[email protected]>
* Move print_version into printversion.{h|c}Ulf Hermann2017-02-171-0/+1
| | | | | | | | Rename version.c so that the implementation is called after the header and the header doesn't clash with the toplevel version.h. print_version depends on argp and is only used in the tools. Signed-off-by: Ulf Hermann <[email protected]>
* http://elfutils.org/ is now hosted at http://sourceware.org/elfutils/Mark Wielaard2016-12-241-15/+0
| | | | | | | | | | | | | | | | | fedorahosted used to be our home, but we are now hosted at sourceware. Change the elfutils project home to http://elfutils.org/ Point hosted services (email, release, git, bug tracker and web pages) to https://sourceware.org/elfutils/ Move design notes from README to NOTES. Add URLs for home, releases, bugs, git and mailinglist to README. Make the --version output of all tools the same by using a common print_version function and update the publicly shown copyright holder to the elfutils developers. Signed-off-by: Mark Wielaard <[email protected]>
* lib: Provide MAX and MIN in system.hAkihiko Odaki2016-10-121-1/+1
| | | | | | | | | | This change also creates a new header file libeu.h to provide the prototypes for the function of libeu. That hides the definition of function crc32, which can conflict with zlib, from libelf. It also prevents mistakes to refer those functions from a component which doesn't link with libeu, such as libelf. Signed-off-by: Akihiko Odaki <[email protected]>
* strip: Don't remove real symbols from allocated symbol tables.Mark Wielaard2016-10-061-11/+24
| | | | | | | | | | | | | | | Having a symbol in an allocated symbol table (like .dynsym) that points to an unallocated section is wrong. Traditionally strip has removed such symbols if they are section or group symbols. But removing a real symbol from an allocate symbol table is hard and probably a mistake. Really removing it means rewriting the dynamic segment and hash sections. Since we don't do that, don't remove the symbol (and corrupt the ELF file). Do warn and set the symbol section to SHN_UNDEF. https://bugzilla.redhat.com/show_bug.cgi?id=1380961 Signed-off-by: Mark Wielaard <[email protected]>
* strip: Use ELF_CHF_FORCE to recompress any sections that were uncompressed.Mark Wielaard2016-08-251-1/+1
| | | | | | | | Older gcc might create small .debug_pubtype. If such a section is compressed then strip should use ELF_CHF_FORCE to return it to compressed state after it is done with the section. Found by the run-strip-reloc.sh testcase on rhel6. Signed-off-by: Mark Wielaard <[email protected]>
* strip: Handle compressed relocation target sections.Mark Wielaard2016-08-151-5/+17
| | | | | | | | | | | | | | | binutils 2.27 assembler will create compressed sections for x86 ELF targets. The linker will decompress them again and it doesn't do this for any other target. This broke one of the run-strip-reloc.sh self tests. Fix by checking if the target of a relocation section is compressed and first decompressing it before applying relocations and then compressing again if necessary. Add explicit testcases for compressed and uncompressed ET_REL files to run-strip-reloc.sh. Signed-off-by: Mark Wielaard <[email protected]>
* dwelf: Add string table functions from ebl.Mark Wielaard2016-08-031-9/+15
| | | | | | | | | | | | | | | | Move the strtab functions from libebl to libdw. Programs often want to create ELF/DWARF string tables. We don't want (static) linking against ebl since those are internal functions that might change. This introduces dwelf_strtab_init, dwelf_strtab_add, dwelf_strtab_add_len, dwelf_strtab_finalize, dwelf_strent_off, dwelf_strent_str and dwelf_strtab_free. Documentation for each has been added to libdwelf.h. The add fucntion got a variant that takes the length explicitly and finalize was changed to return NULL on out of memory instead of aborting. All code and tests now uses the new functions. Signed-off-by: Mark Wielaard <[email protected]>
* Fix GCC6 -Wnull-dereference warnings.Mark Wielaard2016-02-221-2/+4
| | | | | | | | | | | | | | | | | | | | | | | asm_begin.c: In function ‘asm_begin’: asm_begin.c:62:7: error: potential null pointer dereference [-Werror=null-dereference] __fsetlocking (result->out.file, FSETLOCKING_BYCALLER); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ We set result to NULL just before this call in case of error. Fixed by only calling __fsetlocking when result is not NULL. strip.c: In function ‘handle_elf.constprop’: strip.c:1270:31: error: null pointer dereference [-Werror=null-dereference] elf_assert ((versiondata->d_size / sizeof (Elf32_Word)) ~~~~~~~~~~~^~~ src/strip.c:597:37: note: in definition of macro ‘elf_assert’ #define elf_assert(test) do { if (!(test)) goto illformed; } while (0) ^~~~ That is the wrong check, we want to check shndxdata, not versiondata here. Signed-off-by: Mark Wielaard <[email protected]>
* Trust AC_SYS_LARGEFILE to provide large file supportJosh Stone2015-10-091-6/+6
| | | | | | | | | | | | | | | AC_SYS_LARGEFILE defines _FILE_OFFSET_BITS in config.h if needed for LFS, and this automatically maps things like open to open64. But quite a few places used explicit 64-bit names, which won't work on platforms like FreeBSD where off_t is always 64-bit and there are no foo64 names. It's better to just trust that AC_SYS_LARGEFILE is doing it correctly. But we can verify this too, as some file could easily forget to include config.h. The new tests/run-lfs-symbols.sh checks all build targets against lfs-symbols (taken from lintian) to make sure everything was implicitly mapped to 64-bit variants when _FILE_OFFSET_BITS is set. Signed-off-by: Josh Stone <[email protected]>
* strip: Only sanity check symbols to not discarded sections for debug file.Mark Wielaard2015-10-091-3/+5
| | | | | | | | | | | | | | We can only sanity check the symbols to discarded sections when creating a debug file. Otherwise we won't keep track of debug_data. This can trigger erroneously when there are relocation symbols in the symtab for a .debug section but we are not putting those symbols in a separate .debug file. Adjust run-strip-test.sh to always run in-place strip size check even for ET_REL files. Before this fix the sanity check would trigger for some ET_REL files because they contained such relocation symbols to .debug sections (but debug_data would be NULL because -f wasn't used). Signed-off-by: Mark Wielaard <[email protected]>
* Handle merged strtab/shstrtab string tables in strip and unstrip.Mark Wielaard2015-10-051-6/+9
| | | | | | | | | | | | | | | | | | | | | ELF files can share the section header string table (e_shstrndx) with the symtab .strtab section. That might in some cases save a bit of space since symbols and sections might share some (sub)strings. To handle that eu-strip just needs to not unconditionally remove the .shstrtab section (it will be properly marked as used/unused as needed). eu-unstrip needs to make sure the section names are added to the strtab if it decides to rewrite that section. Also makes sure that eu-strip won't move around a SHT_NOBITS section that has SHF_ALLOC set. Although it is allowed to move such sections around, there is no benefit. And some tools might expect no allocated section to move around, not even a nobits section. It also makes it harder to do "roundtripping" sanity checks that make sure splitting a file with eu-strip and then reconstructed with eu-unstrip produce the same ELF file (as is done in the new run-strip-strmerge.sh). Introduces a somewhat large test generator elfstrmerge.c that will hopefully turn into a more generic string table merger program. Signed-off-by: Mark Wielaard <[email protected]>
* Remove old-style function definitions.Mark Wielaard2015-09-231-2/+2
| | | | | | | We already require -std=gnu99 and old-style function definitions might hide some compiler warnings. Signed-off-by: Mark Wielaard <[email protected]>
* strip: Just admit we don't support stripping ar archives for now.Mark Wielaard2015-06-101-2/+12
| | | | | | | | | | | | | We try to handle (in-place) stripping of ar archives, but since elf_clone doesn't really support cloning ar members and we don't handle reducing the size of the members, moving the offsets of other members. This causes crashes or writing garbage. Better to just admit that for now we don't support stripping archives. Call error when we see an ELF_K_AR file. https://bugzilla.redhat.com/show_bug.cgi?id=766156 https://lists.fedorahosted.org/pipermail/elfutils-devel/2009-August/000567.html Signed-off-by: Mark Wielaard <[email protected]>
* strip: Harden against bogus input files. Don't leak tmp debug file on error.Mark Wielaard2015-05-181-77/+191
| | | | | | | | | | There were various places where a bogus/unexpected input file would cause eu-strip to crash. Also on an unexpected error eu-strip would leak the temp debug file it was writing. https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c34 Signed-off-by: Mark Wielaard <[email protected]>
* Always call gelf_fsize with EV_CURRENT as argument.Mark Wielaard2015-05-131-12/+7
| | | | | | | | | | Don't trust the elf version given by the file. It could be completely bogus. In which case gelf_fsize just returns zero. Which could cause divide by zero errors. https://bugzilla.redhat.com/show_bug.cgi?id=1170810#c34 Signed-off-by: Mark Wielaard <[email protected]>
* Replace futimes with futimens.Max Filippov2015-05-051-9/+9
| | | | | | | | futimes is not standard function, and it's not available in uClibc. Use futimens which is POSIX. Suggested-by: Mike Frysinger <[email protected]> Signed-off-by: Max Filippov <[email protected]>
* Drop mtrace calls.Max Filippov2015-05-041-4/+0
| | | | | | | mtrace is a GNU extension not supported by uClibc and it's believed to not be really used. Drop mtrace() calls and #include <mcheck.h>. Signed-off-by: Max Filippov <[email protected]>
* src: Remove __DATE__ from internal error messages.Mark Wielaard2015-04-141-3/+3
| | | | | | | The date didn't really add much information (it is the day on which the code happened to be compiled) and it prevents creating reproducible builds. Signed-off-by: Mark Wielaard <[email protected]>
* Use elf_getphdrnum instead of accessing ehdr->e_phnum directly.Mark Wielaard2014-11-261-4/+9
| | | | | | | Using elf_getphdrnum lets us handle ELF files that use more than PN_XNUM phdrs. And guards against some corrupt files. Signed-off-by: Mark Wielaard <[email protected]>