| 5748 | } |
| 5749 | |
| 5750 | Expected<BitcodeFileContents> |
| 5751 | llvm::getBitcodeFileContents(MemoryBufferRef Buffer) { |
| 5752 | Expected<BitstreamCursor> StreamOrErr = initStream(Buffer); |
| 5753 | if (!StreamOrErr) |
| 5754 | return StreamOrErr.takeError(); |
| 5755 | BitstreamCursor &Stream = *StreamOrErr; |
| 5756 | |
| 5757 | BitcodeFileContents F; |
| 5758 | while (true) { |
| 5759 | uint64_t BCBegin = Stream.getCurrentByteNo(); |
| 5760 | |
| 5761 | // We may be consuming bitcode from a client that leaves garbage at the end |
| 5762 | // of the bitcode stream (e.g. Apple's ar tool). If we are close enough to |
| 5763 | // the end that there cannot possibly be another module, stop looking. |
| 5764 | if (BCBegin + 8 >= Stream.getBitcodeBytes().size()) |
| 5765 | return F; |
| 5766 | |
| 5767 | BitstreamEntry Entry = Stream.advance(); |
| 5768 | switch (Entry.Kind) { |
| 5769 | case BitstreamEntry::EndBlock: |
| 5770 | case BitstreamEntry::Error: |
| 5771 | return error("Malformed block"); |
| 5772 | |
| 5773 | case BitstreamEntry::SubBlock: { |
| 5774 | uint64_t IdentificationBit = -1ull; |
| 5775 | if (Entry.ID == bitc::IDENTIFICATION_BLOCK_ID) { |
| 5776 | IdentificationBit = Stream.GetCurrentBitNo() - BCBegin * 8; |
| 5777 | if (Stream.SkipBlock()) |
| 5778 | return error("Malformed block"); |
| 5779 | |
| 5780 | Entry = Stream.advance(); |
| 5781 | if (Entry.Kind != BitstreamEntry::SubBlock || |
| 5782 | Entry.ID != bitc::MODULE_BLOCK_ID) |
| 5783 | return error("Malformed block"); |
| 5784 | } |
| 5785 | |
| 5786 | if (Entry.ID == bitc::MODULE_BLOCK_ID) { |
| 5787 | uint64_t ModuleBit = Stream.GetCurrentBitNo() - BCBegin * 8; |
| 5788 | if (Stream.SkipBlock()) |
| 5789 | return error("Malformed block"); |
| 5790 | |
| 5791 | F.Mods.push_back({Stream.getBitcodeBytes().slice( |
| 5792 | BCBegin, Stream.getCurrentByteNo() - BCBegin), |
| 5793 | Buffer.getBufferIdentifier(), IdentificationBit, |
| 5794 | ModuleBit}); |
| 5795 | continue; |
| 5796 | } |
| 5797 | |
| 5798 | if (Entry.ID == bitc::STRTAB_BLOCK_ID) { |
| 5799 | Expected<StringRef> Strtab = |
| 5800 | readBlobInRecord(Stream, bitc::STRTAB_BLOCK_ID, bitc::STRTAB_BLOB); |
| 5801 | if (!Strtab) |
| 5802 | return Strtab.takeError(); |
| 5803 | // This string table is used by every preceding bitcode module that does |
| 5804 | // not have its own string table. A bitcode file may have multiple |
| 5805 | // string tables if it was created by binary concatenation, for example |
| 5806 | // with "llvm-cat -b". |
| 5807 | for (auto I = F.Mods.rbegin(), E = F.Mods.rend(); I != E; ++I) { |
nothing calls this directly
no test coverage detected