Skip to content

Commit d1e2f89

Browse files
mstorsjodyung
authored andcommitted
[LLD] [COFF] Fix symbol names for import thunks (#160694)
9cc9efc changed LLD to use a StringTableBuilder for optimizing the string table, used for long section and symbol names. That commit had a bug, where the symbol table entry for an import thunk with a long symbol name wouldn't get fetched from the StringTableBuilder, if the base symbol name (without the "__imp_" prefix) wasn't over 8 chars. This should fix issues with Go, which errors out on reading the executables with a broken symbol table, as noted in mstorsjo/llvm-mingw#518 and golang/go#75219. (cherry picked from commit 0d6af2d)
1 parent 0060034 commit d1e2f89

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lld/COFF/Writer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,7 @@ void Writer::createSymbolAndStringTable() {
15531553
dthunk->wrappedSym->writtenToSymtab = true;
15541554
if (std::optional<coff_symbol16> sym =
15551555
createSymbol(dthunk->wrappedSym)) {
1556-
if (d->getName().size() > COFF::NameSize)
1556+
if (dthunk->wrappedSym->getName().size() > COFF::NameSize)
15571557
longNameSymbols.emplace_back(outputSymtab.size(),
15581558
dthunk->wrappedSym->getName());
15591559
outputSymtab.push_back(*sym);

lld/test/COFF/strtab.s

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
11
# REQUIRES: x86
22
# RUN: llvm-mc -triple=x86_64-windows-msvc %s -filetype=obj -o %t.obj
3-
# RUN: lld-link -out:%t.exe -entry:main %t.obj -debug:dwarf
3+
# RUN: lld-link -machine:x64 -def:%S/Inputs/library.def -implib:%t.lib
4+
# RUN: lld-link -out:%t.exe -entry:main %t.obj %t.lib -debug:dwarf
45
# RUN: llvm-readobj --string-table %t.exe | FileCheck %s
6+
# RUN: llvm-nm %t.exe | FileCheck %s --check-prefix=SYMBOLS
7+
8+
# Note, for this test to have the intended test coverage, the imported symbol
9+
# "function" needs to be such that the symbol name itself is <= 8 chars, while
10+
# "__imp_"+name is >8 chars.
511

612
# CHECK: StringTable {
7-
# CHECK-NEXT: Length: 87
13+
# CHECK-NEXT: Length: 102
814
# CHECK-NEXT: [ 4] .debug_abbrev
915
# CHECK-NEXT: [ 12] .debug_line
1016
# CHECK-NEXT: [ 1e] long_name_symbolz
1117
# CHECK-NEXT: [ 30] .debug_abbrez
12-
# CHECK-NEXT: [ 3e] __impl_long_name_symbolA
18+
# CHECK-NEXT: [ 3e] __imp_function
19+
# CHECK-NEXT: [ 4d] __impl_long_name_symbolA
1320
# CHECK-NEXT: }
1421

22+
# SYMBOLS: 140001000 N .debug_abbrez
23+
# SYMBOLS-NEXT: 140002070 R __imp_function
24+
# SYMBOLS-NEXT: 140001000 t __impl_long_name_symbolA
25+
# SYMBOLS-NEXT: 140001010 T function
26+
# SYMBOLS-NEXT: 140001000 t long_name_symbolA
27+
# SYMBOLS-NEXT: 140001000 t long_name_symbolz
28+
# SYMBOLS-NEXT: 140001000 T main
29+
# SYMBOLS-NEXT: 140001000 t name_symbolA
1530

1631
.global main
1732
.text
@@ -21,6 +36,7 @@ long_name_symbolA:
2136
__impl_long_name_symbolA:
2237
name_symbolA:
2338
.debug_abbrez:
39+
call function
2440
ret
2541

2642
.section .debug_abbrev,"dr"

0 commit comments

Comments
 (0)