1 commit 6ba5517a460c6c438f64d69464fdfc3269a4c91a
2 Author: Rich Felker <dalias@aerifal.cx>
3 Date: Thu Jun 25 22:22:00 2015 +0000
5 fix local-dynamic model TLS on mips and powerpc
7 the TLS ABI spec for mips, powerpc, and some other (presently
8 unsupported) RISC archs has the return value of __tls_get_addr offset
9 by +0x8000 and the result of DTPOFF relocations offset by -0x8000. I
10 had previously assumed this part of the ABI was actually just an
11 implementation detail, since the adjustments cancel out. however, when
12 the local dynamic model is used for accessing TLS that's known to be
13 in the same DSO, either of the following may happen:
15 1. the -0x8000 offset may already be applied to the argument structure
16 passed to __tls_get_addr at ld time, without any opportunity for
19 2. __tls_get_addr may be used with a zero offset argument to obtain a
20 base address for the module's TLS, to which the caller then applies
21 immediate offsets for individual objects accessed using the local
22 dynamic model. since the immediate offsets have the -0x8000 adjustment
23 applied to them, the base address they use needs to include the
26 it would be possible, but more complex, to store the pointers in the
27 dtv[] array with the +0x8000 offset pre-applied, to avoid the runtime
28 cost of adding 0x8000 on each call to __tls_get_addr. this change
29 could be made later if measurements show that it would help.
31 commit ce337daa00e42d4f2d9a4d9ae0ed51b20249d924
32 Author: Rich Felker <dalias@aerifal.cx>
33 Date: Tue Jun 23 04:03:42 2015 +0000
35 make dynamic linker work around MAP_FAILED mmap failure on nommu kernels
37 previously, loading of additional libraries beyond libc/ldso did not
38 work on nommu kernels, nor did loading programs via invocation of the
39 dynamic linker as a command.
41 commit a59341420fdedb288d9ff80e73609ae44e9cf258
42 Author: Rich Felker <dalias@aerifal.cx>
43 Date: Tue Jun 23 00:12:25 2015 +0000
45 reimplement strverscmp to fix corner cases
47 this interface is non-standardized and is a GNU invention, and as
48 such, our implementation should match the behavior of the GNU
49 function. one peculiarity the old implementation got wrong was the
50 handling of all-zero digit sequences: they are supposed to compare
51 greater than digit sequences of which they are a proper prefix, as in
54 in addition, high bytes were treated with char signedness rather than
55 as unsigned. this was wrong regardless of what the GNU function does
56 since the resulting order relation varied by arch.
58 the new strverscmp implementation makes explicit the cases where the
59 order differs from what strcmp would produce, of which there are only
62 commit 153e952e1a688859d7095345b17e6c1df74a295c
63 Author: Rich Felker <dalias@aerifal.cx>
64 Date: Mon Jun 22 20:33:28 2015 +0000
66 fix regression/typo that disabled __simple_malloc when calloc is used
68 commit ba819787ee93ceae94efd274f7849e317c1bff58 introduced this
69 regression. since the __malloc0 weak alias was not properly provided
70 by __simple_malloc, use of calloc forced the full malloc to be linked.
72 commit ba819787ee93ceae94efd274f7849e317c1bff58
73 Author: Rich Felker <dalias@aerifal.cx>
74 Date: Mon Jun 22 18:50:09 2015 +0000
76 fix calloc when __simple_malloc implementation is used
78 previously, calloc's implementation encoded assumptions about the
79 implementation of malloc, accessing a size_t word just prior to the
80 allocated memory to determine if it was obtained by mmap to optimize
81 out the zero-filling. when __simple_malloc is used (static linking a
82 program with no realloc/free), it doesn't matter if the result of this
83 check is wrong, since all allocations are zero-initialized anyway. but
84 the access could be invalid if it crosses a page boundary or if the
85 pointer is not sufficiently aligned, which can happen for very small
88 this patch fixes the issue by moving the zero-fill logic into malloc.c
89 with the full malloc, as a new function named __malloc0, which is
90 provided by a weak alias to __simple_malloc (which always gives
91 zero-filled memory) when the full malloc is not in use.
93 commit 55d061f031085f24d138664c897791aebe9a2fab
94 Author: Rich Felker <dalias@aerifal.cx>
95 Date: Sat Jun 20 03:01:07 2015 +0000
97 provide __stack_chk_fail_local in libc.a
99 this symbol is needed only on archs where the PLT call ABI is klunky,
100 and only for position-independent code compiled with stack protector.
101 thus references usually only appear in shared libraries or PIE
102 executables, but they can also appear when linking statically if some
103 of the object files being linked were built as PIC/PIE.
105 normally libssp_nonshared.a from the compiler toolchain should provide
106 __stack_chk_fail_local, but reportedly it appears prior to -lc in the
107 link order, thus failing to satisfy references from libc itself (which
108 arise only if libc.a was built as PIC/PIE with stack protector
111 commit ce3688eca920aa77549323f84e21f33522397115
112 Author: Rich Felker <dalias@aerifal.cx>
113 Date: Sat Jun 20 02:54:30 2015 +0000
115 work around mips detached thread exit breakage due to kernel regression
117 linux kernel commit 46e12c07b3b9603c60fc1d421ff18618241cb081 caused
118 the mips syscall mechanism to fail with EFAULT when the userspace
119 stack pointer is invalid, breaking __unmapself used for detached
120 thread exit. the workaround is to set $sp to a known-valid, readable
121 address, and the simplest one to obtain is the address of the current
122 function, which is available (per o32 calling convention) in $25.
124 commit 75eceb3ae824d54e865686c0c538551aeebf3372
125 Author: Rich Felker <dalias@aerifal.cx>
126 Date: Wed Jun 17 17:21:46 2015 +0000
128 ignore ENOSYS error from mprotect in pthread_create and dynamic linker
130 this error simply indicated a system without memory protection (NOMMU)
131 and should not cause failure in the caller.
133 commit 10d0268ccfab9152250eeeed3952ce3fed44131a
134 Author: Rich Felker <dalias@aerifal.cx>
135 Date: Tue Jun 16 15:25:02 2015 +0000
137 switch to using trap number 31 for syscalls on sh
139 nominally the low bits of the trap number on sh are the number of
140 syscall arguments, but they have never been used by the kernel, and
141 some code making syscalls does not even know the number of arguments
142 and needs to pass an arbitrary high number anyway.
144 sh3/sh4 traditionally used the trap range 16-31 for syscalls, but part
145 of this range overlapped with hardware exceptions/interrupts on sh2
146 hardware, so an incompatible range 32-47 was chosen for sh2.
148 using trap number 31 everywhere, since it's in the existing sh3/sh4
149 range and does not conflict with sh2 hardware, is a proposed
150 unification of the kernel syscall convention that will allow binaries
151 to be shared between sh2 and sh3/sh4. if this is not accepted into the
152 kernel, we can refit the sh2 target with runtime selection mechanisms
153 for the trap number, but doing so would be invasive and would entail
154 non-trivial overhead.
156 commit 3366a99b17847b58f2d8cc52cbb5d65deb824f8a
157 Author: Rich Felker <dalias@aerifal.cx>
158 Date: Tue Jun 16 14:55:06 2015 +0000
160 switch sh port's __unmapself to generic version when running on sh2/nommu
162 due to the way the interrupt and syscall trap mechanism works,
163 userspace on sh2 must never set the stack pointer to an invalid value.
164 thus, the approach used on most archs, where __unmapself executes with
165 no stack for the interval between SYS_munmap and SYS_exit, is not
168 in order not to pessimize sh3/sh4, the sh asm version of __unmapself
169 is not removed. instead it's renamed and redirected through code that
170 calls either the generic (safe) __unmapself or the sh3/sh4 asm,
171 depending on compile-time and run-time conditions.
173 commit f9d84554bae0fa17c9a1d724549c4408022228a5
174 Author: Rich Felker <dalias@aerifal.cx>
175 Date: Tue Jun 16 14:28:30 2015 +0000
177 add support for sh2 interrupt-masking-based atomics to sh port
179 the sh2 target is being considered an ISA subset of sh3/sh4, in the
180 sense that binaries built for sh2 are intended to be usable on later
181 cpu models/kernels with mmu support. so rather than hard-coding
182 sh2-specific atomics, the runtime atomic selection mechanisms that was
183 already in place has been extended to add sh2 atomics.
185 at this time, the sh2 atomics are not SMP-compatible; since the ISA
186 lacks actual atomic operations, the new code instead masks interrupts
187 for the duration of the atomic operation, producing an atomic result
188 on single-core. this is only possible because the kernel/hardware does
189 not impose protections against userspace doing so. additional changes
190 will be needed to support future SMP systems.
192 care has been taken to avoid producing significant additional code
193 size in the case where it's known at compile-time that the target is
194 not sh2 and does not need sh2-specific code.
196 commit 1b0cdc8700d29ef018bf226d74b2b58b23bce91c
197 Author: Rich Felker <dalias@aerifal.cx>
198 Date: Tue Jun 16 07:11:19 2015 +0000
200 refactor stdio open file list handling, move it out of global libc struct
202 functions which open in-memory FILE stream variants all shared a tail
203 with __fdopen, adding the FILE structure to stdio's open file list.
204 replacing this common tail with a function call reduces code size and
205 duplication of logic. the list is also partially encapsulated now.
207 function signatures were chosen to facilitate tail call optimization
208 and reduce the need for additional accessor functions.
210 with these changes, static linked programs that do not use stdio no
211 longer have an open file list at all.
213 commit f22a9edaf8a6f2ca1d314d18b3785558279a5c03
214 Author: Rich Felker <dalias@aerifal.cx>
215 Date: Tue Jun 16 06:18:00 2015 +0000
217 byte-based C locale, phase 3: make MB_CUR_MAX variable to activate code
219 this patch activates the new byte-based C locale (high bytes treated
220 as abstract code unit "characters" rather than decoded as multibyte
221 characters) by making the value of MB_CUR_MAX depend on the active
222 locale. for the C locale, the LC_CTYPE category pointer is null,
223 yielding a value of 1. all other locales yield a value of 4.
225 commit 16f18d036d9a7bf590ee6eb86785c0a9658220b6
226 Author: Rich Felker <dalias@aerifal.cx>
227 Date: Tue Jun 16 05:35:31 2015 +0000
229 byte-based C locale, phase 2: stdio and iconv (multibyte callers)
231 this patch adjusts libc components which use the multibyte functions
232 internally, and which depend on them operating in a particular
233 encoding, to make the appropriate locale changes before calling them
234 and restore the calling thread's locale afterwards. activating the
235 byte-based C locale without these changes would cause regressions in
238 in the case of iconv, the current implementation was simply using the
239 multibyte functions as UTF-8 conversions. setting a multibyte UTF-8
240 locale for the duration of the iconv operation allows the code to
243 in the case of stdio, POSIX requires that FILE streams have an
244 encoding rule bound at the time of setting wide orientation. as long
245 as all locales, including the C locale, used the same encoding,
246 treating high bytes as UTF-8, there was no need to store an encoding
247 rule as part of the stream's state.
249 a new locale field in the FILE structure points to the locale that
250 should be made active during fgetwc/fputwc/ungetwc on the stream. it
251 cannot point to the locale active at the time the stream becomes
252 oriented, because this locale could be mutable (the global locale) or
253 could be destroyed (locale_t objects produced by newlocale) before the
254 stream is closed. instead, a pointer to the static C or C.UTF-8 locale
255 object added in commit commit aeeac9ca5490d7d90fe061ab72da446c01ddf746
256 is used. this is valid since categories other than LC_CTYPE will not
257 affect these functions.
259 commit 1507ebf837334e9e07cfab1ca1c2e88449069a80
260 Author: Rich Felker <dalias@aerifal.cx>
261 Date: Tue Jun 16 04:44:17 2015 +0000
263 byte-based C locale, phase 1: multibyte character handling functions
265 this patch makes the functions which work directly on multibyte
266 characters treat the high bytes as individual abstract code units
267 rather than as multibyte sequences when MB_CUR_MAX is 1. since
268 MB_CUR_MAX is presently defined as a constant 4, all of the new code
269 added is dead code, and optimizing compilers' code generation should
270 not be affected at all. a future commit will activate the new code.
272 as abstract code units, bytes 0x80 to 0xff are represented by wchar_t
273 values 0xdf80 to 0xdfff, at the end of the surrogates range. this
274 ensures that they will never be misinterpreted as Unicode characters,
275 and that all wctype functions return false for these "characters"
276 without needing locale-specific logic. a high range outside of Unicode
277 such as 0x7fffff80 to 0x7fffffff was also considered, but since C11's
278 char16_t also needs to be able to represent conversions of these
279 bytes, the surrogate range was the natural choice.
281 commit 38e2f727237230300fea6aff68802db04625fd23
282 Author: Rich Felker <dalias@aerifal.cx>
283 Date: Tue Jun 16 04:21:38 2015 +0000
285 fix btowc corner case
287 btowc is required to interpret its argument by conversion to unsigned
288 char, unless the argument is equal to EOF. since the conversion to
289 produces a non-character value anyway, we can just unconditionally
292 commit ee59c296d56bf26f49f354d6eb32b4b6d4190188
293 Author: Szabolcs Nagy <nsz@port70.net>
294 Date: Wed Jun 3 10:32:14 2015 +0100
296 arm: add vdso support
298 vdso will be available on arm in linux v4.2, the user-space code
299 for it is in kernel commit 8512287a8165592466cb9cb347ba94892e9c56a5
301 commit e3bc22f1eff87b8f029a6ab31f1a269d69e4b053
302 Author: Rich Felker <dalias@aerifal.cx>
303 Date: Sun Jun 14 01:59:02 2015 +0000
305 refactor malloc's expand_heap to share with __simple_malloc
307 this extends the brk/stack collision protection added to full malloc
308 in commit 276904c2f6bde3a31a24ebfa201482601d18b4f9 to also protect the
309 __simple_malloc function used in static-linked programs that don't
310 reference the free function.
312 it also extends support for using mmap when brk fails, which full
313 malloc got in commit 5446303328adf4b4e36d9fba21848e6feb55fab4, to
316 since __simple_malloc may expand the heap by arbitrarily large
317 increments, the stack collision detection is enhanced to detect
318 interval overlap rather than just proximity of a single address to the
319 stack. code size is increased a bit, but this is partly offset by the
320 sharing of code between the two malloc implementations, which due to
321 linking semantics, both get linked in a program that needs the full
322 malloc with realloc/free support.
324 commit 4ef9b828c1f39553a69e0635ac91f0fcadd6e8c6
325 Author: Rich Felker <dalias@aerifal.cx>
326 Date: Sat Jun 13 20:53:02 2015 +0000
328 remove cancellation points in stdio
330 commit 58165923890865a6ac042fafce13f440ee986fd9 added these optional
331 cancellation points on the basis that cancellable stdio could be
332 useful, to unblock threads stuck on stdio operations that will never
333 complete. however, the only way to ensure that cancellation can
334 achieve this is to violate the rules for side effects when
335 cancellation is acted upon, discarding knowledge of any partial data
336 transfer already completed. our implementation exhibited this behavior
337 and was thus non-conforming.
339 in addition to improving correctness, removing these cancellation
340 points moderately reduces code size, and should significantly improve
341 performance on i386, where sysenter/syscall instructions can be used
342 instead of "int $128" for non-cancellable syscalls.
344 commit 536c6d5a4205e2a3f161f2983ce1e0ac3082187d
345 Author: Rich Felker <dalias@aerifal.cx>
346 Date: Sat Jun 13 05:17:16 2015 +0000
348 fix idiom for setting stdio stream orientation to wide
350 the old idiom, f->mode |= f->mode+1, was adapted from the idiom for
351 setting byte orientation, f->mode |= f->mode-1, but the adaptation was
352 incorrect. unless the stream was alreasdy set byte-oriented, this code
353 incremented f->mode each time it was executed, which would eventually
354 lead to overflow. it could be fixed by changing it to f->mode |= 1,
355 but upcoming changes will require slightly more work at the time of
356 wide orientation, so it makes sense to just call fwide. as an
357 optimization in the single-character functions, fwide is only called
358 if the stream is not already wide-oriented.
360 commit f8f565df467c13248104223f99abf7f37cef7584
361 Author: Rich Felker <dalias@aerifal.cx>
362 Date: Sat Jun 13 04:42:38 2015 +0000
364 add printing of null %s arguments as "(null)" in wide printf
366 this is undefined, but supported in our implementation of the normal
367 printf, so for consistency the wide variant should support it too.
369 commit f9e25d813860d53cd1e9b6145cc63375d2fe2529
370 Author: Rich Felker <dalias@aerifal.cx>
371 Date: Sat Jun 13 04:37:27 2015 +0000
373 add %m support to wide printf
375 commit ec634aad91f57479ef17525e33ed446c780a61f4
376 Author: Rich Felker <dalias@aerifal.cx>
377 Date: Thu Jun 11 05:01:04 2015 +0000
381 commit c30cbcb0a646b1f13a22c645616dce624465b883
382 Author: Rich Felker <dalias@aerifal.cx>
383 Date: Wed Jun 10 02:27:40 2015 +0000
385 implement arch-generic version of __unmapself
387 this can be used to put off writing an asm version of __unmapself for
388 new archs, or as a permanent solution on archs where it's not
389 practical or even possible to run momentarily with no stack.
391 the concept here is simple: the caller takes a lock on a global shared
392 stack and uses it to make the munmap and exit syscalls. the only trick
393 is unlocking, which must be done after the thread exits, and this is
394 achieved by using the set_tid_address syscall to have the kernel zero
395 and futex-wake the lock word as part of the exit syscall.
397 commit 276904c2f6bde3a31a24ebfa201482601d18b4f9
398 Author: Rich Felker <dalias@aerifal.cx>
399 Date: Tue Jun 9 20:30:35 2015 +0000
401 in malloc, refuse to use brk if it grows into stack
403 the linux/nommu fdpic ELF loader sets up the brk range to overlap
404 entirely with the main thread's stack (but growing from opposite
405 ends), so that the resulting failure mode for malloc is not to return
406 a null pointer but to start returning pointers to memory that overlaps
407 with the caller's stack. needless to say this extremely dangerous and
410 since it's non-trivial to detect execution environments that might be
411 affected by this kernel bug, and since the severity of the bug makes
412 any sort of detection that might yield false-negatives unsafe, we
413 instead check the proximity of the brk to the stack pointer each time
414 the brk is to be expanded. both the main thread's stack (where the
415 real known risk lies) and the calling thread's stack are checked. an
416 arbitrary gap distance of 8 MB is imposed, chosen to be larger than
417 linux default main-thread stack reservation sizes and larger than any
418 reasonable stack configuration on nommu.
420 the effeciveness of this patch relies on an assumption that the amount
421 by which the brk is being grown is smaller than the gap limit, which
422 is always true for malloc's use of brk. reliance on this assumption is
423 why the check is being done in malloc-specific code and not in __brk.
425 commit bd1eaceaa3975bd2a2a34e211cff896affaecadf
426 Author: Rich Felker <dalias@aerifal.cx>
427 Date: Tue Jun 9 20:09:27 2015 +0000
429 fix spurious errors from pwd/grp functions when nscd backend is absent
431 for several pwd/grp functions, the only way the caller can distinguish
432 between a successful negative result ("no such user/group") and an
433 internal error is by clearing errno before the call and checking errno
434 afterwards. the nscd backend support code correctly simulated a
435 not-found response on systems where such a backend is not running, but
436 failed to restore errno.
438 this commit also fixed an outdated/incorrect comment.
440 commit 75ce4503950621b11fcc7f1fd1187dbcf3cde312
441 Author: Rich Felker <dalias@aerifal.cx>
442 Date: Sun Jun 7 20:55:23 2015 +0000
444 fix regression in pre-v7 arm on kernels with kuser helper removed
446 the arm atomics/TLS runtime selection code is called from
447 __set_thread_area and depends on having libc.auxv and __hwcap
448 available. commit 71f099cb7db821c51d8f39dfac622c61e54d794c moved the
449 first call to __set_thread_area to the top of dynamic linking stage 3,
450 before this data is made available, causing the runtime detection code
451 to always see __hwcap as zero and thereby select the atomics/TLS
452 implementations based on kuser helper.
454 upcoming work on superh will use similar runtime detection.
456 ideally this early-init code should be cleanly refactored and shared
457 between the dynamic linker and static-linked startup.
459 commit 32f3c4f70633488550c29a2444f819aafdf345ff
460 Author: Rich Felker <dalias@aerifal.cx>
461 Date: Sun Jun 7 03:09:16 2015 +0000
463 add multiple inclusion guard to locale_impl.h
465 commit 04b8360adbb6487f61aa0c00e53ec3a90a5a0d29
466 Author: Rich Felker <dalias@aerifal.cx>
467 Date: Sun Jun 7 02:59:49 2015 +0000
469 remove redefinition of MB_CUR_MAX in locale_impl.h
471 unless/until the byte-based C locale is implemented, defining
472 MB_CUR_MAX to 1 in the C locale is wrong. no internal code currently
473 uses the MB_CUR_MAX macro, but having it defined inconsistently is
474 error-prone. applications get the value from stdlib.h and were
477 commit 16bf466532d7328e971012b0731ad493b017ad29
478 Author: Rich Felker <dalias@aerifal.cx>
479 Date: Sat Jun 6 18:53:02 2015 +0000
481 make static C and C.UTF-8 locales available outside of newlocale
483 commit 312eea2ea4f4363fb01b73660c08bfcf43dd3bb4
484 Author: Rich Felker <dalias@aerifal.cx>
485 Date: Sat Jun 6 18:20:30 2015 +0000
487 remove another invalid skip of locking in ungetwc
489 commit 3d7e32d28dc9962e9efc1c317c5b44b5b2df3008
490 Author: Rich Felker <dalias@aerifal.cx>
491 Date: Sat Jun 6 18:16:22 2015 +0000
493 add macro version of ctype.h isascii function
495 presumably internal code (ungetwc and fputwc) was written assuming a
496 macro implementation existed; otherwise use of isascii is just a
499 commit 7e816a6487932cbb3cb71d94b609e50e81f4e5bf
500 Author: Rich Felker <dalias@aerifal.cx>
501 Date: Sat Jun 6 18:11:17 2015 +0000
503 remove invalid skip of locking in ungetwc
505 aside from being invalid, the early check only optimized the error
506 case, and likely pessimized the common case by separating the
507 two branches on isascii(c) at opposite ends of the function.
509 commit 63f4b9f18f3674124d8bcb119739fec85e6da005
510 Author: Timo Teräs <timo.teras@iki.fi>
511 Date: Fri Jun 5 10:39:42 2015 +0300
513 fix uselocale((locale_t)0) not to modify locale
515 commit 68630b55c0c7219fe9df70dc28ffbf9efc8021d8 made the new locale to
516 be assigned unconditonally resulting in crashes later on.
518 diff --git a/arch/arm/syscall_arch.h b/arch/arm/syscall_arch.h
519 index 199ad2a..64461ec 100644
520 --- a/arch/arm/syscall_arch.h
521 +++ b/arch/arm/syscall_arch.h
522 @@ -72,3 +72,7 @@ static inline long __syscall6(long n, long a, long b, long c, long d, long e, lo
523 register long r5 __asm__("r5") = f;
524 __asm_syscall("r"(r7), "0"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5));
528 +#define VDSO_CGT_SYM "__vdso_clock_gettime"
529 +#define VDSO_CGT_VER "LINUX_2.6"
530 diff --git a/arch/mips/pthread_arch.h b/arch/mips/pthread_arch.h
531 index f8e35ae..904a248 100644
532 --- a/arch/mips/pthread_arch.h
533 +++ b/arch/mips/pthread_arch.h
534 @@ -13,4 +13,6 @@ static inline struct pthread *__pthread_self()
536 #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + 0x7000)
538 +#define DTP_OFFSET 0x8000
540 #define CANCEL_REG_IP (3-(union {int __i; char __b;}){1}.__b)
541 diff --git a/arch/powerpc/pthread_arch.h b/arch/powerpc/pthread_arch.h
542 index 4115ec8..1cbfc22 100644
543 --- a/arch/powerpc/pthread_arch.h
544 +++ b/arch/powerpc/pthread_arch.h
545 @@ -12,6 +12,8 @@ static inline struct pthread *__pthread_self()
547 #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + 0x7000)
549 +#define DTP_OFFSET 0x8000
551 // offset of the PC register in mcontext_t, divided by the system wordsize
552 // the kernel calls the ip "nip", it's the first saved value after the 32
554 diff --git a/arch/sh/src/__set_thread_area.c b/arch/sh/src/__set_thread_area.c
556 index 0000000..1d3e022
558 +++ b/arch/sh/src/__set_thread_area.c
560 +#include "pthread_impl.h"
562 +#include "sh_atomic.h"
565 +/* Also perform sh-specific init */
567 +#define CPU_HAS_LLSC 0x0040
569 +__attribute__((__visibility__("hidden"))) unsigned __sh_atomic_model, __sh_nommu;
571 +int __set_thread_area(void *p)
574 + __asm__ __volatile__ ( "ldc %0, gbr" : : "r"(p) : "memory" );
576 + if (__hwcap & CPU_HAS_LLSC) {
577 + __sh_atomic_model = SH_A_LLSC;
580 +#if !defined(__SH3__) && !defined(__SH4__)
581 + for (aux=libc.auxv; *aux; aux+=2) {
582 + if (*aux != AT_PLATFORM) continue;
583 + const char *s = (void *)aux[1];
584 + if (s[0]!='s' || s[1]!='h' || s[2]!='2' || s[3]-'0'<10u) break;
585 + __sh_atomic_model = SH_A_IMASK;
590 + /* __sh_atomic_model = SH_A_GUSA; */ /* 0, default */
594 diff --git a/arch/sh/src/__unmapself.c b/arch/sh/src/__unmapself.c
596 index 0000000..4df9e7b
598 +++ b/arch/sh/src/__unmapself.c
600 +#include "pthread_impl.h"
602 +void __unmapself_sh_mmu(void *, size_t);
603 +void __unmapself_sh_nommu(void *, size_t);
605 +#if !defined(__SH3__) && !defined(__SH4__)
606 +#define __unmapself __unmapself_sh_nommu
607 +#include "../../../src/thread/__unmapself.c"
609 +extern __attribute__((__visibility__("hidden"))) unsigned __sh_nommu;
611 +#define __sh_nommu 0
614 +void __unmapself(void *base, size_t size)
616 + if (__sh_nommu) __unmapself_sh_nommu(base, size);
617 + else __unmapself_sh_mmu(base, size);
619 diff --git a/arch/sh/src/atomic.c b/arch/sh/src/atomic.c
620 index f8c615f..7fd7307 100644
621 --- a/arch/sh/src/atomic.c
622 +++ b/arch/sh/src/atomic.c
626 +#include "sh_atomic.h"
630 +static inline unsigned mask()
633 + __asm__ __volatile__ ( "\n"
638 + : "=&r"(sr) : : "memory", "r0" );
642 +static inline void unmask(unsigned sr)
644 + __asm__ __volatile__ ( "ldc %0,sr" : : "r"(sr) : "memory" );
647 /* gusa is a hack in the kernel which lets you create a sequence of instructions
648 * which will be restarted if the process is preempted in the middle of the
649 * sequence. It will do for implementing atomics on non-smp systems. ABI is:
651 " mov.l " new ", @" mem "\n" \
654 -#define CPU_HAS_LLSC 0x0040
656 int __sh_cas(volatile int *p, int t, int s)
658 - if (__hwcap & CPU_HAS_LLSC) return __sh_cas_llsc(p, t, s);
659 + if (__sh_atomic_model == SH_A_LLSC) return __sh_cas_llsc(p, t, s);
661 + if (__sh_atomic_model == SH_A_IMASK) {
662 + unsigned sr = mask();
664 + if (old==t) *p = s;
670 __asm__ __volatile__(
671 @@ -43,7 +67,15 @@ int __sh_cas(volatile int *p, int t, int s)
673 int __sh_swap(volatile int *x, int v)
675 - if (__hwcap & CPU_HAS_LLSC) return __sh_swap_llsc(x, v);
676 + if (__sh_atomic_model == SH_A_LLSC) return __sh_swap_llsc(x, v);
678 + if (__sh_atomic_model == SH_A_IMASK) {
679 + unsigned sr = mask();
687 __asm__ __volatile__(
688 @@ -55,7 +87,15 @@ int __sh_swap(volatile int *x, int v)
690 int __sh_fetch_add(volatile int *x, int v)
692 - if (__hwcap & CPU_HAS_LLSC) return __sh_fetch_add_llsc(x, v);
693 + if (__sh_atomic_model == SH_A_LLSC) return __sh_fetch_add_llsc(x, v);
695 + if (__sh_atomic_model == SH_A_IMASK) {
696 + unsigned sr = mask();
704 __asm__ __volatile__(
705 @@ -69,7 +109,7 @@ int __sh_fetch_add(volatile int *x, int v)
707 void __sh_store(volatile int *p, int x)
709 - if (__hwcap & CPU_HAS_LLSC) return __sh_store_llsc(p, x);
710 + if (__sh_atomic_model == SH_A_LLSC) return __sh_store_llsc(p, x);
711 __asm__ __volatile__(
713 : : "r"(p), "r"(x) : "memory");
714 @@ -77,7 +117,15 @@ void __sh_store(volatile int *p, int x)
716 void __sh_and(volatile int *x, int v)
718 - if (__hwcap & CPU_HAS_LLSC) return __sh_and_llsc(x, v);
719 + if (__sh_atomic_model == SH_A_LLSC) return __sh_and_llsc(x, v);
721 + if (__sh_atomic_model == SH_A_IMASK) {
722 + unsigned sr = mask();
730 __asm__ __volatile__(
731 @@ -89,7 +137,15 @@ void __sh_and(volatile int *x, int v)
733 void __sh_or(volatile int *x, int v)
735 - if (__hwcap & CPU_HAS_LLSC) return __sh_or_llsc(x, v);
736 + if (__sh_atomic_model == SH_A_LLSC) return __sh_or_llsc(x, v);
738 + if (__sh_atomic_model == SH_A_IMASK) {
739 + unsigned sr = mask();
747 __asm__ __volatile__(
748 diff --git a/arch/sh/src/sh_atomic.h b/arch/sh/src/sh_atomic.h
750 index 0000000..054c2a3
752 +++ b/arch/sh/src/sh_atomic.h
754 +#ifndef _SH_ATOMIC_H
755 +#define _SH_ATOMIC_H
760 +#if !defined(__SH3__) && !defined(__SH4__)
761 +#define SH_A_IMASK 3
763 +#define SH_A_IMASK -1LL /* unmatchable by unsigned int */
766 +extern __attribute__((__visibility__("hidden"))) unsigned __sh_atomic_model;
769 diff --git a/arch/sh/syscall_arch.h b/arch/sh/syscall_arch.h
770 index 7ee21a5..f63675a 100644
771 --- a/arch/sh/syscall_arch.h
772 +++ b/arch/sh/syscall_arch.h
775 #define __asm_syscall(trapno, ...) do { \
776 __asm__ __volatile__ ( \
777 - "trapa #" #trapno "\n" \
782 diff --git a/include/ctype.h b/include/ctype.h
783 index cd2e016..7936536 100644
784 --- a/include/ctype.h
785 +++ b/include/ctype.h
786 @@ -64,6 +64,7 @@ int isascii(int);
788 #define _tolower(a) ((a)|0x20)
789 #define _toupper(a) ((a)&0x5f)
790 +#define isascii(a) (0 ? isascii(a) : (unsigned)(a) < 128)
794 diff --git a/include/stdlib.h b/include/stdlib.h
795 index 97ce5a7..d2c911f 100644
796 --- a/include/stdlib.h
797 +++ b/include/stdlib.h
798 @@ -76,7 +76,8 @@ size_t wcstombs (char *__restrict, const wchar_t *__restrict, size_t);
799 #define EXIT_FAILURE 1
800 #define EXIT_SUCCESS 0
802 -#define MB_CUR_MAX ((size_t)+4)
803 +size_t __ctype_get_mb_cur_max(void);
804 +#define MB_CUR_MAX (__ctype_get_mb_cur_max())
806 #define RAND_MAX (0x7fffffff)
808 diff --git a/src/ctype/__ctype_get_mb_cur_max.c b/src/ctype/__ctype_get_mb_cur_max.c
809 index d235f4d..8e946fc 100644
810 --- a/src/ctype/__ctype_get_mb_cur_max.c
811 +++ b/src/ctype/__ctype_get_mb_cur_max.c
815 +#include "locale_impl.h"
817 size_t __ctype_get_mb_cur_max()
822 diff --git a/src/ctype/isascii.c b/src/ctype/isascii.c
823 index 3af0a10..54ad3bf 100644
824 --- a/src/ctype/isascii.c
825 +++ b/src/ctype/isascii.c
832 diff --git a/src/env/__stack_chk_fail.c b/src/env/__stack_chk_fail.c
833 index 47784c6..be0c184 100644
834 --- a/src/env/__stack_chk_fail.c
835 +++ b/src/env/__stack_chk_fail.c
836 @@ -25,4 +25,8 @@ void __stack_chk_fail_local(void)
842 +weak_alias(__stack_chk_fail, __stack_chk_fail_local);
845 diff --git a/src/internal/libc.h b/src/internal/libc.h
846 index 6810cd8..98c7535 100644
847 --- a/src/internal/libc.h
848 +++ b/src/internal/libc.h
849 @@ -17,8 +17,6 @@ struct __libc {
851 volatile int threads_minus_1;
854 - volatile int ofl_lock[2];
857 struct __locale_struct global_locale;
858 diff --git a/src/internal/locale_impl.h b/src/internal/locale_impl.h
859 index 9b8385e..f5e4d9b 100644
860 --- a/src/internal/locale_impl.h
861 +++ b/src/internal/locale_impl.h
863 +#ifndef _LOCALE_IMPL_H
864 +#define _LOCALE_IMPL_H
869 @@ -12,6 +15,10 @@ struct __locale_map {
870 const struct __locale_map *next;
873 +extern const struct __locale_map __c_dot_utf8;
874 +extern const struct __locale_struct __c_locale;
875 +extern const struct __locale_struct __c_dot_utf8_locale;
877 const struct __locale_map *__get_locale(int, const char *);
878 const char *__mo_lookup(const void *, size_t, const char *);
879 const char *__lctrans(const char *, const struct __locale_map *);
880 @@ -20,9 +27,14 @@ const char *__lctrans_cur(const char *);
881 #define LCTRANS(msg, lc, loc) __lctrans(msg, (loc)->cat[(lc)])
882 #define LCTRANS_CUR(msg) __lctrans_cur(msg)
884 +#define C_LOCALE ((locale_t)&__c_locale)
885 +#define UTF8_LOCALE ((locale_t)&__c_dot_utf8_locale)
887 #define CURRENT_LOCALE (__pthread_self()->locale)
889 #define CURRENT_UTF8 (!!__pthread_self()->locale->cat[LC_CTYPE])
892 #define MB_CUR_MAX (CURRENT_UTF8 ? 4 : 1)
895 diff --git a/src/internal/pthread_impl.h b/src/internal/pthread_impl.h
896 index e29f9c8..3890bb5 100644
897 --- a/src/internal/pthread_impl.h
898 +++ b/src/internal/pthread_impl.h
899 @@ -94,6 +94,10 @@ struct __timer {
900 #define CANARY canary
904 +#define DTP_OFFSET 0
909 #define SIGSYNCCALL 34
910 diff --git a/src/internal/sh/syscall.s b/src/internal/sh/syscall.s
911 index d00712a..331918a 100644
912 --- a/src/internal/sh/syscall.s
913 +++ b/src/internal/sh/syscall.s
914 @@ -13,7 +13,7 @@ __syscall:
923 diff --git a/src/internal/stdio_impl.h b/src/internal/stdio_impl.h
924 index e1325fe..0dd7fb5 100644
925 --- a/src/internal/stdio_impl.h
926 +++ b/src/internal/stdio_impl.h
927 @@ -47,6 +47,7 @@ struct _IO_FILE {
928 unsigned char *shend;
930 FILE *prev_locked, *next_locked;
931 + struct __locale_struct *locale;
934 size_t __stdio_read(FILE *, unsigned char *, size_t);
935 @@ -75,8 +76,9 @@ int __putc_unlocked(int, FILE *);
936 FILE *__fdopen(int, const char *);
937 int __fmodeflags(const char *);
939 -#define OFLLOCK() LOCK(libc.ofl_lock)
940 -#define OFLUNLOCK() UNLOCK(libc.ofl_lock)
941 +FILE *__ofl_add(FILE *f);
942 +FILE **__ofl_lock(void);
943 +void __ofl_unlock(void);
945 #define feof(f) ((f)->flags & F_EOF)
946 #define ferror(f) ((f)->flags & F_ERR)
947 diff --git a/src/ldso/dynlink.c b/src/ldso/dynlink.c
948 index 42b056d..d2a7249 100644
949 --- a/src/ldso/dynlink.c
950 +++ b/src/ldso/dynlink.c
951 @@ -337,7 +337,7 @@ static void do_relocs(struct dso *dso, size_t *rel, size_t rel_size, size_t stri
952 *reloc_addr = def.dso->tls_id;
955 - *reloc_addr = tls_val + addend;
956 + *reloc_addr = tls_val + addend - DTP_OFFSET;
960 @@ -423,6 +423,28 @@ static void reclaim_gaps(struct dso *dso)
964 +static void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t off)
966 + char *q = mmap(p, n, prot, flags, fd, off);
967 + if (q != MAP_FAILED || errno != EINVAL) return q;
968 + /* Fallbacks for MAP_FIXED failure on NOMMU kernels. */
969 + if (flags & MAP_ANONYMOUS) {
974 + if (lseek(fd, off, SEEK_SET) < 0) return MAP_FAILED;
975 + for (q=p; n; q+=r, off+=r, n-=r) {
976 + r = read(fd, q, n);
977 + if (r < 0 && errno != EINTR) return MAP_FAILED;
986 static void *map_library(int fd, struct dso *dso)
988 Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
989 @@ -524,19 +546,20 @@ static void *map_library(int fd, struct dso *dso)
990 prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
991 ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
992 ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
993 - if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
994 + if (mmap_fixed(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
996 if (ph->p_memsz > ph->p_filesz) {
997 size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
998 size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
999 memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
1000 - if (pgbrk-(size_t)base < this_max && mmap((void *)pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED)
1001 + if (pgbrk-(size_t)base < this_max && mmap_fixed((void *)pgbrk, (size_t)base+this_max-pgbrk, prot, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) == MAP_FAILED)
1005 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
1006 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
1007 - if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) < 0)
1008 + if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC)
1009 + && errno != ENOSYS)
1013 @@ -927,7 +950,8 @@ static void reloc_all(struct dso *p)
1014 do_relocs(p, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], 3);
1016 if (head != &ldso && p->relro_start != p->relro_end &&
1017 - mprotect(p->base+p->relro_start, p->relro_end-p->relro_start, PROT_READ) < 0) {
1018 + mprotect(p->base+p->relro_start, p->relro_end-p->relro_start, PROT_READ)
1019 + && errno != ENOSYS) {
1020 error("Error relocating %s: RELRO protection failed: %m",
1022 if (runtime) longjmp(*rtld_fail, 1);
1023 @@ -1078,7 +1102,7 @@ void *__tls_get_new(size_t *v)
1024 __block_all_sigs(&set);
1025 if (v[0]<=(size_t)self->dtv[0]) {
1026 __restore_sigs(&set);
1027 - return (char *)self->dtv[v[0]]+v[1];
1028 + return (char *)self->dtv[v[0]]+v[1]+DTP_OFFSET;
1031 /* This is safe without any locks held because, if the caller
1032 @@ -1111,7 +1135,7 @@ void *__tls_get_new(size_t *v)
1033 if (p->tls_id == v[0]) break;
1035 __restore_sigs(&set);
1036 - return mem + v[1];
1037 + return mem + v[1] + DTP_OFFSET;
1040 static void update_tls_size()
1041 @@ -1192,6 +1216,17 @@ _Noreturn void __dls3(size_t *sp)
1042 char **argv_orig = argv;
1043 char **envp = argv+argc+1;
1045 + /* Find aux vector just past environ[] and use it to initialize
1046 + * global data that may be needed before we can make syscalls. */
1048 + for (i=argc+1; argv[i]; i++);
1049 + libc.auxv = auxv = (void *)(argv+i+1);
1050 + decode_vec(auxv, aux, AUX_CNT);
1051 + __hwcap = aux[AT_HWCAP];
1052 + libc.page_size = aux[AT_PAGESZ];
1053 + libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
1054 + || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]);
1056 /* Setup early thread pointer in builtin_tls for ldso/libc itself to
1057 * use during dynamic linking. If possible it will also serve as the
1058 * thread pointer at runtime. */
1059 @@ -1200,25 +1235,11 @@ _Noreturn void __dls3(size_t *sp)
1063 - /* Find aux vector just past environ[] */
1064 - for (i=argc+1; argv[i]; i++)
1065 - if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
1066 - env_path = argv[i]+16;
1067 - else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
1068 - env_preload = argv[i]+11;
1069 - auxv = (void *)(argv+i+1);
1071 - decode_vec(auxv, aux, AUX_CNT);
1073 /* Only trust user/env if kernel says we're not suid/sgid */
1074 - if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
1075 - || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
1079 + if (!libc.secure) {
1080 + env_path = getenv("LD_LIBRARY_PATH");
1081 + env_preload = getenv("LD_PRELOAD");
1083 - libc.page_size = aux[AT_PAGESZ];
1086 /* If the main program was already loaded by the kernel,
1087 * AT_PHDR will point to some location other than the dynamic
1088 diff --git a/src/locale/c_locale.c b/src/locale/c_locale.c
1089 new file mode 100644
1090 index 0000000..77ccf58
1092 +++ b/src/locale/c_locale.c
1094 +#include "locale_impl.h"
1095 +#include <stdint.h>
1097 +static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };
1099 +const struct __locale_map __c_dot_utf8 = {
1101 + .map_size = sizeof empty_mo,
1105 +const struct __locale_struct __c_locale = { 0 };
1106 +const struct __locale_struct __c_dot_utf8_locale = {
1107 + .cat[LC_CTYPE] = &__c_dot_utf8
1109 diff --git a/src/locale/iconv.c b/src/locale/iconv.c
1110 index e6121ae..1eeea94 100644
1111 --- a/src/locale/iconv.c
1112 +++ b/src/locale/iconv.c
1117 +#include "locale_impl.h"
1119 #define UTF_32BE 0300
1120 #define UTF_16LE 0301
1121 @@ -165,9 +166,12 @@ size_t iconv(iconv_t cd0, char **restrict in, size_t *restrict inb, char **restr
1123 unsigned char type = map[-1];
1124 unsigned char totype = tomap[-1];
1125 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
1127 if (!in || !*in || !*inb) return 0;
1129 + *ploc = UTF8_LOCALE;
1131 for (; *inb; *in+=l, *inb-=l) {
1132 c = *(unsigned char *)*in;
1134 @@ -431,6 +435,7 @@ size_t iconv(iconv_t cd0, char **restrict in, size_t *restrict inb, char **restr
1142 @@ -445,5 +450,6 @@ starved:
1149 diff --git a/src/locale/langinfo.c b/src/locale/langinfo.c
1150 index a1ada24..776b447 100644
1151 --- a/src/locale/langinfo.c
1152 +++ b/src/locale/langinfo.c
1153 @@ -33,7 +33,8 @@ char *__nl_langinfo_l(nl_item item, locale_t loc)
1154 int idx = item & 65535;
1157 - if (item == CODESET) return "UTF-8";
1158 + if (item == CODESET)
1159 + return MB_CUR_MAX==1 ? "UTF-8-CODE-UNITS" : "UTF-8";
1163 diff --git a/src/locale/locale_map.c b/src/locale/locale_map.c
1164 index 4346bb0..c3e5917 100644
1165 --- a/src/locale/locale_map.c
1166 +++ b/src/locale/locale_map.c
1167 @@ -24,14 +24,6 @@ static const char envvars[][12] = {
1171 -static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };
1173 -const struct __locale_map __c_dot_utf8 = {
1175 - .map_size = sizeof empty_mo,
1179 const struct __locale_map *__get_locale(int cat, const char *val)
1182 @@ -107,8 +99,8 @@ const struct __locale_map *__get_locale(int cat, const char *val)
1183 * sake of being able to do message translations at the
1184 * application level. */
1185 if (!new && (new = malloc(sizeof *new))) {
1186 - new->map = empty_mo;
1187 - new->map_size = sizeof empty_mo;
1188 + new->map = __c_dot_utf8.map;
1189 + new->map_size = __c_dot_utf8.map_size;
1190 memcpy(new->name, val, n);
1192 new->next = loc_head;
1193 diff --git a/src/locale/newlocale.c b/src/locale/newlocale.c
1194 index 89d36b1..f50bbe9 100644
1195 --- a/src/locale/newlocale.c
1196 +++ b/src/locale/newlocale.c
1198 #include "locale_impl.h"
1201 -extern const struct __locale_map __c_dot_utf8;
1203 -static const struct __locale_struct c_locale = { 0 };
1204 -static const struct __locale_struct c_dot_utf8_locale = {
1205 - .cat[LC_CTYPE] = &__c_dot_utf8
1208 int __loc_is_allocated(locale_t loc)
1210 - return loc && loc != &c_locale && loc != &c_dot_utf8_locale;
1211 + return loc && loc != C_LOCALE && loc != UTF8_LOCALE;
1214 locale_t __newlocale(int mask, const char *name, locale_t loc)
1215 @@ -44,9 +37,9 @@ locale_t __newlocale(int mask, const char *name, locale_t loc)
1219 - return (locale_t)&c_locale;
1220 - if (j==1 && tmp.cat[LC_CTYPE]==c_dot_utf8_locale.cat[LC_CTYPE])
1221 - return (locale_t)&c_dot_utf8_locale;
1223 + if (j==1 && tmp.cat[LC_CTYPE]==&__c_dot_utf8)
1224 + return UTF8_LOCALE;
1226 if ((loc = malloc(sizeof *loc))) *loc = tmp;
1228 diff --git a/src/locale/uselocale.c b/src/locale/uselocale.c
1229 index b70a0c1..0fc5ecb 100644
1230 --- a/src/locale/uselocale.c
1231 +++ b/src/locale/uselocale.c
1232 @@ -8,9 +8,7 @@ locale_t __uselocale(locale_t new)
1233 locale_t old = self->locale;
1234 locale_t global = &libc.global_locale;
1236 - if (new == LC_GLOBAL_LOCALE) new = global;
1238 - self->locale = new;
1239 + if (new) self->locale = new == LC_GLOBAL_LOCALE ? global : new;
1241 return old == global ? LC_GLOBAL_LOCALE : old;
1243 diff --git a/src/malloc/calloc.c b/src/malloc/calloc.c
1244 index c3dfb47..436c0b0 100644
1245 --- a/src/malloc/calloc.c
1246 +++ b/src/malloc/calloc.c
1251 +void *__malloc0(size_t);
1253 void *calloc(size_t m, size_t n)
1257 if (n && m > (size_t)-1/n) {
1264 - /* Only do this for non-mmapped chunks */
1265 - if (((size_t *)p)[-1] & 7) {
1266 - /* Only write words that are not already zero */
1267 - m = (n + sizeof *z - 1)/sizeof *z;
1268 - for (z=p; m; m--, z++) if (*z) *z=0;
1271 + return __malloc0(n * m);
1273 diff --git a/src/malloc/expand_heap.c b/src/malloc/expand_heap.c
1274 new file mode 100644
1275 index 0000000..d8c0be7
1277 +++ b/src/malloc/expand_heap.c
1279 +#include <limits.h>
1280 +#include <stdint.h>
1282 +#include <sys/mman.h>
1284 +#include "syscall.h"
1286 +/* This function returns true if the interval [old,new]
1287 + * intersects the 'len'-sized interval below &libc.auxv
1288 + * (interpreted as the main-thread stack) or below &b
1289 + * (the current stack). It is used to defend against
1290 + * buggy brk implementations that can cross the stack. */
1292 +static int traverses_stack_p(uintptr_t old, uintptr_t new)
1294 + const uintptr_t len = 8<<20;
1297 + b = (uintptr_t)libc.auxv;
1298 + a = b > len ? b-len : 0;
1299 + if (new>a && old<b) return 1;
1301 + b = (uintptr_t)&b;
1302 + a = b > len ? b-len : 0;
1303 + if (new>a && old<b) return 1;
1308 +void *__mmap(void *, size_t, int, int, int, off_t);
1310 +/* Expand the heap in-place if brk can be used, or otherwise via mmap,
1311 + * using an exponential lower bound on growth by mmap to make
1312 + * fragmentation asymptotically irrelevant. The size argument is both
1313 + * an input and an output, since the caller needs to know the size
1314 + * allocated, which will be larger than requested due to page alignment
1315 + * and mmap minimum size rules. The caller is responsible for locking
1316 + * to prevent concurrent calls. */
1318 +void *__expand_heap(size_t *pn)
1320 + static uintptr_t brk;
1321 + static unsigned mmap_step;
1324 + if (n > SIZE_MAX/2 - PAGE_SIZE) {
1328 + n += -n & PAGE_SIZE-1;
1331 + brk = __syscall(SYS_brk, 0);
1332 + brk += -brk & PAGE_SIZE-1;
1335 + if (n < SIZE_MAX-brk && !traverses_stack_p(brk, brk+n)
1336 + && __syscall(SYS_brk, brk+n)==brk+n) {
1339 + return (void *)(brk-n);
1342 + size_t min = (size_t)PAGE_SIZE << mmap_step/2;
1343 + if (n < min) n = min;
1344 + void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
1345 + MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1346 + if (area == MAP_FAILED) return 0;
1351 diff --git a/src/malloc/lite_malloc.c b/src/malloc/lite_malloc.c
1352 index 7643fc2..09ac575 100644
1353 --- a/src/malloc/lite_malloc.c
1354 +++ b/src/malloc/lite_malloc.c
1359 -uintptr_t __brk(uintptr_t);
1363 +void *__expand_heap(size_t *);
1365 void *__simple_malloc(size_t n)
1367 - static uintptr_t cur, brk;
1368 - uintptr_t base, new;
1369 + static char *cur, *end;
1370 static volatile int lock[2];
1372 + size_t align=1, pad;
1376 - if (n > SIZE_MAX/2) goto toobig;
1378 while (align<n && align<ALIGN)
1380 - n = n + align - 1 & -align;
1383 - if (!cur) cur = brk = __brk(0)+16;
1384 - base = cur + align-1 & -align;
1385 - if (n > SIZE_MAX - PAGE_SIZE - base) goto fail;
1386 - if (base+n > brk) {
1387 - new = base+n + PAGE_SIZE-1 & -PAGE_SIZE;
1388 - if (__brk(new) != new) goto fail;
1394 - return (void *)base;
1395 + pad = -(uintptr_t)cur & align-1;
1397 + if (n <= SIZE_MAX/2 + ALIGN) n += pad;
1399 + if (n > end-cur) {
1401 + char *new = __expand_heap(&m);
1424 weak_alias(__simple_malloc, malloc);
1425 +weak_alias(__simple_malloc, __malloc0);
1426 diff --git a/src/malloc/malloc.c b/src/malloc/malloc.c
1427 index d4de2dc..eb68d55 100644
1428 --- a/src/malloc/malloc.c
1429 +++ b/src/malloc/malloc.c
1431 #define inline inline __attribute__((always_inline))
1434 -uintptr_t __brk(uintptr_t);
1435 void *__mmap(void *, size_t, int, int, int, off_t);
1436 int __munmap(void *, size_t);
1437 void *__mremap(void *, size_t, size_t, int, ...);
1438 @@ -31,13 +30,9 @@ struct bin {
1444 volatile uint64_t binmap;
1445 struct bin bins[64];
1446 - volatile int brk_lock[2];
1447 volatile int free_lock[2];
1448 - unsigned mmap_step;
1452 @@ -152,69 +147,52 @@ void __dump_heap(int x)
1456 +void *__expand_heap(size_t *);
1458 static struct chunk *expand_heap(size_t n)
1461 + static int heap_lock[2];
1467 - lock(mal.brk_lock);
1470 - mal.brk = __brk(0);
1472 - mal.brk = mal.brk + PAGE_SIZE-1 & -PAGE_SIZE;
1474 - mal.brk = mal.brk + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
1475 - mal.heap = (void *)mal.brk;
1478 + /* The argument n already accounts for the caller's chunk
1479 + * overhead needs, but if the heap can't be extended in-place,
1480 + * we need room for an extra zero-sized sentinel chunk. */
1483 - if (n > SIZE_MAX - mal.brk - 2*PAGE_SIZE) goto fail;
1484 - new = mal.brk + n + SIZE_ALIGN + PAGE_SIZE - 1 & -PAGE_SIZE;
1485 - n = new - mal.brk;
1488 - if (__brk(new) != new) {
1489 - size_t min = (size_t)PAGE_SIZE << mal.mmap_step/2;
1490 - n += -n & PAGE_SIZE-1;
1491 - if (n < min) n = min;
1492 - void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
1493 - MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1494 - if (area == MAP_FAILED) goto fail;
1495 + p = __expand_heap(&n);
1497 + unlock(heap_lock);
1502 - area = (char *)area + SIZE_ALIGN - OVERHEAD;
1504 + /* If not just expanding existing space, we need to make a
1505 + * new sentinel chunk below the allocated space. */
1507 + /* Valid/safe because of the prologue increment. */
1509 + p = (char *)p + SIZE_ALIGN;
1510 + w = MEM_TO_CHUNK(p);
1511 w->psize = 0 | C_INUSE;
1512 - w->csize = n | C_INUSE;
1513 - w = NEXT_CHUNK(w);
1514 - w->psize = n | C_INUSE;
1515 - w->csize = 0 | C_INUSE;
1517 - unlock(mal.brk_lock);
1522 - w = MEM_TO_CHUNK(mal.heap);
1523 - w->psize = 0 | C_INUSE;
1525 - w = MEM_TO_CHUNK(new);
1526 + /* Record new heap end and fill in footer. */
1527 + end = (char *)p + n;
1528 + w = MEM_TO_CHUNK(end);
1529 w->psize = n | C_INUSE;
1530 w->csize = 0 | C_INUSE;
1532 - w = MEM_TO_CHUNK(mal.brk);
1533 + /* Fill in header, which may be new or may be replacing a
1534 + * zero-size sentinel header at the old end-of-heap. */
1535 + w = MEM_TO_CHUNK(p);
1536 w->csize = n | C_INUSE;
1539 - unlock(mal.brk_lock);
1541 + unlock(heap_lock);
1545 - unlock(mal.brk_lock);
1550 static int adjust_size(size_t *n)
1551 @@ -378,6 +356,17 @@ void *malloc(size_t n)
1552 return CHUNK_TO_MEM(c);
1555 +void *__malloc0(size_t n)
1557 + void *p = malloc(n);
1558 + if (p && !IS_MMAPPED(MEM_TO_CHUNK(p))) {
1560 + n = (n + sizeof *z - 1)/sizeof *z;
1561 + for (z=p; n; n--, z++) if (*z) *z=0;
1566 void *realloc(void *p, size_t n)
1568 struct chunk *self, *next;
1569 diff --git a/src/multibyte/btowc.c b/src/multibyte/btowc.c
1570 index 9d2c3b1..8acd0a2 100644
1571 --- a/src/multibyte/btowc.c
1572 +++ b/src/multibyte/btowc.c
1576 +#include <stdlib.h>
1577 +#include "internal.h"
1581 - return c<128U ? c : EOF;
1582 + int b = (unsigned char)c;
1583 + return b<128U ? b : (MB_CUR_MAX==1 && c!=EOF) ? CODEUNIT(c) : WEOF;
1585 diff --git a/src/multibyte/internal.h b/src/multibyte/internal.h
1586 index cc017fa..53d62ed 100644
1587 --- a/src/multibyte/internal.h
1588 +++ b/src/multibyte/internal.h
1589 @@ -23,3 +23,10 @@ extern const uint32_t bittab[];
1594 +/* Arbitrary encoding for representing code units instead of characters. */
1595 +#define CODEUNIT(c) (0xdfff & (signed char)(c))
1596 +#define IS_CODEUNIT(c) ((unsigned)(c)-0xdf80 < 0x80)
1598 +/* Get inline definition of MB_CUR_MAX. */
1599 +#include "locale_impl.h"
1600 diff --git a/src/multibyte/mbrtowc.c b/src/multibyte/mbrtowc.c
1601 index e7b3654..ca7da70 100644
1602 --- a/src/multibyte/mbrtowc.c
1603 +++ b/src/multibyte/mbrtowc.c
1608 +#include <stdlib.h>
1611 #include "internal.h"
1612 @@ -27,6 +28,7 @@ size_t mbrtowc(wchar_t *restrict wc, const char *restrict src, size_t n, mbstate
1615 if (*s < 0x80) return !!(*wc = *s);
1616 + if (MB_CUR_MAX==1) return (*wc = CODEUNIT(*s)), 1;
1617 if (*s-SA > SB-SA) goto ilseq;
1618 c = bittab[*s++-SA]; n--;
1620 diff --git a/src/multibyte/mbsrtowcs.c b/src/multibyte/mbsrtowcs.c
1621 index 3c1343a..e23083d 100644
1622 --- a/src/multibyte/mbsrtowcs.c
1623 +++ b/src/multibyte/mbsrtowcs.c
1628 +#include <string.h>
1629 +#include <stdlib.h>
1630 #include "internal.h"
1632 size_t mbsrtowcs(wchar_t *restrict ws, const char **restrict src, size_t wn, mbstate_t *restrict st)
1633 @@ -24,6 +26,23 @@ size_t mbsrtowcs(wchar_t *restrict ws, const char **restrict src, size_t wn, mbs
1637 + if (MB_CUR_MAX==1) {
1638 + if (!ws) return strlen((const char *)s);
1641 + *src = (const void *)s;
1646 + *ws++ = CODEUNIT(c);
1655 if (*s-1u < 0x7f && (uintptr_t)s%4 == 0) {
1656 while (!(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
1657 diff --git a/src/multibyte/mbtowc.c b/src/multibyte/mbtowc.c
1658 index 803d221..71a9506 100644
1659 --- a/src/multibyte/mbtowc.c
1660 +++ b/src/multibyte/mbtowc.c
1665 +#include <stdlib.h>
1668 #include "internal.h"
1669 @@ -19,6 +20,7 @@ int mbtowc(wchar_t *restrict wc, const char *restrict src, size_t n)
1670 if (!wc) wc = &dummy;
1672 if (*s < 0x80) return !!(*wc = *s);
1673 + if (MB_CUR_MAX==1) return (*wc = CODEUNIT(*s)), 1;
1674 if (*s-SA > SB-SA) goto ilseq;
1675 c = bittab[*s++-SA];
1677 diff --git a/src/multibyte/wcrtomb.c b/src/multibyte/wcrtomb.c
1678 index 59f733d..ddc37a5 100644
1679 --- a/src/multibyte/wcrtomb.c
1680 +++ b/src/multibyte/wcrtomb.c
1685 +#include <stdlib.h>
1688 +#include "internal.h"
1690 size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict st)
1692 @@ -13,6 +15,13 @@ size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict st)
1693 if ((unsigned)wc < 0x80) {
1696 + } else if (MB_CUR_MAX == 1) {
1697 + if (!IS_CODEUNIT(wc)) {
1703 } else if ((unsigned)wc < 0x800) {
1704 *s++ = 0xc0 | (wc>>6);
1705 *s = 0x80 | (wc&0x3f);
1706 diff --git a/src/multibyte/wctob.c b/src/multibyte/wctob.c
1707 index d6353ee..4aeda6a 100644
1708 --- a/src/multibyte/wctob.c
1709 +++ b/src/multibyte/wctob.c
1713 +#include <stdlib.h>
1714 +#include "internal.h"
1718 if (c < 128U) return c;
1719 + if (MB_CUR_MAX==1 && IS_CODEUNIT(c)) return (unsigned char)c;
1722 diff --git a/src/passwd/nscd_query.c b/src/passwd/nscd_query.c
1723 index 69a7815..d38e371 100644
1724 --- a/src/passwd/nscd_query.c
1725 +++ b/src/passwd/nscd_query.c
1726 @@ -32,6 +32,7 @@ FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *
1730 + int errno_save = errno;
1734 @@ -50,11 +51,14 @@ retry:
1737 if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
1738 - /* If there isn't a running nscd we return -1 to indicate that
1739 - * that is precisely what happened
1741 - if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT)
1742 + /* If there isn't a running nscd we simulate a "not found"
1743 + * result and the caller is responsible for calling
1744 + * fclose on the (unconnected) socket. The value of
1745 + * errno must be left unchanged in this case. */
1746 + if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) {
1747 + errno = errno_save;
1753 diff --git a/src/process/sh/vfork.s b/src/process/sh/vfork.s
1754 new file mode 100644
1755 index 0000000..48cc939
1757 +++ b/src/process/sh/vfork.s
1761 +.type __vfork,@function
1762 +.type vfork,@function
1780 + .hidden __syscall_ret
1781 +1: .long __syscall_ret@PLT-(2b+4-.)
1782 diff --git a/src/regex/fnmatch.c b/src/regex/fnmatch.c
1783 index 7f6b65f..978fff8 100644
1784 --- a/src/regex/fnmatch.c
1785 +++ b/src/regex/fnmatch.c
1790 +#include "locale_impl.h"
1793 #define UNMATCHABLE -2
1794 @@ -229,7 +230,7 @@ static int fnmatch_internal(const char *pat, size_t m, const char *str, size_t n
1795 * On illegal sequences we may get it wrong, but in that case
1796 * we necessarily have a matching failure anyway. */
1797 for (s=endstr; s>str && tailcnt; tailcnt--) {
1798 - if (s[-1] < 128U) s--;
1799 + if (s[-1] < 128U || MB_CUR_MAX==1) s--;
1800 else while ((unsigned char)*--s-0x80U<0x40 && s>str);
1802 if (tailcnt) return FNM_NOMATCH;
1803 diff --git a/src/signal/sh/restore.s b/src/signal/sh/restore.s
1804 index ab26034..eaedcdf 100644
1805 --- a/src/signal/sh/restore.s
1806 +++ b/src/signal/sh/restore.s
1808 .type __restore, @function
1810 mov #119, r3 !__NR_sigreturn
1816 @@ -15,7 +15,7 @@ __restore:
1818 mov #100, r3 !__NR_rt_sigreturn
1825 diff --git a/src/stdio/__fdopen.c b/src/stdio/__fdopen.c
1826 index ef8f47d..8d6ce81 100644
1827 --- a/src/stdio/__fdopen.c
1828 +++ b/src/stdio/__fdopen.c
1829 @@ -54,13 +54,7 @@ FILE *__fdopen(int fd, const char *mode)
1830 if (!libc.threaded) f->lock = -1;
1832 /* Add new FILE to open file list */
1834 - f->next = libc.ofl_head;
1835 - if (libc.ofl_head) libc.ofl_head->prev = f;
1836 - libc.ofl_head = f;
1840 + return __ofl_add(f);
1843 weak_alias(__fdopen, fdopen);
1844 diff --git a/src/stdio/__stdio_exit.c b/src/stdio/__stdio_exit.c
1845 index 716e5f7..191b445 100644
1846 --- a/src/stdio/__stdio_exit.c
1847 +++ b/src/stdio/__stdio_exit.c
1848 @@ -16,8 +16,7 @@ static void close_file(FILE *f)
1849 void __stdio_exit(void)
1853 - for (f=libc.ofl_head; f; f=f->next) close_file(f);
1854 + for (f=*__ofl_lock(); f; f=f->next) close_file(f);
1855 close_file(__stdin_used);
1856 close_file(__stdout_used);
1858 diff --git a/src/stdio/__stdio_read.c b/src/stdio/__stdio_read.c
1859 index 5947344..f8fa6d3 100644
1860 --- a/src/stdio/__stdio_read.c
1861 +++ b/src/stdio/__stdio_read.c
1863 #include "stdio_impl.h"
1864 #include <sys/uio.h>
1865 -#include <pthread.h>
1867 -static void cleanup(void *p)
1870 - if (!f->lockcount) __unlockfile(f);
1873 size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
1875 @@ -16,9 +9,7 @@ size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
1879 - pthread_cleanup_push(cleanup, f);
1880 - cnt = syscall_cp(SYS_readv, f->fd, iov, 2);
1881 - pthread_cleanup_pop(0);
1882 + cnt = syscall(SYS_readv, f->fd, iov, 2);
1884 f->flags |= F_EOF ^ ((F_ERR^F_EOF) & cnt);
1886 diff --git a/src/stdio/__stdio_write.c b/src/stdio/__stdio_write.c
1887 index 8c89389..d2d8947 100644
1888 --- a/src/stdio/__stdio_write.c
1889 +++ b/src/stdio/__stdio_write.c
1891 #include "stdio_impl.h"
1892 #include <sys/uio.h>
1893 -#include <pthread.h>
1895 -static void cleanup(void *p)
1898 - if (!f->lockcount) __unlockfile(f);
1901 size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
1903 @@ -19,9 +12,7 @@ size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
1907 - pthread_cleanup_push(cleanup, f);
1908 - cnt = syscall_cp(SYS_writev, f->fd, iov, iovcnt);
1909 - pthread_cleanup_pop(0);
1910 + cnt = syscall(SYS_writev, f->fd, iov, iovcnt);
1912 f->wend = f->buf + f->buf_size;
1913 f->wpos = f->wbase = f->buf;
1914 @@ -34,11 +25,8 @@ size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
1917 if (cnt > iov[0].iov_len) {
1918 - f->wpos = f->wbase = f->buf;
1919 cnt -= iov[0].iov_len;
1921 - } else if (iovcnt == 2) {
1924 iov[0].iov_base = (char *)iov[0].iov_base + cnt;
1925 iov[0].iov_len -= cnt;
1926 diff --git a/src/stdio/fclose.c b/src/stdio/fclose.c
1927 index 317b3c9..839d88a 100644
1928 --- a/src/stdio/fclose.c
1929 +++ b/src/stdio/fclose.c
1930 @@ -14,11 +14,11 @@ int fclose(FILE *f)
1931 __unlist_locked_file(f);
1933 if (!(perm = f->flags & F_PERM)) {
1935 + FILE **head = __ofl_lock();
1936 if (f->prev) f->prev->next = f->next;
1937 if (f->next) f->next->prev = f->prev;
1938 - if (libc.ofl_head == f) libc.ofl_head = f->next;
1940 + if (*head == f) *head = f->next;
1945 diff --git a/src/stdio/fflush.c b/src/stdio/fflush.c
1946 index 7bf862a..3f462c8 100644
1947 --- a/src/stdio/fflush.c
1948 +++ b/src/stdio/fflush.c
1949 @@ -35,13 +35,12 @@ int fflush(FILE *f)
1951 r = __stdout_used ? fflush(__stdout_used) : 0;
1954 - for (f=libc.ofl_head; f; f=f->next) {
1955 + for (f=*__ofl_lock(); f; f=f->next) {
1957 if (f->wpos > f->wbase) r |= __fflush_unlocked(f);
1965 diff --git a/src/stdio/fgetwc.c b/src/stdio/fgetwc.c
1966 index 8626d54..e455cfe 100644
1967 --- a/src/stdio/fgetwc.c
1968 +++ b/src/stdio/fgetwc.c
1970 #include "stdio_impl.h"
1971 +#include "locale_impl.h"
1975 -wint_t __fgetwc_unlocked(FILE *f)
1976 +static wint_t __fgetwc_unlocked_internal(FILE *f)
1978 mbstate_t st = { 0 };
1980 @@ -10,8 +11,6 @@ wint_t __fgetwc_unlocked(FILE *f)
1984 - f->mode |= f->mode+1;
1986 /* Convert character from buffer if possible */
1987 if (f->rpos < f->rend) {
1988 l = mbrtowc(&wc, (void *)f->rpos, f->rend - f->rpos, &st);
1989 @@ -39,6 +38,16 @@ wint_t __fgetwc_unlocked(FILE *f)
1993 +wint_t __fgetwc_unlocked(FILE *f)
1995 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
1996 + if (f->mode <= 0) fwide(f, 1);
1997 + *ploc = f->locale;
1998 + wchar_t wc = __fgetwc_unlocked_internal(f);
2003 wint_t fgetwc(FILE *f)
2006 diff --git a/src/stdio/fmemopen.c b/src/stdio/fmemopen.c
2007 index d784960..7c193a5 100644
2008 --- a/src/stdio/fmemopen.c
2009 +++ b/src/stdio/fmemopen.c
2010 @@ -110,11 +110,5 @@ FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode)
2012 if (!libc.threaded) f->lock = -1;
2015 - f->next = libc.ofl_head;
2016 - if (libc.ofl_head) libc.ofl_head->prev = f;
2017 - libc.ofl_head = f;
2021 + return __ofl_add(f);
2023 diff --git a/src/stdio/fopen.c b/src/stdio/fopen.c
2024 index 07bdb6e..252f082 100644
2025 --- a/src/stdio/fopen.c
2026 +++ b/src/stdio/fopen.c
2027 @@ -18,7 +18,7 @@ FILE *fopen(const char *restrict filename, const char *restrict mode)
2028 /* Compute the flags to pass to open() */
2029 flags = __fmodeflags(mode);
2031 - fd = sys_open_cp(filename, flags, 0666);
2032 + fd = sys_open(filename, flags, 0666);
2033 if (fd < 0) return 0;
2034 if (flags & O_CLOEXEC)
2035 __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
2036 diff --git a/src/stdio/fputwc.c b/src/stdio/fputwc.c
2037 index 7b621dd..789fe9c 100644
2038 --- a/src/stdio/fputwc.c
2039 +++ b/src/stdio/fputwc.c
2041 #include "stdio_impl.h"
2042 +#include "locale_impl.h"
2046 @@ -7,8 +8,10 @@ wint_t __fputwc_unlocked(wchar_t c, FILE *f)
2048 char mbc[MB_LEN_MAX];
2050 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
2052 - f->mode |= f->mode+1;
2053 + if (f->mode <= 0) fwide(f, 1);
2054 + *ploc = f->locale;
2057 c = putc_unlocked(c, f);
2058 @@ -20,6 +23,8 @@ wint_t __fputwc_unlocked(wchar_t c, FILE *f)
2060 if (l < 0 || __fwritex((void *)mbc, l, f) < l) c = WEOF;
2062 + if (c==WEOF) f->flags |= F_ERR;
2067 diff --git a/src/stdio/fputws.c b/src/stdio/fputws.c
2068 index 5723cbc..0ed02f1 100644
2069 --- a/src/stdio/fputws.c
2070 +++ b/src/stdio/fputws.c
2072 #include "stdio_impl.h"
2073 +#include "locale_impl.h"
2076 int fputws(const wchar_t *restrict ws, FILE *restrict f)
2078 unsigned char buf[BUFSIZ];
2080 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
2084 - f->mode |= f->mode+1;
2086 + *ploc = f->locale;
2088 while (ws && (l = wcsrtombs((void *)buf, (void*)&ws, sizeof buf, 0))+1 > 1)
2089 if (__fwritex(buf, l, f) < l) {
2098 return l; /* 0 or -1 */
2101 diff --git a/src/stdio/fwide.c b/src/stdio/fwide.c
2102 index 8088e7a..8410b15 100644
2103 --- a/src/stdio/fwide.c
2104 +++ b/src/stdio/fwide.c
2107 #include "stdio_impl.h"
2109 -#define SH (8*sizeof(int)-1)
2110 -#define NORMALIZE(x) ((x)>>SH | -((-(x))>>SH))
2111 +#include "locale_impl.h"
2113 int fwide(FILE *f, int mode)
2116 - if (!f->mode) f->mode = NORMALIZE(mode);
2118 + if (!f->locale) f->locale = MB_CUR_MAX==1
2119 + ? C_LOCALE : UTF8_LOCALE;
2120 + if (!f->mode) f->mode = mode>0 ? 1 : -1;
2125 diff --git a/src/stdio/ofl.c b/src/stdio/ofl.c
2126 new file mode 100644
2127 index 0000000..b143999
2129 +++ b/src/stdio/ofl.c
2131 +#include "stdio_impl.h"
2134 +static FILE *ofl_head;
2135 +static volatile int ofl_lock[2];
2137 +FILE **__ofl_lock()
2143 +void __ofl_unlock()
2147 diff --git a/src/stdio/ofl_add.c b/src/stdio/ofl_add.c
2148 new file mode 100644
2149 index 0000000..d7de9f1
2151 +++ b/src/stdio/ofl_add.c
2153 +#include "stdio_impl.h"
2155 +FILE *__ofl_add(FILE *f)
2157 + FILE **head = __ofl_lock();
2159 + if (*head) (*head)->prev = f;
2164 diff --git a/src/stdio/open_memstream.c b/src/stdio/open_memstream.c
2165 index 9eafdfb..58504c9 100644
2166 --- a/src/stdio/open_memstream.c
2167 +++ b/src/stdio/open_memstream.c
2168 @@ -79,11 +79,5 @@ FILE *open_memstream(char **bufp, size_t *sizep)
2170 if (!libc.threaded) f->lock = -1;
2173 - f->next = libc.ofl_head;
2174 - if (libc.ofl_head) libc.ofl_head->prev = f;
2175 - libc.ofl_head = f;
2179 + return __ofl_add(f);
2181 diff --git a/src/stdio/open_wmemstream.c b/src/stdio/open_wmemstream.c
2182 index 3537030..7ab2c64 100644
2183 --- a/src/stdio/open_wmemstream.c
2184 +++ b/src/stdio/open_wmemstream.c
2185 @@ -81,11 +81,5 @@ FILE *open_wmemstream(wchar_t **bufp, size_t *sizep)
2187 if (!libc.threaded) f->lock = -1;
2190 - f->next = libc.ofl_head;
2191 - if (libc.ofl_head) libc.ofl_head->prev = f;
2192 - libc.ofl_head = f;
2196 + return __ofl_add(f);
2198 diff --git a/src/stdio/ungetwc.c b/src/stdio/ungetwc.c
2199 index 913f716..80d6e20 100644
2200 --- a/src/stdio/ungetwc.c
2201 +++ b/src/stdio/ungetwc.c
2203 #include "stdio_impl.h"
2204 +#include "locale_impl.h"
2208 @@ -8,21 +9,19 @@ wint_t ungetwc(wint_t c, FILE *f)
2210 unsigned char mbc[MB_LEN_MAX];
2213 - if (c == WEOF) return c;
2215 - /* Try conversion early so we can fail without locking if invalid */
2216 - if (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)
2218 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
2222 - f->mode |= f->mode+1;
2223 + if (f->mode <= 0) fwide(f, 1);
2224 + *ploc = f->locale;
2226 if (!f->rpos) __toread(f);
2227 - if (!f->rpos || f->rpos < f->buf - UNGET + l) {
2228 + if (!f->rpos || f->rpos < f->buf - UNGET + l || c == WEOF ||
2229 + (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)) {
2236 if (isascii(c)) *--f->rpos = c;
2237 @@ -31,5 +30,6 @@ wint_t ungetwc(wint_t c, FILE *f)
2244 diff --git a/src/stdio/vfwprintf.c b/src/stdio/vfwprintf.c
2245 index ebdff00..f06d5ae 100644
2246 --- a/src/stdio/vfwprintf.c
2247 +++ b/src/stdio/vfwprintf.c
2248 @@ -293,7 +293,10 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
2249 if ((fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
2253 + arg.p = strerror(errno);
2255 + if (!arg.p) arg.p = "(null)";
2257 if (p<0) p = INT_MAX;
2258 for (i=l=0; l<p && (i=mbtowc(&wc, bs, MB_LEN_MAX))>0; bs+=i, l++);
2259 @@ -356,7 +359,7 @@ int vfwprintf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
2263 - f->mode |= f->mode+1;
2265 olderr = f->flags & F_ERR;
2267 ret = wprintf_core(f, fmt, &ap2, nl_arg, nl_type);
2268 diff --git a/src/stdio/vfwscanf.c b/src/stdio/vfwscanf.c
2269 index ac5c2c2..223aad4 100644
2270 --- a/src/stdio/vfwscanf.c
2271 +++ b/src/stdio/vfwscanf.c
2272 @@ -104,7 +104,7 @@ int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
2276 - f->mode |= f->mode+1;
2279 for (p=fmt; *p; p++) {
2281 diff --git a/src/string/strverscmp.c b/src/string/strverscmp.c
2282 index 6f37cc6..4daf276 100644
2283 --- a/src/string/strverscmp.c
2284 +++ b/src/string/strverscmp.c
2289 -int strverscmp(const char *l, const char *r)
2290 +int strverscmp(const char *l0, const char *r0)
2294 - if (!*l) return 0;
2295 + const unsigned char *l = (const void *)l0;
2296 + const unsigned char *r = (const void *)r0;
2304 - } else if (isdigit(*l)) {
2312 + /* Find maximal matching prefix and track its maximal digit
2313 + * suffix and whether those digits are all zeros. */
2314 + for (dp=i=0; l[i]==r[i]; i++) {
2317 + if (!isdigit(c)) dp=i+1, z=1;
2318 + else if (c!='0') z=0;
2320 - if (haszero==1 && (*l=='0' || *r=='0')) {
2323 - if ((isdigit(*l) && isdigit(*r) ) && haszero) {
2324 - size_t lenl=0, lenr=0;
2325 - while (isdigit(l[lenl]) ) lenl++;
2326 - while (isdigit(r[lenr]) ) lenr++;
2329 - } else if (lenl>lenr) {
2337 + if (l[dp]!='0' && r[dp]!='0') {
2338 + /* If we're not looking at a digit sequence that began
2339 + * with a zero, longest digit string is greater. */
2340 + for (j=i; isdigit(l[j]); j++)
2341 + if (!isdigit(r[j])) return 1;
2342 + if (isdigit(r[j])) return -1;
2343 + } else if (z && dp<i && (isdigit(l[i]) || isdigit(r[i]))) {
2344 + /* Otherwise, if common prefix of digit sequence is
2345 + * all zeros, digits order less than non-digits. */
2346 + return (unsigned char)(l[i]-'0') - (unsigned char)(r[i]-'0');
2349 + return l[i] - r[i];
2351 diff --git a/src/thread/__tls_get_addr.c b/src/thread/__tls_get_addr.c
2352 index 3633396..84a413d 100644
2353 --- a/src/thread/__tls_get_addr.c
2354 +++ b/src/thread/__tls_get_addr.c
2355 @@ -8,9 +8,9 @@ void *__tls_get_addr(size_t *v)
2356 __attribute__((__visibility__("hidden")))
2357 void *__tls_get_new(size_t *);
2358 if (v[0]<=(size_t)self->dtv[0])
2359 - return (char *)self->dtv[v[0]]+v[1];
2360 + return (char *)self->dtv[v[0]]+v[1]+DTP_OFFSET;
2361 return __tls_get_new(v);
2363 - return (char *)self->dtv[1]+v[1];
2364 + return (char *)self->dtv[1]+v[1]+DTP_OFFSET;
2367 diff --git a/src/thread/__unmapself.c b/src/thread/__unmapself.c
2368 index e69de29..1d3bee1 100644
2369 --- a/src/thread/__unmapself.c
2370 +++ b/src/thread/__unmapself.c
2372 +#include "pthread_impl.h"
2373 +#include "atomic.h"
2374 +#include "syscall.h"
2375 +/* cheat and reuse CRTJMP macro from dynlink code */
2376 +#include "dynlink.h"
2378 +static volatile int lock;
2379 +static void *unmap_base;
2380 +static size_t unmap_size;
2381 +static char shared_stack[256];
2383 +static void do_unmap()
2385 + __syscall(SYS_munmap, unmap_base, unmap_size);
2386 + __syscall(SYS_exit);
2389 +void __unmapself(void *base, size_t size)
2391 + int tid=__pthread_self()->tid;
2392 + char *stack = shared_stack + sizeof shared_stack;
2393 + stack -= (uintptr_t)stack % 16;
2394 + while (lock || a_cas(&lock, 0, tid))
2396 + __syscall(SYS_set_tid_address, &lock);
2397 + unmap_base = base;
2398 + unmap_size = size;
2399 + CRTJMP(do_unmap, stack);
2401 diff --git a/src/thread/mips/__unmapself.s b/src/thread/mips/__unmapself.s
2402 index 9aa0371..ba139dc 100644
2403 --- a/src/thread/mips/__unmapself.s
2404 +++ b/src/thread/mips/__unmapself.s
2407 .type __unmapself,@function
2413 diff --git a/src/thread/pthread_create.c b/src/thread/pthread_create.c
2414 index de72818..e7df34a 100644
2415 --- a/src/thread/pthread_create.c
2416 +++ b/src/thread/pthread_create.c
2417 @@ -191,8 +191,9 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
2418 if (!libc.can_do_threads) return ENOSYS;
2419 self = __pthread_self();
2420 if (!libc.threaded) {
2421 - for (FILE *f=libc.ofl_head; f; f=f->next)
2422 + for (FILE *f=*__ofl_lock(); f; f=f->next)
2425 init_file_lock(__stdin_used);
2426 init_file_lock(__stdout_used);
2427 init_file_lock(__stderr_used);
2428 @@ -231,7 +232,8 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
2430 map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
2431 if (map == MAP_FAILED) goto fail;
2432 - if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)) {
2433 + if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)
2434 + && errno != ENOSYS) {
2435 __munmap(map, size);
2438 diff --git a/src/thread/sh/__set_thread_area.s b/src/thread/sh/__set_thread_area.s
2439 index d9f1181..e69de29 100644
2440 --- a/src/thread/sh/__set_thread_area.s
2441 +++ b/src/thread/sh/__set_thread_area.s
2443 -.global __set_thread_area
2444 -.type __set_thread_area, @function
2449 diff --git a/src/thread/sh/__unmapself.s b/src/thread/sh/__unmapself.s
2450 index b34c3c8..0161d53 100644
2451 --- a/src/thread/sh/__unmapself.s
2452 +++ b/src/thread/sh/__unmapself.s
2455 -.global __unmapself
2456 -.type __unmapself, @function
2458 +.global __unmapself_sh_mmu
2459 +.type __unmapself_sh_mmu, @function
2460 +__unmapself_sh_mmu:
2461 mov #91, r3 ! SYS_munmap
2467 @@ -13,7 +13,7 @@ __unmapself:
2469 mov #1, r3 ! SYS_exit
2476 diff --git a/src/thread/sh/clone.s b/src/thread/sh/clone.s
2477 index d6c9184..f8ad845 100644
2478 --- a/src/thread/sh/clone.s
2479 +++ b/src/thread/sh/clone.s
2480 @@ -17,7 +17,7 @@ __clone:
2481 mov.l @r15, r6 ! r6 = ptid
2482 mov.l @(8,r15), r7 ! r7 = ctid
2483 mov.l @(4,r15), r0 ! r0 = tls
2489 @@ -38,7 +38,7 @@ __clone:
2491 mov #1, r3 ! __NR_exit
2498 diff --git a/src/thread/sh/syscall_cp.s b/src/thread/sh/syscall_cp.s
2499 index 6b28ddf..c3cafac 100644
2500 --- a/src/thread/sh/syscall_cp.s
2501 +++ b/src/thread/sh/syscall_cp.s
2502 @@ -31,7 +31,7 @@ L1: .long __cancel@PLT-(1b-.)
2510 ! work around hardware bug
2511 diff --git a/src/unistd/sh/pipe.s b/src/unistd/sh/pipe.s
2512 index d865ae3..46c4908 100644
2513 --- a/src/unistd/sh/pipe.s
2514 +++ b/src/unistd/sh/pipe.s
2516 .type pipe, @function
2522 ! work around hardware bug