dl-elf.c 26 KB

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