dl-elf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /* Load an ELF sharable library into memory.
  2. Copyright (C) 1993-1996, Eric Youngdale.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  14. /* This file contains the helper routines to load an ELF sharable
  15. library into memory and add the symbol table info to the chain. */
  16. #include <asm/mman.h>
  17. #include "elf.h"
  18. #include "string.h"
  19. #include "hash.h"
  20. #include "sysdep.h"
  21. #include "syscall.h"
  22. #ifdef USE_CACHE
  23. #include "../config.h"
  24. #endif
  25. extern char *_dl_progname;
  26. #ifdef USE_CACHE
  27. static caddr_t _dl_cache_addr = NULL;
  28. static size_t _dl_cache_size = 0;
  29. int _dl_map_cache(void)
  30. {
  31. int fd;
  32. struct kernel_stat st;
  33. header_t *header;
  34. libentry_t *libent;
  35. int i, strtabsize;
  36. if (_dl_cache_addr == (caddr_t) - 1)
  37. return -1;
  38. else if (_dl_cache_addr != NULL)
  39. return 0;
  40. if (_dl_stat(LDSO_CACHE, &st)
  41. || (fd = _dl_open(LDSO_CACHE, O_RDONLY)) < 0) {
  42. _dl_fdprintf(2, "%s: can't open cache '%s'\n", _dl_progname, LDSO_CACHE);
  43. _dl_cache_addr = (caddr_t) - 1; /* so we won't try again */
  44. return -1;
  45. }
  46. _dl_cache_size = st.st_size;
  47. _dl_cache_addr = (caddr_t) _dl_mmap(0, _dl_cache_size, PROT_READ, MAP_SHARED, fd, 0);
  48. _dl_close(fd);
  49. if (_dl_cache_addr == (caddr_t) - 1) {
  50. _dl_fdprintf(2, "%s: can't map cache '%s'\n",
  51. _dl_progname, LDSO_CACHE);
  52. return -1;
  53. }
  54. header = (header_t *) _dl_cache_addr;
  55. if (_dl_cache_size < sizeof(header_t) ||
  56. _dl_memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN)
  57. || _dl_memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN)
  58. || _dl_cache_size <
  59. (sizeof(header_t) + header->nlibs * sizeof(libentry_t))
  60. || _dl_cache_addr[_dl_cache_size - 1] != '\0')
  61. {
  62. _dl_fdprintf(2, "%s: cache '%s' is corrupt\n", _dl_progname,
  63. LDSO_CACHE);
  64. goto fail;
  65. }
  66. strtabsize = _dl_cache_size - sizeof(header_t) -
  67. header->nlibs * sizeof(libentry_t);
  68. libent = (libentry_t *) & header[1];
  69. for (i = 0; i < header->nlibs; i++) {
  70. if (libent[i].sooffset >= strtabsize ||
  71. libent[i].liboffset >= strtabsize)
  72. {
  73. _dl_fdprintf(2, "%s: cache '%s' is corrupt\n", _dl_progname, LDSO_CACHE);
  74. goto fail;
  75. }
  76. }
  77. return 0;
  78. fail:
  79. _dl_munmap(_dl_cache_addr, _dl_cache_size);
  80. _dl_cache_addr = (caddr_t) - 1;
  81. return -1;
  82. }
  83. int _dl_unmap_cache(void)
  84. {
  85. if (_dl_cache_addr == NULL || _dl_cache_addr == (caddr_t) - 1)
  86. return -1;
  87. #if 1
  88. _dl_munmap(_dl_cache_addr, _dl_cache_size);
  89. _dl_cache_addr = NULL;
  90. #endif
  91. return 0;
  92. }
  93. #endif
  94. /*
  95. * Used to return error codes back to dlopen et. al.
  96. */
  97. unsigned long _dl_error_number;
  98. unsigned long _dl_internal_error_number;
  99. struct elf_resolve *_dl_load_shared_library(int secure,
  100. struct elf_resolve *tpnt, char *full_libname)
  101. {
  102. char *pnt, *pnt1, *pnt2;
  103. struct elf_resolve *tpnt1 = NULL;
  104. char mylibname[2050];
  105. char *libname;
  106. _dl_internal_error_number = 0;
  107. /* quick hack to ensure mylibname buffer doesn't overflow. don't
  108. allow full_libname or any directory to be longer than 1024. */
  109. if (_dl_strlen(full_libname) > 1024)
  110. goto goof;
  111. pnt = libname = full_libname;
  112. while (*pnt) {
  113. if (*pnt == '/')
  114. libname = pnt + 1;
  115. pnt++;
  116. }
  117. /* If the filename has any '/', try it straight and leave it at that.
  118. For IBCS2 compatibility under linux, we substitute the string
  119. /usr/i486-sysv4/lib for /usr/lib in library names. */
  120. if (libname != full_libname) {
  121. tpnt1 = _dl_load_elf_shared_library(secure, full_libname, 0);
  122. if (tpnt1)
  123. return tpnt1;
  124. goto goof;
  125. }
  126. /*
  127. * The ABI specifies that RPATH is searched before LD_*_PATH or
  128. * the default path of /usr/lib.
  129. * Check in rpath directories
  130. */
  131. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  132. if (tpnt->libtype == elf_executable) {
  133. pnt1 = (char *) tpnt->dynamic_info[DT_RPATH];
  134. if (pnt1) {
  135. pnt1 += (unsigned long) tpnt->loadaddr +
  136. tpnt->dynamic_info[DT_STRTAB];
  137. while (*pnt1) {
  138. pnt2 = mylibname;
  139. while (*pnt1 && *pnt1 != ':') {
  140. if (pnt2 - mylibname < 1024)
  141. *pnt2++ = *pnt1++;
  142. else
  143. pnt1++;
  144. }
  145. if (pnt2 - mylibname >= 1024)
  146. break;
  147. if (pnt2[-1] != '/')
  148. *pnt2++ = '/';
  149. pnt = libname;
  150. while (*pnt)
  151. *pnt2++ = *pnt++;
  152. *pnt2++ = 0;
  153. tpnt1 =
  154. _dl_load_elf_shared_library(secure, mylibname, 0);
  155. if (tpnt1)
  156. return tpnt1;
  157. if (*pnt1 == ':')
  158. pnt1++;
  159. }
  160. }
  161. }
  162. }
  163. /* Check in LD_{ELF_}LIBRARY_PATH, if specified and allowed */
  164. pnt1 = _dl_library_path;
  165. if (pnt1 && *pnt1) {
  166. while (*pnt1) {
  167. pnt2 = mylibname;
  168. while (*pnt1 && *pnt1 != ':' && *pnt1 != ';') {
  169. if (pnt2 - mylibname < 1024)
  170. *pnt2++ = *pnt1++;
  171. else
  172. pnt1++;
  173. }
  174. if (pnt2 - mylibname >= 1024)
  175. break;
  176. if (pnt2[-1] != '/')
  177. *pnt2++ = '/';
  178. pnt = libname;
  179. while (*pnt)
  180. *pnt2++ = *pnt++;
  181. *pnt2++ = 0;
  182. tpnt1 = _dl_load_elf_shared_library(secure, mylibname, 0);
  183. if (tpnt1)
  184. return tpnt1;
  185. if (*pnt1 == ':' || *pnt1 == ';')
  186. pnt1++;
  187. }
  188. }
  189. /*
  190. * Where should the cache be searched? There is no such concept in the
  191. * ABI, so we have some flexibility here. For now, search it before
  192. * the default path of /usr/lib.
  193. */
  194. #ifdef USE_CACHE
  195. if (_dl_cache_addr != NULL && _dl_cache_addr != (caddr_t) - 1) {
  196. int i;
  197. header_t *header = (header_t *) _dl_cache_addr;
  198. libentry_t *libent = (libentry_t *) & header[1];
  199. char *strs = (char *) &libent[header->nlibs];
  200. for (i = 0; i < header->nlibs; i++) {
  201. if ((libent[i].flags == LIB_ELF ||
  202. libent[i].flags == LIB_ELF_LIBC5) &&
  203. _dl_strcmp(libname, strs + libent[i].sooffset) == 0 &&
  204. (tpnt1 = _dl_load_elf_shared_library(secure,
  205. strs + libent[i].liboffset, 0)))
  206. return tpnt1;
  207. }
  208. }
  209. #endif
  210. #ifdef UCLIBC_DEVEL
  211. /* Check in /usr/<arch>-linux-uclibc/lib */
  212. pnt1 = UCLIBC_INSTALL_DIR "/lib";
  213. pnt = mylibname;
  214. while (*pnt1)
  215. *pnt++ = *pnt1++;
  216. pnt1 = libname;
  217. while (*pnt1)
  218. *pnt++ = *pnt1++;
  219. *pnt++ = 0;
  220. tpnt1 = _dl_load_elf_shared_library(secure, mylibname, 0);
  221. if (tpnt1)
  222. return tpnt1;
  223. #else /* UCLIBC_DEVEL */
  224. /* Check in /usr/lib */
  225. pnt1 = "/usr/lib/";
  226. pnt = mylibname;
  227. while (*pnt1)
  228. *pnt++ = *pnt1++;
  229. pnt1 = libname;
  230. while (*pnt1)
  231. *pnt++ = *pnt1++;
  232. *pnt++ = 0;
  233. tpnt1 = _dl_load_elf_shared_library(secure, mylibname, 0);
  234. if (tpnt1)
  235. return tpnt1;
  236. /* Check in /lib */
  237. /* try "/lib/". */
  238. pnt1 = "/lib/";
  239. pnt = mylibname;
  240. while (*pnt1)
  241. *pnt++ = *pnt1++;
  242. pnt1 = libname;
  243. while (*pnt1)
  244. *pnt++ = *pnt1++;
  245. *pnt++ = 0;
  246. tpnt1 = _dl_load_elf_shared_library(secure, mylibname, 0);
  247. if (tpnt1)
  248. return tpnt1;
  249. #endif /* UCLIBC_DEVEL */
  250. goof:
  251. /* Well, we shot our wad on that one. All we can do now is punt */
  252. if (_dl_internal_error_number)
  253. _dl_error_number = _dl_internal_error_number;
  254. else
  255. _dl_error_number = DL_ERROR_NOFILE;
  256. return NULL;
  257. }
  258. /*
  259. * Read one ELF library into memory, mmap it into the correct locations and
  260. * add the symbol info to the symbol chain. Perform any relocations that
  261. * are required.
  262. */
  263. //extern _elf_rtbndr(void);
  264. struct elf_resolve *_dl_load_elf_shared_library(int secure,
  265. char *libname, int flag)
  266. {
  267. elfhdr *epnt;
  268. unsigned long dynamic_addr = 0;
  269. unsigned long dynamic_size = 0;
  270. Elf32_Dyn *dpnt;
  271. struct elf_resolve *tpnt;
  272. elf_phdr *ppnt;
  273. int piclib;
  274. char *status;
  275. int flags;
  276. char header[4096];
  277. unsigned long dynamic_info[24];
  278. int *lpnt;
  279. unsigned long libaddr;
  280. unsigned long minvma = 0xffffffff, maxvma = 0;
  281. int i;
  282. int infile;
  283. /* If this file is already loaded, skip this step */
  284. tpnt = _dl_check_hashed_files(libname);
  285. if (tpnt)
  286. return tpnt;
  287. /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
  288. we don't load the library if it isn't setuid. */
  289. if (secure) {
  290. struct kernel_stat st;
  291. if (_dl_stat(libname, &st) || !(st.st_mode & S_ISUID))
  292. return NULL;
  293. }
  294. libaddr = 0;
  295. infile = _dl_open(libname, O_RDONLY);
  296. if (infile < 0) {
  297. #if 0
  298. /*
  299. * NO! When we open shared libraries we may search several paths.
  300. * it is inappropriate to generate an error here.
  301. */
  302. _dl_fdprintf(2, "%s: can't open '%s'\n", _dl_progname, libname);
  303. #endif
  304. _dl_internal_error_number = DL_ERROR_NOFILE;
  305. return NULL;
  306. }
  307. _dl_read(infile, header, sizeof(header));
  308. epnt = (elfhdr *) header;
  309. if (epnt->e_ident[0] != 0x7f ||
  310. epnt->e_ident[1] != 'E' ||
  311. epnt->e_ident[2] != 'L' ||
  312. epnt->e_ident[3] != 'F')
  313. {
  314. _dl_fdprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  315. libname);
  316. _dl_internal_error_number = DL_ERROR_NOTELF;
  317. _dl_close(infile);
  318. return NULL;
  319. };
  320. if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
  321. #ifdef MAGIC2
  322. && epnt->e_machine != MAGIC2
  323. #endif
  324. ))
  325. {
  326. _dl_internal_error_number =
  327. (epnt->e_type != ET_DYN ? DL_ERROR_NOTDYN : DL_ERROR_NOTMAGIC);
  328. _dl_fdprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  329. "\n", _dl_progname, libname);
  330. _dl_close(infile);
  331. return NULL;
  332. };
  333. ppnt = (elf_phdr *) & header[epnt->e_phoff];
  334. piclib = 1;
  335. for (i = 0; i < epnt->e_phnum; i++) {
  336. if (ppnt->p_type == PT_DYNAMIC) {
  337. if (dynamic_addr)
  338. _dl_fdprintf(2, "%s: '%s' has more than one dynamic section\n",
  339. _dl_progname, libname);
  340. dynamic_addr = ppnt->p_vaddr;
  341. dynamic_size = ppnt->p_filesz;
  342. };
  343. if (ppnt->p_type == PT_LOAD) {
  344. /* See if this is a PIC library. */
  345. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  346. piclib = 0;
  347. minvma = ppnt->p_vaddr;
  348. }
  349. if (piclib && ppnt->p_vaddr < minvma) {
  350. minvma = ppnt->p_vaddr;
  351. }
  352. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  353. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  354. }
  355. }
  356. ppnt++;
  357. };
  358. maxvma = (maxvma + 0xfffU) & ~0xfffU;
  359. minvma = minvma & ~0xffffU;
  360. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  361. if (!piclib)
  362. flags |= MAP_FIXED;
  363. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  364. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  365. if (_dl_mmap_check_error(status)) {
  366. _dl_fdprintf(2, "%s: can't map '/dev/zero'\n", _dl_progname);
  367. _dl_internal_error_number = DL_ERROR_MMAP_FAILED;
  368. _dl_close(infile);
  369. return NULL;
  370. };
  371. libaddr = (unsigned long) status;
  372. flags |= MAP_FIXED;
  373. /* Get the memory to store the library */
  374. ppnt = (elf_phdr *) & header[epnt->e_phoff];
  375. for (i = 0; i < epnt->e_phnum; i++) {
  376. if (ppnt->p_type == PT_LOAD) {
  377. /* See if this is a PIC library. */
  378. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  379. piclib = 0;
  380. /* flags |= MAP_FIXED; */
  381. }
  382. if (ppnt->p_flags & PF_W) {
  383. unsigned long map_size;
  384. char *cpnt;
  385. status = (char *) _dl_mmap((char *) ((piclib ? libaddr : 0) +
  386. (ppnt->p_vaddr & 0xfffff000)), (ppnt->p_vaddr & 0xfff)
  387. + ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags, infile,
  388. ppnt->p_offset & 0x7ffff000);
  389. if (_dl_mmap_check_error(status)) {
  390. _dl_fdprintf(2, "%s: can't map '%s'\n",
  391. _dl_progname, libname);
  392. _dl_internal_error_number = DL_ERROR_MMAP_FAILED;
  393. _dl_munmap((char *) libaddr, maxvma - minvma);
  394. _dl_close(infile);
  395. return NULL;
  396. };
  397. /* Pad the last page with zeroes. */
  398. cpnt = (char *) (status + (ppnt->p_vaddr & 0xfff) +
  399. ppnt->p_filesz);
  400. while (((unsigned long) cpnt) & 0xfff)
  401. *cpnt++ = 0;
  402. /* I am not quite sure if this is completely
  403. * correct to do or not, but the basic way that
  404. * we handle bss segments is that we mmap
  405. * /dev/zero if there are any pages left over
  406. * that are not mapped as part of the file */
  407. map_size = (ppnt->p_vaddr + ppnt->p_filesz + 0xfff) & 0xfffff000;
  408. if (map_size < ppnt->p_vaddr + ppnt->p_memsz)
  409. status = (char *) _dl_mmap((char *) map_size +
  410. (piclib ? libaddr : 0),
  411. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  412. LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS, -1, 0);
  413. } else
  414. status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & 0xfffff000)
  415. + (piclib ? libaddr : 0), (ppnt->p_vaddr & 0xfff) +
  416. ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags,
  417. infile, ppnt->p_offset & 0x7ffff000);
  418. if (_dl_mmap_check_error(status)) {
  419. _dl_fdprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
  420. _dl_internal_error_number = DL_ERROR_MMAP_FAILED;
  421. _dl_munmap((char *) libaddr, maxvma - minvma);
  422. _dl_close(infile);
  423. return NULL;
  424. };
  425. /* if(libaddr == 0 && piclib) {
  426. libaddr = (unsigned long) status;
  427. flags |= MAP_FIXED;
  428. }; */
  429. };
  430. ppnt++;
  431. };
  432. _dl_close(infile);
  433. /* For a non-PIC library, the addresses are all absolute */
  434. if (piclib) {
  435. dynamic_addr += (unsigned long) libaddr;
  436. }
  437. /*
  438. * OK, the ELF library is now loaded into VM in the correct locations
  439. * The next step is to go through and do the dynamic linking (if needed).
  440. */
  441. /* Start by scanning the dynamic section to get all of the pointers */
  442. if (!dynamic_addr) {
  443. _dl_internal_error_number = DL_ERROR_NODYNAMIC;
  444. _dl_fdprintf(2, "%s: '%s' is missing a dynamic section\n",
  445. _dl_progname, libname);
  446. return NULL;
  447. }
  448. dpnt = (Elf32_Dyn *) dynamic_addr;
  449. dynamic_size = dynamic_size / sizeof(Elf32_Dyn);
  450. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  451. for (i = 0; i < dynamic_size; i++) {
  452. if (dpnt->d_tag > DT_JMPREL) {
  453. dpnt++;
  454. continue;
  455. }
  456. dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  457. if (dpnt->d_tag == DT_TEXTREL || SVR4_BUGCOMPAT)
  458. dynamic_info[DT_TEXTREL] = 1;
  459. dpnt++;
  460. };
  461. /* If the TEXTREL is set, this means that we need to make the pages
  462. writable before we perform relocations. Do this now. They get set back
  463. again later. */
  464. if (dynamic_info[DT_TEXTREL]) {
  465. ppnt = (elf_phdr *) & header[epnt->e_phoff];
  466. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  467. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  468. _dl_mprotect((void *) ((piclib ? libaddr : 0) +
  469. (ppnt->p_vaddr & 0xfffff000)),
  470. (ppnt->p_vaddr & 0xfff) + (unsigned long) ppnt->p_filesz,
  471. PROT_READ | PROT_WRITE | PROT_EXEC);
  472. }
  473. }
  474. tpnt = _dl_add_elf_hash_table(libname, (char *) libaddr, dynamic_info,
  475. dynamic_addr, dynamic_size);
  476. tpnt->ppnt = (elf_phdr *) (tpnt->loadaddr + epnt->e_phoff);
  477. tpnt->n_phent = epnt->e_phnum;
  478. /*
  479. * OK, the next thing we need to do is to insert the dynamic linker into
  480. * the proper entry in the GOT so that the PLT symbols can be properly
  481. * resolved.
  482. */
  483. lpnt = (int *) dynamic_info[DT_PLTGOT];
  484. if (lpnt) {
  485. lpnt = (int *) (dynamic_info[DT_PLTGOT] + ((int) libaddr));
  486. INIT_GOT(lpnt, tpnt);
  487. };
  488. return tpnt;
  489. }
  490. /* Ugly, ugly. Some versions of the SVr4 linker fail to generate COPY
  491. relocations for global variables that are present both in the image and
  492. the shared library. Go through and do it manually. If the images
  493. are guaranteed to be generated by a trustworthy linker, then this
  494. step can be skipped. */
  495. int _dl_copy_fixups(struct dyn_elf *rpnt)
  496. {
  497. int goof = 0;
  498. struct elf_resolve *tpnt;
  499. if (rpnt->next)
  500. goof += _dl_copy_fixups(rpnt->next);
  501. else
  502. return 0;
  503. tpnt = rpnt->dyn;
  504. if (tpnt->init_flag & COPY_RELOCS_DONE)
  505. return goof;
  506. tpnt->init_flag |= COPY_RELOCS_DONE;
  507. #ifdef ELF_USES_RELOCA
  508. goof += _dl_parse_copy_information(rpnt,
  509. tpnt->dynamic_info[DT_RELA], tpnt->dynamic_info[DT_RELASZ], 0);
  510. #else
  511. goof += _dl_parse_copy_information(rpnt, tpnt->dynamic_info[DT_REL],
  512. tpnt->dynamic_info[DT_RELSZ], 0);
  513. #endif
  514. return goof;
  515. }