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