custom linker script for user apps to guarantee that sections are page-aligned

This commit is contained in:
Benji Dial 2026-01-05 15:14:35 -05:00
parent 225ea67c64
commit 7ec40a1d7e
2 changed files with 59 additions and 18 deletions

View file

@ -52,7 +52,7 @@ echo " command = ld -r \$in -o \$out" >> build.ninja
#eventually maybe a libc will be linked in #eventually maybe a libc will be linked in
echo "rule user_app_ld" >> build.ninja echo "rule user_app_ld" >> build.ninja
echo " command = ld ${COMMON_LD_FLAGS} \$in -o \$out" >> build.ninja echo " command = ld ${COMMON_LD_FLAGS} -T src/user-apps/link.ld \$in -o \$out" >> build.ninja
#builds everything in a directory #builds everything in a directory
# $1 - source directory # $1 - source directory
@ -102,25 +102,27 @@ cp_apps=""
app_deps="" app_deps=""
for dir in src/user-apps/*; do for dir in src/user-apps/*; do
if [ -d $dir ]; then
if [ -e $dir/libraries.txt ]; then
lib_paths=$(cat $dir/libraries.txt | sed -e 's/^.*$/build\/user-libs\/\0\/lib\0.o/' | tr '\n' ' ')
else
lib_paths=""
fi
app_name=$(echo $dir | sed -e 's/^src\/user-apps\///')
build_all $dir user_cc user_app_ld $app_name.elf "$lib_paths"
build_elf=$(echo $dir | sed -e 's/^src/build/')/$app_name.elf
disk_dir=build/disk/calcite/apps/$app_name
disk_elf=$disk_dir/$app_name.elf
cp_apps=$(echo "$cp_apps" \
"mkdir -p $disk_dir &&" \
"cp $build_elf $disk_elf &&")
app_deps="$app_deps $build_elf"
if [ -e $dir/libraries.txt ]; then
lib_paths=$(cat $dir/libraries.txt | sed -e 's/^.*$/build\/user-libs\/\0\/lib\0.o/' | tr '\n' ' ')
else
lib_paths=""
fi fi
app_name=$(echo $dir | sed -e 's/^src\/user-apps\///')
build_all $dir user_cc user_app_ld $app_name.elf "$lib_paths"
build_elf=$(echo $dir | sed -e 's/^src/build/')/$app_name.elf
disk_dir=build/disk/calcite/apps/$app_name
disk_elf=$disk_dir/$app_name.elf
cp_apps=$(echo "$cp_apps" \
"mkdir -p $disk_dir &&" \
"cp $build_elf $disk_elf &&")
app_deps="$app_deps $build_elf"
done done
echo "rule disk" >> build.ninja echo "rule disk" >> build.ninja

39
src/user-apps/link.ld Normal file
View file

@ -0,0 +1,39 @@
OUTPUT_FORMAT(elf64-x86-64)
OUTPUT_ARCH(i386:x86-64)
ENTRY(_start)
PHDRS {
rx PT_LOAD FLAGS(5);
ro PT_LOAD FLAGS(4);
rw PT_LOAD FLAGS(6);
}
SECTIONS {
. = 0x400000;
.text : {
*(.text .text.*)
} : rx
. = ALIGN(4096);
.rodata : {
*(.rodata .rodata.*)
} : ro
. = ALIGN(4096);
.data : {
*(.data .data.*)
} : rw
. = ALIGN(4096);
.bss : {
*(.bss .bss.*)
*(COMMON)
} : rw
}