From: "Markus Schöpflin" <no.spam@spam.spam>
Subject: Re: Importing C function with variable argument list
Date: Fri, 13 Apr 2012 10:04:01 +0200
Date: 2012-04-13T10:04:01+02:00 [thread overview]
Message-ID: <jm8mlg$kj0$1@speranza.aioe.org> (raw)
In-Reply-To: 20120413070856.3e52a20eff4b5eb5fc51389c@iki.fi
Am 13.04.2012 06:08, schrieb Tero Koskinen:
[...]
> So on amd64/x86_64 (unix) platform injecting asm code, which sets
> %rax/%eax to 0, works in most cases (= 0 vector registers used).
Yes, I did read a similar document (I think it's actually %al that is looked
at, not the full register) which lead me to try using inline assembly like this:
---%<---
with SYSTEM.MACHINE_CODE;
with INTERFACES.C;
procedure TEST
is
use SYSTEM.MACHINE_CODE;
use INTERFACES.C;
function PRINTF (FORMAT : in CHAR_ARRAY; N1 : in INT) return INT;
pragma IMPORT (C, PRINTF, "printf");
F1 : constant CHAR_ARRAY := TO_C("%08d" & ASCII.LF);
DUMMY : INT;
begin
ASM ("mov $0, %%al", CLOBBER => "al", VOLATILE => TRUE);
DUMMY := PRINTF(F1, N1 => 123);
end TEST;
--->%---
Using -O0 when compiling results in the following assembly code:
---%<---
401696: b0 00 mov $0x0,%al
401698: 48 8b 85 40 ff ff ff mov -0xc0(%rbp),%rax
40169f: 48 89 55 c0 mov %rdx,-0x40(%rbp)
4016a3: 48 89 4d c8 mov %rcx,-0x38(%rbp)
4016a7: 48 89 45 b0 mov %rax,-0x50(%rbp)
4016ab: 48 8d 45 c0 lea -0x40(%rbp),%rax
4016af: 48 89 45 b8 mov %rax,-0x48(%rbp)
4016b3: 48 8b 45 b0 mov -0x50(%rbp),%rax
4016b7: be 7b 00 00 00 mov $0x7b,%esi
4016bc: 48 89 c7 mov %rax,%rdi
4016bf: e8 2c fb ff ff callq 4011f0 <printf@plt>
4016c4: 89 45 ec mov %eax,-0x14(%rbp)
--->%---
The first line is from the inline assembly, the rest is code generated by gnat
for the call to printf and the handling of the return value. Still no luck, as
%rax is used between the first line and the call to printf for different things.
Interestingly, compiling with -O2 gives:
---%<---
40157c: b0 00 mov $0x0,%al
40157e: be 7b 00 00 00 mov $0x7b,%esi
401583: e8 68 fc ff ff callq 4011f0 <printf@plt>
--->%---
Et voil�, the resulting binary really works and prints out the expected result.
> (Of course, it would be better to have some standard way to
> say this.)
As others have already said, a new calling convention for variadic C functions
would be the way to go, I think.
Markus
next prev parent reply other threads:[~2012-04-13 8:04 UTC|newest]
Thread overview: 34+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-04-10 9:21 Importing C function with variable argument list Maxim Reznik
2012-04-10 11:22 ` Patrick
2012-04-10 13:09 ` Maxim Reznik
2012-04-10 13:50 ` Georg Bauhaus
2012-04-10 13:18 ` Markus Schöpflin
2012-04-10 16:47 ` Simon Wright
2012-04-12 10:08 ` Markus Schöpflin
2012-04-12 16:58 ` Adam Beneschan
2012-04-12 20:47 ` Randy Brukardt
2012-04-13 8:43 ` Markus Schöpflin
2012-04-13 23:01 ` Randy Brukardt
2012-04-27 8:24 ` Natasha Kerensikova
2012-04-27 9:18 ` Jacob Sparre Andersen
2012-04-27 16:48 ` Natasha Kerensikova
2012-05-03 20:15 ` sbelmont700
2012-05-13 4:18 ` David Thompson
2012-05-13 9:03 ` Simon Wright
2012-05-13 17:01 ` Jeffrey Carter
2012-05-13 18:20 ` Simon Wright
2012-05-13 19:11 ` Jeffrey Carter
2012-05-13 19:55 ` Ludovic Brenta
2012-05-14 0:52 ` Jeffrey Carter
2012-05-13 21:12 ` Simon Wright
2012-05-13 23:57 ` Georg Bauhaus
2012-05-14 0:54 ` Jeffrey Carter
2012-05-14 8:10 ` Nomen Nescio
2012-05-14 15:21 ` Natasha Kerensikova
2012-05-14 20:53 ` sbelmont700
2012-04-13 4:08 ` Tero Koskinen
2012-04-13 8:04 ` Markus Schöpflin [this message]
2012-04-10 20:02 ` Tero Koskinen
2012-04-13 3:28 ` Tero Koskinen
2012-04-10 20:25 ` sbelmont700
2012-04-11 23:24 ` Randy Brukardt
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox