public inbox for gcc-patches@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
                   ` (3 more replies)
  0 siblings, 4 replies; 19+ 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] 19+ 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
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 19+ 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] 19+ 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; 19+ 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] 19+ 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; 19+ 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] 19+ 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-08-24 19:32   ` Keith Packard
  2022-09-02 12:36 ` Richard Sandiford
  2022-09-03  6:07 ` Keith Packard
  3 siblings, 1 reply; 19+ 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] 19+ messages in thread

* Re: [PATCH 0/3] picolibc: Add picolibc linking help
  2022-08-24 18:24 ` [PATCH 0/3] picolibc: Add picolibc linking help Andrew Pinski
@ 2022-08-24 19:32   ` Keith Packard
  2022-08-25  7:16     ` Kito Cheng
  0 siblings, 1 reply; 19+ messages in thread
From: Keith Packard @ 2022-08-24 19:32 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: gcc-patches

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

Andrew Pinski <pinskia@gmail.com> writes:

(removing gcc@ as not appropriate for patch discussions)

Thanks for reviewing my patches; I appreciate the time you have taken to
think about this.

> 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?

Several architectures do include support for newlib's libgloss in their
gcc configuration today (i386, m32r, microblaze, nds32, pru, riscv and
sh), so I wondered if it made sense to add support for picolibc's
target-specific libraries as well.

Picolibc does deliver a spec file fragment which implements this
functionality, but that requires the addition of --specs=picolibc.specs
to the gcc command line instead of being built-in to gcc itself. When
creating an integrated toolchain using picolibc, it seems a bit odd to
require an option for the toolchain to work.

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

As a general-purpose embedded C library, picolibc doesn't include any
OS-specific code. Instead, it defines a limited subset of POSIX
interfaces which are to be supplied by the target platform.

Picolibc itself supplies sample implementations of these ABIs that can
run on top of bare metal systems with semihosting support which are used
while testing picolibc itself.

This is similar to newlib's libgloss: the C library is built atop
another library which needs to follow it in the linker command line for
symbol resolution to work correctly. Making this work requires a change
in the *lib spec file fragment.

Adjusting the *lib fragment can either be done in an externally provided
specs file, or built-in to gcc. Both of these mechanisms are present in
the gcc ecosystem today, leading me to wonder whether the gcc community
would be interested in having an integrated option available.

-- 
-keith

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

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

* Re: [PATCH 0/3] picolibc: Add picolibc linking help
  2022-08-24 19:32   ` Keith Packard
@ 2022-08-25  7:16     ` Kito Cheng
  2022-08-25 17:38       ` Keith Packard
  0 siblings, 1 reply; 19+ messages in thread
From: Kito Cheng @ 2022-08-25  7:16 UTC (permalink / raw)
  To: Keith Packard; +Cc: Andrew Pinski, GCC Patches

I am thinking that maybe we should add
-mlibc=[newlib|newlib-nano|picolibc|unknown] option to bare-matel
toolchain, one reason is having an unify interface to select libc
implementation between clang/LLVM, spec file is a GCC specific stuff,
that cause very bad compatibility between GCC and clang/LLVM, and
having option to control that would be better since clang/LLVM don't
have those configure time option.

For linux toolchain, it uses -mbionic, -muclibc, -mglibc and -mmusl,
maybe we could also use an unify -mlibc= option for that?



On Thu, Aug 25, 2022 at 3:32 AM Keith Packard via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> Andrew Pinski <pinskia@gmail.com> writes:
>
> (removing gcc@ as not appropriate for patch discussions)
>
> Thanks for reviewing my patches; I appreciate the time you have taken to
> think about this.
>
> > 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?
>
> Several architectures do include support for newlib's libgloss in their
> gcc configuration today (i386, m32r, microblaze, nds32, pru, riscv and
> sh), so I wondered if it made sense to add support for picolibc's
> target-specific libraries as well.
>
> Picolibc does deliver a spec file fragment which implements this
> functionality, but that requires the addition of --specs=picolibc.specs
> to the gcc command line instead of being built-in to gcc itself. When
> creating an integrated toolchain using picolibc, it seems a bit odd to
> require an option for the toolchain to work.
>
> > What OS libraries are not included in libc? I trying to figure out why
> > this needs to be special cased here.
>
> As a general-purpose embedded C library, picolibc doesn't include any
> OS-specific code. Instead, it defines a limited subset of POSIX
> interfaces which are to be supplied by the target platform.
>
> Picolibc itself supplies sample implementations of these ABIs that can
> run on top of bare metal systems with semihosting support which are used
> while testing picolibc itself.
>
> This is similar to newlib's libgloss: the C library is built atop
> another library which needs to follow it in the linker command line for
> symbol resolution to work correctly. Making this work requires a change
> in the *lib spec file fragment.
>
> Adjusting the *lib fragment can either be done in an externally provided
> specs file, or built-in to gcc. Both of these mechanisms are present in
> the gcc ecosystem today, leading me to wonder whether the gcc community
> would be interested in having an integrated option available.
>
> --
> -keith

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

* Re: [PATCH 0/3] picolibc: Add picolibc linking help
  2022-08-25  7:16     ` Kito Cheng
@ 2022-08-25 17:38       ` Keith Packard
  0 siblings, 0 replies; 19+ messages in thread
From: Keith Packard @ 2022-08-25 17:38 UTC (permalink / raw)
  To: Kito Cheng; +Cc: Andrew Pinski, GCC Patches

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

Kito Cheng <kito.cheng@gmail.com> writes:

> I am thinking that maybe we should add
> -mlibc=[newlib|newlib-nano|picolibc|unknown] option to bare-matel
> toolchain, one reason is having an unify interface to select libc
> implementation between clang/LLVM, spec file is a GCC specific stuff,
> that cause very bad compatibility between GCC and clang/LLVM, and
> having option to control that would be better since clang/LLVM don't
> have those configure time option.

You can create specs file for each library so that you select the
library with --specs=picolibc.specs or --specs=newlib-nano.specs. That
seems sufficient as you can mess with various compiler options and set
the header paths. Crosstool-ng does this and it enables delivering
newlib, newlib-nano and picolibc in the same toolchain.

-- 
-keith

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

^ permalink raw reply	[flat|nested] 19+ 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
  2022-09-03  6:07 ` Keith Packard
  3 siblings, 1 reply; 19+ 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] 19+ 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; 19+ 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] 19+ messages in thread

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

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.

This is the second version of these patches which address several
issues raised in review by Richard Sandiford.

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            | 65 +++++++++++++++++++++++++++++++++------
 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, 104 insertions(+), 9 deletions(-)
 create mode 100644 gcc/config/picolibc.opt

-- 
2.36.1


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

* [PATCH 1/3] Allow default libc to be specified to configure
  2022-09-03  6:07 ` Keith Packard
@ 2022-09-03  6:07   ` Keith Packard
  2022-09-03  6:07   ` [PATCH 2/3] Add newlib and picolibc as default C library choices Keith Packard
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 19+ messages in thread
From: Keith Packard @ 2022-09-03  6:07 UTC (permalink / raw)
  To: gcc-patches; +Cc: Keith Packard, Richard Sandiford

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.

If an unknown --with-default-libc= value is provided, emit an error
and stop.

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

diff --git a/gcc/config.gcc b/gcc/config.gcc
index f4e757bd853..bf838b1545d 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -644,6 +644,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 \
@@ -849,16 +851,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
@@ -951,7 +953,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
@@ -1603,13 +1606,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
 			;;
 		    *)
@@ -2982,7 +2985,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*)
@@ -5772,3 +5775,30 @@ 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
+    ;;
+*)
+    echo "Unknown libc in --with-default-libc=$with_default_libc" 1>&2
+    exit 1
+    ;;
+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 50bb61c1b61..7302c987282 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -2472,6 +2472,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] 19+ messages in thread

* [PATCH 2/3] Add newlib and picolibc as default C library choices
  2022-09-03  6:07 ` Keith Packard
  2022-09-03  6:07   ` [PATCH 1/3] Allow default libc to be specified to configure Keith Packard
@ 2022-09-03  6:07   ` Keith Packard
  2022-09-03  6:07   ` [PATCH 3/3] Add '--oslib=' option when default C library is picolibc Keith Packard
  2023-02-05  7:10   ` [PATCH 0/3] picolibc: Add picolibc linking help (v3) Keith Packard
  3 siblings, 0 replies; 19+ messages in thread
From: Keith Packard @ 2022-09-03  6:07 UTC (permalink / raw)
  To: gcc-patches; +Cc: Keith Packard, Richard Sandiford

Don't set the DEFAULT_LIBC variable for newlib configurations as that
is how it currently works for systems using newlib as the default.

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

diff --git a/gcc/config.gcc b/gcc/config.gcc
index bf838b1545d..6f8f13a811a 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -642,7 +642,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=""
 
@@ -5789,6 +5789,17 @@ bionic)
 musl)
     default_libc=LIBC_MUSL
     ;;
+newlib)
+
+    # Newlib configurations don't set the DEFAULT_LIBC variable, so
+    # avoid changing those allowing --with-default-libc=newlib but
+    # don't actually set the DEFAULT_LIBC variable.
+
+    default_libc=
+    ;;
+picolibc)
+    default_libc=LIBC_PICOLIBC
+    ;;
 *)
     echo "Unknown libc in --with-default-libc=$with_default_libc" 1>&2
     exit 1
-- 
2.36.1


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

* [PATCH 3/3] Add '--oslib=' option when default C library is picolibc
  2022-09-03  6:07 ` Keith Packard
  2022-09-03  6:07   ` [PATCH 1/3] Allow default libc to be specified to configure Keith Packard
  2022-09-03  6:07   ` [PATCH 2/3] Add newlib and picolibc as default C library choices Keith Packard
@ 2022-09-03  6:07   ` Keith Packard
  2022-11-21  7:11     ` Sebastian Huber
  2023-02-05  7:10   ` [PATCH 0/3] picolibc: Add picolibc linking help (v3) Keith Packard
  3 siblings, 1 reply; 19+ messages in thread
From: Keith Packard @ 2022-09-03  6:07 UTC (permalink / raw)
  To: gcc-patches; +Cc: Keith Packard, Richard Sandiford

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 6f8f13a811a..9bb45ec85ab 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -5813,3 +5813,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..dc5b9374814 100644
--- a/gcc/config/arm/elf.h
+++ b/gcc/config/arm/elf.h
@@ -150,3 +150,8 @@
 #undef L_floatundisf
 #endif
 
+#if 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..b9d2f2485e8 100644
--- a/gcc/config/nds32/elf.h
+++ b/gcc/config/nds32/elf.h
@@ -34,8 +34,12 @@
   " %{shared:-shared}" \
   NDS32_RELAX_SPEC
 
+#if 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 f3d767c6d42..57471cee702 100644
--- a/gcc/config/riscv/elf.h
+++ b/gcc/config/riscv/elf.h
@@ -27,9 +27,13 @@ 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 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 " \
   "%{!nostartfiles:%{!nodefaultlibs:%{!nolibc:%{!nostdlib:%:riscv_multi_lib_check()}}}}"
+#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..093bf2800db 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 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] 19+ messages in thread

* Re: [PATCH 3/3] Add '--oslib=' option when default C library is picolibc
  2022-09-03  6:07   ` [PATCH 3/3] Add '--oslib=' option when default C library is picolibc Keith Packard
@ 2022-11-21  7:11     ` Sebastian Huber
  0 siblings, 0 replies; 19+ messages in thread
From: Sebastian Huber @ 2022-11-21  7:11 UTC (permalink / raw)
  To: Keith Packard, gcc-patches; +Cc: Richard Sandiford

On 03/09/2022 08:07, Keith Packard via Gcc-patches wrote:
> diff --git a/gcc/config/arm/elf.h b/gcc/config/arm/elf.h
> index 3d111433ede..dc5b9374814 100644
> --- a/gcc/config/arm/elf.h
> +++ b/gcc/config/arm/elf.h
> @@ -150,3 +150,8 @@
>   #undef L_floatundisf
>   #endif
>   
> +#if DEFAULT_LIBC == LIBC_PICOLIBC
> +#undef  LIB_SPEC
> +#define LIB_SPEC "--start-group %(libgcc)  -lc %{-oslib=*:-l%*} --end-group"
> +#endif
> +

Can't you add a gcc/config/picolibc.h (similar to gcc/config/rtems.h) 
which is placed in gcc/config.gcc with something like this:

picolibc)
     default_libc=LIBC_PICOLIBC
     tm_file="${tm_file} picolibc.h"
     ;;

In this header place:

#undef  LIB_SPEC
#define LIB_SPEC "--start-group %(libgcc)  -lc %{-oslib=*:-l%*} --end-group"
#endif

This avoids having to modify all the ELF-specific files.

-- 
embedded brains GmbH
Herr Sebastian HUBER
Dornierstr. 4
82178 Puchheim
Germany
email: sebastian.huber@embedded-brains.de
phone: +49-89-18 94 741 - 16
fax:   +49-89-18 94 741 - 08

Registergericht: Amtsgericht München
Registernummer: HRB 157899
Vertretungsberechtigte Geschäftsführer: Peter Rasmussen, Thomas Dörfler
Unsere Datenschutzerklärung finden Sie hier:
https://embedded-brains.de/datenschutzerklaerung/

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

* [PATCH 0/3] picolibc: Add picolibc linking help (v3)
  2022-09-03  6:07 ` Keith Packard
                     ` (2 preceding siblings ...)
  2022-09-03  6:07   ` [PATCH 3/3] Add '--oslib=' option when default C library is picolibc Keith Packard
@ 2023-02-05  7:10   ` Keith Packard
  2023-02-05  7:10     ` [PATCH 1/3] Allow default libc to be specified to configure Keith Packard
                       ` (2 more replies)
  3 siblings, 3 replies; 19+ messages in thread
From: Keith Packard @ 2023-02-05  7:10 UTC (permalink / raw)
  To: gcc-patches
  Cc: Keith Packard, Andrew Pinski, Kito Cheng, Richard Sandiford,
	Sebastian Huber

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.

The second version of this series includes the following updates:

 1. Add a check for unrecognized C library passed to the
    --with-default-libc to catch errors. Suggested by Richard Sandiford.

The third version of this series includes the following updates:

 1. Replace the various target-specific hacks with a new file,
    gcc/config/picolibc.h which adjusts the LIB_SPEC, STARTFILE_SPEC
    and ENDFILE_SPEC values. Suggested by Sebastian Huber.

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          | 66 +++++++++++++++++++++++++++++++++++------
 gcc/config/picolibc.h   | 32 ++++++++++++++++++++
 gcc/config/picolibc.opt | 26 ++++++++++++++++
 gcc/configure.ac        |  4 +++
 4 files changed, 119 insertions(+), 9 deletions(-)
 create mode 100644 gcc/config/picolibc.h
 create mode 100644 gcc/config/picolibc.opt

-- 
2.39.0


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

* [PATCH 1/3] Allow default libc to be specified to configure
  2023-02-05  7:10   ` [PATCH 0/3] picolibc: Add picolibc linking help (v3) Keith Packard
@ 2023-02-05  7:10     ` Keith Packard
  2023-02-05  7:10     ` [PATCH 2/3] Add newlib and picolibc as default C library choices Keith Packard
  2023-02-05  7:10     ` [PATCH 3/3] Add '--oslib=' option when default C library is picolibc Keith Packard
  2 siblings, 0 replies; 19+ messages in thread
From: Keith Packard @ 2023-02-05  7:10 UTC (permalink / raw)
  To: gcc-patches
  Cc: Keith Packard, Andrew Pinski, Kito Cheng, Richard Sandiford,
	Sebastian Huber

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.

If an unknown --with-default-libc= value is provided, emit an error
and stop.

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

diff --git a/gcc/config.gcc b/gcc/config.gcc
index f0958e1c959..0679a76bebb 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -663,6 +663,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 \
@@ -869,16 +871,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
@@ -987,7 +989,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
@@ -1651,13 +1654,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
 			;;
 		    *)
@@ -3037,7 +3040,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*)
@@ -5862,3 +5865,32 @@ 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
+    ;;
+"")
+    ;;
+*)
+    echo "Unknown libc in --with-default-libc=$with_default_libc" 1>&2
+    exit 1
+    ;;
+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 62bc908b991..c30e3c7ae72 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -2481,6 +2481,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.39.0


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

* [PATCH 2/3] Add newlib and picolibc as default C library choices
  2023-02-05  7:10   ` [PATCH 0/3] picolibc: Add picolibc linking help (v3) Keith Packard
  2023-02-05  7:10     ` [PATCH 1/3] Allow default libc to be specified to configure Keith Packard
@ 2023-02-05  7:10     ` Keith Packard
  2023-02-05  7:10     ` [PATCH 3/3] Add '--oslib=' option when default C library is picolibc Keith Packard
  2 siblings, 0 replies; 19+ messages in thread
From: Keith Packard @ 2023-02-05  7:10 UTC (permalink / raw)
  To: gcc-patches
  Cc: Keith Packard, Andrew Pinski, Kito Cheng, Richard Sandiford,
	Sebastian Huber

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

diff --git a/gcc/config.gcc b/gcc/config.gcc
index 0679a76bebb..84bca8df615 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -661,7 +661,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=""
 
@@ -5879,6 +5879,15 @@ bionic)
 musl)
     default_libc=LIBC_MUSL
     ;;
+newlib)
+    # Newlib configurations don't set the DEFAULT_LIBC variable, so
+    # avoid changing those by allowing --with-default-libc=newlib but
+    # not actually setting the DEFAULT_LIBC variable.
+    default_libc=
+    ;;
+picolibc)
+    default_libc=LIBC_PICOLIBC
+    ;;
 "")
     ;;
 *)
-- 
2.39.0


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

* [PATCH 3/3] Add '--oslib=' option when default C library is picolibc
  2023-02-05  7:10   ` [PATCH 0/3] picolibc: Add picolibc linking help (v3) Keith Packard
  2023-02-05  7:10     ` [PATCH 1/3] Allow default libc to be specified to configure Keith Packard
  2023-02-05  7:10     ` [PATCH 2/3] Add newlib and picolibc as default C library choices Keith Packard
@ 2023-02-05  7:10     ` Keith Packard
  2 siblings, 0 replies; 19+ messages in thread
From: Keith Packard @ 2023-02-05  7:10 UTC (permalink / raw)
  To: gcc-patches
  Cc: Keith Packard, Andrew Pinski, Kito Cheng, Richard Sandiford,
	Sebastian Huber

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

Signed-off-by: Keith Packard <keithp@keithp.com>
---
 gcc/config.gcc          |  7 +++++++
 gcc/config/picolibc.h   | 32 ++++++++++++++++++++++++++++++++
 gcc/config/picolibc.opt | 26 ++++++++++++++++++++++++++
 3 files changed, 65 insertions(+)
 create mode 100644 gcc/config/picolibc.h
 create mode 100644 gcc/config/picolibc.opt

diff --git a/gcc/config.gcc b/gcc/config.gcc
index 84bca8df615..e2863efd680 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -5903,3 +5903,10 @@ 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"
+	tm_file="${tm_file} picolibc.h"
+	;;
+esac
diff --git a/gcc/config/picolibc.h b/gcc/config/picolibc.h
new file mode 100644
index 00000000000..144f11ebcf2
--- /dev/null
+++ b/gcc/config/picolibc.h
@@ -0,0 +1,32 @@
+/* Configuration common to all targets running Picolibc.
+   Copyright (C) 2023 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.
+
+   Under Section 7 of GPL version 3, you are granted additional
+   permissions described in the GCC Runtime Library Exception, version
+   3.1, as published by the Free Software Foundation.
+
+   You should have received a copy of the GNU General Public License and
+   a copy of the GCC Runtime Library Exception along with this program;
+   see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+   <http://www.gnu.org/licenses/>.  */
+
+#undef  LIB_SPEC
+#define LIB_SPEC "--start-group -lc %{-oslib=*:-l%*} %(libgcc) --end-group"
+
+#undef  STARTFILE_SPEC
+#define STARTFILE_SPEC "crt0%O%s"
+
+#undef  ENDFILE_SPEC
+#define ENDFILE_SPEC ""
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.
-- 
2.39.0


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

end of thread, other threads:[~2023-02-05  7:10 UTC | newest]

Thread overview: 19+ 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-08-24 19:32   ` Keith Packard
2022-08-25  7:16     ` Kito Cheng
2022-08-25 17:38       ` Keith Packard
2022-09-02 12:36 ` Richard Sandiford
2022-09-03  6:02   ` Keith Packard
2022-09-03  6:07 ` Keith Packard
2022-09-03  6:07   ` [PATCH 1/3] Allow default libc to be specified to configure Keith Packard
2022-09-03  6:07   ` [PATCH 2/3] Add newlib and picolibc as default C library choices Keith Packard
2022-09-03  6:07   ` [PATCH 3/3] Add '--oslib=' option when default C library is picolibc Keith Packard
2022-11-21  7:11     ` Sebastian Huber
2023-02-05  7:10   ` [PATCH 0/3] picolibc: Add picolibc linking help (v3) Keith Packard
2023-02-05  7:10     ` [PATCH 1/3] Allow default libc to be specified to configure Keith Packard
2023-02-05  7:10     ` [PATCH 2/3] Add newlib and picolibc as default C library choices Keith Packard
2023-02-05  7:10     ` [PATCH 3/3] Add '--oslib=' option when default C library is picolibc 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).