Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 1 | //===- DbiStreamBuilder.cpp - PDB Dbi Stream Creation -----------*- 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/DbiStreamBuilder.h" |
| 11 | |
Rui Ueyama | f990404 | 2016-10-11 19:43:12 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/ArrayRef.h" |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/MSF/MappedBlockStream.h" |
| 15 | #include "llvm/DebugInfo/MSF/StreamWriter.h" |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/Raw/DbiStream.h" |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/PDB/Raw/RawError.h" |
| 18 | |
| 19 | using namespace llvm; |
| 20 | using namespace llvm::codeview; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 21 | using namespace llvm::msf; |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 22 | using namespace llvm::pdb; |
| 23 | |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | class ModiSubstreamBuilder {}; |
| 26 | } |
| 27 | |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 28 | DbiStreamBuilder::DbiStreamBuilder(msf::MSFBuilder &Msf) |
| 29 | : Msf(Msf), Allocator(Msf.getAllocator()), Age(1), BuildNumber(0), |
| 30 | PdbDllVersion(0), PdbDllRbld(0), Flags(0), MachineType(PDB_Machine::x86), |
Reid Kleckner | 5d0bc63 | 2016-10-11 20:02:57 +0000 | [diff] [blame] | 31 | Header(nullptr), DbgStreams((int)DbgHeaderType::Max) {} |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 32 | |
| 33 | void DbiStreamBuilder::setVersionHeader(PdbRaw_DbiVer V) { VerHeader = V; } |
| 34 | |
| 35 | void DbiStreamBuilder::setAge(uint32_t A) { Age = A; } |
| 36 | |
| 37 | void DbiStreamBuilder::setBuildNumber(uint16_t B) { BuildNumber = B; } |
| 38 | |
| 39 | void DbiStreamBuilder::setPdbDllVersion(uint16_t V) { PdbDllVersion = V; } |
| 40 | |
| 41 | void DbiStreamBuilder::setPdbDllRbld(uint16_t R) { PdbDllRbld = R; } |
| 42 | |
| 43 | void DbiStreamBuilder::setFlags(uint16_t F) { Flags = F; } |
| 44 | |
| 45 | void DbiStreamBuilder::setMachineType(PDB_Machine M) { MachineType = M; } |
| 46 | |
Rui Ueyama | f990404 | 2016-10-11 19:43:12 +0000 | [diff] [blame] | 47 | Error DbiStreamBuilder::addDbgStream(pdb::DbgHeaderType Type, |
| 48 | ArrayRef<uint8_t> Data) { |
| 49 | if (DbgStreams[(int)Type].StreamNumber) |
| 50 | return make_error<RawError>(raw_error_code::duplicate_entry, |
| 51 | "The specified stream type already exists"); |
| 52 | auto ExpectedIndex = Msf.addStream(Data.size()); |
| 53 | if (!ExpectedIndex) |
| 54 | return ExpectedIndex.takeError(); |
| 55 | uint32_t Index = std::move(*ExpectedIndex); |
| 56 | DbgStreams[(int)Type].Data = Data; |
| 57 | DbgStreams[(int)Type].StreamNumber = Index; |
| 58 | return Error::success(); |
| 59 | } |
| 60 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 61 | uint32_t DbiStreamBuilder::calculateSerializedLength() const { |
| 62 | // For now we only support serializing the header. |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 63 | return sizeof(DbiStreamHeader) + calculateFileInfoSubstreamSize() + |
Rui Ueyama | 77be240 | 2016-10-29 00:56:44 +0000 | [diff] [blame^] | 64 | calculateModiSubstreamSize() + calculateDbgStreamsSize(); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | Error DbiStreamBuilder::addModuleInfo(StringRef ObjFile, StringRef Module) { |
| 68 | auto Entry = llvm::make_unique<ModuleInfo>(); |
| 69 | ModuleInfo *M = Entry.get(); |
| 70 | Entry->Mod = Module; |
| 71 | Entry->Obj = ObjFile; |
| 72 | auto Result = ModuleInfos.insert(std::make_pair(Module, std::move(Entry))); |
| 73 | if (!Result.second) |
| 74 | return make_error<RawError>(raw_error_code::duplicate_entry, |
| 75 | "The specified module already exists"); |
| 76 | ModuleInfoList.push_back(M); |
| 77 | return Error::success(); |
| 78 | } |
| 79 | |
| 80 | Error DbiStreamBuilder::addModuleSourceFile(StringRef Module, StringRef File) { |
| 81 | auto ModIter = ModuleInfos.find(Module); |
| 82 | if (ModIter == ModuleInfos.end()) |
| 83 | return make_error<RawError>(raw_error_code::no_entry, |
| 84 | "The specified module was not found"); |
| 85 | uint32_t Index = SourceFileNames.size(); |
| 86 | SourceFileNames.insert(std::make_pair(File, Index)); |
| 87 | auto &ModEntry = *ModIter; |
| 88 | ModEntry.second->SourceFiles.push_back(File); |
| 89 | return Error::success(); |
| 90 | } |
| 91 | |
| 92 | uint32_t DbiStreamBuilder::calculateModiSubstreamSize() const { |
| 93 | uint32_t Size = 0; |
| 94 | for (const auto &M : ModuleInfoList) { |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 95 | Size += sizeof(ModuleInfoHeader); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 96 | Size += M->Mod.size() + 1; |
| 97 | Size += M->Obj.size() + 1; |
| 98 | } |
| 99 | return Size; |
| 100 | } |
| 101 | |
| 102 | uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize() const { |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 103 | uint32_t Size = 0; |
| 104 | Size += sizeof(ulittle16_t); // NumModules |
| 105 | Size += sizeof(ulittle16_t); // NumSourceFiles |
| 106 | Size += ModuleInfoList.size() * sizeof(ulittle16_t); // ModIndices |
| 107 | Size += ModuleInfoList.size() * sizeof(ulittle16_t); // ModFileCounts |
| 108 | uint32_t NumFileInfos = 0; |
| 109 | for (const auto &M : ModuleInfoList) |
| 110 | NumFileInfos += M->SourceFiles.size(); |
| 111 | Size += NumFileInfos * sizeof(ulittle32_t); // FileNameOffsets |
| 112 | Size += calculateNamesBufferSize(); |
| 113 | return Size; |
| 114 | } |
| 115 | |
| 116 | uint32_t DbiStreamBuilder::calculateNamesBufferSize() const { |
| 117 | uint32_t Size = 0; |
| 118 | for (const auto &F : SourceFileNames) { |
| 119 | Size += F.getKeyLength() + 1; // Names[I]; |
| 120 | } |
| 121 | return Size; |
| 122 | } |
| 123 | |
Rui Ueyama | 77be240 | 2016-10-29 00:56:44 +0000 | [diff] [blame^] | 124 | uint32_t DbiStreamBuilder::calculateDbgStreamsSize() const { |
| 125 | return DbgStreams.size() * sizeof(uint16_t); |
| 126 | } |
| 127 | |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 128 | Error DbiStreamBuilder::generateModiSubstream() { |
| 129 | uint32_t Size = calculateModiSubstreamSize(); |
| 130 | auto Data = Allocator.Allocate<uint8_t>(Size); |
| 131 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 132 | ModInfoBuffer = MutableByteStream(MutableArrayRef<uint8_t>(Data, Size)); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 133 | |
| 134 | StreamWriter ModiWriter(ModInfoBuffer); |
| 135 | for (const auto &M : ModuleInfoList) { |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 136 | ModuleInfoHeader Layout = {}; |
| 137 | Layout.ModDiStream = kInvalidStreamIndex; |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 138 | Layout.NumFiles = M->SourceFiles.size(); |
| 139 | if (auto EC = ModiWriter.writeObject(Layout)) |
| 140 | return EC; |
| 141 | if (auto EC = ModiWriter.writeZeroString(M->Mod)) |
| 142 | return EC; |
| 143 | if (auto EC = ModiWriter.writeZeroString(M->Obj)) |
| 144 | return EC; |
| 145 | } |
| 146 | if (ModiWriter.bytesRemaining() != 0) |
| 147 | return make_error<RawError>(raw_error_code::invalid_format, |
| 148 | "Unexpected bytes in Modi Stream Data"); |
| 149 | return Error::success(); |
| 150 | } |
| 151 | |
| 152 | Error DbiStreamBuilder::generateFileInfoSubstream() { |
| 153 | uint32_t Size = calculateFileInfoSubstreamSize(); |
| 154 | uint32_t NameSize = calculateNamesBufferSize(); |
| 155 | auto Data = Allocator.Allocate<uint8_t>(Size); |
| 156 | uint32_t NamesOffset = Size - NameSize; |
| 157 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 158 | FileInfoBuffer = MutableByteStream(MutableArrayRef<uint8_t>(Data, Size)); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 159 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 160 | WritableStreamRef MetadataBuffer = |
| 161 | WritableStreamRef(FileInfoBuffer).keep_front(NamesOffset); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 162 | StreamWriter MetadataWriter(MetadataBuffer); |
| 163 | |
| 164 | uint16_t ModiCount = std::min<uint16_t>(UINT16_MAX, ModuleInfos.size()); |
| 165 | uint16_t FileCount = std::min<uint16_t>(UINT16_MAX, SourceFileNames.size()); |
| 166 | if (auto EC = MetadataWriter.writeInteger(ModiCount)) // NumModules |
| 167 | return EC; |
| 168 | if (auto EC = MetadataWriter.writeInteger(FileCount)) // NumSourceFiles |
| 169 | return EC; |
| 170 | for (uint16_t I = 0; I < ModiCount; ++I) { |
| 171 | if (auto EC = MetadataWriter.writeInteger(I)) // Mod Indices |
| 172 | return EC; |
| 173 | } |
| 174 | for (const auto MI : ModuleInfoList) { |
| 175 | FileCount = static_cast<uint16_t>(MI->SourceFiles.size()); |
| 176 | if (auto EC = MetadataWriter.writeInteger(FileCount)) // Mod File Counts |
| 177 | return EC; |
| 178 | } |
| 179 | |
| 180 | // Before writing the FileNameOffsets array, write the NamesBuffer array. |
| 181 | // A side effect of this is that this will actually compute the various |
| 182 | // file name offsets, so we can then go back and write the FileNameOffsets |
| 183 | // array to the other substream. |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 184 | NamesBuffer = WritableStreamRef(FileInfoBuffer).drop_front(NamesOffset); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 185 | StreamWriter NameBufferWriter(NamesBuffer); |
| 186 | for (auto &Name : SourceFileNames) { |
| 187 | Name.second = NameBufferWriter.getOffset(); |
| 188 | if (auto EC = NameBufferWriter.writeZeroString(Name.getKey())) |
| 189 | return EC; |
| 190 | } |
| 191 | |
| 192 | for (const auto MI : ModuleInfoList) { |
| 193 | for (StringRef Name : MI->SourceFiles) { |
| 194 | auto Result = SourceFileNames.find(Name); |
| 195 | if (Result == SourceFileNames.end()) |
| 196 | return make_error<RawError>(raw_error_code::no_entry, |
| 197 | "The source file was not found."); |
| 198 | if (auto EC = MetadataWriter.writeInteger(Result->second)) |
| 199 | return EC; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | if (NameBufferWriter.bytesRemaining() > 0) |
| 204 | return make_error<RawError>(raw_error_code::invalid_format, |
| 205 | "The names buffer contained unexpected data."); |
| 206 | |
| 207 | if (MetadataWriter.bytesRemaining() > 0) |
| 208 | return make_error<RawError>( |
| 209 | raw_error_code::invalid_format, |
| 210 | "The metadata buffer contained unexpected data."); |
| 211 | |
| 212 | return Error::success(); |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 215 | Error DbiStreamBuilder::finalize() { |
| 216 | if (Header) |
| 217 | return Error::success(); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 218 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 219 | DbiStreamHeader *H = Allocator.Allocate<DbiStreamHeader>(); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 220 | |
| 221 | if (auto EC = generateModiSubstream()) |
Zachary Turner | e98137c | 2016-07-28 19:18:02 +0000 | [diff] [blame] | 222 | return EC; |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 223 | if (auto EC = generateFileInfoSubstream()) |
Zachary Turner | e98137c | 2016-07-28 19:18:02 +0000 | [diff] [blame] | 224 | return EC; |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 225 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 226 | H->VersionHeader = *VerHeader; |
| 227 | H->VersionSignature = -1; |
| 228 | H->Age = Age; |
| 229 | H->BuildNumber = BuildNumber; |
| 230 | H->Flags = Flags; |
| 231 | H->PdbDllRbld = PdbDllRbld; |
| 232 | H->PdbDllVersion = PdbDllVersion; |
| 233 | H->MachineType = static_cast<uint16_t>(MachineType); |
| 234 | |
| 235 | H->ECSubstreamSize = 0; |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 236 | H->FileInfoSize = FileInfoBuffer.getLength(); |
| 237 | H->ModiSubstreamSize = ModInfoBuffer.getLength(); |
Rui Ueyama | f990404 | 2016-10-11 19:43:12 +0000 | [diff] [blame] | 238 | H->OptionalDbgHdrSize = DbgStreams.size() * sizeof(uint16_t); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 239 | H->SecContrSubstreamSize = 0; |
| 240 | H->SectionMapSize = 0; |
| 241 | H->TypeServerSize = 0; |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 242 | H->SymRecordStreamIndex = kInvalidStreamIndex; |
| 243 | H->PublicSymbolStreamIndex = kInvalidStreamIndex; |
| 244 | H->MFCTypeServerIndex = kInvalidStreamIndex; |
| 245 | H->GlobalSymbolStreamIndex = kInvalidStreamIndex; |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 246 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 247 | Header = H; |
| 248 | return Error::success(); |
| 249 | } |
| 250 | |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 251 | Error DbiStreamBuilder::finalizeMsfLayout() { |
| 252 | uint32_t Length = calculateSerializedLength(); |
| 253 | if (auto EC = Msf.setStreamSize(StreamDBI, Length)) |
| 254 | return EC; |
| 255 | return Error::success(); |
| 256 | } |
| 257 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 258 | Expected<std::unique_ptr<DbiStream>> |
| 259 | DbiStreamBuilder::build(PDBFile &File, const msf::WritableStream &Buffer) { |
| 260 | if (!VerHeader.hasValue()) |
| 261 | return make_error<RawError>(raw_error_code::unspecified, |
| 262 | "Missing DBI Stream Version"); |
| 263 | if (auto EC = finalize()) |
| 264 | return std::move(EC); |
| 265 | |
| 266 | auto StreamData = MappedBlockStream::createIndexedStream(File.getMsfLayout(), |
| 267 | Buffer, StreamDBI); |
| 268 | auto Dbi = llvm::make_unique<DbiStream>(File, std::move(StreamData)); |
| 269 | Dbi->Header = Header; |
| 270 | Dbi->FileInfoSubstream = ReadableStreamRef(FileInfoBuffer); |
| 271 | Dbi->ModInfoSubstream = ReadableStreamRef(ModInfoBuffer); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 272 | if (auto EC = Dbi->initializeModInfoArray()) |
| 273 | return std::move(EC); |
| 274 | if (auto EC = Dbi->initializeFileInfo()) |
| 275 | return std::move(EC); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 276 | return std::move(Dbi); |
| 277 | } |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 278 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 279 | Error DbiStreamBuilder::commit(const msf::MSFLayout &Layout, |
Rui Ueyama | d1d8c831 | 2016-08-03 23:43:23 +0000 | [diff] [blame] | 280 | const msf::WritableStream &Buffer) { |
| 281 | if (auto EC = finalize()) |
| 282 | return EC; |
| 283 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 284 | auto InfoS = |
| 285 | WritableMappedBlockStream::createIndexedStream(Layout, Buffer, StreamDBI); |
| 286 | |
| 287 | StreamWriter Writer(*InfoS); |
| 288 | if (auto EC = Writer.writeObject(*Header)) |
| 289 | return EC; |
| 290 | |
| 291 | if (auto EC = Writer.writeStreamRef(ModInfoBuffer)) |
| 292 | return EC; |
| 293 | if (auto EC = Writer.writeStreamRef(FileInfoBuffer)) |
| 294 | return EC; |
Rui Ueyama | f990404 | 2016-10-11 19:43:12 +0000 | [diff] [blame] | 295 | for (auto &Stream : DbgStreams) |
| 296 | if (auto EC = Writer.writeInteger(Stream.StreamNumber)) |
| 297 | return EC; |
| 298 | |
| 299 | for (auto &Stream : DbgStreams) { |
| 300 | if (Stream.StreamNumber == kInvalidStreamIndex) |
| 301 | continue; |
| 302 | auto WritableStream = WritableMappedBlockStream::createIndexedStream( |
| 303 | Layout, Buffer, Stream.StreamNumber); |
| 304 | StreamWriter DbgStreamWriter(*WritableStream); |
| 305 | if (auto EC = DbgStreamWriter.writeArray(Stream.Data)) |
| 306 | return EC; |
| 307 | } |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 308 | |
| 309 | if (Writer.bytesRemaining() > 0) |
| 310 | return make_error<RawError>(raw_error_code::invalid_format, |
| 311 | "Unexpected bytes found in DBI Stream"); |
| 312 | return Error::success(); |
| 313 | } |