package linksem
A formalisation of the core ELF and DWARF file formats written in Lem
Install
Dune Dependency
Authors
Maintainers
Sources
0.8.tar.gz
md5=2075c56715539b3b8f54ae65cc808b8c
sha512=f7c16e4036a1440a6a8d13707a43f0f9f9db0c68489215f948cc300b6a164dba5bf852e58f89503e9d9f38180ee658d9478156ca1a1ef64d6861eec5f9cf43d2
doc/src/linksem_zarith/elf_file.ml.html
Source file elf_file.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 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
(*Generated by Lem from elf_file.lem.*) (** Module [elf_file] packages all components of an ELF file up into a single * record, provides I/O routines for this record, as well as other utility * functions that operate on an entire ELF file. *) open Lem_basic_classes open Lem_bool open Lem_list open Lem_num open Lem_maybe open Lem_string open Elf_header open Elf_interpreted_section open Elf_interpreted_segment open Elf_types_native_uint open Elf_section_header_table open Elf_symbol_table open Elf_program_header_table open String_table open Byte_sequence open Error open Missing_pervasives open Show (** [elf32_file] record captures the internal structure of an ELF32 file. * Invariant: length of the program header and section header tables should match * the length of their interpreted counterparts, and the nth element of the * (program/section) header table must correspond to the nth element of the * interpreted (segments/sections), respectively. *) type elf32_file = { elf32_file_header : elf32_header (** The file header. *) ; elf32_file_program_header_table : elf32_program_header_table (** The program header table. *) ; elf32_file_section_header_table : elf32_section_header_table (** The section header table. *) ; elf32_file_interpreted_segments : elf32_interpreted_segments (** A more usable interpretation of the file's segments. *) ; elf32_file_interpreted_sections : elf32_interpreted_sections (** A more usable interpretation of the file's sections. *) ; elf32_file_bits_and_bobs : (Nat_big_num.num * byte_sequence0) list (** The uninterpreted "rubbish" that may appear in gaps in the binary file. *) } (** [bytes_of_elf32_file f1] blits ELF file [f1] to a byte sequence, ready for * writing to a binary file. Fails if the invariant on [elf32_file] mentioned * above is not respected. *) (*val bytes_of_elf32_file : elf32_file -> error byte_sequence*) let bytes_of_elf32_file ef:(Byte_sequence_wrapper.byte_sequence)error= (let endian = (get_elf32_header_endianness ef.elf32_file_header) in let hdr_bytes = (bytes_of_elf32_header ef.elf32_file_header) in let hdr_layout = ( (Nat_big_num.of_int 0), hdr_bytes) in let pht_bytes = (bytes_of_elf32_program_header_table endian ef.elf32_file_program_header_table) in let sht_bytes = (bytes_of_elf32_section_header_table endian ef.elf32_file_section_header_table) in let pht_off = (Uint32_wrapper.to_bigint ef.elf32_file_header.elf32_phoff) in let sht_off = (Uint32_wrapper.to_bigint ef.elf32_file_header.elf32_shoff) in let pht_layout = (pht_off, pht_bytes) in let sht_layout = (sht_off, sht_bytes) in let bab_layout = (ef.elf32_file_bits_and_bobs) in if List.length ef.elf32_file_program_header_table = List.length ef.elf32_file_interpreted_segments then if List.length ef.elf32_file_section_header_table = List.length ef.elf32_file_interpreted_sections then let segs_zip = (Lem_list.list_combine ef.elf32_file_program_header_table ef.elf32_file_interpreted_segments) in let sects_zip = (Lem_list.list_combine ef.elf32_file_section_header_table ef.elf32_file_interpreted_sections) in let segs_layout = (Lem_list.map (fun (seg, interp_seg) -> (Uint32_wrapper.to_bigint seg.elf32_p_offset, interp_seg.elf32_segment_body) ) (List.filter (fun (x, _) -> not (x.elf32_p_filesz = (Uint32_wrapper.of_bigint( (Nat_big_num.of_int 0))))) segs_zip)) in let sects_layout = (Lem_list.map (fun (sect, interp_sect) -> (Uint32_wrapper.to_bigint sect.elf32_sh_offset, interp_sect.elf32_section_body) ) (List.filter (fun (x, _) -> not (x.elf32_sh_type = (Uint32_wrapper.of_bigint sht_nobits))) sects_zip)) in let pre_layout = (List.rev_append (List.rev (List.rev_append (List.rev (List.rev_append (List.rev [hdr_layout; pht_layout; sht_layout]) sects_layout)) segs_layout)) bab_layout) in let final_layout = (List.sort (fun (off_x, _) (off_y, _) -> Nat_big_num.compare off_x off_y) pre_layout) in let concats = (foldM (fun x y -> let (current_offset, so_far) = x in let (point_to_add, body) = y in if Nat_big_num.less point_to_add current_offset then let diff = (Nat_big_num.sub_nat current_offset point_to_add) in (* Completely contained inside other segment *) if Nat_big_num.less (Byte_sequence.length0 body) diff then return (current_offset, so_far) else bind (Byte_sequence.partition0 diff body) (fun (_, cut) -> let concat2 = (Byte_sequence.concat [so_far; cut]) in let delta = (Nat_big_num.add current_offset (Byte_sequence.length0 cut)) in return (delta, concat2)) else let diff = (Nat_big_num.sub_nat point_to_add current_offset) in let reps = (Byte_sequence.create diff '\000') in let concat2 = (Byte_sequence.concat [so_far; reps; body]) in let delta = (Nat_big_num.add point_to_add (Byte_sequence.length0 body)) in return (delta, concat2) ) ( (Nat_big_num.of_int 0), Byte_sequence.empty) final_layout) in bind concats (fun (offset, body) -> return body) else fail "bytes_of_elf32_file: interpreted sections and section header table must have same length" else fail "bytes_of_elf32_file: interpreted segments and program header table must have same length") (** [elf64_file] record captures the internal structure of an ELF32 file. * Invariant: length of the program header and section header tables should match * the length of their interpreted counterparts, and the nth element of the * (program/section) header table must correspond to the nth element of the * interpreted (segments/sections), respectively. *) type elf64_file = { elf64_file_header : elf64_header (** The file header. *) ; elf64_file_program_header_table : elf64_program_header_table (** The program header table. *) ; elf64_file_section_header_table : elf64_section_header_table (** The section header table. *) ; elf64_file_interpreted_segments : elf64_interpreted_segments (** A more usable interpretation of the file's segments. *) ; elf64_file_interpreted_sections : elf64_interpreted_sections (** A more usable interpretation of the file's sections. *) ; elf64_file_bits_and_bobs : (Nat_big_num.num * byte_sequence0) list (** The uninterpreted "rubbish" that may appear in gaps in the binary file. *) } type elf_file = | ELF_File_32 of elf32_file | ELF_File_64 of elf64_file (** [bytes_of_elf64_file f1] blits ELF file [f1] to a byte sequence, ready for * writing to a binary file. Fails if the invariant on [elf64_file] mentioned * above is not respected. *) (*val bytes_of_elf64_file : elf64_file -> error byte_sequence*) let bytes_of_elf64_file ef:(Byte_sequence_wrapper.byte_sequence)error= (let endian = (get_elf64_header_endianness ef.elf64_file_header) in let hdr_bytes = (bytes_of_elf64_header ef.elf64_file_header) in let hdr_layout = ( (Nat_big_num.of_int 0), hdr_bytes) in let pht_bytes = (bytes_of_elf64_program_header_table endian ef.elf64_file_program_header_table) in let sht_bytes = (bytes_of_elf64_section_header_table endian ef.elf64_file_section_header_table) in let pht_off = (Uint64_wrapper.to_bigint ef.elf64_file_header.elf64_phoff) in let sht_off = (Uint64_wrapper.to_bigint ef.elf64_file_header.elf64_shoff) in let pht_layout = (pht_off, pht_bytes) in let sht_layout = (sht_off, sht_bytes) in let bab_layout = (ef.elf64_file_bits_and_bobs) in if (* List.length ef.elf64_file_program_header_table = List.length ef.elf64_file_interpreted_segments *) true then if List.length ef.elf64_file_section_header_table = List.length ef.elf64_file_interpreted_sections then let segs_zip = (Lem_list.list_combine ef.elf64_file_program_header_table ef.elf64_file_interpreted_segments) in let sects_zip = (Lem_list.list_combine ef.elf64_file_section_header_table ef.elf64_file_interpreted_sections) in let segs_layout = ([]) (* List.map (fun (seg, interp_seg) -> (natural_of_elf64_off seg.elf64_p_offset, interp_seg.elf64_segment_body) ) (List.filter (fun (x, _) -> x.elf64_p_filesz <> elf64_xword_of_natural 0) segs_zip) *) in let sects_layout = (Lem_list.map (fun (sect, interp_sect) -> (Uint64_wrapper.to_bigint sect.elf64_sh_offset, interp_sect.elf64_section_body) ) (List.filter (fun (x, _) -> not (x.elf64_sh_type = (Uint32_wrapper.of_bigint sht_nobits))) sects_zip)) in let pre_layout = (List.rev_append (List.rev (List.rev_append (List.rev (List.rev_append (List.rev [hdr_layout; pht_layout; sht_layout]) sects_layout)) segs_layout)) bab_layout) in let final_layout = (List.sort (fun (off_x, _) (off_y, _) -> Nat_big_num.compare off_x off_y) pre_layout) in let concats = (foldM (fun x y -> let (current_offset, so_far) = x in let (point_to_add, body) = y in if Nat_big_num.less point_to_add current_offset then let diff = (Nat_big_num.sub_nat current_offset point_to_add) in (* Completely contained inside other segment *) if Nat_big_num.less (Byte_sequence.length0 body) diff then return (current_offset, so_far) else bind (Byte_sequence.partition0 diff body) (fun (_, cut) -> let concat2 = (Byte_sequence.concat [so_far; cut]) in let delta = (Nat_big_num.add current_offset (Byte_sequence.length0 cut)) in return (delta, concat2)) else let diff = (Nat_big_num.sub_nat point_to_add current_offset) in let reps = (Byte_sequence.create diff '\000') in let concat2 = (Byte_sequence.concat [so_far; reps; body]) in let delta = (Nat_big_num.add point_to_add (Byte_sequence.length0 body)) in return (delta, concat2) ) ( (Nat_big_num.of_int 0), Byte_sequence.empty) final_layout) in bind concats (fun (offset, body) -> return body) else fail "bytes_of_elf64_file: interpreted sections and section header table must have same length" else fail "bytes_of_elf64_file: interpreted segments and program header table must have same length") (** [obtain_elf32_program_header_table hdr bs0] reads a file's program header table * from byte sequence [bs0] using information gleaned from the file header [hdr]. * Fails if transcription fails. *) (*val obtain_elf32_program_header_table : elf32_header -> byte_sequence -> error elf32_program_header_table*) let obtain_elf32_program_header_table hdr bs0:((elf32_program_header_table_entry)list)error= (let endian = (get_elf32_header_endianness hdr) in let pentries = (Uint32_wrapper.to_bigint hdr.elf32_phnum) in let pentry_size = (Uint32_wrapper.to_bigint hdr.elf32_phentsize) in let psize = (Nat_big_num.mul pentries pentry_size) in if Nat_big_num.equal psize( (Nat_big_num.of_int 0)) then return [] else let poffset = (Uint32_wrapper.to_bigint hdr.elf32_phoff) in bind (Byte_sequence.offset_and_cut poffset psize bs0) (fun pexact -> bind ( (* Byte sequence irrelevant below as exact size used... *)read_elf32_program_header_table psize endian pexact) (fun (pht, _) -> return pht))) (** [obtain_elf64_program_header_table hdr bs0] reads a file's program header table * from byte sequence [bs0] using information gleaned from the file header [hdr]. * Fails if transcription fails. *) (*val obtain_elf64_program_header_table : elf64_header -> byte_sequence -> error elf64_program_header_table*) let obtain_elf64_program_header_table hdr bs0:((elf64_program_header_table_entry)list)error= (let endian = (get_elf64_header_endianness hdr) in let pentries = (Uint32_wrapper.to_bigint hdr.elf64_phnum) in let pentry_size = (Uint32_wrapper.to_bigint hdr.elf64_phentsize) in let psize = (Nat_big_num.mul pentries pentry_size) in if Nat_big_num.equal psize( (Nat_big_num.of_int 0)) then return [] else let poffset = (Uint64_wrapper.to_bigint hdr.elf64_phoff) in bind (Byte_sequence.offset_and_cut poffset psize bs0) (fun pexact -> bind ( (* Byte sequence irrelevant below as exact size used... *)read_elf64_program_header_table psize endian pexact) (fun (pht, _) -> return pht))) (** [obtain_elf32_section_header_table hdr bs0] reads a file's section header table * from byte sequence [bs0] using information gleaned from the file header [hdr]. * Fails if transcription fails. *) (*val obtain_elf32_section_header_table : elf32_header -> byte_sequence -> error elf32_section_header_table*) let obtain_elf32_section_header_table hdr bs0:((elf32_section_header_table_entry)list)error= (let endian = (get_elf32_header_endianness hdr) in let sentries = (Uint32_wrapper.to_bigint hdr.elf32_shnum) in let sentry_size = (Uint32_wrapper.to_bigint hdr.elf32_shentsize) in let ssize = (Nat_big_num.mul sentries sentry_size) in if Nat_big_num.equal ssize( (Nat_big_num.of_int 0)) then return [] else let soffset = (Uint32_wrapper.to_bigint hdr.elf32_shoff) in bind (Byte_sequence.offset_and_cut soffset ssize bs0) (fun sexact -> bind ( (* Byte sequence irrelevant below as exact size used... *)read_elf32_section_header_table ssize endian sexact) (fun (sht, _) -> return sht))) (** [obtain_elf64_section_header_table hdr bs0] reads a file's section header table * from byte sequence [bs0] using information gleaned from the file header [hdr]. * Fails if transcription fails. *) (*val obtain_elf64_section_header_table : elf64_header -> byte_sequence -> error elf64_section_header_table*) let obtain_elf64_section_header_table hdr bs0:((elf64_section_header_table_entry)list)error= (let endian = (get_elf64_header_endianness hdr) in let sentries = (Uint32_wrapper.to_bigint hdr.elf64_shnum) in let sentry_size = (Uint32_wrapper.to_bigint hdr.elf64_shentsize) in let ssize = (Nat_big_num.mul sentries sentry_size) in if Nat_big_num.equal ssize( (Nat_big_num.of_int 0)) then return [] else let soffset = (Uint64_wrapper.to_bigint hdr.elf64_shoff) in bind (Byte_sequence.offset_and_cut soffset ssize bs0) (fun sexact -> bind ( (* Byte sequence irrelevant below as exact size used... *)read_elf64_section_header_table ssize endian sexact) (fun (sht, _) -> return sht))) (** [obtain_elf32_section_header_string_table hdr sht bs0] reads a file's section * header string table from byte sequence [bs0] using information gleaned from * the file header [hdr] and section header table [sht]. * Fails if transcription fails. *) (*val obtain_elf32_section_header_string_table : elf32_header -> elf32_section_header_table -> byte_sequence -> error (maybe string_table)*) let obtain_elf32_section_header_string_table hdr sht bs0:((string_table)option)error= (if Nat_big_num.equal (Uint32_wrapper.to_bigint hdr.elf32_shstrndx) shn_undef then return None else bind (match Ml_bindings.list_index_big_int (Uint32_wrapper.to_bigint hdr.elf32_shstrndx) sht with None -> fail "no section header string table" | Some x -> return x ) (fun sh -> bind (Byte_sequence.offset_and_cut (Uint32_wrapper.to_bigint sh.elf32_sh_offset) (Uint32_wrapper.to_bigint sh.elf32_sh_size) bs0) (fun sexact -> return (Some (string_table_of_byte_sequence sexact))))) (** [obtain_elf64_section_header_string_table hdr sht bs0] reads a file's section * header string table from byte sequence [bs0] using information gleaned from * the file header [hdr] and section header table [sht]. * Fails if transcription fails. *) (*val obtain_elf64_section_header_string_table : elf64_header -> elf64_section_header_table -> byte_sequence -> error (maybe string_table)*) let obtain_elf64_section_header_string_table hdr sht bs0:((string_table)option)error= (if Nat_big_num.equal (Uint32_wrapper.to_bigint hdr.elf64_shstrndx) shn_undef then return None else bind (match Ml_bindings.list_index_big_int (Uint32_wrapper.to_bigint hdr.elf64_shstrndx) sht with None -> fail "no section header string table" | Some x -> return x ) (fun sh -> bind (Byte_sequence.offset_and_cut (Uint64_wrapper.to_bigint sh.elf64_sh_offset) (Ml_bindings.nat_big_num_of_uint64 sh.elf64_sh_size) bs0) (fun sexact -> return (Some (string_table_of_byte_sequence sexact))))) (** [obtain_elf32_interpreted_segments pht bs0] generates the interpreted segments * of an ELF file from the uninterpreted program header table entries in [pht], * read from byte sequence [bs0]. Makes working with segments easier. * May fail if transcription of any segment fails. *) (*val obtain_elf32_interpreted_segments : elf32_program_header_table -> byte_sequence -> error elf32_interpreted_segments*) let obtain_elf32_interpreted_segments pht bdy:((elf32_interpreted_segment)list)error= (mapM (fun ph -> let offset = (Uint32_wrapper.to_bigint ph.elf32_p_offset) in let size2 = (Uint32_wrapper.to_bigint ph.elf32_p_filesz) in bind (if Nat_big_num.equal size2( (Nat_big_num.of_int 0)) then return Byte_sequence.empty else Byte_sequence.offset_and_cut offset size2 bdy) (fun relevant -> let vaddr = (Uint32_wrapper.to_bigint ph.elf32_p_vaddr) in let paddr = (Uint32_wrapper.to_bigint ph.elf32_p_paddr) in let memsz = (Uint32_wrapper.to_bigint ph.elf32_p_memsz) in let typ = (Uint32_wrapper.to_bigint ph.elf32_p_type) in let align = (Uint32_wrapper.to_bigint ph.elf32_p_align) in let flags = (parse_elf_segment_permissions (Uint32_wrapper.to_bigint ph.elf32_p_flags)) in if Nat_big_num.less memsz size2 then fail "obtain_elf32_interpreted_segments: memory size of segment cannot be less than file size" else return { elf32_segment_body = relevant; elf32_segment_type = typ; elf32_segment_size = size2; elf32_segment_memsz = memsz; elf32_segment_base = vaddr; elf32_segment_flags = flags; elf32_segment_paddr = paddr; elf32_segment_align = align; elf32_segment_offset = offset }) ) pht) (** [obtain_elf64_interpreted_segments pht bs0] generates the interpreted segments * of an ELF file from the uninterpreted program header table entries in [pht], * read from byte sequence [bs0]. Makes working with segments easier. * May fail if transcription of any segment fails. *) (*val obtain_elf64_interpreted_segments : elf64_program_header_table -> byte_sequence -> error elf64_interpreted_segments*) let obtain_elf64_interpreted_segments pht bdy:((elf64_interpreted_segment)list)error= (mapM (fun ph -> let offset = (Uint64_wrapper.to_bigint ph.elf64_p_offset) in let size2 = (Ml_bindings.nat_big_num_of_uint64 ph.elf64_p_filesz) in bind (if Nat_big_num.equal size2( (Nat_big_num.of_int 0)) then return Byte_sequence.empty else Byte_sequence.offset_and_cut offset size2 bdy) (fun relevant -> let vaddr = (Ml_bindings.nat_big_num_of_uint64 ph.elf64_p_vaddr) in let paddr = (Ml_bindings.nat_big_num_of_uint64 ph.elf64_p_paddr) in let memsz = (Ml_bindings.nat_big_num_of_uint64 ph.elf64_p_memsz) in let typ = (Uint32_wrapper.to_bigint ph.elf64_p_type) in let align = (Ml_bindings.nat_big_num_of_uint64 ph.elf64_p_align) in let flags = (parse_elf_segment_permissions (Uint32_wrapper.to_bigint ph.elf64_p_flags)) in (* TODO: this happens in coredumps, just disabling this check for now *) (* if memsz < size then fail ("obtain_elf64_interpreted_segments: memory size of segment (0x" ^ (hex_string_of_natural memsz) ^ ") cannot be less than file size (0x" ^ (hex_string_of_natural size) ^ ")") else *) return { elf64_segment_body = relevant; elf64_segment_type = typ; elf64_segment_size = size2; elf64_segment_memsz = memsz; elf64_segment_base = vaddr; elf64_segment_flags = flags; elf64_segment_align = align; elf64_segment_paddr = paddr; elf64_segment_offset = offset }) ) pht) (** [obtain_elf32_interpreted_section sht bs0] generates the interpreted sections * of an ELF file from the uninterpreted section header table entries in [sht], * read from byte sequence [bs0]. Makes working with sections easier. * May fail if transcription of any section fails. *) (*val obtain_elf32_interpreted_sections : maybe string_table -> elf32_section_header_table -> byte_sequence -> error elf32_interpreted_sections*) let obtain_elf32_interpreted_sections shstrtab sht bs0:((elf32_interpreted_section)list)error= (mapM (fun sh -> let offset = (Uint32_wrapper.to_bigint sh.elf32_sh_offset) in let size2 = (Uint32_wrapper.to_bigint sh.elf32_sh_size) in let name1 = (Uint32_wrapper.to_bigint sh.elf32_sh_name) in let typ = (Uint32_wrapper.to_bigint sh.elf32_sh_type) in let filesz = (if Nat_big_num.equal typ sht_nobits then (Nat_big_num.of_int 0) else size2) in let flags = (Uint32_wrapper.to_bigint sh.elf32_sh_flags) in let base = (Uint32_wrapper.to_bigint sh.elf32_sh_addr) in let link1 = (Uint32_wrapper.to_bigint sh.elf32_sh_link) in let info = (Uint32_wrapper.to_bigint sh.elf32_sh_info) in let align = (Uint32_wrapper.to_bigint sh.elf32_sh_addralign) in let entry_size = (Uint32_wrapper.to_bigint sh.elf32_sh_entsize) in let name_string = ((match shstrtab with None -> "" | Some shstrtab -> (match (get_string_at name1 shstrtab) with | Success n -> n | Fail _ -> "" ) )) in bind (if Nat_big_num.equal filesz( (Nat_big_num.of_int 0)) then return Byte_sequence.empty else Byte_sequence.offset_and_cut offset filesz bs0) (fun relevant -> return { elf32_section_name = name1; elf32_section_type = typ; elf32_section_size = size2; elf32_section_offset = offset; elf32_section_flags = flags; elf32_section_addr = base; elf32_section_link = link1; elf32_section_info = info; elf32_section_align = align; elf32_section_body = relevant; elf32_section_entsize = entry_size; elf32_section_name_as_string = name_string }) ) sht) (** [obtain_elf64_interpreted_section sht bs0] generates the interpreted sections * of an ELF file from the uninterpreted section header table entries in [sht], * read from byte sequence [bs0]. Makes working with sections easier. * May fail if transcription of any section fails. *) (*val obtain_elf64_interpreted_sections : maybe string_table -> elf64_section_header_table -> byte_sequence -> error elf64_interpreted_sections*) let obtain_elf64_interpreted_sections shstrtab sht bs0:((elf64_interpreted_section)list)error= (mapM (fun sh -> let offset = (Uint64_wrapper.to_bigint sh.elf64_sh_offset) in let size2 = (Ml_bindings.nat_big_num_of_uint64 sh.elf64_sh_size) in let name1 = (Uint32_wrapper.to_bigint sh.elf64_sh_name) in let typ = (Uint32_wrapper.to_bigint sh.elf64_sh_type) in let filesz = (if Nat_big_num.equal typ sht_nobits then (Nat_big_num.of_int 0) else size2) in let flags = (Ml_bindings.nat_big_num_of_uint64 sh.elf64_sh_flags) in let base = (Ml_bindings.nat_big_num_of_uint64 sh.elf64_sh_addr) in let link1 = (Uint32_wrapper.to_bigint sh.elf64_sh_link) in let info = (Uint32_wrapper.to_bigint sh.elf64_sh_info) in let align = (Ml_bindings.nat_big_num_of_uint64 sh.elf64_sh_addralign) in let entry_size = (Ml_bindings.nat_big_num_of_uint64 sh.elf64_sh_entsize) in let name_string = ((match shstrtab with None -> "" | Some shstrtab -> (match (get_string_at name1 shstrtab) with | Success n -> n | Fail _ -> "" ) )) in bind (if Nat_big_num.equal filesz( (Nat_big_num.of_int 0)) then return Byte_sequence.empty else Byte_sequence.offset_and_cut offset filesz bs0) (fun relevant -> return { elf64_section_name = name1; elf64_section_type = typ; elf64_section_size = size2; elf64_section_offset = offset; elf64_section_flags = flags; elf64_section_addr = base; elf64_section_link = link1; elf64_section_info = info; elf64_section_align = align; elf64_section_body = relevant; elf64_section_entsize = entry_size; elf64_section_name_as_string = name_string }) ) sht) (** [find_first_not_in_range e rngs] for every pair (start, end) in [rngs], finds * the first element, beginning counting from [e], that does not lie between * a start and end value. *) (*val find_first_not_in_range : natural -> list (natural * natural) -> natural*) let rec find_first_not_in_range start ranges:Nat_big_num.num= ((match List.filter (fun (x, y) -> Nat_big_num.greater_equal start x && Nat_big_num.less_equal start y) ranges with | [] -> start | _ -> find_first_not_in_range ( Nat_big_num.add start( (Nat_big_num.of_int 1))) ranges )) (** [find_first_in_range e rngs] for every pair (start, end) in [rngs], finds * the first element, beginning counting from [e], that lies between * a start and end value. *) (*val find_first_in_range : natural -> list (natural * natural) -> natural*) let rec find_first_in_range start ranges:Nat_big_num.num= ((match List.filter (fun (x, y) -> Nat_big_num.greater_equal start x && Nat_big_num.less_equal start y) ranges with | [] -> find_first_in_range ( Nat_big_num.add start( (Nat_big_num.of_int 1))) ranges | _ -> start )) (** [compute_differences start max ranges] is a utility function used for calculating * "dead" spots in an ELF file not covered by any of the interpreted structure * that nevertheless need recording in the bits_and_bobs field of each ELF record * in order to maintain in-out roundtripping up to exact binary equivalence. *) (*val compute_differences : natural -> natural -> list (natural * natural) -> error (list (natural * natural))*) let rec compute_differences start max ranges:((Nat_big_num.num*Nat_big_num.num)list)error= (if Nat_big_num.equal start max then return [] else if Nat_big_num.greater start max then fail "compute_differences: passed maximum" else let first = (find_first_not_in_range start ranges) in if Nat_big_num.greater_equal first max then return [] else let last1 = (find_first_in_range first ranges) in if Nat_big_num.greater last1 max then return [(first, max)] else bind (compute_differences last1 max ranges) (fun tail -> return ((first, last1)::tail))) (** [obtain_elf32_bits_and_bobs hdr pht segs sht sects bs0] identifies and records * the "dead" spots of an ELF file not covered by any meaningful structure of the * ELF file format. *) (*val obtain_elf32_bits_and_bobs : elf32_header -> elf32_program_header_table -> elf32_interpreted_segments -> elf32_section_header_table -> elf32_interpreted_sections -> byte_sequence -> error (list (natural * byte_sequence))*) let obtain_elf32_bits_and_bobs hdr segs interp_segs sects interp_sects bs0:((Nat_big_num.num*Byte_sequence_wrapper.byte_sequence)list)error= (let hdr_off_len = ( (Nat_big_num.of_int 0), Uint32_wrapper.to_bigint hdr.elf32_ehsize) in let pht_off = (Uint32_wrapper.to_bigint hdr.elf32_phoff) in let pht_len = (Nat_big_num.mul (Uint32_wrapper.to_bigint hdr.elf32_phentsize) (Uint32_wrapper.to_bigint hdr.elf32_phnum)) in let pht_off_len = (pht_off, Nat_big_num.add pht_off pht_len) in let sht_off = (Uint32_wrapper.to_bigint hdr.elf32_shoff) in let sht_len = (Nat_big_num.mul (Uint32_wrapper.to_bigint hdr.elf32_shentsize) (Uint32_wrapper.to_bigint hdr.elf32_shnum)) in let sht_off_len = (sht_off, Nat_big_num.add sht_off sht_len) in if List.length interp_segs = List.length segs then let seg_zip = (Lem_list.list_combine segs interp_segs) in if List.length interp_sects = List.length sects then let sect_zip = (Lem_list.list_combine sects interp_sects) in let seg_off_len = (Lem_list.map (fun (seg, interp_seg) -> let start = (Uint32_wrapper.to_bigint seg.elf32_p_offset) in let len = (interp_seg.elf32_segment_size) in (start, Nat_big_num.add start len)) seg_zip) in let sect_off_len = (Lem_list.map (fun (sect, interp_sect) -> let start = (Uint32_wrapper.to_bigint sect.elf32_sh_offset) in let len = (interp_sect.elf32_section_size) in (start, Nat_big_num.add start len)) sect_zip) in let pre_layout = (hdr_off_len :: (pht_off_len :: (sht_off_len :: List.rev_append (List.rev seg_off_len) sect_off_len))) in let layout = (List.sort (fun (off_x, _) (off_y, _) -> Nat_big_num.compare off_x off_y ) pre_layout) in bind (compute_differences( (Nat_big_num.of_int 0)) (Byte_sequence.length0 bs0) layout) (fun diffs -> mapM (fun (start, len) -> bind (Byte_sequence.offset_and_cut start ( Nat_big_num.sub_nat len start) bs0) (fun rel -> return (start, rel)) ) diffs) else fail "obtain_elf32_bits_and_bobs: section header table and interpreted section differ in length" else fail "obtain_elf32_bits_and_bobs: program header table and interpreted segments differ in length") (** [obtain_elf64_bits_and_bobs hdr pht segs sht sects bs0] identifies and records * the "dead" spots of an ELF file not covered by any meaningful structure of the * ELF file format. *) (*val obtain_elf64_bits_and_bobs : elf64_header -> elf64_program_header_table -> elf64_interpreted_segments -> elf64_section_header_table -> elf64_interpreted_sections -> byte_sequence -> error (list (natural * byte_sequence))*) let obtain_elf64_bits_and_bobs hdr segs interp_segs sects interp_sects bs0:((Nat_big_num.num*Byte_sequence_wrapper.byte_sequence)list)error= (let hdr_off_len = ( (Nat_big_num.of_int 0), Uint32_wrapper.to_bigint hdr.elf64_ehsize) in let pht_off = (Uint64_wrapper.to_bigint hdr.elf64_phoff) in let pht_len = (Nat_big_num.mul (Uint32_wrapper.to_bigint hdr.elf64_phentsize) (Uint32_wrapper.to_bigint hdr.elf64_phnum)) in let pht_off_len = (pht_off, Nat_big_num.add pht_off pht_len) in let sht_off = (Uint64_wrapper.to_bigint hdr.elf64_shoff) in let sht_len = (Nat_big_num.mul (Uint32_wrapper.to_bigint hdr.elf64_shentsize) (Uint32_wrapper.to_bigint hdr.elf64_shnum)) in let sht_off_len = (sht_off, Nat_big_num.add sht_off sht_len) in if List.length interp_segs = List.length segs then let seg_zip = (Lem_list.list_combine segs interp_segs) in if List.length interp_sects = List.length sects then let sect_zip = (Lem_list.list_combine sects interp_sects) in let seg_off_len = (Lem_list.map (fun (seg, interp_seg) -> let start = (Uint64_wrapper.to_bigint seg.elf64_p_offset) in let len = (interp_seg.elf64_segment_size) in (start, Nat_big_num.add start len)) seg_zip) in let sect_off_len = (Lem_list.map (fun (sect, interp_sect) -> let start = (Uint64_wrapper.to_bigint sect.elf64_sh_offset) in let len = (interp_sect.elf64_section_size) in (start, Nat_big_num.add start len)) sect_zip) in let pre_layout = (hdr_off_len :: (pht_off_len :: (sht_off_len :: List.rev_append (List.rev seg_off_len) sect_off_len))) in let layout = (List.sort (fun (off_x, _) (off_y, _) -> Nat_big_num.compare off_x off_y ) pre_layout) in bind (compute_differences( (Nat_big_num.of_int 0)) (Byte_sequence.length0 bs0) layout) (fun diffs -> mapM (fun (start, finish) -> bind (Byte_sequence.offset_and_cut start ( Nat_big_num.sub_nat finish start) bs0) (fun rel -> return (start, rel)) ) diffs) else fail "obtain_elf64_bits_and_bobs: section header table and interpreted section differ in length" else fail "obtain_elf64_bits_and_bobs: program header table and interpreted segments differ in length") (** [read_elf32_file bs0] reads an ELF32 file from byte sequence [bs0]. Fails if * transcription fails. *) (*val read_elf32_file : byte_sequence -> error elf32_file*) let read_elf32_file bs0:(elf32_file)error= (bind (read_elf32_header bs0) (fun (hdr, bs1) -> bind (obtain_elf32_program_header_table hdr bs0) (fun pht -> bind (obtain_elf32_section_header_table hdr bs0) (fun sht -> bind (obtain_elf32_section_header_string_table hdr sht bs0) (fun shstrtab -> bind (obtain_elf32_interpreted_segments pht bs0) (fun segs -> bind (obtain_elf32_interpreted_sections shstrtab sht bs0) (fun sects -> bind (obtain_elf32_bits_and_bobs hdr pht segs sht sects bs0) (fun bits_and_bobs -> return { elf32_file_header = hdr; elf32_file_program_header_table = pht; elf32_file_section_header_table = sht; elf32_file_interpreted_segments = segs; elf32_file_interpreted_sections = sects; elf32_file_bits_and_bobs = bits_and_bobs })))))))) (** [read_elf64_file bs0] reads an ELF64 file from byte sequence [bs0]. Fails if * transcription fails. *) (*val read_elf64_file : byte_sequence -> error elf64_file*) let read_elf64_file bs0:(elf64_file)error= (bind (read_elf64_header bs0) (fun (hdr, bs1) -> bind (obtain_elf64_program_header_table hdr bs0) (fun pht -> bind (obtain_elf64_section_header_table hdr bs0) (fun sht -> bind (obtain_elf64_section_header_string_table hdr sht bs0) (fun shstrtab -> bind (obtain_elf64_interpreted_segments pht bs0) (fun segs -> bind (obtain_elf64_interpreted_sections shstrtab sht bs0) (fun sects -> bind (obtain_elf64_bits_and_bobs hdr pht segs sht sects bs0) (fun bits_and_bobs -> return { elf64_file_header = hdr; elf64_file_program_header_table = pht; elf64_file_section_header_table = sht; elf64_file_interpreted_segments = segs; elf64_file_interpreted_sections = sects; elf64_file_bits_and_bobs = bits_and_bobs })))))))) (** [get_elf32_file_secton_header_string_table f1] returns the ELF file, [f1], * section header string table. * TODO: why is this not using obtain_elf32_section_header_string_table above? *) (*val get_elf32_file_section_header_string_table : elf32_file -> error string_table*) let get_elf32_file_section_header_string_table f3:(string_table)error= (let hdr = (f3.elf32_file_header) in let sht = (f3.elf32_file_section_header_table) in let segs = (f3.elf32_file_interpreted_segments) in let idx1 = (Uint32_wrapper.to_bigint hdr.elf32_shstrndx) in bind (bytes_of_elf32_file f3) (fun bs0 -> (match Ml_bindings.list_index_big_int idx1 sht with | None -> fail "obtain_elf32_string_table: invalid offset into section header table" | Some sect -> let offset = (Uint32_wrapper.to_bigint sect.elf32_sh_offset) in let size2 = (Uint32_wrapper.to_bigint sect.elf32_sh_size) in bind (Byte_sequence.offset_and_cut offset size2 bs0) (fun rel -> let strings = (Byte_sequence.string_of_byte_sequence rel) in return (String_table.mk_string_table strings Missing_pervasives.null_char)) ))) (** [get_elf64_file_secton_header_string_table f1] returns the ELF file, [f1], * section header string table. * TODO: why is this not using obtain_elf64_section_header_string_table above? *) (*val get_elf64_file_section_header_string_table : elf64_file -> error string_table*) let get_elf64_file_section_header_string_table f3:(string_table)error= (let hdr = (f3.elf64_file_header) in let sht = (f3.elf64_file_section_header_table) in let segs = (f3.elf64_file_interpreted_segments) in let idx1 = (Uint32_wrapper.to_bigint hdr.elf64_shstrndx) in bind (bytes_of_elf64_file f3) (fun bs0 -> (match Ml_bindings.list_index_big_int idx1 sht with | None -> fail "obtain_elf64_string_table: invalid offset into section header table" | Some sect -> let offset = (Uint64_wrapper.to_bigint sect.elf64_sh_offset) in let size2 = (Ml_bindings.nat_big_num_of_uint64 sect.elf64_sh_size) in bind (Byte_sequence.offset_and_cut offset size2 bs0) (fun rel -> let strings = (Byte_sequence.string_of_byte_sequence rel) in return (String_table.mk_string_table strings Missing_pervasives.null_char)) ))) (*val find_elf32_symbols_by_symtab_idx : natural -> elf32_file -> error (elf32_symbol_table * string_table * natural)*) let find_elf32_symbols_by_symtab_idx sec_idx f:((elf32_symbol_table_entry)list*string_table*Nat_big_num.num)error= (bind (match Lem_list.list_index f.elf32_file_interpreted_sections (Nat_big_num.to_int sec_idx) with None -> fail "impossible: interpreted section found but not indexable" | Some sec -> return sec ) (fun sec -> bind (match Lem_list.list_index f.elf32_file_interpreted_sections (Nat_big_num.to_int sec.elf32_section_link) with None -> fail "no associated strtab" | Some strs -> return strs ) (fun strs -> let strings = (Byte_sequence.string_of_byte_sequence strs.elf32_section_body) in let strtab = (String_table.mk_string_table strings null_char) in let endian = (get_elf32_header_endianness f.elf32_file_header) in bind (read_elf32_symbol_table endian sec.elf32_section_body) (fun symtab -> return (symtab, strtab, sec_idx))))) (*val find_elf32_symtab_by_type : natural -> elf32_file -> error (elf32_symbol_table * string_table * natural)*) let find_elf32_symtab_by_type t f:(elf32_symbol_table*string_table*Nat_big_num.num)error= (let found_symtab_index = (find_index0 (fun sh -> Nat_big_num.equal sh.elf32_section_type t) f.elf32_file_interpreted_sections) in bind (match found_symtab_index with None -> fail "no such symtab" | Some sec_idx -> return sec_idx ) (fun sec_idx -> find_elf32_symbols_by_symtab_idx sec_idx f)) (*val find_elf64_symbols_by_symtab_idx : natural -> elf64_file -> error (elf64_symbol_table * string_table * natural)*) let find_elf64_symbols_by_symtab_idx sec_idx f:((elf64_symbol_table_entry)list*string_table*Nat_big_num.num)error= (bind (match Lem_list.list_index f.elf64_file_interpreted_sections (Nat_big_num.to_int sec_idx) with None -> fail "impossible: interpreted section found but not indexable" | Some sec -> return sec ) (fun sec -> bind (match Lem_list.list_index f.elf64_file_interpreted_sections (Nat_big_num.to_int sec.elf64_section_link) with None -> fail "no associated strtab" | Some strs -> return strs ) (fun strs -> let strings = (Byte_sequence.string_of_byte_sequence strs.elf64_section_body) in let strtab = (String_table.mk_string_table strings null_char) in let endian = (get_elf64_header_endianness f.elf64_file_header) in bind (read_elf64_symbol_table endian sec.elf64_section_body) (fun symtab -> return (symtab, strtab, sec_idx))))) (*val find_elf64_symtab_by_type : natural -> elf64_file -> error (elf64_symbol_table * string_table * natural)*) let find_elf64_symtab_by_type t f:(elf64_symbol_table*string_table*Nat_big_num.num)error= (let found_symtab_index = (find_index0 (fun sh -> Nat_big_num.equal sh.elf64_section_type t) f.elf64_file_interpreted_sections) in bind (match found_symtab_index with None -> fail "no such symtab" | Some sec_idx -> return sec_idx ) (fun sec_idx -> find_elf64_symbols_by_symtab_idx sec_idx f)) (** [get_elf32_file_symbol_string_table f1] returns the ELF file [f1] symbol * string table. May fail. *) (*val get_elf32_file_symbol_string_table : elf32_file -> error string_table*) let get_elf32_file_symbol_string_table f3:(string_table)error= (let hdr = (f3.elf32_file_header) in let sht = (f3.elf32_file_section_header_table) in let segs = (f3.elf32_file_interpreted_segments) in let strtabs = (Missing_pervasives.mapMaybei (fun index sect -> if Nat_big_num.equal (Uint32_wrapper.to_bigint sect.elf32_sh_type) sht_strtab then if Nat_big_num.equal index (Uint32_wrapper.to_bigint hdr.elf32_shstrndx) then None else Some sect else None) sht) in bind (bytes_of_elf32_file f3) (fun bs0 -> bind (mapM (fun sect -> let offset = (Uint32_wrapper.to_bigint sect.elf32_sh_offset) in let size2 = (Uint32_wrapper.to_bigint sect.elf32_sh_size) in bind (Byte_sequence.offset_and_cut offset size2 bs0) (fun bs1 -> let strings = (Byte_sequence.string_of_byte_sequence bs1) in return (String_table.mk_string_table strings Missing_pervasives.null_char))) strtabs) (fun strings -> String_table.concat0 strings))) (** [get_elf64_file_symbol_string_table f1] returns the ELF file [f1] symbol * string table. May fail. *) (*val get_elf64_file_symbol_string_table : elf64_file -> error string_table*) let get_elf64_file_symbol_string_table f3:(string_table)error= (let hdr = (f3.elf64_file_header) in let sht = (f3.elf64_file_section_header_table) in let segs = (f3.elf64_file_interpreted_segments) in let strtabs = (Missing_pervasives.mapMaybei (fun index sect -> if Nat_big_num.equal (Uint32_wrapper.to_bigint sect.elf64_sh_type) sht_strtab then if false (* PS HACK *) (*index = natural_of_elf64_half hdr.elf64_shstrndx *) then None else Some sect else None) sht) in bind (bytes_of_elf64_file f3) (fun bs0 -> bind (mapM (fun sect -> let offset = (Uint64_wrapper.to_bigint sect.elf64_sh_offset) in let size2 = (Ml_bindings.nat_big_num_of_uint64 sect.elf64_sh_size) in bind (Byte_sequence.offset_and_cut offset size2 bs0) (fun bs1 -> let strings = (Byte_sequence.string_of_byte_sequence bs1) in return (String_table.mk_string_table strings Missing_pervasives.null_char))) strtabs) (fun strings -> String_table.concat0 strings))) (** [get_elf32_file_string_table_by_index f1 index] returns the ELF file [f1] * string table that is pointed to by the section header table entry at index * [index]. May fail if index is out of range, or otherwise. *) (*val get_elf32_string_table_by_index : elf32_file -> natural -> error string_table*) let get_elf32_string_table_by_index ef link1:(string_table)error= (let hdr = (ef.elf32_file_header) in let sht = (ef.elf32_file_section_header_table) in let sects = (ef.elf32_file_interpreted_sections) in (match Lem_list.list_index sects (Nat_big_num.to_int link1) with | None -> fail "get_elf32_string_table_by_index: invalid index" | Some sym1 -> return (mk_string_table (Byte_sequence.string_of_byte_sequence sym1.elf32_section_body) Missing_pervasives.null_char) )) (** [get_elf64_file_string_table_by_index f1 index] returns the ELF file [f1] * string table that is pointed to by the section header table entry at index * [index]. May fail if index is out of range, or otherwise. *) (*val get_elf64_string_table_by_index : elf64_file -> natural -> error string_table*) let get_elf64_string_table_by_index ef link1:(string_table)error= (let hdr = (ef.elf64_file_header) in let sht = (ef.elf64_file_section_header_table) in let sects = (ef.elf64_file_interpreted_sections) in (match Lem_list.list_index sects (Nat_big_num.to_int link1) with | None -> fail "get_elf64_string_table_by_index: invalid index" | Some sym1 -> return (mk_string_table (Byte_sequence.string_of_byte_sequence sym1.elf64_section_body) Missing_pervasives.null_char) )) (** [get_elf32_file_symbol_table f1] returns the ELF file [f1] symbol * table. May fail. *) (*val get_elf32_file_symbol_table : elf32_file -> error elf32_symbol_table*) let get_elf32_file_symbol_table f3:((elf32_symbol_table_entry)list)error= (let hdr = (f3.elf32_file_header) in let sht = (f3.elf32_file_section_header_table) in let segs = (f3.elf32_file_interpreted_segments) in let endian = (get_elf32_header_endianness hdr) in let symtabs = (List.filter (fun sect -> Nat_big_num.equal (Uint32_wrapper.to_bigint sect.elf32_sh_type) sht_symtab ) sht) in (match symtabs with | [] -> return [] | [symtab] -> let offset = (Uint32_wrapper.to_bigint symtab.elf32_sh_offset) in let size2 = (Uint32_wrapper.to_bigint symtab.elf32_sh_size) in bind (bytes_of_elf32_file f3) (fun bs0 -> bind (Byte_sequence.offset_and_cut offset size2 bs0) (fun relevant -> read_elf32_symbol_table endian relevant)) | _ -> fail "obtain_elf32_symbol_table: an ELF file may only have one symbol table of type SHT_SYMTAB" )) (** [get_elf64_file_symbol_table f1] returns the ELF file [f1] symbol * table. May fail. *) (*val get_elf64_file_symbol_table : elf64_file -> error (elf64_symbol_table * string_table)*) let get_elf64_file_symbol_table f3:((elf64_symbol_table_entry)list*string_table)error= (let hdr = (f3.elf64_file_header) in let sht = (f3.elf64_file_section_header_table) in let segs = (f3.elf64_file_interpreted_segments) in let endian = (get_elf64_header_endianness hdr) in let symtabs = (List.filter (fun sect -> Nat_big_num.equal (Uint32_wrapper.to_bigint sect.elf64_sh_type) sht_symtab ) sht) in (match symtabs with | [] -> return ([],String_table.dummy_strtab) | [symtab] -> let lnk = (Uint32_wrapper.to_bigint symtab.elf64_sh_link) in bind (get_elf64_string_table_by_index f3 lnk) (fun strtab -> let offset = (Uint64_wrapper.to_bigint symtab.elf64_sh_offset) in let size2 = (Ml_bindings.nat_big_num_of_uint64 symtab.elf64_sh_size) in bind (bytes_of_elf64_file f3) (fun bs0 -> bind (Byte_sequence.offset_and_cut offset size2 bs0) (fun relevant -> bind (read_elf64_symbol_table endian relevant) (fun symtab -> return (symtab,strtab))))) | _ -> fail "obtain_elf64_symbol_table: an ELF file may only have one symbol table of type SHT_SYMTAB" )) (** [get_elf32_file_dynamic_symbol_table f1] returns the ELF file [f1] dynamic * symbol table. May fail. *) (*val get_elf32_file_dynamic_symbol_table : elf32_file -> error elf32_symbol_table*) let get_elf32_file_dynamic_symbol_table ef:((elf32_symbol_table_entry)list)error= (let hdr = (ef.elf32_file_header) in let sht = (ef.elf32_file_section_header_table) in let segs = (ef.elf32_file_interpreted_segments) in let endian = (get_elf32_header_endianness hdr) in let symtabs = (List.filter (fun sect -> Nat_big_num.equal (Uint32_wrapper.to_bigint sect.elf32_sh_type) sht_dynsym ) sht) in (match symtabs with | [] -> return [] | [symtab] -> let offset = (Uint32_wrapper.to_bigint symtab.elf32_sh_offset) in let size2 = (Uint32_wrapper.to_bigint symtab.elf32_sh_size) in bind (bytes_of_elf32_file ef) (fun bs0 -> bind (Byte_sequence.offset_and_cut offset size2 bs0) (fun relevant -> read_elf32_symbol_table endian relevant)) | _ -> fail "obtain_elf32_dynamic_symbol_table: an ELF file may only have one symbol table of type SHT_DYNSYM" )) (** [get_elf64_file_dynamic_symbol_table f1] returns the ELF file [f1] dynamic * symbol table. May fail. *) (*val get_elf64_file_dynamic_symbol_table : elf64_file -> error elf64_symbol_table*) let get_elf64_file_dynamic_symbol_table ef:((elf64_symbol_table_entry)list)error= (let hdr = (ef.elf64_file_header) in let sht = (ef.elf64_file_section_header_table) in let segs = (ef.elf64_file_interpreted_segments) in let endian = (get_elf64_header_endianness hdr) in let symtabs = (List.filter (fun sect -> Nat_big_num.equal (Uint32_wrapper.to_bigint sect.elf64_sh_type) sht_dynsym ) sht) in (match symtabs with | [] -> return [] | [symtab] -> let offset = (Uint64_wrapper.to_bigint symtab.elf64_sh_offset) in let size2 = (Ml_bindings.nat_big_num_of_uint64 symtab.elf64_sh_size) in bind (bytes_of_elf64_file ef) (fun bs0 -> bind (Byte_sequence.offset_and_cut offset size2 bs0) (fun relevant -> read_elf64_symbol_table endian relevant)) | _ -> fail "obtain_elf64_dynamic_symbol_table: an ELF file may only have one symbol table of type SHT_DYNSYM" )) (** [get_elf32_file_symbol_table_by_index f1 index] returns the ELF file [f1] * symbol table that is pointed to by the section header table entry at index * [index]. May fail if index is out of range, or otherwise. *) (*val get_elf32_symbol_table_by_index : elf32_file -> natural -> error elf32_symbol_table*) let get_elf32_symbol_table_by_index ef link1:(elf32_symbol_table)error= (let hdr = (ef.elf32_file_header) in let sht = (ef.elf32_file_section_header_table) in let sects = (ef.elf32_file_interpreted_sections) in let endian = (get_elf32_header_endianness hdr) in (match Lem_list.list_index sects (Nat_big_num.to_int link1) with | None -> fail "get_elf32_symbol_table_by_index: invalid index" | Some sym1 -> read_elf32_symbol_table endian sym1.elf32_section_body )) (** [get_elf64_file_symbol_table_by_index f1 index] returns the ELF file [f1] * symbol table that is pointed to by the section header table entry at index * [index]. May fail if index is out of range, or otherwise. *) (*val get_elf64_symbol_table_by_index : elf64_file -> natural -> error elf64_symbol_table*) let get_elf64_symbol_table_by_index ef link1:(elf64_symbol_table)error= (let hdr = (ef.elf64_file_header) in let sht = (ef.elf64_file_section_header_table) in let sects = (ef.elf64_file_interpreted_sections) in let endian = (get_elf64_header_endianness hdr) in (match Lem_list.list_index sects (Nat_big_num.to_int link1) with | None -> fail "get_elf64_symbol_table_by_index: invalid index" | Some sym1 -> read_elf64_symbol_table endian sym1.elf64_section_body )) (** [segment_provenance] records whether a segment that appears in an executable * process image has been derived directly from an ELF file, or was automatically * created when the image calculation process noticed a segment with a memory * size greater than its file size. * Really a PPCMemism and not strictly needed for the ELF model itself. *) type segment_provenance = FromELF (** Segment derived directly from the source ELF file. *) | AutoGenerated (** Automatically generated during process extraction as memory size is greater than file size. *) (** [elf32_executable_process_image] is a process image for ELF32 files. Contains * all that is necessary to load the executable components of an ELF32 file * and begin execution. * XXX: (segments, provenance), entry point, machine type *) type elf32_executable_process_image = ( (elf32_interpreted_segment * segment_provenance)list * Nat_big_num.num * Nat_big_num.num) (** [elf64_executable_process_image] is a process image for ELF64 files. Contains * all that is necessary to load the executable components of an ELF64 file * and begin execution. * XXX: (segments, provenance), entry point, machine type *) type elf64_executable_process_image = ( (elf64_interpreted_segment * segment_provenance)list * Nat_big_num.num * Nat_big_num.num) (** [get_elf32_executable_image f1] extracts an executable process image from an * executable ELF file. May fail if extraction is impossible. *) (*val get_elf32_executable_image : elf32_file -> error elf32_executable_process_image*) let get_elf32_executable_image f3:((elf32_interpreted_segment*segment_provenance)list*Nat_big_num.num*Nat_big_num.num)error= (if is_elf32_executable_file f3.elf32_file_header then let entr = (f3.elf32_file_header.elf32_entry) in let segs = (f3.elf32_file_interpreted_segments) in let mach = (f3.elf32_file_header.elf32_machine) in (match List.filter (fun sg -> Nat_big_num.equal sg.elf32_segment_type elf_pt_load) segs with | [] -> fail "get_elf32_executable_image: an executable ELF file must have at least one loadable segment" | load -> bind (mapM (fun sg -> if Nat_big_num.equal sg.elf32_segment_memsz( (Nat_big_num.of_int 0)) then return [] else if Nat_big_num.equal sg.elf32_segment_memsz sg.elf32_segment_size then return [(sg, FromELF)] else if Nat_big_num.less sg.elf32_segment_size sg.elf32_segment_memsz then (* Cannot be negative due to check in constructing [segs]. *) let diff = (Nat_big_num.sub_nat sg.elf32_segment_memsz sg.elf32_segment_size) in let zeros1 = (Byte_sequence.zeros diff) in let addr = (Nat_big_num.add sg.elf32_segment_base sg.elf32_segment_size) in let align = (sg.elf32_segment_align) in let paddr = (sg.elf32_segment_paddr) in let seg = ({ elf32_segment_body = zeros1; elf32_segment_type = (sg.elf32_segment_type); elf32_segment_size = diff; elf32_segment_memsz = diff; elf32_segment_base = addr; elf32_segment_flags = (sg.elf32_segment_flags); elf32_segment_align = align; elf32_segment_paddr = paddr; elf32_segment_offset = (sg.elf32_segment_offset) }) in return [(sg, FromELF); (seg, AutoGenerated)] else fail "get_elf32_executable_image: invariant invalidated") load) (fun bs_base -> return (List.concat bs_base, Uint32_wrapper.to_bigint entr, Uint32_wrapper.to_bigint mach)) ) else fail "get_elf32_executable_image: not an ELF executable file") (** [get_elf64_executable_image f1] extracts an executable process image from an * executable ELF file. May fail if extraction is impossible. *) (*val get_elf64_executable_image : elf64_file -> error elf64_executable_process_image*) let get_elf64_executable_image f3:((elf64_interpreted_segment*segment_provenance)list*Nat_big_num.num*Nat_big_num.num)error= (if is_elf64_executable_file f3.elf64_file_header then let entr = (f3.elf64_file_header.elf64_entry) in let segs = (f3.elf64_file_interpreted_segments) in let mach = (f3.elf64_file_header.elf64_machine) in (match List.filter (fun sg -> Nat_big_num.equal sg.elf64_segment_type elf_pt_load) segs with | [] -> fail "get_elf64_executable_image: an executable ELF file must have at least one loadable segment" | load -> bind (mapM (fun sg -> if Nat_big_num.equal sg.elf64_segment_memsz( (Nat_big_num.of_int 0)) then return [] else if Nat_big_num.equal sg.elf64_segment_memsz sg.elf64_segment_size then return [(sg, FromELF)] else if Nat_big_num.less sg.elf64_segment_size sg.elf64_segment_memsz then (* Cannot be negative due to check in constructing [segs]. *) let diff = (Nat_big_num.sub_nat sg.elf64_segment_memsz sg.elf64_segment_size) in let zeros1 = (Byte_sequence.zeros diff) in let addr = (Nat_big_num.add sg.elf64_segment_base sg.elf64_segment_size) in let align = (sg.elf64_segment_align) in let paddr = (sg.elf64_segment_paddr) in let seg = ({ elf64_segment_body = zeros1; elf64_segment_type = (sg.elf64_segment_type); elf64_segment_size = diff; elf64_segment_memsz = diff; elf64_segment_base = addr; elf64_segment_flags = (sg.elf64_segment_flags); elf64_segment_align = align; elf64_segment_paddr = paddr; elf64_segment_offset = (sg.elf64_segment_offset) }) in return [(sg, FromELF); (seg, AutoGenerated)] else fail "get_elf64_executable_image: invariant invalidated") load) (fun bs_base -> return (List.concat bs_base, Ml_bindings.nat_big_num_of_uint64 entr, Uint32_wrapper.to_bigint mach)) ) else fail "elf64_get_executable_image: not an executable ELF file") (** [global_symbol_init_info] records the name, type, size, address, chunk * of initialisation data (if relevant for that symbol), and binding, of every * global symbol in an ELF file. * Another PPCMemism. *) type global_symbol_init_info = (string * (Nat_big_num.num * Nat_big_num.num * Nat_big_num.num * byte_sequence0 option * Nat_big_num.num)) list (** [get_elf32_file_global_symbol_init f1] extracts the global symbol init info * for ELF file [f1]. May fail. *) (*val get_elf32_file_global_symbol_init : elf32_file -> error global_symbol_init_info*) let get_elf32_file_global_symbol_init f3:((string*(Nat_big_num.num*Nat_big_num.num*Nat_big_num.num*(Byte_sequence_wrapper.byte_sequence)option*Nat_big_num.num))list)error= (if is_elf32_executable_file f3.elf32_file_header then let segs = (f3.elf32_file_interpreted_segments) in bind (bytes_of_elf32_file f3) (fun bs0 -> bind (get_elf32_file_symbol_table f3) (fun symtab -> bind (get_elf32_file_symbol_string_table f3) (fun strtab -> bind (Elf_symbol_table.get_elf32_symbol_image_address symtab strtab) (fun strs -> let mapped = (mapM (fun (symbol, (typ, size2, addr, bind1)) -> if Nat_big_num.equal typ Elf_symbol_table.stt_object then bind (get_elf32_executable_image f3) (fun (img2, entry, mach) -> let chunks2 = (List.filter (fun (chunk, _) -> Nat_big_num.greater_equal addr chunk.elf32_segment_base && (if Nat_big_num.greater size2( (Nat_big_num.of_int 0)) then Nat_big_num.less_equal (Nat_big_num.add addr size2) (Nat_big_num.add chunk.elf32_segment_base chunk.elf32_segment_size) (* We don't consider a zero-size symbol one byte after a section (i.e. addr = segment_base + segment_size) to be inside that section. *) else Nat_big_num.less (Nat_big_num.add addr size2) (Nat_big_num.add chunk.elf32_segment_base chunk.elf32_segment_size)) ) img2) in (match chunks2 with | [] -> return (symbol, (typ, size2, addr, None, bind1)) | [(x, _)] -> let rebase = (Nat_big_num.sub_nat addr x.elf32_segment_base) in bind (Byte_sequence.offset_and_cut rebase size2 x.elf32_segment_body) (fun relevant -> return (symbol, (typ, size2, addr, Some relevant, bind1))) | x::xs -> fail "get_elf32_global_symbol_init: invariant failed, global variable appears in multiple segments" )) else return (symbol, (typ, size2, addr, None, bind1))) strs) in mapped)))) else fail "get_elf32_file_global_symbol_init: not an executable ELF file") (** [get_elf64_file_global_symbol_init f1] extracts the global symbol init info * for ELF file [f1]. May fail. *) (*val get_elf64_file_global_symbol_init : elf64_file -> error global_symbol_init_info*) let get_elf64_file_global_symbol_init f3:((string*(Nat_big_num.num*Nat_big_num.num*Nat_big_num.num*(Byte_sequence_wrapper.byte_sequence)option*Nat_big_num.num))list)error= (if is_elf64_executable_file f3.elf64_file_header then let segs = (f3.elf64_file_interpreted_segments) in bind (bytes_of_elf64_file f3) (fun bs0 -> bind (get_elf64_file_symbol_table f3) (fun (symtab,strtab) -> bind ( (* PS HACK: make this more like harness_interface.lem harness_string_of_elf64_syms' *) (* WAS *) (* get_elf64_file_symbol_string_table f3 >>= fun strtab ->*)Elf_symbol_table.get_elf64_symbol_image_address symtab strtab) (fun strs -> let mapped = (mapM (fun (symbol, (typ, size2, addr, bind1)) -> if Nat_big_num.equal typ Elf_symbol_table.stt_object then bind (get_elf64_executable_image f3) (fun (img2, entry, mach) -> let chunks2 = (List.filter (fun (chunk, _) -> Nat_big_num.greater_equal addr chunk.elf64_segment_base && (if Nat_big_num.greater size2( (Nat_big_num.of_int 0)) then Nat_big_num.less_equal (Nat_big_num.add addr size2) (Nat_big_num.add chunk.elf64_segment_base chunk.elf64_segment_size) (* We don't consider a zero-size symbol one byte after a section (i.e. addr = segment_base + segment_size) to be inside that section. *) else Nat_big_num.less (Nat_big_num.add addr size2) (Nat_big_num.add chunk.elf64_segment_base chunk.elf64_segment_size)) ) img2) in (match chunks2 with | [] -> return (symbol, (typ, size2, addr, None, bind1)) | [(x, _)] -> let rebase = (Nat_big_num.sub_nat addr x.elf64_segment_base) in bind (Byte_sequence.offset_and_cut rebase size2 x.elf64_segment_body) (fun relevant -> return (symbol, (typ, size2, addr, Some relevant, bind1))) | x::xs -> fail "get_elf64_global_symbol_init: invariant failed, global variable appears in multiple segments" )) else return (symbol, (typ, size2, addr, None, bind1))) strs) in mapped))) else fail "get_elf64_global_symbol_init: not an executable ELF file") (** [string_of_elf32_file hdr_bdl pht_bdl sht_bdl f1] produces a string-based * representation of ELF file [f1] using ABI-specific print bundles [hdr_bdl], * [pht_bdl] and [sht_bdl]. *) (*val string_of_elf32_file : hdr_print_bundle -> pht_print_bundle -> sht_print_bundle -> elf32_file -> string*) let string_of_elf32_file hdr_bdl pht_bdl sht_bdl f3:string= ((match get_elf32_file_section_header_string_table f3 with | Fail err -> unlines [ "\nError obtaining ELF section header string table:" ; err ] | Success strtab -> unlines [ "\n*Type elf32_file:" ; "**Header:" ; string_of_elf32_header hdr_bdl f3.elf32_file_header ; "**Program header table:" ; string_of_elf32_program_header_table pht_bdl f3.elf32_file_program_header_table ; "**Section header table:" ; string_of_elf32_section_header_table' sht_bdl strtab f3.elf32_file_section_header_table ; "**Bits and bobs (unused junk space):" ; string_of_list (instance_Show_Show_tup2_dict instance_Show_Show_Num_natural_dict Byte_sequence_impl.instance_Show_Show_Byte_sequence_impl_byte_sequence_dict) f3.elf32_file_bits_and_bobs ] )) (** [string_of_elf64_file hdr_bdl pht_bdl sht_bdl f1] produces a string-based * representation of ELF file [f1] using ABI-specific print bundles [hdr_bdl], * [pht_bdl] and [sht_bdl]. *) (*val string_of_elf64_file : hdr_print_bundle -> pht_print_bundle -> sht_print_bundle -> elf64_file -> string*) let string_of_elf64_file hdr_bdl pht_bdl sht_bdl f3:string= ((match get_elf64_file_section_header_string_table f3 with | Fail err -> unlines [ "\nError obtaining ELF section header string table:" ; err ] | Success strtab -> unlines [ "\n*Type elf64_file:" ; "**Header:" ; string_of_elf64_header hdr_bdl f3.elf64_file_header ; "**Program header table:" ; string_of_elf64_program_header_table pht_bdl f3.elf64_file_program_header_table ; "**Section header table:" ; string_of_elf64_section_header_table' sht_bdl strtab f3.elf64_file_section_header_table ; "**Bits and bobs (unused junk space):" ; string_of_list (instance_Show_Show_tup2_dict instance_Show_Show_Num_natural_dict Byte_sequence_impl.instance_Show_Show_Byte_sequence_impl_byte_sequence_dict) f3.elf64_file_bits_and_bobs ] )) (** [flag_is_set flag v] checks whether flag [flag] is set in [v]. * TODO: move elsewhere. Check whether this is still being used. *) (*val flag_is_set : natural -> natural -> bool*) let flag_is_set flag v:bool= ( (* HACK: convert to elf64_xword first. Flags never live * in objects bigger than 64 bits. *)Uint64_wrapper.logand (Uint64_wrapper.of_bigint v) (Uint64_wrapper.of_bigint flag) = (Uint64_wrapper.of_bigint flag))
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>