Skip to content

Commit a2cedb2

Browse files
andreeaflorescualexandruag
authored andcommitted
regenerated test_elf.bin with ELFIO
This was a rather large image. Reduced the size from 8.29KB to 640 bytes. Added source file for generating the image. Signed-off-by: Andreea Florescu <fandree@amazon.com>
1 parent ba6614a commit a2cedb2

File tree

4 files changed

+116
-3
lines changed

4 files changed

+116
-3
lines changed

docs/TESTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ example, with minimal changes on top.
5858
| bad_align_writer.cpp | test_bad_align.bin |
5959
| invalid_pvh_note_writer.cpp | test_invalid_pvh_note.bin |
6060
| dummy_note.cpp | test_dummy_note.bin |
61+
| basic_elf.cpp | test_elf.bin |
6162

6263
#### Example for generating `test_bad_align.bin`
6364

docs/elfio_files/basic_elf.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}

src/loader/x86_64/elf/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,10 +464,10 @@ mod tests {
464464
Some(highmem_start_address),
465465
)
466466
.unwrap();
467-
assert_eq!(loader_result.kernel_load.raw_value(), 0x300000);
467+
assert_eq!(loader_result.kernel_load.raw_value(), 0x200400);
468468

469469
loader_result = Elf::load(&gm, Some(kernel_addr), &mut Cursor::new(&image), None).unwrap();
470-
assert_eq!(loader_result.kernel_load.raw_value(), 0x300000);
470+
assert_eq!(loader_result.kernel_load.raw_value(), 0x200400);
471471

472472
loader_result = Elf::load(
473473
&gm,
@@ -476,7 +476,7 @@ mod tests {
476476
Some(highmem_start_address),
477477
)
478478
.unwrap();
479-
assert_eq!(loader_result.kernel_load.raw_value(), 0x100000);
479+
assert_eq!(loader_result.kernel_load.raw_value(), 0x400);
480480

481481
highmem_start_address = GuestAddress(0xa00000);
482482
assert_eq!(

src/loader/x86_64/elf/test_elf.bin

100755100644
-7.66 KB
Binary file not shown.

0 commit comments

Comments
 (0)