summaryrefslogtreecommitdiff
path: root/lib/ocaml_rts/linksem/main_elf.ml
blob: c5a31ebefb37987656c097abfd646169897c738e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
(*Generated by Lem from main_elf.lem.*)
(** [main_elf], the main module for the test program of the ELF development.
  * Run like so:
  *   ./main_elf.opt --FLAG BINARY
  * where:
  *   BINARY is an ELF binary
  *   FLAG   is in the set { file-header, program-headers, section-headers,
  *                            dynamic, relocs, symbols }
  *
  *)

open Lem_basic_classes
open Lem_function
open Lem_maybe
open Lem_list
open Lem_num
open Lem_string
open Lem_tuple

open Byte_sequence
open Default_printing
open Error
open Hex_printing
open Missing_pervasives
open Show
open Lem_assert_extra

open Endianness

open Elf_dynamic
open Elf_header
open Elf_file
open Elf_program_header_table
open Elf_section_header_table
open Elf_types_native_uint

open Harness_interface
open Sail_interface

open Abi_aarch64_relocation

open Abi_amd64_elf_header
open Abi_amd64_relocation
open Abi_amd64_serialisation

open Abi_power64_dynamic

open Abi_x86_relocation

open Abi_power64_relocation

open Gnu_ext_dynamic
open Gnu_ext_program_header_table
open Gnu_ext_section_header_table

open Dwarf

let default_hdr_bdl:('a ->string)*('b ->string)= 
  (default_os_specific_print, default_proc_specific_print)

let default_pht_bdl:('a ->string)*('b ->string)= 
  (default_os_specific_print, default_proc_specific_print)

let default_sht_bdl:('b ->string)*('a ->string)*('c ->string)= 
  (default_os_specific_print, default_proc_specific_print, default_user_specific_print)


(* unrolled and made tail recursive for efficiency on large ELF files...*)
(*val chunks : list string -> list (list string) -> list (list string) * nat*)
let rec chunks (ss : string list) (accum : ( string list) list):((string)list)list*int=  
 ((match ss with
    | s1::s2::s3::s4::s5::s6::s7::s8::s9::s10::s11::s12::s13::s14::s15::s16::ss ->
        chunks ss ([(s2^s1);(s4^s3);(s6^s5);(s8^s7);(s10^s9);(s12^s11);(s14^s13);(s16^s15)]::accum)
    | s1::s2::s3::s4::s5::s6::s7::s8::s9::s10::s11::s12::s13::s14::s15::[] ->
        let buff    = ([(s2^s1);(s4^s3);(s6^s5);(s8^s7);(s10^s9);(s12^s11);(s14^s13);("00"^s15)]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 15)
    | s1::s2::s3::s4::s5::s6::s7::s8::s9::s10::s11::s12::s13::s14::ss ->
        let bits    = (replicate0(Nat_big_num.of_int 1) "    ") in
        let fixed   = (intercalate " " bits) in
        let buff    =  (List.rev_append (List.rev [(s2^s1);(s4^s3);(s6^s5);(s8^s7);(s10^s9);(s12^s11);(s14^s13)]) [concatS fixed]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 14)
    | s1::s2::s3::s4::s5::s6::s7::s8::s9::s10::s11::s12::s13::[] ->
        let bits    = (replicate0(Nat_big_num.of_int 1) "    ") in
        let fixed   = (intercalate " " bits) in
        let buff    =  (List.rev_append (List.rev [(s2^s1);(s4^s3);(s6^s5);(s8^s7);(s10^s9);(s12^s11);("00"^s13)]) [concatS fixed]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 13)
    | s1::s2::s3::s4::s5::s6::s7::s8::s9::s10::s11::s12::[] ->
        let bits    = (replicate0(Nat_big_num.of_int 2) "    ") in
        let fixed   = (intercalate " " bits) in
        let buff    =  (List.rev_append (List.rev [(s2^s1);(s4^s3);(s6^s5);(s8^s7);(s10^s9);(s12^s11)]) [concatS fixed]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 12)
    | s1::s2::s3::s4::s5::s6::s7::s8::s9::s10::s11::[] ->
        let bits    = (replicate0(Nat_big_num.of_int 2) "    ") in
        let fixed   = (intercalate " " bits) in
        let buff    =  (List.rev_append (List.rev [(s2^s1);(s4^s3);(s6^s5);(s8^s7);(s10^s9);("00"^s11)]) [concatS fixed]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 11)
    | s1::s2::s3::s4::s5::s6::s7::s8::s9::s10::[] ->
        let bits    = (replicate0(Nat_big_num.of_int 3) "    ") in
        let fixed   = (intercalate " " bits) in
        let buff    =  (List.rev_append (List.rev [(s2^s1);(s4^s3);(s6^s5);(s8^s7);(s10^s9)]) [concatS fixed]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 10)
    | s1::s2::s3::s4::s5::s6::s7::s8::s9::[] ->
        let bits    = (replicate0(Nat_big_num.of_int 3) "    ") in
        let fixed   = (intercalate " " bits) in
        let buff    =  (List.rev_append (List.rev [(s2^s1);(s4^s3);(s6^s5);(s8^s7);("00"^s9)]) [concatS fixed]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 9)
    | s1::s2::s3::s4::s5::s6::s7::s8::[] ->
        let bits    = (replicate0(Nat_big_num.of_int 4) "    ") in
        let fixed   = (intercalate " " bits) in
        let buff    =  (List.rev_append (List.rev [(s2^s1);(s4^s3);(s6^s5);(s8^s7)]) [concatS fixed]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 8)
    | s1::s2::s3::s4::s5::s6::s7::[] ->
        let bits    = (replicate0(Nat_big_num.of_int 4) "    ") in
        let fixed   = (intercalate " " bits) in
        let buff    =  (List.rev_append (List.rev [(s2^s1);(s4^s3);(s6^s5);("00"^s7)]) [concatS fixed]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 7)
    | s1::s2::s3::s4::s5::s6::[] ->
        let bits    = (replicate0(Nat_big_num.of_int 5) "    ") in
        let fixed   = (intercalate " " bits) in
        let buff    =  (List.rev_append (List.rev [(s2^s1);(s4^s3);(s6^s5)]) [concatS fixed]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 6)
    | s1::s2::s3::s4::s5::[] ->
        let bits    = (replicate0(Nat_big_num.of_int 5) "    ") in
        let fixed   = (intercalate " " bits) in
        let buff    =  (List.rev_append (List.rev [(s2^s1);(s4^s3);("00"^s5)]) [concatS fixed]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 5)
    | s1::s2::s3::s4::[] ->
        let bits    = (replicate0(Nat_big_num.of_int 6) "    ") in
        let fixed   = (intercalate " " bits) in
        let buff    =  (List.rev_append (List.rev [(s2^s1);(s4^s3)]) [concatS fixed]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 4)
    | s1::s2::s3::[] ->
        let bits    = (replicate0(Nat_big_num.of_int 6) "    ") in
        let fixed   = (intercalate " " bits) in
        let buff    =  (List.rev_append (List.rev [(s2^s1);("00"^s3)]) [concatS fixed]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 3)
    | s1::s2::[] ->
        let bits    = (replicate0(Nat_big_num.of_int 7) "    ") in
        let fixed   = (intercalate " " bits) in
        let buff    =  (List.rev_append (List.rev [(s2^s1)]) [concatS fixed]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 2)
    | s1::[] ->
        let bits    = (replicate0(Nat_big_num.of_int 7) "    ") in
        let fixed   = (intercalate " " bits) in
        let buff    =  (List.rev_append (List.rev [("00"^s1)]) [concatS fixed]) in
          ( List.rev_append (List.rev (List.rev accum)) [buff], 1)
    | [] -> (List.rev accum, 0)
  ))
  
(*val provide_offsets : (list (list string) * nat) -> list (string * list string)*)
let provide_offsets (ss, ed):(string*(string)list)list=  
  
 (List.rev_append (List.rev (Lem_list.mapi (fun i x ->
    let hx = (unsafe_hex_string_of_natural( 7) ( Nat_big_num.mul(Nat_big_num.of_int i)(Nat_big_num.of_int 16))) in
      (hx, x)) ss)) (if ed = 0 then
      [(unsafe_hex_string_of_natural( 7) ( Nat_big_num.mul(Nat_big_num.of_int (List.length ss))(Nat_big_num.of_int 16)), [])]
    else
      [(unsafe_hex_string_of_natural( 7) ( Nat_big_num.add( Nat_big_num.mul(Nat_big_num.of_int ( Nat_num.nat_monus(List.length ss)( 1)))(Nat_big_num.of_int 16)) (Nat_big_num.of_int ed)), [])]))

(*val create_chunks : byte_sequence -> list (string * list string)*)
let create_chunks bs0:(string*(string)list)list=  
 (let ss = (Lem_list.map (fun x ->
    unsafe_hex_string_of_natural( 2) (natural_of_byte x))
    (Byte_sequence.byte_list_of_byte_sequence bs0))
  in
    provide_offsets (chunks ss []))
    
(*val print_chunk : string * list string -> string*)
let print_chunk (off, ss):string=  
 ((match ss with
    | [] -> off
    | _  -> off ^ (" " ^ concatS (intercalate " " ss))
  ))
    
(*val obtain_abi_specific_string_of_reloc_type : natural -> (natural -> string)*)
let obtain_abi_specific_string_of_reloc_type mach:Nat_big_num.num ->string=  
 (if Nat_big_num.equal mach elf_ma_ppc64 then
    string_of_ppc64_relocation_type
  else if Nat_big_num.equal mach elf_ma_386 then
    string_of_x86_relocation_type
  else if Nat_big_num.equal mach elf_ma_aarch64 then
    string_of_aarch64_relocation_type
  else if Nat_big_num.equal mach elf_ma_x86_64 then
    string_of_amd64_relocation_type
  (*else if mach = elf_ma_mips then
    string_of_mips64_relocation_type*)
  else
    (fun y->"Cannot deduce ABI"))
    
let ( _:unit) =  
(let res =    
(let (flag, arg) =      
((match Ml_bindings.argv_list with
        | progname::flag::fname1::more -> (flag, fname1)
        | _                           -> failwith "usage: main_elf <flag> <fname>"
      ))
    in
    Byte_sequence.acquire arg    >>= (fun bs0 ->
    repeatM' Elf_header.ei_nident bs0 (read_unsigned_char Endianness.default_endianness) >>= (fun (ident, bs) ->
    (match Lem_list.list_index ident( 4) with
      | None -> failwith "ELF ident transcription error"
      | Some c  ->
        if Nat_big_num.equal (Nat_big_num.of_string (Uint32.to_string c)) Elf_header.elf_class_32 then
            let ret =              
(if flag = "--file-header" then
                Elf_header.read_elf32_header bs0 >>= (fun (hdr, _) ->
                return (Harness_interface.harness_string_of_elf32_file_header hdr))
              else if flag = "--program-headers" then
                Elf_file.read_elf32_file bs0 >>= (fun f1 ->
                get_elf32_file_section_header_string_table f1 >>= (fun stbl ->
                return (Harness_interface.harness_string_of_elf32_program_headers
                  string_of_gnu_ext_segment_type 
                  (fun x ->
                    Nat_big_num.to_string x)
                  f1.elf32_file_header
                  f1.elf32_file_program_header_table
                  f1.elf32_file_section_header_table
                  stbl
                  bs0)))
              else if flag = "--section-headers" then
                Elf_file.read_elf32_file bs0 >>= (fun f1 ->
                get_elf32_file_section_header_string_table f1 >>= (fun stbl ->
                return (Harness_interface.harness_string_of_elf32_section_headers
                  string_of_gnu_ext_section_type
                  (fun x -> Nat_big_num.to_string x)
                  (fun x -> Nat_big_num.to_string x)
                  f1.elf32_file_header
                  f1.elf32_file_section_header_table
                  stbl)))
              else if flag = "--relocs" then
                Elf_file.read_elf32_file bs0 >>= (fun f1 ->
                let print_reloc = (obtain_abi_specific_string_of_reloc_type (Nat_big_num.of_string (Uint32.to_string f1.elf32_file_header.elf32_machine))) in
                return (Harness_interface.harness_string_of_elf32_relocs
                  f1
                  print_reloc
                  bs0))
(*            else if flag = "--symbols" then
                Harness_interface.harness_string_of_elf32_syms
                  f1
                  show
                  show
                  bs0 *)
              else if flag = "--dynamic" then
                Elf_file.read_elf32_file bs0 >>= (fun f1 ->
                let so = (is_elf32_shared_object_file f1.elf32_file_header) in
                return (Harness_interface.harness_string_of_elf32_dynamic_section
                  f1
                  bs0
                  gnu_ext_os_additional_ranges
                  (fun x -> gnu_ext_tag_correspondence_of_tag x)
                  (fun x -> gnu_ext_tag_correspondence_of_tag x)
                  (fun x -> string_of_dynamic_tag so x gnu_ext_os_additional_ranges string_of_gnu_ext_dynamic_tag (fun _ -> "proc: from main_elf"))
                  gnu_ext_elf32_value_of_elf32_dyn
                  (fun _ _ -> Error.fail "proc: from main_elf")))
              else if flag = "--in-out" then
                Elf_file.read_elf32_file bs0 >>= (fun f1 ->
                (match Elf_file.bytes_of_elf32_file f1 with
                  | Fail    f -> return f
                  | Success s ->
                    let chunks1 = (create_chunks s) in
                    let lines  = (concatS (intercalate "\n" (Lem_list.map print_chunk chunks1))) in
                      return lines
                ))
              else if flag = "--debug-dump=info" then
                Elf_file.read_elf32_file bs0 >>= (fun f1 ->
                get_elf32_file_section_header_string_table f1 >>= (fun stbl ->
                return (Dwarf.harness_string_of_elf32_debug_info_section
                  f1 
                  bs0
                  (*string_of_gnu_ext_section_type
                  (fun x -> show x)
                  (fun x -> show x)
                  f1.elf32_file_header
                  f1.elf32_file_section_header_table
                  stbl*)
                  )))
              else
                failwith "Unrecognised flag")
            in
              ret
        else if Nat_big_num.equal (Nat_big_num.of_string (Uint32.to_string c)) Elf_header.elf_class_64 then
            let ret =              
(if flag = "--file-header" then
                Elf_header.read_elf64_header bs0 >>= (fun (hdr, _) ->
                return (Harness_interface.harness_string_of_elf64_file_header hdr))
              else if flag = "--program-headers" then
                Elf_file.read_elf64_file bs0 >>= (fun f1 ->
                get_elf64_file_section_header_string_table f1 >>= (fun stbl ->
                return (Harness_interface.harness_string_of_elf64_program_headers
                  string_of_gnu_ext_segment_type 
                  (fun x ->
                    Nat_big_num.to_string x)
                  f1.elf64_file_header
                  f1.elf64_file_program_header_table
                  f1.elf64_file_section_header_table
                  stbl
                  bs0)))
              else if flag = "--section-headers" then
                Elf_file.read_elf64_file bs0 >>= (fun f1 ->
                get_elf64_file_section_header_string_table f1 >>= (fun stbl ->
                return (Harness_interface.harness_string_of_elf64_section_headers
                  string_of_gnu_ext_section_type
                  (fun x -> Nat_big_num.to_string x)
                  (fun x -> Nat_big_num.to_string x)
                  f1.elf64_file_header
                  f1.elf64_file_section_header_table
                  stbl)))
              else if flag = "--relocs" then
                Elf_file.read_elf64_file bs0 >>= (fun f1 ->
                let print_reloc = (obtain_abi_specific_string_of_reloc_type (Nat_big_num.of_string (Uint32.to_string f1.elf64_file_header.elf64_machine))) in
                return (Harness_interface.harness_string_of_elf64_relocs
                  f1
                  print_reloc
                  bs0))
              (*else if flag = "--symbols" then
                Harness_interface.harness_string_of_elf64_syms
                  f1
                  show
                  show
                  bs0*)
              else if flag = "--dynamic" then
                Elf_file.read_elf64_file bs0 >>= (fun f1 ->
                let so = (is_elf64_shared_object_file f1.elf64_file_header) in
                return (Harness_interface.harness_string_of_elf64_dynamic_section
                  f1
                  bs0
                  gnu_ext_os_additional_ranges
                  (fun x -> gnu_ext_tag_correspondence_of_tag x)
                  (fun x -> abi_power64_tag_correspondence_of_tag x) (* ABI! *)
                  (fun x -> string_of_dynamic_tag so x gnu_ext_os_additional_ranges string_of_gnu_ext_dynamic_tag string_of_abi_power64_dynamic_tag)
                  gnu_ext_elf64_value_of_elf64_dyn
                  abi_power64_elf64_value_of_elf64_dyn)) (* ABI! *)
              else if flag = "--in-out" then
                Elf_file.read_elf64_file bs0 >>= (fun f1 ->
                (match Elf_file.bytes_of_elf64_file f1 with
                  | Fail    f -> return f
                  | Success s ->
                    let chunks1 = (create_chunks s) in
                    let lines  = (concatS (intercalate "\n" (Lem_list.map print_chunk chunks1))) in
                      return lines
                ))
              else if flag = "--debug-dump=info" then
                Elf_file.read_elf64_file bs0 >>= (fun f1 ->
                get_elf64_file_section_header_string_table f1 >>= (fun stbl ->
                return (Dwarf.harness_string_of_elf64_debug_info_section
                  f1
                  bs0
                  (*string_of_gnu_ext_section_type
                  (fun x -> show x)
                  (fun x -> show x)
                  f1.elf64_file_header
                  f1.elf64_file_section_header_table
                  stbl*)
                  )))
              else
                failwith "Unimplemented (for ELF64) or unrecognised flag")
            in
              ret
        else
          failwith "ELF ident transcription error"
    ))))
  in
    (match res with
      | Fail err  -> prerr_endline ("[!]: " ^ err)
      | Success e -> print_endline (string_of_string e)
    ))

(*
let _ =
  match Sail_interface.populate_and_obtain_global_symbol_init_info "../test/mixed-binaries/tiny-istatic-with-malloc/tiny-tinystatic-with-malloc" with
    | Fail err  -> Missing_pervasives.errln ("[!]: " ^ err)
    | Success (img, syms) -> Missing_pervasives.outln (Sail_interface.string_of_executable_process_image img)
  end
*)