GNU ld removes section

Jonatan picture Jonatan · Apr 21, 2010 · Viewed 9.6k times · Source

I'm writing a boot script for an ARM-Cortex M3 based device. If I compile the assembler boot script and the C application code and then combine the object files and transfer them to my device everything works.

However, if I use ar to create an archive (libboot.a) and combine that archive with the C application there is a problem:

I've put the boot code in a section:

    .section    .boot, "ax"
    .global     _start

_start:
    .word       0x10000800  /* Initial stack pointer (FIXME!) */
    .word       start
    .word       nmi_handler
    .word       hard_fault_handler
    ... etc ...

I've found that ld strips this from the final binary (the section "boot" is not available). This is quite natural as there is no dependency on it that ld knows about, but it causes the device to not boot correctly.

So my question is: what is the best way to force this code to be included?

Answer

Michael Burr picture Michael Burr · Apr 23, 2010

Try adding something like:

KEEP(*(.boot))

in the ld linker script to tell the linker to keep the .boot section.

However, I'm not sure if this is enough to cause ld to pull in any objects from the archive that are in the .boot section - it might not consider an object at all unless some symbol in that object is causing it to be pulled in. If this is a problem, specifying _start as the entry point (using -e _start on the ld command line or using ENTRY(_start) in the linker script) may be the solution.