An executable file that participates in
dynamic linking shall have one
PT_INTERP program header element.
During
exec(BA_OS),
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.
As ``Process Initialization'' in Chapter 3 of the processor supplement mentions, the interpreter 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.
mmap(KE_OS) and related services
[See ``Virtual Address Space'' in Chapter 3 of the processor
supplement].
Consequently, a shared object interpreter typically will
not conflict with the original executable file's
original segment addresses.
PT_INTERP to an executable file, telling the system to invoke
the dynamic linker as the program interpreter.
The locations of the system provided dynamic
linkers are processor specific.
Exec(BA_OS)
and the dynamic linker cooperate to
create the process image for the program, which entails
the following actions:
exec(BA_OS).
The link editor also constructs various data that assist the dynamic linker for executable and shared object files. As shown above in ``Program Header'', this data resides in loadable segments, making them available during execution. (Once again, recall the exact segment contents are processor-specific. See the processor supplement for complete information).
.dynamic section with type SHT_DYNAMIC
holds various data.
The structure residing at the
beginning of the section holds the addresses
of other dynamic linking information.
.hash section with type SHT_HASH
holds a symbol hash table.
.got and .plt sections with type
SHT_PROGBITS
hold two separate tables:
the global offset table and the procedure linkage table.
Chapter 3 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 [See ``System Library'' in Chapter 6], the dynamic linker participates in every ABI-conforming program execution.
As `Program Loading'' explains in the processor supplement, 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 [see exec(BA_OS)]
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=1
LD_BIND_NOW=on
LD_BIND_NOW=off
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 ``Procedure Linkage Table'' in this chapter of the processor
supplement for more information.
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_vald_ptr
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.
d_tag
| Name | Value | d_un |
Executable | Shared Object |
|---|---|---|---|---|
DT_NULL |
0 |
ignored | mandatory | mandatory |
DT_NEEDED |
1 |
d_val |
optional | optional |
DT_PLTRELSZ |
2 |
d_val |
optional | optional |
DT_PLTGOT |
3 |
d_ptr |
optional | optional |
DT_HASH |
4 |
d_ptr |
mandatory | mandatory |
DT_STRTAB |
5 |
d_ptr |
mandatory | mandatory |
DT_SYMTAB |
6 |
d_ptr |
mandatory | mandatory |
DT_RELA |
7 |
d_ptr |
mandatory | optional |
DT_RELASZ |
8 |
d_val |
mandatory | optional |
DT_RELAENT |
9 |
d_val |
mandatory | optional |
DT_STRSZ |
10 |
d_val |
mandatory | mandatory |
DT_SYMENT |
11 |
d_val |
mandatory | mandatory |
DT_INIT |
12 |
d_ptr |
optional | optional |
DT_FINI |
13 |
d_ptr |
optional | optional |
DT_SONAME |
14 |
d_val |
ignored | optional |
DT_RPATH |
15 |
d_val |
optional | ignored |
DT_SYMBOLIC |
16 |
ignored | ignored | optional |
DT_REL |
17 |
d_ptr |
mandatory | optional |
DT_RELSZ |
18 |
d_val |
mandatory | optional |
DT_RELENT |
19 |
d_val |
mandatory | optional |
DT_PLTREL |
20 |
d_val |
optional | optional |
DT_DEBUG |
21 |
d_ptr |
optional | ignored |
DT_TEXTREL |
22 |
ignored | optional | optional |
DT_JMPREL |
23 |
d_ptr |
optional | optional |
DT_BIND_NOW |
24 |
ignored | optional | optional |
DT_INIT_ARRAY |
25 |
d_ptr |
optional | optional |
DT_FINI_ARRAY |
26 |
d_ptr |
optional | optional |
DT_INIT_ARRAYSZ |
27 |
d_val |
optional | optional |
DT_FINI_ARRAYSZ |
28 |
d_val |
optional | optional |
DT_LOOS |
0x60000000 |
unspecified | unspecified | unspecified |
DT_HIOS |
0x6fffffff |
unspecified | unspecified | unspecified |
DT_LOPROC |
0x70000000 |
unspecified | unspecified | unspecified |
DT_HIPROC |
0x7fffffff |
unspecified | unspecified | unspecified |
DT_NULLDT_NULL tag marks the end of the
_DYNAMIC array.
DT_NEEDEDDT_STRTAB code.
See
``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_PLTRELSZDT_JMPREL is present, a
DT_PLTRELSZ must accompany it.
DT_PLTGOTDT_HASHDT_SYMTAB
element.
DT_STRTABDT_SYMTABElf32_Sym
entries for the 32-bit class of files and Elf64_Sym
entries for the 64-bit class of files.
DT_RELAElf32_Rela for the 32-bit file class
or Elf64_Rela for 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
catenates 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 have
DT_RELASZ and DT_RELAENT elements.
When relocation is ``mandatory'' for a file, either
DT_RELA or DT_REL may occur (both are permitted but not required).
DT_RELASZDT_RELA relocation table.
DT_RELAENTDT_RELA relocation entry.
DT_STRSZDT_SYMENTDT_INITDT_FINIDT_SONAMEDT_STRTAB entry.
See
``Shared Object Dependencies''
below for more
information about these names.
DT_RPATHDT_STRTAB entry.
DT_SYMBOLICDT_RELDT_RELA,
except its table has implicit addends, such as
Elf32_Rel for the 32-bit file class
or Elf64_Rel for the 64-bit file class.
If this element is present, the dynamic structure must also have
DT_RELSZ and DT_RELENT elements.
DT_RELSZDT_REL relocation table.
DT_RELENTDT_REL relocation entry.
DT_PLTRELd_val member holds DT_REL or DT_RELA,
as appropriate.
All relocations in a procedure linkage table must use
the same relocation.
DT_DEBUGDT_TEXTRELDT_JMPRELd_ptr
member 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 types
DT_PLTRELSZ and DT_PLTREL
must also be present.
DT_BIND_NOWdlopen(BA_LIB).
DT_INIT_ARRAYDT_INIT
and DT_INIT_ARRAY entries.
DT_FINI_ARRAYDT_FINI
and DT_FINI_ARRAY entries.
DT_INIT_ARRAYSZDT_INIT_ARRAY entry.
If an object has a DT_INIT_ARRAY entry, it must
also have a DT_INIT_ARRAYSZ entry.
DT_FINI_ARRAYSZDT_FINI_ARRAY entry.
If an object has a DT_FINI_ARRAY entry, it must
also have a DT_FINI_ARRAYSZ entry.
DT_LOOS through DT_HIOSDT_LOPROC through DT_HIPROC
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.
When the dynamic linker creates the memory segments for
an object file, the dependencies (recorded in
DT_NEEDED entries of the dynamic structure)
tell what shared objects are needed to
supply the program's services.
By repeatedly connecting referenced shared objects and
their dependencies, the dynamic linker builds a complete process image.
When resolving symbolic references, the dynamic linker
examines the symbol tables with a breadth-first search.
That is, it first looks at the symbol table of the
executable program itself, then at the symbol tables
of the DT_NEEDED entries (in order),
and then at the second level DT_NEEDED entries, and
so on. Shared object files must be readable by the process;
other permissions are not required.
Even when a shared object is referenced multiple
times in the dependency list, the dynamic linker will
connect the object only once to the process.
Names in the dependency list are copies either of the
DT_SONAME strings or the path names of the shared objects used to build
the object file.
For example, if the link editor builds an executable
file using one shared object with a
DT_SONAME entry of lib1
and another shared object library with the path name
/usr/lib/lib2, the executable file will contain
lib1 and /usr/lib/lib2 in its dependency list.
If a shared object name has one or more slash (/)
characters anywhere in the name, such as /usr/lib/lib2
or directory/file, the dynamic linker uses that string directly
as the path name.
If the name has no slashes, such as lib1,
three facilities specify shared object path searching, with the
following precedence.
DT_RPATH may give a string that
holds a list of directories, separated by colons (:).
For example, the string
/home/dir/usr/lib:/home/dir2/usr/lib:
tells the dynamic linker to
search first the directory
/home/dir/lib, then /home/dir2/usr/lib,
and then the current directory to find dependencies.
LD_LIBRARY_PATH
in the process environment [see exec(BA_OS)]
may hold a list of directories as above, optionally
followed by a semicolon (;) and
another directory list.
The following values would be equivalent to the previous example:
LD_LIBRARY_PATH=/home/dir/usr/lib:/home/dir2/usr/lib:
LD_LIBRARY_PATH=/home/dir/usr/lib;/home/dir2/usr/lib:
LD_LIBRARY_PATH=/home/dir/usr/lib:/home/dir2/usr/lib:;
All LD_LIBRARY_PATH directories are searched after those from
DT_RPATH.
Although some programs (such as the link editor) treat the lists
before and after the semicolon differently,
the dynamic linker does not.
Nevertheless, the dynamic linker accepts the semicolon
notation, with the semantics described previously.
/usr/lib.
For security, the dynamic linker ignores
LD_LIBRARY_PATH for set-user and
set-group ID programs.
It does, however, search DT_RPATH directories
and /usr/lib.
The same restriction may be applied to processes that have more than
minimal privileges on systems with installed extended security
systems.
This section requires processor-specific information.
The System V Application Binary Interface supplement
for the desired processor describes the details.
This section requires processor-specific information.
The System V Application Binary Interface supplement
for the desired processor describes the details.
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.
nbucket |
nchain |
bucket[0] |
chain[0] |
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;
}
Before the initialization code for any object A is called, the initialization
code 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 code for an object is 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.
Each processor 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.

Similarly, shared objects may have termination functions,
which are executed with the
atexit(BA_OS) mechanism after the base process begins its
termination sequence.
The order in which the dynamic linker calls termination functions
is the exact reverse order of their corresponding initialization
functions.
If a shared object has a termination function, but no initialization
function, the termination function will execute in the order it
would have as if the shared object's initialization function was
present.
The dynamic linker ensures that it will not execute any 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
``Dynamic Section''
above.
Alternatively, shared objects may 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, in the
order listed in the array, 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.
The address of the array of termination
function pointers is specified by the DT_FINI_ARRAY
entry. The size of each array is specified by
the DT_INIT_ARRAYSZ and DT_FINI_ARRAYSZ
entries.
Typically, the code for the initialization and termination
functions or the array of function pointers reside in the
.init
and
.fini
sections, mentioned in
``Sections''
of Chapter 4.
Although the
atexit(BA_OS)
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 [see
exit(BA_OS)]
or if the process dies because it received a signal
that it neither caught nor ignored.
The dynamic linker is not responsible for calling the executable
file's
initialization function or registering the executable file's
termination function with
atexit(BA_OS).
Termination functions specified by users via the
atexit(BA_OS)
mechanism
must be executed before any termination functions of shared objects.