Re: Need help doing a jmp rather than a call

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

 



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
--
To unsubscribe from this list: send the line "unsubscribe linux-assembly" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Kernel Newbies]     [Security]     [Linux C Programming]     [Linux for Hams]     [DCCP]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux Admin]     [Samba]     [Video 4 Linux]

  Powered by Linux