debug: Fix clang open fortify wrapper (BZ 31927)
[glibc.git] / manual / dynlink.texi
blobd71f7a30d6f46808a135073acc8190cc4087e42c
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 @item aarch64.cpu_features.@dots{}
228 These items are specific to the AArch64 architectures.  They report data
229 @theglibc{} uses to activate conditionally supported features such as
230 BTI and MTE, and to select alternative function implementations.
232 @item aarch64.processor[@var{index}].@dots{}
233 These are additional items for the AArch64 architecture and are
234 described below.
236 @item aarch64.processor[@var{index}].requested=@var{kernel-cpu}
237 The kernel is told to run the subsequent probing on the CPU numbered
238 @var{kernel-cpu}.  The values @var{kernel-cpu} and @var{index} can be
239 distinct if there are gaps in the process CPU affinity mask.  This line
240 is not included if CPU affinity mask information is not available.
242 @item aarch64.processor[@var{index}].observed=@var{kernel-cpu}
243 This line reports the kernel CPU number @var{kernel-cpu} on which the
244 probing code initially ran.  If the CPU number cannot be obtained,
245 this line is not printed.
247 @item aarch64.processor[@var{index}].observed_node=@var{node}
248 This reports the observed NUMA node number, as reported by the
249 @code{getcpu} system call.  If this information cannot be obtained, this
250 line is not printed.
252 @item aarch64.processor[@var{index}].midr_el1=@var{value}
253 The value of the @code{midr_el1} system register on the processor
254 @var{index}.  This line is only printed if the kernel indicates that
255 this system register is supported.
257 @item aarch64.processor[@var{index}].dczid_el0=@var{value}
258 The value of the @code{dczid_el0} system register on the processor
259 @var{index}.
261 @cindex CPUID (diagnostics)
262 @item x86.cpu_features.@dots{}
263 These items are specific to the i386 and x86-64 architectures.  They
264 reflect supported CPU features and information on cache geometry, mostly
265 collected using the CPUID instruction.
267 @item x86.processor[@var{index}].@dots{}
268 These are additional items for the i386 and x86-64 architectures, as
269 described below.  They mostly contain raw data from the CPUID
270 instruction.  The probes are performed for each active CPU for the
271 @code{ld.so} process, and data for different probed CPUs receives a
272 uniqe @var{index} value.  Some CPUID data is expected to differ from CPU
273 core to CPU core.  In some cases, CPUs are not correctly initialized and
274 indicate the presence of different feature sets.
276 @item x86.processor[@var{index}].requested=@var{kernel-cpu}
277 The kernel is told to run the subsequent probing on the CPU numbered
278 @var{kernel-cpu}.  The values @var{kernel-cpu} and @var{index} can be
279 distinct if there are gaps in the process CPU affinity mask.  This line
280 is not included if CPU affinity mask information is not available.
282 @item x86.processor[@var{index}].observed=@var{kernel-cpu}
283 This line reports the kernel CPU number @var{kernel-cpu} on which the
284 probing code initially ran.  If the CPU number cannot be obtained,
285 this line is not printed.
287 @item x86.processor[@var{index}].observed_node=@var{node}
288 This reports the observed NUMA node number, as reported by the
289 @code{getcpu} system call.  If this information cannot be obtained, this
290 line is not printed.
292 @item x86.processor[@var{index}].cpuid_leaves=@var{count}
293 This line indicates that @var{count} distinct CPUID leaves were
294 encountered.  (This reflects internal @code{ld.so} storage space, it
295 does not directly correspond to @code{CPUID} enumeration ranges.)
297 @item x86.processor[@var{index}].ecx_limit=@var{value}
298 The CPUID data extraction code uses a brute-force approach to enumerate
299 subleaves (see the @samp{.subleaf_eax} lines below).  The last
300 @code{%rcx} value used in a CPUID query on this probed CPU was
301 @var{value}.
303 @item x86.processor[@var{index}].cpuid.eax[@var{query_eax}].eax=@var{eax}
304 @itemx x86.processor[@var{index}].cpuid.eax[@var{query_eax}].ebx=@var{ebx}
305 @itemx x86.processor[@var{index}].cpuid.eax[@var{query_eax}].ecx=@var{ecx}
306 @itemx x86.processor[@var{index}].cpuid.eax[@var{query_eax}].edx=@var{edx}
307 These lines report the register contents after executing the CPUID
308 instruction with @samp{%rax == @var{query_eax}} and @samp{%rcx == 0} (a
309 @dfn{leaf}).  For the first probed CPU (with a zero @var{index}), only
310 leaves with non-zero register contents are reported.  For subsequent
311 CPUs, only leaves whose register contents differs from the previously
312 probed CPUs (with @var{index} one less) are reported.
314 Basic and extended leaves are reported using the same syntax.  This
315 means there is a large jump in @var{query_eax} for the first reported
316 extended leaf.
318 @item x86.processor[@var{index}].cpuid.subleaf_eax[@var{query_eax}].ecx[@var{query_ecx}].eax=@var{eax}
319 @itemx x86.processor[@var{index}].cpuid.subleaf_eax[@var{query_eax}].ecx[@var{query_ecx}].ebx=@var{ebx}
320 @itemx x86.processor[@var{index}].cpuid.subleaf_eax[@var{query_eax}].ecx[@var{query_ecx}].ecx=@var{ecx}
321 @itemx x86.processor[@var{index}].cpuid.subleaf_eax[@var{query_eax}].ecx[@var{query_ecx}].edx=@var{edx}
322 This is similar to the leaves above, but for a @dfn{subleaf}.  For
323 subleaves, the CPUID instruction is executed with @samp{%rax ==
324 @var{query_eax}} and @samp{%rcx == @var{query_ecx}}, so the result
325 depends on both register values.  The same rules about filtering zero
326 and identical results apply.
328 @item x86.processor[@var{index}].cpuid.subleaf_eax[@var{query_eax}].ecx[@var{query_ecx}].until_ecx=@var{ecx_limit}
329 Some CPUID results are the same regardless the @var{query_ecx} value.
330 If this situation is detected, a line with the @samp{.until_ecx}
331 selector ins included, and this indicates that the CPUID register
332 contents is the same for @code{%rcx} values between @var{query_ecx}
333 and @var{ecx_limit} (inclusive).
335 @item x86.processor[@var{index}].cpuid.subleaf_eax[@var{query_eax}].ecx[@var{query_ecx}].ecx_query_mask=0xff
336 This line indicates that in an @samp{.until_ecx} range, the CPUID
337 instruction preserved the lowested 8 bits of the input @code{%rcx} in
338 the output @code{%rcx} registers.  Otherwise, the subleaves in the range
339 have identical values.  This special treatment is necessary to report
340 compact range information in case such copying occurs (because the
341 subleaves would otherwise be all different).
343 @item x86.processor[@var{index}].xgetbv.ecx[@var{query_ecx}]=@var{result}
344 This line shows the 64-bit @var{result} value in the @code{%rdx:%rax}
345 register pair after executing the XGETBV instruction with @code{%rcx}
346 set to @var{query_ecx}.  Zero values and values matching the previously
347 probed CPU are omitted.  Nothing is printed if the system does not
348 support the XGETBV instruction.
349 @end table
351 @node Dynamic Linker Introspection
352 @section Dynamic Linker Introspection
354 @Theglibc{} provides various functions for querying information from the
355 dynamic linker.
357 @deftypefun {int} dlinfo (void *@var{handle}, int @var{request}, void *@var{arg})
358 @safety{@mtsafe{}@asunsafe{@asucorrupt{}}@acunsafe{@acucorrupt{}}}
359 @standards{GNU, dlfcn.h}
360 This function returns information about @var{handle} in the memory
361 location @var{arg}, based on @var{request}.  The @var{handle} argument
362 must be a pointer returned by @code{dlopen} or @code{dlmopen}; it must
363 not have been closed by @code{dlclose}.
365 On success, @code{dlinfo} returns 0 for most request types; exceptions
366 are noted below.  If there is an error, the function returns @math{-1},
367 and @code{dlerror} can be used to obtain a corresponding error message.
369 The following operations are defined for use with @var{request}:
371 @vtable @code
372 @item RTLD_DI_LINKMAP
373 The corresponding @code{struct link_map} pointer for @var{handle} is
374 written to @code{*@var{arg}}.  The @var{arg} argument must be the
375 address of an object of type @code{struct link_map *}.
377 @item RTLD_DI_LMID
378 The namespace identifier of @var{handle} is written to
379 @code{*@var{arg}}.  The @var{arg} argument must be the address of an
380 object of type @code{Lmid_t}.
382 @item RTLD_DI_ORIGIN
383 The value of the @code{$ORIGIN} dynamic string token for @var{handle} is
384 written to the character array starting at @var{arg} as a
385 null-terminated string.
387 This request type should not be used because it is prone to buffer
388 overflows.
390 @item RTLD_DI_SERINFO
391 @itemx RTLD_DI_SERINFOSIZE
392 These requests can be used to obtain search path information for
393 @var{handle}.  For both requests, @var{arg} must point to a
394 @code{Dl_serinfo} object.  The @code{RTLD_DI_SERINFOSIZE} request must
395 be made first; it updates the @code{dls_size} and @code{dls_cnt} members
396 of the @code{Dl_serinfo} object.  The caller should then allocate memory
397 to store at least @code{dls_size} bytes and pass that buffer to a
398 @code{RTLD_DI_SERINFO} request.  This second request fills the
399 @code{dls_serpath} array.  The number of array elements was returned in
400 the @code{dls_cnt} member in the initial @code{RTLD_DI_SERINFOSIZE}
401 request.  The caller is responsible for freeing the allocated buffer.
403 This interface is prone to buffer overflows in multi-threaded processes
404 because the required size can change between the
405 @code{RTLD_DI_SERINFOSIZE} and @code{RTLD_DI_SERINFO} requests.
407 @item RTLD_DI_TLS_DATA
408 This request writes the address of the TLS block (in the current thread)
409 for the shared object identified by @var{handle} to @code{*@var{arg}}.
410 The argument @var{arg} must be the address of an object of type
411 @code{void *}.  A null pointer is written if the object does not have
412 any associated TLS block.
414 @item RTLD_DI_TLS_MODID
415 This request writes the TLS module ID for the shared object @var{handle}
416 to @code{*@var{arg}}.  The argument @var{arg} must be the address of an
417 object of type @code{size_t}.  The module ID is zero if the object
418 does not have an associated TLS block.
420 @item RTLD_DI_PHDR
421 This request writes the address of the program header array to
422 @code{*@var{arg}}.  The argument @var{arg} must be the address of an
423 object of type @code{const ElfW(Phdr) *} (that is,
424 @code{const Elf32_Phdr *} or @code{const Elf64_Phdr *}, as appropriate
425 for the current architecture).  For this request, the value returned by
426 @code{dlinfo} is the number of program headers in the program header
427 array.
428 @end vtable
430 The @code{dlinfo} function is a GNU extension.
431 @end deftypefun
433 The remainder of this section documents the @code{_dl_find_object}
434 function and supporting types and constants.
436 @deftp {Data Type} {struct dl_find_object}
437 @standards{GNU, dlfcn.h}
438 This structure contains information about a main program or loaded
439 object.  The @code{_dl_find_object} function uses it to return
440 result data to the caller.
442 @table @code
443 @item unsigned long long int dlfo_flags
444 Currently unused and always 0.
446 @item void *dlfo_map_start
447 The start address of the inspected mapping.  This information comes from
448 the program header, so it follows its convention, and the address is not
449 necessarily page-aligned.
451 @item void *dlfo_map_end
452 The end address of the mapping.
454 @item struct link_map *dlfo_link_map
455 This member contains a pointer to the link map of the object.
457 @item void *dlfo_eh_frame
458 This member contains a pointer to the exception handling data of the
459 object.  See @code{DLFO_EH_SEGMENT_TYPE} below.
461 @end table
463 This structure is a GNU extension.
464 @end deftp
466 @deftypevr Macro int DLFO_STRUCT_HAS_EH_DBASE
467 @standards{GNU, dlfcn.h}
468 On most targets, this macro is defined as @code{0}.  If it is defined to
469 @code{1}, @code{struct dl_find_object} contains an additional member
470 @code{dlfo_eh_dbase} of type @code{void *}.  It is the base address for
471 @code{DW_EH_PE_datarel} DWARF encodings to this location.
473 This macro is a GNU extension.
474 @end deftypevr
476 @deftypevr Macro int DLFO_STRUCT_HAS_EH_COUNT
477 @standards{GNU, dlfcn.h}
478 On most targets, this macro is defined as @code{0}.  If it is defined to
479 @code{1}, @code{struct dl_find_object} contains an additional member
480 @code{dlfo_eh_count} of type @code{int}.  It is the number of exception
481 handling entries in the EH frame segment identified by the
482 @code{dlfo_eh_frame} member.
484 This macro is a GNU extension.
485 @end deftypevr
487 @deftypevr Macro int DLFO_EH_SEGMENT_TYPE
488 @standards{GNU, dlfcn.h}
489 On targets using DWARF-based exception unwinding, this macro expands to
490 @code{PT_GNU_EH_FRAME}.  This indicates that @code{dlfo_eh_frame} in
491 @code{struct dl_find_object} points to the @code{PT_GNU_EH_FRAME}
492 segment of the object.  On targets that use other unwinding formats, the
493 macro expands to the program header type for the unwinding data.
495 This macro is a GNU extension.
496 @end deftypevr
498 @deftypefun {int} _dl_find_object (void *@var{address}, struct dl_find_object *@var{result})
499 @standards{GNU, dlfcn.h}
500 @safety{@mtsafe{}@assafe{}@acsafe{}}
501 On success, this function returns 0 and writes about the object
502 surrounding the address to @code{*@var{result}}.  On failure, -1 is
503 returned.
505 The @var{address} can be a code address or data address.  On
506 architectures using function descriptors, no attempt is made to decode
507 the function descriptor.  Depending on how these descriptors are
508 implemented, @code{_dl_find_object} may return the object that defines
509 the function descriptor (and not the object that contains the code
510 implementing the function), or fail to find any object at all.
512 On success @var{address} is greater than or equal to
513 @code{@var{result}->dlfo_map_start} and less than
514 @code{@var{result}->dlfo_map_end}, that is, the supplied code address is
515 located within the reported mapping.
517 This function returns a pointer to the unwinding information for the
518 object that contains the program code @var{address} in
519 @code{@var{result}->dlfo_eh_frame}.  If the platform uses DWARF
520 unwinding information, this is the in-memory address of the
521 @code{PT_GNU_EH_FRAME} segment.  See @code{DLFO_EH_SEGMENT_TYPE} above.
522 In case @var{address} resides in an object that lacks unwinding information,
523 the function still returns 0, but sets @code{@var{result}->dlfo_eh_frame}
524 to a null pointer.
526 @code{_dl_find_object} itself is thread-safe.  However, if the
527 application invokes @code{dlclose} for the object that contains
528 @var{address} concurrently with @code{_dl_find_object} or after the call
529 returns, accessing the unwinding data for that object or the link map
530 (through @code{@var{result}->dlfo_link_map}) is not safe.  Therefore, the
531 application needs to ensure by other means (e.g., by convention) that
532 @var{address} remains a valid code address while the unwinding
533 information is processed.
535 This function is a GNU extension.
536 @end deftypefun
539 @c FIXME these are undocumented:
540 @c dladdr
541 @c dladdr1
542 @c dlclose
543 @c dlerror
544 @c dlmopen
545 @c dlopen
546 @c dlsym
547 @c dlvsym