musl
4 years agoadd support for powerpc/powerpc64 unaligned relocations
Samuel Holland [Sun, 30 Jun 2019 12:39:20 +0000 (07:39 -0500)]
add support for powerpc/powerpc64 unaligned relocations

R_PPC_UADDR32 (R_PPC64_UADDR64) has the same meaning as R_PPC_ADDR32
(R_PPC64_ADDR64), except that its address need not be aligned. For
powerpc64, BFD ld(1) will automatically convert between ADDR<->UADDR
relocations when the address is/isn't at its native alignment. This
will happen if, for example, there is a pointer in a packed struct.

gold and lld do not currently generate R_PPC64_UADDR64, but pass
through misaligned R_PPC64_ADDR64 relocations from object files,
possibly relaxing them to misaligned R_PPC64_RELATIVE. In both cases
(relaxed or not) this violates the PSABI, which defines the relevant
field type as "a 64-bit field occupying 8 bytes, the alignment of
which is 8 bytes unless otherwise specified."

All three linkers violate the PSABI on 32-bit powerpc, where the only
difference is that the field is 32 bits wide, aligned to 4 bytes.

Currently musl fails to load executables linked by BFD ld containing
R_PPC64_UADDR64, with the error "unsupported relocation type 43".
This change provides compatibility with BFD ld on powerpc64, and any
static linker on either architecture that starts following the PSABI
more closely.

4 years agoldso: remove redundant runtime checks in static TLS logic
Rich Felker [Sun, 11 Aug 2019 15:57:38 +0000 (11:57 -0400)]
ldso: remove redundant runtime checks in static TLS logic

as a result of commit ffab43602b5900c86b7040abdda8ccf6cdec95f5,
static_tls_cnt is now valid during relocations at program startup, so
it's no longer necessary to condition the check against static_tls_cnt
on this being a runtime (dlopen) relocation.

4 years agoldso: fix calloc misuse allocating initial tls
Rich Felker [Sun, 11 Aug 2019 15:48:06 +0000 (11:48 -0400)]
ldso: fix calloc misuse allocating initial tls

this is analogous to commit 2f1f51ae7b2d78247568e7fdb8462f3c19e469a4,
and should have been caught at the same time since it was right next
to the code moved in that commit. between final stage 3 reloc_all and
the jump to the main program's entry point, it is not valid to call
any functions which may be interposed by the application; doing so
results in execution of application code before ctors have run, and on
fdpic archs, before the main program's fdpic self-fixups have taken
place, which will produce runaway wrong execution.

4 years agoadd secure_getenv function
Petr Vaněk [Tue, 28 May 2019 20:47:48 +0000 (22:47 +0200)]
add secure_getenv function

This function is a GNU extension introduced in glibc 2.17.

4 years agoin clock_getres, check for null pointer before storing result
Rich Felker [Thu, 8 Aug 2019 01:35:28 +0000 (21:35 -0400)]
in clock_getres, check for null pointer before storing result

POSIX allows a null pointer, in which case the function only checks
the validity of the clock id argument.

4 years agoremove spurious null check in clock_settime
Rich Felker [Thu, 8 Aug 2019 01:31:41 +0000 (21:31 -0400)]
remove spurious null check in clock_settime

at the point of this check, the pointer has already been dereferenced.
clock_settime is not defined for null pointer arguments.

4 years agofix regression in recvmmsg with no timeout
Rich Felker [Thu, 8 Aug 2019 01:28:37 +0000 (21:28 -0400)]
fix regression in recvmmsg with no timeout

somewhat analogous to commit d0b547dfb5f7678cab6bc39dd736ed6454357ca4,
but here the omission of the null timeout check was in the time64
syscall code path. this code is not yet used except on x32.

4 years agoadd non-stub implementation of catgets localization functions
Rich Felker [Thu, 8 Aug 2019 01:15:53 +0000 (21:15 -0400)]
add non-stub implementation of catgets localization functions

these accept the netbsd/openbsd message catalog file format,
consisting of a sorted list of set headers and a sorted list of
message headers for each set, admitting trivial binary search for
lookups.

the gnu format was not chosen because it's unusably bad. it does not
admit efficient (log time or better) lookups; rather, it requires
linear search or hash table lookups, and the hash function is awful:
it's literally set_id*msg_id.

4 years agofix regression in select with no timeout
Rich Felker [Wed, 7 Aug 2019 06:57:53 +0000 (02:57 -0400)]
fix regression in select with no timeout

commit 722a1ae3351a03ab25010dbebd492eced664853b inadvertently passed a
copy of {s,us} to the syscall even if the timeout argument tv was
null, thereby causing immediate timeout (polling) in place of
unlimited timeout. only archs using SYS_select were affected.

4 years agofix failure of glob to match broken symlinks under some conditions
Rich Felker [Thu, 11 Jul 2019 20:55:17 +0000 (16:55 -0400)]
fix failure of glob to match broken symlinks under some conditions

when the pattern ended with one or more literal path components, or
when the GLOB_MARK flag was passed to request that glob flag directory
results and the type obtained by readdir was unknown or inconclusive
(symlink), the stat function was called to evaluate existence and/or
determine type. however, stat fails with ENOENT for broken symlinks,
and this caused the match to be omitted from the results.

instead, use stat only for the unknown/inconclusive cases with
GLOB_MARK, and otherwise, or if stat fails, use lstat existence still
needs to be determined. this minimizes the number of costly syscalls,
performing both only in the case where GLOB_MARK is in use and there
is a final literal path component which is a broken symlink.

based on/simplified from patch by James Y Knight.

4 years agoremove riscv64 bits/user.h contents
Rich Felker [Tue, 6 Aug 2019 16:50:38 +0000 (12:50 -0400)]
remove riscv64 bits/user.h contents

the contents conflicted with asm/ptrace.h. glibc does not provide
anything in user.h for riscv, so software cannot be depending on it.

simplified from patch submitted by Baruch Siach.

4 years agofix risc64 conflict with kernel headers
Baruch Siach [Tue, 6 Aug 2019 05:51:13 +0000 (08:51 +0300)]
fix risc64 conflict with kernel headers

Rename user registers struct definitions to avoid conflict with the
asm/ptrace.h kernel header that defines the same structs. Use the
__riscv_mc prefix as glibc does.

4 years agoin arm cancellation point asm, don't unnecessarily preserve link register
Patrick Oppenlander [Thu, 1 Aug 2019 04:34:59 +0000 (14:34 +1000)]
in arm cancellation point asm, don't unnecessarily preserve link register

The only reason we needed to preserve the link register was because we
were using a branch-link instruction to branch to __cp_cancel.
Replacing this with a branch means we can avoid the save/restore as
the link register is no longer modified.

4 years agoglob: implement GLOB_TILDE and GLOB_TILDE_CHECK
Ismael Luceno [Thu, 25 Jul 2019 21:50:48 +0000 (23:50 +0200)]
glob: implement GLOB_TILDE and GLOB_TILDE_CHECK

4 years agouse setitimer function rather than syscall to implement alarm
Rich Felker [Mon, 5 Aug 2019 23:55:42 +0000 (19:55 -0400)]
use setitimer function rather than syscall to implement alarm

otherwise alarm will break on 32-bit archs when time_t is changed to
64-bit. a second itimerval object is introduced for retrieving the old
value, since the setitimer function has restrict-qualified arguments.

4 years agofix build regression in i386 asm for atan2, atan2f
Rich Felker [Mon, 5 Aug 2019 23:57:07 +0000 (19:57 -0400)]
fix build regression in i386 asm for atan2, atan2f

commit f3ed8bfe8a82af1870ddc8696ed4cc1d5aa6b441 inadvertently removed
labels that were still needed.

4 years agofix x87 stack imbalance in corner cases of i386 math asm
Rich Felker [Mon, 5 Aug 2019 22:41:47 +0000 (18:41 -0400)]
fix x87 stack imbalance in corner cases of i386 math asm

commit 31c5fb80b9eae86f801be4f46025bc6532a554c5 introduced underflow
code paths for the i386 math asm, along with checks on the fpu status
word to skip the underflow-generation instructions if the underflow
flag was already raised. unfortunately, at least one such path, in
log1p, returned with 2 items on the x87 stack rather than just 1 item
for the return value. this is a violation of the ABI's calling
convention, and could cause subsequent floating point code to produce
NANs due to x87 stack overflow. if floating point results are used in
flow control, this can lead to runaway wrong code execution.

rather than reviewing each "underflow already raised" code path for
correctness, remove them all. they're likely slower than just
performing the underflow code unconditionally, and significantly more
complex.

all of this code should be ripped out and replaced by C source files
with inline asm. doing so would preclude this kind of error by having
the compiler perform all x87 stack register allocation and stack
manipulation, and would produce comparable or better code. however
such a change is a much larger project.

4 years agofix regression in clock_gettime on 32-bit archs without vdso
Rich Felker [Mon, 5 Aug 2019 16:01:13 +0000 (12:01 -0400)]
fix regression in clock_gettime on 32-bit archs without vdso

commit 72f50245d018af0c31b38dec83c557a4e5dd1ea8 broke this by creating
a code path where r is uninitialized.

4 years agoupdate riscv64 syscall numbers to linux v5.1
Rich Felker [Sat, 3 Aug 2019 22:43:36 +0000 (18:43 -0400)]
update riscv64 syscall numbers to linux v5.1

commit f3f96f2daa4d00f0e38489fb465cd0244b531abe added these for the
rest of the archs, but the patch it corresponded to missed riscv64
since riscv64 was not yet upstream at the time. this caused commit
dfc81828f7ab41da08f744c44117a1bb20a05749 to break riscv64 build, due
to a wrong assumption that SYS_statx was unconditionally defined.

4 years agoclock_gettime: add support for 32-bit vdso with 64-bit time_t
Rich Felker [Fri, 2 Aug 2019 18:04:45 +0000 (14:04 -0400)]
clock_gettime: add support for 32-bit vdso with 64-bit time_t

this fixes a major upcoming performance regression introduced by
commit 72f50245d018af0c31b38dec83c557a4e5dd1ea8, whereby 32-bit archs
would lose vdso clock_gettime after switching to 64-bit time_t, unless
the kernel supports time64 and provides a time64 version of the vdso
function. this would incur not just one but two syscalls: first, the
failed time64 syscall, then the fallback time32 one.

overflow of the 32-bit result is detected and triggers a revert to
syscalls. normally, on a system that's not Y2038-ready, this would
still overflow, but if the process has been migrated to a
time64-capable kernel or if the kernel has been hot-patched to add
time64 syscalls, it may conceivably work.

4 years agomove IPC_STAT definition to a new bits/ipcstat.h file
Rich Felker [Thu, 1 Aug 2019 00:35:37 +0000 (20:35 -0400)]
move IPC_STAT definition to a new bits/ipcstat.h file

otherwise, 32-bit archs that could otherwise share the generic
bits/ipc.h would need to duplicate the struct ipc_perm definition,
obscuring the fact that it's the same. sysvipc is not widely used and
these headers are not commonly included, so there is no performance
gain to be had by limiting the number of indirectly included files
here.

files with the existing time32 definition of IPC_STAT are added to all
current 32-bit archs now, so that when it's changed the change will
show up as a change rather than addition of a new file where it's less
obvious that the value is changing vs the generic one that was used
before.

4 years agofix missing declarations for pthread_join extensions in source file
Rich Felker [Wed, 31 Jul 2019 21:18:21 +0000 (17:18 -0400)]
fix missing declarations for pthread_join extensions in source file

per policy, define the feature test macro to get declarations for the
pthread_tryjoin_np and pthread_timedjoin_np functions. in the past
this has been only for checking; with 32-bit archs getting 64-bit
time_t it will also be necessary for symbols to get redirected
correctly.

4 years agoallow archs to define IPC_STAT, propagate time64 bit to other macros
Rich Felker [Wed, 31 Jul 2019 05:17:53 +0000 (01:17 -0400)]
allow archs to define IPC_STAT, propagate time64 bit to other macros

to make use of {sem,shm,msg}ctl IPC_STAT functionality to provide
64-bit time_t on 32-bit archs, IPC_STAT and related macros must be
defined with bit 8 (0x100) set. allow archs to define IPC_STAT in
bits/ipc.h, and define the other macros in terms of it so that they
all get the same value of the time64 bit.

4 years agoclock_gettime: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Wed, 31 Jul 2019 04:26:16 +0000 (00:26 -0400)]
clock_gettime: add time64 syscall support, decouple 32-bit time_t

the time64 syscall has to be used if time_t is 64-bit, since there's
no way of knowing before making a syscall whether the result will fit
in 32 bits, and the 32-bit syscalls do not report overflow as an
error.

on 64-bit archs, there is no change to the code after preprocessing.
on current 32-bit archs, the result is now read from the kernel
through long[2] array, then copied into the timespec, to remove the
assumption that time_t is the same as long.

vdso clock_gettime is still used in place of a syscall if available.
32-bit archs with 64-bit time_t must use the time64 version of the
vdso function; if it's not available, performance will significantly
suffer. support for both vdso functions could be added, but would
break the ability to move a long-lived process from a pre-time64
kernel to one that can outlast Y2038 with checkpoint/resume, at least
without added hacks to identify that the 32-bit function is no longer
usable and stop using it (e.g. by seeing negative tv_sec). this
possibility may be explored in future work on the function.

4 years agoclock_adjtime: add time64 support, decouple 32-bit time_t, fix x32
Rich Felker [Wed, 31 Jul 2019 03:51:45 +0000 (23:51 -0400)]
clock_adjtime: add time64 support, decouple 32-bit time_t, fix x32

the 64-bit/time64 version of the syscall is not API-compatible with
the userspace timex structure definition; fields specified as long
have type long long. so when using the time64 syscall, we have to
convert the entire structure. this was always the case for x32 as
well, but went unnoticed, meaning that clock_adjtime just passed junk
to the kernel on x32. it should be fixed now.

for the fallback case, we avoid encoding any assumptions about the new
location of the time member or naming of the legacy slots by accessing
them through a union of the kernel type and the new userspace type.
the only assumption is that the non-time members live at the same
offsets as in the (non-time64, long-based) kernel timex struct. this
property saves us from having to convert the whole thing, and avoids a
lot of additional work in compat shims.

the new code is statically unreachable for now except on x32, where it
fixes major brokenness. it is permanently unreachable on 64-bit.

4 years agoioctl: add fallback for new time64 SIOCGSTAMP[NS]
Rich Felker [Wed, 31 Jul 2019 03:48:25 +0000 (23:48 -0400)]
ioctl: add fallback for new time64 SIOCGSTAMP[NS]

without this, the SIOCGSTAMP and SIOCGSTAMPNS ioctl commands, for
obtaining timestamps, would stop working on pre-5.1 kernels after
time_t is switched to 64-bit and their values are changed to the new
time64 versions.

new code is written such that it's statically unreachable on 64-bit
archs, and on existing 32-bit archs until the macro values are changed
to activate 64-bit time_t.

4 years agoget/setsockopt: add fallback for new time64 SO_RCVTIMEO/SO_SNDTIMEO
Rich Felker [Wed, 31 Jul 2019 02:11:39 +0000 (22:11 -0400)]
get/setsockopt: add fallback for new time64 SO_RCVTIMEO/SO_SNDTIMEO

without this, the SO_RCVTIMEO and SO_SNDTIMEO socket options would
stop working on pre-5.1 kernels after time_t is switched to 64-bit and
their values are changed to the new time64 versions.

new code is written such that it's statically unreachable on 64-bit
archs, and on existing 32-bit archs until the macro values are changed
to activate 64-bit time_t.

4 years agomake __socketcall analogous to __syscall, error-returning
Rich Felker [Tue, 30 Jul 2019 21:51:16 +0000 (17:51 -0400)]
make __socketcall analogous to __syscall, error-returning

the __socketcall and __socketcall_cp macros are remnants from a really
old version of the syscall-mechanism infrastructure, and don't follow
the pattern that the "__" version of the macro returns the raw negated
error number rather than setting errno and returning -1.

for time64 purposes, some socket syscalls will need to operate on the
error value rather than returning immediately, so fix this up so they
can use it.

4 years agosysvipc: overhaul {sem,shm,msg}ctl for time64
Rich Felker [Tue, 30 Jul 2019 18:51:53 +0000 (14:51 -0400)]
sysvipc: overhaul {sem,shm,msg}ctl for time64

being "ctl" functions that take command numbers, these will be handled
like ioctl/sockopt/etc., using new command numbers for the time64
variants with an "IPC_TIME64" bit added to their values. to obtain
such a reserved bit, we reuse the IPC_64 bit, 0x100, which served only
as part of the libc-to-kernel interface, not as a public interface of
the libc functions.

using new command numbers avoids the need for compat shims (in ABIs
doing time64 through symbol redirection and compat shims) and, by
virtue of having a fixed time64 bit for all commands, we can ensure
that libc can perform the appropriate translations, even if the
application is using new commands from a newer version of the libc
headers than the libc available at runtime.

for the vast majority of 32-bit archs, the kernel {sem,shm,msq}id64_ds
definitions left padding space intended for expanding their time_t
fields to 64 bits in-place, and it would have been really nice to be
able to do time64 support that way. however the padding was almost
always in little-endian order (except on powerpc, and for msqid_ds
only on mips, where it matched the arch's byte order), and more
importantly, the alignment was overlooked. in semid_ds and msqid_ds,
the time_t members were not suitably aligned to be expanded to 64-bit,
due to the ipc_perm header consisting of 9 32-bit words -- except on
powerpc where ipc_perm contains an extra padding word. in shmid_ds,
the time_t members were suitably aligned, except that mips
(accidentally?) omitted the padding for them alltogether.

as a result, we're stuck with adding new time_t fields on the end of
the structures, and assembling the 32-bit lo/hi parts (or 16-bit hi
parts, for mips shmid_ds, which lacked sufficient reserved space for
full 32-bit hi parts) to fill them in.

all of the functional changes here are conditional on the IPC_TIME64
macro having a nonzero definition, which will only happen when
IPC_STAT is redefined for 32-bit archs, and on time_t being larger
than long, so for now the new code is all dead code.

4 years agofix semctl with SEM_STAT_ANY
Rich Felker [Wed, 31 Jul 2019 21:25:40 +0000 (17:25 -0400)]
fix semctl with SEM_STAT_ANY

due to the variadic signature, semctl needs to be made aware of any
new commands that take arguments. this was overlooked when commit
af55070eae5438476f921d827b7ae49e8141c3fe added SEM_STAT_ANY.

4 years agoremove gratuitously-different arch-specific bits/ipc.h files
Rich Felker [Tue, 30 Jul 2019 18:16:12 +0000 (14:16 -0400)]
remove gratuitously-different arch-specific bits/ipc.h files

these differ from generic only in using endian-matched padding with a
short __ipc_perm_seq field in place of the int field in generic. this
is not a documented public interface anyway, and the original intent
was to use int here. some ports just inadvertently slipped in the
kernel short+padding form.

4 years agoremove arch-specific bits/ipc.h that are identical to generic
Rich Felker [Tue, 30 Jul 2019 18:12:58 +0000 (14:12 -0400)]
remove arch-specific bits/ipc.h that are identical to generic

previously these differed from generic because they needed their own
definitions of IPC_64. now that it's no longer in public header,
they're identical.

4 years agomove IPC_64 from public bits/ipc.h to syscall_arch.h
Rich Felker [Tue, 30 Jul 2019 17:07:55 +0000 (13:07 -0400)]
move IPC_64 from public bits/ipc.h to syscall_arch.h

the definition of the IPC_64 macro controls the interface between libc
and the kernel through syscalls; it's not a public API. the meaning is
rather obscure. long ago, Linux's sysvipc *id_ds structures used
16-bit uids/gids and wrong types for a few other fields. this was in
the libc5 era, before glibc. the IPC_64 flag (64 is a misnomer; it's
more like 32) tells the kernel to use the modern[-ish] versions of the
structures.

the definition of IPC_64 has nothing to do with whether the arch is
32- or 64-bit. rather, due to either historical accident or
intentional obnoxiousness, the kernel only accepts and masks off the
0x100 IPC_64 flag conditional on CONFIG_ARCH_WANT_IPC_PARSE_VERSION,
i.e. for archs that want to provide, or that accidentally provided,
both. for archs which don't define this option, no masking is
performed and commands with the 0x100 bit set will fail as invalid. so
ultimately, the definition is just a matter of matching an arbitrary
switch defined per-arch in the kernel.

4 years agoselect: overhaul for time64
Rich Felker [Tue, 30 Jul 2019 01:08:43 +0000 (21:08 -0400)]
select: overhaul for time64

major changes are made alongside adding time64 syscall support to
account for issues found during research. select historically accepts
non-normalized (tv_usec not restricted to less than 1000000) timeouts,
and the kernel normalizes them, but the normalization code is buggy
and subject to integer overflows. since normalization is needed anyway
when using SYS_pselect6 or SYS_pselect6_time64 as the backend, simply
do it up-front to eliminate both code path complexity and the
possibility of kernel bugs.

as a side effect, select no longer updates the caller's timeout
timeval with the remaining time. previously, archs that used
SYS_select updated it and archs that used SYS_pselect6 didn't. this
change may turn out to be controversial and may need revisiting, but
in any case the old behavior was not strictly conforming.

POSIX allows modification of the timeout "upon successful completion",
but the Linux syscall modifies it upon unsuccessful completion (EINTR)
as well (and presumably each time the syscall stops and restarts
before it's known whether completion will be successful). it's
possible that this language does not reflect the actual intent of the
standard, since other historical implementations probably behaved like
Linux, but that should be clarified if there's a desire to bring the
old behavior back. regardless, programs that are depending on this are
not correct and are already broken on some archs we support.

4 years agorecvmmsg: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Tue, 30 Jul 2019 01:03:01 +0000 (21:03 -0400)]
recvmmsg: add time64 syscall support, decouple 32-bit time_t

the time64 syscall is used only if the timeout does not fit in 32
bits. after preprocessing, the code is unchanged on 64-bit archs. for
32-bit archs, the timeout now goes through an intermediate copy,
meaning that the caller does not get back the updated timeout. this is
based on my reading of the documentation, which does not document the
updating as a contract you can rely on, and mentions that the whole
recvmmsg timeout mechanism is buggy and unlikely to be useful. if it
turns out that there's interest in making the remaining time
officially available to callers, such functionality could be added
back later.

4 years agosetitimer, getitimer: decouple time_t from long
Rich Felker [Tue, 30 Jul 2019 00:57:05 +0000 (20:57 -0400)]
setitimer, getitimer: decouple time_t from long

these functions have no new time64 syscall, so the existence of a
time64 syscall cannot be used as the condition for the new code.
instead, assume the syscall takes timevals as longs, which is true
everywhere but x32, and interface with the kernel through long[4]
objects.

rather than adding new hacks to special-case x32 here, just add
x32-specific source files since a trivial syscall wrapper suffices
there.

the new code paths added in this commit are statically unreachable on
all current archs, but will become reachable when 32-bit archs get
64-bit time_t.

4 years agoremove duplicates of new generic bits/msg.h
Rich Felker [Mon, 29 Jul 2019 22:27:17 +0000 (18:27 -0400)]
remove duplicates of new generic bits/msg.h

4 years agouse 64-bit msqid_ds layout in the generic version of bits/msg.h
Rich Felker [Mon, 29 Jul 2019 22:23:27 +0000 (18:23 -0400)]
use 64-bit msqid_ds layout in the generic version of bits/msg.h

this layout is more common already than the old generic, and should
become even more common in the future with new archs added and with
64-bit time_t on 32-bit archs.

4 years agoduplicate generic bits/msg.h for each arch using it, in prep to change
Rich Felker [Mon, 29 Jul 2019 22:22:33 +0000 (18:22 -0400)]
duplicate generic bits/msg.h for each arch using it, in prep to change

4 years agoremove duplicates of new generic bits/sem.h
Rich Felker [Mon, 29 Jul 2019 22:17:43 +0000 (18:17 -0400)]
remove duplicates of new generic bits/sem.h

some of these were not exact duplicates, but had gratuitously
different naming for padding, or omitted the endian checks because the
arch is fixed-endian.

4 years agouse 64-bit semid_ds layout in the generic version of bits/sem.h
Rich Felker [Mon, 29 Jul 2019 22:12:05 +0000 (18:12 -0400)]
use 64-bit semid_ds layout in the generic version of bits/sem.h

this layout is slightly less common than the old generic one, but only
because x86_64 and x32 wrongly (according to comments in the kernel
headers) copied the i386 padding. for future archs, and with 64-bit
time_t on 32-bit archs, the new layout here will become the most
common, and it makes sense to treat it as the generic.

4 years agocollapse out byte order conditions in bits/sem.h for fixed-endian archs
Rich Felker [Mon, 29 Jul 2019 22:09:55 +0000 (18:09 -0400)]
collapse out byte order conditions in bits/sem.h for fixed-endian archs

having preprocessor conditionals on byte order in the bits headers for
fixed-endian archs is confusing at best. remove them.

4 years agoduplicate generic bits/sem.h for each arch using it, in prep to change
Rich Felker [Mon, 29 Jul 2019 22:07:23 +0000 (18:07 -0400)]
duplicate generic bits/sem.h for each arch using it, in prep to change

4 years agoextricate bits/sem.h from x32 time_t hack
Rich Felker [Mon, 29 Jul 2019 22:03:41 +0000 (18:03 -0400)]
extricate bits/sem.h from x32 time_t hack

various padding fields in the generic bits/sem.h were defined in terms
of time_t as a cheap hack standing in for "kernel long", to allow x32
to use the generic version of the file. this was a really bad idea, as
it ended up getting copied into lots of arch-specific versions of the
bits file, and is a blocker to changing time_t to 64-bit on 32-bit
archs.

this commit adds an x32-specific version of the header, and changes
padding type back from time_t to long (currently the same type on all
archs but x32) in the generic header and all the others the hack got
copied into.

4 years agoremove trailing newlines from various versions of bits/shm.h
Rich Felker [Mon, 29 Jul 2019 19:54:38 +0000 (15:54 -0400)]
remove trailing newlines from various versions of bits/shm.h

4 years agoremove duplicates of new generic bits/shm.h
Rich Felker [Mon, 29 Jul 2019 19:52:20 +0000 (15:52 -0400)]
remove duplicates of new generic bits/shm.h

4 years agouse 64-bit shmid_ds layout in the generic version of bits/shm.h
Rich Felker [Mon, 29 Jul 2019 19:44:58 +0000 (15:44 -0400)]
use 64-bit shmid_ds layout in the generic version of bits/shm.h

this layout is more common already than the old generic, and should
become even more common in the future with new archs added and with
64-bit time_t on 32-bit archs.

the duplicate arch-specific copies are not removed yet in this commit,
so as to assist git tooling in copy/rename tracking.

4 years agoduplicate generic bits/shm.h for each arch using it, in prep to change
Rich Felker [Mon, 29 Jul 2019 19:39:24 +0000 (15:39 -0400)]
duplicate generic bits/shm.h for each arch using it, in prep to change

there are more archs sharing the generic 64-bit version of the struct,
which is uniform and much more reasonable, than sharing the current
"generic" one, and depending on how time64 sysvipc is done for 32-bit
archs, even more may be sharing the "64-bit version" in the future.

so, duplicate the current generic to all archs using it (arm, i386,
m68k, microblaze, or1k) so that the generic can be changed freely.

this is recorded as its own commit mainly as a hint to git tooling, to
assist in copy/move tracking.

4 years agotimerfd: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Mon, 29 Jul 2019 03:53:38 +0000 (23:53 -0400)]
timerfd: add time64 syscall support, decouple 32-bit time_t

the changes here are semantically and structurally identical to those
made to timer_settime and timer_gettime for time64 support.

4 years agosched_rr_get_interval: don't assume time_t is 32-bit on 32-bit archs
Rich Felker [Mon, 29 Jul 2019 03:26:38 +0000 (23:26 -0400)]
sched_rr_get_interval: don't assume time_t is 32-bit on 32-bit archs

as with clock_getres, the time64 syscall for this is not necessary or
useful, this time since scheduling timeslices are not on the order 68
years. if there's a 32-bit syscall, use it and expand the result into
timespec; otherwise there is only one syscall and it does the right
thing to store to timespec directly.

on 64-bit archs, there is no change to the code after preprocessing.

4 years agoclock_getres: don't assume time_t is 32-bit on 32-bit archs
Rich Felker [Mon, 29 Jul 2019 02:53:10 +0000 (22:53 -0400)]
clock_getres: don't assume time_t is 32-bit on 32-bit archs

the time64 syscall for this is not necessary or useful, since clock
resolution is generally better than 68-year granularity. if there's a
32-bit syscall, use it and expand the result into timespec; otherwise
there is only one syscall and it does the right thing to store to
timespec directly.

on 64-bit archs, there is no change to the code after preprocessing.

4 years agotimer_gettime: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Mon, 29 Jul 2019 02:46:19 +0000 (22:46 -0400)]
timer_gettime: add time64 syscall support, decouple 32-bit time_t

the time64 syscall has to be used if time_t is 64-bit, since there's
no way of knowing before making a syscall whether the result will fit
in 32 bits, and the 32-bit syscalls do not report overflow as an
error.

on 64-bit archs, there is no change to the code after preprocessing.
on current 32-bit archs, the result is now read from the kernel
through long[4] array, then copied into the timespec, to remove the
assumption that time_t is the same as long.

4 years agoremove x32 syscall timespec fixup hacks
Rich Felker [Sun, 28 Jul 2019 23:29:28 +0000 (19:29 -0400)]
remove x32 syscall timespec fixup hacks

the x32 syscall interfaces treat timespec's tv_nsec member as 64-bit
despite the API type being long and long being 32-bit in the ABI. this
is no problem for syscalls that store timespecs to userspace as
results, but caused uninitialized padding to be misinterpreted as the
high bits in syscalls that take timespecs as input.

since the beginning of the port, we've dealt with this situation with
hacks in syscall_arch.h, and injected between __syscall_cp_c and
__syscall_cp_asm, to special-case the syscall numbers that involve
timespecs as inputs and copy them to a form suitable to pass to the
kernel.

commit 40aa18d55ab763e69ad16d0cf1cebea708ffde47 set the stage for
removal of these hacks by letting us treat the "normal" x32 syscalls
dealing with timespec as if they're x32's "time64" syscalls,
effectively making x32 ax "time64-only 32-bit arch" like riscv32 will
be when it's added. since then, all users of syscalls that x32's
syscall_arch.h had hacks for have been updated to use time64 syscalls,
so the hacks can be removed.

there are still at least a few other timespec-related syscalls broken
on x32, which were overlooked when the x32 hacks were done or added
later. these include at least recvmmsg, adjtimex/clock_adjtime, and
timerfd_settime, and they will be fixed independently later on.

4 years agoutimensat: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Sun, 28 Jul 2019 22:51:20 +0000 (18:51 -0400)]
utimensat: add time64 syscall support, decouple 32-bit time_t

time64 syscall is used only if it's the only one defined for the arch,
or if either of the requested times does not fit in 32 bits. care is
taken to normalize the inputs to account for UTIME_NOW or UTIME_OMIT
in tv_nsec, in which case tv_sec should be ignored. this is needed not
only to avoid spurious time64 syscalls that might waste time failing
with ENOSYS, but also to accurately decide whether fallback is
possible.

if the requested time cannot be represented, the function fails with
ENOTSUP, defined in general as "The implementation does not support
the requested feature or value". neither the time64 syscall, nor this
error, can happen on current 32-bit archs where time_t is a 32-bit
type, and both are statically unreachable.

on 64-bit archs, there are only superficial changes to the
SYS_futimesat fallback path, which has been modified to pass long[4]
instead of struct timeval[2] to the kernel, making it suitable for use
on 32-bit archs even once time_t is changed to 64-bit. for 32-bit
archs, the call to SYS_utimensat has also been changed to copy the
timespecs through an array of long[4] rather than passing the
timespec[2] in-place.

4 years agoclock_settime: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Sun, 28 Jul 2019 22:43:51 +0000 (18:43 -0400)]
clock_settime: add time64 syscall support, decouple 32-bit time_t

time64 syscall is used only if it's the only one defined for the arch,
or if the requested time does not fit in 32 bits. on current 32-bit
archs where time_t is a 32-bit type, this makes it statically
unreachable.

if the time64 syscall is needed because the requested time does not
fit in 32 bits, we define this as an error ENOTSUP, for "The
implementation does not support the requested feature or value".

on 64-bit archs, there is no change to the code after preprocessing.
on current 32-bit archs, the time is moved through an intermediate
copy to remove the assumption that time_t is a 32-bit type.

4 years agotimer_settime: add support for time64 syscall, decouple 32-bit time_t
Rich Felker [Sun, 28 Jul 2019 22:38:28 +0000 (18:38 -0400)]
timer_settime: add support for time64 syscall, decouple 32-bit time_t

time64 syscall is used only if it's the only one defined for the arch,
if either component of the itimerspec does not fit in 32 bits, or if
time_t is 64-bit and the caller requested the old value, in which case
there's a possibility that the old value might not fit in 32 bits. on
current 32-bit archs where time_t is a 32-bit type, this makes it
statically unreachable.

on 64-bit archs, there is no change to the code after preprocessing.
on current 32-bit archs, the time is moved through an intermediate
copy to remove the assumption that time_t is a 32-bit type.

4 years agopselect, ppoll: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Sun, 28 Jul 2019 22:15:22 +0000 (18:15 -0400)]
pselect, ppoll: add time64 syscall support, decouple 32-bit time_t

time64 syscall is used only if it's the only one defined for the arch,
or if the requested timeout length does not fit in 32 bits. on current
32-bit archs where time_t is a 32-bit type, this makes it statically
unreachable.

on 64-bit archs, there are only superficial changes to the code after
preprocessing. both before and after these changes, these functions
copied their timeout arguments to avoid letting the kernel clobber the
caller's copies. now, the copying also serves to change the type from
userspace timespec to a pair of longs, which makes a difference only
in the 32-bit fallback case, not on 64-bit.

4 years agofutex wait operations: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Sun, 28 Jul 2019 21:44:51 +0000 (17:44 -0400)]
futex wait operations: add time64 syscall support, decouple 32-bit time_t

thanks to the original factorization using the __timedwait function,
there are no FUTEX_WAIT calls anywhere else, giving us a single point
of change to make nearly all the timed thread primitives time64-ready.
the one exception is the FUTEX_LOCK_PI command for PI mutex timedlock.
I haven't tried to make these two points share code, since they have
different fallbacks (no non-private fallback needed for PI since PI
was added later) and FUTEX_LOCK_PI isn't a cancellation point (thus
allowing the whole code path to inline into pthread_mutex_timedlock).

as for other changes in this series, the time64 syscall is used only
if it's the only one defined for the arch, or if the requested timeout
does not fit in 32 bits. on current 32-bit archs where time_t is a
32-bit type, this makes it statically unreachable.

on 64-bit archs, there are only superficial changes to the code after
preprocessing. on current 32-bit archs, the time is passed via an
intermediate copy to remove the assumption that time_t is a 32-bit
type.

4 years agosemtimedop: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Sun, 28 Jul 2019 21:28:23 +0000 (17:28 -0400)]
semtimedop: add time64 syscall support, decouple 32-bit time_t

time64 syscall is used only if it's the only one defined for the arch,
or if the requested timeout does not fit in 32 bits. on current 32-bit
archs where time_t is a 32-bit type, this makes it statically
unreachable.

on 64-bit archs, there is no change to the code after preprocessing.
on current 32-bit archs, the time is passed via an intermediate copy
to remove the assumption that time_t is a 32-bit type.

to avoid duplicating SYS_ipc/SYS_semtimedop choice logic, the code for
32-bit archs "falls through" after updating the timeout argument ts to
point to a [compound literal] array of longs. in preparation for
"time64-only" 32-bit archs, an extra case is added for neither SYS_ipc
nor the non-time64 SYS_semtimedop existing; the ENOSYS failure path
here should never be reachable, and is added just in case a compiler
can't see that it's not reachable, to avoid spurious static analysis
complaints.

4 years agosigtimedwait: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Sun, 28 Jul 2019 21:05:47 +0000 (17:05 -0400)]
sigtimedwait: add time64 syscall support, decouple 32-bit time_t

time64 syscall is used only if it's the only one defined for the arch,
or if the requested timeout length does not fit in 32 bits. on current
32-bit archs where time_t is a 32-bit type, this makes it statically
unreachable.

on 64-bit archs, there are only superficial changes to the code after
preprocessing. on current 32-bit archs, the timeout is passed via an
intermediate copy to remove the assumption that time_t is a 32-bit
type.

4 years agomq_timedsend, mq_timedreceive: add time64, decouple 32-bit time_t
Rich Felker [Sun, 28 Jul 2019 19:49:10 +0000 (15:49 -0400)]
mq_timedsend, mq_timedreceive: add time64, decouple 32-bit time_t

time64 syscall is used only if it's the only one defined for the arch,
or if the requested absolute timeout does not fit in 32 bits. on
current 32-bit archs where time_t is a 32-bit type, this makes it
statically unreachable.

on 64-bit archs, there is no change to the code after preprocessing.
on current 32-bit archs, the timeout is passed via an intermediate
copy to remove the assumption that time_t is a 32-bit type.

4 years agoclock_nanosleep: add time64 syscall support, decouple 32-bit time_t
Rich Felker [Sun, 28 Jul 2019 19:08:34 +0000 (15:08 -0400)]
clock_nanosleep: add time64 syscall support, decouple 32-bit time_t

time64 syscall is used only if it's the only one defined for the arch,
or if the requested time does not fit in 32 bits. on current 32-bit
archs where time_t is a 32-bit type, this makes it statically
unreachable.

on 64-bit archs, there is no change to the code after preprocessing.
on current 32-bit archs, the time is moved through an intermediate
copy to remove the assumption that time_t is a 32-bit type.

4 years agoimplement settimeofday in terms of clock_settime, not old syscall
Rich Felker [Sat, 27 Jul 2019 21:48:32 +0000 (17:48 -0400)]
implement settimeofday in terms of clock_settime, not old syscall

this is yet another place where special handling of time syscalls can
and should be avoided by implementing legacy functions in terms of
their modern replacements. in theory a fallback to SYS_settimeofday
could be added to clock_settime, but SYS_clock_settime has been
available since Linux 2.6.0 or earlier, i.e. all the way back to the
minimum supported version.

4 years agointernally, define plain syscalls, if missing, as their time64 variants
Rich Felker [Sat, 27 Jul 2019 17:29:26 +0000 (13:29 -0400)]
internally, define plain syscalls, if missing, as their time64 variants

this commit has no effect whatsoever right now, but is in preparation
for a future riscv32 port and other future 32-bit archs that will be
"time64-only" from the start on the kernel side.

together with the previous x32 changes, this commit ensures that
syscall call points that don't care about time (passing null timeouts,
etc.) can continue to do so without having to special-case time64-only
archs, and allows code using the time64 syscalls to uniformly test for
the need to fallback with SYS_foo != SYS_foo_time64, rather than
needing to check defined(SYS_foo) && SYS_foo != SYS_foo_time64.

4 years agointernally, define time64 syscalls on x32 as the existing syscalls
Rich Felker [Sat, 27 Jul 2019 16:39:29 +0000 (12:39 -0400)]
internally, define time64 syscalls on x32 as the existing syscalls

x32 is odd in that it's the only ILP32 arch/ABI we have where time_t
is 64-bit rather than (32-bit) long, and this has always been
problematic in that it results in struct timespec having unused
padding space, since tv_nsec has type long, which the kernel insists
be zero- or sign-extended (due to negative tv_nsec being invalid, it
doesn't matter which) to match the x86_64 type.

up til now, we've had really ugly hacks in x32/syscall_arch.h to patch
up the timespecs passed to the kernel. but the same requirement to
zero- or sign-extend tv_nsec also applies to all the new time64
syscalls on true 32-bit archs. so let's take advantage of this to
clean things up.

this patch defines all of the time64 syscalls for x32 as aliases for
the existing syscalls by the same name. this establishes the following
invariants:

- if the time64 form is defined, it takes time arguments as 64-bit
  objects, and tv_nsec inputs must be zero-/sign-extended to 64-bit.

- if the time64 form is not defined, or if the time64 form is defined
  and is not equal to the "plain" form, the plain form takes time
  arguments as longs.

this will avoid the need for protocols for archs to define appropriate
types for each family of syscalls, and for the reader of the code to
have to be aware of such type definitions.

in some sense it might be simpler if the plain syscall form were
undefined for x32, so that it would always take longs if defined.
however, a number of these syscalls are used in contexts with a null
time argument, or (e.g. futex) for commands that don't involve time at
all, and having to introduce time64-specific logic to all those call
points does not make sense. thus, while the "plain" forms are kept now
just because they're needed until the affected code is converted over,
they'll also almost surely be kept in the future as well.

4 years agodon't use futimesat syscall as utimensat fallback on x32
Rich Felker [Sat, 27 Jul 2019 16:20:07 +0000 (12:20 -0400)]
don't use futimesat syscall as utimensat fallback on x32

kernel support for x32 was added long after the utimensat syscall was
already available, so having a fallback is just wasted code size.

also, for changes related to time64 support on 32-bit archs, I want to
be able to assume the old futimesat syscall always works with longs,
which is true except for x32. by ensuring that it's not used on x32,
the needed invariant is established.

4 years agofix and simplify futimesat fallback in utimensat
Rich Felker [Sat, 27 Jul 2019 14:20:01 +0000 (10:20 -0400)]
fix and simplify futimesat fallback in utimensat

previously the fallback wrongly failed with EINVAL rather than ENOSYS
when UTIME_NOW was used with one component but not both. commit
dd5f50da6f6c3df5647e922e47f8568a8896a752 introduced this behavior when
initially adding the fallback support.

instead, detect the case where both are UTIME_NOW early and replace
with a null times pointer; this may improve performance slightly (less
copy from user), and removes the complex logic from the fallback case.
it also makes things slightly simpler for adding time64 code paths.

4 years agorefactor thrd_sleep and nanosleep in terms of clock_nanosleep
Rich Felker [Sun, 21 Jul 2019 05:53:14 +0000 (01:53 -0400)]
refactor thrd_sleep and nanosleep in terms of clock_nanosleep

for namespace-safety with thrd_sleep, this requires an alias, which is
also added. this eliminates all but one direct call point for
nanosleep syscalls, and arranges that 64-bit time_t conversion logic
will only need to exist in one file rather than three.

as a bonus, clock_nanosleep with CLOCK_REALTIME and empty flags is now
implemented as SYS_nanosleep, thereby working on older kernels that
may lack POSIX clocks functionality.

4 years agouse the correct stat structure in the fstat path
Samuel Holland [Sun, 21 Jul 2019 04:52:26 +0000 (23:52 -0500)]
use the correct stat structure in the fstat path

commit 01ae3fc6d48f4a45535189b7a6db286535af08ca modified fstatat to
translate the kernel's struct stat ("kstat") into the libc struct stat.
To do this, it created a local kstat object, and copied its contents
into the user-provided object.

However, the commit neglected to update the fstat compatibility path and
its fallbacks. They continued to pass the user-supplied object to the
kernel, later overwiting it with the uninitialized memory in the local
temporary.

4 years agorefactor adjtime function using adjtimex function instead of syscall
Rich Felker [Sat, 20 Jul 2019 21:23:40 +0000 (17:23 -0400)]
refactor adjtime function using adjtimex function instead of syscall

this removes the assumption that userspace struct timex matches the
syscall type and sets the stage for 64-bit time_t on 32-bit archs.

4 years agorefactor adjtimex in terms of clock_adjtime
Rich Felker [Sat, 20 Jul 2019 21:02:49 +0000 (17:02 -0400)]
refactor adjtimex in terms of clock_adjtime

this sets the stage for having the conversion logic for 64-bit time_t
all in one file, and as a bonus makes clock_adjtime for CLOCK_REALTIME
work even on kernels too old to have the clock_adjtime syscall.

4 years agofix inadvertent introduction of extern object stx
Rich Felker [Fri, 19 Jul 2019 04:39:02 +0000 (00:39 -0400)]
fix inadvertent introduction of extern object stx

commit dfc81828f7ab41da08f744c44117a1bb20a05749 accidentally defined
an instance of struct statx along with the struct declaration.

4 years agoimplement fstatat with SYS_statx, conditional on undersized kstat time
Rich Felker [Fri, 19 Jul 2019 01:46:33 +0000 (21:46 -0400)]
implement fstatat with SYS_statx, conditional on undersized kstat time

this commit adds a new backend for fstatat (and thereby the whole stat
family) using the SYS_statx syscall, but conditions the new code on
the kernel stat structure's time fields being smaller than time_t. in
principle that should make it all dead code at present, but mips64 has
a broken stat structure with 32-bit time fields despite having 64-bit
time_t elsewhere, so on mips64 it is a functional change that makes
post-Y2038 filesystem timestamps accessible.

whenever the 32-bit archs end up getting 64-bit time_t, regardless of
how that happens, the changes in this commit will automatically take
effect for them too.

4 years agocleanup includes now that stat, lstat no longer make direct syscalls
Rich Felker [Thu, 18 Jul 2019 23:44:20 +0000 (19:44 -0400)]
cleanup includes now that stat, lstat no longer make direct syscalls

4 years agorestore property that fstat(AT_FDCWD) fails with EBADF
Rich Felker [Thu, 18 Jul 2019 23:41:52 +0000 (19:41 -0400)]
restore property that fstat(AT_FDCWD) fails with EBADF

AT_FDCWD is not a valid file descriptor, so POSIX requires fstat to
fail with EBADF. if passed to fstatat, the call would spuriously
succeed and return results for the working directory.

4 years agoremove mips/n32/64 stat struct hacks from syscall machinery
Rich Felker [Thu, 18 Jul 2019 23:07:32 +0000 (19:07 -0400)]
remove mips/n32/64 stat struct hacks from syscall machinery

now that we have a kstat structure decoupled from the public struct
stat, we can just use the broken kernel structures directly and let
the code in fstatat do the translation.

4 years agodecouple struct stat from kernel type
Rich Felker [Thu, 18 Jul 2019 23:38:12 +0000 (19:38 -0400)]
decouple struct stat from kernel type

presently, all archs/ABIs have struct stat matching the kernel
stat[64] type, except mips/mipsn32/mips64 which do conversion hacks in
syscall_arch.h to work around bugs in the kernel type. this patch
completely decouples them and adds a translation step to the success
path of fstatat. at present, this is just a gratuitous copying, but it
opens up multiple possibilities for future support for 64-bit time_t
on 32-bit archs and for cleaned-up/unified ABIs.

for clarity, the mips hacks are not yet removed in this commit, so the
mips kstat structs still correspond to the output of the hacks in
their syscall_arch.h files, not the raw kernel type. a subsequent
commit will fix this.

4 years agorefactor all stat functions in terms of fstatat
Rich Felker [Thu, 18 Jul 2019 19:16:20 +0000 (15:16 -0400)]
refactor all stat functions in terms of fstatat

equivalent logic for fstat+O_PATH fallback and direct use of
stat/lstat syscalls where appropriate is kept, now in the fstatat
function. this change both improves functionality (now, fstatat forms
equivalent to fstat/lstat/stat will work even on kernels too old to
have the at functions) and localizes direct interfacing with the
kernel stat structure to one file.

4 years agoremove utterly wrong includes from mips64/n32 bits/stat.h
Rich Felker [Thu, 18 Jul 2019 21:08:18 +0000 (17:08 -0400)]
remove utterly wrong includes from mips64/n32 bits/stat.h

these were overlooked during review. bits headers are not allowed to
pull in additional headers (note: that rule is currently broken in
other places but just for endian.h). string.h has no place here
anyway, and including bits/alltypes.h without defining macros to
request types from it is a nop.

4 years agouse register constraint instead of memory operand for riscv64 atomics
Rich Felker [Wed, 17 Jul 2019 23:07:57 +0000 (19:07 -0400)]
use register constraint instead of memory operand for riscv64 atomics

the "A" constraint is simply for an address expression that's a single
register, but it's not yet supported by clang, and has no advantage
here over just using a register operand for the address. the latter is
actually preferable in the a_cas_p case because it avoids aliasing an
lvalue onto the memory.

4 years agofix riscv64 atomic asm constraints
Rich Felker [Wed, 17 Jul 2019 22:53:26 +0000 (18:53 -0400)]
fix riscv64 atomic asm constraints

most egregious problem was the lack of memory clobber and lack of
volatile asm; this made the atomics memory barriers but not compiler
barriers. use of "+r" rather than "=r" for a clobbered temp was also
wrong, since the initial value is indeterminate.

4 years agofix riscv64 syscall asm constraint
Rich Felker [Wed, 17 Jul 2019 22:50:15 +0000 (18:50 -0400)]
fix riscv64 syscall asm constraint

having "+r"(a0) is redundant with "0"(a0) in syscalls with at least 1
arg, which is arguably a constraint violation (clang treats it as
such), and an invalid input with indeterminate value in the 0-arg
case. use the "=r"(a0) form instead.

4 years agofix broken lseek on x32 (x86_64/ILP32) with offsets larger than LONG_MAX
Rich Felker [Wed, 17 Jul 2019 03:07:49 +0000 (23:07 -0400)]
fix broken lseek on x32 (x86_64/ILP32) with offsets larger than LONG_MAX

this is analogous to commit 918c5fa0fc656e49b1ab9ce47183a23e3a36bc00
which fixed the corresponding issue for mips n32.

4 years agofix broken lseek on mipsn32 with offsets larger than LONG_MAX
Rich Felker [Wed, 17 Jul 2019 01:05:24 +0000 (21:05 -0400)]
fix broken lseek on mipsn32 with offsets larger than LONG_MAX

mips n32 has 32-bit long, and generally uses long syscall arguments
and return values, but provides only SYS_lseek, not SYS_llseek. we
have some framework (syscall_arg_t, added for x32) to make syscall
arguments 64-bit in such a setting, but it's not clear whether this
could match the sign-extension semantics needed for 32-bit args to all
the other syscalls, and we don't have any existing mechanism to allow
the return value of syscalls to be something other than long.

instead, just provide a custom mipsn32 version of the lseek function
doing its own syscall asm with 64-bit arguments. as a result of commit
03919b26ed41c31876db41f7cee076ced4513fad, stdio will also get the new
code, fixing fseeko/ftello too.

4 years agoclean up mips64/n32 syscall asm constraints
Rich Felker [Wed, 17 Jul 2019 00:49:02 +0000 (20:49 -0400)]
clean up mips64/n32 syscall asm constraints

ever since inline syscalls were added for (o32) mips in commit
328810d32524e4928fec50b57e37e1bf330b2e40, the asm has nonsensically
loaded the syscall number, rather than taking $2 as an input
constraint to let the compiler load it. commit
cfc09b1ecf0c6981494fd73dffe234416f66af10 improved on this somewhat by
allowing a constant syscall number to propagate into an immediate, but
missed that the whole operation made no sense.

now, only $4, $5, $6, $8, and $9 are potential input-only registers.
$2 is always input and output, and $7 is both when it's an argument,
otherwise output-only. previously, $7 was treated as an input (with a
"1" constraint matching its output position) even when it was not an
input, which was arguably undefined behavior (asm input from
indeterminate value). this is corrected.

4 years agodeduplicate mips64/n32 syscall clobbered register lists
Rich Felker [Wed, 17 Jul 2019 00:31:38 +0000 (20:31 -0400)]
deduplicate mips64/n32 syscall clobbered register lists

this patch is not purely non-functional changes, since before, $8 and
$9 were wrongly in the clobberlist for syscalls with fewer than 5 or 6
arguments. of course it's impossible for syscalls to have different
clobbers depending on their number of arguments. the clobberlist for
the recently-added 5- and 6-argument forms was correct, and for the 0-
to 4-argument forms was erroneously copied from the mips o32 ABI where
the additional arguments had to be passed on the stack.

in making this change, I reviewed the kernel sources, and $8 and $9
are always saved for 64-bit kernels since they're part of the syscall
argument list for n32 and n64 ABIs.

4 years agouse namespace-safe __lseek for __stdio_seek instead of direct syscall
Rich Felker [Tue, 16 Jul 2019 22:31:33 +0000 (18:31 -0400)]
use namespace-safe __lseek for __stdio_seek instead of direct syscall

this probably saves a few bytes, avoids duplicating the clunky
lseek/_llseek syscall convention in two places, and sets the stage for
fixing broken seeks on x32 and mipsn32.

4 years agorelease 1.1.23
Rich Felker [Tue, 16 Jul 2019 19:30:39 +0000 (15:30 -0400)]
release 1.1.23

4 years agoupdate year in COPYRIGHT file
Rich Felker [Mon, 15 Jul 2019 22:28:43 +0000 (18:28 -0400)]
update year in COPYRIGHT file

4 years agoupdate authors/contributors list
Rich Felker [Mon, 15 Jul 2019 22:26:30 +0000 (18:26 -0400)]
update authors/contributors list

these additions were made by scanning git log since the last major
update in commit 1366b3c5e6d89d5ba90dd41fe5bf0246c5299b84.

as before my aim was adding everyone with either substantial code
contributions or a pattern of ongoing simple patch submission; any
omissions are unintentional.

4 years agofix build failure on arm building C code in thumb1 mode
Rich Felker [Mon, 15 Jul 2019 19:33:12 +0000 (15:33 -0400)]
fix build failure on arm building C code in thumb1 mode

a fully thumb1 build is not supported because some asm files are
incompatible with thumb1, but apparently it works to compile the C
code as thumb1

commit 06fbefd10046a0fae7e588b7c6d25fb51811b931 caused this regression
but introducing use of the clz instruction, which is not supported in
arm mode prior to v5, and not supported in thumb prior to thumb2
(v6t2). commit 1b9406b03c0a94ebe2076a8fc1746a8c45e78a83 fixed the
issue only for arm mode pre-v5 but left thumb1 broken.

4 years agofix sigaltstack to ignore ss_size with SS_DISABLE, per POSIX
James Y Knight [Thu, 11 Jul 2019 15:48:08 +0000 (11:48 -0400)]
fix sigaltstack to ignore ss_size with SS_DISABLE, per POSIX

4 years agouse the correct attributes for ___errno_location
Samuel Holland [Sat, 29 Jun 2019 23:19:05 +0000 (18:19 -0500)]
use the correct attributes for ___errno_location

In the public header, __errno_location is declared with the "const"
attribute, conditional on __GNUC__. Ensure that its internal alias has
the same attributes.

Maintainer's note: This change also fixes a regression in quality of
code generation -- multiple references to errno in a single function
started generating multiple calls again -- introduced by commit
e13063aad7aee341d278d2a879a76ec7b59b2ad8.

4 years agofix conflicting mips and powerpc definitions for TIOCSER_TEMT macro
Samuel Holland [Sat, 29 Jun 2019 23:19:06 +0000 (18:19 -0500)]
fix conflicting mips and powerpc definitions for TIOCSER_TEMT macro

Commit 3517d74a5e04a377192d1f4882ad6c8dc22ce69a changed the token in
sys/ioctl.h from 0x01 to 1, so bits/termios.h no longer matches. Revert
the bits/termios.h change to keep the headers in sync.

This reverts commit 9eda4dc69c33852c97c6f69176bf45ffc80b522f.

4 years agofix restrict violations in internal use of several functions
Samuel Holland [Sat, 29 Jun 2019 23:19:04 +0000 (18:19 -0500)]
fix restrict violations in internal use of several functions

The old/new parameters to pthread_sigmask, sigprocmask, and setitimer
are marked restrict, so passing the same address to both is
prohibited. Modify callers of these functions to use a separate object
for each argument.

4 years agomention mips64 n32 ABI support in INSTALL doc
Rich Felker [Tue, 9 Jul 2019 22:40:50 +0000 (18:40 -0400)]
mention mips64 n32 ABI support in INSTALL doc

4 years agodocument riscv64 support in INSTALL document
Rich Felker [Tue, 9 Jul 2019 22:40:07 +0000 (18:40 -0400)]
document riscv64 support in INSTALL document

4 years agoprevent dup2 action for posix_spawn internal pipe fd
Rich Felker [Tue, 9 Jul 2019 03:47:15 +0000 (23:47 -0400)]
prevent dup2 action for posix_spawn internal pipe fd

as reported by Tavian Barnes, a dup2 file action for the internal pipe
fd used by posix_spawn could cause it to remain open after execve and
allow the child to write an artificial error into it, confusing the
parent. POSIX allows internal use of file descriptors by the
implementation, with undefined behavior for poking at them, so this is
not a conformance problem, but it seems preferable to diagnose and
prevent the error when we can do so easily.

catch attempts to apply a dup2 action to the internal pipe fd and
emulate EBADF for it instead.

4 years agofix inadvertent use of uninitialized variable in dladdr
Rich Felker [Sat, 6 Jul 2019 21:47:43 +0000 (17:47 -0400)]
fix inadvertent use of uninitialized variable in dladdr

commit c8b49b2fbc7faa8bf065220f11963d76c8a2eb93 introduced code that
checked bestsym to determine whether a matching symbol was found, but
bestsym is uninitialized if not. instead use best, consistent with use
in the rest of the function.

simplified from bug report and patch by Cheng Liu.

4 years agoremove spurious MAP_32BIT definition from riscv64 arch
Rich Felker [Thu, 4 Jul 2019 16:28:29 +0000 (12:28 -0400)]
remove spurious MAP_32BIT definition from riscv64 arch

this was apparently copied from x86_64; it's not part of the kernel
API for riscv64. this change eliminates the need for a
riscv64-specific bits header and lets it use the generic one.