dl-elf.c 33 KB

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