EmbeddedRelated.com
Forums
The 2024 Embedded Online Conference

Boot section is removed (gcc, ld, ar, as)

Started by Jonatan Magnusson April 21, 2010
Hi,

I'm writing a boot script for an LPC17xx based device. If I compile the
assembler boot script and the C application code and then link the
object files using `ld` 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?
Regards,
Jonatan Magnusson

An Engineer's Guide to the LPC2100 Series

Hi Jonatan,

Try adding KEEP to the linker script. Here's an example I found in a
CodeSourcery script::

SECTIONS
{
.text :
{
[skipped]
. = ALIGN(4);
KEEP(*(.init))

You might also have success with the -u ("add undefined reference") ld
switch. E.g. "-u _start".

On Wed, Apr 21, 2010 at 14:03, Jonatan Magnusson wrote:
> Hi,
>
> I'm writing a boot script for an LPC17xx based device. If I compile the
> assembler boot script and the C application code and then link the
> object files using `ld` 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?
> Regards,
> Jonatan Magnusson
>
Why not just put it in .text?

DaveS

On Wed, Apr 21, 2010 at 5:03 AM, Jonatan Magnusson wrote:
> Hi,
>
> I'm writing a boot script for an LPC17xx based device. If I compile the
> assembler boot script and the C application code and then link the
> object files using `ld` 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?
> Regards,
> Jonatan Magnusson
>
Am 21.04.2010 19:25, schrieb David Smead:
> Why not just put it in .text?

Because he wants to make sure it is linked at the beginning of the image.
--
42Bastian
+
| http://www.sciopta.com
| Fastest direct message passing kernel.
| IEC61508 certified.
+
Fair enough. Tell the linker about it. Here's part of my linker
script if that's a help.

SECTIONS
{
. = 0; /* set location counter to address zero */

.text : { /* collect all sections that should go
into FLASH after startup */
*(.startup) /* the startup code goes into FLASH */
*(.text) /* all .text sections (code) */
*(.rodata) /* all .rodata sections (constants,
strings, etc.) */
*(.rodata*) /* all .rodata* sections (constants,
strings, etc.) */
*(.glue_7) /* all .glue_7 sections (no idea what
these are) */
*(.glue_7t) /* all .glue_7t sections (no idea what
these are) */
_etext = .; /* define a global symbol _etext just
after the last code byte */
} >flash /* put all the above into FLASH */

.data : { /* collect all initialized .data sections
that go into RAM */
_data = .; /* create a global symbol marking the
start of the .data section */
*(.data) /* all .data sections */
_edata = .; /* define a global symbol marking the end
of the .data section */
} >ram AT >flash /* put all the above into RAM (but load
the LMA copy into FLASH) */

.bss : { /* collect all uninitialized .bss sections
that go into RAM */
_bss_start = .; /* define a global symbol marking the start
of the .bss section */
*(.bss) /* all .bss sections */
} >ram /* put all the above in RAM (it will be
cleared in the startup code */

. = ALIGN(4); /* advance location counter to the next
32-bit boundary */
_bss_end = . ; /* define a global symbol marking the
end of the .bss section */

_end = .; /* define a global symbol marking the end
of application RAM */

}

And here's the start of my crt.s file:

.text
.arm

.section .startup,"ax"
.global _startup /* the linker wants this symbol */
.func _startup
_startup:

On Wed, Apr 21, 2010 at 10:44 PM, 42Bastian wrote:
> Am 21.04.2010 19:25, schrieb David Smead:
>> Why not just put it in .text?
>
> Because he wants to make sure it is linked at the beginning of the image.
> --
> 42Bastian
> +
> | http://www.sciopta.com
> | Fastest direct message passing kernel.
> | IEC61508 certified.
> +
>
> -----Original Message-----
> From: l...
> [mailto:l...]On Behalf
> Of David Smead
> Sent: Thursday, April 22, 2010 1:47 AM
> To: l...
> Subject: Re: [lpc2000] Boot section is removed (gcc, ld, ar, as)
> Fair enough. Tell the linker about it. Here's part of my linker
> script if that's a help.
>
> SECTIONS
> {
> . = 0; /* set location counter to
> address zero */
>
> .text : { /* collect all sections
> that should go
> into FLASH after startup */
> *(.startup) /* the startup code goes

If he has the compiler and linker set up to remove unused sections,
he may need something like:
KEEP(*(.startup))
or in his case:
KEEP(*(.boot))

Since there are no calls into the startup code, the compiler will
eliminate these sections if the option is turned on.


Or perhaps you can't put the startup code into a library and
expect it to work, since there is no reference to it, there
is no reason to pull it in.

Mike

I think the linker looks for the symbol _startup. My crt.s is in a
library of assembly languarge functions. There's probably some option
to have the linker look for _boot, but why fight it.

DaveS

On Thu, Apr 22, 2010 at 1:02 AM, Michael Anton wrote:
>> -----Original Message-----
>> From: l...
>> [mailto:l...]On Behalf
>> Of David Smead
>> Sent: Thursday, April 22, 2010 1:47 AM
>> To: l...
>> Subject: Re: [lpc2000] Boot section is removed (gcc, ld, ar, as)
>> Fair enough. Tell the linker about it. Here's part of my linker
>> script if that's a help.
>>
>> SECTIONS
>> {
>> . = 0; /* set location counter to
>> address zero */
>>
>> .text : { /* collect all sections
>> that should go
>> into FLASH after startup */
>> *(.startup) /* the startup code goes
>
> If he has the compiler and linker set up to remove unused sections,
> he may need something like:
> KEEP(*(.startup))
> or in his case:
> KEEP(*(.boot))
>
> Since there are no calls into the startup code, the compiler will
> eliminate these sections if the option is turned on.
> Or perhaps you can't put the startup code into a library and
> expect it to work, since there is no reference to it, there
> is no reason to pull it in.
>
> Mike
>
>

The 2024 Embedded Online Conference