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