public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 0/3] picolibc: Add picolibc linking help
@ 2022-08-24 18:04 Keith Packard
  2022-08-24 18:04 ` [PATCH 1/3] picolibc: Allow default libc to be specified to configure Keith Packard
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Keith Packard @ 2022-08-24 18:04 UTC (permalink / raw)
  To: gcc; +Cc: gcc-patches, Keith Packard

Picolibc is a C library for embedded systems based on code from newlib
and avr libc. To connect some system-dependent picolibc functions
(like stdio) to an underlying platform, the platform may provide an OS
library.

This OS library must follow the C library in the link command line. In
current picolibc, that is done by providing an alternate .specs file
which can rewrite the *lib spec to insert the OS library in the right
spot.

This patch series adds the ability to specify the OS library on the
gcc command line when GCC is configured to us picolibc as the default
C library, and then hooks that up for arm, nds32, riscv and sh targets.

Keith Packard (3):
  Allow default libc to be specified to configure
  Add newlib and picolibc as default C library choices
  Add '--oslib=' option when default C library is picolibc

 gcc/config.gcc            | 56 ++++++++++++++++++++++++++++++++-------
 gcc/config/arm/elf.h      |  5 ++++
 gcc/config/nds32/elf.h    |  4 +++
 gcc/config/picolibc.opt   | 26 ++++++++++++++++++
 gcc/config/riscv/elf.h    |  4 +++
 gcc/config/sh/embed-elf.h |  5 ++++
 gcc/configure.ac          |  4 +++
 7 files changed, 95 insertions(+), 9 deletions(-)
 create mode 100644 gcc/config/picolibc.opt

-- 
2.36.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 1/3] picolibc: Allow default libc to be specified to configure
  2022-08-24 18:04 [PATCH 0/3] picolibc: Add picolibc linking help Keith Packard
@ 2022-08-24 18:04 ` Keith Packard
  2022-08-24 18:04   ` [PATCH 2/3] picolibc: Add newlib and picolibc as default C library choices Keith Packard
  2022-08-24 18:24 ` [PATCH 0/3] picolibc: Add picolibc linking help Andrew Pinski
  2022-09-02 12:36 ` Richard Sandiford
  2 siblings, 1 reply; 7+ messages in thread
From: Keith Packard @ 2022-08-24 18:04 UTC (permalink / raw)
  To: gcc; +Cc: gcc-patches, Keith Packard

The default C library is normally computed based on the target
triplet. However, for embedded systems, it can be useful to leave the
triplet alone while changing which C library is used by default. Other
C libraries may still be available on the system so the compiler and
can be used by specifying suitable include and library paths at build
time.

Signed-off-by: Keith Packard <keithp@keithp.com>
---
 gcc/config.gcc   | 42 ++++++++++++++++++++++++++++++++++--------
 gcc/configure.ac |  4 ++++
 2 files changed, 38 insertions(+), 8 deletions(-)

diff --git a/gcc/config.gcc b/gcc/config.gcc
index 02f58970db0..f8b6da4f4e7 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -641,6 +641,8 @@ esac
 # Common C libraries.
 tm_defines="$tm_defines LIBC_GLIBC=1 LIBC_UCLIBC=2 LIBC_BIONIC=3 LIBC_MUSL=4"
 
+default_libc=""
+
 # 32-bit x86 processors supported by --with-arch=.  Each processor
 # MUST be separated by exactly one space.
 x86_archs="athlon athlon-4 athlon-fx athlon-mp athlon-tbird \
@@ -847,16 +849,16 @@ case ${target} in
   esac
   case $target in
     *-*-*android*)
-      tm_defines="$tm_defines DEFAULT_LIBC=LIBC_BIONIC"
+      default_libc=LIBC_BIONIC
       ;;
     *-*-*uclibc* | *-*-uclinuxfdpiceabi)
-      tm_defines="$tm_defines DEFAULT_LIBC=LIBC_UCLIBC"
+      default_libc=LIBC_UCLIBC
       ;;
     *-*-*musl*)
-      tm_defines="$tm_defines DEFAULT_LIBC=LIBC_MUSL"
+      default_libc=LIBC_MUSL
       ;;
     *)
-      tm_defines="$tm_defines DEFAULT_LIBC=LIBC_GLIBC"
+      default_libc=LIBC_GLIBC
       ;;
   esac
   # Assume that glibc or uClibc or Bionic are being used and so __cxa_atexit
@@ -949,7 +951,8 @@ case ${target} in
   case ${enable_threads} in
     "" | yes | posix) thread_file='posix' ;;
   esac
-  tm_defines="$tm_defines DEFAULT_LIBC=LIBC_UCLIBC SINGLE_LIBC"
+  tm_defines="$tm_defines SINGLE_LIBC"
+  default_libc=LIBC_UCLIBC
   ;;
 *-*-rdos*)
   use_gcc_stdint=wrap
@@ -1606,13 +1609,13 @@ csky-*-*)
 
 		case ${target} in
 		    csky-*-linux-gnu*)
-			tm_defines="$tm_defines DEFAULT_LIBC=LIBC_GLIBC"
+			default_libc=LIBC_GLIBC
 			# Force .init_array support.  The configure script cannot always
 			# automatically detect that GAS supports it, yet we require it.
 			gcc_cv_initfini_array=yes
 			;;
 		    csky-*-linux-uclibc*)
-			tm_defines="$tm_defines DEFAULT_LIBC=LIBC_UCLIBC"
+			default_libc=LIBC_UCLIBC
 			default_use_cxa_atexit=no
 			;;
 		    *)
@@ -3065,7 +3068,7 @@ powerpc*-wrs-vxworks7r*)
 	tmake_file="${tmake_file} t-linux rs6000/t-linux64 rs6000/t-fprules rs6000/t-ppccomm"
 	tmake_file="${tmake_file} rs6000/t-vxworks"
 
-	tm_defines="$tm_defines DEFAULT_LIBC=LIBC_GLIBC"
+	default_libc=LIBC_GLIBC
 	extra_objs="$extra_objs linux.o rs6000-linux.o"
 	;;
 powerpc-wrs-vxworks*)
@@ -5861,3 +5864,26 @@ i[34567]86-*-* | x86_64-*-*)
 	fi
 	;;
 esac
+
+case "${with_default_libc}" in
+glibc)
+    default_libc=LIBC_GLIBC
+    ;;
+uclibc)
+    default_libc=LIBC_UCLIBC
+    ;;
+bionic)
+    default_libc=LIBC_BIONIC
+    ;;
+musl)
+    default_libc=LIBC_MUSL
+    ;;
+esac
+
+case "$default_libc" in
+"")
+        ;;
+*)
+	tm_defines="$tm_defines DEFAULT_LIBC=$default_libc"
+	;;
+esac
diff --git a/gcc/configure.ac b/gcc/configure.ac
index 819b490d1b6..0b76613b635 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -2490,6 +2490,10 @@ if { { test x$host != x$target && test "x$with_sysroot" = x ; } ||
 fi
 AC_SUBST(inhibit_libc)
 
+AC_ARG_WITH(default-libc,
+	[AS_HELP_STRING([--with-default-libc],
+			[Use specified default C library])])
+
 # When building gcc with a cross-compiler, we need to adjust things so
 # that the generator programs are still built with the native compiler.
 # Also, we cannot run fixincludes.
-- 
2.36.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 2/3] picolibc: Add newlib and picolibc as default C library choices
  2022-08-24 18:04 ` [PATCH 1/3] picolibc: Allow default libc to be specified to configure Keith Packard
@ 2022-08-24 18:04   ` Keith Packard
  2022-08-24 18:04     ` [PATCH 3/3] picolibc: Add '--oslib=' option when default C library is picolibc Keith Packard
  0 siblings, 1 reply; 7+ messages in thread
From: Keith Packard @ 2022-08-24 18:04 UTC (permalink / raw)
  To: gcc; +Cc: gcc-patches, Keith Packard

Signed-off-by: Keith Packard <keithp@keithp.com>
---
 gcc/config.gcc | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/gcc/config.gcc b/gcc/config.gcc
index f8b6da4f4e7..0aa4bd6c3dd 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -639,7 +639,7 @@ case ${target} in
 esac
 
 # Common C libraries.
-tm_defines="$tm_defines LIBC_GLIBC=1 LIBC_UCLIBC=2 LIBC_BIONIC=3 LIBC_MUSL=4"
+tm_defines="$tm_defines LIBC_GLIBC=1 LIBC_UCLIBC=2 LIBC_BIONIC=3 LIBC_MUSL=4 LIBC_NEWLIB=5 LIBC_PICOLIBC=6"
 
 default_libc=""
 
@@ -5878,6 +5878,12 @@ bionic)
 musl)
     default_libc=LIBC_MUSL
     ;;
+newlib)
+    default_libc=LIBC_NEWLIB
+    ;;
+picolibc)
+    default_libc=LIBC_PICOLIBC
+    ;;
 esac
 
 case "$default_libc" in
-- 
2.36.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [PATCH 3/3] picolibc: Add '--oslib=' option when default C library is picolibc
  2022-08-24 18:04   ` [PATCH 2/3] picolibc: Add newlib and picolibc as default C library choices Keith Packard
@ 2022-08-24 18:04     ` Keith Packard
  0 siblings, 0 replies; 7+ messages in thread
From: Keith Packard @ 2022-08-24 18:04 UTC (permalink / raw)
  To: gcc; +Cc: gcc-patches, Keith Packard

This option allows targets to insert an OS library after the C library
in the LIB_PATH spec file fragment. This library maps a few POSIX APIs
used by picolibc to underlying system capabilities.

For example, picolibc provides 'libsemihost' on various targets which
maps these APIs to semihosting capabilities. This would be used with
this option by specifying --oslib=semihost

This patch enables --oslib= on arm, nds32, riscv and sh.

Signed-off-by: Keith Packard <keithp@keithp.com>
---
 gcc/config.gcc            |  6 ++++++
 gcc/config/arm/elf.h      |  5 +++++
 gcc/config/nds32/elf.h    |  4 ++++
 gcc/config/picolibc.opt   | 26 ++++++++++++++++++++++++++
 gcc/config/riscv/elf.h    |  4 ++++
 gcc/config/sh/embed-elf.h |  5 +++++
 6 files changed, 50 insertions(+)
 create mode 100644 gcc/config/picolibc.opt

diff --git a/gcc/config.gcc b/gcc/config.gcc
index 0aa4bd6c3dd..80740d8f04e 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -5893,3 +5893,9 @@ case "$default_libc" in
 	tm_defines="$tm_defines DEFAULT_LIBC=$default_libc"
 	;;
 esac
+
+case "$default_libc" in
+    LIBC_PICOLIBC)
+	extra_options="${extra_options} picolibc.opt"
+	;;
+esac
diff --git a/gcc/config/arm/elf.h b/gcc/config/arm/elf.h
index 3d111433ede..d83ee4a1a8c 100644
--- a/gcc/config/arm/elf.h
+++ b/gcc/config/arm/elf.h
@@ -150,3 +150,8 @@
 #undef L_floatundisf
 #endif
 
+#if defined(DEFAULT_LIBC) && defined(LIBC_PICOLIBC) && DEFAULT_LIBC == LIBC_PICOLIBC
+#undef  LIB_SPEC
+#define LIB_SPEC "--start-group %(libgcc)  -lc %{-oslib=*:-l%*} --end-group"
+#endif
+
diff --git a/gcc/config/nds32/elf.h b/gcc/config/nds32/elf.h
index ebdc18cee2a..a956d531ef4 100644
--- a/gcc/config/nds32/elf.h
+++ b/gcc/config/nds32/elf.h
@@ -34,8 +34,12 @@
   " %{shared:-shared}" \
   NDS32_RELAX_SPEC
 
+#if defined(DEFAULT_LIBC) && defined(LIBC_PICOLIBC) && DEFAULT_LIBC == LIBC_PICOLIBC
+#define LIB_SPEC "--start-group %(libgcc)  -lc %{-oslib=*:-l%*} --end-group"
+#else
 #define LIB_SPEC \
   " -lc -lgloss"
+#endif
 
 #define LIBGCC_SPEC \
   " -lgcc"
diff --git a/gcc/config/picolibc.opt b/gcc/config/picolibc.opt
new file mode 100644
index 00000000000..194b3362b03
--- /dev/null
+++ b/gcc/config/picolibc.opt
@@ -0,0 +1,26 @@
+; Processor-independent options for picolibc.
+;
+; Copyright (C) 2022 Free Software Foundation, Inc.
+;
+; This file is part of GCC.
+;
+; GCC is free software; you can redistribute it and/or modify it under
+; the terms of the GNU General Public License as published by the Free
+; Software Foundation; either version 3, or (at your option) any later
+; version.
+;
+; GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+; WARRANTY; without even the implied warranty of MERCHANTABILITY or
+; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+; for more details.
+;
+; You should have received a copy of the GNU General Public License
+; along with GCC; see the file COPYING3.  If not see
+; <http://www.gnu.org/licenses/>.
+
+-oslib
+Driver Separate Alias(-oslib=)
+
+-oslib=
+Driver Joined
+Specify an OS support library to load after libc.
diff --git a/gcc/config/riscv/elf.h b/gcc/config/riscv/elf.h
index f0e865d6ef4..ca906afcd52 100644
--- a/gcc/config/riscv/elf.h
+++ b/gcc/config/riscv/elf.h
@@ -27,7 +27,11 @@ along with GCC; see the file COPYING3.  If not see
 /* Link against Newlib libraries, because the ELF backend assumes Newlib.
    Handle the circular dependence between libc and libgloss. */
 #undef  LIB_SPEC
+#if defined(DEFAULT_LIBC) && defined(LIBC_PICOLIBC) && DEFAULT_LIBC == LIBC_PICOLIBC
+#define LIB_SPEC "--start-group %(libgcc)  -lc %{-oslib=*:-l%*} --end-group"
+#else
 #define LIB_SPEC "--start-group -lc %{!specs=nosys.specs:-lgloss} --end-group"
+#endif
 
 #undef  STARTFILE_SPEC
 #define STARTFILE_SPEC "crt0%O%s crtbegin%O%s"
diff --git a/gcc/config/sh/embed-elf.h b/gcc/config/sh/embed-elf.h
index 21e51dd0bb9..e8605849e07 100644
--- a/gcc/config/sh/embed-elf.h
+++ b/gcc/config/sh/embed-elf.h
@@ -34,3 +34,8 @@ along with GCC; see the file COPYING3.  If not see
   %{Os: -lgcc-Os-4-200} \
   -lgcc \
   %{!Os: -lgcc-Os-4-200}"
+
+#if defined(DEFAULT_LIBC) && defined(LIBC_PICOLIBC) && DEFAULT_LIBC == LIBC_PICOLIBC
+#undef  LIB_SPEC
+#define LIB_SPEC "--start-group %(libgcc)  -lc %{-oslib=*:-l%*} --end-group"
+#endif
-- 
2.36.1


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/3] picolibc: Add picolibc linking help
  2022-08-24 18:04 [PATCH 0/3] picolibc: Add picolibc linking help Keith Packard
  2022-08-24 18:04 ` [PATCH 1/3] picolibc: Allow default libc to be specified to configure Keith Packard
@ 2022-08-24 18:24 ` Andrew Pinski
  2022-09-02 12:36 ` Richard Sandiford
  2 siblings, 0 replies; 7+ messages in thread
From: Andrew Pinski @ 2022-08-24 18:24 UTC (permalink / raw)
  To: Keith Packard; +Cc: gcc, gcc-patches

On Wed, Aug 24, 2022 at 11:12 AM Keith Packard via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> Picolibc is a C library for embedded systems based on code from newlib
> and avr libc. To connect some system-dependent picolibc functions
> (like stdio) to an underlying platform, the platform may provide an OS
> library.
>
> This OS library must follow the C library in the link command line. In
> current picolibc, that is done by providing an alternate .specs file
> which can rewrite the *lib spec to insert the OS library in the right
> spot.

Why do you need to change the specs to support picolibc? Why not have
the library supply the specs file instead, like what is done for
newlib and libgloss?

>
> This patch series adds the ability to specify the OS library on the
> gcc command line when GCC is configured to us picolibc as the default
> C library, and then hooks that up for arm, nds32, riscv and sh targets.


What OS libraries are not included in libc? I trying to figure out why
this needs to be special cased here.

Thanks,
Andrew Pinski

>
> Keith Packard (3):
>   Allow default libc to be specified to configure
>   Add newlib and picolibc as default C library choices
>   Add '--oslib=' option when default C library is picolibc
>
>  gcc/config.gcc            | 56 ++++++++++++++++++++++++++++++++-------
>  gcc/config/arm/elf.h      |  5 ++++
>  gcc/config/nds32/elf.h    |  4 +++
>  gcc/config/picolibc.opt   | 26 ++++++++++++++++++
>  gcc/config/riscv/elf.h    |  4 +++
>  gcc/config/sh/embed-elf.h |  5 ++++
>  gcc/configure.ac          |  4 +++
>  7 files changed, 95 insertions(+), 9 deletions(-)
>  create mode 100644 gcc/config/picolibc.opt
>
> --
> 2.36.1
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/3] picolibc: Add picolibc linking help
  2022-08-24 18:04 [PATCH 0/3] picolibc: Add picolibc linking help Keith Packard
  2022-08-24 18:04 ` [PATCH 1/3] picolibc: Allow default libc to be specified to configure Keith Packard
  2022-08-24 18:24 ` [PATCH 0/3] picolibc: Add picolibc linking help Andrew Pinski
@ 2022-09-02 12:36 ` Richard Sandiford
  2022-09-03  6:02   ` Keith Packard
  2 siblings, 1 reply; 7+ messages in thread
From: Richard Sandiford @ 2022-09-02 12:36 UTC (permalink / raw)
  To: Keith Packard via Gcc-patches; +Cc: gcc, Keith Packard

Keith Packard via Gcc-patches <gcc-patches@gcc.gnu.org> writes:
> Picolibc is a C library for embedded systems based on code from newlib
> and avr libc. To connect some system-dependent picolibc functions
> (like stdio) to an underlying platform, the platform may provide an OS
> library.
>
> This OS library must follow the C library in the link command line. In
> current picolibc, that is done by providing an alternate .specs file
> which can rewrite the *lib spec to insert the OS library in the right
> spot.
>
> This patch series adds the ability to specify the OS library on the
> gcc command line when GCC is configured to us picolibc as the default
> C library, and then hooks that up for arm, nds32, riscv and sh targets.

Not really my area, but the approach LGTM FWIW.  Main question/points:

- In:

  +case "${with_default_libc}" in
  +glibc)
  +    default_libc=LIBC_GLIBC
  +    ;;

  should there be a default case that raises an error for unrecognised
  libcs?  Command-line checking for configure isn't very tight, but we
  do raise similar errors for things like invalid --enable-threads values.

- I'm not sure either way about adding LIBC_NEWLIB.  On the one hand
  it makes sense for completeness, but on the other it's write-only.
  Adding it means that --with-default-libc=newlib toolchains have a
  different macro configuration from a default toolchain even in cases
  where newlib is the default.

  On balance I think it would be better to accept
  --with-default-libc=newlib but set default_libc to the empty string.

- Should we raise an error for toolchains that don't support the given
  C library?  It feels like we should, but I realise that could be
  difficult to do.

- Very minor, but in lines like:

  +#if defined(DEFAULT_LIBC) && defined(LIBC_PICOLIBC) && DEFAULT_LIBC == LIBC_PICOLIBC

  is LIBC_PICOLIB ever undefined?  It looks like config.gcc provides
  an unconditional definition.  If it is always defined then:

  #if DEFAULT_LIBC == LIBC_PICOLIB

  would be clearer.

Thanks,
Richard

>
> Keith Packard (3):
>   Allow default libc to be specified to configure
>   Add newlib and picolibc as default C library choices
>   Add '--oslib=' option when default C library is picolibc
>
>  gcc/config.gcc            | 56 ++++++++++++++++++++++++++++++++-------
>  gcc/config/arm/elf.h      |  5 ++++
>  gcc/config/nds32/elf.h    |  4 +++
>  gcc/config/picolibc.opt   | 26 ++++++++++++++++++
>  gcc/config/riscv/elf.h    |  4 +++
>  gcc/config/sh/embed-elf.h |  5 ++++
>  gcc/configure.ac          |  4 +++
>  7 files changed, 95 insertions(+), 9 deletions(-)
>  create mode 100644 gcc/config/picolibc.opt

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: [PATCH 0/3] picolibc: Add picolibc linking help
  2022-09-02 12:36 ` Richard Sandiford
@ 2022-09-03  6:02   ` Keith Packard
  0 siblings, 0 replies; 7+ messages in thread
From: Keith Packard @ 2022-09-03  6:02 UTC (permalink / raw)
  To: Richard Sandiford, Keith Packard via Gcc-patches; +Cc: gcc

[-- Attachment #1: Type: text/plain, Size: 1454 bytes --]

Richard Sandiford <richard.sandiford@arm.com> writes:

Thanks much for reviewing this series. I really appreciate it.

>   should there be a default case that raises an error for unrecognised
>   libcs?  Command-line checking for configure isn't very tight, but we
>   do raise similar errors for things like invalid --enable-threads
>   values.

Good thinking. It's way to easy to introduce a typo somewhere and have
it get missed.

>   On balance I think it would be better to accept
>   --with-default-libc=newlib but set default_libc to the empty string.

That also makes good sense -- leave existing configurations unchanged.

> - Should we raise an error for toolchains that don't support the given
>   C library?  It feels like we should, but I realise that could be
>   difficult to do.

That would be nice, but it also feels like making it reliable enough to
be useful would be difficult to maintain in the future, even if we did
manage to make it mostly work today.

> - Very minor, but in lines like:
>
>   +#if defined(DEFAULT_LIBC) && defined(LIBC_PICOLIBC) && DEFAULT_LIBC == LIBC_PICOLIBC
>
>   is LIBC_PICOLIB ever undefined?  It looks like config.gcc provides
>   an unconditional definition.  If it is always defined then:
>
>   #if DEFAULT_LIBC == LIBC_PICOLIB
>
>   would be clearer.

Agreed. Thanks again for taking a look; I'll send a new series with
these issues fixed shortly.

-- 
-keith

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2022-09-03  6:02 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-24 18:04 [PATCH 0/3] picolibc: Add picolibc linking help Keith Packard
2022-08-24 18:04 ` [PATCH 1/3] picolibc: Allow default libc to be specified to configure Keith Packard
2022-08-24 18:04   ` [PATCH 2/3] picolibc: Add newlib and picolibc as default C library choices Keith Packard
2022-08-24 18:04     ` [PATCH 3/3] picolibc: Add '--oslib=' option when default C library is picolibc Keith Packard
2022-08-24 18:24 ` [PATCH 0/3] picolibc: Add picolibc linking help Andrew Pinski
2022-09-02 12:36 ` Richard Sandiford
2022-09-03  6:02   ` Keith Packard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).