Match *-picolibc-* and select picolibc as the default C library, plus
continuing to use the newlib-based logic for other configuration
items.

Add custom spec file fragments for use with picolibc:

 * '--oslib='. 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.

 * '--crt0='. Allows targets to use an alternate crt0 in place of the
   usual one as provided by Picolibc.

 * '--printf='. Allows targets to customize the version of printf linked
   from the C library.

Signed-off-by: Keith Packard <kei...@keithp.com>
---
 gcc/config.gcc               | 23 +++++++++++++++-
 gcc/config/picolibc-spec.h   | 53 ++++++++++++++++++++++++++++++++++++
 gcc/config/picolibc.opt      | 40 +++++++++++++++++++++++++++
 gcc/config/picolibc.opt.urls |  2 ++
 4 files changed, 117 insertions(+), 1 deletion(-)
 create mode 100644 gcc/config/picolibc-spec.h
 create mode 100644 gcc/config/picolibc.opt
 create mode 100644 gcc/config/picolibc.opt.urls

diff --git a/gcc/config.gcc b/gcc/config.gcc
index 262b38cf0e3..f57897b0298 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -695,7 +695,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_PICOLIBC=5"
 
 # 32-bit x86 processors supported by --with-arch=.  Each processor
 # MUST be separated by exactly one space.
@@ -1163,6 +1163,10 @@ case ${target} in
       ;;
   esac
   ;;
+*-picolibc-*)
+  tm_defines="$tm_defines DEFAULT_LIBC=LIBC_PICOLIBC"
+  ;;
+
 *-*-elf|arc*-*-elf*)
   # Assume that newlib is being used and so __cxa_atexit is provided.
   default_use_cxa_atexit=yes
@@ -6162,3 +6166,20 @@ i[34567]86-*-* | x86_64-*-*)
        fi
        ;;
 esac
+
+
+# Targets for picolibc should include picolibc-spec.h to allow
+# use of picolibc-specific compiler flags
+case ${target} in
+*picolibc*)
+    # __cxa_atexit is provided.
+    default_use_cxa_atexit=yes
+    use_gcc_stdint=wrap
+    tm_file="${tm_file} picolibc-spec.h"
+    extra_options="${extra_options} picolibc.opt"
+    case "${with_newlib}-${with_headers}" in
+       no-no) use_gcc_stdint=provide ;;
+       *) ;;
+    esac
+    ;;
+esac
diff --git a/gcc/config/picolibc-spec.h b/gcc/config/picolibc-spec.h
new file mode 100644
index 00000000000..e9ef59932f2
--- /dev/null
+++ b/gcc/config/picolibc-spec.h
@@ -0,0 +1,53 @@
+/* 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/>.  */
+
+#define PICOLIBC_LD "picolibc.ld"
+
+/* Default to local-exec TLS model.  */
+#undef OS_CC1_SPEC
+#define OS_CC1_SPEC " %{!ftls-model=*:-ftls-model=local-exec}"
+
+/* Pass along a preprocessor definition when --printf is specified */
+#undef CPP_SPEC
+#define CPP_SPEC \
+  "%{-printf=*: -D_PICOLIBC_PRINTF='%*'}"
+
+/* Add picolibc.ld if it exists. Define vfprintf and vfscanf if --printf is 
set */
+#undef LINK_SPEC
+#define LINK_SPEC \
+  "%:if-exists-then-else(%:find-file(" PICOLIBC_LD ") %{!T:-T%:find-file(" 
PICOLIBC_LD ")})"   \
+  " %{-printf=*:--defsym=vfprintf=__%*_vfprintf 
--defsym=vfscanf=__%*_vfscanf}" \
+
+/*
+ * Place the C library, libgcc and any oslib in a link group to resolve
+ * interdependencies
+ */
+#undef  LIB_SPEC
+#define LIB_SPEC "--start-group -lc %{-oslib=*:-l%*} %(libgcc) --end-group"
+
+/* Select alternate crt0 version if --crt0 is specified */
+#undef  STARTFILE_SPEC
+#define STARTFILE_SPEC "%{-crt0=*:crt0-%*%O%s; :crt0%O%s}"
+
+#define EH_TABLES_CAN_BE_READ_ONLY 1
diff --git a/gcc/config/picolibc.opt b/gcc/config/picolibc.opt
new file mode 100644
index 00000000000..9e342e9153c
--- /dev/null
+++ b/gcc/config/picolibc.opt
@@ -0,0 +1,40 @@
+; 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.
+
+-crt0
+Driver Separate Alias(-crt0=)
+
+-crt0=
+Driver Joined
+Specify an alternate startup file.
+
+-printf
+Driver Separate Alias(-printf=)
+
+-printf=
+Driver Joined
+Specify the printf version linked from libc.
diff --git a/gcc/config/picolibc.opt.urls b/gcc/config/picolibc.opt.urls
new file mode 100644
index 00000000000..b3a57d15a39
--- /dev/null
+++ b/gcc/config/picolibc.opt.urls
@@ -0,0 +1,2 @@
+; Autogenerated by regenerate-opt-urls.py from gcc/config/picolibc.opt and 
generated HTML
+
-- 
2.45.2

Reply via email to