feat: readcoff

This commit is contained in:
d0p1 🏳️‍⚧️ 2024-06-10 08:43:35 +02:00
parent eb24cb88c2
commit 164c1de23f
8 changed files with 204 additions and 11 deletions

View file

@ -15,9 +15,12 @@ RM = rm -f
MK_BUGREPORT := \"https://git.cute.engineering/d0p1/StupidOS/issues\"
MK_COMMIT := \"$(shell git rev-parse --short HEAD)\"
MK_PACKAGE := \"StupidOS\"
CFLAGS = -DMK_COMMIT="$(MK_COMMIT)" -DMK_BUGREPORT="$(MK_BUGREPORT)" -I$(TOPDIR)include
CFLAGS = -DMK_COMMIT="$(MK_COMMIT)" \
-DMK_BUGREPORT="$(MK_BUGREPORT)" \
-DMK_PACKAGE="$(MK_PACKAGE)" \
-I$(TOPDIR)include
LDFLAGS =

175
bin/readcoff/main.c Normal file
View file

@ -0,0 +1,175 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#ifndef __stupidos__
# include <libgen.h>
#endif
#include <coff.h>
static const struct {
int magic;
char *machine;
} MACHINES[] = {
{F_MACH_AM33, "Matsushita AM33"},
{F_MACH_AMD64, "Intel amd64"},
{F_MACH_ARM, "ARM"},
{F_MACH_ARMNT, "ARM Thumb-2"},
{F_MACH_EBC, "EFI byte code"},
{F_MACH_I386, "Intel 80386"},
{F_MACH_IA64, "IA64"},
{F_MACH_M32R, "Mitsubishi M32R"},
/* TODO */
{F_MACH_UNKNOWN, "unknown"}
};
/* */
static const char *prg_name;
static const char *mach = NULL;
/* */
static int display_header = 0;
static int display_program_header = 0;
static int display_sections = 0;
static void
usage(int retval)
{
if (retval == EXIT_FAILURE)
{
fprintf(stderr, "Try '%s -h' for more informations.", prg_name);
}
else
{
printf("Usage: %s [OPTIONS...] [FILES]\n", prg_name);
printf("Options are:\n");
printf("\t-a\tEquivalent to: -H -l -S -s -r -d -v -A -I\n");
printf("\t-H\tDisplay the COFF file header\n");
}
exit(retval);
}
static void
version(void)
{
printf("%s (%s) %s", prg_name, MK_PACKAGE, MK_COMMIT);
exit(EXIT_SUCCESS);
}
int
main(int argc, char **argv)
{
FILHDR file_header;
AOUTHDR aout_header;
SCNHDR section_header;
char name[9];
char *type;
FILE *fp;
int idx;
time_t timedat;
#ifndef __stupidos__
prg_name = basename(argv[0]);
#else
prg_name = argv[0];
#endif
while ((argc > 1) && (argv[1][0] == '-'))
{
switch (argv[1][1])
{
case 'a':
display_header = 1;
display_program_header = 1;
display_sections = 1;
break;
case 'H':
display_header = 1;
break;
case 'h':
usage(EXIT_SUCCESS);
break;
case 'V':
version();
break;
}
argv++;
argc--;
}
if (argc <= 1) usage(EXIT_FAILURE);
fp = fopen(argv[1], "rb");
if (fp == NULL)
{
return (EXIT_FAILURE);
}
if (display_header)
{
if (fread(&file_header, 1, FILHSZ, fp) != FILHSZ)
{
return (EXIT_FAILURE);
}
for (idx = 0; MACHINES[idx].magic != F_MACH_UNKNOWN; idx++)
{
if (MACHINES[idx].magic == file_header.f_magic)
{
mach = MACHINES[idx].machine;
break;
}
}
if (mach == NULL) return (EXIT_FAILURE);
printf("COFF Header:\n");
printf(" Machine: %s\n", mach);
printf(" Number of section headers: %hd\n", file_header.f_nscns);
timedat = file_header.f_timdat;
printf(" Created at: %s", ctime(&timedat));
printf(" Start of Symbol Table: %d (bytes into file)\n", file_header.f_symptr);
printf(" Number of Symbols: %d\n", file_header.f_nsyms);
printf(" Size of optional header: %hu\n", file_header.f_opthdr);
printf(" Flags: 0x%hx\n\n", file_header.f_flags);
}
if (display_sections)
{
fseek(fp, file_header.f_opthdr + FILHSZ, SEEK_SET);
printf("Section Headers:\n");
printf("[Nr] Name Type Address Offset\n");
printf(" Size\n");
for (idx = 0; idx < file_header.f_nscns; idx++)
{
if (fread(&section_header, 1, SCNHSZ, fp) != SCNHSZ)
{
return (EXIT_FAILURE);
}
memset(name, 0, 9);
memcpy(name, section_header.s_name, 8);
if (section_header.s_flags & STYP_TEXT)
{
type = "TEXT";
} else if (section_header.s_flags & STYP_DATA)
{
type = "DATA";
}
else if (section_header.s_flags & STYP_BSS)
{
type = "BSS";
}
else
{
type = "???";
}
printf("[%2d] %-8s %-8s %08X %08X\n", idx, name, type, section_header.s_vaddr, section_header.s_scnptr);
printf(" %08X\n", section_header.s_size);
}
}
return (EXIT_SUCCESS);
}

View file

@ -69,3 +69,13 @@ About: File Header
| uint32_t | Number of entries in the symtab |
| uint16_t | Optional header size |
| uint16_t | Flags |
About: Optional Header
About: Section Header
About: Usefull links
- <https://wiki.osdev.org/COFF>
- <http://www.delorie.com/djgpp/doc/coff/filhdr.html>
- <Official Microsoft COFF Specification (2016) at https://web.archive.org/web/20230703150217/https%3A%2F%2Fdownload.microsoft.com%2Fdownload%2F9%2Fc%2F5%2F9c5b2167-8017-4bae-9fde-d599bac8184a%2Fpecoff_v83.docx>

View file

@ -6,7 +6,6 @@ The "Hungarian" notation conventions MUST be used.
Comments:
TO BE ADDED
About: C

View file

@ -20,7 +20,7 @@ Timestamp: Updated yyyy/mm/dd
# These are indexes you deleted, so Natural Docs will not add them again
# unless you remove them from this line.
Don't Index: Variables, Macros, Classes
Don't Index: Variables, Classes, Macros
# --------------------------------------------------------------------------
@ -48,6 +48,7 @@ Link: Source Code (https://git.cute.engineering/d0p1/StupidOS)
File: Coding Style (docs/coding-style.txt)
File: FAQ (docs/faq.txt)
Link: StupidFS (https://stupidfs.d0p1.eu/)
File: Common Object File Format &lparen;COFF&rparen; (docs/COFF.txt)
Group: BootLoader {
@ -56,11 +57,13 @@ Group: BootLoader {
Group: BootSector {
File: floppy.asm (boot/bootsect/floppy.asm)
File: hdd.asm (boot/bootsect/hdd.asm)
} # Group: BootSector
Group: Loader {
File: loader.asm (boot/loader/loader.asm)
File: logger.inc (boot/loader/logger.inc)
File: memory.inc (boot/loader/memory.inc)
} # Group: Loader
@ -81,6 +84,7 @@ Group: BootLoader {
Group: Kernel {
File: kernel memory map (kernel/intro.txt)
File: kernel.asm (kernel/kernel.asm)
} # Group: Kernel
@ -93,6 +97,8 @@ Group: Lib {
Group: Crypto {
File: rc4.asm (no auto-title, lib/crypto/rc4/rc4.asm)
Group: chacha20 {
File: chacha20.asm (lib/crypto/chacha20/chacha20.asm)
@ -110,6 +116,7 @@ Group: Lib {
} # Group: Crypto
File: ctype (lib/c/ctype.asm)
} # Group: Lib
Group: Index {
@ -118,6 +125,5 @@ Group: Index {
Constant Index: Constants
File Index: Files
Function Index: Functions
Property Index: Properties
} # Group: Index

View file

@ -1,4 +1,4 @@
TARGET = fat$(EXEXT) coff-ld$(EXEXT) parted$(EXEXT) mkfs.stpd$(EXEXT)
TARGET = fat$(EXEXT) coff-ld$(EXEXT) parted$(EXEXT) mkfs.stpd$(EXEXT) readcoff$(EXEXT)
.PHONY: all
all: $(TARGET)
@ -6,15 +6,14 @@ all: $(TARGET)
fat$(EXEXT): fat.c
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
coff-ld$(EXEXT): coff-ld.c
coff-ld$(EXEXT): ../bin/ld/main.c
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
readcoff$(EXEXT): ../bin/readcoff/main.c
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
parted$(EXEXT): ../sbin/parted/main.c
ifneq (,$(findstring cl,$(CC)))
$(CC) /Fo:$@ $^ $(CFLAGS) $(LDFLAGS)
else
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
endif
mkfs.stpd$(EXEXT): ../sbin/mkfs.stpd/main.c
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)

1
tools/coff2elf/main.c Normal file
View file

@ -0,0 +1 @@
#include <coff.h>