API for opdis configuration. More...
Data Structures | |
struct | opdis_info_t |
An opdis disassembler. More... | |
Typedefs | |
typedef int(* | OPDIS_HANDLER )(const opdis_insn_t *i, void *arg) |
Callback used to determine if disassembly should continue. | |
typedef void(* | OPDIS_DISPLAY )(const opdis_insn_t *i, void *arg) |
Callback used to display or store a disassembled instruction. | |
typedef int(* | OPDIS_DECODER )(const opdis_insn_buf_t in, opdis_insn_t *out, const opdis_byte_t *buf, opdis_off_t offset, opdis_vma_t vma, opdis_off_t length, void *arg) |
Callback used to fill an opdis_insn_t from an opdis_insn_buf_t. | |
typedef opdis_vma_t(* | OPDIS_RESOLVER )(const opdis_insn_t *i, void *arg) |
Callback used to convert a branch target to a buffer offset. | |
typedef void(* | OPDIS_ERROR )(enum opdis_error_t error, const char *msg, void *arg) |
Callback used to handle error messages. | |
typedef opdis_info_t * | opdis_t |
Disassembler handle (pointer to opdis_info_t). | |
Enumerations | |
enum | opdis_x86_syntax_t { opdis_x86_syntax_intel, opdis_x86_syntax_att } |
Syntax options for x86 disassembly. More... | |
Functions | |
int | opdis_default_handler (const opdis_insn_t *insn, void *arg) |
The built-in opdis handler callback. The default handler returns true unless the instruction is invalid or if the address has already been visited. | |
void | opdis_default_display (const opdis_insn_t *i, void *arg) |
The built-in opdis display callback This callback writes the instruction ascii field to STDOUT. | |
int | opdis_default_decoder (const opdis_insn_buf_t in, opdis_insn_t *out, const opdis_byte_t *buf, opdis_off_t offset, opdis_vma_t vma, opdis_off_t length, void *arg) |
The built-in opdis instruction decoder This callback fills the ascii, offset, vma, bytes, and size fields of the output instruction object. It is recommended that all other decoders invoke this callback directly to fill these fields. | |
opdis_vma_t | opdis_default_resolver (const opdis_insn_t *insn, void *arg) |
The built-in opdis resolver callback. This callback returns the immediate value of the taregt operand for the instruction (if set) or OPDIS_INVALID_ADDR. | |
void | opdis_default_error_reporter (enum opdis_error_t error, const char *msg, void *arg) |
The built-in error reporter. | |
void LIBCALL | opdis_set_defaults (opdis_t o) |
Initializes an opdis object to default, sane values. | |
void LIBCALL | opdis_set_x86_syntax (opdis_t o, enum opdis_x86_syntax_t syntax) |
Configure the disassembler to use Intel or AT&T syntax. | |
void LIBCALL | opdis_set_arch (opdis_t o, enum bfd_architecture arch, unsigned long mach, disassembler_ftype fn) |
Set the architecture and disassembler routine for libopcodes. | |
void LIBCALL | opdis_set_disassembler_options (opdis_t o, const char *options) |
Set libopcodes disassembler options. | |
void LIBCALL | opdis_set_display (opdis_t o, OPDIS_DISPLAY fn, void *arg) |
Set the callback used to display or store disassembled instructions. | |
void LIBCALL | opdis_set_handler (opdis_t o, OPDIS_HANDLER fn, void *arg) |
Set the callback used to determine whether to continue disassembly. | |
void LIBCALL | opdis_set_decoder (opdis_t o, OPDIS_DECODER fn, void *arg) |
Set the callback used to build an opdis_insn_t from libopcodes data. | |
void LIBCALL | opdis_set_resolver (opdis_t o, OPDIS_RESOLVER fn, void *arg) |
Set the callback used to obtain the buffer offset of a branch target. | |
void LIBCALL | opdis_set_error_reporter (opdis_t o, OPDIS_ERROR fn, void *arg) |
Set the callback used to report errors. |
API for opdis configuration.
The disassembly functions rely on an opdis_t object to store configuration information such as libopcodes options and callbacks for use during disassembly. It is possible to override much of the default behavior of libopdis by configuring the opdis_t appropriately.
There are two types of configuration possible: disassembler configuration, which determines the settings used by libopcodes, and callback configuration, which determines the behavior of libopdis.
Disassembler configuration consists of the following functions:
There are five callbacks used by libopdis :
int(* OPDIS_DECODER)(const opdis_insn_buf_t, opdis_insn_t *, opdis_byte_t *, opdis_off_t, opdis_vma_t, opdis_off_t) |
Callback used to fill an opdis_insn_t from an opdis_insn_buf_t.
in | The opdis_insn_buf_t containing the libopcodes output. | |
out | Pointer to the opdis_insn_t to fill. | |
buf | Buffer containing the instruction | |
offset | Offset of start of instruction in buf. | |
vma | Address (vma) of start of instruction. | |
length | Size of instruction in bytes. | |
arg | Optional argument to pass to callback |
This function is invoked after libopcodes has finished disassembling the instruction. The strings emitted from libopcodes are in the opdis_insn_buf_t; the decoder must use these to build a valid opdis_insn_t. The default decoder for unsupported architectures will only fill the ascii field of the opdis_insn_t. More sophisticated decoders, such as the decoder for the x86 platform, will fill the rest of the object.
void(* OPDIS_DISPLAY)(const opdis_insn_t *i, void *arg) |
Callback used to display or store a disassembled instruction.
i | The most recently disassembled instruction. | |
arg | Argument provided when the callback is set |
This function is invoked every time an instruction is disassembled; it emits the instruction to the caller of the disassembler. The default display function writes to STDOUT.
void(* OPDIS_ERROR)(enum opdis_error_t error, const char *msg, void *arg) |
Callback used to handle error messages.
error | Type of error. | |
msg | Detailed message describing error. | |
arg | Argument provided when the callback is set. |
This function is invoked whenever an error is encountered by the disassembler. The default error handler writes to STDERR.
int(* OPDIS_HANDLER)(const opdis_insn_t *i, void *arg) |
Callback used to determine if disassembly should continue.
i | The most recently disassembled instruction. | |
arg | Argument provided when the callback is set |
This function is invoked after the display callback. The default behavior is to halt disassembly only if an invalid instruction is encountered. The caller can override this function in order to specify more detailed halting conditions, e.g. to halt once a sequence of instructions has been encountered.
opdis_vma_t(* OPDIS_RESOLVER)(const opdis_insn_t *i, void *arg) |
Callback used to convert a branch target to a buffer offset.
i | The instruction to resolve. | |
arg | Argument provided when the callback is set. |
This function is invoked when a branch instruction (call or jmp) is encountered; it determines the buffer offset of the branch target. This is used to convert relative addresses, virtual memory addresses, and registers into buffer offsets. If the branch target cannot be converted to a memory address (e.g. register contents are not being tracked, the load address of the buffer is not known, or the address lies outside the buffer) then this function must return OPDIS_INVALID_ADDR. The default resolver only handled relative addresses.
enum opdis_x86_syntax_t |
int opdis_default_decoder | ( | const opdis_insn_buf_t | in, | |
opdis_insn_t * | out, | |||
const opdis_byte_t * | buf, | |||
opdis_off_t | offset, | |||
opdis_vma_t | vma, | |||
opdis_off_t | length, | |||
void * | arg | |||
) |
The built-in opdis instruction decoder This callback fills the ascii, offset, vma, bytes, and size fields of the output instruction object. It is recommended that all other decoders invoke this callback directly to fill these fields.
void opdis_default_display | ( | const opdis_insn_t * | i, | |
void * | arg | |||
) |
The built-in opdis display callback This callback writes the instruction ascii field to STDOUT.
void opdis_default_error_reporter | ( | enum opdis_error_t | error, | |
const char * | msg, | |||
void * | arg | |||
) |
The built-in error reporter.
int opdis_default_handler | ( | const opdis_insn_t * | insn, | |
void * | arg | |||
) |
The built-in opdis handler callback. The default handler returns true unless the instruction is invalid or if the address has already been visited.
opdis_vma_t opdis_default_resolver | ( | const opdis_insn_t * | insn, | |
void * | arg | |||
) |
The built-in opdis resolver callback. This callback returns the immediate value of the taregt operand for the instruction (if set) or OPDIS_INVALID_ADDR.
opdis_set_arch | ( | opdis_t | o, | |
enum bfd_architecture | arch, | |||
unsigned long | mach, | |||
disassembler_ftype | fn | |||
) |
Set the architecture and disassembler routine for libopcodes.
This will set architecture of the libopcodes target to a BFD architecture, and specify the libopcodes print_insn routine that will be used for disassembly.
o | opdis disassembler to configure. | |
arch | A valid BFD architecture from /usr/include/bfd.h. | |
mach | A valid bfd_mach definition from /usr/include/bfd.h. | |
fn | A valid libopcodes print_insn routine from /usr/include/dis-asm.h . |
opdis_set_decoder | ( | opdis_t | o, | |
OPDIS_DECODER | fn, | |||
void * | arg | |||
) |
Set the callback used to build an opdis_insn_t from libopcodes data.
This sets the function used to fill an opdis_insn_t based on the array of strings (stored in an opdis_insn_buf_t) emitted by libopcodes. If the default handler does not support the ISA being disassembled (i.e. an architecture other than x86 is being used), then this must be overridden in order to get more than the ASCII version of the instruction in the opdis_insn_t.
o | opdis disassembler to configure. | |
fn | The callback function. | |
arg | An optional argument to pass to the callback function. |
opdis_set_defaults | ( | opdis_t | o | ) |
Initializes an opdis object to default, sane values.
o | opdis disassembler to configure. |
opdis_set_disassembler_options | ( | opdis_t | o, | |
const char * | options | |||
) |
Set libopcodes disassembler options.
Sets the libopcodes disassembler_options config field. Opdis passes this string blindly to libopcodes. To view the valid options for a disassembler, invoke the appropriate print_*_disassembler_options routine listed in /usr/include/dis-asm.h .
o | opdis disassembler to configure. | |
options | The options string for the disassembler. |
opdis_set_display | ( | opdis_t | o, | |
OPDIS_DISPLAY | fn, | |||
void * | arg | |||
) |
Set the callback used to display or store disassembled instructions.
This sets the function invoked to manage disassembled instructions. The callback is invoked every time an instruction is disassembled. It is expected to display or store the instruction, which will be discarded when the next address is disassembled. The default handler writes to STDOUT.
o | opdis disassembler to configure. | |
fn | The callback function. | |
arg | An optional argument to pass to the callback function. |
opdis_set_error_reporter | ( | opdis_t | o, | |
OPDIS_ERROR | fn, | |||
void * | arg | |||
) |
Set the callback used to report errors.
This sets the function used to report errors encountered during disassembly. The default error reporter simply writes to STDERR.
o | opdis disassembler to configure. | |
fn | The callback function. | |
arg | An optional argument to pass to the callback function. |
opdis_set_handler | ( | opdis_t | o, | |
OPDIS_HANDLER | fn, | |||
void * | arg | |||
) |
Set the callback used to determine whether to continue disassembly.
This sets the function invoked to determine whether to continue linear or control-flow disassembly after each instruction has been disassembled and displayed. The default handler will halt disassembly if an invalid instruction has been encountered, or (for control-flow) if the end of a control-flow branch (i.e. a return statement) has been reached.
o | opdis disassembler to configure. | |
fn | The callback function. | |
arg | An optional argument to pass to the callback function. |
opdis_set_resolver | ( | opdis_t | o, | |
OPDIS_RESOLVER | fn, | |||
void * | arg | |||
) |
Set the callback used to obtain the buffer offset of a branch target.
This sets the function used to convert the target of a branch to an offset into the buffer. The control flow disassembler will require a resolver in order to disassemble all non-relative branch targets.
o | opdis disassembler to configure. | |
fn | The callback function. | |
arg | An optional argument to pass to the callback function. |
opdis_set_x86_syntax | ( | opdis_t | o, | |
enum opdis_x86_syntax_t | syntax | |||
) |
Configure the disassembler to use Intel or AT&T syntax.
Sets the libopcodes print instruction to either print_insn_i386_intel or print_insn_i386_att.
o | opdis disassembler to configure. | |
syntax | The syntax option to use. |