gcc option: get start and end addresses of inline functions
My question is clear:
How to get the compiled instructions for inlined functions when using -O2? I tried readelf to get the mapping between the source code lines and the binary codes addresses. But there is no mapping at all for the line where the inlined function is called.
So, what compiler option or any approach could be used?
In fact, I feel that there MUST be a way to tell gcc to dump all the start and end addresses of the functions. Doe开发者_开发百科s anyone have a clue? Thanks!
---
Let me rephrase my intention: I want to configure gcc such that it can tell me from which function are a certain set of instructions compiled from. Yes, simply that.
If a file is inlined, then it doesn't have a start and end function. Each time it's called, the entire code will be duplicated where it was called. That's the purpose of inlining.
Each time it's called, it will have a different start and end address. That's why there is no mapping.
If all you're trying to do is extract the assembly for that function, then don't make it inline, and then you'll be able to see it exactly. However, depending on how the compiler optimizes the code, this may not be the exact same assembly that was generated when the function was inlined.
Since the function is inlined, it's not a normal function with a single start and end any more. Its body has been inserted everywhere you have called it.
GCC doesn't normally emit code for inline functions (at high optimization levels). Because inline functions, by intent, should be inlined into their callers, and there's no purpose in keeping their code in object files.
However, this is not required by C++ standard. What C++ standard requires is that function pointers can point to any function, including those inline! But function pointers have a concrete representation in GCC, and it's a pointer to a physical loaction. This means, that to use a pointer to an inline function, it should have a specific address.
So, to get address of an inline function you should, ehm... get its address in the code! This will make compiler instantiate the inline function and emit assembler code for it. And there are more ways to emit their bodies. See GCC docs:
... Some calls [to inline functions] cannot be integrated for various reasons (in particular, calls that precede the function's definition cannot be integrated, and neither can recursive calls within the definition). If there is a nonintegrated call, then the function is compiled to assembler code as usual. The function must also be compiled as usual if the program refers to its address, because that can't be inlined.
Here's a piece code that confirms this:
inline int uiopuiop()
{
return 4;
}
typedef int (*fptr)();
int main()
{
fptr f = uiopuiop;
return uiopuiop();
}
If you compile C code, you can also use -fkeep-inline-functions
for static-inline
functions.
An inlined function is not only (often) inlined, but various optimizations are done after inlining. For instance, if sum
is the obvious inline function adding two integers, sum(3,4)
is replaced by 7
, and no call to sum appear in the program, and you could even not give a position for it.
Hence, you really don't want to know about the inlined calls to inline functions.
精彩评论