gccarmembeddedbare-metalgnu-toolchain

Is it possible to change default sections for a whole file with GNU Arm embedded?


With GNU Arm embedded toolchain, is it possible to change the default sections for symbols for a whole file?

I've previously worked with Rowley Crossworks, which has a parameter you can set for any source file or folder to change various default sections, for instance the default section for zeroed variables: enter image description here (from the Crossworks manual)

This is very useful to make sure a big application fit in flash on and RAM resource constrained microcontrollers. However, I'm unable to find any way to do this with the regular GNU Arm toolchain.

I'm aware that I can use __attribute__((section(".sectionname"))), but this requires code modifications, which is problematic when compiling the same code for different targets, some of which may not have more than one section.

The ideal solution would be a GCC command-line parameter to put for instance zeroed data in a custom section for a specific compilation unit. I could then apply this to specific files, folders or projects from CMake, without making any changes to the actual source code. Does something like this exist?


Solution

  • I was not able to find a command line parameter or similar for this functionality, but the comment from Lundin made me look into linker scripts in some more detail, and ended up with this:

    .bss :
    {
        . = ALIGN(4);
        __bss_start__ = .;
        *main.cpp.obj*(.bss*)
        *(COMMON)
        . = ALIGN(4);
        __bss_end__ = .;
    } >RAM
    
    .ethram (NOLOAD):
    {
        __ethram_start__ = .;
        *(.ethram)
        *(.bss*)
        __ethram_end__ = .;
    } >ETHRAM
    

    In the above I explicitly state that in the output section .bss only main.cpp's .bss section should be included, and that it should be placed in regular RAM. By then having an unconstrained .bss in ETHRAM, the linker places other files' .bss sections there, and this was sufficient for my use.

    It is also possible to explicitly exclude files from an output section, like this, but my application didn't need it:

    .bss :
    {
        . = ALIGN(4);
        __bss_start__ = .;
        EXCLUDE_FILE(*main.cpp.obj*)*(.bss*)
        *(COMMON)
        . = ALIGN(4);
        __bss_end__ = .;
    } >RAM