gpt4 book ai didi

c - ARM 汇编器: bx jump to nowhere

转载 作者:行者123 更新时间:2023-11-30 21:05:34 26 4
gpt4 key购买 nike

我尝试在C内部编写一些汇编代码,然后调用C函数。我的代码如下:
C:

void my_fun(uint32_t *arg) {
//sth
}

汇编器:

ldr r2, my_fun_label
bx r2
my_fun_label: .word my_fun

使用调试器,我发现 r2 寄存器包含一些随机值,而不是 my_fun 地址。因此程序分支无处可去,并保持卡住状态。有人可以帮助我吗?
我正在使用 nRF52 ARM Cortex M4 微 Controller 。

编辑:结果

0003ea0a: my_fun: 

asm_test:
0003ea26: ldr r2, [pc, #0] ;(0x3ea28 <asm_test+2>)
0003ea28: bx r2
0003ea2a: and.w r0, sp, r3

但是my_fun_label似乎指向0x0dea0300

最佳答案

soc.c

void my_fun ( void )
{

}

sos.s

.thumb

ldr r2, my_fun_label
bx r2
my_fun_label: .word my_fun

构建和拆卸

arm-none-eabi-gcc -mthumb -O2 -c soc.c -o soc.o
arm-none-eabi-as sos.s -o sos.o
arm-none-eabi-ld -Ttext=0x1000 sos.o soc.o -o so.elf
arm-none-eabi-objdump -D so.elf

Disassembly of section .text:

00001000 <my_fun_label-0x4>:
1000: 4a00 ldr r2, [pc, #0] ; (1004 <my_fun_label>)
1002: 4710 bx r2

00001004 <my_fun_label>:
1004: 00001009 andeq r1, r0, r9

00001008 <my_fun>:
1008: 4770 bx lr
100a: 46c0 nop ; (mov r8, r8)

完美运行

可能需要对齐地址,这样您就不会赌它有 50/50 的成功机会。

.thumb

ldr r2, my_fun_label
bx r2
nop
my_fun_label: .word my_fun

谢谢你,gn​​u

arm-none-eabi-as sos.s -o sos.o
sos.s: Assembler messages:
sos.s:5: Error: invalid offset, target not word aligned (0x00000002)
sos.s:5: Error: invalid offset, value too big (0x00000002)

强制对齐

.thumb

ldr r2, my_fun_label
bx r2
nop
.align
my_fun_label: .word my_fun

这样更好

Disassembly of section .text:

00001000 <my_fun_label-0x8>:
1000: 4a01 ldr r2, [pc, #4] ; (1008 <my_fun_label>)
1002: 4710 bx r2
1004: 46c0 nop ; (mov r8, r8)
1006: 46c0 nop ; (mov r8, r8)

00001008 <my_fun_label>:
1008: 0000100d andeq r1, r0, sp

0000100c <my_fun>:
100c: 4770 bx lr
100e: 46c0 nop ; (mov r8, r8)

自修改代码是另一个故事,您可能必须使用屏障和/或处理缓存等...

关于c - ARM 汇编器: bx jump to nowhere,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53744854/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com