Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame] | 1 | //===- NamedStreamMap.cpp - PDB Named Stream Map --------------------------===// |
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" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/StringMap.h" |
| 12 | #include "llvm/ADT/StringRef.h" |
| 13 | #include "llvm/ADT/iterator_range.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/PDB/Native/HashTable.h" |
| 15 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 16 | #include "llvm/Support/BinaryStreamReader.h" |
Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame] | 17 | #include "llvm/Support/BinaryStreamRef.h" |
| 18 | #include "llvm/Support/BinaryStreamWriter.h" |
| 19 | #include "llvm/Support/Endian.h" |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Error.h" |
| 21 | #include <algorithm> |
Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame] | 22 | #include <cassert> |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 23 | #include <cstdint> |
Eugene Zelenko | 4fcfc19 | 2017-06-30 23:06:03 +0000 | [diff] [blame] | 24 | #include <tuple> |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 25 | |
| 26 | using namespace llvm; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 27 | using namespace llvm::pdb; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 28 | |
Zachary Turner | 1affd80 | 2017-06-25 03:51:42 +0000 | [diff] [blame] | 29 | // FIXME: This shouldn't be necessary, but if we insert the strings in any |
| 30 | // other order, cvdump cannot read the generated name map. This suggests that |
| 31 | // we may be using the wrong hash function. A closer inspection of the cvdump |
| 32 | // source code may reveal something, but for now this at least makes us work, |
| 33 | // even if only by accident. |
| 34 | static constexpr const char *OrderedStreamNames[] = {"/LinkInfo", "/names", |
| 35 | "/src/headerblock"}; |
| 36 | |
Zachary Turner | f04d6e8 | 2017-01-20 22:41:15 +0000 | [diff] [blame] | 37 | NamedStreamMap::NamedStreamMap() = default; |
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 | Error NamedStreamMap::load(BinaryStreamReader &Stream) { |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 40 | Mapping.clear(); |
| 41 | FinalizedHashTable.clear(); |
| 42 | FinalizedInfo.reset(); |
| 43 | |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 44 | uint32_t StringBufferSize; |
Zachary Turner | 695ed56 | 2017-02-28 00:04:07 +0000 | [diff] [blame] | 45 | if (auto EC = Stream.readInteger(StringBufferSize)) |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 46 | return joinErrors(std::move(EC), |
| 47 | make_error<RawError>(raw_error_code::corrupt_file, |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 48 | "Expected string buffer size")); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 49 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 50 | BinaryStreamRef StringsBuffer; |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 51 | if (auto EC = Stream.readStreamRef(StringsBuffer, StringBufferSize)) |
| 52 | return EC; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 53 | |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 54 | HashTable OffsetIndexMap; |
| 55 | if (auto EC = OffsetIndexMap.load(Stream)) |
| 56 | return EC; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 57 | |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 58 | uint32_t NameOffset; |
| 59 | uint32_t NameIndex; |
| 60 | for (const auto &Entry : OffsetIndexMap) { |
| 61 | std::tie(NameOffset, NameIndex) = Entry; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 62 | |
| 63 | // 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] | 64 | BinaryStreamReader NameReader(StringsBuffer); |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame] | 65 | NameReader.setOffset(NameOffset); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 66 | // Pump out our c-string from the stream. |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 67 | StringRef Str; |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 68 | if (auto EC = NameReader.readCString(Str)) |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 69 | return joinErrors(std::move(EC), |
| 70 | make_error<RawError>(raw_error_code::corrupt_file, |
| 71 | "Expected name map name")); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 72 | |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 73 | // Add this to a string-map from name to stream number. |
| 74 | Mapping.insert({Str, NameIndex}); |
| 75 | } |
| 76 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 77 | return Error::success(); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Zachary Turner | 120faca | 2017-02-27 22:11:43 +0000 | [diff] [blame] | 80 | Error NamedStreamMap::commit(BinaryStreamWriter &Writer) const { |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 81 | assert(FinalizedInfo.hasValue()); |
| 82 | |
| 83 | // The first field is the number of bytes of string data. |
Zachary Turner | 695ed56 | 2017-02-28 00:04:07 +0000 | [diff] [blame] | 84 | if (auto EC = Writer.writeInteger(FinalizedInfo->StringDataBytes)) |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 85 | return EC; |
| 86 | |
Zachary Turner | 1affd80 | 2017-06-25 03:51:42 +0000 | [diff] [blame] | 87 | for (const auto &Name : OrderedStreamNames) { |
| 88 | auto Item = Mapping.find(Name); |
Zachary Turner | 3a11fdf | 2017-07-07 20:25:39 +0000 | [diff] [blame] | 89 | if (Item == Mapping.end()) |
| 90 | continue; |
Zachary Turner | 1affd80 | 2017-06-25 03:51:42 +0000 | [diff] [blame] | 91 | if (auto EC = Writer.writeCString(Item->getKey())) |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 92 | return EC; |
| 93 | } |
| 94 | |
| 95 | // And finally the Offset Index map. |
| 96 | if (auto EC = FinalizedHashTable.commit(Writer)) |
| 97 | return EC; |
| 98 | |
| 99 | return Error::success(); |
| 100 | } |
| 101 | |
| 102 | uint32_t NamedStreamMap::finalize() { |
| 103 | if (FinalizedInfo.hasValue()) |
| 104 | return FinalizedInfo->SerializedLength; |
| 105 | |
| 106 | // Build the finalized hash table. |
| 107 | FinalizedHashTable.clear(); |
| 108 | FinalizedInfo.emplace(); |
Zachary Turner | 1affd80 | 2017-06-25 03:51:42 +0000 | [diff] [blame] | 109 | |
| 110 | for (const auto &Name : OrderedStreamNames) { |
| 111 | auto Item = Mapping.find(Name); |
Zachary Turner | 3a11fdf | 2017-07-07 20:25:39 +0000 | [diff] [blame] | 112 | if (Item == Mapping.end()) |
| 113 | continue; |
Zachary Turner | 1affd80 | 2017-06-25 03:51:42 +0000 | [diff] [blame] | 114 | FinalizedHashTable.set(FinalizedInfo->StringDataBytes, Item->getValue()); |
| 115 | FinalizedInfo->StringDataBytes += Item->getKeyLength() + 1; |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Number of bytes of string data. |
| 119 | FinalizedInfo->SerializedLength += sizeof(support::ulittle32_t); |
| 120 | // Followed by that many actual bytes of string data. |
| 121 | FinalizedInfo->SerializedLength += FinalizedInfo->StringDataBytes; |
| 122 | // Followed by the mapping from Offset to Index. |
| 123 | FinalizedInfo->SerializedLength += |
| 124 | FinalizedHashTable.calculateSerializedLength(); |
| 125 | return FinalizedInfo->SerializedLength; |
| 126 | } |
| 127 | |
Zachary Turner | f04d6e8 | 2017-01-20 22:41:15 +0000 | [diff] [blame] | 128 | iterator_range<StringMapConstIterator<uint32_t>> |
| 129 | NamedStreamMap::entries() const { |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 130 | return make_range<StringMapConstIterator<uint32_t>>(Mapping.begin(), |
| 131 | Mapping.end()); |
Zachary Turner | 85ed80b | 2016-05-25 03:43:17 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Zachary Turner | 02278ce | 2017-03-16 20:18:41 +0000 | [diff] [blame] | 134 | uint32_t NamedStreamMap::size() const { return Mapping.size(); } |
| 135 | |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 136 | bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const { |
| 137 | auto Iter = Mapping.find(Stream); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 138 | if (Iter == Mapping.end()) |
| 139 | return false; |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 140 | StreamNo = Iter->second; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 141 | return true; |
| 142 | } |
Zachary Turner | 60667ca | 2017-01-20 22:41:40 +0000 | [diff] [blame] | 143 | |
| 144 | void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) { |
| 145 | FinalizedInfo.reset(); |
| 146 | Mapping[Stream] = StreamNo; |
| 147 | } |
| 148 | |
| 149 | void NamedStreamMap::remove(StringRef Stream) { |
| 150 | FinalizedInfo.reset(); |
| 151 | Mapping.erase(Stream); |
| 152 | } |