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

# [NAME](#NAME)

elf\_getphdrnum - retrieve the number of program headers in an ELF file

# [SYNOPSIS](#SYNOPSIS)

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

# [DESCRIPTION](#DESCRIPTION)

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

If the number of program headers exceeds the representable range of the *e\_phnum* field in the ELF header, the ELF specification allows encoding the true count in section header 0’s *sh\_info* field, and sets *e\_phnum* to the reserved value **PN\_XNUM** (always 0xFFFF). This function transparently handles that case.

The function also ensures the returned count of program headers does not exceed the number that can actually be addressed in the program header table. This is important when indexing into the array returned by **elf32\_getphdr**() or **elf64\_getphdr**().

# [PARAMETERS](#PARAMETERS)

[*elf*](#elf)

A pointer to an **Elf** descriptor. The descriptor must represent an ELF binary with *Elf\_Kind* **ELF\_K\_ELF .**

[*dst*](#dst)

On success, the number of program headers in *elf* 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 and sets elf\_errno. Returns -1 without setting errno if **elf** is NULL .

# [SEE ALSO](#SEE_ALSO)

[**elf32\_getphdr**(3)](/man/os/elfutils/elf32_getphdr.3), [**elf64\_getphdr**(3)](/man/os/elfutils/elf64_getphdr.3), [**libelf**(3)](/man/os/elfutils/libelf.3), [**elf**(5)](/man/os/elfutils/elf.5)

# [ATTRIBUTES](#ATTRIBUTES)

**Interface** **Attribute** **Value** elf\_getphdrnum () Thread safety MT-Safe

# [REPORTING BUGS](#REPORTING_BUGS)

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

2025-06-30 Libelf

---

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