Symbol Table Corruption Issue and Solution¶
Summary¶
The pcc-nd500 linker has a symbol table corruption bug that truncates symbol names and corrupts the string table. This issue cannot be avoided, but we work around it by using the .map file for symbol resolution instead of the embedded symbol table.
The Problem¶
When linking object files (even a single object file) to create an executable, the pcc-nd500 linker corrupts the symbol table:
Symptoms:
- Symbol names truncated to 3-4 characters
- String table contains garbage
- Most symbols show as
(null)or random fragments
Example:
Expected: Got:
_kernel_main → ain
_init_kernel → ler
_scheduler → ler
_proctab → roc
Root Cause Analysis¶
Test Case 1: Object File (Before Linking)¶
$ nd500-dump -s kernel.o | head -30
Result: ✓ Symbols are GOOD
Idx Name Type Value
0 _kernel_main TEXT|EXT 0x0000000a
1 _sys_read DATA 0x00000021
2 _sys_exit BSS 0x00000036
3 _init_kernel TEXT|EXT 0x00000122
4 _scheduler TEXT|EXT 0x000001f3
...
Test Case 2: After Linking (Single Object)¶
$ nd500-ld -i -e start -m -o kernel kernel.o
$ nd500-dump -s kernel | head -30
Result: ✗ Symbols are CORRUPTED
Idx Name Type Value
0 TEXT 0x0000000a
1 ain EXT 0x0000001d
2 ead BSS 0x00000032
3 xit UNDF 0x00000046
4 rea BSS 0x0000005f
...
Test Case 3: After Linking (Multiple Objects)¶
$ nd500-ld -i -e start -m -o kernel locore.o kernel.o
$ nd500-dump -s kernel | head -30
Result: ✗ Symbols EVEN MORE CORRUPTED
Idx Name Type Value
0 (null) UNDF 0x00000000
1 GES EXT 0x0000001e
2 BPG UNDF 0x0000002c
3 47; UNDF 0x00000060
4 :G1 UNDF 0x000003bf
...
Conclusion¶
The pcc-nd500 linker has a fundamental bug in its symbol table / string table handling.
This corruption happens:
- ✓ With or without
-xflag (preserve locals) - ✓ With single object file
- ✓ With multiple object files (worse)
- ✓ Regardless of source file structure
The bug is in the linker, not our code.
Attempted Fixes (All Failed)¶
Attempt 1: Remove -x Flag¶
Theory: Maybe -x (preserve local symbols) causes corruption.
Test:
LDFLAGS = -i -e start -m # Without -x
Result: ✗ Still corrupted (different garbage, but still broken)
Attempt 2: Single Object File¶
Theory: Maybe linking multiple objects corrupts string table merge.
Implementation:
# Combine locore.s + kernel.s before assembling
cat locore.s kernel.s > kernel_combined.s
nd500-as kernel_combined.s -o kernel.o
nd500-ld -i -e start -m -o kernel kernel.o # Single object!
Result: ✗ Still corrupted (slightly better, but still broken)
Attempt 3: Different Linker Flags¶
Tests:
- Without
-m(map generation) - Without
-i(IMAGIC format) - With
-S(strip symbols)
Results: ✗ All variations still corrupt or remove symbols entirely
The Solution: Use .map Files¶
Since the linker corrupts embedded symbols, we use separate symbol map files instead.
How It Works¶
Step 1: Assembler generates .map file
nd500-as -am kernel.s -o kernel.o
# Creates: kernel.map (line number → address mapping)
Step 2: Linker updates .map with final addresses
nd500-ld -m -o kernel kernel.o
# Updates: kernel.map with relocated addresses
Step 3: Debugger loads .map separately
// In JavaScript (WASM debugger)
await this.handleMapUpload(new File([mapContent], 'kernel.map'));
// In C (native debugger)
ndlib_map_load("kernel.map");
Step 4: Symbol resolution uses .map
const char* symbol = ndlib_symbols_lookup_addr(address);
// Returns: "_kernel_main" (from .map, not from symbol table)
Map File Format¶
Example kernel.map:
; Source: kernel.s
; Assembly line → Address mapping
0000000A _kernel_main
00000021 _sys_read
00000036 _sys_exit
00000122 _init_kernel
000001F3 _scheduler
Map file contains:
- Symbol names (full, not truncated)
- Addresses (relocated by linker)
- Source file line numbers
- Type information
Advantages¶
✓ Full symbol names - No truncation ✓ Reliable - Generated by assembler, not corrupted by linker ✓ Complete - Includes all symbols (local + global) ✓ Source mapping - Line numbers for source-level debugging
Implementation in kernel.zip¶
Files Included¶
kernel.zip contains:
- locore.c (Bootstrap C source)
- locore.s (Bootstrap assembly)
- kernel.c (Kernel C source)
- kernel.s (Kernel assembly)
- kernel (Executable with corrupted symbols - still usable!)
- kernel.map (Symbol map - THIS IS THE KEY!)
WASM Debugger Loading¶
File: src/frontend/nd500wasm/web/debugger.js
async handleZipUpload(file) {
for (const [filename, zipEntry] of Object.entries(zip.files)) {
if (filename.endsWith('.map')) {
// Load map file for symbols
const content = await zipEntry.async('string');
await this.handleMapUpload(new File([content], filename));
}
// ... handle other files
}
}
Native Debugger Loading¶
File: src/ndlib/ndlib_aout.c
int ndlib_load_aout_with_debug(Nd500Machine* m, const char* aout_path, ...) {
// Load executable
ndlib_loadaout_file_ex(m, aout_path, &entry, NULL);
// Load symbols from a.out (corrupted, but try anyway)
ndlib_symbols_load(aout_path);
// Load .map file (overrides corrupted symbols)
if (auto_map) {
strcpy(ext, ".map");
ndlib_map_load(alt_path); // THIS FIXES THE SYMBOLS!
}
}
Workaround Status¶
| Component | Status | Details |
|---|---|---|
| Executable symbols | ✗ Corrupted | Linker bug - cannot fix |
| Map file generation | ✓ Working | Assembler + linker generate correct .map |
| WASM symbol loading | ✓ Working | Loads from .map via JavaScript |
| Native symbol loading | ✓ Working | Loads from .map via C |
| Debugger display | ✓ Working | Uses .map symbols, ignores corrupted table |
| Source mapping | ✓ Working | .map includes line numbers |
Verification¶
Test Symbol Resolution¶
WASM Debugger:
// Open http://localhost:8000
// Load Demo Kernel
// Check browser console:
console.log(Module.ccall('nd500_symbols_lookup_addr', 'string', ['number'], [0x1A]));
// Should return: "_kernel_main" (from .map, not corrupted table)
Native Debugger:
./build/bin/nd500x --debug
> load examples/05-c-kernel/kernel
> symb
# Should show full symbol names from kernel.map
Compare Sources¶
Corrupted symbol table (embedded):
ain, ler, rea, buf, (null), GES, BPG, 47;, :G1
Good symbol map (kernel.map):
_kernel_main, _init_kernel, _init_proctab, _sys_read, _scheduler
Build Process (Current)¶
Makefile Steps¶
# Step 1: Preprocess locore.c → locore.s
$(CC) -E -DLOCORE locore.c > locore.s
# Step 2: Compile kernel.c → kernel.s
$(CC) -S kernel.c -o kernel.s
# Step 3: Combine locore.s + kernel.s
cat locore.s kernel.s > kernel_combined.s
# Step 4: Assemble with map generation
$(AS) -am kernel_combined.s -o kernel.o
# Generates: kernel.map (GOOD SYMBOLS!)
# Step 5: Link (corrupts symbols, but updates .map)
$(LD) -i -e start -m -o kernel kernel.o
# Updates: kernel.map with final relocated addresses
# Corrupts: kernel symbol table (BUT WE DON'T USE IT!)
# Step 6: Package everything
zip kernel.zip locore.c locore.s kernel.c kernel.s kernel kernel.map
Key: We include kernel.map in the ZIP, which has GOOD symbols.
Future Considerations¶
If pcc-nd500 Linker is Fixed¶
If the linker bug is eventually fixed:
- Embedded symbols would work correctly
.mapfiles would still be generated (no harm)- Debugger would prefer embedded symbols over .map (faster)
- Fallback to .map if embedded symbols missing
Alternative: Use nm-style External Symbols¶
Could generate external symbol file from object files:
# Extract symbols before linking (while still good)
nd500-dump -s kernel.o > kernel.sym
# Use kernel.sym instead of corrupted embedded table
But .map files are better because they include source line mappings.
Conclusion¶
Problem: pcc-nd500 linker corrupts symbol tables
Impact: Embedded symbols are unusable for debugging
Solution: Use .map files generated by assembler/linker
- ✓ Full symbol names
- ✓ Source line mappings
- ✓ Already implemented in WASM and native debuggers
- ✓ Included in kernel.zip
Status: ✓ RESOLVED (via workaround)
The kernel works correctly, debugging works correctly, symbols resolve correctly - all thanks to the .map file, NOT the corrupted symbol table.
Files:
examples/05-c-kernel/Makefile- Builds with .map generationexamples/05-c-kernel/kernel.map- Symbol map (good symbols)examples/05-c-kernel/kernel- Executable (corrupted symbols - ignored)
Build command:
cd examples/05-c-kernel
make clean
make all
Result:
- kernel.zip contains all 6 files including kernel.map
- Debugger loads symbols from kernel.map
- Full symbol names available for debugging