mips/masm/asm.h

242 lines
4.7 KiB
C
Raw Normal View History

/* Copyright (c) 2024 Freya Murphy */
#ifndef __ASM_H__
#define __ASM_H__
#include <stddef.h>
2024-09-11 16:06:09 +00:00
#include <elf.h>
#include <mips.h>
#include "mlimits.h"
2024-09-10 00:48:08 +00:00
#include "parse.h"
2024-09-11 16:06:09 +00:00
#include "lex.h"
2024-09-10 00:48:08 +00:00
2024-09-11 16:06:09 +00:00
///
/// ELF string table
///
2024-09-10 22:23:46 +00:00
2024-09-11 16:06:09 +00:00
struct str_table {
// size of the ptr in bytes
size_t size;
2024-09-10 22:23:46 +00:00
2024-09-11 16:06:09 +00:00
// pointer that contains
// the strings
char *ptr;
2024-09-10 22:23:46 +00:00
};
2024-09-11 16:06:09 +00:00
/* 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
///
2024-09-10 22:23:46 +00:00
struct symbol_table {
2024-09-11 16:06:09 +00:00
// 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;
2024-09-10 22:23:46 +00:00
};
2024-09-11 16:06:09 +00:00
/* 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);
2024-09-10 22:23:46 +00:00
2024-09-11 16:06:09 +00:00
/* find a symbol by name in the symbol table */
int symtab_find(struct symbol_table *symtab, Elf32_Sym **sym, size_t *idx,
2024-09-10 22:23:46 +00:00
const char name[MAX_LEX_LENGTH]);
2024-09-11 16:06:09 +00:00
///
/// ELF relocation table
///
2024-09-10 22:23:46 +00:00
2024-09-11 16:06:09 +00:00
struct relocation_table {
size_t len;
2024-09-10 00:48:08 +00:00
size_t size;
2024-09-11 16:06:09 +00:00
Elf32_Rela *data;
};
2024-09-11 16:06:09 +00:00
/* initalize a relocation table */
int reltab_init(struct relocation_table *reltab);
2024-09-11 16:06:09 +00:00
/* free the relocation table */
void reltab_free(struct relocation_table *reltab);
2024-09-11 16:06:09 +00:00
/* add a entry to the relocation table */
int reltab_push(struct relocation_table *reltab, const Elf32_Rela rel);
2024-09-11 16:06:09 +00:00
///
/// section entry
///
enum section_entry_type {
ENT_INS,
ENT_WORD,
ENT_HALF,
ENT_BYTE,
ENT_STR,
2024-09-11 16:06:09 +00:00
ENT_NO_DATA,
2024-09-10 00:48:08 +00:00
};
2024-09-11 16:06:09 +00:00
/* 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
2024-09-12 12:37:46 +00:00
uint32_t ins;
char str[MAX_LEX_LENGTH];
2024-09-11 16:06:09 +00:00
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 {
2024-09-10 22:23:46 +00:00
// the token lexer
2024-09-10 00:48:08 +00:00
struct lexer lexer;
2024-09-10 22:23:46 +00:00
// the expression parser
2024-09-10 00:48:08 +00:00
struct parser parser;
2024-09-11 16:06:09 +00:00
/// 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;
2024-09-10 22:23:46 +00:00
uint32_t phdr_len;
2024-09-11 16:06:09 +00:00
/// section header
Elf32_Shdr *shdr;
2024-09-10 22:23:46 +00:00
uint32_t shdr_len;
};
2024-09-11 16:06:09 +00:00
/* defines arguments to the assembler */
2024-09-10 22:23:46 +00:00
struct assembler_arguments {
char *in_file;
char *out_file;
};
2024-09-11 16:06:09 +00:00
/* initalize the assembler */
2024-09-10 22:23:46 +00:00
int assembler_init(struct assembler *assembler, const char *path);
2024-09-11 16:06:09 +00:00
/* free the assembler */
2024-09-10 22:23:46 +00:00
void assembler_free(struct assembler *assembler);
2024-09-11 16:06:09 +00:00
/* assemble a file */
2024-09-10 22:23:46 +00:00
int assemble_file(struct assembler_arguments args);
2024-09-10 00:48:08 +00:00
#endif /* __ASM_H__ */