How to get the length of a function in bytes?

后端 未结 11 996
醉酒成梦
醉酒成梦 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:50

    You can get this information from the linker if you are using a custom linker script. Add a linker section just for the given function, with linker symbols on either side:

    mysec_start = .;
    *(.mysection)
    mysec_end = .;
    

    Then you can specifically assign the function to that section. The difference between the symbols is the length of the function:

    #include <stdio.h>
    
    int i;
    
     __attribute__((noinline, section(".mysection"))) void test_func (void)
    {
        i++;
    }
    
    int main (void)
    {
        extern unsigned char mysec_start[];
        extern unsigned char mysec_end[];
    
        printf ("Func len: %lu\n", mysec_end - mysec_start);
        test_func ();
    
        return 0;
    }
    

    This example is for GCC, but any C toolchain should have a way to specify which section to assign a function to. I would check the results against the assembly listing to verify that it's working the way you want it to.

    0 讨论(0)
  • 2020-11-28 11:50

    There's no facility defined within the C language itself to return the length of a function; there are simply too many variables involved (compiler, target instruction set, object file/executable file format, optimization settings, debug settings, etc.). The very same source code may result in functions of different sizes for different systems.

    C simply doesn't provide any sort of reflection capability to support this kind of information (although individual compilers may supply extensions, such as the Codewarrior example cited by sskuce). If you need to know how many bytes your function takes up in memory, then you'll have to examine the generated object or executable file directly.

    sizeof func won't work because the expression func is being treated as a pointer to the function, so you're getting the size of a pointer value, not the function itself.

    0 讨论(0)
  • 2020-11-28 11:50

    There is no standard way of doing it either in C or C++. There might naturally exist implementation/platform-specific ways of doiung it, but I am not aware of any

    0 讨论(0)
  • 2020-11-28 11:52

    There is a way to determine the size of a function. The command is:

     nm -S <object_file_name>
    

    This will return the sizes of each function inside the object file. Consult the manual pages in the GNU using 'man nm' to gather more information on this.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题