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

# elf\_open(3)

open ELF objects and ar(1) archives

## NAME

**elf\_open**, **elf\_openmemory** – open ELF objects and [ar(1)](/ar/1.md) archives

## LIBRARY

library “libelf”

## SYNOPSIS

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

*Elf \**\

**elf\_open**(*int fd*);

*Elf \**\

**elf\_openmemory**(*char \*image*, *size\_t sz*);

## DESCRIPTION

*Important*: The functions **elf\_open**() and **elf\_openmemory**() are extensions to the [elf(3)](/elf/3.md) API, for the internal use of the Elftoolchain project. Portable applications should not use these functions.

The function **elf\_open**() returns an Elf descriptor opened with mode ELF\_C\_READ for the ELF object or [ar(1)](/ar/1.md) archive referenced by the file descriptor in argument *fd*.

The function **elf\_openmemory**() returns an ELF descriptor opened with mode ELF\_C\_READ for the ELF object or [ar(1)](/ar/1.md) archive contained in the memory area pointed to by the argument *image*. The argument *sz* specifies the size of the memory area in bytes.

## RETURN VALUES

The function returns a pointer to a ELF descriptor if successful, or NULL if an error occurred.

## COMPATIBILITY

These functions are non-standard extensions to the [elf(3)](/elf/3.md) API set.

The behavior of these functions differs from their counterparts [elf\_begin(3)](/elf_begin/3.md) and [elf\_memory(3)](/elf_memory/3.md) in that these functions will successfully open malformed ELF objects and [ar(1)](/ar/1.md) archives, returning an Elf descriptor of type ELF\_K\_NONE.

## ERRORS

These functions can fail with the following errors:

- \[ELF\_E\_ARGUMENT\]

  The argument *fd* was of an unsupported file type.

- \[ELF\_E\_ARGUMENT\]

  The argument *sz* was zero, or the argument *image* was NULL.

- \[ELF\_E\_IO\]

  The file descriptor in argument *fd* was invalid.

- \[ELF\_E\_IO\]

  The file descriptor in argument *fd* could not be read.

- \[ELF\_E\_RESOURCE\]

  An out of memory condition was encountered.

- \[ELF\_E\_SEQUENCE\]

  Functions **elf\_open**() or **elf\_openmemory**() was called before a working version was established with [elf\_version(3)](/elf_version/3.md).

## SEE ALSO

[elf(3)](/elf/3.md), [elf\_begin(3)](/elf_begin/3.md), [elf\_errno(3)](/elf_errno/3.md), [elf\_memory(3)](/elf_memory/3.md), [gelf(3)](/gelf/3.md)
