GNU ld deletes a partition

I am writing a boot script for an ARM-Cortex M3 based device. If I compile the assembler loading the script and the application code C, and then combine the object files and transfer them to my device, everything will work.

However, if I use ar to create the archive (libboot.a) and combine this archive with the C application, the problem arises:

I installed the download code in the section:

  .section .boot, "ax" .global _start _start: .word 0x10000800 /* Initial stack pointer (FIXME!) */ .word start .word nmi_handler .word hard_fault_handler ... etc ... 

I found that ld removes this from the final binary (the download section is not available). This is quite natural, since there is no dependence on it that ld knows about, but this leads to improper device boot.

So my question is: what is the best way to include this code?

+7
arm gas linker embedded ld
source share
5 answers

Try adding something like:

 KEEP(*(.boot)) 

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

However, I'm not sure that this is enough to force ld to pull any objects from the archive that are in the .boot section - it may not consider the object at all if any character in this object causes it to retract. If this is a problem, it may be a solution to specify _start as the entry point (using -e _start on the ld command line or using ENTRY(_start) in the script builder).

+10
source share

I think you want to pass the - no-gc-sections option to the linker. From the GNU ld documentation :

 --gc-sections --no-gc-sections Enable garbage collection of unused input sections. `--gc-sections' decides which input sections are used by examining symbols and relocations. The section containing the entry symbol and all sections containing symbols undefined on the command-line will be kept, as will sections containing symbols referenced by dynamic objects. Note that when building shared libraries, the linker must assume that any visible symbol is referenced. Once this initial set of sections has been determined, the linker recursively marks as used any section referenced by their relocations. See `--entry' and `--undefined'. 
+3
source share

The component will retrieve from the archive only those objects that are necessary to resolve explicitly designated characters. Your startup code is not explicitly specified, because it is called through the reset vector.

If your boot code contains several modules, you must create a partially linked object file using ld and - r / - relocatable , this will combine the objects into one object without having to allow all characters (e.g. main ()). This can then be used in the full link with your application code. If this is only one object file, then there is no real advantage when creating an archive in any case (and, as you found it, it will not work).

Note that the GNU C runtime is traditionally provided in the crt0.o file (and not in the archive) for the same reason.

+2
source share

You can use --whole-archive when connecting, but it is a large elephant gun. On the manual page:

For each archive specified on the command line after the --whole-archive option, include each object file in the archive in the link, rather than search the archive for the required object files.

+2
source share

You can use the ld -whole-archive option to output non-referenced characters. This ld options page has this for an entire archive - the whole archive

For each archive specified on the command line after the -all-option archive, include each object file in the archive in the link, and not search for the archive for the required object files. This is usually used to turn an archive into a shared library, forcing each object to be included in the resulting shared library. This option can be used more than once. Two entries when using this option from gcc: firstly, gcc does not know about this option, so you need to use -Wl, -whole-archive. Secondly, be sure to use -Wl, -no-whole-archive after your list of archives, because gcc will add its own list of archives and you may not want this to affect them.

Also see this stack overflow question, using the full archive option

+1
source share

All Articles