xref: /freebsd-13.1/libexec/rtld-elf/rtld.1 (revision df674da4)
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.\" $FreeBSD$
30.\"
31.Dd June 2, 2021
32.Dt RTLD 1
33.Os
34.Sh NAME
35.Nm ld-elf.so.1 ,
36.Nm ld.so ,
37.Nm rtld
38.Nd run-time link-editor
39.Sh DESCRIPTION
40The
41.Nm
42utility is a self-contained shared object providing run-time
43support for loading and link-editing shared objects into a process'
44address space.
45It is also commonly known as the dynamic linker.
46It uses the data structures
47contained within dynamically linked programs to determine which shared
48libraries are needed and loads them using the
49.Xr mmap 2
50system call.
51.Pp
52After all shared libraries have been successfully loaded,
53.Nm
54proceeds to resolve external references from both the main program and
55all objects loaded.
56A mechanism is provided for initialization routines
57to be called on a per-object basis, giving a shared object an opportunity
58to perform any extra set-up before execution of the program proper begins.
59This is useful for C++ libraries that contain static constructors.
60.Pp
61When resolving dependencies for the loaded objects,
62.Nm
63translates dynamic token strings in rpath and soname.
64If the
65.Fl "z origin"
66option of the static linker was set when linking the binary,
67the token expansion is performed at the object load time, see
68.Xr ld 1 .
69The following strings are recognized now:
70.Bl -tag -width ".Pa $PLATFORM"
71.It Pa $ORIGIN
72Translated to the full path of the loaded object.
73.It Pa $OSNAME
74Translated to the name of the operating system implementation.
75.It Pa $OSREL
76Translated to the release level of the operating system.
77.It Pa $PLATFORM
78Translated to the machine hardware platform.
79.El
80.Pp
81The
82.Nm
83utility itself is loaded by the kernel together with any dynamically-linked
84program that is to be executed.
85The kernel transfers control to the
86dynamic linker.
87After the dynamic linker has finished loading,
88relocating, and initializing the program and its required shared
89objects, it transfers control to the entry point of the program.
90The following search order is used to locate required shared objects:
91.Pp
92.Bl -enum -offset indent -compact
93.It
94.Dv DT_RPATH
95of the referencing object unless that object also contains a
96.Dv DT_RUNPATH
97tag
98.It
99.Dv DT_RPATH
100of the program unless the referencing object contains a
101.Dv DT_RUNPATH
102tag
103.It
104Path indicated by
105.Ev LD_LIBRARY_PATH
106environment variable
107.It
108.Dv DT_RUNPATH
109of the referencing object
110.It
111Hints file produced by the
112.Xr ldconfig 8
113utility
114.It
115The
116.Pa /lib
117and
118.Pa /usr/lib
119directories, unless the referencing object was linked using the
120.Dq Fl z Ar nodefaultlib
121option
122.El
123.Pp
124The
125.Nm
126utility
127recognizes a number of environment variables that can be used to modify
128its behaviour.
129On 64-bit architectures, the linker for 32-bit objects recognizes
130all the environment variables listed below, but is being prefixed with
131.Ev LD_32_ ,
132for example:
133.Ev LD_32_TRACE_LOADED_OBJECTS .
134If the activated image is setuid or setgid, the variables are ignored.
135.Bl -tag -width ".Ev LD_LIBMAP_DISABLE"
136.It Ev LD_DUMP_REL_POST
137If set,
138.Nm
139will print a table containing all relocations after symbol
140binding and relocation.
141.It Ev LD_DUMP_REL_PRE
142If set,
143.Nm
144will print a table containing all relocations before symbol
145binding and relocation.
146.It Ev LD_LIBMAP
147A library replacement list in the same format as
148.Xr libmap.conf 5 .
149For convenience, the characters
150.Ql =
151and
152.Ql \&,
153can be used instead of a space and a newline.
154This variable is parsed after
155.Xr libmap.conf 5 ,
156and will override its entries.
157This variable is unset for set-user-ID and set-group-ID programs.
158.It Ev LD_LIBMAP_DISABLE
159If set, disables the use of
160.Xr libmap.conf 5
161and
162.Ev LD_LIBMAP .
163This variable is unset for set-user-ID and set-group-ID programs.
164.It Ev LD_ELF_HINTS_PATH
165This variable will override the default location of
166.Dq hints
167file.
168This variable is unset for set-user-ID and set-group-ID programs.
169.It Ev LD_LIBRARY_PATH
170A colon separated list of directories, overriding the default search path
171for shared libraries.
172This variable is unset for set-user-ID and set-group-ID programs.
173.It Ev LD_LIBRARY_PATH_RPATH
174If the variable is specified and has a value starting with
175any of \'y\', \'Y\' or \'1\' symbols, the path specified by
176.Ev LD_LIBRARY_PATH
177variable is allowed to override the path from
178.Dv DT_RPATH
179for binaries which does not contain
180.Dv DT_RUNPATH
181tag.
182For such binaries, when the variable
183.Ev LD_LIBRARY_PATH_RPATH
184is set,
185.Dq Fl z Ar nodefaultlib
186link-time option is ignored as well.
187.It Ev LD_PRELOAD
188A list of shared libraries, separated by colons and/or white space,
189to be linked in before any
190other shared libraries.
191If the directory is not specified then
192the directories specified by
193.Ev LD_LIBRARY_PATH
194will be searched first
195followed by the set of built-in standard directories.
196This variable is unset for set-user-ID and set-group-ID programs.
197.It Ev LD_LIBRARY_PATH_FDS
198A colon separated list of file descriptor numbers for library directories.
199This is intended for use within
200.Xr capsicum 4
201sandboxes, when global namespaces such as the filesystem are unavailable.
202It is consulted just after LD_LIBRARY_PATH.
203This variable is unset for set-user-ID and set-group-ID programs.
204.It Ev LD_BIND_NOT
205When set to a nonempty string, prevents modifications of the PLT slots when
206doing bindings.
207As result, each call of the PLT-resolved function is resolved.
208In combination with debug output, this provides complete account of
209all bind actions at runtime.
210This variable is unset for set-user-ID and set-group-ID programs.
211.It Ev LD_BIND_NOW
212When set to a nonempty string, causes
213.Nm
214to relocate all external function calls before starting execution of the
215program.
216Normally, function calls are bound lazily, at the first call
217of each function.
218.Ev LD_BIND_NOW
219increases the start-up time of a program, but it avoids run-time
220surprises caused by unexpectedly undefined functions.
221.It Ev LD_TRACE_LOADED_OBJECTS
222When set to a nonempty string, causes
223.Nm
224to exit after loading the shared objects and printing a summary which includes
225the absolute pathnames of all objects, to standard output.
226.It Ev LD_TRACE_LOADED_OBJECTS_ALL
227When set to a nonempty string, causes
228.Nm
229to expand the summary to indicate which objects caused each object to
230be loaded.
231.It Ev LD_TRACE_LOADED_OBJECTS_FMT1
232.It Ev LD_TRACE_LOADED_OBJECTS_FMT2
233When set, these variables are interpreted as format strings a la
234.Xr printf 3
235to customize the trace output and are used by
236.Xr ldd 1 Ns 's
237.Fl f
238option and allows
239.Xr ldd 1
240to be operated as a filter more conveniently.
241If the dependency name starts with string
242.Pa lib ,
243.Ev LD_TRACE_LOADED_OBJECTS_FMT1
244is used, otherwise
245.Ev LD_TRACE_LOADED_OBJECTS_FMT2
246is used.
247The following conversions can be used:
248.Bl -tag -width 4n
249.It Li %a
250The main program's name
251(also known as
252.Dq __progname ) .
253.It Li \&%A
254The value of the environment variable
255.Ev LD_TRACE_LOADED_OBJECTS_PROGNAME .
256Typically used to print both the names of programs and shared libraries
257being inspected using
258.Xr ldd 1 .
259.It Li %o
260The library name.
261.It Li %p
262The full pathname as determined by
263.Nm rtld Ns 's
264library search rules.
265.It Li %x
266The library's load address.
267.El
268.Pp
269Additionally,
270.Ql \en
271and
272.Ql \et
273are recognized and have their usual meaning.
274.It Ev LD_UTRACE
275If set,
276.Nm
277will log events such as the loading and unloading of shared objects via
278.Xr utrace 2 .
279.It Ev LD_LOADFLTR
280If set,
281.Nm
282will process the filtee dependencies of the loaded objects immediately,
283instead of postponing it until required.
284Normally, the filtees are opened at the time of the first symbol resolution
285from the filter object.
286.El
287.Sh DIRECT EXECUTION MODE
288.Nm
289is typically used implicitly, loaded by the kernel as requested by the
290.Dv PT_INTERP
291program header of the executed binary.
292.Fx
293also supports a direct execution mode for the dynamic linker.
294In this mode, the user explicitly executes
295.Nm
296and provides the path of the program to be linked and executed as
297an argument.
298This mode allows use of a non-standard dynamic linker for a program
299activation without changing the binary or without changing
300the installed dynamic linker.
301Execution options may be specified.
302.Pp
303The syntax of the direct invocation is
304.Bd -ragged -offset indent
305.Pa /libexec/ld-elf.so.1
306.Op Fl b Ar exe
307.Op Fl f Ar fd
308.Op Fl p
309.Op Fl u
310.Op Fl v
311.Op Fl -
312.Pa image_path
313.Op Ar image arguments
314.Ed
315.Pp
316The options are:
317.Bl -tag -width indent
318.It Fl b Ar exe
319Use the executable
320.Fa exe
321instead of
322.Fa image_path
323for activation.
324If this option is specified,
325.Ar image_path
326is only used to provide the
327.Va argv[0]
328value to the program.
329.It Fl f Ar fd
330File descriptor
331.Ar fd
332references the binary to be activated by
333.Nm .
334It must already be opened in the process when executing
335.Nm .
336If this option is specified,
337.Ar image_path
338is only used to provide the
339.Va argv[0]
340value to the program.
341.It Fl p
342If the
343.Pa image_path
344argument specifies a name which does not contain a slash
345.Dq Li /
346character,
347.Nm
348uses the search path provided by the environment variable
349.Dv PATH
350to find the binary to execute.
351.It Fl u
352Ignore all
353.Ev LD_
354environment variables that otherwise affect the dynamic
355linker behavior.
356.It Fl v
357Display information about this run-time linker binary, then exit.
358.It Fl -
359Ends the
360.Nm
361options.
362The argument following
363.Fl -
364is interpreted as the path of the binary to execute.
365.El
366.Pp
367In the direct execution mode,
368.Nm
369emulates verification of the binary execute permission for the
370current user.
371This is done to avoid breaking user expectations in naively restricted
372execution environments.
373The verification only uses Unix
374.Dv DACs ,
375ignores
376.Dv ACLs ,
377and is naturally prone to race conditions.
378Environments which rely on such restrictions are weak
379and breakable on their own.
380.Sh VERSIONING
381Newer
382.Nm
383might provide some features or changes in runtime behavior that cannot be
384easily detected at runtime by checking of the normal exported symbols.
385Note that it is almost always wrong to verify
386.Dv __FreeBSD_version
387in userspace to detect features, either at compile or at run time,
388because either kernel, or libc, or environment variables could not
389match the running
390.Nm .
391.Pp
392To solve the problem,
393.Nm
394exports some feature indicators in the
395.Fx
396private symbols namespace
397.Dv FBSDprivate_1.0 .
398Symbols start with the
399.Dv _rtld_version
400prefix.
401Current list of defined symbols and corresponding features is:
402.Bl -tag -width indent
403.It Dv _rtld_version__FreeBSD_version
404Symbol exports the value of the
405.Dv __FreeBSD_version
406definition as it was provided during the
407.Nm
408build.
409The symbol is always present since the
410.Dv _rtld_version
411facility was introduced.
412.It Dv _rtld_version_laddr_offset
413The
414.Va l_addr
415member of the
416.Vt link_map
417structure contains the load offset of the shared object.
418Before that,
419.Va l_addr
420contained the base address of the library.
421See
422.Xr dlinfo 3 .
423.Pp
424Also it indicates the presence of
425.Va l_refname
426member of the structure.
427.It Dv _rtld_version_dlpi_tls_data
428The
429.Va dlpi_tls_data
430member of the structure
431.Vt dl_phdr_info
432contains the address of the module TLS segment for the calling thread,
433and not the address of the initialization segment.
434.El
435.Sh FILES
436.Bl -tag -width ".Pa /var/run/ld-elf32.so.hints" -compact
437.It Pa /var/run/ld-elf.so.hints
438Hints file.
439.It Pa /var/run/ld-elf32.so.hints
440Hints file for 32-bit binaries on 64-bit system.
441.It Pa /etc/libmap.conf
442The libmap configuration file.
443.It Pa /etc/libmap32.conf
444The libmap configuration file for 32-bit binaries on 64-bit system.
445.El
446.Sh SEE ALSO
447.Xr ld 1 ,
448.Xr ldd 1 ,
449.Xr dlinfo 3 ,
450.Xr capsicum 4 ,
451.Xr elf 5 ,
452.Xr libmap.conf 5 ,
453.Xr ldconfig 8
454