abb4a9d135fe742696b1e532edd6af137f3c70d7
[openwrt/staging/chunkeey.git] / toolchain / musl / patches / 001-git-2015-06-25.patch
1 commit 6ba5517a460c6c438f64d69464fdfc3269a4c91a
2 Author: Rich Felker <dalias@aerifal.cx>
3 Date: Thu Jun 25 22:22:00 2015 +0000
4
5 fix local-dynamic model TLS on mips and powerpc
6
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:
14
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
17 runtime relocations.
18
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
24 +0x8000 offset.
25
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.
30
31 commit ce337daa00e42d4f2d9a4d9ae0ed51b20249d924
32 Author: Rich Felker <dalias@aerifal.cx>
33 Date: Tue Jun 23 04:03:42 2015 +0000
34
35 make dynamic linker work around MAP_FAILED mmap failure on nommu kernels
36
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.
40
41 commit a59341420fdedb288d9ff80e73609ae44e9cf258
42 Author: Rich Felker <dalias@aerifal.cx>
43 Date: Tue Jun 23 00:12:25 2015 +0000
44
45 reimplement strverscmp to fix corner cases
46
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
52 009 < 00.
53
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.
57
58 the new strverscmp implementation makes explicit the cases where the
59 order differs from what strcmp would produce, of which there are only
60 two.
61
62 commit 153e952e1a688859d7095345b17e6c1df74a295c
63 Author: Rich Felker <dalias@aerifal.cx>
64 Date: Mon Jun 22 20:33:28 2015 +0000
65
66 fix regression/typo that disabled __simple_malloc when calloc is used
67
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.
71
72 commit ba819787ee93ceae94efd274f7849e317c1bff58
73 Author: Rich Felker <dalias@aerifal.cx>
74 Date: Mon Jun 22 18:50:09 2015 +0000
75
76 fix calloc when __simple_malloc implementation is used
77
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
86 allocations.
87
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.
92
93 commit 55d061f031085f24d138664c897791aebe9a2fab
94 Author: Rich Felker <dalias@aerifal.cx>
95 Date: Sat Jun 20 03:01:07 2015 +0000
96
97 provide __stack_chk_fail_local in libc.a
98
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.
104
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
109 enabled).
110
111 commit ce3688eca920aa77549323f84e21f33522397115
112 Author: Rich Felker <dalias@aerifal.cx>
113 Date: Sat Jun 20 02:54:30 2015 +0000
114
115 work around mips detached thread exit breakage due to kernel regression
116
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.
123
124 commit 75eceb3ae824d54e865686c0c538551aeebf3372
125 Author: Rich Felker <dalias@aerifal.cx>
126 Date: Wed Jun 17 17:21:46 2015 +0000
127
128 ignore ENOSYS error from mprotect in pthread_create and dynamic linker
129
130 this error simply indicated a system without memory protection (NOMMU)
131 and should not cause failure in the caller.
132
133 commit 10d0268ccfab9152250eeeed3952ce3fed44131a
134 Author: Rich Felker <dalias@aerifal.cx>
135 Date: Tue Jun 16 15:25:02 2015 +0000
136
137 switch to using trap number 31 for syscalls on sh
138
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.
143
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.
147
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.
155
156 commit 3366a99b17847b58f2d8cc52cbb5d65deb824f8a
157 Author: Rich Felker <dalias@aerifal.cx>
158 Date: Tue Jun 16 14:55:06 2015 +0000
159
160 switch sh port's __unmapself to generic version when running on sh2/nommu
161
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
166 viable on sh2.
167
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.
172
173 commit f9d84554bae0fa17c9a1d724549c4408022228a5
174 Author: Rich Felker <dalias@aerifal.cx>
175 Date: Tue Jun 16 14:28:30 2015 +0000
176
177 add support for sh2 interrupt-masking-based atomics to sh port
178
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.
184
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.
191
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.
195
196 commit 1b0cdc8700d29ef018bf226d74b2b58b23bce91c
197 Author: Rich Felker <dalias@aerifal.cx>
198 Date: Tue Jun 16 07:11:19 2015 +0000
199
200 refactor stdio open file list handling, move it out of global libc struct
201
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.
206
207 function signatures were chosen to facilitate tail call optimization
208 and reduce the need for additional accessor functions.
209
210 with these changes, static linked programs that do not use stdio no
211 longer have an open file list at all.
212
213 commit f22a9edaf8a6f2ca1d314d18b3785558279a5c03
214 Author: Rich Felker <dalias@aerifal.cx>
215 Date: Tue Jun 16 06:18:00 2015 +0000
216
217 byte-based C locale, phase 3: make MB_CUR_MAX variable to activate code
218
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.
224
225 commit 16f18d036d9a7bf590ee6eb86785c0a9658220b6
226 Author: Rich Felker <dalias@aerifal.cx>
227 Date: Tue Jun 16 05:35:31 2015 +0000
228
229 byte-based C locale, phase 2: stdio and iconv (multibyte callers)
230
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
236 stdio and iconv.
237
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
241 continue working.
242
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.
248
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.
258
259 commit 1507ebf837334e9e07cfab1ca1c2e88449069a80
260 Author: Rich Felker <dalias@aerifal.cx>
261 Date: Tue Jun 16 04:44:17 2015 +0000
262
263 byte-based C locale, phase 1: multibyte character handling functions
264
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.
271
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.
280
281 commit 38e2f727237230300fea6aff68802db04625fd23
282 Author: Rich Felker <dalias@aerifal.cx>
283 Date: Tue Jun 16 04:21:38 2015 +0000
284
285 fix btowc corner case
286
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
290 convert, for now.
291
292 commit ee59c296d56bf26f49f354d6eb32b4b6d4190188
293 Author: Szabolcs Nagy <nsz@port70.net>
294 Date: Wed Jun 3 10:32:14 2015 +0100
295
296 arm: add vdso support
297
298 vdso will be available on arm in linux v4.2, the user-space code
299 for it is in kernel commit 8512287a8165592466cb9cb347ba94892e9c56a5
300
301 commit e3bc22f1eff87b8f029a6ab31f1a269d69e4b053
302 Author: Rich Felker <dalias@aerifal.cx>
303 Date: Sun Jun 14 01:59:02 2015 +0000
304
305 refactor malloc's expand_heap to share with __simple_malloc
306
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.
311
312 it also extends support for using mmap when brk fails, which full
313 malloc got in commit 5446303328adf4b4e36d9fba21848e6feb55fab4, to
314 __simple_malloc.
315
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.
323
324 commit 4ef9b828c1f39553a69e0635ac91f0fcadd6e8c6
325 Author: Rich Felker <dalias@aerifal.cx>
326 Date: Sat Jun 13 20:53:02 2015 +0000
327
328 remove cancellation points in stdio
329
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.
338
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.
343
344 commit 536c6d5a4205e2a3f161f2983ce1e0ac3082187d
345 Author: Rich Felker <dalias@aerifal.cx>
346 Date: Sat Jun 13 05:17:16 2015 +0000
347
348 fix idiom for setting stdio stream orientation to wide
349
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.
359
360 commit f8f565df467c13248104223f99abf7f37cef7584
361 Author: Rich Felker <dalias@aerifal.cx>
362 Date: Sat Jun 13 04:42:38 2015 +0000
363
364 add printing of null %s arguments as "(null)" in wide printf
365
366 this is undefined, but supported in our implementation of the normal
367 printf, so for consistency the wide variant should support it too.
368
369 commit f9e25d813860d53cd1e9b6145cc63375d2fe2529
370 Author: Rich Felker <dalias@aerifal.cx>
371 Date: Sat Jun 13 04:37:27 2015 +0000
372
373 add %m support to wide printf
374
375 commit ec634aad91f57479ef17525e33ed446c780a61f4
376 Author: Rich Felker <dalias@aerifal.cx>
377 Date: Thu Jun 11 05:01:04 2015 +0000
378
379 add sh asm for vfork
380
381 commit c30cbcb0a646b1f13a22c645616dce624465b883
382 Author: Rich Felker <dalias@aerifal.cx>
383 Date: Wed Jun 10 02:27:40 2015 +0000
384
385 implement arch-generic version of __unmapself
386
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.
390
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.
396
397 commit 276904c2f6bde3a31a24ebfa201482601d18b4f9
398 Author: Rich Felker <dalias@aerifal.cx>
399 Date: Tue Jun 9 20:30:35 2015 +0000
400
401 in malloc, refuse to use brk if it grows into stack
402
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
408 makes brk unusable.
409
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.
419
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.
424
425 commit bd1eaceaa3975bd2a2a34e211cff896affaecadf
426 Author: Rich Felker <dalias@aerifal.cx>
427 Date: Tue Jun 9 20:09:27 2015 +0000
428
429 fix spurious errors from pwd/grp functions when nscd backend is absent
430
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.
437
438 this commit also fixed an outdated/incorrect comment.
439
440 commit 75ce4503950621b11fcc7f1fd1187dbcf3cde312
441 Author: Rich Felker <dalias@aerifal.cx>
442 Date: Sun Jun 7 20:55:23 2015 +0000
443
444 fix regression in pre-v7 arm on kernels with kuser helper removed
445
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.
453
454 upcoming work on superh will use similar runtime detection.
455
456 ideally this early-init code should be cleanly refactored and shared
457 between the dynamic linker and static-linked startup.
458
459 commit 32f3c4f70633488550c29a2444f819aafdf345ff
460 Author: Rich Felker <dalias@aerifal.cx>
461 Date: Sun Jun 7 03:09:16 2015 +0000
462
463 add multiple inclusion guard to locale_impl.h
464
465 commit 04b8360adbb6487f61aa0c00e53ec3a90a5a0d29
466 Author: Rich Felker <dalias@aerifal.cx>
467 Date: Sun Jun 7 02:59:49 2015 +0000
468
469 remove redefinition of MB_CUR_MAX in locale_impl.h
470
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
475 unaffected.
476
477 commit 16bf466532d7328e971012b0731ad493b017ad29
478 Author: Rich Felker <dalias@aerifal.cx>
479 Date: Sat Jun 6 18:53:02 2015 +0000
480
481 make static C and C.UTF-8 locales available outside of newlocale
482
483 commit 312eea2ea4f4363fb01b73660c08bfcf43dd3bb4
484 Author: Rich Felker <dalias@aerifal.cx>
485 Date: Sat Jun 6 18:20:30 2015 +0000
486
487 remove another invalid skip of locking in ungetwc
488
489 commit 3d7e32d28dc9962e9efc1c317c5b44b5b2df3008
490 Author: Rich Felker <dalias@aerifal.cx>
491 Date: Sat Jun 6 18:16:22 2015 +0000
492
493 add macro version of ctype.h isascii function
494
495 presumably internal code (ungetwc and fputwc) was written assuming a
496 macro implementation existed; otherwise use of isascii is just a
497 pessimization.
498
499 commit 7e816a6487932cbb3cb71d94b609e50e81f4e5bf
500 Author: Rich Felker <dalias@aerifal.cx>
501 Date: Sat Jun 6 18:11:17 2015 +0000
502
503 remove invalid skip of locking in ungetwc
504
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.
508
509 commit 63f4b9f18f3674124d8bcb119739fec85e6da005
510 Author: Timo Teräs <timo.teras@iki.fi>
511 Date: Fri Jun 5 10:39:42 2015 +0300
512
513 fix uselocale((locale_t)0) not to modify locale
514
515 commit 68630b55c0c7219fe9df70dc28ffbf9efc8021d8 made the new locale to
516 be assigned unconditonally resulting in crashes later on.
517
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));
525 }
526 +
527 +#define VDSO_USEFUL
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()
535 #define TLS_ABOVE_TP
536 #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + 0x7000)
537
538 +#define DTP_OFFSET 0x8000
539 +
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()
546 #define TLS_ABOVE_TP
547 #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + 0x7000)
548
549 +#define DTP_OFFSET 0x8000
550 +
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
553 // GPRs.
554 diff --git a/arch/sh/src/__set_thread_area.c b/arch/sh/src/__set_thread_area.c
555 new file mode 100644
556 index 0000000..1d3e022
557 --- /dev/null
558 +++ b/arch/sh/src/__set_thread_area.c
559 @@ -0,0 +1,34 @@
560 +#include "pthread_impl.h"
561 +#include "libc.h"
562 +#include "sh_atomic.h"
563 +#include <elf.h>
564 +
565 +/* Also perform sh-specific init */
566 +
567 +#define CPU_HAS_LLSC 0x0040
568 +
569 +__attribute__((__visibility__("hidden"))) unsigned __sh_atomic_model, __sh_nommu;
570 +
571 +int __set_thread_area(void *p)
572 +{
573 + size_t *aux;
574 + __asm__ __volatile__ ( "ldc %0, gbr" : : "r"(p) : "memory" );
575 +#ifndef __SH4A__
576 + if (__hwcap & CPU_HAS_LLSC) {
577 + __sh_atomic_model = SH_A_LLSC;
578 + return 0;
579 + }
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;
586 + __sh_nommu = 1;
587 + return 0;
588 + }
589 +#endif
590 + /* __sh_atomic_model = SH_A_GUSA; */ /* 0, default */
591 +#endif
592 + return 0;
593 +}
594 diff --git a/arch/sh/src/__unmapself.c b/arch/sh/src/__unmapself.c
595 new file mode 100644
596 index 0000000..4df9e7b
597 --- /dev/null
598 +++ b/arch/sh/src/__unmapself.c
599 @@ -0,0 +1,19 @@
600 +#include "pthread_impl.h"
601 +
602 +void __unmapself_sh_mmu(void *, size_t);
603 +void __unmapself_sh_nommu(void *, size_t);
604 +
605 +#if !defined(__SH3__) && !defined(__SH4__)
606 +#define __unmapself __unmapself_sh_nommu
607 +#include "../../../src/thread/__unmapself.c"
608 +#undef __unmapself
609 +extern __attribute__((__visibility__("hidden"))) unsigned __sh_nommu;
610 +#else
611 +#define __sh_nommu 0
612 +#endif
613 +
614 +void __unmapself(void *base, size_t size)
615 +{
616 + if (__sh_nommu) __unmapself_sh_nommu(base, size);
617 + else __unmapself_sh_mmu(base, size);
618 +}
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
623 @@ -1,8 +1,26 @@
624 #ifndef __SH4A__
625
626 +#include "sh_atomic.h"
627 #include "atomic.h"
628 #include "libc.h"
629
630 +static inline unsigned mask()
631 +{
632 + unsigned sr;
633 + __asm__ __volatile__ ( "\n"
634 + " stc sr,r0 \n"
635 + " mov r0,%0 \n"
636 + " or #0xf0,r0 \n"
637 + " ldc r0,sr \n"
638 + : "=&r"(sr) : : "memory", "r0" );
639 + return sr;
640 +}
641 +
642 +static inline void unmask(unsigned sr)
643 +{
644 + __asm__ __volatile__ ( "ldc %0,sr" : : "r"(sr) : "memory" );
645 +}
646 +
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:
650 @@ -25,11 +43,17 @@
651 " mov.l " new ", @" mem "\n" \
652 "1: mov r1, r15\n"
653
654 -#define CPU_HAS_LLSC 0x0040
655 -
656 int __sh_cas(volatile int *p, int t, int s)
657 {
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);
660 +
661 + if (__sh_atomic_model == SH_A_IMASK) {
662 + unsigned sr = mask();
663 + int old = *p;
664 + if (old==t) *p = s;
665 + unmask(sr);
666 + return old;
667 + }
668
669 int old;
670 __asm__ __volatile__(
671 @@ -43,7 +67,15 @@ int __sh_cas(volatile int *p, int t, int s)
672
673 int __sh_swap(volatile int *x, int v)
674 {
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);
677 +
678 + if (__sh_atomic_model == SH_A_IMASK) {
679 + unsigned sr = mask();
680 + int old = *x;
681 + *x = v;
682 + unmask(sr);
683 + return old;
684 + }
685
686 int old;
687 __asm__ __volatile__(
688 @@ -55,7 +87,15 @@ int __sh_swap(volatile int *x, int v)
689
690 int __sh_fetch_add(volatile int *x, int v)
691 {
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);
694 +
695 + if (__sh_atomic_model == SH_A_IMASK) {
696 + unsigned sr = mask();
697 + int old = *x;
698 + *x = old + v;
699 + unmask(sr);
700 + return old;
701 + }
702
703 int old, dummy;
704 __asm__ __volatile__(
705 @@ -69,7 +109,7 @@ int __sh_fetch_add(volatile int *x, int v)
706
707 void __sh_store(volatile int *p, int x)
708 {
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__(
712 " mov.l %1, @%0\n"
713 : : "r"(p), "r"(x) : "memory");
714 @@ -77,7 +117,15 @@ void __sh_store(volatile int *p, int x)
715
716 void __sh_and(volatile int *x, int v)
717 {
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);
720 +
721 + if (__sh_atomic_model == SH_A_IMASK) {
722 + unsigned sr = mask();
723 + int old = *x;
724 + *x = old & v;
725 + unmask(sr);
726 + return;
727 + }
728
729 int dummy;
730 __asm__ __volatile__(
731 @@ -89,7 +137,15 @@ void __sh_and(volatile int *x, int v)
732
733 void __sh_or(volatile int *x, int v)
734 {
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);
737 +
738 + if (__sh_atomic_model == SH_A_IMASK) {
739 + unsigned sr = mask();
740 + int old = *x;
741 + *x = old | v;
742 + unmask(sr);
743 + return;
744 + }
745
746 int dummy;
747 __asm__ __volatile__(
748 diff --git a/arch/sh/src/sh_atomic.h b/arch/sh/src/sh_atomic.h
749 new file mode 100644
750 index 0000000..054c2a3
751 --- /dev/null
752 +++ b/arch/sh/src/sh_atomic.h
753 @@ -0,0 +1,15 @@
754 +#ifndef _SH_ATOMIC_H
755 +#define _SH_ATOMIC_H
756 +
757 +#define SH_A_GUSA 0
758 +#define SH_A_LLSC 1
759 +#define SH_A_CAS 2
760 +#if !defined(__SH3__) && !defined(__SH4__)
761 +#define SH_A_IMASK 3
762 +#else
763 +#define SH_A_IMASK -1LL /* unmatchable by unsigned int */
764 +#endif
765 +
766 +extern __attribute__((__visibility__("hidden"))) unsigned __sh_atomic_model;
767 +
768 +#endif
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
773 @@ -8,7 +8,7 @@
774 */
775 #define __asm_syscall(trapno, ...) do { \
776 __asm__ __volatile__ ( \
777 - "trapa #" #trapno "\n" \
778 + "trapa #31\n" \
779 "or r0, r0\n" \
780 "or r0, r0\n" \
781 "or r0, r0\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);
787 int toascii(int);
788 #define _tolower(a) ((a)|0x20)
789 #define _toupper(a) ((a)&0x5f)
790 +#define isascii(a) (0 ? isascii(a) : (unsigned)(a) < 128)
791
792 #endif
793
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
801
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())
805
806 #define RAND_MAX (0x7fffffff)
807
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
812 @@ -1,6 +1,7 @@
813 -#include <stddef.h>
814 +#include <stdlib.h>
815 +#include "locale_impl.h"
816
817 size_t __ctype_get_mb_cur_max()
818 {
819 - return 4;
820 + return MB_CUR_MAX;
821 }
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
826 @@ -1,4 +1,5 @@
827 #include <ctype.h>
828 +#undef isascii
829
830 int isascii(int c)
831 {
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)
837 a_crash();
838 }
839
840 +#else
841 +
842 +weak_alias(__stack_chk_fail, __stack_chk_fail_local);
843 +
844 #endif
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 {
850 int secure;
851 volatile int threads_minus_1;
852 size_t *auxv;
853 - FILE *ofl_head;
854 - volatile int ofl_lock[2];
855 size_t tls_size;
856 size_t page_size;
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
862 @@ -1,3 +1,6 @@
863 +#ifndef _LOCALE_IMPL_H
864 +#define _LOCALE_IMPL_H
865 +
866 #include <locale.h>
867 #include <stdlib.h>
868 #include "libc.h"
869 @@ -12,6 +15,10 @@ struct __locale_map {
870 const struct __locale_map *next;
871 };
872
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;
876 +
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)
883
884 +#define C_LOCALE ((locale_t)&__c_locale)
885 +#define UTF8_LOCALE ((locale_t)&__c_dot_utf8_locale)
886 +
887 #define CURRENT_LOCALE (__pthread_self()->locale)
888
889 #define CURRENT_UTF8 (!!__pthread_self()->locale->cat[LC_CTYPE])
890
891 #undef MB_CUR_MAX
892 #define MB_CUR_MAX (CURRENT_UTF8 ? 4 : 1)
893 +
894 +#endif
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
901 #endif
902
903 +#ifndef DTP_OFFSET
904 +#define DTP_OFFSET 0
905 +#endif
906 +
907 #define SIGTIMER 32
908 #define SIGCANCEL 33
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:
915 mov.l @r15, r7
916 mov.l @(4,r15), r0
917 mov.l @(8,r15), r1
918 - trapa #22
919 + trapa #31
920 or r0, r0
921 or r0, r0
922 or r0, r0
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;
929 off_t shlim, shcnt;
930 FILE *prev_locked, *next_locked;
931 + struct __locale_struct *locale;
932 };
933
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 *);
938
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);
944
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;
953 break;
954 case REL_DTPOFF:
955 - *reloc_addr = tls_val + addend;
956 + *reloc_addr = tls_val + addend - DTP_OFFSET;
957 break;
958 #ifdef TLS_ABOVE_TP
959 case REL_TPOFF:
960 @@ -423,6 +423,28 @@ static void reclaim_gaps(struct dso *dso)
961 }
962 }
963
964 +static void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t off)
965 +{
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) {
970 + memset(p, 0, n);
971 + return p;
972 + }
973 + ssize_t r;
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;
978 + if (!r) {
979 + memset(q, 0, n);
980 + break;
981 + }
982 + }
983 + return p;
984 +}
985 +
986 static void *map_library(int fd, struct dso *dso)
987 {
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)
995 goto error;
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)
1002 goto error;
1003 }
1004 }
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)
1010 goto error;
1011 break;
1012 }
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);
1015
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",
1021 p->name);
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;
1029 }
1030
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;
1034 }
1035 __restore_sigs(&set);
1036 - return mem + v[1];
1037 + return mem + v[1] + DTP_OFFSET;
1038 }
1039
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;
1044
1045 + /* Find aux vector just past environ[] and use it to initialize
1046 + * global data that may be needed before we can make syscalls. */
1047 + __environ = envp;
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]);
1055 +
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)
1060 a_crash();
1061 }
1062
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);
1070 -
1071 - decode_vec(auxv, aux, AUX_CNT);
1072 -
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]) {
1076 - env_path = 0;
1077 - env_preload = 0;
1078 - libc.secure = 1;
1079 + if (!libc.secure) {
1080 + env_path = getenv("LD_LIBRARY_PATH");
1081 + env_preload = getenv("LD_PRELOAD");
1082 }
1083 - libc.page_size = aux[AT_PAGESZ];
1084 - libc.auxv = auxv;
1085
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
1091 --- /dev/null
1092 +++ b/src/locale/c_locale.c
1093 @@ -0,0 +1,15 @@
1094 +#include "locale_impl.h"
1095 +#include <stdint.h>
1096 +
1097 +static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };
1098 +
1099 +const struct __locale_map __c_dot_utf8 = {
1100 + .map = empty_mo,
1101 + .map_size = sizeof empty_mo,
1102 + .name = "C.UTF-8"
1103 +};
1104 +
1105 +const struct __locale_struct __c_locale = { 0 };
1106 +const struct __locale_struct __c_dot_utf8_locale = {
1107 + .cat[LC_CTYPE] = &__c_dot_utf8
1108 +};
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
1113 @@ -5,6 +5,7 @@
1114 #include <stdlib.h>
1115 #include <limits.h>
1116 #include <stdint.h>
1117 +#include "locale_impl.h"
1118
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
1122 int err;
1123 unsigned char type = map[-1];
1124 unsigned char totype = tomap[-1];
1125 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
1126
1127 if (!in || !*in || !*inb) return 0;
1128
1129 + *ploc = UTF8_LOCALE;
1130 +
1131 for (; *inb; *in+=l, *inb-=l) {
1132 c = *(unsigned char *)*in;
1133 l = 1;
1134 @@ -431,6 +435,7 @@ size_t iconv(iconv_t cd0, char **restrict in, size_t *restrict inb, char **restr
1135 break;
1136 }
1137 }
1138 + *ploc = loc;
1139 return x;
1140 ilseq:
1141 err = EILSEQ;
1142 @@ -445,5 +450,6 @@ starved:
1143 x = -1;
1144 end:
1145 errno = err;
1146 + *ploc = loc;
1147 return x;
1148 }
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;
1155 const char *str;
1156
1157 - if (item == CODESET) return "UTF-8";
1158 + if (item == CODESET)
1159 + return MB_CUR_MAX==1 ? "UTF-8-CODE-UNITS" : "UTF-8";
1160
1161 switch (cat) {
1162 case LC_NUMERIC:
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] = {
1168 "LC_MESSAGES",
1169 };
1170
1171 -static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };
1172 -
1173 -const struct __locale_map __c_dot_utf8 = {
1174 - .map = empty_mo,
1175 - .map_size = sizeof empty_mo,
1176 - .name = "C.UTF-8"
1177 -};
1178 -
1179 const struct __locale_map *__get_locale(int cat, const char *val)
1180 {
1181 static int lock[2];
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);
1191 new->name[n] = 0;
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
1197 @@ -3,16 +3,9 @@
1198 #include "locale_impl.h"
1199 #include "libc.h"
1200
1201 -extern const struct __locale_map __c_dot_utf8;
1202 -
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
1206 -};
1207 -
1208 int __loc_is_allocated(locale_t loc)
1209 {
1210 - return loc && loc != &c_locale && loc != &c_dot_utf8_locale;
1211 + return loc && loc != C_LOCALE && loc != UTF8_LOCALE;
1212 }
1213
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)
1216 }
1217
1218 if (!j)
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;
1222 + return C_LOCALE;
1223 + if (j==1 && tmp.cat[LC_CTYPE]==&__c_dot_utf8)
1224 + return UTF8_LOCALE;
1225
1226 if ((loc = malloc(sizeof *loc))) *loc = tmp;
1227
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;
1235
1236 - if (new == LC_GLOBAL_LOCALE) new = global;
1237 -
1238 - self->locale = new;
1239 + if (new) self->locale = new == LC_GLOBAL_LOCALE ? global : new;
1240
1241 return old == global ? LC_GLOBAL_LOCALE : old;
1242 }
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
1247 @@ -1,22 +1,13 @@
1248 #include <stdlib.h>
1249 #include <errno.h>
1250
1251 +void *__malloc0(size_t);
1252 +
1253 void *calloc(size_t m, size_t n)
1254 {
1255 - void *p;
1256 - size_t *z;
1257 if (n && m > (size_t)-1/n) {
1258 errno = ENOMEM;
1259 return 0;
1260 }
1261 - n *= m;
1262 - p = malloc(n);
1263 - if (!p) return 0;
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;
1269 - }
1270 - return p;
1271 + return __malloc0(n * m);
1272 }
1273 diff --git a/src/malloc/expand_heap.c b/src/malloc/expand_heap.c
1274 new file mode 100644
1275 index 0000000..d8c0be7
1276 --- /dev/null
1277 +++ b/src/malloc/expand_heap.c
1278 @@ -0,0 +1,72 @@
1279 +#include <limits.h>
1280 +#include <stdint.h>
1281 +#include <errno.h>
1282 +#include <sys/mman.h>
1283 +#include "libc.h"
1284 +#include "syscall.h"
1285 +
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. */
1291 +
1292 +static int traverses_stack_p(uintptr_t old, uintptr_t new)
1293 +{
1294 + const uintptr_t len = 8<<20;
1295 + uintptr_t a, b;
1296 +
1297 + b = (uintptr_t)libc.auxv;
1298 + a = b > len ? b-len : 0;
1299 + if (new>a && old<b) return 1;
1300 +
1301 + b = (uintptr_t)&b;
1302 + a = b > len ? b-len : 0;
1303 + if (new>a && old<b) return 1;
1304 +
1305 + return 0;
1306 +}
1307 +
1308 +void *__mmap(void *, size_t, int, int, int, off_t);
1309 +
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. */
1317 +
1318 +void *__expand_heap(size_t *pn)
1319 +{
1320 + static uintptr_t brk;
1321 + static unsigned mmap_step;
1322 + size_t n = *pn;
1323 +
1324 + if (n > SIZE_MAX/2 - PAGE_SIZE) {
1325 + errno = ENOMEM;
1326 + return 0;
1327 + }
1328 + n += -n & PAGE_SIZE-1;
1329 +
1330 + if (!brk) {
1331 + brk = __syscall(SYS_brk, 0);
1332 + brk += -brk & PAGE_SIZE-1;
1333 + }
1334 +
1335 + if (n < SIZE_MAX-brk && !traverses_stack_p(brk, brk+n)
1336 + && __syscall(SYS_brk, brk+n)==brk+n) {
1337 + *pn = n;
1338 + brk += n;
1339 + return (void *)(brk-n);
1340 + }
1341 +
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;
1347 + *pn = n;
1348 + mmap_step++;
1349 + return area;
1350 +}
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
1355 @@ -4,43 +4,47 @@
1356 #include <errno.h>
1357 #include "libc.h"
1358
1359 -uintptr_t __brk(uintptr_t);
1360 -
1361 #define ALIGN 16
1362
1363 +void *__expand_heap(size_t *);
1364 +
1365 void *__simple_malloc(size_t n)
1366 {
1367 - static uintptr_t cur, brk;
1368 - uintptr_t base, new;
1369 + static char *cur, *end;
1370 static volatile int lock[2];
1371 - size_t align=1;
1372 + size_t align=1, pad;
1373 + void *p;
1374
1375 if (!n) n++;
1376 - if (n > SIZE_MAX/2) goto toobig;
1377 -
1378 while (align<n && align<ALIGN)
1379 align += align;
1380 - n = n + align - 1 & -align;
1381
1382 LOCK(lock);
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;
1389 - brk = new;
1390 - }
1391 - cur = base+n;
1392 - UNLOCK(lock);
1393
1394 - return (void *)base;
1395 + pad = -(uintptr_t)cur & align-1;
1396 +
1397 + if (n <= SIZE_MAX/2 + ALIGN) n += pad;
1398 +
1399 + if (n > end-cur) {
1400 + size_t m = n;
1401 + char *new = __expand_heap(&m);
1402 + if (!new) {
1403 + UNLOCK(lock);
1404 + return 0;
1405 + }
1406 + if (new != end) {
1407 + cur = new;
1408 + n -= pad;
1409 + pad = 0;
1410 + }
1411 + end = new + m;
1412 + }
1413
1414 -fail:
1415 + p = cur + pad;
1416 + cur += n;
1417 UNLOCK(lock);
1418 -toobig:
1419 - errno = ENOMEM;
1420 - return 0;
1421 + return p;
1422 }
1423
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
1430 @@ -13,7 +13,6 @@
1431 #define inline inline __attribute__((always_inline))
1432 #endif
1433
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 {
1439 };
1440
1441 static struct {
1442 - uintptr_t brk;
1443 - size_t *heap;
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;
1449 } mal;
1450
1451
1452 @@ -152,69 +147,52 @@ void __dump_heap(int x)
1453 }
1454 #endif
1455
1456 +void *__expand_heap(size_t *);
1457 +
1458 static struct chunk *expand_heap(size_t n)
1459 {
1460 - static int init;
1461 + static int heap_lock[2];
1462 + static void *end;
1463 + void *p;
1464 struct chunk *w;
1465 - uintptr_t new;
1466 -
1467 - lock(mal.brk_lock);
1468
1469 - if (!init) {
1470 - mal.brk = __brk(0);
1471 -#ifdef SHARED
1472 - mal.brk = mal.brk + PAGE_SIZE-1 & -PAGE_SIZE;
1473 -#endif
1474 - mal.brk = mal.brk + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
1475 - mal.heap = (void *)mal.brk;
1476 - init = 1;
1477 - }
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. */
1481 + n += SIZE_ALIGN;
1482
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;
1486 + lock(heap_lock);
1487
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);
1496 + if (!p) {
1497 + unlock(heap_lock);
1498 + return 0;
1499 + }
1500
1501 - mal.mmap_step++;
1502 - area = (char *)area + SIZE_ALIGN - OVERHEAD;
1503 - w = area;
1504 + /* If not just expanding existing space, we need to make a
1505 + * new sentinel chunk below the allocated space. */
1506 + if (p != end) {
1507 + /* Valid/safe because of the prologue increment. */
1508 n -= SIZE_ALIGN;
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;
1516 -
1517 - unlock(mal.brk_lock);
1518 -
1519 - return area;
1520 }
1521
1522 - w = MEM_TO_CHUNK(mal.heap);
1523 - w->psize = 0 | C_INUSE;
1524 -
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;
1531
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;
1537 - mal.brk = new;
1538 -
1539 - unlock(mal.brk_lock);
1540 +
1541 + unlock(heap_lock);
1542
1543 return w;
1544 -fail:
1545 - unlock(mal.brk_lock);
1546 - errno = ENOMEM;
1547 - return 0;
1548 }
1549
1550 static int adjust_size(size_t *n)
1551 @@ -378,6 +356,17 @@ void *malloc(size_t n)
1552 return CHUNK_TO_MEM(c);
1553 }
1554
1555 +void *__malloc0(size_t n)
1556 +{
1557 + void *p = malloc(n);
1558 + if (p && !IS_MMAPPED(MEM_TO_CHUNK(p))) {
1559 + size_t *z;
1560 + n = (n + sizeof *z - 1)/sizeof *z;
1561 + for (z=p; n; n--, z++) if (*z) *z=0;
1562 + }
1563 + return p;
1564 +}
1565 +
1566 void *realloc(void *p, size_t n)
1567 {
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
1573 @@ -1,7 +1,10 @@
1574 #include <stdio.h>
1575 #include <wchar.h>
1576 +#include <stdlib.h>
1577 +#include "internal.h"
1578
1579 wint_t btowc(int c)
1580 {
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;
1584 }
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[];
1590
1591 #define SA 0xc2u
1592 #define SB 0xf4u
1593 +
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)
1597 +
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
1604 @@ -4,6 +4,7 @@
1605 * unnecessary.
1606 */
1607
1608 +#include <stdlib.h>
1609 #include <wchar.h>
1610 #include <errno.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
1613 if (!n) return -2;
1614 if (!c) {
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--;
1619 }
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
1624 @@ -7,6 +7,8 @@
1625 #include <stdint.h>
1626 #include <wchar.h>
1627 #include <errno.h>
1628 +#include <string.h>
1629 +#include <stdlib.h>
1630 #include "internal.h"
1631
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
1634 }
1635 }
1636
1637 + if (MB_CUR_MAX==1) {
1638 + if (!ws) return strlen((const char *)s);
1639 + for (;;) {
1640 + if (!wn) {
1641 + *src = (const void *)s;
1642 + return wn0;
1643 + }
1644 + if (!*s) break;
1645 + c = *s++;
1646 + *ws++ = CODEUNIT(c);
1647 + wn--;
1648 + }
1649 + *ws = 0;
1650 + *src = 0;
1651 + return wn0-wn;
1652 + }
1653 +
1654 if (!ws) for (;;) {
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
1661 @@ -4,6 +4,7 @@
1662 * unnecessary.
1663 */
1664
1665 +#include <stdlib.h>
1666 #include <wchar.h>
1667 #include <errno.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;
1671
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];
1676
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
1681 @@ -4,8 +4,10 @@
1682 * unnecessary.
1683 */
1684
1685 +#include <stdlib.h>
1686 #include <wchar.h>
1687 #include <errno.h>
1688 +#include "internal.h"
1689
1690 size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict st)
1691 {
1692 @@ -13,6 +15,13 @@ size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict st)
1693 if ((unsigned)wc < 0x80) {
1694 *s = wc;
1695 return 1;
1696 + } else if (MB_CUR_MAX == 1) {
1697 + if (!IS_CODEUNIT(wc)) {
1698 + errno = EILSEQ;
1699 + return -1;
1700 + }
1701 + *s = wc;
1702 + return 1;
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
1710 @@ -1,8 +1,10 @@
1711 -#include <stdio.h>
1712 #include <wchar.h>
1713 +#include <stdlib.h>
1714 +#include "internal.h"
1715
1716 int wctob(wint_t c)
1717 {
1718 if (c < 128U) return c;
1719 + if (MB_CUR_MAX==1 && IS_CODEUNIT(c)) return (unsigned char)c;
1720 return EOF;
1721 }
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 *
1727 },
1728 .msg_iovlen = 2
1729 };
1730 + int errno_save = errno;
1731
1732 *swap = 0;
1733 retry:
1734 @@ -50,11 +51,14 @@ retry:
1735 return f;
1736
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
1740 - */
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;
1748 return f;
1749 + }
1750 goto error;
1751 }
1752
1753 diff --git a/src/process/sh/vfork.s b/src/process/sh/vfork.s
1754 new file mode 100644
1755 index 0000000..48cc939
1756 --- /dev/null
1757 +++ b/src/process/sh/vfork.s
1758 @@ -0,0 +1,23 @@
1759 +.global __vfork
1760 +.weak vfork
1761 +.type __vfork,@function
1762 +.type vfork,@function
1763 +__vfork:
1764 +vfork:
1765 + mov #95, r3
1766 + add r3, r3
1767 +
1768 + trapa #31
1769 + or r0, r0
1770 + or r0, r0
1771 + or r0, r0
1772 + or r0, r0
1773 + or r0, r0
1774 +
1775 + mov r0, r4
1776 + mov.l 1f, r0
1777 +2: braf r0
1778 + nop
1779 + .align 2
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
1786 @@ -18,6 +18,7 @@
1787 #include <stdlib.h>
1788 #include <wchar.h>
1789 #include <wctype.h>
1790 +#include "locale_impl.h"
1791
1792 #define END 0
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);
1801 }
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
1807 @@ -2,7 +2,7 @@
1808 .type __restore, @function
1809 __restore:
1810 mov #119, r3 !__NR_sigreturn
1811 - trapa #16
1812 + trapa #31
1813
1814 or r0, r0
1815 or r0, r0
1816 @@ -15,7 +15,7 @@ __restore:
1817 __restore_rt:
1818 mov #100, r3 !__NR_rt_sigreturn
1819 add #73, r3
1820 - trapa #16
1821 + trapa #31
1822
1823 or r0, r0
1824 or r0, r0
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;
1831
1832 /* Add new FILE to open file list */
1833 - OFLLOCK();
1834 - f->next = libc.ofl_head;
1835 - if (libc.ofl_head) libc.ofl_head->prev = f;
1836 - libc.ofl_head = f;
1837 - OFLUNLOCK();
1838 -
1839 - return f;
1840 + return __ofl_add(f);
1841 }
1842
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)
1850 {
1851 FILE *f;
1852 - OFLLOCK();
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);
1857 }
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
1862 @@ -1,12 +1,5 @@
1863 #include "stdio_impl.h"
1864 #include <sys/uio.h>
1865 -#include <pthread.h>
1866 -
1867 -static void cleanup(void *p)
1868 -{
1869 - FILE *f = p;
1870 - if (!f->lockcount) __unlockfile(f);
1871 -}
1872
1873 size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
1874 {
1875 @@ -16,9 +9,7 @@ size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
1876 };
1877 ssize_t cnt;
1878
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);
1883 if (cnt <= 0) {
1884 f->flags |= F_EOF ^ ((F_ERR^F_EOF) & cnt);
1885 return 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
1890 @@ -1,12 +1,5 @@
1891 #include "stdio_impl.h"
1892 #include <sys/uio.h>
1893 -#include <pthread.h>
1894 -
1895 -static void cleanup(void *p)
1896 -{
1897 - FILE *f = p;
1898 - if (!f->lockcount) __unlockfile(f);
1899 -}
1900
1901 size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
1902 {
1903 @@ -19,9 +12,7 @@ size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
1904 int iovcnt = 2;
1905 ssize_t cnt;
1906 for (;;) {
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);
1911 if (cnt == rem) {
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)
1915 }
1916 rem -= cnt;
1917 if (cnt > iov[0].iov_len) {
1918 - f->wpos = f->wbase = f->buf;
1919 cnt -= iov[0].iov_len;
1920 iov++; iovcnt--;
1921 - } else if (iovcnt == 2) {
1922 - f->wbase += cnt;
1923 }
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);
1932
1933 if (!(perm = f->flags & F_PERM)) {
1934 - OFLLOCK();
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;
1939 - OFLUNLOCK();
1940 + if (*head == f) *head = f->next;
1941 + __ofl_unlock();
1942 }
1943
1944 r = fflush(f);
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)
1950
1951 r = __stdout_used ? fflush(__stdout_used) : 0;
1952
1953 - OFLLOCK();
1954 - for (f=libc.ofl_head; f; f=f->next) {
1955 + for (f=*__ofl_lock(); f; f=f->next) {
1956 FLOCK(f);
1957 if (f->wpos > f->wbase) r |= __fflush_unlocked(f);
1958 FUNLOCK(f);
1959 }
1960 - OFLUNLOCK();
1961 + __ofl_unlock();
1962
1963 return r;
1964 }
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
1969 @@ -1,8 +1,9 @@
1970 #include "stdio_impl.h"
1971 +#include "locale_impl.h"
1972 #include <wchar.h>
1973 #include <errno.h>
1974
1975 -wint_t __fgetwc_unlocked(FILE *f)
1976 +static wint_t __fgetwc_unlocked_internal(FILE *f)
1977 {
1978 mbstate_t st = { 0 };
1979 wchar_t wc;
1980 @@ -10,8 +11,6 @@ wint_t __fgetwc_unlocked(FILE *f)
1981 unsigned char b;
1982 size_t l;
1983
1984 - f->mode |= f->mode+1;
1985 -
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)
1990 return wc;
1991 }
1992
1993 +wint_t __fgetwc_unlocked(FILE *f)
1994 +{
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);
1999 + *ploc = loc;
2000 + return wc;
2001 +}
2002 +
2003 wint_t fgetwc(FILE *f)
2004 {
2005 wint_t c;
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)
2011
2012 if (!libc.threaded) f->lock = -1;
2013
2014 - OFLLOCK();
2015 - f->next = libc.ofl_head;
2016 - if (libc.ofl_head) libc.ofl_head->prev = f;
2017 - libc.ofl_head = f;
2018 - OFLUNLOCK();
2019 -
2020 - return f;
2021 + return __ofl_add(f);
2022 }
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);
2030
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
2040 @@ -1,4 +1,5 @@
2041 #include "stdio_impl.h"
2042 +#include "locale_impl.h"
2043 #include <wchar.h>
2044 #include <limits.h>
2045 #include <ctype.h>
2046 @@ -7,8 +8,10 @@ wint_t __fputwc_unlocked(wchar_t c, FILE *f)
2047 {
2048 char mbc[MB_LEN_MAX];
2049 int l;
2050 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
2051
2052 - f->mode |= f->mode+1;
2053 + if (f->mode <= 0) fwide(f, 1);
2054 + *ploc = f->locale;
2055
2056 if (isascii(c)) {
2057 c = putc_unlocked(c, f);
2058 @@ -20,6 +23,8 @@ wint_t __fputwc_unlocked(wchar_t c, FILE *f)
2059 l = wctomb(mbc, c);
2060 if (l < 0 || __fwritex((void *)mbc, l, f) < l) c = WEOF;
2061 }
2062 + if (c==WEOF) f->flags |= F_ERR;
2063 + *ploc = loc;
2064 return c;
2065 }
2066
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
2071 @@ -1,23 +1,28 @@
2072 #include "stdio_impl.h"
2073 +#include "locale_impl.h"
2074 #include <wchar.h>
2075
2076 int fputws(const wchar_t *restrict ws, FILE *restrict f)
2077 {
2078 unsigned char buf[BUFSIZ];
2079 size_t l=0;
2080 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
2081
2082 FLOCK(f);
2083
2084 - f->mode |= f->mode+1;
2085 + fwide(f, 1);
2086 + *ploc = f->locale;
2087
2088 while (ws && (l = wcsrtombs((void *)buf, (void*)&ws, sizeof buf, 0))+1 > 1)
2089 if (__fwritex(buf, l, f) < l) {
2090 FUNLOCK(f);
2091 + *ploc = loc;
2092 return -1;
2093 }
2094
2095 FUNLOCK(f);
2096
2097 + *ploc = loc;
2098 return l; /* 0 or -1 */
2099 }
2100
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
2105 @@ -1,13 +1,14 @@
2106 -#include <wchar.h>
2107 #include "stdio_impl.h"
2108 -
2109 -#define SH (8*sizeof(int)-1)
2110 -#define NORMALIZE(x) ((x)>>SH | -((-(x))>>SH))
2111 +#include "locale_impl.h"
2112
2113 int fwide(FILE *f, int mode)
2114 {
2115 FLOCK(f);
2116 - if (!f->mode) f->mode = NORMALIZE(mode);
2117 + if (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;
2121 + }
2122 mode = f->mode;
2123 FUNLOCK(f);
2124 return mode;
2125 diff --git a/src/stdio/ofl.c b/src/stdio/ofl.c
2126 new file mode 100644
2127 index 0000000..b143999
2128 --- /dev/null
2129 +++ b/src/stdio/ofl.c
2130 @@ -0,0 +1,16 @@
2131 +#include "stdio_impl.h"
2132 +#include "libc.h"
2133 +
2134 +static FILE *ofl_head;
2135 +static volatile int ofl_lock[2];
2136 +
2137 +FILE **__ofl_lock()
2138 +{
2139 + LOCK(ofl_lock);
2140 + return &ofl_head;
2141 +}
2142 +
2143 +void __ofl_unlock()
2144 +{
2145 + UNLOCK(ofl_lock);
2146 +}
2147 diff --git a/src/stdio/ofl_add.c b/src/stdio/ofl_add.c
2148 new file mode 100644
2149 index 0000000..d7de9f1
2150 --- /dev/null
2151 +++ b/src/stdio/ofl_add.c
2152 @@ -0,0 +1,11 @@
2153 +#include "stdio_impl.h"
2154 +
2155 +FILE *__ofl_add(FILE *f)
2156 +{
2157 + FILE **head = __ofl_lock();
2158 + f->next = *head;
2159 + if (*head) (*head)->prev = f;
2160 + *head = f;
2161 + __ofl_unlock();
2162 + return f;
2163 +}
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)
2169
2170 if (!libc.threaded) f->lock = -1;
2171
2172 - OFLLOCK();
2173 - f->next = libc.ofl_head;
2174 - if (libc.ofl_head) libc.ofl_head->prev = f;
2175 - libc.ofl_head = f;
2176 - OFLUNLOCK();
2177 -
2178 - return f;
2179 + return __ofl_add(f);
2180 }
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)
2186
2187 if (!libc.threaded) f->lock = -1;
2188
2189 - OFLLOCK();
2190 - f->next = libc.ofl_head;
2191 - if (libc.ofl_head) libc.ofl_head->prev = f;
2192 - libc.ofl_head = f;
2193 - OFLUNLOCK();
2194 -
2195 - return f;
2196 + return __ofl_add(f);
2197 }
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
2202 @@ -1,4 +1,5 @@
2203 #include "stdio_impl.h"
2204 +#include "locale_impl.h"
2205 #include <wchar.h>
2206 #include <limits.h>
2207 #include <ctype.h>
2208 @@ -8,21 +9,19 @@ wint_t ungetwc(wint_t c, FILE *f)
2209 {
2210 unsigned char mbc[MB_LEN_MAX];
2211 int l=1;
2212 -
2213 - if (c == WEOF) return c;
2214 -
2215 - /* Try conversion early so we can fail without locking if invalid */
2216 - if (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)
2217 - return WEOF;
2218 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
2219
2220 FLOCK(f);
2221
2222 - f->mode |= f->mode+1;
2223 + if (f->mode <= 0) fwide(f, 1);
2224 + *ploc = f->locale;
2225
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)) {
2230 FUNLOCK(f);
2231 - return EOF;
2232 + *ploc = loc;
2233 + return WEOF;
2234 }
2235
2236 if (isascii(c)) *--f->rpos = c;
2237 @@ -31,5 +30,6 @@ wint_t ungetwc(wint_t c, FILE *f)
2238 f->flags &= ~F_EOF;
2239
2240 FUNLOCK(f);
2241 + *ploc = loc;
2242 return c;
2243 }
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, "");
2250 l=w;
2251 continue;
2252 + case 'm':
2253 + arg.p = strerror(errno);
2254 case 's':
2255 + if (!arg.p) arg.p = "(null)";
2256 bs = arg.p;
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)
2260 }
2261
2262 FLOCK(f);
2263 - f->mode |= f->mode+1;
2264 + fwide(f, 1);
2265 olderr = f->flags & F_ERR;
2266 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)
2273
2274 FLOCK(f);
2275
2276 - f->mode |= f->mode+1;
2277 + fwide(f, 1);
2278
2279 for (p=fmt; *p; p++) {
2280
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
2285 @@ -2,40 +2,33 @@
2286 #include <ctype.h>
2287 #include <string.h>
2288
2289 -int strverscmp(const char *l, const char *r)
2290 +int strverscmp(const char *l0, const char *r0)
2291 {
2292 - int haszero=1;
2293 - while (*l==*r) {
2294 - if (!*l) return 0;
2295 + const unsigned char *l = (const void *)l0;
2296 + const unsigned char *r = (const void *)r0;
2297 + size_t i, dp, j;
2298 + int z = 1;
2299
2300 - if (*l=='0') {
2301 - if (haszero==1) {
2302 - haszero=0;
2303 - }
2304 - } else if (isdigit(*l)) {
2305 - if (haszero==1) {
2306 - haszero=2;
2307 - }
2308 - } else {
2309 - haszero=1;
2310 - }
2311 - l++; r++;
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++) {
2315 + int c = l[i];
2316 + if (!c) return 0;
2317 + if (!isdigit(c)) dp=i+1, z=1;
2318 + else if (c!='0') z=0;
2319 }
2320 - if (haszero==1 && (*l=='0' || *r=='0')) {
2321 - haszero=0;
2322 - }
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++;
2327 - if (lenl==lenr) {
2328 - return (*l - *r);
2329 - } else if (lenl>lenr) {
2330 - return 1;
2331 - } else {
2332 - return -1;
2333 - }
2334 - } else {
2335 - return (*l - *r);
2336 +
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');
2347 }
2348 +
2349 + return l[i] - r[i];
2350 }
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);
2362 #else
2363 - return (char *)self->dtv[1]+v[1];
2364 + return (char *)self->dtv[1]+v[1]+DTP_OFFSET;
2365 #endif
2366 }
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
2371 @@ -0,0 +1,29 @@
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"
2377 +
2378 +static volatile int lock;
2379 +static void *unmap_base;
2380 +static size_t unmap_size;
2381 +static char shared_stack[256];
2382 +
2383 +static void do_unmap()
2384 +{
2385 + __syscall(SYS_munmap, unmap_base, unmap_size);
2386 + __syscall(SYS_exit);
2387 +}
2388 +
2389 +void __unmapself(void *base, size_t size)
2390 +{
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))
2395 + a_spin();
2396 + __syscall(SYS_set_tid_address, &lock);
2397 + unmap_base = base;
2398 + unmap_size = size;
2399 + CRTJMP(do_unmap, stack);
2400 +}
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
2405 @@ -2,6 +2,7 @@
2406 .global __unmapself
2407 .type __unmapself,@function
2408 __unmapself:
2409 + move $sp, $25
2410 li $2, 4091
2411 syscall
2412 li $4, 0
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)
2423 init_file_lock(f);
2424 + __ofl_unlock();
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
2429 if (guard) {
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);
2436 goto fail;
2437 }
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
2442 @@ -1,6 +0,0 @@
2443 -.global __set_thread_area
2444 -.type __set_thread_area, @function
2445 -__set_thread_area:
2446 - ldc r4, gbr
2447 - rts
2448 - mov #0, r0
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
2453 @@ -1,9 +1,9 @@
2454 .text
2455 -.global __unmapself
2456 -.type __unmapself, @function
2457 -__unmapself:
2458 +.global __unmapself_sh_mmu
2459 +.type __unmapself_sh_mmu, @function
2460 +__unmapself_sh_mmu:
2461 mov #91, r3 ! SYS_munmap
2462 - trapa #18
2463 + trapa #31
2464
2465 or r0, r0
2466 or r0, r0
2467 @@ -13,7 +13,7 @@ __unmapself:
2468
2469 mov #1, r3 ! SYS_exit
2470 mov #0, r4
2471 - trapa #17
2472 + trapa #31
2473
2474 or r0, r0
2475 or r0, r0
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
2484 - trapa #21
2485 + trapa #31
2486
2487 or r0, r0
2488 or r0, r0
2489 @@ -38,7 +38,7 @@ __clone:
2490
2491 mov #1, r3 ! __NR_exit
2492 mov r0, r4
2493 - trapa #17
2494 + trapa #31
2495
2496 or r0, r0
2497 or r0, r0
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-.)
2503 mov.l @(4,r15), r7
2504 mov.l @(8,r15), r0
2505 mov.l @(12,r15), r1
2506 - trapa #22
2507 + trapa #31
2508
2509 __cp_end:
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
2515 @@ -2,7 +2,7 @@
2516 .type pipe, @function
2517 pipe:
2518 mov #42, r3
2519 - trapa #17
2520 + trapa #31
2521
2522 ! work around hardware bug
2523 or r0, r0