TIS Committee
May 1995
Tool Interface Standard (TIS)
Executable and Linking Format (ELF)
Specification
Version 1.2
i
The TIS Committee grants you a non-exclusive, worldwide, royalty-free license to use the information disclosed in this Specification
to make your software TIS-compliant; no other license, express or implied, is granted or intended hereby.
The TIS Committee makes no warranty for the use of this standard.
THE TIS COMMITTEE SPECIFICALLY DISCLAIMS ALL WARRANTIES, EXPRESS AND IMPLIED, AND ALL LIABILITY, INCLUDING CONSEQUENTIAL AND OTHER INDIRECT DAMAGES, FOR THE USE OF THESE SPECIFICATION AND THE INFORMATION CONTAINED IN IT, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PROPRIETARY RIGHTS. THE TIS COMMITTEE
DOES NOT ASSUME ANY RESPONSIBILITY FOR ANY ERRORS THAT MAY APPEAR IN THE SPECIFICATION, NOR ANY
RESPONSIBILITY TO UPDATE THE INFORMATION CONTAINED IN THEM.
The TIS Committee retains the right to make changes to this specification at any time without notice.
IBM is a registered trademark and OS/2 is a trademark of International Business Machines Corporation.
The Intel logo is a registered trademark and i386 and Intel386 are trademarks of Intel Corporation and may be used only to identify
Intel products.
Microsoft, Microsoft C, MS, MS-DOS, Windows, and XENIX are registered trademarks of Microsoft Corporation.
Phoenix is a registered trademark of Phoenix Technologies, Ltd.
UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Limited.
* Other brands and names are the property of their respective owners.
iii
Preface
This Executable and Linking Format Specification, Version 1.2, is the result of the work of the
Tool Interface Standards (TIS) Committee–an association of members of the microcomputer
industry formed to work toward standardization of the software interfaces visible to
development tools for 32-bit Intel Architecture operating environments. Such interfaces
include object module formats, executable file formats, and debug record information and
formats.
The goal of the committee is to help streamline the software development process throughout
the microcomputer industry, currently concentrating on 32-bit operating environments. To that
end, the committee has developed specifications–some for file formats that are portable across
leading industry operating systems, and others describing formats for 32-bit Windows*
operating systems. Originally distributed collectively as the TIS Portable Formats
Specifications Version 1.1, these specifications are now separated and distributed individually.
TIS Committee members include representatives from Absoft, Autodesk, Borland International
Corporation, IBM Corporation, Intel Corporation, Lahey, Lotus Corporation, MetaWare
Corporation, Microtec Research, Microsoft Corporation, Novell Corporation, The Santa Cruz
Operation, and WATCOM International Corporation. PharLap Software Incorporated and
Symantec Corporation also participated in the specification definition efforts.
This specification like the others in the TIS collection of specifications is based on existing,
proven formats in keeping with the TIS Committee’s goal to adopt, and when necessary, extend
existing standards rather than invent new ones.
About ELF: Executable and Linking Format
The Executable and Linking Format was originally developed and published by UNIX System
Laboratories (USL) as part of the Application Binary Interface (ABI). The Tool Interface
Standards committee (TIS) has selected the evolving ELF standard as a portable object file
format that works on 32-bit Intel Architecture environments for a variety of operating systems.
The ELF standard is intended to streamline software development by providing developers
with a set of binary interface definitions that extend across multiple operating environments.
This should reduce the number of different interface implementations, thereby reducing the
need for recoding and recompiling code.
About This Document
This document is intended for developers who are creating object or executable files on various
32-bit environment operating systems. In order to extend ELf into different operating systems,
the current ELF version 1.2 document has been reorganized based on operating system-specific
information. It is divided into the following three books:
• Book I: Executable and Linking Format, describes the object file format called ELF. This book
also contains an appendix that describes historical references and lists processor and operating
system reserved names and words.
• Book II: Processor Specific (Intel Achitecture), conveys hardware-specific ELF information,
such as Intel Architecture information.
• Book III: Operating System Specific, describes ELF information that is operating system
dependent, such as System V Release 4 information. This book also contains an appendix that
describes ELF information that is both operating system and processor dependent.
Contents v
Contents
Preface
Book I: Executable and Linking Format (ELF)
1. Object Files
Introduction………………………………………………………………………………………….. 1-1
File Format ………………………………………………………………………………………….. 1-1
ELF Header …………………………………………………………………………………………. 1-4
ELF Identification………………………………………………………………………………….. 1-6
Sections………………………………………………………………………………………………. 1-9
Special Sections …………………………………………………………………………………… 1-15
String Table …………………………………………………………………………………………. 1-18
Symbol Table……………………………………………………………………………………….. 1-19
Symbol Values……………………………………………………………………………………… 1-22
Relocation……………………………………………………………………………………………. 1-23
2. Program Loading and Dynamic Linking
Introduction………………………………………………………………………………………….. 2-1
Program Header …………………………………………………………………………………… 2-2
Program Loading ………………………………………………………………………………….. 2-7
Dynamic Linking …………………………………………………………………………………… 2-8
A. Reserved Names
Introduction………………………………………………………………………………………….. A-1
Special Sections Names………………………………………………………………………… A-2
Dynamic Section Names ……………………………………………………………………….. A-3
Pre-existing Extensions …………………………………………………………………………. A-4
Book II: Processor Specific (Intel Architecture)
1. Object Files
Introduction………………………………………………………………………………………….. 1-1
ELF Header …………………………………………………………………………………………. 1-2
Relocation……………………………………………………………………………………………. 1-3
Contents
vi
Book III: Operating System Specific
(UNIX System V Release 4)
1. Object Files
Introduction………………………………………………………………………………………….. 1-1
Sections………………………………………………………………………………………………. 1-2
Symbol Table……………………………………………………………………………………….. 1-5
2. Program Loading and Dynamic Linking
Introduction………………………………………………………………………………………….. 2-7
Program Header …………………………………………………………………………………… 2-8
Dynamic Linking …………………………………………………………………………………… 2-12
3. Intel Architecture and System V Release 4 Dependencies
Introduction………………………………………………………………………………………….. A-1
Sections………………………………………………………………………………………………. A-2
Symbol Table……………………………………………………………………………………….. A-3
Relocation……………………………………………………………………………………………. A-4
Program Loading and Dynamic Linking……………………………………………………. A-7
Table of Contents vii
List of Figures
Book I: Executable and Linking Format (ELF)
Figure 1-1. Object File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1
Figure 1-2. 32-Bit Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
Figure 1-3. ELF Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4
Figure 1-4. e_ident[] Identification Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6
Figure 1-5. Data Encoding ELFDATA2LSB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8
Figure 1-6. Data Encoding ELFDATA2MSB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8
Figure 1-7. Special Section Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
Figure 1-8. Section Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
Figure 1-9. Section Types, sh_type. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11
Figure 1-10. Section Header Table Entry: Index 0 . . . . . . . . . . . . . . . . . . . . . . . . . 1-13
Figure 1-11. Section Attribute Flags, sh_flags . . . . . . . . . . . . . . . . . . . . . . . . . . 1-14
Figure 1-12. sh_link and sh_info Interpretation . . . . . . . . . . . . . . . . . . . . . . . 1-14
Figure 1-13. Special Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-15
Figure 1-14. String Table Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-18
Figure 1-15. Symbol Table Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-19
Figure 1-16. Symbol Binding, ELF32_ST_BIND . . . . . . . . . . . . . . . . . . . . . . . . . . 1-20
Figure 1-17. Symbol Types, ELF32_ST_TYPE . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-21
Figure 1-18. Symbol Table Entry: Index 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-22
Figure 1-19. Relocation Entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23
Figure 2-1. Program Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Figure 2-2. Segment Types, p_type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
Figure 2-3. Note Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5
Figure 2-4. Example Note Segment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-6
Figure A-1. Special Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-2
Figure A-2. Dynamic Array Tags, d_tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-3
Book II: Processor Specific (Intel Architecture)
Figure 1-1. Intel Identification, e_ident . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
Figure 1-2. Relocatable Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3
Figure 1-3. Relocation Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4
viii Table of Contents
Book III: Operating System Specific
(UNIX System V Release 4)
Figure 1-1. sh_link and sh_info Interpretation . . . . . . . . . . . . . . . . . . . . . . . 1-2
Figure 1-2. Special Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3
Figure 2-1. Segment Types, p_type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Figure 2-2. Segment Flag Bits, p_flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
Figure 2-3. Segment Permissions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4
Figure 2-4. Text Segment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5
Figure 2-5. Data Segment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5
Figure 2-6. Dynamic Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
Figure 2-7. Dynamic Array Tags, d_tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-9
Figure 2-8. Symbol Hash Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14
Figure 2-9. Hashing Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14
Figure 2-10. Initialization Ordering Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16
Figure A-1. Special Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-2
Figure A-2. Relocatable Fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-4
Figure A-3. Relocation Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-5
Figure A-4. Executable File Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-7
Figure A-5. Program Header Segments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-8
Figure A-6. Process Image Segments Example. . . . . . . . . . . . . . . . . . . . . . . . . . . A-9
Figure A-7. Shared Object Segment Addresses Example . . . . . . . . . . . . . . . . . . A-10
Figure A-8. Global Offset Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-11
Figure A-9. Absolute Procedure Linkage Table . . . . . . . . . . . . . . . . . . . . . . . . . . . A-12
Figure A-10. Position-Independent Procedure Linkage Table . . . . . . . . . . . . . . . . A-13
Book I:
Executable and Linking Format (ELF)
Table of Contents xi
Contents
Book I: Executable and Linking Format (ELF)
1 Object Files
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1
File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-1
Data Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
Character Representations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3
ELF Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4
ELF Identification . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-6
Sections. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
Special Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-15
String Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-18
Symbol Table. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-19
Symbol Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-22
Relocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23
2 Program Loading and Dynamic Linking
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-1
Program Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Note Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5
Program Loading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
Dynamic Linking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
A Reserved Names
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-1
Special Sections Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-2
Dynamic Section Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-3
Pre-existing Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . A-4
Contents
xii Book I: Executable and Linking Format (ELF)
Table of Contents xiii
Figures
1-1. Object File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-1
1-2. 32-Bit Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-2
1-3. ELF Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-4
1-4. e_ident[] Identification Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-6
1-5. Data Encoding ELFDATA2LSB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-8
1-6. Data Encoding ELFDATA2MSB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-8
1-7. Special Section Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-9
1-8. Section Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-10
1-9. Section Types, sh_type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-11
1-10. Section Header Table Entry: Index 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-13
1-11. Section Attribute Flags, sh_flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-14
1-12. sh_link and sh_info Interpretation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-14
1-13. Special Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-15
1-14. String Table Indexes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-18
1-15. Symbol Table Entry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-19
1-16. Symbol Binding, ELF32_ST_BIND . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-20
1-17. Symbol Types, ELF32_ST_TYPE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-21
1-18. Symbol Table Entry: Index 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-22
1-19. Relocation Entries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1-23
2-1. Program Header . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-2
2-2. Segment Types, p_type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-3
2-3. Note Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-5
2-4. Example Note Segment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2-6
A-1. Special Sections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A-2
A-2. Dynamic Array Tags, d_tag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A-3
OBJECT FILES 1-1
Introduction
This chapter describes the object file format, called ELF (Executable and Linking Format).
There are three main types of object files.
• A relocatable file holds code and data suitable for linking with other object files to create an
executable or a shared object file.
• An executable file holds a program suitable for execution.
• A shared object file holds code and data suitable for linking in two contexts. First, the link
editor may process it with other relocatable and shared object files to create another object file.
Second, the dynamic linker combines it with an executable file and other shared objects to
create a process image.
Created by the assembler and link editor, object files are binary representations of programs
intended to execute directly on a processor. Programs that require other abstract machines are
excluded.
After the introductory material, this chapter focuses on the file format and how it pertains to
building programs. Chapter 2 also describes parts of the object file, concentrating on the
information necessary to execute a program.
File Format
Object files participate in program linking (building a program) and program execution
(running a program). For convenience and efficiency, the object file format provides parallel
views of a file’s contents, reflecting the differing needs of these activities. Figure 1-1 shows
an object file’s organization.
Figure 1-1. Object File Format
OSD1980
ELF Header |
Program Header Table optional |
Section 1 |
Section Header Table |
. . . |
Sectionn |
. . . |
. . . |
Linking View
ELF Header |
Program Header Table |
Segment 1 |
Section Header Table optional |
. . . |
Segment 2 |
Execution View
Introduction
1-2 Book I: ELF (Executable and Linking Format)
An ELF header resides at the beginning and holds a “road map” describing the file’s
organization. Sections hold the bulk of object file information for the linking view: instructions,
data, symbol table, relocation information, and so on. Descriptions of special sections appear
later in this section. Chapter 2 also describes segments and the program execution view of the
file.
A program header table, if present, tells the system how to create a process image. Files used
to build a process image (execute a program) must have a program header table; relocatable
files do not need one. A section header table contains information describing the file’s sections.
Every section has an entry in the table; each entry gives information such as the section name,
the section size, and so on. Files used during linking must have a section header table; other
object files may or may not have one.
NOTE. Although the figure shows the program header table immediately after the
ELF header, and the section header table following the sections, actual
files may differ. Moreover, sections and segments have no specified
order. Only the ELF header has a fixed position in the file.
Data Representation
As described here, the object file format supports various processors with 8-bit bytes and 32-bit
architectures. Nevertheless, it is intended to be extensible to larger (or smaller) architectures.
Object files therefore represent some control data with a machine-independent format, making
it possible to identify object files and interpret their contents in a common way. Remaining
data in an object file use the encoding of the target processor, regardless of the machine on
which the file was created.
All data structures that the object file format defines follow the “natural” size and alignment
guidelines for the relevant class. If necessary, data structures contain explicit padding to ensure
4-byte alignment for 4-byte objects, to force structure sizes to a multiple of 4, and so on. Data
also have suitable alignment from the beginning of the file. Thus, for example, a structure
containing an Elf32_Addr member will be aligned on a 4-byte boundary within the file.
For portability reasons, ELF uses no bit fields.
Figure 1-2. 32-Bit Data Types
Name Size Alignment Purpose
Elf32_Addr Elf32_Half Elf32_Off Elf32_Sword Elf32_Word unsigned char |
4 2 4 4 4 1 |
4 2 4 4 4 1 |
Unsigned program address Unsigned medium integer Unsigned file offset Signed large integer Unsigned large integer Unsigned small integer |
Introduction
OBJECT FILES 1-3
Character Representations
This section describes the default ELF character representation and defines the standard
character set used for external files that should be portable among systems. Several external
file formats represent control information with characters. These single-byte characters use the
7-bit ASCII character set. In other words, when the ELF interface document mentions character
constants, such as, ‘/’ or ‘n’ their numerical values should follow the 7-bit ASCII guidelines.
For the previous character constants, the single-byte values would be 47 and 10, respectively.
Character values outside the range of 0 to 127 may occupy one or more bytes, according to the
character encoding. Applications can control their own character sets, using different character
set extensions for different languages as appropriate. Although TIS-conformance does not
restrict the character sets, they generally should follow some simple guidelines.
• Character values between 0 and 127 should correspond to the 7-bit ASCII code. That is,
character sets with encodings above 127 should include the 7-bit ASCII code as a subset.
• Multibyte character encodings with values above 127 should contain only bytes with values
outside the range of 0 to 127. That is, a character set that uses more than one byte per character
should not “embed” a byte resembling a 7-bit ASCII character within a multibyte, non-ASCII
character.
• Multibyte characters should be self-identifying. That allows, for example, any multibyte
character to be inserted between any pair of multibyte characters, without changing the
characters’ interpretations.
These cautions are particularly relevant for multilingual applications.
NOTE. There are naming conventions 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. However, pre-existing processor extensions not using this
convention will be supported.
Pre-existing Extensions
DT_JMP_REL
1-4 Book I: ELF (Executable and Linking Format)
ELF Header
Some object file control structures can grow, because the ELF header contains their actual sizes.
If the object file format changes, a program may encounter control structures that are larger or
smaller than expected. Programs might therefore ignore “extra” information. The treatment of
“missing” information depends on context and will be specified when and if extensions are
defined.
e_ident The initial bytes mark the file as an object file and provide machine-independent
data with which to decode and interpret the file’s contents. Complete descriptions
appear below, in “ELF Identification.”
e_type This member identifies the object file type.
Figure 1-3. ELF Header
#define EI_NIDENT 16
typedef struct {
unsigned char e_ident[EI_NIDENT];
Elf32_Half e_type;
Elf32_Half e_machine;
Elf32_Word e_version;
Elf32_Addr e_entry;
Elf32_Off e_phoff;
Elf32_Off e_shoff;
Elf32_Word e_flags;
Elf32_Half e_ehsize;
Elf32_Half e_phentsize;
Elf32_Half e_phnum;
Elf32_Half e_shentsize;
Elf32_Half e_shnum;
Elf32_Half e_shstrndx;
} Elf32_Ehdr;
Name Value Meaning
ET_NONE ET_REL ET_EXEC ET_DYN ET_CORE ET_LOPROC ET_HIPROC |
0 1 2 3 4 0xff00 0xffff |
No file type Relocatable file Executable file Shared object file Core file Processor-specific Processor-specific |
ELF Header
OBJECT FILES 1-5
Although the core file contents are unspecified, type ET_CORE is reserved to mark
the file type. Values from ET_LOPROC through ET_HIPROC (inclusive) are
reserved for processor-specific semantics. Other values are reserved and will be
assigned to new object file types as necessary.
e_machine This member’s value specifies the required architecture for an individual file.
Other values are reserved and will be assigned to new machines as necessary.
Processor-specific ELF names use the machine name to distinguish them. For
example, the flags mentioned below use the prefix EF_; a flag named WIDGET for
the EM_XYZ machine would be called EF_XYZ_WIDGET.
e_version This member identifies the object file version.
The value 1 signifies the original file format; extensions will create new versions
with higher numbers. The value of EV_CURRENT, though given as 1 above, will
change as necessary to reflect the current version number.
e_entry This member gives the virtual address to which the system first transfers control,
thus starting the process. If the file has no associated entry point, this member holds
zero.
e_phoff This member holds the program header table’s file offset in bytes. If the file has no
program header table, this member holds zero.
e_shoff This member holds the section header table’s file offset in bytes. If the file has no
section header table, this member holds zero.
e_flags This member holds processor-specific flags associated with the file. Flag names
take the form EF_machine_flag.
e_ehsize This member holds the ELF header’s size in bytes.
Name Value Meaning
ET_NONE EM_M32 EM_SPARC EM_386 EM_68K EM_88K EM_860 EM_MIPS EM_MIPS_RS4_BE RESERVED |
0 1 2 3 4 5 7 8 10 11-16 |
No machine AT&T WE 32100 SPARC Intel Architecture Motorola 68000 Motorola 88000 Intel 80860 MIPS RS3000 Big-Endian MIPS RS4000 Big-Endian Reserved for future use |
Name Value Meaning
EV_NONE EV_CURRENT |
0 1 |
Invalid versionn Current version |
ELF Header
1-6 Book I: ELF (Executable and Linking Format)
e_phentsize This member holds the size in bytes of one entry in the file’s program header table;
all entries are the same size.
e_phnum This member holds the number of entries in the program header table. Thus the
product of e_phentsize and e_phnum gives the table’s size in bytes. If a file
has no program header table, e_phnum holds the value zero.
e_shentsize This member holds a section header’s size in bytes. A section header is one entry
in the section header table; all entries are the same size.
e_shnum This member holds the number of entries in the section header table. Thus the
product of e_shentsize and e_shnum gives the section header table’s size in
bytes. If a file has no section header table, e_shnum holds the value zero.
e_shstrndx This member holds the section header table index of the entry associated with the
section name string table. If the file has no section name string table, this member
holds the value SHN_UNDEF. See “Sections” and “String Table” below for more
information.
ELF Identification
As mentioned above, ELF provides an object file framework to support multiple processors,
multiple data encodings, and multiple classes of machines. To support this object file family,
the initial bytes of the file specify how to interpret the file, independent of the processor on
which the inquiry is made and independent of the file’s remaining contents.
The initial bytes of an ELF header (and an object file) correspond to the e_ident member.
Figure 1-4. e_ident[] Identification Indexes
Name Value Purpose
EI_MAG0 EI_MAG1 EI_MAG2 EI_MAG3 EI_CLASS EI_DATA EI_VERSION EI_PAD EI_NIDENT |
0 1 2 3 4 5 6 7 16 |
File identification File identification File identification File identification File class Data encoding File version Start of padding bytes Size of e_ident[] |
ELF Header
OBJECT FILES 1-7
These indexes access bytes that hold the following values.
EI_MAG0 to EI_MAG3 A file’s first 4 bytes hold a “magic number,” identifying the file as an ELF
object file.
EI_CLASS The next byte, e_ident[EI_CLASS], identifies the file’s class, or
capacity.
The file format is designed to be portable among machines of various sizes, without
imposing the sizes of the largest machine on the smallest. Class ELFCLASS32
supports machines with files and virtual address spaces up to 4 gigabytes; it uses
the basic types defined above.
Class ELFCLASS64 is incomplete and refers to the 64-bit architectures. Its
appearance here shows how the object file may change. Other classes will be defined
as necessary, with different basic types and sizes for object file data.
EI_DATA Byte e_ident[EI_DATA]specifies the data encoding of the
processor-specific data in the object file. The following encodings are
currently defined.
More information on these encodings appears below. Other values are
reserved and will be assigned to new encodings as necessary.
EI_VERSION Byte e_ident[EI_VERSION] specifies the ELF header version number.
Currently, this value must be EV_CURRENT, as explained above for
e_version.
EI_PAD This value marks the beginning of the unused bytes in e_ident. These
bytes are reserved and set to zero; programs that read object files should
ignore them. The value of EI_PAD will change in the future if currently
unused bytes are given meanings.
Name Value Meaning
ELFMAG0 ELFMAG1 ELFMAG2 ELFMAG3 |
0x7f ’E’ ’L’ ’F’ |
e_ident[EI_MAG0] e_ident[EI_MAG1] e_ident[EI_MAG2] e_ident[EI_MAG3] |
Name Value Meaning
ELFCLASSNONE ELFCLASS32 ELFCLASS64 |
0 1 2 |
Invalid class 32-bit objects 64-bit objects |
Name Value Meaning
ELFDATANONE ELFDATA2LSB ELFDATA2MSB |
0 1 2 |
Invalid data encoding See below See below |
ELF Header
1-8 Book I: ELF (Executable and Linking Format)
A file’s data encoding specifies how to interpret the basic objects in a file. As described above,
class ELFCLASS32 files use objects that occupy 1, 2, and 4 bytes. Under the defined encodings,
objects are represented as shown below. Byte numbers appear in the upper left corners.
Encoding ELFDATA2LSB specifies 2’s complement values, with the least significant byte
occupying the lowest address.
Encoding ELFDATA2MSB specifies 2’s complement values, with the most significant byte
occupying the lowest address.
Figure 1-5. Data Encoding ELFDATA2LSB
Figure 1-6. Data Encoding ELFDATA2MSB
OSD1981
04 0 |
03 1 |
02 2 |
01 3 |
0x01020304
02 0 |
01 1 |
0x0102
01
0
0x01
OSD1982
01 0 |
02 1 |
03 2 |
04 3 |
0x01020304
01 0 |
02 1 |
0x0102
01
0
0x01
OBJECT FILES 1-9
Sections
An object file’s section header table lets one locate all the file’s sections. The section header
table is an array of Elf32_Shdr structures as described below. A section header table index
is a subscript into this array. The ELF header’s e_shoff member gives the byte offset from
the beginning of the file to the section header table; e_shnum tells how many entries the
section header table contains; e_shentsize gives the size in bytes of each entry.
Some section header table indexes are reserved; an object file will not have sections for these
special indexes.
SHN_UNDEF This value marks an undefined, missing, irrelevant, or otherwise
meaningless section reference. For example, a symbol “defined” relative to
section number SHN_UNDEF is an undefined symbol.
NOTE. Although index 0 is reserved as the undefined value, the section header
table contains an entry for index 0. That is, if the e_shnum member of
the ELF header says a file has 6 entries in the section header table, they
have the indexes 0 through 5. The contents of the initial entry are specified
later in this section.
SHN_LORESERVE | This value specifies the lower bound of the range of reserved indexes. |
SHN_LOPROC through Values in this inclusive range are reserved for processor-specific semantics. | |
SHN_HIPROC SHN_ABS |
This value specifies absolute values for the corresponding reference. For |
example, symbols defined relative to section number SHN_ABS have
absolute values and are not affected by relocation.
SHN_COMMON | Symbols defined relative to this section are common symbols, such as FORTRAN COMMON or unallocated C external variables. |
Figure 1-7. Special Section Indexes
Name Value
SHN_UNDEF SHN_LORESERVE SHN_LOPROC SHN_HIPROC SHN_ABS SHN_COMMON SHN_HIRESERVE |
0 0xff00 0xff00 0xff1f 0xfff1 0xfff2 0xffff |
Sections
1-10 Book I: ELF (Executable and Linking Format)
SHN_HIRESERVE This value specifies the upper bound of the range of reserved indexes. The
system reserves indexes between SHN_LORESERVE and
SHN_HIRESERVE, inclusive; the values do not reference the section header
table.That is, the section header table does not contain entries for the
reserved indexes.
Sections contain all information in an object file, except the ELF header, the program header
table, and the section header table. Moreover, object files’ sections satisfy several conditions.
• Every section in an object file has exactly one section header describing it. Section headers may
exist that do not have a section.
• Each section occupies one contiguous (possibly empty) sequence of bytes within a file.
• Sections in a file may not overlap. No byte in a file resides in more than one section.
• An object file may have inactive space. The various headers and the sections might not “cover”
every byte in an object file. The contents of the inactive data are unspecified.
A section header has the following structure.
sh_name This member specifies the name of the section. Its value is an index into
the section header string table section [see “String Table” below], giving
the location of a null-terminated string.
sh_type This member categorizes the section’s contents and semantics. Section
types and their descriptions appear below.
sh_flags Sections support 1-bit flags that describe miscellaneous attributes. Flag
definitions appear below.
sh_addr If the section will appear in the memory image of a process, this member
gives the address at which the section’s first byte should reside. Otherwise,
the member contains 0.
Figure 1-8. Section Header
typedef struct {
Elf32_Word sh_name;
Elf32_Word sh_type;
Elf32_Word sh_flags;
Elf32_Addr sh_addr;
Elf32_Off sh_offset;
Elf32_Word sh_size;
Elf32_Word sh_link;
Elf32_Word sh_info;
Elf32_Word sh_addralign;
Elf32_Word sh_entsize;
} Elf32_Shdr;
Sections
OBJECT FILES 1-11
sh_offset This member’s value gives the byte offset from the beginning of the file to
the first byte in the section. One section type, SHT_NOBITS described
below, occupies no space in the file, and its sh_offset member locates
the conceptual placement in the file.
sh_size | This member gives the section’s size in bytes. Unless the section type is SHT_NOBITS, the section occupies sh_size bytes in the file. A section |
of type SHT_NOBITS may have a non-zero size, but it occupies no space
in the file.
sh_link | This member holds a section header table index link, whose interpretation depends on the section type. A table below describes the values. This member holds extra information, whose interpretation depends on the section type. A table below describes the values. Some sections have address alignment constraints. For example, if a section holds a doubleword, the system must ensure doubleword alignment for the |
sh_info | |
sh_addralign |
entire section. That is, the value of sh_addr must be congruent to 0,
modulo the value of sh_addralign. Currently, only 0 and positive
integral powers of two are allowed. Values 0 and 1 mean the section has no
alignment constraints.
sh_entsize | Some sections hold a table of fixed-size entries, such as a symbol table. For such a section, this member gives the size in bytes of each entry. The |
member contains 0 if the section does not hold a table of fixed-size entries.
A section header’s sh_type member specifies the section’s semantics.
Figure 1-9. Section Types, sh_type
Name Value
SHT_NULL SHT_PROGBITS SHT_SYMTAB SHT_STRTAB SHT_RELA SHT_HASH SHT_DYNAMIC SHT_NOTE SHT_NOBITS SHT_REL SHT_SHLIB SHT_DYNSYM SHT_LOPROC SHT_HIPROC SHT_LOUSER SHT_HIUSER |
0 1 2 3 4 5 6 7 8 9 10 11 0x70000000 0x7fffffff 0x80000000 0xffffffff |
Sections
1-12 Book I: ELF (Executable and Linking Format)
SHT_NULL This value marks the section header as inactive; it does not have an
associated section. Other members of the section header have undefined
values.
SHT_PROGBITS The section holds information defined by the program, whose format and
meaning are determined solely by the program.
SHT_SYMTAB and These sections hold a symbol table.
SHT_DYNSYM
SHT_STRTAB The section holds a string table.
SHT_RELA The section holds relocation entries with explicit addends, such as type
Elf32_Rela for the 32-bit class of object files. An object file may have
multiple relocation sections. See “Relocation” below for details.
SHT_HASH The section holds a symbol hash table.
SHT_DYNAMIC The section holds information for dynamic linking.
SHT_NOTE This section holds information that marks the file in some way.
SHT_NOBITS A section of this type occupies no space in the file but otherwise resembles
SHT_PROGBITS. Although this section contains no bytes, the
sh_offset member contains the conceptual file offset.
SHT_REL The section holds relocation entries without explicit addends, such as type
Elf32_Rel for the 32-bit class of object files. An object file may have
multiple relocation sections. See “Relocation” below for details.
SHT_SHLIB This section type is reserved but has unspecified semantics.
Sections
OBJECT FILES 1-13
SHT_LOPROC through Values in this inclusive range are reserved for processor-specific semantics.
SHT_HIPROC
SHT_LOUSER | This value specifies the lower bound of the range of indexes reserved for application programs. This value specifies the upper bound of the range of indexes reserved for |
SHT_HIUSER |
application programs. Section types between SHT_LOUSER and
SHT_HIUSER may be used by the application, without conflicting with
current or future system-defined section types.
Other section type values are reserved. As mentioned before, the section header for index 0
(SHN_UNDEF) exists, even though the index marks undefined section references. This entry
holds the following.
A section header’s sh_flags member holds 1-bit flags that describe the section’s attributes.
Defined values appear below; other values are reserved.
If a flag bit is set in sh_flags, the attribute is “on” for the section. Otherwise, the attribute
is “off” or does not apply. Undefined attributes are set to zero.
SHF_WRITE The section contains data that should be writable during process execution.
Figure 1-10. Section Header Table Entry: Index 0
Name Value Note
sh_name sh_type sh_flags sh_addr sh_offset sh_size sh_link sh_info sh_addralign sh_entsize |
0 SHT_NULL 0 0 0 0 SHN_UNDEF 0 0 0 |
No name Inactive No flags No address No file offset No size No link information No auxiliary information No alignment No entries |
Figure 1-11. Section Attribute Flags, sh_flags
Name Value
SHF_WRITE SHF_ALLOC SHF_EXECINSTR SHF_MASKPROC |
0x1 0x2 0x4 0xf0000000 |
Sections
1-14 Book I: ELF (Executable and Linking Format)
SHF_ALLOC | The section occupies memory during process execution. Some control sections do not reside in the memory image of an object file; this attribute is off for those sections. The section contains executable machine instructions. All bits included in this mask are reserved for processor-specific semantics. |
SHF_EXECINSTR SHF_MASKPROC |
Two members in the section header, sh_link and sh_info, hold special information,
depending on section type.
Special Sections
Various sections in ELF are pre-defined and hold program and control information. These
Sections are used by the operating system and have different types and attributes for different
operating systems.
Executable files are created from individual object files and libraries through the linking
process. The linker resolves the references (including subroutines and data references) among
the different object files, adjusts the absolute references in the object files, and relocates
instructions. The linking and loading processes, which are described in Chapter 2, require
information defined in the object files and store this information in specific sections such as
.dynamic.
Each operating system supports a set of linking models which fall into two categories:
Static | A set of object files, system libraries and library archives are statically bound, references are resolved, and an executable file is created that is completely self contained. A set of object files, libraries, system shared resources and other shared |
Dynamic |
libraries are linked together to create the executable. When this executable
is loaded, other shared resources and dynamic libraries must be made
available in the system for the program to run successfully.
Figure 1-12. sh_link and sh_info Interpretation
sh_type sh_link sh_info
SHT_DYNAMIC SHT_HASH SHT_REL SHT_RELA SHT_SYMTAB SHT_DYNSYM other |
The section header index of the string table used by entries in the section. The section header index of the symbol table to which the hash table applies. The section header index of the associated symbol table. This information is operating system specific. SHN_UNDEF |
0 0 The section header index of the section to which the relocation applies. This information is operating system specific. 0 |
Sections
OBJECT FILES 1-15
The general method used to resolve references at execution time for a
dynamically linked executable file is described in the linkage model used
by the operating system, and the actual implementation of this linkage
model will contain processor-specific components.
There are also sections that support debugging, such as .debug and .line, and program
control, including.bss, .data, .data1, .rodata, and .rodata1.
.bss This section holds uninitialized data that contribute to the program’s
memory image. By definition, the system initializes the data with zeros
when the program begins to run. The section occupies no file space, as
indicated by the section type, SHT_NOBITS.
.comment This section holds version control information.
.data and .data1 These sections hold initialized data that contribute to the program’s memory
image.
.debug This section holds information for symbolic debugging. The contents are
unspecified. All section names with the prefix .debug are reserved for
future use.
.dynamic This section holds dynamic linking information and has attributes such as
SHF_ALLOC and SHF_WRITE. Whether the SHF_WRITE bit is set is
determined by the operating system and processor.
.hash This section holds a symbol hash table.
Figure 1-13. Special Sections
Name Type Attributes
.bss .comment .data .data1 .debug .dynamic .hash .line .note .rodata .rodata1 |
SHT_NOBITS SHT_PROGBITS SHT_PROGBITS SHT_PROGBITS SHT_PROGBITS SHT_DYNAMIC SHT_HASH SHT_PROGBITS SHT_NOTE SHT_PROGBITS SHT_PROGBITS |
SHF_ALLOC+SHF_WRITE none SHF_ALLOC + SHF_WRITE SHF_ALLOC + SHF_WRITE none see below SHF_ALLOC none none SHF_ALLOC SHF_ALLOC |
.shstrtab .strtab .symtab .text |
SHT_STRTAB SHT_STRTAB SHT_SYMTAB SHT_PROGBITS |
none see below see below SHF_ALLOC + SHF_EXECINSTR |
Sections
1-16 Book I: ELF (Executable and Linking Format)
.line | This section holds line number information for symbolic debugging, which describes the correspondence between the source program and the machine code. The contents are unspecified. This section holds information in the format that is described in the “Note Section” in Chapter 2. These sections hold read-only data that typically contribute to a non-writable segment in the process image. See “Program Header” in Chapter 2 for more information. This section holds section names. This section holds strings, most commonly the strings that represent the names associated with symbol table entries. If a file has a loadable segment that includes the symbol string table, the section’s attributes will include the SHF_ALLOC bit; otherwise, that bit will be off. This section holds a symbol table, as “Symbol Table” in this chapter |
.note | |
.rodata and .rodata1 |
|
.shstrtab .strtab |
|
.symtab |
describes. If a file has a loadable segment that includes the symbol table,
the section’s attributes will include the SHF_ALLOC bit; otherwise, that bit
will be off.
.text This section holds the “text,” or executable instructions, of a program.
Section names with a dot (.) prefix are reserved for the system, although applications may use
these sections if their existing meanings are satisfactory. Applications may use names without
the prefix to avoid conflicts with system sections. The object file format lets one define sections
not in the list above. An object file may have more than one section with the same name.
OBJECT FILES 1-17
String Table
This section describes the default string table. String table sections hold null-terminated
character sequences, commonly called strings. The object file uses these strings to represent
symbol and section names. One references a string as an index into the string table section.
The first byte, which is index zero, is defined to hold a null character. Likewise, a string table’s
last byte is defined to hold a null character, ensuring null termination for all strings. A string
whose index is zero specifies either no name or a null name, depending on the context. An
empty string table section is permitted; its section header’s sh_size member would contain
zero. Non-zero indexes are invalid for an empty string table.
A section header’s sh_name member holds an index into the section header string table section,
as designated by the e_shstrndx member of the ELF header. The following figures show a
string table with 25 bytes and the strings associated with various indexes.
As the example shows, a string table index may refer to any byte in the section. A string may
appear more than once; references to substrings may exist; and a single string may be referenced
multiple times. Unreferenced strings also are allowed.
Index +0 +1 +2 +3 +4 +5 +6 +7 +8 +9
0