Excerpt from http://eli.thegreenplace.net/
Here’s what the linker does:
The linker maintains a symbol table.
This symbol table does a bunch of things, but among them is keeping two lists:
When the linker encounters a new object file, it looks at:
When the linker encounters a new library, things are a bit more interesting.
Here’s what the linker does:
The linker maintains a symbol table.
This symbol table does a bunch of things, but among them is keeping two lists:
- A list of symbols exported by all the objects and libraries encountered so far.
- A list of undefined symbols that the encountered objects and libraries requested to import and were not found yet.
When the linker encounters a new object file, it looks at:
- The symbols it exports: these are added to the list of exported symbols mentioned above.
- If any symbol is in the undefined list, it’s removed from there because it has now been found.
- If any symbol has already been in the exported list, we get a "multiple definition" error: two different objects export the same symbol and the linker is confused.
- The symbols it imports: these are added to the list of undefined symbols, unless they can be found in the list of exported symbols.
When the linker encounters a new library, things are a bit more interesting.
- The linker goes over all the objects in the library. For each one, it first looks at the symbols it exports.
- If any of the symbols it exports are on the undefined list, the object is added to the link and the next step is executed. Otherwise, the next step is skipped.
- If the object has been added to the link, it’s treated as described above – its undefined and exported symbols get added to the symbol table.
- Finally, if any of the objects in the library has been included in the link, the library is rescanned again – it’s possible that symbols imported by the included object can be found in other objects within the same library.
$ gcc simplemain.o -L. -Wl,--start-group -lbar_dep -lfunc_dep -Wl,--end-group $ gcc simplemain.o -L. -Wl,--undefined=bar -lbar_dep -lfunc_dep
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.