summaryrefslogtreecommitdiff
path: root/masm/sectbl.c
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-09-09 20:48:08 -0400
committerFreya Murphy <freya@freyacat.org>2024-09-09 20:48:08 -0400
commit0ff948af3d65150f44c9fe801ada806ce0637fe1 (patch)
treeba766d33d12bf1063660c2144af2d5f5c36b4c87 /masm/sectbl.c
parenti forgor syscall (diff)
downloadmips-0ff948af3d65150f44c9fe801ada806ce0637fe1.tar.gz
mips-0ff948af3d65150f44c9fe801ada806ce0637fe1.tar.bz2
mips-0ff948af3d65150f44c9fe801ada806ce0637fe1.zip
relocation table hell
Diffstat (limited to 'masm/sectbl.c')
-rw-r--r--masm/sectbl.c112
1 files changed, 84 insertions, 28 deletions
diff --git a/masm/sectbl.c b/masm/sectbl.c
index f568a6f..6eafc60 100644
--- a/masm/sectbl.c
+++ b/masm/sectbl.c
@@ -13,13 +13,12 @@ int sectbl_init(struct section_table *sec_tbl)
{
sec_tbl->len = SECTBL_INIT_LEN;
sec_tbl->count = 0;
- sec_tbl->total_ins = 0;
- sec_tbl->sections = malloc(sizeof(struct section) * SECTBL_INIT_LEN);
+ sec_tbl->sections = malloc(sizeof(struct section) * SECTBL_INIT_LEN);
- if (sec_tbl->sections == NULL) {
- ERROR("cannot alloc");
- return M_ERROR;
- }
+ if (sec_tbl->sections == NULL) {
+ ERROR("cannot alloc");
+ return M_ERROR;
+ }
if (sectbl_alloc(sec_tbl, &sec_tbl->current, inital_section))
return M_ERROR;
@@ -30,11 +29,26 @@ int sectbl_init(struct section_table *sec_tbl)
void sectbl_free(struct section_table *sec_tbl)
{
for (uint32_t i = 0; i < sec_tbl->count; i++) {
- free(sec_tbl->sections[i].ins);
+ free(sec_tbl->sections[i].entries);
}
free(sec_tbl->sections);
}
+struct section_settings {
+ const char *name;
+ bool read;
+ bool write;
+ bool execute;
+ uint32_t align;
+};
+
+static struct section_settings default_section_settings[] = {
+ {"data", true, true, false, 1},
+ {"bss", true, true, false, 1},
+ {"rodata", true, false, false, 1},
+ {"text", true, false, true, 4},
+};
+
int sectbl_alloc(struct section_table *sec_tbl, struct section **sec,
const char name[MAX_LEX_LENGTH])
{
@@ -50,54 +64,96 @@ int sectbl_alloc(struct section_table *sec_tbl, struct section **sec,
}
struct section *temp;
- temp = &sec_tbl->sections[sec_tbl->count++];
+ temp = &sec_tbl->sections[sec_tbl->count];
strcpy(temp->name,name);
temp->count = 0;
temp->len = SECTBL_INIT_LEN;
- temp->start = sec_tbl->total_ins;
temp->alignment = 1;
- temp->ins = malloc(sizeof(union mips_instruction) * SECTBL_INIT_LEN);
+ temp->read = true;
+ temp->write = true;
+ temp->execute = false;
+ temp->index = sec_tbl->count;
+ temp->entries = malloc(sizeof(struct section_entry) * SECTBL_INIT_LEN);
- if (temp->ins == NULL) {
+ for (int i = 0; i < 4; i++) {
+ struct section_settings *set = &default_section_settings[i];
+ if (strcmp(set->name, name) == 0) {
+ temp->read = set->read;
+ temp->write = set->write;
+ temp->execute = set->execute;
+ temp->alignment = set->align;
+ break;
+ }
+ }
+
+ if (temp->entries == NULL) {
ERROR("cannot alloc");
return M_ERROR;
}
+ sec_tbl->count++;
+
*sec = temp;
return M_SUCCESS;
}
-int sectbl_push(struct section_table *sec_tbl, struct section *section,
- union mips_instruction ins)
+int sectbl_get(struct section_table *sec_tbl, struct section **sec,
+ const char name[MAX_LEX_LENGTH])
+{
+ for (uint32_t i = 0; i < sec_tbl->count; i++) {
+ struct section *temp = &sec_tbl->sections[i];
+ if (strcmp(name, temp->name) == 0) {
+ if (sec != NULL)
+ *sec = temp;
+ return M_SUCCESS;
+ }
+ }
+
+ return M_ERROR;
+}
+
+int sec_push(struct section *section, struct section_entry entry)
{
if (section->count >= section->len) {
section->len *= 2;
- section->ins = realloc(section->ins,
- sizeof(union mips_instruction) * section->len);
+ void *new = realloc(section->entries,
+ sizeof(struct section_entry) * section->len);
- if (section->ins == NULL) {
+ if (new == NULL) {
ERROR("cannot realloc");
return M_ERROR;
}
+
+ section->entries = new;
}
- section->ins[section->count++] = ins;
- sec_tbl->total_ins++;
+ section->entries[section->count++] = entry;
return M_SUCCESS;
}
-int sectbl_get(struct section_table *sec_tbl, struct section **sec,
- const char name[MAX_LEX_LENGTH])
+size_t sec_size(struct section *sec)
{
- for (uint32_t i = 0; i < sec_tbl->count; i++) {
- struct section *temp = &sec_tbl->sections[i];
- if (strcmp(name, temp->name) == 0) {
- if (sec != NULL)
- *sec = temp;
- return M_SUCCESS;
- }
+ size_t n = 0;
+ for (uint32_t i = 0; i < sec->count; i++) {
+ size_t t = sec->entries[i].size;
+ size_t m = t % sec->alignment;
+ if (m)
+ t += sec->alignment - m;
+ n += t;
}
+ return n;
+}
- return M_ERROR;
+size_t sec_index(struct section *sec, uint32_t idx)
+{
+ size_t n = 0;
+ for (uint32_t i = 0; i < idx; i++) {
+ size_t t = sec->entries[i].size;
+ size_t m = t % sec->alignment;
+ if (m)
+ t += sec->alignment - m;
+ n += t;
+ }
+ return n;
}