dl-elf.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 hard coded paths that follow (i.e before /lib and /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. /* Check in <install-dir>/usr/lib */
  211. pnt1 = UCLIBC_ROOT_DIR "/usr/lib/";
  212. pnt = mylibname;
  213. while (*pnt1)
  214. *pnt++ = *pnt1++;
  215. pnt1 = libname;
  216. while (*pnt1)
  217. *pnt++ = *pnt1++;
  218. *pnt++ = 0;
  219. tpnt1 = _dl_load_elf_shared_library(secure, mylibname, 0);
  220. if (tpnt1)
  221. return tpnt1;
  222. /* Check in <install-dir>/lib */
  223. pnt1 = UCLIBC_ROOT_DIR "/lib/";
  224. pnt = mylibname;
  225. while (*pnt1)
  226. *pnt++ = *pnt1++;
  227. pnt1 = libname;
  228. while (*pnt1)
  229. *pnt++ = *pnt1++;
  230. *pnt++ = 0;
  231. tpnt1 = _dl_load_elf_shared_library(secure, mylibname, 0);
  232. if (tpnt1)
  233. return tpnt1;
  234. goof:
  235. /* Well, we shot our wad on that one. All we can do now is punt */
  236. if (_dl_internal_error_number)
  237. _dl_error_number = _dl_internal_error_number;
  238. else
  239. _dl_error_number = DL_ERROR_NOFILE;
  240. return NULL;
  241. }
  242. /*
  243. * Read one ELF library into memory, mmap it into the correct locations and
  244. * add the symbol info to the symbol chain. Perform any relocations that
  245. * are required.
  246. */
  247. //extern _elf_rtbndr(void);
  248. struct elf_resolve *_dl_load_elf_shared_library(int secure,
  249. char *libname, int flag)
  250. {
  251. elfhdr *epnt;
  252. unsigned long dynamic_addr = 0;
  253. unsigned long dynamic_size = 0;
  254. Elf32_Dyn *dpnt;
  255. struct elf_resolve *tpnt;
  256. elf_phdr *ppnt;
  257. int piclib;
  258. char *status;
  259. int flags;
  260. char header[4096];
  261. unsigned long dynamic_info[24];
  262. int *lpnt;
  263. unsigned long libaddr;
  264. unsigned long minvma = 0xffffffff, maxvma = 0;
  265. int i;
  266. int infile;
  267. /* If this file is already loaded, skip this step */
  268. tpnt = _dl_check_hashed_files(libname);
  269. if (tpnt)
  270. return tpnt;
  271. /* If we are in secure mode (i.e. a setu/gid binary using LD_PRELOAD),
  272. we don't load the library if it isn't setuid. */
  273. if (secure) {
  274. struct kernel_stat st;
  275. if (_dl_stat(libname, &st) || !(st.st_mode & S_ISUID))
  276. return NULL;
  277. }
  278. libaddr = 0;
  279. infile = _dl_open(libname, O_RDONLY);
  280. if (infile < 0) {
  281. #if 0
  282. /*
  283. * NO! When we open shared libraries we may search several paths.
  284. * it is inappropriate to generate an error here.
  285. */
  286. _dl_fdprintf(2, "%s: can't open '%s'\n", _dl_progname, libname);
  287. #endif
  288. _dl_internal_error_number = DL_ERROR_NOFILE;
  289. return NULL;
  290. }
  291. _dl_read(infile, header, sizeof(header));
  292. epnt = (elfhdr *) header;
  293. if (epnt->e_ident[0] != 0x7f ||
  294. epnt->e_ident[1] != 'E' ||
  295. epnt->e_ident[2] != 'L' ||
  296. epnt->e_ident[3] != 'F')
  297. {
  298. _dl_fdprintf(2, "%s: '%s' is not an ELF file\n", _dl_progname,
  299. libname);
  300. _dl_internal_error_number = DL_ERROR_NOTELF;
  301. _dl_close(infile);
  302. return NULL;
  303. };
  304. if ((epnt->e_type != ET_DYN) || (epnt->e_machine != MAGIC1
  305. #ifdef MAGIC2
  306. && epnt->e_machine != MAGIC2
  307. #endif
  308. ))
  309. {
  310. _dl_internal_error_number =
  311. (epnt->e_type != ET_DYN ? DL_ERROR_NOTDYN : DL_ERROR_NOTMAGIC);
  312. _dl_fdprintf(2, "%s: '%s' is not an ELF executable for " ELF_TARGET
  313. "\n", _dl_progname, libname);
  314. _dl_close(infile);
  315. return NULL;
  316. };
  317. ppnt = (elf_phdr *) & header[epnt->e_phoff];
  318. piclib = 1;
  319. for (i = 0; i < epnt->e_phnum; i++) {
  320. if (ppnt->p_type == PT_DYNAMIC) {
  321. if (dynamic_addr)
  322. _dl_fdprintf(2, "%s: '%s' has more than one dynamic section\n",
  323. _dl_progname, libname);
  324. dynamic_addr = ppnt->p_vaddr;
  325. dynamic_size = ppnt->p_filesz;
  326. };
  327. if (ppnt->p_type == PT_LOAD) {
  328. /* See if this is a PIC library. */
  329. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  330. piclib = 0;
  331. minvma = ppnt->p_vaddr;
  332. }
  333. if (piclib && ppnt->p_vaddr < minvma) {
  334. minvma = ppnt->p_vaddr;
  335. }
  336. if (((unsigned long) ppnt->p_vaddr + ppnt->p_memsz) > maxvma) {
  337. maxvma = ppnt->p_vaddr + ppnt->p_memsz;
  338. }
  339. }
  340. ppnt++;
  341. };
  342. maxvma = (maxvma + 0xfffU) & ~0xfffU;
  343. minvma = minvma & ~0xffffU;
  344. flags = MAP_PRIVATE /*| MAP_DENYWRITE */ ;
  345. if (!piclib)
  346. flags |= MAP_FIXED;
  347. status = (char *) _dl_mmap((char *) (piclib ? 0 : minvma),
  348. maxvma - minvma, PROT_NONE, flags | MAP_ANONYMOUS, -1, 0);
  349. if (_dl_mmap_check_error(status)) {
  350. _dl_fdprintf(2, "%s: can't map '/dev/zero'\n", _dl_progname);
  351. _dl_internal_error_number = DL_ERROR_MMAP_FAILED;
  352. _dl_close(infile);
  353. return NULL;
  354. };
  355. libaddr = (unsigned long) status;
  356. flags |= MAP_FIXED;
  357. /* Get the memory to store the library */
  358. ppnt = (elf_phdr *) & header[epnt->e_phoff];
  359. for (i = 0; i < epnt->e_phnum; i++) {
  360. if (ppnt->p_type == PT_LOAD) {
  361. /* See if this is a PIC library. */
  362. if (i == 0 && ppnt->p_vaddr > 0x1000000) {
  363. piclib = 0;
  364. /* flags |= MAP_FIXED; */
  365. }
  366. if (ppnt->p_flags & PF_W) {
  367. unsigned long map_size;
  368. char *cpnt;
  369. status = (char *) _dl_mmap((char *) ((piclib ? libaddr : 0) +
  370. (ppnt->p_vaddr & 0xfffff000)), (ppnt->p_vaddr & 0xfff)
  371. + ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags, infile,
  372. ppnt->p_offset & 0x7ffff000);
  373. if (_dl_mmap_check_error(status)) {
  374. _dl_fdprintf(2, "%s: can't map '%s'\n",
  375. _dl_progname, libname);
  376. _dl_internal_error_number = DL_ERROR_MMAP_FAILED;
  377. _dl_munmap((char *) libaddr, maxvma - minvma);
  378. _dl_close(infile);
  379. return NULL;
  380. };
  381. /* Pad the last page with zeroes. */
  382. cpnt = (char *) (status + (ppnt->p_vaddr & 0xfff) +
  383. ppnt->p_filesz);
  384. while (((unsigned long) cpnt) & 0xfff)
  385. *cpnt++ = 0;
  386. /* I am not quite sure if this is completely
  387. * correct to do or not, but the basic way that
  388. * we handle bss segments is that we mmap
  389. * /dev/zero if there are any pages left over
  390. * that are not mapped as part of the file */
  391. map_size = (ppnt->p_vaddr + ppnt->p_filesz + 0xfff) & 0xfffff000;
  392. if (map_size < ppnt->p_vaddr + ppnt->p_memsz)
  393. status = (char *) _dl_mmap((char *) map_size +
  394. (piclib ? libaddr : 0),
  395. ppnt->p_vaddr + ppnt->p_memsz - map_size,
  396. LXFLAGS(ppnt->p_flags), flags | MAP_ANONYMOUS, -1, 0);
  397. } else
  398. status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & 0xfffff000)
  399. + (piclib ? libaddr : 0), (ppnt->p_vaddr & 0xfff) +
  400. ppnt->p_filesz, LXFLAGS(ppnt->p_flags), flags,
  401. infile, ppnt->p_offset & 0x7ffff000);
  402. if (_dl_mmap_check_error(status)) {
  403. _dl_fdprintf(2, "%s: can't map '%s'\n", _dl_progname, libname);
  404. _dl_internal_error_number = DL_ERROR_MMAP_FAILED;
  405. _dl_munmap((char *) libaddr, maxvma - minvma);
  406. _dl_close(infile);
  407. return NULL;
  408. };
  409. /* if(libaddr == 0 && piclib) {
  410. libaddr = (unsigned long) status;
  411. flags |= MAP_FIXED;
  412. }; */
  413. };
  414. ppnt++;
  415. };
  416. _dl_close(infile);
  417. /* For a non-PIC library, the addresses are all absolute */
  418. if (piclib) {
  419. dynamic_addr += (unsigned long) libaddr;
  420. }
  421. /*
  422. * OK, the ELF library is now loaded into VM in the correct locations
  423. * The next step is to go through and do the dynamic linking (if needed).
  424. */
  425. /* Start by scanning the dynamic section to get all of the pointers */
  426. if (!dynamic_addr) {
  427. _dl_internal_error_number = DL_ERROR_NODYNAMIC;
  428. _dl_fdprintf(2, "%s: '%s' is missing a dynamic section\n",
  429. _dl_progname, libname);
  430. return NULL;
  431. }
  432. dpnt = (Elf32_Dyn *) dynamic_addr;
  433. dynamic_size = dynamic_size / sizeof(Elf32_Dyn);
  434. _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  435. for (i = 0; i < dynamic_size; i++) {
  436. if (dpnt->d_tag > DT_JMPREL) {
  437. dpnt++;
  438. continue;
  439. }
  440. dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  441. if (dpnt->d_tag == DT_TEXTREL || SVR4_BUGCOMPAT)
  442. dynamic_info[DT_TEXTREL] = 1;
  443. dpnt++;
  444. };
  445. /* If the TEXTREL is set, this means that we need to make the pages
  446. writable before we perform relocations. Do this now. They get set back
  447. again later. */
  448. if (dynamic_info[DT_TEXTREL]) {
  449. ppnt = (elf_phdr *) & header[epnt->e_phoff];
  450. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  451. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  452. _dl_mprotect((void *) ((piclib ? libaddr : 0) +
  453. (ppnt->p_vaddr & 0xfffff000)),
  454. (ppnt->p_vaddr & 0xfff) + (unsigned long) ppnt->p_filesz,
  455. PROT_READ | PROT_WRITE | PROT_EXEC);
  456. }
  457. }
  458. tpnt = _dl_add_elf_hash_table(libname, (char *) libaddr, dynamic_info,
  459. dynamic_addr, dynamic_size);
  460. tpnt->ppnt = (elf_phdr *) (tpnt->loadaddr + epnt->e_phoff);
  461. tpnt->n_phent = epnt->e_phnum;
  462. /*
  463. * OK, the next thing we need to do is to insert the dynamic linker into
  464. * the proper entry in the GOT so that the PLT symbols can be properly
  465. * resolved.
  466. */
  467. lpnt = (int *) dynamic_info[DT_PLTGOT];
  468. if (lpnt) {
  469. lpnt = (int *) (dynamic_info[DT_PLTGOT] + ((int) libaddr));
  470. INIT_GOT(lpnt, tpnt);
  471. };
  472. return tpnt;
  473. }
  474. /* Ugly, ugly. Some versions of the SVr4 linker fail to generate COPY
  475. relocations for global variables that are present both in the image and
  476. the shared library. Go through and do it manually. If the images
  477. are guaranteed to be generated by a trustworthy linker, then this
  478. step can be skipped. */
  479. int _dl_copy_fixups(struct dyn_elf *rpnt)
  480. {
  481. int goof = 0;
  482. struct elf_resolve *tpnt;
  483. if (rpnt->next)
  484. goof += _dl_copy_fixups(rpnt->next);
  485. else
  486. return 0;
  487. tpnt = rpnt->dyn;
  488. if (tpnt->init_flag & COPY_RELOCS_DONE)
  489. return goof;
  490. tpnt->init_flag |= COPY_RELOCS_DONE;
  491. #ifdef ELF_USES_RELOCA
  492. goof += _dl_parse_copy_information(rpnt,
  493. tpnt->dynamic_info[DT_RELA], tpnt->dynamic_info[DT_RELASZ], 0);
  494. #else
  495. goof += _dl_parse_copy_information(rpnt, tpnt->dynamic_info[DT_REL],
  496. tpnt->dynamic_info[DT_RELSZ], 0);
  497. #endif
  498. return goof;
  499. }