Section 18.2.15

Figure 10a: linker footprint totals and memory categories

Definition

A linker footprint total is the number of bytes that a program occupies in one memory category, summed from the section sizes the linker §12.4 reports. The linker joins compiled files into one program image and gives every part an address. It groups bytes into sections: .text holds machine code, .rodata constants, .data variables whose initial value is not all zeros, and .bss variables that start at zero. On a microcontroller §6.1, a single-chip computer, the memory categories are flash memory §6.6, which keeps its content without power, and RAM, the working memory.

Where it fits

Main entry: Static versus dynamic footprint (§11.38).

On a microcontroller that runs its code directly from flash, in a simple layout, the flash total is .text plus .rodata plus .data, and the RAM reserved at build time is .data plus .bss. The size command of common compiler toolchains §12.2 prints three totals, named text, data and bss; its text total includes the constants. A .data byte counts in both totals because it exists twice. The variable must be writable, so it lives in RAM. That RAM loses its content without power, so the initial value is stored in flash, and the start-up code, which runs before the program’s main function, copies it into RAM. A .bss variable needs no stored value: the start-up code writes zeros over its RAM range, for which it needs only the start address and the length. So a .bss byte costs RAM only. This matters when two builds are compared. Declaring a global table const moves it from .data to .rodata: RAM falls by the table’s size and flash does not change. Changing a global variable’s initial value from 0 to 1 moves it from .bss to .data: flash grows by the variable’s size and RAM does not change.

The executable file is not the flash image. Besides the bytes that go to the device, the ELF §12.6 (Executable and Linking Format) file that the linker writes holds headers and a symbol table §12.8, which lists every named function and variable with its address. It usually also holds debug information §12.9, which links machine code to source lines and is often larger than the code. None of these is written to the device. A .bss section has a size but no bytes in the file. Files converted for a programming tool differ again. An Intel HEX §12.7 file (Intel hexadecimal object file format) writes each byte as two text characters and adds a byte count, an address, a record type and a checksum §4.32 to every line. A raw binary file covers one continuous range of addresses, so a gap between two flash regions becomes filler bytes.

In an invented build, the ELF file is 180,000 bytes long, and the linker reports .text 12,288 bytes, .rodata 2,048 bytes, .data 512 bytes and .bss 4,096 bytes. Flash use is 12,288+2,048+512=14,84812{,}288 + 2{,}048 + 512 = 14{,}848 bytes. That is 14,848/1024=14.514{,}848 / 1024 = 14.5 KiB, or about 14.8 kB, where 1 kB (kilobyte) is 1000 bytes. RAM reserved at build time is 512+4,096=4,608512 + 4{,}096 = 4{,}608 bytes, which is 4.5 KiB. The file is 180,000/14,848≈12.1180{,}000 / 14{,}848 \approx 12.1 times as large as the flash use.

A linker total counts reservations, not use. If .bss contains an 8 KiB heap area, the region from which the program requests memory while it runs, the static footprint (the memory reserved before the program runs) counts 8 KiB whether objects occupy 0 or 3 KiB of that area at run time. Adding the 3 KiB measured at run time to the 8 KiB reports 11 KiB and counts the same memory twice. Build reports often label the flash total ROM (read-only memory), because the running program reads its code and constants there and does not change them. All numbers in this entry’s examples are invented.

Questions answered

Can the executable file size be called ROM footprint?

No, not in general: the executable file size is the size of a file on the build computer, while the ROM (read-only memory) footprint is the number of bytes the program occupies in the device’s flash memory, which build reports often label ROM. The executable file, usually an ELF (Executable and Linking Format) file, also contains headers, a symbol table and debug information. None of these is written to the device, and together they can be many times larger than the code: a 180,000-byte file can hold a program that occupies 14,848 bytes of flash.

Read the ROM footprint from the section sizes the linker reports: machine code (.text), constants (.rodata) and the stored initial values of initialized variables (.data). The .data bytes also occupy RAM, where the variables live while the program runs. The file size equals the ROM footprint only for a file that holds exactly the bytes written to flash and nothing else, such as a raw binary image of one continuous flash region with no filler bytes.

Related entries: Peak versus retained memory (§11.37), Linker (§12.4), ELF (§12.6), Linker script (§12.5), Intel HEX (§12.7), Symbol table (§12.8), Debug information (DWARF) (§12.9), Figure 10b: signed source-group differences and attribution (§18.2.16).