ELF(3)
πΈ NAME πΈ
This is the ELF(3) manual page, bestie β elf β API for manipulating ELF objects. π
π LIBRARY π
ELF Access Library (libelf, -lelf)
π SYNOPSIS π
#include <libelf.h>
π¦ DESCRIPTION π¦
okay so the ELF Access Library (libelf, -lelf) is literally the toolkit that gives your app functions to read and manipulate ELF object files, and to read ar(1) archives. The way it does this is byte ordering and word-size independent, so your app can read and create ELF objects for 32 and 64 bit architectures and for little- and big-endian machines. Itβs also capable of processing ELF objects that use extended section numbering. no cap. β¨
This manual page is the big-picture overview of what the ELF library can do. Further information lives in the manual pages for the individual ELF(3) functions that make up the library.
π ELF Concepts π
As elf(5) explains, ELF files contain several data structures laid out in a very specific way. ELF files begin with an βExecutable Headerβ, and they may contain an optional βProgram Header Tableβ, plus optional data in the form of ELF βsectionsβ. A βSection Header Tableβ describes the content of the data in those 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 ELFCLASS32. Objects for 64 bit architectures have an ELF class of ELFCLASS64.
ELF objects also have an associated βendiannessβ, which denotes the endianness of the machine architecture associated with the object. This may be ELFDATA2LSB for little-endian architectures and ELFDATA2MSB for big-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 them. π«Ά
π§ Data Representation And Translation π§
The ELF(3) library distinguishes between βnativeβ representations of ELF data structures and their βfileβ representations.
An application works with ELF data in its βnativeβ representation β meaning it uses the native byteorder and alignment mandated by the processor the application is running on. The βfileβ representation of that same data could use a different byte ordering and follow different constraints on object alignment than those native constraints. itβs giving two worlds, same data. π¦
So the ELF(3) library offers translation facilities to and from these representations: elf32_xlatetof(3), elf32_xlatetom(3), elf64_xlatetof(3), and elf64_xlatetom(3). It also provides higher-level APIs β gelf_xlatetof(3) and gelf_xlatetom(3) β that retrieve and store data from the ELF object in a class-agnostic manner.
π Library Working Version π
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 itself.
In order to facilitate working with ELF objects of differing versions, the ELF library requires the application to call the elf_version() function before invoking many of its operations, so it can inform the library of the applicationβs desired working version.
In the current implementation, all three versions have to be EV_CURRENT. periodt. π
π Namespace use π
The ELF library uses the following prefixes:
elf_β Used for class-independent functions.elf32_β Used for functions working with 32 bit ELF objects.elf64_β Used for functions working with 64 bit ELF objects.Elf_β Used for class-independent data types.ELF_C_β Used for command values used in a few functions. These symbols are defined as members of theElf_Cmdenumeration.ELF_E_β Used for error numbers.ELF_F_β Used for flags.ELF_K_β These constants define the kind of file associated with an ELF descriptor. Seeelf_kind(3). The symbols are defined by theElf_Kindenumeration.ELF_T_β These values are defined by theElf_Typeenumeration, and denote the types of ELF data structures that can be present in an ELF object.
In addition, the library uses symbols with prefixes _ELF and _libelf for its internal use.
π«Ά Descriptors π«Ά
Applications communicate with the library using descriptors. These are:
Elf
An Elf descriptor represents an ELF object or an ar(1) archive. It is allocated using one of the elf_begin() or 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 elf32_getehdr() or elf64_getehdr() functions. A new Ehdr structure may be allocated using the elf64_newehdr() or elf64_newehdr() functions.
The βProgram Header Tableβ associated with an ELF descriptor may be allocated using the elf32_getphdr() or elf64_getphdr() functions. A new program header table may be allocated or an existing table resized using the elf32_newphdr() or elf64_newphdr() functions.
The Elf structure is opaque and has no members visible to the application. π
Elf_Data
An Elf_Data data structure describes an individual chunk of a ELF file as represented in memory. It has the following application-visible members:
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_Data descriptors are usually used in conjunction with Elf_Scn descriptors.
Elf_Scn
Elf_Scn descriptors represent sections in an ELF object. These descriptors are opaque and contain no application modifiable fields.
The Elf_Scn descriptor for a specific section in an ELF object can be retrieved using the elf_getscn() function. The sections contained in an ELF object can be traversed using the elf_nextscn() function. New sections are allocated using the elf_newscn() function.
The Elf_Data descriptors associated with a given section can be retrieved using the elf_getdata() function. New data descriptors can be added to a section descriptor using the elf_newdata() function. The untranslated βfileβ representation of data in a section can be retrieved using the elf_rawdata() function.
β Supported Elf Types β
The following ELF datatypes are supported by the library.
ELF_T_ADDRβ Machine addresses.ELF_T_BYTEβ Byte data. The library will not attempt to translate byte data.ELF_T_CAPβ Software and hardware capability records.ELF_T_DYNβ Records used in a section of typeSHT_DYNAMIC.ELF_T_EHDRβ ELF executable header.ELF_T_GNUHASHβ GNU-style hash tables.ELF_T_HALFβ 16-bit unsigned words.ELF_T_LWORDβ 64 bit unsigned words.ELF_T_MOVEβ ELF Move records.ELF_T_NOTEβ ELF Note structures.ELF_T_OFFβ File offsets.ELF_T_PHDRβ ELF program header table entries.ELF_T_RELβ ELF relocation entries.ELF_T_RELAβ ELF relocation entries with addends.ELF_T_SHDRβ ELF section header entries.ELF_T_SWORDβ Signed 32-bit words.ELF_T_SXWORDβ Signed 64-bit words.ELF_T_SYMINFOβ ELF symbol information.ELF_T_SYMβ ELF symbol table entries.ELF_T_VDEFβ Symbol version definition records.ELF_T_VNEEDβ Symbol version requirement records.ELF_T_WORDβ Unsigned 32-bit words.ELF_T_XWORDβ Unsigned 64-bit words.
The symbol ELF_T_NUM denotes the number of Elf types known to the library.
The following table shows the mapping between ELF section types defined in elf(5) and the types supported by the library. π
okay so picking up the section-type table right where it left off β this is the section type β library type β what it actually is tea, and yes, every row is here:
SHT_DYNAMICβELF_T_DYNβ.dynamicsection entries.SHT_DYNSYMβELF_T_SYMβ symbols for dynamic linking.SHT_FINI_ARRAYβELF_T_ADDRβ termination function pointers.SHT_GNU_HASHβELF_T_GNUHASHβ GNU hash sections.SHT_GNU_LIBLISTβELF_T_WORDβ list of libraries to be pre-linked.SHT_GNU_verdefβELF_T_VDEFβ symbol version definitions.SHT_GNU_verneedβELF_T_VNEEDβ symbol versioning requirements.SHT_GNU_versymβELF_T_HALFβ version symbols.SHT_GROUPβELF_T_WORDβ section group marker.SHT_HASHβELF_T_HASHβ symbol hashes.SHT_INIT_ARRAYβELF_T_ADDRβ initialization function pointers.SHT_NOBITSβELF_T_BYTEβ empty sections. Seeelf(5).SHT_NOTEβELF_T_NOTEβ ELF note records.SHT_PREINIT_ARRAYβELF_T_ADDRβ pre-initialization function pointers.SHT_PROGBITSβELF_T_BYTEβ machine code.SHT_RELβELF_T_RELβ ELF relocation records.SHT_RELAβELF_T_RELAβ relocation records with addends.SHT_STRTABβELF_T_BYTEβ string tables.SHT_SYMTABβELF_T_SYMβ symbol tables.SHT_SYMTAB_SHNDXβELF_T_WORDβ used with extended section numbering.SHT_SUNW_dofβELF_T_BYTEβ used bydtrace(1).SHT_SUNW_moveβELF_T_MOVEβ ELF move records.SHT_SUNW_syminfoβELF_T_SYMINFOβ additional symbol flags.SHT_SUNW_verdefβELF_T_VDEFβ same asSHT_GNU_verdef.SHT_SUNW_verneedβELF_T_VNEEDβ same asSHT_GNU_verneed.SHT_SUNW_versymβELF_T_HALFβ same asSHT_GNU_versym.
also important, bestie: section types in the range[SHT_LOOS, SHT_HIUSER]are otherwise considered to be of typeELF_T_BYTE. π«Ά
β¨ Functional Grouping β¨
okay so this is the brief overview of everything the ELF library can do β every function listed here gets its own manual page, so think of this as the group-chat recap before the deep dives. π
π Archive Access π
elf_getarsym()β retrieve the archive symbol table.elf_getarhdr()β retrieve the archive header for an object.elf_getbase()β retrieve the offset of a member inside an archive.elf_next()β iterate through anar(1)archive.elf_rand()β random access inside anar(1)archive.
π¦ Data Structures π¦
elf_getdata()β retrieve translated data for an ELF section.elf_getscn()β retrieve the section descriptor for a named section.elf_ndxscn()β retrieve the index for a section.elf_newdata()β add a newElf_Datadescriptor to an ELF section.elf_newscn()β add a new section descriptor to an ELF descriptor.elf_nextscn()β iterate through the sections in an ELF object.elf_rawdata()β retrieve untranslated data for an ELF section.elf_rawfile()β return a pointer to the untranslated file contents for an ELF object.elf32_getehdr(), elf64_getehdr()β retrieve the Executable Header in an ELF object.elf32_getphdr(), elf64_getphdr()β retrieve the Program Header Table in an ELF object.elf32_getshdr(), elf64_getshdr()β retrieve the ELF section header associated with anElf_Scndescriptor.elf32_newehdr(), elf64_newehdr()β allocate an Executable Header in an ELF object.elf32_newphdr(), elf64_newphdr()β allocate or resize the Program Header Table in an ELF object.
πΈ Data Translation πΈ
elf32_xlatetof(), elf64_xlatetof()β translate an ELF data structure from its native representation to its file representation.elf32_xlatetom(), elf64_xlatetom()β translate an ELF data structure from its file representation to a native representation.
π Error Reporting π
elf_errno()β retrieve the current error.elf_errmsg()β retrieve a human readable description of the current error.
π§ Initialization π§
elf_begin()β opens anar(1)archive or ELF object given a file descriptor.elf_end()β close an ELF descriptor and release all its resources.elf_memory()β opens anar(1)archive or ELF object present in a memory arena.elf_version()β sets the operating version.
π IO Control π
elf_cntl()β manage the association between an ELF descriptor and its underlying file.elf_flagdata()β mark anElf_Datadescriptor as dirty.elf_flagehdr()β mark the ELF Executable Header in an ELF descriptor as dirty.elf_flagphdr()β mark the ELF Program Header Table in an ELF descriptor as dirty.elf_flagscn()β mark anElf_Scndescriptor as dirty.elf_flagshdr()β mark an ELF Section Header as dirty.elf_setshstrndx()β set the index of the section name string table for the ELF object.elf_update()β recompute ELF object layout and optionally write the modified object back to the underlying file.
π Queries π
elf32_checksum(), elf64_checkum()β compute checksum of an ELF object.elf_getident()β retrieve the identification bytes for an ELF object.elf_getphdrnum()β retrieve the number of program headers in an ELF object.elf_getshdrnum()β retrieve the number of sections in an ELF object.elf_getshdrstrndx()β retrieve the section index of the section name string table in an ELF object.elf_hash()β compute the ELF hash value of a string.elf_kind()β query the kind of object associated with an ELF descriptor.elf32_fsize(), elf64_fsize()β return the size of the file representation of an ELF type.
π Controlling ELF Object Layout π
in the usual mode of operation, the library will compute section offsets and alignments based on the contents of an ELF descriptorβs sections without needing any further intervention from the application. iconic, honestly.
but if the application wishes to take complete charge of the layout of the ELF file, it may set the ELF_F_LAYOUT flag on an ELF descriptor using elf_flagelf(3), and after that 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 elf_update(3) manual page.
gaps in between sections will be filled with the fill character set by function elf_fill(). cute little gap filler, no cap. π
π₯ Error Handling π₯
in case an error is encountered, these library functions set an internal error number and signal the presence of the error by returning a special return value. the application can check the current error number by calling elf_errno(3). a human readable description of the recorded error is available by calling elf_errmsg(3). so yeah, the tea is always available. π
π«Ά Memory Management Rules π«Ά
the library keeps track of all Elf_Scn and Elf_Data descriptors associated with an ELF descriptor and recovers them when the descriptor is closed using elf_end(3). thus the application must not call free(3) on data structures allocated by the ELF library. donβt do it, bestie, thatβs not your bag to free. π
conversely the library will not free data that it has not allocated. as an example, an application may call elf_newdata(3) 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 malloc(3). it is the applicationβs responsibility to free this arena, though the library will reclaim the space used by the Elf_Data descriptor itself. balance restored. β¨
π SEE ALSO π
β HISTORY β
the original elf API was developed for AT&T System V UNIX. the current implementation of the API appeared in FreeBSD 7.0. a true glow-up timeline. π
π AUTHORS π
the ELF library was written by Joseph Koshy <jkoshy@FreeBSD.org>.
and thatβs the page, bestie β source/version: Debian Β· date: June 12, 2019 Β· page: ELF(3). mwah π β¨
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: libelf 15.0.0
- Copyright (c) 2006-2008,2011,2019 Joseph Koshy. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms