Skip to content
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

Support Picolibc in addition to newlib #5762

Closed
wants to merge 8 commits into from

Conversation

keith-packard
Copy link

Add support for picolibc

Picolibc is a C library that is designed to work on 32 and 64 bit embedded systems. It is available on Debian derivatives, can be built using crosstool-ng or can be added to the GNU Arm Embedded Toolkit.

Picolibc is comprehensively tested on Arm, Aarch64, x86 and RISC-V embedded systems to ensure standards conformance. It offers significant memory savings in typical usage as well as avoiding dynamic memory allocation for even complex code paths like floating point conversions.

以下的内容不应该在提交PR时的message修改,修改下述message,PR会被直接关闭。请在提交PR后,浏览器查看PR并对以下检查项逐项check,没问题后逐条在页面上打钩。
The following content must not be changed in the submitted PR message. Otherwise, the PR will be closed immediately. After submitted PR, please use a web browser to visit PR, and check items one by one, and ticked them if no problem.

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 本拉取/合并请求代码是高质量的 Code in this PR is of high quality
  • 本拉取/合并使用formatting等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification

Picolibc defines these network macro functions, which conflict with
the definitions in netdev_ipaddr.h.

Signed-off-by: Keith Packard <keithp@keithp.com>
We may have included stdio.h elsewhere in the system.

Signed-off-by: Keith Packard <keithp@keithp.com>
@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@keith-packard
Copy link
Author

CLA assistant check Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.You have signed the CLA already but the status is still pending? Let us recheck it.

I cannot get permission from my employer for any CLA.

@mysterywolf
Copy link
Member

Thank you for your contribution!

Copy link
Member

@BernardXiong BernardXiong left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution. But, where is picolibc implementation? Is it made as a software package?

@@ -606,6 +606,14 @@ struct rt_cpu

#endif

#if defined(RT_USING_PICOLIBC) && defined(_HAVE_PICOLIBC_TLS_API)
#define RT_USING_TLS
Copy link
Member

Choose a reason for hiding this comment

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

So good for this feature. It's a feature planning in v5.0.

src/thread.c Outdated
@@ -177,12 +180,22 @@ static rt_err_t _thread_init(struct rt_thread *thread,
/* init thread stack */
rt_memset(thread->stack_addr, '#', thread->stack_size);
#ifdef ARCH_CPU_STACK_GROWS_UPWARD
#ifdef RT_USING_TLS
thread->tls = thread->stack_addr;
_init_tls(thread->tls);
Copy link
Member

Choose a reason for hiding this comment

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

From my understanding, it's a porting layer for tls (in libc?). It's better to use a unify name for here.

Copy link
Author

Choose a reason for hiding this comment

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

Agreed -- I will adjust this to use an rt-prefixed name instead of the internal picolibc name.

Copy link
Author

Choose a reason for hiding this comment

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

I've pushed an updated version which creates an rt abstraction and uses the picolibc implementation for picolibc.

Copy link
Author

Choose a reason for hiding this comment

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

And another update which installs the GNU Arm Embedded Toolkit version of picolibc for CI tests.

@keith-packard
Copy link
Author

Thank you for your contribution. But, where is picolibc implementation? Is it made as a software package?

Oh, Sorry! Yes, picolibc is available at https://github.com/picolibc/picolibc -- the current version is 1.7.6. I've given numerous presentations:

https://www.youtube.com/watch?v=SC6aBezNFFQ
https://www.youtube.com/watch?v=qhavUyWJ46s
https://www.youtube.com/watch?v=K9C4AJZKauE

Picolibc is also available in Debian and Debian derivatives, and is included in Crosstool-ng. Zephyr has recently approved including Picolibc as a module, which means it can be built along with the operating system.

@keith-packard
Copy link
Author

Oh, I should have made it more clear that the three patches modifying existing BSP are meant as demonstrations of how to use picolibc and what the benefits might be. I would not expect those to be merged unless RT-Thread is ready to accept picolibc as a hard dependency.

@keith-packard keith-packard force-pushed the picolibc branch 2 times, most recently from 59073ef to 017f027 Compare April 3, 2022 03:53
@keith-packard
Copy link
Author

Not sure what happened, but I got the wrong URL for the picolibc bits somehow (an extra 'v' snuck in). Weird.

This allows projects to set the LIBC variable to either 'newlib' or
'picolibc' to select between these two libraries.

RT-Thread expects picolibc to have been built with TLS support, and
that requires a small change to the project linker script to set up
TLS symbols, inserting this section:

    . = ALIGN(8);
    .tdata : {
	*(.tdata)
	*(.tdata.*)
	*(.gnu.linkonce.td.*)
    }

    .tbss (NOLOAD) : {
	*(.tbss .tbss.* .gnu.linkonce.tb.*)
	*(.tcommon)
    }

    __tdata_source = ADDR(.tdata);
    __tdata_size = SIZEOF(.tdata);
    __tbss_size = SIZEOF(.tbss);
    __tls_size = SIZEOF(.tdata) + SIZEOF(.tbss);

These should go with the section defining ROM contents as the '.tdata'
section will place initialization values for the TLS block in memory.

Signed-off-by: Keith Packard <keithp@keithp.com>
This adds a configuration option to replace the RT output functions
with macros that refer to the underlying stdio versions from libc.

Signed-off-by: Keith Packard <keithp@keithp.com>
Signed-off-by: Keith Packard <keithp@keithp.com>
Signed-off-by: Keith Packard <keithp@keithp.com>
This adds the necessary changes to the linker script and
then selects picolibc instead of the default (newlib).

Before:
   text	   data	    bss	    dec	    hex	filename
 561638	   2100	  84928	 648666	  9e5da	rtthread.elf

After:
   text	   data	    bss	    dec	    hex	filename
 525009	    692	  86844	 612545	  958c1	rtthread.elf

Signed-off-by: Keith Packard <keithp@keithp.com>
Before:
   text	   data	    bss	    dec	    hex	filename
 165330	   2256	  14256	 181842	  2c652	rtthread-vexpress.elf

After:
   text	   data	    bss	    dec	    hex	filename
 156747	   1200	  14128	 172075	  2a02b	rtthread-vexpress.elf

Signed-off-by: Keith Packard <keithp@keithp.com>
@mysterywolf mysterywolf added the discussion This PR/issue needs to be discussed later label Apr 20, 2022
@mysterywolf
Copy link
Member

Sorry, We cannot merged it PR. Thanks for you contribution, again.

@mysterywolf mysterywolf mentioned this pull request May 20, 2023
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion This PR/issue needs to be discussed later
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants