I was reading Effective C++, 3rd edition and in item 2 (prefer const, enums, and inlines to #defines), Scott Meyers mentions the symbol table: he explains that #defines
may not appear in the symbol table.
Based on the answer here, a bit of the suggested reading thereof, and the Wikipedia article, I would define the symbol table as follows: since the compiler only creates object files for every translation unit, we still need a way to reference symbols between the translation units. This is done using a table that is created for every object file so that symbols can be defined at a later stage - by the linker when the executable/library is created from the object files. During linking, symbols are substitutes with their appropriate memory addresses by the linker.
Here's what I'd like to know:
Thank you for your time and contribution.
A compiler maintains two types of symbol tables: a global symbol table which can be accessed by all the procedures and scope symbol tables that are created for each scope in the program. To determine the scope of a name, symbol tables are arranged in hierarchical structure as shown in the example below: . . .
Symbol table is an important data structure used in a compiler. Symbol table is used to store the information about the occurrence of various entities such as objects, classes, variable name, interface, function name etc. it is used by both the analysis and synthesis phases.
The information in the symbol table is entered in the lexical analysis and syntax analysis phase, however, is used in later phases of compiler (semantic analysis, intermediate code generation, code optimization, and code generation).
Virtually every phase of the compiler will use the symbol table: The initialization phase will place keywords, operators, and standard identifiers in it. The scanner will place user-defined identifiers and literals in it and will return the corresponding token.
Symbol tables exist both for the compiler (and then the compiler puts even local variable symbols in them; even the preprocessor has some sort of symbol tables for #define
-d names, but the preprocessor might be inside the compiler today) and for the linker. But these are different tables, organized differently.
The linker symbol table is mostly for exported or imported global symbols. Be aware that the linker performs some relocation. Be aware that the linker is behaving quite differently on Windows and on Linux (dllimport
on Windows, __attribute__(visibility...)
on Linux). Notice that for dynamic libraries, some linking happens at runtime (dynamic loading). For C++, name mangling can happen. Read also about vague linkage & template instantiation & link-time optimization in GCC...
Read also Levine's book: Linkers and Loaders and e.g. the wikipage on the ELF format (used for object files, shared libraries and executables on Linux and many Unix systems).
If you have access to some Linux system, use the readelf(1), nm(1) and objdump(1) utilities. Read also Drepper's paper: How To Write Shared Libraries (on Linux)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With