dl-elf.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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. tpnt->init_flag |= DL_OPENED2;
  489. _dl_close(infile);
  490. return tpnt;
  491. }
  492. }
  493. if (rflags & RTLD_NOLOAD) {
  494. _dl_close(infile);
  495. return NULL;
  496. }
  497. header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  498. MAP_PRIVATE | MAP_ANONYMOUS | MAP_UNINITIALIZED, -1, 0);
  499. if (_dl_mmap_check_error(header)) {
  500. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  501. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  502. _dl_close(infile);
  503. return NULL;
  504. }
  505. _dl_read(infile, header, _dl_pagesize);
  506. epnt = (ElfW(Ehdr) *) (intptr_t) header;
  507. p32 = (uint32_t*)&epnt->e_ident;
  508. if (*p32 != ELFMAG_U32) {
  509. _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  510. libname);
  511. _dl_internal_error_number = LD_ERROR_NOTELF;
  512. _dl_close(infile);
  513. _dl_munmap(header, _dl_pagesize);
  514. return NULL;
  515. }
  516. if ((epnt->e_type != ET_DYN
  517. #ifdef __LDSO_STANDALONE_SUPPORT__
  518. && epnt->e_type != ET_EXEC
  519. #endif
  520. ) || (epnt->e_machine != MAGIC1
  521. #ifdef MAGIC2
  522. && epnt->e_machine != MAGIC2
  523. #endif
  524. ))
  525. {
  526. _dl_internal_error_number =
  527. (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
  528. _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  529. "\n", _dl_progname, libname);
  530. _dl_close(infile);
  531. _dl_munmap(header, _dl_pagesize);
  532. return NULL;
  533. }
  534. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  535. piclib = 1;
  536. for (i = 0; i < epnt->e_phnum; i++) {
  537. if (ppnt->p_type == PT_DYNAMIC) {
  538. if (dynamic_addr)
  539. _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
  540. _dl_progname, libname);
  541. dynamic_addr = ppnt->p_vaddr;
  542. }
  543. if (ppnt->p_type == PT_LOAD) {
  544. /* See if this is a PIC library. */
  545. if (minvma == 0xffffffff && ppnt->p_vaddr > 0x1000000) {
  546. piclib = 0;
  547. minvma = ppnt->p_vaddr;
  548. }
  549. if (piclib && ppnt->p_vaddr < minvma) {
  550. minvma = ppnt->p_vaddr;
  551. }
  552. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  553. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  554. }
  555. }
  556. if (ppnt->p_type == PT_TLS) {
  557. #if defined(USE_TLS) && USE_TLS
  558. if (ppnt->p_memsz == 0)
  559. /* Nothing to do for an empty segment. */
  560. continue;
  561. else
  562. /* Save for after 'tpnt' is actually allocated. */
  563. tlsppnt = ppnt;
  564. #else
  565. /*
  566. * Yup, the user was an idiot and tried to sneak in a library with
  567. * TLS in it and we don't support it. Let's fall on our own sword
  568. * and scream at the luser while we die.
  569. */
  570. _dl_dprintf(2, "%s: '%s' library contains unsupported TLS\n",
  571. _dl_progname, libname);
  572. _dl_internal_error_number = LD_ERROR_TLS_FAILED;
  573. _dl_close(infile);
  574. _dl_munmap(header, _dl_pagesize);
  575. return NULL;
  576. #endif
  577. }
  578. ppnt++;
  579. }
  580. #ifdef __LDSO_STANDALONE_SUPPORT__
  581. if (epnt->e_type == ET_EXEC)
  582. piclib = 0;
  583. #endif
  584. DL_CHECK_LIB_TYPE (epnt, piclib, _dl_progname, libname);
  585. maxvma = (maxvma + ADDR_ALIGN) & PAGE_ALIGN;
  586. minvma = minvma & ~ADDR_ALIGN;
  587. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  588. if (piclib == 0 || piclib == 1) {
  589. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  590. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  591. if (_dl_mmap_check_error(status)) {
  592. cant_map:
  593. _dl_dprintf(2, "%s:%i: can't map '%s'\n", _dl_progname, __LINE__, libname);
  594. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  595. _dl_close(infile);
  596. _dl_munmap(header, _dl_pagesize);
  597. return NULL;
  598. }
  599. libaddr = (unsigned long) status;
  600. flags |= MAP_FIXED;
  601. }
  602. /* Get the memory to store the library */
  603. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  604. DL_INIT_LOADADDR(lib_loadaddr, libaddr - minvma, ppnt, epnt->e_phnum);
  605. /* Set _dl_library_offset to lib_loadaddr or 0. */
  606. DL_SET_LIB_OFFSET(lib_loadaddr);
  607. for (i = 0; i < epnt->e_phnum; i++) {
  608. if (DL_IS_SPECIAL_SEGMENT (epnt, ppnt)) {
  609. char *addr;
  610. addr = DL_MAP_SEGMENT (epnt, ppnt, infile, flags);
  611. if (addr == NULL) {
  612. cant_map1:
  613. DL_LOADADDR_UNMAP (lib_loadaddr, maxvma - minvma);
  614. goto cant_map;
  615. }
  616. DL_INIT_LOADADDR_HDR (lib_loadaddr, addr, ppnt);
  617. ppnt++;
  618. continue;
  619. }
  620. if (ppnt->p_type == PT_GNU_RELRO) {
  621. relro_addr = ppnt->p_vaddr;
  622. relro_size = ppnt->p_memsz;
  623. }
  624. if (ppnt->p_type == PT_LOAD) {
  625. char *tryaddr;
  626. ssize_t size;
  627. if (ppnt->p_flags & PF_W) {
  628. status = map_writeable (infile, ppnt, piclib, flags, libaddr);
  629. if (status == NULL)
  630. goto cant_map1;
  631. } else {
  632. tryaddr = (piclib == 2 ? 0
  633. : (char *) (ppnt->p_vaddr & PAGE_ALIGN)
  634. + (piclib ? libaddr : DL_GET_LIB_OFFSET()));
  635. size = (ppnt->p_vaddr & ADDR_ALIGN) + ppnt->p_filesz;
  636. status = (char *) _dl_mmap
  637. (tryaddr, size, LXFLAGS(ppnt->p_flags),
  638. flags | (piclib == 2 ? MAP_EXECUTABLE
  639. | MAP_DENYWRITE : 0),
  640. infile, ppnt->p_offset & OFFS_ALIGN);
  641. if (_dl_mmap_check_error(status)
  642. || (tryaddr && tryaddr != status))
  643. goto cant_map1;
  644. }
  645. DL_INIT_LOADADDR_HDR(lib_loadaddr,
  646. status + (ppnt->p_vaddr & ADDR_ALIGN),
  647. ppnt);
  648. /* if (libaddr == 0 && piclib) {
  649. libaddr = (unsigned long) status;
  650. flags |= MAP_FIXED;
  651. } */
  652. }
  653. ppnt++;
  654. }
  655. /*
  656. * The dynamic_addr must be take into acount lib_loadaddr value, to note
  657. * it is zero when the SO has been mapped to the elf's physical addr
  658. */
  659. #ifdef __LDSO_PRELINK_SUPPORT__
  660. if (DL_GET_LIB_OFFSET()) {
  661. #else
  662. if (piclib) {
  663. #endif
  664. dynamic_addr = (unsigned long) DL_RELOC_ADDR(lib_loadaddr, dynamic_addr);
  665. }
  666. /*
  667. * OK, the ELF library is now loaded into VM in the correct locations
  668. * The next step is to go through and do the dynamic linking (if needed).
  669. */
  670. /* Start by scanning the dynamic section to get all of the pointers */
  671. if (!dynamic_addr) {
  672. _dl_internal_error_number = LD_ERROR_NODYNAMIC;
  673. _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
  674. _dl_progname, libname);
  675. _dl_munmap(header, _dl_pagesize);
  676. _dl_close(infile);
  677. return NULL;
  678. }
  679. dpnt = (ElfW(Dyn) *) dynamic_addr;
  680. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  681. rtld_flags = _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
  682. /* If the TEXTREL is set, this means that we need to make the pages
  683. writable before we perform relocations. Do this now. They get set
  684. back again later. */
  685. if (dynamic_info[DT_TEXTREL]) {
  686. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  687. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  688. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  689. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W)) {
  690. #ifdef __ARCH_USE_MMU__
  691. _dl_mprotect((void *) ((piclib ? libaddr : DL_GET_LIB_OFFSET()) +
  692. (ppnt->p_vaddr & PAGE_ALIGN)),
  693. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  694. PROT_READ | PROT_WRITE | PROT_EXEC);
  695. #else
  696. void *new_addr;
  697. new_addr = map_writeable (infile, ppnt, piclib, flags, libaddr);
  698. if (!new_addr) {
  699. _dl_dprintf(2, "Can't modify %s's text section.",
  700. libname);
  701. _dl_exit(1);
  702. }
  703. DL_UPDATE_LOADADDR_HDR(lib_loadaddr,
  704. new_addr + (ppnt->p_vaddr & ADDR_ALIGN),
  705. ppnt);
  706. /* This has invalidated all pointers into the previously readonly segment.
  707. Update any them to point into the remapped segment. */
  708. _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, lib_loadaddr);
  709. #endif
  710. }
  711. }
  712. #else
  713. _dl_dprintf(2, "Can't modify %s's text section."
  714. " Use GCC option -fPIC for shared objects, please.\n",
  715. libname);
  716. _dl_exit(1);
  717. #endif
  718. }
  719. _dl_close(infile);
  720. tpnt = _dl_add_elf_hash_table(libname, lib_loadaddr, dynamic_info,
  721. dynamic_addr, 0);
  722. tpnt->mapaddr = libaddr;
  723. tpnt->relro_addr = relro_addr;
  724. tpnt->relro_size = relro_size;
  725. tpnt->st_dev = st.st_dev;
  726. tpnt->st_ino = st.st_ino;
  727. tpnt->ppnt = (ElfW(Phdr) *)
  728. DL_RELOC_ADDR(DL_GET_RUN_ADDR(tpnt->loadaddr, tpnt->mapaddr),
  729. epnt->e_phoff);
  730. tpnt->n_phent = epnt->e_phnum;
  731. tpnt->rtld_flags = rflags | rtld_flags;
  732. #ifdef __LDSO_STANDALONE_SUPPORT__
  733. tpnt->l_entry = epnt->e_entry;
  734. #endif
  735. #if defined(USE_TLS) && USE_TLS
  736. if (tlsppnt) {
  737. _dl_debug_early("Found TLS header for %s\n", libname);
  738. # if NO_TLS_OFFSET != 0
  739. tpnt->l_tls_offset = NO_TLS_OFFSET;
  740. # endif
  741. tpnt->l_tls_blocksize = tlsppnt->p_memsz;
  742. tpnt->l_tls_align = tlsppnt->p_align;
  743. if (tlsppnt->p_align == 0)
  744. tpnt->l_tls_firstbyte_offset = 0;
  745. else
  746. tpnt->l_tls_firstbyte_offset = tlsppnt->p_vaddr &
  747. (tlsppnt->p_align - 1);
  748. tpnt->l_tls_initimage_size = tlsppnt->p_filesz;
  749. tpnt->l_tls_initimage = (void *) tlsppnt->p_vaddr;
  750. /* Assign the next available module ID. */
  751. tpnt->l_tls_modid = _dl_next_tls_modid ();
  752. /* We know the load address, so add it to the offset. */
  753. #ifdef __LDSO_STANDALONE_SUPPORT__
  754. if ((tpnt->l_tls_initimage != NULL) && piclib)
  755. #else
  756. if (tpnt->l_tls_initimage != NULL)
  757. #endif
  758. {
  759. # ifdef __SUPPORT_LD_DEBUG_EARLY__
  760. char *tmp = (char *) tpnt->l_tls_initimage;
  761. tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
  762. _dl_debug_early("Relocated TLS initial image from %x to %x (size = %x)\n", tmp, tpnt->l_tls_initimage, tpnt->l_tls_initimage_size);
  763. tmp = 0;
  764. # else
  765. tpnt->l_tls_initimage = (char *) tlsppnt->p_vaddr + tpnt->loadaddr;
  766. # endif
  767. }
  768. }
  769. #endif
  770. /*
  771. * Add this object into the symbol chain
  772. */
  773. if (*rpnt
  774. #ifdef __LDSO_STANDALONE_SUPPORT__
  775. /* Do not create a new chain entry for the main executable */
  776. && (*rpnt)->dyn
  777. #endif
  778. ) {
  779. (*rpnt)->next = _dl_malloc(sizeof(struct dyn_elf));
  780. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  781. (*rpnt)->next->prev = (*rpnt);
  782. *rpnt = (*rpnt)->next;
  783. }
  784. #ifndef SHARED
  785. /* When statically linked, the first time we dlopen a DSO
  786. * the *rpnt is NULL, so we need to allocate memory for it,
  787. * and initialize the _dl_symbol_table.
  788. */
  789. else {
  790. *rpnt = _dl_symbol_tables = _dl_malloc(sizeof(struct dyn_elf));
  791. _dl_memset(*rpnt, 0, sizeof(struct dyn_elf));
  792. }
  793. #endif
  794. (*rpnt)->dyn = tpnt;
  795. tpnt->usage_count++;
  796. if (tpnt->rtld_flags & RTLD_NODELETE)
  797. tpnt->usage_count++;
  798. #ifdef __LDSO_STANDALONE_SUPPORT__
  799. tpnt->libtype = (epnt->e_type == ET_DYN) ? elf_lib : elf_executable;
  800. #else
  801. tpnt->libtype = elf_lib;
  802. #endif
  803. /*
  804. * OK, the next thing we need to do is to insert the dynamic linker into
  805. * the proper entry in the GOT so that the PLT symbols can be properly
  806. * resolved.
  807. */
  808. lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
  809. if (lpnt) {
  810. lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
  811. INIT_GOT(lpnt, tpnt);
  812. }
  813. #ifdef __DSBT__
  814. /* Handle DSBT initialization */
  815. {
  816. struct elf_resolve *t, *ref;
  817. int idx = tpnt->dsbt_index;
  818. void **dsbt = tpnt->dsbt_table;
  819. /*
  820. * It is okay (required actually) to have zero idx for an executable.
  821. * This is the case when running ldso standalone and the program
  822. * is being mapped in via _dl_load_shared_library().
  823. */
  824. if (idx == 0 && tpnt->libtype != elf_executable) {
  825. if (!dynamic_info[DT_TEXTREL]) {
  826. /* This DSO has not been assigned an index. */
  827. _dl_dprintf(2, "%s: '%s' is missing a dsbt index assignment!\n",
  828. _dl_progname, libname);
  829. _dl_exit(1);
  830. }
  831. /* Find a dsbt table from another module. */
  832. ref = NULL;
  833. for (t = _dl_loaded_modules; t; t = t->next) {
  834. if (ref == NULL && t != tpnt) {
  835. ref = t;
  836. break;
  837. }
  838. }
  839. idx = tpnt->dsbt_size;
  840. while (idx-- > 0)
  841. if (!ref || ref->dsbt_table[idx] == NULL)
  842. break;
  843. if (idx <= 0) {
  844. _dl_dprintf(2, "%s: '%s' caused DSBT table overflow!\n",
  845. _dl_progname, libname);
  846. _dl_exit(1);
  847. }
  848. _dl_if_debug_dprint("\n\tfile='%s'; assigned index %d\n",
  849. libname, idx);
  850. tpnt->dsbt_index = idx;
  851. }
  852. /* make sure index is not already used */
  853. if (_dl_ldso_dsbt[idx]) {
  854. struct elf_resolve *dup;
  855. const char *dup_name;
  856. for (dup = _dl_loaded_modules; dup; dup = dup->next)
  857. if (dup != tpnt && dup->dsbt_index == idx)
  858. break;
  859. if (dup)
  860. dup_name = dup->libname;
  861. else if (idx == 1)
  862. dup_name = "runtime linker";
  863. else
  864. dup_name = "unknown library";
  865. _dl_dprintf(2, "%s: '%s' dsbt index %d already used by %s!\n",
  866. _dl_progname, libname, idx, dup_name);
  867. _dl_exit(1);
  868. }
  869. /*
  870. * Setup dsbt slot for this module in dsbt of all modules.
  871. */
  872. for (t = _dl_loaded_modules; t; t = t->next)
  873. t->dsbt_table[idx] = dsbt;
  874. _dl_ldso_dsbt[idx] = dsbt;
  875. _dl_memcpy(dsbt, _dl_ldso_dsbt,
  876. tpnt->dsbt_size * sizeof(tpnt->dsbt_table[0]));
  877. }
  878. #endif
  879. _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname);
  880. _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr, DL_LOADADDR_BASE(lib_loadaddr));
  881. _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
  882. DL_RELOC_ADDR(lib_loadaddr, epnt->e_entry), tpnt->ppnt, tpnt->n_phent);
  883. _dl_munmap(header, _dl_pagesize);
  884. return tpnt;
  885. }
  886. /* now_flag must be RTLD_NOW or zero */
  887. int _dl_fixup(struct dyn_elf *rpnt, struct r_scope_elem *scope, int now_flag)
  888. {
  889. int goof = 0;
  890. struct elf_resolve *tpnt;
  891. ElfW(Word) reloc_size, relative_count;
  892. ElfW(Addr) reloc_addr;
  893. if (rpnt->next)
  894. goof = _dl_fixup(rpnt->next, scope, now_flag);
  895. if (goof)
  896. return goof;
  897. tpnt = rpnt->dyn;
  898. if (!(tpnt->init_flag & RELOCS_DONE))
  899. _dl_if_debug_dprint("relocation processing: %s\n", tpnt->libname);
  900. if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
  901. _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
  902. _dl_progname, UNSUPPORTED_RELOC_STR);
  903. goof++;
  904. return goof;
  905. }
  906. reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
  907. /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
  908. range. Note that according to the ELF spec, this is completely legal! */
  909. #ifdef ELF_MACHINE_PLTREL_OVERLAP
  910. reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
  911. #endif
  912. if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
  913. !(tpnt->init_flag & RELOCS_DONE)) {
  914. reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
  915. relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
  916. if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
  917. reloc_size -= relative_count * sizeof(ELF_RELOC);
  918. #ifdef __LDSO_PRELINK_SUPPORT__
  919. if (tpnt->loadaddr || (!tpnt->dynamic_info[DT_GNU_PRELINKED_IDX]))
  920. #endif
  921. elf_machine_relative(tpnt->loadaddr, reloc_addr, relative_count);
  922. reloc_addr += relative_count * sizeof(ELF_RELOC);
  923. }
  924. goof += _dl_parse_relocation_information(rpnt, scope,
  925. reloc_addr,
  926. reloc_size);
  927. tpnt->init_flag |= RELOCS_DONE;
  928. }
  929. if (tpnt->dynamic_info[DT_BIND_NOW])
  930. now_flag = RTLD_NOW;
  931. if (tpnt->dynamic_info[DT_JMPREL] &&
  932. (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
  933. (now_flag && !(tpnt->rtld_flags & now_flag)))) {
  934. tpnt->rtld_flags |= now_flag;
  935. if (!(tpnt->rtld_flags & RTLD_NOW)) {
  936. _dl_parse_lazy_relocation_information(rpnt,
  937. tpnt->dynamic_info[DT_JMPREL],
  938. tpnt->dynamic_info [DT_PLTRELSZ]);
  939. } else {
  940. goof += _dl_parse_relocation_information(rpnt, scope,
  941. tpnt->dynamic_info[DT_JMPREL],
  942. tpnt->dynamic_info[DT_PLTRELSZ]);
  943. }
  944. tpnt->init_flag |= JMP_RELOCS_DONE;
  945. }
  946. #if 0
  947. /* _dl_add_to_slotinfo is called by init_tls() for initial DSO
  948. or by dlopen() for dynamically loaded DSO. */
  949. #if defined(USE_TLS) && USE_TLS
  950. /* Add object to slot information data if necessasy. */
  951. if (tpnt->l_tls_blocksize != 0 && tls_init_tp_called)
  952. _dl_add_to_slotinfo ((struct link_map *) tpnt);
  953. #endif
  954. #endif
  955. return goof;
  956. }
  957. #ifdef IS_IN_rtld
  958. /* Minimal printf which handles only %s, %d, and %x */
  959. void _dl_dprintf(int fd, const char *fmt, ...)
  960. {
  961. #if __WORDSIZE > 32
  962. long int num;
  963. #else
  964. int num;
  965. #endif
  966. va_list args;
  967. char *start, *ptr, *string;
  968. char *buf;
  969. if (!fmt)
  970. return;
  971. buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  972. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  973. if (_dl_mmap_check_error(buf)) {
  974. _dl_write(fd, "mmap of a spare page failed!\n", 29);
  975. _dl_exit(20);
  976. }
  977. start = ptr = buf;
  978. if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
  979. _dl_write(fd, "overflow\n", 11);
  980. _dl_exit(20);
  981. }
  982. _dl_strcpy(buf, fmt);
  983. va_start(args, fmt);
  984. while (start) {
  985. while (*ptr != '%' && *ptr) {
  986. ptr++;
  987. }
  988. if (*ptr == '%') {
  989. *ptr++ = '\0';
  990. _dl_write(fd, start, _dl_strlen(start));
  991. switch (*ptr++) {
  992. case 's':
  993. string = va_arg(args, char *);
  994. if (!string)
  995. _dl_write(fd, "(null)", 6);
  996. else
  997. _dl_write(fd, string, _dl_strlen(string));
  998. break;
  999. case 'i':
  1000. case 'd':
  1001. {
  1002. char tmp[22];
  1003. #if __WORDSIZE > 32
  1004. num = va_arg(args, long int);
  1005. #else
  1006. num = va_arg(args, int);
  1007. #endif
  1008. string = _dl_simple_ltoa(tmp, num);
  1009. _dl_write(fd, string, _dl_strlen(string));
  1010. break;
  1011. }
  1012. case 'x':
  1013. case 'p':
  1014. {
  1015. char tmp[22];
  1016. #if __WORDSIZE > 32
  1017. num = va_arg(args, long int);
  1018. #else
  1019. num = va_arg(args, int);
  1020. #endif
  1021. string = _dl_simple_ltoahex(tmp, num);
  1022. _dl_write(fd, string, _dl_strlen(string));
  1023. break;
  1024. }
  1025. default:
  1026. _dl_write(fd, "(null)", 6);
  1027. break;
  1028. }
  1029. start = ptr;
  1030. } else {
  1031. _dl_write(fd, start, _dl_strlen(start));
  1032. start = NULL;
  1033. }
  1034. }
  1035. _dl_munmap(buf, _dl_pagesize);
  1036. return;
  1037. }
  1038. char *_dl_strdup(const char *string)
  1039. {
  1040. char *retval;
  1041. int len;
  1042. len = _dl_strlen(string);
  1043. retval = _dl_malloc(len + 1);
  1044. _dl_strcpy(retval, string);
  1045. return retval;
  1046. }
  1047. #endif
  1048. unsigned int _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[],
  1049. void *debug_addr, DL_LOADADDR_TYPE load_off)
  1050. {
  1051. return __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
  1052. }