• 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 executable file for Linux on x86-64
25  *
26  * Instructions:
27  * 1. Compile and link this file with ELFIO library
28  *    g++ writer.cpp -o writer
29  * 2. Execute result file writer
30  *    ./writer
31  * 3. Add executable flag for 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( text_sec, text_sec->get_addr_align() );
87 
88     // Create data section
89     section* data_sec = writer.sections.add( ".data" );
90     data_sec->set_type( SHT_PROGBITS );
91     data_sec->set_flags( SHF_ALLOC | SHF_WRITE );
92     data_sec->set_addr_align( 0x4 );
93 
94     char data[] = {
95         '\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db   'Hello, World!', 10
96         '\x2C', '\x20', '\x57', '\x6F', '\x72',
97         '\x6C', '\x64', '\x21', '\x0A' };
98     data_sec->set_data( data, sizeof( data ) );
99 
100     // Create a read/write segment
101     segment* data_seg = writer.segments.add();
102     data_seg->set_type( PT_LOAD );
103     data_seg->set_virtual_address( DATA_ADDR );
104     data_seg->set_physical_address( DATA_ADDR );
105     data_seg->set_flags( PF_W | PF_R );
106     data_seg->set_align( PAGE_SIZE );
107 
108     // Add code section into program segment
109     data_seg->add_section( data_sec, data_sec->get_addr_align() );
110 
111     // Add optional signature for the file producer
112     section* note_sec = writer.sections.add( ".note" );
113     note_sec->set_type( SHT_NOTE );
114     note_sec->set_addr_align( 1 );
115 
116     note_section_accessor note_writer( writer, note_sec );
117     note_writer.add_note( 0x01, "Created by ELFIO", 0, 0 );
118     char descr[6] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36 };
119     note_writer.add_note( 0x01, "Never easier!", descr, sizeof( descr ) );
120 
121     // Setup entry point. Usually, a linker sets this address on base of
122     // ‘_start’ label.
123     // In this example, the code starts at the first address of the
124     // 'text_seg' segment. Therefore, the start address is set
125     // to be equal to the segment location
126     writer.set_entry( text_seg->get_virtual_address() );
127 
128     // Create ELF file
129     writer.save( "hello_x86_64" );
130 
131     return 0;
132 }
133