about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorptitSeb <sebastien.chev@gmail.com>2021-03-02 10:32:13 +0100
committerptitSeb <sebastien.chev@gmail.com>2021-03-02 10:32:13 +0100
commit542a2a0775e5fe2921ce7893a9c1d77915a87bc4 (patch)
tree352d37a3f4b033d33dd1b8d62cfdc23cc7f4e85c /src
parenta2a78a4edc2e82756d83daf8bbbe2ca26102af47 (diff)
downloadbox64-542a2a0775e5fe2921ce7893a9c1d77915a87bc4.tar.gz
box64-542a2a0775e5fe2921ce7893a9c1d77915a87bc4.zip
Detecting tcmalloc now
Diffstat (limited to 'src')
-rwxr-xr-xsrc/elfs/elfloader.c4
-rwxr-xr-xsrc/include/elfloader.h2
-rwxr-xr-xsrc/main.c59
3 files changed, 55 insertions, 10 deletions
diff --git a/src/elfs/elfloader.c b/src/elfs/elfloader.c
index ebb68138..f1d4cf39 100755
--- a/src/elfs/elfloader.c
+++ b/src/elfs/elfloader.c
@@ -874,7 +874,7 @@ int LoadNeededLibs(elfheader_t* h, lib_t *maplib, needed_libs_t* neededlibs, int
         }
     return 0;
 }
-
+#endif
 int ElfCheckIfUseTCMallocMinimal(elfheader_t* h)
 {
     if(!h)
@@ -889,7 +889,7 @@ int ElfCheckIfUseTCMallocMinimal(elfheader_t* h)
         }
     return 0;
 }
-
+#if 0
 void RunElfInit(elfheader_t* h, x86emu_t *emu)
 {
     if(!h || h->init_done)
diff --git a/src/include/elfloader.h b/src/include/elfloader.h
index d8caa27b..2046865d 100755
--- a/src/include/elfloader.h
+++ b/src/include/elfloader.h
@@ -53,7 +53,7 @@ int ReloadElfMemory(FILE* f, box64context_t* context, elfheader_t* head);
 //void ResetSpecialCaseMainElf(elfheader_t* h);
 //void CreateMemorymapFile(box64context_t* context, int fd);
 
-//int ElfCheckIfUseTCMallocMinimal(elfheader_t* h);   // return 1 if tcmalloc is used
+int ElfCheckIfUseTCMallocMinimal(elfheader_t* h);   // return 1 if tcmalloc is used
 
 
 #endif //__ELF_LOADER_H_
\ No newline at end of file
diff --git a/src/main.c b/src/main.c
index 1e513351..90f513fd 100755
--- a/src/main.c
+++ b/src/main.c
@@ -328,16 +328,16 @@ int GatherEnv(char*** dest, const char** env, const char* prog)
 
 void PrintHelp() {
     printf("\n\nThis is Box86, the Linux x86 emulator with a twist\n");
-    printf("\nUsage is box86 [options] path/to/software [args]\n");
+    printf("\nUsage is box64 [options] path/to/software [args]\n");
     printf("to launch x86 software\n");
     printf(" options can be :\n");
-    printf("    '-v'|'--version' to print box86 version and quit\n");
-    printf("    '-h'|'--help'    to print box86 help and quit\n");
+    printf("    '-v'|'--version' to print box64 version and quit\n");
+    printf("    '-h'|'--help'    to print box64 help and quit\n");
     printf("You can also set some environment variables:\n");
-    printf(" BOX64_PATH is the box86 version of PATH (default is '.:bin')\n");
-    printf(" BOX64_LD_LIBRARY_PATH is the box86 version LD_LIBRARY_PATH (default is '.:lib')\n");
+    printf(" BOX64_PATH is the box64 version of PATH (default is '.:bin')\n");
+    printf(" BOX64_LD_LIBRARY_PATH is the box64 version LD_LIBRARY_PATH (default is '.:lib')\n");
     printf(" BOX64_LOG with 0/1/2/3 or NONE/INFO/DEBUG/DUMP to set the printed debug info\n");
-    printf(" BOX64_NOBANNER with 0/1 to enable/disable the printing of box86 version and build at start\n");
+    printf(" BOX64_NOBANNER with 0/1 to enable/disable the printing of box64 version and build at start\n");
 #ifdef HAVE_TRACE
     printf(" BOX64_TRACE with 1 to enable x86 execution trace\n");
     printf("    or with XXXXXX-YYYYYY to enable x86 execution trace only between address\n");
@@ -761,7 +761,52 @@ int main(int argc, const char **argv, const char **env) {
     }
     // can close the file now
     fclose(f);
-
+    if(ElfCheckIfUseTCMallocMinimal(elf_header)) {
+        if(!box64_tcmalloc_minimal) {
+            // need to reload with tcmalloc_minimal as a LD_PRELOAD!
+            printf_log(LOG_INFO, "BOX86: tcmalloc_minimal.so.4 used, reloading box64 with the lib preladed\n");
+            // need to get a new envv variable. so first count it and check if LD_PRELOAD is there
+            int preload=(getenv("LD_PRELOAD"))?1:0;
+            int nenv = 0;
+            while(env[nenv]) nenv++;
+            // alloc + "LD_PRELOAD" if needd + last NULL ending
+            char** newenv = (char**)calloc(nenv+1+((preload)?0:1), sizeof(char*));
+            // copy strings
+            for (int i=0; i<nenv; ++i)
+                newenv[i] = strdup(env[i]);
+            // add ld_preload
+            if(preload) {
+                // find the line
+                int l = 0;
+                while(l<nenv) {
+                    if(strstr(newenv[l], "LD_PRELOAD=")==newenv[l]) {
+                        // found it!
+                        char *old = newenv[l];
+                        newenv[l] = (char*)calloc(strlen(old)+strlen("libtcmalloc_minimal.so.4:")+1, sizeof(char));
+                        strcpy(newenv[l], "LD_PRELOAD=libtcmalloc_minimal.so.4:");
+                        strcat(newenv[l], old + strlen("LD_PRELOAD="));
+                        free(old);
+                        // done, end loop
+                        l = nenv;
+                    } else ++l;
+                }
+            } else {
+                //move last one
+                newenv[nenv] = strdup(newenv[nenv-1]);
+                free(newenv[nenv-1]);
+                newenv[nenv-1] = strdup("LD_PRELOAD=libtcmalloc_minimal.so.4");
+            }
+            // duplicate argv too
+            char** newargv = calloc(argc+1, sizeof(char*));
+            int narg = 0;
+            while(argv[narg]) {newargv[narg] = strdup(argv[narg]); narg++;}
+            // launch with new env...
+            if(execve(newargv[0], newargv, newenv)<0)
+                printf_log(LOG_NONE, "Failed to relaunch, error is %d/%s\n", errno, strerror(errno));
+        } else {
+            printf_log(LOG_INFO, "BOX86: Using tcmalloc_minimal.so.4, and it's in the LD_PRELOAD command\n");
+        }
+    }
 
     return 0;
 }