llvm.org GIT mirror llvm / a0894a8
Bitcode: Change expected layout of module blocks. We now expect each module's identification block to appear immediately before the module block. Any module block that appears without an identification block immediately before it is interpreted as if it does not have a module block. Also change the interpretation of VST and function offsets in bitcode. The offset is always taken as relative to the start of the identification (or module if not present) block, minus one word. This corresponds to the historical interpretation of offsets, i.e. relative to the start of the file. These changes allow for bitcode modules to be concatenated by copying bytes. Differential Revision: https://reviews.llvm.org/D27184 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@288098 91177308-0d34-0410-b5e6-96231b3b80d8 Peter Collingbourne 3 years ago
2 changed file(s) with 36 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
4141
4242 /// Represents a module in a bitcode file.
4343 class BitcodeModule {
44 // This covers the identification (if present) and module blocks.
4445 ArrayRef Buffer;
4546 StringRef ModuleIdentifier;
4647
23052305 assert(GO);
23062306 }
23072307
2308 uint64_t FuncWordOffset = Record[1];
2308 // Note that we subtract 1 here because the offset is relative to one word
2309 // before the start of the identification or module block, which was
2310 // historically always the start of the regular bitcode header.
2311 uint64_t FuncWordOffset = Record[1] - 1;
23092312 Function *F = dyn_cast(GO);
23102313 assert(F);
23112314 uint64_t FuncBitOffset = FuncWordOffset * 32;
43534356 case bitc::MODULE_CODE_VSTOFFSET:
43544357 if (Record.size() < 1)
43554358 return error("Invalid record");
4356 VSTOffset = Record[0];
4359 // Note that we subtract 1 here because the offset is relative to one word
4360 // before the start of the identification or module block, which was
4361 // historically always the start of the regular bitcode header.
4362 VSTOffset = Record[0] - 1;
43574363 break;
43584364 /// MODULE_CODE_SOURCE_FILENAME: [namechar x N]
43594365 case bitc::MODULE_CODE_SOURCE_FILENAME:
65486554 return StreamOrErr.takeError();
65496555 BitstreamCursor &Stream = *StreamOrErr;
65506556
6551 uint64_t IdentificationBit = -1ull;
65526557 std::vector Modules;
65536558 while (true) {
6559 uint64_t BCBegin = Stream.getCurrentByteNo();
6560
65546561 // We may be consuming bitcode from a client that leaves garbage at the end
65556562 // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to
65566563 // the end that there cannot possibly be another module, stop looking.
6557 if (Stream.getCurrentByteNo() + 8 >= Stream.getBitcodeBytes().size())
6564 if (BCBegin + 8 >= Stream.getBitcodeBytes().size())
65586565 return Modules;
65596566
65606567 BitstreamEntry Entry = Stream.advance();
65636570 case BitstreamEntry::Error:
65646571 return error("Malformed block");
65656572
6566 case BitstreamEntry::SubBlock:
6567 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID)
6568 IdentificationBit = Stream.GetCurrentBitNo();
6569 else if (Entry.ID == bitc::MODULE_BLOCK_ID)
6570 Modules.push_back({Stream.getBitcodeBytes(),
6573 case BitstreamEntry::SubBlock: {
6574 uint64_t IdentificationBit = -1ull;
6575 if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) {
6576 IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8;
6577 if (Stream.SkipBlock())
6578 return error("Malformed block");
6579
6580 Entry = Stream.advance();
6581 if (Entry.Kind != BitstreamEntry::SubBlock ||
6582 Entry.ID != bitc::MODULE_BLOCK_ID)
6583 return error("Malformed block");
6584 }
6585
6586 if (Entry.ID == bitc::MODULE_BLOCK_ID) {
6587 uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8;
6588 if (Stream.SkipBlock())
6589 return error("Malformed block");
6590
6591 Modules.push_back({Stream.getBitcodeBytes().slice(
6592 BCBegin, Stream.getCurrentByteNo() - BCBegin),
65716593 Buffer.getBufferIdentifier(), IdentificationBit,
6572 Stream.GetCurrentBitNo()});
6594 ModuleBit});
6595 continue;
6596 }
65736597
65746598 if (Stream.SkipBlock())
65756599 return error("Malformed block");
65766600 continue;
6601 }
65776602 case BitstreamEntry::Record:
65786603 Stream.skipRecord(Entry.ID);
65796604 continue;