From 5178a489bedc8bf8ed6453cc10a455250bdbc7f2 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Sun, 16 Feb 2020 21:43:58 +0100 Subject: [PATCH] add disassembly from arbitrary file Also changes the naked pointer of the Buffer object to a shared_ptr. Still learning how to use this thing, but I'll manage eventually. --- fxos/main.cpp | 48 ++++++++++++++++++++++++++----------------- include/fxos/target.h | 2 +- include/fxos/util.h | 13 ++---------- lib/load-asm.l | 2 +- lib/load-header.l | 2 +- lib/load-symbols.l | 2 +- lib/load-target.l | 2 +- lib/target.cpp | 2 +- lib/util.cpp | 48 ++++++++----------------------------------- 9 files changed, 46 insertions(+), 75 deletions(-) diff --git a/fxos/main.cpp b/fxos/main.cpp index bea92e9..9452d8e 100644 --- a/fxos/main.cpp +++ b/fxos/main.cpp @@ -328,6 +328,10 @@ int main_disassembly(int argc, char **argv) Library lib; loadconfig(lib); + Target target; + char const *refstr; + uint32_t ref; + if(!file.size()) { std::string tname = argv[optind + 1]; @@ -338,32 +342,38 @@ int main_disassembly(int argc, char **argv) return 1; } - Target target(lib.targets().at(tname), lib.paths()); - - char const *refstr = argv[optind + 2]; - uint32_t ref; - sscanf(refstr, "%x", &ref); + target = Target(lib.targets().at(tname), lib.paths()); + refstr = argv[optind + 2]; log(LOG "Disassembling target %s at %s", tname, refstr); - try - { - disassembly(lib, target, ref, passes); - } - catch(LangError &e) - { - log(ERR "%08x: %s", e.addr(), e.what()); - } - catch(AddrError &e) - { - log(ERR "%08x[%d]: %s", e.addr(), e.size(), e.what()); - } } else { - char const *ref = argv[optind + 1]; + /* Load the file in ROM over 8M */ + Buffer romfile(file, MemoryRegion::ROM.size()); + target = Target(); + target.bind_region(MemoryRegion::ROM, romfile); + target.bind_region(MemoryRegion::ROM_P2, romfile); - log(LOG "Disassembling file %s at %s", file, ref); + refstr = argv[optind + 1]; + log(LOG "Disassembling file %s at %s", file, refstr); + } + + sscanf(refstr, "%x", &ref); + try + { + disassembly(lib, target, ref, passes); + } + catch(LangError &e) + { + log(ERR "%08x: %s", e.addr(), e.what()); + return 1; + } + catch(AddrError &e) + { + log(ERR "%08x[%d]: %s", e.addr(), e.size(), e.what()); + return 1; } return 0; diff --git a/include/fxos/target.h b/include/fxos/target.h index 05c77a7..1184510 100644 --- a/include/fxos/target.h +++ b/include/fxos/target.h @@ -71,7 +71,7 @@ struct Binding: public AbstractMemory /* Targeted region, might overlap with other bindings */ MemoryRegion region; /* Actual data. This area must have at least [size] bytes */ - void const *data; + std::shared_ptr data; /* Binding size (equal to the region size) */ uint32_t size; diff --git a/include/fxos/util.h b/include/fxos/util.h index e0d7f6c..6aaeb44 100644 --- a/include/fxos/util.h +++ b/include/fxos/util.h @@ -79,11 +79,6 @@ public: /* Empty buffer initialized with given byte */ Buffer(size_t size, int fill=0x00); - /* Object is movable and move assignable but not copyable */ - Buffer(Buffer const &other) = delete; - Buffer(Buffer &&other); - Buffer & operator=(Buffer &&other); - /* Buffer initialized from file, reading the given size from the beginning of the file. If the file is smaller than the specified size, the buffer is padded. @@ -96,17 +91,13 @@ public: Buffer(std::string filepath, std::vector &folders, ssize_t size=-1, int fill=0x00); - /* Create a buffer by copying and resizing another buffer (we want no - copy constructor so the size must be explicit) */ + /* Create a buffer by copying and resizing another buffer */ Buffer(Buffer const &other, size_t new_size, int fill=0x00); - /* Free allocated data, obviously */ - ~Buffer(); - /* Buffer size */ size_t size; /* Data */ - char *data; + std::shared_ptr data; /* File path, when constructed from file */ std::string path; }; diff --git a/lib/load-asm.l b/lib/load-asm.l index b7327fe..8fef585 100644 --- a/lib/load-asm.l +++ b/lib/load-asm.l @@ -273,7 +273,7 @@ int load_asm(Buffer const &file, size_t start_offset, size_t start_line) { /* Lex all instructions and fill in the general assembly table */ - YY_BUFFER_STATE buf = yy_scan_bytes(file.data + start_offset, + YY_BUFFER_STATE buf = yy_scan_bytes(file.data.get() + start_offset, file.size - start_offset); yylineno = start_line; filename = file.path; diff --git a/lib/load-header.l b/lib/load-header.l index 7337dfb..de0b971 100644 --- a/lib/load-header.l +++ b/lib/load-header.l @@ -69,7 +69,7 @@ Header load_header(Buffer const &file, size_t &offset_ref, int &line_ref) /* Build a map of properties */ FxOS::Header header; - YY_BUFFER_STATE buf = yy_scan_bytes(file.data, file.size); + YY_BUFFER_STATE buf = yy_scan_bytes(file.data.get(), file.size); filename = file.path; yylineno = 1; lexed = 0; diff --git a/lib/load-symbols.l b/lib/load-symbols.l index 315d7c8..4d17131 100644 --- a/lib/load-symbols.l +++ b/lib/load-symbols.l @@ -63,7 +63,7 @@ namespace FxOS { SymbolTable load_symbols(Buffer const &file, size_t start_offset, size_t start_line) { - YY_BUFFER_STATE buf = yy_scan_bytes(file.data + start_offset, + YY_BUFFER_STATE buf = yy_scan_bytes(file.data.get() + start_offset, file.size - start_offset); yylineno = start_line; filename = file.path; diff --git a/lib/load-target.l b/lib/load-target.l index 7991fde..b6417b5 100644 --- a/lib/load-target.l +++ b/lib/load-target.l @@ -110,7 +110,7 @@ TargetDescription load_target(Buffer const &file, size_t offset, size_t line) /* Build a target description without actually loading the files. */ TargetDescription descr; - YY_BUFFER_STATE buf = yy_scan_bytes(file.data + offset, + YY_BUFFER_STATE buf = yy_scan_bytes(file.data.get() + offset, file.size - offset); yylineno = line; filename = file.path; diff --git a/lib/target.cpp b/lib/target.cpp index 3156578..ecd8af9 100644 --- a/lib/target.cpp +++ b/lib/target.cpp @@ -91,7 +91,7 @@ char const *Binding::translate(uint32_t addr, int size) const throw std::out_of_range("Out of binding range"); } - return (char *)data + (addr - region.start); + return data.get() + (addr - region.start); } uint32_t Binding::search(uint32_t start, uint32_t end, void const *pattern, diff --git a/lib/util.cpp b/lib/util.cpp index 83d71fd..ca311a6 100644 --- a/lib/util.cpp +++ b/lib/util.cpp @@ -19,39 +19,14 @@ Buffer::Buffer(): Buffer::Buffer(size_t bufsize, int fill) { size = bufsize; - data = static_cast(malloc(size)); - if(!data) throw std::bad_alloc(); + char *buffer = static_cast(malloc(size)); + if(!buffer) throw std::bad_alloc(); + memset(buffer, fill, size); - memset(data, fill, size); + data = std::shared_ptr(buffer); path = "(anonymous)"; } -/* Move constructor */ -Buffer::Buffer(Buffer &&other): - size {0}, data {nullptr}, path {} -{ - *this = std::move(other); -} - -/* Move assignment operator */ -Buffer &Buffer::operator=(Buffer &&other) -{ - if(this != &other) - { - free(data); - - data = other.data; - size = other.size; - path = other.path; - - other.data = nullptr; - other.size = 0; - other.path = "(moved)"; - } - - return *this; -} - /* Buffer initialized from file */ Buffer::Buffer(std::string filepath, ssize_t bufsize, int fill) { @@ -72,12 +47,13 @@ Buffer::Buffer(std::string filepath, ssize_t bufsize, int fill) size = (bufsize < 0) ? statbuf.st_size : bufsize; size_t size_to_read = std::min(size, (size_t)statbuf.st_size); - data = static_cast(malloc(size)); + char *buffer = static_cast(malloc(size)); + data = std::shared_ptr(buffer); if(!data) throw std::bad_alloc(); /* Read buffer and fill whatever is left */ - memset(data, fill, size); - x = read(fd, data, size_to_read); + memset(buffer, fill, size); + x = read(fd, buffer, size_to_read); close(fd); if(x != (ssize_t)size_to_read) @@ -112,13 +88,7 @@ Buffer::Buffer(std::string filepath, std::vector &folders, Buffer::Buffer(Buffer const &other, size_t new_size, int fill): Buffer(new_size, fill) { - memcpy(data, other.data, std::min(new_size, other.size)); -} - -/* Free allocated data, obviously */ -Buffer::~Buffer() -{ - free(data); + memcpy(data.get(), other.data.get(), std::min(new_size, other.size)); } /* Generic timer which returns times in ns using CLOCK_REALTIME */