dl-elf.c 32 KB

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