Skip to content

Commit aceb96e

Browse files
authored
Merge pull request #121 from dezgeg/better-error-messages
Better error messages when run on statically-linked (or otherwise weird) binaries
2 parents 840eb0e + daaffbe commit aceb96e

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/patchelf.cc

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,13 @@ ElfFile<ElfFileParamNames>::ElfFile(FileContents fileContents)
394394
error("wrong ELF type");
395395

396396
if ((size_t) (rdi(hdr->e_phoff) + rdi(hdr->e_phnum) * rdi(hdr->e_phentsize)) > fileContents->size())
397-
error("missing program headers");
397+
error("program header table out of bounds");
398+
399+
if (rdi(hdr->e_shnum) == 0)
400+
error("no section headers. The input file is probably a statically linked, self-decompressing binary");
398401

399402
if ((size_t) (rdi(hdr->e_shoff) + rdi(hdr->e_shnum) * rdi(hdr->e_shentsize)) > fileContents->size())
400-
error("missing section headers");
403+
error("section header table out of bounds");
401404

402405
if (rdi(hdr->e_phentsize) != sizeof(Elf_Phdr))
403406
error("program headers have wrong size");
@@ -559,8 +562,12 @@ template<ElfFileParams>
559562
Elf_Shdr & ElfFile<ElfFileParamNames>::findSection(const SectionName & sectionName)
560563
{
561564
Elf_Shdr * shdr = findSection2(sectionName);
562-
if (!shdr)
563-
error("cannot find section '" + sectionName + "'");
565+
if (!shdr) {
566+
std::string extraMsg = "";
567+
if (sectionName == ".interp" || sectionName == ".dynamic" || sectionName == ".dynstr")
568+
extraMsg = ". The input file is most likely statically linked";
569+
error("cannot find section '" + sectionName + "'" + extraMsg);
570+
}
564571
return *shdr;
565572
}
566573

0 commit comments

Comments
 (0)