Relocations in PE executable
Posted: Sun Oct 14, 2012 5:28 am
Hi, for the n-th time, I get back to osdev.org. This time I would like to develop using Visual Studio 2012.
So, VS can only emit PE executables. I don't want any creepy hack to inject a multiboot header in it, so I wrote a custom bootloader for PE executables on a FAT16 hard disk.
I've been successful (at least, it seems to work), to build a file interface like fopen, fread, fseek in real mode ASM. Then I loaded the executable sections to the ImageBase address. The problem is that the code contains references to strange memory addresses.
For example main:
Is compiled to:
So, for example, print should be at address 0x440, while actually it is at 0x101440. So that looks like an RVA. The same is for the string which looks to be at 0x1400. Which flags shall I use to ask the linker to remove all those relocations from my code??
My linker command line is:
On the wiki I read that I should use "/DRIVER". Unfortunatey, that switch doesn't work in VS2012 due to a subtle bug, which I'm trying to workaround. More info here: http://connect.microsoft.com/VisualStud ... ith-driver
What does "/DRIVER" do to the ultimate executable?
Thanks in advance.
Update:
Just upgraded from VS2012 RC to the final release. The problem with /DRIVER is now solved. But relocations are still there...
So, VS can only emit PE executables. I don't want any creepy hack to inject a multiboot header in it, so I wrote a custom bootloader for PE executables on a FAT16 hard disk.
I've been successful (at least, it seems to work), to build a file interface like fopen, fread, fseek in real mode ASM. Then I loaded the executable sections to the ImageBase address. The problem is that the code contains references to strange memory addresses.
For example main:
Code: Select all
int main()
{
print("Hello world from the 64-bit kernel.");
return 0;
}
Code: Select all
00000490 4883EC28 sub rsp,byte +0x28
00000494 488D0D650F0000 lea rcx,[rel 0x1400]
0000049B E8A0FFFFFF call dword 0x440
000004A0 33C0 xor eax,eax
000004A2 4883C428 add rsp,byte +0x28
000004A6 C3 ret
My linker command line is:
Code: Select all
/OUT:".\krnl.exe" /NXCOMPAT:NO /PDB:".\krnl.pdb" /DYNAMICBASE /MAPINFO:EXPORTS /FIXED:NO /LARGEADDRESSAWARE /BASE:"0x100000" /MACHINE:X64 /ENTRY:"main" /WINMD:NO /OPT:REF /SAFESEH:NO /INCREMENTAL:NO /PGD:".\krnl.pgd" /SUBSYSTEM:NATIVE /MAP":Kernel.map" /OPT:ICF /ERRORREPORT:QUEUE /NOLOGO /ALIGN:4096 /NODEFAULTLIB /TLBID:1
What does "/DRIVER" do to the ultimate executable?
Thanks in advance.
Update:
Just upgraded from VS2012 RC to the final release. The problem with /DRIVER is now solved. But relocations are still there...