Skip to content

cmake: libc: minimal: Avoid linking against other libc implementations #79629

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

Merged
merged 1 commit into from
Oct 30, 2024

Conversation

jonathonpenix
Copy link
Contributor

The minimal libc is currently built as a zephyr_library and will be included in the final link line as such. However, the c_library property will still be set as "-lc" (for most linkers) and will be added to the link line. This effectively requires that a separate libc implementation be available in the toolchain and makes it possible to accidentally pull from the non-minimal libc.

This doesn't seem desirable, so try to prevent this by clearing the c_library property if we are using the minimal libc.

The minimal libc is currently built as a zephyr_library and will be
included in the final link line as such. However, the c_library property
will still be set as "-lc" (for most linkers) and will be added to the
link line. This effectively requires that a separate libc implementation
be available in the toolchain and makes it possible to accidentally pull
from the non-minimal libc.

This doesn't seem desirable, so try to prevent this by clearing the
c_library property if we are using the minimal libc.

Signed-off-by: Jonathon Penix <jpenix@quicinc.com>
@@ -1,5 +1,9 @@
# SPDX-License-Identifier: Apache-2.0

# As minimal libc will be built as a zephyr_library, clear c_library to
# prevent accidentally requiring or linking against another libc implementation.
set_property(TARGET linker PROPERTY c_library "")
Copy link
Contributor Author

Choose a reason for hiding this comment

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

One other idea was to modify link_order_library in the various linker_libraries.cmake files to omit c if we're building with CONFIG_MINIMAL_LIBC. That did seem to work (and seems analogous to what was done for CONFIG_MINIMAL_LIBCPP), but this was a smaller change (and is somewhat similar to what is done for picolibc-as-a-module) so I ended up doing this instead--not sure if one is better than the other otherwise or if there is something entirely different that would be preferred!

Copy link
Collaborator

Choose a reason for hiding this comment

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

You probably need to also change math_library and c++_library to make sure we don't mix other incompatible bits. This way we'll get a build error rather than a runtime error.

Copy link
Collaborator

Choose a reason for hiding this comment

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

You probably need to also change math_library and c++_library to make sure we don't mix other incompatible bits.

actually, one intention in the new design is that you don't have to change all the definitions of the libraries, if all you want is to change the libs used and/or the order in which they are linked.
In such cases, the link_order_library property should be adjusted instead.

As the minimal libc is inside the whole-archive flags and it appears we don't want other bits, then I would suggest to do:

set_property(TARGET linker PROPERTY link_order_library "")

A future option is to change the minimal libc into a named library, and then link it specifically at the end, similar to what is done for picolibc, but that is a little more work, and thus I don't request it for this PR.

@jonathonpenix jonathonpenix marked this pull request as ready for review October 9, 2024 23:06
@zephyrbot zephyrbot added the area: C Library C Standard Library label Oct 9, 2024
@jonathonpenix
Copy link
Contributor Author

I'm not able to add reviewers, but please take a look @tejlmand too if possible!

@aescolar aescolar requested a review from tejlmand October 10, 2024 07:52
@jonathonpenix
Copy link
Contributor Author

Gentle ping

@mmahadevan108
Copy link
Collaborator

mmahadevan108 commented Oct 29, 2024

@jonathonpenix , this might be addressed as part of #80473 and #80472
@stephanosio, can you confirm

@jonathonpenix
Copy link
Contributor Author

jonathonpenix commented Oct 29, 2024

Thanks for pointing that out! Yep, I think the PRs you linked resolve this--I'll just close this one since I haven't gotten to the comments above and I think this would end up basically the same.

@tejlmand
Copy link
Collaborator

tejlmand commented Oct 30, 2024

@jonathonpenix reopened this one.
Follows the intention of the new design closer than #80473 and #80472.

@tejlmand tejlmand reopened this Oct 30, 2024
@dkalowsk dkalowsk added this to the v4.0.0 milestone Oct 30, 2024
@mmahadevan108 mmahadevan108 merged commit e020f31 into zephyrproject-rtos:main Oct 30, 2024
54 checks passed
@jonathonpenix
Copy link
Contributor Author

Apologies for pre-emptively closing this and for the slow responses on my end 😅

This was approved/merged, but just to double check: @keith-packard and @tejlmand do you think it is still worth addressing math_library/c++_library per the comments above?

I don't think there's a strict correctness issue here currently (math_library isn't added to link_order_library when minimal libc is selected and the various C++ lib options mostly either have a non-minimal-libc requirement in Kconfig or don't have c++_library set to anything--the one exception I think is when EXTERNAL_LIBCPP is selected, but I'm not really sure what the expectations are in that case), but it also makes sense to me that we might also want to clear math_library just as another safeguard, for example.

If it would still be worthwhile, I can try to add that as a follow-up PR.

@jonathonpenix jonathonpenix deleted the c_lib branch October 30, 2024 20:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: C Library C Standard Library bug The issue is a bug, or the PR is fixing a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants