dl-elf.c 34 KB

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