Question

I (tried to) follow an old answer, but I must be doing something wrong: the function is not inlined (it is still called in the disassembly). Here is what I did:

get_regs.h:
inline unsigned long __get_esp(void) {
__asm__("movl %esp,%eax");
}

program.c:
...
#include "get_regs.h "
...
extern unsigned long __get_esp(void);
...
tmp = __get_esp();

Compiled as (other options for various reasons):
gcc -g -fno-stack-protector -mpreferred-stack-boundary=2 program.c

This follows the second recommendation to include the definition in the header file.

I realize I could use extended assembly and copy the result from %eax to tmp, but would like to understand to do the inlining. I found some hits on SO, but none seemed to cover my case.

System:
- Ubuntu 12.04
- gcc 4.6.3.
- x86 32 bit

Was it helpful?

Solution

You'll need to enable compiler optimization (even the most basic level) for inlining to possibly take place.

By the way, unless you have any special need for a non inlined copy of the function, you can drop the extern re-declaration, and just use the inlined declaration included in the header file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top