TO_ISO :=
TO_CLEAN :=
-include */module.mk
+include **/module.mk
build: amycros.iso
qemu: build
TO_ISO += isodir/boot/$(EXEC_NAME)
TO_CLEAN += $(OUT_DIR) $(DEP_DIR)
-OLD_CXXFLAGS := $(CXXFLAGS)
-OLD_LDFLAGS := $(LDFLAGS)
-CXXFLAGS := $(OLD_CXXFLAGS) -mcmodel=kernel -mno-red-zone \
+LOCAL_CXXFLAGS := $(CXXFLAGS) -mcmodel=kernel -mno-red-zone \
-isystem /usr/include -isystem /usr/include/c++/12.2.1 \
-isystem /usr/include/c++/12.2.1/x86_64-pc-linux-gnu
# The previous two lines are a dirty hack.
-LDFLAGS := $(OLD_LDFLAGS) -T kernel/linker.ld -z max-page-size=0x1000 \
- -mno-red-zone -mcmodel=kernel
+LOCAL_LDFLAGS := $(LDFLAGS) -T kernel/linker.ld -z max-page-size=0x1000 \
+ -mno-red-zone -mcmodel=kernel
CPPOBJS := $(patsubst $(SRC_DIR)%,$(OUT_DIR)%.o,$(shell find $(SRC_DIR) -name '*.cpp'))
ASMOBJS := $(patsubst $(SRC_DIR)%,$(OUT_DIR)%.o,$(shell find $(SRC_DIR) -name '*.S' | grep -v crt))
CRTI_OBJ := $(OUT_DIR)/lib/crti.S.o
-CRTBEGIN_OBJ := $(shell $(CXX) $(LDFLAGS) -print-file-name=crtbegin.o)
-CRTEND_OBJ := $(shell $(CXX) $(LDFLAGS) -print-file-name=crtend.o)
+CRTBEGIN_OBJ := $(shell $(CXX) $(LOCAL_LDFLAGS) -print-file-name=crtbegin.o)
+CRTEND_OBJ := $(shell $(CXX) $(LOCAL_LDFLAGS) -print-file-name=crtend.o)
CRTN_OBJ := $(OUT_DIR)/lib/crtn.S.o
OBJS := $(CRTI_OBJ) $(CRTBEGIN_OBJ) $(CPPOBJS) $(ASMOBJS) $(CRTEND_OBJ) $(CRTN_OBJ)
$(OUT_DIR)$(EXEC_NAME): OBJS := $(OBJS)
$(OUT_DIR)$(EXEC_NAME): LDLIBS := -nostdlib -lgcc
-$(OUT_DIR)$(EXEC_NAME): LDFLAGS := $(LDFLAGS)
+$(OUT_DIR)$(EXEC_NAME): LDFLAGS := $(LOCAL_LDFLAGS)
$(OUT_DIR)$(EXEC_NAME): $(OBJS) kernel/linker.ld
mkdir -p "$(@D)"
$(CXX) $(LDFLAGS) -o "$@" $(OBJS) $(LDLIBS)
-
$(OUT_DIR)%.o: DEP_DIR := $(DEP_DIR)
-$(OUT_DIR)%.o: CXXFLAGS := $(CXXFLAGS)
+$(OUT_DIR)%.o: CXXFLAGS := $(LOCAL_CXXFLAGS)
$(OUT_DIR)%.o: $(SRC_DIR)%
mkdir -p $(@D)
mkdir -p $(dir $(DEP_DIR)$*)
$(CXX) $(CXXFLAGS) -c "$<" -MMD -MT "$@" -MF "$(DEP_DIR)$*.d" -o "$@"
-include $(DEP_DIR)*.d
-
-CXXFLAGS := $(OLD_CXXFLAGS)
-LDFLAGS := $(OLD_LDFLAGS)
--- /dev/null
+#include "elf64.hpp"
--- /dev/null
+#pragma once
#include "serial.hpp"
#include "interrupts.hpp"
#include "ring3.hpp"
+#include "elf64.hpp"
os::idt<32> idt;
extern "C" os::tss TSS;
--- /dev/null
+ENTRY(_start)
+
+SECTIONS {
+ . = 1M;
+
+ . = ALIGN(4K);
+ .text ALIGN(4K) : AT(ADDR(.text)) {
+ *(.text)
+ *(.text*)
+ *(.gnu.linkonce.t*)
+ . = ALIGN(8);
+ *(.init)
+ . = ALIGN(8);
+ *(.fini)
+ . = ALIGN(8);
+ *(.ctors)
+ . = ALIGN(8);
+ *(.dtors)
+ }
+ .rodata ALIGN(4K) : AT(ADDR(.rodata)) {
+ *(.rodata)
+ *(.rodata*)
+ *(.gnu.linkonce.r*)
+ }
+ .eh_frame ALIGN(4K) : AT(ADDR(.eh_frame)) {
+ *(.eh_frame)
+ }
+ .data ALIGN(4K) : AT(ADDR(.data)) {
+ *(.data)
+ *(.data*)
+ *(.gnu.linkonce.d*)
+ }
+ .bss ALIGN(4K) : AT(ADDR(.bss)) {
+ *(.bss)
+ *(COMMON)
+ *(.bss*)
+ *(.gnu.linkonce.b*)
+ }
+ . = ALIGN(4K);
+
+ /DISCARD/ : {
+ *(.comment)
+ }
+}
# So, about the license. I don't think I have a copyright on this file's code, but,
# since I gave it the CC0 license in case I do, that doesn't matter either way.
+SRC_DIR := test_module/src/
+OUT_DIR := test_module/out/
+DEP_DIR := test_module/dep/
+EXEC_NAME := test.elf64
+
TO_ISO += isodir/boot/test-module
+TO_CLEAN += $(OUT_DIR) $(DEP_DIR)
+
+LOCAL_CXXFLAGS := $(CXXFLAGS) -O0
+LOCAL_LDFLAGS := $(LDFLAGS) -T test_module/linker.ld -z max-page-size=0x1000
+
+CPPOBJS := $(patsubst $(SRC_DIR)%,$(OUT_DIR)%.o,$(shell find $(SRC_DIR) -name '*.cpp'))
+ASMOBJS := $(patsubst $(SRC_DIR)%,$(OUT_DIR)%.o,$(shell find $(SRC_DIR) -name '*.S'))
+OBJS := $(CPPOBJS) $(ASMOBJS)
+
+isodir/boot/test-module: $(OUT_DIR)$(EXEC_NAME)
+ mkdir -p "$(@D)"
+ objdump -d "$<" | \
+ grep ' [0-9a-e]\{6\}' | sed 's/ [^ \t]*\t//' | sed 's/\t.*//' | sed 's/[ \t]\+/ /g' | tr '\n' ' ' | \
+ sed 's/ / /g' | xargs -n 1 printf '\\\\x%s\n' | xargs -n 1 printf \
+ > "$@"
+
+$(OUT_DIR)$(EXEC_NAME): OBJS := $(OBJS)
+$(OUT_DIR)$(EXEC_NAME): LDLIBS := -nostdlib -lgcc
+$(OUT_DIR)$(EXEC_NAME): LDFLAGS := $(LOCAL_LDFLAGS)
+$(OUT_DIR)$(EXEC_NAME): $(OBJS) kernel/linker.ld
+ mkdir -p "$(@D)"
+ $(CXX) $(LDFLAGS) -o "$@" $(OBJS) $(LDLIBS)
+
+$(OUT_DIR)%.o: DEP_DIR := $(DEP_DIR)
+$(OUT_DIR)%.o: CXXFLAGS := $(LOCAL_CXXFLAGS)
+$(OUT_DIR)%.o: $(SRC_DIR)%
+ mkdir -p $(@D)
+ mkdir -p $(dir $(DEP_DIR)$*)
+ $(CXX) $(CXXFLAGS) -c "$<" -MMD -MT "$@" -MF "$(DEP_DIR)$*.d" -o "$@"
-isodir/boot/test-module: test_module/test.S
- mkdir -p isodir/boot
- export TMP="$$(mktemp)"; \
- printf "$$($(CXX) $(CXX_ARGS) -c "$<" -o $$TMP && objdump $$TMP -d | grep -A 10000000000 '<_start>' | tail -n +2 | sed 's/^ *[0-9a-f]*:\t//' | sed 's/\t.*$$//' | tr -d '\n' | sed 's/ */ /g' | sed 's/^/ /' | sed 's/ $$//' | sed 's/ /\\x/g')" > isodir/boot/test-module; \
- rm "$$TMP"
--- /dev/null
+.section .text
+
+.globl print
+print:
+ syscall
+ ret
--- /dev/null
+extern "C" void print(char c);
+
+extern "C" void _start() {
+ const char str[4] = "AH\n";
+ for (int i = 0; str[i] != '\0'; i++) {
+ print(str[i]);
+ }
+ while (true) {}
+}
+++ /dev/null
-.section .text
-
-.globl _start
-_start:
- mov $65, %rdi
- syscall
- mov $67, %rdi
- syscall
- mov $65, %rdi
- syscall
- mov $66, %rdi
- syscall
- mov $10, %rdi
- syscall
-1: jmp 1b