ELF64
The ELF (Executable and Linkable Format) header of an elf64 executable contains metadata about the file, including essential information that helps the operating system or loader interpret and execute the binary. Here's a breakdown of what the ELF header contains and how you can find where the code begins:
Structure of the ELF64 Header
The ELF64 header typically contains the following fields:
e_ident(16 bytes):Magic number (
0x7f 'E' 'L' 'F').File class (32-bit or 64-bit).
Data encoding (little-endian or big-endian).
ELF version.
OS/ABI information.
Padding for alignment.
e_type(2 bytes):Type of file (e.g.,
ET_EXECfor executable,ET_DYNfor shared object, etc.).
e_machine(2 bytes):Target architecture (e.g.,
EM_X86_64for x86-64).
e_version(4 bytes):ELF version (usually 1).
e_entry(8 bytes):The virtual address of the entry point where execution starts (beginning of the code).
e_phoff(8 bytes):Offset of the program header table (for runtime execution).
e_shoff(8 bytes):Offset of the section header table (for debugging and linking).
e_flags(4 bytes):Architecture-specific flags.
e_ehsize(2 bytes):Size of the ELF header.
e_phentsize(2 bytes):
Size of each program header table entry.
e_phnum(2 bytes):
Number of entries in the program header table.
e_shentsize(2 bytes):
Size of each section header table entry.
e_shnum(2 bytes):
Number of entries in the section header table.
e_shstrndx(2 bytes):
Index of the section header string table.
Finding Where the Code Begins
To determine where the code starts, follow these steps:
Check the Entry Point (
e_entry):The
e_entryfield in the ELF header gives the virtual address of the entry point where execution begins. However, this is a virtual address, so you may need to map it to a file offset.
Locate the Program Header (
e_phoff):The program header table describes memory segments of the executable, including the code segment (
PT_LOAD).
Find the Code Segment:
Look for a segment in the program header with the type
PT_LOADand executable permission flags (PF_X).This segment's
p_offsetfield gives the file offset of the segment.The
p_vaddrfield provides the virtual address of the segment.
Map Entry Point to File Offset:
Use the
e_entryvirtual address and compare it with thep_vaddrof the segment containing the code. The corresponding offset within the file can be calculated as:file_offset = e_entry - p_vaddr + p_offset
Verify with Disassembly:
Use tools like
objdumporreadelfto disassemble the binary and verify the entry point.
Commands to Analyze ELF Headers
readelf -h <file>: Displays the ELF header, includinge_entry,e_phoff, and more.readelf -l <file>: Displays the program headers and segments, including their file offsets and virtual addresses.objdump -d <file>: Disassembles the executable, allowing you to examine the code.
Example
These tools can help you locate the exact location of the code and understand the structure of the ELF executable.