1 .\" $NetBSD: link.5,v 1.21 2007/03/07 00:41:17 dogcow Exp $
3 .\" Copyright (c) 1996 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by Paul Kranenburg.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
35 .Nd dynamic loader and link editor interface
41 declares several structures that are present in dynamically linked
42 programs and libraries.
43 The structures define the interface between several components of the
44 link-editor and loader mechanism.
45 The layout of a number of these structures within the binaries resembles the
47 format in many places as it serves such similar functions as symbol
48 definitions (including the accompanying string table) and relocation records
49 needed to resolve references to external entities.
51 It also records a number of data structures
52 unique to the dynamic loading and linking process.
53 These include references to other objects that are required to
54 complete the link-editing process and indirection tables to facilitate
55 .Em Position Independent Code
56 (PIC) to improve sharing of code pages among different processes.
58 The collection of data structures described here will be referred to as the
59 .Em Run-time Relocation Section
60 (RRS) and is embedded in the standard text and data segments of
61 the dynamically linked program or shared object image as the existing
63 format offers no room for it elsewhere.
65 Several utilities cooperate to ensure that the task of getting a program
66 ready to run can complete successfully in a way that optimizes the use
68 The compiler emits PIC code from which shared libraries can be built by
70 The compiler also includes size information of any initialized data items
71 through the .size assembler directive.
73 PIC code differs from conventional code in that it accesses data
74 variables through an indirection table, the Global Offset Table,
75 by convention accessible by the reserved name
76 .Em _GLOBAL_OFFSET_TABLE_ .
77 The exact mechanism used for this is machine dependent, usually a machine
78 register is reserved for the purpose.
79 The rational behind this construct is to generate code that is
80 independent of the actual load address.
81 Only the values contained in the Global Offset Table may need
82 updating at run-time depending on the load addresses of the various
83 shared objects in the address space.
85 Likewise, procedure calls to globally defined functions are redirected
86 through the Procedure Linkage Table (PLT) residing in the data
87 segment of the core image.
88 Again, this is done to avoid run-time modifications to the text segment.
90 The linker-editor allocates the Global Offset Table and Procedure
91 Linkage Table when combining PIC object files into an image suitable
92 for mapping into the process address space.
93 It also collects all symbols that may be needed by the run-time
94 link-editor and stores these along with the image's text and data bits.
95 Another reserved symbol,
97 is used to indicate the presence of the run-time linker structures.
100 is relocated to 0, there is no need to invoke the run-time link-editor.
101 If this symbol is non-zero, it points at a data structure from
102 which the location of the necessary relocation- and symbol information
104 This is most notably used by the start-up module,
106 The _DYNAMIC structure is conventionally located at the start of the data
107 segment of the image to which it pertains.
109 The data structures supporting dynamic linking and run-time relocation
110 reside both in the text and data segments of the image they apply to.
111 The text segments contain read-only data such as symbols descriptions and
112 names, while the data segments contain the tables that need to be modified by
113 during the relocation process.
115 The _DYNAMIC symbol references a
118 .Bd -literal -offset indent
121 struct so_debug *d_debug;
123 struct section_dispatch_table *d_sdt;
125 struct ld_entry *d_entry;
128 .Bl -tag -width d_version
130 This field provides for different versions of the dynamic linking
132 The current version numbers understood by ld and ld.so are
133 .Em LD_VERSION_SUN (3) ,
137 .Em LD_VERSION_BSD (8) ,
138 which is currently in use by
143 dependent data structure.
145 this field provides debuggers with a hook to access symbol tables of shared
146 objects loaded as a result of the actions of the run-time link-editor.
148 this field is obsoleted by CRT interface version CRT_VERSION_BSD4, and is
149 replaced by the crt_ldentry in
154 .Fa section_dispatch_table
155 structure is the main
157 table, containing offsets into the image's segments where various symbol
158 and relocation information is located.
159 .Bd -literal -offset indent
160 struct section_dispatch_table {
161 struct so_map *sdt_loaded;
178 .Bl -tag -width sdt_loaded
180 A pointer to the first link map loaded (see below).
183 for the benefit of debuggers that may use it to load a shared object's
186 The start of a (linked) list of shared object descriptors needed by
190 Library search rules.
191 A colon separated list of directories corresponding to the
196 The location of the Global Offset Table within this image.
198 The location of the Procedure Linkage Table within this image.
200 The location of an array of
207 specifying run-time relocations.
209 The location of the hash table for fast symbol lookup in this object's
212 The location of the symbol table.
216 The number of buckets in
219 The location of the symbol string table that goes with
222 The size of the string table.
224 The size of the object's text segment.
226 The size of the Procedure Linkage Table.
231 structure describes a shared object that is needed
232 to complete the link edit process of the object containing it.
233 A list of such objects
241 in the section_dispatch_table structure.
242 .Bd -literal -offset indent
245 u_int sod_library : 1,
253 .Bl -tag -width sod_library
255 The offset in the text segment of a string describing this link object.
259 specifies a library that is to be searched for by ld.so.
260 The path name is obtained by searching a set of directories
265 for a shared object matching
266 .Em lib\&\*[Lt]sod_name\*[Gt]\&.so.n.m .
269 should point at a full path name for the desired shared object.
271 Specifies the major version number of the shared object to load.
273 Specifies the preferred minor version number of the shared object to load.
276 The run-time link-editor maintains a list of structures called
278 to keep track of all shared objects loaded into a process' address space.
279 These structures are only used at run-time and do not occur within
280 the text or data segment of an executable or shared library.
281 .Bd -literal -offset indent
285 struct so_map *som_next;
289 struct _dynamic *som_dynamic;
293 .Bl -tag -width som_dynamic
295 The address at which the shared object associated with this link map has
298 The full path name of the loaded object.
300 Pointer to the next link map.
304 structure that was responsible for loading this shared object.
306 Tossed in later versions the run-time linker.
308 Set if (some portion of) this object's text segment is currently writable.
310 Pointer to this object's
314 Hook for attaching private data maintained by the run-time link-editor.
317 Symbol description with size.
320 structure with one field
323 Used to convey size information on items in the data segment of
325 An array of these lives in the shared object's text segment and is
329 .Fa section_dispatch_table .
330 .Bd -literal -offset indent
334 #define nz_un nlist.n_un
335 #define nz_strx nlist.n_un.n_strx
336 #define nz_name nlist.n_un.n_name
337 #define nz_type nlist.n_type
338 #define nz_value nlist.n_value
339 #define nz_desc nlist.n_desc
340 #define nz_other nlist.n_other
343 .Bl -tag -width nz_size
350 The size of the data represented by this symbol.
353 A hash table is included within the text segment of shared object
354 to facilitate quick lookup of symbols during run-time link-editing.
358 .Fa section_dispatch_table
359 structure points at an array of
362 .Bd -literal -offset indent
364 int rh_symbolnum; /* symbol number */
365 int rh_next; /* next hash entry */
369 .Bl -tag -width rh_symbolnum
371 The index of the symbol in the shared object's symbol table (as given by the
375 In case of collisions, this field is the offset of the next entry in this
377 It is zero for the last bucket element.
381 structure is used to keep track of run-time allocated commons
382 and data items copied from shared objects.
383 These items are kept on linked list and is exported through the
387 structure (see below) for use by debuggers.
388 .Bd -literal -offset indent
390 struct nzlist *rt_sp;
391 struct rt_symbol *rt_next;
392 struct rt_symbol *rt_link;
394 struct so_map *rt_smp;
398 .Bl -tag -width rt_scraddr
400 The symbol description.
402 Virtual address of next rt_symbol.
405 Used by internally by ld.so.
407 Location of the source of initialized data within a shared object.
409 The shared object which is the original source of the data that this
410 run-time symbol describes.
415 structure is used by debuggers to gain knowledge of any shared objects
416 that have been loaded in the process's address space as a result of run-time
418 Since the run-time link-editor runs as a part of process initialization,
419 a debugger that wishes to access symbols from shared objects can
420 only do so after the link-editor has been called from crt0.
421 A dynamically linked binary contains a
423 structure which can be located by means of the
427 .Bd -literal -offset indent
434 struct rt_symbol *dd_cc;
438 .Bl -tag -width dd_in_debugger
440 Version number of this interface.
441 .It Fa dd_in_debugger
442 Set by the debugger to indicate to the run-time linker that the program is
443 run under control of a debugger.
445 Set by the run-time linker whenever it adds symbols by loading shared objects.
447 The address were a breakpoint will be set by the run-time linker to
448 divert control to the debugger.
449 This address is determined by the start-up module,
451 to be some convenient place before the call to _main.
453 Contains the original instruction that was at
455 The debugger is expected to put this instruction back before continuing the
458 A pointer to the linked list of run-time allocated symbols that the debugger
459 may be interested in.
464 structure defines a set of service routines within ld.so.
467 for more information.
468 .Bd -literal -offset indent
470 void *(*dlopen)(char *, int);
471 int (*dlclose)(void *);
472 void *(*dlsym)(void *, char *);
473 int (*dlctl)(void *, int, void *);
474 void (*dlexit)(void);
480 structure defines the interface between ld.so and the start-up code in crt0.
481 .Bd -literal -offset indent
486 struct _dynamic *crt_dp;
493 #define CRT_VERSION_SUN 1
494 #define CRT_VERSION_BSD2 2
495 #define CRT_VERSION_BSD3 3
496 #define CRT_VERSION_BSD4 4
498 .Bl -tag -width crt_dzfd
500 The virtual address at which ld.so was loaded by crt0.
504 systems, this field contains an open file descriptor to
506 used to get demand paged zeroed pages.
509 systems it contains -1.
511 Contains an open file descriptor that was used by crt0 to load ld.so.
517 A pointer to the environment strings.
519 The address at which a breakpoint will be placed by the run-time linker
520 if the main program is run by a debugger.
524 The name of the main program as determined by crt0 (CRT_VERSION_BSD3 only).
526 The path of the run-time linker as mapped by crt0 (CRT_VERSION_BSD4 only).
530 entry points provided by the run-time linker (CRT_VERSION_BSD4 only).
537 structures define the layout of the library hints, normally found in
538 .Dq /var/run/ld.so.hints ,
539 which is used by ld.so to quickly locate the shared object images in the
541 The organization of the hints file is not unlike that of an
543 object file, in that it contains a header determining the offset and size
544 of a table of fixed sized hash buckets and a common string pool.
545 .Bd -literal -offset indent
546 struct hints_header {
548 #define HH_MAGIC 011421044151
550 #define LD_HINTS_VERSION_1 1
551 #define LD_HINTS_VERSION_2 2
560 .Bl -tag -width hh_strtab_sz
562 Hints file magic number.
564 Interface version number.
566 Offset of hash table.
568 Offset of string table.
572 Maximum usable offset in hints file.
574 Offset in string table of a colon-separated list of directories that was
575 used in constructing the hints file.
578 This field is only available with interface version number
579 .Dv LD_HINTS_VERSION_2
583 .Bd -literal -offset indent
585 * Hash table element in hints file.
587 struct hints_bucket {
590 int hi_dewey[MAXDEWEY];
592 #define hi_major hi_dewey[0]
593 #define hi_minor hi_dewey[1]
597 .Bl -tag -width hi_ndewey
599 Index of the string identifying the library.
601 Index of the string representing the full path name of the library.
603 The version numbers of the shared library.
605 The number of valid entries in
608 Next bucket in case of hashing collisions.