1 From bafa38541e911806b74a1ab094a404bbdd692ade Mon Sep 17 00:00:00 2001
2 From: Steven Barth <steven@midlink.org>
3 Date: Sat, 20 Jun 2015 16:59:48 +0200
4 Subject: [PATCH] commit 55d061f031085f24d138664c897791aebe9a2fab Author: Rich
5 Felker <dalias@aerifal.cx> Date: Sat Jun 20 03:01:07 2015 +0000
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
10 provide __stack_chk_fail_local in libc.a
12 this symbol is needed only on archs where the PLT call ABI is klunky,
13 and only for position-independent code compiled with stack protector.
14 thus references usually only appear in shared libraries or PIE
15 executables, but they can also appear when linking statically if some
16 of the object files being linked were built as PIC/PIE.
18 normally libssp_nonshared.a from the compiler toolchain should provide
19 __stack_chk_fail_local, but reportedly it appears prior to -lc in the
20 link order, thus failing to satisfy references from libc itself (which
21 arise only if libc.a was built as PIC/PIE with stack protector
24 commit ce3688eca920aa77549323f84e21f33522397115
25 Author: Rich Felker <dalias@aerifal.cx>
26 Date: Sat Jun 20 02:54:30 2015 +0000
28 work around mips detached thread exit breakage due to kernel regression
30 linux kernel commit 46e12c07b3b9603c60fc1d421ff18618241cb081 caused
31 the mips syscall mechanism to fail with EFAULT when the userspace
32 stack pointer is invalid, breaking __unmapself used for detached
33 thread exit. the workaround is to set $sp to a known-valid, readable
34 address, and the simplest one to obtain is the address of the current
35 function, which is available (per o32 calling convention) in $25.
37 commit 75eceb3ae824d54e865686c0c538551aeebf3372
38 Author: Rich Felker <dalias@aerifal.cx>
39 Date: Wed Jun 17 17:21:46 2015 +0000
41 ignore ENOSYS error from mprotect in pthread_create and dynamic linker
43 this error simply indicated a system without memory protection (NOMMU)
44 and should not cause failure in the caller.
46 commit 10d0268ccfab9152250eeeed3952ce3fed44131a
47 Author: Rich Felker <dalias@aerifal.cx>
48 Date: Tue Jun 16 15:25:02 2015 +0000
50 switch to using trap number 31 for syscalls on sh
52 nominally the low bits of the trap number on sh are the number of
53 syscall arguments, but they have never been used by the kernel, and
54 some code making syscalls does not even know the number of arguments
55 and needs to pass an arbitrary high number anyway.
57 sh3/sh4 traditionally used the trap range 16-31 for syscalls, but part
58 of this range overlapped with hardware exceptions/interrupts on sh2
59 hardware, so an incompatible range 32-47 was chosen for sh2.
61 using trap number 31 everywhere, since it's in the existing sh3/sh4
62 range and does not conflict with sh2 hardware, is a proposed
63 unification of the kernel syscall convention that will allow binaries
64 to be shared between sh2 and sh3/sh4. if this is not accepted into the
65 kernel, we can refit the sh2 target with runtime selection mechanisms
66 for the trap number, but doing so would be invasive and would entail
69 commit 3366a99b17847b58f2d8cc52cbb5d65deb824f8a
70 Author: Rich Felker <dalias@aerifal.cx>
71 Date: Tue Jun 16 14:55:06 2015 +0000
73 switch sh port's __unmapself to generic version when running on sh2/nommu
75 due to the way the interrupt and syscall trap mechanism works,
76 userspace on sh2 must never set the stack pointer to an invalid value.
77 thus, the approach used on most archs, where __unmapself executes with
78 no stack for the interval between SYS_munmap and SYS_exit, is not
81 in order not to pessimize sh3/sh4, the sh asm version of __unmapself
82 is not removed. instead it's renamed and redirected through code that
83 calls either the generic (safe) __unmapself or the sh3/sh4 asm,
84 depending on compile-time and run-time conditions.
86 commit f9d84554bae0fa17c9a1d724549c4408022228a5
87 Author: Rich Felker <dalias@aerifal.cx>
88 Date: Tue Jun 16 14:28:30 2015 +0000
90 add support for sh2 interrupt-masking-based atomics to sh port
92 the sh2 target is being considered an ISA subset of sh3/sh4, in the
93 sense that binaries built for sh2 are intended to be usable on later
94 cpu models/kernels with mmu support. so rather than hard-coding
95 sh2-specific atomics, the runtime atomic selection mechanisms that was
96 already in place has been extended to add sh2 atomics.
98 at this time, the sh2 atomics are not SMP-compatible; since the ISA
99 lacks actual atomic operations, the new code instead masks interrupts
100 for the duration of the atomic operation, producing an atomic result
101 on single-core. this is only possible because the kernel/hardware does
102 not impose protections against userspace doing so. additional changes
103 will be needed to support future SMP systems.
105 care has been taken to avoid producing significant additional code
106 size in the case where it's known at compile-time that the target is
107 not sh2 and does not need sh2-specific code.
109 commit 1b0cdc8700d29ef018bf226d74b2b58b23bce91c
110 Author: Rich Felker <dalias@aerifal.cx>
111 Date: Tue Jun 16 07:11:19 2015 +0000
113 refactor stdio open file list handling, move it out of global libc struct
115 functions which open in-memory FILE stream variants all shared a tail
116 with __fdopen, adding the FILE structure to stdio's open file list.
117 replacing this common tail with a function call reduces code size and
118 duplication of logic. the list is also partially encapsulated now.
120 function signatures were chosen to facilitate tail call optimization
121 and reduce the need for additional accessor functions.
123 with these changes, static linked programs that do not use stdio no
124 longer have an open file list at all.
126 commit f22a9edaf8a6f2ca1d314d18b3785558279a5c03
127 Author: Rich Felker <dalias@aerifal.cx>
128 Date: Tue Jun 16 06:18:00 2015 +0000
130 byte-based C locale, phase 3: make MB_CUR_MAX variable to activate code
132 this patch activates the new byte-based C locale (high bytes treated
133 as abstract code unit "characters" rather than decoded as multibyte
134 characters) by making the value of MB_CUR_MAX depend on the active
135 locale. for the C locale, the LC_CTYPE category pointer is null,
136 yielding a value of 1. all other locales yield a value of 4.
138 commit 16f18d036d9a7bf590ee6eb86785c0a9658220b6
139 Author: Rich Felker <dalias@aerifal.cx>
140 Date: Tue Jun 16 05:35:31 2015 +0000
142 byte-based C locale, phase 2: stdio and iconv (multibyte callers)
144 this patch adjusts libc components which use the multibyte functions
145 internally, and which depend on them operating in a particular
146 encoding, to make the appropriate locale changes before calling them
147 and restore the calling thread's locale afterwards. activating the
148 byte-based C locale without these changes would cause regressions in
151 in the case of iconv, the current implementation was simply using the
152 multibyte functions as UTF-8 conversions. setting a multibyte UTF-8
153 locale for the duration of the iconv operation allows the code to
156 in the case of stdio, POSIX requires that FILE streams have an
157 encoding rule bound at the time of setting wide orientation. as long
158 as all locales, including the C locale, used the same encoding,
159 treating high bytes as UTF-8, there was no need to store an encoding
160 rule as part of the stream's state.
162 a new locale field in the FILE structure points to the locale that
163 should be made active during fgetwc/fputwc/ungetwc on the stream. it
164 cannot point to the locale active at the time the stream becomes
165 oriented, because this locale could be mutable (the global locale) or
166 could be destroyed (locale_t objects produced by newlocale) before the
167 stream is closed. instead, a pointer to the static C or C.UTF-8 locale
168 object added in commit commit aeeac9ca5490d7d90fe061ab72da446c01ddf746
169 is used. this is valid since categories other than LC_CTYPE will not
170 affect these functions.
172 commit 1507ebf837334e9e07cfab1ca1c2e88449069a80
173 Author: Rich Felker <dalias@aerifal.cx>
174 Date: Tue Jun 16 04:44:17 2015 +0000
176 byte-based C locale, phase 1: multibyte character handling functions
178 this patch makes the functions which work directly on multibyte
179 characters treat the high bytes as individual abstract code units
180 rather than as multibyte sequences when MB_CUR_MAX is 1. since
181 MB_CUR_MAX is presently defined as a constant 4, all of the new code
182 added is dead code, and optimizing compilers' code generation should
183 not be affected at all. a future commit will activate the new code.
185 as abstract code units, bytes 0x80 to 0xff are represented by wchar_t
186 values 0xdf80 to 0xdfff, at the end of the surrogates range. this
187 ensures that they will never be misinterpreted as Unicode characters,
188 and that all wctype functions return false for these "characters"
189 without needing locale-specific logic. a high range outside of Unicode
190 such as 0x7fffff80 to 0x7fffffff was also considered, but since C11's
191 char16_t also needs to be able to represent conversions of these
192 bytes, the surrogate range was the natural choice.
194 commit 38e2f727237230300fea6aff68802db04625fd23
195 Author: Rich Felker <dalias@aerifal.cx>
196 Date: Tue Jun 16 04:21:38 2015 +0000
198 fix btowc corner case
200 btowc is required to interpret its argument by conversion to unsigned
201 char, unless the argument is equal to EOF. since the conversion to
202 produces a non-character value anyway, we can just unconditionally
205 commit ee59c296d56bf26f49f354d6eb32b4b6d4190188
206 Author: Szabolcs Nagy <nsz@port70.net>
207 Date: Wed Jun 3 10:32:14 2015 +0100
209 arm: add vdso support
211 vdso will be available on arm in linux v4.2, the user-space code
212 for it is in kernel commit 8512287a8165592466cb9cb347ba94892e9c56a5
214 commit e3bc22f1eff87b8f029a6ab31f1a269d69e4b053
215 Author: Rich Felker <dalias@aerifal.cx>
216 Date: Sun Jun 14 01:59:02 2015 +0000
218 refactor malloc's expand_heap to share with __simple_malloc
220 this extends the brk/stack collision protection added to full malloc
221 in commit 276904c2f6bde3a31a24ebfa201482601d18b4f9 to also protect the
222 __simple_malloc function used in static-linked programs that don't
223 reference the free function.
225 it also extends support for using mmap when brk fails, which full
226 malloc got in commit 5446303328adf4b4e36d9fba21848e6feb55fab4, to
229 since __simple_malloc may expand the heap by arbitrarily large
230 increments, the stack collision detection is enhanced to detect
231 interval overlap rather than just proximity of a single address to the
232 stack. code size is increased a bit, but this is partly offset by the
233 sharing of code between the two malloc implementations, which due to
234 linking semantics, both get linked in a program that needs the full
235 malloc with realloc/free support.
237 commit 4ef9b828c1f39553a69e0635ac91f0fcadd6e8c6
238 Author: Rich Felker <dalias@aerifal.cx>
239 Date: Sat Jun 13 20:53:02 2015 +0000
241 remove cancellation points in stdio
243 commit 58165923890865a6ac042fafce13f440ee986fd9 added these optional
244 cancellation points on the basis that cancellable stdio could be
245 useful, to unblock threads stuck on stdio operations that will never
246 complete. however, the only way to ensure that cancellation can
247 achieve this is to violate the rules for side effects when
248 cancellation is acted upon, discarding knowledge of any partial data
249 transfer already completed. our implementation exhibited this behavior
250 and was thus non-conforming.
252 in addition to improving correctness, removing these cancellation
253 points moderately reduces code size, and should significantly improve
254 performance on i386, where sysenter/syscall instructions can be used
255 instead of "int $128" for non-cancellable syscalls.
257 commit 536c6d5a4205e2a3f161f2983ce1e0ac3082187d
258 Author: Rich Felker <dalias@aerifal.cx>
259 Date: Sat Jun 13 05:17:16 2015 +0000
261 fix idiom for setting stdio stream orientation to wide
263 the old idiom, f->mode |= f->mode+1, was adapted from the idiom for
264 setting byte orientation, f->mode |= f->mode-1, but the adaptation was
265 incorrect. unless the stream was alreasdy set byte-oriented, this code
266 incremented f->mode each time it was executed, which would eventually
267 lead to overflow. it could be fixed by changing it to f->mode |= 1,
268 but upcoming changes will require slightly more work at the time of
269 wide orientation, so it makes sense to just call fwide. as an
270 optimization in the single-character functions, fwide is only called
271 if the stream is not already wide-oriented.
273 commit f8f565df467c13248104223f99abf7f37cef7584
274 Author: Rich Felker <dalias@aerifal.cx>
275 Date: Sat Jun 13 04:42:38 2015 +0000
277 add printing of null %s arguments as "(null)" in wide printf
279 this is undefined, but supported in our implementation of the normal
280 printf, so for consistency the wide variant should support it too.
282 commit f9e25d813860d53cd1e9b6145cc63375d2fe2529
283 Author: Rich Felker <dalias@aerifal.cx>
284 Date: Sat Jun 13 04:37:27 2015 +0000
286 add %m support to wide printf
288 commit ec634aad91f57479ef17525e33ed446c780a61f4
289 Author: Rich Felker <dalias@aerifal.cx>
290 Date: Thu Jun 11 05:01:04 2015 +0000
294 commit c30cbcb0a646b1f13a22c645616dce624465b883
295 Author: Rich Felker <dalias@aerifal.cx>
296 Date: Wed Jun 10 02:27:40 2015 +0000
298 implement arch-generic version of __unmapself
300 this can be used to put off writing an asm version of __unmapself for
301 new archs, or as a permanent solution on archs where it's not
302 practical or even possible to run momentarily with no stack.
304 the concept here is simple: the caller takes a lock on a global shared
305 stack and uses it to make the munmap and exit syscalls. the only trick
306 is unlocking, which must be done after the thread exits, and this is
307 achieved by using the set_tid_address syscall to have the kernel zero
308 and futex-wake the lock word as part of the exit syscall.
310 commit 276904c2f6bde3a31a24ebfa201482601d18b4f9
311 Author: Rich Felker <dalias@aerifal.cx>
312 Date: Tue Jun 9 20:30:35 2015 +0000
314 in malloc, refuse to use brk if it grows into stack
316 the linux/nommu fdpic ELF loader sets up the brk range to overlap
317 entirely with the main thread's stack (but growing from opposite
318 ends), so that the resulting failure mode for malloc is not to return
319 a null pointer but to start returning pointers to memory that overlaps
320 with the caller's stack. needless to say this extremely dangerous and
323 since it's non-trivial to detect execution environments that might be
324 affected by this kernel bug, and since the severity of the bug makes
325 any sort of detection that might yield false-negatives unsafe, we
326 instead check the proximity of the brk to the stack pointer each time
327 the brk is to be expanded. both the main thread's stack (where the
328 real known risk lies) and the calling thread's stack are checked. an
329 arbitrary gap distance of 8 MB is imposed, chosen to be larger than
330 linux default main-thread stack reservation sizes and larger than any
331 reasonable stack configuration on nommu.
333 the effeciveness of this patch relies on an assumption that the amount
334 by which the brk is being grown is smaller than the gap limit, which
335 is always true for malloc's use of brk. reliance on this assumption is
336 why the check is being done in malloc-specific code and not in __brk.
338 commit bd1eaceaa3975bd2a2a34e211cff896affaecadf
339 Author: Rich Felker <dalias@aerifal.cx>
340 Date: Tue Jun 9 20:09:27 2015 +0000
342 fix spurious errors from pwd/grp functions when nscd backend is absent
344 for several pwd/grp functions, the only way the caller can distinguish
345 between a successful negative result ("no such user/group") and an
346 internal error is by clearing errno before the call and checking errno
347 afterwards. the nscd backend support code correctly simulated a
348 not-found response on systems where such a backend is not running, but
349 failed to restore errno.
351 this commit also fixed an outdated/incorrect comment.
353 commit 75ce4503950621b11fcc7f1fd1187dbcf3cde312
354 Author: Rich Felker <dalias@aerifal.cx>
355 Date: Sun Jun 7 20:55:23 2015 +0000
357 fix regression in pre-v7 arm on kernels with kuser helper removed
359 the arm atomics/TLS runtime selection code is called from
360 __set_thread_area and depends on having libc.auxv and __hwcap
361 available. commit 71f099cb7db821c51d8f39dfac622c61e54d794c moved the
362 first call to __set_thread_area to the top of dynamic linking stage 3,
363 before this data is made available, causing the runtime detection code
364 to always see __hwcap as zero and thereby select the atomics/TLS
365 implementations based on kuser helper.
367 upcoming work on superh will use similar runtime detection.
369 ideally this early-init code should be cleanly refactored and shared
370 between the dynamic linker and static-linked startup.
372 commit 32f3c4f70633488550c29a2444f819aafdf345ff
373 Author: Rich Felker <dalias@aerifal.cx>
374 Date: Sun Jun 7 03:09:16 2015 +0000
376 add multiple inclusion guard to locale_impl.h
378 commit 04b8360adbb6487f61aa0c00e53ec3a90a5a0d29
379 Author: Rich Felker <dalias@aerifal.cx>
380 Date: Sun Jun 7 02:59:49 2015 +0000
382 remove redefinition of MB_CUR_MAX in locale_impl.h
384 unless/until the byte-based C locale is implemented, defining
385 MB_CUR_MAX to 1 in the C locale is wrong. no internal code currently
386 uses the MB_CUR_MAX macro, but having it defined inconsistently is
387 error-prone. applications get the value from stdlib.h and were
390 commit 16bf466532d7328e971012b0731ad493b017ad29
391 Author: Rich Felker <dalias@aerifal.cx>
392 Date: Sat Jun 6 18:53:02 2015 +0000
394 make static C and C.UTF-8 locales available outside of newlocale
396 commit 312eea2ea4f4363fb01b73660c08bfcf43dd3bb4
397 Author: Rich Felker <dalias@aerifal.cx>
398 Date: Sat Jun 6 18:20:30 2015 +0000
400 remove another invalid skip of locking in ungetwc
402 commit 3d7e32d28dc9962e9efc1c317c5b44b5b2df3008
403 Author: Rich Felker <dalias@aerifal.cx>
404 Date: Sat Jun 6 18:16:22 2015 +0000
406 add macro version of ctype.h isascii function
408 presumably internal code (ungetwc and fputwc) was written assuming a
409 macro implementation existed; otherwise use of isascii is just a
412 commit 7e816a6487932cbb3cb71d94b609e50e81f4e5bf
413 Author: Rich Felker <dalias@aerifal.cx>
414 Date: Sat Jun 6 18:11:17 2015 +0000
416 remove invalid skip of locking in ungetwc
418 aside from being invalid, the early check only optimized the error
419 case, and likely pessimized the common case by separating the
420 two branches on isascii(c) at opposite ends of the function.
422 commit 63f4b9f18f3674124d8bcb119739fec85e6da005
423 Author: Timo Teräs <timo.teras@iki.fi>
424 Date: Fri Jun 5 10:39:42 2015 +0300
426 fix uselocale((locale_t)0) not to modify locale
428 commit 68630b55c0c7219fe9df70dc28ffbf9efc8021d8 made the new locale to
429 be assigned unconditonally resulting in crashes later on.
431 arch/arm/syscall_arch.h | 4 ++
432 arch/sh/src/__set_thread_area.c | 34 ++++++++++++++++
433 arch/sh/src/__unmapself.c | 19 +++++++++
434 arch/sh/src/atomic.c | 72 ++++++++++++++++++++++++++++++----
435 arch/sh/src/sh_atomic.h | 15 +++++++
436 arch/sh/syscall_arch.h | 2 +-
437 include/ctype.h | 1 +
438 include/stdlib.h | 3 +-
439 src/ctype/__ctype_get_mb_cur_max.c | 5 ++-
440 src/ctype/isascii.c | 1 +
441 src/env/__stack_chk_fail.c | 4 ++
442 src/internal/libc.h | 2 -
443 src/internal/locale_impl.h | 12 ++++++
444 src/internal/sh/syscall.s | 2 +-
445 src/internal/stdio_impl.h | 6 ++-
446 src/ldso/dynlink.c | 37 +++++++++---------
447 src/locale/c_locale.c | 15 +++++++
448 src/locale/iconv.c | 6 +++
449 src/locale/langinfo.c | 3 +-
450 src/locale/locale_map.c | 12 +-----
451 src/locale/newlocale.c | 15 ++-----
452 src/locale/uselocale.c | 4 +-
453 src/malloc/expand_heap.c | 72 ++++++++++++++++++++++++++++++++++
454 src/malloc/lite_malloc.c | 49 ++++++++++++-----------
455 src/malloc/malloc.c | 80 ++++++++++++++------------------------
456 src/multibyte/btowc.c | 5 ++-
457 src/multibyte/internal.h | 7 ++++
458 src/multibyte/mbrtowc.c | 2 +
459 src/multibyte/mbsrtowcs.c | 19 +++++++++
460 src/multibyte/mbtowc.c | 2 +
461 src/multibyte/wcrtomb.c | 9 +++++
462 src/multibyte/wctob.c | 4 +-
463 src/passwd/nscd_query.c | 12 ++++--
464 src/process/sh/vfork.s | 23 +++++++++++
465 src/regex/fnmatch.c | 3 +-
466 src/signal/sh/restore.s | 4 +-
467 src/stdio/__fdopen.c | 8 +---
468 src/stdio/__stdio_exit.c | 3 +-
469 src/stdio/__stdio_read.c | 11 +-----
470 src/stdio/__stdio_write.c | 14 +------
471 src/stdio/fclose.c | 6 +--
472 src/stdio/fflush.c | 5 +--
473 src/stdio/fgetwc.c | 15 +++++--
474 src/stdio/fmemopen.c | 8 +---
475 src/stdio/fopen.c | 2 +-
476 src/stdio/fputwc.c | 7 +++-
477 src/stdio/fputws.c | 7 +++-
478 src/stdio/fwide.c | 11 +++---
479 src/stdio/ofl.c | 16 ++++++++
480 src/stdio/ofl_add.c | 11 ++++++
481 src/stdio/open_memstream.c | 8 +---
482 src/stdio/open_wmemstream.c | 8 +---
483 src/stdio/ungetwc.c | 18 ++++-----
484 src/stdio/vfwprintf.c | 5 ++-
485 src/stdio/vfwscanf.c | 2 +-
486 src/thread/__unmapself.c | 29 ++++++++++++++
487 src/thread/mips/__unmapself.s | 1 +
488 src/thread/pthread_create.c | 6 ++-
489 src/thread/sh/__set_thread_area.s | 6 ---
490 src/thread/sh/__unmapself.s | 10 ++---
491 src/thread/sh/clone.s | 4 +-
492 src/thread/sh/syscall_cp.s | 2 +-
493 src/unistd/sh/pipe.s | 2 +-
494 63 files changed, 548 insertions(+), 242 deletions(-)
495 create mode 100644 arch/sh/src/__set_thread_area.c
496 create mode 100644 arch/sh/src/__unmapself.c
497 create mode 100644 arch/sh/src/sh_atomic.h
498 create mode 100644 src/locale/c_locale.c
499 create mode 100644 src/malloc/expand_heap.c
500 create mode 100644 src/process/sh/vfork.s
501 create mode 100644 src/stdio/ofl.c
502 create mode 100644 src/stdio/ofl_add.c
504 diff --git a/arch/arm/syscall_arch.h b/arch/arm/syscall_arch.h
505 index 199ad2a..64461ec 100644
506 --- a/arch/arm/syscall_arch.h
507 +++ b/arch/arm/syscall_arch.h
508 @@ -72,3 +72,7 @@ static inline long __syscall6(long n, long a, long b, long c, long d, long e, lo
509 register long r5 __asm__("r5") = f;
510 __asm_syscall("r"(r7), "0"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5));
514 +#define VDSO_CGT_SYM "__vdso_clock_gettime"
515 +#define VDSO_CGT_VER "LINUX_2.6"
516 diff --git a/arch/sh/src/__set_thread_area.c b/arch/sh/src/__set_thread_area.c
518 index 0000000..1d3e022
520 +++ b/arch/sh/src/__set_thread_area.c
522 +#include "pthread_impl.h"
524 +#include "sh_atomic.h"
527 +/* Also perform sh-specific init */
529 +#define CPU_HAS_LLSC 0x0040
531 +__attribute__((__visibility__("hidden"))) unsigned __sh_atomic_model, __sh_nommu;
533 +int __set_thread_area(void *p)
536 + __asm__ __volatile__ ( "ldc %0, gbr" : : "r"(p) : "memory" );
538 + if (__hwcap & CPU_HAS_LLSC) {
539 + __sh_atomic_model = SH_A_LLSC;
542 +#if !defined(__SH3__) && !defined(__SH4__)
543 + for (aux=libc.auxv; *aux; aux+=2) {
544 + if (*aux != AT_PLATFORM) continue;
545 + const char *s = (void *)aux[1];
546 + if (s[0]!='s' || s[1]!='h' || s[2]!='2' || s[3]-'0'<10u) break;
547 + __sh_atomic_model = SH_A_IMASK;
552 + /* __sh_atomic_model = SH_A_GUSA; */ /* 0, default */
556 diff --git a/arch/sh/src/__unmapself.c b/arch/sh/src/__unmapself.c
558 index 0000000..4df9e7b
560 +++ b/arch/sh/src/__unmapself.c
562 +#include "pthread_impl.h"
564 +void __unmapself_sh_mmu(void *, size_t);
565 +void __unmapself_sh_nommu(void *, size_t);
567 +#if !defined(__SH3__) && !defined(__SH4__)
568 +#define __unmapself __unmapself_sh_nommu
569 +#include "../../../src/thread/__unmapself.c"
571 +extern __attribute__((__visibility__("hidden"))) unsigned __sh_nommu;
573 +#define __sh_nommu 0
576 +void __unmapself(void *base, size_t size)
578 + if (__sh_nommu) __unmapself_sh_nommu(base, size);
579 + else __unmapself_sh_mmu(base, size);
581 diff --git a/arch/sh/src/atomic.c b/arch/sh/src/atomic.c
582 index f8c615f..7fd7307 100644
583 --- a/arch/sh/src/atomic.c
584 +++ b/arch/sh/src/atomic.c
588 +#include "sh_atomic.h"
592 +static inline unsigned mask()
595 + __asm__ __volatile__ ( "\n"
600 + : "=&r"(sr) : : "memory", "r0" );
604 +static inline void unmask(unsigned sr)
606 + __asm__ __volatile__ ( "ldc %0,sr" : : "r"(sr) : "memory" );
609 /* gusa is a hack in the kernel which lets you create a sequence of instructions
610 * which will be restarted if the process is preempted in the middle of the
611 * sequence. It will do for implementing atomics on non-smp systems. ABI is:
613 " mov.l " new ", @" mem "\n" \
616 -#define CPU_HAS_LLSC 0x0040
618 int __sh_cas(volatile int *p, int t, int s)
620 - if (__hwcap & CPU_HAS_LLSC) return __sh_cas_llsc(p, t, s);
621 + if (__sh_atomic_model == SH_A_LLSC) return __sh_cas_llsc(p, t, s);
623 + if (__sh_atomic_model == SH_A_IMASK) {
624 + unsigned sr = mask();
626 + if (old==t) *p = s;
632 __asm__ __volatile__(
633 @@ -43,7 +67,15 @@ int __sh_cas(volatile int *p, int t, int s)
635 int __sh_swap(volatile int *x, int v)
637 - if (__hwcap & CPU_HAS_LLSC) return __sh_swap_llsc(x, v);
638 + if (__sh_atomic_model == SH_A_LLSC) return __sh_swap_llsc(x, v);
640 + if (__sh_atomic_model == SH_A_IMASK) {
641 + unsigned sr = mask();
649 __asm__ __volatile__(
650 @@ -55,7 +87,15 @@ int __sh_swap(volatile int *x, int v)
652 int __sh_fetch_add(volatile int *x, int v)
654 - if (__hwcap & CPU_HAS_LLSC) return __sh_fetch_add_llsc(x, v);
655 + if (__sh_atomic_model == SH_A_LLSC) return __sh_fetch_add_llsc(x, v);
657 + if (__sh_atomic_model == SH_A_IMASK) {
658 + unsigned sr = mask();
666 __asm__ __volatile__(
667 @@ -69,7 +109,7 @@ int __sh_fetch_add(volatile int *x, int v)
669 void __sh_store(volatile int *p, int x)
671 - if (__hwcap & CPU_HAS_LLSC) return __sh_store_llsc(p, x);
672 + if (__sh_atomic_model == SH_A_LLSC) return __sh_store_llsc(p, x);
673 __asm__ __volatile__(
675 : : "r"(p), "r"(x) : "memory");
676 @@ -77,7 +117,15 @@ void __sh_store(volatile int *p, int x)
678 void __sh_and(volatile int *x, int v)
680 - if (__hwcap & CPU_HAS_LLSC) return __sh_and_llsc(x, v);
681 + if (__sh_atomic_model == SH_A_LLSC) return __sh_and_llsc(x, v);
683 + if (__sh_atomic_model == SH_A_IMASK) {
684 + unsigned sr = mask();
692 __asm__ __volatile__(
693 @@ -89,7 +137,15 @@ void __sh_and(volatile int *x, int v)
695 void __sh_or(volatile int *x, int v)
697 - if (__hwcap & CPU_HAS_LLSC) return __sh_or_llsc(x, v);
698 + if (__sh_atomic_model == SH_A_LLSC) return __sh_or_llsc(x, v);
700 + if (__sh_atomic_model == SH_A_IMASK) {
701 + unsigned sr = mask();
709 __asm__ __volatile__(
710 diff --git a/arch/sh/src/sh_atomic.h b/arch/sh/src/sh_atomic.h
712 index 0000000..054c2a3
714 +++ b/arch/sh/src/sh_atomic.h
716 +#ifndef _SH_ATOMIC_H
717 +#define _SH_ATOMIC_H
722 +#if !defined(__SH3__) && !defined(__SH4__)
723 +#define SH_A_IMASK 3
725 +#define SH_A_IMASK -1LL /* unmatchable by unsigned int */
728 +extern __attribute__((__visibility__("hidden"))) unsigned __sh_atomic_model;
731 diff --git a/arch/sh/syscall_arch.h b/arch/sh/syscall_arch.h
732 index 7ee21a5..f63675a 100644
733 --- a/arch/sh/syscall_arch.h
734 +++ b/arch/sh/syscall_arch.h
737 #define __asm_syscall(trapno, ...) do { \
738 __asm__ __volatile__ ( \
739 - "trapa #" #trapno "\n" \
744 diff --git a/include/ctype.h b/include/ctype.h
745 index cd2e016..7936536 100644
746 --- a/include/ctype.h
747 +++ b/include/ctype.h
748 @@ -64,6 +64,7 @@ int isascii(int);
750 #define _tolower(a) ((a)|0x20)
751 #define _toupper(a) ((a)&0x5f)
752 +#define isascii(a) (0 ? isascii(a) : (unsigned)(a) < 128)
756 diff --git a/include/stdlib.h b/include/stdlib.h
757 index 97ce5a7..d2c911f 100644
758 --- a/include/stdlib.h
759 +++ b/include/stdlib.h
760 @@ -76,7 +76,8 @@ size_t wcstombs (char *__restrict, const wchar_t *__restrict, size_t);
761 #define EXIT_FAILURE 1
762 #define EXIT_SUCCESS 0
764 -#define MB_CUR_MAX ((size_t)+4)
765 +size_t __ctype_get_mb_cur_max(void);
766 +#define MB_CUR_MAX (__ctype_get_mb_cur_max())
768 #define RAND_MAX (0x7fffffff)
770 diff --git a/src/ctype/__ctype_get_mb_cur_max.c b/src/ctype/__ctype_get_mb_cur_max.c
771 index d235f4d..8e946fc 100644
772 --- a/src/ctype/__ctype_get_mb_cur_max.c
773 +++ b/src/ctype/__ctype_get_mb_cur_max.c
777 +#include "locale_impl.h"
779 size_t __ctype_get_mb_cur_max()
784 diff --git a/src/ctype/isascii.c b/src/ctype/isascii.c
785 index 3af0a10..54ad3bf 100644
786 --- a/src/ctype/isascii.c
787 +++ b/src/ctype/isascii.c
794 diff --git a/src/env/__stack_chk_fail.c b/src/env/__stack_chk_fail.c
795 index 47784c6..be0c184 100644
796 --- a/src/env/__stack_chk_fail.c
797 +++ b/src/env/__stack_chk_fail.c
798 @@ -25,4 +25,8 @@ void __stack_chk_fail_local(void)
804 +weak_alias(__stack_chk_fail, __stack_chk_fail_local);
807 diff --git a/src/internal/libc.h b/src/internal/libc.h
808 index 6810cd8..98c7535 100644
809 --- a/src/internal/libc.h
810 +++ b/src/internal/libc.h
811 @@ -17,8 +17,6 @@ struct __libc {
813 volatile int threads_minus_1;
816 - volatile int ofl_lock[2];
819 struct __locale_struct global_locale;
820 diff --git a/src/internal/locale_impl.h b/src/internal/locale_impl.h
821 index 9b8385e..f5e4d9b 100644
822 --- a/src/internal/locale_impl.h
823 +++ b/src/internal/locale_impl.h
825 +#ifndef _LOCALE_IMPL_H
826 +#define _LOCALE_IMPL_H
831 @@ -12,6 +15,10 @@ struct __locale_map {
832 const struct __locale_map *next;
835 +extern const struct __locale_map __c_dot_utf8;
836 +extern const struct __locale_struct __c_locale;
837 +extern const struct __locale_struct __c_dot_utf8_locale;
839 const struct __locale_map *__get_locale(int, const char *);
840 const char *__mo_lookup(const void *, size_t, const char *);
841 const char *__lctrans(const char *, const struct __locale_map *);
842 @@ -20,9 +27,14 @@ const char *__lctrans_cur(const char *);
843 #define LCTRANS(msg, lc, loc) __lctrans(msg, (loc)->cat[(lc)])
844 #define LCTRANS_CUR(msg) __lctrans_cur(msg)
846 +#define C_LOCALE ((locale_t)&__c_locale)
847 +#define UTF8_LOCALE ((locale_t)&__c_dot_utf8_locale)
849 #define CURRENT_LOCALE (__pthread_self()->locale)
851 #define CURRENT_UTF8 (!!__pthread_self()->locale->cat[LC_CTYPE])
854 #define MB_CUR_MAX (CURRENT_UTF8 ? 4 : 1)
857 diff --git a/src/internal/sh/syscall.s b/src/internal/sh/syscall.s
858 index d00712a..331918a 100644
859 --- a/src/internal/sh/syscall.s
860 +++ b/src/internal/sh/syscall.s
861 @@ -13,7 +13,7 @@ __syscall:
870 diff --git a/src/internal/stdio_impl.h b/src/internal/stdio_impl.h
871 index e1325fe..0dd7fb5 100644
872 --- a/src/internal/stdio_impl.h
873 +++ b/src/internal/stdio_impl.h
874 @@ -47,6 +47,7 @@ struct _IO_FILE {
875 unsigned char *shend;
877 FILE *prev_locked, *next_locked;
878 + struct __locale_struct *locale;
881 size_t __stdio_read(FILE *, unsigned char *, size_t);
882 @@ -75,8 +76,9 @@ int __putc_unlocked(int, FILE *);
883 FILE *__fdopen(int, const char *);
884 int __fmodeflags(const char *);
886 -#define OFLLOCK() LOCK(libc.ofl_lock)
887 -#define OFLUNLOCK() UNLOCK(libc.ofl_lock)
888 +FILE *__ofl_add(FILE *f);
889 +FILE **__ofl_lock(void);
890 +void __ofl_unlock(void);
892 #define feof(f) ((f)->flags & F_EOF)
893 #define ferror(f) ((f)->flags & F_ERR)
894 diff --git a/src/ldso/dynlink.c b/src/ldso/dynlink.c
895 index 42b056d..7e56693 100644
896 --- a/src/ldso/dynlink.c
897 +++ b/src/ldso/dynlink.c
898 @@ -536,7 +536,8 @@ static void *map_library(int fd, struct dso *dso)
900 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
901 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
902 - if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) < 0)
903 + if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC)
904 + && errno != ENOSYS)
908 @@ -927,7 +928,8 @@ static void reloc_all(struct dso *p)
909 do_relocs(p, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], 3);
911 if (head != &ldso && p->relro_start != p->relro_end &&
912 - mprotect(p->base+p->relro_start, p->relro_end-p->relro_start, PROT_READ) < 0) {
913 + mprotect(p->base+p->relro_start, p->relro_end-p->relro_start, PROT_READ)
914 + && errno != ENOSYS) {
915 error("Error relocating %s: RELRO protection failed: %m",
917 if (runtime) longjmp(*rtld_fail, 1);
918 @@ -1192,6 +1194,17 @@ _Noreturn void __dls3(size_t *sp)
919 char **argv_orig = argv;
920 char **envp = argv+argc+1;
922 + /* Find aux vector just past environ[] and use it to initialize
923 + * global data that may be needed before we can make syscalls. */
925 + for (i=argc+1; argv[i]; i++);
926 + libc.auxv = auxv = (void *)(argv+i+1);
927 + decode_vec(auxv, aux, AUX_CNT);
928 + __hwcap = aux[AT_HWCAP];
929 + libc.page_size = aux[AT_PAGESZ];
930 + libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
931 + || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]);
933 /* Setup early thread pointer in builtin_tls for ldso/libc itself to
934 * use during dynamic linking. If possible it will also serve as the
935 * thread pointer at runtime. */
936 @@ -1200,25 +1213,11 @@ _Noreturn void __dls3(size_t *sp)
940 - /* Find aux vector just past environ[] */
941 - for (i=argc+1; argv[i]; i++)
942 - if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
943 - env_path = argv[i]+16;
944 - else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
945 - env_preload = argv[i]+11;
946 - auxv = (void *)(argv+i+1);
948 - decode_vec(auxv, aux, AUX_CNT);
950 /* Only trust user/env if kernel says we're not suid/sgid */
951 - if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
952 - || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
956 + if (!libc.secure) {
957 + env_path = getenv("LD_LIBRARY_PATH");
958 + env_preload = getenv("LD_PRELOAD");
960 - libc.page_size = aux[AT_PAGESZ];
963 /* If the main program was already loaded by the kernel,
964 * AT_PHDR will point to some location other than the dynamic
965 diff --git a/src/locale/c_locale.c b/src/locale/c_locale.c
967 index 0000000..77ccf58
969 +++ b/src/locale/c_locale.c
971 +#include "locale_impl.h"
974 +static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };
976 +const struct __locale_map __c_dot_utf8 = {
978 + .map_size = sizeof empty_mo,
982 +const struct __locale_struct __c_locale = { 0 };
983 +const struct __locale_struct __c_dot_utf8_locale = {
984 + .cat[LC_CTYPE] = &__c_dot_utf8
986 diff --git a/src/locale/iconv.c b/src/locale/iconv.c
987 index e6121ae..1eeea94 100644
988 --- a/src/locale/iconv.c
989 +++ b/src/locale/iconv.c
994 +#include "locale_impl.h"
996 #define UTF_32BE 0300
997 #define UTF_16LE 0301
998 @@ -165,9 +166,12 @@ size_t iconv(iconv_t cd0, char **restrict in, size_t *restrict inb, char **restr
1000 unsigned char type = map[-1];
1001 unsigned char totype = tomap[-1];
1002 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
1004 if (!in || !*in || !*inb) return 0;
1006 + *ploc = UTF8_LOCALE;
1008 for (; *inb; *in+=l, *inb-=l) {
1009 c = *(unsigned char *)*in;
1011 @@ -431,6 +435,7 @@ size_t iconv(iconv_t cd0, char **restrict in, size_t *restrict inb, char **restr
1019 @@ -445,5 +450,6 @@ starved:
1026 diff --git a/src/locale/langinfo.c b/src/locale/langinfo.c
1027 index a1ada24..776b447 100644
1028 --- a/src/locale/langinfo.c
1029 +++ b/src/locale/langinfo.c
1030 @@ -33,7 +33,8 @@ char *__nl_langinfo_l(nl_item item, locale_t loc)
1031 int idx = item & 65535;
1034 - if (item == CODESET) return "UTF-8";
1035 + if (item == CODESET)
1036 + return MB_CUR_MAX==1 ? "UTF-8-CODE-UNITS" : "UTF-8";
1040 diff --git a/src/locale/locale_map.c b/src/locale/locale_map.c
1041 index 4346bb0..c3e5917 100644
1042 --- a/src/locale/locale_map.c
1043 +++ b/src/locale/locale_map.c
1044 @@ -24,14 +24,6 @@ static const char envvars[][12] = {
1048 -static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };
1050 -const struct __locale_map __c_dot_utf8 = {
1052 - .map_size = sizeof empty_mo,
1056 const struct __locale_map *__get_locale(int cat, const char *val)
1059 @@ -107,8 +99,8 @@ const struct __locale_map *__get_locale(int cat, const char *val)
1060 * sake of being able to do message translations at the
1061 * application level. */
1062 if (!new && (new = malloc(sizeof *new))) {
1063 - new->map = empty_mo;
1064 - new->map_size = sizeof empty_mo;
1065 + new->map = __c_dot_utf8.map;
1066 + new->map_size = __c_dot_utf8.map_size;
1067 memcpy(new->name, val, n);
1069 new->next = loc_head;
1070 diff --git a/src/locale/newlocale.c b/src/locale/newlocale.c
1071 index 89d36b1..f50bbe9 100644
1072 --- a/src/locale/newlocale.c
1073 +++ b/src/locale/newlocale.c
1075 #include "locale_impl.h"
1078 -extern const struct __locale_map __c_dot_utf8;
1080 -static const struct __locale_struct c_locale = { 0 };
1081 -static const struct __locale_struct c_dot_utf8_locale = {
1082 - .cat[LC_CTYPE] = &__c_dot_utf8
1085 int __loc_is_allocated(locale_t loc)
1087 - return loc && loc != &c_locale && loc != &c_dot_utf8_locale;
1088 + return loc && loc != C_LOCALE && loc != UTF8_LOCALE;
1091 locale_t __newlocale(int mask, const char *name, locale_t loc)
1092 @@ -44,9 +37,9 @@ locale_t __newlocale(int mask, const char *name, locale_t loc)
1096 - return (locale_t)&c_locale;
1097 - if (j==1 && tmp.cat[LC_CTYPE]==c_dot_utf8_locale.cat[LC_CTYPE])
1098 - return (locale_t)&c_dot_utf8_locale;
1100 + if (j==1 && tmp.cat[LC_CTYPE]==&__c_dot_utf8)
1101 + return UTF8_LOCALE;
1103 if ((loc = malloc(sizeof *loc))) *loc = tmp;
1105 diff --git a/src/locale/uselocale.c b/src/locale/uselocale.c
1106 index b70a0c1..0fc5ecb 100644
1107 --- a/src/locale/uselocale.c
1108 +++ b/src/locale/uselocale.c
1109 @@ -8,9 +8,7 @@ locale_t __uselocale(locale_t new)
1110 locale_t old = self->locale;
1111 locale_t global = &libc.global_locale;
1113 - if (new == LC_GLOBAL_LOCALE) new = global;
1115 - self->locale = new;
1116 + if (new) self->locale = new == LC_GLOBAL_LOCALE ? global : new;
1118 return old == global ? LC_GLOBAL_LOCALE : old;
1120 diff --git a/src/malloc/expand_heap.c b/src/malloc/expand_heap.c
1121 new file mode 100644
1122 index 0000000..d8c0be7
1124 +++ b/src/malloc/expand_heap.c
1126 +#include <limits.h>
1127 +#include <stdint.h>
1129 +#include <sys/mman.h>
1131 +#include "syscall.h"
1133 +/* This function returns true if the interval [old,new]
1134 + * intersects the 'len'-sized interval below &libc.auxv
1135 + * (interpreted as the main-thread stack) or below &b
1136 + * (the current stack). It is used to defend against
1137 + * buggy brk implementations that can cross the stack. */
1139 +static int traverses_stack_p(uintptr_t old, uintptr_t new)
1141 + const uintptr_t len = 8<<20;
1144 + b = (uintptr_t)libc.auxv;
1145 + a = b > len ? b-len : 0;
1146 + if (new>a && old<b) return 1;
1148 + b = (uintptr_t)&b;
1149 + a = b > len ? b-len : 0;
1150 + if (new>a && old<b) return 1;
1155 +void *__mmap(void *, size_t, int, int, int, off_t);
1157 +/* Expand the heap in-place if brk can be used, or otherwise via mmap,
1158 + * using an exponential lower bound on growth by mmap to make
1159 + * fragmentation asymptotically irrelevant. The size argument is both
1160 + * an input and an output, since the caller needs to know the size
1161 + * allocated, which will be larger than requested due to page alignment
1162 + * and mmap minimum size rules. The caller is responsible for locking
1163 + * to prevent concurrent calls. */
1165 +void *__expand_heap(size_t *pn)
1167 + static uintptr_t brk;
1168 + static unsigned mmap_step;
1171 + if (n > SIZE_MAX/2 - PAGE_SIZE) {
1175 + n += -n & PAGE_SIZE-1;
1178 + brk = __syscall(SYS_brk, 0);
1179 + brk += -brk & PAGE_SIZE-1;
1182 + if (n < SIZE_MAX-brk && !traverses_stack_p(brk, brk+n)
1183 + && __syscall(SYS_brk, brk+n)==brk+n) {
1186 + return (void *)(brk-n);
1189 + size_t min = (size_t)PAGE_SIZE << mmap_step/2;
1190 + if (n < min) n = min;
1191 + void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
1192 + MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1193 + if (area == MAP_FAILED) return 0;
1198 diff --git a/src/malloc/lite_malloc.c b/src/malloc/lite_malloc.c
1199 index 7643fc2..008549d 100644
1200 --- a/src/malloc/lite_malloc.c
1201 +++ b/src/malloc/lite_malloc.c
1206 -uintptr_t __brk(uintptr_t);
1210 +void *__expand_heap(size_t *);
1212 void *__simple_malloc(size_t n)
1214 - static uintptr_t cur, brk;
1215 - uintptr_t base, new;
1216 + static char *cur, *end;
1217 static volatile int lock[2];
1219 + size_t align=1, pad;
1223 - if (n > SIZE_MAX/2) goto toobig;
1225 while (align<n && align<ALIGN)
1227 - n = n + align - 1 & -align;
1230 - if (!cur) cur = brk = __brk(0)+16;
1231 - base = cur + align-1 & -align;
1232 - if (n > SIZE_MAX - PAGE_SIZE - base) goto fail;
1233 - if (base+n > brk) {
1234 - new = base+n + PAGE_SIZE-1 & -PAGE_SIZE;
1235 - if (__brk(new) != new) goto fail;
1241 - return (void *)base;
1242 + pad = -(uintptr_t)cur & align-1;
1244 + if (n <= SIZE_MAX/2 + ALIGN) n += pad;
1246 + if (n > end-cur) {
1248 + char *new = __expand_heap(&m);
1271 weak_alias(__simple_malloc, malloc);
1272 diff --git a/src/malloc/malloc.c b/src/malloc/malloc.c
1273 index d4de2dc..290fda1 100644
1274 --- a/src/malloc/malloc.c
1275 +++ b/src/malloc/malloc.c
1277 #define inline inline __attribute__((always_inline))
1280 -uintptr_t __brk(uintptr_t);
1281 void *__mmap(void *, size_t, int, int, int, off_t);
1282 int __munmap(void *, size_t);
1283 void *__mremap(void *, size_t, size_t, int, ...);
1284 @@ -31,13 +30,9 @@ struct bin {
1290 volatile uint64_t binmap;
1291 struct bin bins[64];
1292 - volatile int brk_lock[2];
1293 volatile int free_lock[2];
1294 - unsigned mmap_step;
1298 @@ -152,69 +147,52 @@ void __dump_heap(int x)
1302 +void *__expand_heap(size_t *);
1304 static struct chunk *expand_heap(size_t n)
1307 + static int heap_lock[2];
1313 - lock(mal.brk_lock);
1316 - mal.brk = __brk(0);
1318 - mal.brk = mal.brk + PAGE_SIZE-1 & -PAGE_SIZE;
1320 - mal.brk = mal.brk + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
1321 - mal.heap = (void *)mal.brk;
1324 + /* The argument n already accounts for the caller's chunk
1325 + * overhead needs, but if the heap can't be extended in-place,
1326 + * we need room for an extra zero-sized sentinel chunk. */
1329 - if (n > SIZE_MAX - mal.brk - 2*PAGE_SIZE) goto fail;
1330 - new = mal.brk + n + SIZE_ALIGN + PAGE_SIZE - 1 & -PAGE_SIZE;
1331 - n = new - mal.brk;
1334 - if (__brk(new) != new) {
1335 - size_t min = (size_t)PAGE_SIZE << mal.mmap_step/2;
1336 - n += -n & PAGE_SIZE-1;
1337 - if (n < min) n = min;
1338 - void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
1339 - MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1340 - if (area == MAP_FAILED) goto fail;
1341 + p = __expand_heap(&n);
1343 + unlock(heap_lock);
1348 - area = (char *)area + SIZE_ALIGN - OVERHEAD;
1350 + /* If not just expanding existing space, we need to make a
1351 + * new sentinel chunk below the allocated space. */
1353 + /* Valid/safe because of the prologue increment. */
1355 + p = (char *)p + SIZE_ALIGN;
1356 + w = MEM_TO_CHUNK(p);
1357 w->psize = 0 | C_INUSE;
1358 - w->csize = n | C_INUSE;
1359 - w = NEXT_CHUNK(w);
1360 - w->psize = n | C_INUSE;
1361 - w->csize = 0 | C_INUSE;
1363 - unlock(mal.brk_lock);
1368 - w = MEM_TO_CHUNK(mal.heap);
1369 - w->psize = 0 | C_INUSE;
1371 - w = MEM_TO_CHUNK(new);
1372 + /* Record new heap end and fill in footer. */
1373 + end = (char *)p + n;
1374 + w = MEM_TO_CHUNK(end);
1375 w->psize = n | C_INUSE;
1376 w->csize = 0 | C_INUSE;
1378 - w = MEM_TO_CHUNK(mal.brk);
1379 + /* Fill in header, which may be new or may be replacing a
1380 + * zero-size sentinel header at the old end-of-heap. */
1381 + w = MEM_TO_CHUNK(p);
1382 w->csize = n | C_INUSE;
1385 - unlock(mal.brk_lock);
1387 + unlock(heap_lock);
1391 - unlock(mal.brk_lock);
1396 static int adjust_size(size_t *n)
1397 diff --git a/src/multibyte/btowc.c b/src/multibyte/btowc.c
1398 index 9d2c3b1..8acd0a2 100644
1399 --- a/src/multibyte/btowc.c
1400 +++ b/src/multibyte/btowc.c
1404 +#include <stdlib.h>
1405 +#include "internal.h"
1409 - return c<128U ? c : EOF;
1410 + int b = (unsigned char)c;
1411 + return b<128U ? b : (MB_CUR_MAX==1 && c!=EOF) ? CODEUNIT(c) : WEOF;
1413 diff --git a/src/multibyte/internal.h b/src/multibyte/internal.h
1414 index cc017fa..53d62ed 100644
1415 --- a/src/multibyte/internal.h
1416 +++ b/src/multibyte/internal.h
1417 @@ -23,3 +23,10 @@ extern const uint32_t bittab[];
1422 +/* Arbitrary encoding for representing code units instead of characters. */
1423 +#define CODEUNIT(c) (0xdfff & (signed char)(c))
1424 +#define IS_CODEUNIT(c) ((unsigned)(c)-0xdf80 < 0x80)
1426 +/* Get inline definition of MB_CUR_MAX. */
1427 +#include "locale_impl.h"
1428 diff --git a/src/multibyte/mbrtowc.c b/src/multibyte/mbrtowc.c
1429 index e7b3654..ca7da70 100644
1430 --- a/src/multibyte/mbrtowc.c
1431 +++ b/src/multibyte/mbrtowc.c
1436 +#include <stdlib.h>
1439 #include "internal.h"
1440 @@ -27,6 +28,7 @@ size_t mbrtowc(wchar_t *restrict wc, const char *restrict src, size_t n, mbstate
1443 if (*s < 0x80) return !!(*wc = *s);
1444 + if (MB_CUR_MAX==1) return (*wc = CODEUNIT(*s)), 1;
1445 if (*s-SA > SB-SA) goto ilseq;
1446 c = bittab[*s++-SA]; n--;
1448 diff --git a/src/multibyte/mbsrtowcs.c b/src/multibyte/mbsrtowcs.c
1449 index 3c1343a..e23083d 100644
1450 --- a/src/multibyte/mbsrtowcs.c
1451 +++ b/src/multibyte/mbsrtowcs.c
1456 +#include <string.h>
1457 +#include <stdlib.h>
1458 #include "internal.h"
1460 size_t mbsrtowcs(wchar_t *restrict ws, const char **restrict src, size_t wn, mbstate_t *restrict st)
1461 @@ -24,6 +26,23 @@ size_t mbsrtowcs(wchar_t *restrict ws, const char **restrict src, size_t wn, mbs
1465 + if (MB_CUR_MAX==1) {
1466 + if (!ws) return strlen((const char *)s);
1469 + *src = (const void *)s;
1474 + *ws++ = CODEUNIT(c);
1483 if (*s-1u < 0x7f && (uintptr_t)s%4 == 0) {
1484 while (!(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
1485 diff --git a/src/multibyte/mbtowc.c b/src/multibyte/mbtowc.c
1486 index 803d221..71a9506 100644
1487 --- a/src/multibyte/mbtowc.c
1488 +++ b/src/multibyte/mbtowc.c
1493 +#include <stdlib.h>
1496 #include "internal.h"
1497 @@ -19,6 +20,7 @@ int mbtowc(wchar_t *restrict wc, const char *restrict src, size_t n)
1498 if (!wc) wc = &dummy;
1500 if (*s < 0x80) return !!(*wc = *s);
1501 + if (MB_CUR_MAX==1) return (*wc = CODEUNIT(*s)), 1;
1502 if (*s-SA > SB-SA) goto ilseq;
1503 c = bittab[*s++-SA];
1505 diff --git a/src/multibyte/wcrtomb.c b/src/multibyte/wcrtomb.c
1506 index 59f733d..ddc37a5 100644
1507 --- a/src/multibyte/wcrtomb.c
1508 +++ b/src/multibyte/wcrtomb.c
1513 +#include <stdlib.h>
1516 +#include "internal.h"
1518 size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict st)
1520 @@ -13,6 +15,13 @@ size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict st)
1521 if ((unsigned)wc < 0x80) {
1524 + } else if (MB_CUR_MAX == 1) {
1525 + if (!IS_CODEUNIT(wc)) {
1531 } else if ((unsigned)wc < 0x800) {
1532 *s++ = 0xc0 | (wc>>6);
1533 *s = 0x80 | (wc&0x3f);
1534 diff --git a/src/multibyte/wctob.c b/src/multibyte/wctob.c
1535 index d6353ee..4aeda6a 100644
1536 --- a/src/multibyte/wctob.c
1537 +++ b/src/multibyte/wctob.c
1541 +#include <stdlib.h>
1542 +#include "internal.h"
1546 if (c < 128U) return c;
1547 + if (MB_CUR_MAX==1 && IS_CODEUNIT(c)) return (unsigned char)c;
1550 diff --git a/src/passwd/nscd_query.c b/src/passwd/nscd_query.c
1551 index 69a7815..d38e371 100644
1552 --- a/src/passwd/nscd_query.c
1553 +++ b/src/passwd/nscd_query.c
1554 @@ -32,6 +32,7 @@ FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *
1558 + int errno_save = errno;
1562 @@ -50,11 +51,14 @@ retry:
1565 if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
1566 - /* If there isn't a running nscd we return -1 to indicate that
1567 - * that is precisely what happened
1569 - if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT)
1570 + /* If there isn't a running nscd we simulate a "not found"
1571 + * result and the caller is responsible for calling
1572 + * fclose on the (unconnected) socket. The value of
1573 + * errno must be left unchanged in this case. */
1574 + if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) {
1575 + errno = errno_save;
1581 diff --git a/src/process/sh/vfork.s b/src/process/sh/vfork.s
1582 new file mode 100644
1583 index 0000000..48cc939
1585 +++ b/src/process/sh/vfork.s
1589 +.type __vfork,@function
1590 +.type vfork,@function
1608 + .hidden __syscall_ret
1609 +1: .long __syscall_ret@PLT-(2b+4-.)
1610 diff --git a/src/regex/fnmatch.c b/src/regex/fnmatch.c
1611 index 7f6b65f..978fff8 100644
1612 --- a/src/regex/fnmatch.c
1613 +++ b/src/regex/fnmatch.c
1618 +#include "locale_impl.h"
1621 #define UNMATCHABLE -2
1622 @@ -229,7 +230,7 @@ static int fnmatch_internal(const char *pat, size_t m, const char *str, size_t n
1623 * On illegal sequences we may get it wrong, but in that case
1624 * we necessarily have a matching failure anyway. */
1625 for (s=endstr; s>str && tailcnt; tailcnt--) {
1626 - if (s[-1] < 128U) s--;
1627 + if (s[-1] < 128U || MB_CUR_MAX==1) s--;
1628 else while ((unsigned char)*--s-0x80U<0x40 && s>str);
1630 if (tailcnt) return FNM_NOMATCH;
1631 diff --git a/src/signal/sh/restore.s b/src/signal/sh/restore.s
1632 index ab26034..eaedcdf 100644
1633 --- a/src/signal/sh/restore.s
1634 +++ b/src/signal/sh/restore.s
1636 .type __restore, @function
1638 mov #119, r3 !__NR_sigreturn
1644 @@ -15,7 +15,7 @@ __restore:
1646 mov #100, r3 !__NR_rt_sigreturn
1653 diff --git a/src/stdio/__fdopen.c b/src/stdio/__fdopen.c
1654 index ef8f47d..8d6ce81 100644
1655 --- a/src/stdio/__fdopen.c
1656 +++ b/src/stdio/__fdopen.c
1657 @@ -54,13 +54,7 @@ FILE *__fdopen(int fd, const char *mode)
1658 if (!libc.threaded) f->lock = -1;
1660 /* Add new FILE to open file list */
1662 - f->next = libc.ofl_head;
1663 - if (libc.ofl_head) libc.ofl_head->prev = f;
1664 - libc.ofl_head = f;
1668 + return __ofl_add(f);
1671 weak_alias(__fdopen, fdopen);
1672 diff --git a/src/stdio/__stdio_exit.c b/src/stdio/__stdio_exit.c
1673 index 716e5f7..191b445 100644
1674 --- a/src/stdio/__stdio_exit.c
1675 +++ b/src/stdio/__stdio_exit.c
1676 @@ -16,8 +16,7 @@ static void close_file(FILE *f)
1677 void __stdio_exit(void)
1681 - for (f=libc.ofl_head; f; f=f->next) close_file(f);
1682 + for (f=*__ofl_lock(); f; f=f->next) close_file(f);
1683 close_file(__stdin_used);
1684 close_file(__stdout_used);
1686 diff --git a/src/stdio/__stdio_read.c b/src/stdio/__stdio_read.c
1687 index 5947344..f8fa6d3 100644
1688 --- a/src/stdio/__stdio_read.c
1689 +++ b/src/stdio/__stdio_read.c
1691 #include "stdio_impl.h"
1692 #include <sys/uio.h>
1693 -#include <pthread.h>
1695 -static void cleanup(void *p)
1698 - if (!f->lockcount) __unlockfile(f);
1701 size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
1703 @@ -16,9 +9,7 @@ size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
1707 - pthread_cleanup_push(cleanup, f);
1708 - cnt = syscall_cp(SYS_readv, f->fd, iov, 2);
1709 - pthread_cleanup_pop(0);
1710 + cnt = syscall(SYS_readv, f->fd, iov, 2);
1712 f->flags |= F_EOF ^ ((F_ERR^F_EOF) & cnt);
1714 diff --git a/src/stdio/__stdio_write.c b/src/stdio/__stdio_write.c
1715 index 8c89389..d2d8947 100644
1716 --- a/src/stdio/__stdio_write.c
1717 +++ b/src/stdio/__stdio_write.c
1719 #include "stdio_impl.h"
1720 #include <sys/uio.h>
1721 -#include <pthread.h>
1723 -static void cleanup(void *p)
1726 - if (!f->lockcount) __unlockfile(f);
1729 size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
1731 @@ -19,9 +12,7 @@ size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
1735 - pthread_cleanup_push(cleanup, f);
1736 - cnt = syscall_cp(SYS_writev, f->fd, iov, iovcnt);
1737 - pthread_cleanup_pop(0);
1738 + cnt = syscall(SYS_writev, f->fd, iov, iovcnt);
1740 f->wend = f->buf + f->buf_size;
1741 f->wpos = f->wbase = f->buf;
1742 @@ -34,11 +25,8 @@ size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
1745 if (cnt > iov[0].iov_len) {
1746 - f->wpos = f->wbase = f->buf;
1747 cnt -= iov[0].iov_len;
1749 - } else if (iovcnt == 2) {
1752 iov[0].iov_base = (char *)iov[0].iov_base + cnt;
1753 iov[0].iov_len -= cnt;
1754 diff --git a/src/stdio/fclose.c b/src/stdio/fclose.c
1755 index 317b3c9..839d88a 100644
1756 --- a/src/stdio/fclose.c
1757 +++ b/src/stdio/fclose.c
1758 @@ -14,11 +14,11 @@ int fclose(FILE *f)
1759 __unlist_locked_file(f);
1761 if (!(perm = f->flags & F_PERM)) {
1763 + FILE **head = __ofl_lock();
1764 if (f->prev) f->prev->next = f->next;
1765 if (f->next) f->next->prev = f->prev;
1766 - if (libc.ofl_head == f) libc.ofl_head = f->next;
1768 + if (*head == f) *head = f->next;
1773 diff --git a/src/stdio/fflush.c b/src/stdio/fflush.c
1774 index 7bf862a..3f462c8 100644
1775 --- a/src/stdio/fflush.c
1776 +++ b/src/stdio/fflush.c
1777 @@ -35,13 +35,12 @@ int fflush(FILE *f)
1779 r = __stdout_used ? fflush(__stdout_used) : 0;
1782 - for (f=libc.ofl_head; f; f=f->next) {
1783 + for (f=*__ofl_lock(); f; f=f->next) {
1785 if (f->wpos > f->wbase) r |= __fflush_unlocked(f);
1793 diff --git a/src/stdio/fgetwc.c b/src/stdio/fgetwc.c
1794 index 8626d54..e455cfe 100644
1795 --- a/src/stdio/fgetwc.c
1796 +++ b/src/stdio/fgetwc.c
1798 #include "stdio_impl.h"
1799 +#include "locale_impl.h"
1803 -wint_t __fgetwc_unlocked(FILE *f)
1804 +static wint_t __fgetwc_unlocked_internal(FILE *f)
1806 mbstate_t st = { 0 };
1808 @@ -10,8 +11,6 @@ wint_t __fgetwc_unlocked(FILE *f)
1812 - f->mode |= f->mode+1;
1814 /* Convert character from buffer if possible */
1815 if (f->rpos < f->rend) {
1816 l = mbrtowc(&wc, (void *)f->rpos, f->rend - f->rpos, &st);
1817 @@ -39,6 +38,16 @@ wint_t __fgetwc_unlocked(FILE *f)
1821 +wint_t __fgetwc_unlocked(FILE *f)
1823 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
1824 + if (f->mode <= 0) fwide(f, 1);
1825 + *ploc = f->locale;
1826 + wchar_t wc = __fgetwc_unlocked_internal(f);
1831 wint_t fgetwc(FILE *f)
1834 diff --git a/src/stdio/fmemopen.c b/src/stdio/fmemopen.c
1835 index d784960..7c193a5 100644
1836 --- a/src/stdio/fmemopen.c
1837 +++ b/src/stdio/fmemopen.c
1838 @@ -110,11 +110,5 @@ FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode)
1840 if (!libc.threaded) f->lock = -1;
1843 - f->next = libc.ofl_head;
1844 - if (libc.ofl_head) libc.ofl_head->prev = f;
1845 - libc.ofl_head = f;
1849 + return __ofl_add(f);
1851 diff --git a/src/stdio/fopen.c b/src/stdio/fopen.c
1852 index 07bdb6e..252f082 100644
1853 --- a/src/stdio/fopen.c
1854 +++ b/src/stdio/fopen.c
1855 @@ -18,7 +18,7 @@ FILE *fopen(const char *restrict filename, const char *restrict mode)
1856 /* Compute the flags to pass to open() */
1857 flags = __fmodeflags(mode);
1859 - fd = sys_open_cp(filename, flags, 0666);
1860 + fd = sys_open(filename, flags, 0666);
1861 if (fd < 0) return 0;
1862 if (flags & O_CLOEXEC)
1863 __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
1864 diff --git a/src/stdio/fputwc.c b/src/stdio/fputwc.c
1865 index 7b621dd..789fe9c 100644
1866 --- a/src/stdio/fputwc.c
1867 +++ b/src/stdio/fputwc.c
1869 #include "stdio_impl.h"
1870 +#include "locale_impl.h"
1874 @@ -7,8 +8,10 @@ wint_t __fputwc_unlocked(wchar_t c, FILE *f)
1876 char mbc[MB_LEN_MAX];
1878 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
1880 - f->mode |= f->mode+1;
1881 + if (f->mode <= 0) fwide(f, 1);
1882 + *ploc = f->locale;
1885 c = putc_unlocked(c, f);
1886 @@ -20,6 +23,8 @@ wint_t __fputwc_unlocked(wchar_t c, FILE *f)
1888 if (l < 0 || __fwritex((void *)mbc, l, f) < l) c = WEOF;
1890 + if (c==WEOF) f->flags |= F_ERR;
1895 diff --git a/src/stdio/fputws.c b/src/stdio/fputws.c
1896 index 5723cbc..0ed02f1 100644
1897 --- a/src/stdio/fputws.c
1898 +++ b/src/stdio/fputws.c
1900 #include "stdio_impl.h"
1901 +#include "locale_impl.h"
1904 int fputws(const wchar_t *restrict ws, FILE *restrict f)
1906 unsigned char buf[BUFSIZ];
1908 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
1912 - f->mode |= f->mode+1;
1914 + *ploc = f->locale;
1916 while (ws && (l = wcsrtombs((void *)buf, (void*)&ws, sizeof buf, 0))+1 > 1)
1917 if (__fwritex(buf, l, f) < l) {
1926 return l; /* 0 or -1 */
1929 diff --git a/src/stdio/fwide.c b/src/stdio/fwide.c
1930 index 8088e7a..8410b15 100644
1931 --- a/src/stdio/fwide.c
1932 +++ b/src/stdio/fwide.c
1935 #include "stdio_impl.h"
1937 -#define SH (8*sizeof(int)-1)
1938 -#define NORMALIZE(x) ((x)>>SH | -((-(x))>>SH))
1939 +#include "locale_impl.h"
1941 int fwide(FILE *f, int mode)
1944 - if (!f->mode) f->mode = NORMALIZE(mode);
1946 + if (!f->locale) f->locale = MB_CUR_MAX==1
1947 + ? C_LOCALE : UTF8_LOCALE;
1948 + if (!f->mode) f->mode = mode>0 ? 1 : -1;
1953 diff --git a/src/stdio/ofl.c b/src/stdio/ofl.c
1954 new file mode 100644
1955 index 0000000..b143999
1957 +++ b/src/stdio/ofl.c
1959 +#include "stdio_impl.h"
1962 +static FILE *ofl_head;
1963 +static volatile int ofl_lock[2];
1965 +FILE **__ofl_lock()
1971 +void __ofl_unlock()
1975 diff --git a/src/stdio/ofl_add.c b/src/stdio/ofl_add.c
1976 new file mode 100644
1977 index 0000000..d7de9f1
1979 +++ b/src/stdio/ofl_add.c
1981 +#include "stdio_impl.h"
1983 +FILE *__ofl_add(FILE *f)
1985 + FILE **head = __ofl_lock();
1987 + if (*head) (*head)->prev = f;
1992 diff --git a/src/stdio/open_memstream.c b/src/stdio/open_memstream.c
1993 index 9eafdfb..58504c9 100644
1994 --- a/src/stdio/open_memstream.c
1995 +++ b/src/stdio/open_memstream.c
1996 @@ -79,11 +79,5 @@ FILE *open_memstream(char **bufp, size_t *sizep)
1998 if (!libc.threaded) f->lock = -1;
2001 - f->next = libc.ofl_head;
2002 - if (libc.ofl_head) libc.ofl_head->prev = f;
2003 - libc.ofl_head = f;
2007 + return __ofl_add(f);
2009 diff --git a/src/stdio/open_wmemstream.c b/src/stdio/open_wmemstream.c
2010 index 3537030..7ab2c64 100644
2011 --- a/src/stdio/open_wmemstream.c
2012 +++ b/src/stdio/open_wmemstream.c
2013 @@ -81,11 +81,5 @@ FILE *open_wmemstream(wchar_t **bufp, size_t *sizep)
2015 if (!libc.threaded) f->lock = -1;
2018 - f->next = libc.ofl_head;
2019 - if (libc.ofl_head) libc.ofl_head->prev = f;
2020 - libc.ofl_head = f;
2024 + return __ofl_add(f);
2026 diff --git a/src/stdio/ungetwc.c b/src/stdio/ungetwc.c
2027 index 913f716..80d6e20 100644
2028 --- a/src/stdio/ungetwc.c
2029 +++ b/src/stdio/ungetwc.c
2031 #include "stdio_impl.h"
2032 +#include "locale_impl.h"
2036 @@ -8,21 +9,19 @@ wint_t ungetwc(wint_t c, FILE *f)
2038 unsigned char mbc[MB_LEN_MAX];
2041 - if (c == WEOF) return c;
2043 - /* Try conversion early so we can fail without locking if invalid */
2044 - if (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)
2046 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
2050 - f->mode |= f->mode+1;
2051 + if (f->mode <= 0) fwide(f, 1);
2052 + *ploc = f->locale;
2054 if (!f->rpos) __toread(f);
2055 - if (!f->rpos || f->rpos < f->buf - UNGET + l) {
2056 + if (!f->rpos || f->rpos < f->buf - UNGET + l || c == WEOF ||
2057 + (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)) {
2064 if (isascii(c)) *--f->rpos = c;
2065 @@ -31,5 +30,6 @@ wint_t ungetwc(wint_t c, FILE *f)
2072 diff --git a/src/stdio/vfwprintf.c b/src/stdio/vfwprintf.c
2073 index ebdff00..f06d5ae 100644
2074 --- a/src/stdio/vfwprintf.c
2075 +++ b/src/stdio/vfwprintf.c
2076 @@ -293,7 +293,10 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
2077 if ((fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
2081 + arg.p = strerror(errno);
2083 + if (!arg.p) arg.p = "(null)";
2085 if (p<0) p = INT_MAX;
2086 for (i=l=0; l<p && (i=mbtowc(&wc, bs, MB_LEN_MAX))>0; bs+=i, l++);
2087 @@ -356,7 +359,7 @@ int vfwprintf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
2091 - f->mode |= f->mode+1;
2093 olderr = f->flags & F_ERR;
2095 ret = wprintf_core(f, fmt, &ap2, nl_arg, nl_type);
2096 diff --git a/src/stdio/vfwscanf.c b/src/stdio/vfwscanf.c
2097 index ac5c2c2..223aad4 100644
2098 --- a/src/stdio/vfwscanf.c
2099 +++ b/src/stdio/vfwscanf.c
2100 @@ -104,7 +104,7 @@ int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
2104 - f->mode |= f->mode+1;
2107 for (p=fmt; *p; p++) {
2109 diff --git a/src/thread/__unmapself.c b/src/thread/__unmapself.c
2110 index e69de29..1d3bee1 100644
2111 --- a/src/thread/__unmapself.c
2112 +++ b/src/thread/__unmapself.c
2114 +#include "pthread_impl.h"
2115 +#include "atomic.h"
2116 +#include "syscall.h"
2117 +/* cheat and reuse CRTJMP macro from dynlink code */
2118 +#include "dynlink.h"
2120 +static volatile int lock;
2121 +static void *unmap_base;
2122 +static size_t unmap_size;
2123 +static char shared_stack[256];
2125 +static void do_unmap()
2127 + __syscall(SYS_munmap, unmap_base, unmap_size);
2128 + __syscall(SYS_exit);
2131 +void __unmapself(void *base, size_t size)
2133 + int tid=__pthread_self()->tid;
2134 + char *stack = shared_stack + sizeof shared_stack;
2135 + stack -= (uintptr_t)stack % 16;
2136 + while (lock || a_cas(&lock, 0, tid))
2138 + __syscall(SYS_set_tid_address, &lock);
2139 + unmap_base = base;
2140 + unmap_size = size;
2141 + CRTJMP(do_unmap, stack);
2143 diff --git a/src/thread/mips/__unmapself.s b/src/thread/mips/__unmapself.s
2144 index 9aa0371..ba139dc 100644
2145 --- a/src/thread/mips/__unmapself.s
2146 +++ b/src/thread/mips/__unmapself.s
2149 .type __unmapself,@function
2155 diff --git a/src/thread/pthread_create.c b/src/thread/pthread_create.c
2156 index de72818..e7df34a 100644
2157 --- a/src/thread/pthread_create.c
2158 +++ b/src/thread/pthread_create.c
2159 @@ -191,8 +191,9 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
2160 if (!libc.can_do_threads) return ENOSYS;
2161 self = __pthread_self();
2162 if (!libc.threaded) {
2163 - for (FILE *f=libc.ofl_head; f; f=f->next)
2164 + for (FILE *f=*__ofl_lock(); f; f=f->next)
2167 init_file_lock(__stdin_used);
2168 init_file_lock(__stdout_used);
2169 init_file_lock(__stderr_used);
2170 @@ -231,7 +232,8 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
2172 map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
2173 if (map == MAP_FAILED) goto fail;
2174 - if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)) {
2175 + if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)
2176 + && errno != ENOSYS) {
2177 __munmap(map, size);
2180 diff --git a/src/thread/sh/__set_thread_area.s b/src/thread/sh/__set_thread_area.s
2181 index d9f1181..e69de29 100644
2182 --- a/src/thread/sh/__set_thread_area.s
2183 +++ b/src/thread/sh/__set_thread_area.s
2185 -.global __set_thread_area
2186 -.type __set_thread_area, @function
2191 diff --git a/src/thread/sh/__unmapself.s b/src/thread/sh/__unmapself.s
2192 index b34c3c8..0161d53 100644
2193 --- a/src/thread/sh/__unmapself.s
2194 +++ b/src/thread/sh/__unmapself.s
2197 -.global __unmapself
2198 -.type __unmapself, @function
2200 +.global __unmapself_sh_mmu
2201 +.type __unmapself_sh_mmu, @function
2202 +__unmapself_sh_mmu:
2203 mov #91, r3 ! SYS_munmap
2209 @@ -13,7 +13,7 @@ __unmapself:
2211 mov #1, r3 ! SYS_exit
2218 diff --git a/src/thread/sh/clone.s b/src/thread/sh/clone.s
2219 index d6c9184..f8ad845 100644
2220 --- a/src/thread/sh/clone.s
2221 +++ b/src/thread/sh/clone.s
2222 @@ -17,7 +17,7 @@ __clone:
2223 mov.l @r15, r6 ! r6 = ptid
2224 mov.l @(8,r15), r7 ! r7 = ctid
2225 mov.l @(4,r15), r0 ! r0 = tls
2231 @@ -38,7 +38,7 @@ __clone:
2233 mov #1, r3 ! __NR_exit
2240 diff --git a/src/thread/sh/syscall_cp.s b/src/thread/sh/syscall_cp.s
2241 index 6b28ddf..c3cafac 100644
2242 --- a/src/thread/sh/syscall_cp.s
2243 +++ b/src/thread/sh/syscall_cp.s
2244 @@ -31,7 +31,7 @@ L1: .long __cancel@PLT-(1b-.)
2252 ! work around hardware bug
2253 diff --git a/src/unistd/sh/pipe.s b/src/unistd/sh/pipe.s
2254 index d865ae3..46c4908 100644
2255 --- a/src/unistd/sh/pipe.s
2256 +++ b/src/unistd/sh/pipe.s
2258 .type pipe, @function
2264 ! work around hardware bug