pw_elf#

ELF file support

Experimental C++17

pw_elf provides support for interact with Executable and Linkable Format (ELF) files.

Note

This module is currently very limited, primarily supporting other Pigweed modules. Additional functionality (e.g. iterating sections, segments) may be added in the future.

Guides#

Read an ELF section into a buffer#

 1#include "pw_elf/reader.h"
 2
 3#include <vector>
 4
 5#include "pw_status/try.h"
 6#include "pw_stream/std_file_stream.h"
 7
 8pw::Status ReaderExample() {
 9  // Open a file stream for the ELF image.
10  pw::stream::StdFileReader stream("/tmp/example.elf");
11
12  // Create an ElfReader from the file stream.
13  PW_TRY_ASSIGN(auto reader, pw::elf::ElfReader::FromStream(stream));
14
15  // Read the .example section into a vector.
16  PW_TRY_ASSIGN(std::vector<std::byte> section_data,
17                reader.ReadSection(".example"));
18
19  return pw::OkStatus();
20}

API reference#

class ElfReader#

A basic reader for ELF files.

Public Functions

inline stream::SeekableReader &stream() const#

Gets the associated stream.

inline StatusWithSize SeekToSection(std::string_view name)#

Seeks the associated stream to the beginning of the data of the section with the given name.

Parameters:

name[in] The name of the desired section.

Returns:

Code

Description

OK

Successfully found the desired section and seeked the stream to it. The associated size is the size of the associated section.

NOT_FOUND

No section was found with the desired name.

May return other error codes from the underlying stream.

Result<std::vector<std::byte>> ReadSection(std::string_view name)#

Reads a section with the given name.

Parameters:

name[in] The name of the desired section.

Returns:

Code

Description

OK

Successfully read the desired section. The result value is a vector of the section data.

NOT_FOUND

No section was found with the desired name.

May return other error codes from the underlying stream.

Public Static Functions

static Result<ElfReader> FromStream(stream::SeekableReader &stream)#

Creates an ElfReader from a stream.

Returns:

Code

Description

OK

The reader was initialized successfully.

DATA_LOSS

The input file was invalid.

OUT_OF_RANGE

Input stream exhausted (EOF).

UNIMPLEMENTED

Some aspect of the ELF file is not (yet) supported by this class, e.g., non-native endianness, or 64-bit ELF on a 32-bit host.

May return other error codes from the underlying stream.