---
name: "elf_rand"
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/elf_rand"
---

# elf\_rand(3)

provide sequential access to the next archive member

## NAME

**elf\_rand** – provide sequential access to the next archive member

## LIBRARY

library “libelf”

## SYNOPSIS

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

*off\_t*\

**elf\_rand**(*Elf \*archive*, *off\_t offset*);

## DESCRIPTION

The **elf\_rand**() function causes the ELF descriptor *archive* to be adjusted so that the next call to [elf\_begin(3)](/elf_begin/3.md) will provide access to the archive member at byte offset *offset* in the archive. Argument *offset* is the byte offset from the start of the archive to the beginning of the archive header for the desired member.

Archive member offsets may be retrieved using the [elf\_getarsym(3)](/elf_getarsym/3.md) function.

## RETURN VALUES

Function **elf\_rand**() returns *offset* if successful or zero in case of an error.

## EXAMPLES

To process all the members of an archive use:

```
off_t off;
Elf *archive, *e;
...
cmd = ELF_C_READ;
archive = elf_begin(fd, cmd, NULL);
while ((e = elf_begin(fd, cmd, archive)) != (Elf *) 0)
{
	... process `e' here ...
	elf_end(e);
	off = ...new value...;
	if (elf_rand(archive, off) != off) {
		... process error ...
	}
}
elf_end(archive);
```

To rewind an archive, use:

```
Elf *archive;
...
if (elf_rand(archive, SARMAG) != SARMAG) {
	... error ...
}
```

## ERRORS

Function **elf\_rand**() may fail with the following errors:

- \[ELF\_E\_ARGUMENT\]

  Argument *archive* was null.

- \[ELF\_E\_ARGUMENT\]

  Argument *archive* was not a descriptor for an [ar(1)](/ar/1.md) archive.

- \[ELF\_E\_ARCHIVE\]

  Argument *offset* did not correspond to the start of an archive member header.

## SEE ALSO

[ar(1)](/ar/1.md), [elf(3)](/elf/3.md), [elf\_begin(3)](/elf_begin/3.md), [elf\_end(3)](/elf_end/3.md), [elf\_getarsym(3)](/elf_getarsym/3.md), [elf\_next(3)](/elf_next/3.md), [gelf(3)](/gelf/3.md)
