2015-12-05 07:11:05 +08:00
|
|
|
//===- PDB.cpp ------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-09-16 06:24:51 +08:00
|
|
|
#include "PDB.h"
|
2016-11-12 08:00:51 +08:00
|
|
|
#include "Chunks.h"
|
2016-11-22 01:22:35 +08:00
|
|
|
#include "Config.h"
|
2015-12-05 07:11:05 +08:00
|
|
|
#include "Error.h"
|
2016-11-12 08:00:51 +08:00
|
|
|
#include "SymbolTable.h"
|
|
|
|
#include "Symbols.h"
|
2016-11-23 07:51:34 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/SymbolDumper.h"
|
2016-11-22 01:22:35 +08:00
|
|
|
#include "llvm/DebugInfo/CodeView/TypeDumper.h"
|
2016-11-23 07:51:34 +08:00
|
|
|
#include "llvm/DebugInfo/MSF/ByteStream.h"
|
2016-09-16 12:32:33 +08:00
|
|
|
#include "llvm/DebugInfo/MSF/MSFBuilder.h"
|
2016-09-16 02:55:18 +08:00
|
|
|
#include "llvm/DebugInfo/MSF/MSFCommon.h"
|
2016-09-16 12:32:33 +08:00
|
|
|
#include "llvm/DebugInfo/PDB/Raw/DbiStream.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Raw/DbiStreamBuilder.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Raw/InfoStream.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Raw/InfoStreamBuilder.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Raw/PDBFile.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Raw/PDBFileBuilder.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Raw/TpiStream.h"
|
|
|
|
#include "llvm/DebugInfo/PDB/Raw/TpiStreamBuilder.h"
|
2016-11-01 05:09:21 +08:00
|
|
|
#include "llvm/Object/COFF.h"
|
2015-12-09 02:39:55 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2015-12-05 07:11:05 +08:00
|
|
|
#include "llvm/Support/FileOutputBuffer.h"
|
2016-11-22 01:22:35 +08:00
|
|
|
#include "llvm/Support/ScopedPrinter.h"
|
2015-12-05 07:11:05 +08:00
|
|
|
#include <memory>
|
|
|
|
|
2016-09-16 06:24:51 +08:00
|
|
|
using namespace lld;
|
2016-11-12 08:00:51 +08:00
|
|
|
using namespace lld::coff;
|
2015-12-05 07:11:05 +08:00
|
|
|
using namespace llvm;
|
2016-11-22 01:22:35 +08:00
|
|
|
using namespace llvm::codeview;
|
2015-12-09 02:39:55 +08:00
|
|
|
using namespace llvm::support;
|
|
|
|
using namespace llvm::support::endian;
|
2015-12-05 07:11:05 +08:00
|
|
|
|
2016-11-12 08:00:51 +08:00
|
|
|
using llvm::object::coff_section;
|
|
|
|
|
2016-09-16 12:32:33 +08:00
|
|
|
static ExitOnError ExitOnErr;
|
|
|
|
|
2016-11-12 08:00:51 +08:00
|
|
|
// Returns a list of all SectionChunks.
|
|
|
|
static std::vector<coff_section> getInputSections(SymbolTable *Symtab) {
|
|
|
|
std::vector<coff_section> V;
|
|
|
|
for (Chunk *C : Symtab->getChunks())
|
|
|
|
if (auto *SC = dyn_cast<SectionChunk>(C))
|
|
|
|
V.push_back(*SC->Header);
|
|
|
|
return V;
|
|
|
|
}
|
|
|
|
|
2016-11-22 01:22:35 +08:00
|
|
|
static SectionChunk *findByName(std::vector<SectionChunk *> &Sections,
|
|
|
|
StringRef Name) {
|
|
|
|
for (SectionChunk *C : Sections)
|
|
|
|
if (C->getSectionName() == Name)
|
|
|
|
return C;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dump CodeView debug info. This is for debugging.
|
|
|
|
static void dumpCodeView(SymbolTable *Symtab) {
|
|
|
|
ScopedPrinter W(outs());
|
|
|
|
|
|
|
|
for (ObjectFile *File : Symtab->ObjectFiles) {
|
2016-11-23 07:51:34 +08:00
|
|
|
SectionChunk *DebugT = findByName(File->getDebugChunks(), ".debug$T");
|
|
|
|
if (!DebugT)
|
2016-11-22 01:22:35 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
CVTypeDumper TypeDumper(&W, false);
|
2016-11-23 07:51:34 +08:00
|
|
|
if (auto EC = TypeDumper.dump(DebugT->getContents()))
|
2016-11-22 01:22:35 +08:00
|
|
|
fatal(EC, "CVTypeDumper::dump failed");
|
2016-11-23 07:51:34 +08:00
|
|
|
|
|
|
|
SectionChunk *DebugS = findByName(File->getDebugChunks(), ".debug$S");
|
|
|
|
if (!DebugS)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
msf::ByteStream Stream(DebugS->getContents());
|
|
|
|
CVSymbolArray Symbols;
|
|
|
|
msf::StreamReader Reader(Stream);
|
|
|
|
if (auto EC = Reader.readArray(Symbols, Reader.getLength()))
|
|
|
|
fatal(EC, "StreamReader.readArray<CVSymbolArray> failed");
|
|
|
|
|
|
|
|
CVSymbolDumper SymbolDumper(W, TypeDumper, nullptr, false);
|
|
|
|
if (auto EC = SymbolDumper.dump(Symbols))
|
|
|
|
fatal(EC, "CVSymbolDumper::dump failed");
|
2016-11-22 01:22:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-12 08:00:51 +08:00
|
|
|
// Creates a PDB file.
|
|
|
|
void coff::createPDB(StringRef Path, SymbolTable *Symtab,
|
|
|
|
ArrayRef<uint8_t> SectionTable) {
|
2016-11-22 01:22:35 +08:00
|
|
|
if (Config->DumpPdb)
|
|
|
|
dumpCodeView(Symtab);
|
|
|
|
|
2016-09-16 12:32:33 +08:00
|
|
|
BumpPtrAllocator Alloc;
|
|
|
|
pdb::PDBFileBuilder Builder(Alloc);
|
2016-10-01 04:53:45 +08:00
|
|
|
ExitOnErr(Builder.initialize(4096)); // 4096 is blocksize
|
2016-09-16 02:55:18 +08:00
|
|
|
|
2016-10-06 06:08:58 +08:00
|
|
|
// Create streams in MSF for predefined streams, namely
|
|
|
|
// PDB, TPI, DBI and IPI.
|
|
|
|
for (int I = 0; I < (int)pdb::kSpecialStreamCount; ++I)
|
|
|
|
ExitOnErr(Builder.getMsfBuilder().addStream(0));
|
2016-09-16 02:55:18 +08:00
|
|
|
|
2016-09-17 06:51:17 +08:00
|
|
|
// Add an Info stream.
|
|
|
|
auto &InfoBuilder = Builder.getInfoBuilder();
|
|
|
|
InfoBuilder.setAge(1);
|
|
|
|
|
|
|
|
// Should be a random number, 0 for now.
|
|
|
|
InfoBuilder.setGuid({});
|
|
|
|
|
|
|
|
// Should be the current time, but set 0 for reproducibilty.
|
|
|
|
InfoBuilder.setSignature(0);
|
|
|
|
InfoBuilder.setVersion(pdb::PdbRaw_ImplVer::PdbImplVC70);
|
2016-09-16 02:55:18 +08:00
|
|
|
|
2016-10-07 06:52:01 +08:00
|
|
|
// Add an empty DPI stream.
|
|
|
|
auto &DbiBuilder = Builder.getDbiBuilder();
|
|
|
|
DbiBuilder.setVersionHeader(pdb::PdbDbiV110);
|
|
|
|
|
2016-09-16 12:32:33 +08:00
|
|
|
// Add an empty TPI stream.
|
|
|
|
auto &TpiBuilder = Builder.getTpiBuilder();
|
|
|
|
TpiBuilder.setVersionHeader(pdb::PdbTpiV80);
|
2015-12-05 07:11:05 +08:00
|
|
|
|
2016-10-06 05:37:25 +08:00
|
|
|
// Add an empty IPI stream.
|
|
|
|
auto &IpiBuilder = Builder.getIpiBuilder();
|
|
|
|
IpiBuilder.setVersionHeader(pdb::PdbTpiV80);
|
|
|
|
|
2016-11-12 08:00:51 +08:00
|
|
|
// Add Section Contributions.
|
|
|
|
std::vector<pdb::SectionContrib> Contribs =
|
2016-11-15 04:21:41 +08:00
|
|
|
pdb::DbiStreamBuilder::createSectionContribs(getInputSections(Symtab));
|
2016-11-12 08:00:51 +08:00
|
|
|
DbiBuilder.setSectionContribs(Contribs);
|
|
|
|
|
2016-11-01 05:09:21 +08:00
|
|
|
// Add Section Map stream.
|
|
|
|
ArrayRef<object::coff_section> Sections = {
|
2016-11-05 01:41:29 +08:00
|
|
|
(const object::coff_section *)SectionTable.data(),
|
2016-11-01 05:09:21 +08:00
|
|
|
SectionTable.size() / sizeof(object::coff_section)};
|
|
|
|
std::vector<pdb::SecMapEntry> SectionMap =
|
|
|
|
pdb::DbiStreamBuilder::createSectionMap(Sections);
|
|
|
|
DbiBuilder.setSectionMap(SectionMap);
|
|
|
|
|
2016-11-16 09:10:46 +08:00
|
|
|
ExitOnErr(DbiBuilder.addModuleInfo("", "* Linker *"));
|
|
|
|
|
2016-10-12 03:45:07 +08:00
|
|
|
// Add COFF section header stream.
|
|
|
|
ExitOnErr(
|
|
|
|
DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
|
|
|
|
|
2016-09-27 07:53:55 +08:00
|
|
|
// Write to a file.
|
2016-10-01 04:39:04 +08:00
|
|
|
ExitOnErr(Builder.commit(Path));
|
2015-12-05 07:11:05 +08:00
|
|
|
}
|