Skip to content

Conversation

@iabdalkader
Copy link
Contributor

@iabdalkader iabdalkader commented Nov 28, 2025

ld seems to treat quotes in section names as literal characters, which makes it fail to match section names outputted by this script. This fix replaces quoted section names with a wildcard, which matches the section correctly.

Fixes #49155

Context: I'm trying to relocate the kernel heap to another SRAM, using CONFIG_CODE_DATA_RELOCATION. In CMakeLists.txt I have the following:

zephyr_code_relocate(FILES ${ZEPHYR_BASE}/kernel/mempool.c LOCATION SRAM2_BSS_NOINIT)

The generated linker scripts seems to have the right section in SRAM2. Note that the section name is quoted:

 .sram2_noinit_reloc (NOLOAD) :
        {
                . = ALIGN(4);
                KEEP(*mempool.c.obj(.noinit."WEST_TOPDIR/zephyr/kernel/mempool.c".kheap_buf__system_heap))
                . = ALIGN(4);
 } > SRAM2 AT > SRAM2

In the final elf, the section is empty because ld fails to match it:

  .sram2_noinit_reloc
                  0x0000000030020000        0x0
                  0x0000000030020000                . = ALIGN (0x4)
   *mempool.c.obj(SORT_BY_ALIGNMENT(.noinit.) SORT_BY_ALIGNMENT(WEST_TOPDIR/zephyr/kernel/mempool.c)

With this patch:

.sram2_noinit_reloc
                0x0000000030020000    0x20000
                0x0000000030020000                . = ALIGN (0x4)
 *mempool.c.obj(SORT_BY_ALIGNMENT(.noinit.*.kheap_buf__system_heap))
 .noinit."WEST_TOPDIR/zephyr/kernel/mempool.c".kheap_buf__system_heap

ld seems to treat quotes in section names as literal characters, which
makes it fail to match section names outputted by this script. For example,
the following section name, generated with CONFIG_CODE_DATA_RELOCATION to
relocate noinit section, fails to match the section, causing it to Not be
relocated:

KEEP(*file.c.obj(.noinit."WEST_TOPDIR/path/file.c".symbol))

This fix replaces quoted section names with a wildcard, which matches
the section correctly.

Signed-off-by: Ibrahim Abdalkader <i.abdalkader@gmail.com>
@iabdalkader iabdalkader force-pushed the fix_reloc_section_name branch from e0c5902 to 0c914f6 Compare November 28, 2025 15:39
@sonarqubecloud
Copy link

Copy link
Contributor

@tejlmand tejlmand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the fix, but I would like to get a better understanding of when this started to fail, cause it looks like a regression.

Comment on lines +335 to +337
# Replace quoted section name with wildcard.
section_name = re.sub(r'\.\"[^\"]+\"\.', '.*.', section.section_name)
out += template.format(obj_file_name=section.obj_file_name, section_name=section_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to have this fixed before entering the script.

I wonder how / when this issue first started to appear.

@iabdalkader
Copy link
Contributor Author

thanks for the fix, but I would like to get a better understanding of when this started to fail, cause it looks like a regression.

This is the first time I try this, so I'm not sure if it ever worked before. Maybe it was never tested on files that have sections with quotes?

@pdgendt
Copy link
Contributor

pdgendt commented Dec 1, 2025

thanks for the fix, but I would like to get a better understanding of when this started to fail, cause it looks like a regression.

@tejlmand it has been an issue for a long time, see #49155

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Ability to relocate noinit section variables

4 participants