programmern00b wrote:
fpr this step; gcc -ffreestanding -nostdlib -nostartfiles -fno-pic -fcf-protection=none -gdwar>
did you mean this; gcc -ffreestanding -nostdlib -nostartfiles -fno-pic -fcf-protection=none -gdwarf -c main.c, or was it supposed to be like that?
Sorry, nano cut out that last bit
Code:
gcc -ffreestanding -nostdlib -nostartfiles -fno-pic -fcf-protection=none -gdwarf-4 -m16 -ggdb3 -c main.c -o main.o
Since you're using a platform specific compiler, this disables GCC start files and disables fcf protections (they're meant for programs running on an OS, otherwise you'd have endbr32's everywhere)
----------------------------------------------------
The way the book says to do it isos.lds:
Code:
ENTRY(main);
PHDRS
{
headers PT_PHDR FILEHDR PHDRS;
code PT_LOAD;
}
SECTIONS
{
.text 0x600: ALIGN(0x100) { *(.text) } :code
.data : { *(.data) }
.bss : { *(.bss) }
/DISCARD/ : { *(.eh_frame) }
}
and in your build process add -nmagic:
Code:
ld -m elf_i386 -nmagic -T os.lds main.o -o main
The way I did it (is probably not the best, but) literally strips out all of the ELF headers and data.
It produces two files, one binary and one with the symbols; the PC (or emulator) uses the binary file while gdb uses the symbol file.
----------------------------------------------------Just a few tips:
- If you're running a Linux or BSD distro (in a virtual machine, or on real hardware) with a graphical envorionment, there's a software called DDD (Data Display Debugger). It has a horrible UI, but it's one of the easiest ways (for me at least) to see everything I'm debugging. It uses gdb for everything, but makes it much faster and easier to see and debug things.
- If you run QEMU with
Code:
qemu-system-i386 <flags here> -s
it will be the same as
Code:
qemu-system-i386 <flags here> -gdb tcp::1234
- The book you're using ends very soon. At this point, you'll either want to continue to create a
Real Mode OS/bootloader or create a
Protected Mode (or even
Long Mode) OS. If you choose protected mode (like I did, although I did make a little tiny bootloader once), I'd recommend looking at the
Bare Bones page.
- You could have probably put this thread in OS Development rather than General Programming, but I don't think it matters too much
----------------------------------------------------(as a bonus, here's how I'd make bootloader.asm)
Code:
;*************************************************
; bootloader.asm
; A Simple Bootloader
;*************************************************
bits 16
start: jmp boot
;; constants and variable definitions
msg db "Welcome to My Operating System!", 0ah, 0dh, 0h
boot:
cli ; no interrupts
cld ; all that we need to init
xor ax,ax ; This is the same thing as doing "mov ax,0"
mov ss,ax ; Set stack segment to 0
mov sp,0xFFFF ; Set stack to start at 0xFFFF and grow down
;; set buffer
mov es, ax
mov bx, 0x0600
mov al, 1 ; read one sector
mov ch, 0 ; track 0
mov cl, 2 ; sector to read
mov dh, 0 ; head number
mov dl, 0 ; drive number
mov ah, 0x02 ; read sectors from disk
int 0x13 ; call the BIOS routine
call 0x0000:0x0600 ; execute the sector, but provide a way for main to return
hlt ; halt the system
; We have to be 512 bytes. Clear the rest of the bytes with 0
times 510 - ($-$$) db 0
dw 0xAA55 ; Boot Signature