Dealing with C varargs in Assembly routines

OP (Company: Universiti Sains Malaysia) #1448737
Rate this post
useful
not useful
Hi,

I understand the basic issues involved with C-to-Assembly interfacing, 
but I'm stumped by how varags is handled in Assembly, say for an ioctl() 
handling routine.
1
extern int ioctl(int driverenum, unsigned long requestenum, ...);
2

3
int test_ioctl() {
4
  ioctl(1,2,3,4,5,6);
5
  return 1;
6
}

gcc -fomit-frame-pointer is used when compiling to give the following 
ARM assembly output:
1
        .file   "ioctl.c"
2
        .text
3
        .align  2
4
        .global test_ioctl
5
        .type   test_ioctl, %function
6
test_ioctl:
7
        @ args = 0, pretend = 0, frame = 0
8
        @ frame_needed = 0, uses_anonymous_args = 0
9
        str     lr, [sp, #-4]!
10
        sub     sp, sp, #8
11
        mov     r3, #5
12
        str     r3, [sp, #0]
13
        mov     r3, #6
14
        str     r3, [sp, #4]
15
        mov     r0, #1
16
        mov     r1, #2
17
        mov     r2, #3
18
        mov     r3, #4
19
        bl      ioctl
20
        mov     r3, #1
21
        mov     r0, r3
22
        add     sp, sp, #8
23
        ldmfd   sp!, {pc}
24
        .size   test_ioctl, .-test_ioctl
25
        .ident  "GCC: (GNU) 4.4.1"

My question is, how can I determine the number of varargs? Is that a 
limitation of using -fomit-frame-pointer? If I don't specify 
'-fomit-frame-pointer', I can deduce the number of varargs which spilled 
onto the stack using the difference between fp and sp, but I don't see 
how that can be done for the following:
1. The number of varargs fits within r0-r3
2. -fomit-frame-pointer is used (which is normally the case)

TIA
Moderator #1449304
Rate this post
useful
not useful
Tat Wan wrote:

> My question is, how can I determine the number of varargs?

It must be deduced from the other parameters.  For the very typical
printf case, it can only be known while parsing the format string.
For your ioctl example, the number of arguments is only known by
dispatching the respective ioctl request.

It's up to the callee to know how to obtain the remaining arguments,
as the exact details are CPU and ABI specific.

Reply

Please log in before posting.

or

Log in with Google account

Registration is free and takes only a minute.

Register now