dl-elf.c 25 KB

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