Search code examples
linkerembeddedld

using the same linker ld script file target for two files


lets assume I have a memory allocation that looks like this:

MEMORY
{
    firstfile  : ORIGIN = 0x00000000, LENGTH = 0x2000 
    secondfile : ORIGIN = 0x00002000, LENGTH = 0x6000
}

now I want to use the same ld script for two different files. 'firstfile.c' and 'secondfile.c' how to I make firstfile entire allocation go under 'firstfile' section, and the second file under 'secondfile' section?

currently .text all goes under secondfile section. using special attribute section on each of the functions in firstfile.c doesnt help


Solution

  • In your linker script fragment firstfile and secondfile are MEMORY regions not SECTIONS, so the section attributes will (I guess) be ignored because the sections do not exist.

    You must create the MEMORY regions, in which you place SECTIONS, then you assign sections defined in the object code to sections declared in the linker script. Note that it is the object code that is located, not the source file - the linker knows nothing about source files:

    Something like:

    MEMORY
    {
        FIRST_MEMORY  : ORIGIN = 0x00000000, LENGTH = 0x2000 
        SECOND_MEMORY : ORIGIN = 0x00002000, LENGTH = 0x6000
    }
    
    SECTIONS
    {
      .firstsection :
      {
        . = ALIGN(4);
    
        *firstfile.o (.text .text*)   /* Locate firstfile text sections here */
    
      } > FIRST_MEMORY
    
      .secondsection :
      {
        . = ALIGN(4);
    
        *secondfile.o (.text .text*)  /* Locate secondfile text sections here */
    
      } > SECOND_MEMORY
    }
    

    You can then locate any number of modules explicitly to each section.

    You might want a default location to place modules not explicitly located. In which case you should add:

    *(.text)           /* .text sections (code) */
    *(.text*)          /* .text* sections (code) */
    

    to one of the sections (or create a separate default .text section).

    Also if you add:

    *(.firstsection*)  /* Locate anything with firstsection attribute here */
    

    or

    *(.secondsection*) /* Locate anything with secondsection attribute here */
    

    to the respective sections you can use __section__ attributes in the code to locate specific functions (or data) to to these sections as you attempted previously. But locating an entire module is preferable as it does not require code modification and maintenance.