How to get the length of a function in bytes?

后端 未结 11 1096
醉酒成梦
醉酒成梦 2020-11-28 11:16

I want to know the length of C function (written by me) at runtime. Any method to get it? It seems sizeof doesn\'t work here.

11条回答
  •  抹茶落季
    2020-11-28 11:53

    Executables (at least ones which have debug info stripped) doesn't store function lengths in any way. So there's no possibility to parse this info in runtime by self. If you have to manipulate with functions, you should do something with your objects in linking phase or by accessing them as files from your executable. For example, you may tell linker to link symbol tables as ordinary data section into the executable, assign them some name, and parse when program runs. But remember, this would be specific to your linker and object format.

    Also note, that function layout is also platform specific and there are some things that make the term "function length" unclear:

    1. Functions may have store used constants in code sections directly after function code and access them using PC-relative addressing (ARM compilers do this).
    2. Functions may have "prologs" and "epilogs" which may may be common to several functions and thus lie outside main body.
    3. Function code may inline other function code

    They all may count or not count in function length.

    Also function may be completely inlined by compiler, so it loose its body.

提交回复
热议问题