about summary refs log tree commit diff stats
path: root/src/elfs
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2023-01-01 16:13:39 +0100
committerptitSeb <sebastien.chev@gmail.com>2023-01-01 16:13:39 +0100
commitee5398b3be5d45ec39e21b503d4ee8023a665141 (patch)
treee44a6f4233c65c22b41d9219772e548613fb7acc /src/elfs
parent3d3ab0fedc2b98f7adb84e898ffb32f24a2a1a6a (diff)
downloadbox64-ee5398b3be5d45ec39e21b503d4ee8023a665141.tar.gz
box64-ee5398b3be5d45ec39e21b503d4ee8023a665141.zip
Refactored (again) lib init/fini mecanism
Diffstat (limited to 'src/elfs')
-rwxr-xr-xsrc/elfs/elfloader.c26
-rwxr-xr-xsrc/elfs/elfloader_private.h7
2 files changed, 19 insertions, 14 deletions
diff --git a/src/elfs/elfloader.c b/src/elfs/elfloader.c
index e0c52008..0a0f0cf2 100755
--- a/src/elfs/elfloader.c
+++ b/src/elfs/elfloader.c
@@ -62,6 +62,7 @@ elfheader_t* LoadAndCheckElfHeader(FILE* f, const char* name, int exec)
     h->mapsymbols = NewMapSymbols();
     h->weaksymbols = NewMapSymbols();
     h->localsymbols = NewMapSymbols();
+    h->refcnt = 1;
     
     return h;
 }
@@ -1059,8 +1060,10 @@ $PLATFORM – Expands to the processor type of the current machine (see the
 uname(1) man page description of the -i option). For more details of this token
 expansion, see “System Specific Shared Objects”
 */
-int LoadNeededLibs(elfheader_t* h, lib_t *maplib, needed_libs_t* neededlibs, library_t *deplib, int local, int bindnow, box64context_t *box64, x64emu_t* emu)
+int LoadNeededLibs(elfheader_t* h, lib_t *maplib, int local, int bindnow, box64context_t *box64, x64emu_t* emu)
 {
+    if(h->needed)   // already done
+        return 0;
     DumpDynamicRPath(h);
     // update RPATH first
     for (size_t i=0; i<h->numDynamic; ++i)
@@ -1119,22 +1122,21 @@ int LoadNeededLibs(elfheader_t* h, lib_t *maplib, needed_libs_t* neededlibs, lib
                 box_free(rpath);
         }
 
-    if(!h->neededlibs && neededlibs)
-        h->neededlibs = neededlibs;
-
     DumpDynamicNeeded(h);
     int cnt = 0;
     for (int i=0; i<h->numDynamic; ++i)
         if(h->Dynamic[i].d_tag==DT_NEEDED)
             ++cnt;
-    const char* nlibs[cnt];
+    h->needed = new_neededlib(cnt);
+    if(h == my_context->elfs[0])
+        my_context->neededlibs = h->needed;
     int j=0;
     for (int i=0; i<h->numDynamic; ++i)
         if(h->Dynamic[i].d_tag==DT_NEEDED)
-            nlibs[j++] = h->DynStrTab+h->delta+h->Dynamic[i].d_un.d_val;
+            h->needed->names[j++] = h->DynStrTab+h->delta+h->Dynamic[i].d_un.d_val;
 
     // TODO: Add LD_LIBRARY_PATH and RPATH handling
-    if(AddNeededLib(maplib, neededlibs, deplib, local, bindnow, nlibs, cnt, box64, emu)) {
+    if(AddNeededLib(maplib, local, bindnow, h->needed, box64, emu)) {
         printf_log(LOG_INFO, "Error loading one of needed lib\n");
         if(!allow_missing_libs)
             return 1;   //error...
@@ -1185,8 +1187,8 @@ void RunElfInitPltResolver(elfheader_t* h, x64emu_t *emu)
         return;
     uintptr_t p = h->initentry + h->delta;
     h->init_done = 1;
-    for(int i=0; i<h->neededlibs->size; ++i) {
-        library_t *lib = h->neededlibs->libs[i];
+    for(int i=0; i<h->needed->size; ++i) {
+        library_t *lib = h->needed->libs[i];
         elfheader_t *lib_elf = GetElf(lib);
         if(lib_elf)
             RunElfInitPltResolver(lib_elf, emu);
@@ -1229,8 +1231,8 @@ void RunElfInit(elfheader_t* h, x64emu_t *emu)
         return;
     }
     h->init_done = 1;
-    for(int i=0; i<h->neededlibs->size; ++i) {
-        library_t *lib = h->neededlibs->libs[i];
+    for(int i=0; i<h->needed->size; ++i) {
+        library_t *lib = h->needed->libs[i];
         elfheader_t *lib_elf = GetElf(lib);
         if(lib_elf)
             RunElfInit(lib_elf, emu);
@@ -1289,6 +1291,8 @@ void RunElfFini(elfheader_t* h, x64emu_t *emu)
         RunFunctionWithEmu(emu, 0, p, 0);
     }
     h->init_done = 0;   // can be re-inited again...
+    for(int i=0; i<h->needed->size; ++i)
+        FiniLibrary(h->needed->libs[i], emu);
     return;
 }
 
diff --git a/src/elfs/elfloader_private.h b/src/elfs/elfloader_private.h
index eb5e81af..c4637535 100755
--- a/src/elfs/elfloader_private.h
+++ b/src/elfs/elfloader_private.h
@@ -83,15 +83,16 @@ struct elfheader_s {
 
     int         init_done;
     int         fini_done;
+    int         refcnt;     // ref count for the elf
 
-    char*       memory; // char* and not void* to allow math on memory pointer
+    char*       memory;     // char* and not void* to allow math on memory pointer
     void**      multiblock;
     uintptr_t*  multiblock_offs;
     uint64_t*   multiblock_size;
     int         multiblock_n;
 
-    library_t   *lib;
-    needed_libs_t *neededlibs;
+    library_t   *lib;       // attached lib (exept on main elf)
+    needed_libs_t* needed;
 
     kh_mapsymbols_t   *mapsymbols;
     kh_mapsymbols_t   *weaksymbols;