Skip to content

Commit 421cedb

Browse files
elf64 module loader
1 parent 80c894d commit 421cedb

File tree

10 files changed

+806
-2
lines changed

10 files changed

+806
-2
lines changed

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CMakeLists.txt

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ set(HARD_DISK_IMAGE "../../harddisk0" CACHE STRING "Path to hard disk image when
1111

1212
set(CMAKE_VERBOSE_MAKEFILE OFF)
1313
set(CMAKE_C_COMPILER gcc)
14-
set(CMAKE_C_LINK_EXECUTABLE "ld -m elf_x86_64 -nostdlib -no-pie -T ${CMAKE_CURRENT_SOURCE_DIR}/buildtools/linker.ld <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
14+
set(CMAKE_C_LINK_EXECUTABLE "ld -m elf_x86_64 -nostdlib -no-pie --export-dynamic -T ${CMAKE_CURRENT_SOURCE_DIR}/buildtools/linker.ld <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
1515
set(CMAKE_ASM_FLAGS "-pipe -g -fno-PIC -Wall -Wextra -Wno-unused-parameter -Wno-int-to-pointer-cast -nostdlib -ffreestanding -nostartfiles -nodefaultlibs -mcmodel=large -mno-red-zone")
1616

1717
set(CMAKE_ASM_NASM_OBJECT_FORMAT elf64)
@@ -97,5 +97,47 @@ foreach (timezone ${timezone_list})
9797
list(APPEND TIMEZONE_TARGETS "timezone_${TZ_NAME}")
9898
endforeach()
9999

100+
# --- Build loadable modules from modules/<name>/*.c into iso/system/modules/<name>.ko
101+
file(GLOB MODULE_DIRS RELATIVE ${CMAKE_SOURCE_DIR}/modules ${CMAKE_SOURCE_DIR}/modules/*)
102+
set(MODULE_TARGETS "")
103+
set(MODULE_KO_FILES "") # collect .ko outputs so ISO rule can depend on them
104+
foreach(mod ${MODULE_DIRS})
105+
if (IS_DIRECTORY ${CMAKE_SOURCE_DIR}/modules/${mod})
106+
file(GLOB mod_srcs ${CMAKE_SOURCE_DIR}/modules/${mod}/*.c)
107+
if (mod_srcs)
108+
# Compile module sources as an object target with large code model etc.
109+
add_library(mod_${mod} OBJECT ${mod_srcs})
110+
target_compile_options(mod_${mod} PRIVATE
111+
-ffreestanding -fno-pic -fno-plt -mcmodel=large -mno-red-zone
112+
-fvisibility=hidden -fno-asynchronous-unwind-tables -fno-exceptions
113+
)
114+
target_include_directories(mod_${mod} PRIVATE ${UACPI_INCLUDES})
115+
116+
# Link to a single ET_REL (.ko) using ld -r
117+
set(MOD_OUT ${CMAKE_BINARY_DIR}/iso/system/modules/${mod}.ko)
118+
add_custom_command(OUTPUT ${MOD_OUT}
119+
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/iso/system/modules
120+
COMMAND ${CMAKE_LINKER} -r -o ${MOD_OUT} $<TARGET_OBJECTS:mod_${mod}>
121+
DEPENDS $<TARGET_OBJECTS:mod_${mod}>
122+
VERBATIM
123+
)
124+
125+
add_custom_target(module_${mod} ALL DEPENDS ${MOD_OUT})
126+
add_dependencies(ISO module_${mod})
127+
list(APPEND MODULE_TARGETS module_${mod})
128+
list(APPEND MODULE_KO_FILES ${MOD_OUT})
129+
endif()
130+
endif()
131+
endforeach()
132+
133+
# make a single stamp that depends on all .ko files and inject it into iso() deps via IMAGE_TARGETS
134+
set(MODULES_STAMP ${CMAKE_BINARY_DIR}/iso/system/modules/modules.stamp)
135+
add_custom_command(OUTPUT ${MODULES_STAMP}
136+
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/iso/system/modules
137+
COMMAND ${CMAKE_COMMAND} -E touch ${MODULES_STAMP}
138+
DEPENDS ${MODULE_KO_FILES}
139+
VERBATIM
140+
)
141+
list(APPEND IMAGE_TARGETS ${MODULES_STAMP})
100142

101143
iso("rr.iso" "kernel.sym")

buildtools/linker.ld

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ SECTIONS {
4747
KEEP(*(SORT_BY_NAME(.kw.*)))
4848
__stop_kw_array = .;
4949

50+
. = ALIGN(8);
51+
__dynsym_start = .;
52+
KEEP(*(.dynsym))
53+
__dynsym_stop = .;
54+
55+
. = ALIGN(8);
56+
__dynstr_start = .;
57+
KEEP(*(.dynstr))
58+
__dynstr_stop = .;
59+
5060
. = ALIGN(0x1000);
5161
}
5262

cmake/custom_targets.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,6 @@ function(iso TARGETFILE SOURCEFILE)
151151
set(OUTNAME "${CMAKE_BINARY_DIR}/${TARGETFILE}")
152152
add_custom_command(OUTPUT ${OUTNAME}
153153
COMMAND php ../build-boot-image.php && xorriso -as mkisofs --quiet -b limine-cd.bin -joliet -no-emul-boot -boot-load-size 4 -boot-info-table -V "RETROROCKET" --protective-msdos-label "${CMAKE_BINARY_DIR}/iso" -o "${CMAKE_BINARY_DIR}/rr.iso"
154-
DEPENDS SYMBOLS "kernel.bin" "RUN_run.sh" "DEBUG_debug.sh" ${basic_program_list} ${basic_library_list} ${basic_driver_list} ${KEYMAP_TARGETS} ${TIMEZONE_TARGETS} ${IMAGE_TARGETS})
154+
DEPENDS SYMBOLS "kernel.bin" "RUN_run.sh" "DEBUG_debug.sh" ${basic_program_list} ${basic_library_list} ${basic_driver_list} ${KEYMAP_TARGETS} ${TIMEZONE_TARGETS} ${IMAGE_TARGETS} ${MODULE_TARGETS})
155155
add_dependencies(ISO SYMBOLS "kernel.bin" "RUN_run.sh" "DEBUG_debug.sh" "config_limine.cfg")
156156
endfunction()

include/kernel.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#include "retrofs.h"
7676
#include "serial.h"
7777
#include "debug_ringbuffer.h"
78+
#include "module.h"
7879

7980
#define assert(expr, message) if (!(expr)) { \
8081
kprintf("Assertion failure at %s:%d: %s\n", __FILE__, __LINE__, message); \

include/kmalloc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ uint64_t get_total_memory(void);
112112
*/
113113
void preboot_fail(const char* msg);
114114

115+
void* kmalloc_aligned(uint64_t size, uint64_t align);
116+
117+
void kfree_aligned(const void* ptr);
118+
115119
/**
116120
* @brief Free memory and set the pointer to NULL.
117121
*

0 commit comments

Comments
 (0)