[PATCH] D25231: [ELF] - Make checks in ObjectFile<ELFT>::getSection() stricter.

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 4 04:54:56 PDT 2016


grimar created this revision.
grimar added reviewers: ruiu, rafael, davide.
grimar added subscribers: llvm-commits, grimar.

One of the change we made in the past was:
" // 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."

This patch makes the check for null section stricter, so it is only allowed for STT_SECTION symbols now.
I did it because testcase that contains local unnamed symbol crashes without that check in:

  template <class ELFT>
  static bool shouldKeepInSymtab(InputSectionBase<ELFT> *Sec, StringRef SymName,
                                 const SymbolBody &B) {
  ...
    return !(Sec->getSectionHdr()->sh_flags & SHF_MERGE); // HERE, because Sec is null
  }


https://reviews.llvm.org/D25231

Files:
  ELF/InputFiles.cpp
  test/ELF/invalid/Inputs/section-index2.elf
  test/ELF/invalid/invalid-elf.test


Index: test/ELF/invalid/invalid-elf.test
===================================================================
--- test/ELF/invalid/invalid-elf.test
+++ test/ELF/invalid/invalid-elf.test
@@ -20,6 +20,10 @@
 # RUN:   FileCheck --check-prefix=INVALID-SECTION-INDEX-LLD %s
 # INVALID-SECTION-INDEX-LLD: invalid section index
 
+## section-index2.elf has local symbol with incorrect section index.
+# RUN: not ld.lld %p/Inputs/section-index2.elf -o %t2 2>&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
Index: ELF/InputFiles.cpp
===================================================================
--- ELF/InputFiles.cpp
+++ ELF/InputFiles.cpp
@@ -367,14 +367,15 @@
   uint32_t Index = this->getSectionIndex(Sym);
   if (Index == 0)
     return nullptr;
-  if (Index >= Sections.size())
-    fatal(getFilename(this) + ": invalid section index: " + Twine(Index));
-  InputSectionBase<ELFT> *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 (Index >= Sections.size() ||
+      (!Sections[Index] && Sym.getType() != STT_SECTION))
+    fatal(getFilename(this) + ": invalid section index: " + Twine(Index));
+  InputSectionBase<ELFT> *S = Sections[Index];
   if (!S || S == &InputSectionBase<ELFT>::Discarded)
     return S;
   return S->Repl;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25231.73457.patch
Type: text/x-patch
Size: 1731 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161004/6861d9c9/attachment.bin>


More information about the llvm-commits mailing list