Zephyr
Zephyr

Reputation: 1621

Symbol table in linking

I read that in two pass linking, during first pass the symbol table of all object codes are combined into a single global table and all segments are stored into segment table with their addresses.

In 2nd pass, symbol resolution takes place by using global symbol table and segment table.

My question is, symbol table only contains value,symbol name and type entries then how does linker come to know the address of each symbol from the global symbol table during symbol resolution in 2nd pass?

Upvotes: 3

Views: 1455

Answers (1)

vitsoft
vitsoft

Reputation: 5775

When a symbol was defined at assembly time, it was assigned with segment:offset. And if it was declared as PUBLIC, it is stored in global symbol table.

When segments are linked into executable image, each segment obtains its relative virtual address. Adding the offset to this segment's RVA gives the value of symbol. Typical public symbol is the entry of library function.

So the answer to your question: offset is specified at assembly time by the compiler, together with segment's name. Segment address is specified at link time by the linker when segments are ordered one after another. Together they yield the desired symbol value (address).

If another object code refers to this public symbol (calls the library function), it is declared as EXTERN and its value is not knowm at assembly time, hence it is temporarily set to zero and accompanied with data structure called relocation. Relocation specifies address of this temporary zero, and name of external symbol. Linker reads the relocation, finds the corresponding public symbol with matching name and then replaces the temporary zero with value of the public symbol.

Upvotes: 3

Related Questions