---
name: "elf64_newehdr"
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_newehdr"
---

# elf64\_newehdr(3)

retrieve or allocate the object file header

Part of [elf32\_newehdr(3)](/elf32_newehdr/3.md).

## NAME

**elf32\_newehdr**, **elf64\_newehdr**, **gelf\_newehdr** – retrieve or allocate the object file header

## LIBRARY

library “libelf”

## SYNOPSIS

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

*Elf32\_Ehdr \**\

**elf32\_newehdr**(*Elf \*elf*);

*Elf64\_Ehdr \**\

**elf64\_newehdr**(*Elf \*elf*);

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

*void \**\

**gelf\_newehdr**(*Elf \*elf*, *int elfclass*);

## DESCRIPTION

These functions retrieve the ELF header from the ELF descriptor *elf*, allocating a new header if needed. File data structures are translated to their in-memory representations as described in [elf(3)](/elf/3.md).

Function **elf32\_newehdr**() returns a pointer to a 32 bit *Elf32\_Ehdr* structure. Function **elf64\_newehdr**() returns a pointer to a 64 bit *Elf64\_Ehdr* structure.

When argument *elfclass* has value ELFCLASS32, function **gelf\_newehdr**() returns the value returned by **elf32\_newehdr**(*elf*). When argument *elfclass* has value ELFCLASS64 it returns the value returned by **elf64\_newehdr**(*elf*).

If a fresh header structure is allocated, the members of the structure are initialized as follows:

- *e\_ident\[EI\_MAG0..EI\_MAG3\]*

  Identification bytes at offsets EI\_MAG0, EI\_MAG1, EI\_MAG2 and EI\_MAG3 are set to the ELF signature.

- *e\_ident\[EI\_CLASS\]*

  The identification byte at offset EI\_CLASS is set to the ELF class associated with the function being called or to argument *elfclass* for function **gelf\_newehdr**().

- *e\_ident\[EI\_DATA\]*

  The identification byte at offset EI\_DATA is set to ELFDATANONE.

- *e\_ident\[EI\_VERSION\]*

  The identification byte at offset EI\_VERSION is set to the ELF library's operating version set by a prior call to [elf\_version(3)](/elf_version/3.md).

- *e\_machine*

  is set to EM\_NONE.

- *e\_type*

  is set to ELF\_K\_NONE.

- *e\_version*

  is set to the ELF library's operating version set by a prior call to [elf\_version(3)](/elf_version/3.md).

Other members of the header are set to zero. The application is responsible for changing these values as needed before calling **elf\_update**().

If successful, these three functions set the ELF\_F\_DIRTY flag on ELF descriptor *elf*.

## RETURN VALUES

These functions return a pointer to a translated header descriptor if successful, or NULL on failure.

## COMPATIBILITY

The **gelf\_newehdr**() 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 can fail with the following errors:

- \[ELF\_E\_ARGUMENT\]

  The argument *elf* was null.

- \[ELF\_E\_ARGUMENT\]

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

- \[ELF\_E\_ARGUMENT\]

  Argument *elfclass* had an unsupported value.

- \[ELF\_E\_ARGUMENT\]

  The class of the ELF descriptor *elf* did not match that of the requested operation.

- \[ELF\_E\_ARGUMENT\]

  For function **gelf\_newehdr**(), the class of argument *elf* was not ELFCLASSNONE and did not match the argument *elfclass*.

- \[ELF\_E\_CLASS\]

  The ELF class of descriptor *elf* did not match that of the API function being called.

- \[ELF\_E\_HEADER\]

  A malformed ELF header was detected.

- \[ELF\_E\_RESOURCE\]

  An out of memory condition was detected during execution.

- \[ELF\_E\_SECTION\]

  The ELF descriptor in argument *elf* did not adhere to the conventions used for extended numbering.

- \[ELF\_E\_VERSION\]

  The ELF descriptor *elf* had an unsupported ELF version number.

## SEE ALSO

[elf(3)](/elf/3.md), [elf32\_getehdr(3)](/elf32_getehdr/3.md), [elf64\_getehdr(3)](/elf64_getehdr/3.md), [elf\_flagdata(3)](/elf_flagdata/3.md), [elf\_getident(3)](/elf_getident/3.md), [elf\_update(3)](/elf_update/3.md), [elf\_version(3)](/elf_version/3.md), [gelf(3)](/gelf/3.md), [gelf\_getehdr(3)](/gelf_getehdr/3.md), [elf(5)](/elf/5.md)
