| ELF_BEGIN(3) | Library Functions Manual | ELF_BEGIN(3) | 
elf_begin —
#include <libelf.h>
Elf *
  
  elf_begin(int
    fd, Elf_Cmd cmd,
    Elf *elf);
elf_begin() is used to open ELF files and
  ar(1) archives for further
  processing by other APIs in the
  elf(3) library. It is also used to
  access individual ELF members of an
  ar(1) archive in combination with
  the elf_next(3) and
  elf_rand(3) APIs.
Argument fd is an open file descriptor
    returned from an open(2) system
    call. Function elf_begin() uses argument
    fd for reading or writing depending on the value of
    argument cmd. Argument elf is
    primarily used for iterating through archives.
The argument cmd can have the following values:
elf_begin() to return NULL. Arguments
      fd and elf are ignored, and no
      additional error is signalled.If argument elf is NULL, the library will allocate a new ELF descriptor for the file being processed. The argument fd should have been opened for reading.
If argument elf is not NULL, and
        references a regular ELF file previously opened with
        elf_begin(), then the activation count for the
        descriptor referenced by argument elf is
        incremented. The value in argument fd should match
        that used to open the descriptor argument elf.
If argument elf is not NULL, and
        references a descriptor for an
        ar(1) archive opened earlier
        with elf_begin(), a descriptor for an element in
        the archive is returned as described in the section
        Processing ar(1)
        archives below. The value for argument fd
        should match that used to open the archive earlier.
If argument elf is not NULL, and
        references an ar(1) archive
        opened earlier with elf_memory(), then the value
        of the argument fd is ignored.
ELF_C_RDWRArgument fd should have been opened for
        reading and writing. If argument elf is NULL, the
        library will allocate a new ELF descriptor for the file being processed.
        If the argument elf is non-null, it should point
        to a descriptor previously allocated with
        elf_begin() with the same values for arguments
        fd and cmd; in this case the
        library will increment the activation count for descriptor
        elf and return the same descriptor.
Changes to the in-memory image of the ELF file may be written back to disk using the elf_update(3) function.
ELF_C_WRITEELF_C_READ) using elf_begin()
  or elf_memory(). The returned ELF descriptor can be
  passed into to subsequent calls to elf_begin() to
  access individual members of the archive.
Random access within an opened archive is possible using the elf_next(3) and elf_rand(3) functions.
The symbol table of the archive may be retrieved using elf_getarsym(3).
Elf_Cmd c;
Elf *ar_e, *elf_e;
...
c = ELF_C_READ;
if ((ar_e = elf_begin(fd, c, (Elf *) 0)) == 0) {
	... handle error in opening the archive ...
}
while ((elf_e = elf_begin(fd, c, ar_e)) != 0) {
	... process member referenced by elf_e here ...
	c = elf_next(elf_e);
	elf_end(elf_e);
}
To create a new ELF file, use:
int fd;
Elf *e;
...
if ((fd = open("filename", O_RDWR|O_TRUNC|O_CREAT, 0666)) < 0) {
	... handle the error from open(2) ...
}
if ((e = elf_begin(fd, ELF_C_WRITE, (Elf *) 0)) == 0) {
	... handle the error from elf_begin() ...
}
... create the ELF image using other elf(3) APIs ...
elf_update(e, ELF_C_WRITE);
elf_end(e);
elf_begin() can fail with the following errors:
ELF_E_ARCHIVE]ELF_E_ARGUMENT]ELF_E_ARGUMENT]ELF_C_RDWR.ELF_E_ARGUMENT]ELF_E_ARGUMENT]ELF_E_ARGUMENT]ELF_C_RDWR.ELF_E_ARGUMENT]ELF_E_ARGUMENT]ELF_E_IO]ELF_E_IO]ELF_E_RESOURCE]ELF_E_SEQUENCE]elf_begin() was called before a working
      version was established with
      elf_version(3).ELF_E_VERSION]| December 11, 2011 | NetBSD 9.4 |