From e54933dc9a9f913782f6c5877735d34a9cf4e481 Mon Sep 17 00:00:00 2001 From: Alejandro Jimenez Date: Thu, 27 Feb 2020 23:14:59 -0500 Subject: [PATCH] pvh: Add test for PVH note parsing Add a test case as part of the load_elf() tests to verify the functionality that parses the ELF Note header to look for a PVH entry point address if one is encoded. This is done by parsing a minimal ELF binary that encodes a predefined address of 0x1e1fe1f, and verifying that the same value is read. A dummy note header is also included in the binary to verify the ability to ignore irrelevant notes. The minimal ELF source code (elfnote.S): #define ELFNOTE_START(name, type, flags) \ .pushsection .note.name, flags, @note ; \ .balign 4 ; \ .long 2f - 1f /* namesz */ ; \ .long 4484f - 3f /* descsz */ ; \ .long type ; \ 1:.asciz #name ; \ 2:.balign 4 ; \ 3: #define ELFNOTE_END \ 4484:.balign 4 ; \ .popsection ; #define ELFNOTE(name, type, desc) \ ELFNOTE_START(name, type, "a") \ desc ; \ ELFNOTE_END #define XEN_ELFNOTE_PHYS32_ENTRY 18 #define NT_VERSION 1 ELFNOTE(dummy, NT_VERSION, .quad 0xcafecafe) ELFNOTE(PVHNote, XEN_ELFNOTE_PHYS32_ENTRY, .quad 0x1e1fe1f) .section ".text","ax" .global _start _start: Built with: $ gcc elfnote.S -s -nostdlib -o test_elfnote.bin Signed-off-by: Alejandro Jimenez --- src/loader/mod.rs | 14 ++++++++++++++ src/loader/test_elfnote.bin | Bin 0 -> 648 bytes 2 files changed, 14 insertions(+) create mode 100755 src/loader/test_elfnote.bin diff --git a/src/loader/mod.rs b/src/loader/mod.rs index 9c260dfa..f6348b3c 100644 --- a/src/loader/mod.rs +++ b/src/loader/mod.rs @@ -544,6 +544,12 @@ mod test { v } + #[cfg(feature = "elf")] + #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] + fn make_elfnote() -> Vec { + include_bytes!("test_elfnote.bin").to_vec() + } + #[allow(safe_packed_borrows)] #[allow(non_snake_case)] #[test] @@ -664,6 +670,14 @@ mod test { Some(highmem_start_address) ) ); + + let pvhnote_image = make_elfnote(); + loader_result = Elf::load(&gm, None, &mut Cursor::new(&pvhnote_image), None).unwrap(); + println!( + "PVH entry point at address {:8x} \n", + loader_result.pvh_entry_addr.unwrap().raw_value() + ); + assert_eq!(loader_result.pvh_entry_addr.unwrap().raw_value(), 0x1e1fe1f); } #[test] diff --git a/src/loader/test_elfnote.bin b/src/loader/test_elfnote.bin new file mode 100755 index 0000000000000000000000000000000000000000..36efd2d3f1a02ef7f9228e87506a8ef5c88e1246 GIT binary patch literal 648 zcmb<-^>JfjWMqH=CI&kO5RZq^0W1U|85leeN*Ej%EEt%8Br5|8SS<@!C5(XSfY3Zp zeK4AVK>@4~qz8mIfO#O=0nCEYF;H