@@ -20,10 +20,28 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
20
THE SOFTWARE.
21
21
*/
22
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++ 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
+
23
37
#include < elfio/elfio.hpp>
24
38
25
39
using namespace ELFIO ;
26
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
+
27
45
int main ( void )
28
46
{
29
47
elfio writer;
@@ -45,21 +63,24 @@ int main( void )
45
63
char text[] = {
46
64
' \xB8 ' , ' \x04 ' , ' \x00 ' , ' \x00 ' , ' \x00 ' , // mov eax, 4
47
65
' \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
49
67
' \xBA ' , ' \x0E ' , ' \x00 ' , ' \x00 ' , ' \x00 ' , // mov edx, 14
50
68
' \xCD ' , ' \x80 ' , // int 0x80
51
69
' \xB8 ' , ' \x01 ' , ' \x00 ' , ' \x00 ' , ' \x00 ' , // mov eax, 1
52
70
' \xCD ' , ' \x80 ' // int 0x80
53
71
};
72
+ // Adjust data address for 'msg'
73
+ *(uint32_t *)( text + 11 ) = DATA_ADDR;
74
+
54
75
text_sec->set_data ( text, sizeof ( text ) );
55
76
56
77
// Create a loadable segment
57
78
segment* text_seg = writer.segments .add ();
58
79
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 );
61
82
text_seg->set_flags ( PF_X | PF_R );
62
- text_seg->set_align ( 0x1000 );
83
+ text_seg->set_align ( PAGE_SIZE );
63
84
64
85
// Add code section into program segment
65
86
text_seg->add_section_index ( text_sec->get_index (),
@@ -80,10 +101,10 @@ int main( void )
80
101
// Create a read/write segment
81
102
segment* data_seg = writer.segments .add ();
82
103
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 );
85
106
data_seg->set_flags ( PF_W | PF_R );
86
- data_seg->set_align ( 0x1000 );
107
+ data_seg->set_align ( PAGE_SIZE );
87
108
88
109
// Add code section into program segment
89
110
data_seg->add_section_index ( data_sec->get_index (),
@@ -93,6 +114,7 @@ int main( void )
93
114
section* note_sec = writer.sections .add ( " .note" );
94
115
note_sec->set_type ( SHT_NOTE );
95
116
note_sec->set_addr_align ( 1 );
117
+
96
118
note_section_accessor note_writer ( writer, note_sec );
97
119
note_writer.add_note ( 0x01 , " Created by ELFIO" , 0 , 0 );
98
120
char descr[6 ] = { 0x31 , 0x32 , 0x33 , 0x34 , 0x35 , 0x36 };
@@ -103,7 +125,7 @@ int main( void )
103
125
// In this example, the code starts at the first address of the
104
126
// 'text_seg' segment. Therefore, the start address is set
105
127
// to be equal to the segment location
106
- writer.set_entry ( 0x00401000 );
128
+ writer.set_entry ( CODE_ADDR );
107
129
108
130
// Create ELF file
109
131
writer.save ( " hello_x86_64" );
0 commit comments