topic description
In the process of compiling C language, thecompiler must determine the address of each defined symbol (variable, array, function, label, etc.) by manipulating the symbol table, so as to translate the text code into binary machine code. The symbol table section (.symtab) in the resulting ELF file (.o / .exe) contains only variables of storage type static/extern, but not auto local variables. So, how are local variables translated by the compiler?
sources of topics and their own ideas
Source: the compilation process must rely on the symbol table. The compilation process outputs the ELF file, while the symbol table section (.symtab) of the ELF file does not contain local variables. So, how are local variables compiled?
own idea:
1.ELF file is the output of the compilation process and the final result, and its symbol table section (.symtab) is only the final result of the symbol table operation, which can not reflect the symbol table change of the whole compilation process. Therefore, during the compilation process, the local variable will appear in the symbol table, and the compiler determines the address of the local variable (the local variable is located in the stack and its address is the offset from the stack pointer sp), and then realizes the translation of the local variable.
2. The operation of the symbol table includes adding items, finding items, and deleting items. At the end of the compilation process, all functions and blocks will jump out of scope, and accordingly, stack memory will unstack. The associated local variables will also be deleted from the symbol table, leaving only static variables static, global variables and external variables extern. Therefore, the symbol table section (.symtab) of the final output ELF file does not have the contents of local variables.
problem expectation
1. Is the above idea correct?
2. If it is not correct, where is the mistake? What knowledge do you need to look for in order to understand the right way of thinking?