dl-elf.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  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 == (caddr_t) - 1)
  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, 0)) < 0) {
  48. _dl_cache_addr = (caddr_t) - 1; /* 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 = (caddr_t) - 1;
  86. return -1;
  87. }
  88. int _dl_unmap_cache(void)
  89. {
  90. if (_dl_cache_addr == NULL || _dl_cache_addr == (caddr_t) - 1)
  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 & ~(_dl_pagesize - 1));
  104. ElfW(Addr) end = ((base + l->relro_size) & ~(_dl_pagesize - 1));
  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. /* gcc inlines alloca using a single instruction adjusting
  131. * the stack pointer and no stack overflow check and thus
  132. * no NULL error return. No point leaving in dead code... */
  133. #if 0
  134. if (!path || !mylibname) {
  135. _dl_dprintf(2, "Out of memory!\n");
  136. _dl_exit(0);
  137. }
  138. #endif
  139. _dl_memcpy(path, path_list, done+1);
  140. /* Unlike ldd.c, don't bother to eliminate double //s */
  141. /* Replace colons with zeros in path_list */
  142. /* : at the beginning or end of path maps to CWD */
  143. /* :: anywhere maps CWD */
  144. /* "" maps to CWD */
  145. done = 0;
  146. path_n = path;
  147. do {
  148. if (*path == 0) {
  149. *path = ':';
  150. done = 1;
  151. }
  152. if (*path == ':') {
  153. *path = 0;
  154. if (*path_n)
  155. _dl_strcpy(mylibname, path_n);
  156. else
  157. _dl_strcpy(mylibname, "."); /* Assume current dir if empty path */
  158. _dl_strcat(mylibname, "/");
  159. _dl_strcat(mylibname, name);
  160. if ((tpnt = _dl_load_elf_shared_library(secure, rpnt, mylibname)) != NULL)
  161. return tpnt;
  162. path_n = path+1;
  163. }
  164. path++;
  165. } while (!done);
  166. return NULL;
  167. }
  168. /* Used to return error codes back to dlopen et. al. */
  169. unsigned long _dl_error_number;
  170. unsigned long _dl_internal_error_number;
  171. struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
  172. struct elf_resolve *tpnt, char *full_libname, int __attribute__((unused)) trace_loaded_objects)
  173. {
  174. char *pnt;
  175. struct elf_resolve *tpnt1;
  176. char *libname;
  177. _dl_internal_error_number = 0;
  178. libname = full_libname;
  179. /* quick hack to ensure mylibname buffer doesn't overflow. don't
  180. allow full_libname or any directory to be longer than 1024. */
  181. if (_dl_strlen(full_libname) > 1024)
  182. goto goof;
  183. /* Skip over any initial initial './' and '/' stuff to
  184. * get the short form libname with no path garbage */
  185. pnt = _dl_strrchr(libname, '/');
  186. if (pnt) {
  187. libname = pnt + 1;
  188. }
  189. _dl_if_debug_dprint("\tfind library='%s'; searching\n", libname);
  190. /* If the filename has any '/', try it straight and leave it at that.
  191. For IBCS2 compatibility under linux, we substitute the string
  192. /usr/i486-sysv4/lib for /usr/lib in library names. */
  193. if (libname != full_libname) {
  194. _dl_if_debug_dprint("\ttrying file='%s'\n", full_libname);
  195. tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
  196. if (tpnt1) {
  197. return tpnt1;
  198. }
  199. }
  200. /*
  201. * The ABI specifies that RPATH is searched before LD_LIBRARY_PATH or
  202. * the default path of /usr/lib. Check in rpath directories.
  203. */
  204. #ifdef __LDSO_RUNPATH__
  205. pnt = (tpnt ? (char *) tpnt->dynamic_info[DT_RPATH] : NULL);
  206. if (pnt) {
  207. pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
  208. _dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt);
  209. if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
  210. return tpnt1;
  211. }
  212. #endif
  213. /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
  214. if (_dl_library_path) {
  215. _dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
  216. if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
  217. {
  218. return tpnt1;
  219. }
  220. }
  221. /*
  222. * The ABI specifies that RUNPATH is searched after LD_LIBRARY_PATH.
  223. */
  224. #ifdef __LDSO_RUNPATH__
  225. pnt = (tpnt ? (char *)tpnt->dynamic_info[DT_RUNPATH] : NULL);
  226. if (pnt) {
  227. pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
  228. _dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt);
  229. if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
  230. return tpnt1;
  231. }
  232. #endif
  233. /*
  234. * Where should the cache be searched? There is no such concept in the
  235. * ABI, so we have some flexibility here. For now, search it before
  236. * the hard coded paths that follow (i.e before /lib and /usr/lib).
  237. */
  238. #ifdef __LDSO_CACHE_SUPPORT__
  239. if (_dl_cache_addr != NULL && _dl_cache_addr != (caddr_t) - 1) {
  240. int i;
  241. header_t *header = (header_t *) _dl_cache_addr;
  242. libentry_t *libent = (libentry_t *) & header[1];
  243. char *strs = (char *) &libent[header->nlibs];
  244. _dl_if_debug_dprint("\tsearching cache='%s'\n", LDSO_CACHE);
  245. for (i = 0; i < header->nlibs; i++) {
  246. if ((libent[i].flags == LIB_ELF ||
  247. libent[i].flags == LIB_ELF_LIBC0 ||
  248. libent[i].flags == LIB_ELF_LIBC5) &&
  249. _dl_strcmp(libname, strs + libent[i].sooffset) == 0 &&
  250. (tpnt1 = _dl_load_elf_shared_library(secure,
  251. rpnt, strs + libent[i].liboffset)))
  252. return tpnt1;
  253. }
  254. }
  255. #endif
  256. /* Look for libraries wherever the shared library loader
  257. * was installed */
  258. _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath);
  259. if ((tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt)) != NULL)
  260. {
  261. return tpnt1;
  262. }
  263. /* Lastly, search the standard list of paths for the library.
  264. This list must exactly match the list in uClibc/ldso/util/ldd.c */
  265. _dl_if_debug_dprint("\tsearching full lib path list\n");
  266. if ((tpnt1 = search_for_named_library(libname, secure,
  267. UCLIBC_RUNTIME_PREFIX "lib:"
  268. UCLIBC_RUNTIME_PREFIX "usr/lib"
  269. #ifndef __LDSO_CACHE_SUPPORT__
  270. ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
  271. #endif
  272. , rpnt)
  273. ) != NULL)
  274. {
  275. return tpnt1;
  276. }
  277. goof:
  278. /* Well, we shot our wad on that one. All we can do now is punt */
  279. if (_dl_internal_error_number)
  280. _dl_error_number = _dl_internal_error_number;
  281. else
  282. _dl_error_number = LD_ERROR_NOFILE;
  283. _dl_if_debug_dprint("Bummer: could not find '%s'!\n", libname);
  284. return NULL;
  285. }
  286. /*
  287. * Read one ELF library into memory, mmap it into the correct locations and
  288. * add the symbol info to the symbol chain. Perform any relocations that
  289. * are required.
  290. */
  291. struct elf_resolve *_dl_load_elf_shared_library(int secure,
  292. struct dyn_elf **rpnt, char *libname)
  293. {
  294. ElfW(Ehdr) *epnt;
  295. unsigned long dynamic_addr = 0;
  296. ElfW(Dyn) *dpnt;
  297. struct elf_resolve *tpnt;
  298. ElfW(Phdr) *ppnt;
  299. char *status, *header;
  300. unsigned long dynamic_info[DYNAMIC_SIZE];
  301. unsigned long *lpnt;
  302. unsigned long libaddr;
  303. unsigned long minvma = 0xffffffff, maxvma = 0;
  304. int i, flags, piclib, infile;
  305. ElfW(Addr) relro_addr = 0;
  306. size_t relro_size = 0;
  307. struct stat st;
  308. DL_LOADADDR_TYPE lib_loadaddr;
  309. DL_INIT_LOADADDR_EXTRA_DECLS
  310. libaddr = 0;
  311. infile = _dl_open(libname, O_RDONLY, 0);
  312. if (infile < 0) {
  313. _dl_internal_error_number = LD_ERROR_NOFILE;
  314. return NULL;
  315. }
  316. if (_dl_fstat(infile, &st) < 0) {
  317. _dl_internal_error_number = LD_ERROR_NOFILE;
  318. _dl_close(infile);
  319. return NULL;
  320. }
  321. /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
  322. we don't load the library if it isn't setuid. */
  323. if (secure)
  324. if (!(st.st_mode & S_ISUID)) {
  325. _dl_close(infile);
  326. return NULL;
  327. }
  328. /* Check if file is already loaded */
  329. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  330. if (tpnt->st_dev == st.st_dev && tpnt->st_ino == st.st_ino) {
  331. /* Already loaded */
  332. tpnt->usage_count++;
  333. _dl_close(infile);
  334. return tpnt;
  335. }
  336. }
  337. header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  338. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  339. if (_dl_mmap_check_error(header)) {
  340. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  341. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  342. _dl_close(infile);
  343. return NULL;
  344. }
  345. _dl_read(infile, header, _dl_pagesize);
  346. epnt = (ElfW(Ehdr) *) (intptr_t) header;
  347. if (epnt->e_ident[0] != 0x7f ||
  348. epnt->e_ident[1] != 'E' ||
  349. epnt->e_ident[2] != 'L' ||
  350. epnt->e_ident[3] != 'F')
  351. {
  352. _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  353. libname);
  354. _dl_internal_error_number = LD_ERROR_NOTELF;
  355. _dl_close(infile);
  356. _dl_munmap(header, _dl_pagesize);
  357. return NULL;
  358. }
  359. if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
  360. #ifdef MAGIC2
  361. && epnt->e_machine != MAGIC2
  362. #endif
  363. ))
  364. {
  365. _dl_internal_error_number =
  366. (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
  367. _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  368. "\n", _dl_progname, libname);
  369. _dl_close(infile);
  370. _dl_munmap(header, _dl_pagesize);
  371. return NULL;
  372. }
  373. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  374. piclib = 1;
  375. for (i = 0; i < epnt->e_phnum; i++) {
  376. if (ppnt->p_type == PT_DYNAMIC) {
  377. if (dynamic_addr)
  378. _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
  379. _dl_progname, libname);
  380. dynamic_addr = ppnt->p_vaddr;
  381. }
  382. if (ppnt->p_type == PT_LOAD) {
  383. /* See if this is a PIC library. */
  384. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  385. piclib = 0;
  386. minvma = ppnt->p_vaddr;
  387. }
  388. if (piclib && ppnt->p_vaddr < minvma) {
  389. minvma = ppnt->p_vaddr;
  390. }
  391. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  392. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  393. }
  394. }
  395. ppnt++;
  396. }
  397. DL_CHECK_LIB_TYPE (epnt, piclib, _dl_progname, libname);
  398. maxvma = (maxvma + ADDR_ALIGN) & ~ADDR_ALIGN;
  399. minvma = minvma & ~0xffffU;
  400. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  401. if (!piclib)
  402. flags |= MAP_FIXED;
  403. if (piclib == 0 || piclib == 1) {
  404. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  405. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  406. if (_dl_mmap_check_error(status)) {
  407. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  408. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  409. _dl_close(infile);
  410. _dl_munmap(header, _dl_pagesize);
  411. return NULL;
  412. }
  413. libaddr = (unsigned long) status;
  414. flags |= MAP_FIXED;
  415. }
  416. /* Get the memory to store the library */
  417. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  418. DL_INIT_LOADADDR(lib_loadaddr, libaddr, ppnt, epnt->e_phnum);
  419. for (i = 0; i < epnt->e_phnum; i++) {
  420. if (DL_IS_SPECIAL_SEGMENT (epnt, ppnt)) {
  421. char *addr;
  422. addr = DL_MAP_SEGMENT (epnt, ppnt, infile, flags);
  423. if (addr == NULL)
  424. goto cant_map;
  425. DL_INIT_LOADADDR_HDR (lib_loadaddr, addr, ppnt);
  426. ppnt++;
  427. continue;
  428. }
  429. if (ppnt->p_type == PT_GNU_RELRO) {
  430. relro_addr = ppnt->p_vaddr;
  431. relro_size = ppnt->p_memsz;
  432. }
  433. if (ppnt->p_type == PT_LOAD) {
  434. char *tryaddr;
  435. ssize_t size;
  436. /* See if this is a PIC library. */
  437. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  438. piclib = 0;
  439. /* flags |= MAP_FIXED; */
  440. }
  441. if (ppnt->p_flags & PF_W) {
  442. unsigned long map_size;
  443. char *cpnt;
  444. char *piclib2map = 0;
  445. if (piclib == 2 &&
  446. /* We might be able to avoid this
  447. call if memsz doesn't require
  448. an additional page, but this
  449. would require mmap to always
  450. return page-aligned addresses
  451. and a whole number of pages
  452. allocated. Unfortunately on
  453. uClinux may return misaligned
  454. addresses and may allocate
  455. partial pages, so we may end up
  456. doing unnecessary mmap calls.
  457. This is what we could do if we
  458. knew mmap would always return
  459. aligned pages:
  460. ((ppnt->p_vaddr + ppnt->p_filesz
  461. + ADDR_ALIGN)
  462. & PAGE_ALIGN)
  463. < ppnt->p_vaddr + ppnt->p_memsz)
  464. Instead, we have to do this: */
  465. ppnt->p_filesz < ppnt->p_memsz)
  466. {
  467. piclib2map = (char *)
  468. _dl_mmap(0, (ppnt->p_vaddr & ADDR_ALIGN)
  469. + ppnt->p_memsz,
  470. LXFLAGS(ppnt->p_flags),
  471. flags | MAP_ANONYMOUS, -1, 0);
  472. if (_dl_mmap_check_error(piclib2map))
  473. goto cant_map;
  474. DL_INIT_LOADADDR_HDR
  475. (lib_loadaddr, piclib2map
  476. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  477. }
  478. tryaddr = piclib == 2 ? piclib2map
  479. : ((char*) (piclib ? libaddr : 0) +
  480. (ppnt->p_vaddr & PAGE_ALIGN));
  481. size = (ppnt->p_vaddr & ADDR_ALIGN)
  482. + ppnt->p_filesz;
  483. /* For !MMU, mmap to fixed address will fail.
  484. So instead of desperately call mmap and fail,
  485. we set status to MAP_FAILED to save a call
  486. to mmap (). */
  487. #ifndef __ARCH_USE_MMU__
  488. if (piclib2map == 0)
  489. #endif
  490. status = (char *) _dl_mmap
  491. (tryaddr, size, LXFLAGS(ppnt->p_flags),
  492. flags | (piclib2map ? MAP_FIXED : 0),
  493. infile, ppnt->p_offset & OFFS_ALIGN);
  494. #ifndef __ARCH_USE_MMU__
  495. else
  496. status = MAP_FAILED;
  497. #endif
  498. #ifdef _DL_PREAD
  499. if (_dl_mmap_check_error(status) && piclib2map
  500. && (_DL_PREAD (infile, tryaddr, size,
  501. ppnt->p_offset & OFFS_ALIGN)
  502. == size))
  503. status = tryaddr;
  504. #endif
  505. if (_dl_mmap_check_error(status)
  506. || (tryaddr && tryaddr != status)) {
  507. cant_map:
  508. _dl_dprintf(2, "%s:%i: can't map '%s'\n",
  509. _dl_progname, __LINE__, libname);
  510. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  511. DL_LOADADDR_UNMAP (lib_loadaddr, maxvma - minvma);
  512. _dl_close(infile);
  513. _dl_munmap(header, _dl_pagesize);
  514. return NULL;
  515. }
  516. if (! piclib2map) {
  517. DL_INIT_LOADADDR_HDR
  518. (lib_loadaddr, status
  519. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  520. }
  521. /* Now we want to allocate and
  522. zero-out any data from the end of
  523. the region we mapped in from the
  524. file (filesz) to the end of the
  525. loadable segment (memsz). We may
  526. need additional pages for memsz,
  527. that we map in below, and we can
  528. count on the kernel to zero them
  529. out, but we have to zero out stuff
  530. in the last page that we mapped in
  531. from the file. However, we can't
  532. assume to have actually obtained
  533. full pages from the kernel, since
  534. we didn't ask for them, and uClibc
  535. may not give us full pages for
  536. small allocations. So only zero
  537. out up to memsz or the end of the
  538. page, whichever comes first. */
  539. /* CPNT is the beginning of the memsz
  540. portion not backed by filesz. */
  541. cpnt = (char *) (status + size);
  542. /* MAP_SIZE is the address of the
  543. beginning of the next page. */
  544. map_size = (ppnt->p_vaddr + ppnt->p_filesz
  545. + ADDR_ALIGN) & PAGE_ALIGN;
  546. #ifndef MIN
  547. # define MIN(a,b) ((a) < (b) ? (a) : (b))
  548. #endif
  549. _dl_memset (cpnt, 0,
  550. MIN (map_size
  551. - (ppnt->p_vaddr
  552. + ppnt->p_filesz),
  553. ppnt->p_memsz
  554. - ppnt->p_filesz));
  555. if (map_size < ppnt->p_vaddr + ppnt->p_memsz
  556. && !piclib2map) {
  557. tryaddr = map_size + (char*)(piclib ? libaddr : 0);
  558. status = (char *) _dl_mmap(tryaddr,
  559. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  560. LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  561. if (_dl_mmap_check_error(status)
  562. || tryaddr != status)
  563. goto cant_map;
  564. }
  565. } else {
  566. tryaddr = (piclib == 2 ? 0
  567. : (char *) (ppnt->p_vaddr & PAGE_ALIGN)
  568. + (piclib ? libaddr : 0));
  569. size = (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_filesz;
  570. status = (char *) _dl_mmap
  571. (tryaddr, size, LXFLAGS(ppnt->p_flags),
  572. flags | (piclib == 2 ? MAP_EXECUTABLE
  573. | MAP_DENYWRITE : 0),
  574. infile, ppnt->p_offset & OFFS_ALIGN);
  575. if (_dl_mmap_check_error(status)
  576. || (tryaddr && tryaddr != status))
  577. goto cant_map;
  578. DL_INIT_LOADADDR_HDR
  579. (lib_loadaddr, status
  580. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  581. }
  582. /* if (libaddr == 0 && piclib) {
  583. libaddr = (unsigned long) status;
  584. flags |= MAP_FIXED;
  585. } */
  586. }
  587. ppnt++;
  588. }
  589. _dl_close(infile);
  590. /* For a non-PIC library, the addresses are all absolute */
  591. if (piclib) {
  592. dynamic_addr = (unsigned long) DL_RELOC_ADDR(lib_loadaddr, dynamic_addr);
  593. }
  594. /*
  595. * OK, the ELF library is now loaded into VM in the correct locations
  596. * The next step is to go through and do the dynamic linking (if needed).
  597. */
  598. /* Start by scanning the dynamic section to get all of the pointers */
  599. if (!dynamic_addr) {
  600. _dl_internal_error_number = LD_ERROR_NODYNAMIC;
  601. _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
  602. _dl_progname, libname);
  603. _dl_munmap(header, _dl_pagesize);
  604. return NULL;
  605. }
  606. dpnt = (ElfW(Dyn) *) dynamic_addr;
  607. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  608. _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
  609. /* If the TEXTREL is set, this means that we need to make the pages
  610. writable before we perform relocations. Do this now. They get set
  611. back again later. */
  612. if (dynamic_info[DT_TEXTREL]) {
  613. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  614. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  615. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  616. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  617. _dl_mprotect((void *) ((piclib ? libaddr : 0) +
  618. (ppnt->p_vaddr & PAGE_ALIGN)),
  619. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  620. PROT_READ | PROT_WRITE | PROT_EXEC);
  621. }
  622. #else
  623. _dl_dprintf(_dl_debug_file, "Can't modify %s's text section. Use GCC option -fPIC for shared objects, please.\n",libname);
  624. _dl_exit(1);
  625. #endif
  626. }
  627. tpnt = _dl_add_elf_hash_table(libname, lib_loadaddr, dynamic_info,
  628. dynamic_addr, 0);
  629. tpnt->relro_addr = relro_addr;
  630. tpnt->relro_size = relro_size;
  631. tpnt->st_dev = st.st_dev;
  632. tpnt->st_ino = st.st_ino;
  633. tpnt->ppnt = (ElfW(Phdr) *) DL_RELOC_ADDR(tpnt->loadaddr, epnt->e_phoff);
  634. tpnt->n_phent = epnt->e_phnum;
  635. /*
  636. * Add this object into the symbol chain
  637. */
  638. if (*rpnt) {
  639. (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  640. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  641. (*rpnt)->next->prev = (*rpnt);
  642. *rpnt = (*rpnt)->next;
  643. }
  644. #ifndef SHARED
  645. /* When statically linked, the first time we dlopen a DSO
  646. * the *rpnt is NULL, so we need to allocate memory for it,
  647. * and initialize the _dl_symbol_table.
  648. */
  649. else {
  650. *rpnt = _dl_symbol_tables = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  651. _dl_memset(*rpnt, 0, sizeof(struct dyn_elf));
  652. }
  653. #endif
  654. (*rpnt)->dyn = tpnt;
  655. tpnt->symbol_scope = _dl_symbol_tables;
  656. tpnt->usage_count++;
  657. tpnt->libtype = elf_lib;
  658. /*
  659. * OK, the next thing we need to do is to insert the dynamic linker into
  660. * the proper entry in the GOT so that the PLT symbols can be properly
  661. * resolved.
  662. */
  663. lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
  664. if (lpnt) {
  665. lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
  666. INIT_GOT(lpnt, tpnt);
  667. }
  668. _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname);
  669. _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr, DL_LOADADDR_BASE(lib_loadaddr));
  670. _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
  671. DL_RELOC_ADDR(lib_loadaddr, epnt->e_entry), tpnt->ppnt, tpnt->n_phent);
  672. _dl_munmap(header, _dl_pagesize);
  673. return tpnt;
  674. }
  675. /* now_flag must be RTLD_NOW or zero */
  676. int _dl_fixup(struct dyn_elf *rpnt, int now_flag)
  677. {
  678. int goof = 0;
  679. struct elf_resolve *tpnt;
  680. ElfW(Word) reloc_size, relative_count;
  681. ElfW(Addr) reloc_addr;
  682. if (rpnt->next)
  683. goof = _dl_fixup(rpnt->next, now_flag);
  684. if (goof)
  685. return goof;
  686. tpnt = rpnt->dyn;
  687. if (!(tpnt->init_flag & RELOCS_DONE))
  688. _dl_if_debug_dprint("relocation processing: %s\n", tpnt->libname);
  689. if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
  690. _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
  691. _dl_progname, UNSUPPORTED_RELOC_STR);
  692. goof++;
  693. return goof;
  694. }
  695. reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
  696. /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
  697. range. Note that according to the ELF spec, this is completely legal! */
  698. #ifdef ELF_MACHINE_PLTREL_OVERLAP
  699. reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
  700. #endif
  701. if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
  702. !(tpnt->init_flag & RELOCS_DONE)) {
  703. reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
  704. relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
  705. if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
  706. reloc_size -= relative_count * sizeof(ELF_RELOC);
  707. elf_machine_relative(tpnt->loadaddr, reloc_addr, relative_count);
  708. reloc_addr += relative_count * sizeof(ELF_RELOC);
  709. }
  710. goof += _dl_parse_relocation_information(rpnt,
  711. reloc_addr,
  712. reloc_size);
  713. tpnt->init_flag |= RELOCS_DONE;
  714. }
  715. if (tpnt->dynamic_info[DT_BIND_NOW])
  716. now_flag = RTLD_NOW;
  717. if (tpnt->dynamic_info[DT_JMPREL] &&
  718. (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
  719. (now_flag && !(tpnt->rtld_flags & now_flag)))) {
  720. tpnt->rtld_flags |= now_flag;
  721. if (!(tpnt->rtld_flags & RTLD_NOW)) {
  722. _dl_parse_lazy_relocation_information(rpnt,
  723. tpnt->dynamic_info[DT_JMPREL],
  724. tpnt->dynamic_info [DT_PLTRELSZ]);
  725. } else {
  726. goof += _dl_parse_relocation_information(rpnt,
  727. tpnt->dynamic_info[DT_JMPREL],
  728. tpnt->dynamic_info[DT_PLTRELSZ]);
  729. }
  730. tpnt->init_flag |= JMP_RELOCS_DONE;
  731. }
  732. return goof;
  733. }
  734. /* Minimal printf which handles only %s, %d, and %x */
  735. void _dl_dprintf(int fd, const char *fmt, ...)
  736. {
  737. #if __WORDSIZE > 32
  738. long int num;
  739. #else
  740. int num;
  741. #endif
  742. va_list args;
  743. char *start, *ptr, *string;
  744. static char *buf;
  745. if (!fmt)
  746. return;
  747. buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  748. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  749. if (_dl_mmap_check_error(buf)) {
  750. _dl_write(fd, "mmap of a spare page failed!\n", 29);
  751. _dl_exit(20);
  752. }
  753. start = ptr = buf;
  754. if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
  755. _dl_write(fd, "overflow\n", 11);
  756. _dl_exit(20);
  757. }
  758. _dl_strcpy(buf, fmt);
  759. va_start(args, fmt);
  760. while (start) {
  761. while (*ptr != '%' && *ptr) {
  762. ptr++;
  763. }
  764. if (*ptr == '%') {
  765. *ptr++ = '\0';
  766. _dl_write(fd, start, _dl_strlen(start));
  767. switch (*ptr++) {
  768. case 's':
  769. string = va_arg(args, char *);
  770. if (!string)
  771. _dl_write(fd, "(null)", 6);
  772. else
  773. _dl_write(fd, string, _dl_strlen(string));
  774. break;
  775. case 'i':
  776. case 'd':
  777. {
  778. char tmp[22];
  779. #if __WORDSIZE > 32
  780. num = va_arg(args, long int);
  781. #else
  782. num = va_arg(args, int);
  783. #endif
  784. string = _dl_simple_ltoa(tmp, num);
  785. _dl_write(fd, string, _dl_strlen(string));
  786. break;
  787. }
  788. case 'x':
  789. case 'X':
  790. {
  791. char tmp[22];
  792. #if __WORDSIZE > 32
  793. num = va_arg(args, long int);
  794. #else
  795. num = va_arg(args, int);
  796. #endif
  797. string = _dl_simple_ltoahex(tmp, num);
  798. _dl_write(fd, string, _dl_strlen(string));
  799. break;
  800. }
  801. default:
  802. _dl_write(fd, "(null)", 6);
  803. break;
  804. }
  805. start = ptr;
  806. } else {
  807. _dl_write(fd, start, _dl_strlen(start));
  808. start = NULL;
  809. }
  810. }
  811. _dl_munmap(buf, _dl_pagesize);
  812. return;
  813. }
  814. char *_dl_strdup(const char *string)
  815. {
  816. char *retval;
  817. int len;
  818. len = _dl_strlen(string);
  819. retval = _dl_malloc(len + 1);
  820. _dl_strcpy(retval, string);
  821. return retval;
  822. }
  823. void _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[],
  824. void *debug_addr, DL_LOADADDR_TYPE load_off)
  825. {
  826. __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
  827. }
  828. /* we want this in ldso.so and libdl.a but nowhere else */
  829. #ifdef __USE_GNU
  830. #if defined IS_IN_rtld || (defined IS_IN_libdl && ! defined SHARED)
  831. extern __typeof(dl_iterate_phdr) __dl_iterate_phdr;
  832. int
  833. __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data)
  834. {
  835. struct elf_resolve *l;
  836. struct dl_phdr_info info;
  837. int ret = 0;
  838. for (l = _dl_loaded_modules; l != NULL; l = l->next) {
  839. info.dlpi_addr = l->loadaddr;
  840. info.dlpi_name = l->libname;
  841. info.dlpi_phdr = l->ppnt;
  842. info.dlpi_phnum = l->n_phent;
  843. ret = callback (&info, sizeof (struct dl_phdr_info), data);
  844. if (ret)
  845. break;
  846. }
  847. return ret;
  848. }
  849. strong_alias(__dl_iterate_phdr, dl_iterate_phdr)
  850. #endif
  851. #endif