blob: 6076b10c5c9b106ef8ea6a5a334201229ca285d8 [file] [log] [blame]
Eugene Zelenko4fcfc192017-06-30 23:06:03 +00001//===- NamedStreamMap.cpp - PDB Named Stream Map --------------------------===//
Zachary Turnerf34e0162016-04-26 16:20:00 +00002//
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 McCarthy6b6b8c42017-01-25 22:38:55 +000010#include "llvm/DebugInfo/PDB/Native/NamedStreamMap.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000011#include "llvm/ADT/StringMap.h"
12#include "llvm/ADT/StringRef.h"
13#include "llvm/ADT/iterator_range.h"
Zachary Turnercafd4762018-02-16 20:46:04 +000014#include "llvm/DebugInfo/PDB/Native/Hash.h"
Adrian McCarthy6b6b8c42017-01-25 22:38:55 +000015#include "llvm/DebugInfo/PDB/Native/HashTable.h"
16#include "llvm/DebugInfo/PDB/Native/RawError.h"
Zachary Turnerd9dc2822017-03-02 20:52:51 +000017#include "llvm/Support/BinaryStreamReader.h"
Eugene Zelenko4fcfc192017-06-30 23:06:03 +000018#include "llvm/Support/BinaryStreamRef.h"
19#include "llvm/Support/BinaryStreamWriter.h"
20#include "llvm/Support/Endian.h"
Eugene Zelenko570e39a2016-11-23 23:16:32 +000021#include "llvm/Support/Error.h"
22#include <algorithm>
Eugene Zelenko4fcfc192017-06-30 23:06:03 +000023#include <cassert>
Eugene Zelenko570e39a2016-11-23 23:16:32 +000024#include <cstdint>
Eugene Zelenko4fcfc192017-06-30 23:06:03 +000025#include <tuple>
Zachary Turnerf34e0162016-04-26 16:20:00 +000026
27using namespace llvm;
Zachary Turner2f09b502016-04-29 17:28:47 +000028using namespace llvm::pdb;
Zachary Turnerf34e0162016-04-26 16:20:00 +000029
Zachary Turnerebf03f62018-03-15 17:38:26 +000030NamedStreamMapTraits::NamedStreamMapTraits(NamedStreamMap &NS) : NS(&NS) {}
Zachary Turner1affd802017-06-25 03:51:42 +000031
Zachary Turnerebf03f62018-03-15 17:38:26 +000032uint16_t NamedStreamMapTraits::hashLookupKey(StringRef S) const {
33 // In the reference implementation, this uses
34 // HASH Hasher<ULONG*, USHORT*>::hashPbCb(PB pb, size_t cb, ULONG ulMod).
35 // Here, the type HASH is a typedef of unsigned short.
36 // ** It is not a bug that we truncate the result of hashStringV1, in fact
37 // it is a bug if we do not! **
38 return static_cast<uint16_t>(hashStringV1(S));
39}
40
41StringRef NamedStreamMapTraits::storageKeyToLookupKey(uint32_t Offset) const {
42 return NS->getString(Offset);
43}
44
45uint32_t NamedStreamMapTraits::lookupKeyToStorageKey(StringRef S) {
46 return NS->appendStringData(S);
47}
48
49NamedStreamMap::NamedStreamMap()
50 : HashTraits(*this), OffsetIndexMap(HashTraits) {}
Zachary Turnerf34e0162016-04-26 16:20:00 +000051
Zachary Turner120faca2017-02-27 22:11:43 +000052Error NamedStreamMap::load(BinaryStreamReader &Stream) {
Zachary Turner11036a92017-01-19 23:31:24 +000053 uint32_t StringBufferSize;
Zachary Turner695ed562017-02-28 00:04:07 +000054 if (auto EC = Stream.readInteger(StringBufferSize))
David Majnemer836937e2016-05-27 16:16:56 +000055 return joinErrors(std::move(EC),
56 make_error<RawError>(raw_error_code::corrupt_file,
Zachary Turner11036a92017-01-19 23:31:24 +000057 "Expected string buffer size"));
Zachary Turnerf34e0162016-04-26 16:20:00 +000058
Zachary Turnercafd4762018-02-16 20:46:04 +000059 StringRef Buffer;
60 if (auto EC = Stream.readFixedString(Buffer, StringBufferSize))
Zachary Turner11036a92017-01-19 23:31:24 +000061 return EC;
Zachary Turnercafd4762018-02-16 20:46:04 +000062 NamesBuffer.assign(Buffer.begin(), Buffer.end());
Zachary Turnerf34e0162016-04-26 16:20:00 +000063
Zachary Turnercafd4762018-02-16 20:46:04 +000064 return OffsetIndexMap.load(Stream);
Zachary Turnerf34e0162016-04-26 16:20:00 +000065}
66
Zachary Turner120faca2017-02-27 22:11:43 +000067Error NamedStreamMap::commit(BinaryStreamWriter &Writer) const {
Zachary Turner60667ca2017-01-20 22:41:40 +000068 // The first field is the number of bytes of string data.
Zachary Turnercafd4762018-02-16 20:46:04 +000069 if (auto EC = Writer.writeInteger<uint32_t>(NamesBuffer.size()))
Zachary Turner60667ca2017-01-20 22:41:40 +000070 return EC;
71
Zachary Turnercafd4762018-02-16 20:46:04 +000072 // Then the actual string data.
73 StringRef Data(NamesBuffer.data(), NamesBuffer.size());
74 if (auto EC = Writer.writeFixedString(Data))
75 return EC;
Zachary Turner60667ca2017-01-20 22:41:40 +000076
77 // And finally the Offset Index map.
Zachary Turnercafd4762018-02-16 20:46:04 +000078 if (auto EC = OffsetIndexMap.commit(Writer))
Zachary Turner60667ca2017-01-20 22:41:40 +000079 return EC;
80
81 return Error::success();
82}
83
Zachary Turnercafd4762018-02-16 20:46:04 +000084uint32_t NamedStreamMap::calculateSerializedLength() const {
85 return sizeof(uint32_t) // String data size
86 + NamesBuffer.size() // String data
87 + OffsetIndexMap.calculateSerializedLength(); // Offset Index Map
Zachary Turner60667ca2017-01-20 22:41:40 +000088}
89
Zachary Turnercafd4762018-02-16 20:46:04 +000090uint32_t NamedStreamMap::size() const { return OffsetIndexMap.size(); }
91
92StringRef NamedStreamMap::getString(uint32_t Offset) const {
93 assert(NamesBuffer.size() > Offset);
94 return StringRef(NamesBuffer.data() + Offset);
Zachary Turner85ed80b2016-05-25 03:43:17 +000095}
96
Zachary Turnercafd4762018-02-16 20:46:04 +000097uint32_t NamedStreamMap::hashString(uint32_t Offset) const {
98 return hashStringV1(getString(Offset));
99}
Zachary Turner02278ce2017-03-16 20:18:41 +0000100
Zachary Turner60667ca2017-01-20 22:41:40 +0000101bool NamedStreamMap::get(StringRef Stream, uint32_t &StreamNo) const {
Zachary Turnerebf03f62018-03-15 17:38:26 +0000102 auto Iter = OffsetIndexMap.find_as(Stream);
Zachary Turnercafd4762018-02-16 20:46:04 +0000103 if (Iter == OffsetIndexMap.end())
Zachary Turnerf34e0162016-04-26 16:20:00 +0000104 return false;
Zachary Turnercafd4762018-02-16 20:46:04 +0000105 StreamNo = (*Iter).second;
Zachary Turnerf34e0162016-04-26 16:20:00 +0000106 return true;
107}
Zachary Turner60667ca2017-01-20 22:41:40 +0000108
Zachary Turnercafd4762018-02-16 20:46:04 +0000109StringMap<uint32_t> NamedStreamMap::entries() const {
110 StringMap<uint32_t> Result;
111 for (const auto &Entry : OffsetIndexMap) {
112 StringRef Stream(NamesBuffer.data() + Entry.first);
113 Result.try_emplace(Stream, Entry.second);
114 }
115 return Result;
Zachary Turner60667ca2017-01-20 22:41:40 +0000116}
117
Zachary Turnercafd4762018-02-16 20:46:04 +0000118uint32_t NamedStreamMap::appendStringData(StringRef S) {
119 uint32_t Offset = NamesBuffer.size();
120 NamesBuffer.insert(NamesBuffer.end(), S.begin(), S.end());
121 NamesBuffer.push_back('\0');
122 return Offset;
123}
124
125void NamedStreamMap::set(StringRef Stream, uint32_t StreamNo) {
Zachary Turnerebf03f62018-03-15 17:38:26 +0000126 OffsetIndexMap.set_as(Stream, support::ulittle32_t(StreamNo));
Zachary Turner60667ca2017-01-20 22:41:40 +0000127}