Commit Graph

437 Commits

Author SHA1 Message Date
Konstantin Belousov
68f1db20b3 Rtld on diet part 2:
Do not use stdio for libmap.conf read.  Directly map the file and
parse lines from the mappings.

Reviewed by:	kan
MFC after:	3 weeks
2012-03-12 12:16:08 +00:00
Konstantin Belousov
ef5cdcbd69 Rtld on diet part 1:
Provide rtld-private implementations of __stack_chk_guard,
__stack_chk_fail() and __chk_fail() symbols, to be used by functions
linked from libc_pic.a.  This avoids use of libc stack_protector.c,
which pulls in syslog(3) and stdio as dependency.

Also, do initialize rtld-private copy __stack_chk_guard, previously
libc-provided one was not initialized, since we do not call rtld
object _init() methods.

Reviewed by:	kan
MFC after:	3 weeks
2012-03-12 12:15:47 +00:00
Dimitry Andric
3c5b7c6fb9 Amend r232857, now dropping the casts entirely, as they were not
necessary at all.

Submitted by:	stefanf
2012-03-12 11:22:23 +00:00
Dimitry Andric
addfbd03b7 Fix the following warning/error with clang:
libexec/rtld-elf/rtld.c:1898:22: error: comparison between pointer and integer ('Elf_Addr *' (aka 'unsigned int *') and 'Elf_Addr' (aka 'unsigned int')) [-Werror]
    if (preinit_addr == (Elf_Addr)NULL)
        ~~~~~~~~~~~~ ^  ~~~~~~~~~~~~~~
libexec/rtld-elf/rtld.c:2039:16: error: comparison between pointer and integer ('Elf_Addr *' (aka 'unsigned int *') and 'Elf_Addr' (aka 'unsigned int')) [-Werror]
        if (init_addr != (Elf_Addr)NULL) {
            ~~~~~~~~~ ^  ~~~~~~~~~~~~~~

Reviewed by:	kib
2012-03-12 11:04:48 +00:00
Konstantin Belousov
5eab36f290 When iterating over the dso program headers, the object is not initialized
yet, and object segments are not yet mapped.  Only parse the notes that
appear in the first page of the dso (as it should be anyway), and use
the preloaded page content.

Reported and tested by:	stass
MFC after:	20 days
2012-03-12 10:36:03 +00:00
Konstantin Belousov
83aa9cc00c Add support for preinit, init and fini arrays. Some ABIs, in
particular on ARM, do require working init arrays.

Traditional FreeBSD crt1 calls _init and _fini of the binary, instead
of allowing runtime linker to arrange the calls.  This was probably
done to have the same crt code serve both statically and dynamically
linked binaries.  Since ABI mandates that first is called preinit
array functions, then init, and then init array functions, the init
have to be called from rtld now.

To provide binary compatibility to old FreeBSD crt1, which calls _init
itself, rtld only calls intializers and finalizers for main binary if
binary has a note indicating that new crt was used for linking.  Add
parsing of ELF notes to rtld, and cache p_osrel value since we parsed
it anyway.

The patch is inspired by init_array support for DragonflyBSD, written
by John Marino.

Reviewed by:	kan
Tested by:	andrew (arm, previous version), flo (sparc64, previous version)
MFC after:	3 weeks
2012-03-11 20:03:09 +00:00
Konstantin Belousov
6bfe9e8ccf Optimize tls_get_addr_common(). The change provides around 30% speedup
for TLS microbenchmark using global-dynamic TLS model on amd64 (which is
default for PIC dso objects).

Split the slow path into tls_get_addr_slow(), for which inlining is
disabled. This prevents the registers spill on tls_get_addr_common()
entry.

Provide static branch hint to the compiler, indicating that slow path
is not likely to be taken.

While there, do some minimal style adjustments.

Reported and tested by:	davidxu
MFC after:	1 week
2012-03-10 08:49:44 +00:00
Konstantin Belousov
5983b87199 Remove the use of toupper() from rtld_printf.c. Use of the libc function
relies on working TLS, which is particulary not true for LD_DEBUG uses.

MFC after:	1 week
2012-03-09 16:21:40 +00:00
Sergey Kandaurov
44976acae3 Cosmetic nit:
- rename isspace1() macro to the more appropriate rtld_isspace().

Discussed with:	kib
2012-03-06 09:34:30 +00:00
Oleksandr Tymoshenko
0a0f480835 - Switch to saving non-offseted pointer to TLS block in order too keep things simple 2012-03-06 03:27:08 +00:00
Konstantin Belousov
b36070f555 The libmap.conf initialization is performed before TLS is functional.
Since after r232498 the ctype macros require working access to
thread-local variables, rtld crashes when libmap.conf is present.

Use hand-made isspace1() macro which is enough to detect spaces in
libmap.conf.

Reported by:	alc, lme, many on current@
Tested by:	lme
Reviewed by:	dim, kan
MFC after:	1 week
2012-03-05 20:43:06 +00:00
Oleksandr Tymoshenko
dc3b6c3aa0 Add thread-local storage support for ARM to rtld-elf
Reviewed by:    cognet
Obtained from:  NetBSD
2012-02-14 00:16:34 +00:00
Konstantin Belousov
0eddfb0608 Add missed EOL when die() was converted to use rtld_fdputstr() instead
of errx().

Reported by:	amdmi3
PR:	bin/165075
MFC after:	3 days
2012-02-13 11:15:29 +00:00
Oleksandr Tymoshenko
b098345b9e Add handlers for TLS-related relocation entries 2012-02-11 00:54:57 +00:00
Oleksandr Tymoshenko
9b4f251484 Remove debug output 2012-02-10 19:58:04 +00:00
Oleksandr Tymoshenko
20c390af63 Switch MIPS TLS implementation to Variant I 2012-02-10 06:42:50 +00:00
Oleksandr Tymoshenko
cf9c1f8158 Fix debug output for MIPS part of rtld 2012-02-10 03:30:57 +00:00
Konstantin Belousov
6d7610d75a Add support for GNU RELRO.
Submitted by:	John Marino <draco marino st>
MFC after:	2 weeks
2012-01-30 19:52:17 +00:00
Ed Schouten
581f58e7a3 Remove unneeded dtv variable.
It is only assigned and not used at all. The object files stay identical
when the variables are removed.

Approved by:	kib
2012-01-17 21:55:20 +00:00
Ulrich Spörlein
1acf0dba17 Spelling fixes for libexec/ 2012-01-07 16:09:54 +00:00
Konstantin Belousov
5d8aec9c02 Implement fdlopen(3), an rtld interface to load shared object by file
descriptor.

Requested and tested by:	des (previous version)
Reviewed by:  des, kan (previous version)
MFC after:    2 weeks
2012-01-07 10:33:01 +00:00
Konstantin Belousov
68266d5aba Postpone the resolution of IRELATIVE relocations and IFUNC-targeted
relocations until tls is initialized and stacks permissions correctly
set. This allows the ifunc to call malloc(3) and some other heavy
services.

Add debug banner.

MFC after:	3 days
2012-01-04 17:17:11 +00:00
Nathan Whitehorn
4b51c69976 Additional icache paranoia: non-PLT relocations can modify the text segment.
It is then important to make sure the icache is synchronized again to
prevent (rare) random seg faults and illegal instructions.

MFC after:	3 days
2011-12-17 16:20:27 +00:00
Nathan Whitehorn
17bbcc52d1 Fix RTLD on PowerPC after r228435. Changing the order of init_pltgot()
caused the icache to be invalidated at the wrong time, resulting in
an icache full of nonsense in the PLT section.
2011-12-17 15:31:00 +00:00
Konstantin Belousov
5734c46c68 _rtld_bind() read-locks the bind lock, and possible plt resolution
from the dispatcher would also acquire bind lock in read mode, which
is the supported operation. plt is explicitely designed to allow safe
multithreaded updates, so the shared lock do not cause problems.

The error in r228435 is that it allows read lock acquisition after the
write lock for the bind block.  If we dlopened the shared object that
contains IRELATIVE or jump slot which target is STT_GNU_IFUNC, then
possible recursive plt resolve from the dispatcher would cause it.

Postpone the resolution for irelative/ifunc right before initializers
are called, and drop bind lock around calls to dispatcher.  Use
initlist to iterate over the objects instead of the ->next, due to
drop of the bind lock in iteration.

For i386/reloc.c:reloc_iresolve(), fix calculation of the dispatch
function address for dso, by taking into account possible non-zero
relocbase.

MFC after:	3 weeks
2011-12-14 16:47:53 +00:00
Konstantin Belousov
6be4b69715 Add support for STT_GNU_IFUNC and R_MACHINE_IRELATIVE GNU extensions to
rtld on 386 and amd64. This adds runtime bits neccessary for the use
of the dispatch functions from the dynamically-linked executables and
shared libraries.

To allow use of external references from the dispatch function, resolution
of the R_MACHINE_IRESOLVE relocations in PLT is postponed until GOT entries
for PLT are prepared, and normal resolution of the GOT entries is finished.
Similar to how it is done by GNU, IRELATIVE relocations are resolved in
advance, instead of normal lazy handling for PLT.

Move the init_pltgot() call before the relocations for the object are
processed.

MFC after:	3 weeks
2011-12-12 11:03:14 +00:00
Konstantin Belousov
5b646fa11c Typo.
MFC after:	3 days
2011-12-09 20:40:24 +00:00
Konstantin Belousov
953cc85489 Fix fd leak.
Reported by:	slonoman2011 yandex ru
MFC after:	1 week
2011-11-18 09:55:47 +00:00
Eitan Adler
36daf0495a - change "is is" to "is" or "it is"
- change "the the" to "the"

Approved by:	lstewart
Approved by:	sahil (mentor)
MFC after:	3 days
2011-10-16 14:30:28 +00:00
Konstantin Belousov
ef9cbd91d0 Handle the R_386_TLS_TPOFF32 relocation, which is similar to R_386_TLS_TPOFF,
but with negative relocation value.

Found by:	mpfr test suite, pointed to by ale
Reviewed by:	kan
MFC after:	1 week
2011-10-08 12:42:19 +00:00
Konstantin Belousov
4e63ea7386 Setting up TLS block for the main thread must be done after the
relocations are processed, since tls initialization section might be
itself subject for relocations. Only set up of the block is postponed,
the tls block offsets are allocated before relocation processing, since
TLS-related relocations may need offsets ready.

Reported by:	ale
PR:	threads/161344
Reviewed by:	kan
MFC after:	1 week
2011-10-08 12:39:47 +00:00
Konstantin Belousov
750b5e3134 Restore the writing of the .bss sections of the dsos (not the main
executable) after r190885. The whole region for the dso is mmaped with
MAP_NOCORE flag, doing only mprotect(2) over .bss prevented it from
writing .bss to core files.

Revert the optimization of using mprotect(2) to establish .bss, overlap
the section with mmap(2).

Reported by:	attilio
Reviewed by:	attilio, emaste
Approved by:	re (bz)
MFC after:	2 weeks
2011-09-20 21:49:54 +00:00
Konstantin Belousov
f785588713 Use the proper dynamic tls block to calculate the tls variable address
in case tls data generation was updated.

PR:	misc/160721
Submitted by:	"Thinker K.F. Li" <thinker codemud net>
Tested by:	flo
Approved by:	re (bz)
MFC after:	1 week
2011-09-15 11:50:09 +00:00
Konstantin Belousov
b236731716 Do not use the function pointers for the internal operation of rtld_printf()
functions. The _rtld_error() function might be called early during the rtld
bootstrap, in which case function pointers are not yet functional on ia64
due to required relocations not yet performed.

Reported, reviewed and tested by:	marcel
Approved by:	re (bz)
2011-09-06 10:21:33 +00:00
Dimitry Andric
cce0f33cda When libexec/rtld-elf/rtld.c is compiled with clang, the r_debug_state()
function (a hook necessary for gdb support), is inlined, but since the
function contains no code, no calls to it are generated.  When gdb is
debugging a dynamically linked program, this causes backtraces to be
corrupted.

Fix it by marking the function __noinline, and inserting an empty asm
statement, that pretends to clobber memory.  This forces the compiler to
emit calls to r_debug_state() throughout rtld.c.

Approved by:	re (kib)
2011-09-03 11:41:00 +00:00
Konstantin Belousov
0e9a260520 Rtld links with the specially built pic static libc library to get some
C runtime services, like printf(). Unfortunately, the multithread-safeness
measures in the libc do not work in rtld environment.

Rip the kernel printf() implementation and use it in the rtld instead of
libc version. This printf does not require any shared global data and thus
is mt-safe. Systematically use rtld_printf() and related functions, remove
the calls to err(3).

Note that stdio is still pulled from libc due to libmap implementaion using
fopen(). This is safe but unoptimal, and can be changed later.

Reported and tested by:	pgj
Diagnosed and reviewed by:	kan (previous version)
Approved by:	re (bz)
2011-08-24 20:05:13 +00:00
Ben Laurie
5f301949ef Fix clang warnings.
Approved by:	philip (mentor)
2011-06-18 13:56:33 +00:00
Nathan Whitehorn
f33b8de1dc Compile RTLD with global dot symbols on 64-bit PowerPC, as a crutch for
GDB's ability to locate r_debug_state (which is actually the only function
that need be compiled this way).
2011-06-08 13:23:35 +00:00
Andriy Gapon
6014c8de1d rtld: eliminate double call to close(2) that may occur in load_object
The second close(2) call resulted in heisenbugs in some multi-threaded
applications where e.g. dlopen(3) call in one thread could close a file
descriptor for a file having been opened in other thread concurrently.

My litmus test for this issue was an openoffice.org build.

Reviewed by:	jhb
MFC after:	2 weeks
2011-03-25 18:23:10 +00:00
Marius Strobl
080ca1a51b - Add support for TLS relocations.
- Emitt an error when encountering an unsupported and in case of the
  kernel also for unaligned relocations.
- Fix R_SPARC_LOX10 relocations. Apparently these are hardly ever used.
2011-03-11 21:08:02 +00:00
Marius Strobl
cb32ba5229 - Remove clause 3 and 4 from TNF licenses. [1]
- Add the _RF_X committed in r212998 also to the tables in the sparc64
  reloc.c in order reduce differences between the kernel and the userland
  source. This results in no functional change though.
- Fix further inconsistencies in the abbreviations of the names of the
  relocations.
- Further whitespace fixes.

Obtained from:	NetBSD [1]
2011-03-11 20:30:58 +00:00
Nathan Whitehorn
a43ce960b5 Remove dead code that snuck in from the 32-bit PowerPC version of this
file. The error being checked for does not exist on 64-bit systems.
2011-03-07 20:32:46 +00:00
Marius Strobl
25b31a9496 - With the addition of TLS support binutils started to make the addend
values for resolved symbols relative to relocbase instead of sections
  so detect this case and handle as appropriate, which allows using
  kernel modules linked with affected versions of binutils. Actually I
  think this is a bug in binutils but given that apparently nobody
  complained for nearly six years and powerpc has basically the same
  workaround I decided to put it in for the sparc64 kernel, too.
- Fix R_SPARC_HIX22 relocations. Apparently these are hardly ever used.
2011-03-06 15:20:11 +00:00
Marius Strobl
d374d11285 - Consistently abbreviate the names of the relocations.
- End sentences with dots.
- Fix whitespace.
2011-03-06 13:25:46 +00:00
Nathan Whitehorn
7a159e4971 Remove a hack made obsolete by the binutils 2.17 merge. 2011-02-20 16:16:42 +00:00
Nathan Whitehorn
79c77d726e Turn off default generation of userland dot symbols on powerpc64 now that
we have a binutils that supports it. Kernel dot symbols remain on to assist
DDB.
2011-02-18 21:44:53 +00:00
Konstantin Belousov
a7bc470a84 Use sigsetjmp/siglongjmp with disabled signal mask access for
lock upgrade in rtld. There is no need to care about the mask,
which causes a lot of unneeded sigprocmask(2) calls during each
symbol lookup.
2011-02-09 09:20:27 +00:00
Konstantin Belousov
54a6cb4e7b Fix grammar in comment.
Pointy hat to:	kib
MFC after:	1 week
2011-01-30 16:21:25 +00:00
Konstantin Belousov
2c95328822 Make ldd(1) work when versioned dependency file is cannot be loaded.
Instead of aborting in locate_dependency(), propagate the error to
caller. The rtld startup function does the right thing with an error
from rtld_verify_versions(), depending on the mode of operation.

Reported by:	maho
In collaboration with:	kan
MFC after:	1 week
2011-01-30 16:14:09 +00:00
Alexander Kabaev
20a86d1f34 Eliminate the use of symlook_needed function in favor of DAGS.
Place elements on DAG lists in breadth-first order. This allows us to
walk pre-built list in all cases where breadth-first dependency chain
enumeration is required.

Fix dlsym on special handle obtained by dlopen(NULL, ...) to do what
comment claims it does. Take advantage of recently added symlook_global
function to iterate over main objects and global DAGs lists properly in
search of a symbol. Since rtld itself provides part of the global
namespace, search rtld_obj too.

Remove recursion from init_dag and symlook_needed functions. Use
symlook_needed for ELF filtee processing only and change lookup order
used in the function to match the order used by Solaris runtime linker
under same circumstances. While there, fix weak symbol handling in the
loop so that we return the first weak symbol definition if no strong one
was found, instead of the last one.

Reviewed by: kib
MFC after:   1 month
2011-01-28 23:44:57 +00:00