[llvm] r218549 - Object: BSS/virtual sections don't have contents

David Majnemer david.majnemer at gmail.com
Fri Sep 26 15:32:17 PDT 2014


Author: majnemer
Date: Fri Sep 26 17:32:16 2014
New Revision: 218549

URL: http://llvm.org/viewvc/llvm-project?rev=218549&view=rev
Log:
Object: BSS/virtual sections don't have contents

Users of getSectionContents shouldn't try to pass in BSS or virtual
sections.  In all instances, this is a bug in the code calling this
routine.

N.B. Some COFF implementations (like CL) will mark their BSS sections as
taking space on disk.  This would confuse COFFObjectFile into thinking
the section is larger than the file.

Modified:
    llvm/trunk/lib/DebugInfo/DWARFContext.cpp
    llvm/trunk/lib/Object/COFFObjectFile.cpp
    llvm/trunk/test/MC/PowerPC/lcomm.s
    llvm/trunk/tools/llvm-readobj/COFFDumper.cpp
    llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
    llvm/trunk/tools/llvm-readobj/MachODumper.cpp

Modified: llvm/trunk/lib/DebugInfo/DWARFContext.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/DebugInfo/DWARFContext.cpp?rev=218549&r1=218548&r2=218549&view=diff
==============================================================================
--- llvm/trunk/lib/DebugInfo/DWARFContext.cpp (original)
+++ llvm/trunk/lib/DebugInfo/DWARFContext.cpp Fri Sep 26 17:32:16 2014
@@ -565,6 +565,15 @@ DWARFContextInMemory::DWARFContextInMemo
   for (const SectionRef &Section : Obj.sections()) {
     StringRef name;
     Section.getName(name);
+    // Skip BSS and Virtual sections, they aren't interesting.
+    bool IsBSS;
+    Section.isBSS(IsBSS);
+    if (IsBSS)
+      continue;
+    bool IsVirtual;
+    Section.isVirtual(IsVirtual);
+    if (IsVirtual)
+      continue;
     StringRef data;
     Section.getContents(data);
 

Modified: llvm/trunk/lib/Object/COFFObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Object/COFFObjectFile.cpp?rev=218549&r1=218548&r2=218549&view=diff
==============================================================================
--- llvm/trunk/lib/Object/COFFObjectFile.cpp (original)
+++ llvm/trunk/lib/Object/COFFObjectFile.cpp Fri Sep 26 17:32:16 2014
@@ -840,6 +840,10 @@ std::error_code COFFObjectFile::getSecti
 std::error_code
 COFFObjectFile::getSectionContents(const coff_section *Sec,
                                    ArrayRef<uint8_t> &Res) const {
+  // PointerToRawData and SizeOfRawData won't make sense for BSS sections, don't
+  // do anything interesting for them.
+  assert((Sec->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA) == 0 &&
+         "BSS sections don't have contents!");
   // The only thing that we need to verify is that the contents is contained
   // within the file bounds. We don't need to make sure it doesn't cover other
   // data, as there's nothing that says that is not allowed.

Modified: llvm/trunk/test/MC/PowerPC/lcomm.s
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/MC/PowerPC/lcomm.s?rev=218549&r1=218548&r2=218549&view=diff
==============================================================================
--- llvm/trunk/test/MC/PowerPC/lcomm.s (original)
+++ llvm/trunk/test/MC/PowerPC/lcomm.s Fri Sep 26 17:32:16 2014
@@ -19,4 +19,3 @@
 // CHECK-NEXT:     Info: 0
 // CHECK-NEXT:     AddressAlignment: 16
 // CHECK-NEXT:     EntrySize: 0
-// CHECK-NEXT:     SectionData (

Modified: llvm/trunk/tools/llvm-readobj/COFFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/COFFDumper.cpp?rev=218549&r1=218548&r2=218549&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/COFFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/COFFDumper.cpp Fri Sep 26 17:32:16 2014
@@ -635,7 +635,8 @@ void COFFDumper::printSections() {
     if (Name == ".debug$S" && opts::CodeViewLineTables)
       printCodeViewLineTables(Sec);
 
-    if (opts::SectionData) {
+    if (opts::SectionData &&
+        !(Section->Characteristics & COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA)) {
       StringRef Data;
       if (error(Sec.getContents(Data)))
         break;

Modified: llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ELFDumper.cpp?rev=218549&r1=218548&r2=218549&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ELFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/ELFDumper.cpp Fri Sep 26 17:32:16 2014
@@ -604,7 +604,7 @@ void ELFDumper<ELFT>::printSections() {
       }
     }
 
-    if (opts::SectionData) {
+    if (opts::SectionData && Section->sh_type != ELF::SHT_NOBITS) {
       ArrayRef<uint8_t> Data = errorOrDefault(Obj->getSectionContents(Section));
       W.printBinaryBlock("SectionData",
                          StringRef((const char *)Data.data(), Data.size()));

Modified: llvm/trunk/tools/llvm-readobj/MachODumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/MachODumper.cpp?rev=218549&r1=218548&r2=218549&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/MachODumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/MachODumper.cpp Fri Sep 26 17:32:16 2014
@@ -266,11 +266,16 @@ void MachODumper::printSections(const Ma
     }
 
     if (opts::SectionData) {
-      StringRef Data;
-      if (error(Section.getContents(Data)))
+      bool IsBSS;
+      if (error(Section.isBSS(IsBSS)))
         break;
+      if (!IsBSS) {
+        StringRef Data;
+        if (error(Section.getContents(Data)))
+          break;
 
-      W.printBinaryBlock("SectionData", Data);
+        W.printBinaryBlock("SectionData", Data);
+      }
     }
   }
 }





More information about the llvm-commits mailing list