1.\" $NetBSD: ld.elf_so.1,v 1.8 2003/02/25 10:34:49 wiz 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.\" 3. All advertising materials mentioning features or use of this software 18.\" must display the following acknowledgement: 19.\" This product includes software developed by the NetBSD 20.\" Foundation, Inc. and its contributors. 21.\" 4. Neither the name of The NetBSD Foundation nor the names of its 22.\" contributors may be used to endorse or promote products derived 23.\" from this software without specific prior written permission. 24.\" 25.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35.\" POSSIBILITY OF SUCH DAMAGE. 36.\" 37.Dd October 23, 2001 38.Dt LD.ELF_SO 1 39.Os 40.Sh NAME 41.Nm ld.elf_so 42.Nd run-time link-editor (linker) 43.Sh DESCRIPTION 44.Nm 45is a self-contained, position independent program 46image providing run-time support for loading and 47link-editing shared objects into a process' address space. 48It uses information stored in data structures within the binary (see 49.Xr elf 5 ) 50and environment variables to determine which shared objects are needed. 51These shared objects are loaded at a convenient virtual address using the 52.Xr mmap 2 53system call. 54After all shared objects have been successfully loaded, 55.Nm 56proceeds to resolve external references from both 57the main program and all objects loaded. 58Once all required references are resolved control is 59passed to the program via its entry point. 60.Ss Startup 61On the execution of a dynamically linked binary the kernel will load 62the program and its run-time linker as specified in the PT_INTERP 63section in the program header. 64At this point, instead of passing control directly to the program, 65the kernel passes control to the specified linker. 66An auxiliary vector of information is passed that includes 67the address of the program header, the size of each entry in the header, 68and the number of entries. 69The entry point of the program and the base address of where 70.Nm 71is loaded is also supplied. 72.Pp 73Careful use of code allows 74.Nm 75to relocate itself before proceeding. 76Specifically the use of global variables and 77large switch statements is not allowed. 78The later can cause the output of a jump table that 79can use the equivalent of a global variable. 80.Ss Finding objects 81Each 82.Xr elf 5 83object file may contain information in its dynamic (PT_DYNAMIC) section 84about which shared objects it requires (often referred to as dependencies). 85These dependencies are specified in the optional DT_NEEDED entry within 86the dynamic section. 87Each DT_NEEDED entry refers to a filename string of 88the shared object that is to be searched for. 89.Pp 90The linker will search for libraries in three lists of paths: 91.Bl -enum 92.It 93A user defined list of paths as specified in LD_LIBRARY_PATH and 94.Xr ld.so.conf 5 . 95.Pp 96The use of ld.so.conf should be avoided as the setting of a global search 97path can present a security risk. 98.It 99A list of paths specified within a shared object using a DT_RPATH entry in 100the dynamic section. 101This is defined at shared object link time. 102.It 103The list of default paths which is set to 104.Pa /usr/lib . 105.El 106.Pp 107The filename string can be considered free form, however, it will almost 108always take the form lib\*[Lt]name\*[Gt].so.\*[Lt]number\*[Gt], 109where name specifies the 110.Sq library 111name and number is conceptually the library's major version number. 112.Pp 113This name and another of the form lib\*[Lt]name\*[Gt].so are normally 114symbolic links to the real shared object which has a filename of the form 115lib\*[Lt]name\*[Gt].so.\*[Lt]major\*[Gt].\*[Lt]minor\*[Gt][.\*[Lt]teeny\*[Gt]]. 116This naming convention allows a versioning scheme similar to 117.Xr a.out 5 . 118.Ss Relocation 119.Nm 120will perform all necessary relocations immediately except for relocations 121relating to the Procedure Linkage Table (PLT). 122The PLT is used as a indirection method for procedure 123calls to globally defined functions. 124It allows, through the use of intermediate code, the delayed binding of 125a call to a globally defined function to be performed at procedure call time. 126This 127.Sq lazy 128method is the default (see LD_BIND_NOW). 129.Ss Initialization 130A mechanism is provided for initialization and termination routines 131to be called, on a per-object basis before execution of the program proper 132begins or after the program has completed. 133This gives a shared object an opportunity to perform 134any extra set-up or completion work. 135.Pp 136The DT_INIT and DT_FINI entries in the dynamic section specify the addresses 137of the initialization and termination functions, respectively, for 138the shared object. 139.Nm 140arranges for each initialization function to be called before control is passed 141to the program and for the termination functions to be called by using 142.Xr atexit 3 . 143.Pp 144This mechanism is exploited by the system-supplied constructor 145initialization and destructor code located in 146.Pa /usr/lib/crtbeginS.o 147and 148.Pa /usr/lib/crtendS.o . 149These files are automatically included by 150.Xr cc 1 151and 152.Xr c++ 1 153in the list of object-code files passed to 154.Xr ld 1 155when building a shared C or C++ object. 156.Sh ENVIRONMENT 157If the following environment variables exist they will be used by 158.Nm . 159.Bl -tag -width "LD_LIBRARY_PATH" 160.It Ev LD_LIBRARY_PATH 161A colon separated list of directories, overriding the default search path 162for shared libraries. 163.It Ev LD_PRELOAD 164A colon or space separated list of shared object filenames to be loaded 165.Em after 166the main program but 167.Em before 168its shared object dependencies. 169Space is allowed as a separator for backwards compatibility only. 170Support may be removed in a future release and should not be relied upon. 171.It Ev LD_BIND_NOW 172If defined immediate binding of Procedure Link Table (PLT) entries is 173performed instead of the default lazy method. 174.It Ev LD_DEBUG 175If defined a variety of debug information will be written to the standard 176error of an dynamically linked executed when it is run. 177This variable is only recognized if 178.Nm 179was compiled with debugging support 180.Sy ( -DDEBUG ) . 181.El 182.Sh FILES 183.Bl -tag -width /etc/ld.so.conf -compact 184.It Pa /etc/ld.so.conf 185library location hints supplied by the system administrator. 186.El 187.Sh SEE ALSO 188.Xr ld 1 , 189.Xr ld.aout_so 1 , 190.Xr dlfcn 3 , 191.Xr elf 5 192.Sh HISTORY 193The ELF shared library model employed first appeared in Sys V R4. 194.Sh SECURITY CONSIDERATIONS 195The environment variables 196.Ev LD_LIBRARY_PATH 197and 198.Ev LD_PRELOAD 199are not honored when executing in a set-user-ID or set-group-ID environment. 200This action is taken to prevent malicious substitution of shared object 201dependencies or interposition of symbols. 202