Skip to content

Commit 2ff87b1

Browse files
keith-packardnashif
authored andcommitted
Support picolibc targets
Allow --with-default-libc to control picolibc definitions 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. * '--scanf='. Allows targets to customize the version of scanf linked from the C library. Signed-off-by: Keith Packard <[email protected]> fixup Signed-off-by: Keith Packard <[email protected]>
1 parent aec7b62 commit 2ff87b1

File tree

5 files changed

+133
-12
lines changed

5 files changed

+133
-12
lines changed

gcc/config.gcc

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ case ${target} in
693693
esac
694694

695695
# Common C libraries.
696-
tm_defines="$tm_defines LIBC_GLIBC=1 LIBC_UCLIBC=2 LIBC_BIONIC=3 LIBC_MUSL=4 LIBC_NEWLIB=5 LIBC_PICOLIBC=6"
696+
tm_defines="$tm_defines LIBC_GLIBC=1 LIBC_UCLIBC=2 LIBC_BIONIC=3 LIBC_MUSL=4 LIBC_PICOLIBC=5"
697697

698698
default_libc=""
699699

@@ -1169,14 +1169,7 @@ case ${target} in
11691169
esac
11701170
;;
11711171
*-picolibc-*)
1172-
# __cxa_atexit is provided.
1173-
default_use_cxa_atexit=yes
1174-
use_gcc_stdint=wrap
11751172
default_libc=LIBC_PICOLIBC
1176-
case "${with_newlib}-${with_headers}" in
1177-
no-no) use_gcc_stdint=provide ;;
1178-
*) ;;
1179-
esac
11801173
;;
11811174

11821175
*-*-elf|arc*-*-elf*)
@@ -6145,8 +6138,24 @@ esac
61456138

61466139
case "$default_libc" in
61476140
"")
6148-
;;
6141+
;;
61496142
*)
6150-
tm_defines="$tm_defines DEFAULT_LIBC=$default_libc"
6151-
;;
6143+
tm_defines="$tm_defines DEFAULT_LIBC=$default_libc"
6144+
;;
6145+
esac
6146+
6147+
# Targets for picolibc should include picolibc-spec.h to allow
6148+
# use of picolibc-specific compiler flags
6149+
case "$default_libc" in
6150+
LIBC_PICOLIBC)
6151+
# __cxa_atexit is provided.
6152+
default_use_cxa_atexit=yes
6153+
use_gcc_stdint=wrap
6154+
tm_file="${tm_file} picolibc-spec.h"
6155+
extra_options="${extra_options} picolibc.opt"
6156+
case "${with_newlib}-${with_headers}" in
6157+
no-no) use_gcc_stdint=provide ;;
6158+
*) ;;
6159+
esac
6160+
;;
61526161
esac

gcc/config/picolibc-spec.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/* Configuration common to all targets running Picolibc.
2+
Copyright (C) 2023 Free Software Foundation, Inc.
3+
4+
This file is part of GCC.
5+
6+
GCC is free software; you can redistribute it and/or modify it
7+
under the terms of the GNU General Public License as published
8+
by the Free Software Foundation; either version 3, or (at your
9+
option) any later version.
10+
11+
GCC is distributed in the hope that it will be useful, but WITHOUT
12+
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13+
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14+
License for more details.
15+
16+
Under Section 7 of GPL version 3, you are granted additional
17+
permissions described in the GCC Runtime Library Exception, version
18+
3.1, as published by the Free Software Foundation.
19+
20+
You should have received a copy of the GNU General Public License and
21+
a copy of the GCC Runtime Library Exception along with this program;
22+
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23+
<http://www.gnu.org/licenses/>. */
24+
25+
#define PICOLIBC_LD "picolibc.ld"
26+
27+
/* Default to local-exec TLS model. */
28+
#undef OS_CC1_SPEC
29+
#define OS_CC1_SPEC " %{!ftls-model=*:-ftls-model=local-exec}"
30+
31+
/* Pass along preprocessor definitions when --printf or --scanf are specified */
32+
#undef CPP_SPEC
33+
#define CPP_SPEC \
34+
"%{-printf=*: -D_PICOLIBC_PRINTF='%*'}" \
35+
" %{-scanf=*: -D_PICOLIBC_SCANF='%*'}"
36+
37+
/*
38+
* Add picolibc.ld if not using -r or -T and we can find it.
39+
* Define vfprintf if --printf is set
40+
* Define vfscanf if --scanf is set
41+
*/
42+
#undef LINK_LIBC_SPEC
43+
#define LINK_LIBC_SPEC \
44+
"%{!r:%{!T*: %:if-exists-then-else(%:find-file(" PICOLIBC_LD ") %T" PICOLIBC_LD ")}}" \
45+
" %{-printf=*:--defsym=" USER_LABEL_PREFIX "vfprintf=" USER_LABEL_PREFIX "__%*_vfprintf}" \
46+
" %{-scanf=*:--defsym=" USER_LABEL_PREFIX "vfscanf=" USER_LABEL_PREFIX "__%*_vfscanf}"
47+
48+
/*
49+
* Place the C library, libgcc and any oslib in a link group to resolve
50+
* interdependencies
51+
*/
52+
#undef LIB_SPEC
53+
#define LIB_SPEC "--start-group -lc %{-oslib=*:-l%*} %(libgcc) --end-group"
54+
55+
/* Select alternate crt0 version if --crt0 is specified */
56+
#undef STARTFILE_SPEC
57+
#define STARTFILE_SPEC "%{-crt0=*:crt0-%*%O%s; :crt0%O%s}"
58+
59+
#define EH_TABLES_CAN_BE_READ_ONLY 1

gcc/config/picolibc.opt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
; Processor-independent options for picolibc.
2+
;
3+
; Copyright (C) 2022 Free Software Foundation, Inc.
4+
;
5+
; This file is part of GCC.
6+
;
7+
; GCC is free software; you can redistribute it and/or modify it under
8+
; the terms of the GNU General Public License as published by the Free
9+
; Software Foundation; either version 3, or (at your option) any later
10+
; version.
11+
;
12+
; GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13+
; WARRANTY; without even the implied warranty of MERCHANTABILITY or
14+
; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15+
; for more details.
16+
;
17+
; You should have received a copy of the GNU General Public License
18+
; along with GCC; see the file COPYING3. If not see
19+
; <http://www.gnu.org/licenses/>.
20+
21+
-oslib
22+
Driver Separate Alias(-oslib=)
23+
24+
-oslib=
25+
Driver Joined
26+
Specify an OS support library to load after libc.
27+
28+
-crt0
29+
Driver Separate Alias(-crt0=)
30+
31+
-crt0=
32+
Driver Joined
33+
Specify an alternate startup file.
34+
35+
-printf
36+
Driver Separate Alias(-printf=)
37+
38+
-printf=
39+
Driver Joined
40+
Specify the printf version linked from libc.
41+
42+
-scanf
43+
Driver Separate Alias(-scanf=)
44+
45+
-scanf=
46+
Driver Joined
47+
Specify the scanf version linked from libc.

gcc/config/picolibc.opt.urls

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
; Autogenerated by regenerate-opt-urls.py from gcc/config/picolibc.opt and generated HTML
2+

gcc/gcc.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1984,7 +1984,7 @@ init_spec (void)
19841984
#endif
19851985

19861986
#if defined LINK_EH_SPEC || defined LINK_BUILDID_SPEC || \
1987-
defined LINKER_HASH_STYLE
1987+
defined LINKER_HASH_STYLE || defined LINK_LIBC_SPEC
19881988
# ifdef LINK_BUILDID_SPEC
19891989
/* Prepend LINK_BUILDID_SPEC to whatever link_spec we had before. */
19901990
obstack_grow (&obstack, LINK_BUILDID_SPEC, sizeof (LINK_BUILDID_SPEC) - 1);
@@ -2002,6 +2002,10 @@ init_spec (void)
20022002
obstack_grow (&obstack, LINKER_HASH_STYLE, sizeof (LINKER_HASH_STYLE) - 1);
20032003
obstack_1grow (&obstack, ' ');
20042004
}
2005+
# endif
2006+
# ifdef LINK_LIBC_SPEC
2007+
/* Prepend LINK_LIBC_SPEC to whatever link_spec we had before. */
2008+
obstack_grow (&obstack, LINK_LIBC_SPEC, sizeof (LINK_LIBC_SPEC) - 1);
20052009
# endif
20062010
obstack_grow0 (&obstack, link_spec, strlen (link_spec));
20072011
link_spec = XOBFINISH (&obstack, const char *);

0 commit comments

Comments
 (0)