Symbol Table: store symbol definition
content
one symbol can only have one definition
types of symbol
does not contain
Relocatable Object File (.o)
code and data to be combined with other relocatable file
each .o is produced from exactly one .c file
Executable Object File (a.out)
Shared Object File (.so)
Global Symbols
defined in a module that can be referenced by other modules
non-static function, non-static global variable
External Symbols
global symbols that are referenced but not defined by current module
like external function printf
Local Symbols
symbols defined and only referenced in current module
static function, global variables, static variables either in function or global
Not in symbol table:
local variable in function
function arguments
string in function may be in symbol table, depending on compiler
static variable: exist in life time of program, but accessible only in function's scope
Strong Symbol: procedures and initialized globals
Weak Symbol: uninitialized globals, or declared with extern
.
if it is declared extern
, symbol has to be resolve by a strong symbol for linking.
extern
can check type mismatch
Linking Rules
Good Practice With Global Variable
use static
if can
initialize it
use extern
if you need to reference external variable
Header file: contain weak symbol that can be shared by multiple other files
files that include weak symbol of header file must reference with the same type (type checking using include
)
if there are strong symbols in header file, since header file replace everything in normal file, there will be strong symbol conflict in multiple files
Before relocation, global shared variables are not placed in byte code
After relocation, zero values are replaced
Static Libraries (.a archive files)
concatenating all related .o files into a single file with an index (archive)
resolve reference by looking for the symbols in one or more archives
Linking with Static Library
library provide .h
file so that we don't have to write weak symbols. (libc.a
is compiled with gcc automatically)
linking process in command line by order
Disadvantage
duplicate stored executable
duplicate when load into memory
Minor bug fix require rebuild and relink
Dynamic Linking:
link when executable first loaded and run (load-time linking)
ld-linux.so
handles dynamic linkinglibc.so
usually dynamically linkedlink after program has begun running
dlopen()
Region in file specify dynamic libraries
.interp section: which dynamic linker to use (ld-linux.so)
.dynamic section: name of dynamic libraries
ldd a.out
command see dynamically resolved libraries
Table of Content