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. /* 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. * Make a writeable mapping of a segment, regardless of whether PF_W is
  277. * set or not.
  278. */
  279. static void *
  280. map_writeable (int infile, ElfW(Phdr) *ppnt, int piclib, int flags,
  281. unsigned long libaddr)
  282. {
  283. int prot_flags = ppnt->p_flags | PF_W;
  284. char *status, *retval;
  285. char *tryaddr;
  286. ssize_t size;
  287. unsigned long map_size;
  288. char *cpnt;
  289. char *piclib2map = NULL;
  290. if (piclib == 2 &&
  291. /* We might be able to avoid this call if memsz doesn't
  292. require an additional page, but this would require mmap
  293. to always return page-aligned addresses and a whole
  294. number of pages allocated. Unfortunately on uClinux
  295. may return misaligned addresses and may allocate
  296. partial pages, so we may end up doing unnecessary mmap
  297. calls.
  298. This is what we could do if we knew mmap would always
  299. return aligned pages:
  300. ((ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) &
  301. PAGE_ALIGN) < ppnt->p_vaddr + ppnt->p_memsz)
  302. Instead, we have to do this: */
  303. ppnt->p_filesz < ppnt->p_memsz)
  304. {
  305. piclib2map = (char *)
  306. _dl_mmap(0, (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_memsz,
  307. LXFLAGS(prot_flags), flags | MAP_ANONYMOUS, -1, 0);
  308. if (_dl_mmap_check_error(piclib2map))
  309. return 0;
  310. }
  311. tryaddr = piclib == 2 ? piclib2map
  312. : ((char*) (piclib ? libaddr : 0) +
  313. (ppnt->p_vaddr & PAGE_ALIGN));
  314. size = (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_filesz;
  315. /* For !MMU, mmap to fixed address will fail.
  316. So instead of desperately call mmap and fail,
  317. we set status to MAP_FAILED to save a call
  318. to mmap (). */
  319. #ifndef __ARCH_USE_MMU__
  320. if (piclib2map == 0)
  321. #endif
  322. status = (char *) _dl_mmap
  323. (tryaddr, size, LXFLAGS(prot_flags),
  324. flags | (piclib2map ? MAP_FIXED : 0),
  325. infile, ppnt->p_offset & OFFS_ALIGN);
  326. #ifndef __ARCH_USE_MMU__
  327. else
  328. status = MAP_FAILED;
  329. #endif
  330. #ifdef _DL_PREAD
  331. if (_dl_mmap_check_error(status) && piclib2map
  332. && (_DL_PREAD (infile, tryaddr, size,
  333. ppnt->p_offset & OFFS_ALIGN) == size))
  334. status = tryaddr;
  335. #endif
  336. if (_dl_mmap_check_error(status) || (tryaddr && tryaddr != status))
  337. return 0;
  338. if (piclib2map)
  339. retval = piclib2map;
  340. else
  341. retval = status;
  342. /* Now we want to allocate and zero-out any data from the end
  343. of the region we mapped in from the file (filesz) to the
  344. end of the loadable segment (memsz). We may need
  345. additional pages for memsz, that we map in below, and we
  346. can count on the kernel to zero them out, but we have to
  347. zero out stuff in the last page that we mapped in from the
  348. file. However, we can't assume to have actually obtained
  349. full pages from the kernel, since we didn't ask for them,
  350. and uClibc may not give us full pages for small
  351. allocations. So only zero out up to memsz or the end of
  352. the page, whichever comes first. */
  353. /* CPNT is the beginning of the memsz portion not backed by
  354. filesz. */
  355. cpnt = (char *) (status + size);
  356. /* MAP_SIZE is the address of the
  357. beginning of the next page. */
  358. map_size = (ppnt->p_vaddr + ppnt->p_filesz
  359. + ADDR_ALIGN) & PAGE_ALIGN;
  360. _dl_memset (cpnt, 0,
  361. MIN (map_size
  362. - (ppnt->p_vaddr
  363. + ppnt->p_filesz),
  364. ppnt->p_memsz
  365. - ppnt->p_filesz));
  366. if (map_size < ppnt->p_vaddr + ppnt->p_memsz && !piclib2map) {
  367. tryaddr = map_size + (char*)(piclib ? libaddr : 0);
  368. status = (char *) _dl_mmap(tryaddr,
  369. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  370. LXFLAGS(prot_flags),
  371. flags | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  372. if (_dl_mmap_check_error(status) || tryaddr != status)
  373. return NULL;
  374. }
  375. return retval;
  376. }
  377. /*
  378. * Read one ELF library into memory, mmap it into the correct locations and
  379. * add the symbol info to the symbol chain. Perform any relocations that
  380. * are required.
  381. */
  382. struct elf_resolve *_dl_load_elf_shared_library(int secure,
  383. struct dyn_elf **rpnt, const char *libname)
  384. {
  385. ElfW(Ehdr) *epnt;
  386. unsigned long dynamic_addr = 0;
  387. ElfW(Dyn) *dpnt;
  388. struct elf_resolve *tpnt;
  389. ElfW(Phdr) *ppnt;
  390. #if defined(USE_TLS) && USE_TLS
  391. ElfW(Phdr) *tlsppnt = NULL;
  392. #endif
  393. char *status, *header;
  394. unsigned long dynamic_info[DYNAMIC_SIZE];
  395. unsigned long *lpnt;
  396. unsigned long libaddr;
  397. unsigned long minvma = 0xffffffff, maxvma = 0;
  398. unsigned int rtld_flags;
  399. int i, flags, piclib, infile;
  400. ElfW(Addr) relro_addr = 0;
  401. size_t relro_size = 0;
  402. struct stat st;
  403. uint32_t *p32;
  404. DL_LOADADDR_TYPE lib_loadaddr = 0;
  405. DL_INIT_LOADADDR_EXTRA_DECLS
  406. libaddr = 0;
  407. infile = _dl_open(libname, O_RDONLY, 0);
  408. if (infile < 0) {
  409. _dl_internal_error_number = LD_ERROR_NOFILE;
  410. return NULL;
  411. }
  412. if (_dl_fstat(infile, &st) < 0) {
  413. _dl_internal_error_number = LD_ERROR_NOFILE;
  414. _dl_close(infile);
  415. return NULL;
  416. }
  417. /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
  418. we don't load the library if it isn't setuid. */
  419. if (secure) {
  420. if (!(st.st_mode & S_ISUID)) {
  421. _dl_close(infile);
  422. return NULL;
  423. }
  424. }
  425. /* Check if file is already loaded */
  426. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  427. if (tpnt->st_dev == st.st_dev && tpnt->st_ino == st.st_ino) {
  428. /* Already loaded */
  429. tpnt->usage_count++;
  430. _dl_close(infile);
  431. return tpnt;
  432. }
  433. }
  434. header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  435. MAP_PRIVATE | MAP_ANONYMOUS | MAP_UNINITIALIZE, -1, 0);
  436. if (_dl_mmap_check_error(header)) {
  437. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  438. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  439. _dl_close(infile);
  440. return NULL;
  441. }
  442. _dl_read(infile, header, _dl_pagesize);
  443. epnt = (ElfW(Ehdr) *) (intptr_t) header;
  444. p32 = (uint32_t*)&epnt->e_ident;
  445. if (*p32 != ELFMAG_U32) {
  446. _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  447. libname);
  448. _dl_internal_error_number = LD_ERROR_NOTELF;
  449. _dl_close(infile);
  450. _dl_munmap(header, _dl_pagesize);
  451. return NULL;
  452. }
  453. if ((epnt->e_type != ET_DYN
  454. #ifdef __LDSO_STANDALONE_SUPPORT__
  455. && epnt->e_type != ET_EXEC
  456. #endif
  457. ) || (epnt->e_machine != MAGIC1
  458. #ifdef MAGIC2
  459. && epnt->e_machine != MAGIC2
  460. #endif
  461. ))
  462. {
  463. _dl_internal_error_number =
  464. (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
  465. _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  466. "\n", _dl_progname, libname);
  467. _dl_close(infile);
  468. _dl_munmap(header, _dl_pagesize);
  469. return NULL;
  470. }
  471. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  472. piclib = 1;
  473. for (i = 0; i < epnt->e_phnum; i++) {
  474. if (ppnt->p_type == PT_DYNAMIC) {
  475. if (dynamic_addr)
  476. _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
  477. _dl_progname, libname);
  478. dynamic_addr = ppnt->p_vaddr;
  479. }
  480. if (ppnt->p_type == PT_LOAD) {
  481. /* See if this is a PIC library. */
  482. if (minvma == 0xffffffff && ppnt->p_vaddr > 0x1000000) {
  483. piclib = 0;
  484. minvma = ppnt->p_vaddr;
  485. }
  486. if (piclib && ppnt->p_vaddr < minvma) {
  487. minvma = ppnt->p_vaddr;
  488. }
  489. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  490. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  491. }
  492. }
  493. if (ppnt->p_type == PT_TLS) {
  494. #if defined(USE_TLS) && USE_TLS
  495. if (ppnt->p_memsz == 0)
  496. /* Nothing to do for an empty segment. */
  497. continue;
  498. else
  499. /* Save for after 'tpnt' is actually allocated. */
  500. tlsppnt = ppnt;
  501. #else
  502. /*
  503. * Yup, the user was an idiot and tried to sneak in a library with
  504. * TLS in it and we don't support it. Let's fall on our own sword
  505. * and scream at the luser while we die.
  506. */
  507. _dl_dprintf(2, "%s: '%s' library contains unsupported TLS\n",
  508. _dl_progname, libname);
  509. _dl_internal_error_number = LD_ERROR_TLS_FAILED;
  510. _dl_close(infile);
  511. _dl_munmap(header, _dl_pagesize);
  512. return NULL;
  513. #endif
  514. }
  515. ppnt++;
  516. }
  517. #ifdef __LDSO_STANDALONE_SUPPORT__
  518. if (epnt->e_type == ET_EXEC)
  519. piclib = 0;
  520. #endif
  521. DL_CHECK_LIB_TYPE (epnt, piclib, _dl_progname, libname);
  522. maxvma = (maxvma + ADDR_ALIGN) & PAGE_ALIGN;
  523. minvma = minvma & ~ADDR_ALIGN;
  524. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  525. if (piclib == 0 || piclib == 1) {
  526. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  527. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  528. if (_dl_mmap_check_error(status)) {
  529. cant_map:
  530. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  531. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  532. _dl_close(infile);
  533. _dl_munmap(header, _dl_pagesize);
  534. return NULL;
  535. }
  536. libaddr = (unsigned long) status;
  537. flags |= MAP_FIXED;
  538. }
  539. /* Get the memory to store the library */
  540. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  541. DL_INIT_LOADADDR(lib_loadaddr, libaddr - minvma, ppnt, epnt->e_phnum);
  542. for (i = 0; i < epnt->e_phnum; i++) {
  543. if (DL_IS_SPECIAL_SEGMENT (epnt, ppnt)) {
  544. char *addr;
  545. addr = DL_MAP_SEGMENT (epnt, ppnt, infile, flags);
  546. if (addr == NULL) {
  547. cant_map1:
  548. DL_LOADADDR_UNMAP (lib_loadaddr, maxvma - minvma);
  549. goto cant_map;
  550. }
  551. DL_INIT_LOADADDR_HDR (lib_loadaddr, addr, ppnt);
  552. ppnt++;
  553. continue;
  554. }
  555. if (ppnt->p_type == PT_GNU_RELRO) {
  556. relro_addr = ppnt->p_vaddr;
  557. relro_size = ppnt->p_memsz;
  558. }
  559. if (ppnt->p_type == PT_LOAD) {
  560. char *tryaddr;
  561. ssize_t size;
  562. if (ppnt->p_flags & PF_W) {
  563. status = map_writeable (infile, ppnt, piclib, flags, libaddr);
  564. if (status == NULL)
  565. goto cant_map1;
  566. } else {
  567. tryaddr = (piclib == 2 ? 0
  568. : (char *) (ppnt->p_vaddr & PAGE_ALIGN)
  569. + (piclib ? libaddr : lib_loadaddr));
  570. size = (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_filesz;
  571. status = (char *) _dl_mmap
  572. (tryaddr, size, LXFLAGS(ppnt->p_flags),
  573. flags | (piclib == 2 ? MAP_EXECUTABLE
  574. | MAP_DENYWRITE : 0),
  575. infile, ppnt->p_offset & OFFS_ALIGN);
  576. if (_dl_mmap_check_error(status)
  577. || (tryaddr && tryaddr != status))
  578. goto cant_map1;
  579. }
  580. DL_INIT_LOADADDR_HDR(lib_loadaddr,
  581. status + (ppnt->p_vaddr & ADDR_ALIGN),
  582. ppnt);
  583. /* if (libaddr == 0 && piclib) {
  584. libaddr = (unsigned long) status;
  585. flags |= MAP_FIXED;
  586. } */
  587. }
  588. ppnt++;
  589. }
  590. /*
  591. * The dynamic_addr must be take into acount lib_loadaddr value, to note
  592. * it is zero when the SO has been mapped to the elf's physical addr
  593. */
  594. if (lib_loadaddr) {
  595. dynamic_addr = (unsigned long) DL_RELOC_ADDR(lib_loadaddr, dynamic_addr);
  596. }
  597. /*
  598. * OK, the ELF library is now loaded into VM in the correct locations
  599. * The next step is to go through and do the dynamic linking (if needed).
  600. */
  601. /* Start by scanning the dynamic section to get all of the pointers */
  602. if (!dynamic_addr) {
  603. _dl_internal_error_number = LD_ERROR_NODYNAMIC;
  604. _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
  605. _dl_progname, libname);
  606. _dl_munmap(header, _dl_pagesize);
  607. _dl_close(infile);
  608. return NULL;
  609. }
  610. dpnt = (ElfW(Dyn) *) dynamic_addr;
  611. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  612. rtld_flags = _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
  613. /* If the TEXTREL is set, this means that we need to make the pages
  614. writable before we perform relocations. Do this now. They get set
  615. back again later. */
  616. if (dynamic_info[DT_TEXTREL]) {
  617. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  618. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  619. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  620. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W)) {
  621. #ifdef __ARCH_USE_MMU__
  622. _dl_mprotect((void *) ((piclib ? libaddr : lib_loadaddr) +
  623. (ppnt->p_vaddr & PAGE_ALIGN)),
  624. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  625. PROT_READ | PROT_WRITE | PROT_EXEC);
  626. #else
  627. void *new_addr;
  628. new_addr = map_writeable (infile, ppnt, piclib, flags, libaddr);
  629. if (!new_addr) {
  630. _dl_dprintf(_dl_debug_file, "Can't modify %s's text section.",
  631. libname);
  632. _dl_exit(1);
  633. }
  634. DL_UPDATE_LOADADDR_HDR(lib_loadaddr,
  635. new_addr + (ppnt->p_vaddr & ADDR_ALIGN),
  636. ppnt);
  637. /* This has invalidated all pointers into the previously readonly segment.
  638. Update any them to point into the remapped segment. */
  639. _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
  640. #endif
  641. }
  642. }
  643. #else
  644. _dl_dprintf(_dl_debug_file, "Can't modify %s's text section."
  645. " Use GCC option -fPIC for shared objects, please.\n",
  646. libname);
  647. _dl_exit(1);
  648. #endif
  649. }
  650. _dl_close(infile);
  651. tpnt = _dl_add_elf_hash_table(libname, lib_loadaddr, dynamic_info,
  652. dynamic_addr, 0);
  653. tpnt->mapaddr = libaddr;
  654. tpnt->relro_addr = relro_addr;
  655. tpnt->relro_size = relro_size;
  656. tpnt->st_dev = st.st_dev;
  657. tpnt->st_ino = st.st_ino;
  658. tpnt->ppnt = (ElfW(Phdr) *) DL_RELOC_ADDR(tpnt->mapaddr, epnt->e_phoff);
  659. tpnt->n_phent = epnt->e_phnum;
  660. tpnt->rtld_flags |= rtld_flags;
  661. #ifdef __LDSO_STANDALONE_SUPPORT__
  662. tpnt->l_entry = epnt->e_entry;
  663. #endif
  664. #if defined(USE_TLS) && USE_TLS
  665. if (tlsppnt) {
  666. _dl_debug_early("Found TLS header for %s\n", libname);
  667. # if NO_TLS_OFFSET != 0
  668. tpnt->l_tls_offset = NO_TLS_OFFSET;
  669. # endif
  670. tpnt->l_tls_blocksize = tlsppnt->p_memsz;
  671. tpnt->l_tls_align = tlsppnt->p_align;
  672. if (tlsppnt->p_align == 0)
  673. tpnt->l_tls_firstbyte_offset = 0;
  674. else
  675. tpnt->l_tls_firstbyte_offset = tlsppnt->p_vaddr &
  676. (tlsppnt->p_align - 1);
  677. tpnt->l_tls_initimage_size = tlsppnt->p_filesz;
  678. tpnt->l_tls_initimage = (void *) tlsppnt->p_vaddr;
  679. /* Assign the next available module ID. */
  680. tpnt->l_tls_modid = _dl_next_tls_modid ();
  681. /* We know the load address, so add it to the offset. */
  682. #ifdef __LDSO_STANDALONE_SUPPORT__
  683. if ((tpnt->l_tls_initimage != NULL) && piclib)
  684. #else
  685. if (tpnt->l_tls_initimage != NULL)
  686. #endif
  687. {
  688. # ifdef __SUPPORT_LD_DEBUG_EARLY__
  689. unsigned int tmp = (unsigned int) tpnt->l_tls_initimage;
  690. tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
  691. _dl_debug_early("Relocated TLS initial image from %x to %x (size = %x)\n", tmp, tpnt->l_tls_initimage, tpnt->l_tls_initimage_size);
  692. tmp = 0;
  693. # else
  694. tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
  695. # endif
  696. }
  697. }
  698. #endif
  699. /*
  700. * Add this object into the symbol chain
  701. */
  702. if (*rpnt
  703. #ifdef __LDSO_STANDALONE_SUPPORT__
  704. /* Do not create a new chain entry for the main executable */
  705. && (*rpnt)->dyn
  706. #endif
  707. ) {
  708. (*rpnt)->next = _dl_malloc(sizeof(struct dyn_elf));
  709. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  710. (*rpnt)->next->prev = (*rpnt);
  711. *rpnt = (*rpnt)->next;
  712. }
  713. #ifndef SHARED
  714. /* When statically linked, the first time we dlopen a DSO
  715. * the *rpnt is NULL, so we need to allocate memory for it,
  716. * and initialize the _dl_symbol_table.
  717. */
  718. else {
  719. *rpnt = _dl_symbol_tables = _dl_malloc(sizeof(struct dyn_elf));
  720. _dl_memset(*rpnt, 0, sizeof(struct dyn_elf));
  721. }
  722. #endif
  723. (*rpnt)->dyn = tpnt;
  724. tpnt->usage_count++;
  725. #ifdef __LDSO_STANDALONE_SUPPORT__
  726. tpnt->libtype = (epnt->e_type == ET_DYN) ? elf_lib : elf_executable;
  727. #else
  728. tpnt->libtype = elf_lib;
  729. #endif
  730. /*
  731. * OK, the next thing we need to do is to insert the dynamic linker into
  732. * the proper entry in the GOT so that the PLT symbols can be properly
  733. * resolved.
  734. */
  735. lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
  736. if (lpnt) {
  737. lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
  738. INIT_GOT(lpnt, tpnt);
  739. }
  740. #ifdef __DSBT__
  741. /* Handle DSBT initialization */
  742. {
  743. struct elf_resolve *t, *ref;
  744. int idx = tpnt->loadaddr.map->dsbt_index;
  745. unsigned *dsbt = tpnt->loadaddr.map->dsbt_table;
  746. if (idx == 0) {
  747. if (!dynamic_info[DT_TEXTREL]) {
  748. /* This DSO has not been assigned an index. */
  749. _dl_dprintf(2, "%s: '%s' is missing a dsbt index assignment!\n",
  750. _dl_progname, libname);
  751. _dl_exit(1);
  752. }
  753. /* Find a dsbt table from another module. */
  754. ref = NULL;
  755. for (t = _dl_loaded_modules; t; t = t->next) {
  756. if (ref == NULL && t != tpnt) {
  757. ref = t;
  758. break;
  759. }
  760. }
  761. idx = tpnt->loadaddr.map->dsbt_size;
  762. while (idx-- > 0)
  763. if (!ref || ref->loadaddr.map->dsbt_table[idx] == NULL)
  764. break;
  765. if (idx <= 0) {
  766. _dl_dprintf(2, "%s: '%s' caused DSBT table overflow!\n",
  767. _dl_progname, libname);
  768. _dl_exit(1);
  769. }
  770. _dl_if_debug_dprint("\n\tfile='%s'; assigned index %d\n",
  771. libname, idx);
  772. tpnt->loadaddr.map->dsbt_index = idx;
  773. }
  774. /*
  775. * Setup dsbt slot for this module in dsbt of all modules.
  776. */
  777. ref = NULL;
  778. for (t = _dl_loaded_modules; t; t = t->next) {
  779. /* find a dsbt table from another module */
  780. if (ref == NULL && t != tpnt) {
  781. ref = t;
  782. /* make sure index is not already used */
  783. if (t->loadaddr.map->dsbt_table[idx]) {
  784. struct elf_resolve *dup;
  785. char *dup_name;
  786. for (dup = _dl_loaded_modules; dup; dup = dup->next)
  787. if (dup != tpnt && dup->loadaddr.map->dsbt_index == idx)
  788. break;
  789. if (dup)
  790. dup_name = dup->libname;
  791. else if (idx == 1)
  792. dup_name = "runtime linker";
  793. else
  794. dup_name = "unknown library";
  795. _dl_dprintf(2, "%s: '%s' dsbt index %d already used by %s!\n",
  796. _dl_progname, libname, idx, dup_name);
  797. _dl_exit(1);
  798. }
  799. }
  800. t->loadaddr.map->dsbt_table[idx] = (unsigned)dsbt;
  801. }
  802. if (ref)
  803. _dl_memcpy(dsbt, ref->loadaddr.map->dsbt_table,
  804. tpnt->loadaddr.map->dsbt_size * sizeof(unsigned *));
  805. }
  806. #endif
  807. _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname);
  808. _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr, DL_LOADADDR_BASE(lib_loadaddr));
  809. _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
  810. DL_RELOC_ADDR(lib_loadaddr, epnt->e_entry), tpnt->ppnt, tpnt->n_phent);
  811. _dl_munmap(header, _dl_pagesize);
  812. return tpnt;
  813. }
  814. /* now_flag must be RTLD_NOW or zero */
  815. int _dl_fixup(struct dyn_elf *rpnt, struct r_scope_elem *scope, int now_flag)
  816. {
  817. int goof = 0;
  818. struct elf_resolve *tpnt;
  819. ElfW(Word) reloc_size, relative_count;
  820. ElfW(Addr) reloc_addr;
  821. if (rpnt->next)
  822. goof = _dl_fixup(rpnt->next, scope, now_flag);
  823. if (goof)
  824. return goof;
  825. tpnt = rpnt->dyn;
  826. if (!(tpnt->init_flag & RELOCS_DONE))
  827. _dl_if_debug_dprint("relocation processing: %s\n", tpnt->libname);
  828. if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
  829. _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
  830. _dl_progname, UNSUPPORTED_RELOC_STR);
  831. goof++;
  832. return goof;
  833. }
  834. reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
  835. /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
  836. range. Note that according to the ELF spec, this is completely legal! */
  837. #ifdef ELF_MACHINE_PLTREL_OVERLAP
  838. reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
  839. #endif
  840. if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
  841. !(tpnt->init_flag & RELOCS_DONE)) {
  842. reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
  843. relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
  844. if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
  845. reloc_size -= relative_count * sizeof(ELF_RELOC);
  846. if (tpnt->loadaddr
  847. #ifdef __LDSO_PRELINK_SUPPORT__
  848. || (!tpnt->dynamic_info[DT_GNU_PRELINKED_IDX])
  849. #endif
  850. )
  851. elf_machine_relative(tpnt->loadaddr, reloc_addr, relative_count);
  852. reloc_addr += relative_count * sizeof(ELF_RELOC);
  853. }
  854. goof += _dl_parse_relocation_information(rpnt, scope,
  855. reloc_addr,
  856. reloc_size);
  857. tpnt->init_flag |= RELOCS_DONE;
  858. }
  859. if (tpnt->dynamic_info[DT_BIND_NOW])
  860. now_flag = RTLD_NOW;
  861. if (tpnt->dynamic_info[DT_JMPREL] &&
  862. (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
  863. (now_flag && !(tpnt->rtld_flags & now_flag)))) {
  864. tpnt->rtld_flags |= now_flag;
  865. if (!(tpnt->rtld_flags & RTLD_NOW)) {
  866. _dl_parse_lazy_relocation_information(rpnt,
  867. tpnt->dynamic_info[DT_JMPREL],
  868. tpnt->dynamic_info [DT_PLTRELSZ]);
  869. } else {
  870. goof += _dl_parse_relocation_information(rpnt, scope,
  871. tpnt->dynamic_info[DT_JMPREL],
  872. tpnt->dynamic_info[DT_PLTRELSZ]);
  873. }
  874. tpnt->init_flag |= JMP_RELOCS_DONE;
  875. }
  876. #if 0
  877. /* _dl_add_to_slotinfo is called by init_tls() for initial DSO
  878. or by dlopen() for dynamically loaded DSO. */
  879. #if defined(USE_TLS) && USE_TLS
  880. /* Add object to slot information data if necessasy. */
  881. if (tpnt->l_tls_blocksize != 0 && tls_init_tp_called)
  882. _dl_add_to_slotinfo ((struct link_map *) tpnt);
  883. #endif
  884. #endif
  885. return goof;
  886. }
  887. /* Minimal printf which handles only %s, %d, and %x */
  888. void _dl_dprintf(int fd, const char *fmt, ...)
  889. {
  890. #if __WORDSIZE > 32
  891. long int num;
  892. #else
  893. int num;
  894. #endif
  895. va_list args;
  896. char *start, *ptr, *string;
  897. char *buf;
  898. if (!fmt)
  899. return;
  900. buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  901. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  902. if (_dl_mmap_check_error(buf)) {
  903. _dl_write(fd, "mmap of a spare page failed!\n", 29);
  904. _dl_exit(20);
  905. }
  906. start = ptr = buf;
  907. if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
  908. _dl_write(fd, "overflow\n", 11);
  909. _dl_exit(20);
  910. }
  911. _dl_strcpy(buf, fmt);
  912. va_start(args, fmt);
  913. while (start) {
  914. while (*ptr != '%' && *ptr) {
  915. ptr++;
  916. }
  917. if (*ptr == '%') {
  918. *ptr++ = '\0';
  919. _dl_write(fd, start, _dl_strlen(start));
  920. switch (*ptr++) {
  921. case 's':
  922. string = va_arg(args, char *);
  923. if (!string)
  924. _dl_write(fd, "(null)", 6);
  925. else
  926. _dl_write(fd, string, _dl_strlen(string));
  927. break;
  928. case 'i':
  929. case 'd':
  930. {
  931. char tmp[22];
  932. #if __WORDSIZE > 32
  933. num = va_arg(args, long int);
  934. #else
  935. num = va_arg(args, int);
  936. #endif
  937. string = _dl_simple_ltoa(tmp, num);
  938. _dl_write(fd, string, _dl_strlen(string));
  939. break;
  940. }
  941. case 'x':
  942. case 'X':
  943. {
  944. char tmp[22];
  945. #if __WORDSIZE > 32
  946. num = va_arg(args, long int);
  947. #else
  948. num = va_arg(args, int);
  949. #endif
  950. string = _dl_simple_ltoahex(tmp, num);
  951. _dl_write(fd, string, _dl_strlen(string));
  952. break;
  953. }
  954. default:
  955. _dl_write(fd, "(null)", 6);
  956. break;
  957. }
  958. start = ptr;
  959. } else {
  960. _dl_write(fd, start, _dl_strlen(start));
  961. start = NULL;
  962. }
  963. }
  964. _dl_munmap(buf, _dl_pagesize);
  965. return;
  966. }
  967. char *_dl_strdup(const char *string)
  968. {
  969. char *retval;
  970. int len;
  971. len = _dl_strlen(string);
  972. retval = _dl_malloc(len + 1);
  973. _dl_strcpy(retval, string);
  974. return retval;
  975. }
  976. unsigned int _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[],
  977. void *debug_addr, DL_LOADADDR_TYPE load_off)
  978. {
  979. return __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
  980. }