[lld/pdb] Add some basic linker module symbols.

Differential Revision: https://reviews.llvm.org/D35152

llvm-svn: 307590
This commit is contained in:
Zachary Turner 2017-07-10 21:01:37 +00:00
parent b38736706e
commit 6708e0b45e
13 changed files with 85 additions and 18 deletions

View File

@ -92,6 +92,7 @@ struct Configuration {
bool WriteSymtab = true;
unsigned DebugTypes = static_cast<unsigned>(DebugType::None);
llvm::SmallString<128> PDBPath;
std::vector<llvm::StringRef> Argv;
// Symbols in this set are considered as live by the garbage collector.
std::set<SymbolBody *> GCRoot;

View File

@ -55,8 +55,8 @@ std::vector<SpecificAllocBase *> SpecificAllocBase::Instances;
bool link(ArrayRef<const char *> Args, raw_ostream &Diag) {
ErrorCount = 0;
ErrorOS = &Diag;
Argv0 = Args[0];
Config = make<Configuration>();
Config->Argv = {Args.begin(), Args.end()};
Config->ColorDiagnostics =
(ErrorOS == &llvm::errs() && Process::StandardErrHasColors());
Driver = make<LinkerDriver>();

View File

@ -29,7 +29,6 @@ namespace lld {
static std::mutex Mu;
namespace coff {
StringRef Argv0;
uint64_t ErrorCount;
raw_ostream *ErrorOS;
@ -45,7 +44,7 @@ static LLVM_ATTRIBUTE_NORETURN void exitLld(int Val) {
}
static void print(StringRef S, raw_ostream::Colors C) {
*ErrorOS << Argv0 + ": ";
*ErrorOS << Config->Argv[0] << ": ";
if (Config->ColorDiagnostics) {
ErrorOS->changeColor(C, true);
*ErrorOS << S;
@ -58,7 +57,7 @@ static void print(StringRef S, raw_ostream::Colors C) {
void log(const Twine &Msg) {
if (Config->Verbose) {
std::lock_guard<std::mutex> Lock(Mu);
outs() << Argv0 << ": " << Msg << "\n";
outs() << Config->Argv[0] << ": " << Msg << "\n";
outs().flush();
}
}

View File

@ -18,7 +18,6 @@ namespace coff {
extern uint64_t ErrorCount;
extern llvm::raw_ostream *ErrorOS;
extern llvm::StringRef Argv0;
void log(const Twine &Msg);
void message(const Twine &Msg);

View File

@ -18,19 +18,20 @@
#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
#include "llvm/DebugInfo/CodeView/DebugSubsectionVisitor.h"
#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
#include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
#include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
#include "llvm/DebugInfo/MSF/MSFBuilder.h"
#include "llvm/DebugInfo/MSF/MSFCommon.h"
#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
#include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h"
#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
@ -396,9 +397,54 @@ static void addObjectsToPDB(BumpPtrAllocator &Alloc, SymbolTable *Symtab,
addTypeInfo(Builder.getIpiBuilder(), IDTable);
}
static void addLinkerModuleSymbols(StringRef Path,
pdb::DbiModuleDescriptorBuilder &Mod,
BumpPtrAllocator &Allocator) {
codeview::SymbolSerializer Serializer(Allocator, CodeViewContainer::Pdb);
codeview::ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
codeview::Compile3Sym CS(SymbolRecordKind::Compile3Sym);
codeview::EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym);
ONS.Name = "* Linker *";
ONS.Signature = 0;
CS.Machine = Config->is64() ? CPUType::X64 : CPUType::Intel80386;
CS.Flags = CompileSym3Flags::None;
CS.VersionBackendBuild = 0;
CS.VersionBackendMajor = 0;
CS.VersionBackendMinor = 0;
CS.VersionBackendQFE = 0;
CS.VersionFrontendBuild = 0;
CS.VersionFrontendMajor = 0;
CS.VersionFrontendMinor = 0;
CS.VersionFrontendQFE = 0;
CS.Version = "LLVM Linker";
CS.setLanguage(SourceLanguage::Link);
ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front();
std::string ArgStr = llvm::join(Args, " ");
EBS.Fields.push_back("cwd");
SmallString<64> cwd;
llvm::sys::fs::current_path(cwd);
EBS.Fields.push_back(cwd);
EBS.Fields.push_back("exe");
std::string Exe =
llvm::sys::fs::getMainExecutable(Config->Argv[0].data(), nullptr);
EBS.Fields.push_back(Exe);
EBS.Fields.push_back("pdb");
EBS.Fields.push_back(Path);
EBS.Fields.push_back("cmd");
EBS.Fields.push_back(ArgStr);
Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
ONS, Allocator, CodeViewContainer::Pdb));
Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
CS, Allocator, CodeViewContainer::Pdb));
Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
EBS, Allocator, CodeViewContainer::Pdb));
}
// Creates a PDB file.
void coff::createPDB(StringRef Path, SymbolTable *Symtab,
ArrayRef<uint8_t> SectionTable,
void coff::createPDB(SymbolTable *Symtab, ArrayRef<uint8_t> SectionTable,
const llvm::codeview::DebugInfo *DI) {
BumpPtrAllocator Alloc;
pdb::PDBFileBuilder Builder(Alloc);
@ -413,7 +459,8 @@ void coff::createPDB(StringRef Path, SymbolTable *Symtab,
auto &InfoBuilder = Builder.getInfoBuilder();
InfoBuilder.setAge(DI ? DI->PDB70.Age : 0);
llvm::SmallString<128> NativePath(Path.begin(), Path.end());
llvm::SmallString<128> NativePath(Config->PDBPath.begin(),
Config->PDBPath.end());
llvm::sys::fs::make_absolute(NativePath);
llvm::sys::path::native(NativePath, llvm::sys::path::Style::windows);
@ -449,11 +496,12 @@ void coff::createPDB(StringRef Path, SymbolTable *Symtab,
auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
LinkerModule.setPdbFilePathNI(PdbFilePathNI);
addLinkerModuleSymbols(NativePath, LinkerModule, Alloc);
// Add COFF section header stream.
ExitOnErr(
DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
// Write to a file.
ExitOnErr(Builder.commit(Path));
ExitOnErr(Builder.commit(Config->PDBPath));
}

View File

@ -23,8 +23,7 @@ namespace lld {
namespace coff {
class SymbolTable;
void createPDB(llvm::StringRef Path, SymbolTable *Symtab,
llvm::ArrayRef<uint8_t> SectionTable,
void createPDB(SymbolTable *Symtab, llvm::ArrayRef<uint8_t> SectionTable,
const llvm::codeview::DebugInfo *DI);
}
}

View File

@ -239,7 +239,7 @@ void Writer::run() {
const llvm::codeview::DebugInfo *DI = nullptr;
if (Config->DebugTypes & static_cast<unsigned>(coff::DebugType::CV))
DI = BuildId->DI;
createPDB(Config->PDBPath, Symtab, SectionTable, DI);
createPDB(Symtab, SectionTable, DI);
}
writeMapFile(OutputSections);

View File

@ -97,6 +97,7 @@ struct Configuration {
llvm::StringRef ThinLTOCacheDir;
std::string Rpath;
std::vector<VersionDefinition> VersionDefinitions;
std::vector<llvm::StringRef> Argv;
std::vector<llvm::StringRef> AuxiliaryList;
std::vector<llvm::StringRef> SearchPaths;
std::vector<llvm::StringRef> SymbolOrderingFile;

View File

@ -74,13 +74,13 @@ bool elf::link(ArrayRef<const char *> Args, bool CanExitEarly,
raw_ostream &Error) {
ErrorCount = 0;
ErrorOS = &Error;
Argv0 = Args[0];
InputSections.clear();
Tar = nullptr;
Config = make<Configuration>();
Driver = make<LinkerDriver>();
Script = make<LinkerScript>();
Config->Argv = {Args.begin(), Args.end()};
Driver->main(Args, CanExitEarly);
freeArena();

View File

@ -27,7 +27,6 @@ using namespace lld::elf;
uint64_t elf::ErrorCount;
raw_ostream *elf::ErrorOS;
StringRef elf::Argv0;
// The functions defined in this file can be called from multiple threads,
// but outs() or errs() are not thread-safe. We protect them using a mutex.
@ -46,7 +45,7 @@ static void newline(const Twine &Msg) {
}
static void print(StringRef S, raw_ostream::Colors C) {
*ErrorOS << Argv0 + ": ";
*ErrorOS << Config->Argv[0] << ": ";
if (Config->ColorDiagnostics) {
ErrorOS->changeColor(C, true);
*ErrorOS << S;
@ -59,7 +58,7 @@ static void print(StringRef S, raw_ostream::Colors C) {
void elf::log(const Twine &Msg) {
if (Config->Verbose) {
std::lock_guard<std::mutex> Lock(Mu);
outs() << Argv0 << ": " << Msg << "\n";
outs() << Config->Argv[0] << ": " << Msg << "\n";
outs().flush();
}
}

View File

@ -37,7 +37,6 @@ namespace elf {
extern uint64_t ErrorCount;
extern llvm::raw_ostream *ErrorOS;
extern llvm::StringRef Argv0;
void log(const Twine &Msg);
void message(const Twine &Msg);

View File

@ -0,0 +1,18 @@
RUN: lld-link /debug /pdb:%t.pdb /nodefaultlib /entry:main %S/Inputs/pdb-diff.obj
RUN: llvm-pdbutil dump -modules -symbols %t.pdb | FileCheck %s
CHECK: Mod 0001 | `* Linker *`:
CHECK-NEXT: 4 | S_OBJNAME [size = 20] sig=0, `* Linker *`
CHECK-NEXT: 24 | S_COMPILE3 [size = 40]
CHECK-NEXT: machine = intel 80386, Ver = LLVM Linker, language = link
CHECK-NEXT: frontend = 0.0.0.0, backend = 0.0.0.0
CHECK-NEXT: flags = none
CHECK-NEXT: 64 | S_ENVBLOCK
CHECK-NEXT: - cwd
CHECK-NEXT: -
CHECK-NEXT: - exe
CHECK-NEXT: - {{.*}}lld-link
CHECK-NEXT: - pdb
CHECK-NEXT: - {{.*}}pdb-linker-module{{.*}}pdb
CHECK-NEXT: - cmd
CHECK-NEXT: - /debug /pdb:{{.*}}pdb-linker-module{{.*}}pdb /nodefaultlib /entry:main {{.*}}pdb-diff.obj

View File

@ -735,6 +735,10 @@ public:
uint16_t VersionBackendQFE;
StringRef Version;
void setLanguage(SourceLanguage Lang) {
Flags = CompileSym3Flags((uint32_t(Flags) & 0xFFFFFF00) | uint32_t(Lang));
}
uint8_t getLanguage() const { return static_cast<uint32_t>(Flags) & 0xFF; }
uint32_t getFlags() const { return static_cast<uint32_t>(Flags) & ~0xFF; }