GithubHelp home page GithubHelp logo

00mjk / llvm_assemble Goto Github PK

View Code? Open in Web Editor NEW

This project forked from snf/llvm_assemble

0.0 0.0 0.0 22 KB

LLVM based assembler for x86, Arm, Mips, PowerPC, Sparc and SystemZ (Rust API)

License: MIT License

Rust 32.75% C++ 61.26% C 5.99%

llvm_assemble's Introduction

LLVM_Assemble

Build Status

Use LLVM to compile different assembly languages to binary (Rust API).

It (ab)uses LLVM architecture to insert a BinaryStreamer during the ASM parsing run and output the binary code to a buffer instead of an object file. It supports inline and external labels/relocations too.

It supports x86, x86-64, Arm, Armeb, Arm64, Thumb, Mips, Mipsel, PowerPC, PowerPC64, Sparc and SystemZ.

Use it

It depends on LLVM >= 3.7, zlib and libedit.

In Cargo.toml write:

[dependencies.llvm_assemble]
git = "https://github.com/snf/llvm_assemble.git"

Examples

Basic:

use llvm_assemble::assembler::{Arch, assemble};

fn main() {
    assert_eq!(assemble(Arch::X86, "int3").unwrap(),
               [0xcc]);

    assert_eq!(assemble(Arch::X86_64, "vandnps ymm0,ymm1,ymm2").unwrap(),
               [0xC5, 0xF4, 0x55, 0xC2]);

    assert_eq!(assemble(Arch::X86_64, "je label0; nop; label0:").unwrap(),
               [0x74, 0x01, 0x90]);

    assert_eq!(assemble(Arch::Arm, "ldrb r3, [r1], #1").unwrap(),
               [0x01, 0x30, 0xd1, 0xe4]);

    assert_eq!(assemble(Arch::Arm, "pop {pc}").unwrap(),
               [0x4, 0xf0, 0x9d, 0xe4]);

    assert_eq!(assemble(Arch::Arm64, "mul w0, w1, w0").unwrap(),
               [0x20, 0x7c, 0x0, 0x1b]);

    assert_eq!(assemble(Arch::PPC32, "blr").unwrap(),
               [0x4e, 0x80, 0x0, 0x20]);
}

With external relocations:

use llvm_assemble::assembler::{Arch, Reloc, assemble2};

fn main() {
    let addr = 0x1000;
    let label0 = Reloc::new("label0", 0x1003);
    let bytes = assemble2(Arch::X86_64, "jmp label0", addr, &[label0]).unwrap();
    assert_eq!(bytes, [0xeb, 0x01]);
}

Full disclosure

This was coded as a part of a bigger project and while it works, it is not really tested in architectures other than x86, x86_64 and ARM. Use at your own risk of finding bugs.

llvm_assemble's People

Contributors

snf avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.