From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.4 required=5.0 tests=BAYES_00,FORGED_MUA_MOZILLA autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,235855e3822c83d1 X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Received: by 10.68.74.201 with SMTP id w9mr1126258pbv.0.1334304243405; Fri, 13 Apr 2012 01:04:03 -0700 (PDT) Path: r9ni50778pbh.0!nntp.google.com!news1.google.com!goblin2!goblin.stu.neva.ru!aioe.org!.POSTED!not-for-mail From: =?ISO-8859-1?Q?Markus_Sch=F6pflin?= Newsgroups: comp.lang.ada Subject: Re: Importing C function with variable argument list Date: Fri, 13 Apr 2012 10:04:01 +0200 Organization: Aioe.org NNTP Server Message-ID: References: <610ee323-4c7f-413d-8568-aed4955f5152@z38g2000vbu.googlegroups.com> <1288794.275.1334249936588.JavaMail.geo-discussion-forums@ynlp2> <20120413070856.3e52a20eff4b5eb5fc51389c@iki.fi> NNTP-Posting-Host: MdpKeRr+sx3LK7JQiK5aNw.user.speranza.aioe.org Mime-Version: 1.0 X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20120327 Thunderbird/11.0.1 X-Notice: Filtered by postfilter v. 0.8.2 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 8bit Date: 2012-04-13T10:04:01+02:00 List-Id: 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 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 --->%--- 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