Search code examples
x86function-pointersdisassemblystack-framestatic-functions

Why do static functions sometimes have non standard stack frames?


In the wikibook x86 Disassembly, it is written that sometimes there are subroutines that do not set up standard stack frames. One such case is when we declare a static function in C. The following lines have been written in the book.

When an optimizing compiler sees a static function that is only referenced by calls (no references through function pointers), it "knows" that external functions cannot possibly interface with the static function (the compiler controls all access to the function), so the compiler doesn't bother making it standard.

I have following questions regarding the above statement :

  1. Is it possible for an external function to refer to a static function using function pointer? If yes, how? Why is it allowed? (I am asking this because as far as I know, static functions have local scope and cannot be accessed by any external functions)?
  2. What does having a non-standard stack frame mean? How do non-standard stack frames differ from the standard ones? An explanation using assembly code would be welcome :)

Edit: Another question I would like the answer to: Why does the compiler in the above mentioned case set up non-standard stack frame instead of the standard one?


Solution

    1. Sure, as long as it's another function in the same translation unit taking a pointer to the static function and giving it to code in a different translation unit. The function's somewhere, after all. Making it static prevents other objects from finding it, but it doesn't prevent them from being handed it.
    2. No argument pushing or popping, for example, or the return value being stored wherever the compiler likes. Basically, somewhere between standard calling convention, and inlining the function body. Though inlining the function body is the most likely outcome.