761f018f2198425a1b04787d58a9a4c61f02e6d3
[packages/trusty/cirros-testvm.git] / cirros-testvm / src-cirros / buildroot-2015.05 / package / gcc / 4.8.4 / 900-musl-support.patch
1 Add musl support to gcc
2
3 This patch comes from the musl-cross project at
4 https://bitbucket.org/GregorR/musl-cross/src. Compared to the upstream version:
5
6  * the config.sub modifications have been removed, because Buildroot
7    already overwrites all config.sub with a more recent config.sub
8    that has musl support.
9
10  * change to ensure that a dummy dynamic linker path
11    MUSL_DYNAMIC_LINKER<foo> is defined for all architectures,
12    otherwise building gcc for architectures not supported by musl was
13    causing build failure. Bug reported upstream at
14    https://bitbucket.org/GregorR/musl-gcc-patches/issue/4/musl-gcc-patches-break-the-build-on.
15
16  * change the USE_PT_GNU_EH_FRAME logic to keep the existing gcc logic
17    and only add the musl one as an addition, not as a replacement. Not
18    doing this breaks C++ exception handling with glibc, because
19    USE_PT_GNU_EH_FRAME doesn't get defined due to the configure script
20    not testing dl_iterate_phdr() on any system except Solaris.
21
22 Signed-off-by: Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
23 [Gustavo: Update for gcc 4.8.3]
24
25 Index: b/fixincludes/mkfixinc.sh
26 ===================================================================
27 --- a/fixincludes/mkfixinc.sh
28 +++ b/fixincludes/mkfixinc.sh
29 @@ -19,7 +19,8 @@
30      powerpc-*-eabi*    | \
31      powerpc-*-rtems*   | \
32      powerpcle-*-eabisim* | \
33 -    powerpcle-*-eabi* )
34 +    powerpcle-*-eabi* | \
35 +    *-musl* )
36         #  IF there is no include fixing,
37         #  THEN create a no-op fixer and exit
38         (echo "#! /bin/sh" ; echo "exit 0" ) > ${target}
39 Index: b/gcc/config/aarch64/aarch64-linux.h
40 ===================================================================
41 --- a/gcc/config/aarch64/aarch64-linux.h
42 +++ b/gcc/config/aarch64/aarch64-linux.h
43 @@ -21,7 +21,12 @@
44  #ifndef GCC_AARCH64_LINUX_H
45  #define GCC_AARCH64_LINUX_H
46  
47 +/* The AArch64 port currently supports two dynamic linkers:
48 +   - ld-linux-aarch64.so.1 - GLIBC dynamic linker
49 +   - ld-musl-aarch64.so.1 - musl libc dynamic linker  */
50  #define GLIBC_DYNAMIC_LINKER "/lib/ld-linux-aarch64.so.1"
51 +#undef MUSL_DYNAMIC_LINKER
52 +#define MUSL_DYNAMIC_LINKER "/lib/ld-musl-aarch64.so.1"
53  
54  #define CPP_SPEC "%{pthread:-D_REENTRANT}"
55  
56 Index: b/gcc/config/arm/linux-eabi.h
57 ===================================================================
58 --- a/gcc/config/arm/linux-eabi.h
59 +++ b/gcc/config/arm/linux-eabi.h
60 @@ -77,6 +77,23 @@
61      %{mfloat-abi=soft*:" GLIBC_DYNAMIC_LINKER_SOFT_FLOAT "} \
62      %{!mfloat-abi=*:" GLIBC_DYNAMIC_LINKER_DEFAULT "}"
63  
64 +/* For ARM musl currently supports four dynamic linkers:
65 +   - ld-musl-arm.so.1 - for the EABI-derived soft-float ABI
66 +   - ld-musl-armhf.so.1 - for the EABI-derived hard-float ABI
67 +   - ld-musl-armeb.so.1 - for the EABI-derived soft-float ABI, EB
68 +   - ld-musl-armebhf.so.1 - for the EABI-derived hard-float ABI, EB
69 +   musl does not support the legacy OABI mode.
70 +   All the dynamic linkers live in /lib.
71 +   We default to soft-float, EL. */
72 +#undef  MUSL_DYNAMIC_LINKER
73 +#if TARGET_BIG_ENDIAN_DEFAULT
74 +#define MUSL_DYNAMIC_LINKER_E "%{mlittle-endian:;:eb}"
75 +#else
76 +#define MUSL_DYNAMIC_LINKER_E "%{mbig-endian:eb}"
77 +#endif
78 +#define MUSL_DYNAMIC_LINKER \
79 +  "/lib/ld-musl-arm" MUSL_DYNAMIC_LINKER_E "%{mfloat-abi=hard:hf}.so.1"
80 +
81  /* At this point, bpabi.h will have clobbered LINK_SPEC.  We want to
82     use the GNU/Linux version, not the generic BPABI version.  */
83  #undef  LINK_SPEC
84 Index: b/gcc/config/i386/linux64.h
85 ===================================================================
86 --- a/gcc/config/i386/linux64.h
87 +++ b/gcc/config/i386/linux64.h
88 @@ -30,3 +30,10 @@
89  #define GLIBC_DYNAMIC_LINKER32 "/lib/ld-linux.so.2"
90  #define GLIBC_DYNAMIC_LINKER64 "/lib64/ld-linux-x86-64.so.2"
91  #define GLIBC_DYNAMIC_LINKERX32 "/libx32/ld-linux-x32.so.2"
92 +
93 +#undef MUSL_DYNAMIC_LINKER32
94 +#define MUSL_DYNAMIC_LINKER32 "/lib/ld-musl-i386.so.1"
95 +#undef MUSL_DYNAMIC_LINKER64
96 +#define MUSL_DYNAMIC_LINKER64 "/lib/ld-musl-x86_64.so.1"
97 +#undef MUSL_DYNAMIC_LINKERX32
98 +#define MUSL_DYNAMIC_LINKERX32 "/lib/ld-musl-x32.so.1"
99 Index: b/gcc/config/i386/linux.h
100 ===================================================================
101 --- a/gcc/config/i386/linux.h
102 +++ b/gcc/config/i386/linux.h
103 @@ -21,3 +21,5 @@
104  
105  #define GNU_USER_LINK_EMULATION "elf_i386"
106  #define GLIBC_DYNAMIC_LINKER "/lib/ld-linux.so.2"
107 +#undef MUSL_DYNAMIC_LINKER
108 +#define MUSL_DYNAMIC_LINKER "/lib/ld-musl-i386.so.1"
109 Index: b/gcc/config/linux.h
110 ===================================================================
111 --- a/gcc/config/linux.h
112 +++ b/gcc/config/linux.h
113 @@ -32,10 +32,12 @@
114  #define OPTION_GLIBC  (DEFAULT_LIBC == LIBC_GLIBC)
115  #define OPTION_UCLIBC (DEFAULT_LIBC == LIBC_UCLIBC)
116  #define OPTION_BIONIC (DEFAULT_LIBC == LIBC_BIONIC)
117 +#define OPTION_MUSL   (DEFAULT_LIBC == LIBC_MUSL)
118  #else
119  #define OPTION_GLIBC  (linux_libc == LIBC_GLIBC)
120  #define OPTION_UCLIBC (linux_libc == LIBC_UCLIBC)
121  #define OPTION_BIONIC (linux_libc == LIBC_BIONIC)
122 +#define OPTION_MUSL   (linux_libc == LIBC_MUSL)
123  #endif
124  
125  #define GNU_USER_TARGET_OS_CPP_BUILTINS()                      \
126 @@ -53,18 +55,21 @@
127     uClibc or Bionic is the default C library and whether
128     -muclibc or -mglibc or -mbionic has been passed to change the default.  */
129  
130 -#define CHOOSE_DYNAMIC_LINKER1(LIBC1, LIBC2, LIBC3, LD1, LD2, LD3)     \
131 -  "%{" LIBC2 ":" LD2 ";:%{" LIBC3 ":" LD3 ";:" LD1 "}}"
132 +#define CHOOSE_DYNAMIC_LINKER1(LIBC1, LIBC2, LIBC3, LIBC4, LD1, LD2, LD3, LD4) \
133 +  "%{" LIBC2 ":" LD2 ";:%{" LIBC3 ":" LD3 ";:%{" LIBC4 ":" LD4 ";:" LD1 "}}}"
134  
135  #if DEFAULT_LIBC == LIBC_GLIBC
136 -#define CHOOSE_DYNAMIC_LINKER(G, U, B) \
137 -  CHOOSE_DYNAMIC_LINKER1 ("mglibc", "muclibc", "mbionic", G, U, B)
138 +#define CHOOSE_DYNAMIC_LINKER(G, U, B, M) \
139 +  CHOOSE_DYNAMIC_LINKER1 ("mglibc", "muclibc", "mbionic", "mmusl", G, U, B, M)
140  #elif DEFAULT_LIBC == LIBC_UCLIBC
141 -#define CHOOSE_DYNAMIC_LINKER(G, U, B) \
142 -  CHOOSE_DYNAMIC_LINKER1 ("muclibc", "mglibc", "mbionic", U, G, B)
143 +#define CHOOSE_DYNAMIC_LINKER(G, U, B, M) \
144 +  CHOOSE_DYNAMIC_LINKER1 ("muclibc", "mglibc", "mbionic", "mmusl", U, G, B, M)
145  #elif DEFAULT_LIBC == LIBC_BIONIC
146 -#define CHOOSE_DYNAMIC_LINKER(G, U, B) \
147 -  CHOOSE_DYNAMIC_LINKER1 ("mbionic", "mglibc", "muclibc", B, G, U)
148 +#define CHOOSE_DYNAMIC_LINKER(G, U, B, M) \
149 +  CHOOSE_DYNAMIC_LINKER1 ("mbionic", "mglibc", "muclibc", "mmusl", B, G, U, M)
150 +#elif DEFAULT_LIBC == LIBC_MUSL
151 +#define CHOOSE_DYNAMIC_LINKER(G, U, B, M) \
152 +  CHOOSE_DYNAMIC_LINKER1 ("mmusl", "mglibc", "muclibc", "mbionic", M, G, U, B)
153  #else
154  #error "Unsupported DEFAULT_LIBC"
155  #endif /* DEFAULT_LIBC */
156 @@ -82,23 +87,32 @@
157  #define BIONIC_DYNAMIC_LINKER64 "/system/bin/linker64"
158  #define BIONIC_DYNAMIC_LINKERX32 "/system/bin/linkerx32"
159  
160 +/* Musl dynamic linker paths must be defined on a per-architecture
161 +   basis, for each architecture supported by Musl. However, in order
162 +   to let other architectures continue to build with other C
163 +   libraries, we provide a dummy definition of the following defines. */
164 +#define MUSL_DYNAMIC_LINKER "invalid"
165 +#define MUSL_DYNAMIC_LINKER32 "invalid"
166 +#define MUSL_DYNAMIC_LINKER64 "invalid"
167 +#define MUSL_DYNAMIC_LINKERX32 "invalid"
168 +
169  #define GNU_USER_DYNAMIC_LINKER                                                \
170    CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKER, UCLIBC_DYNAMIC_LINKER,  \
171 -                        BIONIC_DYNAMIC_LINKER)
172 +                        BIONIC_DYNAMIC_LINKER, MUSL_DYNAMIC_LINKER)
173  #define GNU_USER_DYNAMIC_LINKER32                                      \
174    CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKER32, UCLIBC_DYNAMIC_LINKER32, \
175 -                        BIONIC_DYNAMIC_LINKER32)
176 +                        BIONIC_DYNAMIC_LINKER32, MUSL_DYNAMIC_LINKER32)
177  #define GNU_USER_DYNAMIC_LINKER64                                      \
178    CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKER64, UCLIBC_DYNAMIC_LINKER64, \
179 -                        BIONIC_DYNAMIC_LINKER64)
180 +                        BIONIC_DYNAMIC_LINKER64, MUSL_DYNAMIC_LINKER64)
181  #define GNU_USER_DYNAMIC_LINKERX32                                     \
182    CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKERX32, UCLIBC_DYNAMIC_LINKERX32, \
183 -                        BIONIC_DYNAMIC_LINKERX32)
184 +                        BIONIC_DYNAMIC_LINKERX32, MUSL_DYNAMIC_LINKERX32)
185  
186  /* Determine whether the entire c99 runtime
187     is present in the runtime library.  */
188  #undef TARGET_C99_FUNCTIONS
189 -#define TARGET_C99_FUNCTIONS (OPTION_GLIBC)
190 +#define TARGET_C99_FUNCTIONS (OPTION_GLIBC || OPTION_MUSL)
191  
192  /* Whether we have sincos that follows the GNU extension.  */
193  #undef TARGET_HAS_SINCOS
194 @@ -107,3 +121,74 @@
195  /* Whether we have Bionic libc runtime */
196  #undef TARGET_HAS_BIONIC
197  #define TARGET_HAS_BIONIC (OPTION_BIONIC)
198 +
199 +/* musl avoids problematic includes by rearranging the include directories.
200 + * Unfortunately, this is mostly duplicated from cppdefault.c */
201 +#if DEFAULT_LIBC == LIBC_MUSL
202 +#define INCLUDE_DEFAULTS_MUSL_GPP                      \
203 +    { GPLUSPLUS_INCLUDE_DIR, "G++", 1, 1,              \
204 +      GPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT, 0 },          \
205 +    { GPLUSPLUS_TOOL_INCLUDE_DIR, "G++", 1, 1,         \
206 +      GPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT, 1 },          \
207 +    { GPLUSPLUS_BACKWARD_INCLUDE_DIR, "G++", 1, 1,     \
208 +      GPLUSPLUS_INCLUDE_DIR_ADD_SYSROOT, 0 },
209 +
210 +#ifdef LOCAL_INCLUDE_DIR
211 +#define INCLUDE_DEFAULTS_MUSL_LOCAL                    \
212 +    { LOCAL_INCLUDE_DIR, 0, 0, 1, 1, 2 },              \
213 +    { LOCAL_INCLUDE_DIR, 0, 0, 1, 1, 0 },
214 +#else
215 +#define INCLUDE_DEFAULTS_MUSL_LOCAL
216 +#endif
217 +
218 +#ifdef PREFIX_INCLUDE_DIR
219 +#define INCLUDE_DEFAULTS_MUSL_PREFIX                   \
220 +    { PREFIX_INCLUDE_DIR, 0, 0, 1, 0, 0},
221 +#else
222 +#define INCLUDE_DEFAULTS_MUSL_PREFIX
223 +#endif
224 +
225 +#ifdef CROSS_INCLUDE_DIR
226 +#define INCLUDE_DEFAULTS_MUSL_CROSS                    \
227 +    { CROSS_INCLUDE_DIR, "GCC", 0, 0, 0, 0},
228 +#else
229 +#define INCLUDE_DEFAULTS_MUSL_CROSS
230 +#endif
231 +
232 +#ifdef TOOL_INCLUDE_DIR
233 +#define INCLUDE_DEFAULTS_MUSL_TOOL                     \
234 +    { TOOL_INCLUDE_DIR, "BINUTILS", 0, 1, 0, 0},
235 +#else
236 +#define INCLUDE_DEFAULTS_MUSL_TOOL
237 +#endif
238 +
239 +#ifdef NATIVE_SYSTEM_HEADER_DIR
240 +#define INCLUDE_DEFAULTS_MUSL_NATIVE                   \
241 +    { NATIVE_SYSTEM_HEADER_DIR, 0, 0, 0, 1, 2 },       \
242 +    { NATIVE_SYSTEM_HEADER_DIR, 0, 0, 0, 1, 0 },
243 +#else
244 +#define INCLUDE_DEFAULTS_MUSL_NATIVE
245 +#endif
246 +
247 +#if defined (CROSS_DIRECTORY_STRUCTURE) && !defined (TARGET_SYSTEM_ROOT)
248 +# undef INCLUDE_DEFAULTS_MUSL_LOCAL
249 +# define INCLUDE_DEFAULTS_MUSL_LOCAL
250 +# undef INCLUDE_DEFAULTS_MUSL_NATIVE
251 +# define INCLUDE_DEFAULTS_MUSL_NATIVE
252 +#else
253 +# undef INCLUDE_DEFAULTS_MUSL_CROSS
254 +# define INCLUDE_DEFAULTS_MUSL_CROSS
255 +#endif
256 +
257 +#undef INCLUDE_DEFAULTS
258 +#define INCLUDE_DEFAULTS                               \
259 +  {                                                    \
260 +    INCLUDE_DEFAULTS_MUSL_GPP                          \
261 +    INCLUDE_DEFAULTS_MUSL_PREFIX                       \
262 +    INCLUDE_DEFAULTS_MUSL_CROSS                                \
263 +    INCLUDE_DEFAULTS_MUSL_TOOL                         \
264 +    INCLUDE_DEFAULTS_MUSL_NATIVE                       \
265 +    { GCC_INCLUDE_DIR, "GCC", 0, 1, 0, 0 },            \
266 +    { 0, 0, 0, 0, 0, 0 }                               \
267 +  }
268 +#endif
269 Index: b/gcc/config/linux.opt
270 ===================================================================
271 --- a/gcc/config/linux.opt
272 +++ b/gcc/config/linux.opt
273 @@ -30,3 +30,7 @@
274  muclibc
275  Target Report RejectNegative Var(linux_libc,LIBC_UCLIBC) Negative(mbionic)
276  Use uClibc C library
277 +
278 +mmusl
279 +Target Report RejectNegative Var(linux_libc,LIBC_MUSL) Negative(mglibc)
280 +Use musl C library
281 Index: b/gcc/config/microblaze/linux.h
282 ===================================================================
283 --- a/gcc/config/microblaze/linux.h
284 +++ b/gcc/config/microblaze/linux.h
285 @@ -25,7 +25,23 @@
286  #undef TLS_NEEDS_GOT
287  #define TLS_NEEDS_GOT 1
288  
289 -#define DYNAMIC_LINKER "/lib/ld.so.1"
290 +#if TARGET_BIG_ENDIAN_DEFAULT == 0 /* LE */
291 +#define MUSL_DYNAMIC_LINKER_E "%{EB:;:el}"
292 +#else
293 +#define MUSL_DYNAMIC_LINKER_E "%{EL:el}"
294 +#endif
295 +
296 +#undef MUSL_DYNAMIC_LINKER
297 +#define MUSL_DYNAMIC_LINKER "/lib/ld-musl-microblaze" MUSL_DYNAMIC_LINKER_E ".so.1"
298 +#define GLIBC_DYNAMIC_LINKER "/lib/ld.so.1"
299 +
300 +#if DEFAULT_LIBC == LIBC_MUSL
301 +#define DYNAMIC_LINKER MUSL_DYNAMIC_LINKER
302 +#else
303 +#define DYNAMIC_LINKER GLIBC_DYNAMIC_LINKER
304 +#endif
305 +
306 +
307  #undef  SUBTARGET_EXTRA_SPECS
308  #define SUBTARGET_EXTRA_SPECS \
309    { "dynamic_linker", DYNAMIC_LINKER }
310 Index: b/gcc/config/mips/linux64.h
311 ===================================================================
312 --- a/gcc/config/mips/linux64.h
313 +++ b/gcc/config/mips/linux64.h
314 @@ -29,4 +29,4 @@
315  #define BIONIC_DYNAMIC_LINKERN32 "/system/bin/linker32"
316  #define GNU_USER_DYNAMIC_LINKERN32 \
317    CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKERN32, UCLIBC_DYNAMIC_LINKERN32, \
318 -                        BIONIC_DYNAMIC_LINKERN32)
319 +                        BIONIC_DYNAMIC_LINKERN32, MUSL_DYNAMIC_LINKER)
320 Index: b/gcc/config/mips/linux.h
321 ===================================================================
322 --- a/gcc/config/mips/linux.h
323 +++ b/gcc/config/mips/linux.h
324 @@ -18,3 +18,11 @@
325  <http://www.gnu.org/licenses/>.  */
326  
327  #define GLIBC_DYNAMIC_LINKER "/lib/ld.so.1"
328 +
329 +#if TARGET_ENDIAN_DEFAULT == 0 /* LE */
330 +#define MUSL_DYNAMIC_LINKER_E "%{EB:;:el}"
331 +#else
332 +#define MUSL_DYNAMIC_LINKER_E "%{EL:el}"
333 +#endif
334 +#undef MUSL_DYNAMIC_LINKER
335 +#define MUSL_DYNAMIC_LINKER "/lib/ld-musl-mips" MUSL_DYNAMIC_LINKER_E ".so.1"
336 Index: b/gcc/config/rs6000/linux64.h
337 ===================================================================
338 --- a/gcc/config/rs6000/linux64.h
339 +++ b/gcc/config/rs6000/linux64.h
340 @@ -374,17 +374,23 @@
341  #endif
342  #define UCLIBC_DYNAMIC_LINKER32 "/lib/ld-uClibc.so.0"
343  #define UCLIBC_DYNAMIC_LINKER64 "/lib/ld64-uClibc.so.0"
344 +#undef MUSL_DYNAMIC_LINKER32
345 +#define MUSL_DYNAMIC_LINKER32 "/lib/ld-musl-powerpc.so.1"
346 +#undef MUSL_DYNAMIC_LINKER64
347 +#define MUSL_DYNAMIC_LINKER64 "/lib/ld-musl-powerpc64.so.1"
348  #if DEFAULT_LIBC == LIBC_UCLIBC
349 -#define CHOOSE_DYNAMIC_LINKER(G, U) "%{mglibc:" G ";:" U "}"
350 +#define CHOOSE_DYNAMIC_LINKER(G, U, M) "%{mglibc:" G ";:%{mmusl:" M ";:" U "}}"
351  #elif DEFAULT_LIBC == LIBC_GLIBC
352 -#define CHOOSE_DYNAMIC_LINKER(G, U) "%{muclibc:" U ";:" G "}"
353 +#define CHOOSE_DYNAMIC_LINKER(G, U, M) "%{muclibc:" U ";:%{mmusl:" M ";:" G "}}"
354 +#elif DEFAULT_LIBC == LIBC_MUSL
355 +#define CHOOSE_DYNAMIC_LINKER(G, U, M) "%{mglibc:" G ";:%{muclibc:" U ";:" M "}}"
356  #else
357  #error "Unsupported DEFAULT_LIBC"
358  #endif
359  #define GNU_USER_DYNAMIC_LINKER32 \
360 -  CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKER32, UCLIBC_DYNAMIC_LINKER32)
361 +  CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKER32, UCLIBC_DYNAMIC_LINKER32, MUSL_DYNAMIC_LINKER32)
362  #define GNU_USER_DYNAMIC_LINKER64 \
363 -  CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKER64, UCLIBC_DYNAMIC_LINKER64)
364 +  CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKER64, UCLIBC_DYNAMIC_LINKER64, MUSL_DYNAMIC_LINKER64)
365  
366  #undef  DEFAULT_ASM_ENDIAN
367  #if (TARGET_DEFAULT & MASK_LITTLE_ENDIAN)
368 Index: b/gcc/config/rs6000/secureplt.h
369 ===================================================================
370 --- a/gcc/config/rs6000/secureplt.h
371 +++ b/gcc/config/rs6000/secureplt.h
372 @@ -18,3 +18,4 @@
373  <http://www.gnu.org/licenses/>.  */
374  
375  #define CC1_SECURE_PLT_DEFAULT_SPEC "-msecure-plt"
376 +#define LINK_SECURE_PLT_DEFAULT_SPEC "--secure-plt"
377 Index: b/gcc/config/rs6000/sysv4.h
378 ===================================================================
379 --- a/gcc/config/rs6000/sysv4.h
380 +++ b/gcc/config/rs6000/sysv4.h
381 @@ -537,6 +537,9 @@
382  #ifndef CC1_SECURE_PLT_DEFAULT_SPEC
383  #define CC1_SECURE_PLT_DEFAULT_SPEC ""
384  #endif
385 +#ifndef LINK_SECURE_PLT_DEFAULT_SPEC
386 +#define LINK_SECURE_PLT_DEFAULT_SPEC ""
387 +#endif
388  
389  /* Pass -G xxx to the compiler.  */
390  #define        CC1_SPEC "%{G*} %(cc1_cpu)" \
391 @@ -585,7 +588,8 @@
392  
393  /* Override the default target of the linker.  */
394  #define        LINK_TARGET_SPEC \
395 -  ENDIAN_SELECT("", " --oformat elf32-powerpcle", "")
396 +  ENDIAN_SELECT("", " --oformat elf32-powerpcle", "") \
397 +  "%{!mbss-plt: %{!msecure-plt: %(link_secure_plt_default)}}"
398  
399  /* Any specific OS flags.  */
400  #define LINK_OS_SPEC "\
401 @@ -763,15 +767,18 @@
402  
403  #define GLIBC_DYNAMIC_LINKER "/lib/ld.so.1"
404  #define UCLIBC_DYNAMIC_LINKER "/lib/ld-uClibc.so.0"
405 +#define MUSL_DYNAMIC_LINKER "/lib/ld-musl-powerpc.so.1"
406  #if DEFAULT_LIBC == LIBC_UCLIBC
407 -#define CHOOSE_DYNAMIC_LINKER(G, U) "%{mglibc:" G ";:" U "}"
408 +#define CHOOSE_DYNAMIC_LINKER(G, U, M) "%{mglibc:" G ";:%{mmusl:" M ";:" U "}}"
409 +#elif DEFAULT_LIBC == LIBC_MUSL
410 +#define CHOOSE_DYNAMIC_LINKER(G, U, M) "%{mglibc:" G ";:%{muclibc:" U ";:" M "}}"
411  #elif !defined (DEFAULT_LIBC) || DEFAULT_LIBC == LIBC_GLIBC
412 -#define CHOOSE_DYNAMIC_LINKER(G, U) "%{muclibc:" U ";:" G "}"
413 +#define CHOOSE_DYNAMIC_LINKER(G, U, M) "%{muclibc:" U ";:%{mmusl:" M ";:" G "}}"
414  #else
415  #error "Unsupported DEFAULT_LIBC"
416  #endif
417  #define GNU_USER_DYNAMIC_LINKER \
418 -  CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKER, UCLIBC_DYNAMIC_LINKER)
419 +  CHOOSE_DYNAMIC_LINKER (GLIBC_DYNAMIC_LINKER, UCLIBC_DYNAMIC_LINKER, MUSL_DYNAMIC_LINKER)
420  
421  #define LINK_OS_LINUX_SPEC "-m elf32ppclinux %{!shared: %{!static: \
422    %{rdynamic:-export-dynamic} \
423 @@ -894,6 +901,7 @@
424    { "link_os_openbsd",         LINK_OS_OPENBSD_SPEC },                 \
425    { "link_os_default",         LINK_OS_DEFAULT_SPEC },                 \
426    { "cc1_secure_plt_default",  CC1_SECURE_PLT_DEFAULT_SPEC },          \
427 +  { "link_secure_plt_default", LINK_SECURE_PLT_DEFAULT_SPEC },         \
428    { "cpp_os_ads",              CPP_OS_ADS_SPEC },                      \
429    { "cpp_os_yellowknife",      CPP_OS_YELLOWKNIFE_SPEC },              \
430    { "cpp_os_mvme",             CPP_OS_MVME_SPEC },                     \
431 Index: b/gcc/config/sh/linux.h
432 ===================================================================
433 --- a/gcc/config/sh/linux.h
434 +++ b/gcc/config/sh/linux.h
435 @@ -43,7 +43,15 @@
436  
437  #define TARGET_ASM_FILE_END file_end_indicate_exec_stack
438  
439 +#if TARGET_BIG_ENDIAN_DEFAULT /* BE */
440 +#define MUSL_DYNAMIC_LINKER_E "eb"
441 +#else
442 +#define MUSL_DYNAMIC_LINKER_E
443 +#endif
444 +
445  #define GLIBC_DYNAMIC_LINKER "/lib/ld-linux.so.2"
446 +#undef MUSL_DYNAMIC_LINKER
447 +#define MUSL_DYNAMIC_LINKER "/lib/ld-musl-sh" MUSL_DYNAMIC_LINKER_E ".so.1"
448  
449  #undef SUBTARGET_LINK_EMUL_SUFFIX
450  #define SUBTARGET_LINK_EMUL_SUFFIX "_linux"
451 Index: b/gcc/config.gcc
452 ===================================================================
453 --- a/gcc/config.gcc
454 +++ b/gcc/config.gcc
455 @@ -550,7 +550,7 @@
456  esac
457  
458  # Common C libraries.
459 -tm_defines="$tm_defines LIBC_GLIBC=1 LIBC_UCLIBC=2 LIBC_BIONIC=3"
460 +tm_defines="$tm_defines LIBC_GLIBC=1 LIBC_UCLIBC=2 LIBC_BIONIC=3 LIBC_MUSL=4"
461  
462  # Common parts for widely ported systems.
463  case ${target} in
464 @@ -653,6 +653,9 @@
465      *-*-*uclibc*)
466        tm_defines="$tm_defines DEFAULT_LIBC=LIBC_UCLIBC"
467        ;;
468 +    *-*-*musl*)
469 +      tm_defines="$tm_defines DEFAULT_LIBC=LIBC_MUSL"
470 +      ;;
471      *)
472        tm_defines="$tm_defines DEFAULT_LIBC=LIBC_GLIBC"
473        ;;
474 @@ -2135,6 +2138,10 @@
475             powerpc*-*-linux*paired*)
476                 tm_file="${tm_file} rs6000/750cl.h" ;;
477         esac
478 +        case ${target} in
479 +            *-linux*-musl*)
480 +                enable_secureplt=yes ;;
481 +        esac
482         if test x${enable_secureplt} = xyes; then
483                 tm_file="rs6000/secureplt.h ${tm_file}"
484         fi
485 Index: b/gcc/configure
486 ===================================================================
487 --- a/gcc/configure
488 +++ b/gcc/configure
489 @@ -26821,6 +26821,9 @@
490  else
491    gcc_cv_libc_provides_ssp=no
492      case "$target" in
493 +       *-*-musl*)
494 +         # All versions of musl provide stack protector
495 +        gcc_cv_libc_provides_ssp=yes;;
496         *-*-linux* | *-*-kfreebsd*-gnu | *-*-knetbsd*-gnu)
497        # glibc 2.4 and later provides __stack_chk_fail and
498        # either __stack_chk_guard, or TLS access to stack guard canary.
499 @@ -26854,6 +26857,7 @@
500          # <http://gcc.gnu.org/ml/gcc/2008-10/msg00130.html>) and for now
501          # simply assert that glibc does provide this, which is true for all
502          # realistically usable GNU/Hurd configurations.
503 +        # All supported versions of musl provide it as well
504          gcc_cv_libc_provides_ssp=yes;;
505         *-*-darwin* | *-*-freebsd*)
506          ac_fn_c_check_func "$LINENO" "__stack_chk_fail" "ac_cv_func___stack_chk_fail"
507 @@ -26936,6 +26940,9 @@
508        gcc_cv_target_dl_iterate_phdr=no
509      fi
510      ;;
511 +  *-linux-musl*)
512 +    gcc_cv_target_dl_iterate_phdr=yes
513 +    ;;
514  esac
515  
516  if test x$gcc_cv_target_dl_iterate_phdr = xyes; then
517 Index: b/gcc/configure.ac
518 ===================================================================
519 --- a/gcc/configure.ac
520 +++ b/gcc/configure.ac
521 @@ -4750,6 +4750,9 @@
522        gcc_cv_libc_provides_ssp,
523        [gcc_cv_libc_provides_ssp=no
524      case "$target" in
525 +       *-*-musl*)
526 +         # All versions of musl provide stack protector
527 +        gcc_cv_libc_provides_ssp=yes;;
528         *-*-linux* | *-*-kfreebsd*-gnu | *-*-knetbsd*-gnu)
529        [# glibc 2.4 and later provides __stack_chk_fail and
530        # either __stack_chk_guard, or TLS access to stack guard canary.
531 @@ -4783,6 +4786,7 @@
532          # <http://gcc.gnu.org/ml/gcc/2008-10/msg00130.html>) and for now
533          # simply assert that glibc does provide this, which is true for all
534          # realistically usable GNU/Hurd configurations.
535 +        # All supported versions of musl provide it as well
536          gcc_cv_libc_provides_ssp=yes;;
537         *-*-darwin* | *-*-freebsd*)
538          AC_CHECK_FUNC(__stack_chk_fail,[gcc_cv_libc_provides_ssp=yes],
539 @@ -4848,6 +4852,9 @@
540        gcc_cv_target_dl_iterate_phdr=no
541      fi
542      ;;
543 +  *-linux-musl*)
544 +    gcc_cv_target_dl_iterate_phdr=yes
545 +    ;;
546  esac
547  GCC_TARGET_TEMPLATE([TARGET_DL_ITERATE_PHDR])
548  if test x$gcc_cv_target_dl_iterate_phdr = xyes; then
549 Index: b/gcc/ginclude/stddef.h
550 ===================================================================
551 --- a/gcc/ginclude/stddef.h
552 +++ b/gcc/ginclude/stddef.h
553 @@ -181,6 +181,7 @@
554  #ifndef _GCC_SIZE_T
555  #ifndef _SIZET_
556  #ifndef __size_t
557 +#ifndef __DEFINED_size_t /* musl */
558  #define __size_t__     /* BeOS */
559  #define __SIZE_T__     /* Cray Unicos/Mk */
560  #define _SIZE_T
561 @@ -197,6 +198,7 @@
562  #define ___int_size_t_h
563  #define _GCC_SIZE_T
564  #define _SIZET_
565 +#define __DEFINED_size_t /* musl */
566  #if (defined (__FreeBSD__) && (__FreeBSD__ >= 5)) \
567    || defined(__FreeBSD_kernel__)
568  /* __size_t is a typedef on FreeBSD 5, must not trash it. */
569 @@ -214,6 +216,7 @@
570  typedef long ssize_t;
571  #endif /* __BEOS__ */
572  #endif /* !(defined (__GNUG__) && defined (size_t)) */
573 +#endif /* __DEFINED_size_t */
574  #endif /* __size_t */
575  #endif /* _SIZET_ */
576  #endif /* _GCC_SIZE_T */
577 Index: b/libgcc/unwind-dw2-fde-dip.c
578 ===================================================================
579 --- a/libgcc/unwind-dw2-fde-dip.c
580 +++ b/libgcc/unwind-dw2-fde-dip.c
581 @@ -75,6 +75,13 @@
582  # define USE_PT_GNU_EH_FRAME
583  #endif
584  
585 +/* For musl libc, TARGET_DL_ITERATE_PHDR gets defined by the configure
586 +   script. */
587 +#if !defined(inhibit_libc) && defined(HAVE_LD_EH_FRAME_HDR) \
588 +    && defined(TARGET_DL_ITERATE_PHDR)
589 +# define USE_PT_GNU_EH_FRAME
590 +#endif
591 +
592  #if defined(USE_PT_GNU_EH_FRAME)
593  
594  #include <link.h>
595 Index: b/libgomp/config/posix/time.c
596 ===================================================================
597 --- a/libgomp/config/posix/time.c
598 +++ b/libgomp/config/posix/time.c
599 @@ -28,6 +28,8 @@
600     The following implementation uses the most simple POSIX routines.
601     If present, POSIX 4 clocks should be used instead.  */
602  
603 +#define _POSIX_C_SOURCE 199309L /* for clocks */
604 +
605  #include "libgomp.h"
606  #include <unistd.h>
607  #if TIME_WITH_SYS_TIME
608 Index: b/libitm/config/arm/hwcap.cc
609 ===================================================================
610 --- a/libitm/config/arm/hwcap.cc
611 +++ b/libitm/config/arm/hwcap.cc
612 @@ -40,7 +40,11 @@
613  
614  #ifdef __linux__
615  #include <unistd.h>
616 +#ifdef __GLIBC__
617  #include <sys/fcntl.h>
618 +#else
619 +#include <fcntl.h>
620 +#endif
621  #include <elf.h>
622  
623  static void __attribute__((constructor))
624 Index: b/libitm/config/linux/x86/tls.h
625 ===================================================================
626 --- a/libitm/config/linux/x86/tls.h
627 +++ b/libitm/config/linux/x86/tls.h
628 @@ -25,16 +25,19 @@
629  #ifndef LIBITM_X86_TLS_H
630  #define LIBITM_X86_TLS_H 1
631  
632 -#if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 10)
633 +#if defined(__GLIBC_PREREQ)
634 +#if __GLIBC_PREREQ(2, 10)
635  /* Use slots in the TCB head rather than __thread lookups.
636     GLIBC has reserved words 10 through 13 for TM.  */
637  #define HAVE_ARCH_GTM_THREAD 1
638  #define HAVE_ARCH_GTM_THREAD_DISP 1
639  #endif
640 +#endif
641  
642  #include "config/generic/tls.h"
643  
644 -#if defined(__GLIBC_PREREQ) && __GLIBC_PREREQ(2, 10)
645 +#if defined(__GLIBC_PREREQ)
646 +#if __GLIBC_PREREQ(2, 10)
647  namespace GTM HIDDEN {
648  
649  #ifdef __x86_64__
650 @@ -101,5 +104,6 @@
651  
652  } // namespace GTM
653  #endif /* >= GLIBC 2.10 */
654 +#endif
655  
656  #endif // LIBITM_X86_TLS_H
657 Index: b/libstdc++-v3/configure.host
658 ===================================================================
659 --- a/libstdc++-v3/configure.host
660 +++ b/libstdc++-v3/configure.host
661 @@ -264,6 +264,13 @@
662      os_include_dir="os/bsd/freebsd"
663      ;;
664    gnu* | linux* | kfreebsd*-gnu | knetbsd*-gnu)
665 +    # check for musl by target
666 +    case "${host_os}" in
667 +      *-musl*)
668 +        os_include_dir="os/generic"
669 +        ;;
670 +      *)
671 +
672      if [ "$uclibc" = "yes" ]; then
673        os_include_dir="os/uclibc"
674      elif [ "$bionic" = "yes" ]; then
675 @@ -272,6 +279,9 @@
676        os_include_dir="os/gnu-linux"
677      fi
678      ;;
679 +
680 +    esac
681 +    ;;
682    hpux*)
683      os_include_dir="os/hpux"
684      ;;