---
name: "gelf_newphdr"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/elf32_newphdr"
---

# gelf\_newphdr(3)

allocate an ELF program header table

Part of [elf32\_newphdr(3)](/elf32_newphdr/3.md).

## NAME

**elf32\_newphdr**, **elf64\_newphdr**, **gelf\_newphdr** – allocate an ELF program header table

## LIBRARY

library “libelf”

## SYNOPSIS

**#include \<libelf.h\>**

*Elf32\_Phdr \**\

**elf32\_newphdr**(*Elf \*elf*, *size\_t count*);

*Elf64\_Phdr \**\

**elf64\_newphdr**(*Elf \*elf*, *size\_t count*);

**#include \<gelf.h\>**

*void \**\

**gelf\_newphdr**(*Elf \*elf*, *size\_t count*);

## DESCRIPTION

These functions allocate an ELF Program Header table for an ELF descriptor. *Elf32\_Phdr* and *Elf64\_Phdr* descriptors are described further in [elf(5)](/elf/5.md).

Functions **elf32\_newphdr**() and **elf64\_newphdr**() allocate a table of *count* *Elf32\_Phdr* and *Elf64\_Phdr* descriptors respectively, discarding any existing program header table already present in the ELF descriptor *elf*. A value of zero for argument *count* may be used to delete an existing program header table from an ELF descriptor.

Function **gelf\_newphdr**() will return a table of *Elf32\_Phdr* or *Elf64\_Phdr* with *count* elements depending on the ELF class of ELF descriptor *elf*.

The functions set the ELF\_F\_DIRTY flag on the program header table. All members of the returned array of Phdr structures will be initialized to zero.

After a successful call to these functions, the pointer returned by a prior call to **elf32\_getphdr**() or **elf64\_getphdr**() on the same descriptor *elf* will no longer be valid.

## RETURN VALUES

The functions a valid pointer if successful, or NULL in case an error was encountered.

## COMPATIBILITY

The **gelf\_newphdr**() function uses a type of *void \** for its returned value. This differs from some other implementations of the [elf(3)](/elf/3.md) API, which use an *unsigned long* return type.

## ERRORS

These functions may fail with the following errors:

- \[ELF\_E\_ARGUMENT\]

  Argument *elf* was NULL.

- \[ELF\_E\_ARGUMENT\]

  Argument *elf* was not a descriptor for an ELF object.

- \[ELF\_E\_CLASS\]

  ELF descriptor *elf* was of an unrecognized class.

- \[ELF\_E\_RESOURCE\]

  An out of memory condition was detected.

- \[ELF\_E\_SEQUENCE\]

  An executable header was not allocated for ELF descriptor *elf* before using these APIs.

## SEE ALSO

[elf(3)](/elf/3.md), [elf32\_getphdr(3)](/elf32_getphdr/3.md), [elf32\_newehdr(3)](/elf32_newehdr/3.md), [elf64\_getphdr(3)](/elf64_getphdr/3.md), [elf64\_newehdr(3)](/elf64_newehdr/3.md), [elf\_flagphdr(3)](/elf_flagphdr/3.md), [elf\_getphnum(3)](/elf_getphnum/3.md), [gelf(3)](/gelf/3.md), [gelf\_getphdr(3)](/gelf_getphdr/3.md), [gelf\_newehdr(3)](/gelf_newehdr/3.md), [elf(5)](/elf/5.md)
