summaryrefslogtreecommitdiff
path: root/libexec/rtld-elf/map_object.c
Commit message (Collapse)AuthorAgeFilesLines
* Undo 'fix off by one' part of r365360.Konstantin Belousov2020-09-051-1/+1
| | | | | | | | | Noted by: emaste Sponsored by: The FreeBSD Foundation MFC after: 6 days Notes: svn path=/head/; revision=365370
* rtld: Handle ELF dso with program headers outside the first page.Konstantin Belousov2020-09-051-10/+28
| | | | | | | | | | | Reported by: Alex Arslan <alex.arslan@julialang.org> PR: 229708 Reviewed by: dim (previous version), emaste Sponsored by: The FreeBSD Foundation Differential revision: https://reviews.freebsd.org/D26323 Notes: svn path=/head/; revision=365360
* Make p_vaddr % p_align == p_offset % p_align for (some) TLS segments.Konstantin Belousov2020-04-041-0/+1
| | | | | | | | | | | | | | | | | | See https://sourceware.org/bugzilla/show_bug.cgi?id=24606 for the test case. See https://reviews.llvm.org/D64930 for the background and more discussion. Also this fixes another bug in malloc_aligned() where total size of the allocated memory might be not enough to fit the aligned requested block after the initial pointer is incremented by the pointer size. Reviewed by: bdragon Tested by: antoine (exp-run PR 244866), bdragon, emaste Sponsored by: The FreeBSD Foundation MFC after: 2 weeks Differential revision: https://reviews.freebsd.org/D21163 Notes: svn path=/head/; revision=359634
* rtld: make checks for mmap(2) failures compliant with documentation.Konstantin Belousov2019-12-121-3/+3
| | | | | | | | | | | On error, mmap(2) returns MAP_FAILED. There is no need to use its definition or to cast. Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=355676
* rtld: do not try to mmap a zero-sized PT_LOADBrandon Bergren2019-12-121-5/+6
| | | | | | | | | | | | | | | | | | | | When a PT_LOAD segment has a zero p_filesz, skip the data mmap, as mmapping zero bytes from a file is an error. A PT_LOAD with zero p_filesz is legal (but somewhat uncommon due to segment merging in modern linkers, as it is more efficient to merge .data and .bss by just extending p_memsz in the previous segment, assuming compatible page protection.) This was seen on ports/graphics/glew on a powerpc64 ELFv2 experimental build. Submitted by: Alfredo Dal'Ava Junior <alfredo.junior@eldorado.org.br> Reviewed by: kib Differential Revision: https://reviews.freebsd.org/D22634 Notes: svn path=/head/; revision=355657
* Remove Obj_Entry textsize member.Konstantin Belousov2018-11-041-1/+0
| | | | | | | | | | | It is unused after r340102, and more important, I do not see how to define textsize in both practically useful and correct way, for binaries with more that one executable segments. Sponsored by: The FreeBSD Foundation Notes: svn path=/head/; revision=340107
* rtld-elf: fix more warnings to allow compiling with WARNS=6Alex Richardson2018-10-291-0/+1
| | | | | | | | | Reviewed By: kib Approved By: brooks (mentor) Differential Revision: https://reviews.freebsd.org/D17154 Notes: svn path=/head/; revision=339879
* rtld-elf: compile with WANRS=4 warnings other than -Wcast-alignAlex Richardson2018-10-291-8/+8
| | | | | | | | | Reviewed By: kib Approved By: brooks (mentor) Differential Revision: https://reviews.freebsd.org/D17153 Notes: svn path=/head/; revision=339878
* rtld-elf: make it compile with WARNS=3Alex Richardson2018-10-291-1/+1
| | | | | | | | | Reviewed By: kib Approved By: brooks (mentor) Differential Revision: https://reviews.freebsd.org/D17150 Notes: svn path=/head/; revision=339877
* rtld: set obj->textsize correctlyAlex Richardson2018-10-291-2/+7
| | | | | | | | | | | | | | | | | | | | With lld-generated binaries the first PT_LOAD will usually be a read-only segment unless you pass --no-rosegment. For those binaries the textsize is determined by the next PT_LOAD. To allow both LLD and bfd 2.17 binaries to be parsed correctly use the end of the last PT_LOAD that is marked as executable instead. I noticed that the value was wrong while adding some debug prints for some rtld changes for CHERI binaries. `obj->textsize` only seems to be used by PPC so the effect is untested. However, the value before was definitely wrong and the new result matches the phdrs. Reviewed By: kib Approved By: brooks (mentor) Differential Revision: https://reviews.freebsd.org/D17117 Notes: svn path=/head/; revision=339876
* libexec: adoption of SPDX licensing ID tags.Pedro F. Giffuni2017-11-271-0/+2
| | | | | | | | | | | | | | | | | Mainly focus on files that use BSD 2-Clause license, however the tool I was using misidentified many licenses so this was mostly a manual - error prone - task. The Software Package Data Exchange (SPDX) group provides a specification to make it easier for automated tools to detect and summarize well known opensource licenses. We are gradually adopting the specification, noting that the tags are considered only advisory and do not, in any way, superceed or replace the license texts. No functional change intended. Notes: svn path=/head/; revision=326274
* Use address space guard to implement inter-segment gap.Konstantin Belousov2017-06-241-1/+4
| | | | | | | | | | | | | Rtld checks and use old MAP_ANON/PROT_NONE method of creating gap if running on old kernel. Reviewed by: alc, markj Tested by: pho, Qualys Sponsored by: The FreeBSD Foundation MFC after: 1 week Notes: svn path=/head/; revision=320318
* Fix the AT_EXECFD functionality.Konstantin Belousov2017-05-151-0/+2
| | | | | | | | | | | | | | | If the mapped object is linked at specific address, we must obey it. If AT_EXECFD is not used, only in-kernel ELF image activator needed to keep the mapping address, since only binaries are linked at the fixed address, and binaries are mapped by kernel in this case. Reviewed by: emaste Sponsored by: The FreeBSD Foundation MFC after: 2 weeks X-Differential revision: https://reviews.freebsd.org/D10701 Notes: svn path=/head/; revision=318312
* Fix handling of DT_TEXTREL for an object with more than one read-onlyKonstantin Belousov2016-03-021-2/+1
| | | | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=296319
* This seems like a very trivial bug that should have been squashed a longMaxim Sobolev2016-01-301-3/+9
| | | | | | | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=295059
* Allow PT_NOTES segments to be located anywhere in the executableKonstantin Belousov2015-10-141-3/+21
| | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=289324
* Before calling mmap() on a shared library's text and data sections, rtldAlan Cox2014-04-111-2/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=264346
* Map libraries linked with -Ttext-segment=base_addr at base_addr.Tijl Coosemans2013-02-271-1/+1
| | | | | | | | | | | | 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 Notes: svn path=/head/; revision=247396
* Parse notes only after object structure had been allocated.Alexander Kabaev2012-08-031-2/+2
| | | | | | | | Reported by: kargl Reviewed by: kib (sans whitespace) Notes: svn path=/head/; revision=239019
* Eliminate the static buffer used to read the first page of the mappedKonstantin Belousov2012-06-141-62/+70
| | | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=237058
* Remove write-only variable.Konstantin Belousov2012-03-161-2/+0
| | | | | | | MFC after: 3 days Notes: svn path=/head/; revision=233041
* Rtld on diet 3.Konstantin Belousov2012-03-141-5/+6
| | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=232974
* When iterating over the dso program headers, the object is not initializedKonstantin Belousov2012-03-121-1/+4
| | | | | | | | | | | | 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 Notes: svn path=/head/; revision=232856
* Add support for preinit, init and fini arrays. Some ABIs, inKonstantin Belousov2012-03-111-0/+10
| | | | | | | | | | | | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=232831
* Add support for GNU RELRO.Konstantin Belousov2012-01-301-0/+12
| | | | | | | | Submitted by: John Marino <draco marino st> MFC after: 2 weeks Notes: svn path=/head/; revision=230784
* Restore the writing of the .bss sections of the dsos (not the mainKonstantin Belousov2011-09-201-2/+3
| | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=225699
* When loading dso without PT_GNU_STACK phdr, only callKonstantin Belousov2011-01-251-1/+1
| | | | | | | | | | __pthread_map_stacks_exec() on architectures that allow executable stacks. Reported and tested by: marcel (ia64) Notes: svn path=/head/; revision=217851
* In rtld, read the initial stack access mode from AT_STACKPROT as setKonstantin Belousov2011-01-081-0/+7
| | | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=217153
* In rtld's map_object(), use pread(..., 0) rather than read() to read theRobert Watson2009-10-061-1/+1
| | | | | | | | | | | | 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 Notes: svn path=/head/; revision=197808
* Only perform .bss mapping and cleaning operations when segment file sizeKonstantin Belousov2009-07-171-21/+27
| | | | | | | | | | | | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=195743
* Currently, when mapping an object, rtld reserves the whole address spaceKonstantin Belousov2009-04-101-7/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=190885
* Update comment to the reality, rtld supports any number of loadable segments.Konstantin Belousov2009-04-101-3/+2
| | | | | | | | | Fix spacing. Reviewed by: kan Notes: svn path=/head/; revision=190883
* Implement the dynamic string token substitution in the rpath andKonstantin Belousov2009-03-181-0/+2
| | | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=189959
* Implement dl_iterate_phdr function.Alexander Kabaev2007-04-031-12/+35
| | | | | | | | Convert boolean flags in internal Obj_Entry structure into bitfields. Properly check for loaded segment alignment in map_object. Notes: svn path=/head/; revision=168312
* Implement ELF symbol versioning using GNU semantics. This code aimsAlexander Kabaev2005-12-181-2/+8
| | | | | | | | | | | | 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. Notes: svn path=/head/; revision=153515
* Attempt to free any static TLS space used by a shared library when itDoug Rabson2005-02-271-0/+3
| | | | | | | | | | is unloaded. This allows applications which load and unload libraries like libGL.so.1 several times to work properly. MFC after: 2 days Notes: svn path=/head/; revision=142645
* Add support for Thread Local Storage.Doug Rabson2004-08-031-2/+14
| | | | Notes: svn path=/head/; revision=133063
* - Add support for DT_FLAGS.Matthew N. Dodd2003-06-181-0/+1
| | | | | | | - Define various things from the most recent ELF spec. Notes: svn path=/head/; revision=116511
* Simplify map_object() by breaking out the ELF header validation bitsMatthew N. Dodd2003-05-311-58/+67
| | | | | | | into a separate function. Notes: svn path=/head/; revision=115446
* Fix signed/unsigned comparison warnings.David E. O'Brien2003-05-041-3/+4
| | | | Notes: svn path=/head/; revision=114625
* Free obj->priv field in obj_free functions. This field is NULLAlexander Kabaev2003-03-141-0/+1
| | | | | | | | on all architectures except ia64, which uses it to keep function description table. Notes: svn path=/head/; revision=112244
* Change the way ELF coredumps are handled. Instead of unconditionallyMatthew Dillon2002-12-161-6/+23
| | | | | | | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=107948
* Add support for binaries with arbitrary number of PT_LOAD sections.Alexander Kabaev2002-10-231-37/+55
| | | | | | | Reviewed by: peter Notes: svn path=/head/; revision=105753
* Add ia64 support. Various adjustments were made to existing targets toDoug Rabson2001-10-151-0/+1
| | | | | | | | cope with a few interface changes required by the ia64. In particular, function pointers on ia64 need special treatment in rtld. Notes: svn path=/head/; revision=85004
* Get the actual pathname of the dynamic linker from the executable'sJohn Polstra1999-08-301-7/+11
| | | | | | | | | | 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. Notes: svn path=/head/; revision=50610
* When checking to see if a shared object is already loaded, look forJohn Polstra1999-08-301-1/+6
| | | | | | | a device/inode match if no pathname match is found. Notes: svn path=/head/; revision=50609
* Revamp the symbol lookup algorithm to cope better with objectsJohn Polstra1999-08-301-1/+37
| | | | | | | | | | | | | | | | | | | | | | | | | 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 Notes: svn path=/head/; revision=50608
* $Id$ -> $FreeBSD$Peter Wemm1999-08-281-1/+1
| | | | Notes: svn path=/head/; revision=50476
* Change many asserts into normal errors. They were all for conditionsJohn Polstra1999-07-181-24/+38
| | | | | | | | | caused by invalid shared objects rather than by internal errors. Enable format string mismatch checking for _rtld_error(). Notes: svn path=/head/; revision=48871
* Add alpha support.Doug Rabson1998-09-041-28/+28
| | | | | | | | Submitted by: John Birrell <jb@cimlogic.com.au> (with extra hacks by me) Obtained from: Probably NetBSD Notes: svn path=/head/; revision=38816