Closure Blocks

The linker is very good at deadstripping unused code and data. Sometimes, however, symbols need to be kept in the output file even if they are never directly referenced. Interrupt handlers, for example, are usually linked at special addresses, without any explicit jumps to transfer control to these places.

Closure blocks provide a way to make symbols immune from deadstripping. The closure is transitive, meaning that symbols referenced by the symbol being closed are also forced into closure, as are any symbols referenced by those symbols, and so on.

Note: The closure blocks need to be in place before the SECTIONS definition in the linker command file.

The two types of closure blocks available are:

Listing: Sample symbol-level closure block
FORCE_ACTIVE {break_handler, interrupt_handler, my_function}
Listing: Sample section-level closure block
KEEP_SECTION {.interrupt1, .interrupt2}

A variant is REF_INCLUDE. It keeps a section in the link, but only if the file where it is coming from is referenced. This is very useful to include version numbers. The following listing shows an example of this.

Listing: Sample section-level closure block with file dependency
REF_INCLUDE {.version}