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