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 | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame^] | 10 | #include "llvm/DebugInfo/PDB/Raw/NameMap.h" |
| 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" |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/MSF/StreamReader.h" |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame^] | 17 | #include "llvm/DebugInfo/PDB/Raw/HashTable.h" |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/PDB/Raw/RawError.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 | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 24 | using namespace llvm::msf; |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 25 | using namespace llvm::pdb; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 26 | |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 27 | NameMap::NameMap() = default; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 28 | |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 29 | Error NameMap::load(StreamReader &Stream) { |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame^] | 30 | uint32_t StringBufferSize; |
| 31 | if (auto EC = Stream.readInteger(StringBufferSize)) |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 32 | return joinErrors(std::move(EC), |
| 33 | make_error<RawError>(raw_error_code::corrupt_file, |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame^] | 34 | "Expected string buffer size")); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 35 | |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame^] | 36 | msf::ReadableStreamRef StringsBuffer; |
| 37 | if (auto EC = Stream.readStreamRef(StringsBuffer, StringBufferSize)) |
| 38 | return EC; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 39 | |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame^] | 40 | HashTable OffsetIndexMap; |
| 41 | if (auto EC = OffsetIndexMap.load(Stream)) |
| 42 | return EC; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 43 | |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame^] | 44 | uint32_t NameOffset; |
| 45 | uint32_t NameIndex; |
| 46 | for (const auto &Entry : OffsetIndexMap) { |
| 47 | std::tie(NameOffset, NameIndex) = Entry; |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 48 | |
| 49 | // Compute the offset of the start of the string relative to the stream. |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame^] | 50 | msf::StreamReader NameReader(StringsBuffer); |
| 51 | NameReader.setOffset(NameOffset); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 52 | // Pump out our c-string from the stream. |
Zachary Turner | 8dbe362 | 2016-05-27 01:54:44 +0000 | [diff] [blame] | 53 | StringRef Str; |
Zachary Turner | 11036a9 | 2017-01-19 23:31:24 +0000 | [diff] [blame^] | 54 | if (auto EC = NameReader.readZeroString(Str)) |
David Majnemer | 836937e | 2016-05-27 16:16:56 +0000 | [diff] [blame] | 55 | return joinErrors(std::move(EC), |
| 56 | make_error<RawError>(raw_error_code::corrupt_file, |
| 57 | "Expected name map name")); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 58 | |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 59 | // Add this to a string-map from name to stream number. |
| 60 | Mapping.insert({Str, NameIndex}); |
| 61 | } |
| 62 | |
Zachary Turner | 819e77d | 2016-05-06 20:51:57 +0000 | [diff] [blame] | 63 | return Error::success(); |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Zachary Turner | 85ed80b | 2016-05-25 03:43:17 +0000 | [diff] [blame] | 66 | iterator_range<StringMapConstIterator<uint32_t>> NameMap::entries() const { |
Eugene Zelenko | 570e39a | 2016-11-23 23:16:32 +0000 | [diff] [blame] | 67 | return make_range<StringMapConstIterator<uint32_t>>(Mapping.begin(), |
| 68 | Mapping.end()); |
Zachary Turner | 85ed80b | 2016-05-25 03:43:17 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Zachary Turner | 2f09b50 | 2016-04-29 17:28:47 +0000 | [diff] [blame] | 71 | bool NameMap::tryGetValue(StringRef Name, uint32_t &Value) const { |
Zachary Turner | f34e016 | 2016-04-26 16:20:00 +0000 | [diff] [blame] | 72 | auto Iter = Mapping.find(Name); |
| 73 | if (Iter == Mapping.end()) |
| 74 | return false; |
| 75 | Value = Iter->second; |
| 76 | return true; |
| 77 | } |