Chris H
Chris H

Reputation: 6581

linker script load vs. virtual address

I've got the following linker script that is supposed to link code to run on a flash based micrcontroller. The uC has flash at address 0x0, and RAM at 0x40000000. I want to put the data section into flash, but link the program so that access to the data section is done in RAM. The point being, I'll manually copy it out of flash into the proper RAM location when the controller starts.

MEMORY 
{
    flash   : ORIGIN = 0x00000000, LENGTH = 512K
    ram : ORIGIN = 0x40000000, LENGTH = 32K
    usbram   : ORIGIN = 0x7FD00000, LENGTH = 8K
    ethram   : ORIGIN = 0x7FE00000, LENGTH = 16K
}

SECTIONS
{
    .text : { *(.text) } >flash
    __end_of_text__ = .;
    .data : 
    {
        __data_beg__ = .;
        __data_beg_src__ = __end_of_text__;
        *(.data)
        __data_end__ = .;
    } >ram AT>flash
    .bss : 
    {
        __bss_beg__ = .;
        *(.bss)
    } >ram
}

The code as shown above generates the following output:

40000000 <__data_beg__>:
40000000:   00000001    andeq   r0, r0, r1
40000004:   00000002    andeq   r0, r0, r2
40000008:   00000003    andeq   r0, r0, r3
4000000c:   00000004    andeq   r0, r0, r4
40000010:   00000005    andeq   r0, r0, r5
40000014:   00000006    andeq   r0, r0, r6

which represents an array of the form

int foo[] = {1,2,3,4,5,6};

Problem is that it's linked to 0x40000000, and not the flash region as I wanted. I expected the AT>flash part of the linker script to specify linking into flash, as explained in the LD manual.

http://sourceware.org/binutils/docs/ld/Output-Section-Attributes.html#Output-Section-Attributes

and here is my ld invocation:

arm-elf-ld  -T ./lpc2368.ld entry.o main.o -o binary.elf

Thanks.

Upvotes: 3

Views: 5651

Answers (2)

Jonathan
Jonathan

Reputation: 883

Your .data virtual address = 0x40000000

Your .data logical address = 0x00000000

This can be seen with command objdump -h file.elf

Sections:
Idx Name          Size      VMA       LMA       File off  Algn
  8 .data         00000014  40000000  00000000  00001000  2**2
                  CONTENTS, ALLOC, LOAD, DATA

Upvotes: 0

Raj
Raj

Reputation: 887

Your linker script LINKS .data to RAM and LOADS .data into FLASH. This is due to AT command.

Why do you want to link volatile data to flash? Data and Bss must ALWAYS be linked to RAM. Your linker script is quite correct. You would only link text and constand data into flash.

Please look at your map file. It would necessarily have assigned a RAM address to data variables.

The program loader code then copies copies (data_end - data_beg) bytes from data_beg_src to data_beg.

So the first data which is the array is copied into the begining of SRAM.

If you need to link data to flash:

 Data :
  {
   *(.data);
  } > flash

Linker will now LINK and LOAD .data into flash. But if I were you, I wouldnt do that.

Upvotes: 1

Related Questions