Create an Elf collection in box86context

This commit is contained in:
ptitSeb 2018-12-15 12:52:32 +01:00
parent e597307c96
commit 77530530f8
5 changed files with 32 additions and 6 deletions

View File

@ -3,6 +3,8 @@
#include <string.h>
#include "box86context.h"
#include "elfloader.h"
#include "debug.h"
box86context_t *NewBox86Context(int argc)
{
@ -43,7 +45,24 @@ void FreeBox86Context(box86context_t** context)
free((*context)->argv[i]);
free((*context)->argv);
for(int i=0; i<(*context)->elfsize; ++i) {
FreeElfHeader(&(*context)->elfs[i]);
}
free((*context)->elfs);
free(*context);
*context = NULL;
}
int AddElfHeader(box86context_t* ctx, elfheader_t* head) {
int idx = ctx->elfsize;
if(idx==ctx->elfcap) {
// resize...
ctx->elfcap += 16;
ctx->elfs = (elfheader_t**)realloc(ctx->elfs, sizeof(elfheader_t*) * ctx->elfcap);
}
ctx->elfs[idx] = head;
ctx->elfsize++;
printf_debug(DEBUG_DEBUG, "Adding \"%s\" as #%d in elf collection\n", ElfName(head), idx);
return idx;
}

View File

@ -16,8 +16,9 @@ typedef struct {
int stackalign;
void* stack; // alocated stack
elfheader_t *elfs; // elf headers and memory
int elfcount; // number of elf loaded
elfheader_t **elfs; // elf headers and memory
int elfcap;
int elfsize; // number of elf loaded
} box86context_t;
@ -25,4 +26,6 @@ box86context_t *NewBox86Context(int argc);
box86context_t *CopyBox86Context(box86context_t* from);
void FreeBox86Context(box86context_t** context);
int AddElfHeader(box86context_t* ctx, elfheader_t* head); // return the index of header
#endif //__BOX86CONTEXT_H_

View File

@ -250,6 +250,11 @@ int CalcLoadAddr(elfheader_t* head)
return 0;
}
const char* ElfName(elfheader_t* head)
{
return head->name;
}
int AllocElfMemory(elfheader_t* head)
{
printf_debug(DEBUG_DEBUG, "Allocating memory for Elf \"%s\"\n", head->name);

View File

@ -6,6 +6,7 @@ typedef struct elfheader_s elfheader_t;
void* LoadAndCheckElfHeader(FILE* f, const char* name, int exec); // exec : 0 = lib, 1 = exec
void FreeElfHeader(elfheader_t** head);
const char* ElfName(elfheader_t* head);
int CalcLoadAddr(elfheader_t* head); // return 0 if OK

View File

@ -111,10 +111,10 @@ int main(int argc, const char **argv) {
FreeBox86Context(&context);
return -1;
}
int mainelf = AddElfHeader(context, elf_header);
if(CalcLoadAddr(elf_header)) {
printf_debug(DEBUG_NONE, "Error, reading elf header of %s\n", context->argv[0]);
FreeElfHeader(&elf_header);
fclose(f);
FreeBox86Context(&context);
return -1;
@ -122,12 +122,12 @@ int main(int argc, const char **argv) {
// allocate memory
if(AllocElfMemory(elf_header)) {
printf_debug(DEBUG_NONE, "Error, allocating memory for elf %s\n", context->argv[0]);
FreeElfHeader(&elf_header);
fclose(f);
FreeBox86Context(&context);
return -1;
}
// Load elf into memory and relocate
fclose(f);
// Call librarian to load all dependant elf
// finalize relocations
// get stack size and align
@ -136,8 +136,6 @@ int main(int argc, const char **argv) {
// emulate!
fclose(f);
FreeElfHeader(&elf_header);
// all done, free context
FreeBox86Context(&context);