mirror of
https://github.com/wiiu-env/WiiUPluginSystem.git
synced 2024-11-16 15:49:23 +01:00
[Loader] Updated the loader to call functions
Progress?
This commit is contained in:
parent
83a9aa7b7b
commit
46456df8fc
@ -45,9 +45,9 @@ INCLUDES := src/libelf \
|
|||||||
# options for code generation
|
# options for code generation
|
||||||
#---------------------------------------------------------------------------------
|
#---------------------------------------------------------------------------------
|
||||||
CFLAGS := -std=gnu11 -mrvl -mcpu=750 -meabi -mhard-float -ffast-math \
|
CFLAGS := -std=gnu11 -mrvl -mcpu=750 -meabi -mhard-float -ffast-math \
|
||||||
-O3 -D__wiiu__ -Wall -Wextra -Wno-unused-parameter -Wno-strict-aliasing $(INCLUDE)
|
-O0 -D__wiiu__ -Wall -Wextra -Wno-unused-parameter -Wno-strict-aliasing $(INCLUDE)
|
||||||
CXXFLAGS := -std=gnu++11 -mrvl -mcpu=750 -meabi -mhard-float -ffast-math \
|
CXXFLAGS := -std=gnu++11 -mrvl -mcpu=750 -meabi -mhard-float -ffast-math \
|
||||||
-O3 -D__wiiu__ -Wall -Wextra -Wno-unused-parameter -Wno-strict-aliasing -D_GNU_SOURCE $(INCLUDE)
|
-O0 -D__wiiu__ -Wall -Wextra -Wno-unused-parameter -Wno-strict-aliasing -D_GNU_SOURCE $(INCLUDE)
|
||||||
|
|
||||||
ifeq ($(DO_LOGGING), 1)
|
ifeq ($(DO_LOGGING), 1)
|
||||||
CFLAGS += -D__LOGGING__
|
CFLAGS += -D__LOGGING__
|
||||||
|
@ -34,8 +34,16 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <utils/logger.h>
|
#include <utils/logger.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include "utils.h"
|
||||||
|
|
||||||
|
#define MODULE_RELOCATIONS_CAPCITY_DEFAULT 128
|
||||||
|
|
||||||
|
module_unresolved_relocation_t *module_relocations = NULL;
|
||||||
|
size_t module_relocations_count = 0;
|
||||||
|
size_t module_relocations_capacity = 0;
|
||||||
|
|
||||||
|
bool Module_ElfLoadSection(const Elf *elf, Elf_Scn *scn, const Elf32_Shdr *shdr,void *destination) {
|
||||||
|
|
||||||
int Module_ElfLoadSection(const Elf *elf, Elf_Scn *scn, const Elf32_Shdr *shdr, void *destination) {
|
|
||||||
assert(destination != NULL);
|
assert(destination != NULL);
|
||||||
|
|
||||||
switch (shdr->sh_type) {
|
switch (shdr->sh_type) {
|
||||||
@ -51,18 +59,18 @@ int Module_ElfLoadSection(const Elf *elf, Elf_Scn *scn, const Elf32_Shdr *shdr,
|
|||||||
memcpy((char *)destination + n, data->d_buf, data->d_size);
|
memcpy((char *)destination + n, data->d_buf, data->d_size);
|
||||||
n += data->d_size;
|
n += data->d_size;
|
||||||
}
|
}
|
||||||
return 1;
|
return true;
|
||||||
} case SHT_NOBITS: {
|
} case SHT_NOBITS: {
|
||||||
memset(destination, 0, shdr->sh_size);
|
memset(destination, 0, shdr->sh_size);
|
||||||
return 1;
|
return true;
|
||||||
} default:
|
} default:
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int Module_LoadElfSymtab(Elf *elf, Elf32_Sym **symtab, size_t *symtab_count,size_t *symtab_strndx) {
|
bool Module_LoadElfSymtab(Elf *elf, Elf32_Sym **symtab, size_t *symtab_count, size_t *symtab_strndx) {
|
||||||
Elf_Scn *scn;
|
Elf_Scn *scn;
|
||||||
int result = 0;
|
bool result = false;
|
||||||
|
|
||||||
for (scn = elf_nextscn(elf, NULL);
|
for (scn = elf_nextscn(elf, NULL);
|
||||||
scn != NULL;
|
scn != NULL;
|
||||||
@ -75,8 +83,6 @@ int Module_LoadElfSymtab(Elf *elf, Elf32_Sym **symtab, size_t *symtab_count,size
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (shdr->sh_type == SHT_SYMTAB) {
|
if (shdr->sh_type == SHT_SYMTAB) {
|
||||||
size_t shstrndx;
|
|
||||||
|
|
||||||
size_t sym;
|
size_t sym;
|
||||||
|
|
||||||
assert (*symtab == NULL);
|
assert (*symtab == NULL);
|
||||||
@ -90,8 +96,9 @@ int Module_LoadElfSymtab(Elf *elf, Elf32_Sym **symtab, size_t *symtab_count,size
|
|||||||
if (!Module_ElfLoadSection(elf, scn, shdr, *symtab))
|
if (!Module_ElfLoadSection(elf, scn, shdr, *symtab))
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
|
|
||||||
for (sym = 0; sym < *symtab_count; sym++)
|
for (sym = 0; sym < *symtab_count; sym++){
|
||||||
(*symtab)[sym].st_other = 0;
|
(*symtab)[sym].st_other = 0;
|
||||||
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -100,13 +107,12 @@ int Module_LoadElfSymtab(Elf *elf, Elf32_Sym **symtab, size_t *symtab_count,size
|
|||||||
if (*symtab == NULL)
|
if (*symtab == NULL)
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
|
|
||||||
result = 1;
|
result = true;
|
||||||
exit_error:
|
exit_error:
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Module_ElfLoadSymbols(size_t shndx, const void *destination, Elf32_Sym *symtab, size_t symtab_count) {
|
void Module_ElfLoadSymbols(size_t shndx, const void *destination, Elf32_Sym *symtab, size_t symtab_count) {
|
||||||
|
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
/* use the st_other field (no defined meaning) to indicate whether or not a
|
/* use the st_other field (no defined meaning) to indicate whether or not a
|
||||||
@ -121,18 +127,17 @@ void Module_ElfLoadSymbols(size_t shndx, const void *destination, Elf32_Sym *sym
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Module_ElfLink(module_information_t * module_information, Elf *elf, size_t shndx, void *destination,Elf32_Sym *symtab, size_t symtab_count, size_t symtab_strndx,int allow_globals) {
|
bool Module_ElfLink(size_t index, Elf *elf, size_t shndx, void *destination, Elf32_Sym *symtab, size_t symtab_count, size_t symtab_strndx, bool allow_globals) {
|
||||||
Elf_Scn *scn;
|
Elf_Scn *scn;
|
||||||
|
|
||||||
for (scn = elf_nextscn(elf, NULL);
|
for (scn = elf_nextscn(elf, NULL); scn != NULL; scn = elf_nextscn(elf, scn)) {
|
||||||
scn != NULL;
|
|
||||||
scn = elf_nextscn(elf, scn)) {
|
|
||||||
|
|
||||||
Elf32_Shdr *shdr;
|
Elf32_Shdr *shdr;
|
||||||
|
|
||||||
shdr = elf32_getshdr(scn);
|
shdr = elf32_getshdr(scn);
|
||||||
if (shdr == NULL)
|
if (shdr == NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
switch (shdr->sh_type) {
|
switch (shdr->sh_type) {
|
||||||
case SHT_REL: {
|
case SHT_REL: {
|
||||||
const Elf32_Rel *rel;
|
const Elf32_Rel *rel;
|
||||||
@ -168,25 +173,32 @@ bool Module_ElfLink(module_information_t * module_information, Elf *elf, size_t
|
|||||||
module_unresolved_relocation_t *reloc;
|
module_unresolved_relocation_t *reloc;
|
||||||
char *name;
|
char *name;
|
||||||
|
|
||||||
reloc = (module_unresolved_relocation_t*) malloc(sizeof(module_unresolved_relocation_t));
|
reloc = (module_unresolved_relocation_t *) Module_ListAllocate(
|
||||||
if (reloc == NULL){
|
&module_relocations,
|
||||||
DEBUG_FUNCTION_LINE("WARNING: failed to allocate space for relocate struct.\n");
|
sizeof(module_unresolved_relocation_t), 1,
|
||||||
|
&module_relocations_capacity,
|
||||||
|
&module_relocations_count,
|
||||||
|
MODULE_RELOCATIONS_CAPCITY_DEFAULT);
|
||||||
|
if (reloc == NULL)
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
name = elf_strptr(elf, symtab_strndx, symtab[symbol].st_name);
|
name = elf_strptr(
|
||||||
|
elf, symtab_strndx, symtab[symbol].st_name);
|
||||||
|
|
||||||
if (name == NULL) {
|
if (name == NULL) {
|
||||||
DEBUG_FUNCTION_LINE("WARNING: name == NULL.\n");
|
module_relocations_count--;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
reloc->name = strdup(name);
|
reloc->name = strdup(name);
|
||||||
if (reloc->name == NULL) {
|
if (reloc->name == NULL) {
|
||||||
DEBUG_FUNCTION_LINE("WARNING: strdup failed.\n");
|
module_relocations_count--;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DEBUG_FUNCTION_LINE("Adding relocation!\n");
|
||||||
|
|
||||||
|
reloc->module = index;
|
||||||
reloc->address = destination;
|
reloc->address = destination;
|
||||||
reloc->offset = rel[i].r_offset;
|
reloc->offset = rel[i].r_offset;
|
||||||
reloc->type = ELF32_R_TYPE(rel[i].r_info);
|
reloc->type = ELF32_R_TYPE(rel[i].r_info);
|
||||||
@ -194,8 +206,6 @@ bool Module_ElfLink(module_information_t * module_information, Elf *elf, size_t
|
|||||||
*(int *)((char *)destination +
|
*(int *)((char *)destination +
|
||||||
rel[i].r_offset);
|
rel[i].r_offset);
|
||||||
|
|
||||||
module_information->rel.push_back(reloc);
|
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
} else
|
} else
|
||||||
return false;
|
return false;
|
||||||
@ -227,7 +237,7 @@ bool Module_ElfLink(module_information_t * module_information, Elf *elf, size_t
|
|||||||
if (data == NULL)
|
if (data == NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
rela = (Elf32_Rela*) data->d_buf;
|
rela = (const Elf32_Rela *) data->d_buf;
|
||||||
|
|
||||||
for (i = 0; i < shdr->sh_size / sizeof(Elf32_Rela); i++) {
|
for (i = 0; i < shdr->sh_size / sizeof(Elf32_Rela); i++) {
|
||||||
uint32_t symbol_addr;
|
uint32_t symbol_addr;
|
||||||
@ -249,40 +259,44 @@ bool Module_ElfLink(module_information_t * module_information, Elf *elf, size_t
|
|||||||
module_unresolved_relocation_t *reloc;
|
module_unresolved_relocation_t *reloc;
|
||||||
char *name;
|
char *name;
|
||||||
|
|
||||||
reloc = (module_unresolved_relocation_t*) malloc(sizeof(module_unresolved_relocation_t));
|
reloc = (module_unresolved_relocation_t *) Module_ListAllocate(
|
||||||
if (reloc == NULL){
|
&module_relocations,
|
||||||
DEBUG_FUNCTION_LINE("WARNING: failed to allocate space for relocate struct.\n");
|
sizeof(module_unresolved_relocation_t), 1,
|
||||||
|
&module_relocations_capacity,
|
||||||
|
&module_relocations_count,
|
||||||
|
MODULE_RELOCATIONS_CAPCITY_DEFAULT);
|
||||||
|
if (reloc == NULL)
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
|
name = elf_strptr(
|
||||||
name = elf_strptr(elf, symtab_strndx, symtab[symbol].st_name);
|
elf, symtab_strndx, symtab[symbol].st_name);
|
||||||
|
|
||||||
if (name == NULL) {
|
if (name == NULL) {
|
||||||
DEBUG_FUNCTION_LINE("WARNING: name == NULL.\n");
|
module_relocations_count--;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
reloc->name = strdup(name);
|
reloc->name = strdup(name);
|
||||||
if (reloc->name == NULL) {
|
if (reloc->name == NULL) {
|
||||||
DEBUG_FUNCTION_LINE("WARNING: strdup failed.\n");
|
module_relocations_count--;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DEBUG_FUNCTION_LINE("Adding relocation!\n");
|
||||||
|
|
||||||
|
reloc->module = index;
|
||||||
reloc->address = destination;
|
reloc->address = destination;
|
||||||
reloc->offset = rela[i].r_offset;
|
reloc->offset = rela[i].r_offset;
|
||||||
reloc->type = ELF32_R_TYPE(rela[i].r_info);
|
reloc->type = ELF32_R_TYPE(rela[i].r_info);
|
||||||
reloc->addend = rela[i].r_addend;
|
reloc->addend = rela[i].r_addend;
|
||||||
|
|
||||||
module_information->rel.push_back(reloc);
|
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
} else
|
} else
|
||||||
return false;
|
return false;
|
||||||
} default: {
|
} default: {
|
||||||
|
|
||||||
if (symtab[symbol].st_other != 1)
|
if (symtab[symbol].st_other != 1)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
symbol_addr = symtab[symbol].st_value;
|
symbol_addr = symtab[symbol].st_value;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -298,13 +312,13 @@ bool Module_ElfLink(module_information_t * module_information, Elf *elf, size_t
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 1;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Module_ElfLinkOne(char type, size_t offset, int addend, void *destination, uint32_t symbol_addr) {
|
bool Module_ElfLinkOne(char type, size_t offset, int addend, void *destination, uint32_t symbol_addr) {
|
||||||
int value;
|
int value;
|
||||||
char *target = (char *)destination + offset;
|
char *target = (char *)destination + offset;
|
||||||
int result = 0;
|
bool result = false;
|
||||||
|
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case R_PPC_ADDR32:
|
case R_PPC_ADDR32:
|
||||||
@ -403,8 +417,9 @@ int Module_ElfLinkOne(char type, size_t offset, int addend, void *destination, u
|
|||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = 1;
|
result = true;
|
||||||
exit_error:
|
exit_error:
|
||||||
if (!result) DEBUG_FUNCTION_LINE("Module_ElfLinkOne: exit_error\n");
|
if (!result) DEBUG_FUNCTION_LINE("Module_ElfLinkOne: exit_error\n");
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,11 +14,13 @@ extern "C" {
|
|||||||
|
|
||||||
#include "module_parser.h"
|
#include "module_parser.h"
|
||||||
|
|
||||||
int Module_ElfLoadSection(const Elf *elf, Elf_Scn *scn, const Elf32_Shdr *shdr, void *destination);
|
bool Module_ElfLoadSection(const Elf *elf, Elf_Scn *scn, const Elf32_Shdr *shdr,void *destination);
|
||||||
int Module_LoadElfSymtab(Elf *elf, Elf32_Sym **symtab, size_t *symtab_count,size_t *symtab_strndx);
|
bool Module_LoadElfSymtab(Elf *elf, Elf32_Sym **symtab, size_t *symtab_count, size_t *symtab_strndx);
|
||||||
void Module_ElfLoadSymbols(size_t shndx, const void *destination, Elf32_Sym *symtab, size_t symtab_count);
|
void Module_ElfLoadSymbols(size_t shndx, const void *destination, Elf32_Sym *symtab, size_t symtab_count);
|
||||||
bool Module_ElfLink(module_information_t * module_information, Elf *elf, size_t shndx, void *destination,Elf32_Sym *symtab, size_t symtab_count, size_t symtab_strndx,int allow_globals);
|
bool Module_ElfLink(size_t index, Elf *elf, size_t shndx, void *destination, Elf32_Sym *symtab, size_t symtab_count, size_t symtab_strndx, bool allow_globals);
|
||||||
int Module_ElfLinkOne(char type, size_t offset, int addend, void *destination, uint32_t symbol_addr);
|
bool Module_ElfLinkOne(char type, size_t offset, int addend, void *destination, uint32_t symbol_addr);
|
||||||
void *Module_ListAllocate(void *list, size_t entry_size, size_t num, size_t *capacity, size_t *count, size_t default_capacity);
|
|
||||||
|
|
||||||
|
extern size_t module_relocations_count;
|
||||||
|
extern module_unresolved_relocation_t *module_relocations;
|
||||||
|
extern size_t module_relocations_capacity;
|
||||||
#endif
|
#endif
|
||||||
|
@ -36,20 +36,32 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <utils/logger.h>
|
#include <utils/logger.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <dynamic_libs/os_functions.h>
|
||||||
|
|
||||||
bool Module_LinkModule(module_information_t * module_information, uint8_t **space) {
|
#define MODULE_ENTRIES_CAPACITY_DEFAULT 128
|
||||||
if(module_information == NULL){
|
|
||||||
DEBUG_FUNCTION_LINE("module_information is null\n");
|
wups_loader_entry_t *module_entries = NULL;
|
||||||
|
size_t module_entries_count = 0;
|
||||||
|
size_t module_entries_capacity = 0;
|
||||||
|
|
||||||
|
bool Module_ListLink(uint8_t **space) {
|
||||||
|
size_t i;
|
||||||
|
bool result = false;
|
||||||
|
|
||||||
|
for (i = 0; i < module_list_count; i++) {
|
||||||
|
if (!Module_LinkModule(i, module_list[i]->path, space))
|
||||||
|
goto exit_error;
|
||||||
}
|
}
|
||||||
module_metadata_t * metadata = module_information->metadata;
|
|
||||||
const char * path = NULL;
|
result = true;
|
||||||
if(metadata != NULL && metadata->path != NULL){
|
exit_error:
|
||||||
path = metadata->path;
|
if (!result) DEBUG_FUNCTION_LINE("Module_ListLink: exit_error\n");
|
||||||
}else{
|
return result;
|
||||||
DEBUG_FUNCTION_LINE("metadata is null\n");
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Module_LinkModule(size_t index, const char *path, uint8_t **space) {
|
||||||
|
DEBUG_FUNCTION_LINE("Running Module_LinkModule for %s\n",path);
|
||||||
|
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
Elf *elf = NULL;
|
Elf *elf = NULL;
|
||||||
bool result = false;
|
bool result = false;
|
||||||
@ -72,7 +84,8 @@ bool Module_LinkModule(module_information_t * module_information, uint8_t **spac
|
|||||||
/* TODO */
|
/* TODO */
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
case ELF_K_ELF:
|
case ELF_K_ELF:
|
||||||
if (!Module_LinkModuleElf(module_information, elf, space))
|
DEBUG_FUNCTION_LINE("Calling now Module_LinkModuleElf\n");
|
||||||
|
if (!Module_LinkModuleElf(index, elf, space))
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
@ -89,7 +102,7 @@ exit_error:
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Module_LinkModuleElf(module_information_t * module_information, Elf *elf, uint8_t **space) {
|
bool Module_LinkModuleElf(size_t index, Elf *elf, uint8_t **space) {
|
||||||
Elf_Scn *scn;
|
Elf_Scn *scn;
|
||||||
size_t symtab_count, section_count, shstrndx, symtab_strndx, entries_count;
|
size_t symtab_count, section_count, shstrndx, symtab_strndx, entries_count;
|
||||||
Elf32_Sym *symtab = NULL;
|
Elf32_Sym *symtab = NULL;
|
||||||
@ -137,7 +150,10 @@ bool Module_LinkModuleElf(module_information_t * module_information, Elf *elf, u
|
|||||||
goto exit_error;
|
goto exit_error;
|
||||||
|
|
||||||
entries_count = shdr->sh_size / sizeof(wups_loader_entry_t);
|
entries_count = shdr->sh_size / sizeof(wups_loader_entry_t);
|
||||||
entries = (wups_loader_entry_t*) malloc(sizeof(wups_loader_entry_t)*entries_count);
|
entries = (wups_loader_entry_t*) Module_ListAllocate(
|
||||||
|
&module_entries, sizeof(wups_loader_entry_t),
|
||||||
|
entries_count, &module_entries_capacity,
|
||||||
|
&module_entries_count, MODULE_ENTRIES_CAPACITY_DEFAULT);
|
||||||
|
|
||||||
if (entries == NULL)
|
if (entries == NULL)
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
@ -145,14 +161,10 @@ bool Module_LinkModuleElf(module_information_t * module_information, Elf *elf, u
|
|||||||
destinations[elf_ndxscn(scn)] = (uint8_t *)entries;
|
destinations[elf_ndxscn(scn)] = (uint8_t *)entries;
|
||||||
if (!Module_ElfLoadSection(elf, scn, shdr, entries))
|
if (!Module_ElfLoadSection(elf, scn, shdr, entries))
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
Module_ElfLoadSymbols(elf_ndxscn(scn), entries, symtab, symtab_count);
|
Module_ElfLoadSymbols(
|
||||||
for(int i = 0;i<entries_count;i++){
|
elf_ndxscn(scn), entries, symtab, symtab_count);
|
||||||
wups_loader_entry_t * curEntry = &entries[i];
|
|
||||||
module_information->entries.push_back(curEntry);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
DEBUG_FUNCTION_LINE("Copy the function to %08X\n",*space);
|
DEBUG_FUNCTION_LINE("Copy function to %08X\n",*space);
|
||||||
*space -= shdr->sh_size;
|
*space -= shdr->sh_size;
|
||||||
if (shdr->sh_addralign > 3)
|
if (shdr->sh_addralign > 3)
|
||||||
*space = (uint8_t *)((int)*space &
|
*space = (uint8_t *)((int)*space &
|
||||||
@ -165,7 +177,8 @@ bool Module_LinkModuleElf(module_information_t * module_information, Elf *elf, u
|
|||||||
assert(*space != NULL);
|
assert(*space != NULL);
|
||||||
if (!Module_ElfLoadSection(elf, scn, shdr, *space))
|
if (!Module_ElfLoadSection(elf, scn, shdr, *space))
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
Module_ElfLoadSymbols(elf_ndxscn(scn), *space, symtab, symtab_count);
|
Module_ElfLoadSymbols(
|
||||||
|
elf_ndxscn(scn), *space, symtab, symtab_count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -187,7 +200,8 @@ bool Module_LinkModuleElf(module_information_t * module_information, Elf *elf, u
|
|||||||
(shdr->sh_flags & SHF_ALLOC) &&
|
(shdr->sh_flags & SHF_ALLOC) &&
|
||||||
destinations[elf_ndxscn(scn)] != NULL) {
|
destinations[elf_ndxscn(scn)] != NULL) {
|
||||||
|
|
||||||
if (!Module_ElfLink(module_information, elf, elf_ndxscn(scn), destinations[elf_ndxscn(scn)],
|
if (!Module_ElfLink(
|
||||||
|
index, elf, elf_ndxscn(scn), destinations[elf_ndxscn(scn)],
|
||||||
symtab, symtab_count, symtab_strndx, true))
|
symtab, symtab_count, symtab_strndx, true))
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
@ -202,3 +216,83 @@ exit_error:
|
|||||||
free(symtab);
|
free(symtab);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Module_ListLinkFinal(uint8_t **space) {
|
||||||
|
size_t relocation_index, entry_index, module_index;
|
||||||
|
bool result = false, has_error = false;
|
||||||
|
|
||||||
|
relocation_index = 0;
|
||||||
|
entry_index = 0;
|
||||||
|
|
||||||
|
/* Process the replacements the link each module in turn.
|
||||||
|
* It must be done in this order, otherwise two replacements to the same
|
||||||
|
* function will cause an infinite loop. */
|
||||||
|
for (module_index = 0; module_index < module_list_count; module_index++) {
|
||||||
|
size_t limit;
|
||||||
|
|
||||||
|
limit = entry_index + module_list[module_index]->entries_count;
|
||||||
|
|
||||||
|
/*for (; entry_index < limit; entry_index++) {
|
||||||
|
wups_loader_entry_t *entry;
|
||||||
|
|
||||||
|
assert(entry_index < module_entries_count);
|
||||||
|
|
||||||
|
entry = module_entries + entry_index;
|
||||||
|
|
||||||
|
switch (entry->type) {
|
||||||
|
case WUPS_LOADER_ENTRY_EXPORT: {
|
||||||
|
break;
|
||||||
|
} case WUPS_LOADER_ENTRY_FUNCTION:
|
||||||
|
case WUPS_LOADER_ENTRY_FUNCTION_MANDATORY: {
|
||||||
|
if (!Module_ListLinkFinalReplaceFunction(space, entry))
|
||||||
|
goto exit_error;
|
||||||
|
break;
|
||||||
|
} default:
|
||||||
|
goto exit_error;
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
for (;
|
||||||
|
relocation_index < module_relocations_count;
|
||||||
|
relocation_index++) {
|
||||||
|
module_unresolved_relocation_t *reloc;
|
||||||
|
void *symbol;
|
||||||
|
|
||||||
|
reloc = module_relocations + relocation_index;
|
||||||
|
|
||||||
|
if (reloc->module != module_index)
|
||||||
|
break;
|
||||||
|
|
||||||
|
assert(reloc->name != NULL);
|
||||||
|
//symbol = Search_SymbolLookup(reloc->name);
|
||||||
|
|
||||||
|
if (symbol == NULL) {
|
||||||
|
DEBUG_FUNCTION_LINE(
|
||||||
|
"Missing symbol '%s' needed by '%s'\n", reloc->name,
|
||||||
|
module_list[module_index]->name);
|
||||||
|
has_error = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Module_ElfLinkOne(
|
||||||
|
reloc->type, reloc->offset, reloc->addend,
|
||||||
|
reloc->address, (uint32_t)symbol))
|
||||||
|
goto exit_error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(entry_index == module_entries_count);
|
||||||
|
assert(relocation_index == module_relocations_count);
|
||||||
|
|
||||||
|
if (has_error)
|
||||||
|
goto exit_error;
|
||||||
|
|
||||||
|
result = true;
|
||||||
|
exit_error:
|
||||||
|
if (!result) DEBUG_FUNCTION_LINE("Module_ListLinkFinal: exit_error\n");
|
||||||
|
module_relocations_count = 0;
|
||||||
|
module_relocations_capacity = 0;
|
||||||
|
free(module_relocations);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -14,6 +14,13 @@ extern "C" {
|
|||||||
|
|
||||||
#include "module_parser.h"
|
#include "module_parser.h"
|
||||||
|
|
||||||
bool Module_LinkModule(module_information_t * module_information, uint8_t **space);
|
bool Module_ListLink(uint8_t **space);
|
||||||
bool Module_LinkModuleElf(module_information_t * module_information, Elf *elf, uint8_t **space);
|
bool Module_LinkModule(size_t index, const char *path, uint8_t **space);
|
||||||
|
bool Module_LinkModuleElf(size_t index, Elf *elf, uint8_t **space);
|
||||||
|
bool Module_ListLinkFinal(uint8_t **space);
|
||||||
|
|
||||||
|
extern wups_loader_entry_t *module_entries;
|
||||||
|
extern size_t module_entries_count;
|
||||||
|
extern size_t module_entries_capacity;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -21,6 +21,10 @@
|
|||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "module_parser.h"
|
#include "module_parser.h"
|
||||||
#include "link_utils.h"
|
#include "link_utils.h"
|
||||||
|
#include "elf_utils.h"
|
||||||
|
|
||||||
|
static void printModuleInfos();
|
||||||
|
static void printInfos();
|
||||||
|
|
||||||
/* Entry point */
|
/* Entry point */
|
||||||
extern "C" int Menu_Main(int argc, char **argv){
|
extern "C" int Menu_Main(int argc, char **argv){
|
||||||
@ -68,11 +72,50 @@ void loadAndProcessElf(const char * elfPath){
|
|||||||
|
|
||||||
DEBUG_FUNCTION_LINE("Now try to use brainslug code\n");
|
DEBUG_FUNCTION_LINE("Now try to use brainslug code\n");
|
||||||
if(Module_CheckFile(elfPath)){
|
if(Module_CheckFile(elfPath)){
|
||||||
module_information_t * moduleInformation = Module_LoadModuleInformation(elfPath);
|
Module_Load(elfPath);
|
||||||
if(moduleInformation != NULL){
|
DEBUG_FUNCTION_LINE("Found %d modules!\n",module_list_count);
|
||||||
uint8_t * space = (uint8_t *)0x880000;
|
//printInfos();
|
||||||
Module_LinkModule(moduleInformation,&space);
|
unsigned char * space = (unsigned char*)0x00910000;
|
||||||
printModuleInformation(moduleInformation);
|
Module_ListLink(&space);
|
||||||
|
|
||||||
|
if(module_relocations_count == 0){
|
||||||
|
DEBUG_FUNCTION_LINE("We need no relocations, we can call the functions!!\n");
|
||||||
|
DEBUG_FUNCTION_LINE("Calling %d functions!\n",module_entries_count);
|
||||||
|
for (unsigned int i = 0; i < module_entries_count; i++) {
|
||||||
|
DEBUG_FUNCTION_LINE("--- Function %d ---\n",i);
|
||||||
|
if( module_entries[i].type == WUPS_LOADER_ENTRY_FUNCTION ||
|
||||||
|
module_entries[i].type == WUPS_LOADER_ENTRY_FUNCTION_MANDATORY){
|
||||||
|
DEBUG_FUNCTION_LINE("Let's call the function: %s \n",module_entries[i].data._function.name);
|
||||||
|
int ret = ( (int (*)(void))((unsigned int*)module_entries[i].data._function.target) )();
|
||||||
|
DEBUG_FUNCTION_LINE("result: %d \n",ret);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static void printInfos(){
|
||||||
|
for (unsigned int i = 0; i < module_list_count; i++) {
|
||||||
|
DEBUG_FUNCTION_LINE("--- Module %d ---\n",i);
|
||||||
|
DEBUG_FUNCTION_LINE("name: %s\n",module_list[i]->name);
|
||||||
|
DEBUG_FUNCTION_LINE("path: %s\n",module_list[i]->path);
|
||||||
|
DEBUG_FUNCTION_LINE("author: %s\n",module_list[i]->author);
|
||||||
|
}
|
||||||
|
DEBUG_FUNCTION_LINE("--- Module list end ---\n");
|
||||||
|
for (unsigned int i = 0; i < module_entries_count; i++) {
|
||||||
|
DEBUG_FUNCTION_LINE("--- Entry %d ---\n",i);
|
||||||
|
if( module_entries[i].type == WUPS_LOADER_ENTRY_FUNCTION ||
|
||||||
|
module_entries[i].type == WUPS_LOADER_ENTRY_FUNCTION_MANDATORY){
|
||||||
|
DEBUG_FUNCTION_LINE("library: %d \n",module_entries[i].data._function.library);
|
||||||
|
DEBUG_FUNCTION_LINE("function: %s \n",module_entries[i].data._function.name);
|
||||||
|
DEBUG_FUNCTION_LINE("pointer: %08X \n",module_entries[i].data._function.target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DEBUG_FUNCTION_LINE("--- Entry list end ---\n");
|
||||||
|
for (unsigned int i = 0; i < module_relocations_count; i++) {
|
||||||
|
DEBUG_FUNCTION_LINE("--- Relocation %d ---\n",i);
|
||||||
|
DEBUG_FUNCTION_LINE("name: %s\n",module_relocations[i].name);
|
||||||
|
}
|
||||||
|
DEBUG_FUNCTION_LINE("--- Relocation list end ---\n");
|
||||||
|
}
|
||||||
|
@ -37,72 +37,18 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
static module_information_t* Module_LoadMetaDataFromElf(const char *path, Elf *elf);
|
bool module_has_error;
|
||||||
static module_information_t* Module_ModuleInformationRead(const char *path, Elf *elf, Elf32_Sym *symtab, size_t symtab_count, size_t symtab_strndx);
|
bool module_has_info;
|
||||||
|
|
||||||
void printModuleInformation(module_information_t* module_information){
|
#define MODULE_LIST_CAPACITY_DEFAULT 16
|
||||||
if(module_information == NULL){
|
|
||||||
DEBUG_FUNCTION_LINE("module_information is null\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
module_metadata_t * metadata = module_information->metadata;
|
size_t module_list_size = 0;
|
||||||
std::vector<module_unresolved_relocation_t *> relocations = module_information->rel;
|
module_metadata_t **module_list = NULL;
|
||||||
DEBUG_FUNCTION_LINE("relocations size: %d\n",relocations.size());
|
size_t module_list_count = 0;
|
||||||
for (std::vector<module_unresolved_relocation_t *>::iterator it = relocations.begin() ; it != relocations.end(); ++it){
|
static size_t module_list_capacity = 0;
|
||||||
module_unresolved_relocation_t * relo = *it;
|
|
||||||
DEBUG_FUNCTION_LINE("----\n",relo);
|
|
||||||
DEBUG_FUNCTION_LINE("name %s\n",relo->name);
|
|
||||||
DEBUG_FUNCTION_LINE("offset %d\n",relo->offset);
|
|
||||||
DEBUG_FUNCTION_LINE("type %d\n",relo->type);
|
|
||||||
DEBUG_FUNCTION_LINE("address %08X\n",relo->address);
|
|
||||||
DEBUG_FUNCTION_LINE("append %d\n",relo->addend);
|
|
||||||
}
|
|
||||||
|
|
||||||
DEBUG_FUNCTION_LINE("entries size: %d\n",relocations.size());
|
static void Module_LoadElf(const char *path, Elf *elf);
|
||||||
std::vector<wups_loader_entry_t *> entries = module_information->entries;
|
static module_metadata_t *Module_MetadataRead(const char *path, size_t index, Elf *elf, Elf32_Sym *symtab, size_t symtab_count, size_t symtab_strndx);
|
||||||
for (std::vector<wups_loader_entry_t *>::iterator it = entries.begin() ; it != entries.end(); ++it){
|
|
||||||
//Added entry.
|
|
||||||
wups_loader_entry_t * curEntry = *it;
|
|
||||||
DEBUG_FUNCTION_LINE("Found entry @ %08X\n",curEntry);
|
|
||||||
dumpHex(curEntry,sizeof(wups_loader_entry_t));
|
|
||||||
|
|
||||||
DEBUG_FUNCTION_LINE("type %d\n",curEntry->type);
|
|
||||||
if( curEntry->type == WUPS_LOADER_ENTRY_FUNCTION ||
|
|
||||||
curEntry->type == WUPS_LOADER_ENTRY_FUNCTION_MANDATORY){
|
|
||||||
|
|
||||||
DEBUG_FUNCTION_LINE("library %d \n",curEntry->data._function.library);
|
|
||||||
DEBUG_FUNCTION_LINE("function %s \n",curEntry->data._function.name);
|
|
||||||
DEBUG_FUNCTION_LINE("target pointer %08X \n",curEntry->data._function.target);
|
|
||||||
dumpHex(curEntry->data._function.target,0x100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(metadata != NULL){
|
|
||||||
if(metadata->author != NULL){
|
|
||||||
DEBUG_FUNCTION_LINE("Author: %s.\n", metadata->author);
|
|
||||||
}else{
|
|
||||||
DEBUG_FUNCTION_LINE("Author: is null\n");
|
|
||||||
}
|
|
||||||
if(metadata->license != NULL){
|
|
||||||
DEBUG_FUNCTION_LINE("License: %s.\n", metadata->license);
|
|
||||||
}else{
|
|
||||||
DEBUG_FUNCTION_LINE("License: is null\n");
|
|
||||||
}
|
|
||||||
if(metadata->name != NULL){
|
|
||||||
DEBUG_FUNCTION_LINE("Name: %s.\n", metadata->name);
|
|
||||||
}else{
|
|
||||||
DEBUG_FUNCTION_LINE("Name: is null\n");
|
|
||||||
}
|
|
||||||
if(metadata->path != NULL){
|
|
||||||
DEBUG_FUNCTION_LINE("Path: %s.\n", metadata->path);
|
|
||||||
}else{
|
|
||||||
DEBUG_FUNCTION_LINE("Path: is null\n");
|
|
||||||
}
|
|
||||||
DEBUG_FUNCTION_LINE("entries_count: %d \n",metadata->entries_count);
|
|
||||||
}else{
|
|
||||||
DEBUG_FUNCTION_LINE("metadata is null\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Module_CheckFile(const char *path) {
|
bool Module_CheckFile(const char *path) {
|
||||||
const char *extension;
|
const char *extension;
|
||||||
@ -126,10 +72,7 @@ bool Module_CheckFile(const char *path) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
module_information_t* Module_LoadModuleInformation(const char *path) {
|
void Module_Load(const char *path) {
|
||||||
DEBUG_FUNCTION_LINE("Loading path: %s\n", path);
|
|
||||||
|
|
||||||
module_information_t* result = NULL;
|
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
Elf *elf = NULL;
|
Elf *elf = NULL;
|
||||||
|
|
||||||
@ -150,34 +93,34 @@ module_information_t* Module_LoadModuleInformation(const char *path) {
|
|||||||
switch (elf_kind(elf)) {
|
switch (elf_kind(elf)) {
|
||||||
case ELF_K_AR:
|
case ELF_K_AR:
|
||||||
/* TODO */
|
/* TODO */
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Archives not yet supported.\n", path);
|
DEBUG_FUNCTION_LINE(
|
||||||
//module_has_info = 1;
|
"Warning: Ignoring '%s' - Archives not yet supported.\n", path);
|
||||||
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
case ELF_K_ELF:
|
case ELF_K_ELF:
|
||||||
DEBUG_FUNCTION_LINE("Module_LoadElf(path, elf);\n");
|
Module_LoadElf(path, elf);
|
||||||
result = Module_LoadMetaDataFromElf(path, elf);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Invalid ELF file.\n", path);
|
DEBUG_FUNCTION_LINE(
|
||||||
|
"Warning: Ignoring '%s' - Invalid ELF file.\n", path);
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
exit_error:
|
exit_error:
|
||||||
if (elf != NULL)
|
if (elf != NULL)
|
||||||
//elf_end(elf);
|
elf_end(elf);
|
||||||
if (fd != -1)
|
if (fd != -1)
|
||||||
close(fd);
|
close(fd);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module_information_t* Module_LoadMetaDataFromElf(const char *path, Elf *elf) {
|
static void Module_LoadElf(const char *path, Elf *elf) {
|
||||||
Elf_Scn *scn;
|
Elf_Scn *scn;
|
||||||
Elf32_Ehdr *ehdr;
|
Elf32_Ehdr *ehdr;
|
||||||
char *ident;
|
char *ident;
|
||||||
size_t shstrndx, sz, symtab_count, i, symtab_strndx;
|
size_t shstrndx, sz, symtab_count, i, symtab_strndx;
|
||||||
Elf32_Sym *symtab = NULL;
|
Elf32_Sym *symtab = NULL;
|
||||||
module_metadata_t *metadata = NULL;
|
module_metadata_t *metadata = NULL;
|
||||||
module_information_t* result = NULL;
|
module_metadata_t **list_ptr;
|
||||||
|
|
||||||
assert(elf != NULL);
|
assert(elf != NULL);
|
||||||
assert(elf_kind(elf) == ELF_K_ELF);
|
assert(elf_kind(elf) == ELF_K_ELF);
|
||||||
@ -186,27 +129,27 @@ module_information_t* Module_LoadMetaDataFromElf(const char *path, Elf *elf) {
|
|||||||
|
|
||||||
if (ident == NULL) {
|
if (ident == NULL) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Invalid ELF header.\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Invalid ELF header.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
if (sz < 7) {
|
if (sz < 7) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Invalid ELF header.\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Invalid ELF header.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
if (ident[4] != ELFCLASS32) {
|
if (ident[4] != ELFCLASS32) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Not 32 bit ELF.\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Not 32 bit ELF.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
if (ident[5] != ELFDATA2MSB) {
|
if (ident[5] != ELFDATA2MSB) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Not Big Endian.\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Not Big Endian.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
if (ident[6] != EV_CURRENT) {
|
if (ident[6] != EV_CURRENT) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Unknown ELF version.\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Unknown ELF version.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -214,50 +157,43 @@ module_information_t* Module_LoadMetaDataFromElf(const char *path, Elf *elf) {
|
|||||||
|
|
||||||
if (ehdr == NULL) {
|
if (ehdr == NULL) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Invalid ELF header\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Invalid ELF header\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
if (ehdr->e_type != ET_REL) {
|
if (ehdr->e_type != ET_REL) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Not relocatable ELF.\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Not relocatable ELF.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
if (ehdr->e_machine != EM_PPC) {
|
if (ehdr->e_machine != EM_PPC) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Architecture not EM_PPC.\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Architecture not EM_PPC.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
if (ehdr->e_version != EV_CURRENT) {
|
if (ehdr->e_version != EV_CURRENT) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Unknown ELF version.\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Unknown ELF version.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
DEBUG_FUNCTION_LINE("Loading Elf symtab\n");
|
|
||||||
if (!Module_LoadElfSymtab(elf, &symtab, &symtab_count, &symtab_strndx)) {
|
if (!Module_LoadElfSymtab(elf, &symtab, &symtab_count, &symtab_strndx)) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Couldn't parse symtab.\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Couldn't parse symtab.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(symtab != NULL);
|
assert(symtab != NULL);
|
||||||
|
|
||||||
result = Module_ModuleInformationRead(path, elf, symtab, symtab_count, symtab_strndx);
|
DEBUG_FUNCTION_LINE("Reading metadata from path %s.\n", path);
|
||||||
|
|
||||||
if (result == NULL){
|
metadata = Module_MetadataRead(path, module_list_count, elf, symtab, symtab_count, symtab_strndx);
|
||||||
DEBUG_FUNCTION_LINE("Module_ModuleInformationRead was null\n");
|
|
||||||
goto exit_error;
|
|
||||||
}
|
|
||||||
|
|
||||||
metadata = result->metadata;
|
|
||||||
|
|
||||||
if (metadata == NULL) /* error reporting done inside method */
|
if (metadata == NULL) /* error reporting done inside method */
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
|
|
||||||
if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
|
if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Couldn't find shdrstndx.\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Couldn't find shdrstndx.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -283,56 +219,67 @@ module_information_t* Module_LoadMetaDataFromElf(const char *path, Elf *elf) {
|
|||||||
if (strcmp(name, ".wups.meta") == 0) {
|
if (strcmp(name, ".wups.meta") == 0) {
|
||||||
continue;
|
continue;
|
||||||
} else if (strcmp(name, ".wups.load") == 0) {
|
} else if (strcmp(name, ".wups.load") == 0) {
|
||||||
metadata->size += shdr->sh_size / sizeof(wups_loader_entry_t) * 12;
|
metadata->size +=
|
||||||
|
shdr->sh_size / sizeof(wups_loader_entry_t) * 12;
|
||||||
} else {
|
} else {
|
||||||
metadata->size += shdr->sh_size;
|
metadata->size += shdr->sh_size;
|
||||||
/* add alignment padding to size */
|
/* add alignment padding to size */
|
||||||
if (shdr->sh_addralign > 3){
|
if (shdr->sh_addralign > 3)
|
||||||
/* roundup to multiple of sh_addralign */
|
/* roundup to multiple of sh_addralign */
|
||||||
metadata->size += (-metadata->size & (shdr->sh_addralign - 1));
|
metadata->size +=
|
||||||
}else{
|
(-metadata->size & (shdr->sh_addralign - 1));
|
||||||
|
else
|
||||||
/* roundup to multiple of 4 */
|
/* roundup to multiple of 4 */
|
||||||
metadata->size += (-metadata->size & 3);
|
metadata->size += (-metadata->size & 3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/* roundup to multiple of 4 */
|
/* roundup to multiple of 4 */
|
||||||
metadata->size += (-metadata->size & 3);
|
metadata->size += (-metadata->size & 3);
|
||||||
return result;
|
|
||||||
|
list_ptr = (module_metadata_t **)Module_ListAllocate(
|
||||||
|
&module_list, sizeof(module_metadata_t *), 1, &module_list_capacity,
|
||||||
|
&module_list_count, MODULE_LIST_CAPACITY_DEFAULT);
|
||||||
|
if (list_ptr == NULL) {
|
||||||
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - ENOMEM.\n", path);
|
||||||
|
module_has_info = true;
|
||||||
|
goto exit_error;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(module_list != NULL);
|
||||||
|
assert(module_list_count <= module_list_capacity);
|
||||||
|
|
||||||
|
*list_ptr = metadata;
|
||||||
|
module_list_size += metadata->size;
|
||||||
|
/* prevent the data being freed */
|
||||||
|
metadata = NULL;
|
||||||
|
|
||||||
exit_error:
|
exit_error:
|
||||||
if (metadata != NULL)
|
if (metadata != NULL)
|
||||||
free(metadata);
|
free(metadata);
|
||||||
if (symtab != NULL)
|
if (symtab != NULL)
|
||||||
free(symtab);
|
free(symtab);
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static module_information_t *Module_ModuleInformationRead(const char *path, Elf *elf, Elf32_Sym *symtab, size_t symtab_count, size_t symtab_strndx) {
|
static module_metadata_t *Module_MetadataRead(const char *path, size_t index, Elf *elf, Elf32_Sym *symtab, size_t symtab_count, size_t symtab_strndx) {
|
||||||
char *metadata = NULL, *metadata_cur, *metadata_end, *tmp;
|
char *metadata = NULL, *metadata_cur, *metadata_end, *tmp;
|
||||||
const char *name, *author, *version, *license, *wups;
|
const char *game, *name, *author, *version, *license, *wups;
|
||||||
module_metadata_t *ret = NULL;
|
module_metadata_t *ret = NULL;
|
||||||
module_information_t *result = NULL;
|
|
||||||
Elf_Scn *scn;
|
Elf_Scn *scn;
|
||||||
size_t shstrndx, entries_count;
|
size_t shstrndx, entries_count;
|
||||||
|
|
||||||
result = (module_information_t *) malloc(sizeof(module_information_t));
|
|
||||||
if (result == NULL){
|
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - couldn't allocate memory for meta information struct.\n", path);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
|
if (elf_getshdrstrndx(elf, &shstrndx) != 0) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Couldn't find shstrndx\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Couldn't find shstrndx\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
entries_count = 0;
|
entries_count = 0;
|
||||||
|
|
||||||
for (scn = elf_nextscn(elf, NULL); scn != NULL; scn = elf_nextscn(elf, scn)) {
|
for (scn = elf_nextscn(elf, NULL);
|
||||||
|
scn != NULL;
|
||||||
|
scn = elf_nextscn(elf, scn)) {
|
||||||
|
|
||||||
Elf32_Shdr *shdr;
|
Elf32_Shdr *shdr;
|
||||||
const char *name;
|
const char *name;
|
||||||
@ -346,8 +293,6 @@ static module_information_t *Module_ModuleInformationRead(const char *path, Elf
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (strcmp(name, ".wups.meta") == 0) {
|
if (strcmp(name, ".wups.meta") == 0) {
|
||||||
DEBUG_FUNCTION_LINE("In section \"%s\" \n",name);
|
|
||||||
|
|
||||||
if (shdr->sh_size == 0)
|
if (shdr->sh_size == 0)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -361,17 +306,20 @@ static module_information_t *Module_ModuleInformationRead(const char *path, Elf
|
|||||||
DEBUG_FUNCTION_LINE(
|
DEBUG_FUNCTION_LINE(
|
||||||
"Warning: Ignoring '%s' - Couldn't load .wups.meta.\n",
|
"Warning: Ignoring '%s' - Couldn't load .wups.meta.\n",
|
||||||
path);
|
path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
Module_ElfLoadSymbols(elf_ndxscn(scn), metadata, symtab, symtab_count);
|
Module_ElfLoadSymbols(
|
||||||
|
elf_ndxscn(scn), metadata, symtab, symtab_count);
|
||||||
|
|
||||||
if (!Module_ElfLink(result, elf, elf_ndxscn(scn), metadata, symtab, symtab_count, symtab_strndx, 0)) {
|
if (!Module_ElfLink(
|
||||||
|
index, elf, elf_ndxscn(scn), metadata,
|
||||||
|
symtab, symtab_count, symtab_strndx, false)) {
|
||||||
DEBUG_FUNCTION_LINE(
|
DEBUG_FUNCTION_LINE(
|
||||||
"Warning: Ignoring '%s' - .wups.meta contains invalid "
|
"Warning: Ignoring '%s' - .wups.meta contains invalid "
|
||||||
"relocations.\n", path);
|
"relocations.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -384,10 +332,11 @@ static module_information_t *Module_ModuleInformationRead(const char *path, Elf
|
|||||||
|
|
||||||
if (metadata == NULL) {
|
if (metadata == NULL) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Not a WUPS module file.\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Not a WUPS module file.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
game = NULL;
|
||||||
name = NULL;
|
name = NULL;
|
||||||
author = NULL;
|
author = NULL;
|
||||||
version = NULL;
|
version = NULL;
|
||||||
@ -409,12 +358,21 @@ static module_information_t *Module_ModuleInformationRead(const char *path, Elf
|
|||||||
if (eq == NULL)
|
if (eq == NULL)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (strncmp(metadata_cur, "name", eq - metadata_cur) == 0) {
|
if (strncmp(metadata_cur, "game", eq - metadata_cur) == 0) {
|
||||||
|
if (game != NULL) {
|
||||||
|
DEBUG_FUNCTION_LINE(
|
||||||
|
"Warning: Ignoring '%s' - Multiple WUPS_MODULE_GAME "
|
||||||
|
"declarations.\n", path);
|
||||||
|
module_has_info = true;
|
||||||
|
goto exit_error;
|
||||||
|
}
|
||||||
|
game = eq + 1;
|
||||||
|
} else if (strncmp(metadata_cur, "name", eq - metadata_cur) == 0) {
|
||||||
if (name != NULL) {
|
if (name != NULL) {
|
||||||
DEBUG_FUNCTION_LINE(
|
DEBUG_FUNCTION_LINE(
|
||||||
"Warning: Ignoring '%s' - Multiple WUPS_MODULE_NAME "
|
"Warning: Ignoring '%s' - Multiple WUPS_MODULE_NAME "
|
||||||
"declarations.\n", path);
|
"declarations.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
name = eq + 1;
|
name = eq + 1;
|
||||||
@ -423,7 +381,7 @@ static module_information_t *Module_ModuleInformationRead(const char *path, Elf
|
|||||||
DEBUG_FUNCTION_LINE(
|
DEBUG_FUNCTION_LINE(
|
||||||
"Warning: Ignoring '%s' - Multiple WUPS_MODULE_AUTHOR "
|
"Warning: Ignoring '%s' - Multiple WUPS_MODULE_AUTHOR "
|
||||||
"declarations.\n", path);
|
"declarations.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
author = eq + 1;
|
author = eq + 1;
|
||||||
@ -432,7 +390,7 @@ static module_information_t *Module_ModuleInformationRead(const char *path, Elf
|
|||||||
DEBUG_FUNCTION_LINE(
|
DEBUG_FUNCTION_LINE(
|
||||||
"Warning: Ignoring '%s' - Multiple WUPS_MODULE_VERSION "
|
"Warning: Ignoring '%s' - Multiple WUPS_MODULE_VERSION "
|
||||||
"declarations.\n", path);
|
"declarations.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
version = eq + 1;
|
version = eq + 1;
|
||||||
@ -441,7 +399,7 @@ static module_information_t *Module_ModuleInformationRead(const char *path, Elf
|
|||||||
DEBUG_FUNCTION_LINE(
|
DEBUG_FUNCTION_LINE(
|
||||||
"Warning: Ignoring '%s' - Multiple WUPS_MODULE_LICENSE "
|
"Warning: Ignoring '%s' - Multiple WUPS_MODULE_LICENSE "
|
||||||
"declarations.\n", path);
|
"declarations.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
license = eq + 1;
|
license = eq + 1;
|
||||||
@ -450,53 +408,56 @@ static module_information_t *Module_ModuleInformationRead(const char *path, Elf
|
|||||||
DEBUG_FUNCTION_LINE(
|
DEBUG_FUNCTION_LINE(
|
||||||
"Warning: Ignoring '%s' - Multiple WUPS_MODULE_NAME "
|
"Warning: Ignoring '%s' - Multiple WUPS_MODULE_NAME "
|
||||||
"declarations.\n", path);
|
"declarations.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
wups = eq + 1;
|
wups = eq + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (game == NULL)
|
||||||
|
game = "";
|
||||||
if (wups == NULL || strcmp(wups, "0.1") != 0) {
|
if (wups == NULL || strcmp(wups, "0.1") != 0) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Unrecognised BSlug version.\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Unrecognised BSlug version.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
if (name == NULL) {
|
if (name == NULL) {
|
||||||
DEBUG_FUNCTION_LINE(
|
DEBUG_FUNCTION_LINE(
|
||||||
"Warning: Ignoring '%s' - Missing WUPS_MODULE_NAME declaration.\n",
|
"Warning: Ignoring '%s' - Missing WUPS_MODULE_NAME declaration.\n",
|
||||||
path);
|
path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
if (author == NULL) {
|
if (author == NULL) {
|
||||||
DEBUG_FUNCTION_LINE(
|
DEBUG_FUNCTION_LINE(
|
||||||
"Warning: Ignoring '%s' - Missing WUPS_MODULE_AUTHOR "
|
"Warning: Ignoring '%s' - Missing WUPS_MODULE_AUTHOR "
|
||||||
"declaration.\n", path);
|
"declaration.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
if (version == NULL) {
|
if (version == NULL) {
|
||||||
DEBUG_FUNCTION_LINE(
|
DEBUG_FUNCTION_LINE(
|
||||||
"Warning: Ignoring '%s' - Missing WUPS_MODULE_VERSION "
|
"Warning: Ignoring '%s' - Missing WUPS_MODULE_VERSION "
|
||||||
"declaration.\n", path);
|
"declaration.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
if (license == NULL) {
|
if (license == NULL) {
|
||||||
DEBUG_FUNCTION_LINE(
|
DEBUG_FUNCTION_LINE(
|
||||||
"Warning: Ignoring '%s' - Missing WUPS_MODULE_LICENSE "
|
"Warning: Ignoring '%s' - Missing WUPS_MODULE_LICENSE "
|
||||||
"declaration.\n", path);
|
"declaration.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = (module_metadata_t *) malloc(sizeof(module_metadata_t) + strlen(path) +
|
ret = (module_metadata_t *)malloc(
|
||||||
strlen(name) + strlen(author) +
|
sizeof(module_metadata_t) + strlen(path) +
|
||||||
|
strlen(game) + strlen(name) + strlen(author) +
|
||||||
strlen(version) + strlen(license) + 6);
|
strlen(version) + strlen(license) + 6);
|
||||||
if (ret == NULL) {
|
if (ret == NULL) {
|
||||||
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Couldn't parse BSlug metadata.\n", path);
|
DEBUG_FUNCTION_LINE("Warning: Ignoring '%s' - Couldn't parse BSlug metadata.\n", path);
|
||||||
//module_has_info = 1;
|
module_has_info = true;
|
||||||
goto exit_error;
|
goto exit_error;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -518,10 +479,9 @@ static module_information_t *Module_ModuleInformationRead(const char *path, Elf
|
|||||||
ret->size = 0;
|
ret->size = 0;
|
||||||
ret->entries_count = entries_count;
|
ret->entries_count = entries_count;
|
||||||
|
|
||||||
result->metadata = ret;
|
|
||||||
exit_error:
|
exit_error:
|
||||||
if (metadata != NULL)
|
if (metadata != NULL)
|
||||||
free(metadata);
|
free(metadata);
|
||||||
|
|
||||||
return result;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,6 @@ extern "C" {
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
const char *path;
|
const char *path;
|
||||||
const char *game;
|
|
||||||
const char *name;
|
const char *name;
|
||||||
const char *author;
|
const char *author;
|
||||||
const char *version;
|
const char *version;
|
||||||
@ -27,6 +26,7 @@ typedef struct {
|
|||||||
} module_metadata_t;
|
} module_metadata_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
size_t module;
|
||||||
const char *name;
|
const char *name;
|
||||||
void *address;
|
void *address;
|
||||||
size_t offset;
|
size_t offset;
|
||||||
@ -34,14 +34,15 @@ typedef struct {
|
|||||||
int addend;
|
int addend;
|
||||||
} module_unresolved_relocation_t;
|
} module_unresolved_relocation_t;
|
||||||
|
|
||||||
typedef struct {
|
extern bool module_has_error;
|
||||||
std::vector<module_unresolved_relocation_t *> rel;
|
/* whether or not to delay loading for debug messages. */
|
||||||
std::vector<wups_loader_entry_t *> entries;
|
extern bool module_has_info;
|
||||||
module_metadata_t * metadata;
|
|
||||||
} module_information_t;
|
extern size_t module_list_size;
|
||||||
|
extern module_metadata_t **module_list;
|
||||||
|
extern size_t module_list_count;
|
||||||
|
|
||||||
bool Module_CheckFile(const char *path);
|
bool Module_CheckFile(const char *path);
|
||||||
void printModuleInformation(module_information_t* module_information);
|
void Module_Load(const char *path);
|
||||||
module_information_t* Module_LoadModuleInformation(const char *path);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -38,3 +38,32 @@ void dumpHex(const void* data, size_t size) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *Module_ListAllocate(void *list, size_t entry_size, size_t num, size_t *capacity, size_t *count, size_t default_capacity) {
|
||||||
|
void *result;
|
||||||
|
|
||||||
|
while (*capacity < *count + num) {
|
||||||
|
if (*count == 0) {
|
||||||
|
*(void **)list = malloc(entry_size * default_capacity);
|
||||||
|
|
||||||
|
if (*(void **)list == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
*capacity = default_capacity;
|
||||||
|
} else {
|
||||||
|
void *temp;
|
||||||
|
|
||||||
|
temp = realloc(*(void **)list, entry_size * *capacity * 2);
|
||||||
|
if (temp == NULL)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
*(void **)list = temp;
|
||||||
|
*capacity = *capacity * 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result = *(char **)list + entry_size * *count;
|
||||||
|
(*count) += num;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
@ -12,4 +12,6 @@ void dumpHex(const void* data, size_t size);
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
void *Module_ListAllocate(void *list, size_t entry_size, size_t num,size_t *capacity, size_t *count, size_t default_capacity);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user