Skip to content

Commit

Permalink
Add "x86 disassembly syntax" selection to View menu (#142).
Browse files Browse the repository at this point in the history
  • Loading branch information
solemnwarning committed Jan 13, 2022
1 parent 25fae16 commit dc0bc6d
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Version TBA

* Added "x86 disassembly syntax" to "View" menu to allow selecting between
Intel or AT&T notation for x86 disassembly.

Version 0.4.1 (2022-01-03)

* Fixed font-dependent rendering glitches when control characters or other
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Reverse Engineer's Hex Editor
# Copyright (C) 2017-2021 Daniel Collins <solemnwarning@solemnwarning.net>
# Copyright (C) 2017-2022 Daniel Collins <solemnwarning@solemnwarning.net>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published by
Expand Down Expand Up @@ -214,6 +214,7 @@ APP_OBJS := \
res/spinner24.o \
src/AboutDialog.o \
src/AppMain.o \
src/AppSettings.o \
src/AppTestable.o \
src/ArtProvider.o \
src/BasicDataTypes.o \
Expand Down Expand Up @@ -282,6 +283,7 @@ TEST_OBJS := \
res/offsets48.o \
res/spinner24.o \
src/AboutDialog.o \
src/AppSettings.o \
src/AppTestable.o \
src/ArtProvider.o \
src/BasicDataTypes.o \
Expand Down
5 changes: 4 additions & 1 deletion src/App.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Reverse Engineer's Hex Editor
* Copyright (C) 2017-2021 Daniel Collins <solemnwarning@solemnwarning.net>
* Copyright (C) 2017-2022 Daniel Collins <solemnwarning@solemnwarning.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
Expand All @@ -18,6 +18,7 @@
#ifndef REHEX_APP_HPP
#define REHEX_APP_HPP

#include "AppSettings.hpp"
#include "ConsoleBuffer.hpp"

#include <functional>
Expand All @@ -34,6 +35,8 @@ namespace REHex {
{
public:
wxConfig *config;
AppSettings *settings;

wxFileHistory *recent_files;
wxLocale *locale;

Expand Down
7 changes: 5 additions & 2 deletions src/AppMain.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Reverse Engineer's Hex Editor
* Copyright (C) 2017-2021 Daniel Collins <solemnwarning@solemnwarning.net>
* Copyright (C) 2017-2022 Daniel Collins <solemnwarning@solemnwarning.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
Expand Down Expand Up @@ -46,8 +46,10 @@ bool REHex::App::OnInit()
ArtProvider::init();

config = new wxConfig("REHex");

config->SetPath("/");

settings = new AppSettings(config);

last_directory = config->Read("last-directory", "");
font_size_adjustment = config->ReadLong("font-size-adjustment", 0);

Expand Down Expand Up @@ -138,6 +140,7 @@ int REHex::App::OnExit()

delete active_palette;
delete recent_files;
delete settings;
delete config;

#ifdef _WIN32
Expand Down
61 changes: 61 additions & 0 deletions src/AppSettings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* Reverse Engineer's Hex Editor
* Copyright (C) 2022 Daniel Collins <solemnwarning@solemnwarning.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#include "AppSettings.hpp"

wxDEFINE_EVENT(REHex::PREFERRED_ASM_SYNTAX_CHANGED, wxCommandEvent);

REHex::AppSettings::AppSettings():
preferred_asm_syntax(AsmSyntax::INTEL) {}

REHex::AppSettings::AppSettings(wxConfig *config): AppSettings()
{
long preferred_asm_syntax = config->ReadLong("preferred-asm-syntax", -1);
switch(preferred_asm_syntax)
{
case (long)(AsmSyntax::INTEL):
case (long)(AsmSyntax::ATT):
this->preferred_asm_syntax = (AsmSyntax)(preferred_asm_syntax);
break;

default:
break;
}
}

void REHex::AppSettings::write(wxConfig *config)
{
config->Write("preferred-asm-syntax", (long)(preferred_asm_syntax));
}

REHex::AsmSyntax REHex::AppSettings::get_preferred_asm_syntax() const
{
return preferred_asm_syntax;
}

void REHex::AppSettings::set_preferred_asm_syntax(AsmSyntax preferred_asm_syntax)
{
if(this->preferred_asm_syntax != preferred_asm_syntax)
{
this->preferred_asm_syntax = preferred_asm_syntax;

wxCommandEvent event(PREFERRED_ASM_SYNTAX_CHANGED);
event.SetEventObject(this);

wxPostEvent(this, event);
}
}
50 changes: 50 additions & 0 deletions src/AppSettings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* Reverse Engineer's Hex Editor
* Copyright (C) 2022 Daniel Collins <solemnwarning@solemnwarning.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

#ifndef REHEX_APPSETTINGS_HPP
#define REHEX_APPSETTINGS_HPP

#include <wx/config.h>
#include <wx/wx.h>

namespace REHex
{
enum class AsmSyntax
{
INTEL = 1,
ATT = 2,
};

class AppSettings: public wxEvtHandler
{
public:
AppSettings();
AppSettings(wxConfig *config);

void write(wxConfig *config);

AsmSyntax get_preferred_asm_syntax() const;
void set_preferred_asm_syntax(AsmSyntax preferred_asm_syntax);

private:
AsmSyntax preferred_asm_syntax;
};

wxDECLARE_EVENT(PREFERRED_ASM_SYNTAX_CHANGED, wxCommandEvent);
}

#endif /* !REHEX_APPSETTINGS_HPP */
35 changes: 33 additions & 2 deletions src/DisassemblyRegion.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Reverse Engineer's Hex Editor
* Copyright (C) 2020-2021 Daniel Collins <solemnwarning@solemnwarning.net>
* Copyright (C) 2020-2022 Daniel Collins <solemnwarning@solemnwarning.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
Expand All @@ -25,6 +25,7 @@
#include <tuple>
#include <vector>

#include "App.hpp"
#include "DisassemblyRegion.hpp"
#include "Events.hpp"
#include "util.hpp"
Expand All @@ -35,7 +36,9 @@ static const size_t INSTRUCTION_CACHE_LIMIT = 250000;
REHex::DisassemblyRegion::DisassemblyRegion(SharedDocumentPointer &doc, off_t offset, off_t length, off_t virt_offset, cs_arch arch, cs_mode mode):
GenericDataRegion(offset, length, virt_offset),
doc(doc),
virt_offset(virt_offset)
virt_offset(virt_offset),
arch(arch),
preferred_asm_syntax(AsmSyntax::INTEL)
{
cs_err error = cs_open(arch, mode, &disassembler);
if(error != CS_ERR_OK)
Expand Down Expand Up @@ -138,6 +141,34 @@ void REHex::DisassemblyRegion::calc_height(DocumentCtrl &doc_ctrl, wxDC &dc)

void REHex::DisassemblyRegion::draw(DocumentCtrl &doc_ctrl, wxDC &dc, int x, int64_t y)
{
if(arch == CS_ARCH_X86)
{
AsmSyntax new_preferred_asm_syntax = wxGetApp().settings->get_preferred_asm_syntax();
if(preferred_asm_syntax != new_preferred_asm_syntax)
{
preferred_asm_syntax = new_preferred_asm_syntax;

switch(preferred_asm_syntax)
{
case AsmSyntax::INTEL:
cs_option(disassembler, CS_OPT_SYNTAX, CS_OPT_SYNTAX_INTEL);
break;

case AsmSyntax::ATT:
cs_option(disassembler, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
break;
}

longest_disasm = 0;
longest_instruction = 0;

dirty.set_range(d_offset, d_length);

processed.clear();
instructions.clear();
}
}

draw_container(doc_ctrl, dc, x, y);

int hf_char_height = doc_ctrl.hf_char_height();
Expand Down
6 changes: 5 additions & 1 deletion src/DisassemblyRegion.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Reverse Engineer's Hex Editor
* Copyright (C) 2020 Daniel Collins <solemnwarning@solemnwarning.net>
* Copyright (C) 2020-2022 Daniel Collins <solemnwarning@solemnwarning.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
Expand All @@ -26,6 +26,7 @@
#include <vector>
#include <wx/wx.h>

#include "AppSettings.hpp"
#include "ByteRangeSet.hpp"
#include "DocumentCtrl.hpp"
#include "Events.hpp"
Expand Down Expand Up @@ -62,6 +63,7 @@ namespace REHex
SharedDocumentPointer doc;
off_t virt_offset;

cs_arch arch;
size_t disassembler;

int offset_text_x; /**< X co-ordinate of left edge of offsets. */
Expand All @@ -76,6 +78,8 @@ namespace REHex
off_t longest_instruction;
size_t longest_disasm;

AsmSyntax preferred_asm_syntax;

void disasm_instruction(const uint8_t **code, size_t *size, uint64_t *address, cs_insn *insn);

void OnDataOverwrite(OffsetLengthEvent &event);
Expand Down
31 changes: 30 additions & 1 deletion src/disassemble.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Reverse Engineer's Hex Editor
* Copyright (C) 2018-2021 Daniel Collins <solemnwarning@solemnwarning.net>
* Copyright (C) 2018-2022 Daniel Collins <solemnwarning@solemnwarning.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
Expand Down Expand Up @@ -174,6 +174,8 @@ REHex::Disassemble::Disassemble(wxWindow *parent, SharedDocumentPointer &documen

this->document_ctrl.auto_cleanup_bind(EV_DISP_SETTING_CHANGED, &REHex::Disassemble::OnBaseChanged, this);

wxGetApp().settings->Bind(PREFERRED_ASM_SYNTAX_CHANGED, &REHex::Disassemble::OnAsmSyntaxChanged, this);

reinit_disassembler();
update();
}
Expand Down Expand Up @@ -336,6 +338,22 @@ void REHex::Disassemble::reinit_disassembler()
/* TODO: Report error */
return;
}

if(desc.arch == CS_ARCH_X86)
{
AsmSyntax preferred_asm_syntax = wxGetApp().settings->get_preferred_asm_syntax();

switch(preferred_asm_syntax)
{
case AsmSyntax::INTEL:
cs_option(disassembler, CS_OPT_SYNTAX, CS_OPT_SYNTAX_INTEL);
break;

case AsmSyntax::ATT:
cs_option(disassembler, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
break;
}
}
}

std::map<off_t, REHex::Disassemble::Instruction> REHex::Disassemble::disassemble(off_t offset, const void *code, size_t size)
Expand Down Expand Up @@ -393,3 +411,14 @@ void REHex::Disassemble::OnBaseChanged(wxCommandEvent &event)
/* Continue propogation. */
event.Skip();
}

void REHex::Disassemble::OnAsmSyntaxChanged(wxCommandEvent &event)
{
const CSArchitecture& arch_desc = arch_list[ arch->GetSelection() ];

if(arch_desc.arch == CS_ARCH_X86)
{
reinit_disassembler();
update();
}
}
3 changes: 2 additions & 1 deletion src/disassemble.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Reverse Engineer's Hex Editor
* Copyright (C) 2018 Daniel Collins <solemnwarning@solemnwarning.net>
* Copyright (C) 2018-2022 Daniel Collins <solemnwarning@solemnwarning.net>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published by
Expand Down Expand Up @@ -71,6 +71,7 @@ namespace REHex {
void OnArch(wxCommandEvent &event);
void OnDataModified(OffsetLengthEvent &event);
void OnBaseChanged(wxCommandEvent &event);
void OnAsmSyntaxChanged(wxCommandEvent &event);

/* Stays at the bottom because it changes the protection... */
DECLARE_EVENT_TABLE()
Expand Down

0 comments on commit dc0bc6d

Please sign in to comment.