Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | From b40c129ed2d53b69463883a5422dd4a012a398f9 Mon Sep 17 00:00:00 2001 |
| 2 | From: Junling Zheng <zhengjunling@huawei.com> |
| 3 | Date: Fri, 3 Apr 2015 05:02:27 +0000 |
| 4 | Subject: [PATCH 6/7] ldso: limited support for $ORIGIN in rpath |
| 5 | MIME-Version: 1.0 |
| 6 | Content-Type: text/plain; charset=UTF-8 |
| 7 | Content-Transfer-Encoding: 8bit |
| 8 | |
| 9 | Derived from: |
| 10 | http://lists.busybox.net/pipermail/uclibc/2011-March/045003.html |
| 11 | |
| 12 | However, the above patch introduced '_dl_strchr' in ldso/ldso/dl-elf.c, |
| 13 | and caused the following undefined referencing compiling error: |
| 14 | |
| 15 | | .../libdl.a(libdl.os): In function `search_for_named_library': |
| 16 | | .../dl-elf.c:156: undefined reference to `_dl_strchr' |
| 17 | | collect2: error: ld returned 1 exit status |
| 18 | |
| 19 | This problem would be reproduced through compiling gdb in static mode |
| 20 | using uclibc. |
| 21 | |
| 22 | So, add the definition of '_dl_strchr' to fix it. The '_dl_strstr' is |
| 23 | added as well. |
| 24 | |
| 25 | Upstream-Status: Submitted |
| 26 | |
| 27 | Signed-off-by: Timo Teräs <timo.teras at iki.fi> |
| 28 | Signed-off-by: Junling Zheng <zhengjunling@huawei.com> |
| 29 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 30 | --- |
| 31 | Upstream-Status: Pending |
| 32 | |
| 33 | ldso/include/dl-string.h | 2 ++ |
| 34 | ldso/ldso/dl-elf.c | 79 +++++++++++++++++++++++++----------------------- |
| 35 | ldso/ldso/ldso.c | 18 +++++++++-- |
| 36 | 3 files changed, 59 insertions(+), 40 deletions(-) |
| 37 | |
| 38 | diff --git a/ldso/include/dl-string.h b/ldso/include/dl-string.h |
| 39 | index aacad10..14ae617 100644 |
| 40 | --- a/ldso/include/dl-string.h |
| 41 | +++ b/ldso/include/dl-string.h |
| 42 | @@ -204,7 +204,9 @@ static __always_inline char * _dl_get_last_path_component(char *path) |
| 43 | # define _dl_strcat strcat |
| 44 | # define _dl_strcpy strcpy |
| 45 | # define _dl_strcmp strcmp |
| 46 | +# define _dl_strchr strchr |
| 47 | # define _dl_strrchr strrchr |
| 48 | +# define _dl_strstr strstr |
| 49 | # define _dl_memcpy memcpy |
| 50 | # define _dl_memcmp memcmp |
| 51 | # define _dl_memset memset |
| 52 | diff --git a/ldso/ldso/dl-elf.c b/ldso/ldso/dl-elf.c |
| 53 | index 5631905..6ab7afe 100644 |
| 54 | --- a/ldso/ldso/dl-elf.c |
| 55 | +++ b/ldso/ldso/dl-elf.c |
| 56 | @@ -133,56 +133,60 @@ _dl_protect_relro (struct elf_resolve *l) |
| 57 | * in uClibc/ldso/util/ldd.c */ |
| 58 | static struct elf_resolve * |
| 59 | search_for_named_library(const char *name, unsigned rflags, const char *path_list, |
| 60 | - struct dyn_elf **rpnt) |
| 61 | + struct dyn_elf **rpnt, const char* origin) |
| 62 | { |
| 63 | - char *path, *path_n, *mylibname; |
| 64 | + char *mylibname; |
| 65 | + const char *p, *pn; |
| 66 | struct elf_resolve *tpnt; |
| 67 | - int done; |
| 68 | + int plen; |
| 69 | |
| 70 | if (path_list==NULL) |
| 71 | return NULL; |
| 72 | |
| 73 | - /* We need a writable copy of this string, but we don't |
| 74 | - * need this allocated permanently since we don't want |
| 75 | - * to leak memory, so use alloca to put path on the stack */ |
| 76 | - done = _dl_strlen(path_list); |
| 77 | - path = alloca(done + 1); |
| 78 | - |
| 79 | /* another bit of local storage */ |
| 80 | mylibname = alloca(2050); |
| 81 | |
| 82 | - _dl_memcpy(path, path_list, done+1); |
| 83 | - |
| 84 | /* Unlike ldd.c, don't bother to eliminate double //s */ |
| 85 | |
| 86 | /* Replace colons with zeros in path_list */ |
| 87 | /* : at the beginning or end of path maps to CWD */ |
| 88 | /* :: anywhere maps CWD */ |
| 89 | /* "" maps to CWD */ |
| 90 | - done = 0; |
| 91 | - path_n = path; |
| 92 | - do { |
| 93 | - if (*path == 0) { |
| 94 | - *path = ':'; |
| 95 | - done = 1; |
| 96 | + for (p = path_list; p != NULL; p = pn) { |
| 97 | + pn = _dl_strchr(p + 1, ':'); |
| 98 | + if (pn != NULL) { |
| 99 | + plen = pn - p; |
| 100 | + pn++; |
| 101 | + } else |
| 102 | + plen = _dl_strlen(p); |
| 103 | + |
| 104 | + if (plen >= 7 && _dl_memcmp(p, "$ORIGIN", 7) == 0) { |
| 105 | + int olen; |
| 106 | + if (rflags && plen != 7) |
| 107 | + continue; |
| 108 | + if (origin == NULL) |
| 109 | + continue; |
| 110 | + for (olen = _dl_strlen(origin) - 1; olen >= 0 && origin[olen] != '/'; olen--) |
| 111 | + ; |
| 112 | + if (olen <= 0) |
| 113 | + continue; |
| 114 | + _dl_memcpy(&mylibname[0], origin, olen); |
| 115 | + _dl_memcpy(&mylibname[olen], p + 7, plen - 7); |
| 116 | + mylibname[olen + plen - 7] = 0; |
| 117 | + } else if (plen != 0) { |
| 118 | + _dl_memcpy(mylibname, p, plen); |
| 119 | + mylibname[plen] = 0; |
| 120 | + } else { |
| 121 | + _dl_strcpy(mylibname, "."); |
| 122 | } |
| 123 | - if (*path == ':') { |
| 124 | - *path = 0; |
| 125 | - if (*path_n) |
| 126 | - _dl_strcpy(mylibname, path_n); |
| 127 | - else |
| 128 | - _dl_strcpy(mylibname, "."); /* Assume current dir if empty path */ |
| 129 | - _dl_strcat(mylibname, "/"); |
| 130 | - _dl_strcat(mylibname, name); |
| 131 | + _dl_strcat(mylibname, "/"); |
| 132 | + _dl_strcat(mylibname, name); |
| 133 | #ifdef __LDSO_SAFE_RUNPATH__ |
| 134 | - if (*mylibname == '/') |
| 135 | + if (*mylibname == '/') |
| 136 | #endif |
| 137 | - if ((tpnt = _dl_load_elf_shared_library(rflags, rpnt, mylibname)) != NULL) |
| 138 | - return tpnt; |
| 139 | - path_n = path+1; |
| 140 | - } |
| 141 | - path++; |
| 142 | - } while (!done); |
| 143 | + if ((tpnt = _dl_load_elf_shared_library(rflags, rpnt, mylibname)) != NULL) |
| 144 | + return tpnt; |
| 145 | + } |
| 146 | return NULL; |
| 147 | } |
| 148 | |
| 149 | @@ -234,7 +238,8 @@ struct elf_resolve *_dl_load_shared_library(unsigned rflags, struct dyn_elf **rp |
| 150 | if (pnt) { |
| 151 | pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB]; |
| 152 | _dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt); |
| 153 | - if ((tpnt1 = search_for_named_library(libname, rflags, pnt, rpnt)) != NULL) |
| 154 | + if ((tpnt1 = search_for_named_library(libname, rflags, pnt, rpnt, |
| 155 | + tpnt->libname)) != NULL) |
| 156 | return tpnt1; |
| 157 | } |
| 158 | #endif |
| 159 | @@ -243,7 +248,7 @@ struct elf_resolve *_dl_load_shared_library(unsigned rflags, struct dyn_elf **rp |
| 160 | /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */ |
| 161 | if (_dl_library_path) { |
| 162 | _dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path); |
| 163 | - if ((tpnt1 = search_for_named_library(libname, rflags, _dl_library_path, rpnt)) != NULL) |
| 164 | + if ((tpnt1 = search_for_named_library(libname, rflags, _dl_library_path, rpnt, NULL)) != NULL) |
| 165 | { |
| 166 | return tpnt1; |
| 167 | } |
| 168 | @@ -257,7 +262,7 @@ struct elf_resolve *_dl_load_shared_library(unsigned rflags, struct dyn_elf **rp |
| 169 | if (pnt) { |
| 170 | pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB]; |
| 171 | _dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt); |
| 172 | - if ((tpnt1 = search_for_named_library(libname, rflags, pnt, rpnt)) != NULL) |
| 173 | + if ((tpnt1 = search_for_named_library(libname, rflags, pnt, rpnt, NULL)) != NULL) |
| 174 | return tpnt1; |
| 175 | } |
| 176 | #endif |
| 177 | @@ -291,7 +296,7 @@ struct elf_resolve *_dl_load_shared_library(unsigned rflags, struct dyn_elf **rp |
| 178 | /* Look for libraries wherever the shared library loader |
| 179 | * was installed */ |
| 180 | _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath); |
| 181 | - tpnt1 = search_for_named_library(libname, rflags, _dl_ldsopath, rpnt); |
| 182 | + tpnt1 = search_for_named_library(libname, rflags, _dl_ldsopath, rpnt, NULL); |
| 183 | if (tpnt1 != NULL) |
| 184 | return tpnt1; |
| 185 | #endif |
| 186 | @@ -304,7 +309,7 @@ struct elf_resolve *_dl_load_shared_library(unsigned rflags, struct dyn_elf **rp |
| 187 | #ifndef __LDSO_CACHE_SUPPORT__ |
| 188 | ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib" |
| 189 | #endif |
| 190 | - , rpnt); |
| 191 | + , rpnt, NULL); |
| 192 | if (tpnt1 != NULL) |
| 193 | return tpnt1; |
| 194 | |
| 195 | diff --git a/ldso/ldso/ldso.c b/ldso/ldso/ldso.c |
| 196 | index f38f9e3..3812908 100644 |
| 197 | --- a/ldso/ldso/ldso.c |
| 198 | +++ b/ldso/ldso/ldso.c |
| 199 | @@ -402,6 +402,20 @@ static ptrdiff_t _dl_build_local_scope (struct elf_resolve **list, |
| 200 | p += _dl_build_local_scope (p, q->tpnt); |
| 201 | return p - list; |
| 202 | } |
| 203 | + |
| 204 | +static void _dl_setup_progname(const char *argv0) |
| 205 | +{ |
| 206 | + char image[PATH_MAX]; |
| 207 | + ssize_t s; |
| 208 | + |
| 209 | + s = _dl_readlink("/proc/self/exe", image, sizeof(image)); |
| 210 | + if (s > 0 && image[0] == '/') { |
| 211 | + image[s] = 0; |
| 212 | + _dl_progname = _dl_strdup(image); |
| 213 | + } else if (argv0) { |
| 214 | + _dl_progname = argv0; |
| 215 | + } |
| 216 | +} |
| 217 | |
| 218 | void *_dl_get_ready_to_run(struct elf_resolve *tpnt, DL_LOADADDR_TYPE load_addr, |
| 219 | ElfW(auxv_t) auxvt[AT_EGID + 1], char **envp, char **argv |
| 220 | @@ -454,9 +468,7 @@ void *_dl_get_ready_to_run(struct elf_resolve *tpnt, DL_LOADADDR_TYPE load_addr, |
| 221 | * been fixed up by now. Still no function calls outside of this |
| 222 | * library, since the dynamic resolver is not yet ready. |
| 223 | */ |
| 224 | - if (argv[0]) { |
| 225 | - _dl_progname = argv[0]; |
| 226 | - } |
| 227 | + _dl_setup_progname(argv[0]); |
| 228 | |
| 229 | #ifdef __DSBT__ |
| 230 | _dl_ldso_dsbt = (void *)tpnt->dynamic_info[DT_DSBT_BASE_IDX]; |
| 231 | -- |
| 232 | 2.1.4 |
| 233 | |