aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Bitcode
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2008-07-09 05:14:23 +0000
committerChris Lattner <sabre@nondot.org>2008-07-09 05:14:23 +0000
commit6fa6a32e4e2fdbb77c82fd7f64a7232cdcac994e (patch)
tree914c16b89dfc2af72fe4eb3a213c4db392be03c2 /lib/Bitcode
parentb02b8af3780e4e7942cb6e5d25260a608076fe6d (diff)
downloadexternal_llvm-6fa6a32e4e2fdbb77c82fd7f64a7232cdcac994e.zip
external_llvm-6fa6a32e4e2fdbb77c82fd7f64a7232cdcac994e.tar.gz
external_llvm-6fa6a32e4e2fdbb77c82fd7f64a7232cdcac994e.tar.bz2
Add a little wrapper header that is put around bc files when emitting
bc files for modules with a target triple that indicates they are for darwin. The reader unconditionally handles this, and the writer could turn this on for more targets if we care. This change has two benefits for darwin: 1) it allows us to encode the cpu type of the file in an easy to read place that doesn't require decoding the bc file. 2) it works around a bug (IMO) in darwin's AR where it is incapable of handling files that are not a multiple of 8 bytes long. BC files are only guaranteed to be multiples of 4 bytes long. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@53275 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Bitcode')
-rw-r--r--lib/Bitcode/Reader/BitcodeReader.cpp52
-rw-r--r--lib/Bitcode/Writer/BitcodeWriter.cpp75
2 files changed, 125 insertions, 2 deletions
diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp
index 3fc6b17..a8c62be 100644
--- a/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -1184,6 +1184,47 @@ bool BitcodeReader::ParseModule(const std::string &ModuleID) {
return Error("Premature end of bitstream");
}
+/// SkipWrapperHeader - Some systems wrap bc files with a special header for
+/// padding or other reasons. The format of this header is:
+///
+/// struct bc_header {
+/// uint32_t Magic; // 0x0B17C0DE
+/// uint32_t Version; // Version, currently always 0.
+/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
+/// uint32_t BitcodeSize; // Size of traditional bitcode file.
+/// ... potentially other gunk ...
+/// };
+///
+/// This function is called when we find a file with a matching magic number.
+/// In this case, skip down to the subsection of the file that is actually a BC
+/// file.
+static bool SkipWrapperHeader(unsigned char *&BufPtr, unsigned char *&BufEnd) {
+ 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;
+
+ unsigned Offset = ( BufPtr[OffsetField ] |
+ (BufPtr[OffsetField+1] << 8) |
+ (BufPtr[OffsetField+2] << 16) |
+ (BufPtr[OffsetField+3] << 24));
+ unsigned Size = ( BufPtr[SizeField ] |
+ (BufPtr[SizeField +1] << 8) |
+ (BufPtr[SizeField +2] << 16) |
+ (BufPtr[SizeField +3] << 24));
+
+ // Verify that Offset+Size fits in the file.
+ if (Offset+Size > unsigned(BufEnd-BufPtr))
+ return true;
+ BufPtr += Offset;
+ BufEnd = BufPtr+Size;
+ return false;
+}
bool BitcodeReader::ParseBitcode() {
TheModule = 0;
@@ -1192,7 +1233,16 @@ bool BitcodeReader::ParseBitcode() {
return Error("Bitcode stream should be a multiple of 4 bytes in length");
unsigned char *BufPtr = (unsigned char *)Buffer->getBufferStart();
- Stream.init(BufPtr, BufPtr+Buffer->getBufferSize());
+ unsigned char *BufEnd = BufPtr+Buffer->getBufferSize();
+
+ // 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 (BufPtr != BufEnd && BufPtr[0] == 0xDE && BufPtr[1] == 0xC0 &&
+ BufPtr[2] == 0x17 && BufPtr[3] == 0x0B)
+ if (SkipWrapperHeader(BufPtr, BufEnd))
+ return Error("Invalid bitcode wrapper header");
+
+ Stream.init(BufPtr, BufEnd);
// Sniff for the signature.
if (Stream.Read(8) != 'B' ||
diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp
index 0030aca..9794fac 100644
--- a/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -1273,6 +1273,70 @@ static void WriteModule(const Module *M, BitstreamWriter &Stream) {
Stream.ExitBlock();
}
+/// EmitDarwinBCHeader - If generating a bc file on darwin, we have to emit a
+/// header and trailer to make it compatible with the system archiver. To do
+/// this we emit the following header, and then emit a trailer that pads the
+/// file out to be a multiple of 16 bytes.
+///
+/// struct bc_header {
+/// uint32_t Magic; // 0x0B17C0DE
+/// uint32_t Version; // Version, currently always 0.
+/// uint32_t BitcodeOffset; // Offset to traditional bitcode file.
+/// uint32_t BitcodeSize; // Size of traditional bitcode file.
+/// uint32_t CPUType; // CPU specifier.
+/// ... potentially more later ...
+/// };
+enum {
+ DarwinBCSizeFieldOffset = 3*4, // Offset to bitcode_size.
+ DarwinBCHeaderSize = 5*4
+};
+
+static void EmitDarwinBCHeader(BitstreamWriter &Stream,
+ const std::string &TT) {
+ unsigned CPUType = ~0U;
+
+ // Match x86_64-*, i[3-9]86-*, powerpc-*, powerpc64-*. The CPUType is a
+ // magic number from /usr/include/mach/machine.h. It is ok to reproduce the
+ // specific constants here because they are implicitly part of the Darwin ABI.
+ enum {
+ DARWIN_CPU_ARCH_ABI64 = 0x01000000,
+ DARWIN_CPU_TYPE_X86 = 7,
+ DARWIN_CPU_TYPE_POWERPC = 18
+ };
+
+ if (TT.find("x86_64-") == 0)
+ CPUType = DARWIN_CPU_TYPE_X86 | DARWIN_CPU_ARCH_ABI64;
+ else if (TT.size() >= 5 && TT[0] == 'i' && TT[2] == '8' && TT[3] == '6' &&
+ TT[4] == '-' && TT[1] - '3' < 6)
+ CPUType = DARWIN_CPU_TYPE_X86;
+ else if (TT.find("powerpc-") == 0)
+ CPUType = DARWIN_CPU_TYPE_POWERPC;
+ else if (TT.find("powerpc64-") == 0)
+ CPUType = DARWIN_CPU_TYPE_POWERPC | DARWIN_CPU_ARCH_ABI64;
+
+ // Traditional Bitcode starts after header.
+ unsigned BCOffset = DarwinBCHeaderSize;
+
+ Stream.Emit(0x0B17C0DE, 32);
+ Stream.Emit(0 , 32); // Version.
+ Stream.Emit(BCOffset , 32);
+ Stream.Emit(0 , 32); // Filled in later.
+ Stream.Emit(CPUType , 32);
+}
+
+/// EmitDarwinBCTrailer - Emit the darwin epilog after the bitcode file and
+/// finalize the header.
+static void EmitDarwinBCTrailer(BitstreamWriter &Stream, unsigned BufferSize) {
+ // Update the size field in the header.
+ Stream.BackpatchWord(DarwinBCSizeFieldOffset, BufferSize-DarwinBCHeaderSize);
+
+ // If the file is not a multiple of 16 bytes, insert dummy padding.
+ while (BufferSize & 15) {
+ Stream.Emit(0, 8);
+ ++BufferSize;
+ }
+}
+
/// WriteBitcodeToFile - Write the specified module to the specified output
/// stream.
@@ -1282,6 +1346,11 @@ void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
Buffer.reserve(256*1024);
+ // If this is darwin, emit a file header and trailer if needed.
+ bool isDarwin = M->getTargetTriple().find("-darwin") != std::string::npos;
+ if (isDarwin)
+ EmitDarwinBCHeader(Stream, M->getTargetTriple());
+
// Emit the file header.
Stream.Emit((unsigned)'B', 8);
Stream.Emit((unsigned)'C', 8);
@@ -1292,10 +1361,14 @@ void llvm::WriteBitcodeToFile(const Module *M, std::ostream &Out) {
// Emit the module.
WriteModule(M, Stream);
+
+ if (isDarwin)
+ EmitDarwinBCTrailer(Stream, Buffer.size());
+
// If writing to stdout, set binary mode.
if (llvm::cout == Out)
- sys::Program::ChangeStdoutToBinary();
+ sys::Program::ChangeStdoutToBinary();
// Write the generated bitstream to "Out".
Out.write((char*)&Buffer.front(), Buffer.size());