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.
This commit is contained in:
Lephenixnoir 2020-02-16 21:43:58 +01:00
parent 6ca3bc1f03
commit 5178a489be
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
9 changed files with 46 additions and 75 deletions

View File

@ -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;

View File

@ -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<char> data;
/* Binding size (equal to the region size) */
uint32_t size;

View File

@ -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<std::string> &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<char> data;
/* File path, when constructed from file */
std::string path;
};

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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,

View File

@ -19,39 +19,14 @@ Buffer::Buffer():
Buffer::Buffer(size_t bufsize, int fill)
{
size = bufsize;
data = static_cast<char *>(malloc(size));
if(!data) throw std::bad_alloc();
char *buffer = static_cast<char *>(malloc(size));
if(!buffer) throw std::bad_alloc();
memset(buffer, fill, size);
memset(data, fill, size);
data = std::shared_ptr<char>(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<char *>(malloc(size));
char *buffer = static_cast<char *>(malloc(size));
data = std::shared_ptr<char>(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<std::string> &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 */