Re: [PATCH 6/7] x86: objtool: add support for R_X86_64_REX_GOTPCRELX

From: Uros Bizjak
Date: Wed Apr 16 2025 - 10:24:57 EST




On 16. 04. 25 10:54, Alexander Potapenko wrote:
When compiling modules with -fsanitize-coverage=trace-pc-guard, Clang
will emit R_X86_64_REX_GOTPCRELX relocations for the
__start___sancov_guards and __stop___sancov_guards symbols. Although
these relocations can be resolved within the same binary, they are left
over by the linker because of the --emit-relocs flag.

This patch makes it possible to resolve the R_X86_64_REX_GOTPCRELX
relocations at runtime, as doing so does not require a .got section.
In addition, add a missing overflow check to R_X86_64_PC32/R_X86_64_PLT32.

Cc: x86@xxxxxxxxxx
Signed-off-by: Alexander Potapenko <glider@xxxxxxxxxx>
---
arch/x86/include/asm/elf.h | 1 +
arch/x86/kernel/module.c | 8 ++++++++
arch/x86/um/asm/elf.h | 1 +
tools/objtool/arch/x86/decode.c | 1 +
4 files changed, 11 insertions(+)

diff --git a/arch/x86/include/asm/elf.h b/arch/x86/include/asm/elf.h
index 1fb83d47711f9..15d0438467e94 100644
--- a/arch/x86/include/asm/elf.h
+++ b/arch/x86/include/asm/elf.h
@@ -63,6 +63,7 @@ typedef struct user_i387_struct elf_fpregset_t;
#define R_X86_64_8 14 /* Direct 8 bit sign extended */
#define R_X86_64_PC8 15 /* 8 bit sign extended pc relative */
#define R_X86_64_PC64 24 /* Place relative 64-bit signed */
+#define R_X86_64_REX_GOTPCRELX 42 /* R_X86_64_GOTPCREL with optimizations */
/*
* These are used to set parameters in the core dumps.
diff --git a/arch/x86/kernel/module.c b/arch/x86/kernel/module.c
index 8984abd91c001..6c8b524bfbe3b 100644
--- a/arch/x86/kernel/module.c
+++ b/arch/x86/kernel/module.c
@@ -133,6 +133,14 @@ static int __write_relocate_add(Elf64_Shdr *sechdrs,
case R_X86_64_PC32:
case R_X86_64_PLT32:
val -= (u64)loc;
+ if ((s64)val != *(s32 *)&val)
+ goto overflow;
+ size = 4;
+ break;
+ case R_X86_64_REX_GOTPCRELX:
+ val -= (u64)loc;
+ if ((s64)val != *(s32 *)&val)
+ goto overflow;
size = 4;
break;

These two cases are the same. You probably want:

case R_X86_64_PC32:
case R_X86_64_PLT32:
case R_X86_64_REX_GOTPCRELX:
val -= (u64)loc;
if ((s64)val != *(s32 *)&val)
goto overflow;
size = 4;
break;

Uros.