For a variable list of parameters on a PC, it is easy to understand that the parameters are all stored on the stack. So: va_list p defines a pointer, Va_start (p, arg_a) gets the address of the parameter list, which is the address after the corresponding data for the second parameter of the Va_start, which is shown on the stack as: arg_a+sizeof (arg_a). Thereafter, the data is taken from the specified parameter list address, based on the parameter type, using Va_arg.
But when it comes to arm, a platform that uses a register to pass parameters, how does it go about handling it?
By writing a simple example program:
int Test (int A, ...) { va_list p; Va_start (P, a); printf ("%d"int)); Va_end (P); }
The actual compilation is as follows:
.Text:00000BE0 ;Test (int, ...).Text:00000BE0EXPORT _z4testiz.Text:00000BE0_z4testiz;CODE xref:main+8p.Text:00000BE0.Text:00000BE0var_1c = -0x1c.Text:00000BE0varg_r0 = -0x10.Text:00000BE0varg_r1 = -0xc.Text:00000BE0VARG_R2 =-8.Text:00000BE0VARG_R3 =-4.Text:00000BE0. Text: 00000BE0 PUSH {R0-R3}. Text: 00000be2 PUSH {R0-R2,LR}.Text:00000be4LDR R0, = (Unk_2168-0xbee).Text:00000be6 ADDR3, SP, #0x20 +varg_r2.Text:00000be8LDR R1, [SP, #0x20 +varg_r1].Text:00000BEA ADDR0, PC;format.Text:00000BEC STRR3, [SP, #0x20 +var_1c].Text:00000BEEBLX printf.Text:00000BF2 ADDsp, SP, #0xC.Text:00000bf4 POP{R3}.Text:00000bf6 ADDsp, SP, #0x10.Text:00000bf8BX R3.Text:00000bf8 ;End of function test (int,...)
You can see the bold section and push 2 times. And the programmer, we know that generally only push once, that is, the next one--to save the Register state and return address.
So what was the first time? Obviously, this is related to mutable parameters!
The use of registers to pass parameters on ARM is generally only used with R0~R3, so the system presses it directly into the stack! The state of the registers that may be overwritten is then protected in the same way as the general function.
Once the R0~R3 is pressed into the stack, if there are more parameters, it must have been pushed into the stack before (should), then the state is similar to the one on the PC. As shown below
(High address)
...
Arg_5
Arg_4
arg_3<---R3
arg_2<---R2
arg_1<---R1
arg_0<---R0
< register value to be protected >
...
(Low address)
Analysis passed on the parameter list on arm (take Android as an example)