• Stars
    star
    263
  • Rank 155,624 (Top 4 %)
  • Language
    Rust
  • License
    MIT License
  • Created about 8 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Magical ELF and Mach-o object file writer backend

faerie Build Status Documentatation.

Emit some object files, at your leisure:

let name = "test.o";
let file = File::create(Path::new(name))?;
let mut obj = ArtifactBuilder::new(triple!("x86_64-unknown-unknown-unknown-elf"))
    .name(name.to_owned())
    .finish();

// first we declare our symbolic references;
// it is a runtime error to define a symbol _without_ declaring it first
obj.declarations(
    [
        ("deadbeef", Decl::function().into()),
        ("main",     Decl::function().global().into()),
        ("str.1",    Decl::cstring().into()),
        ("DEADBEEF", Decl::data_import().into()),
        ("printf",   Decl::function_import().into()),
    ].iter().cloned()
)?;

// we now define our local functions and data
// 0000000000000000 <deadbeef>:
//    0:	55                   	push   %rbp
//    1:	48 89 e5             	mov    %rsp,%rbp
//    4:	48 8b 05 00 00 00 00 	mov    0x0(%rip),%rax        # b <deadbeef+0xb>
// 			7: R_X86_64_GOTPCREL	DEADBEEF-0x4
//    b:	8b 08                	mov    (%rax),%ecx
//    d:	83 c1 01             	add    $0x1,%ecx
//   10:	89 c8                	mov    %ecx,%eax
//   12:	5d                   	pop    %rbp
//   13:	c3                   	retq
obj.define("deadbeef",
    vec![0x55,
         0x48, 0x89, 0xe5,
         0x48, 0x8b, 0x05, 0x00, 0x00, 0x00, 0x00,
         0x8b, 0x08,
         0x83, 0xc1, 0x01,
         0x89, 0xc8,
         0x5d,
         0xc3])?;
// main:
// 55	push   %rbp
// 48 89 e5	mov    %rsp,%rbp
// b8 00 00 00 00	mov    $0x0,%eax
// e8 00 00 00 00   callq  0x0 <deadbeef>
// 89 c6	mov    %eax,%esi
// 48 8d 3d 00 00 00 00 lea    0x0(%rip),%rdi # will be: deadbeef: 0x%x\n
// b8 00 00 00 00	mov    $0x0,%eax
// e8 00 00 00 00	callq  0x3f <main+33>  # printf
// b8 00 00 00 00	mov    $0x0,%eax
// 5d	pop    %rbp
// c3	retq
obj.define("main",
    vec![0x55,
         0x48, 0x89, 0xe5,
         0xb8, 0x00, 0x00, 0x00, 0x00,
         0xe8, 0x00, 0x00, 0x00, 0x00,
         0x89, 0xc6,
         0x48, 0x8d, 0x3d, 0x00, 0x00, 0x00, 0x00,
         0xb8, 0x00, 0x00, 0x00, 0x00,
         0xe8, 0x00, 0x00, 0x00, 0x00,
         0xb8, 0x00, 0x00, 0x00, 0x00,
         0x5d,
         0xc3])?;
obj.define("str.1", b"deadbeef: 0x%x\n\0".to_vec())?;

// Next, we declare our relocations,
// which are _always_ relative to the `from` symbol
obj.link(Link { from: "main", to: "str.1", at: 19 })?;
obj.link(Link { from: "main", to: "printf", at: 29 })?;
obj.link(Link { from: "main", to: "deadbeef", at: 10 })?;
obj.link(Link { from: "deadbeef", to: "DEADBEEF", at: 7 })?;

// Finally, we write the object file
obj.write(file)?;

Will emit an object file like this:

ELF REL X86_64-little-endian @ 0x0:

e_phoff: 0x0 e_shoff: 0x2a2 e_flags: 0x0 e_ehsize: 64 e_phentsize: 56 e_phnum: 0 e_shentsize: 64 e_shnum: 9 e_shstrndx: 1

SectionHeaders(9):
  Idx   Name                      Type   Flags                  Offset   Addr   Size    Link         Entsize   Align  
  0                           SHT_NULL                          0x0      0x0    0x0                  0x0       0x0    
  1     .strtab             SHT_STRTAB                          0x8c     0x0    0xc6                 0x0       0x1    
  2     .symtab             SHT_SYMTAB                          0x152    0x0    0xf0    .strtab(1)   0x18      0x8    
  3     .rodata.str.1       SHT_PROGBITS   ALLOC MERGE STRINGS    0x40     0x0    0x10                 0x1       0x1    
  4     .text.deadbeef    SHT_PROGBITS   ALLOC EXECINSTR        0x50     0x0    0x14                 0x0       0x10   
  5     .text.main        SHT_PROGBITS   ALLOC EXECINSTR        0x64     0x0    0x28                 0x0       0x10   
  6     .reloc.main           SHT_RELA                          0x242    0x0    0x48    .symtab(2)   0x18      0x8    
  7     .reloc.deadbeef       SHT_RELA                          0x28a    0x0    0x18    .symtab(2)   0x18      0x8    
  8     .note.GNU-stack   SHT_PROGBITS                          0x0      0x0    0x0                  0x0       0x1    

Syms(10):
               Addr   Bind       Type        Symbol     Size    Section             Other  
                 0    LOCAL      NOTYPE                 0x0                         0x0    
                 0    LOCAL      FILE        test.o     0x0     ABS                 0x0    
                 0    LOCAL      SECTION                0x0     .rodata.str.1(3)      0x0    
                 0    LOCAL      SECTION                0x0     .text.deadbeef(4)   0x0    
                 0    LOCAL      SECTION                0x0     .text.main(5)       0x0    
                 0    LOCAL      OBJECT      str.1      0x10    .rodata.str.1(3)      0x0    
                 0    LOCAL      FUNC        deadbeef   0x14    .text.deadbeef(4)   0x0    
                 0    GLOBAL     FUNC        main       0x28    .text.main(5)       0x0    
                 0    GLOBAL     NOTYPE      DEADBEEF   0x0                         0x0    
                 0    GLOBAL     NOTYPE      printf     0x0                         0x0    

Shdr Relocations(4):
  .text.main(3)
              13 X86_64_PC32 .rodata.str.1
              1d X86_64_PLT32 printf+-4
               a X86_64_PLT32 .text.deadbeef+-4

  .text.deadbeef(1)
               7 X86_64_GOTPCREL DEADBEEF+-4

😎

More Repositories

1

bingrep

like ~~grep~~ UBER, but for binaries
Rust
1,704
star
2

goblin

An impish, cross-platform binary parsing crate, written in Rust
Rust
1,167
star
3

dryad

An almost-parallel, semi-functioning, dynamic linker experiment, written in Rust
Rust
162
star
4

scroll

Scroll - making scrolling through buffers fun since 2016
Rust
151
star
5

rdr

Rdr is a cross-platform binary analysis and reverse engineering library, utilizing a unique symbol map for global analysis.
OCaml
83
star
6

raml

OCaml runtime and FFI bindings directly in Rust
Rust
78
star
7

elf2json

Converts an ELF binary to a JSON representation
OCaml
32
star
8

bin2json

Converts ELF, mach-o, or PE binaries to a JSON representation
OCaml
29
star
9

cargo-sym

Prints various binary symbols in your crate
Rust
13
star
10

silicon-disassembler

A high-performance, asynchronous disassembler which uses capstone.js as the backend and Web Workers for non-blocking requests
HTML
11
star
11

compilers-vm

A sandmark VM, in OCaml. Very fast.
OCaml
8
star
12

lazy_transducer

Lazy, parallel, indexable, generic data iterators
Rust
7
star
13

apli

Haskell APL Interpreter - ver 0.1
Haskell
6
star
14

stackup

Statically modify a program binary's runtime stack size on Mac OS X --- without recompiling.
Swift
6
star
15

algos

Algebraic arithmetization of boolean algebra and its implementation.
OCaml
6
star
16

silicon-hex-table

An efficient web-component hex table for all your online hexadecimal needs.
HTML
4
star
17

cloaked-robot

compilers final project --- Data Flow
Haskell
3
star
18

silicon-instructions

Display assembly instructions in a list-like format, using highlight.js
CSS
3
star
19

paper-trek

Boldly going where no paper element has gone before!
JavaScript
2
star
20

derp-archer

Project 2: Grammar Analysis and Parsing, CS454/554
Haskell
2
star
21

dotfiles

dotfiles and other configuration files
Haskell
2
star
22

metagoblin

WIP: Provides abstracted meta information about a binary
Rust
1
star
23

element-party

The splash page for http://element-party.xyz --- for those times when you just need to party.
HTML
1
star
24

scroll_derive

Macros 1.1 implementing #[derive(Pread, Pwrite)]
Rust
1
star
25

m4b.github.io

HTML
1
star
26

chat

Simple OCaml chat program
OCaml
1
star
27

ms-present

Master's Examination slides
TeX
1
star
28

silicon-symbols

Displays a list of symbols
HTML
1
star
29

drag-drop-behaviors

HTML
1
star
30

topo-tango

NM Game Jam Topological Tango
Lua
1
star
31

silicon-upload

A simple binary upload widget, to deliver all those bits to the server of your choice
HTML
1
star
32

get-polymer-imports

Simple python script to get all the polymer imports from a bower_components directory
Python
1
star
33

avant-lexeme

haskell compilers project
Haskell
1
star
34

foiled

Scala Implementation of FOIL
Scala
1
star
35

silicon-image

Displays a rasterized binary image with `silicon-image-behavior`, to update binary offsets via mouse movement, and etc.
HTML
1
star