start.s :
[BITS 32]
global start
extern _main
start:
mov esp, _sys_stack ; This points the stack to our new stack area
jmp stublet
; This part MUST be 4byte aligned, so we solve that issue using 'ALIGN 4'
ALIGN 4
mboot:
MULTIBOOT_PAGE_ALIGN equ 1<<0
MULTIBOOT_MEMORY_INFO equ 1<<1
MULTIBOOT_AOUT_KLUDGE equ 1<<16
MULTIBOOT_HEADER_MAGIC equ 0x1BADB002
MULTIBOOT_HEADER_FLAGS equ MULTIBOOT_PAGE_ALIGN | MULTIBOOT_MEMORY_INFO | MULTIBOOT_AOUT_KLUDGE
MULTIBOOT_CHECKSUM equ -(MULTIBOOT_HEADER_MAGIC + MULTIBOOT_HEADER_FLAGS)
EXTERN code, bss, end
dd MULTIBOOT_HEADER_MAGIC
dd MULTIBOOT_HEADER_FLAGS
dd MULTIBOOT_CHECKSUM
dd mboot
dd code
dd bss
dd end
dd start
stublet:
call _main
jmp $
SECTION .bss
resb 8192
_sys_stack:
main.c :
void main()
{
for (;;);
}
link.ld :
OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
.text phys : AT(phys) {
code = .;
*(.text)
*(.rodata)
. = ALIGN(4096);
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
. = ALIGN(4096);
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
. = ALIGN(4096);
}
end = .;
}
برای کامپایل از کد های زیر استفاده میکنم ولی خطا میده
nasm -f aout -o start.o start.s
gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o main.o main.c
ld -T link.ld -o kernel.bin start.o
خطا:
emsys@XPS:~/Storage/Applications/Tios/Kernel$ nasm -f aout -o start.o start.s
emsys@XPS:~/Storage/Applications/Tios/Kernel$ gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -fno-builtin -I./include -c -o main.o main.c
main.c:1:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
emsys@XPS:~/Storage/Applications/Tios/Kernel$ ld -T link.ld -o kernel.bin start.o
ld: i386 architecture of input file `start.o' is incompatible with i386:x86-64 output
start.o:start.o:(.text+0x29): undefined reference to `_main'
emsys@XPS:~/Storage/Applications/Tios/Kernel$