vnmakarov / mir

A lightweight JIT compiler based on MIR (Medium Internal Representation) and C11 JIT compiler and interpreter based on MIR
MIT License
2.31k stars 148 forks source link

hardcoded GLIBC library names #16

Open rofl0r opened 4 years ago

rofl0r commented 4 years ago

currently i use the following hack so mir works on my musl-libc based linux distro:

diff --git a/c2mir/c2mir.c b/c2mir/c2mir.c
index 6659bf6..483b4d2 100644
--- a/c2mir/c2mir.c
+++ b/c2mir/c2mir.c
@@ -11877,7 +11877,13 @@ static int fancy_printf (const char *fmt, ...) { abort (); }
 static struct lib {
   char *name;
   void *handler;
-} libs[] = {{"/lib64/libc.so.6", NULL}, {"/lib64/libm.so.6", NULL}};
+} libs[] = {
+#ifdef __GLIBC__
+{"/lib64/libc.so.6", NULL}, {"/lib64/libm.so.6", NULL}
+#else
+{"/lib/libc.so", NULL},
+#endif
+};

 static void close_libs (void) {
   for (int i = 0; i < sizeof (libs) / sizeof (struct lib); i++)
diff --git a/mir-bin-driver.c b/mir-bin-driver.c
index 9f86915..6abb3f8 100644
--- a/mir-bin-driver.c
+++ b/mir-bin-driver.c
@@ -20,7 +20,13 @@ static int read_byte (MIR_context_t ctx) {
 static struct lib {
   char *name;
   void *handler;
-} libs[] = {{"/lib64/libc.so.6", NULL}, {"/lib64/libm.so.6", NULL}};
+} libs[] = {
+#ifdef __GLIBC__
+{"/lib64/libc.so.6", NULL}, {"/lib64/libm.so.6", NULL}
+#else
+{"/lib/libc.so", NULL},
+#endif
+};

 static void close_libs (void) {
   for (int i = 0; i < sizeof (libs) / sizeof (struct lib); i++)

not trying to push for anything, just raising this for consideration purposes.

unrelated, the following hunk removes dependency on LLVM

diff --git a/Makefile b/Makefile
index 0dd847b..da9bb5d 100644
--- a/Makefile
+++ b/Makefile
@@ -7,7 +7,7 @@ CFLAGS=-O3 -g -DNDEBUG
 TARGET=x86_64
 MIR_DEPS=mir.h mir-varr.h mir-dlist.h mir-htab.h mir-hash.h mir-interp.c mir-x86_64.c
 MIR_GEN_DEPS=$(MIR_DEPS) mir-bitmap.h mir-gen-$(TARGET).c
-OBJS=mir.o mir-gen.o c2m l2m m2b b2m b2ctab
+OBJS=mir.o mir-gen.o c2m m2b b2m b2ctab
 Q=@

 all: $(OBJS)
vnmakarov commented 4 years ago

Thank you. I changed libs in c2mir-driver.c and l2mir-driver.c.

rofl0r commented 4 years ago

thank you, but i don't think the solution is good. afaik musl libc is the only libc which has libm integrated in libc.so. the following works for me:

#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>

int main(int a, char**b) {
        Dl_info dli;
        void *printf_ptr = dlsym(RTLD_NEXT, "printf");
        dladdr(printf_ptr, &dli);
        printf("%s\n", dli.dli_fname);
        void *sinf_ptr = dlsym(RTLD_NEXT, "sinf");
        dladdr(sinf_ptr, &dli);
        printf("%s\n", dli.dli_fname);
}

in my case it prints /lib/ld-musl-x86_64.so.1 twice. (ld-musl.....so is a symlink to /lib/libc.so, so that's ok)

on ubuntu 16.04. it prints:

/lib/x86_64-linux-gnu/libc.so.6
/lib/x86_64-linux-gnu/libc.so.6

it seems there libc.so contains libm as well.