ELF\_GETSHDRNUM(3) Libelf Programmer's Manual ELF\_GETSHDRNUM(3)

# [NAME](#NAME)

elf\_getshdrnum, elf\_getshnum - retrieve the number of section headers in an ELF file

# [SYNOPSIS](#SYNOPSIS)

```
#include <libelf.h>
int elf_getshdrnum("Elf *elf", size_t *dst );
int elf_getshnum("Elf *elf", size_t *dst ); (deprecated)
```

# [DESCRIPTION](#DESCRIPTION)

The **elf\_getshdrnum**() function stores the number of section headers (sections) associated with the ELF descriptor *elf* into the variable pointed to by *dst*.

It transparently handles both standard and extended section header counts. If the number of section headers exceeds the representable range of the *e\_shnum* field in the ELF header, the ELF specification allows encoding the true count in section header 0’s *sh\_size* field, and sets *e\_shnum* to 0. This function transparently handles that case.

The function **elf\_getshnum**() is a deprecated alias for **elf\_getshdrnum**() and should not be used in new code (see HISTORY).

# [PARAMETERS](#PARAMETERS)

[*elf*](#elf)

A pointer to an **Elf** descriptor opened with [**elf\_begin**(3)](/man/os/elfutils/elf_begin.3). The descriptor must represent a file of kind **ELF\_K\_ELF .** If NULL, the function returns -1 without setting elf\_errno.

[*dst*](#dst)

Pointer to a **size\_t** variable. On success, the number of section headers is written here. Must not be NULL.

# [RETURN VALUE](#RETURN_VALUE)

Returns 0 on success and stores the result in *dst*. Returns -1 on failure.

# [SEE ALSO](#SEE_ALSO)

[**elf32\_getshdr**(3)](/man/os/elfutils/elf32_getshdr.3), [**elf64\_getshdr**(3)](/man/os/elfutils/elf64_getshdr.3), [**libelf**(3)](/man/os/elfutils/libelf.3), [**elf**(5)](/man/os/elfutils/elf.5)

# [ATTRIBUTES](#ATTRIBUTES)

**Interface** **Attribute** **Value** elf\_getshdrnum (), elf\_getshnum() Thread safety MT-Safe

# [REPORTING BUGS](#REPORTING_BUGS)

Report bugs to &lt;elfutils-devel@sourceware.org&gt; or https://sourceware.org/bugzilla/.

# [HISTORY](#HISTORY)

**elf\_getshdrnum** first appeared in elfutils 0.142. This function was added due to elf\_getshnum return value inconsistencies between different libelf implementations. **elf\_getshdrnum** was introduced to ensure that 0 is returned on success across all libelf implementations.

2025-06-30 Libelf

---

Document: elf_getshdrnum(3) from elfutils 0.197-r0 (Chainguard OS)
Source: https://sourceware.org/git/elfutils.git
