Try Embedded

ELF, map files and binary inspection

Reading what the linker built with size, nm, objdump and the map file.

The ELF file is the truth

The .elf the linker produces holds the machine code, every symbol with its address and size, the section layout, and (with -g) debug information that maps addresses back to source lines. The .bin or .hex you flash is just the loadable bytes extracted from it. When you need to know what is really in your firmware, inspect the ELF.

Sections you will see

SectionContentsLives in
.isr_vectorThe vector tableStart of flash
.textCodeFlash
.rodataconst data, string literalsFlash
.dataInitialised globalsRAM at run time; initial values stored in flash
.bssZero-initialised globalsRAM; takes no flash
.stack, .heapReserved RAMRAM

Flash used = .text + .rodata + .data (the initial values). RAM used = .data + .bss + stack + heap.

size: the one-line budget

$ arm-none-eabi-size firmware.elf
   text    data     bss     dec     hex filename
  23544     412    6216   30172    75dc firmware.elf

text here includes .text, .rodata and the vector table. So this image needs 23544 + 412 bytes of flash and 412 + 6216 bytes of RAM. Run it in CI and fail the build if either grows past a limit.

nm: who is using the space

$ arm-none-eabi-nm --size-sort -S -C firmware.elf | tail -5
0800a1c4 00000400 r crc_table
20000120 00000800 b uart_rx_buffer
08004d10 00000a2c T _vfprintf_r
20000920 00001000 b log_ring
08001234 00001220 T main_task

Columns are address, size, type and name. The type letter tells you the section: T/t code, R/r read-only data, D/d initialised data, B/b bss; upper case is global, lower case is file-local (static). Here printf's engine costs 2.5 KB of flash, and two buffers cost 6 KB of RAM.

objdump: what the compiler generated

arm-none-eabi-objdump -d -S firmware.elf | less     # disassembly with source interleaved
arm-none-eabi-objdump -h firmware.elf               # section headers: sizes, VMA and LMA

Disassembly answers questions that source cannot: did this function inline, is this loop using the hardware divider, did volatile keep the read inside the loop?

readelf: the structure

arm-none-eabi-readelf -S firmware.elf     # sections
arm-none-eabi-readelf -l firmware.elf     # program headers: what gets loaded where
arm-none-eabi-readelf -s firmware.elf     # full symbol table

addr2line: from a crash address to a line

A fault handler that logs the program counter is only useful if you can map it back:

$ arm-none-eabi-addr2line -e firmware.elf -f -C 0x08002f4a
uart_rx_irq
/src/drivers/uart.c:142

Keep the exact ELF for every released build. Without it, field crash addresses cannot be decoded.

The map file

Ask the linker for one with -Wl,-Map=firmware.map. It is long, but three parts answer most questions:

  1. Memory configuration: the regions from the linker script with their origin and length.
  2. Linker script and memory map: every input section placed in every output section, with the address, size and the object file it came from:
 .text.uart_init
                0x08001a40       0x5c build/drivers/uart.o
 .bss.rx_buf    0x20000400      0x200 build/drivers/uart.o
  1. Discarded input sections: what --gc-sections removed.

It also shows why something was linked in: the "Archive member included to satisfy reference" list at the top names the object that pulled each library member in. That is how you discover that one printf("%f") dragged in the whole floating-point formatting library.

A small C program can show the same idea at run time, by printing where its objects ended up:

#include <stdio.h>
#include <stdint.h>

static const uint8_t table[4] = { 1, 2, 3, 4 };   // .rodata
static uint32_t counter = 7;                      // .data
static uint32_t scratch[8];                       // .bss

int main(void) {
  printf("table   at 0x%08X\n", (unsigned)(uintptr_t)table);
  printf("counter at 0x%08X\n", (unsigned)(uintptr_t)&counter);
  printf("scratch at 0x%08X (%u bytes)\n", (unsigned)(uintptr_t)scratch, (unsigned)sizeof scratch);
  return 0;
}

The simulator keeps every variable in RAM at 0x20000000 (only string literals and code go in its flash at 0x08000000), so all three print RAM addresses. Built with a real toolchain for an STM32, table would be in flash at 0x08xxxxxx, and the map file would list each under .rodata, .data and .bss.

Note: Tools such as puncover, Bloaty or the "memory" view in many IDEs present the same data graphically, by file and function. They all read the ELF and map file underneath.

Interview question: Flash is nearly full. How do you find out what to cut?

Answer: Run size for the totals, then nm --size-sort -S (or a tool like Bloaty or puncover) to list the largest symbols, and read the map file to see which object file and library each comes from and why it was pulled in. Common culprits are printf with float support, large const tables, unused HAL code (fix with -ffunction-sections, -fdata-sections and --gc-sections), inlining at -O2/-O3, and template instantiations. Then compare against the previous release to see what grew.

Interview question: A device in the field logged a HardFault with PC = 0x08003A1E. How do you find the cause?

Answer: Take the ELF from exactly that release and run addr2line -e fw.elf -f 0x08003A1E to get the function and line, or look it up in objdump -d -S. Also decode the stacked LR and the fault status registers (CFSR, HFSR, BFAR, MMFAR) if they were logged, since LR shows the caller and the status bits say whether it was a bus fault, an unaligned access, a divide by zero and so on.

Build it: what is eating the flash?

This is step 2 of the module project. The image is 2 KB too big. The linker's map file lists every function and table with its size and the object file it came from, and the team needs a short report: which files and which symbols are the biggest.

Lab: Read a linker map file

Each line of the map file's symbol section looks like this (fields separated by one or more spaces):

.text.metering_fft 0x08002a10 0x6c4 metering.o

the section and symbol name, the address in hex, the size in hex, and the object file.

  • parse_line(line, e) fills a map_entry_t (the name, without the leading section part up to and including the second dot, so .text.metering_fft becomes metering_fft; the size; and the object file) and returns true. A line that does not have all four fields, or whose address or size is not a 0x hex number, returns false: map files are full of lines like that.
  • report(lines, n) parses every line and prints, for each object file in order of first appearance, its total size in bytes, then the single biggest symbol overall, as shown in the checks.

strtoul, strncpy, strcmp and strchr are available.

Going deeper

Interview question: How would you find which code is using up a doorbell's flash?

Answer: Build with a map file and read the largest sections and symbols, or use a size tool (nm --size-sort, bloaty, puncover) on the ELF. Group the results by module to see which features cost most, look for library code pulled in by one call (such as printf with floats), and track the size per commit in CI so growth is noticed as it happens.

Interview question: Why should you archive the ELF file for every firmware release?

Answer: The flashed binary has no symbols or debug information. To decode crash addresses from the field with addr2line, inspect variables in a post-mortem debugger, or compare memory use between versions, you need the exact ELF that produced that binary; rebuilding later may not give identical addresses. Store it with the release, matched by version or build ID.

Interview question: How can you find out why a large library function was linked into the image?

Answer: The map file's section listing archive members included "to satisfy reference by" names the object file and symbol that pulled each library member in. Following that chain shows, for example, that one printf with %f brought in floating-point formatting. -Wl,--trace-symbol=name and -Wl,--print-gc-sections also help.

Explain it: In your own words, explain how to work out the flash and RAM used by a firmware image from the size output.

Answer: size reports text, data and bss. text covers code, constants and the vector table, which live only in flash. data is initialised variables: they occupy RAM at run time, but their initial values are also stored in flash so the startup code can copy them. bss is zero-initialised variables, which occupy RAM only. So flash used is text plus data, and static RAM used is data plus bss; the stack and heap come on top of that.