ELF_BEGIN(3)
π ELF_BEGIN β okay bestie, let's open a file β¨
So this is elf_begin, section 3 of the Libelf Programmer's Manual, and it's literally the moment where your ELF file gets a handle, a little passport that says "yes, I'm in this now." π
πΈ NAME πΈ
elf_begin β initialize an ELF descriptor. That's it, that's the job, and it slays at it. π«Ά
π SYNOPSIS π
#include <libelf.h>
Elf *elf_begin(int fildes, Elf_Cmd cmd, Elf *ref);
π DESCRIPTION π
Okay so picture this: you've got an ELF file just sitting there, unbothered, minding its business. elf_begin is the function that walks up and says "hey, come with me." It initializes and returns a handle to an ELF file so you can use it with the elfutils libelf library and the related elfutils libraries like libdw. π¦
That Elf descriptor you get back? You must release it with elf_end(3) when you're done. It's giving "borrowed my hoodie" energy β you gotta give it back. π
And before you touch any libelf library, including elf_begin(3) itself, you must call elf_version(3) first. No cap. That's the vibe check you do before the party. β¨
π PARAMETERS π
fildesβ a file descriptor pointing at an ELF object. It should be open for reading, and optionally for writing, depending on what you're tryna do. β Now, ifrefis non-NULL, thenfildesmust either be-1or be set to the same file descriptor as the one associated withref. Like, they have to match, bestie, no third options. πcmdβ the action you wanna perform. The common ones are:ELF_C_NULLβ returns aNULLpointer instead of initializing an ELF descriptor. It straight-up ignoresref. Not rude, just honest. π§ELF_C_READβ opens an ELF descriptor for reading. The classic. πELF_C_WRITEβ opens an ELF descriptor for writing. The descriptor initially refers to an empty file. Blank canvas, main character energy. πELF_C_RDWRβ opens an ELF descriptor for reading and writing. Why choose one. πΈELF_C_READ_MMAPβ opens an ELF descriptor for reading usingmmap, if available. Now listen β theELF_C_*_MMAPcommands are an elfutils libelf extension and may not be available in other libelf implementations. β οΈ Also: once the mmap size is set, attempts to extend the size may fail. So theELF_C_*_MMAPcommands tend to be more useful for in-place modifications or for removing data from an ELF descriptor. Basically: great for glow-ups in place, not for growing. πELF_C_WRITE_MMAPβ opens an ELF descriptor for writing usingmmap, if available. The descriptor initially refers to an empty file. β¨ELF_C_RDWR_MMAPβ opens an ELF descriptor for reading and writing usingmmap, if available. π₯ELF_C_READ_MMAP_PRIVATEβ opens an ELF descriptor for reading usingmmap, if available. This one invokesmmapwithMAP_PRIVATE, whereas the otherELF_C_*_MMAPcommands invokemmapwithMAP_SHARED. Peepmmap(2)for the full tea. π
refβ a reference to an existingElfdescriptor. Here's where it gets interesting: π- If
refrefers to a regular ELF binary (not an AR file), thenelf_beginwill duplicateref. The reference count associated withrefwill be incremented, andelf_end(3)will need to be called an additional time to deallocateref. Andrefmust have been opened with read/write permissions consistent withcmd. Consistency is the moment. β¨ - If
refrefers to an AR file, then the ELF descriptor returned will be the first available object member of the archive. Seeelf_next(3)for more info. π refmay beNULL, in which case this argument is simply ignored. Not me ghosting an argument. π
- If
π RETURN VALUE π
On success, elf_begin() returns a pointer to a new Elf descriptor. Periodt. π«Ά
If cmd is ELF_C_NULL then NULL is returned. If ref is non-NULL and isn't an AR file, then a copy of ref is returned. And on failure, elf_begin() returns NULL and sets an internal error state that you can retrieve with elf_errmsg(3). So if it flops, it tells you why β you just have to ask. π
π SEE ALSO π
mmap(2), elf_clone(3), elf_end(3), elf_next(3), elf_rand(3), libelf(3), elf(5)
The whole friend group. Go say hi. π«Ά
π¦ ATTRIBUTES π¦
- Interface:
elf_begin() - Attribute: Thread safety
- Value: MT-Safe
She's MT-Safe, bestie. Multiple threads can hang out with her and nobody's fighting. π
π¨ REPORTING BUGS π¨
Found a bug? Don't suffer in silence, tell the devs! Report bugs to <elfutils-devel@sourceware.org> or hit up https://sourceware.org/bugzilla/. They wanna know. fr fr. π
And that's the whole page, mwah π β brought to you by Libelf, dated 2025-06-02, filed under ELF_BEGIN(3). Now go call elf_version(3) first, I'm begging you. πβ¨
π¦ where this page comes from
- alpine 3.23: elfutils-doc 0.194-r0 Β· GPL-3.0-or-later AND ( GPL-2.0-or-later OR LGPL-3.0-or-later )
- 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.194-5.fc44 Β· GPL-2.0-or-later OR LGPL-3.0-or-later
- fedora 44: elfutils-libelf-devel 0.194-5.fc44 Β· 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
- nixos 26.05: elfutils 0.194
- nixpkgs unstable: elfutils 0.195
- ubuntu resolute: libelf-dev 0.194-4 Β· GPL-3.0-or-later
- 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