From e3d2e31377030e84066bed4bbc04bf6c56206305 Mon Sep 17 00:00:00 2001 From: Freya Murphy Date: Wed, 11 Sep 2024 12:06:09 -0400 Subject: refactor --- masm/asm.h | 244 +++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 189 insertions(+), 55 deletions(-) (limited to 'masm/asm.h') diff --git a/masm/asm.h b/masm/asm.h index 86f6b9a..c8a6394 100644 --- a/masm/asm.h +++ b/masm/asm.h @@ -4,101 +4,235 @@ #define __ASM_H__ #include +#include +#include -#include "lex.h" #include "parse.h" +#include "lex.h" -enum symbol_flag { - SYM_LOCAL, - SYM_GLOBAL, - SYM_EXTERNAL, -}; +/// +/// ELF string table +/// -struct symbol { - char name[MAX_LEX_LENGTH]; - uint32_t index; - struct section *sec; - enum symbol_flag flag; +struct str_table { + // size of the ptr in bytes + size_t size; + // pointer that contains + // the strings + char *ptr; }; +/* initalize a string table */ +int strtab_init(struct str_table *strtab); + +/* free a string table */ +void strtab_free(struct str_table *strtab); + +/* get a string form the string table */ +int strtab_get_str(struct str_table *strtab, const char *str, size_t *res); + +/* get or append a string into the string table */ +int strtab_write_str(struct str_table *strtab, const char *str, size_t *res); + + +/// +/// ELF symbol table +/// + struct symbol_table { - uint32_t count; - uint32_t len; - struct symbol *symbols; + // length in size in sym ammt + size_t len; + size_t size; + + // the Elf symbols + Elf32_Sym *symbols; + + // keeps track of what section each ELF symbol is in + // *!!this is NOT the section header index in the ELF ehdr!!* + ssize_t *sections; + + // symbols reference a string table that acutally + // holds the strings + // + // *weak* ptr, we do not own this!!! + struct str_table *strtab; + }; -int symtbl_init(struct symbol_table *sym_tbl); -void symtbl_free(struct symbol_table *sym_tbl); +/* initalize a symbol table */ +int symtab_init(struct symbol_table *symtab); + +/* free the symbol table */ +void symtab_free(struct symbol_table *symtab); + +/* add a symbol to the symbol tbl */ +int symtab_push(struct symbol_table *symtab, const Elf32_Sym sym, + ssize_t sec_idx); -int symtbl_push(struct symbol_table *sym_tbl, struct symbol sym); -int symtbl_find(struct symbol_table *sym_tbl, struct symbol **sym, +/* find a symbol by name in the symbol table */ +int symtab_find(struct symbol_table *symtab, Elf32_Sym **sym, size_t *idx, const char name[MAX_LEX_LENGTH]); +/// +/// ELF relocation table +/// -struct str_table { - char *ptr; +struct relocation_table { + size_t len; size_t size; + Elf32_Rela *data; }; -/* initalize a string table */ -int strtbl_init(struct str_table *str_tbl); +/* initalize a relocation table */ +int reltab_init(struct relocation_table *reltab); -/* free a string table */ -void strtbl_free(struct str_table *str_tbl); +/* free the relocation table */ +void reltab_free(struct relocation_table *reltab); -/* get a string form the string table */ -int strtbl_get_str(struct str_table *str_tbl, const char *str, size_t *res); +/* add a entry to the relocation table */ +int reltab_push(struct relocation_table *reltab, const Elf32_Rela rel); -/* get or append a string into the string table */ -int strtbl_write_str(struct str_table *str_tbl, const char *str, size_t *res); - -struct section_meta { - void *reltbl; - uint32_t reltbl_len; - uint32_t reltbl_idx; // reltbl idx in shdr - uint32_t shdr_idx; // sec idx in shdr - uint32_t v_addr; +/// +/// section entry +/// + +enum section_entry_type { + ENT_INS, + ENT_WORD, + ENT_HALF, + ENT_BYTE, + ENT_NO_DATA, }; +/* holds a entry inside the section, i.e. a instruction, raw data, + * special directives */ +struct section_entry { + size_t size; + enum section_entry_type type; + + union { + // to get memory address + char data; + + // data + struct mips_instruction ins; + int32_t word; + int16_t half; + int8_t byte; + }; +}; + +/// +/// section +/// + +/* holds a section of the asm file (i.e. .text, .bss, .data) */ +struct section { + // length and size of amount of entries + size_t len; + size_t size; + struct section_entry *entries; + + // section name + char name[MAX_LEX_LENGTH]; + + // index of the section in + // all the sections + size_t index; + + // index of the sectio in + // the ELF shdr + size_t shdr_idx; + + // ELF section data + bool read; + bool write; + bool execute; + uint16_t alignment; + + // ELF tables + size_t reltab_shidx; + struct relocation_table reltab; +}; + +/* get the size of the section in bytes */ +size_t sec_size(struct section *section); + +/* get the index of a entry in bytes */ +size_t sec_index(struct section *section, size_t index); + +/* add a section entry to the section */ +int sec_push(struct section *section, struct section_entry entry); + +/* holds eachs section */ +struct section_table { + // length and size of amount of sections + size_t len; + size_t size; + struct section *sections; + + // the current section + struct section *current; +}; + +/* initalize the section table */ +int sectab_init(struct section_table *sec_tbl); + +/* free the section table */ +void sectab_free(struct section_table *sec_tbl); + +/* create a new section in the section table */ +int sectab_alloc(struct section_table *sec_tbl, struct section **sec, + const char name[MAX_LEX_LENGTH]); + +/* get a section by name from the section table */ +int sectab_get(struct section_table *sec_tbl, struct section **sec, + const char name[MAX_LEX_LENGTH]); + +/// +/// assembler +/// + struct assembler { // the token lexer struct lexer lexer; // the expression parser struct parser parser; - // shdr indexes - struct section_meta *meta; - size_t shstrtbl_idx; - size_t strtbl_idx; - size_t symtab_idx; - - // symbols and strings - struct symbol_table sym_tbl; - struct str_table shstr_tbl; - struct str_table str_tbl; - - // elf data - void *phdr; // void* since could be Elf32 or Elf64 - void *shdr; - void *symtab; + /// ELF tables + size_t symtab_shidx; + struct symbol_table symtab; + size_t strtab_shidx; + struct str_table strtab; + size_t shstrtab_shidx; + struct str_table shstrtab; + + /// Segments + struct section_table sectab; + + /// program header + Elf32_Phdr *phdr; uint32_t phdr_len; + + /// section header + Elf32_Shdr *shdr; uint32_t shdr_len; - uint32_t symtab_len; }; +/* defines arguments to the assembler */ struct assembler_arguments { char *in_file; char *out_file; - enum mips_isa isa; }; +/* initalize the assembler */ int assembler_init(struct assembler *assembler, const char *path); + +/* free the assembler */ void assembler_free(struct assembler *assembler); +/* assemble a file */ int assemble_file(struct assembler_arguments args); -/* assemble a mips32 file*/ -int assemble_file_mips32(struct assembler_arguments args); - #endif /* __ASM_H__ */ -- cgit v1.2.3-freya