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