Initial import from FreeBSD RELENG_4:
[dragonfly.git] / share / man / man5 / link.5
1 .\" Copyright (c) 1993 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 acknowledgement:
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: src/share/man/man5/link.5,v 1.14.2.9 2001/12/17 11:30:15 ru Exp $
30 .\"
31 .Dd October 23, 1993
32 .Dt LINK 5
33 .Os
34 .Sh NAME
35 .Nm link
36 .Nd dynamic loader and link editor interface
37 .Sh SYNOPSIS
38 .In sys/types.h
39 .In nlist.h
40 .In link.h
41 .Sh DESCRIPTION
42 The include file
43 .Aq Pa link.h
44 declares several structures that are present in dynamically linked
45 programs and libraries.
46 The structures define the interface between several components of the
47 link-editor and loader mechanism.
48 The layout of a number of these
49 structures within the binaries resembles the a.out format in many places
50 as it serves such similar functions as symbol definitions (including the
51 accompanying string table) and relocation records needed to resolve
52 references to external entities.
53 It also records a number of data structures
54 unique to the dynamic loading and linking process.
55 These include references
56 to other objects that are required to complete the link-editing process and
57 indirection tables to facilitate
58 .Em Position Independent Code
59 (PIC for short) to improve sharing of code pages among different processes.
60 The collection of data structures described here will be referred to as the
61 .Em Run-time Relocation Section (RRS)
62 and is embedded in the standard text and data segments of the dynamically
63 linked program or shared object image as the existing
64 .Xr a.out 5
65 format offers no room for it elsewhere.
66 .Pp
67 Several utilities cooperate to ensure that the task of getting a program
68 ready to run can complete successfully in a way that optimizes the use
69 of system resources.
70 The compiler emits PIC code from which shared libraries
71 can be built by
72 .Xr ld 1 .
73 The compiler also includes size information of any initialized data items
74 through the .size assembler directive.
75 PIC code differs from conventional code
76 in that it accesses data variables through an indirection table, the
77 Global Offset Table, by convention accessible by the reserved name
78 .Dv _GLOBAL_OFFSET_TABLE_ .
79 The exact mechanism used for this is machine dependent, usually a machine
80 register is reserved for the purpose.
81 The rational behind this construct
82 is to generate code that is independent of the actual load address.
83 Only
84 the values contained in the Global Offset Table may need updating at run-time
85 depending on the load addresses of the various shared objects in the address
86 space.
87 .Pp
88 Likewise, procedure calls to globally defined functions are redirected through
89 the Procedure Linkage Table (PLT) residing in the data segment of the core
90 image.
91 Again, this is done to avoid run-time modifications to the text segment.
92 .Pp
93 The linker-editor allocates the Global Offset Table and Procedure Linkage Table
94 when combining PIC object files into an image suitable for mapping into the
95 process address space.
96 It also collects all symbols that may be needed by the
97 run-time link-editor and stores these along with the image's text and data bits.
98 Another reserved symbol,
99 .Em _DYNAMIC
100 is used to indicate the presence of the run-time linker structures.
101 Whenever
102 _DYNAMIC is relocated to 0, there is no need to invoke the run-time
103 link-editor.
104 If this symbol is non-zero, it points at a data structure from
105 which the location of the necessary relocation- and symbol information can
106 be derived.
107 This is most notably used by the start-up module,
108 .Em crt0 .
109 The _DYNAMIC structure is conventionally located at the start of the data
110 segment of the image to which it pertains.
111 .Sh DATA STRUCTURES
112 The data structures supporting dynamic linking and run-time relocation
113 reside both in the text and data segments of the image they apply to.
114 The text segments contain read-only data such as symbols descriptions and
115 names, while the data segments contain the tables that need to be modified by
116 during the relocation process.
117 .Pp
118 The _DYNAMIC symbol references a
119 .Fa _dynamic
120 structure:
121 .Bd -literal -offset indent
122 struct  _dynamic {
123         int     d_version;
124         struct  so_debug *d_debug;
125         union {
126                 struct section_dispatch_table *d_sdt;
127         } d_un;
128         struct  ld_entry *d_entry;
129 };
130 .Ed
131 .Bl -tag -width d_version
132 .It Fa d_version
133 This field provides for different versions of the dynamic linking
134 implementation.
135 The current version numbers understood by
136 .Xr ld 1
137 and
138 .Xr ld.so 1
139 are
140 .Em LD_VERSION_SUN (3) ,
141 which is used by the
142 .Tn SunOS
143 4.x releases, and
144 .Em LD_VERSION_BSD (8) ,
145 which has been in use since
146 .Fx 1.1 .
147 .It Fa d_un
148 Refers to a
149 .Em d_version
150 dependent data structure.
151 .It Fa so_debug
152 this field provides debuggers with a hook to access symbol tables of shared
153 objects loaded as a result of the actions of the run-time link-editor.
154 .El
155 .Pp
156 The
157 .Fa section_dispatch_table
158 structure is the main
159 .Dq dispatcher
160 table, containing offsets into the image's segments where various symbol
161 and relocation information is located.
162 .Bd -literal -offset indent
163 struct section_dispatch_table {
164         struct  so_map *sdt_loaded;
165         long    sdt_sods;
166         long    sdt_filler1;
167         long    sdt_got;
168         long    sdt_plt;
169         long    sdt_rel;
170         long    sdt_hash;
171         long    sdt_nzlist;
172         long    sdt_filler2;
173         long    sdt_buckets;
174         long    sdt_strings;
175         long    sdt_str_sz;
176         long    sdt_text_sz;
177         long    sdt_plt_sz;
178 };
179 .Ed
180 .Pp
181 .Bl -tag -width sdt_filler1
182 .It Fa sdt_loaded
183 A pointer to the first link map loaded (see below). This field is set by
184 .Nm ld.so
185 .It Fa sdt_sods
186 The start of a (linked) list of shared object descriptors needed by
187 .Em this
188 object.
189 .It Fa sdt_filler1
190 Deprecated (used by SunOS to specify library search rules).
191 .It Fa sdt_got
192 The location of the Global Offset Table within this image.
193 .It Fa sdt_plt
194 The location of the Procedure Linkage Table within this image.
195 .It Fa sdt_rel
196 The location of an array of
197 .Fa relocation_info
198 structures
199 (see
200 .Xr a.out 5 )
201 specifying run-time relocations.
202 .It Fa sdt_hash
203 The location of the hash table for fast symbol lookup in this object's
204 symbol table.
205 .It Fa sdt_nzlist
206 The location of the symbol table.
207 .It Fa sdt_filler2
208 Currently unused.
209 .It Fa sdt_buckets
210 The number of buckets in
211 .Fa sdt_hash
212 .It Fa sdt_strings
213 The location of the symbol string table that goes with
214 .Fa sdt_nzlist .
215 .It Fa sdt_str_sz
216 The size of the string table.
217 .It Fa sdt_text_sz
218 The size of the object's text segment.
219 .It Fa sdt_plt_sz
220 The size of the Procedure Linkage Table.
221 .El
222 .Pp
223 A
224 .Fa sod
225 structure describes a shared object that is needed
226 to complete the link edit process of the object containing it.
227 A list of such objects
228 (chained through
229 .Fa sod_next )
230 is pointed at
231 by the
232 .Fa sdt_sods
233 in the section_dispatch_table structure.
234 .Bd -literal -offset indent
235 struct sod {
236         long    sod_name;
237         u_int   sod_library : 1,
238                 sod_reserved : 31;
239         short   sod_major;
240         short   sod_minor;
241         long    sod_next;
242 };
243 .Ed
244 .Pp
245 .Bl -tag -width sod_library
246 .It Fa sod_name
247 The offset in the text segment of a string describing this link object.
248 .It Fa sod_library
249 If set,
250 .Fa sod_name
251 specifies a library that is to be searched for by
252 .Nm ld.so .
253 The path name
254 is obtained by searching a set of directories
255 (see also
256 .Xr ldconfig 8 )
257 for a shared object matching
258 .Em lib\&<sod_name>\&.so.n.m .
259 If not set,
260 .Fa sod_name
261 should point at a full path name for the desired shared object.
262 .It Fa sod_major
263 Specifies the major version number of the shared object to load.
264 .It Fa sod_minor
265 Specifies the preferred minor version number of the shared object to load.
266 .El
267 .Pp
268 The run-time link-editor maintains a list of structures called
269 .Em link maps
270 to keep track of all shared objects loaded into a process' address space.
271 These structures are only used at run-time and do not occur within
272 the text or data segment of an executable or shared library.
273 .Bd -literal -offset indent
274 struct so_map {
275         caddr_t som_addr;
276         char    *som_path;
277         struct  so_map *som_next;
278         struct  sod *som_sod;
279         caddr_t som_sodbase;
280         u_int   som_write : 1;
281         struct  _dynamic *som_dynamic;
282         caddr_t som_spd;
283 };
284 .Ed
285 .Bl -tag -width som_dynamic
286 .It Fa som_addr
287 The address at which the shared object associated with this link map has
288 been loaded.
289 .It Fa som_path
290 The full path name of the loaded object.
291 .It Fa som_next
292 Pointer to the next link map.
293 .It Fa som_sod
294 The
295 .Fa sod
296 structure that was responsible for loading this shared object.
297 .It Fa som_sodbase
298 Tossed in later versions the run-time linker.
299 .It Fa som_write
300 Set if (some portion of) this object's text segment is currently writable.
301 .It Fa som_dynamic
302 Pointer to this object's
303 .Fa _dynamic
304 structure.
305 .It Fa som_spd
306 Hook for attaching private data maintained by the run-time link-editor.
307 .El
308 .Pp
309 Symbol description with size.
310 This is simply an
311 .Fa nlist
312 structure with one field
313 .Pq Fa nz_size
314 added.
315 Used to convey size information on items in the data segment
316 of shared objects.
317 An array of these lives in the shared object's
318 text segment and is addressed by the
319 .Fa sdt_nzlist
320 field of
321 .Fa section_dispatch_table .
322 .Bd -literal -offset indent
323 struct nzlist {
324         struct nlist    nlist;
325         u_long          nz_size;
326 #define nz_un           nlist.n_un
327 #define nz_strx         nlist.n_un.n_strx
328 #define nz_name         nlist.n_un.n_name
329 #define nz_type         nlist.n_type
330 #define nz_value        nlist.n_value
331 #define nz_desc         nlist.n_desc
332 #define nz_other        nlist.n_other
333 };
334 .Ed
335 .Bl -tag -width nz_size
336 .It Fa nlist
337 (see
338 .Xr nlist 3 ) .
339 .It Fa nz_size
340 The size of the data represented by this symbol.
341 .El
342 .Pp
343 A hash table is included within the text segment of shared object
344 to facilitate quick lookup of symbols during run-time link-editing.
345 The
346 .Fa sdt_hash
347 field of the
348 .Fa section_dispatch_table
349 structure points at an array of
350 .Fa rrs_hash
351 structures:
352 .Bd -literal -offset indent
353 struct rrs_hash {
354         int     rh_symbolnum;           /* symbol number */
355         int     rh_next;                /* next hash entry */
356 };
357 .Ed
358 .Pp
359 .Bl -tag -width rh_symbolnum
360 .It Fa rh_symbolnum
361 The index of the symbol in the shared object's symbol table (as given by the
362 .Fa ld_symbols
363 field).
364 .It Fa rh_next
365 In case of collisions, this field is the offset of the next entry in this
366 hash table bucket.
367 It is zero for the last bucket element.
368 .El
369 The
370 .Fa rt_symbol
371 structure is used to keep track of run-time allocated commons
372 and data items copied from shared objects.
373 These items are kept on linked list
374 and is exported through the
375 .Fa dd_cc
376 field in the
377 .Fa so_debug
378 structure (see below) for use by debuggers.
379 .Bd -literal -offset indent
380 struct rt_symbol {
381         struct nzlist           *rt_sp;
382         struct rt_symbol        *rt_next;
383         struct rt_symbol        *rt_link;
384         caddr_t                 rt_srcaddr;
385         struct so_map           *rt_smp;
386 };
387 .Ed
388 .Pp
389 .Bl -tag -width rt_scraddr
390 .It Fa rt_sp
391 The symbol description.
392 .It Fa rt_next
393 Virtual address of next rt_symbol.
394 .It Fa rt_link
395 Next in hash bucket.
396 Used by internally by
397 .Nm ld.so .
398 .It Fa rt_srcaddr
399 Location of the source of initialized data within a shared object.
400 .It Fa rt_smp
401 The shared object which is the original source of the data that this
402 run-time symbol describes.
403 .El
404 .Pp
405 The
406 .Fa so_debug
407 structure is used by debuggers to gain knowledge of any shared objects
408 that have been loaded in the process's address space as a result of run-time
409 link-editing.
410 Since the run-time link-editor runs as a part of process
411 initialization, a debugger that wishes to access symbols from shared objects
412 can only do so after the link-editor has been called from crt0.
413 A dynamically linked binary contains a
414 .Fa so_debug
415 structure which can be located by means of the
416 .Fa d_debug
417 field in
418 .Fa _dynamic .
419 .Bd -literal -offset indent
420 struct  so_debug {
421         int     dd_version;
422         int     dd_in_debugger;
423         int     dd_sym_loaded;
424         char    *dd_bpt_addr;
425         int     dd_bpt_shadow;
426         struct rt_symbol *dd_cc;
427 };
428 .Ed
429 .Pp
430 .Bl -tag -width dd_in_debugger
431 .It Fa dd_version
432 Version number of this interface.
433 .It Fa dd_in_debugger
434 Set by the debugger to indicate to the run-time linker that the program is
435 run under control of a debugger.
436 .It Fa dd_sym_loaded
437 Set by the run-time linker whenever it adds symbols by loading shared objects.
438 .It Fa dd_bpt_addr
439 The address were a breakpoint will be set by the run-time linker to
440 divert control to the debugger.
441 This address is determined by the start-up
442 module,
443 .Pa crt0.o ,
444 to be some convenient place before the call to _main.
445 .It Fa dd_bpt_shadow
446 Contains the original instruction that was at
447 .Fa dd_bpt_addr .
448 The debugger is expected to put this instruction back before continuing the
449 program.
450 .It Fa dd_cc
451 A pointer to the linked list of run-time allocated symbols that the debugger
452 may be interested in.
453 .El
454 .Pp
455 The
456 .Em ld_entry
457 structure defines a set of service routines within
458 .Nm ld.so .
459 .\" See
460 .\" .Xr libdl.a
461 .\" for more information.
462 .Bd -literal -offset indent
463 struct ld_entry {
464         void    *(*dlopen)(char *, int);
465         int     (*dlclose)(void *);
466         void    *(*dlsym)(void *, char *);
467         char    *(*dlerror)(void);
468 };
469 .Ed
470 .Pp
471 The
472 .Fa crt_ldso
473 structure defines the interface between the start-up code in crt0 and
474 .Nm ld.so .
475 .Bd -literal -offset indent
476 struct crt_ldso {
477         int             crt_ba;
478         int             crt_dzfd;
479         int             crt_ldfd;
480         struct _dynamic *crt_dp;
481         char            **crt_ep;
482         caddr_t         crt_bp;
483         char            *crt_prog;
484         char            *crt_ldso;
485         struct ld_entry *crt_ldentry;
486 };
487 #define CRT_VERSION_SUN         1
488 #define CRT_VERSION_BSD_2       2
489 #define CRT_VERSION_BSD_3       3
490 #define CRT_VERSION_BSD_4       4
491 .Ed
492 .Bl -tag -width crt_dzfd
493 .It Fa crt_ba
494 The virtual address at which
495 .Nm ld.so
496 was loaded by crt0.
497 .It Fa crt_dzfd
498 On SunOS systems, this field contains an open file descriptor to
499 .Dq Pa /dev/zero
500 used to get demand paged zeroed pages.
501 On
502 .Fx
503 systems it contains -1.
504 .It Fa crt_ldfd
505 Contains an open file descriptor that was used by crt0 to load
506 .Nm ld.so .
507 .It Fa crt_dp
508 A pointer to main's
509 .Fa _dynamic
510 structure.
511 .It Fa crt_ep
512 A pointer to the environment strings.
513 .It Fa crt_bp
514 The address at which a breakpoint will be placed by the run-time linker
515 if the main program is run by a debugger.
516 See
517 .Fa so_debug
518 .It Fa crt_prog
519 The name of the main program as determined by crt0 (CRT_VERSION_BSD3 only).
520 .It Fa crt_ldso
521 The path of the run-time linker as mapped by crt0 (CRT_VERSION_BSD4 only).
522 .El
523 .Pp
524 The
525 .Fa hints_header
526 and
527 .Fa hints_bucket
528 structures define the layout of the library hints, normally found in
529 .Dq Pa /var/run/ld.so.hints ,
530 which is used by
531 .Nm ld.so
532 to quickly locate the shared object images in the
533 filesystem.
534 The organization of the hints file is not unlike that of an
535 .Dq a.out
536 object file, in that it contains a header determining the offset and size
537 of a table of fixed sized hash buckets and a common string pool.
538 .Bd -literal -offset indent
539 struct hints_header {
540         long            hh_magic;
541 #define HH_MAGIC        011421044151
542         long            hh_version;
543 #define LD_HINTS_VERSION_1      1
544         long            hh_hashtab;
545         long            hh_nbucket;
546         long            hh_strtab;
547         long            hh_strtab_sz;
548         long            hh_ehints;
549 };
550 .Ed
551 .Bl -tag -width hh_strtab_sz
552 .It Fa hh_magic
553 Hints file magic number.
554 .It Fa hh_version
555 Interface version number.
556 .It Fa hh_hashtab
557 Offset of hash table.
558 .It Fa hh_strtab
559 Offset of string table.
560 .It Fa hh_strtab_sz
561 Size of strings.
562 .It Fa hh_ehints
563 Maximum usable offset in hints file.
564 .El
565 .Pp
566 .Bd -literal -offset indent
567 /*
568  * Hash table element in hints file.
569  */
570 struct hints_bucket {
571         int             hi_namex;
572         int             hi_pathx;
573         int             hi_dewey[MAXDEWEY];
574         int             hi_ndewey;
575 #define hi_major hi_dewey[0]
576 #define hi_minor hi_dewey[1]
577         int             hi_next;
578 };
579 .Ed
580 .Bl -tag -width hi_ndewey
581 .It Fa hi_namex
582 Index of the string identifying the library.
583 .It Fa hi_pathx
584 Index of the string representing the full path name of the library.
585 .It Fa hi_dewey
586 The version numbers of the shared library.
587 .It Fa hi_ndewey
588 The number of valid entries in
589 .Fa hi_dewey .
590 .It Fa hi_next
591 Next bucket in case of hashing collisions.
592 .El
593 .Sh CAVEATS
594 Only the (GNU) C compiler currently supports the creation of shared libraries.
595 Other programming languages cannot be used.