Here is my code:
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}
When the application is running, I found that the parameter passed by call_from_arm to foo is correct(the address of msg), but the Message instance contains wrong values, as if contents on heap are offset.
the output of this application looks like:
msgid : 10, msgStr : Hello
msgid : (wrong value), msgStr : (wrong value, app may crash here)
msgid : 10, msgStr : Hello
The problem has been bothering me for several days. Please help me. Thanks
if I change class Message into
class Message
{
char* msgStr;
}
I can print the correct value of string "Hello", so I think the problem may be bytes-align. long long
is the key point. but I still don't know why.
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.