ELF_BEGIN(3)

πŸ“¦
version 0.197 Β· 3 versions
, in alpine 3.23, alpine 3.24, arch rolling, centos-stream 10, centos-stream 9, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, ubuntu resolute, ubuntu stonking

πŸ‘‘ 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, if ref is non-NULL, then fildes must either be -1 or be set to the same file descriptor as the one associated with ref. Like, they have to match, bestie, no third options. πŸ’€
  • cmd β€” the action you wanna perform. The common ones are:
    • ELF_C_NULL β€” returns a NULL pointer instead of initializing an ELF descriptor. It straight-up ignores ref. 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 using mmap, if available. Now listen β€” the ELF_C_*_MMAP commands 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 the ELF_C_*_MMAP commands 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 using mmap, if available. The descriptor initially refers to an empty file. ✨
    • ELF_C_RDWR_MMAP β€” opens an ELF descriptor for reading and writing using mmap, if available. πŸ”₯
    • ELF_C_READ_MMAP_PRIVATE β€” opens an ELF descriptor for reading using mmap, if available. This one invokes mmap with MAP_PRIVATE, whereas the other ELF_C_*_MMAP commands invoke mmap with MAP_SHARED. Peep mmap(2) for the full tea. πŸ“
  • ref β€” a reference to an existing Elf descriptor. Here's where it gets interesting: πŸ‘€
    • If ref refers to a regular ELF binary (not an AR file), then elf_begin will duplicate ref. The reference count associated with ref will be incremented, and elf_end(3) will need to be called an additional time to deallocate ref. And ref must have been opened with read/write permissions consistent with cmd. Consistency is the moment. ✨
    • If ref refers to an AR file, then the ELF descriptor returned will be the first available object member of the archive. See elf_next(3) for more info. πŸ“š
    • ref may be NULL, in which case this argument is simply ignored. Not me ghosting an argument. πŸ’€

🌟 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