static function in C

前端 未结 7 1584
天涯浪人
天涯浪人 2020-11-30 17:11

What is the point of making a function static in C?

7条回答
  •  春和景丽
    2020-11-30 17:35

    pmg's answer is very convincing. If you would like to know how static declarations work at object level then this below info could be interesting to you. I reused the same program written by pmg and compiler it into a .so(shared object) file

    Following contents are after dumping the .so file into something human readable

    0000000000000675 f1: address of f1 function

    000000000000068c f2: address of f2(staticc) function

    note the difference in the function address , it means something . For a function that's declared with different address , it can very well signify that f2 lives very far away or in a different segment of the object file.

    Linkers use something called PLT(Procedure linkage table) and GOT(Global offsets table) to understand symbols that they have access to link to .

    For now think that GOT and PLT magically bind all the addresses and a dynamic section holds information of all these functions that are visible by linker.

    After dumping the dynamic section of the .so file we get a bunch of entries but only interested in f1 and f2 function.

    The dynamic section holds entry only for f1 function at address 0000000000000675 and not for f2 !

    Num: Value Size Type Bind Vis Ndx Name

     9: 0000000000000675    23 FUNC    GLOBAL DEFAULT   11 f1
    

    And thats it !. From this its clear that the linker will be unsuccessful in finding the f2 function since its not in the dynamic section of the .so file.

提交回复
热议问题