|
| 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 | +#include <elfio/elfio.hpp> |
| 24 | + |
| 25 | +using namespace ELFIO; |
| 26 | + |
| 27 | +int main( void ) |
| 28 | +{ |
| 29 | + elfio writer; |
| 30 | + |
| 31 | + // You can't proceed without this function call! |
| 32 | + writer.create( ELFCLASS64, ELFDATA2LSB ); |
| 33 | + |
| 34 | + writer.set_os_abi( ELFOSABI_LINUX ); |
| 35 | + writer.set_type( ET_EXEC ); |
| 36 | + writer.set_machine( EM_X86_64 ); |
| 37 | + |
| 38 | + // Create code section |
| 39 | + section* text_sec = writer.sections.add( ".text" ); |
| 40 | + text_sec->set_type( SHT_PROGBITS ); |
| 41 | + text_sec->set_flags( SHF_ALLOC | SHF_EXECINSTR ); |
| 42 | + text_sec->set_addr_align( 0x10 ); |
| 43 | + |
| 44 | + // Add data into it |
| 45 | + char text[] = { |
| 46 | + '\xB8', '\x04', '\x00', '\x00', '\x00', // mov eax, 4 |
| 47 | + '\xBB', '\x01', '\x00', '\x00', '\x00', // mov ebx, 1 |
| 48 | + '\xB9', '\x20', '\x80', '\x04', '\x08', // mov ecx, msg |
| 49 | + '\xBA', '\x0E', '\x00', '\x00', '\x00', // mov edx, 14 |
| 50 | + '\xCD', '\x80', // int 0x80 |
| 51 | + '\xB8', '\x01', '\x00', '\x00', '\x00', // mov eax, 1 |
| 52 | + '\xCD', '\x80' // int 0x80 |
| 53 | + }; |
| 54 | + text_sec->set_data( text, sizeof( text ) ); |
| 55 | + |
| 56 | + // Create a loadable segment |
| 57 | + segment* text_seg = writer.segments.add(); |
| 58 | + text_seg->set_type( PT_LOAD ); |
| 59 | + text_seg->set_virtual_address( 0x400 ); |
| 60 | + text_seg->set_physical_address( 0x0 ); |
| 61 | + text_seg->set_flags( PF_X | PF_R ); |
| 62 | + text_seg->set_align( 0x1 ); |
| 63 | + |
| 64 | + // Add code section into program segment |
| 65 | + text_seg->add_section_index( text_sec->get_index(), |
| 66 | + text_sec->get_addr_align() ); |
| 67 | + |
| 68 | + // Create data section |
| 69 | + section* data_sec = writer.sections.add( ".data" ); |
| 70 | + data_sec->set_type( SHT_PROGBITS ); |
| 71 | + data_sec->set_flags( SHF_ALLOC | SHF_WRITE ); |
| 72 | + data_sec->set_addr_align( 0x4 ); |
| 73 | + |
| 74 | + char data[] = { |
| 75 | + '\x48', '\x65', '\x6C', '\x6C', '\x6F', // msg: db 'Hello, World!', 10 |
| 76 | + '\x2C', '\x20', '\x57', '\x6F', '\x72', |
| 77 | + '\x6C', '\x64', '\x21', '\x0A' }; |
| 78 | + data_sec->set_data( data, sizeof( data ) ); |
| 79 | + |
| 80 | + // Create a read/write segment |
| 81 | + segment* data_seg = writer.segments.add(); |
| 82 | + data_seg->set_type( PT_LOAD ); |
| 83 | + data_seg->set_virtual_address( 0x0420 ); |
| 84 | + data_seg->set_physical_address( 0x0420 ); |
| 85 | + data_seg->set_flags( PF_W | PF_R ); |
| 86 | + data_seg->set_align( 0x10 ); |
| 87 | + |
| 88 | + // Add code section into program segment |
| 89 | + data_seg->add_section_index( data_sec->get_index(), |
| 90 | + data_sec->get_addr_align() ); |
| 91 | + |
| 92 | + // Add optional signature for the file producer |
| 93 | + section* note_sec = writer.sections.add( ".note" ); |
| 94 | + note_sec->set_type( SHT_NOTE ); |
| 95 | + note_sec->set_addr_align( 1 ); |
| 96 | + note_section_accessor note_writer( writer, note_sec ); |
| 97 | + note_writer.add_note( 0x01, "Created by ELFIO", 0, 0 ); |
| 98 | + char descr[6] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36 }; |
| 99 | + note_writer.add_note( 0x01, "Never easier!", descr, sizeof( descr ) ); |
| 100 | + |
| 101 | + // Setup entry point. Usually, a linker sets this address on base of |
| 102 | + // ‘_start’ label. |
| 103 | + // In this example, the code starts at the first address of the |
| 104 | + // 'text_seg' segment. Therefore, the start address is set |
| 105 | + // to be equal to the segment location |
| 106 | + writer.set_entry( 0x400 ); |
| 107 | + |
| 108 | + // Create ELF file |
| 109 | + writer.save( "test_elf.bin" ); |
| 110 | + |
| 111 | + return 0; |
| 112 | +} |
0 commit comments