工具接口标准(TIS)可执行链接格式(ELF)规范版本 1.2
翻译以中英对照方式,英语水平有限,如有翻译不当的地方,请谅解。
Program Loading and Dynamic Linking
程序加载和动态链接
ProgramLoading
程序加载
As the system creates or augmentsa process image, it logically copies a file's segment to a virtual memorysegment. When—and if— the system physically reads the file depends on theprogram's execution behavior, system load, and so on. A process does not requirea physical page unless it references the logical page during execution, andprocesses commonly leave many pages unreferenced. Therefore delaying physicalreads frequently obviates them, improving system performance. To obtain thisefficiency in practice, executable and shared object files must have segmentimages whose file offsets and virtual addresses are congruent, modulo the pagesize.
当系统创建或增加一个进程映像,它逻辑上拷贝一个文件的分段到一个虚拟内存分段。当假如系统在物理上依赖于程序执行行为来读取文件时,系统加载对应的分段。一个进程不会请求一个物理页面,除非它在执行期间引用了对应的逻辑页面,并且系统通常许多页面都是未引用的。延迟物理上频繁的读取页面来提高系统性能。为了在实际上获得这样的效率,可执行和共享对象文件必须使分段映像的文件偏移和虚拟地址是一致的,都以页面大小取模。
Virtual addresses and file offsetsfor the Intel architecture segments are congruent modulo 4KB (0x1000) or largerpowers of 2. Because 4KB is the maximum page size for the Intel Architecture,the files will be suitable for paging regardless of physical page size.
虚拟地址和文件偏移对Intel架构下的分段来说是一致的,都以4K(0x1000)取模或更大的2的幂。因为4KB是Intel架构最大页面大小,所以不管物理页面大小,文件会适合分页。
FigureA-4. Executable File Example
图A-4.可执行文件例子
File Offset | File | Virtual Address |
0 | Program Header Table |
|
| Other Information |
|
0x100 | Text Segment … 0x2be00 Bytes | 0x8048100
0x8073eff |
0x2bf00 | Data Segment … 0x4ee00 Bytes | 0x8074f00
0x8079cff |
0x30d00 | Other Information … |
|
Figure A-5 describes theExecutable File Example in Figure A-4.
图A-5描述了图A-4这个可执行文件例子
FigureA-5. Program Header Segments
图A-5.程序头分段
成员(Member) | 文本(Text) | 数据(Data) |
p_type p_offset p_vaddr p_paddr p_filesz p_memsz p_flags p_align | PT_LOAD 0x100 0x8048100 unspecified 0x2be00 0x2be00 PF_R+PF_X 0x1000 | PT_LOAD 0x2bf00 0x8074f00 unspecified 0x4e00 0x5e24 PF_R+PF_W+PF_X 0x1000 |
Although the example's fileoffsets and virtual addresses are congruent modulo 4KB for both text and data,up to four file pages hold impure text or data (depending on page size and filesystem block size).
• The first text page contains the ELF header, the program header table,and other information.
• The last text page holds a copy of the beginning of data.
• The first data page has a copy of the end of text.
• The last data page may contain file information not relevant to therunning process.
虽然这个例子的文件偏移和虚拟地址对文本和数据都是一致的以4K取模,直到4K文件页有不纯的文本或数据(依赖于页面大小和文件系统块大小).
- 第一个文本页包含ELF头,程序头和其它信息;
- 最后一个文本页有数据开始的部分的拷贝;
- 第一个数据页有文本尾部的拷贝;
- 最后的数据页可能包含和运行进程不相关的文件信息。
Logically, the system enforces thememory permissions as if each segment were complete and separate; segments'addresses are adjusted to ensure each logical page in the address space has asingle set of permissions. In the example above, the region of the file holdingthe end of text and the beginning of data will be mapped twice: at one virtualaddress for text and at a different virtual address for data.
逻辑上,系统强制内存权限就像每个分段是完整的及分离的;分段地址被调整到确保每个逻辑页面在地址空间有一个单一的权限集。在上面的例子中,文件包含文本的尾部及数据的开始部分的区域将被映射两次:在一个文本虚拟地址和一个不同的数据虚拟地址。
The end of the data segmentrequires special handling for uninitialized data, which the system defines tobegin with zero values. Thus if a file's last data page includes informationnot in the logical memory page, the extraneous data must be set to zero, not theunknown contents of the executable file. "Impurities'' in the other threepages are not logically part of the process image; whether the system expungesthem is unspecified. The memory image for this program follows, assuming 4 KB(0x1000 pages).
数据分段尾部的未初始化数据需要特殊的处理,系统使用0来初始化该部分。从而假如一个文件最后的数据页面包含不在逻辑内存页面的信息,这些外部的数据必须被设置为0,不是可执行文件的未知内容。在其它4个页面的“杂质”不是这个进程映像的逻辑部分;系统是否擦出它们是未指定的。以下这个程序的内存映像假设4KB(0x1000页面)对齐。
FigureA-6. Process Image Segments Example
图A-6.进程映像分段举例
Virtual Address | Contents | Segment |
0x8048000 | Header Padding 0x100 Bytes |
|
0x8048100 | Text Segment … 0x2be00 Bytes |
Text |
0x8073f00 | Data Padding 0x100 Bytes |
|
0x8074000 | Text Padding 0xf00 Bytes |
|
0x8074f00 | Data Segment … 0x4e00 Bytes |
Data |
0x80779d00 | 0x1024 Zero Bytes Uninitialized Data |
|
0x807ad24 | 0x2dc Zero Bytes Page Padding |
|
One aspect of segment loadingdiffers between executable files and shared objects. Executable file segmentstypically contain absolute code. To let the process execute correctly, thesegments must reside at the virtual addresses used to build the executablefile. Thus the system uses the p_vaddr values unchanged as virtual addresses.
在可执行文件盒共享对象间,分段加载的样子是不同的。可执行文件分段有代表性的包含绝对代码。为了使进程正确执行,分段必须位于用于建立可执行文件的虚拟地址。从而系统使用不改变的p_vaddr值作为虚拟地址。
On the other hand, shared object segmentstypically contain position-independent code. This lets a segment's virtualaddress change from one process to another, without invalidating executionbehavior. Though the system chooses virtual addresses for individual processes,it maintains the segments relative positions. Because position-independent codeuses relative addressing between segments, the difference between virtualaddresses in memory must match the difference between virtual addresses in thefile. The following table shows possible shared object virtual addressassignments for several processes, illustrating constant relative positioning.The table also illustrates the base address computations.
另一方面,共享对象分段有代表性的包含位置无关代码。这使得一个分段的虚拟地址能够在进程间不同,而不会导致执行行为无效。尽管系统为每个进程选择虚拟地址,但它维护分段的相对位置。因为位置无关的代码在分段间使用相对地址,在内存中虚拟地址的不同必须和文件中虚拟地址的不同相匹配。以下的表格显示了为一些进程分配的可能的共享对象虚拟地址,阐明了不变的相对位置。这个表格也阐明了基地址计算。
FigureA-7. Shared Object Segment AddressesExample
图A-7.共享对象分段地址举例
源(Source) | 文本(Text) | 数据(Data) | 基地址(Base Address) |
File Process 1 Process 2 Process 3 Process 4 | 0x200 0x80000200 0x80081200 0x900c0200 0x900c6200 | 0x2a400 0x8002a400 0x800ab400 0x900ea400 0x900f0400 | 0x0 0x80000000 0x80081000 0x900c0000 0x900c6000 |