Skip to content

Commit db5f528

Browse files
committed
WIP: [LLD] [COFF] Add verbose logging of symbol resolution
1 parent 9704ea0 commit db5f528

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

lld/COFF/Driver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ void LinkerDriver::addArchiveBuffer(MemoryBufferRef mb, StringRef symName,
301301
}
302302

303303
obj->parentName = parentName;
304+
log("Loading " + toString(obj) + " for " + symName);
304305
ctx.symtab.addFile(obj);
305306
log("Loaded " + toString(obj) + " for " + symName);
306307
}

lld/COFF/InputFiles.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ static void checkAndSetWeakAlias(COFFLinkerContext &ctx, InputFile *f,
9393
}
9494
}
9595
u->setWeakAlias(target, isAntiDep);
96+
log("Making " + source->getName() + " an alias for " + target->getName());
9697
}
9798
}
9899

@@ -132,6 +133,9 @@ void ArchiveFile::addMember(const Archive::Symbol &sym) {
132133
CHECK(sym.getMember(),
133134
"could not get the member for symbol " + toCOFFString(ctx, sym));
134135

136+
Expected<StringRef> name = c.getName();
137+
if (name)
138+
log("Loading " + toString(this) + "(" + *name + ") for " + sym.getName());
135139
// Return an empty buffer if we have already returned the same buffer.
136140
if (!seen.insert(c.getChildOffset()).second)
137141
return;

lld/COFF/SymbolTable.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,14 @@ Symbol *SymbolTable::addUndefined(StringRef name, InputFile *f,
641641
bool overrideLazy) {
642642
auto [s, wasInserted] = insert(name, f);
643643
if (wasInserted || (s->isLazy() && overrideLazy)) {
644+
log("Adding undefined " + name + " from " + toString(f));
644645
replaceSymbol<Undefined>(s, name);
645646
return s;
646647
}
647-
if (s->isLazy())
648+
if (s->isLazy()) {
649+
log("Loading lazy " + name + " due to undefined in " + toString(f));
648650
forceLazy(s);
651+
}
649652
return s;
650653
}
651654

@@ -695,6 +698,7 @@ void SymbolTable::addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym) {
695698
return;
696699
auto [s, wasInserted] = insert(name);
697700
if (wasInserted) {
701+
log("Adding lazy archive symbol " + name + " from " + toString(f));
698702
replaceSymbol<LazyArchive>(s, f, sym);
699703
return;
700704
}
@@ -703,6 +707,7 @@ void SymbolTable::addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym) {
703707
s->pendingArchiveLoad)
704708
return;
705709
s->pendingArchiveLoad = true;
710+
log("Immediately loading lazy " + name + " from " + toString(f) + " as it was undefined before");
706711
f->addMember(sym);
707712
}
708713

@@ -712,6 +717,7 @@ void SymbolTable::addLazyObject(InputFile *f, StringRef n) {
712717
return;
713718
auto [s, wasInserted] = insert(n, f);
714719
if (wasInserted) {
720+
log("Adding lazy object symbol " + n + " from " + toString(f));
715721
replaceSymbol<LazyObject>(s, f, n);
716722
return;
717723
}
@@ -808,9 +814,10 @@ void SymbolTable::reportDuplicate(Symbol *existing, InputFile *newFile,
808814
Symbol *SymbolTable::addAbsolute(StringRef n, COFFSymbolRef sym) {
809815
auto [s, wasInserted] = insert(n, nullptr);
810816
s->isUsedInRegularObj = true;
811-
if (wasInserted || isa<Undefined>(s) || s->isLazy())
817+
if (wasInserted || isa<Undefined>(s) || s->isLazy()) {
818+
log("Added absolute " + n);
812819
replaceSymbol<DefinedAbsolute>(s, ctx, n, sym);
813-
else if (auto *da = dyn_cast<DefinedAbsolute>(s)) {
820+
} else if (auto *da = dyn_cast<DefinedAbsolute>(s)) {
814821
if (da->getVA() != sym.getValue())
815822
reportDuplicate(s, nullptr);
816823
} else if (!isa<DefinedCOFF>(s))
@@ -821,9 +828,10 @@ Symbol *SymbolTable::addAbsolute(StringRef n, COFFSymbolRef sym) {
821828
Symbol *SymbolTable::addAbsolute(StringRef n, uint64_t va) {
822829
auto [s, wasInserted] = insert(n, nullptr);
823830
s->isUsedInRegularObj = true;
824-
if (wasInserted || isa<Undefined>(s) || s->isLazy())
831+
if (wasInserted || isa<Undefined>(s) || s->isLazy()) {
832+
log("Added absolute " + n);
825833
replaceSymbol<DefinedAbsolute>(s, ctx, n, va);
826-
else if (auto *da = dyn_cast<DefinedAbsolute>(s)) {
834+
} else if (auto *da = dyn_cast<DefinedAbsolute>(s)) {
827835
if (da->getVA() != va)
828836
reportDuplicate(s, nullptr);
829837
} else if (!isa<DefinedCOFF>(s))
@@ -834,9 +842,10 @@ Symbol *SymbolTable::addAbsolute(StringRef n, uint64_t va) {
834842
Symbol *SymbolTable::addSynthetic(StringRef n, Chunk *c) {
835843
auto [s, wasInserted] = insert(n, nullptr);
836844
s->isUsedInRegularObj = true;
837-
if (wasInserted || isa<Undefined>(s) || s->isLazy())
845+
if (wasInserted || isa<Undefined>(s) || s->isLazy()) {
846+
log("Added synthetic " + n);
838847
replaceSymbol<DefinedSynthetic>(s, n, c);
839-
else if (!isa<DefinedCOFF>(s))
848+
} else if (!isa<DefinedCOFF>(s))
840849
reportDuplicate(s, nullptr);
841850
return s;
842851
}
@@ -845,10 +854,11 @@ Symbol *SymbolTable::addRegular(InputFile *f, StringRef n,
845854
const coff_symbol_generic *sym, SectionChunk *c,
846855
uint32_t sectionOffset, bool isWeak) {
847856
auto [s, wasInserted] = insert(n, f);
848-
if (wasInserted || !isa<DefinedRegular>(s) || s->isWeak)
857+
if (wasInserted || !isa<DefinedRegular>(s) || s->isWeak) {
858+
log("Added regular " + n + " from " + toString(f));
849859
replaceSymbol<DefinedRegular>(s, f, n, /*IsCOMDAT*/ false,
850860
/*IsExternal*/ true, sym, c, isWeak);
851-
else if (!isWeak)
861+
} else if (!isWeak)
852862
reportDuplicate(s, f, c, sectionOffset);
853863
return s;
854864
}
@@ -858,6 +868,7 @@ SymbolTable::addComdat(InputFile *f, StringRef n,
858868
const coff_symbol_generic *sym) {
859869
auto [s, wasInserted] = insert(n, f);
860870
if (wasInserted || !isa<DefinedRegular>(s)) {
871+
log("Added comdat " + n + " from " + toString(f));
861872
replaceSymbol<DefinedRegular>(s, f, n, /*IsCOMDAT*/ true,
862873
/*IsExternal*/ true, sym, nullptr);
863874
return {cast<DefinedRegular>(s), true};
@@ -871,9 +882,10 @@ SymbolTable::addComdat(InputFile *f, StringRef n,
871882
Symbol *SymbolTable::addCommon(InputFile *f, StringRef n, uint64_t size,
872883
const coff_symbol_generic *sym, CommonChunk *c) {
873884
auto [s, wasInserted] = insert(n, f);
874-
if (wasInserted || !isa<DefinedCOFF>(s))
885+
if (wasInserted || !isa<DefinedCOFF>(s)) {
886+
log("Added common " + n + " from " + toString(f));
875887
replaceSymbol<DefinedCommon>(s, f, n, size, sym, c);
876-
else if (auto *dc = dyn_cast<DefinedCommon>(s))
888+
} else if (auto *dc = dyn_cast<DefinedCommon>(s))
877889
if (size > dc->getSize())
878890
replaceSymbol<DefinedCommon>(s, f, n, size, sym, c);
879891
return s;
@@ -884,6 +896,7 @@ DefinedImportData *SymbolTable::addImportData(StringRef n, ImportFile *f,
884896
auto [s, wasInserted] = insert(n, nullptr);
885897
s->isUsedInRegularObj = true;
886898
if (wasInserted || isa<Undefined>(s) || s->isLazy()) {
899+
log("Added import data " + n + " from " + toString(f));
887900
replaceSymbol<DefinedImportData>(s, n, f, location);
888901
return cast<DefinedImportData>(s);
889902
}
@@ -897,6 +910,7 @@ Defined *SymbolTable::addImportThunk(StringRef name, DefinedImportData *id,
897910
auto [s, wasInserted] = insert(name, nullptr);
898911
s->isUsedInRegularObj = true;
899912
if (wasInserted || isa<Undefined>(s) || s->isLazy()) {
913+
log("Added import thunk " + name + " from " + toString(id->file));
900914
replaceSymbol<DefinedImportThunk>(s, ctx, name, id, chunk);
901915
return cast<Defined>(s);
902916
}

lld/COFF/Symbols.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ bool Undefined::resolveWeakAlias() {
138138
// Symbols. For that reason we need to check which type of symbol we
139139
// are dealing with and copy the correct number of bytes.
140140
StringRef name = getName();
141+
log("Replacing " + name + " with alias " + d->getName());
141142
bool wasAntiDep = isAntiDep;
142143
if (isa<DefinedRegular>(d))
143144
memcpy(this, d, sizeof(DefinedRegular));

0 commit comments

Comments
 (0)