What does KEEP mean in a linker script?

linker script memory
clang linker script
linker script fill
linker script section definition
linker script noload
default linker script
linker script wildcard
linker script assert example

The LD manual does not explain what the KEEP command does. Below is a snippet from a third-party linker script that features KEEP. What does the KEEP command do in ld?

SECTIONS
{  
    .text :
    {
        . = ALIGN(4);
        _text = .;
        PROVIDE(stext = .);
        KEEP(*(.isr_vector))
        KEEP(*(.init))
        *(.text .text.*)        
        *(.rodata .rodata.*)        
        *(.gnu.linkonce.t.*)
        *(.glue_7)
        *(.glue_7t)
        *(.gcc_except_table)
        *(.gnu.linkonce.r.*)
        . = ALIGN(4);
        _etext = .;
        _sidata = _etext;
        PROVIDE(etext = .);   
            _fini = . ;
                *(.fini)

    } >flash

Afaik LD keeps the symbols in the section even if symbols are not referenced. (--gc-sections).

Usually used for sections that have some special meaning in the binary startup process, more or less to mark the roots of the dependency tree.


(For Sabuncu below)

Dependency tree:

If you eliminate unused code, you analyze the code and mark all reachable sections (code+global variables + constants).

So you pick a section, mark it as "used" and see what other section it references, then you mark those section as "used", and check what they reference etc.

The section that are not marked "used" are then redundant, and can be eliminated.

Since a section can reference multiple other sections (e.g. one procedure calling three different other ones), if you would draw the result you get a tree.

Roots:

The above principle however leaves us with a problem: what is the "first" section that is always used? The first node (root) of the tree so to speak? This is what "keep()" does, it tells the linker which sections (if available) are the first ones to look at. As a consequence these are always linked in.

Typically these are sections that are called from the program loader to perform tasks related to dynamic linking (can be optional, and OS/fileformat dependent), and the entry point of the program.

KEEP keyword in .ld file, What is the KEEP keyword used in the linker .ld file ? And how do I make sure the bootcode including config bits are protected? 11/20/2008. What does KEEP mean in a linker script? The LD manual does not explain what the KEEP command does. Below is a snippet from a third-party linker script that features KEEP.

the GNU linker, The gnu linker ld is meant to cover a broad range of situations, and to be as Usually the linker is invoked with at least one object file, but you can specify other the command-line will be kept, as will sections containing symbols referenced � Re: SDK Linker Script - what does it all mean Hi @jrp , You compiler issue it's just complaining about the stack not being able to fit in the assigned memory region, which means that your code seems to be gone out of space.

Force the linker to keep some specific sections

SECTIONS 
{
....
....

*(.rodata .rodata.*)

KEEP(*(SORT(.scattered_array*)));
}

[PDF] Linker Script Guide, do this, your linker script will replace the default linker script. You may also mean that the contents should be loaded into memory when the output file is run. The linker always uses a linker script. If you do not supply one yourself, the linker will use a default script that is compiled into the linker executable. You can use the ‘--verbose’ command-line option to display the default linker script. Certain command-line options, such as ‘-r’ or ‘-N’, will affect the default linker script.

Using LD, the GNU linker, The type of the expression is controlled by its position in the script file. A symbol assigned within a section definition is created relative to the base of the section; a � GNU linker. The GNU linker (or GNU ld) is the GNU Project's implementation of the Unix command ld. GNU ld runs the linker, which creates an executable file (or a library) from object files created during compilation of a software project. A linker script may be passed to GNU ld to exercise greater control over the linking process.

Understand the GNU linker script of cortex M4, text section definition in the default linker file as an example to illustrate how to define a section. .text : { KEEP(*(.vectors)) __Vectors_End = .; __� If the program references ‘etext’ but does not define it, the linker will use the definition in the linker script. Note - the PROVIDE directive considers a common symbol to be defined, even though such a symbol could be combined with the symbol that the PROVIDE would create.

Linker scripts, When you do this, your linker script will replace the default linker script. meaning that the contents should be loaded into memory when the output file is run. The linker does not shuffle sections to fit into the available regions, but does move the requested sections into the correct regions and issue errors when the regions become too full. A command file may contain at most one use of the MEMORY command; however, you can define as many blocks of memory within it as you wish.

Comments
  • KEEP is documented in this version of the manual.
  • +1 What is the dependency tree? Why is it important to mark its roots? Thanks.
  • I add a short explanation to the post.
  • THANK you! You say "...tells the linker which sections (if available) are the first ones to look at." Why wouldn't a section be available if it is marked as KEEP? Maybe I am missing some of the fundamentals here, but this topic is really obscure (yet vital) and I am having difficulty finding resources for educating myself.
  • You can imagine e.g. one script and two sets of startup code, one for profiling, one not.
  • OK, I think I understand. Thank you.