Files
reverse-engineering-anything/tests/conformance/ghidra/loader-relocation.c
T
Aurelio Bernalandmorluto 9b1ffa1f7d fix(ghidra): protect only unapplied relocation destinations, not their whole block (#11996)
Since #10483, an unapplied loader relocation in a non-writable block made
that whole block volatile. TLS slots are such relocations and sit in .got
beside the entries Ghidra did apply, so in any shared object or PIE with
initial-exec or dynamic TLS every GOT-indirect call decompiled as pointer
arithmetic instead of a named call.

Mark each unapplied destination volatile through its data item's Mutability
setting, creating an undefined item of the relocation's length where nothing
is defined. Fall back to the whole block only when an instruction or a data
item starting before the destination occupies it. Eligibility, limitations,
the read-only path, permissions and source FileBytes are unchanged.

The loader-relocation fixture now reads an initial-exec TLS variable whose
R_X86_64_TPOFF64 slot shares .got with the applied entries. The probe
requires that block and its applied entries to stay non-volatile and both
unapplied destinations to have volatile data items; on the previous bridge
it fails at applied_got_relocations_not_volatile.

Rename loader_relocation_semantics to
volatile-unapplied-relocation-destinations to state the new behavior.

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-11-18 20:30:01 -05:00

71 lines
2.7 KiB
C

/* Source-owned Linux ELF fixture for the Ghidra loader-relocation boundary. */
typedef unsigned int rea_loader_word;
/* This file-backed value is the constant the unprotected decompiler may fold. */
__attribute__((used, visibility("default")))
const rea_loader_word rea_loader_value = 0x11111111u;
#if defined(__ELF__)
#define REA_LOADER_POINTER_SECTION ".data.rel.ro"
#elif defined(__APPLE__)
#define REA_LOADER_POINTER_SECTION "__DATA_CONST,__const"
#else
#error "The loader-relocation fixture requires an ELF or Mach-O compiler"
#endif
/* The loader fills this pointer before its RELRO block is sealed read-only. */
__attribute__((used, visibility("default"), section(REA_LOADER_POINTER_SECTION)))
const rea_loader_word *volatile const rea_loader_pointer = &rea_loader_value;
/* A const object defined by the provider. In a PIE, GCC and the linker copy
* it into RELRO with a copy relocation that Ghidra cannot apply, so the file
* holds zero where the loader writes the provider's value: the #4177 shape. */
extern const rea_loader_word rea_external_const;
__attribute__((noinline, used, visibility("default")))
rea_loader_word rea_loader_reader(void) {
return rea_external_const;
}
__attribute__((noinline, used, visibility("default")))
void rea_loader_writer(void) {
*(volatile rea_loader_word *)(void *)&rea_external_const = 0;
}
/* An applied relocation in the same RELRO segment; it is not protected. */
__attribute__((noinline, used, visibility("default")))
rea_loader_word rea_loader_pointer_reader(void) {
return *rea_loader_pointer;
}
extern rea_loader_word rea_external(int value);
/* Weak so -fPIE code reads it through a RELRO GOT entry. A strong reference
* lets GCC use a copy relocation into writable .bss instead. */
extern rea_loader_word rea_external_value __attribute__((weak));
__attribute__((noinline, used, visibility("default")))
rea_loader_word rea_loader_got_caller(rea_loader_word value) {
return rea_external((int)value) + rea_external_value;
}
/* An initial-exec TLS variable from the provider. Its R_X86_64_TPOFF64 slot
* sits in the same read-only .got as the applied entries above, and only the
* runtime loader fills it, so Ghidra imports it unapplied. Protecting that
* slot must not make the applied GOT entries beside it opaque. */
extern __thread rea_loader_word rea_external_tls
__attribute__((tls_model("initial-exec")));
__attribute__((noinline, used, visibility("default")))
rea_loader_word rea_loader_tls_reader(void) {
return rea_external_tls;
}
__attribute__((used, visibility("default"))) void rea_loader_start(void) {
volatile rea_loader_word result = rea_loader_reader();
(void)result;
for (;;) {
}
}
#undef REA_LOADER_POINTER_SECTION