Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 1 | //===- NameMap.cpp - PDB Name 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 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/PDB/Raw/NameMap.h" |
David Majnemer | 36b7b08 | 2016-06-04 22:47:39 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/SparseBitVector.h" |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame^] | 12 | #include "llvm/DebugInfo/MSF/StreamReader.h" |
| 13 | #include "llvm/DebugInfo/MSF/StreamWriter.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 17 | using namespace llvm::msf; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 18 | using namespace llvm::pdb; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 19 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 20 | NameMap::NameMap() {} |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 21 | |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 22 | Error NameMap::load(StreamReader &Stream) { |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame] | 23 | |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 24 | // This is some sort of weird string-set/hash table encoded in the stream. |
| 25 | // It starts with the number of bytes in the table. |
| 26 | uint32_t NumberOfBytes; |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 27 | if (auto EC = Stream.readInteger(NumberOfBytes)) |
| 28 | return joinErrors(std::move(EC), |
| 29 | make_error<RawError>(raw_error_code::corrupt_file, |
| 30 | "Expected name map length")); |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 31 | if (Stream.bytesRemaining() < NumberOfBytes) |
| 32 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 33 | "Invalid name map length"); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 34 | |
| 35 | // Following that field is the starting offset of strings in the name table. |
| 36 | uint32_t StringsOffset = Stream.getOffset(); |
| 37 | Stream.setOffset(StringsOffset + NumberOfBytes); |
| 38 | |
| 39 | // This appears to be equivalent to the total number of strings *actually* |
| 40 | // in the name table. |
| 41 | uint32_t HashSize; |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 42 | if (auto EC = Stream.readInteger(HashSize)) |
| 43 | return joinErrors(std::move(EC), |
| 44 | make_error<RawError>(raw_error_code::corrupt_file, |
| 45 | "Expected name map hash size")); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 46 | |
| 47 | // This appears to be an upper bound on the number of strings in the name |
| 48 | // table. |
| 49 | uint32_t MaxNumberOfStrings; |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 50 | if (auto EC = Stream.readInteger(MaxNumberOfStrings)) |
| 51 | return joinErrors(std::move(EC), |
| 52 | make_error<RawError>(raw_error_code::corrupt_file, |
| 53 | "Expected name map max strings")); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 54 | |
David Majnemer | 328b6d3 | 2016-05-28 18:03:37 +0000 | [diff] [blame] | 55 | if (MaxNumberOfStrings > (UINT32_MAX / sizeof(uint32_t))) |
| 56 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 57 | "Implausible number of strings"); |
| 58 | |
| 59 | const uint32_t MaxNumberOfWords = UINT32_MAX / (sizeof(uint32_t) * 8); |
David Majnemer | 869631f | 2016-05-28 05:59:25 +0000 | [diff] [blame] | 60 | |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 61 | // This appears to be a hash table which uses bitfields to determine whether |
| 62 | // or not a bucket is 'present'. |
| 63 | uint32_t NumPresentWords; |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 64 | if (auto EC = Stream.readInteger(NumPresentWords)) |
| 65 | return joinErrors(std::move(EC), |
| 66 | make_error<RawError>(raw_error_code::corrupt_file, |
| 67 | "Expected name map num words")); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 68 | |
David Majnemer | 869631f | 2016-05-28 05:59:25 +0000 | [diff] [blame] | 69 | if (NumPresentWords > MaxNumberOfWords) |
| 70 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 71 | "Number of present words is too large"); |
| 72 | |
David Majnemer | 36b7b08 | 2016-06-04 22:47:39 +0000 | [diff] [blame] | 73 | SparseBitVector<> Present; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 74 | for (uint32_t I = 0; I != NumPresentWords; ++I) { |
| 75 | uint32_t Word; |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 76 | if (auto EC = Stream.readInteger(Word)) |
| 77 | return joinErrors(std::move(EC), |
| 78 | make_error<RawError>(raw_error_code::corrupt_file, |
| 79 | "Expected name map word")); |
David Majnemer | 36b7b08 | 2016-06-04 22:47:39 +0000 | [diff] [blame] | 80 | for (unsigned Idx = 0; Idx < 32; ++Idx) |
| 81 | if (Word & (1U << Idx)) |
| 82 | Present.set((I * 32) + Idx); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // This appears to be a hash table which uses bitfields to determine whether |
| 86 | // or not a bucket is 'deleted'. |
| 87 | uint32_t NumDeletedWords; |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 88 | if (auto EC = Stream.readInteger(NumDeletedWords)) |
| 89 | return joinErrors( |
| 90 | std::move(EC), |
| 91 | make_error<RawError>(raw_error_code::corrupt_file, |
| 92 | "Expected name map num deleted words")); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 93 | |
David Majnemer | 869631f | 2016-05-28 05:59:25 +0000 | [diff] [blame] | 94 | if (NumDeletedWords > MaxNumberOfWords) |
| 95 | return make_error<RawError>(raw_error_code::corrupt_file, |
| 96 | "Number of deleted words is too large"); |
| 97 | |
David Majnemer | 36b7b08 | 2016-06-04 22:47:39 +0000 | [diff] [blame] | 98 | SparseBitVector<> Deleted; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 99 | for (uint32_t I = 0; I != NumDeletedWords; ++I) { |
| 100 | uint32_t Word; |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 101 | if (auto EC = Stream.readInteger(Word)) |
| 102 | return joinErrors(std::move(EC), |
| 103 | make_error<RawError>(raw_error_code::corrupt_file, |
David Majnemer | 36b7b08 | 2016-06-04 22:47:39 +0000 | [diff] [blame] | 104 | "Expected name map word")); |
| 105 | for (unsigned Idx = 0; Idx < 32; ++Idx) |
| 106 | if (Word & (1U << Idx)) |
| 107 | Deleted.set((I * 32) + Idx); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 108 | } |
| 109 | |
David Majnemer | 36b7b08 | 2016-06-04 22:47:39 +0000 | [diff] [blame] | 110 | for (unsigned I : Present) { |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 111 | // For all present entries, dump out their mapping. |
David Majnemer | 36b7b08 | 2016-06-04 22:47:39 +0000 | [diff] [blame] | 112 | (void)I; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 113 | |
| 114 | // This appears to be an offset relative to the start of the strings. |
| 115 | // It tells us where the null-terminated string begins. |
| 116 | uint32_t NameOffset; |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 117 | if (auto EC = Stream.readInteger(NameOffset)) |
| 118 | return joinErrors(std::move(EC), |
| 119 | make_error<RawError>(raw_error_code::corrupt_file, |
| 120 | "Expected name map name offset")); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 121 | |
| 122 | // This appears to be a stream number into the stream directory. |
| 123 | uint32_t NameIndex; |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 124 | if (auto EC = Stream.readInteger(NameIndex)) |
| 125 | return joinErrors(std::move(EC), |
| 126 | make_error<RawError>(raw_error_code::corrupt_file, |
| 127 | "Expected name map name index")); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 128 | |
| 129 | // Compute the offset of the start of the string relative to the stream. |
| 130 | uint32_t StringOffset = StringsOffset + NameOffset; |
| 131 | uint32_t OldOffset = Stream.getOffset(); |
| 132 | // Pump out our c-string from the stream. |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 133 | StringRef Str; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 134 | Stream.setOffset(StringOffset); |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 135 | if (auto EC = Stream.readZeroString(Str)) |
| 136 | return joinErrors(std::move(EC), |
| 137 | make_error<RawError>(raw_error_code::corrupt_file, |
| 138 | "Expected name map name")); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 139 | |
| 140 | Stream.setOffset(OldOffset); |
| 141 | // Add this to a string-map from name to stream number. |
| 142 | Mapping.insert({Str, NameIndex}); |
| 143 | } |
| 144 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 145 | return Error::success(); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Zachary Turner | 85ed80b | 2016-05-25 03:43:17 +0000 | [diff] [blame] | 148 | iterator_range<StringMapConstIterator<uint32_t>> NameMap::entries() const { |
| 149 | return llvm::make_range<StringMapConstIterator<uint32_t>>(Mapping.begin(), |
| 150 | Mapping.end()); |
| 151 | } |
| 152 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 153 | bool NameMap::tryGetValue(StringRef Name, uint32_t &Value) const { |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 154 | auto Iter = Mapping.find(Name); |
| 155 | if (Iter == Mapping.end()) |
| 156 | return false; |
| 157 | Value = Iter->second; |
| 158 | return true; |
| 159 | } |