001-git-2015-07-22.patch 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146
  1. commit 3975577922aedab7d60788dd320a2c8e4e94bc6e
  2. Author: Roman Yeryomin <roman@ubnt.com>
  3. Date: Thu Jul 2 12:29:00 2015 +0300
  4. socket.h: cleanup/reorder mips and powerpc bits/socket.h
  5. ....to be somewhat consistent and easily comparable with asm/socket.h
  6. Signed-off-by: Roman Yeryomin <roman@ubnt.com>
  7. commit 29ec7677a73a5227badbb1064205be09e707e466
  8. Author: Roman Yeryomin <roman@ubnt.com>
  9. Date: Thu Jul 2 12:28:41 2015 +0300
  10. socket.h: fix SO_* for mips
  11. Signed-off-by: Roman Yeryomin <roman@ubnt.com>
  12. commit 3fffa7a658aa925b8f95d36aef7531c1827dbf28
  13. Author: Felix Fietkau <nbd@openwrt.org>
  14. Date: Tue Jul 21 15:01:25 2015 +0200
  15. mips: fix mcontext_t register array field name
  16. glibc and uclibc use gregs instead of regs
  17. Signed-off-by: Felix Fietkau <nbd@openwrt.org>
  18. commit 0f9c2666aca95eb98eb0ef4f4d8d1473c8ce3fa0
  19. Author: Rich Felker <dalias@aerifal.cx>
  20. Date: Thu Jul 9 18:36:02 2015 +0000
  21. handle loss of syslog socket connection
  22. when traditional syslogd implementations are restarted, the old server
  23. socket ceases to exist and a new unix socket with the same pathname is
  24. created. when this happens, the default destination address associated
  25. with the client socket via connect is no longer valid, and attempts to
  26. send produce errors. this happens despite the socket being datagram
  27. type, and is in contrast to the behavior that would be seen with an IP
  28. datagram (UDP) socket.
  29. in order to avoid a situation where the application is unable to send
  30. further syslog messages without calling closelog, this patch makes
  31. syslog attempt to reconnect the socket when send returns an error
  32. indicating a lost connection.
  33. additionally, initial failure to connect the socket no longer results
  34. in the socket being closed. this ensures that an application which
  35. calls openlog to reserve the socket file descriptor will not run into
  36. a situation where transient connection failure (e.g. due to syslogd
  37. restart) prevents fd reservation. however, applications which may be
  38. unable to connect the socket later (e.g. due to chroot, restricted
  39. permissions, seccomp, etc.) will still fail to log if the syslog
  40. socket cannot be connected at openlog time or if it has to be
  41. reconnected later.
  42. commit 11894f6d3a80be950a490dc7dfab349f057a545f
  43. Author: Rich Felker <dalias@aerifal.cx>
  44. Date: Thu Jul 9 17:07:35 2015 +0000
  45. fix incorrect void return type for syncfs function
  46. being nonstandard, the closest thing to a specification for this
  47. function is its man page, which documents it as returning int. it can
  48. fail with EBADF if the file descriptor passed is invalid.
  49. commit e8cbe0bad4284906230a53af4c91ad2b9713d03b
  50. Author: Rich Felker <dalias@aerifal.cx>
  51. Date: Wed Jul 8 02:46:45 2015 +0000
  52. fix negated return value of ns_skiprr, breakage in related functions
  53. due to a reversed pointer difference computation, ns_skiprr always
  54. returned a negative value, which functions using it would interpret as
  55. an error.
  56. patch by Yu Lu.
  57. commit fb58545f8d1c5fa32122244caeaf3625c12ddc01
  58. Author: Shiz <hi@shiz.me>
  59. Date: Sun Jun 28 23:08:21 2015 +0200
  60. add musl-clang, a wrapper for system clang installs
  61. musl-clang allows the user to compile musl-powered programs using their
  62. already existent clang install, without the need of a special cross compiler.
  63. it achieves this by wrapping around both the system clang install and the
  64. linker and passing them special flags to re-target musl at runtime.
  65. it does only affect invocations done through the special musl-clang wrapper
  66. script, so that the user setup remains fully intact otherwise.
  67. the clang wrapper consists of the compiler frontend wrapper script,
  68. musl-clang, and the linker wrapper script, ld.musl-clang.
  69. musl-clang makes sure clang invokes ld.musl-clang to link objects; neither
  70. script needs to be in PATH for the wrapper to work.
  71. commit f8db6f74b2c74a50c4dec7e30be5215f0e2c37a6
  72. Author: Shiz <hi@shiz.me>
  73. Date: Sun Jun 28 23:08:20 2015 +0200
  74. build: fix musl-targeting toolchain test
  75. the old test was broken in that it would never fail on a toolchains built
  76. without dynamic linking support, leading to the wrapper script possibly being
  77. installed on compilers that do not support it. in addition, the new test is
  78. portable across compilers: the old test only worked on GCC.
  79. the new test works by testing whether the toolchain libc defines __GLIBC__:
  80. most non-musl Linux libc's do define this for compatibility even when they
  81. are not glibc, so this is a safe bet to check for musl. in addition, the
  82. compiler runtime would need to have a somewhat glibc-compatible ABI in the
  83. first place, so any non-glibc compatible libc's compiler runtime might not
  84. work. it is safer to disable these cases by default and have the user enable
  85. the wrappers manually there using --enable-wrapper if they certain it works.
  86. commit b3cd7d13fe630ba1847326242525298e361018c1
  87. Author: Shiz <hi@shiz.me>
  88. Date: Sun Jun 28 23:08:19 2015 +0200
  89. build: overhaul wrapper script system for multiple wrapper support
  90. this overhauls part of the build system in order to support multiple
  91. toolchain wrapper scripts, as opposed to solely the musl-gcc wrapper as
  92. before. it thereby replaces --enable-gcc-wrapper with --enable-wrapper=...,
  93. which has the options 'auto' (the default, detect whether to use wrappers),
  94. 'all' (build and install all wrappers), 'no' (don't build any) and finally
  95. the options named after the individual compiler scripts (currently only
  96. 'gcc' is available) to build and install only that wrapper.
  97. the old --enable-gcc-wrapper is removed from --help, but still available.
  98. it also modifies the wrappers to use the C compiler specified to the build
  99. system as 'inner' compiler, when applicable. as wrapper detection works by
  100. probing this compiler, it may not work with any other.
  101. commit 2a780aa3050b86d888489361f04220bfb58890a1
  102. Author: Rich Felker <dalias@aerifal.cx>
  103. Date: Mon Jul 6 22:13:11 2015 +0000
  104. treat empty TZ environment variable as GMT rather than default
  105. this improves compatibility with the behavior of other systems and
  106. with some applications which set an empty TZ var to disable use of
  107. local time by mktime, etc.
  108. commit 8f08a58c635bea5cdfae6bc0e323c80aa9ff82a7
  109. Author: Alexander Monakov <amonakov@ispras.ru>
  110. Date: Sun Jun 28 02:48:33 2015 +0300
  111. dynlink.c: pass gnu-hash table pointer to gnu_lookup
  112. The callers need to check the value of the pointer anyway, so make
  113. them pass the pointer to gnu_lookup instead of reloading it there.
  114. Reorder gnu_lookup arguments so that always-used ones are listed
  115. first. GCC can choose a calling convention with arguments in registers
  116. (e.g. up to 3 arguments in eax, ecx, edx on x86), but cannot reorder
  117. the arguments for static functions.
  118. commit 5b4286e12cd6baac343b10a41dc17ac578832089
  119. Author: Alexander Monakov <amonakov@ispras.ru>
  120. Date: Sun Jun 28 02:48:32 2015 +0300
  121. dynlink.c: slim down gnu_lookup
  122. Do not reference dso->syms and dso->strings until point of use.
  123. Check 'h1 == (h2|1)', the simplest condition, before the others.
  124. commit 84389c64562e2b2ba43225b5b7a9df7d974479b1
  125. Author: Alexander Monakov <amonakov@ispras.ru>
  126. Date: Sun Jun 28 02:48:31 2015 +0300
  127. dynlink.c: use bloom filter in gnu hash lookup
  128. Introduce gnu_lookup_filtered and use it to speed up symbol lookups in
  129. find_sym (do_dlsym is left as is, based on an expectation that
  130. frequently dlsym queries will use a dlopen handle rather than
  131. RTLD_NEXT or RTLD_DEFAULT, and will not need to look at more than one
  132. DSO).
  133. commit 66d45787c865a1807ae397a89a14699394ed4fa4
  134. Author: Alexander Monakov <amonakov@ispras.ru>
  135. Date: Sun Jun 28 02:48:30 2015 +0300
  136. dynlink.c: use a faster expression in gnu_hash
  137. With -Os, GCC uses a multiply rather than a shift and addition for 'h*33'.
  138. Use a more efficient expression explicitely.
  139. commit 6ba5517a460c6c438f64d69464fdfc3269a4c91a
  140. Author: Rich Felker <dalias@aerifal.cx>
  141. Date: Thu Jun 25 22:22:00 2015 +0000
  142. fix local-dynamic model TLS on mips and powerpc
  143. the TLS ABI spec for mips, powerpc, and some other (presently
  144. unsupported) RISC archs has the return value of __tls_get_addr offset
  145. by +0x8000 and the result of DTPOFF relocations offset by -0x8000. I
  146. had previously assumed this part of the ABI was actually just an
  147. implementation detail, since the adjustments cancel out. however, when
  148. the local dynamic model is used for accessing TLS that's known to be
  149. in the same DSO, either of the following may happen:
  150. 1. the -0x8000 offset may already be applied to the argument structure
  151. passed to __tls_get_addr at ld time, without any opportunity for
  152. runtime relocations.
  153. 2. __tls_get_addr may be used with a zero offset argument to obtain a
  154. base address for the module's TLS, to which the caller then applies
  155. immediate offsets for individual objects accessed using the local
  156. dynamic model. since the immediate offsets have the -0x8000 adjustment
  157. applied to them, the base address they use needs to include the
  158. +0x8000 offset.
  159. it would be possible, but more complex, to store the pointers in the
  160. dtv[] array with the +0x8000 offset pre-applied, to avoid the runtime
  161. cost of adding 0x8000 on each call to __tls_get_addr. this change
  162. could be made later if measurements show that it would help.
  163. commit ce337daa00e42d4f2d9a4d9ae0ed51b20249d924
  164. Author: Rich Felker <dalias@aerifal.cx>
  165. Date: Tue Jun 23 04:03:42 2015 +0000
  166. make dynamic linker work around MAP_FAILED mmap failure on nommu kernels
  167. previously, loading of additional libraries beyond libc/ldso did not
  168. work on nommu kernels, nor did loading programs via invocation of the
  169. dynamic linker as a command.
  170. commit a59341420fdedb288d9ff80e73609ae44e9cf258
  171. Author: Rich Felker <dalias@aerifal.cx>
  172. Date: Tue Jun 23 00:12:25 2015 +0000
  173. reimplement strverscmp to fix corner cases
  174. this interface is non-standardized and is a GNU invention, and as
  175. such, our implementation should match the behavior of the GNU
  176. function. one peculiarity the old implementation got wrong was the
  177. handling of all-zero digit sequences: they are supposed to compare
  178. greater than digit sequences of which they are a proper prefix, as in
  179. 009 < 00.
  180. in addition, high bytes were treated with char signedness rather than
  181. as unsigned. this was wrong regardless of what the GNU function does
  182. since the resulting order relation varied by arch.
  183. the new strverscmp implementation makes explicit the cases where the
  184. order differs from what strcmp would produce, of which there are only
  185. two.
  186. commit 153e952e1a688859d7095345b17e6c1df74a295c
  187. Author: Rich Felker <dalias@aerifal.cx>
  188. Date: Mon Jun 22 20:33:28 2015 +0000
  189. fix regression/typo that disabled __simple_malloc when calloc is used
  190. commit ba819787ee93ceae94efd274f7849e317c1bff58 introduced this
  191. regression. since the __malloc0 weak alias was not properly provided
  192. by __simple_malloc, use of calloc forced the full malloc to be linked.
  193. commit ba819787ee93ceae94efd274f7849e317c1bff58
  194. Author: Rich Felker <dalias@aerifal.cx>
  195. Date: Mon Jun 22 18:50:09 2015 +0000
  196. fix calloc when __simple_malloc implementation is used
  197. previously, calloc's implementation encoded assumptions about the
  198. implementation of malloc, accessing a size_t word just prior to the
  199. allocated memory to determine if it was obtained by mmap to optimize
  200. out the zero-filling. when __simple_malloc is used (static linking a
  201. program with no realloc/free), it doesn't matter if the result of this
  202. check is wrong, since all allocations are zero-initialized anyway. but
  203. the access could be invalid if it crosses a page boundary or if the
  204. pointer is not sufficiently aligned, which can happen for very small
  205. allocations.
  206. this patch fixes the issue by moving the zero-fill logic into malloc.c
  207. with the full malloc, as a new function named __malloc0, which is
  208. provided by a weak alias to __simple_malloc (which always gives
  209. zero-filled memory) when the full malloc is not in use.
  210. commit 55d061f031085f24d138664c897791aebe9a2fab
  211. Author: Rich Felker <dalias@aerifal.cx>
  212. Date: Sat Jun 20 03:01:07 2015 +0000
  213. provide __stack_chk_fail_local in libc.a
  214. this symbol is needed only on archs where the PLT call ABI is klunky,
  215. and only for position-independent code compiled with stack protector.
  216. thus references usually only appear in shared libraries or PIE
  217. executables, but they can also appear when linking statically if some
  218. of the object files being linked were built as PIC/PIE.
  219. normally libssp_nonshared.a from the compiler toolchain should provide
  220. __stack_chk_fail_local, but reportedly it appears prior to -lc in the
  221. link order, thus failing to satisfy references from libc itself (which
  222. arise only if libc.a was built as PIC/PIE with stack protector
  223. enabled).
  224. commit ce3688eca920aa77549323f84e21f33522397115
  225. Author: Rich Felker <dalias@aerifal.cx>
  226. Date: Sat Jun 20 02:54:30 2015 +0000
  227. work around mips detached thread exit breakage due to kernel regression
  228. linux kernel commit 46e12c07b3b9603c60fc1d421ff18618241cb081 caused
  229. the mips syscall mechanism to fail with EFAULT when the userspace
  230. stack pointer is invalid, breaking __unmapself used for detached
  231. thread exit. the workaround is to set $sp to a known-valid, readable
  232. address, and the simplest one to obtain is the address of the current
  233. function, which is available (per o32 calling convention) in $25.
  234. commit 75eceb3ae824d54e865686c0c538551aeebf3372
  235. Author: Rich Felker <dalias@aerifal.cx>
  236. Date: Wed Jun 17 17:21:46 2015 +0000
  237. ignore ENOSYS error from mprotect in pthread_create and dynamic linker
  238. this error simply indicated a system without memory protection (NOMMU)
  239. and should not cause failure in the caller.
  240. commit 10d0268ccfab9152250eeeed3952ce3fed44131a
  241. Author: Rich Felker <dalias@aerifal.cx>
  242. Date: Tue Jun 16 15:25:02 2015 +0000
  243. switch to using trap number 31 for syscalls on sh
  244. nominally the low bits of the trap number on sh are the number of
  245. syscall arguments, but they have never been used by the kernel, and
  246. some code making syscalls does not even know the number of arguments
  247. and needs to pass an arbitrary high number anyway.
  248. sh3/sh4 traditionally used the trap range 16-31 for syscalls, but part
  249. of this range overlapped with hardware exceptions/interrupts on sh2
  250. hardware, so an incompatible range 32-47 was chosen for sh2.
  251. using trap number 31 everywhere, since it's in the existing sh3/sh4
  252. range and does not conflict with sh2 hardware, is a proposed
  253. unification of the kernel syscall convention that will allow binaries
  254. to be shared between sh2 and sh3/sh4. if this is not accepted into the
  255. kernel, we can refit the sh2 target with runtime selection mechanisms
  256. for the trap number, but doing so would be invasive and would entail
  257. non-trivial overhead.
  258. commit 3366a99b17847b58f2d8cc52cbb5d65deb824f8a
  259. Author: Rich Felker <dalias@aerifal.cx>
  260. Date: Tue Jun 16 14:55:06 2015 +0000
  261. switch sh port's __unmapself to generic version when running on sh2/nommu
  262. due to the way the interrupt and syscall trap mechanism works,
  263. userspace on sh2 must never set the stack pointer to an invalid value.
  264. thus, the approach used on most archs, where __unmapself executes with
  265. no stack for the interval between SYS_munmap and SYS_exit, is not
  266. viable on sh2.
  267. in order not to pessimize sh3/sh4, the sh asm version of __unmapself
  268. is not removed. instead it's renamed and redirected through code that
  269. calls either the generic (safe) __unmapself or the sh3/sh4 asm,
  270. depending on compile-time and run-time conditions.
  271. commit f9d84554bae0fa17c9a1d724549c4408022228a5
  272. Author: Rich Felker <dalias@aerifal.cx>
  273. Date: Tue Jun 16 14:28:30 2015 +0000
  274. add support for sh2 interrupt-masking-based atomics to sh port
  275. the sh2 target is being considered an ISA subset of sh3/sh4, in the
  276. sense that binaries built for sh2 are intended to be usable on later
  277. cpu models/kernels with mmu support. so rather than hard-coding
  278. sh2-specific atomics, the runtime atomic selection mechanisms that was
  279. already in place has been extended to add sh2 atomics.
  280. at this time, the sh2 atomics are not SMP-compatible; since the ISA
  281. lacks actual atomic operations, the new code instead masks interrupts
  282. for the duration of the atomic operation, producing an atomic result
  283. on single-core. this is only possible because the kernel/hardware does
  284. not impose protections against userspace doing so. additional changes
  285. will be needed to support future SMP systems.
  286. care has been taken to avoid producing significant additional code
  287. size in the case where it's known at compile-time that the target is
  288. not sh2 and does not need sh2-specific code.
  289. commit 1b0cdc8700d29ef018bf226d74b2b58b23bce91c
  290. Author: Rich Felker <dalias@aerifal.cx>
  291. Date: Tue Jun 16 07:11:19 2015 +0000
  292. refactor stdio open file list handling, move it out of global libc struct
  293. functions which open in-memory FILE stream variants all shared a tail
  294. with __fdopen, adding the FILE structure to stdio's open file list.
  295. replacing this common tail with a function call reduces code size and
  296. duplication of logic. the list is also partially encapsulated now.
  297. function signatures were chosen to facilitate tail call optimization
  298. and reduce the need for additional accessor functions.
  299. with these changes, static linked programs that do not use stdio no
  300. longer have an open file list at all.
  301. commit f22a9edaf8a6f2ca1d314d18b3785558279a5c03
  302. Author: Rich Felker <dalias@aerifal.cx>
  303. Date: Tue Jun 16 06:18:00 2015 +0000
  304. byte-based C locale, phase 3: make MB_CUR_MAX variable to activate code
  305. this patch activates the new byte-based C locale (high bytes treated
  306. as abstract code unit "characters" rather than decoded as multibyte
  307. characters) by making the value of MB_CUR_MAX depend on the active
  308. locale. for the C locale, the LC_CTYPE category pointer is null,
  309. yielding a value of 1. all other locales yield a value of 4.
  310. commit 16f18d036d9a7bf590ee6eb86785c0a9658220b6
  311. Author: Rich Felker <dalias@aerifal.cx>
  312. Date: Tue Jun 16 05:35:31 2015 +0000
  313. byte-based C locale, phase 2: stdio and iconv (multibyte callers)
  314. this patch adjusts libc components which use the multibyte functions
  315. internally, and which depend on them operating in a particular
  316. encoding, to make the appropriate locale changes before calling them
  317. and restore the calling thread's locale afterwards. activating the
  318. byte-based C locale without these changes would cause regressions in
  319. stdio and iconv.
  320. in the case of iconv, the current implementation was simply using the
  321. multibyte functions as UTF-8 conversions. setting a multibyte UTF-8
  322. locale for the duration of the iconv operation allows the code to
  323. continue working.
  324. in the case of stdio, POSIX requires that FILE streams have an
  325. encoding rule bound at the time of setting wide orientation. as long
  326. as all locales, including the C locale, used the same encoding,
  327. treating high bytes as UTF-8, there was no need to store an encoding
  328. rule as part of the stream's state.
  329. a new locale field in the FILE structure points to the locale that
  330. should be made active during fgetwc/fputwc/ungetwc on the stream. it
  331. cannot point to the locale active at the time the stream becomes
  332. oriented, because this locale could be mutable (the global locale) or
  333. could be destroyed (locale_t objects produced by newlocale) before the
  334. stream is closed. instead, a pointer to the static C or C.UTF-8 locale
  335. object added in commit commit aeeac9ca5490d7d90fe061ab72da446c01ddf746
  336. is used. this is valid since categories other than LC_CTYPE will not
  337. affect these functions.
  338. commit 1507ebf837334e9e07cfab1ca1c2e88449069a80
  339. Author: Rich Felker <dalias@aerifal.cx>
  340. Date: Tue Jun 16 04:44:17 2015 +0000
  341. byte-based C locale, phase 1: multibyte character handling functions
  342. this patch makes the functions which work directly on multibyte
  343. characters treat the high bytes as individual abstract code units
  344. rather than as multibyte sequences when MB_CUR_MAX is 1. since
  345. MB_CUR_MAX is presently defined as a constant 4, all of the new code
  346. added is dead code, and optimizing compilers' code generation should
  347. not be affected at all. a future commit will activate the new code.
  348. as abstract code units, bytes 0x80 to 0xff are represented by wchar_t
  349. values 0xdf80 to 0xdfff, at the end of the surrogates range. this
  350. ensures that they will never be misinterpreted as Unicode characters,
  351. and that all wctype functions return false for these "characters"
  352. without needing locale-specific logic. a high range outside of Unicode
  353. such as 0x7fffff80 to 0x7fffffff was also considered, but since C11's
  354. char16_t also needs to be able to represent conversions of these
  355. bytes, the surrogate range was the natural choice.
  356. commit 38e2f727237230300fea6aff68802db04625fd23
  357. Author: Rich Felker <dalias@aerifal.cx>
  358. Date: Tue Jun 16 04:21:38 2015 +0000
  359. fix btowc corner case
  360. btowc is required to interpret its argument by conversion to unsigned
  361. char, unless the argument is equal to EOF. since the conversion to
  362. produces a non-character value anyway, we can just unconditionally
  363. convert, for now.
  364. commit ee59c296d56bf26f49f354d6eb32b4b6d4190188
  365. Author: Szabolcs Nagy <nsz@port70.net>
  366. Date: Wed Jun 3 10:32:14 2015 +0100
  367. arm: add vdso support
  368. vdso will be available on arm in linux v4.2, the user-space code
  369. for it is in kernel commit 8512287a8165592466cb9cb347ba94892e9c56a5
  370. commit e3bc22f1eff87b8f029a6ab31f1a269d69e4b053
  371. Author: Rich Felker <dalias@aerifal.cx>
  372. Date: Sun Jun 14 01:59:02 2015 +0000
  373. refactor malloc's expand_heap to share with __simple_malloc
  374. this extends the brk/stack collision protection added to full malloc
  375. in commit 276904c2f6bde3a31a24ebfa201482601d18b4f9 to also protect the
  376. __simple_malloc function used in static-linked programs that don't
  377. reference the free function.
  378. it also extends support for using mmap when brk fails, which full
  379. malloc got in commit 5446303328adf4b4e36d9fba21848e6feb55fab4, to
  380. __simple_malloc.
  381. since __simple_malloc may expand the heap by arbitrarily large
  382. increments, the stack collision detection is enhanced to detect
  383. interval overlap rather than just proximity of a single address to the
  384. stack. code size is increased a bit, but this is partly offset by the
  385. sharing of code between the two malloc implementations, which due to
  386. linking semantics, both get linked in a program that needs the full
  387. malloc with realloc/free support.
  388. commit 4ef9b828c1f39553a69e0635ac91f0fcadd6e8c6
  389. Author: Rich Felker <dalias@aerifal.cx>
  390. Date: Sat Jun 13 20:53:02 2015 +0000
  391. remove cancellation points in stdio
  392. commit 58165923890865a6ac042fafce13f440ee986fd9 added these optional
  393. cancellation points on the basis that cancellable stdio could be
  394. useful, to unblock threads stuck on stdio operations that will never
  395. complete. however, the only way to ensure that cancellation can
  396. achieve this is to violate the rules for side effects when
  397. cancellation is acted upon, discarding knowledge of any partial data
  398. transfer already completed. our implementation exhibited this behavior
  399. and was thus non-conforming.
  400. in addition to improving correctness, removing these cancellation
  401. points moderately reduces code size, and should significantly improve
  402. performance on i386, where sysenter/syscall instructions can be used
  403. instead of "int $128" for non-cancellable syscalls.
  404. commit 536c6d5a4205e2a3f161f2983ce1e0ac3082187d
  405. Author: Rich Felker <dalias@aerifal.cx>
  406. Date: Sat Jun 13 05:17:16 2015 +0000
  407. fix idiom for setting stdio stream orientation to wide
  408. the old idiom, f->mode |= f->mode+1, was adapted from the idiom for
  409. setting byte orientation, f->mode |= f->mode-1, but the adaptation was
  410. incorrect. unless the stream was alreasdy set byte-oriented, this code
  411. incremented f->mode each time it was executed, which would eventually
  412. lead to overflow. it could be fixed by changing it to f->mode |= 1,
  413. but upcoming changes will require slightly more work at the time of
  414. wide orientation, so it makes sense to just call fwide. as an
  415. optimization in the single-character functions, fwide is only called
  416. if the stream is not already wide-oriented.
  417. commit f8f565df467c13248104223f99abf7f37cef7584
  418. Author: Rich Felker <dalias@aerifal.cx>
  419. Date: Sat Jun 13 04:42:38 2015 +0000
  420. add printing of null %s arguments as "(null)" in wide printf
  421. this is undefined, but supported in our implementation of the normal
  422. printf, so for consistency the wide variant should support it too.
  423. commit f9e25d813860d53cd1e9b6145cc63375d2fe2529
  424. Author: Rich Felker <dalias@aerifal.cx>
  425. Date: Sat Jun 13 04:37:27 2015 +0000
  426. add %m support to wide printf
  427. commit ec634aad91f57479ef17525e33ed446c780a61f4
  428. Author: Rich Felker <dalias@aerifal.cx>
  429. Date: Thu Jun 11 05:01:04 2015 +0000
  430. add sh asm for vfork
  431. commit c30cbcb0a646b1f13a22c645616dce624465b883
  432. Author: Rich Felker <dalias@aerifal.cx>
  433. Date: Wed Jun 10 02:27:40 2015 +0000
  434. implement arch-generic version of __unmapself
  435. this can be used to put off writing an asm version of __unmapself for
  436. new archs, or as a permanent solution on archs where it's not
  437. practical or even possible to run momentarily with no stack.
  438. the concept here is simple: the caller takes a lock on a global shared
  439. stack and uses it to make the munmap and exit syscalls. the only trick
  440. is unlocking, which must be done after the thread exits, and this is
  441. achieved by using the set_tid_address syscall to have the kernel zero
  442. and futex-wake the lock word as part of the exit syscall.
  443. commit 276904c2f6bde3a31a24ebfa201482601d18b4f9
  444. Author: Rich Felker <dalias@aerifal.cx>
  445. Date: Tue Jun 9 20:30:35 2015 +0000
  446. in malloc, refuse to use brk if it grows into stack
  447. the linux/nommu fdpic ELF loader sets up the brk range to overlap
  448. entirely with the main thread's stack (but growing from opposite
  449. ends), so that the resulting failure mode for malloc is not to return
  450. a null pointer but to start returning pointers to memory that overlaps
  451. with the caller's stack. needless to say this extremely dangerous and
  452. makes brk unusable.
  453. since it's non-trivial to detect execution environments that might be
  454. affected by this kernel bug, and since the severity of the bug makes
  455. any sort of detection that might yield false-negatives unsafe, we
  456. instead check the proximity of the brk to the stack pointer each time
  457. the brk is to be expanded. both the main thread's stack (where the
  458. real known risk lies) and the calling thread's stack are checked. an
  459. arbitrary gap distance of 8 MB is imposed, chosen to be larger than
  460. linux default main-thread stack reservation sizes and larger than any
  461. reasonable stack configuration on nommu.
  462. the effeciveness of this patch relies on an assumption that the amount
  463. by which the brk is being grown is smaller than the gap limit, which
  464. is always true for malloc's use of brk. reliance on this assumption is
  465. why the check is being done in malloc-specific code and not in __brk.
  466. commit bd1eaceaa3975bd2a2a34e211cff896affaecadf
  467. Author: Rich Felker <dalias@aerifal.cx>
  468. Date: Tue Jun 9 20:09:27 2015 +0000
  469. fix spurious errors from pwd/grp functions when nscd backend is absent
  470. for several pwd/grp functions, the only way the caller can distinguish
  471. between a successful negative result ("no such user/group") and an
  472. internal error is by clearing errno before the call and checking errno
  473. afterwards. the nscd backend support code correctly simulated a
  474. not-found response on systems where such a backend is not running, but
  475. failed to restore errno.
  476. this commit also fixed an outdated/incorrect comment.
  477. commit 75ce4503950621b11fcc7f1fd1187dbcf3cde312
  478. Author: Rich Felker <dalias@aerifal.cx>
  479. Date: Sun Jun 7 20:55:23 2015 +0000
  480. fix regression in pre-v7 arm on kernels with kuser helper removed
  481. the arm atomics/TLS runtime selection code is called from
  482. __set_thread_area and depends on having libc.auxv and __hwcap
  483. available. commit 71f099cb7db821c51d8f39dfac622c61e54d794c moved the
  484. first call to __set_thread_area to the top of dynamic linking stage 3,
  485. before this data is made available, causing the runtime detection code
  486. to always see __hwcap as zero and thereby select the atomics/TLS
  487. implementations based on kuser helper.
  488. upcoming work on superh will use similar runtime detection.
  489. ideally this early-init code should be cleanly refactored and shared
  490. between the dynamic linker and static-linked startup.
  491. commit 32f3c4f70633488550c29a2444f819aafdf345ff
  492. Author: Rich Felker <dalias@aerifal.cx>
  493. Date: Sun Jun 7 03:09:16 2015 +0000
  494. add multiple inclusion guard to locale_impl.h
  495. commit 04b8360adbb6487f61aa0c00e53ec3a90a5a0d29
  496. Author: Rich Felker <dalias@aerifal.cx>
  497. Date: Sun Jun 7 02:59:49 2015 +0000
  498. remove redefinition of MB_CUR_MAX in locale_impl.h
  499. unless/until the byte-based C locale is implemented, defining
  500. MB_CUR_MAX to 1 in the C locale is wrong. no internal code currently
  501. uses the MB_CUR_MAX macro, but having it defined inconsistently is
  502. error-prone. applications get the value from stdlib.h and were
  503. unaffected.
  504. commit 16bf466532d7328e971012b0731ad493b017ad29
  505. Author: Rich Felker <dalias@aerifal.cx>
  506. Date: Sat Jun 6 18:53:02 2015 +0000
  507. make static C and C.UTF-8 locales available outside of newlocale
  508. commit 312eea2ea4f4363fb01b73660c08bfcf43dd3bb4
  509. Author: Rich Felker <dalias@aerifal.cx>
  510. Date: Sat Jun 6 18:20:30 2015 +0000
  511. remove another invalid skip of locking in ungetwc
  512. commit 3d7e32d28dc9962e9efc1c317c5b44b5b2df3008
  513. Author: Rich Felker <dalias@aerifal.cx>
  514. Date: Sat Jun 6 18:16:22 2015 +0000
  515. add macro version of ctype.h isascii function
  516. presumably internal code (ungetwc and fputwc) was written assuming a
  517. macro implementation existed; otherwise use of isascii is just a
  518. pessimization.
  519. commit 7e816a6487932cbb3cb71d94b609e50e81f4e5bf
  520. Author: Rich Felker <dalias@aerifal.cx>
  521. Date: Sat Jun 6 18:11:17 2015 +0000
  522. remove invalid skip of locking in ungetwc
  523. aside from being invalid, the early check only optimized the error
  524. case, and likely pessimized the common case by separating the
  525. two branches on isascii(c) at opposite ends of the function.
  526. commit 63f4b9f18f3674124d8bcb119739fec85e6da005
  527. Author: Timo Teräs <timo.teras@iki.fi>
  528. Date: Fri Jun 5 10:39:42 2015 +0300
  529. fix uselocale((locale_t)0) not to modify locale
  530. commit 68630b55c0c7219fe9df70dc28ffbf9efc8021d8 made the new locale to
  531. be assigned unconditonally resulting in crashes later on.
  532. --- a/.gitignore
  533. +++ b/.gitignore
  534. @@ -7,5 +7,7 @@ arch/*/bits/alltypes.h
  535. config.mak
  536. include/bits
  537. tools/musl-gcc
  538. +tools/musl-clang
  539. +tools/ld.musl-clang
  540. lib/musl-gcc.specs
  541. src/internal/version.h
  542. --- a/Makefile
  543. +++ b/Makefile
  544. @@ -51,6 +51,9 @@ TOOL_LIBS = lib/musl-gcc.specs
  545. ALL_LIBS = $(CRT_LIBS) $(STATIC_LIBS) $(SHARED_LIBS) $(EMPTY_LIBS) $(TOOL_LIBS)
  546. ALL_TOOLS = tools/musl-gcc
  547. +WRAPCC_GCC = gcc
  548. +WRAPCC_CLANG = clang
  549. +
  550. LDSO_PATHNAME = $(syslibdir)/ld-musl-$(ARCH)$(SUBARCH).so.1
  551. -include config.mak
  552. @@ -155,7 +158,11 @@ lib/musl-gcc.specs: tools/musl-gcc.specs
  553. sh $< "$(includedir)" "$(libdir)" "$(LDSO_PATHNAME)" > $@
  554. tools/musl-gcc: config.mak
  555. - printf '#!/bin/sh\nexec "$${REALGCC:-gcc}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
  556. + printf '#!/bin/sh\nexec "$${REALGCC:-$(WRAPCC_GCC)}" "$$@" -specs "%s/musl-gcc.specs"\n' "$(libdir)" > $@
  557. + chmod +x $@
  558. +
  559. +tools/%-clang: tools/%-clang.in config.mak
  560. + sed -e 's!@CC@!$(WRAPCC_CLANG)!g' -e 's!@PREFIX@!$(prefix)!g' -e 's!@INCDIR@!$(includedir)!g' -e 's!@LIBDIR@!$(libdir)!g' -e 's!@LDSO@!$(LDSO_PATHNAME)!g' $< > $@
  561. chmod +x $@
  562. $(DESTDIR)$(bindir)/%: tools/%
  563. --- a/arch/arm/syscall_arch.h
  564. +++ b/arch/arm/syscall_arch.h
  565. @@ -72,3 +72,7 @@ static inline long __syscall6(long n, lo
  566. register long r5 __asm__("r5") = f;
  567. __asm_syscall("r"(r7), "0"(r0), "r"(r1), "r"(r2), "r"(r3), "r"(r4), "r"(r5));
  568. }
  569. +
  570. +#define VDSO_USEFUL
  571. +#define VDSO_CGT_SYM "__vdso_clock_gettime"
  572. +#define VDSO_CGT_VER "LINUX_2.6"
  573. --- a/arch/mips/bits/signal.h
  574. +++ b/arch/mips/bits/signal.h
  575. @@ -28,7 +28,7 @@ struct sigcontext
  576. typedef struct
  577. {
  578. unsigned regmask, status;
  579. - unsigned long long pc, regs[32], fpregs[32];
  580. + unsigned long long pc, gregs[32], fpregs[32];
  581. unsigned ownedfp, fpc_csr, fpc_eir, used_math, dsp;
  582. unsigned long long mdhi, mdlo;
  583. unsigned long hi1, lo1, hi2, lo2, hi3, lo3;
  584. --- a/arch/mips/bits/socket.h
  585. +++ b/arch/mips/bits/socket.h
  586. @@ -22,26 +22,31 @@ struct cmsghdr
  587. #define SOL_SOCKET 65535
  588. #define SO_DEBUG 1
  589. -#define SO_REUSEADDR 4
  590. -#define SO_TYPE 0x1008
  591. -#define SO_ERROR 0x1007
  592. -#define SO_DONTROUTE 16
  593. -#define SO_BROADCAST 32
  594. +
  595. +#define SO_REUSEADDR 0x0004
  596. +#define SO_KEEPALIVE 0x0008
  597. +#define SO_DONTROUTE 0x0010
  598. +#define SO_BROADCAST 0x0020
  599. +#define SO_LINGER 0x0080
  600. +#define SO_OOBINLINE 0x0100
  601. +#define SO_REUSEPORT 0x0200
  602. #define SO_SNDBUF 0x1001
  603. #define SO_RCVBUF 0x1002
  604. -#define SO_KEEPALIVE 8
  605. -#define SO_OOBINLINE 256
  606. +#define SO_SNDLOWAT 0x1003
  607. +#define SO_RCVLOWAT 0x1004
  608. +#define SO_RCVTIMEO 0x1006
  609. +#define SO_SNDTIMEO 0x1005
  610. +#define SO_ERROR 0x1007
  611. +#define SO_TYPE 0x1008
  612. +#define SO_ACCEPTCONN 0x1009
  613. +#define SO_PROTOCOL 0x1028
  614. +#define SO_DOMAIN 0x1029
  615. +
  616. #define SO_NO_CHECK 11
  617. #define SO_PRIORITY 12
  618. -#define SO_LINGER 128
  619. #define SO_BSDCOMPAT 14
  620. -#define SO_REUSEPORT 512
  621. #define SO_PASSCRED 17
  622. #define SO_PEERCRED 18
  623. -#define SO_RCVLOWAT 0x1004
  624. -#define SO_SNDLOWAT 0x1003
  625. -#define SO_RCVTIMEO 0x1006
  626. -#define SO_SNDTIMEO 0x1005
  627. #define SO_SNDBUFFORCE 31
  628. #define SO_RCVBUFFORCE 33
  629. --- a/arch/mips/pthread_arch.h
  630. +++ b/arch/mips/pthread_arch.h
  631. @@ -13,4 +13,6 @@ static inline struct pthread *__pthread_
  632. #define TLS_ABOVE_TP
  633. #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + 0x7000)
  634. +#define DTP_OFFSET 0x8000
  635. +
  636. #define CANCEL_REG_IP (3-(union {int __i; char __b;}){1}.__b)
  637. --- a/arch/powerpc/bits/socket.h
  638. +++ b/arch/powerpc/bits/socket.h
  639. @@ -24,8 +24,6 @@ struct cmsghdr
  640. #define SO_BROADCAST 6
  641. #define SO_SNDBUF 7
  642. #define SO_RCVBUF 8
  643. -#define SO_SNDBUFFORCE 32
  644. -#define SO_RCVBUFFORCE 33
  645. #define SO_KEEPALIVE 9
  646. #define SO_OOBINLINE 10
  647. #define SO_NO_CHECK 11
  648. @@ -39,4 +37,8 @@ struct cmsghdr
  649. #define SO_SNDTIMEO 19
  650. #define SO_PASSCRED 20
  651. #define SO_PEERCRED 21
  652. -
  653. +#define SO_ACCEPTCONN 30
  654. +#define SO_SNDBUFFORCE 32
  655. +#define SO_RCVBUFFORCE 33
  656. +#define SO_PROTOCOL 38
  657. +#define SO_DOMAIN 39
  658. --- a/arch/powerpc/pthread_arch.h
  659. +++ b/arch/powerpc/pthread_arch.h
  660. @@ -12,6 +12,8 @@ static inline struct pthread *__pthread_
  661. #define TLS_ABOVE_TP
  662. #define TP_ADJ(p) ((char *)(p) + sizeof(struct pthread) + 0x7000)
  663. +#define DTP_OFFSET 0x8000
  664. +
  665. // offset of the PC register in mcontext_t, divided by the system wordsize
  666. // the kernel calls the ip "nip", it's the first saved value after the 32
  667. // GPRs.
  668. --- /dev/null
  669. +++ b/arch/sh/src/__set_thread_area.c
  670. @@ -0,0 +1,34 @@
  671. +#include "pthread_impl.h"
  672. +#include "libc.h"
  673. +#include "sh_atomic.h"
  674. +#include <elf.h>
  675. +
  676. +/* Also perform sh-specific init */
  677. +
  678. +#define CPU_HAS_LLSC 0x0040
  679. +
  680. +__attribute__((__visibility__("hidden"))) unsigned __sh_atomic_model, __sh_nommu;
  681. +
  682. +int __set_thread_area(void *p)
  683. +{
  684. + size_t *aux;
  685. + __asm__ __volatile__ ( "ldc %0, gbr" : : "r"(p) : "memory" );
  686. +#ifndef __SH4A__
  687. + if (__hwcap & CPU_HAS_LLSC) {
  688. + __sh_atomic_model = SH_A_LLSC;
  689. + return 0;
  690. + }
  691. +#if !defined(__SH3__) && !defined(__SH4__)
  692. + for (aux=libc.auxv; *aux; aux+=2) {
  693. + if (*aux != AT_PLATFORM) continue;
  694. + const char *s = (void *)aux[1];
  695. + if (s[0]!='s' || s[1]!='h' || s[2]!='2' || s[3]-'0'<10u) break;
  696. + __sh_atomic_model = SH_A_IMASK;
  697. + __sh_nommu = 1;
  698. + return 0;
  699. + }
  700. +#endif
  701. + /* __sh_atomic_model = SH_A_GUSA; */ /* 0, default */
  702. +#endif
  703. + return 0;
  704. +}
  705. --- /dev/null
  706. +++ b/arch/sh/src/__unmapself.c
  707. @@ -0,0 +1,19 @@
  708. +#include "pthread_impl.h"
  709. +
  710. +void __unmapself_sh_mmu(void *, size_t);
  711. +void __unmapself_sh_nommu(void *, size_t);
  712. +
  713. +#if !defined(__SH3__) && !defined(__SH4__)
  714. +#define __unmapself __unmapself_sh_nommu
  715. +#include "../../../src/thread/__unmapself.c"
  716. +#undef __unmapself
  717. +extern __attribute__((__visibility__("hidden"))) unsigned __sh_nommu;
  718. +#else
  719. +#define __sh_nommu 0
  720. +#endif
  721. +
  722. +void __unmapself(void *base, size_t size)
  723. +{
  724. + if (__sh_nommu) __unmapself_sh_nommu(base, size);
  725. + else __unmapself_sh_mmu(base, size);
  726. +}
  727. --- a/arch/sh/src/atomic.c
  728. +++ b/arch/sh/src/atomic.c
  729. @@ -1,8 +1,26 @@
  730. #ifndef __SH4A__
  731. +#include "sh_atomic.h"
  732. #include "atomic.h"
  733. #include "libc.h"
  734. +static inline unsigned mask()
  735. +{
  736. + unsigned sr;
  737. + __asm__ __volatile__ ( "\n"
  738. + " stc sr,r0 \n"
  739. + " mov r0,%0 \n"
  740. + " or #0xf0,r0 \n"
  741. + " ldc r0,sr \n"
  742. + : "=&r"(sr) : : "memory", "r0" );
  743. + return sr;
  744. +}
  745. +
  746. +static inline void unmask(unsigned sr)
  747. +{
  748. + __asm__ __volatile__ ( "ldc %0,sr" : : "r"(sr) : "memory" );
  749. +}
  750. +
  751. /* gusa is a hack in the kernel which lets you create a sequence of instructions
  752. * which will be restarted if the process is preempted in the middle of the
  753. * sequence. It will do for implementing atomics on non-smp systems. ABI is:
  754. @@ -25,11 +43,17 @@
  755. " mov.l " new ", @" mem "\n" \
  756. "1: mov r1, r15\n"
  757. -#define CPU_HAS_LLSC 0x0040
  758. -
  759. int __sh_cas(volatile int *p, int t, int s)
  760. {
  761. - if (__hwcap & CPU_HAS_LLSC) return __sh_cas_llsc(p, t, s);
  762. + if (__sh_atomic_model == SH_A_LLSC) return __sh_cas_llsc(p, t, s);
  763. +
  764. + if (__sh_atomic_model == SH_A_IMASK) {
  765. + unsigned sr = mask();
  766. + int old = *p;
  767. + if (old==t) *p = s;
  768. + unmask(sr);
  769. + return old;
  770. + }
  771. int old;
  772. __asm__ __volatile__(
  773. @@ -43,7 +67,15 @@ int __sh_cas(volatile int *p, int t, int
  774. int __sh_swap(volatile int *x, int v)
  775. {
  776. - if (__hwcap & CPU_HAS_LLSC) return __sh_swap_llsc(x, v);
  777. + if (__sh_atomic_model == SH_A_LLSC) return __sh_swap_llsc(x, v);
  778. +
  779. + if (__sh_atomic_model == SH_A_IMASK) {
  780. + unsigned sr = mask();
  781. + int old = *x;
  782. + *x = v;
  783. + unmask(sr);
  784. + return old;
  785. + }
  786. int old;
  787. __asm__ __volatile__(
  788. @@ -55,7 +87,15 @@ int __sh_swap(volatile int *x, int v)
  789. int __sh_fetch_add(volatile int *x, int v)
  790. {
  791. - if (__hwcap & CPU_HAS_LLSC) return __sh_fetch_add_llsc(x, v);
  792. + if (__sh_atomic_model == SH_A_LLSC) return __sh_fetch_add_llsc(x, v);
  793. +
  794. + if (__sh_atomic_model == SH_A_IMASK) {
  795. + unsigned sr = mask();
  796. + int old = *x;
  797. + *x = old + v;
  798. + unmask(sr);
  799. + return old;
  800. + }
  801. int old, dummy;
  802. __asm__ __volatile__(
  803. @@ -69,7 +109,7 @@ int __sh_fetch_add(volatile int *x, int
  804. void __sh_store(volatile int *p, int x)
  805. {
  806. - if (__hwcap & CPU_HAS_LLSC) return __sh_store_llsc(p, x);
  807. + if (__sh_atomic_model == SH_A_LLSC) return __sh_store_llsc(p, x);
  808. __asm__ __volatile__(
  809. " mov.l %1, @%0\n"
  810. : : "r"(p), "r"(x) : "memory");
  811. @@ -77,7 +117,15 @@ void __sh_store(volatile int *p, int x)
  812. void __sh_and(volatile int *x, int v)
  813. {
  814. - if (__hwcap & CPU_HAS_LLSC) return __sh_and_llsc(x, v);
  815. + if (__sh_atomic_model == SH_A_LLSC) return __sh_and_llsc(x, v);
  816. +
  817. + if (__sh_atomic_model == SH_A_IMASK) {
  818. + unsigned sr = mask();
  819. + int old = *x;
  820. + *x = old & v;
  821. + unmask(sr);
  822. + return;
  823. + }
  824. int dummy;
  825. __asm__ __volatile__(
  826. @@ -89,7 +137,15 @@ void __sh_and(volatile int *x, int v)
  827. void __sh_or(volatile int *x, int v)
  828. {
  829. - if (__hwcap & CPU_HAS_LLSC) return __sh_or_llsc(x, v);
  830. + if (__sh_atomic_model == SH_A_LLSC) return __sh_or_llsc(x, v);
  831. +
  832. + if (__sh_atomic_model == SH_A_IMASK) {
  833. + unsigned sr = mask();
  834. + int old = *x;
  835. + *x = old | v;
  836. + unmask(sr);
  837. + return;
  838. + }
  839. int dummy;
  840. __asm__ __volatile__(
  841. --- /dev/null
  842. +++ b/arch/sh/src/sh_atomic.h
  843. @@ -0,0 +1,15 @@
  844. +#ifndef _SH_ATOMIC_H
  845. +#define _SH_ATOMIC_H
  846. +
  847. +#define SH_A_GUSA 0
  848. +#define SH_A_LLSC 1
  849. +#define SH_A_CAS 2
  850. +#if !defined(__SH3__) && !defined(__SH4__)
  851. +#define SH_A_IMASK 3
  852. +#else
  853. +#define SH_A_IMASK -1LL /* unmatchable by unsigned int */
  854. +#endif
  855. +
  856. +extern __attribute__((__visibility__("hidden"))) unsigned __sh_atomic_model;
  857. +
  858. +#endif
  859. --- a/arch/sh/syscall_arch.h
  860. +++ b/arch/sh/syscall_arch.h
  861. @@ -8,7 +8,7 @@
  862. */
  863. #define __asm_syscall(trapno, ...) do { \
  864. __asm__ __volatile__ ( \
  865. - "trapa #" #trapno "\n" \
  866. + "trapa #31\n" \
  867. "or r0, r0\n" \
  868. "or r0, r0\n" \
  869. "or r0, r0\n" \
  870. --- a/configure
  871. +++ b/configure
  872. @@ -28,7 +28,7 @@ Optional features:
  873. --enable-debug build with debugging information [disabled]
  874. --enable-warnings build with recommended warnings flags [disabled]
  875. --enable-visibility use global visibility options to optimize PIC [auto]
  876. - --enable-gcc-wrapper build musl-gcc toolchain wrapper [auto]
  877. + --enable-wrapper=... build given musl toolchain wrapper [auto]
  878. --disable-shared inhibit building shared library [enabled]
  879. --disable-static inhibit building static library [enabled]
  880. @@ -123,6 +123,8 @@ bindir='$(exec_prefix)/bin'
  881. libdir='$(prefix)/lib'
  882. includedir='$(prefix)/include'
  883. syslibdir='/lib'
  884. +tools=
  885. +tool_libs=
  886. target=
  887. optimize=auto
  888. debug=no
  889. @@ -131,6 +133,8 @@ visibility=auto
  890. shared=auto
  891. static=yes
  892. wrapper=auto
  893. +gcc_wrapper=no
  894. +clang_wrapper=no
  895. for arg ; do
  896. case "$arg" in
  897. @@ -154,7 +158,12 @@ case "$arg" in
  898. --disable-warnings|--enable-warnings=no) warnings=no ;;
  899. --enable-visibility|--enable-visibility=yes) visibility=yes ;;
  900. --disable-visibility|--enable-visibility=no) visibility=no ;;
  901. ---enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ;;
  902. +--enable-wrapper|--enable-wrapper=yes) wrapper=detect ;;
  903. +--enable-wrapper=all) wrapper=yes ; gcc_wrapper=yes ; clang_wrapper=yes ;;
  904. +--enable-wrapper=gcc) wrapper=yes ; gcc_wrapper=yes ;;
  905. +--enable-wrapper=clang) wrapper=yes ; clang_wrapper=yes ;;
  906. +--disable-wrapper|--enable-wrapper=no) wrapper=no ;;
  907. +--enable-gcc-wrapper|--enable-gcc-wrapper=yes) wrapper=yes ; gcc_wrapper=yes ;;
  908. --disable-gcc-wrapper|--enable-gcc-wrapper=no) wrapper=no ;;
  909. --enable-*|--disable-*|--with-*|--without-*|--*dir=*|--build=*) ;;
  910. --host=*|--target=*) target=${arg#*=} ;;
  911. @@ -215,36 +224,51 @@ tryldflag LDFLAGS_TRY -Werror=unknown-wa
  912. tryldflag LDFLAGS_TRY -Werror=unused-command-line-argument
  913. #
  914. -# Need to know if the compiler is gcc to decide whether to build the
  915. -# musl-gcc wrapper, and for critical bug detection in some gcc versions.
  916. +# Need to know if the compiler is gcc or clang to decide which toolchain
  917. +# wrappers to build.
  918. #
  919. -printf "checking whether compiler is gcc... "
  920. -if fnmatch '*gcc\ version*' "$(LC_ALL=C $CC -v 2>&1)" ; then
  921. -cc_is_gcc=yes
  922. +printf "checking for C compiler family... "
  923. +cc_ver="$(LC_ALL=C $CC -v 2>&1)"
  924. +cc_family=unknown
  925. +if fnmatch '*gcc\ version*' "$cc_ver" ; then
  926. +cc_family=gcc
  927. +elif fnmatch '*clang\ version*' "$cc_ver" ; then
  928. +cc_family=clang
  929. +fi
  930. +echo "$cc_family"
  931. +
  932. +#
  933. +# Figure out toolchain wrapper to build
  934. +#
  935. +if test "$wrapper" = auto -o "$wrapper" = detect ; then
  936. +echo "#include <stdlib.h>" > "$tmpc"
  937. +echo "#if ! __GLIBC__" >> "$tmpc"
  938. +echo "#error no" >> "$tmpc"
  939. +echo "#endif" >> "$tmpc"
  940. +printf "checking for toolchain wrapper to build... "
  941. +if test "$wrapper" = auto && ! $CC -c -o /dev/null "$tmpc" >/dev/null 2>&1 ; then
  942. +echo "none"
  943. +elif test "$cc_family" = gcc ; then
  944. +gcc_wrapper=yes
  945. +echo "gcc"
  946. +elif test "$cc_family" = clang ; then
  947. +clang_wrapper=yes
  948. +echo "clang"
  949. else
  950. -cc_is_gcc=no
  951. +echo "none"
  952. +if test "$wrapper" = detect ; then
  953. +fail "$0: could not find an appropriate toolchain wrapper"
  954. fi
  955. -echo "$cc_is_gcc"
  956. -
  957. -#
  958. -# Only build musl-gcc wrapper if toolchain does not already target musl
  959. -#
  960. -if test "$wrapper" = auto ; then
  961. -printf "checking whether to build musl-gcc wrapper... "
  962. -if test "$cc_is_gcc" = yes ; then
  963. -wrapper=yes
  964. -while read line ; do
  965. -case "$line" in */ld-musl-*) wrapper=no ;; esac
  966. -done <<EOF
  967. -$($CC -dumpspecs)
  968. -EOF
  969. -else
  970. -wrapper=no
  971. fi
  972. -echo "$wrapper"
  973. fi
  974. -
  975. +if test "$gcc_wrapper" = yes ; then
  976. +tools="$tools tools/musl-gcc"
  977. +tool_libs="$tool_libs lib/musl-gcc.specs"
  978. +fi
  979. +if test "$clang_wrapper" = yes ; then
  980. +tools="$tools tools/musl-clang tools/ld.musl-clang"
  981. +fi
  982. #
  983. # Find the target architecture
  984. @@ -580,11 +604,13 @@ LDFLAGS = $LDFLAGS_AUTO $LDFLAGS
  985. CROSS_COMPILE = $CROSS_COMPILE
  986. LIBCC = $LIBCC
  987. OPTIMIZE_GLOBS = $OPTIMIZE_GLOBS
  988. +ALL_TOOLS = $tools
  989. +TOOL_LIBS = $tool_libs
  990. EOF
  991. test "x$static" = xno && echo "STATIC_LIBS ="
  992. test "x$shared" = xno && echo "SHARED_LIBS ="
  993. -test "x$wrapper" = xno && echo "ALL_TOOLS ="
  994. -test "x$wrapper" = xno && echo "TOOL_LIBS ="
  995. +test "x$cc_family" = xgcc && echo 'WRAPCC_GCC = $(CC)'
  996. +test "x$cc_family" = xclang && echo 'WRAPCC_CLANG = $(CC)'
  997. exec 1>&3 3>&-
  998. printf "done\n"
  999. --- a/include/ctype.h
  1000. +++ b/include/ctype.h
  1001. @@ -64,6 +64,7 @@ int isascii(int);
  1002. int toascii(int);
  1003. #define _tolower(a) ((a)|0x20)
  1004. #define _toupper(a) ((a)&0x5f)
  1005. +#define isascii(a) (0 ? isascii(a) : (unsigned)(a) < 128)
  1006. #endif
  1007. --- a/include/stdlib.h
  1008. +++ b/include/stdlib.h
  1009. @@ -76,7 +76,8 @@ size_t wcstombs (char *__restrict, const
  1010. #define EXIT_FAILURE 1
  1011. #define EXIT_SUCCESS 0
  1012. -#define MB_CUR_MAX ((size_t)+4)
  1013. +size_t __ctype_get_mb_cur_max(void);
  1014. +#define MB_CUR_MAX (__ctype_get_mb_cur_max())
  1015. #define RAND_MAX (0x7fffffff)
  1016. --- a/include/sys/socket.h
  1017. +++ b/include/sys/socket.h
  1018. @@ -177,8 +177,11 @@ struct linger
  1019. #define SO_SNDLOWAT 19
  1020. #define SO_RCVTIMEO 20
  1021. #define SO_SNDTIMEO 21
  1022. +#define SO_ACCEPTCONN 30
  1023. #define SO_SNDBUFFORCE 32
  1024. #define SO_RCVBUFFORCE 33
  1025. +#define SO_PROTOCOL 38
  1026. +#define SO_DOMAIN 39
  1027. #endif
  1028. #define SO_SECURITY_AUTHENTICATION 22
  1029. @@ -195,7 +198,6 @@ struct linger
  1030. #define SO_TIMESTAMP 29
  1031. #define SCM_TIMESTAMP SO_TIMESTAMP
  1032. -#define SO_ACCEPTCONN 30
  1033. #define SO_PEERSEC 31
  1034. #define SO_PASSSEC 34
  1035. #define SO_TIMESTAMPNS 35
  1036. @@ -203,8 +205,6 @@ struct linger
  1037. #define SO_MARK 36
  1038. #define SO_TIMESTAMPING 37
  1039. #define SCM_TIMESTAMPING SO_TIMESTAMPING
  1040. -#define SO_PROTOCOL 38
  1041. -#define SO_DOMAIN 39
  1042. #define SO_RXQ_OVFL 40
  1043. #define SO_WIFI_STATUS 41
  1044. #define SCM_WIFI_STATUS SO_WIFI_STATUS
  1045. --- a/include/unistd.h
  1046. +++ b/include/unistd.h
  1047. @@ -185,7 +185,7 @@ int setresgid(gid_t, gid_t, gid_t);
  1048. int getresuid(uid_t *, uid_t *, uid_t *);
  1049. int getresgid(gid_t *, gid_t *, gid_t *);
  1050. char *get_current_dir_name(void);
  1051. -void syncfs(int);
  1052. +int syncfs(int);
  1053. int euidaccess(const char *, int);
  1054. int eaccess(const char *, int);
  1055. #endif
  1056. --- a/src/ctype/__ctype_get_mb_cur_max.c
  1057. +++ b/src/ctype/__ctype_get_mb_cur_max.c
  1058. @@ -1,6 +1,7 @@
  1059. -#include <stddef.h>
  1060. +#include <stdlib.h>
  1061. +#include "locale_impl.h"
  1062. size_t __ctype_get_mb_cur_max()
  1063. {
  1064. - return 4;
  1065. + return MB_CUR_MAX;
  1066. }
  1067. --- a/src/ctype/isascii.c
  1068. +++ b/src/ctype/isascii.c
  1069. @@ -1,4 +1,5 @@
  1070. #include <ctype.h>
  1071. +#undef isascii
  1072. int isascii(int c)
  1073. {
  1074. --- a/src/env/__stack_chk_fail.c
  1075. +++ b/src/env/__stack_chk_fail.c
  1076. @@ -25,4 +25,8 @@ void __stack_chk_fail_local(void)
  1077. a_crash();
  1078. }
  1079. +#else
  1080. +
  1081. +weak_alias(__stack_chk_fail, __stack_chk_fail_local);
  1082. +
  1083. #endif
  1084. --- a/src/internal/libc.h
  1085. +++ b/src/internal/libc.h
  1086. @@ -17,8 +17,6 @@ struct __libc {
  1087. int secure;
  1088. volatile int threads_minus_1;
  1089. size_t *auxv;
  1090. - FILE *ofl_head;
  1091. - volatile int ofl_lock[2];
  1092. size_t tls_size;
  1093. size_t page_size;
  1094. struct __locale_struct global_locale;
  1095. --- a/src/internal/locale_impl.h
  1096. +++ b/src/internal/locale_impl.h
  1097. @@ -1,3 +1,6 @@
  1098. +#ifndef _LOCALE_IMPL_H
  1099. +#define _LOCALE_IMPL_H
  1100. +
  1101. #include <locale.h>
  1102. #include <stdlib.h>
  1103. #include "libc.h"
  1104. @@ -12,6 +15,10 @@ struct __locale_map {
  1105. const struct __locale_map *next;
  1106. };
  1107. +extern const struct __locale_map __c_dot_utf8;
  1108. +extern const struct __locale_struct __c_locale;
  1109. +extern const struct __locale_struct __c_dot_utf8_locale;
  1110. +
  1111. const struct __locale_map *__get_locale(int, const char *);
  1112. const char *__mo_lookup(const void *, size_t, const char *);
  1113. const char *__lctrans(const char *, const struct __locale_map *);
  1114. @@ -20,9 +27,14 @@ const char *__lctrans_cur(const char *);
  1115. #define LCTRANS(msg, lc, loc) __lctrans(msg, (loc)->cat[(lc)])
  1116. #define LCTRANS_CUR(msg) __lctrans_cur(msg)
  1117. +#define C_LOCALE ((locale_t)&__c_locale)
  1118. +#define UTF8_LOCALE ((locale_t)&__c_dot_utf8_locale)
  1119. +
  1120. #define CURRENT_LOCALE (__pthread_self()->locale)
  1121. #define CURRENT_UTF8 (!!__pthread_self()->locale->cat[LC_CTYPE])
  1122. #undef MB_CUR_MAX
  1123. #define MB_CUR_MAX (CURRENT_UTF8 ? 4 : 1)
  1124. +
  1125. +#endif
  1126. --- a/src/internal/pthread_impl.h
  1127. +++ b/src/internal/pthread_impl.h
  1128. @@ -94,6 +94,10 @@ struct __timer {
  1129. #define CANARY canary
  1130. #endif
  1131. +#ifndef DTP_OFFSET
  1132. +#define DTP_OFFSET 0
  1133. +#endif
  1134. +
  1135. #define SIGTIMER 32
  1136. #define SIGCANCEL 33
  1137. #define SIGSYNCCALL 34
  1138. --- a/src/internal/sh/syscall.s
  1139. +++ b/src/internal/sh/syscall.s
  1140. @@ -13,7 +13,7 @@ __syscall:
  1141. mov.l @r15, r7
  1142. mov.l @(4,r15), r0
  1143. mov.l @(8,r15), r1
  1144. - trapa #22
  1145. + trapa #31
  1146. or r0, r0
  1147. or r0, r0
  1148. or r0, r0
  1149. --- a/src/internal/stdio_impl.h
  1150. +++ b/src/internal/stdio_impl.h
  1151. @@ -47,6 +47,7 @@ struct _IO_FILE {
  1152. unsigned char *shend;
  1153. off_t shlim, shcnt;
  1154. FILE *prev_locked, *next_locked;
  1155. + struct __locale_struct *locale;
  1156. };
  1157. size_t __stdio_read(FILE *, unsigned char *, size_t);
  1158. @@ -75,8 +76,9 @@ int __putc_unlocked(int, FILE *);
  1159. FILE *__fdopen(int, const char *);
  1160. int __fmodeflags(const char *);
  1161. -#define OFLLOCK() LOCK(libc.ofl_lock)
  1162. -#define OFLUNLOCK() UNLOCK(libc.ofl_lock)
  1163. +FILE *__ofl_add(FILE *f);
  1164. +FILE **__ofl_lock(void);
  1165. +void __ofl_unlock(void);
  1166. #define feof(f) ((f)->flags & F_EOF)
  1167. #define ferror(f) ((f)->flags & F_ERR)
  1168. --- a/src/ldso/dynlink.c
  1169. +++ b/src/ldso/dynlink.c
  1170. @@ -156,7 +156,7 @@ static uint32_t gnu_hash(const char *s0)
  1171. const unsigned char *s = (void *)s0;
  1172. uint_fast32_t h = 5381;
  1173. for (; *s; s++)
  1174. - h = h*33 + *s;
  1175. + h += h*32 + *s;
  1176. return h;
  1177. }
  1178. @@ -174,32 +174,39 @@ static Sym *sysv_lookup(const char *s, u
  1179. return 0;
  1180. }
  1181. -static Sym *gnu_lookup(const char *s, uint32_t h1, struct dso *dso)
  1182. +static Sym *gnu_lookup(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s)
  1183. {
  1184. - Sym *syms = dso->syms;
  1185. - char *strings = dso->strings;
  1186. - uint32_t *hashtab = dso->ghashtab;
  1187. uint32_t nbuckets = hashtab[0];
  1188. uint32_t *buckets = hashtab + 4 + hashtab[2]*(sizeof(size_t)/4);
  1189. - uint32_t h2;
  1190. - uint32_t *hashval;
  1191. uint32_t i = buckets[h1 % nbuckets];
  1192. if (!i) return 0;
  1193. - hashval = buckets + nbuckets + (i - hashtab[1]);
  1194. + uint32_t *hashval = buckets + nbuckets + (i - hashtab[1]);
  1195. for (h1 |= 1; ; i++) {
  1196. - h2 = *hashval++;
  1197. - if ((!dso->versym || dso->versym[i] >= 0)
  1198. - && (h1 == (h2|1)) && !strcmp(s, strings + syms[i].st_name))
  1199. - return syms+i;
  1200. + uint32_t h2 = *hashval++;
  1201. + if ((h1 == (h2|1)) && (!dso->versym || dso->versym[i] >= 0)
  1202. + && !strcmp(s, dso->strings + dso->syms[i].st_name))
  1203. + return dso->syms+i;
  1204. if (h2 & 1) break;
  1205. }
  1206. return 0;
  1207. }
  1208. +static Sym *gnu_lookup_filtered(uint32_t h1, uint32_t *hashtab, struct dso *dso, const char *s, uint32_t fofs, size_t fmask)
  1209. +{
  1210. + const size_t *bloomwords = (const void *)(hashtab+4);
  1211. + size_t f = bloomwords[fofs & (hashtab[2]-1)];
  1212. + if (!(f & fmask)) return 0;
  1213. +
  1214. + f >>= (h1 >> hashtab[3]) % (8 * sizeof f);
  1215. + if (!(f & 1)) return 0;
  1216. +
  1217. + return gnu_lookup(h1, hashtab, dso, s);
  1218. +}
  1219. +
  1220. #define OK_TYPES (1<<STT_NOTYPE | 1<<STT_OBJECT | 1<<STT_FUNC | 1<<STT_COMMON | 1<<STT_TLS)
  1221. #define OK_BINDS (1<<STB_GLOBAL | 1<<STB_WEAK | 1<<STB_GNU_UNIQUE)
  1222. @@ -209,14 +216,20 @@ static Sym *gnu_lookup(const char *s, ui
  1223. static struct symdef find_sym(struct dso *dso, const char *s, int need_def)
  1224. {
  1225. - uint32_t h = 0, gh = 0;
  1226. + uint32_t h = 0, gh, gho, *ght;
  1227. + size_t ghm = 0;
  1228. struct symdef def = {0};
  1229. for (; dso; dso=dso->next) {
  1230. Sym *sym;
  1231. if (!dso->global) continue;
  1232. - if (dso->ghashtab) {
  1233. - if (!gh) gh = gnu_hash(s);
  1234. - sym = gnu_lookup(s, gh, dso);
  1235. + if ((ght = dso->ghashtab)) {
  1236. + if (!ghm) {
  1237. + gh = gnu_hash(s);
  1238. + int maskbits = 8 * sizeof ghm;
  1239. + gho = gh / maskbits;
  1240. + ghm = 1ul << gh % maskbits;
  1241. + }
  1242. + sym = gnu_lookup_filtered(gh, ght, dso, s, gho, ghm);
  1243. } else {
  1244. if (!h) h = sysv_hash(s);
  1245. sym = sysv_lookup(s, h, dso);
  1246. @@ -337,7 +350,7 @@ static void do_relocs(struct dso *dso, s
  1247. *reloc_addr = def.dso->tls_id;
  1248. break;
  1249. case REL_DTPOFF:
  1250. - *reloc_addr = tls_val + addend;
  1251. + *reloc_addr = tls_val + addend - DTP_OFFSET;
  1252. break;
  1253. #ifdef TLS_ABOVE_TP
  1254. case REL_TPOFF:
  1255. @@ -423,6 +436,28 @@ static void reclaim_gaps(struct dso *dso
  1256. }
  1257. }
  1258. +static void *mmap_fixed(void *p, size_t n, int prot, int flags, int fd, off_t off)
  1259. +{
  1260. + char *q = mmap(p, n, prot, flags, fd, off);
  1261. + if (q != MAP_FAILED || errno != EINVAL) return q;
  1262. + /* Fallbacks for MAP_FIXED failure on NOMMU kernels. */
  1263. + if (flags & MAP_ANONYMOUS) {
  1264. + memset(p, 0, n);
  1265. + return p;
  1266. + }
  1267. + ssize_t r;
  1268. + if (lseek(fd, off, SEEK_SET) < 0) return MAP_FAILED;
  1269. + for (q=p; n; q+=r, off+=r, n-=r) {
  1270. + r = read(fd, q, n);
  1271. + if (r < 0 && errno != EINTR) return MAP_FAILED;
  1272. + if (!r) {
  1273. + memset(q, 0, n);
  1274. + break;
  1275. + }
  1276. + }
  1277. + return p;
  1278. +}
  1279. +
  1280. static void *map_library(int fd, struct dso *dso)
  1281. {
  1282. Ehdr buf[(896+sizeof(Ehdr))/sizeof(Ehdr)];
  1283. @@ -524,19 +559,20 @@ static void *map_library(int fd, struct
  1284. prot = (((ph->p_flags&PF_R) ? PROT_READ : 0) |
  1285. ((ph->p_flags&PF_W) ? PROT_WRITE: 0) |
  1286. ((ph->p_flags&PF_X) ? PROT_EXEC : 0));
  1287. - if (mmap(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
  1288. + if (mmap_fixed(base+this_min, this_max-this_min, prot, MAP_PRIVATE|MAP_FIXED, fd, off_start) == MAP_FAILED)
  1289. goto error;
  1290. if (ph->p_memsz > ph->p_filesz) {
  1291. size_t brk = (size_t)base+ph->p_vaddr+ph->p_filesz;
  1292. size_t pgbrk = brk+PAGE_SIZE-1 & -PAGE_SIZE;
  1293. memset((void *)brk, 0, pgbrk-brk & PAGE_SIZE-1);
  1294. - 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)
  1295. + 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)
  1296. goto error;
  1297. }
  1298. }
  1299. for (i=0; ((size_t *)(base+dyn))[i]; i+=2)
  1300. if (((size_t *)(base+dyn))[i]==DT_TEXTREL) {
  1301. - if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC) < 0)
  1302. + if (mprotect(map, map_len, PROT_READ|PROT_WRITE|PROT_EXEC)
  1303. + && errno != ENOSYS)
  1304. goto error;
  1305. break;
  1306. }
  1307. @@ -927,7 +963,8 @@ static void reloc_all(struct dso *p)
  1308. do_relocs(p, (void *)(p->base+dyn[DT_RELA]), dyn[DT_RELASZ], 3);
  1309. if (head != &ldso && p->relro_start != p->relro_end &&
  1310. - mprotect(p->base+p->relro_start, p->relro_end-p->relro_start, PROT_READ) < 0) {
  1311. + mprotect(p->base+p->relro_start, p->relro_end-p->relro_start, PROT_READ)
  1312. + && errno != ENOSYS) {
  1313. error("Error relocating %s: RELRO protection failed: %m",
  1314. p->name);
  1315. if (runtime) longjmp(*rtld_fail, 1);
  1316. @@ -1078,7 +1115,7 @@ void *__tls_get_new(size_t *v)
  1317. __block_all_sigs(&set);
  1318. if (v[0]<=(size_t)self->dtv[0]) {
  1319. __restore_sigs(&set);
  1320. - return (char *)self->dtv[v[0]]+v[1];
  1321. + return (char *)self->dtv[v[0]]+v[1]+DTP_OFFSET;
  1322. }
  1323. /* This is safe without any locks held because, if the caller
  1324. @@ -1111,7 +1148,7 @@ void *__tls_get_new(size_t *v)
  1325. if (p->tls_id == v[0]) break;
  1326. }
  1327. __restore_sigs(&set);
  1328. - return mem + v[1];
  1329. + return mem + v[1] + DTP_OFFSET;
  1330. }
  1331. static void update_tls_size()
  1332. @@ -1192,6 +1229,17 @@ _Noreturn void __dls3(size_t *sp)
  1333. char **argv_orig = argv;
  1334. char **envp = argv+argc+1;
  1335. + /* Find aux vector just past environ[] and use it to initialize
  1336. + * global data that may be needed before we can make syscalls. */
  1337. + __environ = envp;
  1338. + for (i=argc+1; argv[i]; i++);
  1339. + libc.auxv = auxv = (void *)(argv+i+1);
  1340. + decode_vec(auxv, aux, AUX_CNT);
  1341. + __hwcap = aux[AT_HWCAP];
  1342. + libc.page_size = aux[AT_PAGESZ];
  1343. + libc.secure = ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
  1344. + || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]);
  1345. +
  1346. /* Setup early thread pointer in builtin_tls for ldso/libc itself to
  1347. * use during dynamic linking. If possible it will also serve as the
  1348. * thread pointer at runtime. */
  1349. @@ -1200,25 +1248,11 @@ _Noreturn void __dls3(size_t *sp)
  1350. a_crash();
  1351. }
  1352. - /* Find aux vector just past environ[] */
  1353. - for (i=argc+1; argv[i]; i++)
  1354. - if (!memcmp(argv[i], "LD_LIBRARY_PATH=", 16))
  1355. - env_path = argv[i]+16;
  1356. - else if (!memcmp(argv[i], "LD_PRELOAD=", 11))
  1357. - env_preload = argv[i]+11;
  1358. - auxv = (void *)(argv+i+1);
  1359. -
  1360. - decode_vec(auxv, aux, AUX_CNT);
  1361. -
  1362. /* Only trust user/env if kernel says we're not suid/sgid */
  1363. - if ((aux[0]&0x7800)!=0x7800 || aux[AT_UID]!=aux[AT_EUID]
  1364. - || aux[AT_GID]!=aux[AT_EGID] || aux[AT_SECURE]) {
  1365. - env_path = 0;
  1366. - env_preload = 0;
  1367. - libc.secure = 1;
  1368. + if (!libc.secure) {
  1369. + env_path = getenv("LD_LIBRARY_PATH");
  1370. + env_preload = getenv("LD_PRELOAD");
  1371. }
  1372. - libc.page_size = aux[AT_PAGESZ];
  1373. - libc.auxv = auxv;
  1374. /* If the main program was already loaded by the kernel,
  1375. * AT_PHDR will point to some location other than the dynamic
  1376. @@ -1523,7 +1557,7 @@ void *__tls_get_addr(size_t *);
  1377. static void *do_dlsym(struct dso *p, const char *s, void *ra)
  1378. {
  1379. size_t i;
  1380. - uint32_t h = 0, gh = 0;
  1381. + uint32_t h = 0, gh = 0, *ght;
  1382. Sym *sym;
  1383. if (p == head || p == RTLD_DEFAULT || p == RTLD_NEXT) {
  1384. if (p == RTLD_DEFAULT) {
  1385. @@ -1541,9 +1575,9 @@ static void *do_dlsym(struct dso *p, con
  1386. }
  1387. if (invalid_dso_handle(p))
  1388. return 0;
  1389. - if (p->ghashtab) {
  1390. + if ((ght = p->ghashtab)) {
  1391. gh = gnu_hash(s);
  1392. - sym = gnu_lookup(s, gh, p);
  1393. + sym = gnu_lookup(gh, ght, p, s);
  1394. } else {
  1395. h = sysv_hash(s);
  1396. sym = sysv_lookup(s, h, p);
  1397. @@ -1553,9 +1587,9 @@ static void *do_dlsym(struct dso *p, con
  1398. if (sym && sym->st_value && (1<<(sym->st_info&0xf) & OK_TYPES))
  1399. return p->base + sym->st_value;
  1400. if (p->deps) for (i=0; p->deps[i]; i++) {
  1401. - if (p->deps[i]->ghashtab) {
  1402. + if ((ght = p->deps[i]->ghashtab)) {
  1403. if (!gh) gh = gnu_hash(s);
  1404. - sym = gnu_lookup(s, gh, p->deps[i]);
  1405. + sym = gnu_lookup(gh, ght, p->deps[i], s);
  1406. } else {
  1407. if (!h) h = sysv_hash(s);
  1408. sym = sysv_lookup(s, h, p->deps[i]);
  1409. --- a/src/linux/syncfs.c
  1410. +++ b/src/linux/syncfs.c
  1411. @@ -2,7 +2,7 @@
  1412. #include <unistd.h>
  1413. #include "syscall.h"
  1414. -void syncfs(int fd)
  1415. +int syncfs(int fd)
  1416. {
  1417. - __syscall(SYS_syncfs, fd);
  1418. + return syscall(SYS_syncfs, fd);
  1419. }
  1420. --- /dev/null
  1421. +++ b/src/locale/c_locale.c
  1422. @@ -0,0 +1,15 @@
  1423. +#include "locale_impl.h"
  1424. +#include <stdint.h>
  1425. +
  1426. +static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };
  1427. +
  1428. +const struct __locale_map __c_dot_utf8 = {
  1429. + .map = empty_mo,
  1430. + .map_size = sizeof empty_mo,
  1431. + .name = "C.UTF-8"
  1432. +};
  1433. +
  1434. +const struct __locale_struct __c_locale = { 0 };
  1435. +const struct __locale_struct __c_dot_utf8_locale = {
  1436. + .cat[LC_CTYPE] = &__c_dot_utf8
  1437. +};
  1438. --- a/src/locale/iconv.c
  1439. +++ b/src/locale/iconv.c
  1440. @@ -5,6 +5,7 @@
  1441. #include <stdlib.h>
  1442. #include <limits.h>
  1443. #include <stdint.h>
  1444. +#include "locale_impl.h"
  1445. #define UTF_32BE 0300
  1446. #define UTF_16LE 0301
  1447. @@ -165,9 +166,12 @@ size_t iconv(iconv_t cd0, char **restric
  1448. int err;
  1449. unsigned char type = map[-1];
  1450. unsigned char totype = tomap[-1];
  1451. + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
  1452. if (!in || !*in || !*inb) return 0;
  1453. + *ploc = UTF8_LOCALE;
  1454. +
  1455. for (; *inb; *in+=l, *inb-=l) {
  1456. c = *(unsigned char *)*in;
  1457. l = 1;
  1458. @@ -431,6 +435,7 @@ size_t iconv(iconv_t cd0, char **restric
  1459. break;
  1460. }
  1461. }
  1462. + *ploc = loc;
  1463. return x;
  1464. ilseq:
  1465. err = EILSEQ;
  1466. @@ -445,5 +450,6 @@ starved:
  1467. x = -1;
  1468. end:
  1469. errno = err;
  1470. + *ploc = loc;
  1471. return x;
  1472. }
  1473. --- a/src/locale/langinfo.c
  1474. +++ b/src/locale/langinfo.c
  1475. @@ -33,7 +33,8 @@ char *__nl_langinfo_l(nl_item item, loca
  1476. int idx = item & 65535;
  1477. const char *str;
  1478. - if (item == CODESET) return "UTF-8";
  1479. + if (item == CODESET)
  1480. + return MB_CUR_MAX==1 ? "UTF-8-CODE-UNITS" : "UTF-8";
  1481. switch (cat) {
  1482. case LC_NUMERIC:
  1483. --- a/src/locale/locale_map.c
  1484. +++ b/src/locale/locale_map.c
  1485. @@ -24,14 +24,6 @@ static const char envvars[][12] = {
  1486. "LC_MESSAGES",
  1487. };
  1488. -static const uint32_t empty_mo[] = { 0x950412de, 0, -1, -1, -1 };
  1489. -
  1490. -const struct __locale_map __c_dot_utf8 = {
  1491. - .map = empty_mo,
  1492. - .map_size = sizeof empty_mo,
  1493. - .name = "C.UTF-8"
  1494. -};
  1495. -
  1496. const struct __locale_map *__get_locale(int cat, const char *val)
  1497. {
  1498. static int lock[2];
  1499. @@ -107,8 +99,8 @@ const struct __locale_map *__get_locale(
  1500. * sake of being able to do message translations at the
  1501. * application level. */
  1502. if (!new && (new = malloc(sizeof *new))) {
  1503. - new->map = empty_mo;
  1504. - new->map_size = sizeof empty_mo;
  1505. + new->map = __c_dot_utf8.map;
  1506. + new->map_size = __c_dot_utf8.map_size;
  1507. memcpy(new->name, val, n);
  1508. new->name[n] = 0;
  1509. new->next = loc_head;
  1510. --- a/src/locale/newlocale.c
  1511. +++ b/src/locale/newlocale.c
  1512. @@ -3,16 +3,9 @@
  1513. #include "locale_impl.h"
  1514. #include "libc.h"
  1515. -extern const struct __locale_map __c_dot_utf8;
  1516. -
  1517. -static const struct __locale_struct c_locale = { 0 };
  1518. -static const struct __locale_struct c_dot_utf8_locale = {
  1519. - .cat[LC_CTYPE] = &__c_dot_utf8
  1520. -};
  1521. -
  1522. int __loc_is_allocated(locale_t loc)
  1523. {
  1524. - return loc && loc != &c_locale && loc != &c_dot_utf8_locale;
  1525. + return loc && loc != C_LOCALE && loc != UTF8_LOCALE;
  1526. }
  1527. locale_t __newlocale(int mask, const char *name, locale_t loc)
  1528. @@ -44,9 +37,9 @@ locale_t __newlocale(int mask, const cha
  1529. }
  1530. if (!j)
  1531. - return (locale_t)&c_locale;
  1532. - if (j==1 && tmp.cat[LC_CTYPE]==c_dot_utf8_locale.cat[LC_CTYPE])
  1533. - return (locale_t)&c_dot_utf8_locale;
  1534. + return C_LOCALE;
  1535. + if (j==1 && tmp.cat[LC_CTYPE]==&__c_dot_utf8)
  1536. + return UTF8_LOCALE;
  1537. if ((loc = malloc(sizeof *loc))) *loc = tmp;
  1538. --- a/src/locale/uselocale.c
  1539. +++ b/src/locale/uselocale.c
  1540. @@ -8,9 +8,7 @@ locale_t __uselocale(locale_t new)
  1541. locale_t old = self->locale;
  1542. locale_t global = &libc.global_locale;
  1543. - if (new == LC_GLOBAL_LOCALE) new = global;
  1544. -
  1545. - self->locale = new;
  1546. + if (new) self->locale = new == LC_GLOBAL_LOCALE ? global : new;
  1547. return old == global ? LC_GLOBAL_LOCALE : old;
  1548. }
  1549. --- a/src/malloc/calloc.c
  1550. +++ b/src/malloc/calloc.c
  1551. @@ -1,22 +1,13 @@
  1552. #include <stdlib.h>
  1553. #include <errno.h>
  1554. +void *__malloc0(size_t);
  1555. +
  1556. void *calloc(size_t m, size_t n)
  1557. {
  1558. - void *p;
  1559. - size_t *z;
  1560. if (n && m > (size_t)-1/n) {
  1561. errno = ENOMEM;
  1562. return 0;
  1563. }
  1564. - n *= m;
  1565. - p = malloc(n);
  1566. - if (!p) return 0;
  1567. - /* Only do this for non-mmapped chunks */
  1568. - if (((size_t *)p)[-1] & 7) {
  1569. - /* Only write words that are not already zero */
  1570. - m = (n + sizeof *z - 1)/sizeof *z;
  1571. - for (z=p; m; m--, z++) if (*z) *z=0;
  1572. - }
  1573. - return p;
  1574. + return __malloc0(n * m);
  1575. }
  1576. --- /dev/null
  1577. +++ b/src/malloc/expand_heap.c
  1578. @@ -0,0 +1,72 @@
  1579. +#include <limits.h>
  1580. +#include <stdint.h>
  1581. +#include <errno.h>
  1582. +#include <sys/mman.h>
  1583. +#include "libc.h"
  1584. +#include "syscall.h"
  1585. +
  1586. +/* This function returns true if the interval [old,new]
  1587. + * intersects the 'len'-sized interval below &libc.auxv
  1588. + * (interpreted as the main-thread stack) or below &b
  1589. + * (the current stack). It is used to defend against
  1590. + * buggy brk implementations that can cross the stack. */
  1591. +
  1592. +static int traverses_stack_p(uintptr_t old, uintptr_t new)
  1593. +{
  1594. + const uintptr_t len = 8<<20;
  1595. + uintptr_t a, b;
  1596. +
  1597. + b = (uintptr_t)libc.auxv;
  1598. + a = b > len ? b-len : 0;
  1599. + if (new>a && old<b) return 1;
  1600. +
  1601. + b = (uintptr_t)&b;
  1602. + a = b > len ? b-len : 0;
  1603. + if (new>a && old<b) return 1;
  1604. +
  1605. + return 0;
  1606. +}
  1607. +
  1608. +void *__mmap(void *, size_t, int, int, int, off_t);
  1609. +
  1610. +/* Expand the heap in-place if brk can be used, or otherwise via mmap,
  1611. + * using an exponential lower bound on growth by mmap to make
  1612. + * fragmentation asymptotically irrelevant. The size argument is both
  1613. + * an input and an output, since the caller needs to know the size
  1614. + * allocated, which will be larger than requested due to page alignment
  1615. + * and mmap minimum size rules. The caller is responsible for locking
  1616. + * to prevent concurrent calls. */
  1617. +
  1618. +void *__expand_heap(size_t *pn)
  1619. +{
  1620. + static uintptr_t brk;
  1621. + static unsigned mmap_step;
  1622. + size_t n = *pn;
  1623. +
  1624. + if (n > SIZE_MAX/2 - PAGE_SIZE) {
  1625. + errno = ENOMEM;
  1626. + return 0;
  1627. + }
  1628. + n += -n & PAGE_SIZE-1;
  1629. +
  1630. + if (!brk) {
  1631. + brk = __syscall(SYS_brk, 0);
  1632. + brk += -brk & PAGE_SIZE-1;
  1633. + }
  1634. +
  1635. + if (n < SIZE_MAX-brk && !traverses_stack_p(brk, brk+n)
  1636. + && __syscall(SYS_brk, brk+n)==brk+n) {
  1637. + *pn = n;
  1638. + brk += n;
  1639. + return (void *)(brk-n);
  1640. + }
  1641. +
  1642. + size_t min = (size_t)PAGE_SIZE << mmap_step/2;
  1643. + if (n < min) n = min;
  1644. + void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
  1645. + MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  1646. + if (area == MAP_FAILED) return 0;
  1647. + *pn = n;
  1648. + mmap_step++;
  1649. + return area;
  1650. +}
  1651. --- a/src/malloc/lite_malloc.c
  1652. +++ b/src/malloc/lite_malloc.c
  1653. @@ -4,43 +4,47 @@
  1654. #include <errno.h>
  1655. #include "libc.h"
  1656. -uintptr_t __brk(uintptr_t);
  1657. -
  1658. #define ALIGN 16
  1659. +void *__expand_heap(size_t *);
  1660. +
  1661. void *__simple_malloc(size_t n)
  1662. {
  1663. - static uintptr_t cur, brk;
  1664. - uintptr_t base, new;
  1665. + static char *cur, *end;
  1666. static volatile int lock[2];
  1667. - size_t align=1;
  1668. + size_t align=1, pad;
  1669. + void *p;
  1670. if (!n) n++;
  1671. - if (n > SIZE_MAX/2) goto toobig;
  1672. -
  1673. while (align<n && align<ALIGN)
  1674. align += align;
  1675. - n = n + align - 1 & -align;
  1676. LOCK(lock);
  1677. - if (!cur) cur = brk = __brk(0)+16;
  1678. - base = cur + align-1 & -align;
  1679. - if (n > SIZE_MAX - PAGE_SIZE - base) goto fail;
  1680. - if (base+n > brk) {
  1681. - new = base+n + PAGE_SIZE-1 & -PAGE_SIZE;
  1682. - if (__brk(new) != new) goto fail;
  1683. - brk = new;
  1684. - }
  1685. - cur = base+n;
  1686. - UNLOCK(lock);
  1687. - return (void *)base;
  1688. + pad = -(uintptr_t)cur & align-1;
  1689. +
  1690. + if (n <= SIZE_MAX/2 + ALIGN) n += pad;
  1691. +
  1692. + if (n > end-cur) {
  1693. + size_t m = n;
  1694. + char *new = __expand_heap(&m);
  1695. + if (!new) {
  1696. + UNLOCK(lock);
  1697. + return 0;
  1698. + }
  1699. + if (new != end) {
  1700. + cur = new;
  1701. + n -= pad;
  1702. + pad = 0;
  1703. + }
  1704. + end = new + m;
  1705. + }
  1706. -fail:
  1707. + p = cur + pad;
  1708. + cur += n;
  1709. UNLOCK(lock);
  1710. -toobig:
  1711. - errno = ENOMEM;
  1712. - return 0;
  1713. + return p;
  1714. }
  1715. weak_alias(__simple_malloc, malloc);
  1716. +weak_alias(__simple_malloc, __malloc0);
  1717. --- a/src/malloc/malloc.c
  1718. +++ b/src/malloc/malloc.c
  1719. @@ -13,7 +13,6 @@
  1720. #define inline inline __attribute__((always_inline))
  1721. #endif
  1722. -uintptr_t __brk(uintptr_t);
  1723. void *__mmap(void *, size_t, int, int, int, off_t);
  1724. int __munmap(void *, size_t);
  1725. void *__mremap(void *, size_t, size_t, int, ...);
  1726. @@ -31,13 +30,9 @@ struct bin {
  1727. };
  1728. static struct {
  1729. - uintptr_t brk;
  1730. - size_t *heap;
  1731. volatile uint64_t binmap;
  1732. struct bin bins[64];
  1733. - volatile int brk_lock[2];
  1734. volatile int free_lock[2];
  1735. - unsigned mmap_step;
  1736. } mal;
  1737. @@ -152,69 +147,52 @@ void __dump_heap(int x)
  1738. }
  1739. #endif
  1740. +void *__expand_heap(size_t *);
  1741. +
  1742. static struct chunk *expand_heap(size_t n)
  1743. {
  1744. - static int init;
  1745. + static int heap_lock[2];
  1746. + static void *end;
  1747. + void *p;
  1748. struct chunk *w;
  1749. - uintptr_t new;
  1750. -
  1751. - lock(mal.brk_lock);
  1752. -
  1753. - if (!init) {
  1754. - mal.brk = __brk(0);
  1755. -#ifdef SHARED
  1756. - mal.brk = mal.brk + PAGE_SIZE-1 & -PAGE_SIZE;
  1757. -#endif
  1758. - mal.brk = mal.brk + 2*SIZE_ALIGN-1 & -SIZE_ALIGN;
  1759. - mal.heap = (void *)mal.brk;
  1760. - init = 1;
  1761. - }
  1762. -
  1763. - if (n > SIZE_MAX - mal.brk - 2*PAGE_SIZE) goto fail;
  1764. - new = mal.brk + n + SIZE_ALIGN + PAGE_SIZE - 1 & -PAGE_SIZE;
  1765. - n = new - mal.brk;
  1766. -
  1767. - if (__brk(new) != new) {
  1768. - size_t min = (size_t)PAGE_SIZE << mal.mmap_step/2;
  1769. - n += -n & PAGE_SIZE-1;
  1770. - if (n < min) n = min;
  1771. - void *area = __mmap(0, n, PROT_READ|PROT_WRITE,
  1772. - MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  1773. - if (area == MAP_FAILED) goto fail;
  1774. - mal.mmap_step++;
  1775. - area = (char *)area + SIZE_ALIGN - OVERHEAD;
  1776. - w = area;
  1777. + /* The argument n already accounts for the caller's chunk
  1778. + * overhead needs, but if the heap can't be extended in-place,
  1779. + * we need room for an extra zero-sized sentinel chunk. */
  1780. + n += SIZE_ALIGN;
  1781. +
  1782. + lock(heap_lock);
  1783. +
  1784. + p = __expand_heap(&n);
  1785. + if (!p) {
  1786. + unlock(heap_lock);
  1787. + return 0;
  1788. + }
  1789. +
  1790. + /* If not just expanding existing space, we need to make a
  1791. + * new sentinel chunk below the allocated space. */
  1792. + if (p != end) {
  1793. + /* Valid/safe because of the prologue increment. */
  1794. n -= SIZE_ALIGN;
  1795. + p = (char *)p + SIZE_ALIGN;
  1796. + w = MEM_TO_CHUNK(p);
  1797. w->psize = 0 | C_INUSE;
  1798. - w->csize = n | C_INUSE;
  1799. - w = NEXT_CHUNK(w);
  1800. - w->psize = n | C_INUSE;
  1801. - w->csize = 0 | C_INUSE;
  1802. -
  1803. - unlock(mal.brk_lock);
  1804. -
  1805. - return area;
  1806. }
  1807. - w = MEM_TO_CHUNK(mal.heap);
  1808. - w->psize = 0 | C_INUSE;
  1809. -
  1810. - w = MEM_TO_CHUNK(new);
  1811. + /* Record new heap end and fill in footer. */
  1812. + end = (char *)p + n;
  1813. + w = MEM_TO_CHUNK(end);
  1814. w->psize = n | C_INUSE;
  1815. w->csize = 0 | C_INUSE;
  1816. - w = MEM_TO_CHUNK(mal.brk);
  1817. + /* Fill in header, which may be new or may be replacing a
  1818. + * zero-size sentinel header at the old end-of-heap. */
  1819. + w = MEM_TO_CHUNK(p);
  1820. w->csize = n | C_INUSE;
  1821. - mal.brk = new;
  1822. -
  1823. - unlock(mal.brk_lock);
  1824. +
  1825. + unlock(heap_lock);
  1826. return w;
  1827. -fail:
  1828. - unlock(mal.brk_lock);
  1829. - errno = ENOMEM;
  1830. - return 0;
  1831. }
  1832. static int adjust_size(size_t *n)
  1833. @@ -378,6 +356,17 @@ void *malloc(size_t n)
  1834. return CHUNK_TO_MEM(c);
  1835. }
  1836. +void *__malloc0(size_t n)
  1837. +{
  1838. + void *p = malloc(n);
  1839. + if (p && !IS_MMAPPED(MEM_TO_CHUNK(p))) {
  1840. + size_t *z;
  1841. + n = (n + sizeof *z - 1)/sizeof *z;
  1842. + for (z=p; n; n--, z++) if (*z) *z=0;
  1843. + }
  1844. + return p;
  1845. +}
  1846. +
  1847. void *realloc(void *p, size_t n)
  1848. {
  1849. struct chunk *self, *next;
  1850. --- a/src/misc/syslog.c
  1851. +++ b/src/misc/syslog.c
  1852. @@ -48,12 +48,8 @@ void closelog(void)
  1853. static void __openlog()
  1854. {
  1855. - int fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
  1856. - if (fd < 0) return;
  1857. - if (connect(fd, (void *)&log_addr, sizeof log_addr) < 0)
  1858. - close(fd);
  1859. - else
  1860. - log_fd = fd;
  1861. + log_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
  1862. + if (log_fd >= 0) connect(log_fd, (void *)&log_addr, sizeof log_addr);
  1863. }
  1864. void openlog(const char *ident, int opt, int facility)
  1865. @@ -78,6 +74,11 @@ void openlog(const char *ident, int opt,
  1866. pthread_setcancelstate(cs, 0);
  1867. }
  1868. +static int is_lost_conn(int e)
  1869. +{
  1870. + return e==ECONNREFUSED || e==ECONNRESET || e==ENOTCONN || e==EPIPE;
  1871. +}
  1872. +
  1873. static void _vsyslog(int priority, const char *message, va_list ap)
  1874. {
  1875. char timebuf[16];
  1876. @@ -107,7 +108,10 @@ static void _vsyslog(int priority, const
  1877. if (l2 >= sizeof buf - l) l = sizeof buf - 1;
  1878. else l += l2;
  1879. if (buf[l-1] != '\n') buf[l++] = '\n';
  1880. - if (send(log_fd, buf, l, 0) < 0 && (log_opt & LOG_CONS)) {
  1881. + if (send(log_fd, buf, l, 0) < 0 && (!is_lost_conn(errno)
  1882. + || connect(log_fd, (void *)&log_addr, sizeof log_addr) < 0
  1883. + || send(log_fd, buf, l, 0) < 0)
  1884. + && (log_opt & LOG_CONS)) {
  1885. fd = open("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
  1886. if (fd >= 0) {
  1887. dprintf(fd, "%.*s", l-hlen, buf+hlen);
  1888. --- a/src/multibyte/btowc.c
  1889. +++ b/src/multibyte/btowc.c
  1890. @@ -1,7 +1,10 @@
  1891. #include <stdio.h>
  1892. #include <wchar.h>
  1893. +#include <stdlib.h>
  1894. +#include "internal.h"
  1895. wint_t btowc(int c)
  1896. {
  1897. - return c<128U ? c : EOF;
  1898. + int b = (unsigned char)c;
  1899. + return b<128U ? b : (MB_CUR_MAX==1 && c!=EOF) ? CODEUNIT(c) : WEOF;
  1900. }
  1901. --- a/src/multibyte/internal.h
  1902. +++ b/src/multibyte/internal.h
  1903. @@ -23,3 +23,10 @@ extern const uint32_t bittab[];
  1904. #define SA 0xc2u
  1905. #define SB 0xf4u
  1906. +
  1907. +/* Arbitrary encoding for representing code units instead of characters. */
  1908. +#define CODEUNIT(c) (0xdfff & (signed char)(c))
  1909. +#define IS_CODEUNIT(c) ((unsigned)(c)-0xdf80 < 0x80)
  1910. +
  1911. +/* Get inline definition of MB_CUR_MAX. */
  1912. +#include "locale_impl.h"
  1913. --- a/src/multibyte/mbrtowc.c
  1914. +++ b/src/multibyte/mbrtowc.c
  1915. @@ -4,6 +4,7 @@
  1916. * unnecessary.
  1917. */
  1918. +#include <stdlib.h>
  1919. #include <wchar.h>
  1920. #include <errno.h>
  1921. #include "internal.h"
  1922. @@ -27,6 +28,7 @@ size_t mbrtowc(wchar_t *restrict wc, con
  1923. if (!n) return -2;
  1924. if (!c) {
  1925. if (*s < 0x80) return !!(*wc = *s);
  1926. + if (MB_CUR_MAX==1) return (*wc = CODEUNIT(*s)), 1;
  1927. if (*s-SA > SB-SA) goto ilseq;
  1928. c = bittab[*s++-SA]; n--;
  1929. }
  1930. --- a/src/multibyte/mbsrtowcs.c
  1931. +++ b/src/multibyte/mbsrtowcs.c
  1932. @@ -7,6 +7,8 @@
  1933. #include <stdint.h>
  1934. #include <wchar.h>
  1935. #include <errno.h>
  1936. +#include <string.h>
  1937. +#include <stdlib.h>
  1938. #include "internal.h"
  1939. size_t mbsrtowcs(wchar_t *restrict ws, const char **restrict src, size_t wn, mbstate_t *restrict st)
  1940. @@ -24,6 +26,23 @@ size_t mbsrtowcs(wchar_t *restrict ws, c
  1941. }
  1942. }
  1943. + if (MB_CUR_MAX==1) {
  1944. + if (!ws) return strlen((const char *)s);
  1945. + for (;;) {
  1946. + if (!wn) {
  1947. + *src = (const void *)s;
  1948. + return wn0;
  1949. + }
  1950. + if (!*s) break;
  1951. + c = *s++;
  1952. + *ws++ = CODEUNIT(c);
  1953. + wn--;
  1954. + }
  1955. + *ws = 0;
  1956. + *src = 0;
  1957. + return wn0-wn;
  1958. + }
  1959. +
  1960. if (!ws) for (;;) {
  1961. if (*s-1u < 0x7f && (uintptr_t)s%4 == 0) {
  1962. while (!(( *(uint32_t*)s | *(uint32_t*)s-0x01010101) & 0x80808080)) {
  1963. --- a/src/multibyte/mbtowc.c
  1964. +++ b/src/multibyte/mbtowc.c
  1965. @@ -4,6 +4,7 @@
  1966. * unnecessary.
  1967. */
  1968. +#include <stdlib.h>
  1969. #include <wchar.h>
  1970. #include <errno.h>
  1971. #include "internal.h"
  1972. @@ -19,6 +20,7 @@ int mbtowc(wchar_t *restrict wc, const c
  1973. if (!wc) wc = &dummy;
  1974. if (*s < 0x80) return !!(*wc = *s);
  1975. + if (MB_CUR_MAX==1) return (*wc = CODEUNIT(*s)), 1;
  1976. if (*s-SA > SB-SA) goto ilseq;
  1977. c = bittab[*s++-SA];
  1978. --- a/src/multibyte/wcrtomb.c
  1979. +++ b/src/multibyte/wcrtomb.c
  1980. @@ -4,8 +4,10 @@
  1981. * unnecessary.
  1982. */
  1983. +#include <stdlib.h>
  1984. #include <wchar.h>
  1985. #include <errno.h>
  1986. +#include "internal.h"
  1987. size_t wcrtomb(char *restrict s, wchar_t wc, mbstate_t *restrict st)
  1988. {
  1989. @@ -13,6 +15,13 @@ size_t wcrtomb(char *restrict s, wchar_t
  1990. if ((unsigned)wc < 0x80) {
  1991. *s = wc;
  1992. return 1;
  1993. + } else if (MB_CUR_MAX == 1) {
  1994. + if (!IS_CODEUNIT(wc)) {
  1995. + errno = EILSEQ;
  1996. + return -1;
  1997. + }
  1998. + *s = wc;
  1999. + return 1;
  2000. } else if ((unsigned)wc < 0x800) {
  2001. *s++ = 0xc0 | (wc>>6);
  2002. *s = 0x80 | (wc&0x3f);
  2003. --- a/src/multibyte/wctob.c
  2004. +++ b/src/multibyte/wctob.c
  2005. @@ -1,8 +1,10 @@
  2006. -#include <stdio.h>
  2007. #include <wchar.h>
  2008. +#include <stdlib.h>
  2009. +#include "internal.h"
  2010. int wctob(wint_t c)
  2011. {
  2012. if (c < 128U) return c;
  2013. + if (MB_CUR_MAX==1 && IS_CODEUNIT(c)) return (unsigned char)c;
  2014. return EOF;
  2015. }
  2016. --- a/src/network/ns_parse.c
  2017. +++ b/src/network/ns_parse.c
  2018. @@ -95,7 +95,7 @@ int ns_skiprr(const unsigned char *ptr,
  2019. p += r;
  2020. }
  2021. }
  2022. - return ptr - p;
  2023. + return p - ptr;
  2024. bad:
  2025. errno = EMSGSIZE;
  2026. return -1;
  2027. --- a/src/passwd/nscd_query.c
  2028. +++ b/src/passwd/nscd_query.c
  2029. @@ -32,6 +32,7 @@ FILE *__nscd_query(int32_t req, const ch
  2030. },
  2031. .msg_iovlen = 2
  2032. };
  2033. + int errno_save = errno;
  2034. *swap = 0;
  2035. retry:
  2036. @@ -50,11 +51,14 @@ retry:
  2037. return f;
  2038. if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
  2039. - /* If there isn't a running nscd we return -1 to indicate that
  2040. - * that is precisely what happened
  2041. - */
  2042. - if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT)
  2043. + /* If there isn't a running nscd we simulate a "not found"
  2044. + * result and the caller is responsible for calling
  2045. + * fclose on the (unconnected) socket. The value of
  2046. + * errno must be left unchanged in this case. */
  2047. + if (errno == EACCES || errno == ECONNREFUSED || errno == ENOENT) {
  2048. + errno = errno_save;
  2049. return f;
  2050. + }
  2051. goto error;
  2052. }
  2053. --- /dev/null
  2054. +++ b/src/process/sh/vfork.s
  2055. @@ -0,0 +1,23 @@
  2056. +.global __vfork
  2057. +.weak vfork
  2058. +.type __vfork,@function
  2059. +.type vfork,@function
  2060. +__vfork:
  2061. +vfork:
  2062. + mov #95, r3
  2063. + add r3, r3
  2064. +
  2065. + trapa #31
  2066. + or r0, r0
  2067. + or r0, r0
  2068. + or r0, r0
  2069. + or r0, r0
  2070. + or r0, r0
  2071. +
  2072. + mov r0, r4
  2073. + mov.l 1f, r0
  2074. +2: braf r0
  2075. + nop
  2076. + .align 2
  2077. + .hidden __syscall_ret
  2078. +1: .long __syscall_ret@PLT-(2b+4-.)
  2079. --- a/src/regex/fnmatch.c
  2080. +++ b/src/regex/fnmatch.c
  2081. @@ -18,6 +18,7 @@
  2082. #include <stdlib.h>
  2083. #include <wchar.h>
  2084. #include <wctype.h>
  2085. +#include "locale_impl.h"
  2086. #define END 0
  2087. #define UNMATCHABLE -2
  2088. @@ -229,7 +230,7 @@ static int fnmatch_internal(const char *
  2089. * On illegal sequences we may get it wrong, but in that case
  2090. * we necessarily have a matching failure anyway. */
  2091. for (s=endstr; s>str && tailcnt; tailcnt--) {
  2092. - if (s[-1] < 128U) s--;
  2093. + if (s[-1] < 128U || MB_CUR_MAX==1) s--;
  2094. else while ((unsigned char)*--s-0x80U<0x40 && s>str);
  2095. }
  2096. if (tailcnt) return FNM_NOMATCH;
  2097. --- a/src/signal/sh/restore.s
  2098. +++ b/src/signal/sh/restore.s
  2099. @@ -2,7 +2,7 @@
  2100. .type __restore, @function
  2101. __restore:
  2102. mov #119, r3 !__NR_sigreturn
  2103. - trapa #16
  2104. + trapa #31
  2105. or r0, r0
  2106. or r0, r0
  2107. @@ -15,7 +15,7 @@ __restore:
  2108. __restore_rt:
  2109. mov #100, r3 !__NR_rt_sigreturn
  2110. add #73, r3
  2111. - trapa #16
  2112. + trapa #31
  2113. or r0, r0
  2114. or r0, r0
  2115. --- a/src/stdio/__fdopen.c
  2116. +++ b/src/stdio/__fdopen.c
  2117. @@ -54,13 +54,7 @@ FILE *__fdopen(int fd, const char *mode)
  2118. if (!libc.threaded) f->lock = -1;
  2119. /* Add new FILE to open file list */
  2120. - OFLLOCK();
  2121. - f->next = libc.ofl_head;
  2122. - if (libc.ofl_head) libc.ofl_head->prev = f;
  2123. - libc.ofl_head = f;
  2124. - OFLUNLOCK();
  2125. -
  2126. - return f;
  2127. + return __ofl_add(f);
  2128. }
  2129. weak_alias(__fdopen, fdopen);
  2130. --- a/src/stdio/__stdio_exit.c
  2131. +++ b/src/stdio/__stdio_exit.c
  2132. @@ -16,8 +16,7 @@ static void close_file(FILE *f)
  2133. void __stdio_exit(void)
  2134. {
  2135. FILE *f;
  2136. - OFLLOCK();
  2137. - for (f=libc.ofl_head; f; f=f->next) close_file(f);
  2138. + for (f=*__ofl_lock(); f; f=f->next) close_file(f);
  2139. close_file(__stdin_used);
  2140. close_file(__stdout_used);
  2141. }
  2142. --- a/src/stdio/__stdio_read.c
  2143. +++ b/src/stdio/__stdio_read.c
  2144. @@ -1,12 +1,5 @@
  2145. #include "stdio_impl.h"
  2146. #include <sys/uio.h>
  2147. -#include <pthread.h>
  2148. -
  2149. -static void cleanup(void *p)
  2150. -{
  2151. - FILE *f = p;
  2152. - if (!f->lockcount) __unlockfile(f);
  2153. -}
  2154. size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
  2155. {
  2156. @@ -16,9 +9,7 @@ size_t __stdio_read(FILE *f, unsigned ch
  2157. };
  2158. ssize_t cnt;
  2159. - pthread_cleanup_push(cleanup, f);
  2160. - cnt = syscall_cp(SYS_readv, f->fd, iov, 2);
  2161. - pthread_cleanup_pop(0);
  2162. + cnt = syscall(SYS_readv, f->fd, iov, 2);
  2163. if (cnt <= 0) {
  2164. f->flags |= F_EOF ^ ((F_ERR^F_EOF) & cnt);
  2165. return cnt;
  2166. --- a/src/stdio/__stdio_write.c
  2167. +++ b/src/stdio/__stdio_write.c
  2168. @@ -1,12 +1,5 @@
  2169. #include "stdio_impl.h"
  2170. #include <sys/uio.h>
  2171. -#include <pthread.h>
  2172. -
  2173. -static void cleanup(void *p)
  2174. -{
  2175. - FILE *f = p;
  2176. - if (!f->lockcount) __unlockfile(f);
  2177. -}
  2178. size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
  2179. {
  2180. @@ -19,9 +12,7 @@ size_t __stdio_write(FILE *f, const unsi
  2181. int iovcnt = 2;
  2182. ssize_t cnt;
  2183. for (;;) {
  2184. - pthread_cleanup_push(cleanup, f);
  2185. - cnt = syscall_cp(SYS_writev, f->fd, iov, iovcnt);
  2186. - pthread_cleanup_pop(0);
  2187. + cnt = syscall(SYS_writev, f->fd, iov, iovcnt);
  2188. if (cnt == rem) {
  2189. f->wend = f->buf + f->buf_size;
  2190. f->wpos = f->wbase = f->buf;
  2191. @@ -34,11 +25,8 @@ size_t __stdio_write(FILE *f, const unsi
  2192. }
  2193. rem -= cnt;
  2194. if (cnt > iov[0].iov_len) {
  2195. - f->wpos = f->wbase = f->buf;
  2196. cnt -= iov[0].iov_len;
  2197. iov++; iovcnt--;
  2198. - } else if (iovcnt == 2) {
  2199. - f->wbase += cnt;
  2200. }
  2201. iov[0].iov_base = (char *)iov[0].iov_base + cnt;
  2202. iov[0].iov_len -= cnt;
  2203. --- a/src/stdio/fclose.c
  2204. +++ b/src/stdio/fclose.c
  2205. @@ -14,11 +14,11 @@ int fclose(FILE *f)
  2206. __unlist_locked_file(f);
  2207. if (!(perm = f->flags & F_PERM)) {
  2208. - OFLLOCK();
  2209. + FILE **head = __ofl_lock();
  2210. if (f->prev) f->prev->next = f->next;
  2211. if (f->next) f->next->prev = f->prev;
  2212. - if (libc.ofl_head == f) libc.ofl_head = f->next;
  2213. - OFLUNLOCK();
  2214. + if (*head == f) *head = f->next;
  2215. + __ofl_unlock();
  2216. }
  2217. r = fflush(f);
  2218. --- a/src/stdio/fflush.c
  2219. +++ b/src/stdio/fflush.c
  2220. @@ -35,13 +35,12 @@ int fflush(FILE *f)
  2221. r = __stdout_used ? fflush(__stdout_used) : 0;
  2222. - OFLLOCK();
  2223. - for (f=libc.ofl_head; f; f=f->next) {
  2224. + for (f=*__ofl_lock(); f; f=f->next) {
  2225. FLOCK(f);
  2226. if (f->wpos > f->wbase) r |= __fflush_unlocked(f);
  2227. FUNLOCK(f);
  2228. }
  2229. - OFLUNLOCK();
  2230. + __ofl_unlock();
  2231. return r;
  2232. }
  2233. --- a/src/stdio/fgetwc.c
  2234. +++ b/src/stdio/fgetwc.c
  2235. @@ -1,8 +1,9 @@
  2236. #include "stdio_impl.h"
  2237. +#include "locale_impl.h"
  2238. #include <wchar.h>
  2239. #include <errno.h>
  2240. -wint_t __fgetwc_unlocked(FILE *f)
  2241. +static wint_t __fgetwc_unlocked_internal(FILE *f)
  2242. {
  2243. mbstate_t st = { 0 };
  2244. wchar_t wc;
  2245. @@ -10,8 +11,6 @@ wint_t __fgetwc_unlocked(FILE *f)
  2246. unsigned char b;
  2247. size_t l;
  2248. - f->mode |= f->mode+1;
  2249. -
  2250. /* Convert character from buffer if possible */
  2251. if (f->rpos < f->rend) {
  2252. l = mbrtowc(&wc, (void *)f->rpos, f->rend - f->rpos, &st);
  2253. @@ -39,6 +38,16 @@ wint_t __fgetwc_unlocked(FILE *f)
  2254. return wc;
  2255. }
  2256. +wint_t __fgetwc_unlocked(FILE *f)
  2257. +{
  2258. + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
  2259. + if (f->mode <= 0) fwide(f, 1);
  2260. + *ploc = f->locale;
  2261. + wchar_t wc = __fgetwc_unlocked_internal(f);
  2262. + *ploc = loc;
  2263. + return wc;
  2264. +}
  2265. +
  2266. wint_t fgetwc(FILE *f)
  2267. {
  2268. wint_t c;
  2269. --- a/src/stdio/fmemopen.c
  2270. +++ b/src/stdio/fmemopen.c
  2271. @@ -110,11 +110,5 @@ FILE *fmemopen(void *restrict buf, size_
  2272. if (!libc.threaded) f->lock = -1;
  2273. - OFLLOCK();
  2274. - f->next = libc.ofl_head;
  2275. - if (libc.ofl_head) libc.ofl_head->prev = f;
  2276. - libc.ofl_head = f;
  2277. - OFLUNLOCK();
  2278. -
  2279. - return f;
  2280. + return __ofl_add(f);
  2281. }
  2282. --- a/src/stdio/fopen.c
  2283. +++ b/src/stdio/fopen.c
  2284. @@ -18,7 +18,7 @@ FILE *fopen(const char *restrict filenam
  2285. /* Compute the flags to pass to open() */
  2286. flags = __fmodeflags(mode);
  2287. - fd = sys_open_cp(filename, flags, 0666);
  2288. + fd = sys_open(filename, flags, 0666);
  2289. if (fd < 0) return 0;
  2290. if (flags & O_CLOEXEC)
  2291. __syscall(SYS_fcntl, fd, F_SETFD, FD_CLOEXEC);
  2292. --- a/src/stdio/fputwc.c
  2293. +++ b/src/stdio/fputwc.c
  2294. @@ -1,4 +1,5 @@
  2295. #include "stdio_impl.h"
  2296. +#include "locale_impl.h"
  2297. #include <wchar.h>
  2298. #include <limits.h>
  2299. #include <ctype.h>
  2300. @@ -7,8 +8,10 @@ wint_t __fputwc_unlocked(wchar_t c, FILE
  2301. {
  2302. char mbc[MB_LEN_MAX];
  2303. int l;
  2304. + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
  2305. - f->mode |= f->mode+1;
  2306. + if (f->mode <= 0) fwide(f, 1);
  2307. + *ploc = f->locale;
  2308. if (isascii(c)) {
  2309. c = putc_unlocked(c, f);
  2310. @@ -20,6 +23,8 @@ wint_t __fputwc_unlocked(wchar_t c, FILE
  2311. l = wctomb(mbc, c);
  2312. if (l < 0 || __fwritex((void *)mbc, l, f) < l) c = WEOF;
  2313. }
  2314. + if (c==WEOF) f->flags |= F_ERR;
  2315. + *ploc = loc;
  2316. return c;
  2317. }
  2318. --- a/src/stdio/fputws.c
  2319. +++ b/src/stdio/fputws.c
  2320. @@ -1,23 +1,28 @@
  2321. #include "stdio_impl.h"
  2322. +#include "locale_impl.h"
  2323. #include <wchar.h>
  2324. int fputws(const wchar_t *restrict ws, FILE *restrict f)
  2325. {
  2326. unsigned char buf[BUFSIZ];
  2327. size_t l=0;
  2328. + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
  2329. FLOCK(f);
  2330. - f->mode |= f->mode+1;
  2331. + fwide(f, 1);
  2332. + *ploc = f->locale;
  2333. while (ws && (l = wcsrtombs((void *)buf, (void*)&ws, sizeof buf, 0))+1 > 1)
  2334. if (__fwritex(buf, l, f) < l) {
  2335. FUNLOCK(f);
  2336. + *ploc = loc;
  2337. return -1;
  2338. }
  2339. FUNLOCK(f);
  2340. + *ploc = loc;
  2341. return l; /* 0 or -1 */
  2342. }
  2343. --- a/src/stdio/fwide.c
  2344. +++ b/src/stdio/fwide.c
  2345. @@ -1,13 +1,14 @@
  2346. -#include <wchar.h>
  2347. #include "stdio_impl.h"
  2348. -
  2349. -#define SH (8*sizeof(int)-1)
  2350. -#define NORMALIZE(x) ((x)>>SH | -((-(x))>>SH))
  2351. +#include "locale_impl.h"
  2352. int fwide(FILE *f, int mode)
  2353. {
  2354. FLOCK(f);
  2355. - if (!f->mode) f->mode = NORMALIZE(mode);
  2356. + if (mode) {
  2357. + if (!f->locale) f->locale = MB_CUR_MAX==1
  2358. + ? C_LOCALE : UTF8_LOCALE;
  2359. + if (!f->mode) f->mode = mode>0 ? 1 : -1;
  2360. + }
  2361. mode = f->mode;
  2362. FUNLOCK(f);
  2363. return mode;
  2364. --- /dev/null
  2365. +++ b/src/stdio/ofl.c
  2366. @@ -0,0 +1,16 @@
  2367. +#include "stdio_impl.h"
  2368. +#include "libc.h"
  2369. +
  2370. +static FILE *ofl_head;
  2371. +static volatile int ofl_lock[2];
  2372. +
  2373. +FILE **__ofl_lock()
  2374. +{
  2375. + LOCK(ofl_lock);
  2376. + return &ofl_head;
  2377. +}
  2378. +
  2379. +void __ofl_unlock()
  2380. +{
  2381. + UNLOCK(ofl_lock);
  2382. +}
  2383. --- /dev/null
  2384. +++ b/src/stdio/ofl_add.c
  2385. @@ -0,0 +1,11 @@
  2386. +#include "stdio_impl.h"
  2387. +
  2388. +FILE *__ofl_add(FILE *f)
  2389. +{
  2390. + FILE **head = __ofl_lock();
  2391. + f->next = *head;
  2392. + if (*head) (*head)->prev = f;
  2393. + *head = f;
  2394. + __ofl_unlock();
  2395. + return f;
  2396. +}
  2397. --- a/src/stdio/open_memstream.c
  2398. +++ b/src/stdio/open_memstream.c
  2399. @@ -79,11 +79,5 @@ FILE *open_memstream(char **bufp, size_t
  2400. if (!libc.threaded) f->lock = -1;
  2401. - OFLLOCK();
  2402. - f->next = libc.ofl_head;
  2403. - if (libc.ofl_head) libc.ofl_head->prev = f;
  2404. - libc.ofl_head = f;
  2405. - OFLUNLOCK();
  2406. -
  2407. - return f;
  2408. + return __ofl_add(f);
  2409. }
  2410. --- a/src/stdio/open_wmemstream.c
  2411. +++ b/src/stdio/open_wmemstream.c
  2412. @@ -81,11 +81,5 @@ FILE *open_wmemstream(wchar_t **bufp, si
  2413. if (!libc.threaded) f->lock = -1;
  2414. - OFLLOCK();
  2415. - f->next = libc.ofl_head;
  2416. - if (libc.ofl_head) libc.ofl_head->prev = f;
  2417. - libc.ofl_head = f;
  2418. - OFLUNLOCK();
  2419. -
  2420. - return f;
  2421. + return __ofl_add(f);
  2422. }
  2423. --- a/src/stdio/ungetwc.c
  2424. +++ b/src/stdio/ungetwc.c
  2425. @@ -1,4 +1,5 @@
  2426. #include "stdio_impl.h"
  2427. +#include "locale_impl.h"
  2428. #include <wchar.h>
  2429. #include <limits.h>
  2430. #include <ctype.h>
  2431. @@ -8,21 +9,19 @@ wint_t ungetwc(wint_t c, FILE *f)
  2432. {
  2433. unsigned char mbc[MB_LEN_MAX];
  2434. int l=1;
  2435. -
  2436. - if (c == WEOF) return c;
  2437. -
  2438. - /* Try conversion early so we can fail without locking if invalid */
  2439. - if (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)
  2440. - return WEOF;
  2441. + locale_t *ploc = &CURRENT_LOCALE, loc = *ploc;
  2442. FLOCK(f);
  2443. - f->mode |= f->mode+1;
  2444. + if (f->mode <= 0) fwide(f, 1);
  2445. + *ploc = f->locale;
  2446. if (!f->rpos) __toread(f);
  2447. - if (!f->rpos || f->rpos < f->buf - UNGET + l) {
  2448. + if (!f->rpos || f->rpos < f->buf - UNGET + l || c == WEOF ||
  2449. + (!isascii(c) && (l = wctomb((void *)mbc, c)) < 0)) {
  2450. FUNLOCK(f);
  2451. - return EOF;
  2452. + *ploc = loc;
  2453. + return WEOF;
  2454. }
  2455. if (isascii(c)) *--f->rpos = c;
  2456. @@ -31,5 +30,6 @@ wint_t ungetwc(wint_t c, FILE *f)
  2457. f->flags &= ~F_EOF;
  2458. FUNLOCK(f);
  2459. + *ploc = loc;
  2460. return c;
  2461. }
  2462. --- a/src/stdio/vfwprintf.c
  2463. +++ b/src/stdio/vfwprintf.c
  2464. @@ -293,7 +293,10 @@ static int wprintf_core(FILE *f, const w
  2465. if ((fl&LEFT_ADJ)) fprintf(f, "%.*s", w-p, "");
  2466. l=w;
  2467. continue;
  2468. + case 'm':
  2469. + arg.p = strerror(errno);
  2470. case 's':
  2471. + if (!arg.p) arg.p = "(null)";
  2472. bs = arg.p;
  2473. if (p<0) p = INT_MAX;
  2474. for (i=l=0; l<p && (i=mbtowc(&wc, bs, MB_LEN_MAX))>0; bs+=i, l++);
  2475. @@ -356,7 +359,7 @@ int vfwprintf(FILE *restrict f, const wc
  2476. }
  2477. FLOCK(f);
  2478. - f->mode |= f->mode+1;
  2479. + fwide(f, 1);
  2480. olderr = f->flags & F_ERR;
  2481. f->flags &= ~F_ERR;
  2482. ret = wprintf_core(f, fmt, &ap2, nl_arg, nl_type);
  2483. --- a/src/stdio/vfwscanf.c
  2484. +++ b/src/stdio/vfwscanf.c
  2485. @@ -104,7 +104,7 @@ int vfwscanf(FILE *restrict f, const wch
  2486. FLOCK(f);
  2487. - f->mode |= f->mode+1;
  2488. + fwide(f, 1);
  2489. for (p=fmt; *p; p++) {
  2490. --- a/src/string/strverscmp.c
  2491. +++ b/src/string/strverscmp.c
  2492. @@ -2,40 +2,33 @@
  2493. #include <ctype.h>
  2494. #include <string.h>
  2495. -int strverscmp(const char *l, const char *r)
  2496. +int strverscmp(const char *l0, const char *r0)
  2497. {
  2498. - int haszero=1;
  2499. - while (*l==*r) {
  2500. - if (!*l) return 0;
  2501. + const unsigned char *l = (const void *)l0;
  2502. + const unsigned char *r = (const void *)r0;
  2503. + size_t i, dp, j;
  2504. + int z = 1;
  2505. - if (*l=='0') {
  2506. - if (haszero==1) {
  2507. - haszero=0;
  2508. - }
  2509. - } else if (isdigit(*l)) {
  2510. - if (haszero==1) {
  2511. - haszero=2;
  2512. - }
  2513. - } else {
  2514. - haszero=1;
  2515. - }
  2516. - l++; r++;
  2517. + /* Find maximal matching prefix and track its maximal digit
  2518. + * suffix and whether those digits are all zeros. */
  2519. + for (dp=i=0; l[i]==r[i]; i++) {
  2520. + int c = l[i];
  2521. + if (!c) return 0;
  2522. + if (!isdigit(c)) dp=i+1, z=1;
  2523. + else if (c!='0') z=0;
  2524. }
  2525. - if (haszero==1 && (*l=='0' || *r=='0')) {
  2526. - haszero=0;
  2527. - }
  2528. - if ((isdigit(*l) && isdigit(*r) ) && haszero) {
  2529. - size_t lenl=0, lenr=0;
  2530. - while (isdigit(l[lenl]) ) lenl++;
  2531. - while (isdigit(r[lenr]) ) lenr++;
  2532. - if (lenl==lenr) {
  2533. - return (*l - *r);
  2534. - } else if (lenl>lenr) {
  2535. - return 1;
  2536. - } else {
  2537. - return -1;
  2538. - }
  2539. - } else {
  2540. - return (*l - *r);
  2541. +
  2542. + if (l[dp]!='0' && r[dp]!='0') {
  2543. + /* If we're not looking at a digit sequence that began
  2544. + * with a zero, longest digit string is greater. */
  2545. + for (j=i; isdigit(l[j]); j++)
  2546. + if (!isdigit(r[j])) return 1;
  2547. + if (isdigit(r[j])) return -1;
  2548. + } else if (z && dp<i && (isdigit(l[i]) || isdigit(r[i]))) {
  2549. + /* Otherwise, if common prefix of digit sequence is
  2550. + * all zeros, digits order less than non-digits. */
  2551. + return (unsigned char)(l[i]-'0') - (unsigned char)(r[i]-'0');
  2552. }
  2553. +
  2554. + return l[i] - r[i];
  2555. }
  2556. --- a/src/thread/__tls_get_addr.c
  2557. +++ b/src/thread/__tls_get_addr.c
  2558. @@ -8,9 +8,9 @@ void *__tls_get_addr(size_t *v)
  2559. __attribute__((__visibility__("hidden")))
  2560. void *__tls_get_new(size_t *);
  2561. if (v[0]<=(size_t)self->dtv[0])
  2562. - return (char *)self->dtv[v[0]]+v[1];
  2563. + return (char *)self->dtv[v[0]]+v[1]+DTP_OFFSET;
  2564. return __tls_get_new(v);
  2565. #else
  2566. - return (char *)self->dtv[1]+v[1];
  2567. + return (char *)self->dtv[1]+v[1]+DTP_OFFSET;
  2568. #endif
  2569. }
  2570. --- /dev/null
  2571. +++ b/src/thread/__unmapself.c
  2572. @@ -0,0 +1,29 @@
  2573. +#include "pthread_impl.h"
  2574. +#include "atomic.h"
  2575. +#include "syscall.h"
  2576. +/* cheat and reuse CRTJMP macro from dynlink code */
  2577. +#include "dynlink.h"
  2578. +
  2579. +static volatile int lock;
  2580. +static void *unmap_base;
  2581. +static size_t unmap_size;
  2582. +static char shared_stack[256];
  2583. +
  2584. +static void do_unmap()
  2585. +{
  2586. + __syscall(SYS_munmap, unmap_base, unmap_size);
  2587. + __syscall(SYS_exit);
  2588. +}
  2589. +
  2590. +void __unmapself(void *base, size_t size)
  2591. +{
  2592. + int tid=__pthread_self()->tid;
  2593. + char *stack = shared_stack + sizeof shared_stack;
  2594. + stack -= (uintptr_t)stack % 16;
  2595. + while (lock || a_cas(&lock, 0, tid))
  2596. + a_spin();
  2597. + __syscall(SYS_set_tid_address, &lock);
  2598. + unmap_base = base;
  2599. + unmap_size = size;
  2600. + CRTJMP(do_unmap, stack);
  2601. +}
  2602. --- a/src/thread/mips/__unmapself.s
  2603. +++ b/src/thread/mips/__unmapself.s
  2604. @@ -2,6 +2,7 @@
  2605. .global __unmapself
  2606. .type __unmapself,@function
  2607. __unmapself:
  2608. + move $sp, $25
  2609. li $2, 4091
  2610. syscall
  2611. li $4, 0
  2612. --- a/src/thread/pthread_create.c
  2613. +++ b/src/thread/pthread_create.c
  2614. @@ -191,8 +191,9 @@ int __pthread_create(pthread_t *restrict
  2615. if (!libc.can_do_threads) return ENOSYS;
  2616. self = __pthread_self();
  2617. if (!libc.threaded) {
  2618. - for (FILE *f=libc.ofl_head; f; f=f->next)
  2619. + for (FILE *f=*__ofl_lock(); f; f=f->next)
  2620. init_file_lock(f);
  2621. + __ofl_unlock();
  2622. init_file_lock(__stdin_used);
  2623. init_file_lock(__stdout_used);
  2624. init_file_lock(__stderr_used);
  2625. @@ -231,7 +232,8 @@ int __pthread_create(pthread_t *restrict
  2626. if (guard) {
  2627. map = __mmap(0, size, PROT_NONE, MAP_PRIVATE|MAP_ANON, -1, 0);
  2628. if (map == MAP_FAILED) goto fail;
  2629. - if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)) {
  2630. + if (__mprotect(map+guard, size-guard, PROT_READ|PROT_WRITE)
  2631. + && errno != ENOSYS) {
  2632. __munmap(map, size);
  2633. goto fail;
  2634. }
  2635. --- a/src/thread/sh/__set_thread_area.s
  2636. +++ b/src/thread/sh/__set_thread_area.s
  2637. @@ -1,6 +0,0 @@
  2638. -.global __set_thread_area
  2639. -.type __set_thread_area, @function
  2640. -__set_thread_area:
  2641. - ldc r4, gbr
  2642. - rts
  2643. - mov #0, r0
  2644. --- a/src/thread/sh/__unmapself.s
  2645. +++ b/src/thread/sh/__unmapself.s
  2646. @@ -1,9 +1,9 @@
  2647. .text
  2648. -.global __unmapself
  2649. -.type __unmapself, @function
  2650. -__unmapself:
  2651. +.global __unmapself_sh_mmu
  2652. +.type __unmapself_sh_mmu, @function
  2653. +__unmapself_sh_mmu:
  2654. mov #91, r3 ! SYS_munmap
  2655. - trapa #18
  2656. + trapa #31
  2657. or r0, r0
  2658. or r0, r0
  2659. @@ -13,7 +13,7 @@ __unmapself:
  2660. mov #1, r3 ! SYS_exit
  2661. mov #0, r4
  2662. - trapa #17
  2663. + trapa #31
  2664. or r0, r0
  2665. or r0, r0
  2666. --- a/src/thread/sh/clone.s
  2667. +++ b/src/thread/sh/clone.s
  2668. @@ -17,7 +17,7 @@ __clone:
  2669. mov.l @r15, r6 ! r6 = ptid
  2670. mov.l @(8,r15), r7 ! r7 = ctid
  2671. mov.l @(4,r15), r0 ! r0 = tls
  2672. - trapa #21
  2673. + trapa #31
  2674. or r0, r0
  2675. or r0, r0
  2676. @@ -38,7 +38,7 @@ __clone:
  2677. mov #1, r3 ! __NR_exit
  2678. mov r0, r4
  2679. - trapa #17
  2680. + trapa #31
  2681. or r0, r0
  2682. or r0, r0
  2683. --- a/src/thread/sh/syscall_cp.s
  2684. +++ b/src/thread/sh/syscall_cp.s
  2685. @@ -31,7 +31,7 @@ L1: .long __cancel@PLT-(1b-.)
  2686. mov.l @(4,r15), r7
  2687. mov.l @(8,r15), r0
  2688. mov.l @(12,r15), r1
  2689. - trapa #22
  2690. + trapa #31
  2691. __cp_end:
  2692. ! work around hardware bug
  2693. --- a/src/time/__tz.c
  2694. +++ b/src/time/__tz.c
  2695. @@ -125,7 +125,8 @@ static void do_tzset()
  2696. "/usr/share/zoneinfo/\0/share/zoneinfo/\0/etc/zoneinfo/\0";
  2697. s = getenv("TZ");
  2698. - if (!s || !*s) s = "/etc/localtime";
  2699. + if (!s) s = "/etc/localtime";
  2700. + if (!*s) s = __gmt;
  2701. if (old_tz && !strcmp(s, old_tz)) return;
  2702. --- a/src/unistd/sh/pipe.s
  2703. +++ b/src/unistd/sh/pipe.s
  2704. @@ -2,7 +2,7 @@
  2705. .type pipe, @function
  2706. pipe:
  2707. mov #42, r3
  2708. - trapa #17
  2709. + trapa #31
  2710. ! work around hardware bug
  2711. or r0, r0
  2712. --- /dev/null
  2713. +++ b/tools/ld.musl-clang.in
  2714. @@ -0,0 +1,51 @@
  2715. +#!/bin/sh
  2716. +cc="@CC@"
  2717. +libc_lib="@LIBDIR@"
  2718. +ldso="@LDSO@"
  2719. +cleared=
  2720. +shared=
  2721. +userlinkdir=
  2722. +userlink=
  2723. +
  2724. +for x ; do
  2725. + test "$cleared" || set -- ; cleared=1
  2726. +
  2727. + case "$x" in
  2728. + -L-user-start)
  2729. + userlinkdir=1
  2730. + ;;
  2731. + -L-user-end)
  2732. + userlinkdir=
  2733. + ;;
  2734. + -L*)
  2735. + test "$userlinkdir" && set -- "$@" "$x"
  2736. + ;;
  2737. + -l-user-start)
  2738. + userlink=1
  2739. + ;;
  2740. + -l-user-end)
  2741. + userlink=
  2742. + ;;
  2743. + crtbegin*.o|crtend*.o)
  2744. + set -- "$@" $($cc -print-file-name=$x)
  2745. + ;;
  2746. + -lgcc|-lgcc_eh)
  2747. + file=lib${x#-l}.a
  2748. + set -- "$@" $($cc -print-file-name=$file)
  2749. + ;;
  2750. + -l*)
  2751. + test "$userlink" && set -- "$@" "$x"
  2752. + ;;
  2753. + -shared)
  2754. + shared=1
  2755. + set -- "$@" -shared
  2756. + ;;
  2757. + -sysroot=*|--sysroot=*)
  2758. + ;;
  2759. + *)
  2760. + set -- "$@" "$x"
  2761. + ;;
  2762. + esac
  2763. +done
  2764. +
  2765. +exec $($cc -print-prog-name=ld) -nostdlib "$@" -lc -dynamic-linker "$ldso"
  2766. --- /dev/null
  2767. +++ b/tools/musl-clang.in
  2768. @@ -0,0 +1,35 @@
  2769. +#!/bin/sh
  2770. +cc="@CC@"
  2771. +libc="@PREFIX@"
  2772. +libc_inc="@INCDIR@"
  2773. +libc_lib="@LIBDIR@"
  2774. +thisdir="`cd "$(dirname "$0")"; pwd`"
  2775. +
  2776. +# prevent clang from running the linker (and erroring) on no input.
  2777. +sflags=
  2778. +eflags=
  2779. +for x ; do
  2780. + case "$x" in
  2781. + -l*) input=1 ;;
  2782. + *) input= ;;
  2783. + esac
  2784. + if test "$input" ; then
  2785. + sflags="-l-user-start"
  2786. + eflags="-l-user-end"
  2787. + break
  2788. + fi
  2789. +done
  2790. +
  2791. +exec $cc \
  2792. + -B"$thisdir" \
  2793. + -fuse-ld=musl-clang \
  2794. + -static-libgcc \
  2795. + -nostdinc \
  2796. + --sysroot "$libc" \
  2797. + -isystem "$libc_inc" \
  2798. + -L-user-start \
  2799. + $sflags \
  2800. + "$@" \
  2801. + $eflags \
  2802. + -L"$libc_lib" \
  2803. + -L-user-end