dl-elf.c 34 KB

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