ELF_FLAGELF(3)

πŸ“¦
version 0.197 Β· 2 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_FLAGELF πŸ’– β€” from the Libelf Programmer's Manual

okay bestie, sit down, we're doing libelf flag manipulation and honestly? it's giving main character energy ✨

🌸 NAME 🌸

elf_flagelf, elf_flagdata, elf_flagehdr, elf_flagphdr, elf_flagscn, elf_flagshdr β€” set or clear flags on ELF descriptors and components. six little functions, one big vibe. 🫢


✨ SYNOPSIS ✨

#include <libelf.h>

unsigned int elf_flagelf(Elf * elf, Elf_Cmd cmd, unsigned int flags);
unsigned int elf_flagehdr(Elf * elf, Elf_Cmd cmd, unsigned int flags);
unsigned int elf_flagphdr(Elf * elf, Elf_Cmd cmd, unsigned int flags);
unsigned int elf_flagscn(Elf_Scn * scn, Elf_Cmd cmd, unsigned int flags);
unsigned int elf_flagshdr(Elf_Scn * scn, Elf_Cmd cmd, unsigned int flags);
unsigned int elf_flagdata(Elf_Data * data, Elf_Cmd cmd, unsigned int flags);

like, look at them. all lined up. so tidy. πŸ’…


πŸ’œ DESCRIPTION πŸ’œ

So these functions let you, the caller, set or clear flags on the various ELF components that libelf is managing. They're the "hey bestie, I changed something" API. Typically you use them to mark a component as modified (aka dirty 🫧), or to control layout behavior when stuff gets written out.

  • elf_flagelf() β€” modifies flags on the top-level Elf descriptor. the big boss. the CEO. πŸ‘‘
  • elf_flagdata() β€” modifies flags on an Elf_Data descriptor, usually one you got from elf_getdata(3) or made with elf_newdata(3).
  • elf_flagehdr() β€” modifies flags on the ELF header for an ELF descriptor (like, the thing gelf_getehdr(3) hands you).
  • elf_flagphdr() β€” modifies flags on the program header table of an ELF descriptor.
  • elf_flagscn() β€” modifies flags on an Elf_Scn (section) descriptor.
  • elf_flagshdr() β€” modifies flags on the section header of a given Elf_Scn.

The cmd argument is literally just deciding: are we setting these flags (ELF_C_SET) or clearing them (ELF_C_CLR)? that's it. that's the whole job. πŸ™ˆ

The following flag values may be passed via the flags parameter:

  • ELF_F_DIRTY β€” marks the object as modified. This makes sure changes to the component get written out during elf_update(3). if you changed it and you want it on disk, this is the one, no cap. ✍️
  • ELF_F_LAYOUT β€” says that the layout of the ELF component is managed manually by you. Offsets and sizes will be preserved exactly as provided. This flag is only used with elf_flagelf() and is ignored when used with the other functions. If this flag is set, the user must set Ehdr e_phoff, e_shoff, as well as Elf_Data d_off and Shdr sh_offset. If it's not set, then those offsets will be recalculated when elf_update is called. And heads up: if ELF_F_LAYOUT is set, then changes to phdr offsets will prevent elf_update from automatically adjusting shdr offsets. πŸ‘€
  • ELF_F_PERMISSIVE β€” enables relaxed validation of object structure. This flag permits nonstandard layouts without strict checks. Specifically, it removes a check in elf_update() that the section sh_size is a multiple of the section sh_entsize (if not zero). This flag is only used with elf_flagelf() and is ignored when used with the other functions. ELF_F_PERMISSIVE is an elfutils libelf extention and may not be available in other libelf implementations. (yes it says "extention" in the original, we don't fix, we report πŸŽ€)
⚠️ real talk: ELF_F_LAYOUT and ELF_F_PERMISSIVE are only for elf_flagelf(). everywhere else they get ignored. don't be out here passing them to elf_flagshdr and expecting a moment. πŸ’€

πŸ¦‹ PARAMETERS πŸ¦‹

  • obj β€” the object to be modified. For elf_flagelf, elf_flagehdr, elf_flagphdr, this is an Elf * descriptor. For elf_flagscn and elf_flagshdr, this is an Elf_Scn * descriptor. For elf_flagdata, an Elf_Data * buffer.
  • cmd β€” must be either: ELF_C_SET to set the given flags, or ELF_C_CLR to clear them. pick one, bestie, there's no third option. ⭐
  • flags β€” bitmask of flags to modify. May include any combination of ELF_F_DIRTY, ELF_F_LAYOUT, and ELF_F_PERMISSIVE.

Note that ELF_F_LAYOUT and ELF_F_PERMISSIVE are only used with elf_flagelf() and are ignored for other functions. (say it with me one more time for the people in the back πŸ“£)


🌈 RETURN VALUE 🌈

Returns the new value of the flags after the set/clear operation. so you always know what you're working with. slay. πŸ’–

Returns 0 on error, and sets an error code retrievable via elf_errmsg(3).

If the first argument is NULL then 0 is return and no error code is set.

If the last flag is cleared, 0 will be returned as the new flag value. In this case 0 is not an error indicator and no error code is set.

😭 this is the classic "is 0 bad or is 0 just empty" situation. read carefully. zero can mean "everything's cleared, we're chilling" OR "something went wrong". context is everything, iykyk.

🧚 SEE ALSO 🧚

elf_errmsg(3), elf_update(3), libelf(3), elf(5)


πŸŽ€ ATTRIBUTES πŸŽ€

  • Interface: elf_flagelf(), elf_flagehdr(), elf_flagphdr(), elf_flagscn(), elf_flagshdr(), elf_flagdata()
  • Attribute: Thread safety
  • Value: MT-Unsafe race
so like, all six of them, same energy: MT-Unsafe race. be careful out here in multithreaded land. 🫧

πŸ“ REPORTING BUGS πŸ“

Report bugs to <elfutils-devel@sourceware.org> or https://sourceware.org/bugzilla/.

found a bug? tell the girlies at sourceware. they'll handle it. 🫢


and that's the whole page, bestie β€” brought to you by Libelf, dated 2025-06-23, living in section 3 of your friendly neighborhood manual. go forth and flag responsibly. periodt. πŸ’…βœ¨

πŸ“¦ where this page comes from