Skip to content

Commit 6708e0b

Browse files
author
Zachary Turner
committedJul 10, 2017
[lld/pdb] Add some basic linker module symbols.
Differential Revision: https://reviews.llvm.org/D35152 llvm-svn: 307590
1 parent b387367 commit 6708e0b

File tree

13 files changed

+85
-18
lines changed

13 files changed

+85
-18
lines changed
 

‎lld/COFF/Config.h

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ struct Configuration {
9292
bool WriteSymtab = true;
9393
unsigned DebugTypes = static_cast<unsigned>(DebugType::None);
9494
llvm::SmallString<128> PDBPath;
95+
std::vector<llvm::StringRef> Argv;
9596

9697
// Symbols in this set are considered as live by the garbage collector.
9798
std::set<SymbolBody *> GCRoot;

‎lld/COFF/Driver.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ std::vector<SpecificAllocBase *> SpecificAllocBase::Instances;
5555
bool link(ArrayRef<const char *> Args, raw_ostream &Diag) {
5656
ErrorCount = 0;
5757
ErrorOS = &Diag;
58-
Argv0 = Args[0];
5958
Config = make<Configuration>();
59+
Config->Argv = {Args.begin(), Args.end()};
6060
Config->ColorDiagnostics =
6161
(ErrorOS == &llvm::errs() && Process::StandardErrHasColors());
6262
Driver = make<LinkerDriver>();

‎lld/COFF/Error.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ namespace lld {
2929
static std::mutex Mu;
3030

3131
namespace coff {
32-
StringRef Argv0;
3332
uint64_t ErrorCount;
3433
raw_ostream *ErrorOS;
3534

@@ -45,7 +44,7 @@ static LLVM_ATTRIBUTE_NORETURN void exitLld(int Val) {
4544
}
4645

4746
static void print(StringRef S, raw_ostream::Colors C) {
48-
*ErrorOS << Argv0 + ": ";
47+
*ErrorOS << Config->Argv[0] << ": ";
4948
if (Config->ColorDiagnostics) {
5049
ErrorOS->changeColor(C, true);
5150
*ErrorOS << S;
@@ -58,7 +57,7 @@ static void print(StringRef S, raw_ostream::Colors C) {
5857
void log(const Twine &Msg) {
5958
if (Config->Verbose) {
6059
std::lock_guard<std::mutex> Lock(Mu);
61-
outs() << Argv0 << ": " << Msg << "\n";
60+
outs() << Config->Argv[0] << ": " << Msg << "\n";
6261
outs().flush();
6362
}
6463
}

‎lld/COFF/Error.h

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace coff {
1818

1919
extern uint64_t ErrorCount;
2020
extern llvm::raw_ostream *ErrorOS;
21-
extern llvm::StringRef Argv0;
2221

2322
void log(const Twine &Msg);
2423
void message(const Twine &Msg);

‎lld/COFF/PDB.cpp

+53-5
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@
1818
#include "llvm/DebugInfo/CodeView/DebugSubsectionRecord.h"
1919
#include "llvm/DebugInfo/CodeView/DebugSubsectionVisitor.h"
2020
#include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h"
21+
#include "llvm/DebugInfo/CodeView/SymbolSerializer.h"
2122
#include "llvm/DebugInfo/CodeView/TypeDumpVisitor.h"
2223
#include "llvm/DebugInfo/CodeView/TypeIndexDiscovery.h"
2324
#include "llvm/DebugInfo/CodeView/TypeStreamMerger.h"
2425
#include "llvm/DebugInfo/CodeView/TypeTableBuilder.h"
2526
#include "llvm/DebugInfo/MSF/MSFBuilder.h"
2627
#include "llvm/DebugInfo/MSF/MSFCommon.h"
28+
#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
2729
#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
2830
#include "llvm/DebugInfo/PDB/Native/DbiStreamBuilder.h"
2931
#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
3032
#include "llvm/DebugInfo/PDB/Native/InfoStreamBuilder.h"
3133
#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
3234
#include "llvm/DebugInfo/PDB/Native/PDBFileBuilder.h"
33-
#include "llvm/DebugInfo/PDB/Native/DbiModuleDescriptorBuilder.h"
3435
#include "llvm/DebugInfo/PDB/Native/PDBStringTableBuilder.h"
3536
#include "llvm/DebugInfo/PDB/Native/PDBTypeServerHandler.h"
3637
#include "llvm/DebugInfo/PDB/Native/TpiStream.h"
@@ -396,9 +397,54 @@ static void addObjectsToPDB(BumpPtrAllocator &Alloc, SymbolTable *Symtab,
396397
addTypeInfo(Builder.getIpiBuilder(), IDTable);
397398
}
398399

400+
static void addLinkerModuleSymbols(StringRef Path,
401+
pdb::DbiModuleDescriptorBuilder &Mod,
402+
BumpPtrAllocator &Allocator) {
403+
codeview::SymbolSerializer Serializer(Allocator, CodeViewContainer::Pdb);
404+
codeview::ObjNameSym ONS(SymbolRecordKind::ObjNameSym);
405+
codeview::Compile3Sym CS(SymbolRecordKind::Compile3Sym);
406+
codeview::EnvBlockSym EBS(SymbolRecordKind::EnvBlockSym);
407+
408+
ONS.Name = "* Linker *";
409+
ONS.Signature = 0;
410+
411+
CS.Machine = Config->is64() ? CPUType::X64 : CPUType::Intel80386;
412+
CS.Flags = CompileSym3Flags::None;
413+
CS.VersionBackendBuild = 0;
414+
CS.VersionBackendMajor = 0;
415+
CS.VersionBackendMinor = 0;
416+
CS.VersionBackendQFE = 0;
417+
CS.VersionFrontendBuild = 0;
418+
CS.VersionFrontendMajor = 0;
419+
CS.VersionFrontendMinor = 0;
420+
CS.VersionFrontendQFE = 0;
421+
CS.Version = "LLVM Linker";
422+
CS.setLanguage(SourceLanguage::Link);
423+
424+
ArrayRef<StringRef> Args = makeArrayRef(Config->Argv).drop_front();
425+
std::string ArgStr = llvm::join(Args, " ");
426+
EBS.Fields.push_back("cwd");
427+
SmallString<64> cwd;
428+
llvm::sys::fs::current_path(cwd);
429+
EBS.Fields.push_back(cwd);
430+
EBS.Fields.push_back("exe");
431+
std::string Exe =
432+
llvm::sys::fs::getMainExecutable(Config->Argv[0].data(), nullptr);
433+
EBS.Fields.push_back(Exe);
434+
EBS.Fields.push_back("pdb");
435+
EBS.Fields.push_back(Path);
436+
EBS.Fields.push_back("cmd");
437+
EBS.Fields.push_back(ArgStr);
438+
Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
439+
ONS, Allocator, CodeViewContainer::Pdb));
440+
Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
441+
CS, Allocator, CodeViewContainer::Pdb));
442+
Mod.addSymbol(codeview::SymbolSerializer::writeOneSymbol(
443+
EBS, Allocator, CodeViewContainer::Pdb));
444+
}
445+
399446
// Creates a PDB file.
400-
void coff::createPDB(StringRef Path, SymbolTable *Symtab,
401-
ArrayRef<uint8_t> SectionTable,
447+
void coff::createPDB(SymbolTable *Symtab, ArrayRef<uint8_t> SectionTable,
402448
const llvm::codeview::DebugInfo *DI) {
403449
BumpPtrAllocator Alloc;
404450
pdb::PDBFileBuilder Builder(Alloc);
@@ -413,7 +459,8 @@ void coff::createPDB(StringRef Path, SymbolTable *Symtab,
413459
auto &InfoBuilder = Builder.getInfoBuilder();
414460
InfoBuilder.setAge(DI ? DI->PDB70.Age : 0);
415461

416-
llvm::SmallString<128> NativePath(Path.begin(), Path.end());
462+
llvm::SmallString<128> NativePath(Config->PDBPath.begin(),
463+
Config->PDBPath.end());
417464
llvm::sys::fs::make_absolute(NativePath);
418465
llvm::sys::path::native(NativePath, llvm::sys::path::Style::windows);
419466

@@ -449,11 +496,12 @@ void coff::createPDB(StringRef Path, SymbolTable *Symtab,
449496

450497
auto &LinkerModule = ExitOnErr(DbiBuilder.addModuleInfo("* Linker *"));
451498
LinkerModule.setPdbFilePathNI(PdbFilePathNI);
499+
addLinkerModuleSymbols(NativePath, LinkerModule, Alloc);
452500

453501
// Add COFF section header stream.
454502
ExitOnErr(
455503
DbiBuilder.addDbgStream(pdb::DbgHeaderType::SectionHdr, SectionTable));
456504

457505
// Write to a file.
458-
ExitOnErr(Builder.commit(Path));
506+
ExitOnErr(Builder.commit(Config->PDBPath));
459507
}

‎lld/COFF/PDB.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ namespace lld {
2323
namespace coff {
2424
class SymbolTable;
2525

26-
void createPDB(llvm::StringRef Path, SymbolTable *Symtab,
27-
llvm::ArrayRef<uint8_t> SectionTable,
26+
void createPDB(SymbolTable *Symtab, llvm::ArrayRef<uint8_t> SectionTable,
2827
const llvm::codeview::DebugInfo *DI);
2928
}
3029
}

‎lld/COFF/Writer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ void Writer::run() {
239239
const llvm::codeview::DebugInfo *DI = nullptr;
240240
if (Config->DebugTypes & static_cast<unsigned>(coff::DebugType::CV))
241241
DI = BuildId->DI;
242-
createPDB(Config->PDBPath, Symtab, SectionTable, DI);
242+
createPDB(Symtab, SectionTable, DI);
243243
}
244244

245245
writeMapFile(OutputSections);

‎lld/ELF/Config.h

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ struct Configuration {
9797
llvm::StringRef ThinLTOCacheDir;
9898
std::string Rpath;
9999
std::vector<VersionDefinition> VersionDefinitions;
100+
std::vector<llvm::StringRef> Argv;
100101
std::vector<llvm::StringRef> AuxiliaryList;
101102
std::vector<llvm::StringRef> SearchPaths;
102103
std::vector<llvm::StringRef> SymbolOrderingFile;

‎lld/ELF/Driver.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ bool elf::link(ArrayRef<const char *> Args, bool CanExitEarly,
7474
raw_ostream &Error) {
7575
ErrorCount = 0;
7676
ErrorOS = &Error;
77-
Argv0 = Args[0];
7877
InputSections.clear();
7978
Tar = nullptr;
8079

8180
Config = make<Configuration>();
8281
Driver = make<LinkerDriver>();
8382
Script = make<LinkerScript>();
83+
Config->Argv = {Args.begin(), Args.end()};
8484

8585
Driver->main(Args, CanExitEarly);
8686
freeArena();

‎lld/ELF/Error.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ using namespace lld::elf;
2727

2828
uint64_t elf::ErrorCount;
2929
raw_ostream *elf::ErrorOS;
30-
StringRef elf::Argv0;
3130

3231
// The functions defined in this file can be called from multiple threads,
3332
// but outs() or errs() are not thread-safe. We protect them using a mutex.
@@ -46,7 +45,7 @@ static void newline(const Twine &Msg) {
4645
}
4746

4847
static void print(StringRef S, raw_ostream::Colors C) {
49-
*ErrorOS << Argv0 + ": ";
48+
*ErrorOS << Config->Argv[0] << ": ";
5049
if (Config->ColorDiagnostics) {
5150
ErrorOS->changeColor(C, true);
5251
*ErrorOS << S;
@@ -59,7 +58,7 @@ static void print(StringRef S, raw_ostream::Colors C) {
5958
void elf::log(const Twine &Msg) {
6059
if (Config->Verbose) {
6160
std::lock_guard<std::mutex> Lock(Mu);
62-
outs() << Argv0 << ": " << Msg << "\n";
61+
outs() << Config->Argv[0] << ": " << Msg << "\n";
6362
outs().flush();
6463
}
6564
}

‎lld/ELF/Error.h

-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ namespace elf {
3737

3838
extern uint64_t ErrorCount;
3939
extern llvm::raw_ostream *ErrorOS;
40-
extern llvm::StringRef Argv0;
4140

4241
void log(const Twine &Msg);
4342
void message(const Twine &Msg);

‎lld/test/COFF/pdb-linker-module.test

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
RUN: lld-link /debug /pdb:%t.pdb /nodefaultlib /entry:main %S/Inputs/pdb-diff.obj
2+
RUN: llvm-pdbutil dump -modules -symbols %t.pdb | FileCheck %s
3+
4+
CHECK: Mod 0001 | `* Linker *`:
5+
CHECK-NEXT: 4 | S_OBJNAME [size = 20] sig=0, `* Linker *`
6+
CHECK-NEXT: 24 | S_COMPILE3 [size = 40]
7+
CHECK-NEXT: machine = intel 80386, Ver = LLVM Linker, language = link
8+
CHECK-NEXT: frontend = 0.0.0.0, backend = 0.0.0.0
9+
CHECK-NEXT: flags = none
10+
CHECK-NEXT: 64 | S_ENVBLOCK
11+
CHECK-NEXT: - cwd
12+
CHECK-NEXT: -
13+
CHECK-NEXT: - exe
14+
CHECK-NEXT: - {{.*}}lld-link
15+
CHECK-NEXT: - pdb
16+
CHECK-NEXT: - {{.*}}pdb-linker-module{{.*}}pdb
17+
CHECK-NEXT: - cmd
18+
CHECK-NEXT: - /debug /pdb:{{.*}}pdb-linker-module{{.*}}pdb /nodefaultlib /entry:main {{.*}}pdb-diff.obj

‎llvm/include/llvm/DebugInfo/CodeView/SymbolRecord.h

+4
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,10 @@ class Compile3Sym : public SymbolRecord {
735735
uint16_t VersionBackendQFE;
736736
StringRef Version;
737737

738+
void setLanguage(SourceLanguage Lang) {
739+
Flags = CompileSym3Flags((uint32_t(Flags) & 0xFFFFFF00) | uint32_t(Lang));
740+
}
741+
738742
uint8_t getLanguage() const { return static_cast<uint32_t>(Flags) & 0xFF; }
739743
uint32_t getFlags() const { return static_cast<uint32_t>(Flags) & ~0xFF; }
740744

0 commit comments

Comments
 (0)
Please sign in to comment.