mov and add work, sorta! need to do register allocation and syscalls next!

This commit is contained in:
lifning 2021-10-24 23:09:42 -07:00
parent 767c017782
commit d39078c325
9 changed files with 209 additions and 409 deletions

373
Cargo.lock generated
View File

@ -2,30 +2,6 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "addr2line"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e61f2b7f93d2c7d2b08263acaa4a363b3e276806c68af6134c44f523bf1aacd"
dependencies = [
"gimli",
]
[[package]]
name = "adler"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "ansi_term"
version = "0.11.0"
@ -52,21 +28,6 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "backtrace"
version = "0.3.62"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "091bcdf2da9950f96aa522681ce805e6857f6ca8df73833d35736ab2dc78e152"
dependencies = [
"addr2line",
"cc",
"cfg-if 1.0.0",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
]
[[package]]
name = "bincode"
version = "1.3.3"
@ -78,25 +39,22 @@ dependencies = [
[[package]]
name = "bindgen"
version = "0.50.1"
version = "0.55.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb0e5a5f74b2bafe0b39379f616b5975e08bcaca4e779c078d5c31324147e9ba"
checksum = "75b13ce559e6433d360c26305643803cb52cfbabbc2b9c47ce04a58493dfb443"
dependencies = [
"bitflags",
"cexpr",
"cfg-if 0.1.10",
"clang-sys",
"clap",
"env_logger",
"fxhash",
"lazy_static",
"log",
"lazycell",
"peeking_take_while",
"proc-macro2 0.4.30",
"quote 0.6.13",
"proc-macro2",
"quote",
"regex",
"rustc-hash",
"shlex",
"which",
]
[[package]]
@ -117,12 +75,6 @@ dependencies = [
"wyz",
]
[[package]]
name = "byteorder"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "cc"
version = "1.0.71"
@ -131,9 +83,9 @@ checksum = "79c2681d6594606957bbb8631c4b90a7fcaaa72cdb714743a437b156d6a7eedd"
[[package]]
name = "cexpr"
version = "0.3.6"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fce5b5fb86b0c57c20c834c1b412fd09c77c8a59b9473f86272709e78874cd1d"
checksum = "f4aedb84272dbe89af497cf81375129abda4fc0a9e7c5d317498c15cc30c0d27"
dependencies = [
"nom",
]
@ -152,9 +104,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clang-sys"
version = "0.28.1"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81de550971c976f176130da4b2978d3b524eaa0fd9ac31f3ceb5ae1231fb4853"
checksum = "10612c0ec0e0a1ff0e97980647cb058a6e7aedb913d01d009c406b8b7d0b26ee"
dependencies = [
"glob",
"libc",
@ -212,49 +164,12 @@ dependencies = [
"thiserror",
]
[[package]]
name = "env_logger"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aafcde04e90a5226a6443b7aabdb016ba2f8307c847d524724bd9b346dd1a2d3"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "failure"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d32e9bd16cc02eae7db7ef620b392808b89f6a5e16bb3497d159c6b92a0f4f86"
dependencies = [
"backtrace",
]
[[package]]
name = "funty"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed34cd105917e91daa4da6b3728c47b068749d6a62c59811f06ed2ac71d9da7"
[[package]]
name = "fxhash"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c31b6d751ae2c7f11320402d34e41349dd1016f8d5d45e48c4312bc8625af50c"
dependencies = [
"byteorder",
]
[[package]]
name = "gimli"
version = "0.25.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0a01e0497841a3b2db4f8afa483cce65f7e96a3498bd6c541734792aeac8fe7"
[[package]]
name = "glob"
version = "0.3.0"
@ -285,15 +200,6 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
[[package]]
name = "humantime"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
dependencies = [
"quick-error",
]
[[package]]
name = "instant"
version = "0.1.12"
@ -309,6 +215,12 @@ version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "lazycell"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
[[package]]
name = "libc"
version = "0.2.105"
@ -317,24 +229,22 @@ checksum = "869d572136620d55835903746bcb5cdc54cb2851fd0aeec53220b4bb65ef3013"
[[package]]
name = "libloading"
version = "0.5.2"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2b111a074963af1d37a139918ac6d49ad1d0d5e47f72fd55388619691a7d753"
checksum = "c0cf036d15402bea3c5d4de17b3fce76b3e4a56ebc1f577be0e7a72f7c607cf0"
dependencies = [
"cc",
"cfg-if 1.0.0",
"winapi",
]
[[package]]
name = "lightning-sys"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be3152f032ff6b4fbbcd2728917598e49058ab44be6bcfc6e608bbfe04ac855b"
version = "0.2.2"
dependencies = [
"bindgen",
"libc",
"mashup",
"num-traits 0.1.43",
"cc",
"lazy_static",
"tt-call",
]
[[package]]
@ -344,37 +254,12 @@ dependencies = [
"elf-utilities",
"hex",
"lightning-sys",
"llama",
"num-traits 0.2.14",
"num-traits",
"structopt",
"yaxpeax-arch",
"yaxpeax-arm",
]
[[package]]
name = "llama"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5c801f3d21479a9040387ca3c06520ee27ccd6edfebdaa3f3e3eaacdb0473a7"
dependencies = [
"lazy_static",
"llvm-sys",
"thiserror",
]
[[package]]
name = "llvm-sys"
version = "130.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a0de69eac0257a2f6c0da27b42e6a8dd21bae7b39c37caf87aee6021d79d61f"
dependencies = [
"cc",
"lazy_static",
"libc",
"regex",
"semver",
]
[[package]]
name = "lock_api"
version = "0.4.5"
@ -393,42 +278,12 @@ dependencies = [
"cfg-if 1.0.0",
]
[[package]]
name = "mashup"
version = "0.1.13+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6142b06bd98edf0317a51576837ea7b25528b380693ead98fe1278374cb95ab8"
dependencies = [
"mashup-impl",
"proc-macro-hack",
]
[[package]]
name = "mashup-impl"
version = "0.1.13+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e22fe05c98c8c0e9898c8e451ebe005864b0582b926f376c67dab87b97a8719"
dependencies = [
"proc-macro-hack",
"proc-macro2 0.4.30",
]
[[package]]
name = "memchr"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a"
[[package]]
name = "miniz_oxide"
version = "0.4.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a92518e98c078586bc6c934028adcca4c92a53d6a958196de835170a01d84e4b"
dependencies = [
"adler",
"autocfg",
]
[[package]]
name = "mio"
version = "0.7.14"
@ -453,12 +308,12 @@ dependencies = [
[[package]]
name = "nom"
version = "4.2.3"
version = "5.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ad2a91a8e869eeb30b9cb3119ae87773a8f4ae617f41b1eb9c154b2905f7bd6"
checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af"
dependencies = [
"memchr",
"version_check 0.1.5",
"version_check",
]
[[package]]
@ -470,15 +325,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "num-traits"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31"
dependencies = [
"num-traits 0.2.14",
]
[[package]]
name = "num-traits"
version = "0.2.14"
@ -488,15 +334,6 @@ dependencies = [
"autocfg",
]
[[package]]
name = "object"
version = "0.27.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67ac1d3f9a1d3616fd9a60c8d74296f22406a238b6a72f5cc1e6f314df4ffbf9"
dependencies = [
"memchr",
]
[[package]]
name = "parking_lot"
version = "0.11.2"
@ -528,15 +365,6 @@ version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
[[package]]
name = "pest"
version = "2.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10f4872ae94d7b90ae48754df22fd42ad52ce740b8f370b03da4835417403e53"
dependencies = [
"ucd-trie",
]
[[package]]
name = "proc-macro-error"
version = "1.0.4"
@ -544,10 +372,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
dependencies = [
"proc-macro-error-attr",
"proc-macro2 1.0.30",
"quote 1.0.10",
"proc-macro2",
"quote",
"syn",
"version_check 0.9.3",
"version_check",
]
[[package]]
@ -556,33 +384,9 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
dependencies = [
"proc-macro2 1.0.30",
"quote 1.0.10",
"version_check 0.9.3",
]
[[package]]
name = "proc-macro-hack"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7f95648580798cc44ff8efb9bb0d7ee5205ea32e087b31b0732f3e8c2648ee2"
dependencies = [
"proc-macro-hack-impl",
]
[[package]]
name = "proc-macro-hack-impl"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7be55bf0ae1635f4d7c7ddd6efc05c631e98a82104a73d35550bbc52db960027"
[[package]]
name = "proc-macro2"
version = "0.4.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
dependencies = [
"unicode-xid 0.1.0",
"proc-macro2",
"quote",
"version_check",
]
[[package]]
@ -591,22 +395,7 @@ version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "edc3358ebc67bc8b7fa0c007f945b0b18226f78437d61bec735a9eb96b61ee70"
dependencies = [
"unicode-xid 0.2.2",
]
[[package]]
name = "quick-error"
version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quote"
version = "0.6.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"
dependencies = [
"proc-macro2 0.4.30",
"unicode-xid",
]
[[package]]
@ -615,7 +404,7 @@ version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
dependencies = [
"proc-macro2 1.0.30",
"proc-macro2",
]
[[package]]
@ -639,8 +428,6 @@ version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
@ -651,10 +438,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "rustc-demangle"
version = "0.1.21"
name = "rustc-hash"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
[[package]]
name = "scopeguard"
@ -662,24 +449,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "semver"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f301af10236f6df4160f7c3f04eec6dbc70ace82d23326abad5edee88801c6b6"
dependencies = [
"semver-parser",
]
[[package]]
name = "semver-parser"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0bef5b7f9e0df16536d3961cfb6e84331c065b4066afb39768d0e319411f7"
dependencies = [
"pest",
]
[[package]]
name = "serde"
version = "1.0.130"
@ -695,8 +464,8 @@ version = "1.0.130"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b"
dependencies = [
"proc-macro2 1.0.30",
"quote 1.0.10",
"proc-macro2",
"quote",
"syn",
]
@ -757,8 +526,8 @@ checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2 1.0.30",
"quote 1.0.10",
"proc-macro2",
"quote",
"syn",
]
@ -768,9 +537,9 @@ version = "1.0.80"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d010a1623fbd906d51d650a9916aaefc05ffa0e4053ff7fe601167f3e715d194"
dependencies = [
"proc-macro2 1.0.30",
"quote 1.0.10",
"unicode-xid 0.2.2",
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
@ -779,15 +548,6 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "termcolor"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.11.0"
@ -812,16 +572,16 @@ version = "1.0.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b"
dependencies = [
"proc-macro2 1.0.30",
"quote 1.0.10",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "ucd-trie"
version = "0.1.3"
name = "tt-call"
version = "1.0.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56dee185309b50d1f11bfedef0fe6d036842e3fb77413abef29f8f8d1c5d4c1c"
checksum = "5e66dcbec4290c69dd03c57e76c2469ea5c7ce109c6dd4351c13055cf71ea055"
[[package]]
name = "unicode-segmentation"
@ -835,12 +595,6 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]]
name = "unicode-xid"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
[[package]]
name = "unicode-xid"
version = "0.2.2"
@ -853,28 +607,12 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]]
name = "version_check"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "914b1a6776c4c929a602fafd8bc742e06365d4bcbe48c30f9cca5824f70dc9dd"
[[package]]
name = "version_check"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]]
name = "which"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b57acb10231b9493c8472b20cb57317d0679a49e0bdbee44b3b803a6473af164"
dependencies = [
"failure",
"libc",
]
[[package]]
name = "winapi"
version = "0.3.9"
@ -891,15 +629,6 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
@ -919,7 +648,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1ba5c2f163fa2f866c36750c6c931566c6d93231ae9410083b0738953b609d5"
dependencies = [
"crossterm",
"num-traits 0.2.14",
"num-traits",
"serde",
"serde_derive",
]

View File

@ -4,11 +4,14 @@ version = "0.1.0"
edition = "2021"
[dependencies]
lightning-sys = "0.2"
lightning-sys = "0.2.2"
yaxpeax-arm = "0.1"
yaxpeax-arch = "0.2"
structopt = "0.3"
hex = "0.4"
num-traits = "0.2"
elf-utilities = "0.2"
llama = "0.9"
# llama = "0.9"
[patch.crates-io]
lightning-sys = { path = "../lightning-sys" }

View File

@ -1,25 +1,30 @@
TRIPLE := aarch64-linux-gnu-
all: hello-c-syscall.elf return-42.elf hello-syscall.elf hello-printf.elf
clean:
rm -f *.o *.elf
hello-printf.elf: hello-printf.o
aarch64-linux-gnu-gcc hello-printf.o -o hello-printf.elf
$(TRIPLE)gcc hello-printf.o -o hello-printf.elf
hello-printf.o: hello-printf.s
aarch64-linux-gnu-as hello-printf.s -o hello-printf.o
$(TRIPLE)as hello-printf.s -o hello-printf.o
hello-syscall.elf: hello-syscall.o
aarch64-linux-gnu-ld hello-syscall.o -o hello-syscall.elf
$(TRIPLE)ld hello-syscall.o -o hello-syscall.elf
hello-syscall.o: hello-syscall.s
aarch64-linux-gnu-as <(aarch64-linux-gnu-cpp -E hello-syscall.s) -o hello-syscall.o
$(TRIPLE)cpp -E hello-syscall.s | $(TRIPLE)as - -o hello-syscall.o
return-42.o: return-42.s
aarch64-linux-gnu-as return-42.s -o return-42.o
$(TRIPLE)cpp -E return-42.s | $(TRIPLE)as - -o return-42.o
return-42.elf: return-42.o
aarch64-linux-gnu-gcc return-42.o -o return-42.elf
$(TRIPLE)ld return-42.o -o return-42.elf
hello-c-syscall.elf: hello-c-syscall.o
aarch64-linux-gnu-gcc -Wl,-flto hello-c-syscall.o -o hello-c-syscall.elf
$(TRIPLE)gcc -Wl,-flto hello-c-syscall.o -o hello-c-syscall.elf
hello-c-syscall.o: hello-c-syscall.c
aarch64-linux-gnu-gcc -O2 -c hello-c-syscall.c
$(TRIPLE)gcc -O2 -c hello-c-syscall.c

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -1,20 +1,11 @@
/*
Return 42.
For Aarch64 (ARM64) architecture with GNU assembler, using Linux libc functions.
#include "sys/syscall.h"
Assemble with :
as -o hello.o hello.aarch64.linux.libc.gas.asm &&
gcc -o hello hello.o
*/
.global _start
.text
.global main
.section .text
.balign 4
_start:
mov x0, #14
add x0, x0, x0, LSL 1
mov x8, __NR_exit
svc 0
main:
stp x29, x30, [sp, -16]!
add x29, sp, 0
mov w0, 0
add w0, w0, 42
ldp x29, x30, [sp], 16
ret

View File

@ -1,5 +1,8 @@
use std::fmt;
use std::path::PathBuf;
use std::mem::size_of;
use std::ops::{Deref, DerefMut};
use std::convert::TryInto;
use structopt::StructOpt;
@ -9,8 +12,7 @@ use yaxpeax_arch::{AddressBase, Arch, Decoder, Instruction, LengthedInstruction,
use yaxpeax_arm::armv8::a64::{Opcode, Operand, SizeCode, ShiftStyle};
use num_traits::identities::Zero;
use lightning_sys::{Jit, JitState, JitWord};
use std::borrow::Borrow;
use lightning_sys::{Jit, JitState, JitWord, Reg};
mod _arch {
pub use yaxpeax_arm::armv8::a64::ARMv8;
@ -36,7 +38,7 @@ fn main() -> Result<()> {
.ok_or("Invalid UTF-8 in path")
.map_err(Into::into)
.and_then(elf_utilities::parser::parse_elf64)?;
println!("entry: {}", elf.ehdr.e_entry);
//println!("entry: {}", elf.ehdr.e_entry);
let text_section = elf.first_section_by(|p| p.name == ".text")
.ok_or("No .text section in ELF")?;
println!("text_section header: {:?}", text_section.header);
@ -48,87 +50,140 @@ fn main() -> Result<()> {
Ok(())
}
trait ToLightning {
fn translate(&self, js: &mut JitState);
struct ExtJitState<'a> {
js: JitState<'a>,
stack_base: i64,
stack_ptr: i64,
}
fn register_from_operand(operand: &Operand) -> Result<lightning_sys::Reg> {
match operand {
Operand::Register(size, value)
| Operand::RegisterOrSP(size, value) => match size {
SizeCode::X => { Ok(lightning_sys::Reg::R((*value).into())) }
// TODO: return size code too for handling logic?
SizeCode::W => { Ok(lightning_sys::Reg::R((*value).into())) }
impl<'a> Deref for ExtJitState<'a> {
type Target = JitState<'a>;
fn deref(&self) -> &Self::Target {
&self.js
}
}
impl<'a> DerefMut for ExtJitState<'a> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.js
}
}
const STACK_WORDS: usize = 64;
impl<'a> ExtJitState<'a> {
fn new(mut js: JitState<'a>) -> Self {
let stack = js.allocai(size_of::<[JitWord; STACK_WORDS]>() as i32) as i64;
ExtJitState {
js,
stack_base: stack,
stack_ptr: stack,
}
}
fn reg_from_operand(&mut self, operand: &Operand) -> Result<Reg> {
match operand {
Operand::RegisterOrSP(_, value)
| Operand::Register(_, value) => self.reg(*value),
_ => Err(format!("non-register operand: {:?}", operand).into()),
}
}
fn imm_from_operand(&self, operand: &Operand) -> Result<JitWord> {
match operand {
Operand::Immediate(value) => Ok((*value).into()),
Operand::Imm64(value) => Ok(*value as JitWord),
Operand::Imm16(value) => Ok((*value).into()),
Operand::ImmShift(base, shift) => Ok((*base as JitWord) << shift),
_ => Err(format!("non-immediate operand: {:?}", operand).into()),
}
}
// TODO: juggle if we don't have enough on the host
fn reg(&mut self, id: impl TryInto<i32>) -> Result<Reg> {
id.try_into()
.map(Reg::R)
.map_err(|_| "invalid register number (to the point of i32 overflow)".into())
}
fn push(&mut self, reg: Reg) -> Result<()> {
let sp = self.stack_ptr;
self.stxi(sp, Reg::FP, reg);
self.stack_ptr += size_of::<JitWord>() as i64;
if self.stack_ptr > self.stack_base + size_of::<[JitWord; STACK_WORDS]>() as i64 {
Err(format!("stack overflow trying to push {:?}", reg).into())
} else {
Ok(())
}
}
fn pop(&mut self, reg: Reg) -> Result<()> {
self.stack_ptr -= size_of::<JitWord>() as i64;
let sp = self.stack_ptr;
self.ldxi(reg, Reg::FP, sp);
if self.stack_ptr < self.stack_base {
Err(format!("compiler stack underflow trying to pop {:?}", reg).into())
} else {
Ok(())
}
_ => Err(format!("non-register operand: {:?}", operand).into()),
}
}
fn immediate_from_operand(operand: &Operand) -> Result<JitWord> {
match operand {
Operand::Immediate(value) => Ok((*value).into()),
Operand::Imm64(value) => Ok(*value as JitWord),
Operand::Imm16(value) => Ok((*value).into()),
Operand::ImmShift(base, shift) => Ok((*base as JitWord) << shift),
_ => Err(format!("non-immediate operand: {:?}", operand).into()),
}
trait ToLightning {
fn translate(&self, js: &mut ExtJitState);
}
impl ToLightning for _arch::ARMv8_Instr {
fn translate(&self, js: &mut JitState) {
fn translate(&self, js: &mut ExtJitState) {
let _arch::ARMv8_Instr { opcode, operands } = self;
match opcode {
Opcode::Invalid => { panic!("Invalid ARMv8 instruction: {:?}", self); }
Opcode::MOVZ => {
let [dest, src, _, _] = operands;
let dest = register_from_operand(dest).expect("dest");
let src = immediate_from_operand(src).expect("src");
let dest = js.reg_from_operand(dest).expect("dest");
let src = js.imm_from_operand(src).expect("src");
js.movi(dest, src);
}
Opcode::ADD => {
let [dest, src1, src2, _] = operands;
let dest = register_from_operand(dest).expect("dest");
let src1 = register_from_operand(src1).expect("src1");
match src2 {
Operand::Register(size, value)
| Operand::RegisterOrSP(size, value) => {
match size {
SizeCode::X => {
js.addr(dest, src1, lightning_sys::Reg::R((*value).into()));
}
SizeCode::W => {
// TODO: handle width differences
js.addr(dest, src1, lightning_sys::Reg::R((*value).into()));
js.extr_i(dest, dest)
}
}
let [dest_orig, src1_orig, src2_orig, _] = operands;
let dest = js.reg_from_operand(dest_orig).expect("dest");
let src1 = js.reg_from_operand(src1_orig).expect("src1");
match src2_orig {
Operand::Register(_, id)
| Operand::RegisterOrSP(_, id) => {
let src2 = js.reg(*id).expect("src2");
js.addr(dest, src1, src2);
}
Operand::RegShift(shift_style, shift, size, id) => {
todo!();
/*
Operand::RegShift(shift_style, shift, _, id) => {
let src2 = js.reg(*id).expect("src2");
let src2_tmp = Reg::V(*id as i32); // TODO: better "aux reg" handling...
match shift_style {
ShiftStyle::LSL => {}
ShiftStyle::LSR => {}
ShiftStyle::ASR => {}
ShiftStyle::LSL => js.lshi(src2_tmp, src2, (*shift).into()),
ShiftStyle::LSR => js.rshi_u(src2_tmp, src2, (*shift).into()),
ShiftStyle::ASR => js.rshi(src2_tmp, src2, (*shift).into()),
_ => panic!("invalid shift style for ADD"),
}*/
};
js.addr(dest, src1, src2_tmp);
}
Operand::RegOffset(_, _) => {}
Operand::RegRegOffset(_, _, _, _, _) => {}
Operand::RegPreIndex(_, _) => {}
Operand::RegPostIndex(_, _) => {}
Operand::ConditionCode(_) => {}
Operand::Offset(_) => {}
Operand::PCOffset(_) => {}
Operand::Immediate(_)
| Operand::Imm64(_)
| Operand::Imm16(_)
| Operand::ImmShift(..) => {
let src2 = immediate_from_operand(src2).expect("src2");
let src2 = js.imm_from_operand(src2_orig).expect("src2");
js.addi(dest, src1, src2);
}
_ => { panic!("Invalid second operand to ADD: {:?}", src2); }
_ => { panic!("Invalid second operand to ADD: {:?}", src2_orig); }
}
// sign-extend if it's a 32-bit operation
match dest_orig {
Operand::RegisterOrSP(SizeCode::W, ..)
| Operand::Register(SizeCode::W, ..) => {
js.extr_i(dest, dest);
}
_ => {}
}
}
_ => { todo!("opcode {:?}", opcode); }
@ -139,21 +194,31 @@ impl ToLightning for _arch::ARMv8_Instr {
pub(crate) fn decode_input<A: Arch>(buf: &[u8], verbose: bool)
where
A::Instruction: fmt::Display, for<'data> U8Reader<'data>: Reader<A::Address, A::Word>,
A::Instruction: fmt::Display + ToLightning,
for<'data> U8Reader<'data>: Reader<A::Address, A::Word>,
{
decode_input_with_decoder::<A>(A::Decoder::default(), buf, verbose);
}
pub(crate) fn decode_input_with_decoder<A: Arch>(decoder: A::Decoder, buf: &[u8], verbose: bool)
where
A::Instruction: fmt::Display, for<'data> U8Reader<'data>: Reader<A::Address, A::Word>,
A::Instruction: fmt::Display + ToLightning,
for<'data> U8Reader<'data>: Reader<A::Address, A::Word>,
{
let mut jit = Jit::new();
let mut js = jit.new_state();
js.prolog();
let mut ejs = ExtJitState::new(js);
let start = A::Address::zero();
let mut addr = start;
loop {
let mut reader = U8Reader::new(&buf[addr.to_linear()..]);
match decoder.decode(&mut reader) {
Ok(inst) => {
inst.translate(&mut ejs);
println!(
"{:#010x}: {:14}: {}",
addr.to_linear(),
@ -176,8 +241,15 @@ where
addr += A::Instruction::min_size();
}
}
if addr.to_linear() >= buf.len() {
if addr.to_linear() >= start.to_linear() + 8 { // buf.len() {
break;
}
}
let r0 = ejs.reg(0).unwrap();
ejs.retr(r0);
let code: extern fn() -> JitWord = unsafe { ejs.cast_emit() };
ejs.clear_state();
println!("exit code of jit: {}", code());
}