aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Forney <[email protected]>2021-10-10 13:48:25 -0700
committerMichael Forney <[email protected]>2021-10-10 13:57:22 -0700
commitaa7cd52a635be70cc9ef21ad6db0f0c983ba4be3 (patch)
treed23848c6ba3c503861ffa040c0aaaefbecf573be
parent8dd8783aa5b28d4950b9debcf30bd30b367beb7f (diff)
Use void * for functions that access bytes of object representation.
Although out() and secaddbytes() only use their argument to call fwrite()/memcpy(), using the type uint8_t * suggests that it might be dereferenced as uint8_t. In general, it is undefined behavior to access an object through a uint8_t pointer, since uint8_t is not necessarily a character type. So, just use void * to avoid some casts and implication of possible undefined behavior.
-rw-r--r--main.c16
1 files changed, 8 insertions, 8 deletions
diff --git a/main.c b/main.c
index 147aede..4b60a86 100644
--- a/main.c
+++ b/main.c
@@ -46,7 +46,7 @@ static const char *secname(Section *s) {
return (const char *)shstrtab->data + s->hdr.sh_name;
}
-static void secaddbytes(Section *s, uint8_t *bytes, size_t n) {
+static void secaddbytes(Section *s, const void *bytes, size_t n) {
while (s->capacity < s->hdr.sh_size + n) {
s->capacity = s->capacity ? (s->capacity * 2) : 64;
s->data = xrealloc(s->data, s->capacity);
@@ -64,7 +64,7 @@ static Elf64_Word elfstr(Section *sec, const char *s) {
if (strcmp(s, (char *)&sec->data[i]) == 0)
return i;
}
- secaddbytes(sec, (uint8_t *)s, strlen(s) + 1);
+ secaddbytes(sec, s, strlen(s) + 1);
return i;
}
@@ -112,7 +112,7 @@ static void initsections(void) {
symtab->hdr.sh_link = strtab->idx;
symtab->hdr.sh_entsize = sizeof(Elf64_Sym);
memset(&elfsym, 0, sizeof(elfsym));
- secaddbytes(symtab, (uint8_t *)&elfsym, sizeof(Elf64_Sym));
+ secaddbytes(symtab, &elfsym, sizeof(Elf64_Sym));
bss = newsection();
bss->hdr.sh_name = elfstr(shstrtab, ".bss");
@@ -992,7 +992,7 @@ static void addtosymtab(Symbol *sym) {
elfsym.st_info = ELF64_ST_INFO(sbind, stype);
elfsym.st_shndx = sym->section ? sym->section->idx : SHN_UNDEF;
elfsym.st_other = 0;
- secaddbytes(symtab, (uint8_t *)&elfsym, sizeof(Elf64_Sym));
+ secaddbytes(symtab, &elfsym, sizeof(Elf64_Sym));
}
static void fillsymtab(void) {
@@ -1085,7 +1085,7 @@ static void appendreloc(Relocation *reloc) {
unreachable();
}
- secaddbytes(relsection, (uint8_t *)&elfrel, sizeof(elfrel));
+ secaddbytes(relsection, &elfrel, sizeof(elfrel));
}
static void handlerelocs(void) {
@@ -1101,7 +1101,7 @@ static void handlerelocs(void) {
}
}
-static void out(uint8_t *buf, size_t n) {
+static void out(const void *buf, size_t n) {
fwrite(buf, 1, n, outf);
if (ferror(outf))
fatal("fwrite:");
@@ -1129,12 +1129,12 @@ static void outelf(void) {
ehdr.e_shnum = nsections;
ehdr.e_shstrndx = 1;
- out((uint8_t *)&ehdr, sizeof(ehdr));
+ out(&ehdr, sizeof(ehdr));
offset = sizeof(Elf64_Ehdr) + sizeof(Elf64_Shdr) * nsections;
for (i = 0; i < nsections; i++) {
sections[i].hdr.sh_offset = offset;
- out((uint8_t *)&sections[i].hdr, sizeof(Elf64_Shdr));
+ out(&sections[i].hdr, sizeof(Elf64_Shdr));
offset += sections[i].hdr.sh_size;
}
for (i = 0; i < nsections; i++) {