---
name: "elf_update"
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_update"
---

# elf\_update(3)

update an ELF descriptor

## NAME

**elf\_update** – update an ELF descriptor

## LIBRARY

library “libelf”

## SYNOPSIS

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

*off\_t*\

**elf\_update**(*Elf \*elf*, *Elf\_Cmd cmd*);

## DESCRIPTION

Function **elf\_update**() causes the library to recalculate the structure of an ELF object and optionally write out the image of the object to file.

Argument *elf* should reference a valid ELF descriptor.

Argument *cmd* can be one of the following values:

- ELF\_C\_NULL

  The library will recalculate structural information flagging modified structures with the ELF\_F\_DIRTY flag, but will not write data to the underlying file image.

- ELF\_C\_WRITE

  The library will recalculate structural information and will also write the new image to the underlying file. The ELF descriptor referenced by argument *elf* should permit the underlying ELF object to be written or updated (see [elf\_begin(3)](/elf_begin/3.md)).

All pointers to *Elf\_Scn* and *Elf\_Data* descriptors associated with descriptor *elf* should be considered invalid after a call to **elf\_update**().

### Specifying Object Layout

The library “libelf” supports two layout modes.

- Library Layout

  If the ELF\_F\_LAYOUT flag is not set on the ELF descriptor, the ELF library will lay out the ELF object according to the following scheme:

  - *EHDR*

    The ELF executable header will be placed at the start of the object.

  - *PHDR*

    If the ELF descriptor contains a program header table, it will be placed after the Executable Header.

  - *Section* *Data*

    ELF section data, if any, will be placed next, keeping each section's alignment requirements in mind.

  - *SHDR*

    The ELF section header table, if any, will be placed last.

- Application Controlled Layout

  The application can take full control of the layout of the ELF object by setting the ELF\_F\_LAYOUT flag on the ELF descriptor (see [elf\_flagelf(3)](/elf_flagelf/3.md)). In this case the library will lay out the ELF object using application-supplied information as below:

  - *EHDR*

    The ELF executable header will be placed at the start of the object.

  - *PHDR*

    The ELF program header table, if any, it will be placed at the offset specified in the *e\_phoff* field of the ELF executable header.

  - *Section* *Data*

    The data for each ELF section will be placed at the offset specified by the *sh\_offset* field of the section's header. The size of the section will be taken from the *sh\_size* field of the section header.

  - *SHDR*

    The ELF section header table, if any, will be placed at the offset specified by the *e\_shoff* field of the executable header.

Gaps in the coverage of the file's contents will be set to the fill value specified by [elf\_fill(3)](/elf_fill/3.md).

### Application Supplied Information

The application needs to set the following fields in the data structures associated with the ELF descriptor prior to calling **elf\_update**().

- Executable Header

  The fields of the ELF executable header that need to be set by the application are:

  - *e\_entry*

    To be set to the desired entry address for executables.

  - *e\_flags*

    To be set to the desired processor specific flags.

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

    Must be set to one of ELFDATA2LSB or ELFDATA2MSB.

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

    To be set to the OS ABI desired. For example, for FreeBSD executables, this field should be set to ELFOSABI\_FREEBSD.

  - *e\_machine*

    To be set to the desired machine architecture, one of the EM\_\* values in the header file ⟨elfdefinitions.h⟩.

  - *e\_phoff*

    If the application is managing the object's layout, it must set this field to the file offset of the ELF program header table.

  - *e\_shoff*

    If the application is managing the object's layout, it must set this field to the file offset of the ELF section header table.

  - *e\_shstrndx*

    To be set to the index of the string table containing section names.

  - *e\_type*

    To be set to the type of the ELF object, one of the ET\_\* values in the header file ⟨elfdefinitions.h⟩.

  - *e\_version*

    To be set to the desired version of the ELF object.

- Program Header

  All fields of the entries in the program header table need to be set by the application.

- Section Header

  The fields of ELF section headers that need to be set by the application are:

  - *sh\_addr*

    To be set to the memory address where the section should reside.

  - *sh\_addralign*

    If the application is managing the file layout, it must set this field to the desired alignment for the section's contents. This value must be a power of two and must be at least as large as the largest alignment needed by any *Elf\_Data* descriptor associated with the section.

  - *sh\_entsize*

    To be set to the size of each entry, for sections containing fixed size elements, or set to zero for sections without fixed size elements. If the application is not managing file layout, it may leave this field as zero for those sections whose types are known to the library.

  - *sh\_flags*

    To be set to the desired section flags.

  - *sh\_info*

    To be set as described in [elf(5)](/elf/5.md).

  - *sh\_link*

    To be set as described in [elf(5)](/elf/5.md).

  - *sh\_name*

    To be set to the index of the section's name in the string table containing section names.

  - *sh\_offset*

    If the application is managing the file layout, it must set this field to the file offset of the section's contents.

  - *sh\_size*

    If the application is managing the file layout, it must set this field to the file size of the section's contents.

  - *sh\_type*

    To be set to the type of the section.

- Section Data

  The *Elf\_Data* descriptors associated with each section specify its contents (see [elf\_getdata(3)](/elf_getdata/3.md)). While all the fields in these descriptors are under application control, the following fields influence object layout:

  - *d\_align*

    To be set to the desired alignment, within the containing section, of the descriptor's data.

  - *d\_off*

    If the application is managing object layout, it must set this field to the file offset, within the section, at which the descriptor's data should be placed.

  - *d\_size*

    To be set to the size in bytes of the memory representation of the descriptor's data.

## RETURN VALUES

Function **elf\_update**() returns the total size of the file image if successful, or -1 if an error occurred.

## ERRORS

This function may fail with the following errors:

- \[ELF\_E\_ARGUMENT\]

  Argument *elf* was null.

- \[ELF\_E\_ARGUMENT\]

  Argument *cmd* was not recognized.

- \[ELF\_E\_ARGUMENT\]

  The argument *elf* was not a descriptor for an ELF object.

- \[ELF\_E\_CLASS\]

  The *e\_ident\[EI\_CLASS\]* field of the executable header of argument *elf* did not match the class of the file.

- \[ELF\_E\_DATA\]

  An *Elf\_Data* descriptor contained in argument *elf* specified an unsupported type.

- \[ELF\_E\_DATA\]

  An *Elf\_Data* descriptor specified an alignment that was zero or was not a power of two.

- \[ELF\_E\_HEADER\]

  The ELF header in argument *elf* requested a different byte order from the byte order already associated with the file.

- \[ELF\_E\_IO\]

  An I/O error was encountered.

- \[ELF\_E\_LAYOUT\]

  An *Elf\_Data* descriptor contained in argument *elf* specified an alignment incompatible with its containing section.

- \[ELF\_E\_LAYOUT\]

  Argument *elf* contained section descriptors that overlapped in extent.

- \[ELF\_E\_LAYOUT\]

  Argument *elf* contained section descriptors that were incorrectly aligned or were too small for their data.

- \[ELF\_E\_LAYOUT\]

  The flag ELF\_F\_LAYOUT was set on the Elf descriptor and the executable header overlapped with the program header table.

- \[ELF\_E\_LAYOUT\]

  The flag ELF\_F\_LAYOUT was set on the Elf descriptor and the program header table was placed at a misaligned file offset.

- \[ELF\_E\_LAYOUT\]

  The flag ELF\_F\_LAYOUT was set on the Elf descriptor and the section header table overlapped an extent mapped by a section descriptor.

- \[ELF\_E\_LAYOUT\]

  The ELF\_F\_LAYOUT flag was set on the Elf descriptor, and the *d\_offset* field in an *Elf\_Data* descriptor contained a value that was not a multiple of the descriptor's specified alignment.

- \[ELF\_E\_MODE\]

  An ELF\_C\_WRITE operation was requested with an ELF descriptor that was not opened for writing or updating.

- \[ELF\_E\_SECTION\]

  Argument *elf* contained a section with an unrecognized type.

- \[ELF\_E\_SECTION\]

  The section header at index SHN\_UNDEF had an illegal section type.

- \[ELF\_E\_SEQUENCE\]

  An ELF\_C\_WRITE operation was requested after a prior call to **elf\_cntl**(*elf*, *ELF\_C\_FDDONE*) disassociated the ELF descriptor *elf* from its underlying file.

- \[ELF\_E\_UNIMPL\]

  Argument *elf* contained a section with an unsupported ELF type.

- \[ELF\_E\_VERSION\]

  Argument *elf* had an unsupported version or contained an *Elf\_Data* descriptor with an unsupported version.

## SEE ALSO

[elf(3)](/elf/3.md), [elf32\_getehdr(3)](/elf32_getehdr/3.md), [elf32\_getphdr(3)](/elf32_getphdr/3.md), [elf32\_newehdr(3)](/elf32_newehdr/3.md), [elf32\_newphdr(3)](/elf32_newphdr/3.md), [elf64\_getehdr(3)](/elf64_getehdr/3.md), [elf64\_getphdr(3)](/elf64_getphdr/3.md), [elf64\_newehdr(3)](/elf64_newehdr/3.md), [elf64\_newphdr(3)](/elf64_newphdr/3.md), [elf\_begin(3)](/elf_begin/3.md), [elf\_cntl(3)](/elf_cntl/3.md), [elf\_fill(3)](/elf_fill/3.md), [elf\_flagehdr(3)](/elf_flagehdr/3.md), [elf\_flagelf(3)](/elf_flagelf/3.md), [elf\_getdata(3)](/elf_getdata/3.md), [elf\_getscn(3)](/elf_getscn/3.md), [elf\_newdata(3)](/elf_newdata/3.md), [elf\_newscn(3)](/elf_newscn/3.md), [elf\_rawdata(3)](/elf_rawdata/3.md), [gelf(3)](/gelf/3.md), [gelf\_newehdr(3)](/gelf_newehdr/3.md), [gelf\_newphdr(3)](/gelf_newphdr/3.md), [elf(5)](/elf/5.md)
