History log of /netbsd-current/libexec/ld.elf_so/map_object.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.67 04-Jun-2023 joerg

Fix interactions of initial-exec TLS model and dlopen

(1) If an initial-exec relocation was used for a non-local symbol
(i.e. the definition of the symbol is in a different DSO), the
computation of the static TLS offset used the wrong DSO.
This would effectively mean the wrong address was computed
(PR toolchain/50277, PR pkg/57445).

Fix this by forcing the computation of the correct DSO (the one defining
the symbol).

This code uses __UNCONST to avoid the vast interface changes for this
special case.

(2) If symbols from a DSO loaded via dlopen are used with both
global-dynamic/local-dynamic and initial-exec relocations AND
a initial-exec relocation was resolved first in a thread, a split brain
situation could exist where the dynamic relocations would use one memory
block (separate allocation) and the initial-exec relocations the static
per-thread TLS space.

(3) If the initial-exec relocation in (2) is seen after any thread has
already used a GD/LD allocation, bail out. Since IE relocations are used
only in the GOT, this will prevent the dlopen. This is a bit more
aggressive than necessary, but a full blown reference counting doesn't
seem to be justified.


# 1.66 31-May-2023 riastradh

ld.elf_so: Sprinkle tls debug messages.

XXX pullup-10


# 1.65 12-Jan-2023 christos

fix the ldd build


# 1.64 12-Jan-2023 christos

Handle program headers properly; fixes c++ exceptions on arm32.


# 1.63 06-Jan-2023 christos

Remove the limitation of only being able to load binaries with 2 PT_LOAD
sections, like the kernel can. From FreeBSD.


Revision tags: netbsd-10-base
# 1.62 30-Mar-2022 hannken

_rtld_map_object(): no need to mmap an empty bss segment.


Revision tags: cjep_sun2x-base1 cjep_sun2x-base cjep_staticlib_x-base1 cjep_staticlib_x-base phil-wifi-20200421 phil-wifi-20200411 phil-wifi-20200406
# 1.61 04-Mar-2020 thorpej

PT_GNU_RELRO segments are arranged such that their vaddr + memsz ends
on a linker common page size boundary. However, if the common page size
used by the linker is less than the VM page size being used by the kernel,
this can end up in the middle of a VM page and when the region is write-
protected, this can cause objects in neighboring .data to get incorrectly
write-protected, resulting in a crash.

Avoid this situation by calculating the end of the RELRO region not by
rounding memsz up to the VM page size, but rather by adding vaddr + memsz
and then truncating to the VM page size.

Fixes PR toolchain/55043.

XXX pullup-9


Revision tags: is-mlppp-base netbsd-9-0-RELEASE netbsd-9-0-RC2 netbsd-9-0-RC1 phil-wifi-20191119 netbsd-9-base phil-wifi-20190609 pgoyette-compat-20190127 pgoyette-compat-20190118
# 1.60 06-Jan-2019 joerg

branches: 1.60.2;
When loading a non-PIE main binary, the virtual address must match.
Use MAP_TRYFIXED and verify that the result matches the expectation.


# 1.59 04-Jan-2019 joerg

MAP_ALIGNED has existed for years, just assume it exists.


Revision tags: pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521 pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base perseant-stdc-iso10646-base
# 1.58 19-Jun-2017 joerg

branches: 1.58.4; 1.58.6;
Replace COMBREL with just-in-time check in _rtld_relocate_nonplt_objects.

The COMBREL logic predates thread-safety of the dynamic linker and
breaks the use of shared locks for the common symbol lookup case. It is
unlikely to provide any benefit for lazy binding or PLT lookups, so
provide equivalent functionality in the non-PLT relocation handling loop
by checking if the symbol used by the current relocation is the same as
the one used during the last lookup. No inter-object cachine is done as
it is also unlikely to be benefical.

Testing with Firefox startup on AMD64 shows a small performance gain by
the new method.


Revision tags: netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1 pgoyette-localcount-20170320
# 1.57 10-Mar-2017 maya

branches: 1.57.4;
Revert to version 1.55 of map_object.c. This reverts:

"really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero."

As per toolchain/52054: src/libexec/ld.elf_so update breaks everything,
this commit is very broken for some people (but not others). chs mentioned
he has a fix, but best not to leave -current broken in the meantime.


# 1.56 09-Mar-2017 chs

really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero.


Revision tags: bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104 localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.55 16-Jun-2016 christos

branches: 1.55.2; 1.55.4;
Move relro after we've computed out relocbase and re-enable it.
(Matthias Weckbecker)


# 1.54 14-Jun-2016 christos

Add support for GNU RELRO headers from Matthias Weckbecker.


# 1.53 30-Oct-2014 martin

Avoid use after free, reported by the coverity scanner.


Revision tags: netbsd-7-0-2-RELEASE netbsd-7-0-1-RELEASE netbsd-7-0-RELEASE netbsd-7-0-RC3 netbsd-7-0-RC2 netbsd-7-0-RC1 netbsd-7-base yamt-pagecache-base9 tls-earlyentropy-base riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 tls-maxphys-base
# 1.52 03-Aug-2013 skrll

branches: 1.52.4;
Fix DEBUG build.


Revision tags: riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base
# 1.51 09-May-2013 christos

convert to SIMPLEQ like the rest of the queues.


# 1.50 08-May-2013 christos

need xfree() instead of free(). Using free() ends us up in an infinite loop.


# 1.49 06-May-2013 christos

Fixed handling of DT_SONAME:

- add function to add name aliases for shared libraries loaded
XXX[1]: we don't add a name during load time, only when DT_SONAME
is present.
- search already loaded objects in load_by_name for an already
loaded object that matches our name and return it.
- add missing initialization and cleanup for obj->names
XXX[2]: should we make them SIMPLEQ?
- Add XXX in rtld.c about getting the name of an object.

NB: This makes the jdk work again without resorting to a hack of putting
the build path of libjvm.so into the run path (which is a security
problem).
XXX: Pullup-6?


# 1.48 06-May-2013 skrll

Trailing whitespace


# 1.47 25-Apr-2013 skrll

Adjust exidx_start by relocbase


# 1.46 25-Apr-2013 skrll

Handle PT_ARM_EXIDX when mapping objects.


Revision tags: agc-symver-base yamt-pagecache-base8 yamt-pagecache-base7 yamt-pagecache-base6
# 1.45 13-Oct-2012 dholland

Improve the diagnostic messages for invalid/non-ELF files. Proposed a
while back; I forget the context. Has been sitting in one of my trees
awaiting a test run since, apparently, August 2011.


# 1.44 25-Jul-2012 martin

branches: 1.44.2;
Try to make dbg() printfs realy ELFSIZE independent


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 netbsd-6-1-RC2 netbsd-6-1-RC1 netbsd-6-0-1-RELEASE matt-nb6-plus-nbase netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.43 13-Aug-2011 christos

branches: 1.43.2;
consistent debugging info for program headers


Revision tags: cherry-xenmp-base
# 1.42 09-Mar-2011 joerg

Add TLS support infrastructure. For dynamic binaries, ld.elf_so exports
_rtld_tls_allocate and _rtld_tls_free. libpthread uses this functions to
setup the thread private area of all new threads. ld.elf_so is
responsible for setting up the private area for the initial thread.
Similar functions are called from _libc_init for static binaries, using
dl_iterate_phdr to access the ELF Program Header.

Add test cases to exercise the different TLS storage models. Test cases
are compiled and installed on all platforms, but are skipped on
platforms not marked for TLS support.

This material is based upon work partially supported by
The NetBSD Foundation under a contract with Joerg Sonnenberger.

It is inspired by the TLS support in FreeBSD by Doug Rabson and the
clean ups of the DragonFly port of the original FreeBSD modifications.


Revision tags: bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.41 16-Oct-2010 skrll

Implement dl_iterate_phdr.

Somewhat taken from FreeBSD. Manual page from OpenBSD.


# 1.40 11-Sep-2010 skrll

Wrap long line.


# 1.39 10-Jan-2010 skrll

Reset the COMBRELOC cache Obj_Entry if it was freed.

Fixes PR 41482. Done slightly differently to the patch in the PR.


Revision tags: matt-premerge-20091211
# 1.38 19-May-2009 christos

warns=4


Revision tags: jym-xensuspend-nbase jym-xensuspend-base
# 1.37 06-Jan-2009 mrg

add some (uintptr_t) to some casts, so these compile with ELFSIZE=32
*and _LP64. necessary for 32/64 bit combo ldd support.


Revision tags: netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 wrstuden-revivesa-base
# 1.36 03-Jun-2008 ad

branches: 1.36.4;
Don't bother unmapping the ELF header unless it overlaps the first load
section.


Revision tags: yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-baseX yamt-pf42-base2 yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase cube-autoconf-base matt-armv6-base hpcarm-cleanup-base
# 1.35 05-Oct-2007 ad

branches: 1.35.6; 1.35.8;
- Don't use the names malloc/free/etc, since we can end up pulling in libc's
malloc which is not what we want.
- Remove ancient memory compaction hack. Realloc on free memory is now an
error again (from joerg@).

Ok christos@.


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase wrstuden-fixsa-base-1 netbsd-4-0-RELEASE netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-mips64-base wrstuden-fixsa-base abandoned-netbsd-4-base netbsd-4-base
# 1.34 21-Mar-2006 christos

branches: 1.34.10;
Coverity CID 2749: Fix another double free. Instead of removing that last
free() call, change the allocation policy to leave the responsibility for
allocation/freeing the pathname to _rtld_map_object(), instead of having
the caller allocate it and _rtld_map_object() free it. This simplifies the
code a lot and it is more efficient.


# 1.33 18-Mar-2006 matt

Fix some double frees / missing frees / use after freed all
relating to _rtld_load_object. Fix Coverity CID 2601, 2600.


# 1.32 01-Jun-2005 lukem

appease gcc -Wuninitialized


Revision tags: netbsd-3-1-1-RELEASE netbsd-3-0-3-RELEASE netbsd-3-1-RELEASE netbsd-3-0-2-RELEASE netbsd-3-1-RC4 netbsd-3-1-RC3 netbsd-3-1-RC2 netbsd-3-1-RC1 netbsd-3-0-1-RELEASE netbsd-3-0-RELEASE netbsd-3-0-RC6 netbsd-3-0-RC5 netbsd-3-0-RC4 netbsd-3-0-RC3 netbsd-3-0-RC2 netbsd-3-0-RC1 netbsd-3-base
# 1.31 22-Oct-2004 skrll

Add __RCSIDs.

OK'd by mycroft.


Revision tags: netbsd-2-0-3-RELEASE netbsd-2-1-RELEASE netbsd-2-1-RC6 netbsd-2-1-RC5 netbsd-2-1-RC4 netbsd-2-1-RC3 netbsd-2-1-RC2 netbsd-2-1-RC1 netbsd-2-0-2-RELEASE netbsd-2-0-1-RELEASE netbsd-2-base netbsd-2-0-RELEASE netbsd-2-0-RC5 netbsd-2-0-RC4 netbsd-2-0-RC3 netbsd-2-0-RC2 netbsd-2-0-RC1 netbsd-2-0-base
# 1.30 24-Jul-2003 skrll

ANSIfy and de-__P().


# 1.29 05-Jun-2003 simonb

KNF- blank lines before code at start of functions.


# 1.28 06-Mar-2003 taca

make it compile in /usr/src/rescue directory.


# 1.27 06-Mar-2003 matt

Align psections to their specified alignment (if possible). Use
mmap(2) MAP_ALIGNED.


# 1.26 06-Feb-2003 fvdl

If fstat info was passed, and from it it can be seen that the file size
is smaller than an ELF header, don't even bother to try to look at it.
Avoids coredumps with zero-sized files.


Revision tags: fvdl_fs64_base
# 1.25 05-Dec-2002 junyoung

A bit speedup: in _rtld_load_object(), save the number of calls to
strcmp() by performing path name length comparison first. In the test
with Mozilla, the number was reduced to 1068 from 7182 (yes, we saved
6114 strcmp()!).


# 1.24 05-Oct-2002 mycroft

Minor cleanup.


# 1.23 05-Oct-2002 mycroft

Make this work for ldd again.


# 1.22 04-Oct-2002 mycroft

Rearrange _rtld_map_object() a little, so that we don't have to have the first
page of the object double-mapped. Not that it matters much, but someone was
whinging about it.
While I'm at it, nuke obj->phdr and obj->phsize; they're unused.


# 1.21 27-Sep-2002 mycroft

Change the munmap() of the text-data gap to mprotect(PROT_NONE). This avoids
having scattered little mappings, although it's probably not the best way...


# 1.20 24-Sep-2002 mycroft

Woops, unmap the first page in the `win' case, too.
Tidy up a bit.


# 1.19 24-Sep-2002 junyoung

Use _rtld_pagesz instead of PAGESIZE.


# 1.18 24-Sep-2002 junyoung

space -> tab.


# 1.17 24-Sep-2002 junyoung

Don't need to use union any longer.


# 1.16 23-Sep-2002 mycroft

Several small changes that shave 7-8% off the simple-exec-loop test:

* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().

* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.

* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.

* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)

* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().

Also:

* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.


# 1.15 13-Sep-2002 mycroft

Nuke two frivolous assert()s.


# 1.14 06-Sep-2002 mycroft

Introduce a new flag, `isdynamic', which is used to remember whether the
executable was of type ET_DYN. Use this instead of `mainprog' to determine
whether we need to do base-relative fixups of the PLT. (This allows loading
non-relocatable objects, should we desire to do that at some point...)


# 1.13 01-Jun-2002 lukem

SIMPLEQ rototill:
- implement SIMPLEQ_REMOVE(head, elm, type, field). whilst it's O(n),
this mirrors the functionality of SLIST_REMOVE() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE()
- remove the unnecessary elm arg from SIMPLEQ_REMOVE_HEAD().
this mirrors the functionality of SLIST_REMOVE_HEAD() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE_HEAD()
- remove notes about SIMPLEQ not supporting arbitrary element removal
- use SIMPLEQ_FOREACH() instead of home-grown for loops
- use SIMPLEQ_EMPTY() appropriately
- use SIMPLEQ_*() instead of accessing sqh_first,sqh_last,sqe_next directly
- reorder manual page; be consistent about how the types are listed
- other minor cleanups


Revision tags: netbsd-1-6-PATCH002-RELEASE netbsd-1-6-PATCH002 netbsd-1-6-PATCH002-RC4 netbsd-1-6-PATCH002-RC3 netbsd-1-6-PATCH002-RC2 netbsd-1-6-PATCH002-RC1 netbsd-1-6-PATCH001 netbsd-1-6-PATCH001-RELEASE netbsd-1-6-PATCH001-RC3 netbsd-1-6-PATCH001-RC2 netbsd-1-6-PATCH001-RC1 netbsd-1-6-RELEASE netbsd-1-6-RC3 netbsd-1-6-RC2 netbsd-1-6-RC1 netbsd-1-6-base
# 1.12 26-Nov-2000 mycroft

branches: 1.12.2;
Return an error when there are too many segments rather than abort()ing.
This way `ldd /usr/libexec/ld.elf_so' doesn't core dump.


Revision tags: netbsd-1-5-PATCH002 netbsd-1-5-PATCH001 netbsd-1-5-RELEASE netbsd-1-5-BETA2 netbsd-1-5-BETA netbsd-1-5-ALPHA2 netbsd-1-5-base minoura-xpg4dl-base
# 1.11 13-Feb-2000 chs

branches: 1.11.4;
switch back to using a file mapping for the initial mapping of a new object.
this allows the kernel to use PMAP_PREFER() to give us better alignment on
platforms that care.


Revision tags: wrstuden-devbsize-19991221 wrstuden-devbsize-base
# 1.10 07-Nov-1999 mycroft

Changes from msaitoh to fix local/global symbol confusion, and to fix weak
symbol support -- updated by me for elf.h changes.


Revision tags: comdex-fall-1999-base
# 1.9 04-Nov-1999 erh

Remove ELFNAME defines since they are defined in sys/exec_elf.h


# 1.8 25-Oct-1999 kleink

Update to match new SVR4-style definition names in <sys/exec_elf.h>.


# 1.7 22-Oct-1999 hannken

Undo last change. While these macros are defined in exec_elf.h, they are
enclosed by `#ifdef _KERNEL'.


# 1.6 21-Oct-1999 erh

Remove ELFNAME macros which are already defines in exec_elf.h


# 1.5 06-Aug-1999 thorpej

branches: 1.5.2;
When allocating the entire address space of an object, map an anonymous
region first (using the data/bss protection) covering it, then overlay
the text and data regions at the appropriate offsets within the region,
and then unmap any gap between the text and data.

The previous method of maping the entire address space with the actual
file object itself is incorrect, as it may extend past the end of the
file if the section alignment is large enough.

This bug was the source of the libposix failure on the SPARC and another
similar failure (with libc!) on the Alpha (failure was accompanied by
a "uvn_io: size check fired" message on the console).


Revision tags: netbsd-1-4-PATCH003 netbsd-1-4-PATCH002 netbsd-1-4-PATCH001 netbsd-1-4-RELEASE netbsd-1-4-base
# 1.4 01-Mar-1999 christos

KNF


# 1.3 20-Feb-1998 mycroft

Fill in missing (default) mmap(2) flags.


Revision tags: netbsd-1-3-PATCH003 netbsd-1-3-PATCH003-CANDIDATE2 netbsd-1-3-PATCH003-CANDIDATE1 netbsd-1-3-PATCH003-CANDIDATE0 netbsd-1-3-PATCH002 netbsd-1-3-PATCH001 netbsd-1-3-RELEASE netbsd-1-3-BETA netbsd-1-3-base
# 1.2 17-Dec-1996 jonathan

Mipsel changes to ELF ldd/ld.so:
* don't abort() on NetBSD/pmax 1.1 elf binaries;
* Assume 32-bit elf unless compiling for Alpha;
* note bugs in ldd's README.


# 1.1 16-Dec-1996 cgd

First cut at an ELF shared loader. Originally from John Polstra's FreeBSD elf
kit, then hacked on by Matt Thomas <matt@3am-software.com>, then by me (to
make it work with new versions of the toolchain, etc.). This runs, but it's
in serious need of cleaning and/or a fair bit of reworking. See the README
file for more information, and a list of things to do.


# 1.65 12-Jan-2023 christos

fix the ldd build


# 1.64 12-Jan-2023 christos

Handle program headers properly; fixes c++ exceptions on arm32.


# 1.63 06-Jan-2023 christos

Remove the limitation of only being able to load binaries with 2 PT_LOAD
sections, like the kernel can. From FreeBSD.


Revision tags: netbsd-10-base
# 1.62 30-Mar-2022 hannken

_rtld_map_object(): no need to mmap an empty bss segment.


Revision tags: cjep_sun2x-base1 cjep_sun2x-base cjep_staticlib_x-base1 cjep_staticlib_x-base phil-wifi-20200421 phil-wifi-20200411 phil-wifi-20200406
# 1.61 04-Mar-2020 thorpej

PT_GNU_RELRO segments are arranged such that their vaddr + memsz ends
on a linker common page size boundary. However, if the common page size
used by the linker is less than the VM page size being used by the kernel,
this can end up in the middle of a VM page and when the region is write-
protected, this can cause objects in neighboring .data to get incorrectly
write-protected, resulting in a crash.

Avoid this situation by calculating the end of the RELRO region not by
rounding memsz up to the VM page size, but rather by adding vaddr + memsz
and then truncating to the VM page size.

Fixes PR toolchain/55043.

XXX pullup-9


Revision tags: is-mlppp-base netbsd-9-0-RELEASE netbsd-9-0-RC2 netbsd-9-0-RC1 phil-wifi-20191119 netbsd-9-base phil-wifi-20190609 pgoyette-compat-20190127 pgoyette-compat-20190118
# 1.60 06-Jan-2019 joerg

branches: 1.60.2;
When loading a non-PIE main binary, the virtual address must match.
Use MAP_TRYFIXED and verify that the result matches the expectation.


# 1.59 04-Jan-2019 joerg

MAP_ALIGNED has existed for years, just assume it exists.


Revision tags: pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521 pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base perseant-stdc-iso10646-base
# 1.58 19-Jun-2017 joerg

branches: 1.58.4; 1.58.6;
Replace COMBREL with just-in-time check in _rtld_relocate_nonplt_objects.

The COMBREL logic predates thread-safety of the dynamic linker and
breaks the use of shared locks for the common symbol lookup case. It is
unlikely to provide any benefit for lazy binding or PLT lookups, so
provide equivalent functionality in the non-PLT relocation handling loop
by checking if the symbol used by the current relocation is the same as
the one used during the last lookup. No inter-object cachine is done as
it is also unlikely to be benefical.

Testing with Firefox startup on AMD64 shows a small performance gain by
the new method.


Revision tags: netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1 pgoyette-localcount-20170320
# 1.57 10-Mar-2017 maya

branches: 1.57.4;
Revert to version 1.55 of map_object.c. This reverts:

"really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero."

As per toolchain/52054: src/libexec/ld.elf_so update breaks everything,
this commit is very broken for some people (but not others). chs mentioned
he has a fix, but best not to leave -current broken in the meantime.


# 1.56 09-Mar-2017 chs

really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero.


Revision tags: bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104 localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.55 16-Jun-2016 christos

branches: 1.55.2; 1.55.4;
Move relro after we've computed out relocbase and re-enable it.
(Matthias Weckbecker)


# 1.54 14-Jun-2016 christos

Add support for GNU RELRO headers from Matthias Weckbecker.


# 1.53 30-Oct-2014 martin

Avoid use after free, reported by the coverity scanner.


Revision tags: netbsd-7-0-2-RELEASE netbsd-7-0-1-RELEASE netbsd-7-0-RELEASE netbsd-7-0-RC3 netbsd-7-0-RC2 netbsd-7-0-RC1 netbsd-7-base yamt-pagecache-base9 tls-earlyentropy-base riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 tls-maxphys-base
# 1.52 03-Aug-2013 skrll

branches: 1.52.4;
Fix DEBUG build.


Revision tags: riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base
# 1.51 09-May-2013 christos

convert to SIMPLEQ like the rest of the queues.


# 1.50 08-May-2013 christos

need xfree() instead of free(). Using free() ends us up in an infinite loop.


# 1.49 06-May-2013 christos

Fixed handling of DT_SONAME:

- add function to add name aliases for shared libraries loaded
XXX[1]: we don't add a name during load time, only when DT_SONAME
is present.
- search already loaded objects in load_by_name for an already
loaded object that matches our name and return it.
- add missing initialization and cleanup for obj->names
XXX[2]: should we make them SIMPLEQ?
- Add XXX in rtld.c about getting the name of an object.

NB: This makes the jdk work again without resorting to a hack of putting
the build path of libjvm.so into the run path (which is a security
problem).
XXX: Pullup-6?


# 1.48 06-May-2013 skrll

Trailing whitespace


# 1.47 25-Apr-2013 skrll

Adjust exidx_start by relocbase


# 1.46 25-Apr-2013 skrll

Handle PT_ARM_EXIDX when mapping objects.


Revision tags: agc-symver-base yamt-pagecache-base8 yamt-pagecache-base7 yamt-pagecache-base6
# 1.45 13-Oct-2012 dholland

Improve the diagnostic messages for invalid/non-ELF files. Proposed a
while back; I forget the context. Has been sitting in one of my trees
awaiting a test run since, apparently, August 2011.


# 1.44 25-Jul-2012 martin

branches: 1.44.2;
Try to make dbg() printfs realy ELFSIZE independent


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 netbsd-6-1-RC2 netbsd-6-1-RC1 netbsd-6-0-1-RELEASE matt-nb6-plus-nbase netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.43 13-Aug-2011 christos

branches: 1.43.2;
consistent debugging info for program headers


Revision tags: cherry-xenmp-base
# 1.42 09-Mar-2011 joerg

Add TLS support infrastructure. For dynamic binaries, ld.elf_so exports
_rtld_tls_allocate and _rtld_tls_free. libpthread uses this functions to
setup the thread private area of all new threads. ld.elf_so is
responsible for setting up the private area for the initial thread.
Similar functions are called from _libc_init for static binaries, using
dl_iterate_phdr to access the ELF Program Header.

Add test cases to exercise the different TLS storage models. Test cases
are compiled and installed on all platforms, but are skipped on
platforms not marked for TLS support.

This material is based upon work partially supported by
The NetBSD Foundation under a contract with Joerg Sonnenberger.

It is inspired by the TLS support in FreeBSD by Doug Rabson and the
clean ups of the DragonFly port of the original FreeBSD modifications.


Revision tags: bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.41 16-Oct-2010 skrll

Implement dl_iterate_phdr.

Somewhat taken from FreeBSD. Manual page from OpenBSD.


# 1.40 11-Sep-2010 skrll

Wrap long line.


# 1.39 10-Jan-2010 skrll

Reset the COMBRELOC cache Obj_Entry if it was freed.

Fixes PR 41482. Done slightly differently to the patch in the PR.


Revision tags: matt-premerge-20091211
# 1.38 19-May-2009 christos

warns=4


Revision tags: jym-xensuspend-nbase jym-xensuspend-base
# 1.37 06-Jan-2009 mrg

add some (uintptr_t) to some casts, so these compile with ELFSIZE=32
*and _LP64. necessary for 32/64 bit combo ldd support.


Revision tags: netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 wrstuden-revivesa-base
# 1.36 03-Jun-2008 ad

branches: 1.36.4;
Don't bother unmapping the ELF header unless it overlaps the first load
section.


Revision tags: yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-baseX yamt-pf42-base2 yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase cube-autoconf-base matt-armv6-base hpcarm-cleanup-base
# 1.35 05-Oct-2007 ad

branches: 1.35.6; 1.35.8;
- Don't use the names malloc/free/etc, since we can end up pulling in libc's
malloc which is not what we want.
- Remove ancient memory compaction hack. Realloc on free memory is now an
error again (from joerg@).

Ok christos@.


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase wrstuden-fixsa-base-1 netbsd-4-0-RELEASE netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-mips64-base wrstuden-fixsa-base abandoned-netbsd-4-base netbsd-4-base
# 1.34 21-Mar-2006 christos

branches: 1.34.10;
Coverity CID 2749: Fix another double free. Instead of removing that last
free() call, change the allocation policy to leave the responsibility for
allocation/freeing the pathname to _rtld_map_object(), instead of having
the caller allocate it and _rtld_map_object() free it. This simplifies the
code a lot and it is more efficient.


# 1.33 18-Mar-2006 matt

Fix some double frees / missing frees / use after freed all
relating to _rtld_load_object. Fix Coverity CID 2601, 2600.


# 1.32 01-Jun-2005 lukem

appease gcc -Wuninitialized


Revision tags: netbsd-3-1-1-RELEASE netbsd-3-0-3-RELEASE netbsd-3-1-RELEASE netbsd-3-0-2-RELEASE netbsd-3-1-RC4 netbsd-3-1-RC3 netbsd-3-1-RC2 netbsd-3-1-RC1 netbsd-3-0-1-RELEASE netbsd-3-0-RELEASE netbsd-3-0-RC6 netbsd-3-0-RC5 netbsd-3-0-RC4 netbsd-3-0-RC3 netbsd-3-0-RC2 netbsd-3-0-RC1 netbsd-3-base
# 1.31 22-Oct-2004 skrll

Add __RCSIDs.

OK'd by mycroft.


Revision tags: netbsd-2-0-3-RELEASE netbsd-2-1-RELEASE netbsd-2-1-RC6 netbsd-2-1-RC5 netbsd-2-1-RC4 netbsd-2-1-RC3 netbsd-2-1-RC2 netbsd-2-1-RC1 netbsd-2-0-2-RELEASE netbsd-2-0-1-RELEASE netbsd-2-base netbsd-2-0-RELEASE netbsd-2-0-RC5 netbsd-2-0-RC4 netbsd-2-0-RC3 netbsd-2-0-RC2 netbsd-2-0-RC1 netbsd-2-0-base
# 1.30 24-Jul-2003 skrll

ANSIfy and de-__P().


# 1.29 05-Jun-2003 simonb

KNF- blank lines before code at start of functions.


# 1.28 06-Mar-2003 taca

make it compile in /usr/src/rescue directory.


# 1.27 06-Mar-2003 matt

Align psections to their specified alignment (if possible). Use
mmap(2) MAP_ALIGNED.


# 1.26 06-Feb-2003 fvdl

If fstat info was passed, and from it it can be seen that the file size
is smaller than an ELF header, don't even bother to try to look at it.
Avoids coredumps with zero-sized files.


Revision tags: fvdl_fs64_base
# 1.25 05-Dec-2002 junyoung

A bit speedup: in _rtld_load_object(), save the number of calls to
strcmp() by performing path name length comparison first. In the test
with Mozilla, the number was reduced to 1068 from 7182 (yes, we saved
6114 strcmp()!).


# 1.24 05-Oct-2002 mycroft

Minor cleanup.


# 1.23 05-Oct-2002 mycroft

Make this work for ldd again.


# 1.22 04-Oct-2002 mycroft

Rearrange _rtld_map_object() a little, so that we don't have to have the first
page of the object double-mapped. Not that it matters much, but someone was
whinging about it.
While I'm at it, nuke obj->phdr and obj->phsize; they're unused.


# 1.21 27-Sep-2002 mycroft

Change the munmap() of the text-data gap to mprotect(PROT_NONE). This avoids
having scattered little mappings, although it's probably not the best way...


# 1.20 24-Sep-2002 mycroft

Woops, unmap the first page in the `win' case, too.
Tidy up a bit.


# 1.19 24-Sep-2002 junyoung

Use _rtld_pagesz instead of PAGESIZE.


# 1.18 24-Sep-2002 junyoung

space -> tab.


# 1.17 24-Sep-2002 junyoung

Don't need to use union any longer.


# 1.16 23-Sep-2002 mycroft

Several small changes that shave 7-8% off the simple-exec-loop test:

* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().

* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.

* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.

* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)

* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().

Also:

* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.


# 1.15 13-Sep-2002 mycroft

Nuke two frivolous assert()s.


# 1.14 06-Sep-2002 mycroft

Introduce a new flag, `isdynamic', which is used to remember whether the
executable was of type ET_DYN. Use this instead of `mainprog' to determine
whether we need to do base-relative fixups of the PLT. (This allows loading
non-relocatable objects, should we desire to do that at some point...)


# 1.13 01-Jun-2002 lukem

SIMPLEQ rototill:
- implement SIMPLEQ_REMOVE(head, elm, type, field). whilst it's O(n),
this mirrors the functionality of SLIST_REMOVE() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE()
- remove the unnecessary elm arg from SIMPLEQ_REMOVE_HEAD().
this mirrors the functionality of SLIST_REMOVE_HEAD() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE_HEAD()
- remove notes about SIMPLEQ not supporting arbitrary element removal
- use SIMPLEQ_FOREACH() instead of home-grown for loops
- use SIMPLEQ_EMPTY() appropriately
- use SIMPLEQ_*() instead of accessing sqh_first,sqh_last,sqe_next directly
- reorder manual page; be consistent about how the types are listed
- other minor cleanups


Revision tags: netbsd-1-6-PATCH002-RELEASE netbsd-1-6-PATCH002 netbsd-1-6-PATCH002-RC4 netbsd-1-6-PATCH002-RC3 netbsd-1-6-PATCH002-RC2 netbsd-1-6-PATCH002-RC1 netbsd-1-6-PATCH001 netbsd-1-6-PATCH001-RELEASE netbsd-1-6-PATCH001-RC3 netbsd-1-6-PATCH001-RC2 netbsd-1-6-PATCH001-RC1 netbsd-1-6-RELEASE netbsd-1-6-RC3 netbsd-1-6-RC2 netbsd-1-6-RC1 netbsd-1-6-base
# 1.12 26-Nov-2000 mycroft

branches: 1.12.2;
Return an error when there are too many segments rather than abort()ing.
This way `ldd /usr/libexec/ld.elf_so' doesn't core dump.


Revision tags: netbsd-1-5-PATCH002 netbsd-1-5-PATCH001 netbsd-1-5-RELEASE netbsd-1-5-BETA2 netbsd-1-5-BETA netbsd-1-5-ALPHA2 netbsd-1-5-base minoura-xpg4dl-base
# 1.11 13-Feb-2000 chs

branches: 1.11.4;
switch back to using a file mapping for the initial mapping of a new object.
this allows the kernel to use PMAP_PREFER() to give us better alignment on
platforms that care.


Revision tags: wrstuden-devbsize-19991221 wrstuden-devbsize-base
# 1.10 07-Nov-1999 mycroft

Changes from msaitoh to fix local/global symbol confusion, and to fix weak
symbol support -- updated by me for elf.h changes.


Revision tags: comdex-fall-1999-base
# 1.9 04-Nov-1999 erh

Remove ELFNAME defines since they are defined in sys/exec_elf.h


# 1.8 25-Oct-1999 kleink

Update to match new SVR4-style definition names in <sys/exec_elf.h>.


# 1.7 22-Oct-1999 hannken

Undo last change. While these macros are defined in exec_elf.h, they are
enclosed by `#ifdef _KERNEL'.


# 1.6 21-Oct-1999 erh

Remove ELFNAME macros which are already defines in exec_elf.h


# 1.5 06-Aug-1999 thorpej

branches: 1.5.2;
When allocating the entire address space of an object, map an anonymous
region first (using the data/bss protection) covering it, then overlay
the text and data regions at the appropriate offsets within the region,
and then unmap any gap between the text and data.

The previous method of maping the entire address space with the actual
file object itself is incorrect, as it may extend past the end of the
file if the section alignment is large enough.

This bug was the source of the libposix failure on the SPARC and another
similar failure (with libc!) on the Alpha (failure was accompanied by
a "uvn_io: size check fired" message on the console).


Revision tags: netbsd-1-4-PATCH003 netbsd-1-4-PATCH002 netbsd-1-4-PATCH001 netbsd-1-4-RELEASE netbsd-1-4-base
# 1.4 01-Mar-1999 christos

KNF


# 1.3 20-Feb-1998 mycroft

Fill in missing (default) mmap(2) flags.


Revision tags: netbsd-1-3-PATCH003 netbsd-1-3-PATCH003-CANDIDATE2 netbsd-1-3-PATCH003-CANDIDATE1 netbsd-1-3-PATCH003-CANDIDATE0 netbsd-1-3-PATCH002 netbsd-1-3-PATCH001 netbsd-1-3-RELEASE netbsd-1-3-BETA netbsd-1-3-base
# 1.2 17-Dec-1996 jonathan

Mipsel changes to ELF ldd/ld.so:
* don't abort() on NetBSD/pmax 1.1 elf binaries;
* Assume 32-bit elf unless compiling for Alpha;
* note bugs in ldd's README.


# 1.1 16-Dec-1996 cgd

First cut at an ELF shared loader. Originally from John Polstra's FreeBSD elf
kit, then hacked on by Matt Thomas <matt@3am-software.com>, then by me (to
make it work with new versions of the toolchain, etc.). This runs, but it's
in serious need of cleaning and/or a fair bit of reworking. See the README
file for more information, and a list of things to do.


# 1.63 06-Jan-2023 christos

Remove the limitation of only being able to load binaries with 2 PT_LOAD
sections, like the kernel can. From FreeBSD.


Revision tags: netbsd-10-base
# 1.62 30-Mar-2022 hannken

_rtld_map_object(): no need to mmap an empty bss segment.


Revision tags: cjep_sun2x-base1 cjep_sun2x-base cjep_staticlib_x-base1 cjep_staticlib_x-base phil-wifi-20200421 phil-wifi-20200411 phil-wifi-20200406
# 1.61 04-Mar-2020 thorpej

PT_GNU_RELRO segments are arranged such that their vaddr + memsz ends
on a linker common page size boundary. However, if the common page size
used by the linker is less than the VM page size being used by the kernel,
this can end up in the middle of a VM page and when the region is write-
protected, this can cause objects in neighboring .data to get incorrectly
write-protected, resulting in a crash.

Avoid this situation by calculating the end of the RELRO region not by
rounding memsz up to the VM page size, but rather by adding vaddr + memsz
and then truncating to the VM page size.

Fixes PR toolchain/55043.

XXX pullup-9


Revision tags: is-mlppp-base netbsd-9-0-RELEASE netbsd-9-0-RC2 netbsd-9-0-RC1 phil-wifi-20191119 netbsd-9-base phil-wifi-20190609 pgoyette-compat-20190127 pgoyette-compat-20190118
# 1.60 06-Jan-2019 joerg

branches: 1.60.2;
When loading a non-PIE main binary, the virtual address must match.
Use MAP_TRYFIXED and verify that the result matches the expectation.


# 1.59 04-Jan-2019 joerg

MAP_ALIGNED has existed for years, just assume it exists.


Revision tags: pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521 pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base perseant-stdc-iso10646-base
# 1.58 19-Jun-2017 joerg

branches: 1.58.4; 1.58.6;
Replace COMBREL with just-in-time check in _rtld_relocate_nonplt_objects.

The COMBREL logic predates thread-safety of the dynamic linker and
breaks the use of shared locks for the common symbol lookup case. It is
unlikely to provide any benefit for lazy binding or PLT lookups, so
provide equivalent functionality in the non-PLT relocation handling loop
by checking if the symbol used by the current relocation is the same as
the one used during the last lookup. No inter-object cachine is done as
it is also unlikely to be benefical.

Testing with Firefox startup on AMD64 shows a small performance gain by
the new method.


Revision tags: netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1 pgoyette-localcount-20170320
# 1.57 10-Mar-2017 maya

branches: 1.57.4;
Revert to version 1.55 of map_object.c. This reverts:

"really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero."

As per toolchain/52054: src/libexec/ld.elf_so update breaks everything,
this commit is very broken for some people (but not others). chs mentioned
he has a fix, but best not to leave -current broken in the meantime.


# 1.56 09-Mar-2017 chs

really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero.


Revision tags: bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104 localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.55 16-Jun-2016 christos

branches: 1.55.2; 1.55.4;
Move relro after we've computed out relocbase and re-enable it.
(Matthias Weckbecker)


# 1.54 14-Jun-2016 christos

Add support for GNU RELRO headers from Matthias Weckbecker.


# 1.53 30-Oct-2014 martin

Avoid use after free, reported by the coverity scanner.


Revision tags: netbsd-7-0-2-RELEASE netbsd-7-0-1-RELEASE netbsd-7-0-RELEASE netbsd-7-0-RC3 netbsd-7-0-RC2 netbsd-7-0-RC1 netbsd-7-base yamt-pagecache-base9 tls-earlyentropy-base riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 tls-maxphys-base
# 1.52 03-Aug-2013 skrll

branches: 1.52.4;
Fix DEBUG build.


Revision tags: riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base
# 1.51 09-May-2013 christos

convert to SIMPLEQ like the rest of the queues.


# 1.50 08-May-2013 christos

need xfree() instead of free(). Using free() ends us up in an infinite loop.


# 1.49 06-May-2013 christos

Fixed handling of DT_SONAME:

- add function to add name aliases for shared libraries loaded
XXX[1]: we don't add a name during load time, only when DT_SONAME
is present.
- search already loaded objects in load_by_name for an already
loaded object that matches our name and return it.
- add missing initialization and cleanup for obj->names
XXX[2]: should we make them SIMPLEQ?
- Add XXX in rtld.c about getting the name of an object.

NB: This makes the jdk work again without resorting to a hack of putting
the build path of libjvm.so into the run path (which is a security
problem).
XXX: Pullup-6?


# 1.48 06-May-2013 skrll

Trailing whitespace


# 1.47 25-Apr-2013 skrll

Adjust exidx_start by relocbase


# 1.46 25-Apr-2013 skrll

Handle PT_ARM_EXIDX when mapping objects.


Revision tags: agc-symver-base yamt-pagecache-base8 yamt-pagecache-base7 yamt-pagecache-base6
# 1.45 13-Oct-2012 dholland

Improve the diagnostic messages for invalid/non-ELF files. Proposed a
while back; I forget the context. Has been sitting in one of my trees
awaiting a test run since, apparently, August 2011.


# 1.44 25-Jul-2012 martin

branches: 1.44.2;
Try to make dbg() printfs realy ELFSIZE independent


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 netbsd-6-1-RC2 netbsd-6-1-RC1 netbsd-6-0-1-RELEASE matt-nb6-plus-nbase netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.43 13-Aug-2011 christos

branches: 1.43.2;
consistent debugging info for program headers


Revision tags: cherry-xenmp-base
# 1.42 09-Mar-2011 joerg

Add TLS support infrastructure. For dynamic binaries, ld.elf_so exports
_rtld_tls_allocate and _rtld_tls_free. libpthread uses this functions to
setup the thread private area of all new threads. ld.elf_so is
responsible for setting up the private area for the initial thread.
Similar functions are called from _libc_init for static binaries, using
dl_iterate_phdr to access the ELF Program Header.

Add test cases to exercise the different TLS storage models. Test cases
are compiled and installed on all platforms, but are skipped on
platforms not marked for TLS support.

This material is based upon work partially supported by
The NetBSD Foundation under a contract with Joerg Sonnenberger.

It is inspired by the TLS support in FreeBSD by Doug Rabson and the
clean ups of the DragonFly port of the original FreeBSD modifications.


Revision tags: bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.41 16-Oct-2010 skrll

Implement dl_iterate_phdr.

Somewhat taken from FreeBSD. Manual page from OpenBSD.


# 1.40 11-Sep-2010 skrll

Wrap long line.


# 1.39 10-Jan-2010 skrll

Reset the COMBRELOC cache Obj_Entry if it was freed.

Fixes PR 41482. Done slightly differently to the patch in the PR.


Revision tags: matt-premerge-20091211
# 1.38 19-May-2009 christos

warns=4


Revision tags: jym-xensuspend-nbase jym-xensuspend-base
# 1.37 06-Jan-2009 mrg

add some (uintptr_t) to some casts, so these compile with ELFSIZE=32
*and _LP64. necessary for 32/64 bit combo ldd support.


Revision tags: netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 wrstuden-revivesa-base
# 1.36 03-Jun-2008 ad

branches: 1.36.4;
Don't bother unmapping the ELF header unless it overlaps the first load
section.


Revision tags: yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-baseX yamt-pf42-base2 yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase cube-autoconf-base matt-armv6-base hpcarm-cleanup-base
# 1.35 05-Oct-2007 ad

branches: 1.35.6; 1.35.8;
- Don't use the names malloc/free/etc, since we can end up pulling in libc's
malloc which is not what we want.
- Remove ancient memory compaction hack. Realloc on free memory is now an
error again (from joerg@).

Ok christos@.


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase wrstuden-fixsa-base-1 netbsd-4-0-RELEASE netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-mips64-base wrstuden-fixsa-base abandoned-netbsd-4-base netbsd-4-base
# 1.34 21-Mar-2006 christos

branches: 1.34.10;
Coverity CID 2749: Fix another double free. Instead of removing that last
free() call, change the allocation policy to leave the responsibility for
allocation/freeing the pathname to _rtld_map_object(), instead of having
the caller allocate it and _rtld_map_object() free it. This simplifies the
code a lot and it is more efficient.


# 1.33 18-Mar-2006 matt

Fix some double frees / missing frees / use after freed all
relating to _rtld_load_object. Fix Coverity CID 2601, 2600.


# 1.32 01-Jun-2005 lukem

appease gcc -Wuninitialized


Revision tags: netbsd-3-1-1-RELEASE netbsd-3-0-3-RELEASE netbsd-3-1-RELEASE netbsd-3-0-2-RELEASE netbsd-3-1-RC4 netbsd-3-1-RC3 netbsd-3-1-RC2 netbsd-3-1-RC1 netbsd-3-0-1-RELEASE netbsd-3-0-RELEASE netbsd-3-0-RC6 netbsd-3-0-RC5 netbsd-3-0-RC4 netbsd-3-0-RC3 netbsd-3-0-RC2 netbsd-3-0-RC1 netbsd-3-base
# 1.31 22-Oct-2004 skrll

Add __RCSIDs.

OK'd by mycroft.


Revision tags: netbsd-2-0-3-RELEASE netbsd-2-1-RELEASE netbsd-2-1-RC6 netbsd-2-1-RC5 netbsd-2-1-RC4 netbsd-2-1-RC3 netbsd-2-1-RC2 netbsd-2-1-RC1 netbsd-2-0-2-RELEASE netbsd-2-0-1-RELEASE netbsd-2-base netbsd-2-0-RELEASE netbsd-2-0-RC5 netbsd-2-0-RC4 netbsd-2-0-RC3 netbsd-2-0-RC2 netbsd-2-0-RC1 netbsd-2-0-base
# 1.30 24-Jul-2003 skrll

ANSIfy and de-__P().


# 1.29 05-Jun-2003 simonb

KNF- blank lines before code at start of functions.


# 1.28 06-Mar-2003 taca

make it compile in /usr/src/rescue directory.


# 1.27 06-Mar-2003 matt

Align psections to their specified alignment (if possible). Use
mmap(2) MAP_ALIGNED.


# 1.26 06-Feb-2003 fvdl

If fstat info was passed, and from it it can be seen that the file size
is smaller than an ELF header, don't even bother to try to look at it.
Avoids coredumps with zero-sized files.


Revision tags: fvdl_fs64_base
# 1.25 05-Dec-2002 junyoung

A bit speedup: in _rtld_load_object(), save the number of calls to
strcmp() by performing path name length comparison first. In the test
with Mozilla, the number was reduced to 1068 from 7182 (yes, we saved
6114 strcmp()!).


# 1.24 05-Oct-2002 mycroft

Minor cleanup.


# 1.23 05-Oct-2002 mycroft

Make this work for ldd again.


# 1.22 04-Oct-2002 mycroft

Rearrange _rtld_map_object() a little, so that we don't have to have the first
page of the object double-mapped. Not that it matters much, but someone was
whinging about it.
While I'm at it, nuke obj->phdr and obj->phsize; they're unused.


# 1.21 27-Sep-2002 mycroft

Change the munmap() of the text-data gap to mprotect(PROT_NONE). This avoids
having scattered little mappings, although it's probably not the best way...


# 1.20 24-Sep-2002 mycroft

Woops, unmap the first page in the `win' case, too.
Tidy up a bit.


# 1.19 24-Sep-2002 junyoung

Use _rtld_pagesz instead of PAGESIZE.


# 1.18 24-Sep-2002 junyoung

space -> tab.


# 1.17 24-Sep-2002 junyoung

Don't need to use union any longer.


# 1.16 23-Sep-2002 mycroft

Several small changes that shave 7-8% off the simple-exec-loop test:

* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().

* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.

* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.

* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)

* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().

Also:

* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.


# 1.15 13-Sep-2002 mycroft

Nuke two frivolous assert()s.


# 1.14 06-Sep-2002 mycroft

Introduce a new flag, `isdynamic', which is used to remember whether the
executable was of type ET_DYN. Use this instead of `mainprog' to determine
whether we need to do base-relative fixups of the PLT. (This allows loading
non-relocatable objects, should we desire to do that at some point...)


# 1.13 01-Jun-2002 lukem

SIMPLEQ rototill:
- implement SIMPLEQ_REMOVE(head, elm, type, field). whilst it's O(n),
this mirrors the functionality of SLIST_REMOVE() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE()
- remove the unnecessary elm arg from SIMPLEQ_REMOVE_HEAD().
this mirrors the functionality of SLIST_REMOVE_HEAD() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE_HEAD()
- remove notes about SIMPLEQ not supporting arbitrary element removal
- use SIMPLEQ_FOREACH() instead of home-grown for loops
- use SIMPLEQ_EMPTY() appropriately
- use SIMPLEQ_*() instead of accessing sqh_first,sqh_last,sqe_next directly
- reorder manual page; be consistent about how the types are listed
- other minor cleanups


Revision tags: netbsd-1-6-PATCH002-RELEASE netbsd-1-6-PATCH002 netbsd-1-6-PATCH002-RC4 netbsd-1-6-PATCH002-RC3 netbsd-1-6-PATCH002-RC2 netbsd-1-6-PATCH002-RC1 netbsd-1-6-PATCH001 netbsd-1-6-PATCH001-RELEASE netbsd-1-6-PATCH001-RC3 netbsd-1-6-PATCH001-RC2 netbsd-1-6-PATCH001-RC1 netbsd-1-6-RELEASE netbsd-1-6-RC3 netbsd-1-6-RC2 netbsd-1-6-RC1 netbsd-1-6-base
# 1.12 26-Nov-2000 mycroft

branches: 1.12.2;
Return an error when there are too many segments rather than abort()ing.
This way `ldd /usr/libexec/ld.elf_so' doesn't core dump.


Revision tags: netbsd-1-5-PATCH002 netbsd-1-5-PATCH001 netbsd-1-5-RELEASE netbsd-1-5-BETA2 netbsd-1-5-BETA netbsd-1-5-ALPHA2 netbsd-1-5-base minoura-xpg4dl-base
# 1.11 13-Feb-2000 chs

branches: 1.11.4;
switch back to using a file mapping for the initial mapping of a new object.
this allows the kernel to use PMAP_PREFER() to give us better alignment on
platforms that care.


Revision tags: wrstuden-devbsize-19991221 wrstuden-devbsize-base
# 1.10 07-Nov-1999 mycroft

Changes from msaitoh to fix local/global symbol confusion, and to fix weak
symbol support -- updated by me for elf.h changes.


Revision tags: comdex-fall-1999-base
# 1.9 04-Nov-1999 erh

Remove ELFNAME defines since they are defined in sys/exec_elf.h


# 1.8 25-Oct-1999 kleink

Update to match new SVR4-style definition names in <sys/exec_elf.h>.


# 1.7 22-Oct-1999 hannken

Undo last change. While these macros are defined in exec_elf.h, they are
enclosed by `#ifdef _KERNEL'.


# 1.6 21-Oct-1999 erh

Remove ELFNAME macros which are already defines in exec_elf.h


# 1.5 06-Aug-1999 thorpej

branches: 1.5.2;
When allocating the entire address space of an object, map an anonymous
region first (using the data/bss protection) covering it, then overlay
the text and data regions at the appropriate offsets within the region,
and then unmap any gap between the text and data.

The previous method of maping the entire address space with the actual
file object itself is incorrect, as it may extend past the end of the
file if the section alignment is large enough.

This bug was the source of the libposix failure on the SPARC and another
similar failure (with libc!) on the Alpha (failure was accompanied by
a "uvn_io: size check fired" message on the console).


Revision tags: netbsd-1-4-PATCH003 netbsd-1-4-PATCH002 netbsd-1-4-PATCH001 netbsd-1-4-RELEASE netbsd-1-4-base
# 1.4 01-Mar-1999 christos

KNF


# 1.3 20-Feb-1998 mycroft

Fill in missing (default) mmap(2) flags.


Revision tags: netbsd-1-3-PATCH003 netbsd-1-3-PATCH003-CANDIDATE2 netbsd-1-3-PATCH003-CANDIDATE1 netbsd-1-3-PATCH003-CANDIDATE0 netbsd-1-3-PATCH002 netbsd-1-3-PATCH001 netbsd-1-3-RELEASE netbsd-1-3-BETA netbsd-1-3-base
# 1.2 17-Dec-1996 jonathan

Mipsel changes to ELF ldd/ld.so:
* don't abort() on NetBSD/pmax 1.1 elf binaries;
* Assume 32-bit elf unless compiling for Alpha;
* note bugs in ldd's README.


# 1.1 16-Dec-1996 cgd

First cut at an ELF shared loader. Originally from John Polstra's FreeBSD elf
kit, then hacked on by Matt Thomas <matt@3am-software.com>, then by me (to
make it work with new versions of the toolchain, etc.). This runs, but it's
in serious need of cleaning and/or a fair bit of reworking. See the README
file for more information, and a list of things to do.


# 1.62 30-Mar-2022 hannken

_rtld_map_object(): no need to mmap an empty bss segment.


Revision tags: cjep_sun2x-base1 cjep_sun2x-base cjep_staticlib_x-base1 cjep_staticlib_x-base phil-wifi-20200421 phil-wifi-20200411 phil-wifi-20200406
# 1.61 04-Mar-2020 thorpej

PT_GNU_RELRO segments are arranged such that their vaddr + memsz ends
on a linker common page size boundary. However, if the common page size
used by the linker is less than the VM page size being used by the kernel,
this can end up in the middle of a VM page and when the region is write-
protected, this can cause objects in neighboring .data to get incorrectly
write-protected, resulting in a crash.

Avoid this situation by calculating the end of the RELRO region not by
rounding memsz up to the VM page size, but rather by adding vaddr + memsz
and then truncating to the VM page size.

Fixes PR toolchain/55043.

XXX pullup-9


Revision tags: is-mlppp-base netbsd-9-0-RELEASE netbsd-9-0-RC2 netbsd-9-0-RC1 phil-wifi-20191119 netbsd-9-base phil-wifi-20190609 pgoyette-compat-20190127 pgoyette-compat-20190118
# 1.60 06-Jan-2019 joerg

branches: 1.60.2;
When loading a non-PIE main binary, the virtual address must match.
Use MAP_TRYFIXED and verify that the result matches the expectation.


# 1.59 04-Jan-2019 joerg

MAP_ALIGNED has existed for years, just assume it exists.


Revision tags: pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521 pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base perseant-stdc-iso10646-base
# 1.58 19-Jun-2017 joerg

branches: 1.58.4; 1.58.6;
Replace COMBREL with just-in-time check in _rtld_relocate_nonplt_objects.

The COMBREL logic predates thread-safety of the dynamic linker and
breaks the use of shared locks for the common symbol lookup case. It is
unlikely to provide any benefit for lazy binding or PLT lookups, so
provide equivalent functionality in the non-PLT relocation handling loop
by checking if the symbol used by the current relocation is the same as
the one used during the last lookup. No inter-object cachine is done as
it is also unlikely to be benefical.

Testing with Firefox startup on AMD64 shows a small performance gain by
the new method.


Revision tags: netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1 pgoyette-localcount-20170320
# 1.57 10-Mar-2017 maya

branches: 1.57.4;
Revert to version 1.55 of map_object.c. This reverts:

"really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero."

As per toolchain/52054: src/libexec/ld.elf_so update breaks everything,
this commit is very broken for some people (but not others). chs mentioned
he has a fix, but best not to leave -current broken in the meantime.


# 1.56 09-Mar-2017 chs

really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero.


Revision tags: bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104 localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.55 16-Jun-2016 christos

branches: 1.55.2; 1.55.4;
Move relro after we've computed out relocbase and re-enable it.
(Matthias Weckbecker)


# 1.54 14-Jun-2016 christos

Add support for GNU RELRO headers from Matthias Weckbecker.


# 1.53 30-Oct-2014 martin

Avoid use after free, reported by the coverity scanner.


Revision tags: netbsd-7-0-2-RELEASE netbsd-7-0-1-RELEASE netbsd-7-0-RELEASE netbsd-7-0-RC3 netbsd-7-0-RC2 netbsd-7-0-RC1 netbsd-7-base yamt-pagecache-base9 tls-earlyentropy-base riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 tls-maxphys-base
# 1.52 03-Aug-2013 skrll

branches: 1.52.4;
Fix DEBUG build.


Revision tags: riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base
# 1.51 09-May-2013 christos

convert to SIMPLEQ like the rest of the queues.


# 1.50 08-May-2013 christos

need xfree() instead of free(). Using free() ends us up in an infinite loop.


# 1.49 06-May-2013 christos

Fixed handling of DT_SONAME:

- add function to add name aliases for shared libraries loaded
XXX[1]: we don't add a name during load time, only when DT_SONAME
is present.
- search already loaded objects in load_by_name for an already
loaded object that matches our name and return it.
- add missing initialization and cleanup for obj->names
XXX[2]: should we make them SIMPLEQ?
- Add XXX in rtld.c about getting the name of an object.

NB: This makes the jdk work again without resorting to a hack of putting
the build path of libjvm.so into the run path (which is a security
problem).
XXX: Pullup-6?


# 1.48 06-May-2013 skrll

Trailing whitespace


# 1.47 25-Apr-2013 skrll

Adjust exidx_start by relocbase


# 1.46 25-Apr-2013 skrll

Handle PT_ARM_EXIDX when mapping objects.


Revision tags: agc-symver-base yamt-pagecache-base8 yamt-pagecache-base7 yamt-pagecache-base6
# 1.45 13-Oct-2012 dholland

Improve the diagnostic messages for invalid/non-ELF files. Proposed a
while back; I forget the context. Has been sitting in one of my trees
awaiting a test run since, apparently, August 2011.


# 1.44 25-Jul-2012 martin

branches: 1.44.2;
Try to make dbg() printfs realy ELFSIZE independent


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 netbsd-6-1-RC2 netbsd-6-1-RC1 netbsd-6-0-1-RELEASE matt-nb6-plus-nbase netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.43 13-Aug-2011 christos

branches: 1.43.2;
consistent debugging info for program headers


Revision tags: cherry-xenmp-base
# 1.42 09-Mar-2011 joerg

Add TLS support infrastructure. For dynamic binaries, ld.elf_so exports
_rtld_tls_allocate and _rtld_tls_free. libpthread uses this functions to
setup the thread private area of all new threads. ld.elf_so is
responsible for setting up the private area for the initial thread.
Similar functions are called from _libc_init for static binaries, using
dl_iterate_phdr to access the ELF Program Header.

Add test cases to exercise the different TLS storage models. Test cases
are compiled and installed on all platforms, but are skipped on
platforms not marked for TLS support.

This material is based upon work partially supported by
The NetBSD Foundation under a contract with Joerg Sonnenberger.

It is inspired by the TLS support in FreeBSD by Doug Rabson and the
clean ups of the DragonFly port of the original FreeBSD modifications.


Revision tags: bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.41 16-Oct-2010 skrll

Implement dl_iterate_phdr.

Somewhat taken from FreeBSD. Manual page from OpenBSD.


# 1.40 11-Sep-2010 skrll

Wrap long line.


# 1.39 10-Jan-2010 skrll

Reset the COMBRELOC cache Obj_Entry if it was freed.

Fixes PR 41482. Done slightly differently to the patch in the PR.


Revision tags: matt-premerge-20091211
# 1.38 19-May-2009 christos

warns=4


Revision tags: jym-xensuspend-nbase jym-xensuspend-base
# 1.37 06-Jan-2009 mrg

add some (uintptr_t) to some casts, so these compile with ELFSIZE=32
*and _LP64. necessary for 32/64 bit combo ldd support.


Revision tags: netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 wrstuden-revivesa-base
# 1.36 03-Jun-2008 ad

branches: 1.36.4;
Don't bother unmapping the ELF header unless it overlaps the first load
section.


Revision tags: yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-baseX yamt-pf42-base2 yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase cube-autoconf-base matt-armv6-base hpcarm-cleanup-base
# 1.35 05-Oct-2007 ad

branches: 1.35.6; 1.35.8;
- Don't use the names malloc/free/etc, since we can end up pulling in libc's
malloc which is not what we want.
- Remove ancient memory compaction hack. Realloc on free memory is now an
error again (from joerg@).

Ok christos@.


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase wrstuden-fixsa-base-1 netbsd-4-0-RELEASE netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-mips64-base wrstuden-fixsa-base abandoned-netbsd-4-base netbsd-4-base
# 1.34 21-Mar-2006 christos

branches: 1.34.10;
Coverity CID 2749: Fix another double free. Instead of removing that last
free() call, change the allocation policy to leave the responsibility for
allocation/freeing the pathname to _rtld_map_object(), instead of having
the caller allocate it and _rtld_map_object() free it. This simplifies the
code a lot and it is more efficient.


# 1.33 18-Mar-2006 matt

Fix some double frees / missing frees / use after freed all
relating to _rtld_load_object. Fix Coverity CID 2601, 2600.


# 1.32 01-Jun-2005 lukem

appease gcc -Wuninitialized


Revision tags: netbsd-3-1-1-RELEASE netbsd-3-0-3-RELEASE netbsd-3-1-RELEASE netbsd-3-0-2-RELEASE netbsd-3-1-RC4 netbsd-3-1-RC3 netbsd-3-1-RC2 netbsd-3-1-RC1 netbsd-3-0-1-RELEASE netbsd-3-0-RELEASE netbsd-3-0-RC6 netbsd-3-0-RC5 netbsd-3-0-RC4 netbsd-3-0-RC3 netbsd-3-0-RC2 netbsd-3-0-RC1 netbsd-3-base
# 1.31 22-Oct-2004 skrll

Add __RCSIDs.

OK'd by mycroft.


Revision tags: netbsd-2-0-3-RELEASE netbsd-2-1-RELEASE netbsd-2-1-RC6 netbsd-2-1-RC5 netbsd-2-1-RC4 netbsd-2-1-RC3 netbsd-2-1-RC2 netbsd-2-1-RC1 netbsd-2-0-2-RELEASE netbsd-2-0-1-RELEASE netbsd-2-base netbsd-2-0-RELEASE netbsd-2-0-RC5 netbsd-2-0-RC4 netbsd-2-0-RC3 netbsd-2-0-RC2 netbsd-2-0-RC1 netbsd-2-0-base
# 1.30 24-Jul-2003 skrll

ANSIfy and de-__P().


# 1.29 05-Jun-2003 simonb

KNF- blank lines before code at start of functions.


# 1.28 06-Mar-2003 taca

make it compile in /usr/src/rescue directory.


# 1.27 06-Mar-2003 matt

Align psections to their specified alignment (if possible). Use
mmap(2) MAP_ALIGNED.


# 1.26 06-Feb-2003 fvdl

If fstat info was passed, and from it it can be seen that the file size
is smaller than an ELF header, don't even bother to try to look at it.
Avoids coredumps with zero-sized files.


Revision tags: fvdl_fs64_base
# 1.25 05-Dec-2002 junyoung

A bit speedup: in _rtld_load_object(), save the number of calls to
strcmp() by performing path name length comparison first. In the test
with Mozilla, the number was reduced to 1068 from 7182 (yes, we saved
6114 strcmp()!).


# 1.24 05-Oct-2002 mycroft

Minor cleanup.


# 1.23 05-Oct-2002 mycroft

Make this work for ldd again.


# 1.22 04-Oct-2002 mycroft

Rearrange _rtld_map_object() a little, so that we don't have to have the first
page of the object double-mapped. Not that it matters much, but someone was
whinging about it.
While I'm at it, nuke obj->phdr and obj->phsize; they're unused.


# 1.21 27-Sep-2002 mycroft

Change the munmap() of the text-data gap to mprotect(PROT_NONE). This avoids
having scattered little mappings, although it's probably not the best way...


# 1.20 24-Sep-2002 mycroft

Woops, unmap the first page in the `win' case, too.
Tidy up a bit.


# 1.19 24-Sep-2002 junyoung

Use _rtld_pagesz instead of PAGESIZE.


# 1.18 24-Sep-2002 junyoung

space -> tab.


# 1.17 24-Sep-2002 junyoung

Don't need to use union any longer.


# 1.16 23-Sep-2002 mycroft

Several small changes that shave 7-8% off the simple-exec-loop test:

* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().

* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.

* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.

* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)

* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().

Also:

* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.


# 1.15 13-Sep-2002 mycroft

Nuke two frivolous assert()s.


# 1.14 06-Sep-2002 mycroft

Introduce a new flag, `isdynamic', which is used to remember whether the
executable was of type ET_DYN. Use this instead of `mainprog' to determine
whether we need to do base-relative fixups of the PLT. (This allows loading
non-relocatable objects, should we desire to do that at some point...)


# 1.13 01-Jun-2002 lukem

SIMPLEQ rototill:
- implement SIMPLEQ_REMOVE(head, elm, type, field). whilst it's O(n),
this mirrors the functionality of SLIST_REMOVE() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE()
- remove the unnecessary elm arg from SIMPLEQ_REMOVE_HEAD().
this mirrors the functionality of SLIST_REMOVE_HEAD() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE_HEAD()
- remove notes about SIMPLEQ not supporting arbitrary element removal
- use SIMPLEQ_FOREACH() instead of home-grown for loops
- use SIMPLEQ_EMPTY() appropriately
- use SIMPLEQ_*() instead of accessing sqh_first,sqh_last,sqe_next directly
- reorder manual page; be consistent about how the types are listed
- other minor cleanups


Revision tags: netbsd-1-6-PATCH002-RELEASE netbsd-1-6-PATCH002 netbsd-1-6-PATCH002-RC4 netbsd-1-6-PATCH002-RC3 netbsd-1-6-PATCH002-RC2 netbsd-1-6-PATCH002-RC1 netbsd-1-6-PATCH001 netbsd-1-6-PATCH001-RELEASE netbsd-1-6-PATCH001-RC3 netbsd-1-6-PATCH001-RC2 netbsd-1-6-PATCH001-RC1 netbsd-1-6-RELEASE netbsd-1-6-RC3 netbsd-1-6-RC2 netbsd-1-6-RC1 netbsd-1-6-base
# 1.12 26-Nov-2000 mycroft

branches: 1.12.2;
Return an error when there are too many segments rather than abort()ing.
This way `ldd /usr/libexec/ld.elf_so' doesn't core dump.


Revision tags: netbsd-1-5-PATCH002 netbsd-1-5-PATCH001 netbsd-1-5-RELEASE netbsd-1-5-BETA2 netbsd-1-5-BETA netbsd-1-5-ALPHA2 netbsd-1-5-base minoura-xpg4dl-base
# 1.11 13-Feb-2000 chs

branches: 1.11.4;
switch back to using a file mapping for the initial mapping of a new object.
this allows the kernel to use PMAP_PREFER() to give us better alignment on
platforms that care.


Revision tags: wrstuden-devbsize-19991221 wrstuden-devbsize-base
# 1.10 07-Nov-1999 mycroft

Changes from msaitoh to fix local/global symbol confusion, and to fix weak
symbol support -- updated by me for elf.h changes.


Revision tags: comdex-fall-1999-base
# 1.9 04-Nov-1999 erh

Remove ELFNAME defines since they are defined in sys/exec_elf.h


# 1.8 25-Oct-1999 kleink

Update to match new SVR4-style definition names in <sys/exec_elf.h>.


# 1.7 22-Oct-1999 hannken

Undo last change. While these macros are defined in exec_elf.h, they are
enclosed by `#ifdef _KERNEL'.


# 1.6 21-Oct-1999 erh

Remove ELFNAME macros which are already defines in exec_elf.h


# 1.5 06-Aug-1999 thorpej

branches: 1.5.2;
When allocating the entire address space of an object, map an anonymous
region first (using the data/bss protection) covering it, then overlay
the text and data regions at the appropriate offsets within the region,
and then unmap any gap between the text and data.

The previous method of maping the entire address space with the actual
file object itself is incorrect, as it may extend past the end of the
file if the section alignment is large enough.

This bug was the source of the libposix failure on the SPARC and another
similar failure (with libc!) on the Alpha (failure was accompanied by
a "uvn_io: size check fired" message on the console).


Revision tags: netbsd-1-4-PATCH003 netbsd-1-4-PATCH002 netbsd-1-4-PATCH001 netbsd-1-4-RELEASE netbsd-1-4-base
# 1.4 01-Mar-1999 christos

KNF


# 1.3 20-Feb-1998 mycroft

Fill in missing (default) mmap(2) flags.


Revision tags: netbsd-1-3-PATCH003 netbsd-1-3-PATCH003-CANDIDATE2 netbsd-1-3-PATCH003-CANDIDATE1 netbsd-1-3-PATCH003-CANDIDATE0 netbsd-1-3-PATCH002 netbsd-1-3-PATCH001 netbsd-1-3-RELEASE netbsd-1-3-BETA netbsd-1-3-base
# 1.2 17-Dec-1996 jonathan

Mipsel changes to ELF ldd/ld.so:
* don't abort() on NetBSD/pmax 1.1 elf binaries;
* Assume 32-bit elf unless compiling for Alpha;
* note bugs in ldd's README.


# 1.1 16-Dec-1996 cgd

First cut at an ELF shared loader. Originally from John Polstra's FreeBSD elf
kit, then hacked on by Matt Thomas <matt@3am-software.com>, then by me (to
make it work with new versions of the toolchain, etc.). This runs, but it's
in serious need of cleaning and/or a fair bit of reworking. See the README
file for more information, and a list of things to do.


# 1.61 04-Mar-2020 thorpej

PT_GNU_RELRO segments are arranged such that their vaddr + memsz ends
on a linker common page size boundary. However, if the common page size
used by the linker is less than the VM page size being used by the kernel,
this can end up in the middle of a VM page and when the region is write-
protected, this can cause objects in neighboring .data to get incorrectly
write-protected, resulting in a crash.

Avoid this situation by calculating the end of the RELRO region not by
rounding memsz up to the VM page size, but rather by adding vaddr + memsz
and then truncating to the VM page size.

Fixes PR toolchain/55043.

XXX pullup-9


Revision tags: netbsd-9-0-RELEASE netbsd-9-0-RC2 netbsd-9-0-RC1 phil-wifi-20191119 netbsd-9-base phil-wifi-20190609 pgoyette-compat-20190127 pgoyette-compat-20190118
# 1.60 06-Jan-2019 joerg

When loading a non-PIE main binary, the virtual address must match.
Use MAP_TRYFIXED and verify that the result matches the expectation.


# 1.59 04-Jan-2019 joerg

MAP_ALIGNED has existed for years, just assume it exists.


Revision tags: pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521 pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base perseant-stdc-iso10646-base
# 1.58 19-Jun-2017 joerg

branches: 1.58.4; 1.58.6;
Replace COMBREL with just-in-time check in _rtld_relocate_nonplt_objects.

The COMBREL logic predates thread-safety of the dynamic linker and
breaks the use of shared locks for the common symbol lookup case. It is
unlikely to provide any benefit for lazy binding or PLT lookups, so
provide equivalent functionality in the non-PLT relocation handling loop
by checking if the symbol used by the current relocation is the same as
the one used during the last lookup. No inter-object cachine is done as
it is also unlikely to be benefical.

Testing with Firefox startup on AMD64 shows a small performance gain by
the new method.


Revision tags: netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1 pgoyette-localcount-20170320
# 1.57 10-Mar-2017 maya

branches: 1.57.4;
Revert to version 1.55 of map_object.c. This reverts:

"really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero."

As per toolchain/52054: src/libexec/ld.elf_so update breaks everything,
this commit is very broken for some people (but not others). chs mentioned
he has a fix, but best not to leave -current broken in the meantime.


# 1.56 09-Mar-2017 chs

really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero.


Revision tags: bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104 localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.55 16-Jun-2016 christos

branches: 1.55.2; 1.55.4;
Move relro after we've computed out relocbase and re-enable it.
(Matthias Weckbecker)


# 1.54 14-Jun-2016 christos

Add support for GNU RELRO headers from Matthias Weckbecker.


# 1.53 30-Oct-2014 martin

Avoid use after free, reported by the coverity scanner.


Revision tags: netbsd-7-0-2-RELEASE netbsd-7-0-1-RELEASE netbsd-7-0-RELEASE netbsd-7-0-RC3 netbsd-7-0-RC2 netbsd-7-0-RC1 netbsd-7-base yamt-pagecache-base9 tls-earlyentropy-base riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 tls-maxphys-base
# 1.52 03-Aug-2013 skrll

branches: 1.52.4;
Fix DEBUG build.


Revision tags: riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base
# 1.51 09-May-2013 christos

convert to SIMPLEQ like the rest of the queues.


# 1.50 08-May-2013 christos

need xfree() instead of free(). Using free() ends us up in an infinite loop.


# 1.49 06-May-2013 christos

Fixed handling of DT_SONAME:

- add function to add name aliases for shared libraries loaded
XXX[1]: we don't add a name during load time, only when DT_SONAME
is present.
- search already loaded objects in load_by_name for an already
loaded object that matches our name and return it.
- add missing initialization and cleanup for obj->names
XXX[2]: should we make them SIMPLEQ?
- Add XXX in rtld.c about getting the name of an object.

NB: This makes the jdk work again without resorting to a hack of putting
the build path of libjvm.so into the run path (which is a security
problem).
XXX: Pullup-6?


# 1.48 06-May-2013 skrll

Trailing whitespace


# 1.47 25-Apr-2013 skrll

Adjust exidx_start by relocbase


# 1.46 25-Apr-2013 skrll

Handle PT_ARM_EXIDX when mapping objects.


Revision tags: agc-symver-base yamt-pagecache-base8 yamt-pagecache-base7 yamt-pagecache-base6
# 1.45 13-Oct-2012 dholland

Improve the diagnostic messages for invalid/non-ELF files. Proposed a
while back; I forget the context. Has been sitting in one of my trees
awaiting a test run since, apparently, August 2011.


# 1.44 25-Jul-2012 martin

branches: 1.44.2;
Try to make dbg() printfs realy ELFSIZE independent


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 netbsd-6-1-RC2 netbsd-6-1-RC1 netbsd-6-0-1-RELEASE matt-nb6-plus-nbase netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.43 13-Aug-2011 christos

branches: 1.43.2;
consistent debugging info for program headers


Revision tags: cherry-xenmp-base
# 1.42 09-Mar-2011 joerg

Add TLS support infrastructure. For dynamic binaries, ld.elf_so exports
_rtld_tls_allocate and _rtld_tls_free. libpthread uses this functions to
setup the thread private area of all new threads. ld.elf_so is
responsible for setting up the private area for the initial thread.
Similar functions are called from _libc_init for static binaries, using
dl_iterate_phdr to access the ELF Program Header.

Add test cases to exercise the different TLS storage models. Test cases
are compiled and installed on all platforms, but are skipped on
platforms not marked for TLS support.

This material is based upon work partially supported by
The NetBSD Foundation under a contract with Joerg Sonnenberger.

It is inspired by the TLS support in FreeBSD by Doug Rabson and the
clean ups of the DragonFly port of the original FreeBSD modifications.


Revision tags: bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.41 16-Oct-2010 skrll

Implement dl_iterate_phdr.

Somewhat taken from FreeBSD. Manual page from OpenBSD.


# 1.40 11-Sep-2010 skrll

Wrap long line.


# 1.39 10-Jan-2010 skrll

Reset the COMBRELOC cache Obj_Entry if it was freed.

Fixes PR 41482. Done slightly differently to the patch in the PR.


Revision tags: matt-premerge-20091211
# 1.38 19-May-2009 christos

warns=4


Revision tags: jym-xensuspend-nbase jym-xensuspend-base
# 1.37 06-Jan-2009 mrg

add some (uintptr_t) to some casts, so these compile with ELFSIZE=32
*and _LP64. necessary for 32/64 bit combo ldd support.


Revision tags: netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 wrstuden-revivesa-base
# 1.36 03-Jun-2008 ad

branches: 1.36.4;
Don't bother unmapping the ELF header unless it overlaps the first load
section.


Revision tags: yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-baseX yamt-pf42-base2 yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase cube-autoconf-base matt-armv6-base hpcarm-cleanup-base
# 1.35 05-Oct-2007 ad

branches: 1.35.6; 1.35.8;
- Don't use the names malloc/free/etc, since we can end up pulling in libc's
malloc which is not what we want.
- Remove ancient memory compaction hack. Realloc on free memory is now an
error again (from joerg@).

Ok christos@.


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase wrstuden-fixsa-base-1 netbsd-4-0-RELEASE netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-mips64-base wrstuden-fixsa-base abandoned-netbsd-4-base netbsd-4-base
# 1.34 21-Mar-2006 christos

branches: 1.34.10;
Coverity CID 2749: Fix another double free. Instead of removing that last
free() call, change the allocation policy to leave the responsibility for
allocation/freeing the pathname to _rtld_map_object(), instead of having
the caller allocate it and _rtld_map_object() free it. This simplifies the
code a lot and it is more efficient.


# 1.33 18-Mar-2006 matt

Fix some double frees / missing frees / use after freed all
relating to _rtld_load_object. Fix Coverity CID 2601, 2600.


# 1.32 01-Jun-2005 lukem

appease gcc -Wuninitialized


Revision tags: netbsd-3-1-1-RELEASE netbsd-3-0-3-RELEASE netbsd-3-1-RELEASE netbsd-3-0-2-RELEASE netbsd-3-1-RC4 netbsd-3-1-RC3 netbsd-3-1-RC2 netbsd-3-1-RC1 netbsd-3-0-1-RELEASE netbsd-3-0-RELEASE netbsd-3-0-RC6 netbsd-3-0-RC5 netbsd-3-0-RC4 netbsd-3-0-RC3 netbsd-3-0-RC2 netbsd-3-0-RC1 netbsd-3-base
# 1.31 22-Oct-2004 skrll

Add __RCSIDs.

OK'd by mycroft.


Revision tags: netbsd-2-0-3-RELEASE netbsd-2-1-RELEASE netbsd-2-1-RC6 netbsd-2-1-RC5 netbsd-2-1-RC4 netbsd-2-1-RC3 netbsd-2-1-RC2 netbsd-2-1-RC1 netbsd-2-0-2-RELEASE netbsd-2-0-1-RELEASE netbsd-2-base netbsd-2-0-RELEASE netbsd-2-0-RC5 netbsd-2-0-RC4 netbsd-2-0-RC3 netbsd-2-0-RC2 netbsd-2-0-RC1 netbsd-2-0-base
# 1.30 24-Jul-2003 skrll

ANSIfy and de-__P().


# 1.29 05-Jun-2003 simonb

KNF- blank lines before code at start of functions.


# 1.28 06-Mar-2003 taca

make it compile in /usr/src/rescue directory.


# 1.27 06-Mar-2003 matt

Align psections to their specified alignment (if possible). Use
mmap(2) MAP_ALIGNED.


# 1.26 06-Feb-2003 fvdl

If fstat info was passed, and from it it can be seen that the file size
is smaller than an ELF header, don't even bother to try to look at it.
Avoids coredumps with zero-sized files.


Revision tags: fvdl_fs64_base
# 1.25 05-Dec-2002 junyoung

A bit speedup: in _rtld_load_object(), save the number of calls to
strcmp() by performing path name length comparison first. In the test
with Mozilla, the number was reduced to 1068 from 7182 (yes, we saved
6114 strcmp()!).


# 1.24 05-Oct-2002 mycroft

Minor cleanup.


# 1.23 05-Oct-2002 mycroft

Make this work for ldd again.


# 1.22 04-Oct-2002 mycroft

Rearrange _rtld_map_object() a little, so that we don't have to have the first
page of the object double-mapped. Not that it matters much, but someone was
whinging about it.
While I'm at it, nuke obj->phdr and obj->phsize; they're unused.


# 1.21 27-Sep-2002 mycroft

Change the munmap() of the text-data gap to mprotect(PROT_NONE). This avoids
having scattered little mappings, although it's probably not the best way...


# 1.20 24-Sep-2002 mycroft

Woops, unmap the first page in the `win' case, too.
Tidy up a bit.


# 1.19 24-Sep-2002 junyoung

Use _rtld_pagesz instead of PAGESIZE.


# 1.18 24-Sep-2002 junyoung

space -> tab.


# 1.17 24-Sep-2002 junyoung

Don't need to use union any longer.


# 1.16 23-Sep-2002 mycroft

Several small changes that shave 7-8% off the simple-exec-loop test:

* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().

* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.

* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.

* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)

* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().

Also:

* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.


# 1.15 13-Sep-2002 mycroft

Nuke two frivolous assert()s.


# 1.14 06-Sep-2002 mycroft

Introduce a new flag, `isdynamic', which is used to remember whether the
executable was of type ET_DYN. Use this instead of `mainprog' to determine
whether we need to do base-relative fixups of the PLT. (This allows loading
non-relocatable objects, should we desire to do that at some point...)


# 1.13 01-Jun-2002 lukem

SIMPLEQ rototill:
- implement SIMPLEQ_REMOVE(head, elm, type, field). whilst it's O(n),
this mirrors the functionality of SLIST_REMOVE() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE()
- remove the unnecessary elm arg from SIMPLEQ_REMOVE_HEAD().
this mirrors the functionality of SLIST_REMOVE_HEAD() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE_HEAD()
- remove notes about SIMPLEQ not supporting arbitrary element removal
- use SIMPLEQ_FOREACH() instead of home-grown for loops
- use SIMPLEQ_EMPTY() appropriately
- use SIMPLEQ_*() instead of accessing sqh_first,sqh_last,sqe_next directly
- reorder manual page; be consistent about how the types are listed
- other minor cleanups


Revision tags: netbsd-1-6-PATCH002-RELEASE netbsd-1-6-PATCH002 netbsd-1-6-PATCH002-RC4 netbsd-1-6-PATCH002-RC3 netbsd-1-6-PATCH002-RC2 netbsd-1-6-PATCH002-RC1 netbsd-1-6-PATCH001 netbsd-1-6-PATCH001-RELEASE netbsd-1-6-PATCH001-RC3 netbsd-1-6-PATCH001-RC2 netbsd-1-6-PATCH001-RC1 netbsd-1-6-RELEASE netbsd-1-6-RC3 netbsd-1-6-RC2 netbsd-1-6-RC1 netbsd-1-6-base
# 1.12 26-Nov-2000 mycroft

branches: 1.12.2;
Return an error when there are too many segments rather than abort()ing.
This way `ldd /usr/libexec/ld.elf_so' doesn't core dump.


Revision tags: netbsd-1-5-PATCH002 netbsd-1-5-PATCH001 netbsd-1-5-RELEASE netbsd-1-5-BETA2 netbsd-1-5-BETA netbsd-1-5-ALPHA2 netbsd-1-5-base minoura-xpg4dl-base
# 1.11 13-Feb-2000 chs

branches: 1.11.4;
switch back to using a file mapping for the initial mapping of a new object.
this allows the kernel to use PMAP_PREFER() to give us better alignment on
platforms that care.


Revision tags: wrstuden-devbsize-19991221 wrstuden-devbsize-base
# 1.10 07-Nov-1999 mycroft

Changes from msaitoh to fix local/global symbol confusion, and to fix weak
symbol support -- updated by me for elf.h changes.


Revision tags: comdex-fall-1999-base
# 1.9 04-Nov-1999 erh

Remove ELFNAME defines since they are defined in sys/exec_elf.h


# 1.8 25-Oct-1999 kleink

Update to match new SVR4-style definition names in <sys/exec_elf.h>.


# 1.7 22-Oct-1999 hannken

Undo last change. While these macros are defined in exec_elf.h, they are
enclosed by `#ifdef _KERNEL'.


# 1.6 21-Oct-1999 erh

Remove ELFNAME macros which are already defines in exec_elf.h


# 1.5 06-Aug-1999 thorpej

branches: 1.5.2;
When allocating the entire address space of an object, map an anonymous
region first (using the data/bss protection) covering it, then overlay
the text and data regions at the appropriate offsets within the region,
and then unmap any gap between the text and data.

The previous method of maping the entire address space with the actual
file object itself is incorrect, as it may extend past the end of the
file if the section alignment is large enough.

This bug was the source of the libposix failure on the SPARC and another
similar failure (with libc!) on the Alpha (failure was accompanied by
a "uvn_io: size check fired" message on the console).


Revision tags: netbsd-1-4-PATCH003 netbsd-1-4-PATCH002 netbsd-1-4-PATCH001 netbsd-1-4-RELEASE netbsd-1-4-base
# 1.4 01-Mar-1999 christos

KNF


# 1.3 20-Feb-1998 mycroft

Fill in missing (default) mmap(2) flags.


Revision tags: netbsd-1-3-PATCH003 netbsd-1-3-PATCH003-CANDIDATE2 netbsd-1-3-PATCH003-CANDIDATE1 netbsd-1-3-PATCH003-CANDIDATE0 netbsd-1-3-PATCH002 netbsd-1-3-PATCH001 netbsd-1-3-RELEASE netbsd-1-3-BETA netbsd-1-3-base
# 1.2 17-Dec-1996 jonathan

Mipsel changes to ELF ldd/ld.so:
* don't abort() on NetBSD/pmax 1.1 elf binaries;
* Assume 32-bit elf unless compiling for Alpha;
* note bugs in ldd's README.


# 1.1 16-Dec-1996 cgd

First cut at an ELF shared loader. Originally from John Polstra's FreeBSD elf
kit, then hacked on by Matt Thomas <matt@3am-software.com>, then by me (to
make it work with new versions of the toolchain, etc.). This runs, but it's
in serious need of cleaning and/or a fair bit of reworking. See the README
file for more information, and a list of things to do.


Revision tags: pgoyette-compat-20190127 pgoyette-compat-20190118
# 1.60 06-Jan-2019 joerg

When loading a non-PIE main binary, the virtual address must match.
Use MAP_TRYFIXED and verify that the result matches the expectation.


# 1.59 04-Jan-2019 joerg

MAP_ALIGNED has existed for years, just assume it exists.


Revision tags: pgoyette-compat-1226 pgoyette-compat-1126 pgoyette-compat-1020 pgoyette-compat-0930 pgoyette-compat-0906 pgoyette-compat-0728 phil-wifi-base pgoyette-compat-0625 pgoyette-compat-0521 pgoyette-compat-0502 pgoyette-compat-0422 pgoyette-compat-0415 pgoyette-compat-0407 pgoyette-compat-0330 pgoyette-compat-0322 pgoyette-compat-0315 pgoyette-compat-base perseant-stdc-iso10646-base
# 1.58 19-Jun-2017 joerg

branches: 1.58.4;
Replace COMBREL with just-in-time check in _rtld_relocate_nonplt_objects.

The COMBREL logic predates thread-safety of the dynamic linker and
breaks the use of shared locks for the common symbol lookup case. It is
unlikely to provide any benefit for lazy binding or PLT lookups, so
provide equivalent functionality in the non-PLT relocation handling loop
by checking if the symbol used by the current relocation is the same as
the one used during the last lookup. No inter-object cachine is done as
it is also unlikely to be benefical.

Testing with Firefox startup on AMD64 shows a small performance gain by
the new method.


Revision tags: netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1 pgoyette-localcount-20170320
# 1.57 10-Mar-2017 maya

branches: 1.57.4;
Revert to version 1.55 of map_object.c. This reverts:

"really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero."

As per toolchain/52054: src/libexec/ld.elf_so update breaks everything,
this commit is very broken for some people (but not others). chs mentioned
he has a fix, but best not to leave -current broken in the meantime.


# 1.56 09-Mar-2017 chs

really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero.


Revision tags: bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104 localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.55 16-Jun-2016 christos

branches: 1.55.2; 1.55.4;
Move relro after we've computed out relocbase and re-enable it.
(Matthias Weckbecker)


# 1.54 14-Jun-2016 christos

Add support for GNU RELRO headers from Matthias Weckbecker.


# 1.53 30-Oct-2014 martin

Avoid use after free, reported by the coverity scanner.


Revision tags: netbsd-7-0-2-RELEASE netbsd-7-0-1-RELEASE netbsd-7-0-RELEASE netbsd-7-0-RC3 netbsd-7-0-RC2 netbsd-7-0-RC1 netbsd-7-base yamt-pagecache-base9 tls-earlyentropy-base riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 tls-maxphys-base
# 1.52 03-Aug-2013 skrll

branches: 1.52.4;
Fix DEBUG build.


Revision tags: riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base
# 1.51 09-May-2013 christos

convert to SIMPLEQ like the rest of the queues.


# 1.50 08-May-2013 christos

need xfree() instead of free(). Using free() ends us up in an infinite loop.


# 1.49 06-May-2013 christos

Fixed handling of DT_SONAME:

- add function to add name aliases for shared libraries loaded
XXX[1]: we don't add a name during load time, only when DT_SONAME
is present.
- search already loaded objects in load_by_name for an already
loaded object that matches our name and return it.
- add missing initialization and cleanup for obj->names
XXX[2]: should we make them SIMPLEQ?
- Add XXX in rtld.c about getting the name of an object.

NB: This makes the jdk work again without resorting to a hack of putting
the build path of libjvm.so into the run path (which is a security
problem).
XXX: Pullup-6?


# 1.48 06-May-2013 skrll

Trailing whitespace


# 1.47 25-Apr-2013 skrll

Adjust exidx_start by relocbase


# 1.46 25-Apr-2013 skrll

Handle PT_ARM_EXIDX when mapping objects.


Revision tags: agc-symver-base yamt-pagecache-base8 yamt-pagecache-base7 yamt-pagecache-base6
# 1.45 13-Oct-2012 dholland

Improve the diagnostic messages for invalid/non-ELF files. Proposed a
while back; I forget the context. Has been sitting in one of my trees
awaiting a test run since, apparently, August 2011.


# 1.44 25-Jul-2012 martin

branches: 1.44.2;
Try to make dbg() printfs realy ELFSIZE independent


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 netbsd-6-1-RC2 netbsd-6-1-RC1 netbsd-6-0-1-RELEASE matt-nb6-plus-nbase netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.43 13-Aug-2011 christos

branches: 1.43.2;
consistent debugging info for program headers


Revision tags: cherry-xenmp-base
# 1.42 09-Mar-2011 joerg

Add TLS support infrastructure. For dynamic binaries, ld.elf_so exports
_rtld_tls_allocate and _rtld_tls_free. libpthread uses this functions to
setup the thread private area of all new threads. ld.elf_so is
responsible for setting up the private area for the initial thread.
Similar functions are called from _libc_init for static binaries, using
dl_iterate_phdr to access the ELF Program Header.

Add test cases to exercise the different TLS storage models. Test cases
are compiled and installed on all platforms, but are skipped on
platforms not marked for TLS support.

This material is based upon work partially supported by
The NetBSD Foundation under a contract with Joerg Sonnenberger.

It is inspired by the TLS support in FreeBSD by Doug Rabson and the
clean ups of the DragonFly port of the original FreeBSD modifications.


Revision tags: bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.41 16-Oct-2010 skrll

Implement dl_iterate_phdr.

Somewhat taken from FreeBSD. Manual page from OpenBSD.


# 1.40 11-Sep-2010 skrll

Wrap long line.


# 1.39 10-Jan-2010 skrll

Reset the COMBRELOC cache Obj_Entry if it was freed.

Fixes PR 41482. Done slightly differently to the patch in the PR.


Revision tags: matt-premerge-20091211
# 1.38 19-May-2009 christos

warns=4


Revision tags: jym-xensuspend-nbase jym-xensuspend-base
# 1.37 06-Jan-2009 mrg

add some (uintptr_t) to some casts, so these compile with ELFSIZE=32
*and _LP64. necessary for 32/64 bit combo ldd support.


Revision tags: netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 wrstuden-revivesa-base
# 1.36 03-Jun-2008 ad

branches: 1.36.4;
Don't bother unmapping the ELF header unless it overlaps the first load
section.


Revision tags: yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-baseX yamt-pf42-base2 yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase cube-autoconf-base matt-armv6-base hpcarm-cleanup-base
# 1.35 05-Oct-2007 ad

branches: 1.35.6; 1.35.8;
- Don't use the names malloc/free/etc, since we can end up pulling in libc's
malloc which is not what we want.
- Remove ancient memory compaction hack. Realloc on free memory is now an
error again (from joerg@).

Ok christos@.


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase wrstuden-fixsa-base-1 netbsd-4-0-RELEASE netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-mips64-base wrstuden-fixsa-base abandoned-netbsd-4-base netbsd-4-base
# 1.34 21-Mar-2006 christos

branches: 1.34.10;
Coverity CID 2749: Fix another double free. Instead of removing that last
free() call, change the allocation policy to leave the responsibility for
allocation/freeing the pathname to _rtld_map_object(), instead of having
the caller allocate it and _rtld_map_object() free it. This simplifies the
code a lot and it is more efficient.


# 1.33 18-Mar-2006 matt

Fix some double frees / missing frees / use after freed all
relating to _rtld_load_object. Fix Coverity CID 2601, 2600.


# 1.32 01-Jun-2005 lukem

appease gcc -Wuninitialized


Revision tags: netbsd-3-1-1-RELEASE netbsd-3-0-3-RELEASE netbsd-3-1-RELEASE netbsd-3-0-2-RELEASE netbsd-3-1-RC4 netbsd-3-1-RC3 netbsd-3-1-RC2 netbsd-3-1-RC1 netbsd-3-0-1-RELEASE netbsd-3-0-RELEASE netbsd-3-0-RC6 netbsd-3-0-RC5 netbsd-3-0-RC4 netbsd-3-0-RC3 netbsd-3-0-RC2 netbsd-3-0-RC1 netbsd-3-base
# 1.31 22-Oct-2004 skrll

Add __RCSIDs.

OK'd by mycroft.


Revision tags: netbsd-2-0-3-RELEASE netbsd-2-1-RELEASE netbsd-2-1-RC6 netbsd-2-1-RC5 netbsd-2-1-RC4 netbsd-2-1-RC3 netbsd-2-1-RC2 netbsd-2-1-RC1 netbsd-2-0-2-RELEASE netbsd-2-0-1-RELEASE netbsd-2-base netbsd-2-0-RELEASE netbsd-2-0-RC5 netbsd-2-0-RC4 netbsd-2-0-RC3 netbsd-2-0-RC2 netbsd-2-0-RC1 netbsd-2-0-base
# 1.30 24-Jul-2003 skrll

ANSIfy and de-__P().


# 1.29 05-Jun-2003 simonb

KNF- blank lines before code at start of functions.


# 1.28 06-Mar-2003 taca

make it compile in /usr/src/rescue directory.


# 1.27 06-Mar-2003 matt

Align psections to their specified alignment (if possible). Use
mmap(2) MAP_ALIGNED.


# 1.26 06-Feb-2003 fvdl

If fstat info was passed, and from it it can be seen that the file size
is smaller than an ELF header, don't even bother to try to look at it.
Avoids coredumps with zero-sized files.


Revision tags: fvdl_fs64_base
# 1.25 05-Dec-2002 junyoung

A bit speedup: in _rtld_load_object(), save the number of calls to
strcmp() by performing path name length comparison first. In the test
with Mozilla, the number was reduced to 1068 from 7182 (yes, we saved
6114 strcmp()!).


# 1.24 05-Oct-2002 mycroft

Minor cleanup.


# 1.23 05-Oct-2002 mycroft

Make this work for ldd again.


# 1.22 04-Oct-2002 mycroft

Rearrange _rtld_map_object() a little, so that we don't have to have the first
page of the object double-mapped. Not that it matters much, but someone was
whinging about it.
While I'm at it, nuke obj->phdr and obj->phsize; they're unused.


# 1.21 27-Sep-2002 mycroft

Change the munmap() of the text-data gap to mprotect(PROT_NONE). This avoids
having scattered little mappings, although it's probably not the best way...


# 1.20 24-Sep-2002 mycroft

Woops, unmap the first page in the `win' case, too.
Tidy up a bit.


# 1.19 24-Sep-2002 junyoung

Use _rtld_pagesz instead of PAGESIZE.


# 1.18 24-Sep-2002 junyoung

space -> tab.


# 1.17 24-Sep-2002 junyoung

Don't need to use union any longer.


# 1.16 23-Sep-2002 mycroft

Several small changes that shave 7-8% off the simple-exec-loop test:

* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().

* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.

* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.

* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)

* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().

Also:

* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.


# 1.15 13-Sep-2002 mycroft

Nuke two frivolous assert()s.


# 1.14 06-Sep-2002 mycroft

Introduce a new flag, `isdynamic', which is used to remember whether the
executable was of type ET_DYN. Use this instead of `mainprog' to determine
whether we need to do base-relative fixups of the PLT. (This allows loading
non-relocatable objects, should we desire to do that at some point...)


# 1.13 01-Jun-2002 lukem

SIMPLEQ rototill:
- implement SIMPLEQ_REMOVE(head, elm, type, field). whilst it's O(n),
this mirrors the functionality of SLIST_REMOVE() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE()
- remove the unnecessary elm arg from SIMPLEQ_REMOVE_HEAD().
this mirrors the functionality of SLIST_REMOVE_HEAD() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE_HEAD()
- remove notes about SIMPLEQ not supporting arbitrary element removal
- use SIMPLEQ_FOREACH() instead of home-grown for loops
- use SIMPLEQ_EMPTY() appropriately
- use SIMPLEQ_*() instead of accessing sqh_first,sqh_last,sqe_next directly
- reorder manual page; be consistent about how the types are listed
- other minor cleanups


Revision tags: netbsd-1-6-PATCH002-RELEASE netbsd-1-6-PATCH002 netbsd-1-6-PATCH002-RC4 netbsd-1-6-PATCH002-RC3 netbsd-1-6-PATCH002-RC2 netbsd-1-6-PATCH002-RC1 netbsd-1-6-PATCH001 netbsd-1-6-PATCH001-RELEASE netbsd-1-6-PATCH001-RC3 netbsd-1-6-PATCH001-RC2 netbsd-1-6-PATCH001-RC1 netbsd-1-6-RELEASE netbsd-1-6-RC3 netbsd-1-6-RC2 netbsd-1-6-RC1 netbsd-1-6-base
# 1.12 26-Nov-2000 mycroft

branches: 1.12.2;
Return an error when there are too many segments rather than abort()ing.
This way `ldd /usr/libexec/ld.elf_so' doesn't core dump.


Revision tags: netbsd-1-5-PATCH002 netbsd-1-5-PATCH001 netbsd-1-5-RELEASE netbsd-1-5-BETA2 netbsd-1-5-BETA netbsd-1-5-ALPHA2 netbsd-1-5-base minoura-xpg4dl-base
# 1.11 13-Feb-2000 chs

branches: 1.11.4;
switch back to using a file mapping for the initial mapping of a new object.
this allows the kernel to use PMAP_PREFER() to give us better alignment on
platforms that care.


Revision tags: wrstuden-devbsize-19991221 wrstuden-devbsize-base
# 1.10 07-Nov-1999 mycroft

Changes from msaitoh to fix local/global symbol confusion, and to fix weak
symbol support -- updated by me for elf.h changes.


Revision tags: comdex-fall-1999-base
# 1.9 04-Nov-1999 erh

Remove ELFNAME defines since they are defined in sys/exec_elf.h


# 1.8 25-Oct-1999 kleink

Update to match new SVR4-style definition names in <sys/exec_elf.h>.


# 1.7 22-Oct-1999 hannken

Undo last change. While these macros are defined in exec_elf.h, they are
enclosed by `#ifdef _KERNEL'.


# 1.6 21-Oct-1999 erh

Remove ELFNAME macros which are already defines in exec_elf.h


# 1.5 06-Aug-1999 thorpej

branches: 1.5.2;
When allocating the entire address space of an object, map an anonymous
region first (using the data/bss protection) covering it, then overlay
the text and data regions at the appropriate offsets within the region,
and then unmap any gap between the text and data.

The previous method of maping the entire address space with the actual
file object itself is incorrect, as it may extend past the end of the
file if the section alignment is large enough.

This bug was the source of the libposix failure on the SPARC and another
similar failure (with libc!) on the Alpha (failure was accompanied by
a "uvn_io: size check fired" message on the console).


Revision tags: netbsd-1-4-PATCH003 netbsd-1-4-PATCH002 netbsd-1-4-PATCH001 netbsd-1-4-RELEASE netbsd-1-4-base
# 1.4 01-Mar-1999 christos

KNF


# 1.3 20-Feb-1998 mycroft

Fill in missing (default) mmap(2) flags.


Revision tags: netbsd-1-3-PATCH003 netbsd-1-3-PATCH003-CANDIDATE2 netbsd-1-3-PATCH003-CANDIDATE1 netbsd-1-3-PATCH003-CANDIDATE0 netbsd-1-3-PATCH002 netbsd-1-3-PATCH001 netbsd-1-3-RELEASE netbsd-1-3-BETA netbsd-1-3-base
# 1.2 17-Dec-1996 jonathan

Mipsel changes to ELF ldd/ld.so:
* don't abort() on NetBSD/pmax 1.1 elf binaries;
* Assume 32-bit elf unless compiling for Alpha;
* note bugs in ldd's README.


# 1.1 16-Dec-1996 cgd

First cut at an ELF shared loader. Originally from John Polstra's FreeBSD elf
kit, then hacked on by Matt Thomas <matt@3am-software.com>, then by me (to
make it work with new versions of the toolchain, etc.). This runs, but it's
in serious need of cleaning and/or a fair bit of reworking. See the README
file for more information, and a list of things to do.


# 1.58 19-Jun-2017 joerg

Replace COMBREL with just-in-time check in _rtld_relocate_nonplt_objects.

The COMBREL logic predates thread-safety of the dynamic linker and
breaks the use of shared locks for the common symbol lookup case. It is
unlikely to provide any benefit for lazy binding or PLT lookups, so
provide equivalent functionality in the non-PLT relocation handling loop
by checking if the symbol used by the current relocation is the same as
the one used during the last lookup. No inter-object cachine is done as
it is also unlikely to be benefical.

Testing with Firefox startup on AMD64 shows a small performance gain by
the new method.


Revision tags: netbsd-8-base prg-localcount2-base3 prg-localcount2-base2 prg-localcount2-base1 prg-localcount2-base pgoyette-localcount-20170426 bouyer-socketcan-base1 pgoyette-localcount-20170320
# 1.57 10-Mar-2017 maya

Revert to version 1.55 of map_object.c. This reverts:

"really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero."

As per toolchain/52054: src/libexec/ld.elf_so update breaks everything,
this commit is very broken for some people (but not others). chs mentioned
he has a fix, but best not to leave -current broken in the meantime.


# 1.56 09-Mar-2017 chs

really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero.


Revision tags: bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104 localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.55 16-Jun-2016 christos

branches: 1.55.2; 1.55.4;
Move relro after we've computed out relocbase and re-enable it.
(Matthias Weckbecker)


# 1.54 14-Jun-2016 christos

Add support for GNU RELRO headers from Matthias Weckbecker.


# 1.53 30-Oct-2014 martin

Avoid use after free, reported by the coverity scanner.


Revision tags: netbsd-7-0-2-RELEASE netbsd-7-0-1-RELEASE netbsd-7-0-RELEASE netbsd-7-0-RC3 netbsd-7-0-RC2 netbsd-7-0-RC1 netbsd-7-base yamt-pagecache-base9 tls-earlyentropy-base riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 tls-maxphys-base
# 1.52 03-Aug-2013 skrll

branches: 1.52.4;
Fix DEBUG build.


Revision tags: riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base
# 1.51 09-May-2013 christos

convert to SIMPLEQ like the rest of the queues.


# 1.50 08-May-2013 christos

need xfree() instead of free(). Using free() ends us up in an infinite loop.


# 1.49 06-May-2013 christos

Fixed handling of DT_SONAME:

- add function to add name aliases for shared libraries loaded
XXX[1]: we don't add a name during load time, only when DT_SONAME
is present.
- search already loaded objects in load_by_name for an already
loaded object that matches our name and return it.
- add missing initialization and cleanup for obj->names
XXX[2]: should we make them SIMPLEQ?
- Add XXX in rtld.c about getting the name of an object.

NB: This makes the jdk work again without resorting to a hack of putting
the build path of libjvm.so into the run path (which is a security
problem).
XXX: Pullup-6?


# 1.48 06-May-2013 skrll

Trailing whitespace


# 1.47 25-Apr-2013 skrll

Adjust exidx_start by relocbase


# 1.46 25-Apr-2013 skrll

Handle PT_ARM_EXIDX when mapping objects.


Revision tags: agc-symver-base yamt-pagecache-base8 yamt-pagecache-base7 yamt-pagecache-base6
# 1.45 13-Oct-2012 dholland

Improve the diagnostic messages for invalid/non-ELF files. Proposed a
while back; I forget the context. Has been sitting in one of my trees
awaiting a test run since, apparently, August 2011.


# 1.44 25-Jul-2012 martin

branches: 1.44.2;
Try to make dbg() printfs realy ELFSIZE independent


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 netbsd-6-1-RC2 netbsd-6-1-RC1 netbsd-6-0-1-RELEASE matt-nb6-plus-nbase netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.43 13-Aug-2011 christos

branches: 1.43.2;
consistent debugging info for program headers


Revision tags: cherry-xenmp-base
# 1.42 09-Mar-2011 joerg

Add TLS support infrastructure. For dynamic binaries, ld.elf_so exports
_rtld_tls_allocate and _rtld_tls_free. libpthread uses this functions to
setup the thread private area of all new threads. ld.elf_so is
responsible for setting up the private area for the initial thread.
Similar functions are called from _libc_init for static binaries, using
dl_iterate_phdr to access the ELF Program Header.

Add test cases to exercise the different TLS storage models. Test cases
are compiled and installed on all platforms, but are skipped on
platforms not marked for TLS support.

This material is based upon work partially supported by
The NetBSD Foundation under a contract with Joerg Sonnenberger.

It is inspired by the TLS support in FreeBSD by Doug Rabson and the
clean ups of the DragonFly port of the original FreeBSD modifications.


Revision tags: bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.41 16-Oct-2010 skrll

Implement dl_iterate_phdr.

Somewhat taken from FreeBSD. Manual page from OpenBSD.


# 1.40 11-Sep-2010 skrll

Wrap long line.


# 1.39 10-Jan-2010 skrll

Reset the COMBRELOC cache Obj_Entry if it was freed.

Fixes PR 41482. Done slightly differently to the patch in the PR.


Revision tags: matt-premerge-20091211
# 1.38 19-May-2009 christos

warns=4


Revision tags: jym-xensuspend-nbase jym-xensuspend-base
# 1.37 06-Jan-2009 mrg

add some (uintptr_t) to some casts, so these compile with ELFSIZE=32
*and _LP64. necessary for 32/64 bit combo ldd support.


Revision tags: netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 wrstuden-revivesa-base
# 1.36 03-Jun-2008 ad

branches: 1.36.4;
Don't bother unmapping the ELF header unless it overlaps the first load
section.


Revision tags: yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-baseX yamt-pf42-base2 yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase cube-autoconf-base matt-armv6-base hpcarm-cleanup-base
# 1.35 05-Oct-2007 ad

branches: 1.35.6; 1.35.8;
- Don't use the names malloc/free/etc, since we can end up pulling in libc's
malloc which is not what we want.
- Remove ancient memory compaction hack. Realloc on free memory is now an
error again (from joerg@).

Ok christos@.


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase wrstuden-fixsa-base-1 netbsd-4-0-RELEASE netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-mips64-base wrstuden-fixsa-base abandoned-netbsd-4-base netbsd-4-base
# 1.34 21-Mar-2006 christos

branches: 1.34.10;
Coverity CID 2749: Fix another double free. Instead of removing that last
free() call, change the allocation policy to leave the responsibility for
allocation/freeing the pathname to _rtld_map_object(), instead of having
the caller allocate it and _rtld_map_object() free it. This simplifies the
code a lot and it is more efficient.


# 1.33 18-Mar-2006 matt

Fix some double frees / missing frees / use after freed all
relating to _rtld_load_object. Fix Coverity CID 2601, 2600.


# 1.32 01-Jun-2005 lukem

appease gcc -Wuninitialized


Revision tags: netbsd-3-1-1-RELEASE netbsd-3-0-3-RELEASE netbsd-3-1-RELEASE netbsd-3-0-2-RELEASE netbsd-3-1-RC4 netbsd-3-1-RC3 netbsd-3-1-RC2 netbsd-3-1-RC1 netbsd-3-0-1-RELEASE netbsd-3-0-RELEASE netbsd-3-0-RC6 netbsd-3-0-RC5 netbsd-3-0-RC4 netbsd-3-0-RC3 netbsd-3-0-RC2 netbsd-3-0-RC1 netbsd-3-base
# 1.31 22-Oct-2004 skrll

Add __RCSIDs.

OK'd by mycroft.


Revision tags: netbsd-2-0-3-RELEASE netbsd-2-1-RELEASE netbsd-2-1-RC6 netbsd-2-1-RC5 netbsd-2-1-RC4 netbsd-2-1-RC3 netbsd-2-1-RC2 netbsd-2-1-RC1 netbsd-2-0-2-RELEASE netbsd-2-0-1-RELEASE netbsd-2-base netbsd-2-0-RELEASE netbsd-2-0-RC5 netbsd-2-0-RC4 netbsd-2-0-RC3 netbsd-2-0-RC2 netbsd-2-0-RC1 netbsd-2-0-base
# 1.30 24-Jul-2003 skrll

ANSIfy and de-__P().


# 1.29 05-Jun-2003 simonb

KNF- blank lines before code at start of functions.


# 1.28 06-Mar-2003 taca

make it compile in /usr/src/rescue directory.


# 1.27 06-Mar-2003 matt

Align psections to their specified alignment (if possible). Use
mmap(2) MAP_ALIGNED.


# 1.26 06-Feb-2003 fvdl

If fstat info was passed, and from it it can be seen that the file size
is smaller than an ELF header, don't even bother to try to look at it.
Avoids coredumps with zero-sized files.


Revision tags: fvdl_fs64_base
# 1.25 05-Dec-2002 junyoung

A bit speedup: in _rtld_load_object(), save the number of calls to
strcmp() by performing path name length comparison first. In the test
with Mozilla, the number was reduced to 1068 from 7182 (yes, we saved
6114 strcmp()!).


# 1.24 05-Oct-2002 mycroft

Minor cleanup.


# 1.23 05-Oct-2002 mycroft

Make this work for ldd again.


# 1.22 04-Oct-2002 mycroft

Rearrange _rtld_map_object() a little, so that we don't have to have the first
page of the object double-mapped. Not that it matters much, but someone was
whinging about it.
While I'm at it, nuke obj->phdr and obj->phsize; they're unused.


# 1.21 27-Sep-2002 mycroft

Change the munmap() of the text-data gap to mprotect(PROT_NONE). This avoids
having scattered little mappings, although it's probably not the best way...


# 1.20 24-Sep-2002 mycroft

Woops, unmap the first page in the `win' case, too.
Tidy up a bit.


# 1.19 24-Sep-2002 junyoung

Use _rtld_pagesz instead of PAGESIZE.


# 1.18 24-Sep-2002 junyoung

space -> tab.


# 1.17 24-Sep-2002 junyoung

Don't need to use union any longer.


# 1.16 23-Sep-2002 mycroft

Several small changes that shave 7-8% off the simple-exec-loop test:

* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().

* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.

* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.

* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)

* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().

Also:

* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.


# 1.15 13-Sep-2002 mycroft

Nuke two frivolous assert()s.


# 1.14 06-Sep-2002 mycroft

Introduce a new flag, `isdynamic', which is used to remember whether the
executable was of type ET_DYN. Use this instead of `mainprog' to determine
whether we need to do base-relative fixups of the PLT. (This allows loading
non-relocatable objects, should we desire to do that at some point...)


# 1.13 01-Jun-2002 lukem

SIMPLEQ rototill:
- implement SIMPLEQ_REMOVE(head, elm, type, field). whilst it's O(n),
this mirrors the functionality of SLIST_REMOVE() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE()
- remove the unnecessary elm arg from SIMPLEQ_REMOVE_HEAD().
this mirrors the functionality of SLIST_REMOVE_HEAD() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE_HEAD()
- remove notes about SIMPLEQ not supporting arbitrary element removal
- use SIMPLEQ_FOREACH() instead of home-grown for loops
- use SIMPLEQ_EMPTY() appropriately
- use SIMPLEQ_*() instead of accessing sqh_first,sqh_last,sqe_next directly
- reorder manual page; be consistent about how the types are listed
- other minor cleanups


Revision tags: netbsd-1-6-PATCH002-RELEASE netbsd-1-6-PATCH002 netbsd-1-6-PATCH002-RC4 netbsd-1-6-PATCH002-RC3 netbsd-1-6-PATCH002-RC2 netbsd-1-6-PATCH002-RC1 netbsd-1-6-PATCH001 netbsd-1-6-PATCH001-RELEASE netbsd-1-6-PATCH001-RC3 netbsd-1-6-PATCH001-RC2 netbsd-1-6-PATCH001-RC1 netbsd-1-6-RELEASE netbsd-1-6-RC3 netbsd-1-6-RC2 netbsd-1-6-RC1 netbsd-1-6-base
# 1.12 26-Nov-2000 mycroft

branches: 1.12.2;
Return an error when there are too many segments rather than abort()ing.
This way `ldd /usr/libexec/ld.elf_so' doesn't core dump.


Revision tags: netbsd-1-5-PATCH002 netbsd-1-5-PATCH001 netbsd-1-5-RELEASE netbsd-1-5-BETA2 netbsd-1-5-BETA netbsd-1-5-ALPHA2 netbsd-1-5-base minoura-xpg4dl-base
# 1.11 13-Feb-2000 chs

branches: 1.11.4;
switch back to using a file mapping for the initial mapping of a new object.
this allows the kernel to use PMAP_PREFER() to give us better alignment on
platforms that care.


Revision tags: wrstuden-devbsize-19991221 wrstuden-devbsize-base
# 1.10 07-Nov-1999 mycroft

Changes from msaitoh to fix local/global symbol confusion, and to fix weak
symbol support -- updated by me for elf.h changes.


Revision tags: comdex-fall-1999-base
# 1.9 04-Nov-1999 erh

Remove ELFNAME defines since they are defined in sys/exec_elf.h


# 1.8 25-Oct-1999 kleink

Update to match new SVR4-style definition names in <sys/exec_elf.h>.


# 1.7 22-Oct-1999 hannken

Undo last change. While these macros are defined in exec_elf.h, they are
enclosed by `#ifdef _KERNEL'.


# 1.6 21-Oct-1999 erh

Remove ELFNAME macros which are already defines in exec_elf.h


# 1.5 06-Aug-1999 thorpej

branches: 1.5.2;
When allocating the entire address space of an object, map an anonymous
region first (using the data/bss protection) covering it, then overlay
the text and data regions at the appropriate offsets within the region,
and then unmap any gap between the text and data.

The previous method of maping the entire address space with the actual
file object itself is incorrect, as it may extend past the end of the
file if the section alignment is large enough.

This bug was the source of the libposix failure on the SPARC and another
similar failure (with libc!) on the Alpha (failure was accompanied by
a "uvn_io: size check fired" message on the console).


Revision tags: netbsd-1-4-PATCH003 netbsd-1-4-PATCH002 netbsd-1-4-PATCH001 netbsd-1-4-RELEASE netbsd-1-4-base
# 1.4 01-Mar-1999 christos

KNF


# 1.3 20-Feb-1998 mycroft

Fill in missing (default) mmap(2) flags.


Revision tags: netbsd-1-3-PATCH003 netbsd-1-3-PATCH003-CANDIDATE2 netbsd-1-3-PATCH003-CANDIDATE1 netbsd-1-3-PATCH003-CANDIDATE0 netbsd-1-3-PATCH002 netbsd-1-3-PATCH001 netbsd-1-3-RELEASE netbsd-1-3-BETA netbsd-1-3-base
# 1.2 17-Dec-1996 jonathan

Mipsel changes to ELF ldd/ld.so:
* don't abort() on NetBSD/pmax 1.1 elf binaries;
* Assume 32-bit elf unless compiling for Alpha;
* note bugs in ldd's README.


# 1.1 16-Dec-1996 cgd

First cut at an ELF shared loader. Originally from John Polstra's FreeBSD elf
kit, then hacked on by Matt Thomas <matt@3am-software.com>, then by me (to
make it work with new versions of the toolchain, etc.). This runs, but it's
in serious need of cleaning and/or a fair bit of reworking. See the README
file for more information, and a list of things to do.


# 1.57 10-Mar-2017 maya

Revert to version 1.55 of map_object.c. This reverts:

"really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero."

As per toolchain/52054: src/libexec/ld.elf_so update breaks everything,
this commit is very broken for some people (but not others). chs mentioned
he has a fix, but best not to leave -current broken in the meantime.


# 1.56 09-Mar-2017 chs

really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero.


Revision tags: bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104 localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.55 16-Jun-2016 christos

Move relro after we've computed out relocbase and re-enable it.
(Matthias Weckbecker)


# 1.54 14-Jun-2016 christos

Add support for GNU RELRO headers from Matthias Weckbecker.


# 1.53 30-Oct-2014 martin

Avoid use after free, reported by the coverity scanner.


Revision tags: netbsd-7-0-2-RELEASE netbsd-7-0-1-RELEASE netbsd-7-0-RELEASE netbsd-7-0-RC3 netbsd-7-0-RC2 netbsd-7-0-RC1 netbsd-7-base yamt-pagecache-base9 tls-earlyentropy-base riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 tls-maxphys-base
# 1.52 03-Aug-2013 skrll

branches: 1.52.4;
Fix DEBUG build.


Revision tags: riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base
# 1.51 09-May-2013 christos

convert to SIMPLEQ like the rest of the queues.


# 1.50 08-May-2013 christos

need xfree() instead of free(). Using free() ends us up in an infinite loop.


# 1.49 06-May-2013 christos

Fixed handling of DT_SONAME:

- add function to add name aliases for shared libraries loaded
XXX[1]: we don't add a name during load time, only when DT_SONAME
is present.
- search already loaded objects in load_by_name for an already
loaded object that matches our name and return it.
- add missing initialization and cleanup for obj->names
XXX[2]: should we make them SIMPLEQ?
- Add XXX in rtld.c about getting the name of an object.

NB: This makes the jdk work again without resorting to a hack of putting
the build path of libjvm.so into the run path (which is a security
problem).
XXX: Pullup-6?


# 1.48 06-May-2013 skrll

Trailing whitespace


# 1.47 25-Apr-2013 skrll

Adjust exidx_start by relocbase


# 1.46 25-Apr-2013 skrll

Handle PT_ARM_EXIDX when mapping objects.


Revision tags: agc-symver-base yamt-pagecache-base8 yamt-pagecache-base7 yamt-pagecache-base6
# 1.45 13-Oct-2012 dholland

Improve the diagnostic messages for invalid/non-ELF files. Proposed a
while back; I forget the context. Has been sitting in one of my trees
awaiting a test run since, apparently, August 2011.


# 1.44 25-Jul-2012 martin

branches: 1.44.2;
Try to make dbg() printfs realy ELFSIZE independent


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 netbsd-6-1-RC2 netbsd-6-1-RC1 netbsd-6-0-1-RELEASE matt-nb6-plus-nbase netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.43 13-Aug-2011 christos

branches: 1.43.2;
consistent debugging info for program headers


Revision tags: cherry-xenmp-base
# 1.42 09-Mar-2011 joerg

Add TLS support infrastructure. For dynamic binaries, ld.elf_so exports
_rtld_tls_allocate and _rtld_tls_free. libpthread uses this functions to
setup the thread private area of all new threads. ld.elf_so is
responsible for setting up the private area for the initial thread.
Similar functions are called from _libc_init for static binaries, using
dl_iterate_phdr to access the ELF Program Header.

Add test cases to exercise the different TLS storage models. Test cases
are compiled and installed on all platforms, but are skipped on
platforms not marked for TLS support.

This material is based upon work partially supported by
The NetBSD Foundation under a contract with Joerg Sonnenberger.

It is inspired by the TLS support in FreeBSD by Doug Rabson and the
clean ups of the DragonFly port of the original FreeBSD modifications.


Revision tags: bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.41 16-Oct-2010 skrll

Implement dl_iterate_phdr.

Somewhat taken from FreeBSD. Manual page from OpenBSD.


# 1.40 11-Sep-2010 skrll

Wrap long line.


# 1.39 10-Jan-2010 skrll

Reset the COMBRELOC cache Obj_Entry if it was freed.

Fixes PR 41482. Done slightly differently to the patch in the PR.


Revision tags: matt-premerge-20091211
# 1.38 19-May-2009 christos

warns=4


Revision tags: jym-xensuspend-nbase jym-xensuspend-base
# 1.37 06-Jan-2009 mrg

add some (uintptr_t) to some casts, so these compile with ELFSIZE=32
*and _LP64. necessary for 32/64 bit combo ldd support.


Revision tags: netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 wrstuden-revivesa-base
# 1.36 03-Jun-2008 ad

branches: 1.36.4;
Don't bother unmapping the ELF header unless it overlaps the first load
section.


Revision tags: yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-baseX yamt-pf42-base2 yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase cube-autoconf-base matt-armv6-base hpcarm-cleanup-base
# 1.35 05-Oct-2007 ad

branches: 1.35.6; 1.35.8;
- Don't use the names malloc/free/etc, since we can end up pulling in libc's
malloc which is not what we want.
- Remove ancient memory compaction hack. Realloc on free memory is now an
error again (from joerg@).

Ok christos@.


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase wrstuden-fixsa-base-1 netbsd-4-0-RELEASE netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-mips64-base wrstuden-fixsa-base abandoned-netbsd-4-base netbsd-4-base
# 1.34 21-Mar-2006 christos

branches: 1.34.10;
Coverity CID 2749: Fix another double free. Instead of removing that last
free() call, change the allocation policy to leave the responsibility for
allocation/freeing the pathname to _rtld_map_object(), instead of having
the caller allocate it and _rtld_map_object() free it. This simplifies the
code a lot and it is more efficient.


# 1.33 18-Mar-2006 matt

Fix some double frees / missing frees / use after freed all
relating to _rtld_load_object. Fix Coverity CID 2601, 2600.


# 1.32 01-Jun-2005 lukem

appease gcc -Wuninitialized


Revision tags: netbsd-3-1-1-RELEASE netbsd-3-0-3-RELEASE netbsd-3-1-RELEASE netbsd-3-0-2-RELEASE netbsd-3-1-RC4 netbsd-3-1-RC3 netbsd-3-1-RC2 netbsd-3-1-RC1 netbsd-3-0-1-RELEASE netbsd-3-0-RELEASE netbsd-3-0-RC6 netbsd-3-0-RC5 netbsd-3-0-RC4 netbsd-3-0-RC3 netbsd-3-0-RC2 netbsd-3-0-RC1 netbsd-3-base
# 1.31 22-Oct-2004 skrll

Add __RCSIDs.

OK'd by mycroft.


Revision tags: netbsd-2-0-3-RELEASE netbsd-2-1-RELEASE netbsd-2-1-RC6 netbsd-2-1-RC5 netbsd-2-1-RC4 netbsd-2-1-RC3 netbsd-2-1-RC2 netbsd-2-1-RC1 netbsd-2-0-2-RELEASE netbsd-2-0-1-RELEASE netbsd-2-base netbsd-2-0-RELEASE netbsd-2-0-RC5 netbsd-2-0-RC4 netbsd-2-0-RC3 netbsd-2-0-RC2 netbsd-2-0-RC1 netbsd-2-0-base
# 1.30 24-Jul-2003 skrll

ANSIfy and de-__P().


# 1.29 05-Jun-2003 simonb

KNF- blank lines before code at start of functions.


# 1.28 06-Mar-2003 taca

make it compile in /usr/src/rescue directory.


# 1.27 06-Mar-2003 matt

Align psections to their specified alignment (if possible). Use
mmap(2) MAP_ALIGNED.


# 1.26 06-Feb-2003 fvdl

If fstat info was passed, and from it it can be seen that the file size
is smaller than an ELF header, don't even bother to try to look at it.
Avoids coredumps with zero-sized files.


Revision tags: fvdl_fs64_base
# 1.25 05-Dec-2002 junyoung

A bit speedup: in _rtld_load_object(), save the number of calls to
strcmp() by performing path name length comparison first. In the test
with Mozilla, the number was reduced to 1068 from 7182 (yes, we saved
6114 strcmp()!).


# 1.24 05-Oct-2002 mycroft

Minor cleanup.


# 1.23 05-Oct-2002 mycroft

Make this work for ldd again.


# 1.22 04-Oct-2002 mycroft

Rearrange _rtld_map_object() a little, so that we don't have to have the first
page of the object double-mapped. Not that it matters much, but someone was
whinging about it.
While I'm at it, nuke obj->phdr and obj->phsize; they're unused.


# 1.21 27-Sep-2002 mycroft

Change the munmap() of the text-data gap to mprotect(PROT_NONE). This avoids
having scattered little mappings, although it's probably not the best way...


# 1.20 24-Sep-2002 mycroft

Woops, unmap the first page in the `win' case, too.
Tidy up a bit.


# 1.19 24-Sep-2002 junyoung

Use _rtld_pagesz instead of PAGESIZE.


# 1.18 24-Sep-2002 junyoung

space -> tab.


# 1.17 24-Sep-2002 junyoung

Don't need to use union any longer.


# 1.16 23-Sep-2002 mycroft

Several small changes that shave 7-8% off the simple-exec-loop test:

* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().

* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.

* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.

* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)

* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().

Also:

* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.


# 1.15 13-Sep-2002 mycroft

Nuke two frivolous assert()s.


# 1.14 06-Sep-2002 mycroft

Introduce a new flag, `isdynamic', which is used to remember whether the
executable was of type ET_DYN. Use this instead of `mainprog' to determine
whether we need to do base-relative fixups of the PLT. (This allows loading
non-relocatable objects, should we desire to do that at some point...)


# 1.13 01-Jun-2002 lukem

SIMPLEQ rototill:
- implement SIMPLEQ_REMOVE(head, elm, type, field). whilst it's O(n),
this mirrors the functionality of SLIST_REMOVE() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE()
- remove the unnecessary elm arg from SIMPLEQ_REMOVE_HEAD().
this mirrors the functionality of SLIST_REMOVE_HEAD() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE_HEAD()
- remove notes about SIMPLEQ not supporting arbitrary element removal
- use SIMPLEQ_FOREACH() instead of home-grown for loops
- use SIMPLEQ_EMPTY() appropriately
- use SIMPLEQ_*() instead of accessing sqh_first,sqh_last,sqe_next directly
- reorder manual page; be consistent about how the types are listed
- other minor cleanups


Revision tags: netbsd-1-6-PATCH002-RELEASE netbsd-1-6-PATCH002 netbsd-1-6-PATCH002-RC4 netbsd-1-6-PATCH002-RC3 netbsd-1-6-PATCH002-RC2 netbsd-1-6-PATCH002-RC1 netbsd-1-6-PATCH001 netbsd-1-6-PATCH001-RELEASE netbsd-1-6-PATCH001-RC3 netbsd-1-6-PATCH001-RC2 netbsd-1-6-PATCH001-RC1 netbsd-1-6-RELEASE netbsd-1-6-RC3 netbsd-1-6-RC2 netbsd-1-6-RC1 netbsd-1-6-base
# 1.12 26-Nov-2000 mycroft

branches: 1.12.2;
Return an error when there are too many segments rather than abort()ing.
This way `ldd /usr/libexec/ld.elf_so' doesn't core dump.


Revision tags: netbsd-1-5-PATCH002 netbsd-1-5-PATCH001 netbsd-1-5-RELEASE netbsd-1-5-BETA2 netbsd-1-5-BETA netbsd-1-5-ALPHA2 netbsd-1-5-base minoura-xpg4dl-base
# 1.11 13-Feb-2000 chs

branches: 1.11.4;
switch back to using a file mapping for the initial mapping of a new object.
this allows the kernel to use PMAP_PREFER() to give us better alignment on
platforms that care.


Revision tags: wrstuden-devbsize-19991221 wrstuden-devbsize-base
# 1.10 07-Nov-1999 mycroft

Changes from msaitoh to fix local/global symbol confusion, and to fix weak
symbol support -- updated by me for elf.h changes.


Revision tags: comdex-fall-1999-base
# 1.9 04-Nov-1999 erh

Remove ELFNAME defines since they are defined in sys/exec_elf.h


# 1.8 25-Oct-1999 kleink

Update to match new SVR4-style definition names in <sys/exec_elf.h>.


# 1.7 22-Oct-1999 hannken

Undo last change. While these macros are defined in exec_elf.h, they are
enclosed by `#ifdef _KERNEL'.


# 1.6 21-Oct-1999 erh

Remove ELFNAME macros which are already defines in exec_elf.h


# 1.5 06-Aug-1999 thorpej

branches: 1.5.2;
When allocating the entire address space of an object, map an anonymous
region first (using the data/bss protection) covering it, then overlay
the text and data regions at the appropriate offsets within the region,
and then unmap any gap between the text and data.

The previous method of maping the entire address space with the actual
file object itself is incorrect, as it may extend past the end of the
file if the section alignment is large enough.

This bug was the source of the libposix failure on the SPARC and another
similar failure (with libc!) on the Alpha (failure was accompanied by
a "uvn_io: size check fired" message on the console).


Revision tags: netbsd-1-4-PATCH003 netbsd-1-4-PATCH002 netbsd-1-4-PATCH001 netbsd-1-4-RELEASE netbsd-1-4-base
# 1.4 01-Mar-1999 christos

KNF


# 1.3 20-Feb-1998 mycroft

Fill in missing (default) mmap(2) flags.


Revision tags: netbsd-1-3-PATCH003 netbsd-1-3-PATCH003-CANDIDATE2 netbsd-1-3-PATCH003-CANDIDATE1 netbsd-1-3-PATCH003-CANDIDATE0 netbsd-1-3-PATCH002 netbsd-1-3-PATCH001 netbsd-1-3-RELEASE netbsd-1-3-BETA netbsd-1-3-base
# 1.2 17-Dec-1996 jonathan

Mipsel changes to ELF ldd/ld.so:
* don't abort() on NetBSD/pmax 1.1 elf binaries;
* Assume 32-bit elf unless compiling for Alpha;
* note bugs in ldd's README.


# 1.1 16-Dec-1996 cgd

First cut at an ELF shared loader. Originally from John Polstra's FreeBSD elf
kit, then hacked on by Matt Thomas <matt@3am-software.com>, then by me (to
make it work with new versions of the toolchain, etc.). This runs, but it's
in serious need of cleaning and/or a fair bit of reworking. See the README
file for more information, and a list of things to do.


# 1.56 09-Mar-2017 chs

really unmap the gap between the text and data rather than just removing
all access with mprotect(). the latter results in the kernel having to
keep track of that range separately since the permissions are different.
avoid calling mmap() with a size of zero.


Revision tags: bouyer-socketcan-base pgoyette-localcount-20170107 pgoyette-localcount-20161104 localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.55 16-Jun-2016 christos

Move relro after we've computed out relocbase and re-enable it.
(Matthias Weckbecker)


# 1.54 14-Jun-2016 christos

Add support for GNU RELRO headers from Matthias Weckbecker.


# 1.53 30-Oct-2014 martin

Avoid use after free, reported by the coverity scanner.


Revision tags: netbsd-7-0-2-RELEASE netbsd-7-0-1-RELEASE netbsd-7-0-RELEASE netbsd-7-0-RC3 netbsd-7-0-RC2 netbsd-7-0-RC1 netbsd-7-base yamt-pagecache-base9 tls-earlyentropy-base riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 tls-maxphys-base
# 1.52 03-Aug-2013 skrll

branches: 1.52.4;
Fix DEBUG build.


Revision tags: riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base
# 1.51 09-May-2013 christos

convert to SIMPLEQ like the rest of the queues.


# 1.50 08-May-2013 christos

need xfree() instead of free(). Using free() ends us up in an infinite loop.


# 1.49 06-May-2013 christos

Fixed handling of DT_SONAME:

- add function to add name aliases for shared libraries loaded
XXX[1]: we don't add a name during load time, only when DT_SONAME
is present.
- search already loaded objects in load_by_name for an already
loaded object that matches our name and return it.
- add missing initialization and cleanup for obj->names
XXX[2]: should we make them SIMPLEQ?
- Add XXX in rtld.c about getting the name of an object.

NB: This makes the jdk work again without resorting to a hack of putting
the build path of libjvm.so into the run path (which is a security
problem).
XXX: Pullup-6?


# 1.48 06-May-2013 skrll

Trailing whitespace


# 1.47 25-Apr-2013 skrll

Adjust exidx_start by relocbase


# 1.46 25-Apr-2013 skrll

Handle PT_ARM_EXIDX when mapping objects.


Revision tags: agc-symver-base yamt-pagecache-base8 yamt-pagecache-base7 yamt-pagecache-base6
# 1.45 13-Oct-2012 dholland

Improve the diagnostic messages for invalid/non-ELF files. Proposed a
while back; I forget the context. Has been sitting in one of my trees
awaiting a test run since, apparently, August 2011.


# 1.44 25-Jul-2012 martin

branches: 1.44.2;
Try to make dbg() printfs realy ELFSIZE independent


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 netbsd-6-1-RC2 netbsd-6-1-RC1 netbsd-6-0-1-RELEASE matt-nb6-plus-nbase netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.43 13-Aug-2011 christos

branches: 1.43.2;
consistent debugging info for program headers


Revision tags: cherry-xenmp-base
# 1.42 09-Mar-2011 joerg

Add TLS support infrastructure. For dynamic binaries, ld.elf_so exports
_rtld_tls_allocate and _rtld_tls_free. libpthread uses this functions to
setup the thread private area of all new threads. ld.elf_so is
responsible for setting up the private area for the initial thread.
Similar functions are called from _libc_init for static binaries, using
dl_iterate_phdr to access the ELF Program Header.

Add test cases to exercise the different TLS storage models. Test cases
are compiled and installed on all platforms, but are skipped on
platforms not marked for TLS support.

This material is based upon work partially supported by
The NetBSD Foundation under a contract with Joerg Sonnenberger.

It is inspired by the TLS support in FreeBSD by Doug Rabson and the
clean ups of the DragonFly port of the original FreeBSD modifications.


Revision tags: bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.41 16-Oct-2010 skrll

Implement dl_iterate_phdr.

Somewhat taken from FreeBSD. Manual page from OpenBSD.


# 1.40 11-Sep-2010 skrll

Wrap long line.


# 1.39 10-Jan-2010 skrll

Reset the COMBRELOC cache Obj_Entry if it was freed.

Fixes PR 41482. Done slightly differently to the patch in the PR.


Revision tags: matt-premerge-20091211
# 1.38 19-May-2009 christos

warns=4


Revision tags: jym-xensuspend-nbase jym-xensuspend-base
# 1.37 06-Jan-2009 mrg

add some (uintptr_t) to some casts, so these compile with ELFSIZE=32
*and _LP64. necessary for 32/64 bit combo ldd support.


Revision tags: netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 wrstuden-revivesa-base
# 1.36 03-Jun-2008 ad

branches: 1.36.4;
Don't bother unmapping the ELF header unless it overlaps the first load
section.


Revision tags: yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-baseX yamt-pf42-base2 yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase cube-autoconf-base matt-armv6-base hpcarm-cleanup-base
# 1.35 05-Oct-2007 ad

branches: 1.35.6; 1.35.8;
- Don't use the names malloc/free/etc, since we can end up pulling in libc's
malloc which is not what we want.
- Remove ancient memory compaction hack. Realloc on free memory is now an
error again (from joerg@).

Ok christos@.


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase wrstuden-fixsa-base-1 netbsd-4-0-RELEASE netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-mips64-base wrstuden-fixsa-base abandoned-netbsd-4-base netbsd-4-base
# 1.34 21-Mar-2006 christos

branches: 1.34.10;
Coverity CID 2749: Fix another double free. Instead of removing that last
free() call, change the allocation policy to leave the responsibility for
allocation/freeing the pathname to _rtld_map_object(), instead of having
the caller allocate it and _rtld_map_object() free it. This simplifies the
code a lot and it is more efficient.


# 1.33 18-Mar-2006 matt

Fix some double frees / missing frees / use after freed all
relating to _rtld_load_object. Fix Coverity CID 2601, 2600.


# 1.32 01-Jun-2005 lukem

appease gcc -Wuninitialized


Revision tags: netbsd-3-1-1-RELEASE netbsd-3-0-3-RELEASE netbsd-3-1-RELEASE netbsd-3-0-2-RELEASE netbsd-3-1-RC4 netbsd-3-1-RC3 netbsd-3-1-RC2 netbsd-3-1-RC1 netbsd-3-0-1-RELEASE netbsd-3-0-RELEASE netbsd-3-0-RC6 netbsd-3-0-RC5 netbsd-3-0-RC4 netbsd-3-0-RC3 netbsd-3-0-RC2 netbsd-3-0-RC1 netbsd-3-base
# 1.31 22-Oct-2004 skrll

Add __RCSIDs.

OK'd by mycroft.


Revision tags: netbsd-2-0-3-RELEASE netbsd-2-1-RELEASE netbsd-2-1-RC6 netbsd-2-1-RC5 netbsd-2-1-RC4 netbsd-2-1-RC3 netbsd-2-1-RC2 netbsd-2-1-RC1 netbsd-2-0-2-RELEASE netbsd-2-0-1-RELEASE netbsd-2-base netbsd-2-0-RELEASE netbsd-2-0-RC5 netbsd-2-0-RC4 netbsd-2-0-RC3 netbsd-2-0-RC2 netbsd-2-0-RC1 netbsd-2-0-base
# 1.30 24-Jul-2003 skrll

ANSIfy and de-__P().


# 1.29 05-Jun-2003 simonb

KNF- blank lines before code at start of functions.


# 1.28 06-Mar-2003 taca

make it compile in /usr/src/rescue directory.


# 1.27 06-Mar-2003 matt

Align psections to their specified alignment (if possible). Use
mmap(2) MAP_ALIGNED.


# 1.26 06-Feb-2003 fvdl

If fstat info was passed, and from it it can be seen that the file size
is smaller than an ELF header, don't even bother to try to look at it.
Avoids coredumps with zero-sized files.


Revision tags: fvdl_fs64_base
# 1.25 05-Dec-2002 junyoung

A bit speedup: in _rtld_load_object(), save the number of calls to
strcmp() by performing path name length comparison first. In the test
with Mozilla, the number was reduced to 1068 from 7182 (yes, we saved
6114 strcmp()!).


# 1.24 05-Oct-2002 mycroft

Minor cleanup.


# 1.23 05-Oct-2002 mycroft

Make this work for ldd again.


# 1.22 04-Oct-2002 mycroft

Rearrange _rtld_map_object() a little, so that we don't have to have the first
page of the object double-mapped. Not that it matters much, but someone was
whinging about it.
While I'm at it, nuke obj->phdr and obj->phsize; they're unused.


# 1.21 27-Sep-2002 mycroft

Change the munmap() of the text-data gap to mprotect(PROT_NONE). This avoids
having scattered little mappings, although it's probably not the best way...


# 1.20 24-Sep-2002 mycroft

Woops, unmap the first page in the `win' case, too.
Tidy up a bit.


# 1.19 24-Sep-2002 junyoung

Use _rtld_pagesz instead of PAGESIZE.


# 1.18 24-Sep-2002 junyoung

space -> tab.


# 1.17 24-Sep-2002 junyoung

Don't need to use union any longer.


# 1.16 23-Sep-2002 mycroft

Several small changes that shave 7-8% off the simple-exec-loop test:

* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().

* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.

* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.

* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)

* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().

Also:

* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.


# 1.15 13-Sep-2002 mycroft

Nuke two frivolous assert()s.


# 1.14 06-Sep-2002 mycroft

Introduce a new flag, `isdynamic', which is used to remember whether the
executable was of type ET_DYN. Use this instead of `mainprog' to determine
whether we need to do base-relative fixups of the PLT. (This allows loading
non-relocatable objects, should we desire to do that at some point...)


# 1.13 01-Jun-2002 lukem

SIMPLEQ rototill:
- implement SIMPLEQ_REMOVE(head, elm, type, field). whilst it's O(n),
this mirrors the functionality of SLIST_REMOVE() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE()
- remove the unnecessary elm arg from SIMPLEQ_REMOVE_HEAD().
this mirrors the functionality of SLIST_REMOVE_HEAD() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE_HEAD()
- remove notes about SIMPLEQ not supporting arbitrary element removal
- use SIMPLEQ_FOREACH() instead of home-grown for loops
- use SIMPLEQ_EMPTY() appropriately
- use SIMPLEQ_*() instead of accessing sqh_first,sqh_last,sqe_next directly
- reorder manual page; be consistent about how the types are listed
- other minor cleanups


Revision tags: netbsd-1-6-PATCH002-RELEASE netbsd-1-6-PATCH002 netbsd-1-6-PATCH002-RC4 netbsd-1-6-PATCH002-RC3 netbsd-1-6-PATCH002-RC2 netbsd-1-6-PATCH002-RC1 netbsd-1-6-PATCH001 netbsd-1-6-PATCH001-RELEASE netbsd-1-6-PATCH001-RC3 netbsd-1-6-PATCH001-RC2 netbsd-1-6-PATCH001-RC1 netbsd-1-6-RELEASE netbsd-1-6-RC3 netbsd-1-6-RC2 netbsd-1-6-RC1 netbsd-1-6-base
# 1.12 26-Nov-2000 mycroft

branches: 1.12.2;
Return an error when there are too many segments rather than abort()ing.
This way `ldd /usr/libexec/ld.elf_so' doesn't core dump.


Revision tags: netbsd-1-5-PATCH002 netbsd-1-5-PATCH001 netbsd-1-5-RELEASE netbsd-1-5-BETA2 netbsd-1-5-BETA netbsd-1-5-ALPHA2 netbsd-1-5-base minoura-xpg4dl-base
# 1.11 13-Feb-2000 chs

branches: 1.11.4;
switch back to using a file mapping for the initial mapping of a new object.
this allows the kernel to use PMAP_PREFER() to give us better alignment on
platforms that care.


Revision tags: wrstuden-devbsize-19991221 wrstuden-devbsize-base
# 1.10 07-Nov-1999 mycroft

Changes from msaitoh to fix local/global symbol confusion, and to fix weak
symbol support -- updated by me for elf.h changes.


Revision tags: comdex-fall-1999-base
# 1.9 04-Nov-1999 erh

Remove ELFNAME defines since they are defined in sys/exec_elf.h


# 1.8 25-Oct-1999 kleink

Update to match new SVR4-style definition names in <sys/exec_elf.h>.


# 1.7 22-Oct-1999 hannken

Undo last change. While these macros are defined in exec_elf.h, they are
enclosed by `#ifdef _KERNEL'.


# 1.6 21-Oct-1999 erh

Remove ELFNAME macros which are already defines in exec_elf.h


# 1.5 06-Aug-1999 thorpej

branches: 1.5.2;
When allocating the entire address space of an object, map an anonymous
region first (using the data/bss protection) covering it, then overlay
the text and data regions at the appropriate offsets within the region,
and then unmap any gap between the text and data.

The previous method of maping the entire address space with the actual
file object itself is incorrect, as it may extend past the end of the
file if the section alignment is large enough.

This bug was the source of the libposix failure on the SPARC and another
similar failure (with libc!) on the Alpha (failure was accompanied by
a "uvn_io: size check fired" message on the console).


Revision tags: netbsd-1-4-PATCH003 netbsd-1-4-PATCH002 netbsd-1-4-PATCH001 netbsd-1-4-RELEASE netbsd-1-4-base
# 1.4 01-Mar-1999 christos

KNF


# 1.3 20-Feb-1998 mycroft

Fill in missing (default) mmap(2) flags.


Revision tags: netbsd-1-3-PATCH003 netbsd-1-3-PATCH003-CANDIDATE2 netbsd-1-3-PATCH003-CANDIDATE1 netbsd-1-3-PATCH003-CANDIDATE0 netbsd-1-3-PATCH002 netbsd-1-3-PATCH001 netbsd-1-3-RELEASE netbsd-1-3-BETA netbsd-1-3-base
# 1.2 17-Dec-1996 jonathan

Mipsel changes to ELF ldd/ld.so:
* don't abort() on NetBSD/pmax 1.1 elf binaries;
* Assume 32-bit elf unless compiling for Alpha;
* note bugs in ldd's README.


# 1.1 16-Dec-1996 cgd

First cut at an ELF shared loader. Originally from John Polstra's FreeBSD elf
kit, then hacked on by Matt Thomas <matt@3am-software.com>, then by me (to
make it work with new versions of the toolchain, etc.). This runs, but it's
in serious need of cleaning and/or a fair bit of reworking. See the README
file for more information, and a list of things to do.


Revision tags: pgoyette-localcount-20161104 localcount-20160914 pgoyette-localcount-20160806 pgoyette-localcount-20160726 pgoyette-localcount-base
# 1.55 16-Jun-2016 christos

Move relro after we've computed out relocbase and re-enable it.
(Matthias Weckbecker)


# 1.54 14-Jun-2016 christos

Add support for GNU RELRO headers from Matthias Weckbecker.


# 1.53 30-Oct-2014 martin

Avoid use after free, reported by the coverity scanner.


Revision tags: netbsd-7-0-2-RELEASE netbsd-7-0-1-RELEASE netbsd-7-0-RELEASE netbsd-7-0-RC3 netbsd-7-0-RC2 netbsd-7-0-RC1 netbsd-7-base yamt-pagecache-base9 tls-earlyentropy-base riastradh-xf86-video-intel-2-7-1-pre-2-21-15 riastradh-drm2-base3 tls-maxphys-base
# 1.52 03-Aug-2013 skrll

branches: 1.52.4;
Fix DEBUG build.


Revision tags: riastradh-drm2-base2 riastradh-drm2-base1 riastradh-drm2-base
# 1.51 09-May-2013 christos

convert to SIMPLEQ like the rest of the queues.


# 1.50 08-May-2013 christos

need xfree() instead of free(). Using free() ends us up in an infinite loop.


# 1.49 06-May-2013 christos

Fixed handling of DT_SONAME:

- add function to add name aliases for shared libraries loaded
XXX[1]: we don't add a name during load time, only when DT_SONAME
is present.
- search already loaded objects in load_by_name for an already
loaded object that matches our name and return it.
- add missing initialization and cleanup for obj->names
XXX[2]: should we make them SIMPLEQ?
- Add XXX in rtld.c about getting the name of an object.

NB: This makes the jdk work again without resorting to a hack of putting
the build path of libjvm.so into the run path (which is a security
problem).
XXX: Pullup-6?


# 1.48 06-May-2013 skrll

Trailing whitespace


# 1.47 25-Apr-2013 skrll

Adjust exidx_start by relocbase


# 1.46 25-Apr-2013 skrll

Handle PT_ARM_EXIDX when mapping objects.


Revision tags: agc-symver-base yamt-pagecache-base8 yamt-pagecache-base7 yamt-pagecache-base6
# 1.45 13-Oct-2012 dholland

Improve the diagnostic messages for invalid/non-ELF files. Proposed a
while back; I forget the context. Has been sitting in one of my trees
awaiting a test run since, apparently, August 2011.


# 1.44 25-Jul-2012 martin

branches: 1.44.2;
Try to make dbg() printfs realy ELFSIZE independent


Revision tags: netbsd-6-0-6-RELEASE netbsd-6-1-5-RELEASE netbsd-6-1-4-RELEASE netbsd-6-0-5-RELEASE netbsd-6-1-3-RELEASE netbsd-6-0-4-RELEASE netbsd-6-1-2-RELEASE netbsd-6-0-3-RELEASE netbsd-6-1-1-RELEASE netbsd-6-0-2-RELEASE netbsd-6-1-RELEASE netbsd-6-1-RC4 netbsd-6-1-RC3 netbsd-6-1-RC2 netbsd-6-1-RC1 netbsd-6-0-1-RELEASE matt-nb6-plus-nbase netbsd-6-0-RELEASE netbsd-6-0-RC2 matt-nb6-plus-base netbsd-6-0-RC1 yamt-pagecache-base5 yamt-pagecache-base4 netbsd-6-base yamt-pagecache-base3 yamt-pagecache-base2 yamt-pagecache-base
# 1.43 13-Aug-2011 christos

branches: 1.43.2;
consistent debugging info for program headers


Revision tags: cherry-xenmp-base
# 1.42 09-Mar-2011 joerg

Add TLS support infrastructure. For dynamic binaries, ld.elf_so exports
_rtld_tls_allocate and _rtld_tls_free. libpthread uses this functions to
setup the thread private area of all new threads. ld.elf_so is
responsible for setting up the private area for the initial thread.
Similar functions are called from _libc_init for static binaries, using
dl_iterate_phdr to access the ELF Program Header.

Add test cases to exercise the different TLS storage models. Test cases
are compiled and installed on all platforms, but are skipped on
platforms not marked for TLS support.

This material is based upon work partially supported by
The NetBSD Foundation under a contract with Joerg Sonnenberger.

It is inspired by the TLS support in FreeBSD by Doug Rabson and the
clean ups of the DragonFly port of the original FreeBSD modifications.


Revision tags: bouyer-quota2-nbase bouyer-quota2-base matt-mips64-premerge-20101231
# 1.41 16-Oct-2010 skrll

Implement dl_iterate_phdr.

Somewhat taken from FreeBSD. Manual page from OpenBSD.


# 1.40 11-Sep-2010 skrll

Wrap long line.


# 1.39 10-Jan-2010 skrll

Reset the COMBRELOC cache Obj_Entry if it was freed.

Fixes PR 41482. Done slightly differently to the patch in the PR.


Revision tags: matt-premerge-20091211
# 1.38 19-May-2009 christos

warns=4


Revision tags: jym-xensuspend-nbase jym-xensuspend-base
# 1.37 06-Jan-2009 mrg

add some (uintptr_t) to some casts, so these compile with ELFSIZE=32
*and _LP64. necessary for 32/64 bit combo ldd support.


Revision tags: netbsd-5-base matt-mips64-base2 wrstuden-revivesa-base-3 wrstuden-revivesa-base-2 wrstuden-revivesa-base-1 yamt-pf42-base4 wrstuden-revivesa-base
# 1.36 03-Jun-2008 ad

branches: 1.36.4;
Don't bother unmapping the ELF header unless it overlaps the first load
section.


Revision tags: yamt-pf42-base3 hpcarm-cleanup-nbase yamt-pf42-baseX yamt-pf42-base2 yamt-pf42-base keiichi-mipv6-base matt-armv6-nbase cube-autoconf-base matt-armv6-base hpcarm-cleanup-base
# 1.35 05-Oct-2007 ad

branches: 1.35.6; 1.35.8;
- Don't use the names malloc/free/etc, since we can end up pulling in libc's
malloc which is not what we want.
- Remove ancient memory compaction hack. Realloc on free memory is now an
error again (from joerg@).

Ok christos@.


Revision tags: netbsd-4-0-1-RELEASE wrstuden-fixsa-newbase wrstuden-fixsa-base-1 netbsd-4-0-RELEASE netbsd-4-0-RC5 netbsd-4-0-RC4 netbsd-4-0-RC3 netbsd-4-0-RC2 netbsd-4-0-RC1 matt-mips64-base wrstuden-fixsa-base abandoned-netbsd-4-base netbsd-4-base
# 1.34 21-Mar-2006 christos

branches: 1.34.10;
Coverity CID 2749: Fix another double free. Instead of removing that last
free() call, change the allocation policy to leave the responsibility for
allocation/freeing the pathname to _rtld_map_object(), instead of having
the caller allocate it and _rtld_map_object() free it. This simplifies the
code a lot and it is more efficient.


# 1.33 18-Mar-2006 matt

Fix some double frees / missing frees / use after freed all
relating to _rtld_load_object. Fix Coverity CID 2601, 2600.


# 1.32 01-Jun-2005 lukem

appease gcc -Wuninitialized


Revision tags: netbsd-3-1-1-RELEASE netbsd-3-0-3-RELEASE netbsd-3-1-RELEASE netbsd-3-0-2-RELEASE netbsd-3-1-RC4 netbsd-3-1-RC3 netbsd-3-1-RC2 netbsd-3-1-RC1 netbsd-3-0-1-RELEASE netbsd-3-0-RELEASE netbsd-3-0-RC6 netbsd-3-0-RC5 netbsd-3-0-RC4 netbsd-3-0-RC3 netbsd-3-0-RC2 netbsd-3-0-RC1 netbsd-3-base
# 1.31 22-Oct-2004 skrll

Add __RCSIDs.

OK'd by mycroft.


Revision tags: netbsd-2-0-3-RELEASE netbsd-2-1-RELEASE netbsd-2-1-RC6 netbsd-2-1-RC5 netbsd-2-1-RC4 netbsd-2-1-RC3 netbsd-2-1-RC2 netbsd-2-1-RC1 netbsd-2-0-2-RELEASE netbsd-2-0-1-RELEASE netbsd-2-base netbsd-2-0-RELEASE netbsd-2-0-RC5 netbsd-2-0-RC4 netbsd-2-0-RC3 netbsd-2-0-RC2 netbsd-2-0-RC1 netbsd-2-0-base
# 1.30 24-Jul-2003 skrll

ANSIfy and de-__P().


# 1.29 05-Jun-2003 simonb

KNF- blank lines before code at start of functions.


# 1.28 06-Mar-2003 taca

make it compile in /usr/src/rescue directory.


# 1.27 06-Mar-2003 matt

Align psections to their specified alignment (if possible). Use
mmap(2) MAP_ALIGNED.


# 1.26 06-Feb-2003 fvdl

If fstat info was passed, and from it it can be seen that the file size
is smaller than an ELF header, don't even bother to try to look at it.
Avoids coredumps with zero-sized files.


Revision tags: fvdl_fs64_base
# 1.25 05-Dec-2002 junyoung

A bit speedup: in _rtld_load_object(), save the number of calls to
strcmp() by performing path name length comparison first. In the test
with Mozilla, the number was reduced to 1068 from 7182 (yes, we saved
6114 strcmp()!).


# 1.24 05-Oct-2002 mycroft

Minor cleanup.


# 1.23 05-Oct-2002 mycroft

Make this work for ldd again.


# 1.22 04-Oct-2002 mycroft

Rearrange _rtld_map_object() a little, so that we don't have to have the first
page of the object double-mapped. Not that it matters much, but someone was
whinging about it.
While I'm at it, nuke obj->phdr and obj->phsize; they're unused.


# 1.21 27-Sep-2002 mycroft

Change the munmap() of the text-data gap to mprotect(PROT_NONE). This avoids
having scattered little mappings, although it's probably not the best way...


# 1.20 24-Sep-2002 mycroft

Woops, unmap the first page in the `win' case, too.
Tidy up a bit.


# 1.19 24-Sep-2002 junyoung

Use _rtld_pagesz instead of PAGESIZE.


# 1.18 24-Sep-2002 junyoung

space -> tab.


# 1.17 24-Sep-2002 junyoung

Don't need to use union any longer.


# 1.16 23-Sep-2002 mycroft

Several small changes that shave 7-8% off the simple-exec-loop test:

* Rename _rtld_find_library() to _rtld_load_library(). It now calls
_rtld_load_object() if necessary to actually load the object, rather
than having the caller do it. To do this, it also takes the `mode'
argument that gets passed to _rtld_load_object().

* On a related note, remove _rtld_check_library(), and instead call
_rtld_load_object() to instead try actually loading the object. We
save two extra namei's and a bunch of redundant work (almost
literally the same code) this way.

* In _rtld_map_object(), mmap(2) the first page read-only, rather than
read(2)ing it.

* In _rtld_symlook_obj(), compare the *second* character of the symbol
name before calling strcmp(). (This first character is too
frequently `_', and turns out to not be helpful, in libc.)

* Also in _rtld_symlook_obj(), remove the bogus STT_FUNC special case
-- this also allows removing the `in_plt' argument to
_rtld_symlook_list() and _rtld_symlook_obj().

Also:

* In _rtld_obj_from_addr(), rather than trying to look up `_end' in
the each object, instead use obj->mapsize as the upper bound.


# 1.15 13-Sep-2002 mycroft

Nuke two frivolous assert()s.


# 1.14 06-Sep-2002 mycroft

Introduce a new flag, `isdynamic', which is used to remember whether the
executable was of type ET_DYN. Use this instead of `mainprog' to determine
whether we need to do base-relative fixups of the PLT. (This allows loading
non-relocatable objects, should we desire to do that at some point...)


# 1.13 01-Jun-2002 lukem

SIMPLEQ rototill:
- implement SIMPLEQ_REMOVE(head, elm, type, field). whilst it's O(n),
this mirrors the functionality of SLIST_REMOVE() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE()
- remove the unnecessary elm arg from SIMPLEQ_REMOVE_HEAD().
this mirrors the functionality of SLIST_REMOVE_HEAD() (the other
singly-linked list type) and FreeBSD's STAILQ_REMOVE_HEAD()
- remove notes about SIMPLEQ not supporting arbitrary element removal
- use SIMPLEQ_FOREACH() instead of home-grown for loops
- use SIMPLEQ_EMPTY() appropriately
- use SIMPLEQ_*() instead of accessing sqh_first,sqh_last,sqe_next directly
- reorder manual page; be consistent about how the types are listed
- other minor cleanups


Revision tags: netbsd-1-6-PATCH002-RELEASE netbsd-1-6-PATCH002 netbsd-1-6-PATCH002-RC4 netbsd-1-6-PATCH002-RC3 netbsd-1-6-PATCH002-RC2 netbsd-1-6-PATCH002-RC1 netbsd-1-6-PATCH001 netbsd-1-6-PATCH001-RELEASE netbsd-1-6-PATCH001-RC3 netbsd-1-6-PATCH001-RC2 netbsd-1-6-PATCH001-RC1 netbsd-1-6-RELEASE netbsd-1-6-RC3 netbsd-1-6-RC2 netbsd-1-6-RC1 netbsd-1-6-base
# 1.12 26-Nov-2000 mycroft

branches: 1.12.2;
Return an error when there are too many segments rather than abort()ing.
This way `ldd /usr/libexec/ld.elf_so' doesn't core dump.


Revision tags: netbsd-1-5-PATCH002 netbsd-1-5-PATCH001 netbsd-1-5-RELEASE netbsd-1-5-BETA2 netbsd-1-5-BETA netbsd-1-5-ALPHA2 netbsd-1-5-base minoura-xpg4dl-base
# 1.11 13-Feb-2000 chs

branches: 1.11.4;
switch back to using a file mapping for the initial mapping of a new object.
this allows the kernel to use PMAP_PREFER() to give us better alignment on
platforms that care.


Revision tags: wrstuden-devbsize-19991221 wrstuden-devbsize-base
# 1.10 07-Nov-1999 mycroft

Changes from msaitoh to fix local/global symbol confusion, and to fix weak
symbol support -- updated by me for elf.h changes.


Revision tags: comdex-fall-1999-base
# 1.9 04-Nov-1999 erh

Remove ELFNAME defines since they are defined in sys/exec_elf.h


# 1.8 25-Oct-1999 kleink

Update to match new SVR4-style definition names in <sys/exec_elf.h>.


# 1.7 22-Oct-1999 hannken

Undo last change. While these macros are defined in exec_elf.h, they are
enclosed by `#ifdef _KERNEL'.


# 1.6 21-Oct-1999 erh

Remove ELFNAME macros which are already defines in exec_elf.h


# 1.5 06-Aug-1999 thorpej

branches: 1.5.2;
When allocating the entire address space of an object, map an anonymous
region first (using the data/bss protection) covering it, then overlay
the text and data regions at the appropriate offsets within the region,
and then unmap any gap between the text and data.

The previous method of maping the entire address space with the actual
file object itself is incorrect, as it may extend past the end of the
file if the section alignment is large enough.

This bug was the source of the libposix failure on the SPARC and another
similar failure (with libc!) on the Alpha (failure was accompanied by
a "uvn_io: size check fired" message on the console).


Revision tags: netbsd-1-4-PATCH003 netbsd-1-4-PATCH002 netbsd-1-4-PATCH001 netbsd-1-4-RELEASE netbsd-1-4-base
# 1.4 01-Mar-1999 christos

KNF


# 1.3 20-Feb-1998 mycroft

Fill in missing (default) mmap(2) flags.


Revision tags: netbsd-1-3-PATCH003 netbsd-1-3-PATCH003-CANDIDATE2 netbsd-1-3-PATCH003-CANDIDATE1 netbsd-1-3-PATCH003-CANDIDATE0 netbsd-1-3-PATCH002 netbsd-1-3-PATCH001 netbsd-1-3-RELEASE netbsd-1-3-BETA netbsd-1-3-base
# 1.2 17-Dec-1996 jonathan

Mipsel changes to ELF ldd/ld.so:
* don't abort() on NetBSD/pmax 1.1 elf binaries;
* Assume 32-bit elf unless compiling for Alpha;
* note bugs in ldd's README.


# 1.1 16-Dec-1996 cgd

First cut at an ELF shared loader. Originally from John Polstra's FreeBSD elf
kit, then hacked on by Matt Thomas <matt@3am-software.com>, then by me (to
make it work with new versions of the toolchain, etc.). This runs, but it's
in serious need of cleaning and/or a fair bit of reworking. See the README
file for more information, and a list of things to do.