fxos/include/fxos/symbols.h

97 lines
2.8 KiB
C++

//---------------------------------------------------------------------------//
// 1100101 |_ mov #0, r4 __ //
// 11 |_ <0xb380 %5c4> / _|_ _____ ___ //
// 0110 |_ 3.50 -> 3.60 | _\ \ / _ (_-< //
// |_ base# + offset |_| /_\_\___/__/ //
//---------------------------------------------------------------------------//
// fxos/symbols: User-extensible naming scheme for OS objects
//
// This header provides tools to define symbols, ie. names attached to fixed or
// symbolic addresses. Currently supported:
// - Address: the name maps to a fixed 32-bit virtual address.
// - Syscall: the name maps to a syscall number resolved by OS analysis.
//
// The SymbolTable structure manages a set of symbols, usually all the symbols
// in a given virtual space.
//---
#ifndef FXOS_SYMBOLS_H
#define FXOS_SYMBOLS_H
#include <optional>
#include <string>
#include <vector>
#include <cstdint>
namespace FxOS {
/* A named symbol that can be substituted to literal values in the code. */
struct Symbol
{
enum Type { Syscall = 1, Address = 2 };
enum Type type;
uint32_t value;
/* Symbol name, no particular conventions */
std::string name;
bool operator<(const FxOS::Symbol &right) const
{
return (type < right.type)
|| (value < right.value && type == right.type);
}
bool operator>(const FxOS::Symbol &right) const
{
return (type > right.type)
|| (value > right.value && type == right.type);
}
bool operator==(const FxOS::Symbol &right) const
{
return value == right.value && type == right.type;
}
bool operator!=(const FxOS::Symbol &right) const
{
return value != right.value || type != right.type;
}
bool operator>=(const FxOS::Symbol &right) const
{
return (type > right.type)
|| (value >= right.value && type == right.type);
}
bool operator<=(const FxOS::Symbol &right) const
{
return (type < right.type)
|| (value <= right.value && type == right.type);
}
};
/* A symbol table, usually the set of symbols of a virtual space */
struct SymbolTable
{
SymbolTable();
/* Allow move but disable copy */
SymbolTable(SymbolTable const &other) = delete;
SymbolTable(SymbolTable &&other) = default;
std::string table_name;
std::vector<Symbol> symbols;
/* Add a symbol to the table */
void add(Symbol s);
/* Query a value for a certain type of symbol */
std::optional<std::string> query(Symbol::Type type, uint32_t value) const;
/* Lookup the symbol behind a given name */
std::optional<Symbol> lookup(std::string name) const;
};
} /* namespace FxOS */
#endif /* FXOS_SYMBOLS_H */