summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksa Vučković <aleksav013@gmail.com>2021-10-07 12:04:07 +0200
committerAleksa Vučković <aleksav013@gmail.com>2021-10-07 12:04:07 +0200
commited5d024c8b4961b6d722bf45d2c98846afdc1191 (patch)
tree9c3e710bf1f1c64c7cebbffe8527a7c765b96228
parenta9f29cfe168f634434047b55ab16b760e9020680 (diff)
Calling global constructors
-rw-r--r--Makefile52
-rw-r--r--src/boot.s1
-rw-r--r--src/crti.s16
-rw-r--r--src/crtn.s10
-rw-r--r--src/kernel.c9
5 files changed, 72 insertions, 16 deletions
diff --git a/Makefile b/Makefile
index 56ffd05..0a86795 100644
--- a/Makefile
+++ b/Makefile
@@ -1,27 +1,49 @@
-SOURCE_DIR=src
-BUILD_DIR=build
-ISO_DIR=isodir
+CC = i686-elf-gcc
+AS = i686-elf-as
+CFLAGS =
+MKDIR = mkdir -p
+RM = rm -rf
+CP = cp
+
+SOURCE_DIR = src
+BUILD_DIR = build
+ISO_DIR = isodir
+
+AS_SOURCE = boot.s
+C_SOURCE = kernel.c
+
+OBJ_FILES = boot.o kernel.o
+CRTBEGIN_OBJ:=$(shell $(CC) -print-file-name=crtbegin.o)
+CRTEND_OBJ:=$(shell $(CC) -print-file-name=crtend.o)
+
+OBJ = $(BUILD_DIR)/crti.o $(CRTBEGIN_OBJ) $(patsubst %,$(BUILD_DIR)/%,$(OBJ_FILES)) $(CRTEND_OBJ) $(BUILD_DIR)/crtn.o
+
+# Creating iso file
.PHONY: all
all: $(BUILD_DIR)/myos.bin
grub-file --is-x86-multiboot $(BUILD_DIR)/myos.bin
mkdir -p $(ISO_DIR)/boot/grub
- cp $(BUILD_DIR)/myos.bin $(ISO_DIR)/boot/myos.bin
- cp $(SOURCE_DIR)/grub.cfg $(ISO_DIR)/boot/grub/grub.cfg
+ $(CP) $(BUILD_DIR)/myos.bin $(ISO_DIR)/boot/myos.bin
+ $(CP) $(SOURCE_DIR)/grub.cfg $(ISO_DIR)/boot/grub/grub.cfg
grub-mkrescue -o myos.iso $(ISO_DIR)
-$(BUILD_DIR)/boot.o: $(SOURCE_DIR)/boot.s
- mkdir -p $(BUILD_DIR)
- i686-elf-as $(SOURCE_DIR)/boot.s -o $(BUILD_DIR)/boot.o
+# Linking object files
+$(BUILD_DIR)/myos.bin: $(OBJ)
+ $(MKDIR) $(BUILD_DIR)
+ $(CC) -T $(SOURCE_DIR)/linker.ld -o $(BUILD_DIR)/myos.bin -ffreestanding -O2 -nostdlib $(OBJ) -lgcc
-$(BUILD_DIR)/kernel.o: $(SOURCE_DIR)/kernel.c
- mkdir -p $(BUILD_DIR)
- i686-elf-gcc -c $(SOURCE_DIR)/kernel.c -o $(BUILD_DIR)/kernel.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra
+# Compiling as sources
+$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.s
+ $(MKDIR) $(BUILD_DIR)
+ $(AS) $< -o $@
-$(BUILD_DIR)/myos.bin: $(BUILD_DIR)/kernel.o $(BUILD_DIR)/boot.o
- mkdir -p $(BUILD_DIR)
- i686-elf-gcc -T $(SOURCE_DIR)/linker.ld -o $(BUILD_DIR)/myos.bin -ffreestanding -O2 -nostdlib $(BUILD_DIR)/boot.o $(BUILD_DIR)/kernel.o -lgcc
+# Compiling C sources
+$(BUILD_DIR)/%.o: $(SOURCE_DIR)/%.c
+ $(MKDIR) $(BUILD_DIR)
+ $(CC) -c $< -o $@ -std=gnu99 -ffreestanding -O2 -Wall -Wextra
+# Cleaning
.PHONY: clean
clean:
- rm -rf $(BUILD_DIR) $(ISO_DIR) myos.iso
+ $(RM) $(BUILD_DIR) $(ISO_DIR) myos.iso
diff --git a/src/boot.s b/src/boot.s
index 172fd14..a043012 100644
--- a/src/boot.s
+++ b/src/boot.s
@@ -84,6 +84,7 @@ _start:
stack since (pushed 0 bytes so far), so the alignment has thus been
preserved and the call is well defined.
*/
+ call _init
call kernel_main
/*
diff --git a/src/crti.s b/src/crti.s
new file mode 100644
index 0000000..30dd4ea
--- /dev/null
+++ b/src/crti.s
@@ -0,0 +1,16 @@
+/* x86 crti.s */
+.section .init
+.global _init
+.type _init, @function
+_init:
+ push %ebp
+ movl %esp, %ebp
+ /* gcc will nicely put the contents of crtbegin.o's .init section here. */
+
+.section .fini
+.global _fini
+.type _fini, @function
+_fini:
+ push %ebp
+ movl %esp, %ebp
+ /* gcc will nicely put the contents of crtbegin.o's .fini section here. */
diff --git a/src/crtn.s b/src/crtn.s
new file mode 100644
index 0000000..1da795d
--- /dev/null
+++ b/src/crtn.s
@@ -0,0 +1,10 @@
+/* x86 crtn.s */
+.section .init
+ /* gcc will nicely put the contents of crtend.o's .init section here. */
+ popl %ebp
+ ret
+
+.section .fini
+ /* gcc will nicely put the contents of crtend.o's .fini section here. */
+ popl %ebp
+ ret
diff --git a/src/kernel.c b/src/kernel.c
index dd81d33..2c71ba4 100644
--- a/src/kernel.c
+++ b/src/kernel.c
@@ -111,14 +111,21 @@ void terminal_writestring(const char* data)
{
terminal_write(data, strlen(data));
}
+
+char *rec;
+__attribute__ ((constructor)) void foo(void)
+{
+ rec="aleksa";
+}
void kernel_main(void)
{
terminal_initialize();
- for(size_t i=0;i<80;i++)
+ for(size_t i=0;i<50;i++)
{
for(size_t j=0;j<i;j++) terminal_writestring("#");
terminal_writestring("Hello, kernel World!\n");
}
+ terminal_writestring(rec);
}