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 # Collects memory map information from the BIOS, disables APM, enables A20 and performs the switch from real mode to protected mode before jumping to the main program entry point. The memory map information is stored in the 4KB block of memory immediately following the setup code. The layout of the information matches the Linux boot_params struct. A pointer to this block is passed to the main program, for compatibility with the Linux 32-bit boot protocol. . # define BOOT_PARAMS_START (SETUP_SECS * 512) || SETUP_SEC is defined in boot.h . # define BOOT_PARAMS_END (BOOT_PARAMS_START + 4096) <--- here are the 4K .section ".setup", "ax", @progbits .code16 Emulate the Linux boot header, to allow loading by other boot loaders. Indicate that the main program code should be loaded in high memory. .globl setup setup: jmp do_setup 101: do_setup: Reload the segment registers, except for the stack Get the memory map and disable APM Disable interrupts Enable A20 ... 179: flush: Reload the segment registers and jump to the main test program <------------!!!! ... 188: jump: data32 ljmp $KERNEL_CS, $0 389: Pad to the declared size .org (ESTUP_SECS*512)