Search code examples
cfilelinkersections

can .text, .data, .bss from a library .a file be separated into separate sections


My question is with locating library .a files in linker scripts. A normal linker script is arranged with .text, .data, .rodata, and .bss sections.

.text :
{
    *(.text .text.*)
    *(.rodata .rodata*)  /* global const uint32_t i = 10 for example */
} > rom

.data :
{
    *(.data .data.*)
} > ram AT > rom

.bss (NOLOAD) :
{
    *(.bss .bss.*)  
} > ram

What I would like to know is can .text, .data, .rodata, and .bss sections in a library such as libc.a be placed in separate areas from the rest of the sections? Placing .text in a different area is accomplished with *\libc.a:* but how are .data, .rodata, and .bss relocated?

.text1 :
{
    *\libc.a:* //.text from libc.a is placed into rom1 instead of rom 
} > rom1

I anticipated the answer for placing .data, .rodata, and .bss could be with similar syntax but the syntax below is wrong because libc.a sections are still in ram and rom instead of ram1 and rom1.

.text1 :
{
    *\libc.a:*(.text .text.*)     // libc.a.text still resides in rom
    *\libc.a:*(.rodata .rodata.*) // libc.a.rodata still resides in rom
} > rom1

.data1 :
{
    *\libc.a:*(.data .data.*)    // libc.a.data still resides in ram
} > ram1 AT > rom1

.bss1 (NOLOAD) :
{
    *\libc.a:*(.bss .bss.*)      // libc.a.bss still resides in ram
} > ram1

Are there any hints to separate .data, .rodata, and .bss from an .a file into their own sections?


Solution

  • That depends on how you build your library or if you relink it. You can name the sections .text.libc.* either by directly building libc that way or by relinking the object files first with a intermediate linker script.

    The *\libc.a: ... syntax I believe only works if you use the actual *.o names of all the files in the libc.a.

    Note: Gcc also has some options to put code into subsections automatically, like one section per function. Take care not to make things incompatible with that.