Switched to cross compiler

This commit is contained in:
lucic71 2020-06-25 17:48:24 +03:00
parent bc62176374
commit f2d4797be1
6 changed files with 91 additions and 69 deletions

View File

@ -9,19 +9,19 @@ CONFIG_DIR := config/
# Compiler settings # Compiler settings
CC = gcc CC = i686-elf-gcc
CCFLAGS = -m32 -nostdlib -nostdinc -fno-builtin -fno-stack-protector \ CCFLAGS = -ffreestanding -mno-red-zone -c
-nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c CCFLAGS += -Wall -Wextra -Werror -pedantic
# Linker settings # Linker settings
LD = ld LD = i686-elf-gcc
LDFLAGS = -T $(CONFIG_DIR)/link.ld -m elf_i386 LDFLAGS = -T $(CONFIG_DIR)/link.ld -nostdlib -ffreestanding -lgcc
# Assembler settings # Assembler settings
AS = nasm AS = i686-elf-as
ASFLAGS = -f elf32 ASFLAGS =
# Libraries directory. Normally I don't want to include the library headers # Libraries directory. Normally I don't want to include the library headers
# in kmain, but given the fact that in the module interfaces there are includes # in kmain, but given the fact that in the module interfaces there are includes
@ -59,8 +59,6 @@ LIBS = -lklib
.PHONY: all clean module_all .PHONY: all clean module_all
# Build
all: lib_all | module_all kernel.elf all: lib_all | module_all kernel.elf
# module_all - Recursively build all modules. # module_all - Recursively build all modules.
@ -70,6 +68,8 @@ module_all:
@$(MAKE) -C $(MODULES_ROOT_DIR) @$(MAKE) -C $(MODULES_ROOT_DIR)
@echo @echo
# lib_all - Recursively build the library
lib_all: lib_all:
@$(call print_banner, "Building the library") @$(call print_banner, "Building the library")
@$(MAKE) -C $(LIB_DIR) @$(MAKE) -C $(LIB_DIR)
@ -80,7 +80,7 @@ lib_all:
kernel.elf: $(OBJ) kernel.elf: $(OBJ)
@$(call print_banner, "Building the kernel") @$(call print_banner, "Building the kernel")
$(LD) $(LDFLAGS) $(OBJ) $(MODULE_OBJ) -o kernel.elf $(LIBFLAGS) $(LIBS) $(LD) $(OBJ) $(MODULE_OBJ) -o kernel.elf $(LIBFLAGS) $(LIBS) $(LDFLAGS)
# os.iso - Create the OS image and place the kernel fiel in GRUB directory. # os.iso - Create the OS image and place the kernel fiel in GRUB directory.
@ -104,16 +104,14 @@ BOCHS_CONFIG := $(CONFIG_DIR)/bochsrc.txt
run: os.iso run: os.iso
bochs -f $(BOCHS_CONFIG) -q bochs -f $(BOCHS_CONFIG) -q
# Compile and assemble c and s files.
%.o: %.c %.o: %.c
$(CC) $(CCFLAGS) $(INCFLAGS) $< -o $@ $(CC) $< -o $@ $(CCFLAGS) $(INCFLAGS)
%.o: %.s %.o: %.s
$(AS) $(ASFLAGS) $< -o $@ $(AS) $< -o $@ $(ASFLAGS)
clean: mclean lclean clean: mclean lclean
@$(call print_banner, "Cealning root directory") @$(call print_banner, "Cleaning root directory")
@$(RM) *.o kernel.elf os.iso bochslog.txt iso/boot/kernel.elf @$(RM) *.o kernel.elf os.iso bochslog.txt iso/boot/kernel.elf
mclean: mclean:

View File

@ -24,7 +24,7 @@ SO := libklib.a
# Archiver # Archiver
AR = ar AR = i686-elf-ar
ARFLAGS = rcs ARFLAGS = rcs
# Makefile for each library # Makefile for each library

View File

@ -1,16 +1,35 @@
global outb .global outb
global inb .global inb
/*
* outb:
* -----
*
* Passes the port number in %dx and the value to be written in %al. Using
* the outb instruction, the value is written in the I/O port.
*
*/
outb: outb:
mov al, [esp + 8] mov 0x04(%esp), %dx
mov dx, [esp + 4] mov 0x08(%esp), %al
out dx, al outb %al, %dx
ret ret
/*
* inb:
* ----
*
* Passes the port number in %dx. It will return the value in that port after
* after calling the inb instruction.
*
*/
inb: inb:
mov dx, [esp + 4] mov 0x04(%esp), %dx
in al, dx inb %dx, %al
ret ret

View File

@ -1,43 +1,53 @@
global loader .global loader
; Entry point in the kernel. /* Entry point in the kernel. */
extern kmain .extern kmain
; GRUB will load the kernel only if it compiles with the multiboot spec. /*
; The header must contain a magic number, a flag (which can be set to 0x00) * GRUB will load the kernel only if it compiles with the multiboot spec.
; and the checksum (checksum = - (flag + magic_number)) * The header must contain a magic number, a flag (which can be set to 0x00)
* and the checksum (checksum = - (flag + magic_number))
*
*/
MAGIC_NUMBER equ 0x1BADB002 .set MAGIC_NUMBER, 0x1BADB002
FLAGS equ 0x00 .set FLAGS, 0x00
CHECKSUM equ -(MAGIC_NUMBER + FLAGS) .set CHECKSUM, -(MAGIC_NUMBER + FLAGS)
; Put the header in multiboot section so that it is placed in the first 8Kib /*
; of the executable. * Put the header in multiboot section so that it is placed in the first 8Kib
* of the executable.
*
*/
section .multiboot .section .multiboot
align 4 .align 4
dd MAGIC_NUMBER .int MAGIC_NUMBER
dd FLAGS .int FLAGS
dd CHECKSUM .int CHECKSUM
; Create a kstack so that the entry point in the kernel can be called. /* Create a kstack so that the entry point in the kernel can be called. */
KSTACK_SIZE equ 4096 .set KSTACK_SIZE, 4096
section .bss .bss
align 4 .align 16
kstack resb KSTACK_SIZE kstack: .skip KSTACK_SIZE
; Set the stack and call kmain. /* Set the stack and call kmain. */
section .text .text
loader: loader:
mov esp, kstack + KSTACK_SIZE
mov $kstack, %esp
add KSTACK_SIZE, %esp
call kmain call kmain
cli
.halt: .halt:
jmp .halt jmp .halt

View File

@ -28,20 +28,15 @@ OBJ += $(ASM:$(ASM_DIR)/%.s=$(OBJ_DIR)/%.o)
# Compiler settings. # Compiler settings.
CC = gcc CC = i686-elf-gcc
CCFLAGS = -m32 -fPIC -nostdlib -lgcc -nostdinc -fno-builtin -fno-stack-protector \ CCFLAGS = -ffreestanding -mno-red-zone -c
-nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c CCFLAGS += -Wall -Wextra -Werror -pedantic
HFLAGS = -MMD HFLAGS = -MMD
# Linker settings. # Linker settings.
LD = ld LD = i686-elf-gcc
LDFLAGS = -relocatable -m elf_i386 LDFLAGS = -r -nostdlib -ffreestanding -lgcc
# Archiver
AR = ar
ARFLAGS = rcs
# Include flags. # Include flags.
@ -49,8 +44,8 @@ INCFLAGS := $(foreach TMP, $(INC_DIR), -I$(TMP))
# Assembler and flags for assembler. # Assembler and flags for assembler.
AS = nasm AS = i686-elf-as
ASFLAGS = -f elf32 ASFLAGS =
.PHONY: all clean .PHONY: all clean
@ -59,13 +54,13 @@ ASFLAGS = -f elf32
all: $(LIB_OBJ) all: $(LIB_OBJ)
$(LIB_OBJ): $(OBJ) | $(LOBJ_DIR) $(LIB_OBJ): $(OBJ) | $(LOBJ_DIR)
$(LD) $(LDFLAGS) $(OBJ) -o $@ $(LD) $(OBJ) -o $@ $(LDFLAGS)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
$(CC) $(CCFLAGS) $(HFLAGS) $(INCFLAGS) $< -o $@ $(CC) $< -o $@ $(CCFLAGS) $(HFLAGS) $(INCFLAGS)
$(OBJ_DIR)/%.o: $(ASM_DIR)/%.s | $(OBJ_DIR) $(OBJ_DIR)/%.o: $(ASM_DIR)/%.s | $(OBJ_DIR)
$(AS) $(ASFLAGS) $< -o $@ $(AS) $< -o $@ $(ASFLAGS)
$(LOBJ_DIR) $(OBJ_DIR): $(LOBJ_DIR) $(OBJ_DIR):
mkdir -p $@ mkdir -p $@

View File

@ -27,15 +27,15 @@ OBJ := $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)
# Compiler settings # Compiler settings
CC = gcc CC = i686-elf-gcc
CCFLAGS = -m32 -nostdlib -lgcc -nostdinc -fno-builtin -fno-stack-protector \ CCFLAGS = -ffreestanding -mno-red-zone -c
-nostartfiles -nodefaultlibs -Wall -Wextra -Werror -c CCFLAGS += -Wall -Wextra -Werror -pedantic
HFLAGS = -MMD HFLAGS = -MMD
# Linker settings # Linker settings
LD = ld LD = i686-elf-gcc
LDFLAGS = -relocatable -m elf_i386 LDFLAGS = -r -nostdlib -ffreestanding -lgcc
# Include directories # Include directories
@ -47,10 +47,10 @@ INCFLAGS += $(foreach TMP, $(INC_DIR), -I$(TMP))
all: $(MOD_OBJ) all: $(MOD_OBJ)
$(MOD_OBJ): $(OBJ) | $(MOBJ_DIR) $(MOD_OBJ): $(OBJ) | $(MOBJ_DIR)
$(LD) $(LDFLAGS) $(OBJ) -o $@ $(LD) $(OBJ) -o $@ $(LDFLAGS)
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c | $(OBJ_DIR)
$(CC) $(CCFLAGS) $(HFLAGS) $(INCFLAGS) $< -o $@ $(CC) $< -o $@ $(CCFLAGS) $(HFLAGS) $(INCFLAGS)
$(OBJ_DIR) $(MOBJ_DIR): $(OBJ_DIR) $(MOBJ_DIR):
mkdir -p $@ mkdir -p $@