Skip to content

Commit e2bac3d

Browse files
committed
Merge branch 'arm32-func-after-litpool' into 'main'
ARM32: Find function starts after literal pools Closes #518 and gtirb-pprinter#250 See merge request rewriting/ddisasm!1236
2 parents ac2f751 + 0809ff4 commit e2bac3d

5 files changed

Lines changed: 122 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
(`symbol_minus_symbol`) in LEA
3030
* Fix bug that could result in false-positive symbolic data conflicting with real strings
3131
* Fix bug that could result in incorrect symbol references due to incorrect GOT entries when multiple symbols share the same name
32+
* Fix bug that could result in missing functions for ARM32
3233

3334
# 1.9.0
3435

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
AS=arm-linux-gnueabihf-as
2+
LD=arm-linux-gnueabihf-ld
3+
STRIP=arm-linux-gnueabihf-strip
4+
5+
all: ex
6+
7+
ex: ex_original.s
8+
$(AS) -march=armv7-a -o ex.o ex_original.s
9+
$(LD) -o ex ex.o
10+
$(STRIP) ex
11+
@qemu-arm -L /usr/arm-linux-gnueabihf ./ex > out.txt
12+
13+
clean:
14+
rm -f ex ex.o out.txt
15+
16+
check:
17+
qemu-arm -L /usr/arm-linux-gnueabihf ./ex > /tmp/res.txt
18+
@ diff out.txt /tmp/res.txt && echo TEST OK
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#
2+
# This function is to check if ddisasm finds a function entry after literal
3+
# pools even if there is no symbol info nor direct calls to the function.
4+
#
5+
# The function `add` does not have any direct call to it.
6+
# After stripped, `add` has no symbol information.
7+
# It is placed right after the literal pool.
8+
#
9+
10+
.arch armv7-a
11+
12+
.thumb
13+
.text
14+
15+
.equ SYS_EXIT, 1
16+
.equ SYS_WRITE, 4
17+
.equ STDOUT, 1
18+
19+
@ Function: sum (calls a function via BLX r3)
20+
.global sum
21+
.type sum, %function
22+
.thumb_func
23+
sum:
24+
push {lr} @ Save link register
25+
mov r3, r2 @ Store function pointer in r3
26+
blx r3 @ Call function pointer
27+
pop {pc} @ Return
28+
29+
@ Print function: writes r0 as ASCII to stdout
30+
.global print_result
31+
.type print_result, %function
32+
.thumb_func
33+
print_result:
34+
push {r1-r3, lr}
35+
36+
@ Convert number in r0 to ASCII ('0' + value)
37+
add r0, r0, #48 @ Convert to ASCII ('0' = 48)
38+
39+
@ Load address of result using PC-relative addressing
40+
adr r3, result_ptr @ Load address of result into r3
41+
ldr r3, [r3] @ Dereference pointer to get real address
42+
strb r0, [r3, #8] @ Store ASCII digit in result buffer
43+
44+
@ Write "Result: X\n" to stdout
45+
mov r0, #STDOUT @ fd = 1 (stdout)
46+
adr r1, result_ptr @ Load result address using literal pool
47+
ldr r1, [r1] @ Dereference pointer
48+
mov r2, #10 @ size = 10
49+
mov r7, #SYS_WRITE @ syscall: write
50+
svc #0
51+
52+
pop {r1-r3, pc}
53+
54+
@ Entry Point (_start)
55+
.global _start
56+
.type _start, %function
57+
.thumb_func
58+
_start:
59+
mov r0, #3 @ First argument (a)
60+
mov r1, #4 @ Second argument (b)
61+
adr r2, add_ptr @ Load function pointer address
62+
ldr r2, [r2] @ Dereference pointer
63+
bl sum @ Call sum(a, b, add)
64+
65+
bl print_result @ Print the result
66+
67+
mov r7, #SYS_EXIT @ syscall: exit
68+
mov r0, #0 @ exit code 0
69+
svc #0
70+
71+
@ Literal Pool (for PC-relative addressing)
72+
.ltorg
73+
.align 4
74+
result_ptr: .word result
75+
add_ptr: .word add
76+
77+
@ Indirect call target
78+
.type add, %function
79+
.thumb_func
80+
add:
81+
push {lr}
82+
add r0, r0, r1 @ r0 = r0 + r1
83+
pop {pc}
84+
85+
.data
86+
result:
87+
.asciz "Result: X\n"

src/datalog/basic_function_inference.dl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ function_entry(EA):-
9191
direct_jump(Src,EA),
9292
code(EA).
9393

94+
#if defined(ARCH_ARM32)
95+
// ARM32: Code right after literal pool
96+
function_entry(EA):-
97+
binary_isa("ARM"),
98+
refined_block(EA),
99+
!function_entry_initial(EA),
100+
possible_target(EA),
101+
after_end(EA,PrevEA),
102+
data_object(PrevEA,_,_),
103+
litpool_boundaries(PrevEA,EA0),
104+
EA0 = EA - (EA band 1).
105+
#endif
106+
94107
function_without_callframe(EA):-
95108
function_entry(EA),
96109
!fde_addresses(EA,_).

tests/qemu-elf-arm.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ tests:
128128
- name: ex_adr_to_code
129129
<<: *assembly
130130

131+
- name: ex_func_after_litpool
132+
<<: *assembly
133+
131134
- name: ex1
132135
<<: *default
133136

0 commit comments

Comments
 (0)