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 | a9054dd | 2017-01-11 00:35:43 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/CodeView/CVTypeDumper.h" |
Rui Ueyama | 24625fd | 2016-11-22 23:51:34 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/CodeView/SymbolDumper.h" |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/CodeView/TypeDatabase.h" |
| 20 | #include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h" |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/CodeView/TypeStreamMerger.h" |
| 22 | #include "llvm/DebugInfo/CodeView/TypeTableBuilder.h" |
Rui Ueyama | 24625fd | 2016-11-22 23:51:34 +0000 | [diff] [blame] | 23 | #include "llvm/DebugInfo/MSF/ByteStream.h" |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 24 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 25 | #include "llvm/DebugInfo/MSF/MSFCommon.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame^] | 26 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 27 | #include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h" |
| 28 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
| 29 | #include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h" |
| 30 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
| 31 | #include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h" |
| 32 | #include "llvm/DebugInfo/PDB/Native/StringTableBuilder.h" |
| 33 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
| 34 | #include "llvm/DebugInfo/PDB/Native/TpiStreamBuilder.h" |
Rui Ueyama | 20df4ec | 2016-10-31 21:09:21 +0000 | [diff] [blame] | 35 | #include "llvm/Object/COFF.h" |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Endian.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 37 | #include "llvm/Support/FileOutputBuffer.h" |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 38 | #include "llvm/Support/ScopedPrinter.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 39 | #include <memory> |
| 40 | |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame] | 41 | using namespace lld; |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 42 | using namespace lld::coff; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 43 | using namespace llvm; |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 44 | using namespace llvm::codeview; |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 45 | using namespace llvm::support; |
| 46 | using namespace llvm::support::endian; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 47 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 48 | using llvm::object::coff_section; |
| 49 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 50 | static ExitOnError ExitOnErr; |
| 51 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 52 | // Returns a list of all SectionChunks. |
| 53 | static std::vector<coff_section> getInputSections(SymbolTable *Symtab) { |
| 54 | std::vector<coff_section> V; |
| 55 | for (Chunk *C : Symtab->getChunks()) |
| 56 | if (auto *SC = dyn_cast<SectionChunk>(C)) |
| 57 | V.push_back(*SC->Header); |
| 58 | return V; |
| 59 | } |
| 60 | |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 61 | static SectionChunk *findByName(std::vector<SectionChunk *> &Sections, |
| 62 | StringRef Name) { |
| 63 | for (SectionChunk *C : Sections) |
| 64 | if (C->getSectionName() == Name) |
| 65 | return C; |
| 66 | return nullptr; |
| 67 | } |
| 68 | |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 69 | static ArrayRef<uint8_t> getDebugSection(ObjectFile *File, StringRef SecName) { |
| 70 | SectionChunk *Sec = findByName(File->getDebugChunks(), SecName); |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 71 | if (!Sec) |
Rui Ueyama | 26186c7 | 2016-12-09 04:46:54 +0000 | [diff] [blame] | 72 | return {}; |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 73 | |
| 74 | // First 4 bytes are section magic. |
| 75 | ArrayRef<uint8_t> Data = Sec->getContents(); |
| 76 | if (Data.size() < 4) |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 77 | fatal(SecName + " too short"); |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 78 | if (read32le(Data.data()) != COFF::DEBUG_SECTION_MAGIC) |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 79 | fatal(SecName + " has an invalid magic"); |
Rui Ueyama | 26186c7 | 2016-12-09 04:46:54 +0000 | [diff] [blame] | 80 | return Data.slice(4); |
| 81 | } |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 82 | |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 83 | // Merge .debug$T sections and returns it. |
| 84 | static std::vector<uint8_t> mergeDebugT(SymbolTable *Symtab) { |
| 85 | ScopedPrinter W(outs()); |
| 86 | |
| 87 | // Visit all .debug$T sections to add them to Builder. |
| 88 | codeview::TypeTableBuilder Builder(BAlloc); |
| 89 | for (ObjectFile *File : Symtab->ObjectFiles) { |
| 90 | ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T"); |
| 91 | if (Data.empty()) |
| 92 | continue; |
| 93 | |
| 94 | msf::ByteStream Stream(Data); |
| 95 | codeview::CVTypeArray Types; |
| 96 | msf::StreamReader Reader(Stream); |
| 97 | if (auto EC = Reader.readArray(Types, Reader.getLength())) |
| 98 | fatal(EC, "Reader::readArray failed"); |
| 99 | if (!codeview::mergeTypeStreams(Builder, Types)) |
| 100 | fatal("codeview::mergeTypeStreams failed"); |
| 101 | } |
| 102 | |
| 103 | // Construct section contents. |
| 104 | std::vector<uint8_t> V; |
| 105 | Builder.ForEachRecord([&](TypeIndex TI, ArrayRef<uint8_t> Rec) { |
| 106 | V.insert(V.end(), Rec.begin(), Rec.end()); |
| 107 | }); |
| 108 | return V; |
| 109 | } |
| 110 | |
Rui Ueyama | 26186c7 | 2016-12-09 04:46:54 +0000 | [diff] [blame] | 111 | static void dumpDebugT(ScopedPrinter &W, ObjectFile *File) { |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 112 | ListScope LS(W, "DebugT"); |
| 113 | ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$T"); |
Rui Ueyama | 26186c7 | 2016-12-09 04:46:54 +0000 | [diff] [blame] | 114 | if (Data.empty()) |
| 115 | return; |
| 116 | |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 117 | TypeDatabase TDB; |
| 118 | TypeDumpVisitor TDV(TDB, &W, false); |
| 119 | CVTypeDumper TypeDumper(TDB); |
| 120 | if (auto EC = TypeDumper.dump(Data, TDV)) |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 121 | fatal(EC, "CVTypeDumper::dump failed"); |
| 122 | } |
| 123 | |
| 124 | static void dumpDebugS(ScopedPrinter &W, ObjectFile *File) { |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 125 | ListScope LS(W, "DebugS"); |
| 126 | ArrayRef<uint8_t> Data = getDebugSection(File, ".debug$S"); |
| 127 | if (Data.empty()) |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 128 | return; |
| 129 | |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 130 | msf::ByteStream Stream(Data); |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 131 | CVSymbolArray Symbols; |
| 132 | msf::StreamReader Reader(Stream); |
| 133 | if (auto EC = Reader.readArray(Symbols, Reader.getLength())) |
| 134 | fatal(EC, "StreamReader.readArray<CVSymbolArray> failed"); |
| 135 | |
Zachary Turner | 629cb7d | 2017-01-11 23:24:22 +0000 | [diff] [blame] | 136 | TypeDatabase TDB; |
| 137 | CVSymbolDumper SymbolDumper(W, TDB, nullptr, false); |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 138 | if (auto EC = SymbolDumper.dump(Symbols)) |
| 139 | fatal(EC, "CVSymbolDumper::dump failed"); |
| 140 | } |
| 141 | |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 142 | // Dump CodeView debug info. This is for debugging. |
| 143 | static void dumpCodeView(SymbolTable *Symtab) { |
| 144 | ScopedPrinter W(outs()); |
| 145 | |
| 146 | for (ObjectFile *File : Symtab->ObjectFiles) { |
Rui Ueyama | c5cb737 | 2016-12-01 01:22:48 +0000 | [diff] [blame] | 147 | dumpDebugT(W, File); |
| 148 | dumpDebugS(W, File); |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 149 | } |
| 150 | } |
| 151 | |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 152 | static void addTypeInfo(pdb::TpiStreamBuilder &TpiBuilder, |
| 153 | ArrayRef<uint8_t> Data) { |
| 154 | msf::ByteStream Stream(Data); |
| 155 | codeview::CVTypeArray Records; |
| 156 | msf::StreamReader Reader(Stream); |
| 157 | if (auto EC = Reader.readArray(Records, Reader.getLength())) |
| 158 | fatal(EC, "Reader.readArray failed"); |
| 159 | for (const codeview::CVType &Rec : Records) |
| 160 | TpiBuilder.addTypeRecord(Rec); |
Rui Ueyama | 26186c7 | 2016-12-09 04:46:54 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 163 | // Creates a PDB file. |
| 164 | void coff::createPDB(StringRef Path, SymbolTable *Symtab, |
Saleem Abdulrasool | df8a13b | 2017-01-04 17:56:54 +0000 | [diff] [blame] | 165 | ArrayRef<uint8_t> SectionTable, |
| 166 | const llvm::codeview::DebugInfo *DI) { |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame] | 167 | if (Config->DumpPdb) |
| 168 | dumpCodeView(Symtab); |
| 169 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 170 | BumpPtrAllocator Alloc; |
| 171 | pdb::PDBFileBuilder Builder(Alloc); |
Rui Ueyama | 1297954 | 2016-09-30 20:53:45 +0000 | [diff] [blame] | 172 | ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 173 | |
Rui Ueyama | 8d3fb5d | 2016-10-05 22:08:58 +0000 | [diff] [blame] | 174 | // Create streams in MSF for predefined streams, namely |
| 175 | // PDB, TPI, DBI and IPI. |
| 176 | for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I) |
| 177 | ExitOnErr(Builder.getMsfBuilder().addStream(0)); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 178 | |
Rui Ueyama | bb542b3 | 2016-09-16 22:51:17 +0000 | [diff] [blame] | 179 | // Add an Info stream. |
| 180 | auto &InfoBuilder = Builder.getInfoBuilder(); |
Saleem Abdulrasool | df8a13b | 2017-01-04 17:56:54 +0000 | [diff] [blame] | 181 | InfoBuilder.setAge(DI->PDB70.Age); |
| 182 | InfoBuilder.setGuid( |
| 183 | *reinterpret_cast<const pdb::PDB_UniqueId *>(&DI->PDB70.Signature)); |
Rui Ueyama | bb542b3 | 2016-09-16 22:51:17 +0000 | [diff] [blame] | 184 | // Should be the current time, but set 0 for reproducibilty. |
| 185 | InfoBuilder.setSignature(0); |
Rui Ueyama | bb542b3 | 2016-09-16 22:51:17 +0000 | [diff] [blame] | 186 | InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 187 | |
Rui Ueyama | 1343fac | 2016-10-06 22:52:01 +0000 | [diff] [blame] | 188 | // Add an empty DPI stream. |
| 189 | auto &DbiBuilder = Builder.getDbiBuilder(); |
| 190 | DbiBuilder.setVersionHeader(pdb::PdbDbiV110); |
| 191 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 192 | // Add an empty TPI stream. |
| 193 | auto &TpiBuilder = Builder.getTpiBuilder(); |
| 194 | TpiBuilder.setVersionHeader(pdb::PdbTpiV80); |
Rui Ueyama | 5289662 | 2017-01-12 03:09:25 +0000 | [diff] [blame] | 195 | std::vector<uint8_t> TpiData; |
| 196 | if (Config->DebugPdb) { |
| 197 | TpiData = mergeDebugT(Symtab); |
| 198 | addTypeInfo(TpiBuilder, TpiData); |
| 199 | } |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 200 | |
Rui Ueyama | d381c98 | 2016-10-05 21:37:25 +0000 | [diff] [blame] | 201 | // Add an empty IPI stream. |
| 202 | auto &IpiBuilder = Builder.getIpiBuilder(); |
| 203 | IpiBuilder.setVersionHeader(pdb::PdbTpiV80); |
| 204 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 205 | // Add Section Contributions. |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 206 | std::vector<pdb::SectionContrib> Contribs = |
Vitaly Buka | 4cf112c | 2016-11-14 20:21:41 +0000 | [diff] [blame] | 207 | pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab)); |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 208 | DbiBuilder.setSectionContribs(Contribs); |
| 209 | |
Rui Ueyama | 20df4ec | 2016-10-31 21:09:21 +0000 | [diff] [blame] | 210 | // Add Section Map stream. |
| 211 | ArrayRef<object::coff_section> Sections = { |
Rui Ueyama | 6294a24 | 2016-11-04 17:41:29 +0000 | [diff] [blame] | 212 | (const object::coff_section *)SectionTable.data(), |
Rui Ueyama | 20df4ec | 2016-10-31 21:09:21 +0000 | [diff] [blame] | 213 | SectionTable.size() / sizeof(object::coff_section)}; |
| 214 | std::vector<pdb::SecMapEntry> SectionMap = |
| 215 | pdb::DbiStreamBuilder::createSectionMap(Sections); |
| 216 | DbiBuilder.setSectionMap(SectionMap); |
| 217 | |
Rui Ueyama | c91f716 | 2016-11-16 01:10:46 +0000 | [diff] [blame] | 218 | ExitOnErr(DbiBuilder.addModuleInfo("", "* Linker *")); |
| 219 | |
Rui Ueyama | 9f66f82 | 2016-10-11 19:45:07 +0000 | [diff] [blame] | 220 | // Add COFF section header stream. |
| 221 | ExitOnErr( |
| 222 | DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable)); |
| 223 | |
Rui Ueyama | 3e9d6bb | 2016-09-26 23:53:55 +0000 | [diff] [blame] | 224 | // Write to a file. |
Rui Ueyama | 5c82eea | 2016-09-30 20:39:04 +0000 | [diff] [blame] | 225 | ExitOnErr(Builder.commit(Path)); |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 226 | } |