Commit Graph

3102 Commits

Author SHA1 Message Date
Rich Felker c87a521033 move calls to application init functions after crt1 entry point
this change is needed to be compatible with fdpic, where some of the
main application's relocations may be performed as part of the crt1
entry point. if we call init functions before passing control, these
relocations will not yet have been performed, and the init code will
potentially make use of invalid pointers.

conceptually, no code provided by the application or third-party
libraries should run before the application entry point. the
difference is not observable to programs using the crt1 we provide,
but it could come into play if custom entry point code is used, so
it's better to be doing this right anyway.
2015-09-22 20:24:28 +00:00
Rich Felker 78f430295c fix breakage in non-fdpic dynamic linker init/fini processing
a mistaken #ifdef instead of #if caused conversion of code addresses
to function descriptors to be performed even on non-fdpic.
2015-09-22 20:20:39 +00:00
Rich Felker 30fdc06bba fix resolving interp string address on fdpic ldd command 2015-09-22 19:21:57 +00:00
Rich Felker eaf7ab6e24 add real fdpic loading of shared libraries
previously, the normal ELF library loading code was used even for
fdpic, so only the kernel-loaded dynamic linker and main app could
benefit from separate placement of segments and shared text.
2015-09-22 19:12:48 +00:00
Rich Felker 2462370b4f try to suppress linking libc.so if there are undefined symbols
this is always an error and usually results from failure to find/link
the compiler runtime library, but it could also result from
implementation errors in libc, using functions that don't (yet) exist.
either way the resulting libc.so will crash mysteriously at runtime.
the crash happens too early to produce a meaningful error, so these
crashes are very confusing to users and waste a lot of debugging time.
this commit should ensure that they do not happen.
2015-09-22 15:45:40 +00:00
Rich Felker c42650abb0 remove configure's suppression of enable sh/fdpic shared library build 2015-09-22 04:35:27 +00:00
Rich Felker 7f9086df95 size-optimize sh/fdpic dynamic entry point
the __fdpic_fixup code is not needed for ET_DYN executables, which
instead use reloctions, so we can omit it from the dynamic linker and
static-pie entry point and save some code size.
2015-09-22 04:14:07 +00:00
Rich Felker cab2b1f9d7 work around breakage in sh/fdpic __unmapself function
the C implementation of __unmapself used for potentially-nommu sh
assumed CRTJMP takes a function descriptor rather than a code address;
however, the actual dynamic linker needs a code address, and so commit
7a9669e977 changed the definition of the
macro in reloc.h. this commit puts the old macro back in a place where
it only affects __unmapself.

this is an ugly workaround and should be cleaned up at some point, but
at least it's well isolated.
2015-09-22 04:10:42 +00:00
Rich Felker 7a9669e977 add general fdpic support in dynamic linker and arch support for sh
at this point not all functionality is complete. the dynamic linker
itself, and main app if it is also loaded by the kernel, take
advantage of fdpic and do not need constant displacement between
segments, but additional libraries loaded by the dynamic linker follow
normal ELF semantics for mapping still. this fully works, but does not
admit shared text on nommu.

in terms of actual functional correctness, dlsym's results are
presently incorrect for function symbols, RTLD_NEXT fails to identify
the caller correctly, and dladdr fails almost entirely.

with the dynamic linker entry point working, support for static pie is
automatically included, but linking the main application as ET_DYN
(pie) probably does not make sense for fdpic anyway. ET_EXEC is
equally relocatable but more efficient at representing relocations.
2015-09-22 03:54:42 +00:00
Rich Felker 3958144ede factor symbol counting out of dladdr as its own function
the fdpic code will need to count symbols, and it may be useful
elsewhere in the future too. counting is trivial as long as sysv hash
is present, but for gnu-hash-only libraries it's complex.

the behavior of the count is changed slightly: we now include symbols
that are not accessible by the gnu hash table in the count. this may
make dladdr slightly slower. if this is a problem, dladdr can subtract
out the part that should not be accessible. unlike in the old code,
subtracting this out is easy even in the fast path where sysv hash is
available too.
2015-09-21 21:47:50 +00:00
Rich Felker d874064579 simplify dlstart code by using integer type for base address 2015-09-21 20:42:44 +00:00
Rich Felker 2a54733820 refactor some more dynamic linker load address computations
these were just missed in the previous commits.
2015-09-17 19:45:45 +00:00
Rich Felker e6076c99dd remove some useless casts in dynamic linker 2015-09-17 19:21:55 +00:00
Rich Felker eb567c12cd add fdpic structs and reloc types for dynamic linking 2015-09-17 18:51:57 +00:00
Rich Felker a735f53e6a further refactoring of dynamic linker load address computations
these are in do_relocs. the first one was omitted in commit
301335a80b because it slightly changes
code (using dso->base rather than cached local var base) and would
have prevented easy verification. the other was an oversight.
2015-09-17 17:50:43 +00:00
Rich Felker 301335a80b begin refactoring load address computations in dynamic linker
for ordinary ELF with fixed segment displacements, load address
computation is simply adding the base load address. but for FDPIC,
each segment has its own load address, and virtual addresses need to
be adjusted according to the segment they fall in. abstracting this
computation is the first step to making the dynamic linker ready for
FDPIC.

for this first commit, a macro is used rather than a function in order
to facilitate correctness checking. I have verified that the generated
code does not change on my i386 build.
2015-09-17 17:18:09 +00:00
Rich Felker 6fc30c2493 remove old dlstart stage-2 symbolic lookup code; add new generic
this new generic version of the stage-2 function lookup should work
for any arch where static data is accessible via got-relative or
pc-relative addressing, using approximately the technique described in
the log message for commit 2907afb8db.

since all the mips-like archs that need got slots fo access static
data have already transitioned to the new stage chaining scheme, the
old dynamic symbol lookup code is now removed.

aarch64, arm, and sh have not yet transitioned; with this commit, they
are now using the new generic code.
2015-09-17 08:16:24 +00:00
Rich Felker 12b0b7d8ea new dlstart stage-2 chaining for x86_64 and x32 2015-09-17 07:28:44 +00:00
Rich Felker c16182680c new dlstart stage-2 chaining for powerpc 2015-09-17 07:20:58 +00:00
Rich Felker 4761e63bc4 new dlstart stage-2 chaining for or1k 2015-09-17 07:20:51 +00:00
Rich Felker cd7159e7be new dlstart stage-2 chaining for mips 2015-09-17 07:20:43 +00:00
Rich Felker 57e2dce7e4 new dlstart stage-2 chaining for microblaze 2015-09-17 07:20:36 +00:00
Rich Felker 2907afb8db introduce new symbol-lookup-free rcrt1/dlstart stage chaining
previously, the call into stage 2 was made by looking up the symbol
name "__dls2" (which was chosen short to be easy to look up) from the
dynamic symbol table. this was no problem for the dynamic linker,
since it always exports all its symbols. in the case of the static pie
entry point, however, the dynamic symbol table does not contain the
necessary symbol unless -rdynamic/-E was used when linking. this
linking requirement is a major obstacle both to practical use of
static-pie as a nommu binary format (since it greatly enlarges the
file) and to upstream toolchain support for static-pie (adding -E to
default linking specs is not reasonable).

this patch replaces the runtime symbolic lookup with a link-time
lookup via an inline asm fragment, which reloc.h is responsible for
providing. in this initial commit, the asm is provided only for i386,
and the old lookup code is left in place as a fallback for archs that
have not yet transitioned.

modifying crt_arch.h to pass the stage-2 function pointer as an
argument was considered as an alternative, but such an approach would
not be compatible with fdpic, where it's impossible to compute
function pointers without already having performed relocations. it was
also deemed desirable to keep crt_arch.h as simple/minimal as
possible.

in principle, archs with pc-relative or got-relative addressing of
static variables could instead load the stage-2 function pointer from
a static volatile object. that does not work for fdpic, and is not
safe against reordering on mips-like archs that use got slots even for
static functions, but it's a valid on i386 and many others, and could
provide a reasonable default implementation in the future.
2015-09-17 06:30:55 +00:00
Rich Felker a603a75a72 remove attribute((const)) from pthread_self and errno location decls
this attribute was applied to pthread_self and the functions providing
the locations for errno and h_errno as an optimization; however, it is
subtly incorrect. as specified, it means the return value will always
be the same, which is not true; it varies per-thread.

this attribute also implies that the function does not depend on any
state, and that calls to it can safely be reordered across any other
code. however such reordering is unsafe for these functions: they
break when reordered before initialization of the thread pointer. such
breakage was actually observed when compiled by libfirm/cparser.

to some extent the reordering problem could be solved with strong
compiler barriers between the stages of early startup code, but the
specified meaning of of attribute((const)) is sufficiently strong that
a compiler would theoretically be justified inserting gratuitous calls
to attribute((const)) const functions at random locations (e.g. to
save the value in static storage for later use).

this reverts commit cbf35978a9.
2015-09-17 04:45:01 +00:00
Khem Raj ccc71e0ea8 add format argument attributes to gettext function prototypes
their absence completely breaks format string warnings in programs
with gettext message translations: -Wformat gives no results, and
-Wformat-nonliteral produces spurious warnings.

with gcc, the problem manifests only in standards-conforming profiles;
otherwise gcc sets these attributes by default for the gettext family.
with clang, the problem always manifests; clang has no such defaults.
2015-09-15 20:30:36 +00:00
Felix Janda 64b6684ddd reindent powerpc's bits/termios.h to be consistent with other archs 2015-09-15 14:30:08 -04:00
Felix Janda b291e7ca9b fix namespace violations in aarch64/bits/termios.h
in analogy with commit a627eb3586
2015-09-15 14:28:07 -04:00
Rich Felker d4c82d05b8 add sh fdpic subarch variants
with this commit it should be possible to produce a working
static-linked fdpic libc and application binaries for sh.

the changes in reloc.h are largely unused at this point since dynamic
linking is not supported, but the CRTJMP macro is used one place
outside of dynamic linking, in __unmapself.
2015-09-12 03:23:49 +00:00
Rich Felker 4ccc1a01e0 add fdpic version of entry point code for sh
this version of the entry point is only suitable for static linking in
ET_EXEC form. neither dynamic linking nor pie is supported yet. at
some point in the future the fdpic and non-fdpic versions of this code
may be unified but for now it's easiest to work with them separately.
2015-09-12 03:18:08 +00:00
Rich Felker 6d03c4ee58 provide arch-generic fdpic self-relocation code for crt1 to use
this file is intended to be included by crt_arch.h on fdpic-based
targets and needs to be called from the entry point asm.
2015-09-12 03:10:44 +00:00
Rich Felker 234c58467c make sh clone asm fdpic-compatible
clone calls back to a function pointer provided by the caller, which
will actually be a pointer to a function descriptor on fdpic. the
obvious solution is to have a separate version of clone for fdpic, but
I have taken a simpler approach to go around the problem. instead of
calling the pointed-to function from asm, a direct call is made to an
internal C function which then calls the pointed-to function. this
lets the C compiler generate the appropriate calling convention for an
indirect call with no need for ABI-specific assembly.
2015-09-12 02:55:28 +00:00
Rich Felker ad5d8a2bf3 make sh crti/crtn init/fini fragments setup proper stack frame
for fdpic support is is essential that the got pointer be saved at a
known, ABI-dictated offset from the frame pointer, since there is no
way to recover it once it's lost.
2015-09-12 02:50:28 +00:00
Rich Felker 17cdbc4e42 add sh relocation types needed for fdpic to elf.h 2015-09-12 02:49:02 +00:00
Timo Teräs cb1c88d42b fix uninitialized scopeid in lookups from hosts file and ip literals 2015-09-11 06:15:06 +00:00
Rich Felker 878887c50c fix missing earlyclobber flag in i386 a_ctz_64 asm
this error was only found by reading the code, but it seems to have
been causing gcc to produce wrong code in malloc: the same register
was used for the output and the high word of the input. in principle
this could have caused an infinite loop searching for an available
bin, but in practice most x86 models seem to implement the "undefined"
result of the bsf instruction as "unchanged".
2015-09-09 07:18:28 +00:00
Rich Felker deb85ab44d remove unused (and invalid) C version of sigsetjmp
originally, the comment in this code was correct and it would likely
work if the compiler generated a tail call to setjmp. however, commit
583e55122e redesigned sigsetjmp and
siglongjmp such that the old C implementation (which was not intended
to be used) is not even conceptually correct. remove it in the
interest of avoiding confusion when porting to new archs.
2015-09-09 06:59:45 +00:00
Rich Felker 58f6259dff fix breakage in nl_langinfo from previous commit 2015-09-09 06:04:42 +00:00
Rich Felker 844212d94f make nl_langinfo(CODESET) always return "UTF-8"
this restores the original behavior prior to the addition of the
byte-based C locale and fixes what is effectively a regression in
musl's property of always providing working UTF-8 support.

commit 1507ebf837 introduced the codeset
name "UTF-8-CODE-UNITS" for the byte-based C locale to represent that
the semantic content is UTF-8 but that it is being processed as code
units (bytes) rather than whole multibyte characters. however, many
programs assume that the codeset name is usable with iconv and/or
comes from a set of standard/widely-used names known to the
application. such programs are likely to produce warnings or errors,
run with reduced functionality, or mangle character data when run
explicitly in the C locale.

the standard places basically no requirements for the string returned
by nl_langinfo(CODESET) and how it interacts with other interfaces, so
returning "UTF-8" is permissible. moreover, it seems like the right
thing to do, since the identity of the character encoding as "UTF-8"
is independent of whether it is being processed as bytes of characters
by the standard library functions.
2015-09-09 05:13:33 +00:00
Rich Felker 426a0e2912 fix fclose of permanent (stdin/out/err) streams
this fixes a bug reported by Nuno Gonçalves. previously, calling
fclose on stdin or stdout resulted in deadlock at exit time, since
__stdio_exit attempts to lock these streams to flush/seek them, and
has no easy way of knowing that they were closed.

conceptually, leaving a FILE stream locked on fclose is valid since,
in the abstract machine, it ceases to exist. but to satisfy the
implementation-internal assumption in __stdio_exit that it can access
these streams unconditionally, we need to unlock them.

it's also necessary that fclose leaves permanent streams in a state
where __stdio_exit will not attempt any further operations on them.
fortunately, the call to fflush already yields this property.
2015-09-09 04:31:07 +00:00
Timo Teräs d8be1bc019 implement arm eabi mem* functions
these functions are part of the ARM EABI, meaning compilers may
generate references to them. known versions of gcc do not use them,
but llvm does. they are not provided by libgcc, and the de facto
standard seems to be that libc provides them.
2015-08-31 06:35:01 +00:00
Rich Felker d18cf76d73 remove use of buggy .SECONDARY special target in makefile
this functionality is affected by GNU make bug #30653, "intermediate
files incorrectly pruned in parallel builds". on affected versions of
make, parallel builds attempt to compile source files before
alltypes.h is generated.

as noted with commit a91ebdcfac, which
added the use of .SECONDARY, suppression of removal of "intermediate"
files does not seem to be needed at present. if it is needed in the
future, it should be achievable by explicitly mentioning their names
as targets or prerequisites.
2015-08-30 18:44:58 +00:00
Rich Felker 650b04f8cc release 1.1.11 2015-08-30 04:15:56 +00:00
Rich Felker a91ebdcfac fix makefile suppression of intermediate file removal
at one point, GNU make was removing crt/*.o after producing the copies
in lib/ due to an arcane misfeature for handling "intermediate" files.
the circumstances that caused this are no longer present in our
makefile, but the previous workaround using .PRECIOUS was wrong and
could result in corrupt/partial files being left behind during an
interrupted build. using .SECONDARY is the correct, documented fix
that will prevent deletion of "intermediate" files from ever
resurfacing.
2015-08-27 19:58:20 +00:00
Alex Dowad 35b3312b6f Build process uses script to add CFI directives to x86 asm
Some functions implemented in asm need to use EBP for purposes other
than acting as a frame pointer. (Notably, it is used for the 6th
argument to syscalls with 6 arguments.) Without frame pointers, GDB
can only show backtraces if it gets CFI information from a
.debug_frame or .eh_frame ELF section.

Rather than littering our asm with ugly .cfi directives, use an awk
script to insert them in the right places during the build process, so
GDB can keep track of where the current stack frame is relative to the
stack pointer. This means GDB can produce beautiful stack traces at
any given point when single-stepping through asm functions.

Additionally, when registers are saved on the stack and later
overwritten, emit ..cfi directives so GDB will know where they were
saved relative to the stack pointer. This way, when you look back up
the stack from within an asm function, you can still reliably print
the values of local variables in the caller.

If this awk script were to understand every possible wild and crazy
contortion that an asm programmer can do with the stack and registers,
and always emit the exact ..cfi directives needed for GDB to know what
the register values were in the preceding stack frame, it would
necessarily be as complex as a full x86 emulator. That way lies
madness.

Hence, we assume that the stack pointer will _only_ ever be adjusted
using push/pop or else add/sub with a constant. We do not attempt to
detect every possible way that a register value could be saved for
later use, just the simple and common ways.

Thanks to Szabolcs Nagy for suggesting numerous improvements to this
code.
2015-08-26 14:55:13 +00:00
Steven Barth aa71ec3db8 getsubopt: don't include leading = in value string
getsubopt incorrectly returns the delimiting = in the value string,
this patch fixes it by increasing the pointer position by one.

Signed-off-by: Steven Barth <cyrus@openwrt.org>
2015-08-21 01:29:10 -04:00
Rich Felker 5a9c8c05a5 mitigate performance regression in libc-internal locks on x86_64
commit 3c43c0761e fixed missing
synchronization in the atomic store operation for i386 and x86_64, but
opted to use mfence for the barrier on x86_64 where it's always
available. however, in practice mfence is significantly slower than
the barrier approach used on i386 (a nop-like lock orl operation).
this commit changes x86_64 (and x32) to use the faster barrier.
2015-08-16 18:15:18 +00:00
Natanael Copa c13f2af1fe match historical behavior for tm_gmtoff member of struct tm
tm_gmtoff is a nonstandard field, but on historical systems which have
this field, it stores the offset of the local time zone from GMT or
UTC. this is the opposite of the POSIX extern long timezone object and
the offsets used in POSIX-form TZ strings, which represent the offset
from local time to UTC. previously we were storing these negated
offsets in tm_gmtoff too.

programs which only used this field indirectly via strftime were not
affected since strftime performed the negation for presentation.
however, some programs and libraries accesse tm_gmtoff directly and
were obtaining negated time zone offsets.
2015-08-14 00:47:46 +00:00
Szabolcs Nagy e5b086e1d5 aarch64: fix 64-bit syscall argument passing
On 32bit systems long long arguments are passed in a special way
to some syscalls; this accidentally got copied to the AArch64 port.

The following interfaces were broken: fallocate, fanotify, ftruncate,
posix_fadvise, posix_fallocate, pread, pwrite, readahead,
sync_file_range, truncate.
2015-08-11 23:11:57 +00:00
Rich Felker 062bb737de fix failure of tempnam to null-terminate result
tempnam uses an uninitialized buffer which is filled using memcpy and
__randname. It is therefore necessary to explicitly null-terminate it.

based on patch by Felix Janda.
2015-08-09 22:51:09 +00:00
Rich Felker c3761622e8 mitigate blow-up of heap size under malloc/free contention
during calls to free, any free chunks adjacent to the chunk being
freed are momentarily held in allocated state for the purpose of
merging, possibly leaving little or no available free memory for other
threads to allocate. under this condition, other threads will attempt
to expand the heap rather than waiting to use memory that will soon be
available. the race window where this happens is normally very small,
but became huge when free chooses to use madvise to release unused
physical memory, causing unbounded heap size growth.

this patch drastically shrinks the race window for unwanted heap
expansion by performing madvise with the bin lock held and marking the
bin non-empty in the binmask before making the expensive madvise
syscall. testing by Timo Teräs has shown this approach to be a
suitable mitigation.

more invasive changes to the synchronization between malloc and free
would be needed to completely eliminate the problem. it's not clear
whether such changes would improve or worsen typical-case performance,
or whether this would be a worthwhile direction to take malloc
development.
2015-08-07 19:19:49 +00:00