Source file link.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
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 Lem_num
open Lem_maybe
open Lem_assert_extra
open Byte_sequence
open Default_printing
open Error
open Missing_pervasives
open Show
open Endianness
open Elf_header
open Elf_interpreted_section
open Elf_interpreted_segment
open Elf_section_header_table
open Elf_program_header_table
open Elf_symbol_table
open Elf_types_native_uint
open Elf_relocation
open Abis
open Abi_amd64_relocation
open Input_list
open Linkable_list
open Memory_image
open Memory_image_orderings
open Elf_memory_image
open Elf_memory_image_of_elf64_file
open Linker_script
let all_common_symbols img2:(symbol_definition)list= (List.filter (fun def -> Nat_big_num.equal
(Uint32_wrapper.to_bigint def.def_syment.elf64_st_shndx) shn_common
) (elf_memory_image_defined_symbols img2))
let def_is_in_reloc def_item:bool= ((match def_item with
(RelocELF(_), _, _) -> true
| (ScriptAST(_), _, _) -> true
| _ -> false
))
let retrieve_binding_for_ref dict_Basic_classes_Eq_b r r_linkable_idx item bindings_by_name:('b*symbol_reference*'d)*'c=
(let maybe_found_bs = (Pmap.lookup r.ref.ref_symname bindings_by_name)
in
(match maybe_found_bs with
None -> failwith ("impossible: list of bindings does not include symbol reference `" ^ (r.ref.ref_symname ^ "` (map empty)"))
| Some bis_and_bs -> (match List.filter (fun (b_idx, ((b_ref_idx, b_ref, b_ref_item), b_maybe_def)) ->
if dict_Basic_classes_Eq_b.isEqual_method b_ref_idx r_linkable_idx && (b_ref = r.ref) then
true
else false) bis_and_bs with
[] -> failwith ("impossible: list of bindings does not include symbol reference `" ^ (r.ref.ref_symname ^ "` (filtered list empty)"))
| [(bi, b)] -> b
| _ -> failwith ("impossible: list of bindings binds reference to symbol `"
^ (r.ref.ref_symname ^ "' more than one way (filtered list has >1 element)"))
)
))
type reloc_site_resolution = reloc_site * binding * reloc_decision
let mark_fate_of_relocs linkable_idx a options bindings_by_name item img2:(reloc_site*((Nat_big_num.num*symbol_reference*(linkable_object*input_item*input_options))*(Nat_big_num.num*symbol_definition*(linkable_object*input_item*input_options))option)*reloc_decision)list*(any_abi_feature)annotated_memory_image=
(
let building_executable = (Pset.mem (Command_line.OutputKind(Command_line.Executable)) options) in
let building_shared_library = (Pset.mem (Command_line.OutputKind(Command_line.SharedLibrary)) options) in
let bind_functions_early = (Pset.mem Command_line.BindFunctionsEarly options) in
let bind_non_functions_early = (Pset.mem Command_line.BindNonFunctionsEarly options) in
let (new_by_tag, rev_decisions) = (List.fold_left (fun (acc_by_tag, rev_acc_decisions) -> (fun (tag, maybe_range) ->
let pass_through = (Pset.add (tag, maybe_range) acc_by_tag, rev_acc_decisions)
in
(match tag with
SymbolRef(r) ->
(match r.maybe_reloc with
Some reloc1 ->
let (binding_is_final : Command_line.link_option Pset.set -> binding -> bool)
= (fun options -> (fun ((ref_idx, ref1, ref_item), maybe_def) ->
(match maybe_def with
None -> true
| Some (def_idx, def, def_item) -> Nat_big_num.equal
(
get_elf64_symbol_binding def.def_syment) stb_local
||
(
Pset.mem (get_symbol_visibility def.def_syment.elf64_st_info)(Pset.from_list Nat_big_num.compare [ stv_hidden; stv_protected; stv_internal ])
||
(
(building_executable && def_is_in_reloc def_item) ||
(building_shared_library && (def_is_in_reloc def_item &&
( ( Nat_big_num.equal(get_elf64_symbol_type def.def_syment) stt_func && bind_functions_early)
|| ( not (Nat_big_num.equal (get_elf64_symbol_type def.def_syment) stt_func) && bind_non_functions_early)
))
)))
)))
in
let (reloc_is_absolute : reloc_site -> bool) = (fun rs ->
let (kind, _) = (a.parse_reloc_info rs.ref_relent.elf64_ra_info) in
let (is_abs, _) = (a.reloc kind) in
is_abs)
in
let b = (retrieve_binding_for_ref
instance_Basic_classes_Eq_Num_natural_dict r linkable_idx item bindings_by_name)
in
let ((ref_idx, _, ref_item), maybe_def) = b
in
let defined_in_shared_lib = ((match maybe_def with
Some (def_idx, def, def_item) -> not (def_is_in_reloc def_item)
| None -> false
))
in
let decide = (fun decision -> (
Pset.add (SymbolRef({
ref = (r.ref)
; maybe_reloc = (r.maybe_reloc)
; maybe_def_bound_to = (Some (decision,
(match maybe_def with
Some(def_idx, def, def_item) ->
Some { def_symname = (def.def_symname)
; def_syment = (def.def_syment)
; def_sym_scn = (def.def_sym_scn)
; def_sym_idx = (def.def_sym_idx)
; def_linkable_idx = def_idx
}
| None -> None
)
))
}
), maybe_range) acc_by_tag,
((reloc1, b, decision) :: rev_acc_decisions)))
in
if (building_executable && defined_in_shared_lib)
|| (building_shared_library && (reloc_is_absolute reloc1))
then decide LeaveReloc
else
if binding_is_final options b then decide ApplyReloc
else if building_shared_library then decide (ChangeRelocTo( (Nat_big_num.of_int 0), r.ref, reloc1))
else failwith "didn't know what to do with relocation"
| None ->
pass_through
)
| _ -> pass_through
)
)) ((Pset.from_list (pairCompare compare (maybeCompare (pairCompare compare (pairCompare Nat_big_num.compare Nat_big_num.compare)))) []), []) (Pset.elements img2.by_tag))
in
(List.rev rev_decisions, { elements = (img2.elements)
; by_tag = new_by_tag
; by_range = (by_range_from_by_tag
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))) new_by_tag)
}))
let strip_metadata_sections reloc_decisions a img2:(any_abi_feature)annotated_memory_image=
(let (section_tags, section_ranges) = (elf_memory_image_section_ranges img2)
in
let rel_sections = (Lem_list.mapMaybe (fun (range_tag1, (el_name, el_range)) ->
(match range_tag1 with
FileFeature(ElfSection(idx1, isec1)) ->
if Pset.mem isec1.elf64_section_type(Pset.from_list Nat_big_num.compare [ sht_rel; sht_rela ])
then Some (idx1, isec1, el_name)
else None
| _ -> None
)
) (list_combine section_tags section_ranges))
in
let discarded_sections_with_element_name = (Lem_list.mapMaybe (fun (range_tag1, (el_name, el_range)) ->
(match range_tag1 with
FileFeature(ElfSection(idx1, isec1)) ->
if a.section_is_special isec1 img2
then Some (el_name, range_tag1) else None
)
) (list_combine section_tags section_ranges))
in
let discarded_elements_map = (List.fold_left (fun m -> (fun (el_name, range_tag1) ->
Pmap.add el_name range_tag1 m
)) (Pmap.empty compare) discarded_sections_with_element_name)
in
let filtered_image = (Memory_image.filter_elements (fun (el_name, el) -> not (Pmap.mem el_name discarded_elements_map)) img2)
in
let new_reloc_section_length = (fun idx1 -> (fun isec1 ->
let retained_relocs_from_this_section = (let x2 =
([]) in List.fold_right
(fun(reloc1, b, decision) x2 ->
if Nat_big_num.equal reloc1.ref_rel_scn
idx1 && (decision = LeaveReloc) then
(reloc1, b, decision) :: x2 else x2) reloc_decisions x2)
in Nat_big_num.mul (length retained_relocs_from_this_section) isec1.elf64_section_entsize
))
in
let (new_reloc_elements, new_reloc_tags_and_ranges) = (List.split (let x2 =
([]) in List.fold_right
(fun(idx1, isec1, el_name) x2 ->
if Nat_big_num.greater (new_reloc_section_length idx1 isec1)
( (Nat_big_num.of_int 0)) then
(let new_len = (new_reloc_section_length idx1 isec1) in
let new_el = ({ startpos = None ; length1 = (Some new_len); contents =
([]) }) in
let new_isec = ({ elf64_section_name = (isec1.elf64_section_name)
; elf64_section_type = (isec1.elf64_section_type)
; elf64_section_flags = (isec1.elf64_section_flags)
; elf64_section_addr =( (Nat_big_num.of_int 0))
; elf64_section_offset =( (Nat_big_num.of_int 0))
; elf64_section_size = new_len
; elf64_section_link = (isec1.elf64_section_link)
; elf64_section_info = (isec1.elf64_section_info)
; elf64_section_align = (isec1.elf64_section_align)
; elf64_section_entsize = (isec1.elf64_section_entsize)
; elf64_section_body = Byte_sequence.empty
; elf64_section_name_as_string = (isec1.elf64_section_name_as_string)
}) in
let new_meta = (FileFeature (ElfSection (idx1, new_isec))) in
((el_name, new_el), (new_meta, Some
(el_name, ( (Nat_big_num.of_int 0), new_len)))))
:: x2 else x2) rel_sections x2))
in
let new_by_tag = (Pset.bigunion (pairCompare compare (maybeCompare (pairCompare compare (pairCompare Nat_big_num.compare Nat_big_num.compare))))(Pset.from_list (Pset.compare_by (pairCompare compare (maybeCompare (pairCompare compare (pairCompare Nat_big_num.compare Nat_big_num.compare))))) [ filtered_image.by_tag; (Pset.from_list (pairCompare compare (maybeCompare (pairCompare compare (pairCompare Nat_big_num.compare Nat_big_num.compare)))) new_reloc_tags_and_ranges) ]))
in
{
elements = (List.fold_right Pmap.union [filtered_image.elements; Lem_map.fromList
(instance_Map_MapKeyType_var_dict instance_Basic_classes_SetType_var_dict) new_reloc_elements] (Pmap.empty compare))
; by_tag = new_by_tag
; by_range = (by_range_from_by_tag
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))) new_by_tag)
})
let expand_sections_for_one_image a options bindings_by_name linkable_idx item strip_relocs:(reloc_site*binding*reloc_decision)list*(any_abi_feature)annotated_memory_image*(input_spec)list=
((match item with
(RelocELF(img2), (fname1, blob, origin), input_opts) ->
let ((reloc_decisions : (reloc_site * binding * reloc_decision) list), marked_img) = (mark_fate_of_relocs linkable_idx a options bindings_by_name item img2)
in
let stripped_img_with_reloc_sections = (if strip_relocs
then
strip_metadata_sections reloc_decisions a marked_img
else marked_img)
in
let inputs =
(List.rev_append (List.rev (let x2 =
([]) in
List.fold_right
(fun(isec1, shndx1) x2 ->
if true then
(let short_name = (short_string_of_linkable_item item) in
InputSection
({ idx = linkable_idx ; fname = short_name
; img = stripped_img_with_reloc_sections ; shndx = shndx1
; secname = (isec1.elf64_section_name_as_string) ; isec = isec1
})) :: x2 else x2)
(elf_memory_image_sections_with_indices stripped_img_with_reloc_sections)
x2)) (
let common_symbols = (all_common_symbols stripped_img_with_reloc_sections)
in
let x2 = ([]) in List.fold_right
(fun def x2 ->
if
true then
Common (linkable_idx, fname1, stripped_img_with_reloc_sections, def) ::
x2 else x2) common_symbols x2
))
in (reloc_decisions, stripped_img_with_reloc_sections, inputs)
| _ -> failwith "non-reloc linkable not supported yet"
))
type reloc_resolution = reloc_site * binding * reloc_decision
let default_merge_generated a generated_img input_spec_lists:((input_spec)list)list=
(
let dummy_input_item = ("(no file)", Input_list.Reloc(Byte_sequence.empty), ((Command_line.File(Command_line.Filename("(no file)"), Command_line.null_input_file_options)), [InCommandLine( (Nat_big_num.of_int 0))]))
in
let dummy_linkable_item = (RelocELF(generated_img), dummy_input_item, Input_list.null_input_options)
in
let (_, _, generated_inputs) = (expand_sections_for_one_image a(Pset.from_list compare []) (Pmap.empty compare)( (Nat_big_num.of_int 0)) dummy_linkable_item false)
in
(match input_spec_lists with
[] -> failwith "link job empty"
| first_input_list :: more_input_lists -> ( List.rev_append (List.rev first_input_list) generated_inputs) :: more_input_lists
))
let expand_sections_for_all_inputs a options bindings_by_name merge_generated idx_and_linkables:((reloc_site*binding*reloc_decision)list*(any_abi_feature)annotated_memory_image*(input_spec)list)list=
(let (expanded_reloc_lists, expanded_imgs, linker_script_input_lists) = (unzip3 (Lem_list.map (fun (idx1, linkable) ->
expand_sections_for_one_image a options bindings_by_name idx1 linkable true) idx_and_linkables))
in
let fnames = (Lem_list.map (fun (idx1, (_, (fname1, _, _), _)) -> fname1) idx_and_linkables)
in
let generated_img = (a.generate_support (list_combine fnames expanded_imgs))
in
let (final_input_spec_lists : ( Linker_script.input_spec list) list) = (merge_generated a generated_img linker_script_input_lists)
in
zip3 expanded_reloc_lists expanded_imgs final_input_spec_lists)
let relocate_output_image a bindings_by_name img2:(any_abi_feature)annotated_memory_image=
(let relocs = (Multimap.lookupBy0
(instance_Basic_classes_Ord_Memory_image_range_tag_dict
instance_Basic_classes_Ord_Abis_any_abi_feature_dict) (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))) (Memory_image_orderings.tagEquiv
instance_Abi_classes_AbiFeatureTagEquiv_Abis_any_abi_feature_dict) (SymbolRef(null_symbol_reference_and_reloc_site))
img2.by_tag)
in
let apply_reloc = (fun img2 -> fun (el_name, start, len) -> fun symref_and_reloc_site -> fun symaddr -> (
let reloc_site1 = ((match symref_and_reloc_site.maybe_reloc with
None -> failwith "impossible: no reloc site during relocation"
| Some r -> r
))
in
let (rel_type1, _) = (a.parse_reloc_info reloc_site1.ref_relent.elf64_ra_info)
in
let (field_is_absolute_addr, applyfn) = (a.reloc rel_type1)
in
let element1 = ((match Pmap.lookup el_name img2.elements with
None -> failwith "impossible: reloc site in nonexistent section"
| Some e -> e
))
in
let site_address = ((match element1.startpos with
Some addr -> Nat_big_num.add addr start
| None -> failwith "error: relocation in section with no address"
))
in
let (width, calculate) = (applyfn img2 site_address symref_and_reloc_site)
in
let existing_field = (extract_natural_field width element1 start)
in
let addend = (Nat_big_num.of_int64 reloc_site1.ref_relent.elf64_ra_addend)
in
let new_field_value = (calculate symaddr addend existing_field)
in
let new_element = (write_natural_field new_field_value width element1 start)
in
{
elements = (Pmap.add el_name new_element (Pmap.remove el_name img2.elements))
; by_tag = (Pset.diff img2.by_tag(Pset.from_list (pairCompare compare (maybeCompare (pairCompare compare (pairCompare Nat_big_num.compare Nat_big_num.compare)))) [(SymbolRef(symref_and_reloc_site), Some(el_name, (start, len)))]))
; by_range = (Pset.diff img2.by_range(Pset.from_list (pairCompare (maybeCompare (pairCompare compare (pairCompare Nat_big_num.compare Nat_big_num.compare))) compare) [(Some(el_name, (start, len)), SymbolRef(symref_and_reloc_site))]))
}
))
in
let (ranges_and_defs : ( element_range option * symbol_definition) list) = (Memory_image_orderings.defined_symbols_and_ranges
instance_Basic_classes_Ord_Abis_any_abi_feature_dict instance_Abi_classes_AbiFeatureTagEquiv_Abis_any_abi_feature_dict img2)
in
let relocated_img = (List.fold_left (fun acc_img -> (fun (tag, maybe_range) ->
(match tag with
SymbolRef(x) -> (match x.maybe_reloc with
Some rs ->
(match maybe_range with
None -> failwith "impossible: reloc site with no range"
| Some (el_name, (start, len)) ->
let symaddr = ((match x.maybe_def_bound_to with
Some(ApplyReloc, Some(bound_def)) ->
a.get_reloc_symaddr bound_def img2 ranges_and_defs x.maybe_reloc
| None -> failwith "no def found for bound-to symbol"
| Some(ApplyReloc, None) ->
if not (Nat_big_num.equal (get_elf64_symbol_binding x.ref.ref_syment) stb_weak) then
failwith "not a weak reference, but no binding"
else
(Nat_big_num.of_int 0)
| Some(LeaveReloc, _) ->
failwith "internal error: applying reloc that is not to be applied"
))
in
apply_reloc acc_img (el_name, start, len) x symaddr
)
| None -> acc_img
)
| _ -> failwith "impossible: not a symbol ref"
)
)) img2 relocs)
in
relocated_img)
let link alloc_map script1 a options linkables:(any_abi_feature)annotated_memory_image=
(let initial_included_indices = (mapMaybei (fun i -> (fun (obj, inp, (opts : input_options)) ->
if opts.item_force_output
then Some i
else None
)) linkables)
in
let linker_script_linkable_idx = (length linkables)
in
let defmap = (all_definitions_by_name linkables)
in
let (accumulated_bindings : binding list)
=
( accumulate_bindings_objectwise_df a linkables defmap [](Pset.from_list Nat_big_num.compare []) initial_included_indices)
in
let referenced_object_indices_and_reasons = (List.fold_left (fun acc_m -> (fun ((ref_idx, ref_sym, ref_linkable), maybe_def_idx_and_sym_and_linkable) ->
(match maybe_def_idx_and_sym_and_linkable with
None -> acc_m
| Some (def_idx, def_sym, def_linkable) ->
if (Lem.option_equal (Lem.pair_equal (=)
(tripleEqual instance_Basic_classes_Eq_var_dict
(instance_Basic_classes_Eq_tup3_dict
instance_Basic_classes_Eq_string_dict
instance_Basic_classes_Eq_var_dict
(instance_Basic_classes_Eq_tup2_dict
instance_Basic_classes_Eq_var_dict
(instance_Basic_classes_Eq_list_dict
instance_Basic_classes_Eq_var_dict)))
instance_Basic_classes_Eq_var_dict)) (Pmap.lookup def_idx acc_m) None)
then Pmap.add def_idx (ref_sym, ref_linkable) acc_m
else acc_m
)
)) ((Pmap.empty Nat_big_num.compare) : (Nat_big_num.num, (symbol_reference * linkable_item)) Pmap.map) accumulated_bindings)
in
let linkables_not_discarded = (mapMaybei (fun i -> (fun (obj, inp, opts) ->
let referenced_object_map_entry = (Pmap.lookup i referenced_object_indices_and_reasons)
in
let referenced = ( not ((Lem.option_equal (Lem.pair_equal (=)
(tripleEqual instance_Basic_classes_Eq_var_dict
(instance_Basic_classes_Eq_tup3_dict
instance_Basic_classes_Eq_string_dict
instance_Basic_classes_Eq_var_dict
(instance_Basic_classes_Eq_tup2_dict
instance_Basic_classes_Eq_var_dict
(instance_Basic_classes_Eq_list_dict
instance_Basic_classes_Eq_var_dict)))
instance_Basic_classes_Eq_var_dict)) referenced_object_map_entry None)))
in
if referenced || opts.item_force_output
then Some (i, (obj, inp, opts))
else None
)) linkables)
in
let (initial_bindings_by_name : (string, ( (Nat_big_num.num * binding)list)) Pmap.map) =
(List.fold_left (fun m -> fun (b_idx, ((ref_idx, ref1, ref_item), maybe_def)) -> (match Pmap.lookup ref1.ref_symname m with
None -> Pmap.add ref1.ref_symname [ (b_idx, ((ref_idx, ref1, ref_item), maybe_def)) ] m
| Some ((bi, b) :: more) -> Pmap.add ref1.ref_symname ((b_idx, ((ref_idx, ref1, ref_item), maybe_def)) :: ((bi, b) :: more)) m
| _ -> failwith "impossible: found empty list in map lacking empties by construction"
)) (Pmap.empty compare) (Lem_list.mapi (fun i -> fun b -> (Nat_big_num.of_int i, b)) accumulated_bindings))
in
let (expanded_triples : ( reloc_resolution list * elf_memory_image * Linker_script.input_spec list) list)
= (expand_sections_for_all_inputs a options initial_bindings_by_name default_merge_generated linkables_not_discarded)
in
let (reloc_resolutions, imgs, input_lists) = (unzip3 expanded_triples)
in
let input_sections = (list_concat input_lists)
in
let seen_ordering = (fun is1 -> (fun is2 -> (
let toNaturalList = (fun is -> (
let (linkable_idx, section_or_symbol_idx) = ((match is with
Common(idx1, fname1, img2, def) -> (idx1, Nat_big_num.add
(let (_, l) = (elf_memory_image_section_ranges img2) in length l) def.def_sym_idx)
| InputSection(isrec) -> (isrec.idx, isrec.shndx)
))
in
(match Lem_list.list_index linkables (Nat_big_num.to_int linkable_idx) with
None -> failwith "impossible: linker input not in linkables list"
| Some (obj, (fname1, blob, (inp_unit, coords)), options) ->
let (our_cid, our_gid, our_aid, maybe_archive_size) = ((match coords with
InArchive(aid, aidx, _, asize) :: InGroup(gid1, gidx) :: [InCommandLine(cid)] -> (cid, gid1, aid, Some asize)
| InArchive(aid, aidx, _, asize) :: [InCommandLine(cid)] -> (cid, (Nat_big_num.of_int 0), aid, Some asize)
| InGroup(gid1, gidx) :: [InCommandLine(cid)] -> (cid, gid1, (Nat_big_num.of_int 0), None)
| [InCommandLine(cid)] -> (cid, (Nat_big_num.of_int 0), (Nat_big_num.of_int 0), None)
| _ -> failwith "internal error: impossible coordinates"
))
in
let aid_to_use = (if options.item_force_output then our_aid
else
let archive_size = ((match maybe_archive_size with
None -> failwith "impossible: archive with no size"
| Some a -> a
))
in Nat_big_num.add archive_size
(match Lem_list.find_index (fun ((b_ref_idx, b_ref, b_ref_item), b_maybe_def) -> (match b_maybe_def with
Some (b_def_idx, b_def, b_def_item) -> Nat_big_num.equal b_def_idx linkable_idx
| _ -> false
)) accumulated_bindings with
Some n -> Nat_big_num.of_int n
| None -> failwith "impossible: non-force-output object does not contain any bound-to defs"
))
in
[our_cid; aid_to_use; section_or_symbol_idx]
)
))
in
(lexicographic_compare Nat_big_num.compare (toNaturalList is1) (toNaturalList is2))
)))
in
let (unrelocated_output_image_lacking_abs_symbols, bindings_by_name)
= (interpret_linker_control_script alloc_map script1 linkables linker_script_linkable_idx a input_sections seen_ordering default_place_orphans initial_bindings_by_name)
in
let all_abs_range_tags_in_included_inputs = (List.concat (
Lem_list.map (fun (img2, (idx1, linkable)) ->
let abslist = (Lem_list.mapMaybe (fun (tag, maybeRange) ->
(match tag with
SymbolDef(ent) -> if (Lem.option_equal (Lem.pair_equal (=) (Lem.pair_equal Nat_big_num.equal Nat_big_num.equal)) maybeRange None) && Nat_big_num.equal (Uint32_wrapper.to_bigint ent.def_syment.elf64_st_shndx) shn_abs
then Some (maybeRange, ent)
else None
| _ -> None
)
) (tagged_ranges_matching_tag
instance_Basic_classes_Ord_Abis_any_abi_feature_dict instance_Abi_classes_AbiFeatureTagEquiv_Abis_any_abi_feature_dict (SymbolDef(null_symbol_definition)) img2))
in
let x2 = ([]) in List.fold_right
(fun(maybe_range, ent) x2 ->
if true then
(maybe_range, SymbolDef
({ def_symname = (ent.def_symname)
; def_syment = (ent.def_syment)
; def_sym_scn = (ent.def_sym_scn)
; def_sym_idx = (ent.def_sym_idx)
; def_linkable_idx = idx1 })) :: x2 else x2)
abslist x2
) (list_combine imgs linkables_not_discarded)
))
in
let by_range_including_abs_symbols =
(Pset.(union) unrelocated_output_image_lacking_abs_symbols.by_range
((Pset.from_list (pairCompare (maybeCompare (pairCompare compare (pairCompare Nat_big_num.compare Nat_big_num.compare))) compare) all_abs_range_tags_in_included_inputs)))
in
let unrelocated_output_image = ({
elements = (unrelocated_output_image_lacking_abs_symbols.elements)
; by_range = by_range_including_abs_symbols
; by_tag = (by_tag_from_by_range
(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 by_range_including_abs_symbols)
})
in
let remaining_relocs = (Multimap.lookupBy0
(instance_Basic_classes_Ord_Memory_image_range_tag_dict
instance_Basic_classes_Ord_Abis_any_abi_feature_dict) (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))) (Memory_image_orderings.tagEquiv
instance_Abi_classes_AbiFeatureTagEquiv_Abis_any_abi_feature_dict) (SymbolRef(null_symbol_reference_and_reloc_site))
unrelocated_output_image.by_tag)
in
let _ = (List.fold_left (fun _ -> (fun (tag, maybe_range) ->
let _ = ((match tag with
SymbolRef(x) -> (match x.maybe_reloc with
Some rs ->
(match maybe_range with
None -> failwith "impossible: reloc site with no range"
| Some (el_name, (start, len)) ->
()
)
| None -> ()
)
| _ -> failwith "impossible: not a symbol ref"
))
in
()
)) () remaining_relocs)
in
let unrelocated_concrete_output_image = (a.concretise_support unrelocated_output_image)
in
let output_image = (relocate_output_image a bindings_by_name unrelocated_concrete_output_image)
in
let (maybe_entry_point_address : Nat_big_num.num option) =
((match Command_line.find_option_matching_tag (Command_line.EntryAddress( (Nat_big_num.of_int 0))) options with
None -> a.guess_entry_point output_image
| Some(Command_line.EntryAddress(x)) -> Some x
))
in
(match maybe_entry_point_address with
Some addr ->
(match address_to_element_and_offset addr output_image with
Some (el_name, el_offset) ->
tag_image (EntryPoint) el_name el_offset( (Nat_big_num.of_int 0)) output_image
| None ->
failwith ("error: entry point address 0x" ^ ((hex_string_of_natural addr) ^ " does not correspond to any element position"))
)
| None ->
let _ = (prerr_endline "Warning: not tagging entry point in output image")
in
output_image
))