How to get the length of a function in bytes?
Solution 1
There is no way in standard C to get the amount of memory occupied by a function.
Solution 2
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.
Solution 3
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.
Solution 4
I have just came up with a solution for the exact same problem but the code i have written is platform depended.
The idea behind, putting known opcodes at the end of the function and searching for them from start while counting bytes we have skipped. Here is the medium link which i have explained with some code https://medium.com/@gurhanpolat/calculate-c-function-size-x64-x86-c1f49921aa1a
Solution 5
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:
- Functions may have store used constants in code sections directly after function code and access them using PC-relative addressing (ARM compilers do this).
- Functions may have "prologs" and "epilogs" which may may be common to several functions and thus lie outside main body.
- 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.
Related videos on Youtube

Comments
-
Thomson 11 days
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.
-
pmg over 11 yearsPlease define "length of C function" precisely
-
Thomson over 11 yearsI mean the memory occupied by the code of this function.
-
Ben Jackson over 11 yearsIf you are asking this for the same reason I wondered about it many years ago, let me point out Writing a New Jit
-
khachik over 11 yearsYou can calculate by subtracting pointers for the function you need to get the length and the next function. But it depends on the compiler. I'm not sure that the optimization parameters won't affect the arrangement as well. So this is not an answer to your question.
-
Admin over 9 yearsNote that functions are not objects (the Standard explicitly says so), you cannot get their sizes.
-
sehe over 9 years@khachik shame that we can't downvote comments. The last time I reasoned about addresses of globals was in ~1995, and even then the practice was iffy
-
sehe over 9 years
-
-
haylem over 11 yearsDidn't know that. Funny. Sort of.
-
Hasturkun over 11 yearsThere's a similar GCC extension, using it you'd be able to calculate the size as
&&label2 - &&label1
-
Thomson over 11 yearsI hope this would work, but it seems it can't work in Visual C++ (C language).
-
caf over 11 yearsIn a conforming compiler,
sizeof func
does not work because a function designator is not a valid operand of thesizeof
operator. -
Ayman Khamouma about 7 yearsyour example is not proving anything :) you're just printing function addresses !
-
hdl almost 6 years
nm -S --size-sort -t d <objfile> | grep <pattern>
displays the size in base 10 (-t d
for decimal) of symbols containingpattern
in their name, sorted by size. Pretty cool. -
Dmitry over 5 years@JohnBode while you are right that this information is not portable; the compiler is still well aware of all of these factors at compile time, and the result makes perfect sense for that particular platform. Knowing the size of a function is meaningful on that platform because it allows you to memcpy the shellcode of "read only" functions, which has very interesting and consistant implications that we cannot study without having such a feature.
-
Dmitry over 5 yearsthey can but these functions still have fixed length, they will just misbehave without those external dependencies. These functions can be rebuilt to be self contained by taking their dependencies as arguments, and many functions, especially callback functions, are exactly this.
-
jakobbotsch over 4 yearsYou can do this without a custom linker script by calling the section by a valid C identifier name, for example
mysection
instead of.mysection
, and using__start_mysection
and__stop_mysection
instead ofmysec_start
andmysec_end
. The linker automatically defines these symbols for each section encountered. -
smwikipedia over 2 yearsIs there any way without modifying the code just for this?
-
Christoffer Bubach almost 2 yearsErhm.. Yes there is, it's called pointer arithmetic. :P
-
mrkotfw over 1 yearPlease see: stackoverflow.com/a/22047976/604033
-
Andrew Henle about 1 year@ChristofferBubach Ummm, that's wrong. In standard-conforming C you can only do pointer arithmetic on pointers referring to the same object or one item past the end of an object. You can't do pointer arithmetic on pointers to different objects. And function pointers don't refer to objects anyway.
-
acapola 8 monthsAs others have pointed out, it can be done using linker script. The question is not restricted to C standard so linker based solutions are valid
-
Rennex 4 monthsGreat! I tried it for myself with a valid identifier name, and indeed it works with plain GCC without a linker script. acapola's answer has a working example, adapted from this one.
-
Christoffer Bubach 3 monthsPointers are numbers, numbers can be used to do math. See? Haha ok to be fair I should have guessed it’s deemed ”undefined”, but really - why wouldn’t it work - know of platforms that include segments too or…?
-
dsmtoday about 2 monthsIf you use a linker script, you will probably still have to modify that linker script to get this trick to work, so you might as well use FazJaxton's answer.