summaryrefslogtreecommitdiff
path: root/masm/reltab.c
blob: afbd5e795a5214ab2d527212f91f5aef5b69c9f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <elf.h>
#include <stdlib.h>
#include <merror.h>

#include "asm.h"

#define RELTAB_INIT_LEN 8

int reltab_init(struct relocation_table *reltab)
{
	reltab->size = RELTAB_INIT_LEN;
	reltab->len = 0;
	reltab->data = malloc(sizeof(Elf32_Rela) * RELTAB_INIT_LEN);

	if (reltab->data == NULL) {
		PERROR("cannot alloc");
		return M_ERROR;
	}

	return M_SUCCESS;
}

void reltab_free(struct relocation_table *reltab)
{
	free(reltab->data);
}

int reltab_push(struct relocation_table *reltab, const Elf32_Rela rel)
{
	if (reltab->len >= reltab->size) {
		reltab->size *= 2;
		reltab->data = realloc(reltab->data, sizeof(Elf32_Rela)
			 * reltab->size);

		if (reltab->data == NULL) {
			PERROR("cannot realloc");
			return M_ERROR;
		}
	}

	reltab->data[reltab->len++] = rel;
	return M_SUCCESS;
}