Giter Site home page Giter Site logo

mach9's Introduction

Mach-9

A tool to convert Mach-O object files into Plan 9 assembler formatted output.

To use, first write some assembly and run it through your favorite assembler with debug info included

$ cat add_arm64.asm
// To assemble:
//   as add_arm64.asm -g -o add_arm64.o

.global armadd

// func armadd(a, b int) int
//m9: ·armadd(SB),NOSPLIT,$0-24
armadd:
    ldr x0, [x29, #16]  // a
    ldr x1, [x29, #24]  // b
    add x0, x0, x1
    str x0, [x29, #32]  // result
    ret

$ as -g -o add_arm64.o add_arm64.asm

Then pass the assembled object file to mach9:

$ mach9 add_arm64.o | tee add_arm64.s
/*
 * Generated by mach9 add_arm64.o; DO NOT EDIT.
 */

#include "textflag.h"

TEXT ·armadd(SB),NOSPLIT,$0-24
	WORD $0xf9400ba0 // ldr x0, [x29, #16]
	WORD $0xf9400fa1 // ldr x1, [x29, #24]
	WORD $0x8b010000 // add x0, x0, x1
	WORD $0xf90013a0 // str x0, [x29, #32]
	WORD $0xd65f03c0 // ret

The output is a valid Plan9 assembly file that contains the assembled code as byte sequence. To complete the example, the Go code below when built for ARM64 platform will include and execute the generated file and print 30 to stdout.

package main

import "fmt"

func armadd(a, b int) int

func main() {
	fmt.Println(armadd(10, 20))
}

m9 Directive

mach9 is unable to generate a valid Plan9 assembly file from the object file and debug data alone. It requires additional data, namely the function declaration and stack setup, which are provided via an m9: directive. This directive must appear on the line immediately preceeding every public symbol. If the line is missing or empty then mach9 will exit with an error.

Everything after the directive until the end of the line is placed into the output function definition. No validation or checking is performed. So the declaration m9: horses·count,NOSPLIT,$0 will generate output TEXT horses·count,NOSPLIT,$0.

See A Quick Guide to Go's Assembler for more details on the supported assembler directives.

Motivation

For another project I wanted to implement some Go functions in ARM assembly using the NEON instruction extensions (SIMD instruction set), which are not supported by Go Assembler's Intermediate Language. The solution is to use an assembler that does support the extensions and to convert the object code into literal sequences that the Go Assembler will accept.

TODO

  • Resolve questions about platform endianness
  • Verify issue about padding when using WORD, does this affect other platforms?
  • Investigate why DWARF labels drop _ prefix from symbols. C thing?
  • Support additional segments, e.g. data.

mach9's People

Contributors

chriskillpack avatar

Stargazers

Aurélien Rainone avatar Randy Reddig avatar Cameron Walters (cee-dub) avatar Achille avatar  avatar

Watchers

 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.