These examples run on a Windows system.
7 int main(
int argc,
char** argv)
11 std::cout <<
"Usage: tutorial <coff_file>" << std::endl;
20 if (!reader.
load(argv[1])) {
21 std::cout <<
"Can't find or process COFF file " << argv[1] << std::endl;
28 std::cout <<
"COFF file architecture: ";
34 std::cout <<
"Portable Executable PE32+" << std::endl;
37 std::cout <<
"Portable Executable PE32" << std::endl;
41 std::cout <<
"Portable Executable" << std::endl;
45 std::cout <<
"CEVA" << std::endl;
48 std::cout <<
"Texas Instruments" << std::endl;
56 std::cout <<
"Number of sections: " << sec_num << std::endl;
58 std::cout <<
" [" << sec->get_index() <<
"] " << sec->get_name()
59 <<
"\t" << sec->get_data_size() << std::endl;
65 for (
auto sym : *reader.get_symbols()) {
66 std::cout << sym.get_index() <<
" " << sym.get_name() <<
" "
67 << sym.get_value() << std::endl;
The COFFI library's main class.
bool load(const std::string &file_name)
Initializes the coffi object by loading data from COFF binary file.
optional_header * get_optional_header()
Returns the optional COFF header.
sections & get_sections()
Returns a list of the COFF sections.
coffi_architecture_t get_architecture() const
Returns the coffi object architecture.
The COFFI library include file.
#define OH_MAGIC_PE32PLUS
PE32+ format.
@ COFFI_ARCHITECTURE_PE
Windows portable executable (PE or PE+)
@ COFFI_ARCHITECTURE_CEVA
CEVA Inc.
@ COFFI_ARCHITECTURE_TI
Texas Instruments.
17 using namespace COFFI;
21 void write_the_file(
const std::string &filename )
39 '\x68',
'\x00',
'\x20',
'\x40',
'\x00',
40 '\x68',
'\x00',
'\x20',
'\x40',
'\x00',
42 '\xE8',
'\x0E',
'\x00',
'\x00',
'\x00',
44 '\xE8',
'\x01',
'\x00',
'\x00',
'\x00',
46 '\xFF',
'\x25',
'\x4C',
'\x30',
'\x40',
'\x00',
47 '\xFF',
'\x25',
'\x54',
'\x30',
'\x40',
'\x00'
49 text_sec->set_data( text,
sizeof( text ) );
50 text_sec->set_virtual_address(0x1000);
51 text_sec->set_virtual_size(
sizeof(text));
58 char rdata[] =
"Hello World!\0";
59 rdata_sec->set_data( rdata,
sizeof( rdata ) );
60 rdata_sec->set_virtual_address(0x2000);
61 rdata_sec->set_virtual_size(
sizeof(rdata));
72 0x3C, 0x30, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00,
75 0x5C, 0x30, 0x00, 0x00,
76 0x4C, 0x30, 0x00, 0x00,
79 0x44, 0x30, 0x00, 0x00,
80 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00,
82 0x6C, 0x30, 0x00, 0x00,
83 0x54, 0x30, 0x00, 0x00,
86 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00,
88 0x00, 0x00, 0x00, 0x00,
89 0x00, 0x00, 0x00, 0x00,
90 0x00, 0x00, 0x00, 0x00,
94 0x7C, 0x30, 0x00, 0x00,
95 0x00, 0x00, 0x00, 0x00,
97 0x8C, 0x30, 0x00, 0x00,
98 0x00, 0x00, 0x00, 0x00,
102 0x7C, 0x30, 0x00, 0x00,
103 0x00, 0x00, 0x00, 0x00,
105 0x8C, 0x30, 0x00, 0x00,
106 0x00, 0x00, 0x00, 0x00,
110 'K',
'E',
'R',
'N',
'E',
'L',
'3',
'2',
'.',
'd',
'l',
'l', 0, 0, 0, 0,
112 'U',
'S',
'E',
'R',
'3',
'2',
'.',
'd',
'l',
'l', 0, 0, 0, 0, 0, 0,
116 0x5E, 0x01,
'E',
'x',
'i',
't',
'P',
'r',
'o',
'c',
'e',
's',
's', 0, 0, 0,
118 0x7F, 0x02,
'M',
'e',
's',
's',
'a',
'g',
'e',
'B',
'o',
'x',
'A', 0, 0, 0,
121 idata_sec->set_data( (
char*)idata,
sizeof( idata ) );
122 idata_sec->set_virtual_address(0x3000);
123 idata_sec->set_virtual_size(
sizeof(idata));
138 reloc_sec->set_data( (
char*)reloc,
sizeof( reloc ) );
139 reloc_sec->set_virtual_address(0x4000);
140 reloc_sec->set_virtual_size(
sizeof(reloc));
191 writer.
save( filename );
198 write_the_file(
"hello.exe");
section * add_section(const std::string &name)
Add a COFF section.
win_header * get_win_header()
Returns the Windows NT header.
coff_header * get_header()
Returns the COFF header.
bool save(const std::string &file_name)
Creates a file in COFF binary format.
directory * add_directory(const image_data_directory &rva_and_size)
Add a PE data directory.
void create(coffi_architecture_t architecture)
Cleans and/or initializes the coffi object.
void create_optional_header(uint16_t magic=OH_MAGIC_PE32)
Initializes an optional header for the coffi object.
void layout()
Performs the layout of the file.
Interface class for accessing a COFF section, for all the COFF architectures.
#define IMAGE_FILE_EXECUTABLE_IMAGE
Image only. Indicates that the image file is valid and can be run. If this flag is not set,...
#define IMAGE_SCN_CNT_INITIALIZED_DATA
The section contains initialized data.
#define IMAGE_SCN_CNT_CODE
The section contains executable code.
#define IMAGE_SCN_MEM_EXECUTE
The section can be executed as code.
#define IMAGE_SCN_ALIGN_4BYTES
Align data on a 4 - byte boundary.Valid only for object files.
#define IMAGE_SCN_MEM_READ
The section can be read.
#define IMAGE_FILE_32BIT_MACHINE
Machine based on 32-bit-word architecture.
20 using namespace COFFI;
22 void write_the_file(
const std::string& filename)
38 '\x83',
'\xE4',
'\xF0',
39 '\xE8',
'\x00',
'\x00',
'\x00',
'\x00',
40 '\xB8',
'\x2A',
'\x00',
'\x00',
'\x00',
46 text_sec->set_data(text,
sizeof(text));
66 char v[] =
"This is an arbitrary read-only string data\0";
67 v_sec->set_data(v,
sizeof(v));
74 sym1->set_aux_symbols_number(1);
79 sym2->set_type(IMAGE_SYM_TYPE_FUNCTION);
81 sym2->set_section_number(text_sec->get_index() + 1);
82 sym2->set_aux_symbols_number(1);
84 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
85 sym2->get_auxiliary_symbols().push_back(a2);
88 sym3->set_type(IMAGE_SYM_TYPE_NOT_FUNCTION);
90 sym3->set_section_number(text_sec->get_index() + 1);
91 sym3->set_aux_symbols_number(1);
93 text_sec->get_data_size(), 1, 0, 0, 0, 0, {0, 0, 0}};
94 sym3->get_auxiliary_symbols().push_back(
98 sym4->set_type(IMAGE_SYM_TYPE_NOT_FUNCTION);
100 sym4->set_section_number(data_sec->get_index() + 1);
101 sym4->set_aux_symbols_number(1);
103 data_sec->get_data_size(), 0, 0, 0, 0, 0, {0, 0, 0}};
104 sym4->get_auxiliary_symbols().push_back(
108 sym5->set_type(IMAGE_SYM_TYPE_NOT_FUNCTION);
110 sym5->set_section_number(bss_sec->get_index() + 1);
111 sym5->set_aux_symbols_number(1);
113 bss_sec->get_data_size(), 0, 0, 0, 0, 0, {0, 0, 0}};
114 sym5->get_auxiliary_symbols().push_back(
118 sym6->set_type(IMAGE_SYM_TYPE_NOT_FUNCTION);
120 sym6->set_section_number(v_sec->get_index() + 1);
121 sym6->set_aux_symbols_number(1);
123 v_sec->get_data_size(), 0, 0, 0, 0, 0, {0, 0, 0}};
124 sym6->get_auxiliary_symbols().push_back(
128 writer.
save(filename);
133 write_the_file(
"answer.o");
symbol * add_symbol(const std::string &name)
Adds a symbol in the table.
Class for accessing a COFF symbol.
#define IMAGE_SCN_MEM_WRITE
The section can be written to.
#define IMAGE_SYM_DEBUG
The symbol provides general type or debugging information but does not correspond to a section....
#define IMAGE_SYM_CLASS_EXTERNAL
Used by Microsoft tools for external symbols. The Value field indicates the size if the section numbe...
#define IMAGE_FILE_LINE_NUMS_STRIPPED
COFF line numbers have been removed.
#define IMAGE_SYM_CLASS_STATIC
The Value field specifies the offset of the symbol within the section. If the Value is 0,...
#define IMAGE_SCN_CNT_UNINITIALIZED_DATA
The section contains uninitialized data.
#define IMAGE_SYM_CLASS_FILE
Used by Microsoft tools, as well as traditional COFF format, for the source-file symbol record....
PE auxiliary format 4: Files.
PE auxiliary format 5: Section definitions.
Generic auxiliary symbol record, covers any type of auxiliary symbol.