#765·ghidra

Code Segment is not restored after far call on x86

Author: GregoryMorseCreated Jul 8, 2019Updated Sep 28, 2026
LabelsType: BugStatus: Prioritize

Describe the bug The x86 (important for 16-bit programs mainly) CS register is not recovered in the context properly by the decompiler after a far call is executed which has a different segment. The loader currently attempts to place imports in the same segment so this bug is less likely to turn up. Upon continuing disassembly scanning after a far call, the CS register should be implicitly restored to the current instruction pointer/program counter (IP) execution address which requires that CS to even do the scanning. But the scanning proceeds without even needing the context CS:IP values so until an instruction requests CS explicitly its not an issue. Jump tables specifically will cause this problem.

x86-16.pspec contains inexplicably: programcounter register="EIP" where it should really be "CS:IP", and presumably this "EIP and "RIP" is erroneous x86.pspec and x86-64.pspec respectively as the CS register is still used to address that instruction pointer.

I would point out the stackpointer register="SP" is in the compiler spec and may have slightly different handling and behavior as the stack is a useful construct not fundamental to raw processor execution.

To Reproduce Decompile 16-bit code with the following sequence (ensuring import_addr resolves to a different CS segment register than the current segment): ... CALLF import_addr ; 0x9a code... JMP CS:[offset+bx] offset: jump_table... code_for_jump_table_offsets...

Expected behavior The decompiler should return the CS register after a far call to the value prior to the call which is the current CS:IP pair of the code being scanned. The Program Counter in this case is really the pair CS:IP and not merely IP which is part of the problem. Due to the thoroughness of the architecture I have not yet pinpointed exactly how to fix this problem.

Currently the stack pointer is properly adjusted and the fix should be in the same place of scanning post-call with a properly adjusted stack pointer by properly adjusting the code segment register as well.

From my understanding of the model, a call fixup or some sleigh code is supposed to be queried by the decompiler which will for the most part just restore the CS and SP registers yet this is not occurring.

Environment (please complete the following information):

  • OS: Windows 10
  • Java Version: 11.0.3
  • Ghidra Version: 9.0.4

Source: NationalSecurityAgency/ghidra