Skip to content

[AUTOMATED] Keep null checks on undefined weak functions in relocatable objects - #1599

Open
phix33 wants to merge 6 commits into
Noelo-Lab:mainfrom
phix33:octeon/relocations-got-slots
Open

phix33 wants to merge 6 commits into
Noelo-Lab:mainfrom
phix33:octeon/relocations-got-slots

Conversation

@phix33

@phix33 phix33 commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Problem

A relocatable object that tests an undefined weak function before calling it loses the test: the call becomes unconditional and the fallback path disappears. Reproduce with Clang 21:

pr_repro_dir=$(mktemp -d)
printf '%s\n' 'extern int optional(int) __attribute__((weak));' \
  'int guard(int value) { return optional ? optional(value) : value + 7; }' > "$pr_repro_dir/weak.c"
clang-21 --target=mips64-linux-gnuabi64 -O2 -fPIC \
  -c "$pr_repro_dir/weak.c" -o "$pr_repro_dir/weak.o"
decompiler/target/release/kuna decompile "$pr_repro_dir/weak.o" guard

Current main emits:

long guard(int a0)
{
  return (int)optional((long)a0);
}

Fix

  • A MIPS object's GOT entry for an undefined weak symbol now holds 0, as GNU ld leaves it, and is an import slot outside the foldable GOT range. A call through it is named after the symbol. GOT_PAGE/GOT_OFST reach the same entry.
  • The function stays at its synthetic extern address, so the inventory, xrefs and the call graph are what main reports. The slot is another address of the same function, so a rename or prototype edit applies through both. Slot membership is a binary search over merged ranges, not a scan per candidate function.
  • Ordinary address operands read the same slot with their addends: MIPS HI16/LO16/HIGHER/HIGHEST, x86-64 absolute and PC-relative forms, and full-pointer fields in read-only data. Their slots sit in space reserved ahead of COMMON storage. Other operands, such as x86-64 GOTPCREL, get a warning comment; writable data and other architectures are not covered.
  • Linked images are unchanged: only a relocatable object gets these slots.

The example now emits:

long guard(int a0)
{
  if (dat_400090) // branch-flip
    a0 = optional((long)a0);
  else {
    a0 += 7;
  }
  return a0;
}

Tests

Stage tests kuna-mipsgot-weak.xml and kuna-nullable-relocations.xml add thirteen assertions. On main the existing PIC fixture reads return optional();, so the weak-test assertions fail. No existing stage or datatest expectation changes.

Checked-in objects built from original sources cover MIPS N32/N64, both byte orders for the GOT forms, and x86-64 small-model, large-model and RIP-relative forms, including COMMON objects and direct calls beside the weak function; CI needs no cross toolchain. CLI tests compile the emitted C and run it with the symbol bound and unbound, checking results, call counts, arguments and COMMON storage, and check that xrefs --to and decompile-graph still find the direct references.

Over 300 interleaved runs, median decompile and inventory times on a weak-call object, a COMMON object and a linked MIPS image are within 2.5% of main, none slower. Full Rust workspace tests, datatest/stage parity, tooling, lint and specification checks pass.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant