Skip to content

Commit

Permalink
added namespacing for hidden functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nickeldan committed May 1, 2023
1 parent 6ae047f commit 3a795d6
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 62 deletions.
2 changes: 2 additions & 0 deletions changelog
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
0.1.1:
- Added namespacing for hidden functions.
2 changes: 1 addition & 1 deletion include/elfjack/elfjack.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdint.h>
#include <sys/types.h>

#define ELFJACK_VERSION "0.1.0"
#define ELFJACK_VERSION "0.1.1"

#ifdef __GNUC__
#define EJ_PURE __attribute__((pure))
Expand Down
54 changes: 27 additions & 27 deletions src/elfjack.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,24 @@ parseElfHeader(ejElfInfo *info, struct ehdrParams *params)
const Elf32_Ehdr *ehdr_32 = info->map.data;

if (info->map.size <= EI_DATA || memcmp(info->map.data, ELFMAG, SELFMAG) != 0) {
emitError("Not an ELF file");
ejEmitError("Not an ELF file");
return EJ_RET_NOT_ELF;
}

switch (ehdr_64->e_ident[EI_CLASS]) {
case ELFCLASS32: params->_64 = false; break;
case ELFCLASS64: params->_64 = true; break;
default: emitError("Invalid EI_CLASS in ELF header"); return EJ_RET_MALFORMED_ELF;
default: ejEmitError("Invalid EI_CLASS in ELF header"); return EJ_RET_MALFORMED_ELF;
}

switch (ehdr_64->e_ident[EI_DATA]) {
case ELFDATA2LSB: info->visible.little_endian = true; break;
case ELFDATA2MSB: info->visible.little_endian = false; break;
default: emitError("Invalid EI_DATA in ELF header"); return EJ_RET_MALFORMED_ELF;
default: ejEmitError("Invalid EI_DATA in ELF header"); return EJ_RET_MALFORMED_ELF;
}

if (info->map.size < (params->_64 ? sizeof(Elf64_Ehdr) : sizeof(Elf32_Ehdr))) {
emitError("File is not big enough to contain the ELF header");
ejEmitError("File is not big enough to contain the ELF header");
return EJ_RET_MALFORMED_ELF;
}

Expand All @@ -84,21 +84,21 @@ parseElfHeader(ejElfInfo *info, struct ehdrParams *params)
}

if (params->shstrndx >= params->shnum) {
emitError("Invalid e_shstrndx in ELF header");
ejEmitError("Invalid e_shstrndx in ELF header");
return EJ_RET_MALFORMED_ELF;
}

switch (ehdr_64->e_type) {
case ET_DYN: info->dynamic = true; break;
case ET_EXEC: break;
default: emitError("File is neither an executable nor a shared object"); return EJ_RET_NOT_ELF;
default: ejEmitError("File is neither an executable nor a shared object"); return EJ_RET_NOT_ELF;
}

if (params->shnum >= SHN_LORESERVE) {
const void *sheader = AT_OFFSET(info->map.data, params->shoff);

if ((unsigned long)params->shoff + shentsize > info->map.size) {
emitError("File is not big enough to contain the section header table");
ejEmitError("File is not big enough to contain the section header table");
return EJ_RET_MALFORMED_ELF;
}

Expand All @@ -115,11 +115,11 @@ parseElfHeader(ejElfInfo *info, struct ehdrParams *params)
}

if (params->shnum == 0) {
emitError("File contains no section headers");
ejEmitError("File contains no section headers");
return EJ_RET_MISSING_INFO;
}
if ((unsigned long)params->shoff + shentsize * params->shnum > info->map.size) {
emitError("File is not big enough to contain the section header table");
ejEmitError("File is not big enough to contain the section header table");
return EJ_RET_MALFORMED_ELF;
}

Expand All @@ -131,11 +131,11 @@ parseElfHeader(ejElfInfo *info, struct ehdrParams *params)
}

if (params->phnum == 0) {
emitError("File contains no program headers");
ejEmitError("File contains no program headers");
return EJ_RET_MISSING_INFO;
}
if ((unsigned long)(params->phoff + phentsize * params->phnum) > info->map.size) {
emitError("File is not big enough to contain the program header table");
ejEmitError("File is not big enough to contain the program header table");
return EJ_RET_MALFORMED_ELF;
}

Expand All @@ -154,19 +154,19 @@ ejParseElf(const char *path, ejElfInfo *info)
int (*find_shdrs)(ejElfInfo *, const struct ehdrParams *);

if (!path || !info) {
emitError("The arguments cannot be NULL");
ejEmitError("The arguments cannot be NULL");
return EJ_RET_BAD_USAGE;
}

*info = EJ_ELF_INFO_INIT;

fd = open(path, O_RDONLY);
if (fd < 0) {
emitError("open: %s", strerror(errno));
ejEmitError("open: %s", strerror(errno));
return EJ_RET_READ_FAILURE;
}
if (fstat(fd, &fs) != 0) {
emitError("fstat: %s", strerror(errno));
ejEmitError("fstat: %s", strerror(errno));
close(fd);
return EJ_RET_READ_FAILURE;
}
Expand All @@ -177,7 +177,7 @@ ejParseElf(const char *path, ejElfInfo *info)
local_errno = errno;
close(fd);
if (info->map.data == MAP_FAILED) {
emitError("mmap: %s", strerror(local_errno));
ejEmitError("mmap: %s", strerror(local_errno));
info->map.data = NULL;
return EJ_RET_MAP_FAIL;
}
Expand All @@ -188,23 +188,23 @@ ejParseElf(const char *path, ejElfInfo *info)
}

if (params._64) {
find_load_addr = findLoadAddr64;
find_shdrs = findShdrs64;
info->find_symbol = findSymbol64;
info->find_got_entry = findGotEntry64;
find_load_addr = ejFindLoadAddr64;
find_shdrs = ejFindShdrs64;
info->find_symbol = ejFindSymbol64;
info->find_got_entry = ejFindGotEntry64;
info->visible.pointer_size = 8;
}
else {
find_load_addr = findLoadAddr32;
find_shdrs = findShdrs32;
info->find_symbol = findSymbol32;
info->find_got_entry = findGotEntry32;
find_load_addr = ejFindLoadAddr32;
find_shdrs = ejFindShdrs32;
info->find_symbol = ejFindSymbol32;
info->find_got_entry = ejFindGotEntry32;
info->visible.pointer_size = 4;
}

ret = find_load_addr(AT_OFFSET(info->map.data, params.phoff), params.phnum, &load_addr);
if (ret != EJ_RET_OK) {
emitError("No LOAD segment found");
ejEmitError("No LOAD segment found");
goto error;
}

Expand All @@ -217,13 +217,13 @@ ejParseElf(const char *path, ejElfInfo *info)
}

if (!info->symbols.start) {
emitError(".dynsym not found");
ejEmitError(".dynsym not found");
}
else if (!info->symbols.strings) {
emitError(".dynstr not found");
ejEmitError(".dynstr not found");
}
else if (info->text_section_index == 0) {
emitError(".text not found");
ejEmitError(".text not found");
}
else {
return EJ_RET_OK;
Expand Down
2 changes: 1 addition & 1 deletion src/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ejGetError(void)
}

void
emitError(const char *format, ...)
ejEmitError(const char *format, ...)
{
va_list args;

Expand Down
2 changes: 1 addition & 1 deletion src/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ struct ehdrParams {
};

void
emitError(const char *format, ...) EJ_HIDDEN;
ejEmitError(const char *format, ...) EJ_HIDDEN;
22 changes: 11 additions & 11 deletions src/parse32.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ getShdrStrings(const struct ejMapInfo *map, const Elf32_Shdr *shdr, size_t *size
const char *strings;

if (!SHDR_SANITY_CHECK(shdr, map->size)) {
emitError("File is not big enough to contain the section header string table");
ejEmitError("File is not big enough to contain the section header string table");
return NULL;
}

strings = AT_OFFSET(map->data, shdr->sh_offset);
*size = shdr->sh_size;
if (*size == 0) {
emitError("Section header string table is empty");
ejEmitError("Section header string table is empty");
return NULL;
}
if (strings[*size - 1] != '\0') {
emitError("Section header string table is not null-terminated");
ejEmitError("Section header string table is not null-terminated");
return NULL;
}

return strings;
}

int
findLoadAddr32(const void *pheader, uint32_t phnum, unsigned int *load_addr)
ejFindLoadAddr32(const void *pheader, uint32_t phnum, unsigned int *load_addr)
{
const Elf32_Phdr *phdr = pheader;

Expand All @@ -43,7 +43,7 @@ findLoadAddr32(const void *pheader, uint32_t phnum, unsigned int *load_addr)
}

int
findShdrs32(ejElfInfo *info, const struct ehdrParams *params)
ejFindShdrs32(ejElfInfo *info, const struct ehdrParams *params)
{
unsigned int num_found = 0;
size_t strings_size;
Expand All @@ -65,7 +65,7 @@ findShdrs32(ejElfInfo *info, const struct ehdrParams *params)
}

if (!SHDR_SANITY_CHECK(shdr, info->map.size) || shdr->sh_name >= strings_size) {
emitError("File is not big enough to contain section #%llu", (unsigned long long)k);
ejEmitError("File is not big enough to contain section #%llu", (unsigned long long)k);
return EJ_RET_MALFORMED_ELF;
}
section_start = AT_OFFSET(info->map.data, shdr->sh_offset);
Expand All @@ -79,11 +79,11 @@ findShdrs32(ejElfInfo *info, const struct ehdrParams *params)
info->symbols.strings = section_start;
info->symbols.strings_size = shdr->sh_size;
if (info->symbols.strings_size == 0) {
emitError(".dynstr is empty");
ejEmitError(".dynstr is empty");
return EJ_RET_MALFORMED_ELF;
}
if (info->symbols.strings[info->symbols.strings_size - 1] != '\0') {
emitError(".dynstr is not null-terminated");
ejEmitError(".dynstr is not null-terminated");
return EJ_RET_MALFORMED_ELF;
}
}
Expand Down Expand Up @@ -116,8 +116,8 @@ findShdrs32(ejElfInfo *info, const struct ehdrParams *params)
}

bool
findSymbol32(const struct ejSymbolInfo *symbols, const char *func_name, uint16_t section_index, ejAddr *addr,
uint64_t *symbol_index)
ejFindSymbol32(const struct ejSymbolInfo *symbols, const char *func_name, uint16_t section_index,
ejAddr *addr, uint64_t *symbol_index)
{
const Elf32_Sym *syms = symbols->start;

Expand Down Expand Up @@ -146,7 +146,7 @@ findSymbol32(const struct ejSymbolInfo *symbols, const char *func_name, uint16_t
}

ejAddr
findGotEntry32(const struct ejRelInfo *rels, uint64_t symbol_index)
ejFindGotEntry32(const struct ejRelInfo *rels, uint64_t symbol_index)
{
const unsigned char *object = rels->start;

Expand Down
10 changes: 5 additions & 5 deletions src/parse32.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#include "internal.h"

int
findLoadAddr32(const void *pheader, uint32_t phnum, unsigned int *load_addr) EJ_HIDDEN;
ejFindLoadAddr32(const void *pheader, uint32_t phnum, unsigned int *load_addr) EJ_HIDDEN;

int
findShdrs32(ejElfInfo *info, const struct ehdrParams *params) EJ_HIDDEN;
ejFindShdrs32(ejElfInfo *info, const struct ehdrParams *params) EJ_HIDDEN;

bool
findSymbol32(const struct ejSymbolInfo *symbols, const char *func_name, uint16_t section_index, ejAddr *addr,
uint64_t *symbol_index) EJ_HIDDEN;
ejFindSymbol32(const struct ejSymbolInfo *symbols, const char *func_name, uint16_t section_index,
ejAddr *addr, uint64_t *symbol_index) EJ_HIDDEN;

ejAddr
findGotEntry32(const struct ejRelInfo *rels, uint64_t symbol_index) EJ_HIDDEN;
ejFindGotEntry32(const struct ejRelInfo *rels, uint64_t symbol_index) EJ_HIDDEN;
22 changes: 11 additions & 11 deletions src/parse64.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ getShdrStrings(const struct ejMapInfo *map, const Elf64_Shdr *shdr, size_t *size
const char *strings;

if (!SHDR_SANITY_CHECK(shdr, map->size)) {
emitError("File is not big enough to contain the section header string table");
ejEmitError("File is not big enough to contain the section header string table");
return NULL;
}

strings = AT_OFFSET(map->data, shdr->sh_offset);
*size = shdr->sh_size;
if (*size == 0) {
emitError("Section header string table is empty");
ejEmitError("Section header string table is empty");
return NULL;
}
if (strings[*size - 1] != '\0') {
emitError("Section header string table is not null-terminated");
ejEmitError("Section header string table is not null-terminated");
return NULL;
}

return strings;
}

int
findLoadAddr64(const void *pheader, uint32_t phnum, unsigned int *load_addr)
ejFindLoadAddr64(const void *pheader, uint32_t phnum, unsigned int *load_addr)
{
const Elf64_Phdr *phdr = pheader;

Expand All @@ -43,7 +43,7 @@ findLoadAddr64(const void *pheader, uint32_t phnum, unsigned int *load_addr)
}

int
findShdrs64(ejElfInfo *info, const struct ehdrParams *params)
ejFindShdrs64(ejElfInfo *info, const struct ehdrParams *params)
{
unsigned int num_found = 0;
size_t strings_size;
Expand All @@ -65,7 +65,7 @@ findShdrs64(ejElfInfo *info, const struct ehdrParams *params)
}

if (!SHDR_SANITY_CHECK(shdr, info->map.size) || shdr->sh_name >= strings_size) {
emitError("File is not big enough to contain section #%llu", (unsigned long long)k);
ejEmitError("File is not big enough to contain section #%llu", (unsigned long long)k);
return EJ_RET_MALFORMED_ELF;
}
section_start = AT_OFFSET(info->map.data, shdr->sh_offset);
Expand All @@ -79,11 +79,11 @@ findShdrs64(ejElfInfo *info, const struct ehdrParams *params)
info->symbols.strings = section_start;
info->symbols.strings_size = shdr->sh_size;
if (info->symbols.strings_size == 0) {
emitError(".dynstr is empty");
ejEmitError(".dynstr is empty");
return EJ_RET_MALFORMED_ELF;
}
if (info->symbols.strings[info->symbols.strings_size - 1] != '\0') {
emitError(".dynstr is not null-terminated");
ejEmitError(".dynstr is not null-terminated");
return EJ_RET_MALFORMED_ELF;
}
}
Expand Down Expand Up @@ -116,8 +116,8 @@ findShdrs64(ejElfInfo *info, const struct ehdrParams *params)
}

bool
findSymbol64(const struct ejSymbolInfo *symbols, const char *func_name, uint16_t section_index, ejAddr *addr,
uint64_t *symbol_index)
ejFindSymbol64(const struct ejSymbolInfo *symbols, const char *func_name, uint16_t section_index,
ejAddr *addr, uint64_t *symbol_index)
{
const Elf64_Sym *syms = symbols->start;

Expand Down Expand Up @@ -146,7 +146,7 @@ findSymbol64(const struct ejSymbolInfo *symbols, const char *func_name, uint16_t
}

ejAddr
findGotEntry64(const struct ejRelInfo *rels, uint64_t symbol_index)
ejFindGotEntry64(const struct ejRelInfo *rels, uint64_t symbol_index)
{
const unsigned char *object = rels->start;

Expand Down
10 changes: 5 additions & 5 deletions src/parse64.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
#include "internal.h"

int
findLoadAddr64(const void *pheader, uint32_t phnum, unsigned int *load_addr) EJ_HIDDEN;
ejFindLoadAddr64(const void *pheader, uint32_t phnum, unsigned int *load_addr) EJ_HIDDEN;

int
findShdrs64(ejElfInfo *info, const struct ehdrParams *params) EJ_HIDDEN;
ejFindShdrs64(ejElfInfo *info, const struct ehdrParams *params) EJ_HIDDEN;

bool
findSymbol64(const struct ejSymbolInfo *symbols, const char *func_name, uint16_t section_index, ejAddr *addr,
uint64_t *symbol_index) EJ_HIDDEN;
ejFindSymbol64(const struct ejSymbolInfo *symbols, const char *func_name, uint16_t section_index,
ejAddr *addr, uint64_t *symbol_index) EJ_HIDDEN;

ejAddr
findGotEntry64(const struct ejRelInfo *rels, uint64_t symbol_index) EJ_HIDDEN;
ejFindGotEntry64(const struct ejRelInfo *rels, uint64_t symbol_index) EJ_HIDDEN;

0 comments on commit 3a795d6

Please sign in to comment.