dl-elf.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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-2004 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 == (caddr_t) - 1)
  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, 0)) < 0) {
  48. _dl_cache_addr = (caddr_t) - 1; /* so we won't try again */
  49. return -1;
  50. }
  51. _dl_cache_size = st.st_size;
  52. _dl_cache_addr = (caddr_t) _dl_mmap(0, _dl_cache_size, PROT_READ, MAP_SHARED, fd, 0);
  53. _dl_close(fd);
  54. if (_dl_mmap_check_error(_dl_cache_addr)) {
  55. _dl_dprintf(2, "%s: can't map cache '%s'\n",
  56. _dl_progname, 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 = (caddr_t) - 1;
  86. return -1;
  87. }
  88. int _dl_unmap_cache(void)
  89. {
  90. if (_dl_cache_addr == NULL || _dl_cache_addr == (caddr_t) - 1)
  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) start = ((l->loadaddr + l->relro_addr)
  103. & ~(_dl_pagesize - 1));
  104. ElfW(Addr) end = ((l->loadaddr + l->relro_addr + l->relro_size)
  105. & ~(_dl_pagesize - 1));
  106. _dl_if_debug_dprint("RELRO protecting %s: start:%x, end:%x\n", l->libname, start, end);
  107. if (start != end &&
  108. _dl_mprotect ((void *) start, end - start, PROT_READ) < 0) {
  109. _dl_dprintf(2, "%s: cannot apply additional memory protection after relocation", l->libname);
  110. _dl_exit(0);
  111. }
  112. }
  113. /* This function's behavior must exactly match that
  114. * in uClibc/ldso/util/ldd.c */
  115. static struct elf_resolve *
  116. search_for_named_library(const char *name, int secure, const char *path_list,
  117. struct dyn_elf **rpnt)
  118. {
  119. char *path, *path_n;
  120. char mylibname[2050];
  121. struct elf_resolve *tpnt;
  122. int done = 0;
  123. if (path_list==NULL)
  124. return NULL;
  125. /* We need a writable copy of this string */
  126. path = _dl_strdup(path_list);
  127. if (!path) {
  128. _dl_dprintf(2, "Out of memory!\n");
  129. _dl_exit(0);
  130. }
  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. path_n = path;
  137. do {
  138. if (*path == 0) {
  139. *path = ':';
  140. done = 1;
  141. }
  142. if (*path == ':') {
  143. *path = 0;
  144. if (*path_n)
  145. _dl_strcpy(mylibname, path_n);
  146. else
  147. _dl_strcpy(mylibname, "."); /* Assume current dir if empty path */
  148. _dl_strcat(mylibname, "/");
  149. _dl_strcat(mylibname, name);
  150. if ((tpnt = _dl_load_elf_shared_library(secure, rpnt, mylibname)) != NULL)
  151. return tpnt;
  152. path_n = path+1;
  153. }
  154. path++;
  155. } while (!done);
  156. return NULL;
  157. }
  158. /* Used to return error codes back to dlopen et. al. */
  159. unsigned long _dl_error_number;
  160. unsigned long _dl_internal_error_number;
  161. struct elf_resolve *_dl_load_shared_library(int secure, struct dyn_elf **rpnt,
  162. struct elf_resolve *tpnt, char *full_libname, int __attribute__((unused)) trace_loaded_objects)
  163. {
  164. char *pnt;
  165. struct elf_resolve *tpnt1;
  166. char *libname;
  167. _dl_internal_error_number = 0;
  168. libname = full_libname;
  169. /* quick hack to ensure mylibname buffer doesn't overflow. don't
  170. allow full_libname or any directory to be longer than 1024. */
  171. if (_dl_strlen(full_libname) > 1024)
  172. goto goof;
  173. /* Skip over any initial initial './' and '/' stuff to
  174. * get the short form libname with no path garbage */
  175. pnt = _dl_strrchr(libname, '/');
  176. if (pnt) {
  177. libname = pnt + 1;
  178. }
  179. _dl_if_debug_dprint("\tfind library='%s'; searching\n", libname);
  180. /* If the filename has any '/', try it straight and leave it at that.
  181. For IBCS2 compatibility under linux, we substitute the string
  182. /usr/i486-sysv4/lib for /usr/lib in library names. */
  183. if (libname != full_libname) {
  184. _dl_if_debug_dprint("\ttrying file='%s'\n", full_libname);
  185. tpnt1 = _dl_load_elf_shared_library(secure, rpnt, full_libname);
  186. if (tpnt1) {
  187. return tpnt1;
  188. }
  189. }
  190. /*
  191. * The ABI specifies that RPATH is searched before LD_LIBRARY_PATH or
  192. * the default path of /usr/lib. Check in rpath directories.
  193. */
  194. #ifdef __LDSO_RUNPATH__
  195. pnt = (tpnt ? (char *) tpnt->dynamic_info[DT_RPATH] : NULL);
  196. if (pnt) {
  197. pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
  198. _dl_if_debug_dprint("\tsearching RPATH='%s'\n", pnt);
  199. if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
  200. return tpnt1;
  201. }
  202. #endif
  203. /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
  204. if (_dl_library_path) {
  205. _dl_if_debug_dprint("\tsearching LD_LIBRARY_PATH='%s'\n", _dl_library_path);
  206. if ((tpnt1 = search_for_named_library(libname, secure, _dl_library_path, rpnt)) != NULL)
  207. {
  208. return tpnt1;
  209. }
  210. }
  211. /*
  212. * The ABI specifies that RUNPATH is searched after LD_LIBRARY_PATH.
  213. */
  214. #ifdef __LDSO_RUNPATH__
  215. pnt = (tpnt ? (char *)tpnt->dynamic_info[DT_RUNPATH] : NULL);
  216. if (pnt) {
  217. pnt += (unsigned long) tpnt->dynamic_info[DT_STRTAB];
  218. _dl_if_debug_dprint("\tsearching RUNPATH='%s'\n", pnt);
  219. if ((tpnt1 = search_for_named_library(libname, secure, pnt, rpnt)) != NULL)
  220. return tpnt1;
  221. }
  222. #endif
  223. /*
  224. * Where should the cache be searched? There is no such concept in the
  225. * ABI, so we have some flexibility here. For now, search it before
  226. * the hard coded paths that follow (i.e before /lib and /usr/lib).
  227. */
  228. #ifdef __LDSO_CACHE_SUPPORT__
  229. if (_dl_cache_addr != NULL && _dl_cache_addr != (caddr_t) - 1) {
  230. int i;
  231. header_t *header = (header_t *) _dl_cache_addr;
  232. libentry_t *libent = (libentry_t *) & header[1];
  233. char *strs = (char *) &libent[header->nlibs];
  234. _dl_if_debug_dprint("\tsearching cache='%s'\n", LDSO_CACHE);
  235. for (i = 0; i < header->nlibs; i++) {
  236. if ((libent[i].flags == LIB_ELF ||
  237. libent[i].flags == LIB_ELF_LIBC0 ||
  238. libent[i].flags == LIB_ELF_LIBC5) &&
  239. _dl_strcmp(libname, strs + libent[i].sooffset) == 0 &&
  240. (tpnt1 = _dl_load_elf_shared_library(secure,
  241. rpnt, strs + libent[i].liboffset)))
  242. return tpnt1;
  243. }
  244. }
  245. #endif
  246. /* Look for libraries wherever the shared library loader
  247. * was installed */
  248. _dl_if_debug_dprint("\tsearching ldso dir='%s'\n", _dl_ldsopath);
  249. if ((tpnt1 = search_for_named_library(libname, secure, _dl_ldsopath, rpnt)) != NULL)
  250. {
  251. return tpnt1;
  252. }
  253. /* Lastly, search the standard list of paths for the library.
  254. This list must exactly match the list in uClibc/ldso/util/ldd.c */
  255. _dl_if_debug_dprint("\tsearching full lib path list\n");
  256. if ((tpnt1 = search_for_named_library(libname, secure,
  257. UCLIBC_RUNTIME_PREFIX "lib:"
  258. UCLIBC_RUNTIME_PREFIX "usr/lib"
  259. #ifndef __LDSO_CACHE_SUPPORT__
  260. ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib"
  261. #endif
  262. , rpnt)
  263. ) != NULL)
  264. {
  265. return tpnt1;
  266. }
  267. goof:
  268. /* Well, we shot our wad on that one. All we can do now is punt */
  269. if (_dl_internal_error_number)
  270. _dl_error_number = _dl_internal_error_number;
  271. else
  272. _dl_error_number = LD_ERROR_NOFILE;
  273. _dl_if_debug_dprint("Bummer: could not find '%s'!\n", libname);
  274. return NULL;
  275. }
  276. /*
  277. * Read one ELF library into memory, mmap it into the correct locations and
  278. * add the symbol info to the symbol chain. Perform any relocations that
  279. * are required.
  280. */
  281. struct elf_resolve *_dl_load_elf_shared_library(int secure,
  282. struct dyn_elf **rpnt, char *libname)
  283. {
  284. ElfW(Ehdr) *epnt;
  285. unsigned long dynamic_addr = 0;
  286. ElfW(Dyn) *dpnt;
  287. struct elf_resolve *tpnt;
  288. ElfW(Phdr) *ppnt;
  289. char *status, *header;
  290. unsigned long dynamic_info[DYNAMIC_SIZE];
  291. unsigned long *lpnt;
  292. unsigned long libaddr;
  293. unsigned long minvma = 0xffffffff, maxvma = 0;
  294. int i, flags, piclib, infile;
  295. ElfW(Addr) relro_addr = 0;
  296. size_t relro_size = 0;
  297. struct stat st;
  298. libaddr = 0;
  299. infile = _dl_open(libname, O_RDONLY, 0);
  300. if (infile < 0) {
  301. _dl_internal_error_number = LD_ERROR_NOFILE;
  302. return NULL;
  303. }
  304. if (_dl_fstat(infile, &st) < 0) {
  305. _dl_internal_error_number = LD_ERROR_NOFILE;
  306. _dl_close(infile);
  307. return NULL;
  308. }
  309. /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
  310. we don't load the library if it isn't setuid. */
  311. if (secure)
  312. if (!(st.st_mode & S_ISUID)) {
  313. _dl_close(infile);
  314. return NULL;
  315. }
  316. /* Check if file is already loaded */
  317. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  318. if(tpnt->st_dev == st.st_dev && tpnt->st_ino == st.st_ino) {
  319. /* Already loaded */
  320. tpnt->usage_count++;
  321. _dl_close(infile);
  322. return tpnt;
  323. }
  324. }
  325. header = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  326. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  327. if (_dl_mmap_check_error(header)) {
  328. _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
  329. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  330. _dl_close(infile);
  331. return NULL;
  332. };
  333. _dl_read(infile, header, _dl_pagesize);
  334. epnt = (ElfW(Ehdr) *) (intptr_t) header;
  335. if (epnt->e_ident[0] != 0x7f ||
  336. epnt->e_ident[1] != 'E' ||
  337. epnt->e_ident[2] != 'L' ||
  338. epnt->e_ident[3] != 'F')
  339. {
  340. _dl_dprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  341. libname);
  342. _dl_internal_error_number = LD_ERROR_NOTELF;
  343. _dl_close(infile);
  344. _dl_munmap(header, _dl_pagesize);
  345. return NULL;
  346. };
  347. if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
  348. #ifdef MAGIC2
  349. && epnt->e_machine != MAGIC2
  350. #endif
  351. ))
  352. {
  353. _dl_internal_error_number =
  354. (epnt->e_type != ET_DYN ? LD_ERROR_NOTDYN : LD_ERROR_NOTMAGIC);
  355. _dl_dprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  356. "\n", _dl_progname, libname);
  357. _dl_close(infile);
  358. _dl_munmap(header, _dl_pagesize);
  359. return NULL;
  360. };
  361. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  362. piclib = 1;
  363. for (i = 0; i < epnt->e_phnum; i++) {
  364. if (ppnt->p_type == PT_DYNAMIC) {
  365. if (dynamic_addr)
  366. _dl_dprintf(2, "%s: '%s' has more than one dynamic section\n",
  367. _dl_progname, libname);
  368. dynamic_addr = ppnt->p_vaddr;
  369. };
  370. if (ppnt->p_type == PT_LOAD) {
  371. /* See if this is a PIC library. */
  372. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  373. piclib = 0;
  374. minvma = ppnt->p_vaddr;
  375. }
  376. if (piclib && ppnt->p_vaddr < minvma) {
  377. minvma = ppnt->p_vaddr;
  378. }
  379. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  380. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  381. }
  382. }
  383. ppnt++;
  384. };
  385. maxvma = (maxvma + ADDR_ALIGN) & ~ADDR_ALIGN;
  386. minvma = minvma & ~0xffffU;
  387. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  388. if (!piclib)
  389. flags |= MAP_FIXED;
  390. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  391. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  392. if (_dl_mmap_check_error(status)) {
  393. _dl_dprintf(2, "%s: can't map %s\n", _dl_progname, libname);
  394. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  395. _dl_close(infile);
  396. _dl_munmap(header, _dl_pagesize);
  397. return NULL;
  398. };
  399. libaddr = (unsigned long) status;
  400. flags |= MAP_FIXED;
  401. /* Get the memory to store the library */
  402. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  403. for (i = 0; i < epnt->e_phnum; i++) {
  404. if (ppnt->p_type == PT_GNU_RELRO) {
  405. relro_addr = ppnt->p_vaddr;
  406. relro_size = ppnt->p_memsz;
  407. }
  408. if (ppnt->p_type == PT_LOAD) {
  409. /* See if this is a PIC library. */
  410. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  411. piclib = 0;
  412. /* flags |= MAP_FIXED; */
  413. }
  414. if (ppnt->p_flags & PF_W) {
  415. unsigned long map_size;
  416. char *cpnt;
  417. status = (char *) _dl_mmap((char *) ((piclib ? libaddr : 0) +
  418. (ppnt->p_vaddr & PAGE_ALIGN)), (ppnt->p_vaddr & ADDR_ALIGN)
  419. + ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags, infile,
  420. ppnt->p_offset & OFFS_ALIGN);
  421. if (_dl_mmap_check_error(status)) {
  422. _dl_dprintf(2, "%s: can't map '%s'\n",
  423. _dl_progname, libname);
  424. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  425. _dl_munmap((char *) libaddr, maxvma - minvma);
  426. _dl_close(infile);
  427. _dl_munmap(header, _dl_pagesize);
  428. return NULL;
  429. };
  430. /* Pad the last page with zeroes. */
  431. cpnt = (char *) (status + (ppnt->p_vaddr & ADDR_ALIGN) +
  432. ppnt->p_filesz);
  433. while (((unsigned long) cpnt) & ADDR_ALIGN)
  434. *cpnt++ = 0;
  435. /* I am not quite sure if this is completely
  436. * correct to do or not, but the basic way that
  437. * we handle bss segments is that we mmap
  438. * /dev/zero if there are any pages left over
  439. * that are not mapped as part of the file */
  440. map_size = (ppnt->p_vaddr + ppnt->p_filesz + ADDR_ALIGN) & PAGE_ALIGN;
  441. if (map_size < ppnt->p_vaddr + ppnt->p_memsz)
  442. status = (char *) _dl_mmap((char *) map_size +
  443. (piclib ? libaddr : 0),
  444. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  445. LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS, -1, 0);
  446. } else
  447. status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & PAGE_ALIGN)
  448. + (piclib ? libaddr : 0), (ppnt->p_vaddr & ADDR_ALIGN) +
  449. ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags,
  450. infile, ppnt->p_offset & OFFS_ALIGN);
  451. if (_dl_mmap_check_error(status)) {
  452. _dl_dprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
  453. _dl_internal_error_number = LD_ERROR_MMAP_FAILED;
  454. _dl_munmap((char *) libaddr, maxvma - minvma);
  455. _dl_close(infile);
  456. _dl_munmap(header, _dl_pagesize);
  457. return NULL;
  458. };
  459. /* if(libaddr == 0 && piclib) {
  460. libaddr = (unsigned long) status;
  461. flags |= MAP_FIXED;
  462. }; */
  463. };
  464. ppnt++;
  465. };
  466. _dl_close(infile);
  467. /* For a non-PIC library, the addresses are all absolute */
  468. if (piclib) {
  469. dynamic_addr += (unsigned long) libaddr;
  470. }
  471. /*
  472. * OK, the ELF library is now loaded into VM in the correct locations
  473. * The next step is to go through and do the dynamic linking (if needed).
  474. */
  475. /* Start by scanning the dynamic section to get all of the pointers */
  476. if (!dynamic_addr) {
  477. _dl_internal_error_number = LD_ERROR_NODYNAMIC;
  478. _dl_dprintf(2, "%s: '%s' is missing a dynamic section\n",
  479. _dl_progname, libname);
  480. _dl_munmap(header, _dl_pagesize);
  481. return NULL;
  482. }
  483. dpnt = (ElfW(Dyn) *) dynamic_addr;
  484. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  485. _dl_parse_dynamic_info(dpnt, dynamic_info, NULL, libaddr);
  486. /* If the TEXTREL is set, this means that we need to make the pages
  487. writable before we perform relocations. Do this now. They get set
  488. back again later. */
  489. if (dynamic_info[DT_TEXTREL]) {
  490. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  491. ppnt = (ElfW(Phdr) *)(intptr_t) & header[epnt->e_phoff];
  492. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  493. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  494. _dl_mprotect((void *) ((piclib ? libaddr : 0) +
  495. (ppnt->p_vaddr & PAGE_ALIGN)),
  496. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  497. PROT_READ | PROT_WRITE | PROT_EXEC);
  498. }
  499. #else
  500. _dl_dprintf(_dl_debug_file, "Can't modify %s's text section. Use GCC option -fPIC for shared objects, please.\n",libname);
  501. _dl_exit(1);
  502. #endif
  503. }
  504. tpnt = _dl_add_elf_hash_table(libname, (char *) libaddr, dynamic_info,
  505. dynamic_addr, 0);
  506. tpnt->relro_addr = relro_addr;
  507. tpnt->relro_size = relro_size;
  508. tpnt->st_dev = st.st_dev;
  509. tpnt->st_ino = st.st_ino;
  510. tpnt->ppnt = (ElfW(Phdr) *)(intptr_t) (tpnt->loadaddr + epnt->e_phoff);
  511. tpnt->n_phent = epnt->e_phnum;
  512. /*
  513. * Add this object into the symbol chain
  514. */
  515. if (*rpnt) {
  516. (*rpnt)->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  517. _dl_memset((*rpnt)->next, 0, sizeof(struct dyn_elf));
  518. (*rpnt)->next->prev = (*rpnt);
  519. *rpnt = (*rpnt)->next;
  520. (*rpnt)->dyn = tpnt;
  521. tpnt->symbol_scope = _dl_symbol_tables;
  522. }
  523. tpnt->usage_count++;
  524. tpnt->libtype = elf_lib;
  525. /*
  526. * OK, the next thing we need to do is to insert the dynamic linker into
  527. * the proper entry in the GOT so that the PLT symbols can be properly
  528. * resolved.
  529. */
  530. lpnt = (unsigned long *) dynamic_info[DT_PLTGOT];
  531. if (lpnt) {
  532. lpnt = (unsigned long *) (dynamic_info[DT_PLTGOT]);
  533. INIT_GOT(lpnt, tpnt);
  534. }
  535. _dl_if_debug_dprint("\n\tfile='%s'; generating link map\n", libname);
  536. _dl_if_debug_dprint("\t\tdynamic: %x base: %x\n", dynamic_addr, libaddr);
  537. _dl_if_debug_dprint("\t\t entry: %x phdr: %x phnum: %x\n\n",
  538. epnt->e_entry + libaddr, tpnt->ppnt, tpnt->n_phent);
  539. _dl_munmap(header, _dl_pagesize);
  540. return tpnt;
  541. }
  542. /* now_flag must be RTLD_NOW or zero */
  543. int _dl_fixup(struct dyn_elf *rpnt, int now_flag)
  544. {
  545. int goof = 0;
  546. struct elf_resolve *tpnt;
  547. ElfW(Word) reloc_size, relative_count;
  548. ElfW(Addr) reloc_addr;
  549. if (rpnt->next)
  550. goof = _dl_fixup(rpnt->next, now_flag);
  551. if (goof)
  552. return goof;
  553. tpnt = rpnt->dyn;
  554. if(!(tpnt->init_flag & RELOCS_DONE))
  555. _dl_if_debug_dprint("relocation processing: %s\n", tpnt->libname);
  556. if (unlikely(tpnt->dynamic_info[UNSUPPORTED_RELOC_TYPE])) {
  557. _dl_if_debug_dprint("%s: can't handle %s relocation records\n",
  558. _dl_progname, UNSUPPORTED_RELOC_STR);
  559. goof++;
  560. return goof;
  561. }
  562. reloc_size = tpnt->dynamic_info[DT_RELOC_TABLE_SIZE];
  563. /* On some machines, notably SPARC & PPC, DT_REL* includes DT_JMPREL in its
  564. range. Note that according to the ELF spec, this is completely legal! */
  565. #ifdef ELF_MACHINE_PLTREL_OVERLAP
  566. reloc_size -= tpnt->dynamic_info [DT_PLTRELSZ];
  567. #endif
  568. if (tpnt->dynamic_info[DT_RELOC_TABLE_ADDR] &&
  569. !(tpnt->init_flag & RELOCS_DONE)) {
  570. reloc_addr = tpnt->dynamic_info[DT_RELOC_TABLE_ADDR];
  571. relative_count = tpnt->dynamic_info[DT_RELCONT_IDX];
  572. if (relative_count) { /* Optimize the XX_RELATIVE relocations if possible */
  573. reloc_size -= relative_count * sizeof(ELF_RELOC);
  574. elf_machine_relative(tpnt->loadaddr, reloc_addr, relative_count);
  575. reloc_addr += relative_count * sizeof(ELF_RELOC);
  576. }
  577. goof += _dl_parse_relocation_information(rpnt,
  578. reloc_addr,
  579. reloc_size);
  580. tpnt->init_flag |= RELOCS_DONE;
  581. }
  582. if (tpnt->dynamic_info[DT_BIND_NOW])
  583. now_flag = RTLD_NOW;
  584. if (tpnt->dynamic_info[DT_JMPREL] &&
  585. (!(tpnt->init_flag & JMP_RELOCS_DONE) ||
  586. (now_flag && !(tpnt->rtld_flags & now_flag)))) {
  587. tpnt->rtld_flags |= now_flag;
  588. if (!(tpnt->rtld_flags & RTLD_NOW)) {
  589. _dl_parse_lazy_relocation_information(rpnt,
  590. tpnt->dynamic_info[DT_JMPREL],
  591. tpnt->dynamic_info [DT_PLTRELSZ]);
  592. } else {
  593. goof += _dl_parse_relocation_information(rpnt,
  594. tpnt->dynamic_info[DT_JMPREL],
  595. tpnt->dynamic_info[DT_PLTRELSZ]);
  596. }
  597. tpnt->init_flag |= JMP_RELOCS_DONE;
  598. }
  599. return goof;
  600. }
  601. /* Minimal printf which handles only %s, %d, and %x */
  602. void _dl_dprintf(int fd, const char *fmt, ...)
  603. {
  604. #if __WORDSIZE > 32
  605. long int num;
  606. #else
  607. int num;
  608. #endif
  609. va_list args;
  610. char *start, *ptr, *string;
  611. static char *buf;
  612. buf = _dl_mmap((void *) 0, _dl_pagesize, PROT_READ | PROT_WRITE,
  613. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  614. if (_dl_mmap_check_error(buf)) {
  615. _dl_write(fd, "mmap of a spare page failed!\n", 29);
  616. _dl_exit(20);
  617. }
  618. start = ptr = buf;
  619. if (!fmt)
  620. return;
  621. if (_dl_strlen(fmt) >= (_dl_pagesize - 1)) {
  622. _dl_write(fd, "overflow\n", 11);
  623. _dl_exit(20);
  624. }
  625. _dl_strcpy(buf, fmt);
  626. va_start(args, fmt);
  627. while (start) {
  628. while (*ptr != '%' && *ptr) {
  629. ptr++;
  630. }
  631. if (*ptr == '%') {
  632. *ptr++ = '\0';
  633. _dl_write(fd, start, _dl_strlen(start));
  634. switch (*ptr++) {
  635. case 's':
  636. string = va_arg(args, char *);
  637. if (!string)
  638. _dl_write(fd, "(null)", 6);
  639. else
  640. _dl_write(fd, string, _dl_strlen(string));
  641. break;
  642. case 'i':
  643. case 'd':
  644. {
  645. char tmp[22];
  646. #if __WORDSIZE > 32
  647. num = va_arg(args, long int);
  648. #else
  649. num = va_arg(args, int);
  650. #endif
  651. string = _dl_simple_ltoa(tmp, num);
  652. _dl_write(fd, string, _dl_strlen(string));
  653. break;
  654. }
  655. case 'x':
  656. case 'X':
  657. {
  658. char tmp[22];
  659. #if __WORDSIZE > 32
  660. num = va_arg(args, long int);
  661. #else
  662. num = va_arg(args, int);
  663. #endif
  664. string = _dl_simple_ltoahex(tmp, num);
  665. _dl_write(fd, string, _dl_strlen(string));
  666. break;
  667. }
  668. default:
  669. _dl_write(fd, "(null)", 6);
  670. break;
  671. }
  672. start = ptr;
  673. } else {
  674. _dl_write(fd, start, _dl_strlen(start));
  675. start = NULL;
  676. }
  677. }
  678. _dl_munmap(buf, _dl_pagesize);
  679. return;
  680. }
  681. char *_dl_strdup(const char *string)
  682. {
  683. char *retval;
  684. int len;
  685. len = _dl_strlen(string);
  686. retval = _dl_malloc(len + 1);
  687. _dl_strcpy(retval, string);
  688. return retval;
  689. }
  690. void _dl_parse_dynamic_info(ElfW(Dyn) *dpnt, unsigned long dynamic_info[], void *debug_addr, ElfW(Addr) load_off)
  691. {
  692. __dl_parse_dynamic_info(dpnt, dynamic_info, debug_addr, load_off);
  693. }
  694. /* we want this in ldso.so and libdl.a but nowhere else */
  695. #ifdef __USE_GNU
  696. #if defined IS_IN_rtld || (defined IS_IN_libdl && ! defined SHARED)
  697. int
  698. __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info, size_t size, void *data), void *data)
  699. {
  700. struct elf_resolve *l;
  701. struct dl_phdr_info info;
  702. int ret = 0;
  703. for (l = _dl_loaded_modules; l != NULL; l = l->next) {
  704. info.dlpi_addr = l->loadaddr;
  705. info.dlpi_name = l->libname;
  706. info.dlpi_phdr = l->ppnt;
  707. info.dlpi_phnum = l->n_phent;
  708. ret = callback (&info, sizeof (struct dl_phdr_info), data);
  709. if (ret)
  710. break;
  711. }
  712. return ret;
  713. }
  714. strong_alias(__dl_iterate_phdr, dl_iterate_phdr);
  715. #endif
  716. #endif