SimplifyC++ Article
Object File Generation Creating Relocatable Object Files - ELF32 vs ELF64 Differences
Object File Generation : Creating Relocatable Object Files -> ELF32 vs ELF64 Differences
1. Introduction
When designing an assembler targeting x86-64 architectures, understanding the distinctions between the 32-bit ELF (ELF32) and 64-bit ELF (ELF64) formats is essential for generating correct relocatable object files. While both formats share core structural concepts defined by the Executable and Linkable Format (ELF) standard, their differences reflect architectural and data model changes between 32-bit and 64-bit environments.
2. ELF Header Differences
Class Field: The ELF header contains an identification byte specifying whether the file uses ELF32 or ELF64 format. This field distinguishes between the two at the earliest level.
Data Model: ELF32 supports 32-bit addresses and offsets, while ELF64 supports 64-bit addresses and offsets, directly impacting the size of header fields and data structures throughout the file.
3. Address and Offset Sizes
In ELF32, addresses, offsets, and size fields are 32 bits wide, limiting the maximum addressable memory range to 4 GB.
ELF64 uses 64-bit fields for these same items, accommodating larger address spaces and supporting modern 64-bit operating systems and applications.
This difference affects the encoding of section headers, program headers, symbol tables, and relocation entries.
4. Section Header Format
The section header entries in ELF32 contain 32-bit fields for addresses and offsets, whereas ELF64 expands these fields to 64 bits.
This impacts how sections such as
.text,.data,.bss, and others are described, particularly for large binaries or systems with significant memory requirements.
5. Symbol Table Entries
ELF32 symbol table entries (
Elf32_Sym) use 32-bit fields for symbol values and sizes.ELF64 symbol table entries (
Elf64_Sym) use 64-bit fields, allowing representation of symbols in a larger address space.This enables the linker and debugger to handle symbols referencing 64-bit addresses transparently.
6. Relocation Entries
Relocation entries differ notably between ELF32 and ELF64 in structure and semantics:
ELF32 uses
Elf32_RelandElf32_Relawith 32-bit offsets and 32-bit info fields.ELF64 uses
Elf64_RelandElf64_Relawith 64-bit offsets and info fields.
The relocation types and conventions differ to suit the target architecture’s addressing modes and instruction sets.
7. Impact on Instruction Encoding and Relocation
In ELF64, instruction encodings can reference full 64-bit addresses and offsets, requiring corresponding relocation entries with 64-bit data.
ELF32's smaller address space simplifies some relocation calculations but limits applicability for modern 64-bit programs.
An assembler generating ELF64 object files must correctly encode relocation entries to support the full 64-bit address space, including explicit addends and proper relocation types for x86-64.
8. Differences in Data Alignment and Padding
ELF64 generally requires stricter alignment of sections and symbols to 8-byte boundaries or larger, reflecting the 64-bit data model.
ELF32 aligns data primarily on 4-byte boundaries.
These alignment rules impact how the assembler organizes data within sections to comply with the format and the platform’s ABI requirements.
9. Program Header Differences
ELF64 program headers feature 64-bit fields to describe segment locations, sizes, and memory addresses.
This allows efficient representation of large memory mappings for executables and shared libraries.
ELF32 program headers use 32-bit fields, limiting memory mappings accordingly.
10. Summary of Key Differences
| Feature | ELF32 | ELF64 |
|---|---|---|
| Address size | 32-bit | 64-bit |
| Offset size | 32-bit | 64-bit |
| Symbol table entries | 32-bit fields | 64-bit fields |
| Relocation entries | 32-bit offsets and info | 64-bit offsets and info |
| Data alignment | Typically 4 bytes | Typically 8 bytes or larger |
| Program headers | 32-bit fields | 64-bit fields |
| Maximum addressable memory | Up to 4 GB | Much larger, dependent on OS |
11. Practical Implications for Assembler Design
Generating ELF64 object files requires supporting 64-bit wide fields consistently throughout the assembler’s object file generation phase.
The assembler must correctly distinguish between ELF32 and ELF64 formats based on target architecture and user options.
Encoding relocation entries, symbol tables, and headers must follow ELF64 specifications to ensure compatibility with 64-bit linkers and loaders.
The assembler’s internal data structures and file output routines must handle 64-bit values safely and efficiently.
12. Conclusion
Understanding the differences between ELF32 and ELF64 is critical for producing valid relocatable object files on x86-64 platforms. While the ELF64 format introduces larger field sizes and more complex addressing capabilities, it remains consistent with the core ELF principles, enabling smooth transition from 32-bit to 64-bit development environments. An assembler targeting modern x86-64 systems must embrace the ELF64 format fully to support the demands of contemporary software ecosystems.