Issue
i've been working on my kernel project and to simulate it (that is to run it on QEMU), i need it as a .iso file.
here is boot.s:-
.set MAGIC, 0x1BADB002
# set flags to 0
.set FLAGS, 0
# set the checksum
.set CHECKSUM, -(MAGIC + FLAGS)
# set multiboot enabled
.section .multiboot
# define type to long for each data defined as above
.long MAGIC
.long FLAGS
.long CHECKSUM
# set the stack bottom
stackBottom:
# define the maximum size of stack to 512 bytes
.skip 1024
# set the stack top which grows from higher to lower
stackTop:
.section .text
.global _start
.type _start, @function
_start:
# assign current stack pointer location to stackTop
mov $stackTop, %esp
# call the kernel main source
call kernel_entry
cli
# put system in infinite loop
hltLoop:
hlt
jmp hltLoop
.size _start, . - _start
and to assemble it - as --32 boot.s -o boot.o
and for the main code (which is in c++), to compile it - gcc -S kernel.cpp -lstdc++ -o kernel.o
gives no error. but,
while linking it with this linker script:-
ENTRY(_start)
SECTIONS
{
/* we need 1MB of space atleast */
. = 1M;
/* text section */
.text BLOCK(4K) : ALIGN(4K)
{
*(.multiboot)
*(.text)
}
/* read only data section */
.rodata BLOCK(4K) : ALIGN(4K)
{
*(.rodata)
}
/* data section */
.data BLOCK(4K) : ALIGN(4K)
{
*(.data)
}
/* bss section */
.bss BLOCK(4K) : ALIGN(4K)
{
*(COMMON)
*(.bss)
}
}
(the linking command i'm using is ld -T linker.ld kernel.o boot.o -o OS.bin
)
it says:-
ld:kernel.o: file format not recognized; treating as linker script
ld:kernel.o:1: syntax error
am i doing something wrong with linking or assembling boot.s or compiling kernel.cpp?
Solution
gcc -S
produces assembly language, but ld
expects an object file. Somewhere in between you have to run the assembler.
There's no particular need to use the assembler output, so most likely you want to use -c
, which does compilation and then assembly to produce an object file, instead of -S
:
gcc -c kernel.cpp -o kernel.o
The -lstdc++
is useless because it only applies when linking, and anyway it seems unlikely that you can successfully use the standard C++ library in a kernel.
Answered By - Nate Eldredge