get size of function

Johannes Pfau spam at example.com
Wed Aug 31 07:42:00 PDT 2011


Trass3r wrote:
>Am 31.08.2011, 12:05 Uhr, schrieb maarten van damme  
><maartenvd1994 at gmail.com>:
>
>> Am I sure those functions aren't shift around during optimization?
>
>No. It's just an ugly hack and likely to go up in flames.
>Also what you try could only work with PIC but even then...

That's true, as I already said it for sure is an ugly hack. There's no
guarantee that the compiler outputs the functions in this order. The
linker could change the order as well. And if you send this function to
another process (or even a different machine), you might get even more
issues (for example some kind of offset calculated by the compiler and
"hard-coded" into the function could be wrong in the other process).

The Linux kernel does some tricks to store 'dynamic' lists in ELF files.
For this to work it also needs access to the size of the list. You could
use a similar approach: You'd have to write your own linker script to
put your function in an own sections, than output 2 symbols exactly
before and after the function. But that's a lot of work and I personally
think that's still an ugly hack. It's used in the Linux kernel however,
so it has to work.

DMD also uses a similar trick with the _deh_beg and _deh_end symbols,
maybe dmd does something to make that mechanism less fragile, you could
look it up in the compiler source.
-- 
Johannes Pfau



More information about the Digitalmars-d-learn mailing list