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 | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/CodeView/SymbolSerializer.h" |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h" |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 24 | #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h" |
| 25 | #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h" |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 27 | #include "llvm/DebugInfo/MSF/MSFCommon.h" |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 28 | #include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 29 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 30 | #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h" |
| 31 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
| 32 | #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h" |
| 33 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
| 34 | #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h" |
Rafael Espindola | a0f30da | 2017-05-02 20:19:42 +0000 | [diff] [blame] | 35 | #include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h" |
Zachary Turner | 7b327d0 | 2017-02-16 23:35:45 +0000 | [diff] [blame] | 36 | #include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 37 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
| 38 | #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h" |
Rui Ueyama | 20df4ec | 2016-10-31 21:09:21 +0000 | [diff] [blame] | 39 | #include "llvm/Object/COFF.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 40 | #include "llvm/Support/BinaryByteStream.h" |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Endian.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 42 | #include "llvm/Support/FileOutputBuffer.h" |
Zachary Turner | 7b327d0 | 2017-02-16 23:35:45 +0000 | [diff] [blame] | 43 | #include "llvm/Support/Path.h" |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 44 | #include "llvm/Support/ScopedPrinter.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 45 | #include <memory> |
| 46 | |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame] | 47 | using namespace lld; |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 48 | using namespace lld::coff; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 49 | using namespace llvm; |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 50 | using namespace llvm::codeview; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 51 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 52 | using llvm::object::coff_section; |
| 53 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 54 | static ExitOnError ExitOnErr; |
| 55 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 56 | // Returns a list of all SectionChunks. |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 57 | static void addSectionContribs(SymbolTable *Symtab, pdb::DbiStreamBuilder &DbiBuilder) { |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 58 | for (Chunk *C : Symtab->getChunks()) |
| 59 | if (auto *SC = dyn_cast<SectionChunk>(C)) |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 60 | DbiBuilder.addSectionContrib(SC->File->ModuleDBI, SC->Header); |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 63 | static SectionChunk *findByName(std::vector<SectionChunk *> &Sections, |
| 64 | StringRef Name) { |
| 65 | for (SectionChunk *C : Sections) |
| 66 | if (C->getSectionName() == Name) |
| 67 | return C; |
| 68 | return nullptr; |
| 69 | } |
| 70 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 71 | static ArrayRef<uint8_t> consumeDebugMagic(ArrayRef<uint8_t> Data, |
| 72 | StringRef SecName) { |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 73 | // First 4 bytes are section magic. |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 74 | if (Data.size() < 4) |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 75 | fatal(SecName + " too short"); |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 76 | if (support::endian::read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC) |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 77 | fatal(SecName + " has an invalid magic"); |
Rui Ueyama | 26186c7 | 2016-12-09 04:46:54 +0000 | [diff] [blame] | 78 | return Data.slice(4); |
| 79 | } |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 80 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 81 | static ArrayRef<uint8_t> getDebugSection(ObjectFile *File, StringRef SecName) { |
| 82 | if (SectionChunk *Sec = findByName(File->getDebugChunks(), SecName)) |
| 83 | return consumeDebugMagic(Sec->getContents(), SecName); |
| 84 | return {}; |
| 85 | } |
| 86 | |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 87 | static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder, |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 88 | TypeTableBuilder &TypeTable) { |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 89 | // Start the TPI or IPI stream header. |
| 90 | TpiBuilder.setVersionHeader(pdb::PdbTpiV80); |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 91 | |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 92 | // Flatten the in memory type table. |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 93 | TypeTable.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) { |
Reid Kleckner | 13fc411 | 2017-04-04 00:56:34 +0000 | [diff] [blame] | 94 | // FIXME: Hash types. |
| 95 | TpiBuilder.addTypeRecord(Rec, None); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 96 | }); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 99 | static void mergeDebugT(ObjectFile *File, |
| 100 | TypeTableBuilder &IDTable, |
| 101 | TypeTableBuilder &TypeTable, |
| 102 | SmallVectorImpl<TypeIndex> &TypeIndexMap, |
| 103 | pdb::PDBTypeServerHandler &Handler) { |
| 104 | ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T"); |
| 105 | if (Data.empty()) |
| 106 | return; |
| 107 | |
| 108 | BinaryByteStream Stream(Data, support::little); |
| 109 | CVTypeArray Types; |
| 110 | BinaryStreamReader Reader(Stream); |
| 111 | Handler.addSearchPath(sys::path::parent_path(File->getName())); |
| 112 | if (auto EC = Reader.readArray(Types, Reader.getLength())) |
| 113 | fatal(EC, "Reader::readArray failed"); |
| 114 | if (auto Err = mergeTypeAndIdRecords(IDTable, TypeTable, |
| 115 | TypeIndexMap, &Handler, Types)) |
| 116 | fatal(Err, "codeview::mergeTypeStreams failed"); |
| 117 | } |
| 118 | |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 119 | static bool remapTypeIndex(TypeIndex &TI, ArrayRef<TypeIndex> TypeIndexMap) { |
| 120 | if (TI.isSimple()) |
| 121 | return true; |
| 122 | if (TI.toArrayIndex() >= TypeIndexMap.size()) |
| 123 | return false; |
| 124 | TI = TypeIndexMap[TI.toArrayIndex()]; |
| 125 | return true; |
| 126 | } |
| 127 | |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame^] | 128 | static void remapTypesInSymbolRecord(ObjectFile *File, |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 129 | MutableArrayRef<uint8_t> Contents, |
| 130 | ArrayRef<TypeIndex> TypeIndexMap, |
| 131 | ArrayRef<TiReference> TypeRefs) { |
| 132 | for (const TiReference &Ref : TypeRefs) { |
| 133 | unsigned ByteSize = Ref.Count * sizeof(TypeIndex); |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame^] | 134 | if (Contents.size() < Ref.Offset + ByteSize) |
| 135 | fatal("symbol record too short"); |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 136 | MutableArrayRef<TypeIndex> TIs( |
| 137 | reinterpret_cast<TypeIndex *>(Contents.data() + Ref.Offset), Ref.Count); |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame^] | 138 | for (TypeIndex &TI : TIs) { |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 139 | if (!remapTypeIndex(TI, TypeIndexMap)) { |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame^] | 140 | TI = TypeIndex(SimpleTypeKind::NotTranslated); |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 141 | log("ignoring symbol record in " + File->getName() + |
| 142 | " with bad type index 0x" + utohexstr(TI.getIndex())); |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame^] | 143 | continue; |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 144 | } |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame^] | 145 | } |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 146 | } |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 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 | |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 179 | /// Return true if this symbol opens a scope. This implies that the symbol has |
| 180 | /// "parent" and "end" fields, which contain the offset of the S_END or |
| 181 | /// S_INLINESITE_END record. |
| 182 | static bool symbolOpensScope(SymbolKind Kind) { |
| 183 | switch (Kind) { |
| 184 | case SymbolKind::S_GPROC32: |
| 185 | case SymbolKind::S_LPROC32: |
| 186 | case SymbolKind::S_LPROC32_ID: |
| 187 | case SymbolKind::S_GPROC32_ID: |
| 188 | case SymbolKind::S_BLOCK32: |
| 189 | case SymbolKind::S_SEPCODE: |
| 190 | case SymbolKind::S_THUNK32: |
| 191 | case SymbolKind::S_INLINESITE: |
| 192 | case SymbolKind::S_INLINESITE2: |
| 193 | return true; |
| 194 | default: |
| 195 | break; |
| 196 | } |
| 197 | return false; |
| 198 | } |
| 199 | |
| 200 | static bool symbolEndsScope(SymbolKind Kind) { |
| 201 | switch (Kind) { |
| 202 | case SymbolKind::S_END: |
| 203 | case SymbolKind::S_PROC_ID_END: |
| 204 | case SymbolKind::S_INLINESITE_END: |
| 205 | return true; |
| 206 | default: |
| 207 | break; |
| 208 | } |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | struct ScopeRecord { |
| 213 | ulittle32_t PtrParent; |
| 214 | ulittle32_t PtrEnd; |
| 215 | }; |
| 216 | |
| 217 | struct SymbolScope { |
| 218 | ScopeRecord *OpeningRecord; |
| 219 | uint32_t ScopeOffset; |
| 220 | }; |
| 221 | |
| 222 | static void scopeStackOpen(SmallVectorImpl<SymbolScope> &Stack, |
| 223 | uint32_t CurOffset, CVSymbol &Sym) { |
| 224 | assert(symbolOpensScope(Sym.kind())); |
| 225 | SymbolScope S; |
| 226 | S.ScopeOffset = CurOffset; |
| 227 | S.OpeningRecord = const_cast<ScopeRecord *>( |
| 228 | reinterpret_cast<const ScopeRecord *>(Sym.content().data())); |
| 229 | S.OpeningRecord->PtrParent = Stack.empty() ? 0 : Stack.back().ScopeOffset; |
| 230 | Stack.push_back(S); |
| 231 | } |
| 232 | |
| 233 | static void scopeStackClose(SmallVectorImpl<SymbolScope> &Stack, |
| 234 | uint32_t CurOffset, ObjectFile *File) { |
| 235 | if (Stack.empty()) { |
| 236 | warn("symbol scopes are not balanced in " + File->getName()); |
| 237 | return; |
| 238 | } |
| 239 | SymbolScope S = Stack.pop_back_val(); |
| 240 | S.OpeningRecord->PtrEnd = CurOffset; |
| 241 | } |
| 242 | |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 243 | static void mergeSymbolRecords(BumpPtrAllocator &Alloc, ObjectFile *File, |
| 244 | ArrayRef<TypeIndex> TypeIndexMap, |
| 245 | BinaryStreamRef SymData) { |
| 246 | // FIXME: Improve error recovery by warning and skipping records when |
| 247 | // possible. |
| 248 | CVSymbolArray Syms; |
| 249 | BinaryStreamReader Reader(SymData); |
| 250 | ExitOnErr(Reader.readArray(Syms, Reader.getLength())); |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 251 | SmallVector<SymbolScope, 4> Scopes; |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 252 | for (const CVSymbol &Sym : Syms) { |
| 253 | // Discover type index references in the record. Skip it if we don't know |
| 254 | // where they are. |
| 255 | SmallVector<TiReference, 32> TypeRefs; |
| 256 | if (!discoverTypeIndices(Sym, TypeRefs)) { |
| 257 | log("ignoring unknown symbol record with kind 0x" + utohexstr(Sym.kind())); |
| 258 | continue; |
| 259 | } |
| 260 | |
| 261 | // Copy the symbol record so we can mutate it. |
| 262 | MutableArrayRef<uint8_t> NewData = copySymbolForPdb(Sym, Alloc); |
| 263 | |
| 264 | // Re-map all the type index references. |
| 265 | MutableArrayRef<uint8_t> Contents = |
| 266 | NewData.drop_front(sizeof(RecordPrefix)); |
Reid Kleckner | 03b5baf | 2017-07-12 18:49:43 +0000 | [diff] [blame^] | 267 | remapTypesInSymbolRecord(File, Contents, TypeIndexMap, TypeRefs); |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 268 | |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 269 | // Fill in "Parent" and "End" fields by maintaining a stack of scopes. |
| 270 | CVSymbol NewSym(Sym.kind(), NewData); |
| 271 | if (symbolOpensScope(Sym.kind())) |
| 272 | scopeStackOpen(Scopes, File->ModuleDBI->getNextSymbolOffset(), NewSym); |
| 273 | else if (symbolEndsScope(Sym.kind())) |
| 274 | scopeStackClose(Scopes, File->ModuleDBI->getNextSymbolOffset(), File); |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 275 | |
| 276 | // Add the symbol to the module. |
Reid Kleckner | 3f85192 | 2017-07-06 16:39:32 +0000 | [diff] [blame] | 277 | File->ModuleDBI->addSymbol(NewSym); |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 281 | // Allocate memory for a .debug$S section and relocate it. |
| 282 | static ArrayRef<uint8_t> relocateDebugChunk(BumpPtrAllocator &Alloc, |
| 283 | SectionChunk *DebugChunk) { |
| 284 | uint8_t *Buffer = Alloc.Allocate<uint8_t>(DebugChunk->getSize()); |
| 285 | assert(DebugChunk->OutputSectionOff == 0 && |
| 286 | "debug sections should not be in output sections"); |
| 287 | DebugChunk->writeTo(Buffer); |
| 288 | return consumeDebugMagic(makeArrayRef(Buffer, DebugChunk->getSize()), |
| 289 | ".debug$S"); |
| 290 | } |
| 291 | |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 292 | // Add all object files to the PDB. Merge .debug$T sections into IpiData and |
| 293 | // TpiData. |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 294 | static void addObjectsToPDB(BumpPtrAllocator &Alloc, SymbolTable *Symtab, |
| 295 | pdb::PDBFileBuilder &Builder, |
| 296 | TypeTableBuilder &TypeTable, |
| 297 | TypeTableBuilder &IDTable) { |
Zachary Turner | 2897e03 | 2017-05-25 21:16:03 +0000 | [diff] [blame] | 298 | // Follow type servers. If the same type server is encountered more than |
| 299 | // once for this instance of `PDBTypeServerHandler` (for example if many |
| 300 | // object files reference the same TypeServer), the types from the |
| 301 | // TypeServer will only be visited once. |
| 302 | pdb::PDBTypeServerHandler Handler; |
| 303 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 304 | // PDBs use a single global string table for filenames in the file checksum |
| 305 | // table. |
| 306 | auto PDBStrTab = std::make_shared<DebugStringTableSubsection>(); |
| 307 | |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 308 | // Visit all .debug$T sections to add them to Builder. |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 309 | for (ObjectFile *File : Symtab->ObjectFiles) { |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 310 | // Add a module descriptor for every object file. We need to put an absolute |
| 311 | // path to the object into the PDB. If this is a plain object, we make its |
| 312 | // path absolute. If it's an object in an archive, we make the archive path |
| 313 | // absolute. |
| 314 | bool InArchive = !File->ParentName.empty(); |
| 315 | SmallString<128> Path = InArchive ? File->ParentName : File->getName(); |
| 316 | sys::fs::make_absolute(Path); |
Zachary Turner | 448dea4 | 2017-07-07 18:46:14 +0000 | [diff] [blame] | 317 | sys::path::native(Path, llvm::sys::path::Style::windows); |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 318 | StringRef Name = InArchive ? File->getName() : StringRef(Path); |
Zachary Turner | 448dea4 | 2017-07-07 18:46:14 +0000 | [diff] [blame] | 319 | |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 320 | File->ModuleDBI = &ExitOnErr(Builder.getDbiBuilder().addModuleInfo(Name)); |
| 321 | File->ModuleDBI->setObjFileName(Path); |
| 322 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 323 | // Before we can process symbol substreams from .debug$S, we need to process |
| 324 | // type information, file checksums, and the string table. Add type info to |
| 325 | // the PDB first, so that we can get the map from object file type and item |
| 326 | // indices to PDB type and item indices. |
| 327 | SmallVector<TypeIndex, 128> TypeIndexMap; |
| 328 | mergeDebugT(File, IDTable, TypeTable, TypeIndexMap, Handler); |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 329 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 330 | // Now do all line info. |
| 331 | for (SectionChunk *DebugChunk : File->getDebugChunks()) { |
Reid Kleckner | f5bb738 | 2017-06-20 17:14:09 +0000 | [diff] [blame] | 332 | if (!DebugChunk->isLive() || DebugChunk->getSectionName() != ".debug$S") |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 333 | continue; |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 334 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 335 | ArrayRef<uint8_t> RelocatedDebugContents = |
| 336 | relocateDebugChunk(Alloc, DebugChunk); |
| 337 | if (RelocatedDebugContents.empty()) |
| 338 | continue; |
| 339 | |
| 340 | DebugSubsectionArray Subsections; |
| 341 | BinaryStreamReader Reader(RelocatedDebugContents, support::little); |
| 342 | ExitOnErr(Reader.readArray(Subsections, RelocatedDebugContents.size())); |
| 343 | |
| 344 | DebugStringTableSubsectionRef CVStrTab; |
| 345 | DebugChecksumsSubsectionRef Checksums; |
| 346 | for (const DebugSubsectionRecord &SS : Subsections) { |
| 347 | switch (SS.kind()) { |
| 348 | case DebugSubsectionKind::StringTable: |
| 349 | ExitOnErr(CVStrTab.initialize(SS.getRecordData())); |
| 350 | break; |
| 351 | case DebugSubsectionKind::FileChecksums: |
| 352 | ExitOnErr(Checksums.initialize(SS.getRecordData())); |
| 353 | break; |
| 354 | case DebugSubsectionKind::Lines: |
| 355 | // We can add the relocated line table directly to the PDB without |
| 356 | // modification because the file checksum offsets will stay the same. |
| 357 | File->ModuleDBI->addDebugSubsection(SS); |
| 358 | break; |
Reid Kleckner | d0e6e24 | 2017-06-21 17:25:56 +0000 | [diff] [blame] | 359 | case DebugSubsectionKind::Symbols: |
| 360 | mergeSymbolRecords(Alloc, File, TypeIndexMap, SS.getRecordData()); |
| 361 | break; |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 362 | default: |
| 363 | // FIXME: Process the rest of the subsections. |
| 364 | break; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | if (Checksums.valid()) { |
| 369 | // Make a new file checksum table that refers to offsets in the PDB-wide |
| 370 | // string table. Generally the string table subsection appears after the |
| 371 | // checksum table, so we have to do this after looping over all the |
| 372 | // subsections. |
| 373 | if (!CVStrTab.valid()) |
| 374 | fatal(".debug$S sections must have both a string table subsection " |
| 375 | "and a checksum subsection table or neither"); |
| 376 | auto NewChecksums = |
Reid Kleckner | adea0ce | 2017-06-19 17:27:31 +0000 | [diff] [blame] | 377 | make_unique<DebugChecksumsSubsection>(*PDBStrTab); |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 378 | for (FileChecksumEntry &FC : Checksums) { |
| 379 | StringRef FileName = ExitOnErr(CVStrTab.getString(FC.FileNameOffset)); |
| 380 | ExitOnErr(Builder.getDbiBuilder().addModuleSourceFile( |
| 381 | *File->ModuleDBI, FileName)); |
| 382 | NewChecksums->addChecksum(FileName, FC.Kind, FC.Checksum); |
| 383 | } |
| 384 | File->ModuleDBI->addDebugSubsection(std::move(NewChecksums)); |
| 385 | } |
| 386 | } |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 389 | Builder.getStringTableBuilder().setStrings(*PDBStrTab); |
| 390 | |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 391 | // Construct TPI stream contents. |
Reid Kleckner | 13fc411 | 2017-04-04 00:56:34 +0000 | [diff] [blame] | 392 | addTypeInfo(Builder.getTpiBuilder(), TypeTable); |
Reid Kleckner | 5d57752 | 2017-03-24 17:26:38 +0000 | [diff] [blame] | 393 | |
| 394 | // Construct IPI stream contents. |
Reid Kleckner | 13fc411 | 2017-04-04 00:56:34 +0000 | [diff] [blame] | 395 | addTypeInfo(Builder.getIpiBuilder(), IDTable); |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 398 | static void addLinkerModuleSymbols(StringRef Path, |
| 399 | pdb::DbiModuleDescriptorBuilder &Mod, |
| 400 | BumpPtrAllocator &Allocator) { |
| 401 | codeview::SymbolSerializer Serializer(Allocator, CodeViewContainer::Pdb); |
| 402 | codeview::ObjNameSym ONS(SymbolRecordKind::ObjNameSym); |
| 403 | codeview::Compile3Sym CS(SymbolRecordKind::Compile3Sym); |
| 404 | codeview::EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym); |
| 405 | |
| 406 | ONS.Name = "* Linker *"; |
| 407 | ONS.Signature = 0; |
| 408 | |
| 409 | CS.Machine = Config->is64() ? CPUType::X64 : CPUType::Intel80386; |
| 410 | CS.Flags = CompileSym3Flags::None; |
| 411 | CS.VersionBackendBuild = 0; |
| 412 | CS.VersionBackendMajor = 0; |
| 413 | CS.VersionBackendMinor = 0; |
| 414 | CS.VersionBackendQFE = 0; |
| 415 | CS.VersionFrontendBuild = 0; |
| 416 | CS.VersionFrontendMajor = 0; |
| 417 | CS.VersionFrontendMinor = 0; |
| 418 | CS.VersionFrontendQFE = 0; |
| 419 | CS.Version = "LLVM Linker"; |
| 420 | CS.setLanguage(SourceLanguage::Link); |
| 421 | |
| 422 | ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front(); |
| 423 | std::string ArgStr = llvm::join(Args, " "); |
| 424 | EBS.Fields.push_back("cwd"); |
| 425 | SmallString<64> cwd; |
| 426 | llvm::sys::fs::current_path(cwd); |
| 427 | EBS.Fields.push_back(cwd); |
| 428 | EBS.Fields.push_back("exe"); |
Zachary Turner | 77f23b9 | 2017-07-10 21:09:11 +0000 | [diff] [blame] | 429 | EBS.Fields.push_back(Config->Argv[0]); |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 430 | EBS.Fields.push_back("pdb"); |
| 431 | EBS.Fields.push_back(Path); |
| 432 | EBS.Fields.push_back("cmd"); |
| 433 | EBS.Fields.push_back(ArgStr); |
| 434 | Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( |
| 435 | ONS, Allocator, CodeViewContainer::Pdb)); |
| 436 | Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( |
| 437 | CS, Allocator, CodeViewContainer::Pdb)); |
| 438 | Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol( |
| 439 | EBS, Allocator, CodeViewContainer::Pdb)); |
| 440 | } |
| 441 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 442 | // Creates a PDB file. |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 443 | void coff::createPDB(SymbolTable *Symtab, ArrayRef<uint8_t> SectionTable, |
Saleem Abdulrasool | df8a13b | 2017-01-04 17:56:54 +0000 | [diff] [blame] | 444 | const llvm::codeview::DebugInfo *DI) { |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 445 | BumpPtrAllocator Alloc; |
| 446 | pdb::PDBFileBuilder Builder(Alloc); |
Rui Ueyama | 1297954 | 2016-09-30 20:53:45 +0000 | [diff] [blame] | 447 | ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 448 | |
Rui Ueyama | 8d3fb5d | 2016-10-05 22:08:58 +0000 | [diff] [blame] | 449 | // Create streams in MSF for predefined streams, namely |
| 450 | // PDB, TPI, DBI and IPI. |
| 451 | for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I) |
| 452 | ExitOnErr(Builder.getMsfBuilder().addStream(0)); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 453 | |
Rui Ueyama | bb542b3 | 2016-09-16 22:51:17 +0000 | [diff] [blame] | 454 | // Add an Info stream. |
| 455 | auto &InfoBuilder = Builder.getInfoBuilder(); |
Saleem Abdulrasool | 0acd6dd | 2017-02-07 04:28:02 +0000 | [diff] [blame] | 456 | InfoBuilder.setAge(DI ? DI->PDB70.Age : 0); |
| 457 | |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 458 | llvm::SmallString<128> NativePath(Config->PDBPath.begin(), |
| 459 | Config->PDBPath.end()); |
Zachary Turner | 6c4bfba | 2017-07-07 05:04:36 +0000 | [diff] [blame] | 460 | llvm::sys::fs::make_absolute(NativePath); |
Zachary Turner | 448dea4 | 2017-07-07 18:46:14 +0000 | [diff] [blame] | 461 | llvm::sys::path::native(NativePath, llvm::sys::path::Style::windows); |
Zachary Turner | 6c4bfba | 2017-07-07 05:04:36 +0000 | [diff] [blame] | 462 | |
Saleem Abdulrasool | 0acd6dd | 2017-02-07 04:28:02 +0000 | [diff] [blame] | 463 | pdb::PDB_UniqueId uuid{}; |
| 464 | if (DI) |
| 465 | memcpy(&uuid, &DI->PDB70.Signature, sizeof(uuid)); |
| 466 | InfoBuilder.setGuid(uuid); |
Zachary Turner | 3a11fdf | 2017-07-07 20:25:39 +0000 | [diff] [blame] | 467 | InfoBuilder.setSignature(time(nullptr)); |
Rui Ueyama | bb542b3 | 2016-09-16 22:51:17 +0000 | [diff] [blame] | 468 | InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 469 | |
Zachary Turner | 6c4bfba | 2017-07-07 05:04:36 +0000 | [diff] [blame] | 470 | // Add an empty DBI stream. |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 471 | pdb::DbiStreamBuilder &DbiBuilder = Builder.getDbiBuilder(); |
Zachary Turner | c1e93e5 | 2017-07-07 18:45:56 +0000 | [diff] [blame] | 472 | DbiBuilder.setVersionHeader(pdb::PdbDbiV70); |
Zachary Turner | 3a11fdf | 2017-07-07 20:25:39 +0000 | [diff] [blame] | 473 | ExitOnErr(DbiBuilder.addDbgStream(pdb::DbgHeaderType::NewFPO, {})); |
Rui Ueyama | 1343fac | 2016-10-06 22:52:01 +0000 | [diff] [blame] | 474 | |
Zachary Turner | 6c4bfba | 2017-07-07 05:04:36 +0000 | [diff] [blame] | 475 | // It's not entirely clear what this is, but the * Linker * module uses it. |
| 476 | uint32_t PdbFilePathNI = DbiBuilder.addECName(NativePath); |
| 477 | |
Reid Kleckner | 44cdb10 | 2017-06-19 17:21:45 +0000 | [diff] [blame] | 478 | TypeTableBuilder TypeTable(BAlloc); |
| 479 | TypeTableBuilder IDTable(BAlloc); |
| 480 | addObjectsToPDB(Alloc, Symtab, Builder, TypeTable, IDTable); |
Rui Ueyama | d381c98 | 2016-10-05 21:37:25 +0000 | [diff] [blame] | 481 | |
Zachary Turner | 7eaf1d9 | 2017-07-10 22:40:20 +0000 | [diff] [blame] | 482 | // Add public and symbol records stream. |
| 483 | |
| 484 | // For now we don't actually write any thing useful to the publics stream, but |
| 485 | // the act of "getting" it also creates it lazily so that we write an empty |
| 486 | // stream. |
| 487 | (void)Builder.getPublicsBuilder(); |
| 488 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 489 | // Add Section Contributions. |
Reid Kleckner | 8cbdd0c | 2017-06-13 15:49:13 +0000 | [diff] [blame] | 490 | addSectionContribs(Symtab, DbiBuilder); |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 491 | |
Rui Ueyama | 20df4ec | 2016-10-31 21:09:21 +0000 | [diff] [blame] | 492 | // Add Section Map stream. |
| 493 | ArrayRef<object::coff_section> Sections = { |
Rui Ueyama | 6294a24 | 2016-11-04 17:41:29 +0000 | [diff] [blame] | 494 | (const object::coff_section *)SectionTable.data(), |
Rui Ueyama | 20df4ec | 2016-10-31 21:09:21 +0000 | [diff] [blame] | 495 | SectionTable.size() / sizeof(object::coff_section)}; |
| 496 | std::vector<pdb::SecMapEntry> SectionMap = |
| 497 | pdb::DbiStreamBuilder::createSectionMap(Sections); |
| 498 | DbiBuilder.setSectionMap(SectionMap); |
| 499 | |
Zachary Turner | 6c4bfba | 2017-07-07 05:04:36 +0000 | [diff] [blame] | 500 | auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *")); |
| 501 | LinkerModule.setPdbFilePathNI(PdbFilePathNI); |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 502 | addLinkerModuleSymbols(NativePath, LinkerModule, Alloc); |
Rui Ueyama | c91f716 | 2016-11-16 01:10:46 +0000 | [diff] [blame] | 503 | |
Rui Ueyama | 9f66f82 | 2016-10-11 19:45:07 +0000 | [diff] [blame] | 504 | // Add COFF section header stream. |
| 505 | ExitOnErr( |
| 506 | DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable)); |
| 507 | |
Rui Ueyama | 3e9d6bb | 2016-09-26 23:53:55 +0000 | [diff] [blame] | 508 | // Write to a file. |
Zachary Turner | 6708e0b | 2017-07-10 21:01:37 +0000 | [diff] [blame] | 509 | ExitOnErr(Builder.commit(Config->PDBPath)); |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 510 | } |