lightning
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Lightning] How to call varargs functions from lightning in x86_64?


From: Paulo César Pereira de Andrade
Subject: Re: [Lightning] How to call varargs functions from lightning in x86_64?
Date: Mon, 16 Aug 2010 16:55:25 -0300
User-agent: SquirrelMail/1.4.19

Paolo Bonzini wrote:
> 2010/8/16 Paulo César Pereira de Andrade <address@hidden>:
>>  Hi,
>>
>>  I had this issue before, and my solution was to just call a non
>> vararg function, and then call the vararg one from C, and that is
>> actually also what <lightning-git-checkout>/lightning/tests/printf.c
>> does.
>
> I think that is not portable in general.

  gcc (after some fighting to prevent it from generating a puts call):

-%<-
void
say(char *a, char *b, int c)
{
    printf("%s %s%c", a, b, c);
}

int
main(int argc, char *argv[])
{
    say("Hello", "world!", '\n');
    return (0);
}
-%<-

objdump output:
-%<-
void
say(char *a, char *b, int c)
{
  4004a4:       55                      push   %rbp
  4004a5:       48 89 e5                mov    %rsp,%rbp
  4004a8:       53                      push   %rbx
  4004a9:       48 83 ec 28             sub    $0x28,%rsp
  4004ad:       48 89 7d e8             mov    %rdi,-0x18(%rbp)
  4004b1:       48 89 75 e0             mov    %rsi,-0x20(%rbp)
  4004b5:       89 55 dc                mov    %edx,-0x24(%rbp)
    printf("%s %s%c", a, b, c);
  4004b8:       b8 fc 05 40 00          mov    $0x4005fc,%eax
  4004bd:       8b 4d dc                mov    -0x24(%rbp),%ecx
  4004c0:       48 8b 55 e0             mov    -0x20(%rbp),%rdx
  4004c4:       48 8b 5d e8             mov    -0x18(%rbp),%rbx
  4004c8:       48 89 de                mov    %rbx,%rsi
  4004cb:       48 89 c7                mov    %rax,%rdi
  4004ce:       b8 00 00 00 00          mov    $0x0,%eax
  4004d3:       e8 c0 fe ff ff          callq  400398 <address@hidden>
}
-%<-

testing tool file:
-%<-
.data   32
hello:
.c      "Hello world!\n"
.code   64
        prolog 0
        prepare 1
                movi_p %r0 hello
                pusharg_p %r0
        finish @printf
        ret
-%<-

and running it:
-%<-
$ ./lightning -v hello.tst -v3
./lightning:0: warning: feature request: patch finish/calli
./lightning:0: warning: jit_addcr_l not defined
./lightning:0: warning: jit_extr_f_d not defined
./lightning:0: warning: jit_hmulr_l not defined
./lightning:0: warning: jit_negr_f not defined
hello:
         0x171a280      48 65 6c 6c 6f 20 77 6f 72 6c 64 21 0a 00
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
         0x171a390      push   %rbx
                        53
         0x171a391      push   %r12
                        41 54
         0x171a393      push   %r13
                        41 55
         0x171a395      push   %rbp
                        55
         0x171a396      mov    %rsp,%rbp
                        48 89 e5
         0x171a399      push   %rbx
                        53
         0x171a39a      movabs $0x171a280,%rax
                        48 b8 80 a2 71 01 00 00 00 00
         0x171a3a4      mov    %rax,%rdi
                        48 89 c7
         0x171a3a7      movabs $0x7f1a15ad5290,%r9
                        49 b9 90 52 ad 15 1a 7f 00 00
         0x171a3b1      rex.WB callq *%r9
                        49 ff d1
         0x171a3b4      leaveq
                        c9
         0x171a3b5      pop    %r13
                        41 5d
         0x171a3b7      pop    %r12
                        41 5c
         0x171a3b9      pop    %rbx
                        5b
         0x171a3ba      retq
                        c3
  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Falha de segmentação
-%<-

  I am wondering about the rex.WB prefix, is that correct?

> Paolo

Thanks,
Paulo




reply via email to

[Prev in Thread] Current Thread [Next in Thread]