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