dl-elf.c 34 KB

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