Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 1 | //===- NameMapBuilder.cpp - PDB Name Map Builder ----------------*- 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/NameMapBuilder.h" |
| 11 | |
Zachary Turner | a010f5c | 2016-07-29 18:24:26 +0000 | [diff] [blame] | 12 | #include "llvm/DebugInfo/MSF/StreamWriter.h" |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/PDB/Raw/NameMap.h" |
Zachary Turner | 5e534c7 | 2016-07-15 22:17:08 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Endian.h" |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 15 | |
| 16 | using namespace llvm; |
| 17 | using namespace llvm::pdb; |
| 18 | |
| 19 | NameMapBuilder::NameMapBuilder() {} |
| 20 | |
Zachary Turner | 5e534c7 | 2016-07-15 22:17:08 +0000 | [diff] [blame] | 21 | void NameMapBuilder::addMapping(StringRef Name, uint32_t Mapping) { |
| 22 | StringDataBytes += Name.size() + 1; |
| 23 | Map.insert({Name, Mapping}); |
| 24 | } |
| 25 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 26 | Expected<std::unique_ptr<NameMap>> NameMapBuilder::build() { |
Zachary Turner | 5e534c7 | 2016-07-15 22:17:08 +0000 | [diff] [blame] | 27 | auto Result = llvm::make_unique<NameMap>(); |
| 28 | Result->Mapping = Map; |
| 29 | return std::move(Result); |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | uint32_t NameMapBuilder::calculateSerializedLength() const { |
Zachary Turner | 5e534c7 | 2016-07-15 22:17:08 +0000 | [diff] [blame] | 33 | uint32_t TotalLength = 0; |
| 34 | |
| 35 | TotalLength += sizeof(support::ulittle32_t); // StringDataBytes value |
| 36 | TotalLength += StringDataBytes; // actual string data |
| 37 | |
| 38 | TotalLength += sizeof(support::ulittle32_t); // Hash Size |
| 39 | TotalLength += sizeof(support::ulittle32_t); // Max Number of Strings |
| 40 | TotalLength += sizeof(support::ulittle32_t); // Num Present Words |
| 41 | // One bitmask word for each present entry |
| 42 | TotalLength += Map.size() * sizeof(support::ulittle32_t); |
| 43 | TotalLength += sizeof(support::ulittle32_t); // Num Deleted Words |
| 44 | |
| 45 | // For each present word, which we are treating as equivalent to the number of |
| 46 | // entries in the table, we have a pair of integers. An offset into the |
| 47 | // string data, and a corresponding stream number. |
| 48 | TotalLength += Map.size() * 2 * sizeof(support::ulittle32_t); |
| 49 | |
| 50 | return TotalLength; |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 51 | } |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 52 | |
| 53 | Error NameMapBuilder::commit(msf::StreamWriter &Writer) const { |
| 54 | // The first field is the number of bytes of string data. So add |
| 55 | // up the length of all strings plus a null terminator for each |
| 56 | // one. |
| 57 | uint32_t NumBytes = 0; |
| 58 | for (auto B = Map.begin(), E = Map.end(); B != E; ++B) { |
| 59 | NumBytes += B->getKeyLength() + 1; |
| 60 | } |
| 61 | |
| 62 | if (auto EC = Writer.writeInteger(NumBytes)) // Number of bytes of string data |
| 63 | return EC; |
| 64 | // Now all of the string data itself. |
| 65 | for (auto B = Map.begin(), E = Map.end(); B != E; ++B) { |
| 66 | if (auto EC = Writer.writeZeroString(B->getKey())) |
| 67 | return EC; |
| 68 | } |
| 69 | |
| 70 | if (auto EC = Writer.writeInteger(Map.size())) // Hash Size |
| 71 | return EC; |
| 72 | |
| 73 | if (auto EC = Writer.writeInteger(Map.size())) // Max Number of Strings |
| 74 | return EC; |
| 75 | |
| 76 | if (auto EC = Writer.writeInteger(Map.size())) // Num Present Words |
| 77 | return EC; |
| 78 | |
| 79 | // For each entry in the mapping, write a bit mask which represents a bucket |
| 80 | // to store it in. We don't use this, so the value we write isn't important |
| 81 | // to us, it just has to be there. |
| 82 | for (auto B = Map.begin(), E = Map.end(); B != E; ++B) { |
| 83 | if (auto EC = Writer.writeInteger(1U)) |
| 84 | return EC; |
| 85 | } |
| 86 | |
| 87 | if (auto EC = Writer.writeInteger(0U)) // Num Deleted Words |
| 88 | return EC; |
| 89 | |
| 90 | // Mappings of each word. |
| 91 | uint32_t OffsetSoFar = 0; |
| 92 | for (auto B = Map.begin(), E = Map.end(); B != E; ++B) { |
| 93 | // This is a list of key value pairs where the key is the offset into the |
| 94 | // strings buffer, and the value is a stream number. Write each pair. |
| 95 | if (auto EC = Writer.writeInteger(OffsetSoFar)) |
| 96 | return EC; |
| 97 | |
| 98 | if (auto EC = Writer.writeInteger(B->second)) |
| 99 | return EC; |
| 100 | |
| 101 | OffsetSoFar += B->getKeyLength() + 1; |
| 102 | } |
| 103 | |
| 104 | return Error::success(); |
| 105 | } |