COFFI
1.2
               
coffi Class Reference

The COFFI library's main class. More...

+ Inheritance diagram for coffi:

Public Member Functions

 coffi (const coffi &)=delete
 Discards the copy constructor.
 
bool load (const std::string &file_name)
 Initializes the coffi object by loading data from COFF binary file. More...
 
bool load (std::istream &stream)
 Initializes the coffi object by loading data from COFF binary file. More...
 
bool save (const std::string &file_name)
 Creates a file in COFF binary format. More...
 
bool save (std::ostream &stream)
 Creates a file in COFF binary format. More...
 
void create (coffi_architecture_t architecture)
 Cleans and/or initializes the coffi object. More...
 
void create_optional_header (uint16_t magic=OH_MAGIC_PE32)
 Initializes an optional header for the coffi object. More...
 
dos_headerget_msdos_header ()
 Returns the MS-DOS header. More...
 
const dos_headerget_msdos_header () const
 Returns the MS-DOS header. More...
 
coff_headerget_header ()
 Returns the COFF header. More...
 
const coff_headerget_header () const
 Returns the COFF header. More...
 
optional_headerget_optional_header ()
 Returns the optional COFF header. More...
 
const optional_headerget_optional_header () const
 Returns the optional COFF header. More...
 
win_headerget_win_header ()
 Returns the Windows NT header. More...
 
const win_headerget_win_header () const
 Returns the Windows NT header. More...
 
sectionsget_sections ()
 Returns a list of the COFF sections. More...
 
const sectionsget_sections () const
 Returns a list of the COFF sections. More...
 
sectionadd_section (const std::string &name)
 Add a COFF section. More...
 
directoriesget_directories ()
 Returns a list of the PE data directories. More...
 
const directoriesget_directories () const
 Returns a list of the PE data directories. More...
 
directoryadd_directory (const image_data_directory &rva_and_size)
 Add a PE data directory. More...
 
bool is_PE32_plus ()
 PE32+ format check. More...
 
int get_addressable_unit () const
 Returns the character type size in bytes. More...
 
coffi_architecture_t get_architecture () const
 Returns the coffi object architecture. More...
 
void layout ()
 Performs the layout of the file. More...
 
- Public Member Functions inherited from coffi_strings
 coffi_strings (const coffi_strings &)=delete
 Discards the copy constructor.
 
uint32_t get_strings_size () const
 
void set_strings_size (uint32_t value)
 
virtual std::string string_to_name (const char *str) const
 Converts an 8-bytes symbol short name into a full name, eventually by looking into the strings table. More...
 
virtual std::string section_string_to_name (const char *str) const
 Converts an 8-bytes section short name into a full name, eventually by looking into the strings table. More...
 
virtual void name_to_string (const std::string &name, char *str)
 Converts symbol full name into an 8-bytes short name, eventually creating an entry in the strings table. More...
 
virtual void name_to_section_string (const std::string &name, char *str)
 Converts section full name into an 8-bytes short name, eventually creating an entry in the strings table. More...
 
virtual const char * get_strings () const
 
virtual void set_strings (const char *str, uint32_t size)
 
- Public Member Functions inherited from coffi_symbols
virtual symbolget_symbol (uint32_t index)
 Gets a symbol from its index. More...
 
virtual const symbolget_symbol (uint32_t index) const
 Gets a symbol from its index. More...
 
virtual symbolget_symbol (const std::string &name)
 Gets a symbol from its name. More...
 
virtual const symbolget_symbol (const std::string &name) const
 Gets a symbol from its name. More...
 
std::vector< symbol > * get_symbols ()
 
const std::vector< symbol > * get_symbols () const
 
symboladd_symbol (const std::string &name)
 Adds a symbol in the table. More...
 

Detailed Description

The COFFI library's main class.

Definition at line 70 of file coffi.hpp.

Member Function Documentation

◆ add_directory()

directory* add_directory ( const image_data_directory rva_and_size)
inline

Add a PE data directory.

This function is relevant only for the PE architecture (see COFFI_ARCHITECTURE_PE).

Parameters
[in]rva_and_sizeRelative virtual address (RVA) and size
Returns
A pointer to the newly created directory.

Definition at line 525 of file coffi.hpp.

◆ add_section()

section* add_section ( const std::string &  name)
inline

Add a COFF section.

Parameters
[in]nameThe section name
Returns
A pointer to the newly created section.

Definition at line 488 of file coffi.hpp.

◆ create()

void create ( coffi_architecture_t  architecture)
inline

Cleans and/or initializes the coffi object.

Parameters
[in]architectureCOFF architecture, see coffi_architecture_t for the list of supported architectures

Definition at line 353 of file coffi.hpp.

◆ create_optional_header()

void create_optional_header ( uint16_t  magic = OH_MAGIC_PE32)
inline

Initializes an optional header for the coffi object.

The optional header format depends on the architecture:

Parameters
[in]magicUsed only for the PE files (COFFI_ARCHITECTURE_PE): OH_MAGIC_PE32 for PE32 format, OH_MAGIC_PE32PLUS for PE32+ format.

Definition at line 389 of file coffi.hpp.

◆ get_addressable_unit()

int get_addressable_unit ( ) const
inlinevirtual

Returns the character type size in bytes.

Auto-detects the addressable unit: are the addresses in bytes or 2-bytes words?
Some targets have 2-bytes characters, this changes how offsets are computed in the file.

Returns
1 in most cases (characters are 1 byte), 2 for some specific targets (TMS320 C2000 for example).

Implements architecture_provider.

Definition at line 552 of file coffi.hpp.

◆ get_architecture()

coffi_architecture_t get_architecture ( ) const
inlinevirtual

Returns the coffi object architecture.

Returns
COFFI_ARCHITECTURE_NONE if the coffi object is not initialized, or in case of unrecognized architecture when loading a file.

Implements architecture_provider.

Definition at line 572 of file coffi.hpp.

◆ get_directories() [1/2]

directories& get_directories ( )
inline

Returns a list of the PE data directories.

This function is relevant only for the PE architecture (see COFFI_ARCHITECTURE_PE).

Returns
Empty list if the PE data directories are not initialized, or not relevant for the architecture.

Definition at line 510 of file coffi.hpp.

◆ get_directories() [2/2]

const directories& get_directories ( ) const
inline

Returns a list of the PE data directories.

This function is relevant only for the PE architecture (see COFFI_ARCHITECTURE_PE).

Returns
Empty list if the PE data directories are not initialized, or not relevant for the architecture.

Definition at line 515 of file coffi.hpp.

◆ get_header() [1/2]

coff_header* get_header ( )
inline

Returns the COFF header.

Returns
nullptr if the coffi object is not initialized (see create()), or not loaded (see load()).

Definition at line 436 of file coffi.hpp.

◆ get_header() [2/2]

const coff_header* get_header ( ) const
inlinevirtual

Returns the COFF header.

Returns
nullptr if the coffi object is not initialized (see create()), or not loaded (see load()).

Implements sections_provider.

Definition at line 441 of file coffi.hpp.

◆ get_msdos_header() [1/2]

dos_header* get_msdos_header ( )
inline

Returns the MS-DOS header.

Returns
nullptr if the MS-DOS header is not initialized (see create_optional_header()), or not loaded (see load()), or not relevant for the architecture.

Definition at line 424 of file coffi.hpp.

◆ get_msdos_header() [2/2]

const dos_header* get_msdos_header ( ) const
inlinevirtual

Returns the MS-DOS header.

Returns
nullptr if the MS-DOS header is not initialized (see create_optional_header()), or not loaded (see load()), or not relevant for the architecture.

Implements sections_provider.

Definition at line 429 of file coffi.hpp.

◆ get_optional_header() [1/2]

optional_header* get_optional_header ( )
inline

Returns the optional COFF header.

Returns
nullptr if the optional COFF header is not initialized (see create_optional_header()), or not loaded (see load()).

Definition at line 448 of file coffi.hpp.

◆ get_optional_header() [2/2]

const optional_header* get_optional_header ( ) const
inlinevirtual

Returns the optional COFF header.

Returns
nullptr if the optional COFF header is not initialized (see create_optional_header()), or not loaded (see load()).

Implements sections_provider.

Definition at line 453 of file coffi.hpp.

◆ get_sections() [1/2]

sections& get_sections ( )
inline

Returns a list of the COFF sections.

Returns
Empty list if the coffi object is not initialized.

Definition at line 475 of file coffi.hpp.

◆ get_sections() [2/2]

const sections& get_sections ( ) const
inlinevirtual

Returns a list of the COFF sections.

Returns
Empty list if the coffi object is not initialized.

Implements sections_provider.

Definition at line 480 of file coffi.hpp.

◆ get_win_header() [1/2]

win_header* get_win_header ( )
inline

Returns the Windows NT header.

Returns
nullptr if the Windows NT header is not initialized (see create_optional_header()), or not loaded (see load()), or not relevant for the architecture.

Definition at line 463 of file coffi.hpp.

◆ get_win_header() [2/2]

const win_header* get_win_header ( ) const
inlinevirtual

Returns the Windows NT header.

Returns
nullptr if the Windows NT header is not initialized (see create_optional_header()), or not loaded (see load()), or not relevant for the architecture.

Implements sections_provider.

Definition at line 468 of file coffi.hpp.

◆ is_PE32_plus()

bool is_PE32_plus ( )
inline

PE32+ format check.

Returns
true if the file is initialized and is a PE file (see COFFI_ARCHITECTURE_PE), with a magic number indicating a PE32+ file (see OH_MAGIC_PE32PLUS).

Definition at line 540 of file coffi.hpp.

◆ layout()

void layout ( )
inline

Performs the layout of the file.

The layout consists in:

  • Compute the sections alignment,
  • Compute the offsets for: sections, directories, relocations, line numbers, string table offset, etc.

Definition at line 581 of file coffi.hpp.

◆ load() [1/2]

bool load ( const std::string &  file_name)
inline

Initializes the coffi object by loading data from COFF binary file.

Parameters
[in]file_nameFile path of the file to load.
Returns
true if the file was processed successfully, false otherwise.

Definition at line 97 of file coffi.hpp.

◆ load() [2/2]

bool load ( std::istream &  stream)
inline

Initializes the coffi object by loading data from COFF binary file.

See load(const std::string&).

Parameters
[in]streamFile to load, as an opened stream.

Definition at line 115 of file coffi.hpp.

◆ save() [1/2]

bool save ( const std::string &  file_name)
inline

Creates a file in COFF binary format.

Before saving, performs the following modififications:

  • Layout (see layout()): Compute the alignment, offsets, etc.
  • Compute the headers fields than can be guessed from the data, like:
    • Number of sections, directories, etc.
    • Sizes of headers
Parameters
[in]file_nameFile path of the file to create.
Returns
true if the file has been created successfully, false otherwise.

Definition at line 310 of file coffi.hpp.

◆ save() [2/2]

bool save ( std::ostream &  stream)
inline

Creates a file in COFF binary format.

See save(const std::string&).

Parameters
[in]streamFile to create, as an opened stream.

Definition at line 328 of file coffi.hpp.


The documentation for this class was generated from the following file: