Search code examples
gccmemoryarmld

How can I put some memory section in particular memory


I am trying to understand the GCC link script and create a small demo to practice , however I got the "syntax error" from ld. I appreciate any comments or suggestions. Thank you so much!

hello.c

__attribute__((section(".testsection"))) volatile int testVariable;

hello.ld

MEMORY {
  TEST_SECTION: ORIGIN = 0x43840000 , LENGTH = 0x50    
}
SECTIONS{
.testsection: > TEST_SECTION /* Syntax error here*/
}

compile command

gcc -T hello.ld -o hello hello.o
c:/gnu/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld.exe:../hello.ld:20: syntax error
collect2.exe: error: ld returned 1 exit status

Solution

  • Original post

    MEMORY {
      TEST_SECTION: ORIGIN = 0x43840000 LENGTH = 0x50    
    }
    SECTIONS{
    .testsection: > TEST_SECTION
    }
    

    Solution

    MEMORY {
      TEST_SECTION: ORIGIN = 0x43840000, LENGTH = 0x50    
    }
    SECTIONS{
    .testsection: { *(.testsection*) } > TEST_SECTION
    }
    

    I think it was missing two things.

    First the comma between the origin value and LENGTH.

    Second a section command was missing. I don't think the GNU linker script syntax allows for that, you probably need to put something in there anyway to have this be useful. You could try

    .testsection: { } > TEST_SECTION
    

    but my guess without trying it is that you wouldn't end up with anything in that memory.

    Edit

    After doing an experiment:

    so.s

    .text
    add r0,r0,r0
    add r1,r2,r3
    add r1,r1,r1
    add r2,r2,r2
    

    so.ld

    MEMORY
    {
        bob : ORIGIN = 0x30000000, LENGTH = 0x1000
        ted : ORIGIN = 0x20000000, LENGTH = 0x1000
    }
    
    SECTIONS
    {
        .hello : { *(.text*) } > bob
        .world : { } > ted
        .text : { } > ted
    }
    
    Disassembly of section .hello:
    
    30000000 <.hello>:
    30000000:   e0800000    add r0, r0, r0
    30000004:   e0821003    add r1, r2, r3
    30000008:   e0811001    add r1, r1, r1
    3000000c:   e0822002    add r2, r2, r2
    

    As I suspected, first I already new the names in MEMORY are whatever you want, as you saw as well, RAM, ROM, etc are not special names just can't use reserved names if any. I suspected that SECTIONS worked the same way the thing on the left is a new name you are creating for the output. This built and linked just fine (not a real program obviously just enough to get the tools to do a demonstration).

    The linker did not complain, but at the same time since there was nothing actually placed in ted then nothing was placed in that memory space in the output binary. .hello and .text don't have to match, one is an output name one is an input name, very often for simple stuff folks make them match, why create a new name. Only did it here for demonstration purposes to show that by using the same name on the left does not automatically include the input section to the output.

    I highly recommend doing experiments like this and using tools like readelf and objdump and others to examine what is going on (as well as reading the documentation for the scripting language).