diff options
| author | Xavier Del Campo Romero <xavi92@disroot.org> | 2025-11-02 18:21:49 +0100 |
|---|---|---|
| committer | Xavier Del Campo Romero <xavi92@disroot.org> | 2025-11-12 00:47:10 +0100 |
| commit | b16e2f67e7d392890c6835f98ca9b2a7bb44fe2e (patch) | |
| tree | cf07afb610395dd182e1f243ffccf2a55a13effe /wasm.h | |
Diffstat (limited to 'wasm.h')
| -rw-r--r-- | wasm.h | 184 |
1 files changed, 184 insertions, 0 deletions
@@ -0,0 +1,184 @@ +#ifndef WASM_H +#define WASM_H + +#include "instr.h" +#include <libdwarf/libdwarf.h> +#include <QHash> +#include <QObject> +#include <QString> +#include <QVariant> +#include <QVector> +#include <cerrno> +#include <cstdio> +#include <cstring> +#include "types.h" + +enum class WasmType +{ + i32, + i64, + f32, + f64 +}; + +struct WasmGlobal +{ + WasmType type; + bool mutability; + bool operator==(const WasmGlobal &other) const + { + return type == other.type + && mutability == other.mutability; + } +}; + +struct WasmLocal +{ + WasmType type; + bool operator==(const WasmLocal &other) const + { + return type == other.type; + } +}; + +struct WasmParam +{ + QString name, dwtype; + WasmType type; + bool operator==(const WasmParam &other) const + { + return type == other.type + && name == other.name + && dwtype == other.dwtype; + } +}; + +struct WasmRoutine +{ + QString name; + quint32 lopc, hipc; + QVector<WasmParam> params; + QVector<WasmLocal> locals; + QVector<WasmInstr> instructions; + bool operator==(const WasmRoutine &other) const + { + return name == other.name + && lopc == other.lopc + && hipc == other.hipc + && params == other.params + && locals == other.locals + && instructions == other.instructions; + } +}; + +Q_DECLARE_METATYPE(WasmGlobal); +Q_DECLARE_METATYPE(WasmLocal); +Q_DECLARE_METATYPE(WasmParam); +Q_DECLARE_METATYPE(WasmRoutine); + +class Wasm +{ +public: + Wasm(); + ~Wasm(); + int open(const QString &path, QString &error); + const QVector<WasmGlobal> globals() const; + QVariant routine(const QString &name) const; + QVariant routine(quint32 addr) const; + +private: + + struct section + { + varuint7 code; + varuint32 len; + QString name; + }; + + struct dwsection + { + quint32 offset; + varuint32 len; + QVector<Dwarf_Small> data; + dwsection() : offset(0), len(0) {} + }; + + struct header + { + Dwarf_Sig8 type_signature; + Dwarf_Unsigned cu_header_length, typeoffset, next_cu_header_offset; + Dwarf_Half version_stamp, address_size, length_size, extension_size, + header_cu_type; + Dwarf_Off abbrev_offset; + }; + + struct attr + { + QString name; + QVariant value; + }; + + enum + { + DEBUG_EMPTY, + DEBUG_ABBREV, + DEBUG_INFO, + DEBUG_STR, + DEBUG_ARANGES, + DEBUG_FRAME, + DEBUG_LINE, + DEBUG_LOC, + DEBUG_MACINFO, + DEBUG_PUBNAMES, + DEBUG_PUBTYPES, + DEBUG_RANGES, + DEBUG_TYPES, + + NSECTIONS + }; + + int parse(QString &error); + int parse_global(QString &error); + int parse_import(QString &error); + int parse_code(QString &error); + int parse_body(varuint32 index, QString &error); + int parse_name(QString &error, varuint32 len); + int parse_fname(QString &error, varuint32 maxlen); + int get_type(value_type type, WasmType &wtype) const; + int check_header(QString &error) const; + int read_section(QString &error); + int parse_header(section &s, QString &error) const; + int read_name(QString &name, QString &error) const; + dwsection *getdws(const QString &name); + int skip(const section &s, QString &error) const; + int read_dwarf(const Dwarf_Debug dbg, + header &h, Dwarf_Error &e, QString &error); + int read_child(Dwarf_Debug dbg, + Dwarf_Die die, Dwarf_Error &e, QString &error); + int process_die(Dwarf_Debug dbg, + Dwarf_Die die, Dwarf_Error &e, QString &error); + int read_attr(Dwarf_Attribute a, Dwarf_Error &e, QString &error, + attr &attr); + + static bool isformstring(Dwarf_Half form); + static bool isformaddr(Dwarf_Half form); + static bool isformudata(Dwarf_Half form); + static bool isformsdata(Dwarf_Half form); + + static int load_section(void *const obj, const Dwarf_Half section_index, + Dwarf_Small **const return_data, int *const error); + static int get_section_info(void *const obj, const Dwarf_Half section_index, + Dwarf_Obj_Access_Section *const return_section, int *const error); + static Dwarf_Unsigned get_section_count(void *const obj); + + FILE *f; + long code_offset; + varuint32 import_fncnt; + dwsection sections[NSECTIONS]; + QVariant lastwr; + QHash<varuint32, WasmRoutine> fmap; + QVector<WasmGlobal> vglobals; + static const char *const names[NSECTIONS]; +}; + +#endif |
