dl-elf.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  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 & 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 != (caddr_t) - 1) {
  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, 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. char *status, *header;
  289. unsigned long dynamic_info[DYNAMIC_SIZE];
  290. unsigned long *lpnt;
  291. unsigned long libaddr;
  292. unsigned long minvma = 0xffffffff, maxvma = 0;
  293. int i, flags, piclib, infile;
  294. ElfW(Addr) relro_addr = 0;
  295. size_t relro_size = 0;
  296. struct stat st;
  297. uint32_t *p32;
  298. DL_LOADADDR_TYPE lib_loadaddr;
  299. DL_INIT_LOADADDR_EXTRA_DECLS
  300. libaddr = 0;
  301. infile = _dl_open(libname, O_RDONLY, 0);
  302. if (infile < 0) {
  303. _dl_internal_error_number = LD_ERROR_NOFILE;
  304. return NULL;
  305. }
  306. if (_dl_fstat(infile, &st) < 0) {
  307. _dl_internal_error_number = LD_ERROR_NOFILE;
  308. _dl_close(infile);
  309. return NULL;
  310. }
  311. /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
  312. we don't load the library if it isn't setuid. */
  313. if (secure) {
  314. if (!(st.st_mode & S_ISUID)) {
  315. _dl_close(infile);
  316. return NULL;
  317. }
  318. }
  319. /* Check if file is already loaded */
  320. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  321. if (tpnt->st_dev == st.st_dev && tpnt->st_ino == st.st_ino) {
  322. /* Already loaded */
  323. tpnt->usage_count++;
  324. _dl_close(infile);
  325. return tpnt;
  326. }
  327. }
  328. header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  329. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  330. if (_dl_mmap_check_error(header)) {
  331. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  332. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  333. _dl_close(infile);
  334. return NULL;
  335. }
  336. _dl_read(infile, header, _dl_pagesize);
  337. epnt = (ElfW(Ehdr) *) (intptr_t) header;
  338. p32 = (uint32_t*)&epnt->e_ident;
  339. if (*p32 != ELFMAG_U32) {
  340. _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  341. libname);
  342. _dl_internal_error_number = LD_ERROR_NOTELF;
  343. _dl_close(infile);
  344. _dl_munmap(header, _dl_pagesize);
  345. return NULL;
  346. }
  347. if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
  348. #ifdef MAGIC2
  349. && epnt->e_machine != MAGIC2
  350. #endif
  351. ))
  352. {
  353. _dl_internal_error_number =
  354. (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
  355. _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  356. "\n", _dl_progname, libname);
  357. _dl_close(infile);
  358. _dl_munmap(header, _dl_pagesize);
  359. return NULL;
  360. }
  361. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  362. piclib = 1;
  363. for (i = 0; i < epnt->e_phnum; i++) {
  364. if (ppnt->p_type == PT_DYNAMIC) {
  365. if (dynamic_addr)
  366. _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
  367. _dl_progname, libname);
  368. dynamic_addr = ppnt->p_vaddr;
  369. }
  370. if (ppnt->p_type == PT_LOAD) {
  371. /* See if this is a PIC library. */
  372. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  373. piclib = 0;
  374. minvma = ppnt->p_vaddr;
  375. }
  376. if (piclib && ppnt->p_vaddr < minvma) {
  377. minvma = ppnt->p_vaddr;
  378. }
  379. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  380. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  381. }
  382. }
  383. ppnt++;
  384. }
  385. DL_CHECK_LIB_TYPE (epnt, piclib, _dl_progname, libname);
  386. maxvma = (maxvma + ADDR_ALIGN) & PAGE_ALIGN;
  387. minvma = minvma & ~0xffffU;
  388. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  389. if (!piclib)
  390. flags |= MAP_FIXED;
  391. if (piclib == 0 || piclib == 1) {
  392. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  393. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  394. if (_dl_mmap_check_error(status)) {
  395. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  396. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  397. _dl_close(infile);
  398. _dl_munmap(header, _dl_pagesize);
  399. return NULL;
  400. }
  401. libaddr = (unsigned long) status;
  402. flags |= MAP_FIXED;
  403. }
  404. /* Get the memory to store the library */
  405. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  406. DL_INIT_LOADADDR(lib_loadaddr, libaddr, ppnt, epnt->e_phnum);
  407. for (i = 0; i < epnt->e_phnum; i++) {
  408. if (DL_IS_SPECIAL_SEGMENT (epnt, ppnt)) {
  409. char *addr;
  410. addr = DL_MAP_SEGMENT (epnt, ppnt, infile, flags);
  411. if (addr == NULL)
  412. goto cant_map;
  413. DL_INIT_LOADADDR_HDR (lib_loadaddr, addr, ppnt);
  414. ppnt++;
  415. continue;
  416. }
  417. if (ppnt->p_type == PT_GNU_RELRO) {
  418. relro_addr = ppnt->p_vaddr;
  419. relro_size = ppnt->p_memsz;
  420. }
  421. if (ppnt->p_type == PT_LOAD) {
  422. char *tryaddr;
  423. ssize_t size;
  424. /* See if this is a PIC library. */
  425. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  426. piclib = 0;
  427. /* flags |= MAP_FIXED; */
  428. }
  429. if (ppnt->p_flags & PF_W) {
  430. unsigned long map_size;
  431. char *cpnt;
  432. char *piclib2map = 0;
  433. if (piclib == 2 &&
  434. /* We might be able to avoid this
  435. call if memsz doesn't require
  436. an additional page, but this
  437. would require mmap to always
  438. return page-aligned addresses
  439. and a whole number of pages
  440. allocated. Unfortunately on
  441. uClinux may return misaligned
  442. addresses and may allocate
  443. partial pages, so we may end up
  444. doing unnecessary mmap calls.
  445. This is what we could do if we
  446. knew mmap would always return
  447. aligned pages:
  448. ((ppnt->p_vaddr + ppnt->p_filesz
  449. + ADDR_ALIGN)
  450. & PAGE_ALIGN)
  451. < ppnt->p_vaddr + ppnt->p_memsz)
  452. Instead, we have to do this: */
  453. ppnt->p_filesz < ppnt->p_memsz)
  454. {
  455. piclib2map = (char *)
  456. _dl_mmap(0, (ppnt->p_vaddr & ADDR_ALIGN)
  457. + ppnt->p_memsz,
  458. LXFLAGS(ppnt->p_flags),
  459. flags | MAP_ANONYMOUS, -1, 0);
  460. if (_dl_mmap_check_error(piclib2map))
  461. goto cant_map;
  462. DL_INIT_LOADADDR_HDR
  463. (lib_loadaddr, piclib2map
  464. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  465. }
  466. tryaddr = piclib == 2 ? piclib2map
  467. : ((char*) (piclib ? libaddr : 0) +
  468. (ppnt->p_vaddr & PAGE_ALIGN));
  469. size = (ppnt->p_vaddr & ADDR_ALIGN)
  470. + ppnt->p_filesz;
  471. /* For !MMU, mmap to fixed address will fail.
  472. So instead of desperately call mmap and fail,
  473. we set status to MAP_FAILED to save a call
  474. to mmap (). */
  475. #ifndef __ARCH_USE_MMU__
  476. if (piclib2map == 0)
  477. #endif
  478. status = (char *) _dl_mmap
  479. (tryaddr, size, LXFLAGS(ppnt->p_flags),
  480. flags | (piclib2map ? MAP_FIXED : 0),
  481. infile, ppnt->p_offset & OFFS_ALIGN);
  482. #ifndef __ARCH_USE_MMU__
  483. else
  484. status = MAP_FAILED;
  485. #endif
  486. #ifdef _DL_PREAD
  487. if (_dl_mmap_check_error(status) && piclib2map
  488. && (_DL_PREAD (infile, tryaddr, size,
  489. ppnt->p_offset & OFFS_ALIGN)
  490. == size))
  491. status = tryaddr;
  492. #endif
  493. if (_dl_mmap_check_error(status)
  494. || (tryaddr && tryaddr != status)) {
  495. cant_map:
  496. _dl_dprintf(2, "%s:%i: can't map '%s'\n",
  497. _dl_progname, __LINE__, libname);
  498. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  499. DL_LOADADDR_UNMAP (lib_loadaddr, maxvma - minvma);
  500. _dl_close(infile);
  501. _dl_munmap(header, _dl_pagesize);
  502. return NULL;
  503. }
  504. if (! piclib2map) {
  505. DL_INIT_LOADADDR_HDR
  506. (lib_loadaddr, status
  507. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  508. }
  509. /* Now we want to allocate and
  510. zero-out any data from the end of
  511. the region we mapped in from the
  512. file (filesz) to the end of the
  513. loadable segment (memsz). We may
  514. need additional pages for memsz,
  515. that we map in below, and we can
  516. count on the kernel to zero them
  517. out, but we have to zero out stuff
  518. in the last page that we mapped in
  519. from the file. However, we can't
  520. assume to have actually obtained
  521. full pages from the kernel, since
  522. we didn't ask for them, and uClibc
  523. may not give us full pages for
  524. small allocations. So only zero
  525. out up to memsz or the end of the
  526. page, whichever comes first. */
  527. /* CPNT is the beginning of the memsz
  528. portion not backed by filesz. */
  529. cpnt = (char *) (status + size);
  530. /* MAP_SIZE is the address of the
  531. beginning of the next page. */
  532. map_size = (ppnt->p_vaddr + ppnt->p_filesz
  533. + ADDR_ALIGN) & PAGE_ALIGN;
  534. #ifndef MIN
  535. # define MIN(a,b) ((a) < (b) ? (a) : (b))
  536. #endif
  537. _dl_memset (cpnt, 0,
  538. MIN (map_size
  539. - (ppnt->p_vaddr
  540. + ppnt->p_filesz),
  541. ppnt->p_memsz
  542. - ppnt->p_filesz));
  543. if (map_size < ppnt->p_vaddr + ppnt->p_memsz
  544. && !piclib2map) {
  545. tryaddr = map_size + (char*)(piclib ? libaddr : 0);
  546. status = (char *) _dl_mmap(tryaddr,
  547. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  548. LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  549. if (_dl_mmap_check_error(status)
  550. || tryaddr != status)
  551. goto cant_map;
  552. }
  553. } else {
  554. tryaddr = (piclib == 2 ? 0
  555. : (char *) (ppnt->p_vaddr & PAGE_ALIGN)
  556. + (piclib ? libaddr : 0));
  557. size = (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_filesz;
  558. status = (char *) _dl_mmap
  559. (tryaddr, size, LXFLAGS(ppnt->p_flags),
  560. flags | (piclib == 2 ? MAP_EXECUTABLE
  561. | MAP_DENYWRITE : 0),
  562. infile, ppnt->p_offset & OFFS_ALIGN);
  563. if (_dl_mmap_check_error(status)
  564. || (tryaddr && tryaddr != status))
  565. goto cant_map;
  566. DL_INIT_LOADADDR_HDR
  567. (lib_loadaddr, status
  568. + (ppnt->p_vaddr & ADDR_ALIGN), ppnt);
  569. }
  570. /* if (libaddr == 0 && piclib) {
  571. libaddr = (unsigned long) status;
  572. flags |= MAP_FIXED;
  573. } */
  574. }
  575. ppnt++;
  576. }
  577. _dl_close(infile);
  578. /* For a non-PIC library, the addresses are all absolute */
  579. if (piclib) {
  580. dynamic_addr = (unsigned long) DL_RELOC_ADDR(lib_loadaddr, dynamic_addr);
  581. }
  582. /*
  583. * OK, the ELF library is now loaded into VM in the correct locations
  584. * The next step is to go through and do the dynamic linking (if needed).
  585. */
  586. /* Start by scanning the dynamic section to get all of the pointers */
  587. if (!dynamic_addr) {
  588. _dl_internal_error_number = LD_ERROR_NODYNAMIC;
  589. _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
  590. _dl_progname, libname);
  591. _dl_munmap(header, _dl_pagesize);
  592. return NULL;
  593. }
  594. dpnt = (ElfW(Dyn) *) dynamic_addr;
  595. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  596. _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
  597. /* If the TEXTREL is set, this means that we need to make the pages
  598. writable before we perform relocations. Do this now. They get set
  599. back again later. */
  600. if (dynamic_info[DT_TEXTREL]) {
  601. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  602. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  603. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  604. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W)) {
  605. _dl_mprotect((void *) ((piclib ? libaddr : 0) +
  606. (ppnt->p_vaddr & PAGE_ALIGN)),
  607. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  608. PROT_READ | PROT_WRITE | PROT_EXEC);
  609. }
  610. }
  611. #else
  612. _dl_dprintf(_dl_debug_file, "Can't modify %s's text section."
  613. " Use GCC option -fPIC for shared objects, please.\n",
  614. libname);
  615. _dl_exit(1);
  616. #endif
  617. }
  618. tpnt = _dl_add_elf_hash_table(libname, lib_loadaddr, dynamic_info,
  619. dynamic_addr, 0);
  620. tpnt->relro_addr = relro_addr;
  621. tpnt->relro_size = relro_size;
  622. tpnt->st_dev = st.st_dev;
  623. tpnt->st_ino = st.st_ino;
  624. tpnt->ppnt = (ElfW(Phdr) *) DL_RELOC_ADDR(tpnt->loadaddr, epnt->e_phoff);
  625. tpnt->n_phent = epnt->e_phnum;
  626. /*
  627. * Add this object into the symbol chain
  628. */
  629. if (*rpnt) {
  630. (*rpnt)->next = _dl_malloc(sizeof(struct dyn_elf));
  631. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  632. (*rpnt)->next->prev = (*rpnt);
  633. *rpnt = (*rpnt)->next;
  634. }
  635. #ifndef SHARED
  636. /* When statically linked, the first time we dlopen a DSO
  637. * the *rpnt is NULL, so we need to allocate memory for it,
  638. * and initialize the _dl_symbol_table.
  639. */
  640. else {
  641. *rpnt = _dl_symbol_tables = _dl_malloc(sizeof(struct dyn_elf));
  642. _dl_memset(*rpnt, 0, sizeof(struct dyn_elf));
  643. }
  644. #endif
  645. (*rpnt)->dyn = tpnt;
  646. tpnt->symbol_scope = _dl_symbol_tables;
  647. tpnt->usage_count++;
  648. tpnt->libtype = elf_lib;
  649. /*
  650. * OK, the next thing we need to do is to insert the dynamic linker into
  651. * the proper entry in the GOT so that the PLT symbols can be properly
  652. * resolved.
  653. */
  654. lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
  655. if (lpnt) {
  656. lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
  657. INIT_GOT(lpnt, tpnt);
  658. }
  659. _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname);
  660. _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr, DL_LOADADDR_BASE(lib_loadaddr));
  661. _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
  662. DL_RELOC_ADDR(lib_loadaddr, epnt->e_entry), tpnt->ppnt, tpnt->n_phent);
  663. _dl_munmap(header, _dl_pagesize);
  664. return tpnt;
  665. }
  666. /* now_flag must be RTLD_NOW or zero */
  667. int _dl_fixup(struct dyn_elf *rpnt, int now_flag)
  668. {
  669. int goof = 0;
  670. struct elf_resolve *tpnt;
  671. ElfW(Word) reloc_size, relative_count;
  672. ElfW(Addr) reloc_addr;
  673. if (rpnt->next)
  674. goof = _dl_fixup(rpnt->next, now_flag);
  675. if (goof)
  676. return goof;
  677. tpnt = rpnt->dyn;
  678. if (!(tpnt->init_flag & RELOCS_DONE))
  679. _dl_if_debug_dprint("relocation processing: %s\n", tpnt->libname);
  680. if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
  681. _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
  682. _dl_progname, UNSUPPORTED_RELOC_STR);
  683. goof++;
  684. return goof;
  685. }
  686. reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
  687. /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
  688. range. Note that according to the ELF spec, this is completely legal! */
  689. #ifdef ELF_MACHINE_PLTREL_OVERLAP
  690. reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
  691. #endif
  692. if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
  693. !(tpnt->init_flag & RELOCS_DONE)) {
  694. reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
  695. relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
  696. if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
  697. reloc_size -= relative_count * sizeof(ELF_RELOC);
  698. elf_machine_relative(tpnt->loadaddr, reloc_addr, relative_count);
  699. reloc_addr += relative_count * sizeof(ELF_RELOC);
  700. }
  701. goof += _dl_parse_relocation_information(rpnt,
  702. reloc_addr,
  703. reloc_size);
  704. tpnt->init_flag |= RELOCS_DONE;
  705. }
  706. if (tpnt->dynamic_info[DT_BIND_NOW])
  707. now_flag = RTLD_NOW;
  708. if (tpnt->dynamic_info[DT_JMPREL] &&
  709. (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
  710. (now_flag && !(tpnt->rtld_flags & now_flag)))) {
  711. tpnt->rtld_flags |= now_flag;
  712. if (!(tpnt->rtld_flags & RTLD_NOW)) {
  713. _dl_parse_lazy_relocation_information(rpnt,
  714. tpnt->dynamic_info[DT_JMPREL],
  715. tpnt->dynamic_info [DT_PLTRELSZ]);
  716. } else {
  717. goof += _dl_parse_relocation_information(rpnt,
  718. tpnt->dynamic_info[DT_JMPREL],
  719. tpnt->dynamic_info[DT_PLTRELSZ]);
  720. }
  721. tpnt->init_flag |= JMP_RELOCS_DONE;
  722. }
  723. return goof;
  724. }
  725. /* Minimal printf which handles only %s, %d, and %x */
  726. void _dl_dprintf(int fd, const char *fmt, ...)
  727. {
  728. #if __WORDSIZE > 32
  729. long int num;
  730. #else
  731. int num;
  732. #endif
  733. va_list args;
  734. char *start, *ptr, *string;
  735. static char *buf;
  736. if (!fmt)
  737. return;
  738. buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  739. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  740. if (_dl_mmap_check_error(buf)) {
  741. _dl_write(fd, "mmap of a spare page failed!\n", 29);
  742. _dl_exit(20);
  743. }
  744. start = ptr = buf;
  745. if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
  746. _dl_write(fd, "overflow\n", 11);
  747. _dl_exit(20);
  748. }
  749. _dl_strcpy(buf, fmt);
  750. va_start(args, fmt);
  751. while (start) {
  752. while (*ptr != '%' && *ptr) {
  753. ptr++;
  754. }
  755. if (*ptr == '%') {
  756. *ptr++ = '\0';
  757. _dl_write(fd, start, _dl_strlen(start));
  758. switch (*ptr++) {
  759. case 's':
  760. string = va_arg(args, char *);
  761. if (!string)
  762. _dl_write(fd, "(null)", 6);
  763. else
  764. _dl_write(fd, string, _dl_strlen(string));
  765. break;
  766. case 'i':
  767. case 'd':
  768. {
  769. char tmp[22];
  770. #if __WORDSIZE > 32
  771. num = va_arg(args, long int);
  772. #else
  773. num = va_arg(args, int);
  774. #endif
  775. string = _dl_simple_ltoa(tmp, num);
  776. _dl_write(fd, string, _dl_strlen(string));
  777. break;
  778. }
  779. case 'x':
  780. case 'X':
  781. {
  782. char tmp[22];
  783. #if __WORDSIZE > 32
  784. num = va_arg(args, long int);
  785. #else
  786. num = va_arg(args, int);
  787. #endif
  788. string = _dl_simple_ltoahex(tmp, num);
  789. _dl_write(fd, string, _dl_strlen(string));
  790. break;
  791. }
  792. default:
  793. _dl_write(fd, "(null)", 6);
  794. break;
  795. }
  796. start = ptr;
  797. } else {
  798. _dl_write(fd, start, _dl_strlen(start));
  799. start = NULL;
  800. }
  801. }
  802. _dl_munmap(buf, _dl_pagesize);
  803. return;
  804. }
  805. char *_dl_strdup(const char *string)
  806. {
  807. char *retval;
  808. int len;
  809. len = _dl_strlen(string);
  810. retval = _dl_malloc(len + 1);
  811. _dl_strcpy(retval, string);
  812. return retval;
  813. }
  814. void _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[],
  815. void *debug_addr, DL_LOADADDR_TYPE load_off)
  816. {
  817. __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
  818. }