[The World of Linkers—Lab 02] Symbols and Relocations: An Object File's Unfinished Business
Decode SYMTAB and RELA into records for later definition selection, layout, and patching. Theory 02's symbol table and relocation table explain their 24-byte layouts.
Starting point: class2 code and tests, in the private repository; access is required. Reuse class0/class1 range, section, and string access. Implement two interfaces in the class2 crate library without importing an ELF parser.
Implementation tasks
| Task | Interface | Required outcome |
|---|---|---|
| C2.1 | symbols | Read a selected SYMTAB, associate names, and retain binding/type/visibility, definition, value, and size |
| C2.2 | relocations | Read a selected RELA, validate symbol ownership, and retain target offsets, kinds, symbol indexes, and signed addends |
The class2 crate public API declares the interfaces; the class2 README specifies fields and diagnostic order. Both index arguments select entries in the current file's section list. Symbol.name borrows bytes from that input's associated string table. Do not substitute indexes from another file.
SYMTAB sh_link selects its name table; sh_info separates LOCAL from non-LOCAL records. RELA sh_link selects its symbol table; sh_info selects the section to patch. The symbol index packed in r_info belongs to that associated table, not a global name registry.
Make one record concrete: if entry 3 of the current object is SHT_SYMTAB with sh_link = 2, every st_name is resolved in entry 2's string table. If entry 5 is SHT_RELA with sh_link = 3 and sh_info = 1, each r_info symbol index refers to entry 3's symbol table, while r_offset addresses entry 1's section. After switching to another object, the same numeric indices must be interpreted through that object's tables again.
Symbol zero must be all-zero across its complete 24 bytes. Empty symbol tables are invalid; empty RELA tables can be valid after checking metadata and linked tables. Reject SHT_REL explicitly. Preserve unknown symbol attributes and relocation kinds. Since this stage does not patch, do not impose one premature write-width rule on r_offset.
Before decoding relocation records, sh_link must select an SHT_SYMTAB: an out-of-range index is InvalidIndex, while a section of another kind is WrongSectionType. Only then does the implementation call symbols to validate that table. sh_info selects a non-null target section; it is not an index into the relocation records.
Acceptance and what it establishes
From the cloned repository root on native x86-64 Linux:
cargo test --locked -p class0 -p class1 -p class2cargo test --locked -p class0 -p class1 -p class2 --releasepython3 scripts/grade.py class2Contract tests cover full widths, special definitions, local ordering, names, null records, truncation, and table ownership. The native test assembles an unresolved call and follows that RELA's own sh_link to check the symbol and addend, rather than assuming assembler-specific indexes.
Passing establishes faithful input records. It does not prove that every referenced name is defined or write runtime addresses.