about summary refs log tree commit diff stats
path: root/src/box64context.c
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2023-04-10 18:51:07 +0200
committerptitSeb <sebastien.chev@gmail.com>2023-04-10 18:51:16 +0200
commit5d6307184941f6f2171153df6a5bb0105fcbce9e (patch)
treec0c31a50eac0c58725ba8418154ca48a3c4ae2a9 /src/box64context.c
parent8c98c7b67580b2807d37fa03f59416f4526df770 (diff)
downloadbox64-5d6307184941f6f2171153df6a5bb0105fcbce9e.tar.gz
box64-5d6307184941f6f2171153df6a5bb0105fcbce9e.zip
[ELFLOADER] Improved handling of Failed-to-load library (and unloading of libs too)
Diffstat (limited to 'src/box64context.c')
-rwxr-xr-xsrc/box64context.c38
1 files changed, 31 insertions, 7 deletions
diff --git a/src/box64context.c b/src/box64context.c
index 082831f3..f6ca346a 100755
--- a/src/box64context.c
+++ b/src/box64context.c
@@ -338,20 +338,44 @@ void FreeBox64Context(box64context_t** context)
 }
 
 int AddElfHeader(box64context_t* ctx, elfheader_t* head) {
-    int idx = ctx->elfsize;
-    if(idx==ctx->elfcap) {
-        // resize...
-        ctx->elfcap += 16;
-        ctx->elfs = (elfheader_t**)box_realloc(ctx->elfs, sizeof(elfheader_t*) * ctx->elfcap);
+    int idx = 0;
+    while(idx<ctx->elfsize && ctx->elfs[idx]) idx++;
+    if(idx == ctx->elfsize) {
+        if(idx==ctx->elfcap) {
+            // resize...
+            ctx->elfcap += 16;
+            ctx->elfs = (elfheader_t**)box_realloc(ctx->elfs, sizeof(elfheader_t*) * ctx->elfcap);
+        }
+        ctx->elfs[idx] = head;
+        ctx->elfsize++;
+    } else {
+        ctx->elfs[idx] = head;
     }
-    ctx->elfs[idx] = head;
-    ctx->elfsize++;
     printf_log(LOG_DEBUG, "Adding \"%s\" as #%d in elf collection\n", ElfName(head), idx);
     return idx;
 }
 
+void RemoveElfHeader(box64context_t* ctx, elfheader_t* head) {
+    if(GetTLSBase(head)) {
+        // should remove the tls info
+        int tlsbase = GetTLSBase(head);
+        if(tlsbase == -ctx->tlssize) {
+            // not really correct, but will do for now
+            ctx->tlssize -= GetTLSSize(head);
+            if(!(++ctx->sel_serial))
+                ++ctx->sel_serial;
+        }
+    }
+    for(int i=0; i<ctx->elfsize; ++i)
+        if(ctx->elfs[i] == head) {
+            ctx->elfs[i] = NULL;
+            return;
+        }
+}
+
 int AddTLSPartition(box64context_t* context, int tlssize) {
     int oldsize = context->tlssize;
+    // should in fact first try to map a hole, but rewinding all elfs and checking filled space, like with the mapmem utilities
     context->tlssize += tlssize;
     context->tlsdata = box_realloc(context->tlsdata, context->tlssize);
     memmove(context->tlsdata+tlssize, context->tlsdata, oldsize);   // move to the top, using memmove as regions will probably overlap