Ghidra Downloadindependent download & install guide
English
P-CODE ANALYSIS GUIDE

Ghidra P-Code Explained: Operations, Varnodes, and Flow

Ghidra P-code is the intermediate representation that helps explain how machine instructions affect values and control flow. This guide shows how to read an operation, identify its varnodes and address space, trace a memory access, and compare the result with Decompiler output. The examples are schematic: the exact operations depend on the processor and function being analyzed.

Open the P-Code Operation Reference
Best forTracing instruction semantics
Core piecesOperations and varnodes
Inspect withPCode and Decompiler views
Main outputA testable behavior trace
QUICK ANSWER

What Ghidra P-code represents

Ghidra P-code is a low-level intermediate representation (IR) used by Ghidra's analysis and Decompiler. A processor instruction is translated into one or more operations with explicit inputs, outputs, and sizes. That common operation vocabulary lets analysis reason about different processor instructions without treating their assembly mnemonics as the whole program. The translation is defined by the processor language and the instruction being decoded, so two architectures can express similar behavior with different operation sequences.

P-code is not source code, and it is not simply another spelling of an assembly instruction. It makes pieces of behavior visible: arithmetic, copies, loads, stores, comparisons, and branches. A sequence may clarify what a selected instruction does, but it does not tell you by itself why the program performs that action. Intent still needs evidence from surrounding instructions, references, inputs, and control flow.

The Decompiler uses P-code as part of a larger analysis pipeline and turns its results into readable pseudocode. Reading the intermediate operations is useful when a cast, pointer, condition, or memory access in pseudocode seems unclear. For the higher-level view, continue with the Ghidra Decompiler guide; this page focuses on the operations and values underneath that view.

Treat an operation trace as evidence, not a verdict

Use opcode semantics to explain a local behavior, then check the instruction, references, and surrounding blocks before assigning a higher-level meaning to a value.

ViewWhat it showsWhat it cannot establish alone
AssemblyThe processor instruction and its operandsThe full meaning of values without context
P-codeOperations, varnode inputs/outputs, and control effectsSource-level intent or variable names
DecompilerA higher-level pseudocode interpretationThat every inferred type or name is correct
CORE CONCEPTS

Read operations, varnodes, and address spaces

A P-code operation has an opcode, zero or more input varnodes, and sometimes an output varnode. Arithmetic operations such as `INT_ADD` describe a calculation; `COPY` moves a value; `LOAD` and `STORE` access memory; branch operations affect where execution continues. Some operations have no output because their purpose is a side effect or control transfer. The P-Code Operation Reference documents the required operands and semantics for each opcode. The NSA/Ghidra PcodeOp.java source describes this object as an opcode with input varnodes and an optional output; the Riverloop Security case study shows value tracing in a practical analysis task.

A varnode is a location-and-size description of a value. It identifies an address space, an offset within that space, and a byte length. It is not automatically a source-language variable. A varnode can refer to a register, program memory, a constant, or temporary storage used during translation and analysis. The same source-level value may appear through several varnodes as it moves between registers, temporaries, and memory.

Address spaces tell you how to interpret a varnode's location. A `ram` varnode refers to program memory, a register-space varnode refers to a register, a constant-space varnode represents a literal, and a `unique` varnode is a temporary. Actual programs can have other spaces as well. Size matters: `r0:4` is a four-byte value in a register-like space in this teaching notation, not proof of a 32-bit source variable. Confirm the processor language and operation semantics before assigning a type.

The example is schematic

Real varnode names, operation counts, and space identifiers vary with the processor language and instruction. The point is to follow each input and output, not to expect these exact lines in every program.

ItemQuestion to askWhy it matters
OpcodeWhat operation is being performed?Names such as `INT_SRIGHT` and `INT_RIGHT` encode different shift behavior.
InputsWhich varnodes feed the operation?Input order and width affect how arithmetic or memory access is read.
OutputWhere does the result go?A temporary or register destination is not necessarily a stable variable.
Space and sizeWhere is the value stored, and how many bytes?Address-space and width assumptions can change the interpretation.
SequenceWhich instruction or block produced it?Nearby operations and control flow supply context missing from one line.
Teaching notation, not a verbatim Ghidra listingtmp:4 = INT_ADD(base:4, 0x10:4) value:4 = LOAD(ram, tmp:4) result:4 = INT_XOR(value:4, mask:4)
Formal details to keep in mindLOAD(space-id, pointer-offset) -> output STORE(space-id, pointer-offset, value) -> side effect
WORKED EXAMPLE

Trace a memory read and calculation in Ghidra P-code

Suppose an instruction sequence computes an address from a base value, reads a value at that address, and combines it with a mask. In the schematic listing above, start at `INT_ADD`: identify both inputs, compare their sizes, and note that the result is placed in `tmp`. Next read `LOAD` as a memory access through an address space and pointer offset. Its output becomes the input to `INT_XOR`. This gives you a small data-flow story: address calculation, memory read, then bitwise operation.

The `LOAD` shorthand in the example names `ram` for readability. Ghidra's P-code form represents the address space through a special space identifier input and the pointer offset through a varnode. Do not mistake the display shorthand for a claim about exact syntax. Likewise, the byte size `:4` describes the width of the teaching value. Check the actual operation record for the selected instruction before deciding whether it represents a pointer, signed integer, bit field, or something else.

Now test the trace against the surrounding Listing. Does the instruction really form the same offset? Is the loaded address inside the expected memory block? Does another reference load the same field at the same stride? If the result flows into a conditional branch, inspect the comparison operation and its signedness. A trace becomes useful when it agrees with raw bytes, instruction operands, and another use of the value.

  1. 1

    Start at the operation output

    Select a P-code operation and write down its output space and size before following the value elsewhere.

    output varnode -> next use
  2. 2

    Follow inputs backward

    Locate the earlier operation that created each input. Distinguish constants, register values, memory locations, and temporaries.

    input -> defining operation
  3. 3

    Check every memory access

    For LOAD or STORE, record the named space, pointer-offset input, access size, and resulting address range.

    space + offset + width
  4. 4

    Verify at the instruction

    Compare the operation sequence with the selected machine instruction and another reference or use of the value.

    P-code -> Listing -> reference
  • Keep the active processor language and address spaces visible in your notes.
  • Do not infer a pointer only because a value is later added to a constant.
  • Record whether a comparison is signed or unsigned before explaining a branch.
  • Treat temporary varnodes as data-flow locations, not guaranteed source variables.
TWO ANALYSIS LEVELS

Separate raw P-code from high P-code and pseudocode

Raw P-code reflects the processor instruction semantics defined by Ghidra's language specification. It keeps a close relationship to the decoded instruction and is useful when you want to understand how the instruction was modeled. It may contain several small operations for one instruction, or it may use operations that expose details that later analysis can simplify.

High P-code is produced during Decompiler analysis. It can represent data flow and control flow in a form that supports simplification, type propagation, and recovery of higher-level constructs. At joins, for example, a `MULTIEQUAL` operation can represent a value selected from different incoming control-flow paths. That does not mean the original processor had a single instruction named `MULTIEQUAL`; it is an analysis-level representation.

Decompiler pseudocode is another step up: it may combine operations into expressions and assign inferred names or types. Treat each view as a different level of explanation. If the pseudocode says `count++`, inspect the contributing operations and their widths when the exact behavior matters. If the operations do not support the high-level interpretation, revisit the function signature, data types, and control-flow assumptions instead of trusting a clean-looking output.

LayerTypical useReview question
Instruction and ListingAnchor a trace to bytes and decoded operandsIs this the correct instruction and memory context?
Raw P-codeInspect the processor-level translationWhich operations express this instruction's semantics?
High P-codeUnderstand Decompiler data-flow analysisWhich values or branches were joined or simplified?
Decompiler outputRead a higher-level function interpretationDo types, names, and expressions agree with lower-level evidence?
PRACTICAL WORKFLOW

Inspect an instruction and its P-code in Ghidra

Begin with a function or instruction whose behavior you can verify from the Listing. Open the program in CodeBrowser and select the instruction of interest. Ghidra versions and workspace layouts can place analysis windows differently; use the PCode window for the selected instruction when available, and keep Listing and Decompiler nearby for comparison. The exact docking arrangement is not part of the analysis result.

Read the operation as a record, not as an unexplained abbreviation. Identify its opcode, ordered inputs, optional output, and the size of each varnode. If the operation reads or writes memory, record its address space and pointer offset. If it changes control flow, record the destination or condition and relate it to the current basic block. Then follow the result to its next use rather than stopping at the instruction that created it.

For a function-level question, compare the local operation trace with the Decompiler view and the function's references. The Decompiler Concepts documentation explains Ghidra's intermediate language and analysis model; use the Decompiler Concepts reference for terminology, and the official operation reference for opcode semantics. Keep claims narrow: describe the behavior supported by the trace before naming a structure field or application-level purpose.

  1. 1

    Choose a verifiable instruction

    Start with a load, store, arithmetic operation, comparison, or branch whose surrounding Listing is understandable.

    selected instruction + address
  2. 2

    Open the PCode view

    Bring up the PCode window if it is available in the current Ghidra workspace and focus it on the selected instruction.

    selected instruction -> PCode
  3. 3

    Annotate operands and width

    Record opcode, input order, output, size, address space, and any special control-flow effect.

    opcode / inputs / output / size / space
  4. 4

    Compare another view

    Check the instruction operands, nearby Listing, references, and Decompiler output before assigning meaning.

    PCode + Listing + Decompiler
Ghidra Decompiler view used as a higher-level comparison for a P-code trace
Decompiler output is a useful comparison layer; verify important claims against the instruction and its operations.
FUNCTION CONTEXT

Follow P-code data flow alongside control flow

Data flow answers where a value comes from and where it goes. Follow a varnode from its defining operation through copies, arithmetic, memory access, and later uses. Control flow answers which block can execute next. A conditional branch can separate paths that produce different values, and a merge can bring those values together. Keep these questions distinct: a graph edge explains reachability, while the varnode trace explains the data carried along a path.

At a branch, inspect the condition's inputs and the operation that computed them. Check whether the comparison is signed or unsigned, whether the branch is conditional, and where each destination leads. When a later operation receives a merged value, locate the predecessor blocks that contribute it. High P-code can use operations such as `MULTIEQUAL` to express that merge; explain the source paths rather than presenting the operation as a mystery variable.

The Ghidra Function Graph guide helps map blocks and branches, while this P-code guide follows the values within and between those blocks. If you first need to find the relevant function or reference, use the Ghidra search guide. Moving between these views keeps a local operation from being mistaken for a complete function explanation.

Trace one path at a time

Write down the branch condition and the value available on each incoming edge. This prevents a value from one path being described as if it were present on every path.

Ghidra function graph used to connect P-code traces with branches and basic blocks
A function graph shows reachability between blocks; P-code explains the values processed along those paths.
TROUBLESHOOTING

Troubleshoot misleading P-code traces

If an operation seems impossible, first verify that you selected the intended instruction and that the active processor language matches the program. Then check varnode widths, address spaces, and the operation's operand order. A four-byte value interpreted as an eight-byte pointer can redirect every later inference. Similarly, confusing signed and unsigned comparisons can make a branch appear inverted even when the operation is consistent.

If P-code and Decompiler output appear to disagree, determine whether the views refer to the same address and analysis state. Wait for analysis to finish, reselect the instruction or function, and inspect the underlying references. Check whether an inferred type or function signature changed how the Decompiler presents a value. Preserve a project copy before broad edits; adjust one assumption at a time and note which evidence changed.

Keep the evidence trail small and repeatable

Record the address, opcode, inputs, output, sizes, space, and the next use. That is enough to reproduce a trace without claiming more than the operations show.

SymptomCheck firstSafer next step
A value leaves the expected memory rangeAddress space, pointer offset, and widthTreat the value conservatively until a valid target is found.
A branch appears reversedSignedness, comparison opcode, and condition inputsTrace the condition and both destinations.
Decompiler shows an unexpected castFunction signature, type, and varnode sizeCompare the operation and a caller before changing the type.
The same value seems to changeDefinitions, merge blocks, and path conditionsTrace each predecessor path separately.
PCode view looks staleSelected address and analysis completionReselect the instruction and compare the Listing location.
ANALYSIS CHECKLIST

A repeatable workflow for reading Ghidra P-code

A reliable P-code explanation starts from a narrow question: what does this instruction read, calculate, write, or test? Anchor that question to the Listing address, inspect the operation and varnodes, and follow the value to a relevant use. Then check the address space, size, signedness, references, and branch destinations. Use Decompiler output as a higher-level comparison, not as a substitute for checking the operations.

Keep uncertain labels provisional. If the trace suggests a field or pointer, test the interpretation against another access, a caller, or a repeated object before naming it confidently. If the evidence stops at an operation, state that limit. For scripted or repeated work, first verify one manual trace and then use the Ghidra Script Manager guide to explore automation; a script can repeat a sound rule, but it can also spread an unchecked assumption.

  • Anchor every claim to a selected instruction or basic block.
  • Read opcode, inputs, output, sizes, and address spaces before assigning meaning.
  • Follow definitions to uses and separate data flow from control flow.
  • Check memory accesses and branch conditions against the Listing and references.
  • Compare high-level pseudocode only after the operation trace is understood.
  • Mark uncertainty and preserve a reversible project copy before broad changes.
FREQUENTLY ASKED QUESTIONS

Questions about Ghidra P-code

What is Ghidra P-code used for?

Ghidra P-code is an intermediate representation used to analyze how instructions manipulate values and control flow. It helps explain operations beneath Decompiler pseudocode, but it is not source code and does not prove program intent by itself.

What is a varnode in Ghidra?

A varnode describes a value by its address space, offset, and byte size. It may refer to a register, memory, a constant, or temporary storage; it is not automatically a source-language variable.

How is P-code different from Decompiler output?

P-code exposes lower-level operations and their value flow. The Decompiler combines analysis results into higher-level pseudocode with inferred expressions, types, and names. Use P-code to investigate the lower-level behavior when a pseudocode interpretation needs checking.

Does one machine instruction always produce one P-code operation?

No. An instruction may translate into several operations, and the sequence depends on the processor language and instruction semantics. Inspect the selected instruction rather than assuming a fixed one-to-one mapping.

Can P-code prove what a program variable means?

P-code can provide evidence about how a value is calculated, read, written, or tested. A source-level name or purpose needs broader evidence from references, repeated use, surrounding control flow, and program context.