Commit Graph

58 Commits

Author SHA1 Message Date
Rich Felker babf820180 mips dynamic linker support
not heavily tested, but the basics are working. the basic concept is
that the dynamic linker entry point code invokes a pure-PIC (no global
accesses) C function in reloc.h to perform the early GOT relocations
needed to make the dynamic linker itself functional, then invokes
__dynlink like on other archs. since mips uses some ugly arch-specific
hacks to optimize relocating the GOT (rather than just using the
normal DT_REL[A] tables like on other archs), the dynamic linker has
been modified slightly to support calling arch-specific relocation
code in reloc.h.

most of the actual mips-specific behavior was developed by reading the
output of readelf on libc.so and simple executable files. i could not
find good reference information on which relocation types need to be
supported or their semantics, so it's possible that some legitimate
usage cases will not work yet.
2012-08-05 12:50:26 -04:00
Rich Felker 87d13a4c33 more cleanup of dynamic linker internals 2012-08-05 02:49:02 -04:00
Rich Felker 7cb44cd3de more dynamic linker internals cleanup
changing the string printed for the dso name is not a regression; the
old code was simply using the wrong dso name (head rather than the dso
currently being relocated). this will be fixed in a later commit.
2012-08-05 02:44:32 -04:00
Rich Felker 05eff01e89 dynamic linker internals cleanup 2012-08-05 02:38:35 -04:00
Rich Felker 649cec5f98 make dynamic linker tell the debugger its own pathname
use the main program's PT_INTERP header if possible, since this is
sure to be a correct (and hopefully absolute) pathname.
2012-07-13 01:31:02 -04:00
Rich Felker e864a29090 make dynamic linker depend on -DSHARED not -fPIC
if libc.a is compiled PIC for use in static PIE code, this should not
cause the dynamic linker (which still does not support static-linked
main program) to be built into libc.a.
2012-07-11 01:47:30 -04:00
Rich Felker 0420b87446 fix lots of breakage on dlopen, mostly with explicit pathnames
most importantly, the name for such libs was being set from an
uninitialized buffer. also, shortname always had an initial '/'
character, making it useless for looking up already-loaded libraries
by name, and thus causing repeated searches through the library path.

major changes now:

- shortname is the base name for library lookups with no explicit
  pathname. it's initially clear for libraries loaded with an explicit
  pathname (and for the main program), but will be set if the same
  library (detected via inodes match) is later found by a search.

- exact name match is never used to identify libraries loaded with an
  explicit pathname. in this case, there's no explicit search, so we
  can just stat the file and check for inode match.
2012-07-11 01:41:20 -04:00
Rich Felker d93e028c6b fix dlsym RTLD_NEXT support
previously this was being handled the same as a library-specific,
dependency-order lookup on the next library in the global chain, which
is likely to be utterly meaningless. instead the lookup needs to be in
the global namespace, but omitting the initial portion of the global
library chain up through the calling library.
2012-07-07 16:32:27 -04:00
Rich Felker 6343ac8f5a fix char signedness bug (arm-specific) in dynamic linker 2012-06-09 21:20:44 -04:00
Rich Felker f7d15dcc54 treat failure of mprotect in map_library as a fatal load failure
the error will propagate up and be printed to the user at program
start time; at runtime, dlopen will just fail and leave a message for
dlerror.

previously, if mprotect failed, subsequent attempts to perform
relocations would crash the program. this was resulting in an
increasing number of false bug reports on grsec systems where rwx
permission is not possible in cases where users were wrongly
attempting to use non-PIC code in shared libraries. supporting that
usage is in theory possible, but the x86_64 toolchain does not even
support textrels, and the cost of keeping around the necessary
information to handle textrels without rwx permissions is
disproportionate to the benefit (which is essentially just supporting
broken library setups on grsec machines).

also, i unified the error-out code in map_library now that there are 3
places from which munmap might have to be called.
2012-06-06 11:21:28 -04:00
Rich Felker 5c1909a8d2 add ldd and main program loading support to dynamic linker 2012-05-27 16:01:44 -04:00
Rich Felker 4027f4e8f9 fix error reporting for dlsym with global symbols 2012-05-04 20:18:18 -04:00
Rich Felker 58aa5f45ed overhaul SSP support to use a real canary
pthread structure has been adjusted to match the glibc/GCC abi for
where the canary is stored on i386 and x86_64. it will need variants
for other archs to provide the added security of the canary's entropy,
but even without that it still works as well as the old "minimal" ssp
support. eventually such changes will be made anyway, since they are
also needed for GCC/C11 thread-local storage support (not yet
implemented).

care is taken not to attempt initializing the thread pointer unless
the program actually uses SSP (by reference to __stack_chk_fail).
2012-05-03 20:42:45 -04:00
Rich Felker 3ec8d29c75 gdb shared library debugging support
provide the minimal level of dynamic linker-to-debugger glue needed to
let gdb find loaded libraries and load their symbols.
2012-04-25 00:05:42 -04:00
Rich Felker 60872cf9c9 first attempt at enabling stack protector support
the code is written to pre-init the thread pointer in static linked
programs that pull in __stack_chk_fail or dynamic-linked programs that
lookup the symbol. no explicit canary is set; the canary will be
whatever happens to be in the thread structure at the offset gcc
hard-coded. this can be improved later.
2012-04-24 18:07:59 -04:00
Rich Felker a5d10eb1f5 make dlerror produce informative results
note that dlerror is specified to be non-thread-safe, so no locking is
performed on the error flag or message aside from the rwlock already
held by dlopen or dlsym. if 2 invocations of dlsym are generating
errors at the same time, they could clobber each other's results, but
the resulting string, albeit corrupt, will still be null-terminated.
any use of dlerror in such a situation could not be expected to give
meaningful results anyway.
2012-04-23 12:03:31 -04:00
Rich Felker a9e85c0a5c make dlerror conform to posix
the error status is required to be sticky after failure of dlopen or
dlsym until cleared by dlerror. applications and especially libraries
should never rely on this since it is not thread-safe and subject to
race conditions, but glib does anyway.
2012-03-23 00:28:20 -04:00
Rich Felker f2baf4d7b8 protect against cancellation in dlopen
i'm not sure that it's "correct" for dlopen to block cancellation
when calling constructors for libraries it loads, but it sure seems
like the right thing. in any case, dlopen itself needs cancellation
blocked.
2012-02-07 20:31:27 -05:00
Rich Felker 700a8156ad reduce some wasted space in dso structure 2012-02-07 20:29:29 -05:00
Rich Felker ce4d97e3dc run ctors/dtors for shared objects loaded with dlopen 2012-02-06 17:57:29 -05:00
Rich Felker 4ce3cb5cdd add support for init/finit (constructors and destructors)
this is mainly in hopes of supporting c++ (not yet possible for other
reasons) but will also help applications/libraries which use (and more
often, abuse) the gcc __attribute__((__constructor__)) feature in "C"
code.

x86_64 and arm versions of the new startup asm are untested and may
have minor problems.
2012-02-06 14:39:09 -05:00
Rich Felker 5a09a53010 include dummied-out dlopen and dlsym functions for static binaries
these don't work (or do anything at all) but at least make it possible
to static link programs that insist on "having" dynamic loading
support...as long as they don't actually need to use it.

adding real support for dlopen/dlsym with static linking is going to
be significantly more difficult...
2012-02-03 03:16:07 -05:00
Rich Felker fd7015d0c2 fix broken copy relocations from dynamic linker cleanup
this issue affected programs which use global variables exported by
non-libc libraries.
2012-01-23 18:32:40 -05:00
Rich Felker e12fe65c92 dynamic linker support for PIE binaries (position-independent main program)
even with this change, PIE will not work yet due to deficiencies in
the crt1.o startup code.
2012-01-23 02:02:59 -05:00
Rich Felker c82f4a32ec cleanup dynamic linker, removing some code duplication 2012-01-23 00:57:38 -05:00
Rich Felker 0b6dc09744 fix dynamic linker not to depend on DYNAMIC ptr in 0th entry of GOT
this fixes an issue using gold instead of gnu ld for linking. it also
should eliminate the need of the startup code to even load/pass the
got address to the dynamic linker.

based on patch submitted by sh4rm4 with minor cosmetic changes.

further cleanup will follow.
2012-01-20 11:14:27 -05:00
Rich Felker 2adf2fb372 fix char signedness bug in dynlinker hash function
this only affects non-ascii symbol names, which are probably not in
use anyway..
2012-01-17 00:34:58 -05:00
Rich Felker 4f4bf0ad2e disable dynamic linking/loading code in static libc builds, for now
it does not work, but some configure scripts will falsely detect
support then generate programs that crash when they call dlopen.
2011-09-18 16:42:06 -04:00
Rich Felker cf8506ad94 ldso: move the suid/secure check code closer to env/auxv processing
this does not change behavior, but the idea is to avoid letting other
code build up between these two points, whereby the environment
variables might get used before security it checked.
2011-08-16 08:50:03 -04:00
Rich Felker a045883365 honor AT_SECURE aux vector flag 2011-08-16 07:46:42 -04:00
Rich Felker 623753ad64 RTLD_NEXT support
the asm wrapper is needed to get the return address without
compiler-specific extensions.
2011-08-16 00:42:13 -04:00
Rich Felker 2719cc8628 LD_PRELOAD support 2011-08-16 00:24:36 -04:00
Rich Felker e01ac67599 when resolving symbols with only weak defs, use first def, not last def 2011-07-25 09:22:05 -04:00
Rich Felker 427173b932 fix resolution of weak symbols (hopefully right now) and vdso 2011-07-24 02:19:47 -04:00
Rich Felker 6ab444d97a load vdso, if present, into the dso list 2011-07-24 00:54:55 -04:00
Rich Felker a53de812d2 simplify dynamic linker startup
instead of creating temp dso objects on the stack and moving them to
the heap if dlopen/dlsym are used, use static objects to begin with,
and just donate them to malloc if we no longer need them.
2011-07-24 00:26:12 -04:00
Rich Felker 2fdea17c3d fix dlopen UB due to longjmp/volatile rules violation 2011-07-01 22:40:00 -04:00
Rich Felker 191ebcac31 simple rpath support (no token expansion yet) for dynamic linker 2011-06-30 23:02:27 -04:00
Rich Felker 9f17413c75 textrel support, cheap and ugly 2011-06-29 00:29:08 -04:00
Rich Felker 6717e62ac0 reclaim the memory wasted by dynamic linking for use by malloc 2011-06-28 19:40:14 -04:00
Rich Felker bf30100ad7 use load address from elf header if possible
this is mostly useless for shared libs (though it could help for
prelink-like purposes); the intended use case is for adding support
for calling the dynamic linker directly to run a program, as in:
./libc.so ./a.out foo

this usage is not yet supported.
2011-06-28 14:20:41 -04:00
Rich Felker c7debe13ee make dynamic linker relocate the main program image last, after all libs
prior to this change, copy relocations for initialized pointer
variables would not reflect the relocated contents of the pointer.
2011-06-28 14:13:51 -04:00
Rich Felker 9e17b71fa6 fix stale pointer issue in dynamic linker with dlopen 2011-06-26 22:39:34 -04:00
Rich Felker 06933cc724 don't leave the lock held on dlopen failure.. 2011-06-26 22:09:32 -04:00
Rich Felker 97507bde47 add RTLD_DEFAULT support 2011-06-26 21:50:01 -04:00
Rich Felker 0e4dae3d77 in dlopen: don't use null pointer
deps can be null if a library has no dependencies (such as libc itself)
2011-06-26 21:36:44 -04:00
Rich Felker 92ab5d8d15 fix resolving symbols in objects loaded in RTLD_LOCAL mode
basically we temporarily make the library and all its dependencies
part of the global namespace but only for the duration of performing
relocations, then return them to their former state.
2011-06-26 21:21:04 -04:00
Rich Felker 59ab43f5f8 experimental dlopen/dlsym and dynlink changes needed to support them 2011-06-26 19:23:28 -04:00
Rich Felker 6b3d5e508f error handling in dynamic linking
some of the code is not yet used, and is in preparation for dlopen
which needs to be able to handle failure loading libraries without
terminating the program.
2011-06-26 17:39:17 -04:00
Rich Felker 32de61e81a fix some symbol resolution issues in dynamic linker
1. search was wrongly beginning with lib itself rather than dso head
2. inconsistent resolution of function pointers for functions in plt
2011-06-25 22:36:21 -04:00