问题
I have been working on linking my C++ Kernel to my x86 Assembly Stage2 and It links without any errors but the problem is that When I boot up My OS on Virtual box it doesen't jump to Stage2 which leads me to believe that something is wrong with my linker script before I tried to link the C++ kernel to my assembly code I used:
org 0x200
and it worked but when linking you need an elf executable and elf format does not allow the use of the org directive so I now use for the linker :
global _Start:
_Start:
I do however know that it does locate the file it just needs to have the right address Here is the linker script:
KernAddr = 0x200;
ENTRY(_Start)
SECTIONS
{
. = KernAddr;
.text : AT(ADDR(.text) - KernAddr)
{
_code = .;
*(.text)
*(.rodata*)
. = ALIGN(4096);
}
.data : AT(ADDR(.data) - KernAddr)
{
_data = .;
*(.data)
. = ALIGN(4096);
}
.eh_frame : AT(ADDR(.eh_frame) - KernAddr)
{
_ehframe = .;
*(.eh_frame)
. = ALIGN(4096);
}
.bss : AT(ADDR(.bss) - KernAddr)
{
_bss = .;
*(.bss)
/*
* You usually need to include generated COMMON symbols
* under kernel BSS section or use gcc's -fno-common
*/
*(COMMON)
. = ALIGN(4096);
}
_end = .;
/DISCARD/ :
{
*(.comment)
}
}
Here is the batch program I use to assemble, compile, link and format my OS:
nasm Stage1.asm -o Stage1.bin
nasm -f elf64 Stage2.asm -o Stage2.o
x86_64-elf-g++ -ffreestanding -mcmodel=large -mno-red-zone -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow -c -o kernel.o kernel.cpp
x86_64-elf-ld -T linkerscript.ld -o Anmu.bin Stage2.o kernel.o -nostdlib
copy Stage1.bin Root
copy Anmu.bin Root
mkisofs -b Stage1.bin -no-emul-boot -boot-info-table -o BootLoader.iso ./Root
This is what x86_64-elf-g++ -v Outputs:
Using built-in specs.
COLLECT_GCC=x86_64-elf-g++
COLLECT_LTO_WRAPPER=/home/Anonymous/opt/cross/libexec/gcc/x86_64-elf/4.8.3/lto-wrapper
Target: x86_64-elf
Configured with: ./configure --target=x86_64-elf --prefix=/home/Anonymous/opt/cross --host=x86_64-pc-cygwin --build=x86_64-pc-cygwin --disable-nls --enable-languages=c,c++ --without-headers
Thread model: single
gcc version 4.8.3 (GCC)
Output of x86_64-elf-ld -v :
GNU ld (GNU Binutils) 2.24
Also Here is the full source code if you need to look at it : https://github.com/AnonymousUser1337/Anmu
回答1:
Ok So I figured out that I gave the wrong file length so when it tried to jump to the file it jumped to the wrong address causing it to Not to load the next Stage of the bootloader
来源:https://stackoverflow.com/questions/25255748/linker-script-not-settting-the-origin-correctly