• 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++ write_obj.cpp -o writer_obj
29  * 2. Execute result file write_obj
30  *    ./write_obj
31  * 3. Link output file hello.o:
32  *    ld -o hello hello.o
33  * 4. Run the result file:
34  *    ./hello
35  */
36 
37 #include <elfio/elfio.hpp>
38 
39 using namespace ELFIO;
40 
main(void)41 int main( void )
42 {
43     elfio writer;
44 
45     // You can't proceed before this function call!
46     writer.create( ELFCLASS64, ELFDATA2LSB );
47 
48     writer.set_os_abi( ELFOSABI_LINUX );
49     writer.set_type( ET_REL );
50     writer.set_machine( EM_X86_64 );
51 
52     // This is our code
53     char text[] = {
54         '\xB8', '\x04', '\x00', '\x00', '\x00', // mov eax, 4
55         '\xBB', '\x01', '\x00', '\x00', '\x00', // mov ebx, 1
56         '\xB9', '\x00', '\x00', '\x00', '\x00', // mov ecx, msg
57         '\xBA', '\x0E', '\x00', '\x00', '\x00', // mov edx, 14
58         '\xCD', '\x80',                         // int 0x80
59         '\xB8', '\x01', '\x00', '\x00', '\x00', // mov eax, 1
60         '\xCD', '\x80',                         // int 0x80
61         '\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db   'Hello, World!', 10
62         '\x2C', '\x20', '\x57', '\x6F', '\x72',
63         '\x6C', '\x64', '\x21', '\x0A' };
64     Elf64_Addr place_to_adjust = 11;
65 
66     // Create code section
67     section* text_sec = writer.sections.add( ".text" );
68     text_sec->set_type( SHT_PROGBITS );
69     text_sec->set_flags( SHF_ALLOC | SHF_EXECINSTR );
70     text_sec->set_addr_align( 0x10 );
71     text_sec->set_data( text, sizeof( text ) );
72 
73     // Create string table section
74     section* str_sec = writer.sections.add( ".strtab" );
75     str_sec->set_type( SHT_STRTAB );
76 
77     // Create string table writer
78     string_section_accessor stra( str_sec );
79     // Add label name
80     Elf32_Word str_index = stra.add_string( "msg" );
81 
82     // Create symbol table section
83     section* sym_sec = writer.sections.add( ".symtab" );
84     sym_sec->set_type( SHT_SYMTAB );
85     sym_sec->set_info( 1 );
86     sym_sec->set_addr_align( 0x4 );
87     sym_sec->set_entry_size( writer.get_default_entry_size( SHT_SYMTAB ) );
88     sym_sec->set_link( str_sec->get_index() );
89 
90     // Create symbol table writer
91     symbol_section_accessor syma( writer, sym_sec );
92     // Add symbol entry (msg has offset == 29)
93     Elf_Word sym_to_adjust = syma.add_symbol(
94         str_index, 29, 0, STB_GLOBAL, STT_OBJECT, 0, text_sec->get_index() );
95     // Another way to add symbol
96     syma.add_symbol( stra, "_start", 0x00000000, 0, STB_WEAK, STT_FUNC, 0,
97                      text_sec->get_index() );
98 
99     // Create relocation table section
100     section* rel_sec = writer.sections.add( ".rel.text" );
101     rel_sec->set_type( SHT_REL );
102     rel_sec->set_info( text_sec->get_index() );
103     rel_sec->set_addr_align( 0x4 );
104     rel_sec->set_entry_size( writer.get_default_entry_size( SHT_REL ) );
105     rel_sec->set_link( sym_sec->get_index() );
106 
107     // Create relocation table writer
108     relocation_section_accessor rela( writer, rel_sec );
109     // Add relocation entry (adjust address at offset 11)
110     rela.add_entry( place_to_adjust, sym_to_adjust,
111                     (unsigned char)R_386_RELATIVE );
112 
113     // Another method to add the same relocation entry at one step is:
114     // rela.add_entry( stra, "msg",
115     //                 syma, 29, 0,
116     //                 ELF_ST_INFO( STB_GLOBAL, STT_OBJECT ), 0,
117     //                 text_sec->get_index(),
118     //                 place_to_adjust, (unsigned char)R_386_RELATIVE );
119 
120     // Create note section
121     section* note_sec = writer.sections.add( ".note" );
122     note_sec->set_type( SHT_NOTE );
123 
124     note_section_accessor note_writer( writer, note_sec );
125     note_writer.add_note( 0x01, "Created by ELFIO", 0, 0 );
126     char descr[6] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36 };
127     note_writer.add_note( 0x01, "Never easier!", descr, sizeof( descr ) );
128 
129     // We don't use local symbols here. There is no need to rearrange them.
130     // But, for the completeness, we do this just prior 'save'
131     syma.arrange_local_symbols( [&]( Elf_Xword first, Elf_Xword second ) {
132         rela.swap_symbols( first, second );
133     } );
134 
135     // Create ELF object file
136     writer.save( "hello.o" );
137 
138     return 0;
139 }
140