OSDN Git Service

[llvm-objcopy] Add e_machine validity check for reserved section indexes
authorPetr Hosek <phosek@chromium.org>
Wed, 13 Sep 2017 03:04:50 +0000 (03:04 +0000)
committerPetr Hosek <phosek@chromium.org>
Wed, 13 Sep 2017 03:04:50 +0000 (03:04 +0000)
As discussed on llvm-commits it was decided it would be best to check
e_machine before declaring that a reserved section index is valid. The
only special e_machine value that matters here is EM_HEXAGON. This
change adds a special check for EM_HEXAGON.

Patch by Jake Ehrlich

Differential Revision: https://reviews.llvm.org/D37767

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@313114 91177308-0d34-0410-b5e6-96231b3b80d8

test/tools/llvm-objcopy/common-symbol.test
tools/llvm-objcopy/Object.cpp

index 919726d..b659356 100644 (file)
@@ -7,7 +7,7 @@ FileHeader:
   Class:           ELFCLASS64
   Data:            ELFDATA2LSB
   Type:            ET_EXEC
-  Machine:         EM_X86_64
+  Machine:         EM_HEXAGON
 Symbols:
   Global:
     - Name:     test
index cad97b9..ef780d3 100644 (file)
@@ -90,18 +90,22 @@ void StringTableSection::writeSection(FileOutputBuffer &Out) const {
   StrTabBuilder.write(Out.getBufferStart() + Offset);
 }
 
-static bool isValidReservedSectionIndex(uint16_t Index) {
+static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
   switch (Index) {
   case SHN_ABS:
   case SHN_COMMON:
-  case SHN_HEXAGON_SCOMMON:
-  case SHN_HEXAGON_SCOMMON_2:
-  case SHN_HEXAGON_SCOMMON_4:
-  case SHN_HEXAGON_SCOMMON_8:
     return true;
-  default:
-    return false;
   }
+  if (Machine == EM_HEXAGON) {
+    switch (Index) {
+    case SHN_HEXAGON_SCOMMON:
+    case SHN_HEXAGON_SCOMMON_2:
+    case SHN_HEXAGON_SCOMMON_4:
+    case SHN_HEXAGON_SCOMMON_8:
+      return true;
+    }
+  }
+  return false;
 }
 
 uint16_t Symbol::getShndx() const {
@@ -133,7 +137,7 @@ void SymbolTableSection::addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
   Sym.Type = Type;
   Sym.DefinedIn = DefinedIn;
   if (DefinedIn == nullptr) {
-    if (isValidReservedSectionIndex(Shndx))
+    if (Shndx >= SHN_LORESERVE)
       Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
     else
       Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
@@ -289,7 +293,7 @@ void Object<ELFT>::initSymbolTable(const llvm::object::ELFFile<ELFT> &ElfFile,
     SectionBase *DefSection = nullptr;
     StringRef Name = unwrapOrError(Sym.getName(StrTabData));
     if (Sym.st_shndx >= SHN_LORESERVE) {
-      if (!isValidReservedSectionIndex(Sym.st_shndx)) {
+      if (!isValidReservedSectionIndex(Sym.st_shndx, Machine)) {
         error(
             "Symbol '" + Name +
             "' has unsupported value greater than or equal to SHN_LORESERVE: " +