ELF parsing example.
authorRichard W.M. Jones <rich@annexia.org>
Sun, 18 May 2008 19:29:03 +0000 (19:29 +0000)
committerRichard W.M. Jones <rich@annexia.org>
Sun, 18 May 2008 19:29:03 +0000 (19:29 +0000)
examples/elf.ml [new file with mode: 0644]

diff --git a/examples/elf.ml b/examples/elf.ml
new file mode 100644 (file)
index 0000000..0299765
--- /dev/null
@@ -0,0 +1,20 @@
+(* Read an ELF (Linux binary) header.
+ * $Id: 60_ping.ml,v 1.3 2008-04-25 11:08:43 rjones Exp $
+ *)
+
+open Printf
+
+let () =
+  let filename = "/bin/ls" in
+  let bits = Bitmatch.bitstring_of_file filename in
+
+  bitmatch bits with
+  | { 0x7f : 8; "ELF" : 24 : string;   (* ELF magic number *)
+      _ : 12*8 : bitstring;            (* ELF identifier *)
+      e_type : 16;                     (* object file type *)
+      e_machine : 16                   (* architecture *)
+    } ->
+      printf "%s: ELF binary, type %d, arch %d\n" filename e_type e_machine
+
+  | { _ } ->
+      eprintf "%s: Not an ELF binary\n" filename