[llvm] r259162 - [llvm-bcanalyzer] Dump bitcode wrapper header

Akira Hatanaka via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 28 21:55:09 PST 2016


Author: ahatanak
Date: Thu Jan 28 23:55:09 2016
New Revision: 259162

URL: http://llvm.org/viewvc/llvm-project?rev=259162&view=rev
Log:
[llvm-bcanalyzer] Dump bitcode wrapper header

This patch enables llvm-bcanalyzer to print the bitcode wrapper header
if the file has one, which is needed to test the changes made in
r258627 (bitcode-wrapper-header-armv7m.ll is the test case for r258627).

Differential Revision: http://reviews.llvm.org/D16642

Added:
    llvm/trunk/test/Bitcode/bitcode-wrapper-header-armv7m.ll
    llvm/trunk/test/Bitcode/bitcode-wrapper-header-x86_64.ll
Modified:
    llvm/trunk/include/llvm/Bitcode/ReaderWriter.h
    llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
    llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp

Modified: llvm/trunk/include/llvm/Bitcode/ReaderWriter.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Bitcode/ReaderWriter.h?rev=259162&r1=259161&r2=259162&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Bitcode/ReaderWriter.h (original)
+++ llvm/trunk/include/llvm/Bitcode/ReaderWriter.h Thu Jan 28 23:55:09 2016
@@ -30,6 +30,14 @@ namespace llvm {
   class ModulePass;
   class raw_ostream;
 
+  /// Offsets of the 32-bit fields of bitcode wrapper header.
+  static const unsigned BWH_MagicField = 0*4;
+  static const unsigned BWH_VersionField = 1*4;
+  static const unsigned BWH_OffsetField = 2*4;
+  static const unsigned BWH_SizeField = 3*4;
+  static const unsigned BWH_CPUTypeField = 4*4;
+  static const unsigned BWH_HeaderSize = 5*4;
+
   /// Read the header of the specified bitcode buffer and prepare for lazy
   /// deserialization of function bodies. If ShouldLazyLoadMetadata is true,
   /// lazily load metadata as well. If successful, this moves Buffer. On
@@ -163,17 +171,12 @@ namespace llvm {
   inline bool SkipBitcodeWrapperHeader(const unsigned char *&BufPtr,
                                        const unsigned char *&BufEnd,
                                        bool VerifyBufferSize) {
-    enum {
-      KnownHeaderSize = 4*4,  // Size of header we read.
-      OffsetField = 2*4,      // Offset in bytes to Offset field.
-      SizeField = 3*4         // Offset in bytes to Size field.
-    };
-
-    // Must contain the header!
-    if (BufEnd-BufPtr < KnownHeaderSize) return true;
+    // Must contain the offset and size field!
+    if (BufEnd - BufPtr < BWH_SizeField + 4)
+      return true;
 
-    unsigned Offset = support::endian::read32le(&BufPtr[OffsetField]);
-    unsigned Size = support::endian::read32le(&BufPtr[SizeField]);
+    unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]);
+    unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]);
 
     // Verify that Offset+Size fits in the file.
     if (VerifyBufferSize && Offset+Size > unsigned(BufEnd-BufPtr))

Modified: llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp?rev=259162&r1=259161&r2=259162&view=diff
==============================================================================
--- llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp (original)
+++ llvm/trunk/lib/Bitcode/Writer/BitcodeWriter.cpp Thu Jan 28 23:55:09 2016
@@ -2966,10 +2966,6 @@ static void WriteModule(const Module *M,
 ///   uint32_t CPUType;       // CPU specifier.
 ///   ... potentially more later ...
 /// };
-enum {
-  DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
-  DarwinBCHeaderSize = 5*4
-};
 
 static void WriteInt32ToBuffer(uint32_t Value, SmallVectorImpl<char> &Buffer,
                                uint32_t &Position) {
@@ -3005,10 +3001,10 @@ static void EmitDarwinBCHeaderAndTrailer
     CPUType = DARWIN_CPU_TYPE_ARM;
 
   // Traditional Bitcode starts after header.
-  assert(Buffer.size() >= DarwinBCHeaderSize &&
+  assert(Buffer.size() >= BWH_HeaderSize &&
          "Expected header size to be reserved");
-  unsigned BCOffset = DarwinBCHeaderSize;
-  unsigned BCSize = Buffer.size()-DarwinBCHeaderSize;
+  unsigned BCOffset = BWH_HeaderSize;
+  unsigned BCSize = Buffer.size() - BWH_HeaderSize;
 
   // Write the magic and version.
   unsigned Position = 0;
@@ -3046,7 +3042,7 @@ void llvm::WriteBitcodeToFile(const Modu
   // header.
   Triple TT(M->getTargetTriple());
   if (TT.isOSDarwin() || TT.isOSBinFormatMachO())
-    Buffer.insert(Buffer.begin(), DarwinBCHeaderSize, 0);
+    Buffer.insert(Buffer.begin(), BWH_HeaderSize, 0);
 
   // Emit the module into the buffer.
   {

Added: llvm/trunk/test/Bitcode/bitcode-wrapper-header-armv7m.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/bitcode-wrapper-header-armv7m.ll?rev=259162&view=auto
==============================================================================
--- llvm/trunk/test/Bitcode/bitcode-wrapper-header-armv7m.ll (added)
+++ llvm/trunk/test/Bitcode/bitcode-wrapper-header-armv7m.ll Thu Jan 28 23:55:09 2016
@@ -0,0 +1,5 @@
+; RUN: llvm-as < %s | llvm-bcanalyzer -dump | FileCheck %s
+
+target triple = "thumbv7m-apple-unknown-macho"
+
+; CHECK: <BITCODE_WRAPPER_HEADER Magic=0x0b17c0de Version=0x{{[0-9a-f]+}} Offset=0x00000014 Size=0x{{[0-9a-f]+}} CPUType=0x0000000c/>

Added: llvm/trunk/test/Bitcode/bitcode-wrapper-header-x86_64.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bitcode/bitcode-wrapper-header-x86_64.ll?rev=259162&view=auto
==============================================================================
--- llvm/trunk/test/Bitcode/bitcode-wrapper-header-x86_64.ll (added)
+++ llvm/trunk/test/Bitcode/bitcode-wrapper-header-x86_64.ll Thu Jan 28 23:55:09 2016
@@ -0,0 +1,5 @@
+; RUN: llvm-as < %s | llvm-bcanalyzer -dump | FileCheck %s
+
+target triple = "x86_64-apple-macosx10.11.0"
+
+; CHECK: <BITCODE_WRAPPER_HEADER Magic=0x0b17c0de Version=0x{{[0-9a-f]+}} Offset=0x00000014 Size=0x{{[0-9a-f]+}} CPUType=0x01000007/>

Modified: llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp?rev=259162&r1=259161&r2=259162&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp (original)
+++ llvm/trunk/tools/llvm-bcanalyzer/llvm-bcanalyzer.cpp Thu Jan 28 23:55:09 2016
@@ -600,9 +600,28 @@ static bool openBitcodeFile(StringRef Pa
 
   // If we have a wrapper header, parse it and ignore the non-bc file contents.
   // The magic number is 0x0B17C0DE stored in little endian.
-  if (isBitcodeWrapper(BufPtr, EndBufPtr))
+  if (isBitcodeWrapper(BufPtr, EndBufPtr)) {
+    if (EndBufPtr - BufPtr < BWH_HeaderSize)
+      return Error("Invalid bitcode wrapper header");
+
+    if (Dump) {
+      unsigned Magic = support::endian::read32le(&BufPtr[BWH_MagicField]);
+      unsigned Version = support::endian::read32le(&BufPtr[BWH_VersionField]);
+      unsigned Offset = support::endian::read32le(&BufPtr[BWH_OffsetField]);
+      unsigned Size = support::endian::read32le(&BufPtr[BWH_SizeField]);
+      unsigned CPUType = support::endian::read32le(&BufPtr[BWH_CPUTypeField]);
+
+      outs() << "<BITCODE_WRAPPER_HEADER"
+             << " Magic=" << format_hex(Magic, 10)
+             << " Version=" << format_hex(Version, 10)
+             << " Offset=" << format_hex(Offset, 10)
+             << " Size=" << format_hex(Size, 10)
+             << " CPUType=" << format_hex(CPUType, 10) << "/>\n";
+    }
+
     if (SkipBitcodeWrapperHeader(BufPtr, EndBufPtr, true))
       return Error("Invalid bitcode wrapper header");
+  }
 
   StreamFile = BitstreamReader(BufPtr, EndBufPtr);
   Stream = BitstreamCursor(StreamFile);




More information about the llvm-commits mailing list