readelflib1.c 15 KB

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