Skip to content

Instantly share code, notes, and snippets.

@tom-code
Last active October 3, 2017 23:56
Show Gist options
  • Save tom-code/f76580b99cd8cff0f136c60aa391084f to your computer and use it in GitHub Desktop.
Save tom-code/f76580b99cd8cff0f136c60aa391084f to your computer and use it in GitHub Desktop.
temp
type section = {
idx : int;
link : int;
mutable name : string;
offset : int;
name_offset : int;
entsize : int
};;
let get_byte mm ptr =
Bigarray.Genarray.get mm [|ptr|]
let rec dec_int mm ptr togo bits acu =
if togo == 0 then acu
else
let acx = ((int_of_char (get_byte mm (ptr)) ) lsl bits ) lor acu in
(dec_int mm (ptr+1) (togo-1) (bits+8) acx)
let rec dec_int64 mm ptr togo bits acu =
if togo == 0 then acu
else
let acx = ((int_of_char (get_byte mm (ptr)) ) lsl bits ) lor acu in
(dec_int64 mm (ptr+1) (togo-1) (bits+8) acx)
let get_uint32 mm ptr =
dec_int mm ptr 4 0 0
let get_uint16 mm ptr =
dec_int mm ptr 2 0 0
let get_uint64 mm ptr =
dec_int64 mm ptr 8 0 0
let get_bytes mm ptr len =
let out = Bytes.create len in
for idx = 0 to (len-1) do
Bytes.set out idx (get_byte mm (ptr+idx));
done;
out
let rec _read_zt_str mm idx buf =
let chr = get_byte mm idx in
if Char.code chr != 0 then begin
Buffer.add_char buf chr;
_read_zt_str mm (idx+1) buf
end
let read_zt_string mm idx =
let buf = Buffer.create 20 in
_read_zt_str mm idx buf;
Bytes.to_string (Buffer.to_bytes buf)
let rec read_sections mm ptr es num idx lst =
if num == 0 then
lst
else begin
(*let sh_type = (get_uint16 mm (ptr + 0x04)) in*)
let sec = {
idx = idx;
link = (get_uint32 mm (ptr + 0x28));
name = "?";
name_offset = (get_uint32 mm (ptr + 0x00));
offset = (get_uint64 mm (ptr + 0x18));
entsize = (get_uint64 mm (ptr + 0x38));
} in
read_sections mm (ptr+es) es (num-1) (idx+1) (sec::lst);
end
let section_find_off sections id =
List.fold_left (fun acc x -> if x.idx == id then x.offset else acc) 0 sections
let decode_names mm sections =
let sidx = (get_uint16 mm 0x3e) in
let soff = (section_find_off sections sidx) in
List.iter (fun x -> x.name <- (read_zt_string mm (soff+x.name_offset)) ) sections
let dump_sections sections =
List.iter (fun x ->
Printf.printf "idx = %02d name=%-16s link=%2d offset=%8d entsize=%d\n" x.idx x.name x.link x.offset x.entsize
) sections
let() =
let fname = "b" in
let fd = Unix.openfile fname [Unix.O_RDONLY] 0 in
let image_size = (Unix.stat fname).Unix.st_size in
Printf.printf "image_size=%d\n" image_size;
let mm = Bigarray.Genarray.map_file fd Bigarray.Char Bigarray.C_layout false (Array.of_list [image_size]) in
let cls = (int_of_char(get_byte mm 4)) in
let sh_off = (get_uint64 mm 0x28) in
let sh_ent_size = (get_uint16 mm 0x3a) in
let sh_ent_num = (get_uint16 mm 0x3c) in
Printf.printf "magic= %x\n" (get_uint32 mm 0);
Printf.printf "class = %d\n" cls;
Printf.printf "endian= %d\n" (int_of_char(get_byte mm 5));
Printf.printf "program_header_idx = %d\n" (get_uint64 mm 0x20);
Printf.printf "section_header_off = %d\n" sh_off;
Printf.printf "section_header_es = %d\n" sh_ent_size;
Printf.printf "section_header_num = %d\n" sh_ent_num;
let sections = List.rev (read_sections mm sh_off sh_ent_size sh_ent_num 0 []) in
decode_names mm sections;
dump_sections sections;
print_string "aa\n";;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment