ELF_GETSHDRSTRNDX(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_GETSHDRSTRNDX(3) πŸ’–

okay bestie, welcome to the Libelf Programmer's Manual, section 3 β€” this is the page for elf_getshdrstrndx and its lil deprecated twin elf_getshstrndx. we're getting the section header string table index and we're gonna be so normal about it πŸ’…


✨ NAME ✨

elf_getshdrstrndx, elf_getshstrndx β€” retrieve the section header string table index πŸŽ€


✨ SYNOPSIS ✨

#include <libelf.h>

int elf_getshdrstrndx("Elf *elf", size_t *dst );
int elf_getshstrndx("Elf *elf", size_t *dst ); (deprecated)

yes bestie, those quotes are literally in there. we don't make the rules, we just transcribe them. πŸ™ˆ


✨ DESCRIPTION ✨

so the way that elf_getshdrstrndx() works is: it grabs the section header string table index out of the ELF descriptor elf, and it drops it right into the spot that dst is pointing at. that's it. that's the whole job. 🫢

if you wanna actually get a section's name, you hit up elf_strptr(3). and listen β€” that function needs two things from you: the section header string table index that elf_getshdrstrndx() just handed you, plus the sh_name field from the section's Elf32_Shdr or Elf64_Shdr (peep elf32_getshdr and elf64_getshdr for those). iykyk 🌸

now here's the spicy part: this function handles both standard ELF values and extended section indexing. so in that extended case, if the section header string table index field of the ELF header contains SHN_XINDEX, then the real index is chilling in the sh_link field of section 0. sneaky but iconic. πŸ‘‘

and elf_getshstrndx()? that's just a deprecated alias for elf_getshdrstrndx(). do not use it in new code, bestie, i'm begging (see HISTORY). it's giving legacy. πŸ’€


✨ PARAMETERS ✨

  • elf β€” a pointer to an Elf descriptor. it must have kind ELF_K_ELF. and if elf is NULL, then this function returns -1 without setting elf_errno. no drama, no errno, just a -1. 😭
  • dst β€” a pointer to a size_t location where the index of the section header string table gets stored. must not be NULL. fr fr, don't do it.

✨ RETURN VALUE ✨

  • returns 0 on success and stores the section header string table index in dst. slay. ✨
  • returns -1 on failure and sets elf_errno (as long as elf is not NULL).

✨ SEE ALSO ✨

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

go read those next, bestie, they're the supporting cast πŸ¦‹


✨ ATTRIBUTES ✨

here's the vibe check on thread safety:

  • Interface: elf_getshdrstrndx(), elf_getshstrndx()
  • Attribute: Thread safety
  • Value: MT-Safe πŸ”₯

so yeah, she's thread safe. go forth and multithread. 🌈


✨ REPORTING BUGS ✨

found a bug? spill it to <elfutils-devel@sourceware.org> or hit up https://sourceware.org/bugzilla/. we wanna know. πŸ“


✨ HISTORY ✨

elf_getshdrstrndx first appeared in elfutils 0.142. why does it exist, you ask? because elf_getshstrndx had return value inconsistencies between different libelf implementations. messy. so elf_getshdrstrndx was introduced to ensure that 0 is returned on success. consistency queen behavior, honestly. πŸ‘‘


lowkey the whole moral of this page: use elf_getshdrstrndx(), not the deprecated one, and check that dst isn't NULL. periodt. πŸ’…

okay that's the whole page, you ate it πŸ’– this was Libelf, dated 2025-06-30 β€” mwah, go write some ELF code and text me when it compiles πŸŽ€βœ¨πŸ¦‹

πŸ“¦ where this page comes from