Object: BSS/virtual sections don't have contents
authorDavid Majnemer <david.majnemer@gmail.com>
Fri, 26 Sep 2014 22:32:16 +0000 (22:32 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Fri, 26 Sep 2014 22:32:16 +0000 (22:32 +0000)
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.

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

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

index 1be0691a1d974bc9689265b51d78096b3f4e7f50..62e3b9ccf6410718290f71b042768ba29ef0e74b 100644 (file)
@@ -565,6 +565,15 @@ DWARFContextInMemory::DWARFContextInMemory(object::ObjectFile &Obj)
   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);
 
index 7daef06f035036190cea46a13b440d6690d07d42..45de4341b3f00f09f79908d3b42324dadaf47ecb 100644 (file)
@@ -840,6 +840,10 @@ std::error_code COFFObjectFile::getSectionName(const coff_section *Sec,
 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.
index b6beedea5fd5f95eadc9d6fc50a3a3c21e5470b3..a84f138479b42ac0d73ca157a46d44b7109e0a45 100644 (file)
@@ -19,4 +19,3 @@
 // CHECK-NEXT:     Info: 0
 // CHECK-NEXT:     AddressAlignment: 16
 // CHECK-NEXT:     EntrySize: 0
-// CHECK-NEXT:     SectionData (
index 99a374d5a6ae102cdbe014902a90c25ac31f2d38..3d71d832f99b07bdc582d9d5eef8cbd8cc85959e 100644 (file)
@@ -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;
index 1791f5a32471ef59671079b36b170ff354cd8ca9..6da3318c9319850ea5d9a040faeff313fb9fbaf9 100644 (file)
@@ -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()));
index 41a78e762fbc927f5189d5f873a2d5278e8107ec..2d09282f11f904a5cda422b2b8953ee7276e6f56 100644 (file)
@@ -266,11 +266,16 @@ void MachODumper::printSections(const MachOObjectFile *Obj) {
     }
 
     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);
+      }
     }
   }
 }