Skip to content

Commit d2eb34d

Browse files
committed
[LLD] [COFF] Warn if the runtime pseudo relocation function is missing
When then linker creates runtime pseudo relocations, it places them in a list with the assumption that the runtime will fix these relocations later, when the image gets loaded. If the relevant runtime function doesn't seem to be present in the linked image, print a warning. Normally when linking the mingw-w64 runtime libraries, this function always is available. However, if linking without including the mingw-w64 CRT startup files, and the image needs runtime pseudo relocations, try to make the user aware of the situation. This just prints a warning to alert the user about this situation. Alternatively, we could also make this situation a hard error. With ld.bfd, this situation is a hard error; ld.bfd adds an undefined reference to this symbol if runtime pseudo relocations are needed. Yet another alternative would be to actually try to pull in the symbol (if seen in a static library, but not included yet). This would allow decoupling the function from the main CRT startup code (making it optional, only running if the linker actually produced runtime pseudo relocations). Doing that would require restructuring the code (gathering pseudo relocations earlier, in order to be able to continue linking in more object files if the initial set did require pseudo relocations) though. Also, ld.bfd doesn't currently successfully pull in more object files to satisfy the dependency on _pei386_runtime_relocator, so with that in mind, there's not much extra value in making LLD do it currently either. This fixes one issue brought up in #84424.
1 parent 21265f6 commit d2eb34d

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

lld/COFF/Writer.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2072,8 +2072,16 @@ void Writer::createRuntimePseudoRelocs() {
20722072
return;
20732073
}
20742074

2075-
if (!rels.empty())
2075+
if (!rels.empty()) {
20762076
log("Writing " + Twine(rels.size()) + " runtime pseudo relocations");
2077+
const char *symbolName = "_pei386_runtime_relocator";
2078+
Symbol *relocator = ctx.symtab.findUnderscore(symbolName);
2079+
if (!relocator)
2080+
warn("output image has pseudo relocations, but function " +
2081+
Twine(symbolName) +
2082+
" missing; the relocations might not get fixed at runtime");
2083+
}
2084+
20772085
PseudoRelocTableChunk *table = make<PseudoRelocTableChunk>(rels);
20782086
rdataSec->addChunk(table);
20792087
EmptyChunk *endOfList = make<EmptyChunk>();

lld/test/COFF/autoimport-warn-func.s

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# REQUIRES: x86
2+
# RUN: split-file %s %t.dir
3+
4+
# RUN: llvm-dlltool -m i386:x86-64 -d %t.dir/lib.def -D lib.dll -l %t.dir/lib.lib
5+
6+
# RUN: llvm-mc -triple=x86_64-windows-gnu %t.dir/main.s -filetype=obj -o %t.dir/main.obj
7+
# RUN: llvm-mc -triple=x86_64-windows-gnu %t.dir/func.s -filetype=obj -o %t.dir/func.obj
8+
# RUN: lld-link -lldmingw -out:%t.dir/main.exe -entry:main %t.dir/main.obj %t.dir/lib.lib 2>&1 | FileCheck %s --check-prefix=WARN
9+
10+
# RUN: lld-link -lldmingw -out:%t.dir/main.exe -entry:main %t.dir/main.obj %t.dir/func.obj %t.dir/lib.lib 2>&1 | FileCheck %s --check-prefix=NOWARN --allow-empty
11+
12+
# WARN: warning: output image has pseudo relocations, but function _pei386_runtime_relocator missing; the relocations might not get fixed at runtime
13+
14+
# NOWARN-NOT: warning
15+
16+
#--- main.s
17+
.global main
18+
.text
19+
main:
20+
ret
21+
22+
.data
23+
.long 1
24+
.quad variable
25+
.long 2
26+
27+
#--- func.s
28+
.global _pei386_runtime_relocator
29+
.text
30+
_pei386_runtime_relocator:
31+
ret
32+
33+
#--- lib.def
34+
EXPORTS
35+
variable DATA
36+

0 commit comments

Comments
 (0)