summaryrefslogtreecommitdiff
path: root/src/elf_model/elf.ml
blob: ae78935a12f451fe17cd3d6737a1bbaa9d39710e (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
(*Generated by Lem from elf.lem.*)
open Lem_list
open Lem_list_extra
open Lem_maybe
open Lem_num
open Lem_string

open Elf_header
open Elf_program_header_table
open Elf_section_header
open Elf_string_table
open Elf_symbol_table

open Bitstring
open Error

type linked_elf_file =
  { elf_header : elf32_elf_header
	 ; elf_section_header_table :  elf32_section_header_table option
	 ; elf_program_header_table : elf32_program_header_table
	 ; elf_string_table : string list
	 ; elf_dynamic_string_table : string list
	 ; elf_symbol_table : elf32_symbol_table list
	 }

type elf32
	= Linked of linked_elf_file
	| Executable
	
let is_linked elf =	
((match elf with
		| Linked _ -> true
		| _        -> false
  ))
  
let is_executable elf =	
((match elf with
		| Executable -> true
		| _          -> false
  ))
  
let string_of_elf32 elf320 =	
((match elf320 with
		| Executable ->
				List.fold_right (^) [
					"Executable ELF file"
				] ""
		| Linked link ->
			let shdr =				
((match link.elf_section_header_table with
					| None  -> "No section header table present"
					| Some hdr -> string_of_elf32_section_header_table hdr
				))
			in
			let symtabs =				
(List.fold_right (^) (List.map string_of_elf32_symbol_table link.elf_symbol_table) "")
			in
				List.fold_right (^) [
				  string_of_elf32_elf_header (fun x -> "Unsupported") (fun x -> "Unsupported") link.elf_header; "\n\n"
				; string_of_elf32_program_header_table (fun x -> "Unsupported") (fun x -> "Unsupported") link.elf_program_header_table
				; shdr
				; string_of_elf32_string_table link.elf_string_table
				; string_of_elf32_dynamic_string_table link.elf_dynamic_string_table
				; symtabs
				] "\t"
	))
  
let read_elf32 bs0 =	
(read_elf32_elf_header bs0 >>= (fun (elf_header0, bs1) ->
	let (size, entry_size) = (program_header_table_size_and_entry_size elf_header0) in
	read_elf32_program_header_table (size * entry_size) bs1 >>= (fun (program_header_table, bs2) ->
	(if elf32_elf_header_is_section_table_present elf_header0 then
		let (size, entry_size, offset) = (section_header_table_size_and_entry_size_and_offset elf_header0) in
	    read_elf32_section_header_table size entry_size offset bs0 >>= (fun (section_header_table, bs3) ->
	    return (Some section_header_table, bs3))
  	else
	    return (None, bs2)) >>= (fun (section_header_table_opt, bs3) ->
	let string_tables = (read_elf32_string_tables section_header_table_opt bs0) in
	((match section_header_table_opt with
	   | None -> return []
	   | Some section_header_table -> read_elf32_symbol_tables section_header_table bs0
	 )) >>= (fun symbol_table ->
  return (Linked (
    { elf_header = elf_header0
	   ; elf_section_header_table = section_header_table_opt
	   ; elf_program_header_table = program_header_table
	   ; elf_string_table = (List.nth (string_tables)( 1))
	   ; elf_dynamic_string_table = (List.nth (string_tables)( 0))
	   ; elf_symbol_table = symbol_table
	   })))))))