Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
a65d8a4
Parse relocations into expressions (WIP)
maturvo Oct 25, 2024
0db25c0
replace natural by symbolic
maturvo Nov 6, 2024
131b3a6
remove dwarf copy
maturvo Nov 7, 2024
7a9c778
refactor abstract relocations
maturvo Nov 7, 2024
8430a5e
wip
maturvo Nov 7, 2024
71b657e
WIP pass relocations to dwarf
maturvo Nov 15, 2024
14ec896
fixes
maturvo Dec 4, 2024
7b22921
Make it less symbolic where not needed (might revert)
maturvo Dec 6, 2024
6d307ef
extract section body without relocations
maturvo Dec 7, 2024
a504bb9
Merge pull request #1 from maturvo/extract-relocations2
maturvo Dec 28, 2024
92d2164
Make constructing reloc map tail recursive
maturvo Dec 28, 2024
2d0a2e1
sym minus
maturvo Dec 28, 2024
d5c3879
add TODO
maturvo Jan 2, 2025
5108c49
produce non-symbolic dwarf
maturvo Jan 2, 2025
b20a60c
move relocation interpreter to aarch64
maturvo Jan 2, 2025
f680691
Support page reolcations
maturvo Jan 8, 2025
1097f90
fix
maturvo Jan 9, 2025
8da23ec
Check range in relocations
maturvo Jan 15, 2025
5476593
FIx (operator precence)
maturvo Jan 15, 2025
5a40854
Ensure relocation alignment
maturvo Jan 16, 2025
63124a5
Revert to symbolic dwarf
maturvo Jan 21, 2025
870ff7a
Revert "Make it less symbolic where not needed (might revert)"
maturvo Jan 21, 2025
7939d91
Make integers symbolic
maturvo Jan 26, 2025
2b24f23
Keep address symbolic when extracting section with no relocations
maturvo Jan 26, 2025
a59cdac
mod hack
maturvo Jan 27, 2025
5dfe185
remove debug prints
maturvo Jan 27, 2025
cf10bde
Report unsupported relocation
maturvo Jan 27, 2025
e20f2ce
Make ldst target dependent on size
maturvo Feb 1, 2025
26df8fa
wip
maturvo Mar 5, 2025
576dbff
wip
maturvo Mar 5, 2025
1b6f60f
wip (bug?)
maturvo Mar 6, 2025
d25664c
bugfix
maturvo Mar 6, 2025
dec6c34
Dont use sym_map in relocation translation
maturvo Mar 11, 2025
b548b1d
Cleanup dwarf.lem
maturvo Mar 11, 2025
b1dd33d
Don't use symbolic values for non-relocatable files
maturvo Mar 11, 2025
f1c41ae
Cleanup relocation code
maturvo Mar 11, 2025
c914d9e
fix
maturvo Apr 9, 2025
df5625d
Merge pull request #2 from maturvo/refactor
maturvo Apr 9, 2025
6e7cec1
Handle undef symbols
maturvo Apr 9, 2025
94e66ce
add reloc types
maturvo Apr 10, 2025
c8ecbbe
Simplify the abi symbolic relocation representation
maturvo Apr 15, 2025
beb0d46
Add frame base info to the sdt
maturvo Apr 24, 2025
2534c5a
More relocation types
maturvo May 3, 2025
0417d1e
Merge pull request #3 from maturvo/add-frame-base
maturvo May 3, 2025
6f416c4
refactor
maturvo May 3, 2025
fef7e0c
rename abstract relocation
maturvo May 3, 2025
ed23774
remove buildoutput (accidentaly commited before)
maturvo May 3, 2025
6d443b6
Merge pull request #31 from maturvo/master
PeterSewell Jun 14, 2025
1c6bcee
make analyse_type_info_die not strictly require struct byte locations…
PeterSewell Jun 14, 2025
ed0043e
gitignore .vscode
maturvo Jul 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ src/**/*.ml
!src/utility.ml
!src/uint32_wrapper.ml
!src/uint64_wrapper.ml
!src/sym_ocaml.ml
!src/ml_bindings.ml
!src/byte_sequence_wrapper.ml
!src/filesystem_wrapper.ml
Expand All @@ -30,3 +31,7 @@ src/build_num
src/build_zarith
src/_build
src/byte_sequence_impl.lem

_opam

.vscode
182 changes: 182 additions & 0 deletions src/abis/aarch64/abi_aarch64_symbolic_relocation.lem
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
open import Missing_pervasives
open import Error
open import Maybe

open import Num
open import Basic_classes
open import String

open import Elf_types_native_uint
open import Elf_file
open import Elf_header
open import Elf_relocation
open import Elf_symbolic

open import Abi_aarch64_relocation
open import Abi_utilities
open import Abi_symbolic_relocation

type aarch64_relocation_target
= Data64
| Data32
| ADRP
| ADD
| LDST of int
| CALL
| CONDBR
| B

val abi_aarch64_apply_relocation_symbolic : relocation_spec aarch64_relocation_target
let abi_aarch64_apply_relocation_symbolic rel_type =
(** No width, no calculation *)
if rel_type = r_aarch64_none then
return Nothing
(** No width, no calculation *)
else if rel_type = r_aarch64_withdrawn then
return Nothing
(** Signed 64 bit width, calculation: S + A *)
else if rel_type = r_aarch64_abs64 then
let result = Plus(Lift S, Lift A) in
return (Just
<| rel_desc_value = result
; rel_desc_checks = []
; rel_desc_mask = (63, 0)
; rel_desc_target = Data64
|>
)
(** Signed 32 bit width, calculation: S + A *)
else if rel_type = r_aarch64_abs32 then
let result = Plus(Lift S, Lift A) in
return (Just
<| rel_desc_value = result
; rel_desc_checks = [Overflow (~(2**31), 2**32)]
; rel_desc_mask = (31, 0)
; rel_desc_target = Data32
|>
)
(** Signed 64 bit width, calculation: S + A - P *)
else if rel_type = r_aarch64_prel64 then
let result = Minus(Plus(Lift S, Lift A), Lift P) in
return (Just
<| rel_desc_value = result
; rel_desc_checks = []
; rel_desc_mask = (63, 0)
; rel_desc_target = Data64
|>
)
(** Signed 32 bit width, calculation: S + A - P *)
else if rel_type = r_aarch64_prel32 then
let result = Minus(Plus(Lift S, Lift A), Lift P) in
return (Just
<| rel_desc_value = result
; rel_desc_checks = [Overflow (~(2**31), 2**32)]
; rel_desc_mask = (31, 0)
; rel_desc_target = Data32
|>
)
else if rel_type = r_aarch64_adr_prel_pg_hi21 then
let result = Minus(Apply(Page, Plus(Lift S, Lift A)), Apply(Page, Lift P)) in
return (Just
<| rel_desc_value = result
; rel_desc_checks = [Overflow (~(2**32), 2**32)]
; rel_desc_mask = (32, 12)
; rel_desc_target = ADRP
|>
)
else if rel_type = r_aarch64_add_abs_lo12_nc then
let result = Plus(Lift S, Lift A) in
return (Just
<| rel_desc_value = result
; rel_desc_checks = []
; rel_desc_mask = (11, 0)
; rel_desc_target = ADD
|>
)
else if rel_type = r_aarch64_ldst8_abs_lo12_nc then
let result = Plus(Lift S, Lift A) in
return (Just
<| rel_desc_value = result
; rel_desc_checks = []
; rel_desc_mask = (11, 0)
; rel_desc_target = LDST 0
|>
)
else if rel_type = r_aarch64_ldst16_abs_lo12_nc then
let result = Plus(Lift S, Lift A) in
return (Just
<| rel_desc_value = result
; rel_desc_checks = [Alignment 1]
; rel_desc_mask = (11, 1)
; rel_desc_target = LDST 1
|>
)
else if rel_type = r_aarch64_ldst32_abs_lo12_nc then
let result = Plus(Lift S, Lift A) in
return (Just
<| rel_desc_value = result
; rel_desc_checks = [Alignment 2]
; rel_desc_mask = (11, 2)
; rel_desc_target = LDST 2
|>
)
else if rel_type = r_aarch64_ldst64_abs_lo12_nc then
let result = Plus(Lift S, Lift A) in
return (Just
<| rel_desc_value = result
; rel_desc_checks = [Alignment 3]
; rel_desc_mask = (11, 3)
; rel_desc_target = LDST 3
|>
)
else if rel_type = r_aarch64_ldst128_abs_lo12_nc then
let result = Plus(Lift S, Lift A) in
return (Just
<| rel_desc_value = result
; rel_desc_checks = [Alignment 4]
; rel_desc_mask = (11, 4)
; rel_desc_target = LDST 4
|>
)
else if rel_type = r_aarch64_call26 then
let result = Minus(Plus(Lift S, Lift A), Lift P) in
return (Just
<| rel_desc_value = result
; rel_desc_checks = [Overflow (~(2**27), 2**27); Alignment 2]
; rel_desc_mask = (27, 2)
; rel_desc_target = CALL
|>
)
else if rel_type = r_aarch64_condbr19 then
let result = Minus(Plus(Lift S, Lift A), Lift P) in
return (Just
<| rel_desc_value = result
; rel_desc_checks = [Overflow (~(2**20), 2**20); Alignment 2]
; rel_desc_mask = (20, 2)
; rel_desc_target = CONDBR
|>
)
else if rel_type = r_aarch64_jump26 then
let result = Minus(Plus(Lift S, Lift A), Lift P) in
return (Just
<| rel_desc_value = result
; rel_desc_checks = [Overflow (~(2**27), 2**27); Alignment 2]
; rel_desc_mask = (27, 2)
; rel_desc_target = B
|>
)
else
fail ("Unsupported AARCH64 relocation type " ^ (string_of_aarch64_relocation_type rel_type))

let aarch64_relocation_target_to_data_target = function
| Data32 -> return Elf_symbolic.Data32
| Data64 -> return Elf_symbolic.Data64
| _ -> fail "Not a data relocation"
end

let aarch64_relocation_interpreter =
abi_relocation_interpreter abi_aarch64_apply_relocation_symbolic

let aarch64_data_relocation_interpreter =
relocation_interpreter_map_target
aarch64_relocation_interpreter
aarch64_relocation_target_to_data_target
77 changes: 77 additions & 0 deletions src/abis/abi_symbolic_relocation.lem
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
open import Num
open import Maybe
open import Bool
open import Basic_classes
open import String

open import Error

open import Abi_utilities

open import Elf_types_native_uint
open import Elf_symbol_table
open import Elf_symbolic
open import Elf_relocation
open import Elf_file
open import Elf_header
open import Elf_section_header_table
open import Elf_interpreted_section

type relocation_param = S | A | P

let rec eval_op_exp s_val a_val p_val op: error symbolic_expression =
let f = eval_op_exp s_val a_val p_val in
match op with
| Lift S -> return s_val
| Lift A -> return a_val
| Lift P -> return p_val
| Plus (x, y) ->
f x >>= fun a ->
f y >>= fun b ->
return (BinOp (a, Add, b))
| Minus (x, y) ->
f x >>= fun a ->
f y >>= fun b ->
return (BinOp (a, Sub, b))
| Apply(Page, x) ->
f x >>= fun a ->
return (BinOp (a, And, UnOp (Not, Const 4095 (*0xFFF*))))
| _ -> fail "Not supported"
end

let eval_relocation s_val a_val p_val desc=
eval_op_exp s_val a_val p_val desc.rel_desc_value >>= fun value ->
return <| rel_desc_value = value
; rel_desc_checks = desc.rel_desc_checks
; rel_desc_mask = desc.rel_desc_mask
; rel_desc_target = desc.rel_desc_target
|>

type relocation_spec 'a = natural -> error (maybe (relocation_description (relocation_operator_expression relocation_param) 'a))

let relocation_spec_map_target f spec rel_type =
spec rel_type >>= function
| Nothing -> return Nothing
| Just desc -> return (Just (relocation_map_target f desc))
end

val abi_relocation_interpreter : forall 'a. relocation_spec 'a -> relocation_interpreter 'a
let abi_relocation_interpreter spec ef symtab sidx rel =
if is_elf64_relocatable_file ef.elf64_file_header then
section_with_offset ef sidx rel.elf64_ra_offset >>= fun p_val ->
let (rel_type, sym) = parse_elf64_relocation_info rel.elf64_ra_info in
let a_val = Const (integer_of_elf64_sxword rel.elf64_ra_addend) in
let addr = rel.elf64_ra_offset in
match symbolic_address_of_symbol ef symtab (natFromNatural sym) with
| Just x -> x
| Nothing -> fail "Invalid symbol table index"
end >>= fun s_val ->
spec rel_type >>= function
| Nothing -> return Map.empty
| Just rel_desc ->
eval_relocation s_val a_val p_val rel_desc >>= fun x ->
return (Map.singleton addr x)
end
else
fail "abi_relocation_interpreter: not a relocatable file"

6 changes: 4 additions & 2 deletions src/adaptors/harness_interface.lem
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ open import Default_printing
open import Endianness
open import String_table

open import Abi_utilities
open import Elf_dynamic
open import Elf_file
open import Elf_header
Expand Down Expand Up @@ -514,12 +515,13 @@ let {ocaml} harness_string_of_elf64_reloc_a_entry os symtab sht stbl sechdr_stbl
let off = natural_of_elf64_addr rel.elf64_ra_offset in
let inf = natural_of_elf64_xword rel.elf64_ra_info in
let add = integer_of_elf64_sxword rel.elf64_ra_addend in
let typ = Missing_pervasives.unsafe_string_take 22 (os 0) in (*FIXME *)
let (typ, sym) = parse_elf64_relocation_info rel.elf64_ra_info in
let typ = Missing_pervasives.unsafe_string_take 22 (os typ) in
let typs =
let len = naturalFromNat (22 - String.stringLength typ) in
concatS (replicate len " ")
in
let idx = 0 in (* FIXME *)
let idx = sym in
let (nm, value, symtyp, secthdr) =
match List.index symtab (unsafe_nat_of_natural idx) with
| Nothing -> (stn_undef, 0, 0, 0)
Expand Down
Loading