Skip to content

Commit 06097d9

Browse files
committed
Use constants for segment locations
1 parent 7409369 commit 06097d9

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

examples/writer/writer.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,28 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020
THE SOFTWARE.
2121
*/
2222

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+
2337
#include <elfio/elfio.hpp>
2438

2539
using namespace ELFIO;
2640

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+
2745
int main( void )
2846
{
2947
elfio writer;
@@ -45,21 +63,24 @@ int main( void )
4563
char text[] = {
4664
'\xB8', '\x04', '\x00', '\x00', '\x00', // mov eax, 4
4765
'\xBB', '\x01', '\x00', '\x00', '\x00', // mov ebx, 1
48-
'\xB9', '\x00', '\x20', '\x40', '\x00', // mov ecx, msg
66+
'\xB9', '\x00', '\x00', '\x00', '\x00', // mov ecx, msg
4967
'\xBA', '\x0E', '\x00', '\x00', '\x00', // mov edx, 14
5068
'\xCD', '\x80', // int 0x80
5169
'\xB8', '\x01', '\x00', '\x00', '\x00', // mov eax, 1
5270
'\xCD', '\x80' // int 0x80
5371
};
72+
// Adjust data address for 'msg'
73+
*(uint32_t*)( text + 11 ) = DATA_ADDR;
74+
5475
text_sec->set_data( text, sizeof( text ) );
5576

5677
// Create a loadable segment
5778
segment* text_seg = writer.segments.add();
5879
text_seg->set_type( PT_LOAD );
59-
text_seg->set_virtual_address( 0x00401000 );
60-
text_seg->set_physical_address( 0x00401000 );
80+
text_seg->set_virtual_address( CODE_ADDR );
81+
text_seg->set_physical_address( CODE_ADDR );
6182
text_seg->set_flags( PF_X | PF_R );
62-
text_seg->set_align( 0x1000 );
83+
text_seg->set_align( PAGE_SIZE );
6384

6485
// Add code section into program segment
6586
text_seg->add_section_index( text_sec->get_index(),
@@ -80,10 +101,10 @@ int main( void )
80101
// Create a read/write segment
81102
segment* data_seg = writer.segments.add();
82103
data_seg->set_type( PT_LOAD );
83-
data_seg->set_virtual_address( 0x00402000 );
84-
data_seg->set_physical_address( 0x00402000 );
104+
data_seg->set_virtual_address( DATA_ADDR );
105+
data_seg->set_physical_address( DATA_ADDR );
85106
data_seg->set_flags( PF_W | PF_R );
86-
data_seg->set_align( 0x1000 );
107+
data_seg->set_align( PAGE_SIZE );
87108

88109
// Add code section into program segment
89110
data_seg->add_section_index( data_sec->get_index(),
@@ -93,6 +114,7 @@ int main( void )
93114
section* note_sec = writer.sections.add( ".note" );
94115
note_sec->set_type( SHT_NOTE );
95116
note_sec->set_addr_align( 1 );
117+
96118
note_section_accessor note_writer( writer, note_sec );
97119
note_writer.add_note( 0x01, "Created by ELFIO", 0, 0 );
98120
char descr[6] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36 };
@@ -103,7 +125,7 @@ int main( void )
103125
// In this example, the code starts at the first address of the
104126
// 'text_seg' segment. Therefore, the start address is set
105127
// to be equal to the segment location
106-
writer.set_entry( 0x00401000 );
128+
writer.set_entry( CODE_ADDR );
107129

108130
// Create ELF file
109131
writer.save( "hello_x86_64" );

0 commit comments

Comments
 (0)