From b907d46f45c5b1cc697fec8a177176cb5c08448b Mon Sep 17 00:00:00 2001 From: Robert Pengelly Date: Wed, 16 Oct 2024 15:58:42 +0100 Subject: [PATCH] Create ELK object file --- Makefile.p32 | 2 +- Makefile.pdw | 2 +- Makefile.unix | 2 +- Makefile.w32 | 2 +- as.c | 4 ++-- obj.c => elks.c | 41 +++++++++++++++++++--------------- elks.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ obj.h | 46 -------------------------------------- 8 files changed, 88 insertions(+), 70 deletions(-) rename obj.c => elks.c (89%) create mode 100644 elks.h delete mode 100644 obj.h diff --git a/Makefile.p32 b/Makefile.p32 index 8e6b41e..60facf8 100644 --- a/Makefile.p32 +++ b/Makefile.p32 @@ -6,7 +6,7 @@ CC=gcc386 LD=ld386 COPTS=-S -O2 -fno-common -ansi -I. -I./include -I../pdos/pdpclib -I../pdos/src -D__PDOS386__ -D__32BIT__ -D__NOBIVA__ -D__PDOS__ -Wall -Werror -ansi -m32 -pedantic -COBJ=as.o bin.o cstr.o eval.o expr.o fixup.o frag.o hashtab.o intel.o kwd.o lex.o lib.o list.o listing.o ll.o macro.o obj.o process.o report.o section.o symbol.o vector.o +COBJ=as.o bin.o cstr.o elks.o eval.o expr.o fixup.o frag.o hashtab.o intel.o kwd.o lex.o lib.o list.o listing.o ll.o macro.o process.o report.o section.o symbol.o vector.o all: clean sasm.exe diff --git a/Makefile.pdw b/Makefile.pdw index 73915de..c33cb87 100644 --- a/Makefile.pdw +++ b/Makefile.pdw @@ -6,7 +6,7 @@ CC=gccwin LD=ldwin COPTS=-S -O2 -fno-common -ansi -I. -I./include -I../pdos/pdpclib -I../pdos/src -D__WIN32__ -D__NOBIVA__ -D__PDOS__ -Wall -Werror -ansi -m32 -pedantic -COBJ=as.o bin.o cstr.o eval.o expr.o fixup.o frag.o hashtab.o intel.o kwd.o lex.o lib.o list.o listing.o ll.o macro.o obj.o process.o report.o section.o symbol.o vector.o +COBJ=as.o bin.o cstr.o elks.o eval.o expr.o fixup.o frag.o hashtab.o intel.o kwd.o lex.o lib.o list.o listing.o ll.o macro.o process.o report.o section.o symbol.o vector.o all: clean sasm.exe diff --git a/Makefile.unix b/Makefile.unix index 44941e1..94b0919 100644 --- a/Makefile.unix +++ b/Makefile.unix @@ -9,7 +9,7 @@ VPATH := $(SRCDIR) CC := gcc CFLAGS := -D_FILE_OFFSET_BITS=64 -I$(OBJDIR) -I$(SRCDIR)/include -O2 -Wall -Werror -Wextra -ansi -pedantic -std=c90 -CSRC := as.c bin.c cstr.c eval.c expr.c fixup.c frag.c hashtab.c intel.c kwd.c lex.c lib.c list.c listing.c ll.c macro.c obj.c process.c report.c section.c symbol.c vector.c +CSRC := as.c bin.c cstr.c elks.c eval.c expr.c fixup.c frag.c hashtab.c intel.c kwd.c lex.c lib.c list.c listing.c ll.c macro.c process.c report.c section.c symbol.c vector.c ifeq ($(OS), Windows_NT) all: sasm.exe diff --git a/Makefile.w32 b/Makefile.w32 index c70d0cf..b3fc631 100644 --- a/Makefile.w32 +++ b/Makefile.w32 @@ -9,7 +9,7 @@ VPATH := $(SRCDIR) CC := gcc CFLAGS := -D_FILE_OFFSET_BITS=64 -I$(OBJDIR) -I$(SRCDIR)/include -O2 -Wall -Werror -Wextra -ansi -pedantic -std=c90 -CSRC := as.c bin.c cstr.c eval.c expr.c fixup.c frag.c hashtab.c intel.c kwd.c lex.c lib.c list.c listing.c ll.c macro.c obj.c process.c report.c section.c symbol.c vector.c +CSRC := as.c bin.c cstr.c elks.c eval.c expr.c fixup.c frag.c hashtab.c intel.c kwd.c lex.c lib.c list.c listing.c ll.c macro.c process.c report.c section.c symbol.c vector.c all: sasm.exe diff --git a/as.c b/as.c index 3c15e73..961c383 100644 --- a/as.c +++ b/as.c @@ -17,7 +17,7 @@ struct as_state *state = 0; const char *program_name = 0; extern void output_binary (FILE *fp); -extern void output_obj (FILE *fp); +extern void output_elks (FILE *fp); extern void keywords_init (void); extern void sections_init (void); @@ -175,7 +175,7 @@ int main (int argc, char **argv) { } - output_obj (state->ofp); + output_elks (state->ofp); if (get_error_count () > 0) { return EXIT_FAILURE; diff --git a/obj.c b/elks.c similarity index 89% rename from obj.c rename to elks.c index e9eae02..3f60bb9 100644 --- a/obj.c +++ b/elks.c @@ -1,14 +1,14 @@ /****************************************************************************** - * @file obj.c + * @file elks.c *****************************************************************************/ #include #include #include #include "as.h" +#include "elks.h" #include "fixup.h" #include "frag.h" -#include "obj.h" #include "report.h" #include "section.h" #include "symbol.h" @@ -25,7 +25,7 @@ static void write_to_byte_array (unsigned char *arr, unsigned long value, int si static int output_relocation (struct fixup *fixup, unsigned long start_address_of_section, FILE *fp) { - struct relocation_info reloc; + struct elks_relocation_info reloc; long log2_of_size, size; unsigned long r_symbolnum; @@ -35,11 +35,11 @@ static int output_relocation (struct fixup *fixup, unsigned long start_address_o if (symbol_is_section_symbol (fixup->add_symbol)) { if (symbol_get_section (fixup->add_symbol) == text_section) { - r_symbolnum = N_TEXT; + r_symbolnum = ELKS_N_TEXT; } else if (symbol_get_section (fixup->add_symbol) == data_section) { - r_symbolnum = N_DATA; + r_symbolnum = ELKS_N_DATA; } else if (symbol_get_section (fixup->add_symbol) == bss_section) { - r_symbolnum = N_BSS; + r_symbolnum = ELKS_N_BSS; } else { report_at (__FILE__, __LINE__, REPORT_INTERNAL_ERROR, "invalid section %s", section_get_name (symbol_get_section (fixup->add_symbol))); @@ -89,7 +89,7 @@ static int output_relocation (struct fixup *fixup, unsigned long start_address_o } -void output_obj (FILE *fp) { +void output_elks (FILE *fp) { unsigned long start_address_of_data; struct fixup *fixup; @@ -103,10 +103,15 @@ void output_obj (FILE *fp) { unsigned long text_size, data_size, bss_size; unsigned long tr_size, dr_size; - struct exec header; + struct elks_exec header; memset (&header, 0, sizeof (header)); - write_to_byte_array (header.a_info, 0x00640000 | MAGIC, 4); + header.a_magic[0] = ((ELKS_MAGIC >> 8) & 0xff); + header.a_magic[1] = (ELKS_MAGIC & 0xff); + + header.a_flags = 0x10; + header.a_cpu = 0x04; + header.a_hdrlen = sizeof (header); if ((symbol = state->end_symbol)) { write_to_byte_array (header.a_entry, symbol_get_value (symbol), 4); @@ -193,7 +198,7 @@ void output_obj (FILE *fp) { return; } - tr_size += sizeof (struct relocation_info); + tr_size += sizeof (struct elks_relocation_info); } @@ -214,7 +219,7 @@ void output_obj (FILE *fp) { return; } - dr_size += sizeof (struct relocation_info); + dr_size += sizeof (struct elks_relocation_info); } @@ -227,7 +232,7 @@ void output_obj (FILE *fp) { if (symbol_is_external (symbol) || symbol_is_undefined (symbol)) { - struct nlist symbol_entry; + struct elks_nlist symbol_entry; memset (&symbol_entry, 0, sizeof (symbol_entry)); write_to_byte_array (symbol_entry.n_strx, string_table_pos, 4); @@ -238,15 +243,15 @@ void output_obj (FILE *fp) { } if (symbol->section == undefined_section) { - symbol_entry.n_type = N_UNDF; + symbol_entry.n_type = ELKS_N_UNDF; } else if (symbol->section == text_section) { - symbol_entry.n_type = N_TEXT; + symbol_entry.n_type = ELKS_N_TEXT; } else if (symbol->section == data_section) { - symbol_entry.n_type = N_DATA; + symbol_entry.n_type = ELKS_N_DATA; } else if (symbol->section == bss_section) { - symbol_entry.n_type = N_BSS; + symbol_entry.n_type = ELKS_N_BSS; } else if (symbol->section == absolute_section) { - symbol_entry.n_type = N_ABS; + symbol_entry.n_type = ELKS_N_ABS; } else { report_at (__FILE__, __LINE__, REPORT_INTERNAL_ERROR, "invalid section %s", section_get_name (symbol->section)); @@ -255,7 +260,7 @@ void output_obj (FILE *fp) { } write_to_byte_array (symbol_entry.n_value, symbol_get_value (symbol), 4); - symbol_entry.n_type |= N_EXT; + symbol_entry.n_type |= ELKS_N_EXT; if (fwrite (&symbol_entry, sizeof (symbol_entry), 1, fp) != 1) { diff --git a/elks.h b/elks.h new file mode 100644 index 0000000..e9d395f --- /dev/null +++ b/elks.h @@ -0,0 +1,59 @@ +/****************************************************************************** + * @file elks.h + *****************************************************************************/ +#ifndef _ELKS_H +#define _ELKS_H + +struct elks_exec { + + unsigned char a_magic[2]; + unsigned char a_flags; + unsigned char a_cpu; + unsigned char a_hdrlen; + unsigned char a_unused; + unsigned char a_version[2]; + + unsigned char a_text[4]; + unsigned char a_data[4]; + unsigned char a_bss[4]; + unsigned char a_entry[4]; + unsigned char a_total[4]; + unsigned char a_syms[4]; + + unsigned char a_trsize[4]; + unsigned char a_drsize[4]; + unsigned char a_trbase[4]; + unsigned char a_drbase[4]; + +}; + +#define ELKS_MAGIC 0403 + +struct elks_relocation_info { + + unsigned char r_address[4]; + unsigned char r_symbolnum[4]; + +}; + +#define ELKS_N_UNDF 0x00 +#define ELKS_N_ABS 0x02 +#define ELKS_N_TEXT 0x04 +#define ELKS_N_DATA 0x06 +#define ELKS_N_BSS 0x08 + +struct elks_nlist { + + unsigned char n_strx[4]; + unsigned char n_type; + + unsigned char n_other; + unsigned char n_desc[2]; + + unsigned char n_value[4]; + +}; + +#define ELKS_N_EXT 0x01 + +#endif /* _ELKS_H */ diff --git a/obj.h b/obj.h deleted file mode 100644 index a92812e..0000000 --- a/obj.h +++ /dev/null @@ -1,46 +0,0 @@ -/****************************************************************************** - * @file obj.h - *****************************************************************************/ -#ifndef _OBJ_H -#define _OBJ_H - -struct exec { - - unsigned char a_info[4]; - unsigned char a_text[4]; - unsigned char a_data[4]; - unsigned char a_bss[4]; - unsigned char a_syms[4]; - unsigned char a_entry[4]; - unsigned char a_trsize[4]; - unsigned char a_drsize[4]; - -}; - -#define MAGIC 0471 - -struct relocation_info { - - unsigned char r_address[4]; - unsigned char r_symbolnum[4]; - -}; - -#define N_UNDF 0x00 -#define N_ABS 0x02 -#define N_TEXT 0x04 -#define N_DATA 0x06 -#define N_BSS 0x08 - -struct nlist { - - unsigned char n_strx[4]; - unsigned char n_type; - - unsigned char n_value[4]; - -}; - -#define N_EXT 0x01 - -#endif /* _OBJ_H */ -- 2.34.1