|
1 | 1 | use super::*; |
2 | 2 | use crate::falcon::Ram; |
| 3 | +use crate::falcon::decoder::decode; |
| 4 | +use crate::falcon::instruction::Instruction; |
3 | 5 |
|
| 6 | +/// The ELF fixture committed under tests/fixtures/elf (its README says how to |
| 7 | +/// rebuild it). It lives in the repository so these tests run on a clean |
| 8 | +/// checkout, without a rust-to-raven or c-to-raven build lying around. |
4 | 9 | fn elf_path() -> std::path::PathBuf { |
5 | | - let manifest_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
6 | | - let rels = [ |
7 | | - "rust-to-raven/rust-to-raven.elf", |
8 | | - "c-to-raven/c-to-raven.elf", |
9 | | - ]; |
10 | | - |
11 | | - for root in manifest_dir.ancestors() { |
12 | | - for rel in rels { |
13 | | - let path = root.join(rel); |
14 | | - if path.exists() { |
15 | | - return path; |
16 | | - } |
17 | | - } |
18 | | - } |
19 | | - |
20 | | - panic!("no versioned ELF fixture found under rust-to-raven/ or c-to-raven/"); |
| 10 | + std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/elf/fixture.elf") |
21 | 11 | } |
22 | 12 |
|
23 | 13 | fn elf_bytes() -> Vec<u8> { |
24 | | - std::fs::read(elf_path()).expect("failed to read ELF fixture") |
| 14 | + let path = elf_path(); |
| 15 | + std::fs::read(&path) |
| 16 | + .unwrap_or_else(|e| panic!("failed to read ELF fixture {}: {e}", path.display())) |
| 17 | +} |
| 18 | + |
| 19 | +/// Address of `name` in the symbol table `load_elf` returned. |
| 20 | +fn symbol_addr(info: &ElfInfo, name: &str) -> u32 { |
| 21 | + info.symbols |
| 22 | + .iter() |
| 23 | + .find(|(_, names)| names.iter().any(|n| n == name)) |
| 24 | + .map(|(&addr, _)| addr) |
| 25 | + .unwrap_or_else(|| panic!("symbol `{name}` missing from the ELF symbol table")) |
25 | 26 | } |
26 | 27 |
|
27 | 28 | fn read_u16(bytes: &[u8], off: usize) -> u16 { |
@@ -87,6 +88,61 @@ fn elf_text_segment_loaded_into_ram() { |
87 | 88 | // first word at entry point must be readable and non-zero |
88 | 89 | let word = mem.load32(info.entry).expect("load32 at entry failed"); |
89 | 90 | assert_ne!(word, 0, "entry point word is zero — segment not loaded"); |
| 91 | + |
| 92 | + // The disassembler gets the same bytes as RAM, and the word at the entry |
| 93 | + // point is the fixture's first instruction, `li a0, 1`. |
| 94 | + let off = (info.entry - info.text_base) as usize; |
| 95 | + assert_eq!(info.text_bytes[off..off + 4], word.to_le_bytes()); |
| 96 | + assert!( |
| 97 | + matches!(decode(word), Ok(Instruction::Addi { rd: 10, rs1: 0, imm: 1 })), |
| 98 | + "entry word 0x{word:08x} is not `li a0, 1`" |
| 99 | + ); |
| 100 | +} |
| 101 | + |
| 102 | +#[test] |
| 103 | +fn elf_data_segment_loaded_and_bss_tail_zeroed() { |
| 104 | + let bytes = elf_bytes(); |
| 105 | + let mut mem = Ram::new(1024 * 1024); |
| 106 | + let info = load_elf(&bytes, &mut mem).expect("load_elf failed"); |
| 107 | + let counter = symbol_addr(&info, "counter"); |
| 108 | + let scratch = symbol_addr(&info, "scratch"); |
| 109 | + |
| 110 | + // Load again over RAM that is not zero, so a bss tail left alone shows up. |
| 111 | + let mut mem = Ram::new(1024 * 1024); |
| 112 | + for i in 0..16 { |
| 113 | + mem.store8(scratch + i, 0xAA).unwrap(); |
| 114 | + } |
| 115 | + load_elf(&bytes, &mut mem).expect("load_elf failed"); |
| 116 | + |
| 117 | + assert_eq!(mem.load32(counter).unwrap(), 41, ".data word not loaded"); |
| 118 | + for i in 0..16 { |
| 119 | + assert_eq!(mem.load8(scratch + i).unwrap(), 0, "bss byte {i} not zeroed"); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +#[test] |
| 124 | +fn elf_symbols_and_sections_parsed() { |
| 125 | + let bytes = elf_bytes(); |
| 126 | + let mut mem = Ram::new(1024 * 1024); |
| 127 | + let info = load_elf(&bytes, &mut mem).expect("load_elf failed"); |
| 128 | + |
| 129 | + assert_eq!(symbol_addr(&info, "_start"), info.entry); |
| 130 | + assert_eq!(symbol_addr(&info, "bump"), info.text_base); |
| 131 | + assert_eq!(symbol_addr(&info, "greeting"), info.data_base); |
| 132 | + assert!( |
| 133 | + info.symbols.values().flatten().all(|n| !n.starts_with('$')), |
| 134 | + "mapping symbols ($x, $d) should be filtered out" |
| 135 | + ); |
| 136 | + |
| 137 | + let names: Vec<&str> = info.sections.iter().map(|s| s.name.as_str()).collect(); |
| 138 | + assert_eq!(names, [".rodata", ".data", ".bss"]); |
| 139 | + let rodata = &info.sections[0]; |
| 140 | + assert_eq!(rodata.addr, info.data_base); |
| 141 | + assert_eq!(rodata.bytes, b"hello from the ELF fixture\n"); |
| 142 | + let bss = &info.sections[2]; |
| 143 | + assert_eq!(bss.addr, symbol_addr(&info, "scratch")); |
| 144 | + assert_eq!(bss.size, 16); |
| 145 | + assert!(bss.bytes.is_empty(), ".bss has no file bytes"); |
90 | 146 | } |
91 | 147 |
|
92 | 148 | #[test] |
|
0 commit comments