Skip to content

Commit 3ce0e56

Browse files
committed
CHB:ARM: add symbolic names to assembly listing
1 parent ab8cdf0 commit 3ce0e56

File tree

3 files changed

+133
-61
lines changed

3 files changed

+133
-61
lines changed

CodeHawk/CHB/bchlibarm32/bCHARMAssemblyFunctions.ml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ object (self)
537537
dark ^ "\n\n" ^ functionstats
538538

539539
method private collect_data_references =
540-
let _ = pverbose [STR (timing ()); STR "collect data references ..."; NL] in
541540
let livetable = self#get_live_instructions in
542541
let filter = (fun i -> H.mem livetable i#get_address#index) in
543542
let table = H.create 11 in
@@ -571,6 +570,28 @@ object (self)
571570
ch_error_log#add
572571
"LDR (literal) from non-code-address"
573572
(LBLOCK [va#toPretty; STR " refers to "; a#toPretty])
573+
| Adr (_, dst, adr)
574+
when adr#is_absolute_address
575+
&& not (functions_data#is_in_function_stub va) ->
576+
let a = adr#get_absolute_address in
577+
let nextva = va#add_int 4 in
578+
if elf_header#is_program_address a then
579+
(match get_arm_assembly_instruction nextva with
580+
| Ok nxtinstr ->
581+
(match nxtinstr#get_opcode with
582+
| LoadMultipleIncrementAfter (_, _, src, rl, _)
583+
when dst#get_register = src#get_register ->
584+
for i = 0 to rl#get_register_count do
585+
add (a#add_int (4 * i)) nxtinstr
586+
done
587+
| _ ->
588+
add a instr)
589+
| _ ->
590+
add a instr)
591+
else
592+
ch_error_log#add
593+
"ADR with non-code address"
594+
(LBLOCK [va#toPretty; STR " refers to "; a#toPretty])
574595
| VLoadRegister (_, vd, _, mem) when mem#is_pc_relative_address ->
575596
let pcoffset = if instr#is_arm32 then 8 else 4 in
576597
let a = mem#get_pc_relative_address va pcoffset in

CodeHawk/CHB/bchlibarm32/bCHARMAssemblyInstructions.ml

Lines changed: 95 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ open CHXmlDocument
3737
(* bchlib *)
3838
open BCHBasicTypes
3939
open BCHByteUtilities
40-
open BCHConstantDefinitions
4140
open BCHDataBlock
4241
open BCHDoubleword
4342
open BCHFunctionData
@@ -264,7 +263,7 @@ let get_instruction (addr: doubleword_int): arm_assembly_instruction_result =
264263
^ ": "
265264
^ (pretty_to_string p)]
266265

267-
(* Return the addresses of valid instructions in the given address range
266+
(* Return the addresses of valid instructions in the given address range
268267
(inclusive) *)
269268
let get_range_instruction_addrs
270269
(startaddr: doubleword_int) (endaddr: doubleword_int): doubleword_int list =
@@ -586,6 +585,7 @@ object (self)
586585
let firstNew = ref true in
587586
let datareftable = H.create (List.length datarefs) in
588587
let _ = List.iter (fun (a, refs) -> H.add datareftable a refs) datarefs in
588+
let memorymap = BCHGlobalMemoryMap.global_memory_map in
589589
let not_code_to_string nc =
590590
match nc with
591591
| JumpTable jt ->
@@ -657,6 +657,19 @@ object (self)
657657
(List.map
658658
(fun (a, v) ->
659659
let addr = a#to_hex_string in
660+
let datarefstr =
661+
if H.mem datareftable addr then
662+
let datarefs = H.find datareftable addr in
663+
" "
664+
^ "(refs: "
665+
^ (String.concat
666+
", "
667+
(List.map
668+
(fun instr ->
669+
instr#get_address#to_hex_string) datarefs))
670+
^ ")"
671+
else
672+
"" in
660673
if a#lt !stringend then
661674
" "
662675
^ (fixed_length_string addr 10)
@@ -671,66 +684,89 @@ object (self)
671684
" "
672685
^ (fixed_length_string addr 10)
673686
^ " <0xffffffff>"
674-
else if H.mem datareftable addr then
675-
let datarefs = H.find datareftable addr in
687+
else if functions_data#is_function_entry_point v then
688+
let name =
689+
if functions_data#has_function_name v then
690+
let fndata = functions_data#get_function v in
691+
":" ^ fndata#get_function_name
692+
else
693+
"" in
676694
" "
677695
^ (fixed_length_string addr 10)
678-
^ " "
679-
^ (fixed_length_string v#to_hex_string 12)
680-
^ ("referenced by: "
681-
^ (String.concat
682-
", "
683-
(List.map
684-
(fun instr ->
685-
instr#get_address#to_hex_string) datarefs)))
696+
^ " Faddr:<"
697+
^ v#to_hex_string
698+
^ name
699+
^ ">"
700+
^ datarefstr
701+
else if memorymap#has_elf_symbol v then
702+
let name = memorymap#get_elf_symbol v in
703+
" "
704+
^ (fixed_length_string addr 10)
705+
^ " Sym:<"
706+
^ v#to_hex_string
707+
^ ":"
708+
^ name
709+
^ ">"
710+
^ datarefstr
711+
else if elf_header#is_code_address v then
712+
" "
713+
^ (fixed_length_string addr 10)
714+
^ " Code:<"
715+
^ v#to_hex_string
716+
^ ">"
717+
^ datarefstr
718+
else if elf_header#is_data_address v then
719+
let s =
720+
match elf_header#get_string_at_address v with
721+
| Some s ->
722+
let len = String.length s in
723+
if len < 50 then
724+
":\"" ^ s ^ "\""
725+
else
726+
":\"" ^ (String.sub s 0 50) ^ "...\""
727+
| _ -> "" in
728+
" "
729+
^ (fixed_length_string addr 10)
730+
^ " Data:<"
731+
^ v#to_hex_string
732+
^ s
733+
^ ">"
734+
^ datarefstr
735+
else if elf_header#is_uninitialized_data_address v then
736+
" "
737+
^ (fixed_length_string addr 10)
738+
^ " Bss:<"
739+
^ v#to_hex_string
740+
^ ">"
741+
^ datarefstr
742+
else if Option.is_some
743+
(elf_header#get_string_at_address a) then
744+
let s =
745+
Option.get (elf_header#get_string_at_address a) in
746+
begin
747+
(" "
748+
^ (fixed_length_string addr 10)
749+
^ " String:<"
750+
^ (fixed_length_string v#to_hex_string 12)
751+
^ ">: \""
752+
^ s
753+
^ "\"")
754+
^ datarefstr
755+
end
756+
else if (String.length datarefstr) > 0 then
757+
" "
758+
^ (fixed_length_string addr 10)
759+
^ " Value<"
760+
^ v#to_hex_string
761+
^ ">"
762+
^ datarefstr
686763
else
687-
match elf_header#get_string_at_address a with
688-
| Some s ->
689-
stringend := a#add_int ((String.length s) + 1);
690-
begin
691-
(" "
692-
^ (fixed_length_string addr 10)
693-
^ " String:<"
694-
^ (fixed_length_string v#to_hex_string 12)
695-
^ ">: \""
696-
^ s
697-
^ "\"")
698-
end
699-
| _ ->
700-
if functions_data#is_function_entry_point v then
701-
" "
702-
^ (fixed_length_string addr 10)
703-
^ " Faddr:<"
704-
^ v#to_hex_string
705-
^ ">"
706-
else if has_symbolic_address_name v then
707-
let name = get_symbolic_address_name v in
708-
" "
709-
^ (fixed_length_string addr 10)
710-
^ " Sym:<"
711-
^ v#to_hex_string
712-
^ ":"
713-
^ name
714-
^ ">"
715-
else if elf_header#is_code_address v then
716-
" "
717-
^ (fixed_length_string addr 10)
718-
^ " Code:<"
719-
^ v#to_hex_string
720-
^ ">"
721-
else if elf_header#is_data_address v then
722-
" "
723-
^ (fixed_length_string addr 10)
724-
^ " Data:<"
725-
^ v#to_hex_string
726-
^ ">"
727-
else
728-
" "
729-
^ (fixed_length_string addr 10)
730-
^ " "
731-
^ (fixed_length_string v#to_hex_string 14)
732-
^ " "
733-
^ (opcode_string a v))
764+
" "
765+
^ (fixed_length_string addr 10)
766+
^ " "
767+
^ (fixed_length_string v#to_hex_string 14)
768+
^ " "
769+
^ (opcode_string a v))
734770
(List.rev !contents)))
735771
^ "\n" ^ (string_repeat "=" 80) ^ "\n")
736772
end

CodeHawk/CHB/bchlibarm32/bCHARMOpcodeRecords.ml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1665,7 +1665,22 @@ let is_opcode_conditional (opc: arm_opcode_t): bool =
16651665
let arm_opcode_to_string ?(width=12) (opc:arm_opcode_t) =
16661666
let formatter = new string_formatter_t width in
16671667
let default () = (get_record opc).ida_asm formatter in
1668-
default ()
1668+
let fnsdata = BCHFunctionData.functions_data in
1669+
match opc with
1670+
| BranchLink (ACCAlways, tgt) when tgt#is_absolute_address ->
1671+
let tgtaddr = tgt#get_absolute_address in
1672+
if fnsdata#has_function_name tgtaddr then
1673+
let name = (fnsdata#get_function tgtaddr)#get_function_name in
1674+
(fixed_length_string "BL" width)
1675+
^ " <"
1676+
^ tgtaddr#to_hex_string
1677+
^ ":"
1678+
^ name
1679+
^ ">"
1680+
else
1681+
default()
1682+
| _ ->
1683+
default ()
16691684

16701685

16711686
let get_operands_written (opc:arm_opcode_t) =

0 commit comments

Comments
 (0)