ELF_GETSHDRNUM(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_GETSHDRNUM(3) β€” the libelf glow-up πŸ’–

okay bestie, sit down, hydrate, because we're about to learn how to count section headers in an ELF file and honestly? it's giving ✨main character energy✨. this is libelf, and this page is about elf_getshdrnum (and its kinda cringe deprecated cousin elf_getshnum). πŸ¦‹


🌸 NAME 🌸

elf_getshdrnum, elf_getshnum β€” retrieve the number of section headers in an ELF file πŸŽ€


✨ SYNOPSIS ✨

#include <libelf.h>

int elf_getshdrnum("Elf *elf", size_t *dst );
int elf_getshnum("Elf *elf", size_t *dst ); (deprecated)

πŸ’… DESCRIPTION πŸ’…

so. elf_getshdrnum() takes the number of section headers (aka sections) that are associated with the ELF descriptor elf, and stores it into the variable that dst points at. literally that's it, that's the whole job, and it does it flawlessly. πŸ‘‘

the way that it's lowkey a genius function: it transparently handles both standard and extended section header counts. like, if the number of section headers exceeds the representable range of the e_shnum field in the ELF header, the ELF specification allows the true count to be encoded in section header 0's sh_size field, and sets e_shnum to 0. this function handles that case transparently. no cap, it just deals with it for you so you never have to think about it. 🌈

meanwhile elf_getshnum() is a deprecated alias for elf_getshdrnum() and should not be used in new code (go peek at HISTORY if you want the tea β˜•).


πŸŽ€ PARAMETERS πŸŽ€

  • elf β€” a pointer to an Elf descriptor opened with elf_begin(3). the descriptor has to represent a file of kind ELF_K_ELF. and if it's NULL? the function returns -1 without setting elf_errno. rude but fair. πŸ™ˆ
  • dst β€” pointer to a size_t variable. on success, the number of section headers gets written right here. it must not be NULL. πŸ’€

⭐ RETURN VALUE ⭐

returns 0 on success and stores the result in dst. returns -1 on failure. that's the whole vibe check: zero good, negative one bad. 🫢


πŸ‘‘ SEE ALSO πŸ‘‘

elf32_getshdr(3), elf64_getshdr(3), libelf(3), elf(5)


πŸ’œ ATTRIBUTES πŸ’œ

  • elf_getshdrnum(), elf_getshnum() β€” Thread safety β€” MT-Safe
so yes, you can absolutely bring this function to the group chat, it's thread-safe bestie. MT-Safe and unbothered. πŸ’…

πŸ› REPORTING BUGS πŸ›

found a bug? report it to <elfutils-devel@sourceware.org> or hit up https://sourceware.org/bugzilla/. they want to know, fr fr. 🌸


πŸ“œ HISTORY πŸ“œ

elf_getshdrnum first appeared in elfutils 0.142. it got added because elf_getshnum had return value inconsistencies between different libelf implementations (messy, honestly 😭). elf_getshdrnum was introduced to ensure that 0 is returned on success across all libelf implementations. consistency slay. periodt. ✨


and that's the page, bestie β€” written up as part of the Libelf Programmer's Manual, dated 2025-06-30, tagged ELF_GETSHDRNUM(3). go forth and count those section headers. mwah πŸ’–πŸ¦‹πŸŽ€

πŸ“¦ where this page comes from