Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 1 | //===- PDB.cpp ------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Linker |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame] | 10 | #include "PDB.h" |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 11 | #include "Chunks.h" |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 12 | #include "Config.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 13 | #include "Error.h" |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 14 | #include "SymbolTable.h" |
| 15 | #include "Symbols.h" |
Saleem Abdulrasool | df8a13b | 2017-01-04 17:56:54 +0000 | [diff] [blame] | 16 | #include "llvm/DebugInfo/CodeView/CVDebugRecord.h" |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h" |
| 19 | #include "llvm/DebugInfo/CodeView/DebugSubsectionVisitor.h" |
Zachary Turner | 526f4f2 | 2017-05-19 19:26:58 +0000 | [diff] [blame] | 20 | #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame^] | 22 | #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h" |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h" |
| 24 | #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h" |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 25 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo/MSF/MSFCommon.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 27 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 28 | #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h" |
| 29 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
| 30 | #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h" |
| 31 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
| 32 | #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h" |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 33 | #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h" |
Rafael Espindola | a0f30da | 2017-05-02 20:19:42 +0000 | [diff] [blame] | 34 | #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" |
Zachary Turner | 7b327d0 | 2017-02-16 23:35:45 +0000 | [diff] [blame] | 35 | #include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 36 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
| 37 | #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h" |
Rui Ueyama | 20df4ec | 2016-10-31 21:09:21 +0000 | [diff] [blame] | 38 | #include "llvm/Object/COFF.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 39 | #include "llvm/Support/BinaryByteStream.h" |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Endian.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 41 | #include "llvm/Support/FileOutputBuffer.h" |
Zachary Turner | 7b327d0 | 2017-02-16 23:35:45 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Path.h" |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 43 | #include "llvm/Support/ScopedPrinter.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 44 | #include <memory> |
| 45 | |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame] | 46 | using namespace lld; |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 47 | using namespace lld::coff; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 48 | using namespace llvm; |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 49 | using namespace llvm::codeview; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 50 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 51 | using llvm::object::coff_section; |
| 52 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 53 | static ExitOnError ExitOnErr; |
| 54 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 55 | // Returns a list of all SectionChunks. |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 56 | static void addSectionContribs(SymbolTable *Symtab, pdb::DbiStreamBuilder &DbiBuilder) { |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 57 | for (Chunk *C : Symtab->getChunks()) |
| 58 | if (auto *SC = dyn_cast<SectionChunk>(C)) |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 59 | DbiBuilder.addSectionContrib(SC->File->ModuleDBI, SC->Header); |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 62 | static SectionChunk *findByName(std::vector<SectionChunk *> &Sections, |
| 63 | StringRef Name) { |
| 64 | for (SectionChunk *C : Sections) |
| 65 | if (C->getSectionName() == Name) |
| 66 | return C; |
| 67 | return nullptr; |
| 68 | } |
| 69 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 70 | static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data, |
| 71 | StringRef SecName) { |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 72 | // First 4 bytes are section magic. |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 73 | if (Data.size() < 4) |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 74 | fatal(SecName + " too short"); |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 75 | if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC) |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 76 | fatal(SecName + " has an invalid magic"); |
Rui Ueyama | 26186c7 | 2016-12-09 04:46:54 +0000 | [diff] [blame] | 77 | return Data.slice(4); |
| 78 | } |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 79 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 80 | static ArrayRef<uint8_t> getDebugSection(ObjectFile *File, StringRef SecName) { |
| 81 | if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName)) |
| 82 | return consumeDebugMagic(Sec->getContents(), SecName); |
| 83 | return {}; |
| 84 | } |
| 85 | |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 86 | static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder, |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 87 | TypeTableBuilder &TypeTable) { |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 88 | // Start the TPI or IPI stream header. |
| 89 | TpiBuilder.setVersionHeader(pdb::PdbTpiV80); |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 90 | |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 91 | // Flatten the in memory type table. |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 92 | TypeTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) { |
Reid Kleckner | 13fc411 | 2017-04-04 00:56:34 +0000 | [diff] [blame] | 93 | // FIXME: Hash types. |
| 94 | TpiBuilder.addTypeRecord(Rec, None); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 95 | }); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 98 | static void mergeDebugT(ObjectFile *File, |
| 99 | TypeTableBuilder &IDTable, |
| 100 | TypeTableBuilder &TypeTable, |
| 101 | SmallVectorImpl<TypeIndex> &TypeIndexMap, |
| 102 | pdb::PDBTypeServerHandler &Handler) { |
| 103 | ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T"); |
| 104 | if (Data.empty()) |
| 105 | return; |
| 106 | |
| 107 | BinaryByteStream Stream(Data, support::little); |
| 108 | CVTypeArray Types; |
| 109 | BinaryStreamReader Reader(Stream); |
| 110 | Handler.addSearchPath(sys::path::parent_path(File->getName())); |
| 111 | if (auto EC = Reader.readArray(Types, Reader.getLength())) |
| 112 | fatal(EC, "Reader::readArray failed"); |
| 113 | if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable, |
| 114 | TypeIndexMap, &Handler, Types)) |
| 115 | fatal(Err, "codeview::mergeTypeStreams failed"); |
| 116 | } |
| 117 | |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame^] | 118 | static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) { |
| 119 | if (TI.isSimple()) |
| 120 | return true; |
| 121 | if (TI.toArrayIndex() >= TypeIndexMap.size()) |
| 122 | return false; |
| 123 | TI = TypeIndexMap[TI.toArrayIndex()]; |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | static bool remapTypesInSymbolRecord(ObjectFile *File, |
| 128 | MutableArrayRef<uint8_t> Contents, |
| 129 | ArrayRef<TypeIndex> TypeIndexMap, |
| 130 | ArrayRef<TiReference> TypeRefs) { |
| 131 | for (const TiReference &Ref : TypeRefs) { |
| 132 | unsigned ByteSize = Ref.Count * sizeof(TypeIndex); |
| 133 | if (Contents.size() < Ref.Offset + ByteSize) { |
| 134 | log("ignoring short symbol record"); |
| 135 | return false; |
| 136 | } |
| 137 | MutableArrayRef<TypeIndex> TIs( |
| 138 | reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count); |
| 139 | for (TypeIndex &TI : TIs) |
| 140 | if (!remapTypeIndex(TI, TypeIndexMap)) { |
| 141 | log("ignoring symbol record in " + File->getName() + |
| 142 | " with bad type index 0x" + utohexstr(TI.getIndex())); |
| 143 | return false; |
| 144 | } |
| 145 | } |
| 146 | return true; |
| 147 | } |
| 148 | |
| 149 | /// MSVC translates S_PROC_ID_END to S_END. |
| 150 | uint16_t canonicalizeSymbolKind(SymbolKind Kind) { |
| 151 | if (Kind == SymbolKind::S_PROC_ID_END) |
| 152 | return SymbolKind::S_END; |
| 153 | return Kind; |
| 154 | } |
| 155 | |
| 156 | /// Copy the symbol record. In a PDB, symbol records must be 4 byte aligned. |
| 157 | /// The object file may not be aligned. |
| 158 | static MutableArrayRef<uint8_t> copySymbolForPdb(const CVSymbol &Sym, |
| 159 | BumpPtrAllocator &Alloc) { |
| 160 | size_t Size = alignTo(Sym.length(), alignOf(CodeViewContainer::Pdb)); |
| 161 | assert(Size >= 4 && "record too short"); |
| 162 | assert(Size <= MaxRecordLength && "record too long"); |
| 163 | void *Mem = Alloc.Allocate(Size, 4); |
| 164 | |
| 165 | // Copy the symbol record and zero out any padding bytes. |
| 166 | MutableArrayRef<uint8_t> NewData(reinterpret_cast<uint8_t *>(Mem), Size); |
| 167 | memcpy(NewData.data(), Sym.data().data(), Sym.length()); |
| 168 | memset(NewData.data() + Sym.length(), 0, Size - Sym.length()); |
| 169 | |
| 170 | // Update the record prefix length. It should point to the beginning of the |
| 171 | // next record. MSVC does some canonicalization of the record kind, so we do |
| 172 | // that as well. |
| 173 | auto *Prefix = reinterpret_cast<RecordPrefix *>(Mem); |
| 174 | Prefix->RecordKind = canonicalizeSymbolKind(Sym.kind()); |
| 175 | Prefix->RecordLen = Size - 2; |
| 176 | return NewData; |
| 177 | } |
| 178 | |
| 179 | static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjectFile *File, |
| 180 | ArrayRef<TypeIndex> TypeIndexMap, |
| 181 | BinaryStreamRef SymData) { |
| 182 | // FIXME: Improve error recovery by warning and skipping records when |
| 183 | // possible. |
| 184 | CVSymbolArray Syms; |
| 185 | BinaryStreamReader Reader(SymData); |
| 186 | ExitOnErr(Reader.readArray(Syms, Reader.getLength())); |
| 187 | for (const CVSymbol &Sym : Syms) { |
| 188 | // Discover type index references in the record. Skip it if we don't know |
| 189 | // where they are. |
| 190 | SmallVector<TiReference, 32> TypeRefs; |
| 191 | if (!discoverTypeIndices(Sym, TypeRefs)) { |
| 192 | log("ignoring unknown symbol record with kind 0x" + utohexstr(Sym.kind())); |
| 193 | continue; |
| 194 | } |
| 195 | |
| 196 | // Copy the symbol record so we can mutate it. |
| 197 | MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc); |
| 198 | |
| 199 | // Re-map all the type index references. |
| 200 | MutableArrayRef<uint8_t> Contents = |
| 201 | NewData.drop_front(sizeof(RecordPrefix)); |
| 202 | if (!remapTypesInSymbolRecord(File, Contents, TypeIndexMap, TypeRefs)) |
| 203 | continue; |
| 204 | |
| 205 | // FIXME: Fill in "Parent" and "End" fields by maintaining a stack of |
| 206 | // scopes. |
| 207 | |
| 208 | // Add the symbol to the module. |
| 209 | File->ModuleDBI->addSymbol(CVSymbol(Sym.kind(), NewData)); |
| 210 | } |
| 211 | } |
| 212 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 213 | // Allocate memory for a .debug$S section and relocate it. |
| 214 | static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc, |
| 215 | SectionChunk *DebugChunk) { |
| 216 | uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize()); |
| 217 | assert(DebugChunk->OutputSectionOff == 0 && |
| 218 | "debug sections should not be in output sections"); |
| 219 | DebugChunk->writeTo(Buffer); |
| 220 | return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()), |
| 221 | ".debug$S"); |
| 222 | } |
| 223 | |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 224 | // Add all object files to the PDB. Merge .debug$T sections into IpiData and |
| 225 | // TpiData. |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 226 | static void addObjectsToPDB(BumpPtrAllocator &Alloc, SymbolTable *Symtab, |
| 227 | pdb::PDBFileBuilder &Builder, |
| 228 | TypeTableBuilder &TypeTable, |
| 229 | TypeTableBuilder &IDTable) { |
Zachary Turner | 2897e03 | 2017-05-25 21:16:03 +0000 | [diff] [blame] | 230 | // Follow type servers. If the same type server is encountered more than |
| 231 | // once for this instance of `PDBTypeServerHandler` (for example if many |
| 232 | // object files reference the same TypeServer), the types from the |
| 233 | // TypeServer will only be visited once. |
| 234 | pdb::PDBTypeServerHandler Handler; |
| 235 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 236 | // PDBs use a single global string table for filenames in the file checksum |
| 237 | // table. |
| 238 | auto PDBStrTab = std::make_shared<DebugStringTableSubsection>(); |
| 239 | |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 240 | // Visit all .debug$T sections to add them to Builder. |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 241 | for (ObjectFile *File : Symtab->ObjectFiles) { |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 242 | // Add a module descriptor for every object file. We need to put an absolute |
| 243 | // path to the object into the PDB. If this is a plain object, we make its |
| 244 | // path absolute. If it's an object in an archive, we make the archive path |
| 245 | // absolute. |
| 246 | bool InArchive = !File->ParentName.empty(); |
| 247 | SmallString<128> Path = InArchive ? File->ParentName : File->getName(); |
| 248 | sys::fs::make_absolute(Path); |
| 249 | StringRef Name = InArchive ? File->getName() : StringRef(Path); |
| 250 | File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name)); |
| 251 | File->ModuleDBI->setObjFileName(Path); |
| 252 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 253 | // Before we can process symbol substreams from .debug$S, we need to process |
| 254 | // type information, file checksums, and the string table. Add type info to |
| 255 | // the PDB first, so that we can get the map from object file type and item |
| 256 | // indices to PDB type and item indices. |
| 257 | SmallVector<TypeIndex, 128> TypeIndexMap; |
| 258 | mergeDebugT(File, IDTable, TypeTable, TypeIndexMap, Handler); |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 259 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 260 | // Now do all line info. |
| 261 | for (SectionChunk *DebugChunk : File->getDebugChunks()) { |
Reid Kleckner | f5bb738 | 2017-06-20 17:14:09 +0000 | [diff] [blame] | 262 | if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S") |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 263 | continue; |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 264 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 265 | ArrayRef<uint8_t> RelocatedDebugContents = |
| 266 | relocateDebugChunk(Alloc, DebugChunk); |
| 267 | if (RelocatedDebugContents.empty()) |
| 268 | continue; |
| 269 | |
| 270 | DebugSubsectionArray Subsections; |
| 271 | BinaryStreamReader Reader(RelocatedDebugContents, support::little); |
| 272 | ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size())); |
| 273 | |
| 274 | DebugStringTableSubsectionRef CVStrTab; |
| 275 | DebugChecksumsSubsectionRef Checksums; |
| 276 | for (const DebugSubsectionRecord &SS : Subsections) { |
| 277 | switch (SS.kind()) { |
| 278 | case DebugSubsectionKind::StringTable: |
| 279 | ExitOnErr(CVStrTab.initialize(SS.getRecordData())); |
| 280 | break; |
| 281 | case DebugSubsectionKind::FileChecksums: |
| 282 | ExitOnErr(Checksums.initialize(SS.getRecordData())); |
| 283 | break; |
| 284 | case DebugSubsectionKind::Lines: |
| 285 | // We can add the relocated line table directly to the PDB without |
| 286 | // modification because the file checksum offsets will stay the same. |
| 287 | File->ModuleDBI->addDebugSubsection(SS); |
| 288 | break; |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame^] | 289 | case DebugSubsectionKind::Symbols: |
| 290 | mergeSymbolRecords(Alloc, File, TypeIndexMap, SS.getRecordData()); |
| 291 | break; |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 292 | default: |
| 293 | // FIXME: Process the rest of the subsections. |
| 294 | break; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if (Checksums.valid()) { |
| 299 | // Make a new file checksum table that refers to offsets in the PDB-wide |
| 300 | // string table. Generally the string table subsection appears after the |
| 301 | // checksum table, so we have to do this after looping over all the |
| 302 | // subsections. |
| 303 | if (!CVStrTab.valid()) |
| 304 | fatal(".debug$S sections must have both a string table subsection " |
| 305 | "and a checksum subsection table or neither"); |
| 306 | auto NewChecksums = |
Reid Kleckner | adea0ce | 2017-06-19 17:27:31 +0000 | [diff] [blame] | 307 | make_unique<DebugChecksumsSubsection>(*PDBStrTab); |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 308 | for (FileChecksumEntry &FC : Checksums) { |
| 309 | StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset)); |
| 310 | ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile( |
| 311 | *File->ModuleDBI, FileName)); |
| 312 | NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum); |
| 313 | } |
| 314 | File->ModuleDBI->addDebugSubsection(std::move(NewChecksums)); |
| 315 | } |
| 316 | } |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 319 | Builder.getStringTableBuilder().setStrings(*PDBStrTab); |
| 320 | |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 321 | // Construct TPI stream contents. |
Reid Kleckner | 13fc411 | 2017-04-04 00:56:34 +0000 | [diff] [blame] | 322 | addTypeInfo(Builder.getTpiBuilder(), TypeTable); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 323 | |
| 324 | // Construct IPI stream contents. |
Reid Kleckner | 13fc411 | 2017-04-04 00:56:34 +0000 | [diff] [blame] | 325 | addTypeInfo(Builder.getIpiBuilder(), IDTable); |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 328 | // Creates a PDB file. |
| 329 | void coff::createPDB(StringRef Path, SymbolTable *Symtab, |
Saleem Abdulrasool | df8a13b | 2017-01-04 17:56:54 +0000 | [diff] [blame] | 330 | ArrayRef<uint8_t> SectionTable, |
| 331 | const llvm::codeview::DebugInfo *DI) { |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 332 | BumpPtrAllocator Alloc; |
| 333 | pdb::PDBFileBuilder Builder(Alloc); |
Rui Ueyama | 1297954 | 2016-09-30 20:53:45 +0000 | [diff] [blame] | 334 | ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 335 | |
Rui Ueyama | 8d3fb5d | 2016-10-05 22:08:58 +0000 | [diff] [blame] | 336 | // Create streams in MSF for predefined streams, namely |
| 337 | // PDB, TPI, DBI and IPI. |
| 338 | for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I) |
| 339 | ExitOnErr(Builder.getMsfBuilder().addStream(0)); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 340 | |
Rui Ueyama | bb542b3 | 2016-09-16 22:51:17 +0000 | [diff] [blame] | 341 | // Add an Info stream. |
| 342 | auto &InfoBuilder = Builder.getInfoBuilder(); |
Saleem Abdulrasool | 0acd6dd | 2017-02-07 04:28:02 +0000 | [diff] [blame] | 343 | InfoBuilder.setAge(DI ? DI->PDB70.Age : 0); |
| 344 | |
| 345 | pdb::PDB_UniqueId uuid{}; |
| 346 | if (DI) |
| 347 | memcpy(&uuid, &DI->PDB70.Signature, sizeof(uuid)); |
| 348 | InfoBuilder.setGuid(uuid); |
Rui Ueyama | bb542b3 | 2016-09-16 22:51:17 +0000 | [diff] [blame] | 349 | // Should be the current time, but set 0 for reproducibilty. |
| 350 | InfoBuilder.setSignature(0); |
Rui Ueyama | bb542b3 | 2016-09-16 22:51:17 +0000 | [diff] [blame] | 351 | InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 352 | |
Rui Ueyama | 1343fac | 2016-10-06 22:52:01 +0000 | [diff] [blame] | 353 | // Add an empty DPI stream. |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 354 | pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder(); |
Rui Ueyama | 1343fac | 2016-10-06 22:52:01 +0000 | [diff] [blame] | 355 | DbiBuilder.setVersionHeader(pdb::PdbDbiV110); |
| 356 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 357 | TypeTableBuilder TypeTable(BAlloc); |
| 358 | TypeTableBuilder IDTable(BAlloc); |
| 359 | addObjectsToPDB(Alloc, Symtab, Builder, TypeTable, IDTable); |
Rui Ueyama | d381c98 | 2016-10-05 21:37:25 +0000 | [diff] [blame] | 360 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 361 | // Add Section Contributions. |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 362 | addSectionContribs(Symtab, DbiBuilder); |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 363 | |
Rui Ueyama | 20df4ec | 2016-10-31 21:09:21 +0000 | [diff] [blame] | 364 | // Add Section Map stream. |
| 365 | ArrayRef<object::coff_section> Sections = { |
Rui Ueyama | 6294a24 | 2016-11-04 17:41:29 +0000 | [diff] [blame] | 366 | (const object::coff_section *)SectionTable.data(), |
Rui Ueyama | 20df4ec | 2016-10-31 21:09:21 +0000 | [diff] [blame] | 367 | SectionTable.size() / sizeof(object::coff_section)}; |
| 368 | std::vector<pdb::SecMapEntry> SectionMap = |
| 369 | pdb::DbiStreamBuilder::createSectionMap(Sections); |
| 370 | DbiBuilder.setSectionMap(SectionMap); |
| 371 | |
Zachary Turner | ea4e607 | 2017-03-15 22:18:53 +0000 | [diff] [blame] | 372 | ExitOnErr(DbiBuilder.addModuleInfo("* Linker *")); |
Rui Ueyama | c91f716 | 2016-11-16 01:10:46 +0000 | [diff] [blame] | 373 | |
Rui Ueyama | 9f66f82 | 2016-10-11 19:45:07 +0000 | [diff] [blame] | 374 | // Add COFF section header stream. |
| 375 | ExitOnErr( |
| 376 | DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable)); |
| 377 | |
Rui Ueyama | 3e9d6bb | 2016-09-26 23:53:55 +0000 | [diff] [blame] | 378 | // Write to a file. |
Rui Ueyama | 5c82eea | 2016-09-30 20:39:04 +0000 | [diff] [blame] | 379 | ExitOnErr(Builder.commit(Path)); |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 380 | } |