Skip to content

Commit

Permalink
Large refactoring of elfloader
Browse files Browse the repository at this point in the history
  • Loading branch information
ptitSeb committed May 27, 2021
1 parent 213f51c commit 5c8d387
Show file tree
Hide file tree
Showing 29 changed files with 1,084 additions and 499 deletions.
21 changes: 11 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ set(ELFLOADER_SRC
"${BOX86_ROOT}/src/elfs/elfload_dump.c"
"${BOX86_ROOT}/src/librarian/library.c"
"${BOX86_ROOT}/src/librarian/librarian.c"
"${BOX86_ROOT}/src/librarian/dictionnary.c"
"${BOX86_ROOT}/src/librarian/symbols.c"
"${BOX86_ROOT}/src/emu/x86emu.c"
"${BOX86_ROOT}/src/emu/x86run.c"
"${BOX86_ROOT}/src/emu/x86run66.c"
Expand Down Expand Up @@ -647,16 +649,15 @@ add_test(NAME conditionalThreads COMMAND ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAK
-D TEST_REFERENCE=${CMAKE_SOURCE_DIR}/tests/ref14.txt
-P ${CMAKE_SOURCE_DIR}/runTest.cmake )

# WIP
#add_test(NAME linkingIndirectNoversion COMMAND ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/${BOX86}
# -D TEST_ARGS=${CMAKE_SOURCE_DIR}/tests/test15 -D TEST_OUTPUT=tmpfile.txt
# -D TEST_REFERENCE=${CMAKE_SOURCE_DIR}/tests/ref15.txt
# -P ${CMAKE_SOURCE_DIR}/runTest.cmake )
#
#add_test(NAME linkingIndirectVersion COMMAND ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/${BOX86}
# -D TEST_ARGS=${CMAKE_SOURCE_DIR}/tests/test16 -D TEST_OUTPUT=tmpfile.txt
# -D TEST_REFERENCE=${CMAKE_SOURCE_DIR}/tests/ref16.txt
# -P ${CMAKE_SOURCE_DIR}/runTest.cmake )
add_test(NAME linkingIndirectNoversion COMMAND ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/${BOX86}
-D TEST_ARGS=${CMAKE_SOURCE_DIR}/tests/test15 -D TEST_OUTPUT=tmpfile.txt
-D TEST_REFERENCE=${CMAKE_SOURCE_DIR}/tests/ref15.txt
-P ${CMAKE_SOURCE_DIR}/runTest.cmake )

add_test(NAME linkingIndirectVersion COMMAND ${CMAKE_COMMAND} -D TEST_PROGRAM=${CMAKE_BINARY_DIR}/${BOX86}
-D TEST_ARGS=${CMAKE_SOURCE_DIR}/tests/test16 -D TEST_OUTPUT=tmpfile.txt
-D TEST_REFERENCE=${CMAKE_SOURCE_DIR}/tests/ref16.txt
-P ${CMAKE_SOURCE_DIR}/runTest.cmake )

file(GLOB extension_tests "${CMAKE_SOURCE_DIR}/tests/extensions/*.c")
foreach(file ${extension_tests})
Expand Down
15 changes: 9 additions & 6 deletions src/box86context.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "signals.h"
#include <sys/mman.h>
#include "custommem.h"
#include "dictionnary.h"
#ifdef DYNAREC
#include "dynablock.h"
#include "dynarec/arm_lock_helper.h"
Expand Down Expand Up @@ -129,10 +130,10 @@ static void init_mutexes(box86context_t* context)
pthread_mutex_init(&context->mutex_once, &attr);
pthread_mutex_init(&context->mutex_once2, &attr);
pthread_mutex_init(&context->mutex_trace, &attr);
#ifndef DYNAREC
pthread_mutex_init(&context->mutex_lock, &attr);
#else
#ifdef DYNAREC
pthread_mutex_init(&context->mutex_dyndump, &attr);
#else
pthread_mutex_init(&context->mutex_lock, &attr);
#endif
pthread_mutex_init(&context->mutex_tls, &attr);
pthread_mutex_init(&context->mutex_thread, &attr);
Expand Down Expand Up @@ -169,6 +170,7 @@ box86context_t *NewBox86Context(int argc)

context->maplib = NewLibrarian(context, 1);
context->local_maplib = NewLibrarian(context, 1);
context->versym = NewDictionnary();
context->system = NewBridge();
// create vsyscall
context->vsyscall = AddBridge(context->system, vFv, x86Syscall, 0);
Expand Down Expand Up @@ -217,6 +219,7 @@ void FreeBox86Context(box86context_t** context)
FreeLibrarian(&ctx->local_maplib, NULL);
if(ctx->maplib)
FreeLibrarian(&ctx->maplib, NULL);
FreeDictionnary(&ctx->versym);

for(int i=0; i<ctx->elfsize; ++i) {
FreeElfHeader(&ctx->elfs[i]);
Expand Down Expand Up @@ -295,10 +298,10 @@ void FreeBox86Context(box86context_t** context)
pthread_mutex_destroy(&ctx->mutex_once);
pthread_mutex_destroy(&ctx->mutex_once2);
pthread_mutex_destroy(&ctx->mutex_trace);
#ifndef DYNAREC
pthread_mutex_destroy(&ctx->mutex_lock);
#else
#ifdef DYNAREC
pthread_mutex_destroy(&ctx->mutex_dyndump);
#else
pthread_mutex_destroy(&ctx->mutex_lock);
#endif
pthread_mutex_destroy(&ctx->mutex_tls);
pthread_mutex_destroy(&ctx->mutex_thread);
Expand Down
4 changes: 2 additions & 2 deletions src/dynarec/dynablock.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ dynablock_t *AddNewDynablock(dynablocklist_t* dynablocks, uintptr_t addr, int* c
if(dynablocks->direct) {
block = dynablocks->direct[addr-dynablocks->text];
if(block) {
dynarec_log(LOG_DUMP, "Block already exist in Direct Map\n");
dynarec_log(LOG_VERBOSE, "Block already exist in Direct Map\n");
*created = 0;
return block;
}
Expand All @@ -274,7 +274,7 @@ dynablock_t *AddNewDynablock(dynablocklist_t* dynablocks, uintptr_t addr, int* c
}

// create and add new block
dynarec_log(LOG_DUMP, "Ask for DynaRec Block creation @%p\n", (void*)addr);
dynarec_log(LOG_VERBOSE, "Ask for DynaRec Block creation @%p\n", (void*)addr);

block = (dynablock_t*)calloc(1, sizeof(dynablock_t));
block->parent = dynablocks;
Expand Down
121 changes: 62 additions & 59 deletions src/elfs/elfload_dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,14 @@ const char* DumpRelType(int t)
return buff;
}

const char* DumpSym(elfheader_t *h, Elf32_Sym* sym)
const char* DumpSym(elfheader_t *h, Elf32_Sym* sym, int version)
{
static char buff[4096];
const char* vername = (version==-1)?"(none)":((version==0)?"*local*":((version==1)?"*global*":GetSymbolVersion(h, version)));
memset(buff, 0, sizeof(buff));
sprintf(buff, "\"%s\", value=%p, size=%d, info/other=%d/%d index=%d",
sprintf(buff, "\"%s\", value=%p, size=%d, info/other=%d/%d index=%d (ver=%d/%s)",
h->DynStr+sym->st_name, (void*)sym->st_value, sym->st_size,
sym->st_info, sym->st_other, sym->st_shndx);
sym->st_info, sym->st_other, sym->st_shndx, version, vername);
return buff;
}

Expand All @@ -221,116 +222,118 @@ const char* IdxSymName(elfheader_t *h, int sym)

void DumpMainHeader(Elf32_Ehdr *header, elfheader_t *h)
{
if(box86_log>=LOG_DUMP) {
printf_log(LOG_DUMP, "ELF Dump main header\n");
printf_log(LOG_DUMP, " Entry point = %p\n", (void*)header->e_entry);
printf_log(LOG_DUMP, " Program Header table offset = %p\n", (void*)header->e_phoff);
printf_log(LOG_DUMP, " Section Header table offset = %p\n", (void*)header->e_shoff);
printf_log(LOG_DUMP, " Flags = 0x%X\n", header->e_flags);
printf_log(LOG_DUMP, " ELF Header size = %d\n", header->e_ehsize);
printf_log(LOG_DUMP, " Program Header Entry num/size = %d(%d)/%d\n", h->numPHEntries, header->e_phnum, header->e_phentsize);
printf_log(LOG_DUMP, " Section Header Entry num/size = %d(%d)/%d\n", h->numSHEntries, header->e_shnum, header->e_shentsize);
printf_log(LOG_DUMP, " Section Header index num = %d(%d)\n", h->SHIdx, header->e_shstrndx);
printf_log(LOG_DUMP, "ELF Dump ==========\n");
if(box86_dump) {
printf_dump(LOG_NEVER, "ELF Dump main header\n");
printf_dump(LOG_NEVER, " Entry point = %p\n", (void*)header->e_entry);
printf_dump(LOG_NEVER, " Program Header table offset = %p\n", (void*)header->e_phoff);
printf_dump(LOG_NEVER, " Section Header table offset = %p\n", (void*)header->e_shoff);
printf_dump(LOG_NEVER, " Flags = 0x%X\n", header->e_flags);
printf_dump(LOG_NEVER, " ELF Header size = %d\n", header->e_ehsize);
printf_dump(LOG_NEVER, " Program Header Entry num/size = %d(%d)/%d\n", h->numPHEntries, header->e_phnum, header->e_phentsize);
printf_dump(LOG_NEVER, " Section Header Entry num/size = %d(%d)/%d\n", h->numSHEntries, header->e_shnum, header->e_shentsize);
printf_dump(LOG_NEVER, " Section Header index num = %d(%d)\n", h->SHIdx, header->e_shstrndx);
printf_dump(LOG_NEVER, "ELF Dump ==========\n");

printf_log(LOG_DUMP, "ELF Dump PEntries (%d)\n", h->numSHEntries);
printf_dump(LOG_NEVER, "ELF Dump PEntries (%d)\n", h->numSHEntries);
for (int i=0; i<h->numPHEntries; ++i)
printf_log(LOG_DUMP, " PHEntry %04d : %s\n", i, DumpPHEntry(h->PHEntries+i));
printf_log(LOG_DUMP, "ELF Dump PEntries ====\n");
printf_dump(LOG_NEVER, " PHEntry %04d : %s\n", i, DumpPHEntry(h->PHEntries+i));
printf_dump(LOG_NEVER, "ELF Dump PEntries ====\n");

printf_log(LOG_DUMP, "ELF Dump Sections (%d)\n", h->numSHEntries);
printf_dump(LOG_NEVER, "ELF Dump Sections (%d)\n", h->numSHEntries);
for (int i=0; i<h->numSHEntries; ++i)
printf_log(LOG_DUMP, " Section %04d : %s\n", i, DumpSection(h->SHEntries+i, h->SHStrTab));
printf_log(LOG_DUMP, "ELF Dump Sections ====\n");
printf_dump(LOG_NEVER, " Section %04d : %s\n", i, DumpSection(h->SHEntries+i, h->SHStrTab));
printf_dump(LOG_NEVER, "ELF Dump Sections ====\n");
}
}

void DumpSymTab(elfheader_t *h)
{
if(box86_log>=LOG_DUMP && h->SymTab) {
if(box86_dump && h->SymTab) {
const char* name = ElfName(h);
printf_log(LOG_DUMP, "ELF Dump SymTab(%d)=\n", h->numSymTab);
printf_dump(LOG_NEVER, "ELF Dump SymTab(%d)=\n", h->numSymTab);
for (int i=0; i<h->numSymTab; ++i)
printf_log(LOG_DUMP, " %s:SymTab[%d] = \"%s\", value=%p, size=%d, info/other=%d/%d index=%d\n", name,
printf_dump(LOG_NEVER, " %s:SymTab[%d] = \"%s\", value=%p, size=%d, info/other=%d/%d index=%d\n", name,
i, h->StrTab+h->SymTab[i].st_name, (void*)h->SymTab[i].st_value, h->SymTab[i].st_size,
h->SymTab[i].st_info, h->SymTab[i].st_other, h->SymTab[i].st_shndx);
printf_log(LOG_DUMP, "ELF Dump SymTab=====\n");
printf_dump(LOG_NEVER, "ELF Dump SymTab=====\n");
}
}

void DumpDynamicSections(elfheader_t *h)
{
if(box86_log>=LOG_DUMP && h->Dynamic) {
printf_log(LOG_DUMP, "ELF Dump Dynamic(%d)=\n", h->numDynamic);
if(box86_dump && h->Dynamic) {
printf_dump(LOG_NEVER, "ELF Dump Dynamic(%d)=\n", h->numDynamic);
for (int i=0; i<h->numDynamic; ++i)
printf_log(LOG_DUMP, " Dynamic %04d : %s\n", i, DumpDynamic(h->Dynamic+i));
printf_log(LOG_DUMP, "ELF Dump Dynamic=====\n");
printf_dump(LOG_NEVER, " Dynamic %04d : %s\n", i, DumpDynamic(h->Dynamic+i));
printf_dump(LOG_NEVER, "ELF Dump Dynamic=====\n");
}
}

void DumpDynSym(elfheader_t *h)
{
if(box86_log>=LOG_DUMP && h->DynSym) {
if(box86_dump && h->DynSym) {
const char* name = ElfName(h);
printf_log(LOG_DUMP, "ELF Dump DynSym(%d)=\n", h->numDynSym);
for (int i=0; i<h->numDynSym; ++i)
printf_log(LOG_DUMP, " %s:DynSym[%d] = %s\n", name, i, DumpSym(h, h->DynSym+i));
printf_log(LOG_DUMP, "ELF Dump DynSym=====\n");
printf_dump(LOG_NEVER, "ELF Dump DynSym(%d)=\n", h->numDynSym);
for (int i=0; i<h->numDynSym; ++i) {
int version = h->VerSym?((Elf32_Half*)((uintptr_t)h->VerSym+h->delta))[i]:-1;
printf_dump(LOG_NEVER, " %s:DynSym[%d] = %s\n", name, i, DumpSym(h, h->DynSym+i, version));
}
printf_dump(LOG_NEVER, "ELF Dump DynSym=====\n");
}
}

void DumpDynamicNeeded(elfheader_t *h)
{
if(box86_log>=LOG_DUMP && h->DynStrTab) {
printf_log(LOG_DUMP, "ELF Dump DT_NEEDED=====\n");
if(box86_dump && h->DynStrTab) {
printf_dump(LOG_NEVER, "ELF Dump DT_NEEDED=====\n");
for (int i=0; i<h->numDynamic; ++i)
if(h->Dynamic[i].d_tag==DT_NEEDED) {
printf_log(LOG_DUMP, " Needed : %s\n", h->DynStrTab+h->Dynamic[i].d_un.d_val + h->delta);
printf_dump(LOG_NEVER, " Needed : %s\n", h->DynStrTab+h->Dynamic[i].d_un.d_val + h->delta);
}
printf_log(LOG_DUMP, "ELF Dump DT_NEEDED=====\n");
printf_dump(LOG_NEVER, "ELF Dump DT_NEEDED=====\n");
}
}

void DumpDynamicRPath(elfheader_t *h)
{
if(box86_log>=LOG_DUMP && h->DynStrTab) {
printf_log(LOG_DUMP, "ELF Dump DT_RPATH/DT_RUNPATH=====\n");
if(box86_dump && h->DynStrTab) {
printf_dump(LOG_NEVER, "ELF Dump DT_RPATH/DT_RUNPATH=====\n");
for (int i=0; i<h->numDynamic; ++i) {
if(h->Dynamic[i].d_tag==DT_RPATH) {
printf_log(LOG_DUMP, " RPATH : %s\n", h->DynStrTab+h->Dynamic[i].d_un.d_val + h->delta);
printf_dump(LOG_NEVER, " RPATH : %s\n", h->DynStrTab+h->Dynamic[i].d_un.d_val + h->delta);
}
if(h->Dynamic[i].d_tag==DT_RUNPATH) {
printf_log(LOG_DUMP, " RUNPATH : %s\n", h->DynStrTab+h->Dynamic[i].d_un.d_val + h->delta);
printf_dump(LOG_NEVER, " RUNPATH : %s\n", h->DynStrTab+h->Dynamic[i].d_un.d_val + h->delta);
}
}
printf_log(LOG_DUMP, "=====ELF Dump DT_RPATH/DT_RUNPATH\n");
printf_dump(LOG_NEVER, "=====ELF Dump DT_RPATH/DT_RUNPATH\n");
}
}

void DumpRelTable(elfheader_t *h, int cnt, Elf32_Rel *rel, const char* name)
{
if(box86_log>=LOG_DUMP) {
if(box86_dump) {
const char* elfname = ElfName(h);
printf_log(LOG_DUMP, "ELF Dump %s Table(%d) @%p\n", name, cnt, rel);
printf_dump(LOG_NEVER, "ELF Dump %s Table(%d) @%p\n", name, cnt, rel);
for (int i = 0; i<cnt; ++i)
printf_log(LOG_DUMP, " %s:Rel[%d] = %p (0x%X: %s, sym=0x%0X/%s)\n", elfname,
printf_dump(LOG_NEVER, " %s:Rel[%d] = %p (0x%X: %s, sym=0x%0X/%s)\n", elfname,
i, (void*)rel[i].r_offset, rel[i].r_info, DumpRelType(ELF32_R_TYPE(rel[i].r_info)),
ELF32_R_SYM(rel[i].r_info), IdxSymName(h, ELF32_R_SYM(rel[i].r_info)));
printf_log(LOG_DUMP, "ELF Dump Rel Table=====\n");
printf_dump(LOG_NEVER, "ELF Dump Rel Table=====\n");
}
}

void DumpRelATable(elfheader_t *h, int cnt, Elf32_Rela *rela, const char* name)
{
if(box86_log>=LOG_DUMP && h->rela) {
if(box86_dump && h->rela) {
const char* elfname = ElfName(h);
printf_log(LOG_DUMP, "ELF Dump %s Table(%d) @%p\n", name, cnt, rela);
printf_dump(LOG_NEVER, "ELF Dump %s Table(%d) @%p\n", name, cnt, rela);
for (int i = 0; i<cnt; ++i)
printf_log(LOG_DUMP, " %s:RelA[%d] = %p (0x%X: %s, sym=0x%X/%s) Addend=%d\n", elfname,
printf_dump(LOG_NEVER, " %s:RelA[%d] = %p (0x%X: %s, sym=0x%X/%s) Addend=%d\n", elfname,
i, (void*)rela[i].r_offset, rela[i].r_info, DumpRelType(ELF32_R_TYPE(rela[i].r_info)),
ELF32_R_SYM(rela[i].r_info), IdxSymName(h, ELF32_R_SYM(rela[i].r_info)),
rela[i].r_addend);
printf_log(LOG_DUMP, "ELF Dump RelA Table=====\n");
printf_dump(LOG_NEVER, "ELF Dump RelA Table=====\n");
}
}

Expand All @@ -341,24 +344,24 @@ void DumpBinary(char* p, int sz)
unsigned char* d = (unsigned char*)p;
int delta = ((uintptr_t)p)&0xf;
for (int i = 0; sz; ++i) {
printf_log(LOG_DUMP, "%p ", (void*)(((uintptr_t)d)&~0xf));
printf_dump(LOG_NEVER, "%p ", (void*)(((uintptr_t)d)&~0xf));
int n = 16 - delta;
if (n>sz) n = sz;
int fill = 16-sz;
for (int j = 0; j<delta; ++j)
printf_log(LOG_DUMP, " ");
printf_dump(LOG_NEVER, " ");
for (int j = 0; j<n; ++j)
printf_log(LOG_DUMP, "%02X ", d[j]);
printf_dump(LOG_NEVER, "%02X ", d[j]);
for (int j = 0; j<fill; ++j)
printf_log(LOG_DUMP, " ");
printf_log(LOG_DUMP, " | ");
printf_dump(LOG_NEVER, " ");
printf_dump(LOG_NEVER, " | ");
for (int j = 0; j<delta; ++j)
printf_log(LOG_DUMP, " ");
printf_dump(LOG_NEVER, " ");
for (int j = 0; j<n; ++j)
printf_log(LOG_DUMP, "%c", (d[j]<32 || d[j]>127)?'.':d[j]);
printf_dump(LOG_NEVER, "%c", (d[j]<32 || d[j]>127)?'.':d[j]);
for (int j = 0; j<fill; ++j)
printf_log(LOG_DUMP, " ");
printf_log(LOG_DUMP, "\n");
printf_dump(LOG_NEVER, " ");
printf_dump(LOG_NEVER, "\n");
d+=n;
sz-=n;
delta=0;
Expand Down
Loading

0 comments on commit 5c8d387

Please sign in to comment.