xref: /freebsd/libexec/rtld-elf/rtld.1 (revision e9ac41698b2f322d55ccf9da50a3596edb2c1800)
1.\" Copyright (c) 1995 Paul Kranenburg
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgment:
14.\"      This product includes software developed by Paul Kranenburg.
15.\" 3. The name of the author may not be used to endorse or promote products
16.\"    derived from this software without specific prior written permission
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28.\"
29.Dd April 28, 2024
30.Dt RTLD 1
31.Os
32.Sh NAME
33.Nm ld-elf.so.1 ,
34.Nm ld.so ,
35.Nm rtld
36.Nd run-time link-editor
37.Sh DESCRIPTION
38The
39.Nm
40utility is a self-contained shared object providing run-time
41support for loading and link-editing shared objects into a process'
42address space.
43It is also commonly known as the dynamic linker.
44It uses the data structures
45contained within dynamically linked programs to determine which shared
46libraries are needed and loads them using the
47.Xr mmap 2
48system call.
49.Pp
50After all shared libraries have been successfully loaded,
51.Nm
52proceeds to resolve external references from both the main program and
53all objects loaded.
54A mechanism is provided for initialization routines
55to be called on a per-object basis, giving a shared object an opportunity
56to perform any extra set-up before execution of the program proper begins.
57This is useful for C++ libraries that contain static constructors.
58.Pp
59When resolving dependencies for the loaded objects,
60.Nm
61translates dynamic token strings in rpath and soname.
62If the
63.Fl "z origin"
64option of the static linker was set when linking the binary,
65the token expansion is performed at the object load time, see
66.Xr ld 1 .
67The following strings are recognized now:
68.Bl -tag -width ".Pa $PLATFORM"
69.It Pa $ORIGIN
70Translated to the full path of the loaded object.
71.It Pa $OSNAME
72Translated to the name of the operating system implementation.
73.It Pa $OSREL
74Translated to the release level of the operating system.
75.It Pa $PLATFORM
76Translated to the machine hardware platform.
77.It Pa $LIB
78Translated to the system library path component on the platform.
79It is
80.Pa lib
81for native binaries, and typically
82.Pa lib32
83for compat32 binaries.
84Other translations might exist for other ABIs supported on the platform.
85.El
86.Pp
87The
88.Nm
89utility itself is loaded by the kernel together with any dynamically-linked
90program that is to be executed.
91The kernel transfers control to the
92dynamic linker.
93After the dynamic linker has finished loading,
94relocating, and initializing the program and its required shared
95objects, it transfers control to the entry point of the program.
96The following search order is used to locate required shared objects:
97.Pp
98.Bl -enum -offset indent -compact
99.It
100.Dv DT_RPATH
101of the referencing object unless that object also contains a
102.Dv DT_RUNPATH
103tag
104.It
105.Dv DT_RPATH
106of the program unless the referencing object contains a
107.Dv DT_RUNPATH
108tag
109.It
110Path indicated by
111.Ev LD_LIBRARY_PATH
112environment variable
113.It
114.Dv DT_RUNPATH
115of the referencing object
116.It
117Hints file produced by the
118.Xr ldconfig 8
119utility
120.It
121The
122.Pa /lib
123and
124.Pa /usr/lib
125directories, unless the referencing object was linked using the
126.Dq Fl z Ar nodefaultlib
127option
128.El
129.Pp
130The
131.Nm
132utility
133recognizes a number of environment variables that can be used to modify
134its behaviour.
135On 64-bit architectures, the linker for 32-bit objects recognizes
136all the environment variables listed below, but is being prefixed with
137.Ev LD_32_ ,
138for example:
139.Ev LD_32_TRACE_LOADED_OBJECTS .
140If the activated image is setuid or setgid, the variables are ignored.
141.Bl -tag -width ".Ev LD_LIBMAP_DISABLE"
142.It Ev LD_DUMP_REL_POST
143If set,
144.Nm
145will print a table containing all relocations after symbol
146binding and relocation.
147.It Ev LD_DUMP_REL_PRE
148If set,
149.Nm
150will print a table containing all relocations before symbol
151binding and relocation.
152.It Ev LD_DYNAMIC_WEAK
153If set, use the ELF standard-compliant symbol lookup behavior:
154resolve to the first found symbol definition.
155.Pp
156By default,
157.Fx
158provides the non-standard symbol lookup behavior:
159when a weak symbol definition is found, remember the definition and
160keep searching in the remaining shared objects for a non-weak definition.
161If found, the non-weak definition is preferred, otherwise the remembered
162weak definition is returned.
163.Pp
164Symbols exported by dynamic linker itself (see
165.Xr dlfcn 3 )
166are always resolved using
167.Fx
168rules regardless of the presence of the variable.
169This variable is unset for set-user-ID and set-group-ID programs.
170.It Ev LD_LIBMAP
171A library replacement list in the same format as
172.Xr libmap.conf 5 .
173For convenience, the characters
174.Ql =
175and
176.Ql \&,
177can be used instead of a space and a newline.
178This variable is parsed after
179.Xr libmap.conf 5 ,
180and will override its entries.
181This variable is unset for set-user-ID and set-group-ID programs.
182.It Ev LD_LIBMAP_DISABLE
183If set, disables the use of
184.Xr libmap.conf 5
185and
186.Ev LD_LIBMAP .
187This variable is unset for set-user-ID and set-group-ID programs.
188.It Ev LD_ELF_HINTS_PATH
189This variable will override the default location of
190.Dq hints
191file.
192This variable is unset for set-user-ID and set-group-ID programs.
193.It Ev LD_LIBRARY_PATH
194A colon separated list of directories, overriding the default search path
195for shared libraries.
196This variable is unset for set-user-ID and set-group-ID programs.
197.It Ev LD_LIBRARY_PATH_RPATH
198If the variable is specified and has a value starting with
199any of \'y\', \'Y\' or \'1\' symbols, the path specified by
200.Ev LD_LIBRARY_PATH
201variable is allowed to override the path from
202.Dv DT_RPATH
203for binaries which does not contain
204.Dv DT_RUNPATH
205tag.
206For such binaries, when the variable
207.Ev LD_LIBRARY_PATH_RPATH
208is set,
209.Dq Fl z Ar nodefaultlib
210link-time option is ignored as well.
211.It Ev LD_PRELOAD
212A list of shared libraries, separated by colons and/or white space,
213to be linked in before any
214other shared libraries.
215If the directory is not specified then
216the directories specified by
217.Ev LD_LIBRARY_PATH
218will be searched first
219followed by the set of built-in standard directories.
220This variable is unset for set-user-ID and set-group-ID programs.
221.It Ev LD_PRELOAD_FDS
222A colon separated list of file descriptor numbers for libraries.
223This is intended for preloading libraries in which we already have a file
224descriptor.
225This may optimize the process of loading libraries because we do not have to
226look for them in directories.
227It may also be useful in a capability base system where we do not have access to
228global namespaces such as the filesystem.
229.It Ev LD_LIBRARY_PATH_FDS
230A colon separated list of file descriptor numbers for library directories.
231This is intended for use within
232.Xr capsicum 4
233sandboxes, when global namespaces such as the filesystem are unavailable.
234It is consulted just after LD_LIBRARY_PATH.
235This variable is unset for set-user-ID and set-group-ID programs.
236.It Ev LD_BIND_NOT
237When set to a nonempty string, prevents modifications of the PLT slots when
238doing bindings.
239As result, each call of the PLT-resolved function is resolved.
240In combination with debug output, this provides complete account of
241all bind actions at runtime.
242This variable is unset for set-user-ID and set-group-ID programs.
243.It Ev LD_BIND_NOW
244When set to a nonempty string, causes
245.Nm
246to relocate all external function calls before starting execution of the
247program.
248Normally, function calls are bound lazily, at the first call
249of each function.
250.Ev LD_BIND_NOW
251increases the start-up time of a program, but it avoids run-time
252surprises caused by unexpectedly undefined functions.
253.It Ev LD_TRACE_LOADED_OBJECTS
254When set to a nonempty string, causes
255.Nm
256to exit after loading the shared objects and printing a summary which includes
257the absolute pathnames of all objects, to standard output.
258.It Ev LD_TRACE_LOADED_OBJECTS_ALL
259When set to a nonempty string, causes
260.Nm
261to expand the summary to indicate which objects caused each object to
262be loaded.
263.It Ev LD_TRACE_LOADED_OBJECTS_FMT1
264.It Ev LD_TRACE_LOADED_OBJECTS_FMT2
265When set, these variables are interpreted as format strings a la
266.Xr printf 3
267to customize the trace output and are used by
268.Xr ldd 1 Ns 's
269.Fl f
270option and allows
271.Xr ldd 1
272to be operated as a filter more conveniently.
273If the dependency name starts with string
274.Pa lib ,
275.Ev LD_TRACE_LOADED_OBJECTS_FMT1
276is used, otherwise
277.Ev LD_TRACE_LOADED_OBJECTS_FMT2
278is used.
279The following conversions can be used:
280.Bl -tag -width 4n
281.It Li %a
282The main program's name
283(also known as
284.Dq __progname ) .
285.It Li \&%A
286The value of the environment variable
287.Ev LD_TRACE_LOADED_OBJECTS_PROGNAME .
288Typically used to print both the names of programs and shared libraries
289being inspected using
290.Xr ldd 1 .
291.It Li %o
292The library name.
293.It Li %p
294The full pathname as determined by
295.Nm rtld Ns 's
296library search rules.
297.It Li %x
298The library's load address.
299.El
300.Pp
301Additionally,
302.Ql \en
303and
304.Ql \et
305are recognized and have their usual meaning.
306.It Ev LD_UTRACE
307If set,
308.Nm
309will log events such as the loading and unloading of shared objects via
310.Xr utrace 2 .
311.It Ev LD_LOADFLTR
312If set,
313.Nm
314will process the filtee dependencies of the loaded objects immediately,
315instead of postponing it until required.
316Normally, the filtees are opened at the time of the first symbol resolution
317from the filter object.
318.It Ev LD_SHOW_AUXV
319If set, causes
320.Nm
321to dump content of the aux vector to standard output, before passing
322control to any user code.
323.It Ev LD_STATIC_TLS_EXTRA
324If the variable is specified and has a numeric value,
325.Nm
326will set the size of the static TLS extra space to the specified number
327of bytes.
328The static TLS extra space is used when loading objects compiled for
329initial-exec TLS code model with
330.Xr dlopen 3 .
331The minimum value that can be specified is \'128\'.
332.El
333.Sh DIRECT EXECUTION MODE
334.Nm
335is typically used implicitly, loaded by the kernel as requested by the
336.Dv PT_INTERP
337program header of the executed binary.
338.Fx
339also supports a direct execution mode for the dynamic linker.
340In this mode, the user explicitly executes
341.Nm
342and provides the path of the program to be linked and executed as
343an argument.
344This mode allows use of a non-standard dynamic linker for a program
345activation without changing the binary or without changing
346the installed dynamic linker.
347Execution options may be specified.
348.Pp
349The syntax of the direct invocation is
350.Bd -ragged -offset indent
351.Pa /libexec/ld-elf.so.1
352.Op Fl b Ar exe
353.Op Fl d
354.Op Fl f Ar fd
355.Op Fl o Ar OPT=NAME
356.Op Fl p
357.Op Fl u
358.Op Fl v
359.Op Fl -
360.Pa image_path
361.Op Ar image arguments
362.Ed
363.Pp
364The options are:
365.Bl -tag -width indent
366.It Fl b Ar exe
367Use the executable
368.Fa exe
369instead of
370.Fa image_path
371for activation.
372If this option is specified,
373.Ar image_path
374is only used to provide the
375.Va argv[0]
376value to the program.
377.It Fl d
378Turn off the emulation of the binary execute permission.
379.It Fl f Ar fd
380File descriptor
381.Ar fd
382references the binary to be activated by
383.Nm .
384It must already be opened in the process when executing
385.Nm .
386If this option is specified,
387.Ar image_path
388is only used to provide the
389.Va argv[0]
390value to the program.
391.It Fl o Ar OPT=VALUE
392Set the
393.Ar OPT
394configuration variable to the value
395.Ar VALUE .
396The possible variable names are listed above as
397.Ev LD_
398prefixed environment variables, but here are referenced without the
399.Ev LD_
400prefix.
401A configuration variable set this way does not leak into
402the activated image's environment.
403.Pp
404The option can be repeated as many times as needed to set
405all configuration parameters.
406The parameters set using this option have priority over
407the same parameters assigned via environment.
408.It Fl p
409If the
410.Pa image_path
411argument specifies a name which does not contain a slash
412.Dq Li /
413character,
414.Nm
415uses the search path provided by the environment variable
416.Dv PATH
417to find the binary to execute.
418.It Fl u
419Ignore all
420.Ev LD_
421environment variables and previous command line
422.Fl o
423options that otherwise affect the dynamic
424linker behavior.
425.It Fl v
426Display information about this run-time linker binary, then exit.
427.It Fl -
428Ends the
429.Nm
430options.
431The argument following
432.Fl -
433is interpreted as the path of the binary to execute.
434.El
435.Pp
436In the direct execution mode,
437.Nm
438emulates verification of the binary execute permission for the
439current user.
440This is done to avoid breaking user expectations in naively restricted
441execution environments.
442The verification only uses Unix
443.Dv DACs ,
444ignores
445.Dv ACLs ,
446and is naturally prone to race conditions.
447Environments which rely on such restrictions are weak
448and breakable on their own.
449It can be turned off with the
450.Fl d
451option.
452.Sh VERSIONING
453Newer
454.Nm
455might provide some features or changes in runtime behavior that cannot be
456easily detected at runtime by checking of the normal exported symbols.
457Note that it is almost always wrong to verify
458.Dv __FreeBSD_version
459in userspace to detect features, either at compile or at run time,
460because either kernel, or libc, or environment variables could not
461match the running
462.Nm .
463.Pp
464To solve the problem,
465.Nm
466exports some feature indicators in the
467.Fx
468private symbols namespace
469.Dv FBSDprivate_1.0 .
470Symbols start with the
471.Dv _rtld_version
472prefix.
473Current list of defined symbols and corresponding features is:
474.Bl -tag -width indent
475.It Dv _rtld_version__FreeBSD_version
476Symbol exports the value of the
477.Dv __FreeBSD_version
478definition as it was provided during the
479.Nm
480build.
481The symbol is always present since the
482.Dv _rtld_version
483facility was introduced.
484.It Dv _rtld_version_laddr_offset
485The
486.Va l_addr
487member of the
488.Vt link_map
489structure contains the load offset of the shared object.
490Before that,
491.Va l_addr
492contained the base address of the library.
493See
494.Xr dlinfo 3 .
495.Pp
496Also it indicates the presence of
497.Va l_refname
498member of the structure.
499.It Dv _rtld_version_dlpi_tls_data
500The
501.Va dlpi_tls_data
502member of the structure
503.Vt dl_phdr_info
504contains the address of the module TLS segment for the calling thread,
505and not the address of the initialization segment.
506.El
507.Sh FILES
508.Bl -tag -width ".Pa /var/run/ld-elf32.so.hints" -compact
509.It Pa /var/run/ld-elf.so.hints
510Hints file.
511.It Pa /var/run/ld-elf32.so.hints
512Hints file for 32-bit binaries on 64-bit system.
513.It Pa /etc/libmap.conf
514The libmap configuration file.
515.It Pa /etc/libmap32.conf
516The libmap configuration file for 32-bit binaries on 64-bit system.
517.El
518.Sh SEE ALSO
519.Xr ld 1 ,
520.Xr ldd 1 ,
521.Xr dlinfo 3 ,
522.Xr capsicum 4 ,
523.Xr elf 5 ,
524.Xr libmap.conf 5 ,
525.Xr ldconfig 8
526