API for manipulating ELF objects
ELF Access Library (libelf, -lelf)
The ELF Access Library (libelf, -lelf)
provides functions that allow an application to read and manipulate ELF object
files, and to read
archives. The library allows the manipulation of ELF objects in a byte
ordering and word-size independent way, allowing an application to read and
create ELF objects for 32 and 64 bit architectures and for little- and
big-endian machines. The library is capable of processing ELF objects that use
extended section numbering.
This manual page serves to provide an overview of the functionality in the ELF
library. Further information may found in the manual pages for individual
functions that comprise the library.
As described in
ELF files contain several data structures that are laid out in a specific way.
ELF files begin with an “Executable Header”, and may contain an
optional “Program Header Table”, and optional data in the form
of ELF “sections”. A “Section Header Table”
describes the content of the data in these sections.
ELF objects have an associated “ELF class” which denotes the
natural machine word size for the architecture the object is associated with.
Objects for 32 bit architectures have an ELF class of
. Objects for 64 bit
architectures have an ELF class of
ELF objects also have an associated “endianness” which denotes the
endianness of the machine architecture associated with the object. This may be
for little-endian architectures
ELF objects are also associated with an API version number. This version number
determines the layout of the individual components of an ELF file and the
semantics associated with these.
library distinguishes between “native” representations of ELF
data structures and their “file” representations.
An application would work with ELF data in its “native”
representation, i.e., using the native byteorder and alignment mandated by the
processor the application is running on. The “file”
representation of the same data could use a different byte ordering and follow
different constraints on object alignment than these native constraints.
library offers translation facilities
to and from these representations and also provides higher-level APIs that
retrieve and store data from the ELF object in a transparent manner.
Conceptually, there are three version numbers associated with an application
using the ELF library to manipulate ELF objects:
- The ELF version that the application was compiled against. This version
determines the ABI expected by the application.
- The ELF version of the ELF object being manipulated by the application
through the ELF library.
- The ELF version (or set of versions) supported by the ELF library
In order to facilitate working with ELF objects of differing versions, the ELF
library requires the application to call the
() function before invoking many
of its operations, in order to inform the library of the application's desired
In the current implementation, all three versions have to be
The ELF library uses the following prefixes:
- Used for class-independent functions.
- Used for functions working with 32 bit ELF objects.
- Used for functions working with 64 bit ELF objects.
- Used for class-independent data types.
- Used for command values used in a few functions. These symbols are defined
as members of the
- Used for error numbers.
- Used for flags.
- These constants define the kind of file associated with an ELF descriptor.
The symbols are defined by the Elf_Kind
- These values are defined by the Elf_Type
enumeration, and denote the types of ELF data structures that can be
present in an ELF object.
In addition, the library uses symbols with prefixes
for its internal use.
Applications communicate with the library using descriptors. These are:
- An Elf descriptor represents an ELF
object or an
archive. It is allocated using one of the
elf_memory() functions. An
Elf descriptor can be used to read and
write data to an ELF file. An Elf
descriptor can be associated with zero or more
Elf_Scn section descriptors.
Given an ELF descriptor, the application may retrieve the ELF object's
class-dependent “Executable Header” structures using the
elf64_getehdr() functions. A new Ehdr
structure may be allocated using the
The “Program Header Table” associated with an ELF descriptor
may be allocated using the
elf64_getphdr() functions. A new
program header table may be allocated or an existing table resized using
The Elf structure is opaque and has no
members visible to the application.
- An Elf_Data data structure describes an
individual chunk of a ELF file as represented in memory. It has the
following application-visible members:
Elf_Data descriptors are usually associated
with Elf_Scn descriptors. Existing data
descriptors associated with an ELF section may be structures are retrieved
- uint64_t d_align
- The in-file alignment of the data buffer within its containing ELF
section. This value must be non-zero and a power of two.
- void *d_buf
- A pointer to data in memory.
- uint64_t d_off
- The offset within the containing section where this descriptor's data
would be placed. This field will be computed by the library unless the
application requests full control of the ELF object's layout.
- uint64_t d_size
- The number of bytes of data in this descriptor.
- Elf_Type d_type
- The ELF type (see below) of the data in this descriptor.
- unsigned int d_version
- The operating version for the data in this buffer.
elf_rawdata() functions. The
elf_newdata() function may be used to
attach new data descriptors to an ELF section.
- Elf_Scn descriptors represent a section
in an ELF object.
They are retrieved using the
function. An application may iterate through the existing sections of an
ELF object using the
function. New sections may be allocated using the
The Elf_Scn descriptor is opaque and
contains no application modifiable fields.
The following ELF datatypes are supported by the library.
- Machine addresses.
- Byte data. The library will not attempt to translate byte data.
- Software and hardware capability records.
- Records used in a section of type
- ELF executable header.
- GNU-style hash tables.
- 16-bit unsigned words.
- 64 bit unsigned words.
- ELF Move records.
- ELF Note structures.
- File offsets.
- ELF program header table entries.
- ELF relocation entries.
- ELF relocation entries with addends.
- ELF section header entries.
- Signed 32-bit words.
- Signed 64-bit words.
- ELF symbol information.
- ELF symbol table entries.
- Symbol version definition records.
- Symbol version requirement records.
- Unsigned 32-bit words.
- Unsigned 64-bit words.
denotes the number of
Elf types known to the library.
The following table shows the mapping between ELF section types defined in
and the types supported by the library.
Section types in the range [
] are otherwise considered to be
This section contains a brief overview of the available functionality in the ELF
library. Each function listed here is described further in its own manual
- Archive Access
- Retrieve the archive symbol table.
- Retrieve the archive header for an object.
- Retrieve the offset of a member inside an archive.
- Iterate through an
- Random access inside an
- Data Structures
- Retrieve translated data for an ELF section.
- Retrieve the section descriptor for a named section.
- Retrieve the index for a section.
- Add a new Elf_Data descriptor to an
- Add a new section descriptor to an ELF descriptor.
- Iterate through the sections in an ELF object.
- Retrieve untranslated data for an ELF section.
- Return a pointer to the untranslated file contents for an ELF
- Retrieve the Executable Header in an ELF object.
- Retrieve the Program Header Table in an ELF object.
- Retrieve the ELF section header associated with an
- Allocate an Executable Header in an ELF object.
- Allocate or resize the Program Header Table in an ELF object.
- Data Translation
- Translate an ELF data structure from its native representation to its
- Translate an ELF data structure from its file representation to a
- Error Reporting
- Retrieve the current error.
- Retrieve a human readable description of the current error.
- Opens an
archive or ELF object given a file descriptor.
- Close an ELF descriptor and release all its resources.
- Opens an
archive or ELF object present in a memory arena.
- Sets the operating version.
- IO Control
- Manage the association between and ELF descriptor and its underlying
- Mark an Elf_Data descriptor as
- Mark the ELF Executable Header in an ELF descriptor as dirty.
- Mark the ELF Program Header Table in an ELF descriptor as dirty.
- Mark an Elf_Scn descriptor as
- Mark an ELF Section Header as dirty.
- Set the index of the section name string table for the ELF
- Recompute ELF object layout and optionally write the modified object
back to the underlying file.
- Compute checksum of an ELF object.
- Retrieve the identification bytes for an ELF object.
- Retrieve the number of sections in an ELF object.
- Retrieve the section index of the section name string table in an ELF
- Compute the ELF hash value of a string.
- Query the kind of object associated with an ELF descriptor.
- Return the size of the file representation of an ELF type.
In the usual mode of operation, library will compute section offsets and
alignments based on the contents of an ELF descriptor's sections without need
for further intervention by the application.
However, if the application wishes to take complete charge of the layout of the
ELF file, it may set the
on an ELF descriptor using
following which the library will use the data offsets and alignments specified
by the application when laying out the file. Application control of file
layout is described further in the
Gaps in between sections will be filled with the fill character set by function
In case an error is encountered, these library functions set an internal error
number and signal the presence of the error by returning an special return
value. The application can check the current error number by calling
A human readable description of the recorded error is available by calling
The library keeps track of all Elf_Scn
descriptors associated with an ELF
descriptor and recovers them when the descriptor is closed using
Thus the application must not call
on data structures allocated by the ELF library.
Conversely the library will not free data that it has not allocated. As an
example, an application may call
to allocate a new Elf_Data
descriptor and can
set the d_off
member of the descriptor to
point to a region of memory allocated using
It is the applications responsibility to free this arena, though the library
will reclaim the space used by the Elf_Data
The original ELF(3) API was developed for Unix System V. The current
implementation of the ELF(3) API appeared in FreeBSD
The ELF library was written by Joseph Koshy