[PATCH] Object/COFF: add function to check if section number is reserved one

Rui Ueyama ruiu at google.com
Mon Mar 17 16:22:47 PDT 2014


Hi Bigcheese,

Looks like there's no pass to run the pieces of code in COFFObjectFile.cpp
that I modified. Are these dead code? I changed the code but can't write test
for it.

http://llvm-reviews.chandlerc.com/D3103

Files:
  include/llvm/Support/COFF.h
  lib/Object/COFFObjectFile.cpp
  tools/llvm-nm/llvm-nm.cpp
  tools/llvm-readobj/COFFDumper.cpp

Index: include/llvm/Support/COFF.h
===================================================================
--- include/llvm/Support/COFF.h
+++ include/llvm/Support/COFF.h
@@ -30,6 +30,9 @@
 namespace llvm {
 namespace COFF {
 
+  // The maximum number of sections that a COFF object can have (inclusive).
+  const int MaxNumberOfSections = 65299;
+
   // The PE signature bytes that follows the DOS stub header.
   static const char PEMagic[] = { 'P', 'E', '\0', '\0' };
 
@@ -625,6 +628,10 @@
     DEBUG_INDEX_SUBSECTION        = 0xF4
   };
 
+  inline bool isReservedSectionNumber(int N) {
+    return N == IMAGE_SYM_UNDEFINED || N > MaxNumberOfSections;
+  }
+
 } // End namespace COFF.
 } // End namespace llvm.
 
Index: lib/Object/COFFObjectFile.cpp
===================================================================
--- lib/Object/COFFObjectFile.cpp
+++ lib/Object/COFFObjectFile.cpp
@@ -16,6 +16,7 @@
 #include "llvm/ADT/SmallString.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/ADT/Triple.h"
+#include "llvm/Support/COFF.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
 #include <cctype>
@@ -177,7 +178,7 @@
     Result = SymbolRef::ST_Function;
   } else {
     uint32_t Characteristics = 0;
-    if (Symb->SectionNumber > 0) {
+    if (!llvm::COFF::isReservedSectionNumber(Symb->SectionNumber)) {
       const coff_section *Section = NULL;
       if (error_code EC = getSection(Symb->SectionNumber, Section))
         return EC;
@@ -239,9 +240,9 @@
 error_code COFFObjectFile::getSymbolSection(DataRefImpl Ref,
                                             section_iterator &Result) const {
   const coff_symbol *Symb = toSymb(Ref);
-  if (Symb->SectionNumber <= COFF::IMAGE_SYM_UNDEFINED)
+  if (llvm::COFF::isReservedSectionNumber(Symb->SectionNumber)) {
     Result = section_end();
-  else {
+  } else {
     const coff_section *Sec = 0;
     if (error_code EC = getSection(Symb->SectionNumber, Sec)) return EC;
     DataRefImpl Ref;
@@ -722,9 +723,7 @@
 error_code COFFObjectFile::getSection(int32_t Index,
                                       const coff_section *&Result) const {
   // Check for special index values.
-  if (Index == COFF::IMAGE_SYM_UNDEFINED ||
-      Index == COFF::IMAGE_SYM_ABSOLUTE ||
-      Index == COFF::IMAGE_SYM_DEBUG)
+  if (llvm::COFF::isReservedSectionNumber(Index))
     Result = NULL;
   else if (Index > 0 && Index <= COFFHeader->NumberOfSections)
     // We already verified the section table data, so no need to check again.
Index: tools/llvm-nm/llvm-nm.cpp
===================================================================
--- tools/llvm-nm/llvm-nm.cpp
+++ tools/llvm-nm/llvm-nm.cpp
@@ -27,6 +27,7 @@
 #include "llvm/Object/MachO.h"
 #include "llvm/Object/MachOUniversal.h"
 #include "llvm/Object/ObjectFile.h"
+#include "llvm/Support/COFF.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Format.h"
@@ -317,9 +318,7 @@
     return Ret;
 
   uint32_t Characteristics = 0;
-  if (Symb->SectionNumber > 0 &&
-      Symb->SectionNumber != llvm::COFF::IMAGE_SYM_DEBUG &&
-      Symb->SectionNumber != llvm::COFF::IMAGE_SYM_ABSOLUTE) {
+  if (!llvm::COFF::isReservedSectionNumber(Symb->SectionNumber)) {
     section_iterator SecI = Obj.section_end();
     if (error(SymI->getSection(SecI)))
       return '?';
Index: tools/llvm-readobj/COFFDumper.cpp
===================================================================
--- tools/llvm-readobj/COFFDumper.cpp
+++ tools/llvm-readobj/COFFDumper.cpp
@@ -993,7 +993,7 @@
     } else if (
         Symbol->StorageClass   == COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL ||
         (Symbol->StorageClass  == COFF::IMAGE_SYM_CLASS_EXTERNAL &&
-         Symbol->SectionNumber == 0 &&
+         Symbol->SectionNumber == COFF::IMAGE_SYM_UNDEFINED &&
          Symbol->Value         == 0)) {
       const coff_aux_weak_external_definition *Aux;
       if (error(getSymbolAuxData(Obj, Symbol + I, Aux)))
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3103.1.patch
Type: text/x-patch
Size: 3969 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140317/1a49fa5f/attachment.bin>


More information about the llvm-commits mailing list