Skip to content

cmake: Add CONFIG_LINKER_ERROR_RWX_SEGMENTS #89574

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

Conversation

keith-packard
Copy link
Contributor

@keith-packard keith-packard commented May 6, 2025

When this Kconfig parameter is selected, configure the linker so that errors will be produced when the generated ELF file contains segments which are both executable and writable.

This is set to 'n' by default as nearly all Zephyr targets do not mark all data destined to land in ROM as constant.

@keith-packard keith-packard force-pushed the allow-rwx-segments branch 2 times, most recently from 1b07301 to 6d01be7 Compare May 7, 2025 02:29
@ruuddw
Copy link
Member

ruuddw commented May 7, 2025

I agree with @nashif that this is not ARCv3 specific. Furthermore, this raises some questions on if the warning makes sense for Zephyr at all. The (rwx) memory flags in the linker script seem to defined for the linker as a hint on where to put segments that don't have explicit placing rules. The linker warning, however, assumes these to be attributes for the loader or so to set up run-time protections to avoid e.g. executable stacks. I don't think there is a firm or enforced relation in Zephyr between the linker script attributes and the actual runtime attributes (e.g. programming MPU regions based on these).
Do we need a kconfig flag for this, or simply always disable the warning to not confuse users about linker checks and real runtime behavior?

@nashif nashif changed the title qemu/arc: Suppress linker warnings about RWX segments Suppress linker warnings about RWX segments May 7, 2025
@keith-packard
Copy link
Contributor Author

I agree with @nashif that this is not ARCv3 specific. Furthermore, this raises some questions on if the warning makes sense for Zephyr at all.

I'd argue that it's more important on Zephyr than most environments. Placing data in ROM without having it marked const means that the compiler can't warn the developer about mistakes where the code is storing to those variables. Many embedded systems don't trap writes to ROM, they just ignore them. And emulators often fail to prevent these writes, leading to applications which work differently under emulation than on real hardware.

For instance, if you enable CONFIG_DYNAMIC_INTERRUPTS, then you need to place _sw_isr_table in read-write memory or your application will fail in mysterious ways. With this linker flag, you'll get a warning at link time if the linker script placed it in read-only memory.

Once #89577 is working, we will be able to enable this warning on many platforms.

The (rwx) memory flags in the linker script seem to defined for the linker as a hint on where to put segments that don't have explicit placing rules.

Those are ignored when the script has explicit instructions about section placement, leading to read-write variables being placed in read-only memory, even when that memory is correctly labeled.

Do we need a kconfig flag for this, or simply always disable the warning to not confuse users about linker checks and real runtime behavior?

I think Zephyr should use the compiler to help developers find places where they may attempt to store to data stored in ROM and prevent that at build time. You'd think the linker would complain when you place writable sections in memory regions that are marked read-only, but it doesn't. So, this flag is all we've got to help with this problem.

@keith-packard
Copy link
Contributor Author

I've changed the name of the Kconfig option to LINKER_WARN_RWX_SEGMENTS.

@keith-packard
Copy link
Contributor Author

And now changed to actually make RWX segments an error instead of just a warning -- if the goal is to use this to check build correctness, then the build needs to fail if it doesn't work.

Note that this option does not work with the 12.2 toolchain as the linker doesn't support any of this stuff.

@keith-packard keith-packard changed the title Suppress linker warnings about RWX segments cmake: Add CONFIG_LINKER_ERROR_RWX_SEGMENTS May 7, 2025
tejlmand
tejlmand previously approved these changes May 19, 2025
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.

LGTM, only a minor non-blocking nit.

When this Kconfig parameter is selected, configure the linker so that
errors will be produced when the generated ELF file contains segments
which are both executable and writable.

This is set to 'n' by default as nearly all Zephyr targets do not yet mark
all data destined to land in ROM as constant.

Signed-off-by: Keith Packard <[email protected]>
Copy link

Copy link

This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time.

@github-actions github-actions bot added the Stale label Jul 19, 2025
@github-actions github-actions bot closed this Aug 3, 2025
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.

4 participants