Skip to content

Commit adbbd5b

Browse files
kikairoyajeremyd2019
authored andcommitted
[LLD][COFF] Prevent to emit relocations for discarded weak wrapped symbols (llvm#156214)
When a symbol is imported from a DLL, a base relocation record is normally emitted. However, if the import is pulled in via a wrapped symbol (using `--wrap`) and later dropped because it is only referenced weakly, a dangling base relocation remains in the output. At runtime, this relocation changes the symbol value from null to a garbage pointer. This patch adds checks to avoid emitting relocation records for non-`Defined` symbols, and to prevent creating an auto-import entry if an import entry for the wrapped symbol already exists. Fixes llvm#150739 (cherry picked from commit e976622)
1 parent 60838ec commit adbbd5b

File tree

4 files changed

+88
-13
lines changed

4 files changed

+88
-13
lines changed

lld/COFF/Chunks.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ void SectionChunk::getBaserels(std::vector<Baserel> *res) {
570570
if (ty == IMAGE_REL_BASED_ABSOLUTE)
571571
continue;
572572
Symbol *target = file->getSymbol(rel.SymbolTableIndex);
573-
if (!target || isa<DefinedAbsolute>(target))
573+
if (!isa_and_nonnull<Defined>(target) || isa<DefinedAbsolute>(target))
574574
continue;
575575
res->emplace_back(rva + rel.VirtualAddress, ty);
576576
}

lld/COFF/MinGW.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,15 @@ void lld::coff::wrapSymbols(SymbolTable &symtab) {
269269
// (We can't easily distinguish whether any object file actually
270270
// referenced it or not, though.)
271271
if (imp) {
272-
DefinedLocalImport *wrapimp = make<DefinedLocalImport>(
273-
symtab.ctx, saver().save("__imp_" + w.wrap->getName()), d);
274-
symtab.localImportChunks.push_back(wrapimp->getChunk());
275-
map[imp] = wrapimp;
272+
if (Symbol *wrapimp =
273+
symtab.find(("__imp_" + w.wrap->getName()).str())) {
274+
map[imp] = wrapimp;
275+
} else {
276+
DefinedLocalImport *localwrapimp = make<DefinedLocalImport>(
277+
symtab.ctx, saver().save("__imp_" + w.wrap->getName()), d);
278+
symtab.localImportChunks.push_back(localwrapimp->getChunk());
279+
map[imp] = localwrapimp;
280+
}
276281
}
277282
}
278283
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// REQUIRES: x86
2+
3+
// Check that base-relocations for unresolved weak symbols will be omitted.
4+
5+
// RUN: rm -rf %t.dir && split-file %s %t.dir && cd %t.dir
6+
// RUN: llvm-mc -filetype=obj -triple=x86_64-mingw main.s -o main.obj
7+
// RUN: llvm-mc -filetype=obj -triple=x86_64-mingw other.s -o other.obj
8+
9+
// RUN: lld-link -lldmingw -machine:x64 -dll -out:other.dll other.obj -noentry -export:foo -implib:other.lib
10+
// RUN: lld-link -lldmingw -machine:x64 -out:main.exe main.obj other.lib -entry:entry -wrap:foo -debug:symtab
11+
// RUN: llvm-readobj --sections --symbols --coff-imports --coff-basereloc main.exe | FileCheck %s --implicit-check-not=other.dll
12+
13+
// CHECK: Number: 3
14+
// CHECK-NEXT: Name: .data
15+
// CHECK-NEXT: VirtualSize:
16+
// CHECK-NEXT: VirtualAddress: 0x[[#%x,SECTOP:0x3000]]
17+
// CHECK: Name: ref_foo
18+
// CHECK-NEXT: Value: [[#%d,SYMVAL:]]
19+
// CHECK: BaseReloc [
20+
// CHECK-NOT: Address: 0x[[#%x,SECTOP+SYMVAL]]
21+
22+
#--- main.s
23+
.global entry
24+
entry:
25+
movq ref_foo(%rip), %rax
26+
call *%rax
27+
28+
.global __wrap_foo
29+
__wrap_foo:
30+
ret
31+
32+
.data
33+
.global ref_foo
34+
.p2align 3
35+
ref_foo:
36+
.quad __real_foo
37+
38+
.globl _pei386_runtime_relocator
39+
_pei386_runtime_relocator:
40+
movl __RUNTIME_PSEUDO_RELOC_LIST__(%rip), %eax
41+
movl __RUNTIME_PSEUDO_RELOC_LIST_END__(%rip), %eax
42+
43+
.weak __real_foo
44+
.addrsig
45+
.addrsig_sym __real_foo
46+
.addrsig_sym ref_foo
47+
48+
#--- other.s
49+
.global foo
50+
51+
foo:
52+
ret

lld/test/COFF/wrap-dllimport.s

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,60 @@
11
// REQUIRES: x86
22

33
// Check that we can wrap a dllimported symbol, so that references to
4-
// __imp_<symbol> gets redirected to a defined local import instead.
4+
// __imp_<symbol> gets redirected to a symbol that already exists or a defined
5+
// local import instead.
56

67
// RUN: split-file %s %t.dir
78
// RUN: llvm-mc -filetype=obj -triple=i686-win32-gnu %t.dir/main.s -o %t.main.obj
89
// RUN: llvm-mc -filetype=obj -triple=i686-win32-gnu %t.dir/other.s -o %t.other.obj
910

10-
// RUN: lld-link -dll -out:%t.dll %t.other.obj -noentry -safeseh:no -export:foo -implib:%t.lib
11-
// RUN: lld-link -out:%t.exe %t.main.obj %t.lib -entry:entry -subsystem:console -debug:symtab -safeseh:no -wrap:foo -lldmap:%t.map
11+
// RUN: lld-link -dll -out:%t.dll %t.other.obj -noentry -safeseh:no -export:foo -export:bar -implib:%t.lib
12+
// RUN: lld-link -out:%t.exe %t.main.obj %t.lib -entry:entry -subsystem:console -debug:symtab -safeseh:no -wrap:foo -wrap:bar -lldmap:%t.map
1213
// RUN: llvm-objdump -s -d --print-imm-hex %t.exe | FileCheck %s
1314

1415
// CHECK: Contents of section .rdata:
15-
// CHECK-NEXT: 402000 06104000
16+
// CHECK-NEXT: 402000 0c104000
1617

1718
// CHECK: Disassembly of section .text:
1819
// CHECK-EMPTY:
1920
// CHECK: 00401000 <_entry>:
2021
// CHECK-NEXT: 401000: ff 25 00 20 40 00 jmpl *0x402000
22+
// CHECK-NEXT: 401006: ff 25 00 00 00 00 jmpl *0x0
2123
// CHECK-EMPTY:
22-
// CHECK-NEXT: 00401006 <___wrap_foo>:
23-
// CHECK-NEXT: 401006: c3 retl
24+
// CHECK-NEXT: 0040100c <___wrap_foo>:
25+
// CHECK-NEXT: 40100c: c3 retl
26+
// CHECK-EMPTY:
27+
// CHECK-NEXT: 0040100d <___wrap_bar>:
28+
// CHECK-NEXT: 40100d: c3 retl
2429

25-
// The jmpl instruction in _entry points at an address in 0x402000,
30+
// The first jmpl instruction in _entry points at an address in 0x402000,
2631
// which is the first 4 bytes of the .rdata section (above), which is a
2732
// pointer that points at ___wrap_foo.
2833

34+
// The second jmpl instruction in _entry points to null because the referenced
35+
// symbol `__imp____wrap_bar` is declared as a weak reference to prevent pull a
36+
// reference from an external DLL.
37+
2938
#--- main.s
3039
.global _entry
3140
_entry:
3241
jmpl *__imp__foo
42+
jmpl *__imp__bar
3343

3444
.global ___wrap_foo
3545
___wrap_foo:
3646
ret
3747

48+
.weak __imp____wrap_bar
49+
.global ___wrap_bar
50+
___wrap_bar:
51+
ret
52+
3853
#--- other.s
3954
.global _foo
40-
4155
_foo:
4256
ret
57+
58+
.global _bar
59+
_bar:
60+
ret

0 commit comments

Comments
 (0)