1.\" $NetBSD: ld.elf_so.1,v 1.21 2019/04/15 10:17:01 maya Exp $ 2.\" 3.\" Copyright (c) 2001 The NetBSD Foundation, Inc. 4.\" All rights reserved. 5.\" 6.\" This code is derived from software contributed to The NetBSD Foundation 7.\" by Nick Hudson. 8.\" 9.\" Redistribution and use in source and binary forms, with or without 10.\" modification, are permitted provided that the following conditions 11.\" are met: 12.\" 1. Redistributions of source code must retain the above copyright 13.\" notice, this list of conditions and the following disclaimer. 14.\" 2. Redistributions in binary form must reproduce the above copyright 15.\" notice, this list of conditions and the following disclaimer in the 16.\" documentation and/or other materials provided with the distribution. 17.\" 18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28.\" POSSIBILITY OF SUCH DAMAGE. 29.\" 30.Dd April 15, 2019 31.Dt LD.ELF_SO 1 32.Os 33.Sh NAME 34.Nm ld.elf_so 35.Nd run-time link-editor (linker) 36.Sh DESCRIPTION 37.Nm 38is a self-contained, position independent program 39image providing run-time support for loading and 40link-editing shared objects into a process' address space. 41It uses information stored in data structures within the binary (see 42.Xr elf 5 ) 43and environment variables to determine which shared objects are needed. 44These shared objects are loaded at a convenient virtual address using the 45.Xr mmap 2 46system call. 47After all shared objects have been successfully loaded, 48.Nm 49proceeds to resolve external references from both 50the main program and all objects loaded. 51Once all required references are resolved control is 52passed to the program via its entry point. 53.Ss Startup 54On the execution of a dynamically linked binary the kernel will load 55the program and its run-time linker as specified in the PT_INTERP 56section in the program header. 57At this point, instead of passing control directly to the program, 58the kernel passes control to the specified linker. 59An auxiliary vector of information is passed that includes 60the address of the program header, the size of each entry in the header, 61and the number of entries. 62The entry point of the program and the base address of where 63.Nm 64is loaded is also supplied. 65.Ss Finding objects 66Each 67.Xr elf 5 68object file may contain information in its dynamic (PT_DYNAMIC) section 69about which shared objects it requires (often referred to as dependencies). 70These dependencies are specified in the optional 71.Dv DT_NEEDED 72entry within the dynamic section. 73Each 74.Dv DT_NEEDED 75entry refers to a filename string of 76the shared object that is to be searched for. 77.Pp 78The linker will search for libraries in three lists of paths: 79.Bl -enum 80.It 81A user defined list of paths as specified in LD_LIBRARY_PATH and 82.Xr ld.so.conf 5 . 83.Pp 84The use of ld.so.conf should be avoided as the setting of a global search 85path can present a security risk. 86.It 87A list of paths specified within a shared object using the 88.Dv DT_RPATH 89and 90.Dv DT_RUNPATH 91entries in the dynamic section. 92This is defined at shared object link time. 93.It 94The list of default paths which is set to 95.Pa /usr/lib . 96.El 97.Pp 98Dynamic loading via 99.Xr dlopen 3 100uses the 101.Dv DT_RPATH 102and 103.Dv DT_RUNPATH 104entries of the main binary, independently of 105which object the call came from. 106.Pp 107.Nm 108will expand the following variables if present in the paths: 109.Bl -tag -width $PLATFORM 110.It $HWCAP 111Processor hardware capabilities, for example FPU, MMX, SSE. 112Currently unimplemented. 113.It $ISALIST 114List of instructions sets this processor can execute. 115Currently unimplemented. 116.It $ORIGIN 117The directory of the main object. 118.It $OSNAME 119The value of the 120.Dv kern.ostype 121.Xr sysctl 3 . 122.It $OSREL 123The value of the 124.Dv kern.osrelease 125.Xr sysctl 3 . 126.It $PLATFORM 127The value of the 128.Dv hw.machine_arch 129.Xr sysctl 3 . 130.El 131.Pp 132Both 133.Dv ${VARIABLE} 134and 135.Dv $VARIABLE 136are recognized. 137.Pp 138The filename string can be considered free form, however, it will almost 139always take the form lib<name>.so.<number>, 140where name specifies the 141.Sq library 142name and number is conceptually the library's major version number. 143.Pp 144This name and another of the form lib<name>.so are normally 145symbolic links to the real shared object which has a filename of the form 146lib<name>.so.<major>.<minor>[.<teeny>]. 147This naming convention allows a versioning scheme similar to 148.Xr a.out 5 . 149.Ss Relocation 150.Nm 151will perform all necessary relocations immediately except for relocations 152relating to the Procedure Linkage Table (PLT). 153The PLT is used as an indirection method for procedure 154calls to globally defined functions. 155It allows, through the use of intermediate code, the delayed binding of 156a call to a globally defined function to be performed at procedure call time. 157This 158.Sq lazy 159method is the default (see LD_BIND_NOW). 160.Ss Initialization 161A mechanism is provided for initialization and termination routines 162to be called, on a per-object basis before execution of the program proper 163begins or after the program has completed. 164This gives a shared object an opportunity to perform 165any extra set-up or completion work. 166.Pp 167The 168.Dv DT_INIT 169and 170.Dv DT_FINI 171entries in the dynamic section specify the addresses 172of the initialization and termination functions, respectively, for 173the shared object. 174.Nm 175arranges for each initialization function to be called before control is passed 176to the program and for the termination functions to be called by using 177.Xr atexit 3 . 178.Pp 179This mechanism is exploited by the system-supplied constructor 180initialization and destructor code located in 181.Pa /usr/lib/crtbeginS.o 182and 183.Pa /usr/lib/crtendS.o . 184These files are automatically included by 185.Xr cc 1 186and 187.Xr c++ 1 188in the list of object-code files passed to 189.Xr ld 1 190when building a shared C or C++ object. 191.Sh ENVIRONMENT 192If the following environment variables exist they will be used by 193.Nm . 194.Bl -tag -width "LD_LIBRARY_PATH" 195.It Ev LD_LIBRARY_PATH 196A colon separated list of directories, overriding the default search path 197for shared libraries. 198.It Ev LD_PRELOAD 199A colon or space separated list of shared object filenames to be loaded 200.Em after 201the main program but 202.Em before 203its shared object dependencies. 204Space is allowed as a separator for backwards compatibility only. 205Support may be removed in a future release and should not be relied upon. 206.It Ev LD_BIND_NOW 207If defined immediate binding of Procedure Link Table (PLT) entries is 208performed instead of the default lazy method. 209.It Ev LD_DEBUG 210If defined a variety of debug information will be written to the standard 211error of an dynamically linked executed when it is run. 212This variable is only recognized if 213.Nm 214was compiled with debugging support 215.Sy ( -DDEBUG ) . 216.El 217.Sh FILES 218.Bl -tag -width /etc/ld.so.conf -compact 219.It Pa /etc/ld.so.conf 220library location hints supplied by the system administrator. 221.El 222.Sh SEE ALSO 223.Xr ld 1 , 224.Xr ld.aout_so 1 , 225.Xr dlfcn 3 , 226.Xr elf 5 227.Sh HISTORY 228The ELF shared library model employed first appeared in Sys V R4. 229.Pp 230The path expansion variables first appeared in Solaris 10, and 231in 232.Nx 5.0 . 233.Sh SECURITY CONSIDERATIONS 234The environment variables 235.Ev LD_LIBRARY_PATH 236and 237.Ev LD_PRELOAD 238are not honored when executing in a set-user-ID or set-group-ID environment. 239This action is taken to prevent malicious substitution of shared object 240dependencies or interposition of symbols. 241