LINK(5) | File Formats Manual | LINK(5) |
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 Position Independent Code (PIC) to improve sharing of code pages among different processes.
The collection of data structures described here will be referred to as the 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 a.out(5) format offers no room for it elsewhere.
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 ld(1). The compiler also includes size information of any initialized data items through the .size assembler directive.
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 _GLOBAL_OFFSET_TABLE_. The exact mechanism used for this is machine dependent, usually a machine register is reserved for the purpose. The rational 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.
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.
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, _DYNAMIC is used to indicate the presence of the run-time linker structures. Whenever _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, crt0. The _DYNAMIC structure is conventionally located at the start of the data segment of the image to which it pertains.
The _DYNAMIC symbol references a _dynamic structure:
struct _dynamic { int d_version; struct so_debug *d_debug; union { struct section_dispatch_table *d_sdt; } d_un; struct ld_entry *d_entry; };
The section_dispatch_table structure is the main “dispatcher” table, containing offsets into the image's segments where various symbol and relocation information is located.
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; };
A 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 (chained through sod_next) is pointed at by the sdt_sods in the section_dispatch_table structure.
struct sod { long sod_name; u_int sod_library : 1, sod_unused : 31; short sod_major; short sod_minor; long sod_next; };
The run-time link-editor maintains a list of structures called 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.
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; };
Symbol description with size. This is simply an nlist structure with one field (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 sdt_nzlist field of section_dispatch_table.
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 };
A hash table is included within the text segment of shared object to facilitate quick lookup of symbols during run-time link-editing. The sdt_hash field of the section_dispatch_table structure points at an array of rrs_hash structures:
struct rrs_hash { int rh_symbolnum; /* symbol number */ int rh_next; /* next hash entry */ };
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; };
The so_debug structure is used by debuggers to gain knowledge of any shared objects that have been loaded in the process's address space as a result of run-time link-editing. Since the run-time link-editor runs as a part of process initialization, a debugger that wishes to access symbols from shared objects can only do so after the link-editor has been called from crt0. A dynamically linked binary contains a so_debug structure which can be located by means of the d_debug field in _dynamic.
struct so_debug { int dd_version; int dd_in_debugger; int dd_sym_loaded; char *dd_bpt_addr; int dd_bpt_shadow; struct rt_symbol *dd_cc; };
The ld_entry structure defines a set of service routines within ld.so. See dlfcn(3) for more information.
struct ld_entry { void *(*dlopen)(char *, int); int (*dlclose)(void *); void *(*dlsym)(void *, char *); int (*dlctl)(void *, int, void *); void (*dlexit)(void); };
The crt_ldso structure defines the interface between ld.so and the start-up code in crt0.
struct crt_ldso { int crt_ba; int crt_dzfd; int crt_ldfd; struct _dynamic *crt_dp; char **crt_ep; void *crt_bp; char *crt_prog; char *crt_ldso; char *crt_ldentry; }; #define CRT_VERSION_SUN 1 #define CRT_VERSION_BSD2 2 #define CRT_VERSION_BSD3 3 #define CRT_VERSION_BSD4 4
The hints_header and hints_bucket structures define the layout of the library hints, normally found in “/var/run/ld.so.hints”, which is used by ld.so to quickly locate the shared object images in the file system. The organization of the hints file is not unlike that of an a.out(5) object file, in that it contains a header determining the offset and size of a table of fixed sized hash buckets and a common string pool.
struct hints_header { long hh_magic; #define HH_MAGIC 011421044151 long hh_version; #define LD_HINTS_VERSION_1 1 #define LD_HINTS_VERSION_2 2 long hh_hashtab; long hh_nbucket; long hh_strtab; long hh_strtab_sz; long hh_ehints; long hh_dirlist; };
/* * 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; };
October 23, 1993 | NetBSD 6.1 |