diff --git a/changelog b/changelog new file mode 100644 index 0000000..157be3c --- /dev/null +++ b/changelog @@ -0,0 +1,2 @@ +0.1.1: + - Added namespacing for hidden functions. diff --git a/include/elfjack/elfjack.h b/include/elfjack/elfjack.h index 95a97b3..f864482 100644 --- a/include/elfjack/elfjack.h +++ b/include/elfjack/elfjack.h @@ -4,7 +4,7 @@ #include #include -#define ELFJACK_VERSION "0.1.0" +#define ELFJACK_VERSION "0.1.1" #ifdef __GNUC__ #define EJ_PURE __attribute__((pure)) diff --git a/src/elfjack.c b/src/elfjack.c index 8072527..a86f71c 100644 --- a/src/elfjack.c +++ b/src/elfjack.c @@ -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; } @@ -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; } @@ -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; } @@ -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; } @@ -154,7 +154,7 @@ 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; } @@ -162,11 +162,11 @@ ejParseElf(const char *path, ejElfInfo *info) 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; } @@ -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; } @@ -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; } @@ -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; diff --git a/src/internal.c b/src/internal.c index bd615d0..cfb7d67 100644 --- a/src/internal.c +++ b/src/internal.c @@ -14,7 +14,7 @@ ejGetError(void) } void -emitError(const char *format, ...) +ejEmitError(const char *format, ...) { va_list args; diff --git a/src/internal.h b/src/internal.h index c7ffb38..d7c5a1b 100644 --- a/src/internal.h +++ b/src/internal.h @@ -27,4 +27,4 @@ struct ehdrParams { }; void -emitError(const char *format, ...) EJ_HIDDEN; +ejEmitError(const char *format, ...) EJ_HIDDEN; diff --git a/src/parse32.c b/src/parse32.c index 8db779f..5d5eb7e 100644 --- a/src/parse32.c +++ b/src/parse32.c @@ -9,18 +9,18 @@ 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; } @@ -28,7 +28,7 @@ getShdrStrings(const struct ejMapInfo *map, const Elf32_Shdr *shdr, size_t *size } 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; @@ -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; @@ -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); @@ -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; } } @@ -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; @@ -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; diff --git a/src/parse32.h b/src/parse32.h index 3d96573..9a5f3cb 100644 --- a/src/parse32.h +++ b/src/parse32.h @@ -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; diff --git a/src/parse64.c b/src/parse64.c index 6f4a39a..dcc6339 100644 --- a/src/parse64.c +++ b/src/parse64.c @@ -9,18 +9,18 @@ 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; } @@ -28,7 +28,7 @@ getShdrStrings(const struct ejMapInfo *map, const Elf64_Shdr *shdr, size_t *size } 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; @@ -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; @@ -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); @@ -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; } } @@ -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; @@ -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; diff --git a/src/parse64.h b/src/parse64.h index 8482e59..7e8ccea 100644 --- a/src/parse64.h +++ b/src/parse64.h @@ -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;