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 | |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 10 | #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h" |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 11 | |
Rui Ueyama | f990404 | 2016-10-11 19:43:12 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/ArrayRef.h" |
Zachary Turner | d2684b7 | 2017-02-25 00:33:34 +0000 | [diff] [blame^] | 13 | #include "llvm/DebugInfo/MSF/BinaryStreamWriter.h" |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 15 | #include "llvm/DebugInfo/MSF/MappedBlockStream.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 17 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
Rui Ueyama | ddc7922 | 2016-10-31 17:38:56 +0000 | [diff] [blame] | 18 | #include "llvm/Object/COFF.h" |
| 19 | #include "llvm/Support/COFF.h" |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | using namespace llvm::codeview; |
Zachary Turner | bac69d3 | 2016-07-22 19:56:05 +0000 | [diff] [blame] | 23 | using namespace llvm::msf; |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 24 | using namespace llvm::pdb; |
| 25 | |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | class ModiSubstreamBuilder {}; |
| 28 | } |
| 29 | |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 30 | DbiStreamBuilder::DbiStreamBuilder(msf::MSFBuilder &Msf) |
| 31 | : Msf(Msf), Allocator(Msf.getAllocator()), Age(1), BuildNumber(0), |
| 32 | PdbDllVersion(0), PdbDllRbld(0), Flags(0), MachineType(PDB_Machine::x86), |
Reid Kleckner | 5d0bc63 | 2016-10-11 20:02:57 +0000 | [diff] [blame] | 33 | Header(nullptr), DbgStreams((int)DbgHeaderType::Max) {} |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 34 | |
| 35 | void DbiStreamBuilder::setVersionHeader(PdbRaw_DbiVer V) { VerHeader = V; } |
| 36 | |
| 37 | void DbiStreamBuilder::setAge(uint32_t A) { Age = A; } |
| 38 | |
| 39 | void DbiStreamBuilder::setBuildNumber(uint16_t B) { BuildNumber = B; } |
| 40 | |
| 41 | void DbiStreamBuilder::setPdbDllVersion(uint16_t V) { PdbDllVersion = V; } |
| 42 | |
| 43 | void DbiStreamBuilder::setPdbDllRbld(uint16_t R) { PdbDllRbld = R; } |
| 44 | |
| 45 | void DbiStreamBuilder::setFlags(uint16_t F) { Flags = F; } |
| 46 | |
| 47 | void DbiStreamBuilder::setMachineType(PDB_Machine M) { MachineType = M; } |
| 48 | |
Rui Ueyama | f7c9c32 | 2016-11-11 23:41:13 +0000 | [diff] [blame] | 49 | void DbiStreamBuilder::setSectionContribs(ArrayRef<SectionContrib> Arr) { |
| 50 | SectionContribs = Arr; |
| 51 | } |
| 52 | |
Rui Ueyama | ddc7922 | 2016-10-31 17:38:56 +0000 | [diff] [blame] | 53 | void DbiStreamBuilder::setSectionMap(ArrayRef<SecMapEntry> SecMap) { |
| 54 | SectionMap = SecMap; |
| 55 | } |
| 56 | |
Rui Ueyama | f990404 | 2016-10-11 19:43:12 +0000 | [diff] [blame] | 57 | Error DbiStreamBuilder::addDbgStream(pdb::DbgHeaderType Type, |
| 58 | ArrayRef<uint8_t> Data) { |
| 59 | if (DbgStreams[(int)Type].StreamNumber) |
| 60 | return make_error<RawError>(raw_error_code::duplicate_entry, |
| 61 | "The specified stream type already exists"); |
| 62 | auto ExpectedIndex = Msf.addStream(Data.size()); |
| 63 | if (!ExpectedIndex) |
| 64 | return ExpectedIndex.takeError(); |
| 65 | uint32_t Index = std::move(*ExpectedIndex); |
| 66 | DbgStreams[(int)Type].Data = Data; |
| 67 | DbgStreams[(int)Type].StreamNumber = Index; |
| 68 | return Error::success(); |
| 69 | } |
| 70 | |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 71 | uint32_t DbiStreamBuilder::calculateSerializedLength() const { |
| 72 | // For now we only support serializing the header. |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 73 | return sizeof(DbiStreamHeader) + calculateFileInfoSubstreamSize() + |
Rui Ueyama | f7c9c32 | 2016-11-11 23:41:13 +0000 | [diff] [blame] | 74 | calculateModiSubstreamSize() + calculateSectionContribsStreamSize() + |
| 75 | calculateSectionMapStreamSize() + calculateDbgStreamsSize(); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | Error DbiStreamBuilder::addModuleInfo(StringRef ObjFile, StringRef Module) { |
| 79 | auto Entry = llvm::make_unique<ModuleInfo>(); |
| 80 | ModuleInfo *M = Entry.get(); |
| 81 | Entry->Mod = Module; |
| 82 | Entry->Obj = ObjFile; |
| 83 | auto Result = ModuleInfos.insert(std::make_pair(Module, std::move(Entry))); |
| 84 | if (!Result.second) |
| 85 | return make_error<RawError>(raw_error_code::duplicate_entry, |
| 86 | "The specified module already exists"); |
| 87 | ModuleInfoList.push_back(M); |
| 88 | return Error::success(); |
| 89 | } |
| 90 | |
| 91 | Error DbiStreamBuilder::addModuleSourceFile(StringRef Module, StringRef File) { |
| 92 | auto ModIter = ModuleInfos.find(Module); |
| 93 | if (ModIter == ModuleInfos.end()) |
| 94 | return make_error<RawError>(raw_error_code::no_entry, |
| 95 | "The specified module was not found"); |
| 96 | uint32_t Index = SourceFileNames.size(); |
| 97 | SourceFileNames.insert(std::make_pair(File, Index)); |
| 98 | auto &ModEntry = *ModIter; |
| 99 | ModEntry.second->SourceFiles.push_back(File); |
| 100 | return Error::success(); |
| 101 | } |
| 102 | |
| 103 | uint32_t DbiStreamBuilder::calculateModiSubstreamSize() const { |
| 104 | uint32_t Size = 0; |
| 105 | for (const auto &M : ModuleInfoList) { |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 106 | Size += sizeof(ModuleInfoHeader); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 107 | Size += M->Mod.size() + 1; |
| 108 | Size += M->Obj.size() + 1; |
| 109 | } |
Rui Ueyama | fb1e6d2 | 2016-11-16 00:59:27 +0000 | [diff] [blame] | 110 | return alignTo(Size, sizeof(uint32_t)); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Rui Ueyama | f7c9c32 | 2016-11-11 23:41:13 +0000 | [diff] [blame] | 113 | uint32_t DbiStreamBuilder::calculateSectionContribsStreamSize() const { |
| 114 | if (SectionContribs.empty()) |
| 115 | return 0; |
| 116 | return sizeof(enum PdbRaw_DbiSecContribVer) + |
| 117 | sizeof(SectionContribs[0]) * SectionContribs.size(); |
Rui Ueyama | a8a68a9 | 2016-11-12 00:23:32 +0000 | [diff] [blame] | 118 | } |
Rui Ueyama | f7c9c32 | 2016-11-11 23:41:13 +0000 | [diff] [blame] | 119 | |
Rui Ueyama | ddc7922 | 2016-10-31 17:38:56 +0000 | [diff] [blame] | 120 | uint32_t DbiStreamBuilder::calculateSectionMapStreamSize() const { |
| 121 | if (SectionMap.empty()) |
| 122 | return 0; |
| 123 | return sizeof(SecMapHeader) + sizeof(SecMapEntry) * SectionMap.size(); |
| 124 | } |
| 125 | |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 126 | uint32_t DbiStreamBuilder::calculateFileInfoSubstreamSize() const { |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 127 | uint32_t Size = 0; |
| 128 | Size += sizeof(ulittle16_t); // NumModules |
| 129 | Size += sizeof(ulittle16_t); // NumSourceFiles |
| 130 | Size += ModuleInfoList.size() * sizeof(ulittle16_t); // ModIndices |
| 131 | Size += ModuleInfoList.size() * sizeof(ulittle16_t); // ModFileCounts |
| 132 | uint32_t NumFileInfos = 0; |
| 133 | for (const auto &M : ModuleInfoList) |
| 134 | NumFileInfos += M->SourceFiles.size(); |
| 135 | Size += NumFileInfos * sizeof(ulittle32_t); // FileNameOffsets |
| 136 | Size += calculateNamesBufferSize(); |
Rui Ueyama | fb1e6d2 | 2016-11-16 00:59:27 +0000 | [diff] [blame] | 137 | return alignTo(Size, sizeof(uint32_t)); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | uint32_t DbiStreamBuilder::calculateNamesBufferSize() const { |
| 141 | uint32_t Size = 0; |
| 142 | for (const auto &F : SourceFileNames) { |
| 143 | Size += F.getKeyLength() + 1; // Names[I]; |
| 144 | } |
| 145 | return Size; |
| 146 | } |
| 147 | |
Rui Ueyama | 77be240 | 2016-10-29 00:56:44 +0000 | [diff] [blame] | 148 | uint32_t DbiStreamBuilder::calculateDbgStreamsSize() const { |
| 149 | return DbgStreams.size() * sizeof(uint16_t); |
| 150 | } |
| 151 | |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 152 | Error DbiStreamBuilder::generateModiSubstream() { |
| 153 | uint32_t Size = calculateModiSubstreamSize(); |
| 154 | auto Data = Allocator.Allocate<uint8_t>(Size); |
| 155 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 156 | ModInfoBuffer = MutableByteStream(MutableArrayRef<uint8_t>(Data, Size)); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 157 | |
| 158 | StreamWriter ModiWriter(ModInfoBuffer); |
| 159 | for (const auto &M : ModuleInfoList) { |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 160 | ModuleInfoHeader Layout = {}; |
| 161 | Layout.ModDiStream = kInvalidStreamIndex; |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 162 | Layout.NumFiles = M->SourceFiles.size(); |
| 163 | if (auto EC = ModiWriter.writeObject(Layout)) |
| 164 | return EC; |
| 165 | if (auto EC = ModiWriter.writeZeroString(M->Mod)) |
| 166 | return EC; |
| 167 | if (auto EC = ModiWriter.writeZeroString(M->Obj)) |
| 168 | return EC; |
| 169 | } |
Rui Ueyama | fb1e6d2 | 2016-11-16 00:59:27 +0000 | [diff] [blame] | 170 | if (ModiWriter.bytesRemaining() > sizeof(uint32_t)) |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 171 | return make_error<RawError>(raw_error_code::invalid_format, |
| 172 | "Unexpected bytes in Modi Stream Data"); |
| 173 | return Error::success(); |
| 174 | } |
| 175 | |
| 176 | Error DbiStreamBuilder::generateFileInfoSubstream() { |
| 177 | uint32_t Size = calculateFileInfoSubstreamSize(); |
| 178 | uint32_t NameSize = calculateNamesBufferSize(); |
| 179 | auto Data = Allocator.Allocate<uint8_t>(Size); |
| 180 | uint32_t NamesOffset = Size - NameSize; |
| 181 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 182 | FileInfoBuffer = MutableByteStream(MutableArrayRef<uint8_t>(Data, Size)); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 183 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 184 | WritableStreamRef MetadataBuffer = |
| 185 | WritableStreamRef(FileInfoBuffer).keep_front(NamesOffset); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 186 | StreamWriter MetadataWriter(MetadataBuffer); |
| 187 | |
Rui Ueyama | 5070131 | 2016-11-16 00:38:33 +0000 | [diff] [blame] | 188 | uint16_t ModiCount = std::min<uint32_t>(UINT16_MAX, ModuleInfos.size()); |
| 189 | uint16_t FileCount = std::min<uint32_t>(UINT16_MAX, SourceFileNames.size()); |
Zachary Turner | 181fe17 | 2017-02-18 01:35:33 +0000 | [diff] [blame] | 190 | if (auto EC = MetadataWriter.writeInteger( |
| 191 | ModiCount, llvm::support::little)) // NumModules |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 192 | return EC; |
Zachary Turner | 181fe17 | 2017-02-18 01:35:33 +0000 | [diff] [blame] | 193 | if (auto EC = MetadataWriter.writeInteger( |
| 194 | FileCount, llvm::support::little)) // NumSourceFiles |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 195 | return EC; |
| 196 | for (uint16_t I = 0; I < ModiCount; ++I) { |
Zachary Turner | 181fe17 | 2017-02-18 01:35:33 +0000 | [diff] [blame] | 197 | if (auto EC = MetadataWriter.writeInteger( |
| 198 | I, llvm::support::little)) // Mod Indices |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 199 | return EC; |
| 200 | } |
| 201 | for (const auto MI : ModuleInfoList) { |
| 202 | FileCount = static_cast<uint16_t>(MI->SourceFiles.size()); |
Zachary Turner | 181fe17 | 2017-02-18 01:35:33 +0000 | [diff] [blame] | 203 | if (auto EC = MetadataWriter.writeInteger( |
| 204 | FileCount, llvm::support::little)) // Mod File Counts |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 205 | return EC; |
| 206 | } |
| 207 | |
| 208 | // Before writing the FileNameOffsets array, write the NamesBuffer array. |
| 209 | // A side effect of this is that this will actually compute the various |
| 210 | // file name offsets, so we can then go back and write the FileNameOffsets |
| 211 | // array to the other substream. |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 212 | NamesBuffer = WritableStreamRef(FileInfoBuffer).drop_front(NamesOffset); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 213 | StreamWriter NameBufferWriter(NamesBuffer); |
| 214 | for (auto &Name : SourceFileNames) { |
| 215 | Name.second = NameBufferWriter.getOffset(); |
| 216 | if (auto EC = NameBufferWriter.writeZeroString(Name.getKey())) |
| 217 | return EC; |
| 218 | } |
| 219 | |
| 220 | for (const auto MI : ModuleInfoList) { |
| 221 | for (StringRef Name : MI->SourceFiles) { |
| 222 | auto Result = SourceFileNames.find(Name); |
| 223 | if (Result == SourceFileNames.end()) |
| 224 | return make_error<RawError>(raw_error_code::no_entry, |
| 225 | "The source file was not found."); |
Zachary Turner | 181fe17 | 2017-02-18 01:35:33 +0000 | [diff] [blame] | 226 | if (auto EC = MetadataWriter.writeInteger(Result->second, |
| 227 | llvm::support::little)) |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 228 | return EC; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if (NameBufferWriter.bytesRemaining() > 0) |
| 233 | return make_error<RawError>(raw_error_code::invalid_format, |
| 234 | "The names buffer contained unexpected data."); |
| 235 | |
Rui Ueyama | fb1e6d2 | 2016-11-16 00:59:27 +0000 | [diff] [blame] | 236 | if (MetadataWriter.bytesRemaining() > sizeof(uint32_t)) |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 237 | return make_error<RawError>( |
| 238 | raw_error_code::invalid_format, |
| 239 | "The metadata buffer contained unexpected data."); |
| 240 | |
| 241 | return Error::success(); |
Zachary Turner | faa554b | 2016-07-15 22:16:56 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 244 | Error DbiStreamBuilder::finalize() { |
| 245 | if (Header) |
| 246 | return Error::success(); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 247 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 248 | DbiStreamHeader *H = Allocator.Allocate<DbiStreamHeader>(); |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 249 | |
| 250 | if (auto EC = generateModiSubstream()) |
Zachary Turner | e98137c | 2016-07-28 19:18:02 +0000 | [diff] [blame] | 251 | return EC; |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 252 | if (auto EC = generateFileInfoSubstream()) |
Zachary Turner | e98137c | 2016-07-28 19:18:02 +0000 | [diff] [blame] | 253 | return EC; |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 254 | |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 255 | H->VersionHeader = *VerHeader; |
| 256 | H->VersionSignature = -1; |
| 257 | H->Age = Age; |
| 258 | H->BuildNumber = BuildNumber; |
| 259 | H->Flags = Flags; |
| 260 | H->PdbDllRbld = PdbDllRbld; |
| 261 | H->PdbDllVersion = PdbDllVersion; |
| 262 | H->MachineType = static_cast<uint16_t>(MachineType); |
| 263 | |
| 264 | H->ECSubstreamSize = 0; |
Zachary Turner | d218c26 | 2016-07-22 15:46:37 +0000 | [diff] [blame] | 265 | H->FileInfoSize = FileInfoBuffer.getLength(); |
| 266 | H->ModiSubstreamSize = ModInfoBuffer.getLength(); |
Rui Ueyama | f990404 | 2016-10-11 19:43:12 +0000 | [diff] [blame] | 267 | H->OptionalDbgHdrSize = DbgStreams.size() * sizeof(uint16_t); |
Rui Ueyama | f7c9c32 | 2016-11-11 23:41:13 +0000 | [diff] [blame] | 268 | H->SecContrSubstreamSize = calculateSectionContribsStreamSize(); |
Rui Ueyama | ddc7922 | 2016-10-31 17:38:56 +0000 | [diff] [blame] | 269 | H->SectionMapSize = calculateSectionMapStreamSize(); |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 270 | H->TypeServerSize = 0; |
Zachary Turner | b383d62 | 2016-07-22 15:46:46 +0000 | [diff] [blame] | 271 | H->SymRecordStreamIndex = kInvalidStreamIndex; |
| 272 | H->PublicSymbolStreamIndex = kInvalidStreamIndex; |
| 273 | H->MFCTypeServerIndex = kInvalidStreamIndex; |
| 274 | H->GlobalSymbolStreamIndex = kInvalidStreamIndex; |
Zachary Turner | dbeaea7 | 2016-07-11 21:45:26 +0000 | [diff] [blame] | 275 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 276 | Header = H; |
| 277 | return Error::success(); |
| 278 | } |
| 279 | |
Zachary Turner | 620961d | 2016-09-14 23:00:02 +0000 | [diff] [blame] | 280 | Error DbiStreamBuilder::finalizeMsfLayout() { |
| 281 | uint32_t Length = calculateSerializedLength(); |
| 282 | if (auto EC = Msf.setStreamSize(StreamDBI, Length)) |
| 283 | return EC; |
| 284 | return Error::success(); |
| 285 | } |
| 286 | |
Rui Ueyama | ddc7922 | 2016-10-31 17:38:56 +0000 | [diff] [blame] | 287 | static uint16_t toSecMapFlags(uint32_t Flags) { |
| 288 | uint16_t Ret = 0; |
| 289 | if (Flags & COFF::IMAGE_SCN_MEM_READ) |
| 290 | Ret |= static_cast<uint16_t>(OMFSegDescFlags::Read); |
| 291 | if (Flags & COFF::IMAGE_SCN_MEM_WRITE) |
| 292 | Ret |= static_cast<uint16_t>(OMFSegDescFlags::Write); |
| 293 | if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE) |
| 294 | Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute); |
| 295 | if (Flags & COFF::IMAGE_SCN_MEM_EXECUTE) |
| 296 | Ret |= static_cast<uint16_t>(OMFSegDescFlags::Execute); |
| 297 | if (!(Flags & COFF::IMAGE_SCN_MEM_16BIT)) |
| 298 | Ret |= static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit); |
| 299 | |
| 300 | // This seems always 1. |
| 301 | Ret |= static_cast<uint16_t>(OMFSegDescFlags::IsSelector); |
| 302 | |
| 303 | return Ret; |
| 304 | } |
| 305 | |
Rui Ueyama | f7c9c32 | 2016-11-11 23:41:13 +0000 | [diff] [blame] | 306 | // A utility function to create Section Contributions |
| 307 | // for a given input sections. |
| 308 | std::vector<SectionContrib> DbiStreamBuilder::createSectionContribs( |
| 309 | ArrayRef<object::coff_section> SecHdrs) { |
| 310 | std::vector<SectionContrib> Ret; |
| 311 | |
| 312 | // Create a SectionContrib for each input section. |
| 313 | for (auto &Sec : SecHdrs) { |
| 314 | Ret.emplace_back(); |
| 315 | auto &Entry = Ret.back(); |
| 316 | memset(&Entry, 0, sizeof(Entry)); |
| 317 | |
| 318 | Entry.Off = Sec.PointerToRawData; |
| 319 | Entry.Size = Sec.SizeOfRawData; |
| 320 | Entry.Characteristics = Sec.Characteristics; |
| 321 | } |
| 322 | return Ret; |
| 323 | } |
| 324 | |
Rui Ueyama | ddc7922 | 2016-10-31 17:38:56 +0000 | [diff] [blame] | 325 | // A utility function to create a Section Map for a given list of COFF sections. |
| 326 | // |
| 327 | // A Section Map seem to be a copy of a COFF section list in other format. |
| 328 | // I don't know why a PDB file contains both a COFF section header and |
| 329 | // a Section Map, but it seems it must be present in a PDB. |
| 330 | std::vector<SecMapEntry> DbiStreamBuilder::createSectionMap( |
| 331 | ArrayRef<llvm::object::coff_section> SecHdrs) { |
| 332 | std::vector<SecMapEntry> Ret; |
| 333 | int Idx = 0; |
| 334 | |
| 335 | auto Add = [&]() -> SecMapEntry & { |
| 336 | Ret.emplace_back(); |
| 337 | auto &Entry = Ret.back(); |
| 338 | memset(&Entry, 0, sizeof(Entry)); |
| 339 | |
| 340 | Entry.Frame = Idx + 1; |
| 341 | |
| 342 | // We don't know the meaning of these fields yet. |
| 343 | Entry.SecName = UINT16_MAX; |
| 344 | Entry.ClassName = UINT16_MAX; |
| 345 | |
| 346 | return Entry; |
| 347 | }; |
| 348 | |
| 349 | for (auto &Hdr : SecHdrs) { |
| 350 | auto &Entry = Add(); |
| 351 | Entry.Flags = toSecMapFlags(Hdr.Characteristics); |
| 352 | Entry.SecByteLength = Hdr.VirtualSize; |
| 353 | ++Idx; |
| 354 | } |
| 355 | |
| 356 | // The last entry is for absolute symbols. |
| 357 | auto &Entry = Add(); |
| 358 | Entry.Flags = static_cast<uint16_t>(OMFSegDescFlags::AddressIs32Bit) | |
| 359 | static_cast<uint16_t>(OMFSegDescFlags::IsAbsoluteAddress); |
| 360 | Entry.SecByteLength = UINT32_MAX; |
| 361 | |
| 362 | return Ret; |
| 363 | } |
| 364 | |
Zachary Turner | a3225b0 | 2016-07-29 20:56:36 +0000 | [diff] [blame] | 365 | Error DbiStreamBuilder::commit(const msf::MSFLayout &Layout, |
Rui Ueyama | d1d8c831 | 2016-08-03 23:43:23 +0000 | [diff] [blame] | 366 | const msf::WritableStream &Buffer) { |
| 367 | if (auto EC = finalize()) |
| 368 | return EC; |
| 369 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 370 | auto InfoS = |
| 371 | WritableMappedBlockStream::createIndexedStream(Layout, Buffer, StreamDBI); |
| 372 | |
| 373 | StreamWriter Writer(*InfoS); |
| 374 | if (auto EC = Writer.writeObject(*Header)) |
| 375 | return EC; |
| 376 | |
| 377 | if (auto EC = Writer.writeStreamRef(ModInfoBuffer)) |
| 378 | return EC; |
Rui Ueyama | ddc7922 | 2016-10-31 17:38:56 +0000 | [diff] [blame] | 379 | |
Rui Ueyama | f7c9c32 | 2016-11-11 23:41:13 +0000 | [diff] [blame] | 380 | if (!SectionContribs.empty()) { |
Zachary Turner | 181fe17 | 2017-02-18 01:35:33 +0000 | [diff] [blame] | 381 | if (auto EC = Writer.writeEnum(DbiSecContribVer60, llvm::support::little)) |
Rui Ueyama | f7c9c32 | 2016-11-11 23:41:13 +0000 | [diff] [blame] | 382 | return EC; |
| 383 | if (auto EC = Writer.writeArray(SectionContribs)) |
| 384 | return EC; |
| 385 | } |
| 386 | |
Rui Ueyama | ddc7922 | 2016-10-31 17:38:56 +0000 | [diff] [blame] | 387 | if (!SectionMap.empty()) { |
| 388 | ulittle16_t Size = static_cast<ulittle16_t>(SectionMap.size()); |
| 389 | SecMapHeader SMHeader = {Size, Size}; |
| 390 | if (auto EC = Writer.writeObject(SMHeader)) |
| 391 | return EC; |
| 392 | if (auto EC = Writer.writeArray(SectionMap)) |
| 393 | return EC; |
| 394 | } |
| 395 | |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 396 | if (auto EC = Writer.writeStreamRef(FileInfoBuffer)) |
| 397 | return EC; |
Rui Ueyama | ddc7922 | 2016-10-31 17:38:56 +0000 | [diff] [blame] | 398 | |
Rui Ueyama | f990404 | 2016-10-11 19:43:12 +0000 | [diff] [blame] | 399 | for (auto &Stream : DbgStreams) |
Zachary Turner | 181fe17 | 2017-02-18 01:35:33 +0000 | [diff] [blame] | 400 | if (auto EC = |
| 401 | Writer.writeInteger(Stream.StreamNumber, llvm::support::little)) |
Rui Ueyama | f990404 | 2016-10-11 19:43:12 +0000 | [diff] [blame] | 402 | return EC; |
| 403 | |
| 404 | for (auto &Stream : DbgStreams) { |
| 405 | if (Stream.StreamNumber == kInvalidStreamIndex) |
| 406 | continue; |
| 407 | auto WritableStream = WritableMappedBlockStream::createIndexedStream( |
| 408 | Layout, Buffer, Stream.StreamNumber); |
| 409 | StreamWriter DbgStreamWriter(*WritableStream); |
| 410 | if (auto EC = DbgStreamWriter.writeArray(Stream.Data)) |
| 411 | return EC; |
| 412 | } |
Zachary Turner | d66889c | 2016-07-28 19:12:28 +0000 | [diff] [blame] | 413 | |
| 414 | if (Writer.bytesRemaining() > 0) |
| 415 | return make_error<RawError>(raw_error_code::invalid_format, |
| 416 | "Unexpected bytes found in DBI Stream"); |
| 417 | return Error::success(); |
| 418 | } |