Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does C limit a static function's use to only its file?

Tags:

c

assembly

I understand that a static function in C allows that particular function to only be call within the confines of that file. What I am interested in is how this occurs. Is it being placed into a specific part of memory or is the compiler applying a specific operation to that function. Can this same process be applied to a function call in assembly?

like image 257
Blackninja543 Avatar asked Jul 23 '12 14:07

Blackninja543


2 Answers

Declaring a function static doesn't really prevent it from being called from other translation units.

What static does is it prevents the function from being referred (linked) from other translation units by name. That will eliminate the possibility of direct calls to that function, i.e calls "by name". To achieve that, the compiler simply excludes the function name from the table of external names exported from the translation unit. Other than that, there's absolutely nothing special about static functions.

You still can call that function from other translation units by other means. For example, if you somehow obtained a pointer to static function in other translation unit, you can call it through that pointer.

like image 86
AnT Avatar answered Oct 04 '22 00:10

AnT


It doesn't make it into the object's name table which prevents it from being linked into other stuff.

like image 21
djechlin Avatar answered Oct 04 '22 00:10

djechlin