Skip to content

Commit b5e7a39

Browse files
committed
Fix symbol resolution based on ELF specs
Signed-off-by: Josiah White <[email protected]>
1 parent 38af78a commit b5e7a39

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

magic-trace.opam

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ depends: [
2222
"ppx_jane"
2323
"shell"
2424
"dune" {>= "2.0.0"}
25-
"owee" {>= "0.6"}
25+
"owee" {>= "0.8"}
2626
"re" {>= "1.8.0"}
2727
"zstandard"
2828
]

src/elf.ml

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type t =
66
; string : Owee_elf.String_table.t
77
; all_elf : Owee_buf.t
88
; sections : Owee_elf.section array
9+
; programs : Owee_elf.program array
910
; debug : Owee_buf.t option
1011
; ocaml_exception_info : Ocaml_exception_info.t option
1112
; base_offset : int
@@ -90,6 +91,7 @@ let create filename =
9091
let header, sections = Owee_elf.read_elf buffer in
9192
let string = Owee_elf.find_string_table buffer sections in
9293
let symbol = Owee_elf.find_symbol_table buffer sections in
94+
let programs = Owee_elf.read_programs buffer header in
9395
match string, symbol with
9496
| Some string, Some symbol ->
9597
let base_offset =
@@ -106,6 +108,7 @@ let create filename =
106108
; debug
107109
; all_elf = buffer
108110
; sections
111+
; programs
109112
; base_offset
110113
; filename
111114
; statically_mappable
@@ -303,15 +306,32 @@ let selection_stop_info t pid selection =
303306
let compute_addr addr =
304307
if t.statically_mappable
305308
then addr
306-
else
309+
else (
310+
(* Find the lowest p_vaddr from the program headers and use this as the base address *)
311+
let base_address =
312+
match
313+
List.fold_left
314+
~f:(fun acc ph ->
315+
if ph.p_type = 1
316+
then (
317+
match acc with
318+
| None -> Some ph.p_vaddr
319+
| Some min_vaddr -> Some (Int64.min min_vaddr ph.p_vaddr))
320+
else acc)
321+
~init:None
322+
(Array.to_list t.programs)
323+
with
324+
| Some vaddr -> vaddr
325+
| None -> failwith "No program headers of type LOAD found"
326+
in
327+
(* Find the first address that the binary is actually mapped into *)
307328
Owee_linux_maps.scan_pid (Pid.to_int pid)
308-
|> List.filter_map ~f:(fun { address_start; address_end; pathname; offset; _ } ->
329+
|> List.filter_map ~f:(fun { address_start; pathname; _ } ->
309330
let open Int64 in
310-
let length = address_end - address_start in
311-
if String.(pathname = filename) && addr >= offset && addr < offset + length
312-
then Some (addr - offset + address_start)
331+
if String.(pathname = filename)
332+
then Some (address_start - base_address + addr)
313333
else None)
314-
|> List.hd_exn
334+
|> List.hd_exn)
315335
in
316336
let compute_filter ~name ~addr ~size =
317337
let offset = Int64.( - ) addr (Int64.of_int t.base_offset) in

0 commit comments

Comments
 (0)