From bfc3db32d26cbf6d1aa96aa0fd9a82aa5576ba17 Mon Sep 17 00:00:00 2001 From: Tuomas Tynkkynen Date: Wed, 19 Apr 2017 17:14:19 +0300 Subject: Improve error message when run on statically linked binaries If .dynamic, .dynstr or .interp sections aren't found, give an extra hint to the user that the input file is statically linked. --- src/patchelf.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/patchelf.cc b/src/patchelf.cc index bfd9cc1..6363395 100644 --- a/src/patchelf.cc +++ b/src/patchelf.cc @@ -557,8 +557,12 @@ template Elf_Shdr & ElfFile::findSection(const SectionName & sectionName) { Elf_Shdr * shdr = findSection2(sectionName); - if (!shdr) - error("cannot find section '" + sectionName + "'"); + if (!shdr) { + std::string extraMsg = ""; + if (sectionName == ".interp" || sectionName == ".dynamic" || sectionName == ".dynstr") + extraMsg = ". The input file is most likely statically linked"; + error("cannot find section '" + sectionName + "'" + extraMsg); + } return *shdr; } -- cgit v0.12 From daaffbe7f3e4e1ec0052f8d6ce153cd7f5fdeb76 Mon Sep 17 00:00:00 2001 From: Tuomas Tynkkynen Date: Wed, 19 Apr 2017 17:22:23 +0300 Subject: Give a better error message if the file lacks a section header table Currently, patchelf outputs this when run on a UPX-compressed ELF file: patchelf: patchelf.cc:420: ElfFile::ElfFile(FileContents): Assertion `shstrtabIndex < shdrs.size()' failed. Make it give a nicer error message: patchelf: no section headers. The input file is probably a statically linked, self-decompressing binary Fixes #63 --- src/patchelf.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/patchelf.cc b/src/patchelf.cc index 6363395..86195fb 100644 --- a/src/patchelf.cc +++ b/src/patchelf.cc @@ -392,10 +392,13 @@ ElfFile::ElfFile(FileContents fileContents) error("wrong ELF type"); if ((size_t) (rdi(hdr->e_phoff) + rdi(hdr->e_phnum) * rdi(hdr->e_phentsize)) > fileContents->size()) - error("missing program headers"); + error("program header table out of bounds"); + + if (rdi(hdr->e_shnum) == 0) + error("no section headers. The input file is probably a statically linked, self-decompressing binary"); if ((size_t) (rdi(hdr->e_shoff) + rdi(hdr->e_shnum) * rdi(hdr->e_shentsize)) > fileContents->size()) - error("missing section headers"); + error("section header table out of bounds"); if (rdi(hdr->e_phentsize) != sizeof(Elf_Phdr)) error("program headers have wrong size"); -- cgit v0.12