mips/masm/asm.h

100 lines
1.8 KiB
C
Raw Normal View History

/* Copyright (c) 2024 Freya Murphy */
#ifndef __ASM_H__
#define __ASM_H__
2024-09-11 16:06:09 +00:00
#include <elf.h>
2024-10-04 23:41:10 +00:00
#include "gen.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-10-04 23:41:10 +00:00
struct elf_str_table {
2024-09-11 16:06:09 +00:00
// 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 */
2024-10-04 23:41:10 +00:00
int strtab_init(struct elf_str_table *strtab);
2024-09-11 16:06:09 +00:00
/* free a string table */
2024-10-04 23:41:10 +00:00
void strtab_free(struct elf_str_table *strtab);
2024-09-11 16:06:09 +00:00
/* get a string form the string table */
2024-10-04 23:41:10 +00:00
int strtab_get_str(struct elf_str_table *strtab, const char *str, size_t *res);
2024-09-11 16:06:09 +00:00
/* get or append a string into the string table */
2024-10-04 23:41:10 +00:00
int strtab_write_str(struct elf_str_table *strtab, const char *str, size_t *res);
2024-09-11 16:06:09 +00:00
///
2024-10-04 23:41:10 +00:00
/// elf section
2024-09-11 16:06:09 +00:00
///
/* holds a section of the asm file (i.e. .text, .bss, .data) */
2024-10-04 23:41:10 +00:00
struct elf_section {
// section data *weak* pointer
struct section *data;
2024-09-11 16:06:09 +00:00
// index of the section in
// the ELF shdr
size_t shdr_idx;
2024-10-04 23:41:10 +00:00
// relocation table
2024-09-11 16:06:09 +00:00
size_t reltab_shidx;
2024-10-04 23:41:10 +00:00
uint32_t reltab_len;
Elf32_Rel *reltab;
2024-09-11 16:06:09 +00:00
};
///
/// assembler
///
struct assembler {
2024-10-04 23:41:10 +00:00
// the code generator
struct generator gen;
2024-09-10 00:48:08 +00:00
2024-10-04 23:41:10 +00:00
/// symbol table
2024-09-11 16:06:09 +00:00
size_t symtab_shidx;
2024-10-04 23:41:10 +00:00
size_t symtab_len;
Elf32_Sym *symbols;
// sh string table
2024-09-11 16:06:09 +00:00
size_t strtab_shidx;
2024-10-04 23:41:10 +00:00
struct elf_str_table strtab;
2024-09-11 16:06:09 +00:00
2024-10-04 23:41:10 +00:00
// string table
size_t shstrtab_shidx;
struct elf_str_table shstrtab;
2024-09-11 16:06:09 +00:00
2024-10-04 23:41:10 +00:00
/// sections
uint32_t section_len;
struct elf_section *sections;
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__ */