Skip to content

Picolibc #871

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 2 commits into from
Mar 22, 2020
Merged

Picolibc #871

merged 2 commits into from
Mar 22, 2020

Conversation

aykevl
Copy link
Member

@aykevl aykevl commented Jan 25, 2020

This is necessary for better CGo support on bare metal. Existing libraries expect to be able to include parts of libc and expect to be able to link to those symbols.

Because with this all targets have a working libc, it is now possible to add tests to check that a libc in fact works basically.

Not all parts of picolibc are included, such as the math or stdio parts. These should be added later, when needed.

This PR also avoids the need for the custom memcpy/memset/memcmp symbols that are sometimes emitted by LLVM. The C library will take care of that.

(The first commit is basically #849).

@aykevl
Copy link
Member Author

aykevl commented Jan 25, 2020

I've looked at several libraries and I think this is the best one for our purposes. Other libraries I've looked at:

  • Newlib: big and originally not intended for bare metal.
  • EmbeddedArtistry libc: little used and possibly incomplete. Also contains lots of submodules, which can be inconvenient.

Further information:
https://keithp.com/blogs/picolibc/
https://www.youtube.com/watch?v=SC6aBezNFFQ

@aykevl
Copy link
Member Author

aykevl commented Jan 25, 2020

Looks like it'll need an improvement for macOS:

        --- FAIL: TestCompiler/Host/cgo (1.64s)
            main_test.go:136: failed to build: /usr/include/secure/_string.h:82:20: unexpected token ILLEGAL

@aykevl aykevl added the cgo CGo and libc related issues label Jan 26, 2020
@aykevl aykevl force-pushed the picolibc branch 3 times, most recently from 144b9e1 to e1d6814 Compare February 12, 2020 11:49
@aykevl
Copy link
Member Author

aykevl commented Feb 12, 2020

The problem was that functions like strcpy were replaced by __builtin___strcpy_chk for _FORTIFY_SOURCE. I don't think it's sensible to use source code fortification in the CGo layer so I simply disabled it. It is still kept at the default value for C files bundled in packages (such as testdata/cgo/main.c).

This PR is now ready for review.

@bgould
Copy link
Member

bgould commented Mar 22, 2020

Would it make sense to set a build tag for the libc implementation that is used? For example I am trying to implement malloc() and free() in the runtime and it might be useful to be able to protect that with a build tag. I think for now I can do it with the build tags for cortex-m/riscv/gba, but that might be a little fragile in the long run.

@aykevl
Copy link
Member Author

aykevl commented Mar 22, 2020

I'm not sure what you mean. Why would you need a build tag? It's not the C library that defines these functions but the C specification (just like with memset etc).

You can use the baremetal build tag to exclude these functions from targets like WebAssembly and Linux. However, I think renaming these functions to malloc etc would be the better solution. Not sure yet.

@aykevl aykevl mentioned this pull request Mar 22, 2020
@deadprogram
Copy link
Member

@aykevl I suggest rebasing against dev again, since there have been quite a few changes since the last time it was done.

aykevl added 2 commits March 22, 2020 14:37
This refactor makes adding a new library (such as a libc) much easier in
the future as it avoids a lot of duplicate code. Additionally, CI should
become a little bit faster (~15s) as build-builtins now uses the build
cache.
This is necessary for better CGo support on bare metal. Existing
libraries expect to be able to include parts of libc and expect to be
able to link to those symbols.

Because with this all targets have a working libc, it is now possible to
add tests to check that a libc in fact works basically.

Not all parts of picolibc are included, such as the math or stdio parts.
These should be added later, when needed.

This commit also avoids the need for the custom memcpy/memset/memcmp
symbols that are sometimes emitted by LLVM. The C library will take care
of that.
@aykevl
Copy link
Member Author

aykevl commented Mar 22, 2020

Okay, rebased.

@deadprogram
Copy link
Member

Now merging, as this does seem to be the best option.

@deadprogram deadprogram merged commit f316ebc into dev Mar 22, 2020
@deadprogram deadprogram deleted the picolibc branch March 22, 2020 16:15
@aykevl
Copy link
Member Author

aykevl commented Mar 22, 2020

Great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cgo CGo and libc related issues
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants