Improved Linker script & Makefile

This commit is contained in:
Lucia Ceionia 2023-02-10 19:20:17 -06:00
parent cec3b93c83
commit 0953fe31ec
5 changed files with 51 additions and 26 deletions

View File

@ -1,21 +1,41 @@
objects = entry.o kernel.o task.o handler.o interrupt.o v86.o print.o tss.o dosfs/dosfs.o gdt.o usermode.o paging.o fault.o tests.o kbd.o helper.o progs.o disk.o hexedit.o
CFLAGS = -target "i686-elf" -m32 -mgeneral-regs-only -ffreestanding -march=i686 -fno-stack-protector -Wno-int-conversion -nostdlib -c
objects = entry.o kernel.o task.o handler.o interrupt.o v86.o print.o tss.o dosfs/dosfs.o gdt.o\
usermode.o paging.o fault.o tests.o kbd.o helper.o progs.o disk.o hexedit.o
CFLAGS = -target "i686-elf" -m32 -mgeneral-regs-only -ffreestanding\
-march=i686 -fno-stack-protector -Wno-int-conversion -nostdlib -c
LFLAGS = -Wl,--gc-sections -Wl,--print-gc-sections -m32 -nostartfiles -nostdlib
ifeq ($(OUTFILE),)
OUTFILE = virtdisk.bin
endif
.PHONY: $(OUTFILE)
all: $(OUTFILE)
$(OUTFILE): boot.bin kernel.bin
# Copy to boot sector, don't overwrite MBR
dd bs=400 count=1 conv=notrunc if=boot.bin of=$@
# Write kernel beyond boot sector, maximum 128K (256 sectors)
dd bs=512 count=256 seek=1 conv=notrunc if=kernel.bin of=$@
boot.bin: boot.nasm
nasm -o $@ $<
kernel.bin: out.o link.ld
clang $(LFLAGS) -Wl,-M -Tlink.ld -ffreestanding -o $@ $<
out.o: $(objects)
clang $(LFLAGS) -e entry -r -o $@ $^
%.o: %.nasm
nasm -f elf32 -o $@ $<
%.o: %.c
clang $(CFLAGS) -O2 -o $@ $<
clang $(CFLAGS) -ffunction-sections -fdata-sections -Os -o $@ $<
all: $(objects)
nasm boot.nasm -o boot.bin
gcc -Tlink.ld -Wl,-M -m32 -ffreestanding -nostartfiles -nostdlib -o kernel.bin\
$(objects)
dd bs=400 count=1 conv=notrunc if=boot.bin of=virtdisk.bin
dd bs=512 seek=1 conv=notrunc if=kernel.bin of=virtdisk.bin
virtdisk:
dd bs=1M count=32 if=/dev/zero of=virtdisk.bin
echo -n -e '\x55\xaa' | dd bs=1 seek=510 conv=notrunc of=virtdisk.bin
clean:
rm $(objects)
rm -f $(objects) out.o kernel.bin boot.bin

View File

@ -5,12 +5,12 @@
#define BLOCKSHIFT 16 // blockSize = 1 << blockShift
#define MAXFILESIZE 0x80000000 // 2GB
#define TOTALBLOCKS (MAXFILESIZE/BLOCKSIZE)
uint16_t writtenMap[TOTALBLOCKS];
uint32_t blockLenMap[TOTALBLOCKS];
uint16_t writtenMap[TOTALBLOCKS] __attribute__((section(".progbss")));;
uint32_t blockLenMap[TOTALBLOCKS] __attribute__((section(".progbss")));;
// NOTE This is linked at the end of program BSS section,
// so that it can be expanded without telling C how much
// it actually needs
uint8_t writeStoreBase[BLOCKSIZE] __attribute__((section(".bss.end")));
uint8_t writeStoreBase[BLOCKSIZE] __attribute__((section(".proglatebss")));
void HexEditor(uint8_t *path, VOLINFO *vi) {
uint32_t err;
uint16_t *vga_text = (uint16_t *)0xb8000;

27
link.ld
View File

@ -10,33 +10,40 @@ SECTIONS {
.data : {
*(.data);
*(.data*);
*(.rodata);
*(.rodata*);
_edata = .;
}
.realmode 0x4000 :
AT ( ADDR(.data) + SIZEOF(.data) )
AT ( _edata )
{ _v86code = .; *(.v86); _ev86code = .; }
. = ADDR(.data) + SIZEOF(.data) + SIZEOF(.realmode);
. = _edata + SIZEOF(.realmode);
.thing : { _loadusercode = .; }
.usermode 0x400000 :
AT ( ADDR(.data) + SIZEOF(.data) + SIZEOF(.realmode) )
{ _usercode = .; *(.user); _eusercode = .; }
.bss 0x400000 : {
.usermode 0x400000 : AT(_loadusercode) {
_usercode = .; *(.user); _eusercode = .;
}
.bss 0x400000 (NOLOAD) : AT(_loadusercode + SIZEOF(.usermode)) {
_bprogstart = .;
hexedit.o(.bss);
*(.progbss);
*(.progbss*);
_bprogend = .;
hexedit.o(.bss.end);
*(.proglatebss);
*(.proglatebss*);
}
. = ADDR(.data) + SIZEOF(.data) + SIZEOF(.realmode) + SIZEOF(.usermode);
.bss : ALIGN(0x1000)
{
_bstart = .; *(.bss); _bend = .;
_bstart = .; *(.bss); *(.bss*) _bend = .;
}
/DISCARD/ : {
*(.note*)
*(.comment*)
}
}

View File

@ -1,6 +1,4 @@
#include "progs.h"
#include "helper.h"
#include "kbd.h"
void TextViewTest(uint8_t *path, VOLINFO *vi) {
uint16_t *vga_text = (uint16_t *)0xb8000;