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.

From C to an Image the Bench Can LoadStep through the four forms of one program, from the text you write to the file the bench reads.
  • External
  • Memory
  • Debug
  • Data
  • Debug
From C to an Image the Bench Can LoadStep through the four forms of one program, from the text you write to the file the bench reads. external image.c C source: names, noaddresses external image.o object file: sections,symbols at 0,relocations memory firmware.elf linked: every addressfixed memory firmware.hex one 32-bit word a line external symbols, listing name to address; thedisassembly debug Chip bench +ram=, +symbols=,+set= cross-compilelink with the kitobjcopynm, objdumpback-door loadwrite a variable byname
  1. external image.cC source: names, no addresses
    • cross-compile → image.o
  2. external image.oobject file: sections, symbols at 0, relocations
    • link with the kit → firmware.elf
  3. memory firmware.elflinked: every address fixed
    • objcopy → firmware.hex
    • nm, objdump → symbols, listing
  4. memory firmware.hexone 32-bit word a line
    • back-door load → Chip bench
  5. external symbols, listingname to address; the disassembly
    • write a variable by name → Chip bench
  6. debug Chip bench+ram=, +symbols=, +set=
  1. Cross-compile

    A compiler on your computer writes RISC-V instructions for ap_rv32: rv32i with Zicsr, no C library.

  2. 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.

  3. Link

    The linker joins the object with the kit, places every section by the linker script and fills each hole.

  4. 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.

  5. 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.

Five questions this page answers
Question
1Why does a chip test need a cross-compiler?
2What is in an object file, and what is still missing?
3What does linking fix?
4How does the bench find a C variable in memory?
5When 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

New words on this page
WordMeaning
Cross-compilerA compiler that runs on one kind of computer and writes code for another: riscv64-unknown-elf-gcc for ap_rv32.
Object fileThe compiler's output for one source file: sections of code and data, not yet placed at any address.
SymbolA name the linker knows, with an address and a size: a function, a variable, or a label such as __bss_start.
RelocationA note in an object file: "put the final address of this symbol here". The linker fills each one.
LinkingJoining object files, placing their sections by the linker script, and filling every relocation.
Symbol tableThe list of symbols of the linked image: apcc writes it as JSON; the bench reads it with +symbols.
ListingThe linked image disassembled, address by address (apcc's .lst file).
Hex fileThe 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.

The lab program, image.c.
#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.

nm image.o (riscv64-unknown-elf-gcc 14.2.0).
00000000 R banner
00000000 D blinks
00000000 T main
00000000 B seen
U tb_put_hex
U tb_putc
U tb_puts

Read 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.

Part of the symbol table apcc wrote
NameAddressSize (bytes)
main80000278108
blinks8000030c4
seen800003584
banner800003046
__bss_start800003100
__bss_end8000035c0

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

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

The bench's own lines, measured on our bench (soc_tb on Verilator 5.052 2026-09-05; riscv64-unknown-elf-gcc 14.2.0, -Os).
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=00000000

In 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

ap_soc~12 blocks
Phone-class SoChundreds of blocks

This section looks ahead. The lab image has one source file and three sections.

  1. 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.

  2. 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.

  3. 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?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: blinks at 8000030c, 4 bytes word 195 of SRAM line 1228 of the .hex file

Check Yourself

Predict the outputseen is at 80000358. What is the last line the slice prints?
// apcc's .hex file: 8 header words, then 1,024 words of ROM, then the words of SRAM, one a line.
// A variable's line follows from its address alone. Values from this program's symbol table.
module tb;
  localparam logic [31:0] SRAM_BASE = 32'h80000000;
  localparam int HEADER = 8, ROM_WORDS = 1024;
  function automatic int line_of(logic [31:0] address);
    return HEADER + ROM_WORDS + int'((address - SRAM_BASE) >> 2) + 1;   // lines count from 1
  endfunction
  initial begin
    $display("seen at %h, %0d bytes", 32'h80000358, 4);
    $display("word %0d of SRAM", (32'h80000358 - SRAM_BASE) >> 2);
    $display("line %0d of the .hex file", line_of(32'h80000358));
  end
endmodule
ConceptWhy are all addresses 0 in the object file?
ConceptWhat does the bench need to write a C variable before the first instruction?
ConceptThe bench wrote blinks = 5, and the program used 0. What happened?
ConceptWhy should a chip test not be graded on its cycle count?

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.

Practice

Report a problem with this page