readelflib1.c 16 KB

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