Skip to content

[LLD] [COFF] Error out if the runtime pseudo relocation function is missing #88573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lld/COFF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2072,8 +2072,16 @@ void Writer::createRuntimePseudoRelocs() {
return;
}

if (!rels.empty())
if (!rels.empty()) {
log("Writing " + Twine(rels.size()) + " runtime pseudo relocations");
const char *symbolName = "_pei386_runtime_relocator";
Symbol *relocator = ctx.symtab.findUnderscore(symbolName);
if (!relocator)
error("output image has runtime pseudo relocations, but the function " +
Twine(symbolName) +
" is missing; it is needed for fixing the relocations at runtime");
}

PseudoRelocTableChunk *table = make<PseudoRelocTableChunk>(rels);
rdataSec->addChunk(table);
EmptyChunk *endOfList = make<EmptyChunk>();
Expand Down
3 changes: 3 additions & 0 deletions lld/test/COFF/autoimport-arm-data.s
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
.text
.thumb
main:
bx lr
.global _pei386_runtime_relocator
_pei386_runtime_relocator:
bx lr
.data
ptr:
Expand Down
3 changes: 3 additions & 0 deletions lld/test/COFF/autoimport-arm64-data.s
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
.global main
.text
main:
ret
.global _pei386_runtime_relocator
_pei386_runtime_relocator:
ret
.data
ptr:
Expand Down
3 changes: 3 additions & 0 deletions lld/test/COFF/autoimport-gnu-implib.s
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@
.text
main:
movl data(%rip), %eax
ret
.global _pei386_runtime_relocator
_pei386_runtime_relocator:
ret
.data
36 changes: 36 additions & 0 deletions lld/test/COFF/autoimport-handler-func.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# REQUIRES: x86
# RUN: split-file %s %t.dir

# RUN: llvm-dlltool -m i386:x86-64 -d %t.dir/lib.def -D lib.dll -l %t.dir/lib.lib

# RUN: llvm-mc -triple=x86_64-windows-gnu %t.dir/main.s -filetype=obj -o %t.dir/main.obj
# RUN: llvm-mc -triple=x86_64-windows-gnu %t.dir/func.s -filetype=obj -o %t.dir/func.obj
# RUN: env LLD_IN_TEST=1 not lld-link -lldmingw -out:%t.dir/main.exe -entry:main %t.dir/main.obj %t.dir/lib.lib 2>&1 | FileCheck %s --check-prefix=ERR

# 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=NOERR --allow-empty

# ERR: error: output image has runtime pseudo relocations, but the function _pei386_runtime_relocator is missing; it is needed for fixing the relocations at runtime

# NOERR-NOT: error

#--- main.s
.global main
.text
main:
ret

.data
.long 1
.quad variable
.long 2

#--- func.s
.global _pei386_runtime_relocator
.text
_pei386_runtime_relocator:
ret

#--- lib.def
EXPORTS
variable DATA

3 changes: 3 additions & 0 deletions lld/test/COFF/autoimport-warn.s
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ main:
movl variable2(%rip), %ecx
addl %ecx, %eax
ret
.global _pei386_runtime_relocator
_pei386_runtime_relocator:
ret

.section .rdata$.refptr.variable1,"dr",discard,.refptr.variable1
.global .refptr.variable1
Expand Down
3 changes: 3 additions & 0 deletions lld/test/COFF/autoimport-x86.s
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
.text
main:
movl variable(%rip), %eax
ret
.global _pei386_runtime_relocator
_pei386_runtime_relocator:
ret
.data
ptr:
Expand Down