aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorAndrew Chambers <[email protected]>2021-10-02 19:12:18 +1300
committerAndrew Chambers <[email protected]>2021-10-02 19:12:18 +1300
commit11ab3bd2a9054c2cdaa959f59172b67e5b5da495 (patch)
tree586650b754787fe0a49861e355bcafd3853d9720 /main.c
parent9240ca9a72492e6a93cb28e05ae6c0bc52d7bd5d (diff)
strtab sections should have a zero byte at index 0.
Diffstat (limited to 'main.c')
-rw-r--r--main.c12
1 files changed, 6 insertions, 6 deletions
diff --git a/main.c b/main.c
index aa692eb..2d009bf 100644
--- a/main.c
+++ b/main.c
@@ -17,12 +17,8 @@ static Section *bss = NULL;
static Section *text = NULL;
static void secappend(Section *s, uint8_t *bytes, size_t n) {
- if (s->capacity == 0) {
- s->capacity = 32;
- s->data = xmalloc(s->capacity);
- }
while (s->capacity < s->hdr.sh_size + n) {
- s->capacity = s->capacity * 2;
+ s->capacity = s->capacity ? (s->capacity * 2) : 64;
s->data = xrealloc(s->data, s->capacity);
}
memcpy(s->data + s->hdr.sh_size, bytes, n);
@@ -49,15 +45,19 @@ static Section *newsection() {
}
static void initsections(void) {
+ uint8_t zb = 0;
+
shstrtab = newsection();
shstrtab->hdr.sh_name = elfstr(shstrtab, ".shstrtab");
shstrtab->hdr.sh_type = SHT_STRTAB;
shstrtab->hdr.sh_entsize = 1;
+ secappend(shstrtab, &zb, 1);
strtab = newsection();
strtab->hdr.sh_name = elfstr(shstrtab, ".strtab");
strtab->hdr.sh_type = SHT_STRTAB;
strtab->hdr.sh_entsize = 1;
+ secappend(strtab, &zb, 1);
symtab = newsection();
symtab->hdr.sh_name = elfstr(shstrtab, ".symtab");
@@ -102,8 +102,8 @@ static void outelf(void) {
ehdr.e_flags = 0;
ehdr.e_version = 1;
ehdr.e_ehsize = sizeof(Elf64_Ehdr);
- ehdr.e_shentsize = sizeof(Elf64_Shdr);
ehdr.e_shoff = sizeof(Elf64_Ehdr);
+ ehdr.e_shentsize = sizeof(Elf64_Shdr);
ehdr.e_shnum = nsections;
ehdr.e_shstrndx = 1;
offset = sizeof(Elf64_Shdr) * nsections;