Linkers, Loaders & Executables

On a microcontroller without an OS, what does the start-up code do right after power-on before calling main?

  1. AWrite all .text machine code back from RAM to flash
  2. BRead RAM and restore variable values from before the last power-off
  3. CCopy the initial values of .data from flash to RAM and fill the .bss area with zeros
Show answer and explanation

Answer: C. Copy the initial values of .data from flash to RAM and fill the .bss area with zeros

RAM loses its contents at power-off, so the initial values of variables are kept in non-volatile flash, and the start-up code copies them to the variables' locations in RAM. The area for uninitialized variables (.bss) is filled with zeros. The linker script gives .data separate addresses: where it is stored in flash and where it is used in RAM at run time. Only after these steps can main begin in the state that C promises.

  • A. Nothing is written back.
  • B. Ordinary RAM loses its contents at power-off.
  • C. Correct. This honors C's promises about initial values.
Practice this topic Back to topic

More Linkers, Loaders & Executables questions

  1. What is the difference between "sections" and "segments" in an ELF file?
  2. Which attack does W^X (never allowing a page to be both writable and executable) make harder?
  3. On Linux, when is the version number in a shared library's soname, such as the ".1" in libfoo.so.1, generally increased?
  4. What does running the ldd command on an executable on Linux show?
  5. Which pairs major operating systems with the executable formats they use correctly?