Program loading and dynamic linking

This section describes how the Executable and Linking Format (ELF) is used in the construction and execution of programs.

Program Loading

As the system creates or augments a process image, it logically copies a file's segment to a virtual memory segment. When – and if – the system physically reads the file depends on the program's execution behavior, on the system load, and so on. A process does not require a physical page until it references the logical page during execution, and processes commonly leave many pages unreferenced. Therefore, if physical reads can be delayed they can frequently be dispensed with, improving system performance. To obtain this efficiency in practice, executable and shared object files must have segment images of which the offsets and virtual addresses are congruent modulo the page size.

Virtual addresses and file offsets for the zSeries processor family segments are congruent modulo the system page size. The value of the p_align field of each program header in a shared object file must be a multiple of the system page size. Figure 1 is an example of an executable file assuming an executable program linked with a base address of 0x80000000 (2 Gbytes).

Figure 1. Executable File Example

Table 1. Program Header Segments

Member

Text

Data

p_type

PT_LOAD

PT_LOAD

p_offset

0x0

0x1bf58

p_vaddr

0x80000000

0x8001cf58

p_paddr

unspecified

unspecified

p_filesz

0x1bf58

0x17c4

p_memsz

0x1bf58

0x2578

p_flags

PF_R+PF_X

PF_R+PF_W

p_align

0x1000

0x1000

Although the file offsets and virtual addresses are congruent modulo 4 Kbytes for both text and data, up to four file pages can hold impure text or data (depending on page size and file system block size).

Logically, the system enforces memory permissions as if each segment were complete and separate; segment addresses are adjusted to ensure that each logical page in the address space has a single set of permissions. In the example in Table 1 the file region holding the end of text and the beginning of data is mapped twice; at one virtual address for text and at a different virtual address for data.

The end of the data segment requires special handling for uninitialized data, which the system defines to begin with zero values. Thus if the last data page of a file includes information beyond the logical memory page, the extraneous data must be set to zero by the loader, rather than to the unknown contents of the executable file. 'Impurities' in the other three segments are not logically part of the process image, and whether the system clears them is unspecified. The memory image for the program in Table 1 is presented in Figure 2.

Figure 2. Process Image Segments

One aspect of segment loading differs between executable files and shared objects. Executable file segments may contain absolute code. For the process to execute correctly, the segments must reside at the virtual addresses assigned when building the executable file, with the system using the p_vaddr values unchanged as virtual addresses.

On the other hand, shared object segments typically contain position-independent code. This allows a segment's virtual address to change from one process to another, without invalidating execution behavior. Though the system chooses virtual addresses for individual processes, it maintains the "relative positions" of the segments. Because position-independent code uses relative addressing between segments, the difference between virtual addresses in memory must match the difference between virtual addresses in the file. Table 2 shows possible shared object virtual address assignments for several processes, illustrating constant relative positioning. The table also illustrates the base address computations.

Table 2. Shared Object Segment Example for 42–bit address space

Source

Text

Data

Base Address

File

0x00000000200

0x0000002a400

Process 1

0x20000000000

0x2000002a400

0x20000000000

Process 2

0x20000010000

0x2000003a400

0x20000010000

Process 3

0x20000020000

0x2000004a400

0x20000020000

Process 4

0x20000030000

0x2000005a400

0x20000030000