-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loading ELF files #138
Merged
+130
−6
Merged
Loading ELF files #138
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a144741
wip: ELF file support
morganthomas b5765b2
wip: ELF file support
morganthomas cf05780
wip: ELF file support
morganthomas 57d16fa
wip: ELF file support
morganthomas 6874437
cargo fmt
morganthomas ad8faea
issue/135: better error message
morganthomas 28b8c5f
Fix warnings.
a3c35c8
Merge branch 'main' into morgan/issue/135
morganthomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ members = [ | |
"bus", | ||
"cpu", | ||
"derive", | ||
"elf", | ||
"native_field", | ||
"machine", | ||
"memory", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "valida-elf" | ||
version = "0.1.0" | ||
edition = "2021" | ||
license = "MIT OR Apache-2.0" | ||
|
||
[dependencies] | ||
elf = "0.7.4" | ||
valida-machine = { path = "../machine" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#![no_std] | ||
|
||
extern crate alloc; | ||
|
||
use alloc::collections::BTreeMap; | ||
use alloc::vec; | ||
use alloc::vec::Vec; | ||
use elf::abi; | ||
use elf::endian::AnyEndian; | ||
use elf::section::SectionHeader; | ||
use elf::ElfBytes; | ||
use valida_machine::{ProgramROM, Word}; | ||
|
||
pub struct Program { | ||
pub code: ProgramROM<i32>, | ||
pub data: BTreeMap<u32, Word<u8>>, | ||
} | ||
|
||
pub fn load_executable_file(file: Vec<u8>) -> Program { | ||
if file[0] == 0x7F && file[1] == 0x45 && file[2] == 0x4C && file[3] == 0x46 { | ||
load_elf_object_file(file) | ||
} else { | ||
Program { | ||
code: ProgramROM::from_machine_code(file.as_slice()), | ||
data: BTreeMap::new(), | ||
} | ||
} | ||
} | ||
|
||
pub fn load_elf_object_file(file: Vec<u8>) -> Program { | ||
let file = ElfBytes::<AnyEndian>::minimal_parse(file.as_slice()).unwrap(); | ||
let mut data_sections: Vec<(SectionHeader, &[u8])> = vec![]; | ||
let mut bss_sections: Vec<SectionHeader> = vec![]; | ||
let mut text_sections: Vec<(SectionHeader, &[u8])> = vec![]; | ||
for section_header in file.section_headers().unwrap().iter() { | ||
let is_data: bool = section_header.sh_type == abi::SHT_PROGBITS | ||
&& section_header.sh_flags == (abi::SHF_ALLOC | abi::SHF_WRITE).into(); | ||
let is_bss: bool = section_header.sh_type == abi::SHT_NOBITS | ||
&& section_header.sh_flags == (abi::SHF_ALLOC | abi::SHF_WRITE).into(); | ||
let is_text: bool = section_header.sh_type == abi::SHT_PROGBITS | ||
&& section_header.sh_flags == (abi::SHF_ALLOC | abi::SHF_EXECINSTR).into(); | ||
let is_useful: bool = is_data || is_bss || is_text; | ||
if is_useful { | ||
if is_data || is_text { | ||
let section_data = file.section_data(§ion_header).unwrap(); | ||
match section_data { | ||
(section_data, None) => { | ||
if is_data { | ||
data_sections.push((section_header, section_data)); | ||
} else if is_text { | ||
text_sections.push((section_header, section_data)); | ||
} | ||
} | ||
_ => panic!("unsupported: compressed ELF section data"), | ||
} | ||
} else if is_bss { | ||
bss_sections.push(section_header); | ||
} | ||
} | ||
} | ||
let code_size = text_sections | ||
.iter() | ||
.map(|(section_header, _)| section_header.sh_addr + section_header.sh_size) | ||
.fold(0, |a, b| a.max(b)); | ||
let mut code: Vec<u8> = vec![0; code_size as usize]; | ||
for (section_header, section_data) in text_sections { | ||
for i in 0..section_header.sh_size as usize { | ||
code[i + section_header.sh_addr as usize] = section_data[i]; | ||
} | ||
} | ||
let mut data: BTreeMap<u32, Word<u8>> = BTreeMap::new(); | ||
for (section_header, section_data) in data_sections { | ||
for i in 0..(section_header.sh_size / 4) as usize { | ||
data.insert( | ||
<u64 as TryInto<u32>>::try_into(section_header.sh_addr).unwrap() | ||
+ <usize as TryInto<u32>>::try_into(i * 4).unwrap(), | ||
Word([ | ||
section_data[i * 4], | ||
section_data[i * 4 + 1], | ||
section_data[i * 4 + 2], | ||
section_data[i * 4 + 3], | ||
]), | ||
); | ||
} | ||
} | ||
Program { | ||
code: ProgramROM::from_machine_code(code.as_slice()), | ||
data: data, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this is needed. I've removed this in https://github.com/valida-xyz/valida/pull/145/files and confirmed it works.