Symbol Preemption

Sometimes you may need to use some of the functions or data items from a shareable object, but may wish to replace others with your own definitions.  For example, you may want to use the standard C runtime library shareable object, libc.so, but to use your own definitions of the heap management routines malloc() and free().  In this case it is important that calls to malloc() and free() within libc.so call your definition of the routines and not the definitions present in libc.so.  Your definition should override, or preempt, the definition within the shareable object.

This feature of shareable objects is called symbol preemption.  When the runtime loader loads a component, all symbols within the component that have default visibility are subject to preemption by symbols of the same name in components that are already loaded.  Since the main program image is always loaded first, none of the symbols it defines will be preempted.

The possibility of symbol preemption inhibits many valuable compiler optimizations because symbols with default visibility are not bound to a memory address until runtime. For example, calls to a routine with default visibility cannot be inlined because the routine might be preempted if the compilation unit is linked into a shareable object.  A preemptable data symbol cannot be accessed using GP-relative addressing because the name may be bound to a symbol in a different component; the GP-relative address is not known at compile time.

Symbol preemption is a very rarely used feature that has drastic negative consequences for compiler optimization.  For this reason, by default the compiler treats all global symbol definitions as non-preemptable (i.e., protected visibility).  Global references to symbols defined in other compilation units are assumed by default to be preemptable (i.e., default visibility).  In those rare cases when you need all global definitions, as well as references, to be preemptable, specify the -fpic option to override this default.