LIBELF(3)
π LIBELF β the libelf manual, but make it make sense π
okay bestie, sit down, hydrate, because we're about to talk about the Elfutils library β that's libelf, libelf.so, and -lelf if you're linking, iykyk. This is the whole programmer's manual and I am not skipping a single crumb. π
β¨ NAME β¨
libelf β a library for accessing and manipulating ELF (Executable and Linkable Format) files. that's the whole vibe. π
π LIBRARY π
Elfutils library, fr fr: libelf, libelf.so, -lelf.
π SYNOPSIS π
#include <libelf.h>
#include <gelf.h>
two headers, that's it, that's the whole entry point, slay. β¨
πΈ DESCRIPTION πΈ
So the libelf library hands you an API for reading, writing, and manipulating ELF (Executable and Linkable Format) files. And ELF? ELF is the standard format for object files, shared libraries, core dumps, and executables. if you wanna know more about ELF itself, go read elf(5), it's giving lore. π
libelf gives you routines for working with ELF object file headers, sections, symbol tables, relocation entries, and other key components. the whole squad. π«Ά
The core of the library is based on Elf file descriptors representing ELF files β you can read from them, write to them, or update them in-place (main character energy, no copying required). The elf_begin function initializes access to an ELF object, while other functions like elf_getscn, elf_getdata, and elf_ndxscn give you access to specific parts of the ELF file. literally the friend group of functions. π
π GELF INTERFACE π
<gelf.h> provides a class-independent interface to libelf that transparently handles both ELFCLASS32 and ELFCLASS64 binaries. meaning: you don't have to go pick which libelf function is specialized for which class of ELF binary. it just works. obsessed. π
The GElf_ types defined in <gelf.h> are aliases for their Elf64_ equivalents. Any conversions to Elf32_ types are handled internally by gelf_ functions. Wanna see them? Peep GELF DATA STRUCTURES and GELF MACROS below. π
β οΈ heads up bestie: when you're usinggelf_functions that modify data structure values forELFCLASS32binaries, the size of the value should not exceed the maximum size of theELFCLASS32equivalent. don't be out here overflowing, it's not cute. π
π¦ FILE VS MEMORY REPRESENTATION π¦
The libelf library distinguishes between the file representation of an ELF file and its memory representation. two completely different eras, same girl.
File Representation = the format in which an ELF file is stored on disk. The fields in the file may use specific sizes, alignment, and byte ordering (endianness) that could be different from the native format used by the host system. it's got its own whole aesthetic. π
Memory Representation = the way the ELF data is organized when loaded into an application's memory. In memory, the data structures are typically converted into the native format of the host system (e.g., the system's endianness, word size, and alignment). home turf energy. π
libelf provides these functions to translate ELF data between file and memory representations: elf32_xlatetom, elf64_xlatetom, elf32_xlatetof, and elf64_xlatetof. And <gelf.h> gives you gelf_xlatetom and gelf_xlatetof for class-independent translation between file and memory representations.
See elf32_xlatetom(3) for more info. go read it, it's a good time. β¨
β ELF VERSION β
Because there might be multiple versions of the ELF specification out there (chaos, honestly), the ELF version number must be specified with the elf_version function before any other libelf functions. She sets libelf's ELF version to the specified value. Periodt. π₯
π At this time the only supported ELF version is EV_CURRENT. that's it. that's the one.
π DESCRIPTORS π
Elf descriptors are the central libelf object for accessing and manipulating ELF files. They're created with the elf_begin, elf_clone, and elf_memory functions and closed with the elf_end function. open them, use them, close them β like a good houseguest. π
libelf also provides Elf_Scn and Elf_Data descriptors for ELF sections and section contents, respectively. Members of the Elf_Data struct are described below (scroll down, bestie). Members of the Elf and Elf_Scn structs are hidden from applications. π like, mind your business, they're private.
These descriptors can be acquired and modified using various functions provided by libelf. See libelf.h for a complete list. π
π ERROR HANDLING π
If a libelf function encounters an error it will set an internal error code that you can retrieve with elf_errno. And get this β each thread maintains its own separate error code, so no cross-thread drama. π§΅β¨
The meaning of each error code can be determined with elf_errmsg, which returns a string describing the error. And libelf functions returning a pointer return NULL on failure. classic. π
π§ MEMORY MANAGEMENT π§
libelf manages all of the memory it allocates and frees it with elf_end.
π¨ The application must not callfreeon any memory allocated bylibelf. don't do it. not your memory, not your business. hands off. π βοΈ
π NAMESPACE π
libelf uses the following prefix format. See libelf.h and gelf.h for more information. (iykyk β the prefix tells you exactly what you're dealing with)
elf_β Functions usable with both 32-bit and 64-bit ELF files.elf32_β Functions usable with 32-bit ELF files.elf64_β Functions usable with 64-bit ELF files.Elf_β Type that represents data for both 32-bit and 64-bit ELF files.Elf32_β Type that represents data for 32-bit ELF files.Elf64_β Type that represents data for 64-bit ELF files.ELF_C_βElf_Cmdvalues used in functions such aself_flagsetandelf_cntl.ELF_F_β Flags for ELF structures.ELF_K_βElf_Kindidentification values for recognized object file types.ELF_T_βElf_Typevalues representing the known types of ELF data such asELF_T_BYTE, (unsigned char)ELF_T_REL, (relocation entry) orELF_T_SYM(symbol record).gelf_β Class-independent functions.GElf_β Class-independent types.GELF_β Class-independent macros.
πΈ DATA STRUCTURES πΈ
libelf implements the following data structures, in addition to including the data structures given in the ELF specification (see elf(5) for more information). so this is the bonus content, the extra lore. β¨
π Section Compression Header π
typedef struct {
Elf32_Word ch_type;
Elf32_Word ch_size;
Elf32_Word ch_addralign;
} Elf32_Chdr;
typedef struct {
Elf64_Word ch_type;
Elf64_Word ch_reserved;
Elf64_Xword ch_size;
Elf64_Xword ch_addralign;
} Elf64_Chdr;
ch_typeβ Compression format type. Legal values includeELFCOMPRESS_ZLIB,ELFCOMPRESS_ZSTD, the inclusive range betweenELFCOMPRESS_LOOStoELFCOMPRESS_HIOS, as well as the inclusive range betweenELFCOMPRESS_LOPROCandELFCOMPRESS_HIPROC.ELFCOMPRESS_ZLIBβ ZLIB/DEFLATE algorithm.ELFCOMPRESS_ZSTDβ Zstandard algorithm.ELFCOMPRESS_LOOSβ Start of OS-specific compression types.ELFCOMPRESS_HIOSβ End of OS-specific compression types.ELFCOMPRESS_LOPROCβ Start of processor-specific compression types.ELFCOMPRESS_HIPROCβ End of processor-specific compression types.
ch_reservedβ Space reserved for use bylibelf. (not yours, bestie)ch_sizeβ Data size of uncompressed section.ch_addralignβ Alignment of uncompressed section.
π Section Data π
typedef struct {
void *d_buf;
Elf_Type d_type;
unsigned int d_version;
size_t d_size;
int64_t d_off;
size_t d_align;
} Elf_Data;
d_bufβ Pointer to the actual data. Useelf_getdatato retrieve data in memory representation andelf_rawdatato retrieve data in file representation.d_typeβ TheElf_Typeof this piece of data. See theElf_Typeenum inlibelf.hfor descriptions of each value.d_versionβ The ELF version for this data.d_sizeβ The size in bytes of this data.d_offβ The section offset of this data.d_alignβ The section alignment of this data.
π¦ Archive Member Header π¦
typedef struct {
char *ar_name;
time_t ar_date;
uid_t ar_uid;
gid_t ar_gid;
mode_t ar_mode;
int64_t ar_size;
char *ar_rawname;
} Elf_Arhdr;
ar_nameβ Name of archive member.ar_dataβ File date.ar_uidβ User ID.ar_gidβ Group ID.ar_modeβ File mode.ar_sizeβ File size.ar_rawnameβ Original name of archive member.
β Archive Symbol Table Entry β
typedef struct {
char *as_name;
size_t as_off;
unsigned long int as_hash;
} Elf_Arsym;
as_nameβ Symbol name.as_offβ Offset for this file in the archive.as_hashβ Hash value of the name.
π GELF DATA STRUCTURES π
GElf_ types are aliases for their Elf64_ equivalents. See elf(5) for more information regarding the underlying types. here's the full glow-up chart, every single one: π
GElf_HalfβElf64_HalfGElf_WordβElf64_WordGElf_SwordβElf64_SwordGElf_XwordβElf64_XwordGElf_SxwordβElf64_SxwordGElf_AddrβElf64_AddrGElf_OffβElf64_OffGElf_EhdrβElf64_EhdrGElf_ShdrβElf64_ShdrGElf_SectionβElf64_SectionGElf_SymβElf64_SymGElf_SyminfoβElf64_SyminfoGElf_RelβElf64_RelGElf_RelaβElf64_RelaGElf_RelrβElf64_RelrGElf_PhdrβElf64_PhdrGElf_ChdrβElf64_ChdrGElf_DynβElf64_DynGElf_VerdefβElf64_VerdefGElf_VerdauxβElf64_VerdauxGElf_VerneedβElf64_VerneedGElf_VernauxβElf64_VernauxGElf_VersymβElf64_VersymGElf_auxv_tβElf64_auxv_tGElf_NhdrβElf64_NhdrGElf_MoveβElf64_MoveGElf_LibβElf64_Lib
π GELF MACROS π
All GELF_ macros are aliases for their ELF64_ equivalents. See elf(5) for more information regarding the underlying macros. same energy, macro edition: π
GELF_ST_BINDβELF64_ST_BINDGELF_ST_TYPEβELF64_ST_TYPEGELF_ST_INFOβELF64_ST_INFOGELF_ST_VISIBILITYβELF64_ST_VISIBILITYGELF_M_INFOβELF64_M_INFOGELF_M_SIZEβELF64_M_SIZEGELF_M_SYMβELF64_M_SYMGELF_R_INFOβELF64_R_INFOGELF_R_TYPEβELF64_R_TYPEGELF_R_SYMβELF64_R_SYM
π«Ά REPORTING BUGS π«Ά
Found a bug? (not me manifesting one π) Report bugs to <elfutils-devel@sourceware.org> or at https://sourceware.org/bugzilla/. the devs wanna hear it, fr fr. π
and that's the whole page, bestie!! π¦ this was the Libelf Programmer's Manual, dated 2024-10-18, and the page itself is filed under LIBELF(3). go forth and manipulate some ELF files responsibly, mwah π β¨πΈ
π¦ where this page comes from
- alpine 3.24: elfutils-doc 0.195-r0 Β· GPL-3.0-or-later AND ( GPL-2.0-or-later OR LGPL-3.0-or-later )
- arch rolling: libelf 0.196-1
- arch rolling: libelf 0.197-1 Β· GPL-2.0-or-later OR LGPL-3.0-or-later
- centos-stream 10: elfutils-libelf-devel 0.197-1.el10 Β· GPL-2.0-or-later OR LGPL-3.0-or-later
- centos-stream 9: elfutils-libelf-devel 0.197-1.el9 Β· GPL-2.0-or-later OR LGPL-3.0-or-later
- centos-stream 9: elfutils-libelf-devel 0.197-1.el9 Β· GPL-2.0-or-later OR LGPL-3.0-or-later
- fedora 43: elfutils-libelf-devel 0.196-1.fc43
- fedora 43: elfutils-libelf-devel 0.196-1.fc43
- fedora 43: elfutils-libelf-devel 0.197-1.fc43 Β· GPL-2.0-or-later OR LGPL-3.0-or-later
- fedora 43: elfutils-libelf-devel 0.197-1.fc43 Β· GPL-2.0-or-later OR LGPL-3.0-or-later
- fedora 44: elfutils-libelf-devel 0.196-1.fc44
- fedora 44: elfutils-libelf-devel 0.196-1.fc44
- fedora 44: elfutils-libelf-devel 0.197-1.fc44 Β· GPL-2.0-or-later OR LGPL-3.0-or-later
- fedora 44: elfutils-libelf-devel 0.197-1.fc44 Β· GPL-2.0-or-later OR LGPL-3.0-or-later
- nixpkgs unstable: elfutils 0.195
- ubuntu stonking: libelf-dev 0.196-1 Β· GPL-3.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms