Source file memory_image_orderings.ml
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
open Lem_basic_classes
open Lem_function
open Lem_string
open Lem_tuple
open Lem_bool
open Lem_list
open Lem_sorting
open Lem_map
open Lem_set
open Multimap
open Lem_num
open Lem_maybe
open Lem_assert_extra
open Show
open Byte_sequence
open Elf_file
open Elf_header
open Elf_interpreted_segment
open Elf_interpreted_section
open Elf_program_header_table
open Elf_section_header_table
open Elf_symbol_table
open Elf_types_native_uint
open Elf_relocation
open Memory_image
open Abi_classes
open Missing_pervasives
let elfFileFeatureCompare f1 f2:int=
(
(match (f1, f2) with
(ElfHeader(x1), ElfHeader(x2)) -> elf64_header_compare x1 x2
| (ElfHeader(x1), _) -> (-1)
| (ElfSectionHeaderTable(x1), ElfHeader(x2)) -> 1
| (ElfSectionHeaderTable(x1), ElfSectionHeaderTable(x2)) -> ( lexicographic_compare compare_elf64_section_header_table_entry x1 x2)
| (ElfSectionHeaderTable(x1), _) -> (-1)
| (ElfProgramHeaderTable(x1), ElfHeader(x2)) -> 1
| (ElfProgramHeaderTable(x1), ElfSectionHeaderTable(x2)) -> 1
| (ElfProgramHeaderTable(x1), ElfProgramHeaderTable(x2)) -> (lexicographic_compare compare_elf64_program_header_table_entry x1 x2)
| (ElfProgramHeaderTable(x1), _) -> (-1)
| (ElfSection(x1), ElfHeader(x2)) -> 1
| (ElfSection(x1), ElfSectionHeaderTable(x2)) -> 1
| (ElfSection(x1), ElfProgramHeaderTable(x2)) -> 1
| (ElfSection(x1), ElfSection(x2)) -> (pairCompare Nat_big_num.compare compare_elf64_interpreted_section x1 x2)
| (ElfSection(x1), _) -> (-1)
| (ElfSegment(x1), ElfHeader(x2)) -> 1
| (ElfSegment(x1), ElfSectionHeaderTable(x2)) -> 1
| (ElfSegment(x1), ElfProgramHeaderTable(x2)) -> 1
| (ElfSegment(x1), ElfSection(x2)) -> 1
| (ElfSegment(x1), ElfSegment(x2)) -> (pairCompare Nat_big_num.compare compare_elf64_interpreted_segment x1 x2)
| (ElfSegment(x1), _) -> (-1)
))
let elfFileFeatureTagEquiv f1 f2:bool=
(
(match (f1, f2) with
(ElfHeader(x1), ElfHeader(x2)) -> true
| (ElfSectionHeaderTable(x1), ElfSectionHeaderTable(x2)) -> true
| (ElfProgramHeaderTable(x1), ElfProgramHeaderTable(x2)) -> true
| (ElfSection(x1), ElfSection(x2)) -> true
| (ElfSegment(x1), ElfSegment(x2)) -> true
| (_, _) -> false
))
let instance_Basic_classes_Ord_Memory_image_elf_file_feature_dict:(elf_file_feature)ord_class= ({
compare_method = elfFileFeatureCompare;
isLess_method = (fun f1 -> (fun f2 -> ( Lem.orderingEqual(elfFileFeatureCompare f1 f2) (-1))));
isLessEqual_method = (fun f1 -> (fun f2 -> Pset.mem (elfFileFeatureCompare f1 f2)(Pset.from_list compare [(-1); 0])));
isGreater_method = (fun f1 -> (fun f2 -> ( Lem.orderingEqual(elfFileFeatureCompare f1 f2) 1)));
isGreaterEqual_method = (fun f1 -> (fun f2 -> Pset.mem (elfFileFeatureCompare f1 f2)(Pset.from_list compare [1; 0])))})
let tagCompare dict_Basic_classes_Ord_abifeature k1 k2:int=
((match (k1, k2) with
(ImageBase, ImageBase) -> 0
| (ImageBase, _) -> (-1)
| (EntryPoint, ImageBase) -> 1
| (EntryPoint, EntryPoint) -> 0
| (EntryPoint, _) -> (-1)
| (SymbolDef(_), ImageBase) -> 1
| (SymbolDef(_), EntryPoint) -> 1
| (SymbolDef(x1), SymbolDef(x2)) -> symDefCompare x1 x2
| (SymbolDef(_), _) -> (-1)
| (SymbolRef(_), ImageBase) -> 1
| (SymbolRef(_), EntryPoint) -> 1
| (SymbolRef(_), SymbolDef(_)) -> 1
| (SymbolRef(x1), SymbolRef(x2)) -> symRefAndRelocSiteCompare x1 x2
| (SymbolRef(_), _) -> (-1)
| (FileFeature(_), ImageBase) -> 1
| (FileFeature(_), EntryPoint) -> 1
| (FileFeature(_), SymbolDef(_)) -> 1
| (FileFeature(_), SymbolRef(_)) -> 1
| (FileFeature(x1), FileFeature(x2)) -> elfFileFeatureCompare x1 x2
| (FileFeature(_), _) -> (-1)
| (AbiFeature(_), ImageBase) -> 1
| (AbiFeature(_), EntryPoint) -> 1
| (AbiFeature(_), SymbolDef(_)) -> 1
| (AbiFeature(_), SymbolRef(_)) -> 1
| (AbiFeature(_), FileFeature(_)) -> 1
| (AbiFeature(x1), AbiFeature(x2)) ->
dict_Basic_classes_Ord_abifeature.compare_method x1 x2
| (AbiFeature(_), _) -> (-1)
))
let instance_Basic_classes_Ord_Memory_image_range_tag_dict dict_Basic_classes_Ord_abifeature:('abifeature range_tag)ord_class= ({
compare_method =
(tagCompare dict_Basic_classes_Ord_abifeature);
isLess_method = (fun tag1 -> (fun tag2 -> ( Lem.orderingEqual(tagCompare
dict_Basic_classes_Ord_abifeature tag1 tag2) (-1))));
isLessEqual_method = (fun tag1 -> (fun tag2 -> Pset.mem (tagCompare
dict_Basic_classes_Ord_abifeature tag1 tag2)(Pset.from_list compare [(-1); 0])));
isGreater_method = (fun tag1 -> (fun tag2 -> ( Lem.orderingEqual(tagCompare
dict_Basic_classes_Ord_abifeature tag1 tag2) 1)));
isGreaterEqual_method = (fun tag1 -> (fun tag2 -> Pset.mem (tagCompare
dict_Basic_classes_Ord_abifeature tag1 tag2)(Pset.from_list compare [1; 0])))})
let tagEquiv dict_Abi_classes_AbiFeatureTagEquiv_abifeature k1 k2:bool=
((match (k1, k2) with
(ImageBase, ImageBase) -> true
| (EntryPoint, EntryPoint) -> true
| (SymbolDef(x1), SymbolDef(x2)) -> true
| (SymbolRef(_), SymbolRef(_)) -> true
| (FileFeature(x1), FileFeature(x2)) -> elfFileFeatureTagEquiv x1 x2
| (AbiFeature(x1), AbiFeature(x2)) ->
dict_Abi_classes_AbiFeatureTagEquiv_abifeature.abiFeatureTagEquiv_method x1 x2
| (_, _) -> false
))
let unique_tag_matching dict_Basic_classes_Ord_abifeature dict_Abi_classes_AbiFeatureTagEquiv_abifeature tag img2:'abifeature range_tag=
((match Multimap.lookupBy0
(instance_Basic_classes_Ord_Memory_image_range_tag_dict
dict_Basic_classes_Ord_abifeature) (instance_Basic_classes_Ord_Maybe_maybe_dict
(instance_Basic_classes_Ord_tup2_dict
Lem_string_extra.instance_Basic_classes_Ord_string_dict
(instance_Basic_classes_Ord_tup2_dict
instance_Basic_classes_Ord_Num_natural_dict
instance_Basic_classes_Ord_Num_natural_dict))) instance_Basic_classes_SetType_var_dict (instance_Basic_classes_SetType_Maybe_maybe_dict
(instance_Basic_classes_SetType_tup2_dict
instance_Basic_classes_SetType_var_dict
(instance_Basic_classes_SetType_tup2_dict
instance_Basic_classes_SetType_Num_natural_dict
instance_Basic_classes_SetType_Num_natural_dict))) (tagEquiv dict_Abi_classes_AbiFeatureTagEquiv_abifeature) tag img2.by_tag with
[] -> failwith "no tag match found"
| [(t, r)] -> t
| x -> failwith ("more than one tag match")
))
let tagged_ranges_matching_tag dict_Basic_classes_Ord_abifeature dict_Abi_classes_AbiFeatureTagEquiv_abifeature tag img2:('abifeature range_tag*(element_range)option)list=
(Multimap.lookupBy0
(instance_Basic_classes_Ord_Memory_image_range_tag_dict
dict_Basic_classes_Ord_abifeature) (instance_Basic_classes_Ord_Maybe_maybe_dict
(instance_Basic_classes_Ord_tup2_dict
Lem_string_extra.instance_Basic_classes_Ord_string_dict
(instance_Basic_classes_Ord_tup2_dict
instance_Basic_classes_Ord_Num_natural_dict
instance_Basic_classes_Ord_Num_natural_dict))) instance_Basic_classes_SetType_var_dict (instance_Basic_classes_SetType_Maybe_maybe_dict
(instance_Basic_classes_SetType_tup2_dict
instance_Basic_classes_SetType_var_dict
(instance_Basic_classes_SetType_tup2_dict
instance_Basic_classes_SetType_Num_natural_dict
instance_Basic_classes_SetType_Num_natural_dict))) (tagEquiv dict_Abi_classes_AbiFeatureTagEquiv_abifeature) tag img2.by_tag)
let element_range_compare:string*(Nat_big_num.num*Nat_big_num.num) ->string*(Nat_big_num.num*Nat_big_num.num) ->int= (pairCompare compare (pairCompare Nat_big_num.compare Nat_big_num.compare))
let unique_tag_matching_at_range_exact dict_Basic_classes_Ord_abifeature dict_Abi_classes_AbiFeatureTagEquiv_abifeature r tag img2:'abifeature range_tag=
(
let (_, (allRangeMatches : ( 'abifeature range_tag) list)) = (List.split (Multimap.lookupBy0
(instance_Basic_classes_Ord_Maybe_maybe_dict
(instance_Basic_classes_Ord_tup2_dict
Lem_string_extra.instance_Basic_classes_Ord_string_dict
(instance_Basic_classes_Ord_tup2_dict
instance_Basic_classes_Ord_Num_natural_dict
instance_Basic_classes_Ord_Num_natural_dict))) (instance_Basic_classes_Ord_Memory_image_range_tag_dict
dict_Basic_classes_Ord_abifeature) (instance_Basic_classes_SetType_Maybe_maybe_dict
(instance_Basic_classes_SetType_tup2_dict
instance_Basic_classes_SetType_var_dict
(instance_Basic_classes_SetType_tup2_dict
instance_Basic_classes_SetType_Num_natural_dict
instance_Basic_classes_SetType_Num_natural_dict))) instance_Basic_classes_SetType_var_dict (Lem.option_equal (Lem.pair_equal (=) (Lem.pair_equal Nat_big_num.equal Nat_big_num.equal))) r img2.by_range))
in
let (tagAlsoMatches : ( 'abifeature range_tag) list) = (List.filter (fun x -> tagEquiv
dict_Abi_classes_AbiFeatureTagEquiv_abifeature x tag) allRangeMatches)
in
(match tagAlsoMatches with
[] -> failwith "no range/tag match when asserted to exist"
| [x] -> x
| _ -> failwith "multiple range/tag match when asserted unique"
))
let symbol_def_ranges dict_Basic_classes_Ord_abifeature dict_Abi_classes_AbiFeatureTagEquiv_abifeature img2:('abifeature range_tag)list*((element_range)option)list=
(
let (tags, maybe_ranges) = (List.split (
tagged_ranges_matching_tag
dict_Basic_classes_Ord_abifeature dict_Abi_classes_AbiFeatureTagEquiv_abifeature (SymbolDef(null_symbol_definition)) img2
))
in
(tags, maybe_ranges))
let name_of_symbol_def sym1:string= (sym1.def_symname)
let defined_symbols_and_ranges dict_Basic_classes_Ord_abifeature dict_Abi_classes_AbiFeatureTagEquiv_abifeature img2:((element_range)option*symbol_definition)list=
(Lem_list.mapMaybe (fun (tag, maybeRange) ->
(match tag with
SymbolDef(ent) -> Some (maybeRange, ent)
| _ -> failwith "impossible: non-symbol def in list of symbol defs"
)) (tagged_ranges_matching_tag
dict_Basic_classes_Ord_abifeature dict_Abi_classes_AbiFeatureTagEquiv_abifeature (SymbolDef(null_symbol_definition)) img2))
let make_ranges_definite rs:(string*range)list=
(Lem_list.map (fun (maybeR : element_range option) -> (match maybeR with
Some r -> r
| None -> failwith "impossible: range not definite, but asserted to be"
)) rs)
let find_defs_matching bound_def ranges_and_defs:((element_range)option*symbol_definition)list=
(
Lem_list.mapMaybe (fun (maybe_some_range, some_def) ->
if some_def = bound_def
then (
Some(maybe_some_range, some_def)
)
else if some_def.def_symname = bound_def.def_symname then
None
else None
) ranges_and_defs)
let defined_symbols dict_Basic_classes_Ord_abifeature dict_Abi_classes_AbiFeatureTagEquiv_abifeature img2:(symbol_definition)list=
(let (all_symbol_tags, all_symbol_ranges) = (symbol_def_ranges
dict_Basic_classes_Ord_abifeature dict_Abi_classes_AbiFeatureTagEquiv_abifeature img2) in
Lem_list.mapMaybe (fun tag ->
(match tag with
SymbolDef(ent) -> Some ent
| _ -> failwith "impossible: non-symbol def in list of symbol defs"
)) all_symbol_tags)
let default_get_reloc_symaddr dict_Basic_classes_Ord_abifeature dict_Abi_classes_AbiFeatureTagEquiv_abifeature bound_def_in_input output_img ranges_and_defs maybe_reloc1:Nat_big_num.num=
((match find_defs_matching bound_def_in_input ranges_and_defs with
[] -> failwith ("internal error: bound-to symbol (name `" ^ (bound_def_in_input.def_symname ^ "') not defined"))
| (maybe_range, d) :: more ->
let v =
((match maybe_range with
Some(el_name, (start, len)) ->
(match element_and_offset_to_address (el_name, start) output_img with
Some a -> a
| None -> failwith "internal error: could not get address for symbol"
)
| None ->
if Nat_big_num.equal (Uint32_wrapper.to_bigint d.def_syment.elf64_st_shndx) shn_abs
then Ml_bindings.nat_big_num_of_uint64 d.def_syment.elf64_st_value
else failwith "no range for non-ABS symbol"
))
in
(match more with
[] -> v
| _ -> v
)
))