elf_memory(3)

elf_memory3

Process an elf or ar(1) archive mapped into memory

Usage

#include <libelf.h>

Elf *

elf_memory(char *image, size_t size);

Library

library “libelf”

Overview

Function elf_memory() is used to process an ELF file or ar(1) archive whose image is present in memory.

Argument image points to the start of the memory image of the file or archive. Argument size contains the size in bytes of the memory image.

The ELF descriptor is created for reading (i.e., analogous to the use of elf_begin(3) with a command argument value of ELF_C_READ).

Return values

Function elf_memory() returns a pointer to a new ELF descriptor if successful, or NULL if an error occurred.

The return value may be queried for the file type using elf_kind(3).

Examples

To read parse an elf file, use:

int fd;
void *p;
struct stat sb;
Elf *e;
...
if ((fd = open("./elf-file", O_RDONLY)) < 0 ||
    fstat(fd, &sb) < 0 ||
    (p = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, (off_t) 0)) ==
    MAP_FAILED) {
	... handle system error ...
}
if ((e = elf_memory(p, sb.st_size)) == NULL) {
	... handle elf(3) error ...
}
... use ELF descriptor "e" here ...

Errors

Function elf_memory() can fail with the following errors:

[ELF_E_ARGUMENT]

A NULL value was used for argument image or the value of argument sz was zero.

[ELF_E_HEADER]

The header of the ELF object contained an unsupported value in its e_ident[EI_CLASS] field.

[ELF_E_HEADER]

The header of the ELF object contained an unsupported value in its e_ident[EI_DATA] field.

[ELF_E_RESOURCE]

An out of memory condition was detected.

[ELF_E_SEQUENCE]

Function elf_memory() was called before a working version was set using elf_version(3).

[ELF_E_VERSION]

The ELF object referenced by argument image was of an unsupported ELF version.

Releases

44 releases since FreeBSD 7.0-RELEASE; the text changed 20 times.

  1. 7.0
  2. 7.1
  3. 7.2
  4. 7.3
  5. 7.4
  6. 8.0
  7. 8.1
  8. 8.2
  9. 8.3
  10. 8.4
  11. 9.0
  12. 9.1
  13. 9.2
  14. 9.3
  15. 10.0
  16. 10.1
  17. 10.2
  18. 10.3
  19. 10.4
  20. 11.0–12.0
  21. 12.1–16.0

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.