Source file linkable_list.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
open Lem_basic_classes
open Lem_function
open Lem_string
open Lem_string_extra
open Lem_tuple
open Lem_bool
open Lem_list
open Lem_list_extra
open Lem_set
open Lem_set_extra
open Lem_sorting
open Lem_num
open Lem_maybe
open Lem_assert_extra
open Byte_sequence
open Default_printing
open Error
open Missing_pervasives
open Show
open Elf_types_native_uint
open Elf_memory_image
open Elf_header
open Elf_file
open Memory_image
open Elf_memory_image
open Elf_section_header_table
open Elf_symbol_table
open String_table
open Input_list
open Elf_memory_image
open Elf_memory_image_of_elf64_file
type script = byte_sequence0
type linkable_object = RelocELF of elf_memory_image
| SharedELF of elf_memory_image
| ScriptAST of script
| ControlScriptDefs
let string_of_linkable_object l:string= ((match l with
RelocELF(_) -> "a relocatable file (...)"
| SharedELF(_) -> "a shared library (...)"
| ScriptAST(_) -> "a linker script (...)"
| ControlScriptDefs -> "the control script"
))
type linkable_item = linkable_object * input_item * input_options
let short_string_of_linkable_item item:string=
(let (obj, inp, opts) = item
in
short_string_of_input_item inp)
let instance_Show_Show_Linkable_list_linkable_object_dict:(linkable_object)show_class= ({
show_method = string_of_linkable_object})
type linkable_list = linkable_item list
type symbol_resolution_oracle = linkable_list -> int -> string -> int list
type binding = (Nat_big_num.num * symbol_reference * linkable_item) * (Nat_big_num.num * symbol_definition * linkable_item)option
type binding_list = binding list
type binding_map = (string, ( (Nat_big_num.num * binding)list)) Pmap.map
let image_of_linkable_item item:(Abis.any_abi_feature)annotated_memory_image= ((match item with
(RelocELF(image), _, _) -> image
| (SharedELF(image), _, _) -> image
| _ -> failwith "no image"
))
let linkable_item_of_input_item_and_options a it opts:linkable_object*(string*input_blob*(Command_line.input_unit*(origin_coord)list))*input_options=
((match ((match it with
(fname1, Reloc(seq), origin) ->
let _ = (prerr_endline ("Considering relocatable file " ^ fname1)) in bind (Elf_file.read_elf64_file seq) (fun e ->
return (RelocELF(elf_memory_image_of_elf64_file a fname1 e), it, opts))
| (fname1, Shared(seq), origin) ->
fail "unsupported input item"
| (fname1, Script(seq), origin) ->
fail "unsupported input item"
))
with
Success(item) -> item
| Fail(str) -> failwith (str ^ ": non-ELF or non-relocatable input file")
))
let string_of_linkable l:string= ((match l with
(_, item, _) -> string_of_triple
instance_Show_Show_string_dict instance_Show_Show_Input_list_input_blob_dict (instance_Show_Show_tup2_dict
Command_line.instance_Show_Show_Command_line_input_unit_dict
(instance_Show_Show_list_dict
instance_Show_Show_Input_list_origin_coord_dict)) item
))
let add_definition_to_map def_idx_and_def_and_linkable m:((string),((Nat_big_num.num*symbol_definition*(linkable_object*input_item*input_options))list))Pmap.map=
(let (def_idx, def, def_linkable) = def_idx_and_def_and_linkable
in
(match Pmap.lookup def.def_symname m with
Some curlist -> Pmap.add def.def_symname ((def_idx, def, def_linkable) :: curlist) m
| None -> Pmap.add def.def_symname [(def_idx, def, def_linkable)] m
))
let all_definitions_by_name linkables:((string),((Nat_big_num.num*symbol_definition*linkable_item)list))Pmap.map=
(
let list_of_deflists = (Lem_list.mapi (fun (idx1 : int) -> (fun (item : linkable_item) ->
let img2 = (image_of_linkable_item item)
in
let (all_def_tags, all_def_ranges)
= (List.split (Multimap.lookupBy0
(Memory_image_orderings.instance_Basic_classes_Ord_Memory_image_range_tag_dict
Abis.instance_Basic_classes_Ord_Abis_any_abi_feature_dict) (instance_Basic_classes_Ord_Maybe_maybe_dict
(instance_Basic_classes_Ord_tup2_dict
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))) (Memory_image_orderings.tagEquiv
Abis.instance_Abi_classes_AbiFeatureTagEquiv_Abis_any_abi_feature_dict) (SymbolDef(null_symbol_definition)) img2.by_tag))
in
let all_defs = (Lem_list.map (fun tag -> (match tag with
SymbolDef(def) -> (def, item)
| _ -> failwith "matched tag not a symbol definition"
)) all_def_tags)
in
let x2 = ([]) in List.fold_right
(fun(def, def_linkable) x2 ->
if true then (Nat_big_num.of_int idx1, def, def_linkable) :: x2 else x2)
all_defs x2
)) linkables)
in
List.fold_left (fun accum -> (fun deflist ->
List.fold_left (fun m -> (fun (def_idx, def, def_linkable) -> add_definition_to_map (def_idx, def, def_linkable) m)) accum deflist
)) (Pmap.empty compare) list_of_deflists)
type binding_oracle =
linkable_list
-> (string, ( (Nat_big_num.num * symbol_definition * linkable_item)list)) Pmap.map
-> (Nat_big_num.num * symbol_reference * linkable_item)
-> (Nat_big_num.num * symbol_definition * linkable_item)option
let resolve_one_reference_default a linkables defmap ref_idx_and_ref_and_linkable:(Nat_big_num.num*symbol_definition*(linkable_object*(string*input_blob*(Command_line.input_unit*(origin_coord)list))*input_options))option=
(let (ref_idx, ref1, ref_linkable) = ref_idx_and_ref_and_linkable
in
let (defs_and_linkables_with_matching_name : (Nat_big_num.num * symbol_definition * linkable_item) list)
= ((match Pmap.lookup ref1.ref_symname defmap with
Some (l : ( (Nat_big_num.num * symbol_definition * linkable_item)list)) -> l
| None -> []
))
in
let (ref_obj, (ref_fname, ref_blob, (ref_u, ref_coords)), ref_options) = ref_linkable
in
let ref_is_weak = (Nat_big_num.equal (get_elf64_symbol_binding ref1.ref_syment) stb_weak)
in
let def_is_eligible = (fun (def_idx, def, def_linkable) ->
let ref_is_unnamed = (ref1.ref_symname = "")
in
let ref_is_to_defined_or_common_symbol = ( not (Nat_big_num.equal (Uint32_wrapper.to_bigint ref1.ref_syment.elf64_st_shndx) stn_undef))
in
let def_sym_is_ref_sym = ( Nat_big_num.equal ref_idx def_idx && (Nat_big_num.equal ref1.ref_sym_scn def.def_sym_scn
&& Nat_big_num.equal ref1.ref_sym_idx def.def_sym_idx))
in
let (def_obj, (def_fname, def_blob, def_origin), def_options) = def_linkable
in
let (def_u, def_coords) = def_origin
in
let (def_in_group, def_in_archive) = ((match def_coords with
InArchive(aid, aidx, _, _) :: InGroup(gid1, gidx) :: [_] -> (Some gid1, Some aid)
| InArchive(aid, aidx, _, _) :: [_] -> (None, Some aid)
| InGroup(gid1, gidx) :: [_] -> (Some gid1, None)
| [_] -> (None, None)
| _ -> failwith "internal error: didn't understand origin coordinates of definition"
))
in
let ref_is_leftmore = (Nat_big_num.less_equal ref_idx def_idx)
in
let ref_is_in_group_with_def = ((match def_in_group with
None -> false
| Some def_gid ->
(match ref_coords with
InArchive(_, _, _, _) :: InGroup(gid1, _) :: [_] -> Nat_big_num.equal gid1 def_gid
| InGroup(gid1, _) :: [_] -> Nat_big_num.equal gid1 def_gid
| _ -> false
)
))
in
let _ =
(if (ref_fname = "backtrace.o") && (def.def_symname = "_Unwind_GetCFA") then
()
else ())
in
let ref_and_def_are_in_same_archive = ((match (def_coords, ref_coords) with
(InArchive(x1, _, _, _) :: _, InArchive(x2, _, _, _) :: _) -> Nat_big_num.equal x1 x2
| _ -> false
))
in
let def_is_in_archive = ((match def_in_archive with
Some _ -> true
| None -> false
))
in
if ref_is_to_defined_or_common_symbol then def_sym_is_ref_sym
else
if ref_is_unnamed then false
else
if def_is_in_archive
then
((not ref_is_weak) || ref_and_def_are_in_same_archive)
&& (
ref_is_leftmore
|| (ref_and_def_are_in_same_archive
|| ref_is_in_group_with_def)
)
else
true
)
in
let eligible_defs = (List.filter def_is_eligible defs_and_linkables_with_matching_name)
in
let (maybe_target_def_idx, maybe_target_def, maybe_target_def_linkable) = ((match eligible_defs with
[] -> (None, None, None)
| [(def_idx, def, def_linkable)] -> (Some def_idx, Some def, Some def_linkable)
| (d_idx, d, d_l) :: more_pairs ->
let sorted = (insertSortBy (fun (d_idx1, d1, (_, (_, _, (_, d_l1_coords)), _)) -> (fun (d_idx2, d2, (_, (_, _, (_, d_l2_coords)), _)) ->
(match (d_l1_coords, d_l2_coords) with
(InCommandLine(_) :: _, InCommandLine(_) :: _) -> Nat_big_num.less d_idx1 d_idx2
| (InCommandLine(_) :: _, _) -> true
| (_, InCommandLine(_) :: _) -> false
| (_, _) -> Nat_big_num.less d_idx1 d_idx2
))) eligible_defs)
in
(match sorted with
(first_d_idx, first_d, first_d_l) :: _ -> (Some first_d_idx, Some first_d, Some first_d_l)
| _ -> failwith "impossible: sorted list is shorter than original"
)
))
in
let refstr = ("`"
^ (ref1.ref_symname ^ ("' (" ^
((if Nat_big_num.equal (Uint32_wrapper.to_bigint ref1.ref_syment.elf64_st_shndx) shn_undef then "UND" else "defined") ^
(" symbol at index " ^ ((Nat_big_num.to_string ref1.ref_sym_idx) ^ (" in symtab "
^ ((Nat_big_num.to_string ref1.ref_sym_scn) ^ (" in " ^ (ref_fname
^ ")"))))))))))
in
(match (maybe_target_def_idx, maybe_target_def, maybe_target_def_linkable) with
(Some target_def_idx, Some target_def, Some target_def_linkable) ->
Some(target_def_idx, target_def, target_def_linkable)
| (None, None, None) ->
if ref_is_weak then None
else None
| _ -> failwith "impossible: non-matching maybes for target_def_idx and target_def"
))
let resolve_all linkables all_defs oracle refs:((Nat_big_num.num*symbol_reference*(linkable_object*input_item*input_options))*(Nat_big_num.num*symbol_definition*linkable_item)option)list=
(Lem_list.map (fun (ref_idx, ref1, ref_linkable) -> ((ref_idx, ref1, ref_linkable), (oracle linkables all_defs (ref_idx, ref1, ref_linkable)))) refs)
let resolve_undefs_in_one_object linkables all_defs oracle idx1:((Nat_big_num.num*symbol_reference*linkable_item)*(Nat_big_num.num*symbol_definition*linkable_item)option)list=
(
let item = ((match Lem_list.list_index linkables (Nat_big_num.to_int idx1) with
Some it -> it
| None -> failwith "impossible: linkable not in list of linkables"
))
in
let img2 = (image_of_linkable_item item)
in
let (all_ref_tags, all_ref_ranges)
= (List.split (Multimap.lookupBy0
(Memory_image_orderings.instance_Basic_classes_Ord_Memory_image_range_tag_dict
Abis.instance_Basic_classes_Ord_Abis_any_abi_feature_dict) (instance_Basic_classes_Ord_Maybe_maybe_dict
(instance_Basic_classes_Ord_tup2_dict
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))) (Memory_image_orderings.tagEquiv
Abis.instance_Abi_classes_AbiFeatureTagEquiv_Abis_any_abi_feature_dict) (SymbolRef(null_symbol_reference_and_reloc_site)) img2.by_tag))
in
let all_refs = (Pset.from_list compare (Lem_list.map (fun tag -> (match tag with
SymbolRef(r) -> r.ref
| _ -> failwith "matched tag not a relocation site"
)) all_ref_tags))
in
let ref_triples = (let x2 =(Pset.from_list (tripleCompare Nat_big_num.compare compare (tripleCompare compare (tripleCompare compare compare (pairCompare compare (lexicographic_compare compare))) compare))
[]) in Pset.fold
(fun ref1 x2 -> if true then Pset.add (idx1, ref1, item) x2 else x2)
all_refs x2)
in
let und_ref_triples = (let x2 =(Pset.from_list (tripleCompare Nat_big_num.compare compare (tripleCompare compare (tripleCompare compare compare (pairCompare compare (lexicographic_compare compare))) compare))
[]) in Pset.fold
(fun(idx1, ref1, ref_item) x2 ->
if Nat_big_num.equal
(Uint32_wrapper.to_bigint ref1.ref_syment.elf64_st_shndx) shn_undef then
Pset.add (idx1, ref1, ref_item) x2 else x2) ref_triples x2)
in
resolve_all linkables all_defs oracle (Pset.elements ref_triples))
let rec accumulate_bindings_bf a linkables all_defs fully_bound to_bind bindings_accum:((Nat_big_num.num*symbol_reference*linkable_item)*(Nat_big_num.num*symbol_definition*linkable_item)option)list=
(
(match to_bind with
[] -> bindings_accum
| l_idx :: more_idx ->
let new_bindings = (resolve_undefs_in_one_object
linkables
all_defs
(resolve_one_reference_default a)
l_idx)
in
let new_fully_bound = (Pset.add l_idx fully_bound)
in
let new_bindings_def_idx = (list_concat_map (fun (ref1, maybe_def_and_idx_and_linkable) ->
(match maybe_def_and_idx_and_linkable with
Some (def_idx, def, def_linkable) -> [def_idx]
| None -> []
)
) new_bindings)
in
let new_bindings_def_idx_set = (Pset.from_list Nat_big_num.compare new_bindings_def_idx)
in
let included_linkables_idx = (Pset.(union) fully_bound ((Pset.from_list Nat_big_num.compare to_bind)))
in
let new_l_idx = (Pset.diff new_bindings_def_idx_set included_linkables_idx)
in
let new_l_idx_list = (Pset.elements new_l_idx)
in
accumulate_bindings_bf
a
linkables
all_defs
new_fully_bound
( List.rev_append (List.rev more_idx) new_l_idx_list)
( List.rev_append (List.rev bindings_accum) new_bindings)
))
let rec accumulate_bindings_objectwise_df a linkables all_defs bindings_accum blacks greys:((Nat_big_num.num*symbol_reference*linkable_item)*(Nat_big_num.num*symbol_definition*linkable_item)option)list=
((match greys with
[] -> bindings_accum
| l_idx :: more_idx ->
let new_bindings = (resolve_undefs_in_one_object
linkables
all_defs
(resolve_one_reference_default a)
l_idx)
in
let new_fully_bound = (Pset.add l_idx blacks)
in
let new_bindings_def_idx = (list_concat_map (fun (ref1, maybe_def_and_idx_and_linkable) ->
(match maybe_def_and_idx_and_linkable with
Some (def_idx, def, def_linkable) -> [def_idx]
| None -> []
)
) new_bindings)
in
let new_bindings_def_idx_set = (Pset.from_list Nat_big_num.compare new_bindings_def_idx)
in
let included_linkables_idx = (Pset.(union) blacks ((Pset.from_list Nat_big_num.compare greys)))
in
let new_l_idx = (Pset.diff new_bindings_def_idx_set included_linkables_idx)
in
let new_l_idx_list = (Pset.elements new_l_idx)
in
let new_grey_list = (List.rev_append (List.rev new_l_idx_list) (List.filter (fun x -> not ( Pset.mem x new_l_idx)) more_idx))
in
accumulate_bindings_objectwise_df
a
linkables
all_defs
( List.rev_append (List.rev bindings_accum) new_bindings)
(new_fully_bound : Nat_big_num.num Pset.set)
(new_grey_list : Nat_big_num.num list)
))