p It also records a number of data structures unique to the dynamic loading and linking process. These include references to other objects that are required to complete the link-editing process and indirection tables to facilitate .Em Position Independent Code (PIC) to improve sharing of code pages among different processes.
p The collection of data structures described here will be referred to as the .Em Run-time Relocation Section (RRS) and is embedded in the standard text and data segments of the dynamically linked program or shared object image as the existing .Xr a.out 5 format offers no room for it elsewhere.
p Several utilities cooperate to ensure that the task of getting a program ready to run can complete successfully in a way that optimizes the use of system resources. The compiler emits PIC code from which shared libraries can be built by .Xr ld 1 . The compiler also includes size information of any initialized data items through the .size assembler directive.
p PIC code differs from conventional code in that it accesses data variables through an indirection table, the Global Offset Table, by convention accessible by the reserved name .Em _GLOBAL_OFFSET_TABLE_ . The exact mechanism used for this is machine dependent, usually a machine register is reserved for the purpose. The rationale behind this construct is to generate code that is independent of the actual load address. Only the values contained in the Global Offset Table may need updating at run-time depending on the load addresses of the various shared objects in the address space.
p Likewise, procedure calls to globally defined functions are redirected through the Procedure Linkage Table (PLT) residing in the data segment of the core image. Again, this is done to avoid run-time modifications to the text segment.
p The linker-editor allocates the Global Offset Table and Procedure Linkage Table when combining PIC object files into an image suitable for mapping into the process address space. It also collects all symbols that may be needed by the run-time link-editor and stores these along with the image's text and data bits. Another reserved symbol, .Em _DYNAMIC is used to indicate the presence of the run-time linker structures. Whenever .Em _DYNAMIC is relocated to 0, there is no need to invoke the run-time link-editor. If this symbol is non-zero, it points at a data structure from which the location of the necessary relocation and symbol information can be derived. This is most notably used by the start-up module, .Em crt0 . The _DYNAMIC structure is conventionally located at the start of the data segment of the image to which it pertains. .Sh DATA STRUCTURES The data structures supporting dynamic linking and run-time relocation reside both in the text and data segments of the image they apply to. The text segments contain read-only data such as symbols descriptions and names, while the data segments contain the tables that need to be modified by during the relocation process.
p The _DYNAMIC symbol references a .Fa _dynamic structure: d -literal -offset indent struct _dynamic { int d_version; struct so_debug *d_debug; union { struct section_dispatch_table *d_sdt; } d_un; struct ld_entry *d_entry; }; .Ed l -tag -width d_version t Fa d_version This field provides for different versions of the dynamic linking implementation. The current version numbers understood by ld and ld.so are .Em LD_VERSION_SUN (3) , which is used by the .Tn "SunOS 4.x" releases, and .Em LD_VERSION_BSD (8) , which is currently in use by .Nx . t Fa d_un Refers to a .Em d_version dependent data structure. t Fa d_debug this field provides debuggers with a hook to access symbol tables of shared objects loaded as a result of the actions of the run-time link-editor. t Fa d_entry this field is obsoleted by CRT interface version CRT_VERSION_BSD4, and is replaced by the crt_ldentry in .Fa crt_ldso . .El
p The .Fa section_dispatch_table structure is the main .Dq dispatcher table, containing offsets into the image's segments where various symbol and relocation information is located. d -literal -offset indent struct section_dispatch_table { struct so_map *sdt_loaded; long sdt_sods; long sdt_paths; long sdt_got; long sdt_plt; long sdt_rel; long sdt_hash; long sdt_nzlist; long sdt_filler2; long sdt_buckets; long sdt_strings; long sdt_str_sz; long sdt_text_sz; long sdt_plt_sz; }; .Ed
p l -tag -width sdt_loaded t Fa sdt_loaded A pointer to the first link map loaded (see below). This field is set by .Xr ld.so 1 for the benefit of debuggers that may use it to load a shared object's symbol table. t Fa sdt_sods The start of a (linked) list of shared object descriptors needed by .Em this object. t Fa sdt_paths Library search rules. A colon separated list of directories corresponding to the .Fl R option of .Xr ld 1 . t Fa sdt_got The location of the Global Offset Table within this image. t Fa sdt_plt The location of the Procedure Linkage Table within this image. t Fa sdt_rel The location of an array of .Fa relocation_info structures
o see .Xr a.out 5
c specifying run-time relocations. t Fa sdt_hash The location of the hash table for fast symbol lookup in this object's symbol table. t Fa sdt_nzlist The location of the symbol table. t Fa sdt_filler2 Currently unused. t Fa sdt_buckets The number of buckets in .Fa sdt_hash t Fa sdt_strings The location of the symbol string table that goes with .Fa sdt_nzlist . t Fa sdt_str_sz The size of the string table. t Fa sdt_text_sz The size of the object's text segment. t Fa sdt_plt_sz The size of the Procedure Linkage Table. .El
p A .Fa sod structure describes a shared object that is needed to complete the link edit process of the object containing it. A list of such objects
o chained through .Fa sod_next
c is pointed at by the .Fa sdt_sods in the section_dispatch_table structure. d -literal -offset indent struct sod { long sod_name; u_int sod_library : 1, sod_unused : 31; short sod_major; short sod_minor; long sod_next; }; .Ed
p l -tag -width sod_library t Fa sod_name The offset in the text segment of a string describing this link object. t Fa sod_library If set, .Fa sod_name specifies a library that is to be searched for by ld.so. The path name is obtained by searching a set of directories
o see also .Xr ldconfig 8
c
for a shared object matching
.Em lib
p The run-time link-editor maintains a list of structures called .Em link maps to keep track of all shared objects loaded into a process' address space. These structures are only used at run-time and do not occur within the text or data segment of an executable or shared library. d -literal -offset indent struct so_map { void *som_addr; char *som_path; struct so_map *som_next; struct sod *som_sod; void *som_sodbase; u_int som_write : 1; struct _dynamic *som_dynamic; void *som_spd; }; .Ed l -tag -width som_dynamic t Fa som_addr The address at which the shared object associated with this link map has been loaded. t Fa som_path The full path name of the loaded object. t Fa som_next Pointer to the next link map. t Fa som_sod The .Fa sod structure that was responsible for loading this shared object. t Fa som_sodbase Tossed in later versions the run-time linker. t Fa som_write Set if (some portion of) this object's text segment is currently writable. t Fa som_dynamic Pointer to this object's .Fa _dynamic structure. t Fa som_spd Hook for attaching private data maintained by the run-time link-editor. .El
p Symbol description with size. This is simply an .Fa nlist structure with one field
q Fa nz_size added. Used to convey size information on items in the data segment of shared objects. An array of these lives in the shared object's text segment and is addressed by the .Fa sdt_nzlist field of .Fa section_dispatch_table . d -literal -offset indent struct nzlist { struct nlist nlist; u_long nz_size; #define nz_un nlist.n_un #define nz_strx nlist.n_un.n_strx #define nz_name nlist.n_un.n_name #define nz_type nlist.n_type #define nz_value nlist.n_value #define nz_desc nlist.n_desc #define nz_other nlist.n_other }; .Ed l -tag -width nz_size t Fa nlist
o see .Xr nlist 3
c . t Fa nz_size The size of the data represented by this symbol. .El
p A hash table is included within the text segment of shared object to facilitate quick lookup of symbols during run-time link-editing. The .Fa sdt_hash field of the .Fa section_dispatch_table structure points at an array of .Fa rrs_hash structures: d -literal -offset indent struct rrs_hash { int rh_symbolnum; /* symbol number */ int rh_next; /* next hash entry */ }; .Ed
p l -tag -width rh_symbolnum t Fa rh_symbolnum The index of the symbol in the shared object's symbol table (as given by the .Fa ld_symbols field). t Fa rh_next In case of collisions, this field is the offset of the next entry in this hash table bucket. It is zero for the last bucket element. .El The .Fa rt_symbol structure is used to keep track of run-time allocated commons and data items copied from shared objects. These items are kept in a linked list which is exported through the .Fa dd_cc field in the .Fa so_debug structure (see below) for use by debuggers. d -literal -offset indent struct rt_symbol { struct nzlist *rt_sp; struct rt_symbol *rt_next; struct rt_symbol *rt_link; void *rt_srcaddr; struct so_map *rt_smp; }; .Ed
p l -tag -width rt_scraddr t Fa rt_sp The symbol description. t Fa rt_next Virtual address of next rt_symbol. t Fa rt_link Next in hash bucket. Used by internally by ld.so. t Fa rt_srcaddr Location of the source of initialized data within a shared object. t Fa rion Interface version number. t Fa hh_hashtab Offset of hash table. t Fa hh_strtab Offset of string table. t Fa hh_strtab_sz Size of strings. t Fa hh_ehints Maximum usable offset in hints file. t Fa hh_dirlist Offset in string table of a colon-separated list of directories that was used in constructing the hints file. See also .Xr ldconfig 8 . This field is only available with interface version number .Dv LD_HINTS_VERSION_2 and higher. .El
p d -literal -offset indent /* * Hash table element in hints file. */ struct hints_bucket { int hi_namex; int hi_pathx; int hi_dewey[MAXDEWEY]; int hi_ndewey; #define hi_major hi_dewey[0] #define hi_minor hi_dewey[1] int hi_next; }; .Ed l -tag -width hi_ndewey t Fa hi_namex Index of the string identifying the library. t Fa hi_pathx Index of the string representing the full path name of the library. t Fa hi_dewey The version numbers of the shared library. t Fa hi_ndewey The number of valid entries in .Fa hi_dewey . t Fa hi_next Next bucket in case of hashing collisions. .El