About the symbol table management of auto local variables in the process of C compilation?

topic description

In the process of compiling C language, the

compiler 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?


has not been checked


The

symbol table does not always exist during compilation. During the compilation process, there is usually a data structure to store all the symbol, including the global symbol, the symbol on the stack, etc. But different symbol have different types, for example, the global symbol is the global type, the stack symbol is the auto type, only a specific kind of symbol needs to be written into the .symtab, and the auto type is not required.
addressing stack variables, because the compiler knows all the local variables in this function when compiling, so there is a step in compiler compilation that maps all local variables in the function, including those defined by user code, and the temporary stack variables generated by the compiler to the base of the current stack frame, which actually indicates how far the stack variable exists from the stack frame. As long as this is defined, the stack variable can be addressed. In general, abi specifies where the stack starts to be the storage area of the stack's local variables.

in addition, the symbol table is for linker to link a lot of object files to generate a binary, so there is no need to store local variables in the symbol table, because local variables are addressed relative to stack frames, and there is no need for linker to resolve its real address, which is the reason why there are no stack variables in .symtab.

Menu