[fpc-devel] estimating function code size

Marco van de Voort marcov at stack.nl
Thu Jan 19 12:19:51 CET 2012

In our previous episode, Gennadiy Poryev said:
> In my project (win32) I need to estimate the number of machine code bytes  of a certain function.
> The most straightforward option was to look for $c3 which is ret, and it worked on optimization levels O0 and O1. O2 and O3 seem to prefer $c2 $04 $00 (retn 4) instead.
> Then here comes win64, where the function end is marked by $c3 (ret) regardless of optimization level.
> And it turned out the opcode for mov eax,ebx also has $c3 in it.
> For now, I use O3 level and patterns $c2 $04 $00 and $5d c3 00 (pop rbp, ret) for win32 and win64 respectively, in hope that the function is always geting padded to (but not equal to) nearest $20 in size.
> This is far from deploying a full-fledged disassembler but is still not simple and elegant solution.
> I remember back in TP days there were methods for this, by using labels and addressing them with absolute keyword to obtain offset delta.
> I wonder if some similar trick can be used here, or if there are some built-in function to do just that?

I can't think of a foolproof trick other then compiling to assembler with
smartlink support, or moving the relevant function to a separate unit (and
compile that to assembler and postparse it)

Smartlinking divides the code into sections for linking purposes, and this
is visible in the assembler sourcecode (either by section pseudo commands,
or by the fact it is split into multiple files) 

It might also be possible to get such information from the lineinfo in the
debug information.

More information about the fpc-devel mailing list