summaryrefslogtreecommitdiff
path: root/masm/asm/elf32.h
blob: 76aeb19d42a8d77eda7c84e1698642da5fd961ce (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* Copyright (c) 2024 Freya Murphy */

#ifndef __ELF32_H__
#define __ELF32_H__

#include <elf.h>

#include "../gen.h"
#include "../masm.h"

///
/// ELF string table
///

struct elf_str_table {
	// size of the ptr in bytes
	size_t size;

	// pointer that contains
	// the strings
	char *ptr;
};

/* initalize a string table */
int strtab_init(struct elf_str_table *strtab);

/* free a string table */
void strtab_free(struct elf_str_table *strtab);

/* get a string form the string table */
int strtab_get_str(struct elf_str_table *strtab, const char *str, size_t *res);

/* get or append a string into the string table */
int strtab_write_str(struct elf_str_table *strtab, const char *str, size_t *res);

///
/// elf section
///

/* holds a section of the asm file (i.e. .text, .bss, .data) */
struct elf_section {
	// section data *weak* pointer
	struct section *data;

	// index of the section in
	// the ELF shdr
	size_t shdr_idx;

	// relocation table
	size_t reltab_shidx;
	uint32_t reltab_len;
	Elf32_Rel *reltab;
};

///
/// assembler
///

struct elf_assembler {
	// arguments passed in
	struct arguments *args;

	// the code generator
	struct generator *gen;

	/// symbol table
	size_t symtab_shidx;
	size_t symtab_len;
	Elf32_Sym *symbols;

	// sh string table
	size_t strtab_shidx;
	struct elf_str_table strtab;

	// string table
	size_t shstrtab_shidx;
	struct elf_str_table shstrtab;

	/// sections
	uint32_t section_len;
	struct elf_section *sections;

	/// section header
	Elf32_Shdr *shdr;
	uint32_t shdr_len;
};

int assemble_elf32(struct generator *gen, struct arguments *args);

#endif /* __ELF32_H__ */