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