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