public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Meissner, Michael" <michael.meissner@amd.com>
To: "Jean-Sebastien Legare" <jslegare@gmail.com>, gcc-help@gcc.gnu.org
Subject: RE: Constructing function calls
Date: Thu, 21 Jul 2005 23:03:00 -0000	[thread overview]
Message-ID: <6096959DEF5C9447A6BF80BDC7EB9EDC2E9F41@SBOSEXMB1.amd.com> (raw)

I would say it is impossible.  The calling sequence depends on the type
of the arguments, and the declaration of the called function (ie, often
times functions that use variable arguments have a different calling
sequence than functions with a fixed number of arguments).  For example,
a 64 bit floating point value is often passed within a floating point
register, but a 64 bit integer might be required to be passed in an
even:odd register pair on a 32-bit system.

One possible way of 'solving' the program is change the code to use
variable argument function calling sequence, use va_start to create the
va_list object and pass it to the child, much like is done with vprintf.

-----Original Message-----
From: gcc-help-owner@gcc.gnu.org [mailto:gcc-help-owner@gcc.gnu.org] On
Behalf Of Jean-Sebastien Legare
Sent: Thursday, July 21, 2005 6:51 PM
To: gcc-help@gcc.gnu.org
Subject: Constructing function calls

Hi

I have a special task I would like to accomplish regarding construction
of
function calls, without (preferably) using __asm__ sections.

I would like to pass a block of data containing arguments to a function
from
which I don't know the type nor number of arguments. The only thing I
know 
is the address of the function pointer and the size of the argument
block
(number of 32bit words).

That is, I would like to fill this function:

void call_func( (void*)func(), void* args, int size) {

    1. push the contents of args to the stack;
    2. call func() with no args (func will recuperate them from the
stack);
    3. return
}

For instance, I would like to be able to call :

void print3ints(int a, int b, int c) {
    printf(" %d %d %d\n",a,b,c);
}

by doing :

{
    int* args = malloc( 3 *sizeof(int));
    args[0] = 1; args[1] = 2; args[2] = 3;
    call_func(print3ints,args,3);
}


I looked at "__builtin_apply" but it requires calling
"__builtin_apply_args"
beforehand,  which I cannot do.

I successfully managed to do that without using __asm__ on a x86. 
I filled a local int array in call_func with the passed argument block
and then 
I called the passed function (stack bashing).

However, the method breaks when compiling in -O3 mode and it would
certainly
not work on other systems where some arguments are passed inside
registers.

What are my best options ?


             reply	other threads:[~2005-07-21 23:03 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-07-21 23:03 Meissner, Michael [this message]
  -- strict thread matches above, loose matches on Subject: below --
2005-07-21 22:52 Jean-Sebastien Legare
2001-10-10 11:26 Constructing Function Calls Pierre NGUYEN-TUONG
2001-10-10 12:14 ` John Love-Jensen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6096959DEF5C9447A6BF80BDC7EB9EDC2E9F41@SBOSEXMB1.amd.com \
    --to=michael.meissner@amd.com \
    --cc=gcc-help@gcc.gnu.org \
    --cc=jslegare@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).