Skip to content

Commit

Permalink
chore: Update build files and dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
suifei committed May 27, 2024
1 parent 7bd345f commit a34b253
Show file tree
Hide file tree
Showing 14 changed files with 1,004 additions and 440 deletions.
2 changes: 1 addition & 1 deletion FyneApp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Website = "https://github.com/suifei/asm2hex"
Name = "ASM to HEX Converter"
ID = "suifei.asm2hex.app"
Version = "1.1.0"
Build = 53
Build = 54
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,7 @@ clean:
rm -rf $(INSTALL_PREFIX)/include/keystone && \
rm -f $(INSTALL_PREFIX)/lib/libcapstone* && \
rm -f $(INSTALL_PREFIX)/lib/libkeystone* && \
echo "Cleaned up temporary files" && \
echo "Removed Capstone and Keystone libraries" && \
echo "Removed build files" && \
go clean -cache && \
echo "Cleaned up successfully"

lib:
Expand Down
59 changes: 59 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,65 @@

ASM2HEX是一款用Go语言编写的,基于Fyne框架开发的汇编语言与十六进制机器码相互转换的图形化工具。它支持ARM64、ARM和Thumb三种指令集。

## 支持的汇编指令集

下表列出了当前版本支持的汇编指令集以及对应的汇编器(Keystone)和反汇编器(Capstone)的支持情况:

| 指令集 | 架构 | 汇编(Keystone) | 反汇编(Capstone) |
|------------|------------|----------------|-----------------|
| ARM | ARM |||
| ARM64 | ARM64 |||
| MIPS | MIPS |||
| X86 | X86 |||
| PPC | PPC |||
| SPARC | SPARC |||
| SystemZ | SYSTEMZ |||
| Hexagon | HEXAGON |||
| EVM | EVM |||
| XCORE | XCORE |||
| M68K | M68K |||
| TMS320C64X | TMS320C64X |||
| M680X | M680X |||
| MOS65XX | MOS65XX |||
| WASM | WASM |||
| BPF | BPF |||
| RISCV | RISCV |||
| SH | SH |||
| TriCore | TRICORE |||

✓ 表示支持该指令集,✗ 表示不支持该指令集。


## v1.2.0 版本更新说明

### 新增功能

- 增加了对多种指令集,架构汇编、反汇编的支持,现在可以在主界面上选择。
- 支持多种汇编指令集,包括 ARM、ARM64、MIPS、X86、PPC、SPARC、SystemZ、Hexagon 和 EVM。
- 提供了统一的接口,可以方便地进行汇编和反汇编操作。


![](screenshots/v1.2-01.png)

![](screenshots/v1.2-02.png)


### 改进

- 优化了代码结构,提高了代码的可读性和可维护性。
- 改进了错误处理机制,提供更友好的错误提示信息。
- 发布了 github actions 自动化构建流程,保证了代码质量和稳定性。

### 修复

- 修复了一些潜在的 bug 和稳定性问题。

### 其他

- 更新了文档和示例代码,方便用户快速上手使用。

希望这次更新能够为用户带来更好的使用体验,如果您在使用过程中遇到任何问题或有任何建议,欢迎向我们反馈。

[**Full Changelog**](https://github.com/suifei/asm2hex/compare/v1.1...main)

## 功能特点
Expand Down
92 changes: 92 additions & 0 deletions archs/arch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package archs

import (
"fmt"
"strings"

"github.com/suifei/asm2hex/bindings/capstone"
"github.com/suifei/asm2hex/bindings/keystone"
)

func Disassemble(arch capstone.Architecture, mode capstone.Mode, code []byte, offset uint64, bigEndian bool /*syntaxValue capstone.OptionValue, */, addAddress bool) (string, uint64, bool, error) {
defer func() {
if r := recover(); r != nil {
return
}
}()

engine, err := capstone.New(arch, mode)
if err != nil {
return "", 0, false, err
}
defer engine.Close()

// if syntaxValue != 0 {
// engine.Option(capstone.OPT_SYNTAX, capstone.OptionValue(syntaxValue))
// }

if bigEndian {
code, err = capstoneToBigEndian(code, arch, mode)
if err != nil {
return "", 0, false, err
}
}

insns, err := engine.Disasm(code, offset, 0)
if err != nil {
return "", 0, false, err
}

var result string
for _, insn := range insns {
if addAddress {
result += fmt.Sprintf("%08X:\t%-6s\t%-20s\n", insn.Address(), insn.Mnemonic(), insn.OpStr())
} else {
result += fmt.Sprintf("%-6s\t%-20s\n", insn.Mnemonic(), insn.OpStr())
}
}

return result, uint64(len(insns)), true, nil
}
func Assemble(arch keystone.Architecture, mode keystone.Mode, code string, offset uint64, bigEndian bool /*, syntaxValue keystone.OptionValue*/) ([]byte, uint64, bool, error) {
defer func() {
if r := recover(); r != nil {
return
}
}()

code = strings.TrimSpace(code)
if code == "" {
return nil, 0, false, fmt.Errorf("empty code")
}
if strings.HasPrefix(code, ";") {
return nil, 0, false, fmt.Errorf("commented code")
}
if idx := strings.Index(code, ";"); idx > 0 {
code = code[:idx]
}

ks, err := keystone.New(keystone.Architecture(arch), keystone.Mode(mode))
if err != nil {
return nil, 0, false, err
}
defer ks.Close()

// if syntaxValue != 0 {
// ks.Option(keystone.OPT_SYNTAX, keystone.OptionValue(syntaxValue))
// }

encoding, stat_count, ok := ks.Assemble(code, offset)
if err := ks.LastError(); err != nil {
return nil, 0, false, err
}

if ok && bigEndian {
encoding, err = keystoneToBigEndian(encoding, arch, mode)
if err != nil {
return nil, 0, false, err
}
}

return encoding, stat_count, ok, nil
}
94 changes: 94 additions & 0 deletions archs/arch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package archs

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/suifei/asm2hex/bindings/capstone"
"github.com/suifei/asm2hex/bindings/keystone"
)

func TestDisassemble(t *testing.T) {
code := []byte{0x48, 0x83, 0x3d, 0x01, 0x02, 0x03, 0x04, 0x05}
arch := capstone.ARCH_X86
mode := capstone.MODE_64
syntaxValue := capstone.OPT_SYNTAX_INTEL
str, count, ok, err := Disassemble(arch, mode, code, 0x100, false, syntaxValue)
require.NoError(t, err)
require.True(t, ok)
require.Equal(t, 1, int(count))
require.Equal(t, "cmpq\t$0x4030201020304,0x105\t;0x100\n", str)
}

func TestAssemble_x86_64_code_big_endian(t *testing.T) {
code := "mov rax, 1"
encoding, count, ok, err := Assemble(keystone.ARCH_X86, keystone.MODE_64, code, 0x100, true, keystone.OPT_SYNTAX_ATT)
assert.Nil(t, err)
assert.Equal(t, uint64(1), count)
assert.True(t, ok)
assert.Equal(t, []byte{0x48, 0xc7, 0xc0, 0x01, 0x00, 0x00, 0x00, 0x00}, encoding)
}

func TestAssemble_x86_64_code_little_endian(t *testing.T) {
code := "mov rax, 1"
encoding, count, ok, err := Assemble(keystone.ARCH_X86, keystone.MODE_64, code, 0x100, false, keystone.OPT_SYNTAX_ATT)
assert.Nil(t, err)
assert.Equal(t, uint64(1), count)
assert.True(t, ok)
assert.Equal(t, []byte{0xc7, 0xc0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x48}, encoding)
}

func TestAssemble_x86_32_code_big_endian(t *testing.T) {
code := "mov eax, 1"
encoding, count, ok, err := Assemble(keystone.ARCH_X86, keystone.MODE_32, code, 0x100, true, keystone.OPT_SYNTAX_ATT)
assert.Nil(t, err)
assert.Equal(t, uint64(1), count)
assert.True(t, ok)
assert.Equal(t, []byte{0x66, 0x3d, 0x01, 0x00, 0x00, 0x00}, encoding)
}

func TestAssemble_x86_32_code_little_endian(t *testing.T) {
code := "mov eax, 1"
encoding, count, ok, err := Assemble(keystone.ARCH_X86, keystone.MODE_32, code, 0x100, false, keystone.OPT_SYNTAX_ATT)
assert.Nil(t, err)
assert.Equal(t, uint64(1), count)
assert.True(t, ok)
assert.Equal(t, []byte{0x01, 0x3d, 0x66, 0x00, 0x00, 0x00}, encoding)
}

func TestAssemble_ARM_code_big_endian(t *testing.T) {
code := "mov r1, 1"
encoding, count, ok, err := Assemble(keystone.ARCH_ARM, keystone.MODE_ARM, code, 0x100, true, keystone.OPT_SYNTAX_ATT)
assert.Nil(t, err)
assert.Equal(t, uint64(4), count)
assert.True(t, ok)
assert.Equal(t, []byte{0x0, 0x0, 0xa0, 0xe3}, encoding)
}

func TestAssemble_ARM_code_little_endian(t *testing.T) {
code := "mov r1, 1"
encoding, count, ok, err := Assemble(keystone.ARCH_ARM, keystone.MODE_ARM, code, 0x100, false, keystone.OPT_SYNTAX_ATT)
assert.Nil(t, err)
assert.Equal(t, uint64(4), count)
assert.True(t, ok)
assert.Equal(t, []byte{0xe3, 0xa0, 0x0, 0x0}, encoding)
}

func TestAssemble_ARM64_code_big_endian(t *testing.T) {
code := "mov w1, 1"
encoding, count, ok, err := Assemble(keystone.ARCH_ARM64, keystone.MODE_ARM, code, 0x100, true, keystone.OPT_SYNTAX_ATT)
assert.Nil(t, err)
assert.Equal(t, uint64(4), count)
assert.True(t, ok)
assert.Equal(t, []byte{0x0, 0x0, 0x20, 0x1f}, encoding)
}

func TestAssemble_ARM64_code_little_endian(t *testing.T) {
code := "mov w1, 1"
encoding, count, ok, err := Assemble(keystone.ARCH_ARM64, keystone.MODE_ARM, code, 0x100, false, keystone.OPT_SYNTAX_ATT)
assert.Nil(t, err)
assert.Equal(t, uint64(4), count)
assert.True(t, ok)
assert.Equal(t, []byte{0x1f, 0x20, 0x0, 0x0}, encoding)
}
79 changes: 0 additions & 79 deletions archs/arm32.go

This file was deleted.

Loading

0 comments on commit a34b253

Please sign in to comment.