write_obj example implemented

This commit is contained in:
Serge Lamikhov-Center 2012-11-26 19:40:03 +02:00
parent 21c6a8adca
commit f051fc0517
6 changed files with 63 additions and 61 deletions

View File

@ -53,7 +53,7 @@
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>..;c:\Developer\boost_1_52_0;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>..;\Developer\boost_1_52_0;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<EnablePREfast>false</EnablePREfast>
</ClCompile>
<Link>
@ -61,7 +61,7 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>
</AdditionalDependencies>
<AdditionalLibraryDirectories>c:\Developer\boost_1_52_0\stage12\lib</AdditionalLibraryDirectories>
<AdditionalLibraryDirectories>\Developer\boost_1_52_0\stage12\lib</AdditionalLibraryDirectories>
<Profile>true</Profile>
</Link>
<PostBuildEvent>

3
configure vendored
View File

@ -2967,7 +2967,7 @@ fi
ac_config_files="$ac_config_files Makefile examples/Makefile examples/elfdump/Makefile examples/tutorial/Makefile examples/writer/Makefile"
ac_config_files="$ac_config_files Makefile examples/Makefile examples/elfdump/Makefile examples/tutorial/Makefile examples/writer/Makefile examples/write_obj/Makefile"
cat >confcache <<\_ACEOF
# This file is a shell script that caches the results of configure
@ -3720,6 +3720,7 @@ do
"examples/elfdump/Makefile") CONFIG_FILES="$CONFIG_FILES examples/elfdump/Makefile" ;;
"examples/tutorial/Makefile") CONFIG_FILES="$CONFIG_FILES examples/tutorial/Makefile" ;;
"examples/writer/Makefile") CONFIG_FILES="$CONFIG_FILES examples/writer/Makefile" ;;
"examples/write_obj/Makefile") CONFIG_FILES="$CONFIG_FILES examples/write_obj/Makefile" ;;
*) as_fn_error $? "invalid argument: \`$ac_config_target'" "$LINENO" 5;;
esac

View File

@ -19,4 +19,5 @@ AC_OUTPUT([Makefile
examples/Makefile
examples/elfdump/Makefile
examples/tutorial/Makefile
examples/writer/Makefile])
examples/writer/Makefile
examples/write_obj/Makefile])

View File

@ -1 +1 @@
SUBDIRS = elfdump tutorial writer
SUBDIRS = elfdump tutorial writer write_obj

View File

@ -166,7 +166,7 @@ target_alias = @target_alias@
top_build_prefix = @top_build_prefix@
top_builddir = @top_builddir@
top_srcdir = @top_srcdir@
SUBDIRS = elfdump tutorial writer
SUBDIRS = elfdump tutorial writer write_obj
all: all-recursive
.SUFFIXES:

View File

@ -1,14 +1,18 @@
/*
* This example shows how to create an ELF object file for Linux on x86
* This example shows how to create ELF object file for Linux on x86
*
* Instructions:
* 1. Compile and link this file with ELFIO library
* g++ write_obj.cpp -o writer_obj
* 2. Execute result file write_obj
* 3. Link output file test.o:
* gcc -s -nostartfiles test.o -o test
* ./write_obj
* 3. Link output file hello.o:
* gcc -m32 -s -nostartfiles -nostdlib hello.o -o hello
* 4. Run the result file:
* ./hello
*/
#include <elfio/elfio.h>
#include <elfio/elfio.hpp>
using namespace ELFIO;
@ -18,19 +22,12 @@ int main( void )
// You can't proceed before this function call!
writer.create( ELFCLASS32, ELFDATA2LSB );
writer.set_os_abi( ELFOSABI_LINUX );
writer.set_type( ET_EXEC );
writer.set_type( ET_REL );
writer.set_machine( EM_386 );
// Create code section
// Create code section
section* text_sec = writer.sections.add( ".text" );
text_sec->set_type( SHT_PROGBITS );
text_sec->set_flags( SHF_ALLOC | SHF_EXECINSTR );
text_sec->set_addr_align( 0x10 );
// Add data into it
// This is our code
char text[] = { '\xB8', '\x04', '\x00', '\x00', '\x00', // mov eax, 4
'\xBB', '\x01', '\x00', '\x00', '\x00', // mov ebx, 1
'\xB9', '\x00', '\x00', '\x00', '\x00', // mov ecx, msg
@ -42,64 +39,67 @@ int main( void )
'\x2C', '\x20', '\x57', '\x6F', '\x72',
'\x6C', '\x64', '\x21', '\x0A'
};
text_sec->set_data( text, sizeof( text ) );
Elf64_Addr place_to_adjust = 11;
// Create code section
section* text_sec = writer.sections.add( ".text" );
text_sec->set_type ( SHT_PROGBITS );
text_sec->set_flags ( SHF_ALLOC | SHF_EXECINSTR );
text_sec->set_addr_align( 0x10 );
text_sec->set_data ( text, sizeof( text ) );
// Create string table section
section* str_sec = writer.sections.add( ".strtab" );
str_sec->set_type( SHT_STRTAB );
str_sec->set_type ( SHT_STRTAB );
// Create string table writer
string_section_accessor ssa( str_sec );
string_section_accessor stra( str_sec );
// Add label name
Elf32_Word str_index = ssa.add_string( "msg" );
Elf32_Word str_index = stra.add_string( "msg" );
// Create symbol table section
section* sym_sec = writer.sections.add( ".symtab" );
sym_sec->set_type( SHT_SYMTAB );
sym_sec->set_info( 2 );
sym_sec->set_type ( SHT_SYMTAB );
sym_sec->set_info ( 2 );
sym_sec->set_addr_align( 0x4 );
sym_sec->set_entry_size(
writer.get_default_entry_size( SHT_SYMTAB ) );
sym_sec->set_link( str_sec->get_index() );
// Create symbol table writer
IELFOSymbolTable* pSymWriter = 0;
pELFO->CreateSectionWriter( IELFO::ELFO_SYMBOL, pSymSec, (void**)&pSymWriter );
// Add symbol entry (msg has offset == 29)
Elf32_Word nSymIndex = pSymWriter->AddEntry( nStrIndex, 29, 0,
STB_GLOBAL, STT_OBJECT, 0,
pTextSec->GetIndex() );
sym_sec->set_entry_size( writer.get_default_entry_size( SHT_SYMTAB ) );
sym_sec->set_link ( str_sec->get_index() );
// Or another way to add symbol
pSymWriter->AddEntry( pStrWriter, "_start", 0x00000000, 0,
STB_WEAK, STT_FUNC, 0,
pTextSec->GetIndex() );
// Create symbol table writer
symbol_section_accessor syma( writer, sym_sec );
// Add symbol entry (msg has offset == 29)
Elf_Word sym_to_adjust = syma.add_symbol( str_index, 29, 0, STB_GLOBAL,
STT_OBJECT, 0,
text_sec->get_index() );
// Another way to add symbol
syma.add_symbol( stra, "_start", 0x00000000, 0, STB_WEAK, STT_FUNC, 0,
text_sec->get_index() );
// Create relocation table section
IELFOSection* pRelSec = pELFO->AddSection( ".rel.text",
SHT_REL,
0,
pTextSec->GetIndex(),
4,
sizeof(Elf32_Rel) );
pRelSec->SetLink( pSymSec->GetIndex() );
// Create relocation table writer
IELFORelocationTable* pRelWriter = 0;
pELFO->CreateSectionWriter( IELFO::ELFO_RELOCATION, pRelSec, (void**)&pRelWriter );
// Add relocation entry (adjust address at offset 11)
pRelWriter->AddEntry( 11, nSymIndex, (unsigned char)R_386_RELATIVE );
section* rel_sec = writer.sections.add( ".rel.text" );
rel_sec->set_type ( SHT_REL );
rel_sec->set_info ( text_sec->get_index() );
rel_sec->set_addr_align( 0x4 );
rel_sec->set_entry_size( writer.get_default_entry_size( SHT_REL ) );
rel_sec->set_link ( sym_sec->get_index() );
// Another method to add the same relocation entry
// pRelWriter->AddEntry( pStrWriter, "msg",
// pSymWriter, 29, 0,
// ELF32_ST_INFO( STB_GLOBAL, STT_OBJECT ), 0,
// pTextSec->GetIndex(),
// 11, (unsigned char)R_386_RELATIVE );
// Create relocation table writer
relocation_section_accessor rela( writer, rel_sec );
// Add relocation entry (adjust address at offset 11)
rela.add_entry( place_to_adjust, sym_to_adjust,
(unsigned char)R_386_RELATIVE );
// Another method to add the same relocation entry at one step is:
// rela.add_entry( stra, "msg",
// syma, 29, 0,
// ELF_ST_INFO( STB_GLOBAL, STT_OBJECT ), 0,
// text_sec->get_index(),
// place_to_adjust, (unsigned char)R_386_RELATIVE );
// Create note section
section* note_sec = writer.sections.add( ".note" );
note_sec->set_type( SHT_NOTE );
note_sec->set_addr_align( 1 );
note_section_accessor note_writer( writer, note_sec );
note_writer.add_note( 0x01, "Created by ELFIO", 0, 0 );
char descr[6] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36};