dl-elf.c 31 KB

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