Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 1 | //===- PDBNameMap.cpp - PDB Name Map ----------------------------*- C++ -*-===// |
| 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 | |
| 10 | #include "llvm/DebugInfo/PDB/Raw/PDBNameMap.h" |
| 11 | #include "llvm/ADT/BitVector.h" |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame^] | 12 | #include "llvm/DebugInfo/PDB/Raw/StreamReader.h" |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace llvm; |
| 15 | |
| 16 | PDBNameMap::PDBNameMap() {} |
| 17 | |
Zachary Turner | 6ba65de | 2016-04-29 17:22:58 +0000 | [diff] [blame^] | 18 | std::error_code PDBNameMap::load(StreamReader &Stream) { |
| 19 | |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 20 | // This is some sort of weird string-set/hash table encoded in the stream. |
| 21 | // It starts with the number of bytes in the table. |
| 22 | uint32_t NumberOfBytes; |
| 23 | Stream.readInteger(NumberOfBytes); |
| 24 | |
| 25 | // Following that field is the starting offset of strings in the name table. |
| 26 | uint32_t StringsOffset = Stream.getOffset(); |
| 27 | Stream.setOffset(StringsOffset + NumberOfBytes); |
| 28 | |
| 29 | // This appears to be equivalent to the total number of strings *actually* |
| 30 | // in the name table. |
| 31 | uint32_t HashSize; |
| 32 | Stream.readInteger(HashSize); |
| 33 | |
| 34 | // This appears to be an upper bound on the number of strings in the name |
| 35 | // table. |
| 36 | uint32_t MaxNumberOfStrings; |
| 37 | Stream.readInteger(MaxNumberOfStrings); |
| 38 | |
| 39 | // This appears to be a hash table which uses bitfields to determine whether |
| 40 | // or not a bucket is 'present'. |
| 41 | uint32_t NumPresentWords; |
| 42 | Stream.readInteger(NumPresentWords); |
| 43 | |
| 44 | // Store all the 'present' bits in a vector for later processing. |
| 45 | SmallVector<uint32_t, 1> PresentWords; |
| 46 | for (uint32_t I = 0; I != NumPresentWords; ++I) { |
| 47 | uint32_t Word; |
| 48 | Stream.readInteger(Word); |
| 49 | PresentWords.push_back(Word); |
| 50 | } |
| 51 | |
| 52 | // This appears to be a hash table which uses bitfields to determine whether |
| 53 | // or not a bucket is 'deleted'. |
| 54 | uint32_t NumDeletedWords; |
| 55 | Stream.readInteger(NumDeletedWords); |
| 56 | |
| 57 | // Store all the 'deleted' bits in a vector for later processing. |
| 58 | SmallVector<uint32_t, 1> DeletedWords; |
| 59 | for (uint32_t I = 0; I != NumDeletedWords; ++I) { |
| 60 | uint32_t Word; |
| 61 | Stream.readInteger(Word); |
| 62 | DeletedWords.push_back(Word); |
| 63 | } |
| 64 | |
| 65 | BitVector Present(MaxNumberOfStrings, false); |
| 66 | if (!PresentWords.empty()) |
| 67 | Present.setBitsInMask(PresentWords.data(), PresentWords.size()); |
| 68 | BitVector Deleted(MaxNumberOfStrings, false); |
| 69 | if (!DeletedWords.empty()) |
| 70 | Deleted.setBitsInMask(DeletedWords.data(), DeletedWords.size()); |
| 71 | |
| 72 | for (uint32_t I = 0; I < MaxNumberOfStrings; ++I) { |
| 73 | if (!Present.test(I)) |
| 74 | continue; |
| 75 | |
| 76 | // For all present entries, dump out their mapping. |
| 77 | |
| 78 | // This appears to be an offset relative to the start of the strings. |
| 79 | // It tells us where the null-terminated string begins. |
| 80 | uint32_t NameOffset; |
| 81 | Stream.readInteger(NameOffset); |
| 82 | |
| 83 | // This appears to be a stream number into the stream directory. |
| 84 | uint32_t NameIndex; |
| 85 | Stream.readInteger(NameIndex); |
| 86 | |
| 87 | // Compute the offset of the start of the string relative to the stream. |
| 88 | uint32_t StringOffset = StringsOffset + NameOffset; |
| 89 | uint32_t OldOffset = Stream.getOffset(); |
| 90 | // Pump out our c-string from the stream. |
| 91 | std::string Str; |
| 92 | Stream.setOffset(StringOffset); |
| 93 | Stream.readZeroString(Str); |
| 94 | |
| 95 | Stream.setOffset(OldOffset); |
| 96 | // Add this to a string-map from name to stream number. |
| 97 | Mapping.insert({Str, NameIndex}); |
| 98 | } |
| 99 | |
| 100 | return std::error_code(); |
| 101 | } |
| 102 | |
| 103 | bool PDBNameMap::tryGetValue(StringRef Name, uint32_t &Value) const { |
| 104 | auto Iter = Mapping.find(Name); |
| 105 | if (Iter == Mapping.end()) |
| 106 | return false; |
| 107 | Value = Iter->second; |
| 108 | return true; |
| 109 | } |