0

Assuming there's some function in my program or any linked library to it, is there a way to get the length of the function in bytes? By the length I mean the size of the whole code inside the function, from the start to the very end (functions branch out, so the end would be e.g. last 'ret' instruction, or something like that). Is there a simple way to do that, maybe some API functions or is it too much non cost-effective to create such a function?

pazdinho_
  • 43
  • 1
  • 5
  • 1
    No, there's no way to do that. Why would you need it? – Barmar Sep 11 '21 at 21:07
  • 4
    Does this answer your question? [Getting The Size of a C++ Function](https://stackoverflow.com/questions/5655624/getting-the-size-of-a-c-function) – The Average Google User Sep 11 '21 at 21:08
  • What about other resources (i.e. constants)? – tstanisl Sep 11 '21 at 21:09
  • There is no portable way. You could subtract a pointer to the function from a pointer to the next function, but that is dependent on the C compiler. Generating assembly code would tell you if it would work. – stark Sep 11 '21 at 21:41

1 Answers1

0

There are probably tools to dump the contents of an object file, and then you can parse the output of this dump, and there is a chance that you can find out more or less reliably what the length of s function is. Adding data that is used by the function is more tricky. Or adding things like lambdas / closures that should probably be counted towards the length of the function.

gnasher729
  • 51,477
  • 5
  • 75
  • 98