summaryrefslogtreecommitdiff
path: root/checklink/ELF_printers.ml
blob: 82ba479bddd1be2a6fed96625408f9bc61ccfb26 (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
open ELF_types
open Library

let string_of_elf32_half    = string_of_int
let string_of_elf32_addr    = string_of_int32
let string_of_elf32_off     = string_of_int32
let string_of_elf32_word    = string_of_int32

let string_of_elfclass = function
| ELFCLASSNONE    -> "ELFCLASSNONE"
| ELFCLASS32      -> "ELFCLASS32"
| ELFCLASS64      -> "ELFCLASS64"
| ELFCLASSUNKNOWN -> "ELFCLASSUNKNOWN"

let string_of_elfdata = function
| ELFDATANONE    -> "ELFDATANONE"
| ELFDATA2LSB    -> "ELFDATA2LSB"
| ELFDATA2MSB    -> "ELFDATA2MSB"
| ELFDATAUNKNOWN -> "ELFDATAUNKNOWN"

let string_of_ev = function
| EV_NONE    -> "EV_NONE"
| EV_CURRENT -> "EV_CURRENT"
| EV_UNKNOWN -> "EV_UNKNOWN"

let string_of_elf_identification ei =
  Printf.sprintf
    "{
ei_class   = %s;
ei_data    = %s;
ei_version = %s;
}"
    (string_of_elfclass ei.ei_class  )
    (string_of_elfdata  ei.ei_data   )
    (string_of_ev       ei.ei_version)

let string_of_et = function
| ET_NONE    -> "ET_NONE"
| ET_REL     -> "ET_REL"
| ET_EXEC    -> "ET_EXEC"
| ET_DYN     -> "ET_DYN"
| ET_CORE    -> "ET_CORE"
| ET_UNKNOWN -> "ET_UNKNOWN"

let string_of_em = function
| EM_NONE        -> "EM_NONE"
| EM_M32         -> "EM_M32"
| EM_SPARC       -> "EM_SPARC"
| EM_386         -> "EM_386"
| EM_68K         -> "EM_68K"
| EM_88K         -> "EM_88K"
| EM_860         -> "EM_860"
| EM_MIPS        -> "EM_MIPS"
| EM_MIPS_RS4_BE -> "EM_MIPS_RS4_BE"
| EM_PPC         -> "EM_PPC"
| EM_UNKNOWN     -> "EM_UNKNOWN"

let string_of_elf32_ehdr eh =
  Printf.sprintf
    "{
e_ident     = %s;
e_type      = %s;
e_machine   = %s;
e_version   = %s;
e_entry     = %s;
e_phoff     = %s;
e_shoff     = %s;
e_flags     = %s;
e_ehsize    = %s;
e_phentsize = %s;
e_phnum     = %s;
e_shentsize = %s;
e_shnum     = %s;
e_shstrndx  = %s;
}"
    (string_of_elf_identification eh.e_ident    )
    (string_of_et                 eh.e_type     )
    (string_of_em                 eh.e_machine  )
    (string_of_ev                 eh.e_version  )
    (string_of_elf32_addr         eh.e_entry    )
    (string_of_elf32_off          eh.e_phoff    )
    (string_of_elf32_off          eh.e_shoff    )
    (string_of_bitstring          eh.e_flags    )
    (string_of_elf32_half         eh.e_ehsize   )
    (string_of_elf32_half         eh.e_phentsize)
    (string_of_elf32_half         eh.e_phnum    )
    (string_of_elf32_half         eh.e_shentsize)
    (string_of_elf32_half         eh.e_shnum    )
    (string_of_elf32_half         eh.e_shstrndx )

let string_of_sht = function
| SHT_NULL     -> "SHT_NULL"
| SHT_PROGBITS -> "SHT_PROGBITS"
| SHT_SYMTAB   -> "SHT_SYMTAB"
| SHT_STRTAB   -> "SHT_STRTAB"
| SHT_RELA     -> "SHT_RELA"
| SHT_HASH     -> "SHT_HASH"
| SHT_DYNAMIC  -> "SHT_DYNAMIC"
| SHT_NOTE     -> "SHT_NOTE"
| SHT_NOBITS   -> "SHT_NOBITS"
| SHT_REL      -> "SHT_REL"
| SHT_SHLIB    -> "SHT_SHLIB"
| SHT_DYNSYM   -> "SHT_DYNSYM"
| SHT_UNKNOWN  -> "SHT_UNKNOWN"

let string_of_elf32_shdr sh =
  Printf.sprintf
    "{
sh_name      = %s;
sh_type      = %s;
sh_flags     = %s;
sh_addr      = %s;
sh_offset    = %s;
sh_size      = %s;
sh_link      = %s;
sh_info      = %s;
sh_addralign = %s;
sh_entsize   = %s;
}"
    (sh.sh_name                           )
    (string_of_sht         sh.sh_type     )
    (string_of_elf32_word  sh.sh_flags    )
    (string_of_elf32_addr  sh.sh_addr     )
    (string_of_elf32_off   sh.sh_offset   )
    (string_of_elf32_word  sh.sh_size     )
    (string_of_elf32_word  sh.sh_link     )
    (string_of_elf32_word  sh.sh_info     )
    (string_of_elf32_word  sh.sh_addralign)
    (string_of_elf32_word  sh.sh_entsize  )

let string_of_p_type = function
| PT_NULL    -> "PT_NULL"
| PT_LOAD    -> "PT_LOAD"
| PT_DYNAMIC -> "PT_DYNAMIC"
| PT_INTERP  -> "PT_INTERP"
| PT_NOTE    -> "PT_NOTE"
| PT_SHLIB   -> "PT_SHLIB"
| PT_PHDR    -> "PT_PHDR"
| PT_UNKNOWN -> "PT_UNKNOWN"

let string_of_elf32_phdr ph =
  Printf.sprintf
    "{
p_type   = %s;
p_offset = %s;
p_vaddr  = %s;
p_paddr  = %s;
p_filesz = %s;
p_memsz  = %s;
p_flags  = %s;
p_align  = %s;
}"
    (string_of_p_type     ph.p_type  )
    (string_of_elf32_off  ph.p_offset)
    (string_of_elf32_addr ph.p_vaddr )
    (string_of_elf32_addr ph.p_paddr )
    (string_of_elf32_word ph.p_filesz)
    (string_of_elf32_word ph.p_memsz )
    (string_of_bitstring  ph.p_flags )
    (string_of_elf32_word ph.p_align )

let string_of_elf32_st_bind = function
| STB_LOCAL   -> "STB_LOCAL"
| STB_GLOBAL  -> "STB_GLOBAL"
| STB_WEAK    -> "STB_WEAK"
| STB_UNKNOWN -> "STB_UNKNOWN"

let string_of_elf32_st_type = function
| STT_NOTYPE  -> "STT_NOTYPE"
| STT_OBJECT  -> "STT_OBJECT"
| STT_FUNC    -> "STT_FUNC"
| STT_SECTION -> "STT_SECTION"
| STT_FILE    -> "STT_FILE"
| STT_UNKNOWN -> "STT_UNKNOWN"

let string_of_elf32_sym s =
  Printf.sprintf
    "{
st_name  = %s;
st_value = %s;
st_size  = %s;
st_bind  = %s;
st_type  = %s;
st_other = %s;
st_shndx = %s;
}"
    (s.st_name                           )
    (string_of_elf32_addr      s.st_value)
    (string_of_elf32_word      s.st_size )
    (string_of_elf32_st_bind   s.st_bind )
    (string_of_elf32_st_type   s.st_type )
    (string_of_int             s.st_other)
    (string_of_elf32_half      s.st_shndx)

let string_of_elf e =
  Printf.sprintf
    "{
e_header   = %s;
e_sections = %s;
e_programs = %s;
e_symtab   = %s;
}"
    (string_of_elf32_ehdr e.e_hdr                        )
    (string_of_array string_of_elf32_shdr ",\n" e.e_shdra)
    (string_of_array string_of_elf32_phdr ",\n" e.e_phdra)
    (string_of_array string_of_elf32_sym ",\n" e.e_symtab)