Skip to content

[AUTOMATED] fix(analysis): name PPC64 ELFv1 import stubs that end in bctr or load the environment word - #1600

Open
phix33 wants to merge 2 commits into
Noelo-Lab:mainfrom
phix33:fix/elfv1-small-toc-environment-stub
Open

phix33 wants to merge 2 commits into
Noelo-Lab:mainfrom
phix33:fix/elfv1-small-toc-environment-stub

Conversation

@phix33

@phix33 phix33 commented Nov 16, 2026

Copy link
Copy Markdown
Contributor

On 64-bit PowerPC ELFv1, kuna names an import stub in only three of the eight
layouts GNU ld emits. The plain bctr stub that ld gives an executable which
creates no threads is one of the other five, so in a stripped binary the import
is an anonymous function and a no-return import no longer ends its block:

cat > next.c <<'EOF'
void exit(int);
int next(int x)
{
    if (x < 0)
        exit(1);
    return x + 1;
}
EOF
powerpc64-linux-gnu-gcc -O2 -fPIC -shared -nostdlib -Wl,--no-plt-thread-safe next.c -o next.so
powerpc64-linux-gnu-strip next.so
kuna decompile next.so next
long next(int a0) // warn: Control flow encountered bad instruction data
{
  if (0 <= a0)
    return a0 + 1;
  sub_280(1);
  halt_baddata(); // warn: Bad instruction - Truncating control flow here
}

-Wl,--plt-static-chain prints the same. With neither option the output is
exit(1); // no-return.

Fix

  • A stub is std r2,40(r1), an entry load from r2 or from an addis base,
    mtctr, and the TOC load, then bctr or the lazy-resolution tail. The
    environment load that --plt-static-chain adds is now optional in every
    layout, and either ending may follow. Before, bctr was accepted only after
    an environment load from an addis base, and the lazy tail only without one.
  • When r2 itself is the base, the environment load must precede the TOC load,
    which replaces r2. The same two loads in the other order stay unnamed.
  • Naming is otherwise unchanged. The displacement and every possible TOC must
    still select one zero-addend R_PPC64_JMP_SLOT, and a lazy tail must still
    reach that import's resolver entry.

Tests

  • Unit tests take every base, environment and ending combination with either
    entry register, and decline each single deviation: a wrong offset, register
    or base in any load, a missing TOC load, the loads or the CTR move out of
    order, an entry register other than r11 or r12, and an ending that is
    neither bctr nor the whole lazy tail. They fail without the fix.
  • The linked-image CLI tests gain five stub forms. Each is named and marks
    __stack_chk_fail no-return; conflicting TOCs, a local-descriptor alias, a
    nonzero addend and a lazy tail that reaches another import's resolver entry
    stay unnamed. Two of these tests fail without the fix.
  • In radare2-testbins, elf/ppc64v1-more gains its 113 import names. Its 33
    halt_baddata truncations are gone, and 454 of its 457 import call sites
    match the C prototypes; the other three are as before. Four functions there
    lose 44 call sites that were reached only by running past a no-return call.
    elf/ppc64v1-libz-static-chain.so gains its 50 names and prints all 176
    functions exactly as elf/ppc64v1-libz.so. No datatest or stage expectation
    moved.

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