-
Notifications
You must be signed in to change notification settings - Fork 7.6k
arch: arm: cortex_m: Move bss/noinit sections to the end of linker script #74042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
arch: arm: cortex_m: Move bss/noinit sections to the end of linker script #74042
Conversation
…ript This results in a smaller binary with CONFIG_XIP=n as these NOLOAD sections can be excluded. bss/noinit have been included in CONFIG_XIP=n binaries by default since c107827. This also occurs if using iterable sections or adding code/data to ITCM/DTCM sections. Moving bss/noinit to the end of the linker file should ensure they are always excluded Signed-off-by: Grant Ramsay <gramsay@enphaseenergy.com>
de485f9
to
119b7fe
Compare
@ithinuel please take a look |
@MaureenHelm TBH there’s so much things going on in those files I can’t tell if something is right in all circumnstances or not. |
Added @tejlmand as the expert |
A similar issue was also recently fixed for x86: #73044 It could do with a test; a big static storage variable (e.g. |
actually it was not c107827 which introduced the The
they should normally be excluded from loading by the linker if the linker script is properly written. One guess could be the internal macro definition of Taking a look at the issue reported to reproduce this locally before approving a linker script adjustment, even though placing bss later should not impact rom usage on xip targets. I have a hunch that something else has been overlooked. |
c107827 removed -SECTION_PROLOGUE(.last_section,(NOLOAD),)
+SECTION_PROLOGUE(.last_section,,) (P.S. sorry about the email spam, I accidentally clicked an emoji button on my mobile email client...) |
I went through the ARM Cortex-M linker.ld file, extracted some section of interest (ignoring userspace) and tried to make an image of what an XIP=y and XIP=n image might look like in the physical memory regions: With config XIP=y, everything outside of FLASH is either NOLOAD (green) or VMA (virtual memory address) (purple). With XIP=n:
Flattened memory map of the above for an STM32H7B0: If you create a ".bin" file it will be from the first to last blue box. I've gone a bit off topic, but hopefully visualising it helps make it clearer what is going on |
Correct, but as you have also noticed, the But I would still argue that the original cause is that
yep, or perhaps more a feature which has not been taken all the way , that is XIP=n was not considered when doing the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for both finding, investigating and providing a fix for this issue 👍 .
I have found no side-effects so changes lgtm, but I have a couple additional wishes before a +1:
- can you please take userspace support into consideration in this fix.
CONFIG_XIP=n CONFIG_USERSPACE=y
still suffers this problem after this PR, and it's not nice just waiting for someone else hitting this issue again later.
zephyr/include/zephyr/arch/arm/cortex_m/scripts/linker.ld
Lines 283 to 292 in 7773fe8
#if defined(CONFIG_USERSPACE) #define APP_SHARED_ALIGN . = ALIGN(_region_min_align); #define SMEM_PARTITION_ALIGN MPU_ALIGN #include <app_smem.ld> _app_smem_size = _app_smem_end - _app_smem_start; _app_smem_rom_start = LOADADDR(_APP_SMEM_SECTION_NAME); SECTION_DATA_PROLOGUE(_BSS_SECTION_NAME,(NOLOAD),) - Can you please take a look at similar linker.ld files.
Seems like thearm64
,cortex_a_r
suffers the same issue and for the above reason but also because it's nice to have the linker.ld files somewhat aligned in how they look, then it would be nice to have those aligned as well.
Regarding the SoC specific linker.ld files, then but feel free to ping the SoC maintainers and let them evaluate if the same changes should be done there.
@tejlmand thanks! I will check userspace and other linker files soon when I find time |
Small update for Cortex-A/R. There was an attempt to move bss/noinit toward the end of the linker file #60368. This ended up getting reverted due to CI failures. The CI failures were later discovered to be unrelated to this change (there was some undefined behavior in one of the tests that just happened to start failing). This PR was never re-applied Interestingly, Cortex-A/R does not put bss/noinit in different places depending on |
@gramsay0 I consider updating / aligning with cortex_a_r ld template scripts addressed by @Dat-NguyenDuy in #79502 |
There was a discussion regarding this topic for cortex-M, but the issue was closed without any actions. I have just re-opened it #24619 |
Hi @gramsay0, have you had time to look into the USERSPACE issue on cortex-M? I have tried with your commits and always put bss/noinit sections to the end of the linker script. Run tests\kernel, tests\lib on an cortex-M7 based board, i see no issue. |
@Dat-NguyenDuy yes I had done some testing and also found no issues. I think this patch fixed the need for bss to be before the kobject_data section with userspace enabled: #33687 See #21747 (comment) (which I believe is what the above patch does):
@tejlmand do you agree, and if so should I make that change here in in a follow-up PR? |
yes, that does seem to be the case.
it does seem that #33687 provides the needed steps to allow us to move bss to the end. |
@ithinuel could you please review and potentially be the second +1 here? |
This results in a smaller binary with
CONFIG_XIP=n
as these NOLOAD sections can be excludedbss/noinit have been included in
CONFIG_XIP=n
binaries by default since c107827. This also occurs if using iterable sections or adding code/data to ITCM/DTCM sections. Moving bss/noinit to the end of the linker file should ensure they are always excludedPartial fix for #72307