dl-elf.c 34 KB

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