xref: /freebsd/libexec/rtld-elf/rtld.1 (revision 9978553d0199e7ec0bdd1c44fc7f6c7b0c11e43b)
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 November 25, 2025
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.Pp
142The run-time linker is able to access the environment provided
143at process startup.
144After startup, environment variables are maintained by higher-level
145libraries and are not accessible by the run-time linker.
146At run-time, effective settings can be queried using
147.Xr rtld_get_var 3 ,
148and some of them can be changed with
149.Xr rtld_set_var 3 .
150.Bl -tag -width ".Ev LD_LIBMAP_DISABLE"
151.It Ev LD_DUMP_REL_POST
152If set,
153.Nm
154will print a table containing all relocations after symbol
155binding and relocation.
156.It Ev LD_DEBUG
157If set,
158.Nm
159will print voluminous internal debugging messages as it loads
160and links a program.
161.It Ev LD_DUMP_REL_PRE
162If set,
163.Nm
164will print a table containing all relocations before symbol
165binding and relocation.
166.It Ev LD_DYNAMIC_WEAK
167If set, use the ELF standard-compliant symbol lookup behavior:
168resolve to the first found symbol definition.
169.Pp
170By default,
171.Fx
172provides the non-standard symbol lookup behavior:
173when a weak symbol definition is found, remember the definition and
174keep searching in the remaining shared objects for a non-weak definition.
175If found, the non-weak definition is preferred, otherwise the remembered
176weak definition is returned.
177.Pp
178Symbols exported by dynamic linker itself (see
179.Xr dlfcn 3 )
180are always resolved using
181.Fx
182rules regardless of the presence of the variable.
183This variable is unset for set-user-ID and set-group-ID programs.
184.It Ev LD_LIBMAP
185A library replacement list in the same format as
186.Xr libmap.conf 5 .
187For convenience, the characters
188.Ql =
189and
190.Ql \&,
191can be used instead of a space and a newline.
192This variable is parsed after
193.Xr libmap.conf 5 ,
194and will override its entries.
195This variable is unset for set-user-ID and set-group-ID programs.
196.It Ev LD_LIBMAP_DISABLE
197If set, disables the use of
198.Xr libmap.conf 5
199and
200.Ev LD_LIBMAP .
201This variable is unset for set-user-ID and set-group-ID programs.
202.It Ev LD_ELF_HINTS_PATH
203This variable will override the default location of
204.Dq hints
205file.
206This variable is unset for set-user-ID and set-group-ID programs.
207.It Ev LD_LIBRARY_PATH
208A colon separated list of directories, overriding the default search path
209for shared libraries.
210This variable is unset for set-user-ID and set-group-ID programs.
211.It Ev LD_LIBRARY_PATH_RPATH
212If the variable is specified and has a value starting with
213any of \'y\', \'Y\' or \'1\' symbols, the path specified by
214.Ev LD_LIBRARY_PATH
215variable is allowed to override the path from
216.Dv DT_RPATH
217for binaries which does not contain
218.Dv DT_RUNPATH
219tag.
220For such binaries, when the variable
221.Ev LD_LIBRARY_PATH_RPATH
222is set,
223.Dq Fl z Ar nodefaultlib
224link-time option is ignored as well.
225.It Ev LD_PRELOAD
226A list of shared libraries, separated by colons and/or white space,
227to be linked in before any
228other shared libraries.
229If the directory is not specified then
230the directories specified by
231.Ev LD_LIBRARY_PATH
232will be searched first
233followed by the set of built-in standard directories.
234This variable is unset for set-user-ID and set-group-ID programs.
235.It Ev LD_PRELOAD_FDS
236A colon separated list of file descriptor numbers for libraries.
237This is intended for preloading libraries in which we already have a file
238descriptor.
239This may optimize the process of loading libraries because we do not have to
240look for them in directories.
241It may also be useful in a capability base system where we do not have access to
242global namespaces such as the filesystem.
243.It Ev LD_LIBRARY_PATH_FDS
244A colon separated list of file descriptor numbers for library directories.
245This is intended for use within
246.Xr capsicum 4
247sandboxes, when global namespaces such as the filesystem are unavailable.
248It is consulted just after LD_LIBRARY_PATH.
249This variable is unset for set-user-ID and set-group-ID programs.
250.It Ev LD_BIND_NOT
251When set to a nonempty string, prevents modifications of the PLT slots when
252doing bindings.
253As result, each call of the PLT-resolved function is resolved.
254In combination with debug output, this provides complete account of
255all bind actions at runtime.
256This variable is unset for set-user-ID and set-group-ID programs.
257.It Ev LD_BIND_NOW
258When set to a nonempty string, causes
259.Nm
260to relocate all external function calls before starting execution of the
261program.
262Normally, function calls are bound lazily, at the first call
263of each function.
264.Ev LD_BIND_NOW
265increases the start-up time of a program, but it avoids run-time
266surprises caused by unexpectedly undefined functions.
267.It Ev LD_TRACE_LOADED_OBJECTS
268When set to a nonempty string, causes
269.Nm
270to exit after loading the shared objects and printing a summary which includes
271the absolute pathnames of all objects, to standard output.
272.It Ev LD_TRACE_LOADED_OBJECTS_ALL
273When set to a nonempty string, causes
274.Nm
275to expand the summary to indicate which objects caused each object to
276be loaded.
277.It Ev LD_TRACE_LOADED_OBJECTS_FMT1
278.It Ev LD_TRACE_LOADED_OBJECTS_FMT2
279When set, these variables are interpreted as format strings a la
280.Xr printf 3
281to customize the trace output and are used by
282.Xr ldd 1 Ns 's
283.Fl f
284option and allows
285.Xr ldd 1
286to be operated as a filter more conveniently.
287If the dependency name starts with string
288.Pa lib ,
289.Ev LD_TRACE_LOADED_OBJECTS_FMT1
290is used, otherwise
291.Ev LD_TRACE_LOADED_OBJECTS_FMT2
292is used.
293The following conversions can be used:
294.Bl -tag -width 4n
295.It Li %a
296The main program's name
297(also known as
298.Dq __progname ) .
299.It Li \&%A
300The value of the environment variable
301.Ev LD_TRACE_LOADED_OBJECTS_PROGNAME .
302Typically used to print both the names of programs and shared libraries
303being inspected using
304.Xr ldd 1 .
305.It Li %o
306The library name.
307.It Li %p
308The full pathname as determined by
309.Nm rtld Ns 's
310library search rules.
311.It Li %x
312The library's load address.
313.El
314.Pp
315Additionally,
316.Ql \en
317and
318.Ql \et
319are recognized and have their usual meaning.
320.It Ev LD_UTRACE
321If set,
322.Nm
323will log events such as the loading and unloading of shared objects via
324.Xr utrace 2 .
325.It Ev LD_LOADFLTR
326If set,
327.Nm
328will process the filtee dependencies of the loaded objects immediately,
329instead of postponing it until required.
330Normally, the filtees are opened at the time of the first symbol resolution
331from the filter object.
332.It Ev LD_SHOW_AUXV
333If set, causes
334.Nm
335to dump content of the aux vector to standard output, before passing
336control to any user code.
337.It Ev LD_STATIC_TLS_EXTRA
338If the variable is specified and has a numeric value,
339.Nm
340will set the size of the static TLS extra space to the specified number
341of bytes.
342The static TLS extra space is used when loading objects compiled for
343initial-exec TLS code model with
344.Xr dlopen 3 .
345The minimum value that can be specified is \'128\'.
346.It Ev LD_NO_DL_ITERATE_PHDR_AFTER_FORK
347Allow
348.Xr dl_iterate_phdr 3
349to block in callback, without causing deadlock with the
350.Xr fork 2 .
351The drawback is that the image started in this mode cannot use
352.Xr dl_iterate_phdr 3
353after fork.
354.El
355.Sh DIRECT EXECUTION MODE
356.Nm
357is typically used implicitly, loaded by the kernel as requested by the
358.Dv PT_INTERP
359program header of the executed binary.
360.Fx
361also supports a direct execution mode for the dynamic linker.
362In this mode, the user explicitly executes
363.Nm
364and provides the path of the program to be linked and executed as
365an argument.
366This mode allows use of a non-standard dynamic linker for a program
367activation without changing the binary or without changing
368the installed dynamic linker.
369Execution options may be specified.
370.Pp
371The syntax of the direct invocation is
372.Bd -ragged -offset indent
373.Pa /libexec/ld-elf.so.1
374.Op Fl b Ar exe
375.Op Fl d
376.Op Fl f Ar fd
377.Op Fl o Ar OPT=VALUE
378.Op Fl p
379.Op Fl u
380.Op Fl v
381.Op Fl -
382.Pa image_path
383.Op Ar image arguments
384.Ed
385.Pp
386The options are:
387.Bl -tag -width indent
388.It Fl b Ar exe
389Use the executable
390.Fa exe
391instead of
392.Fa image_path
393for activation.
394If this option is specified,
395.Ar image_path
396is only used to provide the
397.Va argv[0]
398value to the program.
399.It Fl d
400Turn off the emulation of the binary execute permission.
401.It Fl f Ar fd
402File descriptor
403.Ar fd
404references the binary to be activated by
405.Nm .
406It must already be opened in the process when executing
407.Nm .
408If this option is specified,
409.Ar image_path
410is only used to provide the
411.Va argv[0]
412value to the program.
413.It Fl o Ar OPT=VALUE
414Set the
415.Ar OPT
416configuration variable to the value
417.Ar VALUE .
418The possible variable names are listed above as
419.Ev LD_
420prefixed environment variables, but here are referenced without the
421.Ev LD_
422prefix.
423A configuration variable set this way does not leak into
424the activated image's environment.
425.Pp
426The option can be repeated as many times as needed to set
427all configuration parameters.
428The parameters set using this option have priority over
429the same parameters assigned via environment.
430.It Fl p
431If the
432.Pa image_path
433argument specifies a name which does not contain a slash
434.Dq Li /
435character,
436.Nm
437uses the search path provided by the environment variable
438.Dv PATH
439to find the binary to execute.
440.It Fl u
441Ignore all
442.Ev LD_
443environment variables and previous command line
444.Fl o
445options that otherwise affect the dynamic
446linker behavior.
447.It Fl v
448Display information about this run-time linker binary, then exit.
449.It Fl -
450Ends the
451.Nm
452options.
453The argument following
454.Fl -
455is interpreted as the path of the binary to execute.
456.El
457.Pp
458In the direct execution mode,
459.Nm
460emulates verification of the binary execute permission for the
461current user.
462This is done to avoid breaking user expectations in naively restricted
463execution environments.
464The verification only uses Unix
465.Dv DACs ,
466ignores
467.Dv ACLs ,
468and is naturally prone to race conditions.
469Environments which rely on such restrictions are weak
470and breakable on their own.
471It can be turned off with the
472.Fl d
473option.
474.Sh VERSIONING
475Newer
476.Nm
477might provide some features or changes in runtime behavior that cannot be
478easily detected at runtime by checking of the normal exported symbols.
479Note that it is almost always wrong to verify
480.Dv __FreeBSD_version
481in userspace to detect features, either at compile or at run time,
482because either kernel, or libc, or environment variables could not
483match the running
484.Nm .
485.Pp
486To solve the problem,
487.Nm
488exports some feature indicators in the
489.Fx
490private symbols namespace
491.Dv FBSDprivate_1.0 .
492Symbols start with the
493.Dv _rtld_version
494prefix.
495Current list of defined symbols and corresponding features is:
496.Bl -tag -width indent
497.It Dv _rtld_version__FreeBSD_version
498Symbol exports the value of the
499.Dv __FreeBSD_version
500definition as it was provided during the
501.Nm
502build.
503The symbol is always present since the
504.Dv _rtld_version
505facility was introduced.
506.It Dv _rtld_version_laddr_offset
507The
508.Va l_addr
509member of the
510.Vt link_map
511structure contains the load offset of the shared object.
512Before that,
513.Va l_addr
514contained the base address of the library.
515See
516.Xr dlinfo 3 .
517.Pp
518Also it indicates the presence of
519.Va l_refname
520member of the structure.
521.It Dv _rtld_version_dlpi_tls_data
522The
523.Va dlpi_tls_data
524member of the structure
525.Vt dl_phdr_info
526contains the address of the module TLS segment for the calling thread,
527and not the address of the initialization segment.
528.El
529.Sh FILES
530.Bl -tag -width ".Pa /var/run/ld-elf32.so.hints" -compact
531.It Pa /var/run/ld-elf.so.hints
532Hints file.
533.It Pa /var/run/ld-elf32.so.hints
534Hints file for 32-bit binaries on 64-bit system.
535.It Pa /etc/libmap.conf
536The libmap configuration file.
537.It Pa /etc/libmap32.conf
538The libmap configuration file for 32-bit binaries on 64-bit system.
539.El
540.Sh SEE ALSO
541.Xr ld 1 ,
542.Xr ldd 1 ,
543.Xr dlinfo 3 ,
544.Xr rtld_get_var 3 ,
545.Xr capsicum 4 ,
546.Xr elf 5 ,
547.Xr libmap.conf 5 ,
548.Xr ldconfig 8
549