[PATCH] D154777: [LLD] [COFF] Warn about pseudo relocations that are too narrow

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 8 14:59:56 PDT 2023


mstorsjo created this revision.
mstorsjo added reviewers: rnk, alvinhochun, mati865, jeremyd2019.
Herald added a project: All.
mstorsjo requested review of this revision.
Herald added a project: LLVM.

In 64 bit mode, any references to symbols that might end up autoimported
must be made via full 64 bit pointers (usually in .refptr stubs
generated by the compiler).

If referenced via e.g. a 32 bit rip relative offset, it might work
as long as DLLs are loaded close together in the 64 bit address
space, but will fail surprisingly later if they happen to be loaded
further apart. Any cases of that happening is usually a toolchain
error, and the sooner we can warn about it, the easier it is to diagnose.

The warning message is a bit odd; normally all lld warnings/errors
start with lower case, but here the error message consists of two
full sentences. Style suggestions for how to handle that are
welcome.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154777

Files:
  lld/COFF/Chunks.cpp
  lld/test/CMakeLists.txt
  lld/test/COFF/autoimport-nowarn.s
  lld/test/COFF/autoimport-warn.s


Index: lld/test/COFF/autoimport-warn.s
===================================================================
--- /dev/null
+++ lld/test/COFF/autoimport-warn.s
@@ -0,0 +1,29 @@
+# REQUIRES: x86
+
+# RUN: echo -e "EXPORTS\nvariable1 DATA\nvariable2 DATA" > %t-lib.def
+# RUN: llvm-dlltool -m i386:x86-64 -d %t-lib.def -D lib.dll -l %t-lib.lib
+
+# RUN: llvm-mc -triple=x86_64-windows-gnu %s -filetype=obj -o %t.obj
+# RUN: lld-link -lldmingw -out:%t.exe -entry:main %t.obj %t-lib.lib -verbose 2>&1 | FileCheck %s
+
+# CHECK-NOT: runtime pseudo relocation {{.*}} against symbol variable1
+# CHECK: warning: runtime pseudo relocation in {{.*}}.obj against symbol variable2 is too narrow; only 32 bits wide. This can fail at runtime depending on memory layout.
+# CHECK-NOT: runtime pseudo relocation {{.*}} against symbol variable1
+
+    .global main
+    .text
+main:
+    movq .refptr.variable1(%rip), %rax
+    movl (%rax), %eax
+    movl variable2(%rip), %ecx
+    addl %ecx, %eax
+    ret
+
+    .section .rdata$.refptr.variable1,"dr",discard,.refptr.variable1
+    .global .refptr.variable1
+.refptr.variable1:
+    .quad variable1
+
+relocs:
+    .quad __RUNTIME_PSEUDO_RELOC_LIST__
+    .quad __RUNTIME_PSEUDO_RELOC_LIST_END__
Index: lld/test/COFF/autoimport-nowarn.s
===================================================================
--- /dev/null
+++ lld/test/COFF/autoimport-nowarn.s
@@ -0,0 +1,19 @@
+# REQUIRES: x86
+
+# RUN: echo -e "EXPORTS\nvariable" > %t-lib.def
+# RUN: llvm-dlltool -m i386 -d %t-lib.def -D lib.dll -l %t-lib.lib
+
+# RUN: llvm-mc -triple=i386-windows-gnu %s -filetype=obj -o %t.obj
+# RUN: lld-link -lldmingw -out:%t.exe -entry:main %t.obj %t-lib.lib -verbose 2>&1 | FileCheck --allow-empty %s
+
+# CHECK-NOT: runtime pseudo relocation {{.*}} is too narrow
+
+    .global _main
+    .text
+_main:
+    movl _variable, %eax
+    ret
+
+relocs:
+    .long ___RUNTIME_PSEUDO_RELOC_LIST__
+    .long ___RUNTIME_PSEUDO_RELOC_LIST_END__
Index: lld/test/CMakeLists.txt
===================================================================
--- lld/test/CMakeLists.txt
+++ lld/test/CMakeLists.txt
@@ -51,6 +51,7 @@
     llvm-config
     llvm-cvtres
     llvm-dis
+    llvm-dlltool
     llvm-dwarfdump
     llvm-lib
     llvm-lipo
Index: lld/COFF/Chunks.cpp
===================================================================
--- lld/COFF/Chunks.cpp
+++ lld/COFF/Chunks.cpp
@@ -661,6 +661,13 @@
             toString(file));
       continue;
     }
+    int addressSizeInBits = file->ctx.config.is64() ? 64 : 32;
+    if (sizeInBits < addressSizeInBits) {
+      warn("runtime pseudo relocation in " + toString(file) + " against " +
+           "symbol " + target->getName() + " is too narrow; only " +
+           Twine(sizeInBits) + " bits wide. This can fail at runtime " +
+           "depending on memory layout.");
+    }
     // sizeInBits is used to initialize the Flags field; currently no
     // other flags are defined.
     res.emplace_back(target, this, rel.VirtualAddress, sizeInBits);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154777.538392.patch
Type: text/x-patch
Size: 3021 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230708/8a5bcc34/attachment.bin>


More information about the llvm-commits mailing list