make the dynamic linker find its path file relative to its own location
authorRich Felker <dalias@aerifal.cx>
Thu, 18 Jul 2013 23:29:44 +0000 (19:29 -0400)
committerRich Felker <dalias@aerifal.cx>
Thu, 18 Jul 2013 23:29:44 +0000 (19:29 -0400)
prior to this change, using a non-default syslibdir was impractical on
systems where the ordinary library paths contain musl-incompatible
library files. the file containing search paths was always taken from
/etc, which would either correspond to a system-wide musl
installation, or fail to exist at all, resulting in searching of the
default library path.

the new search strategy is safe even for suid programs because the
pathname used comes from the PT_INTERP header of the program being
run, rather than any external input.

as part of this change, I have also begun differentiating the names of
arch variants that differ by endianness or floating point calling
convention. the corresponding changes in the build system and and gcc
wrapper script (to use an alternate dynamic linker name) for these
configurations have not yet been made.

arch/arm/reloc.h
arch/i386/reloc.h
arch/microblaze/reloc.h
arch/mips/reloc.h
arch/powerpc/reloc.h
arch/x86_64/reloc.h
src/ldso/dynlink.c

index b41314d..9ca0b48 100644 (file)
@@ -1,7 +1,20 @@
 #include <string.h>
 #include <elf.h>
 #include <string.h>
 #include <elf.h>
+#include <endian.h>
 
 
-#define ETC_LDSO_PATH "/etc/ld-musl-arm.path"
+#if __BYTE_ORDER == __BIG_ENDIAN
+#define ENDIAN_SUFFIX "eb"
+#else
+#define ENDIAN_SUFFIX ""
+#endif
+
+#if __SOFTFP__
+#define FP_SUFFIX ""
+#else
+#define FP_SUFFIX "hf"
+#endif
+
+#define LDSO_ARCH "arm" ENDIAN_SUFFIX FP_SUFFIX
 
 #define IS_COPY(x) ((x)==R_ARM_COPY)
 #define IS_PLT(x) ((x)==R_ARM_JUMP_SLOT)
 
 #define IS_COPY(x) ((x)==R_ARM_COPY)
 #define IS_PLT(x) ((x)==R_ARM_JUMP_SLOT)
index da0bc05..3923b54 100644 (file)
@@ -1,7 +1,7 @@
 #include <string.h>
 #include <elf.h>
 
 #include <string.h>
 #include <elf.h>
 
-#define ETC_LDSO_PATH "/etc/ld-musl-i386.path"
+#define LDSO_ARCH "i386"
 
 #define IS_COPY(x) ((x)==R_386_COPY)
 #define IS_PLT(x) ((x)==R_386_JMP_SLOT)
 
 #define IS_COPY(x) ((x)==R_386_COPY)
 #define IS_PLT(x) ((x)==R_386_JMP_SLOT)
index 81add5e..60f7422 100644 (file)
@@ -1,7 +1,14 @@
 #include <string.h>
 #include <elf.h>
 #include <string.h>
 #include <elf.h>
+#include <endian.h>
 
 
-#define ETC_LDSO_PATH "/etc/ld-musl-microblaze.path"
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define ENDIAN_SUFFIX "el"
+#else
+#define ENDIAN_SUFFIX ""
+#endif
+
+#define LDSO_ARCH "microblaze" ENDIAN_SUFFIX
 
 #define IS_COPY(x) ((x)==R_MICROBLAZE_COPY)
 #define IS_PLT(x) ((x)==R_MICROBLAZE_JUMP_SLOT)
 
 #define IS_COPY(x) ((x)==R_MICROBLAZE_COPY)
 #define IS_PLT(x) ((x)==R_MICROBLAZE_JUMP_SLOT)
index f5e9c77..4c035f3 100644 (file)
@@ -1,7 +1,14 @@
 #include <string.h>
 #include <elf.h>
 #include <string.h>
 #include <elf.h>
+#include <endian.h>
 
 
-#define ETC_LDSO_PATH "/etc/ld-musl-mips.path"
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+#define ENDIAN_SUFFIX "el"
+#else
+#define ENDIAN_SUFFIX ""
+#endif
+
+#define LDSO_ARCH "mips" ENDIAN_SUFFIX
 
 #define IS_COPY(x) ((x)==R_MIPS_COPY)
 #define IS_PLT(x) 1
 
 #define IS_COPY(x) ((x)==R_MIPS_COPY)
 #define IS_PLT(x) 1
index 113dfa1..80b9ebc 100644 (file)
@@ -1,7 +1,7 @@
 #include <string.h>
 #include <elf.h>
 
 #include <string.h>
 #include <elf.h>
 
-#define ETC_LDSO_PATH "/etc/ld-musl-powerpc.path"
+#define LDSO_ARCH "powerpc" ENDIAN_SUFFIX
 
 #define IS_COPY(x) ((x)==R_PPC_COPY)
 #define IS_PLT(x) ((x)==R_PPC_JMP_SLOT)
 
 #define IS_COPY(x) ((x)==R_PPC_COPY)
 #define IS_PLT(x) ((x)==R_PPC_JMP_SLOT)
index 38a6073..28cf7cc 100644 (file)
@@ -2,7 +2,7 @@
 #include <string.h>
 #include <elf.h>
 
 #include <string.h>
 #include <elf.h>
 
-#define ETC_LDSO_PATH "/etc/ld-musl-x86_64.path"
+#define LDSO_ARCH "x86_64"
 
 #define IS_COPY(x) ((x)==R_X86_64_COPY)
 #define IS_PLT(x) ((x)==R_X86_64_JUMP_SLOT)
 
 #define IS_COPY(x) ((x)==R_X86_64_COPY)
 #define IS_PLT(x) ((x)==R_X86_64_JUMP_SLOT)
index ff5b738..1d5e297 100644 (file)
@@ -483,7 +483,26 @@ static struct dso *load_library(const char *name)
                if (fd < 0 && env_path) fd = path_open(name, env_path, buf, sizeof buf);
                if (fd < 0) {
                        if (!sys_path) {
                if (fd < 0 && env_path) fd = path_open(name, env_path, buf, sizeof buf);
                if (fd < 0) {
                        if (!sys_path) {
-                               FILE *f = fopen(ETC_LDSO_PATH, "rbe");
+                               char *prefix = 0;
+                               size_t prefix_len;
+                               if (ldso->name[0]=='/') {
+                                       char *s, *t, *z;
+                                       for (s=t=z=ldso->name; *s; s++)
+                                               if (*s=='/') z=t, t=s;
+                                       prefix_len = z-ldso->name;
+                                       if (prefix_len < PATH_MAX)
+                                               prefix = ldso->name;
+                               }
+                               if (!prefix) {
+                                       prefix = "";
+                                       prefix_len = 0;
+                               }
+                               char etc_ldso_path[prefix_len + 1
+                                       + sizeof "/etc/ld-musl-" LDSO_ARCH ".path"];
+                               snprintf(etc_ldso_path, sizeof etc_ldso_path,
+                                       "%.*s/etc/ld-musl-" LDSO_ARCH ".path",
+                                       (int)prefix_len, prefix);
+                               FILE *f = fopen(etc_ldso_path, "rbe");
                                if (f) {
                                        if (getdelim(&sys_path, (size_t[1]){0}, 0, f) <= 0) {
                                                free(sys_path);
                                if (f) {
                                        if (getdelim(&sys_path, (size_t[1]){0}, 0, f) <= 0) {
                                                free(sys_path);