From 24adce95b27e97b972fddcbcd93cf90421f8689d Mon Sep 17 00:00:00 2001 From: George Rimar Date: Thu, 6 Oct 2016 09:17:55 +0000 Subject: [PATCH] [ELF] - Make checks in ObjectFile::getSection() stricter. This patch makes the check for null section stricter, so it is only allowed for STT_SECTION symbols now. Differential revision: https://reviews.llvm.org/D25231 llvm-svn: 283426 --- lld/ELF/InputFiles.cpp | 11 ++++++++--- lld/test/ELF/invalid/Inputs/section-index2.elf | Bin 0 -> 474 bytes lld/test/ELF/invalid/invalid-elf.test | 4 ++++ 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 lld/test/ELF/invalid/Inputs/section-index2.elf diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 3562609..e8f2bee 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -377,17 +377,22 @@ template InputSectionBase * elf::ObjectFile::getSection(const Elf_Sym &Sym) const { uint32_t Index = this->getSectionIndex(Sym); - if (Index == 0) - return nullptr; if (Index >= Sections.size()) fatal(getFilename(this) + ": invalid section index: " + Twine(Index)); InputSectionBase *S = Sections[Index]; + // We found that GNU assembler 2.17.50 [FreeBSD] 2007-07-03 // could generate broken objects. STT_SECTION symbols can be // associated with SHT_REL[A]/SHT_SYMTAB/SHT_STRTAB sections. // In this case it is fine for section to be null here as we // do not allocate sections of these types. - if (!S || S == &InputSectionBase::Discarded) + if (!S) { + if (Index == 0 || Sym.getType() == STT_SECTION) + return nullptr; + fatal(getFilename(this) + ": invalid section index: " + Twine(Index)); + } + + if (S == &InputSectionBase::Discarded) return S; return S->Repl; } diff --git a/lld/test/ELF/invalid/Inputs/section-index2.elf b/lld/test/ELF/invalid/Inputs/section-index2.elf new file mode 100644 index 0000000000000000000000000000000000000000..5d842880b3755a5fafdaec8e9904d132618f068d GIT binary patch literal 474 zcmb<-^>JfjWMqH=Mg}_u1P><4z~F#jLfH-stPCtjqU13_c7ZS(n)U{$BhYO?cbg&1 | \ +# RUN: FileCheck --check-prefix=INVALID-SECTION-INDEX-LLD %s + # RUN: not ld.lld %p/Inputs/shstrndx.so -o %t2 2>&1 | \ # RUN: FileCheck --check-prefix=INVALID-SECTION-INDEX %s # INVALID-SECTION-INDEX: Invalid section index -- 2.7.4