From C to an Image: Toolchain, Object Files and Symbols
The bench never sees your C. It loads words into memory and writes variables by name. Follow one program from source to object file to linked image. Then use its symbol table to change a test without rebuilding it.
You Will Be Able To
- Name the four forms of a program: source, object file, linked image, hex file.
- Read an object file's symbols and say which are defined, which are missing, and why every address is 0.
- Find a variable's address in the symbol table and its line in the hex file.
- Make a variable the bench can set through the symbol table, and say when start-up code undoes the write.
- Lesson
- V0.6
- Time
- 26 min
- Runs on
- Your browser
- Check
- 5 questions
Forty tests share one image. Each needs a different loop count, but forty builds would mean forty images to keep, load and trust. The bench offers a shortcut: it writes one C variable by name before the core starts. Your first try sets the variable, and the program ignores it.
- External
- Memory
- Debug
- Data
- Debug
- Cross-compile
A compiler on your computer writes RISC-V instructions for ap_rv32: rv32i with Zicsr, no C library.
- The object file
Each function and variable is in a section of its own, at address 0. Calls to the kit are holes, listed as relocations.
- Link
The linker joins the object with the kit, places every section by the linker script and fills each hole.
- Image and symbols
The tool objcopy turns the placed bytes into words for $readmemh. The tools nm and objdump write the symbol table and the listing.
- The bench uses both
The bench loads the words into ROM and SRAM. It uses the symbol table to write a C variable before the first instruction.
Read the diagram as text
- image.c: C source: names, no addresses
- image.o: object file: sections, symbols at 0, relocations
- firmware.elf: linked: every address fixed
- firmware.hex: one 32-bit word a line
- symbols, listing: name to address; the disassembly
- Chip bench: +ram=, +symbols=, +set=
- image.c → image.o: cross-compile
- image.o → firmware.elf: link with the kit
- firmware.elf → firmware.hex: objcopy
- firmware.elf → symbols, listing: nm, objdump
- firmware.hex → Chip bench: back-door load
- symbols, listing → Chip bench: write a variable by name
The firmware kit's build (apcc) for the lab program, riscv64-unknown-elf-gcc 14.2.0, -Os.
Before You Start
Read Before main first: this page uses its sections and its linker script.
| Question | |
|---|---|
| 1 | Why does a chip test need a cross-compiler? |
| 2 | What is in an object file, and what is still missing? |
| 3 | What does linking fix? |
| 4 | How does the bench find a C variable in memory? |
| 5 | When does start-up code undo the bench's write? |
The Idea: The Bench Speaks Addresses, You Speak Names
You write blinks and main. The chip knows only addresses and words. Between the two sits the toolchain: a compiler, an assembler, a linker and a few small tools. Chip verification cares about each step, because each leaves a file the bench or the engineer will read.
The compiler runs on your computer but writes instructions for another processor, ap_rv32. That is cross-compiling. The linker decides every address, using the linker script. Then the bench needs two things back: the words to load, and a table from names to addresses. With that table, a bench can read or write a C variable without knowing C.
New Words
| Word | Meaning |
|---|---|
| Cross-compiler | A compiler that runs on one kind of computer and writes code for another: riscv64-unknown-elf-gcc for ap_rv32. |
| Object file | The compiler's output for one source file: sections of code and data, not yet placed at any address. |
| Symbol | A name the linker knows, with an address and a size: a function, a variable, or a label such as __bss_start. |
| Relocation | A note in an object file: "put the final address of this symbol here". The linker fills each one. |
| Linking | Joining object files, placing their sections by the linker script, and filling every relocation. |
| Symbol table | The list of symbols of the linked image: apcc writes it as JSON; the bench reads it with +symbols. |
| Listing | The linked image disassembled, address by address (apcc's .lst file). |
| Hex file | The image as text for $readmemh: one 32-bit word a line. |
The Smallest Example: An Object File Read by Hand
The lab program has one knob, blinks, with a first value, one counter without, and one constant string.
#include "tb.h"
volatile unsigned blinks = 3; /* a knob: the bench may write it through the symbol table */
volatile unsigned seen; /* starts at 0 */
const char banner[] = "image"; /* never written */
int main(void)
{
for (unsigned i = 0; i < blinks; i++)
seen = seen + 1;
tb_puts(banner);
tb_puts(" blinks=");
tb_put_hex(seen);
tb_putc('\n');
return 0;
}
Compile it alone, without linking, and ask nm for its symbols. Each line is an address, a letter and a name.
00000000 R banner
00000000 D blinks
00000000 T main
00000000 B seen
U tb_put_hex
U tb_putc
U tb_putsRead the letters. T is code (main) and D is data with a first value (blinks). B is data that starts at 0 (seen), and R is read-only (banner). U means "used here, defined somewhere else": tb_puts and its friends live in the kit's tb.c.
Every address is 0, because nothing is placed yet. The object file holds 30 relocations: places where a final address goes in later. One is the address of blinks inside main's load instruction.
After Linking: Addresses and Words
Linked with the kit for the SRAM layout, the program has three placed sections. The .text section holds 778 bytes at 80000000. The .data section holds 4 bytes at 8000030c, and .bss 76 bytes at 80000310. The constant string went into .text, which the kit's script also uses for read-only data.
| Name | Address | Size (bytes) |
|---|---|---|
| main | 80000278 | 108 |
| blinks | 8000030c | 4 |
| seen | 80000358 | 4 |
| banner | 80000304 | 6 |
| __bss_start | 80000310 | 0 |
| __bss_end | 8000035c | 0 |
riscv64-unknown-elf-gcc 14.2.0, -Os. Another compiler or option gives other addresses.
Now find blinks in the hex file. The file starts with 8 header words, then the 1,024 words of ROM, then SRAM from 0x8000_0000. blinks is at 8000030c: (0x30c / 4) = word 195 of SRAM, so line 8 + 1,024 + 195 + 1 = 1228. That line of the file reads 00000003: the 3 of the source.
The chip bench takes this file in two parts. The 1,024 ROM words become its +rom image, and the SRAM words its +ram image. The eight header words serve the core's own bench.
The counter seen has a line too, but it holds nothing useful. The .bss section is not in the image, so its words are zeros like every unused word. Start-up code clears them again anyway.
The Rules
Rule 1: Build for the target, never the host.
apcc calls riscv64-unknown-elf-gcc with -march=rv32i_zicsr -mabi=ilp32 and no C library.
Rule 2: Addresses are decided at link time.
Read them from the symbol table or the listing of this build, not from an old one.
Rule 3: The bench writes a variable through its symbol.
It needs a name in the table, a size, and an address it can reach by the back door.
Rule 4: A knob lives in .data and is read at run time.
Start-up code clears .bss after the bench's write, and a value the compiler already knows is never read.
Rule 5: Grade behaviour, not layout.
Addresses and cycle counts move when the compiler changes; console text and pad bytes do not.
Sources for These Rules
apcc and its outputs: tools/soc/fwkit/apcc.mjs and README.md; plan section 3.3.
The bench's +symbols, +set and +value: tools/soc/ap_soc/bench/soc_tb/README.md; facts 27.12 and 27.13.
Our graders never compare an exact cycle count or address (facts 27.13). GCC 14.2 and 13.2 gave 1,068 and 1,056 cycles for one program.
nm letters, objcopy, relocations: GNU binutils documentation. Not in the reference library; common practice.
Now Run It: One Image, Three Runs
The lab image ran three times on the chip bench. The first run used it as built. The second added +set=blinks +value=00000005. The third used a build where blinks has no first value.
as built: CONSOLE 553: image blinks=00000003
+set blinks=5: SYMBOL 0: blinks at 8000030c = 00000005 | CONSOLE 585: image blinks=00000005
knob in .bss: SYMBOL 0: blinks at 80000358 = 00000005 | CONSOLE 513: image blinks=00000000In the second run the bench printed SYMBOL 0: blinks at 8000030c = 00000005 before the first instruction: rule 3. The image did not change, yet the console says 5. That is one image serving many tests.
In the third run the write landed too, SYMBOL 0: blinks at 80000358 = 00000005, and the console says 0. The variable now sits in .bss, and start-up code cleared it after the bench wrote it: rule 4.
Why It Is Built This Way
Why write variables instead of rebuilding? A rebuild costs compile time for every test on every RTL drop. And each new image is one more thing that might differ. One image with knobs keeps the tested bytes identical, and the same image can later run on an emulator or on silicon.
Why one word a line? $readmemh reads that form directly, so the bench needs no loader of its own. The JSON symbol table is for programs. The listing is for people: it turns a pc in a trace back into a line of C.
The Trap: The Write That Lands and Changes Nothing
The third run is the trap from the opening. Nothing failed loudly. The bench reported its write, and the program ran to its pass word. Only the console showed that the knob had no effect.
A knob can fail silently in more than one way, and the symptom is always the same. The bench's line shows the write, and the program behaves as built. So a test that uses a knob should print it, or check it, and the grader should look at the effect. The practice problem gives you a knob that does exactly this.
At Scale
This section looks ahead. The lab image has one source file and three sections.
Debug information
Built with -g, the image also carries a map from every address to a line of source. Debuggers and trace viewers use it.
Map files
Large projects ask the linker for a map file: every section of every object and where it went. It is the first file to read when an image no longer fits.
Many images
A chip test suite may build hundreds of images. Teams pin the compiler version, because a new one moves every address.
Try It
Run the slice: it turns blinks's address into a line of the hex file.
Change HEADER to 0 and predict the line before you run it.
Replace blinks with main's address from the table above. Where would main be in the file?
blinks at 8000030c, 4 bytes word 195 of SRAM line 1228 of the .hex fileCheck Yourself
Common Mistakes
Wrong Take an address from an old listing.
Every build can move every symbol.
Fix Read the symbol table of the image under test.
Wrong Make a bench knob a variable with no first value.
It lands in .bss, and start-up code clears it after the bench writes it.
Fix Give it a first value, so it lives in .data.
Wrong Grade a firmware test on a cycle count or an address.
A different compiler gives different numbers for the same correct program.
Fix Grade the console, the pads and the effects in the chip.
Remember
- Source, object file, linked image, hex file: four forms of one program.
- An object file has sections at 0, names it uses but does not define, and relocations. Linking places and fills them.
- The symbol table maps names to addresses; the bench uses it to write a variable before the first instruction.
- A knob must live in .data and be read at run time.
- Grade effects, not addresses or cycles: they change with the compiler.