33768a59d82e6942b9c241856a43aca0767eefd1
[openwrt/openwrt.git] / toolchain / musl / patches / 001-git-2015-06-20.patch
1 From bafa38541e911806b74a1ab094a404bbdd692ade Mon Sep 17 00:00:00 2001
2 From: Steven Barth <steven@midlink.org>
3 Date: Sat, 20 Jun 2015 16:59:48 +0200
4 Subject: [PATCH] commit 55d061f031085f24d138664c897791aebe9a2fab Author: Rich
5 Felker <dalias@aerifal.cx> Date: Sat Jun 20 03:01:07 2015 +0000
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 provide __stack_chk_fail_local in libc.a
11
12 this symbol is needed only on archs where the PLT call ABI is klunky,
13 and only for position-independent code compiled with stack protector.
14 thus references usually only appear in shared libraries or PIE
15 executables, but they can also appear when linking statically if some
16 of the object files being linked were built as PIC/PIE.
17
18 normally libssp_nonshared.a from the compiler toolchain should provide
19 __stack_chk_fail_local, but reportedly it appears prior to -lc in the
20 link order, thus failing to satisfy references from libc itself (which
21 arise only if libc.a was built as PIC/PIE with stack protector
22 enabled).
23
24 commit ce3688eca920aa77549323f84e21f33522397115
25 Author: Rich Felker <dalias@aerifal.cx>
26 Date: Sat Jun 20 02:54:30 2015 +0000
27
28 work around mips detached thread exit breakage due to kernel regression
29
30 linux kernel commit 46e12c07b3b9603c60fc1d421ff18618241cb081 caused
31 the mips syscall mechanism to fail with EFAULT when the userspace
32 stack pointer is invalid, breaking __unmapself used for detached
33 thread exit. the workaround is to set $sp to a known-valid, readable
34 address, and the simplest one to obtain is the address of the current
35 function, which is available (per o32 calling convention) in $25.
36
37 commit 75eceb3ae824d54e865686c0c538551aeebf3372
38 Author: Rich Felker <dalias@aerifal.cx>
39 Date: Wed Jun 17 17:21:46 2015 +0000
40
41 ignore ENOSYS error from mprotect in pthread_create and dynamic linker
42
43 this error simply indicated a system without memory protection (NOMMU)
44 and should not cause failure in the caller.
45
46 commit 10d0268ccfab9152250eeeed3952ce3fed44131a
47 Author: Rich Felker <dalias@aerifal.cx>
48 Date: Tue Jun 16 15:25:02 2015 +0000
49
50 switch to using trap number 31 for syscalls on sh
51
52 nominally the low bits of the trap number on sh are the number of
53 syscall arguments, but they have never been used by the kernel, and
54 some code making syscalls does not even know the number of arguments
55 and needs to pass an arbitrary high number anyway.
56
57 sh3/sh4 traditionally used the trap range 16-31 for syscalls, but part
58 of this range overlapped with hardware exceptions/interrupts on sh2
59 hardware, so an incompatible range 32-47 was chosen for sh2.
60
61 using trap number 31 everywhere, since it's in the existing sh3/sh4
62 range and does not conflict with sh2 hardware, is a proposed
63 unification of the kernel syscall convention that will allow binaries
64 to be shared between sh2 and sh3/sh4. if this is not accepted into the
65 kernel, we can refit the sh2 target with runtime selection mechanisms
66 for the trap number, but doing so would be invasive and would entail
67 non-trivial overhead.
68
69 commit 3366a99b17847b58f2d8cc52cbb5d65deb824f8a
70 Author: Rich Felker <dalias@aerifal.cx>
71 Date: Tue Jun 16 14:55:06 2015 +0000
72
73 switch sh port's __unmapself to generic version when running on sh2/nommu
74
75 due to the way the interrupt and syscall trap mechanism works,
76 userspace on sh2 must never set the stack pointer to an invalid value.
77 thus, the approach used on most archs, where __unmapself executes with
78 no stack for the interval between SYS_munmap and SYS_exit, is not
79 viable on sh2.
80
81 in order not to pessimize sh3/sh4, the sh asm version of __unmapself
82 is not removed. instead it's renamed and redirected through code that
83 calls either the generic (safe) __unmapself or the sh3/sh4 asm,
84 depending on compile-time and run-time conditions.
85
86 commit f9d84554bae0fa17c9a1d724549c4408022228a5
87 Author: Rich Felker <dalias@aerifal.cx>
88 Date: Tue Jun 16 14:28:30 2015 +0000
89
90 add support for sh2 interrupt-masking-based atomics to sh port
91
92 the sh2 target is being considered an ISA subset of sh3/sh4, in the
93 sense that binaries built for sh2 are intended to be usable on later
94 cpu models/kernels with mmu support. so rather than hard-coding
95 sh2-specific atomics, the runtime atomic selection mechanisms that was
96 already in place has been extended to add sh2 atomics.
97
98 at this time, the sh2 atomics are not SMP-compatible; since the ISA
99 lacks actual atomic operations, the new code instead masks interrupts
100 for the duration of the atomic operation, producing an atomic result
101 on single-core. this is only possible because the kernel/hardware does
102 not impose protections against userspace doing so. additional changes
103 will be needed to support future SMP systems.
104
105 care has been taken to avoid producing significant additional code
106 size in the case where it's known at compile-time that the target is
107 not sh2 and does not need sh2-specific code.
108
109 commit 1b0cdc8700d29ef018bf226d74b2b58b23bce91c
110 Author: Rich Felker <dalias@aerifal.cx>
111 Date: Tue Jun 16 07:11:19 2015 +0000
112
113 refactor stdio open file list handling, move it out of global libc struct
114
115 functions which open in-memory FILE stream variants all shared a tail
116 with __fdopen, adding the FILE structure to stdio's open file list.
117 replacing this common tail with a function call reduces code size and
118 duplication of logic. the list is also partially encapsulated now.
119
120 function signatures were chosen to facilitate tail call optimization
121 and reduce the need for additional accessor functions.
122
123 with these changes, static linked programs that do not use stdio no
124 longer have an open file list at all.
125
126 commit f22a9edaf8a6f2ca1d314d18b3785558279a5c03
127 Author: Rich Felker <dalias@aerifal.cx>
128 Date: Tue Jun 16 06:18:00 2015 +0000
129
130 byte-based C locale, phase 3: make MB_CUR_MAX variable to activate code
131
132 this patch activates the new byte-based C locale (high bytes treated
133 as abstract code unit "characters" rather than decoded as multibyte
134 characters) by making the value of MB_CUR_MAX depend on the active
135 locale. for the C locale, the LC_CTYPE category pointer is null,
136 yielding a value of 1. all other locales yield a value of 4.
137
138 commit 16f18d036d9a7bf590ee6eb86785c0a9658220b6
139 Author: Rich Felker <dalias@aerifal.cx>
140 Date: Tue Jun 16 05:35:31 2015 +0000
141
142 byte-based C locale, phase 2: stdio and iconv (multibyte callers)
143
144 this patch adjusts libc components which use the multibyte functions
145 internally, and which depend on them operating in a particular
146 encoding, to make the appropriate locale changes before calling them
147 and restore the calling thread's locale afterwards. activating the
148 byte-based C locale without these changes would cause regressions in
149 stdio and iconv.
150
151 in the case of iconv, the current implementation was simply using the
152 multibyte functions as UTF-8 conversions. setting a multibyte UTF-8
153 locale for the duration of the iconv operation allows the code to
154 continue working.
155
156 in the case of stdio, POSIX requires that FILE streams have an
157 encoding rule bound at the time of setting wide orientation. as long
158 as all locales, including the C locale, used the same encoding,
159 treating high bytes as UTF-8, there was no need to store an encoding
160 rule as part of the stream's state.
161
162 a new locale field in the FILE structure points to the locale that
163 should be made active during fgetwc/fputwc/ungetwc on the stream. it
164 cannot point to the locale active at the time the stream becomes
165 oriented, because this locale could be mutable (the global locale) or
166 could be destroyed (locale_t objects produced by newlocale) before the
167 stream is closed. instead, a pointer to the static C or C.UTF-8 locale
168 object added in commit commit aeeac9ca5490d7d90fe061ab72da446c01ddf746
169 is used. this is valid since categories other than LC_CTYPE will not
170 affect these functions.
171
172 commit 1507ebf837334e9e07cfab1ca1c2e88449069a80
173 Author: Rich Felker <dalias@aerifal.cx>
174 Date: Tue Jun 16 04:44:17 2015 +0000
175
176 byte-based C locale, phase 1: multibyte character handling functions
177
178 this patch makes the functions which work directly on multibyte
179 characters treat the high bytes as individual abstract code units
180 rather than as multibyte sequences when MB_CUR_MAX is 1. since
181 MB_CUR_MAX is presently defined as a constant 4, all of the new code
182 added is dead code, and optimizing compilers' code generation should
183 not be affected at all. a future commit will activate the new code.
184
185 as abstract code units, bytes 0x80 to 0xff are represented by wchar_t
186 values 0xdf80 to 0xdfff, at the end of the surrogates range. this
187 ensures that they will never be misinterpreted as Unicode characters,
188 and that all wctype functions return false for these "characters"
189 without needing locale-specific logic. a high range outside of Unicode
190 such as 0x7fffff80 to 0x7fffffff was also considered, but since C11's
191 char16_t also needs to be able to represent conversions of these
192 bytes, the surrogate range was the natural choice.
193
194 commit 38e2f727237230300fea6aff68802db04625fd23
195 Author: Rich Felker <dalias@aerifal.cx>
196 Date: Tue Jun 16 04:21:38 2015 +0000
197
198 fix btowc corner case
199
200 btowc is required to interpret its argument by conversion to unsigned
201 char, unless the argument is equal to EOF. since the conversion to
202 produces a non-character value anyway, we can just unconditionally
203 convert, for now.
204
205 commit ee59c296d56bf26f49f354d6eb32b4b6d4190188
206 Author: Szabolcs Nagy <nsz@port70.net>
207 Date: Wed Jun 3 10:32:14 2015 +0100
208
209 arm: add vdso support
210
211 vdso will be available on arm in linux v4.2, the user-space code
212 for it is in kernel commit 8512287a8165592466cb9cb347ba94892e9c56a5
213
214 commit e3bc22f1eff87b8f029a6ab31f1a269d69e4b053
215 Author: Rich Felker <dalias@aerifal.cx>
216 Date: Sun Jun 14 01:59:02 2015 +0000
217
218 refactor malloc's expand_heap to share with __simple_malloc
219
220 this extends the brk/stack collision protection added to full malloc
221 in commit 276904c2f6bde3a31a24ebfa201482601d18b4f9 to also protect the
222 __simple_malloc function used in static-linked programs that don't
223 reference the free function.
224
225 it also extends support for using mmap when brk fails, which full
226 malloc got in commit 5446303328adf4b4e36d9fba21848e6feb55fab4, to
227 __simple_malloc.
228
229 since __simple_malloc may expand the heap by arbitrarily large
230 increments, the stack collision detection is enhanced to detect
231 interval overlap rather than just proximity of a single address to the
232 stack. code size is increased a bit, but this is partly offset by the
233 sharing of code between the two malloc implementations, which due to
234 linking semantics, both get linked in a program that needs the full
235 malloc with realloc/free support.
236
237 commit 4ef9b828c1f39553a69e0635ac91f0fcadd6e8c6
238 Author: Rich Felker <dalias@aerifal.cx>
239 Date: Sat Jun 13 20:53:02 2015 +0000
240
241 remove cancellation points in stdio
242
243 commit 58165923890865a6ac042fafce13f440ee986fd9 added these optional
244 cancellation points on the basis that cancellable stdio could be
245 useful, to unblock threads stuck on stdio operations that will never
246 complete. however, the only way to ensure that cancellation can
247 achieve this is to violate the rules for side effects when
248 cancellation is acted upon, discarding knowledge of any partial data
249 transfer already completed. our implementation exhibited this behavior
250 and was thus non-conforming.
251
252 in addition to improving correctness, removing these cancellation
253 points moderately reduces code size, and should significantly improve
254 performance on i386, where sysenter/syscall instructions can be used
255 instead of "int $128" for non-cancellable syscalls.
256
257 commit 536c6d5a4205e2a3f161f2983ce1e0ac3082187d
258 Author: Rich Felker <dalias@aerifal.cx>
259 Date: Sat Jun 13 05:17:16 2015 +0000
260
261 fix idiom for setting stdio stream orientation to wide
262
263 the old idiom, f->mode |= f->mode+1, was adapted from the idiom for
264 setting byte orientation, f->mode |= f->mode-1, but the adaptation was
265 incorrect. unless the stream was alreasdy set byte-oriented, this code
266 incremented f->mode each time it was executed, which would eventually
267 lead to overflow. it could be fixed by changing it to f->mode |= 1,
268 but upcoming changes will require slightly more work at the time of
269 wide orientation, so it makes sense to just call fwide. as an
270 optimization in the single-character functions, fwide is only called
271 if the stream is not already wide-oriented.
272
273 commit f8f565df467c13248104223f99abf7f37cef7584
274 Author: Rich Felker <dalias@aerifal.cx>
275 Date: Sat Jun 13 04:42:38 2015 +0000
276
277 add printing of null %s arguments as "(null)" in wide printf
278
279 this is undefined, but supported in our implementation of the normal
280 printf, so for consistency the wide variant should support it too.
281
282 commit f9e25d813860d53cd1e9b6145cc63375d2fe2529
283 Author: Rich Felker <dalias@aerifal.cx>
284 Date: Sat Jun 13 04:37:27 2015 +0000
285
286 add %m support to wide printf
287
288 commit ec634aad91f57479ef17525e33ed446c780a61f4
289 Author: Rich Felker <dalias@aerifal.cx>
290 Date: Thu Jun 11 05:01:04 2015 +0000
291
292 add sh asm for vfork
293
294 commit c30cbcb0a646b1f13a22c645616dce624465b883
295 Author: Rich Felker <dalias@aerifal.cx>
296 Date: Wed Jun 10 02:27:40 2015 +0000
297
298 implement arch-generic version of __unmapself
299
300 this can be used to put off writing an asm version of __unmapself for
301 new archs, or as a permanent solution on archs where it's not
302 practical or even possible to run momentarily with no stack.
303
304 the concept here is simple: the caller takes a lock on a global shared
305 stack and uses it to make the munmap and exit syscalls. the only trick
306 is unlocking, which must be done after the thread exits, and this is
307 achieved by using the set_tid_address syscall to have the kernel zero
308 and futex-wake the lock word as part of the exit syscall.
309
310 commit 276904c2f6bde3a31a24ebfa201482601d18b4f9
311 Author: Rich Felker <dalias@aerifal.cx>
312 Date: Tue Jun 9 20:30:35 2015 +0000
313
314 in malloc, refuse to use brk if it grows into stack
315
316 the linux/nommu fdpic ELF loader sets up the brk range to overlap
317 entirely with the main thread's stack (but growing from opposite
318 ends), so that the resulting failure mode for malloc is not to return
319 a null pointer but to start returning pointers to memory that overlaps
320 with the caller's stack. needless to say this extremely dangerous and
321 makes brk unusable.
322
323 since it's non-trivial to detect execution environments that might be
324 affected by this kernel bug, and since the severity of the bug makes
325 any sort of detection that might yield false-negatives unsafe, we
326 instead check the proximity of the brk to the stack pointer each time
327 the brk is to be expanded. both the main thread's stack (where the
328 real known risk lies) and the calling thread's stack are checked. an
329 arbitrary gap distance of 8 MB is imposed, chosen to be larger than
330 linux default main-thread stack reservation sizes and larger than any
331 reasonable stack configuration on nommu.
332
333 the effeciveness of this patch relies on an assumption that the amount
334 by which the brk is being grown is smaller than the gap limit, which
335 is always true for malloc's use of brk. reliance on this assumption is
336 why the check is being done in malloc-specific code and not in __brk.
337
338 commit bd1eaceaa3975bd2a2a34e211cff896affaecadf
339 Author: Rich Felker <dalias@aerifal.cx>
340 Date: Tue Jun 9 20:09:27 2015 +0000
341
342 fix spurious errors from pwd/grp functions when nscd backend is absent
343
344 for several pwd/grp functions, the only way the caller can distinguish
345 between a successful negative result ("no such user/group") and an
346 internal error is by clearing errno before the call and checking errno
347 afterwards. the nscd backend support code correctly simulated a
348 not-found response on systems where such a backend is not running, but
349 failed to restore errno.
350
351 this commit also fixed an outdated/incorrect comment.
352
353 commit 75ce4503950621b11fcc7f1fd1187dbcf3cde312
354 Author: Rich Felker <dalias@aerifal.cx>
355 Date: Sun Jun 7 20:55:23 2015 +0000
356
357 fix regression in pre-v7 arm on kernels with kuser helper removed
358
359 the arm atomics/TLS runtime selection code is called from
360 __set_thread_area and depends on having libc.auxv and __hwcap
361 available. commit 71f099cb7db821c51d8f39dfac622c61e54d794c moved the
362 first call to __set_thread_area to the top of dynamic linking stage 3,
363 before this data is made available, causing the runtime detection code
364 to always see __hwcap as zero and thereby select the atomics/TLS
365 implementations based on kuser helper.
366
367 upcoming work on superh will use similar runtime detection.
368
369 ideally this early-init code should be cleanly refactored and shared
370 between the dynamic linker and static-linked startup.
371
372 commit 32f3c4f70633488550c29a2444f819aafdf345ff
373 Author: Rich Felker <dalias@aerifal.cx>
374 Date: Sun Jun 7 03:09:16 2015 +0000
375
376 add multiple inclusion guard to locale_impl.h
377
378 commit 04b8360adbb6487f61aa0c00e53ec3a90a5a0d29
379 Author: Rich Felker <dalias@aerifal.cx>
380 Date: Sun Jun 7 02:59:49 2015 +0000
381
382 remove redefinition of MB_CUR_MAX in locale_impl.h
383
384 unless/until the byte-based C locale is implemented, defining
385 MB_CUR_MAX to 1 in the C locale is wrong. no internal code currently
386 uses the MB_CUR_MAX macro, but having it defined inconsistently is
387 error-prone. applications get the value from stdlib.h and were
388 unaffected.
389
390 commit 16bf466532d7328e971012b0731ad493b017ad29
391 Author: Rich Felker <dalias@aerifal.cx>
392 Date: Sat Jun 6 18:53:02 2015 +0000
393
394 make static C and C.UTF-8 locales available outside of newlocale
395
396 commit 312eea2ea4f4363fb01b73660c08bfcf43dd3bb4
397 Author: Rich Felker <dalias@aerifal.cx>
398 Date: Sat Jun 6 18:20:30 2015 +0000
399
400 remove another invalid skip of locking in ungetwc
401
402 commit 3d7e32d28dc9962e9efc1c317c5b44b5b2df3008
403 Author: Rich Felker <dalias@aerifal.cx>
404 Date: Sat Jun 6 18:16:22 2015 +0000
405
406 add macro version of ctype.h isascii function
407
408 presumably internal code (ungetwc and fputwc) was written assuming a
409 macro implementation existed; otherwise use of isascii is just a
410 pessimization.
411
412 commit 7e816a6487932cbb3cb71d94b609e50e81f4e5bf
413 Author: Rich Felker <dalias@aerifal.cx>
414 Date: Sat Jun 6 18:11:17 2015 +0000
415
416 remove invalid skip of locking in ungetwc
417
418 aside from being invalid, the early check only optimized the error
419 case, and likely pessimized the common case by separating the
420 two branches on isascii(c) at opposite ends of the function.
421
422 commit 63f4b9f18f3674124d8bcb119739fec85e6da005
423 Author: Timo Teräs <timo.teras@iki.fi>
424 Date: Fri Jun 5 10:39:42 2015 +0300
425
426 fix uselocale((locale_t)0) not to modify locale
427
428 commit 68630b55c0c7219fe9df70dc28ffbf9efc8021d8 made the new locale to
429 be assigned unconditonally resulting in crashes later on.
430 ---
431 arch/arm/syscall_arch.h | 4 ++
432 arch/sh/src/__set_thread_area.c | 34 ++++++++++++++++
433 arch/sh/src/__unmapself.c | 19 +++++++++
434 arch/sh/src/atomic.c | 72 ++++++++++++++++++++++++++++++----
435 arch/sh/src/sh_atomic.h | 15 +++++++
436 arch/sh/syscall_arch.h | 2 +-
437 include/ctype.h | 1 +
438 include/stdlib.h | 3 +-
439 src/ctype/__ctype_get_mb_cur_max.c | 5 ++-
440 src/ctype/isascii.c | 1 +
441 src/env/__stack_chk_fail.c | 4 ++
442 src/internal/libc.h | 2 -
443 src/internal/locale_impl.h | 12 ++++++
444 src/internal/sh/syscall.s | 2 +-
445 src/internal/stdio_impl.h | 6 ++-
446 src/ldso/dynlink.c | 37 +++++++++---------
447 src/locale/c_locale.c | 15 +++++++
448 src/locale/iconv.c | 6 +++
449 src/locale/langinfo.c | 3 +-
450 src/locale/locale_map.c | 12 +-----
451 src/locale/newlocale.c | 15 ++-----
452 src/locale/uselocale.c | 4 +-
453 src/malloc/expand_heap.c | 72 ++++++++++++++++++++++++++++++++++
454 src/malloc/lite_malloc.c | 49 ++++++++++++-----------
455 src/malloc/malloc.c | 80 ++++++++++++++------------------------
456 src/multibyte/btowc.c | 5 ++-
457 src/multibyte/internal.h | 7 ++++
458 src/multibyte/mbrtowc.c | 2 +
459 src/multibyte/mbsrtowcs.c | 19 +++++++++
460 src/multibyte/mbtowc.c | 2 +
461 src/multibyte/wcrtomb.c | 9 +++++
462 src/multibyte/wctob.c | 4 +-
463 src/passwd/nscd_query.c | 12 ++++--
464 src/process/sh/vfork.s | 23 +++++++++++
465 src/regex/fnmatch.c | 3 +-
466 src/signal/sh/restore.s | 4 +-
467 src/stdio/__fdopen.c | 8 +---
468 src/stdio/__stdio_exit.c | 3 +-
469 src/stdio/__stdio_read.c | 11 +-----
470 src/stdio/__stdio_write.c | 14 +------
471 src/stdio/fclose.c | 6 +--
472 src/stdio/fflush.c | 5 +--
473 src/stdio/fgetwc.c | 15 +++++--
474 src/stdio/fmemopen.c | 8 +---
475 src/stdio/fopen.c | 2 +-
476 src/stdio/fputwc.c | 7 +++-
477 src/stdio/fputws.c | 7 +++-
478 src/stdio/fwide.c | 11 +++---
479 src/stdio/ofl.c | 16 ++++++++
480 src/stdio/ofl_add.c | 11 ++++++
481 src/stdio/open_memstream.c | 8 +---
482 src/stdio/open_wmemstream.c | 8 +---
483 src/stdio/ungetwc.c | 18 ++++-----
484 src/stdio/vfwprintf.c | 5 ++-
485 src/stdio/vfwscanf.c | 2 +-
486 src/thread/__unmapself.c | 29 ++++++++++++++
487 src/thread/mips/__unmapself.s | 1 +
488 src/thread/pthread_create.c | 6 ++-
489 src/thread/sh/__set_thread_area.s | 6 ---
490 src/thread/sh/__unmapself.s | 10 ++---
491 src/thread/sh/clone.s | 4 +-
492 src/thread/sh/syscall_cp.s | 2 +-
493 src/unistd/sh/pipe.s | 2 +-
494 63 files changed, 548 insertions(+), 242 deletions(-)
495 create mode 100644 arch/sh/src/__set_thread_area.c
496 create mode 100644 arch/sh/src/__unmapself.c
497 create mode 100644 arch/sh/src/sh_atomic.h
498 create mode 100644 src/locale/c_locale.c
499 create mode 100644 src/malloc/expand_heap.c
500 create mode 100644 src/process/sh/vfork.s
501 create mode 100644 src/stdio/ofl.c
502 create mode 100644 src/stdio/ofl_add.c
503
504 diff --git a/arch/arm/syscall_arch.h b/arch/arm/syscall_arch.h
505 index 199ad2a..64461ec 100644
506 --- a/arch/arm/syscall_arch.h
507 +++ b/arch/arm/syscall_arch.h
508 @@ -72,3 +72,7 @@ static inline long __syscall6(long n, long a, long b, long c, long d, long e, lo
509 register long r5 __asm__("r5") = f;
510 __asm_syscall("r"(r7), "0"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5));
511 }
512 +
513 +#define VDSO_USEFUL
514 +#define VDSO_CGT_SYM "__vdso_clock_gettime"
515 +#define VDSO_CGT_VER "LINUX_2.6"
516 diff --git a/arch/sh/src/__set_thread_area.c b/arch/sh/src/__set_thread_area.c
517 new file mode 100644
518 index 0000000..1d3e022
519 --- /dev/null
520 +++ b/arch/sh/src/__set_thread_area.c
521 @@ -0,0 +1,34 @@
522 +#include "pthread_impl.h"
523 +#include "libc.h"
524 +#include "sh_atomic.h"
525 +#include <elf.h>
526 +
527 +/* Also perform sh-specific init */
528 +
529 +#define CPU_HAS_LLSC 0x0040
530 +
531 +__attribute__((__visibility__("hidden"))) unsigned __sh_atomic_model, __sh_nommu;
532 +
533 +int __set_thread_area(void *p)
534 +{
535 + size_t *aux;
536 + __asm__ __volatile__ ( "ldc %0, gbr" : : "r"(p) : "memory" );
537 +#ifndef __SH4A__
538 + if (__hwcap & CPU_HAS_LLSC) {
539 + __sh_atomic_model = SH_A_LLSC;
540 + return 0;
541 + }
542 +#if !defined(__SH3__) && !defined(__SH4__)
543 + for (aux=libc.auxv; *aux; aux+=2) {
544 + if (*aux != AT_PLATFORM) continue;
545 + const char *s = (void *)aux[1];
546 + if (s[0]!='s' || s[1]!='h' || s[2]!='2' || s[3]-'0'<10u) break;
547 + __sh_atomic_model = SH_A_IMASK;
548 + __sh_nommu = 1;
549 + return 0;
550 + }
551 +#endif
552 + /* __sh_atomic_model = SH_A_GUSA; */ /* 0, default */
553 +#endif
554 + return 0;
555 +}
556 diff --git a/arch/sh/src/__unmapself.c b/arch/sh/src/__unmapself.c
557 new file mode 100644
558 index 0000000..4df9e7b
559 --- /dev/null
560 +++ b/arch/sh/src/__unmapself.c
561 @@ -0,0 +1,19 @@
562 +#include "pthread_impl.h"
563 +
564 +void __unmapself_sh_mmu(void *, size_t);
565 +void __unmapself_sh_nommu(void *, size_t);
566 +
567 +#if !defined(__SH3__) && !defined(__SH4__)
568 +#define __unmapself __unmapself_sh_nommu
569 +#include "../../../src/thread/__unmapself.c"
570 +#undef __unmapself
571 +extern __attribute__((__visibility__("hidden"))) unsigned __sh_nommu;
572 +#else
573 +#define __sh_nommu 0
574 +#endif
575 +
576 +void __unmapself(void *base, size_t size)
577 +{
578 + if (__sh_nommu) __unmapself_sh_nommu(base, size);
579 + else __unmapself_sh_mmu(base, size);
580 +}
581 diff --git a/arch/sh/src/atomic.c b/arch/sh/src/atomic.c
582 index f8c615f..7fd7307 100644
583 --- a/arch/sh/src/atomic.c
584 +++ b/arch/sh/src/atomic.c
585 @@ -1,8 +1,26 @@
586 #ifndef __SH4A__
587
588 +#include "sh_atomic.h"
589 #include "atomic.h"
590 #include "libc.h"
591
592 +static inline unsigned mask()
593 +{
594 + unsigned sr;
595 + __asm__ __volatile__ ( "\n"
596 + " stc sr,r0 \n"
597 + " mov r0,%0 \n"
598 + " or #0xf0,r0 \n"
599 + " ldc r0,sr \n"
600 + : "=&r"(sr) : : "memory", "r0" );
601 + return sr;
602 +}
603 +
604 +static inline void unmask(unsigned sr)
605 +{
606 + __asm__ __volatile__ ( "ldc %0,sr" : : "r"(sr) : "memory" );
607 +}
608 +
609 /* gusa is a hack in the kernel which lets you create a sequence of instructions
610 * which will be restarted if the process is preempted in the middle of the
611 * sequence. It will do for implementing atomics on non-smp systems. ABI is:
612 @@ -25,11 +43,17 @@
613 " mov.l " new ", @" mem "\n" \
614 "1: mov r1, r15\n"
615
616 -#define CPU_HAS_LLSC 0x0040
617 -
618 int __sh_cas(volatile int *p, int t, int s)
619 {
620 - if (__hwcap & CPU_HAS_LLSC) return __sh_cas_llsc(p, t, s);
621 + if (__sh_atomic_model == SH_A_LLSC) return __sh_cas_llsc(p, t, s);
622 +
623 + if (__sh_atomic_model == SH_A_IMASK) {
624 + unsigned sr = mask();
625 + int old = *p;
626 + if (old==t) *p = s;
627 + unmask(sr);
628 + return old;
629 + }
630
631 int old;
632 __asm__ __volatile__(
633 @@ -43,7 +67,15 @@ int __sh_cas(volatile int *p, int t, int s)
634
635 int __sh_swap(volatile int *x, int v)
636 {
637 - if (__hwcap & CPU_HAS_LLSC) return __sh_swap_llsc(x, v);
638 + if (__sh_atomic_model == SH_A_LLSC) return __sh_swap_llsc(x, v);
639 +
640 + if (__sh_atomic_model == SH_A_IMASK) {
641 + unsigned sr = mask();
642 + int old = *x;
643 + *x = v;
644 + unmask(sr);
645 + return old;
646 + }
647
648 int old;
649 __asm__ __volatile__(
650 @@ -55,7 +87,15 @@ int __sh_swap(volatile int *x, int v)
651
652 int __sh_fetch_add(volatile int *x, int v)
653 {
654 - if (__hwcap & CPU_HAS_LLSC) return __sh_fetch_add_llsc(x, v);
655 + if (__sh_atomic_model == SH_A_LLSC) return __sh_fetch_add_llsc(x, v);
656 +
657 + if (__sh_atomic_model == SH_A_IMASK) {
658 + unsigned sr = mask();
659 + int old = *x;
660 + *x = old + v;
661 + unmask(sr);
662 + return old;
663 + }
664
665 int old, dummy;
666 __asm__ __volatile__(
667 @@ -69,7 +109,7 @@ int __sh_fetch_add(volatile int *x, int v)
668
669 void __sh_store(volatile int *p, int x)
670 {
671 - if (__hwcap & CPU_HAS_LLSC) return __sh_store_llsc(p, x);
672 + if (__sh_atomic_model == SH_A_LLSC) return __sh_store_llsc(p, x);
673 __asm__ __volatile__(
674 " mov.l %1, @%0\n"
675 : : "r"(p), "r"(x) : "memory");
676 @@ -77,7 +117,15 @@ void __sh_store(volatile int *p, int x)
677
678 void __sh_and(volatile int *x, int v)
679 {
680 - if (__hwcap & CPU_HAS_LLSC) return __sh_and_llsc(x, v);
681 + if (__sh_atomic_model == SH_A_LLSC) return __sh_and_llsc(x, v);
682 +
683 + if (__sh_atomic_model == SH_A_IMASK) {
684 + unsigned sr = mask();
685 + int old = *x;
686 + *x = old & v;
687 + unmask(sr);
688 + return;
689 + }
690
691 int dummy;
692 __asm__ __volatile__(
693 @@ -89,7 +137,15 @@ void __sh_and(volatile int *x, int v)
694
695 void __sh_or(volatile int *x, int v)
696 {
697 - if (__hwcap & CPU_HAS_LLSC) return __sh_or_llsc(x, v);
698 + if (__sh_atomic_model == SH_A_LLSC) return __sh_or_llsc(x, v);
699 +
700 + if (__sh_atomic_model == SH_A_IMASK) {
701 + unsigned sr = mask();
702 + int old = *x;
703 + *x = old | v;
704 + unmask(sr);
705 + return;
706 + }
707
708 int dummy;
709 __asm__ __volatile__(
710 diff --git a/arch/sh/src/sh_atomic.h b/arch/sh/src/sh_atomic.h
711 new file mode 100644
712 index 0000000..054c2a3
713 --- /dev/null
714 +++ b/arch/sh/src/sh_atomic.h
715 @@ -0,0 +1,15 @@
716 +#ifndef _SH_ATOMIC_H
717 +#define _SH_ATOMIC_H
718 +
719 +#define SH_A_GUSA 0
720 +#define SH_A_LLSC 1
721 +#define SH_A_CAS 2
722 +#if !defined(__SH3__) && !defined(__SH4__)
723 +#define SH_A_IMASK 3
724 +#else
725 +#define SH_A_IMASK -1LL /* unmatchable by unsigned int */
726 +#endif
727 +
728 +extern __attribute__((__visibility__("hidden"))) unsigned __sh_atomic_model;
729 +
730 +#endif
731 diff --git a/arch/sh/syscall_arch.h b/arch/sh/syscall_arch.h
732 index 7ee21a5..f63675a 100644
733 --- a/arch/sh/syscall_arch.h
734 +++ b/arch/sh/syscall_arch.h
735 @@ -8,7 +8,7 @@
736 */
737 #define __asm_syscall(trapno, ...) do { \
738 __asm__ __volatile__ ( \
739 - "trapa #" #trapno "\n" \
740 + "trapa #31\n" \
741 "or r0, r0\n" \
742 "or r0, r0\n" \
743 "or r0, r0\n" \
744 diff --git a/include/ctype.h b/include/ctype.h
745 index cd2e016..7936536 100644
746 --- a/include/ctype.h
747 +++ b/include/ctype.h
748 @@ -64,6 +64,7 @@ int isascii(int);
749 int toascii(int);
750 #define _tolower(a) ((a)|0x20)
751 #define _toupper(a) ((a)&0x5f)
752 +#define isascii(a) (0 ? isascii(a) : (unsigned)(a) < 128)
753
754 #endif
755
756 diff --git a/include/stdlib.h b/include/stdlib.h
757 index 97ce5a7..d2c911f 100644
758 --- a/include/stdlib.h
759 +++ b/include/stdlib.h
760 @@ -76,7 +76,8 @@ size_t wcstombs (char *__restrict, const wchar_t *__restrict, size_t);
761 #define EXIT_FAILURE 1
762 #define EXIT_SUCCESS 0
763
764 -#define MB_CUR_MAX ((size_t)+4)
765 +size_t __ctype_get_mb_cur_max(void);
766 +#define MB_CUR_MAX (__ctype_get_mb_cur_max())
767
768 #define RAND_MAX (0x7fffffff)
769
770 diff --git a/src/ctype/__ctype_get_mb_cur_max.c b/src/ctype/__ctype_get_mb_cur_max.c
771 index d235f4d..8e946fc 100644
772 --- a/src/ctype/__ctype_get_mb_cur_max.c
773 +++ b/src/ctype/__ctype_get_mb_cur_max.c
774 @@ -1,6 +1,7 @@
775 -#include <stddef.h>
776 +#include <stdlib.h>
777 +#include "locale_impl.h"
778
779 size_t __ctype_get_mb_cur_max()
780 {
781 - return 4;
782 + return MB_CUR_MAX;
783 }
784 diff --git a/src/ctype/isascii.c b/src/ctype/isascii.c
785 index 3af0a10..54ad3bf 100644
786 --- a/src/ctype/isascii.c
787 +++ b/src/ctype/isascii.c
788 @@ -1,4 +1,5 @@
789 #include <ctype.h>
790 +#undef isascii
791
792 int isascii(int c)
793 {
794 diff --git a/src/env/__stack_chk_fail.c b/src/env/__stack_chk_fail.c
795 index 47784c6..be0c184 100644
796 --- a/src/env/__stack_chk_fail.c
797 +++ b/src/env/__stack_chk_fail.c
798 @@ -25,4 +25,8 @@ void __stack_chk_fail_local(void)
799 a_crash();
800 }
801
802 +#else
803 +
804 +weak_alias(__stack_chk_fail, __stack_chk_fail_local);
805 +
806 #endif
807 diff --git a/src/internal/libc.h b/src/internal/libc.h
808 index 6810cd8..98c7535 100644
809 --- a/src/internal/libc.h
810 +++ b/src/internal/libc.h
811 @@ -17,8 +17,6 @@ struct __libc {
812 int secure;
813 volatile int threads_minus_1;
814 size_t *auxv;
815 - FILE *ofl_head;
816 - volatile int ofl_lock[2];
817 size_t tls_size;
818 size_t page_size;
819 struct __locale_struct global_locale;
820 diff --git a/src/internal/locale_impl.h b/src/internal/locale_impl.h
821 index 9b8385e..f5e4d9b 100644
822 --- a/src/internal/locale_impl.h
823 +++ b/src/internal/locale_impl.h
824 @@ -1,3 +1,6 @@
825 +#ifndef _LOCALE_IMPL_H
826 +#define _LOCALE_IMPL_H
827 +
828 #include <locale.h>
829 #include <stdlib.h>
830 #include "libc.h"
831 @@ -12,6 +15,10 @@ struct __locale_map {
832 const struct __locale_map *next;
833 };
834
835 +extern const struct __locale_map __c_dot_utf8;
836 +extern const struct __locale_struct __c_locale;
837 +extern const struct __locale_struct __c_dot_utf8_locale;
838 +
839 const struct __locale_map *__get_locale(int, const char *);
840 const char *__mo_lookup(const void *, size_t, const char *);
841 const char *__lctrans(const char *, const struct __locale_map *);
842 @@ -20,9 +27,14 @@ const char *__lctrans_cur(const char *);
843 #define LCTRANS(msg, lc, loc) __lctrans(msg, (loc)->cat[(lc)])
844 #define LCTRANS_CUR(msg) __lctrans_cur(msg)
845
846 +#define C_LOCALE ((locale_t)&__c_locale)
847 +#define UTF8_LOCALE ((locale_t)&__c_dot_utf8_locale)
848 +
849 #define CURRENT_LOCALE (__pthread_self()->locale)
850
851 #define CURRENT_UTF8 (!!__pthread_self()->locale->cat[LC_CTYPE])
852
853 #undef MB_CUR_MAX
854 #define MB_CUR_MAX (CURRENT_UTF8 ? 4 : 1)
855 +
856 +#endif
857 diff --git a/src/internal/sh/syscall.s b/src/internal/sh/syscall.s
858 index d00712a..331918a 100644
859 --- a/src/internal/sh/syscall.s
860 +++ b/src/internal/sh/syscall.s
861 @@ -13,7 +13,7 @@ __syscall:
862 mov.l @r15, r7
863 mov.l @(4,r15), r0
864 mov.l @(8,r15), r1
865 - trapa #22
866 + trapa #31
867 or r0, r0
868 or r0, r0
869 or r0, r0
870 diff --git a/src/internal/stdio_impl.h b/src/internal/stdio_impl.h
871 index e1325fe..0dd7fb5 100644
872 --- a/src/internal/stdio_impl.h
873 +++ b/src/internal/stdio_impl.h
874 @@ -47,6 +47,7 @@ struct _IO_FILE {
875 unsigned char *shend;
876 off_t shlim, shcnt;
877 FILE *prev_locked, *next_locked;
878 + struct __locale_struct *locale;
879 };
880
881 size_t __stdio_read(FILE *, unsigned char *, size_t);
882 @@ -75,8 +76,9 @@ int __putc_unlocked(int, FILE *);
883 FILE *__fdopen(int, const char *);
884 int __fmodeflags(const char *);
885
886 -#define OFLLOCK() LOCK(libc.ofl_lock)
887 -#define OFLUNLOCK() UNLOCK(libc.ofl_lock)
888 +FILE *__ofl_add(FILE *f);
889 +FILE **__ofl_lock(void);
890 +void __ofl_unlock(void);
891
892 #define feof(f) ((f)->flags & F_EOF)
893 #define ferror(f) ((f)->flags & F_ERR)
894 diff --git a/src/ldso/dynlink.c b/src/ldso/dynlink.c
895 index 42b056d..7e56693 100644
896 --- a/src/ldso/dynlink.c
897 +++ b/src/ldso/dynlink.c
898 @@ -536,7 +536,8 @@ static void *map_library(int fd, struct dso *dso)
899 }
900 for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
901 if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
902 - if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) < 0)
903 + if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC)
904 + && errno != ENOSYS)
905 goto error;
906 break;
907 }
908 @@ -927,7 +928,8 @@ static void reloc_all(struct dso *p)
909 do_relocs(p, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], 3);
910
911 if (head != &ldso && p->relro_start != p->relro_end &&
912 - mprotect(p->base+p->relro_start, p->relro_end-p->relro_start, PROT_READ) < 0) {
913 + mprotect(p->base+p->relro_start, p->relro_end-p->relro_start, PROT_READ)
914 + && errno != ENOSYS) {
915 error("Error relocating %s: RELRO protection failed: %m",
916 p->name);
917 if (runtime) longjmp(*rtld_fail, 1);
918 @@ -1192,6 +1194,17 @@ _Noreturn void __dls3(size_t *sp)
919 char **argv_orig = argv;
920 char **envp = argv+argc+1;
921
922 + /* Find aux vector just past environ[] and use it to initialize
923 + * global data that may be needed before we can make syscalls. */
924 + __environ = envp;
925 + for (i=argc+1; argv[i]; i++);
926 + libc.auxv = auxv = (void *)(argv+i+1);
927 + decode_vec(auxv, aux, AUX_CNT);
928 + __hwcap = aux[AT_HWCAP];
929 + libc.page_size = aux[AT_PAGESZ];
930 + libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
931 + || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]);
932 +
933 /* Setup early thread pointer in builtin_tls for ldso/libc itself to
934 * use during dynamic linking. If possible it will also serve as the
935 * thread pointer at runtime. */
936 @@ -1200,25 +1213,11 @@ _Noreturn void __dls3(size_t *sp)
937 a_crash();
938 }
939
940 - /* Find aux vector just past environ[] */
941 - for (i=argc+1; argv[i]; i++)
942 - if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
943 - env_path = argv[i]+16;
944 - else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
945 - env_preload = argv[i]+11;
946 - auxv = (void *)(argv+i+1);
947 -
948 - decode_vec(auxv, aux, AUX_CNT);
949 -
950 /* Only trust user/env if kernel says we're not suid/sgid */
951 - if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
952 - || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
953 - env_path = 0;
954 - env_preload = 0;
955 - libc.secure = 1;
956 + if (!libc.secure) {
957 + env_path = getenv("LD_LIBRARY_PATH");
958 + env_preload = getenv("LD_PRELOAD");
959 }
960 - libc.page_size = aux[AT_PAGESZ];
961 - libc.auxv = auxv;
962
963 /* If the main program was already loaded by the kernel,
964 * AT_PHDR will point to some location other than the dynamic
965 diff --git a/src/locale/c_locale.c b/src/locale/c_locale.c
966 new file mode 100644
967 index 0000000..77ccf58
968 --- /dev/null
969 +++ b/src/locale/c_locale.c
970 @@ -0,0 +1,15 @@
971 +#include "locale_impl.h"
972 +#include <stdint.h>
973 +
974 +static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };
975 +
976 +const struct __locale_map __c_dot_utf8 = {
977 + .map = empty_mo,
978 + .map_size = sizeof empty_mo,
979 + .name = "C.UTF-8"
980 +};
981 +
982 +const struct __locale_struct __c_locale = { 0 };
983 +const struct __locale_struct __c_dot_utf8_locale = {
984 + .cat[LC_CTYPE] = &__c_dot_utf8
985 +};
986 diff --git a/src/locale/iconv.c b/src/locale/iconv.c
987 index e6121ae..1eeea94 100644
988 --- a/src/locale/iconv.c
989 +++ b/src/locale/iconv.c
990 @@ -5,6 +5,7 @@
991 #include <stdlib.h>
992 #include <limits.h>
993 #include <stdint.h>
994 +#include "locale_impl.h"
995
996 #define UTF_32BE 0300
997 #define UTF_16LE 0301
998 @@ -165,9 +166,12 @@ size_t iconv(iconv_t cd0, char **restrict in, size_t *restrict inb, char **restr
999 int err;
1000 unsigned char type = map[-1];
1001 unsigned char totype = tomap[-1];
1002 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
1003
1004 if (!in || !*in || !*inb) return 0;
1005
1006 + *ploc = UTF8_LOCALE;
1007 +
1008 for (; *inb; *in+=l, *inb-=l) {
1009 c = *(unsigned char *)*in;
1010 l = 1;
1011 @@ -431,6 +435,7 @@ size_t iconv(iconv_t cd0, char **restrict in, size_t *restrict inb, char **restr
1012 break;
1013 }
1014 }
1015 + *ploc = loc;
1016 return x;
1017 ilseq:
1018 err = EILSEQ;
1019 @@ -445,5 +450,6 @@ starved:
1020 x = -1;
1021 end:
1022 errno = err;
1023 + *ploc = loc;
1024 return x;
1025 }
1026 diff --git a/src/locale/langinfo.c b/src/locale/langinfo.c
1027 index a1ada24..776b447 100644
1028 --- a/src/locale/langinfo.c
1029 +++ b/src/locale/langinfo.c
1030 @@ -33,7 +33,8 @@ char *__nl_langinfo_l(nl_item item, locale_t loc)
1031 int idx = item & 65535;
1032 const char *str;
1033
1034 - if (item == CODESET) return "UTF-8";
1035 + if (item == CODESET)
1036 + return MB_CUR_MAX==1 ? "UTF-8-CODE-UNITS" : "UTF-8";
1037
1038 switch (cat) {
1039 case LC_NUMERIC:
1040 diff --git a/src/locale/locale_map.c b/src/locale/locale_map.c
1041 index 4346bb0..c3e5917 100644
1042 --- a/src/locale/locale_map.c
1043 +++ b/src/locale/locale_map.c
1044 @@ -24,14 +24,6 @@ static const char envvars[][12] = {
1045 "LC_MESSAGES",
1046 };
1047
1048 -static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };
1049 -
1050 -const struct __locale_map __c_dot_utf8 = {
1051 - .map = empty_mo,
1052 - .map_size = sizeof empty_mo,
1053 - .name = "C.UTF-8"
1054 -};
1055 -
1056 const struct __locale_map *__get_locale(int cat, const char *val)
1057 {
1058 static int lock[2];
1059 @@ -107,8 +99,8 @@ const struct __locale_map *__get_locale(int cat, const char *val)
1060 * sake of being able to do message translations at the
1061 * application level. */
1062 if (!new && (new = malloc(sizeof *new))) {
1063 - new->map = empty_mo;
1064 - new->map_size = sizeof empty_mo;
1065 + new->map = __c_dot_utf8.map;
1066 + new->map_size = __c_dot_utf8.map_size;
1067 memcpy(new->name, val, n);
1068 new->name[n] = 0;
1069 new->next = loc_head;
1070 diff --git a/src/locale/newlocale.c b/src/locale/newlocale.c
1071 index 89d36b1..f50bbe9 100644
1072 --- a/src/locale/newlocale.c
1073 +++ b/src/locale/newlocale.c
1074 @@ -3,16 +3,9 @@
1075 #include "locale_impl.h"
1076 #include "libc.h"
1077
1078 -extern const struct __locale_map __c_dot_utf8;
1079 -
1080 -static const struct __locale_struct c_locale = { 0 };
1081 -static const struct __locale_struct c_dot_utf8_locale = {
1082 - .cat[LC_CTYPE] = &__c_dot_utf8
1083 -};
1084 -
1085 int __loc_is_allocated(locale_t loc)
1086 {
1087 - return loc && loc != &c_locale && loc != &c_dot_utf8_locale;
1088 + return loc && loc != C_LOCALE && loc != UTF8_LOCALE;
1089 }
1090
1091 locale_t __newlocale(int mask, const char *name, locale_t loc)
1092 @@ -44,9 +37,9 @@ locale_t __newlocale(int mask, const char *name, locale_t loc)
1093 }
1094
1095 if (!j)
1096 - return (locale_t)&c_locale;
1097 - if (j==1 && tmp.cat[LC_CTYPE]==c_dot_utf8_locale.cat[LC_CTYPE])
1098 - return (locale_t)&c_dot_utf8_locale;
1099 + return C_LOCALE;
1100 + if (j==1 && tmp.cat[LC_CTYPE]==&__c_dot_utf8)
1101 + return UTF8_LOCALE;
1102
1103 if ((loc = malloc(sizeof *loc))) *loc = tmp;
1104
1105 diff --git a/src/locale/uselocale.c b/src/locale/uselocale.c
1106 index b70a0c1..0fc5ecb 100644
1107 --- a/src/locale/uselocale.c
1108 +++ b/src/locale/uselocale.c
1109 @@ -8,9 +8,7 @@ locale_t __uselocale(locale_t new)
1110 locale_t old = self->locale;
1111 locale_t global = &libc.global_locale;
1112
1113 - if (new == LC_GLOBAL_LOCALE) new = global;
1114 -
1115 - self->locale = new;
1116 + if (new) self->locale = new == LC_GLOBAL_LOCALE ? global : new;
1117
1118 return old == global ? LC_GLOBAL_LOCALE : old;
1119 }
1120 diff --git a/src/malloc/expand_heap.c b/src/malloc/expand_heap.c
1121 new file mode 100644
1122 index 0000000..d8c0be7
1123 --- /dev/null
1124 +++ b/src/malloc/expand_heap.c
1125 @@ -0,0 +1,72 @@
1126 +#include <limits.h>
1127 +#include <stdint.h>
1128 +#include <errno.h>
1129 +#include <sys/mman.h>
1130 +#include "libc.h"
1131 +#include "syscall.h"
1132 +
1133 +/* This function returns true if the interval [old,new]
1134 + * intersects the 'len'-sized interval below &libc.auxv
1135 + * (interpreted as the main-thread stack) or below &b
1136 + * (the current stack). It is used to defend against
1137 + * buggy brk implementations that can cross the stack. */
1138 +
1139 +static int traverses_stack_p(uintptr_t old, uintptr_t new)
1140 +{
1141 + const uintptr_t len = 8<<20;
1142 + uintptr_t a, b;
1143 +
1144 + b = (uintptr_t)libc.auxv;
1145 + a = b > len ? b-len : 0;
1146 + if (new>a && old<b) return 1;
1147 +
1148 + b = (uintptr_t)&b;
1149 + a = b > len ? b-len : 0;
1150 + if (new>a && old<b) return 1;
1151 +
1152 + return 0;
1153 +}
1154 +
1155 +void *__mmap(void *, size_t, int, int, int, off_t);
1156 +
1157 +/* Expand the heap in-place if brk can be used, or otherwise via mmap,
1158 + * using an exponential lower bound on growth by mmap to make
1159 + * fragmentation asymptotically irrelevant. The size argument is both
1160 + * an input and an output, since the caller needs to know the size
1161 + * allocated, which will be larger than requested due to page alignment
1162 + * and mmap minimum size rules. The caller is responsible for locking
1163 + * to prevent concurrent calls. */
1164 +
1165 +void *__expand_heap(size_t *pn)
1166 +{
1167 + static uintptr_t brk;
1168 + static unsigned mmap_step;
1169 + size_t n = *pn;
1170 +
1171 + if (n > SIZE_MAX/2 - PAGE_SIZE) {
1172 + errno = ENOMEM;
1173 + return 0;
1174 + }
1175 + n += -n & PAGE_SIZE-1;
1176 +
1177 + if (!brk) {
1178 + brk = __syscall(SYS_brk, 0);
1179 + brk += -brk & PAGE_SIZE-1;
1180 + }
1181 +
1182 + if (n < SIZE_MAX-brk && !traverses_stack_p(brk, brk+n)
1183 + && __syscall(SYS_brk, brk+n)==brk+n) {
1184 + *pn = n;
1185 + brk += n;
1186 + return (void *)(brk-n);
1187 + }
1188 +
1189 + size_t min = (size_t)PAGE_SIZE << mmap_step/2;
1190 + if (n < min) n = min;
1191 + void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
1192 + MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1193 + if (area == MAP_FAILED) return 0;
1194 + *pn = n;
1195 + mmap_step++;
1196 + return area;
1197 +}
1198 diff --git a/src/malloc/lite_malloc.c b/src/malloc/lite_malloc.c
1199 index 7643fc2..008549d 100644
1200 --- a/src/malloc/lite_malloc.c
1201 +++ b/src/malloc/lite_malloc.c
1202 @@ -4,43 +4,46 @@
1203 #include <errno.h>
1204 #include "libc.h"
1205
1206 -uintptr_t __brk(uintptr_t);
1207 -
1208 #define ALIGN 16
1209
1210 +void *__expand_heap(size_t *);
1211 +
1212 void *__simple_malloc(size_t n)
1213 {
1214 - static uintptr_t cur, brk;
1215 - uintptr_t base, new;
1216 + static char *cur, *end;
1217 static volatile int lock[2];
1218 - size_t align=1;
1219 + size_t align=1, pad;
1220 + void *p;
1221
1222 if (!n) n++;
1223 - if (n > SIZE_MAX/2) goto toobig;
1224 -
1225 while (align<n && align<ALIGN)
1226 align += align;
1227 - n = n + align - 1 & -align;
1228
1229 LOCK(lock);
1230 - if (!cur) cur = brk = __brk(0)+16;
1231 - base = cur + align-1 & -align;
1232 - if (n > SIZE_MAX - PAGE_SIZE - base) goto fail;
1233 - if (base+n > brk) {
1234 - new = base+n + PAGE_SIZE-1 & -PAGE_SIZE;
1235 - if (__brk(new) != new) goto fail;
1236 - brk = new;
1237 - }
1238 - cur = base+n;
1239 - UNLOCK(lock);
1240
1241 - return (void *)base;
1242 + pad = -(uintptr_t)cur & align-1;
1243 +
1244 + if (n <= SIZE_MAX/2 + ALIGN) n += pad;
1245 +
1246 + if (n > end-cur) {
1247 + size_t m = n;
1248 + char *new = __expand_heap(&m);
1249 + if (!new) {
1250 + UNLOCK(lock);
1251 + return 0;
1252 + }
1253 + if (new != end) {
1254 + cur = new;
1255 + n -= pad;
1256 + pad = 0;
1257 + }
1258 + end = new + m;
1259 + }
1260
1261 -fail:
1262 + p = cur + pad;
1263 + cur += n;
1264 UNLOCK(lock);
1265 -toobig:
1266 - errno = ENOMEM;
1267 - return 0;
1268 + return p;
1269 }
1270
1271 weak_alias(__simple_malloc, malloc);
1272 diff --git a/src/malloc/malloc.c b/src/malloc/malloc.c
1273 index d4de2dc..290fda1 100644
1274 --- a/src/malloc/malloc.c
1275 +++ b/src/malloc/malloc.c
1276 @@ -13,7 +13,6 @@
1277 #define inline inline __attribute__((always_inline))
1278 #endif
1279
1280 -uintptr_t __brk(uintptr_t);
1281 void *__mmap(void *, size_t, int, int, int, off_t);
1282 int __munmap(void *, size_t);
1283 void *__mremap(void *, size_t, size_t, int, ...);
1284 @@ -31,13 +30,9 @@ struct bin {
1285 };
1286
1287 static struct {
1288 - uintptr_t brk;
1289 - size_t *heap;
1290 volatile uint64_t binmap;
1291 struct bin bins[64];
1292 - volatile int brk_lock[2];
1293 volatile int free_lock[2];
1294 - unsigned mmap_step;
1295 } mal;
1296
1297
1298 @@ -152,69 +147,52 @@ void __dump_heap(int x)
1299 }
1300 #endif
1301
1302 +void *__expand_heap(size_t *);
1303 +
1304 static struct chunk *expand_heap(size_t n)
1305 {
1306 - static int init;
1307 + static int heap_lock[2];
1308 + static void *end;
1309 + void *p;
1310 struct chunk *w;
1311 - uintptr_t new;
1312 -
1313 - lock(mal.brk_lock);
1314
1315 - if (!init) {
1316 - mal.brk = __brk(0);
1317 -#ifdef SHARED
1318 - mal.brk = mal.brk + PAGE_SIZE-1 & -PAGE_SIZE;
1319 -#endif
1320 - mal.brk = mal.brk + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
1321 - mal.heap = (void *)mal.brk;
1322 - init = 1;
1323 - }
1324 + /* The argument n already accounts for the caller's chunk
1325 + * overhead needs, but if the heap can't be extended in-place,
1326 + * we need room for an extra zero-sized sentinel chunk. */
1327 + n += SIZE_ALIGN;
1328
1329 - if (n > SIZE_MAX - mal.brk - 2*PAGE_SIZE) goto fail;
1330 - new = mal.brk + n + SIZE_ALIGN + PAGE_SIZE - 1 & -PAGE_SIZE;
1331 - n = new - mal.brk;
1332 + lock(heap_lock);
1333
1334 - if (__brk(new) != new) {
1335 - size_t min = (size_t)PAGE_SIZE << mal.mmap_step/2;
1336 - n += -n & PAGE_SIZE-1;
1337 - if (n < min) n = min;
1338 - void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
1339 - MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1340 - if (area == MAP_FAILED) goto fail;
1341 + p = __expand_heap(&n);
1342 + if (!p) {
1343 + unlock(heap_lock);
1344 + return 0;
1345 + }
1346
1347 - mal.mmap_step++;
1348 - area = (char *)area + SIZE_ALIGN - OVERHEAD;
1349 - w = area;
1350 + /* If not just expanding existing space, we need to make a
1351 + * new sentinel chunk below the allocated space. */
1352 + if (p != end) {
1353 + /* Valid/safe because of the prologue increment. */
1354 n -= SIZE_ALIGN;
1355 + p = (char *)p + SIZE_ALIGN;
1356 + w = MEM_TO_CHUNK(p);
1357 w->psize = 0 | C_INUSE;
1358 - w->csize = n | C_INUSE;
1359 - w = NEXT_CHUNK(w);
1360 - w->psize = n | C_INUSE;
1361 - w->csize = 0 | C_INUSE;
1362 -
1363 - unlock(mal.brk_lock);
1364 -
1365 - return area;
1366 }
1367
1368 - w = MEM_TO_CHUNK(mal.heap);
1369 - w->psize = 0 | C_INUSE;
1370 -
1371 - w = MEM_TO_CHUNK(new);
1372 + /* Record new heap end and fill in footer. */
1373 + end = (char *)p + n;
1374 + w = MEM_TO_CHUNK(end);
1375 w->psize = n | C_INUSE;
1376 w->csize = 0 | C_INUSE;
1377
1378 - w = MEM_TO_CHUNK(mal.brk);
1379 + /* Fill in header, which may be new or may be replacing a
1380 + * zero-size sentinel header at the old end-of-heap. */
1381 + w = MEM_TO_CHUNK(p);
1382 w->csize = n | C_INUSE;
1383 - mal.brk = new;
1384 -
1385 - unlock(mal.brk_lock);
1386 +
1387 + unlock(heap_lock);
1388
1389 return w;
1390 -fail:
1391 - unlock(mal.brk_lock);
1392 - errno = ENOMEM;
1393 - return 0;
1394 }
1395
1396 static int adjust_size(size_t *n)
1397 diff --git a/src/multibyte/btowc.c b/src/multibyte/btowc.c
1398 index 9d2c3b1..8acd0a2 100644
1399 --- a/src/multibyte/btowc.c
1400 +++ b/src/multibyte/btowc.c
1401 @@ -1,7 +1,10 @@
1402 #include <stdio.h>
1403 #include <wchar.h>
1404 +#include <stdlib.h>
1405 +#include "internal.h"
1406
1407 wint_t btowc(int c)
1408 {
1409 - return c<128U ? c : EOF;
1410 + int b = (unsigned char)c;
1411 + return b<128U ? b : (MB_CUR_MAX==1 && c!=EOF) ? CODEUNIT(c) : WEOF;
1412 }
1413 diff --git a/src/multibyte/internal.h b/src/multibyte/internal.h
1414 index cc017fa..53d62ed 100644
1415 --- a/src/multibyte/internal.h
1416 +++ b/src/multibyte/internal.h
1417 @@ -23,3 +23,10 @@ extern const uint32_t bittab[];
1418
1419 #define SA 0xc2u
1420 #define SB 0xf4u
1421 +
1422 +/* Arbitrary encoding for representing code units instead of characters. */
1423 +#define CODEUNIT(c) (0xdfff & (signed char)(c))
1424 +#define IS_CODEUNIT(c) ((unsigned)(c)-0xdf80 < 0x80)
1425 +
1426 +/* Get inline definition of MB_CUR_MAX. */
1427 +#include "locale_impl.h"
1428 diff --git a/src/multibyte/mbrtowc.c b/src/multibyte/mbrtowc.c
1429 index e7b3654..ca7da70 100644
1430 --- a/src/multibyte/mbrtowc.c
1431 +++ b/src/multibyte/mbrtowc.c
1432 @@ -4,6 +4,7 @@
1433 * unnecessary.
1434 */
1435
1436 +#include <stdlib.h>
1437 #include <wchar.h>
1438 #include <errno.h>
1439 #include "internal.h"
1440 @@ -27,6 +28,7 @@ size_t mbrtowc(wchar_t *restrict wc, const char *restrict src, size_t n, mbstate
1441 if (!n) return -2;
1442 if (!c) {
1443 if (*s < 0x80) return !!(*wc = *s);
1444 + if (MB_CUR_MAX==1) return (*wc = CODEUNIT(*s)), 1;
1445 if (*s-SA > SB-SA) goto ilseq;
1446 c = bittab[*s++-SA]; n--;
1447 }
1448 diff --git a/src/multibyte/mbsrtowcs.c b/src/multibyte/mbsrtowcs.c
1449 index 3c1343a..e23083d 100644
1450 --- a/src/multibyte/mbsrtowcs.c
1451 +++ b/src/multibyte/mbsrtowcs.c
1452 @@ -7,6 +7,8 @@
1453 #include <stdint.h>
1454 #include <wchar.h>
1455 #include <errno.h>
1456 +#include <string.h>
1457 +#include <stdlib.h>
1458 #include "internal.h"
1459
1460 size_t mbsrtowcs(wchar_t *restrict ws, const char **restrict src, size_t wn, mbstate_t *restrict st)
1461 @@ -24,6 +26,23 @@ size_t mbsrtowcs(wchar_t *restrict ws, const char **restrict src, size_t wn, mbs
1462 }
1463 }
1464
1465 + if (MB_CUR_MAX==1) {
1466 + if (!ws) return strlen((const char *)s);
1467 + for (;;) {
1468 + if (!wn) {
1469 + *src = (const void *)s;
1470 + return wn0;
1471 + }
1472 + if (!*s) break;
1473 + c = *s++;
1474 + *ws++ = CODEUNIT(c);
1475 + wn--;
1476 + }
1477 + *ws = 0;
1478 + *src = 0;
1479 + return wn0-wn;
1480 + }
1481 +
1482 if (!ws) for (;;) {
1483 if (*s-1u < 0x7f && (uintptr_t)s%4 == 0) {
1484 while (!(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
1485 diff --git a/src/multibyte/mbtowc.c b/src/multibyte/mbtowc.c
1486 index 803d221..71a9506 100644
1487 --- a/src/multibyte/mbtowc.c
1488 +++ b/src/multibyte/mbtowc.c
1489 @@ -4,6 +4,7 @@
1490 * unnecessary.
1491 */
1492
1493 +#include <stdlib.h>
1494 #include <wchar.h>
1495 #include <errno.h>
1496 #include "internal.h"
1497 @@ -19,6 +20,7 @@ int mbtowc(wchar_t *restrict wc, const char *restrict src, size_t n)
1498 if (!wc) wc = &dummy;
1499
1500 if (*s < 0x80) return !!(*wc = *s);
1501 + if (MB_CUR_MAX==1) return (*wc = CODEUNIT(*s)), 1;
1502 if (*s-SA > SB-SA) goto ilseq;
1503 c = bittab[*s++-SA];
1504
1505 diff --git a/src/multibyte/wcrtomb.c b/src/multibyte/wcrtomb.c
1506 index 59f733d..ddc37a5 100644
1507 --- a/src/multibyte/wcrtomb.c
1508 +++ b/src/multibyte/wcrtomb.c
1509 @@ -4,8 +4,10 @@
1510 * unnecessary.
1511 */
1512
1513 +#include <stdlib.h>
1514 #include <wchar.h>
1515 #include <errno.h>
1516 +#include "internal.h"
1517
1518 size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict st)
1519 {
1520 @@ -13,6 +15,13 @@ size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict st)
1521 if ((unsigned)wc < 0x80) {
1522 *s = wc;
1523 return 1;
1524 + } else if (MB_CUR_MAX == 1) {
1525 + if (!IS_CODEUNIT(wc)) {
1526 + errno = EILSEQ;
1527 + return -1;
1528 + }
1529 + *s = wc;
1530 + return 1;
1531 } else if ((unsigned)wc < 0x800) {
1532 *s++ = 0xc0 | (wc>>6);
1533 *s = 0x80 | (wc&0x3f);
1534 diff --git a/src/multibyte/wctob.c b/src/multibyte/wctob.c
1535 index d6353ee..4aeda6a 100644
1536 --- a/src/multibyte/wctob.c
1537 +++ b/src/multibyte/wctob.c
1538 @@ -1,8 +1,10 @@
1539 -#include <stdio.h>
1540 #include <wchar.h>
1541 +#include <stdlib.h>
1542 +#include "internal.h"
1543
1544 int wctob(wint_t c)
1545 {
1546 if (c < 128U) return c;
1547 + if (MB_CUR_MAX==1 && IS_CODEUNIT(c)) return (unsigned char)c;
1548 return EOF;
1549 }
1550 diff --git a/src/passwd/nscd_query.c b/src/passwd/nscd_query.c
1551 index 69a7815..d38e371 100644
1552 --- a/src/passwd/nscd_query.c
1553 +++ b/src/passwd/nscd_query.c
1554 @@ -32,6 +32,7 @@ FILE *__nscd_query(int32_t req, const char *key, int32_t *buf, size_t len, int *
1555 },
1556 .msg_iovlen = 2
1557 };
1558 + int errno_save = errno;
1559
1560 *swap = 0;
1561 retry:
1562 @@ -50,11 +51,14 @@ retry:
1563 return f;
1564
1565 if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
1566 - /* If there isn't a running nscd we return -1 to indicate that
1567 - * that is precisely what happened
1568 - */
1569 - if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT)
1570 + /* If there isn't a running nscd we simulate a "not found"
1571 + * result and the caller is responsible for calling
1572 + * fclose on the (unconnected) socket. The value of
1573 + * errno must be left unchanged in this case. */
1574 + if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) {
1575 + errno = errno_save;
1576 return f;
1577 + }
1578 goto error;
1579 }
1580
1581 diff --git a/src/process/sh/vfork.s b/src/process/sh/vfork.s
1582 new file mode 100644
1583 index 0000000..48cc939
1584 --- /dev/null
1585 +++ b/src/process/sh/vfork.s
1586 @@ -0,0 +1,23 @@
1587 +.global __vfork
1588 +.weak vfork
1589 +.type __vfork,@function
1590 +.type vfork,@function
1591 +__vfork:
1592 +vfork:
1593 + mov #95, r3
1594 + add r3, r3
1595 +
1596 + trapa #31
1597 + or r0, r0
1598 + or r0, r0
1599 + or r0, r0
1600 + or r0, r0
1601 + or r0, r0
1602 +
1603 + mov r0, r4
1604 + mov.l 1f, r0
1605 +2: braf r0
1606 + nop
1607 + .align 2
1608 + .hidden __syscall_ret
1609 +1: .long __syscall_ret@PLT-(2b+4-.)
1610 diff --git a/src/regex/fnmatch.c b/src/regex/fnmatch.c
1611 index 7f6b65f..978fff8 100644
1612 --- a/src/regex/fnmatch.c
1613 +++ b/src/regex/fnmatch.c
1614 @@ -18,6 +18,7 @@
1615 #include <stdlib.h>
1616 #include <wchar.h>
1617 #include <wctype.h>
1618 +#include "locale_impl.h"
1619
1620 #define END 0
1621 #define UNMATCHABLE -2
1622 @@ -229,7 +230,7 @@ static int fnmatch_internal(const char *pat, size_t m, const char *str, size_t n
1623 * On illegal sequences we may get it wrong, but in that case
1624 * we necessarily have a matching failure anyway. */
1625 for (s=endstr; s>str && tailcnt; tailcnt--) {
1626 - if (s[-1] < 128U) s--;
1627 + if (s[-1] < 128U || MB_CUR_MAX==1) s--;
1628 else while ((unsigned char)*--s-0x80U<0x40 && s>str);
1629 }
1630 if (tailcnt) return FNM_NOMATCH;
1631 diff --git a/src/signal/sh/restore.s b/src/signal/sh/restore.s
1632 index ab26034..eaedcdf 100644
1633 --- a/src/signal/sh/restore.s
1634 +++ b/src/signal/sh/restore.s
1635 @@ -2,7 +2,7 @@
1636 .type __restore, @function
1637 __restore:
1638 mov #119, r3 !__NR_sigreturn
1639 - trapa #16
1640 + trapa #31
1641
1642 or r0, r0
1643 or r0, r0
1644 @@ -15,7 +15,7 @@ __restore:
1645 __restore_rt:
1646 mov #100, r3 !__NR_rt_sigreturn
1647 add #73, r3
1648 - trapa #16
1649 + trapa #31
1650
1651 or r0, r0
1652 or r0, r0
1653 diff --git a/src/stdio/__fdopen.c b/src/stdio/__fdopen.c
1654 index ef8f47d..8d6ce81 100644
1655 --- a/src/stdio/__fdopen.c
1656 +++ b/src/stdio/__fdopen.c
1657 @@ -54,13 +54,7 @@ FILE *__fdopen(int fd, const char *mode)
1658 if (!libc.threaded) f->lock = -1;
1659
1660 /* Add new FILE to open file list */
1661 - OFLLOCK();
1662 - f->next = libc.ofl_head;
1663 - if (libc.ofl_head) libc.ofl_head->prev = f;
1664 - libc.ofl_head = f;
1665 - OFLUNLOCK();
1666 -
1667 - return f;
1668 + return __ofl_add(f);
1669 }
1670
1671 weak_alias(__fdopen, fdopen);
1672 diff --git a/src/stdio/__stdio_exit.c b/src/stdio/__stdio_exit.c
1673 index 716e5f7..191b445 100644
1674 --- a/src/stdio/__stdio_exit.c
1675 +++ b/src/stdio/__stdio_exit.c
1676 @@ -16,8 +16,7 @@ static void close_file(FILE *f)
1677 void __stdio_exit(void)
1678 {
1679 FILE *f;
1680 - OFLLOCK();
1681 - for (f=libc.ofl_head; f; f=f->next) close_file(f);
1682 + for (f=*__ofl_lock(); f; f=f->next) close_file(f);
1683 close_file(__stdin_used);
1684 close_file(__stdout_used);
1685 }
1686 diff --git a/src/stdio/__stdio_read.c b/src/stdio/__stdio_read.c
1687 index 5947344..f8fa6d3 100644
1688 --- a/src/stdio/__stdio_read.c
1689 +++ b/src/stdio/__stdio_read.c
1690 @@ -1,12 +1,5 @@
1691 #include "stdio_impl.h"
1692 #include <sys/uio.h>
1693 -#include <pthread.h>
1694 -
1695 -static void cleanup(void *p)
1696 -{
1697 - FILE *f = p;
1698 - if (!f->lockcount) __unlockfile(f);
1699 -}
1700
1701 size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
1702 {
1703 @@ -16,9 +9,7 @@ size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
1704 };
1705 ssize_t cnt;
1706
1707 - pthread_cleanup_push(cleanup, f);
1708 - cnt = syscall_cp(SYS_readv, f->fd, iov, 2);
1709 - pthread_cleanup_pop(0);
1710 + cnt = syscall(SYS_readv, f->fd, iov, 2);
1711 if (cnt <= 0) {
1712 f->flags |= F_EOF ^ ((F_ERR^F_EOF) & cnt);
1713 return cnt;
1714 diff --git a/src/stdio/__stdio_write.c b/src/stdio/__stdio_write.c
1715 index 8c89389..d2d8947 100644
1716 --- a/src/stdio/__stdio_write.c
1717 +++ b/src/stdio/__stdio_write.c
1718 @@ -1,12 +1,5 @@
1719 #include "stdio_impl.h"
1720 #include <sys/uio.h>
1721 -#include <pthread.h>
1722 -
1723 -static void cleanup(void *p)
1724 -{
1725 - FILE *f = p;
1726 - if (!f->lockcount) __unlockfile(f);
1727 -}
1728
1729 size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
1730 {
1731 @@ -19,9 +12,7 @@ size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
1732 int iovcnt = 2;
1733 ssize_t cnt;
1734 for (;;) {
1735 - pthread_cleanup_push(cleanup, f);
1736 - cnt = syscall_cp(SYS_writev, f->fd, iov, iovcnt);
1737 - pthread_cleanup_pop(0);
1738 + cnt = syscall(SYS_writev, f->fd, iov, iovcnt);
1739 if (cnt == rem) {
1740 f->wend = f->buf + f->buf_size;
1741 f->wpos = f->wbase = f->buf;
1742 @@ -34,11 +25,8 @@ size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
1743 }
1744 rem -= cnt;
1745 if (cnt > iov[0].iov_len) {
1746 - f->wpos = f->wbase = f->buf;
1747 cnt -= iov[0].iov_len;
1748 iov++; iovcnt--;
1749 - } else if (iovcnt == 2) {
1750 - f->wbase += cnt;
1751 }
1752 iov[0].iov_base = (char *)iov[0].iov_base + cnt;
1753 iov[0].iov_len -= cnt;
1754 diff --git a/src/stdio/fclose.c b/src/stdio/fclose.c
1755 index 317b3c9..839d88a 100644
1756 --- a/src/stdio/fclose.c
1757 +++ b/src/stdio/fclose.c
1758 @@ -14,11 +14,11 @@ int fclose(FILE *f)
1759 __unlist_locked_file(f);
1760
1761 if (!(perm = f->flags & F_PERM)) {
1762 - OFLLOCK();
1763 + FILE **head = __ofl_lock();
1764 if (f->prev) f->prev->next = f->next;
1765 if (f->next) f->next->prev = f->prev;
1766 - if (libc.ofl_head == f) libc.ofl_head = f->next;
1767 - OFLUNLOCK();
1768 + if (*head == f) *head = f->next;
1769 + __ofl_unlock();
1770 }
1771
1772 r = fflush(f);
1773 diff --git a/src/stdio/fflush.c b/src/stdio/fflush.c
1774 index 7bf862a..3f462c8 100644
1775 --- a/src/stdio/fflush.c
1776 +++ b/src/stdio/fflush.c
1777 @@ -35,13 +35,12 @@ int fflush(FILE *f)
1778
1779 r = __stdout_used ? fflush(__stdout_used) : 0;
1780
1781 - OFLLOCK();
1782 - for (f=libc.ofl_head; f; f=f->next) {
1783 + for (f=*__ofl_lock(); f; f=f->next) {
1784 FLOCK(f);
1785 if (f->wpos > f->wbase) r |= __fflush_unlocked(f);
1786 FUNLOCK(f);
1787 }
1788 - OFLUNLOCK();
1789 + __ofl_unlock();
1790
1791 return r;
1792 }
1793 diff --git a/src/stdio/fgetwc.c b/src/stdio/fgetwc.c
1794 index 8626d54..e455cfe 100644
1795 --- a/src/stdio/fgetwc.c
1796 +++ b/src/stdio/fgetwc.c
1797 @@ -1,8 +1,9 @@
1798 #include "stdio_impl.h"
1799 +#include "locale_impl.h"
1800 #include <wchar.h>
1801 #include <errno.h>
1802
1803 -wint_t __fgetwc_unlocked(FILE *f)
1804 +static wint_t __fgetwc_unlocked_internal(FILE *f)
1805 {
1806 mbstate_t st = { 0 };
1807 wchar_t wc;
1808 @@ -10,8 +11,6 @@ wint_t __fgetwc_unlocked(FILE *f)
1809 unsigned char b;
1810 size_t l;
1811
1812 - f->mode |= f->mode+1;
1813 -
1814 /* Convert character from buffer if possible */
1815 if (f->rpos < f->rend) {
1816 l = mbrtowc(&wc, (void *)f->rpos, f->rend - f->rpos, &st);
1817 @@ -39,6 +38,16 @@ wint_t __fgetwc_unlocked(FILE *f)
1818 return wc;
1819 }
1820
1821 +wint_t __fgetwc_unlocked(FILE *f)
1822 +{
1823 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
1824 + if (f->mode <= 0) fwide(f, 1);
1825 + *ploc = f->locale;
1826 + wchar_t wc = __fgetwc_unlocked_internal(f);
1827 + *ploc = loc;
1828 + return wc;
1829 +}
1830 +
1831 wint_t fgetwc(FILE *f)
1832 {
1833 wint_t c;
1834 diff --git a/src/stdio/fmemopen.c b/src/stdio/fmemopen.c
1835 index d784960..7c193a5 100644
1836 --- a/src/stdio/fmemopen.c
1837 +++ b/src/stdio/fmemopen.c
1838 @@ -110,11 +110,5 @@ FILE *fmemopen(void *restrict buf, size_t size, const char *restrict mode)
1839
1840 if (!libc.threaded) f->lock = -1;
1841
1842 - OFLLOCK();
1843 - f->next = libc.ofl_head;
1844 - if (libc.ofl_head) libc.ofl_head->prev = f;
1845 - libc.ofl_head = f;
1846 - OFLUNLOCK();
1847 -
1848 - return f;
1849 + return __ofl_add(f);
1850 }
1851 diff --git a/src/stdio/fopen.c b/src/stdio/fopen.c
1852 index 07bdb6e..252f082 100644
1853 --- a/src/stdio/fopen.c
1854 +++ b/src/stdio/fopen.c
1855 @@ -18,7 +18,7 @@ FILE *fopen(const char *restrict filename, const char *restrict mode)
1856 /* Compute the flags to pass to open() */
1857 flags = __fmodeflags(mode);
1858
1859 - fd = sys_open_cp(filename, flags, 0666);
1860 + fd = sys_open(filename, flags, 0666);
1861 if (fd < 0) return 0;
1862 if (flags & O_CLOEXEC)
1863 __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
1864 diff --git a/src/stdio/fputwc.c b/src/stdio/fputwc.c
1865 index 7b621dd..789fe9c 100644
1866 --- a/src/stdio/fputwc.c
1867 +++ b/src/stdio/fputwc.c
1868 @@ -1,4 +1,5 @@
1869 #include "stdio_impl.h"
1870 +#include "locale_impl.h"
1871 #include <wchar.h>
1872 #include <limits.h>
1873 #include <ctype.h>
1874 @@ -7,8 +8,10 @@ wint_t __fputwc_unlocked(wchar_t c, FILE *f)
1875 {
1876 char mbc[MB_LEN_MAX];
1877 int l;
1878 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
1879
1880 - f->mode |= f->mode+1;
1881 + if (f->mode <= 0) fwide(f, 1);
1882 + *ploc = f->locale;
1883
1884 if (isascii(c)) {
1885 c = putc_unlocked(c, f);
1886 @@ -20,6 +23,8 @@ wint_t __fputwc_unlocked(wchar_t c, FILE *f)
1887 l = wctomb(mbc, c);
1888 if (l < 0 || __fwritex((void *)mbc, l, f) < l) c = WEOF;
1889 }
1890 + if (c==WEOF) f->flags |= F_ERR;
1891 + *ploc = loc;
1892 return c;
1893 }
1894
1895 diff --git a/src/stdio/fputws.c b/src/stdio/fputws.c
1896 index 5723cbc..0ed02f1 100644
1897 --- a/src/stdio/fputws.c
1898 +++ b/src/stdio/fputws.c
1899 @@ -1,23 +1,28 @@
1900 #include "stdio_impl.h"
1901 +#include "locale_impl.h"
1902 #include <wchar.h>
1903
1904 int fputws(const wchar_t *restrict ws, FILE *restrict f)
1905 {
1906 unsigned char buf[BUFSIZ];
1907 size_t l=0;
1908 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
1909
1910 FLOCK(f);
1911
1912 - f->mode |= f->mode+1;
1913 + fwide(f, 1);
1914 + *ploc = f->locale;
1915
1916 while (ws && (l = wcsrtombs((void *)buf, (void*)&ws, sizeof buf, 0))+1 > 1)
1917 if (__fwritex(buf, l, f) < l) {
1918 FUNLOCK(f);
1919 + *ploc = loc;
1920 return -1;
1921 }
1922
1923 FUNLOCK(f);
1924
1925 + *ploc = loc;
1926 return l; /* 0 or -1 */
1927 }
1928
1929 diff --git a/src/stdio/fwide.c b/src/stdio/fwide.c
1930 index 8088e7a..8410b15 100644
1931 --- a/src/stdio/fwide.c
1932 +++ b/src/stdio/fwide.c
1933 @@ -1,13 +1,14 @@
1934 -#include <wchar.h>
1935 #include "stdio_impl.h"
1936 -
1937 -#define SH (8*sizeof(int)-1)
1938 -#define NORMALIZE(x) ((x)>>SH | -((-(x))>>SH))
1939 +#include "locale_impl.h"
1940
1941 int fwide(FILE *f, int mode)
1942 {
1943 FLOCK(f);
1944 - if (!f->mode) f->mode = NORMALIZE(mode);
1945 + if (mode) {
1946 + if (!f->locale) f->locale = MB_CUR_MAX==1
1947 + ? C_LOCALE : UTF8_LOCALE;
1948 + if (!f->mode) f->mode = mode>0 ? 1 : -1;
1949 + }
1950 mode = f->mode;
1951 FUNLOCK(f);
1952 return mode;
1953 diff --git a/src/stdio/ofl.c b/src/stdio/ofl.c
1954 new file mode 100644
1955 index 0000000..b143999
1956 --- /dev/null
1957 +++ b/src/stdio/ofl.c
1958 @@ -0,0 +1,16 @@
1959 +#include "stdio_impl.h"
1960 +#include "libc.h"
1961 +
1962 +static FILE *ofl_head;
1963 +static volatile int ofl_lock[2];
1964 +
1965 +FILE **__ofl_lock()
1966 +{
1967 + LOCK(ofl_lock);
1968 + return &ofl_head;
1969 +}
1970 +
1971 +void __ofl_unlock()
1972 +{
1973 + UNLOCK(ofl_lock);
1974 +}
1975 diff --git a/src/stdio/ofl_add.c b/src/stdio/ofl_add.c
1976 new file mode 100644
1977 index 0000000..d7de9f1
1978 --- /dev/null
1979 +++ b/src/stdio/ofl_add.c
1980 @@ -0,0 +1,11 @@
1981 +#include "stdio_impl.h"
1982 +
1983 +FILE *__ofl_add(FILE *f)
1984 +{
1985 + FILE **head = __ofl_lock();
1986 + f->next = *head;
1987 + if (*head) (*head)->prev = f;
1988 + *head = f;
1989 + __ofl_unlock();
1990 + return f;
1991 +}
1992 diff --git a/src/stdio/open_memstream.c b/src/stdio/open_memstream.c
1993 index 9eafdfb..58504c9 100644
1994 --- a/src/stdio/open_memstream.c
1995 +++ b/src/stdio/open_memstream.c
1996 @@ -79,11 +79,5 @@ FILE *open_memstream(char **bufp, size_t *sizep)
1997
1998 if (!libc.threaded) f->lock = -1;
1999
2000 - OFLLOCK();
2001 - f->next = libc.ofl_head;
2002 - if (libc.ofl_head) libc.ofl_head->prev = f;
2003 - libc.ofl_head = f;
2004 - OFLUNLOCK();
2005 -
2006 - return f;
2007 + return __ofl_add(f);
2008 }
2009 diff --git a/src/stdio/open_wmemstream.c b/src/stdio/open_wmemstream.c
2010 index 3537030..7ab2c64 100644
2011 --- a/src/stdio/open_wmemstream.c
2012 +++ b/src/stdio/open_wmemstream.c
2013 @@ -81,11 +81,5 @@ FILE *open_wmemstream(wchar_t **bufp, size_t *sizep)
2014
2015 if (!libc.threaded) f->lock = -1;
2016
2017 - OFLLOCK();
2018 - f->next = libc.ofl_head;
2019 - if (libc.ofl_head) libc.ofl_head->prev = f;
2020 - libc.ofl_head = f;
2021 - OFLUNLOCK();
2022 -
2023 - return f;
2024 + return __ofl_add(f);
2025 }
2026 diff --git a/src/stdio/ungetwc.c b/src/stdio/ungetwc.c
2027 index 913f716..80d6e20 100644
2028 --- a/src/stdio/ungetwc.c
2029 +++ b/src/stdio/ungetwc.c
2030 @@ -1,4 +1,5 @@
2031 #include "stdio_impl.h"
2032 +#include "locale_impl.h"
2033 #include <wchar.h>
2034 #include <limits.h>
2035 #include <ctype.h>
2036 @@ -8,21 +9,19 @@ wint_t ungetwc(wint_t c, FILE *f)
2037 {
2038 unsigned char mbc[MB_LEN_MAX];
2039 int l=1;
2040 -
2041 - if (c == WEOF) return c;
2042 -
2043 - /* Try conversion early so we can fail without locking if invalid */
2044 - if (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)
2045 - return WEOF;
2046 + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
2047
2048 FLOCK(f);
2049
2050 - f->mode |= f->mode+1;
2051 + if (f->mode <= 0) fwide(f, 1);
2052 + *ploc = f->locale;
2053
2054 if (!f->rpos) __toread(f);
2055 - if (!f->rpos || f->rpos < f->buf - UNGET + l) {
2056 + if (!f->rpos || f->rpos < f->buf - UNGET + l || c == WEOF ||
2057 + (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)) {
2058 FUNLOCK(f);
2059 - return EOF;
2060 + *ploc = loc;
2061 + return WEOF;
2062 }
2063
2064 if (isascii(c)) *--f->rpos = c;
2065 @@ -31,5 +30,6 @@ wint_t ungetwc(wint_t c, FILE *f)
2066 f->flags &= ~F_EOF;
2067
2068 FUNLOCK(f);
2069 + *ploc = loc;
2070 return c;
2071 }
2072 diff --git a/src/stdio/vfwprintf.c b/src/stdio/vfwprintf.c
2073 index ebdff00..f06d5ae 100644
2074 --- a/src/stdio/vfwprintf.c
2075 +++ b/src/stdio/vfwprintf.c
2076 @@ -293,7 +293,10 @@ static int wprintf_core(FILE *f, const wchar_t *fmt, va_list *ap, union arg *nl_
2077 if ((fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
2078 l=w;
2079 continue;
2080 + case 'm':
2081 + arg.p = strerror(errno);
2082 case 's':
2083 + if (!arg.p) arg.p = "(null)";
2084 bs = arg.p;
2085 if (p<0) p = INT_MAX;
2086 for (i=l=0; l<p && (i=mbtowc(&wc, bs, MB_LEN_MAX))>0; bs+=i, l++);
2087 @@ -356,7 +359,7 @@ int vfwprintf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
2088 }
2089
2090 FLOCK(f);
2091 - f->mode |= f->mode+1;
2092 + fwide(f, 1);
2093 olderr = f->flags & F_ERR;
2094 f->flags &= ~F_ERR;
2095 ret = wprintf_core(f, fmt, &ap2, nl_arg, nl_type);
2096 diff --git a/src/stdio/vfwscanf.c b/src/stdio/vfwscanf.c
2097 index ac5c2c2..223aad4 100644
2098 --- a/src/stdio/vfwscanf.c
2099 +++ b/src/stdio/vfwscanf.c
2100 @@ -104,7 +104,7 @@ int vfwscanf(FILE *restrict f, const wchar_t *restrict fmt, va_list ap)
2101
2102 FLOCK(f);
2103
2104 - f->mode |= f->mode+1;
2105 + fwide(f, 1);
2106
2107 for (p=fmt; *p; p++) {
2108
2109 diff --git a/src/thread/__unmapself.c b/src/thread/__unmapself.c
2110 index e69de29..1d3bee1 100644
2111 --- a/src/thread/__unmapself.c
2112 +++ b/src/thread/__unmapself.c
2113 @@ -0,0 +1,29 @@
2114 +#include "pthread_impl.h"
2115 +#include "atomic.h"
2116 +#include "syscall.h"
2117 +/* cheat and reuse CRTJMP macro from dynlink code */
2118 +#include "dynlink.h"
2119 +
2120 +static volatile int lock;
2121 +static void *unmap_base;
2122 +static size_t unmap_size;
2123 +static char shared_stack[256];
2124 +
2125 +static void do_unmap()
2126 +{
2127 + __syscall(SYS_munmap, unmap_base, unmap_size);
2128 + __syscall(SYS_exit);
2129 +}
2130 +
2131 +void __unmapself(void *base, size_t size)
2132 +{
2133 + int tid=__pthread_self()->tid;
2134 + char *stack = shared_stack + sizeof shared_stack;
2135 + stack -= (uintptr_t)stack % 16;
2136 + while (lock || a_cas(&lock, 0, tid))
2137 + a_spin();
2138 + __syscall(SYS_set_tid_address, &lock);
2139 + unmap_base = base;
2140 + unmap_size = size;
2141 + CRTJMP(do_unmap, stack);
2142 +}
2143 diff --git a/src/thread/mips/__unmapself.s b/src/thread/mips/__unmapself.s
2144 index 9aa0371..ba139dc 100644
2145 --- a/src/thread/mips/__unmapself.s
2146 +++ b/src/thread/mips/__unmapself.s
2147 @@ -2,6 +2,7 @@
2148 .global __unmapself
2149 .type __unmapself,@function
2150 __unmapself:
2151 + move $sp, $25
2152 li $2, 4091
2153 syscall
2154 li $4, 0
2155 diff --git a/src/thread/pthread_create.c b/src/thread/pthread_create.c
2156 index de72818..e7df34a 100644
2157 --- a/src/thread/pthread_create.c
2158 +++ b/src/thread/pthread_create.c
2159 @@ -191,8 +191,9 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
2160 if (!libc.can_do_threads) return ENOSYS;
2161 self = __pthread_self();
2162 if (!libc.threaded) {
2163 - for (FILE *f=libc.ofl_head; f; f=f->next)
2164 + for (FILE *f=*__ofl_lock(); f; f=f->next)
2165 init_file_lock(f);
2166 + __ofl_unlock();
2167 init_file_lock(__stdin_used);
2168 init_file_lock(__stdout_used);
2169 init_file_lock(__stderr_used);
2170 @@ -231,7 +232,8 @@ int __pthread_create(pthread_t *restrict res, const pthread_attr_t *restrict att
2171 if (guard) {
2172 map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
2173 if (map == MAP_FAILED) goto fail;
2174 - if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)) {
2175 + if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)
2176 + && errno != ENOSYS) {
2177 __munmap(map, size);
2178 goto fail;
2179 }
2180 diff --git a/src/thread/sh/__set_thread_area.s b/src/thread/sh/__set_thread_area.s
2181 index d9f1181..e69de29 100644
2182 --- a/src/thread/sh/__set_thread_area.s
2183 +++ b/src/thread/sh/__set_thread_area.s
2184 @@ -1,6 +0,0 @@
2185 -.global __set_thread_area
2186 -.type __set_thread_area, @function
2187 -__set_thread_area:
2188 - ldc r4, gbr
2189 - rts
2190 - mov #0, r0
2191 diff --git a/src/thread/sh/__unmapself.s b/src/thread/sh/__unmapself.s
2192 index b34c3c8..0161d53 100644
2193 --- a/src/thread/sh/__unmapself.s
2194 +++ b/src/thread/sh/__unmapself.s
2195 @@ -1,9 +1,9 @@
2196 .text
2197 -.global __unmapself
2198 -.type __unmapself, @function
2199 -__unmapself:
2200 +.global __unmapself_sh_mmu
2201 +.type __unmapself_sh_mmu, @function
2202 +__unmapself_sh_mmu:
2203 mov #91, r3 ! SYS_munmap
2204 - trapa #18
2205 + trapa #31
2206
2207 or r0, r0
2208 or r0, r0
2209 @@ -13,7 +13,7 @@ __unmapself:
2210
2211 mov #1, r3 ! SYS_exit
2212 mov #0, r4
2213 - trapa #17
2214 + trapa #31
2215
2216 or r0, r0
2217 or r0, r0
2218 diff --git a/src/thread/sh/clone.s b/src/thread/sh/clone.s
2219 index d6c9184..f8ad845 100644
2220 --- a/src/thread/sh/clone.s
2221 +++ b/src/thread/sh/clone.s
2222 @@ -17,7 +17,7 @@ __clone:
2223 mov.l @r15, r6 ! r6 = ptid
2224 mov.l @(8,r15), r7 ! r7 = ctid
2225 mov.l @(4,r15), r0 ! r0 = tls
2226 - trapa #21
2227 + trapa #31
2228
2229 or r0, r0
2230 or r0, r0
2231 @@ -38,7 +38,7 @@ __clone:
2232
2233 mov #1, r3 ! __NR_exit
2234 mov r0, r4
2235 - trapa #17
2236 + trapa #31
2237
2238 or r0, r0
2239 or r0, r0
2240 diff --git a/src/thread/sh/syscall_cp.s b/src/thread/sh/syscall_cp.s
2241 index 6b28ddf..c3cafac 100644
2242 --- a/src/thread/sh/syscall_cp.s
2243 +++ b/src/thread/sh/syscall_cp.s
2244 @@ -31,7 +31,7 @@ L1: .long __cancel@PLT-(1b-.)
2245 mov.l @(4,r15), r7
2246 mov.l @(8,r15), r0
2247 mov.l @(12,r15), r1
2248 - trapa #22
2249 + trapa #31
2250
2251 __cp_end:
2252 ! work around hardware bug
2253 diff --git a/src/unistd/sh/pipe.s b/src/unistd/sh/pipe.s
2254 index d865ae3..46c4908 100644
2255 --- a/src/unistd/sh/pipe.s
2256 +++ b/src/unistd/sh/pipe.s
2257 @@ -2,7 +2,7 @@
2258 .type pipe, @function
2259 pipe:
2260 mov #42, r3
2261 - trapa #17
2262 + trapa #31
2263
2264 ! work around hardware bug
2265 or r0, r0
2266 --
2267 2.1.4
2268