1

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?

crisron
  • 363
  • 2
  • 5
  • 21

1 Answers1

1
  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.
Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • Can the external functions then call the static function through function pointer? – crisron Nov 10 '14 at 15:48
  • @crisron: yes, then the external function can call the static function through a function pointer. As Sneftel said: external code can't find it by name, but it can be exposed anonymously through a function pointer, and it can be called that way. – Rudy Velthuis Nov 10 '14 at 16:14
  • @RudyVelthuis: Why is it allowed? – crisron Nov 10 '14 at 17:10
  • @crisron Why wouldn't it be? What purpose would it serve to disallow such a thing? – Sneftel Nov 10 '14 at 17:25
  • @Sneftel: I meant that being static, external functions should not be able to call it even using function pointer. I do not understand how function pointer allows external functions to call a static function. – crisron Nov 10 '14 at 17:40
  • 1
    @crisron The only difference with static functions is that their name is not visible in the object file's header, and hence can't be found during the linking process. It has nothing to do with what can call what. It's all the same program. – Sneftel Nov 10 '14 at 17:46
  • @crisron: Static functions can not be found by name, but if a function exposes them, then that is fine. Depending on some conditions, the code might even expose a different one, and the others are still inaccessible. There is nothing wrong with that. – Rudy Velthuis Nov 10 '14 at 20:52
  • Why does the compiler in the case mentioned in the question set up a non-standard stack frame instead of the standard one? – crisron Nov 11 '14 at 07:22
  • 1
    @crisron Just to save a few instructions. – Sneftel Nov 11 '14 at 07:48
  • @Sneftel: If the arguments are not pushed on the stack, from where compiler might access them? Also, if the compiler stores them elsewhere, how might it save instructions because it will still have to store them somewhere and access them also. – crisron Nov 12 '14 at 16:50
  • @crisron I suggest you spend some time analyzing the code produced by optimizing compilers. – Sneftel Nov 12 '14 at 17:45
  • @Sneftel: Thanx for the help. Please add to your answer the answer to the question in the edit. – crisron Nov 13 '14 at 08:27