8. Dynamic Linking¶
After the system loads the program, it must complete the process image by resolving symbolic references among the object files that compose the process. This chapter discusses the object file structures that pertain to dynamic linking.
Note
The psABI supplement defines a naming convention for ELF constants
that have processor ranges specified. Names such as DT_, PT_,
for processor specific extensions, incorporate the name of the
processor: DT_M32_SPECIAL, for example.
Pre-existing processor extensions not using this convention will be supported.
Pre-Existing Extensions |
|---|
|
8.1. Program Interpreter¶
An executable file that participates in
dynamic linking shall have one
PT_INTERP program header element.
During process startup (e.g., exec()),
the system retrieves a path name from the PT_INTERP
segment and creates the initial process image from
the interpreter file’s segments. That is,
instead of using the original executable file’s
segment images, the system composes a memory
image for the interpreter.
It then is the interpreter’s responsibility to
receive control from the system and provide an
environment for the application program.
The interpreter typically receives control in one of two ways. First, it may receive a file descriptor to read the executable file, positioned at the beginning. It can use this file descriptor to read and/or map the executable file’s segments into memory. Second, depending on the executable file format, the system may load the executable file into memory instead of giving the interpreter an open file descriptor. With the possible exception of the file descriptor, the interpreter’s initial process state matches what the executable file would have received. The interpreter itself may not require a second interpreter. An interpreter may be either a shared object or an executable file. See the psABI supplement for additional information.
A shared object (the normal case) is loaded as
position-independent, with addresses that may vary
from one process to another; the system creates its segments
in the dynamic segment area used by mmap and related services.
Consequently, a shared object interpreter typically will
not conflict with the original executable file’s
original segment addresses.
An executable file may be loaded at fixed addresses; if so, the system creates its segments using the virtual addresses from the program header table. Consequently, an executable file interpreter’s virtual addresses may collide with the first executable file; the interpreter is responsible for resolving conflicts.
8.2. Dynamic Linker¶
When building an executable file that uses dynamic linking,
the link editor adds a program header element of type
PT_INTERP to an executable file, telling the system to invoke
the dynamic linker as the program interpreter.
Note
The locations of the system provided dynamic linkers are processor specific.
The system loader (e.g., exec()) and the dynamic linker cooperate to
create the process image for the program, which entails
the following actions:
Adding the executable file’s memory segments to the process image;
Adding shared object memory segments to the process image;
Performing relocations for the executable file and its shared objects;
Closing the file descriptor that was used to read the executable file, if one was given to the dynamic linker;
Transferring control to the program, making it look as if the program had received control directly from the system loader.
The link editor also constructs various data that assist the dynamic linker for executable and shared object files. As shown in Chapter 7, Program Loading, this data resides in loadable segments, making them available during execution. (Once again, recall the exact segment contents are processor-specific. See the psABI supplement for complete information).
A
.dynamicsection with typeSHT_DYNAMICholds various data. The structure residing at the beginning of the section holds the addresses of other dynamic linking information.The
.hashsection with typeSHT_HASHholds a symbol hash table.The
.gotand.pltsections with typeSHT_PROGBITShold two separate tables: the global offset table and the procedure linkage table. The psABI supplement discusses how programs use the global offset table for position-independent code. Sections below explain how the dynamic linker uses and changes the tables to create memory images for object files.
Because every ABI-conforming program imports the basic system services from a shared object library, the dynamic linker participates in every ABI-conforming program execution.
Shared objects may occupy virtual memory addresses that are different from the addresses recorded in the file’s program header table. The dynamic linker relocates the memory image, updating absolute addresses before the application gains control. Although the absolute address values would be correct if the library were loaded at the addresses specified in the program header table, this normally is not the case.
If the process environment contains a variable named LD_BIND_NOW
with a non-null value, the dynamic linker processes
all relocations before transferring control to the program.
For example, all the following environment entries
would specify this behavior.
LD_BIND_NOW=1LD_BIND_NOW=onLD_BIND_NOW=off
Otherwise, LD_BIND_NOW either
does not occur in the environment or has a null value.
The dynamic linker is permitted to evaluate procedure linkage table
entries lazily, thus avoiding symbol resolution and relocation
overhead for functions that are not called.
See the psABI supplement for more information.
8.3. Dynamic Section¶
If an object file participates in dynamic linking,
its program header table will have an element of type
PT_DYNAMIC.
This “segment” contains the .dynamic section.
A special symbol, _DYNAMIC,
labels the section, which contains
an array of the following structures.
typedef struct {
Elf32_Sword d_tag;
union {
Elf32_Word d_val;
Elf32_Addr d_ptr;
} d_un;
} Elf32_Dyn;
extern Elf32_Dyn _DYNAMIC[];
typedef struct {
Elf64_Sxword d_tag;
union {
Elf64_Xword d_val;
Elf64_Addr d_ptr;
} d_un;
} Elf64_Dyn;
extern Elf64_Dyn _DYNAMIC[];
For each object with this type, d_tag
controls the interpretation of d_un.
d_valThese objects represent integer values with various interpretations.
d_ptrThese objects represent program virtual addresses. As mentioned previously, a file’s virtual addresses might not match the memory virtual addresses during execution. When interpreting addresses contained in the dynamic structure, the dynamic linker computes actual addresses, based on the original file value and the memory base address. For consistency, files do not contain relocation entries to “correct” addresses in the dynamic structure.
To make it simpler for tools to interpret the contents of
dynamic section entries, the value of each tag, except for those in
two special compatibility ranges,
will determine the interpretation of the d_un
union. A tag whose value is an even number
indicates a dynamic section entry that uses d_ptr.
A tag whose value is an odd number indicates a dynamic section entry
that uses d_val or that uses neither d_ptr
nor d_val. Tags whose values are less
than the special value DT_ENCODING and tags
whose values fall between DT_HIOS and
DT_LOPROC do not follow these rules.
The following table summarizes the tag requirements for executable and shared object files. If a tag is marked “mandatory”, the dynamic linking array for an ABI-conforming file must have an entry of that type. Likewise, “optional” means an entry for the tag may appear but is not required.
Name |
Value |
|
Executable |
Shared Object |
|---|---|---|---|---|
|
|
ignored |
mandatory |
mandatory |
|
|
|
optional |
optional |
|
|
|
optional |
optional |
|
|
|
optional |
optional |
|
|
|
mandatory† |
mandatory† |
|
|
|
mandatory |
mandatory |
|
|
|
mandatory |
mandatory |
|
|
|
mandatory |
optional |
|
|
|
mandatory |
optional |
|
|
|
mandatory |
optional |
|
|
|
mandatory |
mandatory |
|
|
|
mandatory |
mandatory |
|
|
|
optional |
optional |
|
|
|
optional |
optional |
|
|
|
ignored |
optional |
|
|
|
optional |
ignored |
|
|
ignored |
ignored |
optional |
|
|
|
mandatory |
optional |
|
|
|
mandatory |
optional |
|
|
|
mandatory |
optional |
|
|
|
optional |
optional |
|
|
|
optional |
ignored |
|
|
ignored |
optional |
optional |
|
|
|
optional |
optional |
|
|
ignored |
optional |
optional |
|
|
|
optional |
optional |
|
|
|
optional |
optional |
|
|
|
optional |
optional |
|
|
|
optional |
optional |
|
|
|
optional |
optional |
|
|
|
optional |
optional |
|
|
unspecified |
unspecified |
unspecified |
|
|
|
optional |
ignored |
|
|
|
optional |
ignored |
|
|
|
optional |
optional |
|
|
|
optional |
optional |
|
|
|
optional |
optional |
|
|
|
optional |
optional |
|
|
|
optional† |
optional† |
|
|
unspecified |
unspecified |
unspecified |
|
|
unspecified |
unspecified |
unspecified |
|
|
unspecified |
unspecified |
unspecified |
|
|
unspecified |
unspecified |
unspecified |
DT_HASH is optional if DT_SYMTABSZ is provided.DT_NULLAn entry with a
DT_NULLtag marks the end of the_DYNAMICarray.DT_NEEDEDThis element holds the string table offset of a null-terminated string, giving the name of a needed library. The offset is an index into the table recorded in the
DT_STRTABcode. See Section 8.4, Shared Object Dependencies, for more information about these names. The dynamic array may contain multiple entries with this type. These entries’ relative order is significant, though their relation to entries of other types is not.DT_PLTRELSZThis element holds the total size, in bytes, of the relocation entries associated with the procedure linkage table. If an entry of type
DT_JMPRELis present, aDT_PLTRELSZmust accompany it.DT_PLTGOTThis element holds an address associated with the procedure linkage table and/or the global offset table. See the psABI supplement for details.
DT_HASHThis element holds the address of the symbol hash table, described in Section 8.5, Hash Table. This hash table refers to the symbol table referenced by the
DT_SYMTABelement.DT_HASHis normally mandatory. The psABI supplement is allowed to override this requirement by providing an alternative hash mechanism. In such cases,DT_SYMTABSZ, which is normally optional, becomes mandatory.DT_STRTABThis element holds the address of the string table, described in Chapter 4, String Table. Symbol names, library names, and other strings reside in this table.
DT_SYMTABThis element holds the address of the dynamic linking symbol table, as described in Chapter 5, Symbol Table, with
Elf32_Symentries for the 32-bit class of files andElf64_Symentries for the 64-bit class of files.DT_RELAThis element holds the address of a relocation table, described in Chapter 6, Relocation. Entries in the table have explicit addends (
Elf32_Relafor the 32-bit file class orElf64_Relafor the 64-bit file class). An object file may have multiple relocation sections. When building the relocation table for an executable or shared object file, the link editor concatenates those sections to form a single table. Although the sections remain independent in the object file, the dynamic linker sees a single table. When the dynamic linker creates the process image for an executable file or adds a shared object to the process image, it reads the relocation table and performs the associated actions. If this element is present, the dynamic structure must also haveDT_RELASZandDT_RELAENTelements. When relocation is “mandatory” for a file, eitherDT_RELAorDT_RELmay occur (both are permitted but not required).DT_RELASZThis element holds the total size, in bytes, of the
DT_RELArelocation table.DT_RELAENTThis element holds the size, in bytes, of the
DT_RELArelocation entry.DT_STRSZThis element holds the size, in bytes, of the string table.
DT_SYMENTThis element holds the size, in bytes, of a symbol table entry.
DT_INITThis element holds the address of the initialization function, discussed in Section 8.6, Initialization and Termination Functions.
DT_FINIThis element holds the address of the termination function, discussed in Section 8.6, Initialization and Termination Functions.
DT_SONAMEThis element holds the string table offset of a null-terminated string, giving the name of the shared object. The offset is an index into the table recorded in the
DT_STRTABentry. See Section 8.4, Shared Object Dependencies for more information about these names.DT_RPATHThis element holds the string table offset of a null-terminated search library search path string discussed in Section 8.4, Shared Object Dependencies. The offset is an index into the table recorded in the
DT_STRTABentry. This entry is deprecated; its use has been superseded byDT_RUNPATH.DT_SYMBOLICThis element’s presence in a shared object library alters the dynamic linker’s symbol resolution algorithm for references within the library. Instead of starting a symbol search with the executable file, the dynamic linker starts from the shared object itself. If the shared object fails to supply the referenced symbol, the dynamic linker then searches the executable file and other shared objects as usual. This entry is deprecated; its use has been superseded by the
DF_SYMBOLICflag.DT_RELThis element is similar to
DT_RELA, except its table has implicit addends (Elf32_Relfor the 32-bit file class orElf64_Relfor the 64-bit file class). If this element is present, the dynamic structure must also haveDT_RELSZandDT_RELENTelements.DT_RELSZThis element holds the total size, in bytes, of the
DT_RELrelocation table.DT_RELENTThis element holds the size, in bytes, of the
DT_RELrelocation entry.DT_PLTRELThis member specifies the type of relocation entry to which the procedure linkage table refers. The
d_valmember holdsDT_RELorDT_RELA, as appropriate. All relocations in a procedure linkage table must use the same relocation.DT_DEBUGThis member is used for debugging. Its contents are not specified for the ABI; programs that access this entry are not ABI-conforming.
DT_TEXTRELThis member’s absence signifies that no relocation entry should cause a modification to a non-writable segment, as specified by the segment permissions in the program header table. If this member is present, one or more relocation entries might request modifications to a non-writable segment, and the dynamic linker can prepare accordingly. This entry is deprecated; its use has been superseded by the
DF_TEXTRELflag.DT_JMPRELIf present, this entry’s
d_ptrmember holds the address of relocation entries associated solely with the procedure linkage table. Separating these relocation entries lets the dynamic linker ignore them during process initialization, if lazy binding is enabled. If this entry is present, the related entries of typesDT_PLTRELSZandDT_PLTRELmust also be present.DT_BIND_NOWIf present in a shared object or executable, this entry instructs the dynamic linker to process all relocations for the object containing this entry before transferring control to the program. The presence of this entry takes precedence over a directive to use lazy binding for this object when specified through the environment or via
dlopen(). This entry is deprecated; its use has been superseded by theDF_BIND_NOWflag.DT_INIT_ARRAYThis element holds the address of the array of pointers to initialization functions, discussed in Section 8.6, Initialization and Termination Functions.
DT_FINI_ARRAYThis element holds the address of the array of pointers to termination functions, discussed in Section 8.6, Initialization and Termination Functions.
DT_INIT_ARRAYSZThis element holds the size in bytes of the array of initialization functions pointed to by the
DT_INIT_ARRAYentry. If an object has aDT_INIT_ARRAYentry, it must also have aDT_INIT_ARRAYSZentry.DT_FINI_ARRAYSZThis element holds the size in bytes of the array of termination functions pointed to by the
DT_FINI_ARRAYentry. If an object has aDT_FINI_ARRAYentry, it must also have aDT_FINI_ARRAYSZentry.DT_RUNPATHThis element holds the string table offset of a null-terminated library search path string discussed in Section 8.4, Shared Object Dependencies. The offset is an index into the table recorded in the
DT_STRTABentry.DT_FLAGSThis element holds flag values specific to the object being loaded. Each flag value will have the name
DF_flag_name. Defined values and their meanings are described below. All other values are reserved.DT_PREINIT_ARRAYThis element holds the address of the array of pointers to pre-initialization functions, discussed in Section 8.6, Initialization and Termination Functions. The
DT_PREINIT_ARRAYtable is processed only in an executable file; it is ignored if contained in a shared object.DT_PREINIT_ARRAYSZThis element holds the size in bytes of the array of pre-initialization functions pointed to by the
DT_PREINIT_ARRAYentry. If an object has aDT_PREINIT_ARRAYentry, it must also have aDT_PREINIT_ARRAYSZentry. As withDT_PREINIT_ARRAY, this entry is ignored if it appears in a shared object.DT_SYMTAB_SHNDXThis element holds the address of the
SHT_SYMTAB_SHNDXsection associated with the dynamic symbol table referenced by theDT_SYMTABelement.DT_RELRThis element holds the address of an
SHT_RELRrelocation table, described in Section 6.2, Relative Relocation Table. This table will hold entries of eitherElf32_Relrfor the 32-bit file class orElf64_Relrfor the 64-bit file class. If this element is present, the dynamic structure must also haveDT_RELRSZandDT_RELRENTelements. During dynamic linking, aDT_RELRelement is processed before anyDT_RELorDT_RELAelements in the same object file.DT_RELRSZThis element holds the total size, in bytes, of the
DT_RELRrelocation table.DT_RELRENTThis element holds the size, in bytes, of the
DT_RELRrelocation entry.DT_SYMTABSZThis element holds the size, in bytes, of the
DT_SYMTABdynamic linking symbol table. It must be provided if theDT_HASHsymbol hash table is omitted.DT_ENCODINGValues greater than or equal to
DT_ENCODINGand less thanDT_LOOSfollow the rules for the interpretation of thed_ununion described above.DT_LOOSthroughDT_HIOSValues in this inclusive range are reserved for operating system-specific semantics. All such values follow the rules for the interpretation of the
d_ununion described above.DT_LOPROCthroughDT_HIPROCValues in this inclusive range are reserved for processor-specific semantics. If meanings are specified, the psABI supplement explains them. All such values follow the rules for the interpretation of the
d_ununion described above.
Except for the DT_NULL element at the end of the array,
and the relative order of DT_NEEDED
elements, entries may appear in any order.
Tag values not appearing in the table are reserved.
Name |
Value |
|---|---|
|
|
|
|
|
|
|
|
|
|
DF_ORIGINThis flag signifies that the object being loaded may make reference to the
$ORIGINsubstitution string (see Section 8.4.1, Substitution Sequences). The dynamic linker must determine the pathname of the object containing this entry when the object is loaded.DF_SYMBOLICIf this flag is set in a shared object library, the dynamic linker’s symbol resolution algorithm for references within the library is changed. Instead of starting a symbol search with the executable file, the dynamic linker starts from the shared object itself. If the shared object fails to supply the referenced symbol, the dynamic linker then searches the executable file and other shared objects as usual.
DF_TEXTRELIf this flag is not set, no relocation entry should cause a modification to a non-writable segment, as specified by the segment permissions in the program header table. If this flag is set, one or more relocation entries might request modifications to a non-writable segment, and the dynamic linker can prepare accordingly.
DF_BIND_NOWIf set in a shared object or executable, this flag instructs the dynamic linker to process all relocations for the object containing this entry before transferring control to the program. The presence of this entry takes precedence over a directive to use lazy binding for this object when specified through the environment or via
dlopen().DF_STATIC_TLSIf set in a shared object or executable, this flag instructs the dynamic linker to reject attempts to load this file dynamically. It indicates that the shared object or executable contains code using a static thread-local storage scheme. Implementations need not support any form of thread-local storage.
8.5. Hash Table¶
A hash table of Elf32_Word
objects supports symbol table access. The same table
layout is used for both the 32-bit and 64-bit file class.
Labels appear below
to help explain the hash table organization,
but they are not part of the specification.
Figure 8.1 Hash Table¶
The bucket array contains nbucket
entries, and the chain array contains nchain
entries; indexes start at 0.
Both bucket and chain
hold symbol table indexes.
Chain table entries parallel the symbol table.
The number of symbol table entries should equal
nchain;
so symbol table indexes also select chain table entries.
A hashing function (shown below) accepts a symbol name and returns a
value that may be used to compute a bucket index.
Consequently, if the hashing function returns the value
x for some name, bucket[x%nbucket] gives
an index, y,
into both the symbol table and the chain table.
If the symbol table entry is not the one desired,
chain[y] gives the next symbol table entry
with the same hash value.
One can follow the chain
links until either the selected symbol table entry
holds the desired name or the chain entry contains the value
STN_UNDEF.
unsigned long
elf_hash(const unsigned char *name)
{
unsigned long h = 0, g;
while (*name)
{
h = (h << 4) + *name++;
if (g = h & 0xf0000000)
h ^= g >> 24;
h &= ~g;
}
return h;
}
8.6. Initialization and Termination Functions¶
After the dynamic linker has built the process image and performed the relocations, each shared object and the executable file get the opportunity to execute some initialization functions. All shared object initializations happen before the executable file gains control.
Before the initialization functions for any object A is called, the initialization
functions for any other objects that object A depends on are called.
For these purposes, an object A depends on another object B,
if B appears in A’s list of needed objects (recorded in the DT_NEEDED
entries of the dynamic structure).
The order of initialization for circular dependencies is undefined.
The initialization of objects occurs by recursing through the needed entries of each object. The initialization functions for an object are invoked after the needed entries for that object have been processed. The order of processing among the entries of a particular list of needed objects is unspecified.
Note
Each psABI supplement may optionally further restrict the algorithm used to determine the order of initialization. Any such restriction, however, may not conflict with the rules described by this specification.
The following example illustrates two of the possible correct orderings
which can be generated for the example NEEDED lists.
In this example the a.out is dependent on b, d, and e.
b is dependent on d and f, while d is dependent on e and g.
From this information a dependency graph can be drawn.
The above algorithm on initialization will then allow the following
specified initialization orderings (among others).
Figure 8.2 Initialization Ordering Example¶
Similarly, shared objects and executable files may have termination
functions, which are executed with the atexit() mechanism after
the base process begins its termination sequence.
The termination functions for any object A must be called before
the termination functions for any other objects that object A depends
on. For these purposes, an object A depends on another object B,
if B appears in A’s list of needed objects (recorded in the DT_NEEDED
entries of the dynamic structure).
The order of termination for circular dependencies is undefined.
Finally, an executable file may have pre-initialization functions. These functions are executed after the dynamic linker has built the process image and performed relocations but before any shared object initialization functions. Pre-initialization functions are not permitted in shared objects.
Note
Complete initialization of system libraries may not have occurred when pre-initializations are executed, so some features of the system may not be available to pre-initialization code. In general, use of pre-initialization code can be considered portable only if it has no dependencies on system libraries.
The dynamic linker ensures that it will not execute any initialization, pre-initialization, or termination functions more than once.
Shared objects designate their
initialization and termination code in one of two ways.
First, they may specify the address of a function to execute
via the DT_INIT and DT_FINI entries in the dynamic structure,
described in Section 8.3, Dynamic Section.
Note
Note that the address of a function need not be the same as a pointer to a function as defined by the psABI supplement.
Shared objects may also (or instead) specify the address and size of
an array of function pointers. Each element of this
array is a pointer to a function to be executed by the dynamic linker.
Each array element is the size of a pointer in the
programming model followed by the object containing
the array. The address of the array of initialization
function pointers is specified by the DT_INIT_ARRAY
entry in the dynamic structure. Similarly, the address of
the array of pre-initialization functions is specified by
DT_PREINIT_ARRAY and the address of the array
of termination functions is specified by DT_FINI_ARRAY.
The size of each array is specified by the DT_INIT_ARRAYSZ,
DT_PREINIT_ARRAYSZ, and DT_FINI_ARRAYSZ
entries.
Note
The addresses contained in the initialization and termination arrays are function pointers as defined by the psABI supplement for each processor. On some architectures, a function pointer may not contain the actual address of the function.
The functions pointed to in the arrays
specified by DT_INIT_ARRAY and by DT_PREINIT_ARRAY
are executed by the dynamic
linker in the same order in which their addresses appear in
the array; those specified by DT_FINI_ARRAY
are executed in reverse order.
If an object contains both DT_INIT
and DT_INIT_ARRAY entries, the function referenced
by the DT_INIT entry is processed before those
referenced by the DT_INIT_ARRAY entry for that object.
If an object contains both DT_FINI
and DT_FINI_ARRAY entries, the functions referenced
by the DT_FINI_ARRAY entry are processed before the one
referenced by the DT_FINI entry for that object.
Note
Although the atexit() termination processing normally will be done,
it is not guaranteed to have executed upon process death.
In particular, the process will not execute the termination processing
if it calls _exit() or if the process dies because it received a signal
that it neither caught nor ignored.
The psABI supplement for each processor specifies whether the
dynamic linker is responsible for calling the executable file’s
initialization function or registering the executable file’s
termination function with atexit().
Termination functions specified by users via the atexit() mechanism
must be executed before any termination functions of shared objects.