summaryrefslogtreecommitdiff
path: root/masm/asm/strtab.c
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-10-09 12:07:59 -0400
committerFreya Murphy <freya@freyacat.org>2024-10-09 12:07:59 -0400
commitb663f827057fc9fb199293bc1920cf27315d1846 (patch)
tree477b481694ad50f28bac538bb9b301861b3af4d6 /masm/asm/strtab.c
parentupdate generator to support multipe isas, expand grammer syntax (diff)
downloadmips-b663f827057fc9fb199293bc1920cf27315d1846.tar.gz
mips-b663f827057fc9fb199293bc1920cf27315d1846.tar.bz2
mips-b663f827057fc9fb199293bc1920cf27315d1846.zip
refactor elf32 assembler, add support for multiple isa's in cmdline
Diffstat (limited to 'masm/asm/strtab.c')
-rw-r--r--masm/asm/strtab.c54
1 files changed, 54 insertions, 0 deletions
diff --git a/masm/asm/strtab.c b/masm/asm/strtab.c
new file mode 100644
index 0000000..799f0dc
--- /dev/null
+++ b/masm/asm/strtab.c
@@ -0,0 +1,54 @@
+#include <merror.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include "elf32.h"
+
+int strtab_get_str(struct elf_str_table *strtab, const char *str, size_t *res)
+{
+ for (size_t i = 0; i < strtab->size; i ++) {
+ if (strcmp(strtab->ptr + i, str) == 0) {
+ if (res != NULL)
+ *res = i;
+ return M_SUCCESS;
+ }
+ }
+
+ return M_ERROR;
+}
+
+int strtab_write_str(struct elf_str_table *strtab, const char *str, size_t *res)
+{
+ if (strtab_get_str(strtab, str, res) == M_SUCCESS)
+ return M_SUCCESS;
+
+ size_t len = strlen(str);
+ char *new = realloc(strtab->ptr, strtab->size + len + 1);
+ if (new == NULL)
+ return M_ERROR;
+ strtab->ptr = new;
+ memcpy(strtab->ptr + strtab->size, str, len + 1);
+
+ if (res != NULL)
+ *res = strtab->size;
+
+ strtab->size += len + 1;
+ return M_SUCCESS;
+}
+
+int strtab_init(struct elf_str_table *strtab)
+{
+ strtab->size = 1;
+ strtab->ptr = malloc(1);
+ if (strtab->ptr == NULL) {
+ PERROR("cannot alloc");
+ return M_ERROR;
+ }
+ *strtab->ptr = '\0';
+ return M_SUCCESS;
+}
+
+void strtab_free(struct elf_str_table *strtab)
+{
+ free(strtab->ptr);
+}