Skip to content

Instantly share code, notes, and snippets.

@eschulte
Created January 6, 2011 16:20
Show Gist options
  • Save eschulte/768101 to your computer and use it in GitHub Desktop.
Save eschulte/768101 to your computer and use it in GitHub Desktop.
Parse out an elf header, then write the header to a separate file. uses
(in-ns bin-ed.core) ; https://github.com/eschulte/bin-ed
;; example: parse the header of an elf file
(let [a-out (file-to-bytes "data/a.out")
head-ident-tmpl [:mag0 :byte
:mag1 :char
:mag2 :char
:mag3 :char
:class :byte
:data :byte
:ei_version :byte
:osabi :byte
:abiversion :byte
:padding [7 (fn [_] nil)]]
head-ident (parse head-ident-tmpl a-out)
;; address and offset sizes vary on 32 and 64 bit machines
size (case (int (:class (apply hash-map head-ident)))
1 :uint32 2 :uint64)
head-rest-tmpl [:type :uint16
:machine :uint16
:elf_version :uint32
:entry size
:phoff size
:shoff size
:flags :uint32
:ehsize :uint16
:phentsize :uint16
:phnum :uint16
:shentsize :uint16
:shnum :uint16
:shstrndx :uint16]]
;; read the header into a hash
(def header
(apply hash-map
(concat
head-ident
(parse head-rest-tmpl (drop (size-of head-ident-tmpl) a-out)))))
;; write just the header out to a separate file
(bytes-to-file "data/test.out"
(dump (concat head-ident-tmpl head-rest-tmpl) header)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment