readelflib1.c 15 KB

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