linux-assembly.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rob <robpilling@gmail.com>
To: Blake McBride <blake@arahant.com>
Cc: linux-assembly@vger.kernel.org
Subject: Re: Need help doing a jmp rather than a call
Date: Sat, 9 Nov 2013 14:42:46 +0000	[thread overview]
Message-ID: <20131109144246.GD5152@jeffraw> (raw)
In-Reply-To: <l5lftf$c89$1@ger.gmane.org>

On Sat, Nov 09, 2013 at 08:13:13AM -0600, Blake McBride wrote:
> I spent a few hours on the 64 bit stuff with no success.  I checked
> on the Internet and found that the parameter passing style for x64
> is to pass the first 6 arguments in registers.  Argumens are passed
> in order:
>
> ...
>
> This means you can't simply adjust the stack pointer.  When I ran it
> through a debugger I saw that, in fact, the aruments have shifted
> the registers they use becasue of the additional function layer.
>
> ...
>
> I can't disassemble fun4 becasue fun4 is often a different function
> with vastly different arguments and different return types.
> Remember in C you can pass around a pointer to a function all you
> want and execute it with different arguments at different times (so
> long as it is typecast and the cast matches the function signature).
> C does it without knowing the specifics becasue it does know the
> standard agreement between caller and callee.
>
> This has got to be the most trivial piece of code possible.  I am
> just not an assembler programmer, and I already know more about this
> than I want to.

You need to move the parameters around. So from C, an example would look
like this:

void land(int a, int b)
{
	/* ... */
}

void forward(void (*f)(int, int), int a, int b)
{
	f(a, b); /* rsi -> rdi, rdx -> rsi */
}

So there's a few ways to do this. You could use __attribute__((naked)),
except I don't think it's valid on x86_64 (I'm using gcc 4.7.2).

So forget that, the other approach is to manually forward the registers.
This is roughly what optimised disassembly of the above forward function
would look like, if you got gcc to tail-call and not do any frame setup.

.globl forward
forward:
	// any free register that's not preserved across calls
	movq %rdi, %r10

	// forward call registers
	movq %rsi, %rdi
	movq %rdx, %rsi
	movq %rcx, %rdx
	movq %r8, %rdx
	movq %r9, %r8

	// return address is at (%rsp), so we can just jump
	jmp *%r10


HTH,
Rob

  reply	other threads:[~2013-11-09 14:42 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-09  3:02 Need help doing a jmp rather than a call Blake McBride
2013-11-09  6:19 ` Sofiane Akermoun
2013-11-09  8:21   ` Blake McBride
2013-11-09 10:57     ` Sofiane Akermoun
2013-11-09 11:00       ` Sofiane Akermoun
2013-11-09 14:13         ` Blake McBride
2013-11-09 14:42           ` Rob [this message]
2013-11-09 16:19             ` Blake McBride
2013-11-09 21:30               ` Rob
2013-11-09 22:47                 ` Blake McBride
2013-11-10  0:01                   ` Blake McBride

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=20131109144246.GD5152@jeffraw \
    --to=robpilling@gmail.com \
    --cc=blake@arahant.com \
    --cc=linux-assembly@vger.kernel.org \
    /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 NNTP newsgroup(s).