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