2024-09-11 16:06:09 +00:00
|
|
|
#include <merror.h>
|
2024-09-12 12:37:46 +00:00
|
|
|
#include <netinet/in.h>
|
2024-09-11 16:06:09 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2024-10-21 16:27:18 +00:00
|
|
|
#include "../tab.h"
|
2024-09-11 16:06:09 +00:00
|
|
|
|
|
|
|
#define SYMTBL_INIT_LEN 24
|
|
|
|
|
|
|
|
int symtab_init(struct symbol_table *symtab)
|
|
|
|
{
|
2024-10-04 23:41:10 +00:00
|
|
|
symtab->size = SYMTBL_INIT_LEN;
|
|
|
|
symtab->len = 0;
|
|
|
|
symtab->symbols = malloc(sizeof(struct symbol) * SYMTBL_INIT_LEN);
|
2024-09-11 16:06:09 +00:00
|
|
|
|
2024-10-04 23:41:10 +00:00
|
|
|
if (symtab->symbols == NULL) {
|
|
|
|
PERROR("cannot alloc");
|
|
|
|
return M_ERROR;
|
|
|
|
}
|
2024-09-11 16:06:09 +00:00
|
|
|
|
2024-10-04 23:41:10 +00:00
|
|
|
return M_SUCCESS;
|
2024-09-11 16:06:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void symtab_free(struct symbol_table *symtab)
|
|
|
|
{
|
2024-10-04 23:41:10 +00:00
|
|
|
for (uint32_t i = 0; i < symtab->len; i++)
|
|
|
|
string_free(&symtab->symbols[i].name);
|
|
|
|
free(symtab->symbols);
|
2024-09-11 16:06:09 +00:00
|
|
|
}
|
|
|
|
|
2024-10-04 23:41:10 +00:00
|
|
|
int symtab_push(struct symbol_table *symtab, struct symbol *sym)
|
2024-09-11 16:06:09 +00:00
|
|
|
{
|
2024-10-04 23:41:10 +00:00
|
|
|
if (symtab->len >= symtab->size) {
|
|
|
|
symtab->size *= 2;
|
|
|
|
symtab->symbols = realloc(symtab->symbols,
|
|
|
|
sizeof(struct symbol) * symtab->size);
|
|
|
|
if (symtab->symbols == NULL) {
|
|
|
|
PERROR("cannot realloc");
|
|
|
|
return M_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2024-09-11 16:06:09 +00:00
|
|
|
|
2024-10-04 23:41:10 +00:00
|
|
|
sym->tabidx = symtab->len;
|
|
|
|
symtab->symbols[symtab->len++] = *sym;
|
|
|
|
return M_SUCCESS;
|
2024-09-11 16:06:09 +00:00
|
|
|
}
|
|
|
|
|
2024-10-04 23:41:10 +00:00
|
|
|
int symtab_find(struct symbol_table *symtab, struct symbol **res,
|
|
|
|
const char *name)
|
2024-09-11 16:06:09 +00:00
|
|
|
{
|
2024-10-04 23:41:10 +00:00
|
|
|
for (uint32_t i = 0; i < symtab->len; i++) {
|
|
|
|
struct symbol *sym = &symtab->symbols[i];
|
|
|
|
if (strcmp(sym->name.str, name) == 0) {
|
|
|
|
if (res != NULL)
|
|
|
|
*res = sym;
|
|
|
|
return M_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return M_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
int symtab_find_or_stub(struct symbol_table *symtab, struct symbol **res,
|
|
|
|
const struct string *const name)
|
|
|
|
{
|
|
|
|
if (symtab_find(symtab, res, name->str) == M_SUCCESS)
|
|
|
|
return M_SUCCESS;
|
|
|
|
|
|
|
|
struct symbol temp = {
|
|
|
|
.offset = 0,
|
|
|
|
.secidx = SYM_SEC_STUB,
|
|
|
|
.type = SYM_LOCAL,
|
|
|
|
};
|
|
|
|
if (string_clone(&temp.name, name))
|
|
|
|
return M_ERROR;
|
|
|
|
|
|
|
|
if (symtab_push(symtab, &temp)) {
|
|
|
|
string_free(&temp.name);
|
|
|
|
return M_ERROR;
|
|
|
|
}
|
2024-09-11 16:06:09 +00:00
|
|
|
|
2024-10-04 23:41:10 +00:00
|
|
|
if (res != NULL)
|
|
|
|
*res = &symtab->symbols[symtab->len - 1];
|
2024-09-11 16:06:09 +00:00
|
|
|
|
2024-10-04 23:41:10 +00:00
|
|
|
return M_SUCCESS;
|
2024-09-11 16:06:09 +00:00
|
|
|
}
|