Object file sections
Symbol table provides a mapping of symbols to addresses just like the one in your assembler. But it only contains things that:
Might be needed by another file (globals and functions you define)
You don’t have resolved (globals and functions others define that you need)
Relocation table holds addresses of instructions that need to be fixed up with updated addresses. It contains fields that tell you:
- What address the instruction that needs to be fixed is at
- What type of instruction/directive you have (lw, sw, jal, etc)
- What symbol you are using there
C’s static
keyword
- C variables with the
static
keyword don’t need to (but still can) go to the symbol table. If they are included, they are marked as static
. We will accept both answers, so long as you are consistent about it.
- Instructions that use such variables must still go to the relocation table.
- These statements are true for both global and local variables with the
static
keyword.
Instructions that need to be in the relocation table are anything that uses a symbol in the object file’s symbol table or instructions that use static variables. Few example types:
- Function calls to functions in other files
- Function calls to the same file if the ISA does not use PC-relative branches for function calls. For ISAs such as LC2K, include all function calls. If the ISA is not specified, it is safe to include them, but be consistent in including or discluding.
- Uses of things in the data section
** Note: ** Statements that only declare variables do not translate to instructions in the binary. So there is nothing to relocate for such statements.
Where data goes
- All local non-static variables have space allocated on the stack. For arguments, the first few go into registers (specified by the ABI). The rest go on stack.
- Dynamically allocated memory goes on the heap.
- Global and static variables go into data segment.
- Constants, including constant arrays or strings, may sometimes be encoded in immediates of instructions (e.g. ARM’s MOVZ). If the ISA, like LC2K, does not support this, they must go into the data segment and be loaded. For arrays, some compilers will make this decision based on the size of the array.