From 2a5f6261a53bb35a7316444ea66c6249bebef33e Mon Sep 17 00:00:00 2001 From: Timo Kreuzer Date: Mon, 19 Jul 2010 23:18:31 +0000 Subject: [PATCH] [FREELDR] Convert freeldr and setupldr to PE format. Previously freeldr was a raw binary file, which made handling by the bootsector very easy, but it disqualified it from proper debugging with gdb using symbols. This is possible with having unstripped PE files. As we don't have any space to do proper PE loading from the bootsector (I already had to trim some strings to get enough space for the new jump code), we need to make sure, that the PE file doesn't contain a .bss section, which is achieved by a linker script. The next thing is to make sure, we don't have any symbols in the output file, because they would make freeldr too big to be loaded into memory and they are useless anyway. On the other hand we like to keep the symbols in the .nostrip.sys files if requested, as this is the primary purpose. This is in theory not a problem, as we could simply strip the file at the end, but binutils throw a monkey wrench in our plans: both strip and objcopy disrespect the file alignment and create unaligned sections, that don't naturally match their VAs. This is solved by hacking rbuild to do invoke ld 2 times, one time without and one time with the symbols (if requested). Now the bootsectors also got some changes: instead of jumping to the loading address (0x8000) they get the address of the entry point from the image optional header. This is slightly simplified, by assuming the NtHeader begins at offset 0xE0. This finally allows source level debugging of freeldr with gdb. svn path=/trunk/; revision=48124 --- reactos/boot/freeldr/bootsect/ext2.asm | 8 ++- reactos/boot/freeldr/bootsect/fat.asm | 10 +++- reactos/boot/freeldr/bootsect/fat32.asm | 9 +-- reactos/boot/freeldr/bootsect/isoboot.asm | 51 ++++++++++------- reactos/boot/freeldr/freeldr/freeldr.rbuild | 13 ++++- reactos/boot/freeldr/freeldr/freeldr_i386.lnk | 57 +++++++++++++++++++ reactos/boot/freeldr/freeldr/setupldr.rbuild | 8 ++- reactos/tools/rbuild/backend/mingw/mingw.cpp | 2 +- .../rbuild/backend/mingw/modulehandler.cpp | 57 +++++++++---------- reactos/tools/rbuild/module.cpp | 5 +- 10 files changed, 150 insertions(+), 70 deletions(-) create mode 100644 reactos/boot/freeldr/freeldr/freeldr_i386.lnk diff --git a/reactos/boot/freeldr/bootsect/ext2.asm b/reactos/boot/freeldr/bootsect/ext2.asm index 51c7a4d0dc2..3dd20a2a883 100644 --- a/reactos/boot/freeldr/bootsect/ext2.asm +++ b/reactos/boot/freeldr/bootsect/ext2.asm @@ -438,9 +438,11 @@ LoadFreeLoader: mov dl,[BYTE bp+BootDrive] mov dh,[BYTE bp+BootPartition] - push byte 0 ; We loaded at 0000:8000 - push WORD 8000h ; We will do a far return to 0000:8000h - retf ; Transfer control to FreeLoader + push 0 ; push segment (0x0000) + mov eax, [0x8000 + 0xA8] ; load the RVA of the EntryPoint into eax + add eax, 0x8000 ; RVA -> VA + push ax ; push offset + retf ; Transfer control to FreeLoader diff --git a/reactos/boot/freeldr/bootsect/fat.asm b/reactos/boot/freeldr/bootsect/fat.asm index 79aac6a5dec..d41db93a15a 100644 --- a/reactos/boot/freeldr/bootsect/fat.asm +++ b/reactos/boot/freeldr/bootsect/fat.asm @@ -209,7 +209,11 @@ FoundFreeLoader: ; because they contain a jump instruction to skip ; over the helper code in the FreeLoader image. ;jmp 0000:8003h - jmp 8003h + push 0 ; push segment (0x0000) + mov eax, [0x8000 + 0xA8] ; load the RVA of the EntryPoint into eax + add eax, 0x8003 ; RVA -> VA and skip 3 bytes (jump to fathelper code) + push ax ; push offset + retf ; Transfer control to FreeLoader @@ -387,10 +391,10 @@ NoCarryCHS: msgDiskError db 'Disk error',0dh,0ah,0 -msgFreeLdr db 'freeldr.sys not found',0dh,0ah,0 +msgFreeLdr db 'ldr not found',0dh,0ah,0 ; Sorry, need the space... ;msgAnyKey db 'Press any key to restart',0dh,0ah,0 -msgAnyKey db 'Press any key',0dh,0ah,0 +msgAnyKey db 'Press a key',0dh,0ah,0 filename db 'FREELDR SYS' times 509-($-$$) db 0 ; Pad to 509 bytes diff --git a/reactos/boot/freeldr/bootsect/fat32.asm b/reactos/boot/freeldr/bootsect/fat32.asm index f2faa93c0f9..81574b31bc2 100644 --- a/reactos/boot/freeldr/bootsect/fat32.asm +++ b/reactos/boot/freeldr/bootsect/fat32.asm @@ -390,11 +390,12 @@ LoadFile: LoadFileDone: mov dl,[BYTE bp+BootDrive] ; Load boot drive into DL mov dh,[BootPartition] ; Load boot partition into DH - xor ax,ax - push ax ; We loaded at 0000:8000 - push WORD 8000h ; We will do a far return to 0000:8000h - retf ; Transfer control to ROSLDR + push 0 ; push segment (0x0000) + mov eax, [0x8000 + 0xA8] ; load the RVA of the EntryPoint into eax + add eax, 0x8000 ; RVA -> VA + push ax ; push offset + retf ; Transfer control to FreeLoader ; Returns the FAT entry for a given cluster number ; On entry EAX has cluster number diff --git a/reactos/boot/freeldr/bootsect/isoboot.asm b/reactos/boot/freeldr/bootsect/isoboot.asm index 054fc5bee78..1192443efb9 100644 --- a/reactos/boot/freeldr/bootsect/isoboot.asm +++ b/reactos/boot/freeldr/bootsect/isoboot.asm @@ -31,7 +31,7 @@ ; **************************************************************************** ; Note: The Makefile builds one version with DEBUG_MESSAGES automatically. -;%define DEBUG_MESSAGES ; Uncomment to get debugging messages +%define DEBUG_MESSAGES ; Uncomment to get debugging messages %define WAIT_FOR_KEY @@ -373,9 +373,20 @@ get_fs_structures: mov dl, [DriveNo] ; dl = boot drive mov dh, 0 ; dh = boot partition - jmp 0:0x8000 ; jump into OSLoader + + push 0 ; push segment (0x0000) + mov eax, [0x8000 + 0xA8] ; load the RVA of the EntryPoint into eax + add eax, 0x8000 ; RVA -> VA + push ax ; push offset + retf ; Transfer control to ROSLDR + + mov eax, [0x8000 + 0xA8] ; load the EntryPoint into eax + add eax, 0x8000 + mov [jmpaddress], eax + db 0xea ; jmp instruction + jmpaddress dd 0 ; ; searchdir: @@ -922,38 +933,38 @@ pollchar_and_empty: isolinux_banner db CR, LF, 'Loading IsoBoot...', CR, LF, 0 -copyright_str db ' Copyright (C) 1994-2002 H. Peter Anvin', CR, LF, 0 +copyright_str db ' (C) 1994-2002 H. Peter Anvin', CR, LF, 0 presskey_msg db 'Press any key to boot from CD', 0 dot_msg db '.',0 %ifdef DEBUG_MESSAGES -startup_msg: db 'Starting up, DL = ', 0 -spec_ok_msg: db 'Loaded spec packet OK, drive = ', 0 -secsize_msg: db 'Sector size appears to be ', 0 -rootloc_msg: db 'Root directory location: ', 0 -rootlen_msg: db 'Root directory length: ', 0 -rootsect_msg: db 'Root directory length(sectors): ', 0 -fileloc_msg: db 'SETUPLDR.SYS location: ', 0 -filelen_msg: db 'SETUPLDR.SYS length: ', 0 -filesect_msg: db 'SETUPLDR.SYS length(sectors): ', 0 +startup_msg: db 'Startup, DL = ', 0 +spec_ok_msg: db 'packet OK, drive = ', 0 +secsize_msg: db 'size appears to be ', 0 +rootloc_msg: db 'Root dir loc: ', 0 +rootlen_msg: db 'Root dir len: ', 0 +rootsect_msg: db 'Root dir len(sect): ', 0 +fileloc_msg: db 'SETUPLDR loc: ', 0 +filelen_msg: db 'SETUPLDR len: ', 0 +filesect_msg: db 'SETUPLDR len(sect): ', 0 findfail_msg: db 'Failed to find file!', 0 startldr_msg: db 'Starting SETUPLDR.SYS', 0 %endif -nosecsize_msg: db 'Failed to get sector size, assuming 0800', CR, LF, 0 -spec_err_msg: db 'Loading spec packet failed, trying to wing it...', CR, LF, 0 -maybe_msg: db 'Found something at drive = ', 0 -alright_msg: db 'Looks like it might be right, continuing...', CR, LF, 0 -nothing_msg: db 'Failed to locate CD-ROM device; boot failed.', CR, LF, 0 +nosecsize_msg: db 'No sector size, assume 0800', CR, LF, 0 +spec_err_msg: db 'Load spec failed, trying wing ...', CR, LF, 0 +maybe_msg: db 'Found smth at drive = ', 0 +alright_msg: db 'might be ok, continuing...', CR, LF, 0 +nothing_msg: db 'Failed locate CD-ROM; boot failed.', CR, LF, 0 isolinux_str db 'IsoBoot: ', 0 crlf_msg db CR, LF, 0 diskerr_msg: db 'Disk error ', 0 ondrive_str: db ', drive ', 0 -err_bootfailed db CR, LF, 'Boot failed: press a key to retry...' +err_bootfailed db CR, LF, 'failed..', 0 isolinux_dir db '\LOADER', 0 -no_dir_msg db 'Could not find the LOADER directory.', CR, LF, 0 +no_dir_msg db 'LOADER dir not found.', CR, LF, 0 isolinux_bin db 'SETUPLDR.SYS', 0 -no_isolinux_msg db 'Could not find SETUPLDR.SYS.', CR, LF, 0 +no_isolinux_msg db 'SETUPLDR not found.', CR, LF, 0 ; ; El Torito spec packet diff --git a/reactos/boot/freeldr/freeldr/freeldr.rbuild b/reactos/boot/freeldr/freeldr/freeldr.rbuild index b513dabca44..70974fda396 100644 --- a/reactos/boot/freeldr/freeldr/freeldr.rbuild +++ b/reactos/boot/freeldr/freeldr/freeldr.rbuild @@ -1,8 +1,14 @@ + + + + + - + + freeldr_$(ARCH).lnk freeldr_startup freeldr_base64k @@ -15,8 +21,9 @@ rtl libcntpr - -static - -lgcc + + -nostartfiles + diff --git a/reactos/boot/freeldr/freeldr/freeldr_i386.lnk b/reactos/boot/freeldr/freeldr/freeldr_i386.lnk new file mode 100644 index 00000000000..48b7b5d9ec5 --- /dev/null +++ b/reactos/boot/freeldr/freeldr/freeldr_i386.lnk @@ -0,0 +1,57 @@ +OUTPUT_FORMAT(pei-i386) +ENTRY(_mainCRTStartup) +SECTIONS +{ + .text __image_base__ + __section_alignment__ : + { + __text_start__ = .; + *(.init) + *(.text) + *(SORT(.text$*)) + *(.glue_7t) + *(.glue_7) + ___CTOR_LIST__ = .; __CTOR_LIST__ = . ; + LONG (-1); *(.ctors); *(.ctor); LONG (0); + ___DTOR_LIST__ = .; __DTOR_LIST__ = . ; + LONG (-1); *(.dtors); *(.dtor); LONG (0); + *(.fini) + /* ??? Why is .gcc_exc here? */ + *(.gcc_exc) + __text_end__ = .; + *(.gcc_except_table) + } + init BLOCK(__section_alignment__) : + { + __init_start__ = . ; + *(init) + __init_end__ = . ; + } + .data BLOCK(__section_alignment__) : + { + __data_start__ = . ; + *(.data) + *(.data2) + *(SORT(.data$*)) + __data_end__ = . ; + __bss_start__ = . ; + *(.bss) + *(COMMON) + __bss_end__ = . ; + } + .rdata BLOCK(__section_alignment__) : + { + *(.rdata) + *(SORT(.rdata$*)) + *(.eh_frame) + } + .stab BLOCK(__section_alignment__) (NOLOAD) : + { + [ .stab ] + } + .stabstr BLOCK(__section_alignment__) (NOLOAD) : + { + [ .stabstr ] + } + +} + diff --git a/reactos/boot/freeldr/freeldr/setupldr.rbuild b/reactos/boot/freeldr/freeldr/setupldr.rbuild index bf41b103c48..f8c62169b29 100644 --- a/reactos/boot/freeldr/freeldr/setupldr.rbuild +++ b/reactos/boot/freeldr/freeldr/setupldr.rbuild @@ -1,6 +1,7 @@ + freeldr_$(ARCH).lnk freeldr_startup freeldr_base64k @@ -15,8 +16,9 @@ rtl libcntpr - -nostartfiles - -nostdlib - -lgcc + + + + -Tbss 0x50000 diff --git a/reactos/tools/rbuild/backend/mingw/mingw.cpp b/reactos/tools/rbuild/backend/mingw/mingw.cpp index 7afb0da8ff4..98bed1fb873 100644 --- a/reactos/tools/rbuild/backend/mingw/mingw.cpp +++ b/reactos/tools/rbuild/backend/mingw/mingw.cpp @@ -47,7 +47,7 @@ const struct ModuleHandlerInformations ModuleHandlerInformations[] = { { HostFalse, "", "", "$(LDFLAG_DLL)" }, // Win32OCX { HostFalse, "", "", "$(LDFLAG_CONSOLE)" }, // Win32CUI { HostFalse, "", "", "$(LDFLAG_WINDOWS)" }, // Win32GUI - { HostFalse, "", "", "" }, // BootLoader + { HostFalse, "", "", "$(LDFLAG_DRIVER)" }, // BootLoader { HostFalse, "", "-f bin", "" }, // BootSector { HostFalse, "", "", "" }, // Iso { HostFalse, "", "", "" }, // LiveIso diff --git a/reactos/tools/rbuild/backend/mingw/modulehandler.cpp b/reactos/tools/rbuild/backend/mingw/modulehandler.cpp index 8bff6ea0b16..f04c29f0ffe 100644 --- a/reactos/tools/rbuild/backend/mingw/modulehandler.cpp +++ b/reactos/tools/rbuild/backend/mingw/modulehandler.cpp @@ -2439,13 +2439,7 @@ void MingwBootLoaderModuleHandler::GenerateBootLoaderModuleTarget () { fprintf ( fMakefile, "# BOOT LOADER MODULE TARGET\n" ); - string targetName ( module.output->name ); string targetMacro ( GetTargetMacro (module) ); - string workingDirectory = GetWorkingDirectory (); - FileLocation junk_tmp ( TemporaryDirectory, - "", - module.name + ".junk.tmp" ); - CLEAN_FILE ( junk_tmp ); string objectsMacro = GetObjectsMacro ( module ); string libsMacro = GetLibsMacro (); @@ -2460,33 +2454,34 @@ MingwBootLoaderModuleHandler::GenerateBootLoaderModuleTarget () fprintf ( fMakefile, "\t$(ECHO_LD)\n" ); - if (Environment::GetArch() == "arm") - { - fprintf ( fMakefile, - "\t${gcc} -Wl,--subsystem,native -o %s %s %s %s -nostartfiles -nostdlib\n", - backend->GetFullName ( junk_tmp ).c_str (), - objectsMacro.c_str (), - libsMacro.c_str (), - GetLinkerMacro ().c_str ()); + string linkerScriptArgument; + if ( module.linkerScript != NULL ) { + linkerScriptArgument = ssprintf(" -T %s", backend->GetFullName(*module.linkerScript->file).c_str()); } - else - { - fprintf ( fMakefile, - "\t${gcc} -Wl,--subsystem,native -Wl,-Ttext,0x8000 -o %s %s %s %s -nostartfiles -nostdlib\n", - backend->GetFullName ( junk_tmp ).c_str (), - objectsMacro.c_str (), - libsMacro.c_str (), - GetLinkerMacro ().c_str ()); - } - fprintf ( fMakefile, - "\t${objcopy} -O binary %s $@\n", - backend->GetFullName ( junk_tmp ).c_str () ); - GenerateBuildMapCode ( &junk_tmp ); - fprintf ( fMakefile, - "\t-@${rm} %s 2>$(NUL)\n", - backend->GetFullName ( junk_tmp ).c_str () ); - delete target_file; + /* Link the stripped booloader */ + fprintf(fMakefile, + "\t${ld} --strip-all --subsystem native --entry=%s --image-base=%s %s %s $(PROJECT_CCLIBS) " + "$(BUILTIN_LDFLAGS) $(PROJECT_LDFLAGS) $(LDFLAG_DRIVER) %s -o $@\n", + module.GetEntryPoint().c_str(), + module.baseaddress.c_str(), + objectsMacro.c_str(), + libsMacro.c_str(), + linkerScriptArgument.c_str() ); + + /* Link an unstripped version */ + fprintf(fMakefile, + "ifeq ($(ROS_BUILDNOSTRIP),yes)\n" + "\t${ld} --subsystem native --entry=%s --image-base=%s %s %s $(PROJECT_CCLIBS) " + "$(BUILTIN_LDFLAGS) $(PROJECT_LDFLAGS) $(LDFLAG_DRIVER) %s -o %s$(SEP)%s.nostrip.sys\n" + "endif\n", + module.GetEntryPoint().c_str(), + module.baseaddress.c_str(), + objectsMacro.c_str(), + libsMacro.c_str(), + linkerScriptArgument.c_str(), + backend->GetFullPath(*target_file).c_str(), + module.name.c_str()); } diff --git a/reactos/tools/rbuild/module.cpp b/reactos/tools/rbuild/module.cpp index 95af1d0c8db..e0c9ff826df 100644 --- a/reactos/tools/rbuild/module.cpp +++ b/reactos/tools/rbuild/module.cpp @@ -1132,6 +1132,7 @@ Module::GetDefaultModuleEntrypoint () const return "DllMain"; case Win32CUI: case Test: + case BootLoader: return "mainCRTStartup"; case Win32SCR: case Win32GUI: @@ -1140,7 +1141,6 @@ Module::GetDefaultModuleEntrypoint () const case StaticLibrary: case HostStaticLibrary: case ObjectLibrary: - case BootLoader: case BootSector: case Iso: case LiveIso: @@ -1187,11 +1187,12 @@ Module::GetDefaultModuleBaseaddress () const return "0x00010000"; case ElfExecutable: return "0xe00000"; + case BootLoader: + return "0x8000"; case BuildTool: case StaticLibrary: case HostStaticLibrary: case ObjectLibrary: - case BootLoader: case BootSector: case Iso: case LiveIso: