diff --git a/sortix/Makefile b/sortix/Makefile
index eb9b870e..05cb18cb 100644
--- a/sortix/Makefile
+++ b/sortix/Makefile
@@ -62,6 +62,11 @@ endif
# Object files that constitute the kernel.
+CRTI_OBJ:=$(CPU)/crti.o
+CRTBEGIN_OBJ:=$(shell $(HOSTCXX) $(CXXFLAGS) -print-file-name=crtbegin.o)
+CRTEND_OBJ:=$(shell $(HOSTCXX) $(CXXFLAGS) -print-file-name=crtend.o)
+CRTN_OBJ:=$(CPU)/crtn.o
+
LIBS=\
-nostdlib \
-lc-sortix \
@@ -139,9 +144,14 @@ vnode.o \
worker.o \
ALLOBJS=\
+$(CRTI_OBJ) \
$(OBJS) \
+$(CRTN_OBJ) \
end.o
+LINK_OBJECTS=\
+$(CRTI_OBJ) $(CRTBEGIN_OBJ) $(OBJS) $(LIBS) $(CRTN_OBJ) $(CRTEND_OBJ) end.o
+
# Rules and recipes for building the kernel.
all: kernel
@@ -157,7 +167,7 @@ headers:
ifeq ($(CPU),x64)
sortix-x86_64.bin: $(ALLOBJS)
- $(HOSTCXX) $(CXXFLAGS) -Wl,-Ttext -Wl,100000 -Wl,-z -Wl,max-page-size=0x1000 $(OBJS) $(LIBS) end.o -o $@
+ $(HOSTCXX) $(CXXFLAGS) -Wl,-Ttext -Wl,100000 -Wl,-z -Wl,max-page-size=0x1000 $(LINK_OBJECTS) -o $@
sortix.bin: sortix-x86_64.bin
$(HOSTOBJCOPY) $< -O elf32-i386-sortix $@
@@ -168,7 +178,7 @@ endif
ifeq ($(CPU),x86)
sortix.bin: $(ALLOBJS)
- $(HOSTCXX) $(CXXFLAGS) -Wl,-Ttext -Wl,100000 $(OBJS) $(LIBS) end.o -o $@
+ $(HOSTCXX) $(CXXFLAGS) -Wl,-Ttext -Wl,100000 $(LINK_OBJECTS) -o $@
endif
diff --git a/sortix/kernel.cpp b/sortix/kernel.cpp
index d58dac62..0934a5af 100644
--- a/sortix/kernel.cpp
+++ b/sortix/kernel.cpp
@@ -160,6 +160,8 @@ extern "C" void KernelInit(unsigned long magic, multiboot_info_t* bootinfo)
// Stage 1. Initialization of Early Environment.
//
+ // TODO: Call global constructors using the _init function.
+
// Initialize system calls.
Syscall::Init();
diff --git a/sortix/x64/crti.s b/sortix/x64/crti.s
new file mode 100644
index 00000000..f51ce61f
--- /dev/null
+++ b/sortix/x64/crti.s
@@ -0,0 +1,39 @@
+/*******************************************************************************
+
+ Copyright(C) Jonas 'Sortie' Termansen 2012.
+
+ This file is part of Sortix.
+
+ Sortix is free software: you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation, either version 3 of the License, or (at your option) any later
+ version.
+
+ Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ details.
+
+ You should have received a copy of the GNU General Public License along with
+ Sortix. If not, see .
+
+ x64/crti.s
+ Provides the header of the _init and _fini functions.
+
+*******************************************************************************/
+
+.section .init
+.global _init
+.type _init, @function
+_init:
+ push %rbp
+ movq %rsp, %rbp
+ /* gcc will nicely put the contents of crtbegin.o's .init section here. */
+
+.section .fini
+.global _fini
+.type _fini, @function
+_fini:
+ push %rbp
+ movq %rsp, %rbp
+ /* gcc will nicely put the contents of crtbegin.o's .fini section here. */
diff --git a/sortix/x64/crtn.s b/sortix/x64/crtn.s
new file mode 100644
index 00000000..f70b562d
--- /dev/null
+++ b/sortix/x64/crtn.s
@@ -0,0 +1,33 @@
+/*******************************************************************************
+
+ Copyright(C) Jonas 'Sortie' Termansen 2012.
+
+ This file is part of Sortix.
+
+ Sortix is free software: you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation, either version 3 of the License, or (at your option) any later
+ version.
+
+ Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ details.
+
+ You should have received a copy of the GNU General Public License along with
+ Sortix. If not, see .
+
+ x64/crtn.s
+ Provides the tail of the _init and _fini functions.
+
+*******************************************************************************/
+
+.section .init
+ /* gcc will nicely put the contents of crtend.o's .init section here. */
+ popq %rbp
+ ret
+
+.section .fini
+ /* gcc will nicely put the contents of crtend.o's .fini section here. */
+ popq %rbp
+ ret
diff --git a/sortix/x86/crti.s b/sortix/x86/crti.s
new file mode 100644
index 00000000..6a9adfe0
--- /dev/null
+++ b/sortix/x86/crti.s
@@ -0,0 +1,39 @@
+/*******************************************************************************
+
+ Copyright(C) Jonas 'Sortie' Termansen 2012.
+
+ This file is part of Sortix.
+
+ Sortix is free software: you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation, either version 3 of the License, or (at your option) any later
+ version.
+
+ Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ details.
+
+ You should have received a copy of the GNU General Public License along with
+ Sortix. If not, see .
+
+ x86/crti.s
+ Provides the header of the _init and _fini functions.
+
+*******************************************************************************/
+
+.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/sortix/x86/crtn.s b/sortix/x86/crtn.s
new file mode 100644
index 00000000..13ab8252
--- /dev/null
+++ b/sortix/x86/crtn.s
@@ -0,0 +1,33 @@
+/*******************************************************************************
+
+ Copyright(C) Jonas 'Sortie' Termansen 2012.
+
+ This file is part of Sortix.
+
+ Sortix is free software: you can redistribute it and/or modify it under the
+ terms of the GNU General Public License as published by the Free Software
+ Foundation, either version 3 of the License, or (at your option) any later
+ version.
+
+ Sortix is distributed in the hope that it will be useful, but WITHOUT ANY
+ WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+ details.
+
+ You should have received a copy of the GNU General Public License along with
+ Sortix. If not, see .
+
+ x86/crtn.s
+ Provides the tail of the _init and _fini functions.
+
+*******************************************************************************/
+
+.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