1/* Definitions for systems using the Linux kernel, with or without
2   MMU, using ELF at the compiler level but possibly FLT for final
3   linked executables and shared libraries in some no-MMU cases, and
4   possibly with a choice of libc implementations.
5   Copyright (C) 1995-2020 Free Software Foundation, Inc.
6   Contributed by Eric Youngdale.
7   Modified for stabs-in-ELF by H.J. Lu (hjl@lucon.org).
8
9This file is part of GCC.
10
11GCC is free software; you can redistribute it and/or modify
12it under the terms of the GNU General Public License as published by
13the Free Software Foundation; either version 3, or (at your option)
14any later version.
15
16GCC is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19GNU General Public License for more details.
20
21Under Section 7 of GPL version 3, you are granted additional
22permissions described in the GCC Runtime Library Exception, version
233.1, as published by the Free Software Foundation.
24
25You should have received a copy of the GNU General Public License and
26a copy of the GCC Runtime Library Exception along with this program;
27see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
28<http://www.gnu.org/licenses/>.  */
29
30/* C libraries supported on Linux.  */
31#ifdef SINGLE_LIBC
32#define OPTION_GLIBC  (DEFAULT_LIBC == LIBC_GLIBC)
33#define OPTION_UCLIBC (DEFAULT_LIBC == LIBC_UCLIBC)
34#define OPTION_BIONIC (DEFAULT_LIBC == LIBC_BIONIC)
35#undef OPTION_MUSL
36#define OPTION_MUSL   (DEFAULT_LIBC == LIBC_MUSL)
37#else
38#define OPTION_GLIBC  (linux_libc == LIBC_GLIBC)
39#define OPTION_UCLIBC (linux_libc == LIBC_UCLIBC)
40#define OPTION_BIONIC (linux_libc == LIBC_BIONIC)
41#undef OPTION_MUSL
42#define OPTION_MUSL   (linux_libc == LIBC_MUSL)
43#endif
44
45#define GNU_USER_TARGET_OS_CPP_BUILTINS()			\
46    do {							\
47	if (OPTION_GLIBC)					\
48	  builtin_define ("__gnu_linux__");			\
49	builtin_define_std ("linux");				\
50	builtin_define_std ("unix");				\
51	builtin_assert ("system=linux");			\
52	builtin_assert ("system=unix");				\
53	builtin_assert ("system=posix");			\
54    } while (0)
55
56#define GNU_USER_TARGET_D_OS_VERSIONS()				\
57    do {							\
58	builtin_version ("linux");				\
59	if (OPTION_GLIBC)					\
60	  builtin_version ("CRuntime_Glibc");			\
61	else if (OPTION_UCLIBC)					\
62	  builtin_version ("CRuntime_UClibc");			\
63	else if (OPTION_BIONIC)					\
64	  builtin_version ("CRuntime_Bionic");			\
65	else if (OPTION_MUSL)					\
66	  builtin_version ("CRuntime_Musl");			\
67    } while (0)
68
69/* Determine which dynamic linker to use depending on whether GLIBC or
70   uClibc or Bionic or musl is the default C library and whether
71   -muclibc or -mglibc or -mbionic or -mmusl has been passed to change
72   the default.  */
73
74#define CHOOSE_DYNAMIC_LINKER1(LIBC1, LIBC2, LIBC3, LIBC4, LD1, LD2, LD3, LD4)	\
75  "%{" LIBC2 ":" LD2 ";:%{" LIBC3 ":" LD3 ";:%{" LIBC4 ":" LD4 ";:" LD1 "}}}"
76
77#if DEFAULT_LIBC == LIBC_GLIBC
78#define CHOOSE_DYNAMIC_LINKER(G, U, B, M) \
79  CHOOSE_DYNAMIC_LINKER1 ("mglibc", "muclibc", "mbionic", "mmusl", G, U, B, M)
80#elif DEFAULT_LIBC == LIBC_UCLIBC
81#define CHOOSE_DYNAMIC_LINKER(G, U, B, M) \
82  CHOOSE_DYNAMIC_LINKER1 ("muclibc", "mglibc", "mbionic", "mmusl", U, G, B, M)
83#elif DEFAULT_LIBC == LIBC_BIONIC
84#define CHOOSE_DYNAMIC_LINKER(G, U, B, M) \
85  CHOOSE_DYNAMIC_LINKER1 ("mbionic", "mglibc", "muclibc", "mmusl", B, G, U, M)
86#elif DEFAULT_LIBC == LIBC_MUSL
87#define CHOOSE_DYNAMIC_LINKER(G, U, B, M) \
88  CHOOSE_DYNAMIC_LINKER1 ("mmusl", "mglibc", "muclibc", "mbionic", M, G, U, B)
89#else
90#error "Unsupported DEFAULT_LIBC"
91#endif /* DEFAULT_LIBC */
92
93/* For most targets the following definitions suffice;
94   GLIBC_DYNAMIC_LINKER must be defined for each target using them, or
95   GLIBC_DYNAMIC_LINKER32 and GLIBC_DYNAMIC_LINKER64 for targets
96   supporting both 32-bit and 64-bit compilation.  */
97#define UCLIBC_DYNAMIC_LINKER "/lib/ld-uClibc.so.0"
98#define UCLIBC_DYNAMIC_LINKER32 "/lib/ld-uClibc.so.0"
99#define UCLIBC_DYNAMIC_LINKER64 "/lib/ld64-uClibc.so.0"
100#define UCLIBC_DYNAMIC_LINKERX32 "/lib/ldx32-uClibc.so.0"
101#define BIONIC_DYNAMIC_LINKER "/system/bin/linker"
102#define BIONIC_DYNAMIC_LINKER32 "/system/bin/linker"
103#define BIONIC_DYNAMIC_LINKER64 "/system/bin/linker64"
104#define BIONIC_DYNAMIC_LINKERX32 "/system/bin/linkerx32"
105/* Should be redefined for each target that supports musl.  */
106#define MUSL_DYNAMIC_LINKER "/dev/null"
107#define MUSL_DYNAMIC_LINKER32 "/dev/null"
108#define MUSL_DYNAMIC_LINKER64 "/dev/null"
109#define MUSL_DYNAMIC_LINKERX32 "/dev/null"
110
111#define GNU_USER_DYNAMIC_LINKER						\
112  CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKER, UCLIBC_DYNAMIC_LINKER,	\
113			 BIONIC_DYNAMIC_LINKER, MUSL_DYNAMIC_LINKER)
114#define GNU_USER_DYNAMIC_LINKER32					\
115  CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKER32, UCLIBC_DYNAMIC_LINKER32, \
116			 BIONIC_DYNAMIC_LINKER32, MUSL_DYNAMIC_LINKER32)
117#define GNU_USER_DYNAMIC_LINKER64					\
118  CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKER64, UCLIBC_DYNAMIC_LINKER64, \
119			 BIONIC_DYNAMIC_LINKER64, MUSL_DYNAMIC_LINKER64)
120#define GNU_USER_DYNAMIC_LINKERX32					\
121  CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKERX32, UCLIBC_DYNAMIC_LINKERX32, \
122			 BIONIC_DYNAMIC_LINKERX32, MUSL_DYNAMIC_LINKERX32)
123
124/* Whether we have Bionic libc runtime */
125#undef TARGET_HAS_BIONIC
126#define TARGET_HAS_BIONIC (OPTION_BIONIC)
127
128/* musl avoids problematic includes by rearranging the include directories.
129 * Unfortunately, this is mostly duplicated from cppdefault.c */
130#if DEFAULT_LIBC == LIBC_MUSL
131#define INCLUDE_DEFAULTS_MUSL_GPP			\
132    { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1,		\
133      GPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT, 0 },		\
134    { GPLUSPLUS_TOOL_INCLUDE_DIR, "G++", 1, 1,		\
135      GPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT, 1 },		\
136    { GPLUSPLUS_BACKWARD_INCLUDE_DIR, "G++", 1, 1,	\
137      GPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT, 0 },
138
139#ifdef LOCAL_INCLUDE_DIR
140#define INCLUDE_DEFAULTS_MUSL_LOCAL			\
141    { LOCAL_INCLUDE_DIR, 0, 0, 1, 1, 2 },		\
142    { LOCAL_INCLUDE_DIR, 0, 0, 1, 1, 0 },
143#else
144#define INCLUDE_DEFAULTS_MUSL_LOCAL
145#endif
146
147#ifdef PREFIX_INCLUDE_DIR
148#define INCLUDE_DEFAULTS_MUSL_PREFIX			\
149    { PREFIX_INCLUDE_DIR, 0, 0, 1, 0, 0},
150#else
151#define INCLUDE_DEFAULTS_MUSL_PREFIX
152#endif
153
154#ifdef CROSS_INCLUDE_DIR
155#define INCLUDE_DEFAULTS_MUSL_CROSS			\
156    { CROSS_INCLUDE_DIR, "GCC", 0, 0, 0, 0},
157#else
158#define INCLUDE_DEFAULTS_MUSL_CROSS
159#endif
160
161#ifdef TOOL_INCLUDE_DIR
162#define INCLUDE_DEFAULTS_MUSL_TOOL			\
163    { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1, 0, 0},
164#else
165#define INCLUDE_DEFAULTS_MUSL_TOOL
166#endif
167
168#ifdef NATIVE_SYSTEM_HEADER_DIR
169#define INCLUDE_DEFAULTS_MUSL_NATIVE			\
170    { NATIVE_SYSTEM_HEADER_DIR, 0, 0, 0, 1, 2 },	\
171    { NATIVE_SYSTEM_HEADER_DIR, 0, 0, 0, 1, 0 },
172#else
173#define INCLUDE_DEFAULTS_MUSL_NATIVE
174#endif
175
176#if defined (CROSS_DIRECTORY_STRUCTURE) && !defined (TARGET_SYSTEM_ROOT)
177# undef INCLUDE_DEFAULTS_MUSL_LOCAL
178# define INCLUDE_DEFAULTS_MUSL_LOCAL
179# undef INCLUDE_DEFAULTS_MUSL_NATIVE
180# define INCLUDE_DEFAULTS_MUSL_NATIVE
181#else
182# undef INCLUDE_DEFAULTS_MUSL_CROSS
183# define INCLUDE_DEFAULTS_MUSL_CROSS
184#endif
185
186#undef INCLUDE_DEFAULTS
187#define INCLUDE_DEFAULTS				\
188  {							\
189    INCLUDE_DEFAULTS_MUSL_GPP				\
190    INCLUDE_DEFAULTS_MUSL_LOCAL				\
191    INCLUDE_DEFAULTS_MUSL_PREFIX			\
192    INCLUDE_DEFAULTS_MUSL_CROSS				\
193    INCLUDE_DEFAULTS_MUSL_TOOL				\
194    INCLUDE_DEFAULTS_MUSL_NATIVE			\
195    { GCC_INCLUDE_DIR, "GCC", 0, 1, 0, 0 },		\
196    { 0, 0, 0, 0, 0, 0 }				\
197  }
198#endif
199
200#if (DEFAULT_LIBC == LIBC_UCLIBC) && defined (SINGLE_LIBC) /* uClinux */
201/* This is a *uclinux* target.  We don't define below macros to normal linux
202   versions, because doing so would require *uclinux* targets to include
203   linux.c, linux-protos.h, linux.opt, etc.  We could, alternatively, add
204   these files to *uclinux* targets, but that would only pollute option list
205   (add -mglibc, etc.) without adding any useful support.  */
206
207/* Define TARGET_LIBC_HAS_FUNCTION for *uclinux* targets to
208   no_c99_libc_has_function, because uclibc does not, normally, have
209   c99 runtime.  If, in special cases, uclibc does have c99 runtime,
210   this should be defined to a new hook.  Also please note that for targets
211   like *-linux-uclibc that similar check will also need to be added to
212   linux_libc_has_function.  */
213# undef TARGET_LIBC_HAS_FUNCTION
214# define TARGET_LIBC_HAS_FUNCTION no_c99_libc_has_function
215
216#else /* !uClinux, i.e., normal Linux */
217
218/* Determine what functions are present at the runtime;
219   this includes full c99 runtime and sincos.  */
220# undef TARGET_LIBC_HAS_FUNCTION
221# define TARGET_LIBC_HAS_FUNCTION linux_libc_has_function
222
223#endif
224