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. 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 rational 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.Pp 112.Sh DATA STRUCTURES 113The data structures supporting dynamic linking and run-time relocation 114reside both in the text and data segments of the image they apply to. 115The text segments contain read-only data such as symbols descriptions and 116names, while the data segments contain the tables that need to be modified by 117during the relocation process. 118.Pp 119The _DYNAMIC symbol references a 120.Fa _dynamic 121structure: 122.Bd -literal -offset indent 123struct _dynamic { 124 int d_version; 125 struct so_debug *d_debug; 126 union { 127 struct section_dispatch_table *d_sdt; 128 } d_un; 129 struct ld_entry *d_entry; 130}; 131.Ed 132.Bl -tag -width d_version 133.It Fa d_version 134This field provides for different versions of the dynamic linking 135implementation. 136The current version numbers understood by 137.Xr ld 1 138and 139.Xr ld.so 1 140are 141.Em LD_VERSION_SUN (3) , 142which is used by the 143.Tn SunOS 1444.x releases, and 145.Em LD_VERSION_BSD (8) , 146which has been in use since 147.Fx 1.1 . 148.It Fa d_un 149Refers to a 150.Em d_version 151dependent data structure. 152.It Fa so_debug 153this field provides debuggers with a hook to access symbol tables of shared 154objects loaded as a result of the actions of the run-time link-editor. 155.El 156.Pp 157The 158.Fa section_dispatch_table 159structure is the main 160.Dq dispatcher 161table, containing offsets into the image's segments where various symbol 162and relocation information is located. 163.Bd -literal -offset indent 164struct section_dispatch_table { 165 struct so_map *sdt_loaded; 166 long sdt_sods; 167 long sdt_filler1; 168 long sdt_got; 169 long sdt_plt; 170 long sdt_rel; 171 long sdt_hash; 172 long sdt_nzlist; 173 long sdt_filler2; 174 long sdt_buckets; 175 long sdt_strings; 176 long sdt_str_sz; 177 long sdt_text_sz; 178 long sdt_plt_sz; 179}; 180.Ed 181.Pp 182.Bl -tag -width sdt_filler1 183.It Fa sdt_loaded 184A pointer to the first link map loaded (see below). This field is set by 185.Nm ld.so 186.It Fa sdt_sods 187The start of a (linked) list of shared object descriptors needed by 188.Em this 189object. 190.It Fa sdt_filler1 191Deprecated (used by SunOS to specify library search rules). 192.It Fa sdt_got 193The location of the Global Offset Table within this image. 194.It Fa sdt_plt 195The location of the Procedure Linkage Table within this image. 196.It Fa sdt_rel 197The location of an array of 198.Fa relocation_info 199structures 200.Po 201see 202.Xr a.out 5 203.Pc 204specifying run-time relocations. 205.It Fa sdt_hash 206The location of the hash table for fast symbol lookup in this object's 207symbol table. 208.It Fa sdt_nzlist 209The location of the symbol table. 210.It Fa sdt_filler2 211Currently unused. 212.It Fa sdt_buckets 213The number of buckets in 214.Fa sdt_hash 215.It Fa sdt_strings 216The location of the symbol string table that goes with 217.Fa sdt_nzlist . 218.It Fa sdt_str_sz 219The size of the string table. 220.It Fa sdt_text_sz 221The size of the object's text segment. 222.It Fa sdt_plt_sz 223The size of the Procedure Linkage Table. 224.El 225.Pp 226A 227.Fa sod 228structure describes a shared object that is needed 229to complete the link edit process of the object containing it. 230A list of such objects 231.Po 232chained through 233.Fa sod_next 234.Pc 235is pointed at 236by the 237.Fa sdt_sods 238in the section_dispatch_table structure. 239.Bd -literal -offset indent 240struct sod { 241 long sod_name; 242 u_int sod_library : 1, 243 sod_reserved : 31; 244 short sod_major; 245 short sod_minor; 246 long sod_next; 247}; 248.Ed 249.Pp 250.Bl -tag -width sod_library 251.It Fa sod_name 252The offset in the text segment of a string describing this link object. 253.It Fa sod_library 254If set, 255.Fa sod_name 256specifies a library that is to be searched for by 257.Nm ld.so . 258The path name 259is obtained by searching a set of directories 260.Po 261see also 262.Xr ldconfig 8 263.Pc 264for a shared object matching 265.Em lib\&<sod_name>\&.so.n.m . 266If not set, 267.Fa sod_name 268should point at a full path name for the desired shared object. 269.It Fa sod_major 270Specifies the major version number of the shared object to load. 271.It Fa sod_minor 272Specifies the preferred minor version number of the shared object to load. 273.El 274.Pp 275The run-time link-editor maintains a list of structures called 276.Em link maps 277to keep track of all shared objects loaded into a process' address space. 278These structures are only used at run-time and do not occur within 279the text or data segment of an executable or shared library. 280.Bd -literal -offset indent 281struct so_map { 282 caddr_t som_addr; 283 char *som_path; 284 struct so_map *som_next; 285 struct sod *som_sod; 286 caddr_t som_sodbase; 287 u_int som_write : 1; 288 struct _dynamic *som_dynamic; 289 caddr_t som_spd; 290}; 291.Ed 292.Bl -tag -width som_dynamic 293.It Fa som_addr 294The address at which the shared object associated with this link map has 295been loaded. 296.It Fa som_path 297The full path name of the loaded object. 298.It Fa som_next 299Pointer to the next link map. 300.It Fa som_sod 301The 302.Fa sod 303structure that was responsible for loading this shared object. 304.It Fa som_sodbase 305Tossed in later versions the run-time linker. 306.It Fa som_write 307Set if (some portion of) this object's text segment is currently writable. 308.It Fa som_dynamic 309Pointer to this object's 310.Fa _dynamic 311structure. 312.It Fa som_spd 313Hook for attaching private data maintained by the run-time link-editor. 314.El 315.Pp 316Symbol description with size. 317This is simply an 318.Fa nlist 319structure with one field 320.Pq Fa nz_size 321added. 322Used to convey size information on items in the data segment 323of shared objects. 324An array of these lives in the shared object's 325text segment and is addressed by the 326.Fa sdt_nzlist 327field of 328.Fa section_dispatch_table . 329.Bd -literal -offset indent 330struct nzlist { 331 struct nlist nlist; 332 u_long nz_size; 333#define nz_un nlist.n_un 334#define nz_strx nlist.n_un.n_strx 335#define nz_name nlist.n_un.n_name 336#define nz_type nlist.n_type 337#define nz_value nlist.n_value 338#define nz_desc nlist.n_desc 339#define nz_other nlist.n_other 340}; 341.Ed 342.Bl -tag -width nz_size 343.It Fa nlist 344.Po 345see 346.Xr nlist 3 347.Pc . 348.It Fa nz_size 349The size of the data represented by this symbol. 350.El 351.Pp 352A hash table is included within the text segment of shared object to 353to facilitate quick lookup of symbols during run-time link-editing. 354The 355.Fa sdt_hash 356field of the 357.Fa section_dispatch_table 358structure points at an array of 359.Fa rrs_hash 360structures: 361.Bd -literal -offset indent 362struct rrs_hash { 363 int rh_symbolnum; /* symbol number */ 364 int rh_next; /* next hash entry */ 365}; 366.Ed 367.Pp 368.Bl -tag -width rh_symbolnum 369.It Fa rh_symbolnum 370The index of the symbol in the shared object's symbol table (as given by the 371.Fa ld_symbols 372field). 373.It Fa rh_next 374In case of collisions, this field is the offset of the next entry in this 375hash table bucket. 376It is zero for the last bucket element. 377.El 378The 379.Fa rt_symbol 380structure is used to keep track of run-time allocated commons 381and data items copied from shared objects. 382These items are kept on linked list 383and is exported through the 384.Fa dd_cc 385field in the 386.Fa so_debug 387structure (see below) for use by debuggers. 388.Bd -literal -offset indent 389struct rt_symbol { 390 struct nzlist *rt_sp; 391 struct rt_symbol *rt_next; 392 struct rt_symbol *rt_link; 393 caddr_t rt_srcaddr; 394 struct so_map *rt_smp; 395}; 396.Ed 397.Pp 398.Bl -tag -width rt_scraddr 399.It Fa rt_sp 400The symbol description. 401.It Fa rt_next 402Virtual address of next rt_symbol. 403.It Fa rt_link 404Next in hash bucket. 405Used by internally by 406.Nm ld.so . 407.It Fa rt_srcaddr 408Location of the source of initialized data within a shared object. 409.It Fa rt_smp 410The shared object which is the original source of the data that this 411run-time symbol describes. 412.El 413.Pp 414The 415.Fa so_debug 416structure is used by debuggers to gain knowledge of any shared objects 417that have been loaded in the process's address space as a result of run-time 418link-editing. 419Since the run-time link-editor runs as a part of process 420initialization, a debugger that wishes to access symbols from shared objects 421can only do so after the link-editor has been called from crt0. 422A dynamically linked binary contains a 423.Fa so_debug 424structure which can be located by means of the 425.Fa d_debug 426field in 427.Fa _dynamic . 428.Bd -literal -offset indent 429struct so_debug { 430 int dd_version; 431 int dd_in_debugger; 432 int dd_sym_loaded; 433 char *dd_bpt_addr; 434 int dd_bpt_shadow; 435 struct rt_symbol *dd_cc; 436}; 437.Ed 438.Pp 439.Bl -tag -width dd_in_debugger 440.It Fa dd_version 441Version number of this interface. 442.It Fa dd_in_debugger 443Set by the debugger to indicate to the run-time linker that the program is 444run under control of a debugger. 445.It Fa dd_sym_loaded 446Set by the run-time linker whenever it adds symbols by loading shared objects. 447.It Fa dd_bpt_addr 448The address were a breakpoint will be set by the run-time linker to 449divert control to the debugger. 450This address is determined by the start-up 451module, 452.Pa crt0.o , 453to be some convenient place before the call to _main. 454.It Fa dd_bpt_shadow 455Contains the original instruction that was at 456.Fa dd_bpt_addr . 457The debugger is expected to put this instruction back before continuing the 458program. 459.It Fa dd_cc 460A pointer to the linked list of run-time allocated symbols that the debugger 461may be interested in. 462.El 463.Pp 464The 465.Em ld_entry 466structure defines a set of service routines within 467.Nm ld.so . 468.\" See 469.\" .Xr libdl.a 470.\" for more information. 471.Bd -literal -offset indent 472struct ld_entry { 473 void *(*dlopen)(char *, int); 474 int (*dlclose)(void *); 475 void *(*dlsym)(void *, char *); 476 char *(*dlerror)(void); 477}; 478.Ed 479.Pp 480The 481.Fa crt_ldso 482structure defines the interface between the start-up code in crt0 and 483.Nm ld.so . 484.Bd -literal -offset indent 485struct crt_ldso { 486 int crt_ba; 487 int crt_dzfd; 488 int crt_ldfd; 489 struct _dynamic *crt_dp; 490 char **crt_ep; 491 caddr_t crt_bp; 492 char *crt_prog; 493 char *crt_ldso; 494 struct ld_entry *crt_ldentry; 495}; 496#define CRT_VERSION_SUN 1 497#define CRT_VERSION_BSD_2 2 498#define CRT_VERSION_BSD_3 3 499#define CRT_VERSION_BSD_4 4 500.Ed 501.Bl -tag -width crt_dzfd 502.It Fa crt_ba 503The virtual address at which 504.Nm ld.so 505was loaded by crt0. 506.It Fa crt_dzfd 507On SunOS systems, this field contains an open file descriptor to 508.Dq Pa /dev/zero 509used to get demand paged zeroed pages. 510On 511.Fx 512systems it contains -1. 513.It Fa crt_ldfd 514Contains an open file descriptor that was used by crt0 to load 515.Nm ld.so . 516.It Fa crt_dp 517A pointer to main's 518.Fa _dynamic 519structure. 520.It Fa crt_ep 521A pointer to the environment strings. 522.It Fa crt_bp 523The address at which a breakpoint will be placed by the run-time linker 524if the main program is run by a debugger. 525See 526.Fa so_debug 527.It Fa crt_prog 528The name of the main program as determined by crt0 (CRT_VERSION_BSD3 only). 529.It Fa crt_ldso 530The path of the run-time linker as mapped by crt0 (CRT_VERSION_BSD4 only). 531.El 532.Pp 533The 534.Fa hints_header 535and 536.Fa hints_bucket 537structures define the layout of the library hints, normally found in 538.Dq Pa /var/run/ld.so.hints , 539which is used by 540.Nm ld.so 541to quickly locate the shared object images in the 542filesystem. 543The organization of the hints file is not unlike that of an 544.Dq a.out 545object file, in that it contains a header determining the offset and size 546of a table of fixed sized hash buckets and a common string pool. 547.Bd -literal -offset indent 548struct hints_header { 549 long hh_magic; 550#define HH_MAGIC 011421044151 551 long hh_version; 552#define LD_HINTS_VERSION_1 1 553 long hh_hashtab; 554 long hh_nbucket; 555 long hh_strtab; 556 long hh_strtab_sz; 557 long hh_ehints; 558}; 559.Ed 560.Bl -tag -width hh_strtab_sz 561.It Fa hh_magic 562Hints file magic number. 563.It Fa hh_version 564Interface version number. 565.It Fa hh_hashtab 566Offset of hash table. 567.It Fa hh_strtab 568Offset of string table. 569.It Fa hh_strtab_sz 570Size of strings. 571.It Fa hh_ehints 572Maximum usable offset in hints file. 573.El 574.Pp 575.Bd -literal -offset indent 576/* 577 * Hash table element in hints file. 578 */ 579struct hints_bucket { 580 int hi_namex; 581 int hi_pathx; 582 int hi_dewey[MAXDEWEY]; 583 int hi_ndewey; 584#define hi_major hi_dewey[0] 585#define hi_minor hi_dewey[1] 586 int hi_next; 587}; 588.Ed 589.Bl -tag -width hi_ndewey 590.It Fa hi_namex 591Index of the string identifying the library. 592.It Fa hi_pathx 593Index of the string representing the full path name of the library. 594.It Fa hi_dewey 595The version numbers of the shared library. 596.It Fa hi_ndewey 597The number of valid entries in 598.Fa hi_dewey . 599.It Fa hi_next 600Next bucket in case of hashing collisions. 601.El 602.Sh CAVEATS 603Only the (GNU) C compiler currently supports the creation of shared libraries. 604Other programming languages can not be used. 605