这是我的代码:
test.cpp
class Message
{
public:
long long msgid;
char* msgStr;
};
int foo(Message* msg)
{
// TODO
// print: msg->msgid, msg->msgStr
}
int main()
{
char buf[20] = "Hello";
Message msg = new Message;
msg->msgid = 0x10;
msg->msgStr = buf;
foo(msg);
call_from_arm((void*)&foo, (void*)msg);
foo(msg);
return 0;
}
test.S
call_from_arm:
@r0 = ptrFunc
@r1 = obj
STMFD r13!, {r4-r11,r14}
MOV r8, r0 @r8 = ptrFunc
MOV r0, r1 @r0 = r1
BLX r8 @call ptrFunc
LDMFD r13!, {r4-r11,pc}
应用运行时,发现call_from_arm传给foo的参数是正确的(msg的地址),但是Message实例包含错误的值,好像堆上的内容有偏移。
此应用程序的输出如下所示:
msgid : 10, msgStr : Hello
msgid : (wrong value), msgStr : (wrong value, app may crash here)
msgid : 10, msgStr : Hello
这个问题困扰了我好几天。请帮我。谢谢
如果我将类 Message 更改为
class Message
{
char* msgStr;
}
我可以打印字符串“Hello”的正确值,所以我认为问题可能是字节对齐。long long
是关键点。但我仍然不知道为什么。
I have solved this problem. AAPCS requires 8 bytes-align.
My old version code store r4-r11 & lr into stack, whitch is not 8 bytes-aligned.