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
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))
/// 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) {
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;
// 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.
{
--- /dev/null
+; 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/>
--- /dev/null
+; 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/>
// 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);