Zachary Turner | f04d6e8 | 2017-01-20 22:41:15 +0000 | [diff] [blame] | 1 | //===- NamedStreamMap.cpp - PDB Named Stream Map ----------------*- C++ -*-===// |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/PDB/Native/NamedStreamMap.h" |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 11 | |
David Majnemer | 36b7b08 | 2016-06-04 22:47:39 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/SparseBitVector.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/StringMap.h" |
| 14 | #include "llvm/ADT/StringRef.h" |
| 15 | #include "llvm/ADT/iterator_range.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/Native/HashTable.h" |
| 17 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 18 | #include "llvm/Support/BinaryStreamReader.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Error.h" |
| 20 | #include <algorithm> |
| 21 | #include <cstdint> |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 24 | using namespace llvm::pdb; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 25 | |
Zachary Turner | f04d6e8 | 2017-01-20 22:41:15 +0000 | [diff] [blame] | 26 | NamedStreamMap::NamedStreamMap() = default; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 27 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 28 | Error NamedStreamMap::load(BinaryStreamReader &Stream) { |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 29 | Mapping.clear(); |
| 30 | FinalizedHashTable.clear(); |
| 31 | FinalizedInfo.reset(); |
| 32 | |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 33 | uint32_t StringBufferSize; |
Zachary Turner | 695ed56 | 2017-02-28 00:04:07 +0000 | [diff] [blame] | 34 | if (auto EC = Stream.readInteger(StringBufferSize)) |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 35 | return joinErrors(std::move(EC), |
| 36 | make_error<RawError>(raw_error_code::corrupt_file, |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 37 | "Expected string buffer size")); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 38 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 39 | BinaryStreamRef StringsBuffer; |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 40 | if (auto EC = Stream.readStreamRef(StringsBuffer, StringBufferSize)) |
| 41 | return EC; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 42 | |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 43 | HashTable OffsetIndexMap; |
| 44 | if (auto EC = OffsetIndexMap.load(Stream)) |
| 45 | return EC; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 46 | |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 47 | uint32_t NameOffset; |
| 48 | uint32_t NameIndex; |
| 49 | for (const auto &Entry : OffsetIndexMap) { |
| 50 | std::tie(NameOffset, NameIndex) = Entry; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 51 | |
| 52 | // Compute the offset of the start of the string relative to the stream. |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 53 | BinaryStreamReader NameReader(StringsBuffer); |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 54 | NameReader.setOffset(NameOffset); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 55 | // Pump out our c-string from the stream. |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 56 | StringRef Str; |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 57 | if (auto EC = NameReader.readCString(Str)) |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 58 | return joinErrors(std::move(EC), |
| 59 | make_error<RawError>(raw_error_code::corrupt_file, |
| 60 | "Expected name map name")); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 61 | |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 62 | // Add this to a string-map from name to stream number. |
| 63 | Mapping.insert({Str, NameIndex}); |
| 64 | } |
| 65 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 66 | return Error::success(); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 69 | Error NamedStreamMap::commit(BinaryStreamWriter &Writer) const { |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 70 | assert(FinalizedInfo.hasValue()); |
| 71 | |
| 72 | // The first field is the number of bytes of string data. |
Zachary Turner | 695ed56 | 2017-02-28 00:04:07 +0000 | [diff] [blame] | 73 | if (auto EC = Writer.writeInteger(FinalizedInfo->StringDataBytes)) |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 74 | return EC; |
| 75 | |
| 76 | // Now all of the string data itself. |
| 77 | for (const auto &Item : Mapping) { |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 78 | if (auto EC = Writer.writeCString(Item.getKey())) |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 79 | return EC; |
| 80 | } |
| 81 | |
| 82 | // And finally the Offset Index map. |
| 83 | if (auto EC = FinalizedHashTable.commit(Writer)) |
| 84 | return EC; |
| 85 | |
| 86 | return Error::success(); |
| 87 | } |
| 88 | |
| 89 | uint32_t NamedStreamMap::finalize() { |
| 90 | if (FinalizedInfo.hasValue()) |
| 91 | return FinalizedInfo->SerializedLength; |
| 92 | |
| 93 | // Build the finalized hash table. |
| 94 | FinalizedHashTable.clear(); |
| 95 | FinalizedInfo.emplace(); |
| 96 | for (const auto &Item : Mapping) { |
| 97 | FinalizedHashTable.set(FinalizedInfo->StringDataBytes, Item.getValue()); |
| 98 | FinalizedInfo->StringDataBytes += Item.getKeyLength() + 1; |
| 99 | } |
| 100 | |
| 101 | // Number of bytes of string data. |
| 102 | FinalizedInfo->SerializedLength += sizeof(support::ulittle32_t); |
| 103 | // Followed by that many actual bytes of string data. |
| 104 | FinalizedInfo->SerializedLength += FinalizedInfo->StringDataBytes; |
| 105 | // Followed by the mapping from Offset to Index. |
| 106 | FinalizedInfo->SerializedLength += |
| 107 | FinalizedHashTable.calculateSerializedLength(); |
| 108 | return FinalizedInfo->SerializedLength; |
| 109 | } |
| 110 | |
Zachary Turner | f04d6e8 | 2017-01-20 22:41:15 +0000 | [diff] [blame] | 111 | iterator_range<StringMapConstIterator<uint32_t>> |
| 112 | NamedStreamMap::entries() const { |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 113 | return make_range<StringMapConstIterator<uint32_t>>(Mapping.begin(), |
| 114 | Mapping.end()); |
Zachary Turner | 85ed80b | 2016-05-25 03:43:17 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Zachary Turner | 02278ce | 2017-03-16 20:18:41 +0000 | [diff] [blame] | 117 | uint32_t NamedStreamMap::size() const { return Mapping.size(); } |
| 118 | |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 119 | bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const { |
| 120 | auto Iter = Mapping.find(Stream); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 121 | if (Iter == Mapping.end()) |
| 122 | return false; |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 123 | StreamNo = Iter->second; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 124 | return true; |
| 125 | } |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 126 | |
| 127 | void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) { |
| 128 | FinalizedInfo.reset(); |
| 129 | Mapping[Stream] = StreamNo; |
| 130 | } |
| 131 | |
| 132 | void NamedStreamMap::remove(StringRef Stream) { |
| 133 | FinalizedInfo.reset(); |
| 134 | Mapping.erase(Stream); |
| 135 | } |