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" |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame^] | 16 | #include "llvm/DebugInfo/CodeView/TypeDumper.h" |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/MSF/MSFBuilder.h" |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/MSF/MSFCommon.h" |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/PDB/Raw/DbiStream.h" |
| 20 | #include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h" |
| 21 | #include "llvm/DebugInfo/PDB/Raw/InfoStream.h" |
| 22 | #include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h" |
| 23 | #include "llvm/DebugInfo/PDB/Raw/PDBFile.h" |
| 24 | #include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h" |
| 25 | #include "llvm/DebugInfo/PDB/Raw/TpiStream.h" |
| 26 | #include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h" |
Rui Ueyama | 20df4ec | 2016-10-31 21:09:21 +0000 | [diff] [blame] | 27 | #include "llvm/Object/COFF.h" |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Endian.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 29 | #include "llvm/Support/FileOutputBuffer.h" |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame^] | 30 | #include "llvm/Support/ScopedPrinter.h" |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 31 | #include <memory> |
| 32 | |
Rui Ueyama | 1d99ab3 | 2016-09-15 22:24:51 +0000 | [diff] [blame] | 33 | using namespace lld; |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 34 | using namespace lld::coff; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame^] | 36 | using namespace llvm::codeview; |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 37 | using namespace llvm::support; |
| 38 | using namespace llvm::support::endian; |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 39 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 40 | using llvm::object::coff_section; |
| 41 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 42 | static ExitOnError ExitOnErr; |
| 43 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 44 | // Returns a list of all SectionChunks. |
| 45 | static std::vector<coff_section> getInputSections(SymbolTable *Symtab) { |
| 46 | std::vector<coff_section> V; |
| 47 | for (Chunk *C : Symtab->getChunks()) |
| 48 | if (auto *SC = dyn_cast<SectionChunk>(C)) |
| 49 | V.push_back(*SC->Header); |
| 50 | return V; |
| 51 | } |
| 52 | |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame^] | 53 | static SectionChunk *findByName(std::vector<SectionChunk *> &Sections, |
| 54 | StringRef Name) { |
| 55 | for (SectionChunk *C : Sections) |
| 56 | if (C->getSectionName() == Name) |
| 57 | return C; |
| 58 | return nullptr; |
| 59 | } |
| 60 | |
| 61 | // Dump CodeView debug info. This is for debugging. |
| 62 | static void dumpCodeView(SymbolTable *Symtab) { |
| 63 | ScopedPrinter W(outs()); |
| 64 | |
| 65 | for (ObjectFile *File : Symtab->ObjectFiles) { |
| 66 | SectionChunk *C = findByName(File->getDebugChunks(), ".debug$T"); |
| 67 | if (!C) |
| 68 | continue; |
| 69 | |
| 70 | CVTypeDumper TypeDumper(&W, false); |
| 71 | if (auto EC = TypeDumper.dump(C->getContents())) |
| 72 | fatal(EC, "CVTypeDumper::dump failed"); |
| 73 | } |
| 74 | } |
| 75 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 76 | // Creates a PDB file. |
| 77 | void coff::createPDB(StringRef Path, SymbolTable *Symtab, |
| 78 | ArrayRef<uint8_t> SectionTable) { |
Rui Ueyama | be939b3 | 2016-11-21 17:22:35 +0000 | [diff] [blame^] | 79 | if (Config->DumpPdb) |
| 80 | dumpCodeView(Symtab); |
| 81 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 82 | BumpPtrAllocator Alloc; |
| 83 | pdb::PDBFileBuilder Builder(Alloc); |
Rui Ueyama | 1297954 | 2016-09-30 20:53:45 +0000 | [diff] [blame] | 84 | ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 85 | |
Rui Ueyama | 8d3fb5d | 2016-10-05 22:08:58 +0000 | [diff] [blame] | 86 | // Create streams in MSF for predefined streams, namely |
| 87 | // PDB, TPI, DBI and IPI. |
| 88 | for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I) |
| 89 | ExitOnErr(Builder.getMsfBuilder().addStream(0)); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 90 | |
Rui Ueyama | bb542b3 | 2016-09-16 22:51:17 +0000 | [diff] [blame] | 91 | // Add an Info stream. |
| 92 | auto &InfoBuilder = Builder.getInfoBuilder(); |
| 93 | InfoBuilder.setAge(1); |
| 94 | |
| 95 | // Should be a random number, 0 for now. |
| 96 | InfoBuilder.setGuid({}); |
| 97 | |
| 98 | // Should be the current time, but set 0 for reproducibilty. |
| 99 | InfoBuilder.setSignature(0); |
Rui Ueyama | bb542b3 | 2016-09-16 22:51:17 +0000 | [diff] [blame] | 100 | InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70); |
Rui Ueyama | 7f38299 | 2016-09-15 18:55:18 +0000 | [diff] [blame] | 101 | |
Rui Ueyama | 1343fac | 2016-10-06 22:52:01 +0000 | [diff] [blame] | 102 | // Add an empty DPI stream. |
| 103 | auto &DbiBuilder = Builder.getDbiBuilder(); |
| 104 | DbiBuilder.setVersionHeader(pdb::PdbDbiV110); |
| 105 | |
Rui Ueyama | b28c6d4 | 2016-09-16 04:32:33 +0000 | [diff] [blame] | 106 | // Add an empty TPI stream. |
| 107 | auto &TpiBuilder = Builder.getTpiBuilder(); |
| 108 | TpiBuilder.setVersionHeader(pdb::PdbTpiV80); |
Rui Ueyama | 1763c0d | 2015-12-08 18:39:55 +0000 | [diff] [blame] | 109 | |
Rui Ueyama | d381c98 | 2016-10-05 21:37:25 +0000 | [diff] [blame] | 110 | // Add an empty IPI stream. |
| 111 | auto &IpiBuilder = Builder.getIpiBuilder(); |
| 112 | IpiBuilder.setVersionHeader(pdb::PdbTpiV80); |
| 113 | |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 114 | // Add Section Contributions. |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 115 | std::vector<pdb::SectionContrib> Contribs = |
Vitaly Buka | 4cf112c | 2016-11-14 20:21:41 +0000 | [diff] [blame] | 116 | pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab)); |
Rui Ueyama | 09e0b5f | 2016-11-12 00:00:51 +0000 | [diff] [blame] | 117 | DbiBuilder.setSectionContribs(Contribs); |
| 118 | |
Rui Ueyama | 20df4ec | 2016-10-31 21:09:21 +0000 | [diff] [blame] | 119 | // Add Section Map stream. |
| 120 | ArrayRef<object::coff_section> Sections = { |
Rui Ueyama | 6294a24 | 2016-11-04 17:41:29 +0000 | [diff] [blame] | 121 | (const object::coff_section *)SectionTable.data(), |
Rui Ueyama | 20df4ec | 2016-10-31 21:09:21 +0000 | [diff] [blame] | 122 | SectionTable.size() / sizeof(object::coff_section)}; |
| 123 | std::vector<pdb::SecMapEntry> SectionMap = |
| 124 | pdb::DbiStreamBuilder::createSectionMap(Sections); |
| 125 | DbiBuilder.setSectionMap(SectionMap); |
| 126 | |
Rui Ueyama | c91f716 | 2016-11-16 01:10:46 +0000 | [diff] [blame] | 127 | ExitOnErr(DbiBuilder.addModuleInfo("", "* Linker *")); |
| 128 | |
Rui Ueyama | 9f66f82 | 2016-10-11 19:45:07 +0000 | [diff] [blame] | 129 | // Add COFF section header stream. |
| 130 | ExitOnErr( |
| 131 | DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable)); |
| 132 | |
Rui Ueyama | 3e9d6bb | 2016-09-26 23:53:55 +0000 | [diff] [blame] | 133 | // Write to a file. |
Rui Ueyama | 5c82eea | 2016-09-30 20:39:04 +0000 | [diff] [blame] | 134 | ExitOnErr(Builder.commit(Path)); |
Rui Ueyama | e737824 | 2015-12-04 23:11:05 +0000 | [diff] [blame] | 135 | } |