• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 Copyright (C) 2001-present by Serge Lamikhov-Center
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 
23 /*
24  * This example shows how to create ELF object file for Linux on x86
25  *
26  * Instructions:
27  * 1. Compile and link this file with ELFIO library
28  *    g++ writer.cpp -o writer
29  * 2. Execute result file write_obj
30  *    ./writer
31  * 3. Add executable flag to the output file
32  *    chmod +x hello_x86_64
33  * 4. Run the result file:
34  *    ./hello_x86_64
35  */
36 
37 #include <elfio/elfio.hpp>
38 
39 using namespace ELFIO;
40 
41 const Elf64_Addr CODE_ADDR = 0x00401000;
42 const Elf_Xword  PAGE_SIZE = 0x1000;
43 const Elf64_Addr DATA_ADDR = CODE_ADDR + PAGE_SIZE;
44 
main(void)45 int main( void )
46 {
47     elfio writer;
48 
49     // You can't proceed without this function call!
50     writer.create( ELFCLASS64, ELFDATA2LSB );
51 
52     writer.set_os_abi( ELFOSABI_LINUX );
53     writer.set_type( ET_EXEC );
54     writer.set_machine( EM_X86_64 );
55 
56     // Create code section
57     section* text_sec = writer.sections.add( ".text" );
58     text_sec->set_type( SHT_PROGBITS );
59     text_sec->set_flags( SHF_ALLOC | SHF_EXECINSTR );
60     text_sec->set_addr_align( 0x10 );
61 
62     // Add data into it
63     char text[] = {
64         '\xB8', '\x04', '\x00', '\x00', '\x00', // mov eax, 4
65         '\xBB', '\x01', '\x00', '\x00', '\x00', // mov ebx, 1
66         '\xB9', '\x00', '\x00', '\x00', '\x00', // mov ecx, msg
67         '\xBA', '\x0E', '\x00', '\x00', '\x00', // mov edx, 14
68         '\xCD', '\x80',                         // int 0x80
69         '\xB8', '\x01', '\x00', '\x00', '\x00', // mov eax, 1
70         '\xCD', '\x80'                          // int 0x80
71     };
72     // Adjust data address for 'msg'
73     *(uint32_t*)( text + 11 ) = DATA_ADDR;
74 
75     text_sec->set_data( text, sizeof( text ) );
76 
77     // Create a loadable segment
78     segment* text_seg = writer.segments.add();
79     text_seg->set_type( PT_LOAD );
80     text_seg->set_virtual_address( CODE_ADDR );
81     text_seg->set_physical_address( CODE_ADDR );
82     text_seg->set_flags( PF_X | PF_R );
83     text_seg->set_align( PAGE_SIZE );
84 
85     // Add code section into program segment
86     text_seg->add_section_index( text_sec->get_index(),
87                                  text_sec->get_addr_align() );
88 
89     // Create data section
90     section* data_sec = writer.sections.add( ".data" );
91     data_sec->set_type( SHT_PROGBITS );
92     data_sec->set_flags( SHF_ALLOC | SHF_WRITE );
93     data_sec->set_addr_align( 0x4 );
94 
95     char data[] = {
96         '\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db   'Hello, World!', 10
97         '\x2C', '\x20', '\x57', '\x6F', '\x72',
98         '\x6C', '\x64', '\x21', '\x0A' };
99     data_sec->set_data( data, sizeof( data ) );
100 
101     // Create a read/write segment
102     segment* data_seg = writer.segments.add();
103     data_seg->set_type( PT_LOAD );
104     data_seg->set_virtual_address( DATA_ADDR );
105     data_seg->set_physical_address( DATA_ADDR );
106     data_seg->set_flags( PF_W | PF_R );
107     data_seg->set_align( PAGE_SIZE );
108 
109     // Add code section into program segment
110     data_seg->add_section_index( data_sec->get_index(),
111                                  data_sec->get_addr_align() );
112 
113     // Add optional signature for the file producer
114     section* note_sec = writer.sections.add( ".note" );
115     note_sec->set_type( SHT_NOTE );
116     note_sec->set_addr_align( 1 );
117 
118     note_section_accessor note_writer( writer, note_sec );
119     note_writer.add_note( 0x01, "Created by ELFIO", 0, 0 );
120     char descr[6] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36 };
121     note_writer.add_note( 0x01, "Never easier!", descr, sizeof( descr ) );
122 
123     // Setup entry point. Usually, a linker sets this address on base of
124     // ‘_start’ label.
125     // In this example, the code starts at the first address of the
126     // 'text_seg' segment. Therefore, the start address is set
127     // to be equal to the segment location
128     writer.set_entry( CODE_ADDR );
129 
130     // Create ELF file
131     writer.save( "hello_x86_64" );
132 
133     return 0;
134 }
135