fxos/include/fxos/view/assembly.h

116 lines
4.4 KiB
C++

//---------------------------------------------------------------------------//
// 1100101 |_ mov #0, r4 __ //
// 11 |_ <0xb380 %5c4> / _|_ _____ ___ //
// 0110 |_ 3.50 -> 3.60 | _\ \ / _ (_-< //
// |_ base# + offset |_| /_\_\___/__/ //
//---------------------------------------------------------------------------//
// fxos/view/assembly: Assembly code visualization
//
// This header provides a configurable assembly code pretty-printer. Options
// are provided to select between equivalent representations of arguments.
//
// Code is usually printed for a function. On-the-fly disassembly of non-
// functions is supported by either:
// - Building a function rooted at a specified address with a CFG analysis;
// - Building a function with a single block covering a specified interval.
//
// # Visualizing instructions
//
// The main mechanic is the _promotion_ of low-level information to a higher
// level. For instance, a @(disp,pc) argument can promote to its statically-
// determined taret, which can promote to its pointer value for a ROM read,
// which might itself be an object address or a syscall number.
//
// Each promotion thus has 3 settings:
// - Never: the higher-level information is not shown.
// - Append: the higher-level information is shown after the low-level one.
// - Promote: the higher-level information replaces the low-level one.
//
// For example, by default @(disp,pc) promotes to the target address, accessed
// value, and syscall number, but syscall names are set to Append. Hence, a
// mov.l @(disp,pc) loading the address of syscall %ace on FX (which is memcp)
// into r3 will show as "mov.l %ace memcmp, r3".
//
// # Visualizing blocks and functions
// TODO: CFG layout and rendering algorithms
//
// The rendering is split into three steps:
// 1. Generating text for each instruction or object
// 2. Grouping by basic block
// 3. Laying out basic blocks as a graph
//---
#ifndef FXOS_VIEW_ASSEMBLY_H
#define FXOS_VIEW_ASSEMBLY_H
#include <fxos/util/types.h>
#include <fxos/memory.h>
namespace FxOS {
class OS;
struct Binary;
struct Function;
struct BasicBlock;
struct Instruction;
struct StaticFunctionAnalysis;
struct ViewAssemblyOptions
{
enum Promotion : u8 {
Never, /* Never promote */
Append, /* Promote but keep the lower-level information */
Promote, /* Promote and hide the lower-level information */
};
struct
{
/* In jumps, promote "pc+<disp>" to the target address */
Promotion PCJump_to_Location = Promote;
/* In a PC-relative mov, promote "@(<disp>,pc)" to computed address */
Promotion PCRelative_to_Location = Promote;
/* In a mova, promote "pc+<disp>" to the computed address */
Promotion PCAddr_to_Location = Promote;
/* In a read with a know location, promote address to pointed value */
Promotion ReadLocation_to_Constant = Promote;
/* Promote an integer to a binary object's name */
Promotion Constant_to_ObjectName = Promote;
/* Promote an integer to a syscall number (if no name is available) */
Promotion Constant_to_SyscallNumber = Promote;
} promotions;
/* Binary to get symbols from */
Binary *binary = nullptr;
/* Whether to print basic block details, ie. edges and flags */
bool basicBlockDetails = false;
/* Whether to print function analysis results from the binary */
bool printFunctionAnalysis = false;
/* Whether to show details of instruction addresses and encodings */
bool showInstructionDetails = false;
/* TODO: More view assembly options, including CFG layout */
/* TODO: View assembly options: syntax highlighting */
};
void viewAssemblyInstruction(
Instruction const &inst, ViewAssemblyOptions *opts = nullptr);
void viewAssemblyBasicBlock(
BasicBlock const &bb, ViewAssemblyOptions *opts = nullptr);
void viewAssemblyFunction(
Function const &fun, ViewAssemblyOptions *opts = nullptr);
/* Legacy functions. Automatically set b as the binary in the options. */
void viewAssemblyLegacyRegion(
Binary &binary, MemoryRegion r, ViewAssemblyOptions *opts = nullptr);
void viewAssemblyLegacyAddress(
Binary &binary, u32 pc, ViewAssemblyOptions *opts = nullptr);
} /* namespace FxOS */
#endif /* FXOS_VIEW_ASSEMBLY_H */