musl
9 years agofix sendmmsg emulation return value for zero-length vector
Rich Felker [Fri, 20 Jun 2014 06:13:38 +0000 (02:13 -0400)]
fix sendmmsg emulation return value for zero-length vector

this case is not even documented, but the kernel returns 0 here and it
makes sense to be consistent.

9 years agorename dynamic linker entry point from _start to _dlstart
Rich Felker [Fri, 20 Jun 2014 04:25:12 +0000 (00:25 -0400)]
rename dynamic linker entry point from _start to _dlstart

the main motivation for this change is to aid in debugging. since the
main program's entry point is also named _start, it was difficult to
set breakpoints or quickly identify which _start execution stopped in.

9 years agoimplement sendmmsg and recvmmsg
Rich Felker [Fri, 20 Jun 2014 03:01:15 +0000 (23:01 -0400)]
implement sendmmsg and recvmmsg

these are not pure syscall wrappers because they have to work around
kernel API bugs on 64-bit archs. the workarounds could probably be
made somewhat more efficient, but at the cost of more complexity. this
may be revisited later.

9 years agoadd tlsdesc support for x86_64
Rich Felker [Thu, 19 Jun 2014 19:26:04 +0000 (15:26 -0400)]
add tlsdesc support for x86_64

9 years agoseparate __tls_get_addr implementation from dynamic linker/init_tls
Rich Felker [Thu, 19 Jun 2014 06:59:44 +0000 (02:59 -0400)]
separate __tls_get_addr implementation from dynamic linker/init_tls

such separation serves multiple purposes:

- by having the common path for __tls_get_addr alone in its own
  function with a tail call to the slow case, code generation is
  greatly improved.

- by having __tls_get_addr in it own file, it can be replaced on a
  per-arch basis as needed, for optimization or ABI-specific purposes.

- by removing __tls_get_addr from __init_tls.c, a few bytes of code
  are shaved off of static binaries (which are unlikely to use this
  function unless the linker messed up).

9 years agoadd tlsdesc support for i386
Rich Felker [Thu, 19 Jun 2014 06:50:45 +0000 (02:50 -0400)]
add tlsdesc support for i386

9 years agooptimize i386 ___tls_get_addr asm
Rich Felker [Thu, 19 Jun 2014 06:48:45 +0000 (02:48 -0400)]
optimize i386 ___tls_get_addr asm

9 years agochange dynamic TLS installation strategy to optimize access
Rich Felker [Thu, 19 Jun 2014 06:16:57 +0000 (02:16 -0400)]
change dynamic TLS installation strategy to optimize access

previously, accesses to dynamic TLS had to check two conditions before
being able to use a dtv slot: (1) that the module index was within the
bounds of the current dtv size, and (2) that the dynamic tls for the
requested module index was already installed in the dtv.

this commit changes the installation strategy so that, whenever an
attempt is made to access dynamic TLS that's not yet installed in the
dtv, the dynamic TLS for all lower-index modules is also installed.
thus it provides a new invariant: if a given module index is within
the bounds of the current dtv size, we automatically know that its TLS
is installed and directly available. the requirement that the second
condition (above) be checked is eliminated.

9 years agoadd arch-generic support for tlsdesc relocations to dynamic linker
Rich Felker [Thu, 19 Jun 2014 06:01:06 +0000 (02:01 -0400)]
add arch-generic support for tlsdesc relocations to dynamic linker

this code is non-functional without further changes to link up the
arch-specific reloc types for tlsdesc and add asm implementations of
__tlsdesc_static and __tlsdesc_dynamic.

9 years agofix incorrect comparison loop condition in memmem
Rich Felker [Thu, 19 Jun 2014 04:42:28 +0000 (00:42 -0400)]
fix incorrect comparison loop condition in memmem

the logic for this loop was copied from null-terminated-string logic
in strstr without properly adapting it to work with explicit lengths.

presumably this error could result in false negatives (wrongly
comparing past the end of the needle/haystack), false positives
(stopping comparison early when the needle contains null bytes), and
crashes (from runaway reads past the end of mapped memory).

9 years agoreduce code duplication in dynamic linker error paths
Rich Felker [Wed, 18 Jun 2014 07:05:42 +0000 (03:05 -0400)]
reduce code duplication in dynamic linker error paths

eventually this should help making dlerror thread-safe too.

9 years agorefactor to remove arch-specific relocation code from dynamic linker
Rich Felker [Wed, 18 Jun 2014 06:44:02 +0000 (02:44 -0400)]
refactor to remove arch-specific relocation code from dynamic linker

this was one of the main instances of ugly code duplication: all archs
use basically the same types of relocations, but roughly equivalent
logic was duplicated for each arch to account for the different naming
and numbering of relocation types and variation in whether REL or RELA
records are used.

as an added bonus, both REL and RELA are now supported on all archs,
regardless of which is used by the standard toolchain.

9 years agofix powerpc dynamic linker thread-pointer-relative relocations
Rich Felker [Tue, 17 Jun 2014 18:32:43 +0000 (14:32 -0400)]
fix powerpc dynamic linker thread-pointer-relative relocations

processing of R_PPC_TPREL32 was ignoring the addend provided by the
RELA-style relocation and instead using the inline value as the
addend. this presumably broke dynamic-linked access to initial TLS in
cases where the addend was nonzero.

9 years agomultiple fixes to sh (superh) dynamic linker relocations
Rich Felker [Tue, 17 Jun 2014 17:56:54 +0000 (13:56 -0400)]
multiple fixes to sh (superh) dynamic linker relocations

the following issues are fixed:

- R_SH_REL32 was adding the load address of the module being relocated
  to the result. this seems to have been a mistake in the original
  port, since it does not match other dynamic linker implementations
  and since adding a difference between two addresses (the symbol
  value and the relocation address) to a load address does not make
  sense.

- R_SH_TLS_DTPMOD32 was wrongly accepting an inline addend (i.e. using
  += rather than = on *reloc_addr) which makes no sense; addition is
  not an operation that's defined on module ids.

- R_SH_TLS_DTPOFF32 and R_SH_TLS_TPOFF32 were wrongly using inline
  addends rather than the RELA-provided addends.

in addition, handling of R_SH_GLOB_DAT, R_SH_JMP_SLOT, and R_SH_DIR32
are merged to all honor the addend. the first two should not need it
for correct usage generated by toolchains, but other dynamic linkers
allow addends here, and it simplifies the code anyway.

these issues were spotted while reviewing the code for the purpose of
refactoring this part of the dynamic linker. no testing was performed.

9 years agodynamic linker: permit error returns from arch-specific reloc function
Rich Felker [Mon, 16 Jun 2014 07:09:07 +0000 (03:09 -0400)]
dynamic linker: permit error returns from arch-specific reloc function

the immediate motivation is supporting TLSDESC relocations which
require allocation and thus may fail (unless we pre-allocate), but
this mechanism should also be used for throwing an error on
unsupported or invalid relocation types, and perhaps in certain cases,
for reporting when a relocation is not satisfiable.

9 years agofix missing argument to syscall in fanotify_mark
Clément Vasseur [Fri, 13 Jun 2014 16:27:56 +0000 (18:27 +0200)]
fix missing argument to syscall in fanotify_mark

9 years agosupport optional-argument extension to getopt via double-colon
Rich Felker [Thu, 12 Jun 2014 03:38:44 +0000 (23:38 -0400)]
support optional-argument extension to getopt via double-colon

this extension is not incompatible with the standard behavior of the
function, not expensive, and avoids requiring a replacement getopt
with full GNU extensions for a few important apps including busybox's
sed with the -i option.

9 years agofail configure on --enable-shared if -Bsymbolic-functions doesn't work
Rich Felker [Tue, 10 Jun 2014 16:11:12 +0000 (12:11 -0400)]
fail configure on --enable-shared if -Bsymbolic-functions doesn't work

previously, a warning was issued in this case no matter what, even if
--disable-shared was used. now, the default for --enable-shared is
changed from "yes" to "auto", and the warning is issued by default,
but becomes an error if --enable-shared is used, and the test is
suppressed completely if --disable-shared is used.

9 years agosimplify errno implementation
Rich Felker [Tue, 10 Jun 2014 08:49:49 +0000 (04:49 -0400)]
simplify errno implementation

the motivation for the errno_ptr field in the thread structure, which
this commit removes, was to allow the main thread's errno to keep its
address when lazy thread pointer initialization was used. &errno was
evaluated prior to setting up the thread pointer and stored in
errno_ptr for the main thread; subsequently created threads would have
errno_ptr pointing to their own errno_val in the thread structure.

since lazy initialization was removed, there is no need for this extra
level of indirection; __errno_location can simply return the address
of the thread's errno_val directly. this does cause &errno to change,
but the change happens before entry to application code, and thus is
not observable.

9 years agoreplace all remaining internal uses of pthread_self with __pthread_self
Rich Felker [Tue, 10 Jun 2014 08:02:40 +0000 (04:02 -0400)]
replace all remaining internal uses of pthread_self with __pthread_self

prior to version 1.1.0, the difference between pthread_self (the
public function) and __pthread_self (the internal macro or inline
function) was that the former would lazily initialize the thread
pointer if it was not already initialized, whereas the latter would
crash in this case. since lazy initialization is no longer supported,
use of pthread_self no longer makes sense; it simply generates larger,
slower code.

9 years agoadd thread-pointer support for pre-2.6 kernels on i386
Rich Felker [Tue, 10 Jun 2014 07:36:56 +0000 (03:36 -0400)]
add thread-pointer support for pre-2.6 kernels on i386

such kernels cannot support threads, but the thread pointer is also
important for other purposes, most notably stack protector. without a
valid thread pointer, all code compiled with stack protector will
crash. the same applies to any use of thread-local storage by
applications or libraries.

the concept of this patch is to fall back to using the modify_ldt
syscall, which has been around since linux 1.0, to setup the gs
segment register. since the kernel does not have a way to
automatically assign ldt entries, use of slot zero is hard-coded. if
this fallback path is used, __set_thread_area returns a positive value
(rather than the usual zero for success, or negative for error)
indicating to the caller that the thread pointer was successfully set,
but only for the main thread, and that thread creation will not work
properly. the code in __init_tp has been changed accordingly to record
this result for later use by pthread_create.

9 years agoavoid spurious lookup failures from badly-behaved nameservers
Rich Felker [Sat, 7 Jun 2014 08:09:21 +0000 (04:09 -0400)]
avoid spurious lookup failures from badly-behaved nameservers

the results of a dns query, whether it's performed as part of one of
the standard name-resolving functions or directly by res_send, should
be a function of the query, not of the particular nameserver that
responds to it. thus, all responses which indicate a failure or
refusal by the nameserver, as opposed to a positive or negative result
for the query, should be ignored.

the strategy used is to re-issue the query immediately (but with a
limit on the number of retries, in case the server is really broken)
when a response code of 2 (server failure, typically transient) is
seen, and otherwise take no action on bad responses (which generally
indicate a misconfigured nameserver or one which the client does not
have permission to use), allowing the normal retry interval to apply
and of course accepting responses from other nameservers queried in
parallel.

empirically this matches the traditional resolver behavior for
nameservers that respond with a code of 2 in the case where there is
just a single nameserver configured. the behavior diverges when
multiple nameservers are available, since musl is querying them in
parallel. in this case we are mildly more aggressive at retrying.

9 years agouse default timezone from /etc/localtime if $TZ is unset/blank
Rich Felker [Sat, 7 Jun 2014 00:46:05 +0000 (20:46 -0400)]
use default timezone from /etc/localtime if $TZ is unset/blank

the way this is implemented, it also allows explicit setting of
TZ=/etc/localtime even for suid programs. this is not a problem
because /etc/localtime is a trusted path, much like the trusted
zoneinfo search path.

9 years agorelease 1.1.2
Rich Felker [Fri, 6 Jun 2014 22:36:00 +0000 (18:36 -0400)]
release 1.1.2

9 years agoimplement %y and %C specifiers in strptime
Timo Teräs [Wed, 14 May 2014 07:53:56 +0000 (10:53 +0300)]
implement %y and %C specifiers in strptime

9 years agoavoid invalid use of va_arg in open
Rich Felker [Fri, 6 Jun 2014 19:43:16 +0000 (15:43 -0400)]
avoid invalid use of va_arg in open

reading the variadic mode argument is only valid when the O_CREAT flag
is present. this probably does not matter, but is needed for formal
correctness, and could affect LTO or other full-program analysis.

9 years agoadd O_CLOEXEC fallback for open and related functions
Rich Felker [Fri, 6 Jun 2014 19:42:42 +0000 (15:42 -0400)]
add O_CLOEXEC fallback for open and related functions

since there is no easy way to detect whether open honored or ignored
the O_CLOEXEC flag, the optimal solution to providing a fallback is
simply to make the fcntl syscall to set the close-on-exec flag
immediately after open returns.

9 years agooptimize SOCK_CLOEXEC fallback for socket function
Rich Felker [Fri, 6 Jun 2014 19:30:28 +0000 (15:30 -0400)]
optimize SOCK_CLOEXEC fallback for socket function

the fcntl function is heavy, so make the syscall directly instead.
also, avoid the code size and runtime overhead of querying the old
flags, since it's reasonable to assume nothing will be set on a
newly-created socket. this code is only used on old kernels which lack
proper atomic close-on-exec support, so future changes that might
invalidate such an assumption do not need to be considered.

9 years agoadd SOCK_CLOEXEC fallback for socketpair on old kernels
Rich Felker [Fri, 6 Jun 2014 19:29:00 +0000 (15:29 -0400)]
add SOCK_CLOEXEC fallback for socketpair on old kernels

as usual, this is non-atomic, but better than producing an error or
failing to set the close-on-exec flag at all.

9 years agoimplement dn_comp RFC 1035 domain name compression
Szabolcs Nagy [Fri, 6 Jun 2014 18:44:54 +0000 (20:44 +0200)]
implement dn_comp RFC 1035 domain name compression

the input name is validated, the other parameters are assumed to be
valid (the list of already compressed names are not checked for
infinite reference loops or out-of-bound offsets).

names are handled case-sensitively for now.

9 years agoaccept trailing . and empty domain names
Szabolcs Nagy [Fri, 6 Jun 2014 17:20:07 +0000 (19:20 +0200)]
accept trailing . and empty domain names

trailing . should be accepted in domain name strings by convention
(RFC 1034), host name lookup accepts "." but rejects empty "", res_*
interfaces also accept empty name following existing practice.

9 years agoadd vdso clock_gettime acceleration support to i386
Rich Felker [Fri, 6 Jun 2014 07:29:36 +0000 (03:29 -0400)]
add vdso clock_gettime acceleration support to i386

9 years agofix fd leak in tmpfile when the fdopen operation fails
Rich Felker [Fri, 6 Jun 2014 07:17:47 +0000 (03:17 -0400)]
fix fd leak in tmpfile when the fdopen operation fails

this condition could only happen due to malloc failure.

the fdopen operation is also moved to take place after the unlink to
minimize the window during which a link to the file exists in the
directory table.

9 years agofix the domain name length limit checks
Szabolcs Nagy [Thu, 5 Jun 2014 20:52:40 +0000 (22:52 +0200)]
fix the domain name length limit checks

A domain name is at most 255 bytes long (RFC 1035), but the string
representation is two bytes smaller so the strlen maximum is 253.

9 years agofix multiple validation issues in dns response label parsing
Szabolcs Nagy [Thu, 5 Jun 2014 20:32:42 +0000 (22:32 +0200)]
fix multiple validation issues in dns response label parsing

Due to an error introduced in commit fcc522c92335783293ac19df318415cd97fbf66b,
checking of the remaining output buffer space was not performed correctly,
allowing malformed input to write past the end of the buffer.

In addition, the loop detection logic failed to account for the possibility
of infinite loops with no output, which would hang the function.

The output size is now limited more strictly so only names with valid length
are accepted.

9 years agofix missing function declarations in refactored ip literal parsing code
Rich Felker [Thu, 5 Jun 2014 19:55:20 +0000 (15:55 -0400)]
fix missing function declarations in refactored ip literal parsing code

9 years agoadd support for reverse port lookups from services file to getnameinfo
Rich Felker [Wed, 4 Jun 2014 23:33:20 +0000 (19:33 -0400)]
add support for reverse port lookups from services file to getnameinfo

this also affects the legacy getservbyport family, which uses
getnameinfo as its backend.

9 years agoadd support for reverse name lookups from hosts file to getnameinfo
Rich Felker [Wed, 4 Jun 2014 20:53:39 +0000 (16:53 -0400)]
add support for reverse name lookups from hosts file to getnameinfo

this also affects the legacy gethostbyaddr family, which uses
getnameinfo as its backend.

some other minor changes associated with the refactoring of source
files are also made; in particular, the resolv.conf parser now uses
the same code that's used elsewhere to handle ip literals, so as a
side effect it can now accept a scope id for nameserver addressed with
link-local scope.

9 years agoremove some dummy "ent" function aliases that duplicated real ones
Rich Felker [Wed, 4 Jun 2014 08:39:34 +0000 (04:39 -0400)]
remove some dummy "ent" function aliases that duplicated real ones

the service and protocol functions are defined also in other files,
and the protocol ones are actually non-nops elsewhere, so the weak
definitions in ent.c could have prevented the strong definitions from
getting pulled in and used in some static programs.

9 years agosimplify vasprintf implementation
Rich Felker [Wed, 4 Jun 2014 07:39:22 +0000 (03:39 -0400)]
simplify vasprintf implementation

the old implementation preallocated a buffer in order to try to avoid
calling vsnprintf more than once. not only did this potentially lead
to memory fragmentation from trimming with realloc; it also pulled in
realloc/free, which otherwise might not be needed in a static linked
program.

9 years agoadd support for ipv6 scope_id to getaddrinfo and getnameinfo
Rich Felker [Wed, 4 Jun 2014 06:24:38 +0000 (02:24 -0400)]
add support for ipv6 scope_id to getaddrinfo and getnameinfo

for all address types, a scope_id specified as a decimal value is
accepted. for addresses with link-local scope, a string containing the
interface name is also accepted.

some changes are made to error handling to avoid unwanted fallbacks in
the case where the scope_id is invalid: if an earlier name lookup
backend fails with an error rather than simply "0 results", this
failure now suppresses any later attempts with other backends.

in getnameinfo, a light "itoa" type function is added for generating
decimal scope_id results, and decimal port strings for services are
also generated using this function now so as not to pull in the
dependency on snprintf.

in netdb.h, a definition for the NI_NUMERICSCOPE flag is added. this
is required by POSIX (it was previously missing) and needed to allow
callers to suppress interface-name lookups.

9 years agofix if_nametoindex return value when interface does not exist
Rich Felker [Tue, 3 Jun 2014 21:53:11 +0000 (17:53 -0400)]
fix if_nametoindex return value when interface does not exist

the return value is unsigned, so negative results for "errors" do not
make sense; 0 is the value reserved for when the interface name does
not exist.

9 years agofix negative response and non-response handling for dns queries
Rich Felker [Tue, 3 Jun 2014 05:46:40 +0000 (01:46 -0400)]
fix negative response and non-response handling for dns queries

previously, all failures to obtain at least one address were treated
as nonexistant names (EAI_NONAME). this failed to account for the
possibility of transient failures (no response at all, or a response
with rcode of 2, server failure) or permanent failures that do not
indicate the nonexistence of the requested name. only an rcode of 3
should be treated as an indication of nonexistence.

9 years agofix some validation checks in dns response parsing code
Rich Felker [Tue, 3 Jun 2014 05:43:29 +0000 (01:43 -0400)]
fix some validation checks in dns response parsing code

since the buffer passed always has an actual size of 512 bytes, the
maximum possible response packet size, no out-of-bounds access was
possible; however, reading past the end of the valid portion of the
packet could cause the parser to attempt to process junk as answer
content.

9 years agofix incorrect end pointer in some cases when wcsrtombs stops early
Rich Felker [Mon, 2 Jun 2014 20:21:47 +0000 (16:21 -0400)]
fix incorrect end pointer in some cases when wcsrtombs stops early

when wcsrtombs stopped due to hitting zero remaining space in the
output buffer, it was wrongly clearing the position pointer as if it
had completed the conversion successfully.

this commit rearranges the code somewhat to make a clear separation
between the cases of ending due to running out of output buffer space,
and ending due to reaching the end of input or an illegal sequence in
the input. the new branches have been arranged with the hope of
optimizing more common cases, too.

9 years agoremove cruft from old resolver and numeric ip parsing
Rich Felker [Mon, 2 Jun 2014 09:00:48 +0000 (05:00 -0400)]
remove cruft from old resolver and numeric ip parsing

the old resolver code used a function __ipparse which contained the
logic for inet_addr and inet_aton, which is needed in getaddrinfo.
this was phased out in the resolver overhaul in favor of directly
using inet_aton and inet_pton as appropriate.

this commit cleans up some stuff that was left behind.

9 years agoswitch standard resolver functions to use the new dns backend
Rich Felker [Mon, 2 Jun 2014 08:47:45 +0000 (04:47 -0400)]
switch standard resolver functions to use the new dns backend

this is the third phase of the "resolver overhaul" project.

this commit removes all of the old dns code, and switches the
__lookup_name backend (used by getaddrinfo, etc.) and the getnameinfo
function to use the newly implemented __res_mkquery and __res_msend
interfaces. for parsing the results, a new callback-based __dns_parse
function, based on __dns_get_rr from the old dns code, is used.

9 years agofix uninitialized variable in new __res_msend dns function
Rich Felker [Mon, 2 Jun 2014 08:27:54 +0000 (04:27 -0400)]
fix uninitialized variable in new __res_msend dns function

9 years agoimplement new dns backend, res_send and other legacy resolver functions
Rich Felker [Mon, 2 Jun 2014 06:13:57 +0000 (02:13 -0400)]
implement new dns backend, res_send and other legacy resolver functions

this is the second phase of the "resolver overhaul" project.

the key additions in this commit are the __res_msend and __res_mkquery
functions, which have been factored so as to provide a backend for
both the legacy res_* functions and the standard getaddrinfo and
getnameinfo functions. the latter however are still using the old
backend code; there is code duplication which still needs to be
removed, and this will be the next phase of the resolver overhaul.

__res_msend is derived from the old __dns_doqueries function, but
generalized to send arbitrary caller-provided packets in parallel
rather than producing the parallel queries itself. this allows it to
be used (completely trivially) as a backend for res_send. the
factored-out query generation code, with slightly more generality, is
now part of __res_mkquery.

9 years agoadd ipsec and tunneling protocols to getprotoent-family functions
Timo Teräs [Mon, 2 Jun 2014 05:16:08 +0000 (08:16 +0300)]
add ipsec and tunneling protocols to getprotoent-family functions

iptables and ipsec-tools among others require these to function
properly.

9 years agofix off-by-one in checking hostname length in new resolver backend
Rich Felker [Mon, 2 Jun 2014 05:31:28 +0000 (01:31 -0400)]
fix off-by-one in checking hostname length in new resolver backend

this bug was introduced in the recent resolver overhaul commits. it
likely had visible symptoms. these were probably limited to wrongly
accepting truncated versions of over-long names (vs rejecting them),
as opposed to stack-based overflows or anything more severe, but no
extensive checks were made. there have been no releases where this bug
was present.

9 years agoimprove getservbyname_r using new resolver backend
Rich Felker [Sun, 1 Jun 2014 04:45:04 +0000 (00:45 -0400)]
improve getservbyname_r using new resolver backend

now that host and service lookup have been separated in the backend,
there's no need for service lookup functions to pull in the host
lookup code. moreover, dynamic allocation is no longer needed, so this
function should now be async-signal-safe. it's also significantly
smaller.

one change in getservbyname is also made: knowing that getservbyname_r
needs only two character pointers in the caller-provided buffer, some
wasted bss can be avoided.

9 years agoimprove gethostbyname2_r using new resolver backend
Rich Felker [Sun, 1 Jun 2014 04:09:28 +0000 (00:09 -0400)]
improve gethostbyname2_r using new resolver backend

these changes reduce the size of the function somewhat and remove many
of its dependencies, including free. in principle it should now be
async-signal-safe, but this has not been verified in detail.

minor changes to error handling are also made.

9 years agorefactor getaddrinfo and add support for most remaining features
Rich Felker [Sun, 1 Jun 2014 00:57:54 +0000 (20:57 -0400)]
refactor getaddrinfo and add support for most remaining features

this is the first phase of the "resolver overhaul" project.

conceptually, the results of getaddrinfo are a direct product of a
list of address results and a list of service results. the new code
makes this explicit by computing these lists separately and combining
the results. this adds support for services that have both tcp and udp
versions, where the caller has not specified which it wants, and
eliminates a number of duplicate code paths which were all producing
the final output addrinfo structures, but in subtly different ways,
making it difficult to implement any of the features which were
missing.

in addition to the above benefits, the refactoring allows for legacy
functions like gethostbyname to be implemented without using the
getaddrinfo function itself. such changes to the legacy functions have
not yet been made, however.

further improvements include matching of service alias names from
/etc/services (previously only the primary name was supported),
returning multiple results from /etc/hosts (previously only the first
matching line was honored), and support for the AI_V4MAPPED and AI_ALL
flags.

features which remain unimplemented are IDN translations (encoding
non-ASCII hostnames for DNS lookup) and the AI_ADDRCONFIG flag.

at this point, the DNS-based name resolving code is still based on the
old interfaces in __dns.c, albeit somewhat simpler in its use of them.
there may be some dead code which could already be removed, but
changes to this layer will be a later phase of the resolver overhaul.

9 years agofix POSIX namespace pollution in sys/types.h
Szabolcs Nagy [Fri, 30 May 2014 15:57:16 +0000 (17:57 +0200)]
fix POSIX namespace pollution in sys/types.h

int8_t, u_int8_t, etc types are moved under _BSD_SOURCE

9 years agouse cleaner code for handling float rounding in vfprintf
Szabolcs Nagy [Fri, 30 May 2014 15:09:53 +0000 (17:09 +0200)]
use cleaner code for handling float rounding in vfprintf

CONCAT(0x1p,LDBL_MANT_DIG) is not safe outside of libc,
use 2/LDBL_EPSILON instead.

fix was proposed by Morten Welinder.

9 years agoupdate netinet/in.h to match the current kernel uapi
Szabolcs Nagy [Fri, 30 May 2014 12:15:48 +0000 (14:15 +0200)]
update netinet/in.h to match the current kernel uapi

from linux/in.h and linux/in6.h uapi headers the following
missing socket options were added:

IP_NODEFRAG - used with customized ipv4 headers
IPV6_RECVPATHMTU - for ipv6 path mtu
IPV6_PATHMTU - for ipv6 path mtu
IPV6_DONTFRAG - for ipv6 path mtu
IPV6_ADDR_PREFERENCES - RFC5014 Source Address Selection
IPV6_MINHOPCOUNT - RFC5082 Generalized TTL Security Mechanism
IPV6_ORIGDSTADDR - used by tproxy
IPV6_RECVORIGDSTADDR - used by tproxy
IPV6_TRANSPARENT - used by tproxy
IPV6_UNICAST_IF - ipv6 version of IP_UNICAST_IF

and socket option values:

IP_PMTUDISC_OMIT - value for IP_MTU_DISCOVER option, new in linux 3.14
IPV6_PMTUDISC_OMIT - same for IPV6_MTU_DISCOVER
IPV6_PMTUDISC_INTERFACE - ipv6 version of IP_PMTUDISC_INTERFACE
IPV6_PREFER_* - flags for IPV6_ADDR_PREFERENCES

not added: ipv6 flow info and flow label related definitions.
(it's unclear if libc should define these and namespace polluting
type name is involved so they are not provided for now)

9 years agoadd SO_BPF_EXTENSIONS socket option
Szabolcs Nagy [Fri, 30 May 2014 12:07:59 +0000 (14:07 +0200)]
add SO_BPF_EXTENSIONS socket option

this was introduced to query BPF extension support with getsockopt
in linux 3.14, commit ea02f9411d9faa3553ed09ce0ec9f00ceae9885e

9 years agoadd sched_{get,set}attr syscall numbers and SCHED_DEADLINE macro
Szabolcs Nagy [Fri, 30 May 2014 11:47:44 +0000 (13:47 +0200)]
add sched_{get,set}attr syscall numbers and SCHED_DEADLINE macro

linux 3.14 introduced sched_getattr and sched_setattr syscalls in
commit d50dde5a10f305253cbc3855307f608f8a3c5f73

and the related SCHED_DEADLINE scheduling policy in
commit aab03e05e8f7e26f51dee792beddcb5cca9215a5

but struct sched_attr "extended scheduling parameters data structure"
is not yet exported to userspace (necessary for using the syscalls)
so related uapi definitions are not added yet.

9 years agofix breakage from recent syscall commits due to missing errno macros
Rich Felker [Fri, 30 May 2014 16:59:36 +0000 (12:59 -0400)]
fix breakage from recent syscall commits due to missing errno macros

9 years agofix for broken kernel side RLIM_INFINITY on mips
Szabolcs Nagy [Fri, 30 May 2014 06:47:35 +0000 (08:47 +0200)]
fix for broken kernel side RLIM_INFINITY on mips

On 32 bit mips the kernel uses -1UL/2 to mark RLIM_INFINITY (and
this is the definition in the userspace api), but since it is in
the middle of the valid range of limits and limits are often
compared with relational operators, various kernel side logic is
broken if larger than -1UL/2 limits are used. So we truncate the
limits to -1UL/2 in get/setrlimit and prlimit.

Even if the kernel side logic consistently treated -1UL/2 as greater
than any other limit value, there wouldn't be any clean workaround
that allowed using large limits:
* using -1UL/2 as RLIM_INFINITY in userspace would mean different
infinity value for get/setrlimt and prlimit (where infinity is always
-1ULL) and userspace logic could break easily (just like the kernel
is broken now) and more special case code would be needed for mips.
* translating -1UL/2 kernel side value to -1ULL in userspace would
mean that -1UL/2 limit cannot be set (eg. -1UL/2+1 had to be passed
to the kernel instead).

9 years agobreak down coarse-grained 64-bit-off_t syscall remappings
Rich Felker [Fri, 30 May 2014 06:05:59 +0000 (02:05 -0400)]
break down coarse-grained 64-bit-off_t syscall remappings

using the existence of SYS_stat64 as the condition for remapping other
related syscalls is no longer valid, since new archs that omit the old
syscalls will not have SYS_stat or SYS_stat64, but still potentially
need SYS_fstat and others remapped. it would probably be possible to
get by with just one or two extra conditionals, but just breaking them
all down into separate conditions is robust and not significantly
heavier for the preprocessor.

9 years agoadditional fixes for linux kernel apis with old syscalls removed
Rich Felker [Fri, 30 May 2014 05:51:23 +0000 (01:51 -0400)]
additional fixes for linux kernel apis with old syscalls removed

9 years agofix sendfile syscall to use 64-bit off_t
Rich Felker [Fri, 30 May 2014 05:12:42 +0000 (01:12 -0400)]
fix sendfile syscall to use 64-bit off_t

somehow the remapping of this syscall to the 64-bit version was
overlooked. the issue was found, and patch provided, by Stefan
Kristiansson. presumably the reason this bug was not caught earlier is
that the syscall takes a pointer to off_t rather than a value, so on
little-endian systems, everything appears to work as long as the
offset value fits in the low 31 bits. on big-endian systems, though,
sendfile was presumably completely non-functional.

9 years agosupport linux kernel apis (new archs) with old syscalls removed
Rich Felker [Fri, 30 May 2014 01:01:32 +0000 (21:01 -0400)]
support linux kernel apis (new archs) with old syscalls removed

such archs are expected to omit definitions of the SYS_* macros for
syscalls their kernels lack from arch/$ARCH/bits/syscall.h. the
preprocessor is then able to select the an appropriate implementation
for affected functions. two basic strategies are used on a
case-by-case basis:

where the old syscalls correspond to deprecated library-level
functions, the deprecated functions have been converted to wrappers
for the modern function, and the modern function has fallback code
(omitted at the preprocessor level on new archs) to make use of the
old syscalls if the new syscall fails with ENOSYS. this also improves
functionality on older kernels and eliminates the incentive to program
with deprecated library-level functions for the sake of compatibility
with older kernels.

in other situations where the old syscalls correspond to library-level
functions which are not deprecated but merely lack some new features,
such as the *at functions, the old syscalls are still used on archs
which support them. this may change at some point in the future if or
when fallback code is added to the new functions to make them usable
(possibly with reduced functionality) on old kernels.

10 years agoremove useless synchronization in exit and quick_exit
Rich Felker [Thu, 29 May 2014 20:35:09 +0000 (16:35 -0400)]
remove useless synchronization in exit and quick_exit

calling exit more than once invokes undefined behavior. in some cases
it's desirable to detect undefined behavior and diagnose it via a
predictable crash, but the code here was silently covering up an
uncommon case (exit from more than one thread) and turning a much more
common case (recursive calls to exit) into a permanent hang.

10 years agofix missing declaration of strcpy in implementation of tmpnam
Rich Felker [Tue, 27 May 2014 18:21:46 +0000 (14:21 -0400)]
fix missing declaration of strcpy in implementation of tmpnam

10 years agooverhaul tmpfile, tmpnam, and tempnam functions
Rich Felker [Tue, 27 May 2014 04:44:23 +0000 (00:44 -0400)]
overhaul tmpfile, tmpnam, and tempnam functions

these all now use the shared __randname function internally, rather
than duplicating logic for producing a random name. incorrect usage of
the access syscall (which works with real uid/gid, not effective) has
been removed, along with unnecessary heavy dependencies like snprintf.

10 years agofix sys_open macro for archs without the plain open syscall
Rich Felker [Tue, 27 May 2014 04:11:37 +0000 (00:11 -0400)]
fix sys_open macro for archs without the plain open syscall

10 years agofix placement of multiple inclusion guard endif in internal syscall.h
Rich Felker [Tue, 27 May 2014 04:09:05 +0000 (00:09 -0400)]
fix placement of multiple inclusion guard endif in internal syscall.h

this was messed up during a recent commit when the socketcall macros
were moved to the common internal/syscall.h, and the following commit
expanded the problem by adding more new content outside the guard.

10 years agofix type of extended argument array to pselect6 syscall
Rich Felker [Tue, 27 May 2014 01:26:46 +0000 (21:26 -0400)]
fix type of extended argument array to pselect6 syscall

this only matters on x32 (and perhaps future 32-on-64 abis for other
archs); otherwise the type is long anyway. the cast through uintptr_t
prevents nonsensical "sign extension" of pointers, and follows the
principle that uintptr_t is the canonical integer type to which
pointer conversion is safe.

10 years agosupport kernels with no SYS_open syscall, only SYS_openat
Rich Felker [Sun, 25 May 2014 02:54:05 +0000 (22:54 -0400)]
support kernels with no SYS_open syscall, only SYS_openat

open is handled specially because it is used from so many places, in
so many variants (2 or 3 arguments, setting errno or not, and
cancellable or not). trying to do it as a function would not only
increase bloat, but would also risk subtle breakage.

this is the first step towards supporting "new" archs where linux
lacks "old" syscalls.

10 years agooverhaul siginfo_t definition in signal.h
Rich Felker [Sun, 25 May 2014 00:39:46 +0000 (20:39 -0400)]
overhaul siginfo_t definition in signal.h

the main motivation for this change is that, with the previous
definition, it was arguably illegal, in standard C, to initialize both
si_value and si_pid/si_uid with designated initializers, due to the
rule that only one member of a union can have an initializer. whether
or not this affected real-world application code, it affected some
internal code, and clang was producing warnings (and possibly
generating incorrect code).

the new definition uses a more complex hierarchy of structs and unions
to avoid the need to initialize more than one member of a single union
in usage cases that make sense. further work would be needed to
eliminate even the ones with no practical applications.

at the same time, some fixes are made to the exposed names for
nonstandard fields, to match what software using them expects.

10 years agorelease 1.1.1
Rich Felker [Tue, 20 May 2014 22:19:53 +0000 (18:19 -0400)]
release 1.1.1

10 years agotrivial formatting fix for the config.mak generated by configure
Rich Felker [Tue, 20 May 2014 19:49:21 +0000 (15:49 -0400)]
trivial formatting fix for the config.mak generated by configure

10 years agofix unhandled cases in strptime
Rich Felker [Tue, 20 May 2014 02:14:09 +0000 (22:14 -0400)]
fix unhandled cases in strptime

%C, %U, %W, and %y handling were completely missing; %C wrongly
fell-through to unrelated cases, and the rest returned failure. for
now, they all parse numbers in the proper forms and range-check the
values, but they do not store the value anywhere.

it's not clear to me whether, as "derived" fields, %U and %W should
produce any result. they certainly cannot produce a result unless the
year and weekday are also converted, but in this case it might be
desirable for them to do so. clarification is needed on the intended
behavior of strptime in cases like this.

%C and %y have well-defined behavior as long as they are used together
(and %y is defined by itself but may change in the future).
implementing them (including their correct interaction) is left as a
later change to be made.

finally, strptime now rejects unknown/invalid format characters
instead of ignoring them.

10 years agoremove unsupported nonstandard sysconf macros and their table entries
Rich Felker [Mon, 19 May 2014 16:18:16 +0000 (12:18 -0400)]
remove unsupported nonstandard sysconf macros and their table entries

some of these may have been from ancient (pre-SUSv2) POSIX versions;
more likely, they were from POSIX drafts or glibc interpretations of
what ancient versions of POSIX should have added (instead they made
they described functionality mandatory and/or dropped it completely).
others are purely glibc-isms, many of them ill-thought-out, like
providing ways to lookup the min/max values of types at runtime
(despite the impossibility of them changing at runtime and the
impossibility of representing ULONG_MAX in a return value of type
long).

since our sysconf implementation does not support or return meaningful
values for any of these, it's harmful to have the macros around;
applications' build scripts may detect and attempt to use them, only
to get -1/EINVAL as a result.

if removing them does break some applications, and it's determined
that the usage was reasonable, some of these could be added back on an
as-needed basis, but they should return actual meaningful values, not
junk like they were returning before.

10 years agorework sysconf table to treat zero entries as invalid
Rich Felker [Mon, 19 May 2014 15:19:49 +0000 (11:19 -0400)]
rework sysconf table to treat zero entries as invalid

based on patch by Timo Teräs. previously, the value zero was used as a
literal zero, meaning that all invalid sysconf "names", which should
result in sysconf returning -1, had to be explicitly listed. (in
addition, it was not possible for sysconf to set errno to EINVAL, as
there was no distinction between -1 as an error and -1 as a valid
result.)

now, the value 0 is used for invalid/undefined slots in the table and
a new switch table entry is used for returning literal zeros.

in addition, an off-by-one error in checking against the table size is
fixed.

10 years agoadd configure check for broken gcc 4.9.0 and possibly other versions
Rich Felker [Mon, 19 May 2014 14:33:28 +0000 (10:33 -0400)]
add configure check for broken gcc 4.9.0 and possibly other versions

this is gcc bug #61144. the broken compiler is detected, but the user
must manually work around it. this is partly to avoid complex logic
for adding workaround CFLAGS and attempting to recheck with them, and
partly for the sake of letting the user know the compiler is broken
(since the workaround will result in less-efficient code production).

some refactoring was also needed to move the check for gcc outside of
the check for whether to build the compiler wrapper.

10 years agoadd cp437 and cp850 to available iconv conversions
Rich Felker [Tue, 13 May 2014 23:24:48 +0000 (19:24 -0400)]
add cp437 and cp850 to available iconv conversions

perhaps some additional legacy DOS-era codepages would also be useful
to have, but these are the ones for which there has been demand. the
size of the diff is due to the fact that legacychars.h is updated in
such a way that new characters are inserted into the table in unicode
codepoint order; thus other mappings in codepages.h have changed to
reflect the new table indices of their characters.

10 years agoadd configure check for working compiler
Rich Felker [Mon, 12 May 2014 18:22:57 +0000 (14:22 -0400)]
add configure check for working compiler

without this, broken choices of CC/CPPFLAGS/CFLAGS don't show up until
late in the configure process where they are confusingly reported as a
different failure such as incorrect long double type.

10 years agofix strftime %s not to zero pad with default width=2
Szabolcs Nagy [Thu, 8 May 2014 17:04:48 +0000 (19:04 +0200)]
fix strftime %s not to zero pad with default width=2

10 years agoremove useless __yield alias for sched_yield
Rich Felker [Sun, 4 May 2014 17:15:27 +0000 (13:15 -0400)]
remove useless __yield alias for sched_yield

this is no longer used for anything, and reportedly clashed with a
builtin on certain compilers.

10 years agofix arm thread-pointer/atomic asm when compiling to thumb code
Rich Felker [Wed, 30 Apr 2014 19:32:11 +0000 (15:32 -0400)]
fix arm thread-pointer/atomic asm when compiling to thumb code

armv7/thumb2 provides a way to do atomics in thumb mode, but for armv6
we need a call to arm mode.

this commit is based on a patch by Stephen Thomas which fixed the
armv7 cases but not the armv6 ones.

all of this should be revisited if/when runtime selection of thread
pointer access and atomics are added.

10 years agofix missing SO_RCVBUFFORCE and SO_SNDBUFFORCE in mips socket.h
Rich Felker [Wed, 30 Apr 2014 18:47:06 +0000 (14:47 -0400)]
fix missing SO_RCVBUFFORCE and SO_SNDBUFFORCE in mips socket.h

10 years agofix superh nofpu check on old gcc versions
Bobby Bingham [Mon, 28 Apr 2014 02:13:59 +0000 (21:13 -0500)]
fix superh nofpu check on old gcc versions

As far as gcc3 knows, sh4 is the only processor version that can have an
FPU, so it indicates the FPU's presence by defining __SH4__.  This is not
defined if there is no FPU, even if the processor really is an SH4.

Starting with gcc4, there is support for the sh2a processor, which has an
FPU but is not an SH4.  gcc4 therefore additionally defines __SH_FPU_ANY__
when there is an FPU, but still doesn't define __SH4__ for an FPU-less sh4.

Therefore, to support all gcc versions, we must look at both preprocessor
symbols.

10 years agoperform minimal sanity checks on zoneinfo files loaded via TZ variable
Rich Felker [Wed, 23 Apr 2014 00:09:56 +0000 (20:09 -0400)]
perform minimal sanity checks on zoneinfo files loaded via TZ variable

previously, setting TZ to the pathname of a file which was not a valid
zoneinfo file would usually cause programs using local time zone based
operations to crash. the new code checks the file size and magic at
the beginning of the file, which seems sufficient to prevent
accidental misconfiguration from causing crashes. attempting to make
fully-robust validation would be futile unless we wanted to drop use
of mmap (shared zoneinfo) and instead read it into a local buffer,
since such validation would be subject to race conditions with
modification of the file.

10 years agodo not try to interpret implementation specific strings as tz definition
Timo Teräs [Tue, 22 Apr 2014 18:55:20 +0000 (21:55 +0300)]
do not try to interpret implementation specific strings as tz definition

10 years agomake __init_libc static for non-shared libc
Rich Felker [Tue, 22 Apr 2014 00:10:08 +0000 (20:10 -0400)]
make __init_libc static for non-shared libc

being static allows it to be inlined in __libc_start_main; inlining
should take place at all levels since the function is called exactly
once. this further reduces mandatory startup code size for static
binaries.

10 years agofurther micro-optimize startup code for size
Rich Felker [Mon, 21 Apr 2014 23:52:24 +0000 (19:52 -0400)]
further micro-optimize startup code for size

there is no reason (and seemingly there never was any) for
__init_security to be its own function. it's linked unconditionally
so it can just be placed inline in __init_libc.

10 years agomicro-optimize some startup code for size
Rich Felker [Mon, 21 Apr 2014 23:31:35 +0000 (19:31 -0400)]
micro-optimize some startup code for size

moving the call to __init_ssp from __init_security to __init_libc
makes __init_security a leaf function, which allows the compiler to
make it smaller. __init_libc is already non-leaf, and the additional
call makes no difference to the amount of register spillage.

in addition, it really made no sense for the call to __init_ssp to be
buried inside __init_security rather than parallel with other init
functions.

10 years agoallow zoneinfo-path-relative filenames with no slashes in TZ variable
Rich Felker [Mon, 21 Apr 2014 22:11:42 +0000 (18:11 -0400)]
allow zoneinfo-path-relative filenames with no slashes in TZ variable

since the form TZ=name is reserved for POSIX-form time zone strings,
TZ=:name needs to be used when the zoneinfo filename is in the
top-level zoneinfo directory and therefore does not contain a slash.
previously the leading colon was merely dropped, making it impossible
to access such zones without a full absolute pathname.

changes based on patch by Timo Teräs.

10 years agoexpose public execvpe interface
M Farkas-Dyck [Sat, 19 Apr 2014 03:40:28 +0000 (22:40 -0500)]
expose public execvpe interface

10 years agofix false negatives with periodic needles in strstr, wcsstr, and memmem
Rich Felker [Fri, 18 Apr 2014 21:38:35 +0000 (17:38 -0400)]
fix false negatives with periodic needles in strstr, wcsstr, and memmem

in cases where the memorized match range from the right factor
exceeded the length of the left factor, it was wrongly treated as a
mismatch rather than a match.

issue reported by Yves Bastide.

10 years agomake socketcall types common as they are same for all architectures
Timo Teräs [Thu, 17 Apr 2014 17:58:17 +0000 (20:58 +0300)]
make socketcall types common as they are same for all architectures

10 years agoadd options when explicitly invoking dynamic loader
Rich Felker [Wed, 16 Apr 2014 16:45:36 +0000 (12:45 -0400)]
add options when explicitly invoking dynamic loader

so far the options are --library-path and --preload which override the
corresponding environment variables, and --list which forces the
behavior of ldd even if the invocation name is not ldd. both the
two-arg form and the one-arg form using an equals sign are supported.

based loosely on a patch proposed by Rune.

10 years agorelease 1.1.0
Rich Felker [Wed, 16 Apr 2014 08:30:58 +0000 (04:30 -0400)]
release 1.1.0

10 years agoadd working vdso clock_gettime support, including static linking
Rich Felker [Wed, 16 Apr 2014 06:33:29 +0000 (02:33 -0400)]
add working vdso clock_gettime support, including static linking

the vdso symbol lookup code is based on the original 2011 patch by
Nicholas J. Kain, with some streamlining, pointer arithmetic fixes,
and one symbol version matching fix.

on the consumer side (clock_gettime), per-arch macros for the
particular symbol name and version to lookup are added in
syscall_arch.h, and no vdso code is pulled in on archs which do not
define these macros. at this time, vdso is enabled only on x86_64.

the vdso support at the dynamic linker level is no longer useful to
libc, but is left in place for the sake of debuggers (which may need
the vdso in the link map to find its functions) and possibly use with
dlsym.

10 years agofix deadlock race in pthread_once
Rich Felker [Wed, 16 Apr 2014 00:42:39 +0000 (20:42 -0400)]
fix deadlock race in pthread_once

at the end of successful pthread_once, there was a race window during
which another thread calling pthread_once would momentarily change the
state back from 2 (finished) to 1 (in-progress). in this case, the
status was immediately changed back, but with no wake call, meaning
that waiters which arrived during this short window could block
forever. there are two possible fixes. one would be adding the wake to
the code path where it was missing. but it's better just to avoid
reverting the status at all, by using compare-and-swap instead of
swap.

10 years agofix RLIMIT_ constants for mips
Szabolcs Nagy [Mon, 14 Apr 2014 15:42:49 +0000 (17:42 +0200)]
fix RLIMIT_ constants for mips

The mips arch is special in that it uses different RLIMIT_
numbers than other archs, so allow bits/resource.h to override
the default RLIMIT_ numbers (empty on all archs except mips).
Reported by orc.