Skip to content

Commit 9509f4e

Browse files
alvinhochunmstorsjo
authored andcommitted
[LLD][COFF] Improve symbol table info for import thunk
Import thunks themselves contain a jump or branch, which is code by nature. Therefore the import thunk symbol should be marked as function type in the symbol table to help with debugging. The `__imp_` import symbol associated to the import thunk is also useful for debugging. However, when the import symbol isn't directly referenced outside of the import thunk, it doesn't normally get added to the symbol table. This change teaches LLD to add the import symbol explicitly. Reviewed By: mstorsjo Differential Revision: https://reviews.llvm.org/D134169
1 parent 9a2b697 commit 9509f4e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lld/COFF/Writer.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,10 @@ Optional<coff_symbol16> Writer::createSymbol(Defined *def) {
11791179
COFFSymbolRef ref = d->getCOFFSymbol();
11801180
sym.Type = ref.getType();
11811181
sym.StorageClass = ref.getStorageClass();
1182+
} else if (def->kind() == Symbol::DefinedImportThunkKind) {
1183+
sym.Type = (IMAGE_SYM_DTYPE_FUNCTION << SCT_COMPLEX_TYPE_SHIFT) |
1184+
IMAGE_SYM_TYPE_NULL;
1185+
sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
11821186
} else {
11831187
sym.Type = IMAGE_SYM_TYPE_NULL;
11841188
sym.StorageClass = IMAGE_SYM_CLASS_EXTERNAL;
@@ -1225,6 +1229,14 @@ void Writer::createSymbolAndStringTable() {
12251229

12261230
if (Optional<coff_symbol16> sym = createSymbol(d))
12271231
outputSymtab.push_back(*sym);
1232+
1233+
if (auto *dthunk = dyn_cast<DefinedImportThunk>(d)) {
1234+
if (!dthunk->wrappedSym->writtenToSymtab) {
1235+
dthunk->wrappedSym->writtenToSymtab = true;
1236+
if (Optional<coff_symbol16> sym = createSymbol(dthunk->wrappedSym))
1237+
outputSymtab.push_back(*sym);
1238+
}
1239+
}
12281240
}
12291241
}
12301242
}

lld/test/COFF/symtab.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
# CHECK-NEXT: Value: 80
1616
# CHECK-NEXT: Section: .text (1)
1717
# CHECK-NEXT: BaseType: Null (0x0)
18+
# CHECK-NEXT: ComplexType: Function (0x2)
19+
# CHECK-NEXT: StorageClass: External (0x2)
20+
# CHECK-NEXT: AuxSymbolCount: 0
21+
# CHECK-NEXT: }
22+
# CHECK-NEXT: Symbol {
23+
# CHECK-NEXT: Name: __imp_MessageBoxA
24+
# CHECK-NEXT: Value:
25+
# CHECK-NEXT: Section: .rdata (2)
26+
# CHECK-NEXT: BaseType: Null (0x0)
1827
# CHECK-NEXT: ComplexType: Null (0x0)
1928
# CHECK-NEXT: StorageClass: External (0x2)
2029
# CHECK-NEXT: AuxSymbolCount: 0
@@ -24,6 +33,15 @@
2433
# CHECK-NEXT: Value: 64
2534
# CHECK-NEXT: Section: .text (1)
2635
# CHECK-NEXT: BaseType: Null (0x0)
36+
# CHECK-NEXT: ComplexType: Function (0x2)
37+
# CHECK-NEXT: StorageClass: External (0x2)
38+
# CHECK-NEXT: AuxSymbolCount: 0
39+
# CHECK-NEXT: }
40+
# CHECK-NEXT: Symbol {
41+
# CHECK-NEXT: Name: __imp_ExitProcess
42+
# CHECK-NEXT: Value:
43+
# CHECK-NEXT: Section: .rdata (2)
44+
# CHECK-NEXT: BaseType: Null (0x0)
2745
# CHECK-NEXT: ComplexType: Null (0x0)
2846
# CHECK-NEXT: StorageClass: External (0x2)
2947
# CHECK-NEXT: AuxSymbolCount: 0

0 commit comments

Comments
 (0)