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

type elf32_addr     = int32
type elf32_half     = int
type elf32_off      = int32
type elf32_sword    = int32
type elf32_word     = int32
type byte           = int

(** ELF identification *)

type elfclass =
  | ELFCLASSNONE
  | ELFCLASS32
  | ELFCLASS64
  | ELFCLASSUNKNOWN

type elfdata =
  | ELFDATANONE
  | ELFDATA2LSB
  | ELFDATA2MSB
  | ELFDATAUNKNOWN

type ev =
  | EV_NONE
  | EV_CURRENT
  | EV_UNKNOWN

type elf_identification =
    { ei_class   : elfclass (* 32/64 bit *)
    ; ei_data    : elfdata  (* endianness *)
    ; ei_version : ev       (* ELF header version *)
    }

(** ELF header *)

type et =
  | ET_NONE
  | ET_REL
  | ET_EXEC
  | ET_DYN
  | ET_CORE
  | ET_UNKNOWN

type em =
  | EM_NONE
  | EM_M32
  | EM_SPARC
  | EM_386
  | EM_68K
  | EM_88K
  | EM_860
  | EM_MIPS
  | EM_MIPS_RS4_BE
  | EM_PPC
  | EM_UNKNOWN

let shn_UNDEF  = 0
let shn_ABS    = 0xFFF1
let shn_COMMON = 0xFFF2

type elf32_ehdr =
    { e_ident     : elf_identification  (* Machine-independent data *)
    ; e_type      : et                  (* Object file type *)
    ; e_machine   : em                  (* Required architecture *)
    ; e_version   : ev                  (* Object file version *)
    ; e_entry     : elf32_addr          (* Entry point virtual address *)
    ; e_phoff     : elf32_off           (* Program header table's offset *)
    ; e_shoff     : elf32_off           (* Section header table's offset *)
    ; e_flags     : Bitstring.bitstring (* Processor-specific flags *)
    ; e_ehsize    : elf32_half          (* ELF header size *)
    ; e_phentsize : elf32_half          (* Size of a program header's entry *)
    ; e_phnum     : elf32_half          (* Number of program header entries *)
    ; e_shentsize : elf32_half          (* Size of a section header's entry *)
    ; e_shnum     : elf32_half          (* Number of section header entries *)
    ; e_shstrndx  : elf32_half          (* Section name string table index *)
    }

(** ELF section header *)

type sht =
  | SHT_NULL
  | SHT_PROGBITS
  | SHT_SYMTAB
  | SHT_STRTAB
  | SHT_RELA
  | SHT_HASH
  | SHT_DYNAMIC
  | SHT_NOTE
  | SHT_NOBITS
  | SHT_REL
  | SHT_SHLIB
  | SHT_DYNSYM
  | SHT_UNKNOWN

type elf32_shdr =
    { sh_name      : string
    ; sh_type      : sht
    ; sh_flags     : elf32_word
    ; sh_addr      : elf32_addr
    ; sh_offset    : elf32_off
    ; sh_size      : elf32_word
    ; sh_link      : elf32_word
    ; sh_info      : elf32_word
    ; sh_addralign : elf32_word
    ; sh_entsize   : elf32_word
    }

let shf_WRITE = 0x1l
let shf_ALLOC = 0x2l
let shf_EXECINSTR = 0x4l

type elf32_st_bind =
  | STB_LOCAL
  | STB_GLOBAL
  | STB_WEAK
  | STB_UNKNOWN

type elf32_st_type =
  | STT_NOTYPE
  | STT_OBJECT
  | STT_FUNC
  | STT_SECTION
  | STT_FILE
  | STT_UNKNOWN

type elf32_sym =
    { st_name  : string
    ; st_value : elf32_addr
    ; st_size  : elf32_word
    ; st_bind  : elf32_st_bind
    ; st_type  : elf32_st_type
    ; st_other : byte
    ; st_shndx : elf32_half
    }

(** ELF program header *)

type p_type =
  | PT_NULL
  | PT_LOAD
  | PT_DYNAMIC
  | PT_INTERP
  | PT_NOTE
  | PT_SHLIB
  | PT_PHDR
  | PT_UNKNOWN

type elf32_phdr =
    { p_type   : p_type
    ; p_offset : elf32_off
    ; p_vaddr  : elf32_addr
    ; p_paddr  : elf32_addr
    ; p_filesz : elf32_word
    ; p_memsz  : elf32_word
    ; p_flags  : bitstring
    ; p_align  : elf32_word
    }

(** ELF *)
type elf =
    { e_bitstring    : bitstring
    ; e_hdr          : elf32_ehdr
    ; e_shdra        : elf32_shdr array
    ; e_phdra        : elf32_phdr array
    ; e_symtab       : elf32_sym array
    ; e_symtab_sndx  : int (* section index of the symbol table *)
    ; e_sym_phdr     : int32 -> int option (* fast sym -> phdr lookup *)
    ; e_syms_by_name : int list StringMap.t (* fast name -> sym lookup *)
    }