Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing the prologue of a function written in pure assembly

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.

like image 994
K. Charles Avatar asked Mar 27 '11 17:03

K. Charles


1 Answers

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.
like image 104
Rob Kennedy Avatar answered Sep 28 '22 02:09

Rob Kennedy