Function Overview
Functions are fundamental units of code organization in Ghidra, representing callable subroutines with defined entry points, parameters, and return values.The
FunctionPlugin provides comprehensive actions for creating, editing, and deleting functions and their variables.Creating Functions
Manual Function Creation
Position Cursor
Navigate to function entry point:
- Place cursor on first instruction
- Should be a typical function prologue
- Or known entry point from analysis
Create Function
Execute create action:
- Press
Fkey - Right-click >
Function>Create Function - Menu:
Function>Create Function
Automatic Function Detection
Auto-analysis creates functions automatically:- Entry point analysis
- Call target analysis
- Pattern-based detection
- External references
Creating Multiple Functions
Select Range
Select addresses for functions:
- Highlight multiple entry points
- Can span discontinuous ranges
Function Properties
Function Signature
A complete function signature includes:- Return Type
- Parameters
- Calling Convention
- Attributes
Function return value:
- Data type returned
voidfor no return- Set via
SetReturnDataTypeCmd - Affects decompiler output
Editing Signatures
Open Editor
Access function signature editor:
- Press
Ctrl + Shift + Gon function - Right-click function >
Edit Function Signature - Double-click in Function Window
Modify Components
Edit signature parts:
- Change return type via dropdown
- Add/remove/reorder parameters
- Rename parameters
- Set calling convention
Use
ApplyFunctionSignatureCmd programmatically to set signatures from scripts or analysis.Function Variables
Variable Types
- Parameters
- Local Variables
- Register Variables
Function inputs:
- Passed in registers or stack
- Named in signature
- Typed for analysis
- Accessed in function body
Renaming Variables
Select Variable
Find variable to rename:
- In decompiler view
- Or in function editor
- Click on variable name
Rename Action
Execute rename:
- Press
Lin decompiler - Right-click >
Rename Variable - Implemented by
EditNameAction
Retyping Variables
Choose Type
Select new data type:
- Press
Ctrl + Lin decompiler - Browse Data Type Manager
- Select appropriate type
Variable Comments
Add Comment
Document variables:
- Right-click variable >
Set Comment - Implemented by
VariableCommentAction - Appears in function display
Function Window
View all functions in a table:Open Function Window
Access function list:
Window>Functions- Shows all program functions
- Sortable columns
Table Columns
Function table displays:- Name: Function symbol name
- Location: Entry point address
- Size: Function body size in bytes
- Parameter Count: Number of parameters
- Namespace: Containing namespace/class
- Source: Analysis source type
Advanced Function Operations
Thunk Functions
Thunks are forwarding functions:Identify Thunk
Recognize thunk pattern:
- Simple jump to another function
- No actual logic
- Common in import tables
Create Thunk
Mark as thunk:
- Right-click >
Function>Create Thunk Function - Implemented by
thunkFunctionAction - Links to target function
Edit Thunk
Modify thunk properties:
- Use
EditThunkFunctionAction - Change thunk target
- Update calling convention
External Functions
Functions in external libraries:Create External Function
Define library function:
- Use
CreateExternalFunctionAction - Specify library name
- Set function name
Function Purge
For stack cleanup (x86 stdcall):- Bytes popped by callee
- Determined by calling convention
- Affects stack balance analysis
Function Analysis
Call Trees
Visualize function relationships:View Callers
See what calls this function:
- Right-click function >
References>Show Call Trees to... - Tree view of callers
- Navigate call hierarchy
Stack Frame Analysis
Ghidra analyzes stack usage:- Local variable allocation
- Stack parameter access
- Saved registers
- Stack frame size
Register Analysis
Register usage tracking:- Modified registers
- Parameter registers
- Return value registers
- Preserved registers
Function Tags
Organize functions with tags:Apply Tags
Tag functions:
- Right-click function >
Edit Tags - Select applicable tags
- Multiple tags per function
Deleting Functions
Delete Action
Remove function:
- Right-click >
Function>Delete Function - Uses
DeleteFunctionAction - Confirms deletion
Deleting a function doesn’t delete the code - only the function metadata. The instructions remain in the program.
Special Function Types
No-Return Functions
Functions that never return:exit(),abort(), exception throwers- Mark with no-return attribute
- Affects control flow analysis
- Use
FixupNoReturnFunctionsScript.java
Inline Functions
Marking for inline expansion:- Set inline attribute
- Use
MakeFunctionsInlineVoidScript.java - Affects call graph display
Variadic Functions
Variable argument count:printf,sprintf, etc.- Mark with varargs attribute
- Special parameter handling
