From 03c95cb0a40d7d853c5b73373c6f037dfebd4abc Mon Sep 17 00:00:00 2001 From: d0p1 Date: Wed, 20 Mar 2024 16:51:27 +0100 Subject: [PATCH] chore: add 'coff.h' header --- .gitignore | 3 +- Makefile | 2 +- bin/Makefile | 2 + bin/cmd/Makefile | 3 +- bin/cmd/cmd.asm | 13 +++--- bin/cmd/cmd.obj | Bin 0 -> 180 bytes include/Makefile | 16 +++++++ include/coff.h | 103 ++++++++++++++++++++++++++++++++++++++++++++++ kernel/Makefile | 50 +++++++++++----------- kernel/kernel.asm | 36 ++++++++-------- tools/Makefile | 7 +++- tools/coff-ld.c | 95 ++++++++++++++++++++++++++++++++++++++++++ tools/create-iso | 7 ++-- tools/fat.c | 2 +- 14 files changed, 282 insertions(+), 57 deletions(-) create mode 100644 bin/cmd/cmd.obj create mode 100644 include/Makefile create mode 100644 include/coff.h create mode 100644 tools/coff-ld.c diff --git a/.gitignore b/.gitignore index 404c5bf..df6bdda 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ vmstupid vmstupid-dbg /tools/fat +/tools/coff-ld /sysroot log.txt @@ -26,4 +27,4 @@ a.out bochsrc.bxrc /tmp *.tar.gz -*.sys \ No newline at end of file +*.sys diff --git a/Makefile b/Makefile index 9237a55..9cb518a 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ RM = echo MK_BUGREPORT := \"https://git.cute.engineering/d0p1/StupidOS/issues\" MK_COMMIT := \"$(shell git rev-parse --short HEAD)\" -SUBDIRS := tools boot kernel lib bin +SUBDIRS := tools include boot kernel lib bin TARGET = stupid.tar.gz floppy_boot.img ifneq ($(OS),Windows_NT) diff --git a/bin/Makefile b/bin/Makefile index 66aafb1..d66369a 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -2,6 +2,8 @@ SUBDIRS = cmd TOPGOALS = all clean install +SUBDIRS = cmd + .PHONY: $(SUBDIRS) $(SUBDIRS): @echo "📁 bin/$@" diff --git a/bin/cmd/Makefile b/bin/cmd/Makefile index 8d1fed2..900d535 100644 --- a/bin/cmd/Makefile +++ b/bin/cmd/Makefile @@ -1,5 +1,6 @@ all: + fasm cmd.asm clean: -install: +install: all diff --git a/bin/cmd/cmd.asm b/bin/cmd/cmd.asm index ea7e2ea..af13008 100644 --- a/bin/cmd/cmd.asm +++ b/bin/cmd/cmd.asm @@ -1,8 +1,11 @@ - format ELF - entry start + format COFF - include 'builtins.inc' + section '.text' code -start: + public main +main: + int 0x2A - int 0x2A \ No newline at end of file + section '.data' data + + INCLUDE 'builtins.inc' diff --git a/bin/cmd/cmd.obj b/bin/cmd/cmd.obj new file mode 100644 index 0000000000000000000000000000000000000000..c1838f6e2bff05825a04667f5d649e98e3e38ca8 GIT binary patch literal 180 zcmeZaWMUBi`zy7Ifq{V;h#44I81+h0D@wp@2*CuTQjqu{rUDS_r6iUlf;dnh0;JPm z94PGoq|a(GC8sdzF&C$nu$JZlNtV=#%o1c<7=fyo8FCXd^Pt)oKq5>iHh^5g%)kO9 E0gBcX-~a#s literal 0 HcmV?d00001 diff --git a/include/Makefile b/include/Makefile new file mode 100644 index 0000000..6d73e21 --- /dev/null +++ b/include/Makefile @@ -0,0 +1,16 @@ +INCDIR = $(DESTDIR)/usr/include + +INCS = coff.h + +.PHONY: all +all: + +.PHONY: clean +clean: + +.PHONY: install +install: $(INCS) + @ mkdir -p $(INCDIR) + install $< $(INCDIR) + +.PHONY: all clean install diff --git a/include/coff.h b/include/coff.h new file mode 100644 index 0000000..a5b96ce --- /dev/null +++ b/include/coff.h @@ -0,0 +1,103 @@ +#ifndef COFF_H +# define COFF_H 1 + +# include + +typedef struct filehdr +{ + uint16_t f_magic; + uint16_t f_nscns; + int32_t f_timdat; + int32_t f_symptr; + int32_t f_nsyms; + uint16_t f_opthdr; + uint16_t f_flags; +} FILHDR; + +# define FILHSZ sizeof(FILHDR) + +# define F_MACH_I386 0x014c + +# define F_RELFLG 0x0001 +# define F_EXEC 0x0002 +# define F_LNNO 0x0004 +# define F_LSYMS 0x0008 +# define F_LITTLE 0x0100 +# define F_BIG 0x0200 +# define F_SYMMERGE 0x1000 + +typedef struct aouthdr +{ + int16_t magic; + int16_t vstamp; + int32_t tsize; + int32_t dsize; + int32_t bsize; + int32_t entry; + int32_t text_start; + int32_t data_start; +} AOUTHDR; + +# define AOUTHSZ sizeof(AOUTHDR) + +# define OMAGIC 0404 +# define ZMAGIC 0413 +# define STMAGIC 0401 +# define SHMAGIC 0443 + +typedef struct scnhdr +{ + int8_t s_name[8]; + int32_t s_paddr; + int32_t s_vaddr; + int32_t s_size; + int32_t s_scnptr; + int32_t s_relptr; + int32_t s_lnnoptr; + uint16_t s_nreloc; + uint16_t s_nlnno; + int32_t s_flags; +} SCNHDR; + +# define SCNHSZ sizeof(SCNHDR) + +# define STYP_REG 0x000 +# define STYP_DSECT 0x001 +# define STYP_NOLOAD 0x002 +# define STYP_GROUP 0x004 +# define STYP_PAD 0x008 +# define STYP_COPY 0x010 +# define STYP_TEXT 0x020 +# define STYP_DATA 0x040 +# define STYP_BSS 0x080 +# define STYP_INFO 0x200 +# define STYP_OVER 0x400 +# define STYP_LIB 0x800 + +typedef struct reloc +{ + uint32_t r_vaddr; + uint32_t r_symndx; + uint16_t r_type; +} RELOC; +# define RELSZ 10 + +# define R_ABS 0x000 +# define R_DIR16 0x001 +# define R_REL16 0x002 +# define R_DIR32 0x006 +# define R_PCRLONG 0x024 + +typedef struct lineno +{ + union + { + uint32_t l_symndx; + uint32_t l_paddr; + } l_addr; + uint16_t l_lnno; +} LINENO; + +# define LINESZ 6 + +#endif /* !COFF_H */ diff --git a/kernel/Makefile b/kernel/Makefile index ec451dd..4e287b6 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -1,25 +1,25 @@ -AS = fasm -RM = rm -f -INSTALL = install - -KERNEL = vmstupid.sys -SRCS = kernel.asm \ - const.inc \ - mm/mm.inc - -.PHONY: all -all: $(KERNEL) - -$(KERNEL): $(SRCS) - $(AS) kernel.asm $@ - -.PHONY: clean -clean: - $(RM) $(KERNEL) - -.PHONY: install -install: $(KERNEL) - @ mkdir -p $(DESTDIR) - install $< $(DESTDIR) - -.PHONY: all clean +AS = fasm +RM = rm -f +INSTALL = install + +KERNEL = vmstupid.sys +SRCS = kernel.asm \ + const.inc \ + mm/mm.inc + +.PHONY: all +all: $(KERNEL) + +$(KERNEL): $(SRCS) + $(AS) kernel.asm $@ + +.PHONY: clean +clean: + $(RM) $(KERNEL) + +.PHONY: install +install: $(KERNEL) + @ mkdir -p $(DESTDIR) + install $< $(DESTDIR) + +.PHONY: all clean install diff --git a/kernel/kernel.asm b/kernel/kernel.asm index 41e66ac..6888e1f 100644 --- a/kernel/kernel.asm +++ b/kernel/kernel.asm @@ -1,18 +1,18 @@ - INCLUDE 'const.inc' - - ORG KBASE - USE32 - - jmp kmain - - INCLUDE 'mm/mm.inc' - -kmain: - nop - -_edata: - - ; BSS - rb 0x4000 - -_end: + INCLUDE 'const.inc' + + ORG KBASE + USE32 + + jmp kmain + + INCLUDE 'mm/mm.inc' + +kmain: + nop + +_edata: + + ; BSS + rb 0x4000 + +_end: diff --git a/tools/Makefile b/tools/Makefile index 12feecb..41b25ca 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -1,9 +1,9 @@ CC = gcc RM = rm -f -TARGET = fat$(EXEXT) +TARGET = fat$(EXEXT) coff-ld$(EXEXT) -CFLAGS = -DMK_COMMIT="$(MK_COMMIT)" -DMK_BUGREPORT="$(MK_BUGREPORT)" +CFLAGS = -DMK_COMMIT="$(MK_COMMIT)" -DMK_BUGREPORT="$(MK_BUGREPORT)" -I../include LDFLAGS = .PHONY: all @@ -12,5 +12,8 @@ all: $(TARGET) fat$(EXEXT): fat.c $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) +coff-ld$(EXEXT): coff-ld.c + $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) + .PHONY: install install: $(TARGET) diff --git a/tools/coff-ld.c b/tools/coff-ld.c new file mode 100644 index 0000000..202476f --- /dev/null +++ b/tools/coff-ld.c @@ -0,0 +1,95 @@ +#include +#include +#include + +static char *prg_name; +static char *outfile = "a.out"; + +static void +usage(int retcode) +{ + if (retcode == EXIT_FAILURE) + { + fprintf(stderr, "Try '%s -h' form more information.\n", prg_name); + } + else + { + printf("Usage: %s [-hV] [-o outfile] [OBJS...]\n", prg_name); + printf("\t-h\tdisplay this help and exit\n"); + printf("\t-V\toutput version information\n"); + printf("\t-o outfile\tout file (default: %s)\n", outfile); + + printf("\nReport bugs to <%s>\n", MK_BUGREPORT); + } + + exit(retcode); +} + +static void +version(void) +{ + printf("%s commit %s\n", prg_name, MK_COMMIT); + exit(EXIT_SUCCESS); +} + +int +main(int argc, char **argv) +{ + FILE *fp; + FILHDR fhdr; + SCNHDR shdr; + AOUTHDR aout; + + prg_name = argv[0]; + + while ((argc > 1) && (argv[1][0] == '-')) + { + switch (argv[1][1]) + { + case 'h': + usage(EXIT_SUCCESS); + break; + case 'V': + version(); + break; + case 'o': + argv++; + argc--; + if (argc <= 1) usage(EXIT_FAILURE); + outfile = argv[1]; + break; + } + + argv++; + argc--; + } + + if (argc <= 1) usage(EXIT_FAILURE); + + fp = fopen(argv[1], "rb"); + if (fp == NULL) + { + return (EXIT_FAILURE); + } + + fread(&fhdr, 1, FILHSZ, fp); + printf("magic: 0x%hx\n", fhdr.f_magic); + printf("n section: %hd\n", fhdr.f_nscns); + printf("symtab: 0x%X\n", fhdr.f_symptr); + printf("sym entries: %d\n", fhdr.f_nsyms); + printf("optional hdr size: %hu\n", fhdr.f_opthdr); + printf("flags: 0x%hx\n", fhdr.f_flags); + + if (fhdr.f_opthdr > 0) + { + fread(&aout, 1, AOUTHSZ, fp); + } + + fread(&shdr, 1, SCNHSZ, fp); + printf("name: %c%c%c%c%c%c%c%c\n", shdr.s_name[0], shdr.s_name[1],shdr.s_name[2],shdr.s_name[3],shdr.s_name[4],shdr.s_name[5],shdr.s_name[6],shdr.s_name[7]); + printf("flags: 0x%x\n", shdr.s_flags); + + fclose(fp); + + return (EXIT_SUCCESS); +} diff --git a/tools/create-iso b/tools/create-iso index d2a1dcd..d3f42eb 100755 --- a/tools/create-iso +++ b/tools/create-iso @@ -19,9 +19,10 @@ EOF gen_iso_file() { mkdir -p "$2/boot/grub" echo "$grub_config" > "$2/boot/grub/grub.cfg" - sha256sum "$2/vmstupid.sys" > "$2/boot/hashfile" - sha256sum "$2/stpdboot.sys" >> "$2/boot/hashfile" - + (cd "$2"; + sha256sum -b -t "vmstupid.sys" > "boot/hashfile"; + sha256sum -b -t "stpdboot.sys" >> "boot/hashfile" + ) grub-file --is-x86-multiboot "$2/stpdboot.sys" || exit 1 grub-mkrescue -o $1 $2 diff --git a/tools/fat.c b/tools/fat.c index 17838fd..6ed9961 100644 --- a/tools/fat.c +++ b/tools/fat.c @@ -189,7 +189,7 @@ usage(int retcode) static void version(void) { - printf("%S commit %s\n", prg_name, MK_COMMIT); + printf("%s commit %s\n", prg_name, MK_COMMIT); exit(EXIT_SUCCESS); }