dl-elf.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171
  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, unsigned int rflags, const char *path_list,
  116. struct dyn_elf **rpnt, const char* origin)
  117. {
  118. char *mylibname;
  119. struct elf_resolve *tpnt;
  120. const char *p, *pn;
  121. int plen;
  122. if (path_list==NULL)
  123. return NULL;
  124. /* another bit of local storage */
  125. mylibname = alloca(2050);
  126. /* Unlike ldd.c, don't bother to eliminate double //s */
  127. /* Replace colons with zeros in path_list */
  128. /* : at the beginning or end of path maps to CWD */
  129. /* :: anywhere maps CWD */
  130. /* "" maps to CWD */
  131. for (p = path_list; p != NULL; p = pn) {
  132. pn = _dl_strchr(p + 1, ':');
  133. if (pn != NULL) {
  134. plen = pn - p;
  135. pn++;
  136. } else
  137. plen = _dl_strlen(p);
  138. if (plen >= 7 && _dl_memcmp(p, "$ORIGIN", 7) == 0) {
  139. int olen;
  140. /* $ORIGIN is not expanded for SUID/GUID programs
  141. (except if it is $ORIGIN alone) */
  142. if ((rflags & __RTLD_SECURE) && plen != 7)
  143. continue;
  144. if (origin == NULL)
  145. continue;
  146. for (olen = _dl_strlen(origin) - 1; olen >= 0 && origin[olen] != '/'; olen--)
  147. ;
  148. if (olen <= 0)
  149. continue;
  150. _dl_memcpy(&mylibname[0], origin, olen);
  151. _dl_memcpy(&mylibname[olen], p + 7, plen - 7);
  152. mylibname[olen + plen - 7] = 0;
  153. } else if (plen != 0) {
  154. _dl_memcpy(mylibname, p, plen);
  155. mylibname[plen] = 0;
  156. } else {
  157. _dl_strcpy(mylibname, ".");
  158. }
  159. _dl_strcat(mylibname, "/");
  160. _dl_strcat(mylibname, name);
  161. #ifdef __LDSO_SAFE_RUNPATH__
  162. if (*mylibname == '/')
  163. #endif
  164. if ((tpnt = _dl_load_elf_shared_library(rflags, rpnt, mylibname)) != NULL)
  165. return tpnt;
  166. }
  167. return NULL;
  168. }
  169. /* Used to return error codes back to dlopen et. al. */
  170. unsigned long _dl_error_number;
  171. unsigned long _dl_internal_error_number;
  172. struct elf_resolve *_dl_load_shared_library(unsigned int rflags, struct dyn_elf **rpnt,
  173. struct elf_resolve *tpnt, char *full_libname, int attribute_unused trace_loaded_objects)
  174. {
  175. char *pnt;
  176. struct elf_resolve *tpnt1;
  177. char *libname;
  178. _dl_internal_error_number = 0;
  179. libname = full_libname;
  180. /* quick hack to ensure mylibname buffer doesn't overflow. don't
  181. allow full_libname or any directory to be longer than 1024. */
  182. if (_dl_strlen(full_libname) > 1024)
  183. goto goof;
  184. /* Skip over any initial initial './' and '/' stuff to
  185. * get the short form libname with no path garbage */
  186. pnt = _dl_strrchr(libname, '/');
  187. if (pnt) {
  188. libname = pnt + 1;
  189. }
  190. _dl_if_debug_dprint("\tfind library='%s'; searching\n", libname);
  191. /* If the filename has any '/', try it straight and leave it at that.
  192. For IBCS2 compatibility under linux, we substitute the string
  193. /usr/i486-sysv4/lib for /usr/lib in library names. */
  194. if (libname != full_libname) {
  195. _dl_if_debug_dprint("\ttrying file='%s'\n", full_libname);
  196. tpnt1 = _dl_load_elf_shared_library(rflags, rpnt, full_libname);
  197. if (tpnt1) {
  198. return tpnt1;
  199. }
  200. }
  201. /*
  202. * The ABI specifies that RPATH is searched before LD_LIBRARY_PATH or
  203. * the default path of /usr/lib. Check in rpath directories.
  204. */
  205. #ifdef __LDSO_RUNPATH__
  206. pnt = (tpnt ? (char *) tpnt->dynamic_info[DT_RPATH] : NULL);
  207. if (pnt) {
  208. pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
  209. _dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt);
  210. if ((tpnt1 = search_for_named_library(libname, rflags, pnt, rpnt,
  211. tpnt->libname)) != NULL)
  212. return tpnt1;
  213. }
  214. #endif
  215. #ifdef __LDSO_LD_LIBRARY_PATH__
  216. /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
  217. if (_dl_library_path) {
  218. _dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
  219. if ((tpnt1 = search_for_named_library(libname, rflags, _dl_library_path, rpnt, NULL)) != NULL)
  220. {
  221. return tpnt1;
  222. }
  223. }
  224. #endif
  225. /*
  226. * The ABI specifies that RUNPATH is searched after LD_LIBRARY_PATH.
  227. */
  228. #ifdef __LDSO_RUNPATH__
  229. pnt = (tpnt ? (char *)tpnt->dynamic_info[DT_RUNPATH] : NULL);
  230. if (pnt) {
  231. pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
  232. _dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt);
  233. if ((tpnt1 = search_for_named_library(libname, rflags, pnt, rpnt, NULL)) != NULL)
  234. return tpnt1;
  235. }
  236. #ifdef __LDSO_RUNPATH_OF_EXECUTABLE__
  237. /*
  238. * Try the DT_RPATH of the executable itself.
  239. */
  240. pnt = (char *) _dl_loaded_modules->dynamic_info[DT_RPATH];
  241. if (pnt) {
  242. pnt += (unsigned long) _dl_loaded_modules->dynamic_info[DT_STRTAB];
  243. _dl_if_debug_dprint("\tsearching exe's RPATH='%s'\n", pnt);
  244. if ((tpnt1 = search_for_named_library(libname, rflags, pnt, rpnt, NULL)) != NULL)
  245. return tpnt1;
  246. }
  247. #endif
  248. #endif
  249. /*
  250. * Where should the cache be searched? There is no such concept in the
  251. * ABI, so we have some flexibility here. For now, search it before
  252. * the hard coded paths that follow (i.e before /lib and /usr/lib).
  253. */
  254. #ifdef __LDSO_CACHE_SUPPORT__
  255. if (_dl_cache_addr != NULL && _dl_cache_addr != MAP_FAILED) {
  256. int i;
  257. header_t *header = (header_t *) _dl_cache_addr;
  258. libentry_t *libent = (libentry_t *) & header[1];
  259. char *strs = (char *) &libent[header->nlibs];
  260. _dl_if_debug_dprint("\tsearching cache='%s'\n", LDSO_CACHE);
  261. for (i = 0; i < header->nlibs; i++) {
  262. if ((libent[i].flags == LIB_ELF
  263. || libent[i].flags == LIB_ELF_LIBC0
  264. || libent[i].flags == LIB_ELF_LIBC5)
  265. && _dl_strcmp(libname, strs + libent[i].sooffset) == 0
  266. && (tpnt1 = _dl_load_elf_shared_library(rflags, rpnt, strs + libent[i].liboffset))
  267. ) {
  268. return tpnt1;
  269. }
  270. }
  271. }
  272. #endif
  273. #if defined SHARED && defined __LDSO_SEARCH_INTERP_PATH__
  274. /* Look for libraries wherever the shared library loader
  275. * was installed */
  276. _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath);
  277. tpnt1 = search_for_named_library(libname, rflags, _dl_ldsopath, rpnt, NULL);
  278. if (tpnt1 != NULL)
  279. return tpnt1;
  280. #endif
  281. /* Lastly, search the standard list of paths for the library.
  282. This list must exactly match the list in uClibc/ldso/util/ldd.c */
  283. _dl_if_debug_dprint("\tsearching full lib path list\n");
  284. tpnt1 = search_for_named_library(libname, rflags,
  285. UCLIBC_RUNTIME_PREFIX "lib:"
  286. UCLIBC_RUNTIME_PREFIX "usr/lib"
  287. #ifndef __LDSO_CACHE_SUPPORT__
  288. ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
  289. #endif
  290. , rpnt, NULL);
  291. if (tpnt1 != NULL)
  292. return tpnt1;
  293. #ifdef __LDSO_RUNPATH_OF_EXECUTABLE__
  294. /* Very last resort, try the executable's DT_RUNPATH and DT_RPATH */
  295. /* http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#shobj_dependencies
  296. * The set of directories specified by a given DT_RUNPATH entry is
  297. * used to find only the immediate dependencies of the executable or
  298. * shared object containing the DT_RUNPATH entry. That is, it is
  299. * used only for those dependencies contained in the DT_NEEDED
  300. * entries of the dynamic structure containing the DT_RUNPATH entry,
  301. * itself. One object's DT_RUNPATH entry does not affect the search
  302. * for any other object's dependencies.
  303. *
  304. * glibc (around 2.19) violates this and the usual suspects are
  305. * abusing this bug^Wrelaxed, user-friendly behaviour.
  306. */
  307. pnt = (char *) _dl_loaded_modules->dynamic_info[DT_RUNPATH];
  308. if (pnt) {
  309. pnt += (unsigned long) _dl_loaded_modules->dynamic_info[DT_STRTAB];
  310. _dl_if_debug_dprint("\tsearching exe's RUNPATH='%s'\n", pnt);
  311. if ((tpnt1 = search_for_named_library(libname, rflags, pnt, rpnt, NULL)) != NULL)
  312. return tpnt1;
  313. }
  314. pnt = (char *) _dl_loaded_modules->dynamic_info[DT_RPATH];
  315. if (pnt) {
  316. pnt += (unsigned long) _dl_loaded_modules->dynamic_info[DT_STRTAB];
  317. _dl_if_debug_dprint("\tsearching exe's RPATH='%s'\n", pnt);
  318. if ((tpnt1 = search_for_named_library(libname, rflags, pnt, rpnt, NULL)) != NULL)
  319. return tpnt1;
  320. }
  321. #endif
  322. goof:
  323. /* Well, we shot our wad on that one. All we can do now is punt */
  324. if (_dl_internal_error_number)
  325. _dl_error_number = _dl_internal_error_number;
  326. else
  327. _dl_error_number = LD_ERROR_NOFILE;
  328. _dl_if_debug_dprint("Bummer: could not find '%s'!\n", libname);
  329. return NULL;
  330. }
  331. /* Define the _dl_library_offset for the architectures that need it */
  332. DL_DEF_LIB_OFFSET;
  333. /*
  334. * Make a writeable mapping of a segment, regardless of whether PF_W is
  335. * set or not.
  336. */
  337. static void *
  338. map_writeable (int infile, ElfW(Phdr) *ppnt, int piclib, int flags,
  339. unsigned long libaddr)
  340. {
  341. int prot_flags = ppnt->p_flags | PF_W;
  342. char *status, *retval;
  343. char *tryaddr;
  344. ssize_t size;
  345. unsigned long map_size;
  346. char *cpnt;
  347. char *piclib2map = NULL;
  348. if (piclib == 2 &&
  349. /* We might be able to avoid this call if memsz doesn't
  350. require an additional page, but this would require mmap
  351. to always return page-aligned addresses and a whole
  352. number of pages allocated. Unfortunately on uClinux
  353. may return misaligned addresses and may allocate
  354. partial pages, so we may end up doing unnecessary mmap
  355. calls.
  356. This is what we could do if we knew mmap would always
  357. return aligned pages:
  358. ((ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) &
  359. PAGE_ALIGN) < ppnt->p_vaddr + ppnt->p_memsz)
  360. Instead, we have to do this: */
  361. ppnt->p_filesz < ppnt->p_memsz)
  362. {
  363. piclib2map = (char *)
  364. _dl_mmap(0, (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_memsz,
  365. LXFLAGS(prot_flags), flags | MAP_ANONYMOUS, -1, 0);
  366. if (_dl_mmap_check_error(piclib2map))
  367. return 0;
  368. }
  369. tryaddr = piclib == 2 ? piclib2map
  370. : ((char *) (piclib ? libaddr : DL_GET_LIB_OFFSET()) +
  371. (ppnt->p_vaddr & PAGE_ALIGN));
  372. size = (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_filesz;
  373. /* For !MMU, mmap to fixed address will fail.
  374. So instead of desperately call mmap and fail,
  375. we set status to MAP_FAILED to save a call
  376. to mmap (). */
  377. #ifndef __ARCH_USE_MMU__
  378. if (piclib2map == 0)
  379. #endif
  380. status = (char *) _dl_mmap
  381. (tryaddr, size, LXFLAGS(prot_flags),
  382. flags | (piclib2map ? MAP_FIXED : 0),
  383. infile, ppnt->p_offset & OFFS_ALIGN);
  384. #ifndef __ARCH_USE_MMU__
  385. else
  386. status = MAP_FAILED;
  387. #endif
  388. #ifdef _DL_PREAD
  389. if (_dl_mmap_check_error(status) && piclib2map
  390. && (_DL_PREAD (infile, tryaddr, size,
  391. ppnt->p_offset & OFFS_ALIGN) == size))
  392. status = tryaddr;
  393. #endif
  394. if (_dl_mmap_check_error(status) || (tryaddr && tryaddr != status))
  395. return 0;
  396. if (piclib2map)
  397. retval = piclib2map;
  398. else
  399. retval = status;
  400. /* Now we want to allocate and zero-out any data from the end
  401. of the region we mapped in from the file (filesz) to the
  402. end of the loadable segment (memsz). We may need
  403. additional pages for memsz, that we map in below, and we
  404. can count on the kernel to zero them out, but we have to
  405. zero out stuff in the last page that we mapped in from the
  406. file. However, we can't assume to have actually obtained
  407. full pages from the kernel, since we didn't ask for them,
  408. and uClibc may not give us full pages for small
  409. allocations. So only zero out up to memsz or the end of
  410. the page, whichever comes first. */
  411. /* CPNT is the beginning of the memsz portion not backed by
  412. filesz. */
  413. cpnt = (char *) (status + size);
  414. /* MAP_SIZE is the address of the
  415. beginning of the next page. */
  416. map_size = (ppnt->p_vaddr + ppnt->p_filesz
  417. + ADDR_ALIGN) & PAGE_ALIGN;
  418. _dl_memset (cpnt, 0,
  419. MIN (map_size
  420. - (ppnt->p_vaddr
  421. + ppnt->p_filesz),
  422. ppnt->p_memsz
  423. - ppnt->p_filesz));
  424. if (map_size < ppnt->p_vaddr + ppnt->p_memsz && !piclib2map) {
  425. tryaddr = map_size + (char*)(piclib ? libaddr : 0);
  426. status = (char *) _dl_mmap(tryaddr,
  427. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  428. LXFLAGS(prot_flags),
  429. flags | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
  430. if (_dl_mmap_check_error(status) || tryaddr != status)
  431. return NULL;
  432. }
  433. return retval;
  434. }
  435. /*
  436. * Read one ELF library into memory, mmap it into the correct locations and
  437. * add the symbol info to the symbol chain. Perform any relocations that
  438. * are required.
  439. */
  440. struct elf_resolve *_dl_load_elf_shared_library(unsigned int rflags,
  441. struct dyn_elf **rpnt, const char *libname)
  442. {
  443. ElfW(Ehdr) *epnt;
  444. unsigned long dynamic_addr = 0;
  445. ElfW(Dyn) *dpnt;
  446. struct elf_resolve *tpnt;
  447. ElfW(Phdr) *ppnt;
  448. #if defined(USE_TLS) && USE_TLS
  449. ElfW(Phdr) *tlsppnt = NULL;
  450. #endif
  451. char *status, *header;
  452. unsigned long dynamic_info[DYNAMIC_SIZE];
  453. unsigned long *lpnt;
  454. unsigned long libaddr;
  455. unsigned long minvma = 0xffffffff, maxvma = 0;
  456. unsigned int rtld_flags;
  457. int i, flags, piclib, infile;
  458. ElfW(Addr) relro_addr = 0;
  459. size_t relro_size = 0;
  460. struct stat st;
  461. uint32_t *p32;
  462. DL_LOADADDR_TYPE lib_loadaddr;
  463. DL_INIT_LOADADDR_EXTRA_DECLS
  464. libaddr = 0;
  465. infile = _dl_open(libname, O_RDONLY, 0);
  466. if (infile < 0) {
  467. _dl_internal_error_number = LD_ERROR_NOFILE;
  468. return NULL;
  469. }
  470. if (_dl_fstat(infile, &st) < 0) {
  471. _dl_internal_error_number = LD_ERROR_NOFILE;
  472. _dl_close(infile);
  473. return NULL;
  474. }
  475. /* If we are in secure mode (i.e. a setuid/gid binary using LD_PRELOAD),
  476. we don't load the library if it isn't setuid. */
  477. if (rflags & __RTLD_SECURE) {
  478. if (!(st.st_mode & S_ISUID)) {
  479. _dl_close(infile);
  480. return NULL;
  481. }
  482. }
  483. /* Check if file is already loaded */
  484. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  485. if (tpnt->st_dev == st.st_dev && tpnt->st_ino == st.st_ino) {
  486. /* Already loaded */
  487. tpnt->usage_count++;
  488. _dl_close(infile);
  489. return tpnt;
  490. }
  491. }
  492. if (rflags & RTLD_NOLOAD) {
  493. _dl_close(infile);
  494. return NULL;
  495. }
  496. header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  497. MAP_PRIVATE | MAP_ANONYMOUS | MAP_UNINITIALIZED, -1, 0);
  498. if (_dl_mmap_check_error(header)) {
  499. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  500. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  501. _dl_close(infile);
  502. return NULL;
  503. }
  504. _dl_read(infile, header, _dl_pagesize);
  505. epnt = (ElfW(Ehdr) *) (intptr_t) header;
  506. p32 = (uint32_t*)&epnt->e_ident;
  507. if (*p32 != ELFMAG_U32) {
  508. _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  509. libname);
  510. _dl_internal_error_number = LD_ERROR_NOTELF;
  511. _dl_close(infile);
  512. _dl_munmap(header, _dl_pagesize);
  513. return NULL;
  514. }
  515. if ((epnt->e_type != ET_DYN
  516. #ifdef __LDSO_STANDALONE_SUPPORT__
  517. && epnt->e_type != ET_EXEC
  518. #endif
  519. ) || (epnt->e_machine != MAGIC1
  520. #ifdef MAGIC2
  521. && epnt->e_machine != MAGIC2
  522. #endif
  523. ))
  524. {
  525. _dl_internal_error_number =
  526. (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
  527. _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  528. "\n", _dl_progname, libname);
  529. _dl_close(infile);
  530. _dl_munmap(header, _dl_pagesize);
  531. return NULL;
  532. }
  533. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  534. piclib = 1;
  535. for (i = 0; i < epnt->e_phnum; i++) {
  536. if (ppnt->p_type == PT_DYNAMIC) {
  537. if (dynamic_addr)
  538. _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
  539. _dl_progname, libname);
  540. dynamic_addr = ppnt->p_vaddr;
  541. }
  542. if (ppnt->p_type == PT_LOAD) {
  543. /* See if this is a PIC library. */
  544. if (minvma == 0xffffffff && ppnt->p_vaddr > 0x1000000) {
  545. piclib = 0;
  546. minvma = ppnt->p_vaddr;
  547. }
  548. if (piclib && ppnt->p_vaddr < minvma) {
  549. minvma = ppnt->p_vaddr;
  550. }
  551. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  552. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  553. }
  554. }
  555. if (ppnt->p_type == PT_TLS) {
  556. #if defined(USE_TLS) && USE_TLS
  557. if (ppnt->p_memsz == 0)
  558. /* Nothing to do for an empty segment. */
  559. continue;
  560. else
  561. /* Save for after 'tpnt' is actually allocated. */
  562. tlsppnt = ppnt;
  563. #else
  564. /*
  565. * Yup, the user was an idiot and tried to sneak in a library with
  566. * TLS in it and we don't support it. Let's fall on our own sword
  567. * and scream at the luser while we die.
  568. */
  569. _dl_dprintf(2, "%s: '%s' library contains unsupported TLS\n",
  570. _dl_progname, libname);
  571. _dl_internal_error_number = LD_ERROR_TLS_FAILED;
  572. _dl_close(infile);
  573. _dl_munmap(header, _dl_pagesize);
  574. return NULL;
  575. #endif
  576. }
  577. ppnt++;
  578. }
  579. #ifdef __LDSO_STANDALONE_SUPPORT__
  580. if (epnt->e_type == ET_EXEC)
  581. piclib = 0;
  582. #endif
  583. DL_CHECK_LIB_TYPE (epnt, piclib, _dl_progname, libname);
  584. maxvma = (maxvma + ADDR_ALIGN) & PAGE_ALIGN;
  585. minvma = minvma & ~ADDR_ALIGN;
  586. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  587. if (piclib == 0 || piclib == 1) {
  588. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  589. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  590. if (_dl_mmap_check_error(status)) {
  591. cant_map:
  592. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  593. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  594. _dl_close(infile);
  595. _dl_munmap(header, _dl_pagesize);
  596. return NULL;
  597. }
  598. libaddr = (unsigned long) status;
  599. flags |= MAP_FIXED;
  600. }
  601. /* Get the memory to store the library */
  602. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  603. DL_INIT_LOADADDR(lib_loadaddr, libaddr - minvma, ppnt, epnt->e_phnum);
  604. /* Set _dl_library_offset to lib_loadaddr or 0. */
  605. DL_SET_LIB_OFFSET(lib_loadaddr);
  606. for (i = 0; i < epnt->e_phnum; i++) {
  607. if (DL_IS_SPECIAL_SEGMENT (epnt, ppnt)) {
  608. char *addr;
  609. addr = DL_MAP_SEGMENT (epnt, ppnt, infile, flags);
  610. if (addr == NULL) {
  611. cant_map1:
  612. DL_LOADADDR_UNMAP (lib_loadaddr, maxvma - minvma);
  613. goto cant_map;
  614. }
  615. DL_INIT_LOADADDR_HDR (lib_loadaddr, addr, ppnt);
  616. ppnt++;
  617. continue;
  618. }
  619. if (ppnt->p_type == PT_GNU_RELRO) {
  620. relro_addr = ppnt->p_vaddr;
  621. relro_size = ppnt->p_memsz;
  622. }
  623. if (ppnt->p_type == PT_LOAD) {
  624. char *tryaddr;
  625. ssize_t size;
  626. if (ppnt->p_flags & PF_W) {
  627. status = map_writeable (infile, ppnt, piclib, flags, libaddr);
  628. if (status == NULL)
  629. goto cant_map1;
  630. } else {
  631. tryaddr = (piclib == 2 ? 0
  632. : (char *) (ppnt->p_vaddr & PAGE_ALIGN)
  633. + (piclib ? libaddr : DL_GET_LIB_OFFSET()));
  634. size = (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_filesz;
  635. status = (char *) _dl_mmap
  636. (tryaddr, size, LXFLAGS(ppnt->p_flags),
  637. flags | (piclib == 2 ? MAP_EXECUTABLE
  638. | MAP_DENYWRITE : 0),
  639. infile, ppnt->p_offset & OFFS_ALIGN);
  640. if (_dl_mmap_check_error(status)
  641. || (tryaddr && tryaddr != status))
  642. goto cant_map1;
  643. }
  644. DL_INIT_LOADADDR_HDR(lib_loadaddr,
  645. status + (ppnt->p_vaddr & ADDR_ALIGN),
  646. ppnt);
  647. /* if (libaddr == 0 && piclib) {
  648. libaddr = (unsigned long) status;
  649. flags |= MAP_FIXED;
  650. } */
  651. }
  652. ppnt++;
  653. }
  654. /*
  655. * The dynamic_addr must be take into acount lib_loadaddr value, to note
  656. * it is zero when the SO has been mapped to the elf's physical addr
  657. */
  658. #ifdef __LDSO_PRELINK_SUPPORT__
  659. if (DL_GET_LIB_OFFSET()) {
  660. #else
  661. if (piclib) {
  662. #endif
  663. dynamic_addr = (unsigned long) DL_RELOC_ADDR(lib_loadaddr, dynamic_addr);
  664. }
  665. /*
  666. * OK, the ELF library is now loaded into VM in the correct locations
  667. * The next step is to go through and do the dynamic linking (if needed).
  668. */
  669. /* Start by scanning the dynamic section to get all of the pointers */
  670. if (!dynamic_addr) {
  671. _dl_internal_error_number = LD_ERROR_NODYNAMIC;
  672. _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
  673. _dl_progname, libname);
  674. _dl_munmap(header, _dl_pagesize);
  675. _dl_close(infile);
  676. return NULL;
  677. }
  678. dpnt = (ElfW(Dyn) *) dynamic_addr;
  679. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  680. rtld_flags = _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
  681. /* If the TEXTREL is set, this means that we need to make the pages
  682. writable before we perform relocations. Do this now. They get set
  683. back again later. */
  684. if (dynamic_info[DT_TEXTREL]) {
  685. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  686. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  687. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  688. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W)) {
  689. #ifdef __ARCH_USE_MMU__
  690. _dl_mprotect((void *) ((piclib ? libaddr : DL_GET_LIB_OFFSET()) +
  691. (ppnt->p_vaddr & PAGE_ALIGN)),
  692. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  693. PROT_READ | PROT_WRITE | PROT_EXEC);
  694. #else
  695. void *new_addr;
  696. new_addr = map_writeable (infile, ppnt, piclib, flags, libaddr);
  697. if (!new_addr) {
  698. _dl_dprintf(2, "Can't modify %s's text section.",
  699. libname);
  700. _dl_exit(1);
  701. }
  702. DL_UPDATE_LOADADDR_HDR(lib_loadaddr,
  703. new_addr + (ppnt->p_vaddr & ADDR_ALIGN),
  704. ppnt);
  705. /* This has invalidated all pointers into the previously readonly segment.
  706. Update any them to point into the remapped segment. */
  707. _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
  708. #endif
  709. }
  710. }
  711. #else
  712. _dl_dprintf(2, "Can't modify %s's text section."
  713. " Use GCC option -fPIC for shared objects, please.\n",
  714. libname);
  715. _dl_exit(1);
  716. #endif
  717. }
  718. _dl_close(infile);
  719. tpnt = _dl_add_elf_hash_table(libname, lib_loadaddr, dynamic_info,
  720. dynamic_addr, 0);
  721. tpnt->mapaddr = libaddr;
  722. tpnt->relro_addr = relro_addr;
  723. tpnt->relro_size = relro_size;
  724. tpnt->st_dev = st.st_dev;
  725. tpnt->st_ino = st.st_ino;
  726. tpnt->ppnt = (ElfW(Phdr) *)
  727. DL_RELOC_ADDR(DL_GET_RUN_ADDR(tpnt->loadaddr, tpnt->mapaddr),
  728. epnt->e_phoff);
  729. tpnt->n_phent = epnt->e_phnum;
  730. tpnt->rtld_flags = rflags | rtld_flags;
  731. #ifdef __LDSO_STANDALONE_SUPPORT__
  732. tpnt->l_entry = epnt->e_entry;
  733. #endif
  734. #if defined(USE_TLS) && USE_TLS
  735. if (tlsppnt) {
  736. _dl_debug_early("Found TLS header for %s\n", libname);
  737. # if NO_TLS_OFFSET != 0
  738. tpnt->l_tls_offset = NO_TLS_OFFSET;
  739. # endif
  740. tpnt->l_tls_blocksize = tlsppnt->p_memsz;
  741. tpnt->l_tls_align = tlsppnt->p_align;
  742. if (tlsppnt->p_align == 0)
  743. tpnt->l_tls_firstbyte_offset = 0;
  744. else
  745. tpnt->l_tls_firstbyte_offset = tlsppnt->p_vaddr &
  746. (tlsppnt->p_align - 1);
  747. tpnt->l_tls_initimage_size = tlsppnt->p_filesz;
  748. tpnt->l_tls_initimage = (void *) tlsppnt->p_vaddr;
  749. /* Assign the next available module ID. */
  750. tpnt->l_tls_modid = _dl_next_tls_modid ();
  751. /* We know the load address, so add it to the offset. */
  752. #ifdef __LDSO_STANDALONE_SUPPORT__
  753. if ((tpnt->l_tls_initimage != NULL) && piclib)
  754. #else
  755. if (tpnt->l_tls_initimage != NULL)
  756. #endif
  757. {
  758. # ifdef __SUPPORT_LD_DEBUG_EARLY__
  759. char *tmp = (char *) tpnt->l_tls_initimage;
  760. tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
  761. _dl_debug_early("Relocated TLS initial image from %x to %x (size = %x)\n", tmp, tpnt->l_tls_initimage, tpnt->l_tls_initimage_size);
  762. tmp = 0;
  763. # else
  764. tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
  765. # endif
  766. }
  767. }
  768. #endif
  769. /*
  770. * Add this object into the symbol chain
  771. */
  772. if (*rpnt
  773. #ifdef __LDSO_STANDALONE_SUPPORT__
  774. /* Do not create a new chain entry for the main executable */
  775. && (*rpnt)->dyn
  776. #endif
  777. ) {
  778. (*rpnt)->next = _dl_malloc(sizeof(struct dyn_elf));
  779. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  780. (*rpnt)->next->prev = (*rpnt);
  781. *rpnt = (*rpnt)->next;
  782. }
  783. #ifndef SHARED
  784. /* When statically linked, the first time we dlopen a DSO
  785. * the *rpnt is NULL, so we need to allocate memory for it,
  786. * and initialize the _dl_symbol_table.
  787. */
  788. else {
  789. *rpnt = _dl_symbol_tables = _dl_malloc(sizeof(struct dyn_elf));
  790. _dl_memset(*rpnt, 0, sizeof(struct dyn_elf));
  791. }
  792. #endif
  793. (*rpnt)->dyn = tpnt;
  794. tpnt->usage_count++;
  795. #ifdef __LDSO_STANDALONE_SUPPORT__
  796. tpnt->libtype = (epnt->e_type == ET_DYN) ? elf_lib : elf_executable;
  797. #else
  798. tpnt->libtype = elf_lib;
  799. #endif
  800. /*
  801. * OK, the next thing we need to do is to insert the dynamic linker into
  802. * the proper entry in the GOT so that the PLT symbols can be properly
  803. * resolved.
  804. */
  805. lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
  806. if (lpnt) {
  807. lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
  808. INIT_GOT(lpnt, tpnt);
  809. }
  810. #ifdef __DSBT__
  811. /* Handle DSBT initialization */
  812. {
  813. struct elf_resolve *t, *ref;
  814. int idx = tpnt->dsbt_index;
  815. void **dsbt = tpnt->dsbt_table;
  816. /*
  817. * It is okay (required actually) to have zero idx for an executable.
  818. * This is the case when running ldso standalone and the program
  819. * is being mapped in via _dl_load_shared_library().
  820. */
  821. if (idx == 0 && tpnt->libtype != elf_executable) {
  822. if (!dynamic_info[DT_TEXTREL]) {
  823. /* This DSO has not been assigned an index. */
  824. _dl_dprintf(2, "%s: '%s' is missing a dsbt index assignment!\n",
  825. _dl_progname, libname);
  826. _dl_exit(1);
  827. }
  828. /* Find a dsbt table from another module. */
  829. ref = NULL;
  830. for (t = _dl_loaded_modules; t; t = t->next) {
  831. if (ref == NULL && t != tpnt) {
  832. ref = t;
  833. break;
  834. }
  835. }
  836. idx = tpnt->dsbt_size;
  837. while (idx-- > 0)
  838. if (!ref || ref->dsbt_table[idx] == NULL)
  839. break;
  840. if (idx <= 0) {
  841. _dl_dprintf(2, "%s: '%s' caused DSBT table overflow!\n",
  842. _dl_progname, libname);
  843. _dl_exit(1);
  844. }
  845. _dl_if_debug_dprint("\n\tfile='%s'; assigned index %d\n",
  846. libname, idx);
  847. tpnt->dsbt_index = idx;
  848. }
  849. /* make sure index is not already used */
  850. if (_dl_ldso_dsbt[idx]) {
  851. struct elf_resolve *dup;
  852. const char *dup_name;
  853. for (dup = _dl_loaded_modules; dup; dup = dup->next)
  854. if (dup != tpnt && dup->dsbt_index == idx)
  855. break;
  856. if (dup)
  857. dup_name = dup->libname;
  858. else if (idx == 1)
  859. dup_name = "runtime linker";
  860. else
  861. dup_name = "unknown library";
  862. _dl_dprintf(2, "%s: '%s' dsbt index %d already used by %s!\n",
  863. _dl_progname, libname, idx, dup_name);
  864. _dl_exit(1);
  865. }
  866. /*
  867. * Setup dsbt slot for this module in dsbt of all modules.
  868. */
  869. for (t = _dl_loaded_modules; t; t = t->next)
  870. t->dsbt_table[idx] = dsbt;
  871. _dl_ldso_dsbt[idx] = dsbt;
  872. _dl_memcpy(dsbt, _dl_ldso_dsbt,
  873. tpnt->dsbt_size * sizeof(tpnt->dsbt_table[0]));
  874. }
  875. #endif
  876. _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname);
  877. _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr, DL_LOADADDR_BASE(lib_loadaddr));
  878. _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
  879. DL_RELOC_ADDR(lib_loadaddr, epnt->e_entry), tpnt->ppnt, tpnt->n_phent);
  880. _dl_munmap(header, _dl_pagesize);
  881. return tpnt;
  882. }
  883. /* now_flag must be RTLD_NOW or zero */
  884. int _dl_fixup(struct dyn_elf *rpnt, struct r_scope_elem *scope, int now_flag)
  885. {
  886. int goof = 0;
  887. struct elf_resolve *tpnt;
  888. ElfW(Word) reloc_size, relative_count;
  889. ElfW(Addr) reloc_addr;
  890. if (rpnt->next)
  891. goof = _dl_fixup(rpnt->next, scope, now_flag);
  892. if (goof)
  893. return goof;
  894. tpnt = rpnt->dyn;
  895. if (!(tpnt->init_flag & RELOCS_DONE))
  896. _dl_if_debug_dprint("relocation processing: %s\n", tpnt->libname);
  897. if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
  898. _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
  899. _dl_progname, UNSUPPORTED_RELOC_STR);
  900. goof++;
  901. return goof;
  902. }
  903. reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
  904. /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
  905. range. Note that according to the ELF spec, this is completely legal! */
  906. #ifdef ELF_MACHINE_PLTREL_OVERLAP
  907. reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
  908. #endif
  909. if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
  910. !(tpnt->init_flag & RELOCS_DONE)) {
  911. reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
  912. relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
  913. if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
  914. reloc_size -= relative_count * sizeof(ELF_RELOC);
  915. #ifdef __LDSO_PRELINK_SUPPORT__
  916. if (tpnt->loadaddr || (!tpnt->dynamic_info[DT_GNU_PRELINKED_IDX]))
  917. #endif
  918. elf_machine_relative(tpnt->loadaddr, reloc_addr, relative_count);
  919. reloc_addr += relative_count * sizeof(ELF_RELOC);
  920. }
  921. goof += _dl_parse_relocation_information(rpnt, scope,
  922. reloc_addr,
  923. reloc_size);
  924. tpnt->init_flag |= RELOCS_DONE;
  925. }
  926. if (tpnt->dynamic_info[DT_BIND_NOW])
  927. now_flag = RTLD_NOW;
  928. if (tpnt->dynamic_info[DT_JMPREL] &&
  929. (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
  930. (now_flag && !(tpnt->rtld_flags & now_flag)))) {
  931. tpnt->rtld_flags |= now_flag;
  932. if (!(tpnt->rtld_flags & RTLD_NOW)) {
  933. _dl_parse_lazy_relocation_information(rpnt,
  934. tpnt->dynamic_info[DT_JMPREL],
  935. tpnt->dynamic_info [DT_PLTRELSZ]);
  936. } else {
  937. goof += _dl_parse_relocation_information(rpnt, scope,
  938. tpnt->dynamic_info[DT_JMPREL],
  939. tpnt->dynamic_info[DT_PLTRELSZ]);
  940. }
  941. tpnt->init_flag |= JMP_RELOCS_DONE;
  942. }
  943. #if 0
  944. /* _dl_add_to_slotinfo is called by init_tls() for initial DSO
  945. or by dlopen() for dynamically loaded DSO. */
  946. #if defined(USE_TLS) && USE_TLS
  947. /* Add object to slot information data if necessasy. */
  948. if (tpnt->l_tls_blocksize != 0 && tls_init_tp_called)
  949. _dl_add_to_slotinfo ((struct link_map *) tpnt);
  950. #endif
  951. #endif
  952. return goof;
  953. }
  954. #ifdef IS_IN_rtld
  955. /* Minimal printf which handles only %s, %d, and %x */
  956. void _dl_dprintf(int fd, const char *fmt, ...)
  957. {
  958. #if __WORDSIZE > 32
  959. long int num;
  960. #else
  961. int num;
  962. #endif
  963. va_list args;
  964. char *start, *ptr, *string;
  965. char *buf;
  966. if (!fmt)
  967. return;
  968. buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  969. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  970. if (_dl_mmap_check_error(buf)) {
  971. _dl_write(fd, "mmap of a spare page failed!\n", 29);
  972. _dl_exit(20);
  973. }
  974. start = ptr = buf;
  975. if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
  976. _dl_write(fd, "overflow\n", 11);
  977. _dl_exit(20);
  978. }
  979. _dl_strcpy(buf, fmt);
  980. va_start(args, fmt);
  981. while (start) {
  982. while (*ptr != '%' && *ptr) {
  983. ptr++;
  984. }
  985. if (*ptr == '%') {
  986. *ptr++ = '\0';
  987. _dl_write(fd, start, _dl_strlen(start));
  988. switch (*ptr++) {
  989. case 's':
  990. string = va_arg(args, char *);
  991. if (!string)
  992. _dl_write(fd, "(null)", 6);
  993. else
  994. _dl_write(fd, string, _dl_strlen(string));
  995. break;
  996. case 'i':
  997. case 'd':
  998. {
  999. char tmp[22];
  1000. #if __WORDSIZE > 32
  1001. num = va_arg(args, long int);
  1002. #else
  1003. num = va_arg(args, int);
  1004. #endif
  1005. string = _dl_simple_ltoa(tmp, num);
  1006. _dl_write(fd, string, _dl_strlen(string));
  1007. break;
  1008. }
  1009. case 'x':
  1010. case 'p':
  1011. {
  1012. char tmp[22];
  1013. #if __WORDSIZE > 32
  1014. num = va_arg(args, long int);
  1015. #else
  1016. num = va_arg(args, int);
  1017. #endif
  1018. string = _dl_simple_ltoahex(tmp, num);
  1019. _dl_write(fd, string, _dl_strlen(string));
  1020. break;
  1021. }
  1022. default:
  1023. _dl_write(fd, "(null)", 6);
  1024. break;
  1025. }
  1026. start = ptr;
  1027. } else {
  1028. _dl_write(fd, start, _dl_strlen(start));
  1029. start = NULL;
  1030. }
  1031. }
  1032. _dl_munmap(buf, _dl_pagesize);
  1033. return;
  1034. }
  1035. char *_dl_strdup(const char *string)
  1036. {
  1037. char *retval;
  1038. int len;
  1039. len = _dl_strlen(string);
  1040. retval = _dl_malloc(len + 1);
  1041. _dl_strcpy(retval, string);
  1042. return retval;
  1043. }
  1044. #endif
  1045. unsigned int _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[],
  1046. void *debug_addr, DL_LOADADDR_TYPE load_off)
  1047. {
  1048. return __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
  1049. }