string: Use builtins for ffs and ffsll
[glibc.git] / manual / dynlink.texi
blob06a6c15533bfcb88be4c3266db383fa3254b1037
1 @node Dynamic Linker
2 @c @node Dynamic Linker, Internal Probes, Threads, Top
3 @c %MENU% Loading programs and shared objects.
4 @chapter Dynamic Linker
5 @cindex dynamic linker
6 @cindex dynamic loader
8 The @dfn{dynamic linker} is responsible for loading dynamically linked
9 programs and their dependencies (in the form of shared objects).  The
10 dynamic linker in @theglibc{} also supports loading shared objects (such
11 as plugins) later at run time.
13 Dynamic linkers are sometimes called @dfn{dynamic loaders}.
15 @menu
16 * Dynamic Linker Invocation::   Explicit invocation of the dynamic linker.
17 * Dynamic Linker Introspection::    Interfaces for querying mapping information.
18 @end menu
20 @node Dynamic Linker Invocation
21 @section Dynamic Linker Invocation
23 @cindex program interpreter
24 When a dynamically linked program starts, the operating system
25 automatically loads the dynamic linker along with the program.
26 @Theglibc{} also supports invoking the dynamic linker explicitly to
27 launch a program.  This command uses the implied dynamic linker
28 (also sometimes called the @dfn{program interpreter}):
30 @smallexample
31 sh -c 'echo "Hello, world!"'
32 @end smallexample
34 This command specifies the dynamic linker explicitly:
36 @smallexample
37 ld.so /bin/sh -c 'echo "Hello, world!"'
38 @end smallexample
40 Note that @command{ld.so} does not search the @env{PATH} environment
41 variable, so the full file name of the executable needs to be specified.
43 The @command{ld.so} program supports various options.  Options start
44 @samp{--} and need to come before the program that is being launched.
45 Some of the supported options are listed below.
47 @table @code
48 @item --list-diagnostics
49 Print system diagnostic information in a machine-readable format.
50 @xref{Dynamic Linker Diagnostics}.
51 @end table
53 @menu
54 * Dynamic Linker Diagnostics::   Obtaining system diagnostic information.
55 @end menu
57 @node Dynamic Linker Diagnostics
58 @subsection Dynamic Linker Diagnostics
59 @cindex diagnostics (dynamic linker)
61 The @samp{ld.so --list-diagnostics} produces machine-readable
62 diagnostics output.  This output contains system data that affects the
63 behavior of @theglibc{}, and potentially application behavior as well.
65 The exact set of diagnostic items can change between releases of
66 @theglibc{}.  The output format itself is not expected to change
67 radically.
69 The following table shows some example lines that can be written by the
70 diagnostics command.
72 @table @code
73 @item dl_pagesize=0x1000
74 The system page size is 4096 bytes.
76 @item env[0x14]="LANG=en_US.UTF-8"
77 This item indicates that the 21st environment variable at process
78 startup contains a setting for @code{LANG}.
80 @item env_filtered[0x22]="DISPLAY"
81 The 35th environment variable is @code{DISPLAY}.  Its value is not
82 included in the output for privacy reasons because it is not recognized
83 as harmless by the diagnostics code.
85 @item path.prefix="/usr"
86 This means that @theglibc{} was configured with @code{--prefix=/usr}.
88 @item path.system_dirs[0x0]="/lib64/"
89 @itemx path.system_dirs[0x1]="/usr/lib64/"
90 The built-in dynamic linker search path contains two directories,
91 @code{/lib64} and @code{/usr/lib64}.
92 @end table
94 @menu
95 * Dynamic Linker Diagnostics Format::  Format of ld.so output.
96 * Dynamic Linker Diagnostics Values::  Data contain in ld.so output.
97 @end menu
99 @node Dynamic Linker Diagnostics Format
100 @subsubsection Dynamic Linker Diagnostics Format
102 As seen above, diagnostic lines assign values (integers or strings) to a
103 sequence of labeled subscripts, separated by @samp{.}.  Some subscripts
104 have integer indices associated with them.  The subscript indices are
105 not necessarily contiguous or small, so an associative array should be
106 used to store them.  Currently, all integers fit into the 64-bit
107 unsigned integer range.  Every access path to a value has a fixed type
108 (string or integer) independent of subscript index values.  Likewise,
109 whether a subscript is indexed does not depend on previous indices (but
110 may depend on previous subscript labels).
112 A syntax description in ABNF (RFC 5234) follows.  Note that
113 @code{%x30-39} denotes the range of decimal digits.  Diagnostic output
114 lines are expected to match the @code{line} production.
116 @c ABNF-START
117 @smallexample
118 HEXDIG = %x30-39 / %x61-6f ; lowercase a-f only
119 ALPHA = %x41-5a / %x61-7a / %x7f ; letters and underscore
120 ALPHA-NUMERIC = ALPHA / %x30-39 / "_"
121 DQUOTE = %x22 ; "
123 ; Numbers are always hexadecimal and use a 0x prefix.
124 hex-value-prefix = %x30 %x78
125 hex-value = hex-value-prefix 1*HEXDIG
127 ; Strings use octal escape sequences and \\, \".
128 string-char = %x20-21 / %x23-5c / %x5d-7e ; printable but not "\
129 string-quoted-octal = %x30-33 2*2%x30-37
130 string-quoted = "\" ("\" / DQUOTE / string-quoted-octal)
131 string-value = DQUOTE *(string-char / string-quoted) DQUOTE
133 value = hex-value / string-value
135 label = ALPHA *ALPHA-NUMERIC
136 index = "[" hex-value "]"
137 subscript = label [index]
139 line = subscript *("." subscript) "=" value
140 @end smallexample
142 @node Dynamic Linker Diagnostics Values
143 @subsubsection Dynamic Linker Diagnostics Values
145 As mentioned above, the set of diagnostics may change between
146 @theglibc{} releases.  Nevertheless, the following table documents a few
147 common diagnostic items.  All numbers are in hexadecimal, with a
148 @samp{0x} prefix.
150 @table @code
151 @item dl_dst_lib=@var{string}
152 The @code{$LIB} dynamic string token expands to @var{string}.
154 @cindex HWCAP (diagnostics)
155 @item dl_hwcap=@var{integer}
156 @itemx dl_hwcap2=@var{integer}
157 The HWCAP and HWCAP2 values, as returned for @code{getauxval}, and as
158 used in other places depending on the architecture.
160 @cindex page size (diagnostics)
161 @item dl_pagesize=@var{integer}
162 The system page size is @var{integer} bytes.
164 @item dl_platform=@var{string}
165 The @code{$PLATFORM} dynamic string token expands to @var{string}.
167 @item dso.libc=@var{string}
168 This is the soname of the shared @code{libc} object that is part of
169 @theglibc{}.  On most architectures, this is @code{libc.so.6}.
171 @item env[@var{index}]=@var{string}
172 @itemx env_filtered[@var{index}]=@var{string}
173 An environment variable from the process environment.  The integer
174 @var{index} is the array index in the environment array.  Variables
175 under @code{env} include the variable value after the @samp{=} (assuming
176 that it was present), variables under @code{env_filtered} do not.
178 @item path.prefix=@var{string}
179 This indicates that @theglibc{} was configured using
180 @samp{--prefix=@var{string}}.
182 @item path.sysconfdir=@var{string}
183 @Theglibc{} was configured (perhaps implicitly) with
184 @samp{--sysconfdir=@var{string}} (typically @code{/etc}).
186 @item path.system_dirs[@var{index}]=@var{string}
187 These items list the elements of the built-in array that describes the
188 default library search path.  The value @var{string} is a directory file
189 name with a trailing @samp{/}.
191 @item path.rtld=@var{string}
192 This string indicates the application binary interface (ABI) file name
193 of the run-time dynamic linker.
195 @item version.release="stable"
196 @itemx version.release="development"
197 The value @code{"stable"} indicates that this build of @theglibc{} is
198 from a release branch.  Releases labeled as @code{"development"} are
199 unreleased development versions.
201 @cindex version (diagnostics)
202 @item version.version="@var{major}.@var{minor}"
203 @itemx version.version="@var{major}.@var{minor}.9000"
204 @Theglibc{} version.  Development releases end in @samp{.9000}.
206 @cindex auxiliary vector (diagnostics)
207 @item auxv[@var{index}].a_type=@var{type}
208 @itemx auxv[@var{index}].a_val=@var{integer}
209 @itemx auxv[@var{index}].a_val_string=@var{string}
210 An entry in the auxiliary vector (specific to Linux).  The values
211 @var{type} (an integer) and @var{integer} correspond to the members of
212 @code{struct auxv}.  If the value is a string, @code{a_val_string} is
213 used instead of @code{a_val}, so that values have consistent types.
215 The @code{AT_HWCAP} and @code{AT_HWCAP2} values in this output do not
216 reflect adjustment by @theglibc{}.
218 @item uname.sysname=@var{string}
219 @itemx uname.nodename=@var{string}
220 @itemx uname.release=@var{string}
221 @itemx uname.version=@var{string}
222 @itemx uname.machine=@var{string}
223 @itemx uname.domain=@var{string}
224 These Linux-specific items show the values of @code{struct utsname}, as
225 reported by the @code{uname} function.  @xref{Platform Type}.
227 @cindex CPUID (diagnostics)
228 @item x86.cpu_features.@dots{}
229 These items are specific to the i386 and x86-64 architectures.  They
230 reflect supported CPU features and information on cache geometry, mostly
231 collected using the @code{CPUID} instruction.
232 @end table
234 @node Dynamic Linker Introspection
235 @section Dynamic Linker Introspection
237 @Theglibc{} provides various functions for querying information from the
238 dynamic linker.
240 @deftypefun {int} dlinfo (void *@var{handle}, int @var{request}, void *@var{arg})
241 @safety{@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
242 @standards{GNU, dlfcn.h}
243 This function returns information about @var{handle} in the memory
244 location @var{arg}, based on @var{request}.  The @var{handle} argument
245 must be a pointer returned by @code{dlopen} or @code{dlmopen}; it must
246 not have been closed by @code{dlclose}.
248 On success, @code{dlinfo} returns 0 for most request types; exceptions
249 are noted below.  If there is an error, the function returns @math{-1},
250 and @code{dlerror} can be used to obtain a corresponding error message.
252 The following operations are defined for use with @var{request}:
254 @vtable @code
255 @item RTLD_DI_LINKMAP
256 The corresponding @code{struct link_map} pointer for @var{handle} is
257 written to @code{*@var{arg}}.  The @var{arg} argument must be the
258 address of an object of type @code{struct link_map *}.
260 @item RTLD_DI_LMID
261 The namespace identifier of @var{handle} is written to
262 @code{*@var{arg}}.  The @var{arg} argument must be the address of an
263 object of type @code{Lmid_t}.
265 @item RTLD_DI_ORIGIN
266 The value of the @code{$ORIGIN} dynamic string token for @var{handle} is
267 written to the character array starting at @var{arg} as a
268 null-terminated string.
270 This request type should not be used because it is prone to buffer
271 overflows.
273 @item RTLD_DI_SERINFO
274 @itemx RTLD_DI_SERINFOSIZE
275 These requests can be used to obtain search path information for
276 @var{handle}.  For both requests, @var{arg} must point to a
277 @code{Dl_serinfo} object.  The @code{RTLD_DI_SERINFOSIZE} request must
278 be made first; it updates the @code{dls_size} and @code{dls_cnt} members
279 of the @code{Dl_serinfo} object.  The caller should then allocate memory
280 to store at least @code{dls_size} bytes and pass that buffer to a
281 @code{RTLD_DI_SERINFO} request.  This second request fills the
282 @code{dls_serpath} array.  The number of array elements was returned in
283 the @code{dls_cnt} member in the initial @code{RTLD_DI_SERINFOSIZE}
284 request.  The caller is responsible for freeing the allocated buffer.
286 This interface is prone to buffer overflows in multi-threaded processes
287 because the required size can change between the
288 @code{RTLD_DI_SERINFOSIZE} and @code{RTLD_DI_SERINFO} requests.
290 @item RTLD_DI_TLS_DATA
291 This request writes the address of the TLS block (in the current thread)
292 for the shared object identified by @var{handle} to @code{*@var{arg}}.
293 The argument @var{arg} must be the address of an object of type
294 @code{void *}.  A null pointer is written if the object does not have
295 any associated TLS block.
297 @item RTLD_DI_TLS_MODID
298 This request writes the TLS module ID for the shared object @var{handle}
299 to @code{*@var{arg}}.  The argument @var{arg} must be the address of an
300 object of type @code{size_t}.  The module ID is zero if the object
301 does not have an associated TLS block.
303 @item RTLD_DI_PHDR
304 This request writes the address of the program header array to
305 @code{*@var{arg}}.  The argument @var{arg} must be the address of an
306 object of type @code{const ElfW(Phdr) *} (that is,
307 @code{const Elf32_Phdr *} or @code{const Elf64_Phdr *}, as appropriate
308 for the current architecture).  For this request, the value returned by
309 @code{dlinfo} is the number of program headers in the program header
310 array.
311 @end vtable
313 The @code{dlinfo} function is a GNU extension.
314 @end deftypefun
316 The remainder of this section documents the @code{_dl_find_object}
317 function and supporting types and constants.
319 @deftp {Data Type} {struct dl_find_object}
320 @standards{GNU, dlfcn.h}
321 This structure contains information about a main program or loaded
322 object.  The @code{_dl_find_object} function uses it to return
323 result data to the caller.
325 @table @code
326 @item unsigned long long int dlfo_flags
327 Currently unused and always 0.
329 @item void *dlfo_map_start
330 The start address of the inspected mapping.  This information comes from
331 the program header, so it follows its convention, and the address is not
332 necessarily page-aligned.
334 @item void *dlfo_map_end
335 The end address of the mapping.
337 @item struct link_map *dlfo_link_map
338 This member contains a pointer to the link map of the object.
340 @item void *dlfo_eh_frame
341 This member contains a pointer to the exception handling data of the
342 object.  See @code{DLFO_EH_SEGMENT_TYPE} below.
344 @end table
346 This structure is a GNU extension.
347 @end deftp
349 @deftypevr Macro int DLFO_STRUCT_HAS_EH_DBASE
350 @standards{GNU, dlfcn.h}
351 On most targets, this macro is defined as @code{0}.  If it is defined to
352 @code{1}, @code{struct dl_find_object} contains an additional member
353 @code{dlfo_eh_dbase} of type @code{void *}.  It is the base address for
354 @code{DW_EH_PE_datarel} DWARF encodings to this location.
356 This macro is a GNU extension.
357 @end deftypevr
359 @deftypevr Macro int DLFO_STRUCT_HAS_EH_COUNT
360 @standards{GNU, dlfcn.h}
361 On most targets, this macro is defined as @code{0}.  If it is defined to
362 @code{1}, @code{struct dl_find_object} contains an additional member
363 @code{dlfo_eh_count} of type @code{int}.  It is the number of exception
364 handling entries in the EH frame segment identified by the
365 @code{dlfo_eh_frame} member.
367 This macro is a GNU extension.
368 @end deftypevr
370 @deftypevr Macro int DLFO_EH_SEGMENT_TYPE
371 @standards{GNU, dlfcn.h}
372 On targets using DWARF-based exception unwinding, this macro expands to
373 @code{PT_GNU_EH_FRAME}.  This indicates that @code{dlfo_eh_frame} in
374 @code{struct dl_find_object} points to the @code{PT_GNU_EH_FRAME}
375 segment of the object.  On targets that use other unwinding formats, the
376 macro expands to the program header type for the unwinding data.
378 This macro is a GNU extension.
379 @end deftypevr
381 @deftypefun {int} _dl_find_object (void *@var{address}, struct dl_find_object *@var{result})
382 @standards{GNU, dlfcn.h}
383 @safety{@mtsafe{}@assafe{}@acsafe{}}
384 On success, this function returns 0 and writes about the object
385 surrounding the address to @code{*@var{result}}.  On failure, -1 is
386 returned.
388 The @var{address} can be a code address or data address.  On
389 architectures using function descriptors, no attempt is made to decode
390 the function descriptor.  Depending on how these descriptors are
391 implemented, @code{_dl_find_object} may return the object that defines
392 the function descriptor (and not the object that contains the code
393 implementing the function), or fail to find any object at all.
395 On success @var{address} is greater than or equal to
396 @code{@var{result}->dlfo_map_start} and less than
397 @code{@var{result}->dlfo_map_end}, that is, the supplied code address is
398 located within the reported mapping.
400 This function returns a pointer to the unwinding information for the
401 object that contains the program code @var{address} in
402 @code{@var{result}->dlfo_eh_frame}.  If the platform uses DWARF
403 unwinding information, this is the in-memory address of the
404 @code{PT_GNU_EH_FRAME} segment.  See @code{DLFO_EH_SEGMENT_TYPE} above.
405 In case @var{address} resides in an object that lacks unwinding information,
406 the function still returns 0, but sets @code{@var{result}->dlfo_eh_frame}
407 to a null pointer.
409 @code{_dl_find_object} itself is thread-safe.  However, if the
410 application invokes @code{dlclose} for the object that contains
411 @var{address} concurrently with @code{_dl_find_object} or after the call
412 returns, accessing the unwinding data for that object or the link map
413 (through @code{@var{result}->dlfo_link_map}) is not safe.  Therefore, the
414 application needs to ensure by other means (e.g., by convention) that
415 @var{address} remains a valid code address while the unwinding
416 information is processed.
418 This function is a GNU extension.
419 @end deftypefun
422 @c FIXME these are undocumented:
423 @c dladdr
424 @c dladdr1
425 @c dlclose
426 @c dlerror
427 @c dlmopen
428 @c dlopen
429 @c dlsym
430 @c dlvsym