#9677·ghidra

x86-16 emulation: no segment user op, and far returns and indirect far branches lose CS

Author: kibertoadCreated Sep 25, 2026Updated Sep 26, 2026

Describe the bug

16-bit x86 code (x86:LE:16:Protected Mode and x86:LE:16:Real Mode) can't be emulated without workarounds, in the legacy EmulatorHelper, PcodeEmulator or JitPcodeEmulator. There are two causes.

  1. No emulator implements the segment user op. The x86-16 SLEIGH computes every memory operand and several branch targets through segment(seg, off). The processor specs define its meaning for the decompiler (segmentop in x86-16.pspec and x86-16-real.pspec: (zext(base) << 16) + zext(inner) in protected mode, << 4 in real mode), but the x86 userop library has no segment. So the first memory access fails:

    • legacy EmulatorHelper: UnimplementedCallOtherException;
    • PcodeEmulator and JitPcodeEmulator: SleighLinkException: Sleigh userop 'segment' is not in the library.
  2. Some 16-bit far branches in ia.sinc drop or never set the code segment.

    • RET imm16 (C2), RETF imm16 (CA) and IRET (CF) with 16-bit operand and address size set EIP=zext(IP), while plain RET (C3) and RETF (CB) set EIP=segment(CS,IP). After a RETF imm16, execution continues at segment 0000 and the next decode fails, for example Unknown disassembly error (PC=0000:4d3e).
    • CALLF m16:16 (FF /3) branches to the right linear address but never assigns CS, so the next near return or segment-relative access uses the caller's CS.
    • JMPF m16:16 (FF /5) with 16-bit operand size zero-extends the offset and ignores the selector.

    The direct forms (CALLF ptr16:16, JMPF ptr16:16) are correct, because ptr1616 assigns CS.

To Reproduce

  1. Create a PcodeEmulator for x86:LE:16:Real Mode.
  2. Put C2 04 00 (RET 4) at 1000:0100, set CS=1000, SS=2000, SP=0100, and write the word 0234 at 2000:0100.
  3. Step one instruction.
  4. stepInstruction stops on the missing segment op when it pops the stack. With a segment op supplied, the counter ends at 0000:0234 instead of 1000:0234.

Expected behavior

16-bit code emulates with the segment arithmetic the processor specs already define, and far returns, far indirect calls and far indirect jumps leave CS and the counter at the far target.

Environment

  • OS: Windows 11 Pro (build 26200)
  • Java Version: Temurin 21.0.12.1+1
  • Ghidra Version: 12.1.3; the same definitions are in ia.sinc on master
  • Ghidra Origin: official GitHub distro

Additional context

We hit this running functions of a 1994 16-bit Windows (NE) executable in all three emulators. With a segment implementation and the branch targets corrected, PcodeEmulator gave the same results as another emulator on every case we ran. Patching branch targets from outside did not work for JitPcodeEmulator, because the CS in its thread state was out of date when a compiled block exited. With CS assigned in ia.sinc itself, it passes the same single-instruction tests as PcodeEmulator. #9678 has the fix and the tests.

Source: NationalSecurityAgency/ghidra