From 29a8eebf9231692f965f29221d140e20b0a1f64b Mon Sep 17 00:00:00 2001 From: Regia König Date: Wed, 24 Aug 2022 14:28:23 +0200 Subject: Analyse memtest86 binary structure --- .../24_08_22_memtest_build_process | 205 +++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 documentation/memtest86_build_process/24_08_22_memtest_build_process diff --git a/documentation/memtest86_build_process/24_08_22_memtest_build_process b/documentation/memtest86_build_process/24_08_22_memtest_build_process new file mode 100644 index 0000000..9847a5b --- /dev/null +++ b/documentation/memtest86_build_process/24_08_22_memtest_build_process @@ -0,0 +1,205 @@ + +Linker scripts: + memtest_shared.lds + memtest_efi.lds +boot/ + header.S || The standard EFI header + setup.S + efisetup.c || Macros + private functions + || Only for 32-bit boot entry point??? + startup64.S + +*.c files in app/, lib/, system/, tests/ + +########################################################################## +## ## +## MEMTEST LINKING PART 1 - MEMTEST_SHARED ## +## ## +########################################################################## + +# +# memtest_shared.lds +# + +OUTPUT_FORMAT("elf64-x86-64") + +ENTRY(startup64); <--------- in startup64.S + +SECTIONS { + .text : { + _start = .; <---------- First section in memtest_shared-.text + *(.text) + *(.text.*) + ... + + .rodata + .dynsym + .dynstr + .hash + .gnu.hash + .dynamic + + .rela.text + .rela.rodata + .rela.data + .rela.got + .rela.plt + + .data + .got + .bss + +# +# startup64.S +# +118: .globl startup64 +119: startup64: +120: cld +121: cli + + # Save the boot params pointer +125: movq %rsi, boot_params_addr(%rip) + +127: jmp startup + +143: .globl startup +144: startup: + Use startup stack until we pick a correct one + Pick the correct stack + Initialize the pml4 and pdp tables + Set the page directory base address + Initialise the GDT and the segment registers + Load the GDT and the segment registers + Initialise the IDT + Initialise the IDT descriptor + Zero the BSS (if first boot) + Initialise the FPU + Enable SSE + Call the dynamic linker to fix up the addresses in the GOT + +268: call reloc <---------------- system/reloc64.c:151 + get_load_address() + get_dynamic_section_offset() + get_dynamic_info() + do_relocations + +276: call main <---------------- app/main.c:499 # main entry point called from the startup code + +# +# Analysis of the binary +# + +* objdump -h memtest_shared + .text ... File off Algn + 00200000 2**4 + +* objdump -g memtest_shared | grep efi_setup # check for occurences + | grep efi_handover + +* readelf -h memtest_shared + ELF Header: + Magic: 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 + Class: ELF64 + Data: 2's complement, little endian + Version: 1 (current) + OS/ABI: UNIX - System V + ABI Version: 0 + Type: DYN (Shared object file) + Machine: Advanced Micro Devices X86-64 + Version: 0x1 + Entry point address: 0x200 <-----------------That's startup64 !!! + Start of program headers: 64 (bytes into file) + Start of section headers: 2821656 (bytes into file) + Flags: 0x0 + Size of this header: 64 (bytes) + Size of program headers: 56 (bytes) + Number of program headers: 3 + Size of section headers: 64 (bytes) + Number of section headers: 26 + Section header string table index: 25 + +* objdump -d memtest_shared + memtest_shared: file format elf64-x86-64 + + Disassembly of section .text: + + 0000000000000000 <_start>: + 0: cld + 1: cli + + 9f: ljmp *-0x6(%rsp) + + 00000000000001e0 : + + 1ed: jmp 210 + + 0000000000000200 : + + 202: mov %rsi,0x23df7(%rip) # 2400 + 209: jmp 220 + + 0000000000000210 : + + 214: callq 68f + 219: mov %rax,0x23de0(%rip) # 2400 + + 0000000000000220 : + + 2ce: ljmp *-0x6(%rsp) + + + +########################################################################## +## ## +## MEMTEST LINKING PART 2 - MEMTEST.EFI ## +## ## +########################################################################## + +# +# memtest_efi.lds +# +OUTPUT_FORMAT("binary") + +ENTRY(boot); <----- in boot/header.S:36 + +SECTIONS { + .header : { <----- boot/header.S + *(.header) + } + .setup : { <----- boot/setup.S + *(.setup) + } + .text : { + _text_start = .; + *(.data) <----- !!! NO .text !!!! + ... + +# +# header.S +# +The EFI loader loads the header at ImageBase, so we have to locate the main program +after that. This means we can't load the main program at HIGH_LOAD_ADDR. Pick a load +address well away from HIGH_LOAD_ADDR, to avoid overlap when relocating code. + +. # define IMAGE_BASE 0x200000 +. # define BASE_OF_CODE 0x1000 <- where do these values come from????? + + .section ".header", "ax", @progbits + .code16 + + .globl boot + +boot: + "MZ", the MS-DOS header signature + .byte 0x4d + .byte 0x5a + +pe_header: + +coff_header: + +... + +# +# setup.S +# -- cgit v1.2.3-55-g7522