dl-elf.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * This file contains the helper routines to load an ELF shared
  4. * library into memory and add the symbol table info to the chain.
  5. *
  6. * Copyright (C) 2000-2006 by Erik Andersen <andersen@codepoet.org>
  7. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  8. * David Engel, Hongjiu Lu and Mitch D'Souza
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. The name of the above contributors may not be
  16. * used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include "ldso.h"
  32. #ifdef __LDSO_CACHE_SUPPORT__
  33. static caddr_t _dl_cache_addr = NULL;
  34. static size_t _dl_cache_size = 0;
  35. int _dl_map_cache(void)
  36. {
  37. int fd;
  38. struct stat st;
  39. header_t *header;
  40. libentry_t *libent;
  41. int i, strtabsize;
  42. if (_dl_cache_addr == MAP_FAILED)
  43. return -1;
  44. else if (_dl_cache_addr != NULL)
  45. return 0;
  46. if (_dl_stat(LDSO_CACHE, &st)
  47. || (fd = _dl_open(LDSO_CACHE, O_RDONLY|O_CLOEXEC, 0)) < 0) {
  48. _dl_cache_addr = MAP_FAILED; /* so we won't try again */
  49. return -1;
  50. }
  51. _dl_cache_size = st.st_size;
  52. _dl_cache_addr = _dl_mmap(0, _dl_cache_size, PROT_READ, LDSO_CACHE_MMAP_FLAGS, fd, 0);
  53. _dl_close(fd);
  54. if (_dl_mmap_check_error(_dl_cache_addr)) {
  55. _dl_dprintf(2, "%s:%i: can't map '%s'\n",
  56. _dl_progname, __LINE__, LDSO_CACHE);
  57. return -1;
  58. }
  59. header = (header_t *) _dl_cache_addr;
  60. if (_dl_cache_size < sizeof(header_t) ||
  61. _dl_memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
  62. || _dl_memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
  63. || _dl_cache_size <
  64. (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
  65. || _dl_cache_addr[_dl_cache_size - 1] != '\0')
  66. {
  67. _dl_dprintf(2, "%s: cache '%s' is corrupt\n", _dl_progname,
  68. LDSO_CACHE);
  69. goto fail;
  70. }
  71. strtabsize = _dl_cache_size - sizeof(header_t) -
  72. header->nlibs * sizeof(libentry_t);
  73. libent = (libentry_t *) & header[1];
  74. for (i = 0; i < header->nlibs; i++) {
  75. if (libent[i].sooffset >= strtabsize ||
  76. libent[i].liboffset >= strtabsize)
  77. {
  78. _dl_dprintf(2, "%s: cache '%s' is corrupt\n", _dl_progname, LDSO_CACHE);
  79. goto fail;
  80. }
  81. }
  82. return 0;
  83. fail:
  84. _dl_munmap(_dl_cache_addr, _dl_cache_size);
  85. _dl_cache_addr = MAP_FAILED;
  86. return -1;
  87. }
  88. int _dl_unmap_cache(void)
  89. {
  90. if (_dl_cache_addr == NULL || _dl_cache_addr == MAP_FAILED)
  91. return -1;
  92. #if 1
  93. _dl_munmap(_dl_cache_addr, _dl_cache_size);
  94. _dl_cache_addr = NULL;
  95. #endif
  96. return 0;
  97. }
  98. #endif
  99. void
  100. _dl_protect_relro (struct elf_resolve *l)
  101. {
  102. ElfW(Addr) base = (ElfW(Addr)) DL_RELOC_ADDR(l->loadaddr, l->relro_addr);
  103. ElfW(Addr) start = (base & PAGE_ALIGN);
  104. ElfW(Addr) end = ((base + l->relro_size) & PAGE_ALIGN);
  105. _dl_if_debug_dprint("RELRO protecting %s: start:%x, end:%x\n", l->libname, start, end);
  106. if (start != end &&
  107. _dl_mprotect ((void *) start, end - start, PROT_READ) < 0) {
  108. _dl_dprintf(2, "%s: cannot apply additional memory protection after relocation", l->libname);
  109. _dl_exit(0);
  110. }
  111. }
  112. /* This function's behavior must exactly match that
  113. * in uClibc/ldso/util/ldd.c */
  114. static struct elf_resolve *
  115. search_for_named_library(const char *name, int secure, const char *path_list,
  116. struct dyn_elf **rpnt)
  117. {
  118. char *path, *path_n, *mylibname;
  119. struct elf_resolve *tpnt;
  120. int done;
  121. if (path_list==NULL)
  122. return NULL;
  123. /* We need a writable copy of this string, but we don't
  124. * need this allocated permanently since we don't want
  125. * to leak memory, so use alloca to put path on the stack */
  126. done = _dl_strlen(path_list);
  127. path = alloca(done + 1);
  128. /* another bit of local storage */
  129. mylibname = alloca(2050);
  130. _dl_memcpy(path, path_list, done+1);
  131. /* Unlike ldd.c, don't bother to eliminate double //s */
  132. /* Replace colons with zeros in path_list */
  133. /* : at the beginning or end of path maps to CWD */
  134. /* :: anywhere maps CWD */
  135. /* "" maps to CWD */
  136. done = 0;
  137. path_n = path;
  138. do {
  139. if (*path == 0) {
  140. *path = ':';
  141. done = 1;
  142. }
  143. if (*path == ':') {
  144. *path = 0;
  145. if (*path_n)
  146. _dl_strcpy(mylibname, path_n);
  147. else
  148. _dl_strcpy(mylibname, "."); /* Assume current dir if empty path */
  149. _dl_strcat(mylibname, "/");
  150. _dl_strcat(mylibname, name);
  151. if ((tpnt = _dl_load_elf_shared_library(secure, rpnt, mylibname)) != NULL)
  152. return tpnt;
  153. path_n = path+1;
  154. }
  155. path++;
  156. } while (!done);
  157. return NULL;
  158. }
  159. /* Used to return error codes back to dlopen et. al. */
  160. unsigned long _dl_error_number;
  161. unsigned long _dl_internal_error_number;
  162. struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
  163. struct elf_resolve *tpnt, char *full_libname, int __attribute__((unused)) trace_loaded_objects)
  164. {
  165. char *pnt;
  166. struct elf_resolve *tpnt1;
  167. char *libname;
  168. _dl_internal_error_number = 0;
  169. libname = full_libname;
  170. /* quick hack to ensure mylibname buffer doesn't overflow. don't
  171. allow full_libname or any directory to be longer than 1024. */
  172. if (_dl_strlen(full_libname) > 1024)
  173. goto goof;
  174. /* Skip over any initial initial './' and '/' stuff to
  175. * get the short form libname with no path garbage */
  176. pnt = _dl_strrchr(libname, '/');
  177. if (pnt) {
  178. libname = pnt + 1;
  179. }
  180. _dl_if_debug_dprint("\tfind library='%s'; searching\n", libname);
  181. /* If the filename has any '/', try it straight and leave it at that.
  182. For IBCS2 compatibility under linux, we substitute the string
  183. /usr/i486-sysv4/lib for /usr/lib in library names. */
  184. if (libname != full_libname) {
  185. _dl_if_debug_dprint("\ttrying file='%s'\n", full_libname);
  186. tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
  187. if (tpnt1) {
  188. return tpnt1;
  189. }
  190. }
  191. /*
  192. * The ABI specifies that RPATH is searched before LD_LIBRARY_PATH or
  193. * the default path of /usr/lib. Check in rpath directories.
  194. */
  195. #ifdef __LDSO_RUNPATH__
  196. pnt = (tpnt ? (char *) tpnt->dynamic_info[DT_RPATH] : NULL);
  197. if (pnt) {
  198. pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
  199. _dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt);
  200. if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
  201. return tpnt1;
  202. }
  203. #endif
  204. /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
  205. if (_dl_library_path) {
  206. _dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
  207. if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
  208. {
  209. return tpnt1;
  210. }
  211. }
  212. /*
  213. * The ABI specifies that RUNPATH is searched after LD_LIBRARY_PATH.
  214. */
  215. #ifdef __LDSO_RUNPATH__
  216. pnt = (tpnt ? (char *)tpnt->dynamic_info[DT_RUNPATH] : NULL);
  217. if (pnt) {
  218. pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
  219. _dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt);
  220. if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
  221. return tpnt1;
  222. }
  223. #endif
  224. /*
  225. * Where should the cache be searched? There is no such concept in the
  226. * ABI, so we have some flexibility here. For now, search it before
  227. * the hard coded paths that follow (i.e before /lib and /usr/lib).
  228. */
  229. #ifdef __LDSO_CACHE_SUPPORT__
  230. if (_dl_cache_addr != NULL && _dl_cache_addr != MAP_FAILED) {
  231. int i;
  232. header_t *header = (header_t *) _dl_cache_addr;
  233. libentry_t *libent = (libentry_t *) & header[1];
  234. char *strs = (char *) &libent[header->nlibs];
  235. _dl_if_debug_dprint("\tsearching cache='%s'\n", LDSO_CACHE);
  236. for (i = 0; i < header->nlibs; i++) {
  237. if ((libent[i].flags == LIB_ELF
  238. || libent[i].flags == LIB_ELF_LIBC0
  239. || libent[i].flags == LIB_ELF_LIBC5)
  240. && _dl_strcmp(libname, strs + libent[i].sooffset) == 0
  241. && (tpnt1 = _dl_load_elf_shared_library(secure, rpnt, strs + libent[i].liboffset))
  242. ) {
  243. return tpnt1;
  244. }
  245. }
  246. }
  247. #endif
  248. /* Look for libraries wherever the shared library loader
  249. * was installed */
  250. _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath);
  251. tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt);
  252. if (tpnt1 != NULL)
  253. return tpnt1;
  254. /* Lastly, search the standard list of paths for the library.
  255. This list must exactly match the list in uClibc/ldso/util/ldd.c */
  256. _dl_if_debug_dprint("\tsearching full lib path list\n");
  257. tpnt1 = search_for_named_library(libname, secure,
  258. UCLIBC_RUNTIME_PREFIX "lib:"
  259. UCLIBC_RUNTIME_PREFIX "usr/lib"
  260. #ifndef __LDSO_CACHE_SUPPORT__
  261. ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
  262. #endif
  263. , rpnt);
  264. if (tpnt1 != NULL)
  265. return tpnt1;
  266. goof:
  267. /* Well, we shot our wad on that one. All we can do now is punt */
  268. if (_dl_internal_error_number)
  269. _dl_error_number = _dl_internal_error_number;
  270. else
  271. _dl_error_number = LD_ERROR_NOFILE;
  272. _dl_if_debug_dprint("Bummer: could not find '%s'!\n", libname);
  273. return NULL;
  274. }
  275. /*
  276. * Read one ELF library into memory, mmap it into the correct locations and
  277. * add the symbol info to the symbol chain. Perform any relocations that
  278. * are required.
  279. */
  280. struct elf_resolve *_dl_load_elf_shared_library(int secure,
  281. struct dyn_elf **rpnt, const char *libname)
  282. {
  283. ElfW(Ehdr) *epnt;
  284. unsigned long dynamic_addr = 0;
  285. ElfW(Dyn) *dpnt;
  286. struct elf_resolve *tpnt;
  287. ElfW(Phdr) *ppnt;
  288. #if defined(USE_TLS) && USE_TLS
  289. ElfW(Phdr) *tlsppnt = NULL;
  290. #endif
  291. char *status, *header;
  292. unsigned long dynamic_info[DYNAMIC_SIZE];
  293. unsigned long *lpnt;
  294. unsigned long libaddr;
  295. unsigned long minvma = 0xffffffff, maxvma = 0;
  296. unsigned int rtld_flags;
  297. int i, flags, piclib, infile;
  298. ElfW(Addr) relro_addr = 0;
  299. size_t relro_size = 0;
  300. struct stat st;
  301. uint32_t *p32;
  302. DL_LOADADDR_TYPE lib_loadaddr;
  303. DL_INIT_LOADADDR_EXTRA_DECLS
  304. libaddr = 0;
  305. infile = _dl_open(libname, O_RDONLY, 0);
  306. if (infile < 0) {
  307. _dl_internal_error_number = LD_ERROR_NOFILE;
  308. return NULL;
  309. }
  310. if (_dl_fstat(infile, &st) < 0) {
  311. _dl_internal_error_number = LD_ERROR_NOFILE;
  312. _dl_close(infile);
  313. return NULL;
  314. }
  315. /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
  316. we don't load the library if it isn't setuid. */
  317. if (secure) {
  318. if (!(st.st_mode & S_ISUID)) {
  319. _dl_close(infile);
  320. return NULL;
  321. }
  322. }
  323. /* Check if file is already loaded */
  324. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  325. if (tpnt->st_dev == st.st_dev && tpnt->st_ino == st.st_ino) {
  326. /* Already loaded */
  327. tpnt->usage_count++;
  328. _dl_close(infile);
  329. return tpnt;
  330. }
  331. }
  332. header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  333. MAP_PRIVATE | MAP_ANONYMOUS | MAP_UNINITIALIZE, -1, 0);
  334. if (_dl_mmap_check_error(header)) {
  335. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  336. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  337. _dl_close(infile);
  338. return NULL;
  339. }
  340. _dl_read(infile, header, _dl_pagesize);
  341. epnt = (ElfW(Ehdr) *) (intptr_t) header;
  342. p32 = (uint32_t*)&epnt->e_ident;
  343. if (*p32 != ELFMAG_U32) {
  344. _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  345. libname);
  346. _dl_internal_error_number = LD_ERROR_NOTELF;
  347. _dl_close(infile);
  348. _dl_munmap(header, _dl_pagesize);
  349. return NULL;
  350. }
  351. if ((epnt->e_type != ET_DYN
  352. #ifdef __LDSO_STANDALONE_SUPPORT__
  353. && epnt->e_type != ET_EXEC
  354. #endif
  355. ) || (epnt->e_machine != MAGIC1
  356. #ifdef MAGIC2
  357. && epnt->e_machine != MAGIC2
  358. #endif
  359. ))
  360. {
  361. _dl_internal_error_number =
  362. (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
  363. _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  364. "\n", _dl_progname, libname);
  365. _dl_close(infile);
  366. _dl_munmap(header, _dl_pagesize);
  367. return NULL;
  368. }
  369. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  370. piclib = 1;
  371. for (i = 0; i < epnt->e_phnum; i++) {
  372. if (ppnt->p_type == PT_DYNAMIC) {
  373. if (dynamic_addr)
  374. _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
  375. _dl_progname, libname);
  376. dynamic_addr = ppnt->p_vaddr;
  377. }
  378. if (ppnt->p_type == PT_LOAD) {
  379. /* See if this is a PIC library. */
  380. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  381. piclib = 0;
  382. minvma = ppnt->p_vaddr;
  383. }
  384. if (piclib && ppnt->p_vaddr < minvma) {
  385. minvma = ppnt->p_vaddr;
  386. }
  387. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  388. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  389. }
  390. }
  391. if (ppnt->p_type == PT_TLS) {
  392. #if defined(USE_TLS) && USE_TLS
  393. if (ppnt->p_memsz == 0)
  394. /* Nothing to do for an empty segment. */
  395. continue;
  396. else
  397. /* Save for after 'tpnt' is actually allocated. */
  398. tlsppnt = ppnt;
  399. #else
  400. /*
  401. * Yup, the user was an idiot and tried to sneak in a library with
  402. * TLS in it and we don't support it. Let's fall on our own sword
  403. * and scream at the luser while we die.
  404. */
  405. _dl_dprintf(2, "%s: '%s' library contains unsupported TLS\n",
  406. _dl_progname, libname);
  407. _dl_internal_error_number = LD_ERROR_TLS_FAILED;
  408. _dl_close(infile);
  409. _dl_munmap(header, _dl_pagesize);
  410. return NULL;
  411. #endif
  412. }
  413. ppnt++;
  414. }
  415. #ifdef __LDSO_STANDALONE_SUPPORT__
  416. if (epnt->e_type == ET_EXEC)
  417. piclib = 0;
  418. #endif
  419. DL_CHECK_LIB_TYPE (epnt, piclib, _dl_progname, libname);
  420. maxvma = (maxvma + ADDR_ALIGN) & PAGE_ALIGN;
  421. minvma = minvma & ~0xffffU;
  422. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  423. if (!piclib)
  424. flags |= MAP_FIXED;
  425. if (piclib == 0 || piclib == 1) {
  426. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  427. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  428. if (_dl_mmap_check_error(status)) {
  429. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  430. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  431. _dl_close(infile);
  432. _dl_munmap(header, _dl_pagesize);
  433. return NULL;
  434. }
  435. libaddr = (unsigned long) status;
  436. flags |= MAP_FIXED;
  437. }
  438. /* Get the memory to store the library */
  439. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  440. DL_INIT_LOADADDR(lib_loadaddr, libaddr, ppnt, epnt->e_phnum);
  441. for (i = 0; i < epnt->e_phnum; i++) {
  442. if (DL_IS_SPECIAL_SEGMENT (epnt, ppnt)) {
  443. char *addr;
  444. addr = DL_MAP_SEGMENT (epnt, ppnt, infile, flags);
  445. if (addr == NULL)
  446. goto cant_map;
  447. DL_INIT_LOADADDR_HDR (lib_loadaddr, addr, ppnt);
  448. ppnt++;
  449. continue;
  450. }
  451. if (ppnt->p_type == PT_GNU_RELRO) {
  452. relro_addr = ppnt->p_vaddr;
  453. relro_size = ppnt->p_memsz;
  454. }
  455. if (ppnt->p_type == PT_LOAD) {
  456. char *tryaddr;
  457. ssize_t size;
  458. /* See if this is a PIC library. */
  459. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  460. piclib = 0;
  461. /* flags |= MAP_FIXED; */
  462. }
  463. if (ppnt->p_flags & PF_W) {
  464. unsigned long map_size;
  465. char *cpnt;
  466. char *piclib2map = 0;
  467. if (piclib == 2 &&
  468. /* We might be able to avoid this
  469. call if memsz doesn't require
  470. an additional page, but this
  471. would require mmap to always
  472. return page-aligned addresses
  473. and a whole number of pages
  474. allocated. Unfortunately on
  475. uClinux may return misaligned
  476. addresses and may allocate
  477. partial pages, so we may end up
  478. doing unnecessary mmap calls.
  479. This is what we could do if we
  480. knew mmap would always return
  481. aligned pages:
  482. ((ppnt->p_vaddr + ppnt->p_filesz
  483. + ADDR_ALIGN)
  484. & PAGE_ALIGN)
  485. < ppnt->p_vaddr + ppnt->p_memsz)
  486. Instead, we have to do this: */
  487. ppnt->p_filesz < ppnt->p_memsz)
  488. {
  489. piclib2map = (char *)
  490. _dl_mmap(0, (ppnt->p_vaddr & ADDR_ALIGN)
  491. + ppnt->p_memsz,
  492. LXFLAGS(ppnt->p_flags),
  493. flags | MAP_ANONYMOUS, -1, 0);
  494. if (_dl_mmap_check_error(piclib2map))
  495. goto cant_map;
  496. DL_INIT_LOADADDR_HDR
  497. (lib_loadaddr, piclib2map
  498. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  499. }
  500. tryaddr = piclib == 2 ? piclib2map
  501. : ((char*) (piclib ? libaddr : 0) +
  502. (ppnt->p_vaddr & PAGE_ALIGN));
  503. size = (ppnt->p_vaddr & ADDR_ALIGN)
  504. + ppnt->p_filesz;
  505. /* For !MMU, mmap to fixed address will fail.
  506. So instead of desperately call mmap and fail,
  507. we set status to MAP_FAILED to save a call
  508. to mmap (). */
  509. #ifndef __ARCH_USE_MMU__
  510. if (piclib2map == 0)
  511. #endif
  512. status = (char *) _dl_mmap
  513. (tryaddr, size, LXFLAGS(ppnt->p_flags),
  514. flags | (piclib2map ? MAP_FIXED : 0),
  515. infile, ppnt->p_offset & OFFS_ALIGN);
  516. #ifndef __ARCH_USE_MMU__
  517. else
  518. status = MAP_FAILED;
  519. #endif
  520. #ifdef _DL_PREAD
  521. if (_dl_mmap_check_error(status) && piclib2map
  522. && (_DL_PREAD (infile, tryaddr, size,
  523. ppnt->p_offset & OFFS_ALIGN)
  524. == size))
  525. status = tryaddr;
  526. #endif
  527. if (_dl_mmap_check_error(status)
  528. || (tryaddr && tryaddr != status)) {
  529. cant_map:
  530. _dl_dprintf(2, "%s:%i: can't map '%s'\n",
  531. _dl_progname, __LINE__, libname);
  532. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  533. DL_LOADADDR_UNMAP (lib_loadaddr, maxvma - minvma);
  534. _dl_close(infile);
  535. _dl_munmap(header, _dl_pagesize);
  536. return NULL;
  537. }
  538. if (! piclib2map) {
  539. DL_INIT_LOADADDR_HDR
  540. (lib_loadaddr, status
  541. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  542. }
  543. /* Now we want to allocate and
  544. zero-out any data from the end of
  545. the region we mapped in from the
  546. file (filesz) to the end of the
  547. loadable segment (memsz). We may
  548. need additional pages for memsz,
  549. that we map in below, and we can
  550. count on the kernel to zero them
  551. out, but we have to zero out stuff
  552. in the last page that we mapped in
  553. from the file. However, we can't
  554. assume to have actually obtained
  555. full pages from the kernel, since
  556. we didn't ask for them, and uClibc
  557. may not give us full pages for
  558. small allocations. So only zero
  559. out up to memsz or the end of the
  560. page, whichever comes first. */
  561. /* CPNT is the beginning of the memsz
  562. portion not backed by filesz. */
  563. cpnt = (char *) (status + size);
  564. /* MAP_SIZE is the address of the
  565. beginning of the next page. */
  566. map_size = (ppnt->p_vaddr + ppnt->p_filesz
  567. + ADDR_ALIGN) & PAGE_ALIGN;
  568. #ifndef MIN
  569. # define MIN(a,b) ((a) < (b) ? (a) : (b))
  570. #endif
  571. _dl_memset (cpnt, 0,
  572. MIN (map_size
  573. - (ppnt->p_vaddr
  574. + ppnt->p_filesz),
  575. ppnt->p_memsz
  576. - ppnt->p_filesz));
  577. if (map_size < ppnt->p_vaddr + ppnt->p_memsz
  578. && !piclib2map) {
  579. tryaddr = map_size + (char*)(piclib ? libaddr : 0);
  580. status = (char *) _dl_mmap(tryaddr,
  581. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  582. LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  583. if (_dl_mmap_check_error(status)
  584. || tryaddr != status)
  585. goto cant_map;
  586. }
  587. } else {
  588. tryaddr = (piclib == 2 ? 0
  589. : (char *) (ppnt->p_vaddr & PAGE_ALIGN)
  590. + (piclib ? libaddr : 0));
  591. size = (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_filesz;
  592. status = (char *) _dl_mmap
  593. (tryaddr, size, LXFLAGS(ppnt->p_flags),
  594. flags | (piclib == 2 ? MAP_EXECUTABLE
  595. | MAP_DENYWRITE : 0),
  596. infile, ppnt->p_offset & OFFS_ALIGN);
  597. if (_dl_mmap_check_error(status)
  598. || (tryaddr && tryaddr != status))
  599. goto cant_map;
  600. DL_INIT_LOADADDR_HDR
  601. (lib_loadaddr, status
  602. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  603. }
  604. /* if (libaddr == 0 && piclib) {
  605. libaddr = (unsigned long) status;
  606. flags |= MAP_FIXED;
  607. } */
  608. }
  609. ppnt++;
  610. }
  611. _dl_close(infile);
  612. /* For a non-PIC library, the addresses are all absolute */
  613. if (piclib) {
  614. dynamic_addr = (unsigned long) DL_RELOC_ADDR(lib_loadaddr, dynamic_addr);
  615. }
  616. /*
  617. * OK, the ELF library is now loaded into VM in the correct locations
  618. * The next step is to go through and do the dynamic linking (if needed).
  619. */
  620. /* Start by scanning the dynamic section to get all of the pointers */
  621. if (!dynamic_addr) {
  622. _dl_internal_error_number = LD_ERROR_NODYNAMIC;
  623. _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
  624. _dl_progname, libname);
  625. _dl_munmap(header, _dl_pagesize);
  626. return NULL;
  627. }
  628. dpnt = (ElfW(Dyn) *) dynamic_addr;
  629. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  630. #ifdef __LDSO_STANDALONE_SUPPORT__
  631. rtld_flags = _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, piclib ? lib_loadaddr : 0);
  632. #else
  633. rtld_flags = _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
  634. #endif
  635. /* If the TEXTREL is set, this means that we need to make the pages
  636. writable before we perform relocations. Do this now. They get set
  637. back again later. */
  638. if (dynamic_info[DT_TEXTREL]) {
  639. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  640. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  641. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  642. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W)) {
  643. _dl_mprotect((void *) ((piclib ? libaddr : 0) +
  644. (ppnt->p_vaddr & PAGE_ALIGN)),
  645. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  646. PROT_READ | PROT_WRITE | PROT_EXEC);
  647. }
  648. }
  649. #else
  650. _dl_dprintf(_dl_debug_file, "Can't modify %s's text section."
  651. " Use GCC option -fPIC for shared objects, please.\n",
  652. libname);
  653. _dl_exit(1);
  654. #endif
  655. }
  656. tpnt = _dl_add_elf_hash_table(libname, lib_loadaddr, dynamic_info,
  657. dynamic_addr, 0);
  658. tpnt->relro_addr = relro_addr;
  659. tpnt->relro_size = relro_size;
  660. tpnt->st_dev = st.st_dev;
  661. tpnt->st_ino = st.st_ino;
  662. tpnt->ppnt = (ElfW(Phdr) *) DL_RELOC_ADDR(tpnt->loadaddr, epnt->e_phoff);
  663. tpnt->n_phent = epnt->e_phnum;
  664. tpnt->rtld_flags |= rtld_flags;
  665. #ifdef __LDSO_STANDALONE_SUPPORT__
  666. tpnt->l_entry = epnt->e_entry;
  667. #endif
  668. #if defined(USE_TLS) && USE_TLS
  669. if (tlsppnt) {
  670. _dl_debug_early("Found TLS header for %s\n", libname);
  671. # if NO_TLS_OFFSET != 0
  672. tpnt->l_tls_offset = NO_TLS_OFFSET;
  673. # endif
  674. tpnt->l_tls_blocksize = tlsppnt->p_memsz;
  675. tpnt->l_tls_align = tlsppnt->p_align;
  676. if (tlsppnt->p_align == 0)
  677. tpnt->l_tls_firstbyte_offset = 0;
  678. else
  679. tpnt->l_tls_firstbyte_offset = tlsppnt->p_vaddr &
  680. (tlsppnt->p_align - 1);
  681. tpnt->l_tls_initimage_size = tlsppnt->p_filesz;
  682. tpnt->l_tls_initimage = (void *) tlsppnt->p_vaddr;
  683. /* Assign the next available module ID. */
  684. tpnt->l_tls_modid = _dl_next_tls_modid ();
  685. /* We know the load address, so add it to the offset. */
  686. #ifdef __LDSO_STANDALONE_SUPPORT__
  687. if ((tpnt->l_tls_initimage != NULL) && piclib)
  688. #else
  689. if (tpnt->l_tls_initimage != NULL)
  690. #endif
  691. {
  692. # ifdef __SUPPORT_LD_DEBUG_EARLY__
  693. unsigned int tmp = (unsigned int) tpnt->l_tls_initimage;
  694. tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
  695. _dl_debug_early("Relocated TLS initial image from %x to %x (size = %x)\n", tmp, tpnt->l_tls_initimage, tpnt->l_tls_initimage_size);
  696. tmp = 0;
  697. # else
  698. tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
  699. # endif
  700. }
  701. }
  702. #endif
  703. /*
  704. * Add this object into the symbol chain
  705. */
  706. if (*rpnt
  707. #ifdef __LDSO_STANDALONE_SUPPORT__
  708. /* Do not create a new chain entry for the main executable */
  709. && (*rpnt)->dyn
  710. #endif
  711. ) {
  712. (*rpnt)->next = _dl_malloc(sizeof(struct dyn_elf));
  713. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  714. (*rpnt)->next->prev = (*rpnt);
  715. *rpnt = (*rpnt)->next;
  716. }
  717. #ifndef SHARED
  718. /* When statically linked, the first time we dlopen a DSO
  719. * the *rpnt is NULL, so we need to allocate memory for it,
  720. * and initialize the _dl_symbol_table.
  721. */
  722. else {
  723. *rpnt = _dl_symbol_tables = _dl_malloc(sizeof(struct dyn_elf));
  724. _dl_memset(*rpnt, 0, sizeof(struct dyn_elf));
  725. }
  726. #endif
  727. (*rpnt)->dyn = tpnt;
  728. tpnt->symbol_scope = _dl_symbol_tables;
  729. tpnt->usage_count++;
  730. #ifdef __LDSO_STANDALONE_SUPPORT__
  731. tpnt->libtype = (epnt->e_type == ET_DYN) ? elf_lib : elf_executable;
  732. #else
  733. tpnt->libtype = elf_lib;
  734. #endif
  735. /*
  736. * OK, the next thing we need to do is to insert the dynamic linker into
  737. * the proper entry in the GOT so that the PLT symbols can be properly
  738. * resolved.
  739. */
  740. lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
  741. if (lpnt) {
  742. lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
  743. INIT_GOT(lpnt, tpnt);
  744. }
  745. _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname);
  746. _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr, DL_LOADADDR_BASE(lib_loadaddr));
  747. _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
  748. DL_RELOC_ADDR(lib_loadaddr, epnt->e_entry), tpnt->ppnt, tpnt->n_phent);
  749. _dl_munmap(header, _dl_pagesize);
  750. return tpnt;
  751. }
  752. /* now_flag must be RTLD_NOW or zero */
  753. int _dl_fixup(struct dyn_elf *rpnt, int now_flag)
  754. {
  755. int goof = 0;
  756. struct elf_resolve *tpnt;
  757. ElfW(Word) reloc_size, relative_count;
  758. ElfW(Addr) reloc_addr;
  759. if (rpnt->next)
  760. goof = _dl_fixup(rpnt->next, now_flag);
  761. if (goof)
  762. return goof;
  763. tpnt = rpnt->dyn;
  764. if (!(tpnt->init_flag & RELOCS_DONE))
  765. _dl_if_debug_dprint("relocation processing: %s\n", tpnt->libname);
  766. if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
  767. _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
  768. _dl_progname, UNSUPPORTED_RELOC_STR);
  769. goof++;
  770. return goof;
  771. }
  772. reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
  773. /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
  774. range. Note that according to the ELF spec, this is completely legal! */
  775. #ifdef ELF_MACHINE_PLTREL_OVERLAP
  776. reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
  777. #endif
  778. if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
  779. !(tpnt->init_flag & RELOCS_DONE)) {
  780. reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
  781. relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
  782. if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
  783. reloc_size -= relative_count * sizeof(ELF_RELOC);
  784. elf_machine_relative(tpnt->loadaddr, reloc_addr, relative_count);
  785. reloc_addr += relative_count * sizeof(ELF_RELOC);
  786. }
  787. goof += _dl_parse_relocation_information(rpnt,
  788. reloc_addr,
  789. reloc_size);
  790. tpnt->init_flag |= RELOCS_DONE;
  791. }
  792. if (tpnt->dynamic_info[DT_BIND_NOW])
  793. now_flag = RTLD_NOW;
  794. if (tpnt->dynamic_info[DT_JMPREL] &&
  795. (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
  796. (now_flag && !(tpnt->rtld_flags & now_flag)))) {
  797. tpnt->rtld_flags |= now_flag;
  798. if (!(tpnt->rtld_flags & RTLD_NOW)) {
  799. _dl_parse_lazy_relocation_information(rpnt,
  800. tpnt->dynamic_info[DT_JMPREL],
  801. tpnt->dynamic_info [DT_PLTRELSZ]);
  802. } else {
  803. goof += _dl_parse_relocation_information(rpnt,
  804. tpnt->dynamic_info[DT_JMPREL],
  805. tpnt->dynamic_info[DT_PLTRELSZ]);
  806. }
  807. tpnt->init_flag |= JMP_RELOCS_DONE;
  808. }
  809. #if 0
  810. /* _dl_add_to_slotinfo is called by init_tls() for initial DSO
  811. or by dlopen() for dynamically loaded DSO. */
  812. #if defined(USE_TLS) && USE_TLS
  813. /* Add object to slot information data if necessasy. */
  814. if (tpnt->l_tls_blocksize != 0 && tls_init_tp_called)
  815. _dl_add_to_slotinfo ((struct link_map *) tpnt);
  816. #endif
  817. #endif
  818. return goof;
  819. }
  820. /* Minimal printf which handles only %s, %d, and %x */
  821. void _dl_dprintf(int fd, const char *fmt, ...)
  822. {
  823. #if __WORDSIZE > 32
  824. long int num;
  825. #else
  826. int num;
  827. #endif
  828. va_list args;
  829. char *start, *ptr, *string;
  830. char *buf;
  831. if (!fmt)
  832. return;
  833. buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  834. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  835. if (_dl_mmap_check_error(buf)) {
  836. _dl_write(fd, "mmap of a spare page failed!\n", 29);
  837. _dl_exit(20);
  838. }
  839. start = ptr = buf;
  840. if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
  841. _dl_write(fd, "overflow\n", 11);
  842. _dl_exit(20);
  843. }
  844. _dl_strcpy(buf, fmt);
  845. va_start(args, fmt);
  846. while (start) {
  847. while (*ptr != '%' && *ptr) {
  848. ptr++;
  849. }
  850. if (*ptr == '%') {
  851. *ptr++ = '\0';
  852. _dl_write(fd, start, _dl_strlen(start));
  853. switch (*ptr++) {
  854. case 's':
  855. string = va_arg(args, char *);
  856. if (!string)
  857. _dl_write(fd, "(null)", 6);
  858. else
  859. _dl_write(fd, string, _dl_strlen(string));
  860. break;
  861. case 'i':
  862. case 'd':
  863. {
  864. char tmp[22];
  865. #if __WORDSIZE > 32
  866. num = va_arg(args, long int);
  867. #else
  868. num = va_arg(args, int);
  869. #endif
  870. string = _dl_simple_ltoa(tmp, num);
  871. _dl_write(fd, string, _dl_strlen(string));
  872. break;
  873. }
  874. case 'x':
  875. case 'X':
  876. {
  877. char tmp[22];
  878. #if __WORDSIZE > 32
  879. num = va_arg(args, long int);
  880. #else
  881. num = va_arg(args, int);
  882. #endif
  883. string = _dl_simple_ltoahex(tmp, num);
  884. _dl_write(fd, string, _dl_strlen(string));
  885. break;
  886. }
  887. default:
  888. _dl_write(fd, "(null)", 6);
  889. break;
  890. }
  891. start = ptr;
  892. } else {
  893. _dl_write(fd, start, _dl_strlen(start));
  894. start = NULL;
  895. }
  896. }
  897. _dl_munmap(buf, _dl_pagesize);
  898. return;
  899. }
  900. char *_dl_strdup(const char *string)
  901. {
  902. char *retval;
  903. int len;
  904. len = _dl_strlen(string);
  905. retval = _dl_malloc(len + 1);
  906. _dl_strcpy(retval, string);
  907. return retval;
  908. }
  909. unsigned int _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[],
  910. void *debug_addr, DL_LOADADDR_TYPE load_off)
  911. {
  912. return __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
  913. }