Search code examples
sectionsiarlinker-scripts

How to add some padding for a block in IAR linker configuration file


I am working on an IAR project. In its linker configuration file, a block is defined as the following

define block MY_BLOCK with alignment = 32 { section myblock };
place in CODE_RAM { block MY_BLOCK };

This created a MY_BLOCK memory object with 32 byte alignment, which is linked into physical memory CODE_RAM.

What I want to achieve is, leaving some extra padding area (say 64 bytes) in the end of the block. The block definition directive have the size parameter, if I want the MY_BLOCK size to grow to 1024, I can use

define block MY_BLOCK with alignment = 32, size = 1024 { section myblock };

And it works well.

However, I want the size to be relative to the original size. So I use

define block MY_BLOCK with alignment = 32, size = __section_size(section myblock) + 64 { section myblock };

This time linker report a error

Error[Lc009]: "__section_size" undefined

Looks like __section_size can be only used in C instead of linker configuration file.

In comparision, ARMGCC we can simply use

. = . + 64

in linker file to achieve this purpose. I am wondering if it is achievable in IAR.

Can someone help me out?


Solution

  • The way to achieve this is to create an empty block with the padding and add it to MY_BLOCK. To ensure that the content and the padding are placed in the order listed in the file we add the attribute fixed order to MY_BLOCK. We also need to add keep { block MY_PADDING }; to the configuration file to tell the linker to include MY_PADDING even though its content is not referenced from the application. The result looks something like this:

    define block MY_PADDING with size = 64 {};
    define block MY_BLOCK with alignment = 32, fixed order { section myblock,
                                                             block MY_PADDING };
    keep { block MY_PADDING };
    place in CODE_RAM { block MY_BLOCK };