Skip to content

Commit

Permalink
disasm: Introduced Error class, switched some error handling to excep…
Browse files Browse the repository at this point in the history
…tions

Currently exceptions are propagated properly only near main().
It's a start.
  • Loading branch information
mefistotelis committed Jan 10, 2024
1 parent 6052d55 commit 14d1c35
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ le_disasm_SOURCES = \
analyser.cpp \
disassembler.hpp \
disassembler.cpp \
error.hpp \
instruction.hpp \
instruction.cpp \
image.hpp \
Expand Down
56 changes: 56 additions & 0 deletions src/error.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* le_disasm - Linear Executable disassembler
*/
/** @file error.hpp
* Declaration of Error, an exception class with stream operators support.
* @par Purpose:
* Contains the full declaration and implementation of the Error class,
* with ability of constructing error message by stream operators.
* @author Mark Nelson <marknelson.us>
* @date 2010-09-20 - 2024-01-10
* @par Copying and copyrights:
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#ifndef LEDISASM_ERROR_H
#define LEDISASM_ERROR_H

#include <sstream>
#include <stdexcept>
#include <string>

// http://marknelson.us/2007/11/13/no-exceptions/
// throw Error() <<"Game over, "
// <<mHealth
// <<" health points!";
struct Error : public std::exception {
Error() {
}

Error(const Error &that) {
mWhat += that.mStream.str();
}

virtual ~Error() throw() {};

virtual const char *what() const throw () {
if (mStream.str().size()) {
mWhat += mStream.str();
mStream.str("");
}
return mWhat.c_str();
}

template<typename T>
Error& operator<<(const T& t) {
mStream << t;
return *this;
}
private:
mutable std::stringstream mStream;
mutable std::string mWhat;
};

#endif // LEDISASM_ERROR_H
37 changes: 15 additions & 22 deletions src/le.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
*/
#include <iostream>
#include <iomanip>
#include <memory>

#include "le.hpp"
#include "error.hpp"
#include "util.hpp"

using std::cerr;
Expand Down Expand Up @@ -53,7 +55,8 @@ read_u8 (istream *is, uint8_t *ret)
class LinearExecutable::Loader
{
protected:
LinearExecutable *le;
std::unique_ptr<LinearExecutable> le;
//LinearExecutable *le;
std::istream *is;
uint32_t header_offset;
vector<uint32_t> fixup_record_offsets;
Expand Down Expand Up @@ -81,16 +84,14 @@ LinearExecutable::Loader::load (istream *is, const std::string &name)

if (!this->is->good ())
{
cerr << "Failed to open " << name << ".\n";
return NULL;
throw Error() << "Failed to open \"" << name << "\".";
}

this->le = new LinearExecutable;
this->le = std::unique_ptr<LinearExecutable>(new LinearExecutable);

if (!this->load_header ())
{
cerr << "Failed to load LE header.\n";
goto err;
throw Error() << "Failed to load LE header.";
}

#ifdef DEBUG
Expand All @@ -100,8 +101,7 @@ LinearExecutable::Loader::load (istream *is, const std::string &name)

if (!this->load_object_table ())
{
cerr << "Failed to load object table.\n";
goto err;
throw Error() << "Failed to load object table.";
}

#ifdef DEBUG
Expand All @@ -122,8 +122,7 @@ LinearExecutable::Loader::load (istream *is, const std::string &name)

if (!this->load_object_page_table ())
{
cerr << "Failed to load object page table.\n";
goto err;
throw Error() << "Failed to load object page table.";
}

#if 0
Expand All @@ -138,21 +137,15 @@ LinearExecutable::Loader::load (istream *is, const std::string &name)

if (!this->load_fixup_record_offsets ())
{
cerr << "Failed to load fixup page table.\n";
goto err;
throw Error() << "Failed to load fixup page table.";
}

if (!this->load_fixup_record_table ())
{
cerr << "Failed to load fixup table.\n";
goto err;
throw Error() << "Failed to load fixup table.";
}

return this->le;

err:
delete this->le;
return NULL;
return this->le.release();
}

bool
Expand All @@ -161,7 +154,7 @@ LinearExecutable::Loader::load_le_header_offset(void)
char id[2];
uint8_t byte;
istream *is = this->is;
LinearExecutable *le = this->le;
LinearExecutable *le = this->le.get();

is->seekg (0);
is->read (id, 2);
Expand All @@ -187,7 +180,7 @@ LinearExecutable::Loader::load_le_header_offset(void)

if (byte < 0x40)
{
cerr << "Not a LE executable, at offset 0x18: expected 0x40 or more, got 0x" << std::hex << byte << std::endl;
cerr << "Not a LE executable, at offset 0x18: expected 0x40 or more, got 0x" << std::hex << (int)byte << std::endl;
return false;
}

Expand All @@ -204,7 +197,7 @@ LinearExecutable::Loader::load_header (void)
char id[2];
uint8_t byte;
istream *is = this->is;
LinearExecutable *le = this->le;
LinearExecutable *le = this->le.get();

is->seekg (0);
is->read (id, 2);
Expand Down
69 changes: 40 additions & 29 deletions src/le_disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
#include <cassert>
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>

#include "analyser.hpp"
#include "error.hpp"
#include "image.hpp"
#include "instruction.hpp"
#include "known_file.hpp"
Expand Down Expand Up @@ -564,50 +566,59 @@ debug_print_regions (Analyser *anal)
for (itr = map->begin (); itr != map->end (); ++itr)
std::cout << itr->second << "\n";
}
int
main (int argc, char **argv)

void
main_execute(const char *options_fname)
{
LinearExecutable *le;
Image *image;
std::unique_ptr<LinearExecutable> le;
std::unique_ptr<Image> image;
std::ifstream ifs;
Analyser anal;

if (argc < 2)
ifs.open (options_fname, std::ios::binary);
if(!ifs.is_open())
{
std::cerr << "Usage: " << argv[0] << " [main.exe]\n";
return 1;
throw Error() << "Error opening file: " << options_fname;
}

try {

ifs.open (argv[1], std::ios::binary);
if(!ifs.is_open())
{
std::cerr << "Error opening file: " << argv[1];
return 1;
}
le = std::unique_ptr<LinearExecutable>(
LinearExecutable::load (&ifs, options_fname)
);

le = LinearExecutable::load (&ifs, argv[1]);
image = std::unique_ptr<Image>(
create_image (&ifs, le.get())
);

image = create_image (&ifs, le);
anal = Analyser (le.get(), image.get());

anal = Analyser (le, image);
KnownFile::check(anal, le.get());
KnownFile::pre_anal_fixups_apply(anal);

KnownFile::check(anal, le);
KnownFile::pre_anal_fixups_apply(anal);
anal.run ();

anal.run ();
KnownFile::post_anal_fixups_apply(anal);

KnownFile::post_anal_fixups_apply(anal);
print_code (le.get(), image.get(), &anal);
}

print_code (le, image, &anal);
int
main (int argc, char **argv)
{
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " [main.exe]\n";
return 1;
}

} catch (const std::exception &e) {
std::cerr << std::dec << e.what() << std::endl;
}
try
{
main_execute(argv[1]);
}
catch (const std::exception &e)
{
std::cerr << std::dec << e.what() << std::endl;
return 1;
}

delete image;
delete le;
return 0;
}

0 comments on commit 14d1c35

Please sign in to comment.