Commit Graph

38 Commits

Author SHA1 Message Date
Konstantin Belousov
ca8c8dc3eb Fix handling of DT_TEXTREL for an object with more than one read-only
segment.  According to gABI spec, presence of the tag indicates that
dynamic linker must be prepared to handle relocations against any
read-only segment, not only the segment which we, somewhat arbitrary,
declared the text.

For each read-only segment, add write permission before relocs are
processed, and return to the mapping mode requested by the phdr, after
relocs are done.

Reported, tested, and reviewed by:	emaste
PR:	207631
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
2016-03-02 16:36:24 +00:00
Maxim Sobolev
7fd852f860 This seems like a very trivial bug that should have been squashed a long
time ago, but for some reason it was not. Basically, without this change
dlopen(3)'ing an empty .so file would just cause application to dump core
with SIGSEGV.

Make sure the file has enough data for at least the ELF header before
mmap'ing it.

Add a test case to check that dlopen an empty file return an error.

There were a separate discussion as to whether it should be SIGBUS
instead when you try to access region mapped from an empty file,
but it's definitely SIGSEGV now, so if anyone want to check that please
be my guest.
Reviewed by:	mjg, cem
MFC after:	1 week
Differential Revision:	https://reviews.freebsd.org/D5112
2016-01-30 04:16:05 +00:00
Konstantin Belousov
bd56d410c4 Allow PT_NOTES segments to be located anywhere in the executable
image.

The dynamic linker still requires that program headers of the
executable or dso are mapped by a PT_LOAD segment.

Reviewed by:	emaste, jhb
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D3871
2015-10-14 18:29:21 +00:00
Alan Cox
ea8577c712 Before calling mmap() on a shared library's text and data sections, rtld
first calls mmap() with the arguments PROT_NONE and MAP_ANON to reserve a
single, contiguous range of virtual addresses for the entire shared library.
Later, rtld calls mmap() with the the shared library's file descriptor
and the argument MAP_FIXED to place the text and data sections within the
reserved range.  The rationale for mapping shared libraries in this way is
explained in the commit message for Revision 190885.  However, this approach
does have an unintended, negative consequence.  Since the first call to
mmap() specifies MAP_ANON and not the shared library's file descriptor, the
kernel has no idea what alignment the vm object backing the file prefers.
As a result, the reserved range's alignment is unlikely to be the same as
the vm object's, and so mapping with superpages becomes impossible.  To
address this problem, this revision adds the argument MAP_ALIGNED_SUPER to
the first call to mmap() if the text section is larger than the smallest
superpage size.

To determine if the text section is larger than the smallest superpage
size, rtld must always fetch the page size information.  As a result, the
private code for fetching the base page size in rtld's builtin malloc is
redundant.  Eliminate it.  Requested by: kib

Tested by:	zbb (on arm)
Reviewed by:	kib (an earlier version)
Discussed with:	jhb
2014-04-11 16:55:25 +00:00
Tijl Coosemans
157895135a Map libraries linked with -Ttext-segment=base_addr at base_addr.
Normal libraries have base address 0 and are unaffected by this change.

PR:		176216
Submitted by:	Damjan Jovanovic <damjan.jov@gmail.com>
Reviewed by:	kib
MFC after:	1 week
2013-02-27 09:34:09 +00:00
Alexander Kabaev
d958a71be2 Parse notes only after object structure had been allocated.
Reported by: kargl
Reviewed by: kib (sans whitespace)
2012-08-03 17:04:41 +00:00
Konstantin Belousov
e474e51e07 Eliminate the static buffer used to read the first page of the mapped
object, and eliminate the pread(2) call as well [1]. Mmap the first
page of the object temporaly, and unmap it on error or last use.
Potentially, this leaves one-page gap between succeeding dlopen(3),
but there are other mmap(2) consumers as well.

Fix several cases were the whole mapping of the object leaked on error.

Use MAP_PREFAULT_READ for mmap(2) calls which map real object pages [2].

Insipired by the patch by:	Ian Lepore <freebsd damnhippie dyndns org> [1]
Suggested by:	alc [2]
MFC after:	2 weeks
2012-06-14 11:20:22 +00:00
Konstantin Belousov
31f7a2032e Remove write-only variable.
MFC after:	3 days
2012-03-16 19:09:34 +00:00
Konstantin Belousov
6fea10fb9f Rtld on diet 3.
Stop using strerror(3) in rtld, which brings in msgcat and stdio.
Directly access sys_errlist array of errno messages with private
rtld_strerror() function.

Now,
$ size /libexec/ld-elf.so.1
   text    data     bss     dec     hex filename
  96983    2480    8744  108207   1a6af /libexec/ld-elf.so.1

Reviewed by:	dim, kan
MFC after:	2 weeks
2012-03-14 15:39:59 +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
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
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
cb38d4941c When loading dso without PT_GNU_STACK phdr, only call
__pthread_map_stacks_exec() on architectures that allow executable
stacks.

Reported and tested by:	marcel (ia64)
2011-01-25 21:12:31 +00:00
Konstantin Belousov
212f264cd6 In rtld, read the initial stack access mode from AT_STACKPROT as set
by kernel, and parse PT_GNU_STACK phdr from linked and loaded dsos.

If the loaded dso requires executable stack, as specified by PF_X bit
of p_flags of PT_GNU_STACK phdr, but current stack protection does not
permit execution, the __pthread_map_stacks_exec symbol is looked up
and called. It should be implemented in libc or threading library and
change the protection mode of all thread stacks to be executable.

Provide a private interface _rtld_get_stack_prot() to export the stack
access mode as calculated by rtld.

Reviewed by:	   kan
2011-01-08 17:11:49 +00:00
Robert Watson
1c232cd509 In rtld's map_object(), use pread(..., 0) rather than read() to read the
ELF header from the front of the file.  As all other I/O on the binary
is done using mmap(), this avoids the need for seek privileges on the
file descriptor during run-time linking.

MFC after:	1 month
Sponsored by:	Google
2009-10-06 17:14:39 +00:00
Konstantin Belousov
69ca61ba5e Only perform .bss mapping and cleaning operations when segment file size
is not equal to its memory size.

This eliminates unneeded clearing of the text segment that often
happens due to text end not being page-aligned.

For instance,
$ readelf -l /lib/libedit.so.6
Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  LOAD           0x000000 0x00000000 0x00000000 0x139e1 0x139e1 R E 0x1000
  LOAD           0x014000 0x00014000 0x00014000 0x00f04 0x00f14 RW  0x1000
  DYNAMIC        0x014cc4 0x00014cc4 0x00014cc4 0x000d0 0x000d0 RW  0x4
$ procstat -v $$ (for /bin/sh)
68585 0x28097000 0x280aa000 r-x    6    0  21  14 CN vn /lib/libedit.so.6
68585 0x280aa000 0x280ab000 r-x    1    0   1   0 CN vn /lib/libedit.so.6 <==
68585 0x280ab000 0x280ac000 rwx    1    0   1   0 CN vn /lib/libedit.so.6
Note the splitted map entry marked by '<=='.

Reviewed by:	kan
Approved by:	re (kensmith)
MFC after:	1 month
2009-07-17 19:32:04 +00:00
Konstantin Belousov
a3c8e04ef7 Currently, when mapping an object, rtld reserves the whole address space
for the mapping by the object' file with the protection and mode of
the first loadable segment over the whole region. Then, it maps other
segments at the appropriate addresses inside the region.

On amd64, due to default alignment of the segments being 1Gb, the
subsequent segment mappings leave the holes in the region, that usually
contain mapping of the object' file past eof. Such mappings prevent
wiring of the address space, because the pages cannot be faulted in.

Change the way the mapping of the ELF objects is constructed, by first
mapping PROT_NONE anonymous memory over the whole range, and then
mapping the segments of the object over it. Take advantage of this new
order and allocate .bss by changing the protection of the range instead
of remapping.

Note that we cannot simply keep the holes between segments, because
other mappings may be made there. Among other issues, when the dso is
unloaded, rtld unmaps the whole region, deleting unrelated mappings.

The kernel ELF image activator does put the holes between segments, but
this is not critical for now because kernel loads only executable image
and interpreter, both cannot be unloaded. This will be fixed later, if
needed.

Reported and tested by:	Hans Ottevanger <fbsdhackers beasties demon nl>
Suggested and reviewed by:	kan, alc
2009-04-10 10:14:04 +00:00
Konstantin Belousov
11e0093f28 Update comment to the reality, rtld supports any number of loadable segments.
Fix spacing.

Reviewed by:	kan
2009-04-10 09:52:42 +00:00
Konstantin Belousov
28551690e0 Implement the dynamic string token substitution in the rpath and
soneeded pathes. The $ORIGIN, $OSNAME, $OSREL and $PLATFORM tokens
are supported. Enabling the substitution requires DF_ORIGIN flag in
DT_FLAGS or DF_1_ORIGIN if DF_FLAGS_1, that may be set with -z origin
gnu ld flag. Translation is unconditionally disabled for setuid/setgid
processes.

The $ORIGIN translation relies on the AT_EXECPATH auxinfo supplied
by kernel.

Requested by:	maho
Tested by:	maho, pho
Reviewed by:	kan
2009-03-18 13:40:37 +00:00
Alexander Kabaev
49f90ad282 Implement dl_iterate_phdr function.
Convert boolean flags in internal Obj_Entry structure into bitfields.
Properly check for loaded segment alignment in map_object.
2007-04-03 18:31:20 +00:00
Alexander Kabaev
0eb88f2029 Implement ELF symbol versioning using GNU semantics. This code aims
to be compatible with symbol versioning support as implemented by
GNU libc and documented by http://people.redhat.com/~drepper/symbol-versioning
and LSB 3.0.

Implement dlvsym() function to allow lookups for a specific version of
a given symbol.
2005-12-18 19:43:33 +00:00
Doug Rabson
ddab7ee80a Attempt to free any static TLS space used by a shared library when it
is unloaded. This allows applications which load and unload libraries
like libGL.so.1 several times to work properly.

MFC after: 2 days
2005-02-27 12:55:40 +00:00
Doug Rabson
017246d02f Add support for Thread Local Storage. 2004-08-03 08:51:00 +00:00
Matthew N. Dodd
da9f245470 - Add support for DT_FLAGS.
- Define various things from the most recent ELF spec.
2003-06-18 03:34:29 +00:00
Matthew N. Dodd
341b3de62b Simplify map_object() by breaking out the ELF header validation bits
into a separate function.
2003-05-31 14:48:59 +00:00
David E. O'Brien
78af18bd24 Fix signed/unsigned comparison warnings. 2003-05-04 00:56:00 +00:00
Alexander Kabaev
63c1e7cb8d Free obj->priv field in obj_free functions. This field is NULL
on all architectures except ia64, which uses it to keep function
description table.
2003-03-14 21:11:28 +00:00
Matthew Dillon
fa7dd9c5bc Change the way ELF coredumps are handled. Instead of unconditionally
skipping read-only pages, which can result in valuable non-text-related
data not getting dumped, the ELF loader and the dynamic loader now mark
read-only text pages NOCORE and the coredump code only checks (primarily) for
complete inaccessibility of the page or NOCORE being set.

Certain applications which map large amounts of read-only data will
produce much larger cores.  A new sysctl has been added,
debug.elf_legacy_coredump, which will revert to the old behavior.

This commit represents collaborative work by all parties involved.
The PR contains a program demonstrating the problem.

PR:		kern/45994
Submitted by:	"Peter Edwards" <pmedwards@eircom.net>, Archie Cobbs <archie@dellroad.org>
Reviewed by:	jdp, dillon
MFC after:	7 days
2002-12-16 19:24:43 +00:00
Alexander Kabaev
8b7f25d41d Add support for binaries with arbitrary number of PT_LOAD sections.
Reviewed by:	peter
2002-10-23 01:43:29 +00:00
Doug Rabson
b5393d9f78 Add ia64 support. Various adjustments were made to existing targets to
cope with a few interface changes required by the ia64. In particular,
function pointers on ia64 need special treatment in rtld.
2001-10-15 18:48:42 +00:00
John Polstra
a607e5d7f8 Get the actual pathname of the dynamic linker from the executable's
PT_INTERP program header entry, to ensure that gdb always finds
the right dynamic linker.

Use obj->relocbase to simplify a few calculations where appropriate.
1999-08-30 01:54:13 +00:00
John Polstra
7360ae0f2a When checking to see if a shared object is already loaded, look for
a device/inode match if no pathname match is found.
1999-08-30 01:50:41 +00:00
John Polstra
926ea445fe Revamp the symbol lookup algorithm to cope better with objects
loaded separately by dlopen that have global symbols with identical
names.  Viewing each dlopened object as a DAG which is linked by its
DT_NEEDED entries in the dynamic table, the search order is as
follows:

  * If the referencing object was linked with -Bsymbolic, search it
    internally.
  * Search all dlopened DAGs containing the referencing object.
  * Search all objects loaded at program start up.
  * Search all objects which were dlopened() using the RTLD_GLOBAL
    flag (which is now supported too).

The search terminates as soon as a strong definition is found.
Lacking that, the first weak definition is used.

These rules match those of Solaris, as best I could determine them
from its vague manual pages and the results of experiments I performed.

PR:		misc/12438
1999-08-30 01:48:19 +00:00
Peter Wemm
7f3dea244c $Id$ -> $FreeBSD$ 1999-08-28 00:22:10 +00:00
John Polstra
bfb1ef6058 Change many asserts into normal errors. They were all for conditions
caused by invalid shared objects rather than by internal errors.

Enable format string mismatch checking for _rtld_error().
1999-07-18 00:02:19 +00:00
Doug Rabson
13575fc46f Add alpha support.
Submitted by: John Birrell <jb@cimlogic.com.au> (with extra hacks by me)
Obtained from: Probably NetBSD
1998-09-04 19:03:57 +00:00
John Polstra
3124c3e093 Import the ELF dynamic linker. This is the ElfKit version with
quite a few enhancements and bug fixes.  There are still some known
deficiencies, but it should be adequate to get us started with ELF.

Submitted by:	John Polstra <jdp@polstra.com>
1998-03-07 19:24:35 +00:00