chore: add 'coff.h' header
Some checks are pending
Build / test (push) Waiting to run
Docs / test (push) Waiting to run

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-03-20 16:51:27 +01:00
parent 9aaad60e6e
commit 03c95cb0a4
14 changed files with 282 additions and 57 deletions

1
.gitignore vendored
View file

@ -13,6 +13,7 @@ vmstupid
vmstupid-dbg vmstupid-dbg
/tools/fat /tools/fat
/tools/coff-ld
/sysroot /sysroot
log.txt log.txt

View file

@ -9,7 +9,7 @@ RM = echo
MK_BUGREPORT := \"https://git.cute.engineering/d0p1/StupidOS/issues\" MK_BUGREPORT := \"https://git.cute.engineering/d0p1/StupidOS/issues\"
MK_COMMIT := \"$(shell git rev-parse --short HEAD)\" 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 TARGET = stupid.tar.gz floppy_boot.img
ifneq ($(OS),Windows_NT) ifneq ($(OS),Windows_NT)

View file

@ -2,6 +2,8 @@ SUBDIRS = cmd
TOPGOALS = all clean install TOPGOALS = all clean install
SUBDIRS = cmd
.PHONY: $(SUBDIRS) .PHONY: $(SUBDIRS)
$(SUBDIRS): $(SUBDIRS):
@echo "📁 bin/$@" @echo "📁 bin/$@"

View file

@ -1,5 +1,6 @@
all: all:
fasm cmd.asm
clean: clean:
install: install: all

View file

@ -1,8 +1,11 @@
format ELF format COFF
entry start
include 'builtins.inc' section '.text' code
start:
public main
main:
int 0x2A int 0x2A
section '.data' data
INCLUDE 'builtins.inc'

BIN
bin/cmd/cmd.obj Normal file

Binary file not shown.

16
include/Makefile Normal file
View file

@ -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

103
include/coff.h Normal file
View file

@ -0,0 +1,103 @@
#ifndef COFF_H
# define COFF_H 1
# include <stdint.h>
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 */

View file

@ -22,4 +22,4 @@ install: $(KERNEL)
@ mkdir -p $(DESTDIR) @ mkdir -p $(DESTDIR)
install $< $(DESTDIR) install $< $(DESTDIR)
.PHONY: all clean .PHONY: all clean install

View file

@ -1,9 +1,9 @@
CC = gcc CC = gcc
RM = rm -f 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 = LDFLAGS =
.PHONY: all .PHONY: all
@ -12,5 +12,8 @@ all: $(TARGET)
fat$(EXEXT): fat.c fat$(EXEXT): fat.c
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) $(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
coff-ld$(EXEXT): coff-ld.c
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
.PHONY: install .PHONY: install
install: $(TARGET) install: $(TARGET)

95
tools/coff-ld.c Normal file
View file

@ -0,0 +1,95 @@
#include <stdlib.h>
#include <stdio.h>
#include <coff.h>
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);
}

View file

@ -19,9 +19,10 @@ EOF
gen_iso_file() { gen_iso_file() {
mkdir -p "$2/boot/grub" mkdir -p "$2/boot/grub"
echo "$grub_config" > "$2/boot/grub/grub.cfg" echo "$grub_config" > "$2/boot/grub/grub.cfg"
sha256sum "$2/vmstupid.sys" > "$2/boot/hashfile" (cd "$2";
sha256sum "$2/stpdboot.sys" >> "$2/boot/hashfile" 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-file --is-x86-multiboot "$2/stpdboot.sys" || exit 1
grub-mkrescue -o $1 $2 grub-mkrescue -o $1 $2

View file

@ -189,7 +189,7 @@ usage(int retcode)
static void static void
version(void) version(void)
{ {
printf("%S commit %s\n", prg_name, MK_COMMIT); printf("%s commit %s\n", prg_name, MK_COMMIT);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }