[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
This commit is contained in:
Timo Kreuzer 2010-07-19 23:18:31 +00:00
parent de85efc5ed
commit 2a5f6261a5
10 changed files with 150 additions and 70 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -1,8 +1,14 @@
<?xml version="1.0"?>
<if property="ARCH" value="i386">
<property name="BASEADDRESS_FREELDR" value="0x8000" />
<endif>
<!DOCTYPE group SYSTEM "../../../tools/rbuild/project.dtd">
<group>
<if property="ARCH" value="i386">
<module name="freeldr" type="bootloader">
<module name="freeldr" type="bootloader" baseaddress="$(BASEADDRESS_FREELDR)">
<linkerscript>freeldr_$(ARCH).lnk</linkerscript>
<bootstrap installbase="loader" />
<library>freeldr_startup</library>
<library>freeldr_base64k</library>
@ -15,8 +21,9 @@
<library>rtl</library>
<library>libcntpr</library>
<group linkerset="ld">
<linkerflag>-static</linkerflag>
<linkerflag>-lgcc</linkerflag>
<!-- linkerflag>-static</linkerflag -->
<linkerflag>-nostartfiles</linkerflag>
</group>
</module>
</if>

View file

@ -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 ]
}
}

View file

@ -1,6 +1,7 @@
<?xml version="1.0"?>
<!DOCTYPE module SYSTEM "../../../tools/rbuild/project.dtd">
<module name="setupldr" type="bootloader">
<linkerscript>freeldr_$(ARCH).lnk</linkerscript>
<bootstrap installbase="loader" />
<library>freeldr_startup</library>
<library>freeldr_base64k</library>
@ -15,8 +16,9 @@
<library>rtl</library>
<library>libcntpr</library>
<group linkerset="ld">
<linkerflag>-nostartfiles</linkerflag>
<linkerflag>-nostdlib</linkerflag>
<linkerflag>-lgcc</linkerflag>
<!-- linkerflag>-nostartfiles</linkerflag -->
<!-- linkerflag>-nostdlib</linkerflag -->
<!-- linkerflag>--strip-all</linkerflag -->
<linkerflag>-Tbss 0x50000</linkerflag>
</group>
</module>

View file

@ -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

View file

@ -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());
}

View file

@ -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: