Removing the prologue of a function written in pure assembly
Asked Answered
S

3

7

I am using Delphi 2010. Is it possible to tell Delphi to not generate a prologue for a function? I'm writing some pure assembly functions like this:

procedure SomeAssembly; stdcall;
begin
    asm
        ...
    end;
end;

and I would like to tell Delphi not to generate a prologue and epilogue for this function, like C++'s __declspec(naked) feature.

And so no one wastes their time, I don't need help getting these functions to work with the prologue; I can already do that. It's just a large inconvenience and will make maintenance an huge hassle. I'll have to manually inspect the prologues generated by the compiler to see their length, and if that changes, my program will crash.

I also know I can write the function as a series of bytes in a byte array, but that would be even worse than having to go find the length of Delphi's prologue.

Sadden answered 27/3, 2011 at 17:15 Comment(0)
R
20

Delphi doesn't generate prologues or epilogues for functions having no arguments and declared with the register calling convention. If you want functions without prologues, declare them as zero-argument, register-calling-convention functions. Also, skip the begin-end block and go straight into assembly.

procedure SomeAssembly; // register; (implied)
asm
  // ...
end;

Since you're effectively lying about the nature of the functions, calling them may be tricky. If you've implemented a function as though it received parameters and used a different calling convention, then you'll have to make sure the compiler knows about that at the call site. To do that, declare a function pointer that reflects the "real" type of your function instead of the declared type. For example, if your function is really a two-argument stdcall function, declare something like this:

type
  TSomeAssemblyFunc = function (Arg1: Integer; Arg2: PAnsiChar): Boolean; stdcall;
var
  SomeAssemblyProc: TSomeAssemblyProc;

Now, assign that variable so it points at your function:

SomeAssemblyProc := TSomeAssemblyProc(@SomeAssembly);
if SomeAssembly(2, 'foo') then ...

In addition to skipping the prologue and epilogue, the compiler will generate the incorrect RET instruction for this function (because of the different calling convention), so you'll have to make sure you say ret 8 in your code instead of letting the compiler's default ret instruction occur.


Finding the length of Delphi's prologue is trivial, if you have a working debugger:

  1. Set a breakpoint at the start of the function.
  2. Call the function.
  3. When the debugger stops at the breakpoint, switch to the CPU view.
  4. Look at the instructions that make up the prologue.
  5. Count the bytes displayed beside those instructions.
Rhoads answered 27/3, 2011 at 17:34 Comment(7)
Why can't everyone on SO answer like this? Extremely helpful, thanks Rob.Sadden
@K. Charles: Different level of expertise? That was a wild guess, though, no offence intended to anyone.Dehumanize
Isn't there an extra level of indirection when you have a function pointer? Personally I'd have thought tasm would be more suitable.Lawful
Relying on a function pointer to skip the Delphi-generated prologue does sound a bit hacky and not very future-proof.Justiciar
@Cosmin, we're not relying on a function pointer to skip the prologue. The prologue doesn't even exist anymore due to the way the function was defined (which was the point of this exercise).Rhoads
Yes, @David, it's an extra level of indirection. It's no worse than importing a function from a DLL, though. (In fact, that's where I learned this technique. I was patching the import table of an EXE to point to new assembler functions. The functions were called as stdcall, but if I defined them that way, then they had compiler-provided prologues that I then needed to "undo" before getting to the real work.)Rhoads
This is not entirely correct. Also functions (asm..end;-only) with register calling convention, no local variables, and no arguments passed via the stack have no prologue. This means up to 3 (u)int32 arguments are still fine. E.g. the following still has no prologue: function Test1 (a,b,c:longword):longword;asm nop end;Leakage
J
1

According to the this embarcadero docwiki you can skip the surrounding begin and end and the compiler will skip some of it's stuff. But if you really want pure assembler, why not put your function into a separate assembler file, assemble it with tasm (the exe is named tasm32) and link to it. You'll then use the assembler directive in the delphi code.

Justiciar answered 27/3, 2011 at 17:37 Comment(1)
@downvoters, please explain. TASM is part of Delphi and it is the natural way of working with 100% pure assembler. Was this strategic voting?Justiciar
C
0

Doesn't

procedure SomeAssembly; stdcall;
asm
    ...
end;

do the trick?

Carcanet answered 27/3, 2011 at 17:34 Comment(1)
If I'm remembering correctly, stdcall causes the compiler to include a prologue and epilogue for saving and restoring the frame pointer, even if there are no parameters.Rhoads

© 2022 - 2024 — McMap. All rights reserved.